JIT: Transform arithmetic using distributive property#126852
JIT: Transform arithmetic using distributive property#126852BoyBaykiller wants to merge 3 commits intodotnet:mainfrom
Conversation
* add OR and AND are 'distributive' over themselves
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
src/coreclr/jit/morph.cpp
Outdated
| return tree; | ||
| } | ||
|
|
||
| if (((tree->gtFlags & GTF_PERSISTENT_SIDE_EFFECTS) != 0) || ((tree->gtFlags & GTF_ORDER_SIDEEFF) != 0)) |
There was a problem hiding this comment.
This could use the same optimization you are trying to apply😅... Probably handled by c++ though, so this is purely documentation ;-)
There was a problem hiding this comment.
Lol. A new level of self documenting code. I didn't even notice that, I copied this check from elsewhere. I guess this just goes to show how useful the optimization is. Unfortunately even with this PR it's still not handled : (
|
@EgorBo PTAL. Diffs are rather small, but nonetheless I believe it's a step in the right direction. I've written down some future work (which all improve diffs). I think the most interesting case for real-world code is arround bitwise ops. |
Generalization of #126070
Basically we weren't doing any simplification based on distributive property before. So transforming
((A op1 B) op2 (A op1 C)) => (A op1 (B op2 C)). And this adds at least some simple support. Example:Future work to handle more cases
When I write
(A * 10) + (A * 12)there is some previous transformation that gives((A * 5) << 1) + ((A * 3) << 2). In this formfgOptimizeDistributiveArithemticno longer picks it up. Ideally it should becomeA * 120. Either teachfgOptimizeDistributiveArithemticto see through this or run it before the mul-by-const transform:This should be handled too, by transforming into
((A & 4) | (A & 8)) != 0first perhaps. If I callfgMorphBlockStmtfrom optimizeBools it gets handled already:This is similiar to the previous point. We need something that changes order to enable this opt. So (A | B) | C becoming A | (B | C) in this case:
Lastly two general things.
I am currently not calling this for
GT_SUBbecause it's a mess. I want to rewrite the hugeswitch (oper)infgMorphSmpOpto if-statements.Lastly, I have to check for
GTF_ORDER_SIDEEFFto exclude volatile loads but this also causes many false negatives: https://discord.com/channels/143867839282020352/312132327348240384/1487221372899037234