]> granicus.if.org Git - llvm/commit
[InstCombine] Recognize and simplify three way comparison idioms
authorAnna Thomas <anna@azul.com>
Fri, 23 Jun 2017 13:41:45 +0000 (13:41 +0000)
committerAnna Thomas <anna@azul.com>
Fri, 23 Jun 2017 13:41:45 +0000 (13:41 +0000)
commit2cfdb4aa6ccbdbd2bf91a865e3db0e490eb690aa
treed262da9adf4c2207390969dea3cdc5d6e71c2583
parent2d829cd8cbeba2727c36365ad88d7a48a4fbbd9c
[InstCombine] Recognize and simplify three way comparison idioms

Summary:
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.)
We currently recognize one idiom on signed integer compare. In the future, we
plan to recognize and simplify other comparison idioms on
other signed/unsigned datatypes such as floats, vectors etc.

This is a resurrection of Philip Reames' original patch:
https://reviews.llvm.org/D19452

Reviewers: majnemer, apilipenko, reames, sanjoy, mkazantsev

Reviewed by: mkazantsev

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D34278

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@306100 91177308-0d34-0410-b5e6-96231b3b80d8
lib/Transforms/InstCombine/InstCombineCompares.cpp
lib/Transforms/InstCombine/InstCombineInternal.h
test/Transforms/InstCombine/compare-3way.ll [new file with mode: 0644]