[PATCH] D111602: [SCEV][NFC] Tackle quadratic CT consumption in forgetValue

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 11 23:01:02 PDT 2021


mkazantsev created this revision.
mkazantsev added reviewers: nikic, reames.
Herald added subscribers: javed.absar, hiraditya.
mkazantsev requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Since forgetMemoizedResults now traverses SCEV users, calling it for each instruction
during forgetValue causes quadratic overall traversal cost. This patch fixes this by collecting
all SCEVs that are needed to be forgotten all together and forgetting them with single
query. This is NFC and should improve compile time.


https://reviews.llvm.org/D111602

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
@@ -7604,6 +7604,7 @@
   Worklist.push_back(I);
 
   SmallPtrSet<Instruction *, 8> Visited;
+  SmallVector<const SCEV *, 8> ToForget;
   while (!Worklist.empty()) {
     I = Worklist.pop_back_val();
     if (!Visited.insert(I).second)
@@ -7613,13 +7614,14 @@
       ValueExprMap.find_as(static_cast<Value *>(I));
     if (It != ValueExprMap.end()) {
       eraseValueFromMap(It->first);
-      forgetMemoizedResults(It->second);
+      ToForget.push_back(It->second);
       if (PHINode *PN = dyn_cast<PHINode>(I))
         ConstantEvolutionLoopExitValue.erase(PN);
     }
 
     PushDefUseChildren(I, Worklist);
   }
+  forgetMemoizedResults(ToForget);
 }
 
 void ScalarEvolution::forgetLoopDispositions(const Loop *L) {
@@ -12807,10 +12809,18 @@
 }
 
 void ScalarEvolution::forgetMemoizedResults(const SCEV *S) {
+  SmallVector<const SCEV *> ToForget;
+  ToForget.push_back(S);
+  forgetMemoizedResults(ToForget);
+}
+
+void ScalarEvolution::forgetMemoizedResults(ArrayRef<const SCEV *> SCEVs) {
   SmallPtrSet<const SCEV *, 16> Visited;
   SmallVector<const SCEV *, 16> Worklist;
-  Visited.insert(S);
-  Worklist.push_back(S);
+  for (auto *S : SCEVs)
+    if (Visited.insert(S).second)
+      Worklist.push_back(S);
+
   while (!Worklist.empty()) {
     const SCEV *Curr = Worklist.pop_back_val();
     auto Users = SCEVUsers.find(Curr);
Index: llvm/include/llvm/Analysis/ScalarEvolution.h
===================================================================
--- llvm/include/llvm/Analysis/ScalarEvolution.h
+++ llvm/include/llvm/Analysis/ScalarEvolution.h
@@ -1889,6 +1889,9 @@
   /// Drop memoized information computed for S.
   void forgetMemoizedResults(const SCEV *S);
 
+  /// Drop memoized information for all \p SCEVs.
+  void forgetMemoizedResults(ArrayRef<const SCEV *> SCEVs);
+
   void forgetMemoizedResultsImpl(const SCEV *S);
 
   /// Return an existing SCEV for V if there is one, otherwise return nullptr.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D111602.378883.patch
Type: text/x-patch
Size: 2123 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211012/3331bc23/attachment.bin>


More information about the llvm-commits mailing list