1 //===- ValueTracking.cpp - Walk computations to compute properties --------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains routines that help analyze properties that chains of
10 // computations have.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Analysis/ValueTracking.h"
15 #include "llvm/ADT/APFloat.h"
16 #include "llvm/ADT/APInt.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/None.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/ADT/iterator_range.h"
26 #include "llvm/Analysis/AliasAnalysis.h"
27 #include "llvm/Analysis/AssumptionCache.h"
28 #include "llvm/Analysis/GuardUtils.h"
29 #include "llvm/Analysis/InstructionSimplify.h"
30 #include "llvm/Analysis/Loads.h"
31 #include "llvm/Analysis/LoopInfo.h"
32 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
33 #include "llvm/Analysis/TargetLibraryInfo.h"
34 #include "llvm/IR/Argument.h"
35 #include "llvm/IR/Attributes.h"
36 #include "llvm/IR/BasicBlock.h"
37 #include "llvm/IR/CallSite.h"
38 #include "llvm/IR/Constant.h"
39 #include "llvm/IR/ConstantRange.h"
40 #include "llvm/IR/Constants.h"
41 #include "llvm/IR/DerivedTypes.h"
42 #include "llvm/IR/DiagnosticInfo.h"
43 #include "llvm/IR/Dominators.h"
44 #include "llvm/IR/Function.h"
45 #include "llvm/IR/GetElementPtrTypeIterator.h"
46 #include "llvm/IR/GlobalAlias.h"
47 #include "llvm/IR/GlobalValue.h"
48 #include "llvm/IR/GlobalVariable.h"
49 #include "llvm/IR/InstrTypes.h"
50 #include "llvm/IR/Instruction.h"
51 #include "llvm/IR/Instructions.h"
52 #include "llvm/IR/IntrinsicInst.h"
53 #include "llvm/IR/Intrinsics.h"
54 #include "llvm/IR/IntrinsicsAArch64.h"
55 #include "llvm/IR/IntrinsicsX86.h"
56 #include "llvm/IR/LLVMContext.h"
57 #include "llvm/IR/Metadata.h"
58 #include "llvm/IR/Module.h"
59 #include "llvm/IR/Operator.h"
60 #include "llvm/IR/PatternMatch.h"
61 #include "llvm/IR/Type.h"
62 #include "llvm/IR/User.h"
63 #include "llvm/IR/Value.h"
64 #include "llvm/Support/Casting.h"
65 #include "llvm/Support/CommandLine.h"
66 #include "llvm/Support/Compiler.h"
67 #include "llvm/Support/ErrorHandling.h"
68 #include "llvm/Support/KnownBits.h"
69 #include "llvm/Support/MathExtras.h"
70 #include <algorithm>
71 #include <array>
72 #include <cassert>
73 #include <cstdint>
74 #include <iterator>
75 #include <utility>
76 
77 using namespace llvm;
78 using namespace llvm::PatternMatch;
79 
80 const unsigned MaxDepth = 6;
81 
82 // Controls the number of uses of the value searched for possible
83 // dominating comparisons.
84 static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
85                                               cl::Hidden, cl::init(20));
86 
87 /// Returns the bitwidth of the given scalar or pointer type. For vector types,
88 /// returns the element type's bitwidth.
89 static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
90   if (unsigned BitWidth = Ty->getScalarSizeInBits())
91     return BitWidth;
92 
93   return DL.getPointerTypeSizeInBits(Ty);
94 }
95 
96 namespace {
97 
98 // Simplifying using an assume can only be done in a particular control-flow
99 // context (the context instruction provides that context). If an assume and
100 // the context instruction are not in the same block then the DT helps in
101 // figuring out if we can use it.
102 struct Query {
103   const DataLayout &DL;
104   AssumptionCache *AC;
105   const Instruction *CxtI;
106   const DominatorTree *DT;
107 
108   // Unlike the other analyses, this may be a nullptr because not all clients
109   // provide it currently.
110   OptimizationRemarkEmitter *ORE;
111 
112   /// Set of assumptions that should be excluded from further queries.
113   /// This is because of the potential for mutual recursion to cause
114   /// computeKnownBits to repeatedly visit the same assume intrinsic. The
115   /// classic case of this is assume(x = y), which will attempt to determine
116   /// bits in x from bits in y, which will attempt to determine bits in y from
117   /// bits in x, etc. Regarding the mutual recursion, computeKnownBits can call
118   /// isKnownNonZero, which calls computeKnownBits and isKnownToBeAPowerOfTwo
119   /// (all of which can call computeKnownBits), and so on.
120   std::array<const Value *, MaxDepth> Excluded;
121 
122   /// If true, it is safe to use metadata during simplification.
123   InstrInfoQuery IIQ;
124 
125   unsigned NumExcluded = 0;
126 
127   Query(const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI,
128         const DominatorTree *DT, bool UseInstrInfo,
129         OptimizationRemarkEmitter *ORE = nullptr)
130       : DL(DL), AC(AC), CxtI(CxtI), DT(DT), ORE(ORE), IIQ(UseInstrInfo) {}
131 
132   Query(const Query &Q, const Value *NewExcl)
133       : DL(Q.DL), AC(Q.AC), CxtI(Q.CxtI), DT(Q.DT), ORE(Q.ORE), IIQ(Q.IIQ),
134         NumExcluded(Q.NumExcluded) {
135     Excluded = Q.Excluded;
136     Excluded[NumExcluded++] = NewExcl;
137     assert(NumExcluded <= Excluded.size());
138   }
139 
140   bool isExcluded(const Value *Value) const {
141     if (NumExcluded == 0)
142       return false;
143     auto End = Excluded.begin() + NumExcluded;
144     return std::find(Excluded.begin(), End, Value) != End;
145   }
146 };
147 
148 } // end anonymous namespace
149 
150 // Given the provided Value and, potentially, a context instruction, return
151 // the preferred context instruction (if any).
152 static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
153   // If we've been provided with a context instruction, then use that (provided
154   // it has been inserted).
155   if (CxtI && CxtI->getParent())
156     return CxtI;
157 
158   // If the value is really an already-inserted instruction, then use that.
159   CxtI = dyn_cast<Instruction>(V);
160   if (CxtI && CxtI->getParent())
161     return CxtI;
162 
163   return nullptr;
164 }
165 
166 static bool getShuffleDemandedElts(const ShuffleVectorInst *Shuf,
167                                    const APInt &DemandedElts,
168                                    APInt &DemandedLHS, APInt &DemandedRHS) {
169   // The length of scalable vectors is unknown at compile time, thus we
170   // cannot check their values
171   if (Shuf->getMask()->getType()->getVectorElementCount().Scalable)
172     return false;
173 
174   int NumElts = Shuf->getOperand(0)->getType()->getVectorNumElements();
175   int NumMaskElts = Shuf->getMask()->getType()->getVectorNumElements();
176   DemandedLHS = DemandedRHS = APInt::getNullValue(NumElts);
177 
178   for (int i = 0; i != NumMaskElts; ++i) {
179     if (!DemandedElts[i])
180       continue;
181     int M = Shuf->getMaskValue(i);
182     assert(M < (NumElts * 2) && "Invalid shuffle mask constant");
183 
184     // For undef elements, we don't know anything about the common state of
185     // the shuffle result.
186     if (M == -1)
187       return false;
188     if (M < NumElts)
189       DemandedLHS.setBit(M % NumElts);
190     else
191       DemandedRHS.setBit(M % NumElts);
192   }
193 
194   return true;
195 }
196 
197 static void computeKnownBits(const Value *V, const APInt &DemandedElts,
198                              KnownBits &Known, unsigned Depth, const Query &Q);
199 
200 static void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth,
201                              const Query &Q) {
202   Type *Ty = V->getType();
203   APInt DemandedElts = Ty->isVectorTy()
204                            ? APInt::getAllOnesValue(Ty->getVectorNumElements())
205                            : APInt(1, 1);
206   computeKnownBits(V, DemandedElts, Known, Depth, Q);
207 }
208 
209 void llvm::computeKnownBits(const Value *V, KnownBits &Known,
210                             const DataLayout &DL, unsigned Depth,
211                             AssumptionCache *AC, const Instruction *CxtI,
212                             const DominatorTree *DT,
213                             OptimizationRemarkEmitter *ORE, bool UseInstrInfo) {
214   ::computeKnownBits(V, Known, Depth,
215                      Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE));
216 }
217 
218 static KnownBits computeKnownBits(const Value *V, unsigned Depth,
219                                   const Query &Q);
220 
221 KnownBits llvm::computeKnownBits(const Value *V, const DataLayout &DL,
222                                  unsigned Depth, AssumptionCache *AC,
223                                  const Instruction *CxtI,
224                                  const DominatorTree *DT,
225                                  OptimizationRemarkEmitter *ORE,
226                                  bool UseInstrInfo) {
227   return ::computeKnownBits(
228       V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE));
229 }
230 
231 bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS,
232                                const DataLayout &DL, AssumptionCache *AC,
233                                const Instruction *CxtI, const DominatorTree *DT,
234                                bool UseInstrInfo) {
235   assert(LHS->getType() == RHS->getType() &&
236          "LHS and RHS should have the same type");
237   assert(LHS->getType()->isIntOrIntVectorTy() &&
238          "LHS and RHS should be integers");
239   // Look for an inverted mask: (X & ~M) op (Y & M).
240   Value *M;
241   if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
242       match(RHS, m_c_And(m_Specific(M), m_Value())))
243     return true;
244   if (match(RHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
245       match(LHS, m_c_And(m_Specific(M), m_Value())))
246     return true;
247   IntegerType *IT = cast<IntegerType>(LHS->getType()->getScalarType());
248   KnownBits LHSKnown(IT->getBitWidth());
249   KnownBits RHSKnown(IT->getBitWidth());
250   computeKnownBits(LHS, LHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo);
251   computeKnownBits(RHS, RHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo);
252   return (LHSKnown.Zero | RHSKnown.Zero).isAllOnesValue();
253 }
254 
255 bool llvm::isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI) {
256   for (const User *U : CxtI->users()) {
257     if (const ICmpInst *IC = dyn_cast<ICmpInst>(U))
258       if (IC->isEquality())
259         if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
260           if (C->isNullValue())
261             continue;
262     return false;
263   }
264   return true;
265 }
266 
267 static bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
268                                    const Query &Q);
269 
270 bool llvm::isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL,
271                                   bool OrZero, unsigned Depth,
272                                   AssumptionCache *AC, const Instruction *CxtI,
273                                   const DominatorTree *DT, bool UseInstrInfo) {
274   return ::isKnownToBeAPowerOfTwo(
275       V, OrZero, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
276 }
277 
278 static bool isKnownNonZero(const Value *V, unsigned Depth, const Query &Q);
279 
280 bool llvm::isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth,
281                           AssumptionCache *AC, const Instruction *CxtI,
282                           const DominatorTree *DT, bool UseInstrInfo) {
283   return ::isKnownNonZero(V, Depth,
284                           Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
285 }
286 
287 bool llvm::isKnownNonNegative(const Value *V, const DataLayout &DL,
288                               unsigned Depth, AssumptionCache *AC,
289                               const Instruction *CxtI, const DominatorTree *DT,
290                               bool UseInstrInfo) {
291   KnownBits Known =
292       computeKnownBits(V, DL, Depth, AC, CxtI, DT, nullptr, UseInstrInfo);
293   return Known.isNonNegative();
294 }
295 
296 bool llvm::isKnownPositive(const Value *V, const DataLayout &DL, unsigned Depth,
297                            AssumptionCache *AC, const Instruction *CxtI,
298                            const DominatorTree *DT, bool UseInstrInfo) {
299   if (auto *CI = dyn_cast<ConstantInt>(V))
300     return CI->getValue().isStrictlyPositive();
301 
302   // TODO: We'd doing two recursive queries here.  We should factor this such
303   // that only a single query is needed.
304   return isKnownNonNegative(V, DL, Depth, AC, CxtI, DT, UseInstrInfo) &&
305          isKnownNonZero(V, DL, Depth, AC, CxtI, DT, UseInstrInfo);
306 }
307 
308 bool llvm::isKnownNegative(const Value *V, const DataLayout &DL, unsigned Depth,
309                            AssumptionCache *AC, const Instruction *CxtI,
310                            const DominatorTree *DT, bool UseInstrInfo) {
311   KnownBits Known =
312       computeKnownBits(V, DL, Depth, AC, CxtI, DT, nullptr, UseInstrInfo);
313   return Known.isNegative();
314 }
315 
316 static bool isKnownNonEqual(const Value *V1, const Value *V2, const Query &Q);
317 
318 bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
319                            const DataLayout &DL, AssumptionCache *AC,
320                            const Instruction *CxtI, const DominatorTree *DT,
321                            bool UseInstrInfo) {
322   return ::isKnownNonEqual(V1, V2,
323                            Query(DL, AC, safeCxtI(V1, safeCxtI(V2, CxtI)), DT,
324                                  UseInstrInfo, /*ORE=*/nullptr));
325 }
326 
327 static bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth,
328                               const Query &Q);
329 
330 bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
331                              const DataLayout &DL, unsigned Depth,
332                              AssumptionCache *AC, const Instruction *CxtI,
333                              const DominatorTree *DT, bool UseInstrInfo) {
334   return ::MaskedValueIsZero(
335       V, Mask, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
336 }
337 
338 static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
339                                    unsigned Depth, const Query &Q);
340 
341 static unsigned ComputeNumSignBits(const Value *V, unsigned Depth,
342                                    const Query &Q) {
343   Type *Ty = V->getType();
344   APInt DemandedElts = Ty->isVectorTy()
345                            ? APInt::getAllOnesValue(Ty->getVectorNumElements())
346                            : APInt(1, 1);
347   return ComputeNumSignBits(V, DemandedElts, Depth, Q);
348 }
349 
350 unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
351                                   unsigned Depth, AssumptionCache *AC,
352                                   const Instruction *CxtI,
353                                   const DominatorTree *DT, bool UseInstrInfo) {
354   return ::ComputeNumSignBits(
355       V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
356 }
357 
358 static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
359                                    bool NSW,
360                                    KnownBits &KnownOut, KnownBits &Known2,
361                                    unsigned Depth, const Query &Q) {
362   unsigned BitWidth = KnownOut.getBitWidth();
363 
364   // If an initial sequence of bits in the result is not needed, the
365   // corresponding bits in the operands are not needed.
366   KnownBits LHSKnown(BitWidth);
367   computeKnownBits(Op0, LHSKnown, Depth + 1, Q);
368   computeKnownBits(Op1, Known2, Depth + 1, Q);
369 
370   KnownOut = KnownBits::computeForAddSub(Add, NSW, LHSKnown, Known2);
371 }
372 
373 static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
374                                 KnownBits &Known, KnownBits &Known2,
375                                 unsigned Depth, const Query &Q) {
376   unsigned BitWidth = Known.getBitWidth();
377   computeKnownBits(Op1, Known, Depth + 1, Q);
378   computeKnownBits(Op0, Known2, Depth + 1, Q);
379 
380   bool isKnownNegative = false;
381   bool isKnownNonNegative = false;
382   // If the multiplication is known not to overflow, compute the sign bit.
383   if (NSW) {
384     if (Op0 == Op1) {
385       // The product of a number with itself is non-negative.
386       isKnownNonNegative = true;
387     } else {
388       bool isKnownNonNegativeOp1 = Known.isNonNegative();
389       bool isKnownNonNegativeOp0 = Known2.isNonNegative();
390       bool isKnownNegativeOp1 = Known.isNegative();
391       bool isKnownNegativeOp0 = Known2.isNegative();
392       // The product of two numbers with the same sign is non-negative.
393       isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
394         (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
395       // The product of a negative number and a non-negative number is either
396       // negative or zero.
397       if (!isKnownNonNegative)
398         isKnownNegative = (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
399                            isKnownNonZero(Op0, Depth, Q)) ||
400                           (isKnownNegativeOp0 && isKnownNonNegativeOp1 &&
401                            isKnownNonZero(Op1, Depth, Q));
402     }
403   }
404 
405   assert(!Known.hasConflict() && !Known2.hasConflict());
406   // Compute a conservative estimate for high known-0 bits.
407   unsigned LeadZ =  std::max(Known.countMinLeadingZeros() +
408                              Known2.countMinLeadingZeros(),
409                              BitWidth) - BitWidth;
410   LeadZ = std::min(LeadZ, BitWidth);
411 
412   // The result of the bottom bits of an integer multiply can be
413   // inferred by looking at the bottom bits of both operands and
414   // multiplying them together.
415   // We can infer at least the minimum number of known trailing bits
416   // of both operands. Depending on number of trailing zeros, we can
417   // infer more bits, because (a*b) <=> ((a/m) * (b/n)) * (m*n) assuming
418   // a and b are divisible by m and n respectively.
419   // We then calculate how many of those bits are inferrable and set
420   // the output. For example, the i8 mul:
421   //  a = XXXX1100 (12)
422   //  b = XXXX1110 (14)
423   // We know the bottom 3 bits are zero since the first can be divided by
424   // 4 and the second by 2, thus having ((12/4) * (14/2)) * (2*4).
425   // Applying the multiplication to the trimmed arguments gets:
426   //    XX11 (3)
427   //    X111 (7)
428   // -------
429   //    XX11
430   //   XX11
431   //  XX11
432   // XX11
433   // -------
434   // XXXXX01
435   // Which allows us to infer the 2 LSBs. Since we're multiplying the result
436   // by 8, the bottom 3 bits will be 0, so we can infer a total of 5 bits.
437   // The proof for this can be described as:
438   // Pre: (C1 >= 0) && (C1 < (1 << C5)) && (C2 >= 0) && (C2 < (1 << C6)) &&
439   //      (C7 == (1 << (umin(countTrailingZeros(C1), C5) +
440   //                    umin(countTrailingZeros(C2), C6) +
441   //                    umin(C5 - umin(countTrailingZeros(C1), C5),
442   //                         C6 - umin(countTrailingZeros(C2), C6)))) - 1)
443   // %aa = shl i8 %a, C5
444   // %bb = shl i8 %b, C6
445   // %aaa = or i8 %aa, C1
446   // %bbb = or i8 %bb, C2
447   // %mul = mul i8 %aaa, %bbb
448   // %mask = and i8 %mul, C7
449   //   =>
450   // %mask = i8 ((C1*C2)&C7)
451   // Where C5, C6 describe the known bits of %a, %b
452   // C1, C2 describe the known bottom bits of %a, %b.
453   // C7 describes the mask of the known bits of the result.
454   APInt Bottom0 = Known.One;
455   APInt Bottom1 = Known2.One;
456 
457   // How many times we'd be able to divide each argument by 2 (shr by 1).
458   // This gives us the number of trailing zeros on the multiplication result.
459   unsigned TrailBitsKnown0 = (Known.Zero | Known.One).countTrailingOnes();
460   unsigned TrailBitsKnown1 = (Known2.Zero | Known2.One).countTrailingOnes();
461   unsigned TrailZero0 = Known.countMinTrailingZeros();
462   unsigned TrailZero1 = Known2.countMinTrailingZeros();
463   unsigned TrailZ = TrailZero0 + TrailZero1;
464 
465   // Figure out the fewest known-bits operand.
466   unsigned SmallestOperand = std::min(TrailBitsKnown0 - TrailZero0,
467                                       TrailBitsKnown1 - TrailZero1);
468   unsigned ResultBitsKnown = std::min(SmallestOperand + TrailZ, BitWidth);
469 
470   APInt BottomKnown = Bottom0.getLoBits(TrailBitsKnown0) *
471                       Bottom1.getLoBits(TrailBitsKnown1);
472 
473   Known.resetAll();
474   Known.Zero.setHighBits(LeadZ);
475   Known.Zero |= (~BottomKnown).getLoBits(ResultBitsKnown);
476   Known.One |= BottomKnown.getLoBits(ResultBitsKnown);
477 
478   // Only make use of no-wrap flags if we failed to compute the sign bit
479   // directly.  This matters if the multiplication always overflows, in
480   // which case we prefer to follow the result of the direct computation,
481   // though as the program is invoking undefined behaviour we can choose
482   // whatever we like here.
483   if (isKnownNonNegative && !Known.isNegative())
484     Known.makeNonNegative();
485   else if (isKnownNegative && !Known.isNonNegative())
486     Known.makeNegative();
487 }
488 
489 void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
490                                              KnownBits &Known) {
491   unsigned BitWidth = Known.getBitWidth();
492   unsigned NumRanges = Ranges.getNumOperands() / 2;
493   assert(NumRanges >= 1);
494 
495   Known.Zero.setAllBits();
496   Known.One.setAllBits();
497 
498   for (unsigned i = 0; i < NumRanges; ++i) {
499     ConstantInt *Lower =
500         mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
501     ConstantInt *Upper =
502         mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
503     ConstantRange Range(Lower->getValue(), Upper->getValue());
504 
505     // The first CommonPrefixBits of all values in Range are equal.
506     unsigned CommonPrefixBits =
507         (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countLeadingZeros();
508 
509     APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
510     Known.One &= Range.getUnsignedMax() & Mask;
511     Known.Zero &= ~Range.getUnsignedMax() & Mask;
512   }
513 }
514 
515 static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
516   SmallVector<const Value *, 16> WorkSet(1, I);
517   SmallPtrSet<const Value *, 32> Visited;
518   SmallPtrSet<const Value *, 16> EphValues;
519 
520   // The instruction defining an assumption's condition itself is always
521   // considered ephemeral to that assumption (even if it has other
522   // non-ephemeral users). See r246696's test case for an example.
523   if (is_contained(I->operands(), E))
524     return true;
525 
526   while (!WorkSet.empty()) {
527     const Value *V = WorkSet.pop_back_val();
528     if (!Visited.insert(V).second)
529       continue;
530 
531     // If all uses of this value are ephemeral, then so is this value.
532     if (llvm::all_of(V->users(), [&](const User *U) {
533                                    return EphValues.count(U);
534                                  })) {
535       if (V == E)
536         return true;
537 
538       if (V == I || isSafeToSpeculativelyExecute(V)) {
539        EphValues.insert(V);
540        if (const User *U = dyn_cast<User>(V))
541          for (User::const_op_iterator J = U->op_begin(), JE = U->op_end();
542               J != JE; ++J)
543            WorkSet.push_back(*J);
544       }
545     }
546   }
547 
548   return false;
549 }
550 
551 // Is this an intrinsic that cannot be speculated but also cannot trap?
552 bool llvm::isAssumeLikeIntrinsic(const Instruction *I) {
553   if (const CallInst *CI = dyn_cast<CallInst>(I))
554     if (Function *F = CI->getCalledFunction())
555       switch (F->getIntrinsicID()) {
556       default: break;
557       // FIXME: This list is repeated from NoTTI::getIntrinsicCost.
558       case Intrinsic::assume:
559       case Intrinsic::sideeffect:
560       case Intrinsic::dbg_declare:
561       case Intrinsic::dbg_value:
562       case Intrinsic::dbg_label:
563       case Intrinsic::invariant_start:
564       case Intrinsic::invariant_end:
565       case Intrinsic::lifetime_start:
566       case Intrinsic::lifetime_end:
567       case Intrinsic::objectsize:
568       case Intrinsic::ptr_annotation:
569       case Intrinsic::var_annotation:
570         return true;
571       }
572 
573   return false;
574 }
575 
576 bool llvm::isValidAssumeForContext(const Instruction *Inv,
577                                    const Instruction *CxtI,
578                                    const DominatorTree *DT) {
579   // There are two restrictions on the use of an assume:
580   //  1. The assume must dominate the context (or the control flow must
581   //     reach the assume whenever it reaches the context).
582   //  2. The context must not be in the assume's set of ephemeral values
583   //     (otherwise we will use the assume to prove that the condition
584   //     feeding the assume is trivially true, thus causing the removal of
585   //     the assume).
586 
587   if (DT) {
588     if (DT->dominates(Inv, CxtI))
589       return true;
590   } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor()) {
591     // We don't have a DT, but this trivially dominates.
592     return true;
593   }
594 
595   // With or without a DT, the only remaining case we will check is if the
596   // instructions are in the same BB.  Give up if that is not the case.
597   if (Inv->getParent() != CxtI->getParent())
598     return false;
599 
600   // If we have a dom tree, then we now know that the assume doesn't dominate
601   // the other instruction.  If we don't have a dom tree then we can check if
602   // the assume is first in the BB.
603   if (!DT) {
604     // Search forward from the assume until we reach the context (or the end
605     // of the block); the common case is that the assume will come first.
606     for (auto I = std::next(BasicBlock::const_iterator(Inv)),
607          IE = Inv->getParent()->end(); I != IE; ++I)
608       if (&*I == CxtI)
609         return true;
610   }
611 
612   // Don't let an assume affect itself - this would cause the problems
613   // `isEphemeralValueOf` is trying to prevent, and it would also make
614   // the loop below go out of bounds.
615   if (Inv == CxtI)
616     return false;
617 
618   // The context comes first, but they're both in the same block.
619   // Make sure there is nothing in between that might interrupt
620   // the control flow, not even CxtI itself.
621   for (BasicBlock::const_iterator I(CxtI), IE(Inv); I != IE; ++I)
622     if (!isGuaranteedToTransferExecutionToSuccessor(&*I))
623       return false;
624 
625   return !isEphemeralValueOf(Inv, CxtI);
626 }
627 
628 static bool isKnownNonZeroFromAssume(const Value *V, const Query &Q) {
629   // Use of assumptions is context-sensitive. If we don't have a context, we
630   // cannot use them!
631   if (!Q.AC || !Q.CxtI)
632     return false;
633 
634   // Note that the patterns below need to be kept in sync with the code
635   // in AssumptionCache::updateAffectedValues.
636 
637   auto CmpExcludesZero = [V](ICmpInst *Cmp) {
638     auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
639 
640     Value *RHS;
641     CmpInst::Predicate Pred;
642     if (!match(Cmp, m_c_ICmp(Pred, m_V, m_Value(RHS))))
643       return false;
644     // assume(v u> y) -> assume(v != 0)
645     if (Pred == ICmpInst::ICMP_UGT)
646       return true;
647 
648     // assume(v != 0)
649     // We special-case this one to ensure that we handle `assume(v != null)`.
650     if (Pred == ICmpInst::ICMP_NE)
651       return match(RHS, m_Zero());
652 
653     // All other predicates - rely on generic ConstantRange handling.
654     ConstantInt *CI;
655     if (!match(RHS, m_ConstantInt(CI)))
656       return false;
657     ConstantRange RHSRange(CI->getValue());
658     ConstantRange TrueValues =
659         ConstantRange::makeAllowedICmpRegion(Pred, RHSRange);
660     return !TrueValues.contains(APInt::getNullValue(CI->getBitWidth()));
661   };
662 
663   for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
664     if (!AssumeVH)
665       continue;
666     CallInst *I = cast<CallInst>(AssumeVH);
667     assert(I->getFunction() == Q.CxtI->getFunction() &&
668            "Got assumption for the wrong function!");
669     if (Q.isExcluded(I))
670       continue;
671 
672     // Warning: This loop can end up being somewhat performance sensitive.
673     // We're running this loop for once for each value queried resulting in a
674     // runtime of ~O(#assumes * #values).
675 
676     assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&
677            "must be an assume intrinsic");
678 
679     Value *Arg = I->getArgOperand(0);
680     ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
681     if (!Cmp)
682       continue;
683 
684     if (CmpExcludesZero(Cmp) && isValidAssumeForContext(I, Q.CxtI, Q.DT))
685       return true;
686   }
687 
688   return false;
689 }
690 
691 static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
692                                        unsigned Depth, const Query &Q) {
693   // Use of assumptions is context-sensitive. If we don't have a context, we
694   // cannot use them!
695   if (!Q.AC || !Q.CxtI)
696     return;
697 
698   unsigned BitWidth = Known.getBitWidth();
699 
700   // Note that the patterns below need to be kept in sync with the code
701   // in AssumptionCache::updateAffectedValues.
702 
703   for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
704     if (!AssumeVH)
705       continue;
706     CallInst *I = cast<CallInst>(AssumeVH);
707     assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
708            "Got assumption for the wrong function!");
709     if (Q.isExcluded(I))
710       continue;
711 
712     // Warning: This loop can end up being somewhat performance sensitive.
713     // We're running this loop for once for each value queried resulting in a
714     // runtime of ~O(#assumes * #values).
715 
716     assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&
717            "must be an assume intrinsic");
718 
719     Value *Arg = I->getArgOperand(0);
720 
721     if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
722       assert(BitWidth == 1 && "assume operand is not i1?");
723       Known.setAllOnes();
724       return;
725     }
726     if (match(Arg, m_Not(m_Specific(V))) &&
727         isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
728       assert(BitWidth == 1 && "assume operand is not i1?");
729       Known.setAllZero();
730       return;
731     }
732 
733     // The remaining tests are all recursive, so bail out if we hit the limit.
734     if (Depth == MaxDepth)
735       continue;
736 
737     ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
738     if (!Cmp)
739       continue;
740 
741     Value *A, *B;
742     auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
743 
744     CmpInst::Predicate Pred;
745     uint64_t C;
746     switch (Cmp->getPredicate()) {
747     default:
748       break;
749     case ICmpInst::ICMP_EQ:
750       // assume(v = a)
751       if (match(Cmp, m_c_ICmp(Pred, m_V, m_Value(A))) &&
752           isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
753         KnownBits RHSKnown(BitWidth);
754         computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
755         Known.Zero |= RHSKnown.Zero;
756         Known.One  |= RHSKnown.One;
757       // assume(v & b = a)
758       } else if (match(Cmp,
759                        m_c_ICmp(Pred, m_c_And(m_V, m_Value(B)), m_Value(A))) &&
760                  isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
761         KnownBits RHSKnown(BitWidth);
762         computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
763         KnownBits MaskKnown(BitWidth);
764         computeKnownBits(B, MaskKnown, Depth+1, Query(Q, I));
765 
766         // For those bits in the mask that are known to be one, we can propagate
767         // known bits from the RHS to V.
768         Known.Zero |= RHSKnown.Zero & MaskKnown.One;
769         Known.One  |= RHSKnown.One  & MaskKnown.One;
770       // assume(~(v & b) = a)
771       } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_And(m_V, m_Value(B))),
772                                      m_Value(A))) &&
773                  isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
774         KnownBits RHSKnown(BitWidth);
775         computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
776         KnownBits MaskKnown(BitWidth);
777         computeKnownBits(B, MaskKnown, Depth+1, Query(Q, I));
778 
779         // For those bits in the mask that are known to be one, we can propagate
780         // inverted known bits from the RHS to V.
781         Known.Zero |= RHSKnown.One  & MaskKnown.One;
782         Known.One  |= RHSKnown.Zero & MaskKnown.One;
783       // assume(v | b = a)
784       } else if (match(Cmp,
785                        m_c_ICmp(Pred, m_c_Or(m_V, m_Value(B)), m_Value(A))) &&
786                  isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
787         KnownBits RHSKnown(BitWidth);
788         computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
789         KnownBits BKnown(BitWidth);
790         computeKnownBits(B, BKnown, Depth+1, Query(Q, I));
791 
792         // For those bits in B that are known to be zero, we can propagate known
793         // bits from the RHS to V.
794         Known.Zero |= RHSKnown.Zero & BKnown.Zero;
795         Known.One  |= RHSKnown.One  & BKnown.Zero;
796       // assume(~(v | b) = a)
797       } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_Or(m_V, m_Value(B))),
798                                      m_Value(A))) &&
799                  isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
800         KnownBits RHSKnown(BitWidth);
801         computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
802         KnownBits BKnown(BitWidth);
803         computeKnownBits(B, BKnown, Depth+1, Query(Q, I));
804 
805         // For those bits in B that are known to be zero, we can propagate
806         // inverted known bits from the RHS to V.
807         Known.Zero |= RHSKnown.One  & BKnown.Zero;
808         Known.One  |= RHSKnown.Zero & BKnown.Zero;
809       // assume(v ^ b = a)
810       } else if (match(Cmp,
811                        m_c_ICmp(Pred, m_c_Xor(m_V, m_Value(B)), m_Value(A))) &&
812                  isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
813         KnownBits RHSKnown(BitWidth);
814         computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
815         KnownBits BKnown(BitWidth);
816         computeKnownBits(B, BKnown, Depth+1, Query(Q, I));
817 
818         // For those bits in B that are known to be zero, we can propagate known
819         // bits from the RHS to V. For those bits in B that are known to be one,
820         // we can propagate inverted known bits from the RHS to V.
821         Known.Zero |= RHSKnown.Zero & BKnown.Zero;
822         Known.One  |= RHSKnown.One  & BKnown.Zero;
823         Known.Zero |= RHSKnown.One  & BKnown.One;
824         Known.One  |= RHSKnown.Zero & BKnown.One;
825       // assume(~(v ^ b) = a)
826       } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_Xor(m_V, m_Value(B))),
827                                      m_Value(A))) &&
828                  isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
829         KnownBits RHSKnown(BitWidth);
830         computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
831         KnownBits BKnown(BitWidth);
832         computeKnownBits(B, BKnown, Depth+1, Query(Q, I));
833 
834         // For those bits in B that are known to be zero, we can propagate
835         // inverted known bits from the RHS to V. For those bits in B that are
836         // known to be one, we can propagate known bits from the RHS to V.
837         Known.Zero |= RHSKnown.One  & BKnown.Zero;
838         Known.One  |= RHSKnown.Zero & BKnown.Zero;
839         Known.Zero |= RHSKnown.Zero & BKnown.One;
840         Known.One  |= RHSKnown.One  & BKnown.One;
841       // assume(v << c = a)
842       } else if (match(Cmp, m_c_ICmp(Pred, m_Shl(m_V, m_ConstantInt(C)),
843                                      m_Value(A))) &&
844                  isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) {
845         KnownBits RHSKnown(BitWidth);
846         computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
847         // For those bits in RHS that are known, we can propagate them to known
848         // bits in V shifted to the right by C.
849         RHSKnown.Zero.lshrInPlace(C);
850         Known.Zero |= RHSKnown.Zero;
851         RHSKnown.One.lshrInPlace(C);
852         Known.One  |= RHSKnown.One;
853       // assume(~(v << c) = a)
854       } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_Shl(m_V, m_ConstantInt(C))),
855                                      m_Value(A))) &&
856                  isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) {
857         KnownBits RHSKnown(BitWidth);
858         computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
859         // For those bits in RHS that are known, we can propagate them inverted
860         // to known bits in V shifted to the right by C.
861         RHSKnown.One.lshrInPlace(C);
862         Known.Zero |= RHSKnown.One;
863         RHSKnown.Zero.lshrInPlace(C);
864         Known.One  |= RHSKnown.Zero;
865       // assume(v >> c = a)
866       } else if (match(Cmp, m_c_ICmp(Pred, m_Shr(m_V, m_ConstantInt(C)),
867                                      m_Value(A))) &&
868                  isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) {
869         KnownBits RHSKnown(BitWidth);
870         computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
871         // For those bits in RHS that are known, we can propagate them to known
872         // bits in V shifted to the right by C.
873         Known.Zero |= RHSKnown.Zero << C;
874         Known.One  |= RHSKnown.One  << C;
875       // assume(~(v >> c) = a)
876       } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_Shr(m_V, m_ConstantInt(C))),
877                                      m_Value(A))) &&
878                  isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) {
879         KnownBits RHSKnown(BitWidth);
880         computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
881         // For those bits in RHS that are known, we can propagate them inverted
882         // to known bits in V shifted to the right by C.
883         Known.Zero |= RHSKnown.One  << C;
884         Known.One  |= RHSKnown.Zero << C;
885       }
886       break;
887     case ICmpInst::ICMP_SGE:
888       // assume(v >=_s c) where c is non-negative
889       if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) &&
890           isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
891         KnownBits RHSKnown(BitWidth);
892         computeKnownBits(A, RHSKnown, Depth + 1, Query(Q, I));
893 
894         if (RHSKnown.isNonNegative()) {
895           // We know that the sign bit is zero.
896           Known.makeNonNegative();
897         }
898       }
899       break;
900     case ICmpInst::ICMP_SGT:
901       // assume(v >_s c) where c is at least -1.
902       if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) &&
903           isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
904         KnownBits RHSKnown(BitWidth);
905         computeKnownBits(A, RHSKnown, Depth + 1, Query(Q, I));
906 
907         if (RHSKnown.isAllOnes() || RHSKnown.isNonNegative()) {
908           // We know that the sign bit is zero.
909           Known.makeNonNegative();
910         }
911       }
912       break;
913     case ICmpInst::ICMP_SLE:
914       // assume(v <=_s c) where c is negative
915       if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) &&
916           isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
917         KnownBits RHSKnown(BitWidth);
918         computeKnownBits(A, RHSKnown, Depth + 1, Query(Q, I));
919 
920         if (RHSKnown.isNegative()) {
921           // We know that the sign bit is one.
922           Known.makeNegative();
923         }
924       }
925       break;
926     case ICmpInst::ICMP_SLT:
927       // assume(v <_s c) where c is non-positive
928       if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) &&
929           isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
930         KnownBits RHSKnown(BitWidth);
931         computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
932 
933         if (RHSKnown.isZero() || RHSKnown.isNegative()) {
934           // We know that the sign bit is one.
935           Known.makeNegative();
936         }
937       }
938       break;
939     case ICmpInst::ICMP_ULE:
940       // assume(v <=_u c)
941       if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) &&
942           isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
943         KnownBits RHSKnown(BitWidth);
944         computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
945 
946         // Whatever high bits in c are zero are known to be zero.
947         Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros());
948       }
949       break;
950     case ICmpInst::ICMP_ULT:
951       // assume(v <_u c)
952       if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) &&
953           isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
954         KnownBits RHSKnown(BitWidth);
955         computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
956 
957         // If the RHS is known zero, then this assumption must be wrong (nothing
958         // is unsigned less than zero). Signal a conflict and get out of here.
959         if (RHSKnown.isZero()) {
960           Known.Zero.setAllBits();
961           Known.One.setAllBits();
962           break;
963         }
964 
965         // Whatever high bits in c are zero are known to be zero (if c is a power
966         // of 2, then one more).
967         if (isKnownToBeAPowerOfTwo(A, false, Depth + 1, Query(Q, I)))
968           Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros() + 1);
969         else
970           Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros());
971       }
972       break;
973     }
974   }
975 
976   // If assumptions conflict with each other or previous known bits, then we
977   // have a logical fallacy. It's possible that the assumption is not reachable,
978   // so this isn't a real bug. On the other hand, the program may have undefined
979   // behavior, or we might have a bug in the compiler. We can't assert/crash, so
980   // clear out the known bits, try to warn the user, and hope for the best.
981   if (Known.Zero.intersects(Known.One)) {
982     Known.resetAll();
983 
984     if (Q.ORE)
985       Q.ORE->emit([&]() {
986         auto *CxtI = const_cast<Instruction *>(Q.CxtI);
987         return OptimizationRemarkAnalysis("value-tracking", "BadAssumption",
988                                           CxtI)
989                << "Detected conflicting code assumptions. Program may "
990                   "have undefined behavior, or compiler may have "
991                   "internal error.";
992       });
993   }
994 }
995 
996 /// Compute known bits from a shift operator, including those with a
997 /// non-constant shift amount. Known is the output of this function. Known2 is a
998 /// pre-allocated temporary with the same bit width as Known. KZF and KOF are
999 /// operator-specific functions that, given the known-zero or known-one bits
1000 /// respectively, and a shift amount, compute the implied known-zero or
1001 /// known-one bits of the shift operator's result respectively for that shift
1002 /// amount. The results from calling KZF and KOF are conservatively combined for
1003 /// all permitted shift amounts.
1004 static void computeKnownBitsFromShiftOperator(
1005     const Operator *I, KnownBits &Known, KnownBits &Known2,
1006     unsigned Depth, const Query &Q,
1007     function_ref<APInt(const APInt &, unsigned)> KZF,
1008     function_ref<APInt(const APInt &, unsigned)> KOF) {
1009   unsigned BitWidth = Known.getBitWidth();
1010 
1011   if (auto *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
1012     unsigned ShiftAmt = SA->getLimitedValue(BitWidth-1);
1013 
1014     computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1015     Known.Zero = KZF(Known.Zero, ShiftAmt);
1016     Known.One  = KOF(Known.One, ShiftAmt);
1017     // If the known bits conflict, this must be an overflowing left shift, so
1018     // the shift result is poison. We can return anything we want. Choose 0 for
1019     // the best folding opportunity.
1020     if (Known.hasConflict())
1021       Known.setAllZero();
1022 
1023     return;
1024   }
1025 
1026   computeKnownBits(I->getOperand(1), Known, Depth + 1, Q);
1027 
1028   // If the shift amount could be greater than or equal to the bit-width of the
1029   // LHS, the value could be poison, but bail out because the check below is
1030   // expensive. TODO: Should we just carry on?
1031   if (Known.getMaxValue().uge(BitWidth)) {
1032     Known.resetAll();
1033     return;
1034   }
1035 
1036   // Note: We cannot use Known.Zero.getLimitedValue() here, because if
1037   // BitWidth > 64 and any upper bits are known, we'll end up returning the
1038   // limit value (which implies all bits are known).
1039   uint64_t ShiftAmtKZ = Known.Zero.zextOrTrunc(64).getZExtValue();
1040   uint64_t ShiftAmtKO = Known.One.zextOrTrunc(64).getZExtValue();
1041 
1042   // It would be more-clearly correct to use the two temporaries for this
1043   // calculation. Reusing the APInts here to prevent unnecessary allocations.
1044   Known.resetAll();
1045 
1046   // If we know the shifter operand is nonzero, we can sometimes infer more
1047   // known bits. However this is expensive to compute, so be lazy about it and
1048   // only compute it when absolutely necessary.
1049   Optional<bool> ShifterOperandIsNonZero;
1050 
1051   // Early exit if we can't constrain any well-defined shift amount.
1052   if (!(ShiftAmtKZ & (PowerOf2Ceil(BitWidth) - 1)) &&
1053       !(ShiftAmtKO & (PowerOf2Ceil(BitWidth) - 1))) {
1054     ShifterOperandIsNonZero = isKnownNonZero(I->getOperand(1), Depth + 1, Q);
1055     if (!*ShifterOperandIsNonZero)
1056       return;
1057   }
1058 
1059   computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1060 
1061   Known.Zero.setAllBits();
1062   Known.One.setAllBits();
1063   for (unsigned ShiftAmt = 0; ShiftAmt < BitWidth; ++ShiftAmt) {
1064     // Combine the shifted known input bits only for those shift amounts
1065     // compatible with its known constraints.
1066     if ((ShiftAmt & ~ShiftAmtKZ) != ShiftAmt)
1067       continue;
1068     if ((ShiftAmt | ShiftAmtKO) != ShiftAmt)
1069       continue;
1070     // If we know the shifter is nonzero, we may be able to infer more known
1071     // bits. This check is sunk down as far as possible to avoid the expensive
1072     // call to isKnownNonZero if the cheaper checks above fail.
1073     if (ShiftAmt == 0) {
1074       if (!ShifterOperandIsNonZero.hasValue())
1075         ShifterOperandIsNonZero =
1076             isKnownNonZero(I->getOperand(1), Depth + 1, Q);
1077       if (*ShifterOperandIsNonZero)
1078         continue;
1079     }
1080 
1081     Known.Zero &= KZF(Known2.Zero, ShiftAmt);
1082     Known.One  &= KOF(Known2.One, ShiftAmt);
1083   }
1084 
1085   // If the known bits conflict, the result is poison. Return a 0 and hope the
1086   // caller can further optimize that.
1087   if (Known.hasConflict())
1088     Known.setAllZero();
1089 }
1090 
1091 static void computeKnownBitsFromOperator(const Operator *I,
1092                                          const APInt &DemandedElts,
1093                                          KnownBits &Known, unsigned Depth,
1094                                          const Query &Q) {
1095   unsigned BitWidth = Known.getBitWidth();
1096 
1097   KnownBits Known2(Known);
1098   switch (I->getOpcode()) {
1099   default: break;
1100   case Instruction::Load:
1101     if (MDNode *MD =
1102             Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1103       computeKnownBitsFromRangeMetadata(*MD, Known);
1104     break;
1105   case Instruction::And: {
1106     // If either the LHS or the RHS are Zero, the result is zero.
1107     computeKnownBits(I->getOperand(1), Known, Depth + 1, Q);
1108     computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1109 
1110     // Output known-1 bits are only known if set in both the LHS & RHS.
1111     Known.One &= Known2.One;
1112     // Output known-0 are known to be clear if zero in either the LHS | RHS.
1113     Known.Zero |= Known2.Zero;
1114 
1115     // and(x, add (x, -1)) is a common idiom that always clears the low bit;
1116     // here we handle the more general case of adding any odd number by
1117     // matching the form add(x, add(x, y)) where y is odd.
1118     // TODO: This could be generalized to clearing any bit set in y where the
1119     // following bit is known to be unset in y.
1120     Value *X = nullptr, *Y = nullptr;
1121     if (!Known.Zero[0] && !Known.One[0] &&
1122         match(I, m_c_BinOp(m_Value(X), m_Add(m_Deferred(X), m_Value(Y))))) {
1123       Known2.resetAll();
1124       computeKnownBits(Y, Known2, Depth + 1, Q);
1125       if (Known2.countMinTrailingOnes() > 0)
1126         Known.Zero.setBit(0);
1127     }
1128     break;
1129   }
1130   case Instruction::Or:
1131     computeKnownBits(I->getOperand(1), Known, Depth + 1, Q);
1132     computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1133 
1134     // Output known-0 bits are only known if clear in both the LHS & RHS.
1135     Known.Zero &= Known2.Zero;
1136     // Output known-1 are known to be set if set in either the LHS | RHS.
1137     Known.One |= Known2.One;
1138     break;
1139   case Instruction::Xor: {
1140     computeKnownBits(I->getOperand(1), Known, Depth + 1, Q);
1141     computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1142 
1143     // Output known-0 bits are known if clear or set in both the LHS & RHS.
1144     APInt KnownZeroOut = (Known.Zero & Known2.Zero) | (Known.One & Known2.One);
1145     // Output known-1 are known to be set if set in only one of the LHS, RHS.
1146     Known.One = (Known.Zero & Known2.One) | (Known.One & Known2.Zero);
1147     Known.Zero = std::move(KnownZeroOut);
1148     break;
1149   }
1150   case Instruction::Mul: {
1151     bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1152     computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, Known,
1153                         Known2, Depth, Q);
1154     break;
1155   }
1156   case Instruction::UDiv: {
1157     // For the purposes of computing leading zeros we can conservatively
1158     // treat a udiv as a logical right shift by the power of 2 known to
1159     // be less than the denominator.
1160     computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1161     unsigned LeadZ = Known2.countMinLeadingZeros();
1162 
1163     Known2.resetAll();
1164     computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1165     unsigned RHSMaxLeadingZeros = Known2.countMaxLeadingZeros();
1166     if (RHSMaxLeadingZeros != BitWidth)
1167       LeadZ = std::min(BitWidth, LeadZ + BitWidth - RHSMaxLeadingZeros - 1);
1168 
1169     Known.Zero.setHighBits(LeadZ);
1170     break;
1171   }
1172   case Instruction::Select: {
1173     const Value *LHS = nullptr, *RHS = nullptr;
1174     SelectPatternFlavor SPF = matchSelectPattern(I, LHS, RHS).Flavor;
1175     if (SelectPatternResult::isMinOrMax(SPF)) {
1176       computeKnownBits(RHS, Known, Depth + 1, Q);
1177       computeKnownBits(LHS, Known2, Depth + 1, Q);
1178     } else {
1179       computeKnownBits(I->getOperand(2), Known, Depth + 1, Q);
1180       computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1181     }
1182 
1183     unsigned MaxHighOnes = 0;
1184     unsigned MaxHighZeros = 0;
1185     if (SPF == SPF_SMAX) {
1186       // If both sides are negative, the result is negative.
1187       if (Known.isNegative() && Known2.isNegative())
1188         // We can derive a lower bound on the result by taking the max of the
1189         // leading one bits.
1190         MaxHighOnes =
1191             std::max(Known.countMinLeadingOnes(), Known2.countMinLeadingOnes());
1192       // If either side is non-negative, the result is non-negative.
1193       else if (Known.isNonNegative() || Known2.isNonNegative())
1194         MaxHighZeros = 1;
1195     } else if (SPF == SPF_SMIN) {
1196       // If both sides are non-negative, the result is non-negative.
1197       if (Known.isNonNegative() && Known2.isNonNegative())
1198         // We can derive an upper bound on the result by taking the max of the
1199         // leading zero bits.
1200         MaxHighZeros = std::max(Known.countMinLeadingZeros(),
1201                                 Known2.countMinLeadingZeros());
1202       // If either side is negative, the result is negative.
1203       else if (Known.isNegative() || Known2.isNegative())
1204         MaxHighOnes = 1;
1205     } else if (SPF == SPF_UMAX) {
1206       // We can derive a lower bound on the result by taking the max of the
1207       // leading one bits.
1208       MaxHighOnes =
1209           std::max(Known.countMinLeadingOnes(), Known2.countMinLeadingOnes());
1210     } else if (SPF == SPF_UMIN) {
1211       // We can derive an upper bound on the result by taking the max of the
1212       // leading zero bits.
1213       MaxHighZeros =
1214           std::max(Known.countMinLeadingZeros(), Known2.countMinLeadingZeros());
1215     } else if (SPF == SPF_ABS) {
1216       // RHS from matchSelectPattern returns the negation part of abs pattern.
1217       // If the negate has an NSW flag we can assume the sign bit of the result
1218       // will be 0 because that makes abs(INT_MIN) undefined.
1219       if (match(RHS, m_Neg(m_Specific(LHS))) &&
1220           Q.IIQ.hasNoSignedWrap(cast<Instruction>(RHS)))
1221         MaxHighZeros = 1;
1222     }
1223 
1224     // Only known if known in both the LHS and RHS.
1225     Known.One &= Known2.One;
1226     Known.Zero &= Known2.Zero;
1227     if (MaxHighOnes > 0)
1228       Known.One.setHighBits(MaxHighOnes);
1229     if (MaxHighZeros > 0)
1230       Known.Zero.setHighBits(MaxHighZeros);
1231     break;
1232   }
1233   case Instruction::FPTrunc:
1234   case Instruction::FPExt:
1235   case Instruction::FPToUI:
1236   case Instruction::FPToSI:
1237   case Instruction::SIToFP:
1238   case Instruction::UIToFP:
1239     break; // Can't work with floating point.
1240   case Instruction::PtrToInt:
1241   case Instruction::IntToPtr:
1242     // Fall through and handle them the same as zext/trunc.
1243     LLVM_FALLTHROUGH;
1244   case Instruction::ZExt:
1245   case Instruction::Trunc: {
1246     Type *SrcTy = I->getOperand(0)->getType();
1247 
1248     unsigned SrcBitWidth;
1249     // Note that we handle pointer operands here because of inttoptr/ptrtoint
1250     // which fall through here.
1251     Type *ScalarTy = SrcTy->getScalarType();
1252     SrcBitWidth = ScalarTy->isPointerTy() ?
1253       Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1254       Q.DL.getTypeSizeInBits(ScalarTy);
1255 
1256     assert(SrcBitWidth && "SrcBitWidth can't be zero");
1257     Known = Known.anyextOrTrunc(SrcBitWidth);
1258     computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1259     Known = Known.zextOrTrunc(BitWidth);
1260     break;
1261   }
1262   case Instruction::BitCast: {
1263     Type *SrcTy = I->getOperand(0)->getType();
1264     if (SrcTy->isIntOrPtrTy() &&
1265         // TODO: For now, not handling conversions like:
1266         // (bitcast i64 %x to <2 x i32>)
1267         !I->getType()->isVectorTy()) {
1268       computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1269       break;
1270     }
1271     break;
1272   }
1273   case Instruction::SExt: {
1274     // Compute the bits in the result that are not present in the input.
1275     unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1276 
1277     Known = Known.trunc(SrcBitWidth);
1278     computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1279     // If the sign bit of the input is known set or clear, then we know the
1280     // top bits of the result.
1281     Known = Known.sext(BitWidth);
1282     break;
1283   }
1284   case Instruction::Shl: {
1285     // (shl X, C1) & C2 == 0   iff   (X & C2 >>u C1) == 0
1286     bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1287     auto KZF = [NSW](const APInt &KnownZero, unsigned ShiftAmt) {
1288       APInt KZResult = KnownZero << ShiftAmt;
1289       KZResult.setLowBits(ShiftAmt); // Low bits known 0.
1290       // If this shift has "nsw" keyword, then the result is either a poison
1291       // value or has the same sign bit as the first operand.
1292       if (NSW && KnownZero.isSignBitSet())
1293         KZResult.setSignBit();
1294       return KZResult;
1295     };
1296 
1297     auto KOF = [NSW](const APInt &KnownOne, unsigned ShiftAmt) {
1298       APInt KOResult = KnownOne << ShiftAmt;
1299       if (NSW && KnownOne.isSignBitSet())
1300         KOResult.setSignBit();
1301       return KOResult;
1302     };
1303 
1304     computeKnownBitsFromShiftOperator(I, Known, Known2, Depth, Q, KZF, KOF);
1305     break;
1306   }
1307   case Instruction::LShr: {
1308     // (lshr X, C1) & C2 == 0   iff  (-1 >> C1) & C2 == 0
1309     auto KZF = [](const APInt &KnownZero, unsigned ShiftAmt) {
1310       APInt KZResult = KnownZero.lshr(ShiftAmt);
1311       // High bits known zero.
1312       KZResult.setHighBits(ShiftAmt);
1313       return KZResult;
1314     };
1315 
1316     auto KOF = [](const APInt &KnownOne, unsigned ShiftAmt) {
1317       return KnownOne.lshr(ShiftAmt);
1318     };
1319 
1320     computeKnownBitsFromShiftOperator(I, Known, Known2, Depth, Q, KZF, KOF);
1321     break;
1322   }
1323   case Instruction::AShr: {
1324     // (ashr X, C1) & C2 == 0   iff  (-1 >> C1) & C2 == 0
1325     auto KZF = [](const APInt &KnownZero, unsigned ShiftAmt) {
1326       return KnownZero.ashr(ShiftAmt);
1327     };
1328 
1329     auto KOF = [](const APInt &KnownOne, unsigned ShiftAmt) {
1330       return KnownOne.ashr(ShiftAmt);
1331     };
1332 
1333     computeKnownBitsFromShiftOperator(I, Known, Known2, Depth, Q, KZF, KOF);
1334     break;
1335   }
1336   case Instruction::Sub: {
1337     bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1338     computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW,
1339                            Known, Known2, Depth, Q);
1340     break;
1341   }
1342   case Instruction::Add: {
1343     bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1344     computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW,
1345                            Known, Known2, Depth, Q);
1346     break;
1347   }
1348   case Instruction::SRem:
1349     if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
1350       APInt RA = Rem->getValue().abs();
1351       if (RA.isPowerOf2()) {
1352         APInt LowBits = RA - 1;
1353         computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1354 
1355         // The low bits of the first operand are unchanged by the srem.
1356         Known.Zero = Known2.Zero & LowBits;
1357         Known.One = Known2.One & LowBits;
1358 
1359         // If the first operand is non-negative or has all low bits zero, then
1360         // the upper bits are all zero.
1361         if (Known2.isNonNegative() || LowBits.isSubsetOf(Known2.Zero))
1362           Known.Zero |= ~LowBits;
1363 
1364         // If the first operand is negative and not all low bits are zero, then
1365         // the upper bits are all one.
1366         if (Known2.isNegative() && LowBits.intersects(Known2.One))
1367           Known.One |= ~LowBits;
1368 
1369         assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?");
1370         break;
1371       }
1372     }
1373 
1374     // The sign bit is the LHS's sign bit, except when the result of the
1375     // remainder is zero.
1376     computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1377     // If it's known zero, our sign bit is also zero.
1378     if (Known2.isNonNegative())
1379       Known.makeNonNegative();
1380 
1381     break;
1382   case Instruction::URem: {
1383     if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
1384       const APInt &RA = Rem->getValue();
1385       if (RA.isPowerOf2()) {
1386         APInt LowBits = (RA - 1);
1387         computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1388         Known.Zero |= ~LowBits;
1389         Known.One &= LowBits;
1390         break;
1391       }
1392     }
1393 
1394     // Since the result is less than or equal to either operand, any leading
1395     // zero bits in either operand must also exist in the result.
1396     computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1397     computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1398 
1399     unsigned Leaders =
1400         std::max(Known.countMinLeadingZeros(), Known2.countMinLeadingZeros());
1401     Known.resetAll();
1402     Known.Zero.setHighBits(Leaders);
1403     break;
1404   }
1405 
1406   case Instruction::Alloca: {
1407     const AllocaInst *AI = cast<AllocaInst>(I);
1408     unsigned Align = AI->getAlignment();
1409     if (Align == 0)
1410       Align = Q.DL.getABITypeAlignment(AI->getAllocatedType());
1411 
1412     if (Align > 0)
1413       Known.Zero.setLowBits(countTrailingZeros(Align));
1414     break;
1415   }
1416   case Instruction::GetElementPtr: {
1417     // Analyze all of the subscripts of this getelementptr instruction
1418     // to determine if we can prove known low zero bits.
1419     KnownBits LocalKnown(BitWidth);
1420     computeKnownBits(I->getOperand(0), LocalKnown, Depth + 1, Q);
1421     unsigned TrailZ = LocalKnown.countMinTrailingZeros();
1422 
1423     gep_type_iterator GTI = gep_type_begin(I);
1424     for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1425       Value *Index = I->getOperand(i);
1426       if (StructType *STy = GTI.getStructTypeOrNull()) {
1427         // Handle struct member offset arithmetic.
1428 
1429         // Handle case when index is vector zeroinitializer
1430         Constant *CIndex = cast<Constant>(Index);
1431         if (CIndex->isZeroValue())
1432           continue;
1433 
1434         if (CIndex->getType()->isVectorTy())
1435           Index = CIndex->getSplatValue();
1436 
1437         unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1438         const StructLayout *SL = Q.DL.getStructLayout(STy);
1439         uint64_t Offset = SL->getElementOffset(Idx);
1440         TrailZ = std::min<unsigned>(TrailZ,
1441                                     countTrailingZeros(Offset));
1442       } else {
1443         // Handle array index arithmetic.
1444         Type *IndexedTy = GTI.getIndexedType();
1445         if (!IndexedTy->isSized()) {
1446           TrailZ = 0;
1447           break;
1448         }
1449         unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits();
1450         uint64_t TypeSize = Q.DL.getTypeAllocSize(IndexedTy);
1451         LocalKnown.Zero = LocalKnown.One = APInt(GEPOpiBits, 0);
1452         computeKnownBits(Index, LocalKnown, Depth + 1, Q);
1453         TrailZ = std::min(TrailZ,
1454                           unsigned(countTrailingZeros(TypeSize) +
1455                                    LocalKnown.countMinTrailingZeros()));
1456       }
1457     }
1458 
1459     Known.Zero.setLowBits(TrailZ);
1460     break;
1461   }
1462   case Instruction::PHI: {
1463     const PHINode *P = cast<PHINode>(I);
1464     // Handle the case of a simple two-predecessor recurrence PHI.
1465     // There's a lot more that could theoretically be done here, but
1466     // this is sufficient to catch some interesting cases.
1467     if (P->getNumIncomingValues() == 2) {
1468       for (unsigned i = 0; i != 2; ++i) {
1469         Value *L = P->getIncomingValue(i);
1470         Value *R = P->getIncomingValue(!i);
1471         Instruction *RInst = P->getIncomingBlock(!i)->getTerminator();
1472         Instruction *LInst = P->getIncomingBlock(i)->getTerminator();
1473         Operator *LU = dyn_cast<Operator>(L);
1474         if (!LU)
1475           continue;
1476         unsigned Opcode = LU->getOpcode();
1477         // Check for operations that have the property that if
1478         // both their operands have low zero bits, the result
1479         // will have low zero bits.
1480         if (Opcode == Instruction::Add ||
1481             Opcode == Instruction::Sub ||
1482             Opcode == Instruction::And ||
1483             Opcode == Instruction::Or ||
1484             Opcode == Instruction::Mul) {
1485           Value *LL = LU->getOperand(0);
1486           Value *LR = LU->getOperand(1);
1487           // Find a recurrence.
1488           if (LL == I)
1489             L = LR;
1490           else if (LR == I)
1491             L = LL;
1492           else
1493             continue; // Check for recurrence with L and R flipped.
1494 
1495           // Change the context instruction to the "edge" that flows into the
1496           // phi. This is important because that is where the value is actually
1497           // "evaluated" even though it is used later somewhere else. (see also
1498           // D69571).
1499           Query RecQ = Q;
1500 
1501           // Ok, we have a PHI of the form L op= R. Check for low
1502           // zero bits.
1503           RecQ.CxtI = RInst;
1504           computeKnownBits(R, Known2, Depth + 1, RecQ);
1505 
1506           // We need to take the minimum number of known bits
1507           KnownBits Known3(Known);
1508           RecQ.CxtI = LInst;
1509           computeKnownBits(L, Known3, Depth + 1, RecQ);
1510 
1511           Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1512                                          Known3.countMinTrailingZeros()));
1513 
1514           auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(LU);
1515           if (OverflowOp && Q.IIQ.hasNoSignedWrap(OverflowOp)) {
1516             // If initial value of recurrence is nonnegative, and we are adding
1517             // a nonnegative number with nsw, the result can only be nonnegative
1518             // or poison value regardless of the number of times we execute the
1519             // add in phi recurrence. If initial value is negative and we are
1520             // adding a negative number with nsw, the result can only be
1521             // negative or poison value. Similar arguments apply to sub and mul.
1522             //
1523             // (add non-negative, non-negative) --> non-negative
1524             // (add negative, negative) --> negative
1525             if (Opcode == Instruction::Add) {
1526               if (Known2.isNonNegative() && Known3.isNonNegative())
1527                 Known.makeNonNegative();
1528               else if (Known2.isNegative() && Known3.isNegative())
1529                 Known.makeNegative();
1530             }
1531 
1532             // (sub nsw non-negative, negative) --> non-negative
1533             // (sub nsw negative, non-negative) --> negative
1534             else if (Opcode == Instruction::Sub && LL == I) {
1535               if (Known2.isNonNegative() && Known3.isNegative())
1536                 Known.makeNonNegative();
1537               else if (Known2.isNegative() && Known3.isNonNegative())
1538                 Known.makeNegative();
1539             }
1540 
1541             // (mul nsw non-negative, non-negative) --> non-negative
1542             else if (Opcode == Instruction::Mul && Known2.isNonNegative() &&
1543                      Known3.isNonNegative())
1544               Known.makeNonNegative();
1545           }
1546 
1547           break;
1548         }
1549       }
1550     }
1551 
1552     // Unreachable blocks may have zero-operand PHI nodes.
1553     if (P->getNumIncomingValues() == 0)
1554       break;
1555 
1556     // Otherwise take the unions of the known bit sets of the operands,
1557     // taking conservative care to avoid excessive recursion.
1558     if (Depth < MaxDepth - 1 && !Known.Zero && !Known.One) {
1559       // Skip if every incoming value references to ourself.
1560       if (dyn_cast_or_null<UndefValue>(P->hasConstantValue()))
1561         break;
1562 
1563       Known.Zero.setAllBits();
1564       Known.One.setAllBits();
1565       for (unsigned u = 0, e = P->getNumIncomingValues(); u < e; ++u) {
1566         Value *IncValue = P->getIncomingValue(u);
1567         // Skip direct self references.
1568         if (IncValue == P) continue;
1569 
1570         // Change the context instruction to the "edge" that flows into the
1571         // phi. This is important because that is where the value is actually
1572         // "evaluated" even though it is used later somewhere else. (see also
1573         // D69571).
1574         Query RecQ = Q;
1575         RecQ.CxtI = P->getIncomingBlock(u)->getTerminator();
1576 
1577         Known2 = KnownBits(BitWidth);
1578         // Recurse, but cap the recursion to one level, because we don't
1579         // want to waste time spinning around in loops.
1580         computeKnownBits(IncValue, Known2, MaxDepth - 1, RecQ);
1581         Known.Zero &= Known2.Zero;
1582         Known.One &= Known2.One;
1583         // If all bits have been ruled out, there's no need to check
1584         // more operands.
1585         if (!Known.Zero && !Known.One)
1586           break;
1587       }
1588     }
1589     break;
1590   }
1591   case Instruction::Call:
1592   case Instruction::Invoke:
1593     // If range metadata is attached to this call, set known bits from that,
1594     // and then intersect with known bits based on other properties of the
1595     // function.
1596     if (MDNode *MD =
1597             Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
1598       computeKnownBitsFromRangeMetadata(*MD, Known);
1599     if (const Value *RV = ImmutableCallSite(I).getReturnedArgOperand()) {
1600       computeKnownBits(RV, Known2, Depth + 1, Q);
1601       Known.Zero |= Known2.Zero;
1602       Known.One |= Known2.One;
1603     }
1604     if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1605       switch (II->getIntrinsicID()) {
1606       default: break;
1607       case Intrinsic::bitreverse:
1608         computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1609         Known.Zero |= Known2.Zero.reverseBits();
1610         Known.One |= Known2.One.reverseBits();
1611         break;
1612       case Intrinsic::bswap:
1613         computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1614         Known.Zero |= Known2.Zero.byteSwap();
1615         Known.One |= Known2.One.byteSwap();
1616         break;
1617       case Intrinsic::ctlz: {
1618         computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1619         // If we have a known 1, its position is our upper bound.
1620         unsigned PossibleLZ = Known2.One.countLeadingZeros();
1621         // If this call is undefined for 0, the result will be less than 2^n.
1622         if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1623           PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
1624         unsigned LowBits = Log2_32(PossibleLZ)+1;
1625         Known.Zero.setBitsFrom(LowBits);
1626         break;
1627       }
1628       case Intrinsic::cttz: {
1629         computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1630         // If we have a known 1, its position is our upper bound.
1631         unsigned PossibleTZ = Known2.One.countTrailingZeros();
1632         // If this call is undefined for 0, the result will be less than 2^n.
1633         if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1634           PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
1635         unsigned LowBits = Log2_32(PossibleTZ)+1;
1636         Known.Zero.setBitsFrom(LowBits);
1637         break;
1638       }
1639       case Intrinsic::ctpop: {
1640         computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1641         // We can bound the space the count needs.  Also, bits known to be zero
1642         // can't contribute to the population.
1643         unsigned BitsPossiblySet = Known2.countMaxPopulation();
1644         unsigned LowBits = Log2_32(BitsPossiblySet)+1;
1645         Known.Zero.setBitsFrom(LowBits);
1646         // TODO: we could bound KnownOne using the lower bound on the number
1647         // of bits which might be set provided by popcnt KnownOne2.
1648         break;
1649       }
1650       case Intrinsic::fshr:
1651       case Intrinsic::fshl: {
1652         const APInt *SA;
1653         if (!match(I->getOperand(2), m_APInt(SA)))
1654           break;
1655 
1656         // Normalize to funnel shift left.
1657         uint64_t ShiftAmt = SA->urem(BitWidth);
1658         if (II->getIntrinsicID() == Intrinsic::fshr)
1659           ShiftAmt = BitWidth - ShiftAmt;
1660 
1661         KnownBits Known3(Known);
1662         computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1663         computeKnownBits(I->getOperand(1), Known3, Depth + 1, Q);
1664 
1665         Known.Zero =
1666             Known2.Zero.shl(ShiftAmt) | Known3.Zero.lshr(BitWidth - ShiftAmt);
1667         Known.One =
1668             Known2.One.shl(ShiftAmt) | Known3.One.lshr(BitWidth - ShiftAmt);
1669         break;
1670       }
1671       case Intrinsic::uadd_sat:
1672       case Intrinsic::usub_sat: {
1673         bool IsAdd = II->getIntrinsicID() == Intrinsic::uadd_sat;
1674         computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1675         computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1676 
1677         // Add: Leading ones of either operand are preserved.
1678         // Sub: Leading zeros of LHS and leading ones of RHS are preserved
1679         // as leading zeros in the result.
1680         unsigned LeadingKnown;
1681         if (IsAdd)
1682           LeadingKnown = std::max(Known.countMinLeadingOnes(),
1683                                   Known2.countMinLeadingOnes());
1684         else
1685           LeadingKnown = std::max(Known.countMinLeadingZeros(),
1686                                   Known2.countMinLeadingOnes());
1687 
1688         Known = KnownBits::computeForAddSub(
1689             IsAdd, /* NSW */ false, Known, Known2);
1690 
1691         // We select between the operation result and all-ones/zero
1692         // respectively, so we can preserve known ones/zeros.
1693         if (IsAdd) {
1694           Known.One.setHighBits(LeadingKnown);
1695           Known.Zero.clearAllBits();
1696         } else {
1697           Known.Zero.setHighBits(LeadingKnown);
1698           Known.One.clearAllBits();
1699         }
1700         break;
1701       }
1702       case Intrinsic::x86_sse42_crc32_64_64:
1703         Known.Zero.setBitsFrom(32);
1704         break;
1705       }
1706     }
1707     break;
1708   case Instruction::ShuffleVector: {
1709     auto *Shuf = cast<ShuffleVectorInst>(I);
1710     // For undef elements, we don't know anything about the common state of
1711     // the shuffle result.
1712     APInt DemandedLHS, DemandedRHS;
1713     if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
1714       Known.resetAll();
1715       return;
1716     }
1717     Known.One.setAllBits();
1718     Known.Zero.setAllBits();
1719     if (!!DemandedLHS) {
1720       const Value *LHS = Shuf->getOperand(0);
1721       computeKnownBits(LHS, DemandedLHS, Known, Depth + 1, Q);
1722       // If we don't know any bits, early out.
1723       if (Known.isUnknown())
1724         break;
1725     }
1726     if (!!DemandedRHS) {
1727       const Value *RHS = Shuf->getOperand(1);
1728       computeKnownBits(RHS, DemandedRHS, Known2, Depth + 1, Q);
1729       Known.One &= Known2.One;
1730       Known.Zero &= Known2.Zero;
1731     }
1732     break;
1733   }
1734   case Instruction::InsertElement: {
1735     auto *IEI = cast<InsertElementInst>(I);
1736     Value *Vec = IEI->getOperand(0);
1737     Value *Elt = IEI->getOperand(1);
1738     auto *CIdx = dyn_cast<ConstantInt>(IEI->getOperand(2));
1739     // Early out if the index is non-constant or out-of-range.
1740     unsigned NumElts = DemandedElts.getBitWidth();
1741     if (!CIdx || CIdx->getValue().uge(NumElts)) {
1742       Known.resetAll();
1743       return;
1744     }
1745     Known.One.setAllBits();
1746     Known.Zero.setAllBits();
1747     unsigned EltIdx = CIdx->getZExtValue();
1748     // Do we demand the inserted element?
1749     if (DemandedElts[EltIdx]) {
1750       computeKnownBits(Elt, Known, Depth + 1, Q);
1751       // If we don't know any bits, early out.
1752       if (Known.isUnknown())
1753         break;
1754     }
1755     // We don't need the base vector element that has been inserted.
1756     APInt DemandedVecElts = DemandedElts;
1757     DemandedVecElts.clearBit(EltIdx);
1758     if (!!DemandedVecElts) {
1759       computeKnownBits(Vec, DemandedVecElts, Known2, Depth + 1, Q);
1760       Known.One &= Known2.One;
1761       Known.Zero &= Known2.Zero;
1762     }
1763     break;
1764   }
1765   case Instruction::ExtractElement:
1766     // Look through extract element. At the moment we keep this simple and skip
1767     // tracking the specific element. But at least we might find information
1768     // valid for all elements of the vector (for example if vector is sign
1769     // extended, shifted, etc).
1770     computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1771     break;
1772   case Instruction::ExtractValue:
1773     if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
1774       const ExtractValueInst *EVI = cast<ExtractValueInst>(I);
1775       if (EVI->getNumIndices() != 1) break;
1776       if (EVI->getIndices()[0] == 0) {
1777         switch (II->getIntrinsicID()) {
1778         default: break;
1779         case Intrinsic::uadd_with_overflow:
1780         case Intrinsic::sadd_with_overflow:
1781           computeKnownBitsAddSub(true, II->getArgOperand(0),
1782                                  II->getArgOperand(1), false, Known, Known2,
1783                                  Depth, Q);
1784           break;
1785         case Intrinsic::usub_with_overflow:
1786         case Intrinsic::ssub_with_overflow:
1787           computeKnownBitsAddSub(false, II->getArgOperand(0),
1788                                  II->getArgOperand(1), false, Known, Known2,
1789                                  Depth, Q);
1790           break;
1791         case Intrinsic::umul_with_overflow:
1792         case Intrinsic::smul_with_overflow:
1793           computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
1794                               Known, Known2, Depth, Q);
1795           break;
1796         }
1797       }
1798     }
1799     break;
1800   }
1801 }
1802 
1803 /// Determine which bits of V are known to be either zero or one and return
1804 /// them.
1805 KnownBits computeKnownBits(const Value *V, unsigned Depth, const Query &Q) {
1806   KnownBits Known(getBitWidth(V->getType(), Q.DL));
1807   computeKnownBits(V, Known, Depth, Q);
1808   return Known;
1809 }
1810 
1811 /// Determine which bits of V are known to be either zero or one and return
1812 /// them in the Known bit set.
1813 ///
1814 /// NOTE: we cannot consider 'undef' to be "IsZero" here.  The problem is that
1815 /// we cannot optimize based on the assumption that it is zero without changing
1816 /// it to be an explicit zero.  If we don't change it to zero, other code could
1817 /// optimized based on the contradictory assumption that it is non-zero.
1818 /// Because instcombine aggressively folds operations with undef args anyway,
1819 /// this won't lose us code quality.
1820 ///
1821 /// This function is defined on values with integer type, values with pointer
1822 /// type, and vectors of integers.  In the case
1823 /// where V is a vector, known zero, and known one values are the
1824 /// same width as the vector element, and the bit is set only if it is true
1825 /// for all of the demanded elements in the vector specified by DemandedElts.
1826 void computeKnownBits(const Value *V, const APInt &DemandedElts,
1827                       KnownBits &Known, unsigned Depth, const Query &Q) {
1828   assert(V && "No Value?");
1829   assert(Depth <= MaxDepth && "Limit Search Depth");
1830   unsigned BitWidth = Known.getBitWidth();
1831 
1832   Type *Ty = V->getType();
1833   assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&
1834          "Not integer or pointer type!");
1835   assert(((Ty->isVectorTy() &&
1836            Ty->getVectorNumElements() == DemandedElts.getBitWidth()) ||
1837           (!Ty->isVectorTy() && DemandedElts == APInt(1, 1))) &&
1838          "Unexpected vector size");
1839 
1840   Type *ScalarTy = Ty->getScalarType();
1841   unsigned ExpectedWidth = ScalarTy->isPointerTy() ?
1842     Q.DL.getPointerTypeSizeInBits(ScalarTy) : Q.DL.getTypeSizeInBits(ScalarTy);
1843   assert(ExpectedWidth == BitWidth && "V and Known should have same BitWidth");
1844   (void)BitWidth;
1845   (void)ExpectedWidth;
1846 
1847   if (!DemandedElts) {
1848     // No demanded elts, better to assume we don't know anything.
1849     Known.resetAll();
1850     return;
1851   }
1852 
1853   const APInt *C;
1854   if (match(V, m_APInt(C))) {
1855     // We know all of the bits for a scalar constant or a splat vector constant!
1856     Known.One = *C;
1857     Known.Zero = ~Known.One;
1858     return;
1859   }
1860   // Null and aggregate-zero are all-zeros.
1861   if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) {
1862     Known.setAllZero();
1863     return;
1864   }
1865   // Handle a constant vector by taking the intersection of the known bits of
1866   // each element.
1867   if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V)) {
1868     assert((!Ty->isVectorTy() ||
1869             CDS->getNumElements() == DemandedElts.getBitWidth()) &&
1870            "Unexpected vector size");
1871     // We know that CDS must be a vector of integers. Take the intersection of
1872     // each element.
1873     Known.Zero.setAllBits(); Known.One.setAllBits();
1874     for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1875       if (Ty->isVectorTy() && !DemandedElts[i])
1876         continue;
1877       APInt Elt = CDS->getElementAsAPInt(i);
1878       Known.Zero &= ~Elt;
1879       Known.One &= Elt;
1880     }
1881     return;
1882   }
1883 
1884   if (const auto *CV = dyn_cast<ConstantVector>(V)) {
1885     assert(CV->getNumOperands() == DemandedElts.getBitWidth() &&
1886            "Unexpected vector size");
1887     // We know that CV must be a vector of integers. Take the intersection of
1888     // each element.
1889     Known.Zero.setAllBits(); Known.One.setAllBits();
1890     for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
1891       if (!DemandedElts[i])
1892         continue;
1893       Constant *Element = CV->getAggregateElement(i);
1894       auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
1895       if (!ElementCI) {
1896         Known.resetAll();
1897         return;
1898       }
1899       const APInt &Elt = ElementCI->getValue();
1900       Known.Zero &= ~Elt;
1901       Known.One &= Elt;
1902     }
1903     return;
1904   }
1905 
1906   // Start out not knowing anything.
1907   Known.resetAll();
1908 
1909   // We can't imply anything about undefs.
1910   if (isa<UndefValue>(V))
1911     return;
1912 
1913   // There's no point in looking through other users of ConstantData for
1914   // assumptions.  Confirm that we've handled them all.
1915   assert(!isa<ConstantData>(V) && "Unhandled constant data!");
1916 
1917   // Limit search depth.
1918   // All recursive calls that increase depth must come after this.
1919   if (Depth == MaxDepth)
1920     return;
1921 
1922   // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
1923   // the bits of its aliasee.
1924   if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
1925     if (!GA->isInterposable())
1926       computeKnownBits(GA->getAliasee(), Known, Depth + 1, Q);
1927     return;
1928   }
1929 
1930   if (const Operator *I = dyn_cast<Operator>(V))
1931     computeKnownBitsFromOperator(I, DemandedElts, Known, Depth, Q);
1932 
1933   // Aligned pointers have trailing zeros - refine Known.Zero set
1934   if (Ty->isPointerTy()) {
1935     const MaybeAlign Align = V->getPointerAlignment(Q.DL);
1936     if (Align)
1937       Known.Zero.setLowBits(countTrailingZeros(Align->value()));
1938   }
1939 
1940   // computeKnownBitsFromAssume strictly refines Known.
1941   // Therefore, we run them after computeKnownBitsFromOperator.
1942 
1943   // Check whether a nearby assume intrinsic can determine some known bits.
1944   computeKnownBitsFromAssume(V, Known, Depth, Q);
1945 
1946   assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?");
1947 }
1948 
1949 /// Return true if the given value is known to have exactly one
1950 /// bit set when defined. For vectors return true if every element is known to
1951 /// be a power of two when defined. Supports values with integer or pointer
1952 /// types and vectors of integers.
1953 bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
1954                             const Query &Q) {
1955   assert(Depth <= MaxDepth && "Limit Search Depth");
1956 
1957   // Attempt to match against constants.
1958   if (OrZero && match(V, m_Power2OrZero()))
1959       return true;
1960   if (match(V, m_Power2()))
1961       return true;
1962 
1963   // 1 << X is clearly a power of two if the one is not shifted off the end.  If
1964   // it is shifted off the end then the result is undefined.
1965   if (match(V, m_Shl(m_One(), m_Value())))
1966     return true;
1967 
1968   // (signmask) >>l X is clearly a power of two if the one is not shifted off
1969   // the bottom.  If it is shifted off the bottom then the result is undefined.
1970   if (match(V, m_LShr(m_SignMask(), m_Value())))
1971     return true;
1972 
1973   // The remaining tests are all recursive, so bail out if we hit the limit.
1974   if (Depth++ == MaxDepth)
1975     return false;
1976 
1977   Value *X = nullptr, *Y = nullptr;
1978   // A shift left or a logical shift right of a power of two is a power of two
1979   // or zero.
1980   if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) ||
1981                  match(V, m_LShr(m_Value(X), m_Value()))))
1982     return isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q);
1983 
1984   if (const ZExtInst *ZI = dyn_cast<ZExtInst>(V))
1985     return isKnownToBeAPowerOfTwo(ZI->getOperand(0), OrZero, Depth, Q);
1986 
1987   if (const SelectInst *SI = dyn_cast<SelectInst>(V))
1988     return isKnownToBeAPowerOfTwo(SI->getTrueValue(), OrZero, Depth, Q) &&
1989            isKnownToBeAPowerOfTwo(SI->getFalseValue(), OrZero, Depth, Q);
1990 
1991   if (OrZero && match(V, m_And(m_Value(X), m_Value(Y)))) {
1992     // A power of two and'd with anything is a power of two or zero.
1993     if (isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q) ||
1994         isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, Depth, Q))
1995       return true;
1996     // X & (-X) is always a power of two or zero.
1997     if (match(X, m_Neg(m_Specific(Y))) || match(Y, m_Neg(m_Specific(X))))
1998       return true;
1999     return false;
2000   }
2001 
2002   // Adding a power-of-two or zero to the same power-of-two or zero yields
2003   // either the original power-of-two, a larger power-of-two or zero.
2004   if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
2005     const OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V);
2006     if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2007         Q.IIQ.hasNoSignedWrap(VOBO)) {
2008       if (match(X, m_And(m_Specific(Y), m_Value())) ||
2009           match(X, m_And(m_Value(), m_Specific(Y))))
2010         if (isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q))
2011           return true;
2012       if (match(Y, m_And(m_Specific(X), m_Value())) ||
2013           match(Y, m_And(m_Value(), m_Specific(X))))
2014         if (isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q))
2015           return true;
2016 
2017       unsigned BitWidth = V->getType()->getScalarSizeInBits();
2018       KnownBits LHSBits(BitWidth);
2019       computeKnownBits(X, LHSBits, Depth, Q);
2020 
2021       KnownBits RHSBits(BitWidth);
2022       computeKnownBits(Y, RHSBits, Depth, Q);
2023       // If i8 V is a power of two or zero:
2024       //  ZeroBits: 1 1 1 0 1 1 1 1
2025       // ~ZeroBits: 0 0 0 1 0 0 0 0
2026       if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2027         // If OrZero isn't set, we cannot give back a zero result.
2028         // Make sure either the LHS or RHS has a bit set.
2029         if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2030           return true;
2031     }
2032   }
2033 
2034   // An exact divide or right shift can only shift off zero bits, so the result
2035   // is a power of two only if the first operand is a power of two and not
2036   // copying a sign bit (sdiv int_min, 2).
2037   if (match(V, m_Exact(m_LShr(m_Value(), m_Value()))) ||
2038       match(V, m_Exact(m_UDiv(m_Value(), m_Value())))) {
2039     return isKnownToBeAPowerOfTwo(cast<Operator>(V)->getOperand(0), OrZero,
2040                                   Depth, Q);
2041   }
2042 
2043   return false;
2044 }
2045 
2046 /// Test whether a GEP's result is known to be non-null.
2047 ///
2048 /// Uses properties inherent in a GEP to try to determine whether it is known
2049 /// to be non-null.
2050 ///
2051 /// Currently this routine does not support vector GEPs.
2052 static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth,
2053                               const Query &Q) {
2054   const Function *F = nullptr;
2055   if (const Instruction *I = dyn_cast<Instruction>(GEP))
2056     F = I->getFunction();
2057 
2058   if (!GEP->isInBounds() ||
2059       NullPointerIsDefined(F, GEP->getPointerAddressSpace()))
2060     return false;
2061 
2062   // FIXME: Support vector-GEPs.
2063   assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2064 
2065   // If the base pointer is non-null, we cannot walk to a null address with an
2066   // inbounds GEP in address space zero.
2067   if (isKnownNonZero(GEP->getPointerOperand(), Depth, Q))
2068     return true;
2069 
2070   // Walk the GEP operands and see if any operand introduces a non-zero offset.
2071   // If so, then the GEP cannot produce a null pointer, as doing so would
2072   // inherently violate the inbounds contract within address space zero.
2073   for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP);
2074        GTI != GTE; ++GTI) {
2075     // Struct types are easy -- they must always be indexed by a constant.
2076     if (StructType *STy = GTI.getStructTypeOrNull()) {
2077       ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2078       unsigned ElementIdx = OpC->getZExtValue();
2079       const StructLayout *SL = Q.DL.getStructLayout(STy);
2080       uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2081       if (ElementOffset > 0)
2082         return true;
2083       continue;
2084     }
2085 
2086     // If we have a zero-sized type, the index doesn't matter. Keep looping.
2087     if (Q.DL.getTypeAllocSize(GTI.getIndexedType()) == 0)
2088       continue;
2089 
2090     // Fast path the constant operand case both for efficiency and so we don't
2091     // increment Depth when just zipping down an all-constant GEP.
2092     if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2093       if (!OpC->isZero())
2094         return true;
2095       continue;
2096     }
2097 
2098     // We post-increment Depth here because while isKnownNonZero increments it
2099     // as well, when we pop back up that increment won't persist. We don't want
2100     // to recurse 10k times just because we have 10k GEP operands. We don't
2101     // bail completely out because we want to handle constant GEPs regardless
2102     // of depth.
2103     if (Depth++ >= MaxDepth)
2104       continue;
2105 
2106     if (isKnownNonZero(GTI.getOperand(), Depth, Q))
2107       return true;
2108   }
2109 
2110   return false;
2111 }
2112 
2113 static bool isKnownNonNullFromDominatingCondition(const Value *V,
2114                                                   const Instruction *CtxI,
2115                                                   const DominatorTree *DT) {
2116   if (isa<Constant>(V))
2117     return false;
2118 
2119   if (!CtxI || !DT)
2120     return false;
2121 
2122   unsigned NumUsesExplored = 0;
2123   for (auto *U : V->users()) {
2124     // Avoid massive lists
2125     if (NumUsesExplored >= DomConditionsMaxUses)
2126       break;
2127     NumUsesExplored++;
2128 
2129     // If the value is used as an argument to a call or invoke, then argument
2130     // attributes may provide an answer about null-ness.
2131     if (auto CS = ImmutableCallSite(U))
2132       if (auto *CalledFunc = CS.getCalledFunction())
2133         for (const Argument &Arg : CalledFunc->args())
2134           if (CS.getArgOperand(Arg.getArgNo()) == V &&
2135               Arg.hasNonNullAttr() && DT->dominates(CS.getInstruction(), CtxI))
2136             return true;
2137 
2138     // If the value is used as a load/store, then the pointer must be non null.
2139     if (V == getLoadStorePointerOperand(U)) {
2140       const Instruction *I = cast<Instruction>(U);
2141       if (!NullPointerIsDefined(I->getFunction(),
2142                                 V->getType()->getPointerAddressSpace()) &&
2143           DT->dominates(I, CtxI))
2144         return true;
2145     }
2146 
2147     // Consider only compare instructions uniquely controlling a branch
2148     CmpInst::Predicate Pred;
2149     if (!match(const_cast<User *>(U),
2150                m_c_ICmp(Pred, m_Specific(V), m_Zero())) ||
2151         (Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE))
2152       continue;
2153 
2154     SmallVector<const User *, 4> WorkList;
2155     SmallPtrSet<const User *, 4> Visited;
2156     for (auto *CmpU : U->users()) {
2157       assert(WorkList.empty() && "Should be!");
2158       if (Visited.insert(CmpU).second)
2159         WorkList.push_back(CmpU);
2160 
2161       while (!WorkList.empty()) {
2162         auto *Curr = WorkList.pop_back_val();
2163 
2164         // If a user is an AND, add all its users to the work list. We only
2165         // propagate "pred != null" condition through AND because it is only
2166         // correct to assume that all conditions of AND are met in true branch.
2167         // TODO: Support similar logic of OR and EQ predicate?
2168         if (Pred == ICmpInst::ICMP_NE)
2169           if (auto *BO = dyn_cast<BinaryOperator>(Curr))
2170             if (BO->getOpcode() == Instruction::And) {
2171               for (auto *BOU : BO->users())
2172                 if (Visited.insert(BOU).second)
2173                   WorkList.push_back(BOU);
2174               continue;
2175             }
2176 
2177         if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {
2178           assert(BI->isConditional() && "uses a comparison!");
2179 
2180           BasicBlock *NonNullSuccessor =
2181               BI->getSuccessor(Pred == ICmpInst::ICMP_EQ ? 1 : 0);
2182           BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
2183           if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
2184             return true;
2185         } else if (Pred == ICmpInst::ICMP_NE && isGuard(Curr) &&
2186                    DT->dominates(cast<Instruction>(Curr), CtxI)) {
2187           return true;
2188         }
2189       }
2190     }
2191   }
2192 
2193   return false;
2194 }
2195 
2196 /// Does the 'Range' metadata (which must be a valid MD_range operand list)
2197 /// ensure that the value it's attached to is never Value?  'RangeType' is
2198 /// is the type of the value described by the range.
2199 static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
2200   const unsigned NumRanges = Ranges->getNumOperands() / 2;
2201   assert(NumRanges >= 1);
2202   for (unsigned i = 0; i < NumRanges; ++i) {
2203     ConstantInt *Lower =
2204         mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
2205     ConstantInt *Upper =
2206         mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
2207     ConstantRange Range(Lower->getValue(), Upper->getValue());
2208     if (Range.contains(Value))
2209       return false;
2210   }
2211   return true;
2212 }
2213 
2214 /// Return true if the given value is known to be non-zero when defined. For
2215 /// vectors, return true if every element is known to be non-zero when
2216 /// defined. For pointers, if the context instruction and dominator tree are
2217 /// specified, perform context-sensitive analysis and return true if the
2218 /// pointer couldn't possibly be null at the specified instruction.
2219 /// Supports values with integer or pointer type and vectors of integers.
2220 bool isKnownNonZero(const Value *V, unsigned Depth, const Query &Q) {
2221   if (auto *C = dyn_cast<Constant>(V)) {
2222     if (C->isNullValue())
2223       return false;
2224     if (isa<ConstantInt>(C))
2225       // Must be non-zero due to null test above.
2226       return true;
2227 
2228     if (auto *CE = dyn_cast<ConstantExpr>(C)) {
2229       // See the comment for IntToPtr/PtrToInt instructions below.
2230       if (CE->getOpcode() == Instruction::IntToPtr ||
2231           CE->getOpcode() == Instruction::PtrToInt)
2232         if (Q.DL.getTypeSizeInBits(CE->getOperand(0)->getType()) <=
2233             Q.DL.getTypeSizeInBits(CE->getType()))
2234           return isKnownNonZero(CE->getOperand(0), Depth, Q);
2235     }
2236 
2237     // For constant vectors, check that all elements are undefined or known
2238     // non-zero to determine that the whole vector is known non-zero.
2239     if (auto *VecTy = dyn_cast<VectorType>(C->getType())) {
2240       for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
2241         Constant *Elt = C->getAggregateElement(i);
2242         if (!Elt || Elt->isNullValue())
2243           return false;
2244         if (!isa<UndefValue>(Elt) && !isa<ConstantInt>(Elt))
2245           return false;
2246       }
2247       return true;
2248     }
2249 
2250     // A global variable in address space 0 is non null unless extern weak
2251     // or an absolute symbol reference. Other address spaces may have null as a
2252     // valid address for a global, so we can't assume anything.
2253     if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2254       if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
2255           GV->getType()->getAddressSpace() == 0)
2256         return true;
2257     } else
2258       return false;
2259   }
2260 
2261   if (auto *I = dyn_cast<Instruction>(V)) {
2262     if (MDNode *Ranges = Q.IIQ.getMetadata(I, LLVMContext::MD_range)) {
2263       // If the possible ranges don't contain zero, then the value is
2264       // definitely non-zero.
2265       if (auto *Ty = dyn_cast<IntegerType>(V->getType())) {
2266         const APInt ZeroValue(Ty->getBitWidth(), 0);
2267         if (rangeMetadataExcludesValue(Ranges, ZeroValue))
2268           return true;
2269       }
2270     }
2271   }
2272 
2273   if (isKnownNonZeroFromAssume(V, Q))
2274     return true;
2275 
2276   // Some of the tests below are recursive, so bail out if we hit the limit.
2277   if (Depth++ >= MaxDepth)
2278     return false;
2279 
2280   // Check for pointer simplifications.
2281   if (V->getType()->isPointerTy()) {
2282     // Alloca never returns null, malloc might.
2283     if (isa<AllocaInst>(V) && Q.DL.getAllocaAddrSpace() == 0)
2284       return true;
2285 
2286     // A byval, inalloca, or nonnull argument is never null.
2287     if (const Argument *A = dyn_cast<Argument>(V))
2288       if (A->hasByValOrInAllocaAttr() || A->hasNonNullAttr())
2289         return true;
2290 
2291     // A Load tagged with nonnull metadata is never null.
2292     if (const LoadInst *LI = dyn_cast<LoadInst>(V))
2293       if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull))
2294         return true;
2295 
2296     if (const auto *Call = dyn_cast<CallBase>(V)) {
2297       if (Call->isReturnNonNull())
2298         return true;
2299       if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
2300         return isKnownNonZero(RP, Depth, Q);
2301     }
2302   }
2303 
2304   if (isKnownNonNullFromDominatingCondition(V, Q.CxtI, Q.DT))
2305     return true;
2306 
2307   // Check for recursive pointer simplifications.
2308   if (V->getType()->isPointerTy()) {
2309     // Look through bitcast operations, GEPs, and int2ptr instructions as they
2310     // do not alter the value, or at least not the nullness property of the
2311     // value, e.g., int2ptr is allowed to zero/sign extend the value.
2312     //
2313     // Note that we have to take special care to avoid looking through
2314     // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
2315     // as casts that can alter the value, e.g., AddrSpaceCasts.
2316     if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V))
2317       if (isGEPKnownNonNull(GEP, Depth, Q))
2318         return true;
2319 
2320     if (auto *BCO = dyn_cast<BitCastOperator>(V))
2321       return isKnownNonZero(BCO->getOperand(0), Depth, Q);
2322 
2323     if (auto *I2P = dyn_cast<IntToPtrInst>(V))
2324       if (Q.DL.getTypeSizeInBits(I2P->getSrcTy()) <=
2325           Q.DL.getTypeSizeInBits(I2P->getDestTy()))
2326         return isKnownNonZero(I2P->getOperand(0), Depth, Q);
2327   }
2328 
2329   // Similar to int2ptr above, we can look through ptr2int here if the cast
2330   // is a no-op or an extend and not a truncate.
2331   if (auto *P2I = dyn_cast<PtrToIntInst>(V))
2332     if (Q.DL.getTypeSizeInBits(P2I->getSrcTy()) <=
2333         Q.DL.getTypeSizeInBits(P2I->getDestTy()))
2334       return isKnownNonZero(P2I->getOperand(0), Depth, Q);
2335 
2336   unsigned BitWidth = getBitWidth(V->getType()->getScalarType(), Q.DL);
2337 
2338   // X | Y != 0 if X != 0 or Y != 0.
2339   Value *X = nullptr, *Y = nullptr;
2340   if (match(V, m_Or(m_Value(X), m_Value(Y))))
2341     return isKnownNonZero(X, Depth, Q) || isKnownNonZero(Y, Depth, Q);
2342 
2343   // ext X != 0 if X != 0.
2344   if (isa<SExtInst>(V) || isa<ZExtInst>(V))
2345     return isKnownNonZero(cast<Instruction>(V)->getOperand(0), Depth, Q);
2346 
2347   // shl X, Y != 0 if X is odd.  Note that the value of the shift is undefined
2348   // if the lowest bit is shifted off the end.
2349   if (match(V, m_Shl(m_Value(X), m_Value(Y)))) {
2350     // shl nuw can't remove any non-zero bits.
2351     const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
2352     if (Q.IIQ.hasNoUnsignedWrap(BO))
2353       return isKnownNonZero(X, Depth, Q);
2354 
2355     KnownBits Known(BitWidth);
2356     computeKnownBits(X, Known, Depth, Q);
2357     if (Known.One[0])
2358       return true;
2359   }
2360   // shr X, Y != 0 if X is negative.  Note that the value of the shift is not
2361   // defined if the sign bit is shifted off the end.
2362   else if (match(V, m_Shr(m_Value(X), m_Value(Y)))) {
2363     // shr exact can only shift out zero bits.
2364     const PossiblyExactOperator *BO = cast<PossiblyExactOperator>(V);
2365     if (BO->isExact())
2366       return isKnownNonZero(X, Depth, Q);
2367 
2368     KnownBits Known = computeKnownBits(X, Depth, Q);
2369     if (Known.isNegative())
2370       return true;
2371 
2372     // If the shifter operand is a constant, and all of the bits shifted
2373     // out are known to be zero, and X is known non-zero then at least one
2374     // non-zero bit must remain.
2375     if (ConstantInt *Shift = dyn_cast<ConstantInt>(Y)) {
2376       auto ShiftVal = Shift->getLimitedValue(BitWidth - 1);
2377       // Is there a known one in the portion not shifted out?
2378       if (Known.countMaxLeadingZeros() < BitWidth - ShiftVal)
2379         return true;
2380       // Are all the bits to be shifted out known zero?
2381       if (Known.countMinTrailingZeros() >= ShiftVal)
2382         return isKnownNonZero(X, Depth, Q);
2383     }
2384   }
2385   // div exact can only produce a zero if the dividend is zero.
2386   else if (match(V, m_Exact(m_IDiv(m_Value(X), m_Value())))) {
2387     return isKnownNonZero(X, Depth, Q);
2388   }
2389   // X + Y.
2390   else if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
2391     KnownBits XKnown = computeKnownBits(X, Depth, Q);
2392     KnownBits YKnown = computeKnownBits(Y, Depth, Q);
2393 
2394     // If X and Y are both non-negative (as signed values) then their sum is not
2395     // zero unless both X and Y are zero.
2396     if (XKnown.isNonNegative() && YKnown.isNonNegative())
2397       if (isKnownNonZero(X, Depth, Q) || isKnownNonZero(Y, Depth, Q))
2398         return true;
2399 
2400     // If X and Y are both negative (as signed values) then their sum is not
2401     // zero unless both X and Y equal INT_MIN.
2402     if (XKnown.isNegative() && YKnown.isNegative()) {
2403       APInt Mask = APInt::getSignedMaxValue(BitWidth);
2404       // The sign bit of X is set.  If some other bit is set then X is not equal
2405       // to INT_MIN.
2406       if (XKnown.One.intersects(Mask))
2407         return true;
2408       // The sign bit of Y is set.  If some other bit is set then Y is not equal
2409       // to INT_MIN.
2410       if (YKnown.One.intersects(Mask))
2411         return true;
2412     }
2413 
2414     // The sum of a non-negative number and a power of two is not zero.
2415     if (XKnown.isNonNegative() &&
2416         isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q))
2417       return true;
2418     if (YKnown.isNonNegative() &&
2419         isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q))
2420       return true;
2421   }
2422   // X * Y.
2423   else if (match(V, m_Mul(m_Value(X), m_Value(Y)))) {
2424     const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
2425     // If X and Y are non-zero then so is X * Y as long as the multiplication
2426     // does not overflow.
2427     if ((Q.IIQ.hasNoSignedWrap(BO) || Q.IIQ.hasNoUnsignedWrap(BO)) &&
2428         isKnownNonZero(X, Depth, Q) && isKnownNonZero(Y, Depth, Q))
2429       return true;
2430   }
2431   // (C ? X : Y) != 0 if X != 0 and Y != 0.
2432   else if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
2433     if (isKnownNonZero(SI->getTrueValue(), Depth, Q) &&
2434         isKnownNonZero(SI->getFalseValue(), Depth, Q))
2435       return true;
2436   }
2437   // PHI
2438   else if (const PHINode *PN = dyn_cast<PHINode>(V)) {
2439     // Try and detect a recurrence that monotonically increases from a
2440     // starting value, as these are common as induction variables.
2441     if (PN->getNumIncomingValues() == 2) {
2442       Value *Start = PN->getIncomingValue(0);
2443       Value *Induction = PN->getIncomingValue(1);
2444       if (isa<ConstantInt>(Induction) && !isa<ConstantInt>(Start))
2445         std::swap(Start, Induction);
2446       if (ConstantInt *C = dyn_cast<ConstantInt>(Start)) {
2447         if (!C->isZero() && !C->isNegative()) {
2448           ConstantInt *X;
2449           if (Q.IIQ.UseInstrInfo &&
2450               (match(Induction, m_NSWAdd(m_Specific(PN), m_ConstantInt(X))) ||
2451                match(Induction, m_NUWAdd(m_Specific(PN), m_ConstantInt(X)))) &&
2452               !X->isNegative())
2453             return true;
2454         }
2455       }
2456     }
2457     // Check if all incoming values are non-zero constant.
2458     bool AllNonZeroConstants = llvm::all_of(PN->operands(), [](Value *V) {
2459       return isa<ConstantInt>(V) && !cast<ConstantInt>(V)->isZero();
2460     });
2461     if (AllNonZeroConstants)
2462       return true;
2463   }
2464 
2465   KnownBits Known(BitWidth);
2466   computeKnownBits(V, Known, Depth, Q);
2467   return Known.One != 0;
2468 }
2469 
2470 /// Return true if V2 == V1 + X, where X is known non-zero.
2471 static bool isAddOfNonZero(const Value *V1, const Value *V2, const Query &Q) {
2472   const BinaryOperator *BO = dyn_cast<BinaryOperator>(V1);
2473   if (!BO || BO->getOpcode() != Instruction::Add)
2474     return false;
2475   Value *Op = nullptr;
2476   if (V2 == BO->getOperand(0))
2477     Op = BO->getOperand(1);
2478   else if (V2 == BO->getOperand(1))
2479     Op = BO->getOperand(0);
2480   else
2481     return false;
2482   return isKnownNonZero(Op, 0, Q);
2483 }
2484 
2485 /// Return true if it is known that V1 != V2.
2486 static bool isKnownNonEqual(const Value *V1, const Value *V2, const Query &Q) {
2487   if (V1 == V2)
2488     return false;
2489   if (V1->getType() != V2->getType())
2490     // We can't look through casts yet.
2491     return false;
2492   if (isAddOfNonZero(V1, V2, Q) || isAddOfNonZero(V2, V1, Q))
2493     return true;
2494 
2495   if (V1->getType()->isIntOrIntVectorTy()) {
2496     // Are any known bits in V1 contradictory to known bits in V2? If V1
2497     // has a known zero where V2 has a known one, they must not be equal.
2498     KnownBits Known1 = computeKnownBits(V1, 0, Q);
2499     KnownBits Known2 = computeKnownBits(V2, 0, Q);
2500 
2501     if (Known1.Zero.intersects(Known2.One) ||
2502         Known2.Zero.intersects(Known1.One))
2503       return true;
2504   }
2505   return false;
2506 }
2507 
2508 /// Return true if 'V & Mask' is known to be zero.  We use this predicate to
2509 /// simplify operations downstream. Mask is known to be zero for bits that V
2510 /// cannot have.
2511 ///
2512 /// This function is defined on values with integer type, values with pointer
2513 /// type, and vectors of integers.  In the case
2514 /// where V is a vector, the mask, known zero, and known one values are the
2515 /// same width as the vector element, and the bit is set only if it is true
2516 /// for all of the elements in the vector.
2517 bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth,
2518                        const Query &Q) {
2519   KnownBits Known(Mask.getBitWidth());
2520   computeKnownBits(V, Known, Depth, Q);
2521   return Mask.isSubsetOf(Known.Zero);
2522 }
2523 
2524 // Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
2525 // Returns the input and lower/upper bounds.
2526 static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
2527                                 const APInt *&CLow, const APInt *&CHigh) {
2528   assert(isa<Operator>(Select) &&
2529          cast<Operator>(Select)->getOpcode() == Instruction::Select &&
2530          "Input should be a Select!");
2531 
2532   const Value *LHS = nullptr, *RHS = nullptr;
2533   SelectPatternFlavor SPF = matchSelectPattern(Select, LHS, RHS).Flavor;
2534   if (SPF != SPF_SMAX && SPF != SPF_SMIN)
2535     return false;
2536 
2537   if (!match(RHS, m_APInt(CLow)))
2538     return false;
2539 
2540   const Value *LHS2 = nullptr, *RHS2 = nullptr;
2541   SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor;
2542   if (getInverseMinMaxFlavor(SPF) != SPF2)
2543     return false;
2544 
2545   if (!match(RHS2, m_APInt(CHigh)))
2546     return false;
2547 
2548   if (SPF == SPF_SMIN)
2549     std::swap(CLow, CHigh);
2550 
2551   In = LHS2;
2552   return CLow->sle(*CHigh);
2553 }
2554 
2555 /// For vector constants, loop over the elements and find the constant with the
2556 /// minimum number of sign bits. Return 0 if the value is not a vector constant
2557 /// or if any element was not analyzed; otherwise, return the count for the
2558 /// element with the minimum number of sign bits.
2559 static unsigned computeNumSignBitsVectorConstant(const Value *V,
2560                                                  const APInt &DemandedElts,
2561                                                  unsigned TyBits) {
2562   const auto *CV = dyn_cast<Constant>(V);
2563   if (!CV || !CV->getType()->isVectorTy())
2564     return 0;
2565 
2566   unsigned MinSignBits = TyBits;
2567   unsigned NumElts = CV->getType()->getVectorNumElements();
2568   for (unsigned i = 0; i != NumElts; ++i) {
2569     if (!DemandedElts[i])
2570       continue;
2571     // If we find a non-ConstantInt, bail out.
2572     auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
2573     if (!Elt)
2574       return 0;
2575 
2576     MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
2577   }
2578 
2579   return MinSignBits;
2580 }
2581 
2582 static unsigned ComputeNumSignBitsImpl(const Value *V,
2583                                        const APInt &DemandedElts,
2584                                        unsigned Depth, const Query &Q);
2585 
2586 static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
2587                                    unsigned Depth, const Query &Q) {
2588   unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Depth, Q);
2589   assert(Result > 0 && "At least one sign bit needs to be present!");
2590   return Result;
2591 }
2592 
2593 /// Return the number of times the sign bit of the register is replicated into
2594 /// the other bits. We know that at least 1 bit is always equal to the sign bit
2595 /// (itself), but other cases can give us information. For example, immediately
2596 /// after an "ashr X, 2", we know that the top 3 bits are all equal to each
2597 /// other, so we return 3. For vectors, return the number of sign bits for the
2598 /// vector element with the minimum number of known sign bits of the demanded
2599 /// elements in the vector specified by DemandedElts.
2600 static unsigned ComputeNumSignBitsImpl(const Value *V,
2601                                        const APInt &DemandedElts,
2602                                        unsigned Depth, const Query &Q) {
2603   assert(Depth <= MaxDepth && "Limit Search Depth");
2604 
2605   // We return the minimum number of sign bits that are guaranteed to be present
2606   // in V, so for undef we have to conservatively return 1.  We don't have the
2607   // same behavior for poison though -- that's a FIXME today.
2608 
2609   Type *Ty = V->getType();
2610   assert(((Ty->isVectorTy() &&
2611            Ty->getVectorNumElements() == DemandedElts.getBitWidth()) ||
2612           (!Ty->isVectorTy() && DemandedElts == APInt(1, 1))) &&
2613          "Unexpected vector size");
2614 
2615   Type *ScalarTy = Ty->getScalarType();
2616   unsigned TyBits = ScalarTy->isPointerTy() ?
2617     Q.DL.getPointerTypeSizeInBits(ScalarTy) :
2618     Q.DL.getTypeSizeInBits(ScalarTy);
2619 
2620   unsigned Tmp, Tmp2;
2621   unsigned FirstAnswer = 1;
2622 
2623   // Note that ConstantInt is handled by the general computeKnownBits case
2624   // below.
2625 
2626   if (Depth == MaxDepth)
2627     return 1;  // Limit search depth.
2628 
2629   if (auto *U = dyn_cast<Operator>(V)) {
2630     switch (Operator::getOpcode(V)) {
2631     default: break;
2632     case Instruction::SExt:
2633       Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
2634       return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q) + Tmp;
2635 
2636     case Instruction::SDiv: {
2637       const APInt *Denominator;
2638       // sdiv X, C -> adds log(C) sign bits.
2639       if (match(U->getOperand(1), m_APInt(Denominator))) {
2640 
2641         // Ignore non-positive denominator.
2642         if (!Denominator->isStrictlyPositive())
2643           break;
2644 
2645         // Calculate the incoming numerator bits.
2646         unsigned NumBits = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
2647 
2648         // Add floor(log(C)) bits to the numerator bits.
2649         return std::min(TyBits, NumBits + Denominator->logBase2());
2650       }
2651       break;
2652     }
2653 
2654     case Instruction::SRem: {
2655       const APInt *Denominator;
2656       // srem X, C -> we know that the result is within [-C+1,C) when C is a
2657       // positive constant.  This let us put a lower bound on the number of sign
2658       // bits.
2659       if (match(U->getOperand(1), m_APInt(Denominator))) {
2660 
2661         // Ignore non-positive denominator.
2662         if (!Denominator->isStrictlyPositive())
2663           break;
2664 
2665         // Calculate the incoming numerator bits. SRem by a positive constant
2666         // can't lower the number of sign bits.
2667         unsigned NumrBits = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
2668 
2669         // Calculate the leading sign bit constraints by examining the
2670         // denominator.  Given that the denominator is positive, there are two
2671         // cases:
2672         //
2673         //  1. the numerator is positive. The result range is [0,C) and [0,C) u<
2674         //     (1 << ceilLogBase2(C)).
2675         //
2676         //  2. the numerator is negative. Then the result range is (-C,0] and
2677         //     integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
2678         //
2679         // Thus a lower bound on the number of sign bits is `TyBits -
2680         // ceilLogBase2(C)`.
2681 
2682         unsigned ResBits = TyBits - Denominator->ceilLogBase2();
2683         return std::max(NumrBits, ResBits);
2684       }
2685       break;
2686     }
2687 
2688     case Instruction::AShr: {
2689       Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
2690       // ashr X, C   -> adds C sign bits.  Vectors too.
2691       const APInt *ShAmt;
2692       if (match(U->getOperand(1), m_APInt(ShAmt))) {
2693         if (ShAmt->uge(TyBits))
2694           break; // Bad shift.
2695         unsigned ShAmtLimited = ShAmt->getZExtValue();
2696         Tmp += ShAmtLimited;
2697         if (Tmp > TyBits) Tmp = TyBits;
2698       }
2699       return Tmp;
2700     }
2701     case Instruction::Shl: {
2702       const APInt *ShAmt;
2703       if (match(U->getOperand(1), m_APInt(ShAmt))) {
2704         // shl destroys sign bits.
2705         Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
2706         if (ShAmt->uge(TyBits) ||   // Bad shift.
2707             ShAmt->uge(Tmp)) break; // Shifted all sign bits out.
2708         Tmp2 = ShAmt->getZExtValue();
2709         return Tmp - Tmp2;
2710       }
2711       break;
2712     }
2713     case Instruction::And:
2714     case Instruction::Or:
2715     case Instruction::Xor: // NOT is handled here.
2716       // Logical binary ops preserve the number of sign bits at the worst.
2717       Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
2718       if (Tmp != 1) {
2719         Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
2720         FirstAnswer = std::min(Tmp, Tmp2);
2721         // We computed what we know about the sign bits as our first
2722         // answer. Now proceed to the generic code that uses
2723         // computeKnownBits, and pick whichever answer is better.
2724       }
2725       break;
2726 
2727     case Instruction::Select: {
2728       // If we have a clamp pattern, we know that the number of sign bits will
2729       // be the minimum of the clamp min/max range.
2730       const Value *X;
2731       const APInt *CLow, *CHigh;
2732       if (isSignedMinMaxClamp(U, X, CLow, CHigh))
2733         return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
2734 
2735       Tmp = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
2736       if (Tmp == 1) break;
2737       Tmp2 = ComputeNumSignBits(U->getOperand(2), Depth + 1, Q);
2738       return std::min(Tmp, Tmp2);
2739     }
2740 
2741     case Instruction::Add:
2742       // Add can have at most one carry bit.  Thus we know that the output
2743       // is, at worst, one more bit than the inputs.
2744       Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
2745       if (Tmp == 1) break;
2746 
2747       // Special case decrementing a value (ADD X, -1):
2748       if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
2749         if (CRHS->isAllOnesValue()) {
2750           KnownBits Known(TyBits);
2751           computeKnownBits(U->getOperand(0), Known, Depth + 1, Q);
2752 
2753           // If the input is known to be 0 or 1, the output is 0/-1, which is
2754           // all sign bits set.
2755           if ((Known.Zero | 1).isAllOnesValue())
2756             return TyBits;
2757 
2758           // If we are subtracting one from a positive number, there is no carry
2759           // out of the result.
2760           if (Known.isNonNegative())
2761             return Tmp;
2762         }
2763 
2764       Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
2765       if (Tmp2 == 1) break;
2766       return std::min(Tmp, Tmp2) - 1;
2767 
2768     case Instruction::Sub:
2769       Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
2770       if (Tmp2 == 1) break;
2771 
2772       // Handle NEG.
2773       if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
2774         if (CLHS->isNullValue()) {
2775           KnownBits Known(TyBits);
2776           computeKnownBits(U->getOperand(1), Known, Depth + 1, Q);
2777           // If the input is known to be 0 or 1, the output is 0/-1, which is
2778           // all sign bits set.
2779           if ((Known.Zero | 1).isAllOnesValue())
2780             return TyBits;
2781 
2782           // If the input is known to be positive (the sign bit is known clear),
2783           // the output of the NEG has the same number of sign bits as the
2784           // input.
2785           if (Known.isNonNegative())
2786             return Tmp2;
2787 
2788           // Otherwise, we treat this like a SUB.
2789         }
2790 
2791       // Sub can have at most one carry bit.  Thus we know that the output
2792       // is, at worst, one more bit than the inputs.
2793       Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
2794       if (Tmp == 1) break;
2795       return std::min(Tmp, Tmp2) - 1;
2796 
2797     case Instruction::Mul: {
2798       // The output of the Mul can be at most twice the valid bits in the
2799       // inputs.
2800       unsigned SignBitsOp0 = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
2801       if (SignBitsOp0 == 1) break;
2802       unsigned SignBitsOp1 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
2803       if (SignBitsOp1 == 1) break;
2804       unsigned OutValidBits =
2805           (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
2806       return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
2807     }
2808 
2809     case Instruction::PHI: {
2810       const PHINode *PN = cast<PHINode>(U);
2811       unsigned NumIncomingValues = PN->getNumIncomingValues();
2812       // Don't analyze large in-degree PHIs.
2813       if (NumIncomingValues > 4) break;
2814       // Unreachable blocks may have zero-operand PHI nodes.
2815       if (NumIncomingValues == 0) break;
2816 
2817       // Take the minimum of all incoming values.  This can't infinitely loop
2818       // because of our depth threshold.
2819       Tmp = ComputeNumSignBits(PN->getIncomingValue(0), Depth + 1, Q);
2820       for (unsigned i = 1, e = NumIncomingValues; i != e; ++i) {
2821         if (Tmp == 1) return Tmp;
2822         Tmp = std::min(
2823             Tmp, ComputeNumSignBits(PN->getIncomingValue(i), Depth + 1, Q));
2824       }
2825       return Tmp;
2826     }
2827 
2828     case Instruction::Trunc:
2829       // FIXME: it's tricky to do anything useful for this, but it is an
2830       // important case for targets like X86.
2831       break;
2832 
2833     case Instruction::ExtractElement:
2834       // Look through extract element. At the moment we keep this simple and
2835       // skip tracking the specific element. But at least we might find
2836       // information valid for all elements of the vector (for example if vector
2837       // is sign extended, shifted, etc).
2838       return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
2839 
2840     case Instruction::ShuffleVector: {
2841       // Collect the minimum number of sign bits that are shared by every vector
2842       // element referenced by the shuffle.
2843       auto *Shuf = cast<ShuffleVectorInst>(U);
2844       APInt DemandedLHS, DemandedRHS;
2845       // For undef elements, we don't know anything about the common state of
2846       // the shuffle result.
2847       if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
2848         return 1;
2849       Tmp = std::numeric_limits<unsigned>::max();
2850       if (!!DemandedLHS) {
2851         const Value *LHS = Shuf->getOperand(0);
2852         Tmp = ComputeNumSignBits(LHS, DemandedLHS, Depth + 1, Q);
2853       }
2854       // If we don't know anything, early out and try computeKnownBits
2855       // fall-back.
2856       if (Tmp == 1)
2857         break;
2858       if (!!DemandedRHS) {
2859         const Value *RHS = Shuf->getOperand(1);
2860         Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Depth + 1, Q);
2861         Tmp = std::min(Tmp, Tmp2);
2862       }
2863       // If we don't know anything, early out and try computeKnownBits
2864       // fall-back.
2865       if (Tmp == 1)
2866         break;
2867       assert(Tmp <= Ty->getScalarSizeInBits() &&
2868              "Failed to determine minimum sign bits");
2869       return Tmp;
2870     }
2871     }
2872   }
2873 
2874   // Finally, if we can prove that the top bits of the result are 0's or 1's,
2875   // use this information.
2876 
2877   // If we can examine all elements of a vector constant successfully, we're
2878   // done (we can't do any better than that). If not, keep trying.
2879   if (unsigned VecSignBits =
2880           computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
2881     return VecSignBits;
2882 
2883   KnownBits Known(TyBits);
2884   computeKnownBits(V, DemandedElts, Known, Depth, Q);
2885 
2886   // If we know that the sign bit is either zero or one, determine the number of
2887   // identical bits in the top of the input value.
2888   return std::max(FirstAnswer, Known.countMinSignBits());
2889 }
2890 
2891 /// This function computes the integer multiple of Base that equals V.
2892 /// If successful, it returns true and returns the multiple in
2893 /// Multiple. If unsuccessful, it returns false. It looks
2894 /// through SExt instructions only if LookThroughSExt is true.
2895 bool llvm::ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
2896                            bool LookThroughSExt, unsigned Depth) {
2897   assert(V && "No Value?");
2898   assert(Depth <= MaxDepth && "Limit Search Depth");
2899   assert(V->getType()->isIntegerTy() && "Not integer or pointer type!");
2900 
2901   Type *T = V->getType();
2902 
2903   ConstantInt *CI = dyn_cast<ConstantInt>(V);
2904 
2905   if (Base == 0)
2906     return false;
2907 
2908   if (Base == 1) {
2909     Multiple = V;
2910     return true;
2911   }
2912 
2913   ConstantExpr *CO = dyn_cast<ConstantExpr>(V);
2914   Constant *BaseVal = ConstantInt::get(T, Base);
2915   if (CO && CO == BaseVal) {
2916     // Multiple is 1.
2917     Multiple = ConstantInt::get(T, 1);
2918     return true;
2919   }
2920 
2921   if (CI && CI->getZExtValue() % Base == 0) {
2922     Multiple = ConstantInt::get(T, CI->getZExtValue() / Base);
2923     return true;
2924   }
2925 
2926   if (Depth == MaxDepth) return false;  // Limit search depth.
2927 
2928   Operator *I = dyn_cast<Operator>(V);
2929   if (!I) return false;
2930 
2931   switch (I->getOpcode()) {
2932   default: break;
2933   case Instruction::SExt:
2934     if (!LookThroughSExt) return false;
2935     // otherwise fall through to ZExt
2936     LLVM_FALLTHROUGH;
2937   case Instruction::ZExt:
2938     return ComputeMultiple(I->getOperand(0), Base, Multiple,
2939                            LookThroughSExt, Depth+1);
2940   case Instruction::Shl:
2941   case Instruction::Mul: {
2942     Value *Op0 = I->getOperand(0);
2943     Value *Op1 = I->getOperand(1);
2944 
2945     if (I->getOpcode() == Instruction::Shl) {
2946       ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1);
2947       if (!Op1CI) return false;
2948       // Turn Op0 << Op1 into Op0 * 2^Op1
2949       APInt Op1Int = Op1CI->getValue();
2950       uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1);
2951       APInt API(Op1Int.getBitWidth(), 0);
2952       API.setBit(BitToSet);
2953       Op1 = ConstantInt::get(V->getContext(), API);
2954     }
2955 
2956     Value *Mul0 = nullptr;
2957     if (ComputeMultiple(Op0, Base, Mul0, LookThroughSExt, Depth+1)) {
2958       if (Constant *Op1C = dyn_cast<Constant>(Op1))
2959         if (Constant *MulC = dyn_cast<Constant>(Mul0)) {
2960           if (Op1C->getType()->getPrimitiveSizeInBits() <
2961               MulC->getType()->getPrimitiveSizeInBits())
2962             Op1C = ConstantExpr::getZExt(Op1C, MulC->getType());
2963           if (Op1C->getType()->getPrimitiveSizeInBits() >
2964               MulC->getType()->getPrimitiveSizeInBits())
2965             MulC = ConstantExpr::getZExt(MulC, Op1C->getType());
2966 
2967           // V == Base * (Mul0 * Op1), so return (Mul0 * Op1)
2968           Multiple = ConstantExpr::getMul(MulC, Op1C);
2969           return true;
2970         }
2971 
2972       if (ConstantInt *Mul0CI = dyn_cast<ConstantInt>(Mul0))
2973         if (Mul0CI->getValue() == 1) {
2974           // V == Base * Op1, so return Op1
2975           Multiple = Op1;
2976           return true;
2977         }
2978     }
2979 
2980     Value *Mul1 = nullptr;
2981     if (ComputeMultiple(Op1, Base, Mul1, LookThroughSExt, Depth+1)) {
2982       if (Constant *Op0C = dyn_cast<Constant>(Op0))
2983         if (Constant *MulC = dyn_cast<Constant>(Mul1)) {
2984           if (Op0C->getType()->getPrimitiveSizeInBits() <
2985               MulC->getType()->getPrimitiveSizeInBits())
2986             Op0C = ConstantExpr::getZExt(Op0C, MulC->getType());
2987           if (Op0C->getType()->getPrimitiveSizeInBits() >
2988               MulC->getType()->getPrimitiveSizeInBits())
2989             MulC = ConstantExpr::getZExt(MulC, Op0C->getType());
2990 
2991           // V == Base * (Mul1 * Op0), so return (Mul1 * Op0)
2992           Multiple = ConstantExpr::getMul(MulC, Op0C);
2993           return true;
2994         }
2995 
2996       if (ConstantInt *Mul1CI = dyn_cast<ConstantInt>(Mul1))
2997         if (Mul1CI->getValue() == 1) {
2998           // V == Base * Op0, so return Op0
2999           Multiple = Op0;
3000           return true;
3001         }
3002     }
3003   }
3004   }
3005 
3006   // We could not determine if V is a multiple of Base.
3007   return false;
3008 }
3009 
3010 Intrinsic::ID llvm::getIntrinsicForCallSite(ImmutableCallSite ICS,
3011                                             const TargetLibraryInfo *TLI) {
3012   const Function *F = ICS.getCalledFunction();
3013   if (!F)
3014     return Intrinsic::not_intrinsic;
3015 
3016   if (F->isIntrinsic())
3017     return F->getIntrinsicID();
3018 
3019   if (!TLI)
3020     return Intrinsic::not_intrinsic;
3021 
3022   LibFunc Func;
3023   // We're going to make assumptions on the semantics of the functions, check
3024   // that the target knows that it's available in this environment and it does
3025   // not have local linkage.
3026   if (!F || F->hasLocalLinkage() || !TLI->getLibFunc(*F, Func))
3027     return Intrinsic::not_intrinsic;
3028 
3029   if (!ICS.onlyReadsMemory())
3030     return Intrinsic::not_intrinsic;
3031 
3032   // Otherwise check if we have a call to a function that can be turned into a
3033   // vector intrinsic.
3034   switch (Func) {
3035   default:
3036     break;
3037   case LibFunc_sin:
3038   case LibFunc_sinf:
3039   case LibFunc_sinl:
3040     return Intrinsic::sin;
3041   case LibFunc_cos:
3042   case LibFunc_cosf:
3043   case LibFunc_cosl:
3044     return Intrinsic::cos;
3045   case LibFunc_exp:
3046   case LibFunc_expf:
3047   case LibFunc_expl:
3048     return Intrinsic::exp;
3049   case LibFunc_exp2:
3050   case LibFunc_exp2f:
3051   case LibFunc_exp2l:
3052     return Intrinsic::exp2;
3053   case LibFunc_log:
3054   case LibFunc_logf:
3055   case LibFunc_logl:
3056     return Intrinsic::log;
3057   case LibFunc_log10:
3058   case LibFunc_log10f:
3059   case LibFunc_log10l:
3060     return Intrinsic::log10;
3061   case LibFunc_log2:
3062   case LibFunc_log2f:
3063   case LibFunc_log2l:
3064     return Intrinsic::log2;
3065   case LibFunc_fabs:
3066   case LibFunc_fabsf:
3067   case LibFunc_fabsl:
3068     return Intrinsic::fabs;
3069   case LibFunc_fmin:
3070   case LibFunc_fminf:
3071   case LibFunc_fminl:
3072     return Intrinsic::minnum;
3073   case LibFunc_fmax:
3074   case LibFunc_fmaxf:
3075   case LibFunc_fmaxl:
3076     return Intrinsic::maxnum;
3077   case LibFunc_copysign:
3078   case LibFunc_copysignf:
3079   case LibFunc_copysignl:
3080     return Intrinsic::copysign;
3081   case LibFunc_floor:
3082   case LibFunc_floorf:
3083   case LibFunc_floorl:
3084     return Intrinsic::floor;
3085   case LibFunc_ceil:
3086   case LibFunc_ceilf:
3087   case LibFunc_ceill:
3088     return Intrinsic::ceil;
3089   case LibFunc_trunc:
3090   case LibFunc_truncf:
3091   case LibFunc_truncl:
3092     return Intrinsic::trunc;
3093   case LibFunc_rint:
3094   case LibFunc_rintf:
3095   case LibFunc_rintl:
3096     return Intrinsic::rint;
3097   case LibFunc_nearbyint:
3098   case LibFunc_nearbyintf:
3099   case LibFunc_nearbyintl:
3100     return Intrinsic::nearbyint;
3101   case LibFunc_round:
3102   case LibFunc_roundf:
3103   case LibFunc_roundl:
3104     return Intrinsic::round;
3105   case LibFunc_pow:
3106   case LibFunc_powf:
3107   case LibFunc_powl:
3108     return Intrinsic::pow;
3109   case LibFunc_sqrt:
3110   case LibFunc_sqrtf:
3111   case LibFunc_sqrtl:
3112     return Intrinsic::sqrt;
3113   }
3114 
3115   return Intrinsic::not_intrinsic;
3116 }
3117 
3118 /// Return true if we can prove that the specified FP value is never equal to
3119 /// -0.0.
3120 ///
3121 /// NOTE: this function will need to be revisited when we support non-default
3122 /// rounding modes!
3123 bool llvm::CannotBeNegativeZero(const Value *V, const TargetLibraryInfo *TLI,
3124                                 unsigned Depth) {
3125   if (auto *CFP = dyn_cast<ConstantFP>(V))
3126     return !CFP->getValueAPF().isNegZero();
3127 
3128   // Limit search depth.
3129   if (Depth == MaxDepth)
3130     return false;
3131 
3132   auto *Op = dyn_cast<Operator>(V);
3133   if (!Op)
3134     return false;
3135 
3136   // Check if the nsz fast-math flag is set.
3137   if (auto *FPO = dyn_cast<FPMathOperator>(Op))
3138     if (FPO->hasNoSignedZeros())
3139       return true;
3140 
3141   // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
3142   if (match(Op, m_FAdd(m_Value(), m_PosZeroFP())))
3143     return true;
3144 
3145   // sitofp and uitofp turn into +0.0 for zero.
3146   if (isa<SIToFPInst>(Op) || isa<UIToFPInst>(Op))
3147     return true;
3148 
3149   if (auto *Call = dyn_cast<CallInst>(Op)) {
3150     Intrinsic::ID IID = getIntrinsicForCallSite(Call, TLI);
3151     switch (IID) {
3152     default:
3153       break;
3154     // sqrt(-0.0) = -0.0, no other negative results are possible.
3155     case Intrinsic::sqrt:
3156     case Intrinsic::canonicalize:
3157       return CannotBeNegativeZero(Call->getArgOperand(0), TLI, Depth + 1);
3158     // fabs(x) != -0.0
3159     case Intrinsic::fabs:
3160       return true;
3161     }
3162   }
3163 
3164   return false;
3165 }
3166 
3167 /// If \p SignBitOnly is true, test for a known 0 sign bit rather than a
3168 /// standard ordered compare. e.g. make -0.0 olt 0.0 be true because of the sign
3169 /// bit despite comparing equal.
3170 static bool cannotBeOrderedLessThanZeroImpl(const Value *V,
3171                                             const TargetLibraryInfo *TLI,
3172                                             bool SignBitOnly,
3173                                             unsigned Depth) {
3174   // TODO: This function does not do the right thing when SignBitOnly is true
3175   // and we're lowering to a hypothetical IEEE 754-compliant-but-evil platform
3176   // which flips the sign bits of NaNs.  See
3177   // https://llvm.org/bugs/show_bug.cgi?id=31702.
3178 
3179   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
3180     return !CFP->getValueAPF().isNegative() ||
3181            (!SignBitOnly && CFP->getValueAPF().isZero());
3182   }
3183 
3184   // Handle vector of constants.
3185   if (auto *CV = dyn_cast<Constant>(V)) {
3186     if (CV->getType()->isVectorTy()) {
3187       unsigned NumElts = CV->getType()->getVectorNumElements();
3188       for (unsigned i = 0; i != NumElts; ++i) {
3189         auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i));
3190         if (!CFP)
3191           return false;
3192         if (CFP->getValueAPF().isNegative() &&
3193             (SignBitOnly || !CFP->getValueAPF().isZero()))
3194           return false;
3195       }
3196 
3197       // All non-negative ConstantFPs.
3198       return true;
3199     }
3200   }
3201 
3202   if (Depth == MaxDepth)
3203     return false; // Limit search depth.
3204 
3205   const Operator *I = dyn_cast<Operator>(V);
3206   if (!I)
3207     return false;
3208 
3209   switch (I->getOpcode()) {
3210   default:
3211     break;
3212   // Unsigned integers are always nonnegative.
3213   case Instruction::UIToFP:
3214     return true;
3215   case Instruction::FMul:
3216     // x*x is always non-negative or a NaN.
3217     if (I->getOperand(0) == I->getOperand(1) &&
3218         (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs()))
3219       return true;
3220 
3221     LLVM_FALLTHROUGH;
3222   case Instruction::FAdd:
3223   case Instruction::FDiv:
3224   case Instruction::FRem:
3225     return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
3226                                            Depth + 1) &&
3227            cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
3228                                            Depth + 1);
3229   case Instruction::Select:
3230     return cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
3231                                            Depth + 1) &&
3232            cannotBeOrderedLessThanZeroImpl(I->getOperand(2), TLI, SignBitOnly,
3233                                            Depth + 1);
3234   case Instruction::FPExt:
3235   case Instruction::FPTrunc:
3236     // Widening/narrowing never change sign.
3237     return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
3238                                            Depth + 1);
3239   case Instruction::ExtractElement:
3240     // Look through extract element. At the moment we keep this simple and skip
3241     // tracking the specific element. But at least we might find information
3242     // valid for all elements of the vector.
3243     return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
3244                                            Depth + 1);
3245   case Instruction::Call:
3246     const auto *CI = cast<CallInst>(I);
3247     Intrinsic::ID IID = getIntrinsicForCallSite(CI, TLI);
3248     switch (IID) {
3249     default:
3250       break;
3251     case Intrinsic::maxnum:
3252       return (isKnownNeverNaN(I->getOperand(0), TLI) &&
3253               cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI,
3254                                               SignBitOnly, Depth + 1)) ||
3255             (isKnownNeverNaN(I->getOperand(1), TLI) &&
3256               cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI,
3257                                               SignBitOnly, Depth + 1));
3258 
3259     case Intrinsic::maximum:
3260       return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
3261                                              Depth + 1) ||
3262              cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
3263                                              Depth + 1);
3264     case Intrinsic::minnum:
3265     case Intrinsic::minimum:
3266       return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
3267                                              Depth + 1) &&
3268              cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
3269                                              Depth + 1);
3270     case Intrinsic::exp:
3271     case Intrinsic::exp2:
3272     case Intrinsic::fabs:
3273       return true;
3274 
3275     case Intrinsic::sqrt:
3276       // sqrt(x) is always >= -0 or NaN.  Moreover, sqrt(x) == -0 iff x == -0.
3277       if (!SignBitOnly)
3278         return true;
3279       return CI->hasNoNaNs() && (CI->hasNoSignedZeros() ||
3280                                  CannotBeNegativeZero(CI->getOperand(0), TLI));
3281 
3282     case Intrinsic::powi:
3283       if (ConstantInt *Exponent = dyn_cast<ConstantInt>(I->getOperand(1))) {
3284         // powi(x,n) is non-negative if n is even.
3285         if (Exponent->getBitWidth() <= 64 && Exponent->getSExtValue() % 2u == 0)
3286           return true;
3287       }
3288       // TODO: This is not correct.  Given that exp is an integer, here are the
3289       // ways that pow can return a negative value:
3290       //
3291       //   pow(x, exp)    --> negative if exp is odd and x is negative.
3292       //   pow(-0, exp)   --> -inf if exp is negative odd.
3293       //   pow(-0, exp)   --> -0 if exp is positive odd.
3294       //   pow(-inf, exp) --> -0 if exp is negative odd.
3295       //   pow(-inf, exp) --> -inf if exp is positive odd.
3296       //
3297       // Therefore, if !SignBitOnly, we can return true if x >= +0 or x is NaN,
3298       // but we must return false if x == -0.  Unfortunately we do not currently
3299       // have a way of expressing this constraint.  See details in
3300       // https://llvm.org/bugs/show_bug.cgi?id=31702.
3301       return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
3302                                              Depth + 1);
3303 
3304     case Intrinsic::fma:
3305     case Intrinsic::fmuladd:
3306       // x*x+y is non-negative if y is non-negative.
3307       return I->getOperand(0) == I->getOperand(1) &&
3308              (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs()) &&
3309              cannotBeOrderedLessThanZeroImpl(I->getOperand(2), TLI, SignBitOnly,
3310                                              Depth + 1);
3311     }
3312     break;
3313   }
3314   return false;
3315 }
3316 
3317 bool llvm::CannotBeOrderedLessThanZero(const Value *V,
3318                                        const TargetLibraryInfo *TLI) {
3319   return cannotBeOrderedLessThanZeroImpl(V, TLI, false, 0);
3320 }
3321 
3322 bool llvm::SignBitMustBeZero(const Value *V, const TargetLibraryInfo *TLI) {
3323   return cannotBeOrderedLessThanZeroImpl(V, TLI, true, 0);
3324 }
3325 
3326 bool llvm::isKnownNeverInfinity(const Value *V, const TargetLibraryInfo *TLI,
3327                                 unsigned Depth) {
3328   assert(V->getType()->isFPOrFPVectorTy() && "Querying for Inf on non-FP type");
3329 
3330   // If we're told that infinities won't happen, assume they won't.
3331   if (auto *FPMathOp = dyn_cast<FPMathOperator>(V))
3332     if (FPMathOp->hasNoInfs())
3333       return true;
3334 
3335   // Handle scalar constants.
3336   if (auto *CFP = dyn_cast<ConstantFP>(V))
3337     return !CFP->isInfinity();
3338 
3339   if (Depth == MaxDepth)
3340     return false;
3341 
3342   if (auto *Inst = dyn_cast<Instruction>(V)) {
3343     switch (Inst->getOpcode()) {
3344     case Instruction::Select: {
3345       return isKnownNeverInfinity(Inst->getOperand(1), TLI, Depth + 1) &&
3346              isKnownNeverInfinity(Inst->getOperand(2), TLI, Depth + 1);
3347     }
3348     case Instruction::UIToFP:
3349       // If the input type fits into the floating type the result is finite.
3350       return ilogb(APFloat::getLargest(
3351                  Inst->getType()->getScalarType()->getFltSemantics())) >=
3352              (int)Inst->getOperand(0)->getType()->getScalarSizeInBits();
3353     default:
3354       break;
3355     }
3356   }
3357 
3358   // Bail out for constant expressions, but try to handle vector constants.
3359   if (!V->getType()->isVectorTy() || !isa<Constant>(V))
3360     return false;
3361 
3362   // For vectors, verify that each element is not infinity.
3363   unsigned NumElts = V->getType()->getVectorNumElements();
3364   for (unsigned i = 0; i != NumElts; ++i) {
3365     Constant *Elt = cast<Constant>(V)->getAggregateElement(i);
3366     if (!Elt)
3367       return false;
3368     if (isa<UndefValue>(Elt))
3369       continue;
3370     auto *CElt = dyn_cast<ConstantFP>(Elt);
3371     if (!CElt || CElt->isInfinity())
3372       return false;
3373   }
3374   // All elements were confirmed non-infinity or undefined.
3375   return true;
3376 }
3377 
3378 bool llvm::isKnownNeverNaN(const Value *V, const TargetLibraryInfo *TLI,
3379                            unsigned Depth) {
3380   assert(V->getType()->isFPOrFPVectorTy() && "Querying for NaN on non-FP type");
3381 
3382   // If we're told that NaNs won't happen, assume they won't.
3383   if (auto *FPMathOp = dyn_cast<FPMathOperator>(V))
3384     if (FPMathOp->hasNoNaNs())
3385       return true;
3386 
3387   // Handle scalar constants.
3388   if (auto *CFP = dyn_cast<ConstantFP>(V))
3389     return !CFP->isNaN();
3390 
3391   if (Depth == MaxDepth)
3392     return false;
3393 
3394   if (auto *Inst = dyn_cast<Instruction>(V)) {
3395     switch (Inst->getOpcode()) {
3396     case Instruction::FAdd:
3397     case Instruction::FSub:
3398       // Adding positive and negative infinity produces NaN.
3399       return isKnownNeverNaN(Inst->getOperand(0), TLI, Depth + 1) &&
3400              isKnownNeverNaN(Inst->getOperand(1), TLI, Depth + 1) &&
3401              (isKnownNeverInfinity(Inst->getOperand(0), TLI, Depth + 1) ||
3402               isKnownNeverInfinity(Inst->getOperand(1), TLI, Depth + 1));
3403 
3404     case Instruction::FMul:
3405       // Zero multiplied with infinity produces NaN.
3406       // FIXME: If neither side can be zero fmul never produces NaN.
3407       return isKnownNeverNaN(Inst->getOperand(0), TLI, Depth + 1) &&
3408              isKnownNeverInfinity(Inst->getOperand(0), TLI, Depth + 1) &&
3409              isKnownNeverNaN(Inst->getOperand(1), TLI, Depth + 1) &&
3410              isKnownNeverInfinity(Inst->getOperand(1), TLI, Depth + 1);
3411 
3412     case Instruction::FDiv:
3413     case Instruction::FRem:
3414       // FIXME: Only 0/0, Inf/Inf, Inf REM x and x REM 0 produce NaN.
3415       return false;
3416 
3417     case Instruction::Select: {
3418       return isKnownNeverNaN(Inst->getOperand(1), TLI, Depth + 1) &&
3419              isKnownNeverNaN(Inst->getOperand(2), TLI, Depth + 1);
3420     }
3421     case Instruction::SIToFP:
3422     case Instruction::UIToFP:
3423       return true;
3424     case Instruction::FPTrunc:
3425     case Instruction::FPExt:
3426       return isKnownNeverNaN(Inst->getOperand(0), TLI, Depth + 1);
3427     default:
3428       break;
3429     }
3430   }
3431 
3432   if (const auto *II = dyn_cast<IntrinsicInst>(V)) {
3433     switch (II->getIntrinsicID()) {
3434     case Intrinsic::canonicalize:
3435     case Intrinsic::fabs:
3436     case Intrinsic::copysign:
3437     case Intrinsic::exp:
3438     case Intrinsic::exp2:
3439     case Intrinsic::floor:
3440     case Intrinsic::ceil:
3441     case Intrinsic::trunc:
3442     case Intrinsic::rint:
3443     case Intrinsic::nearbyint:
3444     case Intrinsic::round:
3445       return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1);
3446     case Intrinsic::sqrt:
3447       return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1) &&
3448              CannotBeOrderedLessThanZero(II->getArgOperand(0), TLI);
3449     case Intrinsic::minnum:
3450     case Intrinsic::maxnum:
3451       // If either operand is not NaN, the result is not NaN.
3452       return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1) ||
3453              isKnownNeverNaN(II->getArgOperand(1), TLI, Depth + 1);
3454     default:
3455       return false;
3456     }
3457   }
3458 
3459   // Bail out for constant expressions, but try to handle vector constants.
3460   if (!V->getType()->isVectorTy() || !isa<Constant>(V))
3461     return false;
3462 
3463   // For vectors, verify that each element is not NaN.
3464   unsigned NumElts = V->getType()->getVectorNumElements();
3465   for (unsigned i = 0; i != NumElts; ++i) {
3466     Constant *Elt = cast<Constant>(V)->getAggregateElement(i);
3467     if (!Elt)
3468       return false;
3469     if (isa<UndefValue>(Elt))
3470       continue;
3471     auto *CElt = dyn_cast<ConstantFP>(Elt);
3472     if (!CElt || CElt->isNaN())
3473       return false;
3474   }
3475   // All elements were confirmed not-NaN or undefined.
3476   return true;
3477 }
3478 
3479 Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) {
3480 
3481   // All byte-wide stores are splatable, even of arbitrary variables.
3482   if (V->getType()->isIntegerTy(8))
3483     return V;
3484 
3485   LLVMContext &Ctx = V->getContext();
3486 
3487   // Undef don't care.
3488   auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
3489   if (isa<UndefValue>(V))
3490     return UndefInt8;
3491 
3492   const uint64_t Size = DL.getTypeStoreSize(V->getType());
3493   if (!Size)
3494     return UndefInt8;
3495 
3496   Constant *C = dyn_cast<Constant>(V);
3497   if (!C) {
3498     // Conceptually, we could handle things like:
3499     //   %a = zext i8 %X to i16
3500     //   %b = shl i16 %a, 8
3501     //   %c = or i16 %a, %b
3502     // but until there is an example that actually needs this, it doesn't seem
3503     // worth worrying about.
3504     return nullptr;
3505   }
3506 
3507   // Handle 'null' ConstantArrayZero etc.
3508   if (C->isNullValue())
3509     return Constant::getNullValue(Type::getInt8Ty(Ctx));
3510 
3511   // Constant floating-point values can be handled as integer values if the
3512   // corresponding integer value is "byteable".  An important case is 0.0.
3513   if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3514     Type *Ty = nullptr;
3515     if (CFP->getType()->isHalfTy())
3516       Ty = Type::getInt16Ty(Ctx);
3517     else if (CFP->getType()->isFloatTy())
3518       Ty = Type::getInt32Ty(Ctx);
3519     else if (CFP->getType()->isDoubleTy())
3520       Ty = Type::getInt64Ty(Ctx);
3521     // Don't handle long double formats, which have strange constraints.
3522     return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL)
3523               : nullptr;
3524   }
3525 
3526   // We can handle constant integers that are multiple of 8 bits.
3527   if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
3528     if (CI->getBitWidth() % 8 == 0) {
3529       assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
3530       if (!CI->getValue().isSplat(8))
3531         return nullptr;
3532       return ConstantInt::get(Ctx, CI->getValue().trunc(8));
3533     }
3534   }
3535 
3536   if (auto *CE = dyn_cast<ConstantExpr>(C)) {
3537     if (CE->getOpcode() == Instruction::IntToPtr) {
3538       auto PS = DL.getPointerSizeInBits(
3539           cast<PointerType>(CE->getType())->getAddressSpace());
3540       return isBytewiseValue(
3541           ConstantExpr::getIntegerCast(CE->getOperand(0),
3542                                        Type::getIntNTy(Ctx, PS), false),
3543           DL);
3544     }
3545   }
3546 
3547   auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
3548     if (LHS == RHS)
3549       return LHS;
3550     if (!LHS || !RHS)
3551       return nullptr;
3552     if (LHS == UndefInt8)
3553       return RHS;
3554     if (RHS == UndefInt8)
3555       return LHS;
3556     return nullptr;
3557   };
3558 
3559   if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) {
3560     Value *Val = UndefInt8;
3561     for (unsigned I = 0, E = CA->getNumElements(); I != E; ++I)
3562       if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
3563         return nullptr;
3564     return Val;
3565   }
3566 
3567   if (isa<ConstantAggregate>(C)) {
3568     Value *Val = UndefInt8;
3569     for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I)
3570       if (!(Val = Merge(Val, isBytewiseValue(C->getOperand(I), DL))))
3571         return nullptr;
3572     return Val;
3573   }
3574 
3575   // Don't try to handle the handful of other constants.
3576   return nullptr;
3577 }
3578 
3579 // This is the recursive version of BuildSubAggregate. It takes a few different
3580 // arguments. Idxs is the index within the nested struct From that we are
3581 // looking at now (which is of type IndexedType). IdxSkip is the number of
3582 // indices from Idxs that should be left out when inserting into the resulting
3583 // struct. To is the result struct built so far, new insertvalue instructions
3584 // build on that.
3585 static Value *BuildSubAggregate(Value *From, Value* To, Type *IndexedType,
3586                                 SmallVectorImpl<unsigned> &Idxs,
3587                                 unsigned IdxSkip,
3588                                 Instruction *InsertBefore) {
3589   StructType *STy = dyn_cast<StructType>(IndexedType);
3590   if (STy) {
3591     // Save the original To argument so we can modify it
3592     Value *OrigTo = To;
3593     // General case, the type indexed by Idxs is a struct
3594     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
3595       // Process each struct element recursively
3596       Idxs.push_back(i);
3597       Value *PrevTo = To;
3598       To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
3599                              InsertBefore);
3600       Idxs.pop_back();
3601       if (!To) {
3602         // Couldn't find any inserted value for this index? Cleanup
3603         while (PrevTo != OrigTo) {
3604           InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
3605           PrevTo = Del->getAggregateOperand();
3606           Del->eraseFromParent();
3607         }
3608         // Stop processing elements
3609         break;
3610       }
3611     }
3612     // If we successfully found a value for each of our subaggregates
3613     if (To)
3614       return To;
3615   }
3616   // Base case, the type indexed by SourceIdxs is not a struct, or not all of
3617   // the struct's elements had a value that was inserted directly. In the latter
3618   // case, perhaps we can't determine each of the subelements individually, but
3619   // we might be able to find the complete struct somewhere.
3620 
3621   // Find the value that is at that particular spot
3622   Value *V = FindInsertedValue(From, Idxs);
3623 
3624   if (!V)
3625     return nullptr;
3626 
3627   // Insert the value in the new (sub) aggregate
3628   return InsertValueInst::Create(To, V, makeArrayRef(Idxs).slice(IdxSkip),
3629                                  "tmp", InsertBefore);
3630 }
3631 
3632 // This helper takes a nested struct and extracts a part of it (which is again a
3633 // struct) into a new value. For example, given the struct:
3634 // { a, { b, { c, d }, e } }
3635 // and the indices "1, 1" this returns
3636 // { c, d }.
3637 //
3638 // It does this by inserting an insertvalue for each element in the resulting
3639 // struct, as opposed to just inserting a single struct. This will only work if
3640 // each of the elements of the substruct are known (ie, inserted into From by an
3641 // insertvalue instruction somewhere).
3642 //
3643 // All inserted insertvalue instructions are inserted before InsertBefore
3644 static Value *BuildSubAggregate(Value *From, ArrayRef<unsigned> idx_range,
3645                                 Instruction *InsertBefore) {
3646   assert(InsertBefore && "Must have someplace to insert!");
3647   Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
3648                                                              idx_range);
3649   Value *To = UndefValue::get(IndexedType);
3650   SmallVector<unsigned, 10> Idxs(idx_range.begin(), idx_range.end());
3651   unsigned IdxSkip = Idxs.size();
3652 
3653   return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
3654 }
3655 
3656 /// Given an aggregate and a sequence of indices, see if the scalar value
3657 /// indexed is already around as a register, for example if it was inserted
3658 /// directly into the aggregate.
3659 ///
3660 /// If InsertBefore is not null, this function will duplicate (modified)
3661 /// insertvalues when a part of a nested struct is extracted.
3662 Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range,
3663                                Instruction *InsertBefore) {
3664   // Nothing to index? Just return V then (this is useful at the end of our
3665   // recursion).
3666   if (idx_range.empty())
3667     return V;
3668   // We have indices, so V should have an indexable type.
3669   assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
3670          "Not looking at a struct or array?");
3671   assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
3672          "Invalid indices for type?");
3673 
3674   if (Constant *C = dyn_cast<Constant>(V)) {
3675     C = C->getAggregateElement(idx_range[0]);
3676     if (!C) return nullptr;
3677     return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
3678   }
3679 
3680   if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
3681     // Loop the indices for the insertvalue instruction in parallel with the
3682     // requested indices
3683     const unsigned *req_idx = idx_range.begin();
3684     for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
3685          i != e; ++i, ++req_idx) {
3686       if (req_idx == idx_range.end()) {
3687         // We can't handle this without inserting insertvalues
3688         if (!InsertBefore)
3689           return nullptr;
3690 
3691         // The requested index identifies a part of a nested aggregate. Handle
3692         // this specially. For example,
3693         // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
3694         // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
3695         // %C = extractvalue {i32, { i32, i32 } } %B, 1
3696         // This can be changed into
3697         // %A = insertvalue {i32, i32 } undef, i32 10, 0
3698         // %C = insertvalue {i32, i32 } %A, i32 11, 1
3699         // which allows the unused 0,0 element from the nested struct to be
3700         // removed.
3701         return BuildSubAggregate(V, makeArrayRef(idx_range.begin(), req_idx),
3702                                  InsertBefore);
3703       }
3704 
3705       // This insert value inserts something else than what we are looking for.
3706       // See if the (aggregate) value inserted into has the value we are
3707       // looking for, then.
3708       if (*req_idx != *i)
3709         return FindInsertedValue(I->getAggregateOperand(), idx_range,
3710                                  InsertBefore);
3711     }
3712     // If we end up here, the indices of the insertvalue match with those
3713     // requested (though possibly only partially). Now we recursively look at
3714     // the inserted value, passing any remaining indices.
3715     return FindInsertedValue(I->getInsertedValueOperand(),
3716                              makeArrayRef(req_idx, idx_range.end()),
3717                              InsertBefore);
3718   }
3719 
3720   if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
3721     // If we're extracting a value from an aggregate that was extracted from
3722     // something else, we can extract from that something else directly instead.
3723     // However, we will need to chain I's indices with the requested indices.
3724 
3725     // Calculate the number of indices required
3726     unsigned size = I->getNumIndices() + idx_range.size();
3727     // Allocate some space to put the new indices in
3728     SmallVector<unsigned, 5> Idxs;
3729     Idxs.reserve(size);
3730     // Add indices from the extract value instruction
3731     Idxs.append(I->idx_begin(), I->idx_end());
3732 
3733     // Add requested indices
3734     Idxs.append(idx_range.begin(), idx_range.end());
3735 
3736     assert(Idxs.size() == size
3737            && "Number of indices added not correct?");
3738 
3739     return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
3740   }
3741   // Otherwise, we don't know (such as, extracting from a function return value
3742   // or load instruction)
3743   return nullptr;
3744 }
3745 
3746 bool llvm::isGEPBasedOnPointerToString(const GEPOperator *GEP,
3747                                        unsigned CharSize) {
3748   // Make sure the GEP has exactly three arguments.
3749   if (GEP->getNumOperands() != 3)
3750     return false;
3751 
3752   // Make sure the index-ee is a pointer to array of \p CharSize integers.
3753   // CharSize.
3754   ArrayType *AT = dyn_cast<ArrayType>(GEP->getSourceElementType());
3755   if (!AT || !AT->getElementType()->isIntegerTy(CharSize))
3756     return false;
3757 
3758   // Check to make sure that the first operand of the GEP is an integer and
3759   // has value 0 so that we are sure we're indexing into the initializer.
3760   const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
3761   if (!FirstIdx || !FirstIdx->isZero())
3762     return false;
3763 
3764   return true;
3765 }
3766 
3767 bool llvm::getConstantDataArrayInfo(const Value *V,
3768                                     ConstantDataArraySlice &Slice,
3769                                     unsigned ElementSize, uint64_t Offset) {
3770   assert(V);
3771 
3772   // Look through bitcast instructions and geps.
3773   V = V->stripPointerCasts();
3774 
3775   // If the value is a GEP instruction or constant expression, treat it as an
3776   // offset.
3777   if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
3778     // The GEP operator should be based on a pointer to string constant, and is
3779     // indexing into the string constant.
3780     if (!isGEPBasedOnPointerToString(GEP, ElementSize))
3781       return false;
3782 
3783     // If the second index isn't a ConstantInt, then this is a variable index
3784     // into the array.  If this occurs, we can't say anything meaningful about
3785     // the string.
3786     uint64_t StartIdx = 0;
3787     if (const ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
3788       StartIdx = CI->getZExtValue();
3789     else
3790       return false;
3791     return getConstantDataArrayInfo(GEP->getOperand(0), Slice, ElementSize,
3792                                     StartIdx + Offset);
3793   }
3794 
3795   // The GEP instruction, constant or instruction, must reference a global
3796   // variable that is a constant and is initialized. The referenced constant
3797   // initializer is the array that we'll use for optimization.
3798   const GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
3799   if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
3800     return false;
3801 
3802   const ConstantDataArray *Array;
3803   ArrayType *ArrayTy;
3804   if (GV->getInitializer()->isNullValue()) {
3805     Type *GVTy = GV->getValueType();
3806     if ( (ArrayTy = dyn_cast<ArrayType>(GVTy)) ) {
3807       // A zeroinitializer for the array; there is no ConstantDataArray.
3808       Array = nullptr;
3809     } else {
3810       const DataLayout &DL = GV->getParent()->getDataLayout();
3811       uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy);
3812       uint64_t Length = SizeInBytes / (ElementSize / 8);
3813       if (Length <= Offset)
3814         return false;
3815 
3816       Slice.Array = nullptr;
3817       Slice.Offset = 0;
3818       Slice.Length = Length - Offset;
3819       return true;
3820     }
3821   } else {
3822     // This must be a ConstantDataArray.
3823     Array = dyn_cast<ConstantDataArray>(GV->getInitializer());
3824     if (!Array)
3825       return false;
3826     ArrayTy = Array->getType();
3827   }
3828   if (!ArrayTy->getElementType()->isIntegerTy(ElementSize))
3829     return false;
3830 
3831   uint64_t NumElts = ArrayTy->getArrayNumElements();
3832   if (Offset > NumElts)
3833     return false;
3834 
3835   Slice.Array = Array;
3836   Slice.Offset = Offset;
3837   Slice.Length = NumElts - Offset;
3838   return true;
3839 }
3840 
3841 /// This function computes the length of a null-terminated C string pointed to
3842 /// by V. If successful, it returns true and returns the string in Str.
3843 /// If unsuccessful, it returns false.
3844 bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,
3845                                  uint64_t Offset, bool TrimAtNul) {
3846   ConstantDataArraySlice Slice;
3847   if (!getConstantDataArrayInfo(V, Slice, 8, Offset))
3848     return false;
3849 
3850   if (Slice.Array == nullptr) {
3851     if (TrimAtNul) {
3852       Str = StringRef();
3853       return true;
3854     }
3855     if (Slice.Length == 1) {
3856       Str = StringRef("", 1);
3857       return true;
3858     }
3859     // We cannot instantiate a StringRef as we do not have an appropriate string
3860     // of 0s at hand.
3861     return false;
3862   }
3863 
3864   // Start out with the entire array in the StringRef.
3865   Str = Slice.Array->getAsString();
3866   // Skip over 'offset' bytes.
3867   Str = Str.substr(Slice.Offset);
3868 
3869   if (TrimAtNul) {
3870     // Trim off the \0 and anything after it.  If the array is not nul
3871     // terminated, we just return the whole end of string.  The client may know
3872     // some other way that the string is length-bound.
3873     Str = Str.substr(0, Str.find('\0'));
3874   }
3875   return true;
3876 }
3877 
3878 // These next two are very similar to the above, but also look through PHI
3879 // nodes.
3880 // TODO: See if we can integrate these two together.
3881 
3882 /// If we can compute the length of the string pointed to by
3883 /// the specified pointer, return 'len+1'.  If we can't, return 0.
3884 static uint64_t GetStringLengthH(const Value *V,
3885                                  SmallPtrSetImpl<const PHINode*> &PHIs,
3886                                  unsigned CharSize) {
3887   // Look through noop bitcast instructions.
3888   V = V->stripPointerCasts();
3889 
3890   // If this is a PHI node, there are two cases: either we have already seen it
3891   // or we haven't.
3892   if (const PHINode *PN = dyn_cast<PHINode>(V)) {
3893     if (!PHIs.insert(PN).second)
3894       return ~0ULL;  // already in the set.
3895 
3896     // If it was new, see if all the input strings are the same length.
3897     uint64_t LenSoFar = ~0ULL;
3898     for (Value *IncValue : PN->incoming_values()) {
3899       uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
3900       if (Len == 0) return 0; // Unknown length -> unknown.
3901 
3902       if (Len == ~0ULL) continue;
3903 
3904       if (Len != LenSoFar && LenSoFar != ~0ULL)
3905         return 0;    // Disagree -> unknown.
3906       LenSoFar = Len;
3907     }
3908 
3909     // Success, all agree.
3910     return LenSoFar;
3911   }
3912 
3913   // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
3914   if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
3915     uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
3916     if (Len1 == 0) return 0;
3917     uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
3918     if (Len2 == 0) return 0;
3919     if (Len1 == ~0ULL) return Len2;
3920     if (Len2 == ~0ULL) return Len1;
3921     if (Len1 != Len2) return 0;
3922     return Len1;
3923   }
3924 
3925   // Otherwise, see if we can read the string.
3926   ConstantDataArraySlice Slice;
3927   if (!getConstantDataArrayInfo(V, Slice, CharSize))
3928     return 0;
3929 
3930   if (Slice.Array == nullptr)
3931     return 1;
3932 
3933   // Search for nul characters
3934   unsigned NullIndex = 0;
3935   for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
3936     if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
3937       break;
3938   }
3939 
3940   return NullIndex + 1;
3941 }
3942 
3943 /// If we can compute the length of the string pointed to by
3944 /// the specified pointer, return 'len+1'.  If we can't, return 0.
3945 uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
3946   if (!V->getType()->isPointerTy())
3947     return 0;
3948 
3949   SmallPtrSet<const PHINode*, 32> PHIs;
3950   uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
3951   // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
3952   // an empty string as a length.
3953   return Len == ~0ULL ? 1 : Len;
3954 }
3955 
3956 const Value *
3957 llvm::getArgumentAliasingToReturnedPointer(const CallBase *Call,
3958                                            bool MustPreserveNullness) {
3959   assert(Call &&
3960          "getArgumentAliasingToReturnedPointer only works on nonnull calls");
3961   if (const Value *RV = Call->getReturnedArgOperand())
3962     return RV;
3963   // This can be used only as a aliasing property.
3964   if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(
3965           Call, MustPreserveNullness))
3966     return Call->getArgOperand(0);
3967   return nullptr;
3968 }
3969 
3970 bool llvm::isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(
3971     const CallBase *Call, bool MustPreserveNullness) {
3972   return Call->getIntrinsicID() == Intrinsic::launder_invariant_group ||
3973          Call->getIntrinsicID() == Intrinsic::strip_invariant_group ||
3974          Call->getIntrinsicID() == Intrinsic::aarch64_irg ||
3975          Call->getIntrinsicID() == Intrinsic::aarch64_tagp ||
3976          (!MustPreserveNullness &&
3977           Call->getIntrinsicID() == Intrinsic::ptrmask);
3978 }
3979 
3980 /// \p PN defines a loop-variant pointer to an object.  Check if the
3981 /// previous iteration of the loop was referring to the same object as \p PN.
3982 static bool isSameUnderlyingObjectInLoop(const PHINode *PN,
3983                                          const LoopInfo *LI) {
3984   // Find the loop-defined value.
3985   Loop *L = LI->getLoopFor(PN->getParent());
3986   if (PN->getNumIncomingValues() != 2)
3987     return true;
3988 
3989   // Find the value from previous iteration.
3990   auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
3991   if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
3992     PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
3993   if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
3994     return true;
3995 
3996   // If a new pointer is loaded in the loop, the pointer references a different
3997   // object in every iteration.  E.g.:
3998   //    for (i)
3999   //       int *p = a[i];
4000   //       ...
4001   if (auto *Load = dyn_cast<LoadInst>(PrevValue))
4002     if (!L->isLoopInvariant(Load->getPointerOperand()))
4003       return false;
4004   return true;
4005 }
4006 
4007 Value *llvm::GetUnderlyingObject(Value *V, const DataLayout &DL,
4008                                  unsigned MaxLookup) {
4009   if (!V->getType()->isPointerTy())
4010     return V;
4011   for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
4012     if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
4013       V = GEP->getPointerOperand();
4014     } else if (Operator::getOpcode(V) == Instruction::BitCast ||
4015                Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
4016       V = cast<Operator>(V)->getOperand(0);
4017     } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
4018       if (GA->isInterposable())
4019         return V;
4020       V = GA->getAliasee();
4021     } else if (isa<AllocaInst>(V)) {
4022       // An alloca can't be further simplified.
4023       return V;
4024     } else {
4025       if (auto *Call = dyn_cast<CallBase>(V)) {
4026         // CaptureTracking can know about special capturing properties of some
4027         // intrinsics like launder.invariant.group, that can't be expressed with
4028         // the attributes, but have properties like returning aliasing pointer.
4029         // Because some analysis may assume that nocaptured pointer is not
4030         // returned from some special intrinsic (because function would have to
4031         // be marked with returns attribute), it is crucial to use this function
4032         // because it should be in sync with CaptureTracking. Not using it may
4033         // cause weird miscompilations where 2 aliasing pointers are assumed to
4034         // noalias.
4035         if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
4036           V = RP;
4037           continue;
4038         }
4039       }
4040 
4041       // See if InstructionSimplify knows any relevant tricks.
4042       if (Instruction *I = dyn_cast<Instruction>(V))
4043         // TODO: Acquire a DominatorTree and AssumptionCache and use them.
4044         if (Value *Simplified = SimplifyInstruction(I, {DL, I})) {
4045           V = Simplified;
4046           continue;
4047         }
4048 
4049       return V;
4050     }
4051     assert(V->getType()->isPointerTy() && "Unexpected operand type!");
4052   }
4053   return V;
4054 }
4055 
4056 void llvm::GetUnderlyingObjects(const Value *V,
4057                                 SmallVectorImpl<const Value *> &Objects,
4058                                 const DataLayout &DL, LoopInfo *LI,
4059                                 unsigned MaxLookup) {
4060   SmallPtrSet<const Value *, 4> Visited;
4061   SmallVector<const Value *, 4> Worklist;
4062   Worklist.push_back(V);
4063   do {
4064     const Value *P = Worklist.pop_back_val();
4065     P = GetUnderlyingObject(P, DL, MaxLookup);
4066 
4067     if (!Visited.insert(P).second)
4068       continue;
4069 
4070     if (auto *SI = dyn_cast<SelectInst>(P)) {
4071       Worklist.push_back(SI->getTrueValue());
4072       Worklist.push_back(SI->getFalseValue());
4073       continue;
4074     }
4075 
4076     if (auto *PN = dyn_cast<PHINode>(P)) {
4077       // If this PHI changes the underlying object in every iteration of the
4078       // loop, don't look through it.  Consider:
4079       //   int **A;
4080       //   for (i) {
4081       //     Prev = Curr;     // Prev = PHI (Prev_0, Curr)
4082       //     Curr = A[i];
4083       //     *Prev, *Curr;
4084       //
4085       // Prev is tracking Curr one iteration behind so they refer to different
4086       // underlying objects.
4087       if (!LI || !LI->isLoopHeader(PN->getParent()) ||
4088           isSameUnderlyingObjectInLoop(PN, LI))
4089         for (Value *IncValue : PN->incoming_values())
4090           Worklist.push_back(IncValue);
4091       continue;
4092     }
4093 
4094     Objects.push_back(P);
4095   } while (!Worklist.empty());
4096 }
4097 
4098 /// This is the function that does the work of looking through basic
4099 /// ptrtoint+arithmetic+inttoptr sequences.
4100 static const Value *getUnderlyingObjectFromInt(const Value *V) {
4101   do {
4102     if (const Operator *U = dyn_cast<Operator>(V)) {
4103       // If we find a ptrtoint, we can transfer control back to the
4104       // regular getUnderlyingObjectFromInt.
4105       if (U->getOpcode() == Instruction::PtrToInt)
4106         return U->getOperand(0);
4107       // If we find an add of a constant, a multiplied value, or a phi, it's
4108       // likely that the other operand will lead us to the base
4109       // object. We don't have to worry about the case where the
4110       // object address is somehow being computed by the multiply,
4111       // because our callers only care when the result is an
4112       // identifiable object.
4113       if (U->getOpcode() != Instruction::Add ||
4114           (!isa<ConstantInt>(U->getOperand(1)) &&
4115            Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
4116            !isa<PHINode>(U->getOperand(1))))
4117         return V;
4118       V = U->getOperand(0);
4119     } else {
4120       return V;
4121     }
4122     assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
4123   } while (true);
4124 }
4125 
4126 /// This is a wrapper around GetUnderlyingObjects and adds support for basic
4127 /// ptrtoint+arithmetic+inttoptr sequences.
4128 /// It returns false if unidentified object is found in GetUnderlyingObjects.
4129 bool llvm::getUnderlyingObjectsForCodeGen(const Value *V,
4130                           SmallVectorImpl<Value *> &Objects,
4131                           const DataLayout &DL) {
4132   SmallPtrSet<const Value *, 16> Visited;
4133   SmallVector<const Value *, 4> Working(1, V);
4134   do {
4135     V = Working.pop_back_val();
4136 
4137     SmallVector<const Value *, 4> Objs;
4138     GetUnderlyingObjects(V, Objs, DL);
4139 
4140     for (const Value *V : Objs) {
4141       if (!Visited.insert(V).second)
4142         continue;
4143       if (Operator::getOpcode(V) == Instruction::IntToPtr) {
4144         const Value *O =
4145           getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
4146         if (O->getType()->isPointerTy()) {
4147           Working.push_back(O);
4148           continue;
4149         }
4150       }
4151       // If GetUnderlyingObjects fails to find an identifiable object,
4152       // getUnderlyingObjectsForCodeGen also fails for safety.
4153       if (!isIdentifiedObject(V)) {
4154         Objects.clear();
4155         return false;
4156       }
4157       Objects.push_back(const_cast<Value *>(V));
4158     }
4159   } while (!Working.empty());
4160   return true;
4161 }
4162 
4163 /// Return true if the only users of this pointer are lifetime markers.
4164 bool llvm::onlyUsedByLifetimeMarkers(const Value *V) {
4165   for (const User *U : V->users()) {
4166     const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
4167     if (!II) return false;
4168 
4169     if (!II->isLifetimeStartOrEnd())
4170       return false;
4171   }
4172   return true;
4173 }
4174 
4175 bool llvm::mustSuppressSpeculation(const LoadInst &LI) {
4176   if (!LI.isUnordered())
4177     return true;
4178   const Function &F = *LI.getFunction();
4179   // Speculative load may create a race that did not exist in the source.
4180   return F.hasFnAttribute(Attribute::SanitizeThread) ||
4181     // Speculative load may load data from dirty regions.
4182     F.hasFnAttribute(Attribute::SanitizeAddress) ||
4183     F.hasFnAttribute(Attribute::SanitizeHWAddress);
4184 }
4185 
4186 
4187 bool llvm::isSafeToSpeculativelyExecute(const Value *V,
4188                                         const Instruction *CtxI,
4189                                         const DominatorTree *DT) {
4190   const Operator *Inst = dyn_cast<Operator>(V);
4191   if (!Inst)
4192     return false;
4193 
4194   for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i)
4195     if (Constant *C = dyn_cast<Constant>(Inst->getOperand(i)))
4196       if (C->canTrap())
4197         return false;
4198 
4199   switch (Inst->getOpcode()) {
4200   default:
4201     return true;
4202   case Instruction::UDiv:
4203   case Instruction::URem: {
4204     // x / y is undefined if y == 0.
4205     const APInt *V;
4206     if (match(Inst->getOperand(1), m_APInt(V)))
4207       return *V != 0;
4208     return false;
4209   }
4210   case Instruction::SDiv:
4211   case Instruction::SRem: {
4212     // x / y is undefined if y == 0 or x == INT_MIN and y == -1
4213     const APInt *Numerator, *Denominator;
4214     if (!match(Inst->getOperand(1), m_APInt(Denominator)))
4215       return false;
4216     // We cannot hoist this division if the denominator is 0.
4217     if (*Denominator == 0)
4218       return false;
4219     // It's safe to hoist if the denominator is not 0 or -1.
4220     if (*Denominator != -1)
4221       return true;
4222     // At this point we know that the denominator is -1.  It is safe to hoist as
4223     // long we know that the numerator is not INT_MIN.
4224     if (match(Inst->getOperand(0), m_APInt(Numerator)))
4225       return !Numerator->isMinSignedValue();
4226     // The numerator *might* be MinSignedValue.
4227     return false;
4228   }
4229   case Instruction::Load: {
4230     const LoadInst *LI = cast<LoadInst>(Inst);
4231     if (mustSuppressSpeculation(*LI))
4232       return false;
4233     const DataLayout &DL = LI->getModule()->getDataLayout();
4234     return isDereferenceableAndAlignedPointer(
4235         LI->getPointerOperand(), LI->getType(), MaybeAlign(LI->getAlignment()),
4236         DL, CtxI, DT);
4237   }
4238   case Instruction::Call: {
4239     auto *CI = cast<const CallInst>(Inst);
4240     const Function *Callee = CI->getCalledFunction();
4241 
4242     // The called function could have undefined behavior or side-effects, even
4243     // if marked readnone nounwind.
4244     return Callee && Callee->isSpeculatable();
4245   }
4246   case Instruction::VAArg:
4247   case Instruction::Alloca:
4248   case Instruction::Invoke:
4249   case Instruction::CallBr:
4250   case Instruction::PHI:
4251   case Instruction::Store:
4252   case Instruction::Ret:
4253   case Instruction::Br:
4254   case Instruction::IndirectBr:
4255   case Instruction::Switch:
4256   case Instruction::Unreachable:
4257   case Instruction::Fence:
4258   case Instruction::AtomicRMW:
4259   case Instruction::AtomicCmpXchg:
4260   case Instruction::LandingPad:
4261   case Instruction::Resume:
4262   case Instruction::CatchSwitch:
4263   case Instruction::CatchPad:
4264   case Instruction::CatchRet:
4265   case Instruction::CleanupPad:
4266   case Instruction::CleanupRet:
4267     return false; // Misc instructions which have effects
4268   }
4269 }
4270 
4271 bool llvm::mayBeMemoryDependent(const Instruction &I) {
4272   return I.mayReadOrWriteMemory() || !isSafeToSpeculativelyExecute(&I);
4273 }
4274 
4275 /// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
4276 static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR) {
4277   switch (OR) {
4278     case ConstantRange::OverflowResult::MayOverflow:
4279       return OverflowResult::MayOverflow;
4280     case ConstantRange::OverflowResult::AlwaysOverflowsLow:
4281       return OverflowResult::AlwaysOverflowsLow;
4282     case ConstantRange::OverflowResult::AlwaysOverflowsHigh:
4283       return OverflowResult::AlwaysOverflowsHigh;
4284     case ConstantRange::OverflowResult::NeverOverflows:
4285       return OverflowResult::NeverOverflows;
4286   }
4287   llvm_unreachable("Unknown OverflowResult");
4288 }
4289 
4290 /// Combine constant ranges from computeConstantRange() and computeKnownBits().
4291 static ConstantRange computeConstantRangeIncludingKnownBits(
4292     const Value *V, bool ForSigned, const DataLayout &DL, unsigned Depth,
4293     AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT,
4294     OptimizationRemarkEmitter *ORE = nullptr, bool UseInstrInfo = true) {
4295   KnownBits Known = computeKnownBits(
4296       V, DL, Depth, AC, CxtI, DT, ORE, UseInstrInfo);
4297   ConstantRange CR1 = ConstantRange::fromKnownBits(Known, ForSigned);
4298   ConstantRange CR2 = computeConstantRange(V, UseInstrInfo);
4299   ConstantRange::PreferredRangeType RangeType =
4300       ForSigned ? ConstantRange::Signed : ConstantRange::Unsigned;
4301   return CR1.intersectWith(CR2, RangeType);
4302 }
4303 
4304 OverflowResult llvm::computeOverflowForUnsignedMul(
4305     const Value *LHS, const Value *RHS, const DataLayout &DL,
4306     AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT,
4307     bool UseInstrInfo) {
4308   KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT,
4309                                         nullptr, UseInstrInfo);
4310   KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT,
4311                                         nullptr, UseInstrInfo);
4312   ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false);
4313   ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false);
4314   return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
4315 }
4316 
4317 OverflowResult
4318 llvm::computeOverflowForSignedMul(const Value *LHS, const Value *RHS,
4319                                   const DataLayout &DL, AssumptionCache *AC,
4320                                   const Instruction *CxtI,
4321                                   const DominatorTree *DT, bool UseInstrInfo) {
4322   // Multiplying n * m significant bits yields a result of n + m significant
4323   // bits. If the total number of significant bits does not exceed the
4324   // result bit width (minus 1), there is no overflow.
4325   // This means if we have enough leading sign bits in the operands
4326   // we can guarantee that the result does not overflow.
4327   // Ref: "Hacker's Delight" by Henry Warren
4328   unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
4329 
4330   // Note that underestimating the number of sign bits gives a more
4331   // conservative answer.
4332   unsigned SignBits = ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) +
4333                       ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT);
4334 
4335   // First handle the easy case: if we have enough sign bits there's
4336   // definitely no overflow.
4337   if (SignBits > BitWidth + 1)
4338     return OverflowResult::NeverOverflows;
4339 
4340   // There are two ambiguous cases where there can be no overflow:
4341   //   SignBits == BitWidth + 1    and
4342   //   SignBits == BitWidth
4343   // The second case is difficult to check, therefore we only handle the
4344   // first case.
4345   if (SignBits == BitWidth + 1) {
4346     // It overflows only when both arguments are negative and the true
4347     // product is exactly the minimum negative number.
4348     // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
4349     // For simplicity we just check if at least one side is not negative.
4350     KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT,
4351                                           nullptr, UseInstrInfo);
4352     KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT,
4353                                           nullptr, UseInstrInfo);
4354     if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
4355       return OverflowResult::NeverOverflows;
4356   }
4357   return OverflowResult::MayOverflow;
4358 }
4359 
4360 OverflowResult llvm::computeOverflowForUnsignedAdd(
4361     const Value *LHS, const Value *RHS, const DataLayout &DL,
4362     AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT,
4363     bool UseInstrInfo) {
4364   ConstantRange LHSRange = computeConstantRangeIncludingKnownBits(
4365       LHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT,
4366       nullptr, UseInstrInfo);
4367   ConstantRange RHSRange = computeConstantRangeIncludingKnownBits(
4368       RHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT,
4369       nullptr, UseInstrInfo);
4370   return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
4371 }
4372 
4373 static OverflowResult computeOverflowForSignedAdd(const Value *LHS,
4374                                                   const Value *RHS,
4375                                                   const AddOperator *Add,
4376                                                   const DataLayout &DL,
4377                                                   AssumptionCache *AC,
4378                                                   const Instruction *CxtI,
4379                                                   const DominatorTree *DT) {
4380   if (Add && Add->hasNoSignedWrap()) {
4381     return OverflowResult::NeverOverflows;
4382   }
4383 
4384   // If LHS and RHS each have at least two sign bits, the addition will look
4385   // like
4386   //
4387   // XX..... +
4388   // YY.....
4389   //
4390   // If the carry into the most significant position is 0, X and Y can't both
4391   // be 1 and therefore the carry out of the addition is also 0.
4392   //
4393   // If the carry into the most significant position is 1, X and Y can't both
4394   // be 0 and therefore the carry out of the addition is also 1.
4395   //
4396   // Since the carry into the most significant position is always equal to
4397   // the carry out of the addition, there is no signed overflow.
4398   if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 &&
4399       ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1)
4400     return OverflowResult::NeverOverflows;
4401 
4402   ConstantRange LHSRange = computeConstantRangeIncludingKnownBits(
4403       LHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT);
4404   ConstantRange RHSRange = computeConstantRangeIncludingKnownBits(
4405       RHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT);
4406   OverflowResult OR =
4407       mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
4408   if (OR != OverflowResult::MayOverflow)
4409     return OR;
4410 
4411   // The remaining code needs Add to be available. Early returns if not so.
4412   if (!Add)
4413     return OverflowResult::MayOverflow;
4414 
4415   // If the sign of Add is the same as at least one of the operands, this add
4416   // CANNOT overflow. If this can be determined from the known bits of the
4417   // operands the above signedAddMayOverflow() check will have already done so.
4418   // The only other way to improve on the known bits is from an assumption, so
4419   // call computeKnownBitsFromAssume() directly.
4420   bool LHSOrRHSKnownNonNegative =
4421       (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
4422   bool LHSOrRHSKnownNegative =
4423       (LHSRange.isAllNegative() || RHSRange.isAllNegative());
4424   if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
4425     KnownBits AddKnown(LHSRange.getBitWidth());
4426     computeKnownBitsFromAssume(
4427         Add, AddKnown, /*Depth=*/0, Query(DL, AC, CxtI, DT, true));
4428     if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
4429         (AddKnown.isNegative() && LHSOrRHSKnownNegative))
4430       return OverflowResult::NeverOverflows;
4431   }
4432 
4433   return OverflowResult::MayOverflow;
4434 }
4435 
4436 OverflowResult llvm::computeOverflowForUnsignedSub(const Value *LHS,
4437                                                    const Value *RHS,
4438                                                    const DataLayout &DL,
4439                                                    AssumptionCache *AC,
4440                                                    const Instruction *CxtI,
4441                                                    const DominatorTree *DT) {
4442   // Checking for conditions implied by dominating conditions may be expensive.
4443   // Limit it to usub_with_overflow calls for now.
4444   if (match(CxtI,
4445             m_Intrinsic<Intrinsic::usub_with_overflow>(m_Value(), m_Value())))
4446     if (auto C =
4447             isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, CxtI, DL)) {
4448       if (*C)
4449         return OverflowResult::NeverOverflows;
4450       return OverflowResult::AlwaysOverflowsLow;
4451     }
4452   ConstantRange LHSRange = computeConstantRangeIncludingKnownBits(
4453       LHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT);
4454   ConstantRange RHSRange = computeConstantRangeIncludingKnownBits(
4455       RHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT);
4456   return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
4457 }
4458 
4459 OverflowResult llvm::computeOverflowForSignedSub(const Value *LHS,
4460                                                  const Value *RHS,
4461                                                  const DataLayout &DL,
4462                                                  AssumptionCache *AC,
4463                                                  const Instruction *CxtI,
4464                                                  const DominatorTree *DT) {
4465   // If LHS and RHS each have at least two sign bits, the subtraction
4466   // cannot overflow.
4467   if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 &&
4468       ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1)
4469     return OverflowResult::NeverOverflows;
4470 
4471   ConstantRange LHSRange = computeConstantRangeIncludingKnownBits(
4472       LHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT);
4473   ConstantRange RHSRange = computeConstantRangeIncludingKnownBits(
4474       RHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT);
4475   return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
4476 }
4477 
4478 bool llvm::isOverflowIntrinsicNoWrap(const WithOverflowInst *WO,
4479                                      const DominatorTree &DT) {
4480   SmallVector<const BranchInst *, 2> GuardingBranches;
4481   SmallVector<const ExtractValueInst *, 2> Results;
4482 
4483   for (const User *U : WO->users()) {
4484     if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
4485       assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
4486 
4487       if (EVI->getIndices()[0] == 0)
4488         Results.push_back(EVI);
4489       else {
4490         assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
4491 
4492         for (const auto *U : EVI->users())
4493           if (const auto *B = dyn_cast<BranchInst>(U)) {
4494             assert(B->isConditional() && "How else is it using an i1?");
4495             GuardingBranches.push_back(B);
4496           }
4497       }
4498     } else {
4499       // We are using the aggregate directly in a way we don't want to analyze
4500       // here (storing it to a global, say).
4501       return false;
4502     }
4503   }
4504 
4505   auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
4506     BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
4507     if (!NoWrapEdge.isSingleEdge())
4508       return false;
4509 
4510     // Check if all users of the add are provably no-wrap.
4511     for (const auto *Result : Results) {
4512       // If the extractvalue itself is not executed on overflow, the we don't
4513       // need to check each use separately, since domination is transitive.
4514       if (DT.dominates(NoWrapEdge, Result->getParent()))
4515         continue;
4516 
4517       for (auto &RU : Result->uses())
4518         if (!DT.dominates(NoWrapEdge, RU))
4519           return false;
4520     }
4521 
4522     return true;
4523   };
4524 
4525   return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
4526 }
4527 
4528 bool llvm::isGuaranteedNotToBeUndefOrPoison(const Value *V) {
4529   // If the value is a freeze instruction, then it can never
4530   // be undef or poison.
4531   if (isa<FreezeInst>(V))
4532     return true;
4533   // TODO: Some instructions are guaranteed to return neither undef
4534   // nor poison if their arguments are not poison/undef.
4535 
4536   // TODO: Deal with other Constant subclasses.
4537   if (isa<ConstantInt>(V) || isa<GlobalVariable>(V))
4538     return true;
4539 
4540   if (auto PN = dyn_cast<PHINode>(V)) {
4541     if (llvm::all_of(PN->incoming_values(), [](const Use &U) {
4542           return isa<ConstantInt>(U.get());
4543         }))
4544       return true;
4545   }
4546 
4547   if (auto II = dyn_cast<ICmpInst>(V)) {
4548     if (llvm::all_of(II->operands(), [](const Value *V) {
4549           return isGuaranteedNotToBeUndefOrPoison(V);
4550         }))
4551       return true;
4552   }
4553 
4554   return false;
4555 }
4556 
4557 OverflowResult llvm::computeOverflowForSignedAdd(const AddOperator *Add,
4558                                                  const DataLayout &DL,
4559                                                  AssumptionCache *AC,
4560                                                  const Instruction *CxtI,
4561                                                  const DominatorTree *DT) {
4562   return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
4563                                        Add, DL, AC, CxtI, DT);
4564 }
4565 
4566 OverflowResult llvm::computeOverflowForSignedAdd(const Value *LHS,
4567                                                  const Value *RHS,
4568                                                  const DataLayout &DL,
4569                                                  AssumptionCache *AC,
4570                                                  const Instruction *CxtI,
4571                                                  const DominatorTree *DT) {
4572   return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, DL, AC, CxtI, DT);
4573 }
4574 
4575 bool llvm::isGuaranteedToTransferExecutionToSuccessor(const Instruction *I) {
4576   // Note: An atomic operation isn't guaranteed to return in a reasonable amount
4577   // of time because it's possible for another thread to interfere with it for an
4578   // arbitrary length of time, but programs aren't allowed to rely on that.
4579 
4580   // If there is no successor, then execution can't transfer to it.
4581   if (const auto *CRI = dyn_cast<CleanupReturnInst>(I))
4582     return !CRI->unwindsToCaller();
4583   if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(I))
4584     return !CatchSwitch->unwindsToCaller();
4585   if (isa<ResumeInst>(I))
4586     return false;
4587   if (isa<ReturnInst>(I))
4588     return false;
4589   if (isa<UnreachableInst>(I))
4590     return false;
4591 
4592   // Calls can throw, or contain an infinite loop, or kill the process.
4593   if (auto CS = ImmutableCallSite(I)) {
4594     // Call sites that throw have implicit non-local control flow.
4595     if (!CS.doesNotThrow())
4596       return false;
4597 
4598     // A function which doens't throw and has "willreturn" attribute will
4599     // always return.
4600     if (CS.hasFnAttr(Attribute::WillReturn))
4601       return true;
4602 
4603     // Non-throwing call sites can loop infinitely, call exit/pthread_exit
4604     // etc. and thus not return.  However, LLVM already assumes that
4605     //
4606     //  - Thread exiting actions are modeled as writes to memory invisible to
4607     //    the program.
4608     //
4609     //  - Loops that don't have side effects (side effects are volatile/atomic
4610     //    stores and IO) always terminate (see http://llvm.org/PR965).
4611     //    Furthermore IO itself is also modeled as writes to memory invisible to
4612     //    the program.
4613     //
4614     // We rely on those assumptions here, and use the memory effects of the call
4615     // target as a proxy for checking that it always returns.
4616 
4617     // FIXME: This isn't aggressive enough; a call which only writes to a global
4618     // is guaranteed to return.
4619     return CS.onlyReadsMemory() || CS.onlyAccessesArgMemory();
4620   }
4621 
4622   // Other instructions return normally.
4623   return true;
4624 }
4625 
4626 bool llvm::isGuaranteedToTransferExecutionToSuccessor(const BasicBlock *BB) {
4627   // TODO: This is slightly conservative for invoke instruction since exiting
4628   // via an exception *is* normal control for them.
4629   for (auto I = BB->begin(), E = BB->end(); I != E; ++I)
4630     if (!isGuaranteedToTransferExecutionToSuccessor(&*I))
4631       return false;
4632   return true;
4633 }
4634 
4635 bool llvm::isGuaranteedToExecuteForEveryIteration(const Instruction *I,
4636                                                   const Loop *L) {
4637   // The loop header is guaranteed to be executed for every iteration.
4638   //
4639   // FIXME: Relax this constraint to cover all basic blocks that are
4640   // guaranteed to be executed at every iteration.
4641   if (I->getParent() != L->getHeader()) return false;
4642 
4643   for (const Instruction &LI : *L->getHeader()) {
4644     if (&LI == I) return true;
4645     if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
4646   }
4647   llvm_unreachable("Instruction not contained in its own parent basic block.");
4648 }
4649 
4650 bool llvm::propagatesFullPoison(const Instruction *I) {
4651   // TODO: This should include all instructions apart from phis, selects and
4652   // call-like instructions.
4653   switch (I->getOpcode()) {
4654   case Instruction::Add:
4655   case Instruction::Sub:
4656   case Instruction::Xor:
4657   case Instruction::Trunc:
4658   case Instruction::BitCast:
4659   case Instruction::AddrSpaceCast:
4660   case Instruction::Mul:
4661   case Instruction::Shl:
4662   case Instruction::GetElementPtr:
4663     // These operations all propagate poison unconditionally. Note that poison
4664     // is not any particular value, so xor or subtraction of poison with
4665     // itself still yields poison, not zero.
4666     return true;
4667 
4668   case Instruction::AShr:
4669   case Instruction::SExt:
4670     // For these operations, one bit of the input is replicated across
4671     // multiple output bits. A replicated poison bit is still poison.
4672     return true;
4673 
4674   case Instruction::ICmp:
4675     // Comparing poison with any value yields poison.  This is why, for
4676     // instance, x s< (x +nsw 1) can be folded to true.
4677     return true;
4678 
4679   default:
4680     return false;
4681   }
4682 }
4683 
4684 const Value *llvm::getGuaranteedNonFullPoisonOp(const Instruction *I) {
4685   switch (I->getOpcode()) {
4686     case Instruction::Store:
4687       return cast<StoreInst>(I)->getPointerOperand();
4688 
4689     case Instruction::Load:
4690       return cast<LoadInst>(I)->getPointerOperand();
4691 
4692     case Instruction::AtomicCmpXchg:
4693       return cast<AtomicCmpXchgInst>(I)->getPointerOperand();
4694 
4695     case Instruction::AtomicRMW:
4696       return cast<AtomicRMWInst>(I)->getPointerOperand();
4697 
4698     case Instruction::UDiv:
4699     case Instruction::SDiv:
4700     case Instruction::URem:
4701     case Instruction::SRem:
4702       return I->getOperand(1);
4703 
4704     default:
4705       // Note: It's really tempting to think that a conditional branch or
4706       // switch should be listed here, but that's incorrect.  It's not
4707       // branching off of poison which is UB, it is executing a side effecting
4708       // instruction which follows the branch.
4709       return nullptr;
4710   }
4711 }
4712 
4713 bool llvm::mustTriggerUB(const Instruction *I,
4714                          const SmallSet<const Value *, 16>& KnownPoison) {
4715   auto *NotPoison = getGuaranteedNonFullPoisonOp(I);
4716   return (NotPoison && KnownPoison.count(NotPoison));
4717 }
4718 
4719 
4720 bool llvm::programUndefinedIfFullPoison(const Instruction *PoisonI) {
4721   // We currently only look for uses of poison values within the same basic
4722   // block, as that makes it easier to guarantee that the uses will be
4723   // executed given that PoisonI is executed.
4724   //
4725   // FIXME: Expand this to consider uses beyond the same basic block. To do
4726   // this, look out for the distinction between post-dominance and strong
4727   // post-dominance.
4728   const BasicBlock *BB = PoisonI->getParent();
4729 
4730   // Set of instructions that we have proved will yield poison if PoisonI
4731   // does.
4732   SmallSet<const Value *, 16> YieldsPoison;
4733   SmallSet<const BasicBlock *, 4> Visited;
4734   YieldsPoison.insert(PoisonI);
4735   Visited.insert(PoisonI->getParent());
4736 
4737   BasicBlock::const_iterator Begin = PoisonI->getIterator(), End = BB->end();
4738 
4739   unsigned Iter = 0;
4740   while (Iter++ < MaxDepth) {
4741     for (auto &I : make_range(Begin, End)) {
4742       if (&I != PoisonI) {
4743         if (mustTriggerUB(&I, YieldsPoison))
4744           return true;
4745         if (!isGuaranteedToTransferExecutionToSuccessor(&I))
4746           return false;
4747       }
4748 
4749       // Mark poison that propagates from I through uses of I.
4750       if (YieldsPoison.count(&I)) {
4751         for (const User *User : I.users()) {
4752           const Instruction *UserI = cast<Instruction>(User);
4753           if (propagatesFullPoison(UserI))
4754             YieldsPoison.insert(User);
4755         }
4756       }
4757     }
4758 
4759     if (auto *NextBB = BB->getSingleSuccessor()) {
4760       if (Visited.insert(NextBB).second) {
4761         BB = NextBB;
4762         Begin = BB->getFirstNonPHI()->getIterator();
4763         End = BB->end();
4764         continue;
4765       }
4766     }
4767 
4768     break;
4769   }
4770   return false;
4771 }
4772 
4773 static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
4774   if (FMF.noNaNs())
4775     return true;
4776 
4777   if (auto *C = dyn_cast<ConstantFP>(V))
4778     return !C->isNaN();
4779 
4780   if (auto *C = dyn_cast<ConstantDataVector>(V)) {
4781     if (!C->getElementType()->isFloatingPointTy())
4782       return false;
4783     for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
4784       if (C->getElementAsAPFloat(I).isNaN())
4785         return false;
4786     }
4787     return true;
4788   }
4789 
4790   return false;
4791 }
4792 
4793 static bool isKnownNonZero(const Value *V) {
4794   if (auto *C = dyn_cast<ConstantFP>(V))
4795     return !C->isZero();
4796 
4797   if (auto *C = dyn_cast<ConstantDataVector>(V)) {
4798     if (!C->getElementType()->isFloatingPointTy())
4799       return false;
4800     for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
4801       if (C->getElementAsAPFloat(I).isZero())
4802         return false;
4803     }
4804     return true;
4805   }
4806 
4807   return false;
4808 }
4809 
4810 /// Match clamp pattern for float types without care about NaNs or signed zeros.
4811 /// Given non-min/max outer cmp/select from the clamp pattern this
4812 /// function recognizes if it can be substitued by a "canonical" min/max
4813 /// pattern.
4814 static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred,
4815                                                Value *CmpLHS, Value *CmpRHS,
4816                                                Value *TrueVal, Value *FalseVal,
4817                                                Value *&LHS, Value *&RHS) {
4818   // Try to match
4819   //   X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
4820   //   X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
4821   // and return description of the outer Max/Min.
4822 
4823   // First, check if select has inverse order:
4824   if (CmpRHS == FalseVal) {
4825     std::swap(TrueVal, FalseVal);
4826     Pred = CmpInst::getInversePredicate(Pred);
4827   }
4828 
4829   // Assume success now. If there's no match, callers should not use these anyway.
4830   LHS = TrueVal;
4831   RHS = FalseVal;
4832 
4833   const APFloat *FC1;
4834   if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
4835     return {SPF_UNKNOWN, SPNB_NA, false};
4836 
4837   const APFloat *FC2;
4838   switch (Pred) {
4839   case CmpInst::FCMP_OLT:
4840   case CmpInst::FCMP_OLE:
4841   case CmpInst::FCMP_ULT:
4842   case CmpInst::FCMP_ULE:
4843     if (match(FalseVal,
4844               m_CombineOr(m_OrdFMin(m_Specific(CmpLHS), m_APFloat(FC2)),
4845                           m_UnordFMin(m_Specific(CmpLHS), m_APFloat(FC2)))) &&
4846         FC1->compare(*FC2) == APFloat::cmpResult::cmpLessThan)
4847       return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
4848     break;
4849   case CmpInst::FCMP_OGT:
4850   case CmpInst::FCMP_OGE:
4851   case CmpInst::FCMP_UGT:
4852   case CmpInst::FCMP_UGE:
4853     if (match(FalseVal,
4854               m_CombineOr(m_OrdFMax(m_Specific(CmpLHS), m_APFloat(FC2)),
4855                           m_UnordFMax(m_Specific(CmpLHS), m_APFloat(FC2)))) &&
4856         FC1->compare(*FC2) == APFloat::cmpResult::cmpGreaterThan)
4857       return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
4858     break;
4859   default:
4860     break;
4861   }
4862 
4863   return {SPF_UNKNOWN, SPNB_NA, false};
4864 }
4865 
4866 /// Recognize variations of:
4867 ///   CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
4868 static SelectPatternResult matchClamp(CmpInst::Predicate Pred,
4869                                       Value *CmpLHS, Value *CmpRHS,
4870                                       Value *TrueVal, Value *FalseVal) {
4871   // Swap the select operands and predicate to match the patterns below.
4872   if (CmpRHS != TrueVal) {
4873     Pred = ICmpInst::getSwappedPredicate(Pred);
4874     std::swap(TrueVal, FalseVal);
4875   }
4876   const APInt *C1;
4877   if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
4878     const APInt *C2;
4879     // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
4880     if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
4881         C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
4882       return {SPF_SMAX, SPNB_NA, false};
4883 
4884     // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
4885     if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
4886         C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
4887       return {SPF_SMIN, SPNB_NA, false};
4888 
4889     // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
4890     if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
4891         C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
4892       return {SPF_UMAX, SPNB_NA, false};
4893 
4894     // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
4895     if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
4896         C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
4897       return {SPF_UMIN, SPNB_NA, false};
4898   }
4899   return {SPF_UNKNOWN, SPNB_NA, false};
4900 }
4901 
4902 /// Recognize variations of:
4903 ///   a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
4904 static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred,
4905                                                Value *CmpLHS, Value *CmpRHS,
4906                                                Value *TVal, Value *FVal,
4907                                                unsigned Depth) {
4908   // TODO: Allow FP min/max with nnan/nsz.
4909   assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
4910 
4911   Value *A = nullptr, *B = nullptr;
4912   SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
4913   if (!SelectPatternResult::isMinOrMax(L.Flavor))
4914     return {SPF_UNKNOWN, SPNB_NA, false};
4915 
4916   Value *C = nullptr, *D = nullptr;
4917   SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
4918   if (L.Flavor != R.Flavor)
4919     return {SPF_UNKNOWN, SPNB_NA, false};
4920 
4921   // We have something like: x Pred y ? min(a, b) : min(c, d).
4922   // Try to match the compare to the min/max operations of the select operands.
4923   // First, make sure we have the right compare predicate.
4924   switch (L.Flavor) {
4925   case SPF_SMIN:
4926     if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
4927       Pred = ICmpInst::getSwappedPredicate(Pred);
4928       std::swap(CmpLHS, CmpRHS);
4929     }
4930     if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
4931       break;
4932     return {SPF_UNKNOWN, SPNB_NA, false};
4933   case SPF_SMAX:
4934     if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
4935       Pred = ICmpInst::getSwappedPredicate(Pred);
4936       std::swap(CmpLHS, CmpRHS);
4937     }
4938     if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
4939       break;
4940     return {SPF_UNKNOWN, SPNB_NA, false};
4941   case SPF_UMIN:
4942     if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
4943       Pred = ICmpInst::getSwappedPredicate(Pred);
4944       std::swap(CmpLHS, CmpRHS);
4945     }
4946     if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
4947       break;
4948     return {SPF_UNKNOWN, SPNB_NA, false};
4949   case SPF_UMAX:
4950     if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
4951       Pred = ICmpInst::getSwappedPredicate(Pred);
4952       std::swap(CmpLHS, CmpRHS);
4953     }
4954     if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
4955       break;
4956     return {SPF_UNKNOWN, SPNB_NA, false};
4957   default:
4958     return {SPF_UNKNOWN, SPNB_NA, false};
4959   }
4960 
4961   // If there is a common operand in the already matched min/max and the other
4962   // min/max operands match the compare operands (either directly or inverted),
4963   // then this is min/max of the same flavor.
4964 
4965   // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
4966   // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
4967   if (D == B) {
4968     if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
4969                                          match(A, m_Not(m_Specific(CmpRHS)))))
4970       return {L.Flavor, SPNB_NA, false};
4971   }
4972   // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
4973   // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
4974   if (C == B) {
4975     if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
4976                                          match(A, m_Not(m_Specific(CmpRHS)))))
4977       return {L.Flavor, SPNB_NA, false};
4978   }
4979   // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
4980   // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
4981   if (D == A) {
4982     if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
4983                                          match(B, m_Not(m_Specific(CmpRHS)))))
4984       return {L.Flavor, SPNB_NA, false};
4985   }
4986   // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
4987   // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
4988   if (C == A) {
4989     if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
4990                                          match(B, m_Not(m_Specific(CmpRHS)))))
4991       return {L.Flavor, SPNB_NA, false};
4992   }
4993 
4994   return {SPF_UNKNOWN, SPNB_NA, false};
4995 }
4996 
4997 /// Match non-obvious integer minimum and maximum sequences.
4998 static SelectPatternResult matchMinMax(CmpInst::Predicate Pred,
4999                                        Value *CmpLHS, Value *CmpRHS,
5000                                        Value *TrueVal, Value *FalseVal,
5001                                        Value *&LHS, Value *&RHS,
5002                                        unsigned Depth) {
5003   // Assume success. If there's no match, callers should not use these anyway.
5004   LHS = TrueVal;
5005   RHS = FalseVal;
5006 
5007   SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
5008   if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN)
5009     return SPR;
5010 
5011   SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
5012   if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN)
5013     return SPR;
5014 
5015   if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
5016     return {SPF_UNKNOWN, SPNB_NA, false};
5017 
5018   // Z = X -nsw Y
5019   // (X >s Y) ? 0 : Z ==> (Z >s 0) ? 0 : Z ==> SMIN(Z, 0)
5020   // (X <s Y) ? 0 : Z ==> (Z <s 0) ? 0 : Z ==> SMAX(Z, 0)
5021   if (match(TrueVal, m_Zero()) &&
5022       match(FalseVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS))))
5023     return {Pred == CmpInst::ICMP_SGT ? SPF_SMIN : SPF_SMAX, SPNB_NA, false};
5024 
5025   // Z = X -nsw Y
5026   // (X >s Y) ? Z : 0 ==> (Z >s 0) ? Z : 0 ==> SMAX(Z, 0)
5027   // (X <s Y) ? Z : 0 ==> (Z <s 0) ? Z : 0 ==> SMIN(Z, 0)
5028   if (match(FalseVal, m_Zero()) &&
5029       match(TrueVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS))))
5030     return {Pred == CmpInst::ICMP_SGT ? SPF_SMAX : SPF_SMIN, SPNB_NA, false};
5031 
5032   const APInt *C1;
5033   if (!match(CmpRHS, m_APInt(C1)))
5034     return {SPF_UNKNOWN, SPNB_NA, false};
5035 
5036   // An unsigned min/max can be written with a signed compare.
5037   const APInt *C2;
5038   if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
5039       (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
5040     // Is the sign bit set?
5041     // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
5042     // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
5043     if (Pred == CmpInst::ICMP_SLT && C1->isNullValue() &&
5044         C2->isMaxSignedValue())
5045       return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
5046 
5047     // Is the sign bit clear?
5048     // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
5049     // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
5050     if (Pred == CmpInst::ICMP_SGT && C1->isAllOnesValue() &&
5051         C2->isMinSignedValue())
5052       return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
5053   }
5054 
5055   // Look through 'not' ops to find disguised signed min/max.
5056   // (X >s C) ? ~X : ~C ==> (~X <s ~C) ? ~X : ~C ==> SMIN(~X, ~C)
5057   // (X <s C) ? ~X : ~C ==> (~X >s ~C) ? ~X : ~C ==> SMAX(~X, ~C)
5058   if (match(TrueVal, m_Not(m_Specific(CmpLHS))) &&
5059       match(FalseVal, m_APInt(C2)) && ~(*C1) == *C2)
5060     return {Pred == CmpInst::ICMP_SGT ? SPF_SMIN : SPF_SMAX, SPNB_NA, false};
5061 
5062   // (X >s C) ? ~C : ~X ==> (~X <s ~C) ? ~C : ~X ==> SMAX(~C, ~X)
5063   // (X <s C) ? ~C : ~X ==> (~X >s ~C) ? ~C : ~X ==> SMIN(~C, ~X)
5064   if (match(FalseVal, m_Not(m_Specific(CmpLHS))) &&
5065       match(TrueVal, m_APInt(C2)) && ~(*C1) == *C2)
5066     return {Pred == CmpInst::ICMP_SGT ? SPF_SMAX : SPF_SMIN, SPNB_NA, false};
5067 
5068   return {SPF_UNKNOWN, SPNB_NA, false};
5069 }
5070 
5071 bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW) {
5072   assert(X && Y && "Invalid operand");
5073 
5074   // X = sub (0, Y) || X = sub nsw (0, Y)
5075   if ((!NeedNSW && match(X, m_Sub(m_ZeroInt(), m_Specific(Y)))) ||
5076       (NeedNSW && match(X, m_NSWSub(m_ZeroInt(), m_Specific(Y)))))
5077     return true;
5078 
5079   // Y = sub (0, X) || Y = sub nsw (0, X)
5080   if ((!NeedNSW && match(Y, m_Sub(m_ZeroInt(), m_Specific(X)))) ||
5081       (NeedNSW && match(Y, m_NSWSub(m_ZeroInt(), m_Specific(X)))))
5082     return true;
5083 
5084   // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
5085   Value *A, *B;
5086   return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
5087                         match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
5088          (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
5089                        match(Y, m_NSWSub(m_Specific(B), m_Specific(A)))));
5090 }
5091 
5092 static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred,
5093                                               FastMathFlags FMF,
5094                                               Value *CmpLHS, Value *CmpRHS,
5095                                               Value *TrueVal, Value *FalseVal,
5096                                               Value *&LHS, Value *&RHS,
5097                                               unsigned Depth) {
5098   if (CmpInst::isFPPredicate(Pred)) {
5099     // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
5100     // 0.0 operand, set the compare's 0.0 operands to that same value for the
5101     // purpose of identifying min/max. Disregard vector constants with undefined
5102     // elements because those can not be back-propagated for analysis.
5103     Value *OutputZeroVal = nullptr;
5104     if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
5105         !cast<Constant>(TrueVal)->containsUndefElement())
5106       OutputZeroVal = TrueVal;
5107     else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
5108              !cast<Constant>(FalseVal)->containsUndefElement())
5109       OutputZeroVal = FalseVal;
5110 
5111     if (OutputZeroVal) {
5112       if (match(CmpLHS, m_AnyZeroFP()))
5113         CmpLHS = OutputZeroVal;
5114       if (match(CmpRHS, m_AnyZeroFP()))
5115         CmpRHS = OutputZeroVal;
5116     }
5117   }
5118 
5119   LHS = CmpLHS;
5120   RHS = CmpRHS;
5121 
5122   // Signed zero may return inconsistent results between implementations.
5123   //  (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
5124   //  minNum(0.0, -0.0)          // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
5125   // Therefore, we behave conservatively and only proceed if at least one of the
5126   // operands is known to not be zero or if we don't care about signed zero.
5127   switch (Pred) {
5128   default: break;
5129   // FIXME: Include OGT/OLT/UGT/ULT.
5130   case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLE:
5131   case CmpInst::FCMP_UGE: case CmpInst::FCMP_ULE:
5132     if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
5133         !isKnownNonZero(CmpRHS))
5134       return {SPF_UNKNOWN, SPNB_NA, false};
5135   }
5136 
5137   SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
5138   bool Ordered = false;
5139 
5140   // When given one NaN and one non-NaN input:
5141   //   - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
5142   //   - A simple C99 (a < b ? a : b) construction will return 'b' (as the
5143   //     ordered comparison fails), which could be NaN or non-NaN.
5144   // so here we discover exactly what NaN behavior is required/accepted.
5145   if (CmpInst::isFPPredicate(Pred)) {
5146     bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
5147     bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
5148 
5149     if (LHSSafe && RHSSafe) {
5150       // Both operands are known non-NaN.
5151       NaNBehavior = SPNB_RETURNS_ANY;
5152     } else if (CmpInst::isOrdered(Pred)) {
5153       // An ordered comparison will return false when given a NaN, so it
5154       // returns the RHS.
5155       Ordered = true;
5156       if (LHSSafe)
5157         // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
5158         NaNBehavior = SPNB_RETURNS_NAN;
5159       else if (RHSSafe)
5160         NaNBehavior = SPNB_RETURNS_OTHER;
5161       else
5162         // Completely unsafe.
5163         return {SPF_UNKNOWN, SPNB_NA, false};
5164     } else {
5165       Ordered = false;
5166       // An unordered comparison will return true when given a NaN, so it
5167       // returns the LHS.
5168       if (LHSSafe)
5169         // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
5170         NaNBehavior = SPNB_RETURNS_OTHER;
5171       else if (RHSSafe)
5172         NaNBehavior = SPNB_RETURNS_NAN;
5173       else
5174         // Completely unsafe.
5175         return {SPF_UNKNOWN, SPNB_NA, false};
5176     }
5177   }
5178 
5179   if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
5180     std::swap(CmpLHS, CmpRHS);
5181     Pred = CmpInst::getSwappedPredicate(Pred);
5182     if (NaNBehavior == SPNB_RETURNS_NAN)
5183       NaNBehavior = SPNB_RETURNS_OTHER;
5184     else if (NaNBehavior == SPNB_RETURNS_OTHER)
5185       NaNBehavior = SPNB_RETURNS_NAN;
5186     Ordered = !Ordered;
5187   }
5188 
5189   // ([if]cmp X, Y) ? X : Y
5190   if (TrueVal == CmpLHS && FalseVal == CmpRHS) {
5191     switch (Pred) {
5192     default: return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
5193     case ICmpInst::ICMP_UGT:
5194     case ICmpInst::ICMP_UGE: return {SPF_UMAX, SPNB_NA, false};
5195     case ICmpInst::ICMP_SGT:
5196     case ICmpInst::ICMP_SGE: return {SPF_SMAX, SPNB_NA, false};
5197     case ICmpInst::ICMP_ULT:
5198     case ICmpInst::ICMP_ULE: return {SPF_UMIN, SPNB_NA, false};
5199     case ICmpInst::ICMP_SLT:
5200     case ICmpInst::ICMP_SLE: return {SPF_SMIN, SPNB_NA, false};
5201     case FCmpInst::FCMP_UGT:
5202     case FCmpInst::FCMP_UGE:
5203     case FCmpInst::FCMP_OGT:
5204     case FCmpInst::FCMP_OGE: return {SPF_FMAXNUM, NaNBehavior, Ordered};
5205     case FCmpInst::FCMP_ULT:
5206     case FCmpInst::FCMP_ULE:
5207     case FCmpInst::FCMP_OLT:
5208     case FCmpInst::FCMP_OLE: return {SPF_FMINNUM, NaNBehavior, Ordered};
5209     }
5210   }
5211 
5212   if (isKnownNegation(TrueVal, FalseVal)) {
5213     // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
5214     // match against either LHS or sext(LHS).
5215     auto MaybeSExtCmpLHS =
5216         m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
5217     auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
5218     auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
5219     if (match(TrueVal, MaybeSExtCmpLHS)) {
5220       // Set the return values. If the compare uses the negated value (-X >s 0),
5221       // swap the return values because the negated value is always 'RHS'.
5222       LHS = TrueVal;
5223       RHS = FalseVal;
5224       if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
5225         std::swap(LHS, RHS);
5226 
5227       // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
5228       // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
5229       if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
5230         return {SPF_ABS, SPNB_NA, false};
5231 
5232       // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
5233       if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
5234         return {SPF_ABS, SPNB_NA, false};
5235 
5236       // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
5237       // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
5238       if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
5239         return {SPF_NABS, SPNB_NA, false};
5240     }
5241     else if (match(FalseVal, MaybeSExtCmpLHS)) {
5242       // Set the return values. If the compare uses the negated value (-X >s 0),
5243       // swap the return values because the negated value is always 'RHS'.
5244       LHS = FalseVal;
5245       RHS = TrueVal;
5246       if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
5247         std::swap(LHS, RHS);
5248 
5249       // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
5250       // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
5251       if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
5252         return {SPF_NABS, SPNB_NA, false};
5253 
5254       // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
5255       // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
5256       if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
5257         return {SPF_ABS, SPNB_NA, false};
5258     }
5259   }
5260 
5261   if (CmpInst::isIntPredicate(Pred))
5262     return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
5263 
5264   // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
5265   // may return either -0.0 or 0.0, so fcmp/select pair has stricter
5266   // semantics than minNum. Be conservative in such case.
5267   if (NaNBehavior != SPNB_RETURNS_ANY ||
5268       (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
5269        !isKnownNonZero(CmpRHS)))
5270     return {SPF_UNKNOWN, SPNB_NA, false};
5271 
5272   return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
5273 }
5274 
5275 /// Helps to match a select pattern in case of a type mismatch.
5276 ///
5277 /// The function processes the case when type of true and false values of a
5278 /// select instruction differs from type of the cmp instruction operands because
5279 /// of a cast instruction. The function checks if it is legal to move the cast
5280 /// operation after "select". If yes, it returns the new second value of
5281 /// "select" (with the assumption that cast is moved):
5282 /// 1. As operand of cast instruction when both values of "select" are same cast
5283 /// instructions.
5284 /// 2. As restored constant (by applying reverse cast operation) when the first
5285 /// value of the "select" is a cast operation and the second value is a
5286 /// constant.
5287 /// NOTE: We return only the new second value because the first value could be
5288 /// accessed as operand of cast instruction.
5289 static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
5290                               Instruction::CastOps *CastOp) {
5291   auto *Cast1 = dyn_cast<CastInst>(V1);
5292   if (!Cast1)
5293     return nullptr;
5294 
5295   *CastOp = Cast1->getOpcode();
5296   Type *SrcTy = Cast1->getSrcTy();
5297   if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
5298     // If V1 and V2 are both the same cast from the same type, look through V1.
5299     if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
5300       return Cast2->getOperand(0);
5301     return nullptr;
5302   }
5303 
5304   auto *C = dyn_cast<Constant>(V2);
5305   if (!C)
5306     return nullptr;
5307 
5308   Constant *CastedTo = nullptr;
5309   switch (*CastOp) {
5310   case Instruction::ZExt:
5311     if (CmpI->isUnsigned())
5312       CastedTo = ConstantExpr::getTrunc(C, SrcTy);
5313     break;
5314   case Instruction::SExt:
5315     if (CmpI->isSigned())
5316       CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
5317     break;
5318   case Instruction::Trunc:
5319     Constant *CmpConst;
5320     if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
5321         CmpConst->getType() == SrcTy) {
5322       // Here we have the following case:
5323       //
5324       //   %cond = cmp iN %x, CmpConst
5325       //   %tr = trunc iN %x to iK
5326       //   %narrowsel = select i1 %cond, iK %t, iK C
5327       //
5328       // We can always move trunc after select operation:
5329       //
5330       //   %cond = cmp iN %x, CmpConst
5331       //   %widesel = select i1 %cond, iN %x, iN CmpConst
5332       //   %tr = trunc iN %widesel to iK
5333       //
5334       // Note that C could be extended in any way because we don't care about
5335       // upper bits after truncation. It can't be abs pattern, because it would
5336       // look like:
5337       //
5338       //   select i1 %cond, x, -x.
5339       //
5340       // So only min/max pattern could be matched. Such match requires widened C
5341       // == CmpConst. That is why set widened C = CmpConst, condition trunc
5342       // CmpConst == C is checked below.
5343       CastedTo = CmpConst;
5344     } else {
5345       CastedTo = ConstantExpr::getIntegerCast(C, SrcTy, CmpI->isSigned());
5346     }
5347     break;
5348   case Instruction::FPTrunc:
5349     CastedTo = ConstantExpr::getFPExtend(C, SrcTy, true);
5350     break;
5351   case Instruction::FPExt:
5352     CastedTo = ConstantExpr::getFPTrunc(C, SrcTy, true);
5353     break;
5354   case Instruction::FPToUI:
5355     CastedTo = ConstantExpr::getUIToFP(C, SrcTy, true);
5356     break;
5357   case Instruction::FPToSI:
5358     CastedTo = ConstantExpr::getSIToFP(C, SrcTy, true);
5359     break;
5360   case Instruction::UIToFP:
5361     CastedTo = ConstantExpr::getFPToUI(C, SrcTy, true);
5362     break;
5363   case Instruction::SIToFP:
5364     CastedTo = ConstantExpr::getFPToSI(C, SrcTy, true);
5365     break;
5366   default:
5367     break;
5368   }
5369 
5370   if (!CastedTo)
5371     return nullptr;
5372 
5373   // Make sure the cast doesn't lose any information.
5374   Constant *CastedBack =
5375       ConstantExpr::getCast(*CastOp, CastedTo, C->getType(), true);
5376   if (CastedBack != C)
5377     return nullptr;
5378 
5379   return CastedTo;
5380 }
5381 
5382 SelectPatternResult llvm::matchSelectPattern(Value *V, Value *&LHS, Value *&RHS,
5383                                              Instruction::CastOps *CastOp,
5384                                              unsigned Depth) {
5385   if (Depth >= MaxDepth)
5386     return {SPF_UNKNOWN, SPNB_NA, false};
5387 
5388   SelectInst *SI = dyn_cast<SelectInst>(V);
5389   if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
5390 
5391   CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
5392   if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
5393 
5394   Value *TrueVal = SI->getTrueValue();
5395   Value *FalseVal = SI->getFalseValue();
5396 
5397   return llvm::matchDecomposedSelectPattern(CmpI, TrueVal, FalseVal, LHS, RHS,
5398                                             CastOp, Depth);
5399 }
5400 
5401 SelectPatternResult llvm::matchDecomposedSelectPattern(
5402     CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
5403     Instruction::CastOps *CastOp, unsigned Depth) {
5404   CmpInst::Predicate Pred = CmpI->getPredicate();
5405   Value *CmpLHS = CmpI->getOperand(0);
5406   Value *CmpRHS = CmpI->getOperand(1);
5407   FastMathFlags FMF;
5408   if (isa<FPMathOperator>(CmpI))
5409     FMF = CmpI->getFastMathFlags();
5410 
5411   // Bail out early.
5412   if (CmpI->isEquality())
5413     return {SPF_UNKNOWN, SPNB_NA, false};
5414 
5415   // Deal with type mismatches.
5416   if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
5417     if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
5418       // If this is a potential fmin/fmax with a cast to integer, then ignore
5419       // -0.0 because there is no corresponding integer value.
5420       if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
5421         FMF.setNoSignedZeros();
5422       return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
5423                                   cast<CastInst>(TrueVal)->getOperand(0), C,
5424                                   LHS, RHS, Depth);
5425     }
5426     if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
5427       // If this is a potential fmin/fmax with a cast to integer, then ignore
5428       // -0.0 because there is no corresponding integer value.
5429       if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
5430         FMF.setNoSignedZeros();
5431       return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
5432                                   C, cast<CastInst>(FalseVal)->getOperand(0),
5433                                   LHS, RHS, Depth);
5434     }
5435   }
5436   return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
5437                               LHS, RHS, Depth);
5438 }
5439 
5440 CmpInst::Predicate llvm::getMinMaxPred(SelectPatternFlavor SPF, bool Ordered) {
5441   if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
5442   if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
5443   if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
5444   if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
5445   if (SPF == SPF_FMINNUM)
5446     return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
5447   if (SPF == SPF_FMAXNUM)
5448     return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
5449   llvm_unreachable("unhandled!");
5450 }
5451 
5452 SelectPatternFlavor llvm::getInverseMinMaxFlavor(SelectPatternFlavor SPF) {
5453   if (SPF == SPF_SMIN) return SPF_SMAX;
5454   if (SPF == SPF_UMIN) return SPF_UMAX;
5455   if (SPF == SPF_SMAX) return SPF_SMIN;
5456   if (SPF == SPF_UMAX) return SPF_UMIN;
5457   llvm_unreachable("unhandled!");
5458 }
5459 
5460 CmpInst::Predicate llvm::getInverseMinMaxPred(SelectPatternFlavor SPF) {
5461   return getMinMaxPred(getInverseMinMaxFlavor(SPF));
5462 }
5463 
5464 /// Return true if "icmp Pred LHS RHS" is always true.
5465 static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
5466                             const Value *RHS, const DataLayout &DL,
5467                             unsigned Depth) {
5468   assert(!LHS->getType()->isVectorTy() && "TODO: extend to handle vectors!");
5469   if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
5470     return true;
5471 
5472   switch (Pred) {
5473   default:
5474     return false;
5475 
5476   case CmpInst::ICMP_SLE: {
5477     const APInt *C;
5478 
5479     // LHS s<= LHS +_{nsw} C   if C >= 0
5480     if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))))
5481       return !C->isNegative();
5482     return false;
5483   }
5484 
5485   case CmpInst::ICMP_ULE: {
5486     const APInt *C;
5487 
5488     // LHS u<= LHS +_{nuw} C   for any C
5489     if (match(RHS, m_NUWAdd(m_Specific(LHS), m_APInt(C))))
5490       return true;
5491 
5492     // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
5493     auto MatchNUWAddsToSameValue = [&](const Value *A, const Value *B,
5494                                        const Value *&X,
5495                                        const APInt *&CA, const APInt *&CB) {
5496       if (match(A, m_NUWAdd(m_Value(X), m_APInt(CA))) &&
5497           match(B, m_NUWAdd(m_Specific(X), m_APInt(CB))))
5498         return true;
5499 
5500       // If X & C == 0 then (X | C) == X +_{nuw} C
5501       if (match(A, m_Or(m_Value(X), m_APInt(CA))) &&
5502           match(B, m_Or(m_Specific(X), m_APInt(CB)))) {
5503         KnownBits Known(CA->getBitWidth());
5504         computeKnownBits(X, Known, DL, Depth + 1, /*AC*/ nullptr,
5505                          /*CxtI*/ nullptr, /*DT*/ nullptr);
5506         if (CA->isSubsetOf(Known.Zero) && CB->isSubsetOf(Known.Zero))
5507           return true;
5508       }
5509 
5510       return false;
5511     };
5512 
5513     const Value *X;
5514     const APInt *CLHS, *CRHS;
5515     if (MatchNUWAddsToSameValue(LHS, RHS, X, CLHS, CRHS))
5516       return CLHS->ule(*CRHS);
5517 
5518     return false;
5519   }
5520   }
5521 }
5522 
5523 /// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
5524 /// ALHS ARHS" is true.  Otherwise, return None.
5525 static Optional<bool>
5526 isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS,
5527                       const Value *ARHS, const Value *BLHS, const Value *BRHS,
5528                       const DataLayout &DL, unsigned Depth) {
5529   switch (Pred) {
5530   default:
5531     return None;
5532 
5533   case CmpInst::ICMP_SLT:
5534   case CmpInst::ICMP_SLE:
5535     if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS, DL, Depth) &&
5536         isTruePredicate(CmpInst::ICMP_SLE, ARHS, BRHS, DL, Depth))
5537       return true;
5538     return None;
5539 
5540   case CmpInst::ICMP_ULT:
5541   case CmpInst::ICMP_ULE:
5542     if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS, DL, Depth) &&
5543         isTruePredicate(CmpInst::ICMP_ULE, ARHS, BRHS, DL, Depth))
5544       return true;
5545     return None;
5546   }
5547 }
5548 
5549 /// Return true if the operands of the two compares match.  IsSwappedOps is true
5550 /// when the operands match, but are swapped.
5551 static bool isMatchingOps(const Value *ALHS, const Value *ARHS,
5552                           const Value *BLHS, const Value *BRHS,
5553                           bool &IsSwappedOps) {
5554 
5555   bool IsMatchingOps = (ALHS == BLHS && ARHS == BRHS);
5556   IsSwappedOps = (ALHS == BRHS && ARHS == BLHS);
5557   return IsMatchingOps || IsSwappedOps;
5558 }
5559 
5560 /// Return true if "icmp1 APred X, Y" implies "icmp2 BPred X, Y" is true.
5561 /// Return false if "icmp1 APred X, Y" implies "icmp2 BPred X, Y" is false.
5562 /// Otherwise, return None if we can't infer anything.
5563 static Optional<bool> isImpliedCondMatchingOperands(CmpInst::Predicate APred,
5564                                                     CmpInst::Predicate BPred,
5565                                                     bool AreSwappedOps) {
5566   // Canonicalize the predicate as if the operands were not commuted.
5567   if (AreSwappedOps)
5568     BPred = ICmpInst::getSwappedPredicate(BPred);
5569 
5570   if (CmpInst::isImpliedTrueByMatchingCmp(APred, BPred))
5571     return true;
5572   if (CmpInst::isImpliedFalseByMatchingCmp(APred, BPred))
5573     return false;
5574 
5575   return None;
5576 }
5577 
5578 /// Return true if "icmp APred X, C1" implies "icmp BPred X, C2" is true.
5579 /// Return false if "icmp APred X, C1" implies "icmp BPred X, C2" is false.
5580 /// Otherwise, return None if we can't infer anything.
5581 static Optional<bool>
5582 isImpliedCondMatchingImmOperands(CmpInst::Predicate APred,
5583                                  const ConstantInt *C1,
5584                                  CmpInst::Predicate BPred,
5585                                  const ConstantInt *C2) {
5586   ConstantRange DomCR =
5587       ConstantRange::makeExactICmpRegion(APred, C1->getValue());
5588   ConstantRange CR =
5589       ConstantRange::makeAllowedICmpRegion(BPred, C2->getValue());
5590   ConstantRange Intersection = DomCR.intersectWith(CR);
5591   ConstantRange Difference = DomCR.difference(CR);
5592   if (Intersection.isEmptySet())
5593     return false;
5594   if (Difference.isEmptySet())
5595     return true;
5596   return None;
5597 }
5598 
5599 /// Return true if LHS implies RHS is true.  Return false if LHS implies RHS is
5600 /// false.  Otherwise, return None if we can't infer anything.
5601 static Optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
5602                                          CmpInst::Predicate BPred,
5603                                          const Value *BLHS, const Value *BRHS,
5604                                          const DataLayout &DL, bool LHSIsTrue,
5605                                          unsigned Depth) {
5606   Value *ALHS = LHS->getOperand(0);
5607   Value *ARHS = LHS->getOperand(1);
5608 
5609   // The rest of the logic assumes the LHS condition is true.  If that's not the
5610   // case, invert the predicate to make it so.
5611   CmpInst::Predicate APred =
5612       LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate();
5613 
5614   // Can we infer anything when the two compares have matching operands?
5615   bool AreSwappedOps;
5616   if (isMatchingOps(ALHS, ARHS, BLHS, BRHS, AreSwappedOps)) {
5617     if (Optional<bool> Implication = isImpliedCondMatchingOperands(
5618             APred, BPred, AreSwappedOps))
5619       return Implication;
5620     // No amount of additional analysis will infer the second condition, so
5621     // early exit.
5622     return None;
5623   }
5624 
5625   // Can we infer anything when the LHS operands match and the RHS operands are
5626   // constants (not necessarily matching)?
5627   if (ALHS == BLHS && isa<ConstantInt>(ARHS) && isa<ConstantInt>(BRHS)) {
5628     if (Optional<bool> Implication = isImpliedCondMatchingImmOperands(
5629             APred, cast<ConstantInt>(ARHS), BPred, cast<ConstantInt>(BRHS)))
5630       return Implication;
5631     // No amount of additional analysis will infer the second condition, so
5632     // early exit.
5633     return None;
5634   }
5635 
5636   if (APred == BPred)
5637     return isImpliedCondOperands(APred, ALHS, ARHS, BLHS, BRHS, DL, Depth);
5638   return None;
5639 }
5640 
5641 /// Return true if LHS implies RHS is true.  Return false if LHS implies RHS is
5642 /// false.  Otherwise, return None if we can't infer anything.  We expect the
5643 /// RHS to be an icmp and the LHS to be an 'and' or an 'or' instruction.
5644 static Optional<bool>
5645 isImpliedCondAndOr(const BinaryOperator *LHS, CmpInst::Predicate RHSPred,
5646                    const Value *RHSOp0, const Value *RHSOp1,
5647 
5648                    const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
5649   // The LHS must be an 'or' or an 'and' instruction.
5650   assert((LHS->getOpcode() == Instruction::And ||
5651           LHS->getOpcode() == Instruction::Or) &&
5652          "Expected LHS to be 'and' or 'or'.");
5653 
5654   assert(Depth <= MaxDepth && "Hit recursion limit");
5655 
5656   // If the result of an 'or' is false, then we know both legs of the 'or' are
5657   // false.  Similarly, if the result of an 'and' is true, then we know both
5658   // legs of the 'and' are true.
5659   Value *ALHS, *ARHS;
5660   if ((!LHSIsTrue && match(LHS, m_Or(m_Value(ALHS), m_Value(ARHS)))) ||
5661       (LHSIsTrue && match(LHS, m_And(m_Value(ALHS), m_Value(ARHS))))) {
5662     // FIXME: Make this non-recursion.
5663     if (Optional<bool> Implication = isImpliedCondition(
5664             ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
5665       return Implication;
5666     if (Optional<bool> Implication = isImpliedCondition(
5667             ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
5668       return Implication;
5669     return None;
5670   }
5671   return None;
5672 }
5673 
5674 Optional<bool>
5675 llvm::isImpliedCondition(const Value *LHS, CmpInst::Predicate RHSPred,
5676                          const Value *RHSOp0, const Value *RHSOp1,
5677                          const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
5678   // Bail out when we hit the limit.
5679   if (Depth == MaxDepth)
5680     return None;
5681 
5682   // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
5683   // example.
5684   if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
5685     return None;
5686 
5687   Type *OpTy = LHS->getType();
5688   assert(OpTy->isIntOrIntVectorTy(1) && "Expected integer type only!");
5689 
5690   // FIXME: Extending the code below to handle vectors.
5691   if (OpTy->isVectorTy())
5692     return None;
5693 
5694   assert(OpTy->isIntegerTy(1) && "implied by above");
5695 
5696   // Both LHS and RHS are icmps.
5697   const ICmpInst *LHSCmp = dyn_cast<ICmpInst>(LHS);
5698   if (LHSCmp)
5699     return isImpliedCondICmps(LHSCmp, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
5700                               Depth);
5701 
5702   /// The LHS should be an 'or' or an 'and' instruction.  We expect the RHS to
5703   /// be / an icmp. FIXME: Add support for and/or on the RHS.
5704   const BinaryOperator *LHSBO = dyn_cast<BinaryOperator>(LHS);
5705   if (LHSBO) {
5706     if ((LHSBO->getOpcode() == Instruction::And ||
5707          LHSBO->getOpcode() == Instruction::Or))
5708       return isImpliedCondAndOr(LHSBO, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
5709                                 Depth);
5710   }
5711   return None;
5712 }
5713 
5714 Optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
5715                                         const DataLayout &DL, bool LHSIsTrue,
5716                                         unsigned Depth) {
5717   // LHS ==> RHS by definition
5718   if (LHS == RHS)
5719     return LHSIsTrue;
5720 
5721   const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS);
5722   if (RHSCmp)
5723     return isImpliedCondition(LHS, RHSCmp->getPredicate(),
5724                               RHSCmp->getOperand(0), RHSCmp->getOperand(1), DL,
5725                               LHSIsTrue, Depth);
5726   return None;
5727 }
5728 
5729 // Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
5730 // condition dominating ContextI or nullptr, if no condition is found.
5731 static std::pair<Value *, bool>
5732 getDomPredecessorCondition(const Instruction *ContextI) {
5733   if (!ContextI || !ContextI->getParent())
5734     return {nullptr, false};
5735 
5736   // TODO: This is a poor/cheap way to determine dominance. Should we use a
5737   // dominator tree (eg, from a SimplifyQuery) instead?
5738   const BasicBlock *ContextBB = ContextI->getParent();
5739   const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
5740   if (!PredBB)
5741     return {nullptr, false};
5742 
5743   // We need a conditional branch in the predecessor.
5744   Value *PredCond;
5745   BasicBlock *TrueBB, *FalseBB;
5746   if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
5747     return {nullptr, false};
5748 
5749   // The branch should get simplified. Don't bother simplifying this condition.
5750   if (TrueBB == FalseBB)
5751     return {nullptr, false};
5752 
5753   assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
5754          "Predecessor block does not point to successor?");
5755 
5756   // Is this condition implied by the predecessor condition?
5757   return {PredCond, TrueBB == ContextBB};
5758 }
5759 
5760 Optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
5761                                              const Instruction *ContextI,
5762                                              const DataLayout &DL) {
5763   assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
5764   auto PredCond = getDomPredecessorCondition(ContextI);
5765   if (PredCond.first)
5766     return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
5767   return None;
5768 }
5769 
5770 Optional<bool> llvm::isImpliedByDomCondition(CmpInst::Predicate Pred,
5771                                              const Value *LHS, const Value *RHS,
5772                                              const Instruction *ContextI,
5773                                              const DataLayout &DL) {
5774   auto PredCond = getDomPredecessorCondition(ContextI);
5775   if (PredCond.first)
5776     return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
5777                               PredCond.second);
5778   return None;
5779 }
5780 
5781 static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower,
5782                               APInt &Upper, const InstrInfoQuery &IIQ) {
5783   unsigned Width = Lower.getBitWidth();
5784   const APInt *C;
5785   switch (BO.getOpcode()) {
5786   case Instruction::Add:
5787     if (match(BO.getOperand(1), m_APInt(C)) && !C->isNullValue()) {
5788       // FIXME: If we have both nuw and nsw, we should reduce the range further.
5789       if (IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(&BO))) {
5790         // 'add nuw x, C' produces [C, UINT_MAX].
5791         Lower = *C;
5792       } else if (IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(&BO))) {
5793         if (C->isNegative()) {
5794           // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
5795           Lower = APInt::getSignedMinValue(Width);
5796           Upper = APInt::getSignedMaxValue(Width) + *C + 1;
5797         } else {
5798           // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
5799           Lower = APInt::getSignedMinValue(Width) + *C;
5800           Upper = APInt::getSignedMaxValue(Width) + 1;
5801         }
5802       }
5803     }
5804     break;
5805 
5806   case Instruction::And:
5807     if (match(BO.getOperand(1), m_APInt(C)))
5808       // 'and x, C' produces [0, C].
5809       Upper = *C + 1;
5810     break;
5811 
5812   case Instruction::Or:
5813     if (match(BO.getOperand(1), m_APInt(C)))
5814       // 'or x, C' produces [C, UINT_MAX].
5815       Lower = *C;
5816     break;
5817 
5818   case Instruction::AShr:
5819     if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
5820       // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
5821       Lower = APInt::getSignedMinValue(Width).ashr(*C);
5822       Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
5823     } else if (match(BO.getOperand(0), m_APInt(C))) {
5824       unsigned ShiftAmount = Width - 1;
5825       if (!C->isNullValue() && IIQ.isExact(&BO))
5826         ShiftAmount = C->countTrailingZeros();
5827       if (C->isNegative()) {
5828         // 'ashr C, x' produces [C, C >> (Width-1)]
5829         Lower = *C;
5830         Upper = C->ashr(ShiftAmount) + 1;
5831       } else {
5832         // 'ashr C, x' produces [C >> (Width-1), C]
5833         Lower = C->ashr(ShiftAmount);
5834         Upper = *C + 1;
5835       }
5836     }
5837     break;
5838 
5839   case Instruction::LShr:
5840     if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
5841       // 'lshr x, C' produces [0, UINT_MAX >> C].
5842       Upper = APInt::getAllOnesValue(Width).lshr(*C) + 1;
5843     } else if (match(BO.getOperand(0), m_APInt(C))) {
5844       // 'lshr C, x' produces [C >> (Width-1), C].
5845       unsigned ShiftAmount = Width - 1;
5846       if (!C->isNullValue() && IIQ.isExact(&BO))
5847         ShiftAmount = C->countTrailingZeros();
5848       Lower = C->lshr(ShiftAmount);
5849       Upper = *C + 1;
5850     }
5851     break;
5852 
5853   case Instruction::Shl:
5854     if (match(BO.getOperand(0), m_APInt(C))) {
5855       if (IIQ.hasNoUnsignedWrap(&BO)) {
5856         // 'shl nuw C, x' produces [C, C << CLZ(C)]
5857         Lower = *C;
5858         Upper = Lower.shl(Lower.countLeadingZeros()) + 1;
5859       } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
5860         if (C->isNegative()) {
5861           // 'shl nsw C, x' produces [C << CLO(C)-1, C]
5862           unsigned ShiftAmount = C->countLeadingOnes() - 1;
5863           Lower = C->shl(ShiftAmount);
5864           Upper = *C + 1;
5865         } else {
5866           // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
5867           unsigned ShiftAmount = C->countLeadingZeros() - 1;
5868           Lower = *C;
5869           Upper = C->shl(ShiftAmount) + 1;
5870         }
5871       }
5872     }
5873     break;
5874 
5875   case Instruction::SDiv:
5876     if (match(BO.getOperand(1), m_APInt(C))) {
5877       APInt IntMin = APInt::getSignedMinValue(Width);
5878       APInt IntMax = APInt::getSignedMaxValue(Width);
5879       if (C->isAllOnesValue()) {
5880         // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
5881         //    where C != -1 and C != 0 and C != 1
5882         Lower = IntMin + 1;
5883         Upper = IntMax + 1;
5884       } else if (C->countLeadingZeros() < Width - 1) {
5885         // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
5886         //    where C != -1 and C != 0 and C != 1
5887         Lower = IntMin.sdiv(*C);
5888         Upper = IntMax.sdiv(*C);
5889         if (Lower.sgt(Upper))
5890           std::swap(Lower, Upper);
5891         Upper = Upper + 1;
5892         assert(Upper != Lower && "Upper part of range has wrapped!");
5893       }
5894     } else if (match(BO.getOperand(0), m_APInt(C))) {
5895       if (C->isMinSignedValue()) {
5896         // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
5897         Lower = *C;
5898         Upper = Lower.lshr(1) + 1;
5899       } else {
5900         // 'sdiv C, x' produces [-|C|, |C|].
5901         Upper = C->abs() + 1;
5902         Lower = (-Upper) + 1;
5903       }
5904     }
5905     break;
5906 
5907   case Instruction::UDiv:
5908     if (match(BO.getOperand(1), m_APInt(C)) && !C->isNullValue()) {
5909       // 'udiv x, C' produces [0, UINT_MAX / C].
5910       Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
5911     } else if (match(BO.getOperand(0), m_APInt(C))) {
5912       // 'udiv C, x' produces [0, C].
5913       Upper = *C + 1;
5914     }
5915     break;
5916 
5917   case Instruction::SRem:
5918     if (match(BO.getOperand(1), m_APInt(C))) {
5919       // 'srem x, C' produces (-|C|, |C|).
5920       Upper = C->abs();
5921       Lower = (-Upper) + 1;
5922     }
5923     break;
5924 
5925   case Instruction::URem:
5926     if (match(BO.getOperand(1), m_APInt(C)))
5927       // 'urem x, C' produces [0, C).
5928       Upper = *C;
5929     break;
5930 
5931   default:
5932     break;
5933   }
5934 }
5935 
5936 static void setLimitsForIntrinsic(const IntrinsicInst &II, APInt &Lower,
5937                                   APInt &Upper) {
5938   unsigned Width = Lower.getBitWidth();
5939   const APInt *C;
5940   switch (II.getIntrinsicID()) {
5941   case Intrinsic::uadd_sat:
5942     // uadd.sat(x, C) produces [C, UINT_MAX].
5943     if (match(II.getOperand(0), m_APInt(C)) ||
5944         match(II.getOperand(1), m_APInt(C)))
5945       Lower = *C;
5946     break;
5947   case Intrinsic::sadd_sat:
5948     if (match(II.getOperand(0), m_APInt(C)) ||
5949         match(II.getOperand(1), m_APInt(C))) {
5950       if (C->isNegative()) {
5951         // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
5952         Lower = APInt::getSignedMinValue(Width);
5953         Upper = APInt::getSignedMaxValue(Width) + *C + 1;
5954       } else {
5955         // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
5956         Lower = APInt::getSignedMinValue(Width) + *C;
5957         Upper = APInt::getSignedMaxValue(Width) + 1;
5958       }
5959     }
5960     break;
5961   case Intrinsic::usub_sat:
5962     // usub.sat(C, x) produces [0, C].
5963     if (match(II.getOperand(0), m_APInt(C)))
5964       Upper = *C + 1;
5965     // usub.sat(x, C) produces [0, UINT_MAX - C].
5966     else if (match(II.getOperand(1), m_APInt(C)))
5967       Upper = APInt::getMaxValue(Width) - *C + 1;
5968     break;
5969   case Intrinsic::ssub_sat:
5970     if (match(II.getOperand(0), m_APInt(C))) {
5971       if (C->isNegative()) {
5972         // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
5973         Lower = APInt::getSignedMinValue(Width);
5974         Upper = *C - APInt::getSignedMinValue(Width) + 1;
5975       } else {
5976         // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
5977         Lower = *C - APInt::getSignedMaxValue(Width);
5978         Upper = APInt::getSignedMaxValue(Width) + 1;
5979       }
5980     } else if (match(II.getOperand(1), m_APInt(C))) {
5981       if (C->isNegative()) {
5982         // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
5983         Lower = APInt::getSignedMinValue(Width) - *C;
5984         Upper = APInt::getSignedMaxValue(Width) + 1;
5985       } else {
5986         // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
5987         Lower = APInt::getSignedMinValue(Width);
5988         Upper = APInt::getSignedMaxValue(Width) - *C + 1;
5989       }
5990     }
5991     break;
5992   default:
5993     break;
5994   }
5995 }
5996 
5997 static void setLimitsForSelectPattern(const SelectInst &SI, APInt &Lower,
5998                                       APInt &Upper, const InstrInfoQuery &IIQ) {
5999   const Value *LHS = nullptr, *RHS = nullptr;
6000   SelectPatternResult R = matchSelectPattern(&SI, LHS, RHS);
6001   if (R.Flavor == SPF_UNKNOWN)
6002     return;
6003 
6004   unsigned BitWidth = SI.getType()->getScalarSizeInBits();
6005 
6006   if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
6007     // If the negation part of the abs (in RHS) has the NSW flag,
6008     // then the result of abs(X) is [0..SIGNED_MAX],
6009     // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
6010     Lower = APInt::getNullValue(BitWidth);
6011     if (match(RHS, m_Neg(m_Specific(LHS))) &&
6012         IIQ.hasNoSignedWrap(cast<Instruction>(RHS)))
6013       Upper = APInt::getSignedMaxValue(BitWidth) + 1;
6014     else
6015       Upper = APInt::getSignedMinValue(BitWidth) + 1;
6016     return;
6017   }
6018 
6019   if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
6020     // The result of -abs(X) is <= 0.
6021     Lower = APInt::getSignedMinValue(BitWidth);
6022     Upper = APInt(BitWidth, 1);
6023     return;
6024   }
6025 
6026   const APInt *C;
6027   if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
6028     return;
6029 
6030   switch (R.Flavor) {
6031     case SPF_UMIN:
6032       Upper = *C + 1;
6033       break;
6034     case SPF_UMAX:
6035       Lower = *C;
6036       break;
6037     case SPF_SMIN:
6038       Lower = APInt::getSignedMinValue(BitWidth);
6039       Upper = *C + 1;
6040       break;
6041     case SPF_SMAX:
6042       Lower = *C;
6043       Upper = APInt::getSignedMaxValue(BitWidth) + 1;
6044       break;
6045     default:
6046       break;
6047   }
6048 }
6049 
6050 ConstantRange llvm::computeConstantRange(const Value *V, bool UseInstrInfo) {
6051   assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
6052 
6053   const APInt *C;
6054   if (match(V, m_APInt(C)))
6055     return ConstantRange(*C);
6056 
6057   InstrInfoQuery IIQ(UseInstrInfo);
6058   unsigned BitWidth = V->getType()->getScalarSizeInBits();
6059   APInt Lower = APInt(BitWidth, 0);
6060   APInt Upper = APInt(BitWidth, 0);
6061   if (auto *BO = dyn_cast<BinaryOperator>(V))
6062     setLimitsForBinOp(*BO, Lower, Upper, IIQ);
6063   else if (auto *II = dyn_cast<IntrinsicInst>(V))
6064     setLimitsForIntrinsic(*II, Lower, Upper);
6065   else if (auto *SI = dyn_cast<SelectInst>(V))
6066     setLimitsForSelectPattern(*SI, Lower, Upper, IIQ);
6067 
6068   ConstantRange CR = ConstantRange::getNonEmpty(Lower, Upper);
6069 
6070   if (auto *I = dyn_cast<Instruction>(V))
6071     if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
6072       CR = CR.intersectWith(getConstantRangeFromMetadata(*Range));
6073 
6074   return CR;
6075 }
6076 
6077 static Optional<int64_t>
6078 getOffsetFromIndex(const GEPOperator *GEP, unsigned Idx, const DataLayout &DL) {
6079   // Skip over the first indices.
6080   gep_type_iterator GTI = gep_type_begin(GEP);
6081   for (unsigned i = 1; i != Idx; ++i, ++GTI)
6082     /*skip along*/;
6083 
6084   // Compute the offset implied by the rest of the indices.
6085   int64_t Offset = 0;
6086   for (unsigned i = Idx, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
6087     ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand(i));
6088     if (!OpC)
6089       return None;
6090     if (OpC->isZero())
6091       continue; // No offset.
6092 
6093     // Handle struct indices, which add their field offset to the pointer.
6094     if (StructType *STy = GTI.getStructTypeOrNull()) {
6095       Offset += DL.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());
6096       continue;
6097     }
6098 
6099     // Otherwise, we have a sequential type like an array or vector.  Multiply
6100     // the index by the ElementSize.
6101     uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType());
6102     Offset += Size * OpC->getSExtValue();
6103   }
6104 
6105   return Offset;
6106 }
6107 
6108 Optional<int64_t> llvm::isPointerOffset(const Value *Ptr1, const Value *Ptr2,
6109                                         const DataLayout &DL) {
6110   Ptr1 = Ptr1->stripPointerCasts();
6111   Ptr2 = Ptr2->stripPointerCasts();
6112 
6113   // Handle the trivial case first.
6114   if (Ptr1 == Ptr2) {
6115     return 0;
6116   }
6117 
6118   const GEPOperator *GEP1 = dyn_cast<GEPOperator>(Ptr1);
6119   const GEPOperator *GEP2 = dyn_cast<GEPOperator>(Ptr2);
6120 
6121   // If one pointer is a GEP see if the GEP is a constant offset from the base,
6122   // as in "P" and "gep P, 1".
6123   // Also do this iteratively to handle the the following case:
6124   //   Ptr_t1 = GEP Ptr1, c1
6125   //   Ptr_t2 = GEP Ptr_t1, c2
6126   //   Ptr2 = GEP Ptr_t2, c3
6127   // where we will return c1+c2+c3.
6128   // TODO: Handle the case when both Ptr1 and Ptr2 are GEPs of some common base
6129   // -- replace getOffsetFromBase with getOffsetAndBase, check that the bases
6130   // are the same, and return the difference between offsets.
6131   auto getOffsetFromBase = [&DL](const GEPOperator *GEP,
6132                                  const Value *Ptr) -> Optional<int64_t> {
6133     const GEPOperator *GEP_T = GEP;
6134     int64_t OffsetVal = 0;
6135     bool HasSameBase = false;
6136     while (GEP_T) {
6137       auto Offset = getOffsetFromIndex(GEP_T, 1, DL);
6138       if (!Offset)
6139         return None;
6140       OffsetVal += *Offset;
6141       auto Op0 = GEP_T->getOperand(0)->stripPointerCasts();
6142       if (Op0 == Ptr) {
6143         HasSameBase = true;
6144         break;
6145       }
6146       GEP_T = dyn_cast<GEPOperator>(Op0);
6147     }
6148     if (!HasSameBase)
6149       return None;
6150     return OffsetVal;
6151   };
6152 
6153   if (GEP1) {
6154     auto Offset = getOffsetFromBase(GEP1, Ptr2);
6155     if (Offset)
6156       return -*Offset;
6157   }
6158   if (GEP2) {
6159     auto Offset = getOffsetFromBase(GEP2, Ptr1);
6160     if (Offset)
6161       return Offset;
6162   }
6163 
6164   // Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical
6165   // base.  After that base, they may have some number of common (and
6166   // potentially variable) indices.  After that they handle some constant
6167   // offset, which determines their offset from each other.  At this point, we
6168   // handle no other case.
6169   if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0))
6170     return None;
6171 
6172   // Skip any common indices and track the GEP types.
6173   unsigned Idx = 1;
6174   for (; Idx != GEP1->getNumOperands() && Idx != GEP2->getNumOperands(); ++Idx)
6175     if (GEP1->getOperand(Idx) != GEP2->getOperand(Idx))
6176       break;
6177 
6178   auto Offset1 = getOffsetFromIndex(GEP1, Idx, DL);
6179   auto Offset2 = getOffsetFromIndex(GEP2, Idx, DL);
6180   if (!Offset1 || !Offset2)
6181     return None;
6182   return *Offset2 - *Offset1;
6183 }
6184