Generalise ((x1 ^ y1) | (x2 ^ y2)) == 0 transform to more than two pairs of variables https://github.com/llvm/llvm-project/issues/57831.
Depends D154384.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | ||
---|---|---|
2110 | Shouldn't we also be able to chase OrOp1 (if it doesn't match Xor). Figure it should be something along the lines of: if(match(Op0, Xor) && match(Op1, Xor)) { // Base Case } else if(match(Op0, Xor)) { // Chase Op1 } else if(match(Op1, Xor)) { // Chase Op1 } else { break; } | |
2110 | Err third condition should be "Chase Op0" |
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | ||
---|---|---|
2110 | Agree. Updated. |
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | ||
---|---|---|
2093 | AFAICT this condition never comes into play. You check the loop condition before reseting OrOperator every time. I'd say either make this a while(1) or make this BO && B0->getOpcode() == Instruction::Or | |
2116 | There could we a case where we have: (or (or (xor A, B), (xor C, D)), (or (xor E, F), (xor G, H))). |
llvm/test/Transforms/InstCombine/icmp-or.ll | ||
---|---|---|
415 | Can you 1) split the tests to a seperate patch (so we can see the diff this patch generates).
|
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | ||
---|---|---|
2088 | Maybe add as a todo, but this also works for sub (or implement in a follow up patch!) |
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | ||
---|---|---|
2088 | Thanks :) | |
2102 | Prefer not name these Or* as you expect to match them for Xor. Can you just make these Lhs/Rhs. | |
2111 | The match(Op, Xor) { emplace(XorOps) } else { WorkList.Add(Op) } could be a lambda for LHS/RHS. | |
2118 | At first I thought this should be !CmpValues.empty() so you can match something like: (or (or (xor A, B), (xor C, D)), (and E, F)) but realize we will visit the inner or and handle it there. Can you add a comment here (or above where you set Match to false) that such a case will inherently be handled so its okay to fail on any non-or/xor. | |
llvm/test/Transforms/InstCombine/icmp-or.ll | ||
415 | This is still outstanding. |
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | ||
---|---|---|
2091 | Unnecessary llvm:: prefix here and elsewhere. | |
2095 | Why does this one need to be handled separately? | |
2102 | These are the Lhs/Rhs of an Or though. Lhs/Rhs of the Xor are matched below. Not sure why this isn't doing match(CurrentValue, m_Or(m_Value(OrLhs), m_Value(OrRhs)) though. |
Updated implementation
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | ||
---|---|---|
2088 | Added TODO. | |
2091 | Updated. | |
2095 | To properly support lifetime of allocated operators we must allocate them using Builder, and Builder methods return Value *. | |
2102 | Updated implementation to use match. | |
2111 | Updated. | |
2118 | It seems that more easier approach is to clear CmpValues if we fail to match. | |
llvm/test/Transforms/InstCombine/icmp-or.ll | ||
415 | I moved tests into separate patch https://reviews.llvm.org/D154384. Should we merge it first so we can see the difference after this patch is applied ? |
General note, can you actually mark "done" the comments you have addressed rather than just replying to them?
llvm/test/Transforms/InstCombine/icmp-or.ll | ||
---|---|---|
415 | Do the following. Rebase this patch ontop of the test change (so instead of this patch adding a bunch of tests, we should see diffs generated). Then use the "Edit Related Revisions" option on phabricator and set this commit as the child of the test commit. |
llvm/test/Transforms/InstCombine/icmp-or.ll | ||
---|---|---|
488–489 | Can you postfix all the negative tests with '_fail'? |
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | ||
---|---|---|
2095 | You can do something like return replaceInstUsesWith(Or, LhsCmp) to convert the Value into Instruction for the return value. | |
2111 | As this is now a pretty complex fold, I'd suggest moving it into a separate function. That would allow you to use early return nullptr here. |
LG
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | ||
---|---|---|
1980 | nit: Omit braces for single-line if. |
I do not have commit access. Can you please land this one for me Maksim Kita <kitaetoya@gmail.com> ?
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | ||
---|---|---|
1980 | I plan to continue this optimization for 'sub' in my next patch, so I will fix nitpick in next patch. |
nit: Omit braces for single-line if.