blob: 11426863f61e01c181d85e92a4bb40af67b58808 [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"
15#include "llvm/IntrinsicInst.h"
16#include "llvm/Analysis/InstructionSimplify.h"
17#include "llvm/Analysis/MemoryBuiltins.h"
18#include "llvm/Target/TargetData.h"
19#include "llvm/Support/ConstantRange.h"
20#include "llvm/Support/GetElementPtrTypeIterator.h"
21#include "llvm/Support/PatternMatch.h"
22using namespace llvm;
23using namespace PatternMatch;
24
25/// AddOne - Add one to a ConstantInt
26static Constant *AddOne(Constant *C) {
27 return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
28}
29/// SubOne - Subtract one from a ConstantInt
30static Constant *SubOne(ConstantInt *C) {
31 return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
32}
33
34static ConstantInt *ExtractElement(Constant *V, Constant *Idx) {
35 return cast<ConstantInt>(ConstantExpr::getExtractElement(V, Idx));
36}
37
38static bool HasAddOverflow(ConstantInt *Result,
39 ConstantInt *In1, ConstantInt *In2,
40 bool IsSigned) {
41 if (IsSigned)
42 if (In2->getValue().isNegative())
43 return Result->getValue().sgt(In1->getValue());
44 else
45 return Result->getValue().slt(In1->getValue());
46 else
47 return Result->getValue().ult(In1->getValue());
48}
49
50/// AddWithOverflow - Compute Result = In1+In2, returning true if the result
51/// overflowed for this type.
52static bool AddWithOverflow(Constant *&Result, Constant *In1,
53 Constant *In2, bool IsSigned = false) {
54 Result = ConstantExpr::getAdd(In1, In2);
55
56 if (const VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
57 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
58 Constant *Idx = ConstantInt::get(Type::getInt32Ty(In1->getContext()), i);
59 if (HasAddOverflow(ExtractElement(Result, Idx),
60 ExtractElement(In1, Idx),
61 ExtractElement(In2, Idx),
62 IsSigned))
63 return true;
64 }
65 return false;
66 }
67
68 return HasAddOverflow(cast<ConstantInt>(Result),
69 cast<ConstantInt>(In1), cast<ConstantInt>(In2),
70 IsSigned);
71}
72
73static bool HasSubOverflow(ConstantInt *Result,
74 ConstantInt *In1, ConstantInt *In2,
75 bool IsSigned) {
76 if (IsSigned)
77 if (In2->getValue().isNegative())
78 return Result->getValue().slt(In1->getValue());
79 else
80 return Result->getValue().sgt(In1->getValue());
81 else
82 return Result->getValue().ugt(In1->getValue());
83}
84
85/// SubWithOverflow - Compute Result = In1-In2, returning true if the result
86/// overflowed for this type.
87static bool SubWithOverflow(Constant *&Result, Constant *In1,
88 Constant *In2, bool IsSigned = false) {
89 Result = ConstantExpr::getSub(In1, In2);
90
91 if (const VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
92 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
93 Constant *Idx = ConstantInt::get(Type::getInt32Ty(In1->getContext()), i);
94 if (HasSubOverflow(ExtractElement(Result, Idx),
95 ExtractElement(In1, Idx),
96 ExtractElement(In2, Idx),
97 IsSigned))
98 return true;
99 }
100 return false;
101 }
102
103 return HasSubOverflow(cast<ConstantInt>(Result),
104 cast<ConstantInt>(In1), cast<ConstantInt>(In2),
105 IsSigned);
106}
107
108/// isSignBitCheck - Given an exploded icmp instruction, return true if the
109/// comparison only checks the sign bit. If it only checks the sign bit, set
110/// TrueIfSigned if the result of the comparison is true when the input value is
111/// signed.
112static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS,
113 bool &TrueIfSigned) {
114 switch (pred) {
115 case ICmpInst::ICMP_SLT: // True if LHS s< 0
116 TrueIfSigned = true;
117 return RHS->isZero();
118 case ICmpInst::ICMP_SLE: // True if LHS s<= RHS and RHS == -1
119 TrueIfSigned = true;
120 return RHS->isAllOnesValue();
121 case ICmpInst::ICMP_SGT: // True if LHS s> -1
122 TrueIfSigned = false;
123 return RHS->isAllOnesValue();
124 case ICmpInst::ICMP_UGT:
125 // True if LHS u> RHS and RHS == high-bit-mask - 1
126 TrueIfSigned = true;
127 return RHS->getValue() ==
128 APInt::getSignedMaxValue(RHS->getType()->getPrimitiveSizeInBits());
129 case ICmpInst::ICMP_UGE:
130 // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc)
131 TrueIfSigned = true;
132 return RHS->getValue().isSignBit();
133 default:
134 return false;
135 }
136}
137
138// isHighOnes - Return true if the constant is of the form 1+0+.
139// This is the same as lowones(~X).
140static bool isHighOnes(const ConstantInt *CI) {
141 return (~CI->getValue() + 1).isPowerOf2();
142}
143
144/// ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a
145/// set of known zero and one bits, compute the maximum and minimum values that
146/// could have the specified known zero and known one bits, returning them in
147/// min/max.
148static void ComputeSignedMinMaxValuesFromKnownBits(const APInt& KnownZero,
149 const APInt& KnownOne,
150 APInt& Min, APInt& Max) {
151 assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() &&
152 KnownZero.getBitWidth() == Min.getBitWidth() &&
153 KnownZero.getBitWidth() == Max.getBitWidth() &&
154 "KnownZero, KnownOne and Min, Max must have equal bitwidth.");
155 APInt UnknownBits = ~(KnownZero|KnownOne);
156
157 // The minimum value is when all unknown bits are zeros, EXCEPT for the sign
158 // bit if it is unknown.
159 Min = KnownOne;
160 Max = KnownOne|UnknownBits;
161
162 if (UnknownBits.isNegative()) { // Sign bit is unknown
Jay Foad7a874dd2010-12-01 08:53:58 +0000163 Min.setBit(Min.getBitWidth()-1);
164 Max.clearBit(Max.getBitWidth()-1);
Chris Lattner02446fc2010-01-04 07:37:31 +0000165 }
166}
167
168// ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and
169// a set of known zero and one bits, compute the maximum and minimum values that
170// could have the specified known zero and known one bits, returning them in
171// min/max.
172static void ComputeUnsignedMinMaxValuesFromKnownBits(const APInt &KnownZero,
173 const APInt &KnownOne,
174 APInt &Min, APInt &Max) {
175 assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() &&
176 KnownZero.getBitWidth() == Min.getBitWidth() &&
177 KnownZero.getBitWidth() == Max.getBitWidth() &&
178 "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth.");
179 APInt UnknownBits = ~(KnownZero|KnownOne);
180
181 // The minimum value is when the unknown bits are all zeros.
182 Min = KnownOne;
183 // The maximum value is when the unknown bits are all ones.
184 Max = KnownOne|UnknownBits;
185}
186
187
188
189/// FoldCmpLoadFromIndexedGlobal - Called we see this pattern:
190/// cmp pred (load (gep GV, ...)), cmpcst
191/// where GV is a global variable with a constant initializer. Try to simplify
192/// this into some simple computation that does not need the load. For example
193/// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3".
194///
195/// If AndCst is non-null, then the loaded value is masked with that constant
196/// before doing the comparison. This handles cases like "A[i]&4 == 0".
197Instruction *InstCombiner::
198FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV,
199 CmpInst &ICI, ConstantInt *AndCst) {
Chris Lattnerd7f5a582010-01-04 18:57:15 +0000200 // We need TD information to know the pointer size unless this is inbounds.
201 if (!GEP->isInBounds() && TD == 0) return 0;
202
Chris Lattner02446fc2010-01-04 07:37:31 +0000203 ConstantArray *Init = dyn_cast<ConstantArray>(GV->getInitializer());
204 if (Init == 0 || Init->getNumOperands() > 1024) return 0;
205
206 // There are many forms of this optimization we can handle, for now, just do
207 // the simple index into a single-dimensional array.
208 //
209 // Require: GEP GV, 0, i {{, constant indices}}
210 if (GEP->getNumOperands() < 3 ||
211 !isa<ConstantInt>(GEP->getOperand(1)) ||
212 !cast<ConstantInt>(GEP->getOperand(1))->isZero() ||
213 isa<Constant>(GEP->getOperand(2)))
214 return 0;
215
216 // Check that indices after the variable are constants and in-range for the
217 // type they index. Collect the indices. This is typically for arrays of
218 // structs.
219 SmallVector<unsigned, 4> LaterIndices;
220
221 const Type *EltTy = cast<ArrayType>(Init->getType())->getElementType();
222 for (unsigned i = 3, e = GEP->getNumOperands(); i != e; ++i) {
223 ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(i));
224 if (Idx == 0) return 0; // Variable index.
225
226 uint64_t IdxVal = Idx->getZExtValue();
227 if ((unsigned)IdxVal != IdxVal) return 0; // Too large array index.
228
229 if (const StructType *STy = dyn_cast<StructType>(EltTy))
230 EltTy = STy->getElementType(IdxVal);
231 else if (const ArrayType *ATy = dyn_cast<ArrayType>(EltTy)) {
232 if (IdxVal >= ATy->getNumElements()) return 0;
233 EltTy = ATy->getElementType();
234 } else {
235 return 0; // Unknown type.
236 }
237
238 LaterIndices.push_back(IdxVal);
239 }
240
241 enum { Overdefined = -3, Undefined = -2 };
242
243 // Variables for our state machines.
244
245 // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form
246 // "i == 47 | i == 87", where 47 is the first index the condition is true for,
247 // and 87 is the second (and last) index. FirstTrueElement is -2 when
248 // undefined, otherwise set to the first true element. SecondTrueElement is
249 // -2 when undefined, -3 when overdefined and >= 0 when that index is true.
250 int FirstTrueElement = Undefined, SecondTrueElement = Undefined;
251
252 // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the
253 // form "i != 47 & i != 87". Same state transitions as for true elements.
254 int FirstFalseElement = Undefined, SecondFalseElement = Undefined;
255
256 /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these
257 /// define a state machine that triggers for ranges of values that the index
258 /// is true or false for. This triggers on things like "abbbbc"[i] == 'b'.
259 /// This is -2 when undefined, -3 when overdefined, and otherwise the last
260 /// index in the range (inclusive). We use -2 for undefined here because we
261 /// use relative comparisons and don't want 0-1 to match -1.
262 int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined;
263
264 // MagicBitvector - This is a magic bitvector where we set a bit if the
265 // comparison is true for element 'i'. If there are 64 elements or less in
266 // the array, this will fully represent all the comparison results.
267 uint64_t MagicBitvector = 0;
268
269
270 // Scan the array and see if one of our patterns matches.
271 Constant *CompareRHS = cast<Constant>(ICI.getOperand(1));
272 for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
273 Constant *Elt = Init->getOperand(i);
274
275 // If this is indexing an array of structures, get the structure element.
276 if (!LaterIndices.empty())
277 Elt = ConstantExpr::getExtractValue(Elt, LaterIndices.data(),
278 LaterIndices.size());
279
280 // If the element is masked, handle it.
281 if (AndCst) Elt = ConstantExpr::getAnd(Elt, AndCst);
282
283 // Find out if the comparison would be true or false for the i'th element.
284 Constant *C = ConstantFoldCompareInstOperands(ICI.getPredicate(), Elt,
285 CompareRHS, TD);
286 // If the result is undef for this element, ignore it.
287 if (isa<UndefValue>(C)) {
288 // Extend range state machines to cover this element in case there is an
289 // undef in the middle of the range.
290 if (TrueRangeEnd == (int)i-1)
291 TrueRangeEnd = i;
292 if (FalseRangeEnd == (int)i-1)
293 FalseRangeEnd = i;
294 continue;
295 }
296
297 // If we can't compute the result for any of the elements, we have to give
298 // up evaluating the entire conditional.
299 if (!isa<ConstantInt>(C)) return 0;
300
301 // Otherwise, we know if the comparison is true or false for this element,
302 // update our state machines.
303 bool IsTrueForElt = !cast<ConstantInt>(C)->isZero();
304
305 // State machine for single/double/range index comparison.
306 if (IsTrueForElt) {
307 // Update the TrueElement state machine.
308 if (FirstTrueElement == Undefined)
309 FirstTrueElement = TrueRangeEnd = i; // First true element.
310 else {
311 // Update double-compare state machine.
312 if (SecondTrueElement == Undefined)
313 SecondTrueElement = i;
314 else
315 SecondTrueElement = Overdefined;
316
317 // Update range state machine.
318 if (TrueRangeEnd == (int)i-1)
319 TrueRangeEnd = i;
320 else
321 TrueRangeEnd = Overdefined;
322 }
323 } else {
324 // Update the FalseElement state machine.
325 if (FirstFalseElement == Undefined)
326 FirstFalseElement = FalseRangeEnd = i; // First false element.
327 else {
328 // Update double-compare state machine.
329 if (SecondFalseElement == Undefined)
330 SecondFalseElement = i;
331 else
332 SecondFalseElement = Overdefined;
333
334 // Update range state machine.
335 if (FalseRangeEnd == (int)i-1)
336 FalseRangeEnd = i;
337 else
338 FalseRangeEnd = Overdefined;
339 }
340 }
341
342
343 // If this element is in range, update our magic bitvector.
344 if (i < 64 && IsTrueForElt)
345 MagicBitvector |= 1ULL << i;
346
347 // If all of our states become overdefined, bail out early. Since the
348 // predicate is expensive, only check it every 8 elements. This is only
349 // really useful for really huge arrays.
350 if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined &&
351 SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined &&
352 FalseRangeEnd == Overdefined)
353 return 0;
354 }
355
356 // Now that we've scanned the entire array, emit our new comparison(s). We
357 // order the state machines in complexity of the generated code.
358 Value *Idx = GEP->getOperand(2);
359
Chris Lattnerd7f5a582010-01-04 18:57:15 +0000360 // If the index is larger than the pointer size of the target, truncate the
361 // index down like the GEP would do implicitly. We don't have to do this for
362 // an inbounds GEP because the index can't be out of range.
363 if (!GEP->isInBounds() &&
364 Idx->getType()->getPrimitiveSizeInBits() > TD->getPointerSizeInBits())
365 Idx = Builder->CreateTrunc(Idx, TD->getIntPtrType(Idx->getContext()));
Chris Lattner02446fc2010-01-04 07:37:31 +0000366
367 // If the comparison is only true for one or two elements, emit direct
368 // comparisons.
369 if (SecondTrueElement != Overdefined) {
370 // None true -> false.
371 if (FirstTrueElement == Undefined)
372 return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(GEP->getContext()));
373
374 Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement);
375
376 // True for one element -> 'i == 47'.
377 if (SecondTrueElement == Undefined)
378 return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx);
379
380 // True for two elements -> 'i == 47 | i == 72'.
381 Value *C1 = Builder->CreateICmpEQ(Idx, FirstTrueIdx);
382 Value *SecondTrueIdx = ConstantInt::get(Idx->getType(), SecondTrueElement);
383 Value *C2 = Builder->CreateICmpEQ(Idx, SecondTrueIdx);
384 return BinaryOperator::CreateOr(C1, C2);
385 }
386
387 // If the comparison is only false for one or two elements, emit direct
388 // comparisons.
389 if (SecondFalseElement != Overdefined) {
390 // None false -> true.
391 if (FirstFalseElement == Undefined)
392 return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(GEP->getContext()));
393
394 Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement);
395
396 // False for one element -> 'i != 47'.
397 if (SecondFalseElement == Undefined)
398 return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx);
399
400 // False for two elements -> 'i != 47 & i != 72'.
401 Value *C1 = Builder->CreateICmpNE(Idx, FirstFalseIdx);
402 Value *SecondFalseIdx = ConstantInt::get(Idx->getType(),SecondFalseElement);
403 Value *C2 = Builder->CreateICmpNE(Idx, SecondFalseIdx);
404 return BinaryOperator::CreateAnd(C1, C2);
405 }
406
407 // If the comparison can be replaced with a range comparison for the elements
408 // where it is true, emit the range check.
409 if (TrueRangeEnd != Overdefined) {
410 assert(TrueRangeEnd != FirstTrueElement && "Should emit single compare");
411
412 // Generate (i-FirstTrue) <u (TrueRangeEnd-FirstTrue+1).
413 if (FirstTrueElement) {
414 Value *Offs = ConstantInt::get(Idx->getType(), -FirstTrueElement);
415 Idx = Builder->CreateAdd(Idx, Offs);
416 }
417
418 Value *End = ConstantInt::get(Idx->getType(),
419 TrueRangeEnd-FirstTrueElement+1);
420 return new ICmpInst(ICmpInst::ICMP_ULT, Idx, End);
421 }
422
423 // False range check.
424 if (FalseRangeEnd != Overdefined) {
425 assert(FalseRangeEnd != FirstFalseElement && "Should emit single compare");
426 // Generate (i-FirstFalse) >u (FalseRangeEnd-FirstFalse).
427 if (FirstFalseElement) {
428 Value *Offs = ConstantInt::get(Idx->getType(), -FirstFalseElement);
429 Idx = Builder->CreateAdd(Idx, Offs);
430 }
431
432 Value *End = ConstantInt::get(Idx->getType(),
433 FalseRangeEnd-FirstFalseElement);
434 return new ICmpInst(ICmpInst::ICMP_UGT, Idx, End);
435 }
436
437
438 // If a 32-bit or 64-bit magic bitvector captures the entire comparison state
439 // of this load, replace it with computation that does:
440 // ((magic_cst >> i) & 1) != 0
441 if (Init->getNumOperands() <= 32 ||
442 (TD && Init->getNumOperands() <= 64 && TD->isLegalInteger(64))) {
443 const Type *Ty;
444 if (Init->getNumOperands() <= 32)
445 Ty = Type::getInt32Ty(Init->getContext());
446 else
447 Ty = Type::getInt64Ty(Init->getContext());
448 Value *V = Builder->CreateIntCast(Idx, Ty, false);
449 V = Builder->CreateLShr(ConstantInt::get(Ty, MagicBitvector), V);
450 V = Builder->CreateAnd(ConstantInt::get(Ty, 1), V);
451 return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));
452 }
453
454 return 0;
455}
456
457
458/// EvaluateGEPOffsetExpression - Return a value that can be used to compare
459/// the *offset* implied by a GEP to zero. For example, if we have &A[i], we
460/// want to return 'i' for "icmp ne i, 0". Note that, in general, indices can
461/// be complex, and scales are involved. The above expression would also be
462/// legal to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32).
463/// This later form is less amenable to optimization though, and we are allowed
464/// to generate the first by knowing that pointer arithmetic doesn't overflow.
465///
466/// If we can't emit an optimized form for this expression, this returns null.
467///
468static Value *EvaluateGEPOffsetExpression(User *GEP, Instruction &I,
469 InstCombiner &IC) {
470 TargetData &TD = *IC.getTargetData();
471 gep_type_iterator GTI = gep_type_begin(GEP);
472
473 // Check to see if this gep only has a single variable index. If so, and if
474 // any constant indices are a multiple of its scale, then we can compute this
475 // in terms of the scale of the variable index. For example, if the GEP
476 // implies an offset of "12 + i*4", then we can codegen this as "3 + i",
477 // because the expression will cross zero at the same point.
478 unsigned i, e = GEP->getNumOperands();
479 int64_t Offset = 0;
480 for (i = 1; i != e; ++i, ++GTI) {
481 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
482 // Compute the aggregate offset of constant indices.
483 if (CI->isZero()) continue;
484
485 // Handle a struct index, which adds its field offset to the pointer.
486 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
487 Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
488 } else {
489 uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType());
490 Offset += Size*CI->getSExtValue();
491 }
492 } else {
493 // Found our variable index.
494 break;
495 }
496 }
497
498 // If there are no variable indices, we must have a constant offset, just
499 // evaluate it the general way.
500 if (i == e) return 0;
501
502 Value *VariableIdx = GEP->getOperand(i);
503 // Determine the scale factor of the variable element. For example, this is
504 // 4 if the variable index is into an array of i32.
505 uint64_t VariableScale = TD.getTypeAllocSize(GTI.getIndexedType());
506
507 // Verify that there are no other variable indices. If so, emit the hard way.
508 for (++i, ++GTI; i != e; ++i, ++GTI) {
509 ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i));
510 if (!CI) return 0;
511
512 // Compute the aggregate offset of constant indices.
513 if (CI->isZero()) continue;
514
515 // Handle a struct index, which adds its field offset to the pointer.
516 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
517 Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
518 } else {
519 uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType());
520 Offset += Size*CI->getSExtValue();
521 }
522 }
523
524 // Okay, we know we have a single variable index, which must be a
525 // pointer/array/vector index. If there is no offset, life is simple, return
526 // the index.
527 unsigned IntPtrWidth = TD.getPointerSizeInBits();
528 if (Offset == 0) {
529 // Cast to intptrty in case a truncation occurs. If an extension is needed,
530 // we don't need to bother extending: the extension won't affect where the
531 // computation crosses zero.
532 if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth)
533 VariableIdx = new TruncInst(VariableIdx,
534 TD.getIntPtrType(VariableIdx->getContext()),
535 VariableIdx->getName(), &I);
536 return VariableIdx;
537 }
538
539 // Otherwise, there is an index. The computation we will do will be modulo
540 // the pointer size, so get it.
541 uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth);
542
543 Offset &= PtrSizeMask;
544 VariableScale &= PtrSizeMask;
545
546 // To do this transformation, any constant index must be a multiple of the
547 // variable scale factor. For example, we can evaluate "12 + 4*i" as "3 + i",
548 // but we can't evaluate "10 + 3*i" in terms of i. Check that the offset is a
549 // multiple of the variable scale.
550 int64_t NewOffs = Offset / (int64_t)VariableScale;
551 if (Offset != NewOffs*(int64_t)VariableScale)
552 return 0;
553
554 // Okay, we can do this evaluation. Start by converting the index to intptr.
555 const Type *IntPtrTy = TD.getIntPtrType(VariableIdx->getContext());
556 if (VariableIdx->getType() != IntPtrTy)
557 VariableIdx = CastInst::CreateIntegerCast(VariableIdx, IntPtrTy,
558 true /*SExt*/,
559 VariableIdx->getName(), &I);
560 Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs);
561 return BinaryOperator::CreateAdd(VariableIdx, OffsetVal, "offset", &I);
562}
563
564/// FoldGEPICmp - Fold comparisons between a GEP instruction and something
565/// else. At this point we know that the GEP is on the LHS of the comparison.
566Instruction *InstCombiner::FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
567 ICmpInst::Predicate Cond,
568 Instruction &I) {
569 // Look through bitcasts.
570 if (BitCastInst *BCI = dyn_cast<BitCastInst>(RHS))
571 RHS = BCI->getOperand(0);
572
573 Value *PtrBase = GEPLHS->getOperand(0);
574 if (TD && PtrBase == RHS && GEPLHS->isInBounds()) {
575 // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
576 // This transformation (ignoring the base and scales) is valid because we
577 // know pointers can't overflow since the gep is inbounds. See if we can
578 // output an optimized form.
579 Value *Offset = EvaluateGEPOffsetExpression(GEPLHS, I, *this);
580
581 // If not, synthesize the offset the hard way.
582 if (Offset == 0)
583 Offset = EmitGEPOffset(GEPLHS);
584 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset,
585 Constant::getNullValue(Offset->getType()));
586 } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {
587 // If the base pointers are different, but the indices are the same, just
588 // compare the base pointer.
589 if (PtrBase != GEPRHS->getOperand(0)) {
590 bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands();
591 IndicesTheSame &= GEPLHS->getOperand(0)->getType() ==
592 GEPRHS->getOperand(0)->getType();
593 if (IndicesTheSame)
594 for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
595 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
596 IndicesTheSame = false;
597 break;
598 }
599
600 // If all indices are the same, just compare the base pointers.
601 if (IndicesTheSame)
602 return new ICmpInst(ICmpInst::getSignedPredicate(Cond),
603 GEPLHS->getOperand(0), GEPRHS->getOperand(0));
604
605 // Otherwise, the base pointers are different and the indices are
606 // different, bail out.
607 return 0;
608 }
609
610 // If one of the GEPs has all zero indices, recurse.
611 bool AllZeros = true;
612 for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
613 if (!isa<Constant>(GEPLHS->getOperand(i)) ||
614 !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) {
615 AllZeros = false;
616 break;
617 }
618 if (AllZeros)
619 return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0),
620 ICmpInst::getSwappedPredicate(Cond), I);
621
622 // If the other GEP has all zero indices, recurse.
623 AllZeros = true;
624 for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
625 if (!isa<Constant>(GEPRHS->getOperand(i)) ||
626 !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) {
627 AllZeros = false;
628 break;
629 }
630 if (AllZeros)
631 return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I);
632
633 if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) {
634 // If the GEPs only differ by one index, compare it.
635 unsigned NumDifferences = 0; // Keep track of # differences.
636 unsigned DiffOperand = 0; // The operand that differs.
637 for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
638 if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
639 if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() !=
640 GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) {
641 // Irreconcilable differences.
642 NumDifferences = 2;
643 break;
644 } else {
645 if (NumDifferences++) break;
646 DiffOperand = i;
647 }
648 }
649
650 if (NumDifferences == 0) // SAME GEP?
651 return ReplaceInstUsesWith(I, // No comparison is needed here.
652 ConstantInt::get(Type::getInt1Ty(I.getContext()),
653 ICmpInst::isTrueWhenEqual(Cond)));
654
655 else if (NumDifferences == 1) {
656 Value *LHSV = GEPLHS->getOperand(DiffOperand);
657 Value *RHSV = GEPRHS->getOperand(DiffOperand);
658 // Make sure we do a signed comparison here.
659 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV);
660 }
661 }
662
663 // Only lower this if the icmp is the only user of the GEP or if we expect
664 // the result to fold to a constant!
665 if (TD &&
666 (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) &&
667 (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) {
668 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
669 Value *L = EmitGEPOffset(GEPLHS);
670 Value *R = EmitGEPOffset(GEPRHS);
671 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R);
672 }
673 }
674 return 0;
675}
676
677/// FoldICmpAddOpCst - Fold "icmp pred (X+CI), X".
678Instruction *InstCombiner::FoldICmpAddOpCst(ICmpInst &ICI,
679 Value *X, ConstantInt *CI,
680 ICmpInst::Predicate Pred,
681 Value *TheAdd) {
682 // If we have X+0, exit early (simplifying logic below) and let it get folded
683 // elsewhere. icmp X+0, X -> icmp X, X
684 if (CI->isZero()) {
685 bool isTrue = ICmpInst::isTrueWhenEqual(Pred);
686 return ReplaceInstUsesWith(ICI, ConstantInt::get(ICI.getType(), isTrue));
687 }
688
689 // (X+4) == X -> false.
690 if (Pred == ICmpInst::ICMP_EQ)
691 return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(X->getContext()));
692
693 // (X+4) != X -> true.
694 if (Pred == ICmpInst::ICMP_NE)
695 return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(X->getContext()));
696
697 // If this is an instruction (as opposed to constantexpr) get NUW/NSW info.
698 bool isNUW = false, isNSW = false;
699 if (BinaryOperator *Add = dyn_cast<BinaryOperator>(TheAdd)) {
700 isNUW = Add->hasNoUnsignedWrap();
701 isNSW = Add->hasNoSignedWrap();
702 }
703
704 // From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0,
705 // so the values can never be equal. Similiarly for all other "or equals"
706 // operators.
707
Chris Lattner9aa1e242010-01-08 17:48:19 +0000708 // (X+1) <u X --> X >u (MAXUINT-1) --> X == 255
Chris Lattner02446fc2010-01-04 07:37:31 +0000709 // (X+2) <u X --> X >u (MAXUINT-2) --> X > 253
710 // (X+MAXUINT) <u X --> X >u (MAXUINT-MAXUINT) --> X != 0
711 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
712 // If this is an NUW add, then this is always false.
713 if (isNUW)
714 return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(X->getContext()));
715
Chris Lattner9aa1e242010-01-08 17:48:19 +0000716 Value *R =
717 ConstantExpr::getSub(ConstantInt::getAllOnesValue(CI->getType()), CI);
Chris Lattner02446fc2010-01-04 07:37:31 +0000718 return new ICmpInst(ICmpInst::ICMP_UGT, X, R);
719 }
720
721 // (X+1) >u X --> X <u (0-1) --> X != 255
722 // (X+2) >u X --> X <u (0-2) --> X <u 254
723 // (X+MAXUINT) >u X --> X <u (0-MAXUINT) --> X <u 1 --> X == 0
724 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
725 // If this is an NUW add, then this is always true.
726 if (isNUW)
727 return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(X->getContext()));
728 return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantExpr::getNeg(CI));
729 }
730
731 unsigned BitWidth = CI->getType()->getPrimitiveSizeInBits();
732 ConstantInt *SMax = ConstantInt::get(X->getContext(),
733 APInt::getSignedMaxValue(BitWidth));
734
735 // (X+ 1) <s X --> X >s (MAXSINT-1) --> X == 127
736 // (X+ 2) <s X --> X >s (MAXSINT-2) --> X >s 125
737 // (X+MAXSINT) <s X --> X >s (MAXSINT-MAXSINT) --> X >s 0
738 // (X+MINSINT) <s X --> X >s (MAXSINT-MINSINT) --> X >s -1
739 // (X+ -2) <s X --> X >s (MAXSINT- -2) --> X >s 126
740 // (X+ -1) <s X --> X >s (MAXSINT- -1) --> X != 127
741 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
742 // If this is an NSW add, then we have two cases: if the constant is
743 // positive, then this is always false, if negative, this is always true.
744 if (isNSW) {
745 bool isTrue = CI->getValue().isNegative();
746 return ReplaceInstUsesWith(ICI, ConstantInt::get(ICI.getType(), isTrue));
747 }
748
749 return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantExpr::getSub(SMax, CI));
750 }
751
752 // (X+ 1) >s X --> X <s (MAXSINT-(1-1)) --> X != 127
753 // (X+ 2) >s X --> X <s (MAXSINT-(2-1)) --> X <s 126
754 // (X+MAXSINT) >s X --> X <s (MAXSINT-(MAXSINT-1)) --> X <s 1
755 // (X+MINSINT) >s X --> X <s (MAXSINT-(MINSINT-1)) --> X <s -2
756 // (X+ -2) >s X --> X <s (MAXSINT-(-2-1)) --> X <s -126
757 // (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128
758
759 // If this is an NSW add, then we have two cases: if the constant is
760 // positive, then this is always true, if negative, this is always false.
761 if (isNSW) {
762 bool isTrue = !CI->getValue().isNegative();
763 return ReplaceInstUsesWith(ICI, ConstantInt::get(ICI.getType(), isTrue));
764 }
765
766 assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE);
767 Constant *C = ConstantInt::get(X->getContext(), CI->getValue()-1);
768 return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantExpr::getSub(SMax, C));
769}
770
771/// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS
772/// and CmpRHS are both known to be integer constants.
773Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
774 ConstantInt *DivRHS) {
775 ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1));
776 const APInt &CmpRHSV = CmpRHS->getValue();
777
778 // FIXME: If the operand types don't match the type of the divide
779 // then don't attempt this transform. The code below doesn't have the
780 // logic to deal with a signed divide and an unsigned compare (and
781 // vice versa). This is because (x /s C1) <s C2 produces different
782 // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even
783 // (x /u C1) <u C2. Simply casting the operands and result won't
784 // work. :( The if statement below tests that condition and bails
785 // if it finds it.
786 bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv;
787 if (!ICI.isEquality() && DivIsSigned != ICI.isSigned())
788 return 0;
789 if (DivRHS->isZero())
790 return 0; // The ProdOV computation fails on divide by zero.
791 if (DivIsSigned && DivRHS->isAllOnesValue())
792 return 0; // The overflow computation also screws up here
793 if (DivRHS->isOne())
794 return 0; // Not worth bothering, and eliminates some funny cases
795 // with INT_MIN.
796
797 // Compute Prod = CI * DivRHS. We are essentially solving an equation
798 // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and
799 // C2 (CI). By solving for X we can turn this into a range check
800 // instead of computing a divide.
801 Constant *Prod = ConstantExpr::getMul(CmpRHS, DivRHS);
802
803 // Determine if the product overflows by seeing if the product is
804 // not equal to the divide. Make sure we do the same kind of divide
805 // as in the LHS instruction that we're folding.
806 bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) :
807 ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS;
808
809 // Get the ICmp opcode
810 ICmpInst::Predicate Pred = ICI.getPredicate();
811
812 // Figure out the interval that is being checked. For example, a comparison
813 // like "X /u 5 == 0" is really checking that X is in the interval [0, 5).
814 // Compute this interval based on the constants involved and the signedness of
815 // the compare/divide. This computes a half-open interval, keeping track of
816 // whether either value in the interval overflows. After analysis each
817 // overflow variable is set to 0 if it's corresponding bound variable is valid
818 // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
819 int LoOverflow = 0, HiOverflow = 0;
820 Constant *LoBound = 0, *HiBound = 0;
821
822 if (!DivIsSigned) { // udiv
823 // e.g. X/5 op 3 --> [15, 20)
824 LoBound = Prod;
825 HiOverflow = LoOverflow = ProdOV;
826 if (!HiOverflow)
827 HiOverflow = AddWithOverflow(HiBound, LoBound, DivRHS, false);
828 } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0.
829 if (CmpRHSV == 0) { // (X / pos) op 0
830 // Can't overflow. e.g. X/2 op 0 --> [-1, 2)
831 LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS)));
832 HiBound = DivRHS;
833 } else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos
834 LoBound = Prod; // e.g. X/5 op 3 --> [15, 20)
835 HiOverflow = LoOverflow = ProdOV;
836 if (!HiOverflow)
837 HiOverflow = AddWithOverflow(HiBound, Prod, DivRHS, true);
838 } else { // (X / pos) op neg
839 // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14)
840 HiBound = AddOne(Prod);
841 LoOverflow = HiOverflow = ProdOV ? -1 : 0;
842 if (!LoOverflow) {
843 ConstantInt* DivNeg =
844 cast<ConstantInt>(ConstantExpr::getNeg(DivRHS));
845 LoOverflow = AddWithOverflow(LoBound, HiBound, DivNeg, true) ? -1 : 0;
846 }
847 }
848 } else if (DivRHS->getValue().isNegative()) { // Divisor is < 0.
849 if (CmpRHSV == 0) { // (X / neg) op 0
850 // e.g. X/-5 op 0 --> [-4, 5)
851 LoBound = AddOne(DivRHS);
852 HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS));
853 if (HiBound == DivRHS) { // -INTMIN = INTMIN
854 HiOverflow = 1; // [INTMIN+1, overflow)
855 HiBound = 0; // e.g. X/INTMIN = 0 --> X > INTMIN
856 }
857 } else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos
858 // e.g. X/-5 op 3 --> [-19, -14)
859 HiBound = AddOne(Prod);
860 HiOverflow = LoOverflow = ProdOV ? -1 : 0;
861 if (!LoOverflow)
862 LoOverflow = AddWithOverflow(LoBound, HiBound, DivRHS, true) ? -1 : 0;
863 } else { // (X / neg) op neg
864 LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20)
865 LoOverflow = HiOverflow = ProdOV;
866 if (!HiOverflow)
867 HiOverflow = SubWithOverflow(HiBound, Prod, DivRHS, true);
868 }
869
870 // Dividing by a negative swaps the condition. LT <-> GT
871 Pred = ICmpInst::getSwappedPredicate(Pred);
872 }
873
874 Value *X = DivI->getOperand(0);
875 switch (Pred) {
876 default: llvm_unreachable("Unhandled icmp opcode!");
877 case ICmpInst::ICMP_EQ:
878 if (LoOverflow && HiOverflow)
879 return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(ICI.getContext()));
Chris Lattnerf34f48c2010-03-05 08:46:26 +0000880 if (HiOverflow)
Chris Lattner02446fc2010-01-04 07:37:31 +0000881 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
882 ICmpInst::ICMP_UGE, X, LoBound);
Chris Lattnerf34f48c2010-03-05 08:46:26 +0000883 if (LoOverflow)
Chris Lattner02446fc2010-01-04 07:37:31 +0000884 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
885 ICmpInst::ICMP_ULT, X, HiBound);
Chris Lattnerf34f48c2010-03-05 08:46:26 +0000886 return ReplaceInstUsesWith(ICI,
887 InsertRangeTest(X, LoBound, HiBound, DivIsSigned,
888 true));
Chris Lattner02446fc2010-01-04 07:37:31 +0000889 case ICmpInst::ICMP_NE:
890 if (LoOverflow && HiOverflow)
891 return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(ICI.getContext()));
Chris Lattnerf34f48c2010-03-05 08:46:26 +0000892 if (HiOverflow)
Chris Lattner02446fc2010-01-04 07:37:31 +0000893 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
894 ICmpInst::ICMP_ULT, X, LoBound);
Chris Lattnerf34f48c2010-03-05 08:46:26 +0000895 if (LoOverflow)
Chris Lattner02446fc2010-01-04 07:37:31 +0000896 return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
897 ICmpInst::ICMP_UGE, X, HiBound);
Chris Lattnerf34f48c2010-03-05 08:46:26 +0000898 return ReplaceInstUsesWith(ICI, InsertRangeTest(X, LoBound, HiBound,
899 DivIsSigned, false));
Chris Lattner02446fc2010-01-04 07:37:31 +0000900 case ICmpInst::ICMP_ULT:
901 case ICmpInst::ICMP_SLT:
902 if (LoOverflow == +1) // Low bound is greater than input range.
903 return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(ICI.getContext()));
904 if (LoOverflow == -1) // Low bound is less than input range.
905 return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(ICI.getContext()));
906 return new ICmpInst(Pred, X, LoBound);
907 case ICmpInst::ICMP_UGT:
908 case ICmpInst::ICMP_SGT:
909 if (HiOverflow == +1) // High bound greater than input range.
910 return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(ICI.getContext()));
911 else if (HiOverflow == -1) // High bound less than input range.
912 return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(ICI.getContext()));
913 if (Pred == ICmpInst::ICMP_UGT)
914 return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound);
915 else
916 return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound);
917 }
918}
919
920
921/// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)".
922///
923Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
924 Instruction *LHSI,
925 ConstantInt *RHS) {
926 const APInt &RHSV = RHS->getValue();
927
928 switch (LHSI->getOpcode()) {
929 case Instruction::Trunc:
930 if (ICI.isEquality() && LHSI->hasOneUse()) {
931 // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
932 // of the high bits truncated out of x are known.
933 unsigned DstBits = LHSI->getType()->getPrimitiveSizeInBits(),
934 SrcBits = LHSI->getOperand(0)->getType()->getPrimitiveSizeInBits();
935 APInt Mask(APInt::getHighBitsSet(SrcBits, SrcBits-DstBits));
936 APInt KnownZero(SrcBits, 0), KnownOne(SrcBits, 0);
937 ComputeMaskedBits(LHSI->getOperand(0), Mask, KnownZero, KnownOne);
938
939 // If all the high bits are known, we can do this xform.
940 if ((KnownZero|KnownOne).countLeadingOnes() >= SrcBits-DstBits) {
941 // Pull in the high bits from known-ones set.
Jay Foad40f8f622010-12-07 08:25:19 +0000942 APInt NewRHS = RHS->getValue().zext(SrcBits);
Chris Lattner02446fc2010-01-04 07:37:31 +0000943 NewRHS |= KnownOne;
944 return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
945 ConstantInt::get(ICI.getContext(), NewRHS));
946 }
947 }
948 break;
949
950 case Instruction::Xor: // (icmp pred (xor X, XorCST), CI)
951 if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) {
952 // If this is a comparison that tests the signbit (X < 0) or (x > -1),
953 // fold the xor.
954 if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) ||
955 (ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) {
956 Value *CompareVal = LHSI->getOperand(0);
957
958 // If the sign bit of the XorCST is not set, there is no change to
959 // the operation, just stop using the Xor.
960 if (!XorCST->getValue().isNegative()) {
961 ICI.setOperand(0, CompareVal);
962 Worklist.Add(LHSI);
963 return &ICI;
964 }
965
966 // Was the old condition true if the operand is positive?
967 bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT;
968
969 // If so, the new one isn't.
970 isTrueIfPositive ^= true;
971
972 if (isTrueIfPositive)
973 return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal,
974 SubOne(RHS));
975 else
976 return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal,
977 AddOne(RHS));
978 }
979
980 if (LHSI->hasOneUse()) {
981 // (icmp u/s (xor A SignBit), C) -> (icmp s/u A, (xor C SignBit))
982 if (!ICI.isEquality() && XorCST->getValue().isSignBit()) {
983 const APInt &SignBit = XorCST->getValue();
984 ICmpInst::Predicate Pred = ICI.isSigned()
985 ? ICI.getUnsignedPredicate()
986 : ICI.getSignedPredicate();
987 return new ICmpInst(Pred, LHSI->getOperand(0),
988 ConstantInt::get(ICI.getContext(),
989 RHSV ^ SignBit));
990 }
991
992 // (icmp u/s (xor A ~SignBit), C) -> (icmp s/u (xor C ~SignBit), A)
993 if (!ICI.isEquality() && XorCST->getValue().isMaxSignedValue()) {
994 const APInt &NotSignBit = XorCST->getValue();
995 ICmpInst::Predicate Pred = ICI.isSigned()
996 ? ICI.getUnsignedPredicate()
997 : ICI.getSignedPredicate();
998 Pred = ICI.getSwappedPredicate(Pred);
999 return new ICmpInst(Pred, LHSI->getOperand(0),
1000 ConstantInt::get(ICI.getContext(),
1001 RHSV ^ NotSignBit));
1002 }
1003 }
1004 }
1005 break;
1006 case Instruction::And: // (icmp pred (and X, AndCST), RHS)
1007 if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) &&
1008 LHSI->getOperand(0)->hasOneUse()) {
1009 ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1));
1010
1011 // If the LHS is an AND of a truncating cast, we can widen the
1012 // and/compare to be the input width without changing the value
1013 // produced, eliminating a cast.
1014 if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) {
1015 // We can do this transformation if either the AND constant does not
1016 // have its sign bit set or if it is an equality comparison.
1017 // Extending a relational comparison when we're checking the sign
1018 // bit would not work.
1019 if (Cast->hasOneUse() &&
1020 (ICI.isEquality() ||
1021 (AndCST->getValue().isNonNegative() && RHSV.isNonNegative()))) {
1022 uint32_t BitWidth =
1023 cast<IntegerType>(Cast->getOperand(0)->getType())->getBitWidth();
Jay Foad40f8f622010-12-07 08:25:19 +00001024 APInt NewCST = AndCST->getValue().zext(BitWidth);
1025 APInt NewCI = RHSV.zext(BitWidth);
Chris Lattner02446fc2010-01-04 07:37:31 +00001026 Value *NewAnd =
1027 Builder->CreateAnd(Cast->getOperand(0),
1028 ConstantInt::get(ICI.getContext(), NewCST),
1029 LHSI->getName());
1030 return new ICmpInst(ICI.getPredicate(), NewAnd,
1031 ConstantInt::get(ICI.getContext(), NewCI));
1032 }
1033 }
1034
1035 // If this is: (X >> C1) & C2 != C3 (where any shift and any compare
1036 // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This
1037 // happens a LOT in code produced by the C front-end, for bitfield
1038 // access.
1039 BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0));
1040 if (Shift && !Shift->isShift())
1041 Shift = 0;
1042
1043 ConstantInt *ShAmt;
1044 ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0;
1045 const Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift.
1046 const Type *AndTy = AndCST->getType(); // Type of the and.
1047
1048 // We can fold this as long as we can't shift unknown bits
1049 // into the mask. This can only happen with signed shift
1050 // rights, as they sign-extend.
1051 if (ShAmt) {
1052 bool CanFold = Shift->isLogicalShift();
1053 if (!CanFold) {
1054 // To test for the bad case of the signed shr, see if any
1055 // of the bits shifted in could be tested after the mask.
1056 uint32_t TyBits = Ty->getPrimitiveSizeInBits();
1057 int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits);
1058
1059 uint32_t BitWidth = AndTy->getPrimitiveSizeInBits();
1060 if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) &
1061 AndCST->getValue()) == 0)
1062 CanFold = true;
1063 }
1064
1065 if (CanFold) {
1066 Constant *NewCst;
1067 if (Shift->getOpcode() == Instruction::Shl)
1068 NewCst = ConstantExpr::getLShr(RHS, ShAmt);
1069 else
1070 NewCst = ConstantExpr::getShl(RHS, ShAmt);
1071
1072 // Check to see if we are shifting out any of the bits being
1073 // compared.
1074 if (ConstantExpr::get(Shift->getOpcode(),
1075 NewCst, ShAmt) != RHS) {
1076 // If we shifted bits out, the fold is not going to work out.
1077 // As a special case, check to see if this means that the
1078 // result is always true or false now.
1079 if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
1080 return ReplaceInstUsesWith(ICI,
1081 ConstantInt::getFalse(ICI.getContext()));
1082 if (ICI.getPredicate() == ICmpInst::ICMP_NE)
1083 return ReplaceInstUsesWith(ICI,
1084 ConstantInt::getTrue(ICI.getContext()));
1085 } else {
1086 ICI.setOperand(1, NewCst);
1087 Constant *NewAndCST;
1088 if (Shift->getOpcode() == Instruction::Shl)
1089 NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt);
1090 else
1091 NewAndCST = ConstantExpr::getShl(AndCST, ShAmt);
1092 LHSI->setOperand(1, NewAndCST);
1093 LHSI->setOperand(0, Shift->getOperand(0));
1094 Worklist.Add(Shift); // Shift is dead.
1095 return &ICI;
1096 }
1097 }
1098 }
1099
1100 // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is
1101 // preferable because it allows the C<<Y expression to be hoisted out
1102 // of a loop if Y is invariant and X is not.
1103 if (Shift && Shift->hasOneUse() && RHSV == 0 &&
1104 ICI.isEquality() && !Shift->isArithmeticShift() &&
1105 !isa<Constant>(Shift->getOperand(0))) {
1106 // Compute C << Y.
1107 Value *NS;
1108 if (Shift->getOpcode() == Instruction::LShr) {
1109 NS = Builder->CreateShl(AndCST, Shift->getOperand(1), "tmp");
1110 } else {
1111 // Insert a logical shift.
1112 NS = Builder->CreateLShr(AndCST, Shift->getOperand(1), "tmp");
1113 }
1114
1115 // Compute X & (C << Y).
1116 Value *NewAnd =
1117 Builder->CreateAnd(Shift->getOperand(0), NS, LHSI->getName());
1118
1119 ICI.setOperand(0, NewAnd);
1120 return &ICI;
1121 }
1122 }
1123
1124 // Try to optimize things like "A[i]&42 == 0" to index computations.
1125 if (LoadInst *LI = dyn_cast<LoadInst>(LHSI->getOperand(0))) {
1126 if (GetElementPtrInst *GEP =
1127 dyn_cast<GetElementPtrInst>(LI->getOperand(0)))
1128 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
1129 if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
1130 !LI->isVolatile() && isa<ConstantInt>(LHSI->getOperand(1))) {
1131 ConstantInt *C = cast<ConstantInt>(LHSI->getOperand(1));
1132 if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV,ICI, C))
1133 return Res;
1134 }
1135 }
1136 break;
1137
1138 case Instruction::Or: {
1139 if (!ICI.isEquality() || !RHS->isNullValue() || !LHSI->hasOneUse())
1140 break;
1141 Value *P, *Q;
1142 if (match(LHSI, m_Or(m_PtrToInt(m_Value(P)), m_PtrToInt(m_Value(Q))))) {
1143 // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0
1144 // -> and (icmp eq P, null), (icmp eq Q, null).
1145
1146 Value *ICIP = Builder->CreateICmp(ICI.getPredicate(), P,
1147 Constant::getNullValue(P->getType()));
1148 Value *ICIQ = Builder->CreateICmp(ICI.getPredicate(), Q,
1149 Constant::getNullValue(Q->getType()));
1150 Instruction *Op;
1151 if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
1152 Op = BinaryOperator::CreateAnd(ICIP, ICIQ);
1153 else
1154 Op = BinaryOperator::CreateOr(ICIP, ICIQ);
1155 return Op;
1156 }
1157 break;
1158 }
1159
1160 case Instruction::Shl: { // (icmp pred (shl X, ShAmt), CI)
1161 ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1));
1162 if (!ShAmt) break;
1163
1164 uint32_t TypeBits = RHSV.getBitWidth();
1165
1166 // Check that the shift amount is in range. If not, don't perform
1167 // undefined shifts. When the shift is visited it will be
1168 // simplified.
1169 if (ShAmt->uge(TypeBits))
1170 break;
1171
1172 if (ICI.isEquality()) {
1173 // If we are comparing against bits always shifted out, the
1174 // comparison cannot succeed.
1175 Constant *Comp =
1176 ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt),
1177 ShAmt);
1178 if (Comp != RHS) {// Comparing against a bit that we know is zero.
1179 bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
1180 Constant *Cst =
1181 ConstantInt::get(Type::getInt1Ty(ICI.getContext()), IsICMP_NE);
1182 return ReplaceInstUsesWith(ICI, Cst);
1183 }
1184
1185 if (LHSI->hasOneUse()) {
1186 // Otherwise strength reduce the shift into an and.
1187 uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
1188 Constant *Mask =
1189 ConstantInt::get(ICI.getContext(), APInt::getLowBitsSet(TypeBits,
1190 TypeBits-ShAmtVal));
1191
1192 Value *And =
1193 Builder->CreateAnd(LHSI->getOperand(0),Mask, LHSI->getName()+".mask");
1194 return new ICmpInst(ICI.getPredicate(), And,
1195 ConstantInt::get(ICI.getContext(),
1196 RHSV.lshr(ShAmtVal)));
1197 }
1198 }
1199
1200 // Otherwise, if this is a comparison of the sign bit, simplify to and/test.
1201 bool TrueIfSigned = false;
1202 if (LHSI->hasOneUse() &&
1203 isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) {
1204 // (X << 31) <s 0 --> (X&1) != 0
1205 Constant *Mask = ConstantInt::get(ICI.getContext(), APInt(TypeBits, 1) <<
1206 (TypeBits-ShAmt->getZExtValue()-1));
1207 Value *And =
1208 Builder->CreateAnd(LHSI->getOperand(0), Mask, LHSI->getName()+".mask");
1209 return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
1210 And, Constant::getNullValue(And->getType()));
1211 }
1212 break;
1213 }
1214
1215 case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI)
1216 case Instruction::AShr: {
1217 // Only handle equality comparisons of shift-by-constant.
1218 ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1));
1219 if (!ShAmt || !ICI.isEquality()) break;
1220
1221 // Check that the shift amount is in range. If not, don't perform
1222 // undefined shifts. When the shift is visited it will be
1223 // simplified.
1224 uint32_t TypeBits = RHSV.getBitWidth();
1225 if (ShAmt->uge(TypeBits))
1226 break;
1227
1228 uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
1229
1230 // If we are comparing against bits always shifted out, the
1231 // comparison cannot succeed.
1232 APInt Comp = RHSV << ShAmtVal;
1233 if (LHSI->getOpcode() == Instruction::LShr)
1234 Comp = Comp.lshr(ShAmtVal);
1235 else
1236 Comp = Comp.ashr(ShAmtVal);
1237
1238 if (Comp != RHSV) { // Comparing against a bit that we know is zero.
1239 bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
1240 Constant *Cst = ConstantInt::get(Type::getInt1Ty(ICI.getContext()),
1241 IsICMP_NE);
1242 return ReplaceInstUsesWith(ICI, Cst);
1243 }
1244
1245 // Otherwise, check to see if the bits shifted out are known to be zero.
1246 // If so, we can compare against the unshifted value:
1247 // (X & 4) >> 1 == 2 --> (X & 4) == 4.
1248 if (LHSI->hasOneUse() &&
1249 MaskedValueIsZero(LHSI->getOperand(0),
1250 APInt::getLowBitsSet(Comp.getBitWidth(), ShAmtVal))) {
1251 return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
1252 ConstantExpr::getShl(RHS, ShAmt));
1253 }
1254
1255 if (LHSI->hasOneUse()) {
1256 // Otherwise strength reduce the shift into an and.
1257 APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
1258 Constant *Mask = ConstantInt::get(ICI.getContext(), Val);
1259
1260 Value *And = Builder->CreateAnd(LHSI->getOperand(0),
1261 Mask, LHSI->getName()+".mask");
1262 return new ICmpInst(ICI.getPredicate(), And,
1263 ConstantExpr::getShl(RHS, ShAmt));
1264 }
1265 break;
1266 }
1267
1268 case Instruction::SDiv:
1269 case Instruction::UDiv:
1270 // Fold: icmp pred ([us]div X, C1), C2 -> range test
1271 // Fold this div into the comparison, producing a range check.
1272 // Determine, based on the divide type, what the range is being
1273 // checked. If there is an overflow on the low or high side, remember
1274 // it, otherwise compute the range [low, hi) bounding the new value.
1275 // See: InsertRangeTest above for the kinds of replacements possible.
1276 if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1)))
1277 if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI),
1278 DivRHS))
1279 return R;
1280 break;
1281
1282 case Instruction::Add:
1283 // Fold: icmp pred (add X, C1), C2
1284 if (!ICI.isEquality()) {
1285 ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1));
1286 if (!LHSC) break;
1287 const APInt &LHSV = LHSC->getValue();
1288
1289 ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV)
1290 .subtract(LHSV);
1291
1292 if (ICI.isSigned()) {
1293 if (CR.getLower().isSignBit()) {
1294 return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0),
1295 ConstantInt::get(ICI.getContext(),CR.getUpper()));
1296 } else if (CR.getUpper().isSignBit()) {
1297 return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0),
1298 ConstantInt::get(ICI.getContext(),CR.getLower()));
1299 }
1300 } else {
1301 if (CR.getLower().isMinValue()) {
1302 return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0),
1303 ConstantInt::get(ICI.getContext(),CR.getUpper()));
1304 } else if (CR.getUpper().isMinValue()) {
1305 return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0),
1306 ConstantInt::get(ICI.getContext(),CR.getLower()));
1307 }
1308 }
1309 }
1310 break;
1311 }
1312
1313 // Simplify icmp_eq and icmp_ne instructions with integer constant RHS.
1314 if (ICI.isEquality()) {
1315 bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
1316
1317 // If the first operand is (add|sub|and|or|xor|rem) with a constant, and
1318 // the second operand is a constant, simplify a bit.
1319 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) {
1320 switch (BO->getOpcode()) {
1321 case Instruction::SRem:
1322 // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
1323 if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){
1324 const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue();
Dan Gohmane0567812010-04-08 23:03:40 +00001325 if (V.sgt(1) && V.isPowerOf2()) {
Chris Lattner02446fc2010-01-04 07:37:31 +00001326 Value *NewRem =
1327 Builder->CreateURem(BO->getOperand(0), BO->getOperand(1),
1328 BO->getName());
1329 return new ICmpInst(ICI.getPredicate(), NewRem,
1330 Constant::getNullValue(BO->getType()));
1331 }
1332 }
1333 break;
1334 case Instruction::Add:
1335 // Replace ((add A, B) != C) with (A != C-B) if B & C are constants.
1336 if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) {
1337 if (BO->hasOneUse())
1338 return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
1339 ConstantExpr::getSub(RHS, BOp1C));
1340 } else if (RHSV == 0) {
1341 // Replace ((add A, B) != 0) with (A != -B) if A or B is
1342 // efficiently invertible, or if the add has just this one use.
1343 Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
1344
1345 if (Value *NegVal = dyn_castNegVal(BOp1))
1346 return new ICmpInst(ICI.getPredicate(), BOp0, NegVal);
1347 else if (Value *NegVal = dyn_castNegVal(BOp0))
1348 return new ICmpInst(ICI.getPredicate(), NegVal, BOp1);
1349 else if (BO->hasOneUse()) {
1350 Value *Neg = Builder->CreateNeg(BOp1);
1351 Neg->takeName(BO);
1352 return new ICmpInst(ICI.getPredicate(), BOp0, Neg);
1353 }
1354 }
1355 break;
1356 case Instruction::Xor:
1357 // For the xor case, we can xor two constants together, eliminating
1358 // the explicit xor.
1359 if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1)))
1360 return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
1361 ConstantExpr::getXor(RHS, BOC));
1362
1363 // FALLTHROUGH
1364 case Instruction::Sub:
1365 // Replace (([sub|xor] A, B) != 0) with (A != B)
1366 if (RHSV == 0)
1367 return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
1368 BO->getOperand(1));
1369 break;
1370
1371 case Instruction::Or:
1372 // If bits are being or'd in that are not present in the constant we
1373 // are comparing against, then the comparison could never succeed!
Eli Friedman618898e2010-07-29 18:03:33 +00001374 if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
Chris Lattner02446fc2010-01-04 07:37:31 +00001375 Constant *NotCI = ConstantExpr::getNot(RHS);
1376 if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue())
1377 return ReplaceInstUsesWith(ICI,
1378 ConstantInt::get(Type::getInt1Ty(ICI.getContext()),
1379 isICMP_NE));
1380 }
1381 break;
1382
1383 case Instruction::And:
1384 if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
1385 // If bits are being compared against that are and'd out, then the
1386 // comparison can never succeed!
1387 if ((RHSV & ~BOC->getValue()) != 0)
1388 return ReplaceInstUsesWith(ICI,
1389 ConstantInt::get(Type::getInt1Ty(ICI.getContext()),
1390 isICMP_NE));
1391
1392 // If we have ((X & C) == C), turn it into ((X & C) != 0).
1393 if (RHS == BOC && RHSV.isPowerOf2())
1394 return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ :
1395 ICmpInst::ICMP_NE, LHSI,
1396 Constant::getNullValue(RHS->getType()));
1397
1398 // Replace (and X, (1 << size(X)-1) != 0) with x s< 0
1399 if (BOC->getValue().isSignBit()) {
1400 Value *X = BO->getOperand(0);
1401 Constant *Zero = Constant::getNullValue(X->getType());
1402 ICmpInst::Predicate pred = isICMP_NE ?
1403 ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
1404 return new ICmpInst(pred, X, Zero);
1405 }
1406
1407 // ((X & ~7) == 0) --> X < 8
1408 if (RHSV == 0 && isHighOnes(BOC)) {
1409 Value *X = BO->getOperand(0);
1410 Constant *NegX = ConstantExpr::getNeg(BOC);
1411 ICmpInst::Predicate pred = isICMP_NE ?
1412 ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
1413 return new ICmpInst(pred, X, NegX);
1414 }
1415 }
1416 default: break;
1417 }
1418 } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) {
1419 // Handle icmp {eq|ne} <intrinsic>, intcst.
Chris Lattner03357402010-01-05 18:09:56 +00001420 switch (II->getIntrinsicID()) {
1421 case Intrinsic::bswap:
Chris Lattner02446fc2010-01-04 07:37:31 +00001422 Worklist.Add(II);
Gabor Greifcaf70b32010-06-24 16:11:44 +00001423 ICI.setOperand(0, II->getArgOperand(0));
Chris Lattner02446fc2010-01-04 07:37:31 +00001424 ICI.setOperand(1, ConstantInt::get(II->getContext(), RHSV.byteSwap()));
1425 return &ICI;
Chris Lattner03357402010-01-05 18:09:56 +00001426 case Intrinsic::ctlz:
1427 case Intrinsic::cttz:
1428 // ctz(A) == bitwidth(a) -> A == 0 and likewise for !=
1429 if (RHSV == RHS->getType()->getBitWidth()) {
1430 Worklist.Add(II);
Gabor Greifcaf70b32010-06-24 16:11:44 +00001431 ICI.setOperand(0, II->getArgOperand(0));
Chris Lattner03357402010-01-05 18:09:56 +00001432 ICI.setOperand(1, ConstantInt::get(RHS->getType(), 0));
1433 return &ICI;
1434 }
1435 break;
1436 case Intrinsic::ctpop:
1437 // popcount(A) == 0 -> A == 0 and likewise for !=
1438 if (RHS->isZero()) {
1439 Worklist.Add(II);
Gabor Greifcaf70b32010-06-24 16:11:44 +00001440 ICI.setOperand(0, II->getArgOperand(0));
Chris Lattner03357402010-01-05 18:09:56 +00001441 ICI.setOperand(1, RHS);
1442 return &ICI;
1443 }
1444 break;
1445 default:
Duncan Sands34727662010-07-12 08:16:59 +00001446 break;
Chris Lattner02446fc2010-01-04 07:37:31 +00001447 }
1448 }
1449 }
1450 return 0;
1451}
1452
1453/// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst).
1454/// We only handle extending casts so far.
1455///
1456Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) {
1457 const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0));
1458 Value *LHSCIOp = LHSCI->getOperand(0);
1459 const Type *SrcTy = LHSCIOp->getType();
1460 const Type *DestTy = LHSCI->getType();
1461 Value *RHSCIOp;
1462
1463 // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
1464 // integer type is the same size as the pointer type.
1465 if (TD && LHSCI->getOpcode() == Instruction::PtrToInt &&
1466 TD->getPointerSizeInBits() ==
1467 cast<IntegerType>(DestTy)->getBitWidth()) {
1468 Value *RHSOp = 0;
1469 if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) {
1470 RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy);
1471 } else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) {
1472 RHSOp = RHSC->getOperand(0);
1473 // If the pointer types don't match, insert a bitcast.
1474 if (LHSCIOp->getType() != RHSOp->getType())
1475 RHSOp = Builder->CreateBitCast(RHSOp, LHSCIOp->getType());
1476 }
1477
1478 if (RHSOp)
1479 return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp);
1480 }
1481
1482 // The code below only handles extension cast instructions, so far.
1483 // Enforce this.
1484 if (LHSCI->getOpcode() != Instruction::ZExt &&
1485 LHSCI->getOpcode() != Instruction::SExt)
1486 return 0;
1487
1488 bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt;
1489 bool isSignedCmp = ICI.isSigned();
1490
1491 if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) {
1492 // Not an extension from the same type?
1493 RHSCIOp = CI->getOperand(0);
1494 if (RHSCIOp->getType() != LHSCIOp->getType())
1495 return 0;
1496
1497 // If the signedness of the two casts doesn't agree (i.e. one is a sext
1498 // and the other is a zext), then we can't handle this.
1499 if (CI->getOpcode() != LHSCI->getOpcode())
1500 return 0;
1501
1502 // Deal with equality cases early.
1503 if (ICI.isEquality())
1504 return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
1505
1506 // A signed comparison of sign extended values simplifies into a
1507 // signed comparison.
1508 if (isSignedCmp && isSignedExt)
1509 return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
1510
1511 // The other three cases all fold into an unsigned comparison.
1512 return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp);
1513 }
1514
1515 // If we aren't dealing with a constant on the RHS, exit early
1516 ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1));
1517 if (!CI)
1518 return 0;
1519
1520 // Compute the constant that would happen if we truncated to SrcTy then
1521 // reextended to DestTy.
1522 Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy);
1523 Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(),
1524 Res1, DestTy);
1525
1526 // If the re-extended constant didn't change...
1527 if (Res2 == CI) {
1528 // Deal with equality cases early.
1529 if (ICI.isEquality())
1530 return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1);
1531
1532 // A signed comparison of sign extended values simplifies into a
1533 // signed comparison.
1534 if (isSignedExt && isSignedCmp)
1535 return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1);
1536
1537 // The other three cases all fold into an unsigned comparison.
1538 return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, Res1);
1539 }
1540
1541 // The re-extended constant changed so the constant cannot be represented
1542 // in the shorter type. Consequently, we cannot emit a simple comparison.
1543
1544 // First, handle some easy cases. We know the result cannot be equal at this
1545 // point so handle the ICI.isEquality() cases
1546 if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
1547 return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(ICI.getContext()));
1548 if (ICI.getPredicate() == ICmpInst::ICMP_NE)
1549 return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(ICI.getContext()));
1550
1551 // Evaluate the comparison for LT (we invert for GT below). LE and GE cases
1552 // should have been folded away previously and not enter in here.
1553 Value *Result;
1554 if (isSignedCmp) {
1555 // We're performing a signed comparison.
1556 if (cast<ConstantInt>(CI)->getValue().isNegative())
1557 Result = ConstantInt::getFalse(ICI.getContext()); // X < (small) --> false
1558 else
1559 Result = ConstantInt::getTrue(ICI.getContext()); // X < (large) --> true
1560 } else {
1561 // We're performing an unsigned comparison.
1562 if (isSignedExt) {
1563 // We're performing an unsigned comp with a sign extended value.
1564 // This is true if the input is >= 0. [aka >s -1]
1565 Constant *NegOne = Constant::getAllOnesValue(SrcTy);
1566 Result = Builder->CreateICmpSGT(LHSCIOp, NegOne, ICI.getName());
1567 } else {
1568 // Unsigned extend & unsigned compare -> always true.
1569 Result = ConstantInt::getTrue(ICI.getContext());
1570 }
1571 }
1572
1573 // Finally, return the value computed.
1574 if (ICI.getPredicate() == ICmpInst::ICMP_ULT ||
1575 ICI.getPredicate() == ICmpInst::ICMP_SLT)
1576 return ReplaceInstUsesWith(ICI, Result);
1577
1578 assert((ICI.getPredicate()==ICmpInst::ICMP_UGT ||
1579 ICI.getPredicate()==ICmpInst::ICMP_SGT) &&
1580 "ICmp should be folded!");
1581 if (Constant *CI = dyn_cast<Constant>(Result))
1582 return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI));
1583 return BinaryOperator::CreateNot(Result);
1584}
1585
Chris Lattnerf0f568b2010-12-19 17:52:50 +00001586/// ProcessUGT_ADDCST_ADD - The caller has matched a pattern of the form:
1587/// I = icmp ugt (add (add A, B), CI2), CI1
Chris Lattnerdd7e8372010-12-19 18:22:06 +00001588/// If this is of the form:
1589/// sum = a + b
1590/// if (sum+128 >u 255)
1591/// Then replace it with llvm.sadd.with.overflow.i8.
1592///
Chris Lattnerf0f568b2010-12-19 17:52:50 +00001593static Instruction *ProcessUGT_ADDCST_ADD(ICmpInst &I, Value *A, Value *B,
1594 ConstantInt *CI2, ConstantInt *CI1,
1595 InstCombiner::BuilderTy *Builder) {
Chris Lattner368397b2010-12-19 17:59:02 +00001596 // The transformation we're trying to do here is to transform this into an
1597 // llvm.sadd.with.overflow. To do this, we have to replace the original add
1598 // with a narrower add, and discard the add-with-constant that is part of the
1599 // range check (if we can't eliminate it, this isn't profitable).
1600
1601 // In order to eliminate the add-with-constant, the compare can be its only
1602 // use.
Chris Lattnerdd7e8372010-12-19 18:22:06 +00001603 Instruction *AddWithCst = cast<Instruction>(I.getOperand(0));
Chris Lattner368397b2010-12-19 17:59:02 +00001604 if (!AddWithCst->hasOneUse()) return 0;
Chris Lattnerdd7e8372010-12-19 18:22:06 +00001605
1606 // If CI2 is 2^7, 2^15, 2^31, then it might be an sadd.with.overflow.
1607 if (!CI2->getValue().isPowerOf2()) return 0;
1608 unsigned NewWidth = CI2->getValue().countTrailingZeros();
1609 if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31) return 0;
1610
1611 // The width of the new add formed is 1 more than the bias.
1612 ++NewWidth;
1613
1614 // Check to see that CI1 is an all-ones value with NewWidth bits.
1615 if (CI1->getBitWidth() == NewWidth ||
1616 CI1->getValue() != APInt::getLowBitsSet(CI1->getBitWidth(), NewWidth))
1617 return 0;
1618
1619 // In order to replace the original add with a narrower
1620 // llvm.sadd.with.overflow, the only uses allowed are the add-with-constant
1621 // and truncates that discard the high bits of the add. Verify that this is
1622 // the case.
1623 Instruction *OrigAdd = cast<Instruction>(AddWithCst->getOperand(0));
1624 for (Value::use_iterator UI = OrigAdd->use_begin(), E = OrigAdd->use_end();
1625 UI != E; ++UI) {
1626 if (*UI == AddWithCst) continue;
1627
1628 // Only accept truncates for now. We would really like a nice recursive
1629 // predicate like SimplifyDemandedBits, but which goes downwards the use-def
1630 // chain to see which bits of a value are actually demanded. If the
1631 // original add had another add which was then immediately truncated, we
1632 // could still do the transformation.
1633 TruncInst *TI = dyn_cast<TruncInst>(*UI);
1634 if (TI == 0 ||
1635 TI->getType()->getPrimitiveSizeInBits() > NewWidth) return 0;
1636 }
1637
Chris Lattnerf0f568b2010-12-19 17:52:50 +00001638 const IntegerType *WideType = cast<IntegerType>(CI1->getType());
1639 unsigned WideWidth = WideType->getBitWidth();
1640 unsigned NarrowWidth = WideWidth / 2;
1641 const IntegerType *NarrowType =
1642 IntegerType::get(CI1->getContext(), NarrowWidth);
1643
1644 // NarrowAllOnes and NarrowSignBit are the magic constants used to
1645 // perform an overflow check in the wider type: 0x00..00FF..FF and
1646 // 0x00..0010..00 respectively, where the highest set bit in each is
1647 // what would be the sign bit in the narrower type.
1648 ConstantInt *NarrowAllOnes = cast<ConstantInt>(ConstantInt::get(WideType,
1649 APInt::getAllOnesValue(NarrowWidth).zext(WideWidth)));
1650 APInt SignBit(WideWidth, 0);
1651 SignBit.setBit(NarrowWidth-1);
1652 ConstantInt *NarrowSignBit =
1653 cast<ConstantInt>(ConstantInt::get(WideType, SignBit));
1654
1655 if (CI1 != NarrowAllOnes || CI2 != NarrowSignBit)
1656 return 0;
1657
1658 Module *M = I.getParent()->getParent()->getParent();
1659
1660 const Type *IntrinsicType = NarrowType;
1661 Value *F = Intrinsic::getDeclaration(M, Intrinsic::sadd_with_overflow,
1662 &IntrinsicType, 1);
1663
1664 BasicBlock *InitialBlock = Builder->GetInsertBlock();
1665 BasicBlock::iterator InitialInsert = Builder->GetInsertPoint();
1666
1667 // If the pattern matches, truncate the inputs to the narrower type and
1668 // use the sadd_with_overflow intrinsic to efficiently compute both the
1669 // result and the overflow bit.
Chris Lattnerf0f568b2010-12-19 17:52:50 +00001670 Builder->SetInsertPoint(OrigAdd->getParent(),
1671 BasicBlock::iterator(OrigAdd));
1672
1673 Value *TruncA = Builder->CreateTrunc(A, NarrowType, A->getName());
1674 Value *TruncB = Builder->CreateTrunc(B, NarrowType, B->getName());
1675 CallInst *Call = Builder->CreateCall2(F, TruncA, TruncB);
1676 Value *Add = Builder->CreateExtractValue(Call, 0);
1677 Value *ZExt = Builder->CreateZExt(Add, WideType);
1678
1679 // The inner add was the result of the narrow add, zero extended to the
1680 // wider type. Replace it with the result computed by the intrinsic.
1681 OrigAdd->replaceAllUsesWith(ZExt);
1682
1683 Builder->SetInsertPoint(InitialBlock, InitialInsert);
1684
1685 return ExtractValueInst::Create(Call, 1);
1686}
Chris Lattner02446fc2010-01-04 07:37:31 +00001687
1688
1689Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
1690 bool Changed = false;
Chris Lattner5f670d42010-02-01 19:54:45 +00001691 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattner02446fc2010-01-04 07:37:31 +00001692
1693 /// Orders the operands of the compare so that they are listed from most
1694 /// complex to least complex. This puts constants before unary operators,
1695 /// before binary operators.
Chris Lattner5f670d42010-02-01 19:54:45 +00001696 if (getComplexity(Op0) < getComplexity(Op1)) {
Chris Lattner02446fc2010-01-04 07:37:31 +00001697 I.swapOperands();
Chris Lattner5f670d42010-02-01 19:54:45 +00001698 std::swap(Op0, Op1);
Chris Lattner02446fc2010-01-04 07:37:31 +00001699 Changed = true;
1700 }
1701
Chris Lattner02446fc2010-01-04 07:37:31 +00001702 if (Value *V = SimplifyICmpInst(I.getPredicate(), Op0, Op1, TD))
1703 return ReplaceInstUsesWith(I, V);
1704
1705 const Type *Ty = Op0->getType();
1706
1707 // icmp's with boolean values can always be turned into bitwise operations
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001708 if (Ty->isIntegerTy(1)) {
Chris Lattner02446fc2010-01-04 07:37:31 +00001709 switch (I.getPredicate()) {
1710 default: llvm_unreachable("Invalid icmp instruction!");
1711 case ICmpInst::ICMP_EQ: { // icmp eq i1 A, B -> ~(A^B)
1712 Value *Xor = Builder->CreateXor(Op0, Op1, I.getName()+"tmp");
1713 return BinaryOperator::CreateNot(Xor);
1714 }
1715 case ICmpInst::ICMP_NE: // icmp eq i1 A, B -> A^B
1716 return BinaryOperator::CreateXor(Op0, Op1);
1717
1718 case ICmpInst::ICMP_UGT:
1719 std::swap(Op0, Op1); // Change icmp ugt -> icmp ult
1720 // FALL THROUGH
1721 case ICmpInst::ICMP_ULT:{ // icmp ult i1 A, B -> ~A & B
1722 Value *Not = Builder->CreateNot(Op0, I.getName()+"tmp");
1723 return BinaryOperator::CreateAnd(Not, Op1);
1724 }
1725 case ICmpInst::ICMP_SGT:
1726 std::swap(Op0, Op1); // Change icmp sgt -> icmp slt
1727 // FALL THROUGH
1728 case ICmpInst::ICMP_SLT: { // icmp slt i1 A, B -> A & ~B
1729 Value *Not = Builder->CreateNot(Op1, I.getName()+"tmp");
1730 return BinaryOperator::CreateAnd(Not, Op0);
1731 }
1732 case ICmpInst::ICMP_UGE:
1733 std::swap(Op0, Op1); // Change icmp uge -> icmp ule
1734 // FALL THROUGH
1735 case ICmpInst::ICMP_ULE: { // icmp ule i1 A, B -> ~A | B
1736 Value *Not = Builder->CreateNot(Op0, I.getName()+"tmp");
1737 return BinaryOperator::CreateOr(Not, Op1);
1738 }
1739 case ICmpInst::ICMP_SGE:
1740 std::swap(Op0, Op1); // Change icmp sge -> icmp sle
1741 // FALL THROUGH
1742 case ICmpInst::ICMP_SLE: { // icmp sle i1 A, B -> A | ~B
1743 Value *Not = Builder->CreateNot(Op1, I.getName()+"tmp");
1744 return BinaryOperator::CreateOr(Not, Op0);
1745 }
1746 }
1747 }
1748
1749 unsigned BitWidth = 0;
1750 if (TD)
1751 BitWidth = TD->getTypeSizeInBits(Ty->getScalarType());
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001752 else if (Ty->isIntOrIntVectorTy())
Chris Lattner02446fc2010-01-04 07:37:31 +00001753 BitWidth = Ty->getScalarSizeInBits();
1754
1755 bool isSignBit = false;
1756
1757 // See if we are doing a comparison with a constant.
1758 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
1759 Value *A = 0, *B = 0;
1760
Owen Andersone63dda52010-12-17 18:08:00 +00001761 // Match the following pattern, which is a common idiom when writing
1762 // overflow-safe integer arithmetic function. The source performs an
1763 // addition in wider type, and explicitly checks for overflow using
1764 // comparisons against INT_MIN and INT_MAX. Simplify this by using the
1765 // sadd_with_overflow intrinsic.
Chris Lattnerf0f568b2010-12-19 17:52:50 +00001766 //
1767 // TODO: This could probably be generalized to handle other overflow-safe
Owen Andersone63dda52010-12-17 18:08:00 +00001768 // operations if we worked out the formulas to compute the appropriate
1769 // magic constants.
1770 //
Chris Lattnerf0f568b2010-12-19 17:52:50 +00001771 // sum = a + b
1772 // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8
Owen Andersone63dda52010-12-17 18:08:00 +00001773 {
Chris Lattnerf0f568b2010-12-19 17:52:50 +00001774 ConstantInt *CI2; // I = icmp ugt (add (add A, B), CI2), CI
Owen Andersone63dda52010-12-17 18:08:00 +00001775 if (I.getPredicate() == ICmpInst::ICMP_UGT &&
Chris Lattnerf0f568b2010-12-19 17:52:50 +00001776 match(Op0, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2))))
1777 if (Instruction *Res = ProcessUGT_ADDCST_ADD(I, A, B, CI2, CI, Builder))
1778 return Res;
Owen Andersone63dda52010-12-17 18:08:00 +00001779 }
1780
Chris Lattner02446fc2010-01-04 07:37:31 +00001781 // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B)
1782 if (I.isEquality() && CI->isZero() &&
1783 match(Op0, m_Sub(m_Value(A), m_Value(B)))) {
1784 // (icmp cond A B) if cond is equality
1785 return new ICmpInst(I.getPredicate(), A, B);
1786 }
1787
1788 // If we have an icmp le or icmp ge instruction, turn it into the
1789 // appropriate icmp lt or icmp gt instruction. This allows us to rely on
1790 // them being folded in the code below. The SimplifyICmpInst code has
1791 // already handled the edge cases for us, so we just assert on them.
1792 switch (I.getPredicate()) {
1793 default: break;
1794 case ICmpInst::ICMP_ULE:
1795 assert(!CI->isMaxValue(false)); // A <=u MAX -> TRUE
1796 return new ICmpInst(ICmpInst::ICMP_ULT, Op0,
1797 ConstantInt::get(CI->getContext(), CI->getValue()+1));
1798 case ICmpInst::ICMP_SLE:
1799 assert(!CI->isMaxValue(true)); // A <=s MAX -> TRUE
1800 return new ICmpInst(ICmpInst::ICMP_SLT, Op0,
1801 ConstantInt::get(CI->getContext(), CI->getValue()+1));
1802 case ICmpInst::ICMP_UGE:
1803 assert(!CI->isMinValue(false)); // A >=u MIN -> TRUE
1804 return new ICmpInst(ICmpInst::ICMP_UGT, Op0,
1805 ConstantInt::get(CI->getContext(), CI->getValue()-1));
1806 case ICmpInst::ICMP_SGE:
1807 assert(!CI->isMinValue(true)); // A >=s MIN -> TRUE
1808 return new ICmpInst(ICmpInst::ICMP_SGT, Op0,
1809 ConstantInt::get(CI->getContext(), CI->getValue()-1));
1810 }
1811
1812 // If this comparison is a normal comparison, it demands all
1813 // bits, if it is a sign bit comparison, it only demands the sign bit.
1814 bool UnusedBit;
1815 isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit);
1816 }
1817
1818 // See if we can fold the comparison based on range information we can get
1819 // by checking whether bits are known to be zero or one in the input.
1820 if (BitWidth != 0) {
1821 APInt Op0KnownZero(BitWidth, 0), Op0KnownOne(BitWidth, 0);
1822 APInt Op1KnownZero(BitWidth, 0), Op1KnownOne(BitWidth, 0);
1823
1824 if (SimplifyDemandedBits(I.getOperandUse(0),
1825 isSignBit ? APInt::getSignBit(BitWidth)
1826 : APInt::getAllOnesValue(BitWidth),
1827 Op0KnownZero, Op0KnownOne, 0))
1828 return &I;
1829 if (SimplifyDemandedBits(I.getOperandUse(1),
1830 APInt::getAllOnesValue(BitWidth),
1831 Op1KnownZero, Op1KnownOne, 0))
1832 return &I;
1833
1834 // Given the known and unknown bits, compute a range that the LHS could be
1835 // in. Compute the Min, Max and RHS values based on the known bits. For the
1836 // EQ and NE we use unsigned values.
1837 APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
1838 APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
1839 if (I.isSigned()) {
1840 ComputeSignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne,
1841 Op0Min, Op0Max);
1842 ComputeSignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne,
1843 Op1Min, Op1Max);
1844 } else {
1845 ComputeUnsignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne,
1846 Op0Min, Op0Max);
1847 ComputeUnsignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne,
1848 Op1Min, Op1Max);
1849 }
1850
1851 // If Min and Max are known to be the same, then SimplifyDemandedBits
1852 // figured out that the LHS is a constant. Just constant fold this now so
1853 // that code below can assume that Min != Max.
1854 if (!isa<Constant>(Op0) && Op0Min == Op0Max)
1855 return new ICmpInst(I.getPredicate(),
1856 ConstantInt::get(I.getContext(), Op0Min), Op1);
1857 if (!isa<Constant>(Op1) && Op1Min == Op1Max)
1858 return new ICmpInst(I.getPredicate(), Op0,
1859 ConstantInt::get(I.getContext(), Op1Min));
1860
1861 // Based on the range information we know about the LHS, see if we can
1862 // simplify this comparison. For example, (x&4) < 8 is always true.
1863 switch (I.getPredicate()) {
1864 default: llvm_unreachable("Unknown icmp opcode!");
Chris Lattner75d8f592010-11-21 06:44:42 +00001865 case ICmpInst::ICMP_EQ: {
Chris Lattner02446fc2010-01-04 07:37:31 +00001866 if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
1867 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
Chris Lattner75d8f592010-11-21 06:44:42 +00001868
1869 // If all bits are known zero except for one, then we know at most one
1870 // bit is set. If the comparison is against zero, then this is a check
1871 // to see if *that* bit is set.
1872 APInt Op0KnownZeroInverted = ~Op0KnownZero;
1873 if (~Op1KnownZero == 0 && Op0KnownZeroInverted.isPowerOf2()) {
1874 // If the LHS is an AND with the same constant, look through it.
1875 Value *LHS = 0;
1876 ConstantInt *LHSC = 0;
1877 if (!match(Op0, m_And(m_Value(LHS), m_ConstantInt(LHSC))) ||
1878 LHSC->getValue() != Op0KnownZeroInverted)
1879 LHS = Op0;
1880
1881 // 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 +00001882 // then turn "((1 << x)&8) == 0" into "x != 3".
Chris Lattner75d8f592010-11-21 06:44:42 +00001883 Value *X = 0;
1884 if (match(LHS, m_Shl(m_One(), m_Value(X)))) {
1885 unsigned CmpVal = Op0KnownZeroInverted.countTrailingZeros();
Chris Lattner79b967b2010-11-23 02:42:04 +00001886 return new ICmpInst(ICmpInst::ICMP_NE, X,
Chris Lattner75d8f592010-11-21 06:44:42 +00001887 ConstantInt::get(X->getType(), CmpVal));
1888 }
1889
1890 // 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 +00001891 // then turn "((8 >>u x)&1) == 0" into "x != 3".
Chris Lattner75d8f592010-11-21 06:44:42 +00001892 ConstantInt *CI = 0;
1893 if (Op0KnownZeroInverted == 1 &&
1894 match(LHS, m_LShr(m_ConstantInt(CI), m_Value(X))) &&
1895 CI->getValue().isPowerOf2()) {
1896 unsigned CmpVal = CI->getValue().countTrailingZeros();
Chris Lattner79b967b2010-11-23 02:42:04 +00001897 return new ICmpInst(ICmpInst::ICMP_NE, X,
Chris Lattner75d8f592010-11-21 06:44:42 +00001898 ConstantInt::get(X->getType(), CmpVal));
1899 }
1900 }
1901
Chris Lattner02446fc2010-01-04 07:37:31 +00001902 break;
Chris Lattner75d8f592010-11-21 06:44:42 +00001903 }
1904 case ICmpInst::ICMP_NE: {
Chris Lattner02446fc2010-01-04 07:37:31 +00001905 if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
1906 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
Chris Lattner75d8f592010-11-21 06:44:42 +00001907
1908 // If all bits are known zero except for one, then we know at most one
1909 // bit is set. If the comparison is against zero, then this is a check
1910 // to see if *that* bit is set.
1911 APInt Op0KnownZeroInverted = ~Op0KnownZero;
1912 if (~Op1KnownZero == 0 && Op0KnownZeroInverted.isPowerOf2()) {
1913 // If the LHS is an AND with the same constant, look through it.
1914 Value *LHS = 0;
1915 ConstantInt *LHSC = 0;
1916 if (!match(Op0, m_And(m_Value(LHS), m_ConstantInt(LHSC))) ||
1917 LHSC->getValue() != Op0KnownZeroInverted)
1918 LHS = Op0;
1919
1920 // 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 +00001921 // then turn "((1 << x)&8) != 0" into "x == 3".
Chris Lattner75d8f592010-11-21 06:44:42 +00001922 Value *X = 0;
1923 if (match(LHS, m_Shl(m_One(), m_Value(X)))) {
1924 unsigned CmpVal = Op0KnownZeroInverted.countTrailingZeros();
Chris Lattner79b967b2010-11-23 02:42:04 +00001925 return new ICmpInst(ICmpInst::ICMP_EQ, X,
Chris Lattner75d8f592010-11-21 06:44:42 +00001926 ConstantInt::get(X->getType(), CmpVal));
1927 }
1928
1929 // 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 +00001930 // then turn "((8 >>u x)&1) != 0" into "x == 3".
Chris Lattner75d8f592010-11-21 06:44:42 +00001931 ConstantInt *CI = 0;
1932 if (Op0KnownZeroInverted == 1 &&
1933 match(LHS, m_LShr(m_ConstantInt(CI), m_Value(X))) &&
1934 CI->getValue().isPowerOf2()) {
1935 unsigned CmpVal = CI->getValue().countTrailingZeros();
Chris Lattner79b967b2010-11-23 02:42:04 +00001936 return new ICmpInst(ICmpInst::ICMP_EQ, X,
Chris Lattner75d8f592010-11-21 06:44:42 +00001937 ConstantInt::get(X->getType(), CmpVal));
1938 }
1939 }
1940
Chris Lattner02446fc2010-01-04 07:37:31 +00001941 break;
Chris Lattner75d8f592010-11-21 06:44:42 +00001942 }
Chris Lattner02446fc2010-01-04 07:37:31 +00001943 case ICmpInst::ICMP_ULT:
1944 if (Op0Max.ult(Op1Min)) // A <u B -> true if max(A) < min(B)
1945 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
1946 if (Op0Min.uge(Op1Max)) // A <u B -> false if min(A) >= max(B)
1947 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
1948 if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)
1949 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
1950 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
1951 if (Op1Max == Op0Min+1) // A <u C -> A == C-1 if min(A)+1 == C
1952 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
1953 ConstantInt::get(CI->getContext(), CI->getValue()-1));
1954
1955 // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear
1956 if (CI->isMinValue(true))
1957 return new ICmpInst(ICmpInst::ICMP_SGT, Op0,
1958 Constant::getAllOnesValue(Op0->getType()));
1959 }
1960 break;
1961 case ICmpInst::ICMP_UGT:
1962 if (Op0Min.ugt(Op1Max)) // A >u B -> true if min(A) > max(B)
1963 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
1964 if (Op0Max.ule(Op1Min)) // A >u B -> false if max(A) <= max(B)
1965 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
1966
1967 if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
1968 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
1969 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
1970 if (Op1Min == Op0Max-1) // A >u C -> A == C+1 if max(a)-1 == C
1971 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
1972 ConstantInt::get(CI->getContext(), CI->getValue()+1));
1973
1974 // (x >u 2147483647) -> (x <s 0) -> true if sign bit set
1975 if (CI->isMaxValue(true))
1976 return new ICmpInst(ICmpInst::ICMP_SLT, Op0,
1977 Constant::getNullValue(Op0->getType()));
1978 }
1979 break;
1980 case ICmpInst::ICMP_SLT:
1981 if (Op0Max.slt(Op1Min)) // A <s B -> true if max(A) < min(C)
1982 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
1983 if (Op0Min.sge(Op1Max)) // A <s B -> false if min(A) >= max(C)
1984 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
1985 if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)
1986 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
1987 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
1988 if (Op1Max == Op0Min+1) // A <s C -> A == C-1 if min(A)+1 == C
1989 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
1990 ConstantInt::get(CI->getContext(), CI->getValue()-1));
1991 }
1992 break;
1993 case ICmpInst::ICMP_SGT:
1994 if (Op0Min.sgt(Op1Max)) // A >s B -> true if min(A) > max(B)
1995 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
1996 if (Op0Max.sle(Op1Min)) // A >s B -> false if max(A) <= min(B)
1997 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
1998
1999 if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
2000 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
2001 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2002 if (Op1Min == Op0Max-1) // A >s C -> A == C+1 if max(A)-1 == C
2003 return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
2004 ConstantInt::get(CI->getContext(), CI->getValue()+1));
2005 }
2006 break;
2007 case ICmpInst::ICMP_SGE:
2008 assert(!isa<ConstantInt>(Op1) && "ICMP_SGE with ConstantInt not folded!");
2009 if (Op0Min.sge(Op1Max)) // A >=s B -> true if min(A) >= max(B)
2010 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2011 if (Op0Max.slt(Op1Min)) // A >=s B -> false if max(A) < min(B)
2012 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2013 break;
2014 case ICmpInst::ICMP_SLE:
2015 assert(!isa<ConstantInt>(Op1) && "ICMP_SLE with ConstantInt not folded!");
2016 if (Op0Max.sle(Op1Min)) // A <=s B -> true if max(A) <= min(B)
2017 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2018 if (Op0Min.sgt(Op1Max)) // A <=s B -> false if min(A) > max(B)
2019 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2020 break;
2021 case ICmpInst::ICMP_UGE:
2022 assert(!isa<ConstantInt>(Op1) && "ICMP_UGE with ConstantInt not folded!");
2023 if (Op0Min.uge(Op1Max)) // A >=u B -> true if min(A) >= max(B)
2024 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2025 if (Op0Max.ult(Op1Min)) // A >=u B -> false if max(A) < min(B)
2026 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2027 break;
2028 case ICmpInst::ICMP_ULE:
2029 assert(!isa<ConstantInt>(Op1) && "ICMP_ULE with ConstantInt not folded!");
2030 if (Op0Max.ule(Op1Min)) // A <=u B -> true if max(A) <= min(B)
2031 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2032 if (Op0Min.ugt(Op1Max)) // A <=u B -> false if min(A) > max(B)
2033 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2034 break;
2035 }
2036
2037 // Turn a signed comparison into an unsigned one if both operands
2038 // are known to have the same sign.
2039 if (I.isSigned() &&
2040 ((Op0KnownZero.isNegative() && Op1KnownZero.isNegative()) ||
2041 (Op0KnownOne.isNegative() && Op1KnownOne.isNegative())))
2042 return new ICmpInst(I.getUnsignedPredicate(), Op0, Op1);
2043 }
2044
2045 // Test if the ICmpInst instruction is used exclusively by a select as
2046 // part of a minimum or maximum operation. If so, refrain from doing
2047 // any other folding. This helps out other analyses which understand
2048 // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
2049 // and CodeGen. And in this case, at least one of the comparison
2050 // operands has at least one user besides the compare (the select),
2051 // which would often largely negate the benefit of folding anyway.
2052 if (I.hasOneUse())
2053 if (SelectInst *SI = dyn_cast<SelectInst>(*I.use_begin()))
2054 if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) ||
2055 (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1))
2056 return 0;
2057
2058 // See if we are doing a comparison between a constant and an instruction that
2059 // can be folded into the comparison.
2060 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2061 // Since the RHS is a ConstantInt (CI), if the left hand side is an
2062 // instruction, see if that instruction also has constants so that the
2063 // instruction can be folded into the icmp
2064 if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
2065 if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI))
2066 return Res;
2067 }
2068
2069 // Handle icmp with constant (but not simple integer constant) RHS
2070 if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
2071 if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
2072 switch (LHSI->getOpcode()) {
2073 case Instruction::GetElementPtr:
2074 // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null
2075 if (RHSC->isNullValue() &&
2076 cast<GetElementPtrInst>(LHSI)->hasAllZeroIndices())
2077 return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
2078 Constant::getNullValue(LHSI->getOperand(0)->getType()));
2079 break;
2080 case Instruction::PHI:
2081 // Only fold icmp into the PHI if the phi and icmp are in the same
2082 // block. If in the same block, we're encouraging jump threading. If
2083 // not, we are just pessimizing the code by making an i1 phi.
2084 if (LHSI->getParent() == I.getParent())
2085 if (Instruction *NV = FoldOpIntoPhi(I, true))
2086 return NV;
2087 break;
2088 case Instruction::Select: {
2089 // If either operand of the select is a constant, we can fold the
2090 // comparison into the select arms, which will cause one to be
2091 // constant folded and the select turned into a bitwise or.
2092 Value *Op1 = 0, *Op2 = 0;
2093 if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1)))
2094 Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
2095 if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2)))
2096 Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
2097
2098 // We only want to perform this transformation if it will not lead to
2099 // additional code. This is true if either both sides of the select
2100 // fold to a constant (in which case the icmp is replaced with a select
2101 // which will usually simplify) or this is the only user of the
2102 // select (in which case we are trading a select+icmp for a simpler
2103 // select+icmp).
2104 if ((Op1 && Op2) || (LHSI->hasOneUse() && (Op1 || Op2))) {
2105 if (!Op1)
2106 Op1 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(1),
2107 RHSC, I.getName());
2108 if (!Op2)
2109 Op2 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(2),
2110 RHSC, I.getName());
2111 return SelectInst::Create(LHSI->getOperand(0), Op1, Op2);
2112 }
2113 break;
2114 }
Chris Lattner02446fc2010-01-04 07:37:31 +00002115 case Instruction::IntToPtr:
2116 // icmp pred inttoptr(X), null -> icmp pred X, 0
2117 if (RHSC->isNullValue() && TD &&
2118 TD->getIntPtrType(RHSC->getContext()) ==
2119 LHSI->getOperand(0)->getType())
2120 return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
2121 Constant::getNullValue(LHSI->getOperand(0)->getType()));
2122 break;
2123
2124 case Instruction::Load:
2125 // Try to optimize things like "A[i] > 4" to index computations.
2126 if (GetElementPtrInst *GEP =
2127 dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) {
2128 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
2129 if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
2130 !cast<LoadInst>(LHSI)->isVolatile())
2131 if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV, I))
2132 return Res;
2133 }
2134 break;
2135 }
2136 }
2137
2138 // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now.
2139 if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op0))
2140 if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I))
2141 return NI;
2142 if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op1))
2143 if (Instruction *NI = FoldGEPICmp(GEP, Op0,
2144 ICmpInst::getSwappedPredicate(I.getPredicate()), I))
2145 return NI;
2146
2147 // Test to see if the operands of the icmp are casted versions of other
2148 // values. If the ptr->ptr cast can be stripped off both arguments, we do so
2149 // now.
2150 if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) {
Duncan Sands1df98592010-02-16 11:11:14 +00002151 if (Op0->getType()->isPointerTy() &&
Chris Lattner02446fc2010-01-04 07:37:31 +00002152 (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) {
2153 // We keep moving the cast from the left operand over to the right
2154 // operand, where it can often be eliminated completely.
2155 Op0 = CI->getOperand(0);
2156
2157 // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast
2158 // so eliminate it as well.
2159 if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1))
2160 Op1 = CI2->getOperand(0);
2161
2162 // If Op1 is a constant, we can fold the cast into the constant.
2163 if (Op0->getType() != Op1->getType()) {
2164 if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
2165 Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType());
2166 } else {
2167 // Otherwise, cast the RHS right before the icmp
2168 Op1 = Builder->CreateBitCast(Op1, Op0->getType());
2169 }
2170 }
2171 return new ICmpInst(I.getPredicate(), Op0, Op1);
2172 }
2173 }
2174
2175 if (isa<CastInst>(Op0)) {
2176 // Handle the special case of: icmp (cast bool to X), <cst>
2177 // This comes up when you have code like
2178 // int X = A < B;
2179 // if (X) ...
2180 // For generality, we handle any zero-extension of any operand comparison
2181 // with a constant or another cast from the same type.
2182 if (isa<Constant>(Op1) || isa<CastInst>(Op1))
2183 if (Instruction *R = visitICmpInstWithCastAndCast(I))
2184 return R;
2185 }
2186
2187 // See if it's the same type of instruction on the left and right.
2188 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
2189 if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) {
2190 if (Op0I->getOpcode() == Op1I->getOpcode() && Op0I->hasOneUse() &&
2191 Op1I->hasOneUse() && Op0I->getOperand(1) == Op1I->getOperand(1)) {
2192 switch (Op0I->getOpcode()) {
2193 default: break;
2194 case Instruction::Add:
2195 case Instruction::Sub:
2196 case Instruction::Xor:
2197 if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b
2198 return new ICmpInst(I.getPredicate(), Op0I->getOperand(0),
2199 Op1I->getOperand(0));
2200 // icmp u/s (a ^ signbit), (b ^ signbit) --> icmp s/u a, b
2201 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
2202 if (CI->getValue().isSignBit()) {
2203 ICmpInst::Predicate Pred = I.isSigned()
2204 ? I.getUnsignedPredicate()
2205 : I.getSignedPredicate();
2206 return new ICmpInst(Pred, Op0I->getOperand(0),
2207 Op1I->getOperand(0));
2208 }
2209
2210 if (CI->getValue().isMaxSignedValue()) {
2211 ICmpInst::Predicate Pred = I.isSigned()
2212 ? I.getUnsignedPredicate()
2213 : I.getSignedPredicate();
2214 Pred = I.getSwappedPredicate(Pred);
2215 return new ICmpInst(Pred, Op0I->getOperand(0),
2216 Op1I->getOperand(0));
2217 }
2218 }
2219 break;
2220 case Instruction::Mul:
2221 if (!I.isEquality())
2222 break;
2223
2224 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
2225 // a * Cst icmp eq/ne b * Cst --> a & Mask icmp b & Mask
2226 // Mask = -1 >> count-trailing-zeros(Cst).
2227 if (!CI->isZero() && !CI->isOne()) {
2228 const APInt &AP = CI->getValue();
2229 ConstantInt *Mask = ConstantInt::get(I.getContext(),
2230 APInt::getLowBitsSet(AP.getBitWidth(),
2231 AP.getBitWidth() -
2232 AP.countTrailingZeros()));
2233 Value *And1 = Builder->CreateAnd(Op0I->getOperand(0), Mask);
2234 Value *And2 = Builder->CreateAnd(Op1I->getOperand(0), Mask);
2235 return new ICmpInst(I.getPredicate(), And1, And2);
2236 }
2237 }
2238 break;
2239 }
2240 }
2241 }
2242 }
2243
2244 // ~x < ~y --> y < x
2245 { Value *A, *B;
2246 if (match(Op0, m_Not(m_Value(A))) &&
2247 match(Op1, m_Not(m_Value(B))))
2248 return new ICmpInst(I.getPredicate(), B, A);
2249 }
2250
2251 if (I.isEquality()) {
2252 Value *A, *B, *C, *D;
2253
2254 // -x == -y --> x == y
2255 if (match(Op0, m_Neg(m_Value(A))) &&
2256 match(Op1, m_Neg(m_Value(B))))
2257 return new ICmpInst(I.getPredicate(), A, B);
2258
2259 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
2260 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
2261 Value *OtherVal = A == Op1 ? B : A;
2262 return new ICmpInst(I.getPredicate(), OtherVal,
2263 Constant::getNullValue(A->getType()));
2264 }
2265
2266 if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
2267 // A^c1 == C^c2 --> A == C^(c1^c2)
2268 ConstantInt *C1, *C2;
2269 if (match(B, m_ConstantInt(C1)) &&
2270 match(D, m_ConstantInt(C2)) && Op1->hasOneUse()) {
2271 Constant *NC = ConstantInt::get(I.getContext(),
2272 C1->getValue() ^ C2->getValue());
2273 Value *Xor = Builder->CreateXor(C, NC, "tmp");
2274 return new ICmpInst(I.getPredicate(), A, Xor);
2275 }
2276
2277 // A^B == A^D -> B == D
2278 if (A == C) return new ICmpInst(I.getPredicate(), B, D);
2279 if (A == D) return new ICmpInst(I.getPredicate(), B, C);
2280 if (B == C) return new ICmpInst(I.getPredicate(), A, D);
2281 if (B == D) return new ICmpInst(I.getPredicate(), A, C);
2282 }
2283 }
2284
2285 if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
2286 (A == Op0 || B == Op0)) {
2287 // A == (A^B) -> B == 0
2288 Value *OtherVal = A == Op0 ? B : A;
2289 return new ICmpInst(I.getPredicate(), OtherVal,
2290 Constant::getNullValue(A->getType()));
2291 }
2292
2293 // (A-B) == A -> B == 0
2294 if (match(Op0, m_Sub(m_Specific(Op1), m_Value(B))))
2295 return new ICmpInst(I.getPredicate(), B,
2296 Constant::getNullValue(B->getType()));
2297
2298 // A == (A-B) -> B == 0
2299 if (match(Op1, m_Sub(m_Specific(Op0), m_Value(B))))
2300 return new ICmpInst(I.getPredicate(), B,
2301 Constant::getNullValue(B->getType()));
2302
2303 // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
2304 if (Op0->hasOneUse() && Op1->hasOneUse() &&
2305 match(Op0, m_And(m_Value(A), m_Value(B))) &&
2306 match(Op1, m_And(m_Value(C), m_Value(D)))) {
2307 Value *X = 0, *Y = 0, *Z = 0;
2308
2309 if (A == C) {
2310 X = B; Y = D; Z = A;
2311 } else if (A == D) {
2312 X = B; Y = C; Z = A;
2313 } else if (B == C) {
2314 X = A; Y = D; Z = B;
2315 } else if (B == D) {
2316 X = A; Y = C; Z = B;
2317 }
2318
2319 if (X) { // Build (X^Y) & Z
2320 Op1 = Builder->CreateXor(X, Y, "tmp");
2321 Op1 = Builder->CreateAnd(Op1, Z, "tmp");
2322 I.setOperand(0, Op1);
2323 I.setOperand(1, Constant::getNullValue(Op1->getType()));
2324 return &I;
2325 }
2326 }
2327 }
2328
2329 {
2330 Value *X; ConstantInt *Cst;
2331 // icmp X+Cst, X
2332 if (match(Op0, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op1 == X)
2333 return FoldICmpAddOpCst(I, X, Cst, I.getPredicate(), Op0);
2334
2335 // icmp X, X+Cst
2336 if (match(Op1, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op0 == X)
2337 return FoldICmpAddOpCst(I, X, Cst, I.getSwappedPredicate(), Op1);
2338 }
2339 return Changed ? &I : 0;
2340}
2341
2342
2343
2344
2345
2346
2347/// FoldFCmp_IntToFP_Cst - Fold fcmp ([us]itofp x, cst) if possible.
2348///
2349Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I,
2350 Instruction *LHSI,
2351 Constant *RHSC) {
2352 if (!isa<ConstantFP>(RHSC)) return 0;
2353 const APFloat &RHS = cast<ConstantFP>(RHSC)->getValueAPF();
2354
2355 // Get the width of the mantissa. We don't want to hack on conversions that
2356 // might lose information from the integer, e.g. "i64 -> float"
2357 int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
2358 if (MantissaWidth == -1) return 0; // Unknown.
2359
2360 // Check to see that the input is converted from an integer type that is small
2361 // enough that preserves all bits. TODO: check here for "known" sign bits.
2362 // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
2363 unsigned InputSize = LHSI->getOperand(0)->getType()->getScalarSizeInBits();
2364
2365 // If this is a uitofp instruction, we need an extra bit to hold the sign.
2366 bool LHSUnsigned = isa<UIToFPInst>(LHSI);
2367 if (LHSUnsigned)
2368 ++InputSize;
2369
2370 // If the conversion would lose info, don't hack on this.
2371 if ((int)InputSize > MantissaWidth)
2372 return 0;
2373
2374 // Otherwise, we can potentially simplify the comparison. We know that it
2375 // will always come through as an integer value and we know the constant is
2376 // not a NAN (it would have been previously simplified).
2377 assert(!RHS.isNaN() && "NaN comparison not already folded!");
2378
2379 ICmpInst::Predicate Pred;
2380 switch (I.getPredicate()) {
2381 default: llvm_unreachable("Unexpected predicate!");
2382 case FCmpInst::FCMP_UEQ:
2383 case FCmpInst::FCMP_OEQ:
2384 Pred = ICmpInst::ICMP_EQ;
2385 break;
2386 case FCmpInst::FCMP_UGT:
2387 case FCmpInst::FCMP_OGT:
2388 Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
2389 break;
2390 case FCmpInst::FCMP_UGE:
2391 case FCmpInst::FCMP_OGE:
2392 Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
2393 break;
2394 case FCmpInst::FCMP_ULT:
2395 case FCmpInst::FCMP_OLT:
2396 Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
2397 break;
2398 case FCmpInst::FCMP_ULE:
2399 case FCmpInst::FCMP_OLE:
2400 Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
2401 break;
2402 case FCmpInst::FCMP_UNE:
2403 case FCmpInst::FCMP_ONE:
2404 Pred = ICmpInst::ICMP_NE;
2405 break;
2406 case FCmpInst::FCMP_ORD:
2407 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2408 case FCmpInst::FCMP_UNO:
2409 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2410 }
2411
2412 const IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType());
2413
2414 // Now we know that the APFloat is a normal number, zero or inf.
2415
2416 // See if the FP constant is too large for the integer. For example,
2417 // comparing an i8 to 300.0.
2418 unsigned IntWidth = IntTy->getScalarSizeInBits();
2419
2420 if (!LHSUnsigned) {
2421 // If the RHS value is > SignedMax, fold the comparison. This handles +INF
2422 // and large values.
2423 APFloat SMax(RHS.getSemantics(), APFloat::fcZero, false);
2424 SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true,
2425 APFloat::rmNearestTiesToEven);
2426 if (SMax.compare(RHS) == APFloat::cmpLessThan) { // smax < 13123.0
2427 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||
2428 Pred == ICmpInst::ICMP_SLE)
2429 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2430 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2431 }
2432 } else {
2433 // If the RHS value is > UnsignedMax, fold the comparison. This handles
2434 // +INF and large values.
2435 APFloat UMax(RHS.getSemantics(), APFloat::fcZero, false);
2436 UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false,
2437 APFloat::rmNearestTiesToEven);
2438 if (UMax.compare(RHS) == APFloat::cmpLessThan) { // umax < 13123.0
2439 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||
2440 Pred == ICmpInst::ICMP_ULE)
2441 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2442 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2443 }
2444 }
2445
2446 if (!LHSUnsigned) {
2447 // See if the RHS value is < SignedMin.
2448 APFloat SMin(RHS.getSemantics(), APFloat::fcZero, false);
2449 SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true,
2450 APFloat::rmNearestTiesToEven);
2451 if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0
2452 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
2453 Pred == ICmpInst::ICMP_SGE)
2454 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2455 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2456 }
2457 }
2458
2459 // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
2460 // [0, UMAX], but it may still be fractional. See if it is fractional by
2461 // casting the FP value to the integer value and back, checking for equality.
2462 // Don't do this for zero, because -0.0 is not fractional.
2463 Constant *RHSInt = LHSUnsigned
2464 ? ConstantExpr::getFPToUI(RHSC, IntTy)
2465 : ConstantExpr::getFPToSI(RHSC, IntTy);
2466 if (!RHS.isZero()) {
2467 bool Equal = LHSUnsigned
2468 ? ConstantExpr::getUIToFP(RHSInt, RHSC->getType()) == RHSC
2469 : ConstantExpr::getSIToFP(RHSInt, RHSC->getType()) == RHSC;
2470 if (!Equal) {
2471 // If we had a comparison against a fractional value, we have to adjust
2472 // the compare predicate and sometimes the value. RHSC is rounded towards
2473 // zero at this point.
2474 switch (Pred) {
2475 default: llvm_unreachable("Unexpected integer comparison!");
2476 case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
2477 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2478 case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
2479 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2480 case ICmpInst::ICMP_ULE:
2481 // (float)int <= 4.4 --> int <= 4
2482 // (float)int <= -4.4 --> false
2483 if (RHS.isNegative())
2484 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2485 break;
2486 case ICmpInst::ICMP_SLE:
2487 // (float)int <= 4.4 --> int <= 4
2488 // (float)int <= -4.4 --> int < -4
2489 if (RHS.isNegative())
2490 Pred = ICmpInst::ICMP_SLT;
2491 break;
2492 case ICmpInst::ICMP_ULT:
2493 // (float)int < -4.4 --> false
2494 // (float)int < 4.4 --> int <= 4
2495 if (RHS.isNegative())
2496 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2497 Pred = ICmpInst::ICMP_ULE;
2498 break;
2499 case ICmpInst::ICMP_SLT:
2500 // (float)int < -4.4 --> int < -4
2501 // (float)int < 4.4 --> int <= 4
2502 if (!RHS.isNegative())
2503 Pred = ICmpInst::ICMP_SLE;
2504 break;
2505 case ICmpInst::ICMP_UGT:
2506 // (float)int > 4.4 --> int > 4
2507 // (float)int > -4.4 --> true
2508 if (RHS.isNegative())
2509 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2510 break;
2511 case ICmpInst::ICMP_SGT:
2512 // (float)int > 4.4 --> int > 4
2513 // (float)int > -4.4 --> int >= -4
2514 if (RHS.isNegative())
2515 Pred = ICmpInst::ICMP_SGE;
2516 break;
2517 case ICmpInst::ICMP_UGE:
2518 // (float)int >= -4.4 --> true
2519 // (float)int >= 4.4 --> int > 4
2520 if (!RHS.isNegative())
2521 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2522 Pred = ICmpInst::ICMP_UGT;
2523 break;
2524 case ICmpInst::ICMP_SGE:
2525 // (float)int >= -4.4 --> int >= -4
2526 // (float)int >= 4.4 --> int > 4
2527 if (!RHS.isNegative())
2528 Pred = ICmpInst::ICMP_SGT;
2529 break;
2530 }
2531 }
2532 }
2533
2534 // Lower this FP comparison into an appropriate integer version of the
2535 // comparison.
2536 return new ICmpInst(Pred, LHSI->getOperand(0), RHSInt);
2537}
2538
2539Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) {
2540 bool Changed = false;
2541
2542 /// Orders the operands of the compare so that they are listed from most
2543 /// complex to least complex. This puts constants before unary operators,
2544 /// before binary operators.
2545 if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) {
2546 I.swapOperands();
2547 Changed = true;
2548 }
2549
2550 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2551
2552 if (Value *V = SimplifyFCmpInst(I.getPredicate(), Op0, Op1, TD))
2553 return ReplaceInstUsesWith(I, V);
2554
2555 // Simplify 'fcmp pred X, X'
2556 if (Op0 == Op1) {
2557 switch (I.getPredicate()) {
2558 default: llvm_unreachable("Unknown predicate!");
2559 case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
2560 case FCmpInst::FCMP_ULT: // True if unordered or less than
2561 case FCmpInst::FCMP_UGT: // True if unordered or greater than
2562 case FCmpInst::FCMP_UNE: // True if unordered or not equal
2563 // Canonicalize these to be 'fcmp uno %X, 0.0'.
2564 I.setPredicate(FCmpInst::FCMP_UNO);
2565 I.setOperand(1, Constant::getNullValue(Op0->getType()));
2566 return &I;
2567
2568 case FCmpInst::FCMP_ORD: // True if ordered (no nans)
2569 case FCmpInst::FCMP_OEQ: // True if ordered and equal
2570 case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal
2571 case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
2572 // Canonicalize these to be 'fcmp ord %X, 0.0'.
2573 I.setPredicate(FCmpInst::FCMP_ORD);
2574 I.setOperand(1, Constant::getNullValue(Op0->getType()));
2575 return &I;
2576 }
2577 }
2578
2579 // Handle fcmp with constant RHS
2580 if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
2581 if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
2582 switch (LHSI->getOpcode()) {
2583 case Instruction::PHI:
2584 // Only fold fcmp into the PHI if the phi and fcmp are in the same
2585 // block. If in the same block, we're encouraging jump threading. If
2586 // not, we are just pessimizing the code by making an i1 phi.
2587 if (LHSI->getParent() == I.getParent())
2588 if (Instruction *NV = FoldOpIntoPhi(I, true))
2589 return NV;
2590 break;
2591 case Instruction::SIToFP:
2592 case Instruction::UIToFP:
2593 if (Instruction *NV = FoldFCmp_IntToFP_Cst(I, LHSI, RHSC))
2594 return NV;
2595 break;
2596 case Instruction::Select: {
2597 // If either operand of the select is a constant, we can fold the
2598 // comparison into the select arms, which will cause one to be
2599 // constant folded and the select turned into a bitwise or.
2600 Value *Op1 = 0, *Op2 = 0;
2601 if (LHSI->hasOneUse()) {
2602 if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
2603 // Fold the known value into the constant operand.
2604 Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC);
2605 // Insert a new FCmp of the other select operand.
2606 Op2 = Builder->CreateFCmp(I.getPredicate(),
2607 LHSI->getOperand(2), RHSC, I.getName());
2608 } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
2609 // Fold the known value into the constant operand.
2610 Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC);
2611 // Insert a new FCmp of the other select operand.
2612 Op1 = Builder->CreateFCmp(I.getPredicate(), LHSI->getOperand(1),
2613 RHSC, I.getName());
2614 }
2615 }
2616
2617 if (Op1)
2618 return SelectInst::Create(LHSI->getOperand(0), Op1, Op2);
2619 break;
2620 }
Dan Gohman39516a62010-02-24 06:46:09 +00002621 case Instruction::Load:
2622 if (GetElementPtrInst *GEP =
2623 dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) {
2624 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
2625 if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
2626 !cast<LoadInst>(LHSI)->isVolatile())
2627 if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV, I))
2628 return Res;
2629 }
2630 break;
Chris Lattner02446fc2010-01-04 07:37:31 +00002631 }
Chris Lattner02446fc2010-01-04 07:37:31 +00002632 }
2633
2634 return Changed ? &I : 0;
2635}