[PATCH] D86845: [Attributor] Fix callsite check in AAUndefinedBehavior

Shinji Okumura via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 29 19:13:47 PDT 2020


okura created this revision.
Herald added subscribers: llvm-commits, kuter, danielkiss, uenoku, hiraditya.
Herald added a reviewer: uenoku.
Herald added a reviewer: jdoerfert.
Herald added a reviewer: homerdin.
Herald added a project: LLVM.
okura requested review of this revision.
Herald added a reviewer: sstefan1.
Herald added a reviewer: baziotis.
Herald added a subscriber: bbn.

This is the next patch of D86842 <https://reviews.llvm.org/D86842>
When we check `noundef` attribute violation at callsites, we do not have to require `nonnull` in the following two cases.

1. An argument is known to be simplified to undef
2. An argument is known to be dead


https://reviews.llvm.org/D86845

Files:
  llvm/lib/Transforms/IPO/AttributorAttributes.cpp
  llvm/test/Transforms/Attributor/undefined_behavior.ll


Index: llvm/test/Transforms/Attributor/undefined_behavior.ll
===================================================================
--- llvm/test/Transforms/Attributor/undefined_behavior.ll
+++ llvm/test/Transforms/Attributor/undefined_behavior.ll
@@ -1087,3 +1087,32 @@
   call void @callee_ptr_arg(i32* noundef undef)
   ret void
 }
+
+define i32 @argument_noundef(i32 noundef %c) {
+; IS__TUNIT____: Function Attrs: nofree nosync nounwind readnone willreturn
+; IS__TUNIT____-LABEL: define {{[^@]+}}@argument_noundef
+; IS__TUNIT____-SAME: (i32 noundef returned [[C:%.*]]) [[ATTR0]] {
+; IS__TUNIT____-NEXT:    ret i32 [[C]]
+;
+; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
+; IS__CGSCC____-LABEL: define {{[^@]+}}@argument_noundef
+; IS__CGSCC____-SAME: (i32 noundef returned [[C:%.*]]) [[ATTR0]] {
+; IS__CGSCC____-NEXT:    ret i32 [[C]]
+;
+  ret i32 %c
+}
+
+define i32 @violate_noundef_nonpointer() {
+; IS__TUNIT____: Function Attrs: nofree nosync nounwind readnone willreturn
+; IS__TUNIT____-LABEL: define {{[^@]+}}@violate_noundef_nonpointer
+; IS__TUNIT____-SAME: () [[ATTR0]] {
+; IS__TUNIT____-NEXT:    unreachable
+;
+; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
+; IS__CGSCC____-LABEL: define {{[^@]+}}@violate_noundef_nonpointer
+; IS__CGSCC____-SAME: () [[ATTR0]] {
+; IS__CGSCC____-NEXT:    unreachable
+;
+  %ret = call i32 @argument_noundef(i32 undef)
+  ret i32 %ret
+}
Index: llvm/lib/Transforms/IPO/AttributorAttributes.cpp
===================================================================
--- llvm/lib/Transforms/IPO/AttributorAttributes.cpp
+++ llvm/lib/Transforms/IPO/AttributorAttributes.cpp
@@ -2019,33 +2019,35 @@
         if (idx >= Callee->arg_size())
           break;
         Value *ArgVal = CB.getArgOperand(idx);
-        if (!ArgVal || !ArgVal->getType()->isPointerTy())
+        if (!ArgVal)
           continue;
+        // Here, we handle three cases.
+        //   (1) Not having a value means it is dead. (we can replace the value
+        //       with undef)
+        //   (2) Simplified to undef. The argument violate noundef attriubte.
+        //   (3) Simplified to null pointer where known to be nonnull.
+        //       The argument is a poison value and violate noundef attribute.
         IRPosition CalleeArgumentIRP = IRPosition::callsite_argument(CB, idx);
         if (!CalleeArgumentIRP.hasAttr({Attribute::NoUndef}))
           continue;
-        auto &NonNullAA = A.getAAFor<AANonNull>(*this, CalleeArgumentIRP,
-                                                /* TrackDependence */ false);
-        if (!NonNullAA.isKnownNonNull())
-          continue;
         const auto &ValueSimplifyAA = A.getAAFor<AAValueSimplify>(
             *this, IRPosition::value(*ArgVal), /* TrackDependence */ false);
-        Optional<Value *> SimplifiedVal =
-            ValueSimplifyAA.getAssumedSimplifiedValue(A);
-
         if (!ValueSimplifyAA.isKnown())
           continue;
-        // Here, we handle three cases.
-        //   (1) Not having a value means it is dead. (we can replace the value
-        //       with undef)
-        //   (2) Simplified to null pointer. The argument is a poison value and
-        //       violate noundef attribute.
-        //   (3) Simplified to undef. The argument violate noundef attriubte.
+        Optional<Value *> SimplifiedVal =
+            ValueSimplifyAA.getAssumedSimplifiedValue(A);
         if (!SimplifiedVal.hasValue() ||
-            isa<ConstantPointerNull>(*SimplifiedVal.getValue()) ||
             isa<UndefValue>(*SimplifiedVal.getValue())) {
           KnownUBInsts.insert(&I);
-          return true;
+          continue;
+        }
+        if (!ArgVal->getType()->isPointerTy())
+          continue;
+        auto &NonNullAA = A.getAAFor<AANonNull>(*this, CalleeArgumentIRP,
+                                                /* TrackDependence */ false);
+        if (NonNullAA.isKnownNonNull() &&
+            isa<ConstantPointerNull>(*SimplifiedVal.getValue())) {
+          KnownUBInsts.insert(&I);
         }
       }
       return true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D86845.288822.patch
Type: text/x-patch
Size: 4140 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200830/748dea75/attachment.bin>


More information about the llvm-commits mailing list