This is an archive of the discontinued LLVM Phabricator instance.

InstCombine transform pattern "(A ^ B) | ~(A | B) -> ~(A & B)" added
Needs ReviewPublic

Authored by Jac1494 on Aug 21 2020, 12:35 PM.

Details

Summary

This patch implements transform for pattern "(A ^ B) | ~(A | B) -> ~(A & B)".

And this pattern is already implemented in gcc.

Diff Detail

Event Timeline

Jac1494 created this revision.Aug 21 2020, 12:35 PM
Jac1494 requested review of this revision.Aug 21 2020, 12:35 PM
xbolva00 added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2699

m_Specific ?

2703

Does this work for commuted case? @spatel

llvm/test/Transforms/InstCombine/or-and.ll
14

Add vector tests

lebedev.ri added a subscriber: lebedev.ri.
lebedev.ri added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2698–2699

Indeed, this doesn't ensure that it's the same A/B each time, so it's a miscompile

This should be

if(match(&I, m_c_Or(m_Xor(m_Value(A), m_Value(B)), m_Not(m_c_Or(m_Deferred(A), m_Deferred(B))))))
Jac1494 updated this revision to Diff 287169.Aug 22 2020, 3:32 AM

Addressed @lebedev.ri review comments.Thanks

Jac1494 added inline comments.Aug 22 2020, 3:35 AM
llvm/test/Transforms/InstCombine/or-and.ll
14

Could you please clarify , what you meant by "vector tests"..?

xbolva00 added inline comments.Aug 22 2020, 5:31 AM
llvm/test/Transforms/InstCombine/or-and.ll
14

instead of i32, try use vector type, eg. <4 x i32>

define <4 x i32> @vec( <4 x i32> %0, <4 x i32> %1) {

%3 = xor  <4 x i32> %0, %1
%4 = or  <4 x i32> %0, %1
%5 = xor  <4 x i32> %4, <i32 -1, i32 -1, i32 -1, i32 -1>
%6 = or  <4 x i32> %3, %5
ret  <4 x i32> %6

}

Jac1494 updated this revision to Diff 287191.Aug 22 2020, 7:44 AM

Addressed @xbolva00 review comments. Thanks

spatel added inline comments.Aug 22 2020, 7:44 AM
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2703

Off-topic for this review, but no it doesn't.
If I'm seeing that correctly, there are 16 potential commuted variants, and we only handle 2.
Looks like the motivating cases had a constant operand, so the more general patterns weren't noticed even though the matchers were generalized:
rG42af360

The 'and' variant of this pattern is also missing commutes. We could sprinkle around more "m_c_Xor" and probably get these, but it's not something instcombine can handle in the general case - it's a job for reassociation + CSE, and they seem to work as expected here. So I think the best thing is to add some code comments and/or restrict these to matching a pattern with a constant. Also, add some PhaseOrdering tests for all of those commute tests to make sure they are not escaping through "-O1".

lebedev.ri added inline comments.Aug 22 2020, 10:22 AM
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2703

Passing-by remark:
At some point i'd maybe like to take a more fundamental approach at this,
the rules for these logic ops are well-defined and known,
it's a shame the best we do is just add even more patternmatching..

Are you ok with this..?

Jac1494 updated this revision to Diff 288773.Aug 29 2020, 6:37 AM
Jac1494 edited the summary of this revision. (Show Details)

New pattern "~(A | B) | (A ^ B) -> ~(A & B)" and Test cases are added.

Jac1494 updated this revision to Diff 288787.Aug 29 2020, 10:02 AM
Jac1494 edited the summary of this revision. (Show Details)

Removed unnecessary code.

Gentle Ping !!

silvas resigned from this revision.Sep 22 2020, 7:25 PM
dexonsmith resigned from this revision.Oct 6 2020, 3:18 PM
lebedev.ri resigned from this revision.Jan 12 2023, 5:19 PM

This review seems to be stuck/dead, consider abandoning if no longer relevant.

Herald added a project: Restricted Project. · View Herald TranscriptJan 12 2023, 5:19 PM
Herald added a subscriber: StephenFan. · View Herald Transcript