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