[PATCH] D133036: [InstCombine] Treat passing undef to noundef params as UB

Arthur Eubanks via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 31 11:26:47 PDT 2022


aeubanks created this revision.
Herald added subscribers: arphaman, hiraditya.
Herald added a project: All.
aeubanks requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D133036

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
  llvm/test/Transforms/InstCombine/call-undef.ll
  llvm/test/Transforms/InstCombine/out-of-bounds-indexes.ll


Index: llvm/test/Transforms/InstCombine/out-of-bounds-indexes.ll
===================================================================
--- llvm/test/Transforms/InstCombine/out-of-bounds-indexes.ll
+++ llvm/test/Transforms/InstCombine/out-of-bounds-indexes.ll
@@ -6,7 +6,7 @@
 ; CHECK-LABEL: @test_out_of_bounds(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[AND1:%.*]] = and i32 [[A:%.*]], 3
-; CHECK-NEXT:    tail call void @llvm.assume(i1 poison)
+; CHECK-NEXT:    store i1 true, ptr poison, align 1
 ; CHECK-NEXT:    ret i32 [[AND1]]
 ;
 entry:
@@ -20,7 +20,7 @@
 define i128 @test_non64bit(i128 %a) {
 ; CHECK-LABEL: @test_non64bit(
 ; CHECK-NEXT:    [[AND1:%.*]] = and i128 [[A:%.*]], 3
-; CHECK-NEXT:    tail call void @llvm.assume(i1 poison)
+; CHECK-NEXT:    store i1 true, ptr poison, align 1
 ; CHECK-NEXT:    ret i128 [[AND1]]
 ;
   %and1 = and i128 %a, 3
Index: llvm/test/Transforms/InstCombine/call-undef.ll
===================================================================
--- llvm/test/Transforms/InstCombine/call-undef.ll
+++ llvm/test/Transforms/InstCombine/call-undef.ll
@@ -6,7 +6,7 @@
 
 define void @test1() {
 ; CHECK-LABEL: @test1(
-; CHECK-NEXT:    call void @c(i32 undef)
+; CHECK-NEXT:    store i1 true, ptr poison, align 1
 ; CHECK-NEXT:    ret void
 ;
   call void @c(i32 undef)
@@ -15,7 +15,7 @@
 
 define void @test2() {
 ; CHECK-LABEL: @test2(
-; CHECK-NEXT:    call void @c(i32 poison)
+; CHECK-NEXT:    store i1 true, ptr poison, align 1
 ; CHECK-NEXT:    ret void
 ;
   call void @c(i32 poison)
@@ -24,7 +24,7 @@
 
 define void @test3() {
 ; CHECK-LABEL: @test3(
-; CHECK-NEXT:    call void @d(i32 noundef undef)
+; CHECK-NEXT:    store i1 true, ptr poison, align 1
 ; CHECK-NEXT:    ret void
 ;
   call void @d(i32 noundef undef)
@@ -33,7 +33,7 @@
 
 define void @test4() {
 ; CHECK-LABEL: @test4(
-; CHECK-NEXT:    call void @d(i32 noundef poison)
+; CHECK-NEXT:    store i1 true, ptr poison, align 1
 ; CHECK-NEXT:    ret void
 ;
   call void @d(i32 noundef poison)
Index: llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2900,6 +2900,16 @@
   return nullptr;
 }
 
+static bool callPassesUndefToNoUndefParam(CallBase &Call) {
+  for (unsigned I = 0; I < Call.arg_size(); ++I) {
+    if (Call.paramHasAttr(I, Attribute::NoUndef) &&
+        isa<UndefValue>(Call.getArgOperand(I))) {
+      return true;
+    }
+  }
+  return false;
+}
+
 bool InstCombinerImpl::annotateAnyAllocSite(CallBase &Call,
                                             const TargetLibraryInfo *TLI) {
   // Note: We only handle cases which can't be driven from generic attributes
@@ -3026,9 +3036,10 @@
 
   // Calling a null function pointer is undefined if a null address isn't
   // dereferenceable.
+  // Passing an undef/poison argument to a noundef parameter is undefined.
   if ((isa<ConstantPointerNull>(Callee) &&
        !NullPointerIsDefined(Call.getFunction())) ||
-      isa<UndefValue>(Callee)) {
+      isa<UndefValue>(Callee) || callPassesUndefToNoUndefParam(Call)) {
     // If Call does not return void then replaceInstUsesWith poison.
     // This allows ValueHandlers and custom metadata to adjust itself.
     if (!Call.getType()->isVoidTy())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133036.457035.patch
Type: text/x-patch
Size: 3361 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220831/2f3dc9eb/attachment.bin>


More information about the llvm-commits mailing list