diff --git a/llvm/lib/Analysis/ScalarEvolutionExpander.cpp b/llvm/lib/Analysis/ScalarEvolutionExpander.cpp --- a/llvm/lib/Analysis/ScalarEvolutionExpander.cpp +++ b/llvm/lib/Analysis/ScalarEvolutionExpander.cpp @@ -2138,6 +2138,10 @@ bool SCEVExpander::isHighCostExpansionHelper( const SCEV *S, Loop *L, const Instruction *At, int &BudgetRemaining, const TargetTransformInfo *TTI, SmallPtrSetImpl &Processed) { + // Was the cost of expansion of this expression already accounted for? + if (!Processed.insert(S).second) + return false; // We have already accounted for this expression. + // If we can find an existing value for this scev available at the point "At" // then consider the expression cheap. if (At && getRelatedExistingExpansion(S, At, L)) @@ -2159,8 +2163,6 @@ L, At, BudgetRemaining, TTI, Processed); } - if (!Processed.insert(S).second) - return false; if (auto *UDivExpr = dyn_cast(S)) { // If the divisor is a power of two and the SCEV type fits in a native