[polly] r335783 - [ScopHelper] Cache ScopExpander results.

Eli Friedman via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 27 13:35:02 PDT 2018


Author: efriedma
Date: Wed Jun 27 13:35:02 2018
New Revision: 335783

URL: http://llvm.org/viewvc/llvm-project?rev=335783&view=rev
Log:
[ScopHelper] Cache ScopExpander results.

The number of SCEV expressions is usually linear in the number of IR
instructions being modeled.  However, a naive SCEV visitor is not. For
an expression like x*x, "x" will be visited twice.  If x is itself an
expression like x*x, that will be visited twice, etc, and the overall
runtime is O(2^N) in the number of SCEV expressions.

To prevent this from happening, add a cache, so we only visit each SCEV
expression once.

Not sure this is the best solution. Maybe we can instead check whether
the SCEV is scop-invariant (in which case we never need to map the
value). But we don't have a utility for that at the moment.

Differential Revision: https://reviews.llvm.org/D47087


Modified:
    polly/trunk/lib/Support/ScopHelper.cpp

Modified: polly/trunk/lib/Support/ScopHelper.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/Support/ScopHelper.cpp?rev=335783&r1=335782&r2=335783&view=diff
==============================================================================
--- polly/trunk/lib/Support/ScopHelper.cpp (original)
+++ polly/trunk/lib/Support/ScopHelper.cpp Wed Jun 27 13:35:02 2018
@@ -249,6 +249,17 @@ struct ScopExpander : SCEVVisitor<ScopEx
     return Expander.expandCodeFor(E, Ty, I);
   }
 
+  const SCEV *visit(const SCEV *E) {
+    // Cache the expansion results for intermediate SCEV expressions. A SCEV
+    // expression can refer to an operand multiple times (e.g. "x*x), so
+    // a naive visitor takes exponential time.
+    if (SCEVCache.count(E))
+      return SCEVCache[E];
+    const SCEV *Result = SCEVVisitor::visit(E);
+    SCEVCache[E] = Result;
+    return Result;
+  }
+
 private:
   SCEVExpander Expander;
   ScalarEvolution &SE;
@@ -256,6 +267,7 @@ private:
   const Region &R;
   ValueMapT *VMap;
   BasicBlock *RTCBB;
+  DenseMap<const SCEV *, const SCEV *> SCEVCache;
 
   const SCEV *visitGenericInst(const SCEVUnknown *E, Instruction *Inst,
                                Instruction *IP) {




More information about the llvm-commits mailing list