blob: 1ef4acfb058c4961c874d6bd1150bbf64a6e8864 [file] [log] [blame]
Chris Lattner2188e402010-01-04 07:37:31 +00001//===- InstCombineCompares.cpp --------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the visitICmp and visitFCmp functions.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carrutha9174582015-01-22 05:25:13 +000014#include "InstCombineInternal.h"
Matt Arsenault55e73122015-01-06 15:50:59 +000015#include "llvm/ADT/APSInt.h"
Silviu Barangaf29dfd32016-01-15 15:52:05 +000016#include "llvm/ADT/SetVector.h"
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +000017#include "llvm/ADT/Statistic.h"
Eli Friedman911e12f2011-07-20 21:57:23 +000018#include "llvm/Analysis/ConstantFolding.h"
Chris Lattner2188e402010-01-04 07:37:31 +000019#include "llvm/Analysis/InstructionSimplify.h"
20#include "llvm/Analysis/MemoryBuiltins.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000021#include "llvm/Analysis/TargetLibraryInfo.h"
22#include "llvm/Analysis/VectorUtils.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000023#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/DataLayout.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000025#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000027#include "llvm/IR/PatternMatch.h"
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +000028#include "llvm/Support/Debug.h"
Craig Topperb45eabc2017-04-26 16:39:58 +000029#include "llvm/Support/KnownBits.h"
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +000030
Chris Lattner2188e402010-01-04 07:37:31 +000031using namespace llvm;
32using namespace PatternMatch;
33
Chandler Carruth964daaa2014-04-22 02:55:47 +000034#define DEBUG_TYPE "instcombine"
35
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +000036// How many times is a select replaced by one of its operands?
37STATISTIC(NumSel, "Number of select opts");
38
Chris Lattner98457102011-02-10 05:23:05 +000039
Sanjay Pateld93c4c02016-09-15 18:22:25 +000040static ConstantInt *extractElement(Constant *V, Constant *Idx) {
Chris Lattner2188e402010-01-04 07:37:31 +000041 return cast<ConstantInt>(ConstantExpr::getExtractElement(V, Idx));
42}
43
Sanjay Pateld93c4c02016-09-15 18:22:25 +000044static bool hasAddOverflow(ConstantInt *Result,
Chris Lattner2188e402010-01-04 07:37:31 +000045 ConstantInt *In1, ConstantInt *In2,
46 bool IsSigned) {
Chris Lattnerb1a15122011-07-15 06:08:15 +000047 if (!IsSigned)
Chris Lattner2188e402010-01-04 07:37:31 +000048 return Result->getValue().ult(In1->getValue());
Chris Lattnerb1a15122011-07-15 06:08:15 +000049
50 if (In2->isNegative())
51 return Result->getValue().sgt(In1->getValue());
52 return Result->getValue().slt(In1->getValue());
Chris Lattner2188e402010-01-04 07:37:31 +000053}
54
Sanjay Patel5f0217f2016-06-05 16:46:18 +000055/// Compute Result = In1+In2, returning true if the result overflowed for this
56/// type.
Sanjay Pateld93c4c02016-09-15 18:22:25 +000057static bool addWithOverflow(Constant *&Result, Constant *In1,
Chris Lattner2188e402010-01-04 07:37:31 +000058 Constant *In2, bool IsSigned = false) {
59 Result = ConstantExpr::getAdd(In1, In2);
60
Chris Lattner229907c2011-07-18 04:54:35 +000061 if (VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
Chris Lattner2188e402010-01-04 07:37:31 +000062 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
63 Constant *Idx = ConstantInt::get(Type::getInt32Ty(In1->getContext()), i);
Sanjay Pateld93c4c02016-09-15 18:22:25 +000064 if (hasAddOverflow(extractElement(Result, Idx),
65 extractElement(In1, Idx),
66 extractElement(In2, Idx),
Chris Lattner2188e402010-01-04 07:37:31 +000067 IsSigned))
68 return true;
69 }
70 return false;
71 }
72
Sanjay Pateld93c4c02016-09-15 18:22:25 +000073 return hasAddOverflow(cast<ConstantInt>(Result),
Chris Lattner2188e402010-01-04 07:37:31 +000074 cast<ConstantInt>(In1), cast<ConstantInt>(In2),
75 IsSigned);
76}
77
Sanjay Pateld93c4c02016-09-15 18:22:25 +000078static bool hasSubOverflow(ConstantInt *Result,
Chris Lattner2188e402010-01-04 07:37:31 +000079 ConstantInt *In1, ConstantInt *In2,
80 bool IsSigned) {
Chris Lattnerb1a15122011-07-15 06:08:15 +000081 if (!IsSigned)
Chris Lattner2188e402010-01-04 07:37:31 +000082 return Result->getValue().ugt(In1->getValue());
Jim Grosbach129c52a2011-09-30 18:09:53 +000083
Chris Lattnerb1a15122011-07-15 06:08:15 +000084 if (In2->isNegative())
85 return Result->getValue().slt(In1->getValue());
86
87 return Result->getValue().sgt(In1->getValue());
Chris Lattner2188e402010-01-04 07:37:31 +000088}
89
Sanjay Patel5f0217f2016-06-05 16:46:18 +000090/// Compute Result = In1-In2, returning true if the result overflowed for this
91/// type.
Sanjay Pateld93c4c02016-09-15 18:22:25 +000092static bool subWithOverflow(Constant *&Result, Constant *In1,
Chris Lattner2188e402010-01-04 07:37:31 +000093 Constant *In2, bool IsSigned = false) {
94 Result = ConstantExpr::getSub(In1, In2);
95
Chris Lattner229907c2011-07-18 04:54:35 +000096 if (VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
Chris Lattner2188e402010-01-04 07:37:31 +000097 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
98 Constant *Idx = ConstantInt::get(Type::getInt32Ty(In1->getContext()), i);
Sanjay Pateld93c4c02016-09-15 18:22:25 +000099 if (hasSubOverflow(extractElement(Result, Idx),
100 extractElement(In1, Idx),
101 extractElement(In2, Idx),
Chris Lattner2188e402010-01-04 07:37:31 +0000102 IsSigned))
103 return true;
104 }
105 return false;
106 }
107
Sanjay Pateld93c4c02016-09-15 18:22:25 +0000108 return hasSubOverflow(cast<ConstantInt>(Result),
Chris Lattner2188e402010-01-04 07:37:31 +0000109 cast<ConstantInt>(In1), cast<ConstantInt>(In2),
110 IsSigned);
111}
112
Balaram Makam569eaec2016-05-04 21:32:14 +0000113/// Given an icmp instruction, return true if any use of this comparison is a
114/// branch on sign bit comparison.
115static bool isBranchOnSignBitCheck(ICmpInst &I, bool isSignBit) {
116 for (auto *U : I.users())
117 if (isa<BranchInst>(U))
118 return isSignBit;
119 return false;
120}
121
Sanjay Patel5f0217f2016-06-05 16:46:18 +0000122/// Given an exploded icmp instruction, return true if the comparison only
123/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if the
124/// result of the comparison is true when the input value is signed.
Sanjay Patel79263662016-08-21 15:07:45 +0000125static bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS,
Chris Lattner2188e402010-01-04 07:37:31 +0000126 bool &TrueIfSigned) {
Sanjay Patel5f0217f2016-06-05 16:46:18 +0000127 switch (Pred) {
Chris Lattner2188e402010-01-04 07:37:31 +0000128 case ICmpInst::ICMP_SLT: // True if LHS s< 0
129 TrueIfSigned = true;
Craig Topper73ba1c82017-06-07 07:40:37 +0000130 return RHS.isNullValue();
Chris Lattner2188e402010-01-04 07:37:31 +0000131 case ICmpInst::ICMP_SLE: // True if LHS s<= RHS and RHS == -1
132 TrueIfSigned = true;
Sanjay Patel79263662016-08-21 15:07:45 +0000133 return RHS.isAllOnesValue();
Chris Lattner2188e402010-01-04 07:37:31 +0000134 case ICmpInst::ICMP_SGT: // True if LHS s> -1
135 TrueIfSigned = false;
Sanjay Patel79263662016-08-21 15:07:45 +0000136 return RHS.isAllOnesValue();
Chris Lattner2188e402010-01-04 07:37:31 +0000137 case ICmpInst::ICMP_UGT:
138 // True if LHS u> RHS and RHS == high-bit-mask - 1
139 TrueIfSigned = true;
Sanjay Patel79263662016-08-21 15:07:45 +0000140 return RHS.isMaxSignedValue();
Jim Grosbach129c52a2011-09-30 18:09:53 +0000141 case ICmpInst::ICMP_UGE:
Chris Lattner2188e402010-01-04 07:37:31 +0000142 // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc)
143 TrueIfSigned = true;
Craig Topperbcfd2d12017-04-20 16:56:25 +0000144 return RHS.isSignMask();
Chris Lattner2188e402010-01-04 07:37:31 +0000145 default:
146 return false;
147 }
148}
149
Arnaud A. de Grandmaison3ee88e82013-03-25 11:47:38 +0000150/// Returns true if the exploded icmp can be expressed as a signed comparison
151/// to zero and updates the predicate accordingly.
152/// The signedness of the comparison is preserved.
Sanjay Patel5b112842016-08-18 14:59:14 +0000153/// TODO: Refactor with decomposeBitTestICmp()?
154static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C) {
Sanjay Patel5f0217f2016-06-05 16:46:18 +0000155 if (!ICmpInst::isSigned(Pred))
Arnaud A. de Grandmaison9c383d62013-03-25 09:48:49 +0000156 return false;
157
Craig Topper73ba1c82017-06-07 07:40:37 +0000158 if (C.isNullValue())
Sanjay Patel5f0217f2016-06-05 16:46:18 +0000159 return ICmpInst::isRelational(Pred);
Arnaud A. de Grandmaison9c383d62013-03-25 09:48:49 +0000160
Craig Topper73ba1c82017-06-07 07:40:37 +0000161 if (C.isOneValue()) {
Sanjay Patel5f0217f2016-06-05 16:46:18 +0000162 if (Pred == ICmpInst::ICMP_SLT) {
163 Pred = ICmpInst::ICMP_SLE;
Arnaud A. de Grandmaison9c383d62013-03-25 09:48:49 +0000164 return true;
Arnaud A. de Grandmaison9c383d62013-03-25 09:48:49 +0000165 }
Sanjay Patel5b112842016-08-18 14:59:14 +0000166 } else if (C.isAllOnesValue()) {
Sanjay Patel5f0217f2016-06-05 16:46:18 +0000167 if (Pred == ICmpInst::ICMP_SGT) {
168 Pred = ICmpInst::ICMP_SGE;
Arnaud A. de Grandmaison9c383d62013-03-25 09:48:49 +0000169 return true;
Arnaud A. de Grandmaison9c383d62013-03-25 09:48:49 +0000170 }
Arnaud A. de Grandmaison3ee88e82013-03-25 11:47:38 +0000171 }
Arnaud A. de Grandmaison9c383d62013-03-25 09:48:49 +0000172
173 return false;
174}
175
Sanjay Patel5f0217f2016-06-05 16:46:18 +0000176/// Given a signed integer type and a set of known zero and one bits, compute
177/// the maximum and minimum values that could have the specified known zero and
178/// known one bits, returning them in Min/Max.
Craig Topperb45eabc2017-04-26 16:39:58 +0000179/// TODO: Move to method on KnownBits struct?
180static void computeSignedMinMaxValuesFromKnownBits(const KnownBits &Known,
Sanjay Patel5f0217f2016-06-05 16:46:18 +0000181 APInt &Min, APInt &Max) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000182 assert(Known.getBitWidth() == Min.getBitWidth() &&
183 Known.getBitWidth() == Max.getBitWidth() &&
Chris Lattner2188e402010-01-04 07:37:31 +0000184 "KnownZero, KnownOne and Min, Max must have equal bitwidth.");
Craig Topperb45eabc2017-04-26 16:39:58 +0000185 APInt UnknownBits = ~(Known.Zero|Known.One);
Chris Lattner2188e402010-01-04 07:37:31 +0000186
187 // The minimum value is when all unknown bits are zeros, EXCEPT for the sign
188 // bit if it is unknown.
Craig Topperb45eabc2017-04-26 16:39:58 +0000189 Min = Known.One;
190 Max = Known.One|UnknownBits;
Jim Grosbach129c52a2011-09-30 18:09:53 +0000191
Chris Lattner2188e402010-01-04 07:37:31 +0000192 if (UnknownBits.isNegative()) { // Sign bit is unknown
Craig Topper24db6b82017-04-28 16:58:05 +0000193 Min.setSignBit();
194 Max.clearSignBit();
Chris Lattner2188e402010-01-04 07:37:31 +0000195 }
196}
197
Sanjay Patel5f0217f2016-06-05 16:46:18 +0000198/// Given an unsigned integer type and a set of known zero and one bits, compute
199/// the maximum and minimum values that could have the specified known zero and
200/// known one bits, returning them in Min/Max.
Craig Topperb45eabc2017-04-26 16:39:58 +0000201/// TODO: Move to method on KnownBits struct?
202static void computeUnsignedMinMaxValuesFromKnownBits(const KnownBits &Known,
Chris Lattner2188e402010-01-04 07:37:31 +0000203 APInt &Min, APInt &Max) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000204 assert(Known.getBitWidth() == Min.getBitWidth() &&
205 Known.getBitWidth() == Max.getBitWidth() &&
Chris Lattner2188e402010-01-04 07:37:31 +0000206 "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth.");
Craig Topperb45eabc2017-04-26 16:39:58 +0000207 APInt UnknownBits = ~(Known.Zero|Known.One);
Jim Grosbach129c52a2011-09-30 18:09:53 +0000208
Chris Lattner2188e402010-01-04 07:37:31 +0000209 // The minimum value is when the unknown bits are all zeros.
Craig Topperb45eabc2017-04-26 16:39:58 +0000210 Min = Known.One;
Chris Lattner2188e402010-01-04 07:37:31 +0000211 // The maximum value is when the unknown bits are all ones.
Craig Topperb45eabc2017-04-26 16:39:58 +0000212 Max = Known.One|UnknownBits;
Chris Lattner2188e402010-01-04 07:37:31 +0000213}
214
Sanjay Patel5f0217f2016-06-05 16:46:18 +0000215/// This is called when we see this pattern:
Chris Lattner2188e402010-01-04 07:37:31 +0000216/// cmp pred (load (gep GV, ...)), cmpcst
Sanjay Patel5f0217f2016-06-05 16:46:18 +0000217/// where GV is a global variable with a constant initializer. Try to simplify
218/// this into some simple computation that does not need the load. For example
Chris Lattner2188e402010-01-04 07:37:31 +0000219/// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3".
220///
221/// If AndCst is non-null, then the loaded value is masked with that constant
Sanjay Patel5f0217f2016-06-05 16:46:18 +0000222/// before doing the comparison. This handles cases like "A[i]&4 == 0".
Sanjay Patel43395062016-07-21 18:07:40 +0000223Instruction *InstCombiner::foldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
224 GlobalVariable *GV,
225 CmpInst &ICI,
226 ConstantInt *AndCst) {
Chris Lattnerfe741762012-01-31 02:55:06 +0000227 Constant *Init = GV->getInitializer();
228 if (!isa<ConstantArray>(Init) && !isa<ConstantDataArray>(Init))
Craig Topperf40110f2014-04-25 05:29:35 +0000229 return nullptr;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000230
Chris Lattnerfe741762012-01-31 02:55:06 +0000231 uint64_t ArrayElementCount = Init->getType()->getArrayNumElements();
Davide Italiano2133bf52017-02-07 17:56:50 +0000232 // Don't blow up on huge arrays.
233 if (ArrayElementCount > MaxArraySizeForCombine)
234 return nullptr;
Jim Grosbach129c52a2011-09-30 18:09:53 +0000235
Chris Lattner2188e402010-01-04 07:37:31 +0000236 // There are many forms of this optimization we can handle, for now, just do
237 // the simple index into a single-dimensional array.
238 //
239 // Require: GEP GV, 0, i {{, constant indices}}
240 if (GEP->getNumOperands() < 3 ||
241 !isa<ConstantInt>(GEP->getOperand(1)) ||
242 !cast<ConstantInt>(GEP->getOperand(1))->isZero() ||
243 isa<Constant>(GEP->getOperand(2)))
Craig Topperf40110f2014-04-25 05:29:35 +0000244 return nullptr;
Chris Lattner2188e402010-01-04 07:37:31 +0000245
246 // Check that indices after the variable are constants and in-range for the
247 // type they index. Collect the indices. This is typically for arrays of
248 // structs.
249 SmallVector<unsigned, 4> LaterIndices;
Jim Grosbach129c52a2011-09-30 18:09:53 +0000250
Chris Lattnerfe741762012-01-31 02:55:06 +0000251 Type *EltTy = Init->getType()->getArrayElementType();
Chris Lattner2188e402010-01-04 07:37:31 +0000252 for (unsigned i = 3, e = GEP->getNumOperands(); i != e; ++i) {
253 ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(i));
Craig Topperf40110f2014-04-25 05:29:35 +0000254 if (!Idx) return nullptr; // Variable index.
Jim Grosbach129c52a2011-09-30 18:09:53 +0000255
Chris Lattner2188e402010-01-04 07:37:31 +0000256 uint64_t IdxVal = Idx->getZExtValue();
Craig Topperf40110f2014-04-25 05:29:35 +0000257 if ((unsigned)IdxVal != IdxVal) return nullptr; // Too large array index.
Jim Grosbach129c52a2011-09-30 18:09:53 +0000258
Chris Lattner229907c2011-07-18 04:54:35 +0000259 if (StructType *STy = dyn_cast<StructType>(EltTy))
Chris Lattner2188e402010-01-04 07:37:31 +0000260 EltTy = STy->getElementType(IdxVal);
Chris Lattner229907c2011-07-18 04:54:35 +0000261 else if (ArrayType *ATy = dyn_cast<ArrayType>(EltTy)) {
Craig Topperf40110f2014-04-25 05:29:35 +0000262 if (IdxVal >= ATy->getNumElements()) return nullptr;
Chris Lattner2188e402010-01-04 07:37:31 +0000263 EltTy = ATy->getElementType();
264 } else {
Craig Topperf40110f2014-04-25 05:29:35 +0000265 return nullptr; // Unknown type.
Chris Lattner2188e402010-01-04 07:37:31 +0000266 }
Jim Grosbach129c52a2011-09-30 18:09:53 +0000267
Chris Lattner2188e402010-01-04 07:37:31 +0000268 LaterIndices.push_back(IdxVal);
269 }
Jim Grosbach129c52a2011-09-30 18:09:53 +0000270
Chris Lattner2188e402010-01-04 07:37:31 +0000271 enum { Overdefined = -3, Undefined = -2 };
272
273 // Variables for our state machines.
Jim Grosbach129c52a2011-09-30 18:09:53 +0000274
Chris Lattner2188e402010-01-04 07:37:31 +0000275 // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form
276 // "i == 47 | i == 87", where 47 is the first index the condition is true for,
277 // and 87 is the second (and last) index. FirstTrueElement is -2 when
278 // undefined, otherwise set to the first true element. SecondTrueElement is
279 // -2 when undefined, -3 when overdefined and >= 0 when that index is true.
280 int FirstTrueElement = Undefined, SecondTrueElement = Undefined;
281
282 // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the
283 // form "i != 47 & i != 87". Same state transitions as for true elements.
284 int FirstFalseElement = Undefined, SecondFalseElement = Undefined;
Jim Grosbach129c52a2011-09-30 18:09:53 +0000285
Chris Lattner2188e402010-01-04 07:37:31 +0000286 /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these
287 /// define a state machine that triggers for ranges of values that the index
288 /// is true or false for. This triggers on things like "abbbbc"[i] == 'b'.
289 /// This is -2 when undefined, -3 when overdefined, and otherwise the last
290 /// index in the range (inclusive). We use -2 for undefined here because we
291 /// use relative comparisons and don't want 0-1 to match -1.
292 int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined;
Jim Grosbach129c52a2011-09-30 18:09:53 +0000293
Chris Lattner2188e402010-01-04 07:37:31 +0000294 // MagicBitvector - This is a magic bitvector where we set a bit if the
295 // comparison is true for element 'i'. If there are 64 elements or less in
296 // the array, this will fully represent all the comparison results.
297 uint64_t MagicBitvector = 0;
Jim Grosbach129c52a2011-09-30 18:09:53 +0000298
Chris Lattner2188e402010-01-04 07:37:31 +0000299 // Scan the array and see if one of our patterns matches.
300 Constant *CompareRHS = cast<Constant>(ICI.getOperand(1));
Chris Lattnerfe741762012-01-31 02:55:06 +0000301 for (unsigned i = 0, e = ArrayElementCount; i != e; ++i) {
302 Constant *Elt = Init->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +0000303 if (!Elt) return nullptr;
Jim Grosbach129c52a2011-09-30 18:09:53 +0000304
Chris Lattner2188e402010-01-04 07:37:31 +0000305 // If this is indexing an array of structures, get the structure element.
306 if (!LaterIndices.empty())
Jay Foad57aa6362011-07-13 10:26:04 +0000307 Elt = ConstantExpr::getExtractValue(Elt, LaterIndices);
Jim Grosbach129c52a2011-09-30 18:09:53 +0000308
Chris Lattner2188e402010-01-04 07:37:31 +0000309 // If the element is masked, handle it.
310 if (AndCst) Elt = ConstantExpr::getAnd(Elt, AndCst);
Jim Grosbach129c52a2011-09-30 18:09:53 +0000311
Chris Lattner2188e402010-01-04 07:37:31 +0000312 // Find out if the comparison would be true or false for the i'th element.
313 Constant *C = ConstantFoldCompareInstOperands(ICI.getPredicate(), Elt,
Justin Bogner99798402016-08-05 01:06:44 +0000314 CompareRHS, DL, &TLI);
Chris Lattner2188e402010-01-04 07:37:31 +0000315 // If the result is undef for this element, ignore it.
316 if (isa<UndefValue>(C)) {
317 // Extend range state machines to cover this element in case there is an
318 // undef in the middle of the range.
319 if (TrueRangeEnd == (int)i-1)
320 TrueRangeEnd = i;
321 if (FalseRangeEnd == (int)i-1)
322 FalseRangeEnd = i;
323 continue;
324 }
Jim Grosbach129c52a2011-09-30 18:09:53 +0000325
Chris Lattner2188e402010-01-04 07:37:31 +0000326 // If we can't compute the result for any of the elements, we have to give
327 // up evaluating the entire conditional.
Craig Topperf40110f2014-04-25 05:29:35 +0000328 if (!isa<ConstantInt>(C)) return nullptr;
Jim Grosbach129c52a2011-09-30 18:09:53 +0000329
Chris Lattner2188e402010-01-04 07:37:31 +0000330 // Otherwise, we know if the comparison is true or false for this element,
331 // update our state machines.
332 bool IsTrueForElt = !cast<ConstantInt>(C)->isZero();
Jim Grosbach129c52a2011-09-30 18:09:53 +0000333
Chris Lattner2188e402010-01-04 07:37:31 +0000334 // State machine for single/double/range index comparison.
335 if (IsTrueForElt) {
336 // Update the TrueElement state machine.
337 if (FirstTrueElement == Undefined)
338 FirstTrueElement = TrueRangeEnd = i; // First true element.
339 else {
340 // Update double-compare state machine.
341 if (SecondTrueElement == Undefined)
342 SecondTrueElement = i;
343 else
344 SecondTrueElement = Overdefined;
Jim Grosbach129c52a2011-09-30 18:09:53 +0000345
Chris Lattner2188e402010-01-04 07:37:31 +0000346 // Update range state machine.
347 if (TrueRangeEnd == (int)i-1)
348 TrueRangeEnd = i;
349 else
350 TrueRangeEnd = Overdefined;
351 }
352 } else {
353 // Update the FalseElement state machine.
354 if (FirstFalseElement == Undefined)
355 FirstFalseElement = FalseRangeEnd = i; // First false element.
356 else {
357 // Update double-compare state machine.
358 if (SecondFalseElement == Undefined)
359 SecondFalseElement = i;
360 else
361 SecondFalseElement = Overdefined;
Jim Grosbach129c52a2011-09-30 18:09:53 +0000362
Chris Lattner2188e402010-01-04 07:37:31 +0000363 // Update range state machine.
364 if (FalseRangeEnd == (int)i-1)
365 FalseRangeEnd = i;
366 else
367 FalseRangeEnd = Overdefined;
368 }
369 }
Jim Grosbach129c52a2011-09-30 18:09:53 +0000370
Chris Lattner2188e402010-01-04 07:37:31 +0000371 // If this element is in range, update our magic bitvector.
372 if (i < 64 && IsTrueForElt)
373 MagicBitvector |= 1ULL << i;
Jim Grosbach129c52a2011-09-30 18:09:53 +0000374
Chris Lattner2188e402010-01-04 07:37:31 +0000375 // If all of our states become overdefined, bail out early. Since the
376 // predicate is expensive, only check it every 8 elements. This is only
377 // really useful for really huge arrays.
378 if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined &&
379 SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined &&
380 FalseRangeEnd == Overdefined)
Craig Topperf40110f2014-04-25 05:29:35 +0000381 return nullptr;
Chris Lattner2188e402010-01-04 07:37:31 +0000382 }
383
384 // Now that we've scanned the entire array, emit our new comparison(s). We
385 // order the state machines in complexity of the generated code.
386 Value *Idx = GEP->getOperand(2);
387
Matt Arsenault5aeae182013-08-19 21:40:31 +0000388 // If the index is larger than the pointer size of the target, truncate the
389 // index down like the GEP would do implicitly. We don't have to do this for
390 // an inbounds GEP because the index can't be out of range.
Matt Arsenault84680622013-09-30 21:11:01 +0000391 if (!GEP->isInBounds()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000392 Type *IntPtrTy = DL.getIntPtrType(GEP->getType());
Matt Arsenault84680622013-09-30 21:11:01 +0000393 unsigned PtrSize = IntPtrTy->getIntegerBitWidth();
394 if (Idx->getType()->getPrimitiveSizeInBits() > PtrSize)
395 Idx = Builder->CreateTrunc(Idx, IntPtrTy);
396 }
Matt Arsenault5aeae182013-08-19 21:40:31 +0000397
Chris Lattner2188e402010-01-04 07:37:31 +0000398 // If the comparison is only true for one or two elements, emit direct
399 // comparisons.
400 if (SecondTrueElement != Overdefined) {
401 // None true -> false.
402 if (FirstTrueElement == Undefined)
Sanjay Patel4b198802016-02-01 22:23:39 +0000403 return replaceInstUsesWith(ICI, Builder->getFalse());
Jim Grosbach129c52a2011-09-30 18:09:53 +0000404
Chris Lattner2188e402010-01-04 07:37:31 +0000405 Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement);
Jim Grosbach129c52a2011-09-30 18:09:53 +0000406
Chris Lattner2188e402010-01-04 07:37:31 +0000407 // True for one element -> 'i == 47'.
408 if (SecondTrueElement == Undefined)
409 return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx);
Jim Grosbach129c52a2011-09-30 18:09:53 +0000410
Chris Lattner2188e402010-01-04 07:37:31 +0000411 // True for two elements -> 'i == 47 | i == 72'.
412 Value *C1 = Builder->CreateICmpEQ(Idx, FirstTrueIdx);
413 Value *SecondTrueIdx = ConstantInt::get(Idx->getType(), SecondTrueElement);
414 Value *C2 = Builder->CreateICmpEQ(Idx, SecondTrueIdx);
415 return BinaryOperator::CreateOr(C1, C2);
416 }
417
418 // If the comparison is only false for one or two elements, emit direct
419 // comparisons.
420 if (SecondFalseElement != Overdefined) {
421 // None false -> true.
422 if (FirstFalseElement == Undefined)
Sanjay Patel4b198802016-02-01 22:23:39 +0000423 return replaceInstUsesWith(ICI, Builder->getTrue());
Jim Grosbach129c52a2011-09-30 18:09:53 +0000424
Chris Lattner2188e402010-01-04 07:37:31 +0000425 Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement);
426
427 // False for one element -> 'i != 47'.
428 if (SecondFalseElement == Undefined)
429 return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx);
Jim Grosbach129c52a2011-09-30 18:09:53 +0000430
Chris Lattner2188e402010-01-04 07:37:31 +0000431 // False for two elements -> 'i != 47 & i != 72'.
432 Value *C1 = Builder->CreateICmpNE(Idx, FirstFalseIdx);
433 Value *SecondFalseIdx = ConstantInt::get(Idx->getType(),SecondFalseElement);
434 Value *C2 = Builder->CreateICmpNE(Idx, SecondFalseIdx);
435 return BinaryOperator::CreateAnd(C1, C2);
436 }
Jim Grosbach129c52a2011-09-30 18:09:53 +0000437
Chris Lattner2188e402010-01-04 07:37:31 +0000438 // If the comparison can be replaced with a range comparison for the elements
439 // where it is true, emit the range check.
440 if (TrueRangeEnd != Overdefined) {
441 assert(TrueRangeEnd != FirstTrueElement && "Should emit single compare");
Jim Grosbach129c52a2011-09-30 18:09:53 +0000442
Chris Lattner2188e402010-01-04 07:37:31 +0000443 // Generate (i-FirstTrue) <u (TrueRangeEnd-FirstTrue+1).
444 if (FirstTrueElement) {
445 Value *Offs = ConstantInt::get(Idx->getType(), -FirstTrueElement);
446 Idx = Builder->CreateAdd(Idx, Offs);
447 }
Jim Grosbach129c52a2011-09-30 18:09:53 +0000448
Chris Lattner2188e402010-01-04 07:37:31 +0000449 Value *End = ConstantInt::get(Idx->getType(),
450 TrueRangeEnd-FirstTrueElement+1);
451 return new ICmpInst(ICmpInst::ICMP_ULT, Idx, End);
452 }
Jim Grosbach129c52a2011-09-30 18:09:53 +0000453
Chris Lattner2188e402010-01-04 07:37:31 +0000454 // False range check.
455 if (FalseRangeEnd != Overdefined) {
456 assert(FalseRangeEnd != FirstFalseElement && "Should emit single compare");
457 // Generate (i-FirstFalse) >u (FalseRangeEnd-FirstFalse).
458 if (FirstFalseElement) {
459 Value *Offs = ConstantInt::get(Idx->getType(), -FirstFalseElement);
460 Idx = Builder->CreateAdd(Idx, Offs);
461 }
Jim Grosbach129c52a2011-09-30 18:09:53 +0000462
Chris Lattner2188e402010-01-04 07:37:31 +0000463 Value *End = ConstantInt::get(Idx->getType(),
464 FalseRangeEnd-FirstFalseElement);
465 return new ICmpInst(ICmpInst::ICMP_UGT, Idx, End);
466 }
Jim Grosbach129c52a2011-09-30 18:09:53 +0000467
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000468 // If a magic bitvector captures the entire comparison state
Chris Lattner2188e402010-01-04 07:37:31 +0000469 // of this load, replace it with computation that does:
470 // ((magic_cst >> i) & 1) != 0
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000471 {
Craig Topperf40110f2014-04-25 05:29:35 +0000472 Type *Ty = nullptr;
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000473
474 // Look for an appropriate type:
475 // - The type of Idx if the magic fits
476 // - The smallest fitting legal type if we have a DataLayout
477 // - Default to i32
478 if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth())
479 Ty = Idx->getType();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000480 else
481 Ty = DL.getSmallestLegalIntType(Init->getContext(), ArrayElementCount);
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000482
Craig Topperf40110f2014-04-25 05:29:35 +0000483 if (Ty) {
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000484 Value *V = Builder->CreateIntCast(Idx, Ty, false);
485 V = Builder->CreateLShr(ConstantInt::get(Ty, MagicBitvector), V);
486 V = Builder->CreateAnd(ConstantInt::get(Ty, 1), V);
487 return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));
488 }
Chris Lattner2188e402010-01-04 07:37:31 +0000489 }
Jim Grosbach129c52a2011-09-30 18:09:53 +0000490
Craig Topperf40110f2014-04-25 05:29:35 +0000491 return nullptr;
Chris Lattner2188e402010-01-04 07:37:31 +0000492}
493
Sanjay Patel5f0217f2016-06-05 16:46:18 +0000494/// Return a value that can be used to compare the *offset* implied by a GEP to
495/// zero. For example, if we have &A[i], we want to return 'i' for
496/// "icmp ne i, 0". Note that, in general, indices can be complex, and scales
497/// are involved. The above expression would also be legal to codegen as
498/// "icmp ne (i*4), 0" (assuming A is a pointer to i32).
499/// This latter form is less amenable to optimization though, and we are allowed
Chris Lattner2188e402010-01-04 07:37:31 +0000500/// to generate the first by knowing that pointer arithmetic doesn't overflow.
501///
502/// If we can't emit an optimized form for this expression, this returns null.
Jim Grosbach129c52a2011-09-30 18:09:53 +0000503///
Sanjay Pateld93c4c02016-09-15 18:22:25 +0000504static Value *evaluateGEPOffsetExpression(User *GEP, InstCombiner &IC,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000505 const DataLayout &DL) {
Chris Lattner2188e402010-01-04 07:37:31 +0000506 gep_type_iterator GTI = gep_type_begin(GEP);
Jim Grosbach129c52a2011-09-30 18:09:53 +0000507
Chris Lattner2188e402010-01-04 07:37:31 +0000508 // Check to see if this gep only has a single variable index. If so, and if
509 // any constant indices are a multiple of its scale, then we can compute this
510 // in terms of the scale of the variable index. For example, if the GEP
511 // implies an offset of "12 + i*4", then we can codegen this as "3 + i",
512 // because the expression will cross zero at the same point.
513 unsigned i, e = GEP->getNumOperands();
514 int64_t Offset = 0;
515 for (i = 1; i != e; ++i, ++GTI) {
516 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
517 // Compute the aggregate offset of constant indices.
518 if (CI->isZero()) continue;
Jim Grosbach129c52a2011-09-30 18:09:53 +0000519
Chris Lattner2188e402010-01-04 07:37:31 +0000520 // Handle a struct index, which adds its field offset to the pointer.
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000521 if (StructType *STy = GTI.getStructTypeOrNull()) {
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000522 Offset += DL.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
Chris Lattner2188e402010-01-04 07:37:31 +0000523 } else {
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000524 uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType());
Chris Lattner2188e402010-01-04 07:37:31 +0000525 Offset += Size*CI->getSExtValue();
526 }
527 } else {
528 // Found our variable index.
529 break;
530 }
531 }
Jim Grosbach129c52a2011-09-30 18:09:53 +0000532
Chris Lattner2188e402010-01-04 07:37:31 +0000533 // If there are no variable indices, we must have a constant offset, just
534 // evaluate it the general way.
Craig Topperf40110f2014-04-25 05:29:35 +0000535 if (i == e) return nullptr;
Jim Grosbach129c52a2011-09-30 18:09:53 +0000536
Chris Lattner2188e402010-01-04 07:37:31 +0000537 Value *VariableIdx = GEP->getOperand(i);
538 // Determine the scale factor of the variable element. For example, this is
539 // 4 if the variable index is into an array of i32.
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000540 uint64_t VariableScale = DL.getTypeAllocSize(GTI.getIndexedType());
Jim Grosbach129c52a2011-09-30 18:09:53 +0000541
Chris Lattner2188e402010-01-04 07:37:31 +0000542 // Verify that there are no other variable indices. If so, emit the hard way.
543 for (++i, ++GTI; i != e; ++i, ++GTI) {
544 ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i));
Craig Topperf40110f2014-04-25 05:29:35 +0000545 if (!CI) return nullptr;
Jim Grosbach129c52a2011-09-30 18:09:53 +0000546
Chris Lattner2188e402010-01-04 07:37:31 +0000547 // Compute the aggregate offset of constant indices.
548 if (CI->isZero()) continue;
Jim Grosbach129c52a2011-09-30 18:09:53 +0000549
Chris Lattner2188e402010-01-04 07:37:31 +0000550 // Handle a struct index, which adds its field offset to the pointer.
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000551 if (StructType *STy = GTI.getStructTypeOrNull()) {
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000552 Offset += DL.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
Chris Lattner2188e402010-01-04 07:37:31 +0000553 } else {
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000554 uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType());
Chris Lattner2188e402010-01-04 07:37:31 +0000555 Offset += Size*CI->getSExtValue();
556 }
557 }
Jim Grosbach129c52a2011-09-30 18:09:53 +0000558
Chris Lattner2188e402010-01-04 07:37:31 +0000559 // Okay, we know we have a single variable index, which must be a
560 // pointer/array/vector index. If there is no offset, life is simple, return
561 // the index.
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000562 Type *IntPtrTy = DL.getIntPtrType(GEP->getOperand(0)->getType());
Matt Arsenault745101d2013-08-21 19:53:10 +0000563 unsigned IntPtrWidth = IntPtrTy->getIntegerBitWidth();
Chris Lattner2188e402010-01-04 07:37:31 +0000564 if (Offset == 0) {
565 // Cast to intptrty in case a truncation occurs. If an extension is needed,
566 // we don't need to bother extending: the extension won't affect where the
567 // computation crosses zero.
Eli Friedman1754a252011-05-18 23:11:30 +0000568 if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth) {
Eli Friedman1754a252011-05-18 23:11:30 +0000569 VariableIdx = IC.Builder->CreateTrunc(VariableIdx, IntPtrTy);
570 }
Chris Lattner2188e402010-01-04 07:37:31 +0000571 return VariableIdx;
572 }
Jim Grosbach129c52a2011-09-30 18:09:53 +0000573
Chris Lattner2188e402010-01-04 07:37:31 +0000574 // Otherwise, there is an index. The computation we will do will be modulo
575 // the pointer size, so get it.
576 uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth);
Jim Grosbach129c52a2011-09-30 18:09:53 +0000577
Chris Lattner2188e402010-01-04 07:37:31 +0000578 Offset &= PtrSizeMask;
579 VariableScale &= PtrSizeMask;
Jim Grosbach129c52a2011-09-30 18:09:53 +0000580
Chris Lattner2188e402010-01-04 07:37:31 +0000581 // To do this transformation, any constant index must be a multiple of the
582 // variable scale factor. For example, we can evaluate "12 + 4*i" as "3 + i",
583 // but we can't evaluate "10 + 3*i" in terms of i. Check that the offset is a
584 // multiple of the variable scale.
585 int64_t NewOffs = Offset / (int64_t)VariableScale;
586 if (Offset != NewOffs*(int64_t)VariableScale)
Craig Topperf40110f2014-04-25 05:29:35 +0000587 return nullptr;
Jim Grosbach129c52a2011-09-30 18:09:53 +0000588
Chris Lattner2188e402010-01-04 07:37:31 +0000589 // Okay, we can do this evaluation. Start by converting the index to intptr.
Chris Lattner2188e402010-01-04 07:37:31 +0000590 if (VariableIdx->getType() != IntPtrTy)
Eli Friedman1754a252011-05-18 23:11:30 +0000591 VariableIdx = IC.Builder->CreateIntCast(VariableIdx, IntPtrTy,
592 true /*Signed*/);
Chris Lattner2188e402010-01-04 07:37:31 +0000593 Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs);
Eli Friedman1754a252011-05-18 23:11:30 +0000594 return IC.Builder->CreateAdd(VariableIdx, OffsetVal, "offset");
Chris Lattner2188e402010-01-04 07:37:31 +0000595}
596
Silviu Barangaf29dfd32016-01-15 15:52:05 +0000597/// Returns true if we can rewrite Start as a GEP with pointer Base
598/// and some integer offset. The nodes that need to be re-written
599/// for this transformation will be added to Explored.
600static bool canRewriteGEPAsOffset(Value *Start, Value *Base,
601 const DataLayout &DL,
602 SetVector<Value *> &Explored) {
603 SmallVector<Value *, 16> WorkList(1, Start);
604 Explored.insert(Base);
605
606 // The following traversal gives us an order which can be used
607 // when doing the final transformation. Since in the final
608 // transformation we create the PHI replacement instructions first,
609 // we don't have to get them in any particular order.
610 //
611 // However, for other instructions we will have to traverse the
612 // operands of an instruction first, which means that we have to
613 // do a post-order traversal.
614 while (!WorkList.empty()) {
615 SetVector<PHINode *> PHIs;
616
617 while (!WorkList.empty()) {
618 if (Explored.size() >= 100)
619 return false;
620
621 Value *V = WorkList.back();
622
623 if (Explored.count(V) != 0) {
624 WorkList.pop_back();
625 continue;
626 }
627
628 if (!isa<IntToPtrInst>(V) && !isa<PtrToIntInst>(V) &&
David Majnemer8b16da82016-09-15 20:10:09 +0000629 !isa<GetElementPtrInst>(V) && !isa<PHINode>(V))
Silviu Barangaf29dfd32016-01-15 15:52:05 +0000630 // We've found some value that we can't explore which is different from
631 // the base. Therefore we can't do this transformation.
632 return false;
633
634 if (isa<IntToPtrInst>(V) || isa<PtrToIntInst>(V)) {
635 auto *CI = dyn_cast<CastInst>(V);
636 if (!CI->isNoopCast(DL))
637 return false;
638
639 if (Explored.count(CI->getOperand(0)) == 0)
640 WorkList.push_back(CI->getOperand(0));
641 }
642
643 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
644 // We're limiting the GEP to having one index. This will preserve
645 // the original pointer type. We could handle more cases in the
646 // future.
647 if (GEP->getNumIndices() != 1 || !GEP->isInBounds() ||
648 GEP->getType() != Start->getType())
649 return false;
650
651 if (Explored.count(GEP->getOperand(0)) == 0)
652 WorkList.push_back(GEP->getOperand(0));
653 }
654
655 if (WorkList.back() == V) {
656 WorkList.pop_back();
657 // We've finished visiting this node, mark it as such.
658 Explored.insert(V);
659 }
660
661 if (auto *PN = dyn_cast<PHINode>(V)) {
David Majnemercdf28732016-03-19 04:39:52 +0000662 // We cannot transform PHIs on unsplittable basic blocks.
663 if (isa<CatchSwitchInst>(PN->getParent()->getTerminator()))
664 return false;
Silviu Barangaf29dfd32016-01-15 15:52:05 +0000665 Explored.insert(PN);
666 PHIs.insert(PN);
667 }
668 }
669
670 // Explore the PHI nodes further.
671 for (auto *PN : PHIs)
672 for (Value *Op : PN->incoming_values())
673 if (Explored.count(Op) == 0)
674 WorkList.push_back(Op);
675 }
676
677 // Make sure that we can do this. Since we can't insert GEPs in a basic
678 // block before a PHI node, we can't easily do this transformation if
679 // we have PHI node users of transformed instructions.
680 for (Value *Val : Explored) {
681 for (Value *Use : Val->uses()) {
682
683 auto *PHI = dyn_cast<PHINode>(Use);
684 auto *Inst = dyn_cast<Instruction>(Val);
685
686 if (Inst == Base || Inst == PHI || !Inst || !PHI ||
687 Explored.count(PHI) == 0)
688 continue;
689
690 if (PHI->getParent() == Inst->getParent())
691 return false;
692 }
693 }
694 return true;
695}
696
697// Sets the appropriate insert point on Builder where we can add
698// a replacement Instruction for V (if that is possible).
699static void setInsertionPoint(IRBuilder<> &Builder, Value *V,
700 bool Before = true) {
701 if (auto *PHI = dyn_cast<PHINode>(V)) {
702 Builder.SetInsertPoint(&*PHI->getParent()->getFirstInsertionPt());
703 return;
704 }
705 if (auto *I = dyn_cast<Instruction>(V)) {
706 if (!Before)
707 I = &*std::next(I->getIterator());
708 Builder.SetInsertPoint(I);
709 return;
710 }
711 if (auto *A = dyn_cast<Argument>(V)) {
712 // Set the insertion point in the entry block.
713 BasicBlock &Entry = A->getParent()->getEntryBlock();
714 Builder.SetInsertPoint(&*Entry.getFirstInsertionPt());
715 return;
716 }
717 // Otherwise, this is a constant and we don't need to set a new
718 // insertion point.
719 assert(isa<Constant>(V) && "Setting insertion point for unknown value!");
720}
721
722/// Returns a re-written value of Start as an indexed GEP using Base as a
723/// pointer.
724static Value *rewriteGEPAsOffset(Value *Start, Value *Base,
725 const DataLayout &DL,
726 SetVector<Value *> &Explored) {
727 // Perform all the substitutions. This is a bit tricky because we can
728 // have cycles in our use-def chains.
729 // 1. Create the PHI nodes without any incoming values.
730 // 2. Create all the other values.
731 // 3. Add the edges for the PHI nodes.
732 // 4. Emit GEPs to get the original pointers.
733 // 5. Remove the original instructions.
734 Type *IndexType = IntegerType::get(
735 Base->getContext(), DL.getPointerTypeSizeInBits(Start->getType()));
736
737 DenseMap<Value *, Value *> NewInsts;
738 NewInsts[Base] = ConstantInt::getNullValue(IndexType);
739
740 // Create the new PHI nodes, without adding any incoming values.
741 for (Value *Val : Explored) {
742 if (Val == Base)
743 continue;
744 // Create empty phi nodes. This avoids cyclic dependencies when creating
745 // the remaining instructions.
746 if (auto *PHI = dyn_cast<PHINode>(Val))
747 NewInsts[PHI] = PHINode::Create(IndexType, PHI->getNumIncomingValues(),
748 PHI->getName() + ".idx", PHI);
749 }
750 IRBuilder<> Builder(Base->getContext());
751
752 // Create all the other instructions.
753 for (Value *Val : Explored) {
754
755 if (NewInsts.find(Val) != NewInsts.end())
756 continue;
757
758 if (auto *CI = dyn_cast<CastInst>(Val)) {
759 NewInsts[CI] = NewInsts[CI->getOperand(0)];
760 continue;
761 }
762 if (auto *GEP = dyn_cast<GEPOperator>(Val)) {
763 Value *Index = NewInsts[GEP->getOperand(1)] ? NewInsts[GEP->getOperand(1)]
764 : GEP->getOperand(1);
765 setInsertionPoint(Builder, GEP);
766 // Indices might need to be sign extended. GEPs will magically do
767 // this, but we need to do it ourselves here.
768 if (Index->getType()->getScalarSizeInBits() !=
769 NewInsts[GEP->getOperand(0)]->getType()->getScalarSizeInBits()) {
770 Index = Builder.CreateSExtOrTrunc(
771 Index, NewInsts[GEP->getOperand(0)]->getType(),
772 GEP->getOperand(0)->getName() + ".sext");
773 }
774
775 auto *Op = NewInsts[GEP->getOperand(0)];
776 if (isa<ConstantInt>(Op) && dyn_cast<ConstantInt>(Op)->isZero())
777 NewInsts[GEP] = Index;
778 else
779 NewInsts[GEP] = Builder.CreateNSWAdd(
780 Op, Index, GEP->getOperand(0)->getName() + ".add");
781 continue;
782 }
783 if (isa<PHINode>(Val))
784 continue;
785
786 llvm_unreachable("Unexpected instruction type");
787 }
788
789 // Add the incoming values to the PHI nodes.
790 for (Value *Val : Explored) {
791 if (Val == Base)
792 continue;
793 // All the instructions have been created, we can now add edges to the
794 // phi nodes.
795 if (auto *PHI = dyn_cast<PHINode>(Val)) {
796 PHINode *NewPhi = static_cast<PHINode *>(NewInsts[PHI]);
797 for (unsigned I = 0, E = PHI->getNumIncomingValues(); I < E; ++I) {
798 Value *NewIncoming = PHI->getIncomingValue(I);
799
800 if (NewInsts.find(NewIncoming) != NewInsts.end())
801 NewIncoming = NewInsts[NewIncoming];
802
803 NewPhi->addIncoming(NewIncoming, PHI->getIncomingBlock(I));
804 }
805 }
806 }
807
808 for (Value *Val : Explored) {
809 if (Val == Base)
810 continue;
811
812 // Depending on the type, for external users we have to emit
813 // a GEP or a GEP + ptrtoint.
814 setInsertionPoint(Builder, Val, false);
815
816 // If required, create an inttoptr instruction for Base.
817 Value *NewBase = Base;
818 if (!Base->getType()->isPointerTy())
819 NewBase = Builder.CreateBitOrPointerCast(Base, Start->getType(),
820 Start->getName() + "to.ptr");
821
822 Value *GEP = Builder.CreateInBoundsGEP(
823 Start->getType()->getPointerElementType(), NewBase,
824 makeArrayRef(NewInsts[Val]), Val->getName() + ".ptr");
825
826 if (!Val->getType()->isPointerTy()) {
827 Value *Cast = Builder.CreatePointerCast(GEP, Val->getType(),
828 Val->getName() + ".conv");
829 GEP = Cast;
830 }
831 Val->replaceAllUsesWith(GEP);
832 }
833
834 return NewInsts[Start];
835}
836
837/// Looks through GEPs, IntToPtrInsts and PtrToIntInsts in order to express
838/// the input Value as a constant indexed GEP. Returns a pair containing
839/// the GEPs Pointer and Index.
840static std::pair<Value *, Value *>
841getAsConstantIndexedAddress(Value *V, const DataLayout &DL) {
842 Type *IndexType = IntegerType::get(V->getContext(),
843 DL.getPointerTypeSizeInBits(V->getType()));
844
845 Constant *Index = ConstantInt::getNullValue(IndexType);
846 while (true) {
847 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
848 // We accept only inbouds GEPs here to exclude the possibility of
849 // overflow.
850 if (!GEP->isInBounds())
851 break;
852 if (GEP->hasAllConstantIndices() && GEP->getNumIndices() == 1 &&
853 GEP->getType() == V->getType()) {
854 V = GEP->getOperand(0);
855 Constant *GEPIndex = static_cast<Constant *>(GEP->getOperand(1));
856 Index = ConstantExpr::getAdd(
857 Index, ConstantExpr::getSExtOrBitCast(GEPIndex, IndexType));
858 continue;
859 }
860 break;
861 }
862 if (auto *CI = dyn_cast<IntToPtrInst>(V)) {
863 if (!CI->isNoopCast(DL))
864 break;
865 V = CI->getOperand(0);
866 continue;
867 }
868 if (auto *CI = dyn_cast<PtrToIntInst>(V)) {
869 if (!CI->isNoopCast(DL))
870 break;
871 V = CI->getOperand(0);
872 continue;
873 }
874 break;
875 }
876 return {V, Index};
877}
878
Sanjay Patel5f0217f2016-06-05 16:46:18 +0000879/// Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.
880/// We can look through PHIs, GEPs and casts in order to determine a common base
881/// between GEPLHS and RHS.
Silviu Barangaf29dfd32016-01-15 15:52:05 +0000882static Instruction *transformToIndexedCompare(GEPOperator *GEPLHS, Value *RHS,
883 ICmpInst::Predicate Cond,
884 const DataLayout &DL) {
885 if (!GEPLHS->hasAllConstantIndices())
886 return nullptr;
887
Silviu Barangac6d21eb2017-01-31 14:04:15 +0000888 // Make sure the pointers have the same type.
889 if (GEPLHS->getType() != RHS->getType())
890 return nullptr;
891
Silviu Barangaf29dfd32016-01-15 15:52:05 +0000892 Value *PtrBase, *Index;
893 std::tie(PtrBase, Index) = getAsConstantIndexedAddress(GEPLHS, DL);
894
895 // The set of nodes that will take part in this transformation.
896 SetVector<Value *> Nodes;
897
898 if (!canRewriteGEPAsOffset(RHS, PtrBase, DL, Nodes))
899 return nullptr;
900
901 // We know we can re-write this as
902 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2)
903 // Since we've only looked through inbouds GEPs we know that we
904 // can't have overflow on either side. We can therefore re-write
905 // this as:
906 // OFFSET1 cmp OFFSET2
907 Value *NewRHS = rewriteGEPAsOffset(RHS, PtrBase, DL, Nodes);
908
909 // RewriteGEPAsOffset has replaced RHS and all of its uses with a re-written
910 // GEP having PtrBase as the pointer base, and has returned in NewRHS the
911 // offset. Since Index is the offset of LHS to the base pointer, we will now
912 // compare the offsets instead of comparing the pointers.
913 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Index, NewRHS);
914}
915
Sanjay Patel5f0217f2016-06-05 16:46:18 +0000916/// Fold comparisons between a GEP instruction and something else. At this point
917/// we know that the GEP is on the LHS of the comparison.
Sanjay Patel43395062016-07-21 18:07:40 +0000918Instruction *InstCombiner::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
Chris Lattner2188e402010-01-04 07:37:31 +0000919 ICmpInst::Predicate Cond,
920 Instruction &I) {
Benjamin Kramer6ee86902012-02-21 13:31:09 +0000921 // Don't transform signed compares of GEPs into index compares. Even if the
922 // GEP is inbounds, the final add of the base pointer can have signed overflow
923 // and would change the result of the icmp.
924 // e.g. "&foo[0] <s &foo[1]" can't be folded to "true" because "foo" could be
Benjamin Kramerc7a22fe2012-02-21 13:40:06 +0000925 // the maximum signed value for the pointer type.
Benjamin Kramer6ee86902012-02-21 13:31:09 +0000926 if (ICmpInst::isSigned(Cond))
Craig Topperf40110f2014-04-25 05:29:35 +0000927 return nullptr;
Benjamin Kramer6ee86902012-02-21 13:31:09 +0000928
Matt Arsenault44f60d02014-06-09 19:20:29 +0000929 // Look through bitcasts and addrspacecasts. We do not however want to remove
930 // 0 GEPs.
931 if (!isa<GetElementPtrInst>(RHS))
932 RHS = RHS->stripPointerCasts();
Chris Lattner2188e402010-01-04 07:37:31 +0000933
934 Value *PtrBase = GEPLHS->getOperand(0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000935 if (PtrBase == RHS && GEPLHS->isInBounds()) {
Chris Lattner2188e402010-01-04 07:37:31 +0000936 // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
937 // This transformation (ignoring the base and scales) is valid because we
938 // know pointers can't overflow since the gep is inbounds. See if we can
939 // output an optimized form.
Sanjay Pateld93c4c02016-09-15 18:22:25 +0000940 Value *Offset = evaluateGEPOffsetExpression(GEPLHS, *this, DL);
Jim Grosbach129c52a2011-09-30 18:09:53 +0000941
Chris Lattner2188e402010-01-04 07:37:31 +0000942 // If not, synthesize the offset the hard way.
Craig Topperf40110f2014-04-25 05:29:35 +0000943 if (!Offset)
Chris Lattner2188e402010-01-04 07:37:31 +0000944 Offset = EmitGEPOffset(GEPLHS);
945 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset,
946 Constant::getNullValue(Offset->getType()));
947 } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {
948 // If the base pointers are different, but the indices are the same, just
949 // compare the base pointer.
950 if (PtrBase != GEPRHS->getOperand(0)) {
951 bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands();
952 IndicesTheSame &= GEPLHS->getOperand(0)->getType() ==
953 GEPRHS->getOperand(0)->getType();
954 if (IndicesTheSame)
955 for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
956 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
957 IndicesTheSame = false;
958 break;
959 }
960
961 // If all indices are the same, just compare the base pointers.
962 if (IndicesTheSame)
David Majnemer5953d372013-06-29 10:28:04 +0000963 return new ICmpInst(Cond, GEPLHS->getOperand(0), GEPRHS->getOperand(0));
Chris Lattner2188e402010-01-04 07:37:31 +0000964
Benjamin Kramer7adb1892012-02-20 15:07:47 +0000965 // If we're comparing GEPs with two base pointers that only differ in type
966 // and both GEPs have only constant indices or just one use, then fold
967 // the compare with the adjusted indices.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000968 if (GEPLHS->isInBounds() && GEPRHS->isInBounds() &&
Benjamin Kramer7adb1892012-02-20 15:07:47 +0000969 (GEPLHS->hasAllConstantIndices() || GEPLHS->hasOneUse()) &&
970 (GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) &&
971 PtrBase->stripPointerCasts() ==
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000972 GEPRHS->getOperand(0)->stripPointerCasts()) {
Matt Arsenault44f60d02014-06-09 19:20:29 +0000973 Value *LOffset = EmitGEPOffset(GEPLHS);
974 Value *ROffset = EmitGEPOffset(GEPRHS);
975
976 // If we looked through an addrspacecast between different sized address
977 // spaces, the LHS and RHS pointers are different sized
978 // integers. Truncate to the smaller one.
979 Type *LHSIndexTy = LOffset->getType();
980 Type *RHSIndexTy = ROffset->getType();
981 if (LHSIndexTy != RHSIndexTy) {
982 if (LHSIndexTy->getPrimitiveSizeInBits() <
983 RHSIndexTy->getPrimitiveSizeInBits()) {
984 ROffset = Builder->CreateTrunc(ROffset, LHSIndexTy);
985 } else
986 LOffset = Builder->CreateTrunc(LOffset, RHSIndexTy);
987 }
988
Benjamin Kramer7adb1892012-02-20 15:07:47 +0000989 Value *Cmp = Builder->CreateICmp(ICmpInst::getSignedPredicate(Cond),
Matt Arsenault44f60d02014-06-09 19:20:29 +0000990 LOffset, ROffset);
Sanjay Patel4b198802016-02-01 22:23:39 +0000991 return replaceInstUsesWith(I, Cmp);
Benjamin Kramer7adb1892012-02-20 15:07:47 +0000992 }
993
Chris Lattner2188e402010-01-04 07:37:31 +0000994 // Otherwise, the base pointers are different and the indices are
Silviu Barangaf29dfd32016-01-15 15:52:05 +0000995 // different. Try convert this to an indexed compare by looking through
996 // PHIs/casts.
997 return transformToIndexedCompare(GEPLHS, RHS, Cond, DL);
Chris Lattner2188e402010-01-04 07:37:31 +0000998 }
999
1000 // If one of the GEPs has all zero indices, recurse.
Benjamin Kramerd0993e02014-07-07 11:01:16 +00001001 if (GEPLHS->hasAllZeroIndices())
Sanjay Patel43395062016-07-21 18:07:40 +00001002 return foldGEPICmp(GEPRHS, GEPLHS->getOperand(0),
David Majnemer92a8a7d2013-06-29 09:45:35 +00001003 ICmpInst::getSwappedPredicate(Cond), I);
Chris Lattner2188e402010-01-04 07:37:31 +00001004
1005 // If the other GEP has all zero indices, recurse.
Benjamin Kramerd0993e02014-07-07 11:01:16 +00001006 if (GEPRHS->hasAllZeroIndices())
Sanjay Patel43395062016-07-21 18:07:40 +00001007 return foldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I);
Chris Lattner2188e402010-01-04 07:37:31 +00001008
Stuart Hastings66a82b92011-05-14 05:55:10 +00001009 bool GEPsInBounds = GEPLHS->isInBounds() && GEPRHS->isInBounds();
Chris Lattner2188e402010-01-04 07:37:31 +00001010 if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) {
1011 // If the GEPs only differ by one index, compare it.
1012 unsigned NumDifferences = 0; // Keep track of # differences.
1013 unsigned DiffOperand = 0; // The operand that differs.
1014 for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
1015 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
1016 if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() !=
1017 GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) {
1018 // Irreconcilable differences.
1019 NumDifferences = 2;
1020 break;
1021 } else {
1022 if (NumDifferences++) break;
1023 DiffOperand = i;
1024 }
1025 }
1026
Rafael Espindolaa7bbc0b2013-06-06 17:03:05 +00001027 if (NumDifferences == 0) // SAME GEP?
Sanjay Patel4b198802016-02-01 22:23:39 +00001028 return replaceInstUsesWith(I, // No comparison is needed here.
Jakub Staszakbddea112013-06-06 20:18:46 +00001029 Builder->getInt1(ICmpInst::isTrueWhenEqual(Cond)));
Chris Lattner2188e402010-01-04 07:37:31 +00001030
Stuart Hastings66a82b92011-05-14 05:55:10 +00001031 else if (NumDifferences == 1 && GEPsInBounds) {
Chris Lattner2188e402010-01-04 07:37:31 +00001032 Value *LHSV = GEPLHS->getOperand(DiffOperand);
1033 Value *RHSV = GEPRHS->getOperand(DiffOperand);
1034 // Make sure we do a signed comparison here.
1035 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV);
1036 }
1037 }
1038
1039 // Only lower this if the icmp is the only user of the GEP or if we expect
1040 // the result to fold to a constant!
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001041 if (GEPsInBounds && (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) &&
Chris Lattner2188e402010-01-04 07:37:31 +00001042 (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) {
1043 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
1044 Value *L = EmitGEPOffset(GEPLHS);
1045 Value *R = EmitGEPOffset(GEPRHS);
1046 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R);
1047 }
1048 }
Silviu Barangaf29dfd32016-01-15 15:52:05 +00001049
1050 // Try convert this to an indexed compare by looking through PHIs/casts as a
1051 // last resort.
1052 return transformToIndexedCompare(GEPLHS, RHS, Cond, DL);
Chris Lattner2188e402010-01-04 07:37:31 +00001053}
1054
Pete Cooper980a9352016-08-12 17:13:28 +00001055Instruction *InstCombiner::foldAllocaCmp(ICmpInst &ICI,
1056 const AllocaInst *Alloca,
1057 const Value *Other) {
Hans Wennborgf1f36512015-10-07 00:20:07 +00001058 assert(ICI.isEquality() && "Cannot fold non-equality comparison.");
1059
1060 // It would be tempting to fold away comparisons between allocas and any
1061 // pointer not based on that alloca (e.g. an argument). However, even
1062 // though such pointers cannot alias, they can still compare equal.
1063 //
1064 // But LLVM doesn't specify where allocas get their memory, so if the alloca
1065 // doesn't escape we can argue that it's impossible to guess its value, and we
1066 // can therefore act as if any such guesses are wrong.
1067 //
1068 // The code below checks that the alloca doesn't escape, and that it's only
1069 // used in a comparison once (the current instruction). The
1070 // single-comparison-use condition ensures that we're trivially folding all
1071 // comparisons against the alloca consistently, and avoids the risk of
1072 // erroneously folding a comparison of the pointer with itself.
1073
1074 unsigned MaxIter = 32; // Break cycles and bound to constant-time.
1075
Pete Cooper980a9352016-08-12 17:13:28 +00001076 SmallVector<const Use *, 32> Worklist;
1077 for (const Use &U : Alloca->uses()) {
Hans Wennborgf1f36512015-10-07 00:20:07 +00001078 if (Worklist.size() >= MaxIter)
1079 return nullptr;
1080 Worklist.push_back(&U);
1081 }
1082
1083 unsigned NumCmps = 0;
1084 while (!Worklist.empty()) {
1085 assert(Worklist.size() <= MaxIter);
Pete Cooper980a9352016-08-12 17:13:28 +00001086 const Use *U = Worklist.pop_back_val();
1087 const Value *V = U->getUser();
Hans Wennborgf1f36512015-10-07 00:20:07 +00001088 --MaxIter;
1089
1090 if (isa<BitCastInst>(V) || isa<GetElementPtrInst>(V) || isa<PHINode>(V) ||
1091 isa<SelectInst>(V)) {
1092 // Track the uses.
1093 } else if (isa<LoadInst>(V)) {
1094 // Loading from the pointer doesn't escape it.
1095 continue;
Pete Cooper980a9352016-08-12 17:13:28 +00001096 } else if (const auto *SI = dyn_cast<StoreInst>(V)) {
Hans Wennborgf1f36512015-10-07 00:20:07 +00001097 // Storing *to* the pointer is fine, but storing the pointer escapes it.
1098 if (SI->getValueOperand() == U->get())
1099 return nullptr;
1100 continue;
1101 } else if (isa<ICmpInst>(V)) {
1102 if (NumCmps++)
1103 return nullptr; // Found more than one cmp.
1104 continue;
Pete Cooper980a9352016-08-12 17:13:28 +00001105 } else if (const auto *Intrin = dyn_cast<IntrinsicInst>(V)) {
Hans Wennborgf1f36512015-10-07 00:20:07 +00001106 switch (Intrin->getIntrinsicID()) {
1107 // These intrinsics don't escape or compare the pointer. Memset is safe
1108 // because we don't allow ptrtoint. Memcpy and memmove are safe because
1109 // we don't allow stores, so src cannot point to V.
1110 case Intrinsic::lifetime_start: case Intrinsic::lifetime_end:
1111 case Intrinsic::dbg_declare: case Intrinsic::dbg_value:
1112 case Intrinsic::memcpy: case Intrinsic::memmove: case Intrinsic::memset:
1113 continue;
1114 default:
1115 return nullptr;
1116 }
1117 } else {
1118 return nullptr;
1119 }
Pete Cooper980a9352016-08-12 17:13:28 +00001120 for (const Use &U : V->uses()) {
Hans Wennborgf1f36512015-10-07 00:20:07 +00001121 if (Worklist.size() >= MaxIter)
1122 return nullptr;
1123 Worklist.push_back(&U);
1124 }
1125 }
1126
1127 Type *CmpTy = CmpInst::makeCmpResultType(Other->getType());
Sanjay Patel4b198802016-02-01 22:23:39 +00001128 return replaceInstUsesWith(
Hans Wennborgf1f36512015-10-07 00:20:07 +00001129 ICI,
1130 ConstantInt::get(CmpTy, !CmpInst::isTrueWhenEqual(ICI.getPredicate())));
1131}
1132
Sanjay Patel5f0217f2016-06-05 16:46:18 +00001133/// Fold "icmp pred (X+CI), X".
Sanjay Patel43395062016-07-21 18:07:40 +00001134Instruction *InstCombiner::foldICmpAddOpConst(Instruction &ICI,
1135 Value *X, ConstantInt *CI,
1136 ICmpInst::Predicate Pred) {
Chris Lattner2188e402010-01-04 07:37:31 +00001137 // From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0,
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001138 // so the values can never be equal. Similarly for all other "or equals"
Chris Lattner2188e402010-01-04 07:37:31 +00001139 // operators.
Jim Grosbach129c52a2011-09-30 18:09:53 +00001140
Chris Lattner8c92b572010-01-08 17:48:19 +00001141 // (X+1) <u X --> X >u (MAXUINT-1) --> X == 255
Chris Lattner2188e402010-01-04 07:37:31 +00001142 // (X+2) <u X --> X >u (MAXUINT-2) --> X > 253
1143 // (X+MAXUINT) <u X --> X >u (MAXUINT-MAXUINT) --> X != 0
1144 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
Jim Grosbach129c52a2011-09-30 18:09:53 +00001145 Value *R =
Chris Lattner8c92b572010-01-08 17:48:19 +00001146 ConstantExpr::getSub(ConstantInt::getAllOnesValue(CI->getType()), CI);
Chris Lattner2188e402010-01-04 07:37:31 +00001147 return new ICmpInst(ICmpInst::ICMP_UGT, X, R);
1148 }
Jim Grosbach129c52a2011-09-30 18:09:53 +00001149
Chris Lattner2188e402010-01-04 07:37:31 +00001150 // (X+1) >u X --> X <u (0-1) --> X != 255
1151 // (X+2) >u X --> X <u (0-2) --> X <u 254
1152 // (X+MAXUINT) >u X --> X <u (0-MAXUINT) --> X <u 1 --> X == 0
Duncan Sandse5220012011-02-17 07:46:37 +00001153 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
Chris Lattner2188e402010-01-04 07:37:31 +00001154 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantExpr::getNeg(CI));
Jim Grosbach129c52a2011-09-30 18:09:53 +00001155
Chris Lattner2188e402010-01-04 07:37:31 +00001156 unsigned BitWidth = CI->getType()->getPrimitiveSizeInBits();
1157 ConstantInt *SMax = ConstantInt::get(X->getContext(),
1158 APInt::getSignedMaxValue(BitWidth));
1159
1160 // (X+ 1) <s X --> X >s (MAXSINT-1) --> X == 127
1161 // (X+ 2) <s X --> X >s (MAXSINT-2) --> X >s 125
1162 // (X+MAXSINT) <s X --> X >s (MAXSINT-MAXSINT) --> X >s 0
1163 // (X+MINSINT) <s X --> X >s (MAXSINT-MINSINT) --> X >s -1
1164 // (X+ -2) <s X --> X >s (MAXSINT- -2) --> X >s 126
1165 // (X+ -1) <s X --> X >s (MAXSINT- -1) --> X != 127
Duncan Sandse5220012011-02-17 07:46:37 +00001166 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
Chris Lattner2188e402010-01-04 07:37:31 +00001167 return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantExpr::getSub(SMax, CI));
Jim Grosbach129c52a2011-09-30 18:09:53 +00001168
Chris Lattner2188e402010-01-04 07:37:31 +00001169 // (X+ 1) >s X --> X <s (MAXSINT-(1-1)) --> X != 127
1170 // (X+ 2) >s X --> X <s (MAXSINT-(2-1)) --> X <s 126
1171 // (X+MAXSINT) >s X --> X <s (MAXSINT-(MAXSINT-1)) --> X <s 1
1172 // (X+MINSINT) >s X --> X <s (MAXSINT-(MINSINT-1)) --> X <s -2
1173 // (X+ -2) >s X --> X <s (MAXSINT-(-2-1)) --> X <s -126
1174 // (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128
Jim Grosbach129c52a2011-09-30 18:09:53 +00001175
Chris Lattner2188e402010-01-04 07:37:31 +00001176 assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE);
Jakub Staszakbddea112013-06-06 20:18:46 +00001177 Constant *C = Builder->getInt(CI->getValue()-1);
Chris Lattner2188e402010-01-04 07:37:31 +00001178 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantExpr::getSub(SMax, C));
1179}
1180
Sanjay Patel8da42cc2016-09-15 22:26:31 +00001181/// Handle "(icmp eq/ne (ashr/lshr AP2, A), AP1)" ->
1182/// (icmp eq/ne A, Log2(AP2/AP1)) ->
1183/// (icmp eq/ne A, Log2(AP2) - Log2(AP1)).
1184Instruction *InstCombiner::foldICmpShrConstConst(ICmpInst &I, Value *A,
1185 const APInt &AP1,
1186 const APInt &AP2) {
Suyog Sarda3a8c2c12014-07-22 19:19:36 +00001187 assert(I.isEquality() && "Cannot fold icmp gt/lt");
1188
Suyog Sarda3a8c2c12014-07-22 19:19:36 +00001189 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
1190 if (I.getPredicate() == I.ICMP_NE)
1191 Pred = CmpInst::getInversePredicate(Pred);
1192 return new ICmpInst(Pred, LHS, RHS);
1193 };
1194
David Majnemer2abb8182014-10-25 07:13:13 +00001195 // Don't bother doing any work for cases which InstSimplify handles.
Craig Topper73ba1c82017-06-07 07:40:37 +00001196 if (AP2.isNullValue())
David Majnemer2abb8182014-10-25 07:13:13 +00001197 return nullptr;
Sanjay Patel8da42cc2016-09-15 22:26:31 +00001198
1199 bool IsAShr = isa<AShrOperator>(I.getOperand(0));
David Majnemer2abb8182014-10-25 07:13:13 +00001200 if (IsAShr) {
1201 if (AP2.isAllOnesValue())
1202 return nullptr;
1203 if (AP2.isNegative() != AP1.isNegative())
1204 return nullptr;
1205 if (AP2.sgt(AP1))
1206 return nullptr;
1207 }
Suyog Sarda3a8c2c12014-07-22 19:19:36 +00001208
David Majnemerd2056022014-10-21 19:51:55 +00001209 if (!AP1)
Suyog Sarda3a8c2c12014-07-22 19:19:36 +00001210 // 'A' must be large enough to shift out the highest set bit.
1211 return getICmp(I.ICMP_UGT, A,
1212 ConstantInt::get(A->getType(), AP2.logBase2()));
Suyog Sarda3a8c2c12014-07-22 19:19:36 +00001213
David Majnemerd2056022014-10-21 19:51:55 +00001214 if (AP1 == AP2)
1215 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));
Suyog Sarda3a8c2c12014-07-22 19:19:36 +00001216
Andrea Di Biagio5b92b492014-09-17 11:32:31 +00001217 int Shift;
David Majnemerd2056022014-10-21 19:51:55 +00001218 if (IsAShr && AP1.isNegative())
David Majnemere5977eb2015-09-19 00:48:26 +00001219 Shift = AP1.countLeadingOnes() - AP2.countLeadingOnes();
Andrea Di Biagio5b92b492014-09-17 11:32:31 +00001220 else
David Majnemere5977eb2015-09-19 00:48:26 +00001221 Shift = AP1.countLeadingZeros() - AP2.countLeadingZeros();
Suyog Sarda3a8c2c12014-07-22 19:19:36 +00001222
David Majnemerd2056022014-10-21 19:51:55 +00001223 if (Shift > 0) {
David Majnemere5977eb2015-09-19 00:48:26 +00001224 if (IsAShr && AP1 == AP2.ashr(Shift)) {
1225 // There are multiple solutions if we are comparing against -1 and the LHS
David Majnemer47ce0b82015-09-19 00:48:31 +00001226 // of the ashr is not a power of two.
David Majnemere5977eb2015-09-19 00:48:26 +00001227 if (AP1.isAllOnesValue() && !AP2.isPowerOf2())
1228 return getICmp(I.ICMP_UGE, A, ConstantInt::get(A->getType(), Shift));
David Majnemerd2056022014-10-21 19:51:55 +00001229 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
David Majnemere5977eb2015-09-19 00:48:26 +00001230 } else if (AP1 == AP2.lshr(Shift)) {
1231 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1232 }
David Majnemerd2056022014-10-21 19:51:55 +00001233 }
Sanjay Patel524fcdf2016-09-15 19:04:55 +00001234
Suyog Sarda3a8c2c12014-07-22 19:19:36 +00001235 // Shifting const2 will never be equal to const1.
Sanjay Patel524fcdf2016-09-15 19:04:55 +00001236 // FIXME: This should always be handled by InstSimplify?
1237 auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE);
1238 return replaceInstUsesWith(I, TorF);
Suyog Sarda3a8c2c12014-07-22 19:19:36 +00001239}
Chris Lattner2188e402010-01-04 07:37:31 +00001240
Sanjay Patel8da42cc2016-09-15 22:26:31 +00001241/// Handle "(icmp eq/ne (shl AP2, A), AP1)" ->
1242/// (icmp eq/ne A, TrailingZeros(AP1) - TrailingZeros(AP2)).
1243Instruction *InstCombiner::foldICmpShlConstConst(ICmpInst &I, Value *A,
1244 const APInt &AP1,
1245 const APInt &AP2) {
David Majnemer59939ac2014-10-19 08:23:08 +00001246 assert(I.isEquality() && "Cannot fold icmp gt/lt");
1247
David Majnemer59939ac2014-10-19 08:23:08 +00001248 auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
1249 if (I.getPredicate() == I.ICMP_NE)
1250 Pred = CmpInst::getInversePredicate(Pred);
1251 return new ICmpInst(Pred, LHS, RHS);
1252 };
1253
David Majnemer2abb8182014-10-25 07:13:13 +00001254 // Don't bother doing any work for cases which InstSimplify handles.
Craig Topper73ba1c82017-06-07 07:40:37 +00001255 if (AP2.isNullValue())
David Majnemer2abb8182014-10-25 07:13:13 +00001256 return nullptr;
David Majnemer59939ac2014-10-19 08:23:08 +00001257
1258 unsigned AP2TrailingZeros = AP2.countTrailingZeros();
1259
1260 if (!AP1 && AP2TrailingZeros != 0)
Sanjay Patelaf91d1f2016-09-15 21:35:30 +00001261 return getICmp(
1262 I.ICMP_UGE, A,
1263 ConstantInt::get(A->getType(), AP2.getBitWidth() - AP2TrailingZeros));
David Majnemer59939ac2014-10-19 08:23:08 +00001264
1265 if (AP1 == AP2)
1266 return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));
1267
1268 // Get the distance between the lowest bits that are set.
1269 int Shift = AP1.countTrailingZeros() - AP2TrailingZeros;
1270
1271 if (Shift > 0 && AP2.shl(Shift) == AP1)
1272 return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1273
1274 // Shifting const2 will never be equal to const1.
Sanjay Patel524fcdf2016-09-15 19:04:55 +00001275 // FIXME: This should always be handled by InstSimplify?
1276 auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE);
1277 return replaceInstUsesWith(I, TorF);
David Majnemer59939ac2014-10-19 08:23:08 +00001278}
1279
Sanjay Patel06b127a2016-09-15 14:37:50 +00001280/// The caller has matched a pattern of the form:
1281/// I = icmp ugt (add (add A, B), CI2), CI1
1282/// If this is of the form:
1283/// sum = a + b
1284/// if (sum+128 >u 255)
1285/// Then replace it with llvm.sadd.with.overflow.i8.
1286///
Sanjay Pateld93c4c02016-09-15 18:22:25 +00001287static Instruction *processUGT_ADDCST_ADD(ICmpInst &I, Value *A, Value *B,
Sanjay Patel06b127a2016-09-15 14:37:50 +00001288 ConstantInt *CI2, ConstantInt *CI1,
1289 InstCombiner &IC) {
1290 // The transformation we're trying to do here is to transform this into an
1291 // llvm.sadd.with.overflow. To do this, we have to replace the original add
1292 // with a narrower add, and discard the add-with-constant that is part of the
1293 // range check (if we can't eliminate it, this isn't profitable).
1294
1295 // In order to eliminate the add-with-constant, the compare can be its only
1296 // use.
1297 Instruction *AddWithCst = cast<Instruction>(I.getOperand(0));
1298 if (!AddWithCst->hasOneUse())
1299 return nullptr;
1300
1301 // If CI2 is 2^7, 2^15, 2^31, then it might be an sadd.with.overflow.
1302 if (!CI2->getValue().isPowerOf2())
1303 return nullptr;
1304 unsigned NewWidth = CI2->getValue().countTrailingZeros();
1305 if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31)
1306 return nullptr;
1307
1308 // The width of the new add formed is 1 more than the bias.
1309 ++NewWidth;
1310
1311 // Check to see that CI1 is an all-ones value with NewWidth bits.
1312 if (CI1->getBitWidth() == NewWidth ||
1313 CI1->getValue() != APInt::getLowBitsSet(CI1->getBitWidth(), NewWidth))
1314 return nullptr;
1315
1316 // This is only really a signed overflow check if the inputs have been
1317 // sign-extended; check for that condition. For example, if CI2 is 2^31 and
1318 // the operands of the add are 64 bits wide, we need at least 33 sign bits.
1319 unsigned NeededSignBits = CI1->getBitWidth() - NewWidth + 1;
1320 if (IC.ComputeNumSignBits(A, 0, &I) < NeededSignBits ||
1321 IC.ComputeNumSignBits(B, 0, &I) < NeededSignBits)
1322 return nullptr;
1323
1324 // In order to replace the original add with a narrower
1325 // llvm.sadd.with.overflow, the only uses allowed are the add-with-constant
1326 // and truncates that discard the high bits of the add. Verify that this is
1327 // the case.
1328 Instruction *OrigAdd = cast<Instruction>(AddWithCst->getOperand(0));
1329 for (User *U : OrigAdd->users()) {
1330 if (U == AddWithCst)
1331 continue;
1332
1333 // Only accept truncates for now. We would really like a nice recursive
1334 // predicate like SimplifyDemandedBits, but which goes downwards the use-def
1335 // chain to see which bits of a value are actually demanded. If the
1336 // original add had another add which was then immediately truncated, we
1337 // could still do the transformation.
1338 TruncInst *TI = dyn_cast<TruncInst>(U);
1339 if (!TI || TI->getType()->getPrimitiveSizeInBits() > NewWidth)
1340 return nullptr;
1341 }
1342
1343 // If the pattern matches, truncate the inputs to the narrower type and
1344 // use the sadd_with_overflow intrinsic to efficiently compute both the
1345 // result and the overflow bit.
1346 Type *NewType = IntegerType::get(OrigAdd->getContext(), NewWidth);
1347 Value *F = Intrinsic::getDeclaration(I.getModule(),
1348 Intrinsic::sadd_with_overflow, NewType);
1349
1350 InstCombiner::BuilderTy *Builder = IC.Builder;
1351
1352 // Put the new code above the original add, in case there are any uses of the
1353 // add between the add and the compare.
1354 Builder->SetInsertPoint(OrigAdd);
1355
1356 Value *TruncA = Builder->CreateTrunc(A, NewType, A->getName() + ".trunc");
1357 Value *TruncB = Builder->CreateTrunc(B, NewType, B->getName() + ".trunc");
1358 CallInst *Call = Builder->CreateCall(F, {TruncA, TruncB}, "sadd");
1359 Value *Add = Builder->CreateExtractValue(Call, 0, "sadd.result");
1360 Value *ZExt = Builder->CreateZExt(Add, OrigAdd->getType());
1361
1362 // The inner add was the result of the narrow add, zero extended to the
1363 // wider type. Replace it with the result computed by the intrinsic.
1364 IC.replaceInstUsesWith(*OrigAdd, ZExt);
1365
1366 // The original icmp gets replaced with the overflow value.
1367 return ExtractValueInst::Create(Call, 1, "sadd.overflow");
1368}
1369
1370// Fold icmp Pred X, C.
Sanjay Patel97459832016-09-15 15:11:12 +00001371Instruction *InstCombiner::foldICmpWithConstant(ICmpInst &Cmp) {
1372 CmpInst::Predicate Pred = Cmp.getPredicate();
Sanjay Patel40c53ea2016-09-15 16:23:20 +00001373 Value *X = Cmp.getOperand(0);
Sanjay Patel06b127a2016-09-15 14:37:50 +00001374
Sanjay Patel40c53ea2016-09-15 16:23:20 +00001375 const APInt *C;
1376 if (!match(Cmp.getOperand(1), m_APInt(C)))
Sanjay Patel97459832016-09-15 15:11:12 +00001377 return nullptr;
Sanjay Patel06b127a2016-09-15 14:37:50 +00001378
Sanjay Patel97459832016-09-15 15:11:12 +00001379 Value *A = nullptr, *B = nullptr;
Sanjay Patel06b127a2016-09-15 14:37:50 +00001380
Sanjay Patel97459832016-09-15 15:11:12 +00001381 // Match the following pattern, which is a common idiom when writing
1382 // overflow-safe integer arithmetic functions. The source performs an addition
1383 // in wider type and explicitly checks for overflow using comparisons against
1384 // INT_MIN and INT_MAX. Simplify by using the sadd_with_overflow intrinsic.
1385 //
1386 // TODO: This could probably be generalized to handle other overflow-safe
1387 // operations if we worked out the formulas to compute the appropriate magic
1388 // constants.
1389 //
1390 // sum = a + b
1391 // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8
1392 {
1393 ConstantInt *CI2; // I = icmp ugt (add (add A, B), CI2), CI
1394 if (Pred == ICmpInst::ICMP_UGT &&
1395 match(X, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2))))
Sanjay Pateld93c4c02016-09-15 18:22:25 +00001396 if (Instruction *Res = processUGT_ADDCST_ADD(
Sanjay Patel40c53ea2016-09-15 16:23:20 +00001397 Cmp, A, B, CI2, cast<ConstantInt>(Cmp.getOperand(1)), *this))
Sanjay Patel97459832016-09-15 15:11:12 +00001398 return Res;
1399 }
Sanjay Patel06b127a2016-09-15 14:37:50 +00001400
Sanjay Patel97459832016-09-15 15:11:12 +00001401 // (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0)
Craig Topper73ba1c82017-06-07 07:40:37 +00001402 if (C->isNullValue() && Pred == ICmpInst::ICMP_SGT) {
Sanjay Patel40c53ea2016-09-15 16:23:20 +00001403 SelectPatternResult SPR = matchSelectPattern(X, A, B);
1404 if (SPR.Flavor == SPF_SMIN) {
Craig Topperd45185f2017-05-26 18:23:57 +00001405 if (isKnownPositive(A, DL, 0, &AC, &Cmp, &DT))
Sanjay Patel40c53ea2016-09-15 16:23:20 +00001406 return new ICmpInst(Pred, B, Cmp.getOperand(1));
Craig Topperd45185f2017-05-26 18:23:57 +00001407 if (isKnownPositive(B, DL, 0, &AC, &Cmp, &DT))
Sanjay Patel40c53ea2016-09-15 16:23:20 +00001408 return new ICmpInst(Pred, A, Cmp.getOperand(1));
Sanjay Patel06b127a2016-09-15 14:37:50 +00001409 }
Sanjay Patel40c53ea2016-09-15 16:23:20 +00001410 }
1411
1412 // FIXME: Use m_APInt to allow folds for splat constants.
1413 ConstantInt *CI = dyn_cast<ConstantInt>(Cmp.getOperand(1));
1414 if (!CI)
1415 return nullptr;
Sanjay Patel06b127a2016-09-15 14:37:50 +00001416
Sanjay Patel97459832016-09-15 15:11:12 +00001417 // Canonicalize icmp instructions based on dominating conditions.
1418 BasicBlock *Parent = Cmp.getParent();
1419 BasicBlock *Dom = Parent->getSinglePredecessor();
1420 auto *BI = Dom ? dyn_cast<BranchInst>(Dom->getTerminator()) : nullptr;
1421 ICmpInst::Predicate Pred2;
1422 BasicBlock *TrueBB, *FalseBB;
1423 ConstantInt *CI2;
1424 if (BI && match(BI, m_Br(m_ICmp(Pred2, m_Specific(X), m_ConstantInt(CI2)),
1425 TrueBB, FalseBB)) &&
1426 TrueBB != FalseBB) {
1427 ConstantRange CR =
1428 ConstantRange::makeAllowedICmpRegion(Pred, CI->getValue());
1429 ConstantRange DominatingCR =
1430 (Parent == TrueBB)
1431 ? ConstantRange::makeExactICmpRegion(Pred2, CI2->getValue())
1432 : ConstantRange::makeExactICmpRegion(
1433 CmpInst::getInversePredicate(Pred2), CI2->getValue());
1434 ConstantRange Intersection = DominatingCR.intersectWith(CR);
1435 ConstantRange Difference = DominatingCR.difference(CR);
1436 if (Intersection.isEmptySet())
1437 return replaceInstUsesWith(Cmp, Builder->getFalse());
1438 if (Difference.isEmptySet())
1439 return replaceInstUsesWith(Cmp, Builder->getTrue());
Sanjay Patel06b127a2016-09-15 14:37:50 +00001440
Sanjay Patel97459832016-09-15 15:11:12 +00001441 // If this is a normal comparison, it demands all bits. If it is a sign
1442 // bit comparison, it only demands the sign bit.
1443 bool UnusedBit;
1444 bool IsSignBit = isSignBitCheck(Pred, CI->getValue(), UnusedBit);
1445
1446 // Canonicalizing a sign bit comparison that gets used in a branch,
1447 // pessimizes codegen by generating branch on zero instruction instead
1448 // of a test and branch. So we avoid canonicalizing in such situations
1449 // because test and branch instruction has better branch displacement
1450 // than compare and branch instruction.
1451 if (!isBranchOnSignBitCheck(Cmp, IsSignBit) && !Cmp.isEquality()) {
1452 if (auto *AI = Intersection.getSingleElement())
1453 return new ICmpInst(ICmpInst::ICMP_EQ, X, Builder->getInt(*AI));
1454 if (auto *AD = Difference.getSingleElement())
1455 return new ICmpInst(ICmpInst::ICMP_NE, X, Builder->getInt(*AD));
Sanjay Patel06b127a2016-09-15 14:37:50 +00001456 }
1457 }
1458
1459 return nullptr;
1460}
1461
Sanjay Patel5f4ce4e2016-08-18 20:25:16 +00001462/// Fold icmp (trunc X, Y), C.
1463Instruction *InstCombiner::foldICmpTruncConstant(ICmpInst &Cmp,
1464 Instruction *Trunc,
1465 const APInt *C) {
Sanjay Patel5f4ce4e2016-08-18 20:25:16 +00001466 ICmpInst::Predicate Pred = Cmp.getPredicate();
1467 Value *X = Trunc->getOperand(0);
Craig Topper73ba1c82017-06-07 07:40:37 +00001468 if (C->isOneValue() && C->getBitWidth() > 1) {
Sanjay Patela3f4f082016-08-16 17:54:36 +00001469 // icmp slt trunc(signum(V)) 1 --> icmp slt V, 1
1470 Value *V = nullptr;
Sanjay Patel5f4ce4e2016-08-18 20:25:16 +00001471 if (Pred == ICmpInst::ICMP_SLT && match(X, m_Signum(m_Value(V))))
Sanjay Patela3f4f082016-08-16 17:54:36 +00001472 return new ICmpInst(ICmpInst::ICMP_SLT, V,
1473 ConstantInt::get(V->getType(), 1));
1474 }
Sanjay Patel5f4ce4e2016-08-18 20:25:16 +00001475
1476 if (Cmp.isEquality() && Trunc->hasOneUse()) {
Sanjay Patela3f4f082016-08-16 17:54:36 +00001477 // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
1478 // of the high bits truncated out of x are known.
Sanjay Patel40e8ca42016-08-18 20:28:54 +00001479 unsigned DstBits = Trunc->getType()->getScalarSizeInBits(),
1480 SrcBits = X->getType()->getScalarSizeInBits();
Craig Topper8205a1a2017-05-24 16:53:07 +00001481 KnownBits Known = computeKnownBits(X, 0, &Cmp);
Sanjay Patela3f4f082016-08-16 17:54:36 +00001482
1483 // If all the high bits are known, we can do this xform.
Craig Topperb45eabc2017-04-26 16:39:58 +00001484 if ((Known.Zero | Known.One).countLeadingOnes() >= SrcBits - DstBits) {
Sanjay Patela3f4f082016-08-16 17:54:36 +00001485 // Pull in the high bits from known-ones set.
Sanjay Patel5f4ce4e2016-08-18 20:25:16 +00001486 APInt NewRHS = C->zext(SrcBits);
Craig Topperb45eabc2017-04-26 16:39:58 +00001487 NewRHS |= Known.One & APInt::getHighBitsSet(SrcBits, SrcBits - DstBits);
Sanjay Patel40e8ca42016-08-18 20:28:54 +00001488 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), NewRHS));
Sanjay Patela3f4f082016-08-16 17:54:36 +00001489 }
1490 }
Sanjay Patel5f4ce4e2016-08-18 20:25:16 +00001491
Sanjay Patela3f4f082016-08-16 17:54:36 +00001492 return nullptr;
1493}
1494
Sanjay Patel6d5f4482016-08-17 19:23:42 +00001495/// Fold icmp (xor X, Y), C.
Sanjay Patelc9196c42016-08-22 21:24:29 +00001496Instruction *InstCombiner::foldICmpXorConstant(ICmpInst &Cmp,
1497 BinaryOperator *Xor,
Sanjay Patel6d5f4482016-08-17 19:23:42 +00001498 const APInt *C) {
Sanjay Patel4c5e60d2016-08-18 14:10:48 +00001499 Value *X = Xor->getOperand(0);
1500 Value *Y = Xor->getOperand(1);
Sanjay Pateldaffec912016-08-17 19:45:18 +00001501 const APInt *XorC;
Sanjay Patel4c5e60d2016-08-18 14:10:48 +00001502 if (!match(Y, m_APInt(XorC)))
Sanjay Patel6d5f4482016-08-17 19:23:42 +00001503 return nullptr;
Sanjay Patela3f4f082016-08-16 17:54:36 +00001504
Sanjay Patel6d5f4482016-08-17 19:23:42 +00001505 // If this is a comparison that tests the signbit (X < 0) or (x > -1),
1506 // fold the xor.
1507 ICmpInst::Predicate Pred = Cmp.getPredicate();
Craig Topper73ba1c82017-06-07 07:40:37 +00001508 if ((Pred == ICmpInst::ICMP_SLT && C->isNullValue()) ||
Sanjay Patel6d5f4482016-08-17 19:23:42 +00001509 (Pred == ICmpInst::ICMP_SGT && C->isAllOnesValue())) {
Sanjay Patela3f4f082016-08-16 17:54:36 +00001510
Sanjay Patel6d5f4482016-08-17 19:23:42 +00001511 // If the sign bit of the XorCst is not set, there is no change to
1512 // the operation, just stop using the Xor.
Sanjay Pateldaffec912016-08-17 19:45:18 +00001513 if (!XorC->isNegative()) {
1514 Cmp.setOperand(0, X);
Sanjay Patel6d5f4482016-08-17 19:23:42 +00001515 Worklist.Add(Xor);
1516 return &Cmp;
Sanjay Patela3f4f082016-08-16 17:54:36 +00001517 }
1518
Sanjay Patel6d5f4482016-08-17 19:23:42 +00001519 // Was the old condition true if the operand is positive?
1520 bool isTrueIfPositive = Pred == ICmpInst::ICMP_SGT;
Sanjay Patela3f4f082016-08-16 17:54:36 +00001521
Sanjay Patel6d5f4482016-08-17 19:23:42 +00001522 // If so, the new one isn't.
1523 isTrueIfPositive ^= true;
Sanjay Patela3f4f082016-08-16 17:54:36 +00001524
Sanjay Patel4c5e60d2016-08-18 14:10:48 +00001525 Constant *CmpConstant = cast<Constant>(Cmp.getOperand(1));
Sanjay Patel6d5f4482016-08-17 19:23:42 +00001526 if (isTrueIfPositive)
Sanjay Patel4c5e60d2016-08-18 14:10:48 +00001527 return new ICmpInst(ICmpInst::ICMP_SGT, X, SubOne(CmpConstant));
Sanjay Patel6d5f4482016-08-17 19:23:42 +00001528 else
Sanjay Patel4c5e60d2016-08-18 14:10:48 +00001529 return new ICmpInst(ICmpInst::ICMP_SLT, X, AddOne(CmpConstant));
Sanjay Patela3f4f082016-08-16 17:54:36 +00001530 }
Sanjay Patel6d5f4482016-08-17 19:23:42 +00001531
1532 if (Xor->hasOneUse()) {
Craig Topperbcfd2d12017-04-20 16:56:25 +00001533 // (icmp u/s (xor X SignMask), C) -> (icmp s/u X, (xor C SignMask))
1534 if (!Cmp.isEquality() && XorC->isSignMask()) {
Sanjay Pateldaffec912016-08-17 19:45:18 +00001535 Pred = Cmp.isSigned() ? Cmp.getUnsignedPredicate()
1536 : Cmp.getSignedPredicate();
Sanjay Patel4c5e60d2016-08-18 14:10:48 +00001537 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), *C ^ *XorC));
Sanjay Patel6d5f4482016-08-17 19:23:42 +00001538 }
1539
Craig Topperbcfd2d12017-04-20 16:56:25 +00001540 // (icmp u/s (xor X ~SignMask), C) -> (icmp s/u X, (xor C ~SignMask))
Sanjay Pateldaffec912016-08-17 19:45:18 +00001541 if (!Cmp.isEquality() && XorC->isMaxSignedValue()) {
1542 Pred = Cmp.isSigned() ? Cmp.getUnsignedPredicate()
1543 : Cmp.getSignedPredicate();
Sanjay Patel6d5f4482016-08-17 19:23:42 +00001544 Pred = Cmp.getSwappedPredicate(Pred);
Sanjay Patel4c5e60d2016-08-18 14:10:48 +00001545 return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), *C ^ *XorC));
Sanjay Patel6d5f4482016-08-17 19:23:42 +00001546 }
1547 }
1548
1549 // (icmp ugt (xor X, C), ~C) -> (icmp ult X, C)
1550 // iff -C is a power of 2
Sanjay Pateldaffec912016-08-17 19:45:18 +00001551 if (Pred == ICmpInst::ICMP_UGT && *XorC == ~(*C) && (*C + 1).isPowerOf2())
Sanjay Patel4c5e60d2016-08-18 14:10:48 +00001552 return new ICmpInst(ICmpInst::ICMP_ULT, X, Y);
Sanjay Patel6d5f4482016-08-17 19:23:42 +00001553
1554 // (icmp ult (xor X, C), -C) -> (icmp uge X, C)
1555 // iff -C is a power of 2
Sanjay Pateldaffec912016-08-17 19:45:18 +00001556 if (Pred == ICmpInst::ICMP_ULT && *XorC == -(*C) && C->isPowerOf2())
Sanjay Patel4c5e60d2016-08-18 14:10:48 +00001557 return new ICmpInst(ICmpInst::ICMP_UGE, X, Y);
Sanjay Patel6d5f4482016-08-17 19:23:42 +00001558
Sanjay Patela3f4f082016-08-16 17:54:36 +00001559 return nullptr;
1560}
1561
Sanjay Patel14e0e182016-08-26 18:28:46 +00001562/// Fold icmp (and (sh X, Y), C2), C1.
1563Instruction *InstCombiner::foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And,
Sanjay Patel9b40f982016-09-07 22:33:03 +00001564 const APInt *C1, const APInt *C2) {
1565 BinaryOperator *Shift = dyn_cast<BinaryOperator>(And->getOperand(0));
1566 if (!Shift || !Shift->isShift())
Sanjay Patelda9c5622016-08-26 17:15:22 +00001567 return nullptr;
Sanjay Patela3f4f082016-08-16 17:54:36 +00001568
Sanjay Patelda9c5622016-08-26 17:15:22 +00001569 // If this is: (X >> C3) & C2 != C1 (where any shift and any compare could
1570 // exist), turn it into (X & (C2 << C3)) != (C1 << C3). This happens a LOT in
1571 // code produced by the clang front-end, for bitfield access.
Sanjay Patelda9c5622016-08-26 17:15:22 +00001572 // This seemingly simple opportunity to fold away a shift turns out to be
1573 // rather complicated. See PR17827 for details.
Sanjay Patel9b40f982016-09-07 22:33:03 +00001574 unsigned ShiftOpcode = Shift->getOpcode();
1575 bool IsShl = ShiftOpcode == Instruction::Shl;
1576 const APInt *C3;
1577 if (match(Shift->getOperand(1), m_APInt(C3))) {
Sanjay Patelda9c5622016-08-26 17:15:22 +00001578 bool CanFold = false;
Sanjay Patelda9c5622016-08-26 17:15:22 +00001579 if (ShiftOpcode == Instruction::AShr) {
1580 // There may be some constraints that make this possible, but nothing
1581 // simple has been discovered yet.
1582 CanFold = false;
1583 } else if (ShiftOpcode == Instruction::Shl) {
1584 // For a left shift, we can fold if the comparison is not signed. We can
1585 // also fold a signed comparison if the mask value and comparison value
1586 // are not negative. These constraints may not be obvious, but we can
1587 // prove that they are correct using an SMT solver.
Sanjay Patel9b40f982016-09-07 22:33:03 +00001588 if (!Cmp.isSigned() || (!C2->isNegative() && !C1->isNegative()))
Sanjay Patelda9c5622016-08-26 17:15:22 +00001589 CanFold = true;
1590 } else if (ShiftOpcode == Instruction::LShr) {
1591 // For a logical right shift, we can fold if the comparison is not signed.
1592 // We can also fold a signed comparison if the shifted mask value and the
1593 // shifted comparison value are not negative. These constraints may not be
1594 // obvious, but we can prove that they are correct using an SMT solver.
Sanjay Patel9b40f982016-09-07 22:33:03 +00001595 if (!Cmp.isSigned() ||
1596 (!C2->shl(*C3).isNegative() && !C1->shl(*C3).isNegative()))
Sanjay Patelda9c5622016-08-26 17:15:22 +00001597 CanFold = true;
Sanjay Patela3f4f082016-08-16 17:54:36 +00001598 }
1599
Sanjay Patelda9c5622016-08-26 17:15:22 +00001600 if (CanFold) {
Sanjay Patel9b40f982016-09-07 22:33:03 +00001601 APInt NewCst = IsShl ? C1->lshr(*C3) : C1->shl(*C3);
1602 APInt SameAsC1 = IsShl ? NewCst.shl(*C3) : NewCst.lshr(*C3);
Sanjay Patelda9c5622016-08-26 17:15:22 +00001603 // Check to see if we are shifting out any of the bits being compared.
Sanjay Patel9b40f982016-09-07 22:33:03 +00001604 if (SameAsC1 != *C1) {
Sanjay Patelda9c5622016-08-26 17:15:22 +00001605 // If we shifted bits out, the fold is not going to work out. As a
1606 // special case, check to see if this means that the result is always
1607 // true or false now.
1608 if (Cmp.getPredicate() == ICmpInst::ICMP_EQ)
Sanjay Patel1c608f42016-09-08 16:54:02 +00001609 return replaceInstUsesWith(Cmp, ConstantInt::getFalse(Cmp.getType()));
Sanjay Patelda9c5622016-08-26 17:15:22 +00001610 if (Cmp.getPredicate() == ICmpInst::ICMP_NE)
Sanjay Patel1c608f42016-09-08 16:54:02 +00001611 return replaceInstUsesWith(Cmp, ConstantInt::getTrue(Cmp.getType()));
Sanjay Patela3f4f082016-08-16 17:54:36 +00001612 } else {
Sanjay Patel9b40f982016-09-07 22:33:03 +00001613 Cmp.setOperand(1, ConstantInt::get(And->getType(), NewCst));
1614 APInt NewAndCst = IsShl ? C2->lshr(*C3) : C2->shl(*C3);
1615 And->setOperand(1, ConstantInt::get(And->getType(), NewAndCst));
Sanjay Patelda9c5622016-08-26 17:15:22 +00001616 And->setOperand(0, Shift->getOperand(0));
1617 Worklist.Add(Shift); // Shift is dead.
1618 return &Cmp;
Sanjay Patela3f4f082016-08-16 17:54:36 +00001619 }
Sanjay Patelda9c5622016-08-26 17:15:22 +00001620 }
1621 }
Sanjay Patela3f4f082016-08-16 17:54:36 +00001622
Sanjay Patelda9c5622016-08-26 17:15:22 +00001623 // Turn ((X >> Y) & C2) == 0 into (X & (C2 << Y)) == 0. The latter is
1624 // preferable because it allows the C2 << Y expression to be hoisted out of a
1625 // loop if Y is invariant and X is not.
Craig Topper73ba1c82017-06-07 07:40:37 +00001626 if (Shift->hasOneUse() && C1->isNullValue() && Cmp.isEquality() &&
Sanjay Patelda9c5622016-08-26 17:15:22 +00001627 !Shift->isArithmeticShift() && !isa<Constant>(Shift->getOperand(0))) {
1628 // Compute C2 << Y.
Sanjay Patel9b40f982016-09-07 22:33:03 +00001629 Value *NewShift =
1630 IsShl ? Builder->CreateLShr(And->getOperand(1), Shift->getOperand(1))
1631 : Builder->CreateShl(And->getOperand(1), Shift->getOperand(1));
Sanjay Patela3f4f082016-08-16 17:54:36 +00001632
Sanjay Patelda9c5622016-08-26 17:15:22 +00001633 // Compute X & (C2 << Y).
Sanjay Patel9b40f982016-09-07 22:33:03 +00001634 Value *NewAnd = Builder->CreateAnd(Shift->getOperand(0), NewShift);
Sanjay Patelda9c5622016-08-26 17:15:22 +00001635 Cmp.setOperand(0, NewAnd);
1636 return &Cmp;
1637 }
1638
Sanjay Patel14e0e182016-08-26 18:28:46 +00001639 return nullptr;
1640}
1641
1642/// Fold icmp (and X, C2), C1.
1643Instruction *InstCombiner::foldICmpAndConstConst(ICmpInst &Cmp,
1644 BinaryOperator *And,
1645 const APInt *C1) {
Sanjay Patel6b490972016-09-04 14:32:15 +00001646 const APInt *C2;
1647 if (!match(And->getOperand(1), m_APInt(C2)))
Sanjay Patel14e0e182016-08-26 18:28:46 +00001648 return nullptr;
1649
1650 if (!And->hasOneUse() || !And->getOperand(0)->hasOneUse())
1651 return nullptr;
1652
Sanjay Patel6b490972016-09-04 14:32:15 +00001653 // If the LHS is an 'and' of a truncate and we can widen the and/compare to
1654 // the input width without changing the value produced, eliminate the cast:
1655 //
1656 // icmp (and (trunc W), C2), C1 -> icmp (and W, C2'), C1'
1657 //
1658 // We can do this transformation if the constants do not have their sign bits
1659 // set or if it is an equality comparison. Extending a relational comparison
1660 // when we're checking the sign bit would not work.
1661 Value *W;
1662 if (match(And->getOperand(0), m_Trunc(m_Value(W))) &&
1663 (Cmp.isEquality() || (!C1->isNegative() && !C2->isNegative()))) {
1664 // TODO: Is this a good transform for vectors? Wider types may reduce
1665 // throughput. Should this transform be limited (even for scalars) by using
Sanjay Patel2217f752017-01-31 17:25:42 +00001666 // shouldChangeType()?
Sanjay Patel6b490972016-09-04 14:32:15 +00001667 if (!Cmp.getType()->isVectorTy()) {
1668 Type *WideType = W->getType();
1669 unsigned WideScalarBits = WideType->getScalarSizeInBits();
1670 Constant *ZextC1 = ConstantInt::get(WideType, C1->zext(WideScalarBits));
1671 Constant *ZextC2 = ConstantInt::get(WideType, C2->zext(WideScalarBits));
1672 Value *NewAnd = Builder->CreateAnd(W, ZextC2, And->getName());
1673 return new ICmpInst(Cmp.getPredicate(), NewAnd, ZextC1);
Sanjay Patel14e0e182016-08-26 18:28:46 +00001674 }
1675 }
1676
Sanjay Patel9b40f982016-09-07 22:33:03 +00001677 if (Instruction *I = foldICmpAndShift(Cmp, And, C1, C2))
Sanjay Patel14e0e182016-08-26 18:28:46 +00001678 return I;
1679
Sanjay Patelda9c5622016-08-26 17:15:22 +00001680 // (icmp pred (and (or (lshr A, B), A), 1), 0) -->
Sanjay Patel6b490972016-09-04 14:32:15 +00001681 // (icmp pred (and A, (or (shl 1, B), 1), 0))
Sanjay Patelda9c5622016-08-26 17:15:22 +00001682 //
1683 // iff pred isn't signed
Craig Topper73ba1c82017-06-07 07:40:37 +00001684 if (!Cmp.isSigned() && C1->isNullValue() &&
1685 match(And->getOperand(1), m_One())) {
Sanjay Pateldef931e2016-09-07 20:50:44 +00001686 Constant *One = cast<Constant>(And->getOperand(1));
1687 Value *Or = And->getOperand(0);
Sanjay Patelda9c5622016-08-26 17:15:22 +00001688 Value *A, *B, *LShr;
Sanjay Pateldef931e2016-09-07 20:50:44 +00001689 if (match(Or, m_Or(m_Value(LShr), m_Value(A))) &&
1690 match(LShr, m_LShr(m_Specific(A), m_Value(B)))) {
1691 unsigned UsesRemoved = 0;
1692 if (And->hasOneUse())
1693 ++UsesRemoved;
1694 if (Or->hasOneUse())
1695 ++UsesRemoved;
1696 if (LShr->hasOneUse())
1697 ++UsesRemoved;
1698
1699 // Compute A & ((1 << B) | 1)
1700 Value *NewOr = nullptr;
1701 if (auto *C = dyn_cast<Constant>(B)) {
1702 if (UsesRemoved >= 1)
1703 NewOr = ConstantExpr::getOr(ConstantExpr::getNUWShl(One, C), One);
1704 } else {
1705 if (UsesRemoved >= 3)
1706 NewOr = Builder->CreateOr(Builder->CreateShl(One, B, LShr->getName(),
Sanjay Patelda9c5622016-08-26 17:15:22 +00001707 /*HasNUW=*/true),
1708 One, Or->getName());
Sanjay Pateldef931e2016-09-07 20:50:44 +00001709 }
1710 if (NewOr) {
1711 Value *NewAnd = Builder->CreateAnd(A, NewOr, And->getName());
1712 Cmp.setOperand(0, NewAnd);
1713 return &Cmp;
Sanjay Patela3f4f082016-08-16 17:54:36 +00001714 }
1715 }
Sanjay Patela3f4f082016-08-16 17:54:36 +00001716 }
Sanjay Patelda9c5622016-08-26 17:15:22 +00001717
Sanjay Pateldef931e2016-09-07 20:50:44 +00001718 // (X & C2) > C1 --> (X & C2) != 0, if any bit set in (X & C2) will produce a
1719 // result greater than C1.
1720 unsigned NumTZ = C2->countTrailingZeros();
1721 if (Cmp.getPredicate() == ICmpInst::ICMP_UGT && NumTZ < C2->getBitWidth() &&
1722 APInt::getOneBitSet(C2->getBitWidth(), NumTZ).ugt(*C1)) {
1723 Constant *Zero = Constant::getNullValue(And->getType());
1724 return new ICmpInst(ICmpInst::ICMP_NE, And, Zero);
Sanjay Patelda9c5622016-08-26 17:15:22 +00001725 }
1726
Sanjay Pateld3c7bb282016-08-26 16:42:33 +00001727 return nullptr;
1728}
1729
1730/// Fold icmp (and X, Y), C.
1731Instruction *InstCombiner::foldICmpAndConstant(ICmpInst &Cmp,
1732 BinaryOperator *And,
1733 const APInt *C) {
1734 if (Instruction *I = foldICmpAndConstConst(Cmp, And, C))
1735 return I;
1736
Sanjay Patel5c5311f2016-08-28 18:18:00 +00001737 // TODO: These all require that Y is constant too, so refactor with the above.
Sanjay Patela3f4f082016-08-16 17:54:36 +00001738
Sanjay Patel5c5311f2016-08-28 18:18:00 +00001739 // Try to optimize things like "A[i] & 42 == 0" to index computations.
1740 Value *X = And->getOperand(0);
1741 Value *Y = And->getOperand(1);
1742 if (auto *LI = dyn_cast<LoadInst>(X))
1743 if (auto *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)))
1744 if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
Sanjay Patela3f4f082016-08-16 17:54:36 +00001745 if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
Sanjay Patel5c5311f2016-08-28 18:18:00 +00001746 !LI->isVolatile() && isa<ConstantInt>(Y)) {
1747 ConstantInt *C2 = cast<ConstantInt>(Y);
1748 if (Instruction *Res = foldCmpLoadFromIndexedGlobal(GEP, GV, Cmp, C2))
Sanjay Patela3f4f082016-08-16 17:54:36 +00001749 return Res;
1750 }
Sanjay Patel5c5311f2016-08-28 18:18:00 +00001751
1752 if (!Cmp.isEquality())
1753 return nullptr;
Sanjay Patela3f4f082016-08-16 17:54:36 +00001754
1755 // X & -C == -C -> X > u ~C
1756 // X & -C != -C -> X <= u ~C
1757 // iff C is a power of 2
Sanjay Patel5c5311f2016-08-28 18:18:00 +00001758 if (Cmp.getOperand(1) == Y && (-(*C)).isPowerOf2()) {
1759 auto NewPred = Cmp.getPredicate() == CmpInst::ICMP_EQ ? CmpInst::ICMP_UGT
1760 : CmpInst::ICMP_ULE;
1761 return new ICmpInst(NewPred, X, SubOne(cast<Constant>(Cmp.getOperand(1))));
1762 }
Sanjay Patela3f4f082016-08-16 17:54:36 +00001763
Sanjay Patel5c5311f2016-08-28 18:18:00 +00001764 // (X & C2) == 0 -> (trunc X) >= 0
1765 // (X & C2) != 0 -> (trunc X) < 0
1766 // iff C2 is a power of 2 and it masks the sign bit of a legal integer type.
1767 const APInt *C2;
Craig Topper73ba1c82017-06-07 07:40:37 +00001768 if (And->hasOneUse() && C->isNullValue() && match(Y, m_APInt(C2))) {
Sanjay Patel5c5311f2016-08-28 18:18:00 +00001769 int32_t ExactLogBase2 = C2->exactLogBase2();
1770 if (ExactLogBase2 != -1 && DL.isLegalInteger(ExactLogBase2 + 1)) {
1771 Type *NTy = IntegerType::get(Cmp.getContext(), ExactLogBase2 + 1);
1772 if (And->getType()->isVectorTy())
1773 NTy = VectorType::get(NTy, And->getType()->getVectorNumElements());
1774 Value *Trunc = Builder->CreateTrunc(X, NTy);
1775 auto NewPred = Cmp.getPredicate() == CmpInst::ICMP_EQ ? CmpInst::ICMP_SGE
1776 : CmpInst::ICMP_SLT;
1777 return new ICmpInst(NewPred, Trunc, Constant::getNullValue(NTy));
Sanjay Patela3f4f082016-08-16 17:54:36 +00001778 }
1779 }
Sanjay Patel5c5311f2016-08-28 18:18:00 +00001780
Sanjay Patela3f4f082016-08-16 17:54:36 +00001781 return nullptr;
1782}
1783
Sanjay Patel943e92e2016-08-17 16:30:43 +00001784/// Fold icmp (or X, Y), C.
Sanjay Patelc9196c42016-08-22 21:24:29 +00001785Instruction *InstCombiner::foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or,
Sanjay Patel943e92e2016-08-17 16:30:43 +00001786 const APInt *C) {
Sanjay Patel943e92e2016-08-17 16:30:43 +00001787 ICmpInst::Predicate Pred = Cmp.getPredicate();
Craig Topper73ba1c82017-06-07 07:40:37 +00001788 if (C->isOneValue()) {
Sanjay Patela3f4f082016-08-16 17:54:36 +00001789 // icmp slt signum(V) 1 --> icmp slt V, 1
1790 Value *V = nullptr;
Sanjay Patel943e92e2016-08-17 16:30:43 +00001791 if (Pred == ICmpInst::ICMP_SLT && match(Or, m_Signum(m_Value(V))))
Sanjay Patela3f4f082016-08-16 17:54:36 +00001792 return new ICmpInst(ICmpInst::ICMP_SLT, V,
1793 ConstantInt::get(V->getType(), 1));
1794 }
1795
Sanjay Patel50c82c42017-04-05 17:57:05 +00001796 // X | C == C --> X <=u C
1797 // X | C != C --> X >u C
1798 // iff C+1 is a power of 2 (C is a bitmask of the low bits)
1799 if (Cmp.isEquality() && Cmp.getOperand(1) == Or->getOperand(1) &&
1800 (*C + 1).isPowerOf2()) {
1801 Pred = (Pred == CmpInst::ICMP_EQ) ? CmpInst::ICMP_ULE : CmpInst::ICMP_UGT;
1802 return new ICmpInst(Pred, Or->getOperand(0), Or->getOperand(1));
1803 }
1804
Craig Topper73ba1c82017-06-07 07:40:37 +00001805 if (!Cmp.isEquality() || !C->isNullValue() || !Or->hasOneUse())
Sanjay Patela3f4f082016-08-16 17:54:36 +00001806 return nullptr;
1807
1808 Value *P, *Q;
Sanjay Patel943e92e2016-08-17 16:30:43 +00001809 if (match(Or, m_Or(m_PtrToInt(m_Value(P)), m_PtrToInt(m_Value(Q))))) {
Sanjay Patela3f4f082016-08-16 17:54:36 +00001810 // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0
1811 // -> and (icmp eq P, null), (icmp eq Q, null).
Reid Klecknera871d382016-08-19 16:53:18 +00001812 Value *CmpP =
1813 Builder->CreateICmp(Pred, P, ConstantInt::getNullValue(P->getType()));
1814 Value *CmpQ =
1815 Builder->CreateICmp(Pred, Q, ConstantInt::getNullValue(Q->getType()));
Sanjay Patel943e92e2016-08-17 16:30:43 +00001816 auto LogicOpc = Pred == ICmpInst::Predicate::ICMP_EQ ? Instruction::And
1817 : Instruction::Or;
1818 return BinaryOperator::Create(LogicOpc, CmpP, CmpQ);
Sanjay Patela3f4f082016-08-16 17:54:36 +00001819 }
Sanjay Patel943e92e2016-08-17 16:30:43 +00001820
Sanjay Patela3f4f082016-08-16 17:54:36 +00001821 return nullptr;
1822}
1823
Sanjay Patel63478072016-08-18 15:44:44 +00001824/// Fold icmp (mul X, Y), C.
Sanjay Patelc9196c42016-08-22 21:24:29 +00001825Instruction *InstCombiner::foldICmpMulConstant(ICmpInst &Cmp,
1826 BinaryOperator *Mul,
Sanjay Patel63478072016-08-18 15:44:44 +00001827 const APInt *C) {
1828 const APInt *MulC;
1829 if (!match(Mul->getOperand(1), m_APInt(MulC)))
Sanjay Patela3f4f082016-08-16 17:54:36 +00001830 return nullptr;
1831
Sanjay Patel63478072016-08-18 15:44:44 +00001832 // If this is a test of the sign bit and the multiply is sign-preserving with
1833 // a constant operand, use the multiply LHS operand instead.
1834 ICmpInst::Predicate Pred = Cmp.getPredicate();
Sanjay Patelc9196c42016-08-22 21:24:29 +00001835 if (isSignTest(Pred, *C) && Mul->hasNoSignedWrap()) {
Sanjay Patel63478072016-08-18 15:44:44 +00001836 if (MulC->isNegative())
1837 Pred = ICmpInst::getSwappedPredicate(Pred);
1838 return new ICmpInst(Pred, Mul->getOperand(0),
1839 Constant::getNullValue(Mul->getType()));
1840 }
Sanjay Patela3f4f082016-08-16 17:54:36 +00001841
1842 return nullptr;
1843}
1844
Sanjay Patel98cd99d2016-08-18 21:28:30 +00001845/// Fold icmp (shl 1, Y), C.
1846static Instruction *foldICmpShlOne(ICmpInst &Cmp, Instruction *Shl,
1847 const APInt *C) {
1848 Value *Y;
1849 if (!match(Shl, m_Shl(m_One(), m_Value(Y))))
1850 return nullptr;
1851
1852 Type *ShiftType = Shl->getType();
1853 uint32_t TypeBits = C->getBitWidth();
1854 bool CIsPowerOf2 = C->isPowerOf2();
1855 ICmpInst::Predicate Pred = Cmp.getPredicate();
1856 if (Cmp.isUnsigned()) {
1857 // (1 << Y) pred C -> Y pred Log2(C)
1858 if (!CIsPowerOf2) {
1859 // (1 << Y) < 30 -> Y <= 4
1860 // (1 << Y) <= 30 -> Y <= 4
1861 // (1 << Y) >= 30 -> Y > 4
1862 // (1 << Y) > 30 -> Y > 4
1863 if (Pred == ICmpInst::ICMP_ULT)
1864 Pred = ICmpInst::ICMP_ULE;
1865 else if (Pred == ICmpInst::ICMP_UGE)
1866 Pred = ICmpInst::ICMP_UGT;
1867 }
1868
1869 // (1 << Y) >= 2147483648 -> Y >= 31 -> Y == 31
1870 // (1 << Y) < 2147483648 -> Y < 31 -> Y != 31
1871 unsigned CLog2 = C->logBase2();
1872 if (CLog2 == TypeBits - 1) {
1873 if (Pred == ICmpInst::ICMP_UGE)
1874 Pred = ICmpInst::ICMP_EQ;
1875 else if (Pred == ICmpInst::ICMP_ULT)
1876 Pred = ICmpInst::ICMP_NE;
1877 }
1878 return new ICmpInst(Pred, Y, ConstantInt::get(ShiftType, CLog2));
1879 } else if (Cmp.isSigned()) {
1880 Constant *BitWidthMinusOne = ConstantInt::get(ShiftType, TypeBits - 1);
1881 if (C->isAllOnesValue()) {
1882 // (1 << Y) <= -1 -> Y == 31
1883 if (Pred == ICmpInst::ICMP_SLE)
1884 return new ICmpInst(ICmpInst::ICMP_EQ, Y, BitWidthMinusOne);
1885
1886 // (1 << Y) > -1 -> Y != 31
1887 if (Pred == ICmpInst::ICMP_SGT)
1888 return new ICmpInst(ICmpInst::ICMP_NE, Y, BitWidthMinusOne);
1889 } else if (!(*C)) {
1890 // (1 << Y) < 0 -> Y == 31
1891 // (1 << Y) <= 0 -> Y == 31
1892 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
1893 return new ICmpInst(ICmpInst::ICMP_EQ, Y, BitWidthMinusOne);
1894
1895 // (1 << Y) >= 0 -> Y != 31
1896 // (1 << Y) > 0 -> Y != 31
1897 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
1898 return new ICmpInst(ICmpInst::ICMP_NE, Y, BitWidthMinusOne);
1899 }
1900 } else if (Cmp.isEquality() && CIsPowerOf2) {
1901 return new ICmpInst(Pred, Y, ConstantInt::get(ShiftType, C->logBase2()));
1902 }
1903
1904 return nullptr;
1905}
1906
Sanjay Patel38b75062016-08-19 17:20:37 +00001907/// Fold icmp (shl X, Y), C.
Sanjay Patelc9196c42016-08-22 21:24:29 +00001908Instruction *InstCombiner::foldICmpShlConstant(ICmpInst &Cmp,
1909 BinaryOperator *Shl,
Sanjay Patel38b75062016-08-19 17:20:37 +00001910 const APInt *C) {
Sanjay Patel8da42cc2016-09-15 22:26:31 +00001911 const APInt *ShiftVal;
1912 if (Cmp.isEquality() && match(Shl->getOperand(0), m_APInt(ShiftVal)))
1913 return foldICmpShlConstConst(Cmp, Shl->getOperand(1), *C, *ShiftVal);
1914
Sanjay Patelfa7de602016-08-19 22:33:26 +00001915 const APInt *ShiftAmt;
1916 if (!match(Shl->getOperand(1), m_APInt(ShiftAmt)))
Sanjay Patel38b75062016-08-19 17:20:37 +00001917 return foldICmpShlOne(Cmp, Shl, C);
Sanjay Patela867afe2016-08-19 16:12:16 +00001918
Sanjay Patel38b75062016-08-19 17:20:37 +00001919 // Check that the shift amount is in range. If not, don't perform undefined
Sanjay Patel940c0612017-01-09 16:27:56 +00001920 // shifts. When the shift is visited, it will be simplified.
Sanjay Patel38b75062016-08-19 17:20:37 +00001921 unsigned TypeBits = C->getBitWidth();
Sanjay Patelfa7de602016-08-19 22:33:26 +00001922 if (ShiftAmt->uge(TypeBits))
Sanjay Patela3f4f082016-08-16 17:54:36 +00001923 return nullptr;
1924
Sanjay Patele38e79c2016-08-19 17:34:05 +00001925 ICmpInst::Predicate Pred = Cmp.getPredicate();
1926 Value *X = Shl->getOperand(0);
Sanjay Patel14715b32017-01-17 21:25:16 +00001927 Type *ShType = Shl->getType();
1928
Sanjay Patel291c3d82017-01-19 16:12:10 +00001929 // NSW guarantees that we are only shifting out sign bits from the high bits,
1930 // so we can ASHR the compare constant without needing a mask and eliminate
1931 // the shift.
1932 if (Shl->hasNoSignedWrap()) {
1933 if (Pred == ICmpInst::ICMP_SGT) {
1934 // icmp Pred (shl nsw X, ShiftAmt), C --> icmp Pred X, (C >>s ShiftAmt)
1935 APInt ShiftedC = C->ashr(*ShiftAmt);
1936 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
1937 }
1938 if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) {
1939 // This is the same code as the SGT case, but assert the pre-condition
1940 // that is needed for this to work with equality predicates.
1941 assert(C->ashr(*ShiftAmt).shl(*ShiftAmt) == *C &&
1942 "Compare known true or false was not folded");
1943 APInt ShiftedC = C->ashr(*ShiftAmt);
1944 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
1945 }
1946 if (Pred == ICmpInst::ICMP_SLT) {
1947 // SLE is the same as above, but SLE is canonicalized to SLT, so convert:
1948 // (X << S) <=s C is equiv to X <=s (C >> S) for all C
1949 // (X << S) <s (C + 1) is equiv to X <s (C >> S) + 1 if C <s SMAX
1950 // (X << S) <s C is equiv to X <s ((C - 1) >> S) + 1 if C >s SMIN
1951 assert(!C->isMinSignedValue() && "Unexpected icmp slt");
1952 APInt ShiftedC = (*C - 1).ashr(*ShiftAmt) + 1;
1953 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
1954 }
1955 // If this is a signed comparison to 0 and the shift is sign preserving,
1956 // use the shift LHS operand instead; isSignTest may change 'Pred', so only
1957 // do that if we're sure to not continue on in this function.
1958 if (isSignTest(Pred, *C))
1959 return new ICmpInst(Pred, X, Constant::getNullValue(ShType));
1960 }
Sanjay Patel14715b32017-01-17 21:25:16 +00001961
Sanjay Patel291c3d82017-01-19 16:12:10 +00001962 // NUW guarantees that we are only shifting out zero bits from the high bits,
1963 // so we can LSHR the compare constant without needing a mask and eliminate
1964 // the shift.
Sanjay Patel14715b32017-01-17 21:25:16 +00001965 if (Shl->hasNoUnsignedWrap()) {
Sanjay Patelae23d652017-01-18 21:16:12 +00001966 if (Pred == ICmpInst::ICMP_UGT) {
Sanjay Patel14715b32017-01-17 21:25:16 +00001967 // icmp Pred (shl nuw X, ShiftAmt), C --> icmp Pred X, (C >>u ShiftAmt)
1968 APInt ShiftedC = C->lshr(*ShiftAmt);
1969 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
1970 }
Sanjay Patelae23d652017-01-18 21:16:12 +00001971 if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) {
1972 // This is the same code as the UGT case, but assert the pre-condition
1973 // that is needed for this to work with equality predicates.
1974 assert(C->lshr(*ShiftAmt).shl(*ShiftAmt) == *C &&
1975 "Compare known true or false was not folded");
1976 APInt ShiftedC = C->lshr(*ShiftAmt);
1977 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
1978 }
Sanjay Patel14715b32017-01-17 21:25:16 +00001979 if (Pred == ICmpInst::ICMP_ULT) {
1980 // ULE is the same as above, but ULE is canonicalized to ULT, so convert:
1981 // (X << S) <=u C is equiv to X <=u (C >> S) for all C
1982 // (X << S) <u (C + 1) is equiv to X <u (C >> S) + 1 if C <u ~0u
1983 // (X << S) <u C is equiv to X <u ((C - 1) >> S) + 1 if C >u 0
1984 assert(C->ugt(0) && "ult 0 should have been eliminated");
1985 APInt ShiftedC = (*C - 1).lshr(*ShiftAmt) + 1;
1986 return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC));
1987 }
1988 }
1989
Sanjay Patel291c3d82017-01-19 16:12:10 +00001990 if (Cmp.isEquality() && Shl->hasOneUse()) {
1991 // Strength-reduce the shift into an 'and'.
1992 Constant *Mask = ConstantInt::get(
1993 ShType,
1994 APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt->getZExtValue()));
1995 Value *And = Builder->CreateAnd(X, Mask, Shl->getName() + ".mask");
Sanjay Patel14715b32017-01-17 21:25:16 +00001996 Constant *LShrC = ConstantInt::get(ShType, C->lshr(*ShiftAmt));
Sanjay Patel291c3d82017-01-19 16:12:10 +00001997 return new ICmpInst(Pred, And, LShrC);
Sanjay Patela3f4f082016-08-16 17:54:36 +00001998 }
1999
Sanjay Patela3f4f082016-08-16 17:54:36 +00002000 // Otherwise, if this is a comparison of the sign bit, simplify to and/test.
2001 bool TrueIfSigned = false;
Sanjay Patel79263662016-08-21 15:07:45 +00002002 if (Shl->hasOneUse() && isSignBitCheck(Pred, *C, TrueIfSigned)) {
Sanjay Patel7ffcde72016-08-21 16:35:34 +00002003 // (X << 31) <s 0 --> (X & 1) != 0
Sanjay Patela3f4f082016-08-16 17:54:36 +00002004 Constant *Mask = ConstantInt::get(
Sanjay Patel14715b32017-01-17 21:25:16 +00002005 ShType,
Sanjay Patelfa7de602016-08-19 22:33:26 +00002006 APInt::getOneBitSet(TypeBits, TypeBits - ShiftAmt->getZExtValue() - 1));
Sanjay Patele38e79c2016-08-19 17:34:05 +00002007 Value *And = Builder->CreateAnd(X, Mask, Shl->getName() + ".mask");
Sanjay Patela3f4f082016-08-16 17:54:36 +00002008 return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
Sanjay Patel14715b32017-01-17 21:25:16 +00002009 And, Constant::getNullValue(ShType));
Sanjay Patelc0339c72016-11-01 19:19:29 +00002010 }
2011
Sanjay Patel643d21a2016-08-21 17:10:07 +00002012 // Transform (icmp pred iM (shl iM %v, N), C)
2013 // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (C>>N))
2014 // Transform the shl to a trunc if (trunc (C>>N)) has no loss and M-N.
Sanjay Patel940c0612017-01-09 16:27:56 +00002015 // This enables us to get rid of the shift in favor of a trunc that may be
Sanjay Patela3f4f082016-08-16 17:54:36 +00002016 // free on the target. It has the additional benefit of comparing to a
Sanjay Patel940c0612017-01-09 16:27:56 +00002017 // smaller constant that may be more target-friendly.
Sanjay Patelfa7de602016-08-19 22:33:26 +00002018 unsigned Amt = ShiftAmt->getLimitedValue(TypeBits - 1);
Sanjay Patelf3dda132016-10-25 20:11:47 +00002019 if (Shl->hasOneUse() && Amt != 0 && C->countTrailingZeros() >= Amt &&
2020 DL.isLegalInteger(TypeBits - Amt)) {
Sanjay Patel643d21a2016-08-21 17:10:07 +00002021 Type *TruncTy = IntegerType::get(Cmp.getContext(), TypeBits - Amt);
Sanjay Patel14715b32017-01-17 21:25:16 +00002022 if (ShType->isVectorTy())
2023 TruncTy = VectorType::get(TruncTy, ShType->getVectorNumElements());
Sanjay Patel643d21a2016-08-21 17:10:07 +00002024 Constant *NewC =
2025 ConstantInt::get(TruncTy, C->ashr(*ShiftAmt).trunc(TypeBits - Amt));
2026 return new ICmpInst(Pred, Builder->CreateTrunc(X, TruncTy), NewC);
Sanjay Patela3f4f082016-08-16 17:54:36 +00002027 }
2028
2029 return nullptr;
2030}
2031
Sanjay Patela3920492016-08-22 20:45:06 +00002032/// Fold icmp ({al}shr X, Y), C.
Sanjay Patelc9196c42016-08-22 21:24:29 +00002033Instruction *InstCombiner::foldICmpShrConstant(ICmpInst &Cmp,
2034 BinaryOperator *Shr,
2035 const APInt *C) {
Sanjay Patela3920492016-08-22 20:45:06 +00002036 // An exact shr only shifts out zero bits, so:
2037 // icmp eq/ne (shr X, Y), 0 --> icmp eq/ne X, 0
Sanjay Pateld64e9882016-08-23 22:05:55 +00002038 Value *X = Shr->getOperand(0);
Sanjay Patelc9196c42016-08-22 21:24:29 +00002039 CmpInst::Predicate Pred = Cmp.getPredicate();
Craig Topper73ba1c82017-06-07 07:40:37 +00002040 if (Cmp.isEquality() && Shr->isExact() && Shr->hasOneUse() &&
2041 C->isNullValue())
Sanjay Pateld64e9882016-08-23 22:05:55 +00002042 return new ICmpInst(Pred, X, Cmp.getOperand(1));
Sanjay Patela3920492016-08-22 20:45:06 +00002043
Sanjay Patel8da42cc2016-09-15 22:26:31 +00002044 const APInt *ShiftVal;
2045 if (Cmp.isEquality() && match(Shr->getOperand(0), m_APInt(ShiftVal)))
2046 return foldICmpShrConstConst(Cmp, Shr->getOperand(1), *C, *ShiftVal);
2047
Sanjay Pateld398d4a2016-08-24 22:22:06 +00002048 const APInt *ShiftAmt;
2049 if (!match(Shr->getOperand(1), m_APInt(ShiftAmt)))
Sanjay Patela3f4f082016-08-16 17:54:36 +00002050 return nullptr;
2051
Sanjay Pateld398d4a2016-08-24 22:22:06 +00002052 // Check that the shift amount is in range. If not, don't perform undefined
2053 // shifts. When the shift is visited it will be simplified.
2054 unsigned TypeBits = C->getBitWidth();
2055 unsigned ShAmtVal = ShiftAmt->getLimitedValue(TypeBits);
Sanjay Pateldcac0df2016-08-23 21:25:13 +00002056 if (ShAmtVal >= TypeBits || ShAmtVal == 0)
2057 return nullptr;
2058
Sanjay Pateld64e9882016-08-23 22:05:55 +00002059 bool IsAShr = Shr->getOpcode() == Instruction::AShr;
Sanjay Pateldcac0df2016-08-23 21:25:13 +00002060 if (!Cmp.isEquality()) {
2061 // If we have an unsigned comparison and an ashr, we can't simplify this.
2062 // Similarly for signed comparisons with lshr.
Sanjay Pateld64e9882016-08-23 22:05:55 +00002063 if (Cmp.isSigned() != IsAShr)
Sanjay Pateldcac0df2016-08-23 21:25:13 +00002064 return nullptr;
2065
2066 // Otherwise, all lshr and most exact ashr's are equivalent to a udiv/sdiv
2067 // by a power of 2. Since we already have logic to simplify these,
2068 // transform to div and then simplify the resultant comparison.
Sanjay Pateld64e9882016-08-23 22:05:55 +00002069 if (IsAShr && (!Shr->isExact() || ShAmtVal == TypeBits - 1))
Sanjay Pateldcac0df2016-08-23 21:25:13 +00002070 return nullptr;
2071
2072 // Revisit the shift (to delete it).
2073 Worklist.Add(Shr);
2074
2075 Constant *DivCst = ConstantInt::get(
2076 Shr->getType(), APInt::getOneBitSet(TypeBits, ShAmtVal));
2077
Sanjay Pateld64e9882016-08-23 22:05:55 +00002078 Value *Tmp = IsAShr ? Builder->CreateSDiv(X, DivCst, "", Shr->isExact())
2079 : Builder->CreateUDiv(X, DivCst, "", Shr->isExact());
Sanjay Pateldcac0df2016-08-23 21:25:13 +00002080
2081 Cmp.setOperand(0, Tmp);
2082
2083 // If the builder folded the binop, just return it.
2084 BinaryOperator *TheDiv = dyn_cast<BinaryOperator>(Tmp);
2085 if (!TheDiv)
2086 return &Cmp;
2087
2088 // Otherwise, fold this div/compare.
2089 assert(TheDiv->getOpcode() == Instruction::SDiv ||
2090 TheDiv->getOpcode() == Instruction::UDiv);
2091
Sanjay Patelf7ba0892016-08-26 15:53:01 +00002092 Instruction *Res = foldICmpDivConstant(Cmp, TheDiv, C);
Sanjay Pateldcac0df2016-08-23 21:25:13 +00002093 assert(Res && "This div/cst should have folded!");
Sanjay Patela3920492016-08-22 20:45:06 +00002094 return Res;
Sanjay Pateldcac0df2016-08-23 21:25:13 +00002095 }
2096
Sanjay Pateld398d4a2016-08-24 22:22:06 +00002097 // Handle equality comparisons of shift-by-constant.
2098
Sanjay Patel8e297742016-08-24 13:55:55 +00002099 // If the comparison constant changes with the shift, the comparison cannot
2100 // succeed (bits of the comparison constant cannot match the shifted value).
2101 // This should be known by InstSimplify and already be folded to true/false.
2102 assert(((IsAShr && C->shl(ShAmtVal).ashr(ShAmtVal) == *C) ||
2103 (!IsAShr && C->shl(ShAmtVal).lshr(ShAmtVal) == *C)) &&
2104 "Expected icmp+shr simplify did not occur.");
2105
Sanjay Pateldcac0df2016-08-23 21:25:13 +00002106 // Check if the bits shifted out are known to be zero. If so, we can compare
2107 // against the unshifted value:
2108 // (X & 4) >> 1 == 2 --> (X & 4) == 4.
Sanjay Pateld398d4a2016-08-24 22:22:06 +00002109 Constant *ShiftedCmpRHS = ConstantInt::get(Shr->getType(), *C << ShAmtVal);
Sanjay Pateldcac0df2016-08-23 21:25:13 +00002110 if (Shr->hasOneUse()) {
Sanjay Pateld398d4a2016-08-24 22:22:06 +00002111 if (Shr->isExact())
2112 return new ICmpInst(Pred, X, ShiftedCmpRHS);
Sanjay Pateldcac0df2016-08-23 21:25:13 +00002113
Sanjay Pateld398d4a2016-08-24 22:22:06 +00002114 // Otherwise strength reduce the shift into an 'and'.
2115 APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
2116 Constant *Mask = ConstantInt::get(Shr->getType(), Val);
Sanjay Pateld64e9882016-08-23 22:05:55 +00002117 Value *And = Builder->CreateAnd(X, Mask, Shr->getName() + ".mask");
Sanjay Pateldcac0df2016-08-23 21:25:13 +00002118 return new ICmpInst(Pred, And, ShiftedCmpRHS);
2119 }
Sanjay Patela3f4f082016-08-16 17:54:36 +00002120
2121 return nullptr;
2122}
2123
Sanjay Patel12a41052016-08-18 17:37:26 +00002124/// Fold icmp (udiv X, Y), C.
2125Instruction *InstCombiner::foldICmpUDivConstant(ICmpInst &Cmp,
Sanjay Patelc9196c42016-08-22 21:24:29 +00002126 BinaryOperator *UDiv,
Sanjay Patel12a41052016-08-18 17:37:26 +00002127 const APInt *C) {
Sanjay Patelfa5ca2b2016-08-18 17:55:59 +00002128 const APInt *C2;
2129 if (!match(UDiv->getOperand(0), m_APInt(C2)))
2130 return nullptr;
2131
Craig Topper29c282e2017-06-07 07:40:29 +00002132 assert(*C2 != 0 && "udiv 0, X should have been simplified already.");
Sanjay Patelfa5ca2b2016-08-18 17:55:59 +00002133
2134 // (icmp ugt (udiv C2, Y), C) -> (icmp ule Y, C2/(C+1))
2135 Value *Y = UDiv->getOperand(1);
2136 if (Cmp.getPredicate() == ICmpInst::ICMP_UGT) {
2137 assert(!C->isMaxValue() &&
2138 "icmp ugt X, UINT_MAX should have been simplified already.");
2139 return new ICmpInst(ICmpInst::ICMP_ULE, Y,
2140 ConstantInt::get(Y->getType(), C2->udiv(*C + 1)));
2141 }
2142
2143 // (icmp ult (udiv C2, Y), C) -> (icmp ugt Y, C2/C)
2144 if (Cmp.getPredicate() == ICmpInst::ICMP_ULT) {
Craig Topper29c282e2017-06-07 07:40:29 +00002145 assert(*C != 0 && "icmp ult X, 0 should have been simplified already.");
Sanjay Patelfa5ca2b2016-08-18 17:55:59 +00002146 return new ICmpInst(ICmpInst::ICMP_UGT, Y,
2147 ConstantInt::get(Y->getType(), C2->udiv(*C)));
Sanjay Patela3f4f082016-08-16 17:54:36 +00002148 }
2149
2150 return nullptr;
2151}
2152
Sanjay Patelf7ba0892016-08-26 15:53:01 +00002153/// Fold icmp ({su}div X, Y), C.
2154Instruction *InstCombiner::foldICmpDivConstant(ICmpInst &Cmp,
2155 BinaryOperator *Div,
2156 const APInt *C) {
Sanjay Patela7cb4772016-08-30 17:10:49 +00002157 // Fold: icmp pred ([us]div X, C2), C -> range test
Sanjay Patela3f4f082016-08-16 17:54:36 +00002158 // Fold this div into the comparison, producing a range check.
2159 // Determine, based on the divide type, what the range is being
2160 // checked. If there is an overflow on the low or high side, remember
2161 // it, otherwise compute the range [low, hi) bounding the new value.
2162 // See: InsertRangeTest above for the kinds of replacements possible.
Sanjay Patela7cb4772016-08-30 17:10:49 +00002163 const APInt *C2;
2164 if (!match(Div->getOperand(1), m_APInt(C2)))
Sanjay Patel16554142016-08-24 23:03:36 +00002165 return nullptr;
2166
Sanjay Patel16554142016-08-24 23:03:36 +00002167 // FIXME: If the operand types don't match the type of the divide
2168 // then don't attempt this transform. The code below doesn't have the
2169 // logic to deal with a signed divide and an unsigned compare (and
Sanjay Patela7cb4772016-08-30 17:10:49 +00002170 // vice versa). This is because (x /s C2) <s C produces different
2171 // results than (x /s C2) <u C or (x /u C2) <s C or even
2172 // (x /u C2) <u C. Simply casting the operands and result won't
Sanjay Patel16554142016-08-24 23:03:36 +00002173 // work. :( The if statement below tests that condition and bails
2174 // if it finds it.
Sanjay Patelf7ba0892016-08-26 15:53:01 +00002175 bool DivIsSigned = Div->getOpcode() == Instruction::SDiv;
2176 if (!Cmp.isEquality() && DivIsSigned != Cmp.isSigned())
Sanjay Patel16554142016-08-24 23:03:36 +00002177 return nullptr;
Sanjay Patela7cb4772016-08-30 17:10:49 +00002178
Sanjay Pateleea2ef72016-09-05 23:38:22 +00002179 // The ProdOV computation fails on divide by 0 and divide by -1. Cases with
2180 // INT_MIN will also fail if the divisor is 1. Although folds of all these
2181 // division-by-constant cases should be present, we can not assert that they
2182 // have happened before we reach this icmp instruction.
Craig Topper73ba1c82017-06-07 07:40:37 +00002183 if (C2->isNullValue() || C2->isOneValue() ||
2184 (DivIsSigned && C2->isAllOnesValue()))
Sanjay Pateleea2ef72016-09-05 23:38:22 +00002185 return nullptr;
Sanjay Patelb3714572016-08-30 17:31:34 +00002186
Sanjay Patel541aef42016-08-31 21:57:21 +00002187 // TODO: We could do all of the computations below using APInt.
2188 Constant *CmpRHS = cast<Constant>(Cmp.getOperand(1));
2189 Constant *DivRHS = cast<Constant>(Div->getOperand(1));
Sanjay Patelb3714572016-08-30 17:31:34 +00002190
Sanjay Patel541aef42016-08-31 21:57:21 +00002191 // Compute Prod = CmpRHS * DivRHS. We are essentially solving an equation of
2192 // form X / C2 = C. We solve for X by multiplying C2 (DivRHS) and C (CmpRHS).
2193 // By solving for X, we can turn this into a range check instead of computing
2194 // a divide.
2195 Constant *Prod = ConstantExpr::getMul(CmpRHS, DivRHS);
Sanjay Patel16554142016-08-24 23:03:36 +00002196
Sanjay Patel541aef42016-08-31 21:57:21 +00002197 // Determine if the product overflows by seeing if the product is not equal to
2198 // the divide. Make sure we do the same kind of divide as in the LHS
2199 // instruction that we're folding.
2200 bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS)
2201 : ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS;
Sanjay Patel16554142016-08-24 23:03:36 +00002202
Sanjay Patelf7ba0892016-08-26 15:53:01 +00002203 ICmpInst::Predicate Pred = Cmp.getPredicate();
Sanjay Patel16554142016-08-24 23:03:36 +00002204
2205 // If the division is known to be exact, then there is no remainder from the
2206 // divide, so the covered range size is unit, otherwise it is the divisor.
Sanjay Patel541aef42016-08-31 21:57:21 +00002207 Constant *RangeSize =
2208 Div->isExact() ? ConstantInt::get(Div->getType(), 1) : DivRHS;
Sanjay Patel16554142016-08-24 23:03:36 +00002209
2210 // Figure out the interval that is being checked. For example, a comparison
2211 // like "X /u 5 == 0" is really checking that X is in the interval [0, 5).
2212 // Compute this interval based on the constants involved and the signedness of
2213 // the compare/divide. This computes a half-open interval, keeping track of
2214 // whether either value in the interval overflows. After analysis each
2215 // overflow variable is set to 0 if it's corresponding bound variable is valid
2216 // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
2217 int LoOverflow = 0, HiOverflow = 0;
2218 Constant *LoBound = nullptr, *HiBound = nullptr;
2219
2220 if (!DivIsSigned) { // udiv
2221 // e.g. X/5 op 3 --> [15, 20)
2222 LoBound = Prod;
2223 HiOverflow = LoOverflow = ProdOV;
2224 if (!HiOverflow) {
2225 // If this is not an exact divide, then many values in the range collapse
2226 // to the same result value.
Sanjay Pateld93c4c02016-09-15 18:22:25 +00002227 HiOverflow = addWithOverflow(HiBound, LoBound, RangeSize, false);
Sanjay Patel16554142016-08-24 23:03:36 +00002228 }
Sanjay Patel541aef42016-08-31 21:57:21 +00002229 } else if (C2->isStrictlyPositive()) { // Divisor is > 0.
Craig Topper73ba1c82017-06-07 07:40:37 +00002230 if (C->isNullValue()) { // (X / pos) op 0
Sanjay Patel16554142016-08-24 23:03:36 +00002231 // Can't overflow. e.g. X/2 op 0 --> [-1, 2)
2232 LoBound = ConstantExpr::getNeg(SubOne(RangeSize));
2233 HiBound = RangeSize;
Sanjay Patelf7ba0892016-08-26 15:53:01 +00002234 } else if (C->isStrictlyPositive()) { // (X / pos) op pos
Sanjay Patel16554142016-08-24 23:03:36 +00002235 LoBound = Prod; // e.g. X/5 op 3 --> [15, 20)
2236 HiOverflow = LoOverflow = ProdOV;
2237 if (!HiOverflow)
Sanjay Pateld93c4c02016-09-15 18:22:25 +00002238 HiOverflow = addWithOverflow(HiBound, Prod, RangeSize, true);
Sanjay Patel16554142016-08-24 23:03:36 +00002239 } else { // (X / pos) op neg
2240 // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14)
2241 HiBound = AddOne(Prod);
2242 LoOverflow = HiOverflow = ProdOV ? -1 : 0;
2243 if (!LoOverflow) {
Sanjay Patel541aef42016-08-31 21:57:21 +00002244 Constant *DivNeg = ConstantExpr::getNeg(RangeSize);
Sanjay Pateld93c4c02016-09-15 18:22:25 +00002245 LoOverflow = addWithOverflow(LoBound, HiBound, DivNeg, true) ? -1 : 0;
Sanjay Patel16554142016-08-24 23:03:36 +00002246 }
2247 }
Sanjay Patel541aef42016-08-31 21:57:21 +00002248 } else if (C2->isNegative()) { // Divisor is < 0.
Sanjay Patelf7ba0892016-08-26 15:53:01 +00002249 if (Div->isExact())
Sanjay Patel541aef42016-08-31 21:57:21 +00002250 RangeSize = ConstantExpr::getNeg(RangeSize);
Craig Topper73ba1c82017-06-07 07:40:37 +00002251 if (C->isNullValue()) { // (X / neg) op 0
Sanjay Patel16554142016-08-24 23:03:36 +00002252 // e.g. X/-5 op 0 --> [-4, 5)
2253 LoBound = AddOne(RangeSize);
Sanjay Patel541aef42016-08-31 21:57:21 +00002254 HiBound = ConstantExpr::getNeg(RangeSize);
Sanjay Patel16554142016-08-24 23:03:36 +00002255 if (HiBound == DivRHS) { // -INTMIN = INTMIN
2256 HiOverflow = 1; // [INTMIN+1, overflow)
2257 HiBound = nullptr; // e.g. X/INTMIN = 0 --> X > INTMIN
2258 }
Sanjay Patelf7ba0892016-08-26 15:53:01 +00002259 } else if (C->isStrictlyPositive()) { // (X / neg) op pos
Sanjay Patel16554142016-08-24 23:03:36 +00002260 // e.g. X/-5 op 3 --> [-19, -14)
2261 HiBound = AddOne(Prod);
2262 HiOverflow = LoOverflow = ProdOV ? -1 : 0;
2263 if (!LoOverflow)
Sanjay Pateld93c4c02016-09-15 18:22:25 +00002264 LoOverflow = addWithOverflow(LoBound, HiBound, RangeSize, true) ? -1:0;
Sanjay Patel16554142016-08-24 23:03:36 +00002265 } else { // (X / neg) op neg
2266 LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20)
2267 LoOverflow = HiOverflow = ProdOV;
2268 if (!HiOverflow)
Sanjay Pateld93c4c02016-09-15 18:22:25 +00002269 HiOverflow = subWithOverflow(HiBound, Prod, RangeSize, true);
Sanjay Patel16554142016-08-24 23:03:36 +00002270 }
2271
2272 // Dividing by a negative swaps the condition. LT <-> GT
2273 Pred = ICmpInst::getSwappedPredicate(Pred);
2274 }
2275
Sanjay Patelf7ba0892016-08-26 15:53:01 +00002276 Value *X = Div->getOperand(0);
Sanjay Patel16554142016-08-24 23:03:36 +00002277 switch (Pred) {
2278 default: llvm_unreachable("Unhandled icmp opcode!");
2279 case ICmpInst::ICMP_EQ:
2280 if (LoOverflow && HiOverflow)
Sanjay Patelf7ba0892016-08-26 15:53:01 +00002281 return replaceInstUsesWith(Cmp, Builder->getFalse());
Sanjay Patel16554142016-08-24 23:03:36 +00002282 if (HiOverflow)
2283 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
2284 ICmpInst::ICMP_UGE, X, LoBound);
2285 if (LoOverflow)
2286 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
2287 ICmpInst::ICMP_ULT, X, HiBound);
Sanjay Patel85d79742016-08-31 19:49:56 +00002288 return replaceInstUsesWith(
Sanjay Patel541aef42016-08-31 21:57:21 +00002289 Cmp, insertRangeTest(X, LoBound->getUniqueInteger(),
2290 HiBound->getUniqueInteger(), DivIsSigned, true));
Sanjay Patel16554142016-08-24 23:03:36 +00002291 case ICmpInst::ICMP_NE:
2292 if (LoOverflow && HiOverflow)
Sanjay Patelf7ba0892016-08-26 15:53:01 +00002293 return replaceInstUsesWith(Cmp, Builder->getTrue());
Sanjay Patel16554142016-08-24 23:03:36 +00002294 if (HiOverflow)
2295 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
2296 ICmpInst::ICMP_ULT, X, LoBound);
2297 if (LoOverflow)
2298 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
2299 ICmpInst::ICMP_UGE, X, HiBound);
Sanjay Patel541aef42016-08-31 21:57:21 +00002300 return replaceInstUsesWith(Cmp,
2301 insertRangeTest(X, LoBound->getUniqueInteger(),
2302 HiBound->getUniqueInteger(),
2303 DivIsSigned, false));
Sanjay Patel16554142016-08-24 23:03:36 +00002304 case ICmpInst::ICMP_ULT:
2305 case ICmpInst::ICMP_SLT:
2306 if (LoOverflow == +1) // Low bound is greater than input range.
Sanjay Patelf7ba0892016-08-26 15:53:01 +00002307 return replaceInstUsesWith(Cmp, Builder->getTrue());
Sanjay Patel16554142016-08-24 23:03:36 +00002308 if (LoOverflow == -1) // Low bound is less than input range.
Sanjay Patelf7ba0892016-08-26 15:53:01 +00002309 return replaceInstUsesWith(Cmp, Builder->getFalse());
Sanjay Patel16554142016-08-24 23:03:36 +00002310 return new ICmpInst(Pred, X, LoBound);
2311 case ICmpInst::ICMP_UGT:
2312 case ICmpInst::ICMP_SGT:
2313 if (HiOverflow == +1) // High bound greater than input range.
Sanjay Patelf7ba0892016-08-26 15:53:01 +00002314 return replaceInstUsesWith(Cmp, Builder->getFalse());
Sanjay Patel16554142016-08-24 23:03:36 +00002315 if (HiOverflow == -1) // High bound less than input range.
Sanjay Patelf7ba0892016-08-26 15:53:01 +00002316 return replaceInstUsesWith(Cmp, Builder->getTrue());
Sanjay Patel16554142016-08-24 23:03:36 +00002317 if (Pred == ICmpInst::ICMP_UGT)
2318 return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound);
2319 return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound);
2320 }
Sanjay Patela3f4f082016-08-16 17:54:36 +00002321
2322 return nullptr;
2323}
2324
Sanjay Patelb9aa67b2016-08-16 21:26:10 +00002325/// Fold icmp (sub X, Y), C.
Sanjay Patelc9196c42016-08-22 21:24:29 +00002326Instruction *InstCombiner::foldICmpSubConstant(ICmpInst &Cmp,
2327 BinaryOperator *Sub,
Sanjay Patelb9aa67b2016-08-16 21:26:10 +00002328 const APInt *C) {
Sanjay Patel886a5422016-09-15 18:05:17 +00002329 Value *X = Sub->getOperand(0), *Y = Sub->getOperand(1);
2330 ICmpInst::Predicate Pred = Cmp.getPredicate();
2331
2332 // The following transforms are only worth it if the only user of the subtract
2333 // is the icmp.
2334 if (!Sub->hasOneUse())
Sanjay Patela3f4f082016-08-16 17:54:36 +00002335 return nullptr;
2336
Sanjay Patel886a5422016-09-15 18:05:17 +00002337 if (Sub->hasNoSignedWrap()) {
2338 // (icmp sgt (sub nsw X, Y), -1) -> (icmp sge X, Y)
2339 if (Pred == ICmpInst::ICMP_SGT && C->isAllOnesValue())
2340 return new ICmpInst(ICmpInst::ICMP_SGE, X, Y);
Sanjay Patela3f4f082016-08-16 17:54:36 +00002341
Sanjay Patel886a5422016-09-15 18:05:17 +00002342 // (icmp sgt (sub nsw X, Y), 0) -> (icmp sgt X, Y)
Craig Topper73ba1c82017-06-07 07:40:37 +00002343 if (Pred == ICmpInst::ICMP_SGT && C->isNullValue())
Sanjay Patel886a5422016-09-15 18:05:17 +00002344 return new ICmpInst(ICmpInst::ICMP_SGT, X, Y);
2345
2346 // (icmp slt (sub nsw X, Y), 0) -> (icmp slt X, Y)
Craig Topper73ba1c82017-06-07 07:40:37 +00002347 if (Pred == ICmpInst::ICMP_SLT && C->isNullValue())
Sanjay Patel886a5422016-09-15 18:05:17 +00002348 return new ICmpInst(ICmpInst::ICMP_SLT, X, Y);
2349
2350 // (icmp slt (sub nsw X, Y), 1) -> (icmp sle X, Y)
Craig Topper73ba1c82017-06-07 07:40:37 +00002351 if (Pred == ICmpInst::ICMP_SLT && C->isOneValue())
Sanjay Patel886a5422016-09-15 18:05:17 +00002352 return new ICmpInst(ICmpInst::ICMP_SLE, X, Y);
2353 }
2354
2355 const APInt *C2;
2356 if (!match(X, m_APInt(C2)))
2357 return nullptr;
2358
2359 // C2 - Y <u C -> (Y | (C - 1)) == C2
2360 // iff (C2 & (C - 1)) == C - 1 and C is a power of 2
2361 if (Pred == ICmpInst::ICMP_ULT && C->isPowerOf2() &&
2362 (*C2 & (*C - 1)) == (*C - 1))
2363 return new ICmpInst(ICmpInst::ICMP_EQ, Builder->CreateOr(Y, *C - 1), X);
2364
2365 // C2 - Y >u C -> (Y | C) != C2
2366 // iff C2 & C == C and C + 1 is a power of 2
2367 if (Pred == ICmpInst::ICMP_UGT && (*C + 1).isPowerOf2() && (*C2 & *C) == *C)
2368 return new ICmpInst(ICmpInst::ICMP_NE, Builder->CreateOr(Y, *C), X);
Sanjay Patela3f4f082016-08-16 17:54:36 +00002369
2370 return nullptr;
2371}
2372
Sanjay Patel60ea1b42016-08-16 22:34:42 +00002373/// Fold icmp (add X, Y), C.
Sanjay Patelc9196c42016-08-22 21:24:29 +00002374Instruction *InstCombiner::foldICmpAddConstant(ICmpInst &Cmp,
2375 BinaryOperator *Add,
Sanjay Patel60ea1b42016-08-16 22:34:42 +00002376 const APInt *C) {
Sanjay Patel4f7eb2a2016-08-17 15:24:30 +00002377 Value *Y = Add->getOperand(1);
2378 const APInt *C2;
2379 if (Cmp.isEquality() || !match(Y, m_APInt(C2)))
Sanjay Patela3f4f082016-08-16 17:54:36 +00002380 return nullptr;
2381
Sanjay Patel4f7eb2a2016-08-17 15:24:30 +00002382 // Fold icmp pred (add X, C2), C.
Sanjay Patel60ea1b42016-08-16 22:34:42 +00002383 Value *X = Add->getOperand(0);
Sanjay Patel4f7eb2a2016-08-17 15:24:30 +00002384 Type *Ty = Add->getType();
Sanjay Patel6dd2eae2017-02-08 16:19:36 +00002385 CmpInst::Predicate Pred = Cmp.getPredicate();
Sanjay Patel45b7e692017-02-12 16:40:30 +00002386
2387 // If the add does not wrap, we can always adjust the compare by subtracting
2388 // the constants. Equality comparisons are handled elsewhere. SGE/SLE are
2389 // canonicalized to SGT/SLT.
2390 if (Add->hasNoSignedWrap() &&
2391 (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT)) {
2392 bool Overflow;
2393 APInt NewC = C->ssub_ov(*C2, Overflow);
2394 // If there is overflow, the result must be true or false.
2395 // TODO: Can we assert there is no overflow because InstSimplify always
2396 // handles those cases?
2397 if (!Overflow)
2398 // icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2)
2399 return new ICmpInst(Pred, X, ConstantInt::get(Ty, NewC));
2400 }
2401
Sanjay Patel6dd2eae2017-02-08 16:19:36 +00002402 auto CR = ConstantRange::makeExactICmpRegion(Pred, *C).subtract(*C2);
Sanjay Patel60ea1b42016-08-16 22:34:42 +00002403 const APInt &Upper = CR.getUpper();
2404 const APInt &Lower = CR.getLower();
2405 if (Cmp.isSigned()) {
Craig Topperbcfd2d12017-04-20 16:56:25 +00002406 if (Lower.isSignMask())
Sanjay Patel4f7eb2a2016-08-17 15:24:30 +00002407 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, Upper));
Craig Topperbcfd2d12017-04-20 16:56:25 +00002408 if (Upper.isSignMask())
Sanjay Patel4f7eb2a2016-08-17 15:24:30 +00002409 return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, Lower));
Sanjay Patel60ea1b42016-08-16 22:34:42 +00002410 } else {
2411 if (Lower.isMinValue())
Sanjay Patel4f7eb2a2016-08-17 15:24:30 +00002412 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, Upper));
Sanjay Patel60ea1b42016-08-16 22:34:42 +00002413 if (Upper.isMinValue())
Sanjay Patel4f7eb2a2016-08-17 15:24:30 +00002414 return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, Lower));
Sanjay Patel60ea1b42016-08-16 22:34:42 +00002415 }
Sanjay Patela3f4f082016-08-16 17:54:36 +00002416
Sanjay Patel4f7eb2a2016-08-17 15:24:30 +00002417 if (!Add->hasOneUse())
2418 return nullptr;
Sanjay Patela3f4f082016-08-16 17:54:36 +00002419
Sanjay Patel4f7eb2a2016-08-17 15:24:30 +00002420 // X+C <u C2 -> (X & -C2) == C
2421 // iff C & (C2-1) == 0
2422 // C2 is a power of 2
Sanjay Patel6dd2eae2017-02-08 16:19:36 +00002423 if (Pred == ICmpInst::ICMP_ULT && C->isPowerOf2() && (*C2 & (*C - 1)) == 0)
Sanjay Patel4f7eb2a2016-08-17 15:24:30 +00002424 return new ICmpInst(ICmpInst::ICMP_EQ, Builder->CreateAnd(X, -(*C)),
2425 ConstantExpr::getNeg(cast<Constant>(Y)));
2426
2427 // X+C >u C2 -> (X & ~C2) != C
2428 // iff C & C2 == 0
2429 // C2+1 is a power of 2
Sanjay Patel6dd2eae2017-02-08 16:19:36 +00002430 if (Pred == ICmpInst::ICMP_UGT && (*C + 1).isPowerOf2() && (*C2 & *C) == 0)
Sanjay Patel4f7eb2a2016-08-17 15:24:30 +00002431 return new ICmpInst(ICmpInst::ICMP_NE, Builder->CreateAnd(X, ~(*C)),
2432 ConstantExpr::getNeg(cast<Constant>(Y)));
2433
Sanjay Patela3f4f082016-08-16 17:54:36 +00002434 return nullptr;
2435}
2436
Sanjay Patelf58f68c2016-09-10 15:03:44 +00002437/// Try to fold integer comparisons with a constant operand: icmp Pred X, C
2438/// where X is some kind of instruction.
2439Instruction *InstCombiner::foldICmpInstWithConstant(ICmpInst &Cmp) {
Sanjay Patelc9196c42016-08-22 21:24:29 +00002440 const APInt *C;
2441 if (!match(Cmp.getOperand(1), m_APInt(C)))
Sanjay Patel1e5b2d12016-08-16 16:08:11 +00002442 return nullptr;
2443
Sanjay Patelc9196c42016-08-22 21:24:29 +00002444 BinaryOperator *BO;
2445 if (match(Cmp.getOperand(0), m_BinOp(BO))) {
2446 switch (BO->getOpcode()) {
2447 case Instruction::Xor:
2448 if (Instruction *I = foldICmpXorConstant(Cmp, BO, C))
2449 return I;
2450 break;
2451 case Instruction::And:
2452 if (Instruction *I = foldICmpAndConstant(Cmp, BO, C))
2453 return I;
2454 break;
2455 case Instruction::Or:
2456 if (Instruction *I = foldICmpOrConstant(Cmp, BO, C))
2457 return I;
2458 break;
2459 case Instruction::Mul:
2460 if (Instruction *I = foldICmpMulConstant(Cmp, BO, C))
2461 return I;
2462 break;
2463 case Instruction::Shl:
2464 if (Instruction *I = foldICmpShlConstant(Cmp, BO, C))
2465 return I;
2466 break;
2467 case Instruction::LShr:
2468 case Instruction::AShr:
2469 if (Instruction *I = foldICmpShrConstant(Cmp, BO, C))
2470 return I;
2471 break;
2472 case Instruction::UDiv:
2473 if (Instruction *I = foldICmpUDivConstant(Cmp, BO, C))
2474 return I;
2475 LLVM_FALLTHROUGH;
2476 case Instruction::SDiv:
2477 if (Instruction *I = foldICmpDivConstant(Cmp, BO, C))
2478 return I;
2479 break;
2480 case Instruction::Sub:
2481 if (Instruction *I = foldICmpSubConstant(Cmp, BO, C))
2482 return I;
2483 break;
2484 case Instruction::Add:
2485 if (Instruction *I = foldICmpAddConstant(Cmp, BO, C))
2486 return I;
2487 break;
2488 default:
2489 break;
2490 }
Sanjay Patelf58f68c2016-09-10 15:03:44 +00002491 // TODO: These folds could be refactored to be part of the above calls.
2492 if (Instruction *I = foldICmpBinOpEqualityWithConstant(Cmp, BO, C))
2493 return I;
Chris Lattner2188e402010-01-04 07:37:31 +00002494 }
Jim Grosbach129c52a2011-09-30 18:09:53 +00002495
Sanjay Patelc9196c42016-08-22 21:24:29 +00002496 Instruction *LHSI;
2497 if (match(Cmp.getOperand(0), m_Instruction(LHSI)) &&
2498 LHSI->getOpcode() == Instruction::Trunc)
2499 if (Instruction *I = foldICmpTruncConstant(Cmp, LHSI, C))
2500 return I;
2501
Sanjay Patelf58f68c2016-09-10 15:03:44 +00002502 if (Instruction *I = foldICmpIntrinsicWithConstant(Cmp, C))
2503 return I;
2504
Sanjay Patel1710e7c2016-07-21 17:15:49 +00002505 return nullptr;
2506}
Jim Grosbach129c52a2011-09-30 18:09:53 +00002507
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002508/// Fold an icmp equality instruction with binary operator LHS and constant RHS:
2509/// icmp eq/ne BO, C.
2510Instruction *InstCombiner::foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp,
2511 BinaryOperator *BO,
2512 const APInt *C) {
2513 // TODO: Some of these folds could work with arbitrary constants, but this
2514 // function is limited to scalar and vector splat constants.
2515 if (!Cmp.isEquality())
Sanjay Patel1710e7c2016-07-21 17:15:49 +00002516 return nullptr;
2517
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002518 ICmpInst::Predicate Pred = Cmp.getPredicate();
2519 bool isICMP_NE = Pred == ICmpInst::ICMP_NE;
2520 Constant *RHS = cast<Constant>(Cmp.getOperand(1));
Sanjay Patel51a767c2016-08-03 17:23:08 +00002521 Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
Sanjay Patel1710e7c2016-07-21 17:15:49 +00002522
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002523 switch (BO->getOpcode()) {
2524 case Instruction::SRem:
2525 // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
Craig Topper73ba1c82017-06-07 07:40:37 +00002526 if (C->isNullValue() && BO->hasOneUse()) {
Sanjay Patel2e9675f2016-08-03 19:48:40 +00002527 const APInt *BOC;
2528 if (match(BOp1, m_APInt(BOC)) && BOC->sgt(1) && BOC->isPowerOf2()) {
Sanjay Patel51a767c2016-08-03 17:23:08 +00002529 Value *NewRem = Builder->CreateURem(BOp0, BOp1, BO->getName());
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002530 return new ICmpInst(Pred, NewRem,
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002531 Constant::getNullValue(BO->getType()));
Chris Lattner2188e402010-01-04 07:37:31 +00002532 }
Sanjay Patel1710e7c2016-07-21 17:15:49 +00002533 }
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002534 break;
Sanjay Patel00a324e2016-08-03 22:08:44 +00002535 case Instruction::Add: {
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002536 // Replace ((add A, B) != C) with (A != C-B) if B & C are constants.
Sanjay Patel00a324e2016-08-03 22:08:44 +00002537 const APInt *BOC;
2538 if (match(BOp1, m_APInt(BOC))) {
2539 if (BO->hasOneUse()) {
2540 Constant *SubC = ConstantExpr::getSub(RHS, cast<Constant>(BOp1));
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002541 return new ICmpInst(Pred, BOp0, SubC);
Sanjay Patel00a324e2016-08-03 22:08:44 +00002542 }
Craig Topper73ba1c82017-06-07 07:40:37 +00002543 } else if (C->isNullValue()) {
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002544 // Replace ((add A, B) != 0) with (A != -B) if A or B is
2545 // efficiently invertible, or if the add has just this one use.
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002546 if (Value *NegVal = dyn_castNegVal(BOp1))
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002547 return new ICmpInst(Pred, BOp0, NegVal);
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002548 if (Value *NegVal = dyn_castNegVal(BOp0))
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002549 return new ICmpInst(Pred, NegVal, BOp1);
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002550 if (BO->hasOneUse()) {
2551 Value *Neg = Builder->CreateNeg(BOp1);
2552 Neg->takeName(BO);
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002553 return new ICmpInst(Pred, BOp0, Neg);
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002554 }
2555 }
2556 break;
Sanjay Patel00a324e2016-08-03 22:08:44 +00002557 }
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002558 case Instruction::Xor:
2559 if (BO->hasOneUse()) {
Sanjay Patel51a767c2016-08-03 17:23:08 +00002560 if (Constant *BOC = dyn_cast<Constant>(BOp1)) {
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002561 // For the xor case, we can xor two constants together, eliminating
2562 // the explicit xor.
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002563 return new ICmpInst(Pred, BOp0, ConstantExpr::getXor(RHS, BOC));
Craig Topper73ba1c82017-06-07 07:40:37 +00002564 } else if (C->isNullValue()) {
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002565 // Replace ((xor A, B) != 0) with (A != B)
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002566 return new ICmpInst(Pred, BOp0, BOp1);
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002567 }
2568 }
2569 break;
2570 case Instruction::Sub:
2571 if (BO->hasOneUse()) {
Sanjay Patel9d591d12016-08-04 15:19:25 +00002572 const APInt *BOC;
2573 if (match(BOp0, m_APInt(BOC))) {
Sanjay Patel362ff5c2016-09-15 17:01:17 +00002574 // Replace ((sub BOC, B) != C) with (B != BOC-C).
Sanjay Patel9d591d12016-08-04 15:19:25 +00002575 Constant *SubC = ConstantExpr::getSub(cast<Constant>(BOp0), RHS);
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002576 return new ICmpInst(Pred, BOp1, SubC);
Craig Topper73ba1c82017-06-07 07:40:37 +00002577 } else if (C->isNullValue()) {
Sanjay Patel362ff5c2016-09-15 17:01:17 +00002578 // Replace ((sub A, B) != 0) with (A != B).
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002579 return new ICmpInst(Pred, BOp0, BOp1);
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002580 }
2581 }
2582 break;
Sanjay Patelb3de75d2016-08-04 19:12:12 +00002583 case Instruction::Or: {
2584 const APInt *BOC;
2585 if (match(BOp1, m_APInt(BOC)) && BO->hasOneUse() && RHS->isAllOnesValue()) {
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002586 // Comparing if all bits outside of a constant mask are set?
2587 // Replace (X | C) == -1 with (X & ~C) == ~C.
2588 // This removes the -1 constant.
Sanjay Patelb3de75d2016-08-04 19:12:12 +00002589 Constant *NotBOC = ConstantExpr::getNot(cast<Constant>(BOp1));
2590 Value *And = Builder->CreateAnd(BOp0, NotBOC);
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002591 return new ICmpInst(Pred, And, NotBOC);
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002592 }
2593 break;
Sanjay Patelb3de75d2016-08-04 19:12:12 +00002594 }
Sanjay Pateld938e882016-08-04 20:05:02 +00002595 case Instruction::And: {
2596 const APInt *BOC;
2597 if (match(BOp1, m_APInt(BOC))) {
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002598 // If we have ((X & C) == C), turn it into ((X & C) != 0).
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002599 if (C == BOC && C->isPowerOf2())
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002600 return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE,
Sanjay Patelab50a932016-08-02 22:38:33 +00002601 BO, Constant::getNullValue(RHS->getType()));
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002602
2603 // Don't perform the following transforms if the AND has multiple uses
2604 if (!BO->hasOneUse())
2605 break;
2606
2607 // Replace (and X, (1 << size(X)-1) != 0) with x s< 0
Craig Topperbcfd2d12017-04-20 16:56:25 +00002608 if (BOC->isSignMask()) {
Sanjay Patel51a767c2016-08-03 17:23:08 +00002609 Constant *Zero = Constant::getNullValue(BOp0->getType());
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002610 auto NewPred = isICMP_NE ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
2611 return new ICmpInst(NewPred, BOp0, Zero);
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002612 }
2613
2614 // ((X & ~7) == 0) --> X < 8
Craig Topper73ba1c82017-06-07 07:40:37 +00002615 if (C->isNullValue() && (~(*BOC) + 1).isPowerOf2()) {
Sanjay Pateld938e882016-08-04 20:05:02 +00002616 Constant *NegBOC = ConstantExpr::getNeg(cast<Constant>(BOp1));
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002617 auto NewPred = isICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
2618 return new ICmpInst(NewPred, BOp0, NegBOC);
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002619 }
2620 }
2621 break;
Sanjay Pateld938e882016-08-04 20:05:02 +00002622 }
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002623 case Instruction::Mul:
Craig Topper73ba1c82017-06-07 07:40:37 +00002624 if (C->isNullValue() && BO->hasNoSignedWrap()) {
Sanjay Patel3bade132016-08-04 22:19:27 +00002625 const APInt *BOC;
Craig Topper73ba1c82017-06-07 07:40:37 +00002626 if (match(BOp1, m_APInt(BOC)) && !BOC->isNullValue()) {
Sanjay Patel3bade132016-08-04 22:19:27 +00002627 // The trivial case (mul X, 0) is handled by InstSimplify.
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002628 // General case : (mul X, C) != 0 iff X != 0
2629 // (mul X, C) == 0 iff X == 0
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002630 return new ICmpInst(Pred, BOp0, Constant::getNullValue(RHS->getType()));
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002631 }
2632 }
2633 break;
Sanjay Patel6ebd5852016-07-23 00:28:39 +00002634 case Instruction::UDiv:
Craig Topper73ba1c82017-06-07 07:40:37 +00002635 if (C->isNullValue()) {
Sanjay Patel6ebd5852016-07-23 00:28:39 +00002636 // (icmp eq/ne (udiv A, B), 0) -> (icmp ugt/ule i32 B, A)
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002637 auto NewPred = isICMP_NE ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_UGT;
2638 return new ICmpInst(NewPred, BOp1, BOp0);
Sanjay Patel6ebd5852016-07-23 00:28:39 +00002639 }
2640 break;
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002641 default:
2642 break;
2643 }
2644 return nullptr;
2645}
2646
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002647/// Fold an icmp with LLVM intrinsic and constant operand: icmp Pred II, C.
2648Instruction *InstCombiner::foldICmpIntrinsicWithConstant(ICmpInst &Cmp,
2649 const APInt *C) {
2650 IntrinsicInst *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0));
2651 if (!II || !Cmp.isEquality())
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002652 return nullptr;
2653
2654 // Handle icmp {eq|ne} <intrinsic>, intcst.
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002655 switch (II->getIntrinsicID()) {
2656 case Intrinsic::bswap:
2657 Worklist.Add(II);
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002658 Cmp.setOperand(0, II->getArgOperand(0));
2659 Cmp.setOperand(1, Builder->getInt(C->byteSwap()));
2660 return &Cmp;
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002661 case Intrinsic::ctlz:
2662 case Intrinsic::cttz:
Amaury Sechet6bea6742016-08-04 05:27:20 +00002663 // ctz(A) == bitwidth(A) -> A == 0 and likewise for !=
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002664 if (*C == C->getBitWidth()) {
Sanjay Patel1710e7c2016-07-21 17:15:49 +00002665 Worklist.Add(II);
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002666 Cmp.setOperand(0, II->getArgOperand(0));
2667 Cmp.setOperand(1, ConstantInt::getNullValue(II->getType()));
2668 return &Cmp;
Chris Lattner2188e402010-01-04 07:37:31 +00002669 }
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002670 break;
Amaury Sechet6bea6742016-08-04 05:27:20 +00002671 case Intrinsic::ctpop: {
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002672 // popcount(A) == 0 -> A == 0 and likewise for !=
Amaury Sechet6bea6742016-08-04 05:27:20 +00002673 // popcount(A) == bitwidth(A) -> A == -1 and likewise for !=
Craig Topper73ba1c82017-06-07 07:40:37 +00002674 bool IsZero = C->isNullValue();
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002675 if (IsZero || *C == C->getBitWidth()) {
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002676 Worklist.Add(II);
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002677 Cmp.setOperand(0, II->getArgOperand(0));
2678 auto *NewOp = IsZero ? Constant::getNullValue(II->getType())
2679 : Constant::getAllOnesValue(II->getType());
2680 Cmp.setOperand(1, NewOp);
2681 return &Cmp;
Amaury Sechet6bea6742016-08-04 05:27:20 +00002682 }
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002683 break;
Sanjay Patel0a3d72b2016-09-10 15:33:39 +00002684 }
Sanjay Patel18fa9d32016-07-21 23:27:36 +00002685 default:
2686 break;
Chris Lattner2188e402010-01-04 07:37:31 +00002687 }
Craig Topperf40110f2014-04-25 05:29:35 +00002688 return nullptr;
Chris Lattner2188e402010-01-04 07:37:31 +00002689}
2690
Sanjay Patel10494b22016-09-16 16:10:22 +00002691/// Handle icmp with constant (but not simple integer constant) RHS.
2692Instruction *InstCombiner::foldICmpInstWithConstantNotInt(ICmpInst &I) {
2693 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2694 Constant *RHSC = dyn_cast<Constant>(Op1);
2695 Instruction *LHSI = dyn_cast<Instruction>(Op0);
2696 if (!RHSC || !LHSI)
2697 return nullptr;
2698
2699 switch (LHSI->getOpcode()) {
2700 case Instruction::GetElementPtr:
2701 // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null
2702 if (RHSC->isNullValue() &&
2703 cast<GetElementPtrInst>(LHSI)->hasAllZeroIndices())
2704 return new ICmpInst(
2705 I.getPredicate(), LHSI->getOperand(0),
2706 Constant::getNullValue(LHSI->getOperand(0)->getType()));
2707 break;
2708 case Instruction::PHI:
2709 // Only fold icmp into the PHI if the phi and icmp are in the same
2710 // block. If in the same block, we're encouraging jump threading. If
2711 // not, we are just pessimizing the code by making an i1 phi.
2712 if (LHSI->getParent() == I.getParent())
Craig Topperfb71b7d2017-04-14 19:20:12 +00002713 if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
Sanjay Patel10494b22016-09-16 16:10:22 +00002714 return NV;
2715 break;
2716 case Instruction::Select: {
2717 // If either operand of the select is a constant, we can fold the
2718 // comparison into the select arms, which will cause one to be
2719 // constant folded and the select turned into a bitwise or.
2720 Value *Op1 = nullptr, *Op2 = nullptr;
2721 ConstantInt *CI = nullptr;
2722 if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
2723 Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
2724 CI = dyn_cast<ConstantInt>(Op1);
2725 }
2726 if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
2727 Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
2728 CI = dyn_cast<ConstantInt>(Op2);
2729 }
2730
2731 // We only want to perform this transformation if it will not lead to
2732 // additional code. This is true if either both sides of the select
2733 // fold to a constant (in which case the icmp is replaced with a select
2734 // which will usually simplify) or this is the only user of the
2735 // select (in which case we are trading a select+icmp for a simpler
2736 // select+icmp) or all uses of the select can be replaced based on
2737 // dominance information ("Global cases").
2738 bool Transform = false;
2739 if (Op1 && Op2)
2740 Transform = true;
2741 else if (Op1 || Op2) {
2742 // Local case
2743 if (LHSI->hasOneUse())
2744 Transform = true;
2745 // Global cases
2746 else if (CI && !CI->isZero())
2747 // When Op1 is constant try replacing select with second operand.
2748 // Otherwise Op2 is constant and try replacing select with first
2749 // operand.
2750 Transform =
2751 replacedSelectWithOperand(cast<SelectInst>(LHSI), &I, Op1 ? 2 : 1);
2752 }
2753 if (Transform) {
2754 if (!Op1)
2755 Op1 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(1), RHSC,
2756 I.getName());
2757 if (!Op2)
2758 Op2 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(2), RHSC,
2759 I.getName());
2760 return SelectInst::Create(LHSI->getOperand(0), Op1, Op2);
2761 }
2762 break;
2763 }
2764 case Instruction::IntToPtr:
2765 // icmp pred inttoptr(X), null -> icmp pred X, 0
2766 if (RHSC->isNullValue() &&
2767 DL.getIntPtrType(RHSC->getType()) == LHSI->getOperand(0)->getType())
2768 return new ICmpInst(
2769 I.getPredicate(), LHSI->getOperand(0),
2770 Constant::getNullValue(LHSI->getOperand(0)->getType()));
2771 break;
2772
2773 case Instruction::Load:
2774 // Try to optimize things like "A[i] > 4" to index computations.
2775 if (GetElementPtrInst *GEP =
2776 dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) {
2777 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
2778 if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
2779 !cast<LoadInst>(LHSI)->isVolatile())
2780 if (Instruction *Res = foldCmpLoadFromIndexedGlobal(GEP, GV, I))
2781 return Res;
2782 }
2783 break;
2784 }
2785
2786 return nullptr;
2787}
2788
2789/// Try to fold icmp (binop), X or icmp X, (binop).
Sanjay Patel2df38a82017-05-08 16:21:55 +00002790/// TODO: A large part of this logic is duplicated in InstSimplify's
2791/// simplifyICmpWithBinOp(). We should be able to share that and avoid the code
2792/// duplication.
Sanjay Patel10494b22016-09-16 16:10:22 +00002793Instruction *InstCombiner::foldICmpBinOp(ICmpInst &I) {
2794 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2795
2796 // Special logic for binary operators.
2797 BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Op0);
2798 BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Op1);
2799 if (!BO0 && !BO1)
2800 return nullptr;
2801
Sanjay Patel2a062632017-05-08 16:33:42 +00002802 const CmpInst::Predicate Pred = I.getPredicate();
Sanjay Patel10494b22016-09-16 16:10:22 +00002803 bool NoOp0WrapProblem = false, NoOp1WrapProblem = false;
2804 if (BO0 && isa<OverflowingBinaryOperator>(BO0))
2805 NoOp0WrapProblem =
2806 ICmpInst::isEquality(Pred) ||
2807 (CmpInst::isUnsigned(Pred) && BO0->hasNoUnsignedWrap()) ||
2808 (CmpInst::isSigned(Pred) && BO0->hasNoSignedWrap());
2809 if (BO1 && isa<OverflowingBinaryOperator>(BO1))
2810 NoOp1WrapProblem =
2811 ICmpInst::isEquality(Pred) ||
2812 (CmpInst::isUnsigned(Pred) && BO1->hasNoUnsignedWrap()) ||
2813 (CmpInst::isSigned(Pred) && BO1->hasNoSignedWrap());
2814
2815 // Analyze the case when either Op0 or Op1 is an add instruction.
2816 // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null).
2817 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
2818 if (BO0 && BO0->getOpcode() == Instruction::Add) {
2819 A = BO0->getOperand(0);
2820 B = BO0->getOperand(1);
2821 }
2822 if (BO1 && BO1->getOpcode() == Instruction::Add) {
2823 C = BO1->getOperand(0);
2824 D = BO1->getOperand(1);
2825 }
2826
Sanjay Patel10494b22016-09-16 16:10:22 +00002827 // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
2828 if ((A == Op1 || B == Op1) && NoOp0WrapProblem)
2829 return new ICmpInst(Pred, A == Op1 ? B : A,
2830 Constant::getNullValue(Op1->getType()));
2831
2832 // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
2833 if ((C == Op0 || D == Op0) && NoOp1WrapProblem)
2834 return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()),
2835 C == Op0 ? D : C);
2836
2837 // icmp (X+Y), (X+Z) -> icmp Y, Z for equalities or if there is no overflow.
2838 if (A && C && (A == C || A == D || B == C || B == D) && NoOp0WrapProblem &&
2839 NoOp1WrapProblem &&
2840 // Try not to increase register pressure.
2841 BO0->hasOneUse() && BO1->hasOneUse()) {
2842 // Determine Y and Z in the form icmp (X+Y), (X+Z).
2843 Value *Y, *Z;
2844 if (A == C) {
2845 // C + B == C + D -> B == D
2846 Y = B;
2847 Z = D;
2848 } else if (A == D) {
2849 // D + B == C + D -> B == C
2850 Y = B;
2851 Z = C;
2852 } else if (B == C) {
2853 // A + C == C + D -> A == D
2854 Y = A;
2855 Z = D;
2856 } else {
2857 assert(B == D);
2858 // A + D == C + D -> A == C
2859 Y = A;
2860 Z = C;
2861 }
2862 return new ICmpInst(Pred, Y, Z);
2863 }
2864
2865 // icmp slt (X + -1), Y -> icmp sle X, Y
2866 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLT &&
2867 match(B, m_AllOnes()))
2868 return new ICmpInst(CmpInst::ICMP_SLE, A, Op1);
2869
2870 // icmp sge (X + -1), Y -> icmp sgt X, Y
2871 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGE &&
2872 match(B, m_AllOnes()))
2873 return new ICmpInst(CmpInst::ICMP_SGT, A, Op1);
2874
2875 // icmp sle (X + 1), Y -> icmp slt X, Y
2876 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLE && match(B, m_One()))
2877 return new ICmpInst(CmpInst::ICMP_SLT, A, Op1);
2878
2879 // icmp sgt (X + 1), Y -> icmp sge X, Y
2880 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGT && match(B, m_One()))
2881 return new ICmpInst(CmpInst::ICMP_SGE, A, Op1);
2882
2883 // icmp sgt X, (Y + -1) -> icmp sge X, Y
2884 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGT &&
2885 match(D, m_AllOnes()))
2886 return new ICmpInst(CmpInst::ICMP_SGE, Op0, C);
2887
2888 // icmp sle X, (Y + -1) -> icmp slt X, Y
2889 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLE &&
2890 match(D, m_AllOnes()))
2891 return new ICmpInst(CmpInst::ICMP_SLT, Op0, C);
2892
2893 // icmp sge X, (Y + 1) -> icmp sgt X, Y
2894 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGE && match(D, m_One()))
2895 return new ICmpInst(CmpInst::ICMP_SGT, Op0, C);
2896
2897 // icmp slt X, (Y + 1) -> icmp sle X, Y
2898 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLT && match(D, m_One()))
2899 return new ICmpInst(CmpInst::ICMP_SLE, Op0, C);
2900
Sanjay Patel40f40172017-01-13 23:25:46 +00002901 // TODO: The subtraction-related identities shown below also hold, but
2902 // canonicalization from (X -nuw 1) to (X + -1) means that the combinations
2903 // wouldn't happen even if they were implemented.
2904 //
2905 // icmp ult (X - 1), Y -> icmp ule X, Y
2906 // icmp uge (X - 1), Y -> icmp ugt X, Y
2907 // icmp ugt X, (Y - 1) -> icmp uge X, Y
2908 // icmp ule X, (Y - 1) -> icmp ult X, Y
2909
2910 // icmp ule (X + 1), Y -> icmp ult X, Y
2911 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_ULE && match(B, m_One()))
2912 return new ICmpInst(CmpInst::ICMP_ULT, A, Op1);
2913
2914 // icmp ugt (X + 1), Y -> icmp uge X, Y
2915 if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_UGT && match(B, m_One()))
2916 return new ICmpInst(CmpInst::ICMP_UGE, A, Op1);
2917
2918 // icmp uge X, (Y + 1) -> icmp ugt X, Y
2919 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_UGE && match(D, m_One()))
2920 return new ICmpInst(CmpInst::ICMP_UGT, Op0, C);
2921
2922 // icmp ult X, (Y + 1) -> icmp ule X, Y
2923 if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_ULT && match(D, m_One()))
2924 return new ICmpInst(CmpInst::ICMP_ULE, Op0, C);
2925
Sanjay Patel10494b22016-09-16 16:10:22 +00002926 // if C1 has greater magnitude than C2:
2927 // icmp (X + C1), (Y + C2) -> icmp (X + C3), Y
2928 // s.t. C3 = C1 - C2
2929 //
2930 // if C2 has greater magnitude than C1:
2931 // icmp (X + C1), (Y + C2) -> icmp X, (Y + C3)
2932 // s.t. C3 = C2 - C1
2933 if (A && C && NoOp0WrapProblem && NoOp1WrapProblem &&
2934 (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned())
2935 if (ConstantInt *C1 = dyn_cast<ConstantInt>(B))
2936 if (ConstantInt *C2 = dyn_cast<ConstantInt>(D)) {
2937 const APInt &AP1 = C1->getValue();
2938 const APInt &AP2 = C2->getValue();
2939 if (AP1.isNegative() == AP2.isNegative()) {
2940 APInt AP1Abs = C1->getValue().abs();
2941 APInt AP2Abs = C2->getValue().abs();
2942 if (AP1Abs.uge(AP2Abs)) {
2943 ConstantInt *C3 = Builder->getInt(AP1 - AP2);
2944 Value *NewAdd = Builder->CreateNSWAdd(A, C3);
2945 return new ICmpInst(Pred, NewAdd, C);
2946 } else {
2947 ConstantInt *C3 = Builder->getInt(AP2 - AP1);
2948 Value *NewAdd = Builder->CreateNSWAdd(C, C3);
2949 return new ICmpInst(Pred, A, NewAdd);
2950 }
2951 }
2952 }
2953
2954 // Analyze the case when either Op0 or Op1 is a sub instruction.
2955 // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null).
2956 A = nullptr;
2957 B = nullptr;
2958 C = nullptr;
2959 D = nullptr;
2960 if (BO0 && BO0->getOpcode() == Instruction::Sub) {
2961 A = BO0->getOperand(0);
2962 B = BO0->getOperand(1);
2963 }
2964 if (BO1 && BO1->getOpcode() == Instruction::Sub) {
2965 C = BO1->getOperand(0);
2966 D = BO1->getOperand(1);
2967 }
2968
2969 // icmp (X-Y), X -> icmp 0, Y for equalities or if there is no overflow.
2970 if (A == Op1 && NoOp0WrapProblem)
2971 return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B);
2972
2973 // icmp X, (X-Y) -> icmp Y, 0 for equalities or if there is no overflow.
2974 if (C == Op0 && NoOp1WrapProblem)
2975 return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType()));
2976
2977 // icmp (Y-X), (Z-X) -> icmp Y, Z for equalities or if there is no overflow.
2978 if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem &&
2979 // Try not to increase register pressure.
2980 BO0->hasOneUse() && BO1->hasOneUse())
2981 return new ICmpInst(Pred, A, C);
2982
2983 // icmp (X-Y), (X-Z) -> icmp Z, Y for equalities or if there is no overflow.
2984 if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem &&
2985 // Try not to increase register pressure.
2986 BO0->hasOneUse() && BO1->hasOneUse())
2987 return new ICmpInst(Pred, D, B);
2988
2989 // icmp (0-X) < cst --> x > -cst
2990 if (NoOp0WrapProblem && ICmpInst::isSigned(Pred)) {
2991 Value *X;
2992 if (match(BO0, m_Neg(m_Value(X))))
2993 if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1))
2994 if (!RHSC->isMinValue(/*isSigned=*/true))
2995 return new ICmpInst(I.getSwappedPredicate(), X,
2996 ConstantExpr::getNeg(RHSC));
2997 }
2998
2999 BinaryOperator *SRem = nullptr;
3000 // icmp (srem X, Y), Y
3001 if (BO0 && BO0->getOpcode() == Instruction::SRem && Op1 == BO0->getOperand(1))
3002 SRem = BO0;
3003 // icmp Y, (srem X, Y)
3004 else if (BO1 && BO1->getOpcode() == Instruction::SRem &&
3005 Op0 == BO1->getOperand(1))
3006 SRem = BO1;
3007 if (SRem) {
3008 // We don't check hasOneUse to avoid increasing register pressure because
3009 // the value we use is the same value this instruction was already using.
3010 switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) {
3011 default:
3012 break;
3013 case ICmpInst::ICMP_EQ:
3014 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
3015 case ICmpInst::ICMP_NE:
3016 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
3017 case ICmpInst::ICMP_SGT:
3018 case ICmpInst::ICMP_SGE:
3019 return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1),
3020 Constant::getAllOnesValue(SRem->getType()));
3021 case ICmpInst::ICMP_SLT:
3022 case ICmpInst::ICMP_SLE:
3023 return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1),
3024 Constant::getNullValue(SRem->getType()));
3025 }
3026 }
3027
3028 if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() && BO0->hasOneUse() &&
3029 BO1->hasOneUse() && BO0->getOperand(1) == BO1->getOperand(1)) {
3030 switch (BO0->getOpcode()) {
3031 default:
3032 break;
3033 case Instruction::Add:
3034 case Instruction::Sub:
Sanjay Pateld3106ad2017-05-23 17:29:58 +00003035 case Instruction::Xor: {
Sanjay Patel10494b22016-09-16 16:10:22 +00003036 if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b
Sanjay Patel2a062632017-05-08 16:33:42 +00003037 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
Sanjay Pateld3106ad2017-05-23 17:29:58 +00003038
3039 const APInt *C;
3040 if (match(BO0->getOperand(1), m_APInt(C))) {
3041 // icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b
3042 if (C->isSignMask()) {
Sanjay Patel2a062632017-05-08 16:33:42 +00003043 ICmpInst::Predicate NewPred =
Sanjay Patel10494b22016-09-16 16:10:22 +00003044 I.isSigned() ? I.getUnsignedPredicate() : I.getSignedPredicate();
Sanjay Patel2a062632017-05-08 16:33:42 +00003045 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
Sanjay Patel10494b22016-09-16 16:10:22 +00003046 }
3047
Sanjay Pateld3106ad2017-05-23 17:29:58 +00003048 // icmp u/s (a ^ maxsignval), (b ^ maxsignval) --> icmp s/u' a, b
3049 if (BO0->getOpcode() == Instruction::Xor && C->isMaxSignedValue()) {
Sanjay Patel2a062632017-05-08 16:33:42 +00003050 ICmpInst::Predicate NewPred =
Sanjay Patel10494b22016-09-16 16:10:22 +00003051 I.isSigned() ? I.getUnsignedPredicate() : I.getSignedPredicate();
Sanjay Patel2a062632017-05-08 16:33:42 +00003052 NewPred = I.getSwappedPredicate(NewPred);
3053 return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
Sanjay Patel10494b22016-09-16 16:10:22 +00003054 }
3055 }
3056 break;
Sanjay Pateld3106ad2017-05-23 17:29:58 +00003057 }
Sanjay Patel07b1ba52017-05-24 22:58:17 +00003058 case Instruction::Mul: {
Sanjay Patel10494b22016-09-16 16:10:22 +00003059 if (!I.isEquality())
3060 break;
3061
Sanjay Patel07b1ba52017-05-24 22:58:17 +00003062 const APInt *C;
Craig Topper73ba1c82017-06-07 07:40:37 +00003063 if (match(BO0->getOperand(1), m_APInt(C)) && !C->isNullValue() &&
3064 !C->isOneValue()) {
Sanjay Patel07b1ba52017-05-24 22:58:17 +00003065 // icmp eq/ne (X * C), (Y * C) --> icmp (X & Mask), (Y & Mask)
3066 // Mask = -1 >> count-trailing-zeros(C).
Sanjay Patel51506122017-05-25 14:13:57 +00003067 if (unsigned TZs = C->countTrailingZeros()) {
Sanjay Patel07b1ba52017-05-24 22:58:17 +00003068 Constant *Mask = ConstantInt::get(
3069 BO0->getType(),
Sanjay Patel51506122017-05-25 14:13:57 +00003070 APInt::getLowBitsSet(C->getBitWidth(), C->getBitWidth() - TZs));
Sanjay Patel10494b22016-09-16 16:10:22 +00003071 Value *And1 = Builder->CreateAnd(BO0->getOperand(0), Mask);
3072 Value *And2 = Builder->CreateAnd(BO1->getOperand(0), Mask);
Sanjay Patel2a062632017-05-08 16:33:42 +00003073 return new ICmpInst(Pred, And1, And2);
Sanjay Patel10494b22016-09-16 16:10:22 +00003074 }
Sanjay Patel51506122017-05-25 14:13:57 +00003075 // If there are no trailing zeros in the multiplier, just eliminate
3076 // the multiplies (no masking is needed):
3077 // icmp eq/ne (X * C), (Y * C) --> icmp eq/ne X, Y
3078 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
Sanjay Patel10494b22016-09-16 16:10:22 +00003079 }
3080 break;
Sanjay Patel07b1ba52017-05-24 22:58:17 +00003081 }
Sanjay Patel10494b22016-09-16 16:10:22 +00003082 case Instruction::UDiv:
3083 case Instruction::LShr:
Sanjay Patel878715f2017-05-15 19:27:53 +00003084 if (I.isSigned() || !BO0->isExact() || !BO1->isExact())
Sanjay Patel10494b22016-09-16 16:10:22 +00003085 break;
Sanjay Patel878715f2017-05-15 19:27:53 +00003086 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
3087
Sanjay Patel10494b22016-09-16 16:10:22 +00003088 case Instruction::SDiv:
Sanjay Patel878715f2017-05-15 19:27:53 +00003089 if (!I.isEquality() || !BO0->isExact() || !BO1->isExact())
3090 break;
3091 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
3092
Sanjay Patel10494b22016-09-16 16:10:22 +00003093 case Instruction::AShr:
3094 if (!BO0->isExact() || !BO1->isExact())
3095 break;
Sanjay Patel2a062632017-05-08 16:33:42 +00003096 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
Sanjay Patel878715f2017-05-15 19:27:53 +00003097
Sanjay Patel10494b22016-09-16 16:10:22 +00003098 case Instruction::Shl: {
3099 bool NUW = BO0->hasNoUnsignedWrap() && BO1->hasNoUnsignedWrap();
3100 bool NSW = BO0->hasNoSignedWrap() && BO1->hasNoSignedWrap();
3101 if (!NUW && !NSW)
3102 break;
3103 if (!NSW && I.isSigned())
3104 break;
Sanjay Patel2a062632017-05-08 16:33:42 +00003105 return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
Sanjay Patel10494b22016-09-16 16:10:22 +00003106 }
3107 }
3108 }
3109
3110 if (BO0) {
3111 // Transform A & (L - 1) `ult` L --> L != 0
3112 auto LSubOne = m_Add(m_Specific(Op1), m_AllOnes());
3113 auto BitwiseAnd =
3114 m_CombineOr(m_And(m_Value(), LSubOne), m_And(LSubOne, m_Value()));
3115
Sanjay Patel2a062632017-05-08 16:33:42 +00003116 if (match(BO0, BitwiseAnd) && Pred == ICmpInst::ICMP_ULT) {
Sanjay Patel10494b22016-09-16 16:10:22 +00003117 auto *Zero = Constant::getNullValue(BO0->getType());
3118 return new ICmpInst(ICmpInst::ICMP_NE, Op1, Zero);
3119 }
3120 }
3121
3122 return nullptr;
3123}
3124
Sanjay Pateldd46b522016-12-19 17:32:37 +00003125/// Fold icmp Pred min|max(X, Y), X.
3126static Instruction *foldICmpWithMinMax(ICmpInst &Cmp) {
Sanjay Pateld6406412016-12-15 19:13:37 +00003127 ICmpInst::Predicate Pred = Cmp.getPredicate();
3128 Value *Op0 = Cmp.getOperand(0);
3129 Value *X = Cmp.getOperand(1);
3130
Sanjay Pateldd46b522016-12-19 17:32:37 +00003131 // Canonicalize minimum or maximum operand to LHS of the icmp.
Sanjay Patel8296c6c2016-12-19 16:28:53 +00003132 if (match(X, m_c_SMin(m_Specific(Op0), m_Value())) ||
Sanjay Pateldd46b522016-12-19 17:32:37 +00003133 match(X, m_c_SMax(m_Specific(Op0), m_Value())) ||
3134 match(X, m_c_UMin(m_Specific(Op0), m_Value())) ||
3135 match(X, m_c_UMax(m_Specific(Op0), m_Value()))) {
Sanjay Pateld6406412016-12-15 19:13:37 +00003136 std::swap(Op0, X);
3137 Pred = Cmp.getSwappedPredicate();
3138 }
3139
3140 Value *Y;
Sanjay Patel8296c6c2016-12-19 16:28:53 +00003141 if (match(Op0, m_c_SMin(m_Specific(X), m_Value(Y)))) {
Sanjay Pateldd46b522016-12-19 17:32:37 +00003142 // smin(X, Y) == X --> X s<= Y
3143 // smin(X, Y) s>= X --> X s<= Y
Sanjay Patel8296c6c2016-12-19 16:28:53 +00003144 if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_SGE)
3145 return new ICmpInst(ICmpInst::ICMP_SLE, X, Y);
3146
Sanjay Pateldd46b522016-12-19 17:32:37 +00003147 // smin(X, Y) != X --> X s> Y
3148 // smin(X, Y) s< X --> X s> Y
Sanjay Patel8296c6c2016-12-19 16:28:53 +00003149 if (Pred == CmpInst::ICMP_NE || Pred == CmpInst::ICMP_SLT)
3150 return new ICmpInst(ICmpInst::ICMP_SGT, X, Y);
3151
3152 // These cases should be handled in InstSimplify:
Sanjay Pateldd46b522016-12-19 17:32:37 +00003153 // smin(X, Y) s<= X --> true
3154 // smin(X, Y) s> X --> false
Sanjay Pateld6406412016-12-15 19:13:37 +00003155 return nullptr;
Sanjay Patel8296c6c2016-12-19 16:28:53 +00003156 }
Sanjay Pateldd46b522016-12-19 17:32:37 +00003157
Sanjay Patel8296c6c2016-12-19 16:28:53 +00003158 if (match(Op0, m_c_SMax(m_Specific(X), m_Value(Y)))) {
Sanjay Pateldd46b522016-12-19 17:32:37 +00003159 // smax(X, Y) == X --> X s>= Y
3160 // smax(X, Y) s<= X --> X s>= Y
Sanjay Patel8296c6c2016-12-19 16:28:53 +00003161 if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_SLE)
3162 return new ICmpInst(ICmpInst::ICMP_SGE, X, Y);
Sanjay Pateld6406412016-12-15 19:13:37 +00003163
Sanjay Pateldd46b522016-12-19 17:32:37 +00003164 // smax(X, Y) != X --> X s< Y
3165 // smax(X, Y) s> X --> X s< Y
Sanjay Patel8296c6c2016-12-19 16:28:53 +00003166 if (Pred == CmpInst::ICMP_NE || Pred == CmpInst::ICMP_SGT)
3167 return new ICmpInst(ICmpInst::ICMP_SLT, X, Y);
Sanjay Pateld6406412016-12-15 19:13:37 +00003168
Sanjay Patel8296c6c2016-12-19 16:28:53 +00003169 // These cases should be handled in InstSimplify:
Sanjay Pateldd46b522016-12-19 17:32:37 +00003170 // smax(X, Y) s>= X --> true
3171 // smax(X, Y) s< X --> false
3172 return nullptr;
3173 }
3174
3175 if (match(Op0, m_c_UMin(m_Specific(X), m_Value(Y)))) {
3176 // umin(X, Y) == X --> X u<= Y
3177 // umin(X, Y) u>= X --> X u<= Y
3178 if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_UGE)
3179 return new ICmpInst(ICmpInst::ICMP_ULE, X, Y);
3180
3181 // umin(X, Y) != X --> X u> Y
3182 // umin(X, Y) u< X --> X u> Y
3183 if (Pred == CmpInst::ICMP_NE || Pred == CmpInst::ICMP_ULT)
3184 return new ICmpInst(ICmpInst::ICMP_UGT, X, Y);
3185
3186 // These cases should be handled in InstSimplify:
3187 // umin(X, Y) u<= X --> true
3188 // umin(X, Y) u> X --> false
3189 return nullptr;
3190 }
3191
3192 if (match(Op0, m_c_UMax(m_Specific(X), m_Value(Y)))) {
3193 // umax(X, Y) == X --> X u>= Y
3194 // umax(X, Y) u<= X --> X u>= Y
3195 if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_ULE)
3196 return new ICmpInst(ICmpInst::ICMP_UGE, X, Y);
3197
3198 // umax(X, Y) != X --> X u< Y
3199 // umax(X, Y) u> X --> X u< Y
3200 if (Pred == CmpInst::ICMP_NE || Pred == CmpInst::ICMP_UGT)
3201 return new ICmpInst(ICmpInst::ICMP_ULT, X, Y);
3202
3203 // These cases should be handled in InstSimplify:
3204 // umax(X, Y) u>= X --> true
3205 // umax(X, Y) u< X --> false
Sanjay Patel8296c6c2016-12-19 16:28:53 +00003206 return nullptr;
3207 }
Sanjay Pateld6406412016-12-15 19:13:37 +00003208
Sanjay Pateld6406412016-12-15 19:13:37 +00003209 return nullptr;
3210}
3211
Sanjay Patel10494b22016-09-16 16:10:22 +00003212Instruction *InstCombiner::foldICmpEquality(ICmpInst &I) {
3213 if (!I.isEquality())
3214 return nullptr;
3215
3216 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3217 Value *A, *B, *C, *D;
3218 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
3219 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
3220 Value *OtherVal = A == Op1 ? B : A;
3221 return new ICmpInst(I.getPredicate(), OtherVal,
3222 Constant::getNullValue(A->getType()));
3223 }
3224
3225 if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
3226 // A^c1 == C^c2 --> A == C^(c1^c2)
3227 ConstantInt *C1, *C2;
3228 if (match(B, m_ConstantInt(C1)) && match(D, m_ConstantInt(C2)) &&
3229 Op1->hasOneUse()) {
3230 Constant *NC = Builder->getInt(C1->getValue() ^ C2->getValue());
3231 Value *Xor = Builder->CreateXor(C, NC);
3232 return new ICmpInst(I.getPredicate(), A, Xor);
3233 }
3234
3235 // A^B == A^D -> B == D
3236 if (A == C)
3237 return new ICmpInst(I.getPredicate(), B, D);
3238 if (A == D)
3239 return new ICmpInst(I.getPredicate(), B, C);
3240 if (B == C)
3241 return new ICmpInst(I.getPredicate(), A, D);
3242 if (B == D)
3243 return new ICmpInst(I.getPredicate(), A, C);
3244 }
3245 }
3246
3247 if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && (A == Op0 || B == Op0)) {
3248 // A == (A^B) -> B == 0
3249 Value *OtherVal = A == Op0 ? B : A;
3250 return new ICmpInst(I.getPredicate(), OtherVal,
3251 Constant::getNullValue(A->getType()));
3252 }
3253
3254 // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
3255 if (match(Op0, m_OneUse(m_And(m_Value(A), m_Value(B)))) &&
3256 match(Op1, m_OneUse(m_And(m_Value(C), m_Value(D))))) {
3257 Value *X = nullptr, *Y = nullptr, *Z = nullptr;
3258
3259 if (A == C) {
3260 X = B;
3261 Y = D;
3262 Z = A;
3263 } else if (A == D) {
3264 X = B;
3265 Y = C;
3266 Z = A;
3267 } else if (B == C) {
3268 X = A;
3269 Y = D;
3270 Z = B;
3271 } else if (B == D) {
3272 X = A;
3273 Y = C;
3274 Z = B;
3275 }
3276
3277 if (X) { // Build (X^Y) & Z
3278 Op1 = Builder->CreateXor(X, Y);
3279 Op1 = Builder->CreateAnd(Op1, Z);
3280 I.setOperand(0, Op1);
3281 I.setOperand(1, Constant::getNullValue(Op1->getType()));
3282 return &I;
3283 }
3284 }
3285
3286 // Transform (zext A) == (B & (1<<X)-1) --> A == (trunc B)
3287 // and (B & (1<<X)-1) == (zext A) --> A == (trunc B)
3288 ConstantInt *Cst1;
3289 if ((Op0->hasOneUse() && match(Op0, m_ZExt(m_Value(A))) &&
3290 match(Op1, m_And(m_Value(B), m_ConstantInt(Cst1)))) ||
3291 (Op1->hasOneUse() && match(Op0, m_And(m_Value(B), m_ConstantInt(Cst1))) &&
3292 match(Op1, m_ZExt(m_Value(A))))) {
3293 APInt Pow2 = Cst1->getValue() + 1;
3294 if (Pow2.isPowerOf2() && isa<IntegerType>(A->getType()) &&
3295 Pow2.logBase2() == cast<IntegerType>(A->getType())->getBitWidth())
3296 return new ICmpInst(I.getPredicate(), A,
3297 Builder->CreateTrunc(B, A->getType()));
3298 }
3299
3300 // (A >> C) == (B >> C) --> (A^B) u< (1 << C)
3301 // For lshr and ashr pairs.
3302 if ((match(Op0, m_OneUse(m_LShr(m_Value(A), m_ConstantInt(Cst1)))) &&
3303 match(Op1, m_OneUse(m_LShr(m_Value(B), m_Specific(Cst1))))) ||
3304 (match(Op0, m_OneUse(m_AShr(m_Value(A), m_ConstantInt(Cst1)))) &&
3305 match(Op1, m_OneUse(m_AShr(m_Value(B), m_Specific(Cst1)))))) {
3306 unsigned TypeBits = Cst1->getBitWidth();
3307 unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits);
3308 if (ShAmt < TypeBits && ShAmt != 0) {
3309 ICmpInst::Predicate Pred = I.getPredicate() == ICmpInst::ICMP_NE
3310 ? ICmpInst::ICMP_UGE
3311 : ICmpInst::ICMP_ULT;
3312 Value *Xor = Builder->CreateXor(A, B, I.getName() + ".unshifted");
3313 APInt CmpVal = APInt::getOneBitSet(TypeBits, ShAmt);
3314 return new ICmpInst(Pred, Xor, Builder->getInt(CmpVal));
3315 }
3316 }
3317
3318 // (A << C) == (B << C) --> ((A^B) & (~0U >> C)) == 0
3319 if (match(Op0, m_OneUse(m_Shl(m_Value(A), m_ConstantInt(Cst1)))) &&
3320 match(Op1, m_OneUse(m_Shl(m_Value(B), m_Specific(Cst1))))) {
3321 unsigned TypeBits = Cst1->getBitWidth();
3322 unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits);
3323 if (ShAmt < TypeBits && ShAmt != 0) {
3324 Value *Xor = Builder->CreateXor(A, B, I.getName() + ".unshifted");
3325 APInt AndVal = APInt::getLowBitsSet(TypeBits, TypeBits - ShAmt);
3326 Value *And = Builder->CreateAnd(Xor, Builder->getInt(AndVal),
3327 I.getName() + ".mask");
3328 return new ICmpInst(I.getPredicate(), And,
3329 Constant::getNullValue(Cst1->getType()));
3330 }
3331 }
3332
3333 // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to
3334 // "icmp (and X, mask), cst"
3335 uint64_t ShAmt = 0;
3336 if (Op0->hasOneUse() &&
3337 match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A), m_ConstantInt(ShAmt))))) &&
3338 match(Op1, m_ConstantInt(Cst1)) &&
3339 // Only do this when A has multiple uses. This is most important to do
3340 // when it exposes other optimizations.
3341 !A->hasOneUse()) {
3342 unsigned ASize = cast<IntegerType>(A->getType())->getPrimitiveSizeInBits();
3343
3344 if (ShAmt < ASize) {
3345 APInt MaskV =
3346 APInt::getLowBitsSet(ASize, Op0->getType()->getPrimitiveSizeInBits());
3347 MaskV <<= ShAmt;
3348
3349 APInt CmpV = Cst1->getValue().zext(ASize);
3350 CmpV <<= ShAmt;
3351
3352 Value *Mask = Builder->CreateAnd(A, Builder->getInt(MaskV));
3353 return new ICmpInst(I.getPredicate(), Mask, Builder->getInt(CmpV));
3354 }
3355 }
3356
3357 return nullptr;
3358}
3359
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003360/// Handle icmp (cast x to y), (cast/cst). We only handle extending casts so
3361/// far.
Sanjay Patel43395062016-07-21 18:07:40 +00003362Instruction *InstCombiner::foldICmpWithCastAndCast(ICmpInst &ICmp) {
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003363 const CastInst *LHSCI = cast<CastInst>(ICmp.getOperand(0));
Chris Lattner2188e402010-01-04 07:37:31 +00003364 Value *LHSCIOp = LHSCI->getOperand(0);
Chris Lattner229907c2011-07-18 04:54:35 +00003365 Type *SrcTy = LHSCIOp->getType();
3366 Type *DestTy = LHSCI->getType();
Chris Lattner2188e402010-01-04 07:37:31 +00003367 Value *RHSCIOp;
3368
Jim Grosbach129c52a2011-09-30 18:09:53 +00003369 // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
Chris Lattner2188e402010-01-04 07:37:31 +00003370 // integer type is the same size as the pointer type.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003371 if (LHSCI->getOpcode() == Instruction::PtrToInt &&
3372 DL.getPointerTypeSizeInBits(SrcTy) == DestTy->getIntegerBitWidth()) {
Craig Topperf40110f2014-04-25 05:29:35 +00003373 Value *RHSOp = nullptr;
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003374 if (auto *RHSC = dyn_cast<PtrToIntOperator>(ICmp.getOperand(1))) {
Michael Liaod266b922015-02-13 04:51:26 +00003375 Value *RHSCIOp = RHSC->getOperand(0);
3376 if (RHSCIOp->getType()->getPointerAddressSpace() ==
3377 LHSCIOp->getType()->getPointerAddressSpace()) {
3378 RHSOp = RHSC->getOperand(0);
3379 // If the pointer types don't match, insert a bitcast.
3380 if (LHSCIOp->getType() != RHSOp->getType())
3381 RHSOp = Builder->CreateBitCast(RHSOp, LHSCIOp->getType());
3382 }
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003383 } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
Chris Lattner2188e402010-01-04 07:37:31 +00003384 RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy);
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003385 }
Chris Lattner2188e402010-01-04 07:37:31 +00003386
3387 if (RHSOp)
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003388 return new ICmpInst(ICmp.getPredicate(), LHSCIOp, RHSOp);
Chris Lattner2188e402010-01-04 07:37:31 +00003389 }
Jim Grosbach129c52a2011-09-30 18:09:53 +00003390
Chris Lattner2188e402010-01-04 07:37:31 +00003391 // The code below only handles extension cast instructions, so far.
3392 // Enforce this.
3393 if (LHSCI->getOpcode() != Instruction::ZExt &&
3394 LHSCI->getOpcode() != Instruction::SExt)
Craig Topperf40110f2014-04-25 05:29:35 +00003395 return nullptr;
Chris Lattner2188e402010-01-04 07:37:31 +00003396
3397 bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt;
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003398 bool isSignedCmp = ICmp.isSigned();
Chris Lattner2188e402010-01-04 07:37:31 +00003399
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003400 if (auto *CI = dyn_cast<CastInst>(ICmp.getOperand(1))) {
Chris Lattner2188e402010-01-04 07:37:31 +00003401 // Not an extension from the same type?
3402 RHSCIOp = CI->getOperand(0);
Jim Grosbach129c52a2011-09-30 18:09:53 +00003403 if (RHSCIOp->getType() != LHSCIOp->getType())
Craig Topperf40110f2014-04-25 05:29:35 +00003404 return nullptr;
Jim Grosbach129c52a2011-09-30 18:09:53 +00003405
Chris Lattner2188e402010-01-04 07:37:31 +00003406 // If the signedness of the two casts doesn't agree (i.e. one is a sext
3407 // and the other is a zext), then we can't handle this.
3408 if (CI->getOpcode() != LHSCI->getOpcode())
Craig Topperf40110f2014-04-25 05:29:35 +00003409 return nullptr;
Chris Lattner2188e402010-01-04 07:37:31 +00003410
3411 // Deal with equality cases early.
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003412 if (ICmp.isEquality())
3413 return new ICmpInst(ICmp.getPredicate(), LHSCIOp, RHSCIOp);
Chris Lattner2188e402010-01-04 07:37:31 +00003414
3415 // A signed comparison of sign extended values simplifies into a
3416 // signed comparison.
3417 if (isSignedCmp && isSignedExt)
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003418 return new ICmpInst(ICmp.getPredicate(), LHSCIOp, RHSCIOp);
Chris Lattner2188e402010-01-04 07:37:31 +00003419
3420 // The other three cases all fold into an unsigned comparison.
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003421 return new ICmpInst(ICmp.getUnsignedPredicate(), LHSCIOp, RHSCIOp);
Chris Lattner2188e402010-01-04 07:37:31 +00003422 }
3423
Sanjay Patel4c204232016-06-04 20:39:22 +00003424 // If we aren't dealing with a constant on the RHS, exit early.
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003425 auto *C = dyn_cast<Constant>(ICmp.getOperand(1));
3426 if (!C)
Craig Topperf40110f2014-04-25 05:29:35 +00003427 return nullptr;
Chris Lattner2188e402010-01-04 07:37:31 +00003428
3429 // Compute the constant that would happen if we truncated to SrcTy then
Sanjay Patelc774f8c2016-06-04 21:20:44 +00003430 // re-extended to DestTy.
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003431 Constant *Res1 = ConstantExpr::getTrunc(C, SrcTy);
Sanjay Patelc774f8c2016-06-04 21:20:44 +00003432 Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy);
Chris Lattner2188e402010-01-04 07:37:31 +00003433
3434 // If the re-extended constant didn't change...
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003435 if (Res2 == C) {
Chris Lattner2188e402010-01-04 07:37:31 +00003436 // Deal with equality cases early.
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003437 if (ICmp.isEquality())
3438 return new ICmpInst(ICmp.getPredicate(), LHSCIOp, Res1);
Chris Lattner2188e402010-01-04 07:37:31 +00003439
3440 // A signed comparison of sign extended values simplifies into a
3441 // signed comparison.
3442 if (isSignedExt && isSignedCmp)
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003443 return new ICmpInst(ICmp.getPredicate(), LHSCIOp, Res1);
Chris Lattner2188e402010-01-04 07:37:31 +00003444
3445 // The other three cases all fold into an unsigned comparison.
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003446 return new ICmpInst(ICmp.getUnsignedPredicate(), LHSCIOp, Res1);
Chris Lattner2188e402010-01-04 07:37:31 +00003447 }
3448
Sanjay Patel6a333c32016-06-06 16:56:57 +00003449 // The re-extended constant changed, partly changed (in the case of a vector),
3450 // or could not be determined to be equal (in the case of a constant
3451 // expression), so the constant cannot be represented in the shorter type.
3452 // Consequently, we cannot emit a simple comparison.
Duncan Sands8fb2c382011-01-20 13:21:55 +00003453 // All the cases that fold to true or false will have already been handled
3454 // by SimplifyICmpInst, so only deal with the tricky case.
Chris Lattner2188e402010-01-04 07:37:31 +00003455
Sanjay Patel6a333c32016-06-06 16:56:57 +00003456 if (isSignedCmp || !isSignedExt || !isa<ConstantInt>(C))
Craig Topperf40110f2014-04-25 05:29:35 +00003457 return nullptr;
Chris Lattner2188e402010-01-04 07:37:31 +00003458
3459 // Evaluate the comparison for LT (we invert for GT below). LE and GE cases
3460 // should have been folded away previously and not enter in here.
Duncan Sands8fb2c382011-01-20 13:21:55 +00003461
3462 // We're performing an unsigned comp with a sign extended value.
3463 // This is true if the input is >= 0. [aka >s -1]
3464 Constant *NegOne = Constant::getAllOnesValue(SrcTy);
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003465 Value *Result = Builder->CreateICmpSGT(LHSCIOp, NegOne, ICmp.getName());
Chris Lattner2188e402010-01-04 07:37:31 +00003466
3467 // Finally, return the value computed.
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003468 if (ICmp.getPredicate() == ICmpInst::ICMP_ULT)
3469 return replaceInstUsesWith(ICmp, Result);
Chris Lattner2188e402010-01-04 07:37:31 +00003470
Sanjay Patel6f8f47b2016-06-05 00:12:32 +00003471 assert(ICmp.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");
Chris Lattner2188e402010-01-04 07:37:31 +00003472 return BinaryOperator::CreateNot(Result);
3473}
3474
Sanjoy Dasb0984472015-04-08 04:27:22 +00003475bool InstCombiner::OptimizeOverflowCheck(OverflowCheckFlavor OCF, Value *LHS,
3476 Value *RHS, Instruction &OrigI,
3477 Value *&Result, Constant *&Overflow) {
Sanjoy Das827529e2015-08-11 21:33:55 +00003478 if (OrigI.isCommutative() && isa<Constant>(LHS) && !isa<Constant>(RHS))
3479 std::swap(LHS, RHS);
Sanjoy Dasb0984472015-04-08 04:27:22 +00003480
3481 auto SetResult = [&](Value *OpResult, Constant *OverflowVal, bool ReuseName) {
3482 Result = OpResult;
3483 Overflow = OverflowVal;
3484 if (ReuseName)
3485 Result->takeName(&OrigI);
3486 return true;
3487 };
3488
Sanjoy Das6f5dca72015-08-28 19:09:31 +00003489 // If the overflow check was an add followed by a compare, the insertion point
3490 // may be pointing to the compare. We want to insert the new instructions
3491 // before the add in case there are uses of the add between the add and the
3492 // compare.
3493 Builder->SetInsertPoint(&OrigI);
3494
Sanjoy Dasb0984472015-04-08 04:27:22 +00003495 switch (OCF) {
3496 case OCF_INVALID:
3497 llvm_unreachable("bad overflow check kind!");
3498
3499 case OCF_UNSIGNED_ADD: {
3500 OverflowResult OR = computeOverflowForUnsignedAdd(LHS, RHS, &OrigI);
3501 if (OR == OverflowResult::NeverOverflows)
3502 return SetResult(Builder->CreateNUWAdd(LHS, RHS), Builder->getFalse(),
3503 true);
3504
3505 if (OR == OverflowResult::AlwaysOverflows)
3506 return SetResult(Builder->CreateAdd(LHS, RHS), Builder->getTrue(), true);
Justin Bognercd1d5aa2016-08-17 20:30:52 +00003507
3508 // Fall through uadd into sadd
3509 LLVM_FALLTHROUGH;
Sanjoy Dasb0984472015-04-08 04:27:22 +00003510 }
Sanjoy Dasb0984472015-04-08 04:27:22 +00003511 case OCF_SIGNED_ADD: {
David Majnemer27e89ba2015-05-21 23:04:21 +00003512 // X + 0 -> {X, false}
3513 if (match(RHS, m_Zero()))
3514 return SetResult(LHS, Builder->getFalse(), false);
Sanjoy Dasb0984472015-04-08 04:27:22 +00003515
3516 // We can strength reduce this signed add into a regular add if we can prove
3517 // that it will never overflow.
3518 if (OCF == OCF_SIGNED_ADD)
Craig Topper2b1fc322017-05-22 06:25:31 +00003519 if (willNotOverflowSignedAdd(LHS, RHS, OrigI))
Sanjoy Dasb0984472015-04-08 04:27:22 +00003520 return SetResult(Builder->CreateNSWAdd(LHS, RHS), Builder->getFalse(),
3521 true);
Sanjoy Das72cb5e12015-06-05 18:04:42 +00003522 break;
Sanjoy Dasb0984472015-04-08 04:27:22 +00003523 }
3524
3525 case OCF_UNSIGNED_SUB:
3526 case OCF_SIGNED_SUB: {
David Majnemer27e89ba2015-05-21 23:04:21 +00003527 // X - 0 -> {X, false}
3528 if (match(RHS, m_Zero()))
3529 return SetResult(LHS, Builder->getFalse(), false);
Sanjoy Dasb0984472015-04-08 04:27:22 +00003530
3531 if (OCF == OCF_SIGNED_SUB) {
Craig Topper2b1fc322017-05-22 06:25:31 +00003532 if (willNotOverflowSignedSub(LHS, RHS, OrigI))
Sanjoy Dasb0984472015-04-08 04:27:22 +00003533 return SetResult(Builder->CreateNSWSub(LHS, RHS), Builder->getFalse(),
3534 true);
3535 } else {
Craig Topper2b1fc322017-05-22 06:25:31 +00003536 if (willNotOverflowUnsignedSub(LHS, RHS, OrigI))
Sanjoy Dasb0984472015-04-08 04:27:22 +00003537 return SetResult(Builder->CreateNUWSub(LHS, RHS), Builder->getFalse(),
3538 true);
3539 }
3540 break;
3541 }
3542
3543 case OCF_UNSIGNED_MUL: {
3544 OverflowResult OR = computeOverflowForUnsignedMul(LHS, RHS, &OrigI);
3545 if (OR == OverflowResult::NeverOverflows)
3546 return SetResult(Builder->CreateNUWMul(LHS, RHS), Builder->getFalse(),
3547 true);
3548 if (OR == OverflowResult::AlwaysOverflows)
3549 return SetResult(Builder->CreateMul(LHS, RHS), Builder->getTrue(), true);
Justin Bognercd1d5aa2016-08-17 20:30:52 +00003550 LLVM_FALLTHROUGH;
3551 }
Sanjoy Dasb0984472015-04-08 04:27:22 +00003552 case OCF_SIGNED_MUL:
3553 // X * undef -> undef
3554 if (isa<UndefValue>(RHS))
David Majnemer27e89ba2015-05-21 23:04:21 +00003555 return SetResult(RHS, UndefValue::get(Builder->getInt1Ty()), false);
Sanjoy Dasb0984472015-04-08 04:27:22 +00003556
David Majnemer27e89ba2015-05-21 23:04:21 +00003557 // X * 0 -> {0, false}
3558 if (match(RHS, m_Zero()))
3559 return SetResult(RHS, Builder->getFalse(), false);
Sanjoy Dasb0984472015-04-08 04:27:22 +00003560
David Majnemer27e89ba2015-05-21 23:04:21 +00003561 // X * 1 -> {X, false}
3562 if (match(RHS, m_One()))
3563 return SetResult(LHS, Builder->getFalse(), false);
Sanjoy Dasb0984472015-04-08 04:27:22 +00003564
3565 if (OCF == OCF_SIGNED_MUL)
Craig Topper2b1fc322017-05-22 06:25:31 +00003566 if (willNotOverflowSignedMul(LHS, RHS, OrigI))
Sanjoy Dasb0984472015-04-08 04:27:22 +00003567 return SetResult(Builder->CreateNSWMul(LHS, RHS), Builder->getFalse(),
3568 true);
Sanjoy Dasc80dad62015-06-05 18:04:46 +00003569 break;
Sanjoy Dasb0984472015-04-08 04:27:22 +00003570 }
3571
3572 return false;
3573}
3574
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003575/// \brief Recognize and process idiom involving test for multiplication
3576/// overflow.
3577///
3578/// The caller has matched a pattern of the form:
3579/// I = cmp u (mul(zext A, zext B), V
3580/// The function checks if this is a test for overflow and if so replaces
3581/// multiplication with call to 'mul.with.overflow' intrinsic.
3582///
3583/// \param I Compare instruction.
3584/// \param MulVal Result of 'mult' instruction. It is one of the arguments of
3585/// the compare instruction. Must be of integer type.
3586/// \param OtherVal The other argument of compare instruction.
3587/// \returns Instruction which must replace the compare instruction, NULL if no
3588/// replacement required.
Sanjay Pateld93c4c02016-09-15 18:22:25 +00003589static Instruction *processUMulZExtIdiom(ICmpInst &I, Value *MulVal,
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003590 Value *OtherVal, InstCombiner &IC) {
Benjamin Kramerc96a7f82014-06-24 10:47:52 +00003591 // Don't bother doing this transformation for pointers, don't do it for
3592 // vectors.
3593 if (!isa<IntegerType>(MulVal->getType()))
3594 return nullptr;
3595
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003596 assert(I.getOperand(0) == MulVal || I.getOperand(1) == MulVal);
3597 assert(I.getOperand(0) == OtherVal || I.getOperand(1) == OtherVal);
David Majnemerdaa24b92015-09-05 20:44:56 +00003598 auto *MulInstr = dyn_cast<Instruction>(MulVal);
3599 if (!MulInstr)
3600 return nullptr;
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003601 assert(MulInstr->getOpcode() == Instruction::Mul);
3602
David Majnemer634ca232014-11-01 23:46:05 +00003603 auto *LHS = cast<ZExtOperator>(MulInstr->getOperand(0)),
3604 *RHS = cast<ZExtOperator>(MulInstr->getOperand(1));
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003605 assert(LHS->getOpcode() == Instruction::ZExt);
3606 assert(RHS->getOpcode() == Instruction::ZExt);
3607 Value *A = LHS->getOperand(0), *B = RHS->getOperand(0);
3608
3609 // Calculate type and width of the result produced by mul.with.overflow.
3610 Type *TyA = A->getType(), *TyB = B->getType();
3611 unsigned WidthA = TyA->getPrimitiveSizeInBits(),
3612 WidthB = TyB->getPrimitiveSizeInBits();
3613 unsigned MulWidth;
3614 Type *MulType;
3615 if (WidthB > WidthA) {
3616 MulWidth = WidthB;
3617 MulType = TyB;
3618 } else {
3619 MulWidth = WidthA;
3620 MulType = TyA;
3621 }
3622
3623 // In order to replace the original mul with a narrower mul.with.overflow,
3624 // all uses must ignore upper bits of the product. The number of used low
3625 // bits must be not greater than the width of mul.with.overflow.
3626 if (MulVal->hasNUsesOrMore(2))
3627 for (User *U : MulVal->users()) {
3628 if (U == &I)
3629 continue;
3630 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
3631 // Check if truncation ignores bits above MulWidth.
3632 unsigned TruncWidth = TI->getType()->getPrimitiveSizeInBits();
3633 if (TruncWidth > MulWidth)
Craig Topperf40110f2014-04-25 05:29:35 +00003634 return nullptr;
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003635 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
3636 // Check if AND ignores bits above MulWidth.
3637 if (BO->getOpcode() != Instruction::And)
Craig Topperf40110f2014-04-25 05:29:35 +00003638 return nullptr;
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003639 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
3640 const APInt &CVal = CI->getValue();
3641 if (CVal.getBitWidth() - CVal.countLeadingZeros() > MulWidth)
Craig Topperf40110f2014-04-25 05:29:35 +00003642 return nullptr;
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003643 }
3644 } else {
3645 // Other uses prohibit this transformation.
Craig Topperf40110f2014-04-25 05:29:35 +00003646 return nullptr;
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003647 }
3648 }
3649
3650 // Recognize patterns
3651 switch (I.getPredicate()) {
3652 case ICmpInst::ICMP_EQ:
3653 case ICmpInst::ICMP_NE:
3654 // Recognize pattern:
3655 // mulval = mul(zext A, zext B)
3656 // cmp eq/neq mulval, zext trunc mulval
3657 if (ZExtInst *Zext = dyn_cast<ZExtInst>(OtherVal))
3658 if (Zext->hasOneUse()) {
3659 Value *ZextArg = Zext->getOperand(0);
3660 if (TruncInst *Trunc = dyn_cast<TruncInst>(ZextArg))
3661 if (Trunc->getType()->getPrimitiveSizeInBits() == MulWidth)
3662 break; //Recognized
3663 }
3664
3665 // Recognize pattern:
3666 // mulval = mul(zext A, zext B)
3667 // cmp eq/neq mulval, and(mulval, mask), mask selects low MulWidth bits.
3668 ConstantInt *CI;
3669 Value *ValToMask;
3670 if (match(OtherVal, m_And(m_Value(ValToMask), m_ConstantInt(CI)))) {
3671 if (ValToMask != MulVal)
Craig Topperf40110f2014-04-25 05:29:35 +00003672 return nullptr;
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003673 const APInt &CVal = CI->getValue() + 1;
3674 if (CVal.isPowerOf2()) {
3675 unsigned MaskWidth = CVal.logBase2();
3676 if (MaskWidth == MulWidth)
3677 break; // Recognized
3678 }
3679 }
Craig Topperf40110f2014-04-25 05:29:35 +00003680 return nullptr;
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003681
3682 case ICmpInst::ICMP_UGT:
3683 // Recognize pattern:
3684 // mulval = mul(zext A, zext B)
3685 // cmp ugt mulval, max
3686 if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
3687 APInt MaxVal = APInt::getMaxValue(MulWidth);
3688 MaxVal = MaxVal.zext(CI->getBitWidth());
3689 if (MaxVal.eq(CI->getValue()))
3690 break; // Recognized
3691 }
Craig Topperf40110f2014-04-25 05:29:35 +00003692 return nullptr;
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003693
3694 case ICmpInst::ICMP_UGE:
3695 // Recognize pattern:
3696 // mulval = mul(zext A, zext B)
3697 // cmp uge mulval, max+1
3698 if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
3699 APInt MaxVal = APInt::getOneBitSet(CI->getBitWidth(), MulWidth);
3700 if (MaxVal.eq(CI->getValue()))
3701 break; // Recognized
3702 }
Craig Topperf40110f2014-04-25 05:29:35 +00003703 return nullptr;
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003704
3705 case ICmpInst::ICMP_ULE:
3706 // Recognize pattern:
3707 // mulval = mul(zext A, zext B)
3708 // cmp ule mulval, max
3709 if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
3710 APInt MaxVal = APInt::getMaxValue(MulWidth);
3711 MaxVal = MaxVal.zext(CI->getBitWidth());
3712 if (MaxVal.eq(CI->getValue()))
3713 break; // Recognized
3714 }
Craig Topperf40110f2014-04-25 05:29:35 +00003715 return nullptr;
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003716
3717 case ICmpInst::ICMP_ULT:
3718 // Recognize pattern:
3719 // mulval = mul(zext A, zext B)
3720 // cmp ule mulval, max + 1
3721 if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
Serge Pavlovb5f3ddc2014-04-14 02:20:19 +00003722 APInt MaxVal = APInt::getOneBitSet(CI->getBitWidth(), MulWidth);
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003723 if (MaxVal.eq(CI->getValue()))
3724 break; // Recognized
3725 }
Craig Topperf40110f2014-04-25 05:29:35 +00003726 return nullptr;
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003727
3728 default:
Craig Topperf40110f2014-04-25 05:29:35 +00003729 return nullptr;
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003730 }
3731
3732 InstCombiner::BuilderTy *Builder = IC.Builder;
3733 Builder->SetInsertPoint(MulInstr);
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003734
3735 // Replace: mul(zext A, zext B) --> mul.with.overflow(A, B)
3736 Value *MulA = A, *MulB = B;
3737 if (WidthA < MulWidth)
3738 MulA = Builder->CreateZExt(A, MulType);
3739 if (WidthB < MulWidth)
3740 MulB = Builder->CreateZExt(B, MulType);
Sanjay Patelaf674fb2015-12-14 17:24:23 +00003741 Value *F = Intrinsic::getDeclaration(I.getModule(),
3742 Intrinsic::umul_with_overflow, MulType);
David Blaikieff6409d2015-05-18 22:13:54 +00003743 CallInst *Call = Builder->CreateCall(F, {MulA, MulB}, "umul");
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003744 IC.Worklist.Add(MulInstr);
3745
3746 // If there are uses of mul result other than the comparison, we know that
3747 // they are truncation or binary AND. Change them to use result of
Serge Pavlovb5f3ddc2014-04-14 02:20:19 +00003748 // mul.with.overflow and adjust properly mask/size.
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003749 if (MulVal->hasNUsesOrMore(2)) {
3750 Value *Mul = Builder->CreateExtractValue(Call, 0, "umul.value");
3751 for (User *U : MulVal->users()) {
3752 if (U == &I || U == OtherVal)
3753 continue;
3754 if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
3755 if (TI->getType()->getPrimitiveSizeInBits() == MulWidth)
Sanjay Patel4b198802016-02-01 22:23:39 +00003756 IC.replaceInstUsesWith(*TI, Mul);
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003757 else
3758 TI->setOperand(0, Mul);
3759 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
3760 assert(BO->getOpcode() == Instruction::And);
3761 // Replace (mul & mask) --> zext (mul.with.overflow & short_mask)
3762 ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
3763 APInt ShortMask = CI->getValue().trunc(MulWidth);
3764 Value *ShortAnd = Builder->CreateAnd(Mul, ShortMask);
3765 Instruction *Zext =
3766 cast<Instruction>(Builder->CreateZExt(ShortAnd, BO->getType()));
3767 IC.Worklist.Add(Zext);
Sanjay Patel4b198802016-02-01 22:23:39 +00003768 IC.replaceInstUsesWith(*BO, Zext);
Serge Pavlov4bb54d52014-04-13 18:23:41 +00003769 } else {
3770 llvm_unreachable("Unexpected Binary operation");
3771 }
3772 IC.Worklist.Add(cast<Instruction>(U));
3773 }
3774 }
3775 if (isa<Instruction>(OtherVal))
3776 IC.Worklist.Add(cast<Instruction>(OtherVal));
3777
3778 // The original icmp gets replaced with the overflow value, maybe inverted
3779 // depending on predicate.
3780 bool Inverse = false;
3781 switch (I.getPredicate()) {
3782 case ICmpInst::ICMP_NE:
3783 break;
3784 case ICmpInst::ICMP_EQ:
3785 Inverse = true;
3786 break;
3787 case ICmpInst::ICMP_UGT:
3788 case ICmpInst::ICMP_UGE:
3789 if (I.getOperand(0) == MulVal)
3790 break;
3791 Inverse = true;
3792 break;
3793 case ICmpInst::ICMP_ULT:
3794 case ICmpInst::ICMP_ULE:
3795 if (I.getOperand(1) == MulVal)
3796 break;
3797 Inverse = true;
3798 break;
3799 default:
3800 llvm_unreachable("Unexpected predicate");
3801 }
3802 if (Inverse) {
3803 Value *Res = Builder->CreateExtractValue(Call, 1);
3804 return BinaryOperator::CreateNot(Res);
3805 }
3806
3807 return ExtractValueInst::Create(Call, 1);
3808}
3809
Sanjay Patel5f0217f2016-06-05 16:46:18 +00003810/// When performing a comparison against a constant, it is possible that not all
3811/// the bits in the LHS are demanded. This helper method computes the mask that
3812/// IS demanded.
Sanjay Pateld93c4c02016-09-15 18:22:25 +00003813static APInt getDemandedBitsLHSMask(ICmpInst &I, unsigned BitWidth,
3814 bool isSignCheck) {
Owen Andersond490c2d2011-01-11 00:36:45 +00003815 if (isSignCheck)
Craig Topperbcfd2d12017-04-20 16:56:25 +00003816 return APInt::getSignMask(BitWidth);
Jim Grosbach129c52a2011-09-30 18:09:53 +00003817
Owen Andersond490c2d2011-01-11 00:36:45 +00003818 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1));
3819 if (!CI) return APInt::getAllOnesValue(BitWidth);
Owen Anderson0022a4b2011-01-11 18:26:37 +00003820 const APInt &RHS = CI->getValue();
Jim Grosbach129c52a2011-09-30 18:09:53 +00003821
Owen Andersond490c2d2011-01-11 00:36:45 +00003822 switch (I.getPredicate()) {
Jim Grosbach129c52a2011-09-30 18:09:53 +00003823 // For a UGT comparison, we don't care about any bits that
Owen Andersond490c2d2011-01-11 00:36:45 +00003824 // correspond to the trailing ones of the comparand. The value of these
3825 // bits doesn't impact the outcome of the comparison, because any value
3826 // greater than the RHS must differ in a bit higher than these due to carry.
3827 case ICmpInst::ICMP_UGT: {
3828 unsigned trailingOnes = RHS.countTrailingOnes();
Craig Toppere7563f82017-04-13 21:49:48 +00003829 return APInt::getBitsSetFrom(BitWidth, trailingOnes);
Owen Andersond490c2d2011-01-11 00:36:45 +00003830 }
Jim Grosbach129c52a2011-09-30 18:09:53 +00003831
Owen Andersond490c2d2011-01-11 00:36:45 +00003832 // Similarly, for a ULT comparison, we don't care about the trailing zeros.
3833 // Any value less than the RHS must differ in a higher bit because of carries.
3834 case ICmpInst::ICMP_ULT: {
3835 unsigned trailingZeros = RHS.countTrailingZeros();
Craig Toppere7563f82017-04-13 21:49:48 +00003836 return APInt::getBitsSetFrom(BitWidth, trailingZeros);
Owen Andersond490c2d2011-01-11 00:36:45 +00003837 }
Jim Grosbach129c52a2011-09-30 18:09:53 +00003838
Owen Andersond490c2d2011-01-11 00:36:45 +00003839 default:
3840 return APInt::getAllOnesValue(BitWidth);
3841 }
Owen Andersond490c2d2011-01-11 00:36:45 +00003842}
Chris Lattner2188e402010-01-04 07:37:31 +00003843
Quentin Colombet5ab55552013-09-09 20:56:48 +00003844/// \brief Check if the order of \p Op0 and \p Op1 as operand in an ICmpInst
3845/// should be swapped.
Alp Tokercb402912014-01-24 17:20:08 +00003846/// The decision is based on how many times these two operands are reused
Quentin Colombet5ab55552013-09-09 20:56:48 +00003847/// as subtract operands and their positions in those instructions.
3848/// The rational is that several architectures use the same instruction for
3849/// both subtract and cmp, thus it is better if the order of those operands
3850/// match.
3851/// \return true if Op0 and Op1 should be swapped.
3852static bool swapMayExposeCSEOpportunities(const Value * Op0,
3853 const Value * Op1) {
3854 // Filter out pointer value as those cannot appears directly in subtract.
3855 // FIXME: we may want to go through inttoptrs or bitcasts.
3856 if (Op0->getType()->isPointerTy())
3857 return false;
3858 // Count every uses of both Op0 and Op1 in a subtract.
3859 // Each time Op0 is the first operand, count -1: swapping is bad, the
3860 // subtract has already the same layout as the compare.
3861 // Each time Op0 is the second operand, count +1: swapping is good, the
Alp Tokercb402912014-01-24 17:20:08 +00003862 // subtract has a different layout as the compare.
Quentin Colombet5ab55552013-09-09 20:56:48 +00003863 // At the end, if the benefit is greater than 0, Op0 should come second to
3864 // expose more CSE opportunities.
3865 int GlobalSwapBenefits = 0;
Chandler Carruthcdf47882014-03-09 03:16:01 +00003866 for (const User *U : Op0->users()) {
3867 const BinaryOperator *BinOp = dyn_cast<BinaryOperator>(U);
Quentin Colombet5ab55552013-09-09 20:56:48 +00003868 if (!BinOp || BinOp->getOpcode() != Instruction::Sub)
3869 continue;
3870 // If Op0 is the first argument, this is not beneficial to swap the
3871 // arguments.
3872 int LocalSwapBenefits = -1;
3873 unsigned Op1Idx = 1;
3874 if (BinOp->getOperand(Op1Idx) == Op0) {
3875 Op1Idx = 0;
3876 LocalSwapBenefits = 1;
3877 }
3878 if (BinOp->getOperand(Op1Idx) != Op1)
3879 continue;
3880 GlobalSwapBenefits += LocalSwapBenefits;
3881 }
3882 return GlobalSwapBenefits > 0;
3883}
3884
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +00003885/// \brief Check that one use is in the same block as the definition and all
Sanjay Patel53523312016-09-12 14:25:46 +00003886/// other uses are in blocks dominated by a given block.
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +00003887///
3888/// \param DI Definition
3889/// \param UI Use
3890/// \param DB Block that must dominate all uses of \p DI outside
3891/// the parent block
3892/// \return true when \p UI is the only use of \p DI in the parent block
3893/// and all other uses of \p DI are in blocks dominated by \p DB.
3894///
3895bool InstCombiner::dominatesAllUses(const Instruction *DI,
3896 const Instruction *UI,
3897 const BasicBlock *DB) const {
3898 assert(DI && UI && "Instruction not defined\n");
Sanjay Patel53523312016-09-12 14:25:46 +00003899 // Ignore incomplete definitions.
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +00003900 if (!DI->getParent())
3901 return false;
Sanjay Patel53523312016-09-12 14:25:46 +00003902 // DI and UI must be in the same block.
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +00003903 if (DI->getParent() != UI->getParent())
3904 return false;
Sanjay Patel53523312016-09-12 14:25:46 +00003905 // Protect from self-referencing blocks.
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +00003906 if (DI->getParent() == DB)
3907 return false;
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +00003908 for (const User *U : DI->users()) {
3909 auto *Usr = cast<Instruction>(U);
Justin Bogner99798402016-08-05 01:06:44 +00003910 if (Usr != UI && !DT.dominates(DB, Usr->getParent()))
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +00003911 return false;
3912 }
3913 return true;
3914}
3915
Sanjay Patel5f0217f2016-06-05 16:46:18 +00003916/// Return true when the instruction sequence within a block is select-cmp-br.
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +00003917static bool isChainSelectCmpBranch(const SelectInst *SI) {
3918 const BasicBlock *BB = SI->getParent();
3919 if (!BB)
3920 return false;
3921 auto *BI = dyn_cast_or_null<BranchInst>(BB->getTerminator());
3922 if (!BI || BI->getNumSuccessors() != 2)
3923 return false;
3924 auto *IC = dyn_cast<ICmpInst>(BI->getCondition());
3925 if (!IC || (IC->getOperand(0) != SI && IC->getOperand(1) != SI))
3926 return false;
3927 return true;
3928}
3929
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +00003930/// \brief True when a select result is replaced by one of its operands
3931/// in select-icmp sequence. This will eventually result in the elimination
3932/// of the select.
3933///
3934/// \param SI Select instruction
3935/// \param Icmp Compare instruction
3936/// \param SIOpd Operand that replaces the select
3937///
3938/// Notes:
3939/// - The replacement is global and requires dominator information
3940/// - The caller is responsible for the actual replacement
3941///
3942/// Example:
3943///
3944/// entry:
3945/// %4 = select i1 %3, %C* %0, %C* null
3946/// %5 = icmp eq %C* %4, null
3947/// br i1 %5, label %9, label %7
3948/// ...
3949/// ; <label>:7 ; preds = %entry
3950/// %8 = getelementptr inbounds %C* %4, i64 0, i32 0
3951/// ...
3952///
3953/// can be transformed to
3954///
3955/// %5 = icmp eq %C* %0, null
3956/// %6 = select i1 %3, i1 %5, i1 true
3957/// br i1 %6, label %9, label %7
3958/// ...
3959/// ; <label>:7 ; preds = %entry
3960/// %8 = getelementptr inbounds %C* %0, i64 0, i32 0 // replace by %0!
3961///
3962/// Similar when the first operand of the select is a constant or/and
3963/// the compare is for not equal rather than equal.
3964///
3965/// NOTE: The function is only called when the select and compare constants
3966/// are equal, the optimization can work only for EQ predicates. This is not a
3967/// major restriction since a NE compare should be 'normalized' to an equal
3968/// compare, which usually happens in the combiner and test case
Sanjay Patel53523312016-09-12 14:25:46 +00003969/// select-cmp-br.ll checks for it.
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +00003970bool InstCombiner::replacedSelectWithOperand(SelectInst *SI,
3971 const ICmpInst *Icmp,
3972 const unsigned SIOpd) {
David Majnemer83484fd2014-11-22 06:09:28 +00003973 assert((SIOpd == 1 || SIOpd == 2) && "Invalid select operand!");
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +00003974 if (isChainSelectCmpBranch(SI) && Icmp->getPredicate() == ICmpInst::ICMP_EQ) {
3975 BasicBlock *Succ = SI->getParent()->getTerminator()->getSuccessor(1);
Bjorn Petterssone5027cf2017-03-02 15:18:58 +00003976 // The check for the single predecessor is not the best that can be
Sanjay Patel53523312016-09-12 14:25:46 +00003977 // done. But it protects efficiently against cases like when SI's
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +00003978 // home block has two successors, Succ and Succ1, and Succ1 predecessor
3979 // of Succ. Then SI can't be replaced by SIOpd because the use that gets
3980 // replaced can be reached on either path. So the uniqueness check
3981 // guarantees that the path all uses of SI (outside SI's parent) are on
3982 // is disjoint from all other paths out of SI. But that information
3983 // is more expensive to compute, and the trade-off here is in favor
Bjorn Petterssone5027cf2017-03-02 15:18:58 +00003984 // of compile-time. It should also be noticed that we check for a single
3985 // predecessor and not only uniqueness. This to handle the situation when
3986 // Succ and Succ1 points to the same basic block.
3987 if (Succ->getSinglePredecessor() && dominatesAllUses(SI, Icmp, Succ)) {
Gerolf Hoflehnerec6217c2014-11-21 23:36:44 +00003988 NumSel++;
3989 SI->replaceUsesOutsideBlock(SI->getOperand(SIOpd), SI->getParent());
3990 return true;
3991 }
3992 }
3993 return false;
3994}
3995
Sanjay Patel3151dec2016-09-12 15:24:31 +00003996/// Try to fold the comparison based on range information we can get by checking
3997/// whether bits are known to be zero or one in the inputs.
3998Instruction *InstCombiner::foldICmpUsingKnownBits(ICmpInst &I) {
3999 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
4000 Type *Ty = Op0->getType();
Sanjay Patel0531f0a2016-09-12 15:52:28 +00004001 ICmpInst::Predicate Pred = I.getPredicate();
Sanjay Patel3151dec2016-09-12 15:24:31 +00004002
4003 // Get scalar or pointer size.
4004 unsigned BitWidth = Ty->isIntOrIntVectorTy()
4005 ? Ty->getScalarSizeInBits()
4006 : DL.getTypeSizeInBits(Ty->getScalarType());
4007
4008 if (!BitWidth)
4009 return nullptr;
4010
4011 // If this is a normal comparison, it demands all bits. If it is a sign bit
4012 // comparison, it only demands the sign bit.
4013 bool IsSignBit = false;
Sanjay Patelf5887f12016-09-12 16:25:41 +00004014 const APInt *CmpC;
4015 if (match(Op1, m_APInt(CmpC))) {
Sanjay Patel3151dec2016-09-12 15:24:31 +00004016 bool UnusedBit;
Sanjay Patelf5887f12016-09-12 16:25:41 +00004017 IsSignBit = isSignBitCheck(Pred, *CmpC, UnusedBit);
Sanjay Patel3151dec2016-09-12 15:24:31 +00004018 }
4019
Craig Topperb45eabc2017-04-26 16:39:58 +00004020 KnownBits Op0Known(BitWidth);
4021 KnownBits Op1Known(BitWidth);
Sanjay Patel3151dec2016-09-12 15:24:31 +00004022
Craig Topper47596dd2017-03-25 06:52:52 +00004023 if (SimplifyDemandedBits(&I, 0,
Sanjay Pateld93c4c02016-09-15 18:22:25 +00004024 getDemandedBitsLHSMask(I, BitWidth, IsSignBit),
Craig Topperb45eabc2017-04-26 16:39:58 +00004025 Op0Known, 0))
Sanjay Patel3151dec2016-09-12 15:24:31 +00004026 return &I;
4027
Craig Topper47596dd2017-03-25 06:52:52 +00004028 if (SimplifyDemandedBits(&I, 1, APInt::getAllOnesValue(BitWidth),
Craig Topperb45eabc2017-04-26 16:39:58 +00004029 Op1Known, 0))
Sanjay Patel3151dec2016-09-12 15:24:31 +00004030 return &I;
4031
4032 // Given the known and unknown bits, compute a range that the LHS could be
4033 // in. Compute the Min, Max and RHS values based on the known bits. For the
4034 // EQ and NE we use unsigned values.
4035 APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
4036 APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
4037 if (I.isSigned()) {
Craig Topperb45eabc2017-04-26 16:39:58 +00004038 computeSignedMinMaxValuesFromKnownBits(Op0Known, Op0Min, Op0Max);
4039 computeSignedMinMaxValuesFromKnownBits(Op1Known, Op1Min, Op1Max);
Sanjay Patel3151dec2016-09-12 15:24:31 +00004040 } else {
Craig Topperb45eabc2017-04-26 16:39:58 +00004041 computeUnsignedMinMaxValuesFromKnownBits(Op0Known, Op0Min, Op0Max);
4042 computeUnsignedMinMaxValuesFromKnownBits(Op1Known, Op1Min, Op1Max);
Sanjay Patel3151dec2016-09-12 15:24:31 +00004043 }
4044
4045 // If Min and Max are known to be the same, then SimplifyDemandedBits
Sanjay Patel0531f0a2016-09-12 15:52:28 +00004046 // figured out that the LHS is a constant. Constant fold this now, so that
4047 // code below can assume that Min != Max.
Sanjay Patel3151dec2016-09-12 15:24:31 +00004048 if (!isa<Constant>(Op0) && Op0Min == Op0Max)
Sanjay Patel0531f0a2016-09-12 15:52:28 +00004049 return new ICmpInst(Pred, ConstantInt::get(Op0->getType(), Op0Min), Op1);
Sanjay Patel3151dec2016-09-12 15:24:31 +00004050 if (!isa<Constant>(Op1) && Op1Min == Op1Max)
Sanjay Patel0531f0a2016-09-12 15:52:28 +00004051 return new ICmpInst(Pred, Op0, ConstantInt::get(Op1->getType(), Op1Min));
Sanjay Patel3151dec2016-09-12 15:24:31 +00004052
4053 // Based on the range information we know about the LHS, see if we can
4054 // simplify this comparison. For example, (x&4) < 8 is always true.
Sanjay Patel0531f0a2016-09-12 15:52:28 +00004055 switch (Pred) {
Sanjay Patel3151dec2016-09-12 15:24:31 +00004056 default:
4057 llvm_unreachable("Unknown icmp opcode!");
Sanjay Patel9efb1bd2016-09-14 23:38:56 +00004058 case ICmpInst::ICMP_EQ:
Sanjay Patel3151dec2016-09-12 15:24:31 +00004059 case ICmpInst::ICMP_NE: {
Sanjay Patel9efb1bd2016-09-14 23:38:56 +00004060 if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max)) {
4061 return Pred == CmpInst::ICMP_EQ
4062 ? replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()))
4063 : replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
4064 }
Sanjay Patel3151dec2016-09-12 15:24:31 +00004065
Sanjay Patel0531f0a2016-09-12 15:52:28 +00004066 // If all bits are known zero except for one, then we know at most one bit
4067 // is set. If the comparison is against zero, then this is a check to see if
4068 // *that* bit is set.
Craig Topperb45eabc2017-04-26 16:39:58 +00004069 APInt Op0KnownZeroInverted = ~Op0Known.Zero;
Craig Topperf0aeee02017-05-05 17:36:09 +00004070 if (Op1Known.isZero()) {
Sanjay Patel3151dec2016-09-12 15:24:31 +00004071 // If the LHS is an AND with the same constant, look through it.
4072 Value *LHS = nullptr;
Sanjay Patel7577a3d2016-09-15 14:15:47 +00004073 const APInt *LHSC;
4074 if (!match(Op0, m_And(m_Value(LHS), m_APInt(LHSC))) ||
4075 *LHSC != Op0KnownZeroInverted)
Sanjay Patel3151dec2016-09-12 15:24:31 +00004076 LHS = Op0;
4077
Sanjay Patel9efb1bd2016-09-14 23:38:56 +00004078 Value *X;
Sanjay Patel3151dec2016-09-12 15:24:31 +00004079 if (match(LHS, m_Shl(m_One(), m_Value(X)))) {
4080 APInt ValToCheck = Op0KnownZeroInverted;
Sanjay Patel9efb1bd2016-09-14 23:38:56 +00004081 Type *XTy = X->getType();
Sanjay Patel3151dec2016-09-12 15:24:31 +00004082 if (ValToCheck.isPowerOf2()) {
Sanjay Patel9efb1bd2016-09-14 23:38:56 +00004083 // ((1 << X) & 8) == 0 -> X != 3
4084 // ((1 << X) & 8) != 0 -> X == 3
4085 auto *CmpC = ConstantInt::get(XTy, ValToCheck.countTrailingZeros());
4086 auto NewPred = ICmpInst::getInversePredicate(Pred);
4087 return new ICmpInst(NewPred, X, CmpC);
Sanjay Patel3151dec2016-09-12 15:24:31 +00004088 } else if ((++ValToCheck).isPowerOf2()) {
Sanjay Patel9efb1bd2016-09-14 23:38:56 +00004089 // ((1 << X) & 7) == 0 -> X >= 3
4090 // ((1 << X) & 7) != 0 -> X < 3
4091 auto *CmpC = ConstantInt::get(XTy, ValToCheck.countTrailingZeros());
4092 auto NewPred =
4093 Pred == CmpInst::ICMP_EQ ? CmpInst::ICMP_UGE : CmpInst::ICMP_ULT;
4094 return new ICmpInst(NewPred, X, CmpC);
Sanjay Patel3151dec2016-09-12 15:24:31 +00004095 }
4096 }
4097
Sanjay Patel9efb1bd2016-09-14 23:38:56 +00004098 // Check if the LHS is 8 >>u x and the result is a power of 2 like 1.
Sanjay Patel3151dec2016-09-12 15:24:31 +00004099 const APInt *CI;
Craig Topper73ba1c82017-06-07 07:40:37 +00004100 if (Op0KnownZeroInverted.isOneValue() &&
Sanjay Patel9efb1bd2016-09-14 23:38:56 +00004101 match(LHS, m_LShr(m_Power2(CI), m_Value(X)))) {
4102 // ((8 >>u X) & 1) == 0 -> X != 3
4103 // ((8 >>u X) & 1) != 0 -> X == 3
4104 unsigned CmpVal = CI->countTrailingZeros();
4105 auto NewPred = ICmpInst::getInversePredicate(Pred);
4106 return new ICmpInst(NewPred, X, ConstantInt::get(X->getType(), CmpVal));
4107 }
Sanjay Patel3151dec2016-09-12 15:24:31 +00004108 }
4109 break;
4110 }
4111 case ICmpInst::ICMP_ULT: {
4112 if (Op0Max.ult(Op1Min)) // A <u B -> true if max(A) < min(B)
4113 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
4114 if (Op0Min.uge(Op1Max)) // A <u B -> false if min(A) >= max(B)
4115 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
4116 if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)
4117 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4118
4119 const APInt *CmpC;
4120 if (match(Op1, m_APInt(CmpC))) {
4121 // A <u C -> A == C-1 if min(A)+1 == C
4122 if (Op1Max == Op0Min + 1) {
4123 Constant *CMinus1 = ConstantInt::get(Op0->getType(), *CmpC - 1);
4124 return new ICmpInst(ICmpInst::ICMP_EQ, Op0, CMinus1);
4125 }
Sanjay Patel3151dec2016-09-12 15:24:31 +00004126 }
4127 break;
4128 }
4129 case ICmpInst::ICMP_UGT: {
4130 if (Op0Min.ugt(Op1Max)) // A >u B -> true if min(A) > max(B)
4131 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
4132
4133 if (Op0Max.ule(Op1Min)) // A >u B -> false if max(A) <= max(B)
4134 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
4135
4136 if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
4137 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4138
4139 const APInt *CmpC;
4140 if (match(Op1, m_APInt(CmpC))) {
4141 // A >u C -> A == C+1 if max(a)-1 == C
4142 if (*CmpC == Op0Max - 1)
4143 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
4144 ConstantInt::get(Op1->getType(), *CmpC + 1));
Sanjay Patel3151dec2016-09-12 15:24:31 +00004145 }
4146 break;
4147 }
4148 case ICmpInst::ICMP_SLT:
4149 if (Op0Max.slt(Op1Min)) // A <s B -> true if max(A) < min(C)
4150 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
4151 if (Op0Min.sge(Op1Max)) // A <s B -> false if min(A) >= max(C)
4152 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
4153 if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)
4154 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4155 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
4156 if (Op1Max == Op0Min + 1) // A <s C -> A == C-1 if min(A)+1 == C
4157 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
4158 Builder->getInt(CI->getValue() - 1));
4159 }
4160 break;
4161 case ICmpInst::ICMP_SGT:
4162 if (Op0Min.sgt(Op1Max)) // A >s B -> true if min(A) > max(B)
4163 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
4164 if (Op0Max.sle(Op1Min)) // A >s B -> false if max(A) <= min(B)
4165 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
4166
4167 if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
4168 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4169 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
4170 if (Op1Min == Op0Max - 1) // A >s C -> A == C+1 if max(A)-1 == C
4171 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
4172 Builder->getInt(CI->getValue() + 1));
4173 }
4174 break;
4175 case ICmpInst::ICMP_SGE:
4176 assert(!isa<ConstantInt>(Op1) && "ICMP_SGE with ConstantInt not folded!");
4177 if (Op0Min.sge(Op1Max)) // A >=s B -> true if min(A) >= max(B)
4178 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
4179 if (Op0Max.slt(Op1Min)) // A >=s B -> false if max(A) < min(B)
4180 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
4181 break;
4182 case ICmpInst::ICMP_SLE:
4183 assert(!isa<ConstantInt>(Op1) && "ICMP_SLE with ConstantInt not folded!");
4184 if (Op0Max.sle(Op1Min)) // A <=s B -> true if max(A) <= min(B)
4185 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
4186 if (Op0Min.sgt(Op1Max)) // A <=s B -> false if min(A) > max(B)
4187 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
4188 break;
4189 case ICmpInst::ICMP_UGE:
4190 assert(!isa<ConstantInt>(Op1) && "ICMP_UGE with ConstantInt not folded!");
4191 if (Op0Min.uge(Op1Max)) // A >=u B -> true if min(A) >= max(B)
4192 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
4193 if (Op0Max.ult(Op1Min)) // A >=u B -> false if max(A) < min(B)
4194 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
4195 break;
4196 case ICmpInst::ICMP_ULE:
4197 assert(!isa<ConstantInt>(Op1) && "ICMP_ULE with ConstantInt not folded!");
4198 if (Op0Max.ule(Op1Min)) // A <=u B -> true if max(A) <= min(B)
4199 return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
4200 if (Op0Min.ugt(Op1Max)) // A <=u B -> false if min(A) > max(B)
4201 return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
4202 break;
4203 }
4204
4205 // Turn a signed comparison into an unsigned one if both operands are known to
4206 // have the same sign.
4207 if (I.isSigned() &&
Craig Topperb45eabc2017-04-26 16:39:58 +00004208 ((Op0Known.Zero.isNegative() && Op1Known.Zero.isNegative()) ||
4209 (Op0Known.One.isNegative() && Op1Known.One.isNegative())))
Sanjay Patel3151dec2016-09-12 15:24:31 +00004210 return new ICmpInst(I.getUnsignedPredicate(), Op0, Op1);
4211
4212 return nullptr;
4213}
4214
Sanjay Pateld5b0e542016-04-29 16:22:25 +00004215/// If we have an icmp le or icmp ge instruction with a constant operand, turn
4216/// it into the appropriate icmp lt or icmp gt instruction. This transform
4217/// allows them to be folded in visitICmpInst.
Sanjay Patele9b2c322016-05-17 00:57:57 +00004218static ICmpInst *canonicalizeCmpWithConstant(ICmpInst &I) {
4219 ICmpInst::Predicate Pred = I.getPredicate();
4220 if (Pred != ICmpInst::ICMP_SLE && Pred != ICmpInst::ICMP_SGE &&
4221 Pred != ICmpInst::ICMP_ULE && Pred != ICmpInst::ICMP_UGE)
4222 return nullptr;
4223
Sanjay Pateld5b0e542016-04-29 16:22:25 +00004224 Value *Op0 = I.getOperand(0);
4225 Value *Op1 = I.getOperand(1);
Sanjay Patele9b2c322016-05-17 00:57:57 +00004226 auto *Op1C = dyn_cast<Constant>(Op1);
4227 if (!Op1C)
4228 return nullptr;
Sanjay Pateld5b0e542016-04-29 16:22:25 +00004229
Sanjay Patele9b2c322016-05-17 00:57:57 +00004230 // Check if the constant operand can be safely incremented/decremented without
4231 // overflowing/underflowing. For scalars, SimplifyICmpInst has already handled
4232 // the edge cases for us, so we just assert on them. For vectors, we must
4233 // handle the edge cases.
4234 Type *Op1Type = Op1->getType();
4235 bool IsSigned = I.isSigned();
4236 bool IsLE = (Pred == ICmpInst::ICMP_SLE || Pred == ICmpInst::ICMP_ULE);
Sanjay Patel18254932016-05-17 01:12:31 +00004237 auto *CI = dyn_cast<ConstantInt>(Op1C);
4238 if (CI) {
Sanjay Patele9b2c322016-05-17 00:57:57 +00004239 // A <= MAX -> TRUE ; A >= MIN -> TRUE
4240 assert(IsLE ? !CI->isMaxValue(IsSigned) : !CI->isMinValue(IsSigned));
4241 } else if (Op1Type->isVectorTy()) {
Sanjay Patelb79ab272016-05-13 15:10:46 +00004242 // TODO? If the edge cases for vectors were guaranteed to be handled as they
Sanjay Patele9b2c322016-05-17 00:57:57 +00004243 // are for scalar, we could remove the min/max checks. However, to do that,
4244 // we would have to use insertelement/shufflevector to replace edge values.
4245 unsigned NumElts = Op1Type->getVectorNumElements();
4246 for (unsigned i = 0; i != NumElts; ++i) {
4247 Constant *Elt = Op1C->getAggregateElement(i);
Benjamin Kramerca9a0fe2016-05-17 12:08:55 +00004248 if (!Elt)
4249 return nullptr;
4250
Sanjay Patele9b2c322016-05-17 00:57:57 +00004251 if (isa<UndefValue>(Elt))
4252 continue;
Sanjay Patel06b127a2016-09-15 14:37:50 +00004253
Sanjay Patele9b2c322016-05-17 00:57:57 +00004254 // Bail out if we can't determine if this constant is min/max or if we
4255 // know that this constant is min/max.
4256 auto *CI = dyn_cast<ConstantInt>(Elt);
4257 if (!CI || (IsLE ? CI->isMaxValue(IsSigned) : CI->isMinValue(IsSigned)))
4258 return nullptr;
Sanjay Patelb79ab272016-05-13 15:10:46 +00004259 }
Sanjay Patele9b2c322016-05-17 00:57:57 +00004260 } else {
4261 // ConstantExpr?
4262 return nullptr;
Sanjay Patelb79ab272016-05-13 15:10:46 +00004263 }
Sanjay Pateld5b0e542016-04-29 16:22:25 +00004264
Sanjay Patele9b2c322016-05-17 00:57:57 +00004265 // Increment or decrement the constant and set the new comparison predicate:
4266 // ULE -> ULT ; UGE -> UGT ; SLE -> SLT ; SGE -> SGT
Sanjay Patel22b01fe2016-05-17 20:20:40 +00004267 Constant *OneOrNegOne = ConstantInt::get(Op1Type, IsLE ? 1 : -1, true);
Sanjay Patele9b2c322016-05-17 00:57:57 +00004268 CmpInst::Predicate NewPred = IsLE ? ICmpInst::ICMP_ULT: ICmpInst::ICMP_UGT;
4269 NewPred = IsSigned ? ICmpInst::getSignedPredicate(NewPred) : NewPred;
4270 return new ICmpInst(NewPred, Op0, ConstantExpr::getAdd(Op1C, OneOrNegOne));
Sanjay Pateld5b0e542016-04-29 16:22:25 +00004271}
4272
Sanjay Patele5747e32017-05-17 22:15:07 +00004273/// Integer compare with boolean values can always be turned into bitwise ops.
4274static Instruction *canonicalizeICmpBool(ICmpInst &I,
4275 InstCombiner::BuilderTy &Builder) {
4276 Value *A = I.getOperand(0), *B = I.getOperand(1);
4277 assert(A->getType()->getScalarType()->isIntegerTy(1) && "Bools only");
4278
Sanjay Patelba212c22017-05-17 22:29:40 +00004279 // A boolean compared to true/false can be simplified to Op0/true/false in
4280 // 14 out of the 20 (10 predicates * 2 constants) possible combinations.
4281 // Cases not handled by InstSimplify are always 'not' of Op0.
4282 if (match(B, m_Zero())) {
4283 switch (I.getPredicate()) {
4284 case CmpInst::ICMP_EQ: // A == 0 -> !A
4285 case CmpInst::ICMP_ULE: // A <=u 0 -> !A
4286 case CmpInst::ICMP_SGE: // A >=s 0 -> !A
4287 return BinaryOperator::CreateNot(A);
4288 default:
4289 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
4290 }
4291 } else if (match(B, m_One())) {
4292 switch (I.getPredicate()) {
4293 case CmpInst::ICMP_NE: // A != 1 -> !A
4294 case CmpInst::ICMP_ULT: // A <u 1 -> !A
4295 case CmpInst::ICMP_SGT: // A >s -1 -> !A
4296 return BinaryOperator::CreateNot(A);
4297 default:
4298 llvm_unreachable("ICmp i1 X, C not simplified as expected.");
4299 }
4300 }
4301
Sanjay Patele5747e32017-05-17 22:15:07 +00004302 switch (I.getPredicate()) {
4303 default:
4304 llvm_unreachable("Invalid icmp instruction!");
4305 case ICmpInst::ICMP_EQ:
4306 // icmp eq i1 A, B -> ~(A ^ B)
4307 return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
4308
4309 case ICmpInst::ICMP_NE:
4310 // icmp ne i1 A, B -> A ^ B
4311 return BinaryOperator::CreateXor(A, B);
4312
4313 case ICmpInst::ICMP_UGT:
4314 // icmp ugt -> icmp ult
4315 std::swap(A, B);
4316 LLVM_FALLTHROUGH;
4317 case ICmpInst::ICMP_ULT:
4318 // icmp ult i1 A, B -> ~A & B
4319 return BinaryOperator::CreateAnd(Builder.CreateNot(A), B);
4320
4321 case ICmpInst::ICMP_SGT:
4322 // icmp sgt -> icmp slt
4323 std::swap(A, B);
4324 LLVM_FALLTHROUGH;
4325 case ICmpInst::ICMP_SLT:
4326 // icmp slt i1 A, B -> A & ~B
4327 return BinaryOperator::CreateAnd(Builder.CreateNot(B), A);
4328
4329 case ICmpInst::ICMP_UGE:
4330 // icmp uge -> icmp ule
4331 std::swap(A, B);
4332 LLVM_FALLTHROUGH;
4333 case ICmpInst::ICMP_ULE:
4334 // icmp ule i1 A, B -> ~A | B
4335 return BinaryOperator::CreateOr(Builder.CreateNot(A), B);
4336
4337 case ICmpInst::ICMP_SGE:
4338 // icmp sge -> icmp sle
4339 std::swap(A, B);
4340 LLVM_FALLTHROUGH;
4341 case ICmpInst::ICMP_SLE:
4342 // icmp sle i1 A, B -> A | ~B
4343 return BinaryOperator::CreateOr(Builder.CreateNot(B), A);
4344 }
4345}
4346
Chris Lattner2188e402010-01-04 07:37:31 +00004347Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
4348 bool Changed = false;
Chris Lattner9306ffa2010-02-01 19:54:45 +00004349 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Quentin Colombet5ab55552013-09-09 20:56:48 +00004350 unsigned Op0Cplxity = getComplexity(Op0);
4351 unsigned Op1Cplxity = getComplexity(Op1);
Jim Grosbach129c52a2011-09-30 18:09:53 +00004352
Chris Lattner2188e402010-01-04 07:37:31 +00004353 /// Orders the operands of the compare so that they are listed from most
4354 /// complex to least complex. This puts constants before unary operators,
4355 /// before binary operators.
Quentin Colombet5ab55552013-09-09 20:56:48 +00004356 if (Op0Cplxity < Op1Cplxity ||
Sanjay Patel4c204232016-06-04 20:39:22 +00004357 (Op0Cplxity == Op1Cplxity && swapMayExposeCSEOpportunities(Op0, Op1))) {
Chris Lattner2188e402010-01-04 07:37:31 +00004358 I.swapOperands();
Chris Lattner9306ffa2010-02-01 19:54:45 +00004359 std::swap(Op0, Op1);
Chris Lattner2188e402010-01-04 07:37:31 +00004360 Changed = true;
4361 }
Jim Grosbach129c52a2011-09-30 18:09:53 +00004362
Daniel Berlin2c75c632017-04-26 20:56:07 +00004363 if (Value *V = SimplifyICmpInst(I.getPredicate(), Op0, Op1,
4364 SQ.getWithInstruction(&I)))
Sanjay Patel4b198802016-02-01 22:23:39 +00004365 return replaceInstUsesWith(I, V);
Jim Grosbach129c52a2011-09-30 18:09:53 +00004366
Pete Cooperbc5c5242011-12-01 03:58:40 +00004367 // comparing -val or val with non-zero is the same as just comparing val
Pete Cooperfdddc272011-12-01 19:13:26 +00004368 // ie, abs(val) != 0 -> val != 0
Sanjay Patel4c204232016-06-04 20:39:22 +00004369 if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) {
Pete Cooperfdddc272011-12-01 19:13:26 +00004370 Value *Cond, *SelectTrue, *SelectFalse;
4371 if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue),
Pete Cooperbc5c5242011-12-01 03:58:40 +00004372 m_Value(SelectFalse)))) {
Pete Cooperfdddc272011-12-01 19:13:26 +00004373 if (Value *V = dyn_castNegVal(SelectTrue)) {
4374 if (V == SelectFalse)
4375 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
4376 }
4377 else if (Value *V = dyn_castNegVal(SelectFalse)) {
4378 if (V == SelectTrue)
4379 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
Pete Cooperbc5c5242011-12-01 03:58:40 +00004380 }
4381 }
4382 }
4383
Sanjay Patele5747e32017-05-17 22:15:07 +00004384 if (Op0->getType()->getScalarType()->isIntegerTy(1))
4385 if (Instruction *Res = canonicalizeICmpBool(I, *Builder))
4386 return Res;
Chris Lattner2188e402010-01-04 07:37:31 +00004387
Sanjay Patele9b2c322016-05-17 00:57:57 +00004388 if (ICmpInst *NewICmp = canonicalizeCmpWithConstant(I))
Sanjay Pateld5b0e542016-04-29 16:22:25 +00004389 return NewICmp;
4390
Sanjay Patel06b127a2016-09-15 14:37:50 +00004391 if (Instruction *Res = foldICmpWithConstant(I))
4392 return Res;
Chris Lattner2188e402010-01-04 07:37:31 +00004393
Sanjay Patel3151dec2016-09-12 15:24:31 +00004394 if (Instruction *Res = foldICmpUsingKnownBits(I))
4395 return Res;
Chris Lattner2188e402010-01-04 07:37:31 +00004396
4397 // Test if the ICmpInst instruction is used exclusively by a select as
4398 // part of a minimum or maximum operation. If so, refrain from doing
4399 // any other folding. This helps out other analyses which understand
4400 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
4401 // and CodeGen. And in this case, at least one of the comparison
4402 // operands has at least one user besides the compare (the select),
4403 // which would often largely negate the benefit of folding anyway.
4404 if (I.hasOneUse())
Chandler Carruthcdf47882014-03-09 03:16:01 +00004405 if (SelectInst *SI = dyn_cast<SelectInst>(*I.user_begin()))
Chris Lattner2188e402010-01-04 07:37:31 +00004406 if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) ||
4407 (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1))
Craig Topperf40110f2014-04-25 05:29:35 +00004408 return nullptr;
Chris Lattner2188e402010-01-04 07:37:31 +00004409
Sanjay Patelfebcb9c2017-01-27 23:26:27 +00004410 // FIXME: We only do this after checking for min/max to prevent infinite
4411 // looping caused by a reverse canonicalization of these patterns for min/max.
4412 // FIXME: The organization of folds is a mess. These would naturally go into
4413 // canonicalizeCmpWithConstant(), but we can't move all of the above folds
4414 // down here after the min/max restriction.
4415 ICmpInst::Predicate Pred = I.getPredicate();
4416 const APInt *C;
4417 if (match(Op1, m_APInt(C))) {
4418 // For i32: x >u 2147483647 -> x <s 0 -> true if sign bit set
4419 if (Pred == ICmpInst::ICMP_UGT && C->isMaxSignedValue()) {
4420 Constant *Zero = Constant::getNullValue(Op0->getType());
4421 return new ICmpInst(ICmpInst::ICMP_SLT, Op0, Zero);
4422 }
4423
4424 // For i32: x <u 2147483648 -> x >s -1 -> true if sign bit clear
4425 if (Pred == ICmpInst::ICMP_ULT && C->isMinSignedValue()) {
4426 Constant *AllOnes = Constant::getAllOnesValue(Op0->getType());
4427 return new ICmpInst(ICmpInst::ICMP_SGT, Op0, AllOnes);
4428 }
4429 }
4430
Sanjay Patelf58f68c2016-09-10 15:03:44 +00004431 if (Instruction *Res = foldICmpInstWithConstant(I))
Sanjay Patel1271bf92016-07-23 13:06:49 +00004432 return Res;
4433
Sanjay Patel10494b22016-09-16 16:10:22 +00004434 if (Instruction *Res = foldICmpInstWithConstantNotInt(I))
4435 return Res;
Chris Lattner2188e402010-01-04 07:37:31 +00004436
4437 // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now.
4438 if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op0))
Sanjay Patel43395062016-07-21 18:07:40 +00004439 if (Instruction *NI = foldGEPICmp(GEP, Op1, I.getPredicate(), I))
Chris Lattner2188e402010-01-04 07:37:31 +00004440 return NI;
4441 if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op1))
Sanjay Patel43395062016-07-21 18:07:40 +00004442 if (Instruction *NI = foldGEPICmp(GEP, Op0,
Chris Lattner2188e402010-01-04 07:37:31 +00004443 ICmpInst::getSwappedPredicate(I.getPredicate()), I))
4444 return NI;
4445
Hans Wennborgf1f36512015-10-07 00:20:07 +00004446 // Try to optimize equality comparisons against alloca-based pointers.
4447 if (Op0->getType()->isPointerTy() && I.isEquality()) {
4448 assert(Op1->getType()->isPointerTy() && "Comparing pointer with non-pointer?");
4449 if (auto *Alloca = dyn_cast<AllocaInst>(GetUnderlyingObject(Op0, DL)))
Sanjay Patel43395062016-07-21 18:07:40 +00004450 if (Instruction *New = foldAllocaCmp(I, Alloca, Op1))
Hans Wennborgf1f36512015-10-07 00:20:07 +00004451 return New;
4452 if (auto *Alloca = dyn_cast<AllocaInst>(GetUnderlyingObject(Op1, DL)))
Sanjay Patel43395062016-07-21 18:07:40 +00004453 if (Instruction *New = foldAllocaCmp(I, Alloca, Op0))
Hans Wennborgf1f36512015-10-07 00:20:07 +00004454 return New;
4455 }
4456
Chris Lattner2188e402010-01-04 07:37:31 +00004457 // Test to see if the operands of the icmp are casted versions of other
4458 // values. If the ptr->ptr cast can be stripped off both arguments, we do so
4459 // now.
4460 if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) {
Jim Grosbach129c52a2011-09-30 18:09:53 +00004461 if (Op0->getType()->isPointerTy() &&
4462 (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) {
Chris Lattner2188e402010-01-04 07:37:31 +00004463 // We keep moving the cast from the left operand over to the right
4464 // operand, where it can often be eliminated completely.
4465 Op0 = CI->getOperand(0);
4466
4467 // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast
4468 // so eliminate it as well.
4469 if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1))
4470 Op1 = CI2->getOperand(0);
4471
4472 // If Op1 is a constant, we can fold the cast into the constant.
4473 if (Op0->getType() != Op1->getType()) {
4474 if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
4475 Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType());
4476 } else {
4477 // Otherwise, cast the RHS right before the icmp
4478 Op1 = Builder->CreateBitCast(Op1, Op0->getType());
4479 }
4480 }
4481 return new ICmpInst(I.getPredicate(), Op0, Op1);
4482 }
4483 }
Jim Grosbach129c52a2011-09-30 18:09:53 +00004484
Chris Lattner2188e402010-01-04 07:37:31 +00004485 if (isa<CastInst>(Op0)) {
4486 // Handle the special case of: icmp (cast bool to X), <cst>
4487 // This comes up when you have code like
4488 // int X = A < B;
4489 // if (X) ...
4490 // For generality, we handle any zero-extension of any operand comparison
4491 // with a constant or another cast from the same type.
4492 if (isa<Constant>(Op1) || isa<CastInst>(Op1))
Sanjay Patel43395062016-07-21 18:07:40 +00004493 if (Instruction *R = foldICmpWithCastAndCast(I))
Chris Lattner2188e402010-01-04 07:37:31 +00004494 return R;
4495 }
Chris Lattner2188e402010-01-04 07:37:31 +00004496
Sanjay Patel10494b22016-09-16 16:10:22 +00004497 if (Instruction *Res = foldICmpBinOp(I))
4498 return Res;
Duncan Sandse5220012011-02-17 07:46:37 +00004499
Sanjay Pateldd46b522016-12-19 17:32:37 +00004500 if (Instruction *Res = foldICmpWithMinMax(I))
Sanjay Pateld6406412016-12-15 19:13:37 +00004501 return Res;
4502
Sanjay Patel10494b22016-09-16 16:10:22 +00004503 {
4504 Value *A, *B;
David Majnemer1a08acc2013-04-12 17:25:07 +00004505 // Transform (A & ~B) == 0 --> (A & B) != 0
4506 // and (A & ~B) != 0 --> (A & B) == 0
4507 // if A is a power of 2.
4508 if (match(Op0, m_And(m_Value(A), m_Not(m_Value(B)))) &&
Chandler Carruth66b31302015-01-04 12:03:27 +00004509 match(Op1, m_Zero()) &&
Craig Topperd4039f72017-05-25 21:51:12 +00004510 isKnownToBeAPowerOfTwo(A, false, 0, &I) && I.isEquality())
David Majnemer1a08acc2013-04-12 17:25:07 +00004511 return new ICmpInst(I.getInversePredicate(),
4512 Builder->CreateAnd(A, B),
4513 Op1);
4514
Sanjay Patel4dc85eb2017-06-02 16:11:14 +00004515 // ~X < ~Y --> Y < X
4516 // ~X < C --> X > ~C
Chris Lattnerf3c4eef2011-01-15 05:41:33 +00004517 if (match(Op0, m_Not(m_Value(A)))) {
4518 if (match(Op1, m_Not(m_Value(B))))
4519 return new ICmpInst(I.getPredicate(), B, A);
Sanjay Patel4dc85eb2017-06-02 16:11:14 +00004520
Sanjay Patelce241f42017-06-02 16:29:41 +00004521 const APInt *C;
4522 if (match(Op1, m_APInt(C)))
Sanjay Patel4dc85eb2017-06-02 16:11:14 +00004523 return new ICmpInst(I.getSwappedPredicate(), A,
Sanjay Patelce241f42017-06-02 16:29:41 +00004524 ConstantInt::get(Op1->getType(), ~(*C)));
Chris Lattnerf3c4eef2011-01-15 05:41:33 +00004525 }
Chris Lattner5e0c0c72010-12-19 19:37:52 +00004526
Sanjoy Dasb6c59142015-04-10 21:07:09 +00004527 Instruction *AddI = nullptr;
4528 if (match(&I, m_UAddWithOverflow(m_Value(A), m_Value(B),
4529 m_Instruction(AddI))) &&
4530 isa<IntegerType>(A->getType())) {
4531 Value *Result;
4532 Constant *Overflow;
4533 if (OptimizeOverflowCheck(OCF_UNSIGNED_ADD, A, B, *AddI, Result,
4534 Overflow)) {
Sanjay Patel4b198802016-02-01 22:23:39 +00004535 replaceInstUsesWith(*AddI, Result);
4536 return replaceInstUsesWith(I, Overflow);
Sanjoy Dasb6c59142015-04-10 21:07:09 +00004537 }
4538 }
Serge Pavlov4bb54d52014-04-13 18:23:41 +00004539
4540 // (zext a) * (zext b) --> llvm.umul.with.overflow.
4541 if (match(Op0, m_Mul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) {
Sanjay Pateld93c4c02016-09-15 18:22:25 +00004542 if (Instruction *R = processUMulZExtIdiom(I, Op0, Op1, *this))
Serge Pavlov4bb54d52014-04-13 18:23:41 +00004543 return R;
4544 }
4545 if (match(Op1, m_Mul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) {
Sanjay Pateld93c4c02016-09-15 18:22:25 +00004546 if (Instruction *R = processUMulZExtIdiom(I, Op1, Op0, *this))
Serge Pavlov4bb54d52014-04-13 18:23:41 +00004547 return R;
4548 }
Chris Lattner2188e402010-01-04 07:37:31 +00004549 }
Jim Grosbach129c52a2011-09-30 18:09:53 +00004550
Sanjay Patel10494b22016-09-16 16:10:22 +00004551 if (Instruction *Res = foldICmpEquality(I))
4552 return Res;
Jim Grosbach129c52a2011-09-30 18:09:53 +00004553
David Majnemerc1eca5a2014-11-06 23:23:30 +00004554 // The 'cmpxchg' instruction returns an aggregate containing the old value and
4555 // an i1 which indicates whether or not we successfully did the swap.
4556 //
4557 // Replace comparisons between the old value and the expected value with the
4558 // indicator that 'cmpxchg' returns.
4559 //
4560 // N.B. This transform is only valid when the 'cmpxchg' is not permitted to
4561 // spuriously fail. In those cases, the old value may equal the expected
4562 // value but it is possible for the swap to not occur.
4563 if (I.getPredicate() == ICmpInst::ICMP_EQ)
4564 if (auto *EVI = dyn_cast<ExtractValueInst>(Op0))
4565 if (auto *ACXI = dyn_cast<AtomicCmpXchgInst>(EVI->getAggregateOperand()))
4566 if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 &&
4567 !ACXI->isWeak())
4568 return ExtractValueInst::Create(ACXI, 1);
4569
Chris Lattner2188e402010-01-04 07:37:31 +00004570 {
4571 Value *X; ConstantInt *Cst;
4572 // icmp X+Cst, X
4573 if (match(Op0, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op1 == X)
Sanjay Patel43395062016-07-21 18:07:40 +00004574 return foldICmpAddOpConst(I, X, Cst, I.getPredicate());
Chris Lattner2188e402010-01-04 07:37:31 +00004575
4576 // icmp X, X+Cst
4577 if (match(Op1, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op0 == X)
Sanjay Patel43395062016-07-21 18:07:40 +00004578 return foldICmpAddOpConst(I, X, Cst, I.getSwappedPredicate());
Chris Lattner2188e402010-01-04 07:37:31 +00004579 }
Craig Topperf40110f2014-04-25 05:29:35 +00004580 return Changed ? &I : nullptr;
Chris Lattner2188e402010-01-04 07:37:31 +00004581}
4582
Sanjay Patel5f0217f2016-06-05 16:46:18 +00004583/// Fold fcmp ([us]itofp x, cst) if possible.
Sanjay Patel43395062016-07-21 18:07:40 +00004584Instruction *InstCombiner::foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI,
Chris Lattner2188e402010-01-04 07:37:31 +00004585 Constant *RHSC) {
Craig Topperf40110f2014-04-25 05:29:35 +00004586 if (!isa<ConstantFP>(RHSC)) return nullptr;
Chris Lattner2188e402010-01-04 07:37:31 +00004587 const APFloat &RHS = cast<ConstantFP>(RHSC)->getValueAPF();
Jim Grosbach129c52a2011-09-30 18:09:53 +00004588
Chris Lattner2188e402010-01-04 07:37:31 +00004589 // Get the width of the mantissa. We don't want to hack on conversions that
4590 // might lose information from the integer, e.g. "i64 -> float"
4591 int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
Craig Topperf40110f2014-04-25 05:29:35 +00004592 if (MantissaWidth == -1) return nullptr; // Unknown.
Jim Grosbach129c52a2011-09-30 18:09:53 +00004593
Matt Arsenault55e73122015-01-06 15:50:59 +00004594 IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType());
4595
Chris Lattner2188e402010-01-04 07:37:31 +00004596 bool LHSUnsigned = isa<UIToFPInst>(LHSI);
Jim Grosbach129c52a2011-09-30 18:09:53 +00004597
Matt Arsenault55e73122015-01-06 15:50:59 +00004598 if (I.isEquality()) {
4599 FCmpInst::Predicate P = I.getPredicate();
4600 bool IsExact = false;
4601 APSInt RHSCvt(IntTy->getBitWidth(), LHSUnsigned);
4602 RHS.convertToInteger(RHSCvt, APFloat::rmNearestTiesToEven, &IsExact);
4603
4604 // If the floating point constant isn't an integer value, we know if we will
4605 // ever compare equal / not equal to it.
4606 if (!IsExact) {
4607 // TODO: Can never be -0.0 and other non-representable values
4608 APFloat RHSRoundInt(RHS);
4609 RHSRoundInt.roundToIntegral(APFloat::rmNearestTiesToEven);
4610 if (RHS.compare(RHSRoundInt) != APFloat::cmpEqual) {
4611 if (P == FCmpInst::FCMP_OEQ || P == FCmpInst::FCMP_UEQ)
Sanjay Patel4b198802016-02-01 22:23:39 +00004612 return replaceInstUsesWith(I, Builder->getFalse());
Matt Arsenault55e73122015-01-06 15:50:59 +00004613
4614 assert(P == FCmpInst::FCMP_ONE || P == FCmpInst::FCMP_UNE);
Sanjay Patel4b198802016-02-01 22:23:39 +00004615 return replaceInstUsesWith(I, Builder->getTrue());
Matt Arsenault55e73122015-01-06 15:50:59 +00004616 }
4617 }
4618
4619 // TODO: If the constant is exactly representable, is it always OK to do
4620 // equality compares as integer?
4621 }
4622
Arch D. Robison8ed08542015-09-15 17:51:59 +00004623 // Check to see that the input is converted from an integer type that is small
4624 // enough that preserves all bits. TODO: check here for "known" sign bits.
4625 // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
4626 unsigned InputSize = IntTy->getScalarSizeInBits();
Matt Arsenault55e73122015-01-06 15:50:59 +00004627
Justin Bognerc7e4fbe2016-08-05 01:09:48 +00004628 // Following test does NOT adjust InputSize downwards for signed inputs,
4629 // because the most negative value still requires all the mantissa bits
Arch D. Robison8ed08542015-09-15 17:51:59 +00004630 // to distinguish it from one less than that value.
4631 if ((int)InputSize > MantissaWidth) {
4632 // Conversion would lose accuracy. Check if loss can impact comparison.
4633 int Exp = ilogb(RHS);
4634 if (Exp == APFloat::IEK_Inf) {
4635 int MaxExponent = ilogb(APFloat::getLargest(RHS.getSemantics()));
Justin Bognerc7e4fbe2016-08-05 01:09:48 +00004636 if (MaxExponent < (int)InputSize - !LHSUnsigned)
Arch D. Robison8ed08542015-09-15 17:51:59 +00004637 // Conversion could create infinity.
4638 return nullptr;
4639 } else {
Justin Bognerc7e4fbe2016-08-05 01:09:48 +00004640 // Note that if RHS is zero or NaN, then Exp is negative
Arch D. Robison8ed08542015-09-15 17:51:59 +00004641 // and first condition is trivially false.
Justin Bognerc7e4fbe2016-08-05 01:09:48 +00004642 if (MantissaWidth <= Exp && Exp <= (int)InputSize - !LHSUnsigned)
Arch D. Robison8ed08542015-09-15 17:51:59 +00004643 // Conversion could affect comparison.
4644 return nullptr;
4645 }
4646 }
Jim Grosbach129c52a2011-09-30 18:09:53 +00004647
Chris Lattner2188e402010-01-04 07:37:31 +00004648 // Otherwise, we can potentially simplify the comparison. We know that it
4649 // will always come through as an integer value and we know the constant is
4650 // not a NAN (it would have been previously simplified).
4651 assert(!RHS.isNaN() && "NaN comparison not already folded!");
Jim Grosbach129c52a2011-09-30 18:09:53 +00004652
Chris Lattner2188e402010-01-04 07:37:31 +00004653 ICmpInst::Predicate Pred;
4654 switch (I.getPredicate()) {
4655 default: llvm_unreachable("Unexpected predicate!");
4656 case FCmpInst::FCMP_UEQ:
4657 case FCmpInst::FCMP_OEQ:
4658 Pred = ICmpInst::ICMP_EQ;
4659 break;
4660 case FCmpInst::FCMP_UGT:
4661 case FCmpInst::FCMP_OGT:
4662 Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
4663 break;
4664 case FCmpInst::FCMP_UGE:
4665 case FCmpInst::FCMP_OGE:
4666 Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
4667 break;
4668 case FCmpInst::FCMP_ULT:
4669 case FCmpInst::FCMP_OLT:
4670 Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
4671 break;
4672 case FCmpInst::FCMP_ULE:
4673 case FCmpInst::FCMP_OLE:
4674 Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
4675 break;
4676 case FCmpInst::FCMP_UNE:
4677 case FCmpInst::FCMP_ONE:
4678 Pred = ICmpInst::ICMP_NE;
4679 break;
4680 case FCmpInst::FCMP_ORD:
Sanjay Patel4b198802016-02-01 22:23:39 +00004681 return replaceInstUsesWith(I, Builder->getTrue());
Chris Lattner2188e402010-01-04 07:37:31 +00004682 case FCmpInst::FCMP_UNO:
Sanjay Patel4b198802016-02-01 22:23:39 +00004683 return replaceInstUsesWith(I, Builder->getFalse());
Chris Lattner2188e402010-01-04 07:37:31 +00004684 }
Jim Grosbach129c52a2011-09-30 18:09:53 +00004685
Chris Lattner2188e402010-01-04 07:37:31 +00004686 // Now we know that the APFloat is a normal number, zero or inf.
Jim Grosbach129c52a2011-09-30 18:09:53 +00004687
Chris Lattner2188e402010-01-04 07:37:31 +00004688 // See if the FP constant is too large for the integer. For example,
4689 // comparing an i8 to 300.0.
4690 unsigned IntWidth = IntTy->getScalarSizeInBits();
Jim Grosbach129c52a2011-09-30 18:09:53 +00004691
Chris Lattner2188e402010-01-04 07:37:31 +00004692 if (!LHSUnsigned) {
4693 // If the RHS value is > SignedMax, fold the comparison. This handles +INF
4694 // and large values.
Michael Gottesman79b09672013-06-27 21:58:19 +00004695 APFloat SMax(RHS.getSemantics());
Chris Lattner2188e402010-01-04 07:37:31 +00004696 SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true,
4697 APFloat::rmNearestTiesToEven);
4698 if (SMax.compare(RHS) == APFloat::cmpLessThan) { // smax < 13123.0
4699 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||
4700 Pred == ICmpInst::ICMP_SLE)
Sanjay Patel4b198802016-02-01 22:23:39 +00004701 return replaceInstUsesWith(I, Builder->getTrue());
4702 return replaceInstUsesWith(I, Builder->getFalse());
Chris Lattner2188e402010-01-04 07:37:31 +00004703 }
4704 } else {
4705 // If the RHS value is > UnsignedMax, fold the comparison. This handles
4706 // +INF and large values.
Michael Gottesman79b09672013-06-27 21:58:19 +00004707 APFloat UMax(RHS.getSemantics());
Chris Lattner2188e402010-01-04 07:37:31 +00004708 UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false,
4709 APFloat::rmNearestTiesToEven);
4710 if (UMax.compare(RHS) == APFloat::cmpLessThan) { // umax < 13123.0
4711 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||
4712 Pred == ICmpInst::ICMP_ULE)
Sanjay Patel4b198802016-02-01 22:23:39 +00004713 return replaceInstUsesWith(I, Builder->getTrue());
4714 return replaceInstUsesWith(I, Builder->getFalse());
Chris Lattner2188e402010-01-04 07:37:31 +00004715 }
4716 }
Jim Grosbach129c52a2011-09-30 18:09:53 +00004717
Chris Lattner2188e402010-01-04 07:37:31 +00004718 if (!LHSUnsigned) {
4719 // See if the RHS value is < SignedMin.
Michael Gottesman79b09672013-06-27 21:58:19 +00004720 APFloat SMin(RHS.getSemantics());
Chris Lattner2188e402010-01-04 07:37:31 +00004721 SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true,
4722 APFloat::rmNearestTiesToEven);
4723 if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0
4724 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
4725 Pred == ICmpInst::ICMP_SGE)
Sanjay Patel4b198802016-02-01 22:23:39 +00004726 return replaceInstUsesWith(I, Builder->getTrue());
4727 return replaceInstUsesWith(I, Builder->getFalse());
Chris Lattner2188e402010-01-04 07:37:31 +00004728 }
Devang Patel698452b2012-02-13 23:05:18 +00004729 } else {
4730 // See if the RHS value is < UnsignedMin.
Michael Gottesman79b09672013-06-27 21:58:19 +00004731 APFloat SMin(RHS.getSemantics());
Devang Patel698452b2012-02-13 23:05:18 +00004732 SMin.convertFromAPInt(APInt::getMinValue(IntWidth), true,
4733 APFloat::rmNearestTiesToEven);
4734 if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // umin > 12312.0
4735 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||
4736 Pred == ICmpInst::ICMP_UGE)
Sanjay Patel4b198802016-02-01 22:23:39 +00004737 return replaceInstUsesWith(I, Builder->getTrue());
4738 return replaceInstUsesWith(I, Builder->getFalse());
Devang Patel698452b2012-02-13 23:05:18 +00004739 }
Chris Lattner2188e402010-01-04 07:37:31 +00004740 }
4741
4742 // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
4743 // [0, UMAX], but it may still be fractional. See if it is fractional by
4744 // casting the FP value to the integer value and back, checking for equality.
4745 // Don't do this for zero, because -0.0 is not fractional.
4746 Constant *RHSInt = LHSUnsigned
4747 ? ConstantExpr::getFPToUI(RHSC, IntTy)
4748 : ConstantExpr::getFPToSI(RHSC, IntTy);
4749 if (!RHS.isZero()) {
4750 bool Equal = LHSUnsigned
4751 ? ConstantExpr::getUIToFP(RHSInt, RHSC->getType()) == RHSC
4752 : ConstantExpr::getSIToFP(RHSInt, RHSC->getType()) == RHSC;
4753 if (!Equal) {
4754 // If we had a comparison against a fractional value, we have to adjust
4755 // the compare predicate and sometimes the value. RHSC is rounded towards
4756 // zero at this point.
4757 switch (Pred) {
4758 default: llvm_unreachable("Unexpected integer comparison!");
4759 case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
Sanjay Patel4b198802016-02-01 22:23:39 +00004760 return replaceInstUsesWith(I, Builder->getTrue());
Chris Lattner2188e402010-01-04 07:37:31 +00004761 case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
Sanjay Patel4b198802016-02-01 22:23:39 +00004762 return replaceInstUsesWith(I, Builder->getFalse());
Chris Lattner2188e402010-01-04 07:37:31 +00004763 case ICmpInst::ICMP_ULE:
4764 // (float)int <= 4.4 --> int <= 4
4765 // (float)int <= -4.4 --> false
4766 if (RHS.isNegative())
Sanjay Patel4b198802016-02-01 22:23:39 +00004767 return replaceInstUsesWith(I, Builder->getFalse());
Chris Lattner2188e402010-01-04 07:37:31 +00004768 break;
4769 case ICmpInst::ICMP_SLE:
4770 // (float)int <= 4.4 --> int <= 4
4771 // (float)int <= -4.4 --> int < -4
4772 if (RHS.isNegative())
4773 Pred = ICmpInst::ICMP_SLT;
4774 break;
4775 case ICmpInst::ICMP_ULT:
4776 // (float)int < -4.4 --> false
4777 // (float)int < 4.4 --> int <= 4
4778 if (RHS.isNegative())
Sanjay Patel4b198802016-02-01 22:23:39 +00004779 return replaceInstUsesWith(I, Builder->getFalse());
Chris Lattner2188e402010-01-04 07:37:31 +00004780 Pred = ICmpInst::ICMP_ULE;
4781 break;
4782 case ICmpInst::ICMP_SLT:
4783 // (float)int < -4.4 --> int < -4
4784 // (float)int < 4.4 --> int <= 4
4785 if (!RHS.isNegative())
4786 Pred = ICmpInst::ICMP_SLE;
4787 break;
4788 case ICmpInst::ICMP_UGT:
4789 // (float)int > 4.4 --> int > 4
4790 // (float)int > -4.4 --> true
4791 if (RHS.isNegative())
Sanjay Patel4b198802016-02-01 22:23:39 +00004792 return replaceInstUsesWith(I, Builder->getTrue());
Chris Lattner2188e402010-01-04 07:37:31 +00004793 break;
4794 case ICmpInst::ICMP_SGT:
4795 // (float)int > 4.4 --> int > 4
4796 // (float)int > -4.4 --> int >= -4
4797 if (RHS.isNegative())
4798 Pred = ICmpInst::ICMP_SGE;
4799 break;
4800 case ICmpInst::ICMP_UGE:
4801 // (float)int >= -4.4 --> true
4802 // (float)int >= 4.4 --> int > 4
Bob Wilson61f3ad52012-08-07 22:35:16 +00004803 if (RHS.isNegative())
Sanjay Patel4b198802016-02-01 22:23:39 +00004804 return replaceInstUsesWith(I, Builder->getTrue());
Chris Lattner2188e402010-01-04 07:37:31 +00004805 Pred = ICmpInst::ICMP_UGT;
4806 break;
4807 case ICmpInst::ICMP_SGE:
4808 // (float)int >= -4.4 --> int >= -4
4809 // (float)int >= 4.4 --> int > 4
4810 if (!RHS.isNegative())
4811 Pred = ICmpInst::ICMP_SGT;
4812 break;
4813 }
4814 }
4815 }
4816
4817 // Lower this FP comparison into an appropriate integer version of the
4818 // comparison.
4819 return new ICmpInst(Pred, LHSI->getOperand(0), RHSInt);
4820}
4821
4822Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) {
4823 bool Changed = false;
Jim Grosbach129c52a2011-09-30 18:09:53 +00004824
Chris Lattner2188e402010-01-04 07:37:31 +00004825 /// Orders the operands of the compare so that they are listed from most
4826 /// complex to least complex. This puts constants before unary operators,
4827 /// before binary operators.
4828 if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) {
4829 I.swapOperands();
4830 Changed = true;
4831 }
4832
4833 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Jim Grosbach129c52a2011-09-30 18:09:53 +00004834
Daniel Berlin2c75c632017-04-26 20:56:07 +00004835 if (Value *V =
4836 SimplifyFCmpInst(I.getPredicate(), Op0, Op1, I.getFastMathFlags(),
4837 SQ.getWithInstruction(&I)))
Sanjay Patel4b198802016-02-01 22:23:39 +00004838 return replaceInstUsesWith(I, V);
Chris Lattner2188e402010-01-04 07:37:31 +00004839
4840 // Simplify 'fcmp pred X, X'
4841 if (Op0 == Op1) {
4842 switch (I.getPredicate()) {
4843 default: llvm_unreachable("Unknown predicate!");
4844 case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
4845 case FCmpInst::FCMP_ULT: // True if unordered or less than
4846 case FCmpInst::FCMP_UGT: // True if unordered or greater than
4847 case FCmpInst::FCMP_UNE: // True if unordered or not equal
4848 // Canonicalize these to be 'fcmp uno %X, 0.0'.
4849 I.setPredicate(FCmpInst::FCMP_UNO);
4850 I.setOperand(1, Constant::getNullValue(Op0->getType()));
4851 return &I;
Jim Grosbach129c52a2011-09-30 18:09:53 +00004852
Chris Lattner2188e402010-01-04 07:37:31 +00004853 case FCmpInst::FCMP_ORD: // True if ordered (no nans)
4854 case FCmpInst::FCMP_OEQ: // True if ordered and equal
4855 case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal
4856 case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
4857 // Canonicalize these to be 'fcmp ord %X, 0.0'.
4858 I.setPredicate(FCmpInst::FCMP_ORD);
4859 I.setOperand(1, Constant::getNullValue(Op0->getType()));
4860 return &I;
4861 }
4862 }
Jim Grosbach129c52a2011-09-30 18:09:53 +00004863
James Molloy2b21a7c2015-05-20 18:41:25 +00004864 // Test if the FCmpInst instruction is used exclusively by a select as
4865 // part of a minimum or maximum operation. If so, refrain from doing
4866 // any other folding. This helps out other analyses which understand
4867 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
4868 // and CodeGen. And in this case, at least one of the comparison
4869 // operands has at least one user besides the compare (the select),
4870 // which would often largely negate the benefit of folding anyway.
4871 if (I.hasOneUse())
4872 if (SelectInst *SI = dyn_cast<SelectInst>(*I.user_begin()))
4873 if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) ||
4874 (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1))
4875 return nullptr;
4876
Chris Lattner2188e402010-01-04 07:37:31 +00004877 // Handle fcmp with constant RHS
4878 if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
4879 if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
4880 switch (LHSI->getOpcode()) {
Benjamin Kramercbb18e92011-03-31 10:12:07 +00004881 case Instruction::FPExt: {
4882 // fcmp (fpext x), C -> fcmp x, (fptrunc C) if fptrunc is lossless
4883 FPExtInst *LHSExt = cast<FPExtInst>(LHSI);
4884 ConstantFP *RHSF = dyn_cast<ConstantFP>(RHSC);
4885 if (!RHSF)
4886 break;
4887
4888 const fltSemantics *Sem;
4889 // FIXME: This shouldn't be here.
Dan Gohman518cda42011-12-17 00:04:22 +00004890 if (LHSExt->getSrcTy()->isHalfTy())
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004891 Sem = &APFloat::IEEEhalf();
Dan Gohman518cda42011-12-17 00:04:22 +00004892 else if (LHSExt->getSrcTy()->isFloatTy())
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004893 Sem = &APFloat::IEEEsingle();
Benjamin Kramercbb18e92011-03-31 10:12:07 +00004894 else if (LHSExt->getSrcTy()->isDoubleTy())
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004895 Sem = &APFloat::IEEEdouble();
Benjamin Kramercbb18e92011-03-31 10:12:07 +00004896 else if (LHSExt->getSrcTy()->isFP128Ty())
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004897 Sem = &APFloat::IEEEquad();
Benjamin Kramercbb18e92011-03-31 10:12:07 +00004898 else if (LHSExt->getSrcTy()->isX86_FP80Ty())
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004899 Sem = &APFloat::x87DoubleExtended();
Ulrich Weigand6a9bb512012-10-30 12:33:18 +00004900 else if (LHSExt->getSrcTy()->isPPC_FP128Ty())
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004901 Sem = &APFloat::PPCDoubleDouble();
Benjamin Kramercbb18e92011-03-31 10:12:07 +00004902 else
4903 break;
4904
4905 bool Lossy;
4906 APFloat F = RHSF->getValueAPF();
4907 F.convert(*Sem, APFloat::rmNearestTiesToEven, &Lossy);
4908
Jim Grosbach24ff8342011-09-30 18:45:50 +00004909 // Avoid lossy conversions and denormals. Zero is a special case
4910 // that's OK to convert.
Jim Grosbach011dafb2011-09-30 19:58:46 +00004911 APFloat Fabs = F;
4912 Fabs.clearSign();
Benjamin Kramercbb18e92011-03-31 10:12:07 +00004913 if (!Lossy &&
Jim Grosbach011dafb2011-09-30 19:58:46 +00004914 ((Fabs.compare(APFloat::getSmallestNormalized(*Sem)) !=
4915 APFloat::cmpLessThan) || Fabs.isZero()))
Jim Grosbach24ff8342011-09-30 18:45:50 +00004916
Benjamin Kramercbb18e92011-03-31 10:12:07 +00004917 return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0),
4918 ConstantFP::get(RHSC->getContext(), F));
4919 break;
4920 }
Chris Lattner2188e402010-01-04 07:37:31 +00004921 case Instruction::PHI:
4922 // Only fold fcmp into the PHI if the phi and fcmp are in the same
4923 // block. If in the same block, we're encouraging jump threading. If
4924 // not, we are just pessimizing the code by making an i1 phi.
4925 if (LHSI->getParent() == I.getParent())
Craig Topperfb71b7d2017-04-14 19:20:12 +00004926 if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
Chris Lattner2188e402010-01-04 07:37:31 +00004927 return NV;
4928 break;
4929 case Instruction::SIToFP:
4930 case Instruction::UIToFP:
Sanjay Patel43395062016-07-21 18:07:40 +00004931 if (Instruction *NV = foldFCmpIntToFPConst(I, LHSI, RHSC))
Chris Lattner2188e402010-01-04 07:37:31 +00004932 return NV;
4933 break;
Benjamin Kramera8c5d082011-03-31 10:12:15 +00004934 case Instruction::FSub: {
4935 // fcmp pred (fneg x), C -> fcmp swap(pred) x, -C
4936 Value *Op;
4937 if (match(LHSI, m_FNeg(m_Value(Op))))
4938 return new FCmpInst(I.getSwappedPredicate(), Op,
4939 ConstantExpr::getFNeg(RHSC));
4940 break;
4941 }
Dan Gohman94732022010-02-24 06:46:09 +00004942 case Instruction::Load:
4943 if (GetElementPtrInst *GEP =
4944 dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) {
4945 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
4946 if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
4947 !cast<LoadInst>(LHSI)->isVolatile())
Sanjay Patel43395062016-07-21 18:07:40 +00004948 if (Instruction *Res = foldCmpLoadFromIndexedGlobal(GEP, GV, I))
Dan Gohman94732022010-02-24 06:46:09 +00004949 return Res;
4950 }
4951 break;
Benjamin Kramer8c2a7332012-08-18 20:06:47 +00004952 case Instruction::Call: {
Matt Arsenaultb935d9d2015-01-08 20:09:34 +00004953 if (!RHSC->isNullValue())
4954 break;
4955
Benjamin Kramer8c2a7332012-08-18 20:06:47 +00004956 CallInst *CI = cast<CallInst>(LHSI);
Justin Bogner99798402016-08-05 01:06:44 +00004957 Intrinsic::ID IID = getIntrinsicForCallSite(CI, &TLI);
David Majnemer2e02ba72016-04-15 17:21:03 +00004958 if (IID != Intrinsic::fabs)
Matt Arsenaultb935d9d2015-01-08 20:09:34 +00004959 break;
4960
Benjamin Kramer8c2a7332012-08-18 20:06:47 +00004961 // Various optimization for fabs compared with zero.
David Majnemer2e02ba72016-04-15 17:21:03 +00004962 switch (I.getPredicate()) {
4963 default:
4964 break;
4965 // fabs(x) < 0 --> false
4966 case FCmpInst::FCMP_OLT:
4967 llvm_unreachable("handled by SimplifyFCmpInst");
4968 // fabs(x) > 0 --> x != 0
4969 case FCmpInst::FCMP_OGT:
4970 return new FCmpInst(FCmpInst::FCMP_ONE, CI->getArgOperand(0), RHSC);
4971 // fabs(x) <= 0 --> x == 0
4972 case FCmpInst::FCMP_OLE:
4973 return new FCmpInst(FCmpInst::FCMP_OEQ, CI->getArgOperand(0), RHSC);
4974 // fabs(x) >= 0 --> !isnan(x)
4975 case FCmpInst::FCMP_OGE:
4976 return new FCmpInst(FCmpInst::FCMP_ORD, CI->getArgOperand(0), RHSC);
4977 // fabs(x) == 0 --> x == 0
4978 // fabs(x) != 0 --> x != 0
4979 case FCmpInst::FCMP_OEQ:
4980 case FCmpInst::FCMP_UEQ:
4981 case FCmpInst::FCMP_ONE:
4982 case FCmpInst::FCMP_UNE:
4983 return new FCmpInst(I.getPredicate(), CI->getArgOperand(0), RHSC);
Benjamin Kramer8c2a7332012-08-18 20:06:47 +00004984 }
4985 }
Chris Lattner2188e402010-01-04 07:37:31 +00004986 }
Chris Lattner2188e402010-01-04 07:37:31 +00004987 }
4988
Benjamin Kramerbe209ab2011-03-31 10:46:03 +00004989 // fcmp pred (fneg x), (fneg y) -> fcmp swap(pred) x, y
Benjamin Kramerd159d942011-03-31 10:12:22 +00004990 Value *X, *Y;
4991 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))
Benjamin Kramerbe209ab2011-03-31 10:46:03 +00004992 return new FCmpInst(I.getSwappedPredicate(), X, Y);
Benjamin Kramerd159d942011-03-31 10:12:22 +00004993
Benjamin Kramer2ccfbc82011-03-31 10:11:58 +00004994 // fcmp (fpext x), (fpext y) -> fcmp x, y
4995 if (FPExtInst *LHSExt = dyn_cast<FPExtInst>(Op0))
4996 if (FPExtInst *RHSExt = dyn_cast<FPExtInst>(Op1))
4997 if (LHSExt->getSrcTy() == RHSExt->getSrcTy())
4998 return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0),
4999 RHSExt->getOperand(0));
5000
Craig Topperf40110f2014-04-25 05:29:35 +00005001 return Changed ? &I : nullptr;
Chris Lattner2188e402010-01-04 07:37:31 +00005002}