blob: 24af2bfaf5275f262a990104b31103bcd87f5acb [file] [log] [blame]
Chris Lattner02446fc2010-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
14#include "InstCombine.h"
Eli Friedman74703252011-07-20 21:57:23 +000015#include "llvm/Analysis/ConstantFolding.h"
Chris Lattner02446fc2010-01-04 07:37:31 +000016#include "llvm/Analysis/InstructionSimplify.h"
17#include "llvm/Analysis/MemoryBuiltins.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000018#include "llvm/IR/DataLayout.h"
19#include "llvm/IR/IntrinsicInst.h"
Chris Lattner02446fc2010-01-04 07:37:31 +000020#include "llvm/Support/ConstantRange.h"
21#include "llvm/Support/GetElementPtrTypeIterator.h"
22#include "llvm/Support/PatternMatch.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/Target/TargetLibraryInfo.h"
Chris Lattner02446fc2010-01-04 07:37:31 +000024using namespace llvm;
25using namespace PatternMatch;
26
Chris Lattnerb20c0b52011-02-10 05:23:05 +000027static ConstantInt *getOne(Constant *C) {
28 return ConstantInt::get(cast<IntegerType>(C->getType()), 1);
29}
30
Chris Lattner02446fc2010-01-04 07:37:31 +000031/// AddOne - Add one to a ConstantInt
32static Constant *AddOne(Constant *C) {
33 return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
34}
35/// SubOne - Subtract one from a ConstantInt
Chris Lattnerb20c0b52011-02-10 05:23:05 +000036static Constant *SubOne(Constant *C) {
37 return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
Chris Lattner02446fc2010-01-04 07:37:31 +000038}
39
40static ConstantInt *ExtractElement(Constant *V, Constant *Idx) {
41 return cast<ConstantInt>(ConstantExpr::getExtractElement(V, Idx));
42}
43
44static bool HasAddOverflow(ConstantInt *Result,
45 ConstantInt *In1, ConstantInt *In2,
46 bool IsSigned) {
Chris Lattnerc73b24d2011-07-15 06:08:15 +000047 if (!IsSigned)
Chris Lattner02446fc2010-01-04 07:37:31 +000048 return Result->getValue().ult(In1->getValue());
Chris Lattnerc73b24d2011-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 Lattner02446fc2010-01-04 07:37:31 +000053}
54
55/// AddWithOverflow - Compute Result = In1+In2, returning true if the result
56/// overflowed for this type.
57static bool AddWithOverflow(Constant *&Result, Constant *In1,
58 Constant *In2, bool IsSigned = false) {
59 Result = ConstantExpr::getAdd(In1, In2);
60
Chris Lattnerdb125cf2011-07-18 04:54:35 +000061 if (VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
Chris Lattner02446fc2010-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);
64 if (HasAddOverflow(ExtractElement(Result, Idx),
65 ExtractElement(In1, Idx),
66 ExtractElement(In2, Idx),
67 IsSigned))
68 return true;
69 }
70 return false;
71 }
72
73 return HasAddOverflow(cast<ConstantInt>(Result),
74 cast<ConstantInt>(In1), cast<ConstantInt>(In2),
75 IsSigned);
76}
77
78static bool HasSubOverflow(ConstantInt *Result,
79 ConstantInt *In1, ConstantInt *In2,
80 bool IsSigned) {
Chris Lattnerc73b24d2011-07-15 06:08:15 +000081 if (!IsSigned)
Chris Lattner02446fc2010-01-04 07:37:31 +000082 return Result->getValue().ugt(In1->getValue());
Jim Grosbach0cc4a952011-09-30 18:09:53 +000083
Chris Lattnerc73b24d2011-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 Lattner02446fc2010-01-04 07:37:31 +000088}
89
90/// SubWithOverflow - Compute Result = In1-In2, returning true if the result
91/// overflowed for this type.
92static bool SubWithOverflow(Constant *&Result, Constant *In1,
93 Constant *In2, bool IsSigned = false) {
94 Result = ConstantExpr::getSub(In1, In2);
95
Chris Lattnerdb125cf2011-07-18 04:54:35 +000096 if (VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
Chris Lattner02446fc2010-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);
99 if (HasSubOverflow(ExtractElement(Result, Idx),
100 ExtractElement(In1, Idx),
101 ExtractElement(In2, Idx),
102 IsSigned))
103 return true;
104 }
105 return false;
106 }
107
108 return HasSubOverflow(cast<ConstantInt>(Result),
109 cast<ConstantInt>(In1), cast<ConstantInt>(In2),
110 IsSigned);
111}
112
113/// isSignBitCheck - Given an exploded icmp instruction, return true if the
114/// comparison only checks the sign bit. If it only checks the sign bit, set
115/// TrueIfSigned if the result of the comparison is true when the input value is
116/// signed.
117static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS,
118 bool &TrueIfSigned) {
119 switch (pred) {
120 case ICmpInst::ICMP_SLT: // True if LHS s< 0
121 TrueIfSigned = true;
122 return RHS->isZero();
123 case ICmpInst::ICMP_SLE: // True if LHS s<= RHS and RHS == -1
124 TrueIfSigned = true;
125 return RHS->isAllOnesValue();
126 case ICmpInst::ICMP_SGT: // True if LHS s> -1
127 TrueIfSigned = false;
128 return RHS->isAllOnesValue();
129 case ICmpInst::ICMP_UGT:
130 // True if LHS u> RHS and RHS == high-bit-mask - 1
131 TrueIfSigned = true;
Chris Lattnerc73b24d2011-07-15 06:08:15 +0000132 return RHS->isMaxValue(true);
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000133 case ICmpInst::ICMP_UGE:
Chris Lattner02446fc2010-01-04 07:37:31 +0000134 // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc)
135 TrueIfSigned = true;
136 return RHS->getValue().isSignBit();
137 default:
138 return false;
139 }
140}
141
Arnaud A. de Grandmaison35763b12013-03-25 09:48:49 +0000142/// Returns true if the exploded icmp can be expressed as a comparison to zero
143/// and update the predicate accordingly. The signedness of the comparison is
144static bool isSignTest(ICmpInst::Predicate &pred, const ConstantInt *RHS) {
145 if (!ICmpInst::isSigned(pred))
146 return false;
147
148 if (RHS->isZero())
149 return true;
150
151 if (RHS->isOne())
152 switch (pred) {
153 case ICmpInst::ICMP_SGE:
154 pred = ICmpInst::ICMP_SGT;
155 return true;
156 case ICmpInst::ICMP_SLT:
157 pred = ICmpInst::ICMP_SLE;
158 return true;
159 default:
160 return false;
161 }
162
163 if (RHS->isAllOnesValue())
164 switch (pred) {
165 case ICmpInst::ICMP_SLE:
166 pred = ICmpInst::ICMP_SLT;
167 return true;
168 case ICmpInst::ICMP_SGT:
169 pred = ICmpInst::ICMP_SGE;
170 return true;
171 default:
172 return false;
173 }
174
175 return false;
176}
177
Chris Lattner02446fc2010-01-04 07:37:31 +0000178// isHighOnes - Return true if the constant is of the form 1+0+.
179// This is the same as lowones(~X).
180static bool isHighOnes(const ConstantInt *CI) {
181 return (~CI->getValue() + 1).isPowerOf2();
182}
183
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000184/// ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a
Chris Lattner02446fc2010-01-04 07:37:31 +0000185/// set of known zero and one bits, compute the maximum and minimum values that
186/// could have the specified known zero and known one bits, returning them in
187/// min/max.
188static void ComputeSignedMinMaxValuesFromKnownBits(const APInt& KnownZero,
189 const APInt& KnownOne,
190 APInt& Min, APInt& Max) {
191 assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() &&
192 KnownZero.getBitWidth() == Min.getBitWidth() &&
193 KnownZero.getBitWidth() == Max.getBitWidth() &&
194 "KnownZero, KnownOne and Min, Max must have equal bitwidth.");
195 APInt UnknownBits = ~(KnownZero|KnownOne);
196
197 // The minimum value is when all unknown bits are zeros, EXCEPT for the sign
198 // bit if it is unknown.
199 Min = KnownOne;
200 Max = KnownOne|UnknownBits;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000201
Chris Lattner02446fc2010-01-04 07:37:31 +0000202 if (UnknownBits.isNegative()) { // Sign bit is unknown
Jay Foad7a874dd2010-12-01 08:53:58 +0000203 Min.setBit(Min.getBitWidth()-1);
204 Max.clearBit(Max.getBitWidth()-1);
Chris Lattner02446fc2010-01-04 07:37:31 +0000205 }
206}
207
208// ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and
209// a set of known zero and one bits, compute the maximum and minimum values that
210// could have the specified known zero and known one bits, returning them in
211// min/max.
212static void ComputeUnsignedMinMaxValuesFromKnownBits(const APInt &KnownZero,
213 const APInt &KnownOne,
214 APInt &Min, APInt &Max) {
215 assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() &&
216 KnownZero.getBitWidth() == Min.getBitWidth() &&
217 KnownZero.getBitWidth() == Max.getBitWidth() &&
218 "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth.");
219 APInt UnknownBits = ~(KnownZero|KnownOne);
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000220
Chris Lattner02446fc2010-01-04 07:37:31 +0000221 // The minimum value is when the unknown bits are all zeros.
222 Min = KnownOne;
223 // The maximum value is when the unknown bits are all ones.
224 Max = KnownOne|UnknownBits;
225}
226
227
228
229/// FoldCmpLoadFromIndexedGlobal - Called we see this pattern:
230/// cmp pred (load (gep GV, ...)), cmpcst
231/// where GV is a global variable with a constant initializer. Try to simplify
232/// this into some simple computation that does not need the load. For example
233/// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3".
234///
235/// If AndCst is non-null, then the loaded value is masked with that constant
236/// before doing the comparison. This handles cases like "A[i]&4 == 0".
237Instruction *InstCombiner::
238FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV,
239 CmpInst &ICI, ConstantInt *AndCst) {
Chris Lattnerd7f5a582010-01-04 18:57:15 +0000240 // We need TD information to know the pointer size unless this is inbounds.
241 if (!GEP->isInBounds() && TD == 0) return 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000242
Chris Lattnerc8d75c72012-01-31 02:55:06 +0000243 Constant *Init = GV->getInitializer();
244 if (!isa<ConstantArray>(Init) && !isa<ConstantDataArray>(Init))
245 return 0;
246
247 uint64_t ArrayElementCount = Init->getType()->getArrayNumElements();
248 if (ArrayElementCount > 1024) return 0; // Don't blow up on huge arrays.
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000249
Chris Lattner02446fc2010-01-04 07:37:31 +0000250 // There are many forms of this optimization we can handle, for now, just do
251 // the simple index into a single-dimensional array.
252 //
253 // Require: GEP GV, 0, i {{, constant indices}}
254 if (GEP->getNumOperands() < 3 ||
255 !isa<ConstantInt>(GEP->getOperand(1)) ||
256 !cast<ConstantInt>(GEP->getOperand(1))->isZero() ||
257 isa<Constant>(GEP->getOperand(2)))
258 return 0;
259
260 // Check that indices after the variable are constants and in-range for the
261 // type they index. Collect the indices. This is typically for arrays of
262 // structs.
263 SmallVector<unsigned, 4> LaterIndices;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000264
Chris Lattnerc8d75c72012-01-31 02:55:06 +0000265 Type *EltTy = Init->getType()->getArrayElementType();
Chris Lattner02446fc2010-01-04 07:37:31 +0000266 for (unsigned i = 3, e = GEP->getNumOperands(); i != e; ++i) {
267 ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(i));
268 if (Idx == 0) return 0; // Variable index.
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000269
Chris Lattner02446fc2010-01-04 07:37:31 +0000270 uint64_t IdxVal = Idx->getZExtValue();
271 if ((unsigned)IdxVal != IdxVal) return 0; // Too large array index.
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000272
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000273 if (StructType *STy = dyn_cast<StructType>(EltTy))
Chris Lattner02446fc2010-01-04 07:37:31 +0000274 EltTy = STy->getElementType(IdxVal);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000275 else if (ArrayType *ATy = dyn_cast<ArrayType>(EltTy)) {
Chris Lattner02446fc2010-01-04 07:37:31 +0000276 if (IdxVal >= ATy->getNumElements()) return 0;
277 EltTy = ATy->getElementType();
278 } else {
279 return 0; // Unknown type.
280 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000281
Chris Lattner02446fc2010-01-04 07:37:31 +0000282 LaterIndices.push_back(IdxVal);
283 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000284
Chris Lattner02446fc2010-01-04 07:37:31 +0000285 enum { Overdefined = -3, Undefined = -2 };
286
287 // Variables for our state machines.
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000288
Chris Lattner02446fc2010-01-04 07:37:31 +0000289 // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form
290 // "i == 47 | i == 87", where 47 is the first index the condition is true for,
291 // and 87 is the second (and last) index. FirstTrueElement is -2 when
292 // undefined, otherwise set to the first true element. SecondTrueElement is
293 // -2 when undefined, -3 when overdefined and >= 0 when that index is true.
294 int FirstTrueElement = Undefined, SecondTrueElement = Undefined;
295
296 // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the
297 // form "i != 47 & i != 87". Same state transitions as for true elements.
298 int FirstFalseElement = Undefined, SecondFalseElement = Undefined;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000299
Chris Lattner02446fc2010-01-04 07:37:31 +0000300 /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these
301 /// define a state machine that triggers for ranges of values that the index
302 /// is true or false for. This triggers on things like "abbbbc"[i] == 'b'.
303 /// This is -2 when undefined, -3 when overdefined, and otherwise the last
304 /// index in the range (inclusive). We use -2 for undefined here because we
305 /// use relative comparisons and don't want 0-1 to match -1.
306 int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000307
Chris Lattner02446fc2010-01-04 07:37:31 +0000308 // MagicBitvector - This is a magic bitvector where we set a bit if the
309 // comparison is true for element 'i'. If there are 64 elements or less in
310 // the array, this will fully represent all the comparison results.
311 uint64_t MagicBitvector = 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000312
313
Chris Lattner02446fc2010-01-04 07:37:31 +0000314 // Scan the array and see if one of our patterns matches.
315 Constant *CompareRHS = cast<Constant>(ICI.getOperand(1));
Chris Lattnerc8d75c72012-01-31 02:55:06 +0000316 for (unsigned i = 0, e = ArrayElementCount; i != e; ++i) {
317 Constant *Elt = Init->getAggregateElement(i);
318 if (Elt == 0) return 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000319
Chris Lattner02446fc2010-01-04 07:37:31 +0000320 // If this is indexing an array of structures, get the structure element.
321 if (!LaterIndices.empty())
Jay Foadfc6d3a42011-07-13 10:26:04 +0000322 Elt = ConstantExpr::getExtractValue(Elt, LaterIndices);
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000323
Chris Lattner02446fc2010-01-04 07:37:31 +0000324 // If the element is masked, handle it.
325 if (AndCst) Elt = ConstantExpr::getAnd(Elt, AndCst);
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000326
Chris Lattner02446fc2010-01-04 07:37:31 +0000327 // Find out if the comparison would be true or false for the i'th element.
328 Constant *C = ConstantFoldCompareInstOperands(ICI.getPredicate(), Elt,
Chad Rosieraab8e282011-12-02 01:26:24 +0000329 CompareRHS, TD, TLI);
Chris Lattner02446fc2010-01-04 07:37:31 +0000330 // If the result is undef for this element, ignore it.
331 if (isa<UndefValue>(C)) {
332 // Extend range state machines to cover this element in case there is an
333 // undef in the middle of the range.
334 if (TrueRangeEnd == (int)i-1)
335 TrueRangeEnd = i;
336 if (FalseRangeEnd == (int)i-1)
337 FalseRangeEnd = i;
338 continue;
339 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000340
Chris Lattner02446fc2010-01-04 07:37:31 +0000341 // If we can't compute the result for any of the elements, we have to give
342 // up evaluating the entire conditional.
343 if (!isa<ConstantInt>(C)) return 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000344
Chris Lattner02446fc2010-01-04 07:37:31 +0000345 // Otherwise, we know if the comparison is true or false for this element,
346 // update our state machines.
347 bool IsTrueForElt = !cast<ConstantInt>(C)->isZero();
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000348
Chris Lattner02446fc2010-01-04 07:37:31 +0000349 // State machine for single/double/range index comparison.
350 if (IsTrueForElt) {
351 // Update the TrueElement state machine.
352 if (FirstTrueElement == Undefined)
353 FirstTrueElement = TrueRangeEnd = i; // First true element.
354 else {
355 // Update double-compare state machine.
356 if (SecondTrueElement == Undefined)
357 SecondTrueElement = i;
358 else
359 SecondTrueElement = Overdefined;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000360
Chris Lattner02446fc2010-01-04 07:37:31 +0000361 // Update range state machine.
362 if (TrueRangeEnd == (int)i-1)
363 TrueRangeEnd = i;
364 else
365 TrueRangeEnd = Overdefined;
366 }
367 } else {
368 // Update the FalseElement state machine.
369 if (FirstFalseElement == Undefined)
370 FirstFalseElement = FalseRangeEnd = i; // First false element.
371 else {
372 // Update double-compare state machine.
373 if (SecondFalseElement == Undefined)
374 SecondFalseElement = i;
375 else
376 SecondFalseElement = Overdefined;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000377
Chris Lattner02446fc2010-01-04 07:37:31 +0000378 // Update range state machine.
379 if (FalseRangeEnd == (int)i-1)
380 FalseRangeEnd = i;
381 else
382 FalseRangeEnd = Overdefined;
383 }
384 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000385
386
Chris Lattner02446fc2010-01-04 07:37:31 +0000387 // If this element is in range, update our magic bitvector.
388 if (i < 64 && IsTrueForElt)
389 MagicBitvector |= 1ULL << i;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000390
Chris Lattner02446fc2010-01-04 07:37:31 +0000391 // If all of our states become overdefined, bail out early. Since the
392 // predicate is expensive, only check it every 8 elements. This is only
393 // really useful for really huge arrays.
394 if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined &&
395 SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined &&
396 FalseRangeEnd == Overdefined)
397 return 0;
398 }
399
400 // Now that we've scanned the entire array, emit our new comparison(s). We
401 // order the state machines in complexity of the generated code.
402 Value *Idx = GEP->getOperand(2);
403
Chris Lattnerd7f5a582010-01-04 18:57:15 +0000404 // If the index is larger than the pointer size of the target, truncate the
405 // index down like the GEP would do implicitly. We don't have to do this for
406 // an inbounds GEP because the index can't be out of range.
407 if (!GEP->isInBounds() &&
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000408 Idx->getType()->getPrimitiveSizeInBits() > TD->getPointerSizeInBits())
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000409 Idx = Builder->CreateTrunc(Idx, TD->getIntPtrType(Idx->getContext()));
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000410
Chris Lattner02446fc2010-01-04 07:37:31 +0000411 // If the comparison is only true for one or two elements, emit direct
412 // comparisons.
413 if (SecondTrueElement != Overdefined) {
414 // None true -> false.
415 if (FirstTrueElement == Undefined)
416 return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(GEP->getContext()));
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000417
Chris Lattner02446fc2010-01-04 07:37:31 +0000418 Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement);
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000419
Chris Lattner02446fc2010-01-04 07:37:31 +0000420 // True for one element -> 'i == 47'.
421 if (SecondTrueElement == Undefined)
422 return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx);
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000423
Chris Lattner02446fc2010-01-04 07:37:31 +0000424 // True for two elements -> 'i == 47 | i == 72'.
425 Value *C1 = Builder->CreateICmpEQ(Idx, FirstTrueIdx);
426 Value *SecondTrueIdx = ConstantInt::get(Idx->getType(), SecondTrueElement);
427 Value *C2 = Builder->CreateICmpEQ(Idx, SecondTrueIdx);
428 return BinaryOperator::CreateOr(C1, C2);
429 }
430
431 // If the comparison is only false for one or two elements, emit direct
432 // comparisons.
433 if (SecondFalseElement != Overdefined) {
434 // None false -> true.
435 if (FirstFalseElement == Undefined)
436 return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(GEP->getContext()));
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000437
Chris Lattner02446fc2010-01-04 07:37:31 +0000438 Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement);
439
440 // False for one element -> 'i != 47'.
441 if (SecondFalseElement == Undefined)
442 return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx);
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000443
Chris Lattner02446fc2010-01-04 07:37:31 +0000444 // False for two elements -> 'i != 47 & i != 72'.
445 Value *C1 = Builder->CreateICmpNE(Idx, FirstFalseIdx);
446 Value *SecondFalseIdx = ConstantInt::get(Idx->getType(),SecondFalseElement);
447 Value *C2 = Builder->CreateICmpNE(Idx, SecondFalseIdx);
448 return BinaryOperator::CreateAnd(C1, C2);
449 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000450
Chris Lattner02446fc2010-01-04 07:37:31 +0000451 // If the comparison can be replaced with a range comparison for the elements
452 // where it is true, emit the range check.
453 if (TrueRangeEnd != Overdefined) {
454 assert(TrueRangeEnd != FirstTrueElement && "Should emit single compare");
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000455
Chris Lattner02446fc2010-01-04 07:37:31 +0000456 // Generate (i-FirstTrue) <u (TrueRangeEnd-FirstTrue+1).
457 if (FirstTrueElement) {
458 Value *Offs = ConstantInt::get(Idx->getType(), -FirstTrueElement);
459 Idx = Builder->CreateAdd(Idx, Offs);
460 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000461
Chris Lattner02446fc2010-01-04 07:37:31 +0000462 Value *End = ConstantInt::get(Idx->getType(),
463 TrueRangeEnd-FirstTrueElement+1);
464 return new ICmpInst(ICmpInst::ICMP_ULT, Idx, End);
465 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000466
Chris Lattner02446fc2010-01-04 07:37:31 +0000467 // False range check.
468 if (FalseRangeEnd != Overdefined) {
469 assert(FalseRangeEnd != FirstFalseElement && "Should emit single compare");
470 // Generate (i-FirstFalse) >u (FalseRangeEnd-FirstFalse).
471 if (FirstFalseElement) {
472 Value *Offs = ConstantInt::get(Idx->getType(), -FirstFalseElement);
473 Idx = Builder->CreateAdd(Idx, Offs);
474 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000475
Chris Lattner02446fc2010-01-04 07:37:31 +0000476 Value *End = ConstantInt::get(Idx->getType(),
477 FalseRangeEnd-FirstFalseElement);
478 return new ICmpInst(ICmpInst::ICMP_UGT, Idx, End);
479 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000480
481
Arnaud A. de Grandmaison2be921a2013-03-22 08:25:01 +0000482 // If a magic bitvector captures the entire comparison state
Chris Lattner02446fc2010-01-04 07:37:31 +0000483 // of this load, replace it with computation that does:
484 // ((magic_cst >> i) & 1) != 0
Arnaud A. de Grandmaison2be921a2013-03-22 08:25:01 +0000485 {
486 Type *Ty = 0;
487
488 // Look for an appropriate type:
489 // - The type of Idx if the magic fits
490 // - The smallest fitting legal type if we have a DataLayout
491 // - Default to i32
492 if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth())
493 Ty = Idx->getType();
494 else if (TD)
495 Ty = TD->getSmallestLegalIntType(Init->getContext(), ArrayElementCount);
496 else if (ArrayElementCount <= 32)
Chris Lattner02446fc2010-01-04 07:37:31 +0000497 Ty = Type::getInt32Ty(Init->getContext());
Arnaud A. de Grandmaison2be921a2013-03-22 08:25:01 +0000498
499 if (Ty != 0) {
500 Value *V = Builder->CreateIntCast(Idx, Ty, false);
501 V = Builder->CreateLShr(ConstantInt::get(Ty, MagicBitvector), V);
502 V = Builder->CreateAnd(ConstantInt::get(Ty, 1), V);
503 return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));
504 }
Chris Lattner02446fc2010-01-04 07:37:31 +0000505 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000506
Chris Lattner02446fc2010-01-04 07:37:31 +0000507 return 0;
508}
509
510
511/// EvaluateGEPOffsetExpression - Return a value that can be used to compare
512/// the *offset* implied by a GEP to zero. For example, if we have &A[i], we
513/// want to return 'i' for "icmp ne i, 0". Note that, in general, indices can
514/// be complex, and scales are involved. The above expression would also be
515/// legal to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32).
516/// This later form is less amenable to optimization though, and we are allowed
517/// to generate the first by knowing that pointer arithmetic doesn't overflow.
518///
519/// If we can't emit an optimized form for this expression, this returns null.
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000520///
Eli Friedman107ffd52011-05-18 23:11:30 +0000521static Value *EvaluateGEPOffsetExpression(User *GEP, InstCombiner &IC) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000522 DataLayout &TD = *IC.getDataLayout();
Chris Lattner02446fc2010-01-04 07:37:31 +0000523 gep_type_iterator GTI = gep_type_begin(GEP);
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000524
Chris Lattner02446fc2010-01-04 07:37:31 +0000525 // Check to see if this gep only has a single variable index. If so, and if
526 // any constant indices are a multiple of its scale, then we can compute this
527 // in terms of the scale of the variable index. For example, if the GEP
528 // implies an offset of "12 + i*4", then we can codegen this as "3 + i",
529 // because the expression will cross zero at the same point.
530 unsigned i, e = GEP->getNumOperands();
531 int64_t Offset = 0;
532 for (i = 1; i != e; ++i, ++GTI) {
533 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
534 // Compute the aggregate offset of constant indices.
535 if (CI->isZero()) continue;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000536
Chris Lattner02446fc2010-01-04 07:37:31 +0000537 // Handle a struct index, which adds its field offset to the pointer.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000538 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Chris Lattner02446fc2010-01-04 07:37:31 +0000539 Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
540 } else {
541 uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType());
542 Offset += Size*CI->getSExtValue();
543 }
544 } else {
545 // Found our variable index.
546 break;
547 }
548 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000549
Chris Lattner02446fc2010-01-04 07:37:31 +0000550 // If there are no variable indices, we must have a constant offset, just
551 // evaluate it the general way.
552 if (i == e) return 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000553
Chris Lattner02446fc2010-01-04 07:37:31 +0000554 Value *VariableIdx = GEP->getOperand(i);
555 // Determine the scale factor of the variable element. For example, this is
556 // 4 if the variable index is into an array of i32.
557 uint64_t VariableScale = TD.getTypeAllocSize(GTI.getIndexedType());
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000558
Chris Lattner02446fc2010-01-04 07:37:31 +0000559 // Verify that there are no other variable indices. If so, emit the hard way.
560 for (++i, ++GTI; i != e; ++i, ++GTI) {
561 ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i));
562 if (!CI) return 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000563
Chris Lattner02446fc2010-01-04 07:37:31 +0000564 // Compute the aggregate offset of constant indices.
565 if (CI->isZero()) continue;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000566
Chris Lattner02446fc2010-01-04 07:37:31 +0000567 // Handle a struct index, which adds its field offset to the pointer.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000568 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Chris Lattner02446fc2010-01-04 07:37:31 +0000569 Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
570 } else {
571 uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType());
572 Offset += Size*CI->getSExtValue();
573 }
574 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000575
Chris Lattner02446fc2010-01-04 07:37:31 +0000576 // Okay, we know we have a single variable index, which must be a
577 // pointer/array/vector index. If there is no offset, life is simple, return
578 // the index.
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000579 unsigned IntPtrWidth = TD.getPointerSizeInBits();
Chris Lattner02446fc2010-01-04 07:37:31 +0000580 if (Offset == 0) {
581 // Cast to intptrty in case a truncation occurs. If an extension is needed,
582 // we don't need to bother extending: the extension won't affect where the
583 // computation crosses zero.
Eli Friedman107ffd52011-05-18 23:11:30 +0000584 if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth) {
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000585 Type *IntPtrTy = TD.getIntPtrType(VariableIdx->getContext());
Eli Friedman107ffd52011-05-18 23:11:30 +0000586 VariableIdx = IC.Builder->CreateTrunc(VariableIdx, IntPtrTy);
587 }
Chris Lattner02446fc2010-01-04 07:37:31 +0000588 return VariableIdx;
589 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000590
Chris Lattner02446fc2010-01-04 07:37:31 +0000591 // Otherwise, there is an index. The computation we will do will be modulo
592 // the pointer size, so get it.
593 uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth);
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000594
Chris Lattner02446fc2010-01-04 07:37:31 +0000595 Offset &= PtrSizeMask;
596 VariableScale &= PtrSizeMask;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000597
Chris Lattner02446fc2010-01-04 07:37:31 +0000598 // To do this transformation, any constant index must be a multiple of the
599 // variable scale factor. For example, we can evaluate "12 + 4*i" as "3 + i",
600 // but we can't evaluate "10 + 3*i" in terms of i. Check that the offset is a
601 // multiple of the variable scale.
602 int64_t NewOffs = Offset / (int64_t)VariableScale;
603 if (Offset != NewOffs*(int64_t)VariableScale)
604 return 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000605
Chris Lattner02446fc2010-01-04 07:37:31 +0000606 // Okay, we can do this evaluation. Start by converting the index to intptr.
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000607 Type *IntPtrTy = TD.getIntPtrType(VariableIdx->getContext());
Chris Lattner02446fc2010-01-04 07:37:31 +0000608 if (VariableIdx->getType() != IntPtrTy)
Eli Friedman107ffd52011-05-18 23:11:30 +0000609 VariableIdx = IC.Builder->CreateIntCast(VariableIdx, IntPtrTy,
610 true /*Signed*/);
Chris Lattner02446fc2010-01-04 07:37:31 +0000611 Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs);
Eli Friedman107ffd52011-05-18 23:11:30 +0000612 return IC.Builder->CreateAdd(VariableIdx, OffsetVal, "offset");
Chris Lattner02446fc2010-01-04 07:37:31 +0000613}
614
615/// FoldGEPICmp - Fold comparisons between a GEP instruction and something
616/// else. At this point we know that the GEP is on the LHS of the comparison.
617Instruction *InstCombiner::FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
618 ICmpInst::Predicate Cond,
619 Instruction &I) {
Benjamin Kramer8294eb52012-02-21 13:31:09 +0000620 // Don't transform signed compares of GEPs into index compares. Even if the
621 // GEP is inbounds, the final add of the base pointer can have signed overflow
622 // and would change the result of the icmp.
623 // e.g. "&foo[0] <s &foo[1]" can't be folded to "true" because "foo" could be
Benjamin Kramera42d5c42012-02-21 13:40:06 +0000624 // the maximum signed value for the pointer type.
Benjamin Kramer8294eb52012-02-21 13:31:09 +0000625 if (ICmpInst::isSigned(Cond))
626 return 0;
627
Chris Lattner02446fc2010-01-04 07:37:31 +0000628 // Look through bitcasts.
629 if (BitCastInst *BCI = dyn_cast<BitCastInst>(RHS))
630 RHS = BCI->getOperand(0);
631
632 Value *PtrBase = GEPLHS->getOperand(0);
633 if (TD && PtrBase == RHS && GEPLHS->isInBounds()) {
634 // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
635 // This transformation (ignoring the base and scales) is valid because we
636 // know pointers can't overflow since the gep is inbounds. See if we can
637 // output an optimized form.
Eli Friedman107ffd52011-05-18 23:11:30 +0000638 Value *Offset = EvaluateGEPOffsetExpression(GEPLHS, *this);
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000639
Chris Lattner02446fc2010-01-04 07:37:31 +0000640 // If not, synthesize the offset the hard way.
641 if (Offset == 0)
642 Offset = EmitGEPOffset(GEPLHS);
643 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset,
644 Constant::getNullValue(Offset->getType()));
645 } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {
646 // If the base pointers are different, but the indices are the same, just
647 // compare the base pointer.
648 if (PtrBase != GEPRHS->getOperand(0)) {
649 bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands();
650 IndicesTheSame &= GEPLHS->getOperand(0)->getType() ==
651 GEPRHS->getOperand(0)->getType();
652 if (IndicesTheSame)
653 for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
654 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
655 IndicesTheSame = false;
656 break;
657 }
658
659 // If all indices are the same, just compare the base pointers.
660 if (IndicesTheSame)
661 return new ICmpInst(ICmpInst::getSignedPredicate(Cond),
662 GEPLHS->getOperand(0), GEPRHS->getOperand(0));
663
Benjamin Kramer9bb40852012-02-20 15:07:47 +0000664 // If we're comparing GEPs with two base pointers that only differ in type
665 // and both GEPs have only constant indices or just one use, then fold
666 // the compare with the adjusted indices.
Benjamin Kramer6ad48f42012-02-20 18:45:10 +0000667 if (TD && GEPLHS->isInBounds() && GEPRHS->isInBounds() &&
Benjamin Kramer9bb40852012-02-20 15:07:47 +0000668 (GEPLHS->hasAllConstantIndices() || GEPLHS->hasOneUse()) &&
669 (GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) &&
670 PtrBase->stripPointerCasts() ==
671 GEPRHS->getOperand(0)->stripPointerCasts()) {
672 Value *Cmp = Builder->CreateICmp(ICmpInst::getSignedPredicate(Cond),
673 EmitGEPOffset(GEPLHS),
674 EmitGEPOffset(GEPRHS));
675 return ReplaceInstUsesWith(I, Cmp);
676 }
677
Chris Lattner02446fc2010-01-04 07:37:31 +0000678 // Otherwise, the base pointers are different and the indices are
679 // different, bail out.
680 return 0;
681 }
682
683 // If one of the GEPs has all zero indices, recurse.
684 bool AllZeros = true;
685 for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
686 if (!isa<Constant>(GEPLHS->getOperand(i)) ||
687 !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) {
688 AllZeros = false;
689 break;
690 }
691 if (AllZeros)
692 return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0),
693 ICmpInst::getSwappedPredicate(Cond), I);
694
695 // If the other GEP has all zero indices, recurse.
696 AllZeros = true;
697 for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
698 if (!isa<Constant>(GEPRHS->getOperand(i)) ||
699 !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) {
700 AllZeros = false;
701 break;
702 }
703 if (AllZeros)
704 return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I);
705
Stuart Hastings67f071e2011-05-14 05:55:10 +0000706 bool GEPsInBounds = GEPLHS->isInBounds() && GEPRHS->isInBounds();
Chris Lattner02446fc2010-01-04 07:37:31 +0000707 if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) {
708 // If the GEPs only differ by one index, compare it.
709 unsigned NumDifferences = 0; // Keep track of # differences.
710 unsigned DiffOperand = 0; // The operand that differs.
711 for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
712 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
713 if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() !=
714 GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) {
715 // Irreconcilable differences.
716 NumDifferences = 2;
717 break;
718 } else {
719 if (NumDifferences++) break;
720 DiffOperand = i;
721 }
722 }
723
724 if (NumDifferences == 0) // SAME GEP?
725 return ReplaceInstUsesWith(I, // No comparison is needed here.
726 ConstantInt::get(Type::getInt1Ty(I.getContext()),
727 ICmpInst::isTrueWhenEqual(Cond)));
728
Stuart Hastings67f071e2011-05-14 05:55:10 +0000729 else if (NumDifferences == 1 && GEPsInBounds) {
Chris Lattner02446fc2010-01-04 07:37:31 +0000730 Value *LHSV = GEPLHS->getOperand(DiffOperand);
731 Value *RHSV = GEPRHS->getOperand(DiffOperand);
732 // Make sure we do a signed comparison here.
733 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV);
734 }
735 }
736
737 // Only lower this if the icmp is the only user of the GEP or if we expect
738 // the result to fold to a constant!
739 if (TD &&
Stuart Hastings67f071e2011-05-14 05:55:10 +0000740 GEPsInBounds &&
Chris Lattner02446fc2010-01-04 07:37:31 +0000741 (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) &&
742 (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) {
743 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
744 Value *L = EmitGEPOffset(GEPLHS);
745 Value *R = EmitGEPOffset(GEPRHS);
746 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R);
747 }
748 }
749 return 0;
750}
751
752/// FoldICmpAddOpCst - Fold "icmp pred (X+CI), X".
753Instruction *InstCombiner::FoldICmpAddOpCst(ICmpInst &ICI,
754 Value *X, ConstantInt *CI,
755 ICmpInst::Predicate Pred,
756 Value *TheAdd) {
757 // If we have X+0, exit early (simplifying logic below) and let it get folded
758 // elsewhere. icmp X+0, X -> icmp X, X
759 if (CI->isZero()) {
760 bool isTrue = ICmpInst::isTrueWhenEqual(Pred);
761 return ReplaceInstUsesWith(ICI, ConstantInt::get(ICI.getType(), isTrue));
762 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000763
Chris Lattner02446fc2010-01-04 07:37:31 +0000764 // (X+4) == X -> false.
765 if (Pred == ICmpInst::ICMP_EQ)
766 return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(X->getContext()));
767
768 // (X+4) != X -> true.
769 if (Pred == ICmpInst::ICMP_NE)
770 return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(X->getContext()));
771
Chris Lattner02446fc2010-01-04 07:37:31 +0000772 // From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0,
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000773 // so the values can never be equal. Similarly for all other "or equals"
Chris Lattner02446fc2010-01-04 07:37:31 +0000774 // operators.
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000775
Chris Lattner9aa1e242010-01-08 17:48:19 +0000776 // (X+1) <u X --> X >u (MAXUINT-1) --> X == 255
Chris Lattner02446fc2010-01-04 07:37:31 +0000777 // (X+2) <u X --> X >u (MAXUINT-2) --> X > 253
778 // (X+MAXUINT) <u X --> X >u (MAXUINT-MAXUINT) --> X != 0
779 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000780 Value *R =
Chris Lattner9aa1e242010-01-08 17:48:19 +0000781 ConstantExpr::getSub(ConstantInt::getAllOnesValue(CI->getType()), CI);
Chris Lattner02446fc2010-01-04 07:37:31 +0000782 return new ICmpInst(ICmpInst::ICMP_UGT, X, R);
783 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000784
Chris Lattner02446fc2010-01-04 07:37:31 +0000785 // (X+1) >u X --> X <u (0-1) --> X != 255
786 // (X+2) >u X --> X <u (0-2) --> X <u 254
787 // (X+MAXUINT) >u X --> X <u (0-MAXUINT) --> X <u 1 --> X == 0
Duncan Sandsa7724332011-02-17 07:46:37 +0000788 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
Chris Lattner02446fc2010-01-04 07:37:31 +0000789 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantExpr::getNeg(CI));
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000790
Chris Lattner02446fc2010-01-04 07:37:31 +0000791 unsigned BitWidth = CI->getType()->getPrimitiveSizeInBits();
792 ConstantInt *SMax = ConstantInt::get(X->getContext(),
793 APInt::getSignedMaxValue(BitWidth));
794
795 // (X+ 1) <s X --> X >s (MAXSINT-1) --> X == 127
796 // (X+ 2) <s X --> X >s (MAXSINT-2) --> X >s 125
797 // (X+MAXSINT) <s X --> X >s (MAXSINT-MAXSINT) --> X >s 0
798 // (X+MINSINT) <s X --> X >s (MAXSINT-MINSINT) --> X >s -1
799 // (X+ -2) <s X --> X >s (MAXSINT- -2) --> X >s 126
800 // (X+ -1) <s X --> X >s (MAXSINT- -1) --> X != 127
Duncan Sandsa7724332011-02-17 07:46:37 +0000801 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
Chris Lattner02446fc2010-01-04 07:37:31 +0000802 return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantExpr::getSub(SMax, CI));
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000803
Chris Lattner02446fc2010-01-04 07:37:31 +0000804 // (X+ 1) >s X --> X <s (MAXSINT-(1-1)) --> X != 127
805 // (X+ 2) >s X --> X <s (MAXSINT-(2-1)) --> X <s 126
806 // (X+MAXSINT) >s X --> X <s (MAXSINT-(MAXSINT-1)) --> X <s 1
807 // (X+MINSINT) >s X --> X <s (MAXSINT-(MINSINT-1)) --> X <s -2
808 // (X+ -2) >s X --> X <s (MAXSINT-(-2-1)) --> X <s -126
809 // (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000810
Chris Lattner02446fc2010-01-04 07:37:31 +0000811 assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE);
812 Constant *C = ConstantInt::get(X->getContext(), CI->getValue()-1);
813 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantExpr::getSub(SMax, C));
814}
815
816/// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS
817/// and CmpRHS are both known to be integer constants.
818Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
819 ConstantInt *DivRHS) {
820 ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1));
821 const APInt &CmpRHSV = CmpRHS->getValue();
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000822
823 // FIXME: If the operand types don't match the type of the divide
Chris Lattner02446fc2010-01-04 07:37:31 +0000824 // then don't attempt this transform. The code below doesn't have the
825 // logic to deal with a signed divide and an unsigned compare (and
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000826 // vice versa). This is because (x /s C1) <s C2 produces different
Chris Lattner02446fc2010-01-04 07:37:31 +0000827 // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000828 // (x /u C1) <u C2. Simply casting the operands and result won't
829 // work. :( The if statement below tests that condition and bails
Chris Lattnerb20c0b52011-02-10 05:23:05 +0000830 // if it finds it.
Chris Lattner02446fc2010-01-04 07:37:31 +0000831 bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv;
832 if (!ICI.isEquality() && DivIsSigned != ICI.isSigned())
833 return 0;
834 if (DivRHS->isZero())
835 return 0; // The ProdOV computation fails on divide by zero.
836 if (DivIsSigned && DivRHS->isAllOnesValue())
837 return 0; // The overflow computation also screws up here
Chris Lattnerbb75d332011-02-13 08:07:21 +0000838 if (DivRHS->isOne()) {
839 // This eliminates some funny cases with INT_MIN.
840 ICI.setOperand(0, DivI->getOperand(0)); // X/1 == X.
841 return &ICI;
842 }
Chris Lattner02446fc2010-01-04 07:37:31 +0000843
844 // Compute Prod = CI * DivRHS. We are essentially solving an equation
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000845 // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and
846 // C2 (CI). By solving for X we can turn this into a range check
847 // instead of computing a divide.
Chris Lattner02446fc2010-01-04 07:37:31 +0000848 Constant *Prod = ConstantExpr::getMul(CmpRHS, DivRHS);
849
850 // Determine if the product overflows by seeing if the product is
851 // not equal to the divide. Make sure we do the same kind of divide
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000852 // as in the LHS instruction that we're folding.
Chris Lattner02446fc2010-01-04 07:37:31 +0000853 bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) :
854 ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS;
855
856 // Get the ICmp opcode
857 ICmpInst::Predicate Pred = ICI.getPredicate();
858
Chris Lattnerb20c0b52011-02-10 05:23:05 +0000859 /// If the division is known to be exact, then there is no remainder from the
860 /// divide, so the covered range size is unit, otherwise it is the divisor.
861 ConstantInt *RangeSize = DivI->isExact() ? getOne(Prod) : DivRHS;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000862
Chris Lattner02446fc2010-01-04 07:37:31 +0000863 // Figure out the interval that is being checked. For example, a comparison
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000864 // like "X /u 5 == 0" is really checking that X is in the interval [0, 5).
Chris Lattner02446fc2010-01-04 07:37:31 +0000865 // Compute this interval based on the constants involved and the signedness of
866 // the compare/divide. This computes a half-open interval, keeping track of
867 // whether either value in the interval overflows. After analysis each
868 // overflow variable is set to 0 if it's corresponding bound variable is valid
869 // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
870 int LoOverflow = 0, HiOverflow = 0;
871 Constant *LoBound = 0, *HiBound = 0;
Chris Lattnerb20c0b52011-02-10 05:23:05 +0000872
Chris Lattner02446fc2010-01-04 07:37:31 +0000873 if (!DivIsSigned) { // udiv
874 // e.g. X/5 op 3 --> [15, 20)
875 LoBound = Prod;
876 HiOverflow = LoOverflow = ProdOV;
Chris Lattnerb20c0b52011-02-10 05:23:05 +0000877 if (!HiOverflow) {
878 // If this is not an exact divide, then many values in the range collapse
879 // to the same result value.
880 HiOverflow = AddWithOverflow(HiBound, LoBound, RangeSize, false);
881 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000882
Chris Lattner02446fc2010-01-04 07:37:31 +0000883 } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0.
884 if (CmpRHSV == 0) { // (X / pos) op 0
885 // Can't overflow. e.g. X/2 op 0 --> [-1, 2)
Chris Lattnerb20c0b52011-02-10 05:23:05 +0000886 LoBound = ConstantExpr::getNeg(SubOne(RangeSize));
887 HiBound = RangeSize;
Chris Lattner02446fc2010-01-04 07:37:31 +0000888 } else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos
889 LoBound = Prod; // e.g. X/5 op 3 --> [15, 20)
890 HiOverflow = LoOverflow = ProdOV;
891 if (!HiOverflow)
Chris Lattnerb20c0b52011-02-10 05:23:05 +0000892 HiOverflow = AddWithOverflow(HiBound, Prod, RangeSize, true);
Chris Lattner02446fc2010-01-04 07:37:31 +0000893 } else { // (X / pos) op neg
894 // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14)
895 HiBound = AddOne(Prod);
896 LoOverflow = HiOverflow = ProdOV ? -1 : 0;
897 if (!LoOverflow) {
Chris Lattnerb20c0b52011-02-10 05:23:05 +0000898 ConstantInt *DivNeg =cast<ConstantInt>(ConstantExpr::getNeg(RangeSize));
Chris Lattner02446fc2010-01-04 07:37:31 +0000899 LoOverflow = AddWithOverflow(LoBound, HiBound, DivNeg, true) ? -1 : 0;
Chris Lattnerb20c0b52011-02-10 05:23:05 +0000900 }
Chris Lattner02446fc2010-01-04 07:37:31 +0000901 }
Chris Lattnerc73b24d2011-07-15 06:08:15 +0000902 } else if (DivRHS->isNegative()) { // Divisor is < 0.
Chris Lattnerb20c0b52011-02-10 05:23:05 +0000903 if (DivI->isExact())
904 RangeSize = cast<ConstantInt>(ConstantExpr::getNeg(RangeSize));
Chris Lattner02446fc2010-01-04 07:37:31 +0000905 if (CmpRHSV == 0) { // (X / neg) op 0
906 // e.g. X/-5 op 0 --> [-4, 5)
Chris Lattnerb20c0b52011-02-10 05:23:05 +0000907 LoBound = AddOne(RangeSize);
908 HiBound = cast<ConstantInt>(ConstantExpr::getNeg(RangeSize));
Chris Lattner02446fc2010-01-04 07:37:31 +0000909 if (HiBound == DivRHS) { // -INTMIN = INTMIN
910 HiOverflow = 1; // [INTMIN+1, overflow)
911 HiBound = 0; // e.g. X/INTMIN = 0 --> X > INTMIN
912 }
913 } else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos
914 // e.g. X/-5 op 3 --> [-19, -14)
915 HiBound = AddOne(Prod);
916 HiOverflow = LoOverflow = ProdOV ? -1 : 0;
917 if (!LoOverflow)
Chris Lattnerb20c0b52011-02-10 05:23:05 +0000918 LoOverflow = AddWithOverflow(LoBound, HiBound, RangeSize, true) ? -1:0;
Chris Lattner02446fc2010-01-04 07:37:31 +0000919 } else { // (X / neg) op neg
920 LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20)
921 LoOverflow = HiOverflow = ProdOV;
922 if (!HiOverflow)
Chris Lattnerb20c0b52011-02-10 05:23:05 +0000923 HiOverflow = SubWithOverflow(HiBound, Prod, RangeSize, true);
Chris Lattner02446fc2010-01-04 07:37:31 +0000924 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000925
Chris Lattner02446fc2010-01-04 07:37:31 +0000926 // Dividing by a negative swaps the condition. LT <-> GT
927 Pred = ICmpInst::getSwappedPredicate(Pred);
928 }
929
930 Value *X = DivI->getOperand(0);
931 switch (Pred) {
932 default: llvm_unreachable("Unhandled icmp opcode!");
933 case ICmpInst::ICMP_EQ:
934 if (LoOverflow && HiOverflow)
935 return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(ICI.getContext()));
Chris Lattnerf34f48c2010-03-05 08:46:26 +0000936 if (HiOverflow)
Chris Lattner02446fc2010-01-04 07:37:31 +0000937 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
938 ICmpInst::ICMP_UGE, X, LoBound);
Chris Lattnerf34f48c2010-03-05 08:46:26 +0000939 if (LoOverflow)
Chris Lattner02446fc2010-01-04 07:37:31 +0000940 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
941 ICmpInst::ICMP_ULT, X, HiBound);
Chris Lattnerb20c0b52011-02-10 05:23:05 +0000942 return ReplaceInstUsesWith(ICI, InsertRangeTest(X, LoBound, HiBound,
943 DivIsSigned, true));
Chris Lattner02446fc2010-01-04 07:37:31 +0000944 case ICmpInst::ICMP_NE:
945 if (LoOverflow && HiOverflow)
946 return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(ICI.getContext()));
Chris Lattnerf34f48c2010-03-05 08:46:26 +0000947 if (HiOverflow)
Chris Lattner02446fc2010-01-04 07:37:31 +0000948 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
949 ICmpInst::ICMP_ULT, X, LoBound);
Chris Lattnerf34f48c2010-03-05 08:46:26 +0000950 if (LoOverflow)
Chris Lattner02446fc2010-01-04 07:37:31 +0000951 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
952 ICmpInst::ICMP_UGE, X, HiBound);
Chris Lattnerf34f48c2010-03-05 08:46:26 +0000953 return ReplaceInstUsesWith(ICI, InsertRangeTest(X, LoBound, HiBound,
954 DivIsSigned, false));
Chris Lattner02446fc2010-01-04 07:37:31 +0000955 case ICmpInst::ICMP_ULT:
956 case ICmpInst::ICMP_SLT:
957 if (LoOverflow == +1) // Low bound is greater than input range.
958 return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(ICI.getContext()));
959 if (LoOverflow == -1) // Low bound is less than input range.
960 return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(ICI.getContext()));
961 return new ICmpInst(Pred, X, LoBound);
962 case ICmpInst::ICMP_UGT:
963 case ICmpInst::ICMP_SGT:
964 if (HiOverflow == +1) // High bound greater than input range.
965 return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(ICI.getContext()));
Chris Lattnerb20c0b52011-02-10 05:23:05 +0000966 if (HiOverflow == -1) // High bound less than input range.
Chris Lattner02446fc2010-01-04 07:37:31 +0000967 return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(ICI.getContext()));
968 if (Pred == ICmpInst::ICMP_UGT)
969 return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound);
Chris Lattnerb20c0b52011-02-10 05:23:05 +0000970 return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound);
Chris Lattner02446fc2010-01-04 07:37:31 +0000971 }
972}
973
Chris Lattner74542aa2011-02-13 07:43:07 +0000974/// FoldICmpShrCst - Handle "icmp(([al]shr X, cst1), cst2)".
975Instruction *InstCombiner::FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *Shr,
976 ConstantInt *ShAmt) {
Chris Lattner74542aa2011-02-13 07:43:07 +0000977 const APInt &CmpRHSV = cast<ConstantInt>(ICI.getOperand(1))->getValue();
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000978
Chris Lattner74542aa2011-02-13 07:43:07 +0000979 // Check that the shift amount is in range. If not, don't perform
980 // undefined shifts. When the shift is visited it will be
981 // simplified.
982 uint32_t TypeBits = CmpRHSV.getBitWidth();
983 uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
Chris Lattnerbb75d332011-02-13 08:07:21 +0000984 if (ShAmtVal >= TypeBits || ShAmtVal == 0)
Chris Lattner74542aa2011-02-13 07:43:07 +0000985 return 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000986
Chris Lattnerbb75d332011-02-13 08:07:21 +0000987 if (!ICI.isEquality()) {
988 // If we have an unsigned comparison and an ashr, we can't simplify this.
989 // Similarly for signed comparisons with lshr.
990 if (ICI.isSigned() != (Shr->getOpcode() == Instruction::AShr))
991 return 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000992
Eli Friedmana831a9b2011-05-25 23:26:20 +0000993 // Otherwise, all lshr and most exact ashr's are equivalent to a udiv/sdiv
994 // by a power of 2. Since we already have logic to simplify these,
995 // transform to div and then simplify the resultant comparison.
Chris Lattnerbb75d332011-02-13 08:07:21 +0000996 if (Shr->getOpcode() == Instruction::AShr &&
Eli Friedmana831a9b2011-05-25 23:26:20 +0000997 (!Shr->isExact() || ShAmtVal == TypeBits - 1))
Chris Lattnerbb75d332011-02-13 08:07:21 +0000998 return 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +0000999
Chris Lattnerbb75d332011-02-13 08:07:21 +00001000 // Revisit the shift (to delete it).
1001 Worklist.Add(Shr);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001002
Chris Lattnerbb75d332011-02-13 08:07:21 +00001003 Constant *DivCst =
1004 ConstantInt::get(Shr->getType(), APInt::getOneBitSet(TypeBits, ShAmtVal));
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001005
Chris Lattnerbb75d332011-02-13 08:07:21 +00001006 Value *Tmp =
1007 Shr->getOpcode() == Instruction::AShr ?
1008 Builder->CreateSDiv(Shr->getOperand(0), DivCst, "", Shr->isExact()) :
1009 Builder->CreateUDiv(Shr->getOperand(0), DivCst, "", Shr->isExact());
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001010
Chris Lattnerbb75d332011-02-13 08:07:21 +00001011 ICI.setOperand(0, Tmp);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001012
Chris Lattnerbb75d332011-02-13 08:07:21 +00001013 // If the builder folded the binop, just return it.
1014 BinaryOperator *TheDiv = dyn_cast<BinaryOperator>(Tmp);
1015 if (TheDiv == 0)
1016 return &ICI;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001017
Chris Lattnerbb75d332011-02-13 08:07:21 +00001018 // Otherwise, fold this div/compare.
1019 assert(TheDiv->getOpcode() == Instruction::SDiv ||
1020 TheDiv->getOpcode() == Instruction::UDiv);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001021
Chris Lattnerbb75d332011-02-13 08:07:21 +00001022 Instruction *Res = FoldICmpDivCst(ICI, TheDiv, cast<ConstantInt>(DivCst));
1023 assert(Res && "This div/cst should have folded!");
1024 return Res;
1025 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001026
1027
Chris Lattner74542aa2011-02-13 07:43:07 +00001028 // If we are comparing against bits always shifted out, the
1029 // comparison cannot succeed.
1030 APInt Comp = CmpRHSV << ShAmtVal;
1031 ConstantInt *ShiftedCmpRHS = ConstantInt::get(ICI.getContext(), Comp);
1032 if (Shr->getOpcode() == Instruction::LShr)
1033 Comp = Comp.lshr(ShAmtVal);
1034 else
1035 Comp = Comp.ashr(ShAmtVal);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001036
Chris Lattner74542aa2011-02-13 07:43:07 +00001037 if (Comp != CmpRHSV) { // Comparing against a bit that we know is zero.
1038 bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
1039 Constant *Cst = ConstantInt::get(Type::getInt1Ty(ICI.getContext()),
1040 IsICMP_NE);
1041 return ReplaceInstUsesWith(ICI, Cst);
1042 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001043
Chris Lattner74542aa2011-02-13 07:43:07 +00001044 // Otherwise, check to see if the bits shifted out are known to be zero.
1045 // If so, we can compare against the unshifted value:
1046 // (X & 4) >> 1 == 2 --> (X & 4) == 4.
Chris Lattnere5116f82011-02-13 18:30:09 +00001047 if (Shr->hasOneUse() && Shr->isExact())
Chris Lattner74542aa2011-02-13 07:43:07 +00001048 return new ICmpInst(ICI.getPredicate(), Shr->getOperand(0), ShiftedCmpRHS);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001049
Chris Lattner74542aa2011-02-13 07:43:07 +00001050 if (Shr->hasOneUse()) {
1051 // Otherwise strength reduce the shift into an and.
1052 APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
1053 Constant *Mask = ConstantInt::get(ICI.getContext(), Val);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001054
Chris Lattner74542aa2011-02-13 07:43:07 +00001055 Value *And = Builder->CreateAnd(Shr->getOperand(0),
1056 Mask, Shr->getName()+".mask");
1057 return new ICmpInst(ICI.getPredicate(), And, ShiftedCmpRHS);
1058 }
1059 return 0;
1060}
1061
Chris Lattner02446fc2010-01-04 07:37:31 +00001062
1063/// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)".
1064///
1065Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
1066 Instruction *LHSI,
1067 ConstantInt *RHS) {
1068 const APInt &RHSV = RHS->getValue();
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001069
Chris Lattner02446fc2010-01-04 07:37:31 +00001070 switch (LHSI->getOpcode()) {
1071 case Instruction::Trunc:
1072 if (ICI.isEquality() && LHSI->hasOneUse()) {
1073 // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
1074 // of the high bits truncated out of x are known.
1075 unsigned DstBits = LHSI->getType()->getPrimitiveSizeInBits(),
1076 SrcBits = LHSI->getOperand(0)->getType()->getPrimitiveSizeInBits();
Chris Lattner02446fc2010-01-04 07:37:31 +00001077 APInt KnownZero(SrcBits, 0), KnownOne(SrcBits, 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001078 ComputeMaskedBits(LHSI->getOperand(0), KnownZero, KnownOne);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001079
Chris Lattner02446fc2010-01-04 07:37:31 +00001080 // If all the high bits are known, we can do this xform.
1081 if ((KnownZero|KnownOne).countLeadingOnes() >= SrcBits-DstBits) {
1082 // Pull in the high bits from known-ones set.
Jay Foad40f8f622010-12-07 08:25:19 +00001083 APInt NewRHS = RHS->getValue().zext(SrcBits);
Eli Friedman5b6dfee2012-05-11 01:32:59 +00001084 NewRHS |= KnownOne & APInt::getHighBitsSet(SrcBits, SrcBits-DstBits);
Chris Lattner02446fc2010-01-04 07:37:31 +00001085 return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
1086 ConstantInt::get(ICI.getContext(), NewRHS));
1087 }
1088 }
1089 break;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001090
Chris Lattner02446fc2010-01-04 07:37:31 +00001091 case Instruction::Xor: // (icmp pred (xor X, XorCST), CI)
1092 if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) {
1093 // If this is a comparison that tests the signbit (X < 0) or (x > -1),
1094 // fold the xor.
1095 if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) ||
1096 (ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) {
1097 Value *CompareVal = LHSI->getOperand(0);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001098
Chris Lattner02446fc2010-01-04 07:37:31 +00001099 // If the sign bit of the XorCST is not set, there is no change to
1100 // the operation, just stop using the Xor.
Chris Lattnerc73b24d2011-07-15 06:08:15 +00001101 if (!XorCST->isNegative()) {
Chris Lattner02446fc2010-01-04 07:37:31 +00001102 ICI.setOperand(0, CompareVal);
1103 Worklist.Add(LHSI);
1104 return &ICI;
1105 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001106
Chris Lattner02446fc2010-01-04 07:37:31 +00001107 // Was the old condition true if the operand is positive?
1108 bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001109
Chris Lattner02446fc2010-01-04 07:37:31 +00001110 // If so, the new one isn't.
1111 isTrueIfPositive ^= true;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001112
Chris Lattner02446fc2010-01-04 07:37:31 +00001113 if (isTrueIfPositive)
1114 return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal,
1115 SubOne(RHS));
1116 else
1117 return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal,
1118 AddOne(RHS));
1119 }
1120
1121 if (LHSI->hasOneUse()) {
1122 // (icmp u/s (xor A SignBit), C) -> (icmp s/u A, (xor C SignBit))
1123 if (!ICI.isEquality() && XorCST->getValue().isSignBit()) {
1124 const APInt &SignBit = XorCST->getValue();
1125 ICmpInst::Predicate Pred = ICI.isSigned()
1126 ? ICI.getUnsignedPredicate()
1127 : ICI.getSignedPredicate();
1128 return new ICmpInst(Pred, LHSI->getOperand(0),
1129 ConstantInt::get(ICI.getContext(),
1130 RHSV ^ SignBit));
1131 }
1132
1133 // (icmp u/s (xor A ~SignBit), C) -> (icmp s/u (xor C ~SignBit), A)
Chris Lattnerc73b24d2011-07-15 06:08:15 +00001134 if (!ICI.isEquality() && XorCST->isMaxValue(true)) {
Chris Lattner02446fc2010-01-04 07:37:31 +00001135 const APInt &NotSignBit = XorCST->getValue();
1136 ICmpInst::Predicate Pred = ICI.isSigned()
1137 ? ICI.getUnsignedPredicate()
1138 : ICI.getSignedPredicate();
1139 Pred = ICI.getSwappedPredicate(Pred);
1140 return new ICmpInst(Pred, LHSI->getOperand(0),
1141 ConstantInt::get(ICI.getContext(),
1142 RHSV ^ NotSignBit));
1143 }
1144 }
1145 }
1146 break;
1147 case Instruction::And: // (icmp pred (and X, AndCST), RHS)
1148 if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) &&
1149 LHSI->getOperand(0)->hasOneUse()) {
1150 ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1));
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001151
Chris Lattner02446fc2010-01-04 07:37:31 +00001152 // If the LHS is an AND of a truncating cast, we can widen the
1153 // and/compare to be the input width without changing the value
1154 // produced, eliminating a cast.
1155 if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) {
1156 // We can do this transformation if either the AND constant does not
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001157 // have its sign bit set or if it is an equality comparison.
Chris Lattner02446fc2010-01-04 07:37:31 +00001158 // Extending a relational comparison when we're checking the sign
1159 // bit would not work.
Benjamin Kramer7e7c9cc2011-06-12 22:47:53 +00001160 if (ICI.isEquality() ||
Chris Lattnerc73b24d2011-07-15 06:08:15 +00001161 (!AndCST->isNegative() && RHSV.isNonNegative())) {
Benjamin Kramer7e7c9cc2011-06-12 22:47:53 +00001162 Value *NewAnd =
Chris Lattner02446fc2010-01-04 07:37:31 +00001163 Builder->CreateAnd(Cast->getOperand(0),
Benjamin Kramer7e7c9cc2011-06-12 22:47:53 +00001164 ConstantExpr::getZExt(AndCST, Cast->getSrcTy()));
1165 NewAnd->takeName(LHSI);
Chris Lattner02446fc2010-01-04 07:37:31 +00001166 return new ICmpInst(ICI.getPredicate(), NewAnd,
Benjamin Kramer7e7c9cc2011-06-12 22:47:53 +00001167 ConstantExpr::getZExt(RHS, Cast->getSrcTy()));
Chris Lattner02446fc2010-01-04 07:37:31 +00001168 }
1169 }
Benjamin Kramerffd0ae62011-06-12 22:48:00 +00001170
1171 // If the LHS is an AND of a zext, and we have an equality compare, we can
1172 // shrink the and/compare to the smaller type, eliminating the cast.
1173 if (ZExtInst *Cast = dyn_cast<ZExtInst>(LHSI->getOperand(0))) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001174 IntegerType *Ty = cast<IntegerType>(Cast->getSrcTy());
Benjamin Kramerffd0ae62011-06-12 22:48:00 +00001175 // Make sure we don't compare the upper bits, SimplifyDemandedBits
1176 // should fold the icmp to true/false in that case.
1177 if (ICI.isEquality() && RHSV.getActiveBits() <= Ty->getBitWidth()) {
1178 Value *NewAnd =
1179 Builder->CreateAnd(Cast->getOperand(0),
1180 ConstantExpr::getTrunc(AndCST, Ty));
1181 NewAnd->takeName(LHSI);
1182 return new ICmpInst(ICI.getPredicate(), NewAnd,
1183 ConstantExpr::getTrunc(RHS, Ty));
1184 }
1185 }
1186
Chris Lattner02446fc2010-01-04 07:37:31 +00001187 // If this is: (X >> C1) & C2 != C3 (where any shift and any compare
1188 // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This
1189 // happens a LOT in code produced by the C front-end, for bitfield
1190 // access.
1191 BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0));
1192 if (Shift && !Shift->isShift())
1193 Shift = 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001194
Chris Lattner02446fc2010-01-04 07:37:31 +00001195 ConstantInt *ShAmt;
1196 ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001197 Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift.
1198 Type *AndTy = AndCST->getType(); // Type of the and.
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001199
Chris Lattner02446fc2010-01-04 07:37:31 +00001200 // We can fold this as long as we can't shift unknown bits
1201 // into the mask. This can only happen with signed shift
1202 // rights, as they sign-extend.
1203 if (ShAmt) {
1204 bool CanFold = Shift->isLogicalShift();
1205 if (!CanFold) {
1206 // To test for the bad case of the signed shr, see if any
1207 // of the bits shifted in could be tested after the mask.
1208 uint32_t TyBits = Ty->getPrimitiveSizeInBits();
1209 int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001210
Chris Lattner02446fc2010-01-04 07:37:31 +00001211 uint32_t BitWidth = AndTy->getPrimitiveSizeInBits();
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001212 if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) &
Chris Lattner02446fc2010-01-04 07:37:31 +00001213 AndCST->getValue()) == 0)
1214 CanFold = true;
1215 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001216
Chris Lattner02446fc2010-01-04 07:37:31 +00001217 if (CanFold) {
1218 Constant *NewCst;
1219 if (Shift->getOpcode() == Instruction::Shl)
1220 NewCst = ConstantExpr::getLShr(RHS, ShAmt);
1221 else
1222 NewCst = ConstantExpr::getShl(RHS, ShAmt);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001223
Chris Lattner02446fc2010-01-04 07:37:31 +00001224 // Check to see if we are shifting out any of the bits being
1225 // compared.
1226 if (ConstantExpr::get(Shift->getOpcode(),
1227 NewCst, ShAmt) != RHS) {
1228 // If we shifted bits out, the fold is not going to work out.
1229 // As a special case, check to see if this means that the
1230 // result is always true or false now.
1231 if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
1232 return ReplaceInstUsesWith(ICI,
1233 ConstantInt::getFalse(ICI.getContext()));
1234 if (ICI.getPredicate() == ICmpInst::ICMP_NE)
1235 return ReplaceInstUsesWith(ICI,
1236 ConstantInt::getTrue(ICI.getContext()));
1237 } else {
1238 ICI.setOperand(1, NewCst);
1239 Constant *NewAndCST;
1240 if (Shift->getOpcode() == Instruction::Shl)
1241 NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt);
1242 else
1243 NewAndCST = ConstantExpr::getShl(AndCST, ShAmt);
1244 LHSI->setOperand(1, NewAndCST);
1245 LHSI->setOperand(0, Shift->getOperand(0));
1246 Worklist.Add(Shift); // Shift is dead.
1247 return &ICI;
1248 }
1249 }
1250 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001251
Chris Lattner02446fc2010-01-04 07:37:31 +00001252 // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is
1253 // preferable because it allows the C<<Y expression to be hoisted out
1254 // of a loop if Y is invariant and X is not.
1255 if (Shift && Shift->hasOneUse() && RHSV == 0 &&
1256 ICI.isEquality() && !Shift->isArithmeticShift() &&
1257 !isa<Constant>(Shift->getOperand(0))) {
1258 // Compute C << Y.
1259 Value *NS;
1260 if (Shift->getOpcode() == Instruction::LShr) {
Benjamin Kramera9390a42011-09-27 20:39:19 +00001261 NS = Builder->CreateShl(AndCST, Shift->getOperand(1));
Chris Lattner02446fc2010-01-04 07:37:31 +00001262 } else {
1263 // Insert a logical shift.
Benjamin Kramera9390a42011-09-27 20:39:19 +00001264 NS = Builder->CreateLShr(AndCST, Shift->getOperand(1));
Chris Lattner02446fc2010-01-04 07:37:31 +00001265 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001266
Chris Lattner02446fc2010-01-04 07:37:31 +00001267 // Compute X & (C << Y).
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001268 Value *NewAnd =
Chris Lattner02446fc2010-01-04 07:37:31 +00001269 Builder->CreateAnd(Shift->getOperand(0), NS, LHSI->getName());
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001270
Chris Lattner02446fc2010-01-04 07:37:31 +00001271 ICI.setOperand(0, NewAnd);
1272 return &ICI;
1273 }
Paul Redmond6da2e222012-12-19 19:47:13 +00001274
1275 // Replace ((X & AndCST) > RHSV) with ((X & AndCST) != 0), if any
1276 // bit set in (X & AndCST) will produce a result greater than RHSV.
1277 if (ICI.getPredicate() == ICmpInst::ICMP_UGT) {
1278 unsigned NTZ = AndCST->getValue().countTrailingZeros();
1279 if ((NTZ < AndCST->getBitWidth()) &&
1280 APInt::getOneBitSet(AndCST->getBitWidth(), NTZ).ugt(RHSV))
1281 return new ICmpInst(ICmpInst::ICMP_NE, LHSI,
1282 Constant::getNullValue(RHS->getType()));
1283 }
Chris Lattner02446fc2010-01-04 07:37:31 +00001284 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001285
Chris Lattner02446fc2010-01-04 07:37:31 +00001286 // Try to optimize things like "A[i]&42 == 0" to index computations.
1287 if (LoadInst *LI = dyn_cast<LoadInst>(LHSI->getOperand(0))) {
1288 if (GetElementPtrInst *GEP =
1289 dyn_cast<GetElementPtrInst>(LI->getOperand(0)))
1290 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
1291 if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
1292 !LI->isVolatile() && isa<ConstantInt>(LHSI->getOperand(1))) {
1293 ConstantInt *C = cast<ConstantInt>(LHSI->getOperand(1));
1294 if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV,ICI, C))
1295 return Res;
1296 }
1297 }
1298 break;
1299
1300 case Instruction::Or: {
1301 if (!ICI.isEquality() || !RHS->isNullValue() || !LHSI->hasOneUse())
1302 break;
1303 Value *P, *Q;
1304 if (match(LHSI, m_Or(m_PtrToInt(m_Value(P)), m_PtrToInt(m_Value(Q))))) {
1305 // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0
1306 // -> and (icmp eq P, null), (icmp eq Q, null).
Chris Lattner02446fc2010-01-04 07:37:31 +00001307 Value *ICIP = Builder->CreateICmp(ICI.getPredicate(), P,
1308 Constant::getNullValue(P->getType()));
1309 Value *ICIQ = Builder->CreateICmp(ICI.getPredicate(), Q,
1310 Constant::getNullValue(Q->getType()));
1311 Instruction *Op;
1312 if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
1313 Op = BinaryOperator::CreateAnd(ICIP, ICIQ);
1314 else
1315 Op = BinaryOperator::CreateOr(ICIP, ICIQ);
1316 return Op;
1317 }
1318 break;
1319 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001320
Arnaud A. de Grandmaison35763b12013-03-25 09:48:49 +00001321 case Instruction::Mul: { // (icmp pred (mul X, Val), CI)
1322 ConstantInt *Val = dyn_cast<ConstantInt>(LHSI->getOperand(1));
1323 if (!Val) break;
1324
1325 if (!ICI.isEquality()) {
1326 // If this is a signed comparison to 0 and the mul is sign preserving,
1327 // use the mul LHS operand instead.
1328 ICmpInst::Predicate pred = ICI.getPredicate();
1329 if (isSignTest(pred, RHS) && !Val->isZero() &&
1330 cast<BinaryOperator>(LHSI)->hasNoSignedWrap())
1331 return new ICmpInst(Val->isNegative() ?
1332 ICmpInst::getSwappedPredicate(pred) : pred,
1333 LHSI->getOperand(0),
1334 Constant::getNullValue(RHS->getType()));
1335 }
1336
1337 break;
1338 }
1339
Chris Lattner02446fc2010-01-04 07:37:31 +00001340 case Instruction::Shl: { // (icmp pred (shl X, ShAmt), CI)
1341 ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1));
1342 if (!ShAmt) break;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001343
Chris Lattner02446fc2010-01-04 07:37:31 +00001344 uint32_t TypeBits = RHSV.getBitWidth();
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001345
Chris Lattner02446fc2010-01-04 07:37:31 +00001346 // Check that the shift amount is in range. If not, don't perform
1347 // undefined shifts. When the shift is visited it will be
1348 // simplified.
1349 if (ShAmt->uge(TypeBits))
1350 break;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001351
Chris Lattner02446fc2010-01-04 07:37:31 +00001352 if (ICI.isEquality()) {
1353 // If we are comparing against bits always shifted out, the
1354 // comparison cannot succeed.
1355 Constant *Comp =
1356 ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt),
1357 ShAmt);
1358 if (Comp != RHS) {// Comparing against a bit that we know is zero.
1359 bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
1360 Constant *Cst =
1361 ConstantInt::get(Type::getInt1Ty(ICI.getContext()), IsICMP_NE);
1362 return ReplaceInstUsesWith(ICI, Cst);
1363 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001364
Chris Lattnerb20c0b52011-02-10 05:23:05 +00001365 // If the shift is NUW, then it is just shifting out zeros, no need for an
1366 // AND.
1367 if (cast<BinaryOperator>(LHSI)->hasNoUnsignedWrap())
1368 return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
1369 ConstantExpr::getLShr(RHS, ShAmt));
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001370
Arnaud A. de Grandmaison35763b12013-03-25 09:48:49 +00001371 // If the shift is NSW and we compare to 0, then it is just shifting out
1372 // sign bits, no need for an AND either.
1373 if (cast<BinaryOperator>(LHSI)->hasNoSignedWrap() && RHSV == 0)
1374 return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
1375 ConstantExpr::getLShr(RHS, ShAmt));
1376
Chris Lattner02446fc2010-01-04 07:37:31 +00001377 if (LHSI->hasOneUse()) {
1378 // Otherwise strength reduce the shift into an and.
1379 uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
1380 Constant *Mask =
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001381 ConstantInt::get(ICI.getContext(), APInt::getLowBitsSet(TypeBits,
Chris Lattner02446fc2010-01-04 07:37:31 +00001382 TypeBits-ShAmtVal));
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001383
Chris Lattner02446fc2010-01-04 07:37:31 +00001384 Value *And =
1385 Builder->CreateAnd(LHSI->getOperand(0),Mask, LHSI->getName()+".mask");
1386 return new ICmpInst(ICI.getPredicate(), And,
Chris Lattnerb20c0b52011-02-10 05:23:05 +00001387 ConstantExpr::getLShr(RHS, ShAmt));
Chris Lattner02446fc2010-01-04 07:37:31 +00001388 }
1389 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001390
Arnaud A. de Grandmaison35763b12013-03-25 09:48:49 +00001391 // If this is a signed comparison to 0 and the shift is sign preserving,
1392 // use the shift LHS operand instead.
1393 ICmpInst::Predicate pred = ICI.getPredicate();
1394 if (isSignTest(pred, RHS) &&
1395 cast<BinaryOperator>(LHSI)->hasNoSignedWrap())
1396 return new ICmpInst(pred,
1397 LHSI->getOperand(0),
1398 Constant::getNullValue(RHS->getType()));
1399
Chris Lattner02446fc2010-01-04 07:37:31 +00001400 // Otherwise, if this is a comparison of the sign bit, simplify to and/test.
1401 bool TrueIfSigned = false;
1402 if (LHSI->hasOneUse() &&
1403 isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) {
1404 // (X << 31) <s 0 --> (X&1) != 0
Chris Lattnerbb75d332011-02-13 08:07:21 +00001405 Constant *Mask = ConstantInt::get(LHSI->getOperand(0)->getType(),
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001406 APInt::getOneBitSet(TypeBits,
Chris Lattnerbb75d332011-02-13 08:07:21 +00001407 TypeBits-ShAmt->getZExtValue()-1));
Chris Lattner02446fc2010-01-04 07:37:31 +00001408 Value *And =
1409 Builder->CreateAnd(LHSI->getOperand(0), Mask, LHSI->getName()+".mask");
1410 return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
1411 And, Constant::getNullValue(And->getType()));
1412 }
Arnaud A. de Grandmaison7c5c9b32013-02-15 14:35:47 +00001413
1414 // Transform (icmp pred iM (shl iM %v, N), CI)
Arnaud A. de Grandmaisonbdd2d982013-03-13 14:40:37 +00001415 // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (CI>>N))
1416 // Transform the shl to a trunc if (trunc (CI>>N)) has no loss and M-N.
Arnaud A. de Grandmaison7c5c9b32013-02-15 14:35:47 +00001417 // This enables to get rid of the shift in favor of a trunc which can be
1418 // free on the target. It has the additional benefit of comparing to a
1419 // smaller constant, which will be target friendly.
1420 unsigned Amt = ShAmt->getLimitedValue(TypeBits-1);
Arnaud A. de Grandmaisonbdd2d982013-03-13 14:40:37 +00001421 if (LHSI->hasOneUse() &&
1422 Amt != 0 && RHSV.countTrailingZeros() >= Amt) {
Arnaud A. de Grandmaison7c5c9b32013-02-15 14:35:47 +00001423 Type *NTy = IntegerType::get(ICI.getContext(), TypeBits - Amt);
1424 Constant *NCI = ConstantExpr::getTrunc(
1425 ConstantExpr::getAShr(RHS,
1426 ConstantInt::get(RHS->getType(), Amt)),
1427 NTy);
1428 return new ICmpInst(ICI.getPredicate(),
1429 Builder->CreateTrunc(LHSI->getOperand(0), NTy),
Arnaud A. de Grandmaisonad079b22013-02-15 15:18:17 +00001430 NCI);
Arnaud A. de Grandmaison7c5c9b32013-02-15 14:35:47 +00001431 }
1432
Chris Lattner02446fc2010-01-04 07:37:31 +00001433 break;
1434 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001435
Chris Lattner02446fc2010-01-04 07:37:31 +00001436 case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI)
Nick Lewyckyb042f8e2011-02-28 08:31:40 +00001437 case Instruction::AShr: {
1438 // Handle equality comparisons of shift-by-constant.
1439 BinaryOperator *BO = cast<BinaryOperator>(LHSI);
1440 if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1))) {
1441 if (Instruction *Res = FoldICmpShrCst(ICI, BO, ShAmt))
Chris Lattner74542aa2011-02-13 07:43:07 +00001442 return Res;
Nick Lewyckyb042f8e2011-02-28 08:31:40 +00001443 }
1444
1445 // Handle exact shr's.
1446 if (ICI.isEquality() && BO->isExact() && BO->hasOneUse()) {
1447 if (RHSV.isMinValue())
1448 return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), RHS);
1449 }
Chris Lattner02446fc2010-01-04 07:37:31 +00001450 break;
Nick Lewyckyb042f8e2011-02-28 08:31:40 +00001451 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001452
Chris Lattner02446fc2010-01-04 07:37:31 +00001453 case Instruction::SDiv:
1454 case Instruction::UDiv:
1455 // Fold: icmp pred ([us]div X, C1), C2 -> range test
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001456 // Fold this div into the comparison, producing a range check.
1457 // Determine, based on the divide type, what the range is being
1458 // checked. If there is an overflow on the low or high side, remember
Chris Lattner02446fc2010-01-04 07:37:31 +00001459 // it, otherwise compute the range [low, hi) bounding the new value.
1460 // See: InsertRangeTest above for the kinds of replacements possible.
1461 if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1)))
1462 if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI),
1463 DivRHS))
1464 return R;
1465 break;
1466
1467 case Instruction::Add:
1468 // Fold: icmp pred (add X, C1), C2
1469 if (!ICI.isEquality()) {
1470 ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1));
1471 if (!LHSC) break;
1472 const APInt &LHSV = LHSC->getValue();
1473
1474 ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV)
1475 .subtract(LHSV);
1476
1477 if (ICI.isSigned()) {
1478 if (CR.getLower().isSignBit()) {
1479 return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0),
1480 ConstantInt::get(ICI.getContext(),CR.getUpper()));
1481 } else if (CR.getUpper().isSignBit()) {
1482 return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0),
1483 ConstantInt::get(ICI.getContext(),CR.getLower()));
1484 }
1485 } else {
1486 if (CR.getLower().isMinValue()) {
1487 return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0),
1488 ConstantInt::get(ICI.getContext(),CR.getUpper()));
1489 } else if (CR.getUpper().isMinValue()) {
1490 return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0),
1491 ConstantInt::get(ICI.getContext(),CR.getLower()));
1492 }
1493 }
1494 }
1495 break;
1496 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001497
Chris Lattner02446fc2010-01-04 07:37:31 +00001498 // Simplify icmp_eq and icmp_ne instructions with integer constant RHS.
1499 if (ICI.isEquality()) {
1500 bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001501
1502 // If the first operand is (add|sub|and|or|xor|rem) with a constant, and
Chris Lattner02446fc2010-01-04 07:37:31 +00001503 // the second operand is a constant, simplify a bit.
1504 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) {
1505 switch (BO->getOpcode()) {
1506 case Instruction::SRem:
1507 // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
1508 if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){
1509 const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue();
Dan Gohmane0567812010-04-08 23:03:40 +00001510 if (V.sgt(1) && V.isPowerOf2()) {
Chris Lattner02446fc2010-01-04 07:37:31 +00001511 Value *NewRem =
1512 Builder->CreateURem(BO->getOperand(0), BO->getOperand(1),
1513 BO->getName());
1514 return new ICmpInst(ICI.getPredicate(), NewRem,
1515 Constant::getNullValue(BO->getType()));
1516 }
1517 }
1518 break;
1519 case Instruction::Add:
1520 // Replace ((add A, B) != C) with (A != C-B) if B & C are constants.
1521 if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) {
1522 if (BO->hasOneUse())
1523 return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
1524 ConstantExpr::getSub(RHS, BOp1C));
1525 } else if (RHSV == 0) {
1526 // Replace ((add A, B) != 0) with (A != -B) if A or B is
1527 // efficiently invertible, or if the add has just this one use.
1528 Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001529
Chris Lattner02446fc2010-01-04 07:37:31 +00001530 if (Value *NegVal = dyn_castNegVal(BOp1))
1531 return new ICmpInst(ICI.getPredicate(), BOp0, NegVal);
Chris Lattner5036ce42011-04-26 20:02:45 +00001532 if (Value *NegVal = dyn_castNegVal(BOp0))
Chris Lattner02446fc2010-01-04 07:37:31 +00001533 return new ICmpInst(ICI.getPredicate(), NegVal, BOp1);
Chris Lattner5036ce42011-04-26 20:02:45 +00001534 if (BO->hasOneUse()) {
Chris Lattner02446fc2010-01-04 07:37:31 +00001535 Value *Neg = Builder->CreateNeg(BOp1);
1536 Neg->takeName(BO);
1537 return new ICmpInst(ICI.getPredicate(), BOp0, Neg);
1538 }
1539 }
1540 break;
1541 case Instruction::Xor:
1542 // For the xor case, we can xor two constants together, eliminating
1543 // the explicit xor.
Benjamin Kramere7fdcad2011-06-13 15:24:24 +00001544 if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) {
1545 return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
Chris Lattner02446fc2010-01-04 07:37:31 +00001546 ConstantExpr::getXor(RHS, BOC));
Benjamin Kramere7fdcad2011-06-13 15:24:24 +00001547 } else if (RHSV == 0) {
1548 // Replace ((xor A, B) != 0) with (A != B)
Chris Lattner02446fc2010-01-04 07:37:31 +00001549 return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
1550 BO->getOperand(1));
Benjamin Kramere7fdcad2011-06-13 15:24:24 +00001551 }
Chris Lattner02446fc2010-01-04 07:37:31 +00001552 break;
Benjamin Kramere7fdcad2011-06-13 15:24:24 +00001553 case Instruction::Sub:
1554 // Replace ((sub A, B) != C) with (B != A-C) if A & C are constants.
1555 if (ConstantInt *BOp0C = dyn_cast<ConstantInt>(BO->getOperand(0))) {
1556 if (BO->hasOneUse())
1557 return new ICmpInst(ICI.getPredicate(), BO->getOperand(1),
1558 ConstantExpr::getSub(BOp0C, RHS));
1559 } else if (RHSV == 0) {
1560 // Replace ((sub A, B) != 0) with (A != B)
1561 return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
1562 BO->getOperand(1));
1563 }
1564 break;
Chris Lattner02446fc2010-01-04 07:37:31 +00001565 case Instruction::Or:
1566 // If bits are being or'd in that are not present in the constant we
1567 // are comparing against, then the comparison could never succeed!
Eli Friedman618898e2010-07-29 18:03:33 +00001568 if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
Chris Lattner02446fc2010-01-04 07:37:31 +00001569 Constant *NotCI = ConstantExpr::getNot(RHS);
1570 if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue())
1571 return ReplaceInstUsesWith(ICI,
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001572 ConstantInt::get(Type::getInt1Ty(ICI.getContext()),
Chris Lattner02446fc2010-01-04 07:37:31 +00001573 isICMP_NE));
1574 }
1575 break;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001576
Chris Lattner02446fc2010-01-04 07:37:31 +00001577 case Instruction::And:
1578 if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
1579 // If bits are being compared against that are and'd out, then the
1580 // comparison can never succeed!
1581 if ((RHSV & ~BOC->getValue()) != 0)
1582 return ReplaceInstUsesWith(ICI,
1583 ConstantInt::get(Type::getInt1Ty(ICI.getContext()),
1584 isICMP_NE));
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001585
Chris Lattner02446fc2010-01-04 07:37:31 +00001586 // If we have ((X & C) == C), turn it into ((X & C) != 0).
1587 if (RHS == BOC && RHSV.isPowerOf2())
1588 return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ :
1589 ICmpInst::ICMP_NE, LHSI,
1590 Constant::getNullValue(RHS->getType()));
Benjamin Kramerfc87cdc2011-07-04 20:16:36 +00001591
1592 // Don't perform the following transforms if the AND has multiple uses
1593 if (!BO->hasOneUse())
1594 break;
1595
Chris Lattner02446fc2010-01-04 07:37:31 +00001596 // Replace (and X, (1 << size(X)-1) != 0) with x s< 0
1597 if (BOC->getValue().isSignBit()) {
1598 Value *X = BO->getOperand(0);
1599 Constant *Zero = Constant::getNullValue(X->getType());
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001600 ICmpInst::Predicate pred = isICMP_NE ?
Chris Lattner02446fc2010-01-04 07:37:31 +00001601 ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
1602 return new ICmpInst(pred, X, Zero);
1603 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001604
Chris Lattner02446fc2010-01-04 07:37:31 +00001605 // ((X & ~7) == 0) --> X < 8
1606 if (RHSV == 0 && isHighOnes(BOC)) {
1607 Value *X = BO->getOperand(0);
1608 Constant *NegX = ConstantExpr::getNeg(BOC);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001609 ICmpInst::Predicate pred = isICMP_NE ?
Chris Lattner02446fc2010-01-04 07:37:31 +00001610 ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
1611 return new ICmpInst(pred, X, NegX);
1612 }
1613 }
Arnaud A. de Grandmaison35763b12013-03-25 09:48:49 +00001614 break;
1615 case Instruction::Mul:
1616 if (RHSV == 0) {
1617 if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
1618 // The trivial case (mul X, 0) is handled by InstSimplify
1619 // General case : (mul X, C) != 0 iff X != 0
1620 // (mul X, C) == 0 iff X == 0
1621 if (!BOC->isZero())
1622 return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
1623 Constant::getNullValue(RHS->getType()));
1624 }
1625 }
1626 break;
Chris Lattner02446fc2010-01-04 07:37:31 +00001627 default: break;
1628 }
1629 } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) {
1630 // Handle icmp {eq|ne} <intrinsic>, intcst.
Chris Lattner03357402010-01-05 18:09:56 +00001631 switch (II->getIntrinsicID()) {
1632 case Intrinsic::bswap:
Chris Lattner02446fc2010-01-04 07:37:31 +00001633 Worklist.Add(II);
Gabor Greifcaf70b32010-06-24 16:11:44 +00001634 ICI.setOperand(0, II->getArgOperand(0));
Chris Lattner02446fc2010-01-04 07:37:31 +00001635 ICI.setOperand(1, ConstantInt::get(II->getContext(), RHSV.byteSwap()));
1636 return &ICI;
Chris Lattner03357402010-01-05 18:09:56 +00001637 case Intrinsic::ctlz:
1638 case Intrinsic::cttz:
1639 // ctz(A) == bitwidth(a) -> A == 0 and likewise for !=
1640 if (RHSV == RHS->getType()->getBitWidth()) {
1641 Worklist.Add(II);
Gabor Greifcaf70b32010-06-24 16:11:44 +00001642 ICI.setOperand(0, II->getArgOperand(0));
Chris Lattner03357402010-01-05 18:09:56 +00001643 ICI.setOperand(1, ConstantInt::get(RHS->getType(), 0));
1644 return &ICI;
1645 }
1646 break;
1647 case Intrinsic::ctpop:
1648 // popcount(A) == 0 -> A == 0 and likewise for !=
1649 if (RHS->isZero()) {
1650 Worklist.Add(II);
Gabor Greifcaf70b32010-06-24 16:11:44 +00001651 ICI.setOperand(0, II->getArgOperand(0));
Chris Lattner03357402010-01-05 18:09:56 +00001652 ICI.setOperand(1, RHS);
1653 return &ICI;
1654 }
1655 break;
1656 default:
Duncan Sands34727662010-07-12 08:16:59 +00001657 break;
Chris Lattner02446fc2010-01-04 07:37:31 +00001658 }
1659 }
1660 }
1661 return 0;
1662}
1663
1664/// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst).
1665/// We only handle extending casts so far.
1666///
1667Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) {
1668 const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0));
1669 Value *LHSCIOp = LHSCI->getOperand(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001670 Type *SrcTy = LHSCIOp->getType();
1671 Type *DestTy = LHSCI->getType();
Chris Lattner02446fc2010-01-04 07:37:31 +00001672 Value *RHSCIOp;
1673
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001674 // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
Chris Lattner02446fc2010-01-04 07:37:31 +00001675 // integer type is the same size as the pointer type.
1676 if (TD && LHSCI->getOpcode() == Instruction::PtrToInt &&
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001677 TD->getPointerSizeInBits() ==
Chris Lattner02446fc2010-01-04 07:37:31 +00001678 cast<IntegerType>(DestTy)->getBitWidth()) {
1679 Value *RHSOp = 0;
1680 if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) {
1681 RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy);
1682 } else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) {
1683 RHSOp = RHSC->getOperand(0);
1684 // If the pointer types don't match, insert a bitcast.
1685 if (LHSCIOp->getType() != RHSOp->getType())
1686 RHSOp = Builder->CreateBitCast(RHSOp, LHSCIOp->getType());
1687 }
1688
1689 if (RHSOp)
1690 return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp);
1691 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001692
Chris Lattner02446fc2010-01-04 07:37:31 +00001693 // The code below only handles extension cast instructions, so far.
1694 // Enforce this.
1695 if (LHSCI->getOpcode() != Instruction::ZExt &&
1696 LHSCI->getOpcode() != Instruction::SExt)
1697 return 0;
1698
1699 bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt;
1700 bool isSignedCmp = ICI.isSigned();
1701
1702 if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) {
1703 // Not an extension from the same type?
1704 RHSCIOp = CI->getOperand(0);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001705 if (RHSCIOp->getType() != LHSCIOp->getType())
Chris Lattner02446fc2010-01-04 07:37:31 +00001706 return 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001707
Chris Lattner02446fc2010-01-04 07:37:31 +00001708 // If the signedness of the two casts doesn't agree (i.e. one is a sext
1709 // and the other is a zext), then we can't handle this.
1710 if (CI->getOpcode() != LHSCI->getOpcode())
1711 return 0;
1712
1713 // Deal with equality cases early.
1714 if (ICI.isEquality())
1715 return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
1716
1717 // A signed comparison of sign extended values simplifies into a
1718 // signed comparison.
1719 if (isSignedCmp && isSignedExt)
1720 return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
1721
1722 // The other three cases all fold into an unsigned comparison.
1723 return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp);
1724 }
1725
1726 // If we aren't dealing with a constant on the RHS, exit early
1727 ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1));
1728 if (!CI)
1729 return 0;
1730
1731 // Compute the constant that would happen if we truncated to SrcTy then
1732 // reextended to DestTy.
1733 Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy);
1734 Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(),
1735 Res1, DestTy);
1736
1737 // If the re-extended constant didn't change...
1738 if (Res2 == CI) {
1739 // Deal with equality cases early.
1740 if (ICI.isEquality())
1741 return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1);
1742
1743 // A signed comparison of sign extended values simplifies into a
1744 // signed comparison.
1745 if (isSignedExt && isSignedCmp)
1746 return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1);
1747
1748 // The other three cases all fold into an unsigned comparison.
1749 return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, Res1);
1750 }
1751
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001752 // The re-extended constant changed so the constant cannot be represented
Chris Lattner02446fc2010-01-04 07:37:31 +00001753 // in the shorter type. Consequently, we cannot emit a simple comparison.
Duncan Sands9d32f602011-01-20 13:21:55 +00001754 // All the cases that fold to true or false will have already been handled
1755 // by SimplifyICmpInst, so only deal with the tricky case.
Chris Lattner02446fc2010-01-04 07:37:31 +00001756
Duncan Sands9d32f602011-01-20 13:21:55 +00001757 if (isSignedCmp || !isSignedExt)
1758 return 0;
Chris Lattner02446fc2010-01-04 07:37:31 +00001759
1760 // Evaluate the comparison for LT (we invert for GT below). LE and GE cases
1761 // should have been folded away previously and not enter in here.
Duncan Sands9d32f602011-01-20 13:21:55 +00001762
1763 // We're performing an unsigned comp with a sign extended value.
1764 // This is true if the input is >= 0. [aka >s -1]
1765 Constant *NegOne = Constant::getAllOnesValue(SrcTy);
1766 Value *Result = Builder->CreateICmpSGT(LHSCIOp, NegOne, ICI.getName());
Chris Lattner02446fc2010-01-04 07:37:31 +00001767
1768 // Finally, return the value computed.
Duncan Sands9d32f602011-01-20 13:21:55 +00001769 if (ICI.getPredicate() == ICmpInst::ICMP_ULT)
Chris Lattner02446fc2010-01-04 07:37:31 +00001770 return ReplaceInstUsesWith(ICI, Result);
1771
Duncan Sands9d32f602011-01-20 13:21:55 +00001772 assert(ICI.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");
Chris Lattner02446fc2010-01-04 07:37:31 +00001773 return BinaryOperator::CreateNot(Result);
1774}
1775
Chris Lattnerf0f568b2010-12-19 17:52:50 +00001776/// ProcessUGT_ADDCST_ADD - The caller has matched a pattern of the form:
1777/// I = icmp ugt (add (add A, B), CI2), CI1
Chris Lattnerdd7e8372010-12-19 18:22:06 +00001778/// If this is of the form:
1779/// sum = a + b
1780/// if (sum+128 >u 255)
1781/// Then replace it with llvm.sadd.with.overflow.i8.
1782///
Chris Lattnerf0f568b2010-12-19 17:52:50 +00001783static Instruction *ProcessUGT_ADDCST_ADD(ICmpInst &I, Value *A, Value *B,
1784 ConstantInt *CI2, ConstantInt *CI1,
Chris Lattner0fe80bb2010-12-19 18:38:44 +00001785 InstCombiner &IC) {
Chris Lattner368397b2010-12-19 17:59:02 +00001786 // The transformation we're trying to do here is to transform this into an
1787 // llvm.sadd.with.overflow. To do this, we have to replace the original add
1788 // with a narrower add, and discard the add-with-constant that is part of the
1789 // range check (if we can't eliminate it, this isn't profitable).
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001790
Chris Lattner368397b2010-12-19 17:59:02 +00001791 // In order to eliminate the add-with-constant, the compare can be its only
1792 // use.
Chris Lattnerdd7e8372010-12-19 18:22:06 +00001793 Instruction *AddWithCst = cast<Instruction>(I.getOperand(0));
Chris Lattner368397b2010-12-19 17:59:02 +00001794 if (!AddWithCst->hasOneUse()) return 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001795
Chris Lattnerdd7e8372010-12-19 18:22:06 +00001796 // If CI2 is 2^7, 2^15, 2^31, then it might be an sadd.with.overflow.
1797 if (!CI2->getValue().isPowerOf2()) return 0;
1798 unsigned NewWidth = CI2->getValue().countTrailingZeros();
1799 if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31) return 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001800
Chris Lattnerdd7e8372010-12-19 18:22:06 +00001801 // The width of the new add formed is 1 more than the bias.
1802 ++NewWidth;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001803
Chris Lattnerdd7e8372010-12-19 18:22:06 +00001804 // Check to see that CI1 is an all-ones value with NewWidth bits.
1805 if (CI1->getBitWidth() == NewWidth ||
1806 CI1->getValue() != APInt::getLowBitsSet(CI1->getBitWidth(), NewWidth))
1807 return 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001808
Eli Friedman54b92112011-11-28 23:32:19 +00001809 // This is only really a signed overflow check if the inputs have been
1810 // sign-extended; check for that condition. For example, if CI2 is 2^31 and
1811 // the operands of the add are 64 bits wide, we need at least 33 sign bits.
1812 unsigned NeededSignBits = CI1->getBitWidth() - NewWidth + 1;
1813 if (IC.ComputeNumSignBits(A) < NeededSignBits ||
1814 IC.ComputeNumSignBits(B) < NeededSignBits)
1815 return 0;
1816
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001817 // In order to replace the original add with a narrower
Chris Lattnerdd7e8372010-12-19 18:22:06 +00001818 // llvm.sadd.with.overflow, the only uses allowed are the add-with-constant
1819 // and truncates that discard the high bits of the add. Verify that this is
1820 // the case.
1821 Instruction *OrigAdd = cast<Instruction>(AddWithCst->getOperand(0));
1822 for (Value::use_iterator UI = OrigAdd->use_begin(), E = OrigAdd->use_end();
1823 UI != E; ++UI) {
1824 if (*UI == AddWithCst) continue;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001825
Chris Lattnerdd7e8372010-12-19 18:22:06 +00001826 // Only accept truncates for now. We would really like a nice recursive
1827 // predicate like SimplifyDemandedBits, but which goes downwards the use-def
1828 // chain to see which bits of a value are actually demanded. If the
1829 // original add had another add which was then immediately truncated, we
1830 // could still do the transformation.
1831 TruncInst *TI = dyn_cast<TruncInst>(*UI);
1832 if (TI == 0 ||
1833 TI->getType()->getPrimitiveSizeInBits() > NewWidth) return 0;
1834 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001835
Chris Lattnerf0f568b2010-12-19 17:52:50 +00001836 // If the pattern matches, truncate the inputs to the narrower type and
1837 // use the sadd_with_overflow intrinsic to efficiently compute both the
1838 // result and the overflow bit.
Chris Lattner0a624742010-12-19 18:35:09 +00001839 Module *M = I.getParent()->getParent()->getParent();
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001840
Jay Foad5fdd6c82011-07-12 14:06:48 +00001841 Type *NewType = IntegerType::get(OrigAdd->getContext(), NewWidth);
Chris Lattner0a624742010-12-19 18:35:09 +00001842 Value *F = Intrinsic::getDeclaration(M, Intrinsic::sadd_with_overflow,
Benjamin Kramereb9a85f2011-07-14 17:45:39 +00001843 NewType);
Chris Lattner0a624742010-12-19 18:35:09 +00001844
Chris Lattner0fe80bb2010-12-19 18:38:44 +00001845 InstCombiner::BuilderTy *Builder = IC.Builder;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001846
Chris Lattner0a624742010-12-19 18:35:09 +00001847 // Put the new code above the original add, in case there are any uses of the
1848 // add between the add and the compare.
Chris Lattnere5cbdca2010-12-19 19:37:52 +00001849 Builder->SetInsertPoint(OrigAdd);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001850
Chris Lattner0a624742010-12-19 18:35:09 +00001851 Value *TruncA = Builder->CreateTrunc(A, NewType, A->getName()+".trunc");
1852 Value *TruncB = Builder->CreateTrunc(B, NewType, B->getName()+".trunc");
1853 CallInst *Call = Builder->CreateCall2(F, TruncA, TruncB, "sadd");
1854 Value *Add = Builder->CreateExtractValue(Call, 0, "sadd.result");
1855 Value *ZExt = Builder->CreateZExt(Add, OrigAdd->getType());
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001856
Chris Lattnerf0f568b2010-12-19 17:52:50 +00001857 // The inner add was the result of the narrow add, zero extended to the
1858 // wider type. Replace it with the result computed by the intrinsic.
Chris Lattner0fe80bb2010-12-19 18:38:44 +00001859 IC.ReplaceInstUsesWith(*OrigAdd, ZExt);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001860
Chris Lattner0a624742010-12-19 18:35:09 +00001861 // The original icmp gets replaced with the overflow value.
1862 return ExtractValueInst::Create(Call, 1, "sadd.overflow");
Chris Lattnerf0f568b2010-12-19 17:52:50 +00001863}
Chris Lattner02446fc2010-01-04 07:37:31 +00001864
Chris Lattnere5cbdca2010-12-19 19:37:52 +00001865static Instruction *ProcessUAddIdiom(Instruction &I, Value *OrigAddV,
1866 InstCombiner &IC) {
1867 // Don't bother doing this transformation for pointers, don't do it for
1868 // vectors.
1869 if (!isa<IntegerType>(OrigAddV->getType())) return 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001870
Chris Lattnere5cbdca2010-12-19 19:37:52 +00001871 // If the add is a constant expr, then we don't bother transforming it.
1872 Instruction *OrigAdd = dyn_cast<Instruction>(OrigAddV);
1873 if (OrigAdd == 0) return 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001874
Chris Lattnere5cbdca2010-12-19 19:37:52 +00001875 Value *LHS = OrigAdd->getOperand(0), *RHS = OrigAdd->getOperand(1);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001876
Chris Lattnere5cbdca2010-12-19 19:37:52 +00001877 // Put the new code above the original add, in case there are any uses of the
1878 // add between the add and the compare.
1879 InstCombiner::BuilderTy *Builder = IC.Builder;
1880 Builder->SetInsertPoint(OrigAdd);
1881
1882 Module *M = I.getParent()->getParent()->getParent();
Jay Foad5fdd6c82011-07-12 14:06:48 +00001883 Type *Ty = LHS->getType();
Benjamin Kramereb9a85f2011-07-14 17:45:39 +00001884 Value *F = Intrinsic::getDeclaration(M, Intrinsic::uadd_with_overflow, Ty);
Chris Lattnere5cbdca2010-12-19 19:37:52 +00001885 CallInst *Call = Builder->CreateCall2(F, LHS, RHS, "uadd");
1886 Value *Add = Builder->CreateExtractValue(Call, 0);
1887
1888 IC.ReplaceInstUsesWith(*OrigAdd, Add);
1889
1890 // The original icmp gets replaced with the overflow value.
1891 return ExtractValueInst::Create(Call, 1, "uadd.overflow");
1892}
1893
Owen Andersonda1c1222011-01-11 00:36:45 +00001894// DemandedBitsLHSMask - When performing a comparison against a constant,
1895// it is possible that not all the bits in the LHS are demanded. This helper
1896// method computes the mask that IS demanded.
1897static APInt DemandedBitsLHSMask(ICmpInst &I,
1898 unsigned BitWidth, bool isSignCheck) {
1899 if (isSignCheck)
1900 return APInt::getSignBit(BitWidth);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001901
Owen Andersonda1c1222011-01-11 00:36:45 +00001902 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1));
1903 if (!CI) return APInt::getAllOnesValue(BitWidth);
Owen Andersona33b6252011-01-11 18:26:37 +00001904 const APInt &RHS = CI->getValue();
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001905
Owen Andersonda1c1222011-01-11 00:36:45 +00001906 switch (I.getPredicate()) {
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001907 // For a UGT comparison, we don't care about any bits that
Owen Andersonda1c1222011-01-11 00:36:45 +00001908 // correspond to the trailing ones of the comparand. The value of these
1909 // bits doesn't impact the outcome of the comparison, because any value
1910 // greater than the RHS must differ in a bit higher than these due to carry.
1911 case ICmpInst::ICMP_UGT: {
1912 unsigned trailingOnes = RHS.countTrailingOnes();
1913 APInt lowBitsSet = APInt::getLowBitsSet(BitWidth, trailingOnes);
1914 return ~lowBitsSet;
1915 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001916
Owen Andersonda1c1222011-01-11 00:36:45 +00001917 // Similarly, for a ULT comparison, we don't care about the trailing zeros.
1918 // Any value less than the RHS must differ in a higher bit because of carries.
1919 case ICmpInst::ICMP_ULT: {
1920 unsigned trailingZeros = RHS.countTrailingZeros();
1921 APInt lowBitsSet = APInt::getLowBitsSet(BitWidth, trailingZeros);
1922 return ~lowBitsSet;
1923 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001924
Owen Andersonda1c1222011-01-11 00:36:45 +00001925 default:
1926 return APInt::getAllOnesValue(BitWidth);
1927 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001928
Owen Andersonda1c1222011-01-11 00:36:45 +00001929}
Chris Lattner02446fc2010-01-04 07:37:31 +00001930
1931Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
1932 bool Changed = false;
Chris Lattner5f670d42010-02-01 19:54:45 +00001933 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001934
Chris Lattner02446fc2010-01-04 07:37:31 +00001935 /// Orders the operands of the compare so that they are listed from most
1936 /// complex to least complex. This puts constants before unary operators,
1937 /// before binary operators.
Chris Lattner5f670d42010-02-01 19:54:45 +00001938 if (getComplexity(Op0) < getComplexity(Op1)) {
Chris Lattner02446fc2010-01-04 07:37:31 +00001939 I.swapOperands();
Chris Lattner5f670d42010-02-01 19:54:45 +00001940 std::swap(Op0, Op1);
Chris Lattner02446fc2010-01-04 07:37:31 +00001941 Changed = true;
1942 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001943
Chris Lattner02446fc2010-01-04 07:37:31 +00001944 if (Value *V = SimplifyICmpInst(I.getPredicate(), Op0, Op1, TD))
1945 return ReplaceInstUsesWith(I, V);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00001946
Pete Cooper65a6b572011-12-01 03:58:40 +00001947 // comparing -val or val with non-zero is the same as just comparing val
Pete Cooper165695d2011-12-01 19:13:26 +00001948 // ie, abs(val) != 0 -> val != 0
Pete Cooper65a6b572011-12-01 03:58:40 +00001949 if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero()))
1950 {
Pete Cooper165695d2011-12-01 19:13:26 +00001951 Value *Cond, *SelectTrue, *SelectFalse;
1952 if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue),
Pete Cooper65a6b572011-12-01 03:58:40 +00001953 m_Value(SelectFalse)))) {
Pete Cooper165695d2011-12-01 19:13:26 +00001954 if (Value *V = dyn_castNegVal(SelectTrue)) {
1955 if (V == SelectFalse)
1956 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
1957 }
1958 else if (Value *V = dyn_castNegVal(SelectFalse)) {
1959 if (V == SelectTrue)
1960 return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
Pete Cooper65a6b572011-12-01 03:58:40 +00001961 }
1962 }
1963 }
1964
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001965 Type *Ty = Op0->getType();
Chris Lattner02446fc2010-01-04 07:37:31 +00001966
1967 // icmp's with boolean values can always be turned into bitwise operations
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001968 if (Ty->isIntegerTy(1)) {
Chris Lattner02446fc2010-01-04 07:37:31 +00001969 switch (I.getPredicate()) {
1970 default: llvm_unreachable("Invalid icmp instruction!");
1971 case ICmpInst::ICMP_EQ: { // icmp eq i1 A, B -> ~(A^B)
1972 Value *Xor = Builder->CreateXor(Op0, Op1, I.getName()+"tmp");
1973 return BinaryOperator::CreateNot(Xor);
1974 }
1975 case ICmpInst::ICMP_NE: // icmp eq i1 A, B -> A^B
1976 return BinaryOperator::CreateXor(Op0, Op1);
1977
1978 case ICmpInst::ICMP_UGT:
1979 std::swap(Op0, Op1); // Change icmp ugt -> icmp ult
1980 // FALL THROUGH
1981 case ICmpInst::ICMP_ULT:{ // icmp ult i1 A, B -> ~A & B
1982 Value *Not = Builder->CreateNot(Op0, I.getName()+"tmp");
1983 return BinaryOperator::CreateAnd(Not, Op1);
1984 }
1985 case ICmpInst::ICMP_SGT:
1986 std::swap(Op0, Op1); // Change icmp sgt -> icmp slt
1987 // FALL THROUGH
1988 case ICmpInst::ICMP_SLT: { // icmp slt i1 A, B -> A & ~B
1989 Value *Not = Builder->CreateNot(Op1, I.getName()+"tmp");
1990 return BinaryOperator::CreateAnd(Not, Op0);
1991 }
1992 case ICmpInst::ICMP_UGE:
1993 std::swap(Op0, Op1); // Change icmp uge -> icmp ule
1994 // FALL THROUGH
1995 case ICmpInst::ICMP_ULE: { // icmp ule i1 A, B -> ~A | B
1996 Value *Not = Builder->CreateNot(Op0, I.getName()+"tmp");
1997 return BinaryOperator::CreateOr(Not, Op1);
1998 }
1999 case ICmpInst::ICMP_SGE:
2000 std::swap(Op0, Op1); // Change icmp sge -> icmp sle
2001 // FALL THROUGH
2002 case ICmpInst::ICMP_SLE: { // icmp sle i1 A, B -> A | ~B
2003 Value *Not = Builder->CreateNot(Op1, I.getName()+"tmp");
2004 return BinaryOperator::CreateOr(Not, Op0);
2005 }
2006 }
2007 }
2008
2009 unsigned BitWidth = 0;
Chris Lattnere5cbdca2010-12-19 19:37:52 +00002010 if (Ty->isIntOrIntVectorTy())
Chris Lattner02446fc2010-01-04 07:37:31 +00002011 BitWidth = Ty->getScalarSizeInBits();
Chris Lattnere5cbdca2010-12-19 19:37:52 +00002012 else if (TD) // Pointers require TD info to get their size.
2013 BitWidth = TD->getTypeSizeInBits(Ty->getScalarType());
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002014
Chris Lattner02446fc2010-01-04 07:37:31 +00002015 bool isSignBit = false;
2016
2017 // See if we are doing a comparison with a constant.
2018 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2019 Value *A = 0, *B = 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002020
Owen Andersone63dda52010-12-17 18:08:00 +00002021 // Match the following pattern, which is a common idiom when writing
2022 // overflow-safe integer arithmetic function. The source performs an
2023 // addition in wider type, and explicitly checks for overflow using
2024 // comparisons against INT_MIN and INT_MAX. Simplify this by using the
2025 // sadd_with_overflow intrinsic.
Chris Lattnerf0f568b2010-12-19 17:52:50 +00002026 //
2027 // TODO: This could probably be generalized to handle other overflow-safe
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002028 // operations if we worked out the formulas to compute the appropriate
Owen Andersone63dda52010-12-17 18:08:00 +00002029 // magic constants.
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002030 //
Chris Lattnerf0f568b2010-12-19 17:52:50 +00002031 // sum = a + b
2032 // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8
Owen Andersone63dda52010-12-17 18:08:00 +00002033 {
Chris Lattnerf0f568b2010-12-19 17:52:50 +00002034 ConstantInt *CI2; // I = icmp ugt (add (add A, B), CI2), CI
Owen Andersone63dda52010-12-17 18:08:00 +00002035 if (I.getPredicate() == ICmpInst::ICMP_UGT &&
Chris Lattnerf0f568b2010-12-19 17:52:50 +00002036 match(Op0, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2))))
Chris Lattner0fe80bb2010-12-19 18:38:44 +00002037 if (Instruction *Res = ProcessUGT_ADDCST_ADD(I, A, B, CI2, CI, *this))
Chris Lattnerf0f568b2010-12-19 17:52:50 +00002038 return Res;
Owen Andersone63dda52010-12-17 18:08:00 +00002039 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002040
Chris Lattner02446fc2010-01-04 07:37:31 +00002041 // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B)
2042 if (I.isEquality() && CI->isZero() &&
2043 match(Op0, m_Sub(m_Value(A), m_Value(B)))) {
2044 // (icmp cond A B) if cond is equality
2045 return new ICmpInst(I.getPredicate(), A, B);
2046 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002047
Chris Lattner02446fc2010-01-04 07:37:31 +00002048 // If we have an icmp le or icmp ge instruction, turn it into the
2049 // appropriate icmp lt or icmp gt instruction. This allows us to rely on
2050 // them being folded in the code below. The SimplifyICmpInst code has
2051 // already handled the edge cases for us, so we just assert on them.
2052 switch (I.getPredicate()) {
2053 default: break;
2054 case ICmpInst::ICMP_ULE:
2055 assert(!CI->isMaxValue(false)); // A <=u MAX -> TRUE
2056 return new ICmpInst(ICmpInst::ICMP_ULT, Op0,
2057 ConstantInt::get(CI->getContext(), CI->getValue()+1));
2058 case ICmpInst::ICMP_SLE:
2059 assert(!CI->isMaxValue(true)); // A <=s MAX -> TRUE
2060 return new ICmpInst(ICmpInst::ICMP_SLT, Op0,
2061 ConstantInt::get(CI->getContext(), CI->getValue()+1));
2062 case ICmpInst::ICMP_UGE:
Nick Lewyckyd8d15842011-02-28 06:20:05 +00002063 assert(!CI->isMinValue(false)); // A >=u MIN -> TRUE
Chris Lattner02446fc2010-01-04 07:37:31 +00002064 return new ICmpInst(ICmpInst::ICMP_UGT, Op0,
2065 ConstantInt::get(CI->getContext(), CI->getValue()-1));
2066 case ICmpInst::ICMP_SGE:
Nick Lewyckyd8d15842011-02-28 06:20:05 +00002067 assert(!CI->isMinValue(true)); // A >=s MIN -> TRUE
Chris Lattner02446fc2010-01-04 07:37:31 +00002068 return new ICmpInst(ICmpInst::ICMP_SGT, Op0,
2069 ConstantInt::get(CI->getContext(), CI->getValue()-1));
2070 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002071
Chris Lattner02446fc2010-01-04 07:37:31 +00002072 // If this comparison is a normal comparison, it demands all
2073 // bits, if it is a sign bit comparison, it only demands the sign bit.
2074 bool UnusedBit;
2075 isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit);
2076 }
2077
2078 // See if we can fold the comparison based on range information we can get
2079 // by checking whether bits are known to be zero or one in the input.
2080 if (BitWidth != 0) {
2081 APInt Op0KnownZero(BitWidth, 0), Op0KnownOne(BitWidth, 0);
2082 APInt Op1KnownZero(BitWidth, 0), Op1KnownOne(BitWidth, 0);
2083
2084 if (SimplifyDemandedBits(I.getOperandUse(0),
Owen Andersonda1c1222011-01-11 00:36:45 +00002085 DemandedBitsLHSMask(I, BitWidth, isSignBit),
Chris Lattner02446fc2010-01-04 07:37:31 +00002086 Op0KnownZero, Op0KnownOne, 0))
2087 return &I;
2088 if (SimplifyDemandedBits(I.getOperandUse(1),
2089 APInt::getAllOnesValue(BitWidth),
2090 Op1KnownZero, Op1KnownOne, 0))
2091 return &I;
2092
2093 // Given the known and unknown bits, compute a range that the LHS could be
2094 // in. Compute the Min, Max and RHS values based on the known bits. For the
2095 // EQ and NE we use unsigned values.
2096 APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
2097 APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
2098 if (I.isSigned()) {
2099 ComputeSignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne,
2100 Op0Min, Op0Max);
2101 ComputeSignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne,
2102 Op1Min, Op1Max);
2103 } else {
2104 ComputeUnsignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne,
2105 Op0Min, Op0Max);
2106 ComputeUnsignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne,
2107 Op1Min, Op1Max);
2108 }
2109
2110 // If Min and Max are known to be the same, then SimplifyDemandedBits
2111 // figured out that the LHS is a constant. Just constant fold this now so
2112 // that code below can assume that Min != Max.
2113 if (!isa<Constant>(Op0) && Op0Min == Op0Max)
2114 return new ICmpInst(I.getPredicate(),
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002115 ConstantInt::get(Op0->getType(), Op0Min), Op1);
Chris Lattner02446fc2010-01-04 07:37:31 +00002116 if (!isa<Constant>(Op1) && Op1Min == Op1Max)
2117 return new ICmpInst(I.getPredicate(), Op0,
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002118 ConstantInt::get(Op1->getType(), Op1Min));
Chris Lattner02446fc2010-01-04 07:37:31 +00002119
2120 // Based on the range information we know about the LHS, see if we can
Nick Lewyckyd8d15842011-02-28 06:20:05 +00002121 // simplify this comparison. For example, (x&4) < 8 is always true.
Chris Lattner02446fc2010-01-04 07:37:31 +00002122 switch (I.getPredicate()) {
2123 default: llvm_unreachable("Unknown icmp opcode!");
Chris Lattner75d8f592010-11-21 06:44:42 +00002124 case ICmpInst::ICMP_EQ: {
Chris Lattner02446fc2010-01-04 07:37:31 +00002125 if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002126 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002127
Chris Lattner75d8f592010-11-21 06:44:42 +00002128 // If all bits are known zero except for one, then we know at most one
2129 // bit is set. If the comparison is against zero, then this is a check
2130 // to see if *that* bit is set.
2131 APInt Op0KnownZeroInverted = ~Op0KnownZero;
2132 if (~Op1KnownZero == 0 && Op0KnownZeroInverted.isPowerOf2()) {
2133 // If the LHS is an AND with the same constant, look through it.
2134 Value *LHS = 0;
2135 ConstantInt *LHSC = 0;
2136 if (!match(Op0, m_And(m_Value(LHS), m_ConstantInt(LHSC))) ||
2137 LHSC->getValue() != Op0KnownZeroInverted)
2138 LHS = Op0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002139
Chris Lattner75d8f592010-11-21 06:44:42 +00002140 // If the LHS is 1 << x, and we know the result is a power of 2 like 8,
Chris Lattner79b967b2010-11-23 02:42:04 +00002141 // then turn "((1 << x)&8) == 0" into "x != 3".
Chris Lattner75d8f592010-11-21 06:44:42 +00002142 Value *X = 0;
2143 if (match(LHS, m_Shl(m_One(), m_Value(X)))) {
2144 unsigned CmpVal = Op0KnownZeroInverted.countTrailingZeros();
Chris Lattner79b967b2010-11-23 02:42:04 +00002145 return new ICmpInst(ICmpInst::ICMP_NE, X,
Chris Lattner75d8f592010-11-21 06:44:42 +00002146 ConstantInt::get(X->getType(), CmpVal));
2147 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002148
Chris Lattner75d8f592010-11-21 06:44:42 +00002149 // If the LHS is 8 >>u x, and we know the result is a power of 2 like 1,
Chris Lattner79b967b2010-11-23 02:42:04 +00002150 // then turn "((8 >>u x)&1) == 0" into "x != 3".
Chris Lattnerb20c0b52011-02-10 05:23:05 +00002151 const APInt *CI;
Chris Lattner75d8f592010-11-21 06:44:42 +00002152 if (Op0KnownZeroInverted == 1 &&
Chris Lattnerb20c0b52011-02-10 05:23:05 +00002153 match(LHS, m_LShr(m_Power2(CI), m_Value(X))))
Chris Lattner79b967b2010-11-23 02:42:04 +00002154 return new ICmpInst(ICmpInst::ICMP_NE, X,
Chris Lattnerb20c0b52011-02-10 05:23:05 +00002155 ConstantInt::get(X->getType(),
2156 CI->countTrailingZeros()));
Chris Lattner75d8f592010-11-21 06:44:42 +00002157 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002158
Chris Lattner02446fc2010-01-04 07:37:31 +00002159 break;
Chris Lattner75d8f592010-11-21 06:44:42 +00002160 }
2161 case ICmpInst::ICMP_NE: {
Chris Lattner02446fc2010-01-04 07:37:31 +00002162 if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002163 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002164
Chris Lattner75d8f592010-11-21 06:44:42 +00002165 // If all bits are known zero except for one, then we know at most one
2166 // bit is set. If the comparison is against zero, then this is a check
2167 // to see if *that* bit is set.
2168 APInt Op0KnownZeroInverted = ~Op0KnownZero;
2169 if (~Op1KnownZero == 0 && Op0KnownZeroInverted.isPowerOf2()) {
2170 // If the LHS is an AND with the same constant, look through it.
2171 Value *LHS = 0;
2172 ConstantInt *LHSC = 0;
2173 if (!match(Op0, m_And(m_Value(LHS), m_ConstantInt(LHSC))) ||
2174 LHSC->getValue() != Op0KnownZeroInverted)
2175 LHS = Op0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002176
Chris Lattner75d8f592010-11-21 06:44:42 +00002177 // If the LHS is 1 << x, and we know the result is a power of 2 like 8,
Chris Lattner79b967b2010-11-23 02:42:04 +00002178 // then turn "((1 << x)&8) != 0" into "x == 3".
Chris Lattner75d8f592010-11-21 06:44:42 +00002179 Value *X = 0;
2180 if (match(LHS, m_Shl(m_One(), m_Value(X)))) {
2181 unsigned CmpVal = Op0KnownZeroInverted.countTrailingZeros();
Chris Lattner79b967b2010-11-23 02:42:04 +00002182 return new ICmpInst(ICmpInst::ICMP_EQ, X,
Chris Lattner75d8f592010-11-21 06:44:42 +00002183 ConstantInt::get(X->getType(), CmpVal));
2184 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002185
Chris Lattner75d8f592010-11-21 06:44:42 +00002186 // If the LHS is 8 >>u x, and we know the result is a power of 2 like 1,
Chris Lattner79b967b2010-11-23 02:42:04 +00002187 // then turn "((8 >>u x)&1) != 0" into "x == 3".
Chris Lattnerb20c0b52011-02-10 05:23:05 +00002188 const APInt *CI;
Chris Lattner75d8f592010-11-21 06:44:42 +00002189 if (Op0KnownZeroInverted == 1 &&
Chris Lattnerb20c0b52011-02-10 05:23:05 +00002190 match(LHS, m_LShr(m_Power2(CI), m_Value(X))))
Chris Lattner79b967b2010-11-23 02:42:04 +00002191 return new ICmpInst(ICmpInst::ICMP_EQ, X,
Chris Lattnerb20c0b52011-02-10 05:23:05 +00002192 ConstantInt::get(X->getType(),
2193 CI->countTrailingZeros()));
Chris Lattner75d8f592010-11-21 06:44:42 +00002194 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002195
Chris Lattner02446fc2010-01-04 07:37:31 +00002196 break;
Chris Lattner75d8f592010-11-21 06:44:42 +00002197 }
Chris Lattner02446fc2010-01-04 07:37:31 +00002198 case ICmpInst::ICMP_ULT:
2199 if (Op0Max.ult(Op1Min)) // A <u B -> true if max(A) < min(B)
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002200 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
Chris Lattner02446fc2010-01-04 07:37:31 +00002201 if (Op0Min.uge(Op1Max)) // A <u B -> false if min(A) >= max(B)
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002202 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
Chris Lattner02446fc2010-01-04 07:37:31 +00002203 if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)
2204 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
2205 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2206 if (Op1Max == Op0Min+1) // A <u C -> A == C-1 if min(A)+1 == C
2207 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
2208 ConstantInt::get(CI->getContext(), CI->getValue()-1));
2209
2210 // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear
2211 if (CI->isMinValue(true))
2212 return new ICmpInst(ICmpInst::ICMP_SGT, Op0,
2213 Constant::getAllOnesValue(Op0->getType()));
2214 }
2215 break;
2216 case ICmpInst::ICMP_UGT:
2217 if (Op0Min.ugt(Op1Max)) // A >u B -> true if min(A) > max(B)
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002218 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
Chris Lattner02446fc2010-01-04 07:37:31 +00002219 if (Op0Max.ule(Op1Min)) // A >u B -> false if max(A) <= max(B)
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002220 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
Chris Lattner02446fc2010-01-04 07:37:31 +00002221
2222 if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
2223 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
2224 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2225 if (Op1Min == Op0Max-1) // A >u C -> A == C+1 if max(a)-1 == C
2226 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
2227 ConstantInt::get(CI->getContext(), CI->getValue()+1));
2228
2229 // (x >u 2147483647) -> (x <s 0) -> true if sign bit set
2230 if (CI->isMaxValue(true))
2231 return new ICmpInst(ICmpInst::ICMP_SLT, Op0,
2232 Constant::getNullValue(Op0->getType()));
2233 }
2234 break;
2235 case ICmpInst::ICMP_SLT:
2236 if (Op0Max.slt(Op1Min)) // A <s B -> true if max(A) < min(C)
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002237 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
Chris Lattner02446fc2010-01-04 07:37:31 +00002238 if (Op0Min.sge(Op1Max)) // A <s B -> false if min(A) >= max(C)
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002239 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
Chris Lattner02446fc2010-01-04 07:37:31 +00002240 if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)
2241 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
2242 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2243 if (Op1Max == Op0Min+1) // A <s C -> A == C-1 if min(A)+1 == C
2244 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
2245 ConstantInt::get(CI->getContext(), CI->getValue()-1));
2246 }
2247 break;
2248 case ICmpInst::ICMP_SGT:
2249 if (Op0Min.sgt(Op1Max)) // A >s B -> true if min(A) > max(B)
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002250 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
Chris Lattner02446fc2010-01-04 07:37:31 +00002251 if (Op0Max.sle(Op1Min)) // A >s B -> false if max(A) <= min(B)
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002252 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
Chris Lattner02446fc2010-01-04 07:37:31 +00002253
2254 if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
2255 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
2256 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2257 if (Op1Min == Op0Max-1) // A >s C -> A == C+1 if max(A)-1 == C
2258 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
2259 ConstantInt::get(CI->getContext(), CI->getValue()+1));
2260 }
2261 break;
2262 case ICmpInst::ICMP_SGE:
2263 assert(!isa<ConstantInt>(Op1) && "ICMP_SGE with ConstantInt not folded!");
2264 if (Op0Min.sge(Op1Max)) // A >=s B -> true if min(A) >= max(B)
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002265 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
Chris Lattner02446fc2010-01-04 07:37:31 +00002266 if (Op0Max.slt(Op1Min)) // A >=s B -> false if max(A) < min(B)
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002267 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
Chris Lattner02446fc2010-01-04 07:37:31 +00002268 break;
2269 case ICmpInst::ICMP_SLE:
2270 assert(!isa<ConstantInt>(Op1) && "ICMP_SLE with ConstantInt not folded!");
2271 if (Op0Max.sle(Op1Min)) // A <=s B -> true if max(A) <= min(B)
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002272 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
Chris Lattner02446fc2010-01-04 07:37:31 +00002273 if (Op0Min.sgt(Op1Max)) // A <=s B -> false if min(A) > max(B)
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002274 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
Chris Lattner02446fc2010-01-04 07:37:31 +00002275 break;
2276 case ICmpInst::ICMP_UGE:
2277 assert(!isa<ConstantInt>(Op1) && "ICMP_UGE with ConstantInt not folded!");
2278 if (Op0Min.uge(Op1Max)) // A >=u B -> true if min(A) >= max(B)
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002279 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
Chris Lattner02446fc2010-01-04 07:37:31 +00002280 if (Op0Max.ult(Op1Min)) // A >=u B -> false if max(A) < min(B)
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002281 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
Chris Lattner02446fc2010-01-04 07:37:31 +00002282 break;
2283 case ICmpInst::ICMP_ULE:
2284 assert(!isa<ConstantInt>(Op1) && "ICMP_ULE with ConstantInt not folded!");
2285 if (Op0Max.ule(Op1Min)) // A <=u B -> true if max(A) <= min(B)
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002286 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
Chris Lattner02446fc2010-01-04 07:37:31 +00002287 if (Op0Min.ugt(Op1Max)) // A <=u B -> false if min(A) > max(B)
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002288 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
Chris Lattner02446fc2010-01-04 07:37:31 +00002289 break;
2290 }
2291
2292 // Turn a signed comparison into an unsigned one if both operands
2293 // are known to have the same sign.
2294 if (I.isSigned() &&
2295 ((Op0KnownZero.isNegative() && Op1KnownZero.isNegative()) ||
2296 (Op0KnownOne.isNegative() && Op1KnownOne.isNegative())))
2297 return new ICmpInst(I.getUnsignedPredicate(), Op0, Op1);
2298 }
2299
2300 // Test if the ICmpInst instruction is used exclusively by a select as
2301 // part of a minimum or maximum operation. If so, refrain from doing
2302 // any other folding. This helps out other analyses which understand
2303 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
2304 // and CodeGen. And in this case, at least one of the comparison
2305 // operands has at least one user besides the compare (the select),
2306 // which would often largely negate the benefit of folding anyway.
2307 if (I.hasOneUse())
2308 if (SelectInst *SI = dyn_cast<SelectInst>(*I.use_begin()))
2309 if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) ||
2310 (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1))
2311 return 0;
2312
2313 // See if we are doing a comparison between a constant and an instruction that
2314 // can be folded into the comparison.
2315 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002316 // Since the RHS is a ConstantInt (CI), if the left hand side is an
2317 // instruction, see if that instruction also has constants so that the
2318 // instruction can be folded into the icmp
Chris Lattner02446fc2010-01-04 07:37:31 +00002319 if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
2320 if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI))
2321 return Res;
2322 }
2323
2324 // Handle icmp with constant (but not simple integer constant) RHS
2325 if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
2326 if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
2327 switch (LHSI->getOpcode()) {
2328 case Instruction::GetElementPtr:
2329 // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null
2330 if (RHSC->isNullValue() &&
2331 cast<GetElementPtrInst>(LHSI)->hasAllZeroIndices())
2332 return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
2333 Constant::getNullValue(LHSI->getOperand(0)->getType()));
2334 break;
2335 case Instruction::PHI:
2336 // Only fold icmp into the PHI if the phi and icmp are in the same
2337 // block. If in the same block, we're encouraging jump threading. If
2338 // not, we are just pessimizing the code by making an i1 phi.
2339 if (LHSI->getParent() == I.getParent())
Chris Lattner9922ccf2011-01-16 05:14:26 +00002340 if (Instruction *NV = FoldOpIntoPhi(I))
Chris Lattner02446fc2010-01-04 07:37:31 +00002341 return NV;
2342 break;
2343 case Instruction::Select: {
2344 // If either operand of the select is a constant, we can fold the
2345 // comparison into the select arms, which will cause one to be
2346 // constant folded and the select turned into a bitwise or.
2347 Value *Op1 = 0, *Op2 = 0;
2348 if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1)))
2349 Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
2350 if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2)))
2351 Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
2352
2353 // We only want to perform this transformation if it will not lead to
2354 // additional code. This is true if either both sides of the select
2355 // fold to a constant (in which case the icmp is replaced with a select
2356 // which will usually simplify) or this is the only user of the
2357 // select (in which case we are trading a select+icmp for a simpler
2358 // select+icmp).
2359 if ((Op1 && Op2) || (LHSI->hasOneUse() && (Op1 || Op2))) {
2360 if (!Op1)
2361 Op1 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(1),
2362 RHSC, I.getName());
2363 if (!Op2)
2364 Op2 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(2),
2365 RHSC, I.getName());
2366 return SelectInst::Create(LHSI->getOperand(0), Op1, Op2);
2367 }
2368 break;
2369 }
Chris Lattner02446fc2010-01-04 07:37:31 +00002370 case Instruction::IntToPtr:
2371 // icmp pred inttoptr(X), null -> icmp pred X, 0
2372 if (RHSC->isNullValue() && TD &&
Chandler Carruthece6c6b2012-11-01 08:07:29 +00002373 TD->getIntPtrType(RHSC->getContext()) ==
Chris Lattner02446fc2010-01-04 07:37:31 +00002374 LHSI->getOperand(0)->getType())
2375 return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
2376 Constant::getNullValue(LHSI->getOperand(0)->getType()));
2377 break;
2378
2379 case Instruction::Load:
2380 // Try to optimize things like "A[i] > 4" to index computations.
2381 if (GetElementPtrInst *GEP =
2382 dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) {
2383 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
2384 if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
2385 !cast<LoadInst>(LHSI)->isVolatile())
2386 if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV, I))
2387 return Res;
2388 }
2389 break;
2390 }
2391 }
2392
2393 // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now.
2394 if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op0))
2395 if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I))
2396 return NI;
2397 if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op1))
2398 if (Instruction *NI = FoldGEPICmp(GEP, Op0,
2399 ICmpInst::getSwappedPredicate(I.getPredicate()), I))
2400 return NI;
2401
2402 // Test to see if the operands of the icmp are casted versions of other
2403 // values. If the ptr->ptr cast can be stripped off both arguments, we do so
2404 // now.
2405 if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) {
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002406 if (Op0->getType()->isPointerTy() &&
2407 (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) {
Chris Lattner02446fc2010-01-04 07:37:31 +00002408 // We keep moving the cast from the left operand over to the right
2409 // operand, where it can often be eliminated completely.
2410 Op0 = CI->getOperand(0);
2411
2412 // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast
2413 // so eliminate it as well.
2414 if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1))
2415 Op1 = CI2->getOperand(0);
2416
2417 // If Op1 is a constant, we can fold the cast into the constant.
2418 if (Op0->getType() != Op1->getType()) {
2419 if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
2420 Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType());
2421 } else {
2422 // Otherwise, cast the RHS right before the icmp
2423 Op1 = Builder->CreateBitCast(Op1, Op0->getType());
2424 }
2425 }
2426 return new ICmpInst(I.getPredicate(), Op0, Op1);
2427 }
2428 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002429
Chris Lattner02446fc2010-01-04 07:37:31 +00002430 if (isa<CastInst>(Op0)) {
2431 // Handle the special case of: icmp (cast bool to X), <cst>
2432 // This comes up when you have code like
2433 // int X = A < B;
2434 // if (X) ...
2435 // For generality, we handle any zero-extension of any operand comparison
2436 // with a constant or another cast from the same type.
2437 if (isa<Constant>(Op1) || isa<CastInst>(Op1))
2438 if (Instruction *R = visitICmpInstWithCastAndCast(I))
2439 return R;
2440 }
Chris Lattner02446fc2010-01-04 07:37:31 +00002441
Duncan Sandsa7724332011-02-17 07:46:37 +00002442 // Special logic for binary operators.
2443 BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Op0);
2444 BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Op1);
2445 if (BO0 || BO1) {
2446 CmpInst::Predicate Pred = I.getPredicate();
2447 bool NoOp0WrapProblem = false, NoOp1WrapProblem = false;
2448 if (BO0 && isa<OverflowingBinaryOperator>(BO0))
2449 NoOp0WrapProblem = ICmpInst::isEquality(Pred) ||
2450 (CmpInst::isUnsigned(Pred) && BO0->hasNoUnsignedWrap()) ||
2451 (CmpInst::isSigned(Pred) && BO0->hasNoSignedWrap());
2452 if (BO1 && isa<OverflowingBinaryOperator>(BO1))
2453 NoOp1WrapProblem = ICmpInst::isEquality(Pred) ||
2454 (CmpInst::isUnsigned(Pred) && BO1->hasNoUnsignedWrap()) ||
2455 (CmpInst::isSigned(Pred) && BO1->hasNoSignedWrap());
2456
2457 // Analyze the case when either Op0 or Op1 is an add instruction.
2458 // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null).
2459 Value *A = 0, *B = 0, *C = 0, *D = 0;
2460 if (BO0 && BO0->getOpcode() == Instruction::Add)
2461 A = BO0->getOperand(0), B = BO0->getOperand(1);
2462 if (BO1 && BO1->getOpcode() == Instruction::Add)
2463 C = BO1->getOperand(0), D = BO1->getOperand(1);
2464
2465 // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
2466 if ((A == Op1 || B == Op1) && NoOp0WrapProblem)
2467 return new ICmpInst(Pred, A == Op1 ? B : A,
2468 Constant::getNullValue(Op1->getType()));
2469
2470 // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
2471 if ((C == Op0 || D == Op0) && NoOp1WrapProblem)
2472 return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()),
2473 C == Op0 ? D : C);
2474
Duncan Sands39a7de72011-02-18 16:25:37 +00002475 // icmp (X+Y), (X+Z) -> icmp Y, Z for equalities or if there is no overflow.
Duncan Sandsa7724332011-02-17 07:46:37 +00002476 if (A && C && (A == C || A == D || B == C || B == D) &&
2477 NoOp0WrapProblem && NoOp1WrapProblem &&
2478 // Try not to increase register pressure.
2479 BO0->hasOneUse() && BO1->hasOneUse()) {
2480 // Determine Y and Z in the form icmp (X+Y), (X+Z).
Duncan Sandsafe45392012-11-16 18:55:49 +00002481 Value *Y, *Z;
2482 if (A == C) {
Duncan Sands4f0dfbb2012-11-16 20:53:08 +00002483 // C + B == C + D -> B == D
Duncan Sandsafe45392012-11-16 18:55:49 +00002484 Y = B;
2485 Z = D;
2486 } else if (A == D) {
Duncan Sands4f0dfbb2012-11-16 20:53:08 +00002487 // D + B == C + D -> B == C
Duncan Sandsafe45392012-11-16 18:55:49 +00002488 Y = B;
2489 Z = C;
2490 } else if (B == C) {
Duncan Sands4f0dfbb2012-11-16 20:53:08 +00002491 // A + C == C + D -> A == D
Duncan Sandsafe45392012-11-16 18:55:49 +00002492 Y = A;
2493 Z = D;
Duncan Sands4f0dfbb2012-11-16 20:53:08 +00002494 } else {
2495 assert(B == D);
2496 // A + D == C + D -> A == C
Duncan Sandsafe45392012-11-16 18:55:49 +00002497 Y = A;
2498 Z = C;
2499 }
Duncan Sandsa7724332011-02-17 07:46:37 +00002500 return new ICmpInst(Pred, Y, Z);
2501 }
2502
2503 // Analyze the case when either Op0 or Op1 is a sub instruction.
2504 // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null).
2505 A = 0; B = 0; C = 0; D = 0;
2506 if (BO0 && BO0->getOpcode() == Instruction::Sub)
2507 A = BO0->getOperand(0), B = BO0->getOperand(1);
2508 if (BO1 && BO1->getOpcode() == Instruction::Sub)
2509 C = BO1->getOperand(0), D = BO1->getOperand(1);
2510
Duncan Sands39a7de72011-02-18 16:25:37 +00002511 // icmp (X-Y), X -> icmp 0, Y for equalities or if there is no overflow.
2512 if (A == Op1 && NoOp0WrapProblem)
2513 return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B);
2514
2515 // icmp X, (X-Y) -> icmp Y, 0 for equalities or if there is no overflow.
2516 if (C == Op0 && NoOp1WrapProblem)
2517 return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType()));
2518
2519 // icmp (Y-X), (Z-X) -> icmp Y, Z for equalities or if there is no overflow.
Duncan Sandsa7724332011-02-17 07:46:37 +00002520 if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem &&
2521 // Try not to increase register pressure.
2522 BO0->hasOneUse() && BO1->hasOneUse())
2523 return new ICmpInst(Pred, A, C);
2524
Duncan Sands39a7de72011-02-18 16:25:37 +00002525 // icmp (X-Y), (X-Z) -> icmp Z, Y for equalities or if there is no overflow.
2526 if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem &&
2527 // Try not to increase register pressure.
2528 BO0->hasOneUse() && BO1->hasOneUse())
2529 return new ICmpInst(Pred, D, B);
2530
Nick Lewycky9feda172011-03-05 04:28:48 +00002531 BinaryOperator *SRem = NULL;
Nick Lewyckydcf77572011-03-08 06:29:47 +00002532 // icmp (srem X, Y), Y
Nick Lewycky9feda172011-03-05 04:28:48 +00002533 if (BO0 && BO0->getOpcode() == Instruction::SRem &&
2534 Op1 == BO0->getOperand(1))
2535 SRem = BO0;
Nick Lewyckydcf77572011-03-08 06:29:47 +00002536 // icmp Y, (srem X, Y)
Nick Lewycky9feda172011-03-05 04:28:48 +00002537 else if (BO1 && BO1->getOpcode() == Instruction::SRem &&
2538 Op0 == BO1->getOperand(1))
2539 SRem = BO1;
2540 if (SRem) {
2541 // We don't check hasOneUse to avoid increasing register pressure because
2542 // the value we use is the same value this instruction was already using.
2543 switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) {
2544 default: break;
2545 case ICmpInst::ICMP_EQ:
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002546 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
Nick Lewycky9feda172011-03-05 04:28:48 +00002547 case ICmpInst::ICMP_NE:
Nick Lewyckyd01f50f2011-03-06 03:36:19 +00002548 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
Nick Lewycky9feda172011-03-05 04:28:48 +00002549 case ICmpInst::ICMP_SGT:
2550 case ICmpInst::ICMP_SGE:
2551 return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1),
2552 Constant::getAllOnesValue(SRem->getType()));
2553 case ICmpInst::ICMP_SLT:
2554 case ICmpInst::ICMP_SLE:
2555 return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1),
2556 Constant::getNullValue(SRem->getType()));
2557 }
2558 }
2559
Duncan Sandsa7724332011-02-17 07:46:37 +00002560 if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() &&
2561 BO0->hasOneUse() && BO1->hasOneUse() &&
2562 BO0->getOperand(1) == BO1->getOperand(1)) {
2563 switch (BO0->getOpcode()) {
2564 default: break;
2565 case Instruction::Add:
2566 case Instruction::Sub:
2567 case Instruction::Xor:
2568 if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b
2569 return new ICmpInst(I.getPredicate(), BO0->getOperand(0),
2570 BO1->getOperand(0));
2571 // icmp u/s (a ^ signbit), (b ^ signbit) --> icmp s/u a, b
2572 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO0->getOperand(1))) {
2573 if (CI->getValue().isSignBit()) {
2574 ICmpInst::Predicate Pred = I.isSigned()
2575 ? I.getUnsignedPredicate()
2576 : I.getSignedPredicate();
2577 return new ICmpInst(Pred, BO0->getOperand(0),
2578 BO1->getOperand(0));
Chris Lattner02446fc2010-01-04 07:37:31 +00002579 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002580
Chris Lattnerc73b24d2011-07-15 06:08:15 +00002581 if (CI->isMaxValue(true)) {
Duncan Sandsa7724332011-02-17 07:46:37 +00002582 ICmpInst::Predicate Pred = I.isSigned()
2583 ? I.getUnsignedPredicate()
2584 : I.getSignedPredicate();
2585 Pred = I.getSwappedPredicate(Pred);
2586 return new ICmpInst(Pred, BO0->getOperand(0),
2587 BO1->getOperand(0));
2588 }
Chris Lattner02446fc2010-01-04 07:37:31 +00002589 }
Duncan Sandsa7724332011-02-17 07:46:37 +00002590 break;
2591 case Instruction::Mul:
2592 if (!I.isEquality())
2593 break;
2594
2595 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO0->getOperand(1))) {
2596 // a * Cst icmp eq/ne b * Cst --> a & Mask icmp b & Mask
2597 // Mask = -1 >> count-trailing-zeros(Cst).
2598 if (!CI->isZero() && !CI->isOne()) {
2599 const APInt &AP = CI->getValue();
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002600 ConstantInt *Mask = ConstantInt::get(I.getContext(),
Duncan Sandsa7724332011-02-17 07:46:37 +00002601 APInt::getLowBitsSet(AP.getBitWidth(),
2602 AP.getBitWidth() -
2603 AP.countTrailingZeros()));
2604 Value *And1 = Builder->CreateAnd(BO0->getOperand(0), Mask);
2605 Value *And2 = Builder->CreateAnd(BO1->getOperand(0), Mask);
2606 return new ICmpInst(I.getPredicate(), And1, And2);
2607 }
2608 }
2609 break;
Nick Lewycky58bfcdb2011-03-05 05:19:11 +00002610 case Instruction::UDiv:
2611 case Instruction::LShr:
2612 if (I.isSigned())
2613 break;
2614 // fall-through
2615 case Instruction::SDiv:
2616 case Instruction::AShr:
Eli Friedmanb6e7cd62011-05-05 21:59:18 +00002617 if (!BO0->isExact() || !BO1->isExact())
Nick Lewycky58bfcdb2011-03-05 05:19:11 +00002618 break;
2619 return new ICmpInst(I.getPredicate(), BO0->getOperand(0),
2620 BO1->getOperand(0));
2621 case Instruction::Shl: {
2622 bool NUW = BO0->hasNoUnsignedWrap() && BO1->hasNoUnsignedWrap();
2623 bool NSW = BO0->hasNoSignedWrap() && BO1->hasNoSignedWrap();
2624 if (!NUW && !NSW)
2625 break;
2626 if (!NSW && I.isSigned())
2627 break;
2628 return new ICmpInst(I.getPredicate(), BO0->getOperand(0),
2629 BO1->getOperand(0));
2630 }
Chris Lattner02446fc2010-01-04 07:37:31 +00002631 }
2632 }
2633 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002634
Chris Lattner02446fc2010-01-04 07:37:31 +00002635 { Value *A, *B;
Chris Lattnerfdb5b012011-01-15 05:41:33 +00002636 // ~x < ~y --> y < x
2637 // ~x < cst --> ~cst < x
2638 if (match(Op0, m_Not(m_Value(A)))) {
2639 if (match(Op1, m_Not(m_Value(B))))
2640 return new ICmpInst(I.getPredicate(), B, A);
Chris Lattner27a98482011-01-15 05:42:47 +00002641 if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1))
Chris Lattnerfdb5b012011-01-15 05:41:33 +00002642 return new ICmpInst(I.getPredicate(), ConstantExpr::getNot(RHSC), A);
2643 }
Chris Lattnere5cbdca2010-12-19 19:37:52 +00002644
2645 // (a+b) <u a --> llvm.uadd.with.overflow.
2646 // (a+b) <u b --> llvm.uadd.with.overflow.
2647 if (I.getPredicate() == ICmpInst::ICMP_ULT &&
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002648 match(Op0, m_Add(m_Value(A), m_Value(B))) &&
Chris Lattnere5cbdca2010-12-19 19:37:52 +00002649 (Op1 == A || Op1 == B))
2650 if (Instruction *R = ProcessUAddIdiom(I, Op0, *this))
2651 return R;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002652
Chris Lattnere5cbdca2010-12-19 19:37:52 +00002653 // a >u (a+b) --> llvm.uadd.with.overflow.
2654 // b >u (a+b) --> llvm.uadd.with.overflow.
2655 if (I.getPredicate() == ICmpInst::ICMP_UGT &&
2656 match(Op1, m_Add(m_Value(A), m_Value(B))) &&
2657 (Op0 == A || Op0 == B))
2658 if (Instruction *R = ProcessUAddIdiom(I, Op1, *this))
2659 return R;
Chris Lattner02446fc2010-01-04 07:37:31 +00002660 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002661
Chris Lattner02446fc2010-01-04 07:37:31 +00002662 if (I.isEquality()) {
2663 Value *A, *B, *C, *D;
Duncan Sands39a7de72011-02-18 16:25:37 +00002664
Chris Lattner02446fc2010-01-04 07:37:31 +00002665 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
2666 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
2667 Value *OtherVal = A == Op1 ? B : A;
2668 return new ICmpInst(I.getPredicate(), OtherVal,
2669 Constant::getNullValue(A->getType()));
2670 }
2671
2672 if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
2673 // A^c1 == C^c2 --> A == C^(c1^c2)
2674 ConstantInt *C1, *C2;
2675 if (match(B, m_ConstantInt(C1)) &&
2676 match(D, m_ConstantInt(C2)) && Op1->hasOneUse()) {
2677 Constant *NC = ConstantInt::get(I.getContext(),
2678 C1->getValue() ^ C2->getValue());
Benjamin Kramera9390a42011-09-27 20:39:19 +00002679 Value *Xor = Builder->CreateXor(C, NC);
Chris Lattner02446fc2010-01-04 07:37:31 +00002680 return new ICmpInst(I.getPredicate(), A, Xor);
2681 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002682
Chris Lattner02446fc2010-01-04 07:37:31 +00002683 // A^B == A^D -> B == D
2684 if (A == C) return new ICmpInst(I.getPredicate(), B, D);
2685 if (A == D) return new ICmpInst(I.getPredicate(), B, C);
2686 if (B == C) return new ICmpInst(I.getPredicate(), A, D);
2687 if (B == D) return new ICmpInst(I.getPredicate(), A, C);
2688 }
2689 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002690
Chris Lattner02446fc2010-01-04 07:37:31 +00002691 if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
2692 (A == Op0 || B == Op0)) {
2693 // A == (A^B) -> B == 0
2694 Value *OtherVal = A == Op0 ? B : A;
2695 return new ICmpInst(I.getPredicate(), OtherVal,
2696 Constant::getNullValue(A->getType()));
2697 }
2698
Chris Lattner02446fc2010-01-04 07:37:31 +00002699 // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002700 if (match(Op0, m_OneUse(m_And(m_Value(A), m_Value(B)))) &&
Chris Lattner5036ce42011-04-26 20:02:45 +00002701 match(Op1, m_OneUse(m_And(m_Value(C), m_Value(D))))) {
Chris Lattner02446fc2010-01-04 07:37:31 +00002702 Value *X = 0, *Y = 0, *Z = 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002703
Chris Lattner02446fc2010-01-04 07:37:31 +00002704 if (A == C) {
2705 X = B; Y = D; Z = A;
2706 } else if (A == D) {
2707 X = B; Y = C; Z = A;
2708 } else if (B == C) {
2709 X = A; Y = D; Z = B;
2710 } else if (B == D) {
2711 X = A; Y = C; Z = B;
2712 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002713
Chris Lattner02446fc2010-01-04 07:37:31 +00002714 if (X) { // Build (X^Y) & Z
Benjamin Kramera9390a42011-09-27 20:39:19 +00002715 Op1 = Builder->CreateXor(X, Y);
2716 Op1 = Builder->CreateAnd(Op1, Z);
Chris Lattner02446fc2010-01-04 07:37:31 +00002717 I.setOperand(0, Op1);
2718 I.setOperand(1, Constant::getNullValue(Op1->getType()));
2719 return &I;
2720 }
2721 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002722
Benjamin Kramer66821d92012-06-10 20:35:00 +00002723 // Transform (zext A) == (B & (1<<X)-1) --> A == (trunc B)
Benjamin Kramer7a99b462012-06-11 08:01:25 +00002724 // and (B & (1<<X)-1) == (zext A) --> A == (trunc B)
Benjamin Kramer66821d92012-06-10 20:35:00 +00002725 ConstantInt *Cst1;
Benjamin Kramer7a99b462012-06-11 08:01:25 +00002726 if ((Op0->hasOneUse() &&
2727 match(Op0, m_ZExt(m_Value(A))) &&
2728 match(Op1, m_And(m_Value(B), m_ConstantInt(Cst1)))) ||
2729 (Op1->hasOneUse() &&
2730 match(Op0, m_And(m_Value(B), m_ConstantInt(Cst1))) &&
2731 match(Op1, m_ZExt(m_Value(A))))) {
Benjamin Kramer66821d92012-06-10 20:35:00 +00002732 APInt Pow2 = Cst1->getValue() + 1;
2733 if (Pow2.isPowerOf2() && isa<IntegerType>(A->getType()) &&
2734 Pow2.logBase2() == cast<IntegerType>(A->getType())->getBitWidth())
2735 return new ICmpInst(I.getPredicate(), A,
2736 Builder->CreateTrunc(B, A->getType()));
2737 }
2738
Chris Lattner325eeb12011-04-26 20:18:20 +00002739 // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to
2740 // "icmp (and X, mask), cst"
2741 uint64_t ShAmt = 0;
Chris Lattner325eeb12011-04-26 20:18:20 +00002742 if (Op0->hasOneUse() &&
2743 match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A),
2744 m_ConstantInt(ShAmt))))) &&
2745 match(Op1, m_ConstantInt(Cst1)) &&
2746 // Only do this when A has multiple uses. This is most important to do
2747 // when it exposes other optimizations.
2748 !A->hasOneUse()) {
2749 unsigned ASize =cast<IntegerType>(A->getType())->getPrimitiveSizeInBits();
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002750
Chris Lattner325eeb12011-04-26 20:18:20 +00002751 if (ShAmt < ASize) {
2752 APInt MaskV =
2753 APInt::getLowBitsSet(ASize, Op0->getType()->getPrimitiveSizeInBits());
2754 MaskV <<= ShAmt;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002755
Chris Lattner325eeb12011-04-26 20:18:20 +00002756 APInt CmpV = Cst1->getValue().zext(ASize);
2757 CmpV <<= ShAmt;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002758
Chris Lattner325eeb12011-04-26 20:18:20 +00002759 Value *Mask = Builder->CreateAnd(A, Builder->getInt(MaskV));
2760 return new ICmpInst(I.getPredicate(), Mask, Builder->getInt(CmpV));
2761 }
2762 }
Chris Lattner02446fc2010-01-04 07:37:31 +00002763 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002764
Chris Lattner02446fc2010-01-04 07:37:31 +00002765 {
2766 Value *X; ConstantInt *Cst;
2767 // icmp X+Cst, X
2768 if (match(Op0, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op1 == X)
2769 return FoldICmpAddOpCst(I, X, Cst, I.getPredicate(), Op0);
2770
2771 // icmp X, X+Cst
2772 if (match(Op1, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op0 == X)
2773 return FoldICmpAddOpCst(I, X, Cst, I.getSwappedPredicate(), Op1);
2774 }
2775 return Changed ? &I : 0;
2776}
2777
2778
2779
2780
2781
2782
2783/// FoldFCmp_IntToFP_Cst - Fold fcmp ([us]itofp x, cst) if possible.
2784///
2785Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I,
2786 Instruction *LHSI,
2787 Constant *RHSC) {
2788 if (!isa<ConstantFP>(RHSC)) return 0;
2789 const APFloat &RHS = cast<ConstantFP>(RHSC)->getValueAPF();
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002790
Chris Lattner02446fc2010-01-04 07:37:31 +00002791 // Get the width of the mantissa. We don't want to hack on conversions that
2792 // might lose information from the integer, e.g. "i64 -> float"
2793 int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
2794 if (MantissaWidth == -1) return 0; // Unknown.
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002795
Chris Lattner02446fc2010-01-04 07:37:31 +00002796 // Check to see that the input is converted from an integer type that is small
2797 // enough that preserves all bits. TODO: check here for "known" sign bits.
2798 // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
2799 unsigned InputSize = LHSI->getOperand(0)->getType()->getScalarSizeInBits();
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002800
Chris Lattner02446fc2010-01-04 07:37:31 +00002801 // If this is a uitofp instruction, we need an extra bit to hold the sign.
2802 bool LHSUnsigned = isa<UIToFPInst>(LHSI);
2803 if (LHSUnsigned)
2804 ++InputSize;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002805
Chris Lattner02446fc2010-01-04 07:37:31 +00002806 // If the conversion would lose info, don't hack on this.
2807 if ((int)InputSize > MantissaWidth)
2808 return 0;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002809
Chris Lattner02446fc2010-01-04 07:37:31 +00002810 // Otherwise, we can potentially simplify the comparison. We know that it
2811 // will always come through as an integer value and we know the constant is
2812 // not a NAN (it would have been previously simplified).
2813 assert(!RHS.isNaN() && "NaN comparison not already folded!");
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002814
Chris Lattner02446fc2010-01-04 07:37:31 +00002815 ICmpInst::Predicate Pred;
2816 switch (I.getPredicate()) {
2817 default: llvm_unreachable("Unexpected predicate!");
2818 case FCmpInst::FCMP_UEQ:
2819 case FCmpInst::FCMP_OEQ:
2820 Pred = ICmpInst::ICMP_EQ;
2821 break;
2822 case FCmpInst::FCMP_UGT:
2823 case FCmpInst::FCMP_OGT:
2824 Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
2825 break;
2826 case FCmpInst::FCMP_UGE:
2827 case FCmpInst::FCMP_OGE:
2828 Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
2829 break;
2830 case FCmpInst::FCMP_ULT:
2831 case FCmpInst::FCMP_OLT:
2832 Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
2833 break;
2834 case FCmpInst::FCMP_ULE:
2835 case FCmpInst::FCMP_OLE:
2836 Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
2837 break;
2838 case FCmpInst::FCMP_UNE:
2839 case FCmpInst::FCMP_ONE:
2840 Pred = ICmpInst::ICMP_NE;
2841 break;
2842 case FCmpInst::FCMP_ORD:
2843 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2844 case FCmpInst::FCMP_UNO:
2845 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2846 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002847
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002848 IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType());
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002849
Chris Lattner02446fc2010-01-04 07:37:31 +00002850 // Now we know that the APFloat is a normal number, zero or inf.
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002851
Chris Lattner02446fc2010-01-04 07:37:31 +00002852 // See if the FP constant is too large for the integer. For example,
2853 // comparing an i8 to 300.0.
2854 unsigned IntWidth = IntTy->getScalarSizeInBits();
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002855
Chris Lattner02446fc2010-01-04 07:37:31 +00002856 if (!LHSUnsigned) {
2857 // If the RHS value is > SignedMax, fold the comparison. This handles +INF
2858 // and large values.
2859 APFloat SMax(RHS.getSemantics(), APFloat::fcZero, false);
2860 SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true,
2861 APFloat::rmNearestTiesToEven);
2862 if (SMax.compare(RHS) == APFloat::cmpLessThan) { // smax < 13123.0
2863 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||
2864 Pred == ICmpInst::ICMP_SLE)
2865 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2866 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2867 }
2868 } else {
2869 // If the RHS value is > UnsignedMax, fold the comparison. This handles
2870 // +INF and large values.
2871 APFloat UMax(RHS.getSemantics(), APFloat::fcZero, false);
2872 UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false,
2873 APFloat::rmNearestTiesToEven);
2874 if (UMax.compare(RHS) == APFloat::cmpLessThan) { // umax < 13123.0
2875 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||
2876 Pred == ICmpInst::ICMP_ULE)
2877 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2878 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2879 }
2880 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002881
Chris Lattner02446fc2010-01-04 07:37:31 +00002882 if (!LHSUnsigned) {
2883 // See if the RHS value is < SignedMin.
2884 APFloat SMin(RHS.getSemantics(), APFloat::fcZero, false);
2885 SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true,
2886 APFloat::rmNearestTiesToEven);
2887 if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0
2888 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
2889 Pred == ICmpInst::ICMP_SGE)
2890 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2891 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2892 }
Devang Patela2e0f6b2012-02-13 23:05:18 +00002893 } else {
2894 // See if the RHS value is < UnsignedMin.
2895 APFloat SMin(RHS.getSemantics(), APFloat::fcZero, false);
2896 SMin.convertFromAPInt(APInt::getMinValue(IntWidth), true,
2897 APFloat::rmNearestTiesToEven);
2898 if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // umin > 12312.0
2899 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||
2900 Pred == ICmpInst::ICMP_UGE)
2901 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2902 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2903 }
Chris Lattner02446fc2010-01-04 07:37:31 +00002904 }
2905
2906 // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
2907 // [0, UMAX], but it may still be fractional. See if it is fractional by
2908 // casting the FP value to the integer value and back, checking for equality.
2909 // Don't do this for zero, because -0.0 is not fractional.
2910 Constant *RHSInt = LHSUnsigned
2911 ? ConstantExpr::getFPToUI(RHSC, IntTy)
2912 : ConstantExpr::getFPToSI(RHSC, IntTy);
2913 if (!RHS.isZero()) {
2914 bool Equal = LHSUnsigned
2915 ? ConstantExpr::getUIToFP(RHSInt, RHSC->getType()) == RHSC
2916 : ConstantExpr::getSIToFP(RHSInt, RHSC->getType()) == RHSC;
2917 if (!Equal) {
2918 // If we had a comparison against a fractional value, we have to adjust
2919 // the compare predicate and sometimes the value. RHSC is rounded towards
2920 // zero at this point.
2921 switch (Pred) {
2922 default: llvm_unreachable("Unexpected integer comparison!");
2923 case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
2924 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2925 case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
2926 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2927 case ICmpInst::ICMP_ULE:
2928 // (float)int <= 4.4 --> int <= 4
2929 // (float)int <= -4.4 --> false
2930 if (RHS.isNegative())
2931 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2932 break;
2933 case ICmpInst::ICMP_SLE:
2934 // (float)int <= 4.4 --> int <= 4
2935 // (float)int <= -4.4 --> int < -4
2936 if (RHS.isNegative())
2937 Pred = ICmpInst::ICMP_SLT;
2938 break;
2939 case ICmpInst::ICMP_ULT:
2940 // (float)int < -4.4 --> false
2941 // (float)int < 4.4 --> int <= 4
2942 if (RHS.isNegative())
2943 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2944 Pred = ICmpInst::ICMP_ULE;
2945 break;
2946 case ICmpInst::ICMP_SLT:
2947 // (float)int < -4.4 --> int < -4
2948 // (float)int < 4.4 --> int <= 4
2949 if (!RHS.isNegative())
2950 Pred = ICmpInst::ICMP_SLE;
2951 break;
2952 case ICmpInst::ICMP_UGT:
2953 // (float)int > 4.4 --> int > 4
2954 // (float)int > -4.4 --> true
2955 if (RHS.isNegative())
2956 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2957 break;
2958 case ICmpInst::ICMP_SGT:
2959 // (float)int > 4.4 --> int > 4
2960 // (float)int > -4.4 --> int >= -4
2961 if (RHS.isNegative())
2962 Pred = ICmpInst::ICMP_SGE;
2963 break;
2964 case ICmpInst::ICMP_UGE:
2965 // (float)int >= -4.4 --> true
2966 // (float)int >= 4.4 --> int > 4
Bob Wilsonf12c95a2012-08-07 22:35:16 +00002967 if (RHS.isNegative())
Chris Lattner02446fc2010-01-04 07:37:31 +00002968 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2969 Pred = ICmpInst::ICMP_UGT;
2970 break;
2971 case ICmpInst::ICMP_SGE:
2972 // (float)int >= -4.4 --> int >= -4
2973 // (float)int >= 4.4 --> int > 4
2974 if (!RHS.isNegative())
2975 Pred = ICmpInst::ICMP_SGT;
2976 break;
2977 }
2978 }
2979 }
2980
2981 // Lower this FP comparison into an appropriate integer version of the
2982 // comparison.
2983 return new ICmpInst(Pred, LHSI->getOperand(0), RHSInt);
2984}
2985
2986Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) {
2987 bool Changed = false;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002988
Chris Lattner02446fc2010-01-04 07:37:31 +00002989 /// Orders the operands of the compare so that they are listed from most
2990 /// complex to least complex. This puts constants before unary operators,
2991 /// before binary operators.
2992 if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) {
2993 I.swapOperands();
2994 Changed = true;
2995 }
2996
2997 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Jim Grosbach0cc4a952011-09-30 18:09:53 +00002998
Chris Lattner02446fc2010-01-04 07:37:31 +00002999 if (Value *V = SimplifyFCmpInst(I.getPredicate(), Op0, Op1, TD))
3000 return ReplaceInstUsesWith(I, V);
3001
3002 // Simplify 'fcmp pred X, X'
3003 if (Op0 == Op1) {
3004 switch (I.getPredicate()) {
3005 default: llvm_unreachable("Unknown predicate!");
3006 case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
3007 case FCmpInst::FCMP_ULT: // True if unordered or less than
3008 case FCmpInst::FCMP_UGT: // True if unordered or greater than
3009 case FCmpInst::FCMP_UNE: // True if unordered or not equal
3010 // Canonicalize these to be 'fcmp uno %X, 0.0'.
3011 I.setPredicate(FCmpInst::FCMP_UNO);
3012 I.setOperand(1, Constant::getNullValue(Op0->getType()));
3013 return &I;
Jim Grosbach0cc4a952011-09-30 18:09:53 +00003014
Chris Lattner02446fc2010-01-04 07:37:31 +00003015 case FCmpInst::FCMP_ORD: // True if ordered (no nans)
3016 case FCmpInst::FCMP_OEQ: // True if ordered and equal
3017 case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal
3018 case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
3019 // Canonicalize these to be 'fcmp ord %X, 0.0'.
3020 I.setPredicate(FCmpInst::FCMP_ORD);
3021 I.setOperand(1, Constant::getNullValue(Op0->getType()));
3022 return &I;
3023 }
3024 }
Jim Grosbach0cc4a952011-09-30 18:09:53 +00003025
Chris Lattner02446fc2010-01-04 07:37:31 +00003026 // Handle fcmp with constant RHS
3027 if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
3028 if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
3029 switch (LHSI->getOpcode()) {
Benjamin Kramerb194bdc2011-03-31 10:12:07 +00003030 case Instruction::FPExt: {
3031 // fcmp (fpext x), C -> fcmp x, (fptrunc C) if fptrunc is lossless
3032 FPExtInst *LHSExt = cast<FPExtInst>(LHSI);
3033 ConstantFP *RHSF = dyn_cast<ConstantFP>(RHSC);
3034 if (!RHSF)
3035 break;
3036
3037 const fltSemantics *Sem;
3038 // FIXME: This shouldn't be here.
Dan Gohmance163392011-12-17 00:04:22 +00003039 if (LHSExt->getSrcTy()->isHalfTy())
3040 Sem = &APFloat::IEEEhalf;
3041 else if (LHSExt->getSrcTy()->isFloatTy())
Benjamin Kramerb194bdc2011-03-31 10:12:07 +00003042 Sem = &APFloat::IEEEsingle;
3043 else if (LHSExt->getSrcTy()->isDoubleTy())
3044 Sem = &APFloat::IEEEdouble;
3045 else if (LHSExt->getSrcTy()->isFP128Ty())
3046 Sem = &APFloat::IEEEquad;
3047 else if (LHSExt->getSrcTy()->isX86_FP80Ty())
3048 Sem = &APFloat::x87DoubleExtended;
Ulrich Weigand3467b9f2012-10-30 12:33:18 +00003049 else if (LHSExt->getSrcTy()->isPPC_FP128Ty())
3050 Sem = &APFloat::PPCDoubleDouble;
Benjamin Kramerb194bdc2011-03-31 10:12:07 +00003051 else
3052 break;
3053
3054 bool Lossy;
3055 APFloat F = RHSF->getValueAPF();
3056 F.convert(*Sem, APFloat::rmNearestTiesToEven, &Lossy);
3057
Jim Grosbachcbf676b2011-09-30 18:45:50 +00003058 // Avoid lossy conversions and denormals. Zero is a special case
3059 // that's OK to convert.
Jim Grosbach68e05fb2011-09-30 19:58:46 +00003060 APFloat Fabs = F;
3061 Fabs.clearSign();
Benjamin Kramerb194bdc2011-03-31 10:12:07 +00003062 if (!Lossy &&
Jim Grosbach68e05fb2011-09-30 19:58:46 +00003063 ((Fabs.compare(APFloat::getSmallestNormalized(*Sem)) !=
3064 APFloat::cmpLessThan) || Fabs.isZero()))
Jim Grosbachcbf676b2011-09-30 18:45:50 +00003065
Benjamin Kramerb194bdc2011-03-31 10:12:07 +00003066 return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0),
3067 ConstantFP::get(RHSC->getContext(), F));
3068 break;
3069 }
Chris Lattner02446fc2010-01-04 07:37:31 +00003070 case Instruction::PHI:
3071 // Only fold fcmp into the PHI if the phi and fcmp are in the same
3072 // block. If in the same block, we're encouraging jump threading. If
3073 // not, we are just pessimizing the code by making an i1 phi.
3074 if (LHSI->getParent() == I.getParent())
Chris Lattner9922ccf2011-01-16 05:14:26 +00003075 if (Instruction *NV = FoldOpIntoPhi(I))
Chris Lattner02446fc2010-01-04 07:37:31 +00003076 return NV;
3077 break;
3078 case Instruction::SIToFP:
3079 case Instruction::UIToFP:
3080 if (Instruction *NV = FoldFCmp_IntToFP_Cst(I, LHSI, RHSC))
3081 return NV;
3082 break;
3083 case Instruction::Select: {
3084 // If either operand of the select is a constant, we can fold the
3085 // comparison into the select arms, which will cause one to be
3086 // constant folded and the select turned into a bitwise or.
3087 Value *Op1 = 0, *Op2 = 0;
3088 if (LHSI->hasOneUse()) {
3089 if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
3090 // Fold the known value into the constant operand.
3091 Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC);
3092 // Insert a new FCmp of the other select operand.
3093 Op2 = Builder->CreateFCmp(I.getPredicate(),
3094 LHSI->getOperand(2), RHSC, I.getName());
3095 } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
3096 // Fold the known value into the constant operand.
3097 Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC);
3098 // Insert a new FCmp of the other select operand.
3099 Op1 = Builder->CreateFCmp(I.getPredicate(), LHSI->getOperand(1),
3100 RHSC, I.getName());
3101 }
3102 }
3103
3104 if (Op1)
3105 return SelectInst::Create(LHSI->getOperand(0), Op1, Op2);
3106 break;
3107 }
Benjamin Kramer0db50182011-03-31 10:12:15 +00003108 case Instruction::FSub: {
3109 // fcmp pred (fneg x), C -> fcmp swap(pred) x, -C
3110 Value *Op;
3111 if (match(LHSI, m_FNeg(m_Value(Op))))
3112 return new FCmpInst(I.getSwappedPredicate(), Op,
3113 ConstantExpr::getFNeg(RHSC));
3114 break;
3115 }
Dan Gohman39516a62010-02-24 06:46:09 +00003116 case Instruction::Load:
3117 if (GetElementPtrInst *GEP =
3118 dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) {
3119 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
3120 if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
3121 !cast<LoadInst>(LHSI)->isVolatile())
3122 if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV, I))
3123 return Res;
3124 }
3125 break;
Benjamin Kramer00abcd32012-08-18 20:06:47 +00003126 case Instruction::Call: {
3127 CallInst *CI = cast<CallInst>(LHSI);
3128 LibFunc::Func Func;
3129 // Various optimization for fabs compared with zero.
Benjamin Kramera4b57172012-08-18 22:04:34 +00003130 if (RHSC->isNullValue() && CI->getCalledFunction() &&
Benjamin Kramer00abcd32012-08-18 20:06:47 +00003131 TLI->getLibFunc(CI->getCalledFunction()->getName(), Func) &&
3132 TLI->has(Func)) {
3133 if (Func == LibFunc::fabs || Func == LibFunc::fabsf ||
3134 Func == LibFunc::fabsl) {
3135 switch (I.getPredicate()) {
3136 default: break;
3137 // fabs(x) < 0 --> false
3138 case FCmpInst::FCMP_OLT:
3139 return ReplaceInstUsesWith(I, Builder->getFalse());
3140 // fabs(x) > 0 --> x != 0
3141 case FCmpInst::FCMP_OGT:
3142 return new FCmpInst(FCmpInst::FCMP_ONE, CI->getArgOperand(0),
3143 RHSC);
3144 // fabs(x) <= 0 --> x == 0
3145 case FCmpInst::FCMP_OLE:
3146 return new FCmpInst(FCmpInst::FCMP_OEQ, CI->getArgOperand(0),
3147 RHSC);
3148 // fabs(x) >= 0 --> !isnan(x)
3149 case FCmpInst::FCMP_OGE:
3150 return new FCmpInst(FCmpInst::FCMP_ORD, CI->getArgOperand(0),
3151 RHSC);
3152 // fabs(x) == 0 --> x == 0
3153 // fabs(x) != 0 --> x != 0
3154 case FCmpInst::FCMP_OEQ:
3155 case FCmpInst::FCMP_UEQ:
3156 case FCmpInst::FCMP_ONE:
3157 case FCmpInst::FCMP_UNE:
3158 return new FCmpInst(I.getPredicate(), CI->getArgOperand(0),
3159 RHSC);
3160 }
3161 }
3162 }
3163 }
Chris Lattner02446fc2010-01-04 07:37:31 +00003164 }
Chris Lattner02446fc2010-01-04 07:37:31 +00003165 }
3166
Benjamin Kramer00e00d62011-03-31 10:46:03 +00003167 // fcmp pred (fneg x), (fneg y) -> fcmp swap(pred) x, y
Benjamin Kramer68b4bd02011-03-31 10:12:22 +00003168 Value *X, *Y;
3169 if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))
Benjamin Kramer00e00d62011-03-31 10:46:03 +00003170 return new FCmpInst(I.getSwappedPredicate(), X, Y);
Benjamin Kramer68b4bd02011-03-31 10:12:22 +00003171
Benjamin Kramercd0274c2011-03-31 10:11:58 +00003172 // fcmp (fpext x), (fpext y) -> fcmp x, y
3173 if (FPExtInst *LHSExt = dyn_cast<FPExtInst>(Op0))
3174 if (FPExtInst *RHSExt = dyn_cast<FPExtInst>(Op1))
3175 if (LHSExt->getSrcTy() == RHSExt->getSrcTy())
3176 return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0),
3177 RHSExt->getOperand(0));
3178
Chris Lattner02446fc2010-01-04 07:37:31 +00003179 return Changed ? &I : 0;
3180}