File: | include/llvm/IR/PatternMatch.h |
Warning: | line 163, column 9 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- ValueTracking.cpp - Walk computations to compute properties --------===// | ||||
2 | // | ||||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
4 | // See https://llvm.org/LICENSE.txt for license information. | ||||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
6 | // | ||||
7 | //===----------------------------------------------------------------------===// | ||||
8 | // | ||||
9 | // This file contains routines that help analyze properties that chains of | ||||
10 | // computations have. | ||||
11 | // | ||||
12 | //===----------------------------------------------------------------------===// | ||||
13 | |||||
14 | #include "llvm/Analysis/ValueTracking.h" | ||||
15 | #include "llvm/ADT/APFloat.h" | ||||
16 | #include "llvm/ADT/APInt.h" | ||||
17 | #include "llvm/ADT/ArrayRef.h" | ||||
18 | #include "llvm/ADT/None.h" | ||||
19 | #include "llvm/ADT/Optional.h" | ||||
20 | #include "llvm/ADT/STLExtras.h" | ||||
21 | #include "llvm/ADT/SmallPtrSet.h" | ||||
22 | #include "llvm/ADT/SmallSet.h" | ||||
23 | #include "llvm/ADT/SmallVector.h" | ||||
24 | #include "llvm/ADT/StringRef.h" | ||||
25 | #include "llvm/ADT/iterator_range.h" | ||||
26 | #include "llvm/Analysis/AliasAnalysis.h" | ||||
27 | #include "llvm/Analysis/AssumptionCache.h" | ||||
28 | #include "llvm/Analysis/GuardUtils.h" | ||||
29 | #include "llvm/Analysis/InstructionSimplify.h" | ||||
30 | #include "llvm/Analysis/Loads.h" | ||||
31 | #include "llvm/Analysis/LoopInfo.h" | ||||
32 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" | ||||
33 | #include "llvm/Analysis/TargetLibraryInfo.h" | ||||
34 | #include "llvm/IR/Argument.h" | ||||
35 | #include "llvm/IR/Attributes.h" | ||||
36 | #include "llvm/IR/BasicBlock.h" | ||||
37 | #include "llvm/IR/CallSite.h" | ||||
38 | #include "llvm/IR/Constant.h" | ||||
39 | #include "llvm/IR/ConstantRange.h" | ||||
40 | #include "llvm/IR/Constants.h" | ||||
41 | #include "llvm/IR/DataLayout.h" | ||||
42 | #include "llvm/IR/DerivedTypes.h" | ||||
43 | #include "llvm/IR/DiagnosticInfo.h" | ||||
44 | #include "llvm/IR/Dominators.h" | ||||
45 | #include "llvm/IR/Function.h" | ||||
46 | #include "llvm/IR/GetElementPtrTypeIterator.h" | ||||
47 | #include "llvm/IR/GlobalAlias.h" | ||||
48 | #include "llvm/IR/GlobalValue.h" | ||||
49 | #include "llvm/IR/GlobalVariable.h" | ||||
50 | #include "llvm/IR/InstrTypes.h" | ||||
51 | #include "llvm/IR/Instruction.h" | ||||
52 | #include "llvm/IR/Instructions.h" | ||||
53 | #include "llvm/IR/IntrinsicInst.h" | ||||
54 | #include "llvm/IR/Intrinsics.h" | ||||
55 | #include "llvm/IR/LLVMContext.h" | ||||
56 | #include "llvm/IR/Metadata.h" | ||||
57 | #include "llvm/IR/Module.h" | ||||
58 | #include "llvm/IR/Operator.h" | ||||
59 | #include "llvm/IR/PatternMatch.h" | ||||
60 | #include "llvm/IR/Type.h" | ||||
61 | #include "llvm/IR/User.h" | ||||
62 | #include "llvm/IR/Value.h" | ||||
63 | #include "llvm/Support/Casting.h" | ||||
64 | #include "llvm/Support/CommandLine.h" | ||||
65 | #include "llvm/Support/Compiler.h" | ||||
66 | #include "llvm/Support/ErrorHandling.h" | ||||
67 | #include "llvm/Support/KnownBits.h" | ||||
68 | #include "llvm/Support/MathExtras.h" | ||||
69 | #include <algorithm> | ||||
70 | #include <array> | ||||
71 | #include <cassert> | ||||
72 | #include <cstdint> | ||||
73 | #include <iterator> | ||||
74 | #include <utility> | ||||
75 | |||||
76 | using namespace llvm; | ||||
77 | using namespace llvm::PatternMatch; | ||||
78 | |||||
79 | const unsigned MaxDepth = 6; | ||||
80 | |||||
81 | // Controls the number of uses of the value searched for possible | ||||
82 | // dominating comparisons. | ||||
83 | static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses", | ||||
84 | cl::Hidden, cl::init(20)); | ||||
85 | |||||
86 | /// Returns the bitwidth of the given scalar or pointer type. For vector types, | ||||
87 | /// returns the element type's bitwidth. | ||||
88 | static unsigned getBitWidth(Type *Ty, const DataLayout &DL) { | ||||
89 | if (unsigned BitWidth = Ty->getScalarSizeInBits()) | ||||
90 | return BitWidth; | ||||
91 | |||||
92 | return DL.getIndexTypeSizeInBits(Ty); | ||||
93 | } | ||||
94 | |||||
95 | namespace { | ||||
96 | |||||
97 | // Simplifying using an assume can only be done in a particular control-flow | ||||
98 | // context (the context instruction provides that context). If an assume and | ||||
99 | // the context instruction are not in the same block then the DT helps in | ||||
100 | // figuring out if we can use it. | ||||
101 | struct Query { | ||||
102 | const DataLayout &DL; | ||||
103 | AssumptionCache *AC; | ||||
104 | const Instruction *CxtI; | ||||
105 | const DominatorTree *DT; | ||||
106 | |||||
107 | // Unlike the other analyses, this may be a nullptr because not all clients | ||||
108 | // provide it currently. | ||||
109 | OptimizationRemarkEmitter *ORE; | ||||
110 | |||||
111 | /// Set of assumptions that should be excluded from further queries. | ||||
112 | /// This is because of the potential for mutual recursion to cause | ||||
113 | /// computeKnownBits to repeatedly visit the same assume intrinsic. The | ||||
114 | /// classic case of this is assume(x = y), which will attempt to determine | ||||
115 | /// bits in x from bits in y, which will attempt to determine bits in y from | ||||
116 | /// bits in x, etc. Regarding the mutual recursion, computeKnownBits can call | ||||
117 | /// isKnownNonZero, which calls computeKnownBits and isKnownToBeAPowerOfTwo | ||||
118 | /// (all of which can call computeKnownBits), and so on. | ||||
119 | std::array<const Value *, MaxDepth> Excluded; | ||||
120 | |||||
121 | /// If true, it is safe to use metadata during simplification. | ||||
122 | InstrInfoQuery IIQ; | ||||
123 | |||||
124 | unsigned NumExcluded = 0; | ||||
125 | |||||
126 | Query(const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, | ||||
127 | const DominatorTree *DT, bool UseInstrInfo, | ||||
128 | OptimizationRemarkEmitter *ORE = nullptr) | ||||
129 | : DL(DL), AC(AC), CxtI(CxtI), DT(DT), ORE(ORE), IIQ(UseInstrInfo) {} | ||||
130 | |||||
131 | Query(const Query &Q, const Value *NewExcl) | ||||
132 | : DL(Q.DL), AC(Q.AC), CxtI(Q.CxtI), DT(Q.DT), ORE(Q.ORE), IIQ(Q.IIQ), | ||||
133 | NumExcluded(Q.NumExcluded) { | ||||
134 | Excluded = Q.Excluded; | ||||
135 | Excluded[NumExcluded++] = NewExcl; | ||||
136 | assert(NumExcluded <= Excluded.size())((NumExcluded <= Excluded.size()) ? static_cast<void> (0) : __assert_fail ("NumExcluded <= Excluded.size()", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 136, __PRETTY_FUNCTION__)); | ||||
137 | } | ||||
138 | |||||
139 | bool isExcluded(const Value *Value) const { | ||||
140 | if (NumExcluded == 0) | ||||
141 | return false; | ||||
142 | auto End = Excluded.begin() + NumExcluded; | ||||
143 | return std::find(Excluded.begin(), End, Value) != End; | ||||
144 | } | ||||
145 | }; | ||||
146 | |||||
147 | } // end anonymous namespace | ||||
148 | |||||
149 | // Given the provided Value and, potentially, a context instruction, return | ||||
150 | // the preferred context instruction (if any). | ||||
151 | static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) { | ||||
152 | // If we've been provided with a context instruction, then use that (provided | ||||
153 | // it has been inserted). | ||||
154 | if (CxtI && CxtI->getParent()) | ||||
155 | return CxtI; | ||||
156 | |||||
157 | // If the value is really an already-inserted instruction, then use that. | ||||
158 | CxtI = dyn_cast<Instruction>(V); | ||||
159 | if (CxtI && CxtI->getParent()) | ||||
160 | return CxtI; | ||||
161 | |||||
162 | return nullptr; | ||||
163 | } | ||||
164 | |||||
165 | static void computeKnownBits(const Value *V, KnownBits &Known, | ||||
166 | unsigned Depth, const Query &Q); | ||||
167 | |||||
168 | void llvm::computeKnownBits(const Value *V, KnownBits &Known, | ||||
169 | const DataLayout &DL, unsigned Depth, | ||||
170 | AssumptionCache *AC, const Instruction *CxtI, | ||||
171 | const DominatorTree *DT, | ||||
172 | OptimizationRemarkEmitter *ORE, bool UseInstrInfo) { | ||||
173 | ::computeKnownBits(V, Known, Depth, | ||||
174 | Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE)); | ||||
175 | } | ||||
176 | |||||
177 | static KnownBits computeKnownBits(const Value *V, unsigned Depth, | ||||
178 | const Query &Q); | ||||
179 | |||||
180 | KnownBits llvm::computeKnownBits(const Value *V, const DataLayout &DL, | ||||
181 | unsigned Depth, AssumptionCache *AC, | ||||
182 | const Instruction *CxtI, | ||||
183 | const DominatorTree *DT, | ||||
184 | OptimizationRemarkEmitter *ORE, | ||||
185 | bool UseInstrInfo) { | ||||
186 | return ::computeKnownBits( | ||||
187 | V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE)); | ||||
188 | } | ||||
189 | |||||
190 | bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS, | ||||
191 | const DataLayout &DL, AssumptionCache *AC, | ||||
192 | const Instruction *CxtI, const DominatorTree *DT, | ||||
193 | bool UseInstrInfo) { | ||||
194 | assert(LHS->getType() == RHS->getType() &&((LHS->getType() == RHS->getType() && "LHS and RHS should have the same type" ) ? static_cast<void> (0) : __assert_fail ("LHS->getType() == RHS->getType() && \"LHS and RHS should have the same type\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 195, __PRETTY_FUNCTION__)) | ||||
195 | "LHS and RHS should have the same type")((LHS->getType() == RHS->getType() && "LHS and RHS should have the same type" ) ? static_cast<void> (0) : __assert_fail ("LHS->getType() == RHS->getType() && \"LHS and RHS should have the same type\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 195, __PRETTY_FUNCTION__)); | ||||
196 | assert(LHS->getType()->isIntOrIntVectorTy() &&((LHS->getType()->isIntOrIntVectorTy() && "LHS and RHS should be integers" ) ? static_cast<void> (0) : __assert_fail ("LHS->getType()->isIntOrIntVectorTy() && \"LHS and RHS should be integers\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 197, __PRETTY_FUNCTION__)) | ||||
197 | "LHS and RHS should be integers")((LHS->getType()->isIntOrIntVectorTy() && "LHS and RHS should be integers" ) ? static_cast<void> (0) : __assert_fail ("LHS->getType()->isIntOrIntVectorTy() && \"LHS and RHS should be integers\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 197, __PRETTY_FUNCTION__)); | ||||
198 | // Look for an inverted mask: (X & ~M) op (Y & M). | ||||
199 | Value *M; | ||||
200 | if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) && | ||||
201 | match(RHS, m_c_And(m_Specific(M), m_Value()))) | ||||
202 | return true; | ||||
203 | if (match(RHS, m_c_And(m_Not(m_Value(M)), m_Value())) && | ||||
204 | match(LHS, m_c_And(m_Specific(M), m_Value()))) | ||||
205 | return true; | ||||
206 | IntegerType *IT = cast<IntegerType>(LHS->getType()->getScalarType()); | ||||
207 | KnownBits LHSKnown(IT->getBitWidth()); | ||||
208 | KnownBits RHSKnown(IT->getBitWidth()); | ||||
209 | computeKnownBits(LHS, LHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo); | ||||
210 | computeKnownBits(RHS, RHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo); | ||||
211 | return (LHSKnown.Zero | RHSKnown.Zero).isAllOnesValue(); | ||||
212 | } | ||||
213 | |||||
214 | bool llvm::isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI) { | ||||
215 | for (const User *U : CxtI->users()) { | ||||
216 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(U)) | ||||
217 | if (IC->isEquality()) | ||||
218 | if (Constant *C = dyn_cast<Constant>(IC->getOperand(1))) | ||||
219 | if (C->isNullValue()) | ||||
220 | continue; | ||||
221 | return false; | ||||
222 | } | ||||
223 | return true; | ||||
224 | } | ||||
225 | |||||
226 | static bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth, | ||||
227 | const Query &Q); | ||||
228 | |||||
229 | bool llvm::isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, | ||||
230 | bool OrZero, unsigned Depth, | ||||
231 | AssumptionCache *AC, const Instruction *CxtI, | ||||
232 | const DominatorTree *DT, bool UseInstrInfo) { | ||||
233 | return ::isKnownToBeAPowerOfTwo( | ||||
234 | V, OrZero, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo)); | ||||
235 | } | ||||
236 | |||||
237 | static bool isKnownNonZero(const Value *V, unsigned Depth, const Query &Q); | ||||
238 | |||||
239 | bool llvm::isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth, | ||||
240 | AssumptionCache *AC, const Instruction *CxtI, | ||||
241 | const DominatorTree *DT, bool UseInstrInfo) { | ||||
242 | return ::isKnownNonZero(V, Depth, | ||||
243 | Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo)); | ||||
244 | } | ||||
245 | |||||
246 | bool llvm::isKnownNonNegative(const Value *V, const DataLayout &DL, | ||||
247 | unsigned Depth, AssumptionCache *AC, | ||||
248 | const Instruction *CxtI, const DominatorTree *DT, | ||||
249 | bool UseInstrInfo) { | ||||
250 | KnownBits Known = | ||||
251 | computeKnownBits(V, DL, Depth, AC, CxtI, DT, nullptr, UseInstrInfo); | ||||
252 | return Known.isNonNegative(); | ||||
253 | } | ||||
254 | |||||
255 | bool llvm::isKnownPositive(const Value *V, const DataLayout &DL, unsigned Depth, | ||||
256 | AssumptionCache *AC, const Instruction *CxtI, | ||||
257 | const DominatorTree *DT, bool UseInstrInfo) { | ||||
258 | if (auto *CI = dyn_cast<ConstantInt>(V)) | ||||
259 | return CI->getValue().isStrictlyPositive(); | ||||
260 | |||||
261 | // TODO: We'd doing two recursive queries here. We should factor this such | ||||
262 | // that only a single query is needed. | ||||
263 | return isKnownNonNegative(V, DL, Depth, AC, CxtI, DT, UseInstrInfo) && | ||||
264 | isKnownNonZero(V, DL, Depth, AC, CxtI, DT, UseInstrInfo); | ||||
265 | } | ||||
266 | |||||
267 | bool llvm::isKnownNegative(const Value *V, const DataLayout &DL, unsigned Depth, | ||||
268 | AssumptionCache *AC, const Instruction *CxtI, | ||||
269 | const DominatorTree *DT, bool UseInstrInfo) { | ||||
270 | KnownBits Known = | ||||
271 | computeKnownBits(V, DL, Depth, AC, CxtI, DT, nullptr, UseInstrInfo); | ||||
272 | return Known.isNegative(); | ||||
273 | } | ||||
274 | |||||
275 | static bool isKnownNonEqual(const Value *V1, const Value *V2, const Query &Q); | ||||
276 | |||||
277 | bool llvm::isKnownNonEqual(const Value *V1, const Value *V2, | ||||
278 | const DataLayout &DL, AssumptionCache *AC, | ||||
279 | const Instruction *CxtI, const DominatorTree *DT, | ||||
280 | bool UseInstrInfo) { | ||||
281 | return ::isKnownNonEqual(V1, V2, | ||||
282 | Query(DL, AC, safeCxtI(V1, safeCxtI(V2, CxtI)), DT, | ||||
283 | UseInstrInfo, /*ORE=*/nullptr)); | ||||
284 | } | ||||
285 | |||||
286 | static bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth, | ||||
287 | const Query &Q); | ||||
288 | |||||
289 | bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask, | ||||
290 | const DataLayout &DL, unsigned Depth, | ||||
291 | AssumptionCache *AC, const Instruction *CxtI, | ||||
292 | const DominatorTree *DT, bool UseInstrInfo) { | ||||
293 | return ::MaskedValueIsZero( | ||||
294 | V, Mask, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo)); | ||||
295 | } | ||||
296 | |||||
297 | static unsigned ComputeNumSignBits(const Value *V, unsigned Depth, | ||||
298 | const Query &Q); | ||||
299 | |||||
300 | unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL, | ||||
301 | unsigned Depth, AssumptionCache *AC, | ||||
302 | const Instruction *CxtI, | ||||
303 | const DominatorTree *DT, bool UseInstrInfo) { | ||||
304 | return ::ComputeNumSignBits( | ||||
305 | V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo)); | ||||
306 | } | ||||
307 | |||||
308 | static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, | ||||
309 | bool NSW, | ||||
310 | KnownBits &KnownOut, KnownBits &Known2, | ||||
311 | unsigned Depth, const Query &Q) { | ||||
312 | unsigned BitWidth = KnownOut.getBitWidth(); | ||||
313 | |||||
314 | // If an initial sequence of bits in the result is not needed, the | ||||
315 | // corresponding bits in the operands are not needed. | ||||
316 | KnownBits LHSKnown(BitWidth); | ||||
317 | computeKnownBits(Op0, LHSKnown, Depth + 1, Q); | ||||
318 | computeKnownBits(Op1, Known2, Depth + 1, Q); | ||||
319 | |||||
320 | KnownOut = KnownBits::computeForAddSub(Add, NSW, LHSKnown, Known2); | ||||
321 | } | ||||
322 | |||||
323 | static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, | ||||
324 | KnownBits &Known, KnownBits &Known2, | ||||
325 | unsigned Depth, const Query &Q) { | ||||
326 | unsigned BitWidth = Known.getBitWidth(); | ||||
327 | computeKnownBits(Op1, Known, Depth + 1, Q); | ||||
328 | computeKnownBits(Op0, Known2, Depth + 1, Q); | ||||
329 | |||||
330 | bool isKnownNegative = false; | ||||
331 | bool isKnownNonNegative = false; | ||||
332 | // If the multiplication is known not to overflow, compute the sign bit. | ||||
333 | if (NSW) { | ||||
334 | if (Op0 == Op1) { | ||||
335 | // The product of a number with itself is non-negative. | ||||
336 | isKnownNonNegative = true; | ||||
337 | } else { | ||||
338 | bool isKnownNonNegativeOp1 = Known.isNonNegative(); | ||||
339 | bool isKnownNonNegativeOp0 = Known2.isNonNegative(); | ||||
340 | bool isKnownNegativeOp1 = Known.isNegative(); | ||||
341 | bool isKnownNegativeOp0 = Known2.isNegative(); | ||||
342 | // The product of two numbers with the same sign is non-negative. | ||||
343 | isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) || | ||||
344 | (isKnownNonNegativeOp1 && isKnownNonNegativeOp0); | ||||
345 | // The product of a negative number and a non-negative number is either | ||||
346 | // negative or zero. | ||||
347 | if (!isKnownNonNegative) | ||||
348 | isKnownNegative = (isKnownNegativeOp1 && isKnownNonNegativeOp0 && | ||||
349 | isKnownNonZero(Op0, Depth, Q)) || | ||||
350 | (isKnownNegativeOp0 && isKnownNonNegativeOp1 && | ||||
351 | isKnownNonZero(Op1, Depth, Q)); | ||||
352 | } | ||||
353 | } | ||||
354 | |||||
355 | assert(!Known.hasConflict() && !Known2.hasConflict())((!Known.hasConflict() && !Known2.hasConflict()) ? static_cast <void> (0) : __assert_fail ("!Known.hasConflict() && !Known2.hasConflict()" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 355, __PRETTY_FUNCTION__)); | ||||
356 | // Compute a conservative estimate for high known-0 bits. | ||||
357 | unsigned LeadZ = std::max(Known.countMinLeadingZeros() + | ||||
358 | Known2.countMinLeadingZeros(), | ||||
359 | BitWidth) - BitWidth; | ||||
360 | LeadZ = std::min(LeadZ, BitWidth); | ||||
361 | |||||
362 | // The result of the bottom bits of an integer multiply can be | ||||
363 | // inferred by looking at the bottom bits of both operands and | ||||
364 | // multiplying them together. | ||||
365 | // We can infer at least the minimum number of known trailing bits | ||||
366 | // of both operands. Depending on number of trailing zeros, we can | ||||
367 | // infer more bits, because (a*b) <=> ((a/m) * (b/n)) * (m*n) assuming | ||||
368 | // a and b are divisible by m and n respectively. | ||||
369 | // We then calculate how many of those bits are inferrable and set | ||||
370 | // the output. For example, the i8 mul: | ||||
371 | // a = XXXX1100 (12) | ||||
372 | // b = XXXX1110 (14) | ||||
373 | // We know the bottom 3 bits are zero since the first can be divided by | ||||
374 | // 4 and the second by 2, thus having ((12/4) * (14/2)) * (2*4). | ||||
375 | // Applying the multiplication to the trimmed arguments gets: | ||||
376 | // XX11 (3) | ||||
377 | // X111 (7) | ||||
378 | // ------- | ||||
379 | // XX11 | ||||
380 | // XX11 | ||||
381 | // XX11 | ||||
382 | // XX11 | ||||
383 | // ------- | ||||
384 | // XXXXX01 | ||||
385 | // Which allows us to infer the 2 LSBs. Since we're multiplying the result | ||||
386 | // by 8, the bottom 3 bits will be 0, so we can infer a total of 5 bits. | ||||
387 | // The proof for this can be described as: | ||||
388 | // Pre: (C1 >= 0) && (C1 < (1 << C5)) && (C2 >= 0) && (C2 < (1 << C6)) && | ||||
389 | // (C7 == (1 << (umin(countTrailingZeros(C1), C5) + | ||||
390 | // umin(countTrailingZeros(C2), C6) + | ||||
391 | // umin(C5 - umin(countTrailingZeros(C1), C5), | ||||
392 | // C6 - umin(countTrailingZeros(C2), C6)))) - 1) | ||||
393 | // %aa = shl i8 %a, C5 | ||||
394 | // %bb = shl i8 %b, C6 | ||||
395 | // %aaa = or i8 %aa, C1 | ||||
396 | // %bbb = or i8 %bb, C2 | ||||
397 | // %mul = mul i8 %aaa, %bbb | ||||
398 | // %mask = and i8 %mul, C7 | ||||
399 | // => | ||||
400 | // %mask = i8 ((C1*C2)&C7) | ||||
401 | // Where C5, C6 describe the known bits of %a, %b | ||||
402 | // C1, C2 describe the known bottom bits of %a, %b. | ||||
403 | // C7 describes the mask of the known bits of the result. | ||||
404 | APInt Bottom0 = Known.One; | ||||
405 | APInt Bottom1 = Known2.One; | ||||
406 | |||||
407 | // How many times we'd be able to divide each argument by 2 (shr by 1). | ||||
408 | // This gives us the number of trailing zeros on the multiplication result. | ||||
409 | unsigned TrailBitsKnown0 = (Known.Zero | Known.One).countTrailingOnes(); | ||||
410 | unsigned TrailBitsKnown1 = (Known2.Zero | Known2.One).countTrailingOnes(); | ||||
411 | unsigned TrailZero0 = Known.countMinTrailingZeros(); | ||||
412 | unsigned TrailZero1 = Known2.countMinTrailingZeros(); | ||||
413 | unsigned TrailZ = TrailZero0 + TrailZero1; | ||||
414 | |||||
415 | // Figure out the fewest known-bits operand. | ||||
416 | unsigned SmallestOperand = std::min(TrailBitsKnown0 - TrailZero0, | ||||
417 | TrailBitsKnown1 - TrailZero1); | ||||
418 | unsigned ResultBitsKnown = std::min(SmallestOperand + TrailZ, BitWidth); | ||||
419 | |||||
420 | APInt BottomKnown = Bottom0.getLoBits(TrailBitsKnown0) * | ||||
421 | Bottom1.getLoBits(TrailBitsKnown1); | ||||
422 | |||||
423 | Known.resetAll(); | ||||
424 | Known.Zero.setHighBits(LeadZ); | ||||
425 | Known.Zero |= (~BottomKnown).getLoBits(ResultBitsKnown); | ||||
426 | Known.One |= BottomKnown.getLoBits(ResultBitsKnown); | ||||
427 | |||||
428 | // Only make use of no-wrap flags if we failed to compute the sign bit | ||||
429 | // directly. This matters if the multiplication always overflows, in | ||||
430 | // which case we prefer to follow the result of the direct computation, | ||||
431 | // though as the program is invoking undefined behaviour we can choose | ||||
432 | // whatever we like here. | ||||
433 | if (isKnownNonNegative && !Known.isNegative()) | ||||
434 | Known.makeNonNegative(); | ||||
435 | else if (isKnownNegative && !Known.isNonNegative()) | ||||
436 | Known.makeNegative(); | ||||
437 | } | ||||
438 | |||||
439 | void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges, | ||||
440 | KnownBits &Known) { | ||||
441 | unsigned BitWidth = Known.getBitWidth(); | ||||
442 | unsigned NumRanges = Ranges.getNumOperands() / 2; | ||||
443 | assert(NumRanges >= 1)((NumRanges >= 1) ? static_cast<void> (0) : __assert_fail ("NumRanges >= 1", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 443, __PRETTY_FUNCTION__)); | ||||
444 | |||||
445 | Known.Zero.setAllBits(); | ||||
446 | Known.One.setAllBits(); | ||||
447 | |||||
448 | for (unsigned i = 0; i < NumRanges; ++i) { | ||||
449 | ConstantInt *Lower = | ||||
450 | mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0)); | ||||
451 | ConstantInt *Upper = | ||||
452 | mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1)); | ||||
453 | ConstantRange Range(Lower->getValue(), Upper->getValue()); | ||||
454 | |||||
455 | // The first CommonPrefixBits of all values in Range are equal. | ||||
456 | unsigned CommonPrefixBits = | ||||
457 | (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countLeadingZeros(); | ||||
458 | |||||
459 | APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits); | ||||
460 | Known.One &= Range.getUnsignedMax() & Mask; | ||||
461 | Known.Zero &= ~Range.getUnsignedMax() & Mask; | ||||
462 | } | ||||
463 | } | ||||
464 | |||||
465 | static bool isEphemeralValueOf(const Instruction *I, const Value *E) { | ||||
466 | SmallVector<const Value *, 16> WorkSet(1, I); | ||||
467 | SmallPtrSet<const Value *, 32> Visited; | ||||
468 | SmallPtrSet<const Value *, 16> EphValues; | ||||
469 | |||||
470 | // The instruction defining an assumption's condition itself is always | ||||
471 | // considered ephemeral to that assumption (even if it has other | ||||
472 | // non-ephemeral users). See r246696's test case for an example. | ||||
473 | if (is_contained(I->operands(), E)) | ||||
474 | return true; | ||||
475 | |||||
476 | while (!WorkSet.empty()) { | ||||
477 | const Value *V = WorkSet.pop_back_val(); | ||||
478 | if (!Visited.insert(V).second) | ||||
479 | continue; | ||||
480 | |||||
481 | // If all uses of this value are ephemeral, then so is this value. | ||||
482 | if (llvm::all_of(V->users(), [&](const User *U) { | ||||
483 | return EphValues.count(U); | ||||
484 | })) { | ||||
485 | if (V == E) | ||||
486 | return true; | ||||
487 | |||||
488 | if (V == I || isSafeToSpeculativelyExecute(V)) { | ||||
489 | EphValues.insert(V); | ||||
490 | if (const User *U = dyn_cast<User>(V)) | ||||
491 | for (User::const_op_iterator J = U->op_begin(), JE = U->op_end(); | ||||
492 | J != JE; ++J) | ||||
493 | WorkSet.push_back(*J); | ||||
494 | } | ||||
495 | } | ||||
496 | } | ||||
497 | |||||
498 | return false; | ||||
499 | } | ||||
500 | |||||
501 | // Is this an intrinsic that cannot be speculated but also cannot trap? | ||||
502 | bool llvm::isAssumeLikeIntrinsic(const Instruction *I) { | ||||
503 | if (const CallInst *CI = dyn_cast<CallInst>(I)) | ||||
504 | if (Function *F = CI->getCalledFunction()) | ||||
505 | switch (F->getIntrinsicID()) { | ||||
506 | default: break; | ||||
507 | // FIXME: This list is repeated from NoTTI::getIntrinsicCost. | ||||
508 | case Intrinsic::assume: | ||||
509 | case Intrinsic::sideeffect: | ||||
510 | case Intrinsic::dbg_declare: | ||||
511 | case Intrinsic::dbg_value: | ||||
512 | case Intrinsic::dbg_label: | ||||
513 | case Intrinsic::invariant_start: | ||||
514 | case Intrinsic::invariant_end: | ||||
515 | case Intrinsic::lifetime_start: | ||||
516 | case Intrinsic::lifetime_end: | ||||
517 | case Intrinsic::objectsize: | ||||
518 | case Intrinsic::ptr_annotation: | ||||
519 | case Intrinsic::var_annotation: | ||||
520 | return true; | ||||
521 | } | ||||
522 | |||||
523 | return false; | ||||
524 | } | ||||
525 | |||||
526 | bool llvm::isValidAssumeForContext(const Instruction *Inv, | ||||
527 | const Instruction *CxtI, | ||||
528 | const DominatorTree *DT) { | ||||
529 | // There are two restrictions on the use of an assume: | ||||
530 | // 1. The assume must dominate the context (or the control flow must | ||||
531 | // reach the assume whenever it reaches the context). | ||||
532 | // 2. The context must not be in the assume's set of ephemeral values | ||||
533 | // (otherwise we will use the assume to prove that the condition | ||||
534 | // feeding the assume is trivially true, thus causing the removal of | ||||
535 | // the assume). | ||||
536 | |||||
537 | if (DT) { | ||||
538 | if (DT->dominates(Inv, CxtI)) | ||||
539 | return true; | ||||
540 | } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor()) { | ||||
541 | // We don't have a DT, but this trivially dominates. | ||||
542 | return true; | ||||
543 | } | ||||
544 | |||||
545 | // With or without a DT, the only remaining case we will check is if the | ||||
546 | // instructions are in the same BB. Give up if that is not the case. | ||||
547 | if (Inv->getParent() != CxtI->getParent()) | ||||
548 | return false; | ||||
549 | |||||
550 | // If we have a dom tree, then we now know that the assume doesn't dominate | ||||
551 | // the other instruction. If we don't have a dom tree then we can check if | ||||
552 | // the assume is first in the BB. | ||||
553 | if (!DT) { | ||||
554 | // Search forward from the assume until we reach the context (or the end | ||||
555 | // of the block); the common case is that the assume will come first. | ||||
556 | for (auto I = std::next(BasicBlock::const_iterator(Inv)), | ||||
557 | IE = Inv->getParent()->end(); I != IE; ++I) | ||||
558 | if (&*I == CxtI) | ||||
559 | return true; | ||||
560 | } | ||||
561 | |||||
562 | // The context comes first, but they're both in the same block. Make sure | ||||
563 | // there is nothing in between that might interrupt the control flow. | ||||
564 | for (BasicBlock::const_iterator I = | ||||
565 | std::next(BasicBlock::const_iterator(CxtI)), IE(Inv); | ||||
566 | I != IE; ++I) | ||||
567 | if (!isSafeToSpeculativelyExecute(&*I) && !isAssumeLikeIntrinsic(&*I)) | ||||
568 | return false; | ||||
569 | |||||
570 | return !isEphemeralValueOf(Inv, CxtI); | ||||
571 | } | ||||
572 | |||||
573 | static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known, | ||||
574 | unsigned Depth, const Query &Q) { | ||||
575 | // Use of assumptions is context-sensitive. If we don't have a context, we | ||||
576 | // cannot use them! | ||||
577 | if (!Q.AC || !Q.CxtI) | ||||
578 | return; | ||||
579 | |||||
580 | unsigned BitWidth = Known.getBitWidth(); | ||||
581 | |||||
582 | // Note that the patterns below need to be kept in sync with the code | ||||
583 | // in AssumptionCache::updateAffectedValues. | ||||
584 | |||||
585 | for (auto &AssumeVH : Q.AC->assumptionsFor(V)) { | ||||
586 | if (!AssumeVH) | ||||
587 | continue; | ||||
588 | CallInst *I = cast<CallInst>(AssumeVH); | ||||
589 | assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&((I->getParent()->getParent() == Q.CxtI->getParent() ->getParent() && "Got assumption for the wrong function!" ) ? static_cast<void> (0) : __assert_fail ("I->getParent()->getParent() == Q.CxtI->getParent()->getParent() && \"Got assumption for the wrong function!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 590, __PRETTY_FUNCTION__)) | ||||
590 | "Got assumption for the wrong function!")((I->getParent()->getParent() == Q.CxtI->getParent() ->getParent() && "Got assumption for the wrong function!" ) ? static_cast<void> (0) : __assert_fail ("I->getParent()->getParent() == Q.CxtI->getParent()->getParent() && \"Got assumption for the wrong function!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 590, __PRETTY_FUNCTION__)); | ||||
591 | if (Q.isExcluded(I)) | ||||
592 | continue; | ||||
593 | |||||
594 | // Warning: This loop can end up being somewhat performance sensitive. | ||||
595 | // We're running this loop for once for each value queried resulting in a | ||||
596 | // runtime of ~O(#assumes * #values). | ||||
597 | |||||
598 | assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&((I->getCalledFunction()->getIntrinsicID() == Intrinsic ::assume && "must be an assume intrinsic") ? static_cast <void> (0) : __assert_fail ("I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume && \"must be an assume intrinsic\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 599, __PRETTY_FUNCTION__)) | ||||
599 | "must be an assume intrinsic")((I->getCalledFunction()->getIntrinsicID() == Intrinsic ::assume && "must be an assume intrinsic") ? static_cast <void> (0) : __assert_fail ("I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume && \"must be an assume intrinsic\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 599, __PRETTY_FUNCTION__)); | ||||
600 | |||||
601 | Value *Arg = I->getArgOperand(0); | ||||
602 | |||||
603 | if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||
604 | assert(BitWidth == 1 && "assume operand is not i1?")((BitWidth == 1 && "assume operand is not i1?") ? static_cast <void> (0) : __assert_fail ("BitWidth == 1 && \"assume operand is not i1?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 604, __PRETTY_FUNCTION__)); | ||||
605 | Known.setAllOnes(); | ||||
606 | return; | ||||
607 | } | ||||
608 | if (match(Arg, m_Not(m_Specific(V))) && | ||||
609 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||
610 | assert(BitWidth == 1 && "assume operand is not i1?")((BitWidth == 1 && "assume operand is not i1?") ? static_cast <void> (0) : __assert_fail ("BitWidth == 1 && \"assume operand is not i1?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 610, __PRETTY_FUNCTION__)); | ||||
611 | Known.setAllZero(); | ||||
612 | return; | ||||
613 | } | ||||
614 | |||||
615 | // The remaining tests are all recursive, so bail out if we hit the limit. | ||||
616 | if (Depth == MaxDepth) | ||||
617 | continue; | ||||
618 | |||||
619 | ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg); | ||||
620 | if (!Cmp) | ||||
621 | continue; | ||||
622 | |||||
623 | Value *A, *B; | ||||
624 | auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V))); | ||||
625 | |||||
626 | CmpInst::Predicate Pred; | ||||
627 | uint64_t C; | ||||
628 | switch (Cmp->getPredicate()) { | ||||
629 | default: | ||||
630 | break; | ||||
631 | case ICmpInst::ICMP_EQ: | ||||
632 | // assume(v = a) | ||||
633 | if (match(Cmp, m_c_ICmp(Pred, m_V, m_Value(A))) && | ||||
634 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||
635 | KnownBits RHSKnown(BitWidth); | ||||
636 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); | ||||
637 | Known.Zero |= RHSKnown.Zero; | ||||
638 | Known.One |= RHSKnown.One; | ||||
639 | // assume(v & b = a) | ||||
640 | } else if (match(Cmp, | ||||
641 | m_c_ICmp(Pred, m_c_And(m_V, m_Value(B)), m_Value(A))) && | ||||
642 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||
643 | KnownBits RHSKnown(BitWidth); | ||||
644 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); | ||||
645 | KnownBits MaskKnown(BitWidth); | ||||
646 | computeKnownBits(B, MaskKnown, Depth+1, Query(Q, I)); | ||||
647 | |||||
648 | // For those bits in the mask that are known to be one, we can propagate | ||||
649 | // known bits from the RHS to V. | ||||
650 | Known.Zero |= RHSKnown.Zero & MaskKnown.One; | ||||
651 | Known.One |= RHSKnown.One & MaskKnown.One; | ||||
652 | // assume(~(v & b) = a) | ||||
653 | } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_And(m_V, m_Value(B))), | ||||
654 | m_Value(A))) && | ||||
655 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||
656 | KnownBits RHSKnown(BitWidth); | ||||
657 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); | ||||
658 | KnownBits MaskKnown(BitWidth); | ||||
659 | computeKnownBits(B, MaskKnown, Depth+1, Query(Q, I)); | ||||
660 | |||||
661 | // For those bits in the mask that are known to be one, we can propagate | ||||
662 | // inverted known bits from the RHS to V. | ||||
663 | Known.Zero |= RHSKnown.One & MaskKnown.One; | ||||
664 | Known.One |= RHSKnown.Zero & MaskKnown.One; | ||||
665 | // assume(v | b = a) | ||||
666 | } else if (match(Cmp, | ||||
667 | m_c_ICmp(Pred, m_c_Or(m_V, m_Value(B)), m_Value(A))) && | ||||
668 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||
669 | KnownBits RHSKnown(BitWidth); | ||||
670 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); | ||||
671 | KnownBits BKnown(BitWidth); | ||||
672 | computeKnownBits(B, BKnown, Depth+1, Query(Q, I)); | ||||
673 | |||||
674 | // For those bits in B that are known to be zero, we can propagate known | ||||
675 | // bits from the RHS to V. | ||||
676 | Known.Zero |= RHSKnown.Zero & BKnown.Zero; | ||||
677 | Known.One |= RHSKnown.One & BKnown.Zero; | ||||
678 | // assume(~(v | b) = a) | ||||
679 | } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_Or(m_V, m_Value(B))), | ||||
680 | m_Value(A))) && | ||||
681 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||
682 | KnownBits RHSKnown(BitWidth); | ||||
683 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); | ||||
684 | KnownBits BKnown(BitWidth); | ||||
685 | computeKnownBits(B, BKnown, Depth+1, Query(Q, I)); | ||||
686 | |||||
687 | // For those bits in B that are known to be zero, we can propagate | ||||
688 | // inverted known bits from the RHS to V. | ||||
689 | Known.Zero |= RHSKnown.One & BKnown.Zero; | ||||
690 | Known.One |= RHSKnown.Zero & BKnown.Zero; | ||||
691 | // assume(v ^ b = a) | ||||
692 | } else if (match(Cmp, | ||||
693 | m_c_ICmp(Pred, m_c_Xor(m_V, m_Value(B)), m_Value(A))) && | ||||
694 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||
695 | KnownBits RHSKnown(BitWidth); | ||||
696 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); | ||||
697 | KnownBits BKnown(BitWidth); | ||||
698 | computeKnownBits(B, BKnown, Depth+1, Query(Q, I)); | ||||
699 | |||||
700 | // For those bits in B that are known to be zero, we can propagate known | ||||
701 | // bits from the RHS to V. For those bits in B that are known to be one, | ||||
702 | // we can propagate inverted known bits from the RHS to V. | ||||
703 | Known.Zero |= RHSKnown.Zero & BKnown.Zero; | ||||
704 | Known.One |= RHSKnown.One & BKnown.Zero; | ||||
705 | Known.Zero |= RHSKnown.One & BKnown.One; | ||||
706 | Known.One |= RHSKnown.Zero & BKnown.One; | ||||
707 | // assume(~(v ^ b) = a) | ||||
708 | } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_Xor(m_V, m_Value(B))), | ||||
709 | m_Value(A))) && | ||||
710 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||
711 | KnownBits RHSKnown(BitWidth); | ||||
712 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); | ||||
713 | KnownBits BKnown(BitWidth); | ||||
714 | computeKnownBits(B, BKnown, Depth+1, Query(Q, I)); | ||||
715 | |||||
716 | // For those bits in B that are known to be zero, we can propagate | ||||
717 | // inverted known bits from the RHS to V. For those bits in B that are | ||||
718 | // known to be one, we can propagate known bits from the RHS to V. | ||||
719 | Known.Zero |= RHSKnown.One & BKnown.Zero; | ||||
720 | Known.One |= RHSKnown.Zero & BKnown.Zero; | ||||
721 | Known.Zero |= RHSKnown.Zero & BKnown.One; | ||||
722 | Known.One |= RHSKnown.One & BKnown.One; | ||||
723 | // assume(v << c = a) | ||||
724 | } else if (match(Cmp, m_c_ICmp(Pred, m_Shl(m_V, m_ConstantInt(C)), | ||||
725 | m_Value(A))) && | ||||
726 | isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) { | ||||
727 | KnownBits RHSKnown(BitWidth); | ||||
728 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); | ||||
729 | // For those bits in RHS that are known, we can propagate them to known | ||||
730 | // bits in V shifted to the right by C. | ||||
731 | RHSKnown.Zero.lshrInPlace(C); | ||||
732 | Known.Zero |= RHSKnown.Zero; | ||||
733 | RHSKnown.One.lshrInPlace(C); | ||||
734 | Known.One |= RHSKnown.One; | ||||
735 | // assume(~(v << c) = a) | ||||
736 | } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_Shl(m_V, m_ConstantInt(C))), | ||||
737 | m_Value(A))) && | ||||
738 | isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) { | ||||
739 | KnownBits RHSKnown(BitWidth); | ||||
740 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); | ||||
741 | // For those bits in RHS that are known, we can propagate them inverted | ||||
742 | // to known bits in V shifted to the right by C. | ||||
743 | RHSKnown.One.lshrInPlace(C); | ||||
744 | Known.Zero |= RHSKnown.One; | ||||
745 | RHSKnown.Zero.lshrInPlace(C); | ||||
746 | Known.One |= RHSKnown.Zero; | ||||
747 | // assume(v >> c = a) | ||||
748 | } else if (match(Cmp, m_c_ICmp(Pred, m_Shr(m_V, m_ConstantInt(C)), | ||||
749 | m_Value(A))) && | ||||
750 | isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) { | ||||
751 | KnownBits RHSKnown(BitWidth); | ||||
752 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); | ||||
753 | // For those bits in RHS that are known, we can propagate them to known | ||||
754 | // bits in V shifted to the right by C. | ||||
755 | Known.Zero |= RHSKnown.Zero << C; | ||||
756 | Known.One |= RHSKnown.One << C; | ||||
757 | // assume(~(v >> c) = a) | ||||
758 | } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_Shr(m_V, m_ConstantInt(C))), | ||||
759 | m_Value(A))) && | ||||
760 | isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) { | ||||
761 | KnownBits RHSKnown(BitWidth); | ||||
762 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); | ||||
763 | // For those bits in RHS that are known, we can propagate them inverted | ||||
764 | // to known bits in V shifted to the right by C. | ||||
765 | Known.Zero |= RHSKnown.One << C; | ||||
766 | Known.One |= RHSKnown.Zero << C; | ||||
767 | } | ||||
768 | break; | ||||
769 | case ICmpInst::ICMP_SGE: | ||||
770 | // assume(v >=_s c) where c is non-negative | ||||
771 | if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) && | ||||
772 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||
773 | KnownBits RHSKnown(BitWidth); | ||||
774 | computeKnownBits(A, RHSKnown, Depth + 1, Query(Q, I)); | ||||
775 | |||||
776 | if (RHSKnown.isNonNegative()) { | ||||
777 | // We know that the sign bit is zero. | ||||
778 | Known.makeNonNegative(); | ||||
779 | } | ||||
780 | } | ||||
781 | break; | ||||
782 | case ICmpInst::ICMP_SGT: | ||||
783 | // assume(v >_s c) where c is at least -1. | ||||
784 | if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) && | ||||
785 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||
786 | KnownBits RHSKnown(BitWidth); | ||||
787 | computeKnownBits(A, RHSKnown, Depth + 1, Query(Q, I)); | ||||
788 | |||||
789 | if (RHSKnown.isAllOnes() || RHSKnown.isNonNegative()) { | ||||
790 | // We know that the sign bit is zero. | ||||
791 | Known.makeNonNegative(); | ||||
792 | } | ||||
793 | } | ||||
794 | break; | ||||
795 | case ICmpInst::ICMP_SLE: | ||||
796 | // assume(v <=_s c) where c is negative | ||||
797 | if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) && | ||||
798 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||
799 | KnownBits RHSKnown(BitWidth); | ||||
800 | computeKnownBits(A, RHSKnown, Depth + 1, Query(Q, I)); | ||||
801 | |||||
802 | if (RHSKnown.isNegative()) { | ||||
803 | // We know that the sign bit is one. | ||||
804 | Known.makeNegative(); | ||||
805 | } | ||||
806 | } | ||||
807 | break; | ||||
808 | case ICmpInst::ICMP_SLT: | ||||
809 | // assume(v <_s c) where c is non-positive | ||||
810 | if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) && | ||||
811 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||
812 | KnownBits RHSKnown(BitWidth); | ||||
813 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); | ||||
814 | |||||
815 | if (RHSKnown.isZero() || RHSKnown.isNegative()) { | ||||
816 | // We know that the sign bit is one. | ||||
817 | Known.makeNegative(); | ||||
818 | } | ||||
819 | } | ||||
820 | break; | ||||
821 | case ICmpInst::ICMP_ULE: | ||||
822 | // assume(v <=_u c) | ||||
823 | if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) && | ||||
824 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||
825 | KnownBits RHSKnown(BitWidth); | ||||
826 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); | ||||
827 | |||||
828 | // Whatever high bits in c are zero are known to be zero. | ||||
829 | Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros()); | ||||
830 | } | ||||
831 | break; | ||||
832 | case ICmpInst::ICMP_ULT: | ||||
833 | // assume(v <_u c) | ||||
834 | if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) && | ||||
835 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||
836 | KnownBits RHSKnown(BitWidth); | ||||
837 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); | ||||
838 | |||||
839 | // If the RHS is known zero, then this assumption must be wrong (nothing | ||||
840 | // is unsigned less than zero). Signal a conflict and get out of here. | ||||
841 | if (RHSKnown.isZero()) { | ||||
842 | Known.Zero.setAllBits(); | ||||
843 | Known.One.setAllBits(); | ||||
844 | break; | ||||
845 | } | ||||
846 | |||||
847 | // Whatever high bits in c are zero are known to be zero (if c is a power | ||||
848 | // of 2, then one more). | ||||
849 | if (isKnownToBeAPowerOfTwo(A, false, Depth + 1, Query(Q, I))) | ||||
850 | Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros() + 1); | ||||
851 | else | ||||
852 | Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros()); | ||||
853 | } | ||||
854 | break; | ||||
855 | } | ||||
856 | } | ||||
857 | |||||
858 | // If assumptions conflict with each other or previous known bits, then we | ||||
859 | // have a logical fallacy. It's possible that the assumption is not reachable, | ||||
860 | // so this isn't a real bug. On the other hand, the program may have undefined | ||||
861 | // behavior, or we might have a bug in the compiler. We can't assert/crash, so | ||||
862 | // clear out the known bits, try to warn the user, and hope for the best. | ||||
863 | if (Known.Zero.intersects(Known.One)) { | ||||
864 | Known.resetAll(); | ||||
865 | |||||
866 | if (Q.ORE) | ||||
867 | Q.ORE->emit([&]() { | ||||
868 | auto *CxtI = const_cast<Instruction *>(Q.CxtI); | ||||
869 | return OptimizationRemarkAnalysis("value-tracking", "BadAssumption", | ||||
870 | CxtI) | ||||
871 | << "Detected conflicting code assumptions. Program may " | ||||
872 | "have undefined behavior, or compiler may have " | ||||
873 | "internal error."; | ||||
874 | }); | ||||
875 | } | ||||
876 | } | ||||
877 | |||||
878 | /// Compute known bits from a shift operator, including those with a | ||||
879 | /// non-constant shift amount. Known is the output of this function. Known2 is a | ||||
880 | /// pre-allocated temporary with the same bit width as Known. KZF and KOF are | ||||
881 | /// operator-specific functions that, given the known-zero or known-one bits | ||||
882 | /// respectively, and a shift amount, compute the implied known-zero or | ||||
883 | /// known-one bits of the shift operator's result respectively for that shift | ||||
884 | /// amount. The results from calling KZF and KOF are conservatively combined for | ||||
885 | /// all permitted shift amounts. | ||||
886 | static void computeKnownBitsFromShiftOperator( | ||||
887 | const Operator *I, KnownBits &Known, KnownBits &Known2, | ||||
888 | unsigned Depth, const Query &Q, | ||||
889 | function_ref<APInt(const APInt &, unsigned)> KZF, | ||||
890 | function_ref<APInt(const APInt &, unsigned)> KOF) { | ||||
891 | unsigned BitWidth = Known.getBitWidth(); | ||||
892 | |||||
893 | if (auto *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
894 | unsigned ShiftAmt = SA->getLimitedValue(BitWidth-1); | ||||
895 | |||||
896 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||
897 | Known.Zero = KZF(Known.Zero, ShiftAmt); | ||||
898 | Known.One = KOF(Known.One, ShiftAmt); | ||||
899 | // If the known bits conflict, this must be an overflowing left shift, so | ||||
900 | // the shift result is poison. We can return anything we want. Choose 0 for | ||||
901 | // the best folding opportunity. | ||||
902 | if (Known.hasConflict()) | ||||
903 | Known.setAllZero(); | ||||
904 | |||||
905 | return; | ||||
906 | } | ||||
907 | |||||
908 | computeKnownBits(I->getOperand(1), Known, Depth + 1, Q); | ||||
909 | |||||
910 | // If the shift amount could be greater than or equal to the bit-width of the | ||||
911 | // LHS, the value could be poison, but bail out because the check below is | ||||
912 | // expensive. TODO: Should we just carry on? | ||||
913 | if ((~Known.Zero).uge(BitWidth)) { | ||||
914 | Known.resetAll(); | ||||
915 | return; | ||||
916 | } | ||||
917 | |||||
918 | // Note: We cannot use Known.Zero.getLimitedValue() here, because if | ||||
919 | // BitWidth > 64 and any upper bits are known, we'll end up returning the | ||||
920 | // limit value (which implies all bits are known). | ||||
921 | uint64_t ShiftAmtKZ = Known.Zero.zextOrTrunc(64).getZExtValue(); | ||||
922 | uint64_t ShiftAmtKO = Known.One.zextOrTrunc(64).getZExtValue(); | ||||
923 | |||||
924 | // It would be more-clearly correct to use the two temporaries for this | ||||
925 | // calculation. Reusing the APInts here to prevent unnecessary allocations. | ||||
926 | Known.resetAll(); | ||||
927 | |||||
928 | // If we know the shifter operand is nonzero, we can sometimes infer more | ||||
929 | // known bits. However this is expensive to compute, so be lazy about it and | ||||
930 | // only compute it when absolutely necessary. | ||||
931 | Optional<bool> ShifterOperandIsNonZero; | ||||
932 | |||||
933 | // Early exit if we can't constrain any well-defined shift amount. | ||||
934 | if (!(ShiftAmtKZ & (PowerOf2Ceil(BitWidth) - 1)) && | ||||
935 | !(ShiftAmtKO & (PowerOf2Ceil(BitWidth) - 1))) { | ||||
936 | ShifterOperandIsNonZero = isKnownNonZero(I->getOperand(1), Depth + 1, Q); | ||||
937 | if (!*ShifterOperandIsNonZero) | ||||
938 | return; | ||||
939 | } | ||||
940 | |||||
941 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); | ||||
942 | |||||
943 | Known.Zero.setAllBits(); | ||||
944 | Known.One.setAllBits(); | ||||
945 | for (unsigned ShiftAmt = 0; ShiftAmt < BitWidth; ++ShiftAmt) { | ||||
946 | // Combine the shifted known input bits only for those shift amounts | ||||
947 | // compatible with its known constraints. | ||||
948 | if ((ShiftAmt & ~ShiftAmtKZ) != ShiftAmt) | ||||
949 | continue; | ||||
950 | if ((ShiftAmt | ShiftAmtKO) != ShiftAmt) | ||||
951 | continue; | ||||
952 | // If we know the shifter is nonzero, we may be able to infer more known | ||||
953 | // bits. This check is sunk down as far as possible to avoid the expensive | ||||
954 | // call to isKnownNonZero if the cheaper checks above fail. | ||||
955 | if (ShiftAmt == 0) { | ||||
956 | if (!ShifterOperandIsNonZero.hasValue()) | ||||
957 | ShifterOperandIsNonZero = | ||||
958 | isKnownNonZero(I->getOperand(1), Depth + 1, Q); | ||||
959 | if (*ShifterOperandIsNonZero) | ||||
960 | continue; | ||||
961 | } | ||||
962 | |||||
963 | Known.Zero &= KZF(Known2.Zero, ShiftAmt); | ||||
964 | Known.One &= KOF(Known2.One, ShiftAmt); | ||||
965 | } | ||||
966 | |||||
967 | // If the known bits conflict, the result is poison. Return a 0 and hope the | ||||
968 | // caller can further optimize that. | ||||
969 | if (Known.hasConflict()) | ||||
970 | Known.setAllZero(); | ||||
971 | } | ||||
972 | |||||
973 | static void computeKnownBitsFromOperator(const Operator *I, KnownBits &Known, | ||||
974 | unsigned Depth, const Query &Q) { | ||||
975 | unsigned BitWidth = Known.getBitWidth(); | ||||
976 | |||||
977 | KnownBits Known2(Known); | ||||
978 | switch (I->getOpcode()) { | ||||
979 | default: break; | ||||
980 | case Instruction::Load: | ||||
981 | if (MDNode *MD = | ||||
982 | Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range)) | ||||
983 | computeKnownBitsFromRangeMetadata(*MD, Known); | ||||
984 | break; | ||||
985 | case Instruction::And: { | ||||
986 | // If either the LHS or the RHS are Zero, the result is zero. | ||||
987 | computeKnownBits(I->getOperand(1), Known, Depth + 1, Q); | ||||
988 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); | ||||
989 | |||||
990 | // Output known-1 bits are only known if set in both the LHS & RHS. | ||||
991 | Known.One &= Known2.One; | ||||
992 | // Output known-0 are known to be clear if zero in either the LHS | RHS. | ||||
993 | Known.Zero |= Known2.Zero; | ||||
994 | |||||
995 | // and(x, add (x, -1)) is a common idiom that always clears the low bit; | ||||
996 | // here we handle the more general case of adding any odd number by | ||||
997 | // matching the form add(x, add(x, y)) where y is odd. | ||||
998 | // TODO: This could be generalized to clearing any bit set in y where the | ||||
999 | // following bit is known to be unset in y. | ||||
1000 | Value *X = nullptr, *Y = nullptr; | ||||
1001 | if (!Known.Zero[0] && !Known.One[0] && | ||||
1002 | match(I, m_c_BinOp(m_Value(X), m_Add(m_Deferred(X), m_Value(Y))))) { | ||||
1003 | Known2.resetAll(); | ||||
1004 | computeKnownBits(Y, Known2, Depth + 1, Q); | ||||
1005 | if (Known2.countMinTrailingOnes() > 0) | ||||
1006 | Known.Zero.setBit(0); | ||||
1007 | } | ||||
1008 | break; | ||||
1009 | } | ||||
1010 | case Instruction::Or: | ||||
1011 | computeKnownBits(I->getOperand(1), Known, Depth + 1, Q); | ||||
1012 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); | ||||
1013 | |||||
1014 | // Output known-0 bits are only known if clear in both the LHS & RHS. | ||||
1015 | Known.Zero &= Known2.Zero; | ||||
1016 | // Output known-1 are known to be set if set in either the LHS | RHS. | ||||
1017 | Known.One |= Known2.One; | ||||
1018 | break; | ||||
1019 | case Instruction::Xor: { | ||||
1020 | computeKnownBits(I->getOperand(1), Known, Depth + 1, Q); | ||||
1021 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); | ||||
1022 | |||||
1023 | // Output known-0 bits are known if clear or set in both the LHS & RHS. | ||||
1024 | APInt KnownZeroOut = (Known.Zero & Known2.Zero) | (Known.One & Known2.One); | ||||
1025 | // Output known-1 are known to be set if set in only one of the LHS, RHS. | ||||
1026 | Known.One = (Known.Zero & Known2.One) | (Known.One & Known2.Zero); | ||||
1027 | Known.Zero = std::move(KnownZeroOut); | ||||
1028 | break; | ||||
1029 | } | ||||
1030 | case Instruction::Mul: { | ||||
1031 | bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I)); | ||||
1032 | computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, Known, | ||||
1033 | Known2, Depth, Q); | ||||
1034 | break; | ||||
1035 | } | ||||
1036 | case Instruction::UDiv: { | ||||
1037 | // For the purposes of computing leading zeros we can conservatively | ||||
1038 | // treat a udiv as a logical right shift by the power of 2 known to | ||||
1039 | // be less than the denominator. | ||||
1040 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); | ||||
1041 | unsigned LeadZ = Known2.countMinLeadingZeros(); | ||||
1042 | |||||
1043 | Known2.resetAll(); | ||||
1044 | computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); | ||||
1045 | unsigned RHSMaxLeadingZeros = Known2.countMaxLeadingZeros(); | ||||
1046 | if (RHSMaxLeadingZeros != BitWidth) | ||||
1047 | LeadZ = std::min(BitWidth, LeadZ + BitWidth - RHSMaxLeadingZeros - 1); | ||||
1048 | |||||
1049 | Known.Zero.setHighBits(LeadZ); | ||||
1050 | break; | ||||
1051 | } | ||||
1052 | case Instruction::Select: { | ||||
1053 | const Value *LHS, *RHS; | ||||
1054 | SelectPatternFlavor SPF = matchSelectPattern(I, LHS, RHS).Flavor; | ||||
1055 | if (SelectPatternResult::isMinOrMax(SPF)) { | ||||
1056 | computeKnownBits(RHS, Known, Depth + 1, Q); | ||||
1057 | computeKnownBits(LHS, Known2, Depth + 1, Q); | ||||
1058 | } else { | ||||
1059 | computeKnownBits(I->getOperand(2), Known, Depth + 1, Q); | ||||
1060 | computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); | ||||
1061 | } | ||||
1062 | |||||
1063 | unsigned MaxHighOnes = 0; | ||||
1064 | unsigned MaxHighZeros = 0; | ||||
1065 | if (SPF == SPF_SMAX) { | ||||
1066 | // If both sides are negative, the result is negative. | ||||
1067 | if (Known.isNegative() && Known2.isNegative()) | ||||
1068 | // We can derive a lower bound on the result by taking the max of the | ||||
1069 | // leading one bits. | ||||
1070 | MaxHighOnes = | ||||
1071 | std::max(Known.countMinLeadingOnes(), Known2.countMinLeadingOnes()); | ||||
1072 | // If either side is non-negative, the result is non-negative. | ||||
1073 | else if (Known.isNonNegative() || Known2.isNonNegative()) | ||||
1074 | MaxHighZeros = 1; | ||||
1075 | } else if (SPF == SPF_SMIN) { | ||||
1076 | // If both sides are non-negative, the result is non-negative. | ||||
1077 | if (Known.isNonNegative() && Known2.isNonNegative()) | ||||
1078 | // We can derive an upper bound on the result by taking the max of the | ||||
1079 | // leading zero bits. | ||||
1080 | MaxHighZeros = std::max(Known.countMinLeadingZeros(), | ||||
1081 | Known2.countMinLeadingZeros()); | ||||
1082 | // If either side is negative, the result is negative. | ||||
1083 | else if (Known.isNegative() || Known2.isNegative()) | ||||
1084 | MaxHighOnes = 1; | ||||
1085 | } else if (SPF == SPF_UMAX) { | ||||
1086 | // We can derive a lower bound on the result by taking the max of the | ||||
1087 | // leading one bits. | ||||
1088 | MaxHighOnes = | ||||
1089 | std::max(Known.countMinLeadingOnes(), Known2.countMinLeadingOnes()); | ||||
1090 | } else if (SPF == SPF_UMIN) { | ||||
1091 | // We can derive an upper bound on the result by taking the max of the | ||||
1092 | // leading zero bits. | ||||
1093 | MaxHighZeros = | ||||
1094 | std::max(Known.countMinLeadingZeros(), Known2.countMinLeadingZeros()); | ||||
1095 | } else if (SPF == SPF_ABS) { | ||||
1096 | // RHS from matchSelectPattern returns the negation part of abs pattern. | ||||
1097 | // If the negate has an NSW flag we can assume the sign bit of the result | ||||
1098 | // will be 0 because that makes abs(INT_MIN) undefined. | ||||
1099 | if (Q.IIQ.hasNoSignedWrap(cast<Instruction>(RHS))) | ||||
1100 | MaxHighZeros = 1; | ||||
1101 | } | ||||
1102 | |||||
1103 | // Only known if known in both the LHS and RHS. | ||||
1104 | Known.One &= Known2.One; | ||||
1105 | Known.Zero &= Known2.Zero; | ||||
1106 | if (MaxHighOnes > 0) | ||||
1107 | Known.One.setHighBits(MaxHighOnes); | ||||
1108 | if (MaxHighZeros > 0) | ||||
1109 | Known.Zero.setHighBits(MaxHighZeros); | ||||
1110 | break; | ||||
1111 | } | ||||
1112 | case Instruction::FPTrunc: | ||||
1113 | case Instruction::FPExt: | ||||
1114 | case Instruction::FPToUI: | ||||
1115 | case Instruction::FPToSI: | ||||
1116 | case Instruction::SIToFP: | ||||
1117 | case Instruction::UIToFP: | ||||
1118 | break; // Can't work with floating point. | ||||
1119 | case Instruction::PtrToInt: | ||||
1120 | case Instruction::IntToPtr: | ||||
1121 | // Fall through and handle them the same as zext/trunc. | ||||
1122 | LLVM_FALLTHROUGH[[clang::fallthrough]]; | ||||
1123 | case Instruction::ZExt: | ||||
1124 | case Instruction::Trunc: { | ||||
1125 | Type *SrcTy = I->getOperand(0)->getType(); | ||||
1126 | |||||
1127 | unsigned SrcBitWidth; | ||||
1128 | // Note that we handle pointer operands here because of inttoptr/ptrtoint | ||||
1129 | // which fall through here. | ||||
1130 | Type *ScalarTy = SrcTy->getScalarType(); | ||||
1131 | SrcBitWidth = ScalarTy->isPointerTy() ? | ||||
1132 | Q.DL.getIndexTypeSizeInBits(ScalarTy) : | ||||
1133 | Q.DL.getTypeSizeInBits(ScalarTy); | ||||
1134 | |||||
1135 | assert(SrcBitWidth && "SrcBitWidth can't be zero")((SrcBitWidth && "SrcBitWidth can't be zero") ? static_cast <void> (0) : __assert_fail ("SrcBitWidth && \"SrcBitWidth can't be zero\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 1135, __PRETTY_FUNCTION__)); | ||||
1136 | Known = Known.zextOrTrunc(SrcBitWidth, false); | ||||
1137 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||
1138 | Known = Known.zextOrTrunc(BitWidth, true /* ExtendedBitsAreKnownZero */); | ||||
1139 | break; | ||||
1140 | } | ||||
1141 | case Instruction::BitCast: { | ||||
1142 | Type *SrcTy = I->getOperand(0)->getType(); | ||||
1143 | if (SrcTy->isIntOrPtrTy() && | ||||
1144 | // TODO: For now, not handling conversions like: | ||||
1145 | // (bitcast i64 %x to <2 x i32>) | ||||
1146 | !I->getType()->isVectorTy()) { | ||||
1147 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||
1148 | break; | ||||
1149 | } | ||||
1150 | break; | ||||
1151 | } | ||||
1152 | case Instruction::SExt: { | ||||
1153 | // Compute the bits in the result that are not present in the input. | ||||
1154 | unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits(); | ||||
1155 | |||||
1156 | Known = Known.trunc(SrcBitWidth); | ||||
1157 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||
1158 | // If the sign bit of the input is known set or clear, then we know the | ||||
1159 | // top bits of the result. | ||||
1160 | Known = Known.sext(BitWidth); | ||||
1161 | break; | ||||
1162 | } | ||||
1163 | case Instruction::Shl: { | ||||
1164 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 | ||||
1165 | bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I)); | ||||
1166 | auto KZF = [NSW](const APInt &KnownZero, unsigned ShiftAmt) { | ||||
1167 | APInt KZResult = KnownZero << ShiftAmt; | ||||
1168 | KZResult.setLowBits(ShiftAmt); // Low bits known 0. | ||||
1169 | // If this shift has "nsw" keyword, then the result is either a poison | ||||
1170 | // value or has the same sign bit as the first operand. | ||||
1171 | if (NSW && KnownZero.isSignBitSet()) | ||||
1172 | KZResult.setSignBit(); | ||||
1173 | return KZResult; | ||||
1174 | }; | ||||
1175 | |||||
1176 | auto KOF = [NSW](const APInt &KnownOne, unsigned ShiftAmt) { | ||||
1177 | APInt KOResult = KnownOne << ShiftAmt; | ||||
1178 | if (NSW && KnownOne.isSignBitSet()) | ||||
1179 | KOResult.setSignBit(); | ||||
1180 | return KOResult; | ||||
1181 | }; | ||||
1182 | |||||
1183 | computeKnownBitsFromShiftOperator(I, Known, Known2, Depth, Q, KZF, KOF); | ||||
1184 | break; | ||||
1185 | } | ||||
1186 | case Instruction::LShr: { | ||||
1187 | // (lshr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 | ||||
1188 | auto KZF = [](const APInt &KnownZero, unsigned ShiftAmt) { | ||||
1189 | APInt KZResult = KnownZero.lshr(ShiftAmt); | ||||
1190 | // High bits known zero. | ||||
1191 | KZResult.setHighBits(ShiftAmt); | ||||
1192 | return KZResult; | ||||
1193 | }; | ||||
1194 | |||||
1195 | auto KOF = [](const APInt &KnownOne, unsigned ShiftAmt) { | ||||
1196 | return KnownOne.lshr(ShiftAmt); | ||||
1197 | }; | ||||
1198 | |||||
1199 | computeKnownBitsFromShiftOperator(I, Known, Known2, Depth, Q, KZF, KOF); | ||||
1200 | break; | ||||
1201 | } | ||||
1202 | case Instruction::AShr: { | ||||
1203 | // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 | ||||
1204 | auto KZF = [](const APInt &KnownZero, unsigned ShiftAmt) { | ||||
1205 | return KnownZero.ashr(ShiftAmt); | ||||
1206 | }; | ||||
1207 | |||||
1208 | auto KOF = [](const APInt &KnownOne, unsigned ShiftAmt) { | ||||
1209 | return KnownOne.ashr(ShiftAmt); | ||||
1210 | }; | ||||
1211 | |||||
1212 | computeKnownBitsFromShiftOperator(I, Known, Known2, Depth, Q, KZF, KOF); | ||||
1213 | break; | ||||
1214 | } | ||||
1215 | case Instruction::Sub: { | ||||
1216 | bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I)); | ||||
1217 | computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, | ||||
1218 | Known, Known2, Depth, Q); | ||||
1219 | break; | ||||
1220 | } | ||||
1221 | case Instruction::Add: { | ||||
1222 | bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I)); | ||||
1223 | computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, | ||||
1224 | Known, Known2, Depth, Q); | ||||
1225 | break; | ||||
1226 | } | ||||
1227 | case Instruction::SRem: | ||||
1228 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
1229 | APInt RA = Rem->getValue().abs(); | ||||
1230 | if (RA.isPowerOf2()) { | ||||
1231 | APInt LowBits = RA - 1; | ||||
1232 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); | ||||
1233 | |||||
1234 | // The low bits of the first operand are unchanged by the srem. | ||||
1235 | Known.Zero = Known2.Zero & LowBits; | ||||
1236 | Known.One = Known2.One & LowBits; | ||||
1237 | |||||
1238 | // If the first operand is non-negative or has all low bits zero, then | ||||
1239 | // the upper bits are all zero. | ||||
1240 | if (Known2.isNonNegative() || LowBits.isSubsetOf(Known2.Zero)) | ||||
1241 | Known.Zero |= ~LowBits; | ||||
1242 | |||||
1243 | // If the first operand is negative and not all low bits are zero, then | ||||
1244 | // the upper bits are all one. | ||||
1245 | if (Known2.isNegative() && LowBits.intersects(Known2.One)) | ||||
1246 | Known.One |= ~LowBits; | ||||
1247 | |||||
1248 | assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?")(((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?" ) ? static_cast<void> (0) : __assert_fail ("(Known.Zero & Known.One) == 0 && \"Bits known to be one AND zero?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 1248, __PRETTY_FUNCTION__)); | ||||
1249 | break; | ||||
1250 | } | ||||
1251 | } | ||||
1252 | |||||
1253 | // The sign bit is the LHS's sign bit, except when the result of the | ||||
1254 | // remainder is zero. | ||||
1255 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); | ||||
1256 | // If it's known zero, our sign bit is also zero. | ||||
1257 | if (Known2.isNonNegative()) | ||||
1258 | Known.makeNonNegative(); | ||||
1259 | |||||
1260 | break; | ||||
1261 | case Instruction::URem: { | ||||
1262 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
1263 | const APInt &RA = Rem->getValue(); | ||||
1264 | if (RA.isPowerOf2()) { | ||||
1265 | APInt LowBits = (RA - 1); | ||||
1266 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||
1267 | Known.Zero |= ~LowBits; | ||||
1268 | Known.One &= LowBits; | ||||
1269 | break; | ||||
1270 | } | ||||
1271 | } | ||||
1272 | |||||
1273 | // Since the result is less than or equal to either operand, any leading | ||||
1274 | // zero bits in either operand must also exist in the result. | ||||
1275 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||
1276 | computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); | ||||
1277 | |||||
1278 | unsigned Leaders = | ||||
1279 | std::max(Known.countMinLeadingZeros(), Known2.countMinLeadingZeros()); | ||||
1280 | Known.resetAll(); | ||||
1281 | Known.Zero.setHighBits(Leaders); | ||||
1282 | break; | ||||
1283 | } | ||||
1284 | |||||
1285 | case Instruction::Alloca: { | ||||
1286 | const AllocaInst *AI = cast<AllocaInst>(I); | ||||
1287 | unsigned Align = AI->getAlignment(); | ||||
1288 | if (Align == 0) | ||||
1289 | Align = Q.DL.getABITypeAlignment(AI->getAllocatedType()); | ||||
1290 | |||||
1291 | if (Align > 0) | ||||
1292 | Known.Zero.setLowBits(countTrailingZeros(Align)); | ||||
1293 | break; | ||||
1294 | } | ||||
1295 | case Instruction::GetElementPtr: { | ||||
1296 | // Analyze all of the subscripts of this getelementptr instruction | ||||
1297 | // to determine if we can prove known low zero bits. | ||||
1298 | KnownBits LocalKnown(BitWidth); | ||||
1299 | computeKnownBits(I->getOperand(0), LocalKnown, Depth + 1, Q); | ||||
1300 | unsigned TrailZ = LocalKnown.countMinTrailingZeros(); | ||||
1301 | |||||
1302 | gep_type_iterator GTI = gep_type_begin(I); | ||||
1303 | for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) { | ||||
1304 | Value *Index = I->getOperand(i); | ||||
1305 | if (StructType *STy = GTI.getStructTypeOrNull()) { | ||||
1306 | // Handle struct member offset arithmetic. | ||||
1307 | |||||
1308 | // Handle case when index is vector zeroinitializer | ||||
1309 | Constant *CIndex = cast<Constant>(Index); | ||||
1310 | if (CIndex->isZeroValue()) | ||||
1311 | continue; | ||||
1312 | |||||
1313 | if (CIndex->getType()->isVectorTy()) | ||||
1314 | Index = CIndex->getSplatValue(); | ||||
1315 | |||||
1316 | unsigned Idx = cast<ConstantInt>(Index)->getZExtValue(); | ||||
1317 | const StructLayout *SL = Q.DL.getStructLayout(STy); | ||||
1318 | uint64_t Offset = SL->getElementOffset(Idx); | ||||
1319 | TrailZ = std::min<unsigned>(TrailZ, | ||||
1320 | countTrailingZeros(Offset)); | ||||
1321 | } else { | ||||
1322 | // Handle array index arithmetic. | ||||
1323 | Type *IndexedTy = GTI.getIndexedType(); | ||||
1324 | if (!IndexedTy->isSized()) { | ||||
1325 | TrailZ = 0; | ||||
1326 | break; | ||||
1327 | } | ||||
1328 | unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits(); | ||||
1329 | uint64_t TypeSize = Q.DL.getTypeAllocSize(IndexedTy); | ||||
1330 | LocalKnown.Zero = LocalKnown.One = APInt(GEPOpiBits, 0); | ||||
1331 | computeKnownBits(Index, LocalKnown, Depth + 1, Q); | ||||
1332 | TrailZ = std::min(TrailZ, | ||||
1333 | unsigned(countTrailingZeros(TypeSize) + | ||||
1334 | LocalKnown.countMinTrailingZeros())); | ||||
1335 | } | ||||
1336 | } | ||||
1337 | |||||
1338 | Known.Zero.setLowBits(TrailZ); | ||||
1339 | break; | ||||
1340 | } | ||||
1341 | case Instruction::PHI: { | ||||
1342 | const PHINode *P = cast<PHINode>(I); | ||||
1343 | // Handle the case of a simple two-predecessor recurrence PHI. | ||||
1344 | // There's a lot more that could theoretically be done here, but | ||||
1345 | // this is sufficient to catch some interesting cases. | ||||
1346 | if (P->getNumIncomingValues() == 2) { | ||||
1347 | for (unsigned i = 0; i != 2; ++i) { | ||||
1348 | Value *L = P->getIncomingValue(i); | ||||
1349 | Value *R = P->getIncomingValue(!i); | ||||
1350 | Operator *LU = dyn_cast<Operator>(L); | ||||
1351 | if (!LU) | ||||
1352 | continue; | ||||
1353 | unsigned Opcode = LU->getOpcode(); | ||||
1354 | // Check for operations that have the property that if | ||||
1355 | // both their operands have low zero bits, the result | ||||
1356 | // will have low zero bits. | ||||
1357 | if (Opcode == Instruction::Add || | ||||
1358 | Opcode == Instruction::Sub || | ||||
1359 | Opcode == Instruction::And || | ||||
1360 | Opcode == Instruction::Or || | ||||
1361 | Opcode == Instruction::Mul) { | ||||
1362 | Value *LL = LU->getOperand(0); | ||||
1363 | Value *LR = LU->getOperand(1); | ||||
1364 | // Find a recurrence. | ||||
1365 | if (LL == I) | ||||
1366 | L = LR; | ||||
1367 | else if (LR == I) | ||||
1368 | L = LL; | ||||
1369 | else | ||||
1370 | break; | ||||
1371 | // Ok, we have a PHI of the form L op= R. Check for low | ||||
1372 | // zero bits. | ||||
1373 | computeKnownBits(R, Known2, Depth + 1, Q); | ||||
1374 | |||||
1375 | // We need to take the minimum number of known bits | ||||
1376 | KnownBits Known3(Known); | ||||
1377 | computeKnownBits(L, Known3, Depth + 1, Q); | ||||
1378 | |||||
1379 | Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(), | ||||
1380 | Known3.countMinTrailingZeros())); | ||||
1381 | |||||
1382 | auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(LU); | ||||
1383 | if (OverflowOp && Q.IIQ.hasNoSignedWrap(OverflowOp)) { | ||||
1384 | // If initial value of recurrence is nonnegative, and we are adding | ||||
1385 | // a nonnegative number with nsw, the result can only be nonnegative | ||||
1386 | // or poison value regardless of the number of times we execute the | ||||
1387 | // add in phi recurrence. If initial value is negative and we are | ||||
1388 | // adding a negative number with nsw, the result can only be | ||||
1389 | // negative or poison value. Similar arguments apply to sub and mul. | ||||
1390 | // | ||||
1391 | // (add non-negative, non-negative) --> non-negative | ||||
1392 | // (add negative, negative) --> negative | ||||
1393 | if (Opcode == Instruction::Add) { | ||||
1394 | if (Known2.isNonNegative() && Known3.isNonNegative()) | ||||
1395 | Known.makeNonNegative(); | ||||
1396 | else if (Known2.isNegative() && Known3.isNegative()) | ||||
1397 | Known.makeNegative(); | ||||
1398 | } | ||||
1399 | |||||
1400 | // (sub nsw non-negative, negative) --> non-negative | ||||
1401 | // (sub nsw negative, non-negative) --> negative | ||||
1402 | else if (Opcode == Instruction::Sub && LL == I) { | ||||
1403 | if (Known2.isNonNegative() && Known3.isNegative()) | ||||
1404 | Known.makeNonNegative(); | ||||
1405 | else if (Known2.isNegative() && Known3.isNonNegative()) | ||||
1406 | Known.makeNegative(); | ||||
1407 | } | ||||
1408 | |||||
1409 | // (mul nsw non-negative, non-negative) --> non-negative | ||||
1410 | else if (Opcode == Instruction::Mul && Known2.isNonNegative() && | ||||
1411 | Known3.isNonNegative()) | ||||
1412 | Known.makeNonNegative(); | ||||
1413 | } | ||||
1414 | |||||
1415 | break; | ||||
1416 | } | ||||
1417 | } | ||||
1418 | } | ||||
1419 | |||||
1420 | // Unreachable blocks may have zero-operand PHI nodes. | ||||
1421 | if (P->getNumIncomingValues() == 0) | ||||
1422 | break; | ||||
1423 | |||||
1424 | // Otherwise take the unions of the known bit sets of the operands, | ||||
1425 | // taking conservative care to avoid excessive recursion. | ||||
1426 | if (Depth < MaxDepth - 1 && !Known.Zero && !Known.One) { | ||||
1427 | // Skip if every incoming value references to ourself. | ||||
1428 | if (dyn_cast_or_null<UndefValue>(P->hasConstantValue())) | ||||
1429 | break; | ||||
1430 | |||||
1431 | Known.Zero.setAllBits(); | ||||
1432 | Known.One.setAllBits(); | ||||
1433 | for (Value *IncValue : P->incoming_values()) { | ||||
1434 | // Skip direct self references. | ||||
1435 | if (IncValue == P) continue; | ||||
1436 | |||||
1437 | Known2 = KnownBits(BitWidth); | ||||
1438 | // Recurse, but cap the recursion to one level, because we don't | ||||
1439 | // want to waste time spinning around in loops. | ||||
1440 | computeKnownBits(IncValue, Known2, MaxDepth - 1, Q); | ||||
1441 | Known.Zero &= Known2.Zero; | ||||
1442 | Known.One &= Known2.One; | ||||
1443 | // If all bits have been ruled out, there's no need to check | ||||
1444 | // more operands. | ||||
1445 | if (!Known.Zero && !Known.One) | ||||
1446 | break; | ||||
1447 | } | ||||
1448 | } | ||||
1449 | break; | ||||
1450 | } | ||||
1451 | case Instruction::Call: | ||||
1452 | case Instruction::Invoke: | ||||
1453 | // If range metadata is attached to this call, set known bits from that, | ||||
1454 | // and then intersect with known bits based on other properties of the | ||||
1455 | // function. | ||||
1456 | if (MDNode *MD = | ||||
1457 | Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range)) | ||||
1458 | computeKnownBitsFromRangeMetadata(*MD, Known); | ||||
1459 | if (const Value *RV = ImmutableCallSite(I).getReturnedArgOperand()) { | ||||
1460 | computeKnownBits(RV, Known2, Depth + 1, Q); | ||||
1461 | Known.Zero |= Known2.Zero; | ||||
1462 | Known.One |= Known2.One; | ||||
1463 | } | ||||
1464 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { | ||||
1465 | switch (II->getIntrinsicID()) { | ||||
1466 | default: break; | ||||
1467 | case Intrinsic::bitreverse: | ||||
1468 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); | ||||
1469 | Known.Zero |= Known2.Zero.reverseBits(); | ||||
1470 | Known.One |= Known2.One.reverseBits(); | ||||
1471 | break; | ||||
1472 | case Intrinsic::bswap: | ||||
1473 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); | ||||
1474 | Known.Zero |= Known2.Zero.byteSwap(); | ||||
1475 | Known.One |= Known2.One.byteSwap(); | ||||
1476 | break; | ||||
1477 | case Intrinsic::ctlz: { | ||||
1478 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); | ||||
1479 | // If we have a known 1, its position is our upper bound. | ||||
1480 | unsigned PossibleLZ = Known2.One.countLeadingZeros(); | ||||
1481 | // If this call is undefined for 0, the result will be less than 2^n. | ||||
1482 | if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext())) | ||||
1483 | PossibleLZ = std::min(PossibleLZ, BitWidth - 1); | ||||
1484 | unsigned LowBits = Log2_32(PossibleLZ)+1; | ||||
1485 | Known.Zero.setBitsFrom(LowBits); | ||||
1486 | break; | ||||
1487 | } | ||||
1488 | case Intrinsic::cttz: { | ||||
1489 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); | ||||
1490 | // If we have a known 1, its position is our upper bound. | ||||
1491 | unsigned PossibleTZ = Known2.One.countTrailingZeros(); | ||||
1492 | // If this call is undefined for 0, the result will be less than 2^n. | ||||
1493 | if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext())) | ||||
1494 | PossibleTZ = std::min(PossibleTZ, BitWidth - 1); | ||||
1495 | unsigned LowBits = Log2_32(PossibleTZ)+1; | ||||
1496 | Known.Zero.setBitsFrom(LowBits); | ||||
1497 | break; | ||||
1498 | } | ||||
1499 | case Intrinsic::ctpop: { | ||||
1500 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); | ||||
1501 | // We can bound the space the count needs. Also, bits known to be zero | ||||
1502 | // can't contribute to the population. | ||||
1503 | unsigned BitsPossiblySet = Known2.countMaxPopulation(); | ||||
1504 | unsigned LowBits = Log2_32(BitsPossiblySet)+1; | ||||
1505 | Known.Zero.setBitsFrom(LowBits); | ||||
1506 | // TODO: we could bound KnownOne using the lower bound on the number | ||||
1507 | // of bits which might be set provided by popcnt KnownOne2. | ||||
1508 | break; | ||||
1509 | } | ||||
1510 | case Intrinsic::fshr: | ||||
1511 | case Intrinsic::fshl: { | ||||
1512 | const APInt *SA; | ||||
1513 | if (!match(I->getOperand(2), m_APInt(SA))) | ||||
1514 | break; | ||||
1515 | |||||
1516 | // Normalize to funnel shift left. | ||||
1517 | uint64_t ShiftAmt = SA->urem(BitWidth); | ||||
1518 | if (II->getIntrinsicID() == Intrinsic::fshr) | ||||
1519 | ShiftAmt = BitWidth - ShiftAmt; | ||||
1520 | |||||
1521 | KnownBits Known3(Known); | ||||
1522 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); | ||||
1523 | computeKnownBits(I->getOperand(1), Known3, Depth + 1, Q); | ||||
1524 | |||||
1525 | Known.Zero = | ||||
1526 | Known2.Zero.shl(ShiftAmt) | Known3.Zero.lshr(BitWidth - ShiftAmt); | ||||
1527 | Known.One = | ||||
1528 | Known2.One.shl(ShiftAmt) | Known3.One.lshr(BitWidth - ShiftAmt); | ||||
1529 | break; | ||||
1530 | } | ||||
1531 | case Intrinsic::uadd_sat: | ||||
1532 | case Intrinsic::usub_sat: { | ||||
1533 | bool IsAdd = II->getIntrinsicID() == Intrinsic::uadd_sat; | ||||
1534 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||
1535 | computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); | ||||
1536 | |||||
1537 | // Add: Leading ones of either operand are preserved. | ||||
1538 | // Sub: Leading zeros of LHS and leading ones of RHS are preserved | ||||
1539 | // as leading zeros in the result. | ||||
1540 | unsigned LeadingKnown; | ||||
1541 | if (IsAdd) | ||||
1542 | LeadingKnown = std::max(Known.countMinLeadingOnes(), | ||||
1543 | Known2.countMinLeadingOnes()); | ||||
1544 | else | ||||
1545 | LeadingKnown = std::max(Known.countMinLeadingZeros(), | ||||
1546 | Known2.countMinLeadingOnes()); | ||||
1547 | |||||
1548 | Known = KnownBits::computeForAddSub( | ||||
1549 | IsAdd, /* NSW */ false, Known, Known2); | ||||
1550 | |||||
1551 | // We select between the operation result and all-ones/zero | ||||
1552 | // respectively, so we can preserve known ones/zeros. | ||||
1553 | if (IsAdd) { | ||||
1554 | Known.One.setHighBits(LeadingKnown); | ||||
1555 | Known.Zero.clearAllBits(); | ||||
1556 | } else { | ||||
1557 | Known.Zero.setHighBits(LeadingKnown); | ||||
1558 | Known.One.clearAllBits(); | ||||
1559 | } | ||||
1560 | break; | ||||
1561 | } | ||||
1562 | case Intrinsic::x86_sse42_crc32_64_64: | ||||
1563 | Known.Zero.setBitsFrom(32); | ||||
1564 | break; | ||||
1565 | } | ||||
1566 | } | ||||
1567 | break; | ||||
1568 | case Instruction::ExtractElement: | ||||
1569 | // Look through extract element. At the moment we keep this simple and skip | ||||
1570 | // tracking the specific element. But at least we might find information | ||||
1571 | // valid for all elements of the vector (for example if vector is sign | ||||
1572 | // extended, shifted, etc). | ||||
1573 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||
1574 | break; | ||||
1575 | case Instruction::ExtractValue: | ||||
1576 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) { | ||||
1577 | const ExtractValueInst *EVI = cast<ExtractValueInst>(I); | ||||
1578 | if (EVI->getNumIndices() != 1) break; | ||||
1579 | if (EVI->getIndices()[0] == 0) { | ||||
1580 | switch (II->getIntrinsicID()) { | ||||
1581 | default: break; | ||||
1582 | case Intrinsic::uadd_with_overflow: | ||||
1583 | case Intrinsic::sadd_with_overflow: | ||||
1584 | computeKnownBitsAddSub(true, II->getArgOperand(0), | ||||
1585 | II->getArgOperand(1), false, Known, Known2, | ||||
1586 | Depth, Q); | ||||
1587 | break; | ||||
1588 | case Intrinsic::usub_with_overflow: | ||||
1589 | case Intrinsic::ssub_with_overflow: | ||||
1590 | computeKnownBitsAddSub(false, II->getArgOperand(0), | ||||
1591 | II->getArgOperand(1), false, Known, Known2, | ||||
1592 | Depth, Q); | ||||
1593 | break; | ||||
1594 | case Intrinsic::umul_with_overflow: | ||||
1595 | case Intrinsic::smul_with_overflow: | ||||
1596 | computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false, | ||||
1597 | Known, Known2, Depth, Q); | ||||
1598 | break; | ||||
1599 | } | ||||
1600 | } | ||||
1601 | } | ||||
1602 | } | ||||
1603 | } | ||||
1604 | |||||
1605 | /// Determine which bits of V are known to be either zero or one and return | ||||
1606 | /// them. | ||||
1607 | KnownBits computeKnownBits(const Value *V, unsigned Depth, const Query &Q) { | ||||
1608 | KnownBits Known(getBitWidth(V->getType(), Q.DL)); | ||||
1609 | computeKnownBits(V, Known, Depth, Q); | ||||
1610 | return Known; | ||||
1611 | } | ||||
1612 | |||||
1613 | /// Determine which bits of V are known to be either zero or one and return | ||||
1614 | /// them in the Known bit set. | ||||
1615 | /// | ||||
1616 | /// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that | ||||
1617 | /// we cannot optimize based on the assumption that it is zero without changing | ||||
1618 | /// it to be an explicit zero. If we don't change it to zero, other code could | ||||
1619 | /// optimized based on the contradictory assumption that it is non-zero. | ||||
1620 | /// Because instcombine aggressively folds operations with undef args anyway, | ||||
1621 | /// this won't lose us code quality. | ||||
1622 | /// | ||||
1623 | /// This function is defined on values with integer type, values with pointer | ||||
1624 | /// type, and vectors of integers. In the case | ||||
1625 | /// where V is a vector, known zero, and known one values are the | ||||
1626 | /// same width as the vector element, and the bit is set only if it is true | ||||
1627 | /// for all of the elements in the vector. | ||||
1628 | void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, | ||||
1629 | const Query &Q) { | ||||
1630 | assert(V && "No Value?")((V && "No Value?") ? static_cast<void> (0) : __assert_fail ("V && \"No Value?\"", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 1630, __PRETTY_FUNCTION__)); | ||||
1631 | assert(Depth <= MaxDepth && "Limit Search Depth")((Depth <= MaxDepth && "Limit Search Depth") ? static_cast <void> (0) : __assert_fail ("Depth <= MaxDepth && \"Limit Search Depth\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 1631, __PRETTY_FUNCTION__)); | ||||
1632 | unsigned BitWidth = Known.getBitWidth(); | ||||
1633 | |||||
1634 | assert((V->getType()->isIntOrIntVectorTy(BitWidth) ||(((V->getType()->isIntOrIntVectorTy(BitWidth) || V-> getType()->isPtrOrPtrVectorTy()) && "Not integer or pointer type!" ) ? static_cast<void> (0) : __assert_fail ("(V->getType()->isIntOrIntVectorTy(BitWidth) || V->getType()->isPtrOrPtrVectorTy()) && \"Not integer or pointer type!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 1636, __PRETTY_FUNCTION__)) | ||||
1635 | V->getType()->isPtrOrPtrVectorTy()) &&(((V->getType()->isIntOrIntVectorTy(BitWidth) || V-> getType()->isPtrOrPtrVectorTy()) && "Not integer or pointer type!" ) ? static_cast<void> (0) : __assert_fail ("(V->getType()->isIntOrIntVectorTy(BitWidth) || V->getType()->isPtrOrPtrVectorTy()) && \"Not integer or pointer type!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 1636, __PRETTY_FUNCTION__)) | ||||
1636 | "Not integer or pointer type!")(((V->getType()->isIntOrIntVectorTy(BitWidth) || V-> getType()->isPtrOrPtrVectorTy()) && "Not integer or pointer type!" ) ? static_cast<void> (0) : __assert_fail ("(V->getType()->isIntOrIntVectorTy(BitWidth) || V->getType()->isPtrOrPtrVectorTy()) && \"Not integer or pointer type!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 1636, __PRETTY_FUNCTION__)); | ||||
1637 | |||||
1638 | Type *ScalarTy = V->getType()->getScalarType(); | ||||
1639 | unsigned ExpectedWidth = ScalarTy->isPointerTy() ? | ||||
1640 | Q.DL.getIndexTypeSizeInBits(ScalarTy) : Q.DL.getTypeSizeInBits(ScalarTy); | ||||
1641 | assert(ExpectedWidth == BitWidth && "V and Known should have same BitWidth")((ExpectedWidth == BitWidth && "V and Known should have same BitWidth" ) ? static_cast<void> (0) : __assert_fail ("ExpectedWidth == BitWidth && \"V and Known should have same BitWidth\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 1641, __PRETTY_FUNCTION__)); | ||||
1642 | (void)BitWidth; | ||||
1643 | (void)ExpectedWidth; | ||||
1644 | |||||
1645 | const APInt *C; | ||||
1646 | if (match(V, m_APInt(C))) { | ||||
1647 | // We know all of the bits for a scalar constant or a splat vector constant! | ||||
1648 | Known.One = *C; | ||||
1649 | Known.Zero = ~Known.One; | ||||
1650 | return; | ||||
1651 | } | ||||
1652 | // Null and aggregate-zero are all-zeros. | ||||
1653 | if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) { | ||||
1654 | Known.setAllZero(); | ||||
1655 | return; | ||||
1656 | } | ||||
1657 | // Handle a constant vector by taking the intersection of the known bits of | ||||
1658 | // each element. | ||||
1659 | if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V)) { | ||||
1660 | // We know that CDS must be a vector of integers. Take the intersection of | ||||
1661 | // each element. | ||||
1662 | Known.Zero.setAllBits(); Known.One.setAllBits(); | ||||
1663 | for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { | ||||
1664 | APInt Elt = CDS->getElementAsAPInt(i); | ||||
1665 | Known.Zero &= ~Elt; | ||||
1666 | Known.One &= Elt; | ||||
1667 | } | ||||
1668 | return; | ||||
1669 | } | ||||
1670 | |||||
1671 | if (const auto *CV = dyn_cast<ConstantVector>(V)) { | ||||
1672 | // We know that CV must be a vector of integers. Take the intersection of | ||||
1673 | // each element. | ||||
1674 | Known.Zero.setAllBits(); Known.One.setAllBits(); | ||||
1675 | for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) { | ||||
1676 | Constant *Element = CV->getAggregateElement(i); | ||||
1677 | auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element); | ||||
1678 | if (!ElementCI) { | ||||
1679 | Known.resetAll(); | ||||
1680 | return; | ||||
1681 | } | ||||
1682 | const APInt &Elt = ElementCI->getValue(); | ||||
1683 | Known.Zero &= ~Elt; | ||||
1684 | Known.One &= Elt; | ||||
1685 | } | ||||
1686 | return; | ||||
1687 | } | ||||
1688 | |||||
1689 | // Start out not knowing anything. | ||||
1690 | Known.resetAll(); | ||||
1691 | |||||
1692 | // We can't imply anything about undefs. | ||||
1693 | if (isa<UndefValue>(V)) | ||||
1694 | return; | ||||
1695 | |||||
1696 | // There's no point in looking through other users of ConstantData for | ||||
1697 | // assumptions. Confirm that we've handled them all. | ||||
1698 | assert(!isa<ConstantData>(V) && "Unhandled constant data!")((!isa<ConstantData>(V) && "Unhandled constant data!" ) ? static_cast<void> (0) : __assert_fail ("!isa<ConstantData>(V) && \"Unhandled constant data!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 1698, __PRETTY_FUNCTION__)); | ||||
1699 | |||||
1700 | // Limit search depth. | ||||
1701 | // All recursive calls that increase depth must come after this. | ||||
1702 | if (Depth == MaxDepth) | ||||
1703 | return; | ||||
1704 | |||||
1705 | // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has | ||||
1706 | // the bits of its aliasee. | ||||
1707 | if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { | ||||
1708 | if (!GA->isInterposable()) | ||||
1709 | computeKnownBits(GA->getAliasee(), Known, Depth + 1, Q); | ||||
1710 | return; | ||||
1711 | } | ||||
1712 | |||||
1713 | if (const Operator *I = dyn_cast<Operator>(V)) | ||||
1714 | computeKnownBitsFromOperator(I, Known, Depth, Q); | ||||
1715 | |||||
1716 | // Aligned pointers have trailing zeros - refine Known.Zero set | ||||
1717 | if (V->getType()->isPointerTy()) { | ||||
1718 | unsigned Align = V->getPointerAlignment(Q.DL); | ||||
1719 | if (Align) | ||||
1720 | Known.Zero.setLowBits(countTrailingZeros(Align)); | ||||
1721 | } | ||||
1722 | |||||
1723 | // computeKnownBitsFromAssume strictly refines Known. | ||||
1724 | // Therefore, we run them after computeKnownBitsFromOperator. | ||||
1725 | |||||
1726 | // Check whether a nearby assume intrinsic can determine some known bits. | ||||
1727 | computeKnownBitsFromAssume(V, Known, Depth, Q); | ||||
1728 | |||||
1729 | assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?")(((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?" ) ? static_cast<void> (0) : __assert_fail ("(Known.Zero & Known.One) == 0 && \"Bits known to be one AND zero?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 1729, __PRETTY_FUNCTION__)); | ||||
1730 | } | ||||
1731 | |||||
1732 | /// Return true if the given value is known to have exactly one | ||||
1733 | /// bit set when defined. For vectors return true if every element is known to | ||||
1734 | /// be a power of two when defined. Supports values with integer or pointer | ||||
1735 | /// types and vectors of integers. | ||||
1736 | bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth, | ||||
1737 | const Query &Q) { | ||||
1738 | assert(Depth <= MaxDepth && "Limit Search Depth")((Depth <= MaxDepth && "Limit Search Depth") ? static_cast <void> (0) : __assert_fail ("Depth <= MaxDepth && \"Limit Search Depth\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 1738, __PRETTY_FUNCTION__)); | ||||
1739 | |||||
1740 | // Attempt to match against constants. | ||||
1741 | if (OrZero && match(V, m_Power2OrZero())) | ||||
1742 | return true; | ||||
1743 | if (match(V, m_Power2())) | ||||
1744 | return true; | ||||
1745 | |||||
1746 | // 1 << X is clearly a power of two if the one is not shifted off the end. If | ||||
1747 | // it is shifted off the end then the result is undefined. | ||||
1748 | if (match(V, m_Shl(m_One(), m_Value()))) | ||||
1749 | return true; | ||||
1750 | |||||
1751 | // (signmask) >>l X is clearly a power of two if the one is not shifted off | ||||
1752 | // the bottom. If it is shifted off the bottom then the result is undefined. | ||||
1753 | if (match(V, m_LShr(m_SignMask(), m_Value()))) | ||||
1754 | return true; | ||||
1755 | |||||
1756 | // The remaining tests are all recursive, so bail out if we hit the limit. | ||||
1757 | if (Depth++ == MaxDepth) | ||||
1758 | return false; | ||||
1759 | |||||
1760 | Value *X = nullptr, *Y = nullptr; | ||||
1761 | // A shift left or a logical shift right of a power of two is a power of two | ||||
1762 | // or zero. | ||||
1763 | if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) || | ||||
1764 | match(V, m_LShr(m_Value(X), m_Value())))) | ||||
1765 | return isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q); | ||||
1766 | |||||
1767 | if (const ZExtInst *ZI = dyn_cast<ZExtInst>(V)) | ||||
1768 | return isKnownToBeAPowerOfTwo(ZI->getOperand(0), OrZero, Depth, Q); | ||||
1769 | |||||
1770 | if (const SelectInst *SI = dyn_cast<SelectInst>(V)) | ||||
1771 | return isKnownToBeAPowerOfTwo(SI->getTrueValue(), OrZero, Depth, Q) && | ||||
1772 | isKnownToBeAPowerOfTwo(SI->getFalseValue(), OrZero, Depth, Q); | ||||
1773 | |||||
1774 | if (OrZero && match(V, m_And(m_Value(X), m_Value(Y)))) { | ||||
1775 | // A power of two and'd with anything is a power of two or zero. | ||||
1776 | if (isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q) || | ||||
1777 | isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, Depth, Q)) | ||||
1778 | return true; | ||||
1779 | // X & (-X) is always a power of two or zero. | ||||
1780 | if (match(X, m_Neg(m_Specific(Y))) || match(Y, m_Neg(m_Specific(X)))) | ||||
1781 | return true; | ||||
1782 | return false; | ||||
1783 | } | ||||
1784 | |||||
1785 | // Adding a power-of-two or zero to the same power-of-two or zero yields | ||||
1786 | // either the original power-of-two, a larger power-of-two or zero. | ||||
1787 | if (match(V, m_Add(m_Value(X), m_Value(Y)))) { | ||||
1788 | const OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V); | ||||
1789 | if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) || | ||||
1790 | Q.IIQ.hasNoSignedWrap(VOBO)) { | ||||
1791 | if (match(X, m_And(m_Specific(Y), m_Value())) || | ||||
1792 | match(X, m_And(m_Value(), m_Specific(Y)))) | ||||
1793 | if (isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q)) | ||||
1794 | return true; | ||||
1795 | if (match(Y, m_And(m_Specific(X), m_Value())) || | ||||
1796 | match(Y, m_And(m_Value(), m_Specific(X)))) | ||||
1797 | if (isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q)) | ||||
1798 | return true; | ||||
1799 | |||||
1800 | unsigned BitWidth = V->getType()->getScalarSizeInBits(); | ||||
1801 | KnownBits LHSBits(BitWidth); | ||||
1802 | computeKnownBits(X, LHSBits, Depth, Q); | ||||
1803 | |||||
1804 | KnownBits RHSBits(BitWidth); | ||||
1805 | computeKnownBits(Y, RHSBits, Depth, Q); | ||||
1806 | // If i8 V is a power of two or zero: | ||||
1807 | // ZeroBits: 1 1 1 0 1 1 1 1 | ||||
1808 | // ~ZeroBits: 0 0 0 1 0 0 0 0 | ||||
1809 | if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2()) | ||||
1810 | // If OrZero isn't set, we cannot give back a zero result. | ||||
1811 | // Make sure either the LHS or RHS has a bit set. | ||||
1812 | if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue()) | ||||
1813 | return true; | ||||
1814 | } | ||||
1815 | } | ||||
1816 | |||||
1817 | // An exact divide or right shift can only shift off zero bits, so the result | ||||
1818 | // is a power of two only if the first operand is a power of two and not | ||||
1819 | // copying a sign bit (sdiv int_min, 2). | ||||
1820 | if (match(V, m_Exact(m_LShr(m_Value(), m_Value()))) || | ||||
1821 | match(V, m_Exact(m_UDiv(m_Value(), m_Value())))) { | ||||
1822 | return isKnownToBeAPowerOfTwo(cast<Operator>(V)->getOperand(0), OrZero, | ||||
1823 | Depth, Q); | ||||
1824 | } | ||||
1825 | |||||
1826 | return false; | ||||
1827 | } | ||||
1828 | |||||
1829 | /// Test whether a GEP's result is known to be non-null. | ||||
1830 | /// | ||||
1831 | /// Uses properties inherent in a GEP to try to determine whether it is known | ||||
1832 | /// to be non-null. | ||||
1833 | /// | ||||
1834 | /// Currently this routine does not support vector GEPs. | ||||
1835 | static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth, | ||||
1836 | const Query &Q) { | ||||
1837 | const Function *F = nullptr; | ||||
1838 | if (const Instruction *I = dyn_cast<Instruction>(GEP)) | ||||
1839 | F = I->getFunction(); | ||||
1840 | |||||
1841 | if (!GEP->isInBounds() || | ||||
1842 | NullPointerIsDefined(F, GEP->getPointerAddressSpace())) | ||||
1843 | return false; | ||||
1844 | |||||
1845 | // FIXME: Support vector-GEPs. | ||||
1846 | assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP")((GEP->getType()->isPointerTy() && "We only support plain pointer GEP" ) ? static_cast<void> (0) : __assert_fail ("GEP->getType()->isPointerTy() && \"We only support plain pointer GEP\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 1846, __PRETTY_FUNCTION__)); | ||||
1847 | |||||
1848 | // If the base pointer is non-null, we cannot walk to a null address with an | ||||
1849 | // inbounds GEP in address space zero. | ||||
1850 | if (isKnownNonZero(GEP->getPointerOperand(), Depth, Q)) | ||||
1851 | return true; | ||||
1852 | |||||
1853 | // Walk the GEP operands and see if any operand introduces a non-zero offset. | ||||
1854 | // If so, then the GEP cannot produce a null pointer, as doing so would | ||||
1855 | // inherently violate the inbounds contract within address space zero. | ||||
1856 | for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP); | ||||
1857 | GTI != GTE; ++GTI) { | ||||
1858 | // Struct types are easy -- they must always be indexed by a constant. | ||||
1859 | if (StructType *STy = GTI.getStructTypeOrNull()) { | ||||
1860 | ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand()); | ||||
1861 | unsigned ElementIdx = OpC->getZExtValue(); | ||||
1862 | const StructLayout *SL = Q.DL.getStructLayout(STy); | ||||
1863 | uint64_t ElementOffset = SL->getElementOffset(ElementIdx); | ||||
1864 | if (ElementOffset > 0) | ||||
1865 | return true; | ||||
1866 | continue; | ||||
1867 | } | ||||
1868 | |||||
1869 | // If we have a zero-sized type, the index doesn't matter. Keep looping. | ||||
1870 | if (Q.DL.getTypeAllocSize(GTI.getIndexedType()) == 0) | ||||
1871 | continue; | ||||
1872 | |||||
1873 | // Fast path the constant operand case both for efficiency and so we don't | ||||
1874 | // increment Depth when just zipping down an all-constant GEP. | ||||
1875 | if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) { | ||||
1876 | if (!OpC->isZero()) | ||||
1877 | return true; | ||||
1878 | continue; | ||||
1879 | } | ||||
1880 | |||||
1881 | // We post-increment Depth here because while isKnownNonZero increments it | ||||
1882 | // as well, when we pop back up that increment won't persist. We don't want | ||||
1883 | // to recurse 10k times just because we have 10k GEP operands. We don't | ||||
1884 | // bail completely out because we want to handle constant GEPs regardless | ||||
1885 | // of depth. | ||||
1886 | if (Depth++ >= MaxDepth) | ||||
1887 | continue; | ||||
1888 | |||||
1889 | if (isKnownNonZero(GTI.getOperand(), Depth, Q)) | ||||
1890 | return true; | ||||
1891 | } | ||||
1892 | |||||
1893 | return false; | ||||
1894 | } | ||||
1895 | |||||
1896 | static bool isKnownNonNullFromDominatingCondition(const Value *V, | ||||
1897 | const Instruction *CtxI, | ||||
1898 | const DominatorTree *DT) { | ||||
1899 | assert(V->getType()->isPointerTy() && "V must be pointer type")((V->getType()->isPointerTy() && "V must be pointer type" ) ? static_cast<void> (0) : __assert_fail ("V->getType()->isPointerTy() && \"V must be pointer type\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 1899, __PRETTY_FUNCTION__)); | ||||
1900 | assert(!isa<ConstantData>(V) && "Did not expect ConstantPointerNull")((!isa<ConstantData>(V) && "Did not expect ConstantPointerNull" ) ? static_cast<void> (0) : __assert_fail ("!isa<ConstantData>(V) && \"Did not expect ConstantPointerNull\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 1900, __PRETTY_FUNCTION__)); | ||||
1901 | |||||
1902 | if (!CtxI || !DT) | ||||
1903 | return false; | ||||
1904 | |||||
1905 | unsigned NumUsesExplored = 0; | ||||
1906 | for (auto *U : V->users()) { | ||||
1907 | // Avoid massive lists | ||||
1908 | if (NumUsesExplored >= DomConditionsMaxUses) | ||||
1909 | break; | ||||
1910 | NumUsesExplored++; | ||||
1911 | |||||
1912 | // If the value is used as an argument to a call or invoke, then argument | ||||
1913 | // attributes may provide an answer about null-ness. | ||||
1914 | if (auto CS = ImmutableCallSite(U)) | ||||
1915 | if (auto *CalledFunc = CS.getCalledFunction()) | ||||
1916 | for (const Argument &Arg : CalledFunc->args()) | ||||
1917 | if (CS.getArgOperand(Arg.getArgNo()) == V && | ||||
1918 | Arg.hasNonNullAttr() && DT->dominates(CS.getInstruction(), CtxI)) | ||||
1919 | return true; | ||||
1920 | |||||
1921 | // Consider only compare instructions uniquely controlling a branch | ||||
1922 | CmpInst::Predicate Pred; | ||||
1923 | if (!match(const_cast<User *>(U), | ||||
1924 | m_c_ICmp(Pred, m_Specific(V), m_Zero())) || | ||||
1925 | (Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE)) | ||||
1926 | continue; | ||||
1927 | |||||
1928 | SmallVector<const User *, 4> WorkList; | ||||
1929 | SmallPtrSet<const User *, 4> Visited; | ||||
1930 | for (auto *CmpU : U->users()) { | ||||
1931 | assert(WorkList.empty() && "Should be!")((WorkList.empty() && "Should be!") ? static_cast< void> (0) : __assert_fail ("WorkList.empty() && \"Should be!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 1931, __PRETTY_FUNCTION__)); | ||||
1932 | if (Visited.insert(CmpU).second) | ||||
1933 | WorkList.push_back(CmpU); | ||||
1934 | |||||
1935 | while (!WorkList.empty()) { | ||||
1936 | auto *Curr = WorkList.pop_back_val(); | ||||
1937 | |||||
1938 | // If a user is an AND, add all its users to the work list. We only | ||||
1939 | // propagate "pred != null" condition through AND because it is only | ||||
1940 | // correct to assume that all conditions of AND are met in true branch. | ||||
1941 | // TODO: Support similar logic of OR and EQ predicate? | ||||
1942 | if (Pred == ICmpInst::ICMP_NE) | ||||
1943 | if (auto *BO = dyn_cast<BinaryOperator>(Curr)) | ||||
1944 | if (BO->getOpcode() == Instruction::And) { | ||||
1945 | for (auto *BOU : BO->users()) | ||||
1946 | if (Visited.insert(BOU).second) | ||||
1947 | WorkList.push_back(BOU); | ||||
1948 | continue; | ||||
1949 | } | ||||
1950 | |||||
1951 | if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) { | ||||
1952 | assert(BI->isConditional() && "uses a comparison!")((BI->isConditional() && "uses a comparison!") ? static_cast <void> (0) : __assert_fail ("BI->isConditional() && \"uses a comparison!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 1952, __PRETTY_FUNCTION__)); | ||||
1953 | |||||
1954 | BasicBlock *NonNullSuccessor = | ||||
1955 | BI->getSuccessor(Pred == ICmpInst::ICMP_EQ ? 1 : 0); | ||||
1956 | BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor); | ||||
1957 | if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent())) | ||||
1958 | return true; | ||||
1959 | } else if (Pred == ICmpInst::ICMP_NE && isGuard(Curr) && | ||||
1960 | DT->dominates(cast<Instruction>(Curr), CtxI)) { | ||||
1961 | return true; | ||||
1962 | } | ||||
1963 | } | ||||
1964 | } | ||||
1965 | } | ||||
1966 | |||||
1967 | return false; | ||||
1968 | } | ||||
1969 | |||||
1970 | /// Does the 'Range' metadata (which must be a valid MD_range operand list) | ||||
1971 | /// ensure that the value it's attached to is never Value? 'RangeType' is | ||||
1972 | /// is the type of the value described by the range. | ||||
1973 | static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) { | ||||
1974 | const unsigned NumRanges = Ranges->getNumOperands() / 2; | ||||
1975 | assert(NumRanges >= 1)((NumRanges >= 1) ? static_cast<void> (0) : __assert_fail ("NumRanges >= 1", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 1975, __PRETTY_FUNCTION__)); | ||||
1976 | for (unsigned i = 0; i < NumRanges; ++i) { | ||||
1977 | ConstantInt *Lower = | ||||
1978 | mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0)); | ||||
1979 | ConstantInt *Upper = | ||||
1980 | mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1)); | ||||
1981 | ConstantRange Range(Lower->getValue(), Upper->getValue()); | ||||
1982 | if (Range.contains(Value)) | ||||
1983 | return false; | ||||
1984 | } | ||||
1985 | return true; | ||||
1986 | } | ||||
1987 | |||||
1988 | /// Return true if the given value is known to be non-zero when defined. For | ||||
1989 | /// vectors, return true if every element is known to be non-zero when | ||||
1990 | /// defined. For pointers, if the context instruction and dominator tree are | ||||
1991 | /// specified, perform context-sensitive analysis and return true if the | ||||
1992 | /// pointer couldn't possibly be null at the specified instruction. | ||||
1993 | /// Supports values with integer or pointer type and vectors of integers. | ||||
1994 | bool isKnownNonZero(const Value *V, unsigned Depth, const Query &Q) { | ||||
1995 | if (auto *C = dyn_cast<Constant>(V)) { | ||||
1996 | if (C->isNullValue()) | ||||
1997 | return false; | ||||
1998 | if (isa<ConstantInt>(C)) | ||||
1999 | // Must be non-zero due to null test above. | ||||
2000 | return true; | ||||
2001 | |||||
2002 | // For constant vectors, check that all elements are undefined or known | ||||
2003 | // non-zero to determine that the whole vector is known non-zero. | ||||
2004 | if (auto *VecTy = dyn_cast<VectorType>(C->getType())) { | ||||
2005 | for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) { | ||||
2006 | Constant *Elt = C->getAggregateElement(i); | ||||
2007 | if (!Elt || Elt->isNullValue()) | ||||
2008 | return false; | ||||
2009 | if (!isa<UndefValue>(Elt) && !isa<ConstantInt>(Elt)) | ||||
2010 | return false; | ||||
2011 | } | ||||
2012 | return true; | ||||
2013 | } | ||||
2014 | |||||
2015 | // A global variable in address space 0 is non null unless extern weak | ||||
2016 | // or an absolute symbol reference. Other address spaces may have null as a | ||||
2017 | // valid address for a global, so we can't assume anything. | ||||
2018 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) { | ||||
2019 | if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() && | ||||
2020 | GV->getType()->getAddressSpace() == 0) | ||||
2021 | return true; | ||||
2022 | } else | ||||
2023 | return false; | ||||
2024 | } | ||||
2025 | |||||
2026 | if (auto *I = dyn_cast<Instruction>(V)) { | ||||
2027 | if (MDNode *Ranges = Q.IIQ.getMetadata(I, LLVMContext::MD_range)) { | ||||
2028 | // If the possible ranges don't contain zero, then the value is | ||||
2029 | // definitely non-zero. | ||||
2030 | if (auto *Ty = dyn_cast<IntegerType>(V->getType())) { | ||||
2031 | const APInt ZeroValue(Ty->getBitWidth(), 0); | ||||
2032 | if (rangeMetadataExcludesValue(Ranges, ZeroValue)) | ||||
2033 | return true; | ||||
2034 | } | ||||
2035 | } | ||||
2036 | } | ||||
2037 | |||||
2038 | // Some of the tests below are recursive, so bail out if we hit the limit. | ||||
2039 | if (Depth++ >= MaxDepth) | ||||
2040 | return false; | ||||
2041 | |||||
2042 | // Check for pointer simplifications. | ||||
2043 | if (V->getType()->isPointerTy()) { | ||||
2044 | // Alloca never returns null, malloc might. | ||||
2045 | if (isa<AllocaInst>(V) && Q.DL.getAllocaAddrSpace() == 0) | ||||
2046 | return true; | ||||
2047 | |||||
2048 | // A byval, inalloca, or nonnull argument is never null. | ||||
2049 | if (const Argument *A = dyn_cast<Argument>(V)) | ||||
2050 | if (A->hasByValOrInAllocaAttr() || A->hasNonNullAttr()) | ||||
2051 | return true; | ||||
2052 | |||||
2053 | // A Load tagged with nonnull metadata is never null. | ||||
2054 | if (const LoadInst *LI = dyn_cast<LoadInst>(V)) | ||||
2055 | if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull)) | ||||
2056 | return true; | ||||
2057 | |||||
2058 | if (const auto *Call = dyn_cast<CallBase>(V)) { | ||||
2059 | if (Call->isReturnNonNull()) | ||||
2060 | return true; | ||||
2061 | if (const auto *RP = getArgumentAliasingToReturnedPointer(Call)) | ||||
2062 | return isKnownNonZero(RP, Depth, Q); | ||||
2063 | } | ||||
2064 | } | ||||
2065 | |||||
2066 | |||||
2067 | // Check for recursive pointer simplifications. | ||||
2068 | if (V->getType()->isPointerTy()) { | ||||
2069 | if (isKnownNonNullFromDominatingCondition(V, Q.CxtI, Q.DT)) | ||||
2070 | return true; | ||||
2071 | |||||
2072 | // Look through bitcast operations, GEPs, and int2ptr instructions as they | ||||
2073 | // do not alter the value, or at least not the nullness property of the | ||||
2074 | // value, e.g., int2ptr is allowed to zero/sign extend the value. | ||||
2075 | // | ||||
2076 | // Note that we have to take special care to avoid looking through | ||||
2077 | // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well | ||||
2078 | // as casts that can alter the value, e.g., AddrSpaceCasts. | ||||
2079 | if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) | ||||
2080 | if (isGEPKnownNonNull(GEP, Depth, Q)) | ||||
2081 | return true; | ||||
2082 | |||||
2083 | if (auto *BCO = dyn_cast<BitCastOperator>(V)) | ||||
2084 | return isKnownNonZero(BCO->getOperand(0), Depth, Q); | ||||
2085 | |||||
2086 | if (auto *I2P = dyn_cast<IntToPtrInst>(V)) | ||||
2087 | if (Q.DL.getTypeSizeInBits(I2P->getSrcTy()) <= | ||||
2088 | Q.DL.getTypeSizeInBits(I2P->getDestTy())) | ||||
2089 | return isKnownNonZero(I2P->getOperand(0), Depth, Q); | ||||
2090 | } | ||||
2091 | |||||
2092 | // Similar to int2ptr above, we can look through ptr2int here if the cast | ||||
2093 | // is a no-op or an extend and not a truncate. | ||||
2094 | if (auto *P2I = dyn_cast<PtrToIntInst>(V)) | ||||
2095 | if (Q.DL.getTypeSizeInBits(P2I->getSrcTy()) <= | ||||
2096 | Q.DL.getTypeSizeInBits(P2I->getDestTy())) | ||||
2097 | return isKnownNonZero(P2I->getOperand(0), Depth, Q); | ||||
2098 | |||||
2099 | unsigned BitWidth = getBitWidth(V->getType()->getScalarType(), Q.DL); | ||||
2100 | |||||
2101 | // X | Y != 0 if X != 0 or Y != 0. | ||||
2102 | Value *X = nullptr, *Y = nullptr; | ||||
2103 | if (match(V, m_Or(m_Value(X), m_Value(Y)))) | ||||
2104 | return isKnownNonZero(X, Depth, Q) || isKnownNonZero(Y, Depth, Q); | ||||
2105 | |||||
2106 | // ext X != 0 if X != 0. | ||||
2107 | if (isa<SExtInst>(V) || isa<ZExtInst>(V)) | ||||
2108 | return isKnownNonZero(cast<Instruction>(V)->getOperand(0), Depth, Q); | ||||
2109 | |||||
2110 | // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined | ||||
2111 | // if the lowest bit is shifted off the end. | ||||
2112 | if (match(V, m_Shl(m_Value(X), m_Value(Y)))) { | ||||
2113 | // shl nuw can't remove any non-zero bits. | ||||
2114 | const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V); | ||||
2115 | if (Q.IIQ.hasNoUnsignedWrap(BO)) | ||||
2116 | return isKnownNonZero(X, Depth, Q); | ||||
2117 | |||||
2118 | KnownBits Known(BitWidth); | ||||
2119 | computeKnownBits(X, Known, Depth, Q); | ||||
2120 | if (Known.One[0]) | ||||
2121 | return true; | ||||
2122 | } | ||||
2123 | // shr X, Y != 0 if X is negative. Note that the value of the shift is not | ||||
2124 | // defined if the sign bit is shifted off the end. | ||||
2125 | else if (match(V, m_Shr(m_Value(X), m_Value(Y)))) { | ||||
2126 | // shr exact can only shift out zero bits. | ||||
2127 | const PossiblyExactOperator *BO = cast<PossiblyExactOperator>(V); | ||||
2128 | if (BO->isExact()) | ||||
2129 | return isKnownNonZero(X, Depth, Q); | ||||
2130 | |||||
2131 | KnownBits Known = computeKnownBits(X, Depth, Q); | ||||
2132 | if (Known.isNegative()) | ||||
2133 | return true; | ||||
2134 | |||||
2135 | // If the shifter operand is a constant, and all of the bits shifted | ||||
2136 | // out are known to be zero, and X is known non-zero then at least one | ||||
2137 | // non-zero bit must remain. | ||||
2138 | if (ConstantInt *Shift = dyn_cast<ConstantInt>(Y)) { | ||||
2139 | auto ShiftVal = Shift->getLimitedValue(BitWidth - 1); | ||||
2140 | // Is there a known one in the portion not shifted out? | ||||
2141 | if (Known.countMaxLeadingZeros() < BitWidth - ShiftVal) | ||||
2142 | return true; | ||||
2143 | // Are all the bits to be shifted out known zero? | ||||
2144 | if (Known.countMinTrailingZeros() >= ShiftVal) | ||||
2145 | return isKnownNonZero(X, Depth, Q); | ||||
2146 | } | ||||
2147 | } | ||||
2148 | // div exact can only produce a zero if the dividend is zero. | ||||
2149 | else if (match(V, m_Exact(m_IDiv(m_Value(X), m_Value())))) { | ||||
2150 | return isKnownNonZero(X, Depth, Q); | ||||
2151 | } | ||||
2152 | // X + Y. | ||||
2153 | else if (match(V, m_Add(m_Value(X), m_Value(Y)))) { | ||||
2154 | KnownBits XKnown = computeKnownBits(X, Depth, Q); | ||||
2155 | KnownBits YKnown = computeKnownBits(Y, Depth, Q); | ||||
2156 | |||||
2157 | // If X and Y are both non-negative (as signed values) then their sum is not | ||||
2158 | // zero unless both X and Y are zero. | ||||
2159 | if (XKnown.isNonNegative() && YKnown.isNonNegative()) | ||||
2160 | if (isKnownNonZero(X, Depth, Q) || isKnownNonZero(Y, Depth, Q)) | ||||
2161 | return true; | ||||
2162 | |||||
2163 | // If X and Y are both negative (as signed values) then their sum is not | ||||
2164 | // zero unless both X and Y equal INT_MIN. | ||||
2165 | if (XKnown.isNegative() && YKnown.isNegative()) { | ||||
2166 | APInt Mask = APInt::getSignedMaxValue(BitWidth); | ||||
2167 | // The sign bit of X is set. If some other bit is set then X is not equal | ||||
2168 | // to INT_MIN. | ||||
2169 | if (XKnown.One.intersects(Mask)) | ||||
2170 | return true; | ||||
2171 | // The sign bit of Y is set. If some other bit is set then Y is not equal | ||||
2172 | // to INT_MIN. | ||||
2173 | if (YKnown.One.intersects(Mask)) | ||||
2174 | return true; | ||||
2175 | } | ||||
2176 | |||||
2177 | // The sum of a non-negative number and a power of two is not zero. | ||||
2178 | if (XKnown.isNonNegative() && | ||||
2179 | isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q)) | ||||
2180 | return true; | ||||
2181 | if (YKnown.isNonNegative() && | ||||
2182 | isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q)) | ||||
2183 | return true; | ||||
2184 | } | ||||
2185 | // X * Y. | ||||
2186 | else if (match(V, m_Mul(m_Value(X), m_Value(Y)))) { | ||||
2187 | const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V); | ||||
2188 | // If X and Y are non-zero then so is X * Y as long as the multiplication | ||||
2189 | // does not overflow. | ||||
2190 | if ((Q.IIQ.hasNoSignedWrap(BO) || Q.IIQ.hasNoUnsignedWrap(BO)) && | ||||
2191 | isKnownNonZero(X, Depth, Q) && isKnownNonZero(Y, Depth, Q)) | ||||
2192 | return true; | ||||
2193 | } | ||||
2194 | // (C ? X : Y) != 0 if X != 0 and Y != 0. | ||||
2195 | else if (const SelectInst *SI = dyn_cast<SelectInst>(V)) { | ||||
2196 | if (isKnownNonZero(SI->getTrueValue(), Depth, Q) && | ||||
2197 | isKnownNonZero(SI->getFalseValue(), Depth, Q)) | ||||
2198 | return true; | ||||
2199 | } | ||||
2200 | // PHI | ||||
2201 | else if (const PHINode *PN = dyn_cast<PHINode>(V)) { | ||||
2202 | // Try and detect a recurrence that monotonically increases from a | ||||
2203 | // starting value, as these are common as induction variables. | ||||
2204 | if (PN->getNumIncomingValues() == 2) { | ||||
2205 | Value *Start = PN->getIncomingValue(0); | ||||
2206 | Value *Induction = PN->getIncomingValue(1); | ||||
2207 | if (isa<ConstantInt>(Induction) && !isa<ConstantInt>(Start)) | ||||
2208 | std::swap(Start, Induction); | ||||
2209 | if (ConstantInt *C = dyn_cast<ConstantInt>(Start)) { | ||||
2210 | if (!C->isZero() && !C->isNegative()) { | ||||
2211 | ConstantInt *X; | ||||
2212 | if (Q.IIQ.UseInstrInfo && | ||||
2213 | (match(Induction, m_NSWAdd(m_Specific(PN), m_ConstantInt(X))) || | ||||
2214 | match(Induction, m_NUWAdd(m_Specific(PN), m_ConstantInt(X)))) && | ||||
2215 | !X->isNegative()) | ||||
2216 | return true; | ||||
2217 | } | ||||
2218 | } | ||||
2219 | } | ||||
2220 | // Check if all incoming values are non-zero constant. | ||||
2221 | bool AllNonZeroConstants = llvm::all_of(PN->operands(), [](Value *V) { | ||||
2222 | return isa<ConstantInt>(V) && !cast<ConstantInt>(V)->isZero(); | ||||
2223 | }); | ||||
2224 | if (AllNonZeroConstants) | ||||
2225 | return true; | ||||
2226 | } | ||||
2227 | |||||
2228 | KnownBits Known(BitWidth); | ||||
2229 | computeKnownBits(V, Known, Depth, Q); | ||||
2230 | return Known.One != 0; | ||||
2231 | } | ||||
2232 | |||||
2233 | /// Return true if V2 == V1 + X, where X is known non-zero. | ||||
2234 | static bool isAddOfNonZero(const Value *V1, const Value *V2, const Query &Q) { | ||||
2235 | const BinaryOperator *BO = dyn_cast<BinaryOperator>(V1); | ||||
2236 | if (!BO || BO->getOpcode() != Instruction::Add) | ||||
2237 | return false; | ||||
2238 | Value *Op = nullptr; | ||||
2239 | if (V2 == BO->getOperand(0)) | ||||
2240 | Op = BO->getOperand(1); | ||||
2241 | else if (V2 == BO->getOperand(1)) | ||||
2242 | Op = BO->getOperand(0); | ||||
2243 | else | ||||
2244 | return false; | ||||
2245 | return isKnownNonZero(Op, 0, Q); | ||||
2246 | } | ||||
2247 | |||||
2248 | /// Return true if it is known that V1 != V2. | ||||
2249 | static bool isKnownNonEqual(const Value *V1, const Value *V2, const Query &Q) { | ||||
2250 | if (V1 == V2) | ||||
2251 | return false; | ||||
2252 | if (V1->getType() != V2->getType()) | ||||
2253 | // We can't look through casts yet. | ||||
2254 | return false; | ||||
2255 | if (isAddOfNonZero(V1, V2, Q) || isAddOfNonZero(V2, V1, Q)) | ||||
2256 | return true; | ||||
2257 | |||||
2258 | if (V1->getType()->isIntOrIntVectorTy()) { | ||||
2259 | // Are any known bits in V1 contradictory to known bits in V2? If V1 | ||||
2260 | // has a known zero where V2 has a known one, they must not be equal. | ||||
2261 | KnownBits Known1 = computeKnownBits(V1, 0, Q); | ||||
2262 | KnownBits Known2 = computeKnownBits(V2, 0, Q); | ||||
2263 | |||||
2264 | if (Known1.Zero.intersects(Known2.One) || | ||||
2265 | Known2.Zero.intersects(Known1.One)) | ||||
2266 | return true; | ||||
2267 | } | ||||
2268 | return false; | ||||
2269 | } | ||||
2270 | |||||
2271 | /// Return true if 'V & Mask' is known to be zero. We use this predicate to | ||||
2272 | /// simplify operations downstream. Mask is known to be zero for bits that V | ||||
2273 | /// cannot have. | ||||
2274 | /// | ||||
2275 | /// This function is defined on values with integer type, values with pointer | ||||
2276 | /// type, and vectors of integers. In the case | ||||
2277 | /// where V is a vector, the mask, known zero, and known one values are the | ||||
2278 | /// same width as the vector element, and the bit is set only if it is true | ||||
2279 | /// for all of the elements in the vector. | ||||
2280 | bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth, | ||||
2281 | const Query &Q) { | ||||
2282 | KnownBits Known(Mask.getBitWidth()); | ||||
2283 | computeKnownBits(V, Known, Depth, Q); | ||||
2284 | return Mask.isSubsetOf(Known.Zero); | ||||
2285 | } | ||||
2286 | |||||
2287 | // Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow). | ||||
2288 | // Returns the input and lower/upper bounds. | ||||
2289 | static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, | ||||
2290 | const APInt *&CLow, const APInt *&CHigh) { | ||||
2291 | assert(isa<Operator>(Select) &&((isa<Operator>(Select) && cast<Operator> (Select)->getOpcode() == Instruction::Select && "Input should be a Select!" ) ? static_cast<void> (0) : __assert_fail ("isa<Operator>(Select) && cast<Operator>(Select)->getOpcode() == Instruction::Select && \"Input should be a Select!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 2293, __PRETTY_FUNCTION__)) | ||||
2292 | cast<Operator>(Select)->getOpcode() == Instruction::Select &&((isa<Operator>(Select) && cast<Operator> (Select)->getOpcode() == Instruction::Select && "Input should be a Select!" ) ? static_cast<void> (0) : __assert_fail ("isa<Operator>(Select) && cast<Operator>(Select)->getOpcode() == Instruction::Select && \"Input should be a Select!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 2293, __PRETTY_FUNCTION__)) | ||||
2293 | "Input should be a Select!")((isa<Operator>(Select) && cast<Operator> (Select)->getOpcode() == Instruction::Select && "Input should be a Select!" ) ? static_cast<void> (0) : __assert_fail ("isa<Operator>(Select) && cast<Operator>(Select)->getOpcode() == Instruction::Select && \"Input should be a Select!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 2293, __PRETTY_FUNCTION__)); | ||||
2294 | |||||
2295 | const Value *LHS, *RHS, *LHS2, *RHS2; | ||||
2296 | SelectPatternFlavor SPF = matchSelectPattern(Select, LHS, RHS).Flavor; | ||||
2297 | if (SPF != SPF_SMAX && SPF != SPF_SMIN) | ||||
2298 | return false; | ||||
2299 | |||||
2300 | if (!match(RHS, m_APInt(CLow))) | ||||
2301 | return false; | ||||
2302 | |||||
2303 | SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor; | ||||
2304 | if (getInverseMinMaxFlavor(SPF) != SPF2) | ||||
2305 | return false; | ||||
2306 | |||||
2307 | if (!match(RHS2, m_APInt(CHigh))) | ||||
2308 | return false; | ||||
2309 | |||||
2310 | if (SPF == SPF_SMIN) | ||||
2311 | std::swap(CLow, CHigh); | ||||
2312 | |||||
2313 | In = LHS2; | ||||
2314 | return CLow->sle(*CHigh); | ||||
2315 | } | ||||
2316 | |||||
2317 | /// For vector constants, loop over the elements and find the constant with the | ||||
2318 | /// minimum number of sign bits. Return 0 if the value is not a vector constant | ||||
2319 | /// or if any element was not analyzed; otherwise, return the count for the | ||||
2320 | /// element with the minimum number of sign bits. | ||||
2321 | static unsigned computeNumSignBitsVectorConstant(const Value *V, | ||||
2322 | unsigned TyBits) { | ||||
2323 | const auto *CV = dyn_cast<Constant>(V); | ||||
2324 | if (!CV || !CV->getType()->isVectorTy()) | ||||
2325 | return 0; | ||||
2326 | |||||
2327 | unsigned MinSignBits = TyBits; | ||||
2328 | unsigned NumElts = CV->getType()->getVectorNumElements(); | ||||
2329 | for (unsigned i = 0; i != NumElts; ++i) { | ||||
2330 | // If we find a non-ConstantInt, bail out. | ||||
2331 | auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i)); | ||||
2332 | if (!Elt) | ||||
2333 | return 0; | ||||
2334 | |||||
2335 | MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits()); | ||||
2336 | } | ||||
2337 | |||||
2338 | return MinSignBits; | ||||
2339 | } | ||||
2340 | |||||
2341 | static unsigned ComputeNumSignBitsImpl(const Value *V, unsigned Depth, | ||||
2342 | const Query &Q); | ||||
2343 | |||||
2344 | static unsigned ComputeNumSignBits(const Value *V, unsigned Depth, | ||||
2345 | const Query &Q) { | ||||
2346 | unsigned Result = ComputeNumSignBitsImpl(V, Depth, Q); | ||||
2347 | assert(Result > 0 && "At least one sign bit needs to be present!")((Result > 0 && "At least one sign bit needs to be present!" ) ? static_cast<void> (0) : __assert_fail ("Result > 0 && \"At least one sign bit needs to be present!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 2347, __PRETTY_FUNCTION__)); | ||||
2348 | return Result; | ||||
2349 | } | ||||
2350 | |||||
2351 | /// Return the number of times the sign bit of the register is replicated into | ||||
2352 | /// the other bits. We know that at least 1 bit is always equal to the sign bit | ||||
2353 | /// (itself), but other cases can give us information. For example, immediately | ||||
2354 | /// after an "ashr X, 2", we know that the top 3 bits are all equal to each | ||||
2355 | /// other, so we return 3. For vectors, return the number of sign bits for the | ||||
2356 | /// vector element with the minimum number of known sign bits. | ||||
2357 | static unsigned ComputeNumSignBitsImpl(const Value *V, unsigned Depth, | ||||
2358 | const Query &Q) { | ||||
2359 | assert(Depth <= MaxDepth && "Limit Search Depth")((Depth <= MaxDepth && "Limit Search Depth") ? static_cast <void> (0) : __assert_fail ("Depth <= MaxDepth && \"Limit Search Depth\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 2359, __PRETTY_FUNCTION__)); | ||||
2360 | |||||
2361 | // We return the minimum number of sign bits that are guaranteed to be present | ||||
2362 | // in V, so for undef we have to conservatively return 1. We don't have the | ||||
2363 | // same behavior for poison though -- that's a FIXME today. | ||||
2364 | |||||
2365 | Type *ScalarTy = V->getType()->getScalarType(); | ||||
2366 | unsigned TyBits = ScalarTy->isPointerTy() ? | ||||
2367 | Q.DL.getIndexTypeSizeInBits(ScalarTy) : | ||||
2368 | Q.DL.getTypeSizeInBits(ScalarTy); | ||||
2369 | |||||
2370 | unsigned Tmp, Tmp2; | ||||
2371 | unsigned FirstAnswer = 1; | ||||
2372 | |||||
2373 | // Note that ConstantInt is handled by the general computeKnownBits case | ||||
2374 | // below. | ||||
2375 | |||||
2376 | if (Depth == MaxDepth) | ||||
2377 | return 1; // Limit search depth. | ||||
2378 | |||||
2379 | const Operator *U = dyn_cast<Operator>(V); | ||||
2380 | switch (Operator::getOpcode(V)) { | ||||
2381 | default: break; | ||||
2382 | case Instruction::SExt: | ||||
2383 | Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits(); | ||||
2384 | return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q) + Tmp; | ||||
2385 | |||||
2386 | case Instruction::SDiv: { | ||||
2387 | const APInt *Denominator; | ||||
2388 | // sdiv X, C -> adds log(C) sign bits. | ||||
2389 | if (match(U->getOperand(1), m_APInt(Denominator))) { | ||||
2390 | |||||
2391 | // Ignore non-positive denominator. | ||||
2392 | if (!Denominator->isStrictlyPositive()) | ||||
2393 | break; | ||||
2394 | |||||
2395 | // Calculate the incoming numerator bits. | ||||
2396 | unsigned NumBits = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); | ||||
2397 | |||||
2398 | // Add floor(log(C)) bits to the numerator bits. | ||||
2399 | return std::min(TyBits, NumBits + Denominator->logBase2()); | ||||
2400 | } | ||||
2401 | break; | ||||
2402 | } | ||||
2403 | |||||
2404 | case Instruction::SRem: { | ||||
2405 | const APInt *Denominator; | ||||
2406 | // srem X, C -> we know that the result is within [-C+1,C) when C is a | ||||
2407 | // positive constant. This let us put a lower bound on the number of sign | ||||
2408 | // bits. | ||||
2409 | if (match(U->getOperand(1), m_APInt(Denominator))) { | ||||
2410 | |||||
2411 | // Ignore non-positive denominator. | ||||
2412 | if (!Denominator->isStrictlyPositive()) | ||||
2413 | break; | ||||
2414 | |||||
2415 | // Calculate the incoming numerator bits. SRem by a positive constant | ||||
2416 | // can't lower the number of sign bits. | ||||
2417 | unsigned NumrBits = | ||||
2418 | ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); | ||||
2419 | |||||
2420 | // Calculate the leading sign bit constraints by examining the | ||||
2421 | // denominator. Given that the denominator is positive, there are two | ||||
2422 | // cases: | ||||
2423 | // | ||||
2424 | // 1. the numerator is positive. The result range is [0,C) and [0,C) u< | ||||
2425 | // (1 << ceilLogBase2(C)). | ||||
2426 | // | ||||
2427 | // 2. the numerator is negative. Then the result range is (-C,0] and | ||||
2428 | // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)). | ||||
2429 | // | ||||
2430 | // Thus a lower bound on the number of sign bits is `TyBits - | ||||
2431 | // ceilLogBase2(C)`. | ||||
2432 | |||||
2433 | unsigned ResBits = TyBits - Denominator->ceilLogBase2(); | ||||
2434 | return std::max(NumrBits, ResBits); | ||||
2435 | } | ||||
2436 | break; | ||||
2437 | } | ||||
2438 | |||||
2439 | case Instruction::AShr: { | ||||
2440 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); | ||||
2441 | // ashr X, C -> adds C sign bits. Vectors too. | ||||
2442 | const APInt *ShAmt; | ||||
2443 | if (match(U->getOperand(1), m_APInt(ShAmt))) { | ||||
2444 | if (ShAmt->uge(TyBits)) | ||||
2445 | break; // Bad shift. | ||||
2446 | unsigned ShAmtLimited = ShAmt->getZExtValue(); | ||||
2447 | Tmp += ShAmtLimited; | ||||
2448 | if (Tmp > TyBits) Tmp = TyBits; | ||||
2449 | } | ||||
2450 | return Tmp; | ||||
2451 | } | ||||
2452 | case Instruction::Shl: { | ||||
2453 | const APInt *ShAmt; | ||||
2454 | if (match(U->getOperand(1), m_APInt(ShAmt))) { | ||||
2455 | // shl destroys sign bits. | ||||
2456 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); | ||||
2457 | if (ShAmt->uge(TyBits) || // Bad shift. | ||||
2458 | ShAmt->uge(Tmp)) break; // Shifted all sign bits out. | ||||
2459 | Tmp2 = ShAmt->getZExtValue(); | ||||
2460 | return Tmp - Tmp2; | ||||
2461 | } | ||||
2462 | break; | ||||
2463 | } | ||||
2464 | case Instruction::And: | ||||
2465 | case Instruction::Or: | ||||
2466 | case Instruction::Xor: // NOT is handled here. | ||||
2467 | // Logical binary ops preserve the number of sign bits at the worst. | ||||
2468 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); | ||||
2469 | if (Tmp != 1) { | ||||
2470 | Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); | ||||
2471 | FirstAnswer = std::min(Tmp, Tmp2); | ||||
2472 | // We computed what we know about the sign bits as our first | ||||
2473 | // answer. Now proceed to the generic code that uses | ||||
2474 | // computeKnownBits, and pick whichever answer is better. | ||||
2475 | } | ||||
2476 | break; | ||||
2477 | |||||
2478 | case Instruction::Select: { | ||||
2479 | // If we have a clamp pattern, we know that the number of sign bits will be | ||||
2480 | // the minimum of the clamp min/max range. | ||||
2481 | const Value *X; | ||||
2482 | const APInt *CLow, *CHigh; | ||||
2483 | if (isSignedMinMaxClamp(U, X, CLow, CHigh)) | ||||
2484 | return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits()); | ||||
2485 | |||||
2486 | Tmp = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); | ||||
2487 | if (Tmp == 1) break; | ||||
2488 | Tmp2 = ComputeNumSignBits(U->getOperand(2), Depth + 1, Q); | ||||
2489 | return std::min(Tmp, Tmp2); | ||||
2490 | } | ||||
2491 | |||||
2492 | case Instruction::Add: | ||||
2493 | // Add can have at most one carry bit. Thus we know that the output | ||||
2494 | // is, at worst, one more bit than the inputs. | ||||
2495 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); | ||||
2496 | if (Tmp == 1) break; | ||||
2497 | |||||
2498 | // Special case decrementing a value (ADD X, -1): | ||||
2499 | if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1))) | ||||
2500 | if (CRHS->isAllOnesValue()) { | ||||
2501 | KnownBits Known(TyBits); | ||||
2502 | computeKnownBits(U->getOperand(0), Known, Depth + 1, Q); | ||||
2503 | |||||
2504 | // If the input is known to be 0 or 1, the output is 0/-1, which is all | ||||
2505 | // sign bits set. | ||||
2506 | if ((Known.Zero | 1).isAllOnesValue()) | ||||
2507 | return TyBits; | ||||
2508 | |||||
2509 | // If we are subtracting one from a positive number, there is no carry | ||||
2510 | // out of the result. | ||||
2511 | if (Known.isNonNegative()) | ||||
2512 | return Tmp; | ||||
2513 | } | ||||
2514 | |||||
2515 | Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); | ||||
2516 | if (Tmp2 == 1) break; | ||||
2517 | return std::min(Tmp, Tmp2)-1; | ||||
2518 | |||||
2519 | case Instruction::Sub: | ||||
2520 | Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); | ||||
2521 | if (Tmp2 == 1) break; | ||||
2522 | |||||
2523 | // Handle NEG. | ||||
2524 | if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0))) | ||||
2525 | if (CLHS->isNullValue()) { | ||||
2526 | KnownBits Known(TyBits); | ||||
2527 | computeKnownBits(U->getOperand(1), Known, Depth + 1, Q); | ||||
2528 | // If the input is known to be 0 or 1, the output is 0/-1, which is all | ||||
2529 | // sign bits set. | ||||
2530 | if ((Known.Zero | 1).isAllOnesValue()) | ||||
2531 | return TyBits; | ||||
2532 | |||||
2533 | // If the input is known to be positive (the sign bit is known clear), | ||||
2534 | // the output of the NEG has the same number of sign bits as the input. | ||||
2535 | if (Known.isNonNegative()) | ||||
2536 | return Tmp2; | ||||
2537 | |||||
2538 | // Otherwise, we treat this like a SUB. | ||||
2539 | } | ||||
2540 | |||||
2541 | // Sub can have at most one carry bit. Thus we know that the output | ||||
2542 | // is, at worst, one more bit than the inputs. | ||||
2543 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); | ||||
2544 | if (Tmp == 1) break; | ||||
2545 | return std::min(Tmp, Tmp2)-1; | ||||
2546 | |||||
2547 | case Instruction::Mul: { | ||||
2548 | // The output of the Mul can be at most twice the valid bits in the inputs. | ||||
2549 | unsigned SignBitsOp0 = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); | ||||
2550 | if (SignBitsOp0 == 1) break; | ||||
2551 | unsigned SignBitsOp1 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); | ||||
2552 | if (SignBitsOp1 == 1) break; | ||||
2553 | unsigned OutValidBits = | ||||
2554 | (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1); | ||||
2555 | return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1; | ||||
2556 | } | ||||
2557 | |||||
2558 | case Instruction::PHI: { | ||||
2559 | const PHINode *PN = cast<PHINode>(U); | ||||
2560 | unsigned NumIncomingValues = PN->getNumIncomingValues(); | ||||
2561 | // Don't analyze large in-degree PHIs. | ||||
2562 | if (NumIncomingValues > 4) break; | ||||
2563 | // Unreachable blocks may have zero-operand PHI nodes. | ||||
2564 | if (NumIncomingValues == 0) break; | ||||
2565 | |||||
2566 | // Take the minimum of all incoming values. This can't infinitely loop | ||||
2567 | // because of our depth threshold. | ||||
2568 | Tmp = ComputeNumSignBits(PN->getIncomingValue(0), Depth + 1, Q); | ||||
2569 | for (unsigned i = 1, e = NumIncomingValues; i != e; ++i) { | ||||
2570 | if (Tmp == 1) return Tmp; | ||||
2571 | Tmp = std::min( | ||||
2572 | Tmp, ComputeNumSignBits(PN->getIncomingValue(i), Depth + 1, Q)); | ||||
2573 | } | ||||
2574 | return Tmp; | ||||
2575 | } | ||||
2576 | |||||
2577 | case Instruction::Trunc: | ||||
2578 | // FIXME: it's tricky to do anything useful for this, but it is an important | ||||
2579 | // case for targets like X86. | ||||
2580 | break; | ||||
2581 | |||||
2582 | case Instruction::ExtractElement: | ||||
2583 | // Look through extract element. At the moment we keep this simple and skip | ||||
2584 | // tracking the specific element. But at least we might find information | ||||
2585 | // valid for all elements of the vector (for example if vector is sign | ||||
2586 | // extended, shifted, etc). | ||||
2587 | return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); | ||||
2588 | |||||
2589 | case Instruction::ShuffleVector: { | ||||
2590 | // TODO: This is copied almost directly from the SelectionDAG version of | ||||
2591 | // ComputeNumSignBits. It would be better if we could share common | ||||
2592 | // code. If not, make sure that changes are translated to the DAG. | ||||
2593 | |||||
2594 | // Collect the minimum number of sign bits that are shared by every vector | ||||
2595 | // element referenced by the shuffle. | ||||
2596 | auto *Shuf = cast<ShuffleVectorInst>(U); | ||||
2597 | int NumElts = Shuf->getOperand(0)->getType()->getVectorNumElements(); | ||||
2598 | int NumMaskElts = Shuf->getMask()->getType()->getVectorNumElements(); | ||||
2599 | APInt DemandedLHS(NumElts, 0), DemandedRHS(NumElts, 0); | ||||
2600 | for (int i = 0; i != NumMaskElts; ++i) { | ||||
2601 | int M = Shuf->getMaskValue(i); | ||||
2602 | assert(M < NumElts * 2 && "Invalid shuffle mask constant")((M < NumElts * 2 && "Invalid shuffle mask constant" ) ? static_cast<void> (0) : __assert_fail ("M < NumElts * 2 && \"Invalid shuffle mask constant\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 2602, __PRETTY_FUNCTION__)); | ||||
2603 | // For undef elements, we don't know anything about the common state of | ||||
2604 | // the shuffle result. | ||||
2605 | if (M == -1) | ||||
2606 | return 1; | ||||
2607 | if (M < NumElts) | ||||
2608 | DemandedLHS.setBit(M % NumElts); | ||||
2609 | else | ||||
2610 | DemandedRHS.setBit(M % NumElts); | ||||
2611 | } | ||||
2612 | Tmp = std::numeric_limits<unsigned>::max(); | ||||
2613 | if (!!DemandedLHS) | ||||
2614 | Tmp = ComputeNumSignBits(Shuf->getOperand(0), Depth + 1, Q); | ||||
2615 | if (!!DemandedRHS) { | ||||
2616 | Tmp2 = ComputeNumSignBits(Shuf->getOperand(1), Depth + 1, Q); | ||||
2617 | Tmp = std::min(Tmp, Tmp2); | ||||
2618 | } | ||||
2619 | // If we don't know anything, early out and try computeKnownBits fall-back. | ||||
2620 | if (Tmp == 1) | ||||
2621 | break; | ||||
2622 | assert(Tmp <= V->getType()->getScalarSizeInBits() &&((Tmp <= V->getType()->getScalarSizeInBits() && "Failed to determine minimum sign bits") ? static_cast<void > (0) : __assert_fail ("Tmp <= V->getType()->getScalarSizeInBits() && \"Failed to determine minimum sign bits\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 2623, __PRETTY_FUNCTION__)) | ||||
2623 | "Failed to determine minimum sign bits")((Tmp <= V->getType()->getScalarSizeInBits() && "Failed to determine minimum sign bits") ? static_cast<void > (0) : __assert_fail ("Tmp <= V->getType()->getScalarSizeInBits() && \"Failed to determine minimum sign bits\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 2623, __PRETTY_FUNCTION__)); | ||||
2624 | return Tmp; | ||||
2625 | } | ||||
2626 | } | ||||
2627 | |||||
2628 | // Finally, if we can prove that the top bits of the result are 0's or 1's, | ||||
2629 | // use this information. | ||||
2630 | |||||
2631 | // If we can examine all elements of a vector constant successfully, we're | ||||
2632 | // done (we can't do any better than that). If not, keep trying. | ||||
2633 | if (unsigned VecSignBits = computeNumSignBitsVectorConstant(V, TyBits)) | ||||
2634 | return VecSignBits; | ||||
2635 | |||||
2636 | KnownBits Known(TyBits); | ||||
2637 | computeKnownBits(V, Known, Depth, Q); | ||||
2638 | |||||
2639 | // If we know that the sign bit is either zero or one, determine the number of | ||||
2640 | // identical bits in the top of the input value. | ||||
2641 | return std::max(FirstAnswer, Known.countMinSignBits()); | ||||
2642 | } | ||||
2643 | |||||
2644 | /// This function computes the integer multiple of Base that equals V. | ||||
2645 | /// If successful, it returns true and returns the multiple in | ||||
2646 | /// Multiple. If unsuccessful, it returns false. It looks | ||||
2647 | /// through SExt instructions only if LookThroughSExt is true. | ||||
2648 | bool llvm::ComputeMultiple(Value *V, unsigned Base, Value *&Multiple, | ||||
2649 | bool LookThroughSExt, unsigned Depth) { | ||||
2650 | const unsigned MaxDepth = 6; | ||||
2651 | |||||
2652 | assert(V && "No Value?")((V && "No Value?") ? static_cast<void> (0) : __assert_fail ("V && \"No Value?\"", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 2652, __PRETTY_FUNCTION__)); | ||||
2653 | assert(Depth <= MaxDepth && "Limit Search Depth")((Depth <= MaxDepth && "Limit Search Depth") ? static_cast <void> (0) : __assert_fail ("Depth <= MaxDepth && \"Limit Search Depth\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 2653, __PRETTY_FUNCTION__)); | ||||
2654 | assert(V->getType()->isIntegerTy() && "Not integer or pointer type!")((V->getType()->isIntegerTy() && "Not integer or pointer type!" ) ? static_cast<void> (0) : __assert_fail ("V->getType()->isIntegerTy() && \"Not integer or pointer type!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 2654, __PRETTY_FUNCTION__)); | ||||
2655 | |||||
2656 | Type *T = V->getType(); | ||||
2657 | |||||
2658 | ConstantInt *CI = dyn_cast<ConstantInt>(V); | ||||
2659 | |||||
2660 | if (Base == 0) | ||||
2661 | return false; | ||||
2662 | |||||
2663 | if (Base == 1) { | ||||
2664 | Multiple = V; | ||||
2665 | return true; | ||||
2666 | } | ||||
2667 | |||||
2668 | ConstantExpr *CO = dyn_cast<ConstantExpr>(V); | ||||
2669 | Constant *BaseVal = ConstantInt::get(T, Base); | ||||
2670 | if (CO && CO == BaseVal) { | ||||
2671 | // Multiple is 1. | ||||
2672 | Multiple = ConstantInt::get(T, 1); | ||||
2673 | return true; | ||||
2674 | } | ||||
2675 | |||||
2676 | if (CI && CI->getZExtValue() % Base == 0) { | ||||
2677 | Multiple = ConstantInt::get(T, CI->getZExtValue() / Base); | ||||
2678 | return true; | ||||
2679 | } | ||||
2680 | |||||
2681 | if (Depth == MaxDepth) return false; // Limit search depth. | ||||
2682 | |||||
2683 | Operator *I = dyn_cast<Operator>(V); | ||||
2684 | if (!I) return false; | ||||
2685 | |||||
2686 | switch (I->getOpcode()) { | ||||
2687 | default: break; | ||||
2688 | case Instruction::SExt: | ||||
2689 | if (!LookThroughSExt) return false; | ||||
2690 | // otherwise fall through to ZExt | ||||
2691 | LLVM_FALLTHROUGH[[clang::fallthrough]]; | ||||
2692 | case Instruction::ZExt: | ||||
2693 | return ComputeMultiple(I->getOperand(0), Base, Multiple, | ||||
2694 | LookThroughSExt, Depth+1); | ||||
2695 | case Instruction::Shl: | ||||
2696 | case Instruction::Mul: { | ||||
2697 | Value *Op0 = I->getOperand(0); | ||||
2698 | Value *Op1 = I->getOperand(1); | ||||
2699 | |||||
2700 | if (I->getOpcode() == Instruction::Shl) { | ||||
2701 | ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1); | ||||
2702 | if (!Op1CI) return false; | ||||
2703 | // Turn Op0 << Op1 into Op0 * 2^Op1 | ||||
2704 | APInt Op1Int = Op1CI->getValue(); | ||||
2705 | uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1); | ||||
2706 | APInt API(Op1Int.getBitWidth(), 0); | ||||
2707 | API.setBit(BitToSet); | ||||
2708 | Op1 = ConstantInt::get(V->getContext(), API); | ||||
2709 | } | ||||
2710 | |||||
2711 | Value *Mul0 = nullptr; | ||||
2712 | if (ComputeMultiple(Op0, Base, Mul0, LookThroughSExt, Depth+1)) { | ||||
2713 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) | ||||
2714 | if (Constant *MulC = dyn_cast<Constant>(Mul0)) { | ||||
2715 | if (Op1C->getType()->getPrimitiveSizeInBits() < | ||||
2716 | MulC->getType()->getPrimitiveSizeInBits()) | ||||
2717 | Op1C = ConstantExpr::getZExt(Op1C, MulC->getType()); | ||||
2718 | if (Op1C->getType()->getPrimitiveSizeInBits() > | ||||
2719 | MulC->getType()->getPrimitiveSizeInBits()) | ||||
2720 | MulC = ConstantExpr::getZExt(MulC, Op1C->getType()); | ||||
2721 | |||||
2722 | // V == Base * (Mul0 * Op1), so return (Mul0 * Op1) | ||||
2723 | Multiple = ConstantExpr::getMul(MulC, Op1C); | ||||
2724 | return true; | ||||
2725 | } | ||||
2726 | |||||
2727 | if (ConstantInt *Mul0CI = dyn_cast<ConstantInt>(Mul0)) | ||||
2728 | if (Mul0CI->getValue() == 1) { | ||||
2729 | // V == Base * Op1, so return Op1 | ||||
2730 | Multiple = Op1; | ||||
2731 | return true; | ||||
2732 | } | ||||
2733 | } | ||||
2734 | |||||
2735 | Value *Mul1 = nullptr; | ||||
2736 | if (ComputeMultiple(Op1, Base, Mul1, LookThroughSExt, Depth+1)) { | ||||
2737 | if (Constant *Op0C = dyn_cast<Constant>(Op0)) | ||||
2738 | if (Constant *MulC = dyn_cast<Constant>(Mul1)) { | ||||
2739 | if (Op0C->getType()->getPrimitiveSizeInBits() < | ||||
2740 | MulC->getType()->getPrimitiveSizeInBits()) | ||||
2741 | Op0C = ConstantExpr::getZExt(Op0C, MulC->getType()); | ||||
2742 | if (Op0C->getType()->getPrimitiveSizeInBits() > | ||||
2743 | MulC->getType()->getPrimitiveSizeInBits()) | ||||
2744 | MulC = ConstantExpr::getZExt(MulC, Op0C->getType()); | ||||
2745 | |||||
2746 | // V == Base * (Mul1 * Op0), so return (Mul1 * Op0) | ||||
2747 | Multiple = ConstantExpr::getMul(MulC, Op0C); | ||||
2748 | return true; | ||||
2749 | } | ||||
2750 | |||||
2751 | if (ConstantInt *Mul1CI = dyn_cast<ConstantInt>(Mul1)) | ||||
2752 | if (Mul1CI->getValue() == 1) { | ||||
2753 | // V == Base * Op0, so return Op0 | ||||
2754 | Multiple = Op0; | ||||
2755 | return true; | ||||
2756 | } | ||||
2757 | } | ||||
2758 | } | ||||
2759 | } | ||||
2760 | |||||
2761 | // We could not determine if V is a multiple of Base. | ||||
2762 | return false; | ||||
2763 | } | ||||
2764 | |||||
2765 | Intrinsic::ID llvm::getIntrinsicForCallSite(ImmutableCallSite ICS, | ||||
2766 | const TargetLibraryInfo *TLI) { | ||||
2767 | const Function *F = ICS.getCalledFunction(); | ||||
2768 | if (!F) | ||||
2769 | return Intrinsic::not_intrinsic; | ||||
2770 | |||||
2771 | if (F->isIntrinsic()) | ||||
2772 | return F->getIntrinsicID(); | ||||
2773 | |||||
2774 | if (!TLI) | ||||
2775 | return Intrinsic::not_intrinsic; | ||||
2776 | |||||
2777 | LibFunc Func; | ||||
2778 | // We're going to make assumptions on the semantics of the functions, check | ||||
2779 | // that the target knows that it's available in this environment and it does | ||||
2780 | // not have local linkage. | ||||
2781 | if (!F || F->hasLocalLinkage() || !TLI->getLibFunc(*F, Func)) | ||||
2782 | return Intrinsic::not_intrinsic; | ||||
2783 | |||||
2784 | if (!ICS.onlyReadsMemory()) | ||||
2785 | return Intrinsic::not_intrinsic; | ||||
2786 | |||||
2787 | // Otherwise check if we have a call to a function that can be turned into a | ||||
2788 | // vector intrinsic. | ||||
2789 | switch (Func) { | ||||
2790 | default: | ||||
2791 | break; | ||||
2792 | case LibFunc_sin: | ||||
2793 | case LibFunc_sinf: | ||||
2794 | case LibFunc_sinl: | ||||
2795 | return Intrinsic::sin; | ||||
2796 | case LibFunc_cos: | ||||
2797 | case LibFunc_cosf: | ||||
2798 | case LibFunc_cosl: | ||||
2799 | return Intrinsic::cos; | ||||
2800 | case LibFunc_exp: | ||||
2801 | case LibFunc_expf: | ||||
2802 | case LibFunc_expl: | ||||
2803 | return Intrinsic::exp; | ||||
2804 | case LibFunc_exp2: | ||||
2805 | case LibFunc_exp2f: | ||||
2806 | case LibFunc_exp2l: | ||||
2807 | return Intrinsic::exp2; | ||||
2808 | case LibFunc_log: | ||||
2809 | case LibFunc_logf: | ||||
2810 | case LibFunc_logl: | ||||
2811 | return Intrinsic::log; | ||||
2812 | case LibFunc_log10: | ||||
2813 | case LibFunc_log10f: | ||||
2814 | case LibFunc_log10l: | ||||
2815 | return Intrinsic::log10; | ||||
2816 | case LibFunc_log2: | ||||
2817 | case LibFunc_log2f: | ||||
2818 | case LibFunc_log2l: | ||||
2819 | return Intrinsic::log2; | ||||
2820 | case LibFunc_fabs: | ||||
2821 | case LibFunc_fabsf: | ||||
2822 | case LibFunc_fabsl: | ||||
2823 | return Intrinsic::fabs; | ||||
2824 | case LibFunc_fmin: | ||||
2825 | case LibFunc_fminf: | ||||
2826 | case LibFunc_fminl: | ||||
2827 | return Intrinsic::minnum; | ||||
2828 | case LibFunc_fmax: | ||||
2829 | case LibFunc_fmaxf: | ||||
2830 | case LibFunc_fmaxl: | ||||
2831 | return Intrinsic::maxnum; | ||||
2832 | case LibFunc_copysign: | ||||
2833 | case LibFunc_copysignf: | ||||
2834 | case LibFunc_copysignl: | ||||
2835 | return Intrinsic::copysign; | ||||
2836 | case LibFunc_floor: | ||||
2837 | case LibFunc_floorf: | ||||
2838 | case LibFunc_floorl: | ||||
2839 | return Intrinsic::floor; | ||||
2840 | case LibFunc_ceil: | ||||
2841 | case LibFunc_ceilf: | ||||
2842 | case LibFunc_ceill: | ||||
2843 | return Intrinsic::ceil; | ||||
2844 | case LibFunc_trunc: | ||||
2845 | case LibFunc_truncf: | ||||
2846 | case LibFunc_truncl: | ||||
2847 | return Intrinsic::trunc; | ||||
2848 | case LibFunc_rint: | ||||
2849 | case LibFunc_rintf: | ||||
2850 | case LibFunc_rintl: | ||||
2851 | return Intrinsic::rint; | ||||
2852 | case LibFunc_nearbyint: | ||||
2853 | case LibFunc_nearbyintf: | ||||
2854 | case LibFunc_nearbyintl: | ||||
2855 | return Intrinsic::nearbyint; | ||||
2856 | case LibFunc_round: | ||||
2857 | case LibFunc_roundf: | ||||
2858 | case LibFunc_roundl: | ||||
2859 | return Intrinsic::round; | ||||
2860 | case LibFunc_pow: | ||||
2861 | case LibFunc_powf: | ||||
2862 | case LibFunc_powl: | ||||
2863 | return Intrinsic::pow; | ||||
2864 | case LibFunc_sqrt: | ||||
2865 | case LibFunc_sqrtf: | ||||
2866 | case LibFunc_sqrtl: | ||||
2867 | return Intrinsic::sqrt; | ||||
2868 | } | ||||
2869 | |||||
2870 | return Intrinsic::not_intrinsic; | ||||
2871 | } | ||||
2872 | |||||
2873 | /// Return true if we can prove that the specified FP value is never equal to | ||||
2874 | /// -0.0. | ||||
2875 | /// | ||||
2876 | /// NOTE: this function will need to be revisited when we support non-default | ||||
2877 | /// rounding modes! | ||||
2878 | bool llvm::CannotBeNegativeZero(const Value *V, const TargetLibraryInfo *TLI, | ||||
2879 | unsigned Depth) { | ||||
2880 | if (auto *CFP = dyn_cast<ConstantFP>(V)) | ||||
2881 | return !CFP->getValueAPF().isNegZero(); | ||||
2882 | |||||
2883 | // Limit search depth. | ||||
2884 | if (Depth == MaxDepth) | ||||
2885 | return false; | ||||
2886 | |||||
2887 | auto *Op = dyn_cast<Operator>(V); | ||||
2888 | if (!Op) | ||||
2889 | return false; | ||||
2890 | |||||
2891 | // Check if the nsz fast-math flag is set. | ||||
2892 | if (auto *FPO = dyn_cast<FPMathOperator>(Op)) | ||||
2893 | if (FPO->hasNoSignedZeros()) | ||||
2894 | return true; | ||||
2895 | |||||
2896 | // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0. | ||||
2897 | if (match(Op, m_FAdd(m_Value(), m_PosZeroFP()))) | ||||
2898 | return true; | ||||
2899 | |||||
2900 | // sitofp and uitofp turn into +0.0 for zero. | ||||
2901 | if (isa<SIToFPInst>(Op) || isa<UIToFPInst>(Op)) | ||||
2902 | return true; | ||||
2903 | |||||
2904 | if (auto *Call = dyn_cast<CallInst>(Op)) { | ||||
2905 | Intrinsic::ID IID = getIntrinsicForCallSite(Call, TLI); | ||||
2906 | switch (IID) { | ||||
2907 | default: | ||||
2908 | break; | ||||
2909 | // sqrt(-0.0) = -0.0, no other negative results are possible. | ||||
2910 | case Intrinsic::sqrt: | ||||
2911 | case Intrinsic::canonicalize: | ||||
2912 | return CannotBeNegativeZero(Call->getArgOperand(0), TLI, Depth + 1); | ||||
2913 | // fabs(x) != -0.0 | ||||
2914 | case Intrinsic::fabs: | ||||
2915 | return true; | ||||
2916 | } | ||||
2917 | } | ||||
2918 | |||||
2919 | return false; | ||||
2920 | } | ||||
2921 | |||||
2922 | /// If \p SignBitOnly is true, test for a known 0 sign bit rather than a | ||||
2923 | /// standard ordered compare. e.g. make -0.0 olt 0.0 be true because of the sign | ||||
2924 | /// bit despite comparing equal. | ||||
2925 | static bool cannotBeOrderedLessThanZeroImpl(const Value *V, | ||||
2926 | const TargetLibraryInfo *TLI, | ||||
2927 | bool SignBitOnly, | ||||
2928 | unsigned Depth) { | ||||
2929 | // TODO: This function does not do the right thing when SignBitOnly is true | ||||
2930 | // and we're lowering to a hypothetical IEEE 754-compliant-but-evil platform | ||||
2931 | // which flips the sign bits of NaNs. See | ||||
2932 | // https://llvm.org/bugs/show_bug.cgi?id=31702. | ||||
2933 | |||||
2934 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { | ||||
2935 | return !CFP->getValueAPF().isNegative() || | ||||
2936 | (!SignBitOnly && CFP->getValueAPF().isZero()); | ||||
2937 | } | ||||
2938 | |||||
2939 | // Handle vector of constants. | ||||
2940 | if (auto *CV = dyn_cast<Constant>(V)) { | ||||
2941 | if (CV->getType()->isVectorTy()) { | ||||
2942 | unsigned NumElts = CV->getType()->getVectorNumElements(); | ||||
2943 | for (unsigned i = 0; i != NumElts; ++i) { | ||||
2944 | auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i)); | ||||
2945 | if (!CFP) | ||||
2946 | return false; | ||||
2947 | if (CFP->getValueAPF().isNegative() && | ||||
2948 | (SignBitOnly || !CFP->getValueAPF().isZero())) | ||||
2949 | return false; | ||||
2950 | } | ||||
2951 | |||||
2952 | // All non-negative ConstantFPs. | ||||
2953 | return true; | ||||
2954 | } | ||||
2955 | } | ||||
2956 | |||||
2957 | if (Depth == MaxDepth) | ||||
2958 | return false; // Limit search depth. | ||||
2959 | |||||
2960 | const Operator *I = dyn_cast<Operator>(V); | ||||
2961 | if (!I) | ||||
2962 | return false; | ||||
2963 | |||||
2964 | switch (I->getOpcode()) { | ||||
2965 | default: | ||||
2966 | break; | ||||
2967 | // Unsigned integers are always nonnegative. | ||||
2968 | case Instruction::UIToFP: | ||||
2969 | return true; | ||||
2970 | case Instruction::FMul: | ||||
2971 | // x*x is always non-negative or a NaN. | ||||
2972 | if (I->getOperand(0) == I->getOperand(1) && | ||||
2973 | (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs())) | ||||
2974 | return true; | ||||
2975 | |||||
2976 | LLVM_FALLTHROUGH[[clang::fallthrough]]; | ||||
2977 | case Instruction::FAdd: | ||||
2978 | case Instruction::FDiv: | ||||
2979 | case Instruction::FRem: | ||||
2980 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, | ||||
2981 | Depth + 1) && | ||||
2982 | cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly, | ||||
2983 | Depth + 1); | ||||
2984 | case Instruction::Select: | ||||
2985 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly, | ||||
2986 | Depth + 1) && | ||||
2987 | cannotBeOrderedLessThanZeroImpl(I->getOperand(2), TLI, SignBitOnly, | ||||
2988 | Depth + 1); | ||||
2989 | case Instruction::FPExt: | ||||
2990 | case Instruction::FPTrunc: | ||||
2991 | // Widening/narrowing never change sign. | ||||
2992 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, | ||||
2993 | Depth + 1); | ||||
2994 | case Instruction::ExtractElement: | ||||
2995 | // Look through extract element. At the moment we keep this simple and skip | ||||
2996 | // tracking the specific element. But at least we might find information | ||||
2997 | // valid for all elements of the vector. | ||||
2998 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, | ||||
2999 | Depth + 1); | ||||
3000 | case Instruction::Call: | ||||
3001 | const auto *CI = cast<CallInst>(I); | ||||
3002 | Intrinsic::ID IID = getIntrinsicForCallSite(CI, TLI); | ||||
3003 | switch (IID) { | ||||
3004 | default: | ||||
3005 | break; | ||||
3006 | case Intrinsic::maxnum: | ||||
3007 | return (isKnownNeverNaN(I->getOperand(0), TLI) && | ||||
3008 | cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, | ||||
3009 | SignBitOnly, Depth + 1)) || | ||||
3010 | (isKnownNeverNaN(I->getOperand(1), TLI) && | ||||
3011 | cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, | ||||
3012 | SignBitOnly, Depth + 1)); | ||||
3013 | |||||
3014 | case Intrinsic::maximum: | ||||
3015 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, | ||||
3016 | Depth + 1) || | ||||
3017 | cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly, | ||||
3018 | Depth + 1); | ||||
3019 | case Intrinsic::minnum: | ||||
3020 | case Intrinsic::minimum: | ||||
3021 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, | ||||
3022 | Depth + 1) && | ||||
3023 | cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly, | ||||
3024 | Depth + 1); | ||||
3025 | case Intrinsic::exp: | ||||
3026 | case Intrinsic::exp2: | ||||
3027 | case Intrinsic::fabs: | ||||
3028 | return true; | ||||
3029 | |||||
3030 | case Intrinsic::sqrt: | ||||
3031 | // sqrt(x) is always >= -0 or NaN. Moreover, sqrt(x) == -0 iff x == -0. | ||||
3032 | if (!SignBitOnly) | ||||
3033 | return true; | ||||
3034 | return CI->hasNoNaNs() && (CI->hasNoSignedZeros() || | ||||
3035 | CannotBeNegativeZero(CI->getOperand(0), TLI)); | ||||
3036 | |||||
3037 | case Intrinsic::powi: | ||||
3038 | if (ConstantInt *Exponent = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
3039 | // powi(x,n) is non-negative if n is even. | ||||
3040 | if (Exponent->getBitWidth() <= 64 && Exponent->getSExtValue() % 2u == 0) | ||||
3041 | return true; | ||||
3042 | } | ||||
3043 | // TODO: This is not correct. Given that exp is an integer, here are the | ||||
3044 | // ways that pow can return a negative value: | ||||
3045 | // | ||||
3046 | // pow(x, exp) --> negative if exp is odd and x is negative. | ||||
3047 | // pow(-0, exp) --> -inf if exp is negative odd. | ||||
3048 | // pow(-0, exp) --> -0 if exp is positive odd. | ||||
3049 | // pow(-inf, exp) --> -0 if exp is negative odd. | ||||
3050 | // pow(-inf, exp) --> -inf if exp is positive odd. | ||||
3051 | // | ||||
3052 | // Therefore, if !SignBitOnly, we can return true if x >= +0 or x is NaN, | ||||
3053 | // but we must return false if x == -0. Unfortunately we do not currently | ||||
3054 | // have a way of expressing this constraint. See details in | ||||
3055 | // https://llvm.org/bugs/show_bug.cgi?id=31702. | ||||
3056 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, | ||||
3057 | Depth + 1); | ||||
3058 | |||||
3059 | case Intrinsic::fma: | ||||
3060 | case Intrinsic::fmuladd: | ||||
3061 | // x*x+y is non-negative if y is non-negative. | ||||
3062 | return I->getOperand(0) == I->getOperand(1) && | ||||
3063 | (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs()) && | ||||
3064 | cannotBeOrderedLessThanZeroImpl(I->getOperand(2), TLI, SignBitOnly, | ||||
3065 | Depth + 1); | ||||
3066 | } | ||||
3067 | break; | ||||
3068 | } | ||||
3069 | return false; | ||||
3070 | } | ||||
3071 | |||||
3072 | bool llvm::CannotBeOrderedLessThanZero(const Value *V, | ||||
3073 | const TargetLibraryInfo *TLI) { | ||||
3074 | return cannotBeOrderedLessThanZeroImpl(V, TLI, false, 0); | ||||
3075 | } | ||||
3076 | |||||
3077 | bool llvm::SignBitMustBeZero(const Value *V, const TargetLibraryInfo *TLI) { | ||||
3078 | return cannotBeOrderedLessThanZeroImpl(V, TLI, true, 0); | ||||
3079 | } | ||||
3080 | |||||
3081 | bool llvm::isKnownNeverNaN(const Value *V, const TargetLibraryInfo *TLI, | ||||
3082 | unsigned Depth) { | ||||
3083 | assert(V->getType()->isFPOrFPVectorTy() && "Querying for NaN on non-FP type")((V->getType()->isFPOrFPVectorTy() && "Querying for NaN on non-FP type" ) ? static_cast<void> (0) : __assert_fail ("V->getType()->isFPOrFPVectorTy() && \"Querying for NaN on non-FP type\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 3083, __PRETTY_FUNCTION__)); | ||||
3084 | |||||
3085 | // If we're told that NaNs won't happen, assume they won't. | ||||
3086 | if (auto *FPMathOp = dyn_cast<FPMathOperator>(V)) | ||||
3087 | if (FPMathOp->hasNoNaNs()) | ||||
3088 | return true; | ||||
3089 | |||||
3090 | // Handle scalar constants. | ||||
3091 | if (auto *CFP = dyn_cast<ConstantFP>(V)) | ||||
3092 | return !CFP->isNaN(); | ||||
3093 | |||||
3094 | if (Depth == MaxDepth) | ||||
3095 | return false; | ||||
3096 | |||||
3097 | if (auto *Inst = dyn_cast<Instruction>(V)) { | ||||
3098 | switch (Inst->getOpcode()) { | ||||
3099 | case Instruction::FAdd: | ||||
3100 | case Instruction::FMul: | ||||
3101 | case Instruction::FSub: | ||||
3102 | case Instruction::FDiv: | ||||
3103 | case Instruction::FRem: { | ||||
3104 | // TODO: Need isKnownNeverInfinity | ||||
3105 | return false; | ||||
3106 | } | ||||
3107 | case Instruction::Select: { | ||||
3108 | return isKnownNeverNaN(Inst->getOperand(1), TLI, Depth + 1) && | ||||
3109 | isKnownNeverNaN(Inst->getOperand(2), TLI, Depth + 1); | ||||
3110 | } | ||||
3111 | case Instruction::SIToFP: | ||||
3112 | case Instruction::UIToFP: | ||||
3113 | return true; | ||||
3114 | case Instruction::FPTrunc: | ||||
3115 | case Instruction::FPExt: | ||||
3116 | return isKnownNeverNaN(Inst->getOperand(0), TLI, Depth + 1); | ||||
3117 | default: | ||||
3118 | break; | ||||
3119 | } | ||||
3120 | } | ||||
3121 | |||||
3122 | if (const auto *II = dyn_cast<IntrinsicInst>(V)) { | ||||
3123 | switch (II->getIntrinsicID()) { | ||||
3124 | case Intrinsic::canonicalize: | ||||
3125 | case Intrinsic::fabs: | ||||
3126 | case Intrinsic::copysign: | ||||
3127 | case Intrinsic::exp: | ||||
3128 | case Intrinsic::exp2: | ||||
3129 | case Intrinsic::floor: | ||||
3130 | case Intrinsic::ceil: | ||||
3131 | case Intrinsic::trunc: | ||||
3132 | case Intrinsic::rint: | ||||
3133 | case Intrinsic::nearbyint: | ||||
3134 | case Intrinsic::round: | ||||
3135 | return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1); | ||||
3136 | case Intrinsic::sqrt: | ||||
3137 | return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1) && | ||||
3138 | CannotBeOrderedLessThanZero(II->getArgOperand(0), TLI); | ||||
3139 | case Intrinsic::minnum: | ||||
3140 | case Intrinsic::maxnum: | ||||
3141 | // If either operand is not NaN, the result is not NaN. | ||||
3142 | return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1) || | ||||
3143 | isKnownNeverNaN(II->getArgOperand(1), TLI, Depth + 1); | ||||
3144 | default: | ||||
3145 | return false; | ||||
3146 | } | ||||
3147 | } | ||||
3148 | |||||
3149 | // Bail out for constant expressions, but try to handle vector constants. | ||||
3150 | if (!V->getType()->isVectorTy() || !isa<Constant>(V)) | ||||
3151 | return false; | ||||
3152 | |||||
3153 | // For vectors, verify that each element is not NaN. | ||||
3154 | unsigned NumElts = V->getType()->getVectorNumElements(); | ||||
3155 | for (unsigned i = 0; i != NumElts; ++i) { | ||||
3156 | Constant *Elt = cast<Constant>(V)->getAggregateElement(i); | ||||
3157 | if (!Elt) | ||||
3158 | return false; | ||||
3159 | if (isa<UndefValue>(Elt)) | ||||
3160 | continue; | ||||
3161 | auto *CElt = dyn_cast<ConstantFP>(Elt); | ||||
3162 | if (!CElt || CElt->isNaN()) | ||||
3163 | return false; | ||||
3164 | } | ||||
3165 | // All elements were confirmed not-NaN or undefined. | ||||
3166 | return true; | ||||
3167 | } | ||||
3168 | |||||
3169 | Value *llvm::isBytewiseValue(Value *V) { | ||||
3170 | |||||
3171 | // All byte-wide stores are splatable, even of arbitrary variables. | ||||
3172 | if (V->getType()->isIntegerTy(8)) | ||||
3173 | return V; | ||||
3174 | |||||
3175 | LLVMContext &Ctx = V->getContext(); | ||||
3176 | |||||
3177 | // Undef don't care. | ||||
3178 | auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx)); | ||||
3179 | if (isa<UndefValue>(V)) | ||||
3180 | return UndefInt8; | ||||
3181 | |||||
3182 | Constant *C = dyn_cast<Constant>(V); | ||||
3183 | if (!C) { | ||||
3184 | // Conceptually, we could handle things like: | ||||
3185 | // %a = zext i8 %X to i16 | ||||
3186 | // %b = shl i16 %a, 8 | ||||
3187 | // %c = or i16 %a, %b | ||||
3188 | // but until there is an example that actually needs this, it doesn't seem | ||||
3189 | // worth worrying about. | ||||
3190 | return nullptr; | ||||
3191 | } | ||||
3192 | |||||
3193 | // Handle 'null' ConstantArrayZero etc. | ||||
3194 | if (C->isNullValue()) | ||||
3195 | return Constant::getNullValue(Type::getInt8Ty(Ctx)); | ||||
3196 | |||||
3197 | // Constant floating-point values can be handled as integer values if the | ||||
3198 | // corresponding integer value is "byteable". An important case is 0.0. | ||||
3199 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { | ||||
3200 | Type *Ty = nullptr; | ||||
3201 | if (CFP->getType()->isHalfTy()) | ||||
3202 | Ty = Type::getInt16Ty(Ctx); | ||||
3203 | else if (CFP->getType()->isFloatTy()) | ||||
3204 | Ty = Type::getInt32Ty(Ctx); | ||||
3205 | else if (CFP->getType()->isDoubleTy()) | ||||
3206 | Ty = Type::getInt64Ty(Ctx); | ||||
3207 | // Don't handle long double formats, which have strange constraints. | ||||
3208 | return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty)) : nullptr; | ||||
3209 | } | ||||
3210 | |||||
3211 | // We can handle constant integers that are multiple of 8 bits. | ||||
3212 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) { | ||||
3213 | if (CI->getBitWidth() % 8 == 0) { | ||||
3214 | assert(CI->getBitWidth() > 8 && "8 bits should be handled above!")((CI->getBitWidth() > 8 && "8 bits should be handled above!" ) ? static_cast<void> (0) : __assert_fail ("CI->getBitWidth() > 8 && \"8 bits should be handled above!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 3214, __PRETTY_FUNCTION__)); | ||||
3215 | if (!CI->getValue().isSplat(8)) | ||||
3216 | return nullptr; | ||||
3217 | return ConstantInt::get(Ctx, CI->getValue().trunc(8)); | ||||
3218 | } | ||||
3219 | } | ||||
3220 | |||||
3221 | auto Merge = [&](Value *LHS, Value *RHS) -> Value * { | ||||
3222 | if (LHS == RHS) | ||||
3223 | return LHS; | ||||
3224 | if (!LHS || !RHS) | ||||
3225 | return nullptr; | ||||
3226 | if (LHS == UndefInt8) | ||||
3227 | return RHS; | ||||
3228 | if (RHS == UndefInt8) | ||||
3229 | return LHS; | ||||
3230 | return nullptr; | ||||
3231 | }; | ||||
3232 | |||||
3233 | if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) { | ||||
3234 | Value *Val = UndefInt8; | ||||
3235 | for (unsigned I = 0, E = CA->getNumElements(); I != E; ++I) | ||||
3236 | if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I))))) | ||||
3237 | return nullptr; | ||||
3238 | return Val; | ||||
3239 | } | ||||
3240 | |||||
3241 | if (isa<ConstantVector>(C)) { | ||||
3242 | Constant *Splat = cast<ConstantVector>(C)->getSplatValue(); | ||||
3243 | return Splat ? isBytewiseValue(Splat) : nullptr; | ||||
3244 | } | ||||
3245 | |||||
3246 | if (isa<ConstantArray>(C) || isa<ConstantStruct>(C)) { | ||||
3247 | Value *Val = UndefInt8; | ||||
3248 | for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I) | ||||
3249 | if (!(Val = Merge(Val, isBytewiseValue(C->getOperand(I))))) | ||||
3250 | return nullptr; | ||||
3251 | return Val; | ||||
3252 | } | ||||
3253 | |||||
3254 | // Don't try to handle the handful of other constants. | ||||
3255 | return nullptr; | ||||
3256 | } | ||||
3257 | |||||
3258 | // This is the recursive version of BuildSubAggregate. It takes a few different | ||||
3259 | // arguments. Idxs is the index within the nested struct From that we are | ||||
3260 | // looking at now (which is of type IndexedType). IdxSkip is the number of | ||||
3261 | // indices from Idxs that should be left out when inserting into the resulting | ||||
3262 | // struct. To is the result struct built so far, new insertvalue instructions | ||||
3263 | // build on that. | ||||
3264 | static Value *BuildSubAggregate(Value *From, Value* To, Type *IndexedType, | ||||
3265 | SmallVectorImpl<unsigned> &Idxs, | ||||
3266 | unsigned IdxSkip, | ||||
3267 | Instruction *InsertBefore) { | ||||
3268 | StructType *STy = dyn_cast<StructType>(IndexedType); | ||||
3269 | if (STy) { | ||||
3270 | // Save the original To argument so we can modify it | ||||
3271 | Value *OrigTo = To; | ||||
3272 | // General case, the type indexed by Idxs is a struct | ||||
3273 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { | ||||
3274 | // Process each struct element recursively | ||||
3275 | Idxs.push_back(i); | ||||
3276 | Value *PrevTo = To; | ||||
3277 | To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip, | ||||
3278 | InsertBefore); | ||||
3279 | Idxs.pop_back(); | ||||
3280 | if (!To) { | ||||
3281 | // Couldn't find any inserted value for this index? Cleanup | ||||
3282 | while (PrevTo != OrigTo) { | ||||
3283 | InsertValueInst* Del = cast<InsertValueInst>(PrevTo); | ||||
3284 | PrevTo = Del->getAggregateOperand(); | ||||
3285 | Del->eraseFromParent(); | ||||
3286 | } | ||||
3287 | // Stop processing elements | ||||
3288 | break; | ||||
3289 | } | ||||
3290 | } | ||||
3291 | // If we successfully found a value for each of our subaggregates | ||||
3292 | if (To) | ||||
3293 | return To; | ||||
3294 | } | ||||
3295 | // Base case, the type indexed by SourceIdxs is not a struct, or not all of | ||||
3296 | // the struct's elements had a value that was inserted directly. In the latter | ||||
3297 | // case, perhaps we can't determine each of the subelements individually, but | ||||
3298 | // we might be able to find the complete struct somewhere. | ||||
3299 | |||||
3300 | // Find the value that is at that particular spot | ||||
3301 | Value *V = FindInsertedValue(From, Idxs); | ||||
3302 | |||||
3303 | if (!V) | ||||
3304 | return nullptr; | ||||
3305 | |||||
3306 | // Insert the value in the new (sub) aggregate | ||||
3307 | return InsertValueInst::Create(To, V, makeArrayRef(Idxs).slice(IdxSkip), | ||||
3308 | "tmp", InsertBefore); | ||||
3309 | } | ||||
3310 | |||||
3311 | // This helper takes a nested struct and extracts a part of it (which is again a | ||||
3312 | // struct) into a new value. For example, given the struct: | ||||
3313 | // { a, { b, { c, d }, e } } | ||||
3314 | // and the indices "1, 1" this returns | ||||
3315 | // { c, d }. | ||||
3316 | // | ||||
3317 | // It does this by inserting an insertvalue for each element in the resulting | ||||
3318 | // struct, as opposed to just inserting a single struct. This will only work if | ||||
3319 | // each of the elements of the substruct are known (ie, inserted into From by an | ||||
3320 | // insertvalue instruction somewhere). | ||||
3321 | // | ||||
3322 | // All inserted insertvalue instructions are inserted before InsertBefore | ||||
3323 | static Value *BuildSubAggregate(Value *From, ArrayRef<unsigned> idx_range, | ||||
3324 | Instruction *InsertBefore) { | ||||
3325 | assert(InsertBefore && "Must have someplace to insert!")((InsertBefore && "Must have someplace to insert!") ? static_cast<void> (0) : __assert_fail ("InsertBefore && \"Must have someplace to insert!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 3325, __PRETTY_FUNCTION__)); | ||||
3326 | Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(), | ||||
3327 | idx_range); | ||||
3328 | Value *To = UndefValue::get(IndexedType); | ||||
3329 | SmallVector<unsigned, 10> Idxs(idx_range.begin(), idx_range.end()); | ||||
3330 | unsigned IdxSkip = Idxs.size(); | ||||
3331 | |||||
3332 | return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore); | ||||
3333 | } | ||||
3334 | |||||
3335 | /// Given an aggregate and a sequence of indices, see if the scalar value | ||||
3336 | /// indexed is already around as a register, for example if it was inserted | ||||
3337 | /// directly into the aggregate. | ||||
3338 | /// | ||||
3339 | /// If InsertBefore is not null, this function will duplicate (modified) | ||||
3340 | /// insertvalues when a part of a nested struct is extracted. | ||||
3341 | Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range, | ||||
3342 | Instruction *InsertBefore) { | ||||
3343 | // Nothing to index? Just return V then (this is useful at the end of our | ||||
3344 | // recursion). | ||||
3345 | if (idx_range.empty()) | ||||
3346 | return V; | ||||
3347 | // We have indices, so V should have an indexable type. | ||||
3348 | assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&(((V->getType()->isStructTy() || V->getType()->isArrayTy ()) && "Not looking at a struct or array?") ? static_cast <void> (0) : __assert_fail ("(V->getType()->isStructTy() || V->getType()->isArrayTy()) && \"Not looking at a struct or array?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 3349, __PRETTY_FUNCTION__)) | ||||
3349 | "Not looking at a struct or array?")(((V->getType()->isStructTy() || V->getType()->isArrayTy ()) && "Not looking at a struct or array?") ? static_cast <void> (0) : __assert_fail ("(V->getType()->isStructTy() || V->getType()->isArrayTy()) && \"Not looking at a struct or array?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 3349, __PRETTY_FUNCTION__)); | ||||
3350 | assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&((ExtractValueInst::getIndexedType(V->getType(), idx_range ) && "Invalid indices for type?") ? static_cast<void > (0) : __assert_fail ("ExtractValueInst::getIndexedType(V->getType(), idx_range) && \"Invalid indices for type?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 3351, __PRETTY_FUNCTION__)) | ||||
3351 | "Invalid indices for type?")((ExtractValueInst::getIndexedType(V->getType(), idx_range ) && "Invalid indices for type?") ? static_cast<void > (0) : __assert_fail ("ExtractValueInst::getIndexedType(V->getType(), idx_range) && \"Invalid indices for type?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 3351, __PRETTY_FUNCTION__)); | ||||
3352 | |||||
3353 | if (Constant *C = dyn_cast<Constant>(V)) { | ||||
3354 | C = C->getAggregateElement(idx_range[0]); | ||||
3355 | if (!C) return nullptr; | ||||
3356 | return FindInsertedValue(C, idx_range.slice(1), InsertBefore); | ||||
3357 | } | ||||
3358 | |||||
3359 | if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) { | ||||
3360 | // Loop the indices for the insertvalue instruction in parallel with the | ||||
3361 | // requested indices | ||||
3362 | const unsigned *req_idx = idx_range.begin(); | ||||
3363 | for (const unsigned *i = I->idx_begin(), *e = I->idx_end(); | ||||
3364 | i != e; ++i, ++req_idx) { | ||||
3365 | if (req_idx == idx_range.end()) { | ||||
3366 | // We can't handle this without inserting insertvalues | ||||
3367 | if (!InsertBefore) | ||||
3368 | return nullptr; | ||||
3369 | |||||
3370 | // The requested index identifies a part of a nested aggregate. Handle | ||||
3371 | // this specially. For example, | ||||
3372 | // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0 | ||||
3373 | // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1 | ||||
3374 | // %C = extractvalue {i32, { i32, i32 } } %B, 1 | ||||
3375 | // This can be changed into | ||||
3376 | // %A = insertvalue {i32, i32 } undef, i32 10, 0 | ||||
3377 | // %C = insertvalue {i32, i32 } %A, i32 11, 1 | ||||
3378 | // which allows the unused 0,0 element from the nested struct to be | ||||
3379 | // removed. | ||||
3380 | return BuildSubAggregate(V, makeArrayRef(idx_range.begin(), req_idx), | ||||
3381 | InsertBefore); | ||||
3382 | } | ||||
3383 | |||||
3384 | // This insert value inserts something else than what we are looking for. | ||||
3385 | // See if the (aggregate) value inserted into has the value we are | ||||
3386 | // looking for, then. | ||||
3387 | if (*req_idx != *i) | ||||
3388 | return FindInsertedValue(I->getAggregateOperand(), idx_range, | ||||
3389 | InsertBefore); | ||||
3390 | } | ||||
3391 | // If we end up here, the indices of the insertvalue match with those | ||||
3392 | // requested (though possibly only partially). Now we recursively look at | ||||
3393 | // the inserted value, passing any remaining indices. | ||||
3394 | return FindInsertedValue(I->getInsertedValueOperand(), | ||||
3395 | makeArrayRef(req_idx, idx_range.end()), | ||||
3396 | InsertBefore); | ||||
3397 | } | ||||
3398 | |||||
3399 | if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) { | ||||
3400 | // If we're extracting a value from an aggregate that was extracted from | ||||
3401 | // something else, we can extract from that something else directly instead. | ||||
3402 | // However, we will need to chain I's indices with the requested indices. | ||||
3403 | |||||
3404 | // Calculate the number of indices required | ||||
3405 | unsigned size = I->getNumIndices() + idx_range.size(); | ||||
3406 | // Allocate some space to put the new indices in | ||||
3407 | SmallVector<unsigned, 5> Idxs; | ||||
3408 | Idxs.reserve(size); | ||||
3409 | // Add indices from the extract value instruction | ||||
3410 | Idxs.append(I->idx_begin(), I->idx_end()); | ||||
3411 | |||||
3412 | // Add requested indices | ||||
3413 | Idxs.append(idx_range.begin(), idx_range.end()); | ||||
3414 | |||||
3415 | assert(Idxs.size() == size((Idxs.size() == size && "Number of indices added not correct?" ) ? static_cast<void> (0) : __assert_fail ("Idxs.size() == size && \"Number of indices added not correct?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 3416, __PRETTY_FUNCTION__)) | ||||
3416 | && "Number of indices added not correct?")((Idxs.size() == size && "Number of indices added not correct?" ) ? static_cast<void> (0) : __assert_fail ("Idxs.size() == size && \"Number of indices added not correct?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 3416, __PRETTY_FUNCTION__)); | ||||
3417 | |||||
3418 | return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore); | ||||
3419 | } | ||||
3420 | // Otherwise, we don't know (such as, extracting from a function return value | ||||
3421 | // or load instruction) | ||||
3422 | return nullptr; | ||||
3423 | } | ||||
3424 | |||||
3425 | /// Analyze the specified pointer to see if it can be expressed as a base | ||||
3426 | /// pointer plus a constant offset. Return the base and offset to the caller. | ||||
3427 | Value *llvm::GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, | ||||
3428 | const DataLayout &DL) { | ||||
3429 | unsigned BitWidth = DL.getIndexTypeSizeInBits(Ptr->getType()); | ||||
3430 | APInt ByteOffset(BitWidth, 0); | ||||
3431 | |||||
3432 | // We walk up the defs but use a visited set to handle unreachable code. In | ||||
3433 | // that case, we stop after accumulating the cycle once (not that it | ||||
3434 | // matters). | ||||
3435 | SmallPtrSet<Value *, 16> Visited; | ||||
3436 | while (Visited.insert(Ptr).second) { | ||||
3437 | if (Ptr->getType()->isVectorTy()) | ||||
3438 | break; | ||||
3439 | |||||
3440 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) { | ||||
3441 | // If one of the values we have visited is an addrspacecast, then | ||||
3442 | // the pointer type of this GEP may be different from the type | ||||
3443 | // of the Ptr parameter which was passed to this function. This | ||||
3444 | // means when we construct GEPOffset, we need to use the size | ||||
3445 | // of GEP's pointer type rather than the size of the original | ||||
3446 | // pointer type. | ||||
3447 | APInt GEPOffset(DL.getIndexTypeSizeInBits(Ptr->getType()), 0); | ||||
3448 | if (!GEP->accumulateConstantOffset(DL, GEPOffset)) | ||||
3449 | break; | ||||
3450 | |||||
3451 | APInt OrigByteOffset(ByteOffset); | ||||
3452 | ByteOffset += GEPOffset.sextOrTrunc(ByteOffset.getBitWidth()); | ||||
3453 | if (ByteOffset.getMinSignedBits() > 64) { | ||||
3454 | // Stop traversal if the pointer offset wouldn't fit into int64_t | ||||
3455 | // (this should be removed if Offset is updated to an APInt) | ||||
3456 | ByteOffset = OrigByteOffset; | ||||
3457 | break; | ||||
3458 | } | ||||
3459 | |||||
3460 | Ptr = GEP->getPointerOperand(); | ||||
3461 | } else if (Operator::getOpcode(Ptr) == Instruction::BitCast || | ||||
3462 | Operator::getOpcode(Ptr) == Instruction::AddrSpaceCast) { | ||||
3463 | Ptr = cast<Operator>(Ptr)->getOperand(0); | ||||
3464 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(Ptr)) { | ||||
3465 | if (GA->isInterposable()) | ||||
3466 | break; | ||||
3467 | Ptr = GA->getAliasee(); | ||||
3468 | } else { | ||||
3469 | break; | ||||
3470 | } | ||||
3471 | } | ||||
3472 | Offset = ByteOffset.getSExtValue(); | ||||
3473 | return Ptr; | ||||
3474 | } | ||||
3475 | |||||
3476 | bool llvm::isGEPBasedOnPointerToString(const GEPOperator *GEP, | ||||
3477 | unsigned CharSize) { | ||||
3478 | // Make sure the GEP has exactly three arguments. | ||||
3479 | if (GEP->getNumOperands() != 3) | ||||
3480 | return false; | ||||
3481 | |||||
3482 | // Make sure the index-ee is a pointer to array of \p CharSize integers. | ||||
3483 | // CharSize. | ||||
3484 | ArrayType *AT = dyn_cast<ArrayType>(GEP->getSourceElementType()); | ||||
3485 | if (!AT || !AT->getElementType()->isIntegerTy(CharSize)) | ||||
3486 | return false; | ||||
3487 | |||||
3488 | // Check to make sure that the first operand of the GEP is an integer and | ||||
3489 | // has value 0 so that we are sure we're indexing into the initializer. | ||||
3490 | const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1)); | ||||
3491 | if (!FirstIdx || !FirstIdx->isZero()) | ||||
3492 | return false; | ||||
3493 | |||||
3494 | return true; | ||||
3495 | } | ||||
3496 | |||||
3497 | bool llvm::getConstantDataArrayInfo(const Value *V, | ||||
3498 | ConstantDataArraySlice &Slice, | ||||
3499 | unsigned ElementSize, uint64_t Offset) { | ||||
3500 | assert(V)((V) ? static_cast<void> (0) : __assert_fail ("V", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 3500, __PRETTY_FUNCTION__)); | ||||
3501 | |||||
3502 | // Look through bitcast instructions and geps. | ||||
3503 | V = V->stripPointerCasts(); | ||||
3504 | |||||
3505 | // If the value is a GEP instruction or constant expression, treat it as an | ||||
3506 | // offset. | ||||
3507 | if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { | ||||
3508 | // The GEP operator should be based on a pointer to string constant, and is | ||||
3509 | // indexing into the string constant. | ||||
3510 | if (!isGEPBasedOnPointerToString(GEP, ElementSize)) | ||||
3511 | return false; | ||||
3512 | |||||
3513 | // If the second index isn't a ConstantInt, then this is a variable index | ||||
3514 | // into the array. If this occurs, we can't say anything meaningful about | ||||
3515 | // the string. | ||||
3516 | uint64_t StartIdx = 0; | ||||
3517 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2))) | ||||
3518 | StartIdx = CI->getZExtValue(); | ||||
3519 | else | ||||
3520 | return false; | ||||
3521 | return getConstantDataArrayInfo(GEP->getOperand(0), Slice, ElementSize, | ||||
3522 | StartIdx + Offset); | ||||
3523 | } | ||||
3524 | |||||
3525 | // The GEP instruction, constant or instruction, must reference a global | ||||
3526 | // variable that is a constant and is initialized. The referenced constant | ||||
3527 | // initializer is the array that we'll use for optimization. | ||||
3528 | const GlobalVariable *GV = dyn_cast<GlobalVariable>(V); | ||||
3529 | if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer()) | ||||
3530 | return false; | ||||
3531 | |||||
3532 | const ConstantDataArray *Array; | ||||
3533 | ArrayType *ArrayTy; | ||||
3534 | if (GV->getInitializer()->isNullValue()) { | ||||
3535 | Type *GVTy = GV->getValueType(); | ||||
3536 | if ( (ArrayTy = dyn_cast<ArrayType>(GVTy)) ) { | ||||
3537 | // A zeroinitializer for the array; there is no ConstantDataArray. | ||||
3538 | Array = nullptr; | ||||
3539 | } else { | ||||
3540 | const DataLayout &DL = GV->getParent()->getDataLayout(); | ||||
3541 | uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy); | ||||
3542 | uint64_t Length = SizeInBytes / (ElementSize / 8); | ||||
3543 | if (Length <= Offset) | ||||
3544 | return false; | ||||
3545 | |||||
3546 | Slice.Array = nullptr; | ||||
3547 | Slice.Offset = 0; | ||||
3548 | Slice.Length = Length - Offset; | ||||
3549 | return true; | ||||
3550 | } | ||||
3551 | } else { | ||||
3552 | // This must be a ConstantDataArray. | ||||
3553 | Array = dyn_cast<ConstantDataArray>(GV->getInitializer()); | ||||
3554 | if (!Array) | ||||
3555 | return false; | ||||
3556 | ArrayTy = Array->getType(); | ||||
3557 | } | ||||
3558 | if (!ArrayTy->getElementType()->isIntegerTy(ElementSize)) | ||||
3559 | return false; | ||||
3560 | |||||
3561 | uint64_t NumElts = ArrayTy->getArrayNumElements(); | ||||
3562 | if (Offset > NumElts) | ||||
3563 | return false; | ||||
3564 | |||||
3565 | Slice.Array = Array; | ||||
3566 | Slice.Offset = Offset; | ||||
3567 | Slice.Length = NumElts - Offset; | ||||
3568 | return true; | ||||
3569 | } | ||||
3570 | |||||
3571 | /// This function computes the length of a null-terminated C string pointed to | ||||
3572 | /// by V. If successful, it returns true and returns the string in Str. | ||||
3573 | /// If unsuccessful, it returns false. | ||||
3574 | bool llvm::getConstantStringInfo(const Value *V, StringRef &Str, | ||||
3575 | uint64_t Offset, bool TrimAtNul) { | ||||
3576 | ConstantDataArraySlice Slice; | ||||
3577 | if (!getConstantDataArrayInfo(V, Slice, 8, Offset)) | ||||
3578 | return false; | ||||
3579 | |||||
3580 | if (Slice.Array == nullptr) { | ||||
3581 | if (TrimAtNul) { | ||||
3582 | Str = StringRef(); | ||||
3583 | return true; | ||||
3584 | } | ||||
3585 | if (Slice.Length == 1) { | ||||
3586 | Str = StringRef("", 1); | ||||
3587 | return true; | ||||
3588 | } | ||||
3589 | // We cannot instantiate a StringRef as we do not have an appropriate string | ||||
3590 | // of 0s at hand. | ||||
3591 | return false; | ||||
3592 | } | ||||
3593 | |||||
3594 | // Start out with the entire array in the StringRef. | ||||
3595 | Str = Slice.Array->getAsString(); | ||||
3596 | // Skip over 'offset' bytes. | ||||
3597 | Str = Str.substr(Slice.Offset); | ||||
3598 | |||||
3599 | if (TrimAtNul) { | ||||
3600 | // Trim off the \0 and anything after it. If the array is not nul | ||||
3601 | // terminated, we just return the whole end of string. The client may know | ||||
3602 | // some other way that the string is length-bound. | ||||
3603 | Str = Str.substr(0, Str.find('\0')); | ||||
3604 | } | ||||
3605 | return true; | ||||
3606 | } | ||||
3607 | |||||
3608 | // These next two are very similar to the above, but also look through PHI | ||||
3609 | // nodes. | ||||
3610 | // TODO: See if we can integrate these two together. | ||||
3611 | |||||
3612 | /// If we can compute the length of the string pointed to by | ||||
3613 | /// the specified pointer, return 'len+1'. If we can't, return 0. | ||||
3614 | static uint64_t GetStringLengthH(const Value *V, | ||||
3615 | SmallPtrSetImpl<const PHINode*> &PHIs, | ||||
3616 | unsigned CharSize) { | ||||
3617 | // Look through noop bitcast instructions. | ||||
3618 | V = V->stripPointerCasts(); | ||||
3619 | |||||
3620 | // If this is a PHI node, there are two cases: either we have already seen it | ||||
3621 | // or we haven't. | ||||
3622 | if (const PHINode *PN = dyn_cast<PHINode>(V)) { | ||||
3623 | if (!PHIs.insert(PN).second) | ||||
3624 | return ~0ULL; // already in the set. | ||||
3625 | |||||
3626 | // If it was new, see if all the input strings are the same length. | ||||
3627 | uint64_t LenSoFar = ~0ULL; | ||||
3628 | for (Value *IncValue : PN->incoming_values()) { | ||||
3629 | uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize); | ||||
3630 | if (Len == 0) return 0; // Unknown length -> unknown. | ||||
3631 | |||||
3632 | if (Len == ~0ULL) continue; | ||||
3633 | |||||
3634 | if (Len != LenSoFar && LenSoFar != ~0ULL) | ||||
3635 | return 0; // Disagree -> unknown. | ||||
3636 | LenSoFar = Len; | ||||
3637 | } | ||||
3638 | |||||
3639 | // Success, all agree. | ||||
3640 | return LenSoFar; | ||||
3641 | } | ||||
3642 | |||||
3643 | // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y) | ||||
3644 | if (const SelectInst *SI = dyn_cast<SelectInst>(V)) { | ||||
3645 | uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize); | ||||
3646 | if (Len1 == 0) return 0; | ||||
3647 | uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize); | ||||
3648 | if (Len2 == 0) return 0; | ||||
3649 | if (Len1 == ~0ULL) return Len2; | ||||
3650 | if (Len2 == ~0ULL) return Len1; | ||||
3651 | if (Len1 != Len2) return 0; | ||||
3652 | return Len1; | ||||
3653 | } | ||||
3654 | |||||
3655 | // Otherwise, see if we can read the string. | ||||
3656 | ConstantDataArraySlice Slice; | ||||
3657 | if (!getConstantDataArrayInfo(V, Slice, CharSize)) | ||||
3658 | return 0; | ||||
3659 | |||||
3660 | if (Slice.Array == nullptr) | ||||
3661 | return 1; | ||||
3662 | |||||
3663 | // Search for nul characters | ||||
3664 | unsigned NullIndex = 0; | ||||
3665 | for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) { | ||||
3666 | if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0) | ||||
3667 | break; | ||||
3668 | } | ||||
3669 | |||||
3670 | return NullIndex + 1; | ||||
3671 | } | ||||
3672 | |||||
3673 | /// If we can compute the length of the string pointed to by | ||||
3674 | /// the specified pointer, return 'len+1'. If we can't, return 0. | ||||
3675 | uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) { | ||||
3676 | if (!V->getType()->isPointerTy()) | ||||
3677 | return 0; | ||||
3678 | |||||
3679 | SmallPtrSet<const PHINode*, 32> PHIs; | ||||
3680 | uint64_t Len = GetStringLengthH(V, PHIs, CharSize); | ||||
3681 | // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return | ||||
3682 | // an empty string as a length. | ||||
3683 | return Len == ~0ULL ? 1 : Len; | ||||
3684 | } | ||||
3685 | |||||
3686 | const Value *llvm::getArgumentAliasingToReturnedPointer(const CallBase *Call) { | ||||
3687 | assert(Call &&((Call && "getArgumentAliasingToReturnedPointer only works on nonnull calls" ) ? static_cast<void> (0) : __assert_fail ("Call && \"getArgumentAliasingToReturnedPointer only works on nonnull calls\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 3688, __PRETTY_FUNCTION__)) | ||||
3688 | "getArgumentAliasingToReturnedPointer only works on nonnull calls")((Call && "getArgumentAliasingToReturnedPointer only works on nonnull calls" ) ? static_cast<void> (0) : __assert_fail ("Call && \"getArgumentAliasingToReturnedPointer only works on nonnull calls\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 3688, __PRETTY_FUNCTION__)); | ||||
3689 | if (const Value *RV = Call->getReturnedArgOperand()) | ||||
3690 | return RV; | ||||
3691 | // This can be used only as a aliasing property. | ||||
3692 | if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(Call)) | ||||
3693 | return Call->getArgOperand(0); | ||||
3694 | return nullptr; | ||||
3695 | } | ||||
3696 | |||||
3697 | bool llvm::isIntrinsicReturningPointerAliasingArgumentWithoutCapturing( | ||||
3698 | const CallBase *Call) { | ||||
3699 | return Call->getIntrinsicID() == Intrinsic::launder_invariant_group || | ||||
3700 | Call->getIntrinsicID() == Intrinsic::strip_invariant_group; | ||||
3701 | } | ||||
3702 | |||||
3703 | /// \p PN defines a loop-variant pointer to an object. Check if the | ||||
3704 | /// previous iteration of the loop was referring to the same object as \p PN. | ||||
3705 | static bool isSameUnderlyingObjectInLoop(const PHINode *PN, | ||||
3706 | const LoopInfo *LI) { | ||||
3707 | // Find the loop-defined value. | ||||
3708 | Loop *L = LI->getLoopFor(PN->getParent()); | ||||
3709 | if (PN->getNumIncomingValues() != 2) | ||||
3710 | return true; | ||||
3711 | |||||
3712 | // Find the value from previous iteration. | ||||
3713 | auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0)); | ||||
3714 | if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L) | ||||
3715 | PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1)); | ||||
3716 | if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L) | ||||
3717 | return true; | ||||
3718 | |||||
3719 | // If a new pointer is loaded in the loop, the pointer references a different | ||||
3720 | // object in every iteration. E.g.: | ||||
3721 | // for (i) | ||||
3722 | // int *p = a[i]; | ||||
3723 | // ... | ||||
3724 | if (auto *Load = dyn_cast<LoadInst>(PrevValue)) | ||||
3725 | if (!L->isLoopInvariant(Load->getPointerOperand())) | ||||
3726 | return false; | ||||
3727 | return true; | ||||
3728 | } | ||||
3729 | |||||
3730 | Value *llvm::GetUnderlyingObject(Value *V, const DataLayout &DL, | ||||
3731 | unsigned MaxLookup) { | ||||
3732 | if (!V->getType()->isPointerTy()) | ||||
3733 | return V; | ||||
3734 | for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) { | ||||
3735 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { | ||||
3736 | V = GEP->getPointerOperand(); | ||||
3737 | } else if (Operator::getOpcode(V) == Instruction::BitCast || | ||||
3738 | Operator::getOpcode(V) == Instruction::AddrSpaceCast) { | ||||
3739 | V = cast<Operator>(V)->getOperand(0); | ||||
3740 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { | ||||
3741 | if (GA->isInterposable()) | ||||
3742 | return V; | ||||
3743 | V = GA->getAliasee(); | ||||
3744 | } else if (isa<AllocaInst>(V)) { | ||||
3745 | // An alloca can't be further simplified. | ||||
3746 | return V; | ||||
3747 | } else { | ||||
3748 | if (auto *Call = dyn_cast<CallBase>(V)) { | ||||
3749 | // CaptureTracking can know about special capturing properties of some | ||||
3750 | // intrinsics like launder.invariant.group, that can't be expressed with | ||||
3751 | // the attributes, but have properties like returning aliasing pointer. | ||||
3752 | // Because some analysis may assume that nocaptured pointer is not | ||||
3753 | // returned from some special intrinsic (because function would have to | ||||
3754 | // be marked with returns attribute), it is crucial to use this function | ||||
3755 | // because it should be in sync with CaptureTracking. Not using it may | ||||
3756 | // cause weird miscompilations where 2 aliasing pointers are assumed to | ||||
3757 | // noalias. | ||||
3758 | if (auto *RP = getArgumentAliasingToReturnedPointer(Call)) { | ||||
3759 | V = RP; | ||||
3760 | continue; | ||||
3761 | } | ||||
3762 | } | ||||
3763 | |||||
3764 | // See if InstructionSimplify knows any relevant tricks. | ||||
3765 | if (Instruction *I = dyn_cast<Instruction>(V)) | ||||
3766 | // TODO: Acquire a DominatorTree and AssumptionCache and use them. | ||||
3767 | if (Value *Simplified = SimplifyInstruction(I, {DL, I})) { | ||||
3768 | V = Simplified; | ||||
3769 | continue; | ||||
3770 | } | ||||
3771 | |||||
3772 | return V; | ||||
3773 | } | ||||
3774 | assert(V->getType()->isPointerTy() && "Unexpected operand type!")((V->getType()->isPointerTy() && "Unexpected operand type!" ) ? static_cast<void> (0) : __assert_fail ("V->getType()->isPointerTy() && \"Unexpected operand type!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 3774, __PRETTY_FUNCTION__)); | ||||
3775 | } | ||||
3776 | return V; | ||||
3777 | } | ||||
3778 | |||||
3779 | void llvm::GetUnderlyingObjects(const Value *V, | ||||
3780 | SmallVectorImpl<const Value *> &Objects, | ||||
3781 | const DataLayout &DL, LoopInfo *LI, | ||||
3782 | unsigned MaxLookup) { | ||||
3783 | SmallPtrSet<const Value *, 4> Visited; | ||||
3784 | SmallVector<const Value *, 4> Worklist; | ||||
3785 | Worklist.push_back(V); | ||||
3786 | do { | ||||
3787 | const Value *P = Worklist.pop_back_val(); | ||||
3788 | P = GetUnderlyingObject(P, DL, MaxLookup); | ||||
3789 | |||||
3790 | if (!Visited.insert(P).second) | ||||
3791 | continue; | ||||
3792 | |||||
3793 | if (auto *SI = dyn_cast<SelectInst>(P)) { | ||||
3794 | Worklist.push_back(SI->getTrueValue()); | ||||
3795 | Worklist.push_back(SI->getFalseValue()); | ||||
3796 | continue; | ||||
3797 | } | ||||
3798 | |||||
3799 | if (auto *PN = dyn_cast<PHINode>(P)) { | ||||
3800 | // If this PHI changes the underlying object in every iteration of the | ||||
3801 | // loop, don't look through it. Consider: | ||||
3802 | // int **A; | ||||
3803 | // for (i) { | ||||
3804 | // Prev = Curr; // Prev = PHI (Prev_0, Curr) | ||||
3805 | // Curr = A[i]; | ||||
3806 | // *Prev, *Curr; | ||||
3807 | // | ||||
3808 | // Prev is tracking Curr one iteration behind so they refer to different | ||||
3809 | // underlying objects. | ||||
3810 | if (!LI || !LI->isLoopHeader(PN->getParent()) || | ||||
3811 | isSameUnderlyingObjectInLoop(PN, LI)) | ||||
3812 | for (Value *IncValue : PN->incoming_values()) | ||||
3813 | Worklist.push_back(IncValue); | ||||
3814 | continue; | ||||
3815 | } | ||||
3816 | |||||
3817 | Objects.push_back(P); | ||||
3818 | } while (!Worklist.empty()); | ||||
3819 | } | ||||
3820 | |||||
3821 | /// This is the function that does the work of looking through basic | ||||
3822 | /// ptrtoint+arithmetic+inttoptr sequences. | ||||
3823 | static const Value *getUnderlyingObjectFromInt(const Value *V) { | ||||
3824 | do { | ||||
3825 | if (const Operator *U = dyn_cast<Operator>(V)) { | ||||
3826 | // If we find a ptrtoint, we can transfer control back to the | ||||
3827 | // regular getUnderlyingObjectFromInt. | ||||
3828 | if (U->getOpcode() == Instruction::PtrToInt) | ||||
3829 | return U->getOperand(0); | ||||
3830 | // If we find an add of a constant, a multiplied value, or a phi, it's | ||||
3831 | // likely that the other operand will lead us to the base | ||||
3832 | // object. We don't have to worry about the case where the | ||||
3833 | // object address is somehow being computed by the multiply, | ||||
3834 | // because our callers only care when the result is an | ||||
3835 | // identifiable object. | ||||
3836 | if (U->getOpcode() != Instruction::Add || | ||||
3837 | (!isa<ConstantInt>(U->getOperand(1)) && | ||||
3838 | Operator::getOpcode(U->getOperand(1)) != Instruction::Mul && | ||||
3839 | !isa<PHINode>(U->getOperand(1)))) | ||||
3840 | return V; | ||||
3841 | V = U->getOperand(0); | ||||
3842 | } else { | ||||
3843 | return V; | ||||
3844 | } | ||||
3845 | assert(V->getType()->isIntegerTy() && "Unexpected operand type!")((V->getType()->isIntegerTy() && "Unexpected operand type!" ) ? static_cast<void> (0) : __assert_fail ("V->getType()->isIntegerTy() && \"Unexpected operand type!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 3845, __PRETTY_FUNCTION__)); | ||||
3846 | } while (true); | ||||
3847 | } | ||||
3848 | |||||
3849 | /// This is a wrapper around GetUnderlyingObjects and adds support for basic | ||||
3850 | /// ptrtoint+arithmetic+inttoptr sequences. | ||||
3851 | /// It returns false if unidentified object is found in GetUnderlyingObjects. | ||||
3852 | bool llvm::getUnderlyingObjectsForCodeGen(const Value *V, | ||||
3853 | SmallVectorImpl<Value *> &Objects, | ||||
3854 | const DataLayout &DL) { | ||||
3855 | SmallPtrSet<const Value *, 16> Visited; | ||||
3856 | SmallVector<const Value *, 4> Working(1, V); | ||||
3857 | do { | ||||
3858 | V = Working.pop_back_val(); | ||||
3859 | |||||
3860 | SmallVector<const Value *, 4> Objs; | ||||
3861 | GetUnderlyingObjects(V, Objs, DL); | ||||
3862 | |||||
3863 | for (const Value *V : Objs) { | ||||
3864 | if (!Visited.insert(V).second) | ||||
3865 | continue; | ||||
3866 | if (Operator::getOpcode(V) == Instruction::IntToPtr) { | ||||
3867 | const Value *O = | ||||
3868 | getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0)); | ||||
3869 | if (O->getType()->isPointerTy()) { | ||||
3870 | Working.push_back(O); | ||||
3871 | continue; | ||||
3872 | } | ||||
3873 | } | ||||
3874 | // If GetUnderlyingObjects fails to find an identifiable object, | ||||
3875 | // getUnderlyingObjectsForCodeGen also fails for safety. | ||||
3876 | if (!isIdentifiedObject(V)) { | ||||
3877 | Objects.clear(); | ||||
3878 | return false; | ||||
3879 | } | ||||
3880 | Objects.push_back(const_cast<Value *>(V)); | ||||
3881 | } | ||||
3882 | } while (!Working.empty()); | ||||
3883 | return true; | ||||
3884 | } | ||||
3885 | |||||
3886 | /// Return true if the only users of this pointer are lifetime markers. | ||||
3887 | bool llvm::onlyUsedByLifetimeMarkers(const Value *V) { | ||||
3888 | for (const User *U : V->users()) { | ||||
3889 | const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U); | ||||
3890 | if (!II) return false; | ||||
3891 | |||||
3892 | if (!II->isLifetimeStartOrEnd()) | ||||
3893 | return false; | ||||
3894 | } | ||||
3895 | return true; | ||||
3896 | } | ||||
3897 | |||||
3898 | bool llvm::isSafeToSpeculativelyExecute(const Value *V, | ||||
3899 | const Instruction *CtxI, | ||||
3900 | const DominatorTree *DT) { | ||||
3901 | const Operator *Inst = dyn_cast<Operator>(V); | ||||
3902 | if (!Inst) | ||||
3903 | return false; | ||||
3904 | |||||
3905 | for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i) | ||||
3906 | if (Constant *C = dyn_cast<Constant>(Inst->getOperand(i))) | ||||
3907 | if (C->canTrap()) | ||||
3908 | return false; | ||||
3909 | |||||
3910 | switch (Inst->getOpcode()) { | ||||
3911 | default: | ||||
3912 | return true; | ||||
3913 | case Instruction::UDiv: | ||||
3914 | case Instruction::URem: { | ||||
3915 | // x / y is undefined if y == 0. | ||||
3916 | const APInt *V; | ||||
3917 | if (match(Inst->getOperand(1), m_APInt(V))) | ||||
3918 | return *V != 0; | ||||
3919 | return false; | ||||
3920 | } | ||||
3921 | case Instruction::SDiv: | ||||
3922 | case Instruction::SRem: { | ||||
3923 | // x / y is undefined if y == 0 or x == INT_MIN and y == -1 | ||||
3924 | const APInt *Numerator, *Denominator; | ||||
3925 | if (!match(Inst->getOperand(1), m_APInt(Denominator))) | ||||
3926 | return false; | ||||
3927 | // We cannot hoist this division if the denominator is 0. | ||||
3928 | if (*Denominator == 0) | ||||
3929 | return false; | ||||
3930 | // It's safe to hoist if the denominator is not 0 or -1. | ||||
3931 | if (*Denominator != -1) | ||||
3932 | return true; | ||||
3933 | // At this point we know that the denominator is -1. It is safe to hoist as | ||||
3934 | // long we know that the numerator is not INT_MIN. | ||||
3935 | if (match(Inst->getOperand(0), m_APInt(Numerator))) | ||||
3936 | return !Numerator->isMinSignedValue(); | ||||
3937 | // The numerator *might* be MinSignedValue. | ||||
3938 | return false; | ||||
3939 | } | ||||
3940 | case Instruction::Load: { | ||||
3941 | const LoadInst *LI = cast<LoadInst>(Inst); | ||||
3942 | if (!LI->isUnordered() || | ||||
3943 | // Speculative load may create a race that did not exist in the source. | ||||
3944 | LI->getFunction()->hasFnAttribute(Attribute::SanitizeThread) || | ||||
3945 | // Speculative load may load data from dirty regions. | ||||
3946 | LI->getFunction()->hasFnAttribute(Attribute::SanitizeAddress) || | ||||
3947 | LI->getFunction()->hasFnAttribute(Attribute::SanitizeHWAddress)) | ||||
3948 | return false; | ||||
3949 | const DataLayout &DL = LI->getModule()->getDataLayout(); | ||||
3950 | return isDereferenceableAndAlignedPointer(LI->getPointerOperand(), | ||||
3951 | LI->getAlignment(), DL, CtxI, DT); | ||||
3952 | } | ||||
3953 | case Instruction::Call: { | ||||
3954 | auto *CI = cast<const CallInst>(Inst); | ||||
3955 | const Function *Callee = CI->getCalledFunction(); | ||||
3956 | |||||
3957 | // The called function could have undefined behavior or side-effects, even | ||||
3958 | // if marked readnone nounwind. | ||||
3959 | return Callee && Callee->isSpeculatable(); | ||||
3960 | } | ||||
3961 | case Instruction::VAArg: | ||||
3962 | case Instruction::Alloca: | ||||
3963 | case Instruction::Invoke: | ||||
3964 | case Instruction::CallBr: | ||||
3965 | case Instruction::PHI: | ||||
3966 | case Instruction::Store: | ||||
3967 | case Instruction::Ret: | ||||
3968 | case Instruction::Br: | ||||
3969 | case Instruction::IndirectBr: | ||||
3970 | case Instruction::Switch: | ||||
3971 | case Instruction::Unreachable: | ||||
3972 | case Instruction::Fence: | ||||
3973 | case Instruction::AtomicRMW: | ||||
3974 | case Instruction::AtomicCmpXchg: | ||||
3975 | case Instruction::LandingPad: | ||||
3976 | case Instruction::Resume: | ||||
3977 | case Instruction::CatchSwitch: | ||||
3978 | case Instruction::CatchPad: | ||||
3979 | case Instruction::CatchRet: | ||||
3980 | case Instruction::CleanupPad: | ||||
3981 | case Instruction::CleanupRet: | ||||
3982 | return false; // Misc instructions which have effects | ||||
3983 | } | ||||
3984 | } | ||||
3985 | |||||
3986 | bool llvm::mayBeMemoryDependent(const Instruction &I) { | ||||
3987 | return I.mayReadOrWriteMemory() || !isSafeToSpeculativelyExecute(&I); | ||||
3988 | } | ||||
3989 | |||||
3990 | /// Convert ConstantRange OverflowResult into ValueTracking OverflowResult. | ||||
3991 | static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR) { | ||||
3992 | switch (OR) { | ||||
3993 | case ConstantRange::OverflowResult::MayOverflow: | ||||
3994 | return OverflowResult::MayOverflow; | ||||
3995 | case ConstantRange::OverflowResult::AlwaysOverflowsLow: | ||||
3996 | return OverflowResult::AlwaysOverflowsLow; | ||||
3997 | case ConstantRange::OverflowResult::AlwaysOverflowsHigh: | ||||
3998 | return OverflowResult::AlwaysOverflowsHigh; | ||||
3999 | case ConstantRange::OverflowResult::NeverOverflows: | ||||
4000 | return OverflowResult::NeverOverflows; | ||||
4001 | } | ||||
4002 | llvm_unreachable("Unknown OverflowResult")::llvm::llvm_unreachable_internal("Unknown OverflowResult", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 4002); | ||||
4003 | } | ||||
4004 | |||||
4005 | /// Combine constant ranges from computeConstantRange() and computeKnownBits(). | ||||
4006 | static ConstantRange computeConstantRangeIncludingKnownBits( | ||||
4007 | const Value *V, bool ForSigned, const DataLayout &DL, unsigned Depth, | ||||
4008 | AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, | ||||
4009 | OptimizationRemarkEmitter *ORE = nullptr, bool UseInstrInfo = true) { | ||||
4010 | KnownBits Known = computeKnownBits( | ||||
4011 | V, DL, Depth, AC, CxtI, DT, ORE, UseInstrInfo); | ||||
4012 | ConstantRange CR1 = ConstantRange::fromKnownBits(Known, ForSigned); | ||||
4013 | ConstantRange CR2 = computeConstantRange(V, UseInstrInfo); | ||||
4014 | ConstantRange::PreferredRangeType RangeType = | ||||
4015 | ForSigned ? ConstantRange::Signed : ConstantRange::Unsigned; | ||||
4016 | return CR1.intersectWith(CR2, RangeType); | ||||
4017 | } | ||||
4018 | |||||
4019 | OverflowResult llvm::computeOverflowForUnsignedMul( | ||||
4020 | const Value *LHS, const Value *RHS, const DataLayout &DL, | ||||
4021 | AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, | ||||
4022 | bool UseInstrInfo) { | ||||
4023 | KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT, | ||||
4024 | nullptr, UseInstrInfo); | ||||
4025 | KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT, | ||||
4026 | nullptr, UseInstrInfo); | ||||
4027 | ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false); | ||||
4028 | ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false); | ||||
4029 | return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange)); | ||||
4030 | } | ||||
4031 | |||||
4032 | OverflowResult | ||||
4033 | llvm::computeOverflowForSignedMul(const Value *LHS, const Value *RHS, | ||||
4034 | const DataLayout &DL, AssumptionCache *AC, | ||||
4035 | const Instruction *CxtI, | ||||
4036 | const DominatorTree *DT, bool UseInstrInfo) { | ||||
4037 | // Multiplying n * m significant bits yields a result of n + m significant | ||||
4038 | // bits. If the total number of significant bits does not exceed the | ||||
4039 | // result bit width (minus 1), there is no overflow. | ||||
4040 | // This means if we have enough leading sign bits in the operands | ||||
4041 | // we can guarantee that the result does not overflow. | ||||
4042 | // Ref: "Hacker's Delight" by Henry Warren | ||||
4043 | unsigned BitWidth = LHS->getType()->getScalarSizeInBits(); | ||||
4044 | |||||
4045 | // Note that underestimating the number of sign bits gives a more | ||||
4046 | // conservative answer. | ||||
4047 | unsigned SignBits = ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) + | ||||
4048 | ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT); | ||||
4049 | |||||
4050 | // First handle the easy case: if we have enough sign bits there's | ||||
4051 | // definitely no overflow. | ||||
4052 | if (SignBits > BitWidth + 1) | ||||
4053 | return OverflowResult::NeverOverflows; | ||||
4054 | |||||
4055 | // There are two ambiguous cases where there can be no overflow: | ||||
4056 | // SignBits == BitWidth + 1 and | ||||
4057 | // SignBits == BitWidth | ||||
4058 | // The second case is difficult to check, therefore we only handle the | ||||
4059 | // first case. | ||||
4060 | if (SignBits == BitWidth + 1) { | ||||
4061 | // It overflows only when both arguments are negative and the true | ||||
4062 | // product is exactly the minimum negative number. | ||||
4063 | // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000 | ||||
4064 | // For simplicity we just check if at least one side is not negative. | ||||
4065 | KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT, | ||||
4066 | nullptr, UseInstrInfo); | ||||
4067 | KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT, | ||||
4068 | nullptr, UseInstrInfo); | ||||
4069 | if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative()) | ||||
4070 | return OverflowResult::NeverOverflows; | ||||
4071 | } | ||||
4072 | return OverflowResult::MayOverflow; | ||||
4073 | } | ||||
4074 | |||||
4075 | OverflowResult llvm::computeOverflowForUnsignedAdd( | ||||
4076 | const Value *LHS, const Value *RHS, const DataLayout &DL, | ||||
4077 | AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, | ||||
4078 | bool UseInstrInfo) { | ||||
4079 | ConstantRange LHSRange = computeConstantRangeIncludingKnownBits( | ||||
4080 | LHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT, | ||||
4081 | nullptr, UseInstrInfo); | ||||
4082 | ConstantRange RHSRange = computeConstantRangeIncludingKnownBits( | ||||
4083 | RHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT, | ||||
4084 | nullptr, UseInstrInfo); | ||||
4085 | return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange)); | ||||
4086 | } | ||||
4087 | |||||
4088 | static OverflowResult computeOverflowForSignedAdd(const Value *LHS, | ||||
4089 | const Value *RHS, | ||||
4090 | const AddOperator *Add, | ||||
4091 | const DataLayout &DL, | ||||
4092 | AssumptionCache *AC, | ||||
4093 | const Instruction *CxtI, | ||||
4094 | const DominatorTree *DT) { | ||||
4095 | if (Add && Add->hasNoSignedWrap()) { | ||||
4096 | return OverflowResult::NeverOverflows; | ||||
4097 | } | ||||
4098 | |||||
4099 | // If LHS and RHS each have at least two sign bits, the addition will look | ||||
4100 | // like | ||||
4101 | // | ||||
4102 | // XX..... + | ||||
4103 | // YY..... | ||||
4104 | // | ||||
4105 | // If the carry into the most significant position is 0, X and Y can't both | ||||
4106 | // be 1 and therefore the carry out of the addition is also 0. | ||||
4107 | // | ||||
4108 | // If the carry into the most significant position is 1, X and Y can't both | ||||
4109 | // be 0 and therefore the carry out of the addition is also 1. | ||||
4110 | // | ||||
4111 | // Since the carry into the most significant position is always equal to | ||||
4112 | // the carry out of the addition, there is no signed overflow. | ||||
4113 | if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 && | ||||
4114 | ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1) | ||||
4115 | return OverflowResult::NeverOverflows; | ||||
4116 | |||||
4117 | ConstantRange LHSRange = computeConstantRangeIncludingKnownBits( | ||||
4118 | LHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT); | ||||
4119 | ConstantRange RHSRange = computeConstantRangeIncludingKnownBits( | ||||
4120 | RHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT); | ||||
4121 | OverflowResult OR = | ||||
4122 | mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange)); | ||||
4123 | if (OR != OverflowResult::MayOverflow) | ||||
4124 | return OR; | ||||
4125 | |||||
4126 | // The remaining code needs Add to be available. Early returns if not so. | ||||
4127 | if (!Add) | ||||
4128 | return OverflowResult::MayOverflow; | ||||
4129 | |||||
4130 | // If the sign of Add is the same as at least one of the operands, this add | ||||
4131 | // CANNOT overflow. If this can be determined from the known bits of the | ||||
4132 | // operands the above signedAddMayOverflow() check will have already done so. | ||||
4133 | // The only other way to improve on the known bits is from an assumption, so | ||||
4134 | // call computeKnownBitsFromAssume() directly. | ||||
4135 | bool LHSOrRHSKnownNonNegative = | ||||
4136 | (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative()); | ||||
4137 | bool LHSOrRHSKnownNegative = | ||||
4138 | (LHSRange.isAllNegative() || RHSRange.isAllNegative()); | ||||
4139 | if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) { | ||||
4140 | KnownBits AddKnown(LHSRange.getBitWidth()); | ||||
4141 | computeKnownBitsFromAssume( | ||||
4142 | Add, AddKnown, /*Depth=*/0, Query(DL, AC, CxtI, DT, true)); | ||||
4143 | if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) || | ||||
4144 | (AddKnown.isNegative() && LHSOrRHSKnownNegative)) | ||||
4145 | return OverflowResult::NeverOverflows; | ||||
4146 | } | ||||
4147 | |||||
4148 | return OverflowResult::MayOverflow; | ||||
4149 | } | ||||
4150 | |||||
4151 | OverflowResult llvm::computeOverflowForUnsignedSub(const Value *LHS, | ||||
4152 | const Value *RHS, | ||||
4153 | const DataLayout &DL, | ||||
4154 | AssumptionCache *AC, | ||||
4155 | const Instruction *CxtI, | ||||
4156 | const DominatorTree *DT) { | ||||
4157 | ConstantRange LHSRange = computeConstantRangeIncludingKnownBits( | ||||
4158 | LHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT); | ||||
4159 | ConstantRange RHSRange = computeConstantRangeIncludingKnownBits( | ||||
4160 | RHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT); | ||||
4161 | return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange)); | ||||
4162 | } | ||||
4163 | |||||
4164 | OverflowResult llvm::computeOverflowForSignedSub(const Value *LHS, | ||||
4165 | const Value *RHS, | ||||
4166 | const DataLayout &DL, | ||||
4167 | AssumptionCache *AC, | ||||
4168 | const Instruction *CxtI, | ||||
4169 | const DominatorTree *DT) { | ||||
4170 | // If LHS and RHS each have at least two sign bits, the subtraction | ||||
4171 | // cannot overflow. | ||||
4172 | if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 && | ||||
4173 | ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1) | ||||
4174 | return OverflowResult::NeverOverflows; | ||||
4175 | |||||
4176 | ConstantRange LHSRange = computeConstantRangeIncludingKnownBits( | ||||
4177 | LHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT); | ||||
4178 | ConstantRange RHSRange = computeConstantRangeIncludingKnownBits( | ||||
4179 | RHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT); | ||||
4180 | return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange)); | ||||
4181 | } | ||||
4182 | |||||
4183 | bool llvm::isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, | ||||
4184 | const DominatorTree &DT) { | ||||
4185 | SmallVector<const BranchInst *, 2> GuardingBranches; | ||||
4186 | SmallVector<const ExtractValueInst *, 2> Results; | ||||
4187 | |||||
4188 | for (const User *U : WO->users()) { | ||||
4189 | if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) { | ||||
4190 | assert(EVI->getNumIndices() == 1 && "Obvious from CI's type")((EVI->getNumIndices() == 1 && "Obvious from CI's type" ) ? static_cast<void> (0) : __assert_fail ("EVI->getNumIndices() == 1 && \"Obvious from CI's type\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 4190, __PRETTY_FUNCTION__)); | ||||
4191 | |||||
4192 | if (EVI->getIndices()[0] == 0) | ||||
4193 | Results.push_back(EVI); | ||||
4194 | else { | ||||
4195 | assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type")((EVI->getIndices()[0] == 1 && "Obvious from CI's type" ) ? static_cast<void> (0) : __assert_fail ("EVI->getIndices()[0] == 1 && \"Obvious from CI's type\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 4195, __PRETTY_FUNCTION__)); | ||||
4196 | |||||
4197 | for (const auto *U : EVI->users()) | ||||
4198 | if (const auto *B = dyn_cast<BranchInst>(U)) { | ||||
4199 | assert(B->isConditional() && "How else is it using an i1?")((B->isConditional() && "How else is it using an i1?" ) ? static_cast<void> (0) : __assert_fail ("B->isConditional() && \"How else is it using an i1?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 4199, __PRETTY_FUNCTION__)); | ||||
4200 | GuardingBranches.push_back(B); | ||||
4201 | } | ||||
4202 | } | ||||
4203 | } else { | ||||
4204 | // We are using the aggregate directly in a way we don't want to analyze | ||||
4205 | // here (storing it to a global, say). | ||||
4206 | return false; | ||||
4207 | } | ||||
4208 | } | ||||
4209 | |||||
4210 | auto AllUsesGuardedByBranch = [&](const BranchInst *BI) { | ||||
4211 | BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1)); | ||||
4212 | if (!NoWrapEdge.isSingleEdge()) | ||||
4213 | return false; | ||||
4214 | |||||
4215 | // Check if all users of the add are provably no-wrap. | ||||
4216 | for (const auto *Result : Results) { | ||||
4217 | // If the extractvalue itself is not executed on overflow, the we don't | ||||
4218 | // need to check each use separately, since domination is transitive. | ||||
4219 | if (DT.dominates(NoWrapEdge, Result->getParent())) | ||||
4220 | continue; | ||||
4221 | |||||
4222 | for (auto &RU : Result->uses()) | ||||
4223 | if (!DT.dominates(NoWrapEdge, RU)) | ||||
4224 | return false; | ||||
4225 | } | ||||
4226 | |||||
4227 | return true; | ||||
4228 | }; | ||||
4229 | |||||
4230 | return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch); | ||||
4231 | } | ||||
4232 | |||||
4233 | |||||
4234 | OverflowResult llvm::computeOverflowForSignedAdd(const AddOperator *Add, | ||||
4235 | const DataLayout &DL, | ||||
4236 | AssumptionCache *AC, | ||||
4237 | const Instruction *CxtI, | ||||
4238 | const DominatorTree *DT) { | ||||
4239 | return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1), | ||||
4240 | Add, DL, AC, CxtI, DT); | ||||
4241 | } | ||||
4242 | |||||
4243 | OverflowResult llvm::computeOverflowForSignedAdd(const Value *LHS, | ||||
4244 | const Value *RHS, | ||||
4245 | const DataLayout &DL, | ||||
4246 | AssumptionCache *AC, | ||||
4247 | const Instruction *CxtI, | ||||
4248 | const DominatorTree *DT) { | ||||
4249 | return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, DL, AC, CxtI, DT); | ||||
4250 | } | ||||
4251 | |||||
4252 | bool llvm::isGuaranteedToTransferExecutionToSuccessor(const Instruction *I) { | ||||
4253 | // A memory operation returns normally if it isn't volatile. A volatile | ||||
4254 | // operation is allowed to trap. | ||||
4255 | // | ||||
4256 | // An atomic operation isn't guaranteed to return in a reasonable amount of | ||||
4257 | // time because it's possible for another thread to interfere with it for an | ||||
4258 | // arbitrary length of time, but programs aren't allowed to rely on that. | ||||
4259 | if (const LoadInst *LI = dyn_cast<LoadInst>(I)) | ||||
4260 | return !LI->isVolatile(); | ||||
4261 | if (const StoreInst *SI = dyn_cast<StoreInst>(I)) | ||||
4262 | return !SI->isVolatile(); | ||||
4263 | if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I)) | ||||
4264 | return !CXI->isVolatile(); | ||||
4265 | if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I)) | ||||
4266 | return !RMWI->isVolatile(); | ||||
4267 | if (const MemIntrinsic *MII = dyn_cast<MemIntrinsic>(I)) | ||||
4268 | return !MII->isVolatile(); | ||||
4269 | |||||
4270 | // If there is no successor, then execution can't transfer to it. | ||||
4271 | if (const auto *CRI = dyn_cast<CleanupReturnInst>(I)) | ||||
4272 | return !CRI->unwindsToCaller(); | ||||
4273 | if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(I)) | ||||
4274 | return !CatchSwitch->unwindsToCaller(); | ||||
4275 | if (isa<ResumeInst>(I)) | ||||
4276 | return false; | ||||
4277 | if (isa<ReturnInst>(I)) | ||||
4278 | return false; | ||||
4279 | if (isa<UnreachableInst>(I)) | ||||
4280 | return false; | ||||
4281 | |||||
4282 | // Calls can throw, or contain an infinite loop, or kill the process. | ||||
4283 | if (auto CS = ImmutableCallSite(I)) { | ||||
4284 | // Call sites that throw have implicit non-local control flow. | ||||
4285 | if (!CS.doesNotThrow()) | ||||
4286 | return false; | ||||
4287 | |||||
4288 | // Non-throwing call sites can loop infinitely, call exit/pthread_exit | ||||
4289 | // etc. and thus not return. However, LLVM already assumes that | ||||
4290 | // | ||||
4291 | // - Thread exiting actions are modeled as writes to memory invisible to | ||||
4292 | // the program. | ||||
4293 | // | ||||
4294 | // - Loops that don't have side effects (side effects are volatile/atomic | ||||
4295 | // stores and IO) always terminate (see http://llvm.org/PR965). | ||||
4296 | // Furthermore IO itself is also modeled as writes to memory invisible to | ||||
4297 | // the program. | ||||
4298 | // | ||||
4299 | // We rely on those assumptions here, and use the memory effects of the call | ||||
4300 | // target as a proxy for checking that it always returns. | ||||
4301 | |||||
4302 | // FIXME: This isn't aggressive enough; a call which only writes to a global | ||||
4303 | // is guaranteed to return. | ||||
4304 | return CS.onlyReadsMemory() || CS.onlyAccessesArgMemory() || | ||||
4305 | match(I, m_Intrinsic<Intrinsic::assume>()) || | ||||
4306 | match(I, m_Intrinsic<Intrinsic::sideeffect>()) || | ||||
4307 | match(I, m_Intrinsic<Intrinsic::experimental_widenable_condition>()); | ||||
4308 | } | ||||
4309 | |||||
4310 | // Other instructions return normally. | ||||
4311 | return true; | ||||
4312 | } | ||||
4313 | |||||
4314 | bool llvm::isGuaranteedToTransferExecutionToSuccessor(const BasicBlock *BB) { | ||||
4315 | // TODO: This is slightly conservative for invoke instruction since exiting | ||||
4316 | // via an exception *is* normal control for them. | ||||
4317 | for (auto I = BB->begin(), E = BB->end(); I != E; ++I) | ||||
4318 | if (!isGuaranteedToTransferExecutionToSuccessor(&*I)) | ||||
4319 | return false; | ||||
4320 | return true; | ||||
4321 | } | ||||
4322 | |||||
4323 | bool llvm::isGuaranteedToExecuteForEveryIteration(const Instruction *I, | ||||
4324 | const Loop *L) { | ||||
4325 | // The loop header is guaranteed to be executed for every iteration. | ||||
4326 | // | ||||
4327 | // FIXME: Relax this constraint to cover all basic blocks that are | ||||
4328 | // guaranteed to be executed at every iteration. | ||||
4329 | if (I->getParent() != L->getHeader()) return false; | ||||
4330 | |||||
4331 | for (const Instruction &LI : *L->getHeader()) { | ||||
4332 | if (&LI == I) return true; | ||||
4333 | if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false; | ||||
4334 | } | ||||
4335 | llvm_unreachable("Instruction not contained in its own parent basic block.")::llvm::llvm_unreachable_internal("Instruction not contained in its own parent basic block." , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 4335); | ||||
4336 | } | ||||
4337 | |||||
4338 | bool llvm::propagatesFullPoison(const Instruction *I) { | ||||
4339 | switch (I->getOpcode()) { | ||||
4340 | case Instruction::Add: | ||||
4341 | case Instruction::Sub: | ||||
4342 | case Instruction::Xor: | ||||
4343 | case Instruction::Trunc: | ||||
4344 | case Instruction::BitCast: | ||||
4345 | case Instruction::AddrSpaceCast: | ||||
4346 | case Instruction::Mul: | ||||
4347 | case Instruction::Shl: | ||||
4348 | case Instruction::GetElementPtr: | ||||
4349 | // These operations all propagate poison unconditionally. Note that poison | ||||
4350 | // is not any particular value, so xor or subtraction of poison with | ||||
4351 | // itself still yields poison, not zero. | ||||
4352 | return true; | ||||
4353 | |||||
4354 | case Instruction::AShr: | ||||
4355 | case Instruction::SExt: | ||||
4356 | // For these operations, one bit of the input is replicated across | ||||
4357 | // multiple output bits. A replicated poison bit is still poison. | ||||
4358 | return true; | ||||
4359 | |||||
4360 | case Instruction::ICmp: | ||||
4361 | // Comparing poison with any value yields poison. This is why, for | ||||
4362 | // instance, x s< (x +nsw 1) can be folded to true. | ||||
4363 | return true; | ||||
4364 | |||||
4365 | default: | ||||
4366 | return false; | ||||
4367 | } | ||||
4368 | } | ||||
4369 | |||||
4370 | const Value *llvm::getGuaranteedNonFullPoisonOp(const Instruction *I) { | ||||
4371 | switch (I->getOpcode()) { | ||||
4372 | case Instruction::Store: | ||||
4373 | return cast<StoreInst>(I)->getPointerOperand(); | ||||
4374 | |||||
4375 | case Instruction::Load: | ||||
4376 | return cast<LoadInst>(I)->getPointerOperand(); | ||||
4377 | |||||
4378 | case Instruction::AtomicCmpXchg: | ||||
4379 | return cast<AtomicCmpXchgInst>(I)->getPointerOperand(); | ||||
4380 | |||||
4381 | case Instruction::AtomicRMW: | ||||
4382 | return cast<AtomicRMWInst>(I)->getPointerOperand(); | ||||
4383 | |||||
4384 | case Instruction::UDiv: | ||||
4385 | case Instruction::SDiv: | ||||
4386 | case Instruction::URem: | ||||
4387 | case Instruction::SRem: | ||||
4388 | return I->getOperand(1); | ||||
4389 | |||||
4390 | default: | ||||
4391 | return nullptr; | ||||
4392 | } | ||||
4393 | } | ||||
4394 | |||||
4395 | bool llvm::programUndefinedIfFullPoison(const Instruction *PoisonI) { | ||||
4396 | // We currently only look for uses of poison values within the same basic | ||||
4397 | // block, as that makes it easier to guarantee that the uses will be | ||||
4398 | // executed given that PoisonI is executed. | ||||
4399 | // | ||||
4400 | // FIXME: Expand this to consider uses beyond the same basic block. To do | ||||
4401 | // this, look out for the distinction between post-dominance and strong | ||||
4402 | // post-dominance. | ||||
4403 | const BasicBlock *BB = PoisonI->getParent(); | ||||
4404 | |||||
4405 | // Set of instructions that we have proved will yield poison if PoisonI | ||||
4406 | // does. | ||||
4407 | SmallSet<const Value *, 16> YieldsPoison; | ||||
4408 | SmallSet<const BasicBlock *, 4> Visited; | ||||
4409 | YieldsPoison.insert(PoisonI); | ||||
4410 | Visited.insert(PoisonI->getParent()); | ||||
4411 | |||||
4412 | BasicBlock::const_iterator Begin = PoisonI->getIterator(), End = BB->end(); | ||||
4413 | |||||
4414 | unsigned Iter = 0; | ||||
4415 | while (Iter++ < MaxDepth) { | ||||
4416 | for (auto &I : make_range(Begin, End)) { | ||||
4417 | if (&I != PoisonI) { | ||||
4418 | const Value *NotPoison = getGuaranteedNonFullPoisonOp(&I); | ||||
4419 | if (NotPoison != nullptr && YieldsPoison.count(NotPoison)) | ||||
4420 | return true; | ||||
4421 | if (!isGuaranteedToTransferExecutionToSuccessor(&I)) | ||||
4422 | return false; | ||||
4423 | } | ||||
4424 | |||||
4425 | // Mark poison that propagates from I through uses of I. | ||||
4426 | if (YieldsPoison.count(&I)) { | ||||
4427 | for (const User *User : I.users()) { | ||||
4428 | const Instruction *UserI = cast<Instruction>(User); | ||||
4429 | if (propagatesFullPoison(UserI)) | ||||
4430 | YieldsPoison.insert(User); | ||||
4431 | } | ||||
4432 | } | ||||
4433 | } | ||||
4434 | |||||
4435 | if (auto *NextBB = BB->getSingleSuccessor()) { | ||||
4436 | if (Visited.insert(NextBB).second) { | ||||
4437 | BB = NextBB; | ||||
4438 | Begin = BB->getFirstNonPHI()->getIterator(); | ||||
4439 | End = BB->end(); | ||||
4440 | continue; | ||||
4441 | } | ||||
4442 | } | ||||
4443 | |||||
4444 | break; | ||||
4445 | } | ||||
4446 | return false; | ||||
4447 | } | ||||
4448 | |||||
4449 | static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) { | ||||
4450 | if (FMF.noNaNs()) | ||||
4451 | return true; | ||||
4452 | |||||
4453 | if (auto *C = dyn_cast<ConstantFP>(V)) | ||||
4454 | return !C->isNaN(); | ||||
4455 | |||||
4456 | if (auto *C = dyn_cast<ConstantDataVector>(V)) { | ||||
4457 | if (!C->getElementType()->isFloatingPointTy()) | ||||
4458 | return false; | ||||
4459 | for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) { | ||||
4460 | if (C->getElementAsAPFloat(I).isNaN()) | ||||
4461 | return false; | ||||
4462 | } | ||||
4463 | return true; | ||||
4464 | } | ||||
4465 | |||||
4466 | return false; | ||||
4467 | } | ||||
4468 | |||||
4469 | static bool isKnownNonZero(const Value *V) { | ||||
4470 | if (auto *C = dyn_cast<ConstantFP>(V)) | ||||
4471 | return !C->isZero(); | ||||
4472 | |||||
4473 | if (auto *C = dyn_cast<ConstantDataVector>(V)) { | ||||
4474 | if (!C->getElementType()->isFloatingPointTy()) | ||||
4475 | return false; | ||||
4476 | for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) { | ||||
4477 | if (C->getElementAsAPFloat(I).isZero()) | ||||
4478 | return false; | ||||
4479 | } | ||||
4480 | return true; | ||||
4481 | } | ||||
4482 | |||||
4483 | return false; | ||||
4484 | } | ||||
4485 | |||||
4486 | /// Match clamp pattern for float types without care about NaNs or signed zeros. | ||||
4487 | /// Given non-min/max outer cmp/select from the clamp pattern this | ||||
4488 | /// function recognizes if it can be substitued by a "canonical" min/max | ||||
4489 | /// pattern. | ||||
4490 | static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred, | ||||
4491 | Value *CmpLHS, Value *CmpRHS, | ||||
4492 | Value *TrueVal, Value *FalseVal, | ||||
4493 | Value *&LHS, Value *&RHS) { | ||||
4494 | // Try to match | ||||
4495 | // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2)) | ||||
4496 | // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2)) | ||||
4497 | // and return description of the outer Max/Min. | ||||
4498 | |||||
4499 | // First, check if select has inverse order: | ||||
4500 | if (CmpRHS == FalseVal) { | ||||
| |||||
4501 | std::swap(TrueVal, FalseVal); | ||||
4502 | Pred = CmpInst::getInversePredicate(Pred); | ||||
4503 | } | ||||
4504 | |||||
4505 | // Assume success now. If there's no match, callers should not use these anyway. | ||||
4506 | LHS = TrueVal; | ||||
4507 | RHS = FalseVal; | ||||
4508 | |||||
4509 | const APFloat *FC1; | ||||
4510 | if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite()) | ||||
4511 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
4512 | |||||
4513 | const APFloat *FC2; | ||||
4514 | switch (Pred) { | ||||
4515 | case CmpInst::FCMP_OLT: | ||||
4516 | case CmpInst::FCMP_OLE: | ||||
4517 | case CmpInst::FCMP_ULT: | ||||
4518 | case CmpInst::FCMP_ULE: | ||||
4519 | if (match(FalseVal, | ||||
4520 | m_CombineOr(m_OrdFMin(m_Specific(CmpLHS), m_APFloat(FC2)), | ||||
4521 | m_UnordFMin(m_Specific(CmpLHS), m_APFloat(FC2)))) && | ||||
4522 | FC1->compare(*FC2) == APFloat::cmpResult::cmpLessThan) | ||||
4523 | return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false}; | ||||
4524 | break; | ||||
4525 | case CmpInst::FCMP_OGT: | ||||
4526 | case CmpInst::FCMP_OGE: | ||||
4527 | case CmpInst::FCMP_UGT: | ||||
4528 | case CmpInst::FCMP_UGE: | ||||
4529 | if (match(FalseVal, | ||||
| |||||
4530 | m_CombineOr(m_OrdFMax(m_Specific(CmpLHS), m_APFloat(FC2)), | ||||
4531 | m_UnordFMax(m_Specific(CmpLHS), m_APFloat(FC2)))) && | ||||
4532 | FC1->compare(*FC2) == APFloat::cmpResult::cmpGreaterThan) | ||||
4533 | return {SPF_FMINNUM, SPNB_RETURNS_ANY, false}; | ||||
4534 | break; | ||||
4535 | default: | ||||
4536 | break; | ||||
4537 | } | ||||
4538 | |||||
4539 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
4540 | } | ||||
4541 | |||||
4542 | /// Recognize variations of: | ||||
4543 | /// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v))) | ||||
4544 | static SelectPatternResult matchClamp(CmpInst::Predicate Pred, | ||||
4545 | Value *CmpLHS, Value *CmpRHS, | ||||
4546 | Value *TrueVal, Value *FalseVal) { | ||||
4547 | // Swap the select operands and predicate to match the patterns below. | ||||
4548 | if (CmpRHS != TrueVal) { | ||||
4549 | Pred = ICmpInst::getSwappedPredicate(Pred); | ||||
4550 | std::swap(TrueVal, FalseVal); | ||||
4551 | } | ||||
4552 | const APInt *C1; | ||||
4553 | if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) { | ||||
4554 | const APInt *C2; | ||||
4555 | // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1) | ||||
4556 | if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) && | ||||
4557 | C1->slt(*C2) && Pred == CmpInst::ICMP_SLT) | ||||
4558 | return {SPF_SMAX, SPNB_NA, false}; | ||||
4559 | |||||
4560 | // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1) | ||||
4561 | if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) && | ||||
4562 | C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT) | ||||
4563 | return {SPF_SMIN, SPNB_NA, false}; | ||||
4564 | |||||
4565 | // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1) | ||||
4566 | if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) && | ||||
4567 | C1->ult(*C2) && Pred == CmpInst::ICMP_ULT) | ||||
4568 | return {SPF_UMAX, SPNB_NA, false}; | ||||
4569 | |||||
4570 | // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1) | ||||
4571 | if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) && | ||||
4572 | C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT) | ||||
4573 | return {SPF_UMIN, SPNB_NA, false}; | ||||
4574 | } | ||||
4575 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
4576 | } | ||||
4577 | |||||
4578 | /// Recognize variations of: | ||||
4579 | /// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c)) | ||||
4580 | static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred, | ||||
4581 | Value *CmpLHS, Value *CmpRHS, | ||||
4582 | Value *TVal, Value *FVal, | ||||
4583 | unsigned Depth) { | ||||
4584 | // TODO: Allow FP min/max with nnan/nsz. | ||||
4585 | assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison")((CmpInst::isIntPredicate(Pred) && "Expected integer comparison" ) ? static_cast<void> (0) : __assert_fail ("CmpInst::isIntPredicate(Pred) && \"Expected integer comparison\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 4585, __PRETTY_FUNCTION__)); | ||||
4586 | |||||
4587 | Value *A, *B; | ||||
4588 | SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1); | ||||
4589 | if (!SelectPatternResult::isMinOrMax(L.Flavor)) | ||||
4590 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
4591 | |||||
4592 | Value *C, *D; | ||||
4593 | SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1); | ||||
4594 | if (L.Flavor != R.Flavor) | ||||
4595 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
4596 | |||||
4597 | // We have something like: x Pred y ? min(a, b) : min(c, d). | ||||
4598 | // Try to match the compare to the min/max operations of the select operands. | ||||
4599 | // First, make sure we have the right compare predicate. | ||||
4600 | switch (L.Flavor) { | ||||
4601 | case SPF_SMIN: | ||||
4602 | if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) { | ||||
4603 | Pred = ICmpInst::getSwappedPredicate(Pred); | ||||
4604 | std::swap(CmpLHS, CmpRHS); | ||||
4605 | } | ||||
4606 | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) | ||||
4607 | break; | ||||
4608 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
4609 | case SPF_SMAX: | ||||
4610 | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) { | ||||
4611 | Pred = ICmpInst::getSwappedPredicate(Pred); | ||||
4612 | std::swap(CmpLHS, CmpRHS); | ||||
4613 | } | ||||
4614 | if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) | ||||
4615 | break; | ||||
4616 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
4617 | case SPF_UMIN: | ||||
4618 | if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) { | ||||
4619 | Pred = ICmpInst::getSwappedPredicate(Pred); | ||||
4620 | std::swap(CmpLHS, CmpRHS); | ||||
4621 | } | ||||
4622 | if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) | ||||
4623 | break; | ||||
4624 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
4625 | case SPF_UMAX: | ||||
4626 | if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) { | ||||
4627 | Pred = ICmpInst::getSwappedPredicate(Pred); | ||||
4628 | std::swap(CmpLHS, CmpRHS); | ||||
4629 | } | ||||
4630 | if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) | ||||
4631 | break; | ||||
4632 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
4633 | default: | ||||
4634 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
4635 | } | ||||
4636 | |||||
4637 | // If there is a common operand in the already matched min/max and the other | ||||
4638 | // min/max operands match the compare operands (either directly or inverted), | ||||
4639 | // then this is min/max of the same flavor. | ||||
4640 | |||||
4641 | // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b)) | ||||
4642 | // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b)) | ||||
4643 | if (D == B) { | ||||
4644 | if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) && | ||||
4645 | match(A, m_Not(m_Specific(CmpRHS))))) | ||||
4646 | return {L.Flavor, SPNB_NA, false}; | ||||
4647 | } | ||||
4648 | // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d)) | ||||
4649 | // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d)) | ||||
4650 | if (C == B) { | ||||
4651 | if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) && | ||||
4652 | match(A, m_Not(m_Specific(CmpRHS))))) | ||||
4653 | return {L.Flavor, SPNB_NA, false}; | ||||
4654 | } | ||||
4655 | // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a)) | ||||
4656 | // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a)) | ||||
4657 | if (D == A) { | ||||
4658 | if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) && | ||||
4659 | match(B, m_Not(m_Specific(CmpRHS))))) | ||||
4660 | return {L.Flavor, SPNB_NA, false}; | ||||
4661 | } | ||||
4662 | // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d)) | ||||
4663 | // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d)) | ||||
4664 | if (C == A) { | ||||
4665 | if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) && | ||||
4666 | match(B, m_Not(m_Specific(CmpRHS))))) | ||||
4667 | return {L.Flavor, SPNB_NA, false}; | ||||
4668 | } | ||||
4669 | |||||
4670 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
4671 | } | ||||
4672 | |||||
4673 | /// Match non-obvious integer minimum and maximum sequences. | ||||
4674 | static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, | ||||
4675 | Value *CmpLHS, Value *CmpRHS, | ||||
4676 | Value *TrueVal, Value *FalseVal, | ||||
4677 | Value *&LHS, Value *&RHS, | ||||
4678 | unsigned Depth) { | ||||
4679 | // Assume success. If there's no match, callers should not use these anyway. | ||||
4680 | LHS = TrueVal; | ||||
4681 | RHS = FalseVal; | ||||
4682 | |||||
4683 | SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal); | ||||
4684 | if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN) | ||||
4685 | return SPR; | ||||
4686 | |||||
4687 | SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth); | ||||
4688 | if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN) | ||||
4689 | return SPR; | ||||
4690 | |||||
4691 | if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT) | ||||
4692 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
4693 | |||||
4694 | // Z = X -nsw Y | ||||
4695 | // (X >s Y) ? 0 : Z ==> (Z >s 0) ? 0 : Z ==> SMIN(Z, 0) | ||||
4696 | // (X <s Y) ? 0 : Z ==> (Z <s 0) ? 0 : Z ==> SMAX(Z, 0) | ||||
4697 | if (match(TrueVal, m_Zero()) && | ||||
4698 | match(FalseVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS)))) | ||||
4699 | return {Pred == CmpInst::ICMP_SGT ? SPF_SMIN : SPF_SMAX, SPNB_NA, false}; | ||||
4700 | |||||
4701 | // Z = X -nsw Y | ||||
4702 | // (X >s Y) ? Z : 0 ==> (Z >s 0) ? Z : 0 ==> SMAX(Z, 0) | ||||
4703 | // (X <s Y) ? Z : 0 ==> (Z <s 0) ? Z : 0 ==> SMIN(Z, 0) | ||||
4704 | if (match(FalseVal, m_Zero()) && | ||||
4705 | match(TrueVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS)))) | ||||
4706 | return {Pred == CmpInst::ICMP_SGT ? SPF_SMAX : SPF_SMIN, SPNB_NA, false}; | ||||
4707 | |||||
4708 | const APInt *C1; | ||||
4709 | if (!match(CmpRHS, m_APInt(C1))) | ||||
4710 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
4711 | |||||
4712 | // An unsigned min/max can be written with a signed compare. | ||||
4713 | const APInt *C2; | ||||
4714 | if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) || | ||||
4715 | (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) { | ||||
4716 | // Is the sign bit set? | ||||
4717 | // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX | ||||
4718 | // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN | ||||
4719 | if (Pred == CmpInst::ICMP_SLT && C1->isNullValue() && | ||||
4720 | C2->isMaxSignedValue()) | ||||
4721 | return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false}; | ||||
4722 | |||||
4723 | // Is the sign bit clear? | ||||
4724 | // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX | ||||
4725 | // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN | ||||
4726 | if (Pred == CmpInst::ICMP_SGT && C1->isAllOnesValue() && | ||||
4727 | C2->isMinSignedValue()) | ||||
4728 | return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false}; | ||||
4729 | } | ||||
4730 | |||||
4731 | // Look through 'not' ops to find disguised signed min/max. | ||||
4732 | // (X >s C) ? ~X : ~C ==> (~X <s ~C) ? ~X : ~C ==> SMIN(~X, ~C) | ||||
4733 | // (X <s C) ? ~X : ~C ==> (~X >s ~C) ? ~X : ~C ==> SMAX(~X, ~C) | ||||
4734 | if (match(TrueVal, m_Not(m_Specific(CmpLHS))) && | ||||
4735 | match(FalseVal, m_APInt(C2)) && ~(*C1) == *C2) | ||||
4736 | return {Pred == CmpInst::ICMP_SGT ? SPF_SMIN : SPF_SMAX, SPNB_NA, false}; | ||||
4737 | |||||
4738 | // (X >s C) ? ~C : ~X ==> (~X <s ~C) ? ~C : ~X ==> SMAX(~C, ~X) | ||||
4739 | // (X <s C) ? ~C : ~X ==> (~X >s ~C) ? ~C : ~X ==> SMIN(~C, ~X) | ||||
4740 | if (match(FalseVal, m_Not(m_Specific(CmpLHS))) && | ||||
4741 | match(TrueVal, m_APInt(C2)) && ~(*C1) == *C2) | ||||
4742 | return {Pred == CmpInst::ICMP_SGT ? SPF_SMAX : SPF_SMIN, SPNB_NA, false}; | ||||
4743 | |||||
4744 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
4745 | } | ||||
4746 | |||||
4747 | bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW) { | ||||
4748 | assert(X && Y && "Invalid operand")((X && Y && "Invalid operand") ? static_cast< void> (0) : __assert_fail ("X && Y && \"Invalid operand\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 4748, __PRETTY_FUNCTION__)); | ||||
4749 | |||||
4750 | // X = sub (0, Y) || X = sub nsw (0, Y) | ||||
4751 | if ((!NeedNSW && match(X, m_Sub(m_ZeroInt(), m_Specific(Y)))) || | ||||
4752 | (NeedNSW && match(X, m_NSWSub(m_ZeroInt(), m_Specific(Y))))) | ||||
4753 | return true; | ||||
4754 | |||||
4755 | // Y = sub (0, X) || Y = sub nsw (0, X) | ||||
4756 | if ((!NeedNSW && match(Y, m_Sub(m_ZeroInt(), m_Specific(X)))) || | ||||
4757 | (NeedNSW && match(Y, m_NSWSub(m_ZeroInt(), m_Specific(X))))) | ||||
4758 | return true; | ||||
4759 | |||||
4760 | // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A) | ||||
4761 | Value *A, *B; | ||||
4762 | return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) && | ||||
4763 | match(Y, m_Sub(m_Specific(B), m_Specific(A))))) || | ||||
4764 | (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) && | ||||
4765 | match(Y, m_NSWSub(m_Specific(B), m_Specific(A))))); | ||||
4766 | } | ||||
4767 | |||||
4768 | static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred, | ||||
4769 | FastMathFlags FMF, | ||||
4770 | Value *CmpLHS, Value *CmpRHS, | ||||
4771 | Value *TrueVal, Value *FalseVal, | ||||
4772 | Value *&LHS, Value *&RHS, | ||||
4773 | unsigned Depth) { | ||||
4774 | if (CmpInst::isFPPredicate(Pred)) { | ||||
4775 | // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one | ||||
4776 | // 0.0 operand, set the compare's 0.0 operands to that same value for the | ||||
4777 | // purpose of identifying min/max. Disregard vector constants with undefined | ||||
4778 | // elements because those can not be back-propagated for analysis. | ||||
4779 | Value *OutputZeroVal = nullptr; | ||||
4780 | if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) && | ||||
4781 | !cast<Constant>(TrueVal)->containsUndefElement()) | ||||
4782 | OutputZeroVal = TrueVal; | ||||
4783 | else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) && | ||||
4784 | !cast<Constant>(FalseVal)->containsUndefElement()) | ||||
4785 | OutputZeroVal = FalseVal; | ||||
4786 | |||||
4787 | if (OutputZeroVal) { | ||||
4788 | if (match(CmpLHS, m_AnyZeroFP())) | ||||
4789 | CmpLHS = OutputZeroVal; | ||||
4790 | if (match(CmpRHS, m_AnyZeroFP())) | ||||
4791 | CmpRHS = OutputZeroVal; | ||||
4792 | } | ||||
4793 | } | ||||
4794 | |||||
4795 | LHS = CmpLHS; | ||||
4796 | RHS = CmpRHS; | ||||
4797 | |||||
4798 | // Signed zero may return inconsistent results between implementations. | ||||
4799 | // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0 | ||||
4800 | // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1) | ||||
4801 | // Therefore, we behave conservatively and only proceed if at least one of the | ||||
4802 | // operands is known to not be zero or if we don't care about signed zero. | ||||
4803 | switch (Pred) { | ||||
4804 | default: break; | ||||
4805 | // FIXME: Include OGT/OLT/UGT/ULT. | ||||
4806 | case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLE: | ||||
4807 | case CmpInst::FCMP_UGE: case CmpInst::FCMP_ULE: | ||||
4808 | if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) && | ||||
4809 | !isKnownNonZero(CmpRHS)) | ||||
4810 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
4811 | } | ||||
4812 | |||||
4813 | SelectPatternNaNBehavior NaNBehavior = SPNB_NA; | ||||
4814 | bool Ordered = false; | ||||
4815 | |||||
4816 | // When given one NaN and one non-NaN input: | ||||
4817 | // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input. | ||||
4818 | // - A simple C99 (a < b ? a : b) construction will return 'b' (as the | ||||
4819 | // ordered comparison fails), which could be NaN or non-NaN. | ||||
4820 | // so here we discover exactly what NaN behavior is required/accepted. | ||||
4821 | if (CmpInst::isFPPredicate(Pred)) { | ||||
4822 | bool LHSSafe = isKnownNonNaN(CmpLHS, FMF); | ||||
4823 | bool RHSSafe = isKnownNonNaN(CmpRHS, FMF); | ||||
4824 | |||||
4825 | if (LHSSafe && RHSSafe) { | ||||
4826 | // Both operands are known non-NaN. | ||||
4827 | NaNBehavior = SPNB_RETURNS_ANY; | ||||
4828 | } else if (CmpInst::isOrdered(Pred)) { | ||||
4829 | // An ordered comparison will return false when given a NaN, so it | ||||
4830 | // returns the RHS. | ||||
4831 | Ordered = true; | ||||
4832 | if (LHSSafe) | ||||
4833 | // LHS is non-NaN, so if RHS is NaN then NaN will be returned. | ||||
4834 | NaNBehavior = SPNB_RETURNS_NAN; | ||||
4835 | else if (RHSSafe) | ||||
4836 | NaNBehavior = SPNB_RETURNS_OTHER; | ||||
4837 | else | ||||
4838 | // Completely unsafe. | ||||
4839 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
4840 | } else { | ||||
4841 | Ordered = false; | ||||
4842 | // An unordered comparison will return true when given a NaN, so it | ||||
4843 | // returns the LHS. | ||||
4844 | if (LHSSafe) | ||||
4845 | // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned. | ||||
4846 | NaNBehavior = SPNB_RETURNS_OTHER; | ||||
4847 | else if (RHSSafe) | ||||
4848 | NaNBehavior = SPNB_RETURNS_NAN; | ||||
4849 | else | ||||
4850 | // Completely unsafe. | ||||
4851 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
4852 | } | ||||
4853 | } | ||||
4854 | |||||
4855 | if (TrueVal == CmpRHS && FalseVal == CmpLHS) { | ||||
4856 | std::swap(CmpLHS, CmpRHS); | ||||
4857 | Pred = CmpInst::getSwappedPredicate(Pred); | ||||
4858 | if (NaNBehavior == SPNB_RETURNS_NAN) | ||||
4859 | NaNBehavior = SPNB_RETURNS_OTHER; | ||||
4860 | else if (NaNBehavior == SPNB_RETURNS_OTHER) | ||||
4861 | NaNBehavior = SPNB_RETURNS_NAN; | ||||
4862 | Ordered = !Ordered; | ||||
4863 | } | ||||
4864 | |||||
4865 | // ([if]cmp X, Y) ? X : Y | ||||
4866 | if (TrueVal == CmpLHS && FalseVal == CmpRHS) { | ||||
4867 | switch (Pred) { | ||||
4868 | default: return {SPF_UNKNOWN, SPNB_NA, false}; // Equality. | ||||
4869 | case ICmpInst::ICMP_UGT: | ||||
4870 | case ICmpInst::ICMP_UGE: return {SPF_UMAX, SPNB_NA, false}; | ||||
4871 | case ICmpInst::ICMP_SGT: | ||||
4872 | case ICmpInst::ICMP_SGE: return {SPF_SMAX, SPNB_NA, false}; | ||||
4873 | case ICmpInst::ICMP_ULT: | ||||
4874 | case ICmpInst::ICMP_ULE: return {SPF_UMIN, SPNB_NA, false}; | ||||
4875 | case ICmpInst::ICMP_SLT: | ||||
4876 | case ICmpInst::ICMP_SLE: return {SPF_SMIN, SPNB_NA, false}; | ||||
4877 | case FCmpInst::FCMP_UGT: | ||||
4878 | case FCmpInst::FCMP_UGE: | ||||
4879 | case FCmpInst::FCMP_OGT: | ||||
4880 | case FCmpInst::FCMP_OGE: return {SPF_FMAXNUM, NaNBehavior, Ordered}; | ||||
4881 | case FCmpInst::FCMP_ULT: | ||||
4882 | case FCmpInst::FCMP_ULE: | ||||
4883 | case FCmpInst::FCMP_OLT: | ||||
4884 | case FCmpInst::FCMP_OLE: return {SPF_FMINNUM, NaNBehavior, Ordered}; | ||||
4885 | } | ||||
4886 | } | ||||
4887 | |||||
4888 | if (isKnownNegation(TrueVal, FalseVal)) { | ||||
4889 | // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can | ||||
4890 | // match against either LHS or sext(LHS). | ||||
4891 | auto MaybeSExtCmpLHS = | ||||
4892 | m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS))); | ||||
4893 | auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes()); | ||||
4894 | auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One()); | ||||
4895 | if (match(TrueVal, MaybeSExtCmpLHS)) { | ||||
4896 | // Set the return values. If the compare uses the negated value (-X >s 0), | ||||
4897 | // swap the return values because the negated value is always 'RHS'. | ||||
4898 | LHS = TrueVal; | ||||
4899 | RHS = FalseVal; | ||||
4900 | if (match(CmpLHS, m_Neg(m_Specific(FalseVal)))) | ||||
4901 | std::swap(LHS, RHS); | ||||
4902 | |||||
4903 | // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X) | ||||
4904 | // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X) | ||||
4905 | if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes)) | ||||
4906 | return {SPF_ABS, SPNB_NA, false}; | ||||
4907 | |||||
4908 | // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X) | ||||
4909 | if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne)) | ||||
4910 | return {SPF_ABS, SPNB_NA, false}; | ||||
4911 | |||||
4912 | // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X) | ||||
4913 | // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X) | ||||
4914 | if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne)) | ||||
4915 | return {SPF_NABS, SPNB_NA, false}; | ||||
4916 | } | ||||
4917 | else if (match(FalseVal, MaybeSExtCmpLHS)) { | ||||
4918 | // Set the return values. If the compare uses the negated value (-X >s 0), | ||||
4919 | // swap the return values because the negated value is always 'RHS'. | ||||
4920 | LHS = FalseVal; | ||||
4921 | RHS = TrueVal; | ||||
4922 | if (match(CmpLHS, m_Neg(m_Specific(TrueVal)))) | ||||
4923 | std::swap(LHS, RHS); | ||||
4924 | |||||
4925 | // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X) | ||||
4926 | // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X) | ||||
4927 | if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes)) | ||||
4928 | return {SPF_NABS, SPNB_NA, false}; | ||||
4929 | |||||
4930 | // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X) | ||||
4931 | // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X) | ||||
4932 | if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne)) | ||||
4933 | return {SPF_ABS, SPNB_NA, false}; | ||||
4934 | } | ||||
4935 | } | ||||
4936 | |||||
4937 | if (CmpInst::isIntPredicate(Pred)) | ||||
4938 | return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth); | ||||
4939 | |||||
4940 | // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar | ||||
4941 | // may return either -0.0 or 0.0, so fcmp/select pair has stricter | ||||
4942 | // semantics than minNum. Be conservative in such case. | ||||
4943 | if (NaNBehavior != SPNB_RETURNS_ANY || | ||||
4944 | (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) && | ||||
4945 | !isKnownNonZero(CmpRHS))) | ||||
4946 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
4947 | |||||
4948 | return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS); | ||||
4949 | } | ||||
4950 | |||||
4951 | /// Helps to match a select pattern in case of a type mismatch. | ||||
4952 | /// | ||||
4953 | /// The function processes the case when type of true and false values of a | ||||
4954 | /// select instruction differs from type of the cmp instruction operands because | ||||
4955 | /// of a cast instruction. The function checks if it is legal to move the cast | ||||
4956 | /// operation after "select". If yes, it returns the new second value of | ||||
4957 | /// "select" (with the assumption that cast is moved): | ||||
4958 | /// 1. As operand of cast instruction when both values of "select" are same cast | ||||
4959 | /// instructions. | ||||
4960 | /// 2. As restored constant (by applying reverse cast operation) when the first | ||||
4961 | /// value of the "select" is a cast operation and the second value is a | ||||
4962 | /// constant. | ||||
4963 | /// NOTE: We return only the new second value because the first value could be | ||||
4964 | /// accessed as operand of cast instruction. | ||||
4965 | static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, | ||||
4966 | Instruction::CastOps *CastOp) { | ||||
4967 | auto *Cast1 = dyn_cast<CastInst>(V1); | ||||
4968 | if (!Cast1) | ||||
4969 | return nullptr; | ||||
4970 | |||||
4971 | *CastOp = Cast1->getOpcode(); | ||||
4972 | Type *SrcTy = Cast1->getSrcTy(); | ||||
4973 | if (auto *Cast2 = dyn_cast<CastInst>(V2)) { | ||||
4974 | // If V1 and V2 are both the same cast from the same type, look through V1. | ||||
4975 | if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy()) | ||||
4976 | return Cast2->getOperand(0); | ||||
4977 | return nullptr; | ||||
4978 | } | ||||
4979 | |||||
4980 | auto *C = dyn_cast<Constant>(V2); | ||||
4981 | if (!C) | ||||
4982 | return nullptr; | ||||
4983 | |||||
4984 | Constant *CastedTo = nullptr; | ||||
4985 | switch (*CastOp) { | ||||
4986 | case Instruction::ZExt: | ||||
4987 | if (CmpI->isUnsigned()) | ||||
4988 | CastedTo = ConstantExpr::getTrunc(C, SrcTy); | ||||
4989 | break; | ||||
4990 | case Instruction::SExt: | ||||
4991 | if (CmpI->isSigned()) | ||||
4992 | CastedTo = ConstantExpr::getTrunc(C, SrcTy, true); | ||||
4993 | break; | ||||
4994 | case Instruction::Trunc: | ||||
4995 | Constant *CmpConst; | ||||
4996 | if (match(CmpI->getOperand(1), m_Constant(CmpConst)) && | ||||
4997 | CmpConst->getType() == SrcTy) { | ||||
4998 | // Here we have the following case: | ||||
4999 | // | ||||
5000 | // %cond = cmp iN %x, CmpConst | ||||
5001 | // %tr = trunc iN %x to iK | ||||
5002 | // %narrowsel = select i1 %cond, iK %t, iK C | ||||
5003 | // | ||||
5004 | // We can always move trunc after select operation: | ||||
5005 | // | ||||
5006 | // %cond = cmp iN %x, CmpConst | ||||
5007 | // %widesel = select i1 %cond, iN %x, iN CmpConst | ||||
5008 | // %tr = trunc iN %widesel to iK | ||||
5009 | // | ||||
5010 | // Note that C could be extended in any way because we don't care about | ||||
5011 | // upper bits after truncation. It can't be abs pattern, because it would | ||||
5012 | // look like: | ||||
5013 | // | ||||
5014 | // select i1 %cond, x, -x. | ||||
5015 | // | ||||
5016 | // So only min/max pattern could be matched. Such match requires widened C | ||||
5017 | // == CmpConst. That is why set widened C = CmpConst, condition trunc | ||||
5018 | // CmpConst == C is checked below. | ||||
5019 | CastedTo = CmpConst; | ||||
5020 | } else { | ||||
5021 | CastedTo = ConstantExpr::getIntegerCast(C, SrcTy, CmpI->isSigned()); | ||||
5022 | } | ||||
5023 | break; | ||||
5024 | case Instruction::FPTrunc: | ||||
5025 | CastedTo = ConstantExpr::getFPExtend(C, SrcTy, true); | ||||
5026 | break; | ||||
5027 | case Instruction::FPExt: | ||||
5028 | CastedTo = ConstantExpr::getFPTrunc(C, SrcTy, true); | ||||
5029 | break; | ||||
5030 | case Instruction::FPToUI: | ||||
5031 | CastedTo = ConstantExpr::getUIToFP(C, SrcTy, true); | ||||
5032 | break; | ||||
5033 | case Instruction::FPToSI: | ||||
5034 | CastedTo = ConstantExpr::getSIToFP(C, SrcTy, true); | ||||
5035 | break; | ||||
5036 | case Instruction::UIToFP: | ||||
5037 | CastedTo = ConstantExpr::getFPToUI(C, SrcTy, true); | ||||
5038 | break; | ||||
5039 | case Instruction::SIToFP: | ||||
5040 | CastedTo = ConstantExpr::getFPToSI(C, SrcTy, true); | ||||
5041 | break; | ||||
5042 | default: | ||||
5043 | break; | ||||
5044 | } | ||||
5045 | |||||
5046 | if (!CastedTo) | ||||
5047 | return nullptr; | ||||
5048 | |||||
5049 | // Make sure the cast doesn't lose any information. | ||||
5050 | Constant *CastedBack = | ||||
5051 | ConstantExpr::getCast(*CastOp, CastedTo, C->getType(), true); | ||||
5052 | if (CastedBack != C) | ||||
5053 | return nullptr; | ||||
5054 | |||||
5055 | return CastedTo; | ||||
5056 | } | ||||
5057 | |||||
5058 | SelectPatternResult llvm::matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, | ||||
5059 | Instruction::CastOps *CastOp, | ||||
5060 | unsigned Depth) { | ||||
5061 | if (Depth >= MaxDepth) | ||||
5062 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
5063 | |||||
5064 | SelectInst *SI = dyn_cast<SelectInst>(V); | ||||
5065 | if (!SI) return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
5066 | |||||
5067 | CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition()); | ||||
5068 | if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
5069 | |||||
5070 | CmpInst::Predicate Pred = CmpI->getPredicate(); | ||||
5071 | Value *CmpLHS = CmpI->getOperand(0); | ||||
5072 | Value *CmpRHS = CmpI->getOperand(1); | ||||
5073 | Value *TrueVal = SI->getTrueValue(); | ||||
5074 | Value *FalseVal = SI->getFalseValue(); | ||||
5075 | FastMathFlags FMF; | ||||
5076 | if (isa<FPMathOperator>(CmpI)) | ||||
5077 | FMF = CmpI->getFastMathFlags(); | ||||
5078 | |||||
5079 | // Bail out early. | ||||
5080 | if (CmpI->isEquality()) | ||||
5081 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||
5082 | |||||
5083 | // Deal with type mismatches. | ||||
5084 | if (CastOp && CmpLHS->getType() != TrueVal->getType()) { | ||||
5085 | if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) { | ||||
5086 | // If this is a potential fmin/fmax with a cast to integer, then ignore | ||||
5087 | // -0.0 because there is no corresponding integer value. | ||||
5088 | if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI) | ||||
5089 | FMF.setNoSignedZeros(); | ||||
5090 | return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, | ||||
5091 | cast<CastInst>(TrueVal)->getOperand(0), C, | ||||
5092 | LHS, RHS, Depth); | ||||
5093 | } | ||||
5094 | if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) { | ||||
5095 | // If this is a potential fmin/fmax with a cast to integer, then ignore | ||||
5096 | // -0.0 because there is no corresponding integer value. | ||||
5097 | if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI) | ||||
5098 | FMF.setNoSignedZeros(); | ||||
5099 | return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, | ||||
5100 | C, cast<CastInst>(FalseVal)->getOperand(0), | ||||
5101 | LHS, RHS, Depth); | ||||
5102 | } | ||||
5103 | } | ||||
5104 | return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal, | ||||
5105 | LHS, RHS, Depth); | ||||
5106 | } | ||||
5107 | |||||
5108 | CmpInst::Predicate llvm::getMinMaxPred(SelectPatternFlavor SPF, bool Ordered) { | ||||
5109 | if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT; | ||||
5110 | if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT; | ||||
5111 | if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT; | ||||
5112 | if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT; | ||||
5113 | if (SPF == SPF_FMINNUM) | ||||
5114 | return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT; | ||||
5115 | if (SPF == SPF_FMAXNUM) | ||||
5116 | return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT; | ||||
5117 | llvm_unreachable("unhandled!")::llvm::llvm_unreachable_internal("unhandled!", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 5117); | ||||
5118 | } | ||||
5119 | |||||
5120 | SelectPatternFlavor llvm::getInverseMinMaxFlavor(SelectPatternFlavor SPF) { | ||||
5121 | if (SPF == SPF_SMIN) return SPF_SMAX; | ||||
5122 | if (SPF == SPF_UMIN) return SPF_UMAX; | ||||
5123 | if (SPF == SPF_SMAX) return SPF_SMIN; | ||||
5124 | if (SPF == SPF_UMAX) return SPF_UMIN; | ||||
5125 | llvm_unreachable("unhandled!")::llvm::llvm_unreachable_internal("unhandled!", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 5125); | ||||
5126 | } | ||||
5127 | |||||
5128 | CmpInst::Predicate llvm::getInverseMinMaxPred(SelectPatternFlavor SPF) { | ||||
5129 | return getMinMaxPred(getInverseMinMaxFlavor(SPF)); | ||||
5130 | } | ||||
5131 | |||||
5132 | /// Return true if "icmp Pred LHS RHS" is always true. | ||||
5133 | static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, | ||||
5134 | const Value *RHS, const DataLayout &DL, | ||||
5135 | unsigned Depth) { | ||||
5136 | assert(!LHS->getType()->isVectorTy() && "TODO: extend to handle vectors!")((!LHS->getType()->isVectorTy() && "TODO: extend to handle vectors!" ) ? static_cast<void> (0) : __assert_fail ("!LHS->getType()->isVectorTy() && \"TODO: extend to handle vectors!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 5136, __PRETTY_FUNCTION__)); | ||||
5137 | if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS) | ||||
5138 | return true; | ||||
5139 | |||||
5140 | switch (Pred) { | ||||
5141 | default: | ||||
5142 | return false; | ||||
5143 | |||||
5144 | case CmpInst::ICMP_SLE: { | ||||
5145 | const APInt *C; | ||||
5146 | |||||
5147 | // LHS s<= LHS +_{nsw} C if C >= 0 | ||||
5148 | if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C)))) | ||||
5149 | return !C->isNegative(); | ||||
5150 | return false; | ||||
5151 | } | ||||
5152 | |||||
5153 | case CmpInst::ICMP_ULE: { | ||||
5154 | const APInt *C; | ||||
5155 | |||||
5156 | // LHS u<= LHS +_{nuw} C for any C | ||||
5157 | if (match(RHS, m_NUWAdd(m_Specific(LHS), m_APInt(C)))) | ||||
5158 | return true; | ||||
5159 | |||||
5160 | // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB) | ||||
5161 | auto MatchNUWAddsToSameValue = [&](const Value *A, const Value *B, | ||||
5162 | const Value *&X, | ||||
5163 | const APInt *&CA, const APInt *&CB) { | ||||
5164 | if (match(A, m_NUWAdd(m_Value(X), m_APInt(CA))) && | ||||
5165 | match(B, m_NUWAdd(m_Specific(X), m_APInt(CB)))) | ||||
5166 | return true; | ||||
5167 | |||||
5168 | // If X & C == 0 then (X | C) == X +_{nuw} C | ||||
5169 | if (match(A, m_Or(m_Value(X), m_APInt(CA))) && | ||||
5170 | match(B, m_Or(m_Specific(X), m_APInt(CB)))) { | ||||
5171 | KnownBits Known(CA->getBitWidth()); | ||||
5172 | computeKnownBits(X, Known, DL, Depth + 1, /*AC*/ nullptr, | ||||
5173 | /*CxtI*/ nullptr, /*DT*/ nullptr); | ||||
5174 | if (CA->isSubsetOf(Known.Zero) && CB->isSubsetOf(Known.Zero)) | ||||
5175 | return true; | ||||
5176 | } | ||||
5177 | |||||
5178 | return false; | ||||
5179 | }; | ||||
5180 | |||||
5181 | const Value *X; | ||||
5182 | const APInt *CLHS, *CRHS; | ||||
5183 | if (MatchNUWAddsToSameValue(LHS, RHS, X, CLHS, CRHS)) | ||||
5184 | return CLHS->ule(*CRHS); | ||||
5185 | |||||
5186 | return false; | ||||
5187 | } | ||||
5188 | } | ||||
5189 | } | ||||
5190 | |||||
5191 | /// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred | ||||
5192 | /// ALHS ARHS" is true. Otherwise, return None. | ||||
5193 | static Optional<bool> | ||||
5194 | isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, | ||||
5195 | const Value *ARHS, const Value *BLHS, const Value *BRHS, | ||||
5196 | const DataLayout &DL, unsigned Depth) { | ||||
5197 | switch (Pred) { | ||||
5198 | default: | ||||
5199 | return None; | ||||
5200 | |||||
5201 | case CmpInst::ICMP_SLT: | ||||
5202 | case CmpInst::ICMP_SLE: | ||||
5203 | if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS, DL, Depth) && | ||||
5204 | isTruePredicate(CmpInst::ICMP_SLE, ARHS, BRHS, DL, Depth)) | ||||
5205 | return true; | ||||
5206 | return None; | ||||
5207 | |||||
5208 | case CmpInst::ICMP_ULT: | ||||
5209 | case CmpInst::ICMP_ULE: | ||||
5210 | if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS, DL, Depth) && | ||||
5211 | isTruePredicate(CmpInst::ICMP_ULE, ARHS, BRHS, DL, Depth)) | ||||
5212 | return true; | ||||
5213 | return None; | ||||
5214 | } | ||||
5215 | } | ||||
5216 | |||||
5217 | /// Return true if the operands of the two compares match. IsSwappedOps is true | ||||
5218 | /// when the operands match, but are swapped. | ||||
5219 | static bool isMatchingOps(const Value *ALHS, const Value *ARHS, | ||||
5220 | const Value *BLHS, const Value *BRHS, | ||||
5221 | bool &IsSwappedOps) { | ||||
5222 | |||||
5223 | bool IsMatchingOps = (ALHS == BLHS && ARHS == BRHS); | ||||
5224 | IsSwappedOps = (ALHS == BRHS && ARHS == BLHS); | ||||
5225 | return IsMatchingOps || IsSwappedOps; | ||||
5226 | } | ||||
5227 | |||||
5228 | /// Return true if "icmp1 APred X, Y" implies "icmp2 BPred X, Y" is true. | ||||
5229 | /// Return false if "icmp1 APred X, Y" implies "icmp2 BPred X, Y" is false. | ||||
5230 | /// Otherwise, return None if we can't infer anything. | ||||
5231 | static Optional<bool> isImpliedCondMatchingOperands(CmpInst::Predicate APred, | ||||
5232 | CmpInst::Predicate BPred, | ||||
5233 | bool AreSwappedOps) { | ||||
5234 | // Canonicalize the predicate as if the operands were not commuted. | ||||
5235 | if (AreSwappedOps) | ||||
5236 | BPred = ICmpInst::getSwappedPredicate(BPred); | ||||
5237 | |||||
5238 | if (CmpInst::isImpliedTrueByMatchingCmp(APred, BPred)) | ||||
5239 | return true; | ||||
5240 | if (CmpInst::isImpliedFalseByMatchingCmp(APred, BPred)) | ||||
5241 | return false; | ||||
5242 | |||||
5243 | return None; | ||||
5244 | } | ||||
5245 | |||||
5246 | /// Return true if "icmp APred X, C1" implies "icmp BPred X, C2" is true. | ||||
5247 | /// Return false if "icmp APred X, C1" implies "icmp BPred X, C2" is false. | ||||
5248 | /// Otherwise, return None if we can't infer anything. | ||||
5249 | static Optional<bool> | ||||
5250 | isImpliedCondMatchingImmOperands(CmpInst::Predicate APred, | ||||
5251 | const ConstantInt *C1, | ||||
5252 | CmpInst::Predicate BPred, | ||||
5253 | const ConstantInt *C2) { | ||||
5254 | ConstantRange DomCR = | ||||
5255 | ConstantRange::makeExactICmpRegion(APred, C1->getValue()); | ||||
5256 | ConstantRange CR = | ||||
5257 | ConstantRange::makeAllowedICmpRegion(BPred, C2->getValue()); | ||||
5258 | ConstantRange Intersection = DomCR.intersectWith(CR); | ||||
5259 | ConstantRange Difference = DomCR.difference(CR); | ||||
5260 | if (Intersection.isEmptySet()) | ||||
5261 | return false; | ||||
5262 | if (Difference.isEmptySet()) | ||||
5263 | return true; | ||||
5264 | return None; | ||||
5265 | } | ||||
5266 | |||||
5267 | /// Return true if LHS implies RHS is true. Return false if LHS implies RHS is | ||||
5268 | /// false. Otherwise, return None if we can't infer anything. | ||||
5269 | static Optional<bool> isImpliedCondICmps(const ICmpInst *LHS, | ||||
5270 | const ICmpInst *RHS, | ||||
5271 | const DataLayout &DL, bool LHSIsTrue, | ||||
5272 | unsigned Depth) { | ||||
5273 | Value *ALHS = LHS->getOperand(0); | ||||
5274 | Value *ARHS = LHS->getOperand(1); | ||||
5275 | // The rest of the logic assumes the LHS condition is true. If that's not the | ||||
5276 | // case, invert the predicate to make it so. | ||||
5277 | ICmpInst::Predicate APred = | ||||
5278 | LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate(); | ||||
5279 | |||||
5280 | Value *BLHS = RHS->getOperand(0); | ||||
5281 | Value *BRHS = RHS->getOperand(1); | ||||
5282 | ICmpInst::Predicate BPred = RHS->getPredicate(); | ||||
5283 | |||||
5284 | // Can we infer anything when the two compares have matching operands? | ||||
5285 | bool AreSwappedOps; | ||||
5286 | if (isMatchingOps(ALHS, ARHS, BLHS, BRHS, AreSwappedOps)) { | ||||
5287 | if (Optional<bool> Implication = isImpliedCondMatchingOperands( | ||||
5288 | APred, BPred, AreSwappedOps)) | ||||
5289 | return Implication; | ||||
5290 | // No amount of additional analysis will infer the second condition, so | ||||
5291 | // early exit. | ||||
5292 | return None; | ||||
5293 | } | ||||
5294 | |||||
5295 | // Can we infer anything when the LHS operands match and the RHS operands are | ||||
5296 | // constants (not necessarily matching)? | ||||
5297 | if (ALHS == BLHS && isa<ConstantInt>(ARHS) && isa<ConstantInt>(BRHS)) { | ||||
5298 | if (Optional<bool> Implication = isImpliedCondMatchingImmOperands( | ||||
5299 | APred, cast<ConstantInt>(ARHS), BPred, cast<ConstantInt>(BRHS))) | ||||
5300 | return Implication; | ||||
5301 | // No amount of additional analysis will infer the second condition, so | ||||
5302 | // early exit. | ||||
5303 | return None; | ||||
5304 | } | ||||
5305 | |||||
5306 | if (APred == BPred) | ||||
5307 | return isImpliedCondOperands(APred, ALHS, ARHS, BLHS, BRHS, DL, Depth); | ||||
5308 | return None; | ||||
5309 | } | ||||
5310 | |||||
5311 | /// Return true if LHS implies RHS is true. Return false if LHS implies RHS is | ||||
5312 | /// false. Otherwise, return None if we can't infer anything. We expect the | ||||
5313 | /// RHS to be an icmp and the LHS to be an 'and' or an 'or' instruction. | ||||
5314 | static Optional<bool> isImpliedCondAndOr(const BinaryOperator *LHS, | ||||
5315 | const ICmpInst *RHS, | ||||
5316 | const DataLayout &DL, bool LHSIsTrue, | ||||
5317 | unsigned Depth) { | ||||
5318 | // The LHS must be an 'or' or an 'and' instruction. | ||||
5319 | assert((LHS->getOpcode() == Instruction::And ||(((LHS->getOpcode() == Instruction::And || LHS->getOpcode () == Instruction::Or) && "Expected LHS to be 'and' or 'or'." ) ? static_cast<void> (0) : __assert_fail ("(LHS->getOpcode() == Instruction::And || LHS->getOpcode() == Instruction::Or) && \"Expected LHS to be 'and' or 'or'.\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 5321, __PRETTY_FUNCTION__)) | ||||
5320 | LHS->getOpcode() == Instruction::Or) &&(((LHS->getOpcode() == Instruction::And || LHS->getOpcode () == Instruction::Or) && "Expected LHS to be 'and' or 'or'." ) ? static_cast<void> (0) : __assert_fail ("(LHS->getOpcode() == Instruction::And || LHS->getOpcode() == Instruction::Or) && \"Expected LHS to be 'and' or 'or'.\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 5321, __PRETTY_FUNCTION__)) | ||||
5321 | "Expected LHS to be 'and' or 'or'.")(((LHS->getOpcode() == Instruction::And || LHS->getOpcode () == Instruction::Or) && "Expected LHS to be 'and' or 'or'." ) ? static_cast<void> (0) : __assert_fail ("(LHS->getOpcode() == Instruction::And || LHS->getOpcode() == Instruction::Or) && \"Expected LHS to be 'and' or 'or'.\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 5321, __PRETTY_FUNCTION__)); | ||||
5322 | |||||
5323 | assert(Depth <= MaxDepth && "Hit recursion limit")((Depth <= MaxDepth && "Hit recursion limit") ? static_cast <void> (0) : __assert_fail ("Depth <= MaxDepth && \"Hit recursion limit\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 5323, __PRETTY_FUNCTION__)); | ||||
5324 | |||||
5325 | // If the result of an 'or' is false, then we know both legs of the 'or' are | ||||
5326 | // false. Similarly, if the result of an 'and' is true, then we know both | ||||
5327 | // legs of the 'and' are true. | ||||
5328 | Value *ALHS, *ARHS; | ||||
5329 | if ((!LHSIsTrue && match(LHS, m_Or(m_Value(ALHS), m_Value(ARHS)))) || | ||||
5330 | (LHSIsTrue && match(LHS, m_And(m_Value(ALHS), m_Value(ARHS))))) { | ||||
5331 | // FIXME: Make this non-recursion. | ||||
5332 | if (Optional<bool> Implication = | ||||
5333 | isImpliedCondition(ALHS, RHS, DL, LHSIsTrue, Depth + 1)) | ||||
5334 | return Implication; | ||||
5335 | if (Optional<bool> Implication = | ||||
5336 | isImpliedCondition(ARHS, RHS, DL, LHSIsTrue, Depth + 1)) | ||||
5337 | return Implication; | ||||
5338 | return None; | ||||
5339 | } | ||||
5340 | return None; | ||||
5341 | } | ||||
5342 | |||||
5343 | Optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS, | ||||
5344 | const DataLayout &DL, bool LHSIsTrue, | ||||
5345 | unsigned Depth) { | ||||
5346 | // Bail out when we hit the limit. | ||||
5347 | if (Depth == MaxDepth) | ||||
5348 | return None; | ||||
5349 | |||||
5350 | // A mismatch occurs when we compare a scalar cmp to a vector cmp, for | ||||
5351 | // example. | ||||
5352 | if (LHS->getType() != RHS->getType()) | ||||
5353 | return None; | ||||
5354 | |||||
5355 | Type *OpTy = LHS->getType(); | ||||
5356 | assert(OpTy->isIntOrIntVectorTy(1) && "Expected integer type only!")((OpTy->isIntOrIntVectorTy(1) && "Expected integer type only!" ) ? static_cast<void> (0) : __assert_fail ("OpTy->isIntOrIntVectorTy(1) && \"Expected integer type only!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 5356, __PRETTY_FUNCTION__)); | ||||
5357 | |||||
5358 | // LHS ==> RHS by definition | ||||
5359 | if (LHS == RHS) | ||||
5360 | return LHSIsTrue; | ||||
5361 | |||||
5362 | // FIXME: Extending the code below to handle vectors. | ||||
5363 | if (OpTy->isVectorTy()) | ||||
5364 | return None; | ||||
5365 | |||||
5366 | assert(OpTy->isIntegerTy(1) && "implied by above")((OpTy->isIntegerTy(1) && "implied by above") ? static_cast <void> (0) : __assert_fail ("OpTy->isIntegerTy(1) && \"implied by above\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 5366, __PRETTY_FUNCTION__)); | ||||
5367 | |||||
5368 | // Both LHS and RHS are icmps. | ||||
5369 | const ICmpInst *LHSCmp = dyn_cast<ICmpInst>(LHS); | ||||
5370 | const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS); | ||||
5371 | if (LHSCmp && RHSCmp) | ||||
5372 | return isImpliedCondICmps(LHSCmp, RHSCmp, DL, LHSIsTrue, Depth); | ||||
5373 | |||||
5374 | // The LHS should be an 'or' or an 'and' instruction. We expect the RHS to be | ||||
5375 | // an icmp. FIXME: Add support for and/or on the RHS. | ||||
5376 | const BinaryOperator *LHSBO = dyn_cast<BinaryOperator>(LHS); | ||||
5377 | if (LHSBO && RHSCmp) { | ||||
5378 | if ((LHSBO->getOpcode() == Instruction::And || | ||||
5379 | LHSBO->getOpcode() == Instruction::Or)) | ||||
5380 | return isImpliedCondAndOr(LHSBO, RHSCmp, DL, LHSIsTrue, Depth); | ||||
5381 | } | ||||
5382 | return None; | ||||
5383 | } | ||||
5384 | |||||
5385 | Optional<bool> llvm::isImpliedByDomCondition(const Value *Cond, | ||||
5386 | const Instruction *ContextI, | ||||
5387 | const DataLayout &DL) { | ||||
5388 | assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool")((Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool" ) ? static_cast<void> (0) : __assert_fail ("Cond->getType()->isIntOrIntVectorTy(1) && \"Condition must be bool\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 5388, __PRETTY_FUNCTION__)); | ||||
5389 | if (!ContextI || !ContextI->getParent()) | ||||
5390 | return None; | ||||
5391 | |||||
5392 | // TODO: This is a poor/cheap way to determine dominance. Should we use a | ||||
5393 | // dominator tree (eg, from a SimplifyQuery) instead? | ||||
5394 | const BasicBlock *ContextBB = ContextI->getParent(); | ||||
5395 | const BasicBlock *PredBB = ContextBB->getSinglePredecessor(); | ||||
5396 | if (!PredBB) | ||||
5397 | return None; | ||||
5398 | |||||
5399 | // We need a conditional branch in the predecessor. | ||||
5400 | Value *PredCond; | ||||
5401 | BasicBlock *TrueBB, *FalseBB; | ||||
5402 | if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB))) | ||||
5403 | return None; | ||||
5404 | |||||
5405 | // The branch should get simplified. Don't bother simplifying this condition. | ||||
5406 | if (TrueBB == FalseBB) | ||||
5407 | return None; | ||||
5408 | |||||
5409 | assert((TrueBB == ContextBB || FalseBB == ContextBB) &&(((TrueBB == ContextBB || FalseBB == ContextBB) && "Predecessor block does not point to successor?" ) ? static_cast<void> (0) : __assert_fail ("(TrueBB == ContextBB || FalseBB == ContextBB) && \"Predecessor block does not point to successor?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 5410, __PRETTY_FUNCTION__)) | ||||
5410 | "Predecessor block does not point to successor?")(((TrueBB == ContextBB || FalseBB == ContextBB) && "Predecessor block does not point to successor?" ) ? static_cast<void> (0) : __assert_fail ("(TrueBB == ContextBB || FalseBB == ContextBB) && \"Predecessor block does not point to successor?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 5410, __PRETTY_FUNCTION__)); | ||||
5411 | |||||
5412 | // Is this condition implied by the predecessor condition? | ||||
5413 | bool CondIsTrue = TrueBB == ContextBB; | ||||
5414 | return isImpliedCondition(PredCond, Cond, DL, CondIsTrue); | ||||
5415 | } | ||||
5416 | |||||
5417 | static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower, | ||||
5418 | APInt &Upper, const InstrInfoQuery &IIQ) { | ||||
5419 | unsigned Width = Lower.getBitWidth(); | ||||
5420 | const APInt *C; | ||||
5421 | switch (BO.getOpcode()) { | ||||
5422 | case Instruction::Add: | ||||
5423 | if (match(BO.getOperand(1), m_APInt(C)) && !C->isNullValue()) { | ||||
5424 | // FIXME: If we have both nuw and nsw, we should reduce the range further. | ||||
5425 | if (IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(&BO))) { | ||||
5426 | // 'add nuw x, C' produces [C, UINT_MAX]. | ||||
5427 | Lower = *C; | ||||
5428 | } else if (IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(&BO))) { | ||||
5429 | if (C->isNegative()) { | ||||
5430 | // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C]. | ||||
5431 | Lower = APInt::getSignedMinValue(Width); | ||||
5432 | Upper = APInt::getSignedMaxValue(Width) + *C + 1; | ||||
5433 | } else { | ||||
5434 | // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX]. | ||||
5435 | Lower = APInt::getSignedMinValue(Width) + *C; | ||||
5436 | Upper = APInt::getSignedMaxValue(Width) + 1; | ||||
5437 | } | ||||
5438 | } | ||||
5439 | } | ||||
5440 | break; | ||||
5441 | |||||
5442 | case Instruction::And: | ||||
5443 | if (match(BO.getOperand(1), m_APInt(C))) | ||||
5444 | // 'and x, C' produces [0, C]. | ||||
5445 | Upper = *C + 1; | ||||
5446 | break; | ||||
5447 | |||||
5448 | case Instruction::Or: | ||||
5449 | if (match(BO.getOperand(1), m_APInt(C))) | ||||
5450 | // 'or x, C' produces [C, UINT_MAX]. | ||||
5451 | Lower = *C; | ||||
5452 | break; | ||||
5453 | |||||
5454 | case Instruction::AShr: | ||||
5455 | if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) { | ||||
5456 | // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C]. | ||||
5457 | Lower = APInt::getSignedMinValue(Width).ashr(*C); | ||||
5458 | Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1; | ||||
5459 | } else if (match(BO.getOperand(0), m_APInt(C))) { | ||||
5460 | unsigned ShiftAmount = Width - 1; | ||||
5461 | if (!C->isNullValue() && IIQ.isExact(&BO)) | ||||
5462 | ShiftAmount = C->countTrailingZeros(); | ||||
5463 | if (C->isNegative()) { | ||||
5464 | // 'ashr C, x' produces [C, C >> (Width-1)] | ||||
5465 | Lower = *C; | ||||
5466 | Upper = C->ashr(ShiftAmount) + 1; | ||||
5467 | } else { | ||||
5468 | // 'ashr C, x' produces [C >> (Width-1), C] | ||||
5469 | Lower = C->ashr(ShiftAmount); | ||||
5470 | Upper = *C + 1; | ||||
5471 | } | ||||
5472 | } | ||||
5473 | break; | ||||
5474 | |||||
5475 | case Instruction::LShr: | ||||
5476 | if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) { | ||||
5477 | // 'lshr x, C' produces [0, UINT_MAX >> C]. | ||||
5478 | Upper = APInt::getAllOnesValue(Width).lshr(*C) + 1; | ||||
5479 | } else if (match(BO.getOperand(0), m_APInt(C))) { | ||||
5480 | // 'lshr C, x' produces [C >> (Width-1), C]. | ||||
5481 | unsigned ShiftAmount = Width - 1; | ||||
5482 | if (!C->isNullValue() && IIQ.isExact(&BO)) | ||||
5483 | ShiftAmount = C->countTrailingZeros(); | ||||
5484 | Lower = C->lshr(ShiftAmount); | ||||
5485 | Upper = *C + 1; | ||||
5486 | } | ||||
5487 | break; | ||||
5488 | |||||
5489 | case Instruction::Shl: | ||||
5490 | if (match(BO.getOperand(0), m_APInt(C))) { | ||||
5491 | if (IIQ.hasNoUnsignedWrap(&BO)) { | ||||
5492 | // 'shl nuw C, x' produces [C, C << CLZ(C)] | ||||
5493 | Lower = *C; | ||||
5494 | Upper = Lower.shl(Lower.countLeadingZeros()) + 1; | ||||
5495 | } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw? | ||||
5496 | if (C->isNegative()) { | ||||
5497 | // 'shl nsw C, x' produces [C << CLO(C)-1, C] | ||||
5498 | unsigned ShiftAmount = C->countLeadingOnes() - 1; | ||||
5499 | Lower = C->shl(ShiftAmount); | ||||
5500 | Upper = *C + 1; | ||||
5501 | } else { | ||||
5502 | // 'shl nsw C, x' produces [C, C << CLZ(C)-1] | ||||
5503 | unsigned ShiftAmount = C->countLeadingZeros() - 1; | ||||
5504 | Lower = *C; | ||||
5505 | Upper = C->shl(ShiftAmount) + 1; | ||||
5506 | } | ||||
5507 | } | ||||
5508 | } | ||||
5509 | break; | ||||
5510 | |||||
5511 | case Instruction::SDiv: | ||||
5512 | if (match(BO.getOperand(1), m_APInt(C))) { | ||||
5513 | APInt IntMin = APInt::getSignedMinValue(Width); | ||||
5514 | APInt IntMax = APInt::getSignedMaxValue(Width); | ||||
5515 | if (C->isAllOnesValue()) { | ||||
5516 | // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX] | ||||
5517 | // where C != -1 and C != 0 and C != 1 | ||||
5518 | Lower = IntMin + 1; | ||||
5519 | Upper = IntMax + 1; | ||||
5520 | } else if (C->countLeadingZeros() < Width - 1) { | ||||
5521 | // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C] | ||||
5522 | // where C != -1 and C != 0 and C != 1 | ||||
5523 | Lower = IntMin.sdiv(*C); | ||||
5524 | Upper = IntMax.sdiv(*C); | ||||
5525 | if (Lower.sgt(Upper)) | ||||
5526 | std::swap(Lower, Upper); | ||||
5527 | Upper = Upper + 1; | ||||
5528 | assert(Upper != Lower && "Upper part of range has wrapped!")((Upper != Lower && "Upper part of range has wrapped!" ) ? static_cast<void> (0) : __assert_fail ("Upper != Lower && \"Upper part of range has wrapped!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 5528, __PRETTY_FUNCTION__)); | ||||
5529 | } | ||||
5530 | } else if (match(BO.getOperand(0), m_APInt(C))) { | ||||
5531 | if (C->isMinSignedValue()) { | ||||
5532 | // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2]. | ||||
5533 | Lower = *C; | ||||
5534 | Upper = Lower.lshr(1) + 1; | ||||
5535 | } else { | ||||
5536 | // 'sdiv C, x' produces [-|C|, |C|]. | ||||
5537 | Upper = C->abs() + 1; | ||||
5538 | Lower = (-Upper) + 1; | ||||
5539 | } | ||||
5540 | } | ||||
5541 | break; | ||||
5542 | |||||
5543 | case Instruction::UDiv: | ||||
5544 | if (match(BO.getOperand(1), m_APInt(C)) && !C->isNullValue()) { | ||||
5545 | // 'udiv x, C' produces [0, UINT_MAX / C]. | ||||
5546 | Upper = APInt::getMaxValue(Width).udiv(*C) + 1; | ||||
5547 | } else if (match(BO.getOperand(0), m_APInt(C))) { | ||||
5548 | // 'udiv C, x' produces [0, C]. | ||||
5549 | Upper = *C + 1; | ||||
5550 | } | ||||
5551 | break; | ||||
5552 | |||||
5553 | case Instruction::SRem: | ||||
5554 | if (match(BO.getOperand(1), m_APInt(C))) { | ||||
5555 | // 'srem x, C' produces (-|C|, |C|). | ||||
5556 | Upper = C->abs(); | ||||
5557 | Lower = (-Upper) + 1; | ||||
5558 | } | ||||
5559 | break; | ||||
5560 | |||||
5561 | case Instruction::URem: | ||||
5562 | if (match(BO.getOperand(1), m_APInt(C))) | ||||
5563 | // 'urem x, C' produces [0, C). | ||||
5564 | Upper = *C; | ||||
5565 | break; | ||||
5566 | |||||
5567 | default: | ||||
5568 | break; | ||||
5569 | } | ||||
5570 | } | ||||
5571 | |||||
5572 | static void setLimitsForIntrinsic(const IntrinsicInst &II, APInt &Lower, | ||||
5573 | APInt &Upper) { | ||||
5574 | unsigned Width = Lower.getBitWidth(); | ||||
5575 | const APInt *C; | ||||
5576 | switch (II.getIntrinsicID()) { | ||||
5577 | case Intrinsic::uadd_sat: | ||||
5578 | // uadd.sat(x, C) produces [C, UINT_MAX]. | ||||
5579 | if (match(II.getOperand(0), m_APInt(C)) || | ||||
5580 | match(II.getOperand(1), m_APInt(C))) | ||||
5581 | Lower = *C; | ||||
5582 | break; | ||||
5583 | case Intrinsic::sadd_sat: | ||||
5584 | if (match(II.getOperand(0), m_APInt(C)) || | ||||
5585 | match(II.getOperand(1), m_APInt(C))) { | ||||
5586 | if (C->isNegative()) { | ||||
5587 | // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)]. | ||||
5588 | Lower = APInt::getSignedMinValue(Width); | ||||
5589 | Upper = APInt::getSignedMaxValue(Width) + *C + 1; | ||||
5590 | } else { | ||||
5591 | // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX]. | ||||
5592 | Lower = APInt::getSignedMinValue(Width) + *C; | ||||
5593 | Upper = APInt::getSignedMaxValue(Width) + 1; | ||||
5594 | } | ||||
5595 | } | ||||
5596 | break; | ||||
5597 | case Intrinsic::usub_sat: | ||||
5598 | // usub.sat(C, x) produces [0, C]. | ||||
5599 | if (match(II.getOperand(0), m_APInt(C))) | ||||
5600 | Upper = *C + 1; | ||||
5601 | // usub.sat(x, C) produces [0, UINT_MAX - C]. | ||||
5602 | else if (match(II.getOperand(1), m_APInt(C))) | ||||
5603 | Upper = APInt::getMaxValue(Width) - *C + 1; | ||||
5604 | break; | ||||
5605 | case Intrinsic::ssub_sat: | ||||
5606 | if (match(II.getOperand(0), m_APInt(C))) { | ||||
5607 | if (C->isNegative()) { | ||||
5608 | // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)]. | ||||
5609 | Lower = APInt::getSignedMinValue(Width); | ||||
5610 | Upper = *C - APInt::getSignedMinValue(Width) + 1; | ||||
5611 | } else { | ||||
5612 | // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX]. | ||||
5613 | Lower = *C - APInt::getSignedMaxValue(Width); | ||||
5614 | Upper = APInt::getSignedMaxValue(Width) + 1; | ||||
5615 | } | ||||
5616 | } else if (match(II.getOperand(1), m_APInt(C))) { | ||||
5617 | if (C->isNegative()) { | ||||
5618 | // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]: | ||||
5619 | Lower = APInt::getSignedMinValue(Width) - *C; | ||||
5620 | Upper = APInt::getSignedMaxValue(Width) + 1; | ||||
5621 | } else { | ||||
5622 | // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C]. | ||||
5623 | Lower = APInt::getSignedMinValue(Width); | ||||
5624 | Upper = APInt::getSignedMaxValue(Width) - *C + 1; | ||||
5625 | } | ||||
5626 | } | ||||
5627 | break; | ||||
5628 | default: | ||||
5629 | break; | ||||
5630 | } | ||||
5631 | } | ||||
5632 | |||||
5633 | static void setLimitsForSelectPattern(const SelectInst &SI, APInt &Lower, | ||||
5634 | APInt &Upper) { | ||||
5635 | const Value *LHS, *RHS; | ||||
5636 | SelectPatternResult R = matchSelectPattern(&SI, LHS, RHS); | ||||
5637 | if (R.Flavor == SPF_UNKNOWN) | ||||
5638 | return; | ||||
5639 | |||||
5640 | unsigned BitWidth = SI.getType()->getScalarSizeInBits(); | ||||
5641 | |||||
5642 | if (R.Flavor == SelectPatternFlavor::SPF_ABS) { | ||||
5643 | // If the negation part of the abs (in RHS) has the NSW flag, | ||||
5644 | // then the result of abs(X) is [0..SIGNED_MAX], | ||||
5645 | // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN. | ||||
5646 | Lower = APInt::getNullValue(BitWidth); | ||||
5647 | if (cast<Instruction>(RHS)->hasNoSignedWrap()) | ||||
5648 | Upper = APInt::getSignedMaxValue(BitWidth) + 1; | ||||
5649 | else | ||||
5650 | Upper = APInt::getSignedMinValue(BitWidth) + 1; | ||||
5651 | return; | ||||
5652 | } | ||||
5653 | |||||
5654 | if (R.Flavor == SelectPatternFlavor::SPF_NABS) { | ||||
5655 | // The result of -abs(X) is <= 0. | ||||
5656 | Lower = APInt::getSignedMinValue(BitWidth); | ||||
5657 | Upper = APInt(BitWidth, 1); | ||||
5658 | return; | ||||
5659 | } | ||||
5660 | |||||
5661 | const APInt *C; | ||||
5662 | if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C))) | ||||
5663 | return; | ||||
5664 | |||||
5665 | switch (R.Flavor) { | ||||
5666 | case SPF_UMIN: | ||||
5667 | Upper = *C + 1; | ||||
5668 | break; | ||||
5669 | case SPF_UMAX: | ||||
5670 | Lower = *C; | ||||
5671 | break; | ||||
5672 | case SPF_SMIN: | ||||
5673 | Lower = APInt::getSignedMinValue(BitWidth); | ||||
5674 | Upper = *C + 1; | ||||
5675 | break; | ||||
5676 | case SPF_SMAX: | ||||
5677 | Lower = *C; | ||||
5678 | Upper = APInt::getSignedMaxValue(BitWidth) + 1; | ||||
5679 | break; | ||||
5680 | default: | ||||
5681 | break; | ||||
5682 | } | ||||
5683 | } | ||||
5684 | |||||
5685 | ConstantRange llvm::computeConstantRange(const Value *V, bool UseInstrInfo) { | ||||
5686 | assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction")((V->getType()->isIntOrIntVectorTy() && "Expected integer instruction" ) ? static_cast<void> (0) : __assert_fail ("V->getType()->isIntOrIntVectorTy() && \"Expected integer instruction\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Analysis/ValueTracking.cpp" , 5686, __PRETTY_FUNCTION__)); | ||||
5687 | |||||
5688 | const APInt *C; | ||||
5689 | if (match(V, m_APInt(C))) | ||||
5690 | return ConstantRange(*C); | ||||
5691 | |||||
5692 | InstrInfoQuery IIQ(UseInstrInfo); | ||||
5693 | unsigned BitWidth = V->getType()->getScalarSizeInBits(); | ||||
5694 | APInt Lower = APInt(BitWidth, 0); | ||||
5695 | APInt Upper = APInt(BitWidth, 0); | ||||
5696 | if (auto *BO = dyn_cast<BinaryOperator>(V)) | ||||
5697 | setLimitsForBinOp(*BO, Lower, Upper, IIQ); | ||||
5698 | else if (auto *II = dyn_cast<IntrinsicInst>(V)) | ||||
5699 | setLimitsForIntrinsic(*II, Lower, Upper); | ||||
5700 | else if (auto *SI = dyn_cast<SelectInst>(V)) | ||||
5701 | setLimitsForSelectPattern(*SI, Lower, Upper); | ||||
5702 | |||||
5703 | ConstantRange CR = ConstantRange::getNonEmpty(Lower, Upper); | ||||
5704 | |||||
5705 | if (auto *I = dyn_cast<Instruction>(V)) | ||||
5706 | if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range)) | ||||
5707 | CR = CR.intersectWith(getConstantRangeFromMetadata(*Range)); | ||||
5708 | |||||
5709 | return CR; | ||||
5710 | } |
1 | //===- PatternMatch.h - Match on the LLVM IR --------------------*- C++ -*-===// | |||
2 | // | |||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
4 | // See https://llvm.org/LICENSE.txt for license information. | |||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
6 | // | |||
7 | //===----------------------------------------------------------------------===// | |||
8 | // | |||
9 | // This file provides a simple and efficient mechanism for performing general | |||
10 | // tree-based pattern matches on the LLVM IR. The power of these routines is | |||
11 | // that it allows you to write concise patterns that are expressive and easy to | |||
12 | // understand. The other major advantage of this is that it allows you to | |||
13 | // trivially capture/bind elements in the pattern to variables. For example, | |||
14 | // you can do something like this: | |||
15 | // | |||
16 | // Value *Exp = ... | |||
17 | // Value *X, *Y; ConstantInt *C1, *C2; // (X & C1) | (Y & C2) | |||
18 | // if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)), | |||
19 | // m_And(m_Value(Y), m_ConstantInt(C2))))) { | |||
20 | // ... Pattern is matched and variables are bound ... | |||
21 | // } | |||
22 | // | |||
23 | // This is primarily useful to things like the instruction combiner, but can | |||
24 | // also be useful for static analysis tools or code generators. | |||
25 | // | |||
26 | //===----------------------------------------------------------------------===// | |||
27 | ||||
28 | #ifndef LLVM_IR_PATTERNMATCH_H | |||
29 | #define LLVM_IR_PATTERNMATCH_H | |||
30 | ||||
31 | #include "llvm/ADT/APFloat.h" | |||
32 | #include "llvm/ADT/APInt.h" | |||
33 | #include "llvm/IR/Constant.h" | |||
34 | #include "llvm/IR/Constants.h" | |||
35 | #include "llvm/IR/InstrTypes.h" | |||
36 | #include "llvm/IR/Instruction.h" | |||
37 | #include "llvm/IR/Instructions.h" | |||
38 | #include "llvm/IR/Intrinsics.h" | |||
39 | #include "llvm/IR/Operator.h" | |||
40 | #include "llvm/IR/Value.h" | |||
41 | #include "llvm/Support/Casting.h" | |||
42 | #include <cstdint> | |||
43 | ||||
44 | namespace llvm { | |||
45 | namespace PatternMatch { | |||
46 | ||||
47 | template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) { | |||
48 | return const_cast<Pattern &>(P).match(V); | |||
49 | } | |||
50 | ||||
51 | template <typename SubPattern_t> struct OneUse_match { | |||
52 | SubPattern_t SubPattern; | |||
53 | ||||
54 | OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {} | |||
55 | ||||
56 | template <typename OpTy> bool match(OpTy *V) { | |||
57 | return V->hasOneUse() && SubPattern.match(V); | |||
58 | } | |||
59 | }; | |||
60 | ||||
61 | template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) { | |||
62 | return SubPattern; | |||
63 | } | |||
64 | ||||
65 | template <typename Class> struct class_match { | |||
66 | template <typename ITy> bool match(ITy *V) { return isa<Class>(V); } | |||
67 | }; | |||
68 | ||||
69 | /// Match an arbitrary value and ignore it. | |||
70 | inline class_match<Value> m_Value() { return class_match<Value>(); } | |||
71 | ||||
72 | /// Match an arbitrary binary operation and ignore it. | |||
73 | inline class_match<BinaryOperator> m_BinOp() { | |||
74 | return class_match<BinaryOperator>(); | |||
75 | } | |||
76 | ||||
77 | /// Matches any compare instruction and ignore it. | |||
78 | inline class_match<CmpInst> m_Cmp() { return class_match<CmpInst>(); } | |||
79 | ||||
80 | /// Match an arbitrary ConstantInt and ignore it. | |||
81 | inline class_match<ConstantInt> m_ConstantInt() { | |||
82 | return class_match<ConstantInt>(); | |||
83 | } | |||
84 | ||||
85 | /// Match an arbitrary undef constant. | |||
86 | inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); } | |||
87 | ||||
88 | /// Match an arbitrary Constant and ignore it. | |||
89 | inline class_match<Constant> m_Constant() { return class_match<Constant>(); } | |||
90 | ||||
91 | /// Matching combinators | |||
92 | template <typename LTy, typename RTy> struct match_combine_or { | |||
93 | LTy L; | |||
94 | RTy R; | |||
95 | ||||
96 | match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {} | |||
97 | ||||
98 | template <typename ITy> bool match(ITy *V) { | |||
99 | if (L.match(V)) | |||
100 | return true; | |||
101 | if (R.match(V)) | |||
102 | return true; | |||
103 | return false; | |||
104 | } | |||
105 | }; | |||
106 | ||||
107 | template <typename LTy, typename RTy> struct match_combine_and { | |||
108 | LTy L; | |||
109 | RTy R; | |||
110 | ||||
111 | match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {} | |||
112 | ||||
113 | template <typename ITy> bool match(ITy *V) { | |||
114 | if (L.match(V)) | |||
115 | if (R.match(V)) | |||
116 | return true; | |||
117 | return false; | |||
118 | } | |||
119 | }; | |||
120 | ||||
121 | /// Combine two pattern matchers matching L || R | |||
122 | template <typename LTy, typename RTy> | |||
123 | inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) { | |||
124 | return match_combine_or<LTy, RTy>(L, R); | |||
125 | } | |||
126 | ||||
127 | /// Combine two pattern matchers matching L && R | |||
128 | template <typename LTy, typename RTy> | |||
129 | inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) { | |||
130 | return match_combine_and<LTy, RTy>(L, R); | |||
131 | } | |||
132 | ||||
133 | struct apint_match { | |||
134 | const APInt *&Res; | |||
135 | ||||
136 | apint_match(const APInt *&R) : Res(R) {} | |||
137 | ||||
138 | template <typename ITy> bool match(ITy *V) { | |||
139 | if (auto *CI = dyn_cast<ConstantInt>(V)) { | |||
140 | Res = &CI->getValue(); | |||
141 | return true; | |||
142 | } | |||
143 | if (V->getType()->isVectorTy()) | |||
144 | if (const auto *C = dyn_cast<Constant>(V)) | |||
145 | if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) { | |||
146 | Res = &CI->getValue(); | |||
147 | return true; | |||
148 | } | |||
149 | return false; | |||
150 | } | |||
151 | }; | |||
152 | // Either constexpr if or renaming ConstantFP::getValueAPF to | |||
153 | // ConstantFP::getValue is needed to do it via single template | |||
154 | // function for both apint/apfloat. | |||
155 | struct apfloat_match { | |||
156 | const APFloat *&Res; | |||
157 | apfloat_match(const APFloat *&R) : Res(R) {} | |||
158 | template <typename ITy> bool match(ITy *V) { | |||
159 | if (auto *CI = dyn_cast<ConstantFP>(V)) { | |||
160 | Res = &CI->getValueAPF(); | |||
161 | return true; | |||
162 | } | |||
163 | if (V->getType()->isVectorTy()) | |||
| ||||
164 | if (const auto *C = dyn_cast<Constant>(V)) | |||
165 | if (auto *CI = dyn_cast_or_null<ConstantFP>(C->getSplatValue())) { | |||
166 | Res = &CI->getValueAPF(); | |||
167 | return true; | |||
168 | } | |||
169 | return false; | |||
170 | } | |||
171 | }; | |||
172 | ||||
173 | /// Match a ConstantInt or splatted ConstantVector, binding the | |||
174 | /// specified pointer to the contained APInt. | |||
175 | inline apint_match m_APInt(const APInt *&Res) { return Res; } | |||
176 | ||||
177 | /// Match a ConstantFP or splatted ConstantVector, binding the | |||
178 | /// specified pointer to the contained APFloat. | |||
179 | inline apfloat_match m_APFloat(const APFloat *&Res) { return Res; } | |||
180 | ||||
181 | template <int64_t Val> struct constantint_match { | |||
182 | template <typename ITy> bool match(ITy *V) { | |||
183 | if (const auto *CI = dyn_cast<ConstantInt>(V)) { | |||
184 | const APInt &CIV = CI->getValue(); | |||
185 | if (Val >= 0) | |||
186 | return CIV == static_cast<uint64_t>(Val); | |||
187 | // If Val is negative, and CI is shorter than it, truncate to the right | |||
188 | // number of bits. If it is larger, then we have to sign extend. Just | |||
189 | // compare their negated values. | |||
190 | return -CIV == -Val; | |||
191 | } | |||
192 | return false; | |||
193 | } | |||
194 | }; | |||
195 | ||||
196 | /// Match a ConstantInt with a specific value. | |||
197 | template <int64_t Val> inline constantint_match<Val> m_ConstantInt() { | |||
198 | return constantint_match<Val>(); | |||
199 | } | |||
200 | ||||
201 | /// This helper class is used to match scalar and vector integer constants that | |||
202 | /// satisfy a specified predicate. | |||
203 | /// For vector constants, undefined elements are ignored. | |||
204 | template <typename Predicate> struct cst_pred_ty : public Predicate { | |||
205 | template <typename ITy> bool match(ITy *V) { | |||
206 | if (const auto *CI = dyn_cast<ConstantInt>(V)) | |||
207 | return this->isValue(CI->getValue()); | |||
208 | if (V->getType()->isVectorTy()) { | |||
209 | if (const auto *C = dyn_cast<Constant>(V)) { | |||
210 | if (const auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) | |||
211 | return this->isValue(CI->getValue()); | |||
212 | ||||
213 | // Non-splat vector constant: check each element for a match. | |||
214 | unsigned NumElts = V->getType()->getVectorNumElements(); | |||
215 | assert(NumElts != 0 && "Constant vector with no elements?")((NumElts != 0 && "Constant vector with no elements?" ) ? static_cast<void> (0) : __assert_fail ("NumElts != 0 && \"Constant vector with no elements?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/PatternMatch.h" , 215, __PRETTY_FUNCTION__)); | |||
216 | bool HasNonUndefElements = false; | |||
217 | for (unsigned i = 0; i != NumElts; ++i) { | |||
218 | Constant *Elt = C->getAggregateElement(i); | |||
219 | if (!Elt) | |||
220 | return false; | |||
221 | if (isa<UndefValue>(Elt)) | |||
222 | continue; | |||
223 | auto *CI = dyn_cast<ConstantInt>(Elt); | |||
224 | if (!CI || !this->isValue(CI->getValue())) | |||
225 | return false; | |||
226 | HasNonUndefElements = true; | |||
227 | } | |||
228 | return HasNonUndefElements; | |||
229 | } | |||
230 | } | |||
231 | return false; | |||
232 | } | |||
233 | }; | |||
234 | ||||
235 | /// This helper class is used to match scalar and vector constants that | |||
236 | /// satisfy a specified predicate, and bind them to an APInt. | |||
237 | template <typename Predicate> struct api_pred_ty : public Predicate { | |||
238 | const APInt *&Res; | |||
239 | ||||
240 | api_pred_ty(const APInt *&R) : Res(R) {} | |||
241 | ||||
242 | template <typename ITy> bool match(ITy *V) { | |||
243 | if (const auto *CI = dyn_cast<ConstantInt>(V)) | |||
244 | if (this->isValue(CI->getValue())) { | |||
245 | Res = &CI->getValue(); | |||
246 | return true; | |||
247 | } | |||
248 | if (V->getType()->isVectorTy()) | |||
249 | if (const auto *C = dyn_cast<Constant>(V)) | |||
250 | if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) | |||
251 | if (this->isValue(CI->getValue())) { | |||
252 | Res = &CI->getValue(); | |||
253 | return true; | |||
254 | } | |||
255 | ||||
256 | return false; | |||
257 | } | |||
258 | }; | |||
259 | ||||
260 | /// This helper class is used to match scalar and vector floating-point | |||
261 | /// constants that satisfy a specified predicate. | |||
262 | /// For vector constants, undefined elements are ignored. | |||
263 | template <typename Predicate> struct cstfp_pred_ty : public Predicate { | |||
264 | template <typename ITy> bool match(ITy *V) { | |||
265 | if (const auto *CF = dyn_cast<ConstantFP>(V)) | |||
266 | return this->isValue(CF->getValueAPF()); | |||
267 | if (V->getType()->isVectorTy()) { | |||
268 | if (const auto *C = dyn_cast<Constant>(V)) { | |||
269 | if (const auto *CF = dyn_cast_or_null<ConstantFP>(C->getSplatValue())) | |||
270 | return this->isValue(CF->getValueAPF()); | |||
271 | ||||
272 | // Non-splat vector constant: check each element for a match. | |||
273 | unsigned NumElts = V->getType()->getVectorNumElements(); | |||
274 | assert(NumElts != 0 && "Constant vector with no elements?")((NumElts != 0 && "Constant vector with no elements?" ) ? static_cast<void> (0) : __assert_fail ("NumElts != 0 && \"Constant vector with no elements?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/PatternMatch.h" , 274, __PRETTY_FUNCTION__)); | |||
275 | bool HasNonUndefElements = false; | |||
276 | for (unsigned i = 0; i != NumElts; ++i) { | |||
277 | Constant *Elt = C->getAggregateElement(i); | |||
278 | if (!Elt) | |||
279 | return false; | |||
280 | if (isa<UndefValue>(Elt)) | |||
281 | continue; | |||
282 | auto *CF = dyn_cast<ConstantFP>(Elt); | |||
283 | if (!CF || !this->isValue(CF->getValueAPF())) | |||
284 | return false; | |||
285 | HasNonUndefElements = true; | |||
286 | } | |||
287 | return HasNonUndefElements; | |||
288 | } | |||
289 | } | |||
290 | return false; | |||
291 | } | |||
292 | }; | |||
293 | ||||
294 | /////////////////////////////////////////////////////////////////////////////// | |||
295 | // | |||
296 | // Encapsulate constant value queries for use in templated predicate matchers. | |||
297 | // This allows checking if constants match using compound predicates and works | |||
298 | // with vector constants, possibly with relaxed constraints. For example, ignore | |||
299 | // undef values. | |||
300 | // | |||
301 | /////////////////////////////////////////////////////////////////////////////// | |||
302 | ||||
303 | struct is_all_ones { | |||
304 | bool isValue(const APInt &C) { return C.isAllOnesValue(); } | |||
305 | }; | |||
306 | /// Match an integer or vector with all bits set. | |||
307 | /// For vectors, this includes constants with undefined elements. | |||
308 | inline cst_pred_ty<is_all_ones> m_AllOnes() { | |||
309 | return cst_pred_ty<is_all_ones>(); | |||
310 | } | |||
311 | ||||
312 | struct is_maxsignedvalue { | |||
313 | bool isValue(const APInt &C) { return C.isMaxSignedValue(); } | |||
314 | }; | |||
315 | /// Match an integer or vector with values having all bits except for the high | |||
316 | /// bit set (0x7f...). | |||
317 | /// For vectors, this includes constants with undefined elements. | |||
318 | inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() { | |||
319 | return cst_pred_ty<is_maxsignedvalue>(); | |||
320 | } | |||
321 | inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) { | |||
322 | return V; | |||
323 | } | |||
324 | ||||
325 | struct is_negative { | |||
326 | bool isValue(const APInt &C) { return C.isNegative(); } | |||
327 | }; | |||
328 | /// Match an integer or vector of negative values. | |||
329 | /// For vectors, this includes constants with undefined elements. | |||
330 | inline cst_pred_ty<is_negative> m_Negative() { | |||
331 | return cst_pred_ty<is_negative>(); | |||
332 | } | |||
333 | inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { | |||
334 | return V; | |||
335 | } | |||
336 | ||||
337 | struct is_nonnegative { | |||
338 | bool isValue(const APInt &C) { return C.isNonNegative(); } | |||
339 | }; | |||
340 | /// Match an integer or vector of nonnegative values. | |||
341 | /// For vectors, this includes constants with undefined elements. | |||
342 | inline cst_pred_ty<is_nonnegative> m_NonNegative() { | |||
343 | return cst_pred_ty<is_nonnegative>(); | |||
344 | } | |||
345 | inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { | |||
346 | return V; | |||
347 | } | |||
348 | ||||
349 | struct is_one { | |||
350 | bool isValue(const APInt &C) { return C.isOneValue(); } | |||
351 | }; | |||
352 | /// Match an integer 1 or a vector with all elements equal to 1. | |||
353 | /// For vectors, this includes constants with undefined elements. | |||
354 | inline cst_pred_ty<is_one> m_One() { | |||
355 | return cst_pred_ty<is_one>(); | |||
356 | } | |||
357 | ||||
358 | struct is_zero_int { | |||
359 | bool isValue(const APInt &C) { return C.isNullValue(); } | |||
360 | }; | |||
361 | /// Match an integer 0 or a vector with all elements equal to 0. | |||
362 | /// For vectors, this includes constants with undefined elements. | |||
363 | inline cst_pred_ty<is_zero_int> m_ZeroInt() { | |||
364 | return cst_pred_ty<is_zero_int>(); | |||
365 | } | |||
366 | ||||
367 | struct is_zero { | |||
368 | template <typename ITy> bool match(ITy *V) { | |||
369 | auto *C = dyn_cast<Constant>(V); | |||
370 | return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C)); | |||
371 | } | |||
372 | }; | |||
373 | /// Match any null constant or a vector with all elements equal to 0. | |||
374 | /// For vectors, this includes constants with undefined elements. | |||
375 | inline is_zero m_Zero() { | |||
376 | return is_zero(); | |||
377 | } | |||
378 | ||||
379 | struct is_power2 { | |||
380 | bool isValue(const APInt &C) { return C.isPowerOf2(); } | |||
381 | }; | |||
382 | /// Match an integer or vector power-of-2. | |||
383 | /// For vectors, this includes constants with undefined elements. | |||
384 | inline cst_pred_ty<is_power2> m_Power2() { | |||
385 | return cst_pred_ty<is_power2>(); | |||
386 | } | |||
387 | inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { | |||
388 | return V; | |||
389 | } | |||
390 | ||||
391 | struct is_power2_or_zero { | |||
392 | bool isValue(const APInt &C) { return !C || C.isPowerOf2(); } | |||
393 | }; | |||
394 | /// Match an integer or vector of 0 or power-of-2 values. | |||
395 | /// For vectors, this includes constants with undefined elements. | |||
396 | inline cst_pred_ty<is_power2_or_zero> m_Power2OrZero() { | |||
397 | return cst_pred_ty<is_power2_or_zero>(); | |||
398 | } | |||
399 | inline api_pred_ty<is_power2_or_zero> m_Power2OrZero(const APInt *&V) { | |||
400 | return V; | |||
401 | } | |||
402 | ||||
403 | struct is_sign_mask { | |||
404 | bool isValue(const APInt &C) { return C.isSignMask(); } | |||
405 | }; | |||
406 | /// Match an integer or vector with only the sign bit(s) set. | |||
407 | /// For vectors, this includes constants with undefined elements. | |||
408 | inline cst_pred_ty<is_sign_mask> m_SignMask() { | |||
409 | return cst_pred_ty<is_sign_mask>(); | |||
410 | } | |||
411 | ||||
412 | struct is_lowbit_mask { | |||
413 | bool isValue(const APInt &C) { return C.isMask(); } | |||
414 | }; | |||
415 | /// Match an integer or vector with only the low bit(s) set. | |||
416 | /// For vectors, this includes constants with undefined elements. | |||
417 | inline cst_pred_ty<is_lowbit_mask> m_LowBitMask() { | |||
418 | return cst_pred_ty<is_lowbit_mask>(); | |||
419 | } | |||
420 | ||||
421 | struct is_nan { | |||
422 | bool isValue(const APFloat &C) { return C.isNaN(); } | |||
423 | }; | |||
424 | /// Match an arbitrary NaN constant. This includes quiet and signalling nans. | |||
425 | /// For vectors, this includes constants with undefined elements. | |||
426 | inline cstfp_pred_ty<is_nan> m_NaN() { | |||
427 | return cstfp_pred_ty<is_nan>(); | |||
428 | } | |||
429 | ||||
430 | struct is_any_zero_fp { | |||
431 | bool isValue(const APFloat &C) { return C.isZero(); } | |||
432 | }; | |||
433 | /// Match a floating-point negative zero or positive zero. | |||
434 | /// For vectors, this includes constants with undefined elements. | |||
435 | inline cstfp_pred_ty<is_any_zero_fp> m_AnyZeroFP() { | |||
436 | return cstfp_pred_ty<is_any_zero_fp>(); | |||
437 | } | |||
438 | ||||
439 | struct is_pos_zero_fp { | |||
440 | bool isValue(const APFloat &C) { return C.isPosZero(); } | |||
441 | }; | |||
442 | /// Match a floating-point positive zero. | |||
443 | /// For vectors, this includes constants with undefined elements. | |||
444 | inline cstfp_pred_ty<is_pos_zero_fp> m_PosZeroFP() { | |||
445 | return cstfp_pred_ty<is_pos_zero_fp>(); | |||
446 | } | |||
447 | ||||
448 | struct is_neg_zero_fp { | |||
449 | bool isValue(const APFloat &C) { return C.isNegZero(); } | |||
450 | }; | |||
451 | /// Match a floating-point negative zero. | |||
452 | /// For vectors, this includes constants with undefined elements. | |||
453 | inline cstfp_pred_ty<is_neg_zero_fp> m_NegZeroFP() { | |||
454 | return cstfp_pred_ty<is_neg_zero_fp>(); | |||
455 | } | |||
456 | ||||
457 | /////////////////////////////////////////////////////////////////////////////// | |||
458 | ||||
459 | template <typename Class> struct bind_ty { | |||
460 | Class *&VR; | |||
461 | ||||
462 | bind_ty(Class *&V) : VR(V) {} | |||
463 | ||||
464 | template <typename ITy> bool match(ITy *V) { | |||
465 | if (auto *CV = dyn_cast<Class>(V)) { | |||
466 | VR = CV; | |||
467 | return true; | |||
468 | } | |||
469 | return false; | |||
470 | } | |||
471 | }; | |||
472 | ||||
473 | /// Match a value, capturing it if we match. | |||
474 | inline bind_ty<Value> m_Value(Value *&V) { return V; } | |||
475 | inline bind_ty<const Value> m_Value(const Value *&V) { return V; } | |||
476 | ||||
477 | /// Match an instruction, capturing it if we match. | |||
478 | inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; } | |||
479 | /// Match a binary operator, capturing it if we match. | |||
480 | inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; } | |||
481 | ||||
482 | /// Match a ConstantInt, capturing the value if we match. | |||
483 | inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; } | |||
484 | ||||
485 | /// Match a Constant, capturing the value if we match. | |||
486 | inline bind_ty<Constant> m_Constant(Constant *&C) { return C; } | |||
487 | ||||
488 | /// Match a ConstantFP, capturing the value if we match. | |||
489 | inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; } | |||
490 | ||||
491 | /// Match a specified Value*. | |||
492 | struct specificval_ty { | |||
493 | const Value *Val; | |||
494 | ||||
495 | specificval_ty(const Value *V) : Val(V) {} | |||
496 | ||||
497 | template <typename ITy> bool match(ITy *V) { return V == Val; } | |||
498 | }; | |||
499 | ||||
500 | /// Match if we have a specific specified value. | |||
501 | inline specificval_ty m_Specific(const Value *V) { return V; } | |||
502 | ||||
503 | /// Stores a reference to the Value *, not the Value * itself, | |||
504 | /// thus can be used in commutative matchers. | |||
505 | template <typename Class> struct deferredval_ty { | |||
506 | Class *const &Val; | |||
507 | ||||
508 | deferredval_ty(Class *const &V) : Val(V) {} | |||
509 | ||||
510 | template <typename ITy> bool match(ITy *const V) { return V == Val; } | |||
511 | }; | |||
512 | ||||
513 | /// A commutative-friendly version of m_Specific(). | |||
514 | inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; } | |||
515 | inline deferredval_ty<const Value> m_Deferred(const Value *const &V) { | |||
516 | return V; | |||
517 | } | |||
518 | ||||
519 | /// Match a specified floating point value or vector of all elements of | |||
520 | /// that value. | |||
521 | struct specific_fpval { | |||
522 | double Val; | |||
523 | ||||
524 | specific_fpval(double V) : Val(V) {} | |||
525 | ||||
526 | template <typename ITy> bool match(ITy *V) { | |||
527 | if (const auto *CFP = dyn_cast<ConstantFP>(V)) | |||
528 | return CFP->isExactlyValue(Val); | |||
529 | if (V->getType()->isVectorTy()) | |||
530 | if (const auto *C = dyn_cast<Constant>(V)) | |||
531 | if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue())) | |||
532 | return CFP->isExactlyValue(Val); | |||
533 | return false; | |||
534 | } | |||
535 | }; | |||
536 | ||||
537 | /// Match a specific floating point value or vector with all elements | |||
538 | /// equal to the value. | |||
539 | inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); } | |||
540 | ||||
541 | /// Match a float 1.0 or vector with all elements equal to 1.0. | |||
542 | inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); } | |||
543 | ||||
544 | struct bind_const_intval_ty { | |||
545 | uint64_t &VR; | |||
546 | ||||
547 | bind_const_intval_ty(uint64_t &V) : VR(V) {} | |||
548 | ||||
549 | template <typename ITy> bool match(ITy *V) { | |||
550 | if (const auto *CV = dyn_cast<ConstantInt>(V)) | |||
551 | if (CV->getValue().ule(UINT64_MAX(18446744073709551615UL))) { | |||
552 | VR = CV->getZExtValue(); | |||
553 | return true; | |||
554 | } | |||
555 | return false; | |||
556 | } | |||
557 | }; | |||
558 | ||||
559 | /// Match a specified integer value or vector of all elements of that | |||
560 | // value. | |||
561 | struct specific_intval { | |||
562 | uint64_t Val; | |||
563 | ||||
564 | specific_intval(uint64_t V) : Val(V) {} | |||
565 | ||||
566 | template <typename ITy> bool match(ITy *V) { | |||
567 | const auto *CI = dyn_cast<ConstantInt>(V); | |||
568 | if (!CI && V->getType()->isVectorTy()) | |||
569 | if (const auto *C = dyn_cast<Constant>(V)) | |||
570 | CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()); | |||
571 | ||||
572 | return CI && CI->getValue() == Val; | |||
573 | } | |||
574 | }; | |||
575 | ||||
576 | /// Match a specific integer value or vector with all elements equal to | |||
577 | /// the value. | |||
578 | inline specific_intval m_SpecificInt(uint64_t V) { return specific_intval(V); } | |||
579 | ||||
580 | /// Match a ConstantInt and bind to its value. This does not match | |||
581 | /// ConstantInts wider than 64-bits. | |||
582 | inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; } | |||
583 | ||||
584 | //===----------------------------------------------------------------------===// | |||
585 | // Matcher for any binary operator. | |||
586 | // | |||
587 | template <typename LHS_t, typename RHS_t, bool Commutable = false> | |||
588 | struct AnyBinaryOp_match { | |||
589 | LHS_t L; | |||
590 | RHS_t R; | |||
591 | ||||
592 | // The evaluation order is always stable, regardless of Commutability. | |||
593 | // The LHS is always matched first. | |||
594 | AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} | |||
595 | ||||
596 | template <typename OpTy> bool match(OpTy *V) { | |||
597 | if (auto *I = dyn_cast<BinaryOperator>(V)) | |||
598 | return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) || | |||
599 | (Commutable && L.match(I->getOperand(1)) && | |||
600 | R.match(I->getOperand(0))); | |||
601 | return false; | |||
602 | } | |||
603 | }; | |||
604 | ||||
605 | template <typename LHS, typename RHS> | |||
606 | inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) { | |||
607 | return AnyBinaryOp_match<LHS, RHS>(L, R); | |||
608 | } | |||
609 | ||||
610 | //===----------------------------------------------------------------------===// | |||
611 | // Matchers for specific binary operators. | |||
612 | // | |||
613 | ||||
614 | template <typename LHS_t, typename RHS_t, unsigned Opcode, | |||
615 | bool Commutable = false> | |||
616 | struct BinaryOp_match { | |||
617 | LHS_t L; | |||
618 | RHS_t R; | |||
619 | ||||
620 | // The evaluation order is always stable, regardless of Commutability. | |||
621 | // The LHS is always matched first. | |||
622 | BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} | |||
623 | ||||
624 | template <typename OpTy> bool match(OpTy *V) { | |||
625 | if (V->getValueID() == Value::InstructionVal + Opcode) { | |||
626 | auto *I = cast<BinaryOperator>(V); | |||
627 | return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) || | |||
628 | (Commutable && L.match(I->getOperand(1)) && | |||
629 | R.match(I->getOperand(0))); | |||
630 | } | |||
631 | if (auto *CE = dyn_cast<ConstantExpr>(V)) | |||
632 | return CE->getOpcode() == Opcode && | |||
633 | ((L.match(CE->getOperand(0)) && R.match(CE->getOperand(1))) || | |||
634 | (Commutable && L.match(CE->getOperand(1)) && | |||
635 | R.match(CE->getOperand(0)))); | |||
636 | return false; | |||
637 | } | |||
638 | }; | |||
639 | ||||
640 | template <typename LHS, typename RHS> | |||
641 | inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L, | |||
642 | const RHS &R) { | |||
643 | return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R); | |||
644 | } | |||
645 | ||||
646 | template <typename LHS, typename RHS> | |||
647 | inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L, | |||
648 | const RHS &R) { | |||
649 | return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R); | |||
650 | } | |||
651 | ||||
652 | template <typename LHS, typename RHS> | |||
653 | inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L, | |||
654 | const RHS &R) { | |||
655 | return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R); | |||
656 | } | |||
657 | ||||
658 | template <typename LHS, typename RHS> | |||
659 | inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L, | |||
660 | const RHS &R) { | |||
661 | return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R); | |||
662 | } | |||
663 | ||||
664 | template <typename Op_t> struct FNeg_match { | |||
665 | Op_t X; | |||
666 | ||||
667 | FNeg_match(const Op_t &Op) : X(Op) {} | |||
668 | template <typename OpTy> bool match(OpTy *V) { | |||
669 | auto *FPMO = dyn_cast<FPMathOperator>(V); | |||
670 | if (!FPMO) return false; | |||
671 | ||||
672 | if (FPMO->getOpcode() == Instruction::FNeg) | |||
673 | return X.match(FPMO->getOperand(0)); | |||
674 | ||||
675 | if (FPMO->getOpcode() == Instruction::FSub) { | |||
676 | if (FPMO->hasNoSignedZeros()) { | |||
677 | // With 'nsz', any zero goes. | |||
678 | if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0))) | |||
679 | return false; | |||
680 | } else { | |||
681 | // Without 'nsz', we need fsub -0.0, X exactly. | |||
682 | if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0))) | |||
683 | return false; | |||
684 | } | |||
685 | ||||
686 | return X.match(FPMO->getOperand(1)); | |||
687 | } | |||
688 | ||||
689 | return false; | |||
690 | } | |||
691 | }; | |||
692 | ||||
693 | /// Match 'fneg X' as 'fsub -0.0, X'. | |||
694 | template <typename OpTy> | |||
695 | inline FNeg_match<OpTy> | |||
696 | m_FNeg(const OpTy &X) { | |||
697 | return FNeg_match<OpTy>(X); | |||
698 | } | |||
699 | ||||
700 | /// Match 'fneg X' as 'fsub +-0.0, X'. | |||
701 | template <typename RHS> | |||
702 | inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub> | |||
703 | m_FNegNSZ(const RHS &X) { | |||
704 | return m_FSub(m_AnyZeroFP(), X); | |||
705 | } | |||
706 | ||||
707 | template <typename LHS, typename RHS> | |||
708 | inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L, | |||
709 | const RHS &R) { | |||
710 | return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R); | |||
711 | } | |||
712 | ||||
713 | template <typename LHS, typename RHS> | |||
714 | inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L, | |||
715 | const RHS &R) { | |||
716 | return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R); | |||
717 | } | |||
718 | ||||
719 | template <typename LHS, typename RHS> | |||
720 | inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L, | |||
721 | const RHS &R) { | |||
722 | return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R); | |||
723 | } | |||
724 | ||||
725 | template <typename LHS, typename RHS> | |||
726 | inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L, | |||
727 | const RHS &R) { | |||
728 | return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R); | |||
729 | } | |||
730 | ||||
731 | template <typename LHS, typename RHS> | |||
732 | inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L, | |||
733 | const RHS &R) { | |||
734 | return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R); | |||
735 | } | |||
736 | ||||
737 | template <typename LHS, typename RHS> | |||
738 | inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L, | |||
739 | const RHS &R) { | |||
740 | return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R); | |||
741 | } | |||
742 | ||||
743 | template <typename LHS, typename RHS> | |||
744 | inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L, | |||
745 | const RHS &R) { | |||
746 | return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R); | |||
747 | } | |||
748 | ||||
749 | template <typename LHS, typename RHS> | |||
750 | inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L, | |||
751 | const RHS &R) { | |||
752 | return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R); | |||
753 | } | |||
754 | ||||
755 | template <typename LHS, typename RHS> | |||
756 | inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L, | |||
757 | const RHS &R) { | |||
758 | return BinaryOp_match<LHS, RHS, Instruction::And>(L, R); | |||
759 | } | |||
760 | ||||
761 | template <typename LHS, typename RHS> | |||
762 | inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L, | |||
763 | const RHS &R) { | |||
764 | return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R); | |||
765 | } | |||
766 | ||||
767 | template <typename LHS, typename RHS> | |||
768 | inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L, | |||
769 | const RHS &R) { | |||
770 | return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R); | |||
771 | } | |||
772 | ||||
773 | template <typename LHS, typename RHS> | |||
774 | inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L, | |||
775 | const RHS &R) { | |||
776 | return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R); | |||
777 | } | |||
778 | ||||
779 | template <typename LHS, typename RHS> | |||
780 | inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L, | |||
781 | const RHS &R) { | |||
782 | return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R); | |||
783 | } | |||
784 | ||||
785 | template <typename LHS, typename RHS> | |||
786 | inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L, | |||
787 | const RHS &R) { | |||
788 | return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R); | |||
789 | } | |||
790 | ||||
791 | template <typename LHS_t, typename RHS_t, unsigned Opcode, | |||
792 | unsigned WrapFlags = 0> | |||
793 | struct OverflowingBinaryOp_match { | |||
794 | LHS_t L; | |||
795 | RHS_t R; | |||
796 | ||||
797 | OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) | |||
798 | : L(LHS), R(RHS) {} | |||
799 | ||||
800 | template <typename OpTy> bool match(OpTy *V) { | |||
801 | if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) { | |||
802 | if (Op->getOpcode() != Opcode) | |||
803 | return false; | |||
804 | if (WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap && | |||
805 | !Op->hasNoUnsignedWrap()) | |||
806 | return false; | |||
807 | if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap && | |||
808 | !Op->hasNoSignedWrap()) | |||
809 | return false; | |||
810 | return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1)); | |||
811 | } | |||
812 | return false; | |||
813 | } | |||
814 | }; | |||
815 | ||||
816 | template <typename LHS, typename RHS> | |||
817 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, | |||
818 | OverflowingBinaryOperator::NoSignedWrap> | |||
819 | m_NSWAdd(const LHS &L, const RHS &R) { | |||
820 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, | |||
821 | OverflowingBinaryOperator::NoSignedWrap>( | |||
822 | L, R); | |||
823 | } | |||
824 | template <typename LHS, typename RHS> | |||
825 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, | |||
826 | OverflowingBinaryOperator::NoSignedWrap> | |||
827 | m_NSWSub(const LHS &L, const RHS &R) { | |||
828 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, | |||
829 | OverflowingBinaryOperator::NoSignedWrap>( | |||
830 | L, R); | |||
831 | } | |||
832 | template <typename LHS, typename RHS> | |||
833 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, | |||
834 | OverflowingBinaryOperator::NoSignedWrap> | |||
835 | m_NSWMul(const LHS &L, const RHS &R) { | |||
836 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, | |||
837 | OverflowingBinaryOperator::NoSignedWrap>( | |||
838 | L, R); | |||
839 | } | |||
840 | template <typename LHS, typename RHS> | |||
841 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, | |||
842 | OverflowingBinaryOperator::NoSignedWrap> | |||
843 | m_NSWShl(const LHS &L, const RHS &R) { | |||
844 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, | |||
845 | OverflowingBinaryOperator::NoSignedWrap>( | |||
846 | L, R); | |||
847 | } | |||
848 | ||||
849 | template <typename LHS, typename RHS> | |||
850 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, | |||
851 | OverflowingBinaryOperator::NoUnsignedWrap> | |||
852 | m_NUWAdd(const LHS &L, const RHS &R) { | |||
853 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, | |||
854 | OverflowingBinaryOperator::NoUnsignedWrap>( | |||
855 | L, R); | |||
856 | } | |||
857 | template <typename LHS, typename RHS> | |||
858 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, | |||
859 | OverflowingBinaryOperator::NoUnsignedWrap> | |||
860 | m_NUWSub(const LHS &L, const RHS &R) { | |||
861 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, | |||
862 | OverflowingBinaryOperator::NoUnsignedWrap>( | |||
863 | L, R); | |||
864 | } | |||
865 | template <typename LHS, typename RHS> | |||
866 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, | |||
867 | OverflowingBinaryOperator::NoUnsignedWrap> | |||
868 | m_NUWMul(const LHS &L, const RHS &R) { | |||
869 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, | |||
870 | OverflowingBinaryOperator::NoUnsignedWrap>( | |||
871 | L, R); | |||
872 | } | |||
873 | template <typename LHS, typename RHS> | |||
874 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, | |||
875 | OverflowingBinaryOperator::NoUnsignedWrap> | |||
876 | m_NUWShl(const LHS &L, const RHS &R) { | |||
877 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, | |||
878 | OverflowingBinaryOperator::NoUnsignedWrap>( | |||
879 | L, R); | |||
880 | } | |||
881 | ||||
882 | //===----------------------------------------------------------------------===// | |||
883 | // Class that matches a group of binary opcodes. | |||
884 | // | |||
885 | template <typename LHS_t, typename RHS_t, typename Predicate> | |||
886 | struct BinOpPred_match : Predicate { | |||
887 | LHS_t L; | |||
888 | RHS_t R; | |||
889 | ||||
890 | BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} | |||
891 | ||||
892 | template <typename OpTy> bool match(OpTy *V) { | |||
893 | if (auto *I = dyn_cast<Instruction>(V)) | |||
894 | return this->isOpType(I->getOpcode()) && L.match(I->getOperand(0)) && | |||
895 | R.match(I->getOperand(1)); | |||
896 | if (auto *CE = dyn_cast<ConstantExpr>(V)) | |||
897 | return this->isOpType(CE->getOpcode()) && L.match(CE->getOperand(0)) && | |||
898 | R.match(CE->getOperand(1)); | |||
899 | return false; | |||
900 | } | |||
901 | }; | |||
902 | ||||
903 | struct is_shift_op { | |||
904 | bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); } | |||
905 | }; | |||
906 | ||||
907 | struct is_right_shift_op { | |||
908 | bool isOpType(unsigned Opcode) { | |||
909 | return Opcode == Instruction::LShr || Opcode == Instruction::AShr; | |||
910 | } | |||
911 | }; | |||
912 | ||||
913 | struct is_logical_shift_op { | |||
914 | bool isOpType(unsigned Opcode) { | |||
915 | return Opcode == Instruction::LShr || Opcode == Instruction::Shl; | |||
916 | } | |||
917 | }; | |||
918 | ||||
919 | struct is_bitwiselogic_op { | |||
920 | bool isOpType(unsigned Opcode) { | |||
921 | return Instruction::isBitwiseLogicOp(Opcode); | |||
922 | } | |||
923 | }; | |||
924 | ||||
925 | struct is_idiv_op { | |||
926 | bool isOpType(unsigned Opcode) { | |||
927 | return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv; | |||
928 | } | |||
929 | }; | |||
930 | ||||
931 | /// Matches shift operations. | |||
932 | template <typename LHS, typename RHS> | |||
933 | inline BinOpPred_match<LHS, RHS, is_shift_op> m_Shift(const LHS &L, | |||
934 | const RHS &R) { | |||
935 | return BinOpPred_match<LHS, RHS, is_shift_op>(L, R); | |||
936 | } | |||
937 | ||||
938 | /// Matches logical shift operations. | |||
939 | template <typename LHS, typename RHS> | |||
940 | inline BinOpPred_match<LHS, RHS, is_right_shift_op> m_Shr(const LHS &L, | |||
941 | const RHS &R) { | |||
942 | return BinOpPred_match<LHS, RHS, is_right_shift_op>(L, R); | |||
943 | } | |||
944 | ||||
945 | /// Matches logical shift operations. | |||
946 | template <typename LHS, typename RHS> | |||
947 | inline BinOpPred_match<LHS, RHS, is_logical_shift_op> | |||
948 | m_LogicalShift(const LHS &L, const RHS &R) { | |||
949 | return BinOpPred_match<LHS, RHS, is_logical_shift_op>(L, R); | |||
950 | } | |||
951 | ||||
952 | /// Matches bitwise logic operations. | |||
953 | template <typename LHS, typename RHS> | |||
954 | inline BinOpPred_match<LHS, RHS, is_bitwiselogic_op> | |||
955 | m_BitwiseLogic(const LHS &L, const RHS &R) { | |||
956 | return BinOpPred_match<LHS, RHS, is_bitwiselogic_op>(L, R); | |||
957 | } | |||
958 | ||||
959 | /// Matches integer division operations. | |||
960 | template <typename LHS, typename RHS> | |||
961 | inline BinOpPred_match<LHS, RHS, is_idiv_op> m_IDiv(const LHS &L, | |||
962 | const RHS &R) { | |||
963 | return BinOpPred_match<LHS, RHS, is_idiv_op>(L, R); | |||
964 | } | |||
965 | ||||
966 | //===----------------------------------------------------------------------===// | |||
967 | // Class that matches exact binary ops. | |||
968 | // | |||
969 | template <typename SubPattern_t> struct Exact_match { | |||
970 | SubPattern_t SubPattern; | |||
971 | ||||
972 | Exact_match(const SubPattern_t &SP) : SubPattern(SP) {} | |||
973 | ||||
974 | template <typename OpTy> bool match(OpTy *V) { | |||
975 | if (auto *PEO = dyn_cast<PossiblyExactOperator>(V)) | |||
976 | return PEO->isExact() && SubPattern.match(V); | |||
977 | return false; | |||
978 | } | |||
979 | }; | |||
980 | ||||
981 | template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) { | |||
982 | return SubPattern; | |||
983 | } | |||
984 | ||||
985 | //===----------------------------------------------------------------------===// | |||
986 | // Matchers for CmpInst classes | |||
987 | // | |||
988 | ||||
989 | template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy, | |||
990 | bool Commutable = false> | |||
991 | struct CmpClass_match { | |||
992 | PredicateTy &Predicate; | |||
993 | LHS_t L; | |||
994 | RHS_t R; | |||
995 | ||||
996 | // The evaluation order is always stable, regardless of Commutability. | |||
997 | // The LHS is always matched first. | |||
998 | CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS) | |||
999 | : Predicate(Pred), L(LHS), R(RHS) {} | |||
1000 | ||||
1001 | template <typename OpTy> bool match(OpTy *V) { | |||
1002 | if (auto *I = dyn_cast<Class>(V)) | |||
1003 | if ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) || | |||
1004 | (Commutable && L.match(I->getOperand(1)) && | |||
1005 | R.match(I->getOperand(0)))) { | |||
1006 | Predicate = I->getPredicate(); | |||
1007 | return true; | |||
1008 | } | |||
1009 | return false; | |||
1010 | } | |||
1011 | }; | |||
1012 | ||||
1013 | template <typename LHS, typename RHS> | |||
1014 | inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate> | |||
1015 | m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) { | |||
1016 | return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R); | |||
1017 | } | |||
1018 | ||||
1019 | template <typename LHS, typename RHS> | |||
1020 | inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate> | |||
1021 | m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) { | |||
1022 | return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R); | |||
1023 | } | |||
1024 | ||||
1025 | template <typename LHS, typename RHS> | |||
1026 | inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate> | |||
1027 | m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) { | |||
1028 | return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R); | |||
1029 | } | |||
1030 | ||||
1031 | //===----------------------------------------------------------------------===// | |||
1032 | // Matchers for instructions with a given opcode and number of operands. | |||
1033 | // | |||
1034 | ||||
1035 | /// Matches instructions with Opcode and three operands. | |||
1036 | template <typename T0, unsigned Opcode> struct OneOps_match { | |||
1037 | T0 Op1; | |||
1038 | ||||
1039 | OneOps_match(const T0 &Op1) : Op1(Op1) {} | |||
1040 | ||||
1041 | template <typename OpTy> bool match(OpTy *V) { | |||
1042 | if (V->getValueID() == Value::InstructionVal + Opcode) { | |||
1043 | auto *I = cast<Instruction>(V); | |||
1044 | return Op1.match(I->getOperand(0)); | |||
1045 | } | |||
1046 | return false; | |||
1047 | } | |||
1048 | }; | |||
1049 | ||||
1050 | /// Matches instructions with Opcode and three operands. | |||
1051 | template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match { | |||
1052 | T0 Op1; | |||
1053 | T1 Op2; | |||
1054 | ||||
1055 | TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {} | |||
1056 | ||||
1057 | template <typename OpTy> bool match(OpTy *V) { | |||
1058 | if (V->getValueID() == Value::InstructionVal + Opcode) { | |||
1059 | auto *I = cast<Instruction>(V); | |||
1060 | return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)); | |||
1061 | } | |||
1062 | return false; | |||
1063 | } | |||
1064 | }; | |||
1065 | ||||
1066 | /// Matches instructions with Opcode and three operands. | |||
1067 | template <typename T0, typename T1, typename T2, unsigned Opcode> | |||
1068 | struct ThreeOps_match { | |||
1069 | T0 Op1; | |||
1070 | T1 Op2; | |||
1071 | T2 Op3; | |||
1072 | ||||
1073 | ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3) | |||
1074 | : Op1(Op1), Op2(Op2), Op3(Op3) {} | |||
1075 | ||||
1076 | template <typename OpTy> bool match(OpTy *V) { | |||
1077 | if (V->getValueID() == Value::InstructionVal + Opcode) { | |||
1078 | auto *I = cast<Instruction>(V); | |||
1079 | return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) && | |||
1080 | Op3.match(I->getOperand(2)); | |||
1081 | } | |||
1082 | return false; | |||
1083 | } | |||
1084 | }; | |||
1085 | ||||
1086 | /// Matches SelectInst. | |||
1087 | template <typename Cond, typename LHS, typename RHS> | |||
1088 | inline ThreeOps_match<Cond, LHS, RHS, Instruction::Select> | |||
1089 | m_Select(const Cond &C, const LHS &L, const RHS &R) { | |||
1090 | return ThreeOps_match<Cond, LHS, RHS, Instruction::Select>(C, L, R); | |||
1091 | } | |||
1092 | ||||
1093 | /// This matches a select of two constants, e.g.: | |||
1094 | /// m_SelectCst<-1, 0>(m_Value(V)) | |||
1095 | template <int64_t L, int64_t R, typename Cond> | |||
1096 | inline ThreeOps_match<Cond, constantint_match<L>, constantint_match<R>, | |||
1097 | Instruction::Select> | |||
1098 | m_SelectCst(const Cond &C) { | |||
1099 | return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>()); | |||
1100 | } | |||
1101 | ||||
1102 | /// Matches InsertElementInst. | |||
1103 | template <typename Val_t, typename Elt_t, typename Idx_t> | |||
1104 | inline ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement> | |||
1105 | m_InsertElement(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) { | |||
1106 | return ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>( | |||
1107 | Val, Elt, Idx); | |||
1108 | } | |||
1109 | ||||
1110 | /// Matches ExtractElementInst. | |||
1111 | template <typename Val_t, typename Idx_t> | |||
1112 | inline TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement> | |||
1113 | m_ExtractElement(const Val_t &Val, const Idx_t &Idx) { | |||
1114 | return TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>(Val, Idx); | |||
1115 | } | |||
1116 | ||||
1117 | /// Matches ShuffleVectorInst. | |||
1118 | template <typename V1_t, typename V2_t, typename Mask_t> | |||
1119 | inline ThreeOps_match<V1_t, V2_t, Mask_t, Instruction::ShuffleVector> | |||
1120 | m_ShuffleVector(const V1_t &v1, const V2_t &v2, const Mask_t &m) { | |||
1121 | return ThreeOps_match<V1_t, V2_t, Mask_t, Instruction::ShuffleVector>(v1, v2, | |||
1122 | m); | |||
1123 | } | |||
1124 | ||||
1125 | /// Matches LoadInst. | |||
1126 | template <typename OpTy> | |||
1127 | inline OneOps_match<OpTy, Instruction::Load> m_Load(const OpTy &Op) { | |||
1128 | return OneOps_match<OpTy, Instruction::Load>(Op); | |||
1129 | } | |||
1130 | ||||
1131 | /// Matches StoreInst. | |||
1132 | template <typename ValueOpTy, typename PointerOpTy> | |||
1133 | inline TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store> | |||
1134 | m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) { | |||
1135 | return TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>(ValueOp, | |||
1136 | PointerOp); | |||
1137 | } | |||
1138 | ||||
1139 | //===----------------------------------------------------------------------===// | |||
1140 | // Matchers for CastInst classes | |||
1141 | // | |||
1142 | ||||
1143 | template <typename Op_t, unsigned Opcode> struct CastClass_match { | |||
1144 | Op_t Op; | |||
1145 | ||||
1146 | CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {} | |||
1147 | ||||
1148 | template <typename OpTy> bool match(OpTy *V) { | |||
1149 | if (auto *O = dyn_cast<Operator>(V)) | |||
1150 | return O->getOpcode() == Opcode && Op.match(O->getOperand(0)); | |||
1151 | return false; | |||
1152 | } | |||
1153 | }; | |||
1154 | ||||
1155 | /// Matches BitCast. | |||
1156 | template <typename OpTy> | |||
1157 | inline CastClass_match<OpTy, Instruction::BitCast> m_BitCast(const OpTy &Op) { | |||
1158 | return CastClass_match<OpTy, Instruction::BitCast>(Op); | |||
1159 | } | |||
1160 | ||||
1161 | /// Matches PtrToInt. | |||
1162 | template <typename OpTy> | |||
1163 | inline CastClass_match<OpTy, Instruction::PtrToInt> m_PtrToInt(const OpTy &Op) { | |||
1164 | return CastClass_match<OpTy, Instruction::PtrToInt>(Op); | |||
1165 | } | |||
1166 | ||||
1167 | /// Matches Trunc. | |||
1168 | template <typename OpTy> | |||
1169 | inline CastClass_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) { | |||
1170 | return CastClass_match<OpTy, Instruction::Trunc>(Op); | |||
1171 | } | |||
1172 | ||||
1173 | /// Matches SExt. | |||
1174 | template <typename OpTy> | |||
1175 | inline CastClass_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) { | |||
1176 | return CastClass_match<OpTy, Instruction::SExt>(Op); | |||
1177 | } | |||
1178 | ||||
1179 | /// Matches ZExt. | |||
1180 | template <typename OpTy> | |||
1181 | inline CastClass_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) { | |||
1182 | return CastClass_match<OpTy, Instruction::ZExt>(Op); | |||
1183 | } | |||
1184 | ||||
1185 | template <typename OpTy> | |||
1186 | inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>, | |||
1187 | CastClass_match<OpTy, Instruction::SExt>> | |||
1188 | m_ZExtOrSExt(const OpTy &Op) { | |||
1189 | return m_CombineOr(m_ZExt(Op), m_SExt(Op)); | |||
1190 | } | |||
1191 | ||||
1192 | /// Matches UIToFP. | |||
1193 | template <typename OpTy> | |||
1194 | inline CastClass_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) { | |||
1195 | return CastClass_match<OpTy, Instruction::UIToFP>(Op); | |||
1196 | } | |||
1197 | ||||
1198 | /// Matches SIToFP. | |||
1199 | template <typename OpTy> | |||
1200 | inline CastClass_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) { | |||
1201 | return CastClass_match<OpTy, Instruction::SIToFP>(Op); | |||
1202 | } | |||
1203 | ||||
1204 | /// Matches FPTrunc | |||
1205 | template <typename OpTy> | |||
1206 | inline CastClass_match<OpTy, Instruction::FPTrunc> m_FPTrunc(const OpTy &Op) { | |||
1207 | return CastClass_match<OpTy, Instruction::FPTrunc>(Op); | |||
1208 | } | |||
1209 | ||||
1210 | /// Matches FPExt | |||
1211 | template <typename OpTy> | |||
1212 | inline CastClass_match<OpTy, Instruction::FPExt> m_FPExt(const OpTy &Op) { | |||
1213 | return CastClass_match<OpTy, Instruction::FPExt>(Op); | |||
1214 | } | |||
1215 | ||||
1216 | //===----------------------------------------------------------------------===// | |||
1217 | // Matchers for control flow. | |||
1218 | // | |||
1219 | ||||
1220 | struct br_match { | |||
1221 | BasicBlock *&Succ; | |||
1222 | ||||
1223 | br_match(BasicBlock *&Succ) : Succ(Succ) {} | |||
1224 | ||||
1225 | template <typename OpTy> bool match(OpTy *V) { | |||
1226 | if (auto *BI = dyn_cast<BranchInst>(V)) | |||
1227 | if (BI->isUnconditional()) { | |||
1228 | Succ = BI->getSuccessor(0); | |||
1229 | return true; | |||
1230 | } | |||
1231 | return false; | |||
1232 | } | |||
1233 | }; | |||
1234 | ||||
1235 | inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); } | |||
1236 | ||||
1237 | template <typename Cond_t> struct brc_match { | |||
1238 | Cond_t Cond; | |||
1239 | BasicBlock *&T, *&F; | |||
1240 | ||||
1241 | brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f) | |||
1242 | : Cond(C), T(t), F(f) {} | |||
1243 | ||||
1244 | template <typename OpTy> bool match(OpTy *V) { | |||
1245 | if (auto *BI = dyn_cast<BranchInst>(V)) | |||
1246 | if (BI->isConditional() && Cond.match(BI->getCondition())) { | |||
1247 | T = BI->getSuccessor(0); | |||
1248 | F = BI->getSuccessor(1); | |||
1249 | return true; | |||
1250 | } | |||
1251 | return false; | |||
1252 | } | |||
1253 | }; | |||
1254 | ||||
1255 | template <typename Cond_t> | |||
1256 | inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) { | |||
1257 | return brc_match<Cond_t>(C, T, F); | |||
1258 | } | |||
1259 | ||||
1260 | //===----------------------------------------------------------------------===// | |||
1261 | // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y). | |||
1262 | // | |||
1263 | ||||
1264 | template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t, | |||
1265 | bool Commutable = false> | |||
1266 | struct MaxMin_match { | |||
1267 | LHS_t L; | |||
1268 | RHS_t R; | |||
1269 | ||||
1270 | // The evaluation order is always stable, regardless of Commutability. | |||
1271 | // The LHS is always matched first. | |||
1272 | MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} | |||
1273 | ||||
1274 | template <typename OpTy> bool match(OpTy *V) { | |||
1275 | // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x". | |||
1276 | auto *SI = dyn_cast<SelectInst>(V); | |||
1277 | if (!SI) | |||
1278 | return false; | |||
1279 | auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition()); | |||
1280 | if (!Cmp) | |||
1281 | return false; | |||
1282 | // At this point we have a select conditioned on a comparison. Check that | |||
1283 | // it is the values returned by the select that are being compared. | |||
1284 | Value *TrueVal = SI->getTrueValue(); | |||
1285 | Value *FalseVal = SI->getFalseValue(); | |||
1286 | Value *LHS = Cmp->getOperand(0); | |||
1287 | Value *RHS = Cmp->getOperand(1); | |||
1288 | if ((TrueVal != LHS || FalseVal != RHS) && | |||
1289 | (TrueVal != RHS || FalseVal != LHS)) | |||
1290 | return false; | |||
1291 | typename CmpInst_t::Predicate Pred = | |||
1292 | LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate(); | |||
1293 | // Does "(x pred y) ? x : y" represent the desired max/min operation? | |||
1294 | if (!Pred_t::match(Pred)) | |||
1295 | return false; | |||
1296 | // It does! Bind the operands. | |||
1297 | return (L.match(LHS) && R.match(RHS)) || | |||
1298 | (Commutable && L.match(RHS) && R.match(LHS)); | |||
1299 | } | |||
1300 | }; | |||
1301 | ||||
1302 | /// Helper class for identifying signed max predicates. | |||
1303 | struct smax_pred_ty { | |||
1304 | static bool match(ICmpInst::Predicate Pred) { | |||
1305 | return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE; | |||
1306 | } | |||
1307 | }; | |||
1308 | ||||
1309 | /// Helper class for identifying signed min predicates. | |||
1310 | struct smin_pred_ty { | |||
1311 | static bool match(ICmpInst::Predicate Pred) { | |||
1312 | return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE; | |||
1313 | } | |||
1314 | }; | |||
1315 | ||||
1316 | /// Helper class for identifying unsigned max predicates. | |||
1317 | struct umax_pred_ty { | |||
1318 | static bool match(ICmpInst::Predicate Pred) { | |||
1319 | return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE; | |||
1320 | } | |||
1321 | }; | |||
1322 | ||||
1323 | /// Helper class for identifying unsigned min predicates. | |||
1324 | struct umin_pred_ty { | |||
1325 | static bool match(ICmpInst::Predicate Pred) { | |||
1326 | return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE; | |||
1327 | } | |||
1328 | }; | |||
1329 | ||||
1330 | /// Helper class for identifying ordered max predicates. | |||
1331 | struct ofmax_pred_ty { | |||
1332 | static bool match(FCmpInst::Predicate Pred) { | |||
1333 | return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE; | |||
1334 | } | |||
1335 | }; | |||
1336 | ||||
1337 | /// Helper class for identifying ordered min predicates. | |||
1338 | struct ofmin_pred_ty { | |||
1339 | static bool match(FCmpInst::Predicate Pred) { | |||
1340 | return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE; | |||
1341 | } | |||
1342 | }; | |||
1343 | ||||
1344 | /// Helper class for identifying unordered max predicates. | |||
1345 | struct ufmax_pred_ty { | |||
1346 | static bool match(FCmpInst::Predicate Pred) { | |||
1347 | return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE; | |||
1348 | } | |||
1349 | }; | |||
1350 | ||||
1351 | /// Helper class for identifying unordered min predicates. | |||
1352 | struct ufmin_pred_ty { | |||
1353 | static bool match(FCmpInst::Predicate Pred) { | |||
1354 | return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE; | |||
1355 | } | |||
1356 | }; | |||
1357 | ||||
1358 | template <typename LHS, typename RHS> | |||
1359 | inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L, | |||
1360 | const RHS &R) { | |||
1361 | return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R); | |||
1362 | } | |||
1363 | ||||
1364 | template <typename LHS, typename RHS> | |||
1365 | inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L, | |||
1366 | const RHS &R) { | |||
1367 | return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R); | |||
1368 | } | |||
1369 | ||||
1370 | template <typename LHS, typename RHS> | |||
1371 | inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L, | |||
1372 | const RHS &R) { | |||
1373 | return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R); | |||
1374 | } | |||
1375 | ||||
1376 | template <typename LHS, typename RHS> | |||
1377 | inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L, | |||
1378 | const RHS &R) { | |||
1379 | return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R); | |||
1380 | } | |||
1381 | ||||
1382 | /// Match an 'ordered' floating point maximum function. | |||
1383 | /// Floating point has one special value 'NaN'. Therefore, there is no total | |||
1384 | /// order. However, if we can ignore the 'NaN' value (for example, because of a | |||
1385 | /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum' | |||
1386 | /// semantics. In the presence of 'NaN' we have to preserve the original | |||
1387 | /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate. | |||
1388 | /// | |||
1389 | /// max(L, R) iff L and R are not NaN | |||
1390 | /// m_OrdFMax(L, R) = R iff L or R are NaN | |||
1391 | template <typename LHS, typename RHS> | |||
1392 | inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L, | |||
1393 | const RHS &R) { | |||
1394 | return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R); | |||
1395 | } | |||
1396 | ||||
1397 | /// Match an 'ordered' floating point minimum function. | |||
1398 | /// Floating point has one special value 'NaN'. Therefore, there is no total | |||
1399 | /// order. However, if we can ignore the 'NaN' value (for example, because of a | |||
1400 | /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum' | |||
1401 | /// semantics. In the presence of 'NaN' we have to preserve the original | |||
1402 | /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate. | |||
1403 | /// | |||
1404 | /// min(L, R) iff L and R are not NaN | |||
1405 | /// m_OrdFMin(L, R) = R iff L or R are NaN | |||
1406 | template <typename LHS, typename RHS> | |||
1407 | inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L, | |||
1408 | const RHS &R) { | |||
1409 | return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R); | |||
1410 | } | |||
1411 | ||||
1412 | /// Match an 'unordered' floating point maximum function. | |||
1413 | /// Floating point has one special value 'NaN'. Therefore, there is no total | |||
1414 | /// order. However, if we can ignore the 'NaN' value (for example, because of a | |||
1415 | /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum' | |||
1416 | /// semantics. In the presence of 'NaN' we have to preserve the original | |||
1417 | /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate. | |||
1418 | /// | |||
1419 | /// max(L, R) iff L and R are not NaN | |||
1420 | /// m_UnordFMax(L, R) = L iff L or R are NaN | |||
1421 | template <typename LHS, typename RHS> | |||
1422 | inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty> | |||
1423 | m_UnordFMax(const LHS &L, const RHS &R) { | |||
1424 | return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R); | |||
1425 | } | |||
1426 | ||||
1427 | /// Match an 'unordered' floating point minimum function. | |||
1428 | /// Floating point has one special value 'NaN'. Therefore, there is no total | |||
1429 | /// order. However, if we can ignore the 'NaN' value (for example, because of a | |||
1430 | /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum' | |||
1431 | /// semantics. In the presence of 'NaN' we have to preserve the original | |||
1432 | /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate. | |||
1433 | /// | |||
1434 | /// min(L, R) iff L and R are not NaN | |||
1435 | /// m_UnordFMin(L, R) = L iff L or R are NaN | |||
1436 | template <typename LHS, typename RHS> | |||
1437 | inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty> | |||
1438 | m_UnordFMin(const LHS &L, const RHS &R) { | |||
1439 | return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R); | |||
1440 | } | |||
1441 | ||||
1442 | //===----------------------------------------------------------------------===// | |||
1443 | // Matchers for overflow check patterns: e.g. (a + b) u< a | |||
1444 | // | |||
1445 | ||||
1446 | template <typename LHS_t, typename RHS_t, typename Sum_t> | |||
1447 | struct UAddWithOverflow_match { | |||
1448 | LHS_t L; | |||
1449 | RHS_t R; | |||
1450 | Sum_t S; | |||
1451 | ||||
1452 | UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S) | |||
1453 | : L(L), R(R), S(S) {} | |||
1454 | ||||
1455 | template <typename OpTy> bool match(OpTy *V) { | |||
1456 | Value *ICmpLHS, *ICmpRHS; | |||
1457 | ICmpInst::Predicate Pred; | |||
1458 | if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V)) | |||
1459 | return false; | |||
1460 | ||||
1461 | Value *AddLHS, *AddRHS; | |||
1462 | auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS)); | |||
1463 | ||||
1464 | // (a + b) u< a, (a + b) u< b | |||
1465 | if (Pred == ICmpInst::ICMP_ULT) | |||
1466 | if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS)) | |||
1467 | return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS); | |||
1468 | ||||
1469 | // a >u (a + b), b >u (a + b) | |||
1470 | if (Pred == ICmpInst::ICMP_UGT) | |||
1471 | if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS)) | |||
1472 | return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS); | |||
1473 | ||||
1474 | // Match special-case for increment-by-1. | |||
1475 | if (Pred == ICmpInst::ICMP_EQ) { | |||
1476 | // (a + 1) == 0 | |||
1477 | // (1 + a) == 0 | |||
1478 | if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) && | |||
1479 | (m_One().match(AddLHS) || m_One().match(AddRHS))) | |||
1480 | return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS); | |||
1481 | // 0 == (a + 1) | |||
1482 | // 0 == (1 + a) | |||
1483 | if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) && | |||
1484 | (m_One().match(AddLHS) || m_One().match(AddRHS))) | |||
1485 | return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS); | |||
1486 | } | |||
1487 | ||||
1488 | return false; | |||
1489 | } | |||
1490 | }; | |||
1491 | ||||
1492 | /// Match an icmp instruction checking for unsigned overflow on addition. | |||
1493 | /// | |||
1494 | /// S is matched to the addition whose result is being checked for overflow, and | |||
1495 | /// L and R are matched to the LHS and RHS of S. | |||
1496 | template <typename LHS_t, typename RHS_t, typename Sum_t> | |||
1497 | UAddWithOverflow_match<LHS_t, RHS_t, Sum_t> | |||
1498 | m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) { | |||
1499 | return UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>(L, R, S); | |||
1500 | } | |||
1501 | ||||
1502 | template <typename Opnd_t> struct Argument_match { | |||
1503 | unsigned OpI; | |||
1504 | Opnd_t Val; | |||
1505 | ||||
1506 | Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {} | |||
1507 | ||||
1508 | template <typename OpTy> bool match(OpTy *V) { | |||
1509 | // FIXME: Should likely be switched to use `CallBase`. | |||
1510 | if (const auto *CI = dyn_cast<CallInst>(V)) | |||
1511 | return Val.match(CI->getArgOperand(OpI)); | |||
1512 | return false; | |||
1513 | } | |||
1514 | }; | |||
1515 | ||||
1516 | /// Match an argument. | |||
1517 | template <unsigned OpI, typename Opnd_t> | |||
1518 | inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) { | |||
1519 | return Argument_match<Opnd_t>(OpI, Op); | |||
1520 | } | |||
1521 | ||||
1522 | /// Intrinsic matchers. | |||
1523 | struct IntrinsicID_match { | |||
1524 | unsigned ID; | |||
1525 | ||||
1526 | IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {} | |||
1527 | ||||
1528 | template <typename OpTy> bool match(OpTy *V) { | |||
1529 | if (const auto *CI = dyn_cast<CallInst>(V)) | |||
1530 | if (const auto *F = CI->getCalledFunction()) | |||
1531 | return F->getIntrinsicID() == ID; | |||
1532 | return false; | |||
1533 | } | |||
1534 | }; | |||
1535 | ||||
1536 | /// Intrinsic matches are combinations of ID matchers, and argument | |||
1537 | /// matchers. Higher arity matcher are defined recursively in terms of and-ing | |||
1538 | /// them with lower arity matchers. Here's some convenient typedefs for up to | |||
1539 | /// several arguments, and more can be added as needed | |||
1540 | template <typename T0 = void, typename T1 = void, typename T2 = void, | |||
1541 | typename T3 = void, typename T4 = void, typename T5 = void, | |||
1542 | typename T6 = void, typename T7 = void, typename T8 = void, | |||
1543 | typename T9 = void, typename T10 = void> | |||
1544 | struct m_Intrinsic_Ty; | |||
1545 | template <typename T0> struct m_Intrinsic_Ty<T0> { | |||
1546 | using Ty = match_combine_and<IntrinsicID_match, Argument_match<T0>>; | |||
1547 | }; | |||
1548 | template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> { | |||
1549 | using Ty = | |||
1550 | match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>; | |||
1551 | }; | |||
1552 | template <typename T0, typename T1, typename T2> | |||
1553 | struct m_Intrinsic_Ty<T0, T1, T2> { | |||
1554 | using Ty = | |||
1555 | match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty, | |||
1556 | Argument_match<T2>>; | |||
1557 | }; | |||
1558 | template <typename T0, typename T1, typename T2, typename T3> | |||
1559 | struct m_Intrinsic_Ty<T0, T1, T2, T3> { | |||
1560 | using Ty = | |||
1561 | match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty, | |||
1562 | Argument_match<T3>>; | |||
1563 | }; | |||
1564 | ||||
1565 | /// Match intrinsic calls like this: | |||
1566 | /// m_Intrinsic<Intrinsic::fabs>(m_Value(X)) | |||
1567 | template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() { | |||
1568 | return IntrinsicID_match(IntrID); | |||
1569 | } | |||
1570 | ||||
1571 | template <Intrinsic::ID IntrID, typename T0> | |||
1572 | inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) { | |||
1573 | return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0)); | |||
1574 | } | |||
1575 | ||||
1576 | template <Intrinsic::ID IntrID, typename T0, typename T1> | |||
1577 | inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0, | |||
1578 | const T1 &Op1) { | |||
1579 | return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1)); | |||
1580 | } | |||
1581 | ||||
1582 | template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2> | |||
1583 | inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty | |||
1584 | m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) { | |||
1585 | return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2)); | |||
1586 | } | |||
1587 | ||||
1588 | template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2, | |||
1589 | typename T3> | |||
1590 | inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty | |||
1591 | m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) { | |||
1592 | return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3)); | |||
1593 | } | |||
1594 | ||||
1595 | // Helper intrinsic matching specializations. | |||
1596 | template <typename Opnd0> | |||
1597 | inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) { | |||
1598 | return m_Intrinsic<Intrinsic::bitreverse>(Op0); | |||
1599 | } | |||
1600 | ||||
1601 | template <typename Opnd0> | |||
1602 | inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) { | |||
1603 | return m_Intrinsic<Intrinsic::bswap>(Op0); | |||
1604 | } | |||
1605 | ||||
1606 | template <typename Opnd0> | |||
1607 | inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) { | |||
1608 | return m_Intrinsic<Intrinsic::fabs>(Op0); | |||
1609 | } | |||
1610 | ||||
1611 | template <typename Opnd0> | |||
1612 | inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) { | |||
1613 | return m_Intrinsic<Intrinsic::canonicalize>(Op0); | |||
1614 | } | |||
1615 | ||||
1616 | template <typename Opnd0, typename Opnd1> | |||
1617 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0, | |||
1618 | const Opnd1 &Op1) { | |||
1619 | return m_Intrinsic<Intrinsic::minnum>(Op0, Op1); | |||
1620 | } | |||
1621 | ||||
1622 | template <typename Opnd0, typename Opnd1> | |||
1623 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0, | |||
1624 | const Opnd1 &Op1) { | |||
1625 | return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1); | |||
1626 | } | |||
1627 | ||||
1628 | //===----------------------------------------------------------------------===// | |||
1629 | // Matchers for two-operands operators with the operators in either order | |||
1630 | // | |||
1631 | ||||
1632 | /// Matches a BinaryOperator with LHS and RHS in either order. | |||
1633 | template <typename LHS, typename RHS> | |||
1634 | inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) { | |||
1635 | return AnyBinaryOp_match<LHS, RHS, true>(L, R); | |||
1636 | } | |||
1637 | ||||
1638 | /// Matches an ICmp with a predicate over LHS and RHS in either order. | |||
1639 | /// Does not swap the predicate. | |||
1640 | template <typename LHS, typename RHS> | |||
1641 | inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true> | |||
1642 | m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) { | |||
1643 | return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>(Pred, L, | |||
1644 | R); | |||
1645 | } | |||
1646 | ||||
1647 | /// Matches a Add with LHS and RHS in either order. | |||
1648 | template <typename LHS, typename RHS> | |||
1649 | inline BinaryOp_match<LHS, RHS, Instruction::Add, true> m_c_Add(const LHS &L, | |||
1650 | const RHS &R) { | |||
1651 | return BinaryOp_match<LHS, RHS, Instruction::Add, true>(L, R); | |||
1652 | } | |||
1653 | ||||
1654 | /// Matches a Mul with LHS and RHS in either order. | |||
1655 | template <typename LHS, typename RHS> | |||
1656 | inline BinaryOp_match<LHS, RHS, Instruction::Mul, true> m_c_Mul(const LHS &L, | |||
1657 | const RHS &R) { | |||
1658 | return BinaryOp_match<LHS, RHS, Instruction::Mul, true>(L, R); | |||
1659 | } | |||
1660 | ||||
1661 | /// Matches an And with LHS and RHS in either order. | |||
1662 | template <typename LHS, typename RHS> | |||
1663 | inline BinaryOp_match<LHS, RHS, Instruction::And, true> m_c_And(const LHS &L, | |||
1664 | const RHS &R) { | |||
1665 | return BinaryOp_match<LHS, RHS, Instruction::And, true>(L, R); | |||
1666 | } | |||
1667 | ||||
1668 | /// Matches an Or with LHS and RHS in either order. | |||
1669 | template <typename LHS, typename RHS> | |||
1670 | inline BinaryOp_match<LHS, RHS, Instruction::Or, true> m_c_Or(const LHS &L, | |||
1671 | const RHS &R) { | |||
1672 | return BinaryOp_match<LHS, RHS, Instruction::Or, true>(L, R); | |||
1673 | } | |||
1674 | ||||
1675 | /// Matches an Xor with LHS and RHS in either order. | |||
1676 | template <typename LHS, typename RHS> | |||
1677 | inline BinaryOp_match<LHS, RHS, Instruction::Xor, true> m_c_Xor(const LHS &L, | |||
1678 | const RHS &R) { | |||
1679 | return BinaryOp_match<LHS, RHS, Instruction::Xor, true>(L, R); | |||
1680 | } | |||
1681 | ||||
1682 | /// Matches a 'Neg' as 'sub 0, V'. | |||
1683 | template <typename ValTy> | |||
1684 | inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub> | |||
1685 | m_Neg(const ValTy &V) { | |||
1686 | return m_Sub(m_ZeroInt(), V); | |||
1687 | } | |||
1688 | ||||
1689 | /// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'. | |||
1690 | template <typename ValTy> | |||
1691 | inline BinaryOp_match<ValTy, cst_pred_ty<is_all_ones>, Instruction::Xor, true> | |||
1692 | m_Not(const ValTy &V) { | |||
1693 | return m_c_Xor(V, m_AllOnes()); | |||
1694 | } | |||
1695 | ||||
1696 | /// Matches an SMin with LHS and RHS in either order. | |||
1697 | template <typename LHS, typename RHS> | |||
1698 | inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true> | |||
1699 | m_c_SMin(const LHS &L, const RHS &R) { | |||
1700 | return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>(L, R); | |||
1701 | } | |||
1702 | /// Matches an SMax with LHS and RHS in either order. | |||
1703 | template <typename LHS, typename RHS> | |||
1704 | inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true> | |||
1705 | m_c_SMax(const LHS &L, const RHS &R) { | |||
1706 | return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>(L, R); | |||
1707 | } | |||
1708 | /// Matches a UMin with LHS and RHS in either order. | |||
1709 | template <typename LHS, typename RHS> | |||
1710 | inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true> | |||
1711 | m_c_UMin(const LHS &L, const RHS &R) { | |||
1712 | return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>(L, R); | |||
1713 | } | |||
1714 | /// Matches a UMax with LHS and RHS in either order. | |||
1715 | template <typename LHS, typename RHS> | |||
1716 | inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true> | |||
1717 | m_c_UMax(const LHS &L, const RHS &R) { | |||
1718 | return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>(L, R); | |||
1719 | } | |||
1720 | ||||
1721 | /// Matches FAdd with LHS and RHS in either order. | |||
1722 | template <typename LHS, typename RHS> | |||
1723 | inline BinaryOp_match<LHS, RHS, Instruction::FAdd, true> | |||
1724 | m_c_FAdd(const LHS &L, const RHS &R) { | |||
1725 | return BinaryOp_match<LHS, RHS, Instruction::FAdd, true>(L, R); | |||
1726 | } | |||
1727 | ||||
1728 | /// Matches FMul with LHS and RHS in either order. | |||
1729 | template <typename LHS, typename RHS> | |||
1730 | inline BinaryOp_match<LHS, RHS, Instruction::FMul, true> | |||
1731 | m_c_FMul(const LHS &L, const RHS &R) { | |||
1732 | return BinaryOp_match<LHS, RHS, Instruction::FMul, true>(L, R); | |||
1733 | } | |||
1734 | ||||
1735 | template <typename Opnd_t> struct Signum_match { | |||
1736 | Opnd_t Val; | |||
1737 | Signum_match(const Opnd_t &V) : Val(V) {} | |||
1738 | ||||
1739 | template <typename OpTy> bool match(OpTy *V) { | |||
1740 | unsigned TypeSize = V->getType()->getScalarSizeInBits(); | |||
1741 | if (TypeSize == 0) | |||
1742 | return false; | |||
1743 | ||||
1744 | unsigned ShiftWidth = TypeSize - 1; | |||
1745 | Value *OpL = nullptr, *OpR = nullptr; | |||
1746 | ||||
1747 | // This is the representation of signum we match: | |||
1748 | // | |||
1749 | // signum(x) == (x >> 63) | (-x >>u 63) | |||
1750 | // | |||
1751 | // An i1 value is its own signum, so it's correct to match | |||
1752 | // | |||
1753 | // signum(x) == (x >> 0) | (-x >>u 0) | |||
1754 | // | |||
1755 | // for i1 values. | |||
1756 | ||||
1757 | auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth)); | |||
1758 | auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth)); | |||
1759 | auto Signum = m_Or(LHS, RHS); | |||
1760 | ||||
1761 | return Signum.match(V) && OpL == OpR && Val.match(OpL); | |||
1762 | } | |||
1763 | }; | |||
1764 | ||||
1765 | /// Matches a signum pattern. | |||
1766 | /// | |||
1767 | /// signum(x) = | |||
1768 | /// x > 0 -> 1 | |||
1769 | /// x == 0 -> 0 | |||
1770 | /// x < 0 -> -1 | |||
1771 | template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) { | |||
1772 | return Signum_match<Val_t>(V); | |||
1773 | } | |||
1774 | ||||
1775 | } // end namespace PatternMatch | |||
1776 | } // end namespace llvm | |||
1777 | ||||
1778 | #endif // LLVM_IR_PATTERNMATCH_H |
1 | //===- llvm/InstrTypes.h - Important Instruction subclasses -----*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file defines various meta classes of instructions that exist in the VM |
10 | // representation. Specific concrete subclasses of these may be found in the |
11 | // i*.h files... |
12 | // |
13 | //===----------------------------------------------------------------------===// |
14 | |
15 | #ifndef LLVM_IR_INSTRTYPES_H |
16 | #define LLVM_IR_INSTRTYPES_H |
17 | |
18 | #include "llvm/ADT/ArrayRef.h" |
19 | #include "llvm/ADT/None.h" |
20 | #include "llvm/ADT/Optional.h" |
21 | #include "llvm/ADT/STLExtras.h" |
22 | #include "llvm/ADT/StringMap.h" |
23 | #include "llvm/ADT/StringRef.h" |
24 | #include "llvm/ADT/Twine.h" |
25 | #include "llvm/ADT/iterator_range.h" |
26 | #include "llvm/IR/Attributes.h" |
27 | #include "llvm/IR/CallingConv.h" |
28 | #include "llvm/IR/Constants.h" |
29 | #include "llvm/IR/DerivedTypes.h" |
30 | #include "llvm/IR/Function.h" |
31 | #include "llvm/IR/Instruction.h" |
32 | #include "llvm/IR/LLVMContext.h" |
33 | #include "llvm/IR/OperandTraits.h" |
34 | #include "llvm/IR/Type.h" |
35 | #include "llvm/IR/User.h" |
36 | #include "llvm/IR/Value.h" |
37 | #include "llvm/Support/Casting.h" |
38 | #include "llvm/Support/ErrorHandling.h" |
39 | #include <algorithm> |
40 | #include <cassert> |
41 | #include <cstddef> |
42 | #include <cstdint> |
43 | #include <iterator> |
44 | #include <string> |
45 | #include <vector> |
46 | |
47 | namespace llvm { |
48 | |
49 | namespace Intrinsic { |
50 | enum ID : unsigned; |
51 | } |
52 | |
53 | //===----------------------------------------------------------------------===// |
54 | // UnaryInstruction Class |
55 | //===----------------------------------------------------------------------===// |
56 | |
57 | class UnaryInstruction : public Instruction { |
58 | protected: |
59 | UnaryInstruction(Type *Ty, unsigned iType, Value *V, |
60 | Instruction *IB = nullptr) |
61 | : Instruction(Ty, iType, &Op<0>(), 1, IB) { |
62 | Op<0>() = V; |
63 | } |
64 | UnaryInstruction(Type *Ty, unsigned iType, Value *V, BasicBlock *IAE) |
65 | : Instruction(Ty, iType, &Op<0>(), 1, IAE) { |
66 | Op<0>() = V; |
67 | } |
68 | |
69 | public: |
70 | // allocate space for exactly one operand |
71 | void *operator new(size_t s) { |
72 | return User::operator new(s, 1); |
73 | } |
74 | |
75 | /// Transparently provide more efficient getOperand methods. |
76 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
77 | |
78 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
79 | static bool classof(const Instruction *I) { |
80 | return I->isUnaryOp() || |
81 | I->getOpcode() == Instruction::Alloca || |
82 | I->getOpcode() == Instruction::Load || |
83 | I->getOpcode() == Instruction::VAArg || |
84 | I->getOpcode() == Instruction::ExtractValue || |
85 | (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd); |
86 | } |
87 | static bool classof(const Value *V) { |
88 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
89 | } |
90 | }; |
91 | |
92 | template <> |
93 | struct OperandTraits<UnaryInstruction> : |
94 | public FixedNumOperandTraits<UnaryInstruction, 1> { |
95 | }; |
96 | |
97 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)UnaryInstruction::op_iterator UnaryInstruction::op_begin() { return OperandTraits<UnaryInstruction>::op_begin(this); } UnaryInstruction ::const_op_iterator UnaryInstruction::op_begin() const { return OperandTraits<UnaryInstruction>::op_begin(const_cast< UnaryInstruction*>(this)); } UnaryInstruction::op_iterator UnaryInstruction::op_end() { return OperandTraits<UnaryInstruction >::op_end(this); } UnaryInstruction::const_op_iterator UnaryInstruction ::op_end() const { return OperandTraits<UnaryInstruction> ::op_end(const_cast<UnaryInstruction*>(this)); } Value * UnaryInstruction::getOperand(unsigned i_nocapture) const { (( i_nocapture < OperandTraits<UnaryInstruction>::operands (this) && "getOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<UnaryInstruction>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 97, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<UnaryInstruction>::op_begin(const_cast< UnaryInstruction*>(this))[i_nocapture].get()); } void UnaryInstruction ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<UnaryInstruction>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<UnaryInstruction>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 97, __PRETTY_FUNCTION__)); OperandTraits<UnaryInstruction >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned UnaryInstruction::getNumOperands() const { return OperandTraits <UnaryInstruction>::operands(this); } template <int Idx_nocapture > Use &UnaryInstruction::Op() { return this->OpFrom <Idx_nocapture>(this); } template <int Idx_nocapture > const Use &UnaryInstruction::Op() const { return this ->OpFrom<Idx_nocapture>(this); } |
98 | |
99 | //===----------------------------------------------------------------------===// |
100 | // UnaryOperator Class |
101 | //===----------------------------------------------------------------------===// |
102 | |
103 | class UnaryOperator : public UnaryInstruction { |
104 | void AssertOK(); |
105 | |
106 | protected: |
107 | UnaryOperator(UnaryOps iType, Value *S, Type *Ty, |
108 | const Twine &Name, Instruction *InsertBefore); |
109 | UnaryOperator(UnaryOps iType, Value *S, Type *Ty, |
110 | const Twine &Name, BasicBlock *InsertAtEnd); |
111 | |
112 | // Note: Instruction needs to be a friend here to call cloneImpl. |
113 | friend class Instruction; |
114 | |
115 | UnaryOperator *cloneImpl() const; |
116 | |
117 | public: |
118 | |
119 | /// Construct a unary instruction, given the opcode and an operand. |
120 | /// Optionally (if InstBefore is specified) insert the instruction |
121 | /// into a BasicBlock right before the specified instruction. The specified |
122 | /// Instruction is allowed to be a dereferenced end iterator. |
123 | /// |
124 | static UnaryOperator *Create(UnaryOps Op, Value *S, |
125 | const Twine &Name = Twine(), |
126 | Instruction *InsertBefore = nullptr); |
127 | |
128 | /// Construct a unary instruction, given the opcode and an operand. |
129 | /// Also automatically insert this instruction to the end of the |
130 | /// BasicBlock specified. |
131 | /// |
132 | static UnaryOperator *Create(UnaryOps Op, Value *S, |
133 | const Twine &Name, |
134 | BasicBlock *InsertAtEnd); |
135 | |
136 | /// These methods just forward to Create, and are useful when you |
137 | /// statically know what type of instruction you're going to create. These |
138 | /// helpers just save some typing. |
139 | #define HANDLE_UNARY_INST(N, OPC, CLASS) \ |
140 | static UnaryOperator *Create##OPC(Value *V, const Twine &Name = "") {\ |
141 | return Create(Instruction::OPC, V, Name);\ |
142 | } |
143 | #include "llvm/IR/Instruction.def" |
144 | #define HANDLE_UNARY_INST(N, OPC, CLASS) \ |
145 | static UnaryOperator *Create##OPC(Value *V, const Twine &Name, \ |
146 | BasicBlock *BB) {\ |
147 | return Create(Instruction::OPC, V, Name, BB);\ |
148 | } |
149 | #include "llvm/IR/Instruction.def" |
150 | #define HANDLE_UNARY_INST(N, OPC, CLASS) \ |
151 | static UnaryOperator *Create##OPC(Value *V, const Twine &Name, \ |
152 | Instruction *I) {\ |
153 | return Create(Instruction::OPC, V, Name, I);\ |
154 | } |
155 | #include "llvm/IR/Instruction.def" |
156 | |
157 | UnaryOps getOpcode() const { |
158 | return static_cast<UnaryOps>(Instruction::getOpcode()); |
159 | } |
160 | |
161 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
162 | static bool classof(const Instruction *I) { |
163 | return I->isUnaryOp(); |
164 | } |
165 | static bool classof(const Value *V) { |
166 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
167 | } |
168 | }; |
169 | |
170 | //===----------------------------------------------------------------------===// |
171 | // BinaryOperator Class |
172 | //===----------------------------------------------------------------------===// |
173 | |
174 | class BinaryOperator : public Instruction { |
175 | void AssertOK(); |
176 | |
177 | protected: |
178 | BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty, |
179 | const Twine &Name, Instruction *InsertBefore); |
180 | BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty, |
181 | const Twine &Name, BasicBlock *InsertAtEnd); |
182 | |
183 | // Note: Instruction needs to be a friend here to call cloneImpl. |
184 | friend class Instruction; |
185 | |
186 | BinaryOperator *cloneImpl() const; |
187 | |
188 | public: |
189 | // allocate space for exactly two operands |
190 | void *operator new(size_t s) { |
191 | return User::operator new(s, 2); |
192 | } |
193 | |
194 | /// Transparently provide more efficient getOperand methods. |
195 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
196 | |
197 | /// Construct a binary instruction, given the opcode and the two |
198 | /// operands. Optionally (if InstBefore is specified) insert the instruction |
199 | /// into a BasicBlock right before the specified instruction. The specified |
200 | /// Instruction is allowed to be a dereferenced end iterator. |
201 | /// |
202 | static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2, |
203 | const Twine &Name = Twine(), |
204 | Instruction *InsertBefore = nullptr); |
205 | |
206 | /// Construct a binary instruction, given the opcode and the two |
207 | /// operands. Also automatically insert this instruction to the end of the |
208 | /// BasicBlock specified. |
209 | /// |
210 | static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2, |
211 | const Twine &Name, BasicBlock *InsertAtEnd); |
212 | |
213 | /// These methods just forward to Create, and are useful when you |
214 | /// statically know what type of instruction you're going to create. These |
215 | /// helpers just save some typing. |
216 | #define HANDLE_BINARY_INST(N, OPC, CLASS) \ |
217 | static BinaryOperator *Create##OPC(Value *V1, Value *V2, \ |
218 | const Twine &Name = "") {\ |
219 | return Create(Instruction::OPC, V1, V2, Name);\ |
220 | } |
221 | #include "llvm/IR/Instruction.def" |
222 | #define HANDLE_BINARY_INST(N, OPC, CLASS) \ |
223 | static BinaryOperator *Create##OPC(Value *V1, Value *V2, \ |
224 | const Twine &Name, BasicBlock *BB) {\ |
225 | return Create(Instruction::OPC, V1, V2, Name, BB);\ |
226 | } |
227 | #include "llvm/IR/Instruction.def" |
228 | #define HANDLE_BINARY_INST(N, OPC, CLASS) \ |
229 | static BinaryOperator *Create##OPC(Value *V1, Value *V2, \ |
230 | const Twine &Name, Instruction *I) {\ |
231 | return Create(Instruction::OPC, V1, V2, Name, I);\ |
232 | } |
233 | #include "llvm/IR/Instruction.def" |
234 | |
235 | static BinaryOperator *CreateWithCopiedFlags(BinaryOps Opc, |
236 | Value *V1, Value *V2, |
237 | Instruction *CopyO, |
238 | const Twine &Name = "") { |
239 | BinaryOperator *BO = Create(Opc, V1, V2, Name); |
240 | BO->copyIRFlags(CopyO); |
241 | return BO; |
242 | } |
243 | |
244 | static BinaryOperator *CreateFAddFMF(Value *V1, Value *V2, |
245 | Instruction *FMFSource, |
246 | const Twine &Name = "") { |
247 | return CreateWithCopiedFlags(Instruction::FAdd, V1, V2, FMFSource, Name); |
248 | } |
249 | static BinaryOperator *CreateFSubFMF(Value *V1, Value *V2, |
250 | Instruction *FMFSource, |
251 | const Twine &Name = "") { |
252 | return CreateWithCopiedFlags(Instruction::FSub, V1, V2, FMFSource, Name); |
253 | } |
254 | static BinaryOperator *CreateFMulFMF(Value *V1, Value *V2, |
255 | Instruction *FMFSource, |
256 | const Twine &Name = "") { |
257 | return CreateWithCopiedFlags(Instruction::FMul, V1, V2, FMFSource, Name); |
258 | } |
259 | static BinaryOperator *CreateFDivFMF(Value *V1, Value *V2, |
260 | Instruction *FMFSource, |
261 | const Twine &Name = "") { |
262 | return CreateWithCopiedFlags(Instruction::FDiv, V1, V2, FMFSource, Name); |
263 | } |
264 | static BinaryOperator *CreateFRemFMF(Value *V1, Value *V2, |
265 | Instruction *FMFSource, |
266 | const Twine &Name = "") { |
267 | return CreateWithCopiedFlags(Instruction::FRem, V1, V2, FMFSource, Name); |
268 | } |
269 | static BinaryOperator *CreateFNegFMF(Value *Op, Instruction *FMFSource, |
270 | const Twine &Name = "") { |
271 | Value *Zero = ConstantFP::getNegativeZero(Op->getType()); |
272 | return CreateWithCopiedFlags(Instruction::FSub, Zero, Op, FMFSource); |
273 | } |
274 | |
275 | static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2, |
276 | const Twine &Name = "") { |
277 | BinaryOperator *BO = Create(Opc, V1, V2, Name); |
278 | BO->setHasNoSignedWrap(true); |
279 | return BO; |
280 | } |
281 | static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2, |
282 | const Twine &Name, BasicBlock *BB) { |
283 | BinaryOperator *BO = Create(Opc, V1, V2, Name, BB); |
284 | BO->setHasNoSignedWrap(true); |
285 | return BO; |
286 | } |
287 | static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2, |
288 | const Twine &Name, Instruction *I) { |
289 | BinaryOperator *BO = Create(Opc, V1, V2, Name, I); |
290 | BO->setHasNoSignedWrap(true); |
291 | return BO; |
292 | } |
293 | |
294 | static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2, |
295 | const Twine &Name = "") { |
296 | BinaryOperator *BO = Create(Opc, V1, V2, Name); |
297 | BO->setHasNoUnsignedWrap(true); |
298 | return BO; |
299 | } |
300 | static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2, |
301 | const Twine &Name, BasicBlock *BB) { |
302 | BinaryOperator *BO = Create(Opc, V1, V2, Name, BB); |
303 | BO->setHasNoUnsignedWrap(true); |
304 | return BO; |
305 | } |
306 | static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2, |
307 | const Twine &Name, Instruction *I) { |
308 | BinaryOperator *BO = Create(Opc, V1, V2, Name, I); |
309 | BO->setHasNoUnsignedWrap(true); |
310 | return BO; |
311 | } |
312 | |
313 | static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2, |
314 | const Twine &Name = "") { |
315 | BinaryOperator *BO = Create(Opc, V1, V2, Name); |
316 | BO->setIsExact(true); |
317 | return BO; |
318 | } |
319 | static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2, |
320 | const Twine &Name, BasicBlock *BB) { |
321 | BinaryOperator *BO = Create(Opc, V1, V2, Name, BB); |
322 | BO->setIsExact(true); |
323 | return BO; |
324 | } |
325 | static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2, |
326 | const Twine &Name, Instruction *I) { |
327 | BinaryOperator *BO = Create(Opc, V1, V2, Name, I); |
328 | BO->setIsExact(true); |
329 | return BO; |
330 | } |
331 | |
332 | #define DEFINE_HELPERS(OPC, NUWNSWEXACT) \ |
333 | static BinaryOperator *Create##NUWNSWEXACT##OPC(Value *V1, Value *V2, \ |
334 | const Twine &Name = "") { \ |
335 | return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name); \ |
336 | } \ |
337 | static BinaryOperator *Create##NUWNSWEXACT##OPC( \ |
338 | Value *V1, Value *V2, const Twine &Name, BasicBlock *BB) { \ |
339 | return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name, BB); \ |
340 | } \ |
341 | static BinaryOperator *Create##NUWNSWEXACT##OPC( \ |
342 | Value *V1, Value *V2, const Twine &Name, Instruction *I) { \ |
343 | return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name, I); \ |
344 | } |
345 | |
346 | DEFINE_HELPERS(Add, NSW) // CreateNSWAdd |
347 | DEFINE_HELPERS(Add, NUW) // CreateNUWAdd |
348 | DEFINE_HELPERS(Sub, NSW) // CreateNSWSub |
349 | DEFINE_HELPERS(Sub, NUW) // CreateNUWSub |
350 | DEFINE_HELPERS(Mul, NSW) // CreateNSWMul |
351 | DEFINE_HELPERS(Mul, NUW) // CreateNUWMul |
352 | DEFINE_HELPERS(Shl, NSW) // CreateNSWShl |
353 | DEFINE_HELPERS(Shl, NUW) // CreateNUWShl |
354 | |
355 | DEFINE_HELPERS(SDiv, Exact) // CreateExactSDiv |
356 | DEFINE_HELPERS(UDiv, Exact) // CreateExactUDiv |
357 | DEFINE_HELPERS(AShr, Exact) // CreateExactAShr |
358 | DEFINE_HELPERS(LShr, Exact) // CreateExactLShr |
359 | |
360 | #undef DEFINE_HELPERS |
361 | |
362 | /// Helper functions to construct and inspect unary operations (NEG and NOT) |
363 | /// via binary operators SUB and XOR: |
364 | /// |
365 | /// Create the NEG and NOT instructions out of SUB and XOR instructions. |
366 | /// |
367 | static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "", |
368 | Instruction *InsertBefore = nullptr); |
369 | static BinaryOperator *CreateNeg(Value *Op, const Twine &Name, |
370 | BasicBlock *InsertAtEnd); |
371 | static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "", |
372 | Instruction *InsertBefore = nullptr); |
373 | static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name, |
374 | BasicBlock *InsertAtEnd); |
375 | static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "", |
376 | Instruction *InsertBefore = nullptr); |
377 | static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name, |
378 | BasicBlock *InsertAtEnd); |
379 | static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "", |
380 | Instruction *InsertBefore = nullptr); |
381 | static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name, |
382 | BasicBlock *InsertAtEnd); |
383 | static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "", |
384 | Instruction *InsertBefore = nullptr); |
385 | static BinaryOperator *CreateNot(Value *Op, const Twine &Name, |
386 | BasicBlock *InsertAtEnd); |
387 | |
388 | BinaryOps getOpcode() const { |
389 | return static_cast<BinaryOps>(Instruction::getOpcode()); |
390 | } |
391 | |
392 | /// Exchange the two operands to this instruction. |
393 | /// This instruction is safe to use on any binary instruction and |
394 | /// does not modify the semantics of the instruction. If the instruction |
395 | /// cannot be reversed (ie, it's a Div), then return true. |
396 | /// |
397 | bool swapOperands(); |
398 | |
399 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
400 | static bool classof(const Instruction *I) { |
401 | return I->isBinaryOp(); |
402 | } |
403 | static bool classof(const Value *V) { |
404 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
405 | } |
406 | }; |
407 | |
408 | template <> |
409 | struct OperandTraits<BinaryOperator> : |
410 | public FixedNumOperandTraits<BinaryOperator, 2> { |
411 | }; |
412 | |
413 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)BinaryOperator::op_iterator BinaryOperator::op_begin() { return OperandTraits<BinaryOperator>::op_begin(this); } BinaryOperator ::const_op_iterator BinaryOperator::op_begin() const { return OperandTraits<BinaryOperator>::op_begin(const_cast< BinaryOperator*>(this)); } BinaryOperator::op_iterator BinaryOperator ::op_end() { return OperandTraits<BinaryOperator>::op_end (this); } BinaryOperator::const_op_iterator BinaryOperator::op_end () const { return OperandTraits<BinaryOperator>::op_end (const_cast<BinaryOperator*>(this)); } Value *BinaryOperator ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<BinaryOperator>::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<BinaryOperator>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 413, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<BinaryOperator>::op_begin(const_cast< BinaryOperator*>(this))[i_nocapture].get()); } void BinaryOperator ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<BinaryOperator>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<BinaryOperator>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 413, __PRETTY_FUNCTION__)); OperandTraits<BinaryOperator >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned BinaryOperator::getNumOperands() const { return OperandTraits <BinaryOperator>::operands(this); } template <int Idx_nocapture > Use &BinaryOperator::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &BinaryOperator::Op() const { return this-> OpFrom<Idx_nocapture>(this); } |
414 | |
415 | //===----------------------------------------------------------------------===// |
416 | // CastInst Class |
417 | //===----------------------------------------------------------------------===// |
418 | |
419 | /// This is the base class for all instructions that perform data |
420 | /// casts. It is simply provided so that instruction category testing |
421 | /// can be performed with code like: |
422 | /// |
423 | /// if (isa<CastInst>(Instr)) { ... } |
424 | /// Base class of casting instructions. |
425 | class CastInst : public UnaryInstruction { |
426 | protected: |
427 | /// Constructor with insert-before-instruction semantics for subclasses |
428 | CastInst(Type *Ty, unsigned iType, Value *S, |
429 | const Twine &NameStr = "", Instruction *InsertBefore = nullptr) |
430 | : UnaryInstruction(Ty, iType, S, InsertBefore) { |
431 | setName(NameStr); |
432 | } |
433 | /// Constructor with insert-at-end-of-block semantics for subclasses |
434 | CastInst(Type *Ty, unsigned iType, Value *S, |
435 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
436 | : UnaryInstruction(Ty, iType, S, InsertAtEnd) { |
437 | setName(NameStr); |
438 | } |
439 | |
440 | public: |
441 | /// Provides a way to construct any of the CastInst subclasses using an |
442 | /// opcode instead of the subclass's constructor. The opcode must be in the |
443 | /// CastOps category (Instruction::isCast(opcode) returns true). This |
444 | /// constructor has insert-before-instruction semantics to automatically |
445 | /// insert the new CastInst before InsertBefore (if it is non-null). |
446 | /// Construct any of the CastInst subclasses |
447 | static CastInst *Create( |
448 | Instruction::CastOps, ///< The opcode of the cast instruction |
449 | Value *S, ///< The value to be casted (operand 0) |
450 | Type *Ty, ///< The type to which cast should be made |
451 | const Twine &Name = "", ///< Name for the instruction |
452 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
453 | ); |
454 | /// Provides a way to construct any of the CastInst subclasses using an |
455 | /// opcode instead of the subclass's constructor. The opcode must be in the |
456 | /// CastOps category. This constructor has insert-at-end-of-block semantics |
457 | /// to automatically insert the new CastInst at the end of InsertAtEnd (if |
458 | /// its non-null). |
459 | /// Construct any of the CastInst subclasses |
460 | static CastInst *Create( |
461 | Instruction::CastOps, ///< The opcode for the cast instruction |
462 | Value *S, ///< The value to be casted (operand 0) |
463 | Type *Ty, ///< The type to which operand is casted |
464 | const Twine &Name, ///< The name for the instruction |
465 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
466 | ); |
467 | |
468 | /// Create a ZExt or BitCast cast instruction |
469 | static CastInst *CreateZExtOrBitCast( |
470 | Value *S, ///< The value to be casted (operand 0) |
471 | Type *Ty, ///< The type to which cast should be made |
472 | const Twine &Name = "", ///< Name for the instruction |
473 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
474 | ); |
475 | |
476 | /// Create a ZExt or BitCast cast instruction |
477 | static CastInst *CreateZExtOrBitCast( |
478 | Value *S, ///< The value to be casted (operand 0) |
479 | Type *Ty, ///< The type to which operand is casted |
480 | const Twine &Name, ///< The name for the instruction |
481 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
482 | ); |
483 | |
484 | /// Create a SExt or BitCast cast instruction |
485 | static CastInst *CreateSExtOrBitCast( |
486 | Value *S, ///< The value to be casted (operand 0) |
487 | Type *Ty, ///< The type to which cast should be made |
488 | const Twine &Name = "", ///< Name for the instruction |
489 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
490 | ); |
491 | |
492 | /// Create a SExt or BitCast cast instruction |
493 | static CastInst *CreateSExtOrBitCast( |
494 | Value *S, ///< The value to be casted (operand 0) |
495 | Type *Ty, ///< The type to which operand is casted |
496 | const Twine &Name, ///< The name for the instruction |
497 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
498 | ); |
499 | |
500 | /// Create a BitCast AddrSpaceCast, or a PtrToInt cast instruction. |
501 | static CastInst *CreatePointerCast( |
502 | Value *S, ///< The pointer value to be casted (operand 0) |
503 | Type *Ty, ///< The type to which operand is casted |
504 | const Twine &Name, ///< The name for the instruction |
505 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
506 | ); |
507 | |
508 | /// Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction. |
509 | static CastInst *CreatePointerCast( |
510 | Value *S, ///< The pointer value to be casted (operand 0) |
511 | Type *Ty, ///< The type to which cast should be made |
512 | const Twine &Name = "", ///< Name for the instruction |
513 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
514 | ); |
515 | |
516 | /// Create a BitCast or an AddrSpaceCast cast instruction. |
517 | static CastInst *CreatePointerBitCastOrAddrSpaceCast( |
518 | Value *S, ///< The pointer value to be casted (operand 0) |
519 | Type *Ty, ///< The type to which operand is casted |
520 | const Twine &Name, ///< The name for the instruction |
521 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
522 | ); |
523 | |
524 | /// Create a BitCast or an AddrSpaceCast cast instruction. |
525 | static CastInst *CreatePointerBitCastOrAddrSpaceCast( |
526 | Value *S, ///< The pointer value to be casted (operand 0) |
527 | Type *Ty, ///< The type to which cast should be made |
528 | const Twine &Name = "", ///< Name for the instruction |
529 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
530 | ); |
531 | |
532 | /// Create a BitCast, a PtrToInt, or an IntToPTr cast instruction. |
533 | /// |
534 | /// If the value is a pointer type and the destination an integer type, |
535 | /// creates a PtrToInt cast. If the value is an integer type and the |
536 | /// destination a pointer type, creates an IntToPtr cast. Otherwise, creates |
537 | /// a bitcast. |
538 | static CastInst *CreateBitOrPointerCast( |
539 | Value *S, ///< The pointer value to be casted (operand 0) |
540 | Type *Ty, ///< The type to which cast should be made |
541 | const Twine &Name = "", ///< Name for the instruction |
542 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
543 | ); |
544 | |
545 | /// Create a ZExt, BitCast, or Trunc for int -> int casts. |
546 | static CastInst *CreateIntegerCast( |
547 | Value *S, ///< The pointer value to be casted (operand 0) |
548 | Type *Ty, ///< The type to which cast should be made |
549 | bool isSigned, ///< Whether to regard S as signed or not |
550 | const Twine &Name = "", ///< Name for the instruction |
551 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
552 | ); |
553 | |
554 | /// Create a ZExt, BitCast, or Trunc for int -> int casts. |
555 | static CastInst *CreateIntegerCast( |
556 | Value *S, ///< The integer value to be casted (operand 0) |
557 | Type *Ty, ///< The integer type to which operand is casted |
558 | bool isSigned, ///< Whether to regard S as signed or not |
559 | const Twine &Name, ///< The name for the instruction |
560 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
561 | ); |
562 | |
563 | /// Create an FPExt, BitCast, or FPTrunc for fp -> fp casts |
564 | static CastInst *CreateFPCast( |
565 | Value *S, ///< The floating point value to be casted |
566 | Type *Ty, ///< The floating point type to cast to |
567 | const Twine &Name = "", ///< Name for the instruction |
568 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
569 | ); |
570 | |
571 | /// Create an FPExt, BitCast, or FPTrunc for fp -> fp casts |
572 | static CastInst *CreateFPCast( |
573 | Value *S, ///< The floating point value to be casted |
574 | Type *Ty, ///< The floating point type to cast to |
575 | const Twine &Name, ///< The name for the instruction |
576 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
577 | ); |
578 | |
579 | /// Create a Trunc or BitCast cast instruction |
580 | static CastInst *CreateTruncOrBitCast( |
581 | Value *S, ///< The value to be casted (operand 0) |
582 | Type *Ty, ///< The type to which cast should be made |
583 | const Twine &Name = "", ///< Name for the instruction |
584 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
585 | ); |
586 | |
587 | /// Create a Trunc or BitCast cast instruction |
588 | static CastInst *CreateTruncOrBitCast( |
589 | Value *S, ///< The value to be casted (operand 0) |
590 | Type *Ty, ///< The type to which operand is casted |
591 | const Twine &Name, ///< The name for the instruction |
592 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
593 | ); |
594 | |
595 | /// Check whether it is valid to call getCastOpcode for these types. |
596 | static bool isCastable( |
597 | Type *SrcTy, ///< The Type from which the value should be cast. |
598 | Type *DestTy ///< The Type to which the value should be cast. |
599 | ); |
600 | |
601 | /// Check whether a bitcast between these types is valid |
602 | static bool isBitCastable( |
603 | Type *SrcTy, ///< The Type from which the value should be cast. |
604 | Type *DestTy ///< The Type to which the value should be cast. |
605 | ); |
606 | |
607 | /// Check whether a bitcast, inttoptr, or ptrtoint cast between these |
608 | /// types is valid and a no-op. |
609 | /// |
610 | /// This ensures that any pointer<->integer cast has enough bits in the |
611 | /// integer and any other cast is a bitcast. |
612 | static bool isBitOrNoopPointerCastable( |
613 | Type *SrcTy, ///< The Type from which the value should be cast. |
614 | Type *DestTy, ///< The Type to which the value should be cast. |
615 | const DataLayout &DL); |
616 | |
617 | /// Returns the opcode necessary to cast Val into Ty using usual casting |
618 | /// rules. |
619 | /// Infer the opcode for cast operand and type |
620 | static Instruction::CastOps getCastOpcode( |
621 | const Value *Val, ///< The value to cast |
622 | bool SrcIsSigned, ///< Whether to treat the source as signed |
623 | Type *Ty, ///< The Type to which the value should be casted |
624 | bool DstIsSigned ///< Whether to treate the dest. as signed |
625 | ); |
626 | |
627 | /// There are several places where we need to know if a cast instruction |
628 | /// only deals with integer source and destination types. To simplify that |
629 | /// logic, this method is provided. |
630 | /// @returns true iff the cast has only integral typed operand and dest type. |
631 | /// Determine if this is an integer-only cast. |
632 | bool isIntegerCast() const; |
633 | |
634 | /// A lossless cast is one that does not alter the basic value. It implies |
635 | /// a no-op cast but is more stringent, preventing things like int->float, |
636 | /// long->double, or int->ptr. |
637 | /// @returns true iff the cast is lossless. |
638 | /// Determine if this is a lossless cast. |
639 | bool isLosslessCast() const; |
640 | |
641 | /// A no-op cast is one that can be effected without changing any bits. |
642 | /// It implies that the source and destination types are the same size. The |
643 | /// DataLayout argument is to determine the pointer size when examining casts |
644 | /// involving Integer and Pointer types. They are no-op casts if the integer |
645 | /// is the same size as the pointer. However, pointer size varies with |
646 | /// platform. |
647 | /// Determine if the described cast is a no-op cast. |
648 | static bool isNoopCast( |
649 | Instruction::CastOps Opcode, ///< Opcode of cast |
650 | Type *SrcTy, ///< SrcTy of cast |
651 | Type *DstTy, ///< DstTy of cast |
652 | const DataLayout &DL ///< DataLayout to get the Int Ptr type from. |
653 | ); |
654 | |
655 | /// Determine if this cast is a no-op cast. |
656 | /// |
657 | /// \param DL is the DataLayout to determine pointer size. |
658 | bool isNoopCast(const DataLayout &DL) const; |
659 | |
660 | /// Determine how a pair of casts can be eliminated, if they can be at all. |
661 | /// This is a helper function for both CastInst and ConstantExpr. |
662 | /// @returns 0 if the CastInst pair can't be eliminated, otherwise |
663 | /// returns Instruction::CastOps value for a cast that can replace |
664 | /// the pair, casting SrcTy to DstTy. |
665 | /// Determine if a cast pair is eliminable |
666 | static unsigned isEliminableCastPair( |
667 | Instruction::CastOps firstOpcode, ///< Opcode of first cast |
668 | Instruction::CastOps secondOpcode, ///< Opcode of second cast |
669 | Type *SrcTy, ///< SrcTy of 1st cast |
670 | Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast |
671 | Type *DstTy, ///< DstTy of 2nd cast |
672 | Type *SrcIntPtrTy, ///< Integer type corresponding to Ptr SrcTy, or null |
673 | Type *MidIntPtrTy, ///< Integer type corresponding to Ptr MidTy, or null |
674 | Type *DstIntPtrTy ///< Integer type corresponding to Ptr DstTy, or null |
675 | ); |
676 | |
677 | /// Return the opcode of this CastInst |
678 | Instruction::CastOps getOpcode() const { |
679 | return Instruction::CastOps(Instruction::getOpcode()); |
680 | } |
681 | |
682 | /// Return the source type, as a convenience |
683 | Type* getSrcTy() const { return getOperand(0)->getType(); } |
684 | /// Return the destination type, as a convenience |
685 | Type* getDestTy() const { return getType(); } |
686 | |
687 | /// This method can be used to determine if a cast from S to DstTy using |
688 | /// Opcode op is valid or not. |
689 | /// @returns true iff the proposed cast is valid. |
690 | /// Determine if a cast is valid without creating one. |
691 | static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy); |
692 | |
693 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
694 | static bool classof(const Instruction *I) { |
695 | return I->isCast(); |
696 | } |
697 | static bool classof(const Value *V) { |
698 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
699 | } |
700 | }; |
701 | |
702 | //===----------------------------------------------------------------------===// |
703 | // CmpInst Class |
704 | //===----------------------------------------------------------------------===// |
705 | |
706 | /// This class is the base class for the comparison instructions. |
707 | /// Abstract base class of comparison instructions. |
708 | class CmpInst : public Instruction { |
709 | public: |
710 | /// This enumeration lists the possible predicates for CmpInst subclasses. |
711 | /// Values in the range 0-31 are reserved for FCmpInst, while values in the |
712 | /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the |
713 | /// predicate values are not overlapping between the classes. |
714 | /// |
715 | /// Some passes (e.g. InstCombine) depend on the bit-wise characteristics of |
716 | /// FCMP_* values. Changing the bit patterns requires a potential change to |
717 | /// those passes. |
718 | enum Predicate { |
719 | // Opcode U L G E Intuitive operation |
720 | FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded) |
721 | FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal |
722 | FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than |
723 | FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal |
724 | FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than |
725 | FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal |
726 | FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal |
727 | FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans) |
728 | FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y) |
729 | FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal |
730 | FCMP_UGT = 10, ///< 1 0 1 0 True if unordered or greater than |
731 | FCMP_UGE = 11, ///< 1 0 1 1 True if unordered, greater than, or equal |
732 | FCMP_ULT = 12, ///< 1 1 0 0 True if unordered or less than |
733 | FCMP_ULE = 13, ///< 1 1 0 1 True if unordered, less than, or equal |
734 | FCMP_UNE = 14, ///< 1 1 1 0 True if unordered or not equal |
735 | FCMP_TRUE = 15, ///< 1 1 1 1 Always true (always folded) |
736 | FIRST_FCMP_PREDICATE = FCMP_FALSE, |
737 | LAST_FCMP_PREDICATE = FCMP_TRUE, |
738 | BAD_FCMP_PREDICATE = FCMP_TRUE + 1, |
739 | ICMP_EQ = 32, ///< equal |
740 | ICMP_NE = 33, ///< not equal |
741 | ICMP_UGT = 34, ///< unsigned greater than |
742 | ICMP_UGE = 35, ///< unsigned greater or equal |
743 | ICMP_ULT = 36, ///< unsigned less than |
744 | ICMP_ULE = 37, ///< unsigned less or equal |
745 | ICMP_SGT = 38, ///< signed greater than |
746 | ICMP_SGE = 39, ///< signed greater or equal |
747 | ICMP_SLT = 40, ///< signed less than |
748 | ICMP_SLE = 41, ///< signed less or equal |
749 | FIRST_ICMP_PREDICATE = ICMP_EQ, |
750 | LAST_ICMP_PREDICATE = ICMP_SLE, |
751 | BAD_ICMP_PREDICATE = ICMP_SLE + 1 |
752 | }; |
753 | |
754 | protected: |
755 | CmpInst(Type *ty, Instruction::OtherOps op, Predicate pred, |
756 | Value *LHS, Value *RHS, const Twine &Name = "", |
757 | Instruction *InsertBefore = nullptr, |
758 | Instruction *FlagsSource = nullptr); |
759 | |
760 | CmpInst(Type *ty, Instruction::OtherOps op, Predicate pred, |
761 | Value *LHS, Value *RHS, const Twine &Name, |
762 | BasicBlock *InsertAtEnd); |
763 | |
764 | public: |
765 | // allocate space for exactly two operands |
766 | void *operator new(size_t s) { |
767 | return User::operator new(s, 2); |
768 | } |
769 | |
770 | /// Construct a compare instruction, given the opcode, the predicate and |
771 | /// the two operands. Optionally (if InstBefore is specified) insert the |
772 | /// instruction into a BasicBlock right before the specified instruction. |
773 | /// The specified Instruction is allowed to be a dereferenced end iterator. |
774 | /// Create a CmpInst |
775 | static CmpInst *Create(OtherOps Op, |
776 | Predicate predicate, Value *S1, |
777 | Value *S2, const Twine &Name = "", |
778 | Instruction *InsertBefore = nullptr); |
779 | |
780 | /// Construct a compare instruction, given the opcode, the predicate and the |
781 | /// two operands. Also automatically insert this instruction to the end of |
782 | /// the BasicBlock specified. |
783 | /// Create a CmpInst |
784 | static CmpInst *Create(OtherOps Op, Predicate predicate, Value *S1, |
785 | Value *S2, const Twine &Name, BasicBlock *InsertAtEnd); |
786 | |
787 | /// Get the opcode casted to the right type |
788 | OtherOps getOpcode() const { |
789 | return static_cast<OtherOps>(Instruction::getOpcode()); |
790 | } |
791 | |
792 | /// Return the predicate for this instruction. |
793 | Predicate getPredicate() const { |
794 | return Predicate(getSubclassDataFromInstruction()); |
795 | } |
796 | |
797 | /// Set the predicate for this instruction to the specified value. |
798 | void setPredicate(Predicate P) { setInstructionSubclassData(P); } |
799 | |
800 | static bool isFPPredicate(Predicate P) { |
801 | return P >= FIRST_FCMP_PREDICATE && P <= LAST_FCMP_PREDICATE; |
802 | } |
803 | |
804 | static bool isIntPredicate(Predicate P) { |
805 | return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE; |
806 | } |
807 | |
808 | static StringRef getPredicateName(Predicate P); |
809 | |
810 | bool isFPPredicate() const { return isFPPredicate(getPredicate()); } |
811 | bool isIntPredicate() const { return isIntPredicate(getPredicate()); } |
812 | |
813 | /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, |
814 | /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc. |
815 | /// @returns the inverse predicate for the instruction's current predicate. |
816 | /// Return the inverse of the instruction's predicate. |
817 | Predicate getInversePredicate() const { |
818 | return getInversePredicate(getPredicate()); |
819 | } |
820 | |
821 | /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, |
822 | /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc. |
823 | /// @returns the inverse predicate for predicate provided in \p pred. |
824 | /// Return the inverse of a given predicate |
825 | static Predicate getInversePredicate(Predicate pred); |
826 | |
827 | /// For example, EQ->EQ, SLE->SGE, ULT->UGT, |
828 | /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc. |
829 | /// @returns the predicate that would be the result of exchanging the two |
830 | /// operands of the CmpInst instruction without changing the result |
831 | /// produced. |
832 | /// Return the predicate as if the operands were swapped |
833 | Predicate getSwappedPredicate() const { |
834 | return getSwappedPredicate(getPredicate()); |
835 | } |
836 | |
837 | /// This is a static version that you can use without an instruction |
838 | /// available. |
839 | /// Return the predicate as if the operands were swapped. |
840 | static Predicate getSwappedPredicate(Predicate pred); |
841 | |
842 | /// For predicate of kind "is X or equal to 0" returns the predicate "is X". |
843 | /// For predicate of kind "is X" returns the predicate "is X or equal to 0". |
844 | /// does not support other kind of predicates. |
845 | /// @returns the predicate that does not contains is equal to zero if |
846 | /// it had and vice versa. |
847 | /// Return the flipped strictness of predicate |
848 | Predicate getFlippedStrictnessPredicate() const { |
849 | return getFlippedStrictnessPredicate(getPredicate()); |
850 | } |
851 | |
852 | /// This is a static version that you can use without an instruction |
853 | /// available. |
854 | /// Return the flipped strictness of predicate |
855 | static Predicate getFlippedStrictnessPredicate(Predicate pred); |
856 | |
857 | /// For example, SGT -> SGE, SLT -> SLE, ULT -> ULE, UGT -> UGE. |
858 | /// Returns the non-strict version of strict comparisons. |
859 | Predicate getNonStrictPredicate() const { |
860 | return getNonStrictPredicate(getPredicate()); |
861 | } |
862 | |
863 | /// This is a static version that you can use without an instruction |
864 | /// available. |
865 | /// @returns the non-strict version of comparison provided in \p pred. |
866 | /// If \p pred is not a strict comparison predicate, returns \p pred. |
867 | /// Returns the non-strict version of strict comparisons. |
868 | static Predicate getNonStrictPredicate(Predicate pred); |
869 | |
870 | /// Provide more efficient getOperand methods. |
871 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
872 | |
873 | /// This is just a convenience that dispatches to the subclasses. |
874 | /// Swap the operands and adjust predicate accordingly to retain |
875 | /// the same comparison. |
876 | void swapOperands(); |
877 | |
878 | /// This is just a convenience that dispatches to the subclasses. |
879 | /// Determine if this CmpInst is commutative. |
880 | bool isCommutative() const; |
881 | |
882 | /// This is just a convenience that dispatches to the subclasses. |
883 | /// Determine if this is an equals/not equals predicate. |
884 | bool isEquality() const; |
885 | |
886 | /// @returns true if the comparison is signed, false otherwise. |
887 | /// Determine if this instruction is using a signed comparison. |
888 | bool isSigned() const { |
889 | return isSigned(getPredicate()); |
890 | } |
891 | |
892 | /// @returns true if the comparison is unsigned, false otherwise. |
893 | /// Determine if this instruction is using an unsigned comparison. |
894 | bool isUnsigned() const { |
895 | return isUnsigned(getPredicate()); |
896 | } |
897 | |
898 | /// For example, ULT->SLT, ULE->SLE, UGT->SGT, UGE->SGE, SLT->Failed assert |
899 | /// @returns the signed version of the unsigned predicate pred. |
900 | /// return the signed version of a predicate |
901 | static Predicate getSignedPredicate(Predicate pred); |
902 | |
903 | /// For example, ULT->SLT, ULE->SLE, UGT->SGT, UGE->SGE, SLT->Failed assert |
904 | /// @returns the signed version of the predicate for this instruction (which |
905 | /// has to be an unsigned predicate). |
906 | /// return the signed version of a predicate |
907 | Predicate getSignedPredicate() { |
908 | return getSignedPredicate(getPredicate()); |
909 | } |
910 | |
911 | /// This is just a convenience. |
912 | /// Determine if this is true when both operands are the same. |
913 | bool isTrueWhenEqual() const { |
914 | return isTrueWhenEqual(getPredicate()); |
915 | } |
916 | |
917 | /// This is just a convenience. |
918 | /// Determine if this is false when both operands are the same. |
919 | bool isFalseWhenEqual() const { |
920 | return isFalseWhenEqual(getPredicate()); |
921 | } |
922 | |
923 | /// @returns true if the predicate is unsigned, false otherwise. |
924 | /// Determine if the predicate is an unsigned operation. |
925 | static bool isUnsigned(Predicate predicate); |
926 | |
927 | /// @returns true if the predicate is signed, false otherwise. |
928 | /// Determine if the predicate is an signed operation. |
929 | static bool isSigned(Predicate predicate); |
930 | |
931 | /// Determine if the predicate is an ordered operation. |
932 | static bool isOrdered(Predicate predicate); |
933 | |
934 | /// Determine if the predicate is an unordered operation. |
935 | static bool isUnordered(Predicate predicate); |
936 | |
937 | /// Determine if the predicate is true when comparing a value with itself. |
938 | static bool isTrueWhenEqual(Predicate predicate); |
939 | |
940 | /// Determine if the predicate is false when comparing a value with itself. |
941 | static bool isFalseWhenEqual(Predicate predicate); |
942 | |
943 | /// Determine if Pred1 implies Pred2 is true when two compares have matching |
944 | /// operands. |
945 | static bool isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2); |
946 | |
947 | /// Determine if Pred1 implies Pred2 is false when two compares have matching |
948 | /// operands. |
949 | static bool isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2); |
950 | |
951 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
952 | static bool classof(const Instruction *I) { |
953 | return I->getOpcode() == Instruction::ICmp || |
954 | I->getOpcode() == Instruction::FCmp; |
955 | } |
956 | static bool classof(const Value *V) { |
957 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
958 | } |
959 | |
960 | /// Create a result type for fcmp/icmp |
961 | static Type* makeCmpResultType(Type* opnd_type) { |
962 | if (VectorType* vt = dyn_cast<VectorType>(opnd_type)) { |
963 | return VectorType::get(Type::getInt1Ty(opnd_type->getContext()), |
964 | vt->getNumElements()); |
965 | } |
966 | return Type::getInt1Ty(opnd_type->getContext()); |
967 | } |
968 | |
969 | private: |
970 | // Shadow Value::setValueSubclassData with a private forwarding method so that |
971 | // subclasses cannot accidentally use it. |
972 | void setValueSubclassData(unsigned short D) { |
973 | Value::setValueSubclassData(D); |
974 | } |
975 | }; |
976 | |
977 | // FIXME: these are redundant if CmpInst < BinaryOperator |
978 | template <> |
979 | struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> { |
980 | }; |
981 | |
982 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)CmpInst::op_iterator CmpInst::op_begin() { return OperandTraits <CmpInst>::op_begin(this); } CmpInst::const_op_iterator CmpInst::op_begin() const { return OperandTraits<CmpInst> ::op_begin(const_cast<CmpInst*>(this)); } CmpInst::op_iterator CmpInst::op_end() { return OperandTraits<CmpInst>::op_end (this); } CmpInst::const_op_iterator CmpInst::op_end() const { return OperandTraits<CmpInst>::op_end(const_cast<CmpInst *>(this)); } Value *CmpInst::getOperand(unsigned i_nocapture ) const { ((i_nocapture < OperandTraits<CmpInst>::operands (this) && "getOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<CmpInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 982, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<CmpInst>::op_begin(const_cast<CmpInst *>(this))[i_nocapture].get()); } void CmpInst::setOperand( unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<CmpInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<CmpInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 982, __PRETTY_FUNCTION__)); OperandTraits<CmpInst>::op_begin (this)[i_nocapture] = Val_nocapture; } unsigned CmpInst::getNumOperands () const { return OperandTraits<CmpInst>::operands(this ); } template <int Idx_nocapture> Use &CmpInst::Op( ) { return this->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture> const Use &CmpInst::Op() const { return this->OpFrom<Idx_nocapture>(this); } |
983 | |
984 | /// A lightweight accessor for an operand bundle meant to be passed |
985 | /// around by value. |
986 | struct OperandBundleUse { |
987 | ArrayRef<Use> Inputs; |
988 | |
989 | OperandBundleUse() = default; |
990 | explicit OperandBundleUse(StringMapEntry<uint32_t> *Tag, ArrayRef<Use> Inputs) |
991 | : Inputs(Inputs), Tag(Tag) {} |
992 | |
993 | /// Return true if the operand at index \p Idx in this operand bundle |
994 | /// has the attribute A. |
995 | bool operandHasAttr(unsigned Idx, Attribute::AttrKind A) const { |
996 | if (isDeoptOperandBundle()) |
997 | if (A == Attribute::ReadOnly || A == Attribute::NoCapture) |
998 | return Inputs[Idx]->getType()->isPointerTy(); |
999 | |
1000 | // Conservative answer: no operands have any attributes. |
1001 | return false; |
1002 | } |
1003 | |
1004 | /// Return the tag of this operand bundle as a string. |
1005 | StringRef getTagName() const { |
1006 | return Tag->getKey(); |
1007 | } |
1008 | |
1009 | /// Return the tag of this operand bundle as an integer. |
1010 | /// |
1011 | /// Operand bundle tags are interned by LLVMContextImpl::getOrInsertBundleTag, |
1012 | /// and this function returns the unique integer getOrInsertBundleTag |
1013 | /// associated the tag of this operand bundle to. |
1014 | uint32_t getTagID() const { |
1015 | return Tag->getValue(); |
1016 | } |
1017 | |
1018 | /// Return true if this is a "deopt" operand bundle. |
1019 | bool isDeoptOperandBundle() const { |
1020 | return getTagID() == LLVMContext::OB_deopt; |
1021 | } |
1022 | |
1023 | /// Return true if this is a "funclet" operand bundle. |
1024 | bool isFuncletOperandBundle() const { |
1025 | return getTagID() == LLVMContext::OB_funclet; |
1026 | } |
1027 | |
1028 | private: |
1029 | /// Pointer to an entry in LLVMContextImpl::getOrInsertBundleTag. |
1030 | StringMapEntry<uint32_t> *Tag; |
1031 | }; |
1032 | |
1033 | /// A container for an operand bundle being viewed as a set of values |
1034 | /// rather than a set of uses. |
1035 | /// |
1036 | /// Unlike OperandBundleUse, OperandBundleDefT owns the memory it carries, and |
1037 | /// so it is possible to create and pass around "self-contained" instances of |
1038 | /// OperandBundleDef and ConstOperandBundleDef. |
1039 | template <typename InputTy> class OperandBundleDefT { |
1040 | std::string Tag; |
1041 | std::vector<InputTy> Inputs; |
1042 | |
1043 | public: |
1044 | explicit OperandBundleDefT(std::string Tag, std::vector<InputTy> Inputs) |
1045 | : Tag(std::move(Tag)), Inputs(std::move(Inputs)) {} |
1046 | explicit OperandBundleDefT(std::string Tag, ArrayRef<InputTy> Inputs) |
1047 | : Tag(std::move(Tag)), Inputs(Inputs) {} |
1048 | |
1049 | explicit OperandBundleDefT(const OperandBundleUse &OBU) { |
1050 | Tag = OBU.getTagName(); |
1051 | Inputs.insert(Inputs.end(), OBU.Inputs.begin(), OBU.Inputs.end()); |
1052 | } |
1053 | |
1054 | ArrayRef<InputTy> inputs() const { return Inputs; } |
1055 | |
1056 | using input_iterator = typename std::vector<InputTy>::const_iterator; |
1057 | |
1058 | size_t input_size() const { return Inputs.size(); } |
1059 | input_iterator input_begin() const { return Inputs.begin(); } |
1060 | input_iterator input_end() const { return Inputs.end(); } |
1061 | |
1062 | StringRef getTag() const { return Tag; } |
1063 | }; |
1064 | |
1065 | using OperandBundleDef = OperandBundleDefT<Value *>; |
1066 | using ConstOperandBundleDef = OperandBundleDefT<const Value *>; |
1067 | |
1068 | //===----------------------------------------------------------------------===// |
1069 | // CallBase Class |
1070 | //===----------------------------------------------------------------------===// |
1071 | |
1072 | /// Base class for all callable instructions (InvokeInst and CallInst) |
1073 | /// Holds everything related to calling a function. |
1074 | /// |
1075 | /// All call-like instructions are required to use a common operand layout: |
1076 | /// - Zero or more arguments to the call, |
1077 | /// - Zero or more operand bundles with zero or more operand inputs each |
1078 | /// bundle, |
1079 | /// - Zero or more subclass controlled operands |
1080 | /// - The called function. |
1081 | /// |
1082 | /// This allows this base class to easily access the called function and the |
1083 | /// start of the arguments without knowing how many other operands a particular |
1084 | /// subclass requires. Note that accessing the end of the argument list isn't |
1085 | /// as cheap as most other operations on the base class. |
1086 | class CallBase : public Instruction { |
1087 | protected: |
1088 | /// The last operand is the called operand. |
1089 | static constexpr int CalledOperandOpEndIdx = -1; |
1090 | |
1091 | AttributeList Attrs; ///< parameter attributes for callable |
1092 | FunctionType *FTy; |
1093 | |
1094 | template <class... ArgsTy> |
1095 | CallBase(AttributeList const &A, FunctionType *FT, ArgsTy &&... Args) |
1096 | : Instruction(std::forward<ArgsTy>(Args)...), Attrs(A), FTy(FT) {} |
1097 | |
1098 | using Instruction::Instruction; |
1099 | |
1100 | bool hasDescriptor() const { return Value::HasDescriptor; } |
1101 | |
1102 | unsigned getNumSubclassExtraOperands() const { |
1103 | switch (getOpcode()) { |
1104 | case Instruction::Call: |
1105 | return 0; |
1106 | case Instruction::Invoke: |
1107 | return 2; |
1108 | case Instruction::CallBr: |
1109 | return getNumSubclassExtraOperandsDynamic(); |
1110 | } |
1111 | llvm_unreachable("Invalid opcode!")::llvm::llvm_unreachable_internal("Invalid opcode!", "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1111); |
1112 | } |
1113 | |
1114 | /// Get the number of extra operands for instructions that don't have a fixed |
1115 | /// number of extra operands. |
1116 | unsigned getNumSubclassExtraOperandsDynamic() const; |
1117 | |
1118 | public: |
1119 | using Instruction::getContext; |
1120 | |
1121 | static bool classof(const Instruction *I) { |
1122 | return I->getOpcode() == Instruction::Call || |
1123 | I->getOpcode() == Instruction::Invoke || |
1124 | I->getOpcode() == Instruction::CallBr; |
1125 | } |
1126 | static bool classof(const Value *V) { |
1127 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1128 | } |
1129 | |
1130 | FunctionType *getFunctionType() const { return FTy; } |
1131 | |
1132 | void mutateFunctionType(FunctionType *FTy) { |
1133 | Value::mutateType(FTy->getReturnType()); |
1134 | this->FTy = FTy; |
1135 | } |
1136 | |
1137 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
1138 | |
1139 | /// data_operands_begin/data_operands_end - Return iterators iterating over |
1140 | /// the call / invoke argument list and bundle operands. For invokes, this is |
1141 | /// the set of instruction operands except the invoke target and the two |
1142 | /// successor blocks; and for calls this is the set of instruction operands |
1143 | /// except the call target. |
1144 | User::op_iterator data_operands_begin() { return op_begin(); } |
1145 | User::const_op_iterator data_operands_begin() const { |
1146 | return const_cast<CallBase *>(this)->data_operands_begin(); |
1147 | } |
1148 | User::op_iterator data_operands_end() { |
1149 | // Walk from the end of the operands over the called operand and any |
1150 | // subclass operands. |
1151 | return op_end() - getNumSubclassExtraOperands() - 1; |
1152 | } |
1153 | User::const_op_iterator data_operands_end() const { |
1154 | return const_cast<CallBase *>(this)->data_operands_end(); |
1155 | } |
1156 | iterator_range<User::op_iterator> data_ops() { |
1157 | return make_range(data_operands_begin(), data_operands_end()); |
1158 | } |
1159 | iterator_range<User::const_op_iterator> data_ops() const { |
1160 | return make_range(data_operands_begin(), data_operands_end()); |
1161 | } |
1162 | bool data_operands_empty() const { |
1163 | return data_operands_end() == data_operands_begin(); |
1164 | } |
1165 | unsigned data_operands_size() const { |
1166 | return std::distance(data_operands_begin(), data_operands_end()); |
1167 | } |
1168 | |
1169 | bool isDataOperand(const Use *U) const { |
1170 | assert(this == U->getUser() &&((this == U->getUser() && "Only valid to query with a use of this instruction!" ) ? static_cast<void> (0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1171, __PRETTY_FUNCTION__)) |
1171 | "Only valid to query with a use of this instruction!")((this == U->getUser() && "Only valid to query with a use of this instruction!" ) ? static_cast<void> (0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1171, __PRETTY_FUNCTION__)); |
1172 | return data_operands_begin() <= U && U < data_operands_end(); |
1173 | } |
1174 | bool isDataOperand(Value::const_user_iterator UI) const { |
1175 | return isDataOperand(&UI.getUse()); |
1176 | } |
1177 | |
1178 | /// Given a value use iterator, return the data operand corresponding to it. |
1179 | /// Iterator must actually correspond to a data operand. |
1180 | unsigned getDataOperandNo(Value::const_user_iterator UI) const { |
1181 | return getDataOperandNo(&UI.getUse()); |
1182 | } |
1183 | |
1184 | /// Given a use for a data operand, get the data operand number that |
1185 | /// corresponds to it. |
1186 | unsigned getDataOperandNo(const Use *U) const { |
1187 | assert(isDataOperand(U) && "Data operand # out of range!")((isDataOperand(U) && "Data operand # out of range!") ? static_cast<void> (0) : __assert_fail ("isDataOperand(U) && \"Data operand # out of range!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1187, __PRETTY_FUNCTION__)); |
1188 | return U - data_operands_begin(); |
1189 | } |
1190 | |
1191 | /// Return the iterator pointing to the beginning of the argument list. |
1192 | User::op_iterator arg_begin() { return op_begin(); } |
1193 | User::const_op_iterator arg_begin() const { |
1194 | return const_cast<CallBase *>(this)->arg_begin(); |
1195 | } |
1196 | |
1197 | /// Return the iterator pointing to the end of the argument list. |
1198 | User::op_iterator arg_end() { |
1199 | // From the end of the data operands, walk backwards past the bundle |
1200 | // operands. |
1201 | return data_operands_end() - getNumTotalBundleOperands(); |
1202 | } |
1203 | User::const_op_iterator arg_end() const { |
1204 | return const_cast<CallBase *>(this)->arg_end(); |
1205 | } |
1206 | |
1207 | /// Iteration adapter for range-for loops. |
1208 | iterator_range<User::op_iterator> args() { |
1209 | return make_range(arg_begin(), arg_end()); |
1210 | } |
1211 | iterator_range<User::const_op_iterator> args() const { |
1212 | return make_range(arg_begin(), arg_end()); |
1213 | } |
1214 | bool arg_empty() const { return arg_end() == arg_begin(); } |
1215 | unsigned arg_size() const { return arg_end() - arg_begin(); } |
1216 | |
1217 | // Legacy API names that duplicate the above and will be removed once users |
1218 | // are migrated. |
1219 | iterator_range<User::op_iterator> arg_operands() { |
1220 | return make_range(arg_begin(), arg_end()); |
1221 | } |
1222 | iterator_range<User::const_op_iterator> arg_operands() const { |
1223 | return make_range(arg_begin(), arg_end()); |
1224 | } |
1225 | unsigned getNumArgOperands() const { return arg_size(); } |
1226 | |
1227 | Value *getArgOperand(unsigned i) const { |
1228 | assert(i < getNumArgOperands() && "Out of bounds!")((i < getNumArgOperands() && "Out of bounds!") ? static_cast <void> (0) : __assert_fail ("i < getNumArgOperands() && \"Out of bounds!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1228, __PRETTY_FUNCTION__)); |
1229 | return getOperand(i); |
1230 | } |
1231 | |
1232 | void setArgOperand(unsigned i, Value *v) { |
1233 | assert(i < getNumArgOperands() && "Out of bounds!")((i < getNumArgOperands() && "Out of bounds!") ? static_cast <void> (0) : __assert_fail ("i < getNumArgOperands() && \"Out of bounds!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1233, __PRETTY_FUNCTION__)); |
1234 | setOperand(i, v); |
1235 | } |
1236 | |
1237 | /// Wrappers for getting the \c Use of a call argument. |
1238 | const Use &getArgOperandUse(unsigned i) const { |
1239 | assert(i < getNumArgOperands() && "Out of bounds!")((i < getNumArgOperands() && "Out of bounds!") ? static_cast <void> (0) : __assert_fail ("i < getNumArgOperands() && \"Out of bounds!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1239, __PRETTY_FUNCTION__)); |
1240 | return User::getOperandUse(i); |
1241 | } |
1242 | Use &getArgOperandUse(unsigned i) { |
1243 | assert(i < getNumArgOperands() && "Out of bounds!")((i < getNumArgOperands() && "Out of bounds!") ? static_cast <void> (0) : __assert_fail ("i < getNumArgOperands() && \"Out of bounds!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1243, __PRETTY_FUNCTION__)); |
1244 | return User::getOperandUse(i); |
1245 | } |
1246 | |
1247 | bool isArgOperand(const Use *U) const { |
1248 | assert(this == U->getUser() &&((this == U->getUser() && "Only valid to query with a use of this instruction!" ) ? static_cast<void> (0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1249, __PRETTY_FUNCTION__)) |
1249 | "Only valid to query with a use of this instruction!")((this == U->getUser() && "Only valid to query with a use of this instruction!" ) ? static_cast<void> (0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1249, __PRETTY_FUNCTION__)); |
1250 | return arg_begin() <= U && U < arg_end(); |
1251 | } |
1252 | bool isArgOperand(Value::const_user_iterator UI) const { |
1253 | return isArgOperand(&UI.getUse()); |
1254 | } |
1255 | |
1256 | /// Returns true if this CallSite passes the given Value* as an argument to |
1257 | /// the called function. |
1258 | bool hasArgument(const Value *V) const { |
1259 | return llvm::any_of(args(), [V](const Value *Arg) { return Arg == V; }); |
1260 | } |
1261 | |
1262 | Value *getCalledOperand() const { return Op<CalledOperandOpEndIdx>(); } |
1263 | |
1264 | // DEPRECATED: This routine will be removed in favor of `getCalledOperand` in |
1265 | // the near future. |
1266 | Value *getCalledValue() const { return getCalledOperand(); } |
1267 | |
1268 | const Use &getCalledOperandUse() const { return Op<CalledOperandOpEndIdx>(); } |
1269 | Use &getCalledOperandUse() { return Op<CalledOperandOpEndIdx>(); } |
1270 | |
1271 | /// Returns the function called, or null if this is an |
1272 | /// indirect function invocation. |
1273 | Function *getCalledFunction() const { |
1274 | return dyn_cast_or_null<Function>(getCalledOperand()); |
1275 | } |
1276 | |
1277 | /// Return true if the callsite is an indirect call. |
1278 | bool isIndirectCall() const; |
1279 | |
1280 | /// Determine whether the passed iterator points to the callee operand's Use. |
1281 | bool isCallee(Value::const_user_iterator UI) const { |
1282 | return isCallee(&UI.getUse()); |
1283 | } |
1284 | |
1285 | /// Determine whether this Use is the callee operand's Use. |
1286 | bool isCallee(const Use *U) const { return &getCalledOperandUse() == U; } |
1287 | |
1288 | /// Helper to get the caller (the parent function). |
1289 | Function *getCaller(); |
1290 | const Function *getCaller() const { |
1291 | return const_cast<CallBase *>(this)->getCaller(); |
1292 | } |
1293 | |
1294 | /// Tests if this call site must be tail call optimized. Only a CallInst can |
1295 | /// be tail call optimized. |
1296 | bool isMustTailCall() const; |
1297 | |
1298 | /// Tests if this call site is marked as a tail call. |
1299 | bool isTailCall() const; |
1300 | |
1301 | /// Returns the intrinsic ID of the intrinsic called or |
1302 | /// Intrinsic::not_intrinsic if the called function is not an intrinsic, or if |
1303 | /// this is an indirect call. |
1304 | Intrinsic::ID getIntrinsicID() const; |
1305 | |
1306 | void setCalledOperand(Value *V) { Op<CalledOperandOpEndIdx>() = V; } |
1307 | |
1308 | /// Sets the function called, including updating the function type. |
1309 | void setCalledFunction(Function *Fn) { |
1310 | setCalledFunction(Fn->getFunctionType(), Fn); |
1311 | } |
1312 | |
1313 | /// Sets the function called, including updating the function type. |
1314 | void setCalledFunction(FunctionCallee Fn) { |
1315 | setCalledFunction(Fn.getFunctionType(), Fn.getCallee()); |
1316 | } |
1317 | |
1318 | /// Sets the function called, including updating to the specified function |
1319 | /// type. |
1320 | void setCalledFunction(FunctionType *FTy, Value *Fn) { |
1321 | this->FTy = FTy; |
1322 | assert(FTy == cast<FunctionType>(((FTy == cast<FunctionType>( cast<PointerType>(Fn ->getType())->getElementType())) ? static_cast<void> (0) : __assert_fail ("FTy == cast<FunctionType>( cast<PointerType>(Fn->getType())->getElementType())" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1323, __PRETTY_FUNCTION__)) |
1323 | cast<PointerType>(Fn->getType())->getElementType()))((FTy == cast<FunctionType>( cast<PointerType>(Fn ->getType())->getElementType())) ? static_cast<void> (0) : __assert_fail ("FTy == cast<FunctionType>( cast<PointerType>(Fn->getType())->getElementType())" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1323, __PRETTY_FUNCTION__)); |
1324 | // This function doesn't mutate the return type, only the function |
1325 | // type. Seems broken, but I'm just gonna stick an assert in for now. |
1326 | assert(getType() == FTy->getReturnType())((getType() == FTy->getReturnType()) ? static_cast<void > (0) : __assert_fail ("getType() == FTy->getReturnType()" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1326, __PRETTY_FUNCTION__)); |
1327 | setCalledOperand(Fn); |
1328 | } |
1329 | |
1330 | CallingConv::ID getCallingConv() const { |
1331 | return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2); |
1332 | } |
1333 | |
1334 | void setCallingConv(CallingConv::ID CC) { |
1335 | auto ID = static_cast<unsigned>(CC); |
1336 | assert(!(ID & ~CallingConv::MaxID) && "Unsupported calling convention")((!(ID & ~CallingConv::MaxID) && "Unsupported calling convention" ) ? static_cast<void> (0) : __assert_fail ("!(ID & ~CallingConv::MaxID) && \"Unsupported calling convention\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1336, __PRETTY_FUNCTION__)); |
1337 | setInstructionSubclassData((getSubclassDataFromInstruction() & 3) | |
1338 | (ID << 2)); |
1339 | } |
1340 | |
1341 | /// Check if this call is an inline asm statement. |
1342 | bool isInlineAsm() const { return isa<InlineAsm>(getCalledOperand()); } |
1343 | |
1344 | /// \name Attribute API |
1345 | /// |
1346 | /// These methods access and modify attributes on this call (including |
1347 | /// looking through to the attributes on the called function when necessary). |
1348 | ///@{ |
1349 | |
1350 | /// Return the parameter attributes for this call. |
1351 | /// |
1352 | AttributeList getAttributes() const { return Attrs; } |
1353 | |
1354 | /// Set the parameter attributes for this call. |
1355 | /// |
1356 | void setAttributes(AttributeList A) { Attrs = A; } |
1357 | |
1358 | /// Determine whether this call has the given attribute. |
1359 | bool hasFnAttr(Attribute::AttrKind Kind) const { |
1360 | assert(Kind != Attribute::NoBuiltin &&((Kind != Attribute::NoBuiltin && "Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin" ) ? static_cast<void> (0) : __assert_fail ("Kind != Attribute::NoBuiltin && \"Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1361, __PRETTY_FUNCTION__)) |
1361 | "Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin")((Kind != Attribute::NoBuiltin && "Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin" ) ? static_cast<void> (0) : __assert_fail ("Kind != Attribute::NoBuiltin && \"Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1361, __PRETTY_FUNCTION__)); |
1362 | return hasFnAttrImpl(Kind); |
1363 | } |
1364 | |
1365 | /// Determine whether this call has the given attribute. |
1366 | bool hasFnAttr(StringRef Kind) const { return hasFnAttrImpl(Kind); } |
1367 | |
1368 | /// adds the attribute to the list of attributes. |
1369 | void addAttribute(unsigned i, Attribute::AttrKind Kind) { |
1370 | AttributeList PAL = getAttributes(); |
1371 | PAL = PAL.addAttribute(getContext(), i, Kind); |
1372 | setAttributes(PAL); |
1373 | } |
1374 | |
1375 | /// adds the attribute to the list of attributes. |
1376 | void addAttribute(unsigned i, Attribute Attr) { |
1377 | AttributeList PAL = getAttributes(); |
1378 | PAL = PAL.addAttribute(getContext(), i, Attr); |
1379 | setAttributes(PAL); |
1380 | } |
1381 | |
1382 | /// Adds the attribute to the indicated argument |
1383 | void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { |
1384 | assert(ArgNo < getNumArgOperands() && "Out of bounds")((ArgNo < getNumArgOperands() && "Out of bounds") ? static_cast<void> (0) : __assert_fail ("ArgNo < getNumArgOperands() && \"Out of bounds\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1384, __PRETTY_FUNCTION__)); |
1385 | AttributeList PAL = getAttributes(); |
1386 | PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind); |
1387 | setAttributes(PAL); |
1388 | } |
1389 | |
1390 | /// Adds the attribute to the indicated argument |
1391 | void addParamAttr(unsigned ArgNo, Attribute Attr) { |
1392 | assert(ArgNo < getNumArgOperands() && "Out of bounds")((ArgNo < getNumArgOperands() && "Out of bounds") ? static_cast<void> (0) : __assert_fail ("ArgNo < getNumArgOperands() && \"Out of bounds\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1392, __PRETTY_FUNCTION__)); |
1393 | AttributeList PAL = getAttributes(); |
1394 | PAL = PAL.addParamAttribute(getContext(), ArgNo, Attr); |
1395 | setAttributes(PAL); |
1396 | } |
1397 | |
1398 | /// removes the attribute from the list of attributes. |
1399 | void removeAttribute(unsigned i, Attribute::AttrKind Kind) { |
1400 | AttributeList PAL = getAttributes(); |
1401 | PAL = PAL.removeAttribute(getContext(), i, Kind); |
1402 | setAttributes(PAL); |
1403 | } |
1404 | |
1405 | /// removes the attribute from the list of attributes. |
1406 | void removeAttribute(unsigned i, StringRef Kind) { |
1407 | AttributeList PAL = getAttributes(); |
1408 | PAL = PAL.removeAttribute(getContext(), i, Kind); |
1409 | setAttributes(PAL); |
1410 | } |
1411 | |
1412 | /// Removes the attribute from the given argument |
1413 | void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { |
1414 | assert(ArgNo < getNumArgOperands() && "Out of bounds")((ArgNo < getNumArgOperands() && "Out of bounds") ? static_cast<void> (0) : __assert_fail ("ArgNo < getNumArgOperands() && \"Out of bounds\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1414, __PRETTY_FUNCTION__)); |
1415 | AttributeList PAL = getAttributes(); |
1416 | PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind); |
1417 | setAttributes(PAL); |
1418 | } |
1419 | |
1420 | /// Removes the attribute from the given argument |
1421 | void removeParamAttr(unsigned ArgNo, StringRef Kind) { |
1422 | assert(ArgNo < getNumArgOperands() && "Out of bounds")((ArgNo < getNumArgOperands() && "Out of bounds") ? static_cast<void> (0) : __assert_fail ("ArgNo < getNumArgOperands() && \"Out of bounds\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1422, __PRETTY_FUNCTION__)); |
1423 | AttributeList PAL = getAttributes(); |
1424 | PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind); |
1425 | setAttributes(PAL); |
1426 | } |
1427 | |
1428 | /// adds the dereferenceable attribute to the list of attributes. |
1429 | void addDereferenceableAttr(unsigned i, uint64_t Bytes) { |
1430 | AttributeList PAL = getAttributes(); |
1431 | PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes); |
1432 | setAttributes(PAL); |
1433 | } |
1434 | |
1435 | /// adds the dereferenceable_or_null attribute to the list of |
1436 | /// attributes. |
1437 | void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) { |
1438 | AttributeList PAL = getAttributes(); |
1439 | PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes); |
1440 | setAttributes(PAL); |
1441 | } |
1442 | |
1443 | /// Determine whether the return value has the given attribute. |
1444 | bool hasRetAttr(Attribute::AttrKind Kind) const; |
1445 | |
1446 | /// Determine whether the argument or parameter has the given attribute. |
1447 | bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const; |
1448 | |
1449 | /// Get the attribute of a given kind at a position. |
1450 | Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const { |
1451 | return getAttributes().getAttribute(i, Kind); |
1452 | } |
1453 | |
1454 | /// Get the attribute of a given kind at a position. |
1455 | Attribute getAttribute(unsigned i, StringRef Kind) const { |
1456 | return getAttributes().getAttribute(i, Kind); |
1457 | } |
1458 | |
1459 | /// Get the attribute of a given kind from a given arg |
1460 | Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const { |
1461 | assert(ArgNo < getNumArgOperands() && "Out of bounds")((ArgNo < getNumArgOperands() && "Out of bounds") ? static_cast<void> (0) : __assert_fail ("ArgNo < getNumArgOperands() && \"Out of bounds\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1461, __PRETTY_FUNCTION__)); |
1462 | return getAttributes().getParamAttr(ArgNo, Kind); |
1463 | } |
1464 | |
1465 | /// Get the attribute of a given kind from a given arg |
1466 | Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const { |
1467 | assert(ArgNo < getNumArgOperands() && "Out of bounds")((ArgNo < getNumArgOperands() && "Out of bounds") ? static_cast<void> (0) : __assert_fail ("ArgNo < getNumArgOperands() && \"Out of bounds\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1467, __PRETTY_FUNCTION__)); |
1468 | return getAttributes().getParamAttr(ArgNo, Kind); |
1469 | } |
1470 | |
1471 | /// Return true if the data operand at index \p i has the attribute \p |
1472 | /// A. |
1473 | /// |
1474 | /// Data operands include call arguments and values used in operand bundles, |
1475 | /// but does not include the callee operand. This routine dispatches to the |
1476 | /// underlying AttributeList or the OperandBundleUser as appropriate. |
1477 | /// |
1478 | /// The index \p i is interpreted as |
1479 | /// |
1480 | /// \p i == Attribute::ReturnIndex -> the return value |
1481 | /// \p i in [1, arg_size + 1) -> argument number (\p i - 1) |
1482 | /// \p i in [arg_size + 1, data_operand_size + 1) -> bundle operand at index |
1483 | /// (\p i - 1) in the operand list. |
1484 | bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const { |
1485 | // Note that we have to add one because `i` isn't zero-indexed. |
1486 | assert(i < (getNumArgOperands() + getNumTotalBundleOperands() + 1) &&((i < (getNumArgOperands() + getNumTotalBundleOperands() + 1) && "Data operand index out of bounds!") ? static_cast <void> (0) : __assert_fail ("i < (getNumArgOperands() + getNumTotalBundleOperands() + 1) && \"Data operand index out of bounds!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1487, __PRETTY_FUNCTION__)) |
1487 | "Data operand index out of bounds!")((i < (getNumArgOperands() + getNumTotalBundleOperands() + 1) && "Data operand index out of bounds!") ? static_cast <void> (0) : __assert_fail ("i < (getNumArgOperands() + getNumTotalBundleOperands() + 1) && \"Data operand index out of bounds!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1487, __PRETTY_FUNCTION__)); |
1488 | |
1489 | // The attribute A can either be directly specified, if the operand in |
1490 | // question is a call argument; or be indirectly implied by the kind of its |
1491 | // containing operand bundle, if the operand is a bundle operand. |
1492 | |
1493 | if (i == AttributeList::ReturnIndex) |
1494 | return hasRetAttr(Kind); |
1495 | |
1496 | // FIXME: Avoid these i - 1 calculations and update the API to use |
1497 | // zero-based indices. |
1498 | if (i < (getNumArgOperands() + 1)) |
1499 | return paramHasAttr(i - 1, Kind); |
1500 | |
1501 | assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&((hasOperandBundles() && i >= (getBundleOperandsStartIndex () + 1) && "Must be either a call argument or an operand bundle!" ) ? static_cast<void> (0) : __assert_fail ("hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) && \"Must be either a call argument or an operand bundle!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1502, __PRETTY_FUNCTION__)) |
1502 | "Must be either a call argument or an operand bundle!")((hasOperandBundles() && i >= (getBundleOperandsStartIndex () + 1) && "Must be either a call argument or an operand bundle!" ) ? static_cast<void> (0) : __assert_fail ("hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) && \"Must be either a call argument or an operand bundle!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1502, __PRETTY_FUNCTION__)); |
1503 | return bundleOperandHasAttr(i - 1, Kind); |
1504 | } |
1505 | |
1506 | /// Determine whether this data operand is not captured. |
1507 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1508 | // better indicate that this may return a conservative answer. |
1509 | bool doesNotCapture(unsigned OpNo) const { |
1510 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::NoCapture); |
1511 | } |
1512 | |
1513 | /// Determine whether this argument is passed by value. |
1514 | bool isByValArgument(unsigned ArgNo) const { |
1515 | return paramHasAttr(ArgNo, Attribute::ByVal); |
1516 | } |
1517 | |
1518 | /// Determine whether this argument is passed in an alloca. |
1519 | bool isInAllocaArgument(unsigned ArgNo) const { |
1520 | return paramHasAttr(ArgNo, Attribute::InAlloca); |
1521 | } |
1522 | |
1523 | /// Determine whether this argument is passed by value or in an alloca. |
1524 | bool isByValOrInAllocaArgument(unsigned ArgNo) const { |
1525 | return paramHasAttr(ArgNo, Attribute::ByVal) || |
1526 | paramHasAttr(ArgNo, Attribute::InAlloca); |
1527 | } |
1528 | |
1529 | /// Determine if there are is an inalloca argument. Only the last argument can |
1530 | /// have the inalloca attribute. |
1531 | bool hasInAllocaArgument() const { |
1532 | return !arg_empty() && paramHasAttr(arg_size() - 1, Attribute::InAlloca); |
1533 | } |
1534 | |
1535 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1536 | // better indicate that this may return a conservative answer. |
1537 | bool doesNotAccessMemory(unsigned OpNo) const { |
1538 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); |
1539 | } |
1540 | |
1541 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1542 | // better indicate that this may return a conservative answer. |
1543 | bool onlyReadsMemory(unsigned OpNo) const { |
1544 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadOnly) || |
1545 | dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); |
1546 | } |
1547 | |
1548 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1549 | // better indicate that this may return a conservative answer. |
1550 | bool doesNotReadMemory(unsigned OpNo) const { |
1551 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::WriteOnly) || |
1552 | dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); |
1553 | } |
1554 | |
1555 | /// Extract the alignment of the return value. |
1556 | unsigned getRetAlignment() const { return Attrs.getRetAlignment(); } |
1557 | |
1558 | /// Extract the alignment for a call or parameter (0=unknown). |
1559 | unsigned getParamAlignment(unsigned ArgNo) const { |
1560 | return Attrs.getParamAlignment(ArgNo); |
1561 | } |
1562 | |
1563 | /// Extract the byval type for a call or parameter (nullptr=unknown). |
1564 | Type *getParamByValType(unsigned ArgNo) const { |
1565 | return Attrs.getParamByValType(ArgNo); |
1566 | } |
1567 | |
1568 | /// Extract the number of dereferenceable bytes for a call or |
1569 | /// parameter (0=unknown). |
1570 | uint64_t getDereferenceableBytes(unsigned i) const { |
1571 | return Attrs.getDereferenceableBytes(i); |
1572 | } |
1573 | |
1574 | /// Extract the number of dereferenceable_or_null bytes for a call or |
1575 | /// parameter (0=unknown). |
1576 | uint64_t getDereferenceableOrNullBytes(unsigned i) const { |
1577 | return Attrs.getDereferenceableOrNullBytes(i); |
1578 | } |
1579 | |
1580 | /// Return true if the return value is known to be not null. |
1581 | /// This may be because it has the nonnull attribute, or because at least |
1582 | /// one byte is dereferenceable and the pointer is in addrspace(0). |
1583 | bool isReturnNonNull() const; |
1584 | |
1585 | /// Determine if the return value is marked with NoAlias attribute. |
1586 | bool returnDoesNotAlias() const { |
1587 | return Attrs.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias); |
1588 | } |
1589 | |
1590 | /// If one of the arguments has the 'returned' attribute, returns its |
1591 | /// operand value. Otherwise, return nullptr. |
1592 | Value *getReturnedArgOperand() const; |
1593 | |
1594 | /// Return true if the call should not be treated as a call to a |
1595 | /// builtin. |
1596 | bool isNoBuiltin() const { |
1597 | return hasFnAttrImpl(Attribute::NoBuiltin) && |
1598 | !hasFnAttrImpl(Attribute::Builtin); |
1599 | } |
1600 | |
1601 | /// Determine if the call requires strict floating point semantics. |
1602 | bool isStrictFP() const { return hasFnAttr(Attribute::StrictFP); } |
1603 | |
1604 | /// Return true if the call should not be inlined. |
1605 | bool isNoInline() const { return hasFnAttr(Attribute::NoInline); } |
1606 | void setIsNoInline() { |
1607 | addAttribute(AttributeList::FunctionIndex, Attribute::NoInline); |
1608 | } |
1609 | /// Determine if the call does not access memory. |
1610 | bool doesNotAccessMemory() const { return hasFnAttr(Attribute::ReadNone); } |
1611 | void setDoesNotAccessMemory() { |
1612 | addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone); |
1613 | } |
1614 | |
1615 | /// Determine if the call does not access or only reads memory. |
1616 | bool onlyReadsMemory() const { |
1617 | return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly); |
1618 | } |
1619 | void setOnlyReadsMemory() { |
1620 | addAttribute(AttributeList::FunctionIndex, Attribute::ReadOnly); |
1621 | } |
1622 | |
1623 | /// Determine if the call does not access or only writes memory. |
1624 | bool doesNotReadMemory() const { |
1625 | return doesNotAccessMemory() || hasFnAttr(Attribute::WriteOnly); |
1626 | } |
1627 | void setDoesNotReadMemory() { |
1628 | addAttribute(AttributeList::FunctionIndex, Attribute::WriteOnly); |
1629 | } |
1630 | |
1631 | /// Determine if the call can access memmory only using pointers based |
1632 | /// on its arguments. |
1633 | bool onlyAccessesArgMemory() const { |
1634 | return hasFnAttr(Attribute::ArgMemOnly); |
1635 | } |
1636 | void setOnlyAccessesArgMemory() { |
1637 | addAttribute(AttributeList::FunctionIndex, Attribute::ArgMemOnly); |
1638 | } |
1639 | |
1640 | /// Determine if the function may only access memory that is |
1641 | /// inaccessible from the IR. |
1642 | bool onlyAccessesInaccessibleMemory() const { |
1643 | return hasFnAttr(Attribute::InaccessibleMemOnly); |
1644 | } |
1645 | void setOnlyAccessesInaccessibleMemory() { |
1646 | addAttribute(AttributeList::FunctionIndex, Attribute::InaccessibleMemOnly); |
1647 | } |
1648 | |
1649 | /// Determine if the function may only access memory that is |
1650 | /// either inaccessible from the IR or pointed to by its arguments. |
1651 | bool onlyAccessesInaccessibleMemOrArgMem() const { |
1652 | return hasFnAttr(Attribute::InaccessibleMemOrArgMemOnly); |
1653 | } |
1654 | void setOnlyAccessesInaccessibleMemOrArgMem() { |
1655 | addAttribute(AttributeList::FunctionIndex, |
1656 | Attribute::InaccessibleMemOrArgMemOnly); |
1657 | } |
1658 | /// Determine if the call cannot return. |
1659 | bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); } |
1660 | void setDoesNotReturn() { |
1661 | addAttribute(AttributeList::FunctionIndex, Attribute::NoReturn); |
1662 | } |
1663 | |
1664 | /// Determine if the call should not perform indirect branch tracking. |
1665 | bool doesNoCfCheck() const { return hasFnAttr(Attribute::NoCfCheck); } |
1666 | |
1667 | /// Determine if the call cannot unwind. |
1668 | bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); } |
1669 | void setDoesNotThrow() { |
1670 | addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind); |
1671 | } |
1672 | |
1673 | /// Determine if the invoke cannot be duplicated. |
1674 | bool cannotDuplicate() const { return hasFnAttr(Attribute::NoDuplicate); } |
1675 | void setCannotDuplicate() { |
1676 | addAttribute(AttributeList::FunctionIndex, Attribute::NoDuplicate); |
1677 | } |
1678 | |
1679 | /// Determine if the invoke is convergent |
1680 | bool isConvergent() const { return hasFnAttr(Attribute::Convergent); } |
1681 | void setConvergent() { |
1682 | addAttribute(AttributeList::FunctionIndex, Attribute::Convergent); |
1683 | } |
1684 | void setNotConvergent() { |
1685 | removeAttribute(AttributeList::FunctionIndex, Attribute::Convergent); |
1686 | } |
1687 | |
1688 | /// Determine if the call returns a structure through first |
1689 | /// pointer argument. |
1690 | bool hasStructRetAttr() const { |
1691 | if (getNumArgOperands() == 0) |
1692 | return false; |
1693 | |
1694 | // Be friendly and also check the callee. |
1695 | return paramHasAttr(0, Attribute::StructRet); |
1696 | } |
1697 | |
1698 | /// Determine if any call argument is an aggregate passed by value. |
1699 | bool hasByValArgument() const { |
1700 | return Attrs.hasAttrSomewhere(Attribute::ByVal); |
1701 | } |
1702 | |
1703 | ///@{ |
1704 | // End of attribute API. |
1705 | |
1706 | /// \name Operand Bundle API |
1707 | /// |
1708 | /// This group of methods provides the API to access and manipulate operand |
1709 | /// bundles on this call. |
1710 | /// @{ |
1711 | |
1712 | /// Return the number of operand bundles associated with this User. |
1713 | unsigned getNumOperandBundles() const { |
1714 | return std::distance(bundle_op_info_begin(), bundle_op_info_end()); |
1715 | } |
1716 | |
1717 | /// Return true if this User has any operand bundles. |
1718 | bool hasOperandBundles() const { return getNumOperandBundles() != 0; } |
1719 | |
1720 | /// Return the index of the first bundle operand in the Use array. |
1721 | unsigned getBundleOperandsStartIndex() const { |
1722 | assert(hasOperandBundles() && "Don't call otherwise!")((hasOperandBundles() && "Don't call otherwise!") ? static_cast <void> (0) : __assert_fail ("hasOperandBundles() && \"Don't call otherwise!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1722, __PRETTY_FUNCTION__)); |
1723 | return bundle_op_info_begin()->Begin; |
1724 | } |
1725 | |
1726 | /// Return the index of the last bundle operand in the Use array. |
1727 | unsigned getBundleOperandsEndIndex() const { |
1728 | assert(hasOperandBundles() && "Don't call otherwise!")((hasOperandBundles() && "Don't call otherwise!") ? static_cast <void> (0) : __assert_fail ("hasOperandBundles() && \"Don't call otherwise!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1728, __PRETTY_FUNCTION__)); |
1729 | return bundle_op_info_end()[-1].End; |
1730 | } |
1731 | |
1732 | /// Return true if the operand at index \p Idx is a bundle operand. |
1733 | bool isBundleOperand(unsigned Idx) const { |
1734 | return hasOperandBundles() && Idx >= getBundleOperandsStartIndex() && |
1735 | Idx < getBundleOperandsEndIndex(); |
1736 | } |
1737 | |
1738 | /// Returns true if the use is a bundle operand. |
1739 | bool isBundleOperand(const Use *U) const { |
1740 | assert(this == U->getUser() &&((this == U->getUser() && "Only valid to query with a use of this instruction!" ) ? static_cast<void> (0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1741, __PRETTY_FUNCTION__)) |
1741 | "Only valid to query with a use of this instruction!")((this == U->getUser() && "Only valid to query with a use of this instruction!" ) ? static_cast<void> (0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1741, __PRETTY_FUNCTION__)); |
1742 | return hasOperandBundles() && isBundleOperand(U - op_begin()); |
1743 | } |
1744 | bool isBundleOperand(Value::const_user_iterator UI) const { |
1745 | return isBundleOperand(&UI.getUse()); |
1746 | } |
1747 | |
1748 | /// Return the total number operands (not operand bundles) used by |
1749 | /// every operand bundle in this OperandBundleUser. |
1750 | unsigned getNumTotalBundleOperands() const { |
1751 | if (!hasOperandBundles()) |
1752 | return 0; |
1753 | |
1754 | unsigned Begin = getBundleOperandsStartIndex(); |
1755 | unsigned End = getBundleOperandsEndIndex(); |
1756 | |
1757 | assert(Begin <= End && "Should be!")((Begin <= End && "Should be!") ? static_cast<void > (0) : __assert_fail ("Begin <= End && \"Should be!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1757, __PRETTY_FUNCTION__)); |
1758 | return End - Begin; |
1759 | } |
1760 | |
1761 | /// Return the operand bundle at a specific index. |
1762 | OperandBundleUse getOperandBundleAt(unsigned Index) const { |
1763 | assert(Index < getNumOperandBundles() && "Index out of bounds!")((Index < getNumOperandBundles() && "Index out of bounds!" ) ? static_cast<void> (0) : __assert_fail ("Index < getNumOperandBundles() && \"Index out of bounds!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1763, __PRETTY_FUNCTION__)); |
1764 | return operandBundleFromBundleOpInfo(*(bundle_op_info_begin() + Index)); |
1765 | } |
1766 | |
1767 | /// Return the number of operand bundles with the tag Name attached to |
1768 | /// this instruction. |
1769 | unsigned countOperandBundlesOfType(StringRef Name) const { |
1770 | unsigned Count = 0; |
1771 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) |
1772 | if (getOperandBundleAt(i).getTagName() == Name) |
1773 | Count++; |
1774 | |
1775 | return Count; |
1776 | } |
1777 | |
1778 | /// Return the number of operand bundles with the tag ID attached to |
1779 | /// this instruction. |
1780 | unsigned countOperandBundlesOfType(uint32_t ID) const { |
1781 | unsigned Count = 0; |
1782 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) |
1783 | if (getOperandBundleAt(i).getTagID() == ID) |
1784 | Count++; |
1785 | |
1786 | return Count; |
1787 | } |
1788 | |
1789 | /// Return an operand bundle by name, if present. |
1790 | /// |
1791 | /// It is an error to call this for operand bundle types that may have |
1792 | /// multiple instances of them on the same instruction. |
1793 | Optional<OperandBundleUse> getOperandBundle(StringRef Name) const { |
1794 | assert(countOperandBundlesOfType(Name) < 2 && "Precondition violated!")((countOperandBundlesOfType(Name) < 2 && "Precondition violated!" ) ? static_cast<void> (0) : __assert_fail ("countOperandBundlesOfType(Name) < 2 && \"Precondition violated!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1794, __PRETTY_FUNCTION__)); |
1795 | |
1796 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) { |
1797 | OperandBundleUse U = getOperandBundleAt(i); |
1798 | if (U.getTagName() == Name) |
1799 | return U; |
1800 | } |
1801 | |
1802 | return None; |
1803 | } |
1804 | |
1805 | /// Return an operand bundle by tag ID, if present. |
1806 | /// |
1807 | /// It is an error to call this for operand bundle types that may have |
1808 | /// multiple instances of them on the same instruction. |
1809 | Optional<OperandBundleUse> getOperandBundle(uint32_t ID) const { |
1810 | assert(countOperandBundlesOfType(ID) < 2 && "Precondition violated!")((countOperandBundlesOfType(ID) < 2 && "Precondition violated!" ) ? static_cast<void> (0) : __assert_fail ("countOperandBundlesOfType(ID) < 2 && \"Precondition violated!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1810, __PRETTY_FUNCTION__)); |
1811 | |
1812 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) { |
1813 | OperandBundleUse U = getOperandBundleAt(i); |
1814 | if (U.getTagID() == ID) |
1815 | return U; |
1816 | } |
1817 | |
1818 | return None; |
1819 | } |
1820 | |
1821 | /// Return the list of operand bundles attached to this instruction as |
1822 | /// a vector of OperandBundleDefs. |
1823 | /// |
1824 | /// This function copies the OperandBundeUse instances associated with this |
1825 | /// OperandBundleUser to a vector of OperandBundleDefs. Note: |
1826 | /// OperandBundeUses and OperandBundleDefs are non-trivially *different* |
1827 | /// representations of operand bundles (see documentation above). |
1828 | void getOperandBundlesAsDefs(SmallVectorImpl<OperandBundleDef> &Defs) const { |
1829 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) |
1830 | Defs.emplace_back(getOperandBundleAt(i)); |
1831 | } |
1832 | |
1833 | /// Return the operand bundle for the operand at index OpIdx. |
1834 | /// |
1835 | /// It is an error to call this with an OpIdx that does not correspond to an |
1836 | /// bundle operand. |
1837 | OperandBundleUse getOperandBundleForOperand(unsigned OpIdx) const { |
1838 | return operandBundleFromBundleOpInfo(getBundleOpInfoForOperand(OpIdx)); |
1839 | } |
1840 | |
1841 | /// Return true if this operand bundle user has operand bundles that |
1842 | /// may read from the heap. |
1843 | bool hasReadingOperandBundles() const { |
1844 | // Implementation note: this is a conservative implementation of operand |
1845 | // bundle semantics, where *any* operand bundle forces a callsite to be at |
1846 | // least readonly. |
1847 | return hasOperandBundles(); |
1848 | } |
1849 | |
1850 | /// Return true if this operand bundle user has operand bundles that |
1851 | /// may write to the heap. |
1852 | bool hasClobberingOperandBundles() const { |
1853 | for (auto &BOI : bundle_op_infos()) { |
1854 | if (BOI.Tag->second == LLVMContext::OB_deopt || |
1855 | BOI.Tag->second == LLVMContext::OB_funclet) |
1856 | continue; |
1857 | |
1858 | // This instruction has an operand bundle that is not known to us. |
1859 | // Assume the worst. |
1860 | return true; |
1861 | } |
1862 | |
1863 | return false; |
1864 | } |
1865 | |
1866 | /// Return true if the bundle operand at index \p OpIdx has the |
1867 | /// attribute \p A. |
1868 | bool bundleOperandHasAttr(unsigned OpIdx, Attribute::AttrKind A) const { |
1869 | auto &BOI = getBundleOpInfoForOperand(OpIdx); |
1870 | auto OBU = operandBundleFromBundleOpInfo(BOI); |
1871 | return OBU.operandHasAttr(OpIdx - BOI.Begin, A); |
1872 | } |
1873 | |
1874 | /// Return true if \p Other has the same sequence of operand bundle |
1875 | /// tags with the same number of operands on each one of them as this |
1876 | /// OperandBundleUser. |
1877 | bool hasIdenticalOperandBundleSchema(const CallBase &Other) const { |
1878 | if (getNumOperandBundles() != Other.getNumOperandBundles()) |
1879 | return false; |
1880 | |
1881 | return std::equal(bundle_op_info_begin(), bundle_op_info_end(), |
1882 | Other.bundle_op_info_begin()); |
1883 | } |
1884 | |
1885 | /// Return true if this operand bundle user contains operand bundles |
1886 | /// with tags other than those specified in \p IDs. |
1887 | bool hasOperandBundlesOtherThan(ArrayRef<uint32_t> IDs) const { |
1888 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) { |
1889 | uint32_t ID = getOperandBundleAt(i).getTagID(); |
1890 | if (!is_contained(IDs, ID)) |
1891 | return true; |
1892 | } |
1893 | return false; |
1894 | } |
1895 | |
1896 | /// Is the function attribute S disallowed by some operand bundle on |
1897 | /// this operand bundle user? |
1898 | bool isFnAttrDisallowedByOpBundle(StringRef S) const { |
1899 | // Operand bundles only possibly disallow readnone, readonly and argmenonly |
1900 | // attributes. All String attributes are fine. |
1901 | return false; |
1902 | } |
1903 | |
1904 | /// Is the function attribute A disallowed by some operand bundle on |
1905 | /// this operand bundle user? |
1906 | bool isFnAttrDisallowedByOpBundle(Attribute::AttrKind A) const { |
1907 | switch (A) { |
1908 | default: |
1909 | return false; |
1910 | |
1911 | case Attribute::InaccessibleMemOrArgMemOnly: |
1912 | return hasReadingOperandBundles(); |
1913 | |
1914 | case Attribute::InaccessibleMemOnly: |
1915 | return hasReadingOperandBundles(); |
1916 | |
1917 | case Attribute::ArgMemOnly: |
1918 | return hasReadingOperandBundles(); |
1919 | |
1920 | case Attribute::ReadNone: |
1921 | return hasReadingOperandBundles(); |
1922 | |
1923 | case Attribute::ReadOnly: |
1924 | return hasClobberingOperandBundles(); |
1925 | } |
1926 | |
1927 | llvm_unreachable("switch has a default case!")::llvm::llvm_unreachable_internal("switch has a default case!" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 1927); |
1928 | } |
1929 | |
1930 | /// Used to keep track of an operand bundle. See the main comment on |
1931 | /// OperandBundleUser above. |
1932 | struct BundleOpInfo { |
1933 | /// The operand bundle tag, interned by |
1934 | /// LLVMContextImpl::getOrInsertBundleTag. |
1935 | StringMapEntry<uint32_t> *Tag; |
1936 | |
1937 | /// The index in the Use& vector where operands for this operand |
1938 | /// bundle starts. |
1939 | uint32_t Begin; |
1940 | |
1941 | /// The index in the Use& vector where operands for this operand |
1942 | /// bundle ends. |
1943 | uint32_t End; |
1944 | |
1945 | bool operator==(const BundleOpInfo &Other) const { |
1946 | return Tag == Other.Tag && Begin == Other.Begin && End == Other.End; |
1947 | } |
1948 | }; |
1949 | |
1950 | /// Simple helper function to map a BundleOpInfo to an |
1951 | /// OperandBundleUse. |
1952 | OperandBundleUse |
1953 | operandBundleFromBundleOpInfo(const BundleOpInfo &BOI) const { |
1954 | auto begin = op_begin(); |
1955 | ArrayRef<Use> Inputs(begin + BOI.Begin, begin + BOI.End); |
1956 | return OperandBundleUse(BOI.Tag, Inputs); |
1957 | } |
1958 | |
1959 | using bundle_op_iterator = BundleOpInfo *; |
1960 | using const_bundle_op_iterator = const BundleOpInfo *; |
1961 | |
1962 | /// Return the start of the list of BundleOpInfo instances associated |
1963 | /// with this OperandBundleUser. |
1964 | /// |
1965 | /// OperandBundleUser uses the descriptor area co-allocated with the host User |
1966 | /// to store some meta information about which operands are "normal" operands, |
1967 | /// and which ones belong to some operand bundle. |
1968 | /// |
1969 | /// The layout of an operand bundle user is |
1970 | /// |
1971 | /// +-----------uint32_t End-------------------------------------+ |
1972 | /// | | |
1973 | /// | +--------uint32_t Begin--------------------+ | |
1974 | /// | | | | |
1975 | /// ^ ^ v v |
1976 | /// |------|------|----|----|----|----|----|---------|----|---------|----|----- |
1977 | /// | BOI0 | BOI1 | .. | DU | U0 | U1 | .. | BOI0_U0 | .. | BOI1_U0 | .. | Un |
1978 | /// |------|------|----|----|----|----|----|---------|----|---------|----|----- |
1979 | /// v v ^ ^ |
1980 | /// | | | | |
1981 | /// | +--------uint32_t Begin------------+ | |
1982 | /// | | |
1983 | /// +-----------uint32_t End-----------------------------+ |
1984 | /// |
1985 | /// |
1986 | /// BOI0, BOI1 ... are descriptions of operand bundles in this User's use |
1987 | /// list. These descriptions are installed and managed by this class, and |
1988 | /// they're all instances of OperandBundleUser<T>::BundleOpInfo. |
1989 | /// |
1990 | /// DU is an additional descriptor installed by User's 'operator new' to keep |
1991 | /// track of the 'BOI0 ... BOIN' co-allocation. OperandBundleUser does not |
1992 | /// access or modify DU in any way, it's an implementation detail private to |
1993 | /// User. |
1994 | /// |
1995 | /// The regular Use& vector for the User starts at U0. The operand bundle |
1996 | /// uses are part of the Use& vector, just like normal uses. In the diagram |
1997 | /// above, the operand bundle uses start at BOI0_U0. Each instance of |
1998 | /// BundleOpInfo has information about a contiguous set of uses constituting |
1999 | /// an operand bundle, and the total set of operand bundle uses themselves |
2000 | /// form a contiguous set of uses (i.e. there are no gaps between uses |
2001 | /// corresponding to individual operand bundles). |
2002 | /// |
2003 | /// This class does not know the location of the set of operand bundle uses |
2004 | /// within the use list -- that is decided by the User using this class via |
2005 | /// the BeginIdx argument in populateBundleOperandInfos. |
2006 | /// |
2007 | /// Currently operand bundle users with hung-off operands are not supported. |
2008 | bundle_op_iterator bundle_op_info_begin() { |
2009 | if (!hasDescriptor()) |
2010 | return nullptr; |
2011 | |
2012 | uint8_t *BytesBegin = getDescriptor().begin(); |
2013 | return reinterpret_cast<bundle_op_iterator>(BytesBegin); |
2014 | } |
2015 | |
2016 | /// Return the start of the list of BundleOpInfo instances associated |
2017 | /// with this OperandBundleUser. |
2018 | const_bundle_op_iterator bundle_op_info_begin() const { |
2019 | auto *NonConstThis = const_cast<CallBase *>(this); |
2020 | return NonConstThis->bundle_op_info_begin(); |
2021 | } |
2022 | |
2023 | /// Return the end of the list of BundleOpInfo instances associated |
2024 | /// with this OperandBundleUser. |
2025 | bundle_op_iterator bundle_op_info_end() { |
2026 | if (!hasDescriptor()) |
2027 | return nullptr; |
2028 | |
2029 | uint8_t *BytesEnd = getDescriptor().end(); |
2030 | return reinterpret_cast<bundle_op_iterator>(BytesEnd); |
2031 | } |
2032 | |
2033 | /// Return the end of the list of BundleOpInfo instances associated |
2034 | /// with this OperandBundleUser. |
2035 | const_bundle_op_iterator bundle_op_info_end() const { |
2036 | auto *NonConstThis = const_cast<CallBase *>(this); |
2037 | return NonConstThis->bundle_op_info_end(); |
2038 | } |
2039 | |
2040 | /// Return the range [\p bundle_op_info_begin, \p bundle_op_info_end). |
2041 | iterator_range<bundle_op_iterator> bundle_op_infos() { |
2042 | return make_range(bundle_op_info_begin(), bundle_op_info_end()); |
2043 | } |
2044 | |
2045 | /// Return the range [\p bundle_op_info_begin, \p bundle_op_info_end). |
2046 | iterator_range<const_bundle_op_iterator> bundle_op_infos() const { |
2047 | return make_range(bundle_op_info_begin(), bundle_op_info_end()); |
2048 | } |
2049 | |
2050 | /// Populate the BundleOpInfo instances and the Use& vector from \p |
2051 | /// Bundles. Return the op_iterator pointing to the Use& one past the last |
2052 | /// last bundle operand use. |
2053 | /// |
2054 | /// Each \p OperandBundleDef instance is tracked by a OperandBundleInfo |
2055 | /// instance allocated in this User's descriptor. |
2056 | op_iterator populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles, |
2057 | const unsigned BeginIndex); |
2058 | |
2059 | /// Return the BundleOpInfo for the operand at index OpIdx. |
2060 | /// |
2061 | /// It is an error to call this with an OpIdx that does not correspond to an |
2062 | /// bundle operand. |
2063 | const BundleOpInfo &getBundleOpInfoForOperand(unsigned OpIdx) const { |
2064 | for (auto &BOI : bundle_op_infos()) |
2065 | if (BOI.Begin <= OpIdx && OpIdx < BOI.End) |
2066 | return BOI; |
2067 | |
2068 | llvm_unreachable("Did not find operand bundle for operand!")::llvm::llvm_unreachable_internal("Did not find operand bundle for operand!" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 2068); |
2069 | } |
2070 | |
2071 | protected: |
2072 | /// Return the total number of values used in \p Bundles. |
2073 | static unsigned CountBundleInputs(ArrayRef<OperandBundleDef> Bundles) { |
2074 | unsigned Total = 0; |
2075 | for (auto &B : Bundles) |
2076 | Total += B.input_size(); |
2077 | return Total; |
2078 | } |
2079 | |
2080 | /// @} |
2081 | // End of operand bundle API. |
2082 | |
2083 | private: |
2084 | bool hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const; |
2085 | bool hasFnAttrOnCalledFunction(StringRef Kind) const; |
2086 | |
2087 | template <typename AttrKind> bool hasFnAttrImpl(AttrKind Kind) const { |
2088 | if (Attrs.hasAttribute(AttributeList::FunctionIndex, Kind)) |
2089 | return true; |
2090 | |
2091 | // Operand bundles override attributes on the called function, but don't |
2092 | // override attributes directly present on the call instruction. |
2093 | if (isFnAttrDisallowedByOpBundle(Kind)) |
2094 | return false; |
2095 | |
2096 | return hasFnAttrOnCalledFunction(Kind); |
2097 | } |
2098 | }; |
2099 | |
2100 | template <> |
2101 | struct OperandTraits<CallBase> : public VariadicOperandTraits<CallBase, 1> {}; |
2102 | |
2103 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallBase, Value)CallBase::op_iterator CallBase::op_begin() { return OperandTraits <CallBase>::op_begin(this); } CallBase::const_op_iterator CallBase::op_begin() const { return OperandTraits<CallBase >::op_begin(const_cast<CallBase*>(this)); } CallBase ::op_iterator CallBase::op_end() { return OperandTraits<CallBase >::op_end(this); } CallBase::const_op_iterator CallBase::op_end () const { return OperandTraits<CallBase>::op_end(const_cast <CallBase*>(this)); } Value *CallBase::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<CallBase >::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<CallBase>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 2103, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<CallBase>::op_begin(const_cast<CallBase *>(this))[i_nocapture].get()); } void CallBase::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<CallBase>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<CallBase>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 2103, __PRETTY_FUNCTION__)); OperandTraits<CallBase>:: op_begin(this)[i_nocapture] = Val_nocapture; } unsigned CallBase ::getNumOperands() const { return OperandTraits<CallBase> ::operands(this); } template <int Idx_nocapture> Use & CallBase::Op() { return this->OpFrom<Idx_nocapture>( this); } template <int Idx_nocapture> const Use &CallBase ::Op() const { return this->OpFrom<Idx_nocapture>(this ); } |
2104 | |
2105 | //===----------------------------------------------------------------------===// |
2106 | // FuncletPadInst Class |
2107 | //===----------------------------------------------------------------------===// |
2108 | class FuncletPadInst : public Instruction { |
2109 | private: |
2110 | FuncletPadInst(const FuncletPadInst &CPI); |
2111 | |
2112 | explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
2113 | ArrayRef<Value *> Args, unsigned Values, |
2114 | const Twine &NameStr, Instruction *InsertBefore); |
2115 | explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
2116 | ArrayRef<Value *> Args, unsigned Values, |
2117 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
2118 | |
2119 | void init(Value *ParentPad, ArrayRef<Value *> Args, const Twine &NameStr); |
2120 | |
2121 | protected: |
2122 | // Note: Instruction needs to be a friend here to call cloneImpl. |
2123 | friend class Instruction; |
2124 | friend class CatchPadInst; |
2125 | friend class CleanupPadInst; |
2126 | |
2127 | FuncletPadInst *cloneImpl() const; |
2128 | |
2129 | public: |
2130 | /// Provide fast operand accessors |
2131 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
2132 | |
2133 | /// getNumArgOperands - Return the number of funcletpad arguments. |
2134 | /// |
2135 | unsigned getNumArgOperands() const { return getNumOperands() - 1; } |
2136 | |
2137 | /// Convenience accessors |
2138 | |
2139 | /// Return the outer EH-pad this funclet is nested within. |
2140 | /// |
2141 | /// Note: This returns the associated CatchSwitchInst if this FuncletPadInst |
2142 | /// is a CatchPadInst. |
2143 | Value *getParentPad() const { return Op<-1>(); } |
2144 | void setParentPad(Value *ParentPad) { |
2145 | assert(ParentPad)((ParentPad) ? static_cast<void> (0) : __assert_fail ("ParentPad" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 2145, __PRETTY_FUNCTION__)); |
2146 | Op<-1>() = ParentPad; |
2147 | } |
2148 | |
2149 | /// getArgOperand/setArgOperand - Return/set the i-th funcletpad argument. |
2150 | /// |
2151 | Value *getArgOperand(unsigned i) const { return getOperand(i); } |
2152 | void setArgOperand(unsigned i, Value *v) { setOperand(i, v); } |
2153 | |
2154 | /// arg_operands - iteration adapter for range-for loops. |
2155 | op_range arg_operands() { return op_range(op_begin(), op_end() - 1); } |
2156 | |
2157 | /// arg_operands - iteration adapter for range-for loops. |
2158 | const_op_range arg_operands() const { |
2159 | return const_op_range(op_begin(), op_end() - 1); |
2160 | } |
2161 | |
2162 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
2163 | static bool classof(const Instruction *I) { return I->isFuncletPad(); } |
2164 | static bool classof(const Value *V) { |
2165 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
2166 | } |
2167 | }; |
2168 | |
2169 | template <> |
2170 | struct OperandTraits<FuncletPadInst> |
2171 | : public VariadicOperandTraits<FuncletPadInst, /*MINARITY=*/1> {}; |
2172 | |
2173 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(FuncletPadInst, Value)FuncletPadInst::op_iterator FuncletPadInst::op_begin() { return OperandTraits<FuncletPadInst>::op_begin(this); } FuncletPadInst ::const_op_iterator FuncletPadInst::op_begin() const { return OperandTraits<FuncletPadInst>::op_begin(const_cast< FuncletPadInst*>(this)); } FuncletPadInst::op_iterator FuncletPadInst ::op_end() { return OperandTraits<FuncletPadInst>::op_end (this); } FuncletPadInst::const_op_iterator FuncletPadInst::op_end () const { return OperandTraits<FuncletPadInst>::op_end (const_cast<FuncletPadInst*>(this)); } Value *FuncletPadInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<FuncletPadInst>::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<FuncletPadInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 2173, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<FuncletPadInst>::op_begin(const_cast< FuncletPadInst*>(this))[i_nocapture].get()); } void FuncletPadInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<FuncletPadInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<FuncletPadInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/IR/InstrTypes.h" , 2173, __PRETTY_FUNCTION__)); OperandTraits<FuncletPadInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned FuncletPadInst::getNumOperands() const { return OperandTraits <FuncletPadInst>::operands(this); } template <int Idx_nocapture > Use &FuncletPadInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &FuncletPadInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } |
2174 | |
2175 | } // end namespace llvm |
2176 | |
2177 | #endif // LLVM_IR_INSTRTYPES_H |
1 | //===- llvm/Support/Casting.h - Allow flexible, checked, casts --*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file defines the isa<X>(), cast<X>(), dyn_cast<X>(), cast_or_null<X>(), |
10 | // and dyn_cast_or_null<X>() templates. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #ifndef LLVM_SUPPORT_CASTING_H |
15 | #define LLVM_SUPPORT_CASTING_H |
16 | |
17 | #include "llvm/Support/Compiler.h" |
18 | #include "llvm/Support/type_traits.h" |
19 | #include <cassert> |
20 | #include <memory> |
21 | #include <type_traits> |
22 | |
23 | namespace llvm { |
24 | |
25 | //===----------------------------------------------------------------------===// |
26 | // isa<x> Support Templates |
27 | //===----------------------------------------------------------------------===// |
28 | |
29 | // Define a template that can be specialized by smart pointers to reflect the |
30 | // fact that they are automatically dereferenced, and are not involved with the |
31 | // template selection process... the default implementation is a noop. |
32 | // |
33 | template<typename From> struct simplify_type { |
34 | using SimpleType = From; // The real type this represents... |
35 | |
36 | // An accessor to get the real value... |
37 | static SimpleType &getSimplifiedValue(From &Val) { return Val; } |
38 | }; |
39 | |
40 | template<typename From> struct simplify_type<const From> { |
41 | using NonConstSimpleType = typename simplify_type<From>::SimpleType; |
42 | using SimpleType = |
43 | typename add_const_past_pointer<NonConstSimpleType>::type; |
44 | using RetType = |
45 | typename add_lvalue_reference_if_not_pointer<SimpleType>::type; |
46 | |
47 | static RetType getSimplifiedValue(const From& Val) { |
48 | return simplify_type<From>::getSimplifiedValue(const_cast<From&>(Val)); |
49 | } |
50 | }; |
51 | |
52 | // The core of the implementation of isa<X> is here; To and From should be |
53 | // the names of classes. This template can be specialized to customize the |
54 | // implementation of isa<> without rewriting it from scratch. |
55 | template <typename To, typename From, typename Enabler = void> |
56 | struct isa_impl { |
57 | static inline bool doit(const From &Val) { |
58 | return To::classof(&Val); |
59 | } |
60 | }; |
61 | |
62 | /// Always allow upcasts, and perform no dynamic check for them. |
63 | template <typename To, typename From> |
64 | struct isa_impl< |
65 | To, From, typename std::enable_if<std::is_base_of<To, From>::value>::type> { |
66 | static inline bool doit(const From &) { return true; } |
67 | }; |
68 | |
69 | template <typename To, typename From> struct isa_impl_cl { |
70 | static inline bool doit(const From &Val) { |
71 | return isa_impl<To, From>::doit(Val); |
72 | } |
73 | }; |
74 | |
75 | template <typename To, typename From> struct isa_impl_cl<To, const From> { |
76 | static inline bool doit(const From &Val) { |
77 | return isa_impl<To, From>::doit(Val); |
78 | } |
79 | }; |
80 | |
81 | template <typename To, typename From> |
82 | struct isa_impl_cl<To, const std::unique_ptr<From>> { |
83 | static inline bool doit(const std::unique_ptr<From> &Val) { |
84 | assert(Val && "isa<> used on a null pointer")((Val && "isa<> used on a null pointer") ? static_cast <void> (0) : __assert_fail ("Val && \"isa<> used on a null pointer\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Casting.h" , 84, __PRETTY_FUNCTION__)); |
85 | return isa_impl_cl<To, From>::doit(*Val); |
86 | } |
87 | }; |
88 | |
89 | template <typename To, typename From> struct isa_impl_cl<To, From*> { |
90 | static inline bool doit(const From *Val) { |
91 | assert(Val && "isa<> used on a null pointer")((Val && "isa<> used on a null pointer") ? static_cast <void> (0) : __assert_fail ("Val && \"isa<> used on a null pointer\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Casting.h" , 91, __PRETTY_FUNCTION__)); |
92 | return isa_impl<To, From>::doit(*Val); |
93 | } |
94 | }; |
95 | |
96 | template <typename To, typename From> struct isa_impl_cl<To, From*const> { |
97 | static inline bool doit(const From *Val) { |
98 | assert(Val && "isa<> used on a null pointer")((Val && "isa<> used on a null pointer") ? static_cast <void> (0) : __assert_fail ("Val && \"isa<> used on a null pointer\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Casting.h" , 98, __PRETTY_FUNCTION__)); |
99 | return isa_impl<To, From>::doit(*Val); |
100 | } |
101 | }; |
102 | |
103 | template <typename To, typename From> struct isa_impl_cl<To, const From*> { |
104 | static inline bool doit(const From *Val) { |
105 | assert(Val && "isa<> used on a null pointer")((Val && "isa<> used on a null pointer") ? static_cast <void> (0) : __assert_fail ("Val && \"isa<> used on a null pointer\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Casting.h" , 105, __PRETTY_FUNCTION__)); |
106 | return isa_impl<To, From>::doit(*Val); |
107 | } |
108 | }; |
109 | |
110 | template <typename To, typename From> struct isa_impl_cl<To, const From*const> { |
111 | static inline bool doit(const From *Val) { |
112 | assert(Val && "isa<> used on a null pointer")((Val && "isa<> used on a null pointer") ? static_cast <void> (0) : __assert_fail ("Val && \"isa<> used on a null pointer\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Casting.h" , 112, __PRETTY_FUNCTION__)); |
113 | return isa_impl<To, From>::doit(*Val); |
114 | } |
115 | }; |
116 | |
117 | template<typename To, typename From, typename SimpleFrom> |
118 | struct isa_impl_wrap { |
119 | // When From != SimplifiedType, we can simplify the type some more by using |
120 | // the simplify_type template. |
121 | static bool doit(const From &Val) { |
122 | return isa_impl_wrap<To, SimpleFrom, |
123 | typename simplify_type<SimpleFrom>::SimpleType>::doit( |
124 | simplify_type<const From>::getSimplifiedValue(Val)); |
125 | } |
126 | }; |
127 | |
128 | template<typename To, typename FromTy> |
129 | struct isa_impl_wrap<To, FromTy, FromTy> { |
130 | // When From == SimpleType, we are as simple as we are going to get. |
131 | static bool doit(const FromTy &Val) { |
132 | return isa_impl_cl<To,FromTy>::doit(Val); |
133 | } |
134 | }; |
135 | |
136 | // isa<X> - Return true if the parameter to the template is an instance of the |
137 | // template type argument. Used like this: |
138 | // |
139 | // if (isa<Type>(myVal)) { ... } |
140 | // |
141 | template <class X, class Y> LLVM_NODISCARD[[clang::warn_unused_result]] inline bool isa(const Y &Val) { |
142 | return isa_impl_wrap<X, const Y, |
143 | typename simplify_type<const Y>::SimpleType>::doit(Val); |
144 | } |
145 | |
146 | // isa_and_nonnull<X> - Functionally identical to isa, except that a null value |
147 | // is accepted. |
148 | // |
149 | template <class X, class Y> |
150 | LLVM_NODISCARD[[clang::warn_unused_result]] inline bool isa_and_nonnull(const Y &Val) { |
151 | if (!Val) |
152 | return false; |
153 | return isa<X>(Val); |
154 | } |
155 | |
156 | //===----------------------------------------------------------------------===// |
157 | // cast<x> Support Templates |
158 | //===----------------------------------------------------------------------===// |
159 | |
160 | template<class To, class From> struct cast_retty; |
161 | |
162 | // Calculate what type the 'cast' function should return, based on a requested |
163 | // type of To and a source type of From. |
164 | template<class To, class From> struct cast_retty_impl { |
165 | using ret_type = To &; // Normal case, return Ty& |
166 | }; |
167 | template<class To, class From> struct cast_retty_impl<To, const From> { |
168 | using ret_type = const To &; // Normal case, return Ty& |
169 | }; |
170 | |
171 | template<class To, class From> struct cast_retty_impl<To, From*> { |
172 | using ret_type = To *; // Pointer arg case, return Ty* |
173 | }; |
174 | |
175 | template<class To, class From> struct cast_retty_impl<To, const From*> { |
176 | using ret_type = const To *; // Constant pointer arg case, return const Ty* |
177 | }; |
178 | |
179 | template<class To, class From> struct cast_retty_impl<To, const From*const> { |
180 | using ret_type = const To *; // Constant pointer arg case, return const Ty* |
181 | }; |
182 | |
183 | template <class To, class From> |
184 | struct cast_retty_impl<To, std::unique_ptr<From>> { |
185 | private: |
186 | using PointerType = typename cast_retty_impl<To, From *>::ret_type; |
187 | using ResultType = typename std::remove_pointer<PointerType>::type; |
188 | |
189 | public: |
190 | using ret_type = std::unique_ptr<ResultType>; |
191 | }; |
192 | |
193 | template<class To, class From, class SimpleFrom> |
194 | struct cast_retty_wrap { |
195 | // When the simplified type and the from type are not the same, use the type |
196 | // simplifier to reduce the type, then reuse cast_retty_impl to get the |
197 | // resultant type. |
198 | using ret_type = typename cast_retty<To, SimpleFrom>::ret_type; |
199 | }; |
200 | |
201 | template<class To, class FromTy> |
202 | struct cast_retty_wrap<To, FromTy, FromTy> { |
203 | // When the simplified type is equal to the from type, use it directly. |
204 | using ret_type = typename cast_retty_impl<To,FromTy>::ret_type; |
205 | }; |
206 | |
207 | template<class To, class From> |
208 | struct cast_retty { |
209 | using ret_type = typename cast_retty_wrap< |
210 | To, From, typename simplify_type<From>::SimpleType>::ret_type; |
211 | }; |
212 | |
213 | // Ensure the non-simple values are converted using the simplify_type template |
214 | // that may be specialized by smart pointers... |
215 | // |
216 | template<class To, class From, class SimpleFrom> struct cast_convert_val { |
217 | // This is not a simple type, use the template to simplify it... |
218 | static typename cast_retty<To, From>::ret_type doit(From &Val) { |
219 | return cast_convert_val<To, SimpleFrom, |
220 | typename simplify_type<SimpleFrom>::SimpleType>::doit( |
221 | simplify_type<From>::getSimplifiedValue(Val)); |
222 | } |
223 | }; |
224 | |
225 | template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> { |
226 | // This _is_ a simple type, just cast it. |
227 | static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) { |
228 | typename cast_retty<To, FromTy>::ret_type Res2 |
229 | = (typename cast_retty<To, FromTy>::ret_type)const_cast<FromTy&>(Val); |
230 | return Res2; |
231 | } |
232 | }; |
233 | |
234 | template <class X> struct is_simple_type { |
235 | static const bool value = |
236 | std::is_same<X, typename simplify_type<X>::SimpleType>::value; |
237 | }; |
238 | |
239 | // cast<X> - Return the argument parameter cast to the specified type. This |
240 | // casting operator asserts that the type is correct, so it does not return null |
241 | // on failure. It does not allow a null argument (use cast_or_null for that). |
242 | // It is typically used like this: |
243 | // |
244 | // cast<Instruction>(myVal)->getParent() |
245 | // |
246 | template <class X, class Y> |
247 | inline typename std::enable_if<!is_simple_type<Y>::value, |
248 | typename cast_retty<X, const Y>::ret_type>::type |
249 | cast(const Y &Val) { |
250 | assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!")((isa<X>(Val) && "cast<Ty>() argument of incompatible type!" ) ? static_cast<void> (0) : __assert_fail ("isa<X>(Val) && \"cast<Ty>() argument of incompatible type!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Casting.h" , 250, __PRETTY_FUNCTION__)); |
251 | return cast_convert_val< |
252 | X, const Y, typename simplify_type<const Y>::SimpleType>::doit(Val); |
253 | } |
254 | |
255 | template <class X, class Y> |
256 | inline typename cast_retty<X, Y>::ret_type cast(Y &Val) { |
257 | assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!")((isa<X>(Val) && "cast<Ty>() argument of incompatible type!" ) ? static_cast<void> (0) : __assert_fail ("isa<X>(Val) && \"cast<Ty>() argument of incompatible type!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Casting.h" , 257, __PRETTY_FUNCTION__)); |
258 | return cast_convert_val<X, Y, |
259 | typename simplify_type<Y>::SimpleType>::doit(Val); |
260 | } |
261 | |
262 | template <class X, class Y> |
263 | inline typename cast_retty<X, Y *>::ret_type cast(Y *Val) { |
264 | assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!")((isa<X>(Val) && "cast<Ty>() argument of incompatible type!" ) ? static_cast<void> (0) : __assert_fail ("isa<X>(Val) && \"cast<Ty>() argument of incompatible type!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Casting.h" , 264, __PRETTY_FUNCTION__)); |
265 | return cast_convert_val<X, Y*, |
266 | typename simplify_type<Y*>::SimpleType>::doit(Val); |
267 | } |
268 | |
269 | template <class X, class Y> |
270 | inline typename cast_retty<X, std::unique_ptr<Y>>::ret_type |
271 | cast(std::unique_ptr<Y> &&Val) { |
272 | assert(isa<X>(Val.get()) && "cast<Ty>() argument of incompatible type!")((isa<X>(Val.get()) && "cast<Ty>() argument of incompatible type!" ) ? static_cast<void> (0) : __assert_fail ("isa<X>(Val.get()) && \"cast<Ty>() argument of incompatible type!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Casting.h" , 272, __PRETTY_FUNCTION__)); |
273 | using ret_type = typename cast_retty<X, std::unique_ptr<Y>>::ret_type; |
274 | return ret_type( |
275 | cast_convert_val<X, Y *, typename simplify_type<Y *>::SimpleType>::doit( |
276 | Val.release())); |
277 | } |
278 | |
279 | // cast_or_null<X> - Functionally identical to cast, except that a null value is |
280 | // accepted. |
281 | // |
282 | template <class X, class Y> |
283 | LLVM_NODISCARD[[clang::warn_unused_result]] inline |
284 | typename std::enable_if<!is_simple_type<Y>::value, |
285 | typename cast_retty<X, const Y>::ret_type>::type |
286 | cast_or_null(const Y &Val) { |
287 | if (!Val) |
288 | return nullptr; |
289 | assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!")((isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!" ) ? static_cast<void> (0) : __assert_fail ("isa<X>(Val) && \"cast_or_null<Ty>() argument of incompatible type!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Casting.h" , 289, __PRETTY_FUNCTION__)); |
290 | return cast<X>(Val); |
291 | } |
292 | |
293 | template <class X, class Y> |
294 | LLVM_NODISCARD[[clang::warn_unused_result]] inline |
295 | typename std::enable_if<!is_simple_type<Y>::value, |
296 | typename cast_retty<X, Y>::ret_type>::type |
297 | cast_or_null(Y &Val) { |
298 | if (!Val) |
299 | return nullptr; |
300 | assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!")((isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!" ) ? static_cast<void> (0) : __assert_fail ("isa<X>(Val) && \"cast_or_null<Ty>() argument of incompatible type!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Casting.h" , 300, __PRETTY_FUNCTION__)); |
301 | return cast<X>(Val); |
302 | } |
303 | |
304 | template <class X, class Y> |
305 | LLVM_NODISCARD[[clang::warn_unused_result]] inline typename cast_retty<X, Y *>::ret_type |
306 | cast_or_null(Y *Val) { |
307 | if (!Val) return nullptr; |
308 | assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!")((isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!" ) ? static_cast<void> (0) : __assert_fail ("isa<X>(Val) && \"cast_or_null<Ty>() argument of incompatible type!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Casting.h" , 308, __PRETTY_FUNCTION__)); |
309 | return cast<X>(Val); |
310 | } |
311 | |
312 | template <class X, class Y> |
313 | inline typename cast_retty<X, std::unique_ptr<Y>>::ret_type |
314 | cast_or_null(std::unique_ptr<Y> &&Val) { |
315 | if (!Val) |
316 | return nullptr; |
317 | return cast<X>(std::move(Val)); |
318 | } |
319 | |
320 | // dyn_cast<X> - Return the argument parameter cast to the specified type. This |
321 | // casting operator returns null if the argument is of the wrong type, so it can |
322 | // be used to test for a type as well as cast if successful. This should be |
323 | // used in the context of an if statement like this: |
324 | // |
325 | // if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... } |
326 | // |
327 | |
328 | template <class X, class Y> |
329 | LLVM_NODISCARD[[clang::warn_unused_result]] inline |
330 | typename std::enable_if<!is_simple_type<Y>::value, |
331 | typename cast_retty<X, const Y>::ret_type>::type |
332 | dyn_cast(const Y &Val) { |
333 | return isa<X>(Val) ? cast<X>(Val) : nullptr; |
334 | } |
335 | |
336 | template <class X, class Y> |
337 | LLVM_NODISCARD[[clang::warn_unused_result]] inline typename cast_retty<X, Y>::ret_type dyn_cast(Y &Val) { |
338 | return isa<X>(Val) ? cast<X>(Val) : nullptr; |
339 | } |
340 | |
341 | template <class X, class Y> |
342 | LLVM_NODISCARD[[clang::warn_unused_result]] inline typename cast_retty<X, Y *>::ret_type dyn_cast(Y *Val) { |
343 | return isa<X>(Val) ? cast<X>(Val) : nullptr; |
344 | } |
345 | |
346 | // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null |
347 | // value is accepted. |
348 | // |
349 | template <class X, class Y> |
350 | LLVM_NODISCARD[[clang::warn_unused_result]] inline |
351 | typename std::enable_if<!is_simple_type<Y>::value, |
352 | typename cast_retty<X, const Y>::ret_type>::type |
353 | dyn_cast_or_null(const Y &Val) { |
354 | return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr; |
355 | } |
356 | |
357 | template <class X, class Y> |
358 | LLVM_NODISCARD[[clang::warn_unused_result]] inline |
359 | typename std::enable_if<!is_simple_type<Y>::value, |
360 | typename cast_retty<X, Y>::ret_type>::type |
361 | dyn_cast_or_null(Y &Val) { |
362 | return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr; |
363 | } |
364 | |
365 | template <class X, class Y> |
366 | LLVM_NODISCARD[[clang::warn_unused_result]] inline typename cast_retty<X, Y *>::ret_type |
367 | dyn_cast_or_null(Y *Val) { |
368 | return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr; |
369 | } |
370 | |
371 | // unique_dyn_cast<X> - Given a unique_ptr<Y>, try to return a unique_ptr<X>, |
372 | // taking ownership of the input pointer iff isa<X>(Val) is true. If the |
373 | // cast is successful, From refers to nullptr on exit and the casted value |
374 | // is returned. If the cast is unsuccessful, the function returns nullptr |
375 | // and From is unchanged. |
376 | template <class X, class Y> |
377 | LLVM_NODISCARD[[clang::warn_unused_result]] inline auto unique_dyn_cast(std::unique_ptr<Y> &Val) |
378 | -> decltype(cast<X>(Val)) { |
379 | if (!isa<X>(Val)) |
380 | return nullptr; |
381 | return cast<X>(std::move(Val)); |
382 | } |
383 | |
384 | template <class X, class Y> |
385 | LLVM_NODISCARD[[clang::warn_unused_result]] inline auto unique_dyn_cast(std::unique_ptr<Y> &&Val) |
386 | -> decltype(cast<X>(Val)) { |
387 | return unique_dyn_cast<X, Y>(Val); |
388 | } |
389 | |
390 | // dyn_cast_or_null<X> - Functionally identical to unique_dyn_cast, except that |
391 | // a null value is accepted. |
392 | template <class X, class Y> |
393 | LLVM_NODISCARD[[clang::warn_unused_result]] inline auto unique_dyn_cast_or_null(std::unique_ptr<Y> &Val) |
394 | -> decltype(cast<X>(Val)) { |
395 | if (!Val) |
396 | return nullptr; |
397 | return unique_dyn_cast<X, Y>(Val); |
398 | } |
399 | |
400 | template <class X, class Y> |
401 | LLVM_NODISCARD[[clang::warn_unused_result]] inline auto unique_dyn_cast_or_null(std::unique_ptr<Y> &&Val) |
402 | -> decltype(cast<X>(Val)) { |
403 | return unique_dyn_cast_or_null<X, Y>(Val); |
404 | } |
405 | |
406 | } // end namespace llvm |
407 | |
408 | #endif // LLVM_SUPPORT_CASTING_H |