This is an archive of the discontinued LLVM Phabricator instance.

[InstSimplify] (~A & B) | ~(A | B) --> ~A with logical and
ClosedPublic

Authored by dmgreen on Nov 4 2022, 8:51 AM.

Details

Summary

According to https://alive2.llvm.org/ce/z/opsdrb, it is valid to convert (~A & B) | ~(A | B) --> ~A even if the And is a Logical And. This came up from the vector masking of predicated blocks.

Diff Detail

Event Timeline

dmgreen created this revision.Nov 4 2022, 8:51 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 4 2022, 8:51 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
dmgreen requested review of this revision.Nov 4 2022, 8:51 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 4 2022, 8:51 AM
nikic added inline comments.Nov 4 2022, 9:05 AM
llvm/lib/Analysis/InstructionSimplify.cpp
2267

m_c_LogicalAnd also matches m_c_And, no need for both.

2270

From a quick try, it looks like this can also be LogicalOr?

dmgreen added inline comments.Nov 4 2022, 9:23 AM
llvm/lib/Analysis/InstructionSimplify.cpp
2267

Yeah I had that originally, but according to the tests m_c_LogicalAnd only matches i1 types and the original pattern works for an Or of any size.

2270

Sounds good to me.

dmgreen updated this revision to Diff 473260.Nov 4 2022, 9:24 AM

Use LogicalOr

spatel added a comment.Nov 4 2022, 9:25 AM

I wonder if we could replace all of the matchers in this whole function. We can't leak poison when simplifying to an existing value? This will need a pile of tests...

Also, if any logic expression folds to a constant, then a transform must be safe to match with logical and/or sub-expressions too?

spatel accepted this revision.Nov 4 2022, 11:06 AM

LGTM - but please add a TODO comment that other (maybe all) of the similar transforms near here could be enhanced in the same way. And so we probably want to wrap the matchers or create a new matcher that works with both logical and bitwise ops.

This revision is now accepted and ready to land.Nov 4 2022, 11:06 AM
This revision was landed with ongoing or failed builds.Nov 7 2022, 2:03 AM
This revision was automatically updated to reflect the committed changes.