diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -3186,18 +3186,19 @@ // If this instruction was providing nonnull guarantees insert assumptions for // nonnull call site arguments. if (auto CS = dyn_cast(I)) { - for (unsigned Idx = 0; Idx != CS->getNumArgOperands(); Idx++) + for (unsigned Idx = 0; Idx != CS->getNumArgOperands(); Idx++) { + Value *V = CS->getArgOperand(Idx); + if (isa(*V)) + continue; if (CS->paramHasAttr(Idx, Attribute::NonNull) || (CS->paramHasAttr(Idx, Attribute::Dereferenceable) && !llvm::NullPointerIsDefined(CS->getFunction(), - CS->getArgOperand(Idx) - ->getType() - ->getPointerAddressSpace()))) { - Value *V = CS->getArgOperand(Idx); - + V->getType() + ->getPointerAddressSpace()))) { Builder.SetInsertPoint(I->getParent(), I->getIterator()); Builder.CreateAssumption(Builder.CreateIsNotNull(V)); } + } } BasicBlock::iterator InsertPos = DestBlock->getFirstInsertionPt(); diff --git a/llvm/test/Transforms/InstCombine/assume-replacing-call.ll b/llvm/test/Transforms/InstCombine/assume-replacing-call.ll --- a/llvm/test/Transforms/InstCombine/assume-replacing-call.ll +++ b/llvm/test/Transforms/InstCombine/assume-replacing-call.ll @@ -183,6 +183,30 @@ ret i32 %retval.0 } +define i64 @test_sink_undef(i1 %c, i64 %x) { +; Check that we don't insert a nonnull assumption for undef. +; CHECK-LABEL: @test_sink_undef( +; CHECK-NEXT: entry: +; CHECK-NOT: @llvm.assume( +; CHECK: br i1 %c, label %ctrue, label %cend +; CHECK: ctrue: +; CHECK-NEXT: %foo = call i64 @func_test_nonnull(i8* undef) +; CHECK-NEXT: br label %cend +; CHECK: cend: +; CHECK-NEXT: %retval = phi i64 [ %x, %entry ], [ %foo, %ctrue ] +; CHECK-NEXT: ret i64 %retval +entry: + %foo = call i64 @func_test_nonnull(i8* undef) #2 + br i1 %c, label %ctrue, label %cend + +ctrue: + br label %cend + +cend: + %retval = phi i64 [%x, %entry], [%foo, %ctrue] + ret i64 %retval +} + declare i64 @func_test(i8* %0) #1 declare i64 @func_test_nonnull(i8* nonnull %0) #3