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