llvm-project/llvm/lib/Transforms/InstCombine
Anna Thomas d67165c93c [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

llvm-svn: 306100
2017-06-23 13:41:45 +00:00
..
CMakeLists.txt [CMake] NFC. Updating CMake dependency specifications 2016-11-17 04:36:50 +00:00
InstCombineAddSub.cpp [InstCombine] Pass a proper context instruction to all of the calls into InstSimplify 2017-06-09 03:21:29 +00:00
InstCombineAndOrXor.cpp [InstCombine] Add one use checks to or/and->xnor folding 2017-06-22 16:12:02 +00:00
InstCombineCalls.cpp [InstCombine] Add range metadata to cttz/ctlz/ctpop intrinsic calls based on known bits 2017-06-21 16:32:35 +00:00
InstCombineCasts.cpp [InstCombine] reverse bitcast + bitwise-logic canonicalization (PR33138) 2017-06-22 15:46:54 +00:00
InstCombineCompares.cpp [InstCombine] Recognize and simplify three way comparison idioms 2017-06-23 13:41:45 +00:00
InstCombineInternal.h [InstCombine] Recognize and simplify three way comparison idioms 2017-06-23 13:41:45 +00:00
InstCombineLoadStoreAlloca.cpp [ValueTracking] Replace all uses of ComputeSignBit with computeKnownBits. 2017-05-15 06:39:41 +00:00
InstCombineMulDivRem.cpp [InstCombine] Pass a proper context instruction to all of the calls into InstSimplify 2017-06-09 03:21:29 +00:00
InstCombinePHI.cpp [InstCombine] Pass a proper context instruction to all of the calls into InstSimplify 2017-06-09 03:21:29 +00:00
InstCombineSelect.cpp [InstCombine] Teach foldSelectICmpAndOr to recognize (select (icmp slt (trunc (X)), 0), Y, (or Y, C2)) 2017-06-22 16:23:30 +00:00
InstCombineShifts.cpp [InstCombine] lshr (sext iM X to iN), N-M --> zext (ashr X, min(N-M, M-1)) to iN 2017-06-12 14:23:43 +00:00
InstCombineSimplifyDemanded.cpp [InstCombine][InstSimplify] Use APInt::isNullValue/isOneValue to reduce compiled code for comparing APInts with 0 and 1. NFC 2017-06-07 07:40:37 +00:00
InstCombineVectorOps.cpp [InstCombine] Pass a proper context instruction to all of the calls into InstSimplify 2017-06-09 03:21:29 +00:00
InstructionCombining.cpp [InstCombine] Make sure AddReachableCodeToWorklist sets MadeIRChange 2017-06-19 18:00:27 +00:00
LLVMBuild.txt