This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] matchThreeWayIntCompare(): commutativity awareness
ClosedPublic

Authored by lebedev.ri on Aug 22 2019, 10:58 AM.

Details

Summary

matchThreeWayIntCompare() looks for

select i1 (a == b),
       i32 Equal,
       i32 (select i1 (a < b), i32 Less, i32 Greater)

but both of these selects/compares can be in it's commuted form,
so out of 8 variants, only the two most basic ones is handled.
This fixes regression being introduced in D66232.

Diff Detail

Event Timeline

lebedev.ri created this revision.Aug 22 2019, 10:58 AM
lebedev.ri edited the summary of this revision. (Show Details)

Even more commutativity!

spatel added inline comments.Aug 23 2019, 5:05 AM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
2574–2575

I had not seen this API before. The header comment doesn't make sense:

For predicate of kind "is X or equal to 0"...

(what does zero constant have to do with this?)

...and I don't like the name either, but that's independent of this patch. :)

2587–2589

Simplify?

return MatchPat0();

But then, do we need the lambda at all?

lebedev.ri marked an inline comment as done.Aug 23 2019, 5:33 AM

Thank you for taking a look!

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
2574–2575

I added getFlippedStrictnessPredicateAndConstant() in rL368820.
I'm open for the suggestions on the name, it mirrors the fact that
there's CmpInst::getFlippedStrictnessPredicate(), which it uses,
but it also takes care of the constant, so i found the name quite fitting.

I'm also unsure to what header comment you are referring to, can you give a link?

2587–2589

I'm mimicking the original pattern (LHS of the diff)
The idea is - is this the only pattern that we could possibly have?
If no, then the lambda makes sense.
But i could inline it.

spatel added inline comments.Aug 23 2019, 6:30 AM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
2574–2575

I was looking at the existing/underlying function:
https://github.com/llvm-mirror/llvm/blob/master/include/llvm/IR/InstrTypes.h#L856

So my comment was only about stuff that is independent of this patch, so feel free to ignore.

2587–2589

I'd go with the simpler form for now. If we end up adding code/patterns, then that would be the time to add code/flexibility.

lebedev.ri marked 5 inline comments as done.

Inline lambda.

spatel accepted this revision.Aug 23 2019, 7:26 AM

LGTM

This revision is now accepted and ready to land.Aug 23 2019, 7:26 AM

Rebased, NFC.

Thank you for the review!

This revision was automatically updated to reflect the committed changes.