Index: lib/Analysis/ScalarEvolutionExpander.cpp =================================================================== --- lib/Analysis/ScalarEvolutionExpander.cpp +++ lib/Analysis/ScalarEvolutionExpander.cpp @@ -220,9 +220,6 @@ /// division. If so, update S with Factor divided out and return true. /// S need not be evenly divisible if a reasonable remainder can be /// computed. -/// TODO: When ScalarEvolution gets a SCEVSDivExpr, this can be made -/// unnecessary; in its place, just signed-divide Ops[i] by the scale and -/// check to see if the divide was folded. static bool FactorOutConstant(const SCEV *&S, const SCEV *&Remainder, const SCEV *Factor, ScalarEvolution &SE, const DataLayout &DL) { @@ -264,13 +261,15 @@ // Size is known, check if there is a constant operand which is a multiple // of the given factor. If so, we can factor it. const SCEVConstant *FC = cast(Factor); - if (const SCEVConstant *C = dyn_cast(M->getOperand(0))) - if (!C->getAPInt().srem(FC->getAPInt())) { - SmallVector NewMulOps(M->op_begin(), M->op_end()); - NewMulOps[0] = SE.getConstant(C->getAPInt().sdiv(FC->getAPInt())); - S = SE.getMulExpr(NewMulOps); - return true; - } + for (unsigned i = 0, e = M->getNumOperands(); i < e; ++i) { + if (const SCEVConstant *C = dyn_cast(M->getOperand(i))) + if (!C->getAPInt().srem(FC->getAPInt())) { + SmallVector NewMulOps(M->op_begin(), M->op_end()); + NewMulOps[i] = SE.getConstant(C->getAPInt().sdiv(FC->getAPInt())); + S = SE.getMulExpr(NewMulOps); + return true; + } + } } // In an AddRec, check if both start and step are divisible.