87ab3bb7fda6b08c29d9e7683f3d86b77a8197f2
[oota-llvm.git] / lib / Analysis / ValueTracking.cpp
1 //===- ValueTracking.cpp - Walk computations to compute properties --------===//
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 contains routines that help analyze properties that chains of
11 // computations have.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Analysis/ValueTracking.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/Analysis/AssumptionCache.h"
18 #include "llvm/Analysis/InstructionSimplify.h"
19 #include "llvm/Analysis/MemoryBuiltins.h"
20 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/IR/CallSite.h"
22 #include "llvm/IR/ConstantRange.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/Dominators.h"
26 #include "llvm/IR/GetElementPtrTypeIterator.h"
27 #include "llvm/IR/GlobalAlias.h"
28 #include "llvm/IR/GlobalVariable.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/IntrinsicInst.h"
31 #include "llvm/IR/LLVMContext.h"
32 #include "llvm/IR/Metadata.h"
33 #include "llvm/IR/Operator.h"
34 #include "llvm/IR/PatternMatch.h"
35 #include "llvm/IR/Statepoint.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/MathExtras.h"
38 #include <cstring>
39 using namespace llvm;
40 using namespace llvm::PatternMatch;
41
42 const unsigned MaxDepth = 6;
43
44 /// Enable an experimental feature to leverage information about dominating
45 /// conditions to compute known bits.  The individual options below control how
46 /// hard we search.  The defaults are chosen to be fairly aggressive.  If you
47 /// run into compile time problems when testing, scale them back and report
48 /// your findings.
49 static cl::opt<bool> EnableDomConditions("value-tracking-dom-conditions",
50                                          cl::Hidden, cl::init(false));
51
52 // This is expensive, so we only do it for the top level query value.
53 // (TODO: evaluate cost vs profit, consider higher thresholds)
54 static cl::opt<unsigned> DomConditionsMaxDepth("dom-conditions-max-depth",
55                                                cl::Hidden, cl::init(1));
56
57 /// How many dominating blocks should be scanned looking for dominating
58 /// conditions?
59 static cl::opt<unsigned> DomConditionsMaxDomBlocks("dom-conditions-dom-blocks",
60                                                    cl::Hidden,
61                                                    cl::init(20000));
62
63 // Controls the number of uses of the value searched for possible
64 // dominating comparisons.
65 static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
66                                               cl::Hidden, cl::init(2000));
67
68 // If true, don't consider only compares whose only use is a branch.
69 static cl::opt<bool> DomConditionsSingleCmpUse("dom-conditions-single-cmp-use",
70                                                cl::Hidden, cl::init(false));
71
72 /// Returns the bitwidth of the given scalar or pointer type (if unknown returns
73 /// 0). For vector types, returns the element type's bitwidth.
74 static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
75   if (unsigned BitWidth = Ty->getScalarSizeInBits())
76     return BitWidth;
77
78   return DL.getPointerTypeSizeInBits(Ty);
79 }
80
81 // Many of these functions have internal versions that take an assumption
82 // exclusion set. This is because of the potential for mutual recursion to
83 // cause computeKnownBits to repeatedly visit the same assume intrinsic. The
84 // classic case of this is assume(x = y), which will attempt to determine
85 // bits in x from bits in y, which will attempt to determine bits in y from
86 // bits in x, etc. Regarding the mutual recursion, computeKnownBits can call
87 // isKnownNonZero, which calls computeKnownBits and ComputeSignBit and
88 // isKnownToBeAPowerOfTwo (all of which can call computeKnownBits), and so on.
89 typedef SmallPtrSet<const Value *, 8> ExclInvsSet;
90
91 namespace {
92 // Simplifying using an assume can only be done in a particular control-flow
93 // context (the context instruction provides that context). If an assume and
94 // the context instruction are not in the same block then the DT helps in
95 // figuring out if we can use it.
96 struct Query {
97   ExclInvsSet ExclInvs;
98   AssumptionCache *AC;
99   const Instruction *CxtI;
100   const DominatorTree *DT;
101
102   Query(AssumptionCache *AC = nullptr, const Instruction *CxtI = nullptr,
103         const DominatorTree *DT = nullptr)
104       : AC(AC), CxtI(CxtI), DT(DT) {}
105
106   Query(const Query &Q, const Value *NewExcl)
107       : ExclInvs(Q.ExclInvs), AC(Q.AC), CxtI(Q.CxtI), DT(Q.DT) {
108     ExclInvs.insert(NewExcl);
109   }
110 };
111 } // end anonymous namespace
112
113 // Given the provided Value and, potentially, a context instruction, return
114 // the preferred context instruction (if any).
115 static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
116   // If we've been provided with a context instruction, then use that (provided
117   // it has been inserted).
118   if (CxtI && CxtI->getParent())
119     return CxtI;
120
121   // If the value is really an already-inserted instruction, then use that.
122   CxtI = dyn_cast<Instruction>(V);
123   if (CxtI && CxtI->getParent())
124     return CxtI;
125
126   return nullptr;
127 }
128
129 static void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
130                              const DataLayout &DL, unsigned Depth,
131                              const Query &Q);
132
133 void llvm::computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
134                             const DataLayout &DL, unsigned Depth,
135                             AssumptionCache *AC, const Instruction *CxtI,
136                             const DominatorTree *DT) {
137   ::computeKnownBits(V, KnownZero, KnownOne, DL, Depth,
138                      Query(AC, safeCxtI(V, CxtI), DT));
139 }
140
141 bool llvm::haveNoCommonBitsSet(Value *LHS, Value *RHS, const DataLayout &DL,
142                                AssumptionCache *AC, const Instruction *CxtI,
143                                const DominatorTree *DT) {
144   assert(LHS->getType() == RHS->getType() &&
145          "LHS and RHS should have the same type");
146   assert(LHS->getType()->isIntOrIntVectorTy() &&
147          "LHS and RHS should be integers");
148   IntegerType *IT = cast<IntegerType>(LHS->getType()->getScalarType());
149   APInt LHSKnownZero(IT->getBitWidth(), 0), LHSKnownOne(IT->getBitWidth(), 0);
150   APInt RHSKnownZero(IT->getBitWidth(), 0), RHSKnownOne(IT->getBitWidth(), 0);
151   computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, DL, 0, AC, CxtI, DT);
152   computeKnownBits(RHS, RHSKnownZero, RHSKnownOne, DL, 0, AC, CxtI, DT);
153   return (LHSKnownZero | RHSKnownZero).isAllOnesValue();
154 }
155
156 static void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
157                            const DataLayout &DL, unsigned Depth,
158                            const Query &Q);
159
160 void llvm::ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
161                           const DataLayout &DL, unsigned Depth,
162                           AssumptionCache *AC, const Instruction *CxtI,
163                           const DominatorTree *DT) {
164   ::ComputeSignBit(V, KnownZero, KnownOne, DL, Depth,
165                    Query(AC, safeCxtI(V, CxtI), DT));
166 }
167
168 static bool isKnownToBeAPowerOfTwo(Value *V, bool OrZero, unsigned Depth,
169                                    const Query &Q, const DataLayout &DL);
170
171 bool llvm::isKnownToBeAPowerOfTwo(Value *V, const DataLayout &DL, bool OrZero,
172                                   unsigned Depth, AssumptionCache *AC,
173                                   const Instruction *CxtI,
174                                   const DominatorTree *DT) {
175   return ::isKnownToBeAPowerOfTwo(V, OrZero, Depth,
176                                   Query(AC, safeCxtI(V, CxtI), DT), DL);
177 }
178
179 static bool isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth,
180                            const Query &Q);
181
182 bool llvm::isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth,
183                           AssumptionCache *AC, const Instruction *CxtI,
184                           const DominatorTree *DT) {
185   return ::isKnownNonZero(V, DL, Depth, Query(AC, safeCxtI(V, CxtI), DT));
186 }
187
188 static bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL,
189                               unsigned Depth, const Query &Q);
190
191 bool llvm::MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL,
192                              unsigned Depth, AssumptionCache *AC,
193                              const Instruction *CxtI, const DominatorTree *DT) {
194   return ::MaskedValueIsZero(V, Mask, DL, Depth,
195                              Query(AC, safeCxtI(V, CxtI), DT));
196 }
197
198 static unsigned ComputeNumSignBits(Value *V, const DataLayout &DL,
199                                    unsigned Depth, const Query &Q);
200
201 unsigned llvm::ComputeNumSignBits(Value *V, const DataLayout &DL,
202                                   unsigned Depth, AssumptionCache *AC,
203                                   const Instruction *CxtI,
204                                   const DominatorTree *DT) {
205   return ::ComputeNumSignBits(V, DL, Depth, Query(AC, safeCxtI(V, CxtI), DT));
206 }
207
208 static void computeKnownBitsAddSub(bool Add, Value *Op0, Value *Op1, bool NSW,
209                                    APInt &KnownZero, APInt &KnownOne,
210                                    APInt &KnownZero2, APInt &KnownOne2,
211                                    const DataLayout &DL, unsigned Depth,
212                                    const Query &Q) {
213   if (!Add) {
214     if (ConstantInt *CLHS = dyn_cast<ConstantInt>(Op0)) {
215       // We know that the top bits of C-X are clear if X contains less bits
216       // than C (i.e. no wrap-around can happen).  For example, 20-X is
217       // positive if we can prove that X is >= 0 and < 16.
218       if (!CLHS->getValue().isNegative()) {
219         unsigned BitWidth = KnownZero.getBitWidth();
220         unsigned NLZ = (CLHS->getValue()+1).countLeadingZeros();
221         // NLZ can't be BitWidth with no sign bit
222         APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
223         computeKnownBits(Op1, KnownZero2, KnownOne2, DL, Depth + 1, Q);
224
225         // If all of the MaskV bits are known to be zero, then we know the
226         // output top bits are zero, because we now know that the output is
227         // from [0-C].
228         if ((KnownZero2 & MaskV) == MaskV) {
229           unsigned NLZ2 = CLHS->getValue().countLeadingZeros();
230           // Top bits known zero.
231           KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
232         }
233       }
234     }
235   }
236
237   unsigned BitWidth = KnownZero.getBitWidth();
238
239   // If an initial sequence of bits in the result is not needed, the
240   // corresponding bits in the operands are not needed.
241   APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
242   computeKnownBits(Op0, LHSKnownZero, LHSKnownOne, DL, Depth + 1, Q);
243   computeKnownBits(Op1, KnownZero2, KnownOne2, DL, Depth + 1, Q);
244
245   // Carry in a 1 for a subtract, rather than a 0.
246   APInt CarryIn(BitWidth, 0);
247   if (!Add) {
248     // Sum = LHS + ~RHS + 1
249     std::swap(KnownZero2, KnownOne2);
250     CarryIn.setBit(0);
251   }
252
253   APInt PossibleSumZero = ~LHSKnownZero + ~KnownZero2 + CarryIn;
254   APInt PossibleSumOne = LHSKnownOne + KnownOne2 + CarryIn;
255
256   // Compute known bits of the carry.
257   APInt CarryKnownZero = ~(PossibleSumZero ^ LHSKnownZero ^ KnownZero2);
258   APInt CarryKnownOne = PossibleSumOne ^ LHSKnownOne ^ KnownOne2;
259
260   // Compute set of known bits (where all three relevant bits are known).
261   APInt LHSKnown = LHSKnownZero | LHSKnownOne;
262   APInt RHSKnown = KnownZero2 | KnownOne2;
263   APInt CarryKnown = CarryKnownZero | CarryKnownOne;
264   APInt Known = LHSKnown & RHSKnown & CarryKnown;
265
266   assert((PossibleSumZero & Known) == (PossibleSumOne & Known) &&
267          "known bits of sum differ");
268
269   // Compute known bits of the result.
270   KnownZero = ~PossibleSumOne & Known;
271   KnownOne = PossibleSumOne & Known;
272
273   // Are we still trying to solve for the sign bit?
274   if (!Known.isNegative()) {
275     if (NSW) {
276       // Adding two non-negative numbers, or subtracting a negative number from
277       // a non-negative one, can't wrap into negative.
278       if (LHSKnownZero.isNegative() && KnownZero2.isNegative())
279         KnownZero |= APInt::getSignBit(BitWidth);
280       // Adding two negative numbers, or subtracting a non-negative number from
281       // a negative one, can't wrap into non-negative.
282       else if (LHSKnownOne.isNegative() && KnownOne2.isNegative())
283         KnownOne |= APInt::getSignBit(BitWidth);
284     }
285   }
286 }
287
288 static void computeKnownBitsMul(Value *Op0, Value *Op1, bool NSW,
289                                 APInt &KnownZero, APInt &KnownOne,
290                                 APInt &KnownZero2, APInt &KnownOne2,
291                                 const DataLayout &DL, unsigned Depth,
292                                 const Query &Q) {
293   unsigned BitWidth = KnownZero.getBitWidth();
294   computeKnownBits(Op1, KnownZero, KnownOne, DL, Depth + 1, Q);
295   computeKnownBits(Op0, KnownZero2, KnownOne2, DL, Depth + 1, Q);
296
297   bool isKnownNegative = false;
298   bool isKnownNonNegative = false;
299   // If the multiplication is known not to overflow, compute the sign bit.
300   if (NSW) {
301     if (Op0 == Op1) {
302       // The product of a number with itself is non-negative.
303       isKnownNonNegative = true;
304     } else {
305       bool isKnownNonNegativeOp1 = KnownZero.isNegative();
306       bool isKnownNonNegativeOp0 = KnownZero2.isNegative();
307       bool isKnownNegativeOp1 = KnownOne.isNegative();
308       bool isKnownNegativeOp0 = KnownOne2.isNegative();
309       // The product of two numbers with the same sign is non-negative.
310       isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
311         (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
312       // The product of a negative number and a non-negative number is either
313       // negative or zero.
314       if (!isKnownNonNegative)
315         isKnownNegative = (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
316                            isKnownNonZero(Op0, DL, Depth, Q)) ||
317                           (isKnownNegativeOp0 && isKnownNonNegativeOp1 &&
318                            isKnownNonZero(Op1, DL, Depth, Q));
319     }
320   }
321
322   // If low bits are zero in either operand, output low known-0 bits.
323   // Also compute a conserative estimate for high known-0 bits.
324   // More trickiness is possible, but this is sufficient for the
325   // interesting case of alignment computation.
326   KnownOne.clearAllBits();
327   unsigned TrailZ = KnownZero.countTrailingOnes() +
328                     KnownZero2.countTrailingOnes();
329   unsigned LeadZ =  std::max(KnownZero.countLeadingOnes() +
330                              KnownZero2.countLeadingOnes(),
331                              BitWidth) - BitWidth;
332
333   TrailZ = std::min(TrailZ, BitWidth);
334   LeadZ = std::min(LeadZ, BitWidth);
335   KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
336               APInt::getHighBitsSet(BitWidth, LeadZ);
337
338   // Only make use of no-wrap flags if we failed to compute the sign bit
339   // directly.  This matters if the multiplication always overflows, in
340   // which case we prefer to follow the result of the direct computation,
341   // though as the program is invoking undefined behaviour we can choose
342   // whatever we like here.
343   if (isKnownNonNegative && !KnownOne.isNegative())
344     KnownZero.setBit(BitWidth - 1);
345   else if (isKnownNegative && !KnownZero.isNegative())
346     KnownOne.setBit(BitWidth - 1);
347 }
348
349 void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
350                                              APInt &KnownZero) {
351   unsigned BitWidth = KnownZero.getBitWidth();
352   unsigned NumRanges = Ranges.getNumOperands() / 2;
353   assert(NumRanges >= 1);
354
355   // Use the high end of the ranges to find leading zeros.
356   unsigned MinLeadingZeros = BitWidth;
357   for (unsigned i = 0; i < NumRanges; ++i) {
358     ConstantInt *Lower =
359         mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
360     ConstantInt *Upper =
361         mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
362     ConstantRange Range(Lower->getValue(), Upper->getValue());
363     if (Range.isWrappedSet())
364       MinLeadingZeros = 0; // -1 has no zeros
365     unsigned LeadingZeros = (Upper->getValue() - 1).countLeadingZeros();
366     MinLeadingZeros = std::min(LeadingZeros, MinLeadingZeros);
367   }
368
369   KnownZero = APInt::getHighBitsSet(BitWidth, MinLeadingZeros);
370 }
371
372 static bool isEphemeralValueOf(Instruction *I, const Value *E) {
373   SmallVector<const Value *, 16> WorkSet(1, I);
374   SmallPtrSet<const Value *, 32> Visited;
375   SmallPtrSet<const Value *, 16> EphValues;
376
377   while (!WorkSet.empty()) {
378     const Value *V = WorkSet.pop_back_val();
379     if (!Visited.insert(V).second)
380       continue;
381
382     // If all uses of this value are ephemeral, then so is this value.
383     bool FoundNEUse = false;
384     for (const User *I : V->users())
385       if (!EphValues.count(I)) {
386         FoundNEUse = true;
387         break;
388       }
389
390     if (!FoundNEUse) {
391       if (V == E)
392         return true;
393
394       EphValues.insert(V);
395       if (const User *U = dyn_cast<User>(V))
396         for (User::const_op_iterator J = U->op_begin(), JE = U->op_end();
397              J != JE; ++J) {
398           if (isSafeToSpeculativelyExecute(*J))
399             WorkSet.push_back(*J);
400         }
401     }
402   }
403
404   return false;
405 }
406
407 // Is this an intrinsic that cannot be speculated but also cannot trap?
408 static bool isAssumeLikeIntrinsic(const Instruction *I) {
409   if (const CallInst *CI = dyn_cast<CallInst>(I))
410     if (Function *F = CI->getCalledFunction())
411       switch (F->getIntrinsicID()) {
412       default: break;
413       // FIXME: This list is repeated from NoTTI::getIntrinsicCost.
414       case Intrinsic::assume:
415       case Intrinsic::dbg_declare:
416       case Intrinsic::dbg_value:
417       case Intrinsic::invariant_start:
418       case Intrinsic::invariant_end:
419       case Intrinsic::lifetime_start:
420       case Intrinsic::lifetime_end:
421       case Intrinsic::objectsize:
422       case Intrinsic::ptr_annotation:
423       case Intrinsic::var_annotation:
424         return true;
425       }
426
427   return false;
428 }
429
430 static bool isValidAssumeForContext(Value *V, const Query &Q) {
431   Instruction *Inv = cast<Instruction>(V);
432
433   // There are two restrictions on the use of an assume:
434   //  1. The assume must dominate the context (or the control flow must
435   //     reach the assume whenever it reaches the context).
436   //  2. The context must not be in the assume's set of ephemeral values
437   //     (otherwise we will use the assume to prove that the condition
438   //     feeding the assume is trivially true, thus causing the removal of
439   //     the assume).
440
441   if (Q.DT) {
442     if (Q.DT->dominates(Inv, Q.CxtI)) {
443       return true;
444     } else if (Inv->getParent() == Q.CxtI->getParent()) {
445       // The context comes first, but they're both in the same block. Make sure
446       // there is nothing in between that might interrupt the control flow.
447       for (BasicBlock::const_iterator I =
448              std::next(BasicBlock::const_iterator(Q.CxtI)),
449                                       IE(Inv); I != IE; ++I)
450         if (!isSafeToSpeculativelyExecute(I) && !isAssumeLikeIntrinsic(I))
451           return false;
452
453       return !isEphemeralValueOf(Inv, Q.CxtI);
454     }
455
456     return false;
457   }
458
459   // When we don't have a DT, we do a limited search...
460   if (Inv->getParent() == Q.CxtI->getParent()->getSinglePredecessor()) {
461     return true;
462   } else if (Inv->getParent() == Q.CxtI->getParent()) {
463     // Search forward from the assume until we reach the context (or the end
464     // of the block); the common case is that the assume will come first.
465     for (BasicBlock::iterator I = std::next(BasicBlock::iterator(Inv)),
466          IE = Inv->getParent()->end(); I != IE; ++I)
467       if (I == Q.CxtI)
468         return true;
469
470     // The context must come first...
471     for (BasicBlock::const_iterator I =
472            std::next(BasicBlock::const_iterator(Q.CxtI)),
473                                     IE(Inv); I != IE; ++I)
474       if (!isSafeToSpeculativelyExecute(I) && !isAssumeLikeIntrinsic(I))
475         return false;
476
477     return !isEphemeralValueOf(Inv, Q.CxtI);
478   }
479
480   return false;
481 }
482
483 bool llvm::isValidAssumeForContext(const Instruction *I,
484                                    const Instruction *CxtI,
485                                    const DominatorTree *DT) {
486   return ::isValidAssumeForContext(const_cast<Instruction *>(I),
487                                    Query(nullptr, CxtI, DT));
488 }
489
490 template<typename LHS, typename RHS>
491 inline match_combine_or<CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>,
492                         CmpClass_match<RHS, LHS, ICmpInst, ICmpInst::Predicate>>
493 m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
494   return m_CombineOr(m_ICmp(Pred, L, R), m_ICmp(Pred, R, L));
495 }
496
497 template<typename LHS, typename RHS>
498 inline match_combine_or<BinaryOp_match<LHS, RHS, Instruction::And>,
499                         BinaryOp_match<RHS, LHS, Instruction::And>>
500 m_c_And(const LHS &L, const RHS &R) {
501   return m_CombineOr(m_And(L, R), m_And(R, L));
502 }
503
504 template<typename LHS, typename RHS>
505 inline match_combine_or<BinaryOp_match<LHS, RHS, Instruction::Or>,
506                         BinaryOp_match<RHS, LHS, Instruction::Or>>
507 m_c_Or(const LHS &L, const RHS &R) {
508   return m_CombineOr(m_Or(L, R), m_Or(R, L));
509 }
510
511 template<typename LHS, typename RHS>
512 inline match_combine_or<BinaryOp_match<LHS, RHS, Instruction::Xor>,
513                         BinaryOp_match<RHS, LHS, Instruction::Xor>>
514 m_c_Xor(const LHS &L, const RHS &R) {
515   return m_CombineOr(m_Xor(L, R), m_Xor(R, L));
516 }
517
518 /// Compute known bits in 'V' under the assumption that the condition 'Cmp' is
519 /// true (at the context instruction.)  This is mostly a utility function for
520 /// the prototype dominating conditions reasoning below.
521 static void computeKnownBitsFromTrueCondition(Value *V, ICmpInst *Cmp,
522                                               APInt &KnownZero,
523                                               APInt &KnownOne,
524                                               const DataLayout &DL,
525                                               unsigned Depth, const Query &Q) {
526   Value *LHS = Cmp->getOperand(0);
527   Value *RHS = Cmp->getOperand(1);
528   // TODO: We could potentially be more aggressive here.  This would be worth
529   // evaluating.  If we can, explore commoning this code with the assume
530   // handling logic.
531   if (LHS != V && RHS != V)
532     return;
533
534   const unsigned BitWidth = KnownZero.getBitWidth();
535
536   switch (Cmp->getPredicate()) {
537   default:
538     // We know nothing from this condition
539     break;
540   // TODO: implement unsigned bound from below (known one bits)
541   // TODO: common condition check implementations with assumes
542   // TODO: implement other patterns from assume (e.g. V & B == A)
543   case ICmpInst::ICMP_SGT:
544     if (LHS == V) {
545       APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0);
546       computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q);
547       if (KnownOneTemp.isAllOnesValue() || KnownZeroTemp.isNegative()) {
548         // We know that the sign bit is zero.
549         KnownZero |= APInt::getSignBit(BitWidth);
550       }
551     }
552     break;
553   case ICmpInst::ICMP_EQ:
554     {
555       APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0);
556       if (LHS == V)
557         computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q);
558       else if (RHS == V)
559         computeKnownBits(LHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q);
560       else
561         llvm_unreachable("missing use?");
562       KnownZero |= KnownZeroTemp;
563       KnownOne |= KnownOneTemp;
564     }
565     break;
566   case ICmpInst::ICMP_ULE:
567     if (LHS == V) {
568       APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0);
569       computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q);
570       // The known zero bits carry over
571       unsigned SignBits = KnownZeroTemp.countLeadingOnes();
572       KnownZero |= APInt::getHighBitsSet(BitWidth, SignBits);
573     }
574     break;
575   case ICmpInst::ICMP_ULT:
576     if (LHS == V) {
577       APInt KnownZeroTemp(BitWidth, 0), KnownOneTemp(BitWidth, 0);
578       computeKnownBits(RHS, KnownZeroTemp, KnownOneTemp, DL, Depth + 1, Q);
579       // Whatever high bits in rhs are zero are known to be zero (if rhs is a
580       // power of 2, then one more).
581       unsigned SignBits = KnownZeroTemp.countLeadingOnes();
582       if (isKnownToBeAPowerOfTwo(RHS, false, Depth + 1, Query(Q, Cmp), DL))
583         SignBits++;
584       KnownZero |= APInt::getHighBitsSet(BitWidth, SignBits);
585     }
586     break;
587   };
588 }
589
590 /// Compute known bits in 'V' from conditions which are known to be true along
591 /// all paths leading to the context instruction.  In particular, look for
592 /// cases where one branch of an interesting condition dominates the context
593 /// instruction.  This does not do general dataflow.
594 /// NOTE: This code is EXPERIMENTAL and currently off by default.
595 static void computeKnownBitsFromDominatingCondition(Value *V, APInt &KnownZero,
596                                                     APInt &KnownOne,
597                                                     const DataLayout &DL,
598                                                     unsigned Depth,
599                                                     const Query &Q) {
600   // Need both the dominator tree and the query location to do anything useful
601   if (!Q.DT || !Q.CxtI)
602     return;
603   Instruction *Cxt = const_cast<Instruction *>(Q.CxtI);
604
605   // Avoid useless work
606   if (auto VI = dyn_cast<Instruction>(V))
607     if (VI->getParent() == Cxt->getParent())
608       return;
609
610   // Note: We currently implement two options.  It's not clear which of these
611   // will survive long term, we need data for that.
612   // Option 1 - Try walking the dominator tree looking for conditions which
613   // might apply.  This works well for local conditions (loop guards, etc..),
614   // but not as well for things far from the context instruction (presuming a
615   // low max blocks explored).  If we can set an high enough limit, this would
616   // be all we need.
617   // Option 2 - We restrict out search to those conditions which are uses of
618   // the value we're interested in.  This is independent of dom structure,
619   // but is slightly less powerful without looking through lots of use chains.
620   // It does handle conditions far from the context instruction (e.g. early
621   // function exits on entry) really well though.
622
623   // Option 1 - Search the dom tree
624   unsigned NumBlocksExplored = 0;
625   BasicBlock *Current = Cxt->getParent();
626   while (true) {
627     // Stop searching if we've gone too far up the chain
628     if (NumBlocksExplored >= DomConditionsMaxDomBlocks)
629       break;
630     NumBlocksExplored++;
631
632     if (!Q.DT->getNode(Current)->getIDom())
633       break;
634     Current = Q.DT->getNode(Current)->getIDom()->getBlock();
635     if (!Current)
636       // found function entry
637       break;
638
639     BranchInst *BI = dyn_cast<BranchInst>(Current->getTerminator());
640     if (!BI || BI->isUnconditional())
641       continue;
642     ICmpInst *Cmp = dyn_cast<ICmpInst>(BI->getCondition());
643     if (!Cmp)
644       continue;
645
646     // We're looking for conditions that are guaranteed to hold at the context
647     // instruction.  Finding a condition where one path dominates the context
648     // isn't enough because both the true and false cases could merge before
649     // the context instruction we're actually interested in.  Instead, we need
650     // to ensure that the taken *edge* dominates the context instruction.
651     BasicBlock *BB0 = BI->getSuccessor(0);
652     BasicBlockEdge Edge(BI->getParent(), BB0);
653     if (!Edge.isSingleEdge() || !Q.DT->dominates(Edge, Q.CxtI->getParent()))
654       continue;
655
656     computeKnownBitsFromTrueCondition(V, Cmp, KnownZero, KnownOne, DL, Depth,
657                                       Q);
658   }
659
660   // Option 2 - Search the other uses of V
661   unsigned NumUsesExplored = 0;
662   for (auto U : V->users()) {
663     // Avoid massive lists
664     if (NumUsesExplored >= DomConditionsMaxUses)
665       break;
666     NumUsesExplored++;
667     // Consider only compare instructions uniquely controlling a branch
668     ICmpInst *Cmp = dyn_cast<ICmpInst>(U);
669     if (!Cmp)
670       continue;
671
672     if (DomConditionsSingleCmpUse && !Cmp->hasOneUse())
673       continue;
674
675     for (auto *CmpU : Cmp->users()) {
676       BranchInst *BI = dyn_cast<BranchInst>(CmpU);
677       if (!BI || BI->isUnconditional())
678         continue;
679       // We're looking for conditions that are guaranteed to hold at the
680       // context instruction.  Finding a condition where one path dominates
681       // the context isn't enough because both the true and false cases could
682       // merge before the context instruction we're actually interested in.
683       // Instead, we need to ensure that the taken *edge* dominates the context
684       // instruction. 
685       BasicBlock *BB0 = BI->getSuccessor(0);
686       BasicBlockEdge Edge(BI->getParent(), BB0);
687       if (!Edge.isSingleEdge() || !Q.DT->dominates(Edge, Q.CxtI->getParent()))
688         continue;
689
690       computeKnownBitsFromTrueCondition(V, Cmp, KnownZero, KnownOne, DL, Depth,
691                                         Q);
692     }
693   }
694 }
695
696 static void computeKnownBitsFromAssume(Value *V, APInt &KnownZero,
697                                        APInt &KnownOne, const DataLayout &DL,
698                                        unsigned Depth, const Query &Q) {
699   // Use of assumptions is context-sensitive. If we don't have a context, we
700   // cannot use them!
701   if (!Q.AC || !Q.CxtI)
702     return;
703
704   unsigned BitWidth = KnownZero.getBitWidth();
705
706   for (auto &AssumeVH : Q.AC->assumptions()) {
707     if (!AssumeVH)
708       continue;
709     CallInst *I = cast<CallInst>(AssumeVH);
710     assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
711            "Got assumption for the wrong function!");
712     if (Q.ExclInvs.count(I))
713       continue;
714
715     // Warning: This loop can end up being somewhat performance sensetive.
716     // We're running this loop for once for each value queried resulting in a
717     // runtime of ~O(#assumes * #values).
718
719     assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&
720            "must be an assume intrinsic");
721
722     Value *Arg = I->getArgOperand(0);
723
724     if (Arg == V && isValidAssumeForContext(I, Q)) {
725       assert(BitWidth == 1 && "assume operand is not i1?");
726       KnownZero.clearAllBits();
727       KnownOne.setAllBits();
728       return;
729     }
730
731     // The remaining tests are all recursive, so bail out if we hit the limit.
732     if (Depth == MaxDepth)
733       continue;
734
735     Value *A, *B;
736     auto m_V = m_CombineOr(m_Specific(V),
737                            m_CombineOr(m_PtrToInt(m_Specific(V)),
738                            m_BitCast(m_Specific(V))));
739
740     CmpInst::Predicate Pred;
741     ConstantInt *C;
742     // assume(v = a)
743     if (match(Arg, m_c_ICmp(Pred, m_V, m_Value(A))) &&
744         Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
745       APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
746       computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
747       KnownZero |= RHSKnownZero;
748       KnownOne  |= RHSKnownOne;
749     // assume(v & b = a)
750     } else if (match(Arg,
751                      m_c_ICmp(Pred, m_c_And(m_V, m_Value(B)), m_Value(A))) &&
752                Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
753       APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
754       computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
755       APInt MaskKnownZero(BitWidth, 0), MaskKnownOne(BitWidth, 0);
756       computeKnownBits(B, MaskKnownZero, MaskKnownOne, DL, Depth+1, Query(Q, I));
757
758       // For those bits in the mask that are known to be one, we can propagate
759       // known bits from the RHS to V.
760       KnownZero |= RHSKnownZero & MaskKnownOne;
761       KnownOne  |= RHSKnownOne  & MaskKnownOne;
762     // assume(~(v & b) = a)
763     } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_And(m_V, m_Value(B))),
764                                    m_Value(A))) &&
765                Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
766       APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
767       computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
768       APInt MaskKnownZero(BitWidth, 0), MaskKnownOne(BitWidth, 0);
769       computeKnownBits(B, MaskKnownZero, MaskKnownOne, DL, Depth+1, Query(Q, I));
770
771       // For those bits in the mask that are known to be one, we can propagate
772       // inverted known bits from the RHS to V.
773       KnownZero |= RHSKnownOne  & MaskKnownOne;
774       KnownOne  |= RHSKnownZero & MaskKnownOne;
775     // assume(v | b = a)
776     } else if (match(Arg,
777                      m_c_ICmp(Pred, m_c_Or(m_V, m_Value(B)), m_Value(A))) &&
778                Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
779       APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
780       computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
781       APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0);
782       computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I));
783
784       // For those bits in B that are known to be zero, we can propagate known
785       // bits from the RHS to V.
786       KnownZero |= RHSKnownZero & BKnownZero;
787       KnownOne  |= RHSKnownOne  & BKnownZero;
788     // assume(~(v | b) = a)
789     } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_Or(m_V, m_Value(B))),
790                                    m_Value(A))) &&
791                Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
792       APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
793       computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
794       APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0);
795       computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I));
796
797       // For those bits in B that are known to be zero, we can propagate
798       // inverted known bits from the RHS to V.
799       KnownZero |= RHSKnownOne  & BKnownZero;
800       KnownOne  |= RHSKnownZero & BKnownZero;
801     // assume(v ^ b = a)
802     } else if (match(Arg,
803                      m_c_ICmp(Pred, m_c_Xor(m_V, m_Value(B)), m_Value(A))) &&
804                Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
805       APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
806       computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
807       APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0);
808       computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I));
809
810       // For those bits in B that are known to be zero, we can propagate known
811       // bits from the RHS to V. For those bits in B that are known to be one,
812       // we can propagate inverted known bits from the RHS to V.
813       KnownZero |= RHSKnownZero & BKnownZero;
814       KnownOne  |= RHSKnownOne  & BKnownZero;
815       KnownZero |= RHSKnownOne  & BKnownOne;
816       KnownOne  |= RHSKnownZero & BKnownOne;
817     // assume(~(v ^ b) = a)
818     } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_Xor(m_V, m_Value(B))),
819                                    m_Value(A))) &&
820                Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
821       APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
822       computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
823       APInt BKnownZero(BitWidth, 0), BKnownOne(BitWidth, 0);
824       computeKnownBits(B, BKnownZero, BKnownOne, DL, Depth+1, Query(Q, I));
825
826       // For those bits in B that are known to be zero, we can propagate
827       // inverted known bits from the RHS to V. For those bits in B that are
828       // known to be one, we can propagate known bits from the RHS to V.
829       KnownZero |= RHSKnownOne  & BKnownZero;
830       KnownOne  |= RHSKnownZero & BKnownZero;
831       KnownZero |= RHSKnownZero & BKnownOne;
832       KnownOne  |= RHSKnownOne  & BKnownOne;
833     // assume(v << c = a)
834     } else if (match(Arg, m_c_ICmp(Pred, m_Shl(m_V, m_ConstantInt(C)),
835                                    m_Value(A))) &&
836                Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
837       APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
838       computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
839       // For those bits in RHS that are known, we can propagate them to known
840       // bits in V shifted to the right by C.
841       KnownZero |= RHSKnownZero.lshr(C->getZExtValue());
842       KnownOne  |= RHSKnownOne.lshr(C->getZExtValue());
843     // assume(~(v << c) = a)
844     } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_Shl(m_V, m_ConstantInt(C))),
845                                    m_Value(A))) &&
846                Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
847       APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
848       computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
849       // For those bits in RHS that are known, we can propagate them inverted
850       // to known bits in V shifted to the right by C.
851       KnownZero |= RHSKnownOne.lshr(C->getZExtValue());
852       KnownOne  |= RHSKnownZero.lshr(C->getZExtValue());
853     // assume(v >> c = a)
854     } else if (match(Arg,
855                      m_c_ICmp(Pred, m_CombineOr(m_LShr(m_V, m_ConstantInt(C)),
856                                                 m_AShr(m_V, m_ConstantInt(C))),
857                               m_Value(A))) &&
858                Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
859       APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
860       computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
861       // For those bits in RHS that are known, we can propagate them to known
862       // bits in V shifted to the right by C.
863       KnownZero |= RHSKnownZero << C->getZExtValue();
864       KnownOne  |= RHSKnownOne  << C->getZExtValue();
865     // assume(~(v >> c) = a)
866     } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_CombineOr(
867                                              m_LShr(m_V, m_ConstantInt(C)),
868                                              m_AShr(m_V, m_ConstantInt(C)))),
869                                    m_Value(A))) &&
870                Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q)) {
871       APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
872       computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
873       // For those bits in RHS that are known, we can propagate them inverted
874       // to known bits in V shifted to the right by C.
875       KnownZero |= RHSKnownOne  << C->getZExtValue();
876       KnownOne  |= RHSKnownZero << C->getZExtValue();
877     // assume(v >=_s c) where c is non-negative
878     } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
879                Pred == ICmpInst::ICMP_SGE && isValidAssumeForContext(I, Q)) {
880       APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
881       computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
882
883       if (RHSKnownZero.isNegative()) {
884         // We know that the sign bit is zero.
885         KnownZero |= APInt::getSignBit(BitWidth);
886       }
887     // assume(v >_s c) where c is at least -1.
888     } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
889                Pred == ICmpInst::ICMP_SGT && isValidAssumeForContext(I, Q)) {
890       APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
891       computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
892
893       if (RHSKnownOne.isAllOnesValue() || RHSKnownZero.isNegative()) {
894         // We know that the sign bit is zero.
895         KnownZero |= APInt::getSignBit(BitWidth);
896       }
897     // assume(v <=_s c) where c is negative
898     } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
899                Pred == ICmpInst::ICMP_SLE && isValidAssumeForContext(I, Q)) {
900       APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
901       computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
902
903       if (RHSKnownOne.isNegative()) {
904         // We know that the sign bit is one.
905         KnownOne |= APInt::getSignBit(BitWidth);
906       }
907     // assume(v <_s c) where c is non-positive
908     } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
909                Pred == ICmpInst::ICMP_SLT && isValidAssumeForContext(I, Q)) {
910       APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
911       computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
912
913       if (RHSKnownZero.isAllOnesValue() || RHSKnownOne.isNegative()) {
914         // We know that the sign bit is one.
915         KnownOne |= APInt::getSignBit(BitWidth);
916       }
917     // assume(v <=_u c)
918     } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
919                Pred == ICmpInst::ICMP_ULE && isValidAssumeForContext(I, Q)) {
920       APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
921       computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
922
923       // Whatever high bits in c are zero are known to be zero.
924       KnownZero |=
925         APInt::getHighBitsSet(BitWidth, RHSKnownZero.countLeadingOnes());
926     // assume(v <_u c)
927     } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
928                Pred == ICmpInst::ICMP_ULT && isValidAssumeForContext(I, Q)) {
929       APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
930       computeKnownBits(A, RHSKnownZero, RHSKnownOne, DL, Depth+1, Query(Q, I));
931
932       // Whatever high bits in c are zero are known to be zero (if c is a power
933       // of 2, then one more).
934       if (isKnownToBeAPowerOfTwo(A, false, Depth + 1, Query(Q, I), DL))
935         KnownZero |=
936           APInt::getHighBitsSet(BitWidth, RHSKnownZero.countLeadingOnes()+1);
937       else
938         KnownZero |=
939           APInt::getHighBitsSet(BitWidth, RHSKnownZero.countLeadingOnes());
940     }
941   }
942 }
943
944 static void computeKnownBitsFromOperator(Operator *I, APInt &KnownZero,
945                                          APInt &KnownOne, const DataLayout &DL,
946                                          unsigned Depth, const Query &Q) {
947   unsigned BitWidth = KnownZero.getBitWidth();
948
949   APInt KnownZero2(KnownZero), KnownOne2(KnownOne);
950   switch (I->getOpcode()) {
951   default: break;
952   case Instruction::Load:
953     if (MDNode *MD = cast<LoadInst>(I)->getMetadata(LLVMContext::MD_range))
954       computeKnownBitsFromRangeMetadata(*MD, KnownZero);
955     break;
956   case Instruction::And: {
957     // If either the LHS or the RHS are Zero, the result is zero.
958     computeKnownBits(I->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, Q);
959     computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q);
960
961     // Output known-1 bits are only known if set in both the LHS & RHS.
962     KnownOne &= KnownOne2;
963     // Output known-0 are known to be clear if zero in either the LHS | RHS.
964     KnownZero |= KnownZero2;
965     break;
966   }
967   case Instruction::Or: {
968     computeKnownBits(I->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, Q);
969     computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q);
970
971     // Output known-0 bits are only known if clear in both the LHS & RHS.
972     KnownZero &= KnownZero2;
973     // Output known-1 are known to be set if set in either the LHS | RHS.
974     KnownOne |= KnownOne2;
975     break;
976   }
977   case Instruction::Xor: {
978     computeKnownBits(I->getOperand(1), KnownZero, KnownOne, DL, Depth + 1, Q);
979     computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q);
980
981     // Output known-0 bits are known if clear or set in both the LHS & RHS.
982     APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
983     // Output known-1 are known to be set if set in only one of the LHS, RHS.
984     KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
985     KnownZero = KnownZeroOut;
986     break;
987   }
988   case Instruction::Mul: {
989     bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
990     computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, KnownZero,
991                         KnownOne, KnownZero2, KnownOne2, DL, Depth, Q);
992     break;
993   }
994   case Instruction::UDiv: {
995     // For the purposes of computing leading zeros we can conservatively
996     // treat a udiv as a logical right shift by the power of 2 known to
997     // be less than the denominator.
998     computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1, Q);
999     unsigned LeadZ = KnownZero2.countLeadingOnes();
1000
1001     KnownOne2.clearAllBits();
1002     KnownZero2.clearAllBits();
1003     computeKnownBits(I->getOperand(1), KnownZero2, KnownOne2, DL, Depth + 1, Q);
1004     unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1005     if (RHSUnknownLeadingOnes != BitWidth)
1006       LeadZ = std::min(BitWidth,
1007                        LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
1008
1009     KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
1010     break;
1011   }
1012   case Instruction::Select:
1013     computeKnownBits(I->getOperand(2), KnownZero, KnownOne, DL, Depth + 1, Q);
1014     computeKnownBits(I->getOperand(1), KnownZero2, KnownOne2, DL, Depth + 1, Q);
1015
1016     // Only known if known in both the LHS and RHS.
1017     KnownOne &= KnownOne2;
1018     KnownZero &= KnownZero2;
1019     break;
1020   case Instruction::FPTrunc:
1021   case Instruction::FPExt:
1022   case Instruction::FPToUI:
1023   case Instruction::FPToSI:
1024   case Instruction::SIToFP:
1025   case Instruction::UIToFP:
1026     break; // Can't work with floating point.
1027   case Instruction::PtrToInt:
1028   case Instruction::IntToPtr:
1029   case Instruction::AddrSpaceCast: // Pointers could be different sizes.
1030     // FALL THROUGH and handle them the same as zext/trunc.
1031   case Instruction::ZExt:
1032   case Instruction::Trunc: {
1033     Type *SrcTy = I->getOperand(0)->getType();
1034
1035     unsigned SrcBitWidth;
1036     // Note that we handle pointer operands here because of inttoptr/ptrtoint
1037     // which fall through here.
1038     SrcBitWidth = DL.getTypeSizeInBits(SrcTy->getScalarType());
1039
1040     assert(SrcBitWidth && "SrcBitWidth can't be zero");
1041     KnownZero = KnownZero.zextOrTrunc(SrcBitWidth);
1042     KnownOne = KnownOne.zextOrTrunc(SrcBitWidth);
1043     computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
1044     KnownZero = KnownZero.zextOrTrunc(BitWidth);
1045     KnownOne = KnownOne.zextOrTrunc(BitWidth);
1046     // Any top bits are known to be zero.
1047     if (BitWidth > SrcBitWidth)
1048       KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
1049     break;
1050   }
1051   case Instruction::BitCast: {
1052     Type *SrcTy = I->getOperand(0)->getType();
1053     if ((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
1054         // TODO: For now, not handling conversions like:
1055         // (bitcast i64 %x to <2 x i32>)
1056         !I->getType()->isVectorTy()) {
1057       computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
1058       break;
1059     }
1060     break;
1061   }
1062   case Instruction::SExt: {
1063     // Compute the bits in the result that are not present in the input.
1064     unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1065
1066     KnownZero = KnownZero.trunc(SrcBitWidth);
1067     KnownOne = KnownOne.trunc(SrcBitWidth);
1068     computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
1069     KnownZero = KnownZero.zext(BitWidth);
1070     KnownOne = KnownOne.zext(BitWidth);
1071
1072     // If the sign bit of the input is known set or clear, then we know the
1073     // top bits of the result.
1074     if (KnownZero[SrcBitWidth-1])             // Input sign bit known zero
1075       KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
1076     else if (KnownOne[SrcBitWidth-1])           // Input sign bit known set
1077       KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
1078     break;
1079   }
1080   case Instruction::Shl:
1081     // (shl X, C1) & C2 == 0   iff   (X & C2 >>u C1) == 0
1082     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
1083       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
1084       computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
1085       KnownZero <<= ShiftAmt;
1086       KnownOne  <<= ShiftAmt;
1087       KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0
1088     }
1089     break;
1090   case Instruction::LShr:
1091     // (ushr X, C1) & C2 == 0   iff  (-1 >> C1) & C2 == 0
1092     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
1093       // Compute the new bits that are at the top now.
1094       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth);
1095
1096       // Unsigned shift right.
1097       computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
1098       KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
1099       KnownOne  = APIntOps::lshr(KnownOne, ShiftAmt);
1100       // high bits known zero.
1101       KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
1102     }
1103     break;
1104   case Instruction::AShr:
1105     // (ashr X, C1) & C2 == 0   iff  (-1 >> C1) & C2 == 0
1106     if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
1107       // Compute the new bits that are at the top now.
1108       uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
1109
1110       // Signed shift right.
1111       computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
1112       KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
1113       KnownOne  = APIntOps::lshr(KnownOne, ShiftAmt);
1114
1115       APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
1116       if (KnownZero[BitWidth-ShiftAmt-1])    // New bits are known zero.
1117         KnownZero |= HighBits;
1118       else if (KnownOne[BitWidth-ShiftAmt-1])  // New bits are known one.
1119         KnownOne |= HighBits;
1120     }
1121     break;
1122   case Instruction::Sub: {
1123     bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
1124     computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW,
1125                            KnownZero, KnownOne, KnownZero2, KnownOne2, DL,
1126                            Depth, Q);
1127     break;
1128   }
1129   case Instruction::Add: {
1130     bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
1131     computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW,
1132                            KnownZero, KnownOne, KnownZero2, KnownOne2, DL,
1133                            Depth, Q);
1134     break;
1135   }
1136   case Instruction::SRem:
1137     if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
1138       APInt RA = Rem->getValue().abs();
1139       if (RA.isPowerOf2()) {
1140         APInt LowBits = RA - 1;
1141         computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, DL, Depth + 1,
1142                          Q);
1143
1144         // The low bits of the first operand are unchanged by the srem.
1145         KnownZero = KnownZero2 & LowBits;
1146         KnownOne = KnownOne2 & LowBits;
1147
1148         // If the first operand is non-negative or has all low bits zero, then
1149         // the upper bits are all zero.
1150         if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1151           KnownZero |= ~LowBits;
1152
1153         // If the first operand is negative and not all low bits are zero, then
1154         // the upper bits are all one.
1155         if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
1156           KnownOne |= ~LowBits;
1157
1158         assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1159       }
1160     }
1161
1162     // The sign bit is the LHS's sign bit, except when the result of the
1163     // remainder is zero.
1164     if (KnownZero.isNonNegative()) {
1165       APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
1166       computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, DL,
1167                        Depth + 1, Q);
1168       // If it's known zero, our sign bit is also zero.
1169       if (LHSKnownZero.isNegative())
1170         KnownZero.setBit(BitWidth - 1);
1171     }
1172
1173     break;
1174   case Instruction::URem: {
1175     if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
1176       APInt RA = Rem->getValue();
1177       if (RA.isPowerOf2()) {
1178         APInt LowBits = (RA - 1);
1179         computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1,
1180                          Q);
1181         KnownZero |= ~LowBits;
1182         KnownOne &= LowBits;
1183         break;
1184       }
1185     }
1186
1187     // Since the result is less than or equal to either operand, any leading
1188     // zero bits in either operand must also exist in the result.
1189     computeKnownBits(I->getOperand(0), KnownZero, KnownOne, DL, Depth + 1, Q);
1190     computeKnownBits(I->getOperand(1), KnownZero2, KnownOne2, DL, Depth + 1, Q);
1191
1192     unsigned Leaders = std::max(KnownZero.countLeadingOnes(),
1193                                 KnownZero2.countLeadingOnes());
1194     KnownOne.clearAllBits();
1195     KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
1196     break;
1197   }
1198
1199   case Instruction::Alloca: {
1200     AllocaInst *AI = cast<AllocaInst>(I);
1201     unsigned Align = AI->getAlignment();
1202     if (Align == 0)
1203       Align = DL.getABITypeAlignment(AI->getType()->getElementType());
1204
1205     if (Align > 0)
1206       KnownZero = APInt::getLowBitsSet(BitWidth, countTrailingZeros(Align));
1207     break;
1208   }
1209   case Instruction::GetElementPtr: {
1210     // Analyze all of the subscripts of this getelementptr instruction
1211     // to determine if we can prove known low zero bits.
1212     APInt LocalKnownZero(BitWidth, 0), LocalKnownOne(BitWidth, 0);
1213     computeKnownBits(I->getOperand(0), LocalKnownZero, LocalKnownOne, DL,
1214                      Depth + 1, Q);
1215     unsigned TrailZ = LocalKnownZero.countTrailingOnes();
1216
1217     gep_type_iterator GTI = gep_type_begin(I);
1218     for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1219       Value *Index = I->getOperand(i);
1220       if (StructType *STy = dyn_cast<StructType>(*GTI)) {
1221         // Handle struct member offset arithmetic.
1222
1223         // Handle case when index is vector zeroinitializer
1224         Constant *CIndex = cast<Constant>(Index);
1225         if (CIndex->isZeroValue())
1226           continue;
1227
1228         if (CIndex->getType()->isVectorTy())
1229           Index = CIndex->getSplatValue();
1230
1231         unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1232         const StructLayout *SL = DL.getStructLayout(STy);
1233         uint64_t Offset = SL->getElementOffset(Idx);
1234         TrailZ = std::min<unsigned>(TrailZ,
1235                                     countTrailingZeros(Offset));
1236       } else {
1237         // Handle array index arithmetic.
1238         Type *IndexedTy = GTI.getIndexedType();
1239         if (!IndexedTy->isSized()) {
1240           TrailZ = 0;
1241           break;
1242         }
1243         unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits();
1244         uint64_t TypeSize = DL.getTypeAllocSize(IndexedTy);
1245         LocalKnownZero = LocalKnownOne = APInt(GEPOpiBits, 0);
1246         computeKnownBits(Index, LocalKnownZero, LocalKnownOne, DL, Depth + 1,
1247                          Q);
1248         TrailZ = std::min(TrailZ,
1249                           unsigned(countTrailingZeros(TypeSize) +
1250                                    LocalKnownZero.countTrailingOnes()));
1251       }
1252     }
1253
1254     KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ);
1255     break;
1256   }
1257   case Instruction::PHI: {
1258     PHINode *P = cast<PHINode>(I);
1259     // Handle the case of a simple two-predecessor recurrence PHI.
1260     // There's a lot more that could theoretically be done here, but
1261     // this is sufficient to catch some interesting cases.
1262     if (P->getNumIncomingValues() == 2) {
1263       for (unsigned i = 0; i != 2; ++i) {
1264         Value *L = P->getIncomingValue(i);
1265         Value *R = P->getIncomingValue(!i);
1266         Operator *LU = dyn_cast<Operator>(L);
1267         if (!LU)
1268           continue;
1269         unsigned Opcode = LU->getOpcode();
1270         // Check for operations that have the property that if
1271         // both their operands have low zero bits, the result
1272         // will have low zero bits.
1273         if (Opcode == Instruction::Add ||
1274             Opcode == Instruction::Sub ||
1275             Opcode == Instruction::And ||
1276             Opcode == Instruction::Or ||
1277             Opcode == Instruction::Mul) {
1278           Value *LL = LU->getOperand(0);
1279           Value *LR = LU->getOperand(1);
1280           // Find a recurrence.
1281           if (LL == I)
1282             L = LR;
1283           else if (LR == I)
1284             L = LL;
1285           else
1286             break;
1287           // Ok, we have a PHI of the form L op= R. Check for low
1288           // zero bits.
1289           computeKnownBits(R, KnownZero2, KnownOne2, DL, Depth + 1, Q);
1290
1291           // We need to take the minimum number of known bits
1292           APInt KnownZero3(KnownZero), KnownOne3(KnownOne);
1293           computeKnownBits(L, KnownZero3, KnownOne3, DL, Depth + 1, Q);
1294
1295           KnownZero = APInt::getLowBitsSet(BitWidth,
1296                                            std::min(KnownZero2.countTrailingOnes(),
1297                                                     KnownZero3.countTrailingOnes()));
1298           break;
1299         }
1300       }
1301     }
1302
1303     // Unreachable blocks may have zero-operand PHI nodes.
1304     if (P->getNumIncomingValues() == 0)
1305       break;
1306
1307     // Otherwise take the unions of the known bit sets of the operands,
1308     // taking conservative care to avoid excessive recursion.
1309     if (Depth < MaxDepth - 1 && !KnownZero && !KnownOne) {
1310       // Skip if every incoming value references to ourself.
1311       if (dyn_cast_or_null<UndefValue>(P->hasConstantValue()))
1312         break;
1313
1314       KnownZero = APInt::getAllOnesValue(BitWidth);
1315       KnownOne = APInt::getAllOnesValue(BitWidth);
1316       for (Value *IncValue : P->incoming_values()) {
1317         // Skip direct self references.
1318         if (IncValue == P) continue;
1319
1320         KnownZero2 = APInt(BitWidth, 0);
1321         KnownOne2 = APInt(BitWidth, 0);
1322         // Recurse, but cap the recursion to one level, because we don't
1323         // want to waste time spinning around in loops.
1324         computeKnownBits(IncValue, KnownZero2, KnownOne2, DL,
1325                          MaxDepth - 1, Q);
1326         KnownZero &= KnownZero2;
1327         KnownOne &= KnownOne2;
1328         // If all bits have been ruled out, there's no need to check
1329         // more operands.
1330         if (!KnownZero && !KnownOne)
1331           break;
1332       }
1333     }
1334     break;
1335   }
1336   case Instruction::Call:
1337   case Instruction::Invoke:
1338     if (MDNode *MD = cast<Instruction>(I)->getMetadata(LLVMContext::MD_range))
1339       computeKnownBitsFromRangeMetadata(*MD, KnownZero);
1340     // If a range metadata is attached to this IntrinsicInst, intersect the
1341     // explicit range specified by the metadata and the implicit range of
1342     // the intrinsic.
1343     if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1344       switch (II->getIntrinsicID()) {
1345       default: break;
1346       case Intrinsic::ctlz:
1347       case Intrinsic::cttz: {
1348         unsigned LowBits = Log2_32(BitWidth)+1;
1349         // If this call is undefined for 0, the result will be less than 2^n.
1350         if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1351           LowBits -= 1;
1352         KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
1353         break;
1354       }
1355       case Intrinsic::ctpop: {
1356         unsigned LowBits = Log2_32(BitWidth)+1;
1357         KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
1358         break;
1359       }
1360       case Intrinsic::x86_sse42_crc32_64_64:
1361         KnownZero |= APInt::getHighBitsSet(64, 32);
1362         break;
1363       }
1364     }
1365     break;
1366   case Instruction::ExtractValue:
1367     if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
1368       ExtractValueInst *EVI = cast<ExtractValueInst>(I);
1369       if (EVI->getNumIndices() != 1) break;
1370       if (EVI->getIndices()[0] == 0) {
1371         switch (II->getIntrinsicID()) {
1372         default: break;
1373         case Intrinsic::uadd_with_overflow:
1374         case Intrinsic::sadd_with_overflow:
1375           computeKnownBitsAddSub(true, II->getArgOperand(0),
1376                                  II->getArgOperand(1), false, KnownZero,
1377                                  KnownOne, KnownZero2, KnownOne2, DL, Depth, Q);
1378           break;
1379         case Intrinsic::usub_with_overflow:
1380         case Intrinsic::ssub_with_overflow:
1381           computeKnownBitsAddSub(false, II->getArgOperand(0),
1382                                  II->getArgOperand(1), false, KnownZero,
1383                                  KnownOne, KnownZero2, KnownOne2, DL, Depth, Q);
1384           break;
1385         case Intrinsic::umul_with_overflow:
1386         case Intrinsic::smul_with_overflow:
1387           computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
1388                               KnownZero, KnownOne, KnownZero2, KnownOne2, DL,
1389                               Depth, Q);
1390           break;
1391         }
1392       }
1393     }
1394   }
1395 }
1396
1397 /// Determine which bits of V are known to be either zero or one and return
1398 /// them in the KnownZero/KnownOne bit sets.
1399 ///
1400 /// NOTE: we cannot consider 'undef' to be "IsZero" here.  The problem is that
1401 /// we cannot optimize based on the assumption that it is zero without changing
1402 /// it to be an explicit zero.  If we don't change it to zero, other code could
1403 /// optimized based on the contradictory assumption that it is non-zero.
1404 /// Because instcombine aggressively folds operations with undef args anyway,
1405 /// this won't lose us code quality.
1406 ///
1407 /// This function is defined on values with integer type, values with pointer
1408 /// type, and vectors of integers.  In the case
1409 /// where V is a vector, known zero, and known one values are the
1410 /// same width as the vector element, and the bit is set only if it is true
1411 /// for all of the elements in the vector.
1412 void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
1413                       const DataLayout &DL, unsigned Depth, const Query &Q) {
1414   assert(V && "No Value?");
1415   assert(Depth <= MaxDepth && "Limit Search Depth");
1416   unsigned BitWidth = KnownZero.getBitWidth();
1417
1418   assert((V->getType()->isIntOrIntVectorTy() ||
1419           V->getType()->getScalarType()->isPointerTy()) &&
1420          "Not integer or pointer type!");
1421   assert((DL.getTypeSizeInBits(V->getType()->getScalarType()) == BitWidth) &&
1422          (!V->getType()->isIntOrIntVectorTy() ||
1423           V->getType()->getScalarSizeInBits() == BitWidth) &&
1424          KnownZero.getBitWidth() == BitWidth &&
1425          KnownOne.getBitWidth() == BitWidth &&
1426          "V, KnownOne and KnownZero should have same BitWidth");
1427
1428   if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
1429     // We know all of the bits for a constant!
1430     KnownOne = CI->getValue();
1431     KnownZero = ~KnownOne;
1432     return;
1433   }
1434   // Null and aggregate-zero are all-zeros.
1435   if (isa<ConstantPointerNull>(V) ||
1436       isa<ConstantAggregateZero>(V)) {
1437     KnownOne.clearAllBits();
1438     KnownZero = APInt::getAllOnesValue(BitWidth);
1439     return;
1440   }
1441   // Handle a constant vector by taking the intersection of the known bits of
1442   // each element.  There is no real need to handle ConstantVector here, because
1443   // we don't handle undef in any particularly useful way.
1444   if (ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V)) {
1445     // We know that CDS must be a vector of integers. Take the intersection of
1446     // each element.
1447     KnownZero.setAllBits(); KnownOne.setAllBits();
1448     APInt Elt(KnownZero.getBitWidth(), 0);
1449     for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1450       Elt = CDS->getElementAsInteger(i);
1451       KnownZero &= ~Elt;
1452       KnownOne &= Elt;
1453     }
1454     return;
1455   }
1456
1457   // The address of an aligned GlobalValue has trailing zeros.
1458   if (auto *GO = dyn_cast<GlobalObject>(V)) {
1459     unsigned Align = GO->getAlignment();
1460     if (Align == 0) {
1461       if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
1462         Type *ObjectType = GVar->getType()->getElementType();
1463         if (ObjectType->isSized()) {
1464           // If the object is defined in the current Module, we'll be giving
1465           // it the preferred alignment. Otherwise, we have to assume that it
1466           // may only have the minimum ABI alignment.
1467           if (GVar->isStrongDefinitionForLinker())
1468             Align = DL.getPreferredAlignment(GVar);
1469           else
1470             Align = DL.getABITypeAlignment(ObjectType);
1471         }
1472       }
1473     }
1474     if (Align > 0)
1475       KnownZero = APInt::getLowBitsSet(BitWidth,
1476                                        countTrailingZeros(Align));
1477     else
1478       KnownZero.clearAllBits();
1479     KnownOne.clearAllBits();
1480     return;
1481   }
1482
1483   if (Argument *A = dyn_cast<Argument>(V)) {
1484     unsigned Align = A->getType()->isPointerTy() ? A->getParamAlignment() : 0;
1485
1486     if (!Align && A->hasStructRetAttr()) {
1487       // An sret parameter has at least the ABI alignment of the return type.
1488       Type *EltTy = cast<PointerType>(A->getType())->getElementType();
1489       if (EltTy->isSized())
1490         Align = DL.getABITypeAlignment(EltTy);
1491     }
1492
1493     if (Align)
1494       KnownZero = APInt::getLowBitsSet(BitWidth, countTrailingZeros(Align));
1495     else
1496       KnownZero.clearAllBits();
1497     KnownOne.clearAllBits();
1498
1499     // Don't give up yet... there might be an assumption that provides more
1500     // information...
1501     computeKnownBitsFromAssume(V, KnownZero, KnownOne, DL, Depth, Q);
1502
1503     // Or a dominating condition for that matter
1504     if (EnableDomConditions && Depth <= DomConditionsMaxDepth)
1505       computeKnownBitsFromDominatingCondition(V, KnownZero, KnownOne, DL,
1506                                               Depth, Q);
1507     return;
1508   }
1509
1510   // Start out not knowing anything.
1511   KnownZero.clearAllBits(); KnownOne.clearAllBits();
1512
1513   // Limit search depth.
1514   // All recursive calls that increase depth must come after this.
1515   if (Depth == MaxDepth)
1516     return;
1517
1518   // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
1519   // the bits of its aliasee.
1520   if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
1521     if (!GA->mayBeOverridden())
1522       computeKnownBits(GA->getAliasee(), KnownZero, KnownOne, DL, Depth + 1, Q);
1523     return;
1524   }
1525
1526   if (Operator *I = dyn_cast<Operator>(V))
1527     computeKnownBitsFromOperator(I, KnownZero, KnownOne, DL, Depth, Q);
1528   // computeKnownBitsFromAssume and computeKnownBitsFromDominatingCondition
1529   // strictly refines KnownZero and KnownOne. Therefore, we run them after
1530   // computeKnownBitsFromOperator.
1531
1532   // Check whether a nearby assume intrinsic can determine some known bits.
1533   computeKnownBitsFromAssume(V, KnownZero, KnownOne, DL, Depth, Q);
1534
1535   // Check whether there's a dominating condition which implies something about
1536   // this value at the given context.
1537   if (EnableDomConditions && Depth <= DomConditionsMaxDepth)
1538     computeKnownBitsFromDominatingCondition(V, KnownZero, KnownOne, DL, Depth,
1539                                             Q);
1540
1541   assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1542 }
1543
1544 /// Determine whether the sign bit is known to be zero or one.
1545 /// Convenience wrapper around computeKnownBits.
1546 void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
1547                     const DataLayout &DL, unsigned Depth, const Query &Q) {
1548   unsigned BitWidth = getBitWidth(V->getType(), DL);
1549   if (!BitWidth) {
1550     KnownZero = false;
1551     KnownOne = false;
1552     return;
1553   }
1554   APInt ZeroBits(BitWidth, 0);
1555   APInt OneBits(BitWidth, 0);
1556   computeKnownBits(V, ZeroBits, OneBits, DL, Depth, Q);
1557   KnownOne = OneBits[BitWidth - 1];
1558   KnownZero = ZeroBits[BitWidth - 1];
1559 }
1560
1561 /// Return true if the given value is known to have exactly one
1562 /// bit set when defined. For vectors return true if every element is known to
1563 /// be a power of two when defined. Supports values with integer or pointer
1564 /// types and vectors of integers.
1565 bool isKnownToBeAPowerOfTwo(Value *V, bool OrZero, unsigned Depth,
1566                             const Query &Q, const DataLayout &DL) {
1567   if (Constant *C = dyn_cast<Constant>(V)) {
1568     if (C->isNullValue())
1569       return OrZero;
1570     if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
1571       return CI->getValue().isPowerOf2();
1572     // TODO: Handle vector constants.
1573   }
1574
1575   // 1 << X is clearly a power of two if the one is not shifted off the end.  If
1576   // it is shifted off the end then the result is undefined.
1577   if (match(V, m_Shl(m_One(), m_Value())))
1578     return true;
1579
1580   // (signbit) >>l X is clearly a power of two if the one is not shifted off the
1581   // bottom.  If it is shifted off the bottom then the result is undefined.
1582   if (match(V, m_LShr(m_SignBit(), m_Value())))
1583     return true;
1584
1585   // The remaining tests are all recursive, so bail out if we hit the limit.
1586   if (Depth++ == MaxDepth)
1587     return false;
1588
1589   Value *X = nullptr, *Y = nullptr;
1590   // A shift of a power of two is a power of two or zero.
1591   if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) ||
1592                  match(V, m_Shr(m_Value(X), m_Value()))))
1593     return isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q, DL);
1594
1595   if (ZExtInst *ZI = dyn_cast<ZExtInst>(V))
1596     return isKnownToBeAPowerOfTwo(ZI->getOperand(0), OrZero, Depth, Q, DL);
1597
1598   if (SelectInst *SI = dyn_cast<SelectInst>(V))
1599     return isKnownToBeAPowerOfTwo(SI->getTrueValue(), OrZero, Depth, Q, DL) &&
1600            isKnownToBeAPowerOfTwo(SI->getFalseValue(), OrZero, Depth, Q, DL);
1601
1602   if (OrZero && match(V, m_And(m_Value(X), m_Value(Y)))) {
1603     // A power of two and'd with anything is a power of two or zero.
1604     if (isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q, DL) ||
1605         isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, Depth, Q, DL))
1606       return true;
1607     // X & (-X) is always a power of two or zero.
1608     if (match(X, m_Neg(m_Specific(Y))) || match(Y, m_Neg(m_Specific(X))))
1609       return true;
1610     return false;
1611   }
1612
1613   // Adding a power-of-two or zero to the same power-of-two or zero yields
1614   // either the original power-of-two, a larger power-of-two or zero.
1615   if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
1616     OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V);
1617     if (OrZero || VOBO->hasNoUnsignedWrap() || VOBO->hasNoSignedWrap()) {
1618       if (match(X, m_And(m_Specific(Y), m_Value())) ||
1619           match(X, m_And(m_Value(), m_Specific(Y))))
1620         if (isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q, DL))
1621           return true;
1622       if (match(Y, m_And(m_Specific(X), m_Value())) ||
1623           match(Y, m_And(m_Value(), m_Specific(X))))
1624         if (isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q, DL))
1625           return true;
1626
1627       unsigned BitWidth = V->getType()->getScalarSizeInBits();
1628       APInt LHSZeroBits(BitWidth, 0), LHSOneBits(BitWidth, 0);
1629       computeKnownBits(X, LHSZeroBits, LHSOneBits, DL, Depth, Q);
1630
1631       APInt RHSZeroBits(BitWidth, 0), RHSOneBits(BitWidth, 0);
1632       computeKnownBits(Y, RHSZeroBits, RHSOneBits, DL, Depth, Q);
1633       // If i8 V is a power of two or zero:
1634       //  ZeroBits: 1 1 1 0 1 1 1 1
1635       // ~ZeroBits: 0 0 0 1 0 0 0 0
1636       if ((~(LHSZeroBits & RHSZeroBits)).isPowerOf2())
1637         // If OrZero isn't set, we cannot give back a zero result.
1638         // Make sure either the LHS or RHS has a bit set.
1639         if (OrZero || RHSOneBits.getBoolValue() || LHSOneBits.getBoolValue())
1640           return true;
1641     }
1642   }
1643
1644   // An exact divide or right shift can only shift off zero bits, so the result
1645   // is a power of two only if the first operand is a power of two and not
1646   // copying a sign bit (sdiv int_min, 2).
1647   if (match(V, m_Exact(m_LShr(m_Value(), m_Value()))) ||
1648       match(V, m_Exact(m_UDiv(m_Value(), m_Value())))) {
1649     return isKnownToBeAPowerOfTwo(cast<Operator>(V)->getOperand(0), OrZero,
1650                                   Depth, Q, DL);
1651   }
1652
1653   return false;
1654 }
1655
1656 /// \brief Test whether a GEP's result is known to be non-null.
1657 ///
1658 /// Uses properties inherent in a GEP to try to determine whether it is known
1659 /// to be non-null.
1660 ///
1661 /// Currently this routine does not support vector GEPs.
1662 static bool isGEPKnownNonNull(GEPOperator *GEP, const DataLayout &DL,
1663                               unsigned Depth, const Query &Q) {
1664   if (!GEP->isInBounds() || GEP->getPointerAddressSpace() != 0)
1665     return false;
1666
1667   // FIXME: Support vector-GEPs.
1668   assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
1669
1670   // If the base pointer is non-null, we cannot walk to a null address with an
1671   // inbounds GEP in address space zero.
1672   if (isKnownNonZero(GEP->getPointerOperand(), DL, Depth, Q))
1673     return true;
1674
1675   // Walk the GEP operands and see if any operand introduces a non-zero offset.
1676   // If so, then the GEP cannot produce a null pointer, as doing so would
1677   // inherently violate the inbounds contract within address space zero.
1678   for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP);
1679        GTI != GTE; ++GTI) {
1680     // Struct types are easy -- they must always be indexed by a constant.
1681     if (StructType *STy = dyn_cast<StructType>(*GTI)) {
1682       ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
1683       unsigned ElementIdx = OpC->getZExtValue();
1684       const StructLayout *SL = DL.getStructLayout(STy);
1685       uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
1686       if (ElementOffset > 0)
1687         return true;
1688       continue;
1689     }
1690
1691     // If we have a zero-sized type, the index doesn't matter. Keep looping.
1692     if (DL.getTypeAllocSize(GTI.getIndexedType()) == 0)
1693       continue;
1694
1695     // Fast path the constant operand case both for efficiency and so we don't
1696     // increment Depth when just zipping down an all-constant GEP.
1697     if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
1698       if (!OpC->isZero())
1699         return true;
1700       continue;
1701     }
1702
1703     // We post-increment Depth here because while isKnownNonZero increments it
1704     // as well, when we pop back up that increment won't persist. We don't want
1705     // to recurse 10k times just because we have 10k GEP operands. We don't
1706     // bail completely out because we want to handle constant GEPs regardless
1707     // of depth.
1708     if (Depth++ >= MaxDepth)
1709       continue;
1710
1711     if (isKnownNonZero(GTI.getOperand(), DL, Depth, Q))
1712       return true;
1713   }
1714
1715   return false;
1716 }
1717
1718 /// Does the 'Range' metadata (which must be a valid MD_range operand list)
1719 /// ensure that the value it's attached to is never Value?  'RangeType' is
1720 /// is the type of the value described by the range.
1721 static bool rangeMetadataExcludesValue(MDNode* Ranges,
1722                                        const APInt& Value) {
1723   const unsigned NumRanges = Ranges->getNumOperands() / 2;
1724   assert(NumRanges >= 1);
1725   for (unsigned i = 0; i < NumRanges; ++i) {
1726     ConstantInt *Lower =
1727         mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
1728     ConstantInt *Upper =
1729         mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
1730     ConstantRange Range(Lower->getValue(), Upper->getValue());
1731     if (Range.contains(Value))
1732       return false;
1733   }
1734   return true;
1735 }
1736
1737 /// Return true if the given value is known to be non-zero when defined.
1738 /// For vectors return true if every element is known to be non-zero when
1739 /// defined. Supports values with integer or pointer type and vectors of
1740 /// integers.
1741 bool isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth,
1742                     const Query &Q) {
1743   if (Constant *C = dyn_cast<Constant>(V)) {
1744     if (C->isNullValue())
1745       return false;
1746     if (isa<ConstantInt>(C))
1747       // Must be non-zero due to null test above.
1748       return true;
1749     // TODO: Handle vectors
1750     return false;
1751   }
1752
1753   if (Instruction* I = dyn_cast<Instruction>(V)) {
1754     if (MDNode *Ranges = I->getMetadata(LLVMContext::MD_range)) {
1755       // If the possible ranges don't contain zero, then the value is
1756       // definitely non-zero.
1757       if (IntegerType* Ty = dyn_cast<IntegerType>(V->getType())) {
1758         const APInt ZeroValue(Ty->getBitWidth(), 0);
1759         if (rangeMetadataExcludesValue(Ranges, ZeroValue))
1760           return true;
1761       }
1762     }
1763   }
1764
1765   // The remaining tests are all recursive, so bail out if we hit the limit.
1766   if (Depth++ >= MaxDepth)
1767     return false;
1768
1769   // Check for pointer simplifications.
1770   if (V->getType()->isPointerTy()) {
1771     if (isKnownNonNull(V))
1772       return true; 
1773     if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
1774       if (isGEPKnownNonNull(GEP, DL, Depth, Q))
1775         return true;
1776   }
1777
1778   unsigned BitWidth = getBitWidth(V->getType()->getScalarType(), DL);
1779
1780   // X | Y != 0 if X != 0 or Y != 0.
1781   Value *X = nullptr, *Y = nullptr;
1782   if (match(V, m_Or(m_Value(X), m_Value(Y))))
1783     return isKnownNonZero(X, DL, Depth, Q) || isKnownNonZero(Y, DL, Depth, Q);
1784
1785   // ext X != 0 if X != 0.
1786   if (isa<SExtInst>(V) || isa<ZExtInst>(V))
1787     return isKnownNonZero(cast<Instruction>(V)->getOperand(0), DL, Depth, Q);
1788
1789   // shl X, Y != 0 if X is odd.  Note that the value of the shift is undefined
1790   // if the lowest bit is shifted off the end.
1791   if (BitWidth && match(V, m_Shl(m_Value(X), m_Value(Y)))) {
1792     // shl nuw can't remove any non-zero bits.
1793     OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
1794     if (BO->hasNoUnsignedWrap())
1795       return isKnownNonZero(X, DL, Depth, Q);
1796
1797     APInt KnownZero(BitWidth, 0);
1798     APInt KnownOne(BitWidth, 0);
1799     computeKnownBits(X, KnownZero, KnownOne, DL, Depth, Q);
1800     if (KnownOne[0])
1801       return true;
1802   }
1803   // shr X, Y != 0 if X is negative.  Note that the value of the shift is not
1804   // defined if the sign bit is shifted off the end.
1805   else if (match(V, m_Shr(m_Value(X), m_Value(Y)))) {
1806     // shr exact can only shift out zero bits.
1807     PossiblyExactOperator *BO = cast<PossiblyExactOperator>(V);
1808     if (BO->isExact())
1809       return isKnownNonZero(X, DL, Depth, Q);
1810
1811     bool XKnownNonNegative, XKnownNegative;
1812     ComputeSignBit(X, XKnownNonNegative, XKnownNegative, DL, Depth, Q);
1813     if (XKnownNegative)
1814       return true;
1815   }
1816   // div exact can only produce a zero if the dividend is zero.
1817   else if (match(V, m_Exact(m_IDiv(m_Value(X), m_Value())))) {
1818     return isKnownNonZero(X, DL, Depth, Q);
1819   }
1820   // X + Y.
1821   else if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
1822     bool XKnownNonNegative, XKnownNegative;
1823     bool YKnownNonNegative, YKnownNegative;
1824     ComputeSignBit(X, XKnownNonNegative, XKnownNegative, DL, Depth, Q);
1825     ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, DL, Depth, Q);
1826
1827     // If X and Y are both non-negative (as signed values) then their sum is not
1828     // zero unless both X and Y are zero.
1829     if (XKnownNonNegative && YKnownNonNegative)
1830       if (isKnownNonZero(X, DL, Depth, Q) || isKnownNonZero(Y, DL, Depth, Q))
1831         return true;
1832
1833     // If X and Y are both negative (as signed values) then their sum is not
1834     // zero unless both X and Y equal INT_MIN.
1835     if (BitWidth && XKnownNegative && YKnownNegative) {
1836       APInt KnownZero(BitWidth, 0);
1837       APInt KnownOne(BitWidth, 0);
1838       APInt Mask = APInt::getSignedMaxValue(BitWidth);
1839       // The sign bit of X is set.  If some other bit is set then X is not equal
1840       // to INT_MIN.
1841       computeKnownBits(X, KnownZero, KnownOne, DL, Depth, Q);
1842       if ((KnownOne & Mask) != 0)
1843         return true;
1844       // The sign bit of Y is set.  If some other bit is set then Y is not equal
1845       // to INT_MIN.
1846       computeKnownBits(Y, KnownZero, KnownOne, DL, Depth, Q);
1847       if ((KnownOne & Mask) != 0)
1848         return true;
1849     }
1850
1851     // The sum of a non-negative number and a power of two is not zero.
1852     if (XKnownNonNegative &&
1853         isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q, DL))
1854       return true;
1855     if (YKnownNonNegative &&
1856         isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q, DL))
1857       return true;
1858   }
1859   // X * Y.
1860   else if (match(V, m_Mul(m_Value(X), m_Value(Y)))) {
1861     OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
1862     // If X and Y are non-zero then so is X * Y as long as the multiplication
1863     // does not overflow.
1864     if ((BO->hasNoSignedWrap() || BO->hasNoUnsignedWrap()) &&
1865         isKnownNonZero(X, DL, Depth, Q) && isKnownNonZero(Y, DL, Depth, Q))
1866       return true;
1867   }
1868   // (C ? X : Y) != 0 if X != 0 and Y != 0.
1869   else if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
1870     if (isKnownNonZero(SI->getTrueValue(), DL, Depth, Q) &&
1871         isKnownNonZero(SI->getFalseValue(), DL, Depth, Q))
1872       return true;
1873   }
1874
1875   if (!BitWidth) return false;
1876   APInt KnownZero(BitWidth, 0);
1877   APInt KnownOne(BitWidth, 0);
1878   computeKnownBits(V, KnownZero, KnownOne, DL, Depth, Q);
1879   return KnownOne != 0;
1880 }
1881
1882 /// Return true if 'V & Mask' is known to be zero.  We use this predicate to
1883 /// simplify operations downstream. Mask is known to be zero for bits that V
1884 /// cannot have.
1885 ///
1886 /// This function is defined on values with integer type, values with pointer
1887 /// type, and vectors of integers.  In the case
1888 /// where V is a vector, the mask, known zero, and known one values are the
1889 /// same width as the vector element, and the bit is set only if it is true
1890 /// for all of the elements in the vector.
1891 bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL,
1892                        unsigned Depth, const Query &Q) {
1893   APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0);
1894   computeKnownBits(V, KnownZero, KnownOne, DL, Depth, Q);
1895   return (KnownZero & Mask) == Mask;
1896 }
1897
1898
1899
1900 /// Return the number of times the sign bit of the register is replicated into
1901 /// the other bits. We know that at least 1 bit is always equal to the sign bit
1902 /// (itself), but other cases can give us information. For example, immediately
1903 /// after an "ashr X, 2", we know that the top 3 bits are all equal to each
1904 /// other, so we return 3.
1905 ///
1906 /// 'Op' must have a scalar integer type.
1907 ///
1908 unsigned ComputeNumSignBits(Value *V, const DataLayout &DL, unsigned Depth,
1909                             const Query &Q) {
1910   unsigned TyBits = DL.getTypeSizeInBits(V->getType()->getScalarType());
1911   unsigned Tmp, Tmp2;
1912   unsigned FirstAnswer = 1;
1913
1914   // Note that ConstantInt is handled by the general computeKnownBits case
1915   // below.
1916
1917   if (Depth == 6)
1918     return 1;  // Limit search depth.
1919
1920   Operator *U = dyn_cast<Operator>(V);
1921   switch (Operator::getOpcode(V)) {
1922   default: break;
1923   case Instruction::SExt:
1924     Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
1925     return ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q) + Tmp;
1926
1927   case Instruction::SDiv: {
1928     const APInt *Denominator;
1929     // sdiv X, C -> adds log(C) sign bits.
1930     if (match(U->getOperand(1), m_APInt(Denominator))) {
1931
1932       // Ignore non-positive denominator.
1933       if (!Denominator->isStrictlyPositive())
1934         break;
1935
1936       // Calculate the incoming numerator bits.
1937       unsigned NumBits = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
1938
1939       // Add floor(log(C)) bits to the numerator bits.
1940       return std::min(TyBits, NumBits + Denominator->logBase2());
1941     }
1942     break;
1943   }
1944
1945   case Instruction::SRem: {
1946     const APInt *Denominator;
1947     // srem X, C -> we know that the result is within [-C+1,C) when C is a
1948     // positive constant.  This let us put a lower bound on the number of sign
1949     // bits.
1950     if (match(U->getOperand(1), m_APInt(Denominator))) {
1951
1952       // Ignore non-positive denominator.
1953       if (!Denominator->isStrictlyPositive())
1954         break;
1955
1956       // Calculate the incoming numerator bits. SRem by a positive constant
1957       // can't lower the number of sign bits.
1958       unsigned NumrBits =
1959           ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
1960
1961       // Calculate the leading sign bit constraints by examining the
1962       // denominator.  Given that the denominator is positive, there are two
1963       // cases:
1964       //
1965       //  1. the numerator is positive.  The result range is [0,C) and [0,C) u<
1966       //     (1 << ceilLogBase2(C)).
1967       //
1968       //  2. the numerator is negative.  Then the result range is (-C,0] and
1969       //     integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
1970       //
1971       // Thus a lower bound on the number of sign bits is `TyBits -
1972       // ceilLogBase2(C)`.
1973
1974       unsigned ResBits = TyBits - Denominator->ceilLogBase2();
1975       return std::max(NumrBits, ResBits);
1976     }
1977     break;
1978   }
1979
1980   case Instruction::AShr: {
1981     Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
1982     // ashr X, C   -> adds C sign bits.  Vectors too.
1983     const APInt *ShAmt;
1984     if (match(U->getOperand(1), m_APInt(ShAmt))) {
1985       Tmp += ShAmt->getZExtValue();
1986       if (Tmp > TyBits) Tmp = TyBits;
1987     }
1988     return Tmp;
1989   }
1990   case Instruction::Shl: {
1991     const APInt *ShAmt;
1992     if (match(U->getOperand(1), m_APInt(ShAmt))) {
1993       // shl destroys sign bits.
1994       Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
1995       Tmp2 = ShAmt->getZExtValue();
1996       if (Tmp2 >= TyBits ||      // Bad shift.
1997           Tmp2 >= Tmp) break;    // Shifted all sign bits out.
1998       return Tmp - Tmp2;
1999     }
2000     break;
2001   }
2002   case Instruction::And:
2003   case Instruction::Or:
2004   case Instruction::Xor:    // NOT is handled here.
2005     // Logical binary ops preserve the number of sign bits at the worst.
2006     Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
2007     if (Tmp != 1) {
2008       Tmp2 = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q);
2009       FirstAnswer = std::min(Tmp, Tmp2);
2010       // We computed what we know about the sign bits as our first
2011       // answer. Now proceed to the generic code that uses
2012       // computeKnownBits, and pick whichever answer is better.
2013     }
2014     break;
2015
2016   case Instruction::Select:
2017     Tmp = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q);
2018     if (Tmp == 1) return 1;  // Early out.
2019     Tmp2 = ComputeNumSignBits(U->getOperand(2), DL, Depth + 1, Q);
2020     return std::min(Tmp, Tmp2);
2021
2022   case Instruction::Add:
2023     // Add can have at most one carry bit.  Thus we know that the output
2024     // is, at worst, one more bit than the inputs.
2025     Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
2026     if (Tmp == 1) return 1;  // Early out.
2027
2028     // Special case decrementing a value (ADD X, -1):
2029     if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
2030       if (CRHS->isAllOnesValue()) {
2031         APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
2032         computeKnownBits(U->getOperand(0), KnownZero, KnownOne, DL, Depth + 1,
2033                          Q);
2034
2035         // If the input is known to be 0 or 1, the output is 0/-1, which is all
2036         // sign bits set.
2037         if ((KnownZero | APInt(TyBits, 1)).isAllOnesValue())
2038           return TyBits;
2039
2040         // If we are subtracting one from a positive number, there is no carry
2041         // out of the result.
2042         if (KnownZero.isNegative())
2043           return Tmp;
2044       }
2045
2046     Tmp2 = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q);
2047     if (Tmp2 == 1) return 1;
2048     return std::min(Tmp, Tmp2)-1;
2049
2050   case Instruction::Sub:
2051     Tmp2 = ComputeNumSignBits(U->getOperand(1), DL, Depth + 1, Q);
2052     if (Tmp2 == 1) return 1;
2053
2054     // Handle NEG.
2055     if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
2056       if (CLHS->isNullValue()) {
2057         APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
2058         computeKnownBits(U->getOperand(1), KnownZero, KnownOne, DL, Depth + 1,
2059                          Q);
2060         // If the input is known to be 0 or 1, the output is 0/-1, which is all
2061         // sign bits set.
2062         if ((KnownZero | APInt(TyBits, 1)).isAllOnesValue())
2063           return TyBits;
2064
2065         // If the input is known to be positive (the sign bit is known clear),
2066         // the output of the NEG has the same number of sign bits as the input.
2067         if (KnownZero.isNegative())
2068           return Tmp2;
2069
2070         // Otherwise, we treat this like a SUB.
2071       }
2072
2073     // Sub can have at most one carry bit.  Thus we know that the output
2074     // is, at worst, one more bit than the inputs.
2075     Tmp = ComputeNumSignBits(U->getOperand(0), DL, Depth + 1, Q);
2076     if (Tmp == 1) return 1;  // Early out.
2077     return std::min(Tmp, Tmp2)-1;
2078
2079   case Instruction::PHI: {
2080     PHINode *PN = cast<PHINode>(U);
2081     unsigned NumIncomingValues = PN->getNumIncomingValues();
2082     // Don't analyze large in-degree PHIs.
2083     if (NumIncomingValues > 4) break;
2084     // Unreachable blocks may have zero-operand PHI nodes.
2085     if (NumIncomingValues == 0) break;
2086
2087     // Take the minimum of all incoming values.  This can't infinitely loop
2088     // because of our depth threshold.
2089     Tmp = ComputeNumSignBits(PN->getIncomingValue(0), DL, Depth + 1, Q);
2090     for (unsigned i = 1, e = NumIncomingValues; i != e; ++i) {
2091       if (Tmp == 1) return Tmp;
2092       Tmp = std::min(
2093           Tmp, ComputeNumSignBits(PN->getIncomingValue(i), DL, Depth + 1, Q));
2094     }
2095     return Tmp;
2096   }
2097
2098   case Instruction::Trunc:
2099     // FIXME: it's tricky to do anything useful for this, but it is an important
2100     // case for targets like X86.
2101     break;
2102   }
2103
2104   // Finally, if we can prove that the top bits of the result are 0's or 1's,
2105   // use this information.
2106   APInt KnownZero(TyBits, 0), KnownOne(TyBits, 0);
2107   APInt Mask;
2108   computeKnownBits(V, KnownZero, KnownOne, DL, Depth, Q);
2109
2110   if (KnownZero.isNegative()) {        // sign bit is 0
2111     Mask = KnownZero;
2112   } else if (KnownOne.isNegative()) {  // sign bit is 1;
2113     Mask = KnownOne;
2114   } else {
2115     // Nothing known.
2116     return FirstAnswer;
2117   }
2118
2119   // Okay, we know that the sign bit in Mask is set.  Use CLZ to determine
2120   // the number of identical bits in the top of the input value.
2121   Mask = ~Mask;
2122   Mask <<= Mask.getBitWidth()-TyBits;
2123   // Return # leading zeros.  We use 'min' here in case Val was zero before
2124   // shifting.  We don't want to return '64' as for an i32 "0".
2125   return std::max(FirstAnswer, std::min(TyBits, Mask.countLeadingZeros()));
2126 }
2127
2128 /// This function computes the integer multiple of Base that equals V.
2129 /// If successful, it returns true and returns the multiple in
2130 /// Multiple. If unsuccessful, it returns false. It looks
2131 /// through SExt instructions only if LookThroughSExt is true.
2132 bool llvm::ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
2133                            bool LookThroughSExt, unsigned Depth) {
2134   const unsigned MaxDepth = 6;
2135
2136   assert(V && "No Value?");
2137   assert(Depth <= MaxDepth && "Limit Search Depth");
2138   assert(V->getType()->isIntegerTy() && "Not integer or pointer type!");
2139
2140   Type *T = V->getType();
2141
2142   ConstantInt *CI = dyn_cast<ConstantInt>(V);
2143
2144   if (Base == 0)
2145     return false;
2146
2147   if (Base == 1) {
2148     Multiple = V;
2149     return true;
2150   }
2151
2152   ConstantExpr *CO = dyn_cast<ConstantExpr>(V);
2153   Constant *BaseVal = ConstantInt::get(T, Base);
2154   if (CO && CO == BaseVal) {
2155     // Multiple is 1.
2156     Multiple = ConstantInt::get(T, 1);
2157     return true;
2158   }
2159
2160   if (CI && CI->getZExtValue() % Base == 0) {
2161     Multiple = ConstantInt::get(T, CI->getZExtValue() / Base);
2162     return true;
2163   }
2164
2165   if (Depth == MaxDepth) return false;  // Limit search depth.
2166
2167   Operator *I = dyn_cast<Operator>(V);
2168   if (!I) return false;
2169
2170   switch (I->getOpcode()) {
2171   default: break;
2172   case Instruction::SExt:
2173     if (!LookThroughSExt) return false;
2174     // otherwise fall through to ZExt
2175   case Instruction::ZExt:
2176     return ComputeMultiple(I->getOperand(0), Base, Multiple,
2177                            LookThroughSExt, Depth+1);
2178   case Instruction::Shl:
2179   case Instruction::Mul: {
2180     Value *Op0 = I->getOperand(0);
2181     Value *Op1 = I->getOperand(1);
2182
2183     if (I->getOpcode() == Instruction::Shl) {
2184       ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1);
2185       if (!Op1CI) return false;
2186       // Turn Op0 << Op1 into Op0 * 2^Op1
2187       APInt Op1Int = Op1CI->getValue();
2188       uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1);
2189       APInt API(Op1Int.getBitWidth(), 0);
2190       API.setBit(BitToSet);
2191       Op1 = ConstantInt::get(V->getContext(), API);
2192     }
2193
2194     Value *Mul0 = nullptr;
2195     if (ComputeMultiple(Op0, Base, Mul0, LookThroughSExt, Depth+1)) {
2196       if (Constant *Op1C = dyn_cast<Constant>(Op1))
2197         if (Constant *MulC = dyn_cast<Constant>(Mul0)) {
2198           if (Op1C->getType()->getPrimitiveSizeInBits() <
2199               MulC->getType()->getPrimitiveSizeInBits())
2200             Op1C = ConstantExpr::getZExt(Op1C, MulC->getType());
2201           if (Op1C->getType()->getPrimitiveSizeInBits() >
2202               MulC->getType()->getPrimitiveSizeInBits())
2203             MulC = ConstantExpr::getZExt(MulC, Op1C->getType());
2204
2205           // V == Base * (Mul0 * Op1), so return (Mul0 * Op1)
2206           Multiple = ConstantExpr::getMul(MulC, Op1C);
2207           return true;
2208         }
2209
2210       if (ConstantInt *Mul0CI = dyn_cast<ConstantInt>(Mul0))
2211         if (Mul0CI->getValue() == 1) {
2212           // V == Base * Op1, so return Op1
2213           Multiple = Op1;
2214           return true;
2215         }
2216     }
2217
2218     Value *Mul1 = nullptr;
2219     if (ComputeMultiple(Op1, Base, Mul1, LookThroughSExt, Depth+1)) {
2220       if (Constant *Op0C = dyn_cast<Constant>(Op0))
2221         if (Constant *MulC = dyn_cast<Constant>(Mul1)) {
2222           if (Op0C->getType()->getPrimitiveSizeInBits() <
2223               MulC->getType()->getPrimitiveSizeInBits())
2224             Op0C = ConstantExpr::getZExt(Op0C, MulC->getType());
2225           if (Op0C->getType()->getPrimitiveSizeInBits() >
2226               MulC->getType()->getPrimitiveSizeInBits())
2227             MulC = ConstantExpr::getZExt(MulC, Op0C->getType());
2228
2229           // V == Base * (Mul1 * Op0), so return (Mul1 * Op0)
2230           Multiple = ConstantExpr::getMul(MulC, Op0C);
2231           return true;
2232         }
2233
2234       if (ConstantInt *Mul1CI = dyn_cast<ConstantInt>(Mul1))
2235         if (Mul1CI->getValue() == 1) {
2236           // V == Base * Op0, so return Op0
2237           Multiple = Op0;
2238           return true;
2239         }
2240     }
2241   }
2242   }
2243
2244   // We could not determine if V is a multiple of Base.
2245   return false;
2246 }
2247
2248 /// Return true if we can prove that the specified FP value is never equal to
2249 /// -0.0.
2250 ///
2251 /// NOTE: this function will need to be revisited when we support non-default
2252 /// rounding modes!
2253 ///
2254 bool llvm::CannotBeNegativeZero(const Value *V, unsigned Depth) {
2255   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
2256     return !CFP->getValueAPF().isNegZero();
2257
2258   // FIXME: Magic number! At the least, this should be given a name because it's
2259   // used similarly in CannotBeOrderedLessThanZero(). A better fix may be to
2260   // expose it as a parameter, so it can be used for testing / experimenting.
2261   if (Depth == 6)
2262     return false;  // Limit search depth.
2263
2264   const Operator *I = dyn_cast<Operator>(V);
2265   if (!I) return false;
2266
2267   // Check if the nsz fast-math flag is set
2268   if (const FPMathOperator *FPO = dyn_cast<FPMathOperator>(I))
2269     if (FPO->hasNoSignedZeros())
2270       return true;
2271
2272   // (add x, 0.0) is guaranteed to return +0.0, not -0.0.
2273   if (I->getOpcode() == Instruction::FAdd)
2274     if (ConstantFP *CFP = dyn_cast<ConstantFP>(I->getOperand(1)))
2275       if (CFP->isNullValue())
2276         return true;
2277
2278   // sitofp and uitofp turn into +0.0 for zero.
2279   if (isa<SIToFPInst>(I) || isa<UIToFPInst>(I))
2280     return true;
2281
2282   if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2283     // sqrt(-0.0) = -0.0, no other negative results are possible.
2284     if (II->getIntrinsicID() == Intrinsic::sqrt)
2285       return CannotBeNegativeZero(II->getArgOperand(0), Depth+1);
2286
2287   if (const CallInst *CI = dyn_cast<CallInst>(I))
2288     if (const Function *F = CI->getCalledFunction()) {
2289       if (F->isDeclaration()) {
2290         // abs(x) != -0.0
2291         if (F->getName() == "abs") return true;
2292         // fabs[lf](x) != -0.0
2293         if (F->getName() == "fabs") return true;
2294         if (F->getName() == "fabsf") return true;
2295         if (F->getName() == "fabsl") return true;
2296         if (F->getName() == "sqrt" || F->getName() == "sqrtf" ||
2297             F->getName() == "sqrtl")
2298           return CannotBeNegativeZero(CI->getArgOperand(0), Depth+1);
2299       }
2300     }
2301
2302   return false;
2303 }
2304
2305 bool llvm::CannotBeOrderedLessThanZero(const Value *V, unsigned Depth) {
2306   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
2307     return !CFP->getValueAPF().isNegative() || CFP->getValueAPF().isZero();
2308
2309   // FIXME: Magic number! At the least, this should be given a name because it's
2310   // used similarly in CannotBeNegativeZero(). A better fix may be to
2311   // expose it as a parameter, so it can be used for testing / experimenting.
2312   if (Depth == 6)
2313     return false;  // Limit search depth.
2314
2315   const Operator *I = dyn_cast<Operator>(V);
2316   if (!I) return false;
2317
2318   switch (I->getOpcode()) {
2319   default: break;
2320   case Instruction::FMul:
2321     // x*x is always non-negative or a NaN.
2322     if (I->getOperand(0) == I->getOperand(1)) 
2323       return true;
2324     // Fall through
2325   case Instruction::FAdd:
2326   case Instruction::FDiv:
2327   case Instruction::FRem:
2328     return CannotBeOrderedLessThanZero(I->getOperand(0), Depth+1) &&
2329            CannotBeOrderedLessThanZero(I->getOperand(1), Depth+1);
2330   case Instruction::FPExt:
2331   case Instruction::FPTrunc:
2332     // Widening/narrowing never change sign.
2333     return CannotBeOrderedLessThanZero(I->getOperand(0), Depth+1);
2334   case Instruction::Call: 
2335     if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) 
2336       switch (II->getIntrinsicID()) {
2337       default: break;
2338       case Intrinsic::exp:
2339       case Intrinsic::exp2:
2340       case Intrinsic::fabs:
2341       case Intrinsic::sqrt:
2342         return true;
2343       case Intrinsic::powi: 
2344         if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
2345           // powi(x,n) is non-negative if n is even.
2346           if (CI->getBitWidth() <= 64 && CI->getSExtValue() % 2u == 0)
2347             return true;
2348         }
2349         return CannotBeOrderedLessThanZero(I->getOperand(0), Depth+1);
2350       case Intrinsic::fma:
2351       case Intrinsic::fmuladd:
2352         // x*x+y is non-negative if y is non-negative.
2353         return I->getOperand(0) == I->getOperand(1) && 
2354                CannotBeOrderedLessThanZero(I->getOperand(2), Depth+1);
2355       }
2356     break;
2357   }
2358   return false; 
2359 }
2360
2361 /// If the specified value can be set by repeating the same byte in memory,
2362 /// return the i8 value that it is represented with.  This is
2363 /// true for all i8 values obviously, but is also true for i32 0, i32 -1,
2364 /// i16 0xF0F0, double 0.0 etc.  If the value can't be handled with a repeated
2365 /// byte store (e.g. i16 0x1234), return null.
2366 Value *llvm::isBytewiseValue(Value *V) {
2367   // All byte-wide stores are splatable, even of arbitrary variables.
2368   if (V->getType()->isIntegerTy(8)) return V;
2369
2370   // Handle 'null' ConstantArrayZero etc.
2371   if (Constant *C = dyn_cast<Constant>(V))
2372     if (C->isNullValue())
2373       return Constant::getNullValue(Type::getInt8Ty(V->getContext()));
2374
2375   // Constant float and double values can be handled as integer values if the
2376   // corresponding integer value is "byteable".  An important case is 0.0.
2377   if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
2378     if (CFP->getType()->isFloatTy())
2379       V = ConstantExpr::getBitCast(CFP, Type::getInt32Ty(V->getContext()));
2380     if (CFP->getType()->isDoubleTy())
2381       V = ConstantExpr::getBitCast(CFP, Type::getInt64Ty(V->getContext()));
2382     // Don't handle long double formats, which have strange constraints.
2383   }
2384
2385   // We can handle constant integers that are multiple of 8 bits.
2386   if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
2387     if (CI->getBitWidth() % 8 == 0) {
2388       assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
2389
2390       if (!CI->getValue().isSplat(8))
2391         return nullptr;
2392       return ConstantInt::get(V->getContext(), CI->getValue().trunc(8));
2393     }
2394   }
2395
2396   // A ConstantDataArray/Vector is splatable if all its members are equal and
2397   // also splatable.
2398   if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(V)) {
2399     Value *Elt = CA->getElementAsConstant(0);
2400     Value *Val = isBytewiseValue(Elt);
2401     if (!Val)
2402       return nullptr;
2403
2404     for (unsigned I = 1, E = CA->getNumElements(); I != E; ++I)
2405       if (CA->getElementAsConstant(I) != Elt)
2406         return nullptr;
2407
2408     return Val;
2409   }
2410
2411   // Conceptually, we could handle things like:
2412   //   %a = zext i8 %X to i16
2413   //   %b = shl i16 %a, 8
2414   //   %c = or i16 %a, %b
2415   // but until there is an example that actually needs this, it doesn't seem
2416   // worth worrying about.
2417   return nullptr;
2418 }
2419
2420
2421 // This is the recursive version of BuildSubAggregate. It takes a few different
2422 // arguments. Idxs is the index within the nested struct From that we are
2423 // looking at now (which is of type IndexedType). IdxSkip is the number of
2424 // indices from Idxs that should be left out when inserting into the resulting
2425 // struct. To is the result struct built so far, new insertvalue instructions
2426 // build on that.
2427 static Value *BuildSubAggregate(Value *From, Value* To, Type *IndexedType,
2428                                 SmallVectorImpl<unsigned> &Idxs,
2429                                 unsigned IdxSkip,
2430                                 Instruction *InsertBefore) {
2431   llvm::StructType *STy = dyn_cast<llvm::StructType>(IndexedType);
2432   if (STy) {
2433     // Save the original To argument so we can modify it
2434     Value *OrigTo = To;
2435     // General case, the type indexed by Idxs is a struct
2436     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
2437       // Process each struct element recursively
2438       Idxs.push_back(i);
2439       Value *PrevTo = To;
2440       To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
2441                              InsertBefore);
2442       Idxs.pop_back();
2443       if (!To) {
2444         // Couldn't find any inserted value for this index? Cleanup
2445         while (PrevTo != OrigTo) {
2446           InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
2447           PrevTo = Del->getAggregateOperand();
2448           Del->eraseFromParent();
2449         }
2450         // Stop processing elements
2451         break;
2452       }
2453     }
2454     // If we successfully found a value for each of our subaggregates
2455     if (To)
2456       return To;
2457   }
2458   // Base case, the type indexed by SourceIdxs is not a struct, or not all of
2459   // the struct's elements had a value that was inserted directly. In the latter
2460   // case, perhaps we can't determine each of the subelements individually, but
2461   // we might be able to find the complete struct somewhere.
2462
2463   // Find the value that is at that particular spot
2464   Value *V = FindInsertedValue(From, Idxs);
2465
2466   if (!V)
2467     return nullptr;
2468
2469   // Insert the value in the new (sub) aggregrate
2470   return llvm::InsertValueInst::Create(To, V, makeArrayRef(Idxs).slice(IdxSkip),
2471                                        "tmp", InsertBefore);
2472 }
2473
2474 // This helper takes a nested struct and extracts a part of it (which is again a
2475 // struct) into a new value. For example, given the struct:
2476 // { a, { b, { c, d }, e } }
2477 // and the indices "1, 1" this returns
2478 // { c, d }.
2479 //
2480 // It does this by inserting an insertvalue for each element in the resulting
2481 // struct, as opposed to just inserting a single struct. This will only work if
2482 // each of the elements of the substruct are known (ie, inserted into From by an
2483 // insertvalue instruction somewhere).
2484 //
2485 // All inserted insertvalue instructions are inserted before InsertBefore
2486 static Value *BuildSubAggregate(Value *From, ArrayRef<unsigned> idx_range,
2487                                 Instruction *InsertBefore) {
2488   assert(InsertBefore && "Must have someplace to insert!");
2489   Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
2490                                                              idx_range);
2491   Value *To = UndefValue::get(IndexedType);
2492   SmallVector<unsigned, 10> Idxs(idx_range.begin(), idx_range.end());
2493   unsigned IdxSkip = Idxs.size();
2494
2495   return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
2496 }
2497
2498 /// Given an aggregrate and an sequence of indices, see if
2499 /// the scalar value indexed is already around as a register, for example if it
2500 /// were inserted directly into the aggregrate.
2501 ///
2502 /// If InsertBefore is not null, this function will duplicate (modified)
2503 /// insertvalues when a part of a nested struct is extracted.
2504 Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range,
2505                                Instruction *InsertBefore) {
2506   // Nothing to index? Just return V then (this is useful at the end of our
2507   // recursion).
2508   if (idx_range.empty())
2509     return V;
2510   // We have indices, so V should have an indexable type.
2511   assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
2512          "Not looking at a struct or array?");
2513   assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
2514          "Invalid indices for type?");
2515
2516   if (Constant *C = dyn_cast<Constant>(V)) {
2517     C = C->getAggregateElement(idx_range[0]);
2518     if (!C) return nullptr;
2519     return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
2520   }
2521
2522   if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
2523     // Loop the indices for the insertvalue instruction in parallel with the
2524     // requested indices
2525     const unsigned *req_idx = idx_range.begin();
2526     for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
2527          i != e; ++i, ++req_idx) {
2528       if (req_idx == idx_range.end()) {
2529         // We can't handle this without inserting insertvalues
2530         if (!InsertBefore)
2531           return nullptr;
2532
2533         // The requested index identifies a part of a nested aggregate. Handle
2534         // this specially. For example,
2535         // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
2536         // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
2537         // %C = extractvalue {i32, { i32, i32 } } %B, 1
2538         // This can be changed into
2539         // %A = insertvalue {i32, i32 } undef, i32 10, 0
2540         // %C = insertvalue {i32, i32 } %A, i32 11, 1
2541         // which allows the unused 0,0 element from the nested struct to be
2542         // removed.
2543         return BuildSubAggregate(V, makeArrayRef(idx_range.begin(), req_idx),
2544                                  InsertBefore);
2545       }
2546
2547       // This insert value inserts something else than what we are looking for.
2548       // See if the (aggregate) value inserted into has the value we are
2549       // looking for, then.
2550       if (*req_idx != *i)
2551         return FindInsertedValue(I->getAggregateOperand(), idx_range,
2552                                  InsertBefore);
2553     }
2554     // If we end up here, the indices of the insertvalue match with those
2555     // requested (though possibly only partially). Now we recursively look at
2556     // the inserted value, passing any remaining indices.
2557     return FindInsertedValue(I->getInsertedValueOperand(),
2558                              makeArrayRef(req_idx, idx_range.end()),
2559                              InsertBefore);
2560   }
2561
2562   if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
2563     // If we're extracting a value from an aggregate that was extracted from
2564     // something else, we can extract from that something else directly instead.
2565     // However, we will need to chain I's indices with the requested indices.
2566
2567     // Calculate the number of indices required
2568     unsigned size = I->getNumIndices() + idx_range.size();
2569     // Allocate some space to put the new indices in
2570     SmallVector<unsigned, 5> Idxs;
2571     Idxs.reserve(size);
2572     // Add indices from the extract value instruction
2573     Idxs.append(I->idx_begin(), I->idx_end());
2574
2575     // Add requested indices
2576     Idxs.append(idx_range.begin(), idx_range.end());
2577
2578     assert(Idxs.size() == size
2579            && "Number of indices added not correct?");
2580
2581     return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
2582   }
2583   // Otherwise, we don't know (such as, extracting from a function return value
2584   // or load instruction)
2585   return nullptr;
2586 }
2587
2588 /// Analyze the specified pointer to see if it can be expressed as a base
2589 /// pointer plus a constant offset. Return the base and offset to the caller.
2590 Value *llvm::GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
2591                                               const DataLayout &DL) {
2592   unsigned BitWidth = DL.getPointerTypeSizeInBits(Ptr->getType());
2593   APInt ByteOffset(BitWidth, 0);
2594   while (1) {
2595     if (Ptr->getType()->isVectorTy())
2596       break;
2597
2598     if (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) {
2599       APInt GEPOffset(BitWidth, 0);
2600       if (!GEP->accumulateConstantOffset(DL, GEPOffset))
2601         break;
2602
2603       ByteOffset += GEPOffset;
2604
2605       Ptr = GEP->getPointerOperand();
2606     } else if (Operator::getOpcode(Ptr) == Instruction::BitCast ||
2607                Operator::getOpcode(Ptr) == Instruction::AddrSpaceCast) {
2608       Ptr = cast<Operator>(Ptr)->getOperand(0);
2609     } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(Ptr)) {
2610       if (GA->mayBeOverridden())
2611         break;
2612       Ptr = GA->getAliasee();
2613     } else {
2614       break;
2615     }
2616   }
2617   Offset = ByteOffset.getSExtValue();
2618   return Ptr;
2619 }
2620
2621
2622 /// This function computes the length of a null-terminated C string pointed to
2623 /// by V. If successful, it returns true and returns the string in Str.
2624 /// If unsuccessful, it returns false.
2625 bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,
2626                                  uint64_t Offset, bool TrimAtNul) {
2627   assert(V);
2628
2629   // Look through bitcast instructions and geps.
2630   V = V->stripPointerCasts();
2631
2632   // If the value is a GEP instruction or constant expression, treat it as an
2633   // offset.
2634   if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
2635     // Make sure the GEP has exactly three arguments.
2636     if (GEP->getNumOperands() != 3)
2637       return false;
2638
2639     // Make sure the index-ee is a pointer to array of i8.
2640     PointerType *PT = cast<PointerType>(GEP->getOperand(0)->getType());
2641     ArrayType *AT = dyn_cast<ArrayType>(PT->getElementType());
2642     if (!AT || !AT->getElementType()->isIntegerTy(8))
2643       return false;
2644
2645     // Check to make sure that the first operand of the GEP is an integer and
2646     // has value 0 so that we are sure we're indexing into the initializer.
2647     const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
2648     if (!FirstIdx || !FirstIdx->isZero())
2649       return false;
2650
2651     // If the second index isn't a ConstantInt, then this is a variable index
2652     // into the array.  If this occurs, we can't say anything meaningful about
2653     // the string.
2654     uint64_t StartIdx = 0;
2655     if (const ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
2656       StartIdx = CI->getZExtValue();
2657     else
2658       return false;
2659     return getConstantStringInfo(GEP->getOperand(0), Str, StartIdx + Offset,
2660                                  TrimAtNul);
2661   }
2662
2663   // The GEP instruction, constant or instruction, must reference a global
2664   // variable that is a constant and is initialized. The referenced constant
2665   // initializer is the array that we'll use for optimization.
2666   const GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
2667   if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
2668     return false;
2669
2670   // Handle the all-zeros case
2671   if (GV->getInitializer()->isNullValue()) {
2672     // This is a degenerate case. The initializer is constant zero so the
2673     // length of the string must be zero.
2674     Str = "";
2675     return true;
2676   }
2677
2678   // Must be a Constant Array
2679   const ConstantDataArray *Array =
2680     dyn_cast<ConstantDataArray>(GV->getInitializer());
2681   if (!Array || !Array->isString())
2682     return false;
2683
2684   // Get the number of elements in the array
2685   uint64_t NumElts = Array->getType()->getArrayNumElements();
2686
2687   // Start out with the entire array in the StringRef.
2688   Str = Array->getAsString();
2689
2690   if (Offset > NumElts)
2691     return false;
2692
2693   // Skip over 'offset' bytes.
2694   Str = Str.substr(Offset);
2695
2696   if (TrimAtNul) {
2697     // Trim off the \0 and anything after it.  If the array is not nul
2698     // terminated, we just return the whole end of string.  The client may know
2699     // some other way that the string is length-bound.
2700     Str = Str.substr(0, Str.find('\0'));
2701   }
2702   return true;
2703 }
2704
2705 // These next two are very similar to the above, but also look through PHI
2706 // nodes.
2707 // TODO: See if we can integrate these two together.
2708
2709 /// If we can compute the length of the string pointed to by
2710 /// the specified pointer, return 'len+1'.  If we can't, return 0.
2711 static uint64_t GetStringLengthH(Value *V, SmallPtrSetImpl<PHINode*> &PHIs) {
2712   // Look through noop bitcast instructions.
2713   V = V->stripPointerCasts();
2714
2715   // If this is a PHI node, there are two cases: either we have already seen it
2716   // or we haven't.
2717   if (PHINode *PN = dyn_cast<PHINode>(V)) {
2718     if (!PHIs.insert(PN).second)
2719       return ~0ULL;  // already in the set.
2720
2721     // If it was new, see if all the input strings are the same length.
2722     uint64_t LenSoFar = ~0ULL;
2723     for (Value *IncValue : PN->incoming_values()) {
2724       uint64_t Len = GetStringLengthH(IncValue, PHIs);
2725       if (Len == 0) return 0; // Unknown length -> unknown.
2726
2727       if (Len == ~0ULL) continue;
2728
2729       if (Len != LenSoFar && LenSoFar != ~0ULL)
2730         return 0;    // Disagree -> unknown.
2731       LenSoFar = Len;
2732     }
2733
2734     // Success, all agree.
2735     return LenSoFar;
2736   }
2737
2738   // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
2739   if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
2740     uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
2741     if (Len1 == 0) return 0;
2742     uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
2743     if (Len2 == 0) return 0;
2744     if (Len1 == ~0ULL) return Len2;
2745     if (Len2 == ~0ULL) return Len1;
2746     if (Len1 != Len2) return 0;
2747     return Len1;
2748   }
2749
2750   // Otherwise, see if we can read the string.
2751   StringRef StrData;
2752   if (!getConstantStringInfo(V, StrData))
2753     return 0;
2754
2755   return StrData.size()+1;
2756 }
2757
2758 /// If we can compute the length of the string pointed to by
2759 /// the specified pointer, return 'len+1'.  If we can't, return 0.
2760 uint64_t llvm::GetStringLength(Value *V) {
2761   if (!V->getType()->isPointerTy()) return 0;
2762
2763   SmallPtrSet<PHINode*, 32> PHIs;
2764   uint64_t Len = GetStringLengthH(V, PHIs);
2765   // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
2766   // an empty string as a length.
2767   return Len == ~0ULL ? 1 : Len;
2768 }
2769
2770 /// \brief \p PN defines a loop-variant pointer to an object.  Check if the
2771 /// previous iteration of the loop was referring to the same object as \p PN.
2772 static bool isSameUnderlyingObjectInLoop(PHINode *PN, LoopInfo *LI) {
2773   // Find the loop-defined value.
2774   Loop *L = LI->getLoopFor(PN->getParent());
2775   if (PN->getNumIncomingValues() != 2)
2776     return true;
2777
2778   // Find the value from previous iteration.
2779   auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
2780   if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
2781     PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
2782   if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
2783     return true;
2784
2785   // If a new pointer is loaded in the loop, the pointer references a different
2786   // object in every iteration.  E.g.:
2787   //    for (i)
2788   //       int *p = a[i];
2789   //       ...
2790   if (auto *Load = dyn_cast<LoadInst>(PrevValue))
2791     if (!L->isLoopInvariant(Load->getPointerOperand()))
2792       return false;
2793   return true;
2794 }
2795
2796 Value *llvm::GetUnderlyingObject(Value *V, const DataLayout &DL,
2797                                  unsigned MaxLookup) {
2798   if (!V->getType()->isPointerTy())
2799     return V;
2800   for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
2801     if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
2802       V = GEP->getPointerOperand();
2803     } else if (Operator::getOpcode(V) == Instruction::BitCast ||
2804                Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
2805       V = cast<Operator>(V)->getOperand(0);
2806     } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2807       if (GA->mayBeOverridden())
2808         return V;
2809       V = GA->getAliasee();
2810     } else {
2811       // See if InstructionSimplify knows any relevant tricks.
2812       if (Instruction *I = dyn_cast<Instruction>(V))
2813         // TODO: Acquire a DominatorTree and AssumptionCache and use them.
2814         if (Value *Simplified = SimplifyInstruction(I, DL, nullptr)) {
2815           V = Simplified;
2816           continue;
2817         }
2818
2819       return V;
2820     }
2821     assert(V->getType()->isPointerTy() && "Unexpected operand type!");
2822   }
2823   return V;
2824 }
2825
2826 void llvm::GetUnderlyingObjects(Value *V, SmallVectorImpl<Value *> &Objects,
2827                                 const DataLayout &DL, LoopInfo *LI,
2828                                 unsigned MaxLookup) {
2829   SmallPtrSet<Value *, 4> Visited;
2830   SmallVector<Value *, 4> Worklist;
2831   Worklist.push_back(V);
2832   do {
2833     Value *P = Worklist.pop_back_val();
2834     P = GetUnderlyingObject(P, DL, MaxLookup);
2835
2836     if (!Visited.insert(P).second)
2837       continue;
2838
2839     if (SelectInst *SI = dyn_cast<SelectInst>(P)) {
2840       Worklist.push_back(SI->getTrueValue());
2841       Worklist.push_back(SI->getFalseValue());
2842       continue;
2843     }
2844
2845     if (PHINode *PN = dyn_cast<PHINode>(P)) {
2846       // If this PHI changes the underlying object in every iteration of the
2847       // loop, don't look through it.  Consider:
2848       //   int **A;
2849       //   for (i) {
2850       //     Prev = Curr;     // Prev = PHI (Prev_0, Curr)
2851       //     Curr = A[i];
2852       //     *Prev, *Curr;
2853       //
2854       // Prev is tracking Curr one iteration behind so they refer to different
2855       // underlying objects.
2856       if (!LI || !LI->isLoopHeader(PN->getParent()) ||
2857           isSameUnderlyingObjectInLoop(PN, LI))
2858         for (Value *IncValue : PN->incoming_values())
2859           Worklist.push_back(IncValue);
2860       continue;
2861     }
2862
2863     Objects.push_back(P);
2864   } while (!Worklist.empty());
2865 }
2866
2867 /// Return true if the only users of this pointer are lifetime markers.
2868 bool llvm::onlyUsedByLifetimeMarkers(const Value *V) {
2869   for (const User *U : V->users()) {
2870     const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
2871     if (!II) return false;
2872
2873     if (II->getIntrinsicID() != Intrinsic::lifetime_start &&
2874         II->getIntrinsicID() != Intrinsic::lifetime_end)
2875       return false;
2876   }
2877   return true;
2878 }
2879
2880 static bool isDereferenceableFromAttribute(const Value *BV, APInt Offset,
2881                                            Type *Ty, const DataLayout &DL,
2882                                            const Instruction *CtxI,
2883                                            const DominatorTree *DT,
2884                                            const TargetLibraryInfo *TLI) {
2885   assert(Offset.isNonNegative() && "offset can't be negative");
2886   assert(Ty->isSized() && "must be sized");
2887   
2888   APInt DerefBytes(Offset.getBitWidth(), 0);
2889   bool CheckForNonNull = false;
2890   if (const Argument *A = dyn_cast<Argument>(BV)) {
2891     DerefBytes = A->getDereferenceableBytes();
2892     if (!DerefBytes.getBoolValue()) {
2893       DerefBytes = A->getDereferenceableOrNullBytes();
2894       CheckForNonNull = true;
2895     }
2896   } else if (auto CS = ImmutableCallSite(BV)) {
2897     DerefBytes = CS.getDereferenceableBytes(0);
2898     if (!DerefBytes.getBoolValue()) {
2899       DerefBytes = CS.getDereferenceableOrNullBytes(0);
2900       CheckForNonNull = true;
2901     }
2902   } else if (const LoadInst *LI = dyn_cast<LoadInst>(BV)) {
2903     if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) {
2904       ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
2905       DerefBytes = CI->getLimitedValue();
2906     }
2907     if (!DerefBytes.getBoolValue()) {
2908       if (MDNode *MD = 
2909               LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
2910         ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
2911         DerefBytes = CI->getLimitedValue();
2912       }
2913       CheckForNonNull = true;
2914     }
2915   }
2916   
2917   if (DerefBytes.getBoolValue())
2918     if (DerefBytes.uge(Offset + DL.getTypeStoreSize(Ty)))
2919       if (!CheckForNonNull || isKnownNonNullAt(BV, CtxI, DT, TLI))
2920         return true;
2921
2922   return false;
2923 }
2924
2925 static bool isDereferenceableFromAttribute(const Value *V, const DataLayout &DL,
2926                                            const Instruction *CtxI,
2927                                            const DominatorTree *DT,
2928                                            const TargetLibraryInfo *TLI) {
2929   Type *VTy = V->getType();
2930   Type *Ty = VTy->getPointerElementType();
2931   if (!Ty->isSized())
2932     return false;
2933   
2934   APInt Offset(DL.getTypeStoreSizeInBits(VTy), 0);
2935   return isDereferenceableFromAttribute(V, Offset, Ty, DL, CtxI, DT, TLI);
2936 }
2937
2938 /// Return true if Value is always a dereferenceable pointer.
2939 ///
2940 /// Test if V is always a pointer to allocated and suitably aligned memory for
2941 /// a simple load or store.
2942 static bool isDereferenceablePointer(const Value *V, const DataLayout &DL,
2943                                      const Instruction *CtxI,
2944                                      const DominatorTree *DT,
2945                                      const TargetLibraryInfo *TLI,
2946                                      SmallPtrSetImpl<const Value *> &Visited) {
2947   // Note that it is not safe to speculate into a malloc'd region because
2948   // malloc may return null.
2949
2950   // These are obviously ok.
2951   if (isa<AllocaInst>(V)) return true;
2952
2953   // It's not always safe to follow a bitcast, for example:
2954   //   bitcast i8* (alloca i8) to i32*
2955   // would result in a 4-byte load from a 1-byte alloca. However,
2956   // if we're casting from a pointer from a type of larger size
2957   // to a type of smaller size (or the same size), and the alignment
2958   // is at least as large as for the resulting pointer type, then
2959   // we can look through the bitcast.
2960   if (const BitCastOperator *BC = dyn_cast<BitCastOperator>(V)) {
2961     Type *STy = BC->getSrcTy()->getPointerElementType(),
2962          *DTy = BC->getDestTy()->getPointerElementType();
2963     if (STy->isSized() && DTy->isSized() &&
2964         (DL.getTypeStoreSize(STy) >= DL.getTypeStoreSize(DTy)) &&
2965         (DL.getABITypeAlignment(STy) >= DL.getABITypeAlignment(DTy)))
2966       return isDereferenceablePointer(BC->getOperand(0), DL, CtxI,
2967                                       DT, TLI, Visited);
2968   }
2969
2970   // Global variables which can't collapse to null are ok.
2971   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
2972     return !GV->hasExternalWeakLinkage();
2973
2974   // byval arguments are okay.
2975   if (const Argument *A = dyn_cast<Argument>(V))
2976     if (A->hasByValAttr())
2977       return true;
2978     
2979   if (isDereferenceableFromAttribute(V, DL, CtxI, DT, TLI))
2980     return true;
2981
2982   // For GEPs, determine if the indexing lands within the allocated object.
2983   if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
2984     Type *VTy = GEP->getType();
2985     Type *Ty = VTy->getPointerElementType();
2986     const Value *Base = GEP->getPointerOperand();
2987
2988     // Conservatively require that the base pointer be fully dereferenceable.
2989     if (!Visited.insert(Base).second)
2990       return false;
2991     if (!isDereferenceablePointer(Base, DL, CtxI,
2992                                   DT, TLI, Visited))
2993       return false;
2994     
2995     APInt Offset(DL.getPointerTypeSizeInBits(VTy), 0);
2996     if (!GEP->accumulateConstantOffset(DL, Offset))
2997       return false;
2998     
2999     // Check if the load is within the bounds of the underlying object.
3000     uint64_t LoadSize = DL.getTypeStoreSize(Ty);
3001     Type *BaseType = Base->getType()->getPointerElementType();
3002     return (Offset + LoadSize).ule(DL.getTypeAllocSize(BaseType));
3003   }
3004
3005   // For gc.relocate, look through relocations
3006   if (const IntrinsicInst *I = dyn_cast<IntrinsicInst>(V))
3007     if (I->getIntrinsicID() == Intrinsic::experimental_gc_relocate) {
3008       GCRelocateOperands RelocateInst(I);
3009       return isDereferenceablePointer(RelocateInst.getDerivedPtr(), DL, CtxI,
3010                                       DT, TLI, Visited);
3011     }
3012
3013   if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V))
3014     return isDereferenceablePointer(ASC->getOperand(0), DL, CtxI,
3015                                     DT, TLI, Visited);
3016
3017   // If we don't know, assume the worst.
3018   return false;
3019 }
3020
3021 bool llvm::isDereferenceablePointer(const Value *V, const DataLayout &DL,
3022                                     const Instruction *CtxI,
3023                                     const DominatorTree *DT,
3024                                     const TargetLibraryInfo *TLI) {
3025   // When dereferenceability information is provided by a dereferenceable
3026   // attribute, we know exactly how many bytes are dereferenceable. If we can
3027   // determine the exact offset to the attributed variable, we can use that
3028   // information here.
3029   Type *VTy = V->getType();
3030   Type *Ty = VTy->getPointerElementType();
3031   if (Ty->isSized()) {
3032     APInt Offset(DL.getTypeStoreSizeInBits(VTy), 0);
3033     const Value *BV = V->stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
3034     
3035     if (Offset.isNonNegative())
3036       if (isDereferenceableFromAttribute(BV, Offset, Ty, DL,
3037                                          CtxI, DT, TLI))
3038         return true;
3039   }
3040
3041   SmallPtrSet<const Value *, 32> Visited;
3042   return ::isDereferenceablePointer(V, DL, CtxI, DT, TLI, Visited);
3043 }
3044
3045 bool llvm::isSafeToSpeculativelyExecute(const Value *V,
3046                                         const Instruction *CtxI,
3047                                         const DominatorTree *DT,
3048                                         const TargetLibraryInfo *TLI) {
3049   const Operator *Inst = dyn_cast<Operator>(V);
3050   if (!Inst)
3051     return false;
3052
3053   for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i)
3054     if (Constant *C = dyn_cast<Constant>(Inst->getOperand(i)))
3055       if (C->canTrap())
3056         return false;
3057
3058   switch (Inst->getOpcode()) {
3059   default:
3060     return true;
3061   case Instruction::UDiv:
3062   case Instruction::URem: {
3063     // x / y is undefined if y == 0.
3064     const APInt *V;
3065     if (match(Inst->getOperand(1), m_APInt(V)))
3066       return *V != 0;
3067     return false;
3068   }
3069   case Instruction::SDiv:
3070   case Instruction::SRem: {
3071     // x / y is undefined if y == 0 or x == INT_MIN and y == -1
3072     const APInt *Numerator, *Denominator;
3073     if (!match(Inst->getOperand(1), m_APInt(Denominator)))
3074       return false;
3075     // We cannot hoist this division if the denominator is 0.
3076     if (*Denominator == 0)
3077       return false;
3078     // It's safe to hoist if the denominator is not 0 or -1.
3079     if (*Denominator != -1)
3080       return true;
3081     // At this point we know that the denominator is -1.  It is safe to hoist as
3082     // long we know that the numerator is not INT_MIN.
3083     if (match(Inst->getOperand(0), m_APInt(Numerator)))
3084       return !Numerator->isMinSignedValue();
3085     // The numerator *might* be MinSignedValue.
3086     return false;
3087   }
3088   case Instruction::Load: {
3089     const LoadInst *LI = cast<LoadInst>(Inst);
3090     if (!LI->isUnordered() ||
3091         // Speculative load may create a race that did not exist in the source.
3092         LI->getParent()->getParent()->hasFnAttribute(Attribute::SanitizeThread))
3093       return false;
3094     const DataLayout &DL = LI->getModule()->getDataLayout();
3095     return isDereferenceablePointer(LI->getPointerOperand(), DL, CtxI, DT, TLI);
3096   }
3097   case Instruction::Call: {
3098     if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
3099       switch (II->getIntrinsicID()) {
3100       // These synthetic intrinsics have no side-effects and just mark
3101       // information about their operands.
3102       // FIXME: There are other no-op synthetic instructions that potentially
3103       // should be considered at least *safe* to speculate...
3104       case Intrinsic::dbg_declare:
3105       case Intrinsic::dbg_value:
3106         return true;
3107
3108       case Intrinsic::bswap:
3109       case Intrinsic::ctlz:
3110       case Intrinsic::ctpop:
3111       case Intrinsic::cttz:
3112       case Intrinsic::objectsize:
3113       case Intrinsic::sadd_with_overflow:
3114       case Intrinsic::smul_with_overflow:
3115       case Intrinsic::ssub_with_overflow:
3116       case Intrinsic::uadd_with_overflow:
3117       case Intrinsic::umul_with_overflow:
3118       case Intrinsic::usub_with_overflow:
3119         return true;
3120       // Sqrt should be OK, since the llvm sqrt intrinsic isn't defined to set
3121       // errno like libm sqrt would.
3122       case Intrinsic::sqrt:
3123       case Intrinsic::fma:
3124       case Intrinsic::fmuladd:
3125       case Intrinsic::fabs:
3126       case Intrinsic::minnum:
3127       case Intrinsic::maxnum:
3128         return true;
3129       // TODO: some fp intrinsics are marked as having the same error handling
3130       // as libm. They're safe to speculate when they won't error.
3131       // TODO: are convert_{from,to}_fp16 safe?
3132       // TODO: can we list target-specific intrinsics here?
3133       default: break;
3134       }
3135     }
3136     return false; // The called function could have undefined behavior or
3137                   // side-effects, even if marked readnone nounwind.
3138   }
3139   case Instruction::VAArg:
3140   case Instruction::Alloca:
3141   case Instruction::Invoke:
3142   case Instruction::PHI:
3143   case Instruction::Store:
3144   case Instruction::Ret:
3145   case Instruction::Br:
3146   case Instruction::IndirectBr:
3147   case Instruction::Switch:
3148   case Instruction::Unreachable:
3149   case Instruction::Fence:
3150   case Instruction::AtomicRMW:
3151   case Instruction::AtomicCmpXchg:
3152   case Instruction::LandingPad:
3153   case Instruction::Resume:
3154   case Instruction::CatchPad:
3155   case Instruction::CatchEndPad:
3156   case Instruction::CatchRet:
3157   case Instruction::CleanupPad:
3158   case Instruction::CleanupRet:
3159   case Instruction::TerminatePad:
3160     return false; // Misc instructions which have effects
3161   }
3162 }
3163
3164 bool llvm::mayBeMemoryDependent(const Instruction &I) {
3165   return I.mayReadOrWriteMemory() || !isSafeToSpeculativelyExecute(&I);
3166 }
3167
3168 /// Return true if we know that the specified value is never null.
3169 bool llvm::isKnownNonNull(const Value *V, const TargetLibraryInfo *TLI) {
3170   // Alloca never returns null, malloc might.
3171   if (isa<AllocaInst>(V)) return true;
3172
3173   // A byval, inalloca, or nonnull argument is never null.
3174   if (const Argument *A = dyn_cast<Argument>(V))
3175     return A->hasByValOrInAllocaAttr() || A->hasNonNullAttr();
3176
3177   // Global values are not null unless extern weak.
3178   if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
3179     return !GV->hasExternalWeakLinkage();
3180
3181   // A Load tagged w/nonnull metadata is never null. 
3182   if (const LoadInst *LI = dyn_cast<LoadInst>(V))
3183     return LI->getMetadata(LLVMContext::MD_nonnull);
3184
3185   if (auto CS = ImmutableCallSite(V))
3186     if (CS.isReturnNonNull())
3187       return true;
3188
3189   // operator new never returns null.
3190   if (isOperatorNewLikeFn(V, TLI, /*LookThroughBitCast=*/true))
3191     return true;
3192
3193   return false;
3194 }
3195
3196 static bool isKnownNonNullFromDominatingCondition(const Value *V,
3197                                                   const Instruction *CtxI,
3198                                                   const DominatorTree *DT) {
3199   unsigned NumUsesExplored = 0;
3200   for (auto U : V->users()) {
3201     // Avoid massive lists
3202     if (NumUsesExplored >= DomConditionsMaxUses)
3203       break;
3204     NumUsesExplored++;
3205     // Consider only compare instructions uniquely controlling a branch
3206     const ICmpInst *Cmp = dyn_cast<ICmpInst>(U);
3207     if (!Cmp)
3208       continue;
3209
3210     if (DomConditionsSingleCmpUse && !Cmp->hasOneUse())
3211       continue;
3212
3213     for (auto *CmpU : Cmp->users()) {
3214       const BranchInst *BI = dyn_cast<BranchInst>(CmpU);
3215       if (!BI)
3216         continue;
3217       
3218       assert(BI->isConditional() && "uses a comparison!");
3219
3220       BasicBlock *NonNullSuccessor = nullptr;
3221       CmpInst::Predicate Pred;
3222
3223       if (match(const_cast<ICmpInst*>(Cmp),
3224                 m_c_ICmp(Pred, m_Specific(V), m_Zero()))) {
3225         if (Pred == ICmpInst::ICMP_EQ)
3226           NonNullSuccessor = BI->getSuccessor(1);
3227         else if (Pred == ICmpInst::ICMP_NE)
3228           NonNullSuccessor = BI->getSuccessor(0);
3229       }
3230
3231       if (NonNullSuccessor) {
3232         BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
3233         if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
3234           return true;
3235       }
3236     }
3237   }
3238
3239   return false;
3240 }
3241
3242 bool llvm::isKnownNonNullAt(const Value *V, const Instruction *CtxI,
3243                    const DominatorTree *DT, const TargetLibraryInfo *TLI) {
3244   if (isKnownNonNull(V, TLI))
3245     return true;
3246
3247   return CtxI ? ::isKnownNonNullFromDominatingCondition(V, CtxI, DT) : false;
3248 }
3249
3250 OverflowResult llvm::computeOverflowForUnsignedMul(Value *LHS, Value *RHS,
3251                                                    const DataLayout &DL,
3252                                                    AssumptionCache *AC,
3253                                                    const Instruction *CxtI,
3254                                                    const DominatorTree *DT) {
3255   // Multiplying n * m significant bits yields a result of n + m significant
3256   // bits. If the total number of significant bits does not exceed the
3257   // result bit width (minus 1), there is no overflow.
3258   // This means if we have enough leading zero bits in the operands
3259   // we can guarantee that the result does not overflow.
3260   // Ref: "Hacker's Delight" by Henry Warren
3261   unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
3262   APInt LHSKnownZero(BitWidth, 0);
3263   APInt LHSKnownOne(BitWidth, 0);
3264   APInt RHSKnownZero(BitWidth, 0);
3265   APInt RHSKnownOne(BitWidth, 0);
3266   computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, DL, /*Depth=*/0, AC, CxtI,
3267                    DT);
3268   computeKnownBits(RHS, RHSKnownZero, RHSKnownOne, DL, /*Depth=*/0, AC, CxtI,
3269                    DT);
3270   // Note that underestimating the number of zero bits gives a more
3271   // conservative answer.
3272   unsigned ZeroBits = LHSKnownZero.countLeadingOnes() +
3273                       RHSKnownZero.countLeadingOnes();
3274   // First handle the easy case: if we have enough zero bits there's
3275   // definitely no overflow.
3276   if (ZeroBits >= BitWidth)
3277     return OverflowResult::NeverOverflows;
3278
3279   // Get the largest possible values for each operand.
3280   APInt LHSMax = ~LHSKnownZero;
3281   APInt RHSMax = ~RHSKnownZero;
3282
3283   // We know the multiply operation doesn't overflow if the maximum values for
3284   // each operand will not overflow after we multiply them together.
3285   bool MaxOverflow;
3286   LHSMax.umul_ov(RHSMax, MaxOverflow);
3287   if (!MaxOverflow)
3288     return OverflowResult::NeverOverflows;
3289
3290   // We know it always overflows if multiplying the smallest possible values for
3291   // the operands also results in overflow.
3292   bool MinOverflow;
3293   LHSKnownOne.umul_ov(RHSKnownOne, MinOverflow);
3294   if (MinOverflow)
3295     return OverflowResult::AlwaysOverflows;
3296
3297   return OverflowResult::MayOverflow;
3298 }
3299
3300 OverflowResult llvm::computeOverflowForUnsignedAdd(Value *LHS, Value *RHS,
3301                                                    const DataLayout &DL,
3302                                                    AssumptionCache *AC,
3303                                                    const Instruction *CxtI,
3304                                                    const DominatorTree *DT) {
3305   bool LHSKnownNonNegative, LHSKnownNegative;
3306   ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, DL, /*Depth=*/0,
3307                  AC, CxtI, DT);
3308   if (LHSKnownNonNegative || LHSKnownNegative) {
3309     bool RHSKnownNonNegative, RHSKnownNegative;
3310     ComputeSignBit(RHS, RHSKnownNonNegative, RHSKnownNegative, DL, /*Depth=*/0,
3311                    AC, CxtI, DT);
3312
3313     if (LHSKnownNegative && RHSKnownNegative) {
3314       // The sign bit is set in both cases: this MUST overflow.
3315       // Create a simple add instruction, and insert it into the struct.
3316       return OverflowResult::AlwaysOverflows;
3317     }
3318
3319     if (LHSKnownNonNegative && RHSKnownNonNegative) {
3320       // The sign bit is clear in both cases: this CANNOT overflow.
3321       // Create a simple add instruction, and insert it into the struct.
3322       return OverflowResult::NeverOverflows;
3323     }
3324   }
3325
3326   return OverflowResult::MayOverflow;
3327 }
3328
3329 bool llvm::isGuaranteedToTransferExecutionToSuccessor(const Instruction *I) {
3330   // FIXME: This conservative implementation can be relaxed. E.g. most
3331   // atomic operations are guaranteed to terminate on most platforms
3332   // and most functions terminate.
3333
3334   return !I->isAtomic() &&       // atomics may never succeed on some platforms
3335          !isa<CallInst>(I) &&    // could throw and might not terminate
3336          !isa<InvokeInst>(I) &&  // might not terminate and could throw to
3337                                  //   non-successor (see bug 24185 for details).
3338          !isa<ResumeInst>(I) &&  // has no successors
3339          !isa<ReturnInst>(I);    // has no successors
3340 }
3341
3342 bool llvm::isGuaranteedToExecuteForEveryIteration(const Instruction *I,
3343                                                   const Loop *L) {
3344   // The loop header is guaranteed to be executed for every iteration.
3345   //
3346   // FIXME: Relax this constraint to cover all basic blocks that are
3347   // guaranteed to be executed at every iteration.
3348   if (I->getParent() != L->getHeader()) return false;
3349
3350   for (const Instruction &LI : *L->getHeader()) {
3351     if (&LI == I) return true;
3352     if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
3353   }
3354   llvm_unreachable("Instruction not contained in its own parent basic block.");
3355 }
3356
3357 bool llvm::propagatesFullPoison(const Instruction *I) {
3358   switch (I->getOpcode()) {
3359     case Instruction::Add:
3360     case Instruction::Sub:
3361     case Instruction::Xor:
3362     case Instruction::Trunc:
3363     case Instruction::BitCast:
3364     case Instruction::AddrSpaceCast:
3365       // These operations all propagate poison unconditionally. Note that poison
3366       // is not any particular value, so xor or subtraction of poison with
3367       // itself still yields poison, not zero.
3368       return true;
3369
3370     case Instruction::AShr:
3371     case Instruction::SExt:
3372       // For these operations, one bit of the input is replicated across
3373       // multiple output bits. A replicated poison bit is still poison.
3374       return true;
3375
3376     case Instruction::Shl: {
3377       // Left shift *by* a poison value is poison. The number of
3378       // positions to shift is unsigned, so no negative values are
3379       // possible there. Left shift by zero places preserves poison. So
3380       // it only remains to consider left shift of poison by a positive
3381       // number of places.
3382       //
3383       // A left shift by a positive number of places leaves the lowest order bit
3384       // non-poisoned. However, if such a shift has a no-wrap flag, then we can
3385       // make the poison operand violate that flag, yielding a fresh full-poison
3386       // value.
3387       auto *OBO = cast<OverflowingBinaryOperator>(I);
3388       return OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap();
3389     }
3390
3391     case Instruction::Mul: {
3392       // A multiplication by zero yields a non-poison zero result, so we need to
3393       // rule out zero as an operand. Conservatively, multiplication by a
3394       // non-zero constant is not multiplication by zero.
3395       //
3396       // Multiplication by a non-zero constant can leave some bits
3397       // non-poisoned. For example, a multiplication by 2 leaves the lowest
3398       // order bit unpoisoned. So we need to consider that.
3399       //
3400       // Multiplication by 1 preserves poison. If the multiplication has a
3401       // no-wrap flag, then we can make the poison operand violate that flag
3402       // when multiplied by any integer other than 0 and 1.
3403       auto *OBO = cast<OverflowingBinaryOperator>(I);
3404       if (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) {
3405         for (Value *V : OBO->operands()) {
3406           if (auto *CI = dyn_cast<ConstantInt>(V)) {
3407             // A ConstantInt cannot yield poison, so we can assume that it is
3408             // the other operand that is poison.
3409             return !CI->isZero();
3410           }
3411         }
3412       }
3413       return false;
3414     }
3415
3416     case Instruction::GetElementPtr:
3417       // A GEP implicitly represents a sequence of additions, subtractions,
3418       // truncations, sign extensions and multiplications. The multiplications
3419       // are by the non-zero sizes of some set of types, so we do not have to be
3420       // concerned with multiplication by zero. If the GEP is in-bounds, then
3421       // these operations are implicitly no-signed-wrap so poison is propagated
3422       // by the arguments above for Add, Sub, Trunc, SExt and Mul.
3423       return cast<GEPOperator>(I)->isInBounds();
3424
3425     default:
3426       return false;
3427   }
3428 }
3429
3430 const Value *llvm::getGuaranteedNonFullPoisonOp(const Instruction *I) {
3431   switch (I->getOpcode()) {
3432     case Instruction::Store:
3433       return cast<StoreInst>(I)->getPointerOperand();
3434
3435     case Instruction::Load:
3436       return cast<LoadInst>(I)->getPointerOperand();
3437
3438     case Instruction::AtomicCmpXchg:
3439       return cast<AtomicCmpXchgInst>(I)->getPointerOperand();
3440
3441     case Instruction::AtomicRMW:
3442       return cast<AtomicRMWInst>(I)->getPointerOperand();
3443
3444     case Instruction::UDiv:
3445     case Instruction::SDiv:
3446     case Instruction::URem:
3447     case Instruction::SRem:
3448       return I->getOperand(1);
3449
3450     default:
3451       return nullptr;
3452   }
3453 }
3454
3455 bool llvm::isKnownNotFullPoison(const Instruction *PoisonI) {
3456   // We currently only look for uses of poison values within the same basic
3457   // block, as that makes it easier to guarantee that the uses will be
3458   // executed given that PoisonI is executed.
3459   //
3460   // FIXME: Expand this to consider uses beyond the same basic block. To do
3461   // this, look out for the distinction between post-dominance and strong
3462   // post-dominance.
3463   const BasicBlock *BB = PoisonI->getParent();
3464
3465   // Set of instructions that we have proved will yield poison if PoisonI
3466   // does.
3467   SmallSet<const Value *, 16> YieldsPoison;
3468   YieldsPoison.insert(PoisonI);
3469
3470   for (const Instruction *I = PoisonI, *E = BB->end(); I != E;
3471        I = I->getNextNode()) {
3472     if (I != PoisonI) {
3473       const Value *NotPoison = getGuaranteedNonFullPoisonOp(I);
3474       if (NotPoison != nullptr && YieldsPoison.count(NotPoison)) return true;
3475       if (!isGuaranteedToTransferExecutionToSuccessor(I)) return false;
3476     }
3477
3478     // Mark poison that propagates from I through uses of I.
3479     if (YieldsPoison.count(I)) {
3480       for (const User *User : I->users()) {
3481         const Instruction *UserI = cast<Instruction>(User);
3482         if (UserI->getParent() == BB && propagatesFullPoison(UserI))
3483           YieldsPoison.insert(User);
3484       }
3485     }
3486   }
3487   return false;
3488 }
3489
3490 static bool isKnownNonNaN(Value *V, FastMathFlags FMF) {
3491   if (FMF.noNaNs())
3492     return true;
3493
3494   if (auto *C = dyn_cast<ConstantFP>(V))
3495     return !C->isNaN();
3496   return false;
3497 }
3498
3499 static bool isKnownNonZero(Value *V) {
3500   if (auto *C = dyn_cast<ConstantFP>(V))
3501     return !C->isZero();
3502   return false;
3503 }
3504
3505 static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred,
3506                                               FastMathFlags FMF,
3507                                               Value *CmpLHS, Value *CmpRHS,
3508                                               Value *TrueVal, Value *FalseVal,
3509                                               Value *&LHS, Value *&RHS) {
3510   LHS = CmpLHS;
3511   RHS = CmpRHS;
3512
3513   // If the predicate is an "or-equal"  (FP) predicate, then signed zeroes may
3514   // return inconsistent results between implementations.
3515   //   (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
3516   //   minNum(0.0, -0.0)          // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
3517   // Therefore we behave conservatively and only proceed if at least one of the
3518   // operands is known to not be zero, or if we don't care about signed zeroes.
3519   switch (Pred) {
3520   default: break;
3521   case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLE:
3522   case CmpInst::FCMP_UGE: case CmpInst::FCMP_ULE:
3523     if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
3524         !isKnownNonZero(CmpRHS))
3525       return {SPF_UNKNOWN, SPNB_NA, false};
3526   }
3527
3528   SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
3529   bool Ordered = false;
3530
3531   // When given one NaN and one non-NaN input:
3532   //   - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
3533   //   - A simple C99 (a < b ? a : b) construction will return 'b' (as the
3534   //     ordered comparison fails), which could be NaN or non-NaN.
3535   // so here we discover exactly what NaN behavior is required/accepted.
3536   if (CmpInst::isFPPredicate(Pred)) {
3537     bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
3538     bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
3539
3540     if (LHSSafe && RHSSafe) {
3541       // Both operands are known non-NaN.
3542       NaNBehavior = SPNB_RETURNS_ANY;
3543     } else if (CmpInst::isOrdered(Pred)) {
3544       // An ordered comparison will return false when given a NaN, so it
3545       // returns the RHS.
3546       Ordered = true;
3547       if (LHSSafe)
3548         // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
3549         NaNBehavior = SPNB_RETURNS_NAN;
3550       else if (RHSSafe)
3551         NaNBehavior = SPNB_RETURNS_OTHER;
3552       else
3553         // Completely unsafe.
3554         return {SPF_UNKNOWN, SPNB_NA, false};
3555     } else {
3556       Ordered = false;
3557       // An unordered comparison will return true when given a NaN, so it
3558       // returns the LHS.
3559       if (LHSSafe)
3560         // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
3561         NaNBehavior = SPNB_RETURNS_OTHER;
3562       else if (RHSSafe)
3563         NaNBehavior = SPNB_RETURNS_NAN;
3564       else
3565         // Completely unsafe.
3566         return {SPF_UNKNOWN, SPNB_NA, false};
3567     }
3568   }
3569
3570   if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
3571     std::swap(CmpLHS, CmpRHS);
3572     Pred = CmpInst::getSwappedPredicate(Pred);
3573     if (NaNBehavior == SPNB_RETURNS_NAN)
3574       NaNBehavior = SPNB_RETURNS_OTHER;
3575     else if (NaNBehavior == SPNB_RETURNS_OTHER)
3576       NaNBehavior = SPNB_RETURNS_NAN;
3577     Ordered = !Ordered;
3578   }
3579
3580   // ([if]cmp X, Y) ? X : Y
3581   if (TrueVal == CmpLHS && FalseVal == CmpRHS) {
3582     switch (Pred) {
3583     default: return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
3584     case ICmpInst::ICMP_UGT:
3585     case ICmpInst::ICMP_UGE: return {SPF_UMAX, SPNB_NA, false};
3586     case ICmpInst::ICMP_SGT:
3587     case ICmpInst::ICMP_SGE: return {SPF_SMAX, SPNB_NA, false};
3588     case ICmpInst::ICMP_ULT:
3589     case ICmpInst::ICMP_ULE: return {SPF_UMIN, SPNB_NA, false};
3590     case ICmpInst::ICMP_SLT:
3591     case ICmpInst::ICMP_SLE: return {SPF_SMIN, SPNB_NA, false};
3592     case FCmpInst::FCMP_UGT:
3593     case FCmpInst::FCMP_UGE:
3594     case FCmpInst::FCMP_OGT:
3595     case FCmpInst::FCMP_OGE: return {SPF_FMAXNUM, NaNBehavior, Ordered};
3596     case FCmpInst::FCMP_ULT:
3597     case FCmpInst::FCMP_ULE:
3598     case FCmpInst::FCMP_OLT:
3599     case FCmpInst::FCMP_OLE: return {SPF_FMINNUM, NaNBehavior, Ordered};
3600     }
3601   }
3602
3603   if (ConstantInt *C1 = dyn_cast<ConstantInt>(CmpRHS)) {
3604     if ((CmpLHS == TrueVal && match(FalseVal, m_Neg(m_Specific(CmpLHS)))) ||
3605         (CmpLHS == FalseVal && match(TrueVal, m_Neg(m_Specific(CmpLHS))))) {
3606
3607       // ABS(X) ==> (X >s 0) ? X : -X and (X >s -1) ? X : -X
3608       // NABS(X) ==> (X >s 0) ? -X : X and (X >s -1) ? -X : X
3609       if (Pred == ICmpInst::ICMP_SGT && (C1->isZero() || C1->isMinusOne())) {
3610         return {(CmpLHS == TrueVal) ? SPF_ABS : SPF_NABS, SPNB_NA, false};
3611       }
3612
3613       // ABS(X) ==> (X <s 0) ? -X : X and (X <s 1) ? -X : X
3614       // NABS(X) ==> (X <s 0) ? X : -X and (X <s 1) ? X : -X
3615       if (Pred == ICmpInst::ICMP_SLT && (C1->isZero() || C1->isOne())) {
3616         return {(CmpLHS == FalseVal) ? SPF_ABS : SPF_NABS, SPNB_NA, false};
3617       }
3618     }
3619     
3620     // Y >s C ? ~Y : ~C == ~Y <s ~C ? ~Y : ~C = SMIN(~Y, ~C)
3621     if (const auto *C2 = dyn_cast<ConstantInt>(FalseVal)) {
3622       if (C1->getType() == C2->getType() && ~C1->getValue() == C2->getValue() &&
3623           (match(TrueVal, m_Not(m_Specific(CmpLHS))) ||
3624            match(CmpLHS, m_Not(m_Specific(TrueVal))))) {
3625         LHS = TrueVal;
3626         RHS = FalseVal;
3627         return {SPF_SMIN, SPNB_NA, false};
3628       }
3629     }
3630   }
3631
3632   // TODO: (X > 4) ? X : 5   -->  (X >= 5) ? X : 5  -->  MAX(X, 5)
3633
3634   return {SPF_UNKNOWN, SPNB_NA, false};
3635 }
3636
3637 static Constant *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
3638                                  Instruction::CastOps *CastOp) {
3639   CastInst *CI = dyn_cast<CastInst>(V1);
3640   Constant *C = dyn_cast<Constant>(V2);
3641   if (!CI || !C)
3642     return nullptr;
3643   *CastOp = CI->getOpcode();
3644
3645   if (isa<SExtInst>(CI) && CmpI->isSigned()) {
3646     Constant *T = ConstantExpr::getTrunc(C, CI->getSrcTy());
3647     // This is only valid if the truncated value can be sign-extended
3648     // back to the original value.
3649     if (ConstantExpr::getSExt(T, C->getType()) == C)
3650       return T;
3651     return nullptr;
3652   }
3653   if (isa<ZExtInst>(CI) && CmpI->isUnsigned())
3654     return ConstantExpr::getTrunc(C, CI->getSrcTy());
3655
3656   if (isa<TruncInst>(CI))
3657     return ConstantExpr::getIntegerCast(C, CI->getSrcTy(), CmpI->isSigned());
3658
3659   if (isa<FPToUIInst>(CI))
3660     return ConstantExpr::getUIToFP(C, CI->getSrcTy(), true);
3661
3662   if (isa<FPToSIInst>(CI))
3663     return ConstantExpr::getSIToFP(C, CI->getSrcTy(), true);
3664
3665   if (isa<UIToFPInst>(CI))
3666     return ConstantExpr::getFPToUI(C, CI->getSrcTy(), true);
3667
3668   if (isa<SIToFPInst>(CI))
3669     return ConstantExpr::getFPToSI(C, CI->getSrcTy(), true);
3670
3671   if (isa<FPTruncInst>(CI))
3672     return ConstantExpr::getFPExtend(C, CI->getSrcTy(), true);
3673
3674   if (isa<FPExtInst>(CI))
3675     return ConstantExpr::getFPTrunc(C, CI->getSrcTy(), true);
3676
3677   return nullptr;
3678 }
3679
3680 SelectPatternResult llvm::matchSelectPattern(Value *V,
3681                                              Value *&LHS, Value *&RHS,
3682                                              Instruction::CastOps *CastOp) {
3683   SelectInst *SI = dyn_cast<SelectInst>(V);
3684   if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
3685
3686   CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
3687   if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
3688
3689   CmpInst::Predicate Pred = CmpI->getPredicate();
3690   Value *CmpLHS = CmpI->getOperand(0);
3691   Value *CmpRHS = CmpI->getOperand(1);
3692   Value *TrueVal = SI->getTrueValue();
3693   Value *FalseVal = SI->getFalseValue();
3694   FastMathFlags FMF;
3695   if (isa<FPMathOperator>(CmpI))
3696     FMF = CmpI->getFastMathFlags();
3697
3698   // Bail out early.
3699   if (CmpI->isEquality())
3700     return {SPF_UNKNOWN, SPNB_NA, false};
3701
3702   // Deal with type mismatches.
3703   if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
3704     if (Constant *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp))
3705       return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
3706                                   cast<CastInst>(TrueVal)->getOperand(0), C,
3707                                   LHS, RHS);
3708     if (Constant *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp))
3709       return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
3710                                   C, cast<CastInst>(FalseVal)->getOperand(0),
3711                                   LHS, RHS);
3712   }
3713   return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
3714                               LHS, RHS);
3715 }