Index: ScalarEvolution.cpp =================================================================== --- ScalarEvolution.cpp +++ ScalarEvolution.cpp @@ -127,6 +127,11 @@ cl::desc("Threshold for inlining multiplication operands into a SCEV"), cl::init(1000)); +static cl::opt + MaxCompareDepth("scalar-evolution-max-compare-depth", cl::Hidden, + cl::desc("Maximum depth of recursive compare complexity"), + cl::init(10)); + //===----------------------------------------------------------------------===// // SCEV class definitions //===----------------------------------------------------------------------===// @@ -551,7 +556,7 @@ // than RHS, respectively. A three-way result allows recursive comparisons to be // more efficient. static int CompareSCEVComplexity(const LoopInfo *const LI, const SCEV *LHS, - const SCEV *RHS) { + const SCEV *RHS, unsigned Depth = 0) { // Fast-path: SCEVs are uniqued so we can do a quick equality check. if (LHS == RHS) return 0; @@ -561,6 +566,10 @@ if (LType != RType) return (int)LType - (int)RType; + // Limit recursion depth + if (Depth > MaxCompareDepth) + return 0; + // 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. @@ -605,7 +614,8 @@ // Lexicographically compare. for (unsigned i = 0; i != LNumOps; ++i) { - long X = CompareSCEVComplexity(LI, LA->getOperand(i), RA->getOperand(i)); + long X = CompareSCEVComplexity(LI, LA->getOperand(i), RA->getOperand(i), + Depth + 1); if (X != 0) return X; } @@ -628,7 +638,8 @@ for (unsigned i = 0; i != LNumOps; ++i) { if (i >= RNumOps) return 1; - long X = CompareSCEVComplexity(LI, LC->getOperand(i), RC->getOperand(i)); + long X = CompareSCEVComplexity(LI, LC->getOperand(i), RC->getOperand(i), + Depth + 1); if (X != 0) return X; } @@ -640,10 +651,10 @@ const SCEVUDivExpr *RC = cast(RHS); // Lexicographically compare udiv expressions. - long X = CompareSCEVComplexity(LI, LC->getLHS(), RC->getLHS()); + long X = CompareSCEVComplexity(LI, LC->getLHS(), RC->getLHS(), Depth + 1); if (X != 0) return X; - return CompareSCEVComplexity(LI, LC->getRHS(), RC->getRHS()); + return CompareSCEVComplexity(LI, LC->getRHS(), RC->getRHS(), Depth + 1); } case scTruncate: @@ -653,7 +664,7 @@ const SCEVCastExpr *RC = cast(RHS); // Compare cast expressions by operand. - return CompareSCEVComplexity(LI, LC->getOperand(), RC->getOperand()); + return CompareSCEVComplexity(LI, LC->getOperand(), RC->getOperand(), Depth + 1); } case scCouldNotCompute: