Index: lib/Analysis/GlobalsModRef.cpp =================================================================== --- lib/Analysis/GlobalsModRef.cpp +++ lib/Analysis/GlobalsModRef.cpp @@ -666,22 +666,30 @@ // non-addr-taken globals. continue; } + // Recurse through a limited number of selects, loads and PHIs. This is an + // arbitrary depth of 4, lower numbers could be used to fix compile time + // issues if needed, but this is generally expected to be only be important + // for small depths. + if (++Depth > 4) + return false; + if (auto *LI = dyn_cast(Input)) { // A pointer loaded from a global would have been captured, and we know // that the global is non-escaping, so no alias. if (isa(GetUnderlyingObject(LI->getPointerOperand(), DL))) continue; - // Otherwise, a load could come from anywhere, so bail. - return false; + auto *Ptr = GetUnderlyingObject(LI->getPointerOperand(), DL); + if (Ptr) { + // If the object this is loaded from is itself captured, then this + // pointer must have been captured too. + Inputs.push_back(Ptr); + continue; + } else { + // Otherwise, a load could come from anywhere, so bail. + return false; + } } - - // Recurse through a limited number of selects and PHIs. This is an - // arbitrary depth of 4, lower numbers could be used to fix compile time - // issues if needed, but this is generally expected to be only be important - // for small depths. - if (++Depth > 4) - return false; if (auto *SI = dyn_cast(Input)) { const Value *LHS = GetUnderlyingObject(SI->getTrueValue(), DL); const Value *RHS = GetUnderlyingObject(SI->getFalseValue(), DL); Index: test/Analysis/GlobalsModRef/nonescaping-noalias.ll =================================================================== --- test/Analysis/GlobalsModRef/nonescaping-noalias.ll +++ test/Analysis/GlobalsModRef/nonescaping-noalias.ll @@ -97,3 +97,20 @@ %v = load i32, i32* @g1 ret i32 %v } + +define i32 @test5(i32** %param) { +; Ensure that we can fold a store to a load of a global across a store to +; a parameter that has been dereferenced when the global is non-escaping. +; +; CHECK-LABEL: @test5( +; CHECK: %p = load i32* +; CHECK: store i32 42, i32* @g1 +; CHECK-NOT: load i32 +; CHECK: ret i32 42 +entry: + %p = load i32*, i32** %param + store i32 42, i32* @g1 + store i32 7, i32* %p + %v = load i32, i32* @g1 + ret i32 %v +}