[PATCH] D70097: [SCEV] Add missing cache queries

Ehud Katz via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 24 11:14:17 PST 2019


ekatz updated this revision to Diff 230815.
ekatz retitled this revision from "[SCEV] Optimize SCEV cache usage" to "[SCEV] Add missing cache queries".
ekatz edited the summary of this revision.
ekatz added a comment.

Remove unrelated changes. Now only the missing cache queries are present in the patch.


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

https://reviews.llvm.org/D70097

Files:
  llvm/lib/Analysis/ScalarEvolution.cpp


Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -2454,6 +2454,15 @@
   // Limit recursion calls depth.
   if (Depth > MaxArithDepth || hasHugeExpression(Ops))
     return getOrCreateAddExpr(Ops, Flags);
+  else {
+    FoldingSetNodeID ID;
+    ID.AddInteger(scAddExpr);
+    for (const SCEV *Op : Ops)
+      ID.AddPointer(Op);
+    void *IP = nullptr;
+    if (SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
+      return S;
+  }
 
   // Okay, check to see if the same value occurs in the operand list more than
   // once.  If so, merge them together into an multiply expression.  Since we
@@ -2933,6 +2942,15 @@
   // Limit recursion calls depth.
   if (Depth > MaxArithDepth || hasHugeExpression(Ops))
     return getOrCreateMulExpr(Ops, Flags);
+  else {
+    FoldingSetNodeID ID;
+    ID.AddInteger(scMulExpr);
+    for (unsigned i = 0, e = Ops.size(); i != e; ++i)
+      ID.AddPointer(Ops[i]);
+    void *IP = nullptr;
+    if (SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
+      return S;
+  }
 
   // If there are any constants, fold them together.
   unsigned Idx = 0;
@@ -3197,6 +3215,14 @@
          getEffectiveSCEVType(RHS->getType()) &&
          "SCEVUDivExpr operand types don't match!");
 
+  FoldingSetNodeID ID;
+  ID.AddInteger(scUDivExpr);
+  ID.AddPointer(LHS);
+  ID.AddPointer(RHS);
+  void *IP = nullptr;
+  if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
+    return S;
+
   if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
     if (RHSC->getValue()->isOne())
       return LHS;                               // X udiv 1 --> x
@@ -3243,9 +3269,24 @@
                             AR->getLoop(), SCEV::FlagAnyWrap)) {
             const APInt &StartInt = StartC->getAPInt();
             const APInt &StartRem = StartInt.urem(StepInt);
-            if (StartRem != 0)
-              LHS = getAddRecExpr(getConstant(StartInt - StartRem), Step,
-                                  AR->getLoop(), SCEV::FlagNW);
+            if (StartRem != 0) {
+              const SCEV *NewLHS =
+                  getAddRecExpr(getConstant(StartInt - StartRem), Step,
+                                AR->getLoop(), SCEV::FlagNW);
+              if (LHS != NewLHS) {
+                LHS = NewLHS;
+
+                // Reset the ID to include the new LHS, and check if it is
+                // already cached.
+                ID.clear();
+                ID.AddInteger(scUDivExpr);
+                ID.AddPointer(LHS);
+                ID.AddPointer(RHS);
+                IP = nullptr;
+                if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
+                  return S;
+              }
+            }
           }
         }
       // (A*B)/C --> A*(B/C) if safe and B/C can be folded.
@@ -3310,12 +3351,6 @@
     }
   }
 
-  FoldingSetNodeID ID;
-  ID.AddInteger(scUDivExpr);
-  ID.AddPointer(LHS);
-  ID.AddPointer(RHS);
-  void *IP = nullptr;
-  if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
   SCEV *S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(SCEVAllocator),
                                              LHS, RHS);
   UniqueSCEVs.InsertNode(S, IP);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D70097.230815.patch
Type: text/x-patch
Size: 3308 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191124/5e88b34f/attachment.bin>


More information about the llvm-commits mailing list