[PATCH] D19452: [instcombine] recognize three way comparison idioms

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Wed May 4 12:08:47 PDT 2016


ping

On 04/22/2016 08:37 PM, Philip Reames wrote:
> reames created this revision.
> reames added reviewers: majnemer, sanjoy, igor-laevsky.
> reames added a subscriber: llvm-commits.
> Herald added a subscriber: mcrosier.
>
> Many languages have a three way comparison idiom where comparing two values produces not a boolean, but a tri-state value.  Typical values (e.g. as used in the lcmp/fcmp bytecodes from Java) are -1 for less than, 0 for equality, and +1 for greater than.
>
> We actually do a great job already of converting three way comparisons into binary comparisons when the result produced has one a single use.  Unfortunately, such values can have more than one use, and in that case, our existing optimizations break down.
>
> The patch adds a peephole which converts a three-way compare + test idiom into a binary comparison on the original inputs.  It focused on replacing the test on the result of the three way compare and does nothing about removing the three way compare itself.  That's left to other optimizations (which do actually kick in commonly.)
>
> I specifically only matching a single select idiom at the moment.  We don't need to worry about phis since we canonicalize selects into phis.  We should handle (or canonicalize) a larger family of select idioms, but I wanted to start small and iterate.  Similarly, the current patch only adds support for signed compare idioms.  Future work could add unsigned idioms, and possibly floating point idioms.  I did try to generalize over the actual values returns by the three way compare so that the transform might kick on more than just the idiomatic three way comparison.
>
> http://reviews.llvm.org/D19452
>
> Files:
>    lib/Transforms/InstCombine/InstCombineCompares.cpp
>    test/Transforms/InstCombine/compare-3way.ll
>



More information about the llvm-commits mailing list