Hi Eveyone,
This patch implements the following transformation
" ((X % Z) + (Y % Z)) % Z -> (X + Y) % Z "
Please help in reviewing it.
Regards,
Ankur.
Differential D5351
Added InstCombine transform for pattern " ((X % Z) + (Y % Z)) % Z -> (X + Y) % Z ". ankur29.garg on Sep 15 2014, 3:38 AM. Authored by
Details
Hi Eveyone, This patch implements the following transformation " ((X % Z) + (Y % Z)) % Z -> (X + Y) % Z " Please help in reviewing it. Regards,
Diff Detail Event TimelineComment Actions This patch is incorrect: $ ./alive.py < a.optOptimization: 1 => %6 = add %x, %y Done: 1 Example: Meaning that the optimized code is introducing undefined behaviour Nuno Citando Ankur Garg <ankur29.garg@samsung.com>:
Comment Actions Thanks nlopes for the comments. Thanks. Comment Actions Hi nlopes, Thanks. Comment Actions Hi David, Thanks. Comment Actions Sorry, still not correct: Pre: WillNotOverflowSignedAdd(%x, %y) %1 = srem %x, %z %2 = srem %y, %z %3 = add %1, %2 %r = srem %3, %z => %6 = add %x, %y %r = srem %6, %z Done: 1 Example: Comment Actions Hi nlopes, In the example you have stated, %x i2 = 3 since we are talking about signed integers, that is equivalent to %x i2 = -1 Similarly, %y i2 = -1 and %z i2 = -1. I am not sure how [%6 = add %x, %y] calculates to [%6 i2 = 2]. Shouldn't it be [%6 i2 = -2] ? Please clarify. Comment Actions I think our output is now slightly better: Example: %x i4 = 0xC (12, -4) %z i4 = 0xF (15, -1) %y i4 = 0xC (12, -4) %1 i4 = 0x0 (0) %2 i4 = 0x0 (0) %3 i4 = 0x0 (0) %6 i4 = 0x8 (8, -8) Source value: 0x0 (0) Target value: undef The idea is that %6 is INT_MIN and %z=-1. And so we get 'srem INT_MIN, -1', which is undefined behavior per the manual (http://llvm.org/docs/LangRef.html#srem-instruction). Comment Actions Thanks for clarifying that. I understand now, what that undefined behavior is for. Another thing, in these examples, all three inputs, %x, %y, & %z are known. That is, they are constants. I think such cases wouldn't make it to the transformation. When I was testing the transformation, I tested it with these inputs, the part of the code where the transformation takes place was never reached. Thanks. Comment Actions So the counterexample shows concrete, constant, values, yes. But that doesn't mean that you need to have these constants in the IR. Comment Actions Oh, thanks for clarifying that. I will work on the comments and resubmit the transformation. Thanks. |
Why are you mutating the LHS of I? This is unsafe if it has any other users.