InstCombine: simplify comparisons to zero of (shl %x, Cst) or (mul %x, Cst)
[oota-llvm.git] / lib / Transforms / InstCombine / InstCombineCompares.cpp
1 //===- 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/Analysis/ConstantFolding.h"
16 #include "llvm/Analysis/InstructionSimplify.h"
17 #include "llvm/Analysis/MemoryBuiltins.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/IntrinsicInst.h"
20 #include "llvm/Support/ConstantRange.h"
21 #include "llvm/Support/GetElementPtrTypeIterator.h"
22 #include "llvm/Support/PatternMatch.h"
23 #include "llvm/Target/TargetLibraryInfo.h"
24 using namespace llvm;
25 using namespace PatternMatch;
26
27 static ConstantInt *getOne(Constant *C) {
28   return ConstantInt::get(cast<IntegerType>(C->getType()), 1);
29 }
30
31 /// AddOne - Add one to a ConstantInt
32 static Constant *AddOne(Constant *C) {
33   return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
34 }
35 /// SubOne - Subtract one from a ConstantInt
36 static Constant *SubOne(Constant *C) {
37   return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
38 }
39
40 static ConstantInt *ExtractElement(Constant *V, Constant *Idx) {
41   return cast<ConstantInt>(ConstantExpr::getExtractElement(V, Idx));
42 }
43
44 static bool HasAddOverflow(ConstantInt *Result,
45                            ConstantInt *In1, ConstantInt *In2,
46                            bool IsSigned) {
47   if (!IsSigned)
48     return Result->getValue().ult(In1->getValue());
49
50   if (In2->isNegative())
51     return Result->getValue().sgt(In1->getValue());
52   return Result->getValue().slt(In1->getValue());
53 }
54
55 /// AddWithOverflow - Compute Result = In1+In2, returning true if the result
56 /// overflowed for this type.
57 static bool AddWithOverflow(Constant *&Result, Constant *In1,
58                             Constant *In2, bool IsSigned = false) {
59   Result = ConstantExpr::getAdd(In1, In2);
60
61   if (VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
62     for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
63       Constant *Idx = ConstantInt::get(Type::getInt32Ty(In1->getContext()), i);
64       if (HasAddOverflow(ExtractElement(Result, Idx),
65                          ExtractElement(In1, Idx),
66                          ExtractElement(In2, Idx),
67                          IsSigned))
68         return true;
69     }
70     return false;
71   }
72
73   return HasAddOverflow(cast<ConstantInt>(Result),
74                         cast<ConstantInt>(In1), cast<ConstantInt>(In2),
75                         IsSigned);
76 }
77
78 static bool HasSubOverflow(ConstantInt *Result,
79                            ConstantInt *In1, ConstantInt *In2,
80                            bool IsSigned) {
81   if (!IsSigned)
82     return Result->getValue().ugt(In1->getValue());
83
84   if (In2->isNegative())
85     return Result->getValue().slt(In1->getValue());
86
87   return Result->getValue().sgt(In1->getValue());
88 }
89
90 /// SubWithOverflow - Compute Result = In1-In2, returning true if the result
91 /// overflowed for this type.
92 static bool SubWithOverflow(Constant *&Result, Constant *In1,
93                             Constant *In2, bool IsSigned = false) {
94   Result = ConstantExpr::getSub(In1, In2);
95
96   if (VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
97     for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
98       Constant *Idx = ConstantInt::get(Type::getInt32Ty(In1->getContext()), i);
99       if (HasSubOverflow(ExtractElement(Result, Idx),
100                          ExtractElement(In1, Idx),
101                          ExtractElement(In2, Idx),
102                          IsSigned))
103         return true;
104     }
105     return false;
106   }
107
108   return HasSubOverflow(cast<ConstantInt>(Result),
109                         cast<ConstantInt>(In1), cast<ConstantInt>(In2),
110                         IsSigned);
111 }
112
113 /// isSignBitCheck - Given an exploded icmp instruction, return true if the
114 /// comparison only checks the sign bit.  If it only checks the sign bit, set
115 /// TrueIfSigned if the result of the comparison is true when the input value is
116 /// signed.
117 static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS,
118                            bool &TrueIfSigned) {
119   switch (pred) {
120   case ICmpInst::ICMP_SLT:   // True if LHS s< 0
121     TrueIfSigned = true;
122     return RHS->isZero();
123   case ICmpInst::ICMP_SLE:   // True if LHS s<= RHS and RHS == -1
124     TrueIfSigned = true;
125     return RHS->isAllOnesValue();
126   case ICmpInst::ICMP_SGT:   // True if LHS s> -1
127     TrueIfSigned = false;
128     return RHS->isAllOnesValue();
129   case ICmpInst::ICMP_UGT:
130     // True if LHS u> RHS and RHS == high-bit-mask - 1
131     TrueIfSigned = true;
132     return RHS->isMaxValue(true);
133   case ICmpInst::ICMP_UGE:
134     // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc)
135     TrueIfSigned = true;
136     return RHS->getValue().isSignBit();
137   default:
138     return false;
139   }
140 }
141
142 /// Returns true if the exploded icmp can be expressed as a comparison to zero
143 /// and update the predicate accordingly. The signedness of the comparison is
144 static bool isSignTest(ICmpInst::Predicate &pred, const ConstantInt *RHS) {
145   if (!ICmpInst::isSigned(pred))
146     return false;
147
148   if (RHS->isZero())
149     return true;
150
151   if (RHS->isOne())
152     switch (pred) {
153     case ICmpInst::ICMP_SGE:
154       pred = ICmpInst::ICMP_SGT;
155       return true;
156     case ICmpInst::ICMP_SLT:
157       pred = ICmpInst::ICMP_SLE;
158       return true;
159     default:
160       return false;
161     }
162
163   if (RHS->isAllOnesValue())
164     switch (pred) {
165     case ICmpInst::ICMP_SLE:
166       pred = ICmpInst::ICMP_SLT;
167       return true;
168     case ICmpInst::ICMP_SGT:
169       pred = ICmpInst::ICMP_SGE;
170       return true;
171     default:
172       return false;
173     }
174
175   return false;
176 }
177
178 // isHighOnes - Return true if the constant is of the form 1+0+.
179 // This is the same as lowones(~X).
180 static bool isHighOnes(const ConstantInt *CI) {
181   return (~CI->getValue() + 1).isPowerOf2();
182 }
183
184 /// ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a
185 /// set of known zero and one bits, compute the maximum and minimum values that
186 /// could have the specified known zero and known one bits, returning them in
187 /// min/max.
188 static void ComputeSignedMinMaxValuesFromKnownBits(const APInt& KnownZero,
189                                                    const APInt& KnownOne,
190                                                    APInt& Min, APInt& Max) {
191   assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() &&
192          KnownZero.getBitWidth() == Min.getBitWidth() &&
193          KnownZero.getBitWidth() == Max.getBitWidth() &&
194          "KnownZero, KnownOne and Min, Max must have equal bitwidth.");
195   APInt UnknownBits = ~(KnownZero|KnownOne);
196
197   // The minimum value is when all unknown bits are zeros, EXCEPT for the sign
198   // bit if it is unknown.
199   Min = KnownOne;
200   Max = KnownOne|UnknownBits;
201
202   if (UnknownBits.isNegative()) { // Sign bit is unknown
203     Min.setBit(Min.getBitWidth()-1);
204     Max.clearBit(Max.getBitWidth()-1);
205   }
206 }
207
208 // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and
209 // a set of known zero and one bits, compute the maximum and minimum values that
210 // could have the specified known zero and known one bits, returning them in
211 // min/max.
212 static void ComputeUnsignedMinMaxValuesFromKnownBits(const APInt &KnownZero,
213                                                      const APInt &KnownOne,
214                                                      APInt &Min, APInt &Max) {
215   assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() &&
216          KnownZero.getBitWidth() == Min.getBitWidth() &&
217          KnownZero.getBitWidth() == Max.getBitWidth() &&
218          "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth.");
219   APInt UnknownBits = ~(KnownZero|KnownOne);
220
221   // The minimum value is when the unknown bits are all zeros.
222   Min = KnownOne;
223   // The maximum value is when the unknown bits are all ones.
224   Max = KnownOne|UnknownBits;
225 }
226
227
228
229 /// FoldCmpLoadFromIndexedGlobal - Called we see this pattern:
230 ///   cmp pred (load (gep GV, ...)), cmpcst
231 /// where GV is a global variable with a constant initializer.  Try to simplify
232 /// this into some simple computation that does not need the load.  For example
233 /// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3".
234 ///
235 /// If AndCst is non-null, then the loaded value is masked with that constant
236 /// before doing the comparison.  This handles cases like "A[i]&4 == 0".
237 Instruction *InstCombiner::
238 FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV,
239                              CmpInst &ICI, ConstantInt *AndCst) {
240   // We need TD information to know the pointer size unless this is inbounds.
241   if (!GEP->isInBounds() && TD == 0) return 0;
242
243   Constant *Init = GV->getInitializer();
244   if (!isa<ConstantArray>(Init) && !isa<ConstantDataArray>(Init))
245     return 0;
246   
247   uint64_t ArrayElementCount = Init->getType()->getArrayNumElements();
248   if (ArrayElementCount > 1024) return 0;  // Don't blow up on huge arrays.
249
250   // There are many forms of this optimization we can handle, for now, just do
251   // the simple index into a single-dimensional array.
252   //
253   // Require: GEP GV, 0, i {{, constant indices}}
254   if (GEP->getNumOperands() < 3 ||
255       !isa<ConstantInt>(GEP->getOperand(1)) ||
256       !cast<ConstantInt>(GEP->getOperand(1))->isZero() ||
257       isa<Constant>(GEP->getOperand(2)))
258     return 0;
259
260   // Check that indices after the variable are constants and in-range for the
261   // type they index.  Collect the indices.  This is typically for arrays of
262   // structs.
263   SmallVector<unsigned, 4> LaterIndices;
264
265   Type *EltTy = Init->getType()->getArrayElementType();
266   for (unsigned i = 3, e = GEP->getNumOperands(); i != e; ++i) {
267     ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(i));
268     if (Idx == 0) return 0;  // Variable index.
269
270     uint64_t IdxVal = Idx->getZExtValue();
271     if ((unsigned)IdxVal != IdxVal) return 0; // Too large array index.
272
273     if (StructType *STy = dyn_cast<StructType>(EltTy))
274       EltTy = STy->getElementType(IdxVal);
275     else if (ArrayType *ATy = dyn_cast<ArrayType>(EltTy)) {
276       if (IdxVal >= ATy->getNumElements()) return 0;
277       EltTy = ATy->getElementType();
278     } else {
279       return 0; // Unknown type.
280     }
281
282     LaterIndices.push_back(IdxVal);
283   }
284
285   enum { Overdefined = -3, Undefined = -2 };
286
287   // Variables for our state machines.
288
289   // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form
290   // "i == 47 | i == 87", where 47 is the first index the condition is true for,
291   // and 87 is the second (and last) index.  FirstTrueElement is -2 when
292   // undefined, otherwise set to the first true element.  SecondTrueElement is
293   // -2 when undefined, -3 when overdefined and >= 0 when that index is true.
294   int FirstTrueElement = Undefined, SecondTrueElement = Undefined;
295
296   // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the
297   // form "i != 47 & i != 87".  Same state transitions as for true elements.
298   int FirstFalseElement = Undefined, SecondFalseElement = Undefined;
299
300   /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these
301   /// define a state machine that triggers for ranges of values that the index
302   /// is true or false for.  This triggers on things like "abbbbc"[i] == 'b'.
303   /// This is -2 when undefined, -3 when overdefined, and otherwise the last
304   /// index in the range (inclusive).  We use -2 for undefined here because we
305   /// use relative comparisons and don't want 0-1 to match -1.
306   int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined;
307
308   // MagicBitvector - This is a magic bitvector where we set a bit if the
309   // comparison is true for element 'i'.  If there are 64 elements or less in
310   // the array, this will fully represent all the comparison results.
311   uint64_t MagicBitvector = 0;
312
313
314   // Scan the array and see if one of our patterns matches.
315   Constant *CompareRHS = cast<Constant>(ICI.getOperand(1));
316   for (unsigned i = 0, e = ArrayElementCount; i != e; ++i) {
317     Constant *Elt = Init->getAggregateElement(i);
318     if (Elt == 0) return 0;
319
320     // If this is indexing an array of structures, get the structure element.
321     if (!LaterIndices.empty())
322       Elt = ConstantExpr::getExtractValue(Elt, LaterIndices);
323
324     // If the element is masked, handle it.
325     if (AndCst) Elt = ConstantExpr::getAnd(Elt, AndCst);
326
327     // Find out if the comparison would be true or false for the i'th element.
328     Constant *C = ConstantFoldCompareInstOperands(ICI.getPredicate(), Elt,
329                                                   CompareRHS, TD, TLI);
330     // If the result is undef for this element, ignore it.
331     if (isa<UndefValue>(C)) {
332       // Extend range state machines to cover this element in case there is an
333       // undef in the middle of the range.
334       if (TrueRangeEnd == (int)i-1)
335         TrueRangeEnd = i;
336       if (FalseRangeEnd == (int)i-1)
337         FalseRangeEnd = i;
338       continue;
339     }
340
341     // If we can't compute the result for any of the elements, we have to give
342     // up evaluating the entire conditional.
343     if (!isa<ConstantInt>(C)) return 0;
344
345     // Otherwise, we know if the comparison is true or false for this element,
346     // update our state machines.
347     bool IsTrueForElt = !cast<ConstantInt>(C)->isZero();
348
349     // State machine for single/double/range index comparison.
350     if (IsTrueForElt) {
351       // Update the TrueElement state machine.
352       if (FirstTrueElement == Undefined)
353         FirstTrueElement = TrueRangeEnd = i;  // First true element.
354       else {
355         // Update double-compare state machine.
356         if (SecondTrueElement == Undefined)
357           SecondTrueElement = i;
358         else
359           SecondTrueElement = Overdefined;
360
361         // Update range state machine.
362         if (TrueRangeEnd == (int)i-1)
363           TrueRangeEnd = i;
364         else
365           TrueRangeEnd = Overdefined;
366       }
367     } else {
368       // Update the FalseElement state machine.
369       if (FirstFalseElement == Undefined)
370         FirstFalseElement = FalseRangeEnd = i; // First false element.
371       else {
372         // Update double-compare state machine.
373         if (SecondFalseElement == Undefined)
374           SecondFalseElement = i;
375         else
376           SecondFalseElement = Overdefined;
377
378         // Update range state machine.
379         if (FalseRangeEnd == (int)i-1)
380           FalseRangeEnd = i;
381         else
382           FalseRangeEnd = Overdefined;
383       }
384     }
385
386
387     // If this element is in range, update our magic bitvector.
388     if (i < 64 && IsTrueForElt)
389       MagicBitvector |= 1ULL << i;
390
391     // If all of our states become overdefined, bail out early.  Since the
392     // predicate is expensive, only check it every 8 elements.  This is only
393     // really useful for really huge arrays.
394     if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined &&
395         SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined &&
396         FalseRangeEnd == Overdefined)
397       return 0;
398   }
399
400   // Now that we've scanned the entire array, emit our new comparison(s).  We
401   // order the state machines in complexity of the generated code.
402   Value *Idx = GEP->getOperand(2);
403
404   // If the index is larger than the pointer size of the target, truncate the
405   // index down like the GEP would do implicitly.  We don't have to do this for
406   // an inbounds GEP because the index can't be out of range.
407   if (!GEP->isInBounds() &&
408       Idx->getType()->getPrimitiveSizeInBits() > TD->getPointerSizeInBits())
409     Idx = Builder->CreateTrunc(Idx, TD->getIntPtrType(Idx->getContext()));
410
411   // If the comparison is only true for one or two elements, emit direct
412   // comparisons.
413   if (SecondTrueElement != Overdefined) {
414     // None true -> false.
415     if (FirstTrueElement == Undefined)
416       return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(GEP->getContext()));
417
418     Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement);
419
420     // True for one element -> 'i == 47'.
421     if (SecondTrueElement == Undefined)
422       return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx);
423
424     // True for two elements -> 'i == 47 | i == 72'.
425     Value *C1 = Builder->CreateICmpEQ(Idx, FirstTrueIdx);
426     Value *SecondTrueIdx = ConstantInt::get(Idx->getType(), SecondTrueElement);
427     Value *C2 = Builder->CreateICmpEQ(Idx, SecondTrueIdx);
428     return BinaryOperator::CreateOr(C1, C2);
429   }
430
431   // If the comparison is only false for one or two elements, emit direct
432   // comparisons.
433   if (SecondFalseElement != Overdefined) {
434     // None false -> true.
435     if (FirstFalseElement == Undefined)
436       return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(GEP->getContext()));
437
438     Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement);
439
440     // False for one element -> 'i != 47'.
441     if (SecondFalseElement == Undefined)
442       return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx);
443
444     // False for two elements -> 'i != 47 & i != 72'.
445     Value *C1 = Builder->CreateICmpNE(Idx, FirstFalseIdx);
446     Value *SecondFalseIdx = ConstantInt::get(Idx->getType(),SecondFalseElement);
447     Value *C2 = Builder->CreateICmpNE(Idx, SecondFalseIdx);
448     return BinaryOperator::CreateAnd(C1, C2);
449   }
450
451   // If the comparison can be replaced with a range comparison for the elements
452   // where it is true, emit the range check.
453   if (TrueRangeEnd != Overdefined) {
454     assert(TrueRangeEnd != FirstTrueElement && "Should emit single compare");
455
456     // Generate (i-FirstTrue) <u (TrueRangeEnd-FirstTrue+1).
457     if (FirstTrueElement) {
458       Value *Offs = ConstantInt::get(Idx->getType(), -FirstTrueElement);
459       Idx = Builder->CreateAdd(Idx, Offs);
460     }
461
462     Value *End = ConstantInt::get(Idx->getType(),
463                                   TrueRangeEnd-FirstTrueElement+1);
464     return new ICmpInst(ICmpInst::ICMP_ULT, Idx, End);
465   }
466
467   // False range check.
468   if (FalseRangeEnd != Overdefined) {
469     assert(FalseRangeEnd != FirstFalseElement && "Should emit single compare");
470     // Generate (i-FirstFalse) >u (FalseRangeEnd-FirstFalse).
471     if (FirstFalseElement) {
472       Value *Offs = ConstantInt::get(Idx->getType(), -FirstFalseElement);
473       Idx = Builder->CreateAdd(Idx, Offs);
474     }
475
476     Value *End = ConstantInt::get(Idx->getType(),
477                                   FalseRangeEnd-FirstFalseElement);
478     return new ICmpInst(ICmpInst::ICMP_UGT, Idx, End);
479   }
480
481
482   // If a magic bitvector captures the entire comparison state
483   // of this load, replace it with computation that does:
484   //   ((magic_cst >> i) & 1) != 0
485   {
486     Type *Ty = 0;
487
488     // Look for an appropriate type:
489     // - The type of Idx if the magic fits
490     // - The smallest fitting legal type if we have a DataLayout
491     // - Default to i32
492     if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth())
493       Ty = Idx->getType();
494     else if (TD)
495       Ty = TD->getSmallestLegalIntType(Init->getContext(), ArrayElementCount);
496     else if (ArrayElementCount <= 32)
497       Ty = Type::getInt32Ty(Init->getContext());
498
499     if (Ty != 0) {
500       Value *V = Builder->CreateIntCast(Idx, Ty, false);
501       V = Builder->CreateLShr(ConstantInt::get(Ty, MagicBitvector), V);
502       V = Builder->CreateAnd(ConstantInt::get(Ty, 1), V);
503       return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));
504     }
505   }
506
507   return 0;
508 }
509
510
511 /// EvaluateGEPOffsetExpression - Return a value that can be used to compare
512 /// the *offset* implied by a GEP to zero.  For example, if we have &A[i], we
513 /// want to return 'i' for "icmp ne i, 0".  Note that, in general, indices can
514 /// be complex, and scales are involved.  The above expression would also be
515 /// legal to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32).
516 /// This later form is less amenable to optimization though, and we are allowed
517 /// to generate the first by knowing that pointer arithmetic doesn't overflow.
518 ///
519 /// If we can't emit an optimized form for this expression, this returns null.
520 ///
521 static Value *EvaluateGEPOffsetExpression(User *GEP, InstCombiner &IC) {
522   DataLayout &TD = *IC.getDataLayout();
523   gep_type_iterator GTI = gep_type_begin(GEP);
524
525   // Check to see if this gep only has a single variable index.  If so, and if
526   // any constant indices are a multiple of its scale, then we can compute this
527   // in terms of the scale of the variable index.  For example, if the GEP
528   // implies an offset of "12 + i*4", then we can codegen this as "3 + i",
529   // because the expression will cross zero at the same point.
530   unsigned i, e = GEP->getNumOperands();
531   int64_t Offset = 0;
532   for (i = 1; i != e; ++i, ++GTI) {
533     if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
534       // Compute the aggregate offset of constant indices.
535       if (CI->isZero()) continue;
536
537       // Handle a struct index, which adds its field offset to the pointer.
538       if (StructType *STy = dyn_cast<StructType>(*GTI)) {
539         Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
540       } else {
541         uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType());
542         Offset += Size*CI->getSExtValue();
543       }
544     } else {
545       // Found our variable index.
546       break;
547     }
548   }
549
550   // If there are no variable indices, we must have a constant offset, just
551   // evaluate it the general way.
552   if (i == e) return 0;
553
554   Value *VariableIdx = GEP->getOperand(i);
555   // Determine the scale factor of the variable element.  For example, this is
556   // 4 if the variable index is into an array of i32.
557   uint64_t VariableScale = TD.getTypeAllocSize(GTI.getIndexedType());
558
559   // Verify that there are no other variable indices.  If so, emit the hard way.
560   for (++i, ++GTI; i != e; ++i, ++GTI) {
561     ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i));
562     if (!CI) return 0;
563
564     // Compute the aggregate offset of constant indices.
565     if (CI->isZero()) continue;
566
567     // Handle a struct index, which adds its field offset to the pointer.
568     if (StructType *STy = dyn_cast<StructType>(*GTI)) {
569       Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
570     } else {
571       uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType());
572       Offset += Size*CI->getSExtValue();
573     }
574   }
575
576   // Okay, we know we have a single variable index, which must be a
577   // pointer/array/vector index.  If there is no offset, life is simple, return
578   // the index.
579   unsigned IntPtrWidth = TD.getPointerSizeInBits();
580   if (Offset == 0) {
581     // Cast to intptrty in case a truncation occurs.  If an extension is needed,
582     // we don't need to bother extending: the extension won't affect where the
583     // computation crosses zero.
584     if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth) {
585       Type *IntPtrTy = TD.getIntPtrType(VariableIdx->getContext());
586       VariableIdx = IC.Builder->CreateTrunc(VariableIdx, IntPtrTy);
587     }
588     return VariableIdx;
589   }
590
591   // Otherwise, there is an index.  The computation we will do will be modulo
592   // the pointer size, so get it.
593   uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth);
594
595   Offset &= PtrSizeMask;
596   VariableScale &= PtrSizeMask;
597
598   // To do this transformation, any constant index must be a multiple of the
599   // variable scale factor.  For example, we can evaluate "12 + 4*i" as "3 + i",
600   // but we can't evaluate "10 + 3*i" in terms of i.  Check that the offset is a
601   // multiple of the variable scale.
602   int64_t NewOffs = Offset / (int64_t)VariableScale;
603   if (Offset != NewOffs*(int64_t)VariableScale)
604     return 0;
605
606   // Okay, we can do this evaluation.  Start by converting the index to intptr.
607   Type *IntPtrTy = TD.getIntPtrType(VariableIdx->getContext());
608   if (VariableIdx->getType() != IntPtrTy)
609     VariableIdx = IC.Builder->CreateIntCast(VariableIdx, IntPtrTy,
610                                             true /*Signed*/);
611   Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs);
612   return IC.Builder->CreateAdd(VariableIdx, OffsetVal, "offset");
613 }
614
615 /// FoldGEPICmp - Fold comparisons between a GEP instruction and something
616 /// else.  At this point we know that the GEP is on the LHS of the comparison.
617 Instruction *InstCombiner::FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
618                                        ICmpInst::Predicate Cond,
619                                        Instruction &I) {
620   // Don't transform signed compares of GEPs into index compares. Even if the
621   // GEP is inbounds, the final add of the base pointer can have signed overflow
622   // and would change the result of the icmp.
623   // e.g. "&foo[0] <s &foo[1]" can't be folded to "true" because "foo" could be
624   // the maximum signed value for the pointer type.
625   if (ICmpInst::isSigned(Cond))
626     return 0;
627
628   // Look through bitcasts.
629   if (BitCastInst *BCI = dyn_cast<BitCastInst>(RHS))
630     RHS = BCI->getOperand(0);
631
632   Value *PtrBase = GEPLHS->getOperand(0);
633   if (TD && PtrBase == RHS && GEPLHS->isInBounds()) {
634     // ((gep Ptr, OFFSET) cmp Ptr)   ---> (OFFSET cmp 0).
635     // This transformation (ignoring the base and scales) is valid because we
636     // know pointers can't overflow since the gep is inbounds.  See if we can
637     // output an optimized form.
638     Value *Offset = EvaluateGEPOffsetExpression(GEPLHS, *this);
639
640     // If not, synthesize the offset the hard way.
641     if (Offset == 0)
642       Offset = EmitGEPOffset(GEPLHS);
643     return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset,
644                         Constant::getNullValue(Offset->getType()));
645   } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {
646     // If the base pointers are different, but the indices are the same, just
647     // compare the base pointer.
648     if (PtrBase != GEPRHS->getOperand(0)) {
649       bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands();
650       IndicesTheSame &= GEPLHS->getOperand(0)->getType() ==
651                         GEPRHS->getOperand(0)->getType();
652       if (IndicesTheSame)
653         for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
654           if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
655             IndicesTheSame = false;
656             break;
657           }
658
659       // If all indices are the same, just compare the base pointers.
660       if (IndicesTheSame)
661         return new ICmpInst(ICmpInst::getSignedPredicate(Cond),
662                             GEPLHS->getOperand(0), GEPRHS->getOperand(0));
663
664       // If we're comparing GEPs with two base pointers that only differ in type
665       // and both GEPs have only constant indices or just one use, then fold
666       // the compare with the adjusted indices.
667       if (TD && GEPLHS->isInBounds() && GEPRHS->isInBounds() &&
668           (GEPLHS->hasAllConstantIndices() || GEPLHS->hasOneUse()) &&
669           (GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) &&
670           PtrBase->stripPointerCasts() ==
671             GEPRHS->getOperand(0)->stripPointerCasts()) {
672         Value *Cmp = Builder->CreateICmp(ICmpInst::getSignedPredicate(Cond),
673                                          EmitGEPOffset(GEPLHS),
674                                          EmitGEPOffset(GEPRHS));
675         return ReplaceInstUsesWith(I, Cmp);
676       }
677
678       // Otherwise, the base pointers are different and the indices are
679       // different, bail out.
680       return 0;
681     }
682
683     // If one of the GEPs has all zero indices, recurse.
684     bool AllZeros = true;
685     for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
686       if (!isa<Constant>(GEPLHS->getOperand(i)) ||
687           !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) {
688         AllZeros = false;
689         break;
690       }
691     if (AllZeros)
692       return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0),
693                           ICmpInst::getSwappedPredicate(Cond), I);
694
695     // If the other GEP has all zero indices, recurse.
696     AllZeros = true;
697     for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
698       if (!isa<Constant>(GEPRHS->getOperand(i)) ||
699           !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) {
700         AllZeros = false;
701         break;
702       }
703     if (AllZeros)
704       return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I);
705
706     bool GEPsInBounds = GEPLHS->isInBounds() && GEPRHS->isInBounds();
707     if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) {
708       // If the GEPs only differ by one index, compare it.
709       unsigned NumDifferences = 0;  // Keep track of # differences.
710       unsigned DiffOperand = 0;     // The operand that differs.
711       for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
712         if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
713           if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() !=
714                    GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) {
715             // Irreconcilable differences.
716             NumDifferences = 2;
717             break;
718           } else {
719             if (NumDifferences++) break;
720             DiffOperand = i;
721           }
722         }
723
724       if (NumDifferences == 0)   // SAME GEP?
725         return ReplaceInstUsesWith(I, // No comparison is needed here.
726                                ConstantInt::get(Type::getInt1Ty(I.getContext()),
727                                              ICmpInst::isTrueWhenEqual(Cond)));
728
729       else if (NumDifferences == 1 && GEPsInBounds) {
730         Value *LHSV = GEPLHS->getOperand(DiffOperand);
731         Value *RHSV = GEPRHS->getOperand(DiffOperand);
732         // Make sure we do a signed comparison here.
733         return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV);
734       }
735     }
736
737     // Only lower this if the icmp is the only user of the GEP or if we expect
738     // the result to fold to a constant!
739     if (TD &&
740         GEPsInBounds &&
741         (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) &&
742         (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) {
743       // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2)  --->  (OFFSET1 cmp OFFSET2)
744       Value *L = EmitGEPOffset(GEPLHS);
745       Value *R = EmitGEPOffset(GEPRHS);
746       return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R);
747     }
748   }
749   return 0;
750 }
751
752 /// FoldICmpAddOpCst - Fold "icmp pred (X+CI), X".
753 Instruction *InstCombiner::FoldICmpAddOpCst(ICmpInst &ICI,
754                                             Value *X, ConstantInt *CI,
755                                             ICmpInst::Predicate Pred,
756                                             Value *TheAdd) {
757   // If we have X+0, exit early (simplifying logic below) and let it get folded
758   // elsewhere.   icmp X+0, X  -> icmp X, X
759   if (CI->isZero()) {
760     bool isTrue = ICmpInst::isTrueWhenEqual(Pred);
761     return ReplaceInstUsesWith(ICI, ConstantInt::get(ICI.getType(), isTrue));
762   }
763
764   // (X+4) == X -> false.
765   if (Pred == ICmpInst::ICMP_EQ)
766     return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(X->getContext()));
767
768   // (X+4) != X -> true.
769   if (Pred == ICmpInst::ICMP_NE)
770     return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(X->getContext()));
771
772   // From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0,
773   // so the values can never be equal.  Similarly for all other "or equals"
774   // operators.
775
776   // (X+1) <u X        --> X >u (MAXUINT-1)        --> X == 255
777   // (X+2) <u X        --> X >u (MAXUINT-2)        --> X > 253
778   // (X+MAXUINT) <u X  --> X >u (MAXUINT-MAXUINT)  --> X != 0
779   if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
780     Value *R =
781       ConstantExpr::getSub(ConstantInt::getAllOnesValue(CI->getType()), CI);
782     return new ICmpInst(ICmpInst::ICMP_UGT, X, R);
783   }
784
785   // (X+1) >u X        --> X <u (0-1)        --> X != 255
786   // (X+2) >u X        --> X <u (0-2)        --> X <u 254
787   // (X+MAXUINT) >u X  --> X <u (0-MAXUINT)  --> X <u 1  --> X == 0
788   if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
789     return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantExpr::getNeg(CI));
790
791   unsigned BitWidth = CI->getType()->getPrimitiveSizeInBits();
792   ConstantInt *SMax = ConstantInt::get(X->getContext(),
793                                        APInt::getSignedMaxValue(BitWidth));
794
795   // (X+ 1) <s X       --> X >s (MAXSINT-1)          --> X == 127
796   // (X+ 2) <s X       --> X >s (MAXSINT-2)          --> X >s 125
797   // (X+MAXSINT) <s X  --> X >s (MAXSINT-MAXSINT)    --> X >s 0
798   // (X+MINSINT) <s X  --> X >s (MAXSINT-MINSINT)    --> X >s -1
799   // (X+ -2) <s X      --> X >s (MAXSINT- -2)        --> X >s 126
800   // (X+ -1) <s X      --> X >s (MAXSINT- -1)        --> X != 127
801   if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
802     return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantExpr::getSub(SMax, CI));
803
804   // (X+ 1) >s X       --> X <s (MAXSINT-(1-1))       --> X != 127
805   // (X+ 2) >s X       --> X <s (MAXSINT-(2-1))       --> X <s 126
806   // (X+MAXSINT) >s X  --> X <s (MAXSINT-(MAXSINT-1)) --> X <s 1
807   // (X+MINSINT) >s X  --> X <s (MAXSINT-(MINSINT-1)) --> X <s -2
808   // (X+ -2) >s X      --> X <s (MAXSINT-(-2-1))      --> X <s -126
809   // (X+ -1) >s X      --> X <s (MAXSINT-(-1-1))      --> X == -128
810
811   assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE);
812   Constant *C = ConstantInt::get(X->getContext(), CI->getValue()-1);
813   return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantExpr::getSub(SMax, C));
814 }
815
816 /// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS
817 /// and CmpRHS are both known to be integer constants.
818 Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
819                                           ConstantInt *DivRHS) {
820   ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1));
821   const APInt &CmpRHSV = CmpRHS->getValue();
822
823   // FIXME: If the operand types don't match the type of the divide
824   // then don't attempt this transform. The code below doesn't have the
825   // logic to deal with a signed divide and an unsigned compare (and
826   // vice versa). This is because (x /s C1) <s C2  produces different
827   // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even
828   // (x /u C1) <u C2.  Simply casting the operands and result won't
829   // work. :(  The if statement below tests that condition and bails
830   // if it finds it.
831   bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv;
832   if (!ICI.isEquality() && DivIsSigned != ICI.isSigned())
833     return 0;
834   if (DivRHS->isZero())
835     return 0; // The ProdOV computation fails on divide by zero.
836   if (DivIsSigned && DivRHS->isAllOnesValue())
837     return 0; // The overflow computation also screws up here
838   if (DivRHS->isOne()) {
839     // This eliminates some funny cases with INT_MIN.
840     ICI.setOperand(0, DivI->getOperand(0));   // X/1 == X.
841     return &ICI;
842   }
843
844   // Compute Prod = CI * DivRHS. We are essentially solving an equation
845   // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and
846   // C2 (CI). By solving for X we can turn this into a range check
847   // instead of computing a divide.
848   Constant *Prod = ConstantExpr::getMul(CmpRHS, DivRHS);
849
850   // Determine if the product overflows by seeing if the product is
851   // not equal to the divide. Make sure we do the same kind of divide
852   // as in the LHS instruction that we're folding.
853   bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) :
854                  ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS;
855
856   // Get the ICmp opcode
857   ICmpInst::Predicate Pred = ICI.getPredicate();
858
859   /// If the division is known to be exact, then there is no remainder from the
860   /// divide, so the covered range size is unit, otherwise it is the divisor.
861   ConstantInt *RangeSize = DivI->isExact() ? getOne(Prod) : DivRHS;
862
863   // Figure out the interval that is being checked.  For example, a comparison
864   // like "X /u 5 == 0" is really checking that X is in the interval [0, 5).
865   // Compute this interval based on the constants involved and the signedness of
866   // the compare/divide.  This computes a half-open interval, keeping track of
867   // whether either value in the interval overflows.  After analysis each
868   // overflow variable is set to 0 if it's corresponding bound variable is valid
869   // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
870   int LoOverflow = 0, HiOverflow = 0;
871   Constant *LoBound = 0, *HiBound = 0;
872
873   if (!DivIsSigned) {  // udiv
874     // e.g. X/5 op 3  --> [15, 20)
875     LoBound = Prod;
876     HiOverflow = LoOverflow = ProdOV;
877     if (!HiOverflow) {
878       // If this is not an exact divide, then many values in the range collapse
879       // to the same result value.
880       HiOverflow = AddWithOverflow(HiBound, LoBound, RangeSize, false);
881     }
882
883   } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0.
884     if (CmpRHSV == 0) {       // (X / pos) op 0
885       // Can't overflow.  e.g.  X/2 op 0 --> [-1, 2)
886       LoBound = ConstantExpr::getNeg(SubOne(RangeSize));
887       HiBound = RangeSize;
888     } else if (CmpRHSV.isStrictlyPositive()) {   // (X / pos) op pos
889       LoBound = Prod;     // e.g.   X/5 op 3 --> [15, 20)
890       HiOverflow = LoOverflow = ProdOV;
891       if (!HiOverflow)
892         HiOverflow = AddWithOverflow(HiBound, Prod, RangeSize, true);
893     } else {                       // (X / pos) op neg
894       // e.g. X/5 op -3  --> [-15-4, -15+1) --> [-19, -14)
895       HiBound = AddOne(Prod);
896       LoOverflow = HiOverflow = ProdOV ? -1 : 0;
897       if (!LoOverflow) {
898         ConstantInt *DivNeg =cast<ConstantInt>(ConstantExpr::getNeg(RangeSize));
899         LoOverflow = AddWithOverflow(LoBound, HiBound, DivNeg, true) ? -1 : 0;
900       }
901     }
902   } else if (DivRHS->isNegative()) { // Divisor is < 0.
903     if (DivI->isExact())
904       RangeSize = cast<ConstantInt>(ConstantExpr::getNeg(RangeSize));
905     if (CmpRHSV == 0) {       // (X / neg) op 0
906       // e.g. X/-5 op 0  --> [-4, 5)
907       LoBound = AddOne(RangeSize);
908       HiBound = cast<ConstantInt>(ConstantExpr::getNeg(RangeSize));
909       if (HiBound == DivRHS) {     // -INTMIN = INTMIN
910         HiOverflow = 1;            // [INTMIN+1, overflow)
911         HiBound = 0;               // e.g. X/INTMIN = 0 --> X > INTMIN
912       }
913     } else if (CmpRHSV.isStrictlyPositive()) {   // (X / neg) op pos
914       // e.g. X/-5 op 3  --> [-19, -14)
915       HiBound = AddOne(Prod);
916       HiOverflow = LoOverflow = ProdOV ? -1 : 0;
917       if (!LoOverflow)
918         LoOverflow = AddWithOverflow(LoBound, HiBound, RangeSize, true) ? -1:0;
919     } else {                       // (X / neg) op neg
920       LoBound = Prod;       // e.g. X/-5 op -3  --> [15, 20)
921       LoOverflow = HiOverflow = ProdOV;
922       if (!HiOverflow)
923         HiOverflow = SubWithOverflow(HiBound, Prod, RangeSize, true);
924     }
925
926     // Dividing by a negative swaps the condition.  LT <-> GT
927     Pred = ICmpInst::getSwappedPredicate(Pred);
928   }
929
930   Value *X = DivI->getOperand(0);
931   switch (Pred) {
932   default: llvm_unreachable("Unhandled icmp opcode!");
933   case ICmpInst::ICMP_EQ:
934     if (LoOverflow && HiOverflow)
935       return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(ICI.getContext()));
936     if (HiOverflow)
937       return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
938                           ICmpInst::ICMP_UGE, X, LoBound);
939     if (LoOverflow)
940       return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
941                           ICmpInst::ICMP_ULT, X, HiBound);
942     return ReplaceInstUsesWith(ICI, InsertRangeTest(X, LoBound, HiBound,
943                                                     DivIsSigned, true));
944   case ICmpInst::ICMP_NE:
945     if (LoOverflow && HiOverflow)
946       return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(ICI.getContext()));
947     if (HiOverflow)
948       return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
949                           ICmpInst::ICMP_ULT, X, LoBound);
950     if (LoOverflow)
951       return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
952                           ICmpInst::ICMP_UGE, X, HiBound);
953     return ReplaceInstUsesWith(ICI, InsertRangeTest(X, LoBound, HiBound,
954                                                     DivIsSigned, false));
955   case ICmpInst::ICMP_ULT:
956   case ICmpInst::ICMP_SLT:
957     if (LoOverflow == +1)   // Low bound is greater than input range.
958       return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(ICI.getContext()));
959     if (LoOverflow == -1)   // Low bound is less than input range.
960       return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(ICI.getContext()));
961     return new ICmpInst(Pred, X, LoBound);
962   case ICmpInst::ICMP_UGT:
963   case ICmpInst::ICMP_SGT:
964     if (HiOverflow == +1)       // High bound greater than input range.
965       return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(ICI.getContext()));
966     if (HiOverflow == -1)       // High bound less than input range.
967       return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(ICI.getContext()));
968     if (Pred == ICmpInst::ICMP_UGT)
969       return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound);
970     return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound);
971   }
972 }
973
974 /// FoldICmpShrCst - Handle "icmp(([al]shr X, cst1), cst2)".
975 Instruction *InstCombiner::FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *Shr,
976                                           ConstantInt *ShAmt) {
977   const APInt &CmpRHSV = cast<ConstantInt>(ICI.getOperand(1))->getValue();
978
979   // Check that the shift amount is in range.  If not, don't perform
980   // undefined shifts.  When the shift is visited it will be
981   // simplified.
982   uint32_t TypeBits = CmpRHSV.getBitWidth();
983   uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
984   if (ShAmtVal >= TypeBits || ShAmtVal == 0)
985     return 0;
986
987   if (!ICI.isEquality()) {
988     // If we have an unsigned comparison and an ashr, we can't simplify this.
989     // Similarly for signed comparisons with lshr.
990     if (ICI.isSigned() != (Shr->getOpcode() == Instruction::AShr))
991       return 0;
992
993     // Otherwise, all lshr and most exact ashr's are equivalent to a udiv/sdiv
994     // by a power of 2.  Since we already have logic to simplify these,
995     // transform to div and then simplify the resultant comparison.
996     if (Shr->getOpcode() == Instruction::AShr &&
997         (!Shr->isExact() || ShAmtVal == TypeBits - 1))
998       return 0;
999
1000     // Revisit the shift (to delete it).
1001     Worklist.Add(Shr);
1002
1003     Constant *DivCst =
1004       ConstantInt::get(Shr->getType(), APInt::getOneBitSet(TypeBits, ShAmtVal));
1005
1006     Value *Tmp =
1007       Shr->getOpcode() == Instruction::AShr ?
1008       Builder->CreateSDiv(Shr->getOperand(0), DivCst, "", Shr->isExact()) :
1009       Builder->CreateUDiv(Shr->getOperand(0), DivCst, "", Shr->isExact());
1010
1011     ICI.setOperand(0, Tmp);
1012
1013     // If the builder folded the binop, just return it.
1014     BinaryOperator *TheDiv = dyn_cast<BinaryOperator>(Tmp);
1015     if (TheDiv == 0)
1016       return &ICI;
1017
1018     // Otherwise, fold this div/compare.
1019     assert(TheDiv->getOpcode() == Instruction::SDiv ||
1020            TheDiv->getOpcode() == Instruction::UDiv);
1021
1022     Instruction *Res = FoldICmpDivCst(ICI, TheDiv, cast<ConstantInt>(DivCst));
1023     assert(Res && "This div/cst should have folded!");
1024     return Res;
1025   }
1026
1027
1028   // If we are comparing against bits always shifted out, the
1029   // comparison cannot succeed.
1030   APInt Comp = CmpRHSV << ShAmtVal;
1031   ConstantInt *ShiftedCmpRHS = ConstantInt::get(ICI.getContext(), Comp);
1032   if (Shr->getOpcode() == Instruction::LShr)
1033     Comp = Comp.lshr(ShAmtVal);
1034   else
1035     Comp = Comp.ashr(ShAmtVal);
1036
1037   if (Comp != CmpRHSV) { // Comparing against a bit that we know is zero.
1038     bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
1039     Constant *Cst = ConstantInt::get(Type::getInt1Ty(ICI.getContext()),
1040                                      IsICMP_NE);
1041     return ReplaceInstUsesWith(ICI, Cst);
1042   }
1043
1044   // Otherwise, check to see if the bits shifted out are known to be zero.
1045   // If so, we can compare against the unshifted value:
1046   //  (X & 4) >> 1 == 2  --> (X & 4) == 4.
1047   if (Shr->hasOneUse() && Shr->isExact())
1048     return new ICmpInst(ICI.getPredicate(), Shr->getOperand(0), ShiftedCmpRHS);
1049
1050   if (Shr->hasOneUse()) {
1051     // Otherwise strength reduce the shift into an and.
1052     APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
1053     Constant *Mask = ConstantInt::get(ICI.getContext(), Val);
1054
1055     Value *And = Builder->CreateAnd(Shr->getOperand(0),
1056                                     Mask, Shr->getName()+".mask");
1057     return new ICmpInst(ICI.getPredicate(), And, ShiftedCmpRHS);
1058   }
1059   return 0;
1060 }
1061
1062
1063 /// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)".
1064 ///
1065 Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
1066                                                           Instruction *LHSI,
1067                                                           ConstantInt *RHS) {
1068   const APInt &RHSV = RHS->getValue();
1069
1070   switch (LHSI->getOpcode()) {
1071   case Instruction::Trunc:
1072     if (ICI.isEquality() && LHSI->hasOneUse()) {
1073       // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
1074       // of the high bits truncated out of x are known.
1075       unsigned DstBits = LHSI->getType()->getPrimitiveSizeInBits(),
1076              SrcBits = LHSI->getOperand(0)->getType()->getPrimitiveSizeInBits();
1077       APInt KnownZero(SrcBits, 0), KnownOne(SrcBits, 0);
1078       ComputeMaskedBits(LHSI->getOperand(0), KnownZero, KnownOne);
1079
1080       // If all the high bits are known, we can do this xform.
1081       if ((KnownZero|KnownOne).countLeadingOnes() >= SrcBits-DstBits) {
1082         // Pull in the high bits from known-ones set.
1083         APInt NewRHS = RHS->getValue().zext(SrcBits);
1084         NewRHS |= KnownOne & APInt::getHighBitsSet(SrcBits, SrcBits-DstBits);
1085         return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
1086                             ConstantInt::get(ICI.getContext(), NewRHS));
1087       }
1088     }
1089     break;
1090
1091   case Instruction::Xor:         // (icmp pred (xor X, XorCST), CI)
1092     if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) {
1093       // If this is a comparison that tests the signbit (X < 0) or (x > -1),
1094       // fold the xor.
1095       if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) ||
1096           (ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) {
1097         Value *CompareVal = LHSI->getOperand(0);
1098
1099         // If the sign bit of the XorCST is not set, there is no change to
1100         // the operation, just stop using the Xor.
1101         if (!XorCST->isNegative()) {
1102           ICI.setOperand(0, CompareVal);
1103           Worklist.Add(LHSI);
1104           return &ICI;
1105         }
1106
1107         // Was the old condition true if the operand is positive?
1108         bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT;
1109
1110         // If so, the new one isn't.
1111         isTrueIfPositive ^= true;
1112
1113         if (isTrueIfPositive)
1114           return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal,
1115                               SubOne(RHS));
1116         else
1117           return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal,
1118                               AddOne(RHS));
1119       }
1120
1121       if (LHSI->hasOneUse()) {
1122         // (icmp u/s (xor A SignBit), C) -> (icmp s/u A, (xor C SignBit))
1123         if (!ICI.isEquality() && XorCST->getValue().isSignBit()) {
1124           const APInt &SignBit = XorCST->getValue();
1125           ICmpInst::Predicate Pred = ICI.isSigned()
1126                                          ? ICI.getUnsignedPredicate()
1127                                          : ICI.getSignedPredicate();
1128           return new ICmpInst(Pred, LHSI->getOperand(0),
1129                               ConstantInt::get(ICI.getContext(),
1130                                                RHSV ^ SignBit));
1131         }
1132
1133         // (icmp u/s (xor A ~SignBit), C) -> (icmp s/u (xor C ~SignBit), A)
1134         if (!ICI.isEquality() && XorCST->isMaxValue(true)) {
1135           const APInt &NotSignBit = XorCST->getValue();
1136           ICmpInst::Predicate Pred = ICI.isSigned()
1137                                          ? ICI.getUnsignedPredicate()
1138                                          : ICI.getSignedPredicate();
1139           Pred = ICI.getSwappedPredicate(Pred);
1140           return new ICmpInst(Pred, LHSI->getOperand(0),
1141                               ConstantInt::get(ICI.getContext(),
1142                                                RHSV ^ NotSignBit));
1143         }
1144       }
1145     }
1146     break;
1147   case Instruction::And:         // (icmp pred (and X, AndCST), RHS)
1148     if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) &&
1149         LHSI->getOperand(0)->hasOneUse()) {
1150       ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1));
1151
1152       // If the LHS is an AND of a truncating cast, we can widen the
1153       // and/compare to be the input width without changing the value
1154       // produced, eliminating a cast.
1155       if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) {
1156         // We can do this transformation if either the AND constant does not
1157         // have its sign bit set or if it is an equality comparison.
1158         // Extending a relational comparison when we're checking the sign
1159         // bit would not work.
1160         if (ICI.isEquality() ||
1161             (!AndCST->isNegative() && RHSV.isNonNegative())) {
1162           Value *NewAnd =
1163             Builder->CreateAnd(Cast->getOperand(0),
1164                                ConstantExpr::getZExt(AndCST, Cast->getSrcTy()));
1165           NewAnd->takeName(LHSI);
1166           return new ICmpInst(ICI.getPredicate(), NewAnd,
1167                               ConstantExpr::getZExt(RHS, Cast->getSrcTy()));
1168         }
1169       }
1170
1171       // If the LHS is an AND of a zext, and we have an equality compare, we can
1172       // shrink the and/compare to the smaller type, eliminating the cast.
1173       if (ZExtInst *Cast = dyn_cast<ZExtInst>(LHSI->getOperand(0))) {
1174         IntegerType *Ty = cast<IntegerType>(Cast->getSrcTy());
1175         // Make sure we don't compare the upper bits, SimplifyDemandedBits
1176         // should fold the icmp to true/false in that case.
1177         if (ICI.isEquality() && RHSV.getActiveBits() <= Ty->getBitWidth()) {
1178           Value *NewAnd =
1179             Builder->CreateAnd(Cast->getOperand(0),
1180                                ConstantExpr::getTrunc(AndCST, Ty));
1181           NewAnd->takeName(LHSI);
1182           return new ICmpInst(ICI.getPredicate(), NewAnd,
1183                               ConstantExpr::getTrunc(RHS, Ty));
1184         }
1185       }
1186
1187       // If this is: (X >> C1) & C2 != C3 (where any shift and any compare
1188       // could exist), turn it into (X & (C2 << C1)) != (C3 << C1).  This
1189       // happens a LOT in code produced by the C front-end, for bitfield
1190       // access.
1191       BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0));
1192       if (Shift && !Shift->isShift())
1193         Shift = 0;
1194
1195       ConstantInt *ShAmt;
1196       ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0;
1197       Type *Ty = Shift ? Shift->getType() : 0;  // Type of the shift.
1198       Type *AndTy = AndCST->getType();          // Type of the and.
1199
1200       // We can fold this as long as we can't shift unknown bits
1201       // into the mask.  This can only happen with signed shift
1202       // rights, as they sign-extend.
1203       if (ShAmt) {
1204         bool CanFold = Shift->isLogicalShift();
1205         if (!CanFold) {
1206           // To test for the bad case of the signed shr, see if any
1207           // of the bits shifted in could be tested after the mask.
1208           uint32_t TyBits = Ty->getPrimitiveSizeInBits();
1209           int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits);
1210
1211           uint32_t BitWidth = AndTy->getPrimitiveSizeInBits();
1212           if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) &
1213                AndCST->getValue()) == 0)
1214             CanFold = true;
1215         }
1216
1217         if (CanFold) {
1218           Constant *NewCst;
1219           if (Shift->getOpcode() == Instruction::Shl)
1220             NewCst = ConstantExpr::getLShr(RHS, ShAmt);
1221           else
1222             NewCst = ConstantExpr::getShl(RHS, ShAmt);
1223
1224           // Check to see if we are shifting out any of the bits being
1225           // compared.
1226           if (ConstantExpr::get(Shift->getOpcode(),
1227                                        NewCst, ShAmt) != RHS) {
1228             // If we shifted bits out, the fold is not going to work out.
1229             // As a special case, check to see if this means that the
1230             // result is always true or false now.
1231             if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
1232               return ReplaceInstUsesWith(ICI,
1233                                        ConstantInt::getFalse(ICI.getContext()));
1234             if (ICI.getPredicate() == ICmpInst::ICMP_NE)
1235               return ReplaceInstUsesWith(ICI,
1236                                        ConstantInt::getTrue(ICI.getContext()));
1237           } else {
1238             ICI.setOperand(1, NewCst);
1239             Constant *NewAndCST;
1240             if (Shift->getOpcode() == Instruction::Shl)
1241               NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt);
1242             else
1243               NewAndCST = ConstantExpr::getShl(AndCST, ShAmt);
1244             LHSI->setOperand(1, NewAndCST);
1245             LHSI->setOperand(0, Shift->getOperand(0));
1246             Worklist.Add(Shift); // Shift is dead.
1247             return &ICI;
1248           }
1249         }
1250       }
1251
1252       // Turn ((X >> Y) & C) == 0  into  (X & (C << Y)) == 0.  The later is
1253       // preferable because it allows the C<<Y expression to be hoisted out
1254       // of a loop if Y is invariant and X is not.
1255       if (Shift && Shift->hasOneUse() && RHSV == 0 &&
1256           ICI.isEquality() && !Shift->isArithmeticShift() &&
1257           !isa<Constant>(Shift->getOperand(0))) {
1258         // Compute C << Y.
1259         Value *NS;
1260         if (Shift->getOpcode() == Instruction::LShr) {
1261           NS = Builder->CreateShl(AndCST, Shift->getOperand(1));
1262         } else {
1263           // Insert a logical shift.
1264           NS = Builder->CreateLShr(AndCST, Shift->getOperand(1));
1265         }
1266
1267         // Compute X & (C << Y).
1268         Value *NewAnd =
1269           Builder->CreateAnd(Shift->getOperand(0), NS, LHSI->getName());
1270
1271         ICI.setOperand(0, NewAnd);
1272         return &ICI;
1273       }
1274
1275       // Replace ((X & AndCST) > RHSV) with ((X & AndCST) != 0), if any
1276       // bit set in (X & AndCST) will produce a result greater than RHSV.
1277       if (ICI.getPredicate() == ICmpInst::ICMP_UGT) {
1278         unsigned NTZ = AndCST->getValue().countTrailingZeros();
1279         if ((NTZ < AndCST->getBitWidth()) &&
1280             APInt::getOneBitSet(AndCST->getBitWidth(), NTZ).ugt(RHSV))
1281           return new ICmpInst(ICmpInst::ICMP_NE, LHSI,
1282                               Constant::getNullValue(RHS->getType()));
1283       }
1284     }
1285
1286     // Try to optimize things like "A[i]&42 == 0" to index computations.
1287     if (LoadInst *LI = dyn_cast<LoadInst>(LHSI->getOperand(0))) {
1288       if (GetElementPtrInst *GEP =
1289           dyn_cast<GetElementPtrInst>(LI->getOperand(0)))
1290         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
1291           if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
1292               !LI->isVolatile() && isa<ConstantInt>(LHSI->getOperand(1))) {
1293             ConstantInt *C = cast<ConstantInt>(LHSI->getOperand(1));
1294             if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV,ICI, C))
1295               return Res;
1296           }
1297     }
1298     break;
1299
1300   case Instruction::Or: {
1301     if (!ICI.isEquality() || !RHS->isNullValue() || !LHSI->hasOneUse())
1302       break;
1303     Value *P, *Q;
1304     if (match(LHSI, m_Or(m_PtrToInt(m_Value(P)), m_PtrToInt(m_Value(Q))))) {
1305       // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0
1306       // -> and (icmp eq P, null), (icmp eq Q, null).
1307       Value *ICIP = Builder->CreateICmp(ICI.getPredicate(), P,
1308                                         Constant::getNullValue(P->getType()));
1309       Value *ICIQ = Builder->CreateICmp(ICI.getPredicate(), Q,
1310                                         Constant::getNullValue(Q->getType()));
1311       Instruction *Op;
1312       if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
1313         Op = BinaryOperator::CreateAnd(ICIP, ICIQ);
1314       else
1315         Op = BinaryOperator::CreateOr(ICIP, ICIQ);
1316       return Op;
1317     }
1318     break;
1319   }
1320
1321   case Instruction::Mul: {       // (icmp pred (mul X, Val), CI)
1322     ConstantInt *Val = dyn_cast<ConstantInt>(LHSI->getOperand(1));
1323     if (!Val) break;
1324
1325     if (!ICI.isEquality()) {
1326       // If this is a signed comparison to 0 and the mul is sign preserving,
1327       // use the mul LHS operand instead.
1328       ICmpInst::Predicate pred = ICI.getPredicate();
1329       if (isSignTest(pred, RHS) && !Val->isZero() &&
1330           cast<BinaryOperator>(LHSI)->hasNoSignedWrap())
1331           return new ICmpInst(Val->isNegative() ?
1332                                   ICmpInst::getSwappedPredicate(pred) : pred,
1333                               LHSI->getOperand(0),
1334                               Constant::getNullValue(RHS->getType()));
1335     }
1336
1337     break;
1338   }
1339
1340   case Instruction::Shl: {       // (icmp pred (shl X, ShAmt), CI)
1341     ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1));
1342     if (!ShAmt) break;
1343
1344     uint32_t TypeBits = RHSV.getBitWidth();
1345
1346     // Check that the shift amount is in range.  If not, don't perform
1347     // undefined shifts.  When the shift is visited it will be
1348     // simplified.
1349     if (ShAmt->uge(TypeBits))
1350       break;
1351
1352     if (ICI.isEquality()) {
1353       // If we are comparing against bits always shifted out, the
1354       // comparison cannot succeed.
1355       Constant *Comp =
1356         ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt),
1357                                                                  ShAmt);
1358       if (Comp != RHS) {// Comparing against a bit that we know is zero.
1359         bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
1360         Constant *Cst =
1361           ConstantInt::get(Type::getInt1Ty(ICI.getContext()), IsICMP_NE);
1362         return ReplaceInstUsesWith(ICI, Cst);
1363       }
1364
1365       // If the shift is NUW, then it is just shifting out zeros, no need for an
1366       // AND.
1367       if (cast<BinaryOperator>(LHSI)->hasNoUnsignedWrap())
1368         return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
1369                             ConstantExpr::getLShr(RHS, ShAmt));
1370
1371       // If the shift is NSW and we compare to 0, then it is just shifting out
1372       // sign bits, no need for an AND either.
1373       if (cast<BinaryOperator>(LHSI)->hasNoSignedWrap() && RHSV == 0)
1374         return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
1375                             ConstantExpr::getLShr(RHS, ShAmt));
1376
1377       if (LHSI->hasOneUse()) {
1378         // Otherwise strength reduce the shift into an and.
1379         uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
1380         Constant *Mask =
1381           ConstantInt::get(ICI.getContext(), APInt::getLowBitsSet(TypeBits,
1382                                                        TypeBits-ShAmtVal));
1383
1384         Value *And =
1385           Builder->CreateAnd(LHSI->getOperand(0),Mask, LHSI->getName()+".mask");
1386         return new ICmpInst(ICI.getPredicate(), And,
1387                             ConstantExpr::getLShr(RHS, ShAmt));
1388       }
1389     }
1390
1391     // If this is a signed comparison to 0 and the shift is sign preserving,
1392     // use the shift LHS operand instead.
1393     ICmpInst::Predicate pred = ICI.getPredicate();
1394     if (isSignTest(pred, RHS) &&
1395         cast<BinaryOperator>(LHSI)->hasNoSignedWrap())
1396       return new ICmpInst(pred,
1397                           LHSI->getOperand(0),
1398                           Constant::getNullValue(RHS->getType()));
1399
1400     // Otherwise, if this is a comparison of the sign bit, simplify to and/test.
1401     bool TrueIfSigned = false;
1402     if (LHSI->hasOneUse() &&
1403         isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) {
1404       // (X << 31) <s 0  --> (X&1) != 0
1405       Constant *Mask = ConstantInt::get(LHSI->getOperand(0)->getType(),
1406                                         APInt::getOneBitSet(TypeBits,
1407                                             TypeBits-ShAmt->getZExtValue()-1));
1408       Value *And =
1409         Builder->CreateAnd(LHSI->getOperand(0), Mask, LHSI->getName()+".mask");
1410       return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
1411                           And, Constant::getNullValue(And->getType()));
1412     }
1413
1414     // Transform (icmp pred iM (shl iM %v, N), CI)
1415     // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (CI>>N))
1416     // Transform the shl to a trunc if (trunc (CI>>N)) has no loss and M-N.
1417     // This enables to get rid of the shift in favor of a trunc which can be
1418     // free on the target. It has the additional benefit of comparing to a
1419     // smaller constant, which will be target friendly.
1420     unsigned Amt = ShAmt->getLimitedValue(TypeBits-1);
1421     if (LHSI->hasOneUse() &&
1422         Amt != 0 && RHSV.countTrailingZeros() >= Amt) {
1423       Type *NTy = IntegerType::get(ICI.getContext(), TypeBits - Amt);
1424       Constant *NCI = ConstantExpr::getTrunc(
1425                         ConstantExpr::getAShr(RHS,
1426                           ConstantInt::get(RHS->getType(), Amt)),
1427                         NTy);
1428       return new ICmpInst(ICI.getPredicate(),
1429                           Builder->CreateTrunc(LHSI->getOperand(0), NTy),
1430                           NCI);
1431     }
1432
1433     break;
1434   }
1435
1436   case Instruction::LShr:         // (icmp pred (shr X, ShAmt), CI)
1437   case Instruction::AShr: {
1438     // Handle equality comparisons of shift-by-constant.
1439     BinaryOperator *BO = cast<BinaryOperator>(LHSI);
1440     if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1))) {
1441       if (Instruction *Res = FoldICmpShrCst(ICI, BO, ShAmt))
1442         return Res;
1443     }
1444
1445     // Handle exact shr's.
1446     if (ICI.isEquality() && BO->isExact() && BO->hasOneUse()) {
1447       if (RHSV.isMinValue())
1448         return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), RHS);
1449     }
1450     break;
1451   }
1452
1453   case Instruction::SDiv:
1454   case Instruction::UDiv:
1455     // Fold: icmp pred ([us]div X, C1), C2 -> range test
1456     // Fold this div into the comparison, producing a range check.
1457     // Determine, based on the divide type, what the range is being
1458     // checked.  If there is an overflow on the low or high side, remember
1459     // it, otherwise compute the range [low, hi) bounding the new value.
1460     // See: InsertRangeTest above for the kinds of replacements possible.
1461     if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1)))
1462       if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI),
1463                                           DivRHS))
1464         return R;
1465     break;
1466
1467   case Instruction::Add:
1468     // Fold: icmp pred (add X, C1), C2
1469     if (!ICI.isEquality()) {
1470       ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1));
1471       if (!LHSC) break;
1472       const APInt &LHSV = LHSC->getValue();
1473
1474       ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV)
1475                             .subtract(LHSV);
1476
1477       if (ICI.isSigned()) {
1478         if (CR.getLower().isSignBit()) {
1479           return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0),
1480                               ConstantInt::get(ICI.getContext(),CR.getUpper()));
1481         } else if (CR.getUpper().isSignBit()) {
1482           return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0),
1483                               ConstantInt::get(ICI.getContext(),CR.getLower()));
1484         }
1485       } else {
1486         if (CR.getLower().isMinValue()) {
1487           return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0),
1488                               ConstantInt::get(ICI.getContext(),CR.getUpper()));
1489         } else if (CR.getUpper().isMinValue()) {
1490           return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0),
1491                               ConstantInt::get(ICI.getContext(),CR.getLower()));
1492         }
1493       }
1494     }
1495     break;
1496   }
1497
1498   // Simplify icmp_eq and icmp_ne instructions with integer constant RHS.
1499   if (ICI.isEquality()) {
1500     bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
1501
1502     // If the first operand is (add|sub|and|or|xor|rem) with a constant, and
1503     // the second operand is a constant, simplify a bit.
1504     if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) {
1505       switch (BO->getOpcode()) {
1506       case Instruction::SRem:
1507         // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
1508         if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){
1509           const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue();
1510           if (V.sgt(1) && V.isPowerOf2()) {
1511             Value *NewRem =
1512               Builder->CreateURem(BO->getOperand(0), BO->getOperand(1),
1513                                   BO->getName());
1514             return new ICmpInst(ICI.getPredicate(), NewRem,
1515                                 Constant::getNullValue(BO->getType()));
1516           }
1517         }
1518         break;
1519       case Instruction::Add:
1520         // Replace ((add A, B) != C) with (A != C-B) if B & C are constants.
1521         if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) {
1522           if (BO->hasOneUse())
1523             return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
1524                                 ConstantExpr::getSub(RHS, BOp1C));
1525         } else if (RHSV == 0) {
1526           // Replace ((add A, B) != 0) with (A != -B) if A or B is
1527           // efficiently invertible, or if the add has just this one use.
1528           Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
1529
1530           if (Value *NegVal = dyn_castNegVal(BOp1))
1531             return new ICmpInst(ICI.getPredicate(), BOp0, NegVal);
1532           if (Value *NegVal = dyn_castNegVal(BOp0))
1533             return new ICmpInst(ICI.getPredicate(), NegVal, BOp1);
1534           if (BO->hasOneUse()) {
1535             Value *Neg = Builder->CreateNeg(BOp1);
1536             Neg->takeName(BO);
1537             return new ICmpInst(ICI.getPredicate(), BOp0, Neg);
1538           }
1539         }
1540         break;
1541       case Instruction::Xor:
1542         // For the xor case, we can xor two constants together, eliminating
1543         // the explicit xor.
1544         if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) {
1545           return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
1546                               ConstantExpr::getXor(RHS, BOC));
1547         } else if (RHSV == 0) {
1548           // Replace ((xor A, B) != 0) with (A != B)
1549           return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
1550                               BO->getOperand(1));
1551         }
1552         break;
1553       case Instruction::Sub:
1554         // Replace ((sub A, B) != C) with (B != A-C) if A & C are constants.
1555         if (ConstantInt *BOp0C = dyn_cast<ConstantInt>(BO->getOperand(0))) {
1556           if (BO->hasOneUse())
1557             return new ICmpInst(ICI.getPredicate(), BO->getOperand(1),
1558                                 ConstantExpr::getSub(BOp0C, RHS));
1559         } else if (RHSV == 0) {
1560           // Replace ((sub A, B) != 0) with (A != B)
1561           return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
1562                               BO->getOperand(1));
1563         }
1564         break;
1565       case Instruction::Or:
1566         // If bits are being or'd in that are not present in the constant we
1567         // are comparing against, then the comparison could never succeed!
1568         if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
1569           Constant *NotCI = ConstantExpr::getNot(RHS);
1570           if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue())
1571             return ReplaceInstUsesWith(ICI,
1572                              ConstantInt::get(Type::getInt1Ty(ICI.getContext()),
1573                                        isICMP_NE));
1574         }
1575         break;
1576
1577       case Instruction::And:
1578         if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
1579           // If bits are being compared against that are and'd out, then the
1580           // comparison can never succeed!
1581           if ((RHSV & ~BOC->getValue()) != 0)
1582             return ReplaceInstUsesWith(ICI,
1583                              ConstantInt::get(Type::getInt1Ty(ICI.getContext()),
1584                                        isICMP_NE));
1585
1586           // If we have ((X & C) == C), turn it into ((X & C) != 0).
1587           if (RHS == BOC && RHSV.isPowerOf2())
1588             return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ :
1589                                 ICmpInst::ICMP_NE, LHSI,
1590                                 Constant::getNullValue(RHS->getType()));
1591
1592           // Don't perform the following transforms if the AND has multiple uses
1593           if (!BO->hasOneUse())
1594             break;
1595
1596           // Replace (and X, (1 << size(X)-1) != 0) with x s< 0
1597           if (BOC->getValue().isSignBit()) {
1598             Value *X = BO->getOperand(0);
1599             Constant *Zero = Constant::getNullValue(X->getType());
1600             ICmpInst::Predicate pred = isICMP_NE ?
1601               ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
1602             return new ICmpInst(pred, X, Zero);
1603           }
1604
1605           // ((X & ~7) == 0) --> X < 8
1606           if (RHSV == 0 && isHighOnes(BOC)) {
1607             Value *X = BO->getOperand(0);
1608             Constant *NegX = ConstantExpr::getNeg(BOC);
1609             ICmpInst::Predicate pred = isICMP_NE ?
1610               ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
1611             return new ICmpInst(pred, X, NegX);
1612           }
1613         }
1614         break;
1615       case Instruction::Mul:
1616         if (RHSV == 0) {
1617           if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
1618             // The trivial case (mul X, 0) is handled by InstSimplify
1619             // General case : (mul X, C) != 0 iff X != 0
1620             //                (mul X, C) == 0 iff X == 0
1621             if (!BOC->isZero())
1622               return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
1623                                   Constant::getNullValue(RHS->getType()));
1624           }
1625         }
1626         break;
1627       default: break;
1628       }
1629     } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) {
1630       // Handle icmp {eq|ne} <intrinsic>, intcst.
1631       switch (II->getIntrinsicID()) {
1632       case Intrinsic::bswap:
1633         Worklist.Add(II);
1634         ICI.setOperand(0, II->getArgOperand(0));
1635         ICI.setOperand(1, ConstantInt::get(II->getContext(), RHSV.byteSwap()));
1636         return &ICI;
1637       case Intrinsic::ctlz:
1638       case Intrinsic::cttz:
1639         // ctz(A) == bitwidth(a)  ->  A == 0 and likewise for !=
1640         if (RHSV == RHS->getType()->getBitWidth()) {
1641           Worklist.Add(II);
1642           ICI.setOperand(0, II->getArgOperand(0));
1643           ICI.setOperand(1, ConstantInt::get(RHS->getType(), 0));
1644           return &ICI;
1645         }
1646         break;
1647       case Intrinsic::ctpop:
1648         // popcount(A) == 0  ->  A == 0 and likewise for !=
1649         if (RHS->isZero()) {
1650           Worklist.Add(II);
1651           ICI.setOperand(0, II->getArgOperand(0));
1652           ICI.setOperand(1, RHS);
1653           return &ICI;
1654         }
1655         break;
1656       default:
1657         break;
1658       }
1659     }
1660   }
1661   return 0;
1662 }
1663
1664 /// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst).
1665 /// We only handle extending casts so far.
1666 ///
1667 Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) {
1668   const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0));
1669   Value *LHSCIOp        = LHSCI->getOperand(0);
1670   Type *SrcTy     = LHSCIOp->getType();
1671   Type *DestTy    = LHSCI->getType();
1672   Value *RHSCIOp;
1673
1674   // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
1675   // integer type is the same size as the pointer type.
1676   if (TD && LHSCI->getOpcode() == Instruction::PtrToInt &&
1677       TD->getPointerSizeInBits() ==
1678          cast<IntegerType>(DestTy)->getBitWidth()) {
1679     Value *RHSOp = 0;
1680     if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) {
1681       RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy);
1682     } else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) {
1683       RHSOp = RHSC->getOperand(0);
1684       // If the pointer types don't match, insert a bitcast.
1685       if (LHSCIOp->getType() != RHSOp->getType())
1686         RHSOp = Builder->CreateBitCast(RHSOp, LHSCIOp->getType());
1687     }
1688
1689     if (RHSOp)
1690       return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp);
1691   }
1692
1693   // The code below only handles extension cast instructions, so far.
1694   // Enforce this.
1695   if (LHSCI->getOpcode() != Instruction::ZExt &&
1696       LHSCI->getOpcode() != Instruction::SExt)
1697     return 0;
1698
1699   bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt;
1700   bool isSignedCmp = ICI.isSigned();
1701
1702   if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) {
1703     // Not an extension from the same type?
1704     RHSCIOp = CI->getOperand(0);
1705     if (RHSCIOp->getType() != LHSCIOp->getType())
1706       return 0;
1707
1708     // If the signedness of the two casts doesn't agree (i.e. one is a sext
1709     // and the other is a zext), then we can't handle this.
1710     if (CI->getOpcode() != LHSCI->getOpcode())
1711       return 0;
1712
1713     // Deal with equality cases early.
1714     if (ICI.isEquality())
1715       return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
1716
1717     // A signed comparison of sign extended values simplifies into a
1718     // signed comparison.
1719     if (isSignedCmp && isSignedExt)
1720       return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
1721
1722     // The other three cases all fold into an unsigned comparison.
1723     return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp);
1724   }
1725
1726   // If we aren't dealing with a constant on the RHS, exit early
1727   ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1));
1728   if (!CI)
1729     return 0;
1730
1731   // Compute the constant that would happen if we truncated to SrcTy then
1732   // reextended to DestTy.
1733   Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy);
1734   Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(),
1735                                                 Res1, DestTy);
1736
1737   // If the re-extended constant didn't change...
1738   if (Res2 == CI) {
1739     // Deal with equality cases early.
1740     if (ICI.isEquality())
1741       return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1);
1742
1743     // A signed comparison of sign extended values simplifies into a
1744     // signed comparison.
1745     if (isSignedExt && isSignedCmp)
1746       return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1);
1747
1748     // The other three cases all fold into an unsigned comparison.
1749     return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, Res1);
1750   }
1751
1752   // The re-extended constant changed so the constant cannot be represented
1753   // in the shorter type. Consequently, we cannot emit a simple comparison.
1754   // All the cases that fold to true or false will have already been handled
1755   // by SimplifyICmpInst, so only deal with the tricky case.
1756
1757   if (isSignedCmp || !isSignedExt)
1758     return 0;
1759
1760   // Evaluate the comparison for LT (we invert for GT below). LE and GE cases
1761   // should have been folded away previously and not enter in here.
1762
1763   // We're performing an unsigned comp with a sign extended value.
1764   // This is true if the input is >= 0. [aka >s -1]
1765   Constant *NegOne = Constant::getAllOnesValue(SrcTy);
1766   Value *Result = Builder->CreateICmpSGT(LHSCIOp, NegOne, ICI.getName());
1767
1768   // Finally, return the value computed.
1769   if (ICI.getPredicate() == ICmpInst::ICMP_ULT)
1770     return ReplaceInstUsesWith(ICI, Result);
1771
1772   assert(ICI.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");
1773   return BinaryOperator::CreateNot(Result);
1774 }
1775
1776 /// ProcessUGT_ADDCST_ADD - The caller has matched a pattern of the form:
1777 ///   I = icmp ugt (add (add A, B), CI2), CI1
1778 /// If this is of the form:
1779 ///   sum = a + b
1780 ///   if (sum+128 >u 255)
1781 /// Then replace it with llvm.sadd.with.overflow.i8.
1782 ///
1783 static Instruction *ProcessUGT_ADDCST_ADD(ICmpInst &I, Value *A, Value *B,
1784                                           ConstantInt *CI2, ConstantInt *CI1,
1785                                           InstCombiner &IC) {
1786   // The transformation we're trying to do here is to transform this into an
1787   // llvm.sadd.with.overflow.  To do this, we have to replace the original add
1788   // with a narrower add, and discard the add-with-constant that is part of the
1789   // range check (if we can't eliminate it, this isn't profitable).
1790
1791   // In order to eliminate the add-with-constant, the compare can be its only
1792   // use.
1793   Instruction *AddWithCst = cast<Instruction>(I.getOperand(0));
1794   if (!AddWithCst->hasOneUse()) return 0;
1795
1796   // If CI2 is 2^7, 2^15, 2^31, then it might be an sadd.with.overflow.
1797   if (!CI2->getValue().isPowerOf2()) return 0;
1798   unsigned NewWidth = CI2->getValue().countTrailingZeros();
1799   if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31) return 0;
1800
1801   // The width of the new add formed is 1 more than the bias.
1802   ++NewWidth;
1803
1804   // Check to see that CI1 is an all-ones value with NewWidth bits.
1805   if (CI1->getBitWidth() == NewWidth ||
1806       CI1->getValue() != APInt::getLowBitsSet(CI1->getBitWidth(), NewWidth))
1807     return 0;
1808
1809   // This is only really a signed overflow check if the inputs have been
1810   // sign-extended; check for that condition. For example, if CI2 is 2^31 and
1811   // the operands of the add are 64 bits wide, we need at least 33 sign bits.
1812   unsigned NeededSignBits = CI1->getBitWidth() - NewWidth + 1;
1813   if (IC.ComputeNumSignBits(A) < NeededSignBits ||
1814       IC.ComputeNumSignBits(B) < NeededSignBits)
1815     return 0;
1816
1817   // In order to replace the original add with a narrower
1818   // llvm.sadd.with.overflow, the only uses allowed are the add-with-constant
1819   // and truncates that discard the high bits of the add.  Verify that this is
1820   // the case.
1821   Instruction *OrigAdd = cast<Instruction>(AddWithCst->getOperand(0));
1822   for (Value::use_iterator UI = OrigAdd->use_begin(), E = OrigAdd->use_end();
1823        UI != E; ++UI) {
1824     if (*UI == AddWithCst) continue;
1825
1826     // Only accept truncates for now.  We would really like a nice recursive
1827     // predicate like SimplifyDemandedBits, but which goes downwards the use-def
1828     // chain to see which bits of a value are actually demanded.  If the
1829     // original add had another add which was then immediately truncated, we
1830     // could still do the transformation.
1831     TruncInst *TI = dyn_cast<TruncInst>(*UI);
1832     if (TI == 0 ||
1833         TI->getType()->getPrimitiveSizeInBits() > NewWidth) return 0;
1834   }
1835
1836   // If the pattern matches, truncate the inputs to the narrower type and
1837   // use the sadd_with_overflow intrinsic to efficiently compute both the
1838   // result and the overflow bit.
1839   Module *M = I.getParent()->getParent()->getParent();
1840
1841   Type *NewType = IntegerType::get(OrigAdd->getContext(), NewWidth);
1842   Value *F = Intrinsic::getDeclaration(M, Intrinsic::sadd_with_overflow,
1843                                        NewType);
1844
1845   InstCombiner::BuilderTy *Builder = IC.Builder;
1846
1847   // Put the new code above the original add, in case there are any uses of the
1848   // add between the add and the compare.
1849   Builder->SetInsertPoint(OrigAdd);
1850
1851   Value *TruncA = Builder->CreateTrunc(A, NewType, A->getName()+".trunc");
1852   Value *TruncB = Builder->CreateTrunc(B, NewType, B->getName()+".trunc");
1853   CallInst *Call = Builder->CreateCall2(F, TruncA, TruncB, "sadd");
1854   Value *Add = Builder->CreateExtractValue(Call, 0, "sadd.result");
1855   Value *ZExt = Builder->CreateZExt(Add, OrigAdd->getType());
1856
1857   // The inner add was the result of the narrow add, zero extended to the
1858   // wider type.  Replace it with the result computed by the intrinsic.
1859   IC.ReplaceInstUsesWith(*OrigAdd, ZExt);
1860
1861   // The original icmp gets replaced with the overflow value.
1862   return ExtractValueInst::Create(Call, 1, "sadd.overflow");
1863 }
1864
1865 static Instruction *ProcessUAddIdiom(Instruction &I, Value *OrigAddV,
1866                                      InstCombiner &IC) {
1867   // Don't bother doing this transformation for pointers, don't do it for
1868   // vectors.
1869   if (!isa<IntegerType>(OrigAddV->getType())) return 0;
1870
1871   // If the add is a constant expr, then we don't bother transforming it.
1872   Instruction *OrigAdd = dyn_cast<Instruction>(OrigAddV);
1873   if (OrigAdd == 0) return 0;
1874
1875   Value *LHS = OrigAdd->getOperand(0), *RHS = OrigAdd->getOperand(1);
1876
1877   // Put the new code above the original add, in case there are any uses of the
1878   // add between the add and the compare.
1879   InstCombiner::BuilderTy *Builder = IC.Builder;
1880   Builder->SetInsertPoint(OrigAdd);
1881
1882   Module *M = I.getParent()->getParent()->getParent();
1883   Type *Ty = LHS->getType();
1884   Value *F = Intrinsic::getDeclaration(M, Intrinsic::uadd_with_overflow, Ty);
1885   CallInst *Call = Builder->CreateCall2(F, LHS, RHS, "uadd");
1886   Value *Add = Builder->CreateExtractValue(Call, 0);
1887
1888   IC.ReplaceInstUsesWith(*OrigAdd, Add);
1889
1890   // The original icmp gets replaced with the overflow value.
1891   return ExtractValueInst::Create(Call, 1, "uadd.overflow");
1892 }
1893
1894 // DemandedBitsLHSMask - When performing a comparison against a constant,
1895 // it is possible that not all the bits in the LHS are demanded.  This helper
1896 // method computes the mask that IS demanded.
1897 static APInt DemandedBitsLHSMask(ICmpInst &I,
1898                                  unsigned BitWidth, bool isSignCheck) {
1899   if (isSignCheck)
1900     return APInt::getSignBit(BitWidth);
1901
1902   ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1));
1903   if (!CI) return APInt::getAllOnesValue(BitWidth);
1904   const APInt &RHS = CI->getValue();
1905
1906   switch (I.getPredicate()) {
1907   // For a UGT comparison, we don't care about any bits that
1908   // correspond to the trailing ones of the comparand.  The value of these
1909   // bits doesn't impact the outcome of the comparison, because any value
1910   // greater than the RHS must differ in a bit higher than these due to carry.
1911   case ICmpInst::ICMP_UGT: {
1912     unsigned trailingOnes = RHS.countTrailingOnes();
1913     APInt lowBitsSet = APInt::getLowBitsSet(BitWidth, trailingOnes);
1914     return ~lowBitsSet;
1915   }
1916
1917   // Similarly, for a ULT comparison, we don't care about the trailing zeros.
1918   // Any value less than the RHS must differ in a higher bit because of carries.
1919   case ICmpInst::ICMP_ULT: {
1920     unsigned trailingZeros = RHS.countTrailingZeros();
1921     APInt lowBitsSet = APInt::getLowBitsSet(BitWidth, trailingZeros);
1922     return ~lowBitsSet;
1923   }
1924
1925   default:
1926     return APInt::getAllOnesValue(BitWidth);
1927   }
1928
1929 }
1930
1931 Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
1932   bool Changed = false;
1933   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1934
1935   /// Orders the operands of the compare so that they are listed from most
1936   /// complex to least complex.  This puts constants before unary operators,
1937   /// before binary operators.
1938   if (getComplexity(Op0) < getComplexity(Op1)) {
1939     I.swapOperands();
1940     std::swap(Op0, Op1);
1941     Changed = true;
1942   }
1943
1944   if (Value *V = SimplifyICmpInst(I.getPredicate(), Op0, Op1, TD))
1945     return ReplaceInstUsesWith(I, V);
1946
1947   // comparing -val or val with non-zero is the same as just comparing val
1948   // ie, abs(val) != 0 -> val != 0
1949   if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero()))
1950   {
1951     Value *Cond, *SelectTrue, *SelectFalse;
1952     if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue),
1953                             m_Value(SelectFalse)))) {
1954       if (Value *V = dyn_castNegVal(SelectTrue)) {
1955         if (V == SelectFalse)
1956           return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
1957       }
1958       else if (Value *V = dyn_castNegVal(SelectFalse)) {
1959         if (V == SelectTrue)
1960           return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
1961       }
1962     }
1963   }
1964
1965   Type *Ty = Op0->getType();
1966
1967   // icmp's with boolean values can always be turned into bitwise operations
1968   if (Ty->isIntegerTy(1)) {
1969     switch (I.getPredicate()) {
1970     default: llvm_unreachable("Invalid icmp instruction!");
1971     case ICmpInst::ICMP_EQ: {               // icmp eq i1 A, B -> ~(A^B)
1972       Value *Xor = Builder->CreateXor(Op0, Op1, I.getName()+"tmp");
1973       return BinaryOperator::CreateNot(Xor);
1974     }
1975     case ICmpInst::ICMP_NE:                  // icmp eq i1 A, B -> A^B
1976       return BinaryOperator::CreateXor(Op0, Op1);
1977
1978     case ICmpInst::ICMP_UGT:
1979       std::swap(Op0, Op1);                   // Change icmp ugt -> icmp ult
1980       // FALL THROUGH
1981     case ICmpInst::ICMP_ULT:{               // icmp ult i1 A, B -> ~A & B
1982       Value *Not = Builder->CreateNot(Op0, I.getName()+"tmp");
1983       return BinaryOperator::CreateAnd(Not, Op1);
1984     }
1985     case ICmpInst::ICMP_SGT:
1986       std::swap(Op0, Op1);                   // Change icmp sgt -> icmp slt
1987       // FALL THROUGH
1988     case ICmpInst::ICMP_SLT: {               // icmp slt i1 A, B -> A & ~B
1989       Value *Not = Builder->CreateNot(Op1, I.getName()+"tmp");
1990       return BinaryOperator::CreateAnd(Not, Op0);
1991     }
1992     case ICmpInst::ICMP_UGE:
1993       std::swap(Op0, Op1);                   // Change icmp uge -> icmp ule
1994       // FALL THROUGH
1995     case ICmpInst::ICMP_ULE: {               //  icmp ule i1 A, B -> ~A | B
1996       Value *Not = Builder->CreateNot(Op0, I.getName()+"tmp");
1997       return BinaryOperator::CreateOr(Not, Op1);
1998     }
1999     case ICmpInst::ICMP_SGE:
2000       std::swap(Op0, Op1);                   // Change icmp sge -> icmp sle
2001       // FALL THROUGH
2002     case ICmpInst::ICMP_SLE: {               //  icmp sle i1 A, B -> A | ~B
2003       Value *Not = Builder->CreateNot(Op1, I.getName()+"tmp");
2004       return BinaryOperator::CreateOr(Not, Op0);
2005     }
2006     }
2007   }
2008
2009   unsigned BitWidth = 0;
2010   if (Ty->isIntOrIntVectorTy())
2011     BitWidth = Ty->getScalarSizeInBits();
2012   else if (TD)  // Pointers require TD info to get their size.
2013     BitWidth = TD->getTypeSizeInBits(Ty->getScalarType());
2014
2015   bool isSignBit = false;
2016
2017   // See if we are doing a comparison with a constant.
2018   if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2019     Value *A = 0, *B = 0;
2020
2021     // Match the following pattern, which is a common idiom when writing
2022     // overflow-safe integer arithmetic function.  The source performs an
2023     // addition in wider type, and explicitly checks for overflow using
2024     // comparisons against INT_MIN and INT_MAX.  Simplify this by using the
2025     // sadd_with_overflow intrinsic.
2026     //
2027     // TODO: This could probably be generalized to handle other overflow-safe
2028     // operations if we worked out the formulas to compute the appropriate
2029     // magic constants.
2030     //
2031     // sum = a + b
2032     // if (sum+128 >u 255)  ...  -> llvm.sadd.with.overflow.i8
2033     {
2034     ConstantInt *CI2;    // I = icmp ugt (add (add A, B), CI2), CI
2035     if (I.getPredicate() == ICmpInst::ICMP_UGT &&
2036         match(Op0, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2))))
2037       if (Instruction *Res = ProcessUGT_ADDCST_ADD(I, A, B, CI2, CI, *this))
2038         return Res;
2039     }
2040
2041     // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B)
2042     if (I.isEquality() && CI->isZero() &&
2043         match(Op0, m_Sub(m_Value(A), m_Value(B)))) {
2044       // (icmp cond A B) if cond is equality
2045       return new ICmpInst(I.getPredicate(), A, B);
2046     }
2047
2048     // If we have an icmp le or icmp ge instruction, turn it into the
2049     // appropriate icmp lt or icmp gt instruction.  This allows us to rely on
2050     // them being folded in the code below.  The SimplifyICmpInst code has
2051     // already handled the edge cases for us, so we just assert on them.
2052     switch (I.getPredicate()) {
2053     default: break;
2054     case ICmpInst::ICMP_ULE:
2055       assert(!CI->isMaxValue(false));                 // A <=u MAX -> TRUE
2056       return new ICmpInst(ICmpInst::ICMP_ULT, Op0,
2057                           ConstantInt::get(CI->getContext(), CI->getValue()+1));
2058     case ICmpInst::ICMP_SLE:
2059       assert(!CI->isMaxValue(true));                  // A <=s MAX -> TRUE
2060       return new ICmpInst(ICmpInst::ICMP_SLT, Op0,
2061                           ConstantInt::get(CI->getContext(), CI->getValue()+1));
2062     case ICmpInst::ICMP_UGE:
2063       assert(!CI->isMinValue(false));                 // A >=u MIN -> TRUE
2064       return new ICmpInst(ICmpInst::ICMP_UGT, Op0,
2065                           ConstantInt::get(CI->getContext(), CI->getValue()-1));
2066     case ICmpInst::ICMP_SGE:
2067       assert(!CI->isMinValue(true));                  // A >=s MIN -> TRUE
2068       return new ICmpInst(ICmpInst::ICMP_SGT, Op0,
2069                           ConstantInt::get(CI->getContext(), CI->getValue()-1));
2070     }
2071
2072     // If this comparison is a normal comparison, it demands all
2073     // bits, if it is a sign bit comparison, it only demands the sign bit.
2074     bool UnusedBit;
2075     isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit);
2076   }
2077
2078   // See if we can fold the comparison based on range information we can get
2079   // by checking whether bits are known to be zero or one in the input.
2080   if (BitWidth != 0) {
2081     APInt Op0KnownZero(BitWidth, 0), Op0KnownOne(BitWidth, 0);
2082     APInt Op1KnownZero(BitWidth, 0), Op1KnownOne(BitWidth, 0);
2083
2084     if (SimplifyDemandedBits(I.getOperandUse(0),
2085                              DemandedBitsLHSMask(I, BitWidth, isSignBit),
2086                              Op0KnownZero, Op0KnownOne, 0))
2087       return &I;
2088     if (SimplifyDemandedBits(I.getOperandUse(1),
2089                              APInt::getAllOnesValue(BitWidth),
2090                              Op1KnownZero, Op1KnownOne, 0))
2091       return &I;
2092
2093     // Given the known and unknown bits, compute a range that the LHS could be
2094     // in.  Compute the Min, Max and RHS values based on the known bits. For the
2095     // EQ and NE we use unsigned values.
2096     APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
2097     APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
2098     if (I.isSigned()) {
2099       ComputeSignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne,
2100                                              Op0Min, Op0Max);
2101       ComputeSignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne,
2102                                              Op1Min, Op1Max);
2103     } else {
2104       ComputeUnsignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne,
2105                                                Op0Min, Op0Max);
2106       ComputeUnsignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne,
2107                                                Op1Min, Op1Max);
2108     }
2109
2110     // If Min and Max are known to be the same, then SimplifyDemandedBits
2111     // figured out that the LHS is a constant.  Just constant fold this now so
2112     // that code below can assume that Min != Max.
2113     if (!isa<Constant>(Op0) && Op0Min == Op0Max)
2114       return new ICmpInst(I.getPredicate(),
2115                           ConstantInt::get(Op0->getType(), Op0Min), Op1);
2116     if (!isa<Constant>(Op1) && Op1Min == Op1Max)
2117       return new ICmpInst(I.getPredicate(), Op0,
2118                           ConstantInt::get(Op1->getType(), Op1Min));
2119
2120     // Based on the range information we know about the LHS, see if we can
2121     // simplify this comparison.  For example, (x&4) < 8 is always true.
2122     switch (I.getPredicate()) {
2123     default: llvm_unreachable("Unknown icmp opcode!");
2124     case ICmpInst::ICMP_EQ: {
2125       if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
2126         return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
2127
2128       // If all bits are known zero except for one, then we know at most one
2129       // bit is set.   If the comparison is against zero, then this is a check
2130       // to see if *that* bit is set.
2131       APInt Op0KnownZeroInverted = ~Op0KnownZero;
2132       if (~Op1KnownZero == 0 && Op0KnownZeroInverted.isPowerOf2()) {
2133         // If the LHS is an AND with the same constant, look through it.
2134         Value *LHS = 0;
2135         ConstantInt *LHSC = 0;
2136         if (!match(Op0, m_And(m_Value(LHS), m_ConstantInt(LHSC))) ||
2137             LHSC->getValue() != Op0KnownZeroInverted)
2138           LHS = Op0;
2139
2140         // If the LHS is 1 << x, and we know the result is a power of 2 like 8,
2141         // then turn "((1 << x)&8) == 0" into "x != 3".
2142         Value *X = 0;
2143         if (match(LHS, m_Shl(m_One(), m_Value(X)))) {
2144           unsigned CmpVal = Op0KnownZeroInverted.countTrailingZeros();
2145           return new ICmpInst(ICmpInst::ICMP_NE, X,
2146                               ConstantInt::get(X->getType(), CmpVal));
2147         }
2148
2149         // If the LHS is 8 >>u x, and we know the result is a power of 2 like 1,
2150         // then turn "((8 >>u x)&1) == 0" into "x != 3".
2151         const APInt *CI;
2152         if (Op0KnownZeroInverted == 1 &&
2153             match(LHS, m_LShr(m_Power2(CI), m_Value(X))))
2154           return new ICmpInst(ICmpInst::ICMP_NE, X,
2155                               ConstantInt::get(X->getType(),
2156                                                CI->countTrailingZeros()));
2157       }
2158
2159       break;
2160     }
2161     case ICmpInst::ICMP_NE: {
2162       if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
2163         return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
2164
2165       // If all bits are known zero except for one, then we know at most one
2166       // bit is set.   If the comparison is against zero, then this is a check
2167       // to see if *that* bit is set.
2168       APInt Op0KnownZeroInverted = ~Op0KnownZero;
2169       if (~Op1KnownZero == 0 && Op0KnownZeroInverted.isPowerOf2()) {
2170         // If the LHS is an AND with the same constant, look through it.
2171         Value *LHS = 0;
2172         ConstantInt *LHSC = 0;
2173         if (!match(Op0, m_And(m_Value(LHS), m_ConstantInt(LHSC))) ||
2174             LHSC->getValue() != Op0KnownZeroInverted)
2175           LHS = Op0;
2176
2177         // If the LHS is 1 << x, and we know the result is a power of 2 like 8,
2178         // then turn "((1 << x)&8) != 0" into "x == 3".
2179         Value *X = 0;
2180         if (match(LHS, m_Shl(m_One(), m_Value(X)))) {
2181           unsigned CmpVal = Op0KnownZeroInverted.countTrailingZeros();
2182           return new ICmpInst(ICmpInst::ICMP_EQ, X,
2183                               ConstantInt::get(X->getType(), CmpVal));
2184         }
2185
2186         // If the LHS is 8 >>u x, and we know the result is a power of 2 like 1,
2187         // then turn "((8 >>u x)&1) != 0" into "x == 3".
2188         const APInt *CI;
2189         if (Op0KnownZeroInverted == 1 &&
2190             match(LHS, m_LShr(m_Power2(CI), m_Value(X))))
2191           return new ICmpInst(ICmpInst::ICMP_EQ, X,
2192                               ConstantInt::get(X->getType(),
2193                                                CI->countTrailingZeros()));
2194       }
2195
2196       break;
2197     }
2198     case ICmpInst::ICMP_ULT:
2199       if (Op0Max.ult(Op1Min))          // A <u B -> true if max(A) < min(B)
2200         return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
2201       if (Op0Min.uge(Op1Max))          // A <u B -> false if min(A) >= max(B)
2202         return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
2203       if (Op1Min == Op0Max)            // A <u B -> A != B if max(A) == min(B)
2204         return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
2205       if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2206         if (Op1Max == Op0Min+1)        // A <u C -> A == C-1 if min(A)+1 == C
2207           return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
2208                           ConstantInt::get(CI->getContext(), CI->getValue()-1));
2209
2210         // (x <u 2147483648) -> (x >s -1)  -> true if sign bit clear
2211         if (CI->isMinValue(true))
2212           return new ICmpInst(ICmpInst::ICMP_SGT, Op0,
2213                            Constant::getAllOnesValue(Op0->getType()));
2214       }
2215       break;
2216     case ICmpInst::ICMP_UGT:
2217       if (Op0Min.ugt(Op1Max))          // A >u B -> true if min(A) > max(B)
2218         return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
2219       if (Op0Max.ule(Op1Min))          // A >u B -> false if max(A) <= max(B)
2220         return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
2221
2222       if (Op1Max == Op0Min)            // A >u B -> A != B if min(A) == max(B)
2223         return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
2224       if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2225         if (Op1Min == Op0Max-1)        // A >u C -> A == C+1 if max(a)-1 == C
2226           return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
2227                           ConstantInt::get(CI->getContext(), CI->getValue()+1));
2228
2229         // (x >u 2147483647) -> (x <s 0)  -> true if sign bit set
2230         if (CI->isMaxValue(true))
2231           return new ICmpInst(ICmpInst::ICMP_SLT, Op0,
2232                               Constant::getNullValue(Op0->getType()));
2233       }
2234       break;
2235     case ICmpInst::ICMP_SLT:
2236       if (Op0Max.slt(Op1Min))          // A <s B -> true if max(A) < min(C)
2237         return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
2238       if (Op0Min.sge(Op1Max))          // A <s B -> false if min(A) >= max(C)
2239         return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
2240       if (Op1Min == Op0Max)            // A <s B -> A != B if max(A) == min(B)
2241         return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
2242       if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2243         if (Op1Max == Op0Min+1)        // A <s C -> A == C-1 if min(A)+1 == C
2244           return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
2245                           ConstantInt::get(CI->getContext(), CI->getValue()-1));
2246       }
2247       break;
2248     case ICmpInst::ICMP_SGT:
2249       if (Op0Min.sgt(Op1Max))          // A >s B -> true if min(A) > max(B)
2250         return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
2251       if (Op0Max.sle(Op1Min))          // A >s B -> false if max(A) <= min(B)
2252         return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
2253
2254       if (Op1Max == Op0Min)            // A >s B -> A != B if min(A) == max(B)
2255         return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
2256       if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2257         if (Op1Min == Op0Max-1)        // A >s C -> A == C+1 if max(A)-1 == C
2258           return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
2259                           ConstantInt::get(CI->getContext(), CI->getValue()+1));
2260       }
2261       break;
2262     case ICmpInst::ICMP_SGE:
2263       assert(!isa<ConstantInt>(Op1) && "ICMP_SGE with ConstantInt not folded!");
2264       if (Op0Min.sge(Op1Max))          // A >=s B -> true if min(A) >= max(B)
2265         return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
2266       if (Op0Max.slt(Op1Min))          // A >=s B -> false if max(A) < min(B)
2267         return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
2268       break;
2269     case ICmpInst::ICMP_SLE:
2270       assert(!isa<ConstantInt>(Op1) && "ICMP_SLE with ConstantInt not folded!");
2271       if (Op0Max.sle(Op1Min))          // A <=s B -> true if max(A) <= min(B)
2272         return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
2273       if (Op0Min.sgt(Op1Max))          // A <=s B -> false if min(A) > max(B)
2274         return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
2275       break;
2276     case ICmpInst::ICMP_UGE:
2277       assert(!isa<ConstantInt>(Op1) && "ICMP_UGE with ConstantInt not folded!");
2278       if (Op0Min.uge(Op1Max))          // A >=u B -> true if min(A) >= max(B)
2279         return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
2280       if (Op0Max.ult(Op1Min))          // A >=u B -> false if max(A) < min(B)
2281         return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
2282       break;
2283     case ICmpInst::ICMP_ULE:
2284       assert(!isa<ConstantInt>(Op1) && "ICMP_ULE with ConstantInt not folded!");
2285       if (Op0Max.ule(Op1Min))          // A <=u B -> true if max(A) <= min(B)
2286         return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
2287       if (Op0Min.ugt(Op1Max))          // A <=u B -> false if min(A) > max(B)
2288         return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
2289       break;
2290     }
2291
2292     // Turn a signed comparison into an unsigned one if both operands
2293     // are known to have the same sign.
2294     if (I.isSigned() &&
2295         ((Op0KnownZero.isNegative() && Op1KnownZero.isNegative()) ||
2296          (Op0KnownOne.isNegative() && Op1KnownOne.isNegative())))
2297       return new ICmpInst(I.getUnsignedPredicate(), Op0, Op1);
2298   }
2299
2300   // Test if the ICmpInst instruction is used exclusively by a select as
2301   // part of a minimum or maximum operation. If so, refrain from doing
2302   // any other folding. This helps out other analyses which understand
2303   // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
2304   // and CodeGen. And in this case, at least one of the comparison
2305   // operands has at least one user besides the compare (the select),
2306   // which would often largely negate the benefit of folding anyway.
2307   if (I.hasOneUse())
2308     if (SelectInst *SI = dyn_cast<SelectInst>(*I.use_begin()))
2309       if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) ||
2310           (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1))
2311         return 0;
2312
2313   // See if we are doing a comparison between a constant and an instruction that
2314   // can be folded into the comparison.
2315   if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2316     // Since the RHS is a ConstantInt (CI), if the left hand side is an
2317     // instruction, see if that instruction also has constants so that the
2318     // instruction can be folded into the icmp
2319     if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
2320       if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI))
2321         return Res;
2322   }
2323
2324   // Handle icmp with constant (but not simple integer constant) RHS
2325   if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
2326     if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
2327       switch (LHSI->getOpcode()) {
2328       case Instruction::GetElementPtr:
2329           // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null
2330         if (RHSC->isNullValue() &&
2331             cast<GetElementPtrInst>(LHSI)->hasAllZeroIndices())
2332           return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
2333                   Constant::getNullValue(LHSI->getOperand(0)->getType()));
2334         break;
2335       case Instruction::PHI:
2336         // Only fold icmp into the PHI if the phi and icmp are in the same
2337         // block.  If in the same block, we're encouraging jump threading.  If
2338         // not, we are just pessimizing the code by making an i1 phi.
2339         if (LHSI->getParent() == I.getParent())
2340           if (Instruction *NV = FoldOpIntoPhi(I))
2341             return NV;
2342         break;
2343       case Instruction::Select: {
2344         // If either operand of the select is a constant, we can fold the
2345         // comparison into the select arms, which will cause one to be
2346         // constant folded and the select turned into a bitwise or.
2347         Value *Op1 = 0, *Op2 = 0;
2348         if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1)))
2349           Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
2350         if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2)))
2351           Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
2352
2353         // We only want to perform this transformation if it will not lead to
2354         // additional code. This is true if either both sides of the select
2355         // fold to a constant (in which case the icmp is replaced with a select
2356         // which will usually simplify) or this is the only user of the
2357         // select (in which case we are trading a select+icmp for a simpler
2358         // select+icmp).
2359         if ((Op1 && Op2) || (LHSI->hasOneUse() && (Op1 || Op2))) {
2360           if (!Op1)
2361             Op1 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(1),
2362                                       RHSC, I.getName());
2363           if (!Op2)
2364             Op2 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(2),
2365                                       RHSC, I.getName());
2366           return SelectInst::Create(LHSI->getOperand(0), Op1, Op2);
2367         }
2368         break;
2369       }
2370       case Instruction::IntToPtr:
2371         // icmp pred inttoptr(X), null -> icmp pred X, 0
2372         if (RHSC->isNullValue() && TD &&
2373             TD->getIntPtrType(RHSC->getContext()) ==
2374                LHSI->getOperand(0)->getType())
2375           return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
2376                         Constant::getNullValue(LHSI->getOperand(0)->getType()));
2377         break;
2378
2379       case Instruction::Load:
2380         // Try to optimize things like "A[i] > 4" to index computations.
2381         if (GetElementPtrInst *GEP =
2382               dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) {
2383           if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
2384             if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
2385                 !cast<LoadInst>(LHSI)->isVolatile())
2386               if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV, I))
2387                 return Res;
2388         }
2389         break;
2390       }
2391   }
2392
2393   // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now.
2394   if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op0))
2395     if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I))
2396       return NI;
2397   if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op1))
2398     if (Instruction *NI = FoldGEPICmp(GEP, Op0,
2399                            ICmpInst::getSwappedPredicate(I.getPredicate()), I))
2400       return NI;
2401
2402   // Test to see if the operands of the icmp are casted versions of other
2403   // values.  If the ptr->ptr cast can be stripped off both arguments, we do so
2404   // now.
2405   if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) {
2406     if (Op0->getType()->isPointerTy() &&
2407         (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) {
2408       // We keep moving the cast from the left operand over to the right
2409       // operand, where it can often be eliminated completely.
2410       Op0 = CI->getOperand(0);
2411
2412       // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast
2413       // so eliminate it as well.
2414       if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1))
2415         Op1 = CI2->getOperand(0);
2416
2417       // If Op1 is a constant, we can fold the cast into the constant.
2418       if (Op0->getType() != Op1->getType()) {
2419         if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
2420           Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType());
2421         } else {
2422           // Otherwise, cast the RHS right before the icmp
2423           Op1 = Builder->CreateBitCast(Op1, Op0->getType());
2424         }
2425       }
2426       return new ICmpInst(I.getPredicate(), Op0, Op1);
2427     }
2428   }
2429
2430   if (isa<CastInst>(Op0)) {
2431     // Handle the special case of: icmp (cast bool to X), <cst>
2432     // This comes up when you have code like
2433     //   int X = A < B;
2434     //   if (X) ...
2435     // For generality, we handle any zero-extension of any operand comparison
2436     // with a constant or another cast from the same type.
2437     if (isa<Constant>(Op1) || isa<CastInst>(Op1))
2438       if (Instruction *R = visitICmpInstWithCastAndCast(I))
2439         return R;
2440   }
2441
2442   // Special logic for binary operators.
2443   BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Op0);
2444   BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Op1);
2445   if (BO0 || BO1) {
2446     CmpInst::Predicate Pred = I.getPredicate();
2447     bool NoOp0WrapProblem = false, NoOp1WrapProblem = false;
2448     if (BO0 && isa<OverflowingBinaryOperator>(BO0))
2449       NoOp0WrapProblem = ICmpInst::isEquality(Pred) ||
2450         (CmpInst::isUnsigned(Pred) && BO0->hasNoUnsignedWrap()) ||
2451         (CmpInst::isSigned(Pred) && BO0->hasNoSignedWrap());
2452     if (BO1 && isa<OverflowingBinaryOperator>(BO1))
2453       NoOp1WrapProblem = ICmpInst::isEquality(Pred) ||
2454         (CmpInst::isUnsigned(Pred) && BO1->hasNoUnsignedWrap()) ||
2455         (CmpInst::isSigned(Pred) && BO1->hasNoSignedWrap());
2456
2457     // Analyze the case when either Op0 or Op1 is an add instruction.
2458     // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null).
2459     Value *A = 0, *B = 0, *C = 0, *D = 0;
2460     if (BO0 && BO0->getOpcode() == Instruction::Add)
2461       A = BO0->getOperand(0), B = BO0->getOperand(1);
2462     if (BO1 && BO1->getOpcode() == Instruction::Add)
2463       C = BO1->getOperand(0), D = BO1->getOperand(1);
2464
2465     // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
2466     if ((A == Op1 || B == Op1) && NoOp0WrapProblem)
2467       return new ICmpInst(Pred, A == Op1 ? B : A,
2468                           Constant::getNullValue(Op1->getType()));
2469
2470     // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
2471     if ((C == Op0 || D == Op0) && NoOp1WrapProblem)
2472       return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()),
2473                           C == Op0 ? D : C);
2474
2475     // icmp (X+Y), (X+Z) -> icmp Y, Z for equalities or if there is no overflow.
2476     if (A && C && (A == C || A == D || B == C || B == D) &&
2477         NoOp0WrapProblem && NoOp1WrapProblem &&
2478         // Try not to increase register pressure.
2479         BO0->hasOneUse() && BO1->hasOneUse()) {
2480       // Determine Y and Z in the form icmp (X+Y), (X+Z).
2481       Value *Y, *Z;
2482       if (A == C) {
2483         // C + B == C + D  ->  B == D
2484         Y = B;
2485         Z = D;
2486       } else if (A == D) {
2487         // D + B == C + D  ->  B == C
2488         Y = B;
2489         Z = C;
2490       } else if (B == C) {
2491         // A + C == C + D  ->  A == D
2492         Y = A;
2493         Z = D;
2494       } else {
2495         assert(B == D);
2496         // A + D == C + D  ->  A == C
2497         Y = A;
2498         Z = C;
2499       }
2500       return new ICmpInst(Pred, Y, Z);
2501     }
2502
2503     // Analyze the case when either Op0 or Op1 is a sub instruction.
2504     // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null).
2505     A = 0; B = 0; C = 0; D = 0;
2506     if (BO0 && BO0->getOpcode() == Instruction::Sub)
2507       A = BO0->getOperand(0), B = BO0->getOperand(1);
2508     if (BO1 && BO1->getOpcode() == Instruction::Sub)
2509       C = BO1->getOperand(0), D = BO1->getOperand(1);
2510
2511     // icmp (X-Y), X -> icmp 0, Y for equalities or if there is no overflow.
2512     if (A == Op1 && NoOp0WrapProblem)
2513       return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B);
2514
2515     // icmp X, (X-Y) -> icmp Y, 0 for equalities or if there is no overflow.
2516     if (C == Op0 && NoOp1WrapProblem)
2517       return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType()));
2518
2519     // icmp (Y-X), (Z-X) -> icmp Y, Z for equalities or if there is no overflow.
2520     if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem &&
2521         // Try not to increase register pressure.
2522         BO0->hasOneUse() && BO1->hasOneUse())
2523       return new ICmpInst(Pred, A, C);
2524
2525     // icmp (X-Y), (X-Z) -> icmp Z, Y for equalities or if there is no overflow.
2526     if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem &&
2527         // Try not to increase register pressure.
2528         BO0->hasOneUse() && BO1->hasOneUse())
2529       return new ICmpInst(Pred, D, B);
2530
2531     BinaryOperator *SRem = NULL;
2532     // icmp (srem X, Y), Y
2533     if (BO0 && BO0->getOpcode() == Instruction::SRem &&
2534         Op1 == BO0->getOperand(1))
2535       SRem = BO0;
2536     // icmp Y, (srem X, Y)
2537     else if (BO1 && BO1->getOpcode() == Instruction::SRem &&
2538              Op0 == BO1->getOperand(1))
2539       SRem = BO1;
2540     if (SRem) {
2541       // We don't check hasOneUse to avoid increasing register pressure because
2542       // the value we use is the same value this instruction was already using.
2543       switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) {
2544         default: break;
2545         case ICmpInst::ICMP_EQ:
2546           return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
2547         case ICmpInst::ICMP_NE:
2548           return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
2549         case ICmpInst::ICMP_SGT:
2550         case ICmpInst::ICMP_SGE:
2551           return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1),
2552                               Constant::getAllOnesValue(SRem->getType()));
2553         case ICmpInst::ICMP_SLT:
2554         case ICmpInst::ICMP_SLE:
2555           return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1),
2556                               Constant::getNullValue(SRem->getType()));
2557       }
2558     }
2559
2560     if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() &&
2561         BO0->hasOneUse() && BO1->hasOneUse() &&
2562         BO0->getOperand(1) == BO1->getOperand(1)) {
2563       switch (BO0->getOpcode()) {
2564       default: break;
2565       case Instruction::Add:
2566       case Instruction::Sub:
2567       case Instruction::Xor:
2568         if (I.isEquality())    // a+x icmp eq/ne b+x --> a icmp b
2569           return new ICmpInst(I.getPredicate(), BO0->getOperand(0),
2570                               BO1->getOperand(0));
2571         // icmp u/s (a ^ signbit), (b ^ signbit) --> icmp s/u a, b
2572         if (ConstantInt *CI = dyn_cast<ConstantInt>(BO0->getOperand(1))) {
2573           if (CI->getValue().isSignBit()) {
2574             ICmpInst::Predicate Pred = I.isSigned()
2575                                            ? I.getUnsignedPredicate()
2576                                            : I.getSignedPredicate();
2577             return new ICmpInst(Pred, BO0->getOperand(0),
2578                                 BO1->getOperand(0));
2579           }
2580
2581           if (CI->isMaxValue(true)) {
2582             ICmpInst::Predicate Pred = I.isSigned()
2583                                            ? I.getUnsignedPredicate()
2584                                            : I.getSignedPredicate();
2585             Pred = I.getSwappedPredicate(Pred);
2586             return new ICmpInst(Pred, BO0->getOperand(0),
2587                                 BO1->getOperand(0));
2588           }
2589         }
2590         break;
2591       case Instruction::Mul:
2592         if (!I.isEquality())
2593           break;
2594
2595         if (ConstantInt *CI = dyn_cast<ConstantInt>(BO0->getOperand(1))) {
2596           // a * Cst icmp eq/ne b * Cst --> a & Mask icmp b & Mask
2597           // Mask = -1 >> count-trailing-zeros(Cst).
2598           if (!CI->isZero() && !CI->isOne()) {
2599             const APInt &AP = CI->getValue();
2600             ConstantInt *Mask = ConstantInt::get(I.getContext(),
2601                                     APInt::getLowBitsSet(AP.getBitWidth(),
2602                                                          AP.getBitWidth() -
2603                                                     AP.countTrailingZeros()));
2604             Value *And1 = Builder->CreateAnd(BO0->getOperand(0), Mask);
2605             Value *And2 = Builder->CreateAnd(BO1->getOperand(0), Mask);
2606             return new ICmpInst(I.getPredicate(), And1, And2);
2607           }
2608         }
2609         break;
2610       case Instruction::UDiv:
2611       case Instruction::LShr:
2612         if (I.isSigned())
2613           break;
2614         // fall-through
2615       case Instruction::SDiv:
2616       case Instruction::AShr:
2617         if (!BO0->isExact() || !BO1->isExact())
2618           break;
2619         return new ICmpInst(I.getPredicate(), BO0->getOperand(0),
2620                             BO1->getOperand(0));
2621       case Instruction::Shl: {
2622         bool NUW = BO0->hasNoUnsignedWrap() && BO1->hasNoUnsignedWrap();
2623         bool NSW = BO0->hasNoSignedWrap() && BO1->hasNoSignedWrap();
2624         if (!NUW && !NSW)
2625           break;
2626         if (!NSW && I.isSigned())
2627           break;
2628         return new ICmpInst(I.getPredicate(), BO0->getOperand(0),
2629                             BO1->getOperand(0));
2630       }
2631       }
2632     }
2633   }
2634
2635   { Value *A, *B;
2636     // ~x < ~y --> y < x
2637     // ~x < cst --> ~cst < x
2638     if (match(Op0, m_Not(m_Value(A)))) {
2639       if (match(Op1, m_Not(m_Value(B))))
2640         return new ICmpInst(I.getPredicate(), B, A);
2641       if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1))
2642         return new ICmpInst(I.getPredicate(), ConstantExpr::getNot(RHSC), A);
2643     }
2644
2645     // (a+b) <u a  --> llvm.uadd.with.overflow.
2646     // (a+b) <u b  --> llvm.uadd.with.overflow.
2647     if (I.getPredicate() == ICmpInst::ICMP_ULT &&
2648         match(Op0, m_Add(m_Value(A), m_Value(B))) &&
2649         (Op1 == A || Op1 == B))
2650       if (Instruction *R = ProcessUAddIdiom(I, Op0, *this))
2651         return R;
2652
2653     // a >u (a+b)  --> llvm.uadd.with.overflow.
2654     // b >u (a+b)  --> llvm.uadd.with.overflow.
2655     if (I.getPredicate() == ICmpInst::ICMP_UGT &&
2656         match(Op1, m_Add(m_Value(A), m_Value(B))) &&
2657         (Op0 == A || Op0 == B))
2658       if (Instruction *R = ProcessUAddIdiom(I, Op1, *this))
2659         return R;
2660   }
2661
2662   if (I.isEquality()) {
2663     Value *A, *B, *C, *D;
2664
2665     if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
2666       if (A == Op1 || B == Op1) {    // (A^B) == A  ->  B == 0
2667         Value *OtherVal = A == Op1 ? B : A;
2668         return new ICmpInst(I.getPredicate(), OtherVal,
2669                             Constant::getNullValue(A->getType()));
2670       }
2671
2672       if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
2673         // A^c1 == C^c2 --> A == C^(c1^c2)
2674         ConstantInt *C1, *C2;
2675         if (match(B, m_ConstantInt(C1)) &&
2676             match(D, m_ConstantInt(C2)) && Op1->hasOneUse()) {
2677           Constant *NC = ConstantInt::get(I.getContext(),
2678                                           C1->getValue() ^ C2->getValue());
2679           Value *Xor = Builder->CreateXor(C, NC);
2680           return new ICmpInst(I.getPredicate(), A, Xor);
2681         }
2682
2683         // A^B == A^D -> B == D
2684         if (A == C) return new ICmpInst(I.getPredicate(), B, D);
2685         if (A == D) return new ICmpInst(I.getPredicate(), B, C);
2686         if (B == C) return new ICmpInst(I.getPredicate(), A, D);
2687         if (B == D) return new ICmpInst(I.getPredicate(), A, C);
2688       }
2689     }
2690
2691     if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
2692         (A == Op0 || B == Op0)) {
2693       // A == (A^B)  ->  B == 0
2694       Value *OtherVal = A == Op0 ? B : A;
2695       return new ICmpInst(I.getPredicate(), OtherVal,
2696                           Constant::getNullValue(A->getType()));
2697     }
2698
2699     // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
2700     if (match(Op0, m_OneUse(m_And(m_Value(A), m_Value(B)))) &&
2701         match(Op1, m_OneUse(m_And(m_Value(C), m_Value(D))))) {
2702       Value *X = 0, *Y = 0, *Z = 0;
2703
2704       if (A == C) {
2705         X = B; Y = D; Z = A;
2706       } else if (A == D) {
2707         X = B; Y = C; Z = A;
2708       } else if (B == C) {
2709         X = A; Y = D; Z = B;
2710       } else if (B == D) {
2711         X = A; Y = C; Z = B;
2712       }
2713
2714       if (X) {   // Build (X^Y) & Z
2715         Op1 = Builder->CreateXor(X, Y);
2716         Op1 = Builder->CreateAnd(Op1, Z);
2717         I.setOperand(0, Op1);
2718         I.setOperand(1, Constant::getNullValue(Op1->getType()));
2719         return &I;
2720       }
2721     }
2722
2723     // Transform (zext A) == (B & (1<<X)-1) --> A == (trunc B)
2724     // and       (B & (1<<X)-1) == (zext A) --> A == (trunc B)
2725     ConstantInt *Cst1;
2726     if ((Op0->hasOneUse() &&
2727          match(Op0, m_ZExt(m_Value(A))) &&
2728          match(Op1, m_And(m_Value(B), m_ConstantInt(Cst1)))) ||
2729         (Op1->hasOneUse() &&
2730          match(Op0, m_And(m_Value(B), m_ConstantInt(Cst1))) &&
2731          match(Op1, m_ZExt(m_Value(A))))) {
2732       APInt Pow2 = Cst1->getValue() + 1;
2733       if (Pow2.isPowerOf2() && isa<IntegerType>(A->getType()) &&
2734           Pow2.logBase2() == cast<IntegerType>(A->getType())->getBitWidth())
2735         return new ICmpInst(I.getPredicate(), A,
2736                             Builder->CreateTrunc(B, A->getType()));
2737     }
2738
2739     // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to
2740     // "icmp (and X, mask), cst"
2741     uint64_t ShAmt = 0;
2742     if (Op0->hasOneUse() &&
2743         match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A),
2744                                            m_ConstantInt(ShAmt))))) &&
2745         match(Op1, m_ConstantInt(Cst1)) &&
2746         // Only do this when A has multiple uses.  This is most important to do
2747         // when it exposes other optimizations.
2748         !A->hasOneUse()) {
2749       unsigned ASize =cast<IntegerType>(A->getType())->getPrimitiveSizeInBits();
2750
2751       if (ShAmt < ASize) {
2752         APInt MaskV =
2753           APInt::getLowBitsSet(ASize, Op0->getType()->getPrimitiveSizeInBits());
2754         MaskV <<= ShAmt;
2755
2756         APInt CmpV = Cst1->getValue().zext(ASize);
2757         CmpV <<= ShAmt;
2758
2759         Value *Mask = Builder->CreateAnd(A, Builder->getInt(MaskV));
2760         return new ICmpInst(I.getPredicate(), Mask, Builder->getInt(CmpV));
2761       }
2762     }
2763   }
2764
2765   {
2766     Value *X; ConstantInt *Cst;
2767     // icmp X+Cst, X
2768     if (match(Op0, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op1 == X)
2769       return FoldICmpAddOpCst(I, X, Cst, I.getPredicate(), Op0);
2770
2771     // icmp X, X+Cst
2772     if (match(Op1, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op0 == X)
2773       return FoldICmpAddOpCst(I, X, Cst, I.getSwappedPredicate(), Op1);
2774   }
2775   return Changed ? &I : 0;
2776 }
2777
2778
2779
2780
2781
2782
2783 /// FoldFCmp_IntToFP_Cst - Fold fcmp ([us]itofp x, cst) if possible.
2784 ///
2785 Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I,
2786                                                 Instruction *LHSI,
2787                                                 Constant *RHSC) {
2788   if (!isa<ConstantFP>(RHSC)) return 0;
2789   const APFloat &RHS = cast<ConstantFP>(RHSC)->getValueAPF();
2790
2791   // Get the width of the mantissa.  We don't want to hack on conversions that
2792   // might lose information from the integer, e.g. "i64 -> float"
2793   int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
2794   if (MantissaWidth == -1) return 0;  // Unknown.
2795
2796   // Check to see that the input is converted from an integer type that is small
2797   // enough that preserves all bits.  TODO: check here for "known" sign bits.
2798   // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
2799   unsigned InputSize = LHSI->getOperand(0)->getType()->getScalarSizeInBits();
2800
2801   // If this is a uitofp instruction, we need an extra bit to hold the sign.
2802   bool LHSUnsigned = isa<UIToFPInst>(LHSI);
2803   if (LHSUnsigned)
2804     ++InputSize;
2805
2806   // If the conversion would lose info, don't hack on this.
2807   if ((int)InputSize > MantissaWidth)
2808     return 0;
2809
2810   // Otherwise, we can potentially simplify the comparison.  We know that it
2811   // will always come through as an integer value and we know the constant is
2812   // not a NAN (it would have been previously simplified).
2813   assert(!RHS.isNaN() && "NaN comparison not already folded!");
2814
2815   ICmpInst::Predicate Pred;
2816   switch (I.getPredicate()) {
2817   default: llvm_unreachable("Unexpected predicate!");
2818   case FCmpInst::FCMP_UEQ:
2819   case FCmpInst::FCMP_OEQ:
2820     Pred = ICmpInst::ICMP_EQ;
2821     break;
2822   case FCmpInst::FCMP_UGT:
2823   case FCmpInst::FCMP_OGT:
2824     Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
2825     break;
2826   case FCmpInst::FCMP_UGE:
2827   case FCmpInst::FCMP_OGE:
2828     Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
2829     break;
2830   case FCmpInst::FCMP_ULT:
2831   case FCmpInst::FCMP_OLT:
2832     Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
2833     break;
2834   case FCmpInst::FCMP_ULE:
2835   case FCmpInst::FCMP_OLE:
2836     Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
2837     break;
2838   case FCmpInst::FCMP_UNE:
2839   case FCmpInst::FCMP_ONE:
2840     Pred = ICmpInst::ICMP_NE;
2841     break;
2842   case FCmpInst::FCMP_ORD:
2843     return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2844   case FCmpInst::FCMP_UNO:
2845     return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2846   }
2847
2848   IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType());
2849
2850   // Now we know that the APFloat is a normal number, zero or inf.
2851
2852   // See if the FP constant is too large for the integer.  For example,
2853   // comparing an i8 to 300.0.
2854   unsigned IntWidth = IntTy->getScalarSizeInBits();
2855
2856   if (!LHSUnsigned) {
2857     // If the RHS value is > SignedMax, fold the comparison.  This handles +INF
2858     // and large values.
2859     APFloat SMax(RHS.getSemantics(), APFloat::fcZero, false);
2860     SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true,
2861                           APFloat::rmNearestTiesToEven);
2862     if (SMax.compare(RHS) == APFloat::cmpLessThan) {  // smax < 13123.0
2863       if (Pred == ICmpInst::ICMP_NE  || Pred == ICmpInst::ICMP_SLT ||
2864           Pred == ICmpInst::ICMP_SLE)
2865         return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2866       return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2867     }
2868   } else {
2869     // If the RHS value is > UnsignedMax, fold the comparison. This handles
2870     // +INF and large values.
2871     APFloat UMax(RHS.getSemantics(), APFloat::fcZero, false);
2872     UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false,
2873                           APFloat::rmNearestTiesToEven);
2874     if (UMax.compare(RHS) == APFloat::cmpLessThan) {  // umax < 13123.0
2875       if (Pred == ICmpInst::ICMP_NE  || Pred == ICmpInst::ICMP_ULT ||
2876           Pred == ICmpInst::ICMP_ULE)
2877         return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2878       return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2879     }
2880   }
2881
2882   if (!LHSUnsigned) {
2883     // See if the RHS value is < SignedMin.
2884     APFloat SMin(RHS.getSemantics(), APFloat::fcZero, false);
2885     SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true,
2886                           APFloat::rmNearestTiesToEven);
2887     if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0
2888       if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
2889           Pred == ICmpInst::ICMP_SGE)
2890         return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2891       return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2892     }
2893   } else {
2894     // See if the RHS value is < UnsignedMin.
2895     APFloat SMin(RHS.getSemantics(), APFloat::fcZero, false);
2896     SMin.convertFromAPInt(APInt::getMinValue(IntWidth), true,
2897                           APFloat::rmNearestTiesToEven);
2898     if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // umin > 12312.0
2899       if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||
2900           Pred == ICmpInst::ICMP_UGE)
2901         return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2902       return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2903     }
2904   }
2905
2906   // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
2907   // [0, UMAX], but it may still be fractional.  See if it is fractional by
2908   // casting the FP value to the integer value and back, checking for equality.
2909   // Don't do this for zero, because -0.0 is not fractional.
2910   Constant *RHSInt = LHSUnsigned
2911     ? ConstantExpr::getFPToUI(RHSC, IntTy)
2912     : ConstantExpr::getFPToSI(RHSC, IntTy);
2913   if (!RHS.isZero()) {
2914     bool Equal = LHSUnsigned
2915       ? ConstantExpr::getUIToFP(RHSInt, RHSC->getType()) == RHSC
2916       : ConstantExpr::getSIToFP(RHSInt, RHSC->getType()) == RHSC;
2917     if (!Equal) {
2918       // If we had a comparison against a fractional value, we have to adjust
2919       // the compare predicate and sometimes the value.  RHSC is rounded towards
2920       // zero at this point.
2921       switch (Pred) {
2922       default: llvm_unreachable("Unexpected integer comparison!");
2923       case ICmpInst::ICMP_NE:  // (float)int != 4.4   --> true
2924         return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2925       case ICmpInst::ICMP_EQ:  // (float)int == 4.4   --> false
2926         return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2927       case ICmpInst::ICMP_ULE:
2928         // (float)int <= 4.4   --> int <= 4
2929         // (float)int <= -4.4  --> false
2930         if (RHS.isNegative())
2931           return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2932         break;
2933       case ICmpInst::ICMP_SLE:
2934         // (float)int <= 4.4   --> int <= 4
2935         // (float)int <= -4.4  --> int < -4
2936         if (RHS.isNegative())
2937           Pred = ICmpInst::ICMP_SLT;
2938         break;
2939       case ICmpInst::ICMP_ULT:
2940         // (float)int < -4.4   --> false
2941         // (float)int < 4.4    --> int <= 4
2942         if (RHS.isNegative())
2943           return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2944         Pred = ICmpInst::ICMP_ULE;
2945         break;
2946       case ICmpInst::ICMP_SLT:
2947         // (float)int < -4.4   --> int < -4
2948         // (float)int < 4.4    --> int <= 4
2949         if (!RHS.isNegative())
2950           Pred = ICmpInst::ICMP_SLE;
2951         break;
2952       case ICmpInst::ICMP_UGT:
2953         // (float)int > 4.4    --> int > 4
2954         // (float)int > -4.4   --> true
2955         if (RHS.isNegative())
2956           return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2957         break;
2958       case ICmpInst::ICMP_SGT:
2959         // (float)int > 4.4    --> int > 4
2960         // (float)int > -4.4   --> int >= -4
2961         if (RHS.isNegative())
2962           Pred = ICmpInst::ICMP_SGE;
2963         break;
2964       case ICmpInst::ICMP_UGE:
2965         // (float)int >= -4.4   --> true
2966         // (float)int >= 4.4    --> int > 4
2967         if (RHS.isNegative())
2968           return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2969         Pred = ICmpInst::ICMP_UGT;
2970         break;
2971       case ICmpInst::ICMP_SGE:
2972         // (float)int >= -4.4   --> int >= -4
2973         // (float)int >= 4.4    --> int > 4
2974         if (!RHS.isNegative())
2975           Pred = ICmpInst::ICMP_SGT;
2976         break;
2977       }
2978     }
2979   }
2980
2981   // Lower this FP comparison into an appropriate integer version of the
2982   // comparison.
2983   return new ICmpInst(Pred, LHSI->getOperand(0), RHSInt);
2984 }
2985
2986 Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) {
2987   bool Changed = false;
2988
2989   /// Orders the operands of the compare so that they are listed from most
2990   /// complex to least complex.  This puts constants before unary operators,
2991   /// before binary operators.
2992   if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) {
2993     I.swapOperands();
2994     Changed = true;
2995   }
2996
2997   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2998
2999   if (Value *V = SimplifyFCmpInst(I.getPredicate(), Op0, Op1, TD))
3000     return ReplaceInstUsesWith(I, V);
3001
3002   // Simplify 'fcmp pred X, X'
3003   if (Op0 == Op1) {
3004     switch (I.getPredicate()) {
3005     default: llvm_unreachable("Unknown predicate!");
3006     case FCmpInst::FCMP_UNO:    // True if unordered: isnan(X) | isnan(Y)
3007     case FCmpInst::FCMP_ULT:    // True if unordered or less than
3008     case FCmpInst::FCMP_UGT:    // True if unordered or greater than
3009     case FCmpInst::FCMP_UNE:    // True if unordered or not equal
3010       // Canonicalize these to be 'fcmp uno %X, 0.0'.
3011       I.setPredicate(FCmpInst::FCMP_UNO);
3012       I.setOperand(1, Constant::getNullValue(Op0->getType()));
3013       return &I;
3014
3015     case FCmpInst::FCMP_ORD:    // True if ordered (no nans)
3016     case FCmpInst::FCMP_OEQ:    // True if ordered and equal
3017     case FCmpInst::FCMP_OGE:    // True if ordered and greater than or equal
3018     case FCmpInst::FCMP_OLE:    // True if ordered and less than or equal
3019       // Canonicalize these to be 'fcmp ord %X, 0.0'.
3020       I.setPredicate(FCmpInst::FCMP_ORD);
3021       I.setOperand(1, Constant::getNullValue(Op0->getType()));
3022       return &I;
3023     }
3024   }
3025
3026   // Handle fcmp with constant RHS
3027   if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
3028     if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
3029       switch (LHSI->getOpcode()) {
3030       case Instruction::FPExt: {
3031         // fcmp (fpext x), C -> fcmp x, (fptrunc C) if fptrunc is lossless
3032         FPExtInst *LHSExt = cast<FPExtInst>(LHSI);
3033         ConstantFP *RHSF = dyn_cast<ConstantFP>(RHSC);
3034         if (!RHSF)
3035           break;
3036
3037         const fltSemantics *Sem;
3038         // FIXME: This shouldn't be here.
3039         if (LHSExt->getSrcTy()->isHalfTy())
3040           Sem = &APFloat::IEEEhalf;
3041         else if (LHSExt->getSrcTy()->isFloatTy())
3042           Sem = &APFloat::IEEEsingle;
3043         else if (LHSExt->getSrcTy()->isDoubleTy())
3044           Sem = &APFloat::IEEEdouble;
3045         else if (LHSExt->getSrcTy()->isFP128Ty())
3046           Sem = &APFloat::IEEEquad;
3047         else if (LHSExt->getSrcTy()->isX86_FP80Ty())
3048           Sem = &APFloat::x87DoubleExtended;
3049         else if (LHSExt->getSrcTy()->isPPC_FP128Ty())
3050           Sem = &APFloat::PPCDoubleDouble;
3051         else
3052           break;
3053
3054         bool Lossy;
3055         APFloat F = RHSF->getValueAPF();
3056         F.convert(*Sem, APFloat::rmNearestTiesToEven, &Lossy);
3057
3058         // Avoid lossy conversions and denormals. Zero is a special case
3059         // that's OK to convert.
3060         APFloat Fabs = F;
3061         Fabs.clearSign();
3062         if (!Lossy &&
3063             ((Fabs.compare(APFloat::getSmallestNormalized(*Sem)) !=
3064                  APFloat::cmpLessThan) || Fabs.isZero()))
3065
3066           return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0),
3067                               ConstantFP::get(RHSC->getContext(), F));
3068         break;
3069       }
3070       case Instruction::PHI:
3071         // Only fold fcmp into the PHI if the phi and fcmp are in the same
3072         // block.  If in the same block, we're encouraging jump threading.  If
3073         // not, we are just pessimizing the code by making an i1 phi.
3074         if (LHSI->getParent() == I.getParent())
3075           if (Instruction *NV = FoldOpIntoPhi(I))
3076             return NV;
3077         break;
3078       case Instruction::SIToFP:
3079       case Instruction::UIToFP:
3080         if (Instruction *NV = FoldFCmp_IntToFP_Cst(I, LHSI, RHSC))
3081           return NV;
3082         break;
3083       case Instruction::Select: {
3084         // If either operand of the select is a constant, we can fold the
3085         // comparison into the select arms, which will cause one to be
3086         // constant folded and the select turned into a bitwise or.
3087         Value *Op1 = 0, *Op2 = 0;
3088         if (LHSI->hasOneUse()) {
3089           if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
3090             // Fold the known value into the constant operand.
3091             Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC);
3092             // Insert a new FCmp of the other select operand.
3093             Op2 = Builder->CreateFCmp(I.getPredicate(),
3094                                       LHSI->getOperand(2), RHSC, I.getName());
3095           } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
3096             // Fold the known value into the constant operand.
3097             Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC);
3098             // Insert a new FCmp of the other select operand.
3099             Op1 = Builder->CreateFCmp(I.getPredicate(), LHSI->getOperand(1),
3100                                       RHSC, I.getName());
3101           }
3102         }
3103
3104         if (Op1)
3105           return SelectInst::Create(LHSI->getOperand(0), Op1, Op2);
3106         break;
3107       }
3108       case Instruction::FSub: {
3109         // fcmp pred (fneg x), C -> fcmp swap(pred) x, -C
3110         Value *Op;
3111         if (match(LHSI, m_FNeg(m_Value(Op))))
3112           return new FCmpInst(I.getSwappedPredicate(), Op,
3113                               ConstantExpr::getFNeg(RHSC));
3114         break;
3115       }
3116       case Instruction::Load:
3117         if (GetElementPtrInst *GEP =
3118             dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) {
3119           if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
3120             if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
3121                 !cast<LoadInst>(LHSI)->isVolatile())
3122               if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV, I))
3123                 return Res;
3124         }
3125         break;
3126       case Instruction::Call: {
3127         CallInst *CI = cast<CallInst>(LHSI);
3128         LibFunc::Func Func;
3129         // Various optimization for fabs compared with zero.
3130         if (RHSC->isNullValue() && CI->getCalledFunction() &&
3131             TLI->getLibFunc(CI->getCalledFunction()->getName(), Func) &&
3132             TLI->has(Func)) {
3133           if (Func == LibFunc::fabs || Func == LibFunc::fabsf ||
3134               Func == LibFunc::fabsl) {
3135             switch (I.getPredicate()) {
3136             default: break;
3137             // fabs(x) < 0 --> false
3138             case FCmpInst::FCMP_OLT:
3139               return ReplaceInstUsesWith(I, Builder->getFalse());
3140             // fabs(x) > 0 --> x != 0
3141             case FCmpInst::FCMP_OGT:
3142               return new FCmpInst(FCmpInst::FCMP_ONE, CI->getArgOperand(0),
3143                                   RHSC);
3144             // fabs(x) <= 0 --> x == 0
3145             case FCmpInst::FCMP_OLE:
3146               return new FCmpInst(FCmpInst::FCMP_OEQ, CI->getArgOperand(0),
3147                                   RHSC);
3148             // fabs(x) >= 0 --> !isnan(x)
3149             case FCmpInst::FCMP_OGE:
3150               return new FCmpInst(FCmpInst::FCMP_ORD, CI->getArgOperand(0),
3151                                   RHSC);
3152             // fabs(x) == 0 --> x == 0
3153             // fabs(x) != 0 --> x != 0
3154             case FCmpInst::FCMP_OEQ:
3155             case FCmpInst::FCMP_UEQ:
3156             case FCmpInst::FCMP_ONE:
3157             case FCmpInst::FCMP_UNE:
3158               return new FCmpInst(I.getPredicate(), CI->getArgOperand(0),
3159                                   RHSC);
3160             }
3161           }
3162         }
3163       }
3164       }
3165   }
3166
3167   // fcmp pred (fneg x), (fneg y) -> fcmp swap(pred) x, y
3168   Value *X, *Y;
3169   if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))
3170     return new FCmpInst(I.getSwappedPredicate(), X, Y);
3171
3172   // fcmp (fpext x), (fpext y) -> fcmp x, y
3173   if (FPExtInst *LHSExt = dyn_cast<FPExtInst>(Op0))
3174     if (FPExtInst *RHSExt = dyn_cast<FPExtInst>(Op1))
3175       if (LHSExt->getSrcTy() == RHSExt->getSrcTy())
3176         return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0),
3177                             RHSExt->getOperand(0));
3178
3179   return Changed ? &I : 0;
3180 }