You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JIT: mulshift optimization should happen in Lower #8668
Currently we have code that rewrites multiplication by constants into shift/lea sequences split between Morph and CodeGen. Ideally it would live in Lower so that CodeGen isn't performing optimizations (which complicates its contract), the target-specific optimization is handled by the back-end, and pre-Lower optimizations see the simpler multiplication tree.
This is more complicated than it seems. What we have now in morph can't be easily moved to lower, it interferes poorly with address mode formation and results in significant regressions.
Consider:
CSE picks up MUL(x, 3) and we now have STORE(cse0, MUL(x, 3)) and two MUL(LCL(cse0), 4)
Address mode builder use the two MULs to build LEAs that end up as memory operands
Net result: two MUL(x, 12) were transformed into a single MUL(x, 3) instruction. Saves 5 cycles and 1 instruction.
If this transform is moved to lower:
CSE picks up MUL(x, 12) and we now have STORE(cse0, MUL(x, 12)) and two LCL(cse0)
Lower transforms MUL(x, 12) into MUL(MUL(x, 3), 4)
Address mode builder can't see through the local var, grabs its bags and goes on holiday
Net result: two MUL(x, 12) were transformed into two instructions - SHL(LEA(x+x*2), 2). Saves 4 cycles and no instructions.
So by doing this we make things worse and it doesn't seem easy to solve this. Use SSA to build address modes so we can see through local vars? That's a more general problem of address mode building.
Granted, we can duplicate this optimization in lower. Tried, got 3 MUL(x, 10) that morph missed in the entire framework. Doesn't seem to be worth it.
Activity
mikedn commentedon Sep 16, 2017
This is more complicated than it seems. What we have now in morph can't be easily moved to lower, it interferes poorly with address mode formation and results in significant regressions.
Consider:
This generates:
MUL(x, 12)MUL(MUL(x, 3), 4)MUL(x, 3)and we now haveSTORE(cse0, MUL(x, 3))and twoMUL(LCL(cse0), 4)Net result: two
MUL(x, 12)were transformed into a singleMUL(x, 3)instruction. Saves 5 cycles and 1 instruction.If this transform is moved to lower:
MUL(x, 12)and we now haveSTORE(cse0, MUL(x, 12))and twoLCL(cse0)MUL(x, 12)intoMUL(MUL(x, 3), 4)Net result: two
MUL(x, 12)were transformed into two instructions -SHL(LEA(x+x*2), 2). Saves 4 cycles and no instructions.So by doing this we make things worse and it doesn't seem easy to solve this. Use SSA to build address modes so we can see through local vars? That's a more general problem of address mode building.
Granted, we can duplicate this optimization in lower. Tried, got 3
MUL(x, 10)that morph missed in the entire framework. Doesn't seem to be worth it.JosephTremoulet commentedon Sep 16, 2017
Ok. Thanks for digging in.