This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] try to fold icmp with mismatched extended operands
ClosedPublic

Authored by spatel on Apr 25 2022, 2:11 PM.

Details

Summary

If a value is known to be non-negative and zexted, that's the same thing as sexted.

So for the purpose of looking past the casts with an icmp, treat it as if it was a sext:
https://alive2.llvm.org/ce/z/_BDsGV

This is necessary, but not enough to solve the motivating problem:
https://github.com/llvm/llvm-project/issues/55013

Diff Detail

Event Timeline

spatel created this revision.Apr 25 2022, 2:11 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 25 2022, 2:11 PM
spatel requested review of this revision.Apr 25 2022, 2:11 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 25 2022, 2:11 PM

Yes, I saw that. Putting flags on casts would probably change the way this is implemented, but it does not change the need for this transform? It seems similar to the recent bf09a925f21a - we'd end up using the ValueTracking call to annotate the zext somewhere else, then pattern match the 'zext nsw' here.

nikic added inline comments.Apr 25 2022, 2:45 PM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
4740
spatel added inline comments.Apr 25 2022, 3:28 PM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
4740

No - we are checking if Op1 (right-side of the cmp) is sext, then is Op0 source (zext on left-side) non-negative. The next clause is checking if Op1 is zext, then is Op1 source non-negative.

I coded it this way because we didn't actually test that Op1 is zext or sext (it could be some other cast). I can change that or add a code comment to better explain.

nikic added inline comments.Apr 26 2022, 2:29 AM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
4740

Okay, so we're using CastOp1 == sext here as a proxy for CastOp0 == zext, because we know that it's not the same opcode, but must be one of zext or sext. This does seem pretty roundabout.

spatel updated this revision to Diff 425191.Apr 26 2022, 5:34 AM
spatel marked 2 inline comments as done.

Patch updated:
Improve readability by clearly matching zext/sext and adding code comments.

nikic accepted this revision.Apr 26 2022, 9:22 AM

LGTM

This revision is now accepted and ready to land.Apr 26 2022, 9:22 AM
This revision was landed with ongoing or failed builds.Apr 26 2022, 11:36 AM
This revision was automatically updated to reflect the committed changes.