[PATCH] D82632: [NFCI][SCEV] getPointerBase(): de-recursify

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 26 02:08:48 PDT 2020


lebedev.ri created this revision.
lebedev.ri added reviewers: efriedma, mkazantsev, reames, nikic.
lebedev.ri added a project: LLVM.
Herald added a subscriber: hiraditya.
lebedev.ri added a child revision: D82633: [SCEV] Make SCEVAddExpr actually always return pointer type if there is pointer operand (PR46457).

This is boringly straight-forward, each iteration we see if
V is some expression that we can look into, and if it has
a single pointer operand, then set V to that operand
and repeat.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D82632

Files:
  llvm/include/llvm/Analysis/ScalarEvolution.h
  llvm/lib/Analysis/ScalarEvolution.cpp


Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -3948,27 +3948,32 @@
   return getUMinExpr(PromotedOps);
 }
 
-const SCEV *ScalarEvolution::getPointerBase(const SCEV *V) {
+const SCEV *ScalarEvolution::getPointerBase(const SCEV *V,
+                                            unsigned DepthLimit) {
   // A pointer operand may evaluate to a nonpointer expression, such as null.
   if (!V->getType()->isPointerTy())
     return V;
 
-  if (const SCEVCastExpr *Cast = dyn_cast<SCEVCastExpr>(V)) {
-    return getPointerBase(Cast->getOperand());
-  } else if (const SCEVNAryExpr *NAry = dyn_cast<SCEVNAryExpr>(V)) {
-    const SCEV *PtrOp = nullptr;
-    for (const SCEV *NAryOp : NAry->operands()) {
-      if (NAryOp->getType()->isPointerTy()) {
-        // Cannot find the base of an expression with multiple pointer operands.
-        if (PtrOp)
-          return V;
-        PtrOp = NAryOp;
+  for (unsigned Depth = 0; DepthLimit == 0 || Depth < DepthLimit; ++Depth) {
+    if (const SCEVCastExpr *Cast = dyn_cast<SCEVCastExpr>(V)) {
+      V = Cast->getOperand();
+    } else if (const SCEVNAryExpr *NAry = dyn_cast<SCEVNAryExpr>(V)) {
+      const SCEV *PtrOp = nullptr;
+      for (const SCEV *NAryOp : NAry->operands()) {
+        if (NAryOp->getType()->isPointerTy()) {
+          // Cannot find the base of an expression with multiple pointer ops.
+          if (PtrOp)
+            return V;
+          PtrOp = NAryOp;
+        }
       }
-    }
-    if (!PtrOp)
+      if (!PtrOp) // All operands were non-pointer.
+        return V;
+      V = PtrOp;
+    } else // Not something we can look into.
       return V;
-    return getPointerBase(PtrOp);
   }
+  // Depth cut-off reached, not allowed to look any furter.
   return V;
 }
 
Index: llvm/include/llvm/Analysis/ScalarEvolution.h
===================================================================
--- llvm/include/llvm/Analysis/ScalarEvolution.h
+++ llvm/include/llvm/Analysis/ScalarEvolution.h
@@ -653,8 +653,9 @@
   /// Transitively follow the chain of pointer-type operands until reaching a
   /// SCEV that does not have a single pointer operand. This returns a
   /// SCEVUnknown pointer for well-formed pointer-type expressions, but corner
-  /// cases do exist.
-  const SCEV *getPointerBase(const SCEV *V);
+  /// cases do exist. DepthLimit allows to limit amount of traversal done,
+  /// limit of 0 (default) imposes no restrictions.
+  const SCEV *getPointerBase(const SCEV *V, unsigned DepthLimit = 0);
 
   /// Return a SCEV expression for the specified value at the specified scope
   /// in the program.  The L value specifies a loop nest to evaluate the


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82632.273622.patch
Type: text/x-patch
Size: 2791 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200626/cb137bdc/attachment.bin>


More information about the llvm-commits mailing list