From: Max Kazantsev Date: Wed, 6 Dec 2017 08:58:16 +0000 (+0000) Subject: [SCEV][NFC] Share value cache between SCEVs in GroupByComplexity X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3126a95a415482146b41cb2cbf319f9ab38f9609;p=llvm [SCEV][NFC] Share value cache between SCEVs in GroupByComplexity Current implementation of `compareSCEVComplexity` is being unreasonable with `SCEVUnknown`s: every time it sees one, it creates a new value cache and tries to prove equality of two values using it. This cache reallocates and gets lost from SCEV to SCEV. This patch changes this behavior: now we create one cache for all values and share it between SCEVs. Reviewed By: sanjoy Differential Revision: https://reviews.llvm.org/D40597 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@319880 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp index 8e60ca01c02..48c08b3e10f 100644 --- a/lib/Analysis/ScalarEvolution.cpp +++ b/lib/Analysis/ScalarEvolution.cpp @@ -549,10 +549,10 @@ bool SCEVUnknown::isOffsetOf(Type *&CTy, Constant *&FieldNo) const { /// Since we do not continue running this routine on expression trees once we /// have seen unequal values, there is no need to track them in the cache. static int -CompareValueComplexity(EquivalenceClasses &EqCache, +CompareValueComplexity(EquivalenceClasses &EqCacheValue, const LoopInfo *const LI, Value *LV, Value *RV, unsigned Depth) { - if (Depth > MaxValueCompareDepth || EqCache.isEquivalent(LV, RV)) + if (Depth > MaxValueCompareDepth || EqCacheValue.isEquivalent(LV, RV)) return 0; // Order pointer values after integer values. This helps SCEVExpander form @@ -612,14 +612,14 @@ CompareValueComplexity(EquivalenceClasses &EqCache, for (unsigned Idx : seq(0u, LNumOps)) { int Result = - CompareValueComplexity(EqCache, LI, LInst->getOperand(Idx), + CompareValueComplexity(EqCacheValue, LI, LInst->getOperand(Idx), RInst->getOperand(Idx), Depth + 1); if (Result != 0) return Result; } } - EqCache.unionSets(LV, RV); + EqCacheValue.unionSets(LV, RV); return 0; } @@ -628,6 +628,7 @@ CompareValueComplexity(EquivalenceClasses &EqCache, // more efficient. static int CompareSCEVComplexity( EquivalenceClasses &EqCacheSCEV, + EquivalenceClasses &EqCacheValue, const LoopInfo *const LI, const SCEV *LHS, const SCEV *RHS, DominatorTree &DT, unsigned Depth = 0) { // Fast-path: SCEVs are uniqued so we can do a quick equality check. @@ -649,9 +650,8 @@ static int CompareSCEVComplexity( const SCEVUnknown *LU = cast(LHS); const SCEVUnknown *RU = cast(RHS); - EquivalenceClasses EqCache; - int X = CompareValueComplexity(EqCache, LI, LU->getValue(), RU->getValue(), - Depth + 1); + int X = CompareValueComplexity(EqCacheValue, LI, LU->getValue(), + RU->getValue(), Depth + 1); if (X == 0) EqCacheSCEV.unionSets(LHS, RHS); return X; @@ -696,8 +696,9 @@ static int CompareSCEVComplexity( // Lexicographically compare. for (unsigned i = 0; i != LNumOps; ++i) { - int X = CompareSCEVComplexity(EqCacheSCEV, LI, LA->getOperand(i), - RA->getOperand(i), DT, Depth + 1); + int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, + LA->getOperand(i), RA->getOperand(i), DT, + Depth + 1); if (X != 0) return X; } @@ -718,8 +719,9 @@ static int CompareSCEVComplexity( return (int)LNumOps - (int)RNumOps; for (unsigned i = 0; i != LNumOps; ++i) { - int X = CompareSCEVComplexity(EqCacheSCEV, LI, LC->getOperand(i), - RC->getOperand(i), DT, Depth + 1); + int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, + LC->getOperand(i), RC->getOperand(i), DT, + Depth + 1); if (X != 0) return X; } @@ -732,12 +734,12 @@ static int CompareSCEVComplexity( const SCEVUDivExpr *RC = cast(RHS); // Lexicographically compare udiv expressions. - int X = CompareSCEVComplexity(EqCacheSCEV, LI, LC->getLHS(), RC->getLHS(), - DT, Depth + 1); + int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LC->getLHS(), + RC->getLHS(), DT, Depth + 1); if (X != 0) return X; - X = CompareSCEVComplexity(EqCacheSCEV, LI, LC->getRHS(), RC->getRHS(), DT, - Depth + 1); + X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LC->getRHS(), + RC->getRHS(), DT, Depth + 1); if (X == 0) EqCacheSCEV.unionSets(LHS, RHS); return X; @@ -750,8 +752,9 @@ static int CompareSCEVComplexity( const SCEVCastExpr *RC = cast(RHS); // Compare cast expressions by operand. - int X = CompareSCEVComplexity(EqCacheSCEV, LI, LC->getOperand(), - RC->getOperand(), DT, Depth + 1); + int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, + LC->getOperand(), RC->getOperand(), DT, + Depth + 1); if (X == 0) EqCacheSCEV.unionSets(LHS, RHS); return X; @@ -776,21 +779,22 @@ static void GroupByComplexity(SmallVectorImpl &Ops, LoopInfo *LI, DominatorTree &DT) { if (Ops.size() < 2) return; // Noop - EquivalenceClasses EqCache; + EquivalenceClasses EqCacheSCEV; + EquivalenceClasses EqCacheValue; if (Ops.size() == 2) { // This is the common case, which also happens to be trivially simple. // Special case it. const SCEV *&LHS = Ops[0], *&RHS = Ops[1]; - if (CompareSCEVComplexity(EqCache, LI, RHS, LHS, DT) < 0) + if (CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, RHS, LHS, DT) < 0) std::swap(LHS, RHS); return; } // Do the rough sort by complexity. std::stable_sort(Ops.begin(), Ops.end(), - [&EqCache, LI, &DT](const SCEV *LHS, const SCEV *RHS) { - return - CompareSCEVComplexity(EqCache, LI, LHS, RHS, DT) < 0; + [&](const SCEV *LHS, const SCEV *RHS) { + return CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, + LHS, RHS, DT) < 0; }); // Now that we are sorted by complexity, group elements of the same