]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Merge ACPICA 20170303.
[FreeBSD/FreeBSD.git] / contrib / llvm / 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 "InstCombineInternal.h"
15 #include "llvm/ADT/APSInt.h"
16 #include "llvm/ADT/SetVector.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/Analysis/ConstantFolding.h"
19 #include "llvm/Analysis/InstructionSimplify.h"
20 #include "llvm/Analysis/MemoryBuiltins.h"
21 #include "llvm/Analysis/TargetLibraryInfo.h"
22 #include "llvm/Analysis/VectorUtils.h"
23 #include "llvm/IR/ConstantRange.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/GetElementPtrTypeIterator.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/IR/PatternMatch.h"
28 #include "llvm/Support/Debug.h"
29
30 using namespace llvm;
31 using namespace PatternMatch;
32
33 #define DEBUG_TYPE "instcombine"
34
35 // How many times is a select replaced by one of its operands?
36 STATISTIC(NumSel, "Number of select opts");
37
38
39 static ConstantInt *extractElement(Constant *V, Constant *Idx) {
40   return cast<ConstantInt>(ConstantExpr::getExtractElement(V, Idx));
41 }
42
43 static bool hasAddOverflow(ConstantInt *Result,
44                            ConstantInt *In1, ConstantInt *In2,
45                            bool IsSigned) {
46   if (!IsSigned)
47     return Result->getValue().ult(In1->getValue());
48
49   if (In2->isNegative())
50     return Result->getValue().sgt(In1->getValue());
51   return Result->getValue().slt(In1->getValue());
52 }
53
54 /// Compute Result = In1+In2, returning true if the result overflowed for this
55 /// type.
56 static bool addWithOverflow(Constant *&Result, Constant *In1,
57                             Constant *In2, bool IsSigned = false) {
58   Result = ConstantExpr::getAdd(In1, In2);
59
60   if (VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
61     for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
62       Constant *Idx = ConstantInt::get(Type::getInt32Ty(In1->getContext()), i);
63       if (hasAddOverflow(extractElement(Result, Idx),
64                          extractElement(In1, Idx),
65                          extractElement(In2, Idx),
66                          IsSigned))
67         return true;
68     }
69     return false;
70   }
71
72   return hasAddOverflow(cast<ConstantInt>(Result),
73                         cast<ConstantInt>(In1), cast<ConstantInt>(In2),
74                         IsSigned);
75 }
76
77 static bool hasSubOverflow(ConstantInt *Result,
78                            ConstantInt *In1, ConstantInt *In2,
79                            bool IsSigned) {
80   if (!IsSigned)
81     return Result->getValue().ugt(In1->getValue());
82
83   if (In2->isNegative())
84     return Result->getValue().slt(In1->getValue());
85
86   return Result->getValue().sgt(In1->getValue());
87 }
88
89 /// Compute Result = In1-In2, returning true if the result overflowed for this
90 /// type.
91 static bool subWithOverflow(Constant *&Result, Constant *In1,
92                             Constant *In2, bool IsSigned = false) {
93   Result = ConstantExpr::getSub(In1, In2);
94
95   if (VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
96     for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
97       Constant *Idx = ConstantInt::get(Type::getInt32Ty(In1->getContext()), i);
98       if (hasSubOverflow(extractElement(Result, Idx),
99                          extractElement(In1, Idx),
100                          extractElement(In2, Idx),
101                          IsSigned))
102         return true;
103     }
104     return false;
105   }
106
107   return hasSubOverflow(cast<ConstantInt>(Result),
108                         cast<ConstantInt>(In1), cast<ConstantInt>(In2),
109                         IsSigned);
110 }
111
112 /// Given an icmp instruction, return true if any use of this comparison is a
113 /// branch on sign bit comparison.
114 static bool isBranchOnSignBitCheck(ICmpInst &I, bool isSignBit) {
115   for (auto *U : I.users())
116     if (isa<BranchInst>(U))
117       return isSignBit;
118   return false;
119 }
120
121 /// Given an exploded icmp instruction, return true if the comparison only
122 /// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if the
123 /// result of the comparison is true when the input value is signed.
124 static bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS,
125                            bool &TrueIfSigned) {
126   switch (Pred) {
127   case ICmpInst::ICMP_SLT:   // True if LHS s< 0
128     TrueIfSigned = true;
129     return RHS == 0;
130   case ICmpInst::ICMP_SLE:   // True if LHS s<= RHS and RHS == -1
131     TrueIfSigned = true;
132     return RHS.isAllOnesValue();
133   case ICmpInst::ICMP_SGT:   // True if LHS s> -1
134     TrueIfSigned = false;
135     return RHS.isAllOnesValue();
136   case ICmpInst::ICMP_UGT:
137     // True if LHS u> RHS and RHS == high-bit-mask - 1
138     TrueIfSigned = true;
139     return RHS.isMaxSignedValue();
140   case ICmpInst::ICMP_UGE:
141     // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc)
142     TrueIfSigned = true;
143     return RHS.isSignBit();
144   default:
145     return false;
146   }
147 }
148
149 /// Returns true if the exploded icmp can be expressed as a signed comparison
150 /// to zero and updates the predicate accordingly.
151 /// The signedness of the comparison is preserved.
152 /// TODO: Refactor with decomposeBitTestICmp()?
153 static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C) {
154   if (!ICmpInst::isSigned(Pred))
155     return false;
156
157   if (C == 0)
158     return ICmpInst::isRelational(Pred);
159
160   if (C == 1) {
161     if (Pred == ICmpInst::ICMP_SLT) {
162       Pred = ICmpInst::ICMP_SLE;
163       return true;
164     }
165   } else if (C.isAllOnesValue()) {
166     if (Pred == ICmpInst::ICMP_SGT) {
167       Pred = ICmpInst::ICMP_SGE;
168       return true;
169     }
170   }
171
172   return false;
173 }
174
175 /// Given a signed integer type and a set of known zero and one bits, compute
176 /// the maximum and minimum values that could have the specified known zero and
177 /// known one bits, returning them in Min/Max.
178 static void computeSignedMinMaxValuesFromKnownBits(const APInt &KnownZero,
179                                                    const APInt &KnownOne,
180                                                    APInt &Min, APInt &Max) {
181   assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() &&
182          KnownZero.getBitWidth() == Min.getBitWidth() &&
183          KnownZero.getBitWidth() == Max.getBitWidth() &&
184          "KnownZero, KnownOne and Min, Max must have equal bitwidth.");
185   APInt UnknownBits = ~(KnownZero|KnownOne);
186
187   // The minimum value is when all unknown bits are zeros, EXCEPT for the sign
188   // bit if it is unknown.
189   Min = KnownOne;
190   Max = KnownOne|UnknownBits;
191
192   if (UnknownBits.isNegative()) { // Sign bit is unknown
193     Min.setBit(Min.getBitWidth()-1);
194     Max.clearBit(Max.getBitWidth()-1);
195   }
196 }
197
198 /// Given an unsigned integer type and a set of known zero and one bits, compute
199 /// the maximum and minimum values that could have the specified known zero and
200 /// known one bits, returning them in Min/Max.
201 static void computeUnsignedMinMaxValuesFromKnownBits(const APInt &KnownZero,
202                                                      const APInt &KnownOne,
203                                                      APInt &Min, APInt &Max) {
204   assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() &&
205          KnownZero.getBitWidth() == Min.getBitWidth() &&
206          KnownZero.getBitWidth() == Max.getBitWidth() &&
207          "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth.");
208   APInt UnknownBits = ~(KnownZero|KnownOne);
209
210   // The minimum value is when the unknown bits are all zeros.
211   Min = KnownOne;
212   // The maximum value is when the unknown bits are all ones.
213   Max = KnownOne|UnknownBits;
214 }
215
216 /// This is called when we see this pattern:
217 ///   cmp pred (load (gep GV, ...)), cmpcst
218 /// where GV is a global variable with a constant initializer. Try to simplify
219 /// this into some simple computation that does not need the load. For example
220 /// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3".
221 ///
222 /// If AndCst is non-null, then the loaded value is masked with that constant
223 /// before doing the comparison. This handles cases like "A[i]&4 == 0".
224 Instruction *InstCombiner::foldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
225                                                         GlobalVariable *GV,
226                                                         CmpInst &ICI,
227                                                         ConstantInt *AndCst) {
228   Constant *Init = GV->getInitializer();
229   if (!isa<ConstantArray>(Init) && !isa<ConstantDataArray>(Init))
230     return nullptr;
231
232   uint64_t ArrayElementCount = Init->getType()->getArrayNumElements();
233   if (ArrayElementCount > 1024) return nullptr; // Don't blow up on huge arrays.
234
235   // There are many forms of this optimization we can handle, for now, just do
236   // the simple index into a single-dimensional array.
237   //
238   // Require: GEP GV, 0, i {{, constant indices}}
239   if (GEP->getNumOperands() < 3 ||
240       !isa<ConstantInt>(GEP->getOperand(1)) ||
241       !cast<ConstantInt>(GEP->getOperand(1))->isZero() ||
242       isa<Constant>(GEP->getOperand(2)))
243     return nullptr;
244
245   // Check that indices after the variable are constants and in-range for the
246   // type they index.  Collect the indices.  This is typically for arrays of
247   // structs.
248   SmallVector<unsigned, 4> LaterIndices;
249
250   Type *EltTy = Init->getType()->getArrayElementType();
251   for (unsigned i = 3, e = GEP->getNumOperands(); i != e; ++i) {
252     ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(i));
253     if (!Idx) return nullptr;  // Variable index.
254
255     uint64_t IdxVal = Idx->getZExtValue();
256     if ((unsigned)IdxVal != IdxVal) return nullptr; // Too large array index.
257
258     if (StructType *STy = dyn_cast<StructType>(EltTy))
259       EltTy = STy->getElementType(IdxVal);
260     else if (ArrayType *ATy = dyn_cast<ArrayType>(EltTy)) {
261       if (IdxVal >= ATy->getNumElements()) return nullptr;
262       EltTy = ATy->getElementType();
263     } else {
264       return nullptr; // Unknown type.
265     }
266
267     LaterIndices.push_back(IdxVal);
268   }
269
270   enum { Overdefined = -3, Undefined = -2 };
271
272   // Variables for our state machines.
273
274   // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form
275   // "i == 47 | i == 87", where 47 is the first index the condition is true for,
276   // and 87 is the second (and last) index.  FirstTrueElement is -2 when
277   // undefined, otherwise set to the first true element.  SecondTrueElement is
278   // -2 when undefined, -3 when overdefined and >= 0 when that index is true.
279   int FirstTrueElement = Undefined, SecondTrueElement = Undefined;
280
281   // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the
282   // form "i != 47 & i != 87".  Same state transitions as for true elements.
283   int FirstFalseElement = Undefined, SecondFalseElement = Undefined;
284
285   /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these
286   /// define a state machine that triggers for ranges of values that the index
287   /// is true or false for.  This triggers on things like "abbbbc"[i] == 'b'.
288   /// This is -2 when undefined, -3 when overdefined, and otherwise the last
289   /// index in the range (inclusive).  We use -2 for undefined here because we
290   /// use relative comparisons and don't want 0-1 to match -1.
291   int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined;
292
293   // MagicBitvector - This is a magic bitvector where we set a bit if the
294   // comparison is true for element 'i'.  If there are 64 elements or less in
295   // the array, this will fully represent all the comparison results.
296   uint64_t MagicBitvector = 0;
297
298   // Scan the array and see if one of our patterns matches.
299   Constant *CompareRHS = cast<Constant>(ICI.getOperand(1));
300   for (unsigned i = 0, e = ArrayElementCount; i != e; ++i) {
301     Constant *Elt = Init->getAggregateElement(i);
302     if (!Elt) return nullptr;
303
304     // If this is indexing an array of structures, get the structure element.
305     if (!LaterIndices.empty())
306       Elt = ConstantExpr::getExtractValue(Elt, LaterIndices);
307
308     // If the element is masked, handle it.
309     if (AndCst) Elt = ConstantExpr::getAnd(Elt, AndCst);
310
311     // Find out if the comparison would be true or false for the i'th element.
312     Constant *C = ConstantFoldCompareInstOperands(ICI.getPredicate(), Elt,
313                                                   CompareRHS, DL, &TLI);
314     // If the result is undef for this element, ignore it.
315     if (isa<UndefValue>(C)) {
316       // Extend range state machines to cover this element in case there is an
317       // undef in the middle of the range.
318       if (TrueRangeEnd == (int)i-1)
319         TrueRangeEnd = i;
320       if (FalseRangeEnd == (int)i-1)
321         FalseRangeEnd = i;
322       continue;
323     }
324
325     // If we can't compute the result for any of the elements, we have to give
326     // up evaluating the entire conditional.
327     if (!isa<ConstantInt>(C)) return nullptr;
328
329     // Otherwise, we know if the comparison is true or false for this element,
330     // update our state machines.
331     bool IsTrueForElt = !cast<ConstantInt>(C)->isZero();
332
333     // State machine for single/double/range index comparison.
334     if (IsTrueForElt) {
335       // Update the TrueElement state machine.
336       if (FirstTrueElement == Undefined)
337         FirstTrueElement = TrueRangeEnd = i;  // First true element.
338       else {
339         // Update double-compare state machine.
340         if (SecondTrueElement == Undefined)
341           SecondTrueElement = i;
342         else
343           SecondTrueElement = Overdefined;
344
345         // Update range state machine.
346         if (TrueRangeEnd == (int)i-1)
347           TrueRangeEnd = i;
348         else
349           TrueRangeEnd = Overdefined;
350       }
351     } else {
352       // Update the FalseElement state machine.
353       if (FirstFalseElement == Undefined)
354         FirstFalseElement = FalseRangeEnd = i; // First false element.
355       else {
356         // Update double-compare state machine.
357         if (SecondFalseElement == Undefined)
358           SecondFalseElement = i;
359         else
360           SecondFalseElement = Overdefined;
361
362         // Update range state machine.
363         if (FalseRangeEnd == (int)i-1)
364           FalseRangeEnd = i;
365         else
366           FalseRangeEnd = Overdefined;
367       }
368     }
369
370     // If this element is in range, update our magic bitvector.
371     if (i < 64 && IsTrueForElt)
372       MagicBitvector |= 1ULL << i;
373
374     // If all of our states become overdefined, bail out early.  Since the
375     // predicate is expensive, only check it every 8 elements.  This is only
376     // really useful for really huge arrays.
377     if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined &&
378         SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined &&
379         FalseRangeEnd == Overdefined)
380       return nullptr;
381   }
382
383   // Now that we've scanned the entire array, emit our new comparison(s).  We
384   // order the state machines in complexity of the generated code.
385   Value *Idx = GEP->getOperand(2);
386
387   // If the index is larger than the pointer size of the target, truncate the
388   // index down like the GEP would do implicitly.  We don't have to do this for
389   // an inbounds GEP because the index can't be out of range.
390   if (!GEP->isInBounds()) {
391     Type *IntPtrTy = DL.getIntPtrType(GEP->getType());
392     unsigned PtrSize = IntPtrTy->getIntegerBitWidth();
393     if (Idx->getType()->getPrimitiveSizeInBits() > PtrSize)
394       Idx = Builder->CreateTrunc(Idx, IntPtrTy);
395   }
396
397   // If the comparison is only true for one or two elements, emit direct
398   // comparisons.
399   if (SecondTrueElement != Overdefined) {
400     // None true -> false.
401     if (FirstTrueElement == Undefined)
402       return replaceInstUsesWith(ICI, Builder->getFalse());
403
404     Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement);
405
406     // True for one element -> 'i == 47'.
407     if (SecondTrueElement == Undefined)
408       return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx);
409
410     // True for two elements -> 'i == 47 | i == 72'.
411     Value *C1 = Builder->CreateICmpEQ(Idx, FirstTrueIdx);
412     Value *SecondTrueIdx = ConstantInt::get(Idx->getType(), SecondTrueElement);
413     Value *C2 = Builder->CreateICmpEQ(Idx, SecondTrueIdx);
414     return BinaryOperator::CreateOr(C1, C2);
415   }
416
417   // If the comparison is only false for one or two elements, emit direct
418   // comparisons.
419   if (SecondFalseElement != Overdefined) {
420     // None false -> true.
421     if (FirstFalseElement == Undefined)
422       return replaceInstUsesWith(ICI, Builder->getTrue());
423
424     Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement);
425
426     // False for one element -> 'i != 47'.
427     if (SecondFalseElement == Undefined)
428       return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx);
429
430     // False for two elements -> 'i != 47 & i != 72'.
431     Value *C1 = Builder->CreateICmpNE(Idx, FirstFalseIdx);
432     Value *SecondFalseIdx = ConstantInt::get(Idx->getType(),SecondFalseElement);
433     Value *C2 = Builder->CreateICmpNE(Idx, SecondFalseIdx);
434     return BinaryOperator::CreateAnd(C1, C2);
435   }
436
437   // If the comparison can be replaced with a range comparison for the elements
438   // where it is true, emit the range check.
439   if (TrueRangeEnd != Overdefined) {
440     assert(TrueRangeEnd != FirstTrueElement && "Should emit single compare");
441
442     // Generate (i-FirstTrue) <u (TrueRangeEnd-FirstTrue+1).
443     if (FirstTrueElement) {
444       Value *Offs = ConstantInt::get(Idx->getType(), -FirstTrueElement);
445       Idx = Builder->CreateAdd(Idx, Offs);
446     }
447
448     Value *End = ConstantInt::get(Idx->getType(),
449                                   TrueRangeEnd-FirstTrueElement+1);
450     return new ICmpInst(ICmpInst::ICMP_ULT, Idx, End);
451   }
452
453   // False range check.
454   if (FalseRangeEnd != Overdefined) {
455     assert(FalseRangeEnd != FirstFalseElement && "Should emit single compare");
456     // Generate (i-FirstFalse) >u (FalseRangeEnd-FirstFalse).
457     if (FirstFalseElement) {
458       Value *Offs = ConstantInt::get(Idx->getType(), -FirstFalseElement);
459       Idx = Builder->CreateAdd(Idx, Offs);
460     }
461
462     Value *End = ConstantInt::get(Idx->getType(),
463                                   FalseRangeEnd-FirstFalseElement);
464     return new ICmpInst(ICmpInst::ICMP_UGT, Idx, End);
465   }
466
467   // If a magic bitvector captures the entire comparison state
468   // of this load, replace it with computation that does:
469   //   ((magic_cst >> i) & 1) != 0
470   {
471     Type *Ty = nullptr;
472
473     // Look for an appropriate type:
474     // - The type of Idx if the magic fits
475     // - The smallest fitting legal type if we have a DataLayout
476     // - Default to i32
477     if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth())
478       Ty = Idx->getType();
479     else
480       Ty = DL.getSmallestLegalIntType(Init->getContext(), ArrayElementCount);
481
482     if (Ty) {
483       Value *V = Builder->CreateIntCast(Idx, Ty, false);
484       V = Builder->CreateLShr(ConstantInt::get(Ty, MagicBitvector), V);
485       V = Builder->CreateAnd(ConstantInt::get(Ty, 1), V);
486       return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));
487     }
488   }
489
490   return nullptr;
491 }
492
493 /// Return a value that can be used to compare the *offset* implied by a GEP to
494 /// zero. For example, if we have &A[i], we want to return 'i' for
495 /// "icmp ne i, 0". Note that, in general, indices can be complex, and scales
496 /// are involved. The above expression would also be legal to codegen as
497 /// "icmp ne (i*4), 0" (assuming A is a pointer to i32).
498 /// This latter form is less amenable to optimization though, and we are allowed
499 /// to generate the first by knowing that pointer arithmetic doesn't overflow.
500 ///
501 /// If we can't emit an optimized form for this expression, this returns null.
502 ///
503 static Value *evaluateGEPOffsetExpression(User *GEP, InstCombiner &IC,
504                                           const DataLayout &DL) {
505   gep_type_iterator GTI = gep_type_begin(GEP);
506
507   // Check to see if this gep only has a single variable index.  If so, and if
508   // any constant indices are a multiple of its scale, then we can compute this
509   // in terms of the scale of the variable index.  For example, if the GEP
510   // implies an offset of "12 + i*4", then we can codegen this as "3 + i",
511   // because the expression will cross zero at the same point.
512   unsigned i, e = GEP->getNumOperands();
513   int64_t Offset = 0;
514   for (i = 1; i != e; ++i, ++GTI) {
515     if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
516       // Compute the aggregate offset of constant indices.
517       if (CI->isZero()) continue;
518
519       // Handle a struct index, which adds its field offset to the pointer.
520       if (StructType *STy = GTI.getStructTypeOrNull()) {
521         Offset += DL.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
522       } else {
523         uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType());
524         Offset += Size*CI->getSExtValue();
525       }
526     } else {
527       // Found our variable index.
528       break;
529     }
530   }
531
532   // If there are no variable indices, we must have a constant offset, just
533   // evaluate it the general way.
534   if (i == e) return nullptr;
535
536   Value *VariableIdx = GEP->getOperand(i);
537   // Determine the scale factor of the variable element.  For example, this is
538   // 4 if the variable index is into an array of i32.
539   uint64_t VariableScale = DL.getTypeAllocSize(GTI.getIndexedType());
540
541   // Verify that there are no other variable indices.  If so, emit the hard way.
542   for (++i, ++GTI; i != e; ++i, ++GTI) {
543     ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i));
544     if (!CI) return nullptr;
545
546     // Compute the aggregate offset of constant indices.
547     if (CI->isZero()) continue;
548
549     // Handle a struct index, which adds its field offset to the pointer.
550     if (StructType *STy = GTI.getStructTypeOrNull()) {
551       Offset += DL.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
552     } else {
553       uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType());
554       Offset += Size*CI->getSExtValue();
555     }
556   }
557
558   // Okay, we know we have a single variable index, which must be a
559   // pointer/array/vector index.  If there is no offset, life is simple, return
560   // the index.
561   Type *IntPtrTy = DL.getIntPtrType(GEP->getOperand(0)->getType());
562   unsigned IntPtrWidth = IntPtrTy->getIntegerBitWidth();
563   if (Offset == 0) {
564     // Cast to intptrty in case a truncation occurs.  If an extension is needed,
565     // we don't need to bother extending: the extension won't affect where the
566     // computation crosses zero.
567     if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth) {
568       VariableIdx = IC.Builder->CreateTrunc(VariableIdx, IntPtrTy);
569     }
570     return VariableIdx;
571   }
572
573   // Otherwise, there is an index.  The computation we will do will be modulo
574   // the pointer size, so get it.
575   uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth);
576
577   Offset &= PtrSizeMask;
578   VariableScale &= PtrSizeMask;
579
580   // To do this transformation, any constant index must be a multiple of the
581   // variable scale factor.  For example, we can evaluate "12 + 4*i" as "3 + i",
582   // but we can't evaluate "10 + 3*i" in terms of i.  Check that the offset is a
583   // multiple of the variable scale.
584   int64_t NewOffs = Offset / (int64_t)VariableScale;
585   if (Offset != NewOffs*(int64_t)VariableScale)
586     return nullptr;
587
588   // Okay, we can do this evaluation.  Start by converting the index to intptr.
589   if (VariableIdx->getType() != IntPtrTy)
590     VariableIdx = IC.Builder->CreateIntCast(VariableIdx, IntPtrTy,
591                                             true /*Signed*/);
592   Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs);
593   return IC.Builder->CreateAdd(VariableIdx, OffsetVal, "offset");
594 }
595
596 /// Returns true if we can rewrite Start as a GEP with pointer Base
597 /// and some integer offset. The nodes that need to be re-written
598 /// for this transformation will be added to Explored.
599 static bool canRewriteGEPAsOffset(Value *Start, Value *Base,
600                                   const DataLayout &DL,
601                                   SetVector<Value *> &Explored) {
602   SmallVector<Value *, 16> WorkList(1, Start);
603   Explored.insert(Base);
604
605   // The following traversal gives us an order which can be used
606   // when doing the final transformation. Since in the final
607   // transformation we create the PHI replacement instructions first,
608   // we don't have to get them in any particular order.
609   //
610   // However, for other instructions we will have to traverse the
611   // operands of an instruction first, which means that we have to
612   // do a post-order traversal.
613   while (!WorkList.empty()) {
614     SetVector<PHINode *> PHIs;
615
616     while (!WorkList.empty()) {
617       if (Explored.size() >= 100)
618         return false;
619
620       Value *V = WorkList.back();
621
622       if (Explored.count(V) != 0) {
623         WorkList.pop_back();
624         continue;
625       }
626
627       if (!isa<IntToPtrInst>(V) && !isa<PtrToIntInst>(V) &&
628           !isa<GetElementPtrInst>(V) && !isa<PHINode>(V))
629         // We've found some value that we can't explore which is different from
630         // the base. Therefore we can't do this transformation.
631         return false;
632
633       if (isa<IntToPtrInst>(V) || isa<PtrToIntInst>(V)) {
634         auto *CI = dyn_cast<CastInst>(V);
635         if (!CI->isNoopCast(DL))
636           return false;
637
638         if (Explored.count(CI->getOperand(0)) == 0)
639           WorkList.push_back(CI->getOperand(0));
640       }
641
642       if (auto *GEP = dyn_cast<GEPOperator>(V)) {
643         // We're limiting the GEP to having one index. This will preserve
644         // the original pointer type. We could handle more cases in the
645         // future.
646         if (GEP->getNumIndices() != 1 || !GEP->isInBounds() ||
647             GEP->getType() != Start->getType())
648           return false;
649
650         if (Explored.count(GEP->getOperand(0)) == 0)
651           WorkList.push_back(GEP->getOperand(0));
652       }
653
654       if (WorkList.back() == V) {
655         WorkList.pop_back();
656         // We've finished visiting this node, mark it as such.
657         Explored.insert(V);
658       }
659
660       if (auto *PN = dyn_cast<PHINode>(V)) {
661         // We cannot transform PHIs on unsplittable basic blocks.
662         if (isa<CatchSwitchInst>(PN->getParent()->getTerminator()))
663           return false;
664         Explored.insert(PN);
665         PHIs.insert(PN);
666       }
667     }
668
669     // Explore the PHI nodes further.
670     for (auto *PN : PHIs)
671       for (Value *Op : PN->incoming_values())
672         if (Explored.count(Op) == 0)
673           WorkList.push_back(Op);
674   }
675
676   // Make sure that we can do this. Since we can't insert GEPs in a basic
677   // block before a PHI node, we can't easily do this transformation if
678   // we have PHI node users of transformed instructions.
679   for (Value *Val : Explored) {
680     for (Value *Use : Val->uses()) {
681
682       auto *PHI = dyn_cast<PHINode>(Use);
683       auto *Inst = dyn_cast<Instruction>(Val);
684
685       if (Inst == Base || Inst == PHI || !Inst || !PHI ||
686           Explored.count(PHI) == 0)
687         continue;
688
689       if (PHI->getParent() == Inst->getParent())
690         return false;
691     }
692   }
693   return true;
694 }
695
696 // Sets the appropriate insert point on Builder where we can add
697 // a replacement Instruction for V (if that is possible).
698 static void setInsertionPoint(IRBuilder<> &Builder, Value *V,
699                               bool Before = true) {
700   if (auto *PHI = dyn_cast<PHINode>(V)) {
701     Builder.SetInsertPoint(&*PHI->getParent()->getFirstInsertionPt());
702     return;
703   }
704   if (auto *I = dyn_cast<Instruction>(V)) {
705     if (!Before)
706       I = &*std::next(I->getIterator());
707     Builder.SetInsertPoint(I);
708     return;
709   }
710   if (auto *A = dyn_cast<Argument>(V)) {
711     // Set the insertion point in the entry block.
712     BasicBlock &Entry = A->getParent()->getEntryBlock();
713     Builder.SetInsertPoint(&*Entry.getFirstInsertionPt());
714     return;
715   }
716   // Otherwise, this is a constant and we don't need to set a new
717   // insertion point.
718   assert(isa<Constant>(V) && "Setting insertion point for unknown value!");
719 }
720
721 /// Returns a re-written value of Start as an indexed GEP using Base as a
722 /// pointer.
723 static Value *rewriteGEPAsOffset(Value *Start, Value *Base,
724                                  const DataLayout &DL,
725                                  SetVector<Value *> &Explored) {
726   // Perform all the substitutions. This is a bit tricky because we can
727   // have cycles in our use-def chains.
728   // 1. Create the PHI nodes without any incoming values.
729   // 2. Create all the other values.
730   // 3. Add the edges for the PHI nodes.
731   // 4. Emit GEPs to get the original pointers.
732   // 5. Remove the original instructions.
733   Type *IndexType = IntegerType::get(
734       Base->getContext(), DL.getPointerTypeSizeInBits(Start->getType()));
735
736   DenseMap<Value *, Value *> NewInsts;
737   NewInsts[Base] = ConstantInt::getNullValue(IndexType);
738
739   // Create the new PHI nodes, without adding any incoming values.
740   for (Value *Val : Explored) {
741     if (Val == Base)
742       continue;
743     // Create empty phi nodes. This avoids cyclic dependencies when creating
744     // the remaining instructions.
745     if (auto *PHI = dyn_cast<PHINode>(Val))
746       NewInsts[PHI] = PHINode::Create(IndexType, PHI->getNumIncomingValues(),
747                                       PHI->getName() + ".idx", PHI);
748   }
749   IRBuilder<> Builder(Base->getContext());
750
751   // Create all the other instructions.
752   for (Value *Val : Explored) {
753
754     if (NewInsts.find(Val) != NewInsts.end())
755       continue;
756
757     if (auto *CI = dyn_cast<CastInst>(Val)) {
758       NewInsts[CI] = NewInsts[CI->getOperand(0)];
759       continue;
760     }
761     if (auto *GEP = dyn_cast<GEPOperator>(Val)) {
762       Value *Index = NewInsts[GEP->getOperand(1)] ? NewInsts[GEP->getOperand(1)]
763                                                   : GEP->getOperand(1);
764       setInsertionPoint(Builder, GEP);
765       // Indices might need to be sign extended. GEPs will magically do
766       // this, but we need to do it ourselves here.
767       if (Index->getType()->getScalarSizeInBits() !=
768           NewInsts[GEP->getOperand(0)]->getType()->getScalarSizeInBits()) {
769         Index = Builder.CreateSExtOrTrunc(
770             Index, NewInsts[GEP->getOperand(0)]->getType(),
771             GEP->getOperand(0)->getName() + ".sext");
772       }
773
774       auto *Op = NewInsts[GEP->getOperand(0)];
775       if (isa<ConstantInt>(Op) && dyn_cast<ConstantInt>(Op)->isZero())
776         NewInsts[GEP] = Index;
777       else
778         NewInsts[GEP] = Builder.CreateNSWAdd(
779             Op, Index, GEP->getOperand(0)->getName() + ".add");
780       continue;
781     }
782     if (isa<PHINode>(Val))
783       continue;
784
785     llvm_unreachable("Unexpected instruction type");
786   }
787
788   // Add the incoming values to the PHI nodes.
789   for (Value *Val : Explored) {
790     if (Val == Base)
791       continue;
792     // All the instructions have been created, we can now add edges to the
793     // phi nodes.
794     if (auto *PHI = dyn_cast<PHINode>(Val)) {
795       PHINode *NewPhi = static_cast<PHINode *>(NewInsts[PHI]);
796       for (unsigned I = 0, E = PHI->getNumIncomingValues(); I < E; ++I) {
797         Value *NewIncoming = PHI->getIncomingValue(I);
798
799         if (NewInsts.find(NewIncoming) != NewInsts.end())
800           NewIncoming = NewInsts[NewIncoming];
801
802         NewPhi->addIncoming(NewIncoming, PHI->getIncomingBlock(I));
803       }
804     }
805   }
806
807   for (Value *Val : Explored) {
808     if (Val == Base)
809       continue;
810
811     // Depending on the type, for external users we have to emit
812     // a GEP or a GEP + ptrtoint.
813     setInsertionPoint(Builder, Val, false);
814
815     // If required, create an inttoptr instruction for Base.
816     Value *NewBase = Base;
817     if (!Base->getType()->isPointerTy())
818       NewBase = Builder.CreateBitOrPointerCast(Base, Start->getType(),
819                                                Start->getName() + "to.ptr");
820
821     Value *GEP = Builder.CreateInBoundsGEP(
822         Start->getType()->getPointerElementType(), NewBase,
823         makeArrayRef(NewInsts[Val]), Val->getName() + ".ptr");
824
825     if (!Val->getType()->isPointerTy()) {
826       Value *Cast = Builder.CreatePointerCast(GEP, Val->getType(),
827                                               Val->getName() + ".conv");
828       GEP = Cast;
829     }
830     Val->replaceAllUsesWith(GEP);
831   }
832
833   return NewInsts[Start];
834 }
835
836 /// Looks through GEPs, IntToPtrInsts and PtrToIntInsts in order to express
837 /// the input Value as a constant indexed GEP. Returns a pair containing
838 /// the GEPs Pointer and Index.
839 static std::pair<Value *, Value *>
840 getAsConstantIndexedAddress(Value *V, const DataLayout &DL) {
841   Type *IndexType = IntegerType::get(V->getContext(),
842                                      DL.getPointerTypeSizeInBits(V->getType()));
843
844   Constant *Index = ConstantInt::getNullValue(IndexType);
845   while (true) {
846     if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
847       // We accept only inbouds GEPs here to exclude the possibility of
848       // overflow.
849       if (!GEP->isInBounds())
850         break;
851       if (GEP->hasAllConstantIndices() && GEP->getNumIndices() == 1 &&
852           GEP->getType() == V->getType()) {
853         V = GEP->getOperand(0);
854         Constant *GEPIndex = static_cast<Constant *>(GEP->getOperand(1));
855         Index = ConstantExpr::getAdd(
856             Index, ConstantExpr::getSExtOrBitCast(GEPIndex, IndexType));
857         continue;
858       }
859       break;
860     }
861     if (auto *CI = dyn_cast<IntToPtrInst>(V)) {
862       if (!CI->isNoopCast(DL))
863         break;
864       V = CI->getOperand(0);
865       continue;
866     }
867     if (auto *CI = dyn_cast<PtrToIntInst>(V)) {
868       if (!CI->isNoopCast(DL))
869         break;
870       V = CI->getOperand(0);
871       continue;
872     }
873     break;
874   }
875   return {V, Index};
876 }
877
878 /// Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.
879 /// We can look through PHIs, GEPs and casts in order to determine a common base
880 /// between GEPLHS and RHS.
881 static Instruction *transformToIndexedCompare(GEPOperator *GEPLHS, Value *RHS,
882                                               ICmpInst::Predicate Cond,
883                                               const DataLayout &DL) {
884   if (!GEPLHS->hasAllConstantIndices())
885     return nullptr;
886
887   // Make sure the pointers have the same type.
888   if (GEPLHS->getType() != RHS->getType())
889     return nullptr;
890
891   Value *PtrBase, *Index;
892   std::tie(PtrBase, Index) = getAsConstantIndexedAddress(GEPLHS, DL);
893
894   // The set of nodes that will take part in this transformation.
895   SetVector<Value *> Nodes;
896
897   if (!canRewriteGEPAsOffset(RHS, PtrBase, DL, Nodes))
898     return nullptr;
899
900   // We know we can re-write this as
901   //  ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2)
902   // Since we've only looked through inbouds GEPs we know that we
903   // can't have overflow on either side. We can therefore re-write
904   // this as:
905   //   OFFSET1 cmp OFFSET2
906   Value *NewRHS = rewriteGEPAsOffset(RHS, PtrBase, DL, Nodes);
907
908   // RewriteGEPAsOffset has replaced RHS and all of its uses with a re-written
909   // GEP having PtrBase as the pointer base, and has returned in NewRHS the
910   // offset. Since Index is the offset of LHS to the base pointer, we will now
911   // compare the offsets instead of comparing the pointers.
912   return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Index, NewRHS);
913 }
914
915 /// Fold comparisons between a GEP instruction and something else. At this point
916 /// we know that the GEP is on the LHS of the comparison.
917 Instruction *InstCombiner::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
918                                        ICmpInst::Predicate Cond,
919                                        Instruction &I) {
920   // Don't transform signed compares of GEPs into index compares. Even if the
921   // GEP is inbounds, the final add of the base pointer can have signed overflow
922   // and would change the result of the icmp.
923   // e.g. "&foo[0] <s &foo[1]" can't be folded to "true" because "foo" could be
924   // the maximum signed value for the pointer type.
925   if (ICmpInst::isSigned(Cond))
926     return nullptr;
927
928   // Look through bitcasts and addrspacecasts. We do not however want to remove
929   // 0 GEPs.
930   if (!isa<GetElementPtrInst>(RHS))
931     RHS = RHS->stripPointerCasts();
932
933   Value *PtrBase = GEPLHS->getOperand(0);
934   if (PtrBase == RHS && GEPLHS->isInBounds()) {
935     // ((gep Ptr, OFFSET) cmp Ptr)   ---> (OFFSET cmp 0).
936     // This transformation (ignoring the base and scales) is valid because we
937     // know pointers can't overflow since the gep is inbounds.  See if we can
938     // output an optimized form.
939     Value *Offset = evaluateGEPOffsetExpression(GEPLHS, *this, DL);
940
941     // If not, synthesize the offset the hard way.
942     if (!Offset)
943       Offset = EmitGEPOffset(GEPLHS);
944     return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset,
945                         Constant::getNullValue(Offset->getType()));
946   } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {
947     // If the base pointers are different, but the indices are the same, just
948     // compare the base pointer.
949     if (PtrBase != GEPRHS->getOperand(0)) {
950       bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands();
951       IndicesTheSame &= GEPLHS->getOperand(0)->getType() ==
952                         GEPRHS->getOperand(0)->getType();
953       if (IndicesTheSame)
954         for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
955           if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
956             IndicesTheSame = false;
957             break;
958           }
959
960       // If all indices are the same, just compare the base pointers.
961       if (IndicesTheSame)
962         return new ICmpInst(Cond, GEPLHS->getOperand(0), GEPRHS->getOperand(0));
963
964       // If we're comparing GEPs with two base pointers that only differ in type
965       // and both GEPs have only constant indices or just one use, then fold
966       // the compare with the adjusted indices.
967       if (GEPLHS->isInBounds() && GEPRHS->isInBounds() &&
968           (GEPLHS->hasAllConstantIndices() || GEPLHS->hasOneUse()) &&
969           (GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) &&
970           PtrBase->stripPointerCasts() ==
971               GEPRHS->getOperand(0)->stripPointerCasts()) {
972         Value *LOffset = EmitGEPOffset(GEPLHS);
973         Value *ROffset = EmitGEPOffset(GEPRHS);
974
975         // If we looked through an addrspacecast between different sized address
976         // spaces, the LHS and RHS pointers are different sized
977         // integers. Truncate to the smaller one.
978         Type *LHSIndexTy = LOffset->getType();
979         Type *RHSIndexTy = ROffset->getType();
980         if (LHSIndexTy != RHSIndexTy) {
981           if (LHSIndexTy->getPrimitiveSizeInBits() <
982               RHSIndexTy->getPrimitiveSizeInBits()) {
983             ROffset = Builder->CreateTrunc(ROffset, LHSIndexTy);
984           } else
985             LOffset = Builder->CreateTrunc(LOffset, RHSIndexTy);
986         }
987
988         Value *Cmp = Builder->CreateICmp(ICmpInst::getSignedPredicate(Cond),
989                                          LOffset, ROffset);
990         return replaceInstUsesWith(I, Cmp);
991       }
992
993       // Otherwise, the base pointers are different and the indices are
994       // different. Try convert this to an indexed compare by looking through
995       // PHIs/casts.
996       return transformToIndexedCompare(GEPLHS, RHS, Cond, DL);
997     }
998
999     // If one of the GEPs has all zero indices, recurse.
1000     if (GEPLHS->hasAllZeroIndices())
1001       return foldGEPICmp(GEPRHS, GEPLHS->getOperand(0),
1002                          ICmpInst::getSwappedPredicate(Cond), I);
1003
1004     // If the other GEP has all zero indices, recurse.
1005     if (GEPRHS->hasAllZeroIndices())
1006       return foldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I);
1007
1008     bool GEPsInBounds = GEPLHS->isInBounds() && GEPRHS->isInBounds();
1009     if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) {
1010       // If the GEPs only differ by one index, compare it.
1011       unsigned NumDifferences = 0;  // Keep track of # differences.
1012       unsigned DiffOperand = 0;     // The operand that differs.
1013       for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
1014         if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
1015           if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() !=
1016                    GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) {
1017             // Irreconcilable differences.
1018             NumDifferences = 2;
1019             break;
1020           } else {
1021             if (NumDifferences++) break;
1022             DiffOperand = i;
1023           }
1024         }
1025
1026       if (NumDifferences == 0)   // SAME GEP?
1027         return replaceInstUsesWith(I, // No comparison is needed here.
1028                              Builder->getInt1(ICmpInst::isTrueWhenEqual(Cond)));
1029
1030       else if (NumDifferences == 1 && GEPsInBounds) {
1031         Value *LHSV = GEPLHS->getOperand(DiffOperand);
1032         Value *RHSV = GEPRHS->getOperand(DiffOperand);
1033         // Make sure we do a signed comparison here.
1034         return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV);
1035       }
1036     }
1037
1038     // Only lower this if the icmp is the only user of the GEP or if we expect
1039     // the result to fold to a constant!
1040     if (GEPsInBounds && (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) &&
1041         (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) {
1042       // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2)  --->  (OFFSET1 cmp OFFSET2)
1043       Value *L = EmitGEPOffset(GEPLHS);
1044       Value *R = EmitGEPOffset(GEPRHS);
1045       return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R);
1046     }
1047   }
1048
1049   // Try convert this to an indexed compare by looking through PHIs/casts as a
1050   // last resort.
1051   return transformToIndexedCompare(GEPLHS, RHS, Cond, DL);
1052 }
1053
1054 Instruction *InstCombiner::foldAllocaCmp(ICmpInst &ICI,
1055                                          const AllocaInst *Alloca,
1056                                          const Value *Other) {
1057   assert(ICI.isEquality() && "Cannot fold non-equality comparison.");
1058
1059   // It would be tempting to fold away comparisons between allocas and any
1060   // pointer not based on that alloca (e.g. an argument). However, even
1061   // though such pointers cannot alias, they can still compare equal.
1062   //
1063   // But LLVM doesn't specify where allocas get their memory, so if the alloca
1064   // doesn't escape we can argue that it's impossible to guess its value, and we
1065   // can therefore act as if any such guesses are wrong.
1066   //
1067   // The code below checks that the alloca doesn't escape, and that it's only
1068   // used in a comparison once (the current instruction). The
1069   // single-comparison-use condition ensures that we're trivially folding all
1070   // comparisons against the alloca consistently, and avoids the risk of
1071   // erroneously folding a comparison of the pointer with itself.
1072
1073   unsigned MaxIter = 32; // Break cycles and bound to constant-time.
1074
1075   SmallVector<const Use *, 32> Worklist;
1076   for (const Use &U : Alloca->uses()) {
1077     if (Worklist.size() >= MaxIter)
1078       return nullptr;
1079     Worklist.push_back(&U);
1080   }
1081
1082   unsigned NumCmps = 0;
1083   while (!Worklist.empty()) {
1084     assert(Worklist.size() <= MaxIter);
1085     const Use *U = Worklist.pop_back_val();
1086     const Value *V = U->getUser();
1087     --MaxIter;
1088
1089     if (isa<BitCastInst>(V) || isa<GetElementPtrInst>(V) || isa<PHINode>(V) ||
1090         isa<SelectInst>(V)) {
1091       // Track the uses.
1092     } else if (isa<LoadInst>(V)) {
1093       // Loading from the pointer doesn't escape it.
1094       continue;
1095     } else if (const auto *SI = dyn_cast<StoreInst>(V)) {
1096       // Storing *to* the pointer is fine, but storing the pointer escapes it.
1097       if (SI->getValueOperand() == U->get())
1098         return nullptr;
1099       continue;
1100     } else if (isa<ICmpInst>(V)) {
1101       if (NumCmps++)
1102         return nullptr; // Found more than one cmp.
1103       continue;
1104     } else if (const auto *Intrin = dyn_cast<IntrinsicInst>(V)) {
1105       switch (Intrin->getIntrinsicID()) {
1106         // These intrinsics don't escape or compare the pointer. Memset is safe
1107         // because we don't allow ptrtoint. Memcpy and memmove are safe because
1108         // we don't allow stores, so src cannot point to V.
1109         case Intrinsic::lifetime_start: case Intrinsic::lifetime_end:
1110         case Intrinsic::dbg_declare: case Intrinsic::dbg_value:
1111         case Intrinsic::memcpy: case Intrinsic::memmove: case Intrinsic::memset:
1112           continue;
1113         default:
1114           return nullptr;
1115       }
1116     } else {
1117       return nullptr;
1118     }
1119     for (const Use &U : V->uses()) {
1120       if (Worklist.size() >= MaxIter)
1121         return nullptr;
1122       Worklist.push_back(&U);
1123     }
1124   }
1125
1126   Type *CmpTy = CmpInst::makeCmpResultType(Other->getType());
1127   return replaceInstUsesWith(
1128       ICI,
1129       ConstantInt::get(CmpTy, !CmpInst::isTrueWhenEqual(ICI.getPredicate())));
1130 }
1131
1132 /// Fold "icmp pred (X+CI), X".
1133 Instruction *InstCombiner::foldICmpAddOpConst(Instruction &ICI,
1134                                               Value *X, ConstantInt *CI,
1135                                               ICmpInst::Predicate Pred) {
1136   // From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0,
1137   // so the values can never be equal.  Similarly for all other "or equals"
1138   // operators.
1139
1140   // (X+1) <u X        --> X >u (MAXUINT-1)        --> X == 255
1141   // (X+2) <u X        --> X >u (MAXUINT-2)        --> X > 253
1142   // (X+MAXUINT) <u X  --> X >u (MAXUINT-MAXUINT)  --> X != 0
1143   if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
1144     Value *R =
1145       ConstantExpr::getSub(ConstantInt::getAllOnesValue(CI->getType()), CI);
1146     return new ICmpInst(ICmpInst::ICMP_UGT, X, R);
1147   }
1148
1149   // (X+1) >u X        --> X <u (0-1)        --> X != 255
1150   // (X+2) >u X        --> X <u (0-2)        --> X <u 254
1151   // (X+MAXUINT) >u X  --> X <u (0-MAXUINT)  --> X <u 1  --> X == 0
1152   if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
1153     return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantExpr::getNeg(CI));
1154
1155   unsigned BitWidth = CI->getType()->getPrimitiveSizeInBits();
1156   ConstantInt *SMax = ConstantInt::get(X->getContext(),
1157                                        APInt::getSignedMaxValue(BitWidth));
1158
1159   // (X+ 1) <s X       --> X >s (MAXSINT-1)          --> X == 127
1160   // (X+ 2) <s X       --> X >s (MAXSINT-2)          --> X >s 125
1161   // (X+MAXSINT) <s X  --> X >s (MAXSINT-MAXSINT)    --> X >s 0
1162   // (X+MINSINT) <s X  --> X >s (MAXSINT-MINSINT)    --> X >s -1
1163   // (X+ -2) <s X      --> X >s (MAXSINT- -2)        --> X >s 126
1164   // (X+ -1) <s X      --> X >s (MAXSINT- -1)        --> X != 127
1165   if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
1166     return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantExpr::getSub(SMax, CI));
1167
1168   // (X+ 1) >s X       --> X <s (MAXSINT-(1-1))       --> X != 127
1169   // (X+ 2) >s X       --> X <s (MAXSINT-(2-1))       --> X <s 126
1170   // (X+MAXSINT) >s X  --> X <s (MAXSINT-(MAXSINT-1)) --> X <s 1
1171   // (X+MINSINT) >s X  --> X <s (MAXSINT-(MINSINT-1)) --> X <s -2
1172   // (X+ -2) >s X      --> X <s (MAXSINT-(-2-1))      --> X <s -126
1173   // (X+ -1) >s X      --> X <s (MAXSINT-(-1-1))      --> X == -128
1174
1175   assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE);
1176   Constant *C = Builder->getInt(CI->getValue()-1);
1177   return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantExpr::getSub(SMax, C));
1178 }
1179
1180 /// Handle "(icmp eq/ne (ashr/lshr AP2, A), AP1)" ->
1181 /// (icmp eq/ne A, Log2(AP2/AP1)) ->
1182 /// (icmp eq/ne A, Log2(AP2) - Log2(AP1)).
1183 Instruction *InstCombiner::foldICmpShrConstConst(ICmpInst &I, Value *A,
1184                                                  const APInt &AP1,
1185                                                  const APInt &AP2) {
1186   assert(I.isEquality() && "Cannot fold icmp gt/lt");
1187
1188   auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
1189     if (I.getPredicate() == I.ICMP_NE)
1190       Pred = CmpInst::getInversePredicate(Pred);
1191     return new ICmpInst(Pred, LHS, RHS);
1192   };
1193
1194   // Don't bother doing any work for cases which InstSimplify handles.
1195   if (AP2 == 0)
1196     return nullptr;
1197
1198   bool IsAShr = isa<AShrOperator>(I.getOperand(0));
1199   if (IsAShr) {
1200     if (AP2.isAllOnesValue())
1201       return nullptr;
1202     if (AP2.isNegative() != AP1.isNegative())
1203       return nullptr;
1204     if (AP2.sgt(AP1))
1205       return nullptr;
1206   }
1207
1208   if (!AP1)
1209     // 'A' must be large enough to shift out the highest set bit.
1210     return getICmp(I.ICMP_UGT, A,
1211                    ConstantInt::get(A->getType(), AP2.logBase2()));
1212
1213   if (AP1 == AP2)
1214     return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));
1215
1216   int Shift;
1217   if (IsAShr && AP1.isNegative())
1218     Shift = AP1.countLeadingOnes() - AP2.countLeadingOnes();
1219   else
1220     Shift = AP1.countLeadingZeros() - AP2.countLeadingZeros();
1221
1222   if (Shift > 0) {
1223     if (IsAShr && AP1 == AP2.ashr(Shift)) {
1224       // There are multiple solutions if we are comparing against -1 and the LHS
1225       // of the ashr is not a power of two.
1226       if (AP1.isAllOnesValue() && !AP2.isPowerOf2())
1227         return getICmp(I.ICMP_UGE, A, ConstantInt::get(A->getType(), Shift));
1228       return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1229     } else if (AP1 == AP2.lshr(Shift)) {
1230       return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1231     }
1232   }
1233
1234   // Shifting const2 will never be equal to const1.
1235   // FIXME: This should always be handled by InstSimplify?
1236   auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE);
1237   return replaceInstUsesWith(I, TorF);
1238 }
1239
1240 /// Handle "(icmp eq/ne (shl AP2, A), AP1)" ->
1241 /// (icmp eq/ne A, TrailingZeros(AP1) - TrailingZeros(AP2)).
1242 Instruction *InstCombiner::foldICmpShlConstConst(ICmpInst &I, Value *A,
1243                                                  const APInt &AP1,
1244                                                  const APInt &AP2) {
1245   assert(I.isEquality() && "Cannot fold icmp gt/lt");
1246
1247   auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
1248     if (I.getPredicate() == I.ICMP_NE)
1249       Pred = CmpInst::getInversePredicate(Pred);
1250     return new ICmpInst(Pred, LHS, RHS);
1251   };
1252
1253   // Don't bother doing any work for cases which InstSimplify handles.
1254   if (AP2 == 0)
1255     return nullptr;
1256
1257   unsigned AP2TrailingZeros = AP2.countTrailingZeros();
1258
1259   if (!AP1 && AP2TrailingZeros != 0)
1260     return getICmp(
1261         I.ICMP_UGE, A,
1262         ConstantInt::get(A->getType(), AP2.getBitWidth() - AP2TrailingZeros));
1263
1264   if (AP1 == AP2)
1265     return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));
1266
1267   // Get the distance between the lowest bits that are set.
1268   int Shift = AP1.countTrailingZeros() - AP2TrailingZeros;
1269
1270   if (Shift > 0 && AP2.shl(Shift) == AP1)
1271     return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
1272
1273   // Shifting const2 will never be equal to const1.
1274   // FIXME: This should always be handled by InstSimplify?
1275   auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE);
1276   return replaceInstUsesWith(I, TorF);
1277 }
1278
1279 /// The caller has matched a pattern of the form:
1280 ///   I = icmp ugt (add (add A, B), CI2), CI1
1281 /// If this is of the form:
1282 ///   sum = a + b
1283 ///   if (sum+128 >u 255)
1284 /// Then replace it with llvm.sadd.with.overflow.i8.
1285 ///
1286 static Instruction *processUGT_ADDCST_ADD(ICmpInst &I, Value *A, Value *B,
1287                                           ConstantInt *CI2, ConstantInt *CI1,
1288                                           InstCombiner &IC) {
1289   // The transformation we're trying to do here is to transform this into an
1290   // llvm.sadd.with.overflow.  To do this, we have to replace the original add
1291   // with a narrower add, and discard the add-with-constant that is part of the
1292   // range check (if we can't eliminate it, this isn't profitable).
1293
1294   // In order to eliminate the add-with-constant, the compare can be its only
1295   // use.
1296   Instruction *AddWithCst = cast<Instruction>(I.getOperand(0));
1297   if (!AddWithCst->hasOneUse())
1298     return nullptr;
1299
1300   // If CI2 is 2^7, 2^15, 2^31, then it might be an sadd.with.overflow.
1301   if (!CI2->getValue().isPowerOf2())
1302     return nullptr;
1303   unsigned NewWidth = CI2->getValue().countTrailingZeros();
1304   if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31)
1305     return nullptr;
1306
1307   // The width of the new add formed is 1 more than the bias.
1308   ++NewWidth;
1309
1310   // Check to see that CI1 is an all-ones value with NewWidth bits.
1311   if (CI1->getBitWidth() == NewWidth ||
1312       CI1->getValue() != APInt::getLowBitsSet(CI1->getBitWidth(), NewWidth))
1313     return nullptr;
1314
1315   // This is only really a signed overflow check if the inputs have been
1316   // sign-extended; check for that condition. For example, if CI2 is 2^31 and
1317   // the operands of the add are 64 bits wide, we need at least 33 sign bits.
1318   unsigned NeededSignBits = CI1->getBitWidth() - NewWidth + 1;
1319   if (IC.ComputeNumSignBits(A, 0, &I) < NeededSignBits ||
1320       IC.ComputeNumSignBits(B, 0, &I) < NeededSignBits)
1321     return nullptr;
1322
1323   // In order to replace the original add with a narrower
1324   // llvm.sadd.with.overflow, the only uses allowed are the add-with-constant
1325   // and truncates that discard the high bits of the add.  Verify that this is
1326   // the case.
1327   Instruction *OrigAdd = cast<Instruction>(AddWithCst->getOperand(0));
1328   for (User *U : OrigAdd->users()) {
1329     if (U == AddWithCst)
1330       continue;
1331
1332     // Only accept truncates for now.  We would really like a nice recursive
1333     // predicate like SimplifyDemandedBits, but which goes downwards the use-def
1334     // chain to see which bits of a value are actually demanded.  If the
1335     // original add had another add which was then immediately truncated, we
1336     // could still do the transformation.
1337     TruncInst *TI = dyn_cast<TruncInst>(U);
1338     if (!TI || TI->getType()->getPrimitiveSizeInBits() > NewWidth)
1339       return nullptr;
1340   }
1341
1342   // If the pattern matches, truncate the inputs to the narrower type and
1343   // use the sadd_with_overflow intrinsic to efficiently compute both the
1344   // result and the overflow bit.
1345   Type *NewType = IntegerType::get(OrigAdd->getContext(), NewWidth);
1346   Value *F = Intrinsic::getDeclaration(I.getModule(),
1347                                        Intrinsic::sadd_with_overflow, NewType);
1348
1349   InstCombiner::BuilderTy *Builder = IC.Builder;
1350
1351   // Put the new code above the original add, in case there are any uses of the
1352   // add between the add and the compare.
1353   Builder->SetInsertPoint(OrigAdd);
1354
1355   Value *TruncA = Builder->CreateTrunc(A, NewType, A->getName() + ".trunc");
1356   Value *TruncB = Builder->CreateTrunc(B, NewType, B->getName() + ".trunc");
1357   CallInst *Call = Builder->CreateCall(F, {TruncA, TruncB}, "sadd");
1358   Value *Add = Builder->CreateExtractValue(Call, 0, "sadd.result");
1359   Value *ZExt = Builder->CreateZExt(Add, OrigAdd->getType());
1360
1361   // The inner add was the result of the narrow add, zero extended to the
1362   // wider type.  Replace it with the result computed by the intrinsic.
1363   IC.replaceInstUsesWith(*OrigAdd, ZExt);
1364
1365   // The original icmp gets replaced with the overflow value.
1366   return ExtractValueInst::Create(Call, 1, "sadd.overflow");
1367 }
1368
1369 // Fold icmp Pred X, C.
1370 Instruction *InstCombiner::foldICmpWithConstant(ICmpInst &Cmp) {
1371   CmpInst::Predicate Pred = Cmp.getPredicate();
1372   Value *X = Cmp.getOperand(0);
1373
1374   const APInt *C;
1375   if (!match(Cmp.getOperand(1), m_APInt(C)))
1376     return nullptr;
1377
1378   Value *A = nullptr, *B = nullptr;
1379
1380   // Match the following pattern, which is a common idiom when writing
1381   // overflow-safe integer arithmetic functions. The source performs an addition
1382   // in wider type and explicitly checks for overflow using comparisons against
1383   // INT_MIN and INT_MAX. Simplify by using the sadd_with_overflow intrinsic.
1384   //
1385   // TODO: This could probably be generalized to handle other overflow-safe
1386   // operations if we worked out the formulas to compute the appropriate magic
1387   // constants.
1388   //
1389   // sum = a + b
1390   // if (sum+128 >u 255)  ...  -> llvm.sadd.with.overflow.i8
1391   {
1392     ConstantInt *CI2; // I = icmp ugt (add (add A, B), CI2), CI
1393     if (Pred == ICmpInst::ICMP_UGT &&
1394         match(X, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2))))
1395       if (Instruction *Res = processUGT_ADDCST_ADD(
1396               Cmp, A, B, CI2, cast<ConstantInt>(Cmp.getOperand(1)), *this))
1397         return Res;
1398   }
1399
1400   // (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0)
1401   if (*C == 0 && Pred == ICmpInst::ICMP_SGT) {
1402     SelectPatternResult SPR = matchSelectPattern(X, A, B);
1403     if (SPR.Flavor == SPF_SMIN) {
1404       if (isKnownPositive(A, DL))
1405         return new ICmpInst(Pred, B, Cmp.getOperand(1));
1406       if (isKnownPositive(B, DL))
1407         return new ICmpInst(Pred, A, Cmp.getOperand(1));
1408     }
1409   }
1410
1411   // FIXME: Use m_APInt to allow folds for splat constants.
1412   ConstantInt *CI = dyn_cast<ConstantInt>(Cmp.getOperand(1));
1413   if (!CI)
1414     return nullptr;
1415
1416   // Canonicalize icmp instructions based on dominating conditions.
1417   BasicBlock *Parent = Cmp.getParent();
1418   BasicBlock *Dom = Parent->getSinglePredecessor();
1419   auto *BI = Dom ? dyn_cast<BranchInst>(Dom->getTerminator()) : nullptr;
1420   ICmpInst::Predicate Pred2;
1421   BasicBlock *TrueBB, *FalseBB;
1422   ConstantInt *CI2;
1423   if (BI && match(BI, m_Br(m_ICmp(Pred2, m_Specific(X), m_ConstantInt(CI2)),
1424                            TrueBB, FalseBB)) &&
1425       TrueBB != FalseBB) {
1426     ConstantRange CR =
1427         ConstantRange::makeAllowedICmpRegion(Pred, CI->getValue());
1428     ConstantRange DominatingCR =
1429         (Parent == TrueBB)
1430             ? ConstantRange::makeExactICmpRegion(Pred2, CI2->getValue())
1431             : ConstantRange::makeExactICmpRegion(
1432                   CmpInst::getInversePredicate(Pred2), CI2->getValue());
1433     ConstantRange Intersection = DominatingCR.intersectWith(CR);
1434     ConstantRange Difference = DominatingCR.difference(CR);
1435     if (Intersection.isEmptySet())
1436       return replaceInstUsesWith(Cmp, Builder->getFalse());
1437     if (Difference.isEmptySet())
1438       return replaceInstUsesWith(Cmp, Builder->getTrue());
1439
1440     // If this is a normal comparison, it demands all bits. If it is a sign
1441     // bit comparison, it only demands the sign bit.
1442     bool UnusedBit;
1443     bool IsSignBit = isSignBitCheck(Pred, CI->getValue(), UnusedBit);
1444
1445     // Canonicalizing a sign bit comparison that gets used in a branch,
1446     // pessimizes codegen by generating branch on zero instruction instead
1447     // of a test and branch. So we avoid canonicalizing in such situations
1448     // because test and branch instruction has better branch displacement
1449     // than compare and branch instruction.
1450     if (!isBranchOnSignBitCheck(Cmp, IsSignBit) && !Cmp.isEquality()) {
1451       if (auto *AI = Intersection.getSingleElement())
1452         return new ICmpInst(ICmpInst::ICMP_EQ, X, Builder->getInt(*AI));
1453       if (auto *AD = Difference.getSingleElement())
1454         return new ICmpInst(ICmpInst::ICMP_NE, X, Builder->getInt(*AD));
1455     }
1456   }
1457
1458   return nullptr;
1459 }
1460
1461 /// Fold icmp (trunc X, Y), C.
1462 Instruction *InstCombiner::foldICmpTruncConstant(ICmpInst &Cmp,
1463                                                  Instruction *Trunc,
1464                                                  const APInt *C) {
1465   ICmpInst::Predicate Pred = Cmp.getPredicate();
1466   Value *X = Trunc->getOperand(0);
1467   if (*C == 1 && C->getBitWidth() > 1) {
1468     // icmp slt trunc(signum(V)) 1 --> icmp slt V, 1
1469     Value *V = nullptr;
1470     if (Pred == ICmpInst::ICMP_SLT && match(X, m_Signum(m_Value(V))))
1471       return new ICmpInst(ICmpInst::ICMP_SLT, V,
1472                           ConstantInt::get(V->getType(), 1));
1473   }
1474
1475   if (Cmp.isEquality() && Trunc->hasOneUse()) {
1476     // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
1477     // of the high bits truncated out of x are known.
1478     unsigned DstBits = Trunc->getType()->getScalarSizeInBits(),
1479              SrcBits = X->getType()->getScalarSizeInBits();
1480     APInt KnownZero(SrcBits, 0), KnownOne(SrcBits, 0);
1481     computeKnownBits(X, KnownZero, KnownOne, 0, &Cmp);
1482
1483     // If all the high bits are known, we can do this xform.
1484     if ((KnownZero | KnownOne).countLeadingOnes() >= SrcBits - DstBits) {
1485       // Pull in the high bits from known-ones set.
1486       APInt NewRHS = C->zext(SrcBits);
1487       NewRHS |= KnownOne & APInt::getHighBitsSet(SrcBits, SrcBits - DstBits);
1488       return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), NewRHS));
1489     }
1490   }
1491
1492   return nullptr;
1493 }
1494
1495 /// Fold icmp (xor X, Y), C.
1496 Instruction *InstCombiner::foldICmpXorConstant(ICmpInst &Cmp,
1497                                                BinaryOperator *Xor,
1498                                                const APInt *C) {
1499   Value *X = Xor->getOperand(0);
1500   Value *Y = Xor->getOperand(1);
1501   const APInt *XorC;
1502   if (!match(Y, m_APInt(XorC)))
1503     return nullptr;
1504
1505   // If this is a comparison that tests the signbit (X < 0) or (x > -1),
1506   // fold the xor.
1507   ICmpInst::Predicate Pred = Cmp.getPredicate();
1508   if ((Pred == ICmpInst::ICMP_SLT && *C == 0) ||
1509       (Pred == ICmpInst::ICMP_SGT && C->isAllOnesValue())) {
1510
1511     // If the sign bit of the XorCst is not set, there is no change to
1512     // the operation, just stop using the Xor.
1513     if (!XorC->isNegative()) {
1514       Cmp.setOperand(0, X);
1515       Worklist.Add(Xor);
1516       return &Cmp;
1517     }
1518
1519     // Was the old condition true if the operand is positive?
1520     bool isTrueIfPositive = Pred == ICmpInst::ICMP_SGT;
1521
1522     // If so, the new one isn't.
1523     isTrueIfPositive ^= true;
1524
1525     Constant *CmpConstant = cast<Constant>(Cmp.getOperand(1));
1526     if (isTrueIfPositive)
1527       return new ICmpInst(ICmpInst::ICMP_SGT, X, SubOne(CmpConstant));
1528     else
1529       return new ICmpInst(ICmpInst::ICMP_SLT, X, AddOne(CmpConstant));
1530   }
1531
1532   if (Xor->hasOneUse()) {
1533     // (icmp u/s (xor X SignBit), C) -> (icmp s/u X, (xor C SignBit))
1534     if (!Cmp.isEquality() && XorC->isSignBit()) {
1535       Pred = Cmp.isSigned() ? Cmp.getUnsignedPredicate()
1536                             : Cmp.getSignedPredicate();
1537       return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), *C ^ *XorC));
1538     }
1539
1540     // (icmp u/s (xor X ~SignBit), C) -> (icmp s/u X, (xor C ~SignBit))
1541     if (!Cmp.isEquality() && XorC->isMaxSignedValue()) {
1542       Pred = Cmp.isSigned() ? Cmp.getUnsignedPredicate()
1543                             : Cmp.getSignedPredicate();
1544       Pred = Cmp.getSwappedPredicate(Pred);
1545       return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), *C ^ *XorC));
1546     }
1547   }
1548
1549   // (icmp ugt (xor X, C), ~C) -> (icmp ult X, C)
1550   //   iff -C is a power of 2
1551   if (Pred == ICmpInst::ICMP_UGT && *XorC == ~(*C) && (*C + 1).isPowerOf2())
1552     return new ICmpInst(ICmpInst::ICMP_ULT, X, Y);
1553
1554   // (icmp ult (xor X, C), -C) -> (icmp uge X, C)
1555   //   iff -C is a power of 2
1556   if (Pred == ICmpInst::ICMP_ULT && *XorC == -(*C) && C->isPowerOf2())
1557     return new ICmpInst(ICmpInst::ICMP_UGE, X, Y);
1558
1559   return nullptr;
1560 }
1561
1562 /// Fold icmp (and (sh X, Y), C2), C1.
1563 Instruction *InstCombiner::foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And,
1564                                             const APInt *C1, const APInt *C2) {
1565   BinaryOperator *Shift = dyn_cast<BinaryOperator>(And->getOperand(0));
1566   if (!Shift || !Shift->isShift())
1567     return nullptr;
1568
1569   // If this is: (X >> C3) & C2 != C1 (where any shift and any compare could
1570   // exist), turn it into (X & (C2 << C3)) != (C1 << C3). This happens a LOT in
1571   // code produced by the clang front-end, for bitfield access.
1572   // This seemingly simple opportunity to fold away a shift turns out to be
1573   // rather complicated. See PR17827 for details.
1574   unsigned ShiftOpcode = Shift->getOpcode();
1575   bool IsShl = ShiftOpcode == Instruction::Shl;
1576   const APInt *C3;
1577   if (match(Shift->getOperand(1), m_APInt(C3))) {
1578     bool CanFold = false;
1579     if (ShiftOpcode == Instruction::AShr) {
1580       // There may be some constraints that make this possible, but nothing
1581       // simple has been discovered yet.
1582       CanFold = false;
1583     } else if (ShiftOpcode == Instruction::Shl) {
1584       // For a left shift, we can fold if the comparison is not signed. We can
1585       // also fold a signed comparison if the mask value and comparison value
1586       // are not negative. These constraints may not be obvious, but we can
1587       // prove that they are correct using an SMT solver.
1588       if (!Cmp.isSigned() || (!C2->isNegative() && !C1->isNegative()))
1589         CanFold = true;
1590     } else if (ShiftOpcode == Instruction::LShr) {
1591       // For a logical right shift, we can fold if the comparison is not signed.
1592       // We can also fold a signed comparison if the shifted mask value and the
1593       // shifted comparison value are not negative. These constraints may not be
1594       // obvious, but we can prove that they are correct using an SMT solver.
1595       if (!Cmp.isSigned() ||
1596           (!C2->shl(*C3).isNegative() && !C1->shl(*C3).isNegative()))
1597         CanFold = true;
1598     }
1599
1600     if (CanFold) {
1601       APInt NewCst = IsShl ? C1->lshr(*C3) : C1->shl(*C3);
1602       APInt SameAsC1 = IsShl ? NewCst.shl(*C3) : NewCst.lshr(*C3);
1603       // Check to see if we are shifting out any of the bits being compared.
1604       if (SameAsC1 != *C1) {
1605         // If we shifted bits out, the fold is not going to work out. As a
1606         // special case, check to see if this means that the result is always
1607         // true or false now.
1608         if (Cmp.getPredicate() == ICmpInst::ICMP_EQ)
1609           return replaceInstUsesWith(Cmp, ConstantInt::getFalse(Cmp.getType()));
1610         if (Cmp.getPredicate() == ICmpInst::ICMP_NE)
1611           return replaceInstUsesWith(Cmp, ConstantInt::getTrue(Cmp.getType()));
1612       } else {
1613         Cmp.setOperand(1, ConstantInt::get(And->getType(), NewCst));
1614         APInt NewAndCst = IsShl ? C2->lshr(*C3) : C2->shl(*C3);
1615         And->setOperand(1, ConstantInt::get(And->getType(), NewAndCst));
1616         And->setOperand(0, Shift->getOperand(0));
1617         Worklist.Add(Shift); // Shift is dead.
1618         return &Cmp;
1619       }
1620     }
1621   }
1622
1623   // Turn ((X >> Y) & C2) == 0  into  (X & (C2 << Y)) == 0.  The latter is
1624   // preferable because it allows the C2 << Y expression to be hoisted out of a
1625   // loop if Y is invariant and X is not.
1626   if (Shift->hasOneUse() && *C1 == 0 && Cmp.isEquality() &&
1627       !Shift->isArithmeticShift() && !isa<Constant>(Shift->getOperand(0))) {
1628     // Compute C2 << Y.
1629     Value *NewShift =
1630         IsShl ? Builder->CreateLShr(And->getOperand(1), Shift->getOperand(1))
1631               : Builder->CreateShl(And->getOperand(1), Shift->getOperand(1));
1632
1633     // Compute X & (C2 << Y).
1634     Value *NewAnd = Builder->CreateAnd(Shift->getOperand(0), NewShift);
1635     Cmp.setOperand(0, NewAnd);
1636     return &Cmp;
1637   }
1638
1639   return nullptr;
1640 }
1641
1642 /// Fold icmp (and X, C2), C1.
1643 Instruction *InstCombiner::foldICmpAndConstConst(ICmpInst &Cmp,
1644                                                  BinaryOperator *And,
1645                                                  const APInt *C1) {
1646   const APInt *C2;
1647   if (!match(And->getOperand(1), m_APInt(C2)))
1648     return nullptr;
1649
1650   if (!And->hasOneUse() || !And->getOperand(0)->hasOneUse())
1651     return nullptr;
1652
1653   // If the LHS is an 'and' of a truncate and we can widen the and/compare to
1654   // the input width without changing the value produced, eliminate the cast:
1655   //
1656   // icmp (and (trunc W), C2), C1 -> icmp (and W, C2'), C1'
1657   //
1658   // We can do this transformation if the constants do not have their sign bits
1659   // set or if it is an equality comparison. Extending a relational comparison
1660   // when we're checking the sign bit would not work.
1661   Value *W;
1662   if (match(And->getOperand(0), m_Trunc(m_Value(W))) &&
1663       (Cmp.isEquality() || (!C1->isNegative() && !C2->isNegative()))) {
1664     // TODO: Is this a good transform for vectors? Wider types may reduce
1665     // throughput. Should this transform be limited (even for scalars) by using
1666     // ShouldChangeType()?
1667     if (!Cmp.getType()->isVectorTy()) {
1668       Type *WideType = W->getType();
1669       unsigned WideScalarBits = WideType->getScalarSizeInBits();
1670       Constant *ZextC1 = ConstantInt::get(WideType, C1->zext(WideScalarBits));
1671       Constant *ZextC2 = ConstantInt::get(WideType, C2->zext(WideScalarBits));
1672       Value *NewAnd = Builder->CreateAnd(W, ZextC2, And->getName());
1673       return new ICmpInst(Cmp.getPredicate(), NewAnd, ZextC1);
1674     }
1675   }
1676
1677   if (Instruction *I = foldICmpAndShift(Cmp, And, C1, C2))
1678     return I;
1679
1680   // (icmp pred (and (or (lshr A, B), A), 1), 0) -->
1681   // (icmp pred (and A, (or (shl 1, B), 1), 0))
1682   //
1683   // iff pred isn't signed
1684   if (!Cmp.isSigned() && *C1 == 0 && match(And->getOperand(1), m_One())) {
1685     Constant *One = cast<Constant>(And->getOperand(1));
1686     Value *Or = And->getOperand(0);
1687     Value *A, *B, *LShr;
1688     if (match(Or, m_Or(m_Value(LShr), m_Value(A))) &&
1689         match(LShr, m_LShr(m_Specific(A), m_Value(B)))) {
1690       unsigned UsesRemoved = 0;
1691       if (And->hasOneUse())
1692         ++UsesRemoved;
1693       if (Or->hasOneUse())
1694         ++UsesRemoved;
1695       if (LShr->hasOneUse())
1696         ++UsesRemoved;
1697
1698       // Compute A & ((1 << B) | 1)
1699       Value *NewOr = nullptr;
1700       if (auto *C = dyn_cast<Constant>(B)) {
1701         if (UsesRemoved >= 1)
1702           NewOr = ConstantExpr::getOr(ConstantExpr::getNUWShl(One, C), One);
1703       } else {
1704         if (UsesRemoved >= 3)
1705           NewOr = Builder->CreateOr(Builder->CreateShl(One, B, LShr->getName(),
1706                                                        /*HasNUW=*/true),
1707                                     One, Or->getName());
1708       }
1709       if (NewOr) {
1710         Value *NewAnd = Builder->CreateAnd(A, NewOr, And->getName());
1711         Cmp.setOperand(0, NewAnd);
1712         return &Cmp;
1713       }
1714     }
1715   }
1716
1717   // (X & C2) > C1 --> (X & C2) != 0, if any bit set in (X & C2) will produce a
1718   // result greater than C1.
1719   unsigned NumTZ = C2->countTrailingZeros();
1720   if (Cmp.getPredicate() == ICmpInst::ICMP_UGT && NumTZ < C2->getBitWidth() &&
1721       APInt::getOneBitSet(C2->getBitWidth(), NumTZ).ugt(*C1)) {
1722     Constant *Zero = Constant::getNullValue(And->getType());
1723     return new ICmpInst(ICmpInst::ICMP_NE, And, Zero);
1724   }
1725
1726   return nullptr;
1727 }
1728
1729 /// Fold icmp (and X, Y), C.
1730 Instruction *InstCombiner::foldICmpAndConstant(ICmpInst &Cmp,
1731                                                BinaryOperator *And,
1732                                                const APInt *C) {
1733   if (Instruction *I = foldICmpAndConstConst(Cmp, And, C))
1734     return I;
1735
1736   // TODO: These all require that Y is constant too, so refactor with the above.
1737
1738   // Try to optimize things like "A[i] & 42 == 0" to index computations.
1739   Value *X = And->getOperand(0);
1740   Value *Y = And->getOperand(1);
1741   if (auto *LI = dyn_cast<LoadInst>(X))
1742     if (auto *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)))
1743       if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
1744         if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
1745             !LI->isVolatile() && isa<ConstantInt>(Y)) {
1746           ConstantInt *C2 = cast<ConstantInt>(Y);
1747           if (Instruction *Res = foldCmpLoadFromIndexedGlobal(GEP, GV, Cmp, C2))
1748             return Res;
1749         }
1750
1751   if (!Cmp.isEquality())
1752     return nullptr;
1753
1754   // X & -C == -C -> X >  u ~C
1755   // X & -C != -C -> X <= u ~C
1756   //   iff C is a power of 2
1757   if (Cmp.getOperand(1) == Y && (-(*C)).isPowerOf2()) {
1758     auto NewPred = Cmp.getPredicate() == CmpInst::ICMP_EQ ? CmpInst::ICMP_UGT
1759                                                           : CmpInst::ICMP_ULE;
1760     return new ICmpInst(NewPred, X, SubOne(cast<Constant>(Cmp.getOperand(1))));
1761   }
1762
1763   // (X & C2) == 0 -> (trunc X) >= 0
1764   // (X & C2) != 0 -> (trunc X) <  0
1765   //   iff C2 is a power of 2 and it masks the sign bit of a legal integer type.
1766   const APInt *C2;
1767   if (And->hasOneUse() && *C == 0 && match(Y, m_APInt(C2))) {
1768     int32_t ExactLogBase2 = C2->exactLogBase2();
1769     if (ExactLogBase2 != -1 && DL.isLegalInteger(ExactLogBase2 + 1)) {
1770       Type *NTy = IntegerType::get(Cmp.getContext(), ExactLogBase2 + 1);
1771       if (And->getType()->isVectorTy())
1772         NTy = VectorType::get(NTy, And->getType()->getVectorNumElements());
1773       Value *Trunc = Builder->CreateTrunc(X, NTy);
1774       auto NewPred = Cmp.getPredicate() == CmpInst::ICMP_EQ ? CmpInst::ICMP_SGE
1775                                                             : CmpInst::ICMP_SLT;
1776       return new ICmpInst(NewPred, Trunc, Constant::getNullValue(NTy));
1777     }
1778   }
1779
1780   return nullptr;
1781 }
1782
1783 /// Fold icmp (or X, Y), C.
1784 Instruction *InstCombiner::foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or,
1785                                               const APInt *C) {
1786   ICmpInst::Predicate Pred = Cmp.getPredicate();
1787   if (*C == 1) {
1788     // icmp slt signum(V) 1 --> icmp slt V, 1
1789     Value *V = nullptr;
1790     if (Pred == ICmpInst::ICMP_SLT && match(Or, m_Signum(m_Value(V))))
1791       return new ICmpInst(ICmpInst::ICMP_SLT, V,
1792                           ConstantInt::get(V->getType(), 1));
1793   }
1794
1795   if (!Cmp.isEquality() || *C != 0 || !Or->hasOneUse())
1796     return nullptr;
1797
1798   Value *P, *Q;
1799   if (match(Or, m_Or(m_PtrToInt(m_Value(P)), m_PtrToInt(m_Value(Q))))) {
1800     // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0
1801     // -> and (icmp eq P, null), (icmp eq Q, null).
1802     Value *CmpP =
1803         Builder->CreateICmp(Pred, P, ConstantInt::getNullValue(P->getType()));
1804     Value *CmpQ =
1805         Builder->CreateICmp(Pred, Q, ConstantInt::getNullValue(Q->getType()));
1806     auto LogicOpc = Pred == ICmpInst::Predicate::ICMP_EQ ? Instruction::And
1807                                                          : Instruction::Or;
1808     return BinaryOperator::Create(LogicOpc, CmpP, CmpQ);
1809   }
1810
1811   return nullptr;
1812 }
1813
1814 /// Fold icmp (mul X, Y), C.
1815 Instruction *InstCombiner::foldICmpMulConstant(ICmpInst &Cmp,
1816                                                BinaryOperator *Mul,
1817                                                const APInt *C) {
1818   const APInt *MulC;
1819   if (!match(Mul->getOperand(1), m_APInt(MulC)))
1820     return nullptr;
1821
1822   // If this is a test of the sign bit and the multiply is sign-preserving with
1823   // a constant operand, use the multiply LHS operand instead.
1824   ICmpInst::Predicate Pred = Cmp.getPredicate();
1825   if (isSignTest(Pred, *C) && Mul->hasNoSignedWrap()) {
1826     if (MulC->isNegative())
1827       Pred = ICmpInst::getSwappedPredicate(Pred);
1828     return new ICmpInst(Pred, Mul->getOperand(0),
1829                         Constant::getNullValue(Mul->getType()));
1830   }
1831
1832   return nullptr;
1833 }
1834
1835 /// Fold icmp (shl 1, Y), C.
1836 static Instruction *foldICmpShlOne(ICmpInst &Cmp, Instruction *Shl,
1837                                    const APInt *C) {
1838   Value *Y;
1839   if (!match(Shl, m_Shl(m_One(), m_Value(Y))))
1840     return nullptr;
1841
1842   Type *ShiftType = Shl->getType();
1843   uint32_t TypeBits = C->getBitWidth();
1844   bool CIsPowerOf2 = C->isPowerOf2();
1845   ICmpInst::Predicate Pred = Cmp.getPredicate();
1846   if (Cmp.isUnsigned()) {
1847     // (1 << Y) pred C -> Y pred Log2(C)
1848     if (!CIsPowerOf2) {
1849       // (1 << Y) <  30 -> Y <= 4
1850       // (1 << Y) <= 30 -> Y <= 4
1851       // (1 << Y) >= 30 -> Y >  4
1852       // (1 << Y) >  30 -> Y >  4
1853       if (Pred == ICmpInst::ICMP_ULT)
1854         Pred = ICmpInst::ICMP_ULE;
1855       else if (Pred == ICmpInst::ICMP_UGE)
1856         Pred = ICmpInst::ICMP_UGT;
1857     }
1858
1859     // (1 << Y) >= 2147483648 -> Y >= 31 -> Y == 31
1860     // (1 << Y) <  2147483648 -> Y <  31 -> Y != 31
1861     unsigned CLog2 = C->logBase2();
1862     if (CLog2 == TypeBits - 1) {
1863       if (Pred == ICmpInst::ICMP_UGE)
1864         Pred = ICmpInst::ICMP_EQ;
1865       else if (Pred == ICmpInst::ICMP_ULT)
1866         Pred = ICmpInst::ICMP_NE;
1867     }
1868     return new ICmpInst(Pred, Y, ConstantInt::get(ShiftType, CLog2));
1869   } else if (Cmp.isSigned()) {
1870     Constant *BitWidthMinusOne = ConstantInt::get(ShiftType, TypeBits - 1);
1871     if (C->isAllOnesValue()) {
1872       // (1 << Y) <= -1 -> Y == 31
1873       if (Pred == ICmpInst::ICMP_SLE)
1874         return new ICmpInst(ICmpInst::ICMP_EQ, Y, BitWidthMinusOne);
1875
1876       // (1 << Y) >  -1 -> Y != 31
1877       if (Pred == ICmpInst::ICMP_SGT)
1878         return new ICmpInst(ICmpInst::ICMP_NE, Y, BitWidthMinusOne);
1879     } else if (!(*C)) {
1880       // (1 << Y) <  0 -> Y == 31
1881       // (1 << Y) <= 0 -> Y == 31
1882       if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
1883         return new ICmpInst(ICmpInst::ICMP_EQ, Y, BitWidthMinusOne);
1884
1885       // (1 << Y) >= 0 -> Y != 31
1886       // (1 << Y) >  0 -> Y != 31
1887       if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
1888         return new ICmpInst(ICmpInst::ICMP_NE, Y, BitWidthMinusOne);
1889     }
1890   } else if (Cmp.isEquality() && CIsPowerOf2) {
1891     return new ICmpInst(Pred, Y, ConstantInt::get(ShiftType, C->logBase2()));
1892   }
1893
1894   return nullptr;
1895 }
1896
1897 /// Fold icmp (shl X, Y), C.
1898 Instruction *InstCombiner::foldICmpShlConstant(ICmpInst &Cmp,
1899                                                BinaryOperator *Shl,
1900                                                const APInt *C) {
1901   const APInt *ShiftVal;
1902   if (Cmp.isEquality() && match(Shl->getOperand(0), m_APInt(ShiftVal)))
1903     return foldICmpShlConstConst(Cmp, Shl->getOperand(1), *C, *ShiftVal);
1904
1905   const APInt *ShiftAmt;
1906   if (!match(Shl->getOperand(1), m_APInt(ShiftAmt)))
1907     return foldICmpShlOne(Cmp, Shl, C);
1908
1909   // Check that the shift amount is in range. If not, don't perform undefined
1910   // shifts. When the shift is visited, it will be simplified.
1911   unsigned TypeBits = C->getBitWidth();
1912   if (ShiftAmt->uge(TypeBits))
1913     return nullptr;
1914
1915   ICmpInst::Predicate Pred = Cmp.getPredicate();
1916   Value *X = Shl->getOperand(0);
1917   if (Cmp.isEquality()) {
1918     // If the shift is NUW, then it is just shifting out zeros, no need for an
1919     // AND.
1920     Constant *LShrC = ConstantInt::get(Shl->getType(), C->lshr(*ShiftAmt));
1921     if (Shl->hasNoUnsignedWrap())
1922       return new ICmpInst(Pred, X, LShrC);
1923
1924     // If the shift is NSW and we compare to 0, then it is just shifting out
1925     // sign bits, no need for an AND either.
1926     if (Shl->hasNoSignedWrap() && *C == 0)
1927       return new ICmpInst(Pred, X, LShrC);
1928
1929     if (Shl->hasOneUse()) {
1930       // Otherwise, strength reduce the shift into an and.
1931       Constant *Mask = ConstantInt::get(Shl->getType(),
1932           APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt->getZExtValue()));
1933
1934       Value *And = Builder->CreateAnd(X, Mask, Shl->getName() + ".mask");
1935       return new ICmpInst(Pred, And, LShrC);
1936     }
1937   }
1938
1939   // If this is a signed comparison to 0 and the shift is sign preserving,
1940   // use the shift LHS operand instead; isSignTest may change 'Pred', so only
1941   // do that if we're sure to not continue on in this function.
1942   if (Shl->hasNoSignedWrap() && isSignTest(Pred, *C))
1943     return new ICmpInst(Pred, X, Constant::getNullValue(X->getType()));
1944
1945   // Otherwise, if this is a comparison of the sign bit, simplify to and/test.
1946   bool TrueIfSigned = false;
1947   if (Shl->hasOneUse() && isSignBitCheck(Pred, *C, TrueIfSigned)) {
1948     // (X << 31) <s 0  --> (X & 1) != 0
1949     Constant *Mask = ConstantInt::get(
1950         X->getType(),
1951         APInt::getOneBitSet(TypeBits, TypeBits - ShiftAmt->getZExtValue() - 1));
1952     Value *And = Builder->CreateAnd(X, Mask, Shl->getName() + ".mask");
1953     return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
1954                         And, Constant::getNullValue(And->getType()));
1955   }
1956
1957   // When the shift is nuw and pred is >u or <=u, comparison only really happens
1958   // in the pre-shifted bits. Since InstSimplify canonicalizes <=u into <u, the
1959   // <=u case can be further converted to match <u (see below).
1960   if (Shl->hasNoUnsignedWrap() &&
1961       (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT)) {
1962     // Derivation for the ult case:
1963     // (X << S) <=u C is equiv to X <=u (C >> S) for all C
1964     // (X << S) <u (C + 1) is equiv to X <u (C >> S) + 1 if C <u ~0u
1965     // (X << S) <u C is equiv to X <u ((C - 1) >> S) + 1 if C >u 0
1966     assert((Pred != ICmpInst::ICMP_ULT || C->ugt(0)) &&
1967            "Encountered `ult 0` that should have been eliminated by "
1968            "InstSimplify.");
1969     APInt ShiftedC = Pred == ICmpInst::ICMP_ULT ? (*C - 1).lshr(*ShiftAmt) + 1
1970                                                 : C->lshr(*ShiftAmt);
1971     return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), ShiftedC));
1972   }
1973
1974   // Transform (icmp pred iM (shl iM %v, N), C)
1975   // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (C>>N))
1976   // Transform the shl to a trunc if (trunc (C>>N)) has no loss and M-N.
1977   // This enables us to get rid of the shift in favor of a trunc that may be
1978   // free on the target. It has the additional benefit of comparing to a
1979   // smaller constant that may be more target-friendly.
1980   unsigned Amt = ShiftAmt->getLimitedValue(TypeBits - 1);
1981   if (Shl->hasOneUse() && Amt != 0 && C->countTrailingZeros() >= Amt &&
1982       DL.isLegalInteger(TypeBits - Amt)) {
1983     Type *TruncTy = IntegerType::get(Cmp.getContext(), TypeBits - Amt);
1984     if (X->getType()->isVectorTy())
1985       TruncTy = VectorType::get(TruncTy, X->getType()->getVectorNumElements());
1986     Constant *NewC =
1987         ConstantInt::get(TruncTy, C->ashr(*ShiftAmt).trunc(TypeBits - Amt));
1988     return new ICmpInst(Pred, Builder->CreateTrunc(X, TruncTy), NewC);
1989   }
1990
1991   return nullptr;
1992 }
1993
1994 /// Fold icmp ({al}shr X, Y), C.
1995 Instruction *InstCombiner::foldICmpShrConstant(ICmpInst &Cmp,
1996                                                BinaryOperator *Shr,
1997                                                const APInt *C) {
1998   // An exact shr only shifts out zero bits, so:
1999   // icmp eq/ne (shr X, Y), 0 --> icmp eq/ne X, 0
2000   Value *X = Shr->getOperand(0);
2001   CmpInst::Predicate Pred = Cmp.getPredicate();
2002   if (Cmp.isEquality() && Shr->isExact() && Shr->hasOneUse() && *C == 0)
2003     return new ICmpInst(Pred, X, Cmp.getOperand(1));
2004
2005   const APInt *ShiftVal;
2006   if (Cmp.isEquality() && match(Shr->getOperand(0), m_APInt(ShiftVal)))
2007     return foldICmpShrConstConst(Cmp, Shr->getOperand(1), *C, *ShiftVal);
2008
2009   const APInt *ShiftAmt;
2010   if (!match(Shr->getOperand(1), m_APInt(ShiftAmt)))
2011     return nullptr;
2012
2013   // Check that the shift amount is in range. If not, don't perform undefined
2014   // shifts. When the shift is visited it will be simplified.
2015   unsigned TypeBits = C->getBitWidth();
2016   unsigned ShAmtVal = ShiftAmt->getLimitedValue(TypeBits);
2017   if (ShAmtVal >= TypeBits || ShAmtVal == 0)
2018     return nullptr;
2019
2020   bool IsAShr = Shr->getOpcode() == Instruction::AShr;
2021   if (!Cmp.isEquality()) {
2022     // If we have an unsigned comparison and an ashr, we can't simplify this.
2023     // Similarly for signed comparisons with lshr.
2024     if (Cmp.isSigned() != IsAShr)
2025       return nullptr;
2026
2027     // Otherwise, all lshr and most exact ashr's are equivalent to a udiv/sdiv
2028     // by a power of 2.  Since we already have logic to simplify these,
2029     // transform to div and then simplify the resultant comparison.
2030     if (IsAShr && (!Shr->isExact() || ShAmtVal == TypeBits - 1))
2031       return nullptr;
2032
2033     // Revisit the shift (to delete it).
2034     Worklist.Add(Shr);
2035
2036     Constant *DivCst = ConstantInt::get(
2037         Shr->getType(), APInt::getOneBitSet(TypeBits, ShAmtVal));
2038
2039     Value *Tmp = IsAShr ? Builder->CreateSDiv(X, DivCst, "", Shr->isExact())
2040                         : Builder->CreateUDiv(X, DivCst, "", Shr->isExact());
2041
2042     Cmp.setOperand(0, Tmp);
2043
2044     // If the builder folded the binop, just return it.
2045     BinaryOperator *TheDiv = dyn_cast<BinaryOperator>(Tmp);
2046     if (!TheDiv)
2047       return &Cmp;
2048
2049     // Otherwise, fold this div/compare.
2050     assert(TheDiv->getOpcode() == Instruction::SDiv ||
2051            TheDiv->getOpcode() == Instruction::UDiv);
2052
2053     Instruction *Res = foldICmpDivConstant(Cmp, TheDiv, C);
2054     assert(Res && "This div/cst should have folded!");
2055     return Res;
2056   }
2057
2058   // Handle equality comparisons of shift-by-constant.
2059
2060   // If the comparison constant changes with the shift, the comparison cannot
2061   // succeed (bits of the comparison constant cannot match the shifted value).
2062   // This should be known by InstSimplify and already be folded to true/false.
2063   assert(((IsAShr && C->shl(ShAmtVal).ashr(ShAmtVal) == *C) ||
2064           (!IsAShr && C->shl(ShAmtVal).lshr(ShAmtVal) == *C)) &&
2065          "Expected icmp+shr simplify did not occur.");
2066
2067   // Check if the bits shifted out are known to be zero. If so, we can compare
2068   // against the unshifted value:
2069   //  (X & 4) >> 1 == 2  --> (X & 4) == 4.
2070   Constant *ShiftedCmpRHS = ConstantInt::get(Shr->getType(), *C << ShAmtVal);
2071   if (Shr->hasOneUse()) {
2072     if (Shr->isExact())
2073       return new ICmpInst(Pred, X, ShiftedCmpRHS);
2074
2075     // Otherwise strength reduce the shift into an 'and'.
2076     APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
2077     Constant *Mask = ConstantInt::get(Shr->getType(), Val);
2078     Value *And = Builder->CreateAnd(X, Mask, Shr->getName() + ".mask");
2079     return new ICmpInst(Pred, And, ShiftedCmpRHS);
2080   }
2081
2082   return nullptr;
2083 }
2084
2085 /// Fold icmp (udiv X, Y), C.
2086 Instruction *InstCombiner::foldICmpUDivConstant(ICmpInst &Cmp,
2087                                                 BinaryOperator *UDiv,
2088                                                 const APInt *C) {
2089   const APInt *C2;
2090   if (!match(UDiv->getOperand(0), m_APInt(C2)))
2091     return nullptr;
2092
2093   assert(C2 != 0 && "udiv 0, X should have been simplified already.");
2094
2095   // (icmp ugt (udiv C2, Y), C) -> (icmp ule Y, C2/(C+1))
2096   Value *Y = UDiv->getOperand(1);
2097   if (Cmp.getPredicate() == ICmpInst::ICMP_UGT) {
2098     assert(!C->isMaxValue() &&
2099            "icmp ugt X, UINT_MAX should have been simplified already.");
2100     return new ICmpInst(ICmpInst::ICMP_ULE, Y,
2101                         ConstantInt::get(Y->getType(), C2->udiv(*C + 1)));
2102   }
2103
2104   // (icmp ult (udiv C2, Y), C) -> (icmp ugt Y, C2/C)
2105   if (Cmp.getPredicate() == ICmpInst::ICMP_ULT) {
2106     assert(C != 0 && "icmp ult X, 0 should have been simplified already.");
2107     return new ICmpInst(ICmpInst::ICMP_UGT, Y,
2108                         ConstantInt::get(Y->getType(), C2->udiv(*C)));
2109   }
2110
2111   return nullptr;
2112 }
2113
2114 /// Fold icmp ({su}div X, Y), C.
2115 Instruction *InstCombiner::foldICmpDivConstant(ICmpInst &Cmp,
2116                                                BinaryOperator *Div,
2117                                                const APInt *C) {
2118   // Fold: icmp pred ([us]div X, C2), C -> range test
2119   // Fold this div into the comparison, producing a range check.
2120   // Determine, based on the divide type, what the range is being
2121   // checked.  If there is an overflow on the low or high side, remember
2122   // it, otherwise compute the range [low, hi) bounding the new value.
2123   // See: InsertRangeTest above for the kinds of replacements possible.
2124   const APInt *C2;
2125   if (!match(Div->getOperand(1), m_APInt(C2)))
2126     return nullptr;
2127
2128   // FIXME: If the operand types don't match the type of the divide
2129   // then don't attempt this transform. The code below doesn't have the
2130   // logic to deal with a signed divide and an unsigned compare (and
2131   // vice versa). This is because (x /s C2) <s C  produces different
2132   // results than (x /s C2) <u C or (x /u C2) <s C or even
2133   // (x /u C2) <u C.  Simply casting the operands and result won't
2134   // work. :(  The if statement below tests that condition and bails
2135   // if it finds it.
2136   bool DivIsSigned = Div->getOpcode() == Instruction::SDiv;
2137   if (!Cmp.isEquality() && DivIsSigned != Cmp.isSigned())
2138     return nullptr;
2139
2140   // The ProdOV computation fails on divide by 0 and divide by -1. Cases with
2141   // INT_MIN will also fail if the divisor is 1. Although folds of all these
2142   // division-by-constant cases should be present, we can not assert that they
2143   // have happened before we reach this icmp instruction.
2144   if (*C2 == 0 || *C2 == 1 || (DivIsSigned && C2->isAllOnesValue()))
2145     return nullptr;
2146
2147   // TODO: We could do all of the computations below using APInt.
2148   Constant *CmpRHS = cast<Constant>(Cmp.getOperand(1));
2149   Constant *DivRHS = cast<Constant>(Div->getOperand(1));
2150
2151   // Compute Prod = CmpRHS * DivRHS. We are essentially solving an equation of
2152   // form X / C2 = C. We solve for X by multiplying C2 (DivRHS) and C (CmpRHS).
2153   // By solving for X, we can turn this into a range check instead of computing
2154   // a divide.
2155   Constant *Prod = ConstantExpr::getMul(CmpRHS, DivRHS);
2156
2157   // Determine if the product overflows by seeing if the product is not equal to
2158   // the divide. Make sure we do the same kind of divide as in the LHS
2159   // instruction that we're folding.
2160   bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS)
2161                              : ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS;
2162
2163   ICmpInst::Predicate Pred = Cmp.getPredicate();
2164
2165   // If the division is known to be exact, then there is no remainder from the
2166   // divide, so the covered range size is unit, otherwise it is the divisor.
2167   Constant *RangeSize =
2168       Div->isExact() ? ConstantInt::get(Div->getType(), 1) : DivRHS;
2169
2170   // Figure out the interval that is being checked.  For example, a comparison
2171   // like "X /u 5 == 0" is really checking that X is in the interval [0, 5).
2172   // Compute this interval based on the constants involved and the signedness of
2173   // the compare/divide.  This computes a half-open interval, keeping track of
2174   // whether either value in the interval overflows.  After analysis each
2175   // overflow variable is set to 0 if it's corresponding bound variable is valid
2176   // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
2177   int LoOverflow = 0, HiOverflow = 0;
2178   Constant *LoBound = nullptr, *HiBound = nullptr;
2179
2180   if (!DivIsSigned) {  // udiv
2181     // e.g. X/5 op 3  --> [15, 20)
2182     LoBound = Prod;
2183     HiOverflow = LoOverflow = ProdOV;
2184     if (!HiOverflow) {
2185       // If this is not an exact divide, then many values in the range collapse
2186       // to the same result value.
2187       HiOverflow = addWithOverflow(HiBound, LoBound, RangeSize, false);
2188     }
2189   } else if (C2->isStrictlyPositive()) { // Divisor is > 0.
2190     if (*C == 0) {       // (X / pos) op 0
2191       // Can't overflow.  e.g.  X/2 op 0 --> [-1, 2)
2192       LoBound = ConstantExpr::getNeg(SubOne(RangeSize));
2193       HiBound = RangeSize;
2194     } else if (C->isStrictlyPositive()) {   // (X / pos) op pos
2195       LoBound = Prod;     // e.g.   X/5 op 3 --> [15, 20)
2196       HiOverflow = LoOverflow = ProdOV;
2197       if (!HiOverflow)
2198         HiOverflow = addWithOverflow(HiBound, Prod, RangeSize, true);
2199     } else {                       // (X / pos) op neg
2200       // e.g. X/5 op -3  --> [-15-4, -15+1) --> [-19, -14)
2201       HiBound = AddOne(Prod);
2202       LoOverflow = HiOverflow = ProdOV ? -1 : 0;
2203       if (!LoOverflow) {
2204         Constant *DivNeg = ConstantExpr::getNeg(RangeSize);
2205         LoOverflow = addWithOverflow(LoBound, HiBound, DivNeg, true) ? -1 : 0;
2206       }
2207     }
2208   } else if (C2->isNegative()) { // Divisor is < 0.
2209     if (Div->isExact())
2210       RangeSize = ConstantExpr::getNeg(RangeSize);
2211     if (*C == 0) {       // (X / neg) op 0
2212       // e.g. X/-5 op 0  --> [-4, 5)
2213       LoBound = AddOne(RangeSize);
2214       HiBound = ConstantExpr::getNeg(RangeSize);
2215       if (HiBound == DivRHS) {     // -INTMIN = INTMIN
2216         HiOverflow = 1;            // [INTMIN+1, overflow)
2217         HiBound = nullptr;         // e.g. X/INTMIN = 0 --> X > INTMIN
2218       }
2219     } else if (C->isStrictlyPositive()) {   // (X / neg) op pos
2220       // e.g. X/-5 op 3  --> [-19, -14)
2221       HiBound = AddOne(Prod);
2222       HiOverflow = LoOverflow = ProdOV ? -1 : 0;
2223       if (!LoOverflow)
2224         LoOverflow = addWithOverflow(LoBound, HiBound, RangeSize, true) ? -1:0;
2225     } else {                       // (X / neg) op neg
2226       LoBound = Prod;       // e.g. X/-5 op -3  --> [15, 20)
2227       LoOverflow = HiOverflow = ProdOV;
2228       if (!HiOverflow)
2229         HiOverflow = subWithOverflow(HiBound, Prod, RangeSize, true);
2230     }
2231
2232     // Dividing by a negative swaps the condition.  LT <-> GT
2233     Pred = ICmpInst::getSwappedPredicate(Pred);
2234   }
2235
2236   Value *X = Div->getOperand(0);
2237   switch (Pred) {
2238     default: llvm_unreachable("Unhandled icmp opcode!");
2239     case ICmpInst::ICMP_EQ:
2240       if (LoOverflow && HiOverflow)
2241         return replaceInstUsesWith(Cmp, Builder->getFalse());
2242       if (HiOverflow)
2243         return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
2244                             ICmpInst::ICMP_UGE, X, LoBound);
2245       if (LoOverflow)
2246         return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
2247                             ICmpInst::ICMP_ULT, X, HiBound);
2248       return replaceInstUsesWith(
2249           Cmp, insertRangeTest(X, LoBound->getUniqueInteger(),
2250                                HiBound->getUniqueInteger(), DivIsSigned, true));
2251     case ICmpInst::ICMP_NE:
2252       if (LoOverflow && HiOverflow)
2253         return replaceInstUsesWith(Cmp, Builder->getTrue());
2254       if (HiOverflow)
2255         return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
2256                             ICmpInst::ICMP_ULT, X, LoBound);
2257       if (LoOverflow)
2258         return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
2259                             ICmpInst::ICMP_UGE, X, HiBound);
2260       return replaceInstUsesWith(Cmp,
2261                                  insertRangeTest(X, LoBound->getUniqueInteger(),
2262                                                  HiBound->getUniqueInteger(),
2263                                                  DivIsSigned, false));
2264     case ICmpInst::ICMP_ULT:
2265     case ICmpInst::ICMP_SLT:
2266       if (LoOverflow == +1)   // Low bound is greater than input range.
2267         return replaceInstUsesWith(Cmp, Builder->getTrue());
2268       if (LoOverflow == -1)   // Low bound is less than input range.
2269         return replaceInstUsesWith(Cmp, Builder->getFalse());
2270       return new ICmpInst(Pred, X, LoBound);
2271     case ICmpInst::ICMP_UGT:
2272     case ICmpInst::ICMP_SGT:
2273       if (HiOverflow == +1)       // High bound greater than input range.
2274         return replaceInstUsesWith(Cmp, Builder->getFalse());
2275       if (HiOverflow == -1)       // High bound less than input range.
2276         return replaceInstUsesWith(Cmp, Builder->getTrue());
2277       if (Pred == ICmpInst::ICMP_UGT)
2278         return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound);
2279       return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound);
2280   }
2281
2282   return nullptr;
2283 }
2284
2285 /// Fold icmp (sub X, Y), C.
2286 Instruction *InstCombiner::foldICmpSubConstant(ICmpInst &Cmp,
2287                                                BinaryOperator *Sub,
2288                                                const APInt *C) {
2289   Value *X = Sub->getOperand(0), *Y = Sub->getOperand(1);
2290   ICmpInst::Predicate Pred = Cmp.getPredicate();
2291
2292   // The following transforms are only worth it if the only user of the subtract
2293   // is the icmp.
2294   if (!Sub->hasOneUse())
2295     return nullptr;
2296
2297   if (Sub->hasNoSignedWrap()) {
2298     // (icmp sgt (sub nsw X, Y), -1) -> (icmp sge X, Y)
2299     if (Pred == ICmpInst::ICMP_SGT && C->isAllOnesValue())
2300       return new ICmpInst(ICmpInst::ICMP_SGE, X, Y);
2301
2302     // (icmp sgt (sub nsw X, Y), 0) -> (icmp sgt X, Y)
2303     if (Pred == ICmpInst::ICMP_SGT && *C == 0)
2304       return new ICmpInst(ICmpInst::ICMP_SGT, X, Y);
2305
2306     // (icmp slt (sub nsw X, Y), 0) -> (icmp slt X, Y)
2307     if (Pred == ICmpInst::ICMP_SLT && *C == 0)
2308       return new ICmpInst(ICmpInst::ICMP_SLT, X, Y);
2309
2310     // (icmp slt (sub nsw X, Y), 1) -> (icmp sle X, Y)
2311     if (Pred == ICmpInst::ICMP_SLT && *C == 1)
2312       return new ICmpInst(ICmpInst::ICMP_SLE, X, Y);
2313   }
2314
2315   const APInt *C2;
2316   if (!match(X, m_APInt(C2)))
2317     return nullptr;
2318
2319   // C2 - Y <u C -> (Y | (C - 1)) == C2
2320   //   iff (C2 & (C - 1)) == C - 1 and C is a power of 2
2321   if (Pred == ICmpInst::ICMP_ULT && C->isPowerOf2() &&
2322       (*C2 & (*C - 1)) == (*C - 1))
2323     return new ICmpInst(ICmpInst::ICMP_EQ, Builder->CreateOr(Y, *C - 1), X);
2324
2325   // C2 - Y >u C -> (Y | C) != C2
2326   //   iff C2 & C == C and C + 1 is a power of 2
2327   if (Pred == ICmpInst::ICMP_UGT && (*C + 1).isPowerOf2() && (*C2 & *C) == *C)
2328     return new ICmpInst(ICmpInst::ICMP_NE, Builder->CreateOr(Y, *C), X);
2329
2330   return nullptr;
2331 }
2332
2333 /// Fold icmp (add X, Y), C.
2334 Instruction *InstCombiner::foldICmpAddConstant(ICmpInst &Cmp,
2335                                                BinaryOperator *Add,
2336                                                const APInt *C) {
2337   Value *Y = Add->getOperand(1);
2338   const APInt *C2;
2339   if (Cmp.isEquality() || !match(Y, m_APInt(C2)))
2340     return nullptr;
2341
2342   // Fold icmp pred (add X, C2), C.
2343   Value *X = Add->getOperand(0);
2344   Type *Ty = Add->getType();
2345   auto CR =
2346       ConstantRange::makeExactICmpRegion(Cmp.getPredicate(), *C).subtract(*C2);
2347   const APInt &Upper = CR.getUpper();
2348   const APInt &Lower = CR.getLower();
2349   if (Cmp.isSigned()) {
2350     if (Lower.isSignBit())
2351       return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, Upper));
2352     if (Upper.isSignBit())
2353       return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, Lower));
2354   } else {
2355     if (Lower.isMinValue())
2356       return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, Upper));
2357     if (Upper.isMinValue())
2358       return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, Lower));
2359   }
2360
2361   if (!Add->hasOneUse())
2362     return nullptr;
2363
2364   // X+C <u C2 -> (X & -C2) == C
2365   //   iff C & (C2-1) == 0
2366   //       C2 is a power of 2
2367   if (Cmp.getPredicate() == ICmpInst::ICMP_ULT && C->isPowerOf2() &&
2368       (*C2 & (*C - 1)) == 0)
2369     return new ICmpInst(ICmpInst::ICMP_EQ, Builder->CreateAnd(X, -(*C)),
2370                         ConstantExpr::getNeg(cast<Constant>(Y)));
2371
2372   // X+C >u C2 -> (X & ~C2) != C
2373   //   iff C & C2 == 0
2374   //       C2+1 is a power of 2
2375   if (Cmp.getPredicate() == ICmpInst::ICMP_UGT && (*C + 1).isPowerOf2() &&
2376       (*C2 & *C) == 0)
2377     return new ICmpInst(ICmpInst::ICMP_NE, Builder->CreateAnd(X, ~(*C)),
2378                         ConstantExpr::getNeg(cast<Constant>(Y)));
2379
2380   return nullptr;
2381 }
2382
2383 /// Try to fold integer comparisons with a constant operand: icmp Pred X, C
2384 /// where X is some kind of instruction.
2385 Instruction *InstCombiner::foldICmpInstWithConstant(ICmpInst &Cmp) {
2386   const APInt *C;
2387   if (!match(Cmp.getOperand(1), m_APInt(C)))
2388     return nullptr;
2389
2390   BinaryOperator *BO;
2391   if (match(Cmp.getOperand(0), m_BinOp(BO))) {
2392     switch (BO->getOpcode()) {
2393     case Instruction::Xor:
2394       if (Instruction *I = foldICmpXorConstant(Cmp, BO, C))
2395         return I;
2396       break;
2397     case Instruction::And:
2398       if (Instruction *I = foldICmpAndConstant(Cmp, BO, C))
2399         return I;
2400       break;
2401     case Instruction::Or:
2402       if (Instruction *I = foldICmpOrConstant(Cmp, BO, C))
2403         return I;
2404       break;
2405     case Instruction::Mul:
2406       if (Instruction *I = foldICmpMulConstant(Cmp, BO, C))
2407         return I;
2408       break;
2409     case Instruction::Shl:
2410       if (Instruction *I = foldICmpShlConstant(Cmp, BO, C))
2411         return I;
2412       break;
2413     case Instruction::LShr:
2414     case Instruction::AShr:
2415       if (Instruction *I = foldICmpShrConstant(Cmp, BO, C))
2416         return I;
2417       break;
2418     case Instruction::UDiv:
2419       if (Instruction *I = foldICmpUDivConstant(Cmp, BO, C))
2420         return I;
2421       LLVM_FALLTHROUGH;
2422     case Instruction::SDiv:
2423       if (Instruction *I = foldICmpDivConstant(Cmp, BO, C))
2424         return I;
2425       break;
2426     case Instruction::Sub:
2427       if (Instruction *I = foldICmpSubConstant(Cmp, BO, C))
2428         return I;
2429       break;
2430     case Instruction::Add:
2431       if (Instruction *I = foldICmpAddConstant(Cmp, BO, C))
2432         return I;
2433       break;
2434     default:
2435       break;
2436     }
2437     // TODO: These folds could be refactored to be part of the above calls.
2438     if (Instruction *I = foldICmpBinOpEqualityWithConstant(Cmp, BO, C))
2439       return I;
2440   }
2441
2442   Instruction *LHSI;
2443   if (match(Cmp.getOperand(0), m_Instruction(LHSI)) &&
2444       LHSI->getOpcode() == Instruction::Trunc)
2445     if (Instruction *I = foldICmpTruncConstant(Cmp, LHSI, C))
2446       return I;
2447
2448   if (Instruction *I = foldICmpIntrinsicWithConstant(Cmp, C))
2449     return I;
2450
2451   return nullptr;
2452 }
2453
2454 /// Fold an icmp equality instruction with binary operator LHS and constant RHS:
2455 /// icmp eq/ne BO, C.
2456 Instruction *InstCombiner::foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp,
2457                                                              BinaryOperator *BO,
2458                                                              const APInt *C) {
2459   // TODO: Some of these folds could work with arbitrary constants, but this
2460   // function is limited to scalar and vector splat constants.
2461   if (!Cmp.isEquality())
2462     return nullptr;
2463
2464   ICmpInst::Predicate Pred = Cmp.getPredicate();
2465   bool isICMP_NE = Pred == ICmpInst::ICMP_NE;
2466   Constant *RHS = cast<Constant>(Cmp.getOperand(1));
2467   Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
2468
2469   switch (BO->getOpcode()) {
2470   case Instruction::SRem:
2471     // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
2472     if (*C == 0 && BO->hasOneUse()) {
2473       const APInt *BOC;
2474       if (match(BOp1, m_APInt(BOC)) && BOC->sgt(1) && BOC->isPowerOf2()) {
2475         Value *NewRem = Builder->CreateURem(BOp0, BOp1, BO->getName());
2476         return new ICmpInst(Pred, NewRem,
2477                             Constant::getNullValue(BO->getType()));
2478       }
2479     }
2480     break;
2481   case Instruction::Add: {
2482     // Replace ((add A, B) != C) with (A != C-B) if B & C are constants.
2483     const APInt *BOC;
2484     if (match(BOp1, m_APInt(BOC))) {
2485       if (BO->hasOneUse()) {
2486         Constant *SubC = ConstantExpr::getSub(RHS, cast<Constant>(BOp1));
2487         return new ICmpInst(Pred, BOp0, SubC);
2488       }
2489     } else if (*C == 0) {
2490       // Replace ((add A, B) != 0) with (A != -B) if A or B is
2491       // efficiently invertible, or if the add has just this one use.
2492       if (Value *NegVal = dyn_castNegVal(BOp1))
2493         return new ICmpInst(Pred, BOp0, NegVal);
2494       if (Value *NegVal = dyn_castNegVal(BOp0))
2495         return new ICmpInst(Pred, NegVal, BOp1);
2496       if (BO->hasOneUse()) {
2497         Value *Neg = Builder->CreateNeg(BOp1);
2498         Neg->takeName(BO);
2499         return new ICmpInst(Pred, BOp0, Neg);
2500       }
2501     }
2502     break;
2503   }
2504   case Instruction::Xor:
2505     if (BO->hasOneUse()) {
2506       if (Constant *BOC = dyn_cast<Constant>(BOp1)) {
2507         // For the xor case, we can xor two constants together, eliminating
2508         // the explicit xor.
2509         return new ICmpInst(Pred, BOp0, ConstantExpr::getXor(RHS, BOC));
2510       } else if (*C == 0) {
2511         // Replace ((xor A, B) != 0) with (A != B)
2512         return new ICmpInst(Pred, BOp0, BOp1);
2513       }
2514     }
2515     break;
2516   case Instruction::Sub:
2517     if (BO->hasOneUse()) {
2518       const APInt *BOC;
2519       if (match(BOp0, m_APInt(BOC))) {
2520         // Replace ((sub BOC, B) != C) with (B != BOC-C).
2521         Constant *SubC = ConstantExpr::getSub(cast<Constant>(BOp0), RHS);
2522         return new ICmpInst(Pred, BOp1, SubC);
2523       } else if (*C == 0) {
2524         // Replace ((sub A, B) != 0) with (A != B).
2525         return new ICmpInst(Pred, BOp0, BOp1);
2526       }
2527     }
2528     break;
2529   case Instruction::Or: {
2530     const APInt *BOC;
2531     if (match(BOp1, m_APInt(BOC)) && BO->hasOneUse() && RHS->isAllOnesValue()) {
2532       // Comparing if all bits outside of a constant mask are set?
2533       // Replace (X | C) == -1 with (X & ~C) == ~C.
2534       // This removes the -1 constant.
2535       Constant *NotBOC = ConstantExpr::getNot(cast<Constant>(BOp1));
2536       Value *And = Builder->CreateAnd(BOp0, NotBOC);
2537       return new ICmpInst(Pred, And, NotBOC);
2538     }
2539     break;
2540   }
2541   case Instruction::And: {
2542     const APInt *BOC;
2543     if (match(BOp1, m_APInt(BOC))) {
2544       // If we have ((X & C) == C), turn it into ((X & C) != 0).
2545       if (C == BOC && C->isPowerOf2())
2546         return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE,
2547                             BO, Constant::getNullValue(RHS->getType()));
2548
2549       // Don't perform the following transforms if the AND has multiple uses
2550       if (!BO->hasOneUse())
2551         break;
2552
2553       // Replace (and X, (1 << size(X)-1) != 0) with x s< 0
2554       if (BOC->isSignBit()) {
2555         Constant *Zero = Constant::getNullValue(BOp0->getType());
2556         auto NewPred = isICMP_NE ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
2557         return new ICmpInst(NewPred, BOp0, Zero);
2558       }
2559
2560       // ((X & ~7) == 0) --> X < 8
2561       if (*C == 0 && (~(*BOC) + 1).isPowerOf2()) {
2562         Constant *NegBOC = ConstantExpr::getNeg(cast<Constant>(BOp1));
2563         auto NewPred = isICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
2564         return new ICmpInst(NewPred, BOp0, NegBOC);
2565       }
2566     }
2567     break;
2568   }
2569   case Instruction::Mul:
2570     if (*C == 0 && BO->hasNoSignedWrap()) {
2571       const APInt *BOC;
2572       if (match(BOp1, m_APInt(BOC)) && *BOC != 0) {
2573         // The trivial case (mul X, 0) is handled by InstSimplify.
2574         // General case : (mul X, C) != 0 iff X != 0
2575         //                (mul X, C) == 0 iff X == 0
2576         return new ICmpInst(Pred, BOp0, Constant::getNullValue(RHS->getType()));
2577       }
2578     }
2579     break;
2580   case Instruction::UDiv:
2581     if (*C == 0) {
2582       // (icmp eq/ne (udiv A, B), 0) -> (icmp ugt/ule i32 B, A)
2583       auto NewPred = isICMP_NE ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_UGT;
2584       return new ICmpInst(NewPred, BOp1, BOp0);
2585     }
2586     break;
2587   default:
2588     break;
2589   }
2590   return nullptr;
2591 }
2592
2593 /// Fold an icmp with LLVM intrinsic and constant operand: icmp Pred II, C.
2594 Instruction *InstCombiner::foldICmpIntrinsicWithConstant(ICmpInst &Cmp,
2595                                                          const APInt *C) {
2596   IntrinsicInst *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0));
2597   if (!II || !Cmp.isEquality())
2598     return nullptr;
2599
2600   // Handle icmp {eq|ne} <intrinsic>, intcst.
2601   switch (II->getIntrinsicID()) {
2602   case Intrinsic::bswap:
2603     Worklist.Add(II);
2604     Cmp.setOperand(0, II->getArgOperand(0));
2605     Cmp.setOperand(1, Builder->getInt(C->byteSwap()));
2606     return &Cmp;
2607   case Intrinsic::ctlz:
2608   case Intrinsic::cttz:
2609     // ctz(A) == bitwidth(A)  ->  A == 0 and likewise for !=
2610     if (*C == C->getBitWidth()) {
2611       Worklist.Add(II);
2612       Cmp.setOperand(0, II->getArgOperand(0));
2613       Cmp.setOperand(1, ConstantInt::getNullValue(II->getType()));
2614       return &Cmp;
2615     }
2616     break;
2617   case Intrinsic::ctpop: {
2618     // popcount(A) == 0  ->  A == 0 and likewise for !=
2619     // popcount(A) == bitwidth(A)  ->  A == -1 and likewise for !=
2620     bool IsZero = *C == 0;
2621     if (IsZero || *C == C->getBitWidth()) {
2622       Worklist.Add(II);
2623       Cmp.setOperand(0, II->getArgOperand(0));
2624       auto *NewOp = IsZero ? Constant::getNullValue(II->getType())
2625                            : Constant::getAllOnesValue(II->getType());
2626       Cmp.setOperand(1, NewOp);
2627       return &Cmp;
2628     }
2629     break;
2630   }
2631   default:
2632     break;
2633   }
2634   return nullptr;
2635 }
2636
2637 /// Handle icmp with constant (but not simple integer constant) RHS.
2638 Instruction *InstCombiner::foldICmpInstWithConstantNotInt(ICmpInst &I) {
2639   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2640   Constant *RHSC = dyn_cast<Constant>(Op1);
2641   Instruction *LHSI = dyn_cast<Instruction>(Op0);
2642   if (!RHSC || !LHSI)
2643     return nullptr;
2644
2645   switch (LHSI->getOpcode()) {
2646   case Instruction::GetElementPtr:
2647     // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null
2648     if (RHSC->isNullValue() &&
2649         cast<GetElementPtrInst>(LHSI)->hasAllZeroIndices())
2650       return new ICmpInst(
2651           I.getPredicate(), LHSI->getOperand(0),
2652           Constant::getNullValue(LHSI->getOperand(0)->getType()));
2653     break;
2654   case Instruction::PHI:
2655     // Only fold icmp into the PHI if the phi and icmp are in the same
2656     // block.  If in the same block, we're encouraging jump threading.  If
2657     // not, we are just pessimizing the code by making an i1 phi.
2658     if (LHSI->getParent() == I.getParent())
2659       if (Instruction *NV = FoldOpIntoPhi(I))
2660         return NV;
2661     break;
2662   case Instruction::Select: {
2663     // If either operand of the select is a constant, we can fold the
2664     // comparison into the select arms, which will cause one to be
2665     // constant folded and the select turned into a bitwise or.
2666     Value *Op1 = nullptr, *Op2 = nullptr;
2667     ConstantInt *CI = nullptr;
2668     if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
2669       Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
2670       CI = dyn_cast<ConstantInt>(Op1);
2671     }
2672     if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
2673       Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
2674       CI = dyn_cast<ConstantInt>(Op2);
2675     }
2676
2677     // We only want to perform this transformation if it will not lead to
2678     // additional code. This is true if either both sides of the select
2679     // fold to a constant (in which case the icmp is replaced with a select
2680     // which will usually simplify) or this is the only user of the
2681     // select (in which case we are trading a select+icmp for a simpler
2682     // select+icmp) or all uses of the select can be replaced based on
2683     // dominance information ("Global cases").
2684     bool Transform = false;
2685     if (Op1 && Op2)
2686       Transform = true;
2687     else if (Op1 || Op2) {
2688       // Local case
2689       if (LHSI->hasOneUse())
2690         Transform = true;
2691       // Global cases
2692       else if (CI && !CI->isZero())
2693         // When Op1 is constant try replacing select with second operand.
2694         // Otherwise Op2 is constant and try replacing select with first
2695         // operand.
2696         Transform =
2697             replacedSelectWithOperand(cast<SelectInst>(LHSI), &I, Op1 ? 2 : 1);
2698     }
2699     if (Transform) {
2700       if (!Op1)
2701         Op1 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(1), RHSC,
2702                                   I.getName());
2703       if (!Op2)
2704         Op2 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(2), RHSC,
2705                                   I.getName());
2706       return SelectInst::Create(LHSI->getOperand(0), Op1, Op2);
2707     }
2708     break;
2709   }
2710   case Instruction::IntToPtr:
2711     // icmp pred inttoptr(X), null -> icmp pred X, 0
2712     if (RHSC->isNullValue() &&
2713         DL.getIntPtrType(RHSC->getType()) == LHSI->getOperand(0)->getType())
2714       return new ICmpInst(
2715           I.getPredicate(), LHSI->getOperand(0),
2716           Constant::getNullValue(LHSI->getOperand(0)->getType()));
2717     break;
2718
2719   case Instruction::Load:
2720     // Try to optimize things like "A[i] > 4" to index computations.
2721     if (GetElementPtrInst *GEP =
2722             dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) {
2723       if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
2724         if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
2725             !cast<LoadInst>(LHSI)->isVolatile())
2726           if (Instruction *Res = foldCmpLoadFromIndexedGlobal(GEP, GV, I))
2727             return Res;
2728     }
2729     break;
2730   }
2731
2732   return nullptr;
2733 }
2734
2735 /// Try to fold icmp (binop), X or icmp X, (binop).
2736 Instruction *InstCombiner::foldICmpBinOp(ICmpInst &I) {
2737   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2738
2739   // Special logic for binary operators.
2740   BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Op0);
2741   BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Op1);
2742   if (!BO0 && !BO1)
2743     return nullptr;
2744
2745   CmpInst::Predicate Pred = I.getPredicate();
2746   bool NoOp0WrapProblem = false, NoOp1WrapProblem = false;
2747   if (BO0 && isa<OverflowingBinaryOperator>(BO0))
2748     NoOp0WrapProblem =
2749         ICmpInst::isEquality(Pred) ||
2750         (CmpInst::isUnsigned(Pred) && BO0->hasNoUnsignedWrap()) ||
2751         (CmpInst::isSigned(Pred) && BO0->hasNoSignedWrap());
2752   if (BO1 && isa<OverflowingBinaryOperator>(BO1))
2753     NoOp1WrapProblem =
2754         ICmpInst::isEquality(Pred) ||
2755         (CmpInst::isUnsigned(Pred) && BO1->hasNoUnsignedWrap()) ||
2756         (CmpInst::isSigned(Pred) && BO1->hasNoSignedWrap());
2757
2758   // Analyze the case when either Op0 or Op1 is an add instruction.
2759   // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null).
2760   Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
2761   if (BO0 && BO0->getOpcode() == Instruction::Add) {
2762     A = BO0->getOperand(0);
2763     B = BO0->getOperand(1);
2764   }
2765   if (BO1 && BO1->getOpcode() == Instruction::Add) {
2766     C = BO1->getOperand(0);
2767     D = BO1->getOperand(1);
2768   }
2769
2770   // icmp (X+cst) < 0 --> X < -cst
2771   if (NoOp0WrapProblem && ICmpInst::isSigned(Pred) && match(Op1, m_Zero()))
2772     if (ConstantInt *RHSC = dyn_cast_or_null<ConstantInt>(B))
2773       if (!RHSC->isMinValue(/*isSigned=*/true))
2774         return new ICmpInst(Pred, A, ConstantExpr::getNeg(RHSC));
2775
2776   // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
2777   if ((A == Op1 || B == Op1) && NoOp0WrapProblem)
2778     return new ICmpInst(Pred, A == Op1 ? B : A,
2779                         Constant::getNullValue(Op1->getType()));
2780
2781   // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
2782   if ((C == Op0 || D == Op0) && NoOp1WrapProblem)
2783     return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()),
2784                         C == Op0 ? D : C);
2785
2786   // icmp (X+Y), (X+Z) -> icmp Y, Z for equalities or if there is no overflow.
2787   if (A && C && (A == C || A == D || B == C || B == D) && NoOp0WrapProblem &&
2788       NoOp1WrapProblem &&
2789       // Try not to increase register pressure.
2790       BO0->hasOneUse() && BO1->hasOneUse()) {
2791     // Determine Y and Z in the form icmp (X+Y), (X+Z).
2792     Value *Y, *Z;
2793     if (A == C) {
2794       // C + B == C + D  ->  B == D
2795       Y = B;
2796       Z = D;
2797     } else if (A == D) {
2798       // D + B == C + D  ->  B == C
2799       Y = B;
2800       Z = C;
2801     } else if (B == C) {
2802       // A + C == C + D  ->  A == D
2803       Y = A;
2804       Z = D;
2805     } else {
2806       assert(B == D);
2807       // A + D == C + D  ->  A == C
2808       Y = A;
2809       Z = C;
2810     }
2811     return new ICmpInst(Pred, Y, Z);
2812   }
2813
2814   // icmp slt (X + -1), Y -> icmp sle X, Y
2815   if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLT &&
2816       match(B, m_AllOnes()))
2817     return new ICmpInst(CmpInst::ICMP_SLE, A, Op1);
2818
2819   // icmp sge (X + -1), Y -> icmp sgt X, Y
2820   if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGE &&
2821       match(B, m_AllOnes()))
2822     return new ICmpInst(CmpInst::ICMP_SGT, A, Op1);
2823
2824   // icmp sle (X + 1), Y -> icmp slt X, Y
2825   if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLE && match(B, m_One()))
2826     return new ICmpInst(CmpInst::ICMP_SLT, A, Op1);
2827
2828   // icmp sgt (X + 1), Y -> icmp sge X, Y
2829   if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGT && match(B, m_One()))
2830     return new ICmpInst(CmpInst::ICMP_SGE, A, Op1);
2831
2832   // icmp sgt X, (Y + -1) -> icmp sge X, Y
2833   if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGT &&
2834       match(D, m_AllOnes()))
2835     return new ICmpInst(CmpInst::ICMP_SGE, Op0, C);
2836
2837   // icmp sle X, (Y + -1) -> icmp slt X, Y
2838   if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLE &&
2839       match(D, m_AllOnes()))
2840     return new ICmpInst(CmpInst::ICMP_SLT, Op0, C);
2841
2842   // icmp sge X, (Y + 1) -> icmp sgt X, Y
2843   if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGE && match(D, m_One()))
2844     return new ICmpInst(CmpInst::ICMP_SGT, Op0, C);
2845
2846   // icmp slt X, (Y + 1) -> icmp sle X, Y
2847   if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLT && match(D, m_One()))
2848     return new ICmpInst(CmpInst::ICMP_SLE, Op0, C);
2849
2850   // if C1 has greater magnitude than C2:
2851   //  icmp (X + C1), (Y + C2) -> icmp (X + C3), Y
2852   //  s.t. C3 = C1 - C2
2853   //
2854   // if C2 has greater magnitude than C1:
2855   //  icmp (X + C1), (Y + C2) -> icmp X, (Y + C3)
2856   //  s.t. C3 = C2 - C1
2857   if (A && C && NoOp0WrapProblem && NoOp1WrapProblem &&
2858       (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned())
2859     if (ConstantInt *C1 = dyn_cast<ConstantInt>(B))
2860       if (ConstantInt *C2 = dyn_cast<ConstantInt>(D)) {
2861         const APInt &AP1 = C1->getValue();
2862         const APInt &AP2 = C2->getValue();
2863         if (AP1.isNegative() == AP2.isNegative()) {
2864           APInt AP1Abs = C1->getValue().abs();
2865           APInt AP2Abs = C2->getValue().abs();
2866           if (AP1Abs.uge(AP2Abs)) {
2867             ConstantInt *C3 = Builder->getInt(AP1 - AP2);
2868             Value *NewAdd = Builder->CreateNSWAdd(A, C3);
2869             return new ICmpInst(Pred, NewAdd, C);
2870           } else {
2871             ConstantInt *C3 = Builder->getInt(AP2 - AP1);
2872             Value *NewAdd = Builder->CreateNSWAdd(C, C3);
2873             return new ICmpInst(Pred, A, NewAdd);
2874           }
2875         }
2876       }
2877
2878   // Analyze the case when either Op0 or Op1 is a sub instruction.
2879   // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null).
2880   A = nullptr;
2881   B = nullptr;
2882   C = nullptr;
2883   D = nullptr;
2884   if (BO0 && BO0->getOpcode() == Instruction::Sub) {
2885     A = BO0->getOperand(0);
2886     B = BO0->getOperand(1);
2887   }
2888   if (BO1 && BO1->getOpcode() == Instruction::Sub) {
2889     C = BO1->getOperand(0);
2890     D = BO1->getOperand(1);
2891   }
2892
2893   // icmp (X-Y), X -> icmp 0, Y for equalities or if there is no overflow.
2894   if (A == Op1 && NoOp0WrapProblem)
2895     return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B);
2896
2897   // icmp X, (X-Y) -> icmp Y, 0 for equalities or if there is no overflow.
2898   if (C == Op0 && NoOp1WrapProblem)
2899     return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType()));
2900
2901   // icmp (Y-X), (Z-X) -> icmp Y, Z for equalities or if there is no overflow.
2902   if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem &&
2903       // Try not to increase register pressure.
2904       BO0->hasOneUse() && BO1->hasOneUse())
2905     return new ICmpInst(Pred, A, C);
2906
2907   // icmp (X-Y), (X-Z) -> icmp Z, Y for equalities or if there is no overflow.
2908   if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem &&
2909       // Try not to increase register pressure.
2910       BO0->hasOneUse() && BO1->hasOneUse())
2911     return new ICmpInst(Pred, D, B);
2912
2913   // icmp (0-X) < cst --> x > -cst
2914   if (NoOp0WrapProblem && ICmpInst::isSigned(Pred)) {
2915     Value *X;
2916     if (match(BO0, m_Neg(m_Value(X))))
2917       if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1))
2918         if (!RHSC->isMinValue(/*isSigned=*/true))
2919           return new ICmpInst(I.getSwappedPredicate(), X,
2920                               ConstantExpr::getNeg(RHSC));
2921   }
2922
2923   BinaryOperator *SRem = nullptr;
2924   // icmp (srem X, Y), Y
2925   if (BO0 && BO0->getOpcode() == Instruction::SRem && Op1 == BO0->getOperand(1))
2926     SRem = BO0;
2927   // icmp Y, (srem X, Y)
2928   else if (BO1 && BO1->getOpcode() == Instruction::SRem &&
2929            Op0 == BO1->getOperand(1))
2930     SRem = BO1;
2931   if (SRem) {
2932     // We don't check hasOneUse to avoid increasing register pressure because
2933     // the value we use is the same value this instruction was already using.
2934     switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) {
2935     default:
2936       break;
2937     case ICmpInst::ICMP_EQ:
2938       return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
2939     case ICmpInst::ICMP_NE:
2940       return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
2941     case ICmpInst::ICMP_SGT:
2942     case ICmpInst::ICMP_SGE:
2943       return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1),
2944                           Constant::getAllOnesValue(SRem->getType()));
2945     case ICmpInst::ICMP_SLT:
2946     case ICmpInst::ICMP_SLE:
2947       return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1),
2948                           Constant::getNullValue(SRem->getType()));
2949     }
2950   }
2951
2952   if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() && BO0->hasOneUse() &&
2953       BO1->hasOneUse() && BO0->getOperand(1) == BO1->getOperand(1)) {
2954     switch (BO0->getOpcode()) {
2955     default:
2956       break;
2957     case Instruction::Add:
2958     case Instruction::Sub:
2959     case Instruction::Xor:
2960       if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b
2961         return new ICmpInst(I.getPredicate(), BO0->getOperand(0),
2962                             BO1->getOperand(0));
2963       // icmp u/s (a ^ signbit), (b ^ signbit) --> icmp s/u a, b
2964       if (ConstantInt *CI = dyn_cast<ConstantInt>(BO0->getOperand(1))) {
2965         if (CI->getValue().isSignBit()) {
2966           ICmpInst::Predicate Pred =
2967               I.isSigned() ? I.getUnsignedPredicate() : I.getSignedPredicate();
2968           return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
2969         }
2970
2971         if (BO0->getOpcode() == Instruction::Xor && CI->isMaxValue(true)) {
2972           ICmpInst::Predicate Pred =
2973               I.isSigned() ? I.getUnsignedPredicate() : I.getSignedPredicate();
2974           Pred = I.getSwappedPredicate(Pred);
2975           return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0));
2976         }
2977       }
2978       break;
2979     case Instruction::Mul:
2980       if (!I.isEquality())
2981         break;
2982
2983       if (ConstantInt *CI = dyn_cast<ConstantInt>(BO0->getOperand(1))) {
2984         // a * Cst icmp eq/ne b * Cst --> a & Mask icmp b & Mask
2985         // Mask = -1 >> count-trailing-zeros(Cst).
2986         if (!CI->isZero() && !CI->isOne()) {
2987           const APInt &AP = CI->getValue();
2988           ConstantInt *Mask = ConstantInt::get(
2989               I.getContext(),
2990               APInt::getLowBitsSet(AP.getBitWidth(),
2991                                    AP.getBitWidth() - AP.countTrailingZeros()));
2992           Value *And1 = Builder->CreateAnd(BO0->getOperand(0), Mask);
2993           Value *And2 = Builder->CreateAnd(BO1->getOperand(0), Mask);
2994           return new ICmpInst(I.getPredicate(), And1, And2);
2995         }
2996       }
2997       break;
2998     case Instruction::UDiv:
2999     case Instruction::LShr:
3000       if (I.isSigned())
3001         break;
3002       LLVM_FALLTHROUGH;
3003     case Instruction::SDiv:
3004     case Instruction::AShr:
3005       if (!BO0->isExact() || !BO1->isExact())
3006         break;
3007       return new ICmpInst(I.getPredicate(), BO0->getOperand(0),
3008                           BO1->getOperand(0));
3009     case Instruction::Shl: {
3010       bool NUW = BO0->hasNoUnsignedWrap() && BO1->hasNoUnsignedWrap();
3011       bool NSW = BO0->hasNoSignedWrap() && BO1->hasNoSignedWrap();
3012       if (!NUW && !NSW)
3013         break;
3014       if (!NSW && I.isSigned())
3015         break;
3016       return new ICmpInst(I.getPredicate(), BO0->getOperand(0),
3017                           BO1->getOperand(0));
3018     }
3019     }
3020   }
3021
3022   if (BO0) {
3023     // Transform  A & (L - 1) `ult` L --> L != 0
3024     auto LSubOne = m_Add(m_Specific(Op1), m_AllOnes());
3025     auto BitwiseAnd =
3026         m_CombineOr(m_And(m_Value(), LSubOne), m_And(LSubOne, m_Value()));
3027
3028     if (match(BO0, BitwiseAnd) && I.getPredicate() == ICmpInst::ICMP_ULT) {
3029       auto *Zero = Constant::getNullValue(BO0->getType());
3030       return new ICmpInst(ICmpInst::ICMP_NE, Op1, Zero);
3031     }
3032   }
3033
3034   return nullptr;
3035 }
3036
3037 /// Fold icmp Pred min|max(X, Y), X.
3038 static Instruction *foldICmpWithMinMax(ICmpInst &Cmp) {
3039   ICmpInst::Predicate Pred = Cmp.getPredicate();
3040   Value *Op0 = Cmp.getOperand(0);
3041   Value *X = Cmp.getOperand(1);
3042
3043   // Canonicalize minimum or maximum operand to LHS of the icmp.
3044   if (match(X, m_c_SMin(m_Specific(Op0), m_Value())) ||
3045       match(X, m_c_SMax(m_Specific(Op0), m_Value())) ||
3046       match(X, m_c_UMin(m_Specific(Op0), m_Value())) ||
3047       match(X, m_c_UMax(m_Specific(Op0), m_Value()))) {
3048     std::swap(Op0, X);
3049     Pred = Cmp.getSwappedPredicate();
3050   }
3051
3052   Value *Y;
3053   if (match(Op0, m_c_SMin(m_Specific(X), m_Value(Y)))) {
3054     // smin(X, Y)  == X --> X s<= Y
3055     // smin(X, Y) s>= X --> X s<= Y
3056     if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_SGE)
3057       return new ICmpInst(ICmpInst::ICMP_SLE, X, Y);
3058
3059     // smin(X, Y) != X --> X s> Y
3060     // smin(X, Y) s< X --> X s> Y
3061     if (Pred == CmpInst::ICMP_NE || Pred == CmpInst::ICMP_SLT)
3062       return new ICmpInst(ICmpInst::ICMP_SGT, X, Y);
3063
3064     // These cases should be handled in InstSimplify:
3065     // smin(X, Y) s<= X --> true
3066     // smin(X, Y) s> X --> false
3067     return nullptr;
3068   }
3069
3070   if (match(Op0, m_c_SMax(m_Specific(X), m_Value(Y)))) {
3071     // smax(X, Y)  == X --> X s>= Y
3072     // smax(X, Y) s<= X --> X s>= Y
3073     if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_SLE)
3074       return new ICmpInst(ICmpInst::ICMP_SGE, X, Y);
3075
3076     // smax(X, Y) != X --> X s< Y
3077     // smax(X, Y) s> X --> X s< Y
3078     if (Pred == CmpInst::ICMP_NE || Pred == CmpInst::ICMP_SGT)
3079       return new ICmpInst(ICmpInst::ICMP_SLT, X, Y);
3080
3081     // These cases should be handled in InstSimplify:
3082     // smax(X, Y) s>= X --> true
3083     // smax(X, Y) s< X --> false
3084     return nullptr;
3085   }
3086
3087   if (match(Op0, m_c_UMin(m_Specific(X), m_Value(Y)))) {
3088     // umin(X, Y)  == X --> X u<= Y
3089     // umin(X, Y) u>= X --> X u<= Y
3090     if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_UGE)
3091       return new ICmpInst(ICmpInst::ICMP_ULE, X, Y);
3092
3093     // umin(X, Y) != X --> X u> Y
3094     // umin(X, Y) u< X --> X u> Y
3095     if (Pred == CmpInst::ICMP_NE || Pred == CmpInst::ICMP_ULT)
3096       return new ICmpInst(ICmpInst::ICMP_UGT, X, Y);
3097
3098     // These cases should be handled in InstSimplify:
3099     // umin(X, Y) u<= X --> true
3100     // umin(X, Y) u> X --> false
3101     return nullptr;
3102   }
3103
3104   if (match(Op0, m_c_UMax(m_Specific(X), m_Value(Y)))) {
3105     // umax(X, Y)  == X --> X u>= Y
3106     // umax(X, Y) u<= X --> X u>= Y
3107     if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_ULE)
3108       return new ICmpInst(ICmpInst::ICMP_UGE, X, Y);
3109
3110     // umax(X, Y) != X --> X u< Y
3111     // umax(X, Y) u> X --> X u< Y
3112     if (Pred == CmpInst::ICMP_NE || Pred == CmpInst::ICMP_UGT)
3113       return new ICmpInst(ICmpInst::ICMP_ULT, X, Y);
3114
3115     // These cases should be handled in InstSimplify:
3116     // umax(X, Y) u>= X --> true
3117     // umax(X, Y) u< X --> false
3118     return nullptr;
3119   }
3120
3121   return nullptr;
3122 }
3123
3124 Instruction *InstCombiner::foldICmpEquality(ICmpInst &I) {
3125   if (!I.isEquality())
3126     return nullptr;
3127
3128   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3129   Value *A, *B, *C, *D;
3130   if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
3131     if (A == Op1 || B == Op1) { // (A^B) == A  ->  B == 0
3132       Value *OtherVal = A == Op1 ? B : A;
3133       return new ICmpInst(I.getPredicate(), OtherVal,
3134                           Constant::getNullValue(A->getType()));
3135     }
3136
3137     if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
3138       // A^c1 == C^c2 --> A == C^(c1^c2)
3139       ConstantInt *C1, *C2;
3140       if (match(B, m_ConstantInt(C1)) && match(D, m_ConstantInt(C2)) &&
3141           Op1->hasOneUse()) {
3142         Constant *NC = Builder->getInt(C1->getValue() ^ C2->getValue());
3143         Value *Xor = Builder->CreateXor(C, NC);
3144         return new ICmpInst(I.getPredicate(), A, Xor);
3145       }
3146
3147       // A^B == A^D -> B == D
3148       if (A == C)
3149         return new ICmpInst(I.getPredicate(), B, D);
3150       if (A == D)
3151         return new ICmpInst(I.getPredicate(), B, C);
3152       if (B == C)
3153         return new ICmpInst(I.getPredicate(), A, D);
3154       if (B == D)
3155         return new ICmpInst(I.getPredicate(), A, C);
3156     }
3157   }
3158
3159   if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && (A == Op0 || B == Op0)) {
3160     // A == (A^B)  ->  B == 0
3161     Value *OtherVal = A == Op0 ? B : A;
3162     return new ICmpInst(I.getPredicate(), OtherVal,
3163                         Constant::getNullValue(A->getType()));
3164   }
3165
3166   // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
3167   if (match(Op0, m_OneUse(m_And(m_Value(A), m_Value(B)))) &&
3168       match(Op1, m_OneUse(m_And(m_Value(C), m_Value(D))))) {
3169     Value *X = nullptr, *Y = nullptr, *Z = nullptr;
3170
3171     if (A == C) {
3172       X = B;
3173       Y = D;
3174       Z = A;
3175     } else if (A == D) {
3176       X = B;
3177       Y = C;
3178       Z = A;
3179     } else if (B == C) {
3180       X = A;
3181       Y = D;
3182       Z = B;
3183     } else if (B == D) {
3184       X = A;
3185       Y = C;
3186       Z = B;
3187     }
3188
3189     if (X) { // Build (X^Y) & Z
3190       Op1 = Builder->CreateXor(X, Y);
3191       Op1 = Builder->CreateAnd(Op1, Z);
3192       I.setOperand(0, Op1);
3193       I.setOperand(1, Constant::getNullValue(Op1->getType()));
3194       return &I;
3195     }
3196   }
3197
3198   // Transform (zext A) == (B & (1<<X)-1) --> A == (trunc B)
3199   // and       (B & (1<<X)-1) == (zext A) --> A == (trunc B)
3200   ConstantInt *Cst1;
3201   if ((Op0->hasOneUse() && match(Op0, m_ZExt(m_Value(A))) &&
3202        match(Op1, m_And(m_Value(B), m_ConstantInt(Cst1)))) ||
3203       (Op1->hasOneUse() && match(Op0, m_And(m_Value(B), m_ConstantInt(Cst1))) &&
3204        match(Op1, m_ZExt(m_Value(A))))) {
3205     APInt Pow2 = Cst1->getValue() + 1;
3206     if (Pow2.isPowerOf2() && isa<IntegerType>(A->getType()) &&
3207         Pow2.logBase2() == cast<IntegerType>(A->getType())->getBitWidth())
3208       return new ICmpInst(I.getPredicate(), A,
3209                           Builder->CreateTrunc(B, A->getType()));
3210   }
3211
3212   // (A >> C) == (B >> C) --> (A^B) u< (1 << C)
3213   // For lshr and ashr pairs.
3214   if ((match(Op0, m_OneUse(m_LShr(m_Value(A), m_ConstantInt(Cst1)))) &&
3215        match(Op1, m_OneUse(m_LShr(m_Value(B), m_Specific(Cst1))))) ||
3216       (match(Op0, m_OneUse(m_AShr(m_Value(A), m_ConstantInt(Cst1)))) &&
3217        match(Op1, m_OneUse(m_AShr(m_Value(B), m_Specific(Cst1)))))) {
3218     unsigned TypeBits = Cst1->getBitWidth();
3219     unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits);
3220     if (ShAmt < TypeBits && ShAmt != 0) {
3221       ICmpInst::Predicate Pred = I.getPredicate() == ICmpInst::ICMP_NE
3222                                      ? ICmpInst::ICMP_UGE
3223                                      : ICmpInst::ICMP_ULT;
3224       Value *Xor = Builder->CreateXor(A, B, I.getName() + ".unshifted");
3225       APInt CmpVal = APInt::getOneBitSet(TypeBits, ShAmt);
3226       return new ICmpInst(Pred, Xor, Builder->getInt(CmpVal));
3227     }
3228   }
3229
3230   // (A << C) == (B << C) --> ((A^B) & (~0U >> C)) == 0
3231   if (match(Op0, m_OneUse(m_Shl(m_Value(A), m_ConstantInt(Cst1)))) &&
3232       match(Op1, m_OneUse(m_Shl(m_Value(B), m_Specific(Cst1))))) {
3233     unsigned TypeBits = Cst1->getBitWidth();
3234     unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits);
3235     if (ShAmt < TypeBits && ShAmt != 0) {
3236       Value *Xor = Builder->CreateXor(A, B, I.getName() + ".unshifted");
3237       APInt AndVal = APInt::getLowBitsSet(TypeBits, TypeBits - ShAmt);
3238       Value *And = Builder->CreateAnd(Xor, Builder->getInt(AndVal),
3239                                       I.getName() + ".mask");
3240       return new ICmpInst(I.getPredicate(), And,
3241                           Constant::getNullValue(Cst1->getType()));
3242     }
3243   }
3244
3245   // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to
3246   // "icmp (and X, mask), cst"
3247   uint64_t ShAmt = 0;
3248   if (Op0->hasOneUse() &&
3249       match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A), m_ConstantInt(ShAmt))))) &&
3250       match(Op1, m_ConstantInt(Cst1)) &&
3251       // Only do this when A has multiple uses.  This is most important to do
3252       // when it exposes other optimizations.
3253       !A->hasOneUse()) {
3254     unsigned ASize = cast<IntegerType>(A->getType())->getPrimitiveSizeInBits();
3255
3256     if (ShAmt < ASize) {
3257       APInt MaskV =
3258           APInt::getLowBitsSet(ASize, Op0->getType()->getPrimitiveSizeInBits());
3259       MaskV <<= ShAmt;
3260
3261       APInt CmpV = Cst1->getValue().zext(ASize);
3262       CmpV <<= ShAmt;
3263
3264       Value *Mask = Builder->CreateAnd(A, Builder->getInt(MaskV));
3265       return new ICmpInst(I.getPredicate(), Mask, Builder->getInt(CmpV));
3266     }
3267   }
3268
3269   return nullptr;
3270 }
3271
3272 /// Handle icmp (cast x to y), (cast/cst). We only handle extending casts so
3273 /// far.
3274 Instruction *InstCombiner::foldICmpWithCastAndCast(ICmpInst &ICmp) {
3275   const CastInst *LHSCI = cast<CastInst>(ICmp.getOperand(0));
3276   Value *LHSCIOp        = LHSCI->getOperand(0);
3277   Type *SrcTy     = LHSCIOp->getType();
3278   Type *DestTy    = LHSCI->getType();
3279   Value *RHSCIOp;
3280
3281   // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
3282   // integer type is the same size as the pointer type.
3283   if (LHSCI->getOpcode() == Instruction::PtrToInt &&
3284       DL.getPointerTypeSizeInBits(SrcTy) == DestTy->getIntegerBitWidth()) {
3285     Value *RHSOp = nullptr;
3286     if (auto *RHSC = dyn_cast<PtrToIntOperator>(ICmp.getOperand(1))) {
3287       Value *RHSCIOp = RHSC->getOperand(0);
3288       if (RHSCIOp->getType()->getPointerAddressSpace() ==
3289           LHSCIOp->getType()->getPointerAddressSpace()) {
3290         RHSOp = RHSC->getOperand(0);
3291         // If the pointer types don't match, insert a bitcast.
3292         if (LHSCIOp->getType() != RHSOp->getType())
3293           RHSOp = Builder->CreateBitCast(RHSOp, LHSCIOp->getType());
3294       }
3295     } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) {
3296       RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy);
3297     }
3298
3299     if (RHSOp)
3300       return new ICmpInst(ICmp.getPredicate(), LHSCIOp, RHSOp);
3301   }
3302
3303   // The code below only handles extension cast instructions, so far.
3304   // Enforce this.
3305   if (LHSCI->getOpcode() != Instruction::ZExt &&
3306       LHSCI->getOpcode() != Instruction::SExt)
3307     return nullptr;
3308
3309   bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt;
3310   bool isSignedCmp = ICmp.isSigned();
3311
3312   if (auto *CI = dyn_cast<CastInst>(ICmp.getOperand(1))) {
3313     // Not an extension from the same type?
3314     RHSCIOp = CI->getOperand(0);
3315     if (RHSCIOp->getType() != LHSCIOp->getType())
3316       return nullptr;
3317
3318     // If the signedness of the two casts doesn't agree (i.e. one is a sext
3319     // and the other is a zext), then we can't handle this.
3320     if (CI->getOpcode() != LHSCI->getOpcode())
3321       return nullptr;
3322
3323     // Deal with equality cases early.
3324     if (ICmp.isEquality())
3325       return new ICmpInst(ICmp.getPredicate(), LHSCIOp, RHSCIOp);
3326
3327     // A signed comparison of sign extended values simplifies into a
3328     // signed comparison.
3329     if (isSignedCmp && isSignedExt)
3330       return new ICmpInst(ICmp.getPredicate(), LHSCIOp, RHSCIOp);
3331
3332     // The other three cases all fold into an unsigned comparison.
3333     return new ICmpInst(ICmp.getUnsignedPredicate(), LHSCIOp, RHSCIOp);
3334   }
3335
3336   // If we aren't dealing with a constant on the RHS, exit early.
3337   auto *C = dyn_cast<Constant>(ICmp.getOperand(1));
3338   if (!C)
3339     return nullptr;
3340
3341   // Compute the constant that would happen if we truncated to SrcTy then
3342   // re-extended to DestTy.
3343   Constant *Res1 = ConstantExpr::getTrunc(C, SrcTy);
3344   Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy);
3345
3346   // If the re-extended constant didn't change...
3347   if (Res2 == C) {
3348     // Deal with equality cases early.
3349     if (ICmp.isEquality())
3350       return new ICmpInst(ICmp.getPredicate(), LHSCIOp, Res1);
3351
3352     // A signed comparison of sign extended values simplifies into a
3353     // signed comparison.
3354     if (isSignedExt && isSignedCmp)
3355       return new ICmpInst(ICmp.getPredicate(), LHSCIOp, Res1);
3356
3357     // The other three cases all fold into an unsigned comparison.
3358     return new ICmpInst(ICmp.getUnsignedPredicate(), LHSCIOp, Res1);
3359   }
3360
3361   // The re-extended constant changed, partly changed (in the case of a vector),
3362   // or could not be determined to be equal (in the case of a constant
3363   // expression), so the constant cannot be represented in the shorter type.
3364   // Consequently, we cannot emit a simple comparison.
3365   // All the cases that fold to true or false will have already been handled
3366   // by SimplifyICmpInst, so only deal with the tricky case.
3367
3368   if (isSignedCmp || !isSignedExt || !isa<ConstantInt>(C))
3369     return nullptr;
3370
3371   // Evaluate the comparison for LT (we invert for GT below). LE and GE cases
3372   // should have been folded away previously and not enter in here.
3373
3374   // We're performing an unsigned comp with a sign extended value.
3375   // This is true if the input is >= 0. [aka >s -1]
3376   Constant *NegOne = Constant::getAllOnesValue(SrcTy);
3377   Value *Result = Builder->CreateICmpSGT(LHSCIOp, NegOne, ICmp.getName());
3378
3379   // Finally, return the value computed.
3380   if (ICmp.getPredicate() == ICmpInst::ICMP_ULT)
3381     return replaceInstUsesWith(ICmp, Result);
3382
3383   assert(ICmp.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");
3384   return BinaryOperator::CreateNot(Result);
3385 }
3386
3387 bool InstCombiner::OptimizeOverflowCheck(OverflowCheckFlavor OCF, Value *LHS,
3388                                          Value *RHS, Instruction &OrigI,
3389                                          Value *&Result, Constant *&Overflow) {
3390   if (OrigI.isCommutative() && isa<Constant>(LHS) && !isa<Constant>(RHS))
3391     std::swap(LHS, RHS);
3392
3393   auto SetResult = [&](Value *OpResult, Constant *OverflowVal, bool ReuseName) {
3394     Result = OpResult;
3395     Overflow = OverflowVal;
3396     if (ReuseName)
3397       Result->takeName(&OrigI);
3398     return true;
3399   };
3400
3401   // If the overflow check was an add followed by a compare, the insertion point
3402   // may be pointing to the compare.  We want to insert the new instructions
3403   // before the add in case there are uses of the add between the add and the
3404   // compare.
3405   Builder->SetInsertPoint(&OrigI);
3406
3407   switch (OCF) {
3408   case OCF_INVALID:
3409     llvm_unreachable("bad overflow check kind!");
3410
3411   case OCF_UNSIGNED_ADD: {
3412     OverflowResult OR = computeOverflowForUnsignedAdd(LHS, RHS, &OrigI);
3413     if (OR == OverflowResult::NeverOverflows)
3414       return SetResult(Builder->CreateNUWAdd(LHS, RHS), Builder->getFalse(),
3415                        true);
3416
3417     if (OR == OverflowResult::AlwaysOverflows)
3418       return SetResult(Builder->CreateAdd(LHS, RHS), Builder->getTrue(), true);
3419
3420     // Fall through uadd into sadd
3421     LLVM_FALLTHROUGH;
3422   }
3423   case OCF_SIGNED_ADD: {
3424     // X + 0 -> {X, false}
3425     if (match(RHS, m_Zero()))
3426       return SetResult(LHS, Builder->getFalse(), false);
3427
3428     // We can strength reduce this signed add into a regular add if we can prove
3429     // that it will never overflow.
3430     if (OCF == OCF_SIGNED_ADD)
3431       if (WillNotOverflowSignedAdd(LHS, RHS, OrigI))
3432         return SetResult(Builder->CreateNSWAdd(LHS, RHS), Builder->getFalse(),
3433                          true);
3434     break;
3435   }
3436
3437   case OCF_UNSIGNED_SUB:
3438   case OCF_SIGNED_SUB: {
3439     // X - 0 -> {X, false}
3440     if (match(RHS, m_Zero()))
3441       return SetResult(LHS, Builder->getFalse(), false);
3442
3443     if (OCF == OCF_SIGNED_SUB) {
3444       if (WillNotOverflowSignedSub(LHS, RHS, OrigI))
3445         return SetResult(Builder->CreateNSWSub(LHS, RHS), Builder->getFalse(),
3446                          true);
3447     } else {
3448       if (WillNotOverflowUnsignedSub(LHS, RHS, OrigI))
3449         return SetResult(Builder->CreateNUWSub(LHS, RHS), Builder->getFalse(),
3450                          true);
3451     }
3452     break;
3453   }
3454
3455   case OCF_UNSIGNED_MUL: {
3456     OverflowResult OR = computeOverflowForUnsignedMul(LHS, RHS, &OrigI);
3457     if (OR == OverflowResult::NeverOverflows)
3458       return SetResult(Builder->CreateNUWMul(LHS, RHS), Builder->getFalse(),
3459                        true);
3460     if (OR == OverflowResult::AlwaysOverflows)
3461       return SetResult(Builder->CreateMul(LHS, RHS), Builder->getTrue(), true);
3462     LLVM_FALLTHROUGH;
3463   }
3464   case OCF_SIGNED_MUL:
3465     // X * undef -> undef
3466     if (isa<UndefValue>(RHS))
3467       return SetResult(RHS, UndefValue::get(Builder->getInt1Ty()), false);
3468
3469     // X * 0 -> {0, false}
3470     if (match(RHS, m_Zero()))
3471       return SetResult(RHS, Builder->getFalse(), false);
3472
3473     // X * 1 -> {X, false}
3474     if (match(RHS, m_One()))
3475       return SetResult(LHS, Builder->getFalse(), false);
3476
3477     if (OCF == OCF_SIGNED_MUL)
3478       if (WillNotOverflowSignedMul(LHS, RHS, OrigI))
3479         return SetResult(Builder->CreateNSWMul(LHS, RHS), Builder->getFalse(),
3480                          true);
3481     break;
3482   }
3483
3484   return false;
3485 }
3486
3487 /// \brief Recognize and process idiom involving test for multiplication
3488 /// overflow.
3489 ///
3490 /// The caller has matched a pattern of the form:
3491 ///   I = cmp u (mul(zext A, zext B), V
3492 /// The function checks if this is a test for overflow and if so replaces
3493 /// multiplication with call to 'mul.with.overflow' intrinsic.
3494 ///
3495 /// \param I Compare instruction.
3496 /// \param MulVal Result of 'mult' instruction.  It is one of the arguments of
3497 ///               the compare instruction.  Must be of integer type.
3498 /// \param OtherVal The other argument of compare instruction.
3499 /// \returns Instruction which must replace the compare instruction, NULL if no
3500 ///          replacement required.
3501 static Instruction *processUMulZExtIdiom(ICmpInst &I, Value *MulVal,
3502                                          Value *OtherVal, InstCombiner &IC) {
3503   // Don't bother doing this transformation for pointers, don't do it for
3504   // vectors.
3505   if (!isa<IntegerType>(MulVal->getType()))
3506     return nullptr;
3507
3508   assert(I.getOperand(0) == MulVal || I.getOperand(1) == MulVal);
3509   assert(I.getOperand(0) == OtherVal || I.getOperand(1) == OtherVal);
3510   auto *MulInstr = dyn_cast<Instruction>(MulVal);
3511   if (!MulInstr)
3512     return nullptr;
3513   assert(MulInstr->getOpcode() == Instruction::Mul);
3514
3515   auto *LHS = cast<ZExtOperator>(MulInstr->getOperand(0)),
3516        *RHS = cast<ZExtOperator>(MulInstr->getOperand(1));
3517   assert(LHS->getOpcode() == Instruction::ZExt);
3518   assert(RHS->getOpcode() == Instruction::ZExt);
3519   Value *A = LHS->getOperand(0), *B = RHS->getOperand(0);
3520
3521   // Calculate type and width of the result produced by mul.with.overflow.
3522   Type *TyA = A->getType(), *TyB = B->getType();
3523   unsigned WidthA = TyA->getPrimitiveSizeInBits(),
3524            WidthB = TyB->getPrimitiveSizeInBits();
3525   unsigned MulWidth;
3526   Type *MulType;
3527   if (WidthB > WidthA) {
3528     MulWidth = WidthB;
3529     MulType = TyB;
3530   } else {
3531     MulWidth = WidthA;
3532     MulType = TyA;
3533   }
3534
3535   // In order to replace the original mul with a narrower mul.with.overflow,
3536   // all uses must ignore upper bits of the product.  The number of used low
3537   // bits must be not greater than the width of mul.with.overflow.
3538   if (MulVal->hasNUsesOrMore(2))
3539     for (User *U : MulVal->users()) {
3540       if (U == &I)
3541         continue;
3542       if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
3543         // Check if truncation ignores bits above MulWidth.
3544         unsigned TruncWidth = TI->getType()->getPrimitiveSizeInBits();
3545         if (TruncWidth > MulWidth)
3546           return nullptr;
3547       } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
3548         // Check if AND ignores bits above MulWidth.
3549         if (BO->getOpcode() != Instruction::And)
3550           return nullptr;
3551         if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
3552           const APInt &CVal = CI->getValue();
3553           if (CVal.getBitWidth() - CVal.countLeadingZeros() > MulWidth)
3554             return nullptr;
3555         }
3556       } else {
3557         // Other uses prohibit this transformation.
3558         return nullptr;
3559       }
3560     }
3561
3562   // Recognize patterns
3563   switch (I.getPredicate()) {
3564   case ICmpInst::ICMP_EQ:
3565   case ICmpInst::ICMP_NE:
3566     // Recognize pattern:
3567     //   mulval = mul(zext A, zext B)
3568     //   cmp eq/neq mulval, zext trunc mulval
3569     if (ZExtInst *Zext = dyn_cast<ZExtInst>(OtherVal))
3570       if (Zext->hasOneUse()) {
3571         Value *ZextArg = Zext->getOperand(0);
3572         if (TruncInst *Trunc = dyn_cast<TruncInst>(ZextArg))
3573           if (Trunc->getType()->getPrimitiveSizeInBits() == MulWidth)
3574             break; //Recognized
3575       }
3576
3577     // Recognize pattern:
3578     //   mulval = mul(zext A, zext B)
3579     //   cmp eq/neq mulval, and(mulval, mask), mask selects low MulWidth bits.
3580     ConstantInt *CI;
3581     Value *ValToMask;
3582     if (match(OtherVal, m_And(m_Value(ValToMask), m_ConstantInt(CI)))) {
3583       if (ValToMask != MulVal)
3584         return nullptr;
3585       const APInt &CVal = CI->getValue() + 1;
3586       if (CVal.isPowerOf2()) {
3587         unsigned MaskWidth = CVal.logBase2();
3588         if (MaskWidth == MulWidth)
3589           break; // Recognized
3590       }
3591     }
3592     return nullptr;
3593
3594   case ICmpInst::ICMP_UGT:
3595     // Recognize pattern:
3596     //   mulval = mul(zext A, zext B)
3597     //   cmp ugt mulval, max
3598     if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
3599       APInt MaxVal = APInt::getMaxValue(MulWidth);
3600       MaxVal = MaxVal.zext(CI->getBitWidth());
3601       if (MaxVal.eq(CI->getValue()))
3602         break; // Recognized
3603     }
3604     return nullptr;
3605
3606   case ICmpInst::ICMP_UGE:
3607     // Recognize pattern:
3608     //   mulval = mul(zext A, zext B)
3609     //   cmp uge mulval, max+1
3610     if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
3611       APInt MaxVal = APInt::getOneBitSet(CI->getBitWidth(), MulWidth);
3612       if (MaxVal.eq(CI->getValue()))
3613         break; // Recognized
3614     }
3615     return nullptr;
3616
3617   case ICmpInst::ICMP_ULE:
3618     // Recognize pattern:
3619     //   mulval = mul(zext A, zext B)
3620     //   cmp ule mulval, max
3621     if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
3622       APInt MaxVal = APInt::getMaxValue(MulWidth);
3623       MaxVal = MaxVal.zext(CI->getBitWidth());
3624       if (MaxVal.eq(CI->getValue()))
3625         break; // Recognized
3626     }
3627     return nullptr;
3628
3629   case ICmpInst::ICMP_ULT:
3630     // Recognize pattern:
3631     //   mulval = mul(zext A, zext B)
3632     //   cmp ule mulval, max + 1
3633     if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
3634       APInt MaxVal = APInt::getOneBitSet(CI->getBitWidth(), MulWidth);
3635       if (MaxVal.eq(CI->getValue()))
3636         break; // Recognized
3637     }
3638     return nullptr;
3639
3640   default:
3641     return nullptr;
3642   }
3643
3644   InstCombiner::BuilderTy *Builder = IC.Builder;
3645   Builder->SetInsertPoint(MulInstr);
3646
3647   // Replace: mul(zext A, zext B) --> mul.with.overflow(A, B)
3648   Value *MulA = A, *MulB = B;
3649   if (WidthA < MulWidth)
3650     MulA = Builder->CreateZExt(A, MulType);
3651   if (WidthB < MulWidth)
3652     MulB = Builder->CreateZExt(B, MulType);
3653   Value *F = Intrinsic::getDeclaration(I.getModule(),
3654                                        Intrinsic::umul_with_overflow, MulType);
3655   CallInst *Call = Builder->CreateCall(F, {MulA, MulB}, "umul");
3656   IC.Worklist.Add(MulInstr);
3657
3658   // If there are uses of mul result other than the comparison, we know that
3659   // they are truncation or binary AND. Change them to use result of
3660   // mul.with.overflow and adjust properly mask/size.
3661   if (MulVal->hasNUsesOrMore(2)) {
3662     Value *Mul = Builder->CreateExtractValue(Call, 0, "umul.value");
3663     for (User *U : MulVal->users()) {
3664       if (U == &I || U == OtherVal)
3665         continue;
3666       if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
3667         if (TI->getType()->getPrimitiveSizeInBits() == MulWidth)
3668           IC.replaceInstUsesWith(*TI, Mul);
3669         else
3670           TI->setOperand(0, Mul);
3671       } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
3672         assert(BO->getOpcode() == Instruction::And);
3673         // Replace (mul & mask) --> zext (mul.with.overflow & short_mask)
3674         ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
3675         APInt ShortMask = CI->getValue().trunc(MulWidth);
3676         Value *ShortAnd = Builder->CreateAnd(Mul, ShortMask);
3677         Instruction *Zext =
3678             cast<Instruction>(Builder->CreateZExt(ShortAnd, BO->getType()));
3679         IC.Worklist.Add(Zext);
3680         IC.replaceInstUsesWith(*BO, Zext);
3681       } else {
3682         llvm_unreachable("Unexpected Binary operation");
3683       }
3684       IC.Worklist.Add(cast<Instruction>(U));
3685     }
3686   }
3687   if (isa<Instruction>(OtherVal))
3688     IC.Worklist.Add(cast<Instruction>(OtherVal));
3689
3690   // The original icmp gets replaced with the overflow value, maybe inverted
3691   // depending on predicate.
3692   bool Inverse = false;
3693   switch (I.getPredicate()) {
3694   case ICmpInst::ICMP_NE:
3695     break;
3696   case ICmpInst::ICMP_EQ:
3697     Inverse = true;
3698     break;
3699   case ICmpInst::ICMP_UGT:
3700   case ICmpInst::ICMP_UGE:
3701     if (I.getOperand(0) == MulVal)
3702       break;
3703     Inverse = true;
3704     break;
3705   case ICmpInst::ICMP_ULT:
3706   case ICmpInst::ICMP_ULE:
3707     if (I.getOperand(1) == MulVal)
3708       break;
3709     Inverse = true;
3710     break;
3711   default:
3712     llvm_unreachable("Unexpected predicate");
3713   }
3714   if (Inverse) {
3715     Value *Res = Builder->CreateExtractValue(Call, 1);
3716     return BinaryOperator::CreateNot(Res);
3717   }
3718
3719   return ExtractValueInst::Create(Call, 1);
3720 }
3721
3722 /// When performing a comparison against a constant, it is possible that not all
3723 /// the bits in the LHS are demanded. This helper method computes the mask that
3724 /// IS demanded.
3725 static APInt getDemandedBitsLHSMask(ICmpInst &I, unsigned BitWidth,
3726                                     bool isSignCheck) {
3727   if (isSignCheck)
3728     return APInt::getSignBit(BitWidth);
3729
3730   ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1));
3731   if (!CI) return APInt::getAllOnesValue(BitWidth);
3732   const APInt &RHS = CI->getValue();
3733
3734   switch (I.getPredicate()) {
3735   // For a UGT comparison, we don't care about any bits that
3736   // correspond to the trailing ones of the comparand.  The value of these
3737   // bits doesn't impact the outcome of the comparison, because any value
3738   // greater than the RHS must differ in a bit higher than these due to carry.
3739   case ICmpInst::ICMP_UGT: {
3740     unsigned trailingOnes = RHS.countTrailingOnes();
3741     APInt lowBitsSet = APInt::getLowBitsSet(BitWidth, trailingOnes);
3742     return ~lowBitsSet;
3743   }
3744
3745   // Similarly, for a ULT comparison, we don't care about the trailing zeros.
3746   // Any value less than the RHS must differ in a higher bit because of carries.
3747   case ICmpInst::ICMP_ULT: {
3748     unsigned trailingZeros = RHS.countTrailingZeros();
3749     APInt lowBitsSet = APInt::getLowBitsSet(BitWidth, trailingZeros);
3750     return ~lowBitsSet;
3751   }
3752
3753   default:
3754     return APInt::getAllOnesValue(BitWidth);
3755   }
3756 }
3757
3758 /// \brief Check if the order of \p Op0 and \p Op1 as operand in an ICmpInst
3759 /// should be swapped.
3760 /// The decision is based on how many times these two operands are reused
3761 /// as subtract operands and their positions in those instructions.
3762 /// The rational is that several architectures use the same instruction for
3763 /// both subtract and cmp, thus it is better if the order of those operands
3764 /// match.
3765 /// \return true if Op0 and Op1 should be swapped.
3766 static bool swapMayExposeCSEOpportunities(const Value * Op0,
3767                                           const Value * Op1) {
3768   // Filter out pointer value as those cannot appears directly in subtract.
3769   // FIXME: we may want to go through inttoptrs or bitcasts.
3770   if (Op0->getType()->isPointerTy())
3771     return false;
3772   // Count every uses of both Op0 and Op1 in a subtract.
3773   // Each time Op0 is the first operand, count -1: swapping is bad, the
3774   // subtract has already the same layout as the compare.
3775   // Each time Op0 is the second operand, count +1: swapping is good, the
3776   // subtract has a different layout as the compare.
3777   // At the end, if the benefit is greater than 0, Op0 should come second to
3778   // expose more CSE opportunities.
3779   int GlobalSwapBenefits = 0;
3780   for (const User *U : Op0->users()) {
3781     const BinaryOperator *BinOp = dyn_cast<BinaryOperator>(U);
3782     if (!BinOp || BinOp->getOpcode() != Instruction::Sub)
3783       continue;
3784     // If Op0 is the first argument, this is not beneficial to swap the
3785     // arguments.
3786     int LocalSwapBenefits = -1;
3787     unsigned Op1Idx = 1;
3788     if (BinOp->getOperand(Op1Idx) == Op0) {
3789       Op1Idx = 0;
3790       LocalSwapBenefits = 1;
3791     }
3792     if (BinOp->getOperand(Op1Idx) != Op1)
3793       continue;
3794     GlobalSwapBenefits += LocalSwapBenefits;
3795   }
3796   return GlobalSwapBenefits > 0;
3797 }
3798
3799 /// \brief Check that one use is in the same block as the definition and all
3800 /// other uses are in blocks dominated by a given block.
3801 ///
3802 /// \param DI Definition
3803 /// \param UI Use
3804 /// \param DB Block that must dominate all uses of \p DI outside
3805 ///           the parent block
3806 /// \return true when \p UI is the only use of \p DI in the parent block
3807 /// and all other uses of \p DI are in blocks dominated by \p DB.
3808 ///
3809 bool InstCombiner::dominatesAllUses(const Instruction *DI,
3810                                     const Instruction *UI,
3811                                     const BasicBlock *DB) const {
3812   assert(DI && UI && "Instruction not defined\n");
3813   // Ignore incomplete definitions.
3814   if (!DI->getParent())
3815     return false;
3816   // DI and UI must be in the same block.
3817   if (DI->getParent() != UI->getParent())
3818     return false;
3819   // Protect from self-referencing blocks.
3820   if (DI->getParent() == DB)
3821     return false;
3822   for (const User *U : DI->users()) {
3823     auto *Usr = cast<Instruction>(U);
3824     if (Usr != UI && !DT.dominates(DB, Usr->getParent()))
3825       return false;
3826   }
3827   return true;
3828 }
3829
3830 /// Return true when the instruction sequence within a block is select-cmp-br.
3831 static bool isChainSelectCmpBranch(const SelectInst *SI) {
3832   const BasicBlock *BB = SI->getParent();
3833   if (!BB)
3834     return false;
3835   auto *BI = dyn_cast_or_null<BranchInst>(BB->getTerminator());
3836   if (!BI || BI->getNumSuccessors() != 2)
3837     return false;
3838   auto *IC = dyn_cast<ICmpInst>(BI->getCondition());
3839   if (!IC || (IC->getOperand(0) != SI && IC->getOperand(1) != SI))
3840     return false;
3841   return true;
3842 }
3843
3844 /// \brief True when a select result is replaced by one of its operands
3845 /// in select-icmp sequence. This will eventually result in the elimination
3846 /// of the select.
3847 ///
3848 /// \param SI    Select instruction
3849 /// \param Icmp  Compare instruction
3850 /// \param SIOpd Operand that replaces the select
3851 ///
3852 /// Notes:
3853 /// - The replacement is global and requires dominator information
3854 /// - The caller is responsible for the actual replacement
3855 ///
3856 /// Example:
3857 ///
3858 /// entry:
3859 ///  %4 = select i1 %3, %C* %0, %C* null
3860 ///  %5 = icmp eq %C* %4, null
3861 ///  br i1 %5, label %9, label %7
3862 ///  ...
3863 ///  ; <label>:7                                       ; preds = %entry
3864 ///  %8 = getelementptr inbounds %C* %4, i64 0, i32 0
3865 ///  ...
3866 ///
3867 /// can be transformed to
3868 ///
3869 ///  %5 = icmp eq %C* %0, null
3870 ///  %6 = select i1 %3, i1 %5, i1 true
3871 ///  br i1 %6, label %9, label %7
3872 ///  ...
3873 ///  ; <label>:7                                       ; preds = %entry
3874 ///  %8 = getelementptr inbounds %C* %0, i64 0, i32 0  // replace by %0!
3875 ///
3876 /// Similar when the first operand of the select is a constant or/and
3877 /// the compare is for not equal rather than equal.
3878 ///
3879 /// NOTE: The function is only called when the select and compare constants
3880 /// are equal, the optimization can work only for EQ predicates. This is not a
3881 /// major restriction since a NE compare should be 'normalized' to an equal
3882 /// compare, which usually happens in the combiner and test case
3883 /// select-cmp-br.ll checks for it.
3884 bool InstCombiner::replacedSelectWithOperand(SelectInst *SI,
3885                                              const ICmpInst *Icmp,
3886                                              const unsigned SIOpd) {
3887   assert((SIOpd == 1 || SIOpd == 2) && "Invalid select operand!");
3888   if (isChainSelectCmpBranch(SI) && Icmp->getPredicate() == ICmpInst::ICMP_EQ) {
3889     BasicBlock *Succ = SI->getParent()->getTerminator()->getSuccessor(1);
3890     // The check for the unique predecessor is not the best that can be
3891     // done. But it protects efficiently against cases like when SI's
3892     // home block has two successors, Succ and Succ1, and Succ1 predecessor
3893     // of Succ. Then SI can't be replaced by SIOpd because the use that gets
3894     // replaced can be reached on either path. So the uniqueness check
3895     // guarantees that the path all uses of SI (outside SI's parent) are on
3896     // is disjoint from all other paths out of SI. But that information
3897     // is more expensive to compute, and the trade-off here is in favor
3898     // of compile-time.
3899     if (Succ->getUniquePredecessor() && dominatesAllUses(SI, Icmp, Succ)) {
3900       NumSel++;
3901       SI->replaceUsesOutsideBlock(SI->getOperand(SIOpd), SI->getParent());
3902       return true;
3903     }
3904   }
3905   return false;
3906 }
3907
3908 /// Try to fold the comparison based on range information we can get by checking
3909 /// whether bits are known to be zero or one in the inputs.
3910 Instruction *InstCombiner::foldICmpUsingKnownBits(ICmpInst &I) {
3911   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3912   Type *Ty = Op0->getType();
3913   ICmpInst::Predicate Pred = I.getPredicate();
3914
3915   // Get scalar or pointer size.
3916   unsigned BitWidth = Ty->isIntOrIntVectorTy()
3917                           ? Ty->getScalarSizeInBits()
3918                           : DL.getTypeSizeInBits(Ty->getScalarType());
3919
3920   if (!BitWidth)
3921     return nullptr;
3922
3923   // If this is a normal comparison, it demands all bits. If it is a sign bit
3924   // comparison, it only demands the sign bit.
3925   bool IsSignBit = false;
3926   const APInt *CmpC;
3927   if (match(Op1, m_APInt(CmpC))) {
3928     bool UnusedBit;
3929     IsSignBit = isSignBitCheck(Pred, *CmpC, UnusedBit);
3930   }
3931
3932   APInt Op0KnownZero(BitWidth, 0), Op0KnownOne(BitWidth, 0);
3933   APInt Op1KnownZero(BitWidth, 0), Op1KnownOne(BitWidth, 0);
3934
3935   if (SimplifyDemandedBits(I.getOperandUse(0),
3936                            getDemandedBitsLHSMask(I, BitWidth, IsSignBit),
3937                            Op0KnownZero, Op0KnownOne, 0))
3938     return &I;
3939
3940   if (SimplifyDemandedBits(I.getOperandUse(1), APInt::getAllOnesValue(BitWidth),
3941                            Op1KnownZero, Op1KnownOne, 0))
3942     return &I;
3943
3944   // Given the known and unknown bits, compute a range that the LHS could be
3945   // in.  Compute the Min, Max and RHS values based on the known bits. For the
3946   // EQ and NE we use unsigned values.
3947   APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
3948   APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
3949   if (I.isSigned()) {
3950     computeSignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne, Op0Min,
3951                                            Op0Max);
3952     computeSignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne, Op1Min,
3953                                            Op1Max);
3954   } else {
3955     computeUnsignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne, Op0Min,
3956                                              Op0Max);
3957     computeUnsignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne, Op1Min,
3958                                              Op1Max);
3959   }
3960
3961   // If Min and Max are known to be the same, then SimplifyDemandedBits
3962   // figured out that the LHS is a constant. Constant fold this now, so that
3963   // code below can assume that Min != Max.
3964   if (!isa<Constant>(Op0) && Op0Min == Op0Max)
3965     return new ICmpInst(Pred, ConstantInt::get(Op0->getType(), Op0Min), Op1);
3966   if (!isa<Constant>(Op1) && Op1Min == Op1Max)
3967     return new ICmpInst(Pred, Op0, ConstantInt::get(Op1->getType(), Op1Min));
3968
3969   // Based on the range information we know about the LHS, see if we can
3970   // simplify this comparison.  For example, (x&4) < 8 is always true.
3971   switch (Pred) {
3972   default:
3973     llvm_unreachable("Unknown icmp opcode!");
3974   case ICmpInst::ICMP_EQ:
3975   case ICmpInst::ICMP_NE: {
3976     if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max)) {
3977       return Pred == CmpInst::ICMP_EQ
3978                  ? replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()))
3979                  : replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
3980     }
3981
3982     // If all bits are known zero except for one, then we know at most one bit
3983     // is set. If the comparison is against zero, then this is a check to see if
3984     // *that* bit is set.
3985     APInt Op0KnownZeroInverted = ~Op0KnownZero;
3986     if (~Op1KnownZero == 0) {
3987       // If the LHS is an AND with the same constant, look through it.
3988       Value *LHS = nullptr;
3989       const APInt *LHSC;
3990       if (!match(Op0, m_And(m_Value(LHS), m_APInt(LHSC))) ||
3991           *LHSC != Op0KnownZeroInverted)
3992         LHS = Op0;
3993
3994       Value *X;
3995       if (match(LHS, m_Shl(m_One(), m_Value(X)))) {
3996         APInt ValToCheck = Op0KnownZeroInverted;
3997         Type *XTy = X->getType();
3998         if (ValToCheck.isPowerOf2()) {
3999           // ((1 << X) & 8) == 0 -> X != 3
4000           // ((1 << X) & 8) != 0 -> X == 3
4001           auto *CmpC = ConstantInt::get(XTy, ValToCheck.countTrailingZeros());
4002           auto NewPred = ICmpInst::getInversePredicate(Pred);
4003           return new ICmpInst(NewPred, X, CmpC);
4004         } else if ((++ValToCheck).isPowerOf2()) {
4005           // ((1 << X) & 7) == 0 -> X >= 3
4006           // ((1 << X) & 7) != 0 -> X  < 3
4007           auto *CmpC = ConstantInt::get(XTy, ValToCheck.countTrailingZeros());
4008           auto NewPred =
4009               Pred == CmpInst::ICMP_EQ ? CmpInst::ICMP_UGE : CmpInst::ICMP_ULT;
4010           return new ICmpInst(NewPred, X, CmpC);
4011         }
4012       }
4013
4014       // Check if the LHS is 8 >>u x and the result is a power of 2 like 1.
4015       const APInt *CI;
4016       if (Op0KnownZeroInverted == 1 &&
4017           match(LHS, m_LShr(m_Power2(CI), m_Value(X)))) {
4018         // ((8 >>u X) & 1) == 0 -> X != 3
4019         // ((8 >>u X) & 1) != 0 -> X == 3
4020         unsigned CmpVal = CI->countTrailingZeros();
4021         auto NewPred = ICmpInst::getInversePredicate(Pred);
4022         return new ICmpInst(NewPred, X, ConstantInt::get(X->getType(), CmpVal));
4023       }
4024     }
4025     break;
4026   }
4027   case ICmpInst::ICMP_ULT: {
4028     if (Op0Max.ult(Op1Min)) // A <u B -> true if max(A) < min(B)
4029       return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
4030     if (Op0Min.uge(Op1Max)) // A <u B -> false if min(A) >= max(B)
4031       return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
4032     if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)
4033       return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4034
4035     const APInt *CmpC;
4036     if (match(Op1, m_APInt(CmpC))) {
4037       // A <u C -> A == C-1 if min(A)+1 == C
4038       if (Op1Max == Op0Min + 1) {
4039         Constant *CMinus1 = ConstantInt::get(Op0->getType(), *CmpC - 1);
4040         return new ICmpInst(ICmpInst::ICMP_EQ, Op0, CMinus1);
4041       }
4042     }
4043     break;
4044   }
4045   case ICmpInst::ICMP_UGT: {
4046     if (Op0Min.ugt(Op1Max)) // A >u B -> true if min(A) > max(B)
4047       return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
4048
4049     if (Op0Max.ule(Op1Min)) // A >u B -> false if max(A) <= max(B)
4050       return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
4051
4052     if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
4053       return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4054
4055     const APInt *CmpC;
4056     if (match(Op1, m_APInt(CmpC))) {
4057       // A >u C -> A == C+1 if max(a)-1 == C
4058       if (*CmpC == Op0Max - 1)
4059         return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
4060                             ConstantInt::get(Op1->getType(), *CmpC + 1));
4061     }
4062     break;
4063   }
4064   case ICmpInst::ICMP_SLT:
4065     if (Op0Max.slt(Op1Min)) // A <s B -> true if max(A) < min(C)
4066       return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
4067     if (Op0Min.sge(Op1Max)) // A <s B -> false if min(A) >= max(C)
4068       return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
4069     if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)
4070       return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4071     if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
4072       if (Op1Max == Op0Min + 1) // A <s C -> A == C-1 if min(A)+1 == C
4073         return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
4074                             Builder->getInt(CI->getValue() - 1));
4075     }
4076     break;
4077   case ICmpInst::ICMP_SGT:
4078     if (Op0Min.sgt(Op1Max)) // A >s B -> true if min(A) > max(B)
4079       return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
4080     if (Op0Max.sle(Op1Min)) // A >s B -> false if max(A) <= min(B)
4081       return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
4082
4083     if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
4084       return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
4085     if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
4086       if (Op1Min == Op0Max - 1) // A >s C -> A == C+1 if max(A)-1 == C
4087         return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
4088                             Builder->getInt(CI->getValue() + 1));
4089     }
4090     break;
4091   case ICmpInst::ICMP_SGE:
4092     assert(!isa<ConstantInt>(Op1) && "ICMP_SGE with ConstantInt not folded!");
4093     if (Op0Min.sge(Op1Max)) // A >=s B -> true if min(A) >= max(B)
4094       return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
4095     if (Op0Max.slt(Op1Min)) // A >=s B -> false if max(A) < min(B)
4096       return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
4097     break;
4098   case ICmpInst::ICMP_SLE:
4099     assert(!isa<ConstantInt>(Op1) && "ICMP_SLE with ConstantInt not folded!");
4100     if (Op0Max.sle(Op1Min)) // A <=s B -> true if max(A) <= min(B)
4101       return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
4102     if (Op0Min.sgt(Op1Max)) // A <=s B -> false if min(A) > max(B)
4103       return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
4104     break;
4105   case ICmpInst::ICMP_UGE:
4106     assert(!isa<ConstantInt>(Op1) && "ICMP_UGE with ConstantInt not folded!");
4107     if (Op0Min.uge(Op1Max)) // A >=u B -> true if min(A) >= max(B)
4108       return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
4109     if (Op0Max.ult(Op1Min)) // A >=u B -> false if max(A) < min(B)
4110       return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
4111     break;
4112   case ICmpInst::ICMP_ULE:
4113     assert(!isa<ConstantInt>(Op1) && "ICMP_ULE with ConstantInt not folded!");
4114     if (Op0Max.ule(Op1Min)) // A <=u B -> true if max(A) <= min(B)
4115       return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
4116     if (Op0Min.ugt(Op1Max)) // A <=u B -> false if min(A) > max(B)
4117       return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
4118     break;
4119   }
4120
4121   // Turn a signed comparison into an unsigned one if both operands are known to
4122   // have the same sign.
4123   if (I.isSigned() &&
4124       ((Op0KnownZero.isNegative() && Op1KnownZero.isNegative()) ||
4125        (Op0KnownOne.isNegative() && Op1KnownOne.isNegative())))
4126     return new ICmpInst(I.getUnsignedPredicate(), Op0, Op1);
4127
4128   return nullptr;
4129 }
4130
4131 /// If we have an icmp le or icmp ge instruction with a constant operand, turn
4132 /// it into the appropriate icmp lt or icmp gt instruction. This transform
4133 /// allows them to be folded in visitICmpInst.
4134 static ICmpInst *canonicalizeCmpWithConstant(ICmpInst &I) {
4135   ICmpInst::Predicate Pred = I.getPredicate();
4136   if (Pred != ICmpInst::ICMP_SLE && Pred != ICmpInst::ICMP_SGE &&
4137       Pred != ICmpInst::ICMP_ULE && Pred != ICmpInst::ICMP_UGE)
4138     return nullptr;
4139
4140   Value *Op0 = I.getOperand(0);
4141   Value *Op1 = I.getOperand(1);
4142   auto *Op1C = dyn_cast<Constant>(Op1);
4143   if (!Op1C)
4144     return nullptr;
4145
4146   // Check if the constant operand can be safely incremented/decremented without
4147   // overflowing/underflowing. For scalars, SimplifyICmpInst has already handled
4148   // the edge cases for us, so we just assert on them. For vectors, we must
4149   // handle the edge cases.
4150   Type *Op1Type = Op1->getType();
4151   bool IsSigned = I.isSigned();
4152   bool IsLE = (Pred == ICmpInst::ICMP_SLE || Pred == ICmpInst::ICMP_ULE);
4153   auto *CI = dyn_cast<ConstantInt>(Op1C);
4154   if (CI) {
4155     // A <= MAX -> TRUE ; A >= MIN -> TRUE
4156     assert(IsLE ? !CI->isMaxValue(IsSigned) : !CI->isMinValue(IsSigned));
4157   } else if (Op1Type->isVectorTy()) {
4158     // TODO? If the edge cases for vectors were guaranteed to be handled as they
4159     // are for scalar, we could remove the min/max checks. However, to do that,
4160     // we would have to use insertelement/shufflevector to replace edge values.
4161     unsigned NumElts = Op1Type->getVectorNumElements();
4162     for (unsigned i = 0; i != NumElts; ++i) {
4163       Constant *Elt = Op1C->getAggregateElement(i);
4164       if (!Elt)
4165         return nullptr;
4166
4167       if (isa<UndefValue>(Elt))
4168         continue;
4169
4170       // Bail out if we can't determine if this constant is min/max or if we
4171       // know that this constant is min/max.
4172       auto *CI = dyn_cast<ConstantInt>(Elt);
4173       if (!CI || (IsLE ? CI->isMaxValue(IsSigned) : CI->isMinValue(IsSigned)))
4174         return nullptr;
4175     }
4176   } else {
4177     // ConstantExpr?
4178     return nullptr;
4179   }
4180
4181   // Increment or decrement the constant and set the new comparison predicate:
4182   // ULE -> ULT ; UGE -> UGT ; SLE -> SLT ; SGE -> SGT
4183   Constant *OneOrNegOne = ConstantInt::get(Op1Type, IsLE ? 1 : -1, true);
4184   CmpInst::Predicate NewPred = IsLE ? ICmpInst::ICMP_ULT: ICmpInst::ICMP_UGT;
4185   NewPred = IsSigned ? ICmpInst::getSignedPredicate(NewPred) : NewPred;
4186   return new ICmpInst(NewPred, Op0, ConstantExpr::getAdd(Op1C, OneOrNegOne));
4187 }
4188
4189 Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
4190   bool Changed = false;
4191   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
4192   unsigned Op0Cplxity = getComplexity(Op0);
4193   unsigned Op1Cplxity = getComplexity(Op1);
4194
4195   /// Orders the operands of the compare so that they are listed from most
4196   /// complex to least complex.  This puts constants before unary operators,
4197   /// before binary operators.
4198   if (Op0Cplxity < Op1Cplxity ||
4199       (Op0Cplxity == Op1Cplxity && swapMayExposeCSEOpportunities(Op0, Op1))) {
4200     I.swapOperands();
4201     std::swap(Op0, Op1);
4202     Changed = true;
4203   }
4204
4205   if (Value *V =
4206           SimplifyICmpInst(I.getPredicate(), Op0, Op1, DL, &TLI, &DT, &AC, &I))
4207     return replaceInstUsesWith(I, V);
4208
4209   // comparing -val or val with non-zero is the same as just comparing val
4210   // ie, abs(val) != 0 -> val != 0
4211   if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) {
4212     Value *Cond, *SelectTrue, *SelectFalse;
4213     if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue),
4214                             m_Value(SelectFalse)))) {
4215       if (Value *V = dyn_castNegVal(SelectTrue)) {
4216         if (V == SelectFalse)
4217           return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
4218       }
4219       else if (Value *V = dyn_castNegVal(SelectFalse)) {
4220         if (V == SelectTrue)
4221           return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
4222       }
4223     }
4224   }
4225
4226   Type *Ty = Op0->getType();
4227
4228   // icmp's with boolean values can always be turned into bitwise operations
4229   if (Ty->getScalarType()->isIntegerTy(1)) {
4230     switch (I.getPredicate()) {
4231     default: llvm_unreachable("Invalid icmp instruction!");
4232     case ICmpInst::ICMP_EQ: {                // icmp eq i1 A, B -> ~(A^B)
4233       Value *Xor = Builder->CreateXor(Op0, Op1, I.getName() + "tmp");
4234       return BinaryOperator::CreateNot(Xor);
4235     }
4236     case ICmpInst::ICMP_NE:                  // icmp ne i1 A, B -> A^B
4237       return BinaryOperator::CreateXor(Op0, Op1);
4238
4239     case ICmpInst::ICMP_UGT:
4240       std::swap(Op0, Op1);                   // Change icmp ugt -> icmp ult
4241       LLVM_FALLTHROUGH;
4242     case ICmpInst::ICMP_ULT:{                // icmp ult i1 A, B -> ~A & B
4243       Value *Not = Builder->CreateNot(Op0, I.getName() + "tmp");
4244       return BinaryOperator::CreateAnd(Not, Op1);
4245     }
4246     case ICmpInst::ICMP_SGT:
4247       std::swap(Op0, Op1);                   // Change icmp sgt -> icmp slt
4248       LLVM_FALLTHROUGH;
4249     case ICmpInst::ICMP_SLT: {               // icmp slt i1 A, B -> A & ~B
4250       Value *Not = Builder->CreateNot(Op1, I.getName() + "tmp");
4251       return BinaryOperator::CreateAnd(Not, Op0);
4252     }
4253     case ICmpInst::ICMP_UGE:
4254       std::swap(Op0, Op1);                   // Change icmp uge -> icmp ule
4255       LLVM_FALLTHROUGH;
4256     case ICmpInst::ICMP_ULE: {               // icmp ule i1 A, B -> ~A | B
4257       Value *Not = Builder->CreateNot(Op0, I.getName() + "tmp");
4258       return BinaryOperator::CreateOr(Not, Op1);
4259     }
4260     case ICmpInst::ICMP_SGE:
4261       std::swap(Op0, Op1);                   // Change icmp sge -> icmp sle
4262       LLVM_FALLTHROUGH;
4263     case ICmpInst::ICMP_SLE: {               // icmp sle i1 A, B -> A | ~B
4264       Value *Not = Builder->CreateNot(Op1, I.getName() + "tmp");
4265       return BinaryOperator::CreateOr(Not, Op0);
4266     }
4267     }
4268   }
4269
4270   if (ICmpInst *NewICmp = canonicalizeCmpWithConstant(I))
4271     return NewICmp;
4272
4273   if (Instruction *Res = foldICmpWithConstant(I))
4274     return Res;
4275
4276   if (Instruction *Res = foldICmpUsingKnownBits(I))
4277     return Res;
4278
4279   // Test if the ICmpInst instruction is used exclusively by a select as
4280   // part of a minimum or maximum operation. If so, refrain from doing
4281   // any other folding. This helps out other analyses which understand
4282   // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
4283   // and CodeGen. And in this case, at least one of the comparison
4284   // operands has at least one user besides the compare (the select),
4285   // which would often largely negate the benefit of folding anyway.
4286   if (I.hasOneUse())
4287     if (SelectInst *SI = dyn_cast<SelectInst>(*I.user_begin()))
4288       if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) ||
4289           (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1))
4290         return nullptr;
4291
4292   // FIXME: We only do this after checking for min/max to prevent infinite
4293   // looping caused by a reverse canonicalization of these patterns for min/max.
4294   // FIXME: The organization of folds is a mess. These would naturally go into
4295   // canonicalizeCmpWithConstant(), but we can't move all of the above folds
4296   // down here after the min/max restriction.
4297   ICmpInst::Predicate Pred = I.getPredicate();
4298   const APInt *C;
4299   if (match(Op1, m_APInt(C))) {
4300     // For i32: x >u 2147483647 -> x <s 0  -> true if sign bit set
4301     if (Pred == ICmpInst::ICMP_UGT && C->isMaxSignedValue()) {
4302       Constant *Zero = Constant::getNullValue(Op0->getType());
4303       return new ICmpInst(ICmpInst::ICMP_SLT, Op0, Zero);
4304     }
4305
4306     // For i32: x <u 2147483648 -> x >s -1  -> true if sign bit clear
4307     if (Pred == ICmpInst::ICMP_ULT && C->isMinSignedValue()) {
4308       Constant *AllOnes = Constant::getAllOnesValue(Op0->getType());
4309       return new ICmpInst(ICmpInst::ICMP_SGT, Op0, AllOnes);
4310     }
4311   }
4312
4313   if (Instruction *Res = foldICmpInstWithConstant(I))
4314     return Res;
4315
4316   if (Instruction *Res = foldICmpInstWithConstantNotInt(I))
4317     return Res;
4318
4319   // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now.
4320   if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op0))
4321     if (Instruction *NI = foldGEPICmp(GEP, Op1, I.getPredicate(), I))
4322       return NI;
4323   if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op1))
4324     if (Instruction *NI = foldGEPICmp(GEP, Op0,
4325                            ICmpInst::getSwappedPredicate(I.getPredicate()), I))
4326       return NI;
4327
4328   // Try to optimize equality comparisons against alloca-based pointers.
4329   if (Op0->getType()->isPointerTy() && I.isEquality()) {
4330     assert(Op1->getType()->isPointerTy() && "Comparing pointer with non-pointer?");
4331     if (auto *Alloca = dyn_cast<AllocaInst>(GetUnderlyingObject(Op0, DL)))
4332       if (Instruction *New = foldAllocaCmp(I, Alloca, Op1))
4333         return New;
4334     if (auto *Alloca = dyn_cast<AllocaInst>(GetUnderlyingObject(Op1, DL)))
4335       if (Instruction *New = foldAllocaCmp(I, Alloca, Op0))
4336         return New;
4337   }
4338
4339   // Test to see if the operands of the icmp are casted versions of other
4340   // values.  If the ptr->ptr cast can be stripped off both arguments, we do so
4341   // now.
4342   if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) {
4343     if (Op0->getType()->isPointerTy() &&
4344         (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) {
4345       // We keep moving the cast from the left operand over to the right
4346       // operand, where it can often be eliminated completely.
4347       Op0 = CI->getOperand(0);
4348
4349       // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast
4350       // so eliminate it as well.
4351       if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1))
4352         Op1 = CI2->getOperand(0);
4353
4354       // If Op1 is a constant, we can fold the cast into the constant.
4355       if (Op0->getType() != Op1->getType()) {
4356         if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
4357           Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType());
4358         } else {
4359           // Otherwise, cast the RHS right before the icmp
4360           Op1 = Builder->CreateBitCast(Op1, Op0->getType());
4361         }
4362       }
4363       return new ICmpInst(I.getPredicate(), Op0, Op1);
4364     }
4365   }
4366
4367   if (isa<CastInst>(Op0)) {
4368     // Handle the special case of: icmp (cast bool to X), <cst>
4369     // This comes up when you have code like
4370     //   int X = A < B;
4371     //   if (X) ...
4372     // For generality, we handle any zero-extension of any operand comparison
4373     // with a constant or another cast from the same type.
4374     if (isa<Constant>(Op1) || isa<CastInst>(Op1))
4375       if (Instruction *R = foldICmpWithCastAndCast(I))
4376         return R;
4377   }
4378
4379   if (Instruction *Res = foldICmpBinOp(I))
4380     return Res;
4381
4382   if (Instruction *Res = foldICmpWithMinMax(I))
4383     return Res;
4384
4385   {
4386     Value *A, *B;
4387     // Transform (A & ~B) == 0 --> (A & B) != 0
4388     // and       (A & ~B) != 0 --> (A & B) == 0
4389     // if A is a power of 2.
4390     if (match(Op0, m_And(m_Value(A), m_Not(m_Value(B)))) &&
4391         match(Op1, m_Zero()) &&
4392         isKnownToBeAPowerOfTwo(A, DL, false, 0, &AC, &I, &DT) && I.isEquality())
4393       return new ICmpInst(I.getInversePredicate(),
4394                           Builder->CreateAnd(A, B),
4395                           Op1);
4396
4397     // ~x < ~y --> y < x
4398     // ~x < cst --> ~cst < x
4399     if (match(Op0, m_Not(m_Value(A)))) {
4400       if (match(Op1, m_Not(m_Value(B))))
4401         return new ICmpInst(I.getPredicate(), B, A);
4402       if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1))
4403         return new ICmpInst(I.getPredicate(), ConstantExpr::getNot(RHSC), A);
4404     }
4405
4406     Instruction *AddI = nullptr;
4407     if (match(&I, m_UAddWithOverflow(m_Value(A), m_Value(B),
4408                                      m_Instruction(AddI))) &&
4409         isa<IntegerType>(A->getType())) {
4410       Value *Result;
4411       Constant *Overflow;
4412       if (OptimizeOverflowCheck(OCF_UNSIGNED_ADD, A, B, *AddI, Result,
4413                                 Overflow)) {
4414         replaceInstUsesWith(*AddI, Result);
4415         return replaceInstUsesWith(I, Overflow);
4416       }
4417     }
4418
4419     // (zext a) * (zext b)  --> llvm.umul.with.overflow.
4420     if (match(Op0, m_Mul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) {
4421       if (Instruction *R = processUMulZExtIdiom(I, Op0, Op1, *this))
4422         return R;
4423     }
4424     if (match(Op1, m_Mul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) {
4425       if (Instruction *R = processUMulZExtIdiom(I, Op1, Op0, *this))
4426         return R;
4427     }
4428   }
4429
4430   if (Instruction *Res = foldICmpEquality(I))
4431     return Res;
4432
4433   // The 'cmpxchg' instruction returns an aggregate containing the old value and
4434   // an i1 which indicates whether or not we successfully did the swap.
4435   //
4436   // Replace comparisons between the old value and the expected value with the
4437   // indicator that 'cmpxchg' returns.
4438   //
4439   // N.B.  This transform is only valid when the 'cmpxchg' is not permitted to
4440   // spuriously fail.  In those cases, the old value may equal the expected
4441   // value but it is possible for the swap to not occur.
4442   if (I.getPredicate() == ICmpInst::ICMP_EQ)
4443     if (auto *EVI = dyn_cast<ExtractValueInst>(Op0))
4444       if (auto *ACXI = dyn_cast<AtomicCmpXchgInst>(EVI->getAggregateOperand()))
4445         if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 &&
4446             !ACXI->isWeak())
4447           return ExtractValueInst::Create(ACXI, 1);
4448
4449   {
4450     Value *X; ConstantInt *Cst;
4451     // icmp X+Cst, X
4452     if (match(Op0, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op1 == X)
4453       return foldICmpAddOpConst(I, X, Cst, I.getPredicate());
4454
4455     // icmp X, X+Cst
4456     if (match(Op1, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op0 == X)
4457       return foldICmpAddOpConst(I, X, Cst, I.getSwappedPredicate());
4458   }
4459   return Changed ? &I : nullptr;
4460 }
4461
4462 /// Fold fcmp ([us]itofp x, cst) if possible.
4463 Instruction *InstCombiner::foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI,
4464                                                 Constant *RHSC) {
4465   if (!isa<ConstantFP>(RHSC)) return nullptr;
4466   const APFloat &RHS = cast<ConstantFP>(RHSC)->getValueAPF();
4467
4468   // Get the width of the mantissa.  We don't want to hack on conversions that
4469   // might lose information from the integer, e.g. "i64 -> float"
4470   int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
4471   if (MantissaWidth == -1) return nullptr;  // Unknown.
4472
4473   IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType());
4474
4475   bool LHSUnsigned = isa<UIToFPInst>(LHSI);
4476
4477   if (I.isEquality()) {
4478     FCmpInst::Predicate P = I.getPredicate();
4479     bool IsExact = false;
4480     APSInt RHSCvt(IntTy->getBitWidth(), LHSUnsigned);
4481     RHS.convertToInteger(RHSCvt, APFloat::rmNearestTiesToEven, &IsExact);
4482
4483     // If the floating point constant isn't an integer value, we know if we will
4484     // ever compare equal / not equal to it.
4485     if (!IsExact) {
4486       // TODO: Can never be -0.0 and other non-representable values
4487       APFloat RHSRoundInt(RHS);
4488       RHSRoundInt.roundToIntegral(APFloat::rmNearestTiesToEven);
4489       if (RHS.compare(RHSRoundInt) != APFloat::cmpEqual) {
4490         if (P == FCmpInst::FCMP_OEQ || P == FCmpInst::FCMP_UEQ)
4491           return replaceInstUsesWith(I, Builder->getFalse());
4492
4493         assert(P == FCmpInst::FCMP_ONE || P == FCmpInst::FCMP_UNE);
4494         return replaceInstUsesWith(I, Builder->getTrue());
4495       }
4496     }
4497
4498     // TODO: If the constant is exactly representable, is it always OK to do
4499     // equality compares as integer?
4500   }
4501
4502   // Check to see that the input is converted from an integer type that is small
4503   // enough that preserves all bits.  TODO: check here for "known" sign bits.
4504   // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
4505   unsigned InputSize = IntTy->getScalarSizeInBits();
4506
4507   // Following test does NOT adjust InputSize downwards for signed inputs,
4508   // because the most negative value still requires all the mantissa bits
4509   // to distinguish it from one less than that value.
4510   if ((int)InputSize > MantissaWidth) {
4511     // Conversion would lose accuracy. Check if loss can impact comparison.
4512     int Exp = ilogb(RHS);
4513     if (Exp == APFloat::IEK_Inf) {
4514       int MaxExponent = ilogb(APFloat::getLargest(RHS.getSemantics()));
4515       if (MaxExponent < (int)InputSize - !LHSUnsigned)
4516         // Conversion could create infinity.
4517         return nullptr;
4518     } else {
4519       // Note that if RHS is zero or NaN, then Exp is negative
4520       // and first condition is trivially false.
4521       if (MantissaWidth <= Exp && Exp <= (int)InputSize - !LHSUnsigned)
4522         // Conversion could affect comparison.
4523         return nullptr;
4524     }
4525   }
4526
4527   // Otherwise, we can potentially simplify the comparison.  We know that it
4528   // will always come through as an integer value and we know the constant is
4529   // not a NAN (it would have been previously simplified).
4530   assert(!RHS.isNaN() && "NaN comparison not already folded!");
4531
4532   ICmpInst::Predicate Pred;
4533   switch (I.getPredicate()) {
4534   default: llvm_unreachable("Unexpected predicate!");
4535   case FCmpInst::FCMP_UEQ:
4536   case FCmpInst::FCMP_OEQ:
4537     Pred = ICmpInst::ICMP_EQ;
4538     break;
4539   case FCmpInst::FCMP_UGT:
4540   case FCmpInst::FCMP_OGT:
4541     Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
4542     break;
4543   case FCmpInst::FCMP_UGE:
4544   case FCmpInst::FCMP_OGE:
4545     Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
4546     break;
4547   case FCmpInst::FCMP_ULT:
4548   case FCmpInst::FCMP_OLT:
4549     Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
4550     break;
4551   case FCmpInst::FCMP_ULE:
4552   case FCmpInst::FCMP_OLE:
4553     Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
4554     break;
4555   case FCmpInst::FCMP_UNE:
4556   case FCmpInst::FCMP_ONE:
4557     Pred = ICmpInst::ICMP_NE;
4558     break;
4559   case FCmpInst::FCMP_ORD:
4560     return replaceInstUsesWith(I, Builder->getTrue());
4561   case FCmpInst::FCMP_UNO:
4562     return replaceInstUsesWith(I, Builder->getFalse());
4563   }
4564
4565   // Now we know that the APFloat is a normal number, zero or inf.
4566
4567   // See if the FP constant is too large for the integer.  For example,
4568   // comparing an i8 to 300.0.
4569   unsigned IntWidth = IntTy->getScalarSizeInBits();
4570
4571   if (!LHSUnsigned) {
4572     // If the RHS value is > SignedMax, fold the comparison.  This handles +INF
4573     // and large values.
4574     APFloat SMax(RHS.getSemantics());
4575     SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true,
4576                           APFloat::rmNearestTiesToEven);
4577     if (SMax.compare(RHS) == APFloat::cmpLessThan) {  // smax < 13123.0
4578       if (Pred == ICmpInst::ICMP_NE  || Pred == ICmpInst::ICMP_SLT ||
4579           Pred == ICmpInst::ICMP_SLE)
4580         return replaceInstUsesWith(I, Builder->getTrue());
4581       return replaceInstUsesWith(I, Builder->getFalse());
4582     }
4583   } else {
4584     // If the RHS value is > UnsignedMax, fold the comparison. This handles
4585     // +INF and large values.
4586     APFloat UMax(RHS.getSemantics());
4587     UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false,
4588                           APFloat::rmNearestTiesToEven);
4589     if (UMax.compare(RHS) == APFloat::cmpLessThan) {  // umax < 13123.0
4590       if (Pred == ICmpInst::ICMP_NE  || Pred == ICmpInst::ICMP_ULT ||
4591           Pred == ICmpInst::ICMP_ULE)
4592         return replaceInstUsesWith(I, Builder->getTrue());
4593       return replaceInstUsesWith(I, Builder->getFalse());
4594     }
4595   }
4596
4597   if (!LHSUnsigned) {
4598     // See if the RHS value is < SignedMin.
4599     APFloat SMin(RHS.getSemantics());
4600     SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true,
4601                           APFloat::rmNearestTiesToEven);
4602     if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0
4603       if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
4604           Pred == ICmpInst::ICMP_SGE)
4605         return replaceInstUsesWith(I, Builder->getTrue());
4606       return replaceInstUsesWith(I, Builder->getFalse());
4607     }
4608   } else {
4609     // See if the RHS value is < UnsignedMin.
4610     APFloat SMin(RHS.getSemantics());
4611     SMin.convertFromAPInt(APInt::getMinValue(IntWidth), true,
4612                           APFloat::rmNearestTiesToEven);
4613     if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // umin > 12312.0
4614       if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||
4615           Pred == ICmpInst::ICMP_UGE)
4616         return replaceInstUsesWith(I, Builder->getTrue());
4617       return replaceInstUsesWith(I, Builder->getFalse());
4618     }
4619   }
4620
4621   // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
4622   // [0, UMAX], but it may still be fractional.  See if it is fractional by
4623   // casting the FP value to the integer value and back, checking for equality.
4624   // Don't do this for zero, because -0.0 is not fractional.
4625   Constant *RHSInt = LHSUnsigned
4626     ? ConstantExpr::getFPToUI(RHSC, IntTy)
4627     : ConstantExpr::getFPToSI(RHSC, IntTy);
4628   if (!RHS.isZero()) {
4629     bool Equal = LHSUnsigned
4630       ? ConstantExpr::getUIToFP(RHSInt, RHSC->getType()) == RHSC
4631       : ConstantExpr::getSIToFP(RHSInt, RHSC->getType()) == RHSC;
4632     if (!Equal) {
4633       // If we had a comparison against a fractional value, we have to adjust
4634       // the compare predicate and sometimes the value.  RHSC is rounded towards
4635       // zero at this point.
4636       switch (Pred) {
4637       default: llvm_unreachable("Unexpected integer comparison!");
4638       case ICmpInst::ICMP_NE:  // (float)int != 4.4   --> true
4639         return replaceInstUsesWith(I, Builder->getTrue());
4640       case ICmpInst::ICMP_EQ:  // (float)int == 4.4   --> false
4641         return replaceInstUsesWith(I, Builder->getFalse());
4642       case ICmpInst::ICMP_ULE:
4643         // (float)int <= 4.4   --> int <= 4
4644         // (float)int <= -4.4  --> false
4645         if (RHS.isNegative())
4646           return replaceInstUsesWith(I, Builder->getFalse());
4647         break;
4648       case ICmpInst::ICMP_SLE:
4649         // (float)int <= 4.4   --> int <= 4
4650         // (float)int <= -4.4  --> int < -4
4651         if (RHS.isNegative())
4652           Pred = ICmpInst::ICMP_SLT;
4653         break;
4654       case ICmpInst::ICMP_ULT:
4655         // (float)int < -4.4   --> false
4656         // (float)int < 4.4    --> int <= 4
4657         if (RHS.isNegative())
4658           return replaceInstUsesWith(I, Builder->getFalse());
4659         Pred = ICmpInst::ICMP_ULE;
4660         break;
4661       case ICmpInst::ICMP_SLT:
4662         // (float)int < -4.4   --> int < -4
4663         // (float)int < 4.4    --> int <= 4
4664         if (!RHS.isNegative())
4665           Pred = ICmpInst::ICMP_SLE;
4666         break;
4667       case ICmpInst::ICMP_UGT:
4668         // (float)int > 4.4    --> int > 4
4669         // (float)int > -4.4   --> true
4670         if (RHS.isNegative())
4671           return replaceInstUsesWith(I, Builder->getTrue());
4672         break;
4673       case ICmpInst::ICMP_SGT:
4674         // (float)int > 4.4    --> int > 4
4675         // (float)int > -4.4   --> int >= -4
4676         if (RHS.isNegative())
4677           Pred = ICmpInst::ICMP_SGE;
4678         break;
4679       case ICmpInst::ICMP_UGE:
4680         // (float)int >= -4.4   --> true
4681         // (float)int >= 4.4    --> int > 4
4682         if (RHS.isNegative())
4683           return replaceInstUsesWith(I, Builder->getTrue());
4684         Pred = ICmpInst::ICMP_UGT;
4685         break;
4686       case ICmpInst::ICMP_SGE:
4687         // (float)int >= -4.4   --> int >= -4
4688         // (float)int >= 4.4    --> int > 4
4689         if (!RHS.isNegative())
4690           Pred = ICmpInst::ICMP_SGT;
4691         break;
4692       }
4693     }
4694   }
4695
4696   // Lower this FP comparison into an appropriate integer version of the
4697   // comparison.
4698   return new ICmpInst(Pred, LHSI->getOperand(0), RHSInt);
4699 }
4700
4701 Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) {
4702   bool Changed = false;
4703
4704   /// Orders the operands of the compare so that they are listed from most
4705   /// complex to least complex.  This puts constants before unary operators,
4706   /// before binary operators.
4707   if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) {
4708     I.swapOperands();
4709     Changed = true;
4710   }
4711
4712   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
4713
4714   if (Value *V = SimplifyFCmpInst(I.getPredicate(), Op0, Op1,
4715                                   I.getFastMathFlags(), DL, &TLI, &DT, &AC, &I))
4716     return replaceInstUsesWith(I, V);
4717
4718   // Simplify 'fcmp pred X, X'
4719   if (Op0 == Op1) {
4720     switch (I.getPredicate()) {
4721     default: llvm_unreachable("Unknown predicate!");
4722     case FCmpInst::FCMP_UNO:    // True if unordered: isnan(X) | isnan(Y)
4723     case FCmpInst::FCMP_ULT:    // True if unordered or less than
4724     case FCmpInst::FCMP_UGT:    // True if unordered or greater than
4725     case FCmpInst::FCMP_UNE:    // True if unordered or not equal
4726       // Canonicalize these to be 'fcmp uno %X, 0.0'.
4727       I.setPredicate(FCmpInst::FCMP_UNO);
4728       I.setOperand(1, Constant::getNullValue(Op0->getType()));
4729       return &I;
4730
4731     case FCmpInst::FCMP_ORD:    // True if ordered (no nans)
4732     case FCmpInst::FCMP_OEQ:    // True if ordered and equal
4733     case FCmpInst::FCMP_OGE:    // True if ordered and greater than or equal
4734     case FCmpInst::FCMP_OLE:    // True if ordered and less than or equal
4735       // Canonicalize these to be 'fcmp ord %X, 0.0'.
4736       I.setPredicate(FCmpInst::FCMP_ORD);
4737       I.setOperand(1, Constant::getNullValue(Op0->getType()));
4738       return &I;
4739     }
4740   }
4741
4742   // Test if the FCmpInst instruction is used exclusively by a select as
4743   // part of a minimum or maximum operation. If so, refrain from doing
4744   // any other folding. This helps out other analyses which understand
4745   // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
4746   // and CodeGen. And in this case, at least one of the comparison
4747   // operands has at least one user besides the compare (the select),
4748   // which would often largely negate the benefit of folding anyway.
4749   if (I.hasOneUse())
4750     if (SelectInst *SI = dyn_cast<SelectInst>(*I.user_begin()))
4751       if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) ||
4752           (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1))
4753         return nullptr;
4754
4755   // Handle fcmp with constant RHS
4756   if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
4757     if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
4758       switch (LHSI->getOpcode()) {
4759       case Instruction::FPExt: {
4760         // fcmp (fpext x), C -> fcmp x, (fptrunc C) if fptrunc is lossless
4761         FPExtInst *LHSExt = cast<FPExtInst>(LHSI);
4762         ConstantFP *RHSF = dyn_cast<ConstantFP>(RHSC);
4763         if (!RHSF)
4764           break;
4765
4766         const fltSemantics *Sem;
4767         // FIXME: This shouldn't be here.
4768         if (LHSExt->getSrcTy()->isHalfTy())
4769           Sem = &APFloat::IEEEhalf();
4770         else if (LHSExt->getSrcTy()->isFloatTy())
4771           Sem = &APFloat::IEEEsingle();
4772         else if (LHSExt->getSrcTy()->isDoubleTy())
4773           Sem = &APFloat::IEEEdouble();
4774         else if (LHSExt->getSrcTy()->isFP128Ty())
4775           Sem = &APFloat::IEEEquad();
4776         else if (LHSExt->getSrcTy()->isX86_FP80Ty())
4777           Sem = &APFloat::x87DoubleExtended();
4778         else if (LHSExt->getSrcTy()->isPPC_FP128Ty())
4779           Sem = &APFloat::PPCDoubleDouble();
4780         else
4781           break;
4782
4783         bool Lossy;
4784         APFloat F = RHSF->getValueAPF();
4785         F.convert(*Sem, APFloat::rmNearestTiesToEven, &Lossy);
4786
4787         // Avoid lossy conversions and denormals. Zero is a special case
4788         // that's OK to convert.
4789         APFloat Fabs = F;
4790         Fabs.clearSign();
4791         if (!Lossy &&
4792             ((Fabs.compare(APFloat::getSmallestNormalized(*Sem)) !=
4793                  APFloat::cmpLessThan) || Fabs.isZero()))
4794
4795           return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0),
4796                               ConstantFP::get(RHSC->getContext(), F));
4797         break;
4798       }
4799       case Instruction::PHI:
4800         // Only fold fcmp into the PHI if the phi and fcmp are in the same
4801         // block.  If in the same block, we're encouraging jump threading.  If
4802         // not, we are just pessimizing the code by making an i1 phi.
4803         if (LHSI->getParent() == I.getParent())
4804           if (Instruction *NV = FoldOpIntoPhi(I))
4805             return NV;
4806         break;
4807       case Instruction::SIToFP:
4808       case Instruction::UIToFP:
4809         if (Instruction *NV = foldFCmpIntToFPConst(I, LHSI, RHSC))
4810           return NV;
4811         break;
4812       case Instruction::FSub: {
4813         // fcmp pred (fneg x), C -> fcmp swap(pred) x, -C
4814         Value *Op;
4815         if (match(LHSI, m_FNeg(m_Value(Op))))
4816           return new FCmpInst(I.getSwappedPredicate(), Op,
4817                               ConstantExpr::getFNeg(RHSC));
4818         break;
4819       }
4820       case Instruction::Load:
4821         if (GetElementPtrInst *GEP =
4822             dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) {
4823           if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
4824             if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
4825                 !cast<LoadInst>(LHSI)->isVolatile())
4826               if (Instruction *Res = foldCmpLoadFromIndexedGlobal(GEP, GV, I))
4827                 return Res;
4828         }
4829         break;
4830       case Instruction::Call: {
4831         if (!RHSC->isNullValue())
4832           break;
4833
4834         CallInst *CI = cast<CallInst>(LHSI);
4835         Intrinsic::ID IID = getIntrinsicForCallSite(CI, &TLI);
4836         if (IID != Intrinsic::fabs)
4837           break;
4838
4839         // Various optimization for fabs compared with zero.
4840         switch (I.getPredicate()) {
4841         default:
4842           break;
4843         // fabs(x) < 0 --> false
4844         case FCmpInst::FCMP_OLT:
4845           llvm_unreachable("handled by SimplifyFCmpInst");
4846         // fabs(x) > 0 --> x != 0
4847         case FCmpInst::FCMP_OGT:
4848           return new FCmpInst(FCmpInst::FCMP_ONE, CI->getArgOperand(0), RHSC);
4849         // fabs(x) <= 0 --> x == 0
4850         case FCmpInst::FCMP_OLE:
4851           return new FCmpInst(FCmpInst::FCMP_OEQ, CI->getArgOperand(0), RHSC);
4852         // fabs(x) >= 0 --> !isnan(x)
4853         case FCmpInst::FCMP_OGE:
4854           return new FCmpInst(FCmpInst::FCMP_ORD, CI->getArgOperand(0), RHSC);
4855         // fabs(x) == 0 --> x == 0
4856         // fabs(x) != 0 --> x != 0
4857         case FCmpInst::FCMP_OEQ:
4858         case FCmpInst::FCMP_UEQ:
4859         case FCmpInst::FCMP_ONE:
4860         case FCmpInst::FCMP_UNE:
4861           return new FCmpInst(I.getPredicate(), CI->getArgOperand(0), RHSC);
4862         }
4863       }
4864       }
4865   }
4866
4867   // fcmp pred (fneg x), (fneg y) -> fcmp swap(pred) x, y
4868   Value *X, *Y;
4869   if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))
4870     return new FCmpInst(I.getSwappedPredicate(), X, Y);
4871
4872   // fcmp (fpext x), (fpext y) -> fcmp x, y
4873   if (FPExtInst *LHSExt = dyn_cast<FPExtInst>(Op0))
4874     if (FPExtInst *RHSExt = dyn_cast<FPExtInst>(Op1))
4875       if (LHSExt->getSrcTy() == RHSExt->getSrcTy())
4876         return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0),
4877                             RHSExt->getOperand(0));
4878
4879   return Changed ? &I : nullptr;
4880 }