Index: lib/Analysis/CaptureTracking.cpp =================================================================== --- lib/Analysis/CaptureTracking.cpp +++ lib/Analysis/CaptureTracking.cpp @@ -300,7 +300,7 @@ Worklist.push_back(&UU); } break; - case Instruction::ICmp: + case Instruction::ICmp: { // Don't count comparisons of a no-alias return value against null as // captures. This allows us to ignore comparisons of malloc results // with null, for example. @@ -309,11 +309,17 @@ if (CPN->getType()->getAddressSpace() == 0) if (isNoAliasCall(V->stripPointerCasts())) break; + // Comparison against value stored in global variable. + unsigned OtherIndex = (I->getOperand(0) == V)? 1:0; + auto *LI = dyn_cast(I->getOperand(OtherIndex)); + if (LI && isa(LI->getPointerOperand())) + break; // Otherwise, be conservative. There are crazy ways to capture pointers // using comparisons. if (Tracker->captured(U)) return; break; + } default: // Something else - be conservative and say it is captured. if (Tracker->captured(U)) Index: lib/Analysis/InstructionSimplify.cpp =================================================================== --- lib/Analysis/InstructionSimplify.cpp +++ lib/Analysis/InstructionSimplify.cpp @@ -21,6 +21,7 @@ #include "llvm/ADT/SetVector.h" #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/AliasAnalysis.h" +#include "llvm/Analysis/CaptureTracking.h" #include "llvm/Analysis/ConstantFolding.h" #include "llvm/Analysis/MemoryBuiltins.h" #include "llvm/Analysis/ValueTracking.h" @@ -2083,6 +2084,27 @@ !CmpInst::isTrueWhenEqual(Pred)); } + // True when V is neither NULL pointer nor a NULL value loaded + auto isNonNullValue = [](Value *V) { + if (auto *LI = dyn_cast(V)) + return LI->getMetadata(LLVMContext::MD_nonnull) != NULL; + return !isa(V); + }; + + // Fold Comparisons for non-escaping pointer even if the allocation call + // cannot be elided. + // Malloc comparison to NULL cannot be folded. + // The dynamic allocation call could be either of the operands. + Value *MI = nullptr; + if (isAllocLikeFn(LHS, TLI) && isNonNullValue(RHS)) + MI = LHS; + else if (isAllocLikeFn(RHS, TLI) && isNonNullValue(LHS)) + MI = RHS; + // FIXME: We should also fold the compare when the pointer escapes, but the + // compare dominates the pointer escape + if (MI && !PointerMayBeCaptured(MI, false, false)) + return ConstantInt::get(GetCompareTy(LHS), !CmpInst::isTrueWhenEqual(Pred)); + // Otherwise, fail. return nullptr; } Index: test/Transforms/InstCombine/compare-unescaped.ll =================================================================== --- test/Transforms/InstCombine/compare-unescaped.ll +++ test/Transforms/InstCombine/compare-unescaped.ll @@ -27,18 +27,36 @@ ; Although the %m is marked nocapture in the deopt operand in call to function f, ; we cannot remove the alloc site: call to malloc -; FIXME: The comparison should fold to false irrespective of whether the call to malloc can be elided or not +; The comparison should fold to false irrespective of whether the call to malloc can be elided or not declare void @f() -define i32 @compare_and_call_with_deopt() { +define i1 @compare_and_call_with_deopt() { ; CHECK-LABEL: compare_and_call_with_deopt %m = call i8* @malloc(i64 24) %bc = bitcast i8* %m to i32* + %lgp = load i32*, i32** @gp, align 8, !nonnull !0 + %cmp = icmp eq i32* %lgp, %bc + tail call void @f() [ "deopt"(i8* %m) ] + ret i1 %cmp +; CHECK: ret i1 false +} + +; FIXME: The comparison should fold to false since %m escapes (call to function escape) +; after the comparison. +declare void @escape(i8*) +define i1 @compare_and_call_after() { +; CHECK-LABEL: compare_and_call_after + %m = call i8* @malloc(i64 24) + %bc = bitcast i8* %m to i32* %lgp = load i32*, i32** @gp, align 8 %cmp = icmp eq i32* %bc, %lgp - %rt = zext i1 %cmp to i32 - tail call void @f() [ "deopt"(i8* %m) ] - ret i32 %rt -; CHECK: ret i32 %rt + br i1 %cmp, label %escape_call, label %just_return + +escape_call: + call void @escape(i8* %m) + ret i1 true + +just_return: + ret i1 %cmp } define i1 @compare_distinct_mallocs() { @@ -90,3 +108,5 @@ ; CHECK-NEXT: tail call void @f() [ "deopt"(i8* %m) ] ; CHECK-NEXT: ret i1 true } + +!0 = !{}