[PATCH] D70749: [InstCombine] do not insert nonnull assumption for undef

Bob Haarman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 26 16:00:39 PST 2019


inglorion created this revision.
inglorion added reviewers: Tyker, spatel, rnk.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

D69477 <https://reviews.llvm.org/D69477> makes us keep non-null assumptions at the original call site
when sinking calls. It also does this when the non-null argument is
undef, which is incorrect - it can lead us to assume some code invokes
undefined behavior when it actually doesn't.  This change avoids
creating non-null assumptions for undef.

Fixes PR44154.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D70749

Files:
  llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
  llvm/test/Transforms/InstCombine/assume-replacing-call.ll


Index: llvm/test/Transforms/InstCombine/assume-replacing-call.ll
===================================================================
--- llvm/test/Transforms/InstCombine/assume-replacing-call.ll
+++ 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
Index: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -3194,7 +3194,8 @@
                                            ->getType()
                                            ->getPointerAddressSpace()))) {
         Value *V = CS->getArgOperand(Idx);
-
+        if (isa<UndefValue>(*V))
+          continue;
         Builder.SetInsertPoint(I->getParent(), I->getIterator());
         Builder.CreateAssumption(Builder.CreateIsNotNull(V));
       }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D70749.231144.patch
Type: text/x-patch
Size: 1737 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191127/c56c2112/attachment.bin>


More information about the llvm-commits mailing list