While this resolves the regression from D73722 in llvm/test/Transforms/IndVarSimplify/exit_value_test2.ll,
this now regresses llvm/test/Transforms/IndVarSimplify/elim-extend.ll @nestedIV test,
we no longer can perform that expansion within default budget of 4, but require budget of 6.
That regression is being addressed by D73777.
The basic idea here is simple.
Op0, Op1, Op2 ... | | | \--+--/ | | | \---+---/
I.e. given N operands, we will have N-1 operations,
so we have to add cost of an add (mul) for every Op processed,
except the first one, plus we need to recurse into *every* Op.
I'm guessing there's already canonicalization that ensures we won't
have 1 operand in scMulExpr, and no 0 in scAddExpr/scMulExpr.
This is a very rough approximation for Mul, because of Bin Pow algorithm used in Expander. See https://github.com/llvm-mirror/llvm/blob/master/lib/Analysis/ScalarEvolutionExpander.cpp#L781. You might want to factor it in in the future, but what you did is fine for the 1st step. Please add a TODO for that.