Do magic division optimization in lowering
authorMike Danes <onemihaid@hotmail.com>
Sun, 13 Nov 2016 09:16:41 +0000 (11:16 +0200)
committerMike Danes <onemihaid@hotmail.com>
Tue, 15 Nov 2016 06:04:21 +0000 (08:04 +0200)
commitbc4e600a838ba7daba7161806689a419aeef2f76
tree8b64861296f2c40cc264d786e092ca65eeb34fcf
parent67e8bd89504b7abd0ff8132680f9b160ff9211c4
Do magic division optimization in lowering

Currently this optimization is done during global morph. Global morph
takes place very early so cases that rely on constant propagation and
folding (e.g "int d = 3; return n / d;") aren't handled.

Also, the IR generated during morph is complex and unlikely to enable
further optimizations. Quite the contrary, the presence of GT_ASG and
GT_MULHI nodes blocks other optimizations (CSE, LICM currently).

The generated code is identical to the code generated by the morph
implementation with one exception: when 2 shifts are needed
(e.g. for x / 7) they are now computed independently:

mov eax, edx
sar eax, 2
shr edx, 31
add eax, edx

instead of:
mov eax, edx
sar eax, 2
mov edx, eax
shr edx, 31
add eax, edx

This results in shorter code and avoids creating an additional temp.
src/jit/lower.cpp