Cost in loop-strength-reduce pass

Hello. May somebody explain how cost in loop strength reduction is calculated?

E.q. I received two different costs with 3 regs and with 1reg and imms. Why second combination in preffered?

(Some from debug log)

New best at 3 regs, with addrec cost 3.
Regs: {20000,+,-1}<%for.body> {@x,+,4}<%for.body> {@y,+,4}<%for.body>
New best at 3 regs, with addrec cost 1, plus 16 imm cost.
Regs: {0,+,-1}<%for.body> @x @y

Hello. May somebody explain how cost in loop strength reduction is calculated?
E.q. I received two different costs with 3 regs and with 1reg and imms. Why second combination in preffered?

(Some from debug log)
New best at 3 regs, with addrec cost 3.
Regs: {20000,+,-1}<nw><%for.body> {@x,+,4}<nsw><%for.body> {@y,+,4}<nsw><%for.body>
New best at 3 regs, with addrec cost 1, plus 16 imm cost.
Regs: {0,+,-1}<nw><%for.body> @x @y

The addrec cost is the cost of a recurrence whose value is stored in a register. So those registers need to be live throughout the entire loop. LSR tries to minimize those.

-Andy