[PATCH] D120133: [instsimplify] Assume storage for byval args doesn't overlap allocas, globals, or other byval args

Philip Reames via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 18 08:13:02 PST 2022


reames created this revision.
reames added reviewers: nikic, fhahn, lebedev.ri.
Herald added subscribers: bollu, hiraditya, mcrosier.
reames requested review of this revision.
Herald added a project: LLVM.

This allows us to discharge many pointer comparisons based on byval arguments.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120133

Files:
  llvm/lib/Analysis/InstructionSimplify.cpp
  llvm/test/Transforms/InstSimplify/compare.ll


Index: llvm/test/Transforms/InstSimplify/compare.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/compare.ll
+++ llvm/test/Transforms/InstSimplify/compare.ll
@@ -2740,8 +2740,7 @@
 ; TODO: Never equal
 define i1 @byval_args_inequal(i32* byval(i32) %a, i32* byval(i32) %b) {
 ; CHECK-LABEL: @byval_args_inequal(
-; CHECK-NEXT:    [[RES:%.*]] = icmp ne i32* [[A:%.*]], [[B:%.*]]
-; CHECK-NEXT:    ret i1 [[RES]]
+; CHECK-NEXT:    ret i1 true
 ;
   %res = icmp ne i32* %a, %b
   ret i1 %res
@@ -2762,9 +2761,7 @@
 ; TODO: Never equal
 define i1 @test_byval_alloca_inequal(i32* byval(i32) %a) {
 ; CHECK-LABEL: @test_byval_alloca_inequal(
-; CHECK-NEXT:    [[B:%.*]] = alloca i32, align 4
-; CHECK-NEXT:    [[RES:%.*]] = icmp ne i32* [[A:%.*]], [[B]]
-; CHECK-NEXT:    ret i1 [[RES]]
+; CHECK-NEXT:    ret i1 true
 ;
   %b = alloca i32
   %res = icmp ne i32* %a, %b
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -2533,6 +2533,19 @@
   // So, we'll assume that two non-empty allocas have different addresses
   // for now.
   //
+
+  auto isByValArg = [](const Value *V) {
+    const Argument *A = dyn_cast<Argument>(V);
+    return A && A->hasByValAttr();
+  };
+
+  // Byval args are backed by store which does not overlap with each other,
+  // allocas, or globals.
+  if (isByValArg(V1))
+    return isa<AllocaInst>(V2) || isa<GlobalVariable>(V2) || isByValArg(V2);
+  if (isByValArg(V2))
+    return isa<AllocaInst>(V1) || isa<GlobalVariable>(V1) || isByValArg(V1);
+
   return isa<AllocaInst>(V1) &&
     (isa<AllocaInst>(V2) || isa<GlobalVariable>(V2));
 }
@@ -2638,7 +2651,7 @@
       ObjectSizeOpts Opts;
       Opts.EvalMode = ObjectSizeOpts::Mode::Min;
       Opts.NullIsUnknownSize =
-          NullPointerIsDefined(cast<AllocaInst>(LHS)->getFunction());
+        !CxtI || NullPointerIsDefined(CxtI->getFunction());
       if (getObjectSize(LHS, LHSSize, DL, TLI, Opts) &&
           getObjectSize(RHS, RHSSize, DL, TLI, Opts) &&
           !LHSOffset.isNegative() && !RHSOffset.isNegative() &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120133.409940.patch
Type: text/x-patch
Size: 2228 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220218/10201320/attachment.bin>


More information about the llvm-commits mailing list