[PATCH] D143641: [MemorySSA] Recursively check if gep's pointer operand is a guaranteed loop invariant

luxufan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 12 22:19:01 PST 2023


StephenFan updated this revision to Diff 496842.
StephenFan added a comment.

Replace recursion as bounded iteration.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D143641/new/

https://reviews.llvm.org/D143641

Files:
  llvm/lib/Analysis/MemorySSA.cpp
  llvm/test/Analysis/MemorySSA/loop-invariant.ll


Index: llvm/test/Analysis/MemorySSA/loop-invariant.ll
===================================================================
--- llvm/test/Analysis/MemorySSA/loop-invariant.ll
+++ llvm/test/Analysis/MemorySSA/loop-invariant.ll
@@ -4,7 +4,7 @@
 ; %p2 is a loop invariant and the MemoryLoc of load instr and store inst in
 ; loop block are NoAlias
 ;
-; CHECK: MemoryUse(2)
+; CHECK: MemoryUse(liveOnEntry)
 ; CHECK: %val = load i32, ptr %p2
 define void @gep(ptr %ptr) {
 entry:
@@ -24,7 +24,8 @@
   br label %loop
 }
 
-; CHECK: MemoryUse(2)
+; CHECK: %x = phi i32 [ 0, %tmp ], [ %x.inc, %loop ]
+; CHECK-NEXT: MemoryUse(liveOnEntry)
 ; CHECK-NEXT: %val = load i32, ptr %p2
 define void @load_entry_block(ptr %ptr, ptr %addr) {
 entry:
Index: llvm/lib/Analysis/MemorySSA.cpp
===================================================================
--- llvm/lib/Analysis/MemorySSA.cpp
+++ llvm/lib/Analysis/MemorySSA.cpp
@@ -2625,21 +2625,20 @@
 }
 
 bool upward_defs_iterator::IsGuaranteedLoopInvariant(const Value *Ptr) const {
-  auto IsGuaranteedLoopInvariantBase = [](const Value *Ptr) {
+  // TODO: Increase limit?
+  const unsigned MaxGEPToLookup = 2;
+  for (unsigned I = 0; I < MaxGEPToLookup; I++) {
     Ptr = Ptr->stripPointerCasts();
-    if (!isa<Instruction>(Ptr))
-      return true;
-    return isa<AllocaInst>(Ptr);
-  };
-
-  Ptr = Ptr->stripPointerCasts();
-  if (auto *I = dyn_cast<Instruction>(Ptr)) {
-    if (I->getParent()->isEntryBlock())
+    if (auto *I = dyn_cast<Instruction>(Ptr)) {
+      if (isa<AllocaInst>(Ptr) || I->getParent()->isEntryBlock())
+        return true;
+      if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
+        if (!GEP->hasAllConstantIndices())
+          return false;
+        Ptr = GEP->getPointerOperand();
+      }
+    } else
       return true;
   }
-  if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
-    return IsGuaranteedLoopInvariantBase(GEP->getPointerOperand()) &&
-           GEP->hasAllConstantIndices();
-  }
-  return IsGuaranteedLoopInvariantBase(Ptr);
+  return false;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143641.496842.patch
Type: text/x-patch
Size: 2036 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230213/70b2b4f9/attachment.bin>


More information about the llvm-commits mailing list