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