So, i wouldn't call this *obviously* correct,
but i think i got it right this time :)
Roughly, we have
Op0*x^0 + Op1*x^1 + Op2*x^2 ...
where Op_{n} * x^{n} is called term, and n the degree of term.
Due to the way they are stored internally in SCEVAddRecExpr,
i believe we can have Op_{n} to be 0, so we should not charge for those.
I think it is most straight-forward to count the cost in 4 steps:
- First, count it the same way we counted scAddExpr, but be sure to skip terms with zero constants. Much like with add expr we will have one less addition than number of terms.
- Each non-constant term (term degree >= 1) requires a multiplication between the Op_{n} and x^{n}. But again, only charge for it if it is required - Op_{n} must not be 0 (no term) or 1 (no multiplication needed), and obviously don't charge constant terms (x^0 == 1).
- We must charge for all the x^0..x^{poly_degree} themselves. Since x^{poly_degree} is x * x * ... * x, i.e. poly_degree x'es multiplied, for final poly_degree term we again require poly_degree-1 multiplications. Note that all the x^{0}..x^{poly_degree-1} will be computed for the free along the way there.
- And finally, the operands themselves.
Here, much like with add/mul exprs, we really don't look for preexisting instructions..
This variable name is somewhat misleading, it should clearly say that it only counts non-one coefficients.