[PATCH] D98899: [DAE] Adjust param/arg attributes when changing parameter to undef

Guozhi Wei via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 18 14:29:10 PDT 2021


Carrot created this revision.
Carrot added a reviewer: spatel.
Herald added a subscriber: hiraditya.
Carrot requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

In DeadArgumentElimination pass, if a function's argument is never used, corresponding caller's parameter can be changed to undef. If the param/arg has attribute noundef or other related attributes, LLVM LangRef(https://llvm.org/docs/LangRef.html#parameter-attributes) says its behavior is undefined. SimplifyCFG(D97244 <https://reviews.llvm.org/D97244>) takes advantage of this behavior and does bad transformation on valid code.

To avoid this undefined behavior when change caller's parameter to undef, this patch removes noundef attribute and other attributes imply noundef on param/arg.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D98899

Files:
  llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp
  llvm/test/Transforms/DeadArgElim/NoundefAttrs.ll
  llvm/test/Transforms/InstCombine/unused-nonnull.ll


Index: llvm/test/Transforms/InstCombine/unused-nonnull.ll
===================================================================
--- llvm/test/Transforms/InstCombine/unused-nonnull.ll
+++ llvm/test/Transforms/InstCombine/unused-nonnull.ll
@@ -37,7 +37,7 @@
 
 define i32 @compute(i8* noundef nonnull %ptr, i32 %x) #1 {
 ; CHECK-LABEL: define {{[^@]+}}@compute
-; CHECK-SAME: (i8* nocapture noundef nonnull readnone [[PTR:%.*]], i32 returned [[X:%.*]]) local_unnamed_addr #1
+; CHECK-SAME: (i8* nocapture readnone [[PTR:%.*]], i32 returned [[X:%.*]]) local_unnamed_addr #1
 ; CHECK-NEXT:    ret i32 [[X]]
 ;
   ret i32 %x
Index: llvm/test/Transforms/DeadArgElim/NoundefAttrs.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/DeadArgElim/NoundefAttrs.ll
@@ -0,0 +1,18 @@
+; RUN: opt -deadargelim -S < %s | FileCheck %s
+
+; If caller is changed to pass in undef, noundef and related attributes
+; should be deleted.
+
+
+; CHECK:   define i64 @bar(i64* %0, i64 %1)
+define i64 @bar(i64* nonnull dereferenceable(8) %0, i64 %1) {
+entry:
+  %2 = add i64 %1, 8
+  ret i64 %2
+}
+
+define i64 @foo(i64* %p, i64 %v) {
+; CHECK:   %retval = call i64 @bar(i64* undef, i64 %v)
+  %retval = call i64 @bar(i64* nonnull dereferenceable(8) %p, i64 %v)
+  ret i64 %retval
+}
Index: llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp
===================================================================
--- llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp
+++ llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp
@@ -295,6 +295,10 @@
         Changed = true;
       }
       UnusedArgs.push_back(Arg.getArgNo());
+      Arg.removeAttr(Attribute::NoUndef);
+      Arg.removeAttr(Attribute::Dereferenceable);
+      Arg.removeAttr(Attribute::DereferenceableOrNull);
+      Arg.removeAttr(Attribute::NonNull);
     }
   }
 
@@ -312,6 +316,11 @@
 
       Value *Arg = CB->getArgOperand(ArgNo);
       CB->setArgOperand(ArgNo, UndefValue::get(Arg->getType()));
+      CB->removeParamAttr(ArgNo, Attribute::NoUndef);
+      CB->removeParamAttr(ArgNo, Attribute::Dereferenceable);
+      CB->removeParamAttr(ArgNo, Attribute::DereferenceableOrNull);
+      CB->removeParamAttr(ArgNo, Attribute::NonNull);
+
       ++NumArgumentsReplacedWithUndef;
       Changed = true;
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98899.331684.patch
Type: text/x-patch
Size: 2313 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210318/e400e05e/attachment.bin>


More information about the llvm-commits mailing list