Index: llvm/lib/Analysis/ScalarEvolution.cpp =================================================================== --- llvm/lib/Analysis/ScalarEvolution.cpp +++ llvm/lib/Analysis/ScalarEvolution.cpp @@ -689,11 +689,14 @@ // Return negative, zero, or positive, if LHS is less than, equal to, or greater // than RHS, respectively. A three-way result allows recursive comparisons to be // more efficient. -static int CompareSCEVComplexity( - EquivalenceClasses &EqCacheSCEV, - EquivalenceClasses &EqCacheValue, - const LoopInfo *const LI, const SCEV *LHS, const SCEV *RHS, - DominatorTree &DT, unsigned Depth = 0) { +// If the max analysis depth was reached, return None, assuming we do not know +// if +// they are equivalent for sure. +static Optional +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. if (LHS == RHS) return 0; @@ -703,8 +706,12 @@ if (LType != RType) return (int)LType - (int)RType; - if (Depth > MaxSCEVCompareDepth || EqCacheSCEV.isEquivalent(LHS, RHS)) + if (EqCacheSCEV.isEquivalent(LHS, RHS)) return 0; + + if (Depth > MaxSCEVCompareDepth) + return None; + // Aside from the getSCEVType() ordering, the particular ordering // isn't very important except that it's beneficial to be consistent, // so that (a + b) and (b + a) don't end up as different expressions. @@ -759,9 +766,9 @@ // Lexicographically compare. for (unsigned i = 0; i != LNumOps; ++i) { - int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, - LA->getOperand(i), RA->getOperand(i), DT, - Depth + 1); + auto X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, + LA->getOperand(i), RA->getOperand(i), DT, + Depth + 1); if (X != 0) return X; } @@ -784,9 +791,9 @@ return (int)LNumOps - (int)RNumOps; for (unsigned i = 0; i != LNumOps; ++i) { - int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, - LC->getOperand(i), RC->getOperand(i), DT, - Depth + 1); + auto X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, + LC->getOperand(i), RC->getOperand(i), DT, + Depth + 1); if (X != 0) return X; } @@ -799,8 +806,8 @@ const SCEVUDivExpr *RC = cast(RHS); // Lexicographically compare udiv expressions. - int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LC->getLHS(), - RC->getLHS(), DT, Depth + 1); + auto X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LC->getLHS(), + RC->getLHS(), DT, Depth + 1); if (X != 0) return X; X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LC->getRHS(), @@ -818,9 +825,9 @@ const SCEVCastExpr *RC = cast(RHS); // Compare cast expressions by operand. - int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, - LC->getOperand(), RC->getOperand(), DT, - Depth + 1); + auto X = + CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LC->getOperand(), + RC->getOperand(), DT, Depth + 1); if (X == 0) EqCacheSCEV.unionSets(LHS, RHS); return X; @@ -847,19 +854,25 @@ EquivalenceClasses EqCacheSCEV; EquivalenceClasses EqCacheValue; + + // Whether LHS has provably less complexity than RHS. + auto IsLessComplex = [&](const SCEV *LHS, const SCEV *RHS) { + auto Complexity = + CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LHS, RHS, DT); + return Complexity && *Complexity < 0; + }; 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(EqCacheSCEV, EqCacheValue, LI, RHS, LHS, DT) < 0) + if (IsLessComplex(RHS, LHS)) std::swap(LHS, RHS); return; } // Do the rough sort by complexity. llvm::stable_sort(Ops, [&](const SCEV *LHS, const SCEV *RHS) { - return CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LHS, RHS, DT) < - 0; + return IsLessComplex(LHS, RHS); }); // Now that we are sorted by complexity, group elements of the same