File: | llvm/include/llvm/IR/Instructions.h |
Warning: | line 1728, column 19 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- InstCombineSelect.cpp ----------------------------------------------===// | ||||||||||
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 implements the visitSelect function. | ||||||||||
10 | // | ||||||||||
11 | //===----------------------------------------------------------------------===// | ||||||||||
12 | |||||||||||
13 | #include "InstCombineInternal.h" | ||||||||||
14 | #include "llvm/ADT/APInt.h" | ||||||||||
15 | #include "llvm/ADT/Optional.h" | ||||||||||
16 | #include "llvm/ADT/STLExtras.h" | ||||||||||
17 | #include "llvm/ADT/SmallVector.h" | ||||||||||
18 | #include "llvm/Analysis/AssumptionCache.h" | ||||||||||
19 | #include "llvm/Analysis/CmpInstAnalysis.h" | ||||||||||
20 | #include "llvm/Analysis/InstructionSimplify.h" | ||||||||||
21 | #include "llvm/Analysis/ValueTracking.h" | ||||||||||
22 | #include "llvm/IR/BasicBlock.h" | ||||||||||
23 | #include "llvm/IR/Constant.h" | ||||||||||
24 | #include "llvm/IR/Constants.h" | ||||||||||
25 | #include "llvm/IR/DerivedTypes.h" | ||||||||||
26 | #include "llvm/IR/IRBuilder.h" | ||||||||||
27 | #include "llvm/IR/InstrTypes.h" | ||||||||||
28 | #include "llvm/IR/Instruction.h" | ||||||||||
29 | #include "llvm/IR/Instructions.h" | ||||||||||
30 | #include "llvm/IR/IntrinsicInst.h" | ||||||||||
31 | #include "llvm/IR/Intrinsics.h" | ||||||||||
32 | #include "llvm/IR/Operator.h" | ||||||||||
33 | #include "llvm/IR/PatternMatch.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 "llvm/Support/KnownBits.h" | ||||||||||
40 | #include "llvm/Transforms/InstCombine/InstCombineWorklist.h" | ||||||||||
41 | #include <cassert> | ||||||||||
42 | #include <utility> | ||||||||||
43 | |||||||||||
44 | using namespace llvm; | ||||||||||
45 | using namespace PatternMatch; | ||||||||||
46 | |||||||||||
47 | #define DEBUG_TYPE"instcombine" "instcombine" | ||||||||||
48 | |||||||||||
49 | static Value *createMinMax(InstCombiner::BuilderTy &Builder, | ||||||||||
50 | SelectPatternFlavor SPF, Value *A, Value *B) { | ||||||||||
51 | CmpInst::Predicate Pred = getMinMaxPred(SPF); | ||||||||||
52 | assert(CmpInst::isIntPredicate(Pred) && "Expected integer predicate")((CmpInst::isIntPredicate(Pred) && "Expected integer predicate" ) ? static_cast<void> (0) : __assert_fail ("CmpInst::isIntPredicate(Pred) && \"Expected integer predicate\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp" , 52, __PRETTY_FUNCTION__)); | ||||||||||
53 | return Builder.CreateSelect(Builder.CreateICmp(Pred, A, B), A, B); | ||||||||||
54 | } | ||||||||||
55 | |||||||||||
56 | /// Replace a select operand based on an equality comparison with the identity | ||||||||||
57 | /// constant of a binop. | ||||||||||
58 | static Instruction *foldSelectBinOpIdentity(SelectInst &Sel, | ||||||||||
59 | const TargetLibraryInfo &TLI, | ||||||||||
60 | InstCombiner &IC) { | ||||||||||
61 | // The select condition must be an equality compare with a constant operand. | ||||||||||
62 | Value *X; | ||||||||||
63 | Constant *C; | ||||||||||
64 | CmpInst::Predicate Pred; | ||||||||||
65 | if (!match(Sel.getCondition(), m_Cmp(Pred, m_Value(X), m_Constant(C)))) | ||||||||||
66 | return nullptr; | ||||||||||
67 | |||||||||||
68 | bool IsEq; | ||||||||||
69 | if (ICmpInst::isEquality(Pred)) | ||||||||||
70 | IsEq = Pred == ICmpInst::ICMP_EQ; | ||||||||||
71 | else if (Pred == FCmpInst::FCMP_OEQ) | ||||||||||
72 | IsEq = true; | ||||||||||
73 | else if (Pred == FCmpInst::FCMP_UNE) | ||||||||||
74 | IsEq = false; | ||||||||||
75 | else | ||||||||||
76 | return nullptr; | ||||||||||
77 | |||||||||||
78 | // A select operand must be a binop. | ||||||||||
79 | BinaryOperator *BO; | ||||||||||
80 | if (!match(Sel.getOperand(IsEq ? 1 : 2), m_BinOp(BO))) | ||||||||||
81 | return nullptr; | ||||||||||
82 | |||||||||||
83 | // The compare constant must be the identity constant for that binop. | ||||||||||
84 | // If this a floating-point compare with 0.0, any zero constant will do. | ||||||||||
85 | Type *Ty = BO->getType(); | ||||||||||
86 | Constant *IdC = ConstantExpr::getBinOpIdentity(BO->getOpcode(), Ty, true); | ||||||||||
87 | if (IdC != C) { | ||||||||||
88 | if (!IdC || !CmpInst::isFPPredicate(Pred)) | ||||||||||
89 | return nullptr; | ||||||||||
90 | if (!match(IdC, m_AnyZeroFP()) || !match(C, m_AnyZeroFP())) | ||||||||||
91 | return nullptr; | ||||||||||
92 | } | ||||||||||
93 | |||||||||||
94 | // Last, match the compare variable operand with a binop operand. | ||||||||||
95 | Value *Y; | ||||||||||
96 | if (!BO->isCommutative() && !match(BO, m_BinOp(m_Value(Y), m_Specific(X)))) | ||||||||||
97 | return nullptr; | ||||||||||
98 | if (!match(BO, m_c_BinOp(m_Value(Y), m_Specific(X)))) | ||||||||||
99 | return nullptr; | ||||||||||
100 | |||||||||||
101 | // +0.0 compares equal to -0.0, and so it does not behave as required for this | ||||||||||
102 | // transform. Bail out if we can not exclude that possibility. | ||||||||||
103 | if (isa<FPMathOperator>(BO)) | ||||||||||
104 | if (!BO->hasNoSignedZeros() && !CannotBeNegativeZero(Y, &TLI)) | ||||||||||
105 | return nullptr; | ||||||||||
106 | |||||||||||
107 | // BO = binop Y, X | ||||||||||
108 | // S = { select (cmp eq X, C), BO, ? } or { select (cmp ne X, C), ?, BO } | ||||||||||
109 | // => | ||||||||||
110 | // S = { select (cmp eq X, C), Y, ? } or { select (cmp ne X, C), ?, Y } | ||||||||||
111 | return IC.replaceOperand(Sel, IsEq ? 1 : 2, Y); | ||||||||||
112 | } | ||||||||||
113 | |||||||||||
114 | /// This folds: | ||||||||||
115 | /// select (icmp eq (and X, C1)), TC, FC | ||||||||||
116 | /// iff C1 is a power 2 and the difference between TC and FC is a power-of-2. | ||||||||||
117 | /// To something like: | ||||||||||
118 | /// (shr (and (X, C1)), (log2(C1) - log2(TC-FC))) + FC | ||||||||||
119 | /// Or: | ||||||||||
120 | /// (shl (and (X, C1)), (log2(TC-FC) - log2(C1))) + FC | ||||||||||
121 | /// With some variations depending if FC is larger than TC, or the shift | ||||||||||
122 | /// isn't needed, or the bit widths don't match. | ||||||||||
123 | static Value *foldSelectICmpAnd(SelectInst &Sel, ICmpInst *Cmp, | ||||||||||
124 | InstCombiner::BuilderTy &Builder) { | ||||||||||
125 | const APInt *SelTC, *SelFC; | ||||||||||
126 | if (!match(Sel.getTrueValue(), m_APInt(SelTC)) || | ||||||||||
127 | !match(Sel.getFalseValue(), m_APInt(SelFC))) | ||||||||||
128 | return nullptr; | ||||||||||
129 | |||||||||||
130 | // If this is a vector select, we need a vector compare. | ||||||||||
131 | Type *SelType = Sel.getType(); | ||||||||||
132 | if (SelType->isVectorTy() != Cmp->getType()->isVectorTy()) | ||||||||||
133 | return nullptr; | ||||||||||
134 | |||||||||||
135 | Value *V; | ||||||||||
136 | APInt AndMask; | ||||||||||
137 | bool CreateAnd = false; | ||||||||||
138 | ICmpInst::Predicate Pred = Cmp->getPredicate(); | ||||||||||
139 | if (ICmpInst::isEquality(Pred)) { | ||||||||||
140 | if (!match(Cmp->getOperand(1), m_Zero())) | ||||||||||
141 | return nullptr; | ||||||||||
142 | |||||||||||
143 | V = Cmp->getOperand(0); | ||||||||||
144 | const APInt *AndRHS; | ||||||||||
145 | if (!match(V, m_And(m_Value(), m_Power2(AndRHS)))) | ||||||||||
146 | return nullptr; | ||||||||||
147 | |||||||||||
148 | AndMask = *AndRHS; | ||||||||||
149 | } else if (decomposeBitTestICmp(Cmp->getOperand(0), Cmp->getOperand(1), | ||||||||||
150 | Pred, V, AndMask)) { | ||||||||||
151 | assert(ICmpInst::isEquality(Pred) && "Not equality test?")((ICmpInst::isEquality(Pred) && "Not equality test?") ? static_cast<void> (0) : __assert_fail ("ICmpInst::isEquality(Pred) && \"Not equality test?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp" , 151, __PRETTY_FUNCTION__)); | ||||||||||
152 | if (!AndMask.isPowerOf2()) | ||||||||||
153 | return nullptr; | ||||||||||
154 | |||||||||||
155 | CreateAnd = true; | ||||||||||
156 | } else { | ||||||||||
157 | return nullptr; | ||||||||||
158 | } | ||||||||||
159 | |||||||||||
160 | // In general, when both constants are non-zero, we would need an offset to | ||||||||||
161 | // replace the select. This would require more instructions than we started | ||||||||||
162 | // with. But there's one special-case that we handle here because it can | ||||||||||
163 | // simplify/reduce the instructions. | ||||||||||
164 | APInt TC = *SelTC; | ||||||||||
165 | APInt FC = *SelFC; | ||||||||||
166 | if (!TC.isNullValue() && !FC.isNullValue()) { | ||||||||||
167 | // If the select constants differ by exactly one bit and that's the same | ||||||||||
168 | // bit that is masked and checked by the select condition, the select can | ||||||||||
169 | // be replaced by bitwise logic to set/clear one bit of the constant result. | ||||||||||
170 | if (TC.getBitWidth() != AndMask.getBitWidth() || (TC ^ FC) != AndMask) | ||||||||||
171 | return nullptr; | ||||||||||
172 | if (CreateAnd) { | ||||||||||
173 | // If we have to create an 'and', then we must kill the cmp to not | ||||||||||
174 | // increase the instruction count. | ||||||||||
175 | if (!Cmp->hasOneUse()) | ||||||||||
176 | return nullptr; | ||||||||||
177 | V = Builder.CreateAnd(V, ConstantInt::get(SelType, AndMask)); | ||||||||||
178 | } | ||||||||||
179 | bool ExtraBitInTC = TC.ugt(FC); | ||||||||||
180 | if (Pred == ICmpInst::ICMP_EQ) { | ||||||||||
181 | // If the masked bit in V is clear, clear or set the bit in the result: | ||||||||||
182 | // (V & AndMaskC) == 0 ? TC : FC --> (V & AndMaskC) ^ TC | ||||||||||
183 | // (V & AndMaskC) == 0 ? TC : FC --> (V & AndMaskC) | TC | ||||||||||
184 | Constant *C = ConstantInt::get(SelType, TC); | ||||||||||
185 | return ExtraBitInTC ? Builder.CreateXor(V, C) : Builder.CreateOr(V, C); | ||||||||||
186 | } | ||||||||||
187 | if (Pred == ICmpInst::ICMP_NE) { | ||||||||||
188 | // If the masked bit in V is set, set or clear the bit in the result: | ||||||||||
189 | // (V & AndMaskC) != 0 ? TC : FC --> (V & AndMaskC) | FC | ||||||||||
190 | // (V & AndMaskC) != 0 ? TC : FC --> (V & AndMaskC) ^ FC | ||||||||||
191 | Constant *C = ConstantInt::get(SelType, FC); | ||||||||||
192 | return ExtraBitInTC ? Builder.CreateOr(V, C) : Builder.CreateXor(V, C); | ||||||||||
193 | } | ||||||||||
194 | llvm_unreachable("Only expecting equality predicates")::llvm::llvm_unreachable_internal("Only expecting equality predicates" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp" , 194); | ||||||||||
195 | } | ||||||||||
196 | |||||||||||
197 | // Make sure one of the select arms is a power-of-2. | ||||||||||
198 | if (!TC.isPowerOf2() && !FC.isPowerOf2()) | ||||||||||
199 | return nullptr; | ||||||||||
200 | |||||||||||
201 | // Determine which shift is needed to transform result of the 'and' into the | ||||||||||
202 | // desired result. | ||||||||||
203 | const APInt &ValC = !TC.isNullValue() ? TC : FC; | ||||||||||
204 | unsigned ValZeros = ValC.logBase2(); | ||||||||||
205 | unsigned AndZeros = AndMask.logBase2(); | ||||||||||
206 | |||||||||||
207 | // Insert the 'and' instruction on the input to the truncate. | ||||||||||
208 | if (CreateAnd) | ||||||||||
209 | V = Builder.CreateAnd(V, ConstantInt::get(V->getType(), AndMask)); | ||||||||||
210 | |||||||||||
211 | // If types don't match, we can still convert the select by introducing a zext | ||||||||||
212 | // or a trunc of the 'and'. | ||||||||||
213 | if (ValZeros > AndZeros) { | ||||||||||
214 | V = Builder.CreateZExtOrTrunc(V, SelType); | ||||||||||
215 | V = Builder.CreateShl(V, ValZeros - AndZeros); | ||||||||||
216 | } else if (ValZeros < AndZeros) { | ||||||||||
217 | V = Builder.CreateLShr(V, AndZeros - ValZeros); | ||||||||||
218 | V = Builder.CreateZExtOrTrunc(V, SelType); | ||||||||||
219 | } else { | ||||||||||
220 | V = Builder.CreateZExtOrTrunc(V, SelType); | ||||||||||
221 | } | ||||||||||
222 | |||||||||||
223 | // Okay, now we know that everything is set up, we just don't know whether we | ||||||||||
224 | // have a icmp_ne or icmp_eq and whether the true or false val is the zero. | ||||||||||
225 | bool ShouldNotVal = !TC.isNullValue(); | ||||||||||
226 | ShouldNotVal ^= Pred == ICmpInst::ICMP_NE; | ||||||||||
227 | if (ShouldNotVal) | ||||||||||
228 | V = Builder.CreateXor(V, ValC); | ||||||||||
229 | |||||||||||
230 | return V; | ||||||||||
231 | } | ||||||||||
232 | |||||||||||
233 | /// We want to turn code that looks like this: | ||||||||||
234 | /// %C = or %A, %B | ||||||||||
235 | /// %D = select %cond, %C, %A | ||||||||||
236 | /// into: | ||||||||||
237 | /// %C = select %cond, %B, 0 | ||||||||||
238 | /// %D = or %A, %C | ||||||||||
239 | /// | ||||||||||
240 | /// Assuming that the specified instruction is an operand to the select, return | ||||||||||
241 | /// a bitmask indicating which operands of this instruction are foldable if they | ||||||||||
242 | /// equal the other incoming value of the select. | ||||||||||
243 | static unsigned getSelectFoldableOperands(BinaryOperator *I) { | ||||||||||
244 | switch (I->getOpcode()) { | ||||||||||
245 | case Instruction::Add: | ||||||||||
246 | case Instruction::Mul: | ||||||||||
247 | case Instruction::And: | ||||||||||
248 | case Instruction::Or: | ||||||||||
249 | case Instruction::Xor: | ||||||||||
250 | return 3; // Can fold through either operand. | ||||||||||
251 | case Instruction::Sub: // Can only fold on the amount subtracted. | ||||||||||
252 | case Instruction::Shl: // Can only fold on the shift amount. | ||||||||||
253 | case Instruction::LShr: | ||||||||||
254 | case Instruction::AShr: | ||||||||||
255 | return 1; | ||||||||||
256 | default: | ||||||||||
257 | return 0; // Cannot fold | ||||||||||
258 | } | ||||||||||
259 | } | ||||||||||
260 | |||||||||||
261 | /// For the same transformation as the previous function, return the identity | ||||||||||
262 | /// constant that goes into the select. | ||||||||||
263 | static APInt getSelectFoldableConstant(BinaryOperator *I) { | ||||||||||
264 | switch (I->getOpcode()) { | ||||||||||
265 | default: llvm_unreachable("This cannot happen!")::llvm::llvm_unreachable_internal("This cannot happen!", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp" , 265); | ||||||||||
266 | case Instruction::Add: | ||||||||||
267 | case Instruction::Sub: | ||||||||||
268 | case Instruction::Or: | ||||||||||
269 | case Instruction::Xor: | ||||||||||
270 | case Instruction::Shl: | ||||||||||
271 | case Instruction::LShr: | ||||||||||
272 | case Instruction::AShr: | ||||||||||
273 | return APInt::getNullValue(I->getType()->getScalarSizeInBits()); | ||||||||||
274 | case Instruction::And: | ||||||||||
275 | return APInt::getAllOnesValue(I->getType()->getScalarSizeInBits()); | ||||||||||
276 | case Instruction::Mul: | ||||||||||
277 | return APInt(I->getType()->getScalarSizeInBits(), 1); | ||||||||||
278 | } | ||||||||||
279 | } | ||||||||||
280 | |||||||||||
281 | /// We have (select c, TI, FI), and we know that TI and FI have the same opcode. | ||||||||||
282 | Instruction *InstCombiner::foldSelectOpOp(SelectInst &SI, Instruction *TI, | ||||||||||
283 | Instruction *FI) { | ||||||||||
284 | // Don't break up min/max patterns. The hasOneUse checks below prevent that | ||||||||||
285 | // for most cases, but vector min/max with bitcasts can be transformed. If the | ||||||||||
286 | // one-use restrictions are eased for other patterns, we still don't want to | ||||||||||
287 | // obfuscate min/max. | ||||||||||
288 | if ((match(&SI, m_SMin(m_Value(), m_Value())) || | ||||||||||
289 | match(&SI, m_SMax(m_Value(), m_Value())) || | ||||||||||
290 | match(&SI, m_UMin(m_Value(), m_Value())) || | ||||||||||
291 | match(&SI, m_UMax(m_Value(), m_Value())))) | ||||||||||
292 | return nullptr; | ||||||||||
293 | |||||||||||
294 | // If this is a cast from the same type, merge. | ||||||||||
295 | Value *Cond = SI.getCondition(); | ||||||||||
296 | Type *CondTy = Cond->getType(); | ||||||||||
297 | if (TI->getNumOperands() == 1 && TI->isCast()) { | ||||||||||
298 | Type *FIOpndTy = FI->getOperand(0)->getType(); | ||||||||||
299 | if (TI->getOperand(0)->getType() != FIOpndTy) | ||||||||||
300 | return nullptr; | ||||||||||
301 | |||||||||||
302 | // The select condition may be a vector. We may only change the operand | ||||||||||
303 | // type if the vector width remains the same (and matches the condition). | ||||||||||
304 | if (CondTy->isVectorTy()) { | ||||||||||
305 | if (!FIOpndTy->isVectorTy()) | ||||||||||
306 | return nullptr; | ||||||||||
307 | if (CondTy->getVectorNumElements() != FIOpndTy->getVectorNumElements()) | ||||||||||
308 | return nullptr; | ||||||||||
309 | |||||||||||
310 | // TODO: If the backend knew how to deal with casts better, we could | ||||||||||
311 | // remove this limitation. For now, there's too much potential to create | ||||||||||
312 | // worse codegen by promoting the select ahead of size-altering casts | ||||||||||
313 | // (PR28160). | ||||||||||
314 | // | ||||||||||
315 | // Note that ValueTracking's matchSelectPattern() looks through casts | ||||||||||
316 | // without checking 'hasOneUse' when it matches min/max patterns, so this | ||||||||||
317 | // transform may end up happening anyway. | ||||||||||
318 | if (TI->getOpcode() != Instruction::BitCast && | ||||||||||
319 | (!TI->hasOneUse() || !FI->hasOneUse())) | ||||||||||
320 | return nullptr; | ||||||||||
321 | } else if (!TI->hasOneUse() || !FI->hasOneUse()) { | ||||||||||
322 | // TODO: The one-use restrictions for a scalar select could be eased if | ||||||||||
323 | // the fold of a select in visitLoadInst() was enhanced to match a pattern | ||||||||||
324 | // that includes a cast. | ||||||||||
325 | return nullptr; | ||||||||||
326 | } | ||||||||||
327 | |||||||||||
328 | // Fold this by inserting a select from the input values. | ||||||||||
329 | Value *NewSI = | ||||||||||
330 | Builder.CreateSelect(Cond, TI->getOperand(0), FI->getOperand(0), | ||||||||||
331 | SI.getName() + ".v", &SI); | ||||||||||
332 | return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI, | ||||||||||
333 | TI->getType()); | ||||||||||
334 | } | ||||||||||
335 | |||||||||||
336 | // Cond ? -X : -Y --> -(Cond ? X : Y) | ||||||||||
337 | Value *X, *Y; | ||||||||||
338 | if (match(TI, m_FNeg(m_Value(X))) && match(FI, m_FNeg(m_Value(Y))) && | ||||||||||
339 | (TI->hasOneUse() || FI->hasOneUse())) { | ||||||||||
340 | Value *NewSel = Builder.CreateSelect(Cond, X, Y, SI.getName() + ".v", &SI); | ||||||||||
341 | // TODO: Remove the hack for the binop form when the unary op is optimized | ||||||||||
342 | // properly with all IR passes. | ||||||||||
343 | if (TI->getOpcode() != Instruction::FNeg) | ||||||||||
344 | return UnaryOperator::CreateFNegFMF(NewSel, cast<BinaryOperator>(TI)); | ||||||||||
345 | return UnaryOperator::CreateFNeg(NewSel); | ||||||||||
346 | } | ||||||||||
347 | |||||||||||
348 | // Only handle binary operators (including two-operand getelementptr) with | ||||||||||
349 | // one-use here. As with the cast case above, it may be possible to relax the | ||||||||||
350 | // one-use constraint, but that needs be examined carefully since it may not | ||||||||||
351 | // reduce the total number of instructions. | ||||||||||
352 | if (TI->getNumOperands() != 2 || FI->getNumOperands() != 2 || | ||||||||||
353 | (!isa<BinaryOperator>(TI) && !isa<GetElementPtrInst>(TI)) || | ||||||||||
354 | !TI->hasOneUse() || !FI->hasOneUse()) | ||||||||||
355 | return nullptr; | ||||||||||
356 | |||||||||||
357 | // Figure out if the operations have any operands in common. | ||||||||||
358 | Value *MatchOp, *OtherOpT, *OtherOpF; | ||||||||||
359 | bool MatchIsOpZero; | ||||||||||
360 | if (TI->getOperand(0) == FI->getOperand(0)) { | ||||||||||
361 | MatchOp = TI->getOperand(0); | ||||||||||
362 | OtherOpT = TI->getOperand(1); | ||||||||||
363 | OtherOpF = FI->getOperand(1); | ||||||||||
364 | MatchIsOpZero = true; | ||||||||||
365 | } else if (TI->getOperand(1) == FI->getOperand(1)) { | ||||||||||
366 | MatchOp = TI->getOperand(1); | ||||||||||
367 | OtherOpT = TI->getOperand(0); | ||||||||||
368 | OtherOpF = FI->getOperand(0); | ||||||||||
369 | MatchIsOpZero = false; | ||||||||||
370 | } else if (!TI->isCommutative()) { | ||||||||||
371 | return nullptr; | ||||||||||
372 | } else if (TI->getOperand(0) == FI->getOperand(1)) { | ||||||||||
373 | MatchOp = TI->getOperand(0); | ||||||||||
374 | OtherOpT = TI->getOperand(1); | ||||||||||
375 | OtherOpF = FI->getOperand(0); | ||||||||||
376 | MatchIsOpZero = true; | ||||||||||
377 | } else if (TI->getOperand(1) == FI->getOperand(0)) { | ||||||||||
378 | MatchOp = TI->getOperand(1); | ||||||||||
379 | OtherOpT = TI->getOperand(0); | ||||||||||
380 | OtherOpF = FI->getOperand(1); | ||||||||||
381 | MatchIsOpZero = true; | ||||||||||
382 | } else { | ||||||||||
383 | return nullptr; | ||||||||||
384 | } | ||||||||||
385 | |||||||||||
386 | // If the select condition is a vector, the operands of the original select's | ||||||||||
387 | // operands also must be vectors. This may not be the case for getelementptr | ||||||||||
388 | // for example. | ||||||||||
389 | if (CondTy->isVectorTy() && (!OtherOpT->getType()->isVectorTy() || | ||||||||||
390 | !OtherOpF->getType()->isVectorTy())) | ||||||||||
391 | return nullptr; | ||||||||||
392 | |||||||||||
393 | // If we reach here, they do have operations in common. | ||||||||||
394 | Value *NewSI = Builder.CreateSelect(Cond, OtherOpT, OtherOpF, | ||||||||||
395 | SI.getName() + ".v", &SI); | ||||||||||
396 | Value *Op0 = MatchIsOpZero ? MatchOp : NewSI; | ||||||||||
397 | Value *Op1 = MatchIsOpZero ? NewSI : MatchOp; | ||||||||||
398 | if (auto *BO = dyn_cast<BinaryOperator>(TI)) { | ||||||||||
399 | BinaryOperator *NewBO = BinaryOperator::Create(BO->getOpcode(), Op0, Op1); | ||||||||||
400 | NewBO->copyIRFlags(TI); | ||||||||||
401 | NewBO->andIRFlags(FI); | ||||||||||
402 | return NewBO; | ||||||||||
403 | } | ||||||||||
404 | if (auto *TGEP = dyn_cast<GetElementPtrInst>(TI)) { | ||||||||||
405 | auto *FGEP = cast<GetElementPtrInst>(FI); | ||||||||||
406 | Type *ElementType = TGEP->getResultElementType(); | ||||||||||
407 | return TGEP->isInBounds() && FGEP->isInBounds() | ||||||||||
408 | ? GetElementPtrInst::CreateInBounds(ElementType, Op0, {Op1}) | ||||||||||
409 | : GetElementPtrInst::Create(ElementType, Op0, {Op1}); | ||||||||||
410 | } | ||||||||||
411 | llvm_unreachable("Expected BinaryOperator or GEP")::llvm::llvm_unreachable_internal("Expected BinaryOperator or GEP" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp" , 411); | ||||||||||
412 | return nullptr; | ||||||||||
413 | } | ||||||||||
414 | |||||||||||
415 | static bool isSelect01(const APInt &C1I, const APInt &C2I) { | ||||||||||
416 | if (!C1I.isNullValue() && !C2I.isNullValue()) // One side must be zero. | ||||||||||
417 | return false; | ||||||||||
418 | return C1I.isOneValue() || C1I.isAllOnesValue() || | ||||||||||
419 | C2I.isOneValue() || C2I.isAllOnesValue(); | ||||||||||
420 | } | ||||||||||
421 | |||||||||||
422 | /// Try to fold the select into one of the operands to allow further | ||||||||||
423 | /// optimization. | ||||||||||
424 | Instruction *InstCombiner::foldSelectIntoOp(SelectInst &SI, Value *TrueVal, | ||||||||||
425 | Value *FalseVal) { | ||||||||||
426 | // See the comment above GetSelectFoldableOperands for a description of the | ||||||||||
427 | // transformation we are doing here. | ||||||||||
428 | if (auto *TVI = dyn_cast<BinaryOperator>(TrueVal)) { | ||||||||||
429 | if (TVI->hasOneUse() && !isa<Constant>(FalseVal)) { | ||||||||||
430 | if (unsigned SFO = getSelectFoldableOperands(TVI)) { | ||||||||||
431 | unsigned OpToFold = 0; | ||||||||||
432 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { | ||||||||||
433 | OpToFold = 1; | ||||||||||
434 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { | ||||||||||
435 | OpToFold = 2; | ||||||||||
436 | } | ||||||||||
437 | |||||||||||
438 | if (OpToFold) { | ||||||||||
439 | APInt CI = getSelectFoldableConstant(TVI); | ||||||||||
440 | Value *OOp = TVI->getOperand(2-OpToFold); | ||||||||||
441 | // Avoid creating select between 2 constants unless it's selecting | ||||||||||
442 | // between 0, 1 and -1. | ||||||||||
443 | const APInt *OOpC; | ||||||||||
444 | bool OOpIsAPInt = match(OOp, m_APInt(OOpC)); | ||||||||||
445 | if (!isa<Constant>(OOp) || (OOpIsAPInt && isSelect01(CI, *OOpC))) { | ||||||||||
446 | Value *C = ConstantInt::get(OOp->getType(), CI); | ||||||||||
447 | Value *NewSel = Builder.CreateSelect(SI.getCondition(), OOp, C); | ||||||||||
448 | NewSel->takeName(TVI); | ||||||||||
449 | BinaryOperator *BO = BinaryOperator::Create(TVI->getOpcode(), | ||||||||||
450 | FalseVal, NewSel); | ||||||||||
451 | BO->copyIRFlags(TVI); | ||||||||||
452 | return BO; | ||||||||||
453 | } | ||||||||||
454 | } | ||||||||||
455 | } | ||||||||||
456 | } | ||||||||||
457 | } | ||||||||||
458 | |||||||||||
459 | if (auto *FVI = dyn_cast<BinaryOperator>(FalseVal)) { | ||||||||||
460 | if (FVI->hasOneUse() && !isa<Constant>(TrueVal)) { | ||||||||||
461 | if (unsigned SFO = getSelectFoldableOperands(FVI)) { | ||||||||||
462 | unsigned OpToFold = 0; | ||||||||||
463 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { | ||||||||||
464 | OpToFold = 1; | ||||||||||
465 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { | ||||||||||
466 | OpToFold = 2; | ||||||||||
467 | } | ||||||||||
468 | |||||||||||
469 | if (OpToFold) { | ||||||||||
470 | APInt CI = getSelectFoldableConstant(FVI); | ||||||||||
471 | Value *OOp = FVI->getOperand(2-OpToFold); | ||||||||||
472 | // Avoid creating select between 2 constants unless it's selecting | ||||||||||
473 | // between 0, 1 and -1. | ||||||||||
474 | const APInt *OOpC; | ||||||||||
475 | bool OOpIsAPInt = match(OOp, m_APInt(OOpC)); | ||||||||||
476 | if (!isa<Constant>(OOp) || (OOpIsAPInt && isSelect01(CI, *OOpC))) { | ||||||||||
477 | Value *C = ConstantInt::get(OOp->getType(), CI); | ||||||||||
478 | Value *NewSel = Builder.CreateSelect(SI.getCondition(), C, OOp); | ||||||||||
479 | NewSel->takeName(FVI); | ||||||||||
480 | BinaryOperator *BO = BinaryOperator::Create(FVI->getOpcode(), | ||||||||||
481 | TrueVal, NewSel); | ||||||||||
482 | BO->copyIRFlags(FVI); | ||||||||||
483 | return BO; | ||||||||||
484 | } | ||||||||||
485 | } | ||||||||||
486 | } | ||||||||||
487 | } | ||||||||||
488 | } | ||||||||||
489 | |||||||||||
490 | return nullptr; | ||||||||||
491 | } | ||||||||||
492 | |||||||||||
493 | /// We want to turn: | ||||||||||
494 | /// (select (icmp eq (and X, Y), 0), (and (lshr X, Z), 1), 1) | ||||||||||
495 | /// into: | ||||||||||
496 | /// zext (icmp ne i32 (and X, (or Y, (shl 1, Z))), 0) | ||||||||||
497 | /// Note: | ||||||||||
498 | /// Z may be 0 if lshr is missing. | ||||||||||
499 | /// Worst-case scenario is that we will replace 5 instructions with 5 different | ||||||||||
500 | /// instructions, but we got rid of select. | ||||||||||
501 | static Instruction *foldSelectICmpAndAnd(Type *SelType, const ICmpInst *Cmp, | ||||||||||
502 | Value *TVal, Value *FVal, | ||||||||||
503 | InstCombiner::BuilderTy &Builder) { | ||||||||||
504 | if (!(Cmp->hasOneUse() && Cmp->getOperand(0)->hasOneUse() && | ||||||||||
505 | Cmp->getPredicate() == ICmpInst::ICMP_EQ && | ||||||||||
506 | match(Cmp->getOperand(1), m_Zero()) && match(FVal, m_One()))) | ||||||||||
507 | return nullptr; | ||||||||||
508 | |||||||||||
509 | // The TrueVal has general form of: and %B, 1 | ||||||||||
510 | Value *B; | ||||||||||
511 | if (!match(TVal, m_OneUse(m_And(m_Value(B), m_One())))) | ||||||||||
512 | return nullptr; | ||||||||||
513 | |||||||||||
514 | // Where %B may be optionally shifted: lshr %X, %Z. | ||||||||||
515 | Value *X, *Z; | ||||||||||
516 | const bool HasShift = match(B, m_OneUse(m_LShr(m_Value(X), m_Value(Z)))); | ||||||||||
517 | if (!HasShift) | ||||||||||
518 | X = B; | ||||||||||
519 | |||||||||||
520 | Value *Y; | ||||||||||
521 | if (!match(Cmp->getOperand(0), m_c_And(m_Specific(X), m_Value(Y)))) | ||||||||||
522 | return nullptr; | ||||||||||
523 | |||||||||||
524 | // ((X & Y) == 0) ? ((X >> Z) & 1) : 1 --> (X & (Y | (1 << Z))) != 0 | ||||||||||
525 | // ((X & Y) == 0) ? (X & 1) : 1 --> (X & (Y | 1)) != 0 | ||||||||||
526 | Constant *One = ConstantInt::get(SelType, 1); | ||||||||||
527 | Value *MaskB = HasShift ? Builder.CreateShl(One, Z) : One; | ||||||||||
528 | Value *FullMask = Builder.CreateOr(Y, MaskB); | ||||||||||
529 | Value *MaskedX = Builder.CreateAnd(X, FullMask); | ||||||||||
530 | Value *ICmpNeZero = Builder.CreateIsNotNull(MaskedX); | ||||||||||
531 | return new ZExtInst(ICmpNeZero, SelType); | ||||||||||
532 | } | ||||||||||
533 | |||||||||||
534 | /// We want to turn: | ||||||||||
535 | /// (select (icmp sgt x, C), lshr (X, Y), ashr (X, Y)); iff C s>= -1 | ||||||||||
536 | /// (select (icmp slt x, C), ashr (X, Y), lshr (X, Y)); iff C s>= 0 | ||||||||||
537 | /// into: | ||||||||||
538 | /// ashr (X, Y) | ||||||||||
539 | static Value *foldSelectICmpLshrAshr(const ICmpInst *IC, Value *TrueVal, | ||||||||||
540 | Value *FalseVal, | ||||||||||
541 | InstCombiner::BuilderTy &Builder) { | ||||||||||
542 | ICmpInst::Predicate Pred = IC->getPredicate(); | ||||||||||
543 | Value *CmpLHS = IC->getOperand(0); | ||||||||||
544 | Value *CmpRHS = IC->getOperand(1); | ||||||||||
545 | if (!CmpRHS->getType()->isIntOrIntVectorTy()) | ||||||||||
546 | return nullptr; | ||||||||||
547 | |||||||||||
548 | Value *X, *Y; | ||||||||||
549 | unsigned Bitwidth = CmpRHS->getType()->getScalarSizeInBits(); | ||||||||||
550 | if ((Pred != ICmpInst::ICMP_SGT || | ||||||||||
551 | !match(CmpRHS, | ||||||||||
552 | m_SpecificInt_ICMP(ICmpInst::ICMP_SGE, APInt(Bitwidth, -1)))) && | ||||||||||
553 | (Pred != ICmpInst::ICMP_SLT || | ||||||||||
554 | !match(CmpRHS, | ||||||||||
555 | m_SpecificInt_ICMP(ICmpInst::ICMP_SGE, APInt(Bitwidth, 0))))) | ||||||||||
556 | return nullptr; | ||||||||||
557 | |||||||||||
558 | // Canonicalize so that ashr is in FalseVal. | ||||||||||
559 | if (Pred == ICmpInst::ICMP_SLT) | ||||||||||
560 | std::swap(TrueVal, FalseVal); | ||||||||||
561 | |||||||||||
562 | if (match(TrueVal, m_LShr(m_Value(X), m_Value(Y))) && | ||||||||||
563 | match(FalseVal, m_AShr(m_Specific(X), m_Specific(Y))) && | ||||||||||
564 | match(CmpLHS, m_Specific(X))) { | ||||||||||
565 | const auto *Ashr = cast<Instruction>(FalseVal); | ||||||||||
566 | // if lshr is not exact and ashr is, this new ashr must not be exact. | ||||||||||
567 | bool IsExact = Ashr->isExact() && cast<Instruction>(TrueVal)->isExact(); | ||||||||||
568 | return Builder.CreateAShr(X, Y, IC->getName(), IsExact); | ||||||||||
569 | } | ||||||||||
570 | |||||||||||
571 | return nullptr; | ||||||||||
572 | } | ||||||||||
573 | |||||||||||
574 | /// We want to turn: | ||||||||||
575 | /// (select (icmp eq (and X, C1), 0), Y, (or Y, C2)) | ||||||||||
576 | /// into: | ||||||||||
577 | /// (or (shl (and X, C1), C3), Y) | ||||||||||
578 | /// iff: | ||||||||||
579 | /// C1 and C2 are both powers of 2 | ||||||||||
580 | /// where: | ||||||||||
581 | /// C3 = Log(C2) - Log(C1) | ||||||||||
582 | /// | ||||||||||
583 | /// This transform handles cases where: | ||||||||||
584 | /// 1. The icmp predicate is inverted | ||||||||||
585 | /// 2. The select operands are reversed | ||||||||||
586 | /// 3. The magnitude of C2 and C1 are flipped | ||||||||||
587 | static Value *foldSelectICmpAndOr(const ICmpInst *IC, Value *TrueVal, | ||||||||||
588 | Value *FalseVal, | ||||||||||
589 | InstCombiner::BuilderTy &Builder) { | ||||||||||
590 | // Only handle integer compares. Also, if this is a vector select, we need a | ||||||||||
591 | // vector compare. | ||||||||||
592 | if (!TrueVal->getType()->isIntOrIntVectorTy() || | ||||||||||
593 | TrueVal->getType()->isVectorTy() != IC->getType()->isVectorTy()) | ||||||||||
594 | return nullptr; | ||||||||||
595 | |||||||||||
596 | Value *CmpLHS = IC->getOperand(0); | ||||||||||
597 | Value *CmpRHS = IC->getOperand(1); | ||||||||||
598 | |||||||||||
599 | Value *V; | ||||||||||
600 | unsigned C1Log; | ||||||||||
601 | bool IsEqualZero; | ||||||||||
602 | bool NeedAnd = false; | ||||||||||
603 | if (IC->isEquality()) { | ||||||||||
604 | if (!match(CmpRHS, m_Zero())) | ||||||||||
605 | return nullptr; | ||||||||||
606 | |||||||||||
607 | const APInt *C1; | ||||||||||
608 | if (!match(CmpLHS, m_And(m_Value(), m_Power2(C1)))) | ||||||||||
609 | return nullptr; | ||||||||||
610 | |||||||||||
611 | V = CmpLHS; | ||||||||||
612 | C1Log = C1->logBase2(); | ||||||||||
613 | IsEqualZero = IC->getPredicate() == ICmpInst::ICMP_EQ; | ||||||||||
614 | } else if (IC->getPredicate() == ICmpInst::ICMP_SLT || | ||||||||||
615 | IC->getPredicate() == ICmpInst::ICMP_SGT) { | ||||||||||
616 | // We also need to recognize (icmp slt (trunc (X)), 0) and | ||||||||||
617 | // (icmp sgt (trunc (X)), -1). | ||||||||||
618 | IsEqualZero = IC->getPredicate() == ICmpInst::ICMP_SGT; | ||||||||||
619 | if ((IsEqualZero && !match(CmpRHS, m_AllOnes())) || | ||||||||||
620 | (!IsEqualZero && !match(CmpRHS, m_Zero()))) | ||||||||||
621 | return nullptr; | ||||||||||
622 | |||||||||||
623 | if (!match(CmpLHS, m_OneUse(m_Trunc(m_Value(V))))) | ||||||||||
624 | return nullptr; | ||||||||||
625 | |||||||||||
626 | C1Log = CmpLHS->getType()->getScalarSizeInBits() - 1; | ||||||||||
627 | NeedAnd = true; | ||||||||||
628 | } else { | ||||||||||
629 | return nullptr; | ||||||||||
630 | } | ||||||||||
631 | |||||||||||
632 | const APInt *C2; | ||||||||||
633 | bool OrOnTrueVal = false; | ||||||||||
634 | bool OrOnFalseVal = match(FalseVal, m_Or(m_Specific(TrueVal), m_Power2(C2))); | ||||||||||
635 | if (!OrOnFalseVal) | ||||||||||
636 | OrOnTrueVal = match(TrueVal, m_Or(m_Specific(FalseVal), m_Power2(C2))); | ||||||||||
637 | |||||||||||
638 | if (!OrOnFalseVal && !OrOnTrueVal) | ||||||||||
639 | return nullptr; | ||||||||||
640 | |||||||||||
641 | Value *Y = OrOnFalseVal ? TrueVal : FalseVal; | ||||||||||
642 | |||||||||||
643 | unsigned C2Log = C2->logBase2(); | ||||||||||
644 | |||||||||||
645 | bool NeedXor = (!IsEqualZero && OrOnFalseVal) || (IsEqualZero && OrOnTrueVal); | ||||||||||
646 | bool NeedShift = C1Log != C2Log; | ||||||||||
647 | bool NeedZExtTrunc = Y->getType()->getScalarSizeInBits() != | ||||||||||
648 | V->getType()->getScalarSizeInBits(); | ||||||||||
649 | |||||||||||
650 | // Make sure we don't create more instructions than we save. | ||||||||||
651 | Value *Or = OrOnFalseVal ? FalseVal : TrueVal; | ||||||||||
652 | if ((NeedShift + NeedXor + NeedZExtTrunc) > | ||||||||||
653 | (IC->hasOneUse() + Or->hasOneUse())) | ||||||||||
654 | return nullptr; | ||||||||||
655 | |||||||||||
656 | if (NeedAnd) { | ||||||||||
657 | // Insert the AND instruction on the input to the truncate. | ||||||||||
658 | APInt C1 = APInt::getOneBitSet(V->getType()->getScalarSizeInBits(), C1Log); | ||||||||||
659 | V = Builder.CreateAnd(V, ConstantInt::get(V->getType(), C1)); | ||||||||||
660 | } | ||||||||||
661 | |||||||||||
662 | if (C2Log > C1Log) { | ||||||||||
663 | V = Builder.CreateZExtOrTrunc(V, Y->getType()); | ||||||||||
664 | V = Builder.CreateShl(V, C2Log - C1Log); | ||||||||||
665 | } else if (C1Log > C2Log) { | ||||||||||
666 | V = Builder.CreateLShr(V, C1Log - C2Log); | ||||||||||
667 | V = Builder.CreateZExtOrTrunc(V, Y->getType()); | ||||||||||
668 | } else | ||||||||||
669 | V = Builder.CreateZExtOrTrunc(V, Y->getType()); | ||||||||||
670 | |||||||||||
671 | if (NeedXor) | ||||||||||
672 | V = Builder.CreateXor(V, *C2); | ||||||||||
673 | |||||||||||
674 | return Builder.CreateOr(V, Y); | ||||||||||
675 | } | ||||||||||
676 | |||||||||||
677 | /// Transform patterns such as (a > b) ? a - b : 0 into usub.sat(a, b). | ||||||||||
678 | /// There are 8 commuted/swapped variants of this pattern. | ||||||||||
679 | /// TODO: Also support a - UMIN(a,b) patterns. | ||||||||||
680 | static Value *canonicalizeSaturatedSubtract(const ICmpInst *ICI, | ||||||||||
681 | const Value *TrueVal, | ||||||||||
682 | const Value *FalseVal, | ||||||||||
683 | InstCombiner::BuilderTy &Builder) { | ||||||||||
684 | ICmpInst::Predicate Pred = ICI->getPredicate(); | ||||||||||
685 | if (!ICmpInst::isUnsigned(Pred)) | ||||||||||
686 | return nullptr; | ||||||||||
687 | |||||||||||
688 | // (b > a) ? 0 : a - b -> (b <= a) ? a - b : 0 | ||||||||||
689 | if (match(TrueVal, m_Zero())) { | ||||||||||
690 | Pred = ICmpInst::getInversePredicate(Pred); | ||||||||||
691 | std::swap(TrueVal, FalseVal); | ||||||||||
692 | } | ||||||||||
693 | if (!match(FalseVal, m_Zero())) | ||||||||||
694 | return nullptr; | ||||||||||
695 | |||||||||||
696 | Value *A = ICI->getOperand(0); | ||||||||||
697 | Value *B = ICI->getOperand(1); | ||||||||||
698 | if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_ULT) { | ||||||||||
699 | // (b < a) ? a - b : 0 -> (a > b) ? a - b : 0 | ||||||||||
700 | std::swap(A, B); | ||||||||||
701 | Pred = ICmpInst::getSwappedPredicate(Pred); | ||||||||||
702 | } | ||||||||||
703 | |||||||||||
704 | assert((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_UGT) &&(((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_UGT) && "Unexpected isUnsigned predicate!") ? static_cast<void> (0) : __assert_fail ("(Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_UGT) && \"Unexpected isUnsigned predicate!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp" , 705, __PRETTY_FUNCTION__)) | ||||||||||
705 | "Unexpected isUnsigned predicate!")(((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_UGT) && "Unexpected isUnsigned predicate!") ? static_cast<void> (0) : __assert_fail ("(Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_UGT) && \"Unexpected isUnsigned predicate!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp" , 705, __PRETTY_FUNCTION__)); | ||||||||||
706 | |||||||||||
707 | // Ensure the sub is of the form: | ||||||||||
708 | // (a > b) ? a - b : 0 -> usub.sat(a, b) | ||||||||||
709 | // (a > b) ? b - a : 0 -> -usub.sat(a, b) | ||||||||||
710 | // Checking for both a-b and a+(-b) as a constant. | ||||||||||
711 | bool IsNegative = false; | ||||||||||
712 | const APInt *C; | ||||||||||
713 | if (match(TrueVal, m_Sub(m_Specific(B), m_Specific(A))) || | ||||||||||
714 | (match(A, m_APInt(C)) && | ||||||||||
715 | match(TrueVal, m_Add(m_Specific(B), m_SpecificInt(-*C))))) | ||||||||||
716 | IsNegative = true; | ||||||||||
717 | else if (!match(TrueVal, m_Sub(m_Specific(A), m_Specific(B))) && | ||||||||||
718 | !(match(B, m_APInt(C)) && | ||||||||||
719 | match(TrueVal, m_Add(m_Specific(A), m_SpecificInt(-*C))))) | ||||||||||
720 | return nullptr; | ||||||||||
721 | |||||||||||
722 | // If we are adding a negate and the sub and icmp are used anywhere else, we | ||||||||||
723 | // would end up with more instructions. | ||||||||||
724 | if (IsNegative && !TrueVal->hasOneUse() && !ICI->hasOneUse()) | ||||||||||
725 | return nullptr; | ||||||||||
726 | |||||||||||
727 | // (a > b) ? a - b : 0 -> usub.sat(a, b) | ||||||||||
728 | // (a > b) ? b - a : 0 -> -usub.sat(a, b) | ||||||||||
729 | Value *Result = Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, A, B); | ||||||||||
730 | if (IsNegative) | ||||||||||
731 | Result = Builder.CreateNeg(Result); | ||||||||||
732 | return Result; | ||||||||||
733 | } | ||||||||||
734 | |||||||||||
735 | static Value *canonicalizeSaturatedAdd(ICmpInst *Cmp, Value *TVal, Value *FVal, | ||||||||||
736 | InstCombiner::BuilderTy &Builder) { | ||||||||||
737 | if (!Cmp->hasOneUse()) | ||||||||||
738 | return nullptr; | ||||||||||
739 | |||||||||||
740 | // Match unsigned saturated add with constant. | ||||||||||
741 | Value *Cmp0 = Cmp->getOperand(0); | ||||||||||
742 | Value *Cmp1 = Cmp->getOperand(1); | ||||||||||
743 | ICmpInst::Predicate Pred = Cmp->getPredicate(); | ||||||||||
744 | Value *X; | ||||||||||
745 | const APInt *C, *CmpC; | ||||||||||
746 | if (Pred == ICmpInst::ICMP_ULT && | ||||||||||
747 | match(TVal, m_Add(m_Value(X), m_APInt(C))) && X == Cmp0 && | ||||||||||
748 | match(FVal, m_AllOnes()) && match(Cmp1, m_APInt(CmpC)) && *CmpC == ~*C) { | ||||||||||
749 | // (X u< ~C) ? (X + C) : -1 --> uadd.sat(X, C) | ||||||||||
750 | return Builder.CreateBinaryIntrinsic( | ||||||||||
751 | Intrinsic::uadd_sat, X, ConstantInt::get(X->getType(), *C)); | ||||||||||
752 | } | ||||||||||
753 | |||||||||||
754 | // Match unsigned saturated add of 2 variables with an unnecessary 'not'. | ||||||||||
755 | // There are 8 commuted variants. | ||||||||||
756 | // Canonicalize -1 (saturated result) to true value of the select. Just | ||||||||||
757 | // swapping the compare operands is legal, because the selected value is the | ||||||||||
758 | // same in case of equality, so we can interchange u< and u<=. | ||||||||||
759 | if (match(FVal, m_AllOnes())) { | ||||||||||
760 | std::swap(TVal, FVal); | ||||||||||
761 | std::swap(Cmp0, Cmp1); | ||||||||||
762 | } | ||||||||||
763 | if (!match(TVal, m_AllOnes())) | ||||||||||
764 | return nullptr; | ||||||||||
765 | |||||||||||
766 | // Canonicalize predicate to 'ULT'. | ||||||||||
767 | if (Pred == ICmpInst::ICMP_UGT) { | ||||||||||
768 | Pred = ICmpInst::ICMP_ULT; | ||||||||||
769 | std::swap(Cmp0, Cmp1); | ||||||||||
770 | } | ||||||||||
771 | if (Pred != ICmpInst::ICMP_ULT) | ||||||||||
772 | return nullptr; | ||||||||||
773 | |||||||||||
774 | // Match unsigned saturated add of 2 variables with an unnecessary 'not'. | ||||||||||
775 | Value *Y; | ||||||||||
776 | if (match(Cmp0, m_Not(m_Value(X))) && | ||||||||||
777 | match(FVal, m_c_Add(m_Specific(X), m_Value(Y))) && Y == Cmp1) { | ||||||||||
778 | // (~X u< Y) ? -1 : (X + Y) --> uadd.sat(X, Y) | ||||||||||
779 | // (~X u< Y) ? -1 : (Y + X) --> uadd.sat(X, Y) | ||||||||||
780 | return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, X, Y); | ||||||||||
781 | } | ||||||||||
782 | // The 'not' op may be included in the sum but not the compare. | ||||||||||
783 | X = Cmp0; | ||||||||||
784 | Y = Cmp1; | ||||||||||
785 | if (match(FVal, m_c_Add(m_Not(m_Specific(X)), m_Specific(Y)))) { | ||||||||||
786 | // (X u< Y) ? -1 : (~X + Y) --> uadd.sat(~X, Y) | ||||||||||
787 | // (X u< Y) ? -1 : (Y + ~X) --> uadd.sat(Y, ~X) | ||||||||||
788 | BinaryOperator *BO = cast<BinaryOperator>(FVal); | ||||||||||
789 | return Builder.CreateBinaryIntrinsic( | ||||||||||
790 | Intrinsic::uadd_sat, BO->getOperand(0), BO->getOperand(1)); | ||||||||||
791 | } | ||||||||||
792 | // The overflow may be detected via the add wrapping round. | ||||||||||
793 | if (match(Cmp0, m_c_Add(m_Specific(Cmp1), m_Value(Y))) && | ||||||||||
794 | match(FVal, m_c_Add(m_Specific(Cmp1), m_Specific(Y)))) { | ||||||||||
795 | // ((X + Y) u< X) ? -1 : (X + Y) --> uadd.sat(X, Y) | ||||||||||
796 | // ((X + Y) u< Y) ? -1 : (X + Y) --> uadd.sat(X, Y) | ||||||||||
797 | return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, Cmp1, Y); | ||||||||||
798 | } | ||||||||||
799 | |||||||||||
800 | return nullptr; | ||||||||||
801 | } | ||||||||||
802 | |||||||||||
803 | /// Fold the following code sequence: | ||||||||||
804 | /// \code | ||||||||||
805 | /// int a = ctlz(x & -x); | ||||||||||
806 | // x ? 31 - a : a; | ||||||||||
807 | /// \code | ||||||||||
808 | /// | ||||||||||
809 | /// into: | ||||||||||
810 | /// cttz(x) | ||||||||||
811 | static Instruction *foldSelectCtlzToCttz(ICmpInst *ICI, Value *TrueVal, | ||||||||||
812 | Value *FalseVal, | ||||||||||
813 | InstCombiner::BuilderTy &Builder) { | ||||||||||
814 | unsigned BitWidth = TrueVal->getType()->getScalarSizeInBits(); | ||||||||||
815 | if (!ICI->isEquality() || !match(ICI->getOperand(1), m_Zero())) | ||||||||||
816 | return nullptr; | ||||||||||
817 | |||||||||||
818 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) | ||||||||||
819 | std::swap(TrueVal, FalseVal); | ||||||||||
820 | |||||||||||
821 | if (!match(FalseVal, | ||||||||||
822 | m_Xor(m_Deferred(TrueVal), m_SpecificInt(BitWidth - 1)))) | ||||||||||
823 | return nullptr; | ||||||||||
824 | |||||||||||
825 | if (!match(TrueVal, m_Intrinsic<Intrinsic::ctlz>())) | ||||||||||
826 | return nullptr; | ||||||||||
827 | |||||||||||
828 | Value *X = ICI->getOperand(0); | ||||||||||
829 | auto *II = cast<IntrinsicInst>(TrueVal); | ||||||||||
830 | if (!match(II->getOperand(0), m_c_And(m_Specific(X), m_Neg(m_Specific(X))))) | ||||||||||
831 | return nullptr; | ||||||||||
832 | |||||||||||
833 | Function *F = Intrinsic::getDeclaration(II->getModule(), Intrinsic::cttz, | ||||||||||
834 | II->getType()); | ||||||||||
835 | return CallInst::Create(F, {X, II->getArgOperand(1)}); | ||||||||||
836 | } | ||||||||||
837 | |||||||||||
838 | /// Attempt to fold a cttz/ctlz followed by a icmp plus select into a single | ||||||||||
839 | /// call to cttz/ctlz with flag 'is_zero_undef' cleared. | ||||||||||
840 | /// | ||||||||||
841 | /// For example, we can fold the following code sequence: | ||||||||||
842 | /// \code | ||||||||||
843 | /// %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 true) | ||||||||||
844 | /// %1 = icmp ne i32 %x, 0 | ||||||||||
845 | /// %2 = select i1 %1, i32 %0, i32 32 | ||||||||||
846 | /// \code | ||||||||||
847 | /// | ||||||||||
848 | /// into: | ||||||||||
849 | /// %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 false) | ||||||||||
850 | static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal, | ||||||||||
851 | InstCombiner::BuilderTy &Builder) { | ||||||||||
852 | ICmpInst::Predicate Pred = ICI->getPredicate(); | ||||||||||
853 | Value *CmpLHS = ICI->getOperand(0); | ||||||||||
854 | Value *CmpRHS = ICI->getOperand(1); | ||||||||||
855 | |||||||||||
856 | // Check if the condition value compares a value for equality against zero. | ||||||||||
857 | if (!ICI->isEquality() || !match(CmpRHS, m_Zero())) | ||||||||||
858 | return nullptr; | ||||||||||
859 | |||||||||||
860 | Value *SelectArg = FalseVal; | ||||||||||
861 | Value *ValueOnZero = TrueVal; | ||||||||||
862 | if (Pred == ICmpInst::ICMP_NE) | ||||||||||
863 | std::swap(SelectArg, ValueOnZero); | ||||||||||
864 | |||||||||||
865 | // Skip zero extend/truncate. | ||||||||||
866 | Value *Count = nullptr; | ||||||||||
867 | if (!match(SelectArg, m_ZExt(m_Value(Count))) && | ||||||||||
868 | !match(SelectArg, m_Trunc(m_Value(Count)))) | ||||||||||
869 | Count = SelectArg; | ||||||||||
870 | |||||||||||
871 | // Check that 'Count' is a call to intrinsic cttz/ctlz. Also check that the | ||||||||||
872 | // input to the cttz/ctlz is used as LHS for the compare instruction. | ||||||||||
873 | if (!match(Count, m_Intrinsic<Intrinsic::cttz>(m_Specific(CmpLHS))) && | ||||||||||
874 | !match(Count, m_Intrinsic<Intrinsic::ctlz>(m_Specific(CmpLHS)))) | ||||||||||
875 | return nullptr; | ||||||||||
876 | |||||||||||
877 | IntrinsicInst *II = cast<IntrinsicInst>(Count); | ||||||||||
878 | |||||||||||
879 | // Check if the value propagated on zero is a constant number equal to the | ||||||||||
880 | // sizeof in bits of 'Count'. | ||||||||||
881 | unsigned SizeOfInBits = Count->getType()->getScalarSizeInBits(); | ||||||||||
882 | if (match(ValueOnZero, m_SpecificInt(SizeOfInBits))) { | ||||||||||
883 | // Explicitly clear the 'undef_on_zero' flag. It's always valid to go from | ||||||||||
884 | // true to false on this flag, so we can replace it for all users. | ||||||||||
885 | II->setArgOperand(1, ConstantInt::getFalse(II->getContext())); | ||||||||||
886 | return SelectArg; | ||||||||||
887 | } | ||||||||||
888 | |||||||||||
889 | // If the ValueOnZero is not the bitwidth, we can at least make use of the | ||||||||||
890 | // fact that the cttz/ctlz result will not be used if the input is zero, so | ||||||||||
891 | // it's okay to relax it to undef for that case. | ||||||||||
892 | if (II->hasOneUse() && !match(II->getArgOperand(1), m_One())) | ||||||||||
893 | II->setArgOperand(1, ConstantInt::getTrue(II->getContext())); | ||||||||||
894 | |||||||||||
895 | return nullptr; | ||||||||||
896 | } | ||||||||||
897 | |||||||||||
898 | /// Return true if we find and adjust an icmp+select pattern where the compare | ||||||||||
899 | /// is with a constant that can be incremented or decremented to match the | ||||||||||
900 | /// minimum or maximum idiom. | ||||||||||
901 | static bool adjustMinMax(SelectInst &Sel, ICmpInst &Cmp) { | ||||||||||
902 | ICmpInst::Predicate Pred = Cmp.getPredicate(); | ||||||||||
903 | Value *CmpLHS = Cmp.getOperand(0); | ||||||||||
904 | Value *CmpRHS = Cmp.getOperand(1); | ||||||||||
905 | Value *TrueVal = Sel.getTrueValue(); | ||||||||||
906 | Value *FalseVal = Sel.getFalseValue(); | ||||||||||
907 | |||||||||||
908 | // We may move or edit the compare, so make sure the select is the only user. | ||||||||||
909 | const APInt *CmpC; | ||||||||||
910 | if (!Cmp.hasOneUse() || !match(CmpRHS, m_APInt(CmpC))) | ||||||||||
911 | return false; | ||||||||||
912 | |||||||||||
913 | // These transforms only work for selects of integers or vector selects of | ||||||||||
914 | // integer vectors. | ||||||||||
915 | Type *SelTy = Sel.getType(); | ||||||||||
916 | auto *SelEltTy = dyn_cast<IntegerType>(SelTy->getScalarType()); | ||||||||||
917 | if (!SelEltTy || SelTy->isVectorTy() != Cmp.getType()->isVectorTy()) | ||||||||||
918 | return false; | ||||||||||
919 | |||||||||||
920 | Constant *AdjustedRHS; | ||||||||||
921 | if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SGT) | ||||||||||
922 | AdjustedRHS = ConstantInt::get(CmpRHS->getType(), *CmpC + 1); | ||||||||||
923 | else if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SLT) | ||||||||||
924 | AdjustedRHS = ConstantInt::get(CmpRHS->getType(), *CmpC - 1); | ||||||||||
925 | else | ||||||||||
926 | return false; | ||||||||||
927 | |||||||||||
928 | // X > C ? X : C+1 --> X < C+1 ? C+1 : X | ||||||||||
929 | // X < C ? X : C-1 --> X > C-1 ? C-1 : X | ||||||||||
930 | if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) || | ||||||||||
931 | (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) { | ||||||||||
932 | ; // Nothing to do here. Values match without any sign/zero extension. | ||||||||||
933 | } | ||||||||||
934 | // Types do not match. Instead of calculating this with mixed types, promote | ||||||||||
935 | // all to the larger type. This enables scalar evolution to analyze this | ||||||||||
936 | // expression. | ||||||||||
937 | else if (CmpRHS->getType()->getScalarSizeInBits() < SelEltTy->getBitWidth()) { | ||||||||||
938 | Constant *SextRHS = ConstantExpr::getSExt(AdjustedRHS, SelTy); | ||||||||||
939 | |||||||||||
940 | // X = sext x; x >s c ? X : C+1 --> X = sext x; X <s C+1 ? C+1 : X | ||||||||||
941 | // X = sext x; x <s c ? X : C-1 --> X = sext x; X >s C-1 ? C-1 : X | ||||||||||
942 | // X = sext x; x >u c ? X : C+1 --> X = sext x; X <u C+1 ? C+1 : X | ||||||||||
943 | // X = sext x; x <u c ? X : C-1 --> X = sext x; X >u C-1 ? C-1 : X | ||||||||||
944 | if (match(TrueVal, m_SExt(m_Specific(CmpLHS))) && SextRHS == FalseVal) { | ||||||||||
945 | CmpLHS = TrueVal; | ||||||||||
946 | AdjustedRHS = SextRHS; | ||||||||||
947 | } else if (match(FalseVal, m_SExt(m_Specific(CmpLHS))) && | ||||||||||
948 | SextRHS == TrueVal) { | ||||||||||
949 | CmpLHS = FalseVal; | ||||||||||
950 | AdjustedRHS = SextRHS; | ||||||||||
951 | } else if (Cmp.isUnsigned()) { | ||||||||||
952 | Constant *ZextRHS = ConstantExpr::getZExt(AdjustedRHS, SelTy); | ||||||||||
953 | // X = zext x; x >u c ? X : C+1 --> X = zext x; X <u C+1 ? C+1 : X | ||||||||||
954 | // X = zext x; x <u c ? X : C-1 --> X = zext x; X >u C-1 ? C-1 : X | ||||||||||
955 | // zext + signed compare cannot be changed: | ||||||||||
956 | // 0xff <s 0x00, but 0x00ff >s 0x0000 | ||||||||||
957 | if (match(TrueVal, m_ZExt(m_Specific(CmpLHS))) && ZextRHS == FalseVal) { | ||||||||||
958 | CmpLHS = TrueVal; | ||||||||||
959 | AdjustedRHS = ZextRHS; | ||||||||||
960 | } else if (match(FalseVal, m_ZExt(m_Specific(CmpLHS))) && | ||||||||||
961 | ZextRHS == TrueVal) { | ||||||||||
962 | CmpLHS = FalseVal; | ||||||||||
963 | AdjustedRHS = ZextRHS; | ||||||||||
964 | } else { | ||||||||||
965 | return false; | ||||||||||
966 | } | ||||||||||
967 | } else { | ||||||||||
968 | return false; | ||||||||||
969 | } | ||||||||||
970 | } else { | ||||||||||
971 | return false; | ||||||||||
972 | } | ||||||||||
973 | |||||||||||
974 | Pred = ICmpInst::getSwappedPredicate(Pred); | ||||||||||
975 | CmpRHS = AdjustedRHS; | ||||||||||
976 | std::swap(FalseVal, TrueVal); | ||||||||||
977 | Cmp.setPredicate(Pred); | ||||||||||
978 | Cmp.setOperand(0, CmpLHS); | ||||||||||
979 | Cmp.setOperand(1, CmpRHS); | ||||||||||
980 | Sel.setOperand(1, TrueVal); | ||||||||||
981 | Sel.setOperand(2, FalseVal); | ||||||||||
982 | Sel.swapProfMetadata(); | ||||||||||
983 | |||||||||||
984 | // Move the compare instruction right before the select instruction. Otherwise | ||||||||||
985 | // the sext/zext value may be defined after the compare instruction uses it. | ||||||||||
986 | Cmp.moveBefore(&Sel); | ||||||||||
987 | |||||||||||
988 | return true; | ||||||||||
989 | } | ||||||||||
990 | |||||||||||
991 | /// If this is an integer min/max (icmp + select) with a constant operand, | ||||||||||
992 | /// create the canonical icmp for the min/max operation and canonicalize the | ||||||||||
993 | /// constant to the 'false' operand of the select: | ||||||||||
994 | /// select (icmp Pred X, C1), C2, X --> select (icmp Pred' X, C2), X, C2 | ||||||||||
995 | /// Note: if C1 != C2, this will change the icmp constant to the existing | ||||||||||
996 | /// constant operand of the select. | ||||||||||
997 | static Instruction * | ||||||||||
998 | canonicalizeMinMaxWithConstant(SelectInst &Sel, ICmpInst &Cmp, | ||||||||||
999 | InstCombiner &IC) { | ||||||||||
1000 | if (!Cmp.hasOneUse() || !isa<Constant>(Cmp.getOperand(1))) | ||||||||||
1001 | return nullptr; | ||||||||||
1002 | |||||||||||
1003 | // Canonicalize the compare predicate based on whether we have min or max. | ||||||||||
1004 | Value *LHS, *RHS; | ||||||||||
1005 | SelectPatternResult SPR = matchSelectPattern(&Sel, LHS, RHS); | ||||||||||
1006 | if (!SelectPatternResult::isMinOrMax(SPR.Flavor)) | ||||||||||
1007 | return nullptr; | ||||||||||
1008 | |||||||||||
1009 | // Is this already canonical? | ||||||||||
1010 | ICmpInst::Predicate CanonicalPred = getMinMaxPred(SPR.Flavor); | ||||||||||
1011 | if (Cmp.getOperand(0) == LHS && Cmp.getOperand(1) == RHS && | ||||||||||
1012 | Cmp.getPredicate() == CanonicalPred) | ||||||||||
1013 | return nullptr; | ||||||||||
1014 | |||||||||||
1015 | // Bail out on unsimplified X-0 operand (due to some worklist management bug), | ||||||||||
1016 | // as this may cause an infinite combine loop. Let the sub be folded first. | ||||||||||
1017 | if (match(LHS, m_Sub(m_Value(), m_Zero())) || | ||||||||||
1018 | match(RHS, m_Sub(m_Value(), m_Zero()))) | ||||||||||
1019 | return nullptr; | ||||||||||
1020 | |||||||||||
1021 | // Create the canonical compare and plug it into the select. | ||||||||||
1022 | IC.replaceOperand(Sel, 0, IC.Builder.CreateICmp(CanonicalPred, LHS, RHS)); | ||||||||||
1023 | |||||||||||
1024 | // If the select operands did not change, we're done. | ||||||||||
1025 | if (Sel.getTrueValue() == LHS && Sel.getFalseValue() == RHS) | ||||||||||
1026 | return &Sel; | ||||||||||
1027 | |||||||||||
1028 | // If we are swapping the select operands, swap the metadata too. | ||||||||||
1029 | assert(Sel.getTrueValue() == RHS && Sel.getFalseValue() == LHS &&((Sel.getTrueValue() == RHS && Sel.getFalseValue() == LHS && "Unexpected results from matchSelectPattern") ? static_cast<void> (0) : __assert_fail ("Sel.getTrueValue() == RHS && Sel.getFalseValue() == LHS && \"Unexpected results from matchSelectPattern\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp" , 1030, __PRETTY_FUNCTION__)) | ||||||||||
1030 | "Unexpected results from matchSelectPattern")((Sel.getTrueValue() == RHS && Sel.getFalseValue() == LHS && "Unexpected results from matchSelectPattern") ? static_cast<void> (0) : __assert_fail ("Sel.getTrueValue() == RHS && Sel.getFalseValue() == LHS && \"Unexpected results from matchSelectPattern\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp" , 1030, __PRETTY_FUNCTION__)); | ||||||||||
1031 | Sel.swapValues(); | ||||||||||
1032 | Sel.swapProfMetadata(); | ||||||||||
1033 | return &Sel; | ||||||||||
1034 | } | ||||||||||
1035 | |||||||||||
1036 | /// There are many select variants for each of ABS/NABS. | ||||||||||
1037 | /// In matchSelectPattern(), there are different compare constants, compare | ||||||||||
1038 | /// predicates/operands and select operands. | ||||||||||
1039 | /// In isKnownNegation(), there are different formats of negated operands. | ||||||||||
1040 | /// Canonicalize all these variants to 1 pattern. | ||||||||||
1041 | /// This makes CSE more likely. | ||||||||||
1042 | static Instruction *canonicalizeAbsNabs(SelectInst &Sel, ICmpInst &Cmp, | ||||||||||
1043 | InstCombiner::BuilderTy &Builder) { | ||||||||||
1044 | if (!Cmp.hasOneUse() || !isa<Constant>(Cmp.getOperand(1))) | ||||||||||
1045 | return nullptr; | ||||||||||
1046 | |||||||||||
1047 | // Choose a sign-bit check for the compare (likely simpler for codegen). | ||||||||||
1048 | // ABS: (X <s 0) ? -X : X | ||||||||||
1049 | // NABS: (X <s 0) ? X : -X | ||||||||||
1050 | Value *LHS, *RHS; | ||||||||||
1051 | SelectPatternFlavor SPF = matchSelectPattern(&Sel, LHS, RHS).Flavor; | ||||||||||
1052 | if (SPF != SelectPatternFlavor::SPF_ABS && | ||||||||||
1053 | SPF != SelectPatternFlavor::SPF_NABS) | ||||||||||
1054 | return nullptr; | ||||||||||
1055 | |||||||||||
1056 | Value *TVal = Sel.getTrueValue(); | ||||||||||
1057 | Value *FVal = Sel.getFalseValue(); | ||||||||||
1058 | assert(isKnownNegation(TVal, FVal) &&((isKnownNegation(TVal, FVal) && "Unexpected result from matchSelectPattern" ) ? static_cast<void> (0) : __assert_fail ("isKnownNegation(TVal, FVal) && \"Unexpected result from matchSelectPattern\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp" , 1059, __PRETTY_FUNCTION__)) | ||||||||||
1059 | "Unexpected result from matchSelectPattern")((isKnownNegation(TVal, FVal) && "Unexpected result from matchSelectPattern" ) ? static_cast<void> (0) : __assert_fail ("isKnownNegation(TVal, FVal) && \"Unexpected result from matchSelectPattern\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp" , 1059, __PRETTY_FUNCTION__)); | ||||||||||
1060 | |||||||||||
1061 | // The compare may use the negated abs()/nabs() operand, or it may use | ||||||||||
1062 | // negation in non-canonical form such as: sub A, B. | ||||||||||
1063 | bool CmpUsesNegatedOp = match(Cmp.getOperand(0), m_Neg(m_Specific(TVal))) || | ||||||||||
1064 | match(Cmp.getOperand(0), m_Neg(m_Specific(FVal))); | ||||||||||
1065 | |||||||||||
1066 | bool CmpCanonicalized = !CmpUsesNegatedOp && | ||||||||||
1067 | match(Cmp.getOperand(1), m_ZeroInt()) && | ||||||||||
1068 | Cmp.getPredicate() == ICmpInst::ICMP_SLT; | ||||||||||
1069 | bool RHSCanonicalized = match(RHS, m_Neg(m_Specific(LHS))); | ||||||||||
1070 | |||||||||||
1071 | // Is this already canonical? | ||||||||||
1072 | if (CmpCanonicalized && RHSCanonicalized) | ||||||||||
1073 | return nullptr; | ||||||||||
1074 | |||||||||||
1075 | // If RHS is not canonical but is used by other instructions, don't | ||||||||||
1076 | // canonicalize it and potentially increase the instruction count. | ||||||||||
1077 | if (!RHSCanonicalized) | ||||||||||
1078 | if (!(RHS->hasOneUse() || (RHS->hasNUses(2) && CmpUsesNegatedOp))) | ||||||||||
1079 | return nullptr; | ||||||||||
1080 | |||||||||||
1081 | // Create the canonical compare: icmp slt LHS 0. | ||||||||||
1082 | if (!CmpCanonicalized) { | ||||||||||
1083 | Cmp.setPredicate(ICmpInst::ICMP_SLT); | ||||||||||
1084 | Cmp.setOperand(1, ConstantInt::getNullValue(Cmp.getOperand(0)->getType())); | ||||||||||
1085 | if (CmpUsesNegatedOp) | ||||||||||
1086 | Cmp.setOperand(0, LHS); | ||||||||||
1087 | } | ||||||||||
1088 | |||||||||||
1089 | // Create the canonical RHS: RHS = sub (0, LHS). | ||||||||||
1090 | if (!RHSCanonicalized) { | ||||||||||
1091 | assert(RHS->hasOneUse() && "RHS use number is not right")((RHS->hasOneUse() && "RHS use number is not right" ) ? static_cast<void> (0) : __assert_fail ("RHS->hasOneUse() && \"RHS use number is not right\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp" , 1091, __PRETTY_FUNCTION__)); | ||||||||||
1092 | RHS = Builder.CreateNeg(LHS); | ||||||||||
1093 | if (TVal == LHS) { | ||||||||||
1094 | Sel.setFalseValue(RHS); | ||||||||||
1095 | FVal = RHS; | ||||||||||
1096 | } else { | ||||||||||
1097 | Sel.setTrueValue(RHS); | ||||||||||
1098 | TVal = RHS; | ||||||||||
1099 | } | ||||||||||
1100 | } | ||||||||||
1101 | |||||||||||
1102 | // If the select operands do not change, we're done. | ||||||||||
1103 | if (SPF == SelectPatternFlavor::SPF_NABS) { | ||||||||||
1104 | if (TVal == LHS) | ||||||||||
1105 | return &Sel; | ||||||||||
1106 | assert(FVal == LHS && "Unexpected results from matchSelectPattern")((FVal == LHS && "Unexpected results from matchSelectPattern" ) ? static_cast<void> (0) : __assert_fail ("FVal == LHS && \"Unexpected results from matchSelectPattern\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp" , 1106, __PRETTY_FUNCTION__)); | ||||||||||
1107 | } else { | ||||||||||
1108 | if (FVal == LHS) | ||||||||||
1109 | return &Sel; | ||||||||||
1110 | assert(TVal == LHS && "Unexpected results from matchSelectPattern")((TVal == LHS && "Unexpected results from matchSelectPattern" ) ? static_cast<void> (0) : __assert_fail ("TVal == LHS && \"Unexpected results from matchSelectPattern\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp" , 1110, __PRETTY_FUNCTION__)); | ||||||||||
1111 | } | ||||||||||
1112 | |||||||||||
1113 | // We are swapping the select operands, so swap the metadata too. | ||||||||||
1114 | Sel.swapValues(); | ||||||||||
1115 | Sel.swapProfMetadata(); | ||||||||||
1116 | return &Sel; | ||||||||||
1117 | } | ||||||||||
1118 | |||||||||||
1119 | static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *ReplaceOp, | ||||||||||
1120 | const SimplifyQuery &Q) { | ||||||||||
1121 | // If this is a binary operator, try to simplify it with the replaced op | ||||||||||
1122 | // because we know Op and ReplaceOp are equivalant. | ||||||||||
1123 | // For example: V = X + 1, Op = X, ReplaceOp = 42 | ||||||||||
1124 | // Simplifies as: add(42, 1) --> 43 | ||||||||||
1125 | if (auto *BO = dyn_cast<BinaryOperator>(V)) { | ||||||||||
1126 | if (BO->getOperand(0) == Op) | ||||||||||
1127 | return SimplifyBinOp(BO->getOpcode(), ReplaceOp, BO->getOperand(1), Q); | ||||||||||
1128 | if (BO->getOperand(1) == Op) | ||||||||||
1129 | return SimplifyBinOp(BO->getOpcode(), BO->getOperand(0), ReplaceOp, Q); | ||||||||||
1130 | } | ||||||||||
1131 | |||||||||||
1132 | return nullptr; | ||||||||||
1133 | } | ||||||||||
1134 | |||||||||||
1135 | /// If we have a select with an equality comparison, then we know the value in | ||||||||||
1136 | /// one of the arms of the select. See if substituting this value into an arm | ||||||||||
1137 | /// and simplifying the result yields the same value as the other arm. | ||||||||||
1138 | /// | ||||||||||
1139 | /// To make this transform safe, we must drop poison-generating flags | ||||||||||
1140 | /// (nsw, etc) if we simplified to a binop because the select may be guarding | ||||||||||
1141 | /// that poison from propagating. If the existing binop already had no | ||||||||||
1142 | /// poison-generating flags, then this transform can be done by instsimplify. | ||||||||||
1143 | /// | ||||||||||
1144 | /// Consider: | ||||||||||
1145 | /// %cmp = icmp eq i32 %x, 2147483647 | ||||||||||
1146 | /// %add = add nsw i32 %x, 1 | ||||||||||
1147 | /// %sel = select i1 %cmp, i32 -2147483648, i32 %add | ||||||||||
1148 | /// | ||||||||||
1149 | /// We can't replace %sel with %add unless we strip away the flags. | ||||||||||
1150 | /// TODO: Wrapping flags could be preserved in some cases with better analysis. | ||||||||||
1151 | static Value *foldSelectValueEquivalence(SelectInst &Sel, ICmpInst &Cmp, | ||||||||||
1152 | const SimplifyQuery &Q) { | ||||||||||
1153 | if (!Cmp.isEquality()) | ||||||||||
1154 | return nullptr; | ||||||||||
1155 | |||||||||||
1156 | // Canonicalize the pattern to ICMP_EQ by swapping the select operands. | ||||||||||
1157 | Value *TrueVal = Sel.getTrueValue(), *FalseVal = Sel.getFalseValue(); | ||||||||||
1158 | if (Cmp.getPredicate() == ICmpInst::ICMP_NE) | ||||||||||
1159 | std::swap(TrueVal, FalseVal); | ||||||||||
1160 | |||||||||||
1161 | // Try each equivalence substitution possibility. | ||||||||||
1162 | // We have an 'EQ' comparison, so the select's false value will propagate. | ||||||||||
1163 | // Example: | ||||||||||
1164 | // (X == 42) ? 43 : (X + 1) --> (X == 42) ? (X + 1) : (X + 1) --> X + 1 | ||||||||||
1165 | // (X == 42) ? (X + 1) : 43 --> (X == 42) ? (42 + 1) : 43 --> 43 | ||||||||||
1166 | Value *CmpLHS = Cmp.getOperand(0), *CmpRHS = Cmp.getOperand(1); | ||||||||||
1167 | if (simplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q) == TrueVal || | ||||||||||
1168 | simplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q) == TrueVal || | ||||||||||
1169 | simplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q) == FalseVal || | ||||||||||
1170 | simplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q) == FalseVal) { | ||||||||||
1171 | if (auto *FalseInst = dyn_cast<Instruction>(FalseVal)) | ||||||||||
1172 | FalseInst->dropPoisonGeneratingFlags(); | ||||||||||
1173 | return FalseVal; | ||||||||||
1174 | } | ||||||||||
1175 | return nullptr; | ||||||||||
1176 | } | ||||||||||
1177 | |||||||||||
1178 | // See if this is a pattern like: | ||||||||||
1179 | // %old_cmp1 = icmp slt i32 %x, C2 | ||||||||||
1180 | // %old_replacement = select i1 %old_cmp1, i32 %target_low, i32 %target_high | ||||||||||
1181 | // %old_x_offseted = add i32 %x, C1 | ||||||||||
1182 | // %old_cmp0 = icmp ult i32 %old_x_offseted, C0 | ||||||||||
1183 | // %r = select i1 %old_cmp0, i32 %x, i32 %old_replacement | ||||||||||
1184 | // This can be rewritten as more canonical pattern: | ||||||||||
1185 | // %new_cmp1 = icmp slt i32 %x, -C1 | ||||||||||
1186 | // %new_cmp2 = icmp sge i32 %x, C0-C1 | ||||||||||
1187 | // %new_clamped_low = select i1 %new_cmp1, i32 %target_low, i32 %x | ||||||||||
1188 | // %r = select i1 %new_cmp2, i32 %target_high, i32 %new_clamped_low | ||||||||||
1189 | // Iff -C1 s<= C2 s<= C0-C1 | ||||||||||
1190 | // Also ULT predicate can also be UGT iff C0 != -1 (+invert result) | ||||||||||
1191 | // SLT predicate can also be SGT iff C2 != INT_MAX (+invert res.) | ||||||||||
1192 | static Instruction *canonicalizeClampLike(SelectInst &Sel0, ICmpInst &Cmp0, | ||||||||||
1193 | InstCombiner::BuilderTy &Builder) { | ||||||||||
1194 | Value *X = Sel0.getTrueValue(); | ||||||||||
1195 | Value *Sel1 = Sel0.getFalseValue(); | ||||||||||
1196 | |||||||||||
1197 | // First match the condition of the outermost select. | ||||||||||
1198 | // Said condition must be one-use. | ||||||||||
1199 | if (!Cmp0.hasOneUse()) | ||||||||||
1200 | return nullptr; | ||||||||||
1201 | Value *Cmp00 = Cmp0.getOperand(0); | ||||||||||
1202 | Constant *C0; | ||||||||||
1203 | if (!match(Cmp0.getOperand(1), | ||||||||||
1204 | m_CombineAnd(m_AnyIntegralConstant(), m_Constant(C0)))) | ||||||||||
1205 | return nullptr; | ||||||||||
1206 | // Canonicalize Cmp0 into the form we expect. | ||||||||||
1207 | // FIXME: we shouldn't care about lanes that are 'undef' in the end? | ||||||||||
1208 | switch (Cmp0.getPredicate()) { | ||||||||||
1209 | case ICmpInst::Predicate::ICMP_ULT: | ||||||||||
1210 | break; // Great! | ||||||||||
1211 | case ICmpInst::Predicate::ICMP_ULE: | ||||||||||
1212 | // We'd have to increment C0 by one, and for that it must not have all-ones | ||||||||||
1213 | // element, but then it would have been canonicalized to 'ult' before | ||||||||||
1214 | // we get here. So we can't do anything useful with 'ule'. | ||||||||||
1215 | return nullptr; | ||||||||||
1216 | case ICmpInst::Predicate::ICMP_UGT: | ||||||||||
1217 | // We want to canonicalize it to 'ult', so we'll need to increment C0, | ||||||||||
1218 | // which again means it must not have any all-ones elements. | ||||||||||
1219 | if (!match(C0, | ||||||||||
1220 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, | ||||||||||
1221 | APInt::getAllOnesValue( | ||||||||||
1222 | C0->getType()->getScalarSizeInBits())))) | ||||||||||
1223 | return nullptr; // Can't do, have all-ones element[s]. | ||||||||||
1224 | C0 = AddOne(C0); | ||||||||||
1225 | std::swap(X, Sel1); | ||||||||||
1226 | break; | ||||||||||
1227 | case ICmpInst::Predicate::ICMP_UGE: | ||||||||||
1228 | // The only way we'd get this predicate if this `icmp` has extra uses, | ||||||||||
1229 | // but then we won't be able to do this fold. | ||||||||||
1230 | return nullptr; | ||||||||||
1231 | default: | ||||||||||
1232 | return nullptr; // Unknown predicate. | ||||||||||
1233 | } | ||||||||||
1234 | |||||||||||
1235 | // Now that we've canonicalized the ICmp, we know the X we expect; | ||||||||||
1236 | // the select in other hand should be one-use. | ||||||||||
1237 | if (!Sel1->hasOneUse()) | ||||||||||
1238 | return nullptr; | ||||||||||
1239 | |||||||||||
1240 | // We now can finish matching the condition of the outermost select: | ||||||||||
1241 | // it should either be the X itself, or an addition of some constant to X. | ||||||||||
1242 | Constant *C1; | ||||||||||
1243 | if (Cmp00 == X) | ||||||||||
1244 | C1 = ConstantInt::getNullValue(Sel0.getType()); | ||||||||||
1245 | else if (!match(Cmp00, | ||||||||||
1246 | m_Add(m_Specific(X), | ||||||||||
1247 | m_CombineAnd(m_AnyIntegralConstant(), m_Constant(C1))))) | ||||||||||
1248 | return nullptr; | ||||||||||
1249 | |||||||||||
1250 | Value *Cmp1; | ||||||||||
1251 | ICmpInst::Predicate Pred1; | ||||||||||
1252 | Constant *C2; | ||||||||||
1253 | Value *ReplacementLow, *ReplacementHigh; | ||||||||||
1254 | if (!match(Sel1, m_Select(m_Value(Cmp1), m_Value(ReplacementLow), | ||||||||||
1255 | m_Value(ReplacementHigh))) || | ||||||||||
1256 | !match(Cmp1, | ||||||||||
1257 | m_ICmp(Pred1, m_Specific(X), | ||||||||||
1258 | m_CombineAnd(m_AnyIntegralConstant(), m_Constant(C2))))) | ||||||||||
1259 | return nullptr; | ||||||||||
1260 | |||||||||||
1261 | if (!Cmp1->hasOneUse() && (Cmp00 == X || !Cmp00->hasOneUse())) | ||||||||||
1262 | return nullptr; // Not enough one-use instructions for the fold. | ||||||||||
1263 | // FIXME: this restriction could be relaxed if Cmp1 can be reused as one of | ||||||||||
1264 | // two comparisons we'll need to build. | ||||||||||
1265 | |||||||||||
1266 | // Canonicalize Cmp1 into the form we expect. | ||||||||||
1267 | // FIXME: we shouldn't care about lanes that are 'undef' in the end? | ||||||||||
1268 | switch (Pred1) { | ||||||||||
1269 | case ICmpInst::Predicate::ICMP_SLT: | ||||||||||
1270 | break; | ||||||||||
1271 | case ICmpInst::Predicate::ICMP_SLE: | ||||||||||
1272 | // We'd have to increment C2 by one, and for that it must not have signed | ||||||||||
1273 | // max element, but then it would have been canonicalized to 'slt' before | ||||||||||
1274 | // we get here. So we can't do anything useful with 'sle'. | ||||||||||
1275 | return nullptr; | ||||||||||
1276 | case ICmpInst::Predicate::ICMP_SGT: | ||||||||||
1277 | // We want to canonicalize it to 'slt', so we'll need to increment C2, | ||||||||||
1278 | // which again means it must not have any signed max elements. | ||||||||||
1279 | if (!match(C2, | ||||||||||
1280 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, | ||||||||||
1281 | APInt::getSignedMaxValue( | ||||||||||
1282 | C2->getType()->getScalarSizeInBits())))) | ||||||||||
1283 | return nullptr; // Can't do, have signed max element[s]. | ||||||||||
1284 | C2 = AddOne(C2); | ||||||||||
1285 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | ||||||||||
1286 | case ICmpInst::Predicate::ICMP_SGE: | ||||||||||
1287 | // Also non-canonical, but here we don't need to change C2, | ||||||||||
1288 | // so we don't have any restrictions on C2, so we can just handle it. | ||||||||||
1289 | std::swap(ReplacementLow, ReplacementHigh); | ||||||||||
1290 | break; | ||||||||||
1291 | default: | ||||||||||
1292 | return nullptr; // Unknown predicate. | ||||||||||
1293 | } | ||||||||||
1294 | |||||||||||
1295 | // The thresholds of this clamp-like pattern. | ||||||||||
1296 | auto *ThresholdLowIncl = ConstantExpr::getNeg(C1); | ||||||||||
1297 | auto *ThresholdHighExcl = ConstantExpr::getSub(C0, C1); | ||||||||||
1298 | |||||||||||
1299 | // The fold has a precondition 1: C2 s>= ThresholdLow | ||||||||||
1300 | auto *Precond1 = ConstantExpr::getICmp(ICmpInst::Predicate::ICMP_SGE, C2, | ||||||||||
1301 | ThresholdLowIncl); | ||||||||||
1302 | if (!match(Precond1, m_One())) | ||||||||||
1303 | return nullptr; | ||||||||||
1304 | // The fold has a precondition 2: C2 s<= ThresholdHigh | ||||||||||
1305 | auto *Precond2 = ConstantExpr::getICmp(ICmpInst::Predicate::ICMP_SLE, C2, | ||||||||||
1306 | ThresholdHighExcl); | ||||||||||
1307 | if (!match(Precond2, m_One())) | ||||||||||
1308 | return nullptr; | ||||||||||
1309 | |||||||||||
1310 | // All good, finally emit the new pattern. | ||||||||||
1311 | Value *ShouldReplaceLow = Builder.CreateICmpSLT(X, ThresholdLowIncl); | ||||||||||
1312 | Value *ShouldReplaceHigh = Builder.CreateICmpSGE(X, ThresholdHighExcl); | ||||||||||
1313 | Value *MaybeReplacedLow = | ||||||||||
1314 | Builder.CreateSelect(ShouldReplaceLow, ReplacementLow, X); | ||||||||||
1315 | Instruction *MaybeReplacedHigh = | ||||||||||
1316 | SelectInst::Create(ShouldReplaceHigh, ReplacementHigh, MaybeReplacedLow); | ||||||||||
1317 | |||||||||||
1318 | return MaybeReplacedHigh; | ||||||||||
1319 | } | ||||||||||
1320 | |||||||||||
1321 | // If we have | ||||||||||
1322 | // %cmp = icmp [canonical predicate] i32 %x, C0 | ||||||||||
1323 | // %r = select i1 %cmp, i32 %y, i32 C1 | ||||||||||
1324 | // Where C0 != C1 and %x may be different from %y, see if the constant that we | ||||||||||
1325 | // will have if we flip the strictness of the predicate (i.e. without changing | ||||||||||
1326 | // the result) is identical to the C1 in select. If it matches we can change | ||||||||||
1327 | // original comparison to one with swapped predicate, reuse the constant, | ||||||||||
1328 | // and swap the hands of select. | ||||||||||
1329 | static Instruction * | ||||||||||
1330 | tryToReuseConstantFromSelectInComparison(SelectInst &Sel, ICmpInst &Cmp, | ||||||||||
1331 | InstCombiner &IC) { | ||||||||||
1332 | ICmpInst::Predicate Pred; | ||||||||||
1333 | Value *X; | ||||||||||
1334 | Constant *C0; | ||||||||||
1335 | if (!match(&Cmp, m_OneUse(m_ICmp( | ||||||||||
1336 | Pred, m_Value(X), | ||||||||||
1337 | m_CombineAnd(m_AnyIntegralConstant(), m_Constant(C0)))))) | ||||||||||
1338 | return nullptr; | ||||||||||
1339 | |||||||||||
1340 | // If comparison predicate is non-relational, we won't be able to do anything. | ||||||||||
1341 | if (ICmpInst::isEquality(Pred)) | ||||||||||
1342 | return nullptr; | ||||||||||
1343 | |||||||||||
1344 | // If comparison predicate is non-canonical, then we certainly won't be able | ||||||||||
1345 | // to make it canonical; canonicalizeCmpWithConstant() already tried. | ||||||||||
1346 | if (!isCanonicalPredicate(Pred)) | ||||||||||
1347 | return nullptr; | ||||||||||
1348 | |||||||||||
1349 | // If the [input] type of comparison and select type are different, lets abort | ||||||||||
1350 | // for now. We could try to compare constants with trunc/[zs]ext though. | ||||||||||
1351 | if (C0->getType() != Sel.getType()) | ||||||||||
1352 | return nullptr; | ||||||||||
1353 | |||||||||||
1354 | // FIXME: are there any magic icmp predicate+constant pairs we must not touch? | ||||||||||
1355 | |||||||||||
1356 | Value *SelVal0, *SelVal1; // We do not care which one is from where. | ||||||||||
1357 | match(&Sel, m_Select(m_Value(), m_Value(SelVal0), m_Value(SelVal1))); | ||||||||||
1358 | // At least one of these values we are selecting between must be a constant | ||||||||||
1359 | // else we'll never succeed. | ||||||||||
1360 | if (!match(SelVal0, m_AnyIntegralConstant()) && | ||||||||||
1361 | !match(SelVal1, m_AnyIntegralConstant())) | ||||||||||
1362 | return nullptr; | ||||||||||
1363 | |||||||||||
1364 | // Does this constant C match any of the `select` values? | ||||||||||
1365 | auto MatchesSelectValue = [SelVal0, SelVal1](Constant *C) { | ||||||||||
1366 | return C->isElementWiseEqual(SelVal0) || C->isElementWiseEqual(SelVal1); | ||||||||||
1367 | }; | ||||||||||
1368 | |||||||||||
1369 | // If C0 *already* matches true/false value of select, we are done. | ||||||||||
1370 | if (MatchesSelectValue(C0)) | ||||||||||
1371 | return nullptr; | ||||||||||
1372 | |||||||||||
1373 | // Check the constant we'd have with flipped-strictness predicate. | ||||||||||
1374 | auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(Pred, C0); | ||||||||||
1375 | if (!FlippedStrictness) | ||||||||||
1376 | return nullptr; | ||||||||||
1377 | |||||||||||
1378 | // If said constant doesn't match either, then there is no hope, | ||||||||||
1379 | if (!MatchesSelectValue(FlippedStrictness->second)) | ||||||||||
1380 | return nullptr; | ||||||||||
1381 | |||||||||||
1382 | // It matched! Lets insert the new comparison just before select. | ||||||||||
1383 | InstCombiner::BuilderTy::InsertPointGuard Guard(IC.Builder); | ||||||||||
1384 | IC.Builder.SetInsertPoint(&Sel); | ||||||||||
1385 | |||||||||||
1386 | Pred = ICmpInst::getSwappedPredicate(Pred); // Yes, swapped. | ||||||||||
1387 | Value *NewCmp = IC.Builder.CreateICmp(Pred, X, FlippedStrictness->second, | ||||||||||
1388 | Cmp.getName() + ".inv"); | ||||||||||
1389 | IC.replaceOperand(Sel, 0, NewCmp); | ||||||||||
1390 | Sel.swapValues(); | ||||||||||
1391 | Sel.swapProfMetadata(); | ||||||||||
1392 | |||||||||||
1393 | return &Sel; | ||||||||||
1394 | } | ||||||||||
1395 | |||||||||||
1396 | /// Visit a SelectInst that has an ICmpInst as its first operand. | ||||||||||
1397 | Instruction *InstCombiner::foldSelectInstWithICmp(SelectInst &SI, | ||||||||||
1398 | ICmpInst *ICI) { | ||||||||||
1399 | if (Value *V = foldSelectValueEquivalence(SI, *ICI, SQ)) | ||||||||||
1400 | return replaceInstUsesWith(SI, V); | ||||||||||
1401 | |||||||||||
1402 | if (Instruction *NewSel = canonicalizeMinMaxWithConstant(SI, *ICI, *this)) | ||||||||||
1403 | return NewSel; | ||||||||||
1404 | |||||||||||
1405 | if (Instruction *NewAbs = canonicalizeAbsNabs(SI, *ICI, Builder)) | ||||||||||
1406 | return NewAbs; | ||||||||||
1407 | |||||||||||
1408 | if (Instruction *NewAbs = canonicalizeClampLike(SI, *ICI, Builder)) | ||||||||||
1409 | return NewAbs; | ||||||||||
1410 | |||||||||||
1411 | if (Instruction *NewSel = | ||||||||||
1412 | tryToReuseConstantFromSelectInComparison(SI, *ICI, *this)) | ||||||||||
1413 | return NewSel; | ||||||||||
1414 | |||||||||||
1415 | bool Changed = adjustMinMax(SI, *ICI); | ||||||||||
1416 | |||||||||||
1417 | if (Value *V = foldSelectICmpAnd(SI, ICI, Builder)) | ||||||||||
1418 | return replaceInstUsesWith(SI, V); | ||||||||||
1419 | |||||||||||
1420 | // NOTE: if we wanted to, this is where to detect integer MIN/MAX | ||||||||||
1421 | Value *TrueVal = SI.getTrueValue(); | ||||||||||
1422 | Value *FalseVal = SI.getFalseValue(); | ||||||||||
1423 | ICmpInst::Predicate Pred = ICI->getPredicate(); | ||||||||||
1424 | Value *CmpLHS = ICI->getOperand(0); | ||||||||||
1425 | Value *CmpRHS = ICI->getOperand(1); | ||||||||||
1426 | if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS)) { | ||||||||||
1427 | if (CmpLHS == TrueVal && Pred == ICmpInst::ICMP_EQ) { | ||||||||||
1428 | // Transform (X == C) ? X : Y -> (X == C) ? C : Y | ||||||||||
1429 | SI.setOperand(1, CmpRHS); | ||||||||||
1430 | Changed = true; | ||||||||||
1431 | } else if (CmpLHS == FalseVal && Pred == ICmpInst::ICMP_NE) { | ||||||||||
1432 | // Transform (X != C) ? Y : X -> (X != C) ? Y : C | ||||||||||
1433 | SI.setOperand(2, CmpRHS); | ||||||||||
1434 | Changed = true; | ||||||||||
1435 | } | ||||||||||
1436 | } | ||||||||||
1437 | |||||||||||
1438 | // FIXME: This code is nearly duplicated in InstSimplify. Using/refactoring | ||||||||||
1439 | // decomposeBitTestICmp() might help. | ||||||||||
1440 | { | ||||||||||
1441 | unsigned BitWidth = | ||||||||||
1442 | DL.getTypeSizeInBits(TrueVal->getType()->getScalarType()); | ||||||||||
1443 | APInt MinSignedValue = APInt::getSignedMinValue(BitWidth); | ||||||||||
1444 | Value *X; | ||||||||||
1445 | const APInt *Y, *C; | ||||||||||
1446 | bool TrueWhenUnset; | ||||||||||
1447 | bool IsBitTest = false; | ||||||||||
1448 | if (ICmpInst::isEquality(Pred) && | ||||||||||
1449 | match(CmpLHS, m_And(m_Value(X), m_Power2(Y))) && | ||||||||||
1450 | match(CmpRHS, m_Zero())) { | ||||||||||
1451 | IsBitTest = true; | ||||||||||
1452 | TrueWhenUnset = Pred == ICmpInst::ICMP_EQ; | ||||||||||
1453 | } else if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, m_Zero())) { | ||||||||||
1454 | X = CmpLHS; | ||||||||||
1455 | Y = &MinSignedValue; | ||||||||||
1456 | IsBitTest = true; | ||||||||||
1457 | TrueWhenUnset = false; | ||||||||||
1458 | } else if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes())) { | ||||||||||
1459 | X = CmpLHS; | ||||||||||
1460 | Y = &MinSignedValue; | ||||||||||
1461 | IsBitTest = true; | ||||||||||
1462 | TrueWhenUnset = true; | ||||||||||
1463 | } | ||||||||||
1464 | if (IsBitTest) { | ||||||||||
1465 | Value *V = nullptr; | ||||||||||
1466 | // (X & Y) == 0 ? X : X ^ Y --> X & ~Y | ||||||||||
1467 | if (TrueWhenUnset && TrueVal == X && | ||||||||||
1468 | match(FalseVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C) | ||||||||||
1469 | V = Builder.CreateAnd(X, ~(*Y)); | ||||||||||
1470 | // (X & Y) != 0 ? X ^ Y : X --> X & ~Y | ||||||||||
1471 | else if (!TrueWhenUnset && FalseVal == X && | ||||||||||
1472 | match(TrueVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C) | ||||||||||
1473 | V = Builder.CreateAnd(X, ~(*Y)); | ||||||||||
1474 | // (X & Y) == 0 ? X ^ Y : X --> X | Y | ||||||||||
1475 | else if (TrueWhenUnset && FalseVal == X && | ||||||||||
1476 | match(TrueVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C) | ||||||||||
1477 | V = Builder.CreateOr(X, *Y); | ||||||||||
1478 | // (X & Y) != 0 ? X : X ^ Y --> X | Y | ||||||||||
1479 | else if (!TrueWhenUnset && TrueVal == X && | ||||||||||
1480 | match(FalseVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C) | ||||||||||
1481 | V = Builder.CreateOr(X, *Y); | ||||||||||
1482 | |||||||||||
1483 | if (V) | ||||||||||
1484 | return replaceInstUsesWith(SI, V); | ||||||||||
1485 | } | ||||||||||
1486 | } | ||||||||||
1487 | |||||||||||
1488 | if (Instruction *V = | ||||||||||
1489 | foldSelectICmpAndAnd(SI.getType(), ICI, TrueVal, FalseVal, Builder)) | ||||||||||
1490 | return V; | ||||||||||
1491 | |||||||||||
1492 | if (Instruction *V = foldSelectCtlzToCttz(ICI, TrueVal, FalseVal, Builder)) | ||||||||||
1493 | return V; | ||||||||||
1494 | |||||||||||
1495 | if (Value *V = foldSelectICmpAndOr(ICI, TrueVal, FalseVal, Builder)) | ||||||||||
1496 | return replaceInstUsesWith(SI, V); | ||||||||||
1497 | |||||||||||
1498 | if (Value *V = foldSelectICmpLshrAshr(ICI, TrueVal, FalseVal, Builder)) | ||||||||||
1499 | return replaceInstUsesWith(SI, V); | ||||||||||
1500 | |||||||||||
1501 | if (Value *V = foldSelectCttzCtlz(ICI, TrueVal, FalseVal, Builder)) | ||||||||||
1502 | return replaceInstUsesWith(SI, V); | ||||||||||
1503 | |||||||||||
1504 | if (Value *V = canonicalizeSaturatedSubtract(ICI, TrueVal, FalseVal, Builder)) | ||||||||||
1505 | return replaceInstUsesWith(SI, V); | ||||||||||
1506 | |||||||||||
1507 | if (Value *V = canonicalizeSaturatedAdd(ICI, TrueVal, FalseVal, Builder)) | ||||||||||
1508 | return replaceInstUsesWith(SI, V); | ||||||||||
1509 | |||||||||||
1510 | return Changed ? &SI : nullptr; | ||||||||||
1511 | } | ||||||||||
1512 | |||||||||||
1513 | /// SI is a select whose condition is a PHI node (but the two may be in | ||||||||||
1514 | /// different blocks). See if the true/false values (V) are live in all of the | ||||||||||
1515 | /// predecessor blocks of the PHI. For example, cases like this can't be mapped: | ||||||||||
1516 | /// | ||||||||||
1517 | /// X = phi [ C1, BB1], [C2, BB2] | ||||||||||
1518 | /// Y = add | ||||||||||
1519 | /// Z = select X, Y, 0 | ||||||||||
1520 | /// | ||||||||||
1521 | /// because Y is not live in BB1/BB2. | ||||||||||
1522 | static bool canSelectOperandBeMappingIntoPredBlock(const Value *V, | ||||||||||
1523 | const SelectInst &SI) { | ||||||||||
1524 | // If the value is a non-instruction value like a constant or argument, it | ||||||||||
1525 | // can always be mapped. | ||||||||||
1526 | const Instruction *I = dyn_cast<Instruction>(V); | ||||||||||
1527 | if (!I) return true; | ||||||||||
1528 | |||||||||||
1529 | // If V is a PHI node defined in the same block as the condition PHI, we can | ||||||||||
1530 | // map the arguments. | ||||||||||
1531 | const PHINode *CondPHI = cast<PHINode>(SI.getCondition()); | ||||||||||
1532 | |||||||||||
1533 | if (const PHINode *VP = dyn_cast<PHINode>(I)) | ||||||||||
1534 | if (VP->getParent() == CondPHI->getParent()) | ||||||||||
1535 | return true; | ||||||||||
1536 | |||||||||||
1537 | // Otherwise, if the PHI and select are defined in the same block and if V is | ||||||||||
1538 | // defined in a different block, then we can transform it. | ||||||||||
1539 | if (SI.getParent() == CondPHI->getParent() && | ||||||||||
1540 | I->getParent() != CondPHI->getParent()) | ||||||||||
1541 | return true; | ||||||||||
1542 | |||||||||||
1543 | // Otherwise we have a 'hard' case and we can't tell without doing more | ||||||||||
1544 | // detailed dominator based analysis, punt. | ||||||||||
1545 | return false; | ||||||||||
1546 | } | ||||||||||
1547 | |||||||||||
1548 | /// We have an SPF (e.g. a min or max) of an SPF of the form: | ||||||||||
1549 | /// SPF2(SPF1(A, B), C) | ||||||||||
1550 | Instruction *InstCombiner::foldSPFofSPF(Instruction *Inner, | ||||||||||
1551 | SelectPatternFlavor SPF1, | ||||||||||
1552 | Value *A, Value *B, | ||||||||||
1553 | Instruction &Outer, | ||||||||||
1554 | SelectPatternFlavor SPF2, Value *C) { | ||||||||||
1555 | if (Outer.getType() != Inner->getType()) | ||||||||||
1556 | return nullptr; | ||||||||||
1557 | |||||||||||
1558 | if (C == A || C == B) { | ||||||||||
1559 | // MAX(MAX(A, B), B) -> MAX(A, B) | ||||||||||
1560 | // MIN(MIN(a, b), a) -> MIN(a, b) | ||||||||||
1561 | // TODO: This could be done in instsimplify. | ||||||||||
1562 | if (SPF1 == SPF2 && SelectPatternResult::isMinOrMax(SPF1)) | ||||||||||
1563 | return replaceInstUsesWith(Outer, Inner); | ||||||||||
1564 | |||||||||||
1565 | // MAX(MIN(a, b), a) -> a | ||||||||||
1566 | // MIN(MAX(a, b), a) -> a | ||||||||||
1567 | // TODO: This could be done in instsimplify. | ||||||||||
1568 | if ((SPF1 == SPF_SMIN && SPF2 == SPF_SMAX) || | ||||||||||
1569 | (SPF1 == SPF_SMAX && SPF2 == SPF_SMIN) || | ||||||||||
1570 | (SPF1 == SPF_UMIN && SPF2 == SPF_UMAX) || | ||||||||||
1571 | (SPF1 == SPF_UMAX && SPF2 == SPF_UMIN)) | ||||||||||
1572 | return replaceInstUsesWith(Outer, C); | ||||||||||
1573 | } | ||||||||||
1574 | |||||||||||
1575 | if (SPF1 == SPF2) { | ||||||||||
1576 | const APInt *CB, *CC; | ||||||||||
1577 | if (match(B, m_APInt(CB)) && match(C, m_APInt(CC))) { | ||||||||||
1578 | // MIN(MIN(A, 23), 97) -> MIN(A, 23) | ||||||||||
1579 | // MAX(MAX(A, 97), 23) -> MAX(A, 97) | ||||||||||
1580 | // TODO: This could be done in instsimplify. | ||||||||||
1581 | if ((SPF1 == SPF_UMIN && CB->ule(*CC)) || | ||||||||||
1582 | (SPF1 == SPF_SMIN && CB->sle(*CC)) || | ||||||||||
1583 | (SPF1 == SPF_UMAX && CB->uge(*CC)) || | ||||||||||
1584 | (SPF1 == SPF_SMAX && CB->sge(*CC))) | ||||||||||
1585 | return replaceInstUsesWith(Outer, Inner); | ||||||||||
1586 | |||||||||||
1587 | // MIN(MIN(A, 97), 23) -> MIN(A, 23) | ||||||||||
1588 | // MAX(MAX(A, 23), 97) -> MAX(A, 97) | ||||||||||
1589 | if ((SPF1 == SPF_UMIN && CB->ugt(*CC)) || | ||||||||||
1590 | (SPF1 == SPF_SMIN && CB->sgt(*CC)) || | ||||||||||
1591 | (SPF1 == SPF_UMAX && CB->ult(*CC)) || | ||||||||||
1592 | (SPF1 == SPF_SMAX && CB->slt(*CC))) { | ||||||||||
1593 | Outer.replaceUsesOfWith(Inner, A); | ||||||||||
1594 | return &Outer; | ||||||||||
1595 | } | ||||||||||
1596 | } | ||||||||||
1597 | } | ||||||||||
1598 | |||||||||||
1599 | // max(max(A, B), min(A, B)) --> max(A, B) | ||||||||||
1600 | // min(min(A, B), max(A, B)) --> min(A, B) | ||||||||||
1601 | // TODO: This could be done in instsimplify. | ||||||||||
1602 | if (SPF1 == SPF2 && | ||||||||||
1603 | ((SPF1 == SPF_UMIN && match(C, m_c_UMax(m_Specific(A), m_Specific(B)))) || | ||||||||||
1604 | (SPF1 == SPF_SMIN && match(C, m_c_SMax(m_Specific(A), m_Specific(B)))) || | ||||||||||
1605 | (SPF1 == SPF_UMAX && match(C, m_c_UMin(m_Specific(A), m_Specific(B)))) || | ||||||||||
1606 | (SPF1 == SPF_SMAX && match(C, m_c_SMin(m_Specific(A), m_Specific(B)))))) | ||||||||||
1607 | return replaceInstUsesWith(Outer, Inner); | ||||||||||
1608 | |||||||||||
1609 | // ABS(ABS(X)) -> ABS(X) | ||||||||||
1610 | // NABS(NABS(X)) -> NABS(X) | ||||||||||
1611 | // TODO: This could be done in instsimplify. | ||||||||||
1612 | if (SPF1 == SPF2 && (SPF1 == SPF_ABS || SPF1 == SPF_NABS)) { | ||||||||||
1613 | return replaceInstUsesWith(Outer, Inner); | ||||||||||
1614 | } | ||||||||||
1615 | |||||||||||
1616 | // ABS(NABS(X)) -> ABS(X) | ||||||||||
1617 | // NABS(ABS(X)) -> NABS(X) | ||||||||||
1618 | if ((SPF1 == SPF_ABS && SPF2 == SPF_NABS) || | ||||||||||
1619 | (SPF1 == SPF_NABS && SPF2 == SPF_ABS)) { | ||||||||||
1620 | SelectInst *SI = cast<SelectInst>(Inner); | ||||||||||
1621 | Value *NewSI = | ||||||||||
1622 | Builder.CreateSelect(SI->getCondition(), SI->getFalseValue(), | ||||||||||
1623 | SI->getTrueValue(), SI->getName(), SI); | ||||||||||
1624 | return replaceInstUsesWith(Outer, NewSI); | ||||||||||
1625 | } | ||||||||||
1626 | |||||||||||
1627 | auto IsFreeOrProfitableToInvert = | ||||||||||
1628 | [&](Value *V, Value *&NotV, bool &ElidesXor) { | ||||||||||
1629 | if (match(V, m_Not(m_Value(NotV)))) { | ||||||||||
1630 | // If V has at most 2 uses then we can get rid of the xor operation | ||||||||||
1631 | // entirely. | ||||||||||
1632 | ElidesXor |= !V->hasNUsesOrMore(3); | ||||||||||
1633 | return true; | ||||||||||
1634 | } | ||||||||||
1635 | |||||||||||
1636 | if (isFreeToInvert(V, !V->hasNUsesOrMore(3))) { | ||||||||||
1637 | NotV = nullptr; | ||||||||||
1638 | return true; | ||||||||||
1639 | } | ||||||||||
1640 | |||||||||||
1641 | return false; | ||||||||||
1642 | }; | ||||||||||
1643 | |||||||||||
1644 | Value *NotA, *NotB, *NotC; | ||||||||||
1645 | bool ElidesXor = false; | ||||||||||
1646 | |||||||||||
1647 | // MIN(MIN(~A, ~B), ~C) == ~MAX(MAX(A, B), C) | ||||||||||
1648 | // MIN(MAX(~A, ~B), ~C) == ~MAX(MIN(A, B), C) | ||||||||||
1649 | // MAX(MIN(~A, ~B), ~C) == ~MIN(MAX(A, B), C) | ||||||||||
1650 | // MAX(MAX(~A, ~B), ~C) == ~MIN(MIN(A, B), C) | ||||||||||
1651 | // | ||||||||||
1652 | // This transform is performance neutral if we can elide at least one xor from | ||||||||||
1653 | // the set of three operands, since we'll be tacking on an xor at the very | ||||||||||
1654 | // end. | ||||||||||
1655 | if (SelectPatternResult::isMinOrMax(SPF1) && | ||||||||||
1656 | SelectPatternResult::isMinOrMax(SPF2) && | ||||||||||
1657 | IsFreeOrProfitableToInvert(A, NotA, ElidesXor) && | ||||||||||
1658 | IsFreeOrProfitableToInvert(B, NotB, ElidesXor) && | ||||||||||
1659 | IsFreeOrProfitableToInvert(C, NotC, ElidesXor) && ElidesXor) { | ||||||||||
1660 | if (!NotA) | ||||||||||
1661 | NotA = Builder.CreateNot(A); | ||||||||||
1662 | if (!NotB) | ||||||||||
1663 | NotB = Builder.CreateNot(B); | ||||||||||
1664 | if (!NotC) | ||||||||||
1665 | NotC = Builder.CreateNot(C); | ||||||||||
1666 | |||||||||||
1667 | Value *NewInner = createMinMax(Builder, getInverseMinMaxFlavor(SPF1), NotA, | ||||||||||
1668 | NotB); | ||||||||||
1669 | Value *NewOuter = Builder.CreateNot( | ||||||||||
1670 | createMinMax(Builder, getInverseMinMaxFlavor(SPF2), NewInner, NotC)); | ||||||||||
1671 | return replaceInstUsesWith(Outer, NewOuter); | ||||||||||
1672 | } | ||||||||||
1673 | |||||||||||
1674 | return nullptr; | ||||||||||
1675 | } | ||||||||||
1676 | |||||||||||
1677 | /// Turn select C, (X + Y), (X - Y) --> (X + (select C, Y, (-Y))). | ||||||||||
1678 | /// This is even legal for FP. | ||||||||||
1679 | static Instruction *foldAddSubSelect(SelectInst &SI, | ||||||||||
1680 | InstCombiner::BuilderTy &Builder) { | ||||||||||
1681 | Value *CondVal = SI.getCondition(); | ||||||||||
1682 | Value *TrueVal = SI.getTrueValue(); | ||||||||||
1683 | Value *FalseVal = SI.getFalseValue(); | ||||||||||
1684 | auto *TI = dyn_cast<Instruction>(TrueVal); | ||||||||||
1685 | auto *FI = dyn_cast<Instruction>(FalseVal); | ||||||||||
1686 | if (!TI || !FI || !TI->hasOneUse() || !FI->hasOneUse()) | ||||||||||
1687 | return nullptr; | ||||||||||
1688 | |||||||||||
1689 | Instruction *AddOp = nullptr, *SubOp = nullptr; | ||||||||||
1690 | if ((TI->getOpcode() == Instruction::Sub && | ||||||||||
1691 | FI->getOpcode() == Instruction::Add) || | ||||||||||
1692 | (TI->getOpcode() == Instruction::FSub && | ||||||||||
1693 | FI->getOpcode() == Instruction::FAdd)) { | ||||||||||
1694 | AddOp = FI; | ||||||||||
1695 | SubOp = TI; | ||||||||||
1696 | } else if ((FI->getOpcode() == Instruction::Sub && | ||||||||||
1697 | TI->getOpcode() == Instruction::Add) || | ||||||||||
1698 | (FI->getOpcode() == Instruction::FSub && | ||||||||||
1699 | TI->getOpcode() == Instruction::FAdd)) { | ||||||||||
1700 | AddOp = TI; | ||||||||||
1701 | SubOp = FI; | ||||||||||
1702 | } | ||||||||||
1703 | |||||||||||
1704 | if (AddOp) { | ||||||||||
1705 | Value *OtherAddOp = nullptr; | ||||||||||
1706 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { | ||||||||||
1707 | OtherAddOp = AddOp->getOperand(1); | ||||||||||
1708 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { | ||||||||||
1709 | OtherAddOp = AddOp->getOperand(0); | ||||||||||
1710 | } | ||||||||||
1711 | |||||||||||
1712 | if (OtherAddOp) { | ||||||||||
1713 | // So at this point we know we have (Y -> OtherAddOp): | ||||||||||
1714 | // select C, (add X, Y), (sub X, Z) | ||||||||||
1715 | Value *NegVal; // Compute -Z | ||||||||||
1716 | if (SI.getType()->isFPOrFPVectorTy()) { | ||||||||||
1717 | NegVal = Builder.CreateFNeg(SubOp->getOperand(1)); | ||||||||||
1718 | if (Instruction *NegInst = dyn_cast<Instruction>(NegVal)) { | ||||||||||
1719 | FastMathFlags Flags = AddOp->getFastMathFlags(); | ||||||||||
1720 | Flags &= SubOp->getFastMathFlags(); | ||||||||||
1721 | NegInst->setFastMathFlags(Flags); | ||||||||||
1722 | } | ||||||||||
1723 | } else { | ||||||||||
1724 | NegVal = Builder.CreateNeg(SubOp->getOperand(1)); | ||||||||||
1725 | } | ||||||||||
1726 | |||||||||||
1727 | Value *NewTrueOp = OtherAddOp; | ||||||||||
1728 | Value *NewFalseOp = NegVal; | ||||||||||
1729 | if (AddOp != TI) | ||||||||||
1730 | std::swap(NewTrueOp, NewFalseOp); | ||||||||||
1731 | Value *NewSel = Builder.CreateSelect(CondVal, NewTrueOp, NewFalseOp, | ||||||||||
1732 | SI.getName() + ".p", &SI); | ||||||||||
1733 | |||||||||||
1734 | if (SI.getType()->isFPOrFPVectorTy()) { | ||||||||||
1735 | Instruction *RI = | ||||||||||
1736 | BinaryOperator::CreateFAdd(SubOp->getOperand(0), NewSel); | ||||||||||
1737 | |||||||||||
1738 | FastMathFlags Flags = AddOp->getFastMathFlags(); | ||||||||||
1739 | Flags &= SubOp->getFastMathFlags(); | ||||||||||
1740 | RI->setFastMathFlags(Flags); | ||||||||||
1741 | return RI; | ||||||||||
1742 | } else | ||||||||||
1743 | return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel); | ||||||||||
1744 | } | ||||||||||
1745 | } | ||||||||||
1746 | return nullptr; | ||||||||||
1747 | } | ||||||||||
1748 | |||||||||||
1749 | /// Turn X + Y overflows ? -1 : X + Y -> uadd_sat X, Y | ||||||||||
1750 | /// And X - Y overflows ? 0 : X - Y -> usub_sat X, Y | ||||||||||
1751 | /// Along with a number of patterns similar to: | ||||||||||
1752 | /// X + Y overflows ? (X < 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y | ||||||||||
1753 | /// X - Y overflows ? (X > 0 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y | ||||||||||
1754 | static Instruction * | ||||||||||
1755 | foldOverflowingAddSubSelect(SelectInst &SI, InstCombiner::BuilderTy &Builder) { | ||||||||||
1756 | Value *CondVal = SI.getCondition(); | ||||||||||
1757 | Value *TrueVal = SI.getTrueValue(); | ||||||||||
1758 | Value *FalseVal = SI.getFalseValue(); | ||||||||||
1759 | |||||||||||
1760 | WithOverflowInst *II; | ||||||||||
1761 | if (!match(CondVal, m_ExtractValue<1>(m_WithOverflowInst(II))) || | ||||||||||
1762 | !match(FalseVal, m_ExtractValue<0>(m_Specific(II)))) | ||||||||||
1763 | return nullptr; | ||||||||||
1764 | |||||||||||
1765 | Value *X = II->getLHS(); | ||||||||||
1766 | Value *Y = II->getRHS(); | ||||||||||
1767 | |||||||||||
1768 | auto IsSignedSaturateLimit = [&](Value *Limit, bool IsAdd) { | ||||||||||
1769 | Type *Ty = Limit->getType(); | ||||||||||
1770 | |||||||||||
1771 | ICmpInst::Predicate Pred; | ||||||||||
1772 | Value *TrueVal, *FalseVal, *Op; | ||||||||||
1773 | const APInt *C; | ||||||||||
1774 | if (!match(Limit, m_Select(m_ICmp(Pred, m_Value(Op), m_APInt(C)), | ||||||||||
1775 | m_Value(TrueVal), m_Value(FalseVal)))) | ||||||||||
1776 | return false; | ||||||||||
1777 | |||||||||||
1778 | auto IsZeroOrOne = [](const APInt &C) { | ||||||||||
1779 | return C.isNullValue() || C.isOneValue(); | ||||||||||
1780 | }; | ||||||||||
1781 | auto IsMinMax = [&](Value *Min, Value *Max) { | ||||||||||
1782 | APInt MinVal = APInt::getSignedMinValue(Ty->getScalarSizeInBits()); | ||||||||||
1783 | APInt MaxVal = APInt::getSignedMaxValue(Ty->getScalarSizeInBits()); | ||||||||||
1784 | return match(Min, m_SpecificInt(MinVal)) && | ||||||||||
1785 | match(Max, m_SpecificInt(MaxVal)); | ||||||||||
1786 | }; | ||||||||||
1787 | |||||||||||
1788 | if (Op != X && Op != Y) | ||||||||||
1789 | return false; | ||||||||||
1790 | |||||||||||
1791 | if (IsAdd) { | ||||||||||
1792 | // X + Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y | ||||||||||
1793 | // X + Y overflows ? (X <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y | ||||||||||
1794 | // X + Y overflows ? (Y <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y | ||||||||||
1795 | // X + Y overflows ? (Y <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y | ||||||||||
1796 | if (Pred == ICmpInst::ICMP_SLT && IsZeroOrOne(*C) && | ||||||||||
1797 | IsMinMax(TrueVal, FalseVal)) | ||||||||||
1798 | return true; | ||||||||||
1799 | // X + Y overflows ? (X >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y | ||||||||||
1800 | // X + Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y | ||||||||||
1801 | // X + Y overflows ? (Y >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y | ||||||||||
1802 | // X + Y overflows ? (Y >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y | ||||||||||
1803 | if (Pred == ICmpInst::ICMP_SGT && IsZeroOrOne(*C + 1) && | ||||||||||
1804 | IsMinMax(FalseVal, TrueVal)) | ||||||||||
1805 | return true; | ||||||||||
1806 | } else { | ||||||||||
1807 | // X - Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y | ||||||||||
1808 | // X - Y overflows ? (X <s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y | ||||||||||
1809 | if (Op == X && Pred == ICmpInst::ICMP_SLT && IsZeroOrOne(*C + 1) && | ||||||||||
1810 | IsMinMax(TrueVal, FalseVal)) | ||||||||||
1811 | return true; | ||||||||||
1812 | // X - Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y | ||||||||||
1813 | // X - Y overflows ? (X >s -2 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y | ||||||||||
1814 | if (Op == X && Pred == ICmpInst::ICMP_SGT && IsZeroOrOne(*C + 2) && | ||||||||||
1815 | IsMinMax(FalseVal, TrueVal)) | ||||||||||
1816 | return true; | ||||||||||
1817 | // X - Y overflows ? (Y <s 0 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y | ||||||||||
1818 | // X - Y overflows ? (Y <s 1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y | ||||||||||
1819 | if (Op == Y && Pred == ICmpInst::ICMP_SLT && IsZeroOrOne(*C) && | ||||||||||
1820 | IsMinMax(FalseVal, TrueVal)) | ||||||||||
1821 | return true; | ||||||||||
1822 | // X - Y overflows ? (Y >s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y | ||||||||||
1823 | // X - Y overflows ? (Y >s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y | ||||||||||
1824 | if (Op == Y && Pred == ICmpInst::ICMP_SGT && IsZeroOrOne(*C + 1) && | ||||||||||
1825 | IsMinMax(TrueVal, FalseVal)) | ||||||||||
1826 | return true; | ||||||||||
1827 | } | ||||||||||
1828 | |||||||||||
1829 | return false; | ||||||||||
1830 | }; | ||||||||||
1831 | |||||||||||
1832 | Intrinsic::ID NewIntrinsicID; | ||||||||||
1833 | if (II->getIntrinsicID() == Intrinsic::uadd_with_overflow && | ||||||||||
1834 | match(TrueVal, m_AllOnes())) | ||||||||||
1835 | // X + Y overflows ? -1 : X + Y -> uadd_sat X, Y | ||||||||||
1836 | NewIntrinsicID = Intrinsic::uadd_sat; | ||||||||||
1837 | else if (II->getIntrinsicID() == Intrinsic::usub_with_overflow && | ||||||||||
1838 | match(TrueVal, m_Zero())) | ||||||||||
1839 | // X - Y overflows ? 0 : X - Y -> usub_sat X, Y | ||||||||||
1840 | NewIntrinsicID = Intrinsic::usub_sat; | ||||||||||
1841 | else if (II->getIntrinsicID() == Intrinsic::sadd_with_overflow && | ||||||||||
1842 | IsSignedSaturateLimit(TrueVal, /*IsAdd=*/true)) | ||||||||||
1843 | // X + Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y | ||||||||||
1844 | // X + Y overflows ? (X <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y | ||||||||||
1845 | // X + Y overflows ? (X >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y | ||||||||||
1846 | // X + Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y | ||||||||||
1847 | // X + Y overflows ? (Y <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y | ||||||||||
1848 | // X + Y overflows ? (Y <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y | ||||||||||
1849 | // X + Y overflows ? (Y >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y | ||||||||||
1850 | // X + Y overflows ? (Y >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y | ||||||||||
1851 | NewIntrinsicID = Intrinsic::sadd_sat; | ||||||||||
1852 | else if (II->getIntrinsicID() == Intrinsic::ssub_with_overflow && | ||||||||||
1853 | IsSignedSaturateLimit(TrueVal, /*IsAdd=*/false)) | ||||||||||
1854 | // X - Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y | ||||||||||
1855 | // X - Y overflows ? (X <s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y | ||||||||||
1856 | // X - Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y | ||||||||||
1857 | // X - Y overflows ? (X >s -2 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y | ||||||||||
1858 | // X - Y overflows ? (Y <s 0 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y | ||||||||||
1859 | // X - Y overflows ? (Y <s 1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y | ||||||||||
1860 | // X - Y overflows ? (Y >s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y | ||||||||||
1861 | // X - Y overflows ? (Y >s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y | ||||||||||
1862 | NewIntrinsicID = Intrinsic::ssub_sat; | ||||||||||
1863 | else | ||||||||||
1864 | return nullptr; | ||||||||||
1865 | |||||||||||
1866 | Function *F = | ||||||||||
1867 | Intrinsic::getDeclaration(SI.getModule(), NewIntrinsicID, SI.getType()); | ||||||||||
1868 | return CallInst::Create(F, {X, Y}); | ||||||||||
1869 | } | ||||||||||
1870 | |||||||||||
1871 | Instruction *InstCombiner::foldSelectExtConst(SelectInst &Sel) { | ||||||||||
1872 | Constant *C; | ||||||||||
1873 | if (!match(Sel.getTrueValue(), m_Constant(C)) && | ||||||||||
1874 | !match(Sel.getFalseValue(), m_Constant(C))) | ||||||||||
1875 | return nullptr; | ||||||||||
1876 | |||||||||||
1877 | Instruction *ExtInst; | ||||||||||
1878 | if (!match(Sel.getTrueValue(), m_Instruction(ExtInst)) && | ||||||||||
1879 | !match(Sel.getFalseValue(), m_Instruction(ExtInst))) | ||||||||||
1880 | return nullptr; | ||||||||||
1881 | |||||||||||
1882 | auto ExtOpcode = ExtInst->getOpcode(); | ||||||||||
1883 | if (ExtOpcode != Instruction::ZExt && ExtOpcode != Instruction::SExt) | ||||||||||
1884 | return nullptr; | ||||||||||
1885 | |||||||||||
1886 | // If we are extending from a boolean type or if we can create a select that | ||||||||||
1887 | // has the same size operands as its condition, try to narrow the select. | ||||||||||
1888 | Value *X = ExtInst->getOperand(0); | ||||||||||
1889 | Type *SmallType = X->getType(); | ||||||||||
1890 | Value *Cond = Sel.getCondition(); | ||||||||||
1891 | auto *Cmp = dyn_cast<CmpInst>(Cond); | ||||||||||
1892 | if (!SmallType->isIntOrIntVectorTy(1) && | ||||||||||
1893 | (!Cmp || Cmp->getOperand(0)->getType() != SmallType)) | ||||||||||
1894 | return nullptr; | ||||||||||
1895 | |||||||||||
1896 | // If the constant is the same after truncation to the smaller type and | ||||||||||
1897 | // extension to the original type, we can narrow the select. | ||||||||||
1898 | Type *SelType = Sel.getType(); | ||||||||||
1899 | Constant *TruncC = ConstantExpr::getTrunc(C, SmallType); | ||||||||||
1900 | Constant *ExtC = ConstantExpr::getCast(ExtOpcode, TruncC, SelType); | ||||||||||
1901 | if (ExtC == C) { | ||||||||||
1902 | Value *TruncCVal = cast<Value>(TruncC); | ||||||||||
1903 | if (ExtInst == Sel.getFalseValue()) | ||||||||||
1904 | std::swap(X, TruncCVal); | ||||||||||
1905 | |||||||||||
1906 | // select Cond, (ext X), C --> ext(select Cond, X, C') | ||||||||||
1907 | // select Cond, C, (ext X) --> ext(select Cond, C', X) | ||||||||||
1908 | Value *NewSel = Builder.CreateSelect(Cond, X, TruncCVal, "narrow", &Sel); | ||||||||||
1909 | return CastInst::Create(Instruction::CastOps(ExtOpcode), NewSel, SelType); | ||||||||||
1910 | } | ||||||||||
1911 | |||||||||||
1912 | // If one arm of the select is the extend of the condition, replace that arm | ||||||||||
1913 | // with the extension of the appropriate known bool value. | ||||||||||
1914 | if (Cond == X) { | ||||||||||
1915 | if (ExtInst == Sel.getTrueValue()) { | ||||||||||
1916 | // select X, (sext X), C --> select X, -1, C | ||||||||||
1917 | // select X, (zext X), C --> select X, 1, C | ||||||||||
1918 | Constant *One = ConstantInt::getTrue(SmallType); | ||||||||||
1919 | Constant *AllOnesOrOne = ConstantExpr::getCast(ExtOpcode, One, SelType); | ||||||||||
1920 | return SelectInst::Create(Cond, AllOnesOrOne, C, "", nullptr, &Sel); | ||||||||||
1921 | } else { | ||||||||||
1922 | // select X, C, (sext X) --> select X, C, 0 | ||||||||||
1923 | // select X, C, (zext X) --> select X, C, 0 | ||||||||||
1924 | Constant *Zero = ConstantInt::getNullValue(SelType); | ||||||||||
1925 | return SelectInst::Create(Cond, C, Zero, "", nullptr, &Sel); | ||||||||||
1926 | } | ||||||||||
1927 | } | ||||||||||
1928 | |||||||||||
1929 | return nullptr; | ||||||||||
1930 | } | ||||||||||
1931 | |||||||||||
1932 | /// Try to transform a vector select with a constant condition vector into a | ||||||||||
1933 | /// shuffle for easier combining with other shuffles and insert/extract. | ||||||||||
1934 | static Instruction *canonicalizeSelectToShuffle(SelectInst &SI) { | ||||||||||
1935 | Value *CondVal = SI.getCondition(); | ||||||||||
1936 | Constant *CondC; | ||||||||||
1937 | if (!CondVal->getType()->isVectorTy() || !match(CondVal, m_Constant(CondC))) | ||||||||||
1938 | return nullptr; | ||||||||||
1939 | |||||||||||
1940 | unsigned NumElts = CondVal->getType()->getVectorNumElements(); | ||||||||||
1941 | SmallVector<Constant *, 16> Mask; | ||||||||||
1942 | Mask.reserve(NumElts); | ||||||||||
1943 | Type *Int32Ty = Type::getInt32Ty(CondVal->getContext()); | ||||||||||
1944 | for (unsigned i = 0; i != NumElts; ++i) { | ||||||||||
1945 | Constant *Elt = CondC->getAggregateElement(i); | ||||||||||
1946 | if (!Elt) | ||||||||||
1947 | return nullptr; | ||||||||||
1948 | |||||||||||
1949 | if (Elt->isOneValue()) { | ||||||||||
1950 | // If the select condition element is true, choose from the 1st vector. | ||||||||||
1951 | Mask.push_back(ConstantInt::get(Int32Ty, i)); | ||||||||||
1952 | } else if (Elt->isNullValue()) { | ||||||||||
1953 | // If the select condition element is false, choose from the 2nd vector. | ||||||||||
1954 | Mask.push_back(ConstantInt::get(Int32Ty, i + NumElts)); | ||||||||||
1955 | } else if (isa<UndefValue>(Elt)) { | ||||||||||
1956 | // Undef in a select condition (choose one of the operands) does not mean | ||||||||||
1957 | // the same thing as undef in a shuffle mask (any value is acceptable), so | ||||||||||
1958 | // give up. | ||||||||||
1959 | return nullptr; | ||||||||||
1960 | } else { | ||||||||||
1961 | // Bail out on a constant expression. | ||||||||||
1962 | return nullptr; | ||||||||||
1963 | } | ||||||||||
1964 | } | ||||||||||
1965 | |||||||||||
1966 | return new ShuffleVectorInst(SI.getTrueValue(), SI.getFalseValue(), | ||||||||||
1967 | ConstantVector::get(Mask)); | ||||||||||
1968 | } | ||||||||||
1969 | |||||||||||
1970 | /// If we have a select of vectors with a scalar condition, try to convert that | ||||||||||
1971 | /// to a vector select by splatting the condition. A splat may get folded with | ||||||||||
1972 | /// other operations in IR and having all operands of a select be vector types | ||||||||||
1973 | /// is likely better for vector codegen. | ||||||||||
1974 | static Instruction *canonicalizeScalarSelectOfVecs( | ||||||||||
1975 | SelectInst &Sel, InstCombiner &IC) { | ||||||||||
1976 | Type *Ty = Sel.getType(); | ||||||||||
1977 | if (!Ty->isVectorTy()) | ||||||||||
1978 | return nullptr; | ||||||||||
1979 | |||||||||||
1980 | // We can replace a single-use extract with constant index. | ||||||||||
1981 | Value *Cond = Sel.getCondition(); | ||||||||||
1982 | if (!match(Cond, m_OneUse(m_ExtractElement(m_Value(), m_ConstantInt())))) | ||||||||||
1983 | return nullptr; | ||||||||||
1984 | |||||||||||
1985 | // select (extelt V, Index), T, F --> select (splat V, Index), T, F | ||||||||||
1986 | // Splatting the extracted condition reduces code (we could directly create a | ||||||||||
1987 | // splat shuffle of the source vector to eliminate the intermediate step). | ||||||||||
1988 | unsigned NumElts = Ty->getVectorNumElements(); | ||||||||||
1989 | return IC.replaceOperand(Sel, 0, IC.Builder.CreateVectorSplat(NumElts, Cond)); | ||||||||||
1990 | } | ||||||||||
1991 | |||||||||||
1992 | /// Reuse bitcasted operands between a compare and select: | ||||||||||
1993 | /// select (cmp (bitcast C), (bitcast D)), (bitcast' C), (bitcast' D) --> | ||||||||||
1994 | /// bitcast (select (cmp (bitcast C), (bitcast D)), (bitcast C), (bitcast D)) | ||||||||||
1995 | static Instruction *foldSelectCmpBitcasts(SelectInst &Sel, | ||||||||||
1996 | InstCombiner::BuilderTy &Builder) { | ||||||||||
1997 | Value *Cond = Sel.getCondition(); | ||||||||||
1998 | Value *TVal = Sel.getTrueValue(); | ||||||||||
1999 | Value *FVal = Sel.getFalseValue(); | ||||||||||
2000 | |||||||||||
2001 | CmpInst::Predicate Pred; | ||||||||||
2002 | Value *A, *B; | ||||||||||
2003 | if (!match(Cond, m_Cmp(Pred, m_Value(A), m_Value(B)))) | ||||||||||
2004 | return nullptr; | ||||||||||
2005 | |||||||||||
2006 | // The select condition is a compare instruction. If the select's true/false | ||||||||||
2007 | // values are already the same as the compare operands, there's nothing to do. | ||||||||||
2008 | if (TVal == A || TVal == B || FVal == A || FVal == B) | ||||||||||
2009 | return nullptr; | ||||||||||
2010 | |||||||||||
2011 | Value *C, *D; | ||||||||||
2012 | if (!match(A, m_BitCast(m_Value(C))) || !match(B, m_BitCast(m_Value(D)))) | ||||||||||
2013 | return nullptr; | ||||||||||
2014 | |||||||||||
2015 | // select (cmp (bitcast C), (bitcast D)), (bitcast TSrc), (bitcast FSrc) | ||||||||||
2016 | Value *TSrc, *FSrc; | ||||||||||
2017 | if (!match(TVal, m_BitCast(m_Value(TSrc))) || | ||||||||||
2018 | !match(FVal, m_BitCast(m_Value(FSrc)))) | ||||||||||
2019 | return nullptr; | ||||||||||
2020 | |||||||||||
2021 | // If the select true/false values are *different bitcasts* of the same source | ||||||||||
2022 | // operands, make the select operands the same as the compare operands and | ||||||||||
2023 | // cast the result. This is the canonical select form for min/max. | ||||||||||
2024 | Value *NewSel; | ||||||||||
2025 | if (TSrc == C && FSrc == D) { | ||||||||||
2026 | // select (cmp (bitcast C), (bitcast D)), (bitcast' C), (bitcast' D) --> | ||||||||||
2027 | // bitcast (select (cmp A, B), A, B) | ||||||||||
2028 | NewSel = Builder.CreateSelect(Cond, A, B, "", &Sel); | ||||||||||
2029 | } else if (TSrc == D && FSrc == C) { | ||||||||||
2030 | // select (cmp (bitcast C), (bitcast D)), (bitcast' D), (bitcast' C) --> | ||||||||||
2031 | // bitcast (select (cmp A, B), B, A) | ||||||||||
2032 | NewSel = Builder.CreateSelect(Cond, B, A, "", &Sel); | ||||||||||
2033 | } else { | ||||||||||
2034 | return nullptr; | ||||||||||
2035 | } | ||||||||||
2036 | return CastInst::CreateBitOrPointerCast(NewSel, Sel.getType()); | ||||||||||
2037 | } | ||||||||||
2038 | |||||||||||
2039 | /// Try to eliminate select instructions that test the returned flag of cmpxchg | ||||||||||
2040 | /// instructions. | ||||||||||
2041 | /// | ||||||||||
2042 | /// If a select instruction tests the returned flag of a cmpxchg instruction and | ||||||||||
2043 | /// selects between the returned value of the cmpxchg instruction its compare | ||||||||||
2044 | /// operand, the result of the select will always be equal to its false value. | ||||||||||
2045 | /// For example: | ||||||||||
2046 | /// | ||||||||||
2047 | /// %0 = cmpxchg i64* %ptr, i64 %compare, i64 %new_value seq_cst seq_cst | ||||||||||
2048 | /// %1 = extractvalue { i64, i1 } %0, 1 | ||||||||||
2049 | /// %2 = extractvalue { i64, i1 } %0, 0 | ||||||||||
2050 | /// %3 = select i1 %1, i64 %compare, i64 %2 | ||||||||||
2051 | /// ret i64 %3 | ||||||||||
2052 | /// | ||||||||||
2053 | /// The returned value of the cmpxchg instruction (%2) is the original value | ||||||||||
2054 | /// located at %ptr prior to any update. If the cmpxchg operation succeeds, %2 | ||||||||||
2055 | /// must have been equal to %compare. Thus, the result of the select is always | ||||||||||
2056 | /// equal to %2, and the code can be simplified to: | ||||||||||
2057 | /// | ||||||||||
2058 | /// %0 = cmpxchg i64* %ptr, i64 %compare, i64 %new_value seq_cst seq_cst | ||||||||||
2059 | /// %1 = extractvalue { i64, i1 } %0, 0 | ||||||||||
2060 | /// ret i64 %1 | ||||||||||
2061 | /// | ||||||||||
2062 | static Instruction *foldSelectCmpXchg(SelectInst &SI) { | ||||||||||
2063 | // A helper that determines if V is an extractvalue instruction whose | ||||||||||
2064 | // aggregate operand is a cmpxchg instruction and whose single index is equal | ||||||||||
2065 | // to I. If such conditions are true, the helper returns the cmpxchg | ||||||||||
2066 | // instruction; otherwise, a nullptr is returned. | ||||||||||
2067 | auto isExtractFromCmpXchg = [](Value *V, unsigned I) -> AtomicCmpXchgInst * { | ||||||||||
2068 | auto *Extract = dyn_cast<ExtractValueInst>(V); | ||||||||||
2069 | if (!Extract) | ||||||||||
2070 | return nullptr; | ||||||||||
2071 | if (Extract->getIndices()[0] != I) | ||||||||||
2072 | return nullptr; | ||||||||||
2073 | return dyn_cast<AtomicCmpXchgInst>(Extract->getAggregateOperand()); | ||||||||||
2074 | }; | ||||||||||
2075 | |||||||||||
2076 | // If the select has a single user, and this user is a select instruction that | ||||||||||
2077 | // we can simplify, skip the cmpxchg simplification for now. | ||||||||||
2078 | if (SI.hasOneUse()) | ||||||||||
2079 | if (auto *Select = dyn_cast<SelectInst>(SI.user_back())) | ||||||||||
2080 | if (Select->getCondition() == SI.getCondition()) | ||||||||||
2081 | if (Select->getFalseValue() == SI.getTrueValue() || | ||||||||||
2082 | Select->getTrueValue() == SI.getFalseValue()) | ||||||||||
2083 | return nullptr; | ||||||||||
2084 | |||||||||||
2085 | // Ensure the select condition is the returned flag of a cmpxchg instruction. | ||||||||||
2086 | auto *CmpXchg = isExtractFromCmpXchg(SI.getCondition(), 1); | ||||||||||
2087 | if (!CmpXchg) | ||||||||||
2088 | return nullptr; | ||||||||||
2089 | |||||||||||
2090 | // Check the true value case: The true value of the select is the returned | ||||||||||
2091 | // value of the same cmpxchg used by the condition, and the false value is the | ||||||||||
2092 | // cmpxchg instruction's compare operand. | ||||||||||
2093 | if (auto *X = isExtractFromCmpXchg(SI.getTrueValue(), 0)) | ||||||||||
2094 | if (X == CmpXchg && X->getCompareOperand() == SI.getFalseValue()) { | ||||||||||
2095 | SI.setTrueValue(SI.getFalseValue()); | ||||||||||
2096 | return &SI; | ||||||||||
2097 | } | ||||||||||
2098 | |||||||||||
2099 | // Check the false value case: The false value of the select is the returned | ||||||||||
2100 | // value of the same cmpxchg used by the condition, and the true value is the | ||||||||||
2101 | // cmpxchg instruction's compare operand. | ||||||||||
2102 | if (auto *X = isExtractFromCmpXchg(SI.getFalseValue(), 0)) | ||||||||||
2103 | if (X == CmpXchg && X->getCompareOperand() == SI.getTrueValue()) { | ||||||||||
2104 | SI.setTrueValue(SI.getFalseValue()); | ||||||||||
2105 | return &SI; | ||||||||||
2106 | } | ||||||||||
2107 | |||||||||||
2108 | return nullptr; | ||||||||||
2109 | } | ||||||||||
2110 | |||||||||||
2111 | static Instruction *moveAddAfterMinMax(SelectPatternFlavor SPF, Value *X, | ||||||||||
2112 | Value *Y, | ||||||||||
2113 | InstCombiner::BuilderTy &Builder) { | ||||||||||
2114 | assert(SelectPatternResult::isMinOrMax(SPF) && "Expected min/max pattern")((SelectPatternResult::isMinOrMax(SPF) && "Expected min/max pattern" ) ? static_cast<void> (0) : __assert_fail ("SelectPatternResult::isMinOrMax(SPF) && \"Expected min/max pattern\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp" , 2114, __PRETTY_FUNCTION__)); | ||||||||||
2115 | bool IsUnsigned = SPF == SelectPatternFlavor::SPF_UMIN || | ||||||||||
2116 | SPF == SelectPatternFlavor::SPF_UMAX; | ||||||||||
2117 | // TODO: If InstSimplify could fold all cases where C2 <= C1, we could change | ||||||||||
2118 | // the constant value check to an assert. | ||||||||||
2119 | Value *A; | ||||||||||
2120 | const APInt *C1, *C2; | ||||||||||
2121 | if (IsUnsigned && match(X, m_NUWAdd(m_Value(A), m_APInt(C1))) && | ||||||||||
2122 | match(Y, m_APInt(C2)) && C2->uge(*C1) && X->hasNUses(2)) { | ||||||||||
2123 | // umin (add nuw A, C1), C2 --> add nuw (umin A, C2 - C1), C1 | ||||||||||
2124 | // umax (add nuw A, C1), C2 --> add nuw (umax A, C2 - C1), C1 | ||||||||||
2125 | Value *NewMinMax = createMinMax(Builder, SPF, A, | ||||||||||
2126 | ConstantInt::get(X->getType(), *C2 - *C1)); | ||||||||||
2127 | return BinaryOperator::CreateNUW(BinaryOperator::Add, NewMinMax, | ||||||||||
2128 | ConstantInt::get(X->getType(), *C1)); | ||||||||||
2129 | } | ||||||||||
2130 | |||||||||||
2131 | if (!IsUnsigned && match(X, m_NSWAdd(m_Value(A), m_APInt(C1))) && | ||||||||||
2132 | match(Y, m_APInt(C2)) && X->hasNUses(2)) { | ||||||||||
2133 | bool Overflow; | ||||||||||
2134 | APInt Diff = C2->ssub_ov(*C1, Overflow); | ||||||||||
2135 | if (!Overflow) { | ||||||||||
2136 | // smin (add nsw A, C1), C2 --> add nsw (smin A, C2 - C1), C1 | ||||||||||
2137 | // smax (add nsw A, C1), C2 --> add nsw (smax A, C2 - C1), C1 | ||||||||||
2138 | Value *NewMinMax = createMinMax(Builder, SPF, A, | ||||||||||
2139 | ConstantInt::get(X->getType(), Diff)); | ||||||||||
2140 | return BinaryOperator::CreateNSW(BinaryOperator::Add, NewMinMax, | ||||||||||
2141 | ConstantInt::get(X->getType(), *C1)); | ||||||||||
2142 | } | ||||||||||
2143 | } | ||||||||||
2144 | |||||||||||
2145 | return nullptr; | ||||||||||
2146 | } | ||||||||||
2147 | |||||||||||
2148 | /// Match a sadd_sat or ssub_sat which is using min/max to clamp the value. | ||||||||||
2149 | Instruction *InstCombiner::matchSAddSubSat(SelectInst &MinMax1) { | ||||||||||
2150 | Type *Ty = MinMax1.getType(); | ||||||||||
2151 | |||||||||||
2152 | // We are looking for a tree of: | ||||||||||
2153 | // max(INT_MIN, min(INT_MAX, add(sext(A), sext(B)))) | ||||||||||
2154 | // Where the min and max could be reversed | ||||||||||
2155 | Instruction *MinMax2; | ||||||||||
2156 | BinaryOperator *AddSub; | ||||||||||
2157 | const APInt *MinValue, *MaxValue; | ||||||||||
2158 | if (match(&MinMax1, m_SMin(m_Instruction(MinMax2), m_APInt(MaxValue)))) { | ||||||||||
2159 | if (!match(MinMax2, m_SMax(m_BinOp(AddSub), m_APInt(MinValue)))) | ||||||||||
2160 | return nullptr; | ||||||||||
2161 | } else if (match(&MinMax1, | ||||||||||
2162 | m_SMax(m_Instruction(MinMax2), m_APInt(MinValue)))) { | ||||||||||
2163 | if (!match(MinMax2, m_SMin(m_BinOp(AddSub), m_APInt(MaxValue)))) | ||||||||||
2164 | return nullptr; | ||||||||||
2165 | } else | ||||||||||
2166 | return nullptr; | ||||||||||
2167 | |||||||||||
2168 | // Check that the constants clamp a saturate, and that the new type would be | ||||||||||
2169 | // sensible to convert to. | ||||||||||
2170 | if (!(*MaxValue + 1).isPowerOf2() || -*MinValue != *MaxValue + 1) | ||||||||||
2171 | return nullptr; | ||||||||||
2172 | // In what bitwidth can this be treated as saturating arithmetics? | ||||||||||
2173 | unsigned NewBitWidth = (*MaxValue + 1).logBase2() + 1; | ||||||||||
2174 | // FIXME: This isn't quite right for vectors, but using the scalar type is a | ||||||||||
2175 | // good first approximation for what should be done there. | ||||||||||
2176 | if (!shouldChangeType(Ty->getScalarType()->getIntegerBitWidth(), NewBitWidth)) | ||||||||||
2177 | return nullptr; | ||||||||||
2178 | |||||||||||
2179 | // Also make sure that the number of uses is as expected. The "3"s are for the | ||||||||||
2180 | // the two items of min/max (the compare and the select). | ||||||||||
2181 | if (MinMax2->hasNUsesOrMore(3) || AddSub->hasNUsesOrMore(3)) | ||||||||||
2182 | return nullptr; | ||||||||||
2183 | |||||||||||
2184 | // Create the new type (which can be a vector type) | ||||||||||
2185 | Type *NewTy = Ty->getWithNewBitWidth(NewBitWidth); | ||||||||||
2186 | // Match the two extends from the add/sub | ||||||||||
2187 | Value *A, *B; | ||||||||||
2188 | if(!match(AddSub, m_BinOp(m_SExt(m_Value(A)), m_SExt(m_Value(B))))) | ||||||||||
2189 | return nullptr; | ||||||||||
2190 | // And check the incoming values are of a type smaller than or equal to the | ||||||||||
2191 | // size of the saturation. Otherwise the higher bits can cause different | ||||||||||
2192 | // results. | ||||||||||
2193 | if (A->getType()->getScalarSizeInBits() > NewBitWidth || | ||||||||||
2194 | B->getType()->getScalarSizeInBits() > NewBitWidth) | ||||||||||
2195 | return nullptr; | ||||||||||
2196 | |||||||||||
2197 | Intrinsic::ID IntrinsicID; | ||||||||||
2198 | if (AddSub->getOpcode() == Instruction::Add) | ||||||||||
2199 | IntrinsicID = Intrinsic::sadd_sat; | ||||||||||
2200 | else if (AddSub->getOpcode() == Instruction::Sub) | ||||||||||
2201 | IntrinsicID = Intrinsic::ssub_sat; | ||||||||||
2202 | else | ||||||||||
2203 | return nullptr; | ||||||||||
2204 | |||||||||||
2205 | // Finally create and return the sat intrinsic, truncated to the new type | ||||||||||
2206 | Function *F = Intrinsic::getDeclaration(MinMax1.getModule(), IntrinsicID, NewTy); | ||||||||||
2207 | Value *AT = Builder.CreateSExt(A, NewTy); | ||||||||||
2208 | Value *BT = Builder.CreateSExt(B, NewTy); | ||||||||||
2209 | Value *Sat = Builder.CreateCall(F, {AT, BT}); | ||||||||||
2210 | return CastInst::Create(Instruction::SExt, Sat, Ty); | ||||||||||
2211 | } | ||||||||||
2212 | |||||||||||
2213 | /// Reduce a sequence of min/max with a common operand. | ||||||||||
2214 | static Instruction *factorizeMinMaxTree(SelectPatternFlavor SPF, Value *LHS, | ||||||||||
2215 | Value *RHS, | ||||||||||
2216 | InstCombiner::BuilderTy &Builder) { | ||||||||||
2217 | assert(SelectPatternResult::isMinOrMax(SPF) && "Expected a min/max")((SelectPatternResult::isMinOrMax(SPF) && "Expected a min/max" ) ? static_cast<void> (0) : __assert_fail ("SelectPatternResult::isMinOrMax(SPF) && \"Expected a min/max\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp" , 2217, __PRETTY_FUNCTION__)); | ||||||||||
2218 | // TODO: Allow FP min/max with nnan/nsz. | ||||||||||
2219 | if (!LHS->getType()->isIntOrIntVectorTy()) | ||||||||||
2220 | return nullptr; | ||||||||||
2221 | |||||||||||
2222 | // Match 3 of the same min/max ops. Example: umin(umin(), umin()). | ||||||||||
2223 | Value *A, *B, *C, *D; | ||||||||||
2224 | SelectPatternResult L = matchSelectPattern(LHS, A, B); | ||||||||||
2225 | SelectPatternResult R = matchSelectPattern(RHS, C, D); | ||||||||||
2226 | if (SPF != L.Flavor || L.Flavor != R.Flavor) | ||||||||||
2227 | return nullptr; | ||||||||||
2228 | |||||||||||
2229 | // Look for a common operand. The use checks are different than usual because | ||||||||||
2230 | // a min/max pattern typically has 2 uses of each op: 1 by the cmp and 1 by | ||||||||||
2231 | // the select. | ||||||||||
2232 | Value *MinMaxOp = nullptr; | ||||||||||
2233 | Value *ThirdOp = nullptr; | ||||||||||
2234 | if (!LHS->hasNUsesOrMore(3) && RHS->hasNUsesOrMore(3)) { | ||||||||||
2235 | // If the LHS is only used in this chain and the RHS is used outside of it, | ||||||||||
2236 | // reuse the RHS min/max because that will eliminate the LHS. | ||||||||||
2237 | if (D == A || C == A) { | ||||||||||
2238 | // min(min(a, b), min(c, a)) --> min(min(c, a), b) | ||||||||||
2239 | // min(min(a, b), min(a, d)) --> min(min(a, d), b) | ||||||||||
2240 | MinMaxOp = RHS; | ||||||||||
2241 | ThirdOp = B; | ||||||||||
2242 | } else if (D == B || C == B) { | ||||||||||
2243 | // min(min(a, b), min(c, b)) --> min(min(c, b), a) | ||||||||||
2244 | // min(min(a, b), min(b, d)) --> min(min(b, d), a) | ||||||||||
2245 | MinMaxOp = RHS; | ||||||||||
2246 | ThirdOp = A; | ||||||||||
2247 | } | ||||||||||
2248 | } else if (!RHS->hasNUsesOrMore(3)) { | ||||||||||
2249 | // Reuse the LHS. This will eliminate the RHS. | ||||||||||
2250 | if (D == A || D == B) { | ||||||||||
2251 | // min(min(a, b), min(c, a)) --> min(min(a, b), c) | ||||||||||
2252 | // min(min(a, b), min(c, b)) --> min(min(a, b), c) | ||||||||||
2253 | MinMaxOp = LHS; | ||||||||||
2254 | ThirdOp = C; | ||||||||||
2255 | } else if (C == A || C == B) { | ||||||||||
2256 | // min(min(a, b), min(b, d)) --> min(min(a, b), d) | ||||||||||
2257 | // min(min(a, b), min(c, b)) --> min(min(a, b), d) | ||||||||||
2258 | MinMaxOp = LHS; | ||||||||||
2259 | ThirdOp = D; | ||||||||||
2260 | } | ||||||||||
2261 | } | ||||||||||
2262 | if (!MinMaxOp || !ThirdOp) | ||||||||||
2263 | return nullptr; | ||||||||||
2264 | |||||||||||
2265 | CmpInst::Predicate P = getMinMaxPred(SPF); | ||||||||||
2266 | Value *CmpABC = Builder.CreateICmp(P, MinMaxOp, ThirdOp); | ||||||||||
2267 | return SelectInst::Create(CmpABC, MinMaxOp, ThirdOp); | ||||||||||
2268 | } | ||||||||||
2269 | |||||||||||
2270 | /// Try to reduce a rotate pattern that includes a compare and select into a | ||||||||||
2271 | /// funnel shift intrinsic. Example: | ||||||||||
2272 | /// rotl32(a, b) --> (b == 0 ? a : ((a >> (32 - b)) | (a << b))) | ||||||||||
2273 | /// --> call llvm.fshl.i32(a, a, b) | ||||||||||
2274 | static Instruction *foldSelectRotate(SelectInst &Sel) { | ||||||||||
2275 | // The false value of the select must be a rotate of the true value. | ||||||||||
2276 | Value *Or0, *Or1; | ||||||||||
2277 | if (!match(Sel.getFalseValue(), m_OneUse(m_Or(m_Value(Or0), m_Value(Or1))))) | ||||||||||
2278 | return nullptr; | ||||||||||
2279 | |||||||||||
2280 | Value *TVal = Sel.getTrueValue(); | ||||||||||
2281 | Value *SA0, *SA1; | ||||||||||
2282 | if (!match(Or0, m_OneUse(m_LogicalShift(m_Specific(TVal), m_Value(SA0)))) || | ||||||||||
2283 | !match(Or1, m_OneUse(m_LogicalShift(m_Specific(TVal), m_Value(SA1))))) | ||||||||||
2284 | return nullptr; | ||||||||||
2285 | |||||||||||
2286 | auto ShiftOpcode0 = cast<BinaryOperator>(Or0)->getOpcode(); | ||||||||||
2287 | auto ShiftOpcode1 = cast<BinaryOperator>(Or1)->getOpcode(); | ||||||||||
2288 | if (ShiftOpcode0 == ShiftOpcode1) | ||||||||||
2289 | return nullptr; | ||||||||||
2290 | |||||||||||
2291 | // We have one of these patterns so far: | ||||||||||
2292 | // select ?, TVal, (or (lshr TVal, SA0), (shl TVal, SA1)) | ||||||||||
2293 | // select ?, TVal, (or (shl TVal, SA0), (lshr TVal, SA1)) | ||||||||||
2294 | // This must be a power-of-2 rotate for a bitmasking transform to be valid. | ||||||||||
2295 | unsigned Width = Sel.getType()->getScalarSizeInBits(); | ||||||||||
2296 | if (!isPowerOf2_32(Width)) | ||||||||||
2297 | return nullptr; | ||||||||||
2298 | |||||||||||
2299 | // Check the shift amounts to see if they are an opposite pair. | ||||||||||
2300 | Value *ShAmt; | ||||||||||
2301 | if (match(SA1, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(SA0))))) | ||||||||||
2302 | ShAmt = SA0; | ||||||||||
2303 | else if (match(SA0, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(SA1))))) | ||||||||||
2304 | ShAmt = SA1; | ||||||||||
2305 | else | ||||||||||
2306 | return nullptr; | ||||||||||
2307 | |||||||||||
2308 | // Finally, see if the select is filtering out a shift-by-zero. | ||||||||||
2309 | Value *Cond = Sel.getCondition(); | ||||||||||
2310 | ICmpInst::Predicate Pred; | ||||||||||
2311 | if (!match(Cond, m_OneUse(m_ICmp(Pred, m_Specific(ShAmt), m_ZeroInt()))) || | ||||||||||
2312 | Pred != ICmpInst::ICMP_EQ) | ||||||||||
2313 | return nullptr; | ||||||||||
2314 | |||||||||||
2315 | // This is a rotate that avoids shift-by-bitwidth UB in a suboptimal way. | ||||||||||
2316 | // Convert to funnel shift intrinsic. | ||||||||||
2317 | bool IsFshl = (ShAmt == SA0 && ShiftOpcode0 == BinaryOperator::Shl) || | ||||||||||
2318 | (ShAmt == SA1 && ShiftOpcode1 == BinaryOperator::Shl); | ||||||||||
2319 | Intrinsic::ID IID = IsFshl ? Intrinsic::fshl : Intrinsic::fshr; | ||||||||||
2320 | Function *F = Intrinsic::getDeclaration(Sel.getModule(), IID, Sel.getType()); | ||||||||||
2321 | return IntrinsicInst::Create(F, { TVal, TVal, ShAmt }); | ||||||||||
2322 | } | ||||||||||
2323 | |||||||||||
2324 | static Instruction *foldSelectToCopysign(SelectInst &Sel, | ||||||||||
2325 | InstCombiner::BuilderTy &Builder) { | ||||||||||
2326 | Value *Cond = Sel.getCondition(); | ||||||||||
2327 | Value *TVal = Sel.getTrueValue(); | ||||||||||
2328 | Value *FVal = Sel.getFalseValue(); | ||||||||||
2329 | Type *SelType = Sel.getType(); | ||||||||||
2330 | |||||||||||
2331 | // Match select ?, TC, FC where the constants are equal but negated. | ||||||||||
2332 | // TODO: Generalize to handle a negated variable operand? | ||||||||||
2333 | const APFloat *TC, *FC; | ||||||||||
2334 | if (!match(TVal, m_APFloat(TC)) || !match(FVal, m_APFloat(FC)) || | ||||||||||
2335 | !abs(*TC).bitwiseIsEqual(abs(*FC))) | ||||||||||
2336 | return nullptr; | ||||||||||
2337 | |||||||||||
2338 | assert(TC != FC && "Expected equal select arms to simplify")((TC != FC && "Expected equal select arms to simplify" ) ? static_cast<void> (0) : __assert_fail ("TC != FC && \"Expected equal select arms to simplify\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp" , 2338, __PRETTY_FUNCTION__)); | ||||||||||
2339 | |||||||||||
2340 | Value *X; | ||||||||||
2341 | const APInt *C; | ||||||||||
2342 | bool IsTrueIfSignSet; | ||||||||||
2343 | ICmpInst::Predicate Pred; | ||||||||||
2344 | if (!match(Cond, m_OneUse(m_ICmp(Pred, m_BitCast(m_Value(X)), m_APInt(C)))) || | ||||||||||
2345 | !isSignBitCheck(Pred, *C, IsTrueIfSignSet) || X->getType() != SelType) | ||||||||||
2346 | return nullptr; | ||||||||||
2347 | |||||||||||
2348 | // If needed, negate the value that will be the sign argument of the copysign: | ||||||||||
2349 | // (bitcast X) < 0 ? -TC : TC --> copysign(TC, X) | ||||||||||
2350 | // (bitcast X) < 0 ? TC : -TC --> copysign(TC, -X) | ||||||||||
2351 | // (bitcast X) >= 0 ? -TC : TC --> copysign(TC, -X) | ||||||||||
2352 | // (bitcast X) >= 0 ? TC : -TC --> copysign(TC, X) | ||||||||||
2353 | if (IsTrueIfSignSet ^ TC->isNegative()) | ||||||||||
2354 | X = Builder.CreateFNegFMF(X, &Sel); | ||||||||||
2355 | |||||||||||
2356 | // Canonicalize the magnitude argument as the positive constant since we do | ||||||||||
2357 | // not care about its sign. | ||||||||||
2358 | Value *MagArg = TC->isNegative() ? FVal : TVal; | ||||||||||
2359 | Function *F = Intrinsic::getDeclaration(Sel.getModule(), Intrinsic::copysign, | ||||||||||
2360 | Sel.getType()); | ||||||||||
2361 | Instruction *CopySign = IntrinsicInst::Create(F, { MagArg, X }); | ||||||||||
2362 | CopySign->setFastMathFlags(Sel.getFastMathFlags()); | ||||||||||
2363 | return CopySign; | ||||||||||
2364 | } | ||||||||||
2365 | |||||||||||
2366 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { | ||||||||||
2367 | Value *CondVal = SI.getCondition(); | ||||||||||
2368 | Value *TrueVal = SI.getTrueValue(); | ||||||||||
2369 | Value *FalseVal = SI.getFalseValue(); | ||||||||||
| |||||||||||
2370 | Type *SelType = SI.getType(); | ||||||||||
2371 | |||||||||||
2372 | // FIXME: Remove this workaround when freeze related patches are done. | ||||||||||
2373 | // For select with undef operand which feeds into an equality comparison, | ||||||||||
2374 | // don't simplify it so loop unswitch can know the equality comparison | ||||||||||
2375 | // may have an undef operand. This is a workaround for PR31652 caused by | ||||||||||
2376 | // descrepancy about branch on undef between LoopUnswitch and GVN. | ||||||||||
2377 | if (isa<UndefValue>(TrueVal) || isa<UndefValue>(FalseVal)) { | ||||||||||
2378 | if (llvm::any_of(SI.users(), [&](User *U) { | ||||||||||
2379 | ICmpInst *CI = dyn_cast<ICmpInst>(U); | ||||||||||
2380 | if (CI && CI->isEquality()) | ||||||||||
2381 | return true; | ||||||||||
2382 | return false; | ||||||||||
2383 | })) { | ||||||||||
2384 | return nullptr; | ||||||||||
2385 | } | ||||||||||
2386 | } | ||||||||||
2387 | |||||||||||
2388 | if (Value *V = SimplifySelectInst(CondVal, TrueVal, FalseVal, | ||||||||||
2389 | SQ.getWithInstruction(&SI))) | ||||||||||
2390 | return replaceInstUsesWith(SI, V); | ||||||||||
2391 | |||||||||||
2392 | if (Instruction *I
| ||||||||||
2393 | return I; | ||||||||||
2394 | |||||||||||
2395 | if (Instruction *I
| ||||||||||
2396 | return I; | ||||||||||
2397 | |||||||||||
2398 | // Canonicalize a one-use integer compare with a non-canonical predicate by | ||||||||||
2399 | // inverting the predicate and swapping the select operands. This matches a | ||||||||||
2400 | // compare canonicalization for conditional branches. | ||||||||||
2401 | // TODO: Should we do the same for FP compares? | ||||||||||
2402 | CmpInst::Predicate Pred; | ||||||||||
2403 | if (match(CondVal, m_OneUse(m_ICmp(Pred, m_Value(), m_Value()))) && | ||||||||||
2404 | !isCanonicalPredicate(Pred)) { | ||||||||||
2405 | // Swap true/false values and condition. | ||||||||||
2406 | CmpInst *Cond = cast<CmpInst>(CondVal); | ||||||||||
2407 | Cond->setPredicate(CmpInst::getInversePredicate(Pred)); | ||||||||||
2408 | SI.swapValues(); | ||||||||||
2409 | SI.swapProfMetadata(); | ||||||||||
2410 | Worklist.push(Cond); | ||||||||||
2411 | return &SI; | ||||||||||
2412 | } | ||||||||||
2413 | |||||||||||
2414 | if (SelType->isIntOrIntVectorTy(1) && | ||||||||||
2415 | TrueVal->getType() == CondVal->getType()) { | ||||||||||
2416 | if (match(TrueVal, m_One())) { | ||||||||||
2417 | // Change: A = select B, true, C --> A = or B, C | ||||||||||
2418 | return BinaryOperator::CreateOr(CondVal, FalseVal); | ||||||||||
2419 | } | ||||||||||
2420 | if (match(TrueVal, m_Zero())) { | ||||||||||
2421 | // Change: A = select B, false, C --> A = and !B, C | ||||||||||
2422 | Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName()); | ||||||||||
2423 | return BinaryOperator::CreateAnd(NotCond, FalseVal); | ||||||||||
2424 | } | ||||||||||
2425 | if (match(FalseVal, m_Zero())) { | ||||||||||
2426 | // Change: A = select B, C, false --> A = and B, C | ||||||||||
2427 | return BinaryOperator::CreateAnd(CondVal, TrueVal); | ||||||||||
2428 | } | ||||||||||
2429 | if (match(FalseVal, m_One())) { | ||||||||||
2430 | // Change: A = select B, C, true --> A = or !B, C | ||||||||||
2431 | Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName()); | ||||||||||
2432 | return BinaryOperator::CreateOr(NotCond, TrueVal); | ||||||||||
2433 | } | ||||||||||
2434 | |||||||||||
2435 | // select a, a, b -> a | b | ||||||||||
2436 | // select a, b, a -> a & b | ||||||||||
2437 | if (CondVal == TrueVal) | ||||||||||
2438 | return BinaryOperator::CreateOr(CondVal, FalseVal); | ||||||||||
2439 | if (CondVal == FalseVal) | ||||||||||
2440 | return BinaryOperator::CreateAnd(CondVal, TrueVal); | ||||||||||
2441 | |||||||||||
2442 | // select a, ~a, b -> (~a) & b | ||||||||||
2443 | // select a, b, ~a -> (~a) | b | ||||||||||
2444 | if (match(TrueVal, m_Not(m_Specific(CondVal)))) | ||||||||||
2445 | return BinaryOperator::CreateAnd(TrueVal, FalseVal); | ||||||||||
2446 | if (match(FalseVal, m_Not(m_Specific(CondVal)))) | ||||||||||
2447 | return BinaryOperator::CreateOr(TrueVal, FalseVal); | ||||||||||
2448 | } | ||||||||||
2449 | |||||||||||
2450 | // Selecting between two integer or vector splat integer constants? | ||||||||||
2451 | // | ||||||||||
2452 | // Note that we don't handle a scalar select of vectors: | ||||||||||
2453 | // select i1 %c, <2 x i8> <1, 1>, <2 x i8> <0, 0> | ||||||||||
2454 | // because that may need 3 instructions to splat the condition value: | ||||||||||
2455 | // extend, insertelement, shufflevector. | ||||||||||
2456 | if (SelType->isIntOrIntVectorTy() && | ||||||||||
2457 | CondVal->getType()->isVectorTy() == SelType->isVectorTy()) { | ||||||||||
2458 | // select C, 1, 0 -> zext C to int | ||||||||||
2459 | if (match(TrueVal, m_One()) && match(FalseVal, m_Zero())) | ||||||||||
2460 | return new ZExtInst(CondVal, SelType); | ||||||||||
2461 | |||||||||||
2462 | // select C, -1, 0 -> sext C to int | ||||||||||
2463 | if (match(TrueVal, m_AllOnes()) && match(FalseVal, m_Zero())) | ||||||||||
2464 | return new SExtInst(CondVal, SelType); | ||||||||||
2465 | |||||||||||
2466 | // select C, 0, 1 -> zext !C to int | ||||||||||
2467 | if (match(TrueVal, m_Zero()) && match(FalseVal, m_One())) { | ||||||||||
2468 | Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName()); | ||||||||||
2469 | return new ZExtInst(NotCond, SelType); | ||||||||||
2470 | } | ||||||||||
2471 | |||||||||||
2472 | // select C, 0, -1 -> sext !C to int | ||||||||||
2473 | if (match(TrueVal, m_Zero()) && match(FalseVal, m_AllOnes())) { | ||||||||||
2474 | Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName()); | ||||||||||
2475 | return new SExtInst(NotCond, SelType); | ||||||||||
2476 | } | ||||||||||
2477 | } | ||||||||||
2478 | |||||||||||
2479 | // See if we are selecting two values based on a comparison of the two values. | ||||||||||
2480 | if (FCmpInst *FCI
| ||||||||||
2481 | Value *Cmp0 = FCI->getOperand(0), *Cmp1 = FCI->getOperand(1); | ||||||||||
2482 | if ((Cmp0 == TrueVal && Cmp1 == FalseVal) || | ||||||||||
2483 | (Cmp0 == FalseVal && Cmp1 == TrueVal)) { | ||||||||||
2484 | // Canonicalize to use ordered comparisons by swapping the select | ||||||||||
2485 | // operands. | ||||||||||
2486 | // | ||||||||||
2487 | // e.g. | ||||||||||
2488 | // (X ugt Y) ? X : Y -> (X ole Y) ? Y : X | ||||||||||
2489 | if (FCI->hasOneUse() && FCmpInst::isUnordered(FCI->getPredicate())) { | ||||||||||
2490 | FCmpInst::Predicate InvPred = FCI->getInversePredicate(); | ||||||||||
2491 | IRBuilder<>::FastMathFlagGuard FMFG(Builder); | ||||||||||
2492 | // FIXME: The FMF should propagate from the select, not the fcmp. | ||||||||||
2493 | Builder.setFastMathFlags(FCI->getFastMathFlags()); | ||||||||||
2494 | Value *NewCond = Builder.CreateFCmp(InvPred, Cmp0, Cmp1, | ||||||||||
2495 | FCI->getName() + ".inv"); | ||||||||||
2496 | Value *NewSel = Builder.CreateSelect(NewCond, FalseVal, TrueVal); | ||||||||||
2497 | return replaceInstUsesWith(SI, NewSel); | ||||||||||
2498 | } | ||||||||||
2499 | |||||||||||
2500 | // NOTE: if we wanted to, this is where to detect MIN/MAX | ||||||||||
2501 | } | ||||||||||
2502 | } | ||||||||||
2503 | |||||||||||
2504 | // Canonicalize select with fcmp to fabs(). -0.0 makes this tricky. We need | ||||||||||
2505 | // fast-math-flags (nsz) or fsub with +0.0 (not fneg) for this to work. We | ||||||||||
2506 | // also require nnan because we do not want to unintentionally change the | ||||||||||
2507 | // sign of a NaN value. | ||||||||||
2508 | // FIXME: These folds should test/propagate FMF from the select, not the | ||||||||||
2509 | // fsub or fneg. | ||||||||||
2510 | // (X <= +/-0.0) ? (0.0 - X) : X --> fabs(X) | ||||||||||
2511 | Instruction *FSub; | ||||||||||
2512 | if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) && | ||||||||||
2513 | match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(FalseVal))) && | ||||||||||
2514 | match(TrueVal, m_Instruction(FSub)) && FSub->hasNoNaNs() && | ||||||||||
2515 | (Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)) { | ||||||||||
2516 | Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, FSub); | ||||||||||
2517 | return replaceInstUsesWith(SI, Fabs); | ||||||||||
2518 | } | ||||||||||
2519 | // (X > +/-0.0) ? X : (0.0 - X) --> fabs(X) | ||||||||||
2520 | if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) && | ||||||||||
2521 | match(FalseVal, m_FSub(m_PosZeroFP(), m_Specific(TrueVal))) && | ||||||||||
2522 | match(FalseVal, m_Instruction(FSub)) && FSub->hasNoNaNs() && | ||||||||||
2523 | (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_UGT)) { | ||||||||||
2524 | Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, FSub); | ||||||||||
2525 | return replaceInstUsesWith(SI, Fabs); | ||||||||||
2526 | } | ||||||||||
2527 | // With nnan and nsz: | ||||||||||
2528 | // (X < +/-0.0) ? -X : X --> fabs(X) | ||||||||||
2529 | // (X <= +/-0.0) ? -X : X --> fabs(X) | ||||||||||
2530 | Instruction *FNeg; | ||||||||||
2531 | if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) && | ||||||||||
2532 | match(TrueVal, m_FNeg(m_Specific(FalseVal))) && | ||||||||||
2533 | match(TrueVal, m_Instruction(FNeg)) && | ||||||||||
2534 | FNeg->hasNoNaNs() && FNeg->hasNoSignedZeros() && | ||||||||||
2535 | (Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_OLE || | ||||||||||
2536 | Pred == FCmpInst::FCMP_ULT || Pred == FCmpInst::FCMP_ULE)) { | ||||||||||
2537 | Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, FNeg); | ||||||||||
2538 | return replaceInstUsesWith(SI, Fabs); | ||||||||||
2539 | } | ||||||||||
2540 | // With nnan and nsz: | ||||||||||
2541 | // (X > +/-0.0) ? X : -X --> fabs(X) | ||||||||||
2542 | // (X >= +/-0.0) ? X : -X --> fabs(X) | ||||||||||
2543 | if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) && | ||||||||||
2544 | match(FalseVal, m_FNeg(m_Specific(TrueVal))) && | ||||||||||
2545 | match(FalseVal, m_Instruction(FNeg)) && | ||||||||||
2546 | FNeg->hasNoNaNs() && FNeg->hasNoSignedZeros() && | ||||||||||
2547 | (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OGE || | ||||||||||
2548 | Pred == FCmpInst::FCMP_UGT || Pred == FCmpInst::FCMP_UGE)) { | ||||||||||
2549 | Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, FNeg); | ||||||||||
2550 | return replaceInstUsesWith(SI, Fabs); | ||||||||||
2551 | } | ||||||||||
2552 | |||||||||||
2553 | // See if we are selecting two values based on a comparison of the two values. | ||||||||||
2554 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) | ||||||||||
2555 | if (Instruction *Result = foldSelectInstWithICmp(SI, ICI)) | ||||||||||
2556 | return Result; | ||||||||||
2557 | |||||||||||
2558 | if (Instruction *Add = foldAddSubSelect(SI, Builder)) | ||||||||||
2559 | return Add; | ||||||||||
2560 | if (Instruction *Add = foldOverflowingAddSubSelect(SI, Builder)) | ||||||||||
2561 | return Add; | ||||||||||
2562 | |||||||||||
2563 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) | ||||||||||
2564 | auto *TI = dyn_cast<Instruction>(TrueVal); | ||||||||||
2565 | auto *FI = dyn_cast<Instruction>(FalseVal); | ||||||||||
2566 | if (TI && FI && TI->getOpcode() == FI->getOpcode()) | ||||||||||
2567 | if (Instruction *IV = foldSelectOpOp(SI, TI, FI)) | ||||||||||
2568 | return IV; | ||||||||||
2569 | |||||||||||
2570 | if (Instruction *I = foldSelectExtConst(SI)) | ||||||||||
2571 | return I; | ||||||||||
2572 | |||||||||||
2573 | // See if we can fold the select into one of our operands. | ||||||||||
2574 | if (SelType->isIntOrIntVectorTy() || SelType->isFPOrFPVectorTy()) { | ||||||||||
2575 | if (Instruction *FoldI = foldSelectIntoOp(SI, TrueVal, FalseVal)) | ||||||||||
2576 | return FoldI; | ||||||||||
2577 | |||||||||||
2578 | Value *LHS, *RHS; | ||||||||||
2579 | Instruction::CastOps CastOp; | ||||||||||
2580 | SelectPatternResult SPR = matchSelectPattern(&SI, LHS, RHS, &CastOp); | ||||||||||
2581 | auto SPF = SPR.Flavor; | ||||||||||
2582 | if (SPF) { | ||||||||||
2583 | Value *LHS2, *RHS2; | ||||||||||
2584 | if (SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor) | ||||||||||
2585 | if (Instruction *R = foldSPFofSPF(cast<Instruction>(LHS), SPF2, LHS2, | ||||||||||
2586 | RHS2, SI, SPF, RHS)) | ||||||||||
2587 | return R; | ||||||||||
2588 | if (SelectPatternFlavor SPF2 = matchSelectPattern(RHS, LHS2, RHS2).Flavor) | ||||||||||
2589 | if (Instruction *R = foldSPFofSPF(cast<Instruction>(RHS), SPF2, LHS2, | ||||||||||
2590 | RHS2, SI, SPF, LHS)) | ||||||||||
2591 | return R; | ||||||||||
2592 | // TODO. | ||||||||||
2593 | // ABS(-X) -> ABS(X) | ||||||||||
2594 | } | ||||||||||
2595 | |||||||||||
2596 | if (SelectPatternResult::isMinOrMax(SPF)) { | ||||||||||
2597 | // Canonicalize so that | ||||||||||
2598 | // - type casts are outside select patterns. | ||||||||||
2599 | // - float clamp is transformed to min/max pattern | ||||||||||
2600 | |||||||||||
2601 | bool IsCastNeeded = LHS->getType() != SelType; | ||||||||||
2602 | Value *CmpLHS = cast<CmpInst>(CondVal)->getOperand(0); | ||||||||||
2603 | Value *CmpRHS = cast<CmpInst>(CondVal)->getOperand(1); | ||||||||||
2604 | if (IsCastNeeded || | ||||||||||
2605 | (LHS->getType()->isFPOrFPVectorTy() && | ||||||||||
2606 | ((CmpLHS != LHS && CmpLHS != RHS) || | ||||||||||
2607 | (CmpRHS != LHS && CmpRHS != RHS)))) { | ||||||||||
2608 | CmpInst::Predicate MinMaxPred = getMinMaxPred(SPF, SPR.Ordered); | ||||||||||
2609 | |||||||||||
2610 | Value *Cmp; | ||||||||||
2611 | if (CmpInst::isIntPredicate(MinMaxPred)) { | ||||||||||
2612 | Cmp = Builder.CreateICmp(MinMaxPred, LHS, RHS); | ||||||||||
2613 | } else { | ||||||||||
2614 | IRBuilder<>::FastMathFlagGuard FMFG(Builder); | ||||||||||
2615 | auto FMF = | ||||||||||
2616 | cast<FPMathOperator>(SI.getCondition())->getFastMathFlags(); | ||||||||||
2617 | Builder.setFastMathFlags(FMF); | ||||||||||
2618 | Cmp = Builder.CreateFCmp(MinMaxPred, LHS, RHS); | ||||||||||
2619 | } | ||||||||||
2620 | |||||||||||
2621 | Value *NewSI = Builder.CreateSelect(Cmp, LHS, RHS, SI.getName(), &SI); | ||||||||||
2622 | if (!IsCastNeeded) | ||||||||||
2623 | return replaceInstUsesWith(SI, NewSI); | ||||||||||
2624 | |||||||||||
2625 | Value *NewCast = Builder.CreateCast(CastOp, NewSI, SelType); | ||||||||||
2626 | return replaceInstUsesWith(SI, NewCast); | ||||||||||
2627 | } | ||||||||||
2628 | |||||||||||
2629 | // MAX(~a, ~b) -> ~MIN(a, b) | ||||||||||
2630 | // MAX(~a, C) -> ~MIN(a, ~C) | ||||||||||
2631 | // MIN(~a, ~b) -> ~MAX(a, b) | ||||||||||
2632 | // MIN(~a, C) -> ~MAX(a, ~C) | ||||||||||
2633 | auto moveNotAfterMinMax = [&](Value *X, Value *Y) -> Instruction * { | ||||||||||
2634 | Value *A; | ||||||||||
2635 | if (match(X, m_Not(m_Value(A))) && !X->hasNUsesOrMore(3) && | ||||||||||
2636 | !isFreeToInvert(A, A->hasOneUse()) && | ||||||||||
2637 | // Passing false to only consider m_Not and constants. | ||||||||||
2638 | isFreeToInvert(Y, false)) { | ||||||||||
2639 | Value *B = Builder.CreateNot(Y); | ||||||||||
2640 | Value *NewMinMax = createMinMax(Builder, getInverseMinMaxFlavor(SPF), | ||||||||||
2641 | A, B); | ||||||||||
2642 | // Copy the profile metadata. | ||||||||||
2643 | if (MDNode *MD = SI.getMetadata(LLVMContext::MD_prof)) { | ||||||||||
2644 | cast<SelectInst>(NewMinMax)->setMetadata(LLVMContext::MD_prof, MD); | ||||||||||
2645 | // Swap the metadata if the operands are swapped. | ||||||||||
2646 | if (X == SI.getFalseValue() && Y == SI.getTrueValue()) | ||||||||||
2647 | cast<SelectInst>(NewMinMax)->swapProfMetadata(); | ||||||||||
2648 | } | ||||||||||
2649 | |||||||||||
2650 | return BinaryOperator::CreateNot(NewMinMax); | ||||||||||
2651 | } | ||||||||||
2652 | |||||||||||
2653 | return nullptr; | ||||||||||
2654 | }; | ||||||||||
2655 | |||||||||||
2656 | if (Instruction *I = moveNotAfterMinMax(LHS, RHS)) | ||||||||||
2657 | return I; | ||||||||||
2658 | if (Instruction *I = moveNotAfterMinMax(RHS, LHS)) | ||||||||||
2659 | return I; | ||||||||||
2660 | |||||||||||
2661 | if (Instruction *I = moveAddAfterMinMax(SPF, LHS, RHS, Builder)) | ||||||||||
2662 | return I; | ||||||||||
2663 | |||||||||||
2664 | if (Instruction *I = factorizeMinMaxTree(SPF, LHS, RHS, Builder)) | ||||||||||
2665 | return I; | ||||||||||
2666 | if (Instruction *I = matchSAddSubSat(SI)) | ||||||||||
2667 | return I; | ||||||||||
2668 | } | ||||||||||
2669 | } | ||||||||||
2670 | |||||||||||
2671 | // Canonicalize select of FP values where NaN and -0.0 are not valid as | ||||||||||
2672 | // minnum/maxnum intrinsics. | ||||||||||
2673 | if (isa<FPMathOperator>(SI) && SI.hasNoNaNs() && SI.hasNoSignedZeros()) { | ||||||||||
2674 | Value *X, *Y; | ||||||||||
2675 | if (match(&SI, m_OrdFMax(m_Value(X), m_Value(Y)))) | ||||||||||
2676 | return replaceInstUsesWith( | ||||||||||
2677 | SI, Builder.CreateBinaryIntrinsic(Intrinsic::maxnum, X, Y, &SI)); | ||||||||||
2678 | |||||||||||
2679 | if (match(&SI, m_OrdFMin(m_Value(X), m_Value(Y)))) | ||||||||||
2680 | return replaceInstUsesWith( | ||||||||||
2681 | SI, Builder.CreateBinaryIntrinsic(Intrinsic::minnum, X, Y, &SI)); | ||||||||||
2682 | } | ||||||||||
2683 | |||||||||||
2684 | // See if we can fold the select into a phi node if the condition is a select. | ||||||||||
2685 | if (auto *PN = dyn_cast<PHINode>(SI.getCondition())) | ||||||||||
2686 | // The true/false values have to be live in the PHI predecessor's blocks. | ||||||||||
2687 | if (canSelectOperandBeMappingIntoPredBlock(TrueVal, SI) && | ||||||||||
2688 | canSelectOperandBeMappingIntoPredBlock(FalseVal, SI)) | ||||||||||
2689 | if (Instruction *NV = foldOpIntoPhi(SI, PN)) | ||||||||||
2690 | return NV; | ||||||||||
2691 | |||||||||||
2692 | if (SelectInst *TrueSI = dyn_cast<SelectInst>(TrueVal)) { | ||||||||||
2693 | if (TrueSI->getCondition()->getType() == CondVal->getType()) { | ||||||||||
2694 | // select(C, select(C, a, b), c) -> select(C, a, c) | ||||||||||
2695 | if (TrueSI->getCondition() == CondVal) { | ||||||||||
2696 | if (SI.getTrueValue() == TrueSI->getTrueValue()) | ||||||||||
2697 | return nullptr; | ||||||||||
2698 | return replaceOperand(SI, 1, TrueSI->getTrueValue()); | ||||||||||
2699 | } | ||||||||||
2700 | // select(C0, select(C1, a, b), b) -> select(C0&C1, a, b) | ||||||||||
2701 | // We choose this as normal form to enable folding on the And and shortening | ||||||||||
2702 | // paths for the values (this helps GetUnderlyingObjects() for example). | ||||||||||
2703 | if (TrueSI->getFalseValue() == FalseVal && TrueSI->hasOneUse()) { | ||||||||||
2704 | Value *And = Builder.CreateAnd(CondVal, TrueSI->getCondition()); | ||||||||||
2705 | SI.setOperand(0, And); | ||||||||||
2706 | SI.setOperand(1, TrueSI->getTrueValue()); | ||||||||||
2707 | return &SI; | ||||||||||
2708 | } | ||||||||||
2709 | } | ||||||||||
2710 | } | ||||||||||
2711 | if (SelectInst *FalseSI = dyn_cast<SelectInst>(FalseVal)) { | ||||||||||
2712 | if (FalseSI->getCondition()->getType() == CondVal->getType()) { | ||||||||||
2713 | // select(C, a, select(C, b, c)) -> select(C, a, c) | ||||||||||
2714 | if (FalseSI->getCondition() == CondVal) { | ||||||||||
2715 | if (SI.getFalseValue() == FalseSI->getFalseValue()) | ||||||||||
2716 | return nullptr; | ||||||||||
2717 | return replaceOperand(SI, 2, FalseSI->getFalseValue()); | ||||||||||
2718 | } | ||||||||||
2719 | // select(C0, a, select(C1, a, b)) -> select(C0|C1, a, b) | ||||||||||
2720 | if (FalseSI->getTrueValue() == TrueVal && FalseSI->hasOneUse()) { | ||||||||||
2721 | Value *Or = Builder.CreateOr(CondVal, FalseSI->getCondition()); | ||||||||||
2722 | SI.setOperand(0, Or); | ||||||||||
2723 | SI.setOperand(2, FalseSI->getFalseValue()); | ||||||||||
2724 | return &SI; | ||||||||||
2725 | } | ||||||||||
2726 | } | ||||||||||
2727 | } | ||||||||||
2728 | |||||||||||
2729 | auto canMergeSelectThroughBinop = [](BinaryOperator *BO) { | ||||||||||
2730 | // The select might be preventing a division by 0. | ||||||||||
2731 | switch (BO->getOpcode()) { | ||||||||||
2732 | default: | ||||||||||
2733 | return true; | ||||||||||
2734 | case Instruction::SRem: | ||||||||||
2735 | case Instruction::URem: | ||||||||||
2736 | case Instruction::SDiv: | ||||||||||
2737 | case Instruction::UDiv: | ||||||||||
2738 | return false; | ||||||||||
2739 | } | ||||||||||
2740 | }; | ||||||||||
2741 | |||||||||||
2742 | // Try to simplify a binop sandwiched between 2 selects with the same | ||||||||||
2743 | // condition. | ||||||||||
2744 | // select(C, binop(select(C, X, Y), W), Z) -> select(C, binop(X, W), Z) | ||||||||||
2745 | BinaryOperator *TrueBO; | ||||||||||
2746 | if (match(TrueVal, m_OneUse(m_BinOp(TrueBO))) && | ||||||||||
2747 | canMergeSelectThroughBinop(TrueBO)) { | ||||||||||
2748 | if (auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->getOperand(0))) { | ||||||||||
2749 | if (TrueBOSI->getCondition() == CondVal) { | ||||||||||
2750 | TrueBO->setOperand(0, TrueBOSI->getTrueValue()); | ||||||||||
2751 | Worklist.push(TrueBO); | ||||||||||
2752 | return &SI; | ||||||||||
2753 | } | ||||||||||
2754 | } | ||||||||||
2755 | if (auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->getOperand(1))) { | ||||||||||
2756 | if (TrueBOSI->getCondition() == CondVal) { | ||||||||||
2757 | TrueBO->setOperand(1, TrueBOSI->getTrueValue()); | ||||||||||
2758 | Worklist.push(TrueBO); | ||||||||||
2759 | return &SI; | ||||||||||
2760 | } | ||||||||||
2761 | } | ||||||||||
2762 | } | ||||||||||
2763 | |||||||||||
2764 | // select(C, Z, binop(select(C, X, Y), W)) -> select(C, Z, binop(Y, W)) | ||||||||||
2765 | BinaryOperator *FalseBO; | ||||||||||
2766 | if (match(FalseVal, m_OneUse(m_BinOp(FalseBO))) && | ||||||||||
2767 | canMergeSelectThroughBinop(FalseBO)) { | ||||||||||
2768 | if (auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->getOperand(0))) { | ||||||||||
2769 | if (FalseBOSI->getCondition() == CondVal) { | ||||||||||
2770 | FalseBO->setOperand(0, FalseBOSI->getFalseValue()); | ||||||||||
2771 | Worklist.push(FalseBO); | ||||||||||
2772 | return &SI; | ||||||||||
2773 | } | ||||||||||
2774 | } | ||||||||||
2775 | if (auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->getOperand(1))) { | ||||||||||
2776 | if (FalseBOSI->getCondition() == CondVal) { | ||||||||||
2777 | FalseBO->setOperand(1, FalseBOSI->getFalseValue()); | ||||||||||
2778 | Worklist.push(FalseBO); | ||||||||||
2779 | return &SI; | ||||||||||
2780 | } | ||||||||||
2781 | } | ||||||||||
2782 | } | ||||||||||
2783 | |||||||||||
2784 | Value *NotCond; | ||||||||||
2785 | if (match(CondVal, m_Not(m_Value(NotCond)))) { | ||||||||||
2786 | replaceOperand(SI, 0, NotCond); | ||||||||||
2787 | SI.swapValues(); | ||||||||||
2788 | SI.swapProfMetadata(); | ||||||||||
2789 | return &SI; | ||||||||||
2790 | } | ||||||||||
2791 | |||||||||||
2792 | if (VectorType *VecTy = dyn_cast<VectorType>(SelType)) { | ||||||||||
2793 | unsigned VWidth = VecTy->getNumElements(); | ||||||||||
2794 | APInt UndefElts(VWidth, 0); | ||||||||||
2795 | APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); | ||||||||||
2796 | if (Value *V = SimplifyDemandedVectorElts(&SI, AllOnesEltMask, UndefElts)) { | ||||||||||
2797 | if (V != &SI) | ||||||||||
2798 | return replaceInstUsesWith(SI, V); | ||||||||||
2799 | return &SI; | ||||||||||
2800 | } | ||||||||||
2801 | } | ||||||||||
2802 | |||||||||||
2803 | // If we can compute the condition, there's no need for a select. | ||||||||||
2804 | // Like the above fold, we are attempting to reduce compile-time cost by | ||||||||||
2805 | // putting this fold here with limitations rather than in InstSimplify. | ||||||||||
2806 | // The motivation for this call into value tracking is to take advantage of | ||||||||||
2807 | // the assumption cache, so make sure that is populated. | ||||||||||
2808 | if (!CondVal->getType()->isVectorTy() && !AC.assumptions().empty()) { | ||||||||||
2809 | KnownBits Known(1); | ||||||||||
2810 | computeKnownBits(CondVal, Known, 0, &SI); | ||||||||||
2811 | if (Known.One.isOneValue()) | ||||||||||
2812 | return replaceInstUsesWith(SI, TrueVal); | ||||||||||
2813 | if (Known.Zero.isOneValue()) | ||||||||||
2814 | return replaceInstUsesWith(SI, FalseVal); | ||||||||||
2815 | } | ||||||||||
2816 | |||||||||||
2817 | if (Instruction *BitCastSel = foldSelectCmpBitcasts(SI, Builder)) | ||||||||||
2818 | return BitCastSel; | ||||||||||
2819 | |||||||||||
2820 | // Simplify selects that test the returned flag of cmpxchg instructions. | ||||||||||
2821 | if (Instruction *Select = foldSelectCmpXchg(SI)) | ||||||||||
2822 | return Select; | ||||||||||
2823 | |||||||||||
2824 | if (Instruction *Select = foldSelectBinOpIdentity(SI, TLI, *this)) | ||||||||||
2825 | return Select; | ||||||||||
2826 | |||||||||||
2827 | if (Instruction *Rot = foldSelectRotate(SI)) | ||||||||||
2828 | return Rot; | ||||||||||
2829 | |||||||||||
2830 | if (Instruction *Copysign = foldSelectToCopysign(SI, Builder)) | ||||||||||
2831 | return Copysign; | ||||||||||
2832 | |||||||||||
2833 | return nullptr; | ||||||||||
2834 | } |
1 | //===- llvm/Type.h - Classes for handling data types ------------*- 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 contains the declaration of the Type class. For more "Type" |
10 | // stuff, look in DerivedTypes.h. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #ifndef LLVM_IR_TYPE_H |
15 | #define LLVM_IR_TYPE_H |
16 | |
17 | #include "llvm/ADT/APFloat.h" |
18 | #include "llvm/ADT/ArrayRef.h" |
19 | #include "llvm/ADT/SmallPtrSet.h" |
20 | #include "llvm/Support/CBindingWrapping.h" |
21 | #include "llvm/Support/Casting.h" |
22 | #include "llvm/Support/Compiler.h" |
23 | #include "llvm/Support/ErrorHandling.h" |
24 | #include "llvm/Support/TypeSize.h" |
25 | #include <cassert> |
26 | #include <cstdint> |
27 | #include <iterator> |
28 | |
29 | namespace llvm { |
30 | |
31 | template<class GraphType> struct GraphTraits; |
32 | class IntegerType; |
33 | class LLVMContext; |
34 | class PointerType; |
35 | class raw_ostream; |
36 | class StringRef; |
37 | |
38 | /// The instances of the Type class are immutable: once they are created, |
39 | /// they are never changed. Also note that only one instance of a particular |
40 | /// type is ever created. Thus seeing if two types are equal is a matter of |
41 | /// doing a trivial pointer comparison. To enforce that no two equal instances |
42 | /// are created, Type instances can only be created via static factory methods |
43 | /// in class Type and in derived classes. Once allocated, Types are never |
44 | /// free'd. |
45 | /// |
46 | class Type { |
47 | public: |
48 | //===--------------------------------------------------------------------===// |
49 | /// Definitions of all of the base types for the Type system. Based on this |
50 | /// value, you can cast to a class defined in DerivedTypes.h. |
51 | /// Note: If you add an element to this, you need to add an element to the |
52 | /// Type::getPrimitiveType function, or else things will break! |
53 | /// Also update LLVMTypeKind and LLVMGetTypeKind () in the C binding. |
54 | /// |
55 | enum TypeID { |
56 | // PrimitiveTypes - make sure LastPrimitiveTyID stays up to date. |
57 | VoidTyID = 0, ///< 0: type with no size |
58 | HalfTyID, ///< 1: 16-bit floating point type |
59 | FloatTyID, ///< 2: 32-bit floating point type |
60 | DoubleTyID, ///< 3: 64-bit floating point type |
61 | X86_FP80TyID, ///< 4: 80-bit floating point type (X87) |
62 | FP128TyID, ///< 5: 128-bit floating point type (112-bit mantissa) |
63 | PPC_FP128TyID, ///< 6: 128-bit floating point type (two 64-bits, PowerPC) |
64 | LabelTyID, ///< 7: Labels |
65 | MetadataTyID, ///< 8: Metadata |
66 | X86_MMXTyID, ///< 9: MMX vectors (64 bits, X86 specific) |
67 | TokenTyID, ///< 10: Tokens |
68 | |
69 | // Derived types... see DerivedTypes.h file. |
70 | // Make sure FirstDerivedTyID stays up to date! |
71 | IntegerTyID, ///< 11: Arbitrary bit width integers |
72 | FunctionTyID, ///< 12: Functions |
73 | StructTyID, ///< 13: Structures |
74 | ArrayTyID, ///< 14: Arrays |
75 | PointerTyID, ///< 15: Pointers |
76 | VectorTyID ///< 16: SIMD 'packed' format, or other vector type |
77 | }; |
78 | |
79 | private: |
80 | /// This refers to the LLVMContext in which this type was uniqued. |
81 | LLVMContext &Context; |
82 | |
83 | TypeID ID : 8; // The current base type of this type. |
84 | unsigned SubclassData : 24; // Space for subclasses to store data. |
85 | // Note that this should be synchronized with |
86 | // MAX_INT_BITS value in IntegerType class. |
87 | |
88 | protected: |
89 | friend class LLVMContextImpl; |
90 | |
91 | explicit Type(LLVMContext &C, TypeID tid) |
92 | : Context(C), ID(tid), SubclassData(0) {} |
93 | ~Type() = default; |
94 | |
95 | unsigned getSubclassData() const { return SubclassData; } |
96 | |
97 | void setSubclassData(unsigned val) { |
98 | SubclassData = val; |
99 | // Ensure we don't have any accidental truncation. |
100 | assert(getSubclassData() == val && "Subclass data too large for field")((getSubclassData() == val && "Subclass data too large for field" ) ? static_cast<void> (0) : __assert_fail ("getSubclassData() == val && \"Subclass data too large for field\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Type.h" , 100, __PRETTY_FUNCTION__)); |
101 | } |
102 | |
103 | /// Keeps track of how many Type*'s there are in the ContainedTys list. |
104 | unsigned NumContainedTys = 0; |
105 | |
106 | /// A pointer to the array of Types contained by this Type. For example, this |
107 | /// includes the arguments of a function type, the elements of a structure, |
108 | /// the pointee of a pointer, the element type of an array, etc. This pointer |
109 | /// may be 0 for types that don't contain other types (Integer, Double, |
110 | /// Float). |
111 | Type * const *ContainedTys = nullptr; |
112 | |
113 | static bool isSequentialType(TypeID TyID) { |
114 | return TyID == ArrayTyID || TyID == VectorTyID; |
115 | } |
116 | |
117 | public: |
118 | /// Print the current type. |
119 | /// Omit the type details if \p NoDetails == true. |
120 | /// E.g., let %st = type { i32, i16 } |
121 | /// When \p NoDetails is true, we only print %st. |
122 | /// Put differently, \p NoDetails prints the type as if |
123 | /// inlined with the operands when printing an instruction. |
124 | void print(raw_ostream &O, bool IsForDebug = false, |
125 | bool NoDetails = false) const; |
126 | |
127 | void dump() const; |
128 | |
129 | /// Return the LLVMContext in which this type was uniqued. |
130 | LLVMContext &getContext() const { return Context; } |
131 | |
132 | //===--------------------------------------------------------------------===// |
133 | // Accessors for working with types. |
134 | // |
135 | |
136 | /// Return the type id for the type. This will return one of the TypeID enum |
137 | /// elements defined above. |
138 | TypeID getTypeID() const { return ID; } |
139 | |
140 | /// Return true if this is 'void'. |
141 | bool isVoidTy() const { return getTypeID() == VoidTyID; } |
142 | |
143 | /// Return true if this is 'half', a 16-bit IEEE fp type. |
144 | bool isHalfTy() const { return getTypeID() == HalfTyID; } |
145 | |
146 | /// Return true if this is 'float', a 32-bit IEEE fp type. |
147 | bool isFloatTy() const { return getTypeID() == FloatTyID; } |
148 | |
149 | /// Return true if this is 'double', a 64-bit IEEE fp type. |
150 | bool isDoubleTy() const { return getTypeID() == DoubleTyID; } |
151 | |
152 | /// Return true if this is x86 long double. |
153 | bool isX86_FP80Ty() const { return getTypeID() == X86_FP80TyID; } |
154 | |
155 | /// Return true if this is 'fp128'. |
156 | bool isFP128Ty() const { return getTypeID() == FP128TyID; } |
157 | |
158 | /// Return true if this is powerpc long double. |
159 | bool isPPC_FP128Ty() const { return getTypeID() == PPC_FP128TyID; } |
160 | |
161 | /// Return true if this is one of the six floating-point types |
162 | bool isFloatingPointTy() const { |
163 | return getTypeID() == HalfTyID || getTypeID() == FloatTyID || |
164 | getTypeID() == DoubleTyID || |
165 | getTypeID() == X86_FP80TyID || getTypeID() == FP128TyID || |
166 | getTypeID() == PPC_FP128TyID; |
167 | } |
168 | |
169 | const fltSemantics &getFltSemantics() const { |
170 | switch (getTypeID()) { |
171 | case HalfTyID: return APFloat::IEEEhalf(); |
172 | case FloatTyID: return APFloat::IEEEsingle(); |
173 | case DoubleTyID: return APFloat::IEEEdouble(); |
174 | case X86_FP80TyID: return APFloat::x87DoubleExtended(); |
175 | case FP128TyID: return APFloat::IEEEquad(); |
176 | case PPC_FP128TyID: return APFloat::PPCDoubleDouble(); |
177 | default: llvm_unreachable("Invalid floating type")::llvm::llvm_unreachable_internal("Invalid floating type", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Type.h" , 177); |
178 | } |
179 | } |
180 | |
181 | /// Return true if this is X86 MMX. |
182 | bool isX86_MMXTy() const { return getTypeID() == X86_MMXTyID; } |
183 | |
184 | /// Return true if this is a FP type or a vector of FP. |
185 | bool isFPOrFPVectorTy() const { return getScalarType()->isFloatingPointTy(); } |
186 | |
187 | /// Return true if this is 'label'. |
188 | bool isLabelTy() const { return getTypeID() == LabelTyID; } |
189 | |
190 | /// Return true if this is 'metadata'. |
191 | bool isMetadataTy() const { return getTypeID() == MetadataTyID; } |
192 | |
193 | /// Return true if this is 'token'. |
194 | bool isTokenTy() const { return getTypeID() == TokenTyID; } |
195 | |
196 | /// True if this is an instance of IntegerType. |
197 | bool isIntegerTy() const { return getTypeID() == IntegerTyID; } |
198 | |
199 | /// Return true if this is an IntegerType of the given width. |
200 | bool isIntegerTy(unsigned Bitwidth) const; |
201 | |
202 | /// Return true if this is an integer type or a vector of integer types. |
203 | bool isIntOrIntVectorTy() const { return getScalarType()->isIntegerTy(); } |
204 | |
205 | /// Return true if this is an integer type or a vector of integer types of |
206 | /// the given width. |
207 | bool isIntOrIntVectorTy(unsigned BitWidth) const { |
208 | return getScalarType()->isIntegerTy(BitWidth); |
209 | } |
210 | |
211 | /// Return true if this is an integer type or a pointer type. |
212 | bool isIntOrPtrTy() const { return isIntegerTy() || isPointerTy(); } |
213 | |
214 | /// True if this is an instance of FunctionType. |
215 | bool isFunctionTy() const { return getTypeID() == FunctionTyID; } |
216 | |
217 | /// True if this is an instance of StructType. |
218 | bool isStructTy() const { return getTypeID() == StructTyID; } |
219 | |
220 | /// True if this is an instance of ArrayType. |
221 | bool isArrayTy() const { return getTypeID() == ArrayTyID; } |
222 | |
223 | /// True if this is an instance of PointerType. |
224 | bool isPointerTy() const { return getTypeID() == PointerTyID; } |
225 | |
226 | /// Return true if this is a pointer type or a vector of pointer types. |
227 | bool isPtrOrPtrVectorTy() const { return getScalarType()->isPointerTy(); } |
228 | |
229 | /// True if this is an instance of VectorType. |
230 | bool isVectorTy() const { return getTypeID() == VectorTyID; } |
231 | |
232 | /// Return true if this type could be converted with a lossless BitCast to |
233 | /// type 'Ty'. For example, i8* to i32*. BitCasts are valid for types of the |
234 | /// same size only where no re-interpretation of the bits is done. |
235 | /// Determine if this type could be losslessly bitcast to Ty |
236 | bool canLosslesslyBitCastTo(Type *Ty) const; |
237 | |
238 | /// Return true if this type is empty, that is, it has no elements or all of |
239 | /// its elements are empty. |
240 | bool isEmptyTy() const; |
241 | |
242 | /// Return true if the type is "first class", meaning it is a valid type for a |
243 | /// Value. |
244 | bool isFirstClassType() const { |
245 | return getTypeID() != FunctionTyID && getTypeID() != VoidTyID; |
246 | } |
247 | |
248 | /// Return true if the type is a valid type for a register in codegen. This |
249 | /// includes all first-class types except struct and array types. |
250 | bool isSingleValueType() const { |
251 | return isFloatingPointTy() || isX86_MMXTy() || isIntegerTy() || |
252 | isPointerTy() || isVectorTy(); |
253 | } |
254 | |
255 | /// Return true if the type is an aggregate type. This means it is valid as |
256 | /// the first operand of an insertvalue or extractvalue instruction. This |
257 | /// includes struct and array types, but does not include vector types. |
258 | bool isAggregateType() const { |
259 | return getTypeID() == StructTyID || getTypeID() == ArrayTyID; |
260 | } |
261 | |
262 | /// Return true if it makes sense to take the size of this type. To get the |
263 | /// actual size for a particular target, it is reasonable to use the |
264 | /// DataLayout subsystem to do this. |
265 | bool isSized(SmallPtrSetImpl<Type*> *Visited = nullptr) const { |
266 | // If it's a primitive, it is always sized. |
267 | if (getTypeID() == IntegerTyID || isFloatingPointTy() || |
268 | getTypeID() == PointerTyID || |
269 | getTypeID() == X86_MMXTyID) |
270 | return true; |
271 | // If it is not something that can have a size (e.g. a function or label), |
272 | // it doesn't have a size. |
273 | if (getTypeID() != StructTyID && getTypeID() != ArrayTyID && |
274 | getTypeID() != VectorTyID) |
275 | return false; |
276 | // Otherwise we have to try harder to decide. |
277 | return isSizedDerivedType(Visited); |
278 | } |
279 | |
280 | /// Return the basic size of this type if it is a primitive type. These are |
281 | /// fixed by LLVM and are not target-dependent. |
282 | /// This will return zero if the type does not have a size or is not a |
283 | /// primitive type. |
284 | /// |
285 | /// If this is a scalable vector type, the scalable property will be set and |
286 | /// the runtime size will be a positive integer multiple of the base size. |
287 | /// |
288 | /// Note that this may not reflect the size of memory allocated for an |
289 | /// instance of the type or the number of bytes that are written when an |
290 | /// instance of the type is stored to memory. The DataLayout class provides |
291 | /// additional query functions to provide this information. |
292 | /// |
293 | TypeSize getPrimitiveSizeInBits() const LLVM_READONLY__attribute__((__pure__)); |
294 | |
295 | /// If this is a vector type, return the getPrimitiveSizeInBits value for the |
296 | /// element type. Otherwise return the getPrimitiveSizeInBits value for this |
297 | /// type. |
298 | unsigned getScalarSizeInBits() const LLVM_READONLY__attribute__((__pure__)); |
299 | |
300 | /// Return the width of the mantissa of this type. This is only valid on |
301 | /// floating-point types. If the FP type does not have a stable mantissa (e.g. |
302 | /// ppc long double), this method returns -1. |
303 | int getFPMantissaWidth() const; |
304 | |
305 | /// If this is a vector type, return the element type, otherwise return |
306 | /// 'this'. |
307 | Type *getScalarType() const { |
308 | if (isVectorTy()) |
309 | return getVectorElementType(); |
310 | return const_cast<Type*>(this); |
311 | } |
312 | |
313 | //===--------------------------------------------------------------------===// |
314 | // Type Iteration support. |
315 | // |
316 | using subtype_iterator = Type * const *; |
317 | |
318 | subtype_iterator subtype_begin() const { return ContainedTys; } |
319 | subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];} |
320 | ArrayRef<Type*> subtypes() const { |
321 | return makeArrayRef(subtype_begin(), subtype_end()); |
322 | } |
323 | |
324 | using subtype_reverse_iterator = std::reverse_iterator<subtype_iterator>; |
325 | |
326 | subtype_reverse_iterator subtype_rbegin() const { |
327 | return subtype_reverse_iterator(subtype_end()); |
328 | } |
329 | subtype_reverse_iterator subtype_rend() const { |
330 | return subtype_reverse_iterator(subtype_begin()); |
331 | } |
332 | |
333 | /// This method is used to implement the type iterator (defined at the end of |
334 | /// the file). For derived types, this returns the types 'contained' in the |
335 | /// derived type. |
336 | Type *getContainedType(unsigned i) const { |
337 | assert(i < NumContainedTys && "Index out of range!")((i < NumContainedTys && "Index out of range!") ? static_cast <void> (0) : __assert_fail ("i < NumContainedTys && \"Index out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Type.h" , 337, __PRETTY_FUNCTION__)); |
338 | return ContainedTys[i]; |
339 | } |
340 | |
341 | /// Return the number of types in the derived type. |
342 | unsigned getNumContainedTypes() const { return NumContainedTys; } |
343 | |
344 | //===--------------------------------------------------------------------===// |
345 | // Helper methods corresponding to subclass methods. This forces a cast to |
346 | // the specified subclass and calls its accessor. "getVectorNumElements" (for |
347 | // example) is shorthand for cast<VectorType>(Ty)->getNumElements(). This is |
348 | // only intended to cover the core methods that are frequently used, helper |
349 | // methods should not be added here. |
350 | |
351 | inline unsigned getIntegerBitWidth() const; |
352 | |
353 | inline Type *getFunctionParamType(unsigned i) const; |
354 | inline unsigned getFunctionNumParams() const; |
355 | inline bool isFunctionVarArg() const; |
356 | |
357 | inline StringRef getStructName() const; |
358 | inline unsigned getStructNumElements() const; |
359 | inline Type *getStructElementType(unsigned N) const; |
360 | |
361 | inline Type *getSequentialElementType() const { |
362 | assert(isSequentialType(getTypeID()) && "Not a sequential type!")((isSequentialType(getTypeID()) && "Not a sequential type!" ) ? static_cast<void> (0) : __assert_fail ("isSequentialType(getTypeID()) && \"Not a sequential type!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Type.h" , 362, __PRETTY_FUNCTION__)); |
363 | return ContainedTys[0]; |
364 | } |
365 | |
366 | inline uint64_t getArrayNumElements() const; |
367 | |
368 | Type *getArrayElementType() const { |
369 | assert(getTypeID() == ArrayTyID)((getTypeID() == ArrayTyID) ? static_cast<void> (0) : __assert_fail ("getTypeID() == ArrayTyID", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Type.h" , 369, __PRETTY_FUNCTION__)); |
370 | return ContainedTys[0]; |
371 | } |
372 | |
373 | inline bool getVectorIsScalable() const; |
374 | inline unsigned getVectorNumElements() const; |
375 | inline ElementCount getVectorElementCount() const; |
376 | Type *getVectorElementType() const { |
377 | assert(getTypeID() == VectorTyID)((getTypeID() == VectorTyID) ? static_cast<void> (0) : __assert_fail ("getTypeID() == VectorTyID", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Type.h" , 377, __PRETTY_FUNCTION__)); |
378 | return ContainedTys[0]; |
379 | } |
380 | |
381 | Type *getPointerElementType() const { |
382 | assert(getTypeID() == PointerTyID)((getTypeID() == PointerTyID) ? static_cast<void> (0) : __assert_fail ("getTypeID() == PointerTyID", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Type.h" , 382, __PRETTY_FUNCTION__)); |
383 | return ContainedTys[0]; |
384 | } |
385 | |
386 | /// Given an integer or vector type, change the lane bitwidth to NewBitwidth, |
387 | /// whilst keeping the old number of lanes. |
388 | inline Type *getWithNewBitWidth(unsigned NewBitWidth) const; |
389 | |
390 | /// Given scalar/vector integer type, returns a type with elements twice as |
391 | /// wide as in the original type. For vectors, preserves element count. |
392 | inline Type *getExtendedType() const; |
393 | |
394 | /// Get the address space of this pointer or pointer vector type. |
395 | inline unsigned getPointerAddressSpace() const; |
396 | |
397 | //===--------------------------------------------------------------------===// |
398 | // Static members exported by the Type class itself. Useful for getting |
399 | // instances of Type. |
400 | // |
401 | |
402 | /// Return a type based on an identifier. |
403 | static Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber); |
404 | |
405 | //===--------------------------------------------------------------------===// |
406 | // These are the builtin types that are always available. |
407 | // |
408 | static Type *getVoidTy(LLVMContext &C); |
409 | static Type *getLabelTy(LLVMContext &C); |
410 | static Type *getHalfTy(LLVMContext &C); |
411 | static Type *getFloatTy(LLVMContext &C); |
412 | static Type *getDoubleTy(LLVMContext &C); |
413 | static Type *getMetadataTy(LLVMContext &C); |
414 | static Type *getX86_FP80Ty(LLVMContext &C); |
415 | static Type *getFP128Ty(LLVMContext &C); |
416 | static Type *getPPC_FP128Ty(LLVMContext &C); |
417 | static Type *getX86_MMXTy(LLVMContext &C); |
418 | static Type *getTokenTy(LLVMContext &C); |
419 | static IntegerType *getIntNTy(LLVMContext &C, unsigned N); |
420 | static IntegerType *getInt1Ty(LLVMContext &C); |
421 | static IntegerType *getInt8Ty(LLVMContext &C); |
422 | static IntegerType *getInt16Ty(LLVMContext &C); |
423 | static IntegerType *getInt32Ty(LLVMContext &C); |
424 | static IntegerType *getInt64Ty(LLVMContext &C); |
425 | static IntegerType *getInt128Ty(LLVMContext &C); |
426 | template <typename ScalarTy> static Type *getScalarTy(LLVMContext &C) { |
427 | int noOfBits = sizeof(ScalarTy) * CHAR_BIT8; |
428 | if (std::is_integral<ScalarTy>::value) { |
429 | return (Type*) Type::getIntNTy(C, noOfBits); |
430 | } else if (std::is_floating_point<ScalarTy>::value) { |
431 | switch (noOfBits) { |
432 | case 32: |
433 | return Type::getFloatTy(C); |
434 | case 64: |
435 | return Type::getDoubleTy(C); |
436 | } |
437 | } |
438 | llvm_unreachable("Unsupported type in Type::getScalarTy")::llvm::llvm_unreachable_internal("Unsupported type in Type::getScalarTy" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Type.h" , 438); |
439 | } |
440 | |
441 | //===--------------------------------------------------------------------===// |
442 | // Convenience methods for getting pointer types with one of the above builtin |
443 | // types as pointee. |
444 | // |
445 | static PointerType *getHalfPtrTy(LLVMContext &C, unsigned AS = 0); |
446 | static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0); |
447 | static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0); |
448 | static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0); |
449 | static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0); |
450 | static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0); |
451 | static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0); |
452 | static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0); |
453 | static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0); |
454 | static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0); |
455 | static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0); |
456 | static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0); |
457 | static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0); |
458 | |
459 | /// Return a pointer to the current type. This is equivalent to |
460 | /// PointerType::get(Foo, AddrSpace). |
461 | PointerType *getPointerTo(unsigned AddrSpace = 0) const; |
462 | |
463 | private: |
464 | /// Derived types like structures and arrays are sized iff all of the members |
465 | /// of the type are sized as well. Since asking for their size is relatively |
466 | /// uncommon, move this operation out-of-line. |
467 | bool isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited = nullptr) const; |
468 | }; |
469 | |
470 | // Printing of types. |
471 | inline raw_ostream &operator<<(raw_ostream &OS, const Type &T) { |
472 | T.print(OS); |
473 | return OS; |
474 | } |
475 | |
476 | // allow isa<PointerType>(x) to work without DerivedTypes.h included. |
477 | template <> struct isa_impl<PointerType, Type> { |
478 | static inline bool doit(const Type &Ty) { |
479 | return Ty.getTypeID() == Type::PointerTyID; |
480 | } |
481 | }; |
482 | |
483 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
484 | DEFINE_ISA_CONVERSION_FUNCTIONS(Type, LLVMTypeRef)inline Type *unwrap(LLVMTypeRef P) { return reinterpret_cast< Type*>(P); } inline LLVMTypeRef wrap(const Type *P) { return reinterpret_cast<LLVMTypeRef>(const_cast<Type*>( P)); } template<typename T> inline T *unwrap(LLVMTypeRef P) { return cast<T>(unwrap(P)); } |
485 | |
486 | /* Specialized opaque type conversions. |
487 | */ |
488 | inline Type **unwrap(LLVMTypeRef* Tys) { |
489 | return reinterpret_cast<Type**>(Tys); |
490 | } |
491 | |
492 | inline LLVMTypeRef *wrap(Type **Tys) { |
493 | return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys)); |
494 | } |
495 | |
496 | } // end namespace llvm |
497 | |
498 | #endif // LLVM_IR_TYPE_H |
1 | //===- llvm/Value.h - Definition of the Value class -------------*- 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 declares the Value class. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_IR_VALUE_H |
14 | #define LLVM_IR_VALUE_H |
15 | |
16 | #include "llvm-c/Types.h" |
17 | #include "llvm/ADT/STLExtras.h" |
18 | #include "llvm/ADT/iterator_range.h" |
19 | #include "llvm/IR/Use.h" |
20 | #include "llvm/Support/Alignment.h" |
21 | #include "llvm/Support/CBindingWrapping.h" |
22 | #include "llvm/Support/Casting.h" |
23 | #include <cassert> |
24 | #include <iterator> |
25 | #include <memory> |
26 | |
27 | namespace llvm { |
28 | |
29 | class APInt; |
30 | class Argument; |
31 | class BasicBlock; |
32 | class Constant; |
33 | class ConstantData; |
34 | class ConstantAggregate; |
35 | class DataLayout; |
36 | class Function; |
37 | class GlobalAlias; |
38 | class GlobalIFunc; |
39 | class GlobalIndirectSymbol; |
40 | class GlobalObject; |
41 | class GlobalValue; |
42 | class GlobalVariable; |
43 | class InlineAsm; |
44 | class Instruction; |
45 | class LLVMContext; |
46 | class Module; |
47 | class ModuleSlotTracker; |
48 | class raw_ostream; |
49 | template<typename ValueTy> class StringMapEntry; |
50 | class StringRef; |
51 | class Twine; |
52 | class Type; |
53 | class User; |
54 | |
55 | using ValueName = StringMapEntry<Value *>; |
56 | |
57 | //===----------------------------------------------------------------------===// |
58 | // Value Class |
59 | //===----------------------------------------------------------------------===// |
60 | |
61 | /// LLVM Value Representation |
62 | /// |
63 | /// This is a very important LLVM class. It is the base class of all values |
64 | /// computed by a program that may be used as operands to other values. Value is |
65 | /// the super class of other important classes such as Instruction and Function. |
66 | /// All Values have a Type. Type is not a subclass of Value. Some values can |
67 | /// have a name and they belong to some Module. Setting the name on the Value |
68 | /// automatically updates the module's symbol table. |
69 | /// |
70 | /// Every value has a "use list" that keeps track of which other Values are |
71 | /// using this Value. A Value can also have an arbitrary number of ValueHandle |
72 | /// objects that watch it and listen to RAUW and Destroy events. See |
73 | /// llvm/IR/ValueHandle.h for details. |
74 | class Value { |
75 | // The least-significant bit of the first word of Value *must* be zero: |
76 | // http://www.llvm.org/docs/ProgrammersManual.html#the-waymarking-algorithm |
77 | Type *VTy; |
78 | Use *UseList; |
79 | |
80 | friend class ValueAsMetadata; // Allow access to IsUsedByMD. |
81 | friend class ValueHandleBase; |
82 | |
83 | const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast) |
84 | unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this? |
85 | |
86 | protected: |
87 | /// Hold subclass data that can be dropped. |
88 | /// |
89 | /// This member is similar to SubclassData, however it is for holding |
90 | /// information which may be used to aid optimization, but which may be |
91 | /// cleared to zero without affecting conservative interpretation. |
92 | unsigned char SubclassOptionalData : 7; |
93 | |
94 | private: |
95 | /// Hold arbitrary subclass data. |
96 | /// |
97 | /// This member is defined by this class, but is not used for anything. |
98 | /// Subclasses can use it to hold whatever state they find useful. This |
99 | /// field is initialized to zero by the ctor. |
100 | unsigned short SubclassData; |
101 | |
102 | protected: |
103 | /// The number of operands in the subclass. |
104 | /// |
105 | /// This member is defined by this class, but not used for anything. |
106 | /// Subclasses can use it to store their number of operands, if they have |
107 | /// any. |
108 | /// |
109 | /// This is stored here to save space in User on 64-bit hosts. Since most |
110 | /// instances of Value have operands, 32-bit hosts aren't significantly |
111 | /// affected. |
112 | /// |
113 | /// Note, this should *NOT* be used directly by any class other than User. |
114 | /// User uses this value to find the Use list. |
115 | enum : unsigned { NumUserOperandsBits = 28 }; |
116 | unsigned NumUserOperands : NumUserOperandsBits; |
117 | |
118 | // Use the same type as the bitfield above so that MSVC will pack them. |
119 | unsigned IsUsedByMD : 1; |
120 | unsigned HasName : 1; |
121 | unsigned HasHungOffUses : 1; |
122 | unsigned HasDescriptor : 1; |
123 | |
124 | private: |
125 | template <typename UseT> // UseT == 'Use' or 'const Use' |
126 | class use_iterator_impl |
127 | : public std::iterator<std::forward_iterator_tag, UseT *> { |
128 | friend class Value; |
129 | |
130 | UseT *U; |
131 | |
132 | explicit use_iterator_impl(UseT *u) : U(u) {} |
133 | |
134 | public: |
135 | use_iterator_impl() : U() {} |
136 | |
137 | bool operator==(const use_iterator_impl &x) const { return U == x.U; } |
138 | bool operator!=(const use_iterator_impl &x) const { return !operator==(x); } |
139 | |
140 | use_iterator_impl &operator++() { // Preincrement |
141 | assert(U && "Cannot increment end iterator!")((U && "Cannot increment end iterator!") ? static_cast <void> (0) : __assert_fail ("U && \"Cannot increment end iterator!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Value.h" , 141, __PRETTY_FUNCTION__)); |
142 | U = U->getNext(); |
143 | return *this; |
144 | } |
145 | |
146 | use_iterator_impl operator++(int) { // Postincrement |
147 | auto tmp = *this; |
148 | ++*this; |
149 | return tmp; |
150 | } |
151 | |
152 | UseT &operator*() const { |
153 | assert(U && "Cannot dereference end iterator!")((U && "Cannot dereference end iterator!") ? static_cast <void> (0) : __assert_fail ("U && \"Cannot dereference end iterator!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Value.h" , 153, __PRETTY_FUNCTION__)); |
154 | return *U; |
155 | } |
156 | |
157 | UseT *operator->() const { return &operator*(); } |
158 | |
159 | operator use_iterator_impl<const UseT>() const { |
160 | return use_iterator_impl<const UseT>(U); |
161 | } |
162 | }; |
163 | |
164 | template <typename UserTy> // UserTy == 'User' or 'const User' |
165 | class user_iterator_impl |
166 | : public std::iterator<std::forward_iterator_tag, UserTy *> { |
167 | use_iterator_impl<Use> UI; |
168 | explicit user_iterator_impl(Use *U) : UI(U) {} |
169 | friend class Value; |
170 | |
171 | public: |
172 | user_iterator_impl() = default; |
173 | |
174 | bool operator==(const user_iterator_impl &x) const { return UI == x.UI; } |
175 | bool operator!=(const user_iterator_impl &x) const { return !operator==(x); } |
176 | |
177 | /// Returns true if this iterator is equal to user_end() on the value. |
178 | bool atEnd() const { return *this == user_iterator_impl(); } |
179 | |
180 | user_iterator_impl &operator++() { // Preincrement |
181 | ++UI; |
182 | return *this; |
183 | } |
184 | |
185 | user_iterator_impl operator++(int) { // Postincrement |
186 | auto tmp = *this; |
187 | ++*this; |
188 | return tmp; |
189 | } |
190 | |
191 | // Retrieve a pointer to the current User. |
192 | UserTy *operator*() const { |
193 | return UI->getUser(); |
194 | } |
195 | |
196 | UserTy *operator->() const { return operator*(); } |
197 | |
198 | operator user_iterator_impl<const UserTy>() const { |
199 | return user_iterator_impl<const UserTy>(*UI); |
200 | } |
201 | |
202 | Use &getUse() const { return *UI; } |
203 | }; |
204 | |
205 | protected: |
206 | Value(Type *Ty, unsigned scid); |
207 | |
208 | /// Value's destructor should be virtual by design, but that would require |
209 | /// that Value and all of its subclasses have a vtable that effectively |
210 | /// duplicates the information in the value ID. As a size optimization, the |
211 | /// destructor has been protected, and the caller should manually call |
212 | /// deleteValue. |
213 | ~Value(); // Use deleteValue() to delete a generic Value. |
214 | |
215 | public: |
216 | Value(const Value &) = delete; |
217 | Value &operator=(const Value &) = delete; |
218 | |
219 | /// Delete a pointer to a generic Value. |
220 | void deleteValue(); |
221 | |
222 | /// Support for debugging, callable in GDB: V->dump() |
223 | void dump() const; |
224 | |
225 | /// Implement operator<< on Value. |
226 | /// @{ |
227 | void print(raw_ostream &O, bool IsForDebug = false) const; |
228 | void print(raw_ostream &O, ModuleSlotTracker &MST, |
229 | bool IsForDebug = false) const; |
230 | /// @} |
231 | |
232 | /// Print the name of this Value out to the specified raw_ostream. |
233 | /// |
234 | /// This is useful when you just want to print 'int %reg126', not the |
235 | /// instruction that generated it. If you specify a Module for context, then |
236 | /// even constanst get pretty-printed; for example, the type of a null |
237 | /// pointer is printed symbolically. |
238 | /// @{ |
239 | void printAsOperand(raw_ostream &O, bool PrintType = true, |
240 | const Module *M = nullptr) const; |
241 | void printAsOperand(raw_ostream &O, bool PrintType, |
242 | ModuleSlotTracker &MST) const; |
243 | /// @} |
244 | |
245 | /// All values are typed, get the type of this value. |
246 | Type *getType() const { return VTy; } |
247 | |
248 | /// All values hold a context through their type. |
249 | LLVMContext &getContext() const; |
250 | |
251 | // All values can potentially be named. |
252 | bool hasName() const { return HasName; } |
253 | ValueName *getValueName() const; |
254 | void setValueName(ValueName *VN); |
255 | |
256 | private: |
257 | void destroyValueName(); |
258 | enum class ReplaceMetadataUses { No, Yes }; |
259 | void doRAUW(Value *New, ReplaceMetadataUses); |
260 | void setNameImpl(const Twine &Name); |
261 | |
262 | public: |
263 | /// Return a constant reference to the value's name. |
264 | /// |
265 | /// This guaranteed to return the same reference as long as the value is not |
266 | /// modified. If the value has a name, this does a hashtable lookup, so it's |
267 | /// not free. |
268 | StringRef getName() const; |
269 | |
270 | /// Change the name of the value. |
271 | /// |
272 | /// Choose a new unique name if the provided name is taken. |
273 | /// |
274 | /// \param Name The new name; or "" if the value's name should be removed. |
275 | void setName(const Twine &Name); |
276 | |
277 | /// Transfer the name from V to this value. |
278 | /// |
279 | /// After taking V's name, sets V's name to empty. |
280 | /// |
281 | /// \note It is an error to call V->takeName(V). |
282 | void takeName(Value *V); |
283 | |
284 | /// Change all uses of this to point to a new Value. |
285 | /// |
286 | /// Go through the uses list for this definition and make each use point to |
287 | /// "V" instead of "this". After this completes, 'this's use list is |
288 | /// guaranteed to be empty. |
289 | void replaceAllUsesWith(Value *V); |
290 | |
291 | /// Change non-metadata uses of this to point to a new Value. |
292 | /// |
293 | /// Go through the uses list for this definition and make each use point to |
294 | /// "V" instead of "this". This function skips metadata entries in the list. |
295 | void replaceNonMetadataUsesWith(Value *V); |
296 | |
297 | /// Go through the uses list for this definition and make each use point |
298 | /// to "V" if the callback ShouldReplace returns true for the given Use. |
299 | /// Unlike replaceAllUsesWith() this function does not support basic block |
300 | /// values or constant users. |
301 | void replaceUsesWithIf(Value *New, |
302 | llvm::function_ref<bool(Use &U)> ShouldReplace) { |
303 | assert(New && "Value::replaceUsesWithIf(<null>) is invalid!")((New && "Value::replaceUsesWithIf(<null>) is invalid!" ) ? static_cast<void> (0) : __assert_fail ("New && \"Value::replaceUsesWithIf(<null>) is invalid!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Value.h" , 303, __PRETTY_FUNCTION__)); |
304 | assert(New->getType() == getType() &&((New->getType() == getType() && "replaceUses of value with new value of different type!" ) ? static_cast<void> (0) : __assert_fail ("New->getType() == getType() && \"replaceUses of value with new value of different type!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Value.h" , 305, __PRETTY_FUNCTION__)) |
305 | "replaceUses of value with new value of different type!")((New->getType() == getType() && "replaceUses of value with new value of different type!" ) ? static_cast<void> (0) : __assert_fail ("New->getType() == getType() && \"replaceUses of value with new value of different type!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Value.h" , 305, __PRETTY_FUNCTION__)); |
306 | |
307 | for (use_iterator UI = use_begin(), E = use_end(); UI != E;) { |
308 | Use &U = *UI; |
309 | ++UI; |
310 | if (!ShouldReplace(U)) |
311 | continue; |
312 | U.set(New); |
313 | } |
314 | } |
315 | |
316 | /// replaceUsesOutsideBlock - Go through the uses list for this definition and |
317 | /// make each use point to "V" instead of "this" when the use is outside the |
318 | /// block. 'This's use list is expected to have at least one element. |
319 | /// Unlike replaceAllUsesWith() this function does not support basic block |
320 | /// values or constant users. |
321 | void replaceUsesOutsideBlock(Value *V, BasicBlock *BB); |
322 | |
323 | //---------------------------------------------------------------------- |
324 | // Methods for handling the chain of uses of this Value. |
325 | // |
326 | // Materializing a function can introduce new uses, so these methods come in |
327 | // two variants: |
328 | // The methods that start with materialized_ check the uses that are |
329 | // currently known given which functions are materialized. Be very careful |
330 | // when using them since you might not get all uses. |
331 | // The methods that don't start with materialized_ assert that modules is |
332 | // fully materialized. |
333 | void assertModuleIsMaterializedImpl() const; |
334 | // This indirection exists so we can keep assertModuleIsMaterializedImpl() |
335 | // around in release builds of Value.cpp to be linked with other code built |
336 | // in debug mode. But this avoids calling it in any of the release built code. |
337 | void assertModuleIsMaterialized() const { |
338 | #ifndef NDEBUG |
339 | assertModuleIsMaterializedImpl(); |
340 | #endif |
341 | } |
342 | |
343 | bool use_empty() const { |
344 | assertModuleIsMaterialized(); |
345 | return UseList == nullptr; |
346 | } |
347 | |
348 | bool materialized_use_empty() const { |
349 | return UseList == nullptr; |
350 | } |
351 | |
352 | using use_iterator = use_iterator_impl<Use>; |
353 | using const_use_iterator = use_iterator_impl<const Use>; |
354 | |
355 | use_iterator materialized_use_begin() { return use_iterator(UseList); } |
356 | const_use_iterator materialized_use_begin() const { |
357 | return const_use_iterator(UseList); |
358 | } |
359 | use_iterator use_begin() { |
360 | assertModuleIsMaterialized(); |
361 | return materialized_use_begin(); |
362 | } |
363 | const_use_iterator use_begin() const { |
364 | assertModuleIsMaterialized(); |
365 | return materialized_use_begin(); |
366 | } |
367 | use_iterator use_end() { return use_iterator(); } |
368 | const_use_iterator use_end() const { return const_use_iterator(); } |
369 | iterator_range<use_iterator> materialized_uses() { |
370 | return make_range(materialized_use_begin(), use_end()); |
371 | } |
372 | iterator_range<const_use_iterator> materialized_uses() const { |
373 | return make_range(materialized_use_begin(), use_end()); |
374 | } |
375 | iterator_range<use_iterator> uses() { |
376 | assertModuleIsMaterialized(); |
377 | return materialized_uses(); |
378 | } |
379 | iterator_range<const_use_iterator> uses() const { |
380 | assertModuleIsMaterialized(); |
381 | return materialized_uses(); |
382 | } |
383 | |
384 | bool user_empty() const { |
385 | assertModuleIsMaterialized(); |
386 | return UseList == nullptr; |
387 | } |
388 | |
389 | using user_iterator = user_iterator_impl<User>; |
390 | using const_user_iterator = user_iterator_impl<const User>; |
391 | |
392 | user_iterator materialized_user_begin() { return user_iterator(UseList); } |
393 | const_user_iterator materialized_user_begin() const { |
394 | return const_user_iterator(UseList); |
395 | } |
396 | user_iterator user_begin() { |
397 | assertModuleIsMaterialized(); |
398 | return materialized_user_begin(); |
399 | } |
400 | const_user_iterator user_begin() const { |
401 | assertModuleIsMaterialized(); |
402 | return materialized_user_begin(); |
403 | } |
404 | user_iterator user_end() { return user_iterator(); } |
405 | const_user_iterator user_end() const { return const_user_iterator(); } |
406 | User *user_back() { |
407 | assertModuleIsMaterialized(); |
408 | return *materialized_user_begin(); |
409 | } |
410 | const User *user_back() const { |
411 | assertModuleIsMaterialized(); |
412 | return *materialized_user_begin(); |
413 | } |
414 | iterator_range<user_iterator> materialized_users() { |
415 | return make_range(materialized_user_begin(), user_end()); |
416 | } |
417 | iterator_range<const_user_iterator> materialized_users() const { |
418 | return make_range(materialized_user_begin(), user_end()); |
419 | } |
420 | iterator_range<user_iterator> users() { |
421 | assertModuleIsMaterialized(); |
422 | return materialized_users(); |
423 | } |
424 | iterator_range<const_user_iterator> users() const { |
425 | assertModuleIsMaterialized(); |
426 | return materialized_users(); |
427 | } |
428 | |
429 | /// Return true if there is exactly one user of this value. |
430 | /// |
431 | /// This is specialized because it is a common request and does not require |
432 | /// traversing the whole use list. |
433 | bool hasOneUse() const { |
434 | const_use_iterator I = use_begin(), E = use_end(); |
435 | if (I == E) return false; |
436 | return ++I == E; |
437 | } |
438 | |
439 | /// Return true if this Value has exactly N users. |
440 | bool hasNUses(unsigned N) const; |
441 | |
442 | /// Return true if this value has N users or more. |
443 | /// |
444 | /// This is logically equivalent to getNumUses() >= N. |
445 | bool hasNUsesOrMore(unsigned N) const; |
446 | |
447 | /// Check if this value is used in the specified basic block. |
448 | bool isUsedInBasicBlock(const BasicBlock *BB) const; |
449 | |
450 | /// This method computes the number of uses of this Value. |
451 | /// |
452 | /// This is a linear time operation. Use hasOneUse, hasNUses, or |
453 | /// hasNUsesOrMore to check for specific values. |
454 | unsigned getNumUses() const; |
455 | |
456 | /// This method should only be used by the Use class. |
457 | void addUse(Use &U) { U.addToList(&UseList); } |
458 | |
459 | /// Concrete subclass of this. |
460 | /// |
461 | /// An enumeration for keeping track of the concrete subclass of Value that |
462 | /// is actually instantiated. Values of this enumeration are kept in the |
463 | /// Value classes SubclassID field. They are used for concrete type |
464 | /// identification. |
465 | enum ValueTy { |
466 | #define HANDLE_VALUE(Name) Name##Val, |
467 | #include "llvm/IR/Value.def" |
468 | |
469 | // Markers: |
470 | #define HANDLE_CONSTANT_MARKER(Marker, Constant) Marker = Constant##Val, |
471 | #include "llvm/IR/Value.def" |
472 | }; |
473 | |
474 | /// Return an ID for the concrete type of this object. |
475 | /// |
476 | /// This is used to implement the classof checks. This should not be used |
477 | /// for any other purpose, as the values may change as LLVM evolves. Also, |
478 | /// note that for instructions, the Instruction's opcode is added to |
479 | /// InstructionVal. So this means three things: |
480 | /// # there is no value with code InstructionVal (no opcode==0). |
481 | /// # there are more possible values for the value type than in ValueTy enum. |
482 | /// # the InstructionVal enumerator must be the highest valued enumerator in |
483 | /// the ValueTy enum. |
484 | unsigned getValueID() const { |
485 | return SubclassID; |
486 | } |
487 | |
488 | /// Return the raw optional flags value contained in this value. |
489 | /// |
490 | /// This should only be used when testing two Values for equivalence. |
491 | unsigned getRawSubclassOptionalData() const { |
492 | return SubclassOptionalData; |
493 | } |
494 | |
495 | /// Clear the optional flags contained in this value. |
496 | void clearSubclassOptionalData() { |
497 | SubclassOptionalData = 0; |
498 | } |
499 | |
500 | /// Check the optional flags for equality. |
501 | bool hasSameSubclassOptionalData(const Value *V) const { |
502 | return SubclassOptionalData == V->SubclassOptionalData; |
503 | } |
504 | |
505 | /// Return true if there is a value handle associated with this value. |
506 | bool hasValueHandle() const { return HasValueHandle; } |
507 | |
508 | /// Return true if there is metadata referencing this value. |
509 | bool isUsedByMetadata() const { return IsUsedByMD; } |
510 | |
511 | /// Return true if this value is a swifterror value. |
512 | /// |
513 | /// swifterror values can be either a function argument or an alloca with a |
514 | /// swifterror attribute. |
515 | bool isSwiftError() const; |
516 | |
517 | /// Strip off pointer casts, all-zero GEPs and address space casts. |
518 | /// |
519 | /// Returns the original uncasted value. If this is called on a non-pointer |
520 | /// value, it returns 'this'. |
521 | const Value *stripPointerCasts() const; |
522 | Value *stripPointerCasts() { |
523 | return const_cast<Value *>( |
524 | static_cast<const Value *>(this)->stripPointerCasts()); |
525 | } |
526 | |
527 | /// Strip off pointer casts, all-zero GEPs, address space casts, and aliases. |
528 | /// |
529 | /// Returns the original uncasted value. If this is called on a non-pointer |
530 | /// value, it returns 'this'. |
531 | const Value *stripPointerCastsAndAliases() const; |
532 | Value *stripPointerCastsAndAliases() { |
533 | return const_cast<Value *>( |
534 | static_cast<const Value *>(this)->stripPointerCastsAndAliases()); |
535 | } |
536 | |
537 | /// Strip off pointer casts, all-zero GEPs and address space casts |
538 | /// but ensures the representation of the result stays the same. |
539 | /// |
540 | /// Returns the original uncasted value with the same representation. If this |
541 | /// is called on a non-pointer value, it returns 'this'. |
542 | const Value *stripPointerCastsSameRepresentation() const; |
543 | Value *stripPointerCastsSameRepresentation() { |
544 | return const_cast<Value *>(static_cast<const Value *>(this) |
545 | ->stripPointerCastsSameRepresentation()); |
546 | } |
547 | |
548 | /// Strip off pointer casts, all-zero GEPs and invariant group info. |
549 | /// |
550 | /// Returns the original uncasted value. If this is called on a non-pointer |
551 | /// value, it returns 'this'. This function should be used only in |
552 | /// Alias analysis. |
553 | const Value *stripPointerCastsAndInvariantGroups() const; |
554 | Value *stripPointerCastsAndInvariantGroups() { |
555 | return const_cast<Value *>(static_cast<const Value *>(this) |
556 | ->stripPointerCastsAndInvariantGroups()); |
557 | } |
558 | |
559 | /// Strip off pointer casts and all-constant inbounds GEPs. |
560 | /// |
561 | /// Returns the original pointer value. If this is called on a non-pointer |
562 | /// value, it returns 'this'. |
563 | const Value *stripInBoundsConstantOffsets() const; |
564 | Value *stripInBoundsConstantOffsets() { |
565 | return const_cast<Value *>( |
566 | static_cast<const Value *>(this)->stripInBoundsConstantOffsets()); |
567 | } |
568 | |
569 | /// Accumulate the constant offset this value has compared to a base pointer. |
570 | /// Only 'getelementptr' instructions (GEPs) with constant indices are |
571 | /// accumulated but other instructions, e.g., casts, are stripped away as |
572 | /// well. The accumulated constant offset is added to \p Offset and the base |
573 | /// pointer is returned. |
574 | /// |
575 | /// The APInt \p Offset has to have a bit-width equal to the IntPtr type for |
576 | /// the address space of 'this' pointer value, e.g., use |
577 | /// DataLayout::getIndexTypeSizeInBits(Ty). |
578 | /// |
579 | /// If \p AllowNonInbounds is true, constant offsets in GEPs are stripped and |
580 | /// accumulated even if the GEP is not "inbounds". |
581 | /// |
582 | /// If this is called on a non-pointer value, it returns 'this' and the |
583 | /// \p Offset is not modified. |
584 | /// |
585 | /// Note that this function will never return a nullptr. It will also never |
586 | /// manipulate the \p Offset in a way that would not match the difference |
587 | /// between the underlying value and the returned one. Thus, if no constant |
588 | /// offset was found, the returned value is the underlying one and \p Offset |
589 | /// is unchanged. |
590 | const Value *stripAndAccumulateConstantOffsets(const DataLayout &DL, |
591 | APInt &Offset, |
592 | bool AllowNonInbounds) const; |
593 | Value *stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, |
594 | bool AllowNonInbounds) { |
595 | return const_cast<Value *>( |
596 | static_cast<const Value *>(this)->stripAndAccumulateConstantOffsets( |
597 | DL, Offset, AllowNonInbounds)); |
598 | } |
599 | |
600 | /// This is a wrapper around stripAndAccumulateConstantOffsets with the |
601 | /// in-bounds requirement set to false. |
602 | const Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, |
603 | APInt &Offset) const { |
604 | return stripAndAccumulateConstantOffsets(DL, Offset, |
605 | /* AllowNonInbounds */ false); |
606 | } |
607 | Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, |
608 | APInt &Offset) { |
609 | return stripAndAccumulateConstantOffsets(DL, Offset, |
610 | /* AllowNonInbounds */ false); |
611 | } |
612 | |
613 | /// Strip off pointer casts and inbounds GEPs. |
614 | /// |
615 | /// Returns the original pointer value. If this is called on a non-pointer |
616 | /// value, it returns 'this'. |
617 | const Value *stripInBoundsOffsets() const; |
618 | Value *stripInBoundsOffsets() { |
619 | return const_cast<Value *>( |
620 | static_cast<const Value *>(this)->stripInBoundsOffsets()); |
621 | } |
622 | |
623 | /// Returns the number of bytes known to be dereferenceable for the |
624 | /// pointer value. |
625 | /// |
626 | /// If CanBeNull is set by this function the pointer can either be null or be |
627 | /// dereferenceable up to the returned number of bytes. |
628 | uint64_t getPointerDereferenceableBytes(const DataLayout &DL, |
629 | bool &CanBeNull) const; |
630 | |
631 | /// Returns an alignment of the pointer value. |
632 | /// |
633 | /// Returns an alignment which is either specified explicitly, e.g. via |
634 | /// align attribute of a function argument, or guaranteed by DataLayout. |
635 | MaybeAlign getPointerAlignment(const DataLayout &DL) const; |
636 | |
637 | /// Translate PHI node to its predecessor from the given basic block. |
638 | /// |
639 | /// If this value is a PHI node with CurBB as its parent, return the value in |
640 | /// the PHI node corresponding to PredBB. If not, return ourself. This is |
641 | /// useful if you want to know the value something has in a predecessor |
642 | /// block. |
643 | const Value *DoPHITranslation(const BasicBlock *CurBB, |
644 | const BasicBlock *PredBB) const; |
645 | Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) { |
646 | return const_cast<Value *>( |
647 | static_cast<const Value *>(this)->DoPHITranslation(CurBB, PredBB)); |
648 | } |
649 | |
650 | /// The maximum alignment for instructions. |
651 | /// |
652 | /// This is the greatest alignment value supported by load, store, and alloca |
653 | /// instructions, and global values. |
654 | static const unsigned MaxAlignmentExponent = 29; |
655 | static const unsigned MaximumAlignment = 1u << MaxAlignmentExponent; |
656 | |
657 | /// Mutate the type of this Value to be of the specified type. |
658 | /// |
659 | /// Note that this is an extremely dangerous operation which can create |
660 | /// completely invalid IR very easily. It is strongly recommended that you |
661 | /// recreate IR objects with the right types instead of mutating them in |
662 | /// place. |
663 | void mutateType(Type *Ty) { |
664 | VTy = Ty; |
665 | } |
666 | |
667 | /// Sort the use-list. |
668 | /// |
669 | /// Sorts the Value's use-list by Cmp using a stable mergesort. Cmp is |
670 | /// expected to compare two \a Use references. |
671 | template <class Compare> void sortUseList(Compare Cmp); |
672 | |
673 | /// Reverse the use-list. |
674 | void reverseUseList(); |
675 | |
676 | private: |
677 | /// Merge two lists together. |
678 | /// |
679 | /// Merges \c L and \c R using \c Cmp. To enable stable sorts, always pushes |
680 | /// "equal" items from L before items from R. |
681 | /// |
682 | /// \return the first element in the list. |
683 | /// |
684 | /// \note Completely ignores \a Use::Prev (doesn't read, doesn't update). |
685 | template <class Compare> |
686 | static Use *mergeUseLists(Use *L, Use *R, Compare Cmp) { |
687 | Use *Merged; |
688 | Use **Next = &Merged; |
689 | |
690 | while (true) { |
691 | if (!L) { |
692 | *Next = R; |
693 | break; |
694 | } |
695 | if (!R) { |
696 | *Next = L; |
697 | break; |
698 | } |
699 | if (Cmp(*R, *L)) { |
700 | *Next = R; |
701 | Next = &R->Next; |
702 | R = R->Next; |
703 | } else { |
704 | *Next = L; |
705 | Next = &L->Next; |
706 | L = L->Next; |
707 | } |
708 | } |
709 | |
710 | return Merged; |
711 | } |
712 | |
713 | protected: |
714 | unsigned short getSubclassDataFromValue() const { return SubclassData; } |
715 | void setValueSubclassData(unsigned short D) { SubclassData = D; } |
716 | }; |
717 | |
718 | struct ValueDeleter { void operator()(Value *V) { V->deleteValue(); } }; |
719 | |
720 | /// Use this instead of std::unique_ptr<Value> or std::unique_ptr<Instruction>. |
721 | /// Those don't work because Value and Instruction's destructors are protected, |
722 | /// aren't virtual, and won't destroy the complete object. |
723 | using unique_value = std::unique_ptr<Value, ValueDeleter>; |
724 | |
725 | inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) { |
726 | V.print(OS); |
727 | return OS; |
728 | } |
729 | |
730 | void Use::set(Value *V) { |
731 | if (Val) removeFromList(); |
732 | Val = V; |
733 | if (V) V->addUse(*this); |
734 | } |
735 | |
736 | Value *Use::operator=(Value *RHS) { |
737 | set(RHS); |
738 | return RHS; |
739 | } |
740 | |
741 | const Use &Use::operator=(const Use &RHS) { |
742 | set(RHS.Val); |
743 | return *this; |
744 | } |
745 | |
746 | template <class Compare> void Value::sortUseList(Compare Cmp) { |
747 | if (!UseList || !UseList->Next) |
748 | // No need to sort 0 or 1 uses. |
749 | return; |
750 | |
751 | // Note: this function completely ignores Prev pointers until the end when |
752 | // they're fixed en masse. |
753 | |
754 | // Create a binomial vector of sorted lists, visiting uses one at a time and |
755 | // merging lists as necessary. |
756 | const unsigned MaxSlots = 32; |
757 | Use *Slots[MaxSlots]; |
758 | |
759 | // Collect the first use, turning it into a single-item list. |
760 | Use *Next = UseList->Next; |
761 | UseList->Next = nullptr; |
762 | unsigned NumSlots = 1; |
763 | Slots[0] = UseList; |
764 | |
765 | // Collect all but the last use. |
766 | while (Next->Next) { |
767 | Use *Current = Next; |
768 | Next = Current->Next; |
769 | |
770 | // Turn Current into a single-item list. |
771 | Current->Next = nullptr; |
772 | |
773 | // Save Current in the first available slot, merging on collisions. |
774 | unsigned I; |
775 | for (I = 0; I < NumSlots; ++I) { |
776 | if (!Slots[I]) |
777 | break; |
778 | |
779 | // Merge two lists, doubling the size of Current and emptying slot I. |
780 | // |
781 | // Since the uses in Slots[I] originally preceded those in Current, send |
782 | // Slots[I] in as the left parameter to maintain a stable sort. |
783 | Current = mergeUseLists(Slots[I], Current, Cmp); |
784 | Slots[I] = nullptr; |
785 | } |
786 | // Check if this is a new slot. |
787 | if (I == NumSlots) { |
788 | ++NumSlots; |
789 | assert(NumSlots <= MaxSlots && "Use list bigger than 2^32")((NumSlots <= MaxSlots && "Use list bigger than 2^32" ) ? static_cast<void> (0) : __assert_fail ("NumSlots <= MaxSlots && \"Use list bigger than 2^32\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Value.h" , 789, __PRETTY_FUNCTION__)); |
790 | } |
791 | |
792 | // Found an open slot. |
793 | Slots[I] = Current; |
794 | } |
795 | |
796 | // Merge all the lists together. |
797 | assert(Next && "Expected one more Use")((Next && "Expected one more Use") ? static_cast<void > (0) : __assert_fail ("Next && \"Expected one more Use\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Value.h" , 797, __PRETTY_FUNCTION__)); |
798 | assert(!Next->Next && "Expected only one Use")((!Next->Next && "Expected only one Use") ? static_cast <void> (0) : __assert_fail ("!Next->Next && \"Expected only one Use\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Value.h" , 798, __PRETTY_FUNCTION__)); |
799 | UseList = Next; |
800 | for (unsigned I = 0; I < NumSlots; ++I) |
801 | if (Slots[I]) |
802 | // Since the uses in Slots[I] originally preceded those in UseList, send |
803 | // Slots[I] in as the left parameter to maintain a stable sort. |
804 | UseList = mergeUseLists(Slots[I], UseList, Cmp); |
805 | |
806 | // Fix the Prev pointers. |
807 | for (Use *I = UseList, **Prev = &UseList; I; I = I->Next) { |
808 | I->setPrev(Prev); |
809 | Prev = &I->Next; |
810 | } |
811 | } |
812 | |
813 | // isa - Provide some specializations of isa so that we don't have to include |
814 | // the subtype header files to test to see if the value is a subclass... |
815 | // |
816 | template <> struct isa_impl<Constant, Value> { |
817 | static inline bool doit(const Value &Val) { |
818 | static_assert(Value::ConstantFirstVal == 0, "Val.getValueID() >= Value::ConstantFirstVal"); |
819 | return Val.getValueID() <= Value::ConstantLastVal; |
820 | } |
821 | }; |
822 | |
823 | template <> struct isa_impl<ConstantData, Value> { |
824 | static inline bool doit(const Value &Val) { |
825 | return Val.getValueID() >= Value::ConstantDataFirstVal && |
826 | Val.getValueID() <= Value::ConstantDataLastVal; |
827 | } |
828 | }; |
829 | |
830 | template <> struct isa_impl<ConstantAggregate, Value> { |
831 | static inline bool doit(const Value &Val) { |
832 | return Val.getValueID() >= Value::ConstantAggregateFirstVal && |
833 | Val.getValueID() <= Value::ConstantAggregateLastVal; |
834 | } |
835 | }; |
836 | |
837 | template <> struct isa_impl<Argument, Value> { |
838 | static inline bool doit (const Value &Val) { |
839 | return Val.getValueID() == Value::ArgumentVal; |
840 | } |
841 | }; |
842 | |
843 | template <> struct isa_impl<InlineAsm, Value> { |
844 | static inline bool doit(const Value &Val) { |
845 | return Val.getValueID() == Value::InlineAsmVal; |
846 | } |
847 | }; |
848 | |
849 | template <> struct isa_impl<Instruction, Value> { |
850 | static inline bool doit(const Value &Val) { |
851 | return Val.getValueID() >= Value::InstructionVal; |
852 | } |
853 | }; |
854 | |
855 | template <> struct isa_impl<BasicBlock, Value> { |
856 | static inline bool doit(const Value &Val) { |
857 | return Val.getValueID() == Value::BasicBlockVal; |
858 | } |
859 | }; |
860 | |
861 | template <> struct isa_impl<Function, Value> { |
862 | static inline bool doit(const Value &Val) { |
863 | return Val.getValueID() == Value::FunctionVal; |
864 | } |
865 | }; |
866 | |
867 | template <> struct isa_impl<GlobalVariable, Value> { |
868 | static inline bool doit(const Value &Val) { |
869 | return Val.getValueID() == Value::GlobalVariableVal; |
870 | } |
871 | }; |
872 | |
873 | template <> struct isa_impl<GlobalAlias, Value> { |
874 | static inline bool doit(const Value &Val) { |
875 | return Val.getValueID() == Value::GlobalAliasVal; |
876 | } |
877 | }; |
878 | |
879 | template <> struct isa_impl<GlobalIFunc, Value> { |
880 | static inline bool doit(const Value &Val) { |
881 | return Val.getValueID() == Value::GlobalIFuncVal; |
882 | } |
883 | }; |
884 | |
885 | template <> struct isa_impl<GlobalIndirectSymbol, Value> { |
886 | static inline bool doit(const Value &Val) { |
887 | return isa<GlobalAlias>(Val) || isa<GlobalIFunc>(Val); |
888 | } |
889 | }; |
890 | |
891 | template <> struct isa_impl<GlobalValue, Value> { |
892 | static inline bool doit(const Value &Val) { |
893 | return isa<GlobalObject>(Val) || isa<GlobalIndirectSymbol>(Val); |
894 | } |
895 | }; |
896 | |
897 | template <> struct isa_impl<GlobalObject, Value> { |
898 | static inline bool doit(const Value &Val) { |
899 | return isa<GlobalVariable>(Val) || isa<Function>(Val); |
900 | } |
901 | }; |
902 | |
903 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
904 | DEFINE_ISA_CONVERSION_FUNCTIONS(Value, LLVMValueRef)inline Value *unwrap(LLVMValueRef P) { return reinterpret_cast <Value*>(P); } inline LLVMValueRef wrap(const Value *P) { return reinterpret_cast<LLVMValueRef>(const_cast< Value*>(P)); } template<typename T> inline T *unwrap (LLVMValueRef P) { return cast<T>(unwrap(P)); } |
905 | |
906 | // Specialized opaque value conversions. |
907 | inline Value **unwrap(LLVMValueRef *Vals) { |
908 | return reinterpret_cast<Value**>(Vals); |
909 | } |
910 | |
911 | template<typename T> |
912 | inline T **unwrap(LLVMValueRef *Vals, unsigned Length) { |
913 | #ifndef NDEBUG |
914 | for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I) |
915 | unwrap<T>(*I); // For side effect of calling assert on invalid usage. |
916 | #endif |
917 | (void)Length; |
918 | return reinterpret_cast<T**>(Vals); |
919 | } |
920 | |
921 | inline LLVMValueRef *wrap(const Value **Vals) { |
922 | return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals)); |
923 | } |
924 | |
925 | } // end namespace llvm |
926 | |
927 | #endif // LLVM_IR_VALUE_H |
1 | //===- llvm/IRBuilder.h - Builder for LLVM Instructions ---------*- 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 IRBuilder class, which is used as a convenient way | ||||||||||
10 | // to create LLVM instructions with a consistent and simplified interface. | ||||||||||
11 | // | ||||||||||
12 | //===----------------------------------------------------------------------===// | ||||||||||
13 | |||||||||||
14 | #ifndef LLVM_IR_IRBUILDER_H | ||||||||||
15 | #define LLVM_IR_IRBUILDER_H | ||||||||||
16 | |||||||||||
17 | #include "llvm-c/Types.h" | ||||||||||
18 | #include "llvm/ADT/ArrayRef.h" | ||||||||||
19 | #include "llvm/ADT/None.h" | ||||||||||
20 | #include "llvm/ADT/StringRef.h" | ||||||||||
21 | #include "llvm/ADT/Twine.h" | ||||||||||
22 | #include "llvm/IR/BasicBlock.h" | ||||||||||
23 | #include "llvm/IR/Constant.h" | ||||||||||
24 | #include "llvm/IR/ConstantFolder.h" | ||||||||||
25 | #include "llvm/IR/Constants.h" | ||||||||||
26 | #include "llvm/IR/DataLayout.h" | ||||||||||
27 | #include "llvm/IR/DebugLoc.h" | ||||||||||
28 | #include "llvm/IR/DerivedTypes.h" | ||||||||||
29 | #include "llvm/IR/Function.h" | ||||||||||
30 | #include "llvm/IR/GlobalVariable.h" | ||||||||||
31 | #include "llvm/IR/InstrTypes.h" | ||||||||||
32 | #include "llvm/IR/Instruction.h" | ||||||||||
33 | #include "llvm/IR/Instructions.h" | ||||||||||
34 | #include "llvm/IR/IntrinsicInst.h" | ||||||||||
35 | #include "llvm/IR/LLVMContext.h" | ||||||||||
36 | #include "llvm/IR/Module.h" | ||||||||||
37 | #include "llvm/IR/Operator.h" | ||||||||||
38 | #include "llvm/IR/Type.h" | ||||||||||
39 | #include "llvm/IR/Value.h" | ||||||||||
40 | #include "llvm/IR/ValueHandle.h" | ||||||||||
41 | #include "llvm/Support/AtomicOrdering.h" | ||||||||||
42 | #include "llvm/Support/CBindingWrapping.h" | ||||||||||
43 | #include "llvm/Support/Casting.h" | ||||||||||
44 | #include <cassert> | ||||||||||
45 | #include <cstddef> | ||||||||||
46 | #include <cstdint> | ||||||||||
47 | #include <functional> | ||||||||||
48 | #include <utility> | ||||||||||
49 | |||||||||||
50 | namespace llvm { | ||||||||||
51 | |||||||||||
52 | class APInt; | ||||||||||
53 | class MDNode; | ||||||||||
54 | class Use; | ||||||||||
55 | |||||||||||
56 | /// This provides the default implementation of the IRBuilder | ||||||||||
57 | /// 'InsertHelper' method that is called whenever an instruction is created by | ||||||||||
58 | /// IRBuilder and needs to be inserted. | ||||||||||
59 | /// | ||||||||||
60 | /// By default, this inserts the instruction at the insertion point. | ||||||||||
61 | class IRBuilderDefaultInserter { | ||||||||||
62 | public: | ||||||||||
63 | virtual ~IRBuilderDefaultInserter(); | ||||||||||
64 | |||||||||||
65 | virtual void InsertHelper(Instruction *I, const Twine &Name, | ||||||||||
66 | BasicBlock *BB, | ||||||||||
67 | BasicBlock::iterator InsertPt) const { | ||||||||||
68 | if (BB) BB->getInstList().insert(InsertPt, I); | ||||||||||
69 | I->setName(Name); | ||||||||||
70 | } | ||||||||||
71 | }; | ||||||||||
72 | |||||||||||
73 | /// Provides an 'InsertHelper' that calls a user-provided callback after | ||||||||||
74 | /// performing the default insertion. | ||||||||||
75 | class IRBuilderCallbackInserter : public IRBuilderDefaultInserter { | ||||||||||
76 | std::function<void(Instruction *)> Callback; | ||||||||||
77 | |||||||||||
78 | public: | ||||||||||
79 | virtual ~IRBuilderCallbackInserter(); | ||||||||||
80 | |||||||||||
81 | IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback) | ||||||||||
82 | : Callback(std::move(Callback)) {} | ||||||||||
83 | |||||||||||
84 | void InsertHelper(Instruction *I, const Twine &Name, | ||||||||||
85 | BasicBlock *BB, | ||||||||||
86 | BasicBlock::iterator InsertPt) const override { | ||||||||||
87 | IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt); | ||||||||||
88 | Callback(I); | ||||||||||
89 | } | ||||||||||
90 | }; | ||||||||||
91 | |||||||||||
92 | /// Common base class shared among various IRBuilders. | ||||||||||
93 | class IRBuilderBase { | ||||||||||
94 | DebugLoc CurDbgLocation; | ||||||||||
95 | |||||||||||
96 | protected: | ||||||||||
97 | BasicBlock *BB; | ||||||||||
98 | BasicBlock::iterator InsertPt; | ||||||||||
99 | LLVMContext &Context; | ||||||||||
100 | const IRBuilderFolder &Folder; | ||||||||||
101 | const IRBuilderDefaultInserter &Inserter; | ||||||||||
102 | |||||||||||
103 | MDNode *DefaultFPMathTag; | ||||||||||
104 | FastMathFlags FMF; | ||||||||||
105 | |||||||||||
106 | bool IsFPConstrained; | ||||||||||
107 | fp::ExceptionBehavior DefaultConstrainedExcept; | ||||||||||
108 | fp::RoundingMode DefaultConstrainedRounding; | ||||||||||
109 | |||||||||||
110 | ArrayRef<OperandBundleDef> DefaultOperandBundles; | ||||||||||
111 | |||||||||||
112 | public: | ||||||||||
113 | IRBuilderBase(LLVMContext &context, const IRBuilderFolder &Folder, | ||||||||||
114 | const IRBuilderDefaultInserter &Inserter, | ||||||||||
115 | MDNode *FPMathTag, ArrayRef<OperandBundleDef> OpBundles) | ||||||||||
116 | : Context(context), Folder(Folder), Inserter(Inserter), | ||||||||||
117 | DefaultFPMathTag(FPMathTag), IsFPConstrained(false), | ||||||||||
118 | DefaultConstrainedExcept(fp::ebStrict), | ||||||||||
119 | DefaultConstrainedRounding(fp::rmDynamic), | ||||||||||
120 | DefaultOperandBundles(OpBundles) { | ||||||||||
121 | ClearInsertionPoint(); | ||||||||||
122 | } | ||||||||||
123 | |||||||||||
124 | /// Insert and return the specified instruction. | ||||||||||
125 | template<typename InstTy> | ||||||||||
126 | InstTy *Insert(InstTy *I, const Twine &Name = "") const { | ||||||||||
127 | Inserter.InsertHelper(I, Name, BB, InsertPt); | ||||||||||
128 | SetInstDebugLocation(I); | ||||||||||
129 | return I; | ||||||||||
130 | } | ||||||||||
131 | |||||||||||
132 | /// No-op overload to handle constants. | ||||||||||
133 | Constant *Insert(Constant *C, const Twine& = "") const { | ||||||||||
134 | return C; | ||||||||||
135 | } | ||||||||||
136 | |||||||||||
137 | Value *Insert(Value *V, const Twine &Name = "") const { | ||||||||||
138 | if (Instruction *I = dyn_cast<Instruction>(V)) | ||||||||||
139 | return Insert(I, Name); | ||||||||||
140 | assert(isa<Constant>(V))((isa<Constant>(V)) ? static_cast<void> (0) : __assert_fail ("isa<Constant>(V)", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 140, __PRETTY_FUNCTION__)); | ||||||||||
141 | return V; | ||||||||||
142 | } | ||||||||||
143 | |||||||||||
144 | //===--------------------------------------------------------------------===// | ||||||||||
145 | // Builder configuration methods | ||||||||||
146 | //===--------------------------------------------------------------------===// | ||||||||||
147 | |||||||||||
148 | /// Clear the insertion point: created instructions will not be | ||||||||||
149 | /// inserted into a block. | ||||||||||
150 | void ClearInsertionPoint() { | ||||||||||
151 | BB = nullptr; | ||||||||||
152 | InsertPt = BasicBlock::iterator(); | ||||||||||
153 | } | ||||||||||
154 | |||||||||||
155 | BasicBlock *GetInsertBlock() const { return BB; } | ||||||||||
156 | BasicBlock::iterator GetInsertPoint() const { return InsertPt; } | ||||||||||
157 | LLVMContext &getContext() const { return Context; } | ||||||||||
158 | |||||||||||
159 | /// This specifies that created instructions should be appended to the | ||||||||||
160 | /// end of the specified block. | ||||||||||
161 | void SetInsertPoint(BasicBlock *TheBB) { | ||||||||||
162 | BB = TheBB; | ||||||||||
163 | InsertPt = BB->end(); | ||||||||||
164 | } | ||||||||||
165 | |||||||||||
166 | /// This specifies that created instructions should be inserted before | ||||||||||
167 | /// the specified instruction. | ||||||||||
168 | void SetInsertPoint(Instruction *I) { | ||||||||||
169 | BB = I->getParent(); | ||||||||||
170 | InsertPt = I->getIterator(); | ||||||||||
171 | assert(InsertPt != BB->end() && "Can't read debug loc from end()")((InsertPt != BB->end() && "Can't read debug loc from end()" ) ? static_cast<void> (0) : __assert_fail ("InsertPt != BB->end() && \"Can't read debug loc from end()\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 171, __PRETTY_FUNCTION__)); | ||||||||||
172 | SetCurrentDebugLocation(I->getDebugLoc()); | ||||||||||
173 | } | ||||||||||
174 | |||||||||||
175 | /// This specifies that created instructions should be inserted at the | ||||||||||
176 | /// specified point. | ||||||||||
177 | void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) { | ||||||||||
178 | BB = TheBB; | ||||||||||
179 | InsertPt = IP; | ||||||||||
180 | if (IP != TheBB->end()) | ||||||||||
181 | SetCurrentDebugLocation(IP->getDebugLoc()); | ||||||||||
182 | } | ||||||||||
183 | |||||||||||
184 | /// Set location information used by debugging information. | ||||||||||
185 | void SetCurrentDebugLocation(DebugLoc L) { CurDbgLocation = std::move(L); } | ||||||||||
186 | |||||||||||
187 | /// Get location information used by debugging information. | ||||||||||
188 | const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; } | ||||||||||
189 | |||||||||||
190 | /// If this builder has a current debug location, set it on the | ||||||||||
191 | /// specified instruction. | ||||||||||
192 | void SetInstDebugLocation(Instruction *I) const { | ||||||||||
193 | if (CurDbgLocation) | ||||||||||
194 | I->setDebugLoc(CurDbgLocation); | ||||||||||
195 | } | ||||||||||
196 | |||||||||||
197 | /// Get the return type of the current function that we're emitting | ||||||||||
198 | /// into. | ||||||||||
199 | Type *getCurrentFunctionReturnType() const; | ||||||||||
200 | |||||||||||
201 | /// InsertPoint - A saved insertion point. | ||||||||||
202 | class InsertPoint { | ||||||||||
203 | BasicBlock *Block = nullptr; | ||||||||||
204 | BasicBlock::iterator Point; | ||||||||||
205 | |||||||||||
206 | public: | ||||||||||
207 | /// Creates a new insertion point which doesn't point to anything. | ||||||||||
208 | InsertPoint() = default; | ||||||||||
209 | |||||||||||
210 | /// Creates a new insertion point at the given location. | ||||||||||
211 | InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint) | ||||||||||
212 | : Block(InsertBlock), Point(InsertPoint) {} | ||||||||||
213 | |||||||||||
214 | /// Returns true if this insert point is set. | ||||||||||
215 | bool isSet() const { return (Block != nullptr); } | ||||||||||
216 | |||||||||||
217 | BasicBlock *getBlock() const { return Block; } | ||||||||||
218 | BasicBlock::iterator getPoint() const { return Point; } | ||||||||||
219 | }; | ||||||||||
220 | |||||||||||
221 | /// Returns the current insert point. | ||||||||||
222 | InsertPoint saveIP() const { | ||||||||||
223 | return InsertPoint(GetInsertBlock(), GetInsertPoint()); | ||||||||||
224 | } | ||||||||||
225 | |||||||||||
226 | /// Returns the current insert point, clearing it in the process. | ||||||||||
227 | InsertPoint saveAndClearIP() { | ||||||||||
228 | InsertPoint IP(GetInsertBlock(), GetInsertPoint()); | ||||||||||
229 | ClearInsertionPoint(); | ||||||||||
230 | return IP; | ||||||||||
231 | } | ||||||||||
232 | |||||||||||
233 | /// Sets the current insert point to a previously-saved location. | ||||||||||
234 | void restoreIP(InsertPoint IP) { | ||||||||||
235 | if (IP.isSet()) | ||||||||||
236 | SetInsertPoint(IP.getBlock(), IP.getPoint()); | ||||||||||
237 | else | ||||||||||
238 | ClearInsertionPoint(); | ||||||||||
239 | } | ||||||||||
240 | |||||||||||
241 | /// Get the floating point math metadata being used. | ||||||||||
242 | MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; } | ||||||||||
243 | |||||||||||
244 | /// Get the flags to be applied to created floating point ops | ||||||||||
245 | FastMathFlags getFastMathFlags() const { return FMF; } | ||||||||||
246 | |||||||||||
247 | /// Clear the fast-math flags. | ||||||||||
248 | void clearFastMathFlags() { FMF.clear(); } | ||||||||||
249 | |||||||||||
250 | /// Set the floating point math metadata to be used. | ||||||||||
251 | void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; } | ||||||||||
252 | |||||||||||
253 | /// Set the fast-math flags to be used with generated fp-math operators | ||||||||||
254 | void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; } | ||||||||||
255 | |||||||||||
256 | /// Enable/Disable use of constrained floating point math. When | ||||||||||
257 | /// enabled the CreateF<op>() calls instead create constrained | ||||||||||
258 | /// floating point intrinsic calls. Fast math flags are unaffected | ||||||||||
259 | /// by this setting. | ||||||||||
260 | void setIsFPConstrained(bool IsCon) { IsFPConstrained = IsCon; } | ||||||||||
261 | |||||||||||
262 | /// Query for the use of constrained floating point math | ||||||||||
263 | bool getIsFPConstrained() { return IsFPConstrained; } | ||||||||||
264 | |||||||||||
265 | /// Set the exception handling to be used with constrained floating point | ||||||||||
266 | void setDefaultConstrainedExcept(fp::ExceptionBehavior NewExcept) { | ||||||||||
267 | DefaultConstrainedExcept = NewExcept; | ||||||||||
268 | } | ||||||||||
269 | |||||||||||
270 | /// Set the rounding mode handling to be used with constrained floating point | ||||||||||
271 | void setDefaultConstrainedRounding(fp::RoundingMode NewRounding) { | ||||||||||
272 | DefaultConstrainedRounding = NewRounding; | ||||||||||
273 | } | ||||||||||
274 | |||||||||||
275 | /// Get the exception handling used with constrained floating point | ||||||||||
276 | fp::ExceptionBehavior getDefaultConstrainedExcept() { | ||||||||||
277 | return DefaultConstrainedExcept; | ||||||||||
278 | } | ||||||||||
279 | |||||||||||
280 | /// Get the rounding mode handling used with constrained floating point | ||||||||||
281 | fp::RoundingMode getDefaultConstrainedRounding() { | ||||||||||
282 | return DefaultConstrainedRounding; | ||||||||||
283 | } | ||||||||||
284 | |||||||||||
285 | void setConstrainedFPFunctionAttr() { | ||||||||||
286 | assert(BB && "Must have a basic block to set any function attributes!")((BB && "Must have a basic block to set any function attributes!" ) ? static_cast<void> (0) : __assert_fail ("BB && \"Must have a basic block to set any function attributes!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 286, __PRETTY_FUNCTION__)); | ||||||||||
287 | |||||||||||
288 | Function *F = BB->getParent(); | ||||||||||
289 | if (!F->hasFnAttribute(Attribute::StrictFP)) { | ||||||||||
290 | F->addFnAttr(Attribute::StrictFP); | ||||||||||
291 | } | ||||||||||
292 | } | ||||||||||
293 | |||||||||||
294 | void setConstrainedFPCallAttr(CallInst *I) { | ||||||||||
295 | if (!I->hasFnAttr(Attribute::StrictFP)) | ||||||||||
296 | I->addAttribute(AttributeList::FunctionIndex, Attribute::StrictFP); | ||||||||||
297 | } | ||||||||||
298 | |||||||||||
299 | void setDefaultOperandBundles(ArrayRef<OperandBundleDef> OpBundles) { | ||||||||||
300 | DefaultOperandBundles = OpBundles; | ||||||||||
301 | } | ||||||||||
302 | |||||||||||
303 | //===--------------------------------------------------------------------===// | ||||||||||
304 | // RAII helpers. | ||||||||||
305 | //===--------------------------------------------------------------------===// | ||||||||||
306 | |||||||||||
307 | // RAII object that stores the current insertion point and restores it | ||||||||||
308 | // when the object is destroyed. This includes the debug location. | ||||||||||
309 | class InsertPointGuard { | ||||||||||
310 | IRBuilderBase &Builder; | ||||||||||
311 | AssertingVH<BasicBlock> Block; | ||||||||||
312 | BasicBlock::iterator Point; | ||||||||||
313 | DebugLoc DbgLoc; | ||||||||||
314 | |||||||||||
315 | public: | ||||||||||
316 | InsertPointGuard(IRBuilderBase &B) | ||||||||||
317 | : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()), | ||||||||||
318 | DbgLoc(B.getCurrentDebugLocation()) {} | ||||||||||
319 | |||||||||||
320 | InsertPointGuard(const InsertPointGuard &) = delete; | ||||||||||
321 | InsertPointGuard &operator=(const InsertPointGuard &) = delete; | ||||||||||
322 | |||||||||||
323 | ~InsertPointGuard() { | ||||||||||
324 | Builder.restoreIP(InsertPoint(Block, Point)); | ||||||||||
325 | Builder.SetCurrentDebugLocation(DbgLoc); | ||||||||||
326 | } | ||||||||||
327 | }; | ||||||||||
328 | |||||||||||
329 | // RAII object that stores the current fast math settings and restores | ||||||||||
330 | // them when the object is destroyed. | ||||||||||
331 | class FastMathFlagGuard { | ||||||||||
332 | IRBuilderBase &Builder; | ||||||||||
333 | FastMathFlags FMF; | ||||||||||
334 | MDNode *FPMathTag; | ||||||||||
335 | |||||||||||
336 | public: | ||||||||||
337 | FastMathFlagGuard(IRBuilderBase &B) | ||||||||||
338 | : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {} | ||||||||||
339 | |||||||||||
340 | FastMathFlagGuard(const FastMathFlagGuard &) = delete; | ||||||||||
341 | FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete; | ||||||||||
342 | |||||||||||
343 | ~FastMathFlagGuard() { | ||||||||||
344 | Builder.FMF = FMF; | ||||||||||
345 | Builder.DefaultFPMathTag = FPMathTag; | ||||||||||
346 | } | ||||||||||
347 | }; | ||||||||||
348 | |||||||||||
349 | // RAII object that stores the current default operand bundles and restores | ||||||||||
350 | // them when the object is destroyed. | ||||||||||
351 | class OperandBundlesGuard { | ||||||||||
352 | IRBuilderBase &Builder; | ||||||||||
353 | ArrayRef<OperandBundleDef> DefaultOperandBundles; | ||||||||||
354 | |||||||||||
355 | public: | ||||||||||
356 | OperandBundlesGuard(IRBuilderBase &B) | ||||||||||
357 | : Builder(B), DefaultOperandBundles(B.DefaultOperandBundles) {} | ||||||||||
358 | |||||||||||
359 | OperandBundlesGuard(const OperandBundlesGuard &) = delete; | ||||||||||
360 | OperandBundlesGuard &operator=(const OperandBundlesGuard &) = delete; | ||||||||||
361 | |||||||||||
362 | ~OperandBundlesGuard() { | ||||||||||
363 | Builder.DefaultOperandBundles = DefaultOperandBundles; | ||||||||||
364 | } | ||||||||||
365 | }; | ||||||||||
366 | |||||||||||
367 | |||||||||||
368 | //===--------------------------------------------------------------------===// | ||||||||||
369 | // Miscellaneous creation methods. | ||||||||||
370 | //===--------------------------------------------------------------------===// | ||||||||||
371 | |||||||||||
372 | /// Make a new global variable with initializer type i8* | ||||||||||
373 | /// | ||||||||||
374 | /// Make a new global variable with an initializer that has array of i8 type | ||||||||||
375 | /// filled in with the null terminated string value specified. The new global | ||||||||||
376 | /// variable will be marked mergable with any others of the same contents. If | ||||||||||
377 | /// Name is specified, it is the name of the global variable created. | ||||||||||
378 | GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "", | ||||||||||
379 | unsigned AddressSpace = 0); | ||||||||||
380 | |||||||||||
381 | /// Get a constant value representing either true or false. | ||||||||||
382 | ConstantInt *getInt1(bool V) { | ||||||||||
383 | return ConstantInt::get(getInt1Ty(), V); | ||||||||||
384 | } | ||||||||||
385 | |||||||||||
386 | /// Get the constant value for i1 true. | ||||||||||
387 | ConstantInt *getTrue() { | ||||||||||
388 | return ConstantInt::getTrue(Context); | ||||||||||
389 | } | ||||||||||
390 | |||||||||||
391 | /// Get the constant value for i1 false. | ||||||||||
392 | ConstantInt *getFalse() { | ||||||||||
393 | return ConstantInt::getFalse(Context); | ||||||||||
394 | } | ||||||||||
395 | |||||||||||
396 | /// Get a constant 8-bit value. | ||||||||||
397 | ConstantInt *getInt8(uint8_t C) { | ||||||||||
398 | return ConstantInt::get(getInt8Ty(), C); | ||||||||||
399 | } | ||||||||||
400 | |||||||||||
401 | /// Get a constant 16-bit value. | ||||||||||
402 | ConstantInt *getInt16(uint16_t C) { | ||||||||||
403 | return ConstantInt::get(getInt16Ty(), C); | ||||||||||
404 | } | ||||||||||
405 | |||||||||||
406 | /// Get a constant 32-bit value. | ||||||||||
407 | ConstantInt *getInt32(uint32_t C) { | ||||||||||
408 | return ConstantInt::get(getInt32Ty(), C); | ||||||||||
409 | } | ||||||||||
410 | |||||||||||
411 | /// Get a constant 64-bit value. | ||||||||||
412 | ConstantInt *getInt64(uint64_t C) { | ||||||||||
413 | return ConstantInt::get(getInt64Ty(), C); | ||||||||||
414 | } | ||||||||||
415 | |||||||||||
416 | /// Get a constant N-bit value, zero extended or truncated from | ||||||||||
417 | /// a 64-bit value. | ||||||||||
418 | ConstantInt *getIntN(unsigned N, uint64_t C) { | ||||||||||
419 | return ConstantInt::get(getIntNTy(N), C); | ||||||||||
420 | } | ||||||||||
421 | |||||||||||
422 | /// Get a constant integer value. | ||||||||||
423 | ConstantInt *getInt(const APInt &AI) { | ||||||||||
424 | return ConstantInt::get(Context, AI); | ||||||||||
425 | } | ||||||||||
426 | |||||||||||
427 | //===--------------------------------------------------------------------===// | ||||||||||
428 | // Type creation methods | ||||||||||
429 | //===--------------------------------------------------------------------===// | ||||||||||
430 | |||||||||||
431 | /// Fetch the type representing a single bit | ||||||||||
432 | IntegerType *getInt1Ty() { | ||||||||||
433 | return Type::getInt1Ty(Context); | ||||||||||
434 | } | ||||||||||
435 | |||||||||||
436 | /// Fetch the type representing an 8-bit integer. | ||||||||||
437 | IntegerType *getInt8Ty() { | ||||||||||
438 | return Type::getInt8Ty(Context); | ||||||||||
439 | } | ||||||||||
440 | |||||||||||
441 | /// Fetch the type representing a 16-bit integer. | ||||||||||
442 | IntegerType *getInt16Ty() { | ||||||||||
443 | return Type::getInt16Ty(Context); | ||||||||||
444 | } | ||||||||||
445 | |||||||||||
446 | /// Fetch the type representing a 32-bit integer. | ||||||||||
447 | IntegerType *getInt32Ty() { | ||||||||||
448 | return Type::getInt32Ty(Context); | ||||||||||
449 | } | ||||||||||
450 | |||||||||||
451 | /// Fetch the type representing a 64-bit integer. | ||||||||||
452 | IntegerType *getInt64Ty() { | ||||||||||
453 | return Type::getInt64Ty(Context); | ||||||||||
454 | } | ||||||||||
455 | |||||||||||
456 | /// Fetch the type representing a 128-bit integer. | ||||||||||
457 | IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); } | ||||||||||
458 | |||||||||||
459 | /// Fetch the type representing an N-bit integer. | ||||||||||
460 | IntegerType *getIntNTy(unsigned N) { | ||||||||||
461 | return Type::getIntNTy(Context, N); | ||||||||||
462 | } | ||||||||||
463 | |||||||||||
464 | /// Fetch the type representing a 16-bit floating point value. | ||||||||||
465 | Type *getHalfTy() { | ||||||||||
466 | return Type::getHalfTy(Context); | ||||||||||
467 | } | ||||||||||
468 | |||||||||||
469 | /// Fetch the type representing a 32-bit floating point value. | ||||||||||
470 | Type *getFloatTy() { | ||||||||||
471 | return Type::getFloatTy(Context); | ||||||||||
472 | } | ||||||||||
473 | |||||||||||
474 | /// Fetch the type representing a 64-bit floating point value. | ||||||||||
475 | Type *getDoubleTy() { | ||||||||||
476 | return Type::getDoubleTy(Context); | ||||||||||
477 | } | ||||||||||
478 | |||||||||||
479 | /// Fetch the type representing void. | ||||||||||
480 | Type *getVoidTy() { | ||||||||||
481 | return Type::getVoidTy(Context); | ||||||||||
482 | } | ||||||||||
483 | |||||||||||
484 | /// Fetch the type representing a pointer to an 8-bit integer value. | ||||||||||
485 | PointerType *getInt8PtrTy(unsigned AddrSpace = 0) { | ||||||||||
486 | return Type::getInt8PtrTy(Context, AddrSpace); | ||||||||||
487 | } | ||||||||||
488 | |||||||||||
489 | /// Fetch the type representing a pointer to an integer value. | ||||||||||
490 | IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) { | ||||||||||
491 | return DL.getIntPtrType(Context, AddrSpace); | ||||||||||
492 | } | ||||||||||
493 | |||||||||||
494 | //===--------------------------------------------------------------------===// | ||||||||||
495 | // Intrinsic creation methods | ||||||||||
496 | //===--------------------------------------------------------------------===// | ||||||||||
497 | |||||||||||
498 | /// Create and insert a memset to the specified pointer and the | ||||||||||
499 | /// specified value. | ||||||||||
500 | /// | ||||||||||
501 | /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is | ||||||||||
502 | /// specified, it will be added to the instruction. Likewise with alias.scope | ||||||||||
503 | /// and noalias tags. | ||||||||||
504 | CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, | ||||||||||
505 | MaybeAlign Align, bool isVolatile = false, | ||||||||||
506 | MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, | ||||||||||
507 | MDNode *NoAliasTag = nullptr) { | ||||||||||
508 | return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, | ||||||||||
509 | TBAATag, ScopeTag, NoAliasTag); | ||||||||||
510 | } | ||||||||||
511 | |||||||||||
512 | CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, MaybeAlign Align, | ||||||||||
513 | bool isVolatile = false, MDNode *TBAATag = nullptr, | ||||||||||
514 | MDNode *ScopeTag = nullptr, | ||||||||||
515 | MDNode *NoAliasTag = nullptr); | ||||||||||
516 | |||||||||||
517 | /// Create and insert an element unordered-atomic memset of the region of | ||||||||||
518 | /// memory starting at the given pointer to the given value. | ||||||||||
519 | /// | ||||||||||
520 | /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is | ||||||||||
521 | /// specified, it will be added to the instruction. Likewise with alias.scope | ||||||||||
522 | /// and noalias tags. | ||||||||||
523 | /// FIXME: Remove this function once transition to Align is over. | ||||||||||
524 | /// Use the version that takes Align instead of this one. | ||||||||||
525 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value *Val, uint64_t Size, unsigned Alignment, uint32_t ElementSize , MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one" ))) | ||||||||||
526 | CallInst *CreateElementUnorderedAtomicMemSet(CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value *Val, uint64_t Size, unsigned Alignment, uint32_t ElementSize , MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one" ))) | ||||||||||
527 | Value *Ptr, Value *Val, uint64_t Size, unsigned Alignment,CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value *Val, uint64_t Size, unsigned Alignment, uint32_t ElementSize , MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one" ))) | ||||||||||
528 | uint32_t ElementSize, MDNode *TBAATag = nullptr,CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value *Val, uint64_t Size, unsigned Alignment, uint32_t ElementSize , MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one" ))) | ||||||||||
529 | MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr),CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value *Val, uint64_t Size, unsigned Alignment, uint32_t ElementSize , MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one" ))) | ||||||||||
530 | "Use the version that takes Align instead of this one")CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value *Val, uint64_t Size, unsigned Alignment, uint32_t ElementSize , MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one" ))) { | ||||||||||
531 | return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size), | ||||||||||
532 | Align(Alignment), ElementSize, | ||||||||||
533 | TBAATag, ScopeTag, NoAliasTag); | ||||||||||
534 | } | ||||||||||
535 | |||||||||||
536 | CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val, | ||||||||||
537 | uint64_t Size, Align Alignment, | ||||||||||
538 | uint32_t ElementSize, | ||||||||||
539 | MDNode *TBAATag = nullptr, | ||||||||||
540 | MDNode *ScopeTag = nullptr, | ||||||||||
541 | MDNode *NoAliasTag = nullptr) { | ||||||||||
542 | return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size), | ||||||||||
543 | Align(Alignment), ElementSize, | ||||||||||
544 | TBAATag, ScopeTag, NoAliasTag); | ||||||||||
545 | } | ||||||||||
546 | |||||||||||
547 | /// FIXME: Remove this function once transition to Align is over. | ||||||||||
548 | /// Use the version that takes Align instead of this one. | ||||||||||
549 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value *Val, Value *Size, unsigned Alignment, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one" ))) | ||||||||||
550 | CallInst *CreateElementUnorderedAtomicMemSet(CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value *Val, Value *Size, unsigned Alignment, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one" ))) | ||||||||||
551 | Value *Ptr, Value *Val, Value *Size, unsigned Alignment,CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value *Val, Value *Size, unsigned Alignment, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one" ))) | ||||||||||
552 | uint32_t ElementSize, MDNode *TBAATag = nullptr,CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value *Val, Value *Size, unsigned Alignment, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one" ))) | ||||||||||
553 | MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr),CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value *Val, Value *Size, unsigned Alignment, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one" ))) | ||||||||||
554 | "Use the version that takes Align instead of this one")CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value *Val, Value *Size, unsigned Alignment, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one" ))) { | ||||||||||
555 | return CreateElementUnorderedAtomicMemSet(Ptr, Val, Size, Align(Alignment), | ||||||||||
556 | ElementSize, TBAATag, ScopeTag, | ||||||||||
557 | NoAliasTag); | ||||||||||
558 | } | ||||||||||
559 | |||||||||||
560 | CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val, | ||||||||||
561 | Value *Size, Align Alignment, | ||||||||||
562 | uint32_t ElementSize, | ||||||||||
563 | MDNode *TBAATag = nullptr, | ||||||||||
564 | MDNode *ScopeTag = nullptr, | ||||||||||
565 | MDNode *NoAliasTag = nullptr); | ||||||||||
566 | |||||||||||
567 | /// Create and insert a memcpy between the specified pointers. | ||||||||||
568 | /// | ||||||||||
569 | /// If the pointers aren't i8*, they will be converted. If a TBAA tag is | ||||||||||
570 | /// specified, it will be added to the instruction. Likewise with alias.scope | ||||||||||
571 | /// and noalias tags. | ||||||||||
572 | /// FIXME: Remove this function once transition to Align is over. | ||||||||||
573 | /// Use the version that takes MaybeAlign instead of this one. | ||||||||||
574 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value * Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__ ((deprecated("Use the version that takes MaybeAlign instead") )) | ||||||||||
575 | CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value * Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__ ((deprecated("Use the version that takes MaybeAlign instead") )) | ||||||||||
576 | unsigned SrcAlign, uint64_t Size,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value * Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__ ((deprecated("Use the version that takes MaybeAlign instead") )) | ||||||||||
577 | bool isVolatile = false, MDNode *TBAATag = nullptr,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value * Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__ ((deprecated("Use the version that takes MaybeAlign instead") )) | ||||||||||
578 | MDNode *TBAAStructTag = nullptr,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value * Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__ ((deprecated("Use the version that takes MaybeAlign instead") )) | ||||||||||
579 | MDNode *ScopeTag = nullptr,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value * Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__ ((deprecated("Use the version that takes MaybeAlign instead") )) | ||||||||||
580 | MDNode *NoAliasTag = nullptr),CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value * Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__ ((deprecated("Use the version that takes MaybeAlign instead") )) | ||||||||||
581 | "Use the version that takes MaybeAlign instead")CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value * Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__ ((deprecated("Use the version that takes MaybeAlign instead") )) { | ||||||||||
582 | return CreateMemCpy(Dst, MaybeAlign(DstAlign), Src, MaybeAlign(SrcAlign), | ||||||||||
583 | getInt64(Size), isVolatile, TBAATag, TBAAStructTag, | ||||||||||
584 | ScopeTag, NoAliasTag); | ||||||||||
585 | } | ||||||||||
586 | |||||||||||
587 | CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src, | ||||||||||
588 | MaybeAlign SrcAlign, uint64_t Size, | ||||||||||
589 | bool isVolatile = false, MDNode *TBAATag = nullptr, | ||||||||||
590 | MDNode *TBAAStructTag = nullptr, | ||||||||||
591 | MDNode *ScopeTag = nullptr, | ||||||||||
592 | MDNode *NoAliasTag = nullptr) { | ||||||||||
593 | return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, getInt64(Size), | ||||||||||
594 | isVolatile, TBAATag, TBAAStructTag, ScopeTag, | ||||||||||
595 | NoAliasTag); | ||||||||||
596 | } | ||||||||||
597 | |||||||||||
598 | /// FIXME: Remove this function once transition to Align is over. | ||||||||||
599 | /// Use the version that takes MaybeAlign instead of this one. | ||||||||||
600 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value * Src, unsigned SrcAlign, Value *Size, bool isVolatile = false, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__ ((deprecated("Use the version that takes MaybeAlign instead") )) | ||||||||||
601 | CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value * Src, unsigned SrcAlign, Value *Size, bool isVolatile = false, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__ ((deprecated("Use the version that takes MaybeAlign instead") )) | ||||||||||
602 | unsigned SrcAlign, Value *Size,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value * Src, unsigned SrcAlign, Value *Size, bool isVolatile = false, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__ ((deprecated("Use the version that takes MaybeAlign instead") )) | ||||||||||
603 | bool isVolatile = false, MDNode *TBAATag = nullptr,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value * Src, unsigned SrcAlign, Value *Size, bool isVolatile = false, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__ ((deprecated("Use the version that takes MaybeAlign instead") )) | ||||||||||
604 | MDNode *TBAAStructTag = nullptr,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value * Src, unsigned SrcAlign, Value *Size, bool isVolatile = false, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__ ((deprecated("Use the version that takes MaybeAlign instead") )) | ||||||||||
605 | MDNode *ScopeTag = nullptr,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value * Src, unsigned SrcAlign, Value *Size, bool isVolatile = false, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__ ((deprecated("Use the version that takes MaybeAlign instead") )) | ||||||||||
606 | MDNode *NoAliasTag = nullptr),CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value * Src, unsigned SrcAlign, Value *Size, bool isVolatile = false, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__ ((deprecated("Use the version that takes MaybeAlign instead") )) | ||||||||||
607 | "Use the version that takes MaybeAlign instead")CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value * Src, unsigned SrcAlign, Value *Size, bool isVolatile = false, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__ ((deprecated("Use the version that takes MaybeAlign instead") )); | ||||||||||
608 | CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src, | ||||||||||
609 | MaybeAlign SrcAlign, Value *Size, | ||||||||||
610 | bool isVolatile = false, MDNode *TBAATag = nullptr, | ||||||||||
611 | MDNode *TBAAStructTag = nullptr, | ||||||||||
612 | MDNode *ScopeTag = nullptr, | ||||||||||
613 | MDNode *NoAliasTag = nullptr); | ||||||||||
614 | |||||||||||
615 | CallInst *CreateMemCpyInline(Value *Dst, MaybeAlign DstAlign, Value *Src, | ||||||||||
616 | MaybeAlign SrcAlign, Value *Size); | ||||||||||
617 | |||||||||||
618 | /// Create and insert an element unordered-atomic memcpy between the | ||||||||||
619 | /// specified pointers. | ||||||||||
620 | /// | ||||||||||
621 | /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively. | ||||||||||
622 | /// | ||||||||||
623 | /// If the pointers aren't i8*, they will be converted. If a TBAA tag is | ||||||||||
624 | /// specified, it will be added to the instruction. Likewise with alias.scope | ||||||||||
625 | /// and noalias tags. | ||||||||||
626 | CallInst *CreateElementUnorderedAtomicMemCpy( | ||||||||||
627 | Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size, | ||||||||||
628 | uint32_t ElementSize, MDNode *TBAATag = nullptr, | ||||||||||
629 | MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, | ||||||||||
630 | MDNode *NoAliasTag = nullptr); | ||||||||||
631 | |||||||||||
632 | /// FIXME: Remove this function once transition to Align is over. | ||||||||||
633 | /// Use the version that takes Align instead of this one. | ||||||||||
634 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateElementUnorderedAtomicMemCpy(CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
635 | Value *Dst, unsigned DstAlign, Value *Src,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
636 | unsigned SrcAlign, uint64_t Size,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
637 | uint32_t ElementSize, MDNode *TBAATag = nullptr,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
638 | MDNode *TBAAStructTag = nullptr,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
639 | MDNode *ScopeTag = nullptr,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
640 | MDNode *NoAliasTag = nullptr),CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
641 | "Use the version that takes Align instead")CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) { | ||||||||||
642 | return CreateElementUnorderedAtomicMemCpy( | ||||||||||
643 | Dst, Align(DstAlign), Src, Align(SrcAlign), getInt64(Size), ElementSize, | ||||||||||
644 | TBAATag, TBAAStructTag, ScopeTag, NoAliasTag); | ||||||||||
645 | } | ||||||||||
646 | |||||||||||
647 | /// FIXME: Remove this function once transition to Align is over. | ||||||||||
648 | /// Use the version that takes Align instead of this one. | ||||||||||
649 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateElementUnorderedAtomicMemCpy(CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
650 | Value *Dst, unsigned DstAlign, Value *Src,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
651 | unsigned SrcAlign, Value *Size,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
652 | uint32_t ElementSize, MDNode *TBAATag = nullptr,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
653 | MDNode *TBAAStructTag = nullptr,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
654 | MDNode *ScopeTag = nullptr,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
655 | MDNode *NoAliasTag = nullptr),CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
656 | "Use the version that takes Align instead")CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) { | ||||||||||
657 | return CreateElementUnorderedAtomicMemCpy( | ||||||||||
658 | Dst, Align(DstAlign), Src, Align(SrcAlign), Size, ElementSize, TBAATag, | ||||||||||
659 | TBAAStructTag, ScopeTag, NoAliasTag); | ||||||||||
660 | } | ||||||||||
661 | |||||||||||
662 | /// Create and insert a memmove between the specified | ||||||||||
663 | /// pointers. | ||||||||||
664 | /// | ||||||||||
665 | /// If the pointers aren't i8*, they will be converted. If a TBAA tag is | ||||||||||
666 | /// specified, it will be added to the instruction. Likewise with alias.scope | ||||||||||
667 | /// and noalias tags. | ||||||||||
668 | /// FIXME: Remove this function once transition to Align is over. | ||||||||||
669 | /// Use the version that takes MaybeAlign instead of this one. | ||||||||||
670 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign" ))) | ||||||||||
671 | CallInst *CreateMemMove(CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign" ))) | ||||||||||
672 | Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign" ))) | ||||||||||
673 | uint64_t Size, bool isVolatile = false, MDNode *TBAATag = nullptr,CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign" ))) | ||||||||||
674 | MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr),CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign" ))) | ||||||||||
675 | "Use the version that takes MaybeAlign")CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign" ))) { | ||||||||||
676 | return CreateMemMove(Dst, MaybeAlign(DstAlign), Src, MaybeAlign(SrcAlign), | ||||||||||
677 | getInt64(Size), isVolatile, TBAATag, ScopeTag, | ||||||||||
678 | NoAliasTag); | ||||||||||
679 | } | ||||||||||
680 | CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src, | ||||||||||
681 | MaybeAlign SrcAlign, uint64_t Size, | ||||||||||
682 | bool isVolatile = false, MDNode *TBAATag = nullptr, | ||||||||||
683 | MDNode *ScopeTag = nullptr, | ||||||||||
684 | MDNode *NoAliasTag = nullptr) { | ||||||||||
685 | return CreateMemMove(Dst, DstAlign, Src, SrcAlign, getInt64(Size), | ||||||||||
686 | isVolatile, TBAATag, ScopeTag, NoAliasTag); | ||||||||||
687 | } | ||||||||||
688 | /// FIXME: Remove this function once transition to Align is over. | ||||||||||
689 | /// Use the version that takes MaybeAlign instead of this one. | ||||||||||
690 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign" ))) | ||||||||||
691 | CallInst *CreateMemMove(CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign" ))) | ||||||||||
692 | Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign" ))) | ||||||||||
693 | Value *Size, bool isVolatile = false, MDNode *TBAATag = nullptr,CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign" ))) | ||||||||||
694 | MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr),CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign" ))) | ||||||||||
695 | "Use the version that takes MaybeAlign")CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, bool isVolatile = false , MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign" ))) { | ||||||||||
696 | return CreateMemMove(Dst, MaybeAlign(DstAlign), Src, MaybeAlign(SrcAlign), | ||||||||||
697 | Size, isVolatile, TBAATag, ScopeTag, NoAliasTag); | ||||||||||
698 | } | ||||||||||
699 | CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src, | ||||||||||
700 | MaybeAlign SrcAlign, Value *Size, | ||||||||||
701 | bool isVolatile = false, MDNode *TBAATag = nullptr, | ||||||||||
702 | MDNode *ScopeTag = nullptr, | ||||||||||
703 | MDNode *NoAliasTag = nullptr); | ||||||||||
704 | |||||||||||
705 | /// \brief Create and insert an element unordered-atomic memmove between the | ||||||||||
706 | /// specified pointers. | ||||||||||
707 | /// | ||||||||||
708 | /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, | ||||||||||
709 | /// respectively. | ||||||||||
710 | /// | ||||||||||
711 | /// If the pointers aren't i8*, they will be converted. If a TBAA tag is | ||||||||||
712 | /// specified, it will be added to the instruction. Likewise with alias.scope | ||||||||||
713 | /// and noalias tags. | ||||||||||
714 | CallInst *CreateElementUnorderedAtomicMemMove( | ||||||||||
715 | Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size, | ||||||||||
716 | uint32_t ElementSize, MDNode *TBAATag = nullptr, | ||||||||||
717 | MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, | ||||||||||
718 | MDNode *NoAliasTag = nullptr); | ||||||||||
719 | |||||||||||
720 | /// FIXME: Remove this function once transition to Align is over. | ||||||||||
721 | /// Use the version that takes Align instead of this one. | ||||||||||
722 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateElementUnorderedAtomicMemMove(CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
723 | Value *Dst, unsigned DstAlign, Value *Src,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
724 | unsigned SrcAlign, uint64_t Size,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
725 | uint32_t ElementSize, MDNode *TBAATag = nullptr,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
726 | MDNode *TBAAStructTag = nullptr,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
727 | MDNode *ScopeTag = nullptr,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
728 | MDNode *NoAliasTag = nullptr),CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
729 | "Use the version that takes Align instead")CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) { | ||||||||||
730 | return CreateElementUnorderedAtomicMemMove( | ||||||||||
731 | Dst, Align(DstAlign), Src, Align(SrcAlign), getInt64(Size), ElementSize, | ||||||||||
732 | TBAATag, TBAAStructTag, ScopeTag, NoAliasTag); | ||||||||||
733 | } | ||||||||||
734 | |||||||||||
735 | /// FIXME: Remove this function once transition to Align is over. | ||||||||||
736 | /// Use the version that takes Align instead of this one. | ||||||||||
737 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateElementUnorderedAtomicMemMove(CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
738 | Value *Dst, unsigned DstAlign, Value *Src,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
739 | unsigned SrcAlign, Value *Size,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
740 | uint32_t ElementSize, MDNode *TBAATag = nullptr,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
741 | MDNode *TBAAStructTag = nullptr,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
742 | MDNode *ScopeTag = nullptr,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
743 | MDNode *NoAliasTag = nullptr),CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
744 | "Use the version that takes Align instead")CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) { | ||||||||||
745 | return CreateElementUnorderedAtomicMemMove( | ||||||||||
746 | Dst, Align(DstAlign), Src, Align(SrcAlign), Size, ElementSize, TBAATag, | ||||||||||
747 | TBAAStructTag, ScopeTag, NoAliasTag); | ||||||||||
748 | } | ||||||||||
749 | |||||||||||
750 | /// Create a vector fadd reduction intrinsic of the source vector. | ||||||||||
751 | /// The first parameter is a scalar accumulator value for ordered reductions. | ||||||||||
752 | CallInst *CreateFAddReduce(Value *Acc, Value *Src); | ||||||||||
753 | |||||||||||
754 | /// Create a vector fmul reduction intrinsic of the source vector. | ||||||||||
755 | /// The first parameter is a scalar accumulator value for ordered reductions. | ||||||||||
756 | CallInst *CreateFMulReduce(Value *Acc, Value *Src); | ||||||||||
757 | |||||||||||
758 | /// Create a vector int add reduction intrinsic of the source vector. | ||||||||||
759 | CallInst *CreateAddReduce(Value *Src); | ||||||||||
760 | |||||||||||
761 | /// Create a vector int mul reduction intrinsic of the source vector. | ||||||||||
762 | CallInst *CreateMulReduce(Value *Src); | ||||||||||
763 | |||||||||||
764 | /// Create a vector int AND reduction intrinsic of the source vector. | ||||||||||
765 | CallInst *CreateAndReduce(Value *Src); | ||||||||||
766 | |||||||||||
767 | /// Create a vector int OR reduction intrinsic of the source vector. | ||||||||||
768 | CallInst *CreateOrReduce(Value *Src); | ||||||||||
769 | |||||||||||
770 | /// Create a vector int XOR reduction intrinsic of the source vector. | ||||||||||
771 | CallInst *CreateXorReduce(Value *Src); | ||||||||||
772 | |||||||||||
773 | /// Create a vector integer max reduction intrinsic of the source | ||||||||||
774 | /// vector. | ||||||||||
775 | CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false); | ||||||||||
776 | |||||||||||
777 | /// Create a vector integer min reduction intrinsic of the source | ||||||||||
778 | /// vector. | ||||||||||
779 | CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false); | ||||||||||
780 | |||||||||||
781 | /// Create a vector float max reduction intrinsic of the source | ||||||||||
782 | /// vector. | ||||||||||
783 | CallInst *CreateFPMaxReduce(Value *Src, bool NoNaN = false); | ||||||||||
784 | |||||||||||
785 | /// Create a vector float min reduction intrinsic of the source | ||||||||||
786 | /// vector. | ||||||||||
787 | CallInst *CreateFPMinReduce(Value *Src, bool NoNaN = false); | ||||||||||
788 | |||||||||||
789 | /// Create a lifetime.start intrinsic. | ||||||||||
790 | /// | ||||||||||
791 | /// If the pointer isn't i8* it will be converted. | ||||||||||
792 | CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr); | ||||||||||
793 | |||||||||||
794 | /// Create a lifetime.end intrinsic. | ||||||||||
795 | /// | ||||||||||
796 | /// If the pointer isn't i8* it will be converted. | ||||||||||
797 | CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr); | ||||||||||
798 | |||||||||||
799 | /// Create a call to invariant.start intrinsic. | ||||||||||
800 | /// | ||||||||||
801 | /// If the pointer isn't i8* it will be converted. | ||||||||||
802 | CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr); | ||||||||||
803 | |||||||||||
804 | /// Create a call to Masked Load intrinsic | ||||||||||
805 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMaskedLoad(Value *Ptr, unsigned Alignment, Value *Mask, Value *PassThru = nullptr, const Twine &Name = "" ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
806 | CallInst *CreateMaskedLoad(Value *Ptr, unsigned Alignment, Value *Mask,CallInst *CreateMaskedLoad(Value *Ptr, unsigned Alignment, Value *Mask, Value *PassThru = nullptr, const Twine &Name = "" ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
807 | Value *PassThru = nullptr,CallInst *CreateMaskedLoad(Value *Ptr, unsigned Alignment, Value *Mask, Value *PassThru = nullptr, const Twine &Name = "" ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
808 | const Twine &Name = ""),CallInst *CreateMaskedLoad(Value *Ptr, unsigned Alignment, Value *Mask, Value *PassThru = nullptr, const Twine &Name = "" ) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
809 | "Use the version that takes Align instead")CallInst *CreateMaskedLoad(Value *Ptr, unsigned Alignment, Value *Mask, Value *PassThru = nullptr, const Twine &Name = "" ) __attribute__((deprecated("Use the version that takes Align instead" ))) { | ||||||||||
810 | return CreateMaskedLoad(Ptr, assumeAligned(Alignment), Mask, PassThru, | ||||||||||
811 | Name); | ||||||||||
812 | } | ||||||||||
813 | CallInst *CreateMaskedLoad(Value *Ptr, Align Alignment, Value *Mask, | ||||||||||
814 | Value *PassThru = nullptr, const Twine &Name = ""); | ||||||||||
815 | |||||||||||
816 | /// Create a call to Masked Store intrinsic | ||||||||||
817 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMaskedStore(Value *Val, Value *Ptr,CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Alignment , Value *Mask) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
818 | unsigned Alignment,CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Alignment , Value *Mask) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
819 | Value *Mask),CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Alignment , Value *Mask) __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
820 | "Use the version that takes Align instead")CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Alignment , Value *Mask) __attribute__((deprecated("Use the version that takes Align instead" ))) { | ||||||||||
821 | return CreateMaskedStore(Val, Ptr, assumeAligned(Alignment), Mask); | ||||||||||
822 | } | ||||||||||
823 | |||||||||||
824 | CallInst *CreateMaskedStore(Value *Val, Value *Ptr, Align Alignment, | ||||||||||
825 | Value *Mask); | ||||||||||
826 | |||||||||||
827 | /// Create a call to Masked Gather intrinsic | ||||||||||
828 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment, Value *Mask = nullptr, Value *PassThru = nullptr, const Twine &Name = "") __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
829 | CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment,CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment, Value *Mask = nullptr, Value *PassThru = nullptr, const Twine &Name = "") __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
830 | Value *Mask = nullptr,CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment, Value *Mask = nullptr, Value *PassThru = nullptr, const Twine &Name = "") __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
831 | Value *PassThru = nullptr,CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment, Value *Mask = nullptr, Value *PassThru = nullptr, const Twine &Name = "") __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
832 | const Twine &Name = ""),CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment, Value *Mask = nullptr, Value *PassThru = nullptr, const Twine &Name = "") __attribute__((deprecated("Use the version that takes Align instead" ))) | ||||||||||
833 | "Use the version that takes Align instead")CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment, Value *Mask = nullptr, Value *PassThru = nullptr, const Twine &Name = "") __attribute__((deprecated("Use the version that takes Align instead" ))) { | ||||||||||
834 | return CreateMaskedGather(Ptrs, Align(Alignment), Mask, PassThru, Name); | ||||||||||
835 | } | ||||||||||
836 | |||||||||||
837 | /// Create a call to Masked Gather intrinsic | ||||||||||
838 | CallInst *CreateMaskedGather(Value *Ptrs, Align Alignment, | ||||||||||
839 | Value *Mask = nullptr, Value *PassThru = nullptr, | ||||||||||
840 | const Twine &Name = ""); | ||||||||||
841 | |||||||||||
842 | /// Create a call to Masked Scatter intrinsic | ||||||||||
843 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Alignment, Value *Mask = nullptr) __attribute__((deprecated( "Use the version that takes Align instead"))) | ||||||||||
844 | CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Alignment,CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Alignment, Value *Mask = nullptr) __attribute__((deprecated( "Use the version that takes Align instead"))) | ||||||||||
845 | Value *Mask = nullptr),CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Alignment, Value *Mask = nullptr) __attribute__((deprecated( "Use the version that takes Align instead"))) | ||||||||||
846 | "Use the version that takes Align instead")CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Alignment, Value *Mask = nullptr) __attribute__((deprecated( "Use the version that takes Align instead"))) { | ||||||||||
847 | return CreateMaskedScatter(Val, Ptrs, Align(Alignment), Mask); | ||||||||||
848 | } | ||||||||||
849 | |||||||||||
850 | /// Create a call to Masked Scatter intrinsic | ||||||||||
851 | CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, Align Alignment, | ||||||||||
852 | Value *Mask = nullptr); | ||||||||||
853 | |||||||||||
854 | /// Create an assume intrinsic call that allows the optimizer to | ||||||||||
855 | /// assume that the provided condition will be true. | ||||||||||
856 | CallInst *CreateAssumption(Value *Cond); | ||||||||||
857 | |||||||||||
858 | /// Create a call to the experimental.gc.statepoint intrinsic to | ||||||||||
859 | /// start a new statepoint sequence. | ||||||||||
860 | CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes, | ||||||||||
861 | Value *ActualCallee, | ||||||||||
862 | ArrayRef<Value *> CallArgs, | ||||||||||
863 | ArrayRef<Value *> DeoptArgs, | ||||||||||
864 | ArrayRef<Value *> GCArgs, | ||||||||||
865 | const Twine &Name = ""); | ||||||||||
866 | |||||||||||
867 | /// Create a call to the experimental.gc.statepoint intrinsic to | ||||||||||
868 | /// start a new statepoint sequence. | ||||||||||
869 | CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes, | ||||||||||
870 | Value *ActualCallee, uint32_t Flags, | ||||||||||
871 | ArrayRef<Use> CallArgs, | ||||||||||
872 | ArrayRef<Use> TransitionArgs, | ||||||||||
873 | ArrayRef<Use> DeoptArgs, | ||||||||||
874 | ArrayRef<Value *> GCArgs, | ||||||||||
875 | const Twine &Name = ""); | ||||||||||
876 | |||||||||||
877 | /// Conveninence function for the common case when CallArgs are filled | ||||||||||
878 | /// in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be | ||||||||||
879 | /// .get()'ed to get the Value pointer. | ||||||||||
880 | CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes, | ||||||||||
881 | Value *ActualCallee, ArrayRef<Use> CallArgs, | ||||||||||
882 | ArrayRef<Value *> DeoptArgs, | ||||||||||
883 | ArrayRef<Value *> GCArgs, | ||||||||||
884 | const Twine &Name = ""); | ||||||||||
885 | |||||||||||
886 | /// Create an invoke to the experimental.gc.statepoint intrinsic to | ||||||||||
887 | /// start a new statepoint sequence. | ||||||||||
888 | InvokeInst * | ||||||||||
889 | CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes, | ||||||||||
890 | Value *ActualInvokee, BasicBlock *NormalDest, | ||||||||||
891 | BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs, | ||||||||||
892 | ArrayRef<Value *> DeoptArgs, | ||||||||||
893 | ArrayRef<Value *> GCArgs, const Twine &Name = ""); | ||||||||||
894 | |||||||||||
895 | /// Create an invoke to the experimental.gc.statepoint intrinsic to | ||||||||||
896 | /// start a new statepoint sequence. | ||||||||||
897 | InvokeInst *CreateGCStatepointInvoke( | ||||||||||
898 | uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee, | ||||||||||
899 | BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags, | ||||||||||
900 | ArrayRef<Use> InvokeArgs, ArrayRef<Use> TransitionArgs, | ||||||||||
901 | ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs, | ||||||||||
902 | const Twine &Name = ""); | ||||||||||
903 | |||||||||||
904 | // Convenience function for the common case when CallArgs are filled in using | ||||||||||
905 | // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to | ||||||||||
906 | // get the Value *. | ||||||||||
907 | InvokeInst * | ||||||||||
908 | CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes, | ||||||||||
909 | Value *ActualInvokee, BasicBlock *NormalDest, | ||||||||||
910 | BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs, | ||||||||||
911 | ArrayRef<Value *> DeoptArgs, | ||||||||||
912 | ArrayRef<Value *> GCArgs, const Twine &Name = ""); | ||||||||||
913 | |||||||||||
914 | /// Create a call to the experimental.gc.result intrinsic to extract | ||||||||||
915 | /// the result from a call wrapped in a statepoint. | ||||||||||
916 | CallInst *CreateGCResult(Instruction *Statepoint, | ||||||||||
917 | Type *ResultType, | ||||||||||
918 | const Twine &Name = ""); | ||||||||||
919 | |||||||||||
920 | /// Create a call to the experimental.gc.relocate intrinsics to | ||||||||||
921 | /// project the relocated value of one pointer from the statepoint. | ||||||||||
922 | CallInst *CreateGCRelocate(Instruction *Statepoint, | ||||||||||
923 | int BaseOffset, | ||||||||||
924 | int DerivedOffset, | ||||||||||
925 | Type *ResultType, | ||||||||||
926 | const Twine &Name = ""); | ||||||||||
927 | |||||||||||
928 | /// Create a call to intrinsic \p ID with 1 operand which is mangled on its | ||||||||||
929 | /// type. | ||||||||||
930 | CallInst *CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V, | ||||||||||
931 | Instruction *FMFSource = nullptr, | ||||||||||
932 | const Twine &Name = ""); | ||||||||||
933 | |||||||||||
934 | /// Create a call to intrinsic \p ID with 2 operands which is mangled on the | ||||||||||
935 | /// first type. | ||||||||||
936 | CallInst *CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, | ||||||||||
937 | Instruction *FMFSource = nullptr, | ||||||||||
938 | const Twine &Name = ""); | ||||||||||
939 | |||||||||||
940 | /// Create a call to intrinsic \p ID with \p args, mangled using \p Types. If | ||||||||||
941 | /// \p FMFSource is provided, copy fast-math-flags from that instruction to | ||||||||||
942 | /// the intrinsic. | ||||||||||
943 | CallInst *CreateIntrinsic(Intrinsic::ID ID, ArrayRef<Type *> Types, | ||||||||||
944 | ArrayRef<Value *> Args, | ||||||||||
945 | Instruction *FMFSource = nullptr, | ||||||||||
946 | const Twine &Name = ""); | ||||||||||
947 | |||||||||||
948 | /// Create call to the minnum intrinsic. | ||||||||||
949 | CallInst *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
950 | return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, nullptr, Name); | ||||||||||
951 | } | ||||||||||
952 | |||||||||||
953 | /// Create call to the maxnum intrinsic. | ||||||||||
954 | CallInst *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
955 | return CreateBinaryIntrinsic(Intrinsic::maxnum, LHS, RHS, nullptr, Name); | ||||||||||
956 | } | ||||||||||
957 | |||||||||||
958 | /// Create call to the minimum intrinsic. | ||||||||||
959 | CallInst *CreateMinimum(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
960 | return CreateBinaryIntrinsic(Intrinsic::minimum, LHS, RHS, nullptr, Name); | ||||||||||
961 | } | ||||||||||
962 | |||||||||||
963 | /// Create call to the maximum intrinsic. | ||||||||||
964 | CallInst *CreateMaximum(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
965 | return CreateBinaryIntrinsic(Intrinsic::maximum, LHS, RHS, nullptr, Name); | ||||||||||
966 | } | ||||||||||
967 | |||||||||||
968 | private: | ||||||||||
969 | /// Create a call to a masked intrinsic with given Id. | ||||||||||
970 | CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops, | ||||||||||
971 | ArrayRef<Type *> OverloadedTypes, | ||||||||||
972 | const Twine &Name = ""); | ||||||||||
973 | |||||||||||
974 | Value *getCastedInt8PtrValue(Value *Ptr); | ||||||||||
975 | |||||||||||
976 | //===--------------------------------------------------------------------===// | ||||||||||
977 | // Instruction creation methods: Terminators | ||||||||||
978 | //===--------------------------------------------------------------------===// | ||||||||||
979 | |||||||||||
980 | private: | ||||||||||
981 | /// Helper to add branch weight and unpredictable metadata onto an | ||||||||||
982 | /// instruction. | ||||||||||
983 | /// \returns The annotated instruction. | ||||||||||
984 | template <typename InstTy> | ||||||||||
985 | InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) { | ||||||||||
986 | if (Weights) | ||||||||||
987 | I->setMetadata(LLVMContext::MD_prof, Weights); | ||||||||||
988 | if (Unpredictable) | ||||||||||
989 | I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable); | ||||||||||
990 | return I; | ||||||||||
991 | } | ||||||||||
992 | |||||||||||
993 | public: | ||||||||||
994 | /// Create a 'ret void' instruction. | ||||||||||
995 | ReturnInst *CreateRetVoid() { | ||||||||||
996 | return Insert(ReturnInst::Create(Context)); | ||||||||||
997 | } | ||||||||||
998 | |||||||||||
999 | /// Create a 'ret <val>' instruction. | ||||||||||
1000 | ReturnInst *CreateRet(Value *V) { | ||||||||||
1001 | return Insert(ReturnInst::Create(Context, V)); | ||||||||||
1002 | } | ||||||||||
1003 | |||||||||||
1004 | /// Create a sequence of N insertvalue instructions, | ||||||||||
1005 | /// with one Value from the retVals array each, that build a aggregate | ||||||||||
1006 | /// return value one value at a time, and a ret instruction to return | ||||||||||
1007 | /// the resulting aggregate value. | ||||||||||
1008 | /// | ||||||||||
1009 | /// This is a convenience function for code that uses aggregate return values | ||||||||||
1010 | /// as a vehicle for having multiple return values. | ||||||||||
1011 | ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) { | ||||||||||
1012 | Value *V = UndefValue::get(getCurrentFunctionReturnType()); | ||||||||||
1013 | for (unsigned i = 0; i != N; ++i) | ||||||||||
1014 | V = CreateInsertValue(V, retVals[i], i, "mrv"); | ||||||||||
1015 | return Insert(ReturnInst::Create(Context, V)); | ||||||||||
1016 | } | ||||||||||
1017 | |||||||||||
1018 | /// Create an unconditional 'br label X' instruction. | ||||||||||
1019 | BranchInst *CreateBr(BasicBlock *Dest) { | ||||||||||
1020 | return Insert(BranchInst::Create(Dest)); | ||||||||||
1021 | } | ||||||||||
1022 | |||||||||||
1023 | /// Create a conditional 'br Cond, TrueDest, FalseDest' | ||||||||||
1024 | /// instruction. | ||||||||||
1025 | BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, | ||||||||||
1026 | MDNode *BranchWeights = nullptr, | ||||||||||
1027 | MDNode *Unpredictable = nullptr) { | ||||||||||
1028 | return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond), | ||||||||||
1029 | BranchWeights, Unpredictable)); | ||||||||||
1030 | } | ||||||||||
1031 | |||||||||||
1032 | /// Create a conditional 'br Cond, TrueDest, FalseDest' | ||||||||||
1033 | /// instruction. Copy branch meta data if available. | ||||||||||
1034 | BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, | ||||||||||
1035 | Instruction *MDSrc) { | ||||||||||
1036 | BranchInst *Br = BranchInst::Create(True, False, Cond); | ||||||||||
1037 | if (MDSrc) { | ||||||||||
1038 | unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable, | ||||||||||
1039 | LLVMContext::MD_make_implicit, LLVMContext::MD_dbg}; | ||||||||||
1040 | Br->copyMetadata(*MDSrc, makeArrayRef(&WL[0], 4)); | ||||||||||
1041 | } | ||||||||||
1042 | return Insert(Br); | ||||||||||
1043 | } | ||||||||||
1044 | |||||||||||
1045 | /// Create a switch instruction with the specified value, default dest, | ||||||||||
1046 | /// and with a hint for the number of cases that will be added (for efficient | ||||||||||
1047 | /// allocation). | ||||||||||
1048 | SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10, | ||||||||||
1049 | MDNode *BranchWeights = nullptr, | ||||||||||
1050 | MDNode *Unpredictable = nullptr) { | ||||||||||
1051 | return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases), | ||||||||||
1052 | BranchWeights, Unpredictable)); | ||||||||||
1053 | } | ||||||||||
1054 | |||||||||||
1055 | /// Create an indirect branch instruction with the specified address | ||||||||||
1056 | /// operand, with an optional hint for the number of destinations that will be | ||||||||||
1057 | /// added (for efficient allocation). | ||||||||||
1058 | IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) { | ||||||||||
1059 | return Insert(IndirectBrInst::Create(Addr, NumDests)); | ||||||||||
1060 | } | ||||||||||
1061 | |||||||||||
1062 | /// Create an invoke instruction. | ||||||||||
1063 | InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee, | ||||||||||
1064 | BasicBlock *NormalDest, BasicBlock *UnwindDest, | ||||||||||
1065 | ArrayRef<Value *> Args, | ||||||||||
1066 | ArrayRef<OperandBundleDef> OpBundles, | ||||||||||
1067 | const Twine &Name = "") { | ||||||||||
1068 | return Insert( | ||||||||||
1069 | InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args, OpBundles), | ||||||||||
1070 | Name); | ||||||||||
1071 | } | ||||||||||
1072 | InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee, | ||||||||||
1073 | BasicBlock *NormalDest, BasicBlock *UnwindDest, | ||||||||||
1074 | ArrayRef<Value *> Args = None, | ||||||||||
1075 | const Twine &Name = "") { | ||||||||||
1076 | return Insert(InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args), | ||||||||||
1077 | Name); | ||||||||||
1078 | } | ||||||||||
1079 | |||||||||||
1080 | InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest, | ||||||||||
1081 | BasicBlock *UnwindDest, ArrayRef<Value *> Args, | ||||||||||
1082 | ArrayRef<OperandBundleDef> OpBundles, | ||||||||||
1083 | const Twine &Name = "") { | ||||||||||
1084 | return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(), | ||||||||||
1085 | NormalDest, UnwindDest, Args, OpBundles, Name); | ||||||||||
1086 | } | ||||||||||
1087 | |||||||||||
1088 | InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest, | ||||||||||
1089 | BasicBlock *UnwindDest, | ||||||||||
1090 | ArrayRef<Value *> Args = None, | ||||||||||
1091 | const Twine &Name = "") { | ||||||||||
1092 | return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(), | ||||||||||
1093 | NormalDest, UnwindDest, Args, Name); | ||||||||||
1094 | } | ||||||||||
1095 | |||||||||||
1096 | // Deprecated [opaque pointer types] | ||||||||||
1097 | InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest, | ||||||||||
1098 | BasicBlock *UnwindDest, ArrayRef<Value *> Args, | ||||||||||
1099 | ArrayRef<OperandBundleDef> OpBundles, | ||||||||||
1100 | const Twine &Name = "") { | ||||||||||
1101 | return CreateInvoke( | ||||||||||
1102 | cast<FunctionType>( | ||||||||||
1103 | cast<PointerType>(Callee->getType())->getElementType()), | ||||||||||
1104 | Callee, NormalDest, UnwindDest, Args, OpBundles, Name); | ||||||||||
1105 | } | ||||||||||
1106 | |||||||||||
1107 | // Deprecated [opaque pointer types] | ||||||||||
1108 | InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest, | ||||||||||
1109 | BasicBlock *UnwindDest, | ||||||||||
1110 | ArrayRef<Value *> Args = None, | ||||||||||
1111 | const Twine &Name = "") { | ||||||||||
1112 | return CreateInvoke( | ||||||||||
1113 | cast<FunctionType>( | ||||||||||
1114 | cast<PointerType>(Callee->getType())->getElementType()), | ||||||||||
1115 | Callee, NormalDest, UnwindDest, Args, Name); | ||||||||||
1116 | } | ||||||||||
1117 | |||||||||||
1118 | /// \brief Create a callbr instruction. | ||||||||||
1119 | CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee, | ||||||||||
1120 | BasicBlock *DefaultDest, | ||||||||||
1121 | ArrayRef<BasicBlock *> IndirectDests, | ||||||||||
1122 | ArrayRef<Value *> Args = None, | ||||||||||
1123 | const Twine &Name = "") { | ||||||||||
1124 | return Insert(CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, | ||||||||||
1125 | Args), Name); | ||||||||||
1126 | } | ||||||||||
1127 | CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee, | ||||||||||
1128 | BasicBlock *DefaultDest, | ||||||||||
1129 | ArrayRef<BasicBlock *> IndirectDests, | ||||||||||
1130 | ArrayRef<Value *> Args, | ||||||||||
1131 | ArrayRef<OperandBundleDef> OpBundles, | ||||||||||
1132 | const Twine &Name = "") { | ||||||||||
1133 | return Insert( | ||||||||||
1134 | CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args, | ||||||||||
1135 | OpBundles), Name); | ||||||||||
1136 | } | ||||||||||
1137 | |||||||||||
1138 | CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest, | ||||||||||
1139 | ArrayRef<BasicBlock *> IndirectDests, | ||||||||||
1140 | ArrayRef<Value *> Args = None, | ||||||||||
1141 | const Twine &Name = "") { | ||||||||||
1142 | return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(), | ||||||||||
1143 | DefaultDest, IndirectDests, Args, Name); | ||||||||||
1144 | } | ||||||||||
1145 | CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest, | ||||||||||
1146 | ArrayRef<BasicBlock *> IndirectDests, | ||||||||||
1147 | ArrayRef<Value *> Args, | ||||||||||
1148 | ArrayRef<OperandBundleDef> OpBundles, | ||||||||||
1149 | const Twine &Name = "") { | ||||||||||
1150 | return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(), | ||||||||||
1151 | DefaultDest, IndirectDests, Args, Name); | ||||||||||
1152 | } | ||||||||||
1153 | |||||||||||
1154 | ResumeInst *CreateResume(Value *Exn) { | ||||||||||
1155 | return Insert(ResumeInst::Create(Exn)); | ||||||||||
1156 | } | ||||||||||
1157 | |||||||||||
1158 | CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad, | ||||||||||
1159 | BasicBlock *UnwindBB = nullptr) { | ||||||||||
1160 | return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB)); | ||||||||||
1161 | } | ||||||||||
1162 | |||||||||||
1163 | CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB, | ||||||||||
1164 | unsigned NumHandlers, | ||||||||||
1165 | const Twine &Name = "") { | ||||||||||
1166 | return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers), | ||||||||||
1167 | Name); | ||||||||||
1168 | } | ||||||||||
1169 | |||||||||||
1170 | CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args, | ||||||||||
1171 | const Twine &Name = "") { | ||||||||||
1172 | return Insert(CatchPadInst::Create(ParentPad, Args), Name); | ||||||||||
1173 | } | ||||||||||
1174 | |||||||||||
1175 | CleanupPadInst *CreateCleanupPad(Value *ParentPad, | ||||||||||
1176 | ArrayRef<Value *> Args = None, | ||||||||||
1177 | const Twine &Name = "") { | ||||||||||
1178 | return Insert(CleanupPadInst::Create(ParentPad, Args), Name); | ||||||||||
1179 | } | ||||||||||
1180 | |||||||||||
1181 | CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) { | ||||||||||
1182 | return Insert(CatchReturnInst::Create(CatchPad, BB)); | ||||||||||
1183 | } | ||||||||||
1184 | |||||||||||
1185 | UnreachableInst *CreateUnreachable() { | ||||||||||
1186 | return Insert(new UnreachableInst(Context)); | ||||||||||
1187 | } | ||||||||||
1188 | |||||||||||
1189 | //===--------------------------------------------------------------------===// | ||||||||||
1190 | // Instruction creation methods: Binary Operators | ||||||||||
1191 | //===--------------------------------------------------------------------===// | ||||||||||
1192 | private: | ||||||||||
1193 | BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc, | ||||||||||
1194 | Value *LHS, Value *RHS, | ||||||||||
1195 | const Twine &Name, | ||||||||||
1196 | bool HasNUW, bool HasNSW) { | ||||||||||
1197 | BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name); | ||||||||||
1198 | if (HasNUW) BO->setHasNoUnsignedWrap(); | ||||||||||
1199 | if (HasNSW) BO->setHasNoSignedWrap(); | ||||||||||
1200 | return BO; | ||||||||||
1201 | } | ||||||||||
1202 | |||||||||||
1203 | Instruction *setFPAttrs(Instruction *I, MDNode *FPMD, | ||||||||||
1204 | FastMathFlags FMF) const { | ||||||||||
1205 | if (!FPMD) | ||||||||||
1206 | FPMD = DefaultFPMathTag; | ||||||||||
1207 | if (FPMD) | ||||||||||
1208 | I->setMetadata(LLVMContext::MD_fpmath, FPMD); | ||||||||||
1209 | I->setFastMathFlags(FMF); | ||||||||||
1210 | return I; | ||||||||||
1211 | } | ||||||||||
1212 | |||||||||||
1213 | Value *foldConstant(Instruction::BinaryOps Opc, Value *L, | ||||||||||
1214 | Value *R, const Twine &Name) const { | ||||||||||
1215 | auto *LC = dyn_cast<Constant>(L); | ||||||||||
1216 | auto *RC = dyn_cast<Constant>(R); | ||||||||||
1217 | return (LC && RC) ? Insert(Folder.CreateBinOp(Opc, LC, RC), Name) : nullptr; | ||||||||||
1218 | } | ||||||||||
1219 | |||||||||||
1220 | Value *getConstrainedFPRounding(Optional<fp::RoundingMode> Rounding) { | ||||||||||
1221 | fp::RoundingMode UseRounding = DefaultConstrainedRounding; | ||||||||||
1222 | |||||||||||
1223 | if (Rounding.hasValue()) | ||||||||||
1224 | UseRounding = Rounding.getValue(); | ||||||||||
1225 | |||||||||||
1226 | Optional<StringRef> RoundingStr = RoundingModeToStr(UseRounding); | ||||||||||
1227 | assert(RoundingStr.hasValue() && "Garbage strict rounding mode!")((RoundingStr.hasValue() && "Garbage strict rounding mode!" ) ? static_cast<void> (0) : __assert_fail ("RoundingStr.hasValue() && \"Garbage strict rounding mode!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 1227, __PRETTY_FUNCTION__)); | ||||||||||
1228 | auto *RoundingMDS = MDString::get(Context, RoundingStr.getValue()); | ||||||||||
1229 | |||||||||||
1230 | return MetadataAsValue::get(Context, RoundingMDS); | ||||||||||
1231 | } | ||||||||||
1232 | |||||||||||
1233 | Value *getConstrainedFPExcept(Optional<fp::ExceptionBehavior> Except) { | ||||||||||
1234 | fp::ExceptionBehavior UseExcept = DefaultConstrainedExcept; | ||||||||||
1235 | |||||||||||
1236 | if (Except.hasValue()) | ||||||||||
1237 | UseExcept = Except.getValue(); | ||||||||||
1238 | |||||||||||
1239 | Optional<StringRef> ExceptStr = ExceptionBehaviorToStr(UseExcept); | ||||||||||
1240 | assert(ExceptStr.hasValue() && "Garbage strict exception behavior!")((ExceptStr.hasValue() && "Garbage strict exception behavior!" ) ? static_cast<void> (0) : __assert_fail ("ExceptStr.hasValue() && \"Garbage strict exception behavior!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 1240, __PRETTY_FUNCTION__)); | ||||||||||
1241 | auto *ExceptMDS = MDString::get(Context, ExceptStr.getValue()); | ||||||||||
1242 | |||||||||||
1243 | return MetadataAsValue::get(Context, ExceptMDS); | ||||||||||
1244 | } | ||||||||||
1245 | |||||||||||
1246 | Value *getConstrainedFPPredicate(CmpInst::Predicate Predicate) { | ||||||||||
1247 | assert(CmpInst::isFPPredicate(Predicate) &&((CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst ::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && "Invalid constrained FP comparison predicate!") ? static_cast <void> (0) : __assert_fail ("CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && \"Invalid constrained FP comparison predicate!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 1250, __PRETTY_FUNCTION__)) | ||||||||||
1248 | Predicate != CmpInst::FCMP_FALSE &&((CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst ::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && "Invalid constrained FP comparison predicate!") ? static_cast <void> (0) : __assert_fail ("CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && \"Invalid constrained FP comparison predicate!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 1250, __PRETTY_FUNCTION__)) | ||||||||||
1249 | Predicate != CmpInst::FCMP_TRUE &&((CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst ::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && "Invalid constrained FP comparison predicate!") ? static_cast <void> (0) : __assert_fail ("CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && \"Invalid constrained FP comparison predicate!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 1250, __PRETTY_FUNCTION__)) | ||||||||||
1250 | "Invalid constrained FP comparison predicate!")((CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst ::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && "Invalid constrained FP comparison predicate!") ? static_cast <void> (0) : __assert_fail ("CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && \"Invalid constrained FP comparison predicate!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 1250, __PRETTY_FUNCTION__)); | ||||||||||
1251 | |||||||||||
1252 | StringRef PredicateStr = CmpInst::getPredicateName(Predicate); | ||||||||||
1253 | auto *PredicateMDS = MDString::get(Context, PredicateStr); | ||||||||||
1254 | |||||||||||
1255 | return MetadataAsValue::get(Context, PredicateMDS); | ||||||||||
1256 | } | ||||||||||
1257 | |||||||||||
1258 | public: | ||||||||||
1259 | Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
1260 | bool HasNUW = false, bool HasNSW = false) { | ||||||||||
1261 | if (auto *LC = dyn_cast<Constant>(LHS)) | ||||||||||
1262 | if (auto *RC = dyn_cast<Constant>(RHS)) | ||||||||||
1263 | return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name); | ||||||||||
1264 | return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name, | ||||||||||
1265 | HasNUW, HasNSW); | ||||||||||
1266 | } | ||||||||||
1267 | |||||||||||
1268 | Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
1269 | return CreateAdd(LHS, RHS, Name, false, true); | ||||||||||
1270 | } | ||||||||||
1271 | |||||||||||
1272 | Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
1273 | return CreateAdd(LHS, RHS, Name, true, false); | ||||||||||
1274 | } | ||||||||||
1275 | |||||||||||
1276 | Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
1277 | bool HasNUW = false, bool HasNSW = false) { | ||||||||||
1278 | if (auto *LC = dyn_cast<Constant>(LHS)) | ||||||||||
1279 | if (auto *RC = dyn_cast<Constant>(RHS)) | ||||||||||
1280 | return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name); | ||||||||||
1281 | return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name, | ||||||||||
1282 | HasNUW, HasNSW); | ||||||||||
1283 | } | ||||||||||
1284 | |||||||||||
1285 | Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
1286 | return CreateSub(LHS, RHS, Name, false, true); | ||||||||||
1287 | } | ||||||||||
1288 | |||||||||||
1289 | Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
1290 | return CreateSub(LHS, RHS, Name, true, false); | ||||||||||
1291 | } | ||||||||||
1292 | |||||||||||
1293 | Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
1294 | bool HasNUW = false, bool HasNSW = false) { | ||||||||||
1295 | if (auto *LC = dyn_cast<Constant>(LHS)) | ||||||||||
1296 | if (auto *RC = dyn_cast<Constant>(RHS)) | ||||||||||
1297 | return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name); | ||||||||||
1298 | return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name, | ||||||||||
1299 | HasNUW, HasNSW); | ||||||||||
1300 | } | ||||||||||
1301 | |||||||||||
1302 | Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
1303 | return CreateMul(LHS, RHS, Name, false, true); | ||||||||||
1304 | } | ||||||||||
1305 | |||||||||||
1306 | Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
1307 | return CreateMul(LHS, RHS, Name, true, false); | ||||||||||
1308 | } | ||||||||||
1309 | |||||||||||
1310 | Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
1311 | bool isExact = false) { | ||||||||||
1312 | if (auto *LC = dyn_cast<Constant>(LHS)) | ||||||||||
1313 | if (auto *RC = dyn_cast<Constant>(RHS)) | ||||||||||
1314 | return Insert(Folder.CreateUDiv(LC, RC, isExact), Name); | ||||||||||
1315 | if (!isExact) | ||||||||||
1316 | return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name); | ||||||||||
1317 | return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name); | ||||||||||
1318 | } | ||||||||||
1319 | |||||||||||
1320 | Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
1321 | return CreateUDiv(LHS, RHS, Name, true); | ||||||||||
1322 | } | ||||||||||
1323 | |||||||||||
1324 | Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
1325 | bool isExact = false) { | ||||||||||
1326 | if (auto *LC = dyn_cast<Constant>(LHS)) | ||||||||||
1327 | if (auto *RC = dyn_cast<Constant>(RHS)) | ||||||||||
1328 | return Insert(Folder.CreateSDiv(LC, RC, isExact), Name); | ||||||||||
1329 | if (!isExact) | ||||||||||
1330 | return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name); | ||||||||||
1331 | return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name); | ||||||||||
1332 | } | ||||||||||
1333 | |||||||||||
1334 | Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
1335 | return CreateSDiv(LHS, RHS, Name, true); | ||||||||||
1336 | } | ||||||||||
1337 | |||||||||||
1338 | Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
1339 | if (Value *V = foldConstant(Instruction::URem, LHS, RHS, Name)) return V; | ||||||||||
1340 | return Insert(BinaryOperator::CreateURem(LHS, RHS), Name); | ||||||||||
1341 | } | ||||||||||
1342 | |||||||||||
1343 | Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
1344 | if (Value *V = foldConstant(Instruction::SRem, LHS, RHS, Name)) return V; | ||||||||||
1345 | return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name); | ||||||||||
1346 | } | ||||||||||
1347 | |||||||||||
1348 | Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
1349 | bool HasNUW = false, bool HasNSW = false) { | ||||||||||
1350 | if (auto *LC = dyn_cast<Constant>(LHS)) | ||||||||||
1351 | if (auto *RC = dyn_cast<Constant>(RHS)) | ||||||||||
1352 | return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name); | ||||||||||
1353 | return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name, | ||||||||||
1354 | HasNUW, HasNSW); | ||||||||||
1355 | } | ||||||||||
1356 | |||||||||||
1357 | Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "", | ||||||||||
1358 | bool HasNUW = false, bool HasNSW = false) { | ||||||||||
1359 | return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name, | ||||||||||
1360 | HasNUW, HasNSW); | ||||||||||
1361 | } | ||||||||||
1362 | |||||||||||
1363 | Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "", | ||||||||||
1364 | bool HasNUW = false, bool HasNSW = false) { | ||||||||||
1365 | return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name, | ||||||||||
1366 | HasNUW, HasNSW); | ||||||||||
1367 | } | ||||||||||
1368 | |||||||||||
1369 | Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
1370 | bool isExact = false) { | ||||||||||
1371 | if (auto *LC = dyn_cast<Constant>(LHS)) | ||||||||||
1372 | if (auto *RC = dyn_cast<Constant>(RHS)) | ||||||||||
1373 | return Insert(Folder.CreateLShr(LC, RC, isExact), Name); | ||||||||||
1374 | if (!isExact) | ||||||||||
1375 | return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name); | ||||||||||
1376 | return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name); | ||||||||||
1377 | } | ||||||||||
1378 | |||||||||||
1379 | Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "", | ||||||||||
1380 | bool isExact = false) { | ||||||||||
1381 | return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); | ||||||||||
1382 | } | ||||||||||
1383 | |||||||||||
1384 | Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "", | ||||||||||
1385 | bool isExact = false) { | ||||||||||
1386 | return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); | ||||||||||
1387 | } | ||||||||||
1388 | |||||||||||
1389 | Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
1390 | bool isExact = false) { | ||||||||||
1391 | if (auto *LC = dyn_cast<Constant>(LHS)) | ||||||||||
1392 | if (auto *RC = dyn_cast<Constant>(RHS)) | ||||||||||
1393 | return Insert(Folder.CreateAShr(LC, RC, isExact), Name); | ||||||||||
1394 | if (!isExact) | ||||||||||
1395 | return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name); | ||||||||||
1396 | return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name); | ||||||||||
1397 | } | ||||||||||
1398 | |||||||||||
1399 | Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "", | ||||||||||
1400 | bool isExact = false) { | ||||||||||
1401 | return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); | ||||||||||
1402 | } | ||||||||||
1403 | |||||||||||
1404 | Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "", | ||||||||||
1405 | bool isExact = false) { | ||||||||||
1406 | return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); | ||||||||||
1407 | } | ||||||||||
1408 | |||||||||||
1409 | Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
1410 | if (auto *RC = dyn_cast<Constant>(RHS)) { | ||||||||||
1411 | if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isMinusOne()) | ||||||||||
1412 | return LHS; // LHS & -1 -> LHS | ||||||||||
1413 | if (auto *LC = dyn_cast<Constant>(LHS)) | ||||||||||
1414 | return Insert(Folder.CreateAnd(LC, RC), Name); | ||||||||||
1415 | } | ||||||||||
1416 | return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name); | ||||||||||
1417 | } | ||||||||||
1418 | |||||||||||
1419 | Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") { | ||||||||||
1420 | return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name); | ||||||||||
1421 | } | ||||||||||
1422 | |||||||||||
1423 | Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") { | ||||||||||
1424 | return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name); | ||||||||||
1425 | } | ||||||||||
1426 | |||||||||||
1427 | Value *CreateAnd(ArrayRef<Value*> Ops) { | ||||||||||
1428 | assert(!Ops.empty())((!Ops.empty()) ? static_cast<void> (0) : __assert_fail ("!Ops.empty()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 1428, __PRETTY_FUNCTION__)); | ||||||||||
1429 | Value *Accum = Ops[0]; | ||||||||||
1430 | for (unsigned i = 1; i < Ops.size(); i++) | ||||||||||
1431 | Accum = CreateAnd(Accum, Ops[i]); | ||||||||||
1432 | return Accum; | ||||||||||
1433 | } | ||||||||||
1434 | |||||||||||
1435 | Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
1436 | if (auto *RC = dyn_cast<Constant>(RHS)) { | ||||||||||
1437 | if (RC->isNullValue()) | ||||||||||
1438 | return LHS; // LHS | 0 -> LHS | ||||||||||
1439 | if (auto *LC = dyn_cast<Constant>(LHS)) | ||||||||||
1440 | return Insert(Folder.CreateOr(LC, RC), Name); | ||||||||||
1441 | } | ||||||||||
1442 | return Insert(BinaryOperator::CreateOr(LHS, RHS), Name); | ||||||||||
1443 | } | ||||||||||
1444 | |||||||||||
1445 | Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") { | ||||||||||
1446 | return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name); | ||||||||||
1447 | } | ||||||||||
1448 | |||||||||||
1449 | Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") { | ||||||||||
1450 | return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name); | ||||||||||
1451 | } | ||||||||||
1452 | |||||||||||
1453 | Value *CreateOr(ArrayRef<Value*> Ops) { | ||||||||||
1454 | assert(!Ops.empty())((!Ops.empty()) ? static_cast<void> (0) : __assert_fail ("!Ops.empty()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 1454, __PRETTY_FUNCTION__)); | ||||||||||
1455 | Value *Accum = Ops[0]; | ||||||||||
1456 | for (unsigned i = 1; i < Ops.size(); i++) | ||||||||||
1457 | Accum = CreateOr(Accum, Ops[i]); | ||||||||||
1458 | return Accum; | ||||||||||
1459 | } | ||||||||||
1460 | |||||||||||
1461 | Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
1462 | if (Value *V = foldConstant(Instruction::Xor, LHS, RHS, Name)) return V; | ||||||||||
1463 | return Insert(BinaryOperator::CreateXor(LHS, RHS), Name); | ||||||||||
1464 | } | ||||||||||
1465 | |||||||||||
1466 | Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") { | ||||||||||
1467 | return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name); | ||||||||||
1468 | } | ||||||||||
1469 | |||||||||||
1470 | Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") { | ||||||||||
1471 | return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name); | ||||||||||
1472 | } | ||||||||||
1473 | |||||||||||
1474 | Value *CreateFAdd(Value *L, Value *R, const Twine &Name = "", | ||||||||||
1475 | MDNode *FPMD = nullptr) { | ||||||||||
1476 | if (IsFPConstrained) | ||||||||||
1477 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd, | ||||||||||
1478 | L, R, nullptr, Name, FPMD); | ||||||||||
1479 | |||||||||||
1480 | if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V; | ||||||||||
1481 | Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD, FMF); | ||||||||||
1482 | return Insert(I, Name); | ||||||||||
1483 | } | ||||||||||
1484 | |||||||||||
1485 | /// Copy fast-math-flags from an instruction rather than using the builder's | ||||||||||
1486 | /// default FMF. | ||||||||||
1487 | Value *CreateFAddFMF(Value *L, Value *R, Instruction *FMFSource, | ||||||||||
1488 | const Twine &Name = "") { | ||||||||||
1489 | if (IsFPConstrained) | ||||||||||
1490 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd, | ||||||||||
1491 | L, R, FMFSource, Name); | ||||||||||
1492 | |||||||||||
1493 | if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V; | ||||||||||
1494 | Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), nullptr, | ||||||||||
1495 | FMFSource->getFastMathFlags()); | ||||||||||
1496 | return Insert(I, Name); | ||||||||||
1497 | } | ||||||||||
1498 | |||||||||||
1499 | Value *CreateFSub(Value *L, Value *R, const Twine &Name = "", | ||||||||||
1500 | MDNode *FPMD = nullptr) { | ||||||||||
1501 | if (IsFPConstrained) | ||||||||||
1502 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub, | ||||||||||
1503 | L, R, nullptr, Name, FPMD); | ||||||||||
1504 | |||||||||||
1505 | if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V; | ||||||||||
1506 | Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD, FMF); | ||||||||||
1507 | return Insert(I, Name); | ||||||||||
1508 | } | ||||||||||
1509 | |||||||||||
1510 | /// Copy fast-math-flags from an instruction rather than using the builder's | ||||||||||
1511 | /// default FMF. | ||||||||||
1512 | Value *CreateFSubFMF(Value *L, Value *R, Instruction *FMFSource, | ||||||||||
1513 | const Twine &Name = "") { | ||||||||||
1514 | if (IsFPConstrained) | ||||||||||
1515 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub, | ||||||||||
1516 | L, R, FMFSource, Name); | ||||||||||
1517 | |||||||||||
1518 | if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V; | ||||||||||
1519 | Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), nullptr, | ||||||||||
1520 | FMFSource->getFastMathFlags()); | ||||||||||
1521 | return Insert(I, Name); | ||||||||||
1522 | } | ||||||||||
1523 | |||||||||||
1524 | Value *CreateFMul(Value *L, Value *R, const Twine &Name = "", | ||||||||||
1525 | MDNode *FPMD = nullptr) { | ||||||||||
1526 | if (IsFPConstrained) | ||||||||||
1527 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul, | ||||||||||
1528 | L, R, nullptr, Name, FPMD); | ||||||||||
1529 | |||||||||||
1530 | if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V; | ||||||||||
1531 | Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD, FMF); | ||||||||||
1532 | return Insert(I, Name); | ||||||||||
1533 | } | ||||||||||
1534 | |||||||||||
1535 | /// Copy fast-math-flags from an instruction rather than using the builder's | ||||||||||
1536 | /// default FMF. | ||||||||||
1537 | Value *CreateFMulFMF(Value *L, Value *R, Instruction *FMFSource, | ||||||||||
1538 | const Twine &Name = "") { | ||||||||||
1539 | if (IsFPConstrained) | ||||||||||
1540 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul, | ||||||||||
1541 | L, R, FMFSource, Name); | ||||||||||
1542 | |||||||||||
1543 | if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V; | ||||||||||
1544 | Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), nullptr, | ||||||||||
1545 | FMFSource->getFastMathFlags()); | ||||||||||
1546 | return Insert(I, Name); | ||||||||||
1547 | } | ||||||||||
1548 | |||||||||||
1549 | Value *CreateFDiv(Value *L, Value *R, const Twine &Name = "", | ||||||||||
1550 | MDNode *FPMD = nullptr) { | ||||||||||
1551 | if (IsFPConstrained) | ||||||||||
1552 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv, | ||||||||||
1553 | L, R, nullptr, Name, FPMD); | ||||||||||
1554 | |||||||||||
1555 | if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V; | ||||||||||
1556 | Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD, FMF); | ||||||||||
1557 | return Insert(I, Name); | ||||||||||
1558 | } | ||||||||||
1559 | |||||||||||
1560 | /// Copy fast-math-flags from an instruction rather than using the builder's | ||||||||||
1561 | /// default FMF. | ||||||||||
1562 | Value *CreateFDivFMF(Value *L, Value *R, Instruction *FMFSource, | ||||||||||
1563 | const Twine &Name = "") { | ||||||||||
1564 | if (IsFPConstrained) | ||||||||||
1565 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv, | ||||||||||
1566 | L, R, FMFSource, Name); | ||||||||||
1567 | |||||||||||
1568 | if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V; | ||||||||||
1569 | Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), nullptr, | ||||||||||
1570 | FMFSource->getFastMathFlags()); | ||||||||||
1571 | return Insert(I, Name); | ||||||||||
1572 | } | ||||||||||
1573 | |||||||||||
1574 | Value *CreateFRem(Value *L, Value *R, const Twine &Name = "", | ||||||||||
1575 | MDNode *FPMD = nullptr) { | ||||||||||
1576 | if (IsFPConstrained) | ||||||||||
1577 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem, | ||||||||||
1578 | L, R, nullptr, Name, FPMD); | ||||||||||
1579 | |||||||||||
1580 | if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V; | ||||||||||
1581 | Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD, FMF); | ||||||||||
1582 | return Insert(I, Name); | ||||||||||
1583 | } | ||||||||||
1584 | |||||||||||
1585 | /// Copy fast-math-flags from an instruction rather than using the builder's | ||||||||||
1586 | /// default FMF. | ||||||||||
1587 | Value *CreateFRemFMF(Value *L, Value *R, Instruction *FMFSource, | ||||||||||
1588 | const Twine &Name = "") { | ||||||||||
1589 | if (IsFPConstrained) | ||||||||||
1590 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem, | ||||||||||
1591 | L, R, FMFSource, Name); | ||||||||||
1592 | |||||||||||
1593 | if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V; | ||||||||||
1594 | Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), nullptr, | ||||||||||
1595 | FMFSource->getFastMathFlags()); | ||||||||||
1596 | return Insert(I, Name); | ||||||||||
1597 | } | ||||||||||
1598 | |||||||||||
1599 | Value *CreateBinOp(Instruction::BinaryOps Opc, | ||||||||||
1600 | Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
1601 | MDNode *FPMathTag = nullptr) { | ||||||||||
1602 | if (Value *V = foldConstant(Opc, LHS, RHS, Name)) return V; | ||||||||||
1603 | Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS); | ||||||||||
1604 | if (isa<FPMathOperator>(BinOp)) | ||||||||||
1605 | setFPAttrs(BinOp, FPMathTag, FMF); | ||||||||||
1606 | return Insert(BinOp, Name); | ||||||||||
1607 | } | ||||||||||
1608 | |||||||||||
1609 | CallInst *CreateConstrainedFPBinOp( | ||||||||||
1610 | Intrinsic::ID ID, Value *L, Value *R, Instruction *FMFSource = nullptr, | ||||||||||
1611 | const Twine &Name = "", MDNode *FPMathTag = nullptr, | ||||||||||
1612 | Optional<fp::RoundingMode> Rounding = None, | ||||||||||
1613 | Optional<fp::ExceptionBehavior> Except = None) { | ||||||||||
1614 | Value *RoundingV = getConstrainedFPRounding(Rounding); | ||||||||||
1615 | Value *ExceptV = getConstrainedFPExcept(Except); | ||||||||||
1616 | |||||||||||
1617 | FastMathFlags UseFMF = FMF; | ||||||||||
1618 | if (FMFSource) | ||||||||||
1619 | UseFMF = FMFSource->getFastMathFlags(); | ||||||||||
1620 | |||||||||||
1621 | CallInst *C = CreateIntrinsic(ID, {L->getType()}, | ||||||||||
1622 | {L, R, RoundingV, ExceptV}, nullptr, Name); | ||||||||||
1623 | setConstrainedFPCallAttr(C); | ||||||||||
1624 | setFPAttrs(C, FPMathTag, UseFMF); | ||||||||||
1625 | return C; | ||||||||||
1626 | } | ||||||||||
1627 | |||||||||||
1628 | Value *CreateNeg(Value *V, const Twine &Name = "", | ||||||||||
1629 | bool HasNUW = false, bool HasNSW = false) { | ||||||||||
1630 | if (auto *VC = dyn_cast<Constant>(V)) | ||||||||||
1631 | return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name); | ||||||||||
1632 | BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name); | ||||||||||
1633 | if (HasNUW) BO->setHasNoUnsignedWrap(); | ||||||||||
1634 | if (HasNSW) BO->setHasNoSignedWrap(); | ||||||||||
1635 | return BO; | ||||||||||
1636 | } | ||||||||||
1637 | |||||||||||
1638 | Value *CreateNSWNeg(Value *V, const Twine &Name = "") { | ||||||||||
1639 | return CreateNeg(V, Name, false, true); | ||||||||||
1640 | } | ||||||||||
1641 | |||||||||||
1642 | Value *CreateNUWNeg(Value *V, const Twine &Name = "") { | ||||||||||
1643 | return CreateNeg(V, Name, true, false); | ||||||||||
1644 | } | ||||||||||
1645 | |||||||||||
1646 | Value *CreateFNeg(Value *V, const Twine &Name = "", | ||||||||||
1647 | MDNode *FPMathTag = nullptr) { | ||||||||||
1648 | if (auto *VC = dyn_cast<Constant>(V)) | ||||||||||
1649 | return Insert(Folder.CreateFNeg(VC), Name); | ||||||||||
1650 | return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), FPMathTag, FMF), | ||||||||||
1651 | Name); | ||||||||||
1652 | } | ||||||||||
1653 | |||||||||||
1654 | /// Copy fast-math-flags from an instruction rather than using the builder's | ||||||||||
1655 | /// default FMF. | ||||||||||
1656 | Value *CreateFNegFMF(Value *V, Instruction *FMFSource, | ||||||||||
1657 | const Twine &Name = "") { | ||||||||||
1658 | if (auto *VC = dyn_cast<Constant>(V)) | ||||||||||
1659 | return Insert(Folder.CreateFNeg(VC), Name); | ||||||||||
1660 | return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), nullptr, | ||||||||||
1661 | FMFSource->getFastMathFlags()), | ||||||||||
1662 | Name); | ||||||||||
1663 | } | ||||||||||
1664 | |||||||||||
1665 | Value *CreateNot(Value *V, const Twine &Name = "") { | ||||||||||
1666 | if (auto *VC = dyn_cast<Constant>(V)) | ||||||||||
1667 | return Insert(Folder.CreateNot(VC), Name); | ||||||||||
1668 | return Insert(BinaryOperator::CreateNot(V), Name); | ||||||||||
1669 | } | ||||||||||
1670 | |||||||||||
1671 | Value *CreateUnOp(Instruction::UnaryOps Opc, | ||||||||||
1672 | Value *V, const Twine &Name = "", | ||||||||||
1673 | MDNode *FPMathTag = nullptr) { | ||||||||||
1674 | if (auto *VC = dyn_cast<Constant>(V)) | ||||||||||
1675 | return Insert(Folder.CreateUnOp(Opc, VC), Name); | ||||||||||
1676 | Instruction *UnOp = UnaryOperator::Create(Opc, V); | ||||||||||
1677 | if (isa<FPMathOperator>(UnOp)) | ||||||||||
1678 | setFPAttrs(UnOp, FPMathTag, FMF); | ||||||||||
1679 | return Insert(UnOp, Name); | ||||||||||
1680 | } | ||||||||||
1681 | |||||||||||
1682 | /// Create either a UnaryOperator or BinaryOperator depending on \p Opc. | ||||||||||
1683 | /// Correct number of operands must be passed accordingly. | ||||||||||
1684 | Value *CreateNAryOp(unsigned Opc, ArrayRef<Value *> Ops, | ||||||||||
1685 | const Twine &Name = "", | ||||||||||
1686 | MDNode *FPMathTag = nullptr) { | ||||||||||
1687 | if (Instruction::isBinaryOp(Opc)) { | ||||||||||
1688 | assert(Ops.size() == 2 && "Invalid number of operands!")((Ops.size() == 2 && "Invalid number of operands!") ? static_cast<void> (0) : __assert_fail ("Ops.size() == 2 && \"Invalid number of operands!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 1688, __PRETTY_FUNCTION__)); | ||||||||||
1689 | return CreateBinOp(static_cast<Instruction::BinaryOps>(Opc), | ||||||||||
1690 | Ops[0], Ops[1], Name, FPMathTag); | ||||||||||
1691 | } | ||||||||||
1692 | if (Instruction::isUnaryOp(Opc)) { | ||||||||||
1693 | assert(Ops.size() == 1 && "Invalid number of operands!")((Ops.size() == 1 && "Invalid number of operands!") ? static_cast<void> (0) : __assert_fail ("Ops.size() == 1 && \"Invalid number of operands!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 1693, __PRETTY_FUNCTION__)); | ||||||||||
1694 | return CreateUnOp(static_cast<Instruction::UnaryOps>(Opc), | ||||||||||
1695 | Ops[0], Name, FPMathTag); | ||||||||||
1696 | } | ||||||||||
1697 | llvm_unreachable("Unexpected opcode!")::llvm::llvm_unreachable_internal("Unexpected opcode!", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 1697); | ||||||||||
1698 | } | ||||||||||
1699 | |||||||||||
1700 | //===--------------------------------------------------------------------===// | ||||||||||
1701 | // Instruction creation methods: Memory Instructions | ||||||||||
1702 | //===--------------------------------------------------------------------===// | ||||||||||
1703 | |||||||||||
1704 | AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace, | ||||||||||
1705 | Value *ArraySize = nullptr, const Twine &Name = "") { | ||||||||||
1706 | return Insert(new AllocaInst(Ty, AddrSpace, ArraySize), Name); | ||||||||||
1707 | } | ||||||||||
1708 | |||||||||||
1709 | AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr, | ||||||||||
1710 | const Twine &Name = "") { | ||||||||||
1711 | const DataLayout &DL = BB->getParent()->getParent()->getDataLayout(); | ||||||||||
1712 | return Insert(new AllocaInst(Ty, DL.getAllocaAddrSpace(), ArraySize), Name); | ||||||||||
1713 | } | ||||||||||
1714 | |||||||||||
1715 | /// Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of | ||||||||||
1716 | /// converting the string to 'bool' for the isVolatile parameter. | ||||||||||
1717 | LoadInst *CreateLoad(Type *Ty, Value *Ptr, const char *Name) { | ||||||||||
1718 | return Insert(new LoadInst(Ty, Ptr), Name); | ||||||||||
1719 | } | ||||||||||
1720 | |||||||||||
1721 | LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") { | ||||||||||
1722 | return Insert(new LoadInst(Ty, Ptr), Name); | ||||||||||
1723 | } | ||||||||||
1724 | |||||||||||
1725 | LoadInst *CreateLoad(Type *Ty, Value *Ptr, bool isVolatile, | ||||||||||
1726 | const Twine &Name = "") { | ||||||||||
1727 | return Insert(new LoadInst(Ty, Ptr, Twine(), isVolatile), Name); | ||||||||||
1728 | } | ||||||||||
1729 | |||||||||||
1730 | // Deprecated [opaque pointer types] | ||||||||||
1731 | LoadInst *CreateLoad(Value *Ptr, const char *Name) { | ||||||||||
1732 | return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, Name); | ||||||||||
1733 | } | ||||||||||
1734 | |||||||||||
1735 | // Deprecated [opaque pointer types] | ||||||||||
1736 | LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") { | ||||||||||
1737 | return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, Name); | ||||||||||
1738 | } | ||||||||||
1739 | |||||||||||
1740 | // Deprecated [opaque pointer types] | ||||||||||
1741 | LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") { | ||||||||||
1742 | return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, isVolatile, | ||||||||||
1743 | Name); | ||||||||||
1744 | } | ||||||||||
1745 | |||||||||||
1746 | StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) { | ||||||||||
1747 | return Insert(new StoreInst(Val, Ptr, isVolatile)); | ||||||||||
1748 | } | ||||||||||
1749 | |||||||||||
1750 | /// Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")' | ||||||||||
1751 | /// correctly, instead of converting the string to 'bool' for the isVolatile | ||||||||||
1752 | /// parameter. | ||||||||||
1753 | /// FIXME: Remove this function once transition to Align is over. | ||||||||||
1754 | /// Use the version that takes MaybeAlign instead of this one. | ||||||||||
1755 | LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , const char *Name) __attribute__((deprecated("Use the version that takes NaybeAlign instead" ))) | ||||||||||
1756 | unsigned Align,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , const char *Name) __attribute__((deprecated("Use the version that takes NaybeAlign instead" ))) | ||||||||||
1757 | const char *Name),LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , const char *Name) __attribute__((deprecated("Use the version that takes NaybeAlign instead" ))) | ||||||||||
1758 | "Use the version that takes NaybeAlign instead")LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , const char *Name) __attribute__((deprecated("Use the version that takes NaybeAlign instead" ))) { | ||||||||||
1759 | return CreateAlignedLoad(Ty, Ptr, MaybeAlign(Align), Name); | ||||||||||
1760 | } | ||||||||||
1761 | LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, | ||||||||||
1762 | const char *Name) { | ||||||||||
1763 | LoadInst *LI = CreateLoad(Ty, Ptr, Name); | ||||||||||
1764 | LI->setAlignment(Align); | ||||||||||
1765 | return LI; | ||||||||||
1766 | } | ||||||||||
1767 | /// FIXME: Remove this function once transition to Align is over. | ||||||||||
1768 | /// Use the version that takes MaybeAlign instead of this one. | ||||||||||
1769 | LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) | ||||||||||
1770 | unsigned Align,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) | ||||||||||
1771 | const Twine &Name = ""),LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) | ||||||||||
1772 | "Use the version that takes MaybeAlign instead")LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) { | ||||||||||
1773 | return CreateAlignedLoad(Ty, Ptr, MaybeAlign(Align), Name); | ||||||||||
1774 | } | ||||||||||
1775 | LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, | ||||||||||
1776 | const Twine &Name = "") { | ||||||||||
1777 | LoadInst *LI = CreateLoad(Ty, Ptr, Name); | ||||||||||
1778 | LI->setAlignment(Align); | ||||||||||
1779 | return LI; | ||||||||||
1780 | } | ||||||||||
1781 | /// FIXME: Remove this function once transition to Align is over. | ||||||||||
1782 | /// Use the version that takes MaybeAlign instead of this one. | ||||||||||
1783 | LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , bool isVolatile, const Twine &Name = "") __attribute__( (deprecated("Use the version that takes MaybeAlign instead")) ) | ||||||||||
1784 | unsigned Align,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , bool isVolatile, const Twine &Name = "") __attribute__( (deprecated("Use the version that takes MaybeAlign instead")) ) | ||||||||||
1785 | bool isVolatile,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , bool isVolatile, const Twine &Name = "") __attribute__( (deprecated("Use the version that takes MaybeAlign instead")) ) | ||||||||||
1786 | const Twine &Name = ""),LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , bool isVolatile, const Twine &Name = "") __attribute__( (deprecated("Use the version that takes MaybeAlign instead")) ) | ||||||||||
1787 | "Use the version that takes MaybeAlign instead")LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , bool isVolatile, const Twine &Name = "") __attribute__( (deprecated("Use the version that takes MaybeAlign instead")) ) { | ||||||||||
1788 | return CreateAlignedLoad(Ty, Ptr, MaybeAlign(Align), isVolatile, Name); | ||||||||||
1789 | } | ||||||||||
1790 | LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, | ||||||||||
1791 | bool isVolatile, const Twine &Name = "") { | ||||||||||
1792 | LoadInst *LI = CreateLoad(Ty, Ptr, isVolatile, Name); | ||||||||||
1793 | LI->setAlignment(Align); | ||||||||||
1794 | return LI; | ||||||||||
1795 | } | ||||||||||
1796 | |||||||||||
1797 | // Deprecated [opaque pointer types] | ||||||||||
1798 | LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Value *Ptr,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) | ||||||||||
1799 | unsigned Align,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) | ||||||||||
1800 | const char *Name),LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) | ||||||||||
1801 | "Use the version that takes MaybeAlign instead")LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) { | ||||||||||
1802 | return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr, | ||||||||||
1803 | MaybeAlign(Align), Name); | ||||||||||
1804 | } | ||||||||||
1805 | // Deprecated [opaque pointer types] | ||||||||||
1806 | LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Value *Ptr,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) | ||||||||||
1807 | unsigned Align,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) | ||||||||||
1808 | const Twine &Name = ""),LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) | ||||||||||
1809 | "Use the version that takes MaybeAlign instead")LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) { | ||||||||||
1810 | return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr, | ||||||||||
1811 | MaybeAlign(Align), Name); | ||||||||||
1812 | } | ||||||||||
1813 | // Deprecated [opaque pointer types] | ||||||||||
1814 | LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Value *Ptr,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile , const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) | ||||||||||
1815 | unsigned Align,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile , const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) | ||||||||||
1816 | bool isVolatile,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile , const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) | ||||||||||
1817 | const Twine &Name = ""),LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile , const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) | ||||||||||
1818 | "Use the version that takes MaybeAlign instead")LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile , const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) { | ||||||||||
1819 | return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr, | ||||||||||
1820 | MaybeAlign(Align), isVolatile, Name); | ||||||||||
1821 | } | ||||||||||
1822 | // Deprecated [opaque pointer types] | ||||||||||
1823 | LoadInst *CreateAlignedLoad(Value *Ptr, MaybeAlign Align, const char *Name) { | ||||||||||
1824 | return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr, | ||||||||||
1825 | Align, Name); | ||||||||||
1826 | } | ||||||||||
1827 | // Deprecated [opaque pointer types] | ||||||||||
1828 | LoadInst *CreateAlignedLoad(Value *Ptr, MaybeAlign Align, | ||||||||||
1829 | const Twine &Name = "") { | ||||||||||
1830 | return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr, | ||||||||||
1831 | Align, Name); | ||||||||||
1832 | } | ||||||||||
1833 | // Deprecated [opaque pointer types] | ||||||||||
1834 | LoadInst *CreateAlignedLoad(Value *Ptr, MaybeAlign Align, bool isVolatile, | ||||||||||
1835 | const Twine &Name = "") { | ||||||||||
1836 | return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr, | ||||||||||
1837 | Align, isVolatile, Name); | ||||||||||
1838 | } | ||||||||||
1839 | |||||||||||
1840 | /// FIXME: Remove this function once transition to Align is over. | ||||||||||
1841 | /// Use the version that takes MaybeAlign instead of this one. | ||||||||||
1842 | LLVM_ATTRIBUTE_DEPRECATED(StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align, bool isVolatile = false) __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) | ||||||||||
1843 | StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align, bool isVolatile = false) __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) | ||||||||||
1844 | bool isVolatile = false),StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align, bool isVolatile = false) __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) | ||||||||||
1845 | "Use the version that takes MaybeAlign instead")StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align, bool isVolatile = false) __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) { | ||||||||||
1846 | return CreateAlignedStore(Val, Ptr, MaybeAlign(Align), isVolatile); | ||||||||||
1847 | } | ||||||||||
1848 | StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align, | ||||||||||
1849 | bool isVolatile = false) { | ||||||||||
1850 | StoreInst *SI = CreateStore(Val, Ptr, isVolatile); | ||||||||||
1851 | SI->setAlignment(Align); | ||||||||||
1852 | return SI; | ||||||||||
1853 | } | ||||||||||
1854 | FenceInst *CreateFence(AtomicOrdering Ordering, | ||||||||||
1855 | SyncScope::ID SSID = SyncScope::System, | ||||||||||
1856 | const Twine &Name = "") { | ||||||||||
1857 | return Insert(new FenceInst(Context, Ordering, SSID), Name); | ||||||||||
1858 | } | ||||||||||
1859 | |||||||||||
1860 | AtomicCmpXchgInst * | ||||||||||
1861 | CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New, | ||||||||||
1862 | AtomicOrdering SuccessOrdering, | ||||||||||
1863 | AtomicOrdering FailureOrdering, | ||||||||||
1864 | SyncScope::ID SSID = SyncScope::System) { | ||||||||||
1865 | return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, | ||||||||||
1866 | FailureOrdering, SSID)); | ||||||||||
1867 | } | ||||||||||
1868 | |||||||||||
1869 | AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val, | ||||||||||
1870 | AtomicOrdering Ordering, | ||||||||||
1871 | SyncScope::ID SSID = SyncScope::System) { | ||||||||||
1872 | return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SSID)); | ||||||||||
1873 | } | ||||||||||
1874 | |||||||||||
1875 | Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList, | ||||||||||
1876 | const Twine &Name = "") { | ||||||||||
1877 | return CreateGEP(nullptr, Ptr, IdxList, Name); | ||||||||||
1878 | } | ||||||||||
1879 | |||||||||||
1880 | Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList, | ||||||||||
1881 | const Twine &Name = "") { | ||||||||||
1882 | if (auto *PC = dyn_cast<Constant>(Ptr)) { | ||||||||||
1883 | // Every index must be constant. | ||||||||||
1884 | size_t i, e; | ||||||||||
1885 | for (i = 0, e = IdxList.size(); i != e; ++i) | ||||||||||
1886 | if (!isa<Constant>(IdxList[i])) | ||||||||||
1887 | break; | ||||||||||
1888 | if (i == e) | ||||||||||
1889 | return Insert(Folder.CreateGetElementPtr(Ty, PC, IdxList), Name); | ||||||||||
1890 | } | ||||||||||
1891 | return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name); | ||||||||||
1892 | } | ||||||||||
1893 | |||||||||||
1894 | Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList, | ||||||||||
1895 | const Twine &Name = "") { | ||||||||||
1896 | return CreateInBoundsGEP(nullptr, Ptr, IdxList, Name); | ||||||||||
1897 | } | ||||||||||
1898 | |||||||||||
1899 | Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList, | ||||||||||
1900 | const Twine &Name = "") { | ||||||||||
1901 | if (auto *PC = dyn_cast<Constant>(Ptr)) { | ||||||||||
1902 | // Every index must be constant. | ||||||||||
1903 | size_t i, e; | ||||||||||
1904 | for (i = 0, e = IdxList.size(); i != e; ++i) | ||||||||||
1905 | if (!isa<Constant>(IdxList[i])) | ||||||||||
1906 | break; | ||||||||||
1907 | if (i == e) | ||||||||||
1908 | return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IdxList), | ||||||||||
1909 | Name); | ||||||||||
1910 | } | ||||||||||
1911 | return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name); | ||||||||||
1912 | } | ||||||||||
1913 | |||||||||||
1914 | Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") { | ||||||||||
1915 | return CreateGEP(nullptr, Ptr, Idx, Name); | ||||||||||
1916 | } | ||||||||||
1917 | |||||||||||
1918 | Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") { | ||||||||||
1919 | if (auto *PC = dyn_cast<Constant>(Ptr)) | ||||||||||
1920 | if (auto *IC = dyn_cast<Constant>(Idx)) | ||||||||||
1921 | return Insert(Folder.CreateGetElementPtr(Ty, PC, IC), Name); | ||||||||||
1922 | return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name); | ||||||||||
1923 | } | ||||||||||
1924 | |||||||||||
1925 | Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx, | ||||||||||
1926 | const Twine &Name = "") { | ||||||||||
1927 | if (auto *PC = dyn_cast<Constant>(Ptr)) | ||||||||||
1928 | if (auto *IC = dyn_cast<Constant>(Idx)) | ||||||||||
1929 | return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IC), Name); | ||||||||||
1930 | return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name); | ||||||||||
1931 | } | ||||||||||
1932 | |||||||||||
1933 | Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") { | ||||||||||
1934 | return CreateConstGEP1_32(nullptr, Ptr, Idx0, Name); | ||||||||||
1935 | } | ||||||||||
1936 | |||||||||||
1937 | Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0, | ||||||||||
1938 | const Twine &Name = "") { | ||||||||||
1939 | Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0); | ||||||||||
1940 | |||||||||||
1941 | if (auto *PC = dyn_cast<Constant>(Ptr)) | ||||||||||
1942 | return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name); | ||||||||||
1943 | |||||||||||
1944 | return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name); | ||||||||||
1945 | } | ||||||||||
1946 | |||||||||||
1947 | Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0, | ||||||||||
1948 | const Twine &Name = "") { | ||||||||||
1949 | Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0); | ||||||||||
1950 | |||||||||||
1951 | if (auto *PC = dyn_cast<Constant>(Ptr)) | ||||||||||
1952 | return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name); | ||||||||||
1953 | |||||||||||
1954 | return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name); | ||||||||||
1955 | } | ||||||||||
1956 | |||||||||||
1957 | Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1, | ||||||||||
1958 | const Twine &Name = "") { | ||||||||||
1959 | Value *Idxs[] = { | ||||||||||
1960 | ConstantInt::get(Type::getInt32Ty(Context), Idx0), | ||||||||||
1961 | ConstantInt::get(Type::getInt32Ty(Context), Idx1) | ||||||||||
1962 | }; | ||||||||||
1963 | |||||||||||
1964 | if (auto *PC = dyn_cast<Constant>(Ptr)) | ||||||||||
1965 | return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name); | ||||||||||
1966 | |||||||||||
1967 | return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name); | ||||||||||
1968 | } | ||||||||||
1969 | |||||||||||
1970 | Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, | ||||||||||
1971 | unsigned Idx1, const Twine &Name = "") { | ||||||||||
1972 | Value *Idxs[] = { | ||||||||||
1973 | ConstantInt::get(Type::getInt32Ty(Context), Idx0), | ||||||||||
1974 | ConstantInt::get(Type::getInt32Ty(Context), Idx1) | ||||||||||
1975 | }; | ||||||||||
1976 | |||||||||||
1977 | if (auto *PC = dyn_cast<Constant>(Ptr)) | ||||||||||
1978 | return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name); | ||||||||||
1979 | |||||||||||
1980 | return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name); | ||||||||||
1981 | } | ||||||||||
1982 | |||||||||||
1983 | Value *CreateConstGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0, | ||||||||||
1984 | const Twine &Name = "") { | ||||||||||
1985 | Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0); | ||||||||||
1986 | |||||||||||
1987 | if (auto *PC = dyn_cast<Constant>(Ptr)) | ||||||||||
1988 | return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name); | ||||||||||
1989 | |||||||||||
1990 | return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name); | ||||||||||
1991 | } | ||||||||||
1992 | |||||||||||
1993 | Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") { | ||||||||||
1994 | return CreateConstGEP1_64(nullptr, Ptr, Idx0, Name); | ||||||||||
1995 | } | ||||||||||
1996 | |||||||||||
1997 | Value *CreateConstInBoundsGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0, | ||||||||||
1998 | const Twine &Name = "") { | ||||||||||
1999 | Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0); | ||||||||||
2000 | |||||||||||
2001 | if (auto *PC = dyn_cast<Constant>(Ptr)) | ||||||||||
2002 | return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name); | ||||||||||
2003 | |||||||||||
2004 | return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name); | ||||||||||
2005 | } | ||||||||||
2006 | |||||||||||
2007 | Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0, | ||||||||||
2008 | const Twine &Name = "") { | ||||||||||
2009 | return CreateConstInBoundsGEP1_64(nullptr, Ptr, Idx0, Name); | ||||||||||
2010 | } | ||||||||||
2011 | |||||||||||
2012 | Value *CreateConstGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1, | ||||||||||
2013 | const Twine &Name = "") { | ||||||||||
2014 | Value *Idxs[] = { | ||||||||||
2015 | ConstantInt::get(Type::getInt64Ty(Context), Idx0), | ||||||||||
2016 | ConstantInt::get(Type::getInt64Ty(Context), Idx1) | ||||||||||
2017 | }; | ||||||||||
2018 | |||||||||||
2019 | if (auto *PC = dyn_cast<Constant>(Ptr)) | ||||||||||
2020 | return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name); | ||||||||||
2021 | |||||||||||
2022 | return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name); | ||||||||||
2023 | } | ||||||||||
2024 | |||||||||||
2025 | Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1, | ||||||||||
2026 | const Twine &Name = "") { | ||||||||||
2027 | return CreateConstGEP2_64(nullptr, Ptr, Idx0, Idx1, Name); | ||||||||||
2028 | } | ||||||||||
2029 | |||||||||||
2030 | Value *CreateConstInBoundsGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, | ||||||||||
2031 | uint64_t Idx1, const Twine &Name = "") { | ||||||||||
2032 | Value *Idxs[] = { | ||||||||||
2033 | ConstantInt::get(Type::getInt64Ty(Context), Idx0), | ||||||||||
2034 | ConstantInt::get(Type::getInt64Ty(Context), Idx1) | ||||||||||
2035 | }; | ||||||||||
2036 | |||||||||||
2037 | if (auto *PC = dyn_cast<Constant>(Ptr)) | ||||||||||
2038 | return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name); | ||||||||||
2039 | |||||||||||
2040 | return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name); | ||||||||||
2041 | } | ||||||||||
2042 | |||||||||||
2043 | Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1, | ||||||||||
2044 | const Twine &Name = "") { | ||||||||||
2045 | return CreateConstInBoundsGEP2_64(nullptr, Ptr, Idx0, Idx1, Name); | ||||||||||
2046 | } | ||||||||||
2047 | |||||||||||
2048 | Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx, | ||||||||||
2049 | const Twine &Name = "") { | ||||||||||
2050 | return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name); | ||||||||||
2051 | } | ||||||||||
2052 | |||||||||||
2053 | Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") { | ||||||||||
2054 | return CreateConstInBoundsGEP2_32(nullptr, Ptr, 0, Idx, Name); | ||||||||||
2055 | } | ||||||||||
2056 | |||||||||||
2057 | /// Same as CreateGlobalString, but return a pointer with "i8*" type | ||||||||||
2058 | /// instead of a pointer to array of i8. | ||||||||||
2059 | Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "", | ||||||||||
2060 | unsigned AddressSpace = 0) { | ||||||||||
2061 | GlobalVariable *GV = CreateGlobalString(Str, Name, AddressSpace); | ||||||||||
2062 | Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0); | ||||||||||
2063 | Constant *Indices[] = {Zero, Zero}; | ||||||||||
2064 | return ConstantExpr::getInBoundsGetElementPtr(GV->getValueType(), GV, | ||||||||||
2065 | Indices); | ||||||||||
2066 | } | ||||||||||
2067 | |||||||||||
2068 | //===--------------------------------------------------------------------===// | ||||||||||
2069 | // Instruction creation methods: Cast/Conversion Operators | ||||||||||
2070 | //===--------------------------------------------------------------------===// | ||||||||||
2071 | |||||||||||
2072 | Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") { | ||||||||||
2073 | return CreateCast(Instruction::Trunc, V, DestTy, Name); | ||||||||||
2074 | } | ||||||||||
2075 | |||||||||||
2076 | Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") { | ||||||||||
2077 | return CreateCast(Instruction::ZExt, V, DestTy, Name); | ||||||||||
2078 | } | ||||||||||
2079 | |||||||||||
2080 | Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") { | ||||||||||
2081 | return CreateCast(Instruction::SExt, V, DestTy, Name); | ||||||||||
2082 | } | ||||||||||
2083 | |||||||||||
2084 | /// Create a ZExt or Trunc from the integer value V to DestTy. Return | ||||||||||
2085 | /// the value untouched if the type of V is already DestTy. | ||||||||||
2086 | Value *CreateZExtOrTrunc(Value *V, Type *DestTy, | ||||||||||
2087 | const Twine &Name = "") { | ||||||||||
2088 | assert(V->getType()->isIntOrIntVectorTy() &&((V->getType()->isIntOrIntVectorTy() && DestTy-> isIntOrIntVectorTy() && "Can only zero extend/truncate integers!" ) ? static_cast<void> (0) : __assert_fail ("V->getType()->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy() && \"Can only zero extend/truncate integers!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2090, __PRETTY_FUNCTION__)) | ||||||||||
2089 | DestTy->isIntOrIntVectorTy() &&((V->getType()->isIntOrIntVectorTy() && DestTy-> isIntOrIntVectorTy() && "Can only zero extend/truncate integers!" ) ? static_cast<void> (0) : __assert_fail ("V->getType()->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy() && \"Can only zero extend/truncate integers!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2090, __PRETTY_FUNCTION__)) | ||||||||||
2090 | "Can only zero extend/truncate integers!")((V->getType()->isIntOrIntVectorTy() && DestTy-> isIntOrIntVectorTy() && "Can only zero extend/truncate integers!" ) ? static_cast<void> (0) : __assert_fail ("V->getType()->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy() && \"Can only zero extend/truncate integers!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2090, __PRETTY_FUNCTION__)); | ||||||||||
2091 | Type *VTy = V->getType(); | ||||||||||
2092 | if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits()) | ||||||||||
2093 | return CreateZExt(V, DestTy, Name); | ||||||||||
2094 | if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits()) | ||||||||||
2095 | return CreateTrunc(V, DestTy, Name); | ||||||||||
2096 | return V; | ||||||||||
2097 | } | ||||||||||
2098 | |||||||||||
2099 | /// Create a SExt or Trunc from the integer value V to DestTy. Return | ||||||||||
2100 | /// the value untouched if the type of V is already DestTy. | ||||||||||
2101 | Value *CreateSExtOrTrunc(Value *V, Type *DestTy, | ||||||||||
2102 | const Twine &Name = "") { | ||||||||||
2103 | assert(V->getType()->isIntOrIntVectorTy() &&((V->getType()->isIntOrIntVectorTy() && DestTy-> isIntOrIntVectorTy() && "Can only sign extend/truncate integers!" ) ? static_cast<void> (0) : __assert_fail ("V->getType()->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy() && \"Can only sign extend/truncate integers!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2105, __PRETTY_FUNCTION__)) | ||||||||||
2104 | DestTy->isIntOrIntVectorTy() &&((V->getType()->isIntOrIntVectorTy() && DestTy-> isIntOrIntVectorTy() && "Can only sign extend/truncate integers!" ) ? static_cast<void> (0) : __assert_fail ("V->getType()->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy() && \"Can only sign extend/truncate integers!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2105, __PRETTY_FUNCTION__)) | ||||||||||
2105 | "Can only sign extend/truncate integers!")((V->getType()->isIntOrIntVectorTy() && DestTy-> isIntOrIntVectorTy() && "Can only sign extend/truncate integers!" ) ? static_cast<void> (0) : __assert_fail ("V->getType()->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy() && \"Can only sign extend/truncate integers!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2105, __PRETTY_FUNCTION__)); | ||||||||||
2106 | Type *VTy = V->getType(); | ||||||||||
2107 | if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits()) | ||||||||||
2108 | return CreateSExt(V, DestTy, Name); | ||||||||||
2109 | if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits()) | ||||||||||
2110 | return CreateTrunc(V, DestTy, Name); | ||||||||||
2111 | return V; | ||||||||||
2112 | } | ||||||||||
2113 | |||||||||||
2114 | Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = "") { | ||||||||||
2115 | if (IsFPConstrained) | ||||||||||
2116 | return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptoui, | ||||||||||
2117 | V, DestTy, nullptr, Name); | ||||||||||
2118 | return CreateCast(Instruction::FPToUI, V, DestTy, Name); | ||||||||||
2119 | } | ||||||||||
2120 | |||||||||||
2121 | Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = "") { | ||||||||||
2122 | if (IsFPConstrained) | ||||||||||
2123 | return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptosi, | ||||||||||
2124 | V, DestTy, nullptr, Name); | ||||||||||
2125 | return CreateCast(Instruction::FPToSI, V, DestTy, Name); | ||||||||||
2126 | } | ||||||||||
2127 | |||||||||||
2128 | Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){ | ||||||||||
2129 | if (IsFPConstrained) | ||||||||||
2130 | return CreateConstrainedFPCast(Intrinsic::experimental_constrained_uitofp, | ||||||||||
2131 | V, DestTy, nullptr, Name); | ||||||||||
2132 | return CreateCast(Instruction::UIToFP, V, DestTy, Name); | ||||||||||
2133 | } | ||||||||||
2134 | |||||||||||
2135 | Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){ | ||||||||||
2136 | if (IsFPConstrained) | ||||||||||
2137 | return CreateConstrainedFPCast(Intrinsic::experimental_constrained_sitofp, | ||||||||||
2138 | V, DestTy, nullptr, Name); | ||||||||||
2139 | return CreateCast(Instruction::SIToFP, V, DestTy, Name); | ||||||||||
2140 | } | ||||||||||
2141 | |||||||||||
2142 | Value *CreateFPTrunc(Value *V, Type *DestTy, | ||||||||||
2143 | const Twine &Name = "") { | ||||||||||
2144 | if (IsFPConstrained) | ||||||||||
2145 | return CreateConstrainedFPCast( | ||||||||||
2146 | Intrinsic::experimental_constrained_fptrunc, V, DestTy, nullptr, | ||||||||||
2147 | Name); | ||||||||||
2148 | return CreateCast(Instruction::FPTrunc, V, DestTy, Name); | ||||||||||
2149 | } | ||||||||||
2150 | |||||||||||
2151 | Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") { | ||||||||||
2152 | if (IsFPConstrained) | ||||||||||
2153 | return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fpext, | ||||||||||
2154 | V, DestTy, nullptr, Name); | ||||||||||
2155 | return CreateCast(Instruction::FPExt, V, DestTy, Name); | ||||||||||
2156 | } | ||||||||||
2157 | |||||||||||
2158 | Value *CreatePtrToInt(Value *V, Type *DestTy, | ||||||||||
2159 | const Twine &Name = "") { | ||||||||||
2160 | return CreateCast(Instruction::PtrToInt, V, DestTy, Name); | ||||||||||
2161 | } | ||||||||||
2162 | |||||||||||
2163 | Value *CreateIntToPtr(Value *V, Type *DestTy, | ||||||||||
2164 | const Twine &Name = "") { | ||||||||||
2165 | return CreateCast(Instruction::IntToPtr, V, DestTy, Name); | ||||||||||
2166 | } | ||||||||||
2167 | |||||||||||
2168 | Value *CreateBitCast(Value *V, Type *DestTy, | ||||||||||
2169 | const Twine &Name = "") { | ||||||||||
2170 | return CreateCast(Instruction::BitCast, V, DestTy, Name); | ||||||||||
2171 | } | ||||||||||
2172 | |||||||||||
2173 | Value *CreateAddrSpaceCast(Value *V, Type *DestTy, | ||||||||||
2174 | const Twine &Name = "") { | ||||||||||
2175 | return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name); | ||||||||||
2176 | } | ||||||||||
2177 | |||||||||||
2178 | Value *CreateZExtOrBitCast(Value *V, Type *DestTy, | ||||||||||
2179 | const Twine &Name = "") { | ||||||||||
2180 | if (V->getType() == DestTy) | ||||||||||
2181 | return V; | ||||||||||
2182 | if (auto *VC = dyn_cast<Constant>(V)) | ||||||||||
2183 | return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name); | ||||||||||
2184 | return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name); | ||||||||||
2185 | } | ||||||||||
2186 | |||||||||||
2187 | Value *CreateSExtOrBitCast(Value *V, Type *DestTy, | ||||||||||
2188 | const Twine &Name = "") { | ||||||||||
2189 | if (V->getType() == DestTy) | ||||||||||
2190 | return V; | ||||||||||
2191 | if (auto *VC = dyn_cast<Constant>(V)) | ||||||||||
2192 | return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name); | ||||||||||
2193 | return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name); | ||||||||||
2194 | } | ||||||||||
2195 | |||||||||||
2196 | Value *CreateTruncOrBitCast(Value *V, Type *DestTy, | ||||||||||
2197 | const Twine &Name = "") { | ||||||||||
2198 | if (V->getType() == DestTy) | ||||||||||
2199 | return V; | ||||||||||
2200 | if (auto *VC = dyn_cast<Constant>(V)) | ||||||||||
2201 | return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name); | ||||||||||
2202 | return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name); | ||||||||||
2203 | } | ||||||||||
2204 | |||||||||||
2205 | Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, | ||||||||||
2206 | const Twine &Name = "") { | ||||||||||
2207 | if (V->getType() == DestTy) | ||||||||||
2208 | return V; | ||||||||||
2209 | if (auto *VC = dyn_cast<Constant>(V)) | ||||||||||
2210 | return Insert(Folder.CreateCast(Op, VC, DestTy), Name); | ||||||||||
2211 | return Insert(CastInst::Create(Op, V, DestTy), Name); | ||||||||||
2212 | } | ||||||||||
2213 | |||||||||||
2214 | Value *CreatePointerCast(Value *V, Type *DestTy, | ||||||||||
2215 | const Twine &Name = "") { | ||||||||||
2216 | if (V->getType() == DestTy) | ||||||||||
2217 | return V; | ||||||||||
2218 | if (auto *VC = dyn_cast<Constant>(V)) | ||||||||||
2219 | return Insert(Folder.CreatePointerCast(VC, DestTy), Name); | ||||||||||
2220 | return Insert(CastInst::CreatePointerCast(V, DestTy), Name); | ||||||||||
2221 | } | ||||||||||
2222 | |||||||||||
2223 | Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy, | ||||||||||
2224 | const Twine &Name = "") { | ||||||||||
2225 | if (V->getType() == DestTy) | ||||||||||
2226 | return V; | ||||||||||
2227 | |||||||||||
2228 | if (auto *VC = dyn_cast<Constant>(V)) { | ||||||||||
2229 | return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy), | ||||||||||
2230 | Name); | ||||||||||
2231 | } | ||||||||||
2232 | |||||||||||
2233 | return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy), | ||||||||||
2234 | Name); | ||||||||||
2235 | } | ||||||||||
2236 | |||||||||||
2237 | Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned, | ||||||||||
2238 | const Twine &Name = "") { | ||||||||||
2239 | if (V->getType() == DestTy) | ||||||||||
2240 | return V; | ||||||||||
2241 | if (auto *VC = dyn_cast<Constant>(V)) | ||||||||||
2242 | return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name); | ||||||||||
2243 | return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name); | ||||||||||
2244 | } | ||||||||||
2245 | |||||||||||
2246 | Value *CreateBitOrPointerCast(Value *V, Type *DestTy, | ||||||||||
2247 | const Twine &Name = "") { | ||||||||||
2248 | if (V->getType() == DestTy) | ||||||||||
2249 | return V; | ||||||||||
2250 | if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy()) | ||||||||||
2251 | return CreatePtrToInt(V, DestTy, Name); | ||||||||||
2252 | if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy()) | ||||||||||
2253 | return CreateIntToPtr(V, DestTy, Name); | ||||||||||
2254 | |||||||||||
2255 | return CreateBitCast(V, DestTy, Name); | ||||||||||
2256 | } | ||||||||||
2257 | |||||||||||
2258 | Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") { | ||||||||||
2259 | if (V->getType() == DestTy) | ||||||||||
2260 | return V; | ||||||||||
2261 | if (auto *VC = dyn_cast<Constant>(V)) | ||||||||||
2262 | return Insert(Folder.CreateFPCast(VC, DestTy), Name); | ||||||||||
2263 | return Insert(CastInst::CreateFPCast(V, DestTy), Name); | ||||||||||
2264 | } | ||||||||||
2265 | |||||||||||
2266 | CallInst *CreateConstrainedFPCast( | ||||||||||
2267 | Intrinsic::ID ID, Value *V, Type *DestTy, | ||||||||||
2268 | Instruction *FMFSource = nullptr, const Twine &Name = "", | ||||||||||
2269 | MDNode *FPMathTag = nullptr, | ||||||||||
2270 | Optional<fp::RoundingMode> Rounding = None, | ||||||||||
2271 | Optional<fp::ExceptionBehavior> Except = None) { | ||||||||||
2272 | Value *ExceptV = getConstrainedFPExcept(Except); | ||||||||||
2273 | |||||||||||
2274 | FastMathFlags UseFMF = FMF; | ||||||||||
2275 | if (FMFSource) | ||||||||||
2276 | UseFMF = FMFSource->getFastMathFlags(); | ||||||||||
2277 | |||||||||||
2278 | CallInst *C; | ||||||||||
2279 | bool HasRoundingMD = false; | ||||||||||
2280 | switch (ID) { | ||||||||||
2281 | default: | ||||||||||
2282 | break; | ||||||||||
2283 | #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \ | ||||||||||
2284 | case Intrinsic::INTRINSIC: \ | ||||||||||
2285 | HasRoundingMD = ROUND_MODE; \ | ||||||||||
2286 | break; | ||||||||||
2287 | #include "llvm/IR/ConstrainedOps.def" | ||||||||||
2288 | } | ||||||||||
2289 | if (HasRoundingMD) { | ||||||||||
2290 | Value *RoundingV = getConstrainedFPRounding(Rounding); | ||||||||||
2291 | C = CreateIntrinsic(ID, {DestTy, V->getType()}, {V, RoundingV, ExceptV}, | ||||||||||
2292 | nullptr, Name); | ||||||||||
2293 | } else | ||||||||||
2294 | C = CreateIntrinsic(ID, {DestTy, V->getType()}, {V, ExceptV}, nullptr, | ||||||||||
2295 | Name); | ||||||||||
2296 | |||||||||||
2297 | setConstrainedFPCallAttr(C); | ||||||||||
2298 | |||||||||||
2299 | if (isa<FPMathOperator>(C)) | ||||||||||
2300 | setFPAttrs(C, FPMathTag, UseFMF); | ||||||||||
2301 | return C; | ||||||||||
2302 | } | ||||||||||
2303 | |||||||||||
2304 | // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a | ||||||||||
2305 | // compile time error, instead of converting the string to bool for the | ||||||||||
2306 | // isSigned parameter. | ||||||||||
2307 | Value *CreateIntCast(Value *, Type *, const char *) = delete; | ||||||||||
2308 | |||||||||||
2309 | //===--------------------------------------------------------------------===// | ||||||||||
2310 | // Instruction creation methods: Compare Instructions | ||||||||||
2311 | //===--------------------------------------------------------------------===// | ||||||||||
2312 | |||||||||||
2313 | Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
2314 | return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name); | ||||||||||
2315 | } | ||||||||||
2316 | |||||||||||
2317 | Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
2318 | return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name); | ||||||||||
2319 | } | ||||||||||
2320 | |||||||||||
2321 | Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
2322 | return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name); | ||||||||||
2323 | } | ||||||||||
2324 | |||||||||||
2325 | Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
2326 | return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name); | ||||||||||
2327 | } | ||||||||||
2328 | |||||||||||
2329 | Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
2330 | return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name); | ||||||||||
2331 | } | ||||||||||
2332 | |||||||||||
2333 | Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
2334 | return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name); | ||||||||||
2335 | } | ||||||||||
2336 | |||||||||||
2337 | Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
2338 | return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name); | ||||||||||
2339 | } | ||||||||||
2340 | |||||||||||
2341 | Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
2342 | return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name); | ||||||||||
2343 | } | ||||||||||
2344 | |||||||||||
2345 | Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
2346 | return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name); | ||||||||||
2347 | } | ||||||||||
2348 | |||||||||||
2349 | Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
2350 | return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name); | ||||||||||
2351 | } | ||||||||||
2352 | |||||||||||
2353 | Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
2354 | MDNode *FPMathTag = nullptr) { | ||||||||||
2355 | return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag); | ||||||||||
2356 | } | ||||||||||
2357 | |||||||||||
2358 | Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
2359 | MDNode *FPMathTag = nullptr) { | ||||||||||
2360 | return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag); | ||||||||||
2361 | } | ||||||||||
2362 | |||||||||||
2363 | Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
2364 | MDNode *FPMathTag = nullptr) { | ||||||||||
2365 | return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag); | ||||||||||
2366 | } | ||||||||||
2367 | |||||||||||
2368 | Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
2369 | MDNode *FPMathTag = nullptr) { | ||||||||||
2370 | return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag); | ||||||||||
2371 | } | ||||||||||
2372 | |||||||||||
2373 | Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
2374 | MDNode *FPMathTag = nullptr) { | ||||||||||
2375 | return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag); | ||||||||||
2376 | } | ||||||||||
2377 | |||||||||||
2378 | Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
2379 | MDNode *FPMathTag = nullptr) { | ||||||||||
2380 | return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag); | ||||||||||
2381 | } | ||||||||||
2382 | |||||||||||
2383 | Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
2384 | MDNode *FPMathTag = nullptr) { | ||||||||||
2385 | return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag); | ||||||||||
2386 | } | ||||||||||
2387 | |||||||||||
2388 | Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
2389 | MDNode *FPMathTag = nullptr) { | ||||||||||
2390 | return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag); | ||||||||||
2391 | } | ||||||||||
2392 | |||||||||||
2393 | Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
2394 | MDNode *FPMathTag = nullptr) { | ||||||||||
2395 | return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag); | ||||||||||
2396 | } | ||||||||||
2397 | |||||||||||
2398 | Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
2399 | MDNode *FPMathTag = nullptr) { | ||||||||||
2400 | return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag); | ||||||||||
2401 | } | ||||||||||
2402 | |||||||||||
2403 | Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
2404 | MDNode *FPMathTag = nullptr) { | ||||||||||
2405 | return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag); | ||||||||||
2406 | } | ||||||||||
2407 | |||||||||||
2408 | Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
2409 | MDNode *FPMathTag = nullptr) { | ||||||||||
2410 | return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag); | ||||||||||
2411 | } | ||||||||||
2412 | |||||||||||
2413 | Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
2414 | MDNode *FPMathTag = nullptr) { | ||||||||||
2415 | return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag); | ||||||||||
2416 | } | ||||||||||
2417 | |||||||||||
2418 | Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "", | ||||||||||
2419 | MDNode *FPMathTag = nullptr) { | ||||||||||
2420 | return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag); | ||||||||||
2421 | } | ||||||||||
2422 | |||||||||||
2423 | Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, | ||||||||||
2424 | const Twine &Name = "") { | ||||||||||
2425 | if (auto *LC = dyn_cast<Constant>(LHS)) | ||||||||||
2426 | if (auto *RC = dyn_cast<Constant>(RHS)) | ||||||||||
2427 | return Insert(Folder.CreateICmp(P, LC, RC), Name); | ||||||||||
2428 | return Insert(new ICmpInst(P, LHS, RHS), Name); | ||||||||||
2429 | } | ||||||||||
2430 | |||||||||||
2431 | // Create a quiet floating-point comparison (i.e. one that raises an FP | ||||||||||
2432 | // exception only in the case where an input is a signaling NaN). | ||||||||||
2433 | // Note that this differs from CreateFCmpS only if IsFPConstrained is true. | ||||||||||
2434 | Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, | ||||||||||
2435 | const Twine &Name = "", MDNode *FPMathTag = nullptr) { | ||||||||||
2436 | return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, false); | ||||||||||
2437 | } | ||||||||||
2438 | |||||||||||
2439 | // Create a signaling floating-point comparison (i.e. one that raises an FP | ||||||||||
2440 | // exception whenever an input is any NaN, signaling or quiet). | ||||||||||
2441 | // Note that this differs from CreateFCmp only if IsFPConstrained is true. | ||||||||||
2442 | Value *CreateFCmpS(CmpInst::Predicate P, Value *LHS, Value *RHS, | ||||||||||
2443 | const Twine &Name = "", MDNode *FPMathTag = nullptr) { | ||||||||||
2444 | return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, true); | ||||||||||
2445 | } | ||||||||||
2446 | |||||||||||
2447 | private: | ||||||||||
2448 | // Helper routine to create either a signaling or a quiet FP comparison. | ||||||||||
2449 | Value *CreateFCmpHelper(CmpInst::Predicate P, Value *LHS, Value *RHS, | ||||||||||
2450 | const Twine &Name, MDNode *FPMathTag, | ||||||||||
2451 | bool IsSignaling) { | ||||||||||
2452 | if (IsFPConstrained) { | ||||||||||
2453 | auto ID = IsSignaling ? Intrinsic::experimental_constrained_fcmps | ||||||||||
2454 | : Intrinsic::experimental_constrained_fcmp; | ||||||||||
2455 | return CreateConstrainedFPCmp(ID, P, LHS, RHS, Name); | ||||||||||
2456 | } | ||||||||||
2457 | |||||||||||
2458 | if (auto *LC = dyn_cast<Constant>(LHS)) | ||||||||||
2459 | if (auto *RC = dyn_cast<Constant>(RHS)) | ||||||||||
2460 | return Insert(Folder.CreateFCmp(P, LC, RC), Name); | ||||||||||
2461 | return Insert(setFPAttrs(new FCmpInst(P, LHS, RHS), FPMathTag, FMF), Name); | ||||||||||
2462 | } | ||||||||||
2463 | |||||||||||
2464 | public: | ||||||||||
2465 | CallInst *CreateConstrainedFPCmp( | ||||||||||
2466 | Intrinsic::ID ID, CmpInst::Predicate P, Value *L, Value *R, | ||||||||||
2467 | const Twine &Name = "", | ||||||||||
2468 | Optional<fp::ExceptionBehavior> Except = None) { | ||||||||||
2469 | Value *PredicateV = getConstrainedFPPredicate(P); | ||||||||||
2470 | Value *ExceptV = getConstrainedFPExcept(Except); | ||||||||||
2471 | |||||||||||
2472 | CallInst *C = CreateIntrinsic(ID, {L->getType()}, | ||||||||||
2473 | {L, R, PredicateV, ExceptV}, nullptr, Name); | ||||||||||
2474 | setConstrainedFPCallAttr(C); | ||||||||||
2475 | return C; | ||||||||||
2476 | } | ||||||||||
2477 | |||||||||||
2478 | //===--------------------------------------------------------------------===// | ||||||||||
2479 | // Instruction creation methods: Other Instructions | ||||||||||
2480 | //===--------------------------------------------------------------------===// | ||||||||||
2481 | |||||||||||
2482 | PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues, | ||||||||||
2483 | const Twine &Name = "") { | ||||||||||
2484 | PHINode *Phi = PHINode::Create(Ty, NumReservedValues); | ||||||||||
2485 | if (isa<FPMathOperator>(Phi)) | ||||||||||
2486 | setFPAttrs(Phi, nullptr /* MDNode* */, FMF); | ||||||||||
2487 | return Insert(Phi, Name); | ||||||||||
2488 | } | ||||||||||
2489 | |||||||||||
2490 | CallInst *CreateCall(FunctionType *FTy, Value *Callee, | ||||||||||
2491 | ArrayRef<Value *> Args = None, const Twine &Name = "", | ||||||||||
2492 | MDNode *FPMathTag = nullptr) { | ||||||||||
2493 | CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles); | ||||||||||
2494 | if (IsFPConstrained) | ||||||||||
2495 | setConstrainedFPCallAttr(CI); | ||||||||||
2496 | if (isa<FPMathOperator>(CI)) | ||||||||||
2497 | setFPAttrs(CI, FPMathTag, FMF); | ||||||||||
2498 | return Insert(CI, Name); | ||||||||||
2499 | } | ||||||||||
2500 | |||||||||||
2501 | CallInst *CreateCall(FunctionType *FTy, Value *Callee, ArrayRef<Value *> Args, | ||||||||||
2502 | ArrayRef<OperandBundleDef> OpBundles, | ||||||||||
2503 | const Twine &Name = "", MDNode *FPMathTag = nullptr) { | ||||||||||
2504 | CallInst *CI = CallInst::Create(FTy, Callee, Args, OpBundles); | ||||||||||
2505 | if (IsFPConstrained) | ||||||||||
2506 | setConstrainedFPCallAttr(CI); | ||||||||||
2507 | if (isa<FPMathOperator>(CI)) | ||||||||||
2508 | setFPAttrs(CI, FPMathTag, FMF); | ||||||||||
2509 | return Insert(CI, Name); | ||||||||||
2510 | } | ||||||||||
2511 | |||||||||||
2512 | CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args = None, | ||||||||||
2513 | const Twine &Name = "", MDNode *FPMathTag = nullptr) { | ||||||||||
2514 | return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, Name, | ||||||||||
2515 | FPMathTag); | ||||||||||
2516 | } | ||||||||||
2517 | |||||||||||
2518 | CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args, | ||||||||||
2519 | ArrayRef<OperandBundleDef> OpBundles, | ||||||||||
2520 | const Twine &Name = "", MDNode *FPMathTag = nullptr) { | ||||||||||
2521 | return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, | ||||||||||
2522 | OpBundles, Name, FPMathTag); | ||||||||||
2523 | } | ||||||||||
2524 | |||||||||||
2525 | // Deprecated [opaque pointer types] | ||||||||||
2526 | CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args = None, | ||||||||||
2527 | const Twine &Name = "", MDNode *FPMathTag = nullptr) { | ||||||||||
2528 | return CreateCall( | ||||||||||
2529 | cast<FunctionType>(Callee->getType()->getPointerElementType()), Callee, | ||||||||||
2530 | Args, Name, FPMathTag); | ||||||||||
2531 | } | ||||||||||
2532 | |||||||||||
2533 | // Deprecated [opaque pointer types] | ||||||||||
2534 | CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args, | ||||||||||
2535 | ArrayRef<OperandBundleDef> OpBundles, | ||||||||||
2536 | const Twine &Name = "", MDNode *FPMathTag = nullptr) { | ||||||||||
2537 | return CreateCall( | ||||||||||
2538 | cast<FunctionType>(Callee->getType()->getPointerElementType()), Callee, | ||||||||||
2539 | Args, OpBundles, Name, FPMathTag); | ||||||||||
2540 | } | ||||||||||
2541 | |||||||||||
2542 | CallInst *CreateConstrainedFPCall( | ||||||||||
2543 | Function *Callee, ArrayRef<Value *> Args, const Twine &Name = "", | ||||||||||
2544 | Optional<fp::RoundingMode> Rounding = None, | ||||||||||
2545 | Optional<fp::ExceptionBehavior> Except = None) { | ||||||||||
2546 | llvm::SmallVector<Value *, 6> UseArgs; | ||||||||||
2547 | |||||||||||
2548 | for (auto *OneArg : Args) | ||||||||||
2549 | UseArgs.push_back(OneArg); | ||||||||||
2550 | bool HasRoundingMD = false; | ||||||||||
2551 | switch (Callee->getIntrinsicID()) { | ||||||||||
2552 | default: | ||||||||||
2553 | break; | ||||||||||
2554 | #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \ | ||||||||||
2555 | case Intrinsic::INTRINSIC: \ | ||||||||||
2556 | HasRoundingMD = ROUND_MODE; \ | ||||||||||
2557 | break; | ||||||||||
2558 | #include "llvm/IR/ConstrainedOps.def" | ||||||||||
2559 | } | ||||||||||
2560 | if (HasRoundingMD) | ||||||||||
2561 | UseArgs.push_back(getConstrainedFPRounding(Rounding)); | ||||||||||
2562 | UseArgs.push_back(getConstrainedFPExcept(Except)); | ||||||||||
2563 | |||||||||||
2564 | CallInst *C = CreateCall(Callee, UseArgs, Name); | ||||||||||
2565 | setConstrainedFPCallAttr(C); | ||||||||||
2566 | return C; | ||||||||||
2567 | } | ||||||||||
2568 | |||||||||||
2569 | Value *CreateSelect(Value *C, Value *True, Value *False, | ||||||||||
2570 | const Twine &Name = "", Instruction *MDFrom = nullptr) { | ||||||||||
2571 | if (auto *CC
| ||||||||||
2572 | if (auto *TC = dyn_cast<Constant>(True)) | ||||||||||
2573 | if (auto *FC = dyn_cast<Constant>(False)) | ||||||||||
2574 | return Insert(Folder.CreateSelect(CC, TC, FC), Name); | ||||||||||
2575 | |||||||||||
2576 | SelectInst *Sel = SelectInst::Create(C, True, False); | ||||||||||
2577 | if (MDFrom) { | ||||||||||
2578 | MDNode *Prof = MDFrom->getMetadata(LLVMContext::MD_prof); | ||||||||||
2579 | MDNode *Unpred = MDFrom->getMetadata(LLVMContext::MD_unpredictable); | ||||||||||
2580 | Sel = addBranchMetadata(Sel, Prof, Unpred); | ||||||||||
2581 | } | ||||||||||
2582 | if (isa<FPMathOperator>(Sel)) | ||||||||||
2583 | setFPAttrs(Sel, nullptr /* MDNode* */, FMF); | ||||||||||
2584 | return Insert(Sel, Name); | ||||||||||
2585 | } | ||||||||||
2586 | |||||||||||
2587 | VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") { | ||||||||||
2588 | return Insert(new VAArgInst(List, Ty), Name); | ||||||||||
2589 | } | ||||||||||
2590 | |||||||||||
2591 | Value *CreateExtractElement(Value *Vec, Value *Idx, | ||||||||||
2592 | const Twine &Name = "") { | ||||||||||
2593 | if (auto *VC = dyn_cast<Constant>(Vec)) | ||||||||||
2594 | if (auto *IC = dyn_cast<Constant>(Idx)) | ||||||||||
2595 | return Insert(Folder.CreateExtractElement(VC, IC), Name); | ||||||||||
2596 | return Insert(ExtractElementInst::Create(Vec, Idx), Name); | ||||||||||
2597 | } | ||||||||||
2598 | |||||||||||
2599 | Value *CreateExtractElement(Value *Vec, uint64_t Idx, | ||||||||||
2600 | const Twine &Name = "") { | ||||||||||
2601 | return CreateExtractElement(Vec, getInt64(Idx), Name); | ||||||||||
2602 | } | ||||||||||
2603 | |||||||||||
2604 | Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx, | ||||||||||
2605 | const Twine &Name = "") { | ||||||||||
2606 | if (auto *VC = dyn_cast<Constant>(Vec)) | ||||||||||
2607 | if (auto *NC = dyn_cast<Constant>(NewElt)) | ||||||||||
2608 | if (auto *IC = dyn_cast<Constant>(Idx)) | ||||||||||
2609 | return Insert(Folder.CreateInsertElement(VC, NC, IC), Name); | ||||||||||
2610 | return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name); | ||||||||||
2611 | } | ||||||||||
2612 | |||||||||||
2613 | Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx, | ||||||||||
2614 | const Twine &Name = "") { | ||||||||||
2615 | return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name); | ||||||||||
2616 | } | ||||||||||
2617 | |||||||||||
2618 | Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask, | ||||||||||
2619 | const Twine &Name = "") { | ||||||||||
2620 | if (auto *V1C = dyn_cast<Constant>(V1)) | ||||||||||
2621 | if (auto *V2C = dyn_cast<Constant>(V2)) | ||||||||||
2622 | if (auto *MC = dyn_cast<Constant>(Mask)) | ||||||||||
2623 | return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name); | ||||||||||
2624 | return Insert(new ShuffleVectorInst(V1, V2, Mask), Name); | ||||||||||
2625 | } | ||||||||||
2626 | |||||||||||
2627 | Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<uint32_t> IntMask, | ||||||||||
2628 | const Twine &Name = "") { | ||||||||||
2629 | Value *Mask = ConstantDataVector::get(Context, IntMask); | ||||||||||
2630 | return CreateShuffleVector(V1, V2, Mask, Name); | ||||||||||
2631 | } | ||||||||||
2632 | |||||||||||
2633 | Value *CreateExtractValue(Value *Agg, | ||||||||||
2634 | ArrayRef<unsigned> Idxs, | ||||||||||
2635 | const Twine &Name = "") { | ||||||||||
2636 | if (auto *AggC = dyn_cast<Constant>(Agg)) | ||||||||||
2637 | return Insert(Folder.CreateExtractValue(AggC, Idxs), Name); | ||||||||||
2638 | return Insert(ExtractValueInst::Create(Agg, Idxs), Name); | ||||||||||
2639 | } | ||||||||||
2640 | |||||||||||
2641 | Value *CreateInsertValue(Value *Agg, Value *Val, | ||||||||||
2642 | ArrayRef<unsigned> Idxs, | ||||||||||
2643 | const Twine &Name = "") { | ||||||||||
2644 | if (auto *AggC = dyn_cast<Constant>(Agg)) | ||||||||||
2645 | if (auto *ValC = dyn_cast<Constant>(Val)) | ||||||||||
2646 | return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name); | ||||||||||
2647 | return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name); | ||||||||||
2648 | } | ||||||||||
2649 | |||||||||||
2650 | LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses, | ||||||||||
2651 | const Twine &Name = "") { | ||||||||||
2652 | return Insert(LandingPadInst::Create(Ty, NumClauses), Name); | ||||||||||
2653 | } | ||||||||||
2654 | |||||||||||
2655 | Value *CreateFreeze(Value *V, const Twine &Name = "") { | ||||||||||
2656 | return Insert(new FreezeInst(V), Name); | ||||||||||
2657 | } | ||||||||||
2658 | |||||||||||
2659 | //===--------------------------------------------------------------------===// | ||||||||||
2660 | // Utility creation methods | ||||||||||
2661 | //===--------------------------------------------------------------------===// | ||||||||||
2662 | |||||||||||
2663 | /// Return an i1 value testing if \p Arg is null. | ||||||||||
2664 | Value *CreateIsNull(Value *Arg, const Twine &Name = "") { | ||||||||||
2665 | return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()), | ||||||||||
2666 | Name); | ||||||||||
2667 | } | ||||||||||
2668 | |||||||||||
2669 | /// Return an i1 value testing if \p Arg is not null. | ||||||||||
2670 | Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") { | ||||||||||
2671 | return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()), | ||||||||||
2672 | Name); | ||||||||||
2673 | } | ||||||||||
2674 | |||||||||||
2675 | /// Return the i64 difference between two pointer values, dividing out | ||||||||||
2676 | /// the size of the pointed-to objects. | ||||||||||
2677 | /// | ||||||||||
2678 | /// This is intended to implement C-style pointer subtraction. As such, the | ||||||||||
2679 | /// pointers must be appropriately aligned for their element types and | ||||||||||
2680 | /// pointing into the same object. | ||||||||||
2681 | Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") { | ||||||||||
2682 | assert(LHS->getType() == RHS->getType() &&((LHS->getType() == RHS->getType() && "Pointer subtraction operand types must match!" ) ? static_cast<void> (0) : __assert_fail ("LHS->getType() == RHS->getType() && \"Pointer subtraction operand types must match!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2683, __PRETTY_FUNCTION__)) | ||||||||||
2683 | "Pointer subtraction operand types must match!")((LHS->getType() == RHS->getType() && "Pointer subtraction operand types must match!" ) ? static_cast<void> (0) : __assert_fail ("LHS->getType() == RHS->getType() && \"Pointer subtraction operand types must match!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2683, __PRETTY_FUNCTION__)); | ||||||||||
2684 | auto *ArgType = cast<PointerType>(LHS->getType()); | ||||||||||
2685 | Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context)); | ||||||||||
2686 | Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context)); | ||||||||||
2687 | Value *Difference = CreateSub(LHS_int, RHS_int); | ||||||||||
2688 | return CreateExactSDiv(Difference, | ||||||||||
2689 | ConstantExpr::getSizeOf(ArgType->getElementType()), | ||||||||||
2690 | Name); | ||||||||||
2691 | } | ||||||||||
2692 | |||||||||||
2693 | /// Create a launder.invariant.group intrinsic call. If Ptr type is | ||||||||||
2694 | /// different from pointer to i8, it's casted to pointer to i8 in the same | ||||||||||
2695 | /// address space before call and casted back to Ptr type after call. | ||||||||||
2696 | Value *CreateLaunderInvariantGroup(Value *Ptr) { | ||||||||||
2697 | assert(isa<PointerType>(Ptr->getType()) &&((isa<PointerType>(Ptr->getType()) && "launder.invariant.group only applies to pointers." ) ? static_cast<void> (0) : __assert_fail ("isa<PointerType>(Ptr->getType()) && \"launder.invariant.group only applies to pointers.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2698, __PRETTY_FUNCTION__)) | ||||||||||
2698 | "launder.invariant.group only applies to pointers.")((isa<PointerType>(Ptr->getType()) && "launder.invariant.group only applies to pointers." ) ? static_cast<void> (0) : __assert_fail ("isa<PointerType>(Ptr->getType()) && \"launder.invariant.group only applies to pointers.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2698, __PRETTY_FUNCTION__)); | ||||||||||
2699 | // FIXME: we could potentially avoid casts to/from i8*. | ||||||||||
2700 | auto *PtrType = Ptr->getType(); | ||||||||||
2701 | auto *Int8PtrTy = getInt8PtrTy(PtrType->getPointerAddressSpace()); | ||||||||||
2702 | if (PtrType != Int8PtrTy) | ||||||||||
2703 | Ptr = CreateBitCast(Ptr, Int8PtrTy); | ||||||||||
2704 | Module *M = BB->getParent()->getParent(); | ||||||||||
2705 | Function *FnLaunderInvariantGroup = Intrinsic::getDeclaration( | ||||||||||
2706 | M, Intrinsic::launder_invariant_group, {Int8PtrTy}); | ||||||||||
2707 | |||||||||||
2708 | assert(FnLaunderInvariantGroup->getReturnType() == Int8PtrTy &&((FnLaunderInvariantGroup->getReturnType() == Int8PtrTy && FnLaunderInvariantGroup->getFunctionType()->getParamType (0) == Int8PtrTy && "LaunderInvariantGroup should take and return the same type" ) ? static_cast<void> (0) : __assert_fail ("FnLaunderInvariantGroup->getReturnType() == Int8PtrTy && FnLaunderInvariantGroup->getFunctionType()->getParamType(0) == Int8PtrTy && \"LaunderInvariantGroup should take and return the same type\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2711, __PRETTY_FUNCTION__)) | ||||||||||
2709 | FnLaunderInvariantGroup->getFunctionType()->getParamType(0) ==((FnLaunderInvariantGroup->getReturnType() == Int8PtrTy && FnLaunderInvariantGroup->getFunctionType()->getParamType (0) == Int8PtrTy && "LaunderInvariantGroup should take and return the same type" ) ? static_cast<void> (0) : __assert_fail ("FnLaunderInvariantGroup->getReturnType() == Int8PtrTy && FnLaunderInvariantGroup->getFunctionType()->getParamType(0) == Int8PtrTy && \"LaunderInvariantGroup should take and return the same type\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2711, __PRETTY_FUNCTION__)) | ||||||||||
2710 | Int8PtrTy &&((FnLaunderInvariantGroup->getReturnType() == Int8PtrTy && FnLaunderInvariantGroup->getFunctionType()->getParamType (0) == Int8PtrTy && "LaunderInvariantGroup should take and return the same type" ) ? static_cast<void> (0) : __assert_fail ("FnLaunderInvariantGroup->getReturnType() == Int8PtrTy && FnLaunderInvariantGroup->getFunctionType()->getParamType(0) == Int8PtrTy && \"LaunderInvariantGroup should take and return the same type\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2711, __PRETTY_FUNCTION__)) | ||||||||||
2711 | "LaunderInvariantGroup should take and return the same type")((FnLaunderInvariantGroup->getReturnType() == Int8PtrTy && FnLaunderInvariantGroup->getFunctionType()->getParamType (0) == Int8PtrTy && "LaunderInvariantGroup should take and return the same type" ) ? static_cast<void> (0) : __assert_fail ("FnLaunderInvariantGroup->getReturnType() == Int8PtrTy && FnLaunderInvariantGroup->getFunctionType()->getParamType(0) == Int8PtrTy && \"LaunderInvariantGroup should take and return the same type\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2711, __PRETTY_FUNCTION__)); | ||||||||||
2712 | |||||||||||
2713 | CallInst *Fn = CreateCall(FnLaunderInvariantGroup, {Ptr}); | ||||||||||
2714 | |||||||||||
2715 | if (PtrType != Int8PtrTy) | ||||||||||
2716 | return CreateBitCast(Fn, PtrType); | ||||||||||
2717 | return Fn; | ||||||||||
2718 | } | ||||||||||
2719 | |||||||||||
2720 | /// \brief Create a strip.invariant.group intrinsic call. If Ptr type is | ||||||||||
2721 | /// different from pointer to i8, it's casted to pointer to i8 in the same | ||||||||||
2722 | /// address space before call and casted back to Ptr type after call. | ||||||||||
2723 | Value *CreateStripInvariantGroup(Value *Ptr) { | ||||||||||
2724 | assert(isa<PointerType>(Ptr->getType()) &&((isa<PointerType>(Ptr->getType()) && "strip.invariant.group only applies to pointers." ) ? static_cast<void> (0) : __assert_fail ("isa<PointerType>(Ptr->getType()) && \"strip.invariant.group only applies to pointers.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2725, __PRETTY_FUNCTION__)) | ||||||||||
2725 | "strip.invariant.group only applies to pointers.")((isa<PointerType>(Ptr->getType()) && "strip.invariant.group only applies to pointers." ) ? static_cast<void> (0) : __assert_fail ("isa<PointerType>(Ptr->getType()) && \"strip.invariant.group only applies to pointers.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2725, __PRETTY_FUNCTION__)); | ||||||||||
2726 | |||||||||||
2727 | // FIXME: we could potentially avoid casts to/from i8*. | ||||||||||
2728 | auto *PtrType = Ptr->getType(); | ||||||||||
2729 | auto *Int8PtrTy = getInt8PtrTy(PtrType->getPointerAddressSpace()); | ||||||||||
2730 | if (PtrType != Int8PtrTy) | ||||||||||
2731 | Ptr = CreateBitCast(Ptr, Int8PtrTy); | ||||||||||
2732 | Module *M = BB->getParent()->getParent(); | ||||||||||
2733 | Function *FnStripInvariantGroup = Intrinsic::getDeclaration( | ||||||||||
2734 | M, Intrinsic::strip_invariant_group, {Int8PtrTy}); | ||||||||||
2735 | |||||||||||
2736 | assert(FnStripInvariantGroup->getReturnType() == Int8PtrTy &&((FnStripInvariantGroup->getReturnType() == Int8PtrTy && FnStripInvariantGroup->getFunctionType()->getParamType (0) == Int8PtrTy && "StripInvariantGroup should take and return the same type" ) ? static_cast<void> (0) : __assert_fail ("FnStripInvariantGroup->getReturnType() == Int8PtrTy && FnStripInvariantGroup->getFunctionType()->getParamType(0) == Int8PtrTy && \"StripInvariantGroup should take and return the same type\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2739, __PRETTY_FUNCTION__)) | ||||||||||
2737 | FnStripInvariantGroup->getFunctionType()->getParamType(0) ==((FnStripInvariantGroup->getReturnType() == Int8PtrTy && FnStripInvariantGroup->getFunctionType()->getParamType (0) == Int8PtrTy && "StripInvariantGroup should take and return the same type" ) ? static_cast<void> (0) : __assert_fail ("FnStripInvariantGroup->getReturnType() == Int8PtrTy && FnStripInvariantGroup->getFunctionType()->getParamType(0) == Int8PtrTy && \"StripInvariantGroup should take and return the same type\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2739, __PRETTY_FUNCTION__)) | ||||||||||
2738 | Int8PtrTy &&((FnStripInvariantGroup->getReturnType() == Int8PtrTy && FnStripInvariantGroup->getFunctionType()->getParamType (0) == Int8PtrTy && "StripInvariantGroup should take and return the same type" ) ? static_cast<void> (0) : __assert_fail ("FnStripInvariantGroup->getReturnType() == Int8PtrTy && FnStripInvariantGroup->getFunctionType()->getParamType(0) == Int8PtrTy && \"StripInvariantGroup should take and return the same type\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2739, __PRETTY_FUNCTION__)) | ||||||||||
2739 | "StripInvariantGroup should take and return the same type")((FnStripInvariantGroup->getReturnType() == Int8PtrTy && FnStripInvariantGroup->getFunctionType()->getParamType (0) == Int8PtrTy && "StripInvariantGroup should take and return the same type" ) ? static_cast<void> (0) : __assert_fail ("FnStripInvariantGroup->getReturnType() == Int8PtrTy && FnStripInvariantGroup->getFunctionType()->getParamType(0) == Int8PtrTy && \"StripInvariantGroup should take and return the same type\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2739, __PRETTY_FUNCTION__)); | ||||||||||
2740 | |||||||||||
2741 | CallInst *Fn = CreateCall(FnStripInvariantGroup, {Ptr}); | ||||||||||
2742 | |||||||||||
2743 | if (PtrType != Int8PtrTy) | ||||||||||
2744 | return CreateBitCast(Fn, PtrType); | ||||||||||
2745 | return Fn; | ||||||||||
2746 | } | ||||||||||
2747 | |||||||||||
2748 | /// Return a vector value that contains \arg V broadcasted to \p | ||||||||||
2749 | /// NumElts elements. | ||||||||||
2750 | Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") { | ||||||||||
2751 | assert(NumElts > 0 && "Cannot splat to an empty vector!")((NumElts > 0 && "Cannot splat to an empty vector!" ) ? static_cast<void> (0) : __assert_fail ("NumElts > 0 && \"Cannot splat to an empty vector!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2751, __PRETTY_FUNCTION__)); | ||||||||||
2752 | |||||||||||
2753 | // First insert it into an undef vector so we can shuffle it. | ||||||||||
2754 | Type *I32Ty = getInt32Ty(); | ||||||||||
2755 | Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts)); | ||||||||||
2756 | V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0), | ||||||||||
2757 | Name + ".splatinsert"); | ||||||||||
2758 | |||||||||||
2759 | // Shuffle the value across the desired number of elements. | ||||||||||
2760 | Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts)); | ||||||||||
2761 | return CreateShuffleVector(V, Undef, Zeros, Name + ".splat"); | ||||||||||
2762 | } | ||||||||||
2763 | |||||||||||
2764 | /// Return a value that has been extracted from a larger integer type. | ||||||||||
2765 | Value *CreateExtractInteger(const DataLayout &DL, Value *From, | ||||||||||
2766 | IntegerType *ExtractedTy, uint64_t Offset, | ||||||||||
2767 | const Twine &Name) { | ||||||||||
2768 | auto *IntTy = cast<IntegerType>(From->getType()); | ||||||||||
2769 | assert(DL.getTypeStoreSize(ExtractedTy) + Offset <=((DL.getTypeStoreSize(ExtractedTy) + Offset <= DL.getTypeStoreSize (IntTy) && "Element extends past full value") ? static_cast <void> (0) : __assert_fail ("DL.getTypeStoreSize(ExtractedTy) + Offset <= DL.getTypeStoreSize(IntTy) && \"Element extends past full value\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2771, __PRETTY_FUNCTION__)) | ||||||||||
2770 | DL.getTypeStoreSize(IntTy) &&((DL.getTypeStoreSize(ExtractedTy) + Offset <= DL.getTypeStoreSize (IntTy) && "Element extends past full value") ? static_cast <void> (0) : __assert_fail ("DL.getTypeStoreSize(ExtractedTy) + Offset <= DL.getTypeStoreSize(IntTy) && \"Element extends past full value\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2771, __PRETTY_FUNCTION__)) | ||||||||||
2771 | "Element extends past full value")((DL.getTypeStoreSize(ExtractedTy) + Offset <= DL.getTypeStoreSize (IntTy) && "Element extends past full value") ? static_cast <void> (0) : __assert_fail ("DL.getTypeStoreSize(ExtractedTy) + Offset <= DL.getTypeStoreSize(IntTy) && \"Element extends past full value\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2771, __PRETTY_FUNCTION__)); | ||||||||||
2772 | uint64_t ShAmt = 8 * Offset; | ||||||||||
2773 | Value *V = From; | ||||||||||
2774 | if (DL.isBigEndian()) | ||||||||||
2775 | ShAmt = 8 * (DL.getTypeStoreSize(IntTy) - | ||||||||||
2776 | DL.getTypeStoreSize(ExtractedTy) - Offset); | ||||||||||
2777 | if (ShAmt) { | ||||||||||
2778 | V = CreateLShr(V, ShAmt, Name + ".shift"); | ||||||||||
2779 | } | ||||||||||
2780 | assert(ExtractedTy->getBitWidth() <= IntTy->getBitWidth() &&((ExtractedTy->getBitWidth() <= IntTy->getBitWidth() && "Cannot extract to a larger integer!") ? static_cast <void> (0) : __assert_fail ("ExtractedTy->getBitWidth() <= IntTy->getBitWidth() && \"Cannot extract to a larger integer!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2781, __PRETTY_FUNCTION__)) | ||||||||||
2781 | "Cannot extract to a larger integer!")((ExtractedTy->getBitWidth() <= IntTy->getBitWidth() && "Cannot extract to a larger integer!") ? static_cast <void> (0) : __assert_fail ("ExtractedTy->getBitWidth() <= IntTy->getBitWidth() && \"Cannot extract to a larger integer!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2781, __PRETTY_FUNCTION__)); | ||||||||||
2782 | if (ExtractedTy != IntTy) { | ||||||||||
2783 | V = CreateTrunc(V, ExtractedTy, Name + ".trunc"); | ||||||||||
2784 | } | ||||||||||
2785 | return V; | ||||||||||
2786 | } | ||||||||||
2787 | |||||||||||
2788 | Value *CreatePreserveArrayAccessIndex(Type *ElTy, Value *Base, | ||||||||||
2789 | unsigned Dimension, unsigned LastIndex, | ||||||||||
2790 | MDNode *DbgInfo) { | ||||||||||
2791 | assert(isa<PointerType>(Base->getType()) &&((isa<PointerType>(Base->getType()) && "Invalid Base ptr type for preserve.array.access.index." ) ? static_cast<void> (0) : __assert_fail ("isa<PointerType>(Base->getType()) && \"Invalid Base ptr type for preserve.array.access.index.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2792, __PRETTY_FUNCTION__)) | ||||||||||
2792 | "Invalid Base ptr type for preserve.array.access.index.")((isa<PointerType>(Base->getType()) && "Invalid Base ptr type for preserve.array.access.index." ) ? static_cast<void> (0) : __assert_fail ("isa<PointerType>(Base->getType()) && \"Invalid Base ptr type for preserve.array.access.index.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2792, __PRETTY_FUNCTION__)); | ||||||||||
2793 | auto *BaseType = Base->getType(); | ||||||||||
2794 | |||||||||||
2795 | Value *LastIndexV = getInt32(LastIndex); | ||||||||||
2796 | Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0); | ||||||||||
2797 | SmallVector<Value *, 4> IdxList; | ||||||||||
2798 | for (unsigned I = 0; I < Dimension; ++I) | ||||||||||
2799 | IdxList.push_back(Zero); | ||||||||||
2800 | IdxList.push_back(LastIndexV); | ||||||||||
2801 | |||||||||||
2802 | Type *ResultType = | ||||||||||
2803 | GetElementPtrInst::getGEPReturnType(ElTy, Base, IdxList); | ||||||||||
2804 | |||||||||||
2805 | Module *M = BB->getParent()->getParent(); | ||||||||||
2806 | Function *FnPreserveArrayAccessIndex = Intrinsic::getDeclaration( | ||||||||||
2807 | M, Intrinsic::preserve_array_access_index, {ResultType, BaseType}); | ||||||||||
2808 | |||||||||||
2809 | Value *DimV = getInt32(Dimension); | ||||||||||
2810 | CallInst *Fn = | ||||||||||
2811 | CreateCall(FnPreserveArrayAccessIndex, {Base, DimV, LastIndexV}); | ||||||||||
2812 | if (DbgInfo) | ||||||||||
2813 | Fn->setMetadata(LLVMContext::MD_preserve_access_index, DbgInfo); | ||||||||||
2814 | |||||||||||
2815 | return Fn; | ||||||||||
2816 | } | ||||||||||
2817 | |||||||||||
2818 | Value *CreatePreserveUnionAccessIndex(Value *Base, unsigned FieldIndex, | ||||||||||
2819 | MDNode *DbgInfo) { | ||||||||||
2820 | assert(isa<PointerType>(Base->getType()) &&((isa<PointerType>(Base->getType()) && "Invalid Base ptr type for preserve.union.access.index." ) ? static_cast<void> (0) : __assert_fail ("isa<PointerType>(Base->getType()) && \"Invalid Base ptr type for preserve.union.access.index.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2821, __PRETTY_FUNCTION__)) | ||||||||||
2821 | "Invalid Base ptr type for preserve.union.access.index.")((isa<PointerType>(Base->getType()) && "Invalid Base ptr type for preserve.union.access.index." ) ? static_cast<void> (0) : __assert_fail ("isa<PointerType>(Base->getType()) && \"Invalid Base ptr type for preserve.union.access.index.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2821, __PRETTY_FUNCTION__)); | ||||||||||
2822 | auto *BaseType = Base->getType(); | ||||||||||
2823 | |||||||||||
2824 | Module *M = BB->getParent()->getParent(); | ||||||||||
2825 | Function *FnPreserveUnionAccessIndex = Intrinsic::getDeclaration( | ||||||||||
2826 | M, Intrinsic::preserve_union_access_index, {BaseType, BaseType}); | ||||||||||
2827 | |||||||||||
2828 | Value *DIIndex = getInt32(FieldIndex); | ||||||||||
2829 | CallInst *Fn = | ||||||||||
2830 | CreateCall(FnPreserveUnionAccessIndex, {Base, DIIndex}); | ||||||||||
2831 | if (DbgInfo) | ||||||||||
2832 | Fn->setMetadata(LLVMContext::MD_preserve_access_index, DbgInfo); | ||||||||||
2833 | |||||||||||
2834 | return Fn; | ||||||||||
2835 | } | ||||||||||
2836 | |||||||||||
2837 | Value *CreatePreserveStructAccessIndex(Type *ElTy, Value *Base, | ||||||||||
2838 | unsigned Index, unsigned FieldIndex, | ||||||||||
2839 | MDNode *DbgInfo) { | ||||||||||
2840 | assert(isa<PointerType>(Base->getType()) &&((isa<PointerType>(Base->getType()) && "Invalid Base ptr type for preserve.struct.access.index." ) ? static_cast<void> (0) : __assert_fail ("isa<PointerType>(Base->getType()) && \"Invalid Base ptr type for preserve.struct.access.index.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2841, __PRETTY_FUNCTION__)) | ||||||||||
2841 | "Invalid Base ptr type for preserve.struct.access.index.")((isa<PointerType>(Base->getType()) && "Invalid Base ptr type for preserve.struct.access.index." ) ? static_cast<void> (0) : __assert_fail ("isa<PointerType>(Base->getType()) && \"Invalid Base ptr type for preserve.struct.access.index.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2841, __PRETTY_FUNCTION__)); | ||||||||||
2842 | auto *BaseType = Base->getType(); | ||||||||||
2843 | |||||||||||
2844 | Value *GEPIndex = getInt32(Index); | ||||||||||
2845 | Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0); | ||||||||||
2846 | Type *ResultType = | ||||||||||
2847 | GetElementPtrInst::getGEPReturnType(ElTy, Base, {Zero, GEPIndex}); | ||||||||||
2848 | |||||||||||
2849 | Module *M = BB->getParent()->getParent(); | ||||||||||
2850 | Function *FnPreserveStructAccessIndex = Intrinsic::getDeclaration( | ||||||||||
2851 | M, Intrinsic::preserve_struct_access_index, {ResultType, BaseType}); | ||||||||||
2852 | |||||||||||
2853 | Value *DIIndex = getInt32(FieldIndex); | ||||||||||
2854 | CallInst *Fn = CreateCall(FnPreserveStructAccessIndex, | ||||||||||
2855 | {Base, GEPIndex, DIIndex}); | ||||||||||
2856 | if (DbgInfo) | ||||||||||
2857 | Fn->setMetadata(LLVMContext::MD_preserve_access_index, DbgInfo); | ||||||||||
2858 | |||||||||||
2859 | return Fn; | ||||||||||
2860 | } | ||||||||||
2861 | |||||||||||
2862 | private: | ||||||||||
2863 | /// Helper function that creates an assume intrinsic call that | ||||||||||
2864 | /// represents an alignment assumption on the provided Ptr, Mask, Type | ||||||||||
2865 | /// and Offset. It may be sometimes useful to do some other logic | ||||||||||
2866 | /// based on this alignment check, thus it can be stored into 'TheCheck'. | ||||||||||
2867 | CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL, | ||||||||||
2868 | Value *PtrValue, Value *Mask, | ||||||||||
2869 | Type *IntPtrTy, Value *OffsetValue, | ||||||||||
2870 | Value **TheCheck) { | ||||||||||
2871 | Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint"); | ||||||||||
2872 | |||||||||||
2873 | if (OffsetValue) { | ||||||||||
2874 | bool IsOffsetZero = false; | ||||||||||
2875 | if (const auto *CI = dyn_cast<ConstantInt>(OffsetValue)) | ||||||||||
2876 | IsOffsetZero = CI->isZero(); | ||||||||||
2877 | |||||||||||
2878 | if (!IsOffsetZero) { | ||||||||||
2879 | if (OffsetValue->getType() != IntPtrTy) | ||||||||||
2880 | OffsetValue = CreateIntCast(OffsetValue, IntPtrTy, /*isSigned*/ true, | ||||||||||
2881 | "offsetcast"); | ||||||||||
2882 | PtrIntValue = CreateSub(PtrIntValue, OffsetValue, "offsetptr"); | ||||||||||
2883 | } | ||||||||||
2884 | } | ||||||||||
2885 | |||||||||||
2886 | Value *Zero = ConstantInt::get(IntPtrTy, 0); | ||||||||||
2887 | Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr"); | ||||||||||
2888 | Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond"); | ||||||||||
2889 | if (TheCheck) | ||||||||||
2890 | *TheCheck = InvCond; | ||||||||||
2891 | |||||||||||
2892 | return CreateAssumption(InvCond); | ||||||||||
2893 | } | ||||||||||
2894 | |||||||||||
2895 | public: | ||||||||||
2896 | /// Create an assume intrinsic call that represents an alignment | ||||||||||
2897 | /// assumption on the provided pointer. | ||||||||||
2898 | /// | ||||||||||
2899 | /// An optional offset can be provided, and if it is provided, the offset | ||||||||||
2900 | /// must be subtracted from the provided pointer to get the pointer with the | ||||||||||
2901 | /// specified alignment. | ||||||||||
2902 | /// | ||||||||||
2903 | /// It may be sometimes useful to do some other logic | ||||||||||
2904 | /// based on this alignment check, thus it can be stored into 'TheCheck'. | ||||||||||
2905 | CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue, | ||||||||||
2906 | unsigned Alignment, | ||||||||||
2907 | Value *OffsetValue = nullptr, | ||||||||||
2908 | Value **TheCheck = nullptr) { | ||||||||||
2909 | assert(isa<PointerType>(PtrValue->getType()) &&((isa<PointerType>(PtrValue->getType()) && "trying to create an alignment assumption on a non-pointer?" ) ? static_cast<void> (0) : __assert_fail ("isa<PointerType>(PtrValue->getType()) && \"trying to create an alignment assumption on a non-pointer?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2910, __PRETTY_FUNCTION__)) | ||||||||||
2910 | "trying to create an alignment assumption on a non-pointer?")((isa<PointerType>(PtrValue->getType()) && "trying to create an alignment assumption on a non-pointer?" ) ? static_cast<void> (0) : __assert_fail ("isa<PointerType>(PtrValue->getType()) && \"trying to create an alignment assumption on a non-pointer?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2910, __PRETTY_FUNCTION__)); | ||||||||||
2911 | assert(Alignment != 0 && "Invalid Alignment")((Alignment != 0 && "Invalid Alignment") ? static_cast <void> (0) : __assert_fail ("Alignment != 0 && \"Invalid Alignment\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2911, __PRETTY_FUNCTION__)); | ||||||||||
2912 | auto *PtrTy = cast<PointerType>(PtrValue->getType()); | ||||||||||
2913 | Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace()); | ||||||||||
2914 | |||||||||||
2915 | Value *Mask = ConstantInt::get(IntPtrTy, Alignment - 1); | ||||||||||
2916 | return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy, | ||||||||||
2917 | OffsetValue, TheCheck); | ||||||||||
2918 | } | ||||||||||
2919 | |||||||||||
2920 | /// Create an assume intrinsic call that represents an alignment | ||||||||||
2921 | /// assumption on the provided pointer. | ||||||||||
2922 | /// | ||||||||||
2923 | /// An optional offset can be provided, and if it is provided, the offset | ||||||||||
2924 | /// must be subtracted from the provided pointer to get the pointer with the | ||||||||||
2925 | /// specified alignment. | ||||||||||
2926 | /// | ||||||||||
2927 | /// It may be sometimes useful to do some other logic | ||||||||||
2928 | /// based on this alignment check, thus it can be stored into 'TheCheck'. | ||||||||||
2929 | /// | ||||||||||
2930 | /// This overload handles the condition where the Alignment is dependent | ||||||||||
2931 | /// on an existing value rather than a static value. | ||||||||||
2932 | CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue, | ||||||||||
2933 | Value *Alignment, | ||||||||||
2934 | Value *OffsetValue = nullptr, | ||||||||||
2935 | Value **TheCheck = nullptr) { | ||||||||||
2936 | assert(isa<PointerType>(PtrValue->getType()) &&((isa<PointerType>(PtrValue->getType()) && "trying to create an alignment assumption on a non-pointer?" ) ? static_cast<void> (0) : __assert_fail ("isa<PointerType>(PtrValue->getType()) && \"trying to create an alignment assumption on a non-pointer?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2937, __PRETTY_FUNCTION__)) | ||||||||||
2937 | "trying to create an alignment assumption on a non-pointer?")((isa<PointerType>(PtrValue->getType()) && "trying to create an alignment assumption on a non-pointer?" ) ? static_cast<void> (0) : __assert_fail ("isa<PointerType>(PtrValue->getType()) && \"trying to create an alignment assumption on a non-pointer?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/IRBuilder.h" , 2937, __PRETTY_FUNCTION__)); | ||||||||||
2938 | auto *PtrTy = cast<PointerType>(PtrValue->getType()); | ||||||||||
2939 | Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace()); | ||||||||||
2940 | |||||||||||
2941 | if (Alignment->getType() != IntPtrTy) | ||||||||||
2942 | Alignment = CreateIntCast(Alignment, IntPtrTy, /*isSigned*/ false, | ||||||||||
2943 | "alignmentcast"); | ||||||||||
2944 | |||||||||||
2945 | Value *Mask = CreateSub(Alignment, ConstantInt::get(IntPtrTy, 1), "mask"); | ||||||||||
2946 | |||||||||||
2947 | return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy, | ||||||||||
2948 | OffsetValue, TheCheck); | ||||||||||
2949 | } | ||||||||||
2950 | }; | ||||||||||
2951 | |||||||||||
2952 | /// This provides a uniform API for creating instructions and inserting | ||||||||||
2953 | /// them into a basic block: either at the end of a BasicBlock, or at a specific | ||||||||||
2954 | /// iterator location in a block. | ||||||||||
2955 | /// | ||||||||||
2956 | /// Note that the builder does not expose the full generality of LLVM | ||||||||||
2957 | /// instructions. For access to extra instruction properties, use the mutators | ||||||||||
2958 | /// (e.g. setVolatile) on the instructions after they have been | ||||||||||
2959 | /// created. Convenience state exists to specify fast-math flags and fp-math | ||||||||||
2960 | /// tags. | ||||||||||
2961 | /// | ||||||||||
2962 | /// The first template argument specifies a class to use for creating constants. | ||||||||||
2963 | /// This defaults to creating minimally folded constants. The second template | ||||||||||
2964 | /// argument allows clients to specify custom insertion hooks that are called on | ||||||||||
2965 | /// every newly created insertion. | ||||||||||
2966 | template <typename FolderTy = ConstantFolder, | ||||||||||
2967 | typename InserterTy = IRBuilderDefaultInserter> | ||||||||||
2968 | class IRBuilder : public IRBuilderBase { | ||||||||||
2969 | private: | ||||||||||
2970 | FolderTy Folder; | ||||||||||
2971 | InserterTy Inserter; | ||||||||||
2972 | |||||||||||
2973 | public: | ||||||||||
2974 | IRBuilder(LLVMContext &C, FolderTy Folder, InserterTy Inserter = InserterTy(), | ||||||||||
2975 | MDNode *FPMathTag = nullptr, | ||||||||||
2976 | ArrayRef<OperandBundleDef> OpBundles = None) | ||||||||||
2977 | : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles), | ||||||||||
2978 | Folder(Folder), Inserter(Inserter) {} | ||||||||||
2979 | |||||||||||
2980 | explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr, | ||||||||||
2981 | ArrayRef<OperandBundleDef> OpBundles = None) | ||||||||||
2982 | : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles) {} | ||||||||||
2983 | |||||||||||
2984 | explicit IRBuilder(BasicBlock *TheBB, FolderTy Folder, | ||||||||||
2985 | MDNode *FPMathTag = nullptr, | ||||||||||
2986 | ArrayRef<OperandBundleDef> OpBundles = None) | ||||||||||
2987 | : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter, | ||||||||||
2988 | FPMathTag, OpBundles), Folder(Folder) { | ||||||||||
2989 | SetInsertPoint(TheBB); | ||||||||||
2990 | } | ||||||||||
2991 | |||||||||||
2992 | explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr, | ||||||||||
2993 | ArrayRef<OperandBundleDef> OpBundles = None) | ||||||||||
2994 | : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter, | ||||||||||
2995 | FPMathTag, OpBundles) { | ||||||||||
2996 | SetInsertPoint(TheBB); | ||||||||||
2997 | } | ||||||||||
2998 | |||||||||||
2999 | explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr, | ||||||||||
3000 | ArrayRef<OperandBundleDef> OpBundles = None) | ||||||||||
3001 | : IRBuilderBase(IP->getContext(), this->Folder, this->Inserter, | ||||||||||
3002 | FPMathTag, OpBundles) { | ||||||||||
3003 | SetInsertPoint(IP); | ||||||||||
3004 | } | ||||||||||
3005 | |||||||||||
3006 | IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, FolderTy Folder, | ||||||||||
3007 | MDNode *FPMathTag = nullptr, | ||||||||||
3008 | ArrayRef<OperandBundleDef> OpBundles = None) | ||||||||||
3009 | : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter, | ||||||||||
3010 | FPMathTag, OpBundles), Folder(Folder) { | ||||||||||
3011 | SetInsertPoint(TheBB, IP); | ||||||||||
3012 | } | ||||||||||
3013 | |||||||||||
3014 | IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, | ||||||||||
3015 | MDNode *FPMathTag = nullptr, | ||||||||||
3016 | ArrayRef<OperandBundleDef> OpBundles = None) | ||||||||||
3017 | : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter, | ||||||||||
3018 | FPMathTag, OpBundles) { | ||||||||||
3019 | SetInsertPoint(TheBB, IP); | ||||||||||
3020 | } | ||||||||||
3021 | |||||||||||
3022 | /// Avoid copying the full IRBuilder. Prefer using InsertPointGuard | ||||||||||
3023 | /// or FastMathFlagGuard instead. | ||||||||||
3024 | IRBuilder(const IRBuilder &) = delete; | ||||||||||
3025 | |||||||||||
3026 | InserterTy &getInserter() { return Inserter; } | ||||||||||
3027 | }; | ||||||||||
3028 | |||||||||||
3029 | // Create wrappers for C Binding types (see CBindingWrapping.h). | ||||||||||
3030 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)inline IRBuilder<> *unwrap(LLVMBuilderRef P) { return reinterpret_cast <IRBuilder<>*>(P); } inline LLVMBuilderRef wrap(const IRBuilder<> *P) { return reinterpret_cast<LLVMBuilderRef >(const_cast<IRBuilder<>*>(P)); } | ||||||||||
3031 | |||||||||||
3032 | } // end namespace llvm | ||||||||||
3033 | |||||||||||
3034 | #endif // LLVM_IR_IRBUILDER_H |
1 | //===- llvm/Instructions.h - Instruction subclass definitions ---*- 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 exposes the class definitions of all of the subclasses of the | |||
10 | // Instruction class. This is meant to be an easy way to get access to all | |||
11 | // instruction subclasses. | |||
12 | // | |||
13 | //===----------------------------------------------------------------------===// | |||
14 | ||||
15 | #ifndef LLVM_IR_INSTRUCTIONS_H | |||
16 | #define LLVM_IR_INSTRUCTIONS_H | |||
17 | ||||
18 | #include "llvm/ADT/ArrayRef.h" | |||
19 | #include "llvm/ADT/None.h" | |||
20 | #include "llvm/ADT/STLExtras.h" | |||
21 | #include "llvm/ADT/SmallVector.h" | |||
22 | #include "llvm/ADT/StringRef.h" | |||
23 | #include "llvm/ADT/Twine.h" | |||
24 | #include "llvm/ADT/iterator.h" | |||
25 | #include "llvm/ADT/iterator_range.h" | |||
26 | #include "llvm/IR/Attributes.h" | |||
27 | #include "llvm/IR/BasicBlock.h" | |||
28 | #include "llvm/IR/CallingConv.h" | |||
29 | #include "llvm/IR/Constant.h" | |||
30 | #include "llvm/IR/DerivedTypes.h" | |||
31 | #include "llvm/IR/Function.h" | |||
32 | #include "llvm/IR/InstrTypes.h" | |||
33 | #include "llvm/IR/Instruction.h" | |||
34 | #include "llvm/IR/OperandTraits.h" | |||
35 | #include "llvm/IR/Type.h" | |||
36 | #include "llvm/IR/Use.h" | |||
37 | #include "llvm/IR/User.h" | |||
38 | #include "llvm/IR/Value.h" | |||
39 | #include "llvm/Support/AtomicOrdering.h" | |||
40 | #include "llvm/Support/Casting.h" | |||
41 | #include "llvm/Support/ErrorHandling.h" | |||
42 | #include <cassert> | |||
43 | #include <cstddef> | |||
44 | #include <cstdint> | |||
45 | #include <iterator> | |||
46 | ||||
47 | namespace llvm { | |||
48 | ||||
49 | class APInt; | |||
50 | class ConstantInt; | |||
51 | class DataLayout; | |||
52 | class LLVMContext; | |||
53 | ||||
54 | //===----------------------------------------------------------------------===// | |||
55 | // AllocaInst Class | |||
56 | //===----------------------------------------------------------------------===// | |||
57 | ||||
58 | /// an instruction to allocate memory on the stack | |||
59 | class AllocaInst : public UnaryInstruction { | |||
60 | Type *AllocatedType; | |||
61 | ||||
62 | protected: | |||
63 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
64 | friend class Instruction; | |||
65 | ||||
66 | AllocaInst *cloneImpl() const; | |||
67 | ||||
68 | public: | |||
69 | explicit AllocaInst(Type *Ty, unsigned AddrSpace, | |||
70 | Value *ArraySize = nullptr, | |||
71 | const Twine &Name = "", | |||
72 | Instruction *InsertBefore = nullptr); | |||
73 | AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, | |||
74 | const Twine &Name, BasicBlock *InsertAtEnd); | |||
75 | ||||
76 | AllocaInst(Type *Ty, unsigned AddrSpace, | |||
77 | const Twine &Name, Instruction *InsertBefore = nullptr); | |||
78 | AllocaInst(Type *Ty, unsigned AddrSpace, | |||
79 | const Twine &Name, BasicBlock *InsertAtEnd); | |||
80 | ||||
81 | AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, MaybeAlign Align, | |||
82 | const Twine &Name = "", Instruction *InsertBefore = nullptr); | |||
83 | AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, MaybeAlign Align, | |||
84 | const Twine &Name, BasicBlock *InsertAtEnd); | |||
85 | ||||
86 | /// Return true if there is an allocation size parameter to the allocation | |||
87 | /// instruction that is not 1. | |||
88 | bool isArrayAllocation() const; | |||
89 | ||||
90 | /// Get the number of elements allocated. For a simple allocation of a single | |||
91 | /// element, this will return a constant 1 value. | |||
92 | const Value *getArraySize() const { return getOperand(0); } | |||
93 | Value *getArraySize() { return getOperand(0); } | |||
94 | ||||
95 | /// Overload to return most specific pointer type. | |||
96 | PointerType *getType() const { | |||
97 | return cast<PointerType>(Instruction::getType()); | |||
98 | } | |||
99 | ||||
100 | /// Get allocation size in bits. Returns None if size can't be determined, | |||
101 | /// e.g. in case of a VLA. | |||
102 | Optional<uint64_t> getAllocationSizeInBits(const DataLayout &DL) const; | |||
103 | ||||
104 | /// Return the type that is being allocated by the instruction. | |||
105 | Type *getAllocatedType() const { return AllocatedType; } | |||
106 | /// for use only in special circumstances that need to generically | |||
107 | /// transform a whole instruction (eg: IR linking and vectorization). | |||
108 | void setAllocatedType(Type *Ty) { AllocatedType = Ty; } | |||
109 | ||||
110 | /// Return the alignment of the memory that is being allocated by the | |||
111 | /// instruction. | |||
112 | MaybeAlign getAlign() const { | |||
113 | return decodeMaybeAlign(getSubclassDataFromInstruction() & 31); | |||
114 | } | |||
115 | // FIXME: Remove this one transition to Align is over. | |||
116 | unsigned getAlignment() const { | |||
117 | if (const auto MA = getAlign()) | |||
118 | return MA->value(); | |||
119 | return 0; | |||
120 | } | |||
121 | void setAlignment(MaybeAlign Align); | |||
122 | ||||
123 | /// Return true if this alloca is in the entry block of the function and is a | |||
124 | /// constant size. If so, the code generator will fold it into the | |||
125 | /// prolog/epilog code, so it is basically free. | |||
126 | bool isStaticAlloca() const; | |||
127 | ||||
128 | /// Return true if this alloca is used as an inalloca argument to a call. Such | |||
129 | /// allocas are never considered static even if they are in the entry block. | |||
130 | bool isUsedWithInAlloca() const { | |||
131 | return getSubclassDataFromInstruction() & 32; | |||
132 | } | |||
133 | ||||
134 | /// Specify whether this alloca is used to represent the arguments to a call. | |||
135 | void setUsedWithInAlloca(bool V) { | |||
136 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) | | |||
137 | (V ? 32 : 0)); | |||
138 | } | |||
139 | ||||
140 | /// Return true if this alloca is used as a swifterror argument to a call. | |||
141 | bool isSwiftError() const { | |||
142 | return getSubclassDataFromInstruction() & 64; | |||
143 | } | |||
144 | ||||
145 | /// Specify whether this alloca is used to represent a swifterror. | |||
146 | void setSwiftError(bool V) { | |||
147 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~64) | | |||
148 | (V ? 64 : 0)); | |||
149 | } | |||
150 | ||||
151 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
152 | static bool classof(const Instruction *I) { | |||
153 | return (I->getOpcode() == Instruction::Alloca); | |||
154 | } | |||
155 | static bool classof(const Value *V) { | |||
156 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
157 | } | |||
158 | ||||
159 | private: | |||
160 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
161 | // method so that subclasses cannot accidentally use it. | |||
162 | void setInstructionSubclassData(unsigned short D) { | |||
163 | Instruction::setInstructionSubclassData(D); | |||
164 | } | |||
165 | }; | |||
166 | ||||
167 | //===----------------------------------------------------------------------===// | |||
168 | // LoadInst Class | |||
169 | //===----------------------------------------------------------------------===// | |||
170 | ||||
171 | /// An instruction for reading from memory. This uses the SubclassData field in | |||
172 | /// Value to store whether or not the load is volatile. | |||
173 | class LoadInst : public UnaryInstruction { | |||
174 | void AssertOK(); | |||
175 | ||||
176 | protected: | |||
177 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
178 | friend class Instruction; | |||
179 | ||||
180 | LoadInst *cloneImpl() const; | |||
181 | ||||
182 | public: | |||
183 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr = "", | |||
184 | Instruction *InsertBefore = nullptr); | |||
185 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
186 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, | |||
187 | Instruction *InsertBefore = nullptr); | |||
188 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, | |||
189 | BasicBlock *InsertAtEnd); | |||
190 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, | |||
191 | MaybeAlign Align, Instruction *InsertBefore = nullptr); | |||
192 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, | |||
193 | MaybeAlign Align, BasicBlock *InsertAtEnd); | |||
194 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, | |||
195 | MaybeAlign Align, AtomicOrdering Order, | |||
196 | SyncScope::ID SSID = SyncScope::System, | |||
197 | Instruction *InsertBefore = nullptr); | |||
198 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, | |||
199 | MaybeAlign Align, AtomicOrdering Order, SyncScope::ID SSID, | |||
200 | BasicBlock *InsertAtEnd); | |||
201 | ||||
202 | // Deprecated [opaque pointer types] | |||
203 | explicit LoadInst(Value *Ptr, const Twine &NameStr = "", | |||
204 | Instruction *InsertBefore = nullptr) | |||
205 | : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr, | |||
206 | InsertBefore) {} | |||
207 | LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd) | |||
208 | : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr, | |||
209 | InsertAtEnd) {} | |||
210 | LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, | |||
211 | Instruction *InsertBefore = nullptr) | |||
212 | : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr, | |||
213 | isVolatile, InsertBefore) {} | |||
214 | LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, | |||
215 | BasicBlock *InsertAtEnd) | |||
216 | : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr, | |||
217 | isVolatile, InsertAtEnd) {} | |||
218 | LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, MaybeAlign Align, | |||
219 | Instruction *InsertBefore = nullptr) | |||
220 | : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr, | |||
221 | isVolatile, Align, InsertBefore) {} | |||
222 | LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, MaybeAlign Align, | |||
223 | BasicBlock *InsertAtEnd) | |||
224 | : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr, | |||
225 | isVolatile, Align, InsertAtEnd) {} | |||
226 | LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, MaybeAlign Align, | |||
227 | AtomicOrdering Order, SyncScope::ID SSID = SyncScope::System, | |||
228 | Instruction *InsertBefore = nullptr) | |||
229 | : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr, | |||
230 | isVolatile, Align, Order, SSID, InsertBefore) {} | |||
231 | LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, MaybeAlign Align, | |||
232 | AtomicOrdering Order, SyncScope::ID SSID, BasicBlock *InsertAtEnd) | |||
233 | : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr, | |||
234 | isVolatile, Align, Order, SSID, InsertAtEnd) {} | |||
235 | ||||
236 | /// Return true if this is a load from a volatile memory location. | |||
237 | bool isVolatile() const { return getSubclassDataFromInstruction() & 1; } | |||
238 | ||||
239 | /// Specify whether this is a volatile load or not. | |||
240 | void setVolatile(bool V) { | |||
241 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | | |||
242 | (V ? 1 : 0)); | |||
243 | } | |||
244 | ||||
245 | /// Return the alignment of the access that is being performed. | |||
246 | /// FIXME: Remove this function once transition to Align is over. | |||
247 | /// Use getAlign() instead. | |||
248 | unsigned getAlignment() const { | |||
249 | if (const auto MA = getAlign()) | |||
250 | return MA->value(); | |||
251 | return 0; | |||
252 | } | |||
253 | ||||
254 | /// Return the alignment of the access that is being performed. | |||
255 | MaybeAlign getAlign() const { | |||
256 | return decodeMaybeAlign((getSubclassDataFromInstruction() >> 1) & 31); | |||
257 | } | |||
258 | ||||
259 | void setAlignment(MaybeAlign Alignment); | |||
260 | ||||
261 | /// Returns the ordering constraint of this load instruction. | |||
262 | AtomicOrdering getOrdering() const { | |||
263 | return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7); | |||
264 | } | |||
265 | ||||
266 | /// Sets the ordering constraint of this load instruction. May not be Release | |||
267 | /// or AcquireRelease. | |||
268 | void setOrdering(AtomicOrdering Ordering) { | |||
269 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) | | |||
270 | ((unsigned)Ordering << 7)); | |||
271 | } | |||
272 | ||||
273 | /// Returns the synchronization scope ID of this load instruction. | |||
274 | SyncScope::ID getSyncScopeID() const { | |||
275 | return SSID; | |||
276 | } | |||
277 | ||||
278 | /// Sets the synchronization scope ID of this load instruction. | |||
279 | void setSyncScopeID(SyncScope::ID SSID) { | |||
280 | this->SSID = SSID; | |||
281 | } | |||
282 | ||||
283 | /// Sets the ordering constraint and the synchronization scope ID of this load | |||
284 | /// instruction. | |||
285 | void setAtomic(AtomicOrdering Ordering, | |||
286 | SyncScope::ID SSID = SyncScope::System) { | |||
287 | setOrdering(Ordering); | |||
288 | setSyncScopeID(SSID); | |||
289 | } | |||
290 | ||||
291 | bool isSimple() const { return !isAtomic() && !isVolatile(); } | |||
292 | ||||
293 | bool isUnordered() const { | |||
294 | return (getOrdering() == AtomicOrdering::NotAtomic || | |||
295 | getOrdering() == AtomicOrdering::Unordered) && | |||
296 | !isVolatile(); | |||
297 | } | |||
298 | ||||
299 | Value *getPointerOperand() { return getOperand(0); } | |||
300 | const Value *getPointerOperand() const { return getOperand(0); } | |||
301 | static unsigned getPointerOperandIndex() { return 0U; } | |||
302 | Type *getPointerOperandType() const { return getPointerOperand()->getType(); } | |||
303 | ||||
304 | /// Returns the address space of the pointer operand. | |||
305 | unsigned getPointerAddressSpace() const { | |||
306 | return getPointerOperandType()->getPointerAddressSpace(); | |||
307 | } | |||
308 | ||||
309 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
310 | static bool classof(const Instruction *I) { | |||
311 | return I->getOpcode() == Instruction::Load; | |||
312 | } | |||
313 | static bool classof(const Value *V) { | |||
314 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
315 | } | |||
316 | ||||
317 | private: | |||
318 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
319 | // method so that subclasses cannot accidentally use it. | |||
320 | void setInstructionSubclassData(unsigned short D) { | |||
321 | Instruction::setInstructionSubclassData(D); | |||
322 | } | |||
323 | ||||
324 | /// The synchronization scope ID of this load instruction. Not quite enough | |||
325 | /// room in SubClassData for everything, so synchronization scope ID gets its | |||
326 | /// own field. | |||
327 | SyncScope::ID SSID; | |||
328 | }; | |||
329 | ||||
330 | //===----------------------------------------------------------------------===// | |||
331 | // StoreInst Class | |||
332 | //===----------------------------------------------------------------------===// | |||
333 | ||||
334 | /// An instruction for storing to memory. | |||
335 | class StoreInst : public Instruction { | |||
336 | void AssertOK(); | |||
337 | ||||
338 | protected: | |||
339 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
340 | friend class Instruction; | |||
341 | ||||
342 | StoreInst *cloneImpl() const; | |||
343 | ||||
344 | public: | |||
345 | StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore); | |||
346 | StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd); | |||
347 | StoreInst(Value *Val, Value *Ptr, bool isVolatile = false, | |||
348 | Instruction *InsertBefore = nullptr); | |||
349 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd); | |||
350 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, MaybeAlign Align, | |||
351 | Instruction *InsertBefore = nullptr); | |||
352 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, MaybeAlign Align, | |||
353 | BasicBlock *InsertAtEnd); | |||
354 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, MaybeAlign Align, | |||
355 | AtomicOrdering Order, SyncScope::ID SSID = SyncScope::System, | |||
356 | Instruction *InsertBefore = nullptr); | |||
357 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, MaybeAlign Align, | |||
358 | AtomicOrdering Order, SyncScope::ID SSID, BasicBlock *InsertAtEnd); | |||
359 | ||||
360 | // allocate space for exactly two operands | |||
361 | void *operator new(size_t s) { | |||
362 | return User::operator new(s, 2); | |||
363 | } | |||
364 | ||||
365 | /// Return true if this is a store to a volatile memory location. | |||
366 | bool isVolatile() const { return getSubclassDataFromInstruction() & 1; } | |||
367 | ||||
368 | /// Specify whether this is a volatile store or not. | |||
369 | void setVolatile(bool V) { | |||
370 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | | |||
371 | (V ? 1 : 0)); | |||
372 | } | |||
373 | ||||
374 | /// Transparently provide more efficient getOperand methods. | |||
375 | 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; | |||
376 | ||||
377 | /// Return the alignment of the access that is being performed | |||
378 | /// FIXME: Remove this function once transition to Align is over. | |||
379 | /// Use getAlign() instead. | |||
380 | unsigned getAlignment() const { | |||
381 | if (const auto MA = getAlign()) | |||
382 | return MA->value(); | |||
383 | return 0; | |||
384 | } | |||
385 | ||||
386 | MaybeAlign getAlign() const { | |||
387 | return decodeMaybeAlign((getSubclassDataFromInstruction() >> 1) & 31); | |||
388 | } | |||
389 | ||||
390 | void setAlignment(MaybeAlign Alignment); | |||
391 | ||||
392 | /// Returns the ordering constraint of this store instruction. | |||
393 | AtomicOrdering getOrdering() const { | |||
394 | return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7); | |||
395 | } | |||
396 | ||||
397 | /// Sets the ordering constraint of this store instruction. May not be | |||
398 | /// Acquire or AcquireRelease. | |||
399 | void setOrdering(AtomicOrdering Ordering) { | |||
400 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) | | |||
401 | ((unsigned)Ordering << 7)); | |||
402 | } | |||
403 | ||||
404 | /// Returns the synchronization scope ID of this store instruction. | |||
405 | SyncScope::ID getSyncScopeID() const { | |||
406 | return SSID; | |||
407 | } | |||
408 | ||||
409 | /// Sets the synchronization scope ID of this store instruction. | |||
410 | void setSyncScopeID(SyncScope::ID SSID) { | |||
411 | this->SSID = SSID; | |||
412 | } | |||
413 | ||||
414 | /// Sets the ordering constraint and the synchronization scope ID of this | |||
415 | /// store instruction. | |||
416 | void setAtomic(AtomicOrdering Ordering, | |||
417 | SyncScope::ID SSID = SyncScope::System) { | |||
418 | setOrdering(Ordering); | |||
419 | setSyncScopeID(SSID); | |||
420 | } | |||
421 | ||||
422 | bool isSimple() const { return !isAtomic() && !isVolatile(); } | |||
423 | ||||
424 | bool isUnordered() const { | |||
425 | return (getOrdering() == AtomicOrdering::NotAtomic || | |||
426 | getOrdering() == AtomicOrdering::Unordered) && | |||
427 | !isVolatile(); | |||
428 | } | |||
429 | ||||
430 | Value *getValueOperand() { return getOperand(0); } | |||
431 | const Value *getValueOperand() const { return getOperand(0); } | |||
432 | ||||
433 | Value *getPointerOperand() { return getOperand(1); } | |||
434 | const Value *getPointerOperand() const { return getOperand(1); } | |||
435 | static unsigned getPointerOperandIndex() { return 1U; } | |||
436 | Type *getPointerOperandType() const { return getPointerOperand()->getType(); } | |||
437 | ||||
438 | /// Returns the address space of the pointer operand. | |||
439 | unsigned getPointerAddressSpace() const { | |||
440 | return getPointerOperandType()->getPointerAddressSpace(); | |||
441 | } | |||
442 | ||||
443 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
444 | static bool classof(const Instruction *I) { | |||
445 | return I->getOpcode() == Instruction::Store; | |||
446 | } | |||
447 | static bool classof(const Value *V) { | |||
448 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
449 | } | |||
450 | ||||
451 | private: | |||
452 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
453 | // method so that subclasses cannot accidentally use it. | |||
454 | void setInstructionSubclassData(unsigned short D) { | |||
455 | Instruction::setInstructionSubclassData(D); | |||
456 | } | |||
457 | ||||
458 | /// The synchronization scope ID of this store instruction. Not quite enough | |||
459 | /// room in SubClassData for everything, so synchronization scope ID gets its | |||
460 | /// own field. | |||
461 | SyncScope::ID SSID; | |||
462 | }; | |||
463 | ||||
464 | template <> | |||
465 | struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> { | |||
466 | }; | |||
467 | ||||
468 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)StoreInst::op_iterator StoreInst::op_begin() { return OperandTraits <StoreInst>::op_begin(this); } StoreInst::const_op_iterator StoreInst::op_begin() const { return OperandTraits<StoreInst >::op_begin(const_cast<StoreInst*>(this)); } StoreInst ::op_iterator StoreInst::op_end() { return OperandTraits<StoreInst >::op_end(this); } StoreInst::const_op_iterator StoreInst:: op_end() const { return OperandTraits<StoreInst>::op_end (const_cast<StoreInst*>(this)); } Value *StoreInst::getOperand (unsigned i_nocapture) const { ((i_nocapture < OperandTraits <StoreInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<StoreInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 468, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<StoreInst>::op_begin(const_cast<StoreInst *>(this))[i_nocapture].get()); } void StoreInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<StoreInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<StoreInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 468, __PRETTY_FUNCTION__)); OperandTraits<StoreInst>:: op_begin(this)[i_nocapture] = Val_nocapture; } unsigned StoreInst ::getNumOperands() const { return OperandTraits<StoreInst> ::operands(this); } template <int Idx_nocapture> Use & StoreInst::Op() { return this->OpFrom<Idx_nocapture> (this); } template <int Idx_nocapture> const Use &StoreInst ::Op() const { return this->OpFrom<Idx_nocapture>(this ); } | |||
469 | ||||
470 | //===----------------------------------------------------------------------===// | |||
471 | // FenceInst Class | |||
472 | //===----------------------------------------------------------------------===// | |||
473 | ||||
474 | /// An instruction for ordering other memory operations. | |||
475 | class FenceInst : public Instruction { | |||
476 | void Init(AtomicOrdering Ordering, SyncScope::ID SSID); | |||
477 | ||||
478 | protected: | |||
479 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
480 | friend class Instruction; | |||
481 | ||||
482 | FenceInst *cloneImpl() const; | |||
483 | ||||
484 | public: | |||
485 | // Ordering may only be Acquire, Release, AcquireRelease, or | |||
486 | // SequentiallyConsistent. | |||
487 | FenceInst(LLVMContext &C, AtomicOrdering Ordering, | |||
488 | SyncScope::ID SSID = SyncScope::System, | |||
489 | Instruction *InsertBefore = nullptr); | |||
490 | FenceInst(LLVMContext &C, AtomicOrdering Ordering, SyncScope::ID SSID, | |||
491 | BasicBlock *InsertAtEnd); | |||
492 | ||||
493 | // allocate space for exactly zero operands | |||
494 | void *operator new(size_t s) { | |||
495 | return User::operator new(s, 0); | |||
496 | } | |||
497 | ||||
498 | /// Returns the ordering constraint of this fence instruction. | |||
499 | AtomicOrdering getOrdering() const { | |||
500 | return AtomicOrdering(getSubclassDataFromInstruction() >> 1); | |||
501 | } | |||
502 | ||||
503 | /// Sets the ordering constraint of this fence instruction. May only be | |||
504 | /// Acquire, Release, AcquireRelease, or SequentiallyConsistent. | |||
505 | void setOrdering(AtomicOrdering Ordering) { | |||
506 | setInstructionSubclassData((getSubclassDataFromInstruction() & 1) | | |||
507 | ((unsigned)Ordering << 1)); | |||
508 | } | |||
509 | ||||
510 | /// Returns the synchronization scope ID of this fence instruction. | |||
511 | SyncScope::ID getSyncScopeID() const { | |||
512 | return SSID; | |||
513 | } | |||
514 | ||||
515 | /// Sets the synchronization scope ID of this fence instruction. | |||
516 | void setSyncScopeID(SyncScope::ID SSID) { | |||
517 | this->SSID = SSID; | |||
518 | } | |||
519 | ||||
520 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
521 | static bool classof(const Instruction *I) { | |||
522 | return I->getOpcode() == Instruction::Fence; | |||
523 | } | |||
524 | static bool classof(const Value *V) { | |||
525 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
526 | } | |||
527 | ||||
528 | private: | |||
529 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
530 | // method so that subclasses cannot accidentally use it. | |||
531 | void setInstructionSubclassData(unsigned short D) { | |||
532 | Instruction::setInstructionSubclassData(D); | |||
533 | } | |||
534 | ||||
535 | /// The synchronization scope ID of this fence instruction. Not quite enough | |||
536 | /// room in SubClassData for everything, so synchronization scope ID gets its | |||
537 | /// own field. | |||
538 | SyncScope::ID SSID; | |||
539 | }; | |||
540 | ||||
541 | //===----------------------------------------------------------------------===// | |||
542 | // AtomicCmpXchgInst Class | |||
543 | //===----------------------------------------------------------------------===// | |||
544 | ||||
545 | /// An instruction that atomically checks whether a | |||
546 | /// specified value is in a memory location, and, if it is, stores a new value | |||
547 | /// there. The value returned by this instruction is a pair containing the | |||
548 | /// original value as first element, and an i1 indicating success (true) or | |||
549 | /// failure (false) as second element. | |||
550 | /// | |||
551 | class AtomicCmpXchgInst : public Instruction { | |||
552 | void Init(Value *Ptr, Value *Cmp, Value *NewVal, | |||
553 | AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering, | |||
554 | SyncScope::ID SSID); | |||
555 | ||||
556 | protected: | |||
557 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
558 | friend class Instruction; | |||
559 | ||||
560 | AtomicCmpXchgInst *cloneImpl() const; | |||
561 | ||||
562 | public: | |||
563 | AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, | |||
564 | AtomicOrdering SuccessOrdering, | |||
565 | AtomicOrdering FailureOrdering, | |||
566 | SyncScope::ID SSID, Instruction *InsertBefore = nullptr); | |||
567 | AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, | |||
568 | AtomicOrdering SuccessOrdering, | |||
569 | AtomicOrdering FailureOrdering, | |||
570 | SyncScope::ID SSID, BasicBlock *InsertAtEnd); | |||
571 | ||||
572 | // allocate space for exactly three operands | |||
573 | void *operator new(size_t s) { | |||
574 | return User::operator new(s, 3); | |||
575 | } | |||
576 | ||||
577 | /// Return true if this is a cmpxchg from a volatile memory | |||
578 | /// location. | |||
579 | /// | |||
580 | bool isVolatile() const { | |||
581 | return getSubclassDataFromInstruction() & 1; | |||
582 | } | |||
583 | ||||
584 | /// Specify whether this is a volatile cmpxchg. | |||
585 | /// | |||
586 | void setVolatile(bool V) { | |||
587 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | | |||
588 | (unsigned)V); | |||
589 | } | |||
590 | ||||
591 | /// Return true if this cmpxchg may spuriously fail. | |||
592 | bool isWeak() const { | |||
593 | return getSubclassDataFromInstruction() & 0x100; | |||
594 | } | |||
595 | ||||
596 | void setWeak(bool IsWeak) { | |||
597 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x100) | | |||
598 | (IsWeak << 8)); | |||
599 | } | |||
600 | ||||
601 | /// Transparently provide more efficient getOperand methods. | |||
602 | 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; | |||
603 | ||||
604 | /// Returns the success ordering constraint of this cmpxchg instruction. | |||
605 | AtomicOrdering getSuccessOrdering() const { | |||
606 | return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7); | |||
607 | } | |||
608 | ||||
609 | /// Sets the success ordering constraint of this cmpxchg instruction. | |||
610 | void setSuccessOrdering(AtomicOrdering Ordering) { | |||
611 | assert(Ordering != AtomicOrdering::NotAtomic &&((Ordering != AtomicOrdering::NotAtomic && "CmpXchg instructions can only be atomic." ) ? static_cast<void> (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"CmpXchg instructions can only be atomic.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 612, __PRETTY_FUNCTION__)) | |||
612 | "CmpXchg instructions can only be atomic.")((Ordering != AtomicOrdering::NotAtomic && "CmpXchg instructions can only be atomic." ) ? static_cast<void> (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"CmpXchg instructions can only be atomic.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 612, __PRETTY_FUNCTION__)); | |||
613 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) | | |||
614 | ((unsigned)Ordering << 2)); | |||
615 | } | |||
616 | ||||
617 | /// Returns the failure ordering constraint of this cmpxchg instruction. | |||
618 | AtomicOrdering getFailureOrdering() const { | |||
619 | return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7); | |||
620 | } | |||
621 | ||||
622 | /// Sets the failure ordering constraint of this cmpxchg instruction. | |||
623 | void setFailureOrdering(AtomicOrdering Ordering) { | |||
624 | assert(Ordering != AtomicOrdering::NotAtomic &&((Ordering != AtomicOrdering::NotAtomic && "CmpXchg instructions can only be atomic." ) ? static_cast<void> (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"CmpXchg instructions can only be atomic.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 625, __PRETTY_FUNCTION__)) | |||
625 | "CmpXchg instructions can only be atomic.")((Ordering != AtomicOrdering::NotAtomic && "CmpXchg instructions can only be atomic." ) ? static_cast<void> (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"CmpXchg instructions can only be atomic.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 625, __PRETTY_FUNCTION__)); | |||
626 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) | | |||
627 | ((unsigned)Ordering << 5)); | |||
628 | } | |||
629 | ||||
630 | /// Returns the synchronization scope ID of this cmpxchg instruction. | |||
631 | SyncScope::ID getSyncScopeID() const { | |||
632 | return SSID; | |||
633 | } | |||
634 | ||||
635 | /// Sets the synchronization scope ID of this cmpxchg instruction. | |||
636 | void setSyncScopeID(SyncScope::ID SSID) { | |||
637 | this->SSID = SSID; | |||
638 | } | |||
639 | ||||
640 | Value *getPointerOperand() { return getOperand(0); } | |||
641 | const Value *getPointerOperand() const { return getOperand(0); } | |||
642 | static unsigned getPointerOperandIndex() { return 0U; } | |||
643 | ||||
644 | Value *getCompareOperand() { return getOperand(1); } | |||
645 | const Value *getCompareOperand() const { return getOperand(1); } | |||
646 | ||||
647 | Value *getNewValOperand() { return getOperand(2); } | |||
648 | const Value *getNewValOperand() const { return getOperand(2); } | |||
649 | ||||
650 | /// Returns the address space of the pointer operand. | |||
651 | unsigned getPointerAddressSpace() const { | |||
652 | return getPointerOperand()->getType()->getPointerAddressSpace(); | |||
653 | } | |||
654 | ||||
655 | /// Returns the strongest permitted ordering on failure, given the | |||
656 | /// desired ordering on success. | |||
657 | /// | |||
658 | /// If the comparison in a cmpxchg operation fails, there is no atomic store | |||
659 | /// so release semantics cannot be provided. So this function drops explicit | |||
660 | /// Release requests from the AtomicOrdering. A SequentiallyConsistent | |||
661 | /// operation would remain SequentiallyConsistent. | |||
662 | static AtomicOrdering | |||
663 | getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) { | |||
664 | switch (SuccessOrdering) { | |||
665 | default: | |||
666 | llvm_unreachable("invalid cmpxchg success ordering")::llvm::llvm_unreachable_internal("invalid cmpxchg success ordering" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 666); | |||
667 | case AtomicOrdering::Release: | |||
668 | case AtomicOrdering::Monotonic: | |||
669 | return AtomicOrdering::Monotonic; | |||
670 | case AtomicOrdering::AcquireRelease: | |||
671 | case AtomicOrdering::Acquire: | |||
672 | return AtomicOrdering::Acquire; | |||
673 | case AtomicOrdering::SequentiallyConsistent: | |||
674 | return AtomicOrdering::SequentiallyConsistent; | |||
675 | } | |||
676 | } | |||
677 | ||||
678 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
679 | static bool classof(const Instruction *I) { | |||
680 | return I->getOpcode() == Instruction::AtomicCmpXchg; | |||
681 | } | |||
682 | static bool classof(const Value *V) { | |||
683 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
684 | } | |||
685 | ||||
686 | private: | |||
687 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
688 | // method so that subclasses cannot accidentally use it. | |||
689 | void setInstructionSubclassData(unsigned short D) { | |||
690 | Instruction::setInstructionSubclassData(D); | |||
691 | } | |||
692 | ||||
693 | /// The synchronization scope ID of this cmpxchg instruction. Not quite | |||
694 | /// enough room in SubClassData for everything, so synchronization scope ID | |||
695 | /// gets its own field. | |||
696 | SyncScope::ID SSID; | |||
697 | }; | |||
698 | ||||
699 | template <> | |||
700 | struct OperandTraits<AtomicCmpXchgInst> : | |||
701 | public FixedNumOperandTraits<AtomicCmpXchgInst, 3> { | |||
702 | }; | |||
703 | ||||
704 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)AtomicCmpXchgInst::op_iterator AtomicCmpXchgInst::op_begin() { return OperandTraits<AtomicCmpXchgInst>::op_begin(this ); } AtomicCmpXchgInst::const_op_iterator AtomicCmpXchgInst:: op_begin() const { return OperandTraits<AtomicCmpXchgInst> ::op_begin(const_cast<AtomicCmpXchgInst*>(this)); } AtomicCmpXchgInst ::op_iterator AtomicCmpXchgInst::op_end() { return OperandTraits <AtomicCmpXchgInst>::op_end(this); } AtomicCmpXchgInst:: const_op_iterator AtomicCmpXchgInst::op_end() const { return OperandTraits <AtomicCmpXchgInst>::op_end(const_cast<AtomicCmpXchgInst *>(this)); } Value *AtomicCmpXchgInst::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<AtomicCmpXchgInst >::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicCmpXchgInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 704, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<AtomicCmpXchgInst>::op_begin(const_cast <AtomicCmpXchgInst*>(this))[i_nocapture].get()); } void AtomicCmpXchgInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((i_nocapture < OperandTraits<AtomicCmpXchgInst> ::operands(this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicCmpXchgInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 704, __PRETTY_FUNCTION__)); OperandTraits<AtomicCmpXchgInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned AtomicCmpXchgInst::getNumOperands() const { return OperandTraits <AtomicCmpXchgInst>::operands(this); } template <int Idx_nocapture> Use &AtomicCmpXchgInst::Op() { return this ->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture > const Use &AtomicCmpXchgInst::Op() const { return this ->OpFrom<Idx_nocapture>(this); } | |||
705 | ||||
706 | //===----------------------------------------------------------------------===// | |||
707 | // AtomicRMWInst Class | |||
708 | //===----------------------------------------------------------------------===// | |||
709 | ||||
710 | /// an instruction that atomically reads a memory location, | |||
711 | /// combines it with another value, and then stores the result back. Returns | |||
712 | /// the old value. | |||
713 | /// | |||
714 | class AtomicRMWInst : public Instruction { | |||
715 | protected: | |||
716 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
717 | friend class Instruction; | |||
718 | ||||
719 | AtomicRMWInst *cloneImpl() const; | |||
720 | ||||
721 | public: | |||
722 | /// This enumeration lists the possible modifications atomicrmw can make. In | |||
723 | /// the descriptions, 'p' is the pointer to the instruction's memory location, | |||
724 | /// 'old' is the initial value of *p, and 'v' is the other value passed to the | |||
725 | /// instruction. These instructions always return 'old'. | |||
726 | enum BinOp { | |||
727 | /// *p = v | |||
728 | Xchg, | |||
729 | /// *p = old + v | |||
730 | Add, | |||
731 | /// *p = old - v | |||
732 | Sub, | |||
733 | /// *p = old & v | |||
734 | And, | |||
735 | /// *p = ~(old & v) | |||
736 | Nand, | |||
737 | /// *p = old | v | |||
738 | Or, | |||
739 | /// *p = old ^ v | |||
740 | Xor, | |||
741 | /// *p = old >signed v ? old : v | |||
742 | Max, | |||
743 | /// *p = old <signed v ? old : v | |||
744 | Min, | |||
745 | /// *p = old >unsigned v ? old : v | |||
746 | UMax, | |||
747 | /// *p = old <unsigned v ? old : v | |||
748 | UMin, | |||
749 | ||||
750 | /// *p = old + v | |||
751 | FAdd, | |||
752 | ||||
753 | /// *p = old - v | |||
754 | FSub, | |||
755 | ||||
756 | FIRST_BINOP = Xchg, | |||
757 | LAST_BINOP = FSub, | |||
758 | BAD_BINOP | |||
759 | }; | |||
760 | ||||
761 | AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, | |||
762 | AtomicOrdering Ordering, SyncScope::ID SSID, | |||
763 | Instruction *InsertBefore = nullptr); | |||
764 | AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, | |||
765 | AtomicOrdering Ordering, SyncScope::ID SSID, | |||
766 | BasicBlock *InsertAtEnd); | |||
767 | ||||
768 | // allocate space for exactly two operands | |||
769 | void *operator new(size_t s) { | |||
770 | return User::operator new(s, 2); | |||
771 | } | |||
772 | ||||
773 | BinOp getOperation() const { | |||
774 | return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5); | |||
775 | } | |||
776 | ||||
777 | static StringRef getOperationName(BinOp Op); | |||
778 | ||||
779 | static bool isFPOperation(BinOp Op) { | |||
780 | switch (Op) { | |||
781 | case AtomicRMWInst::FAdd: | |||
782 | case AtomicRMWInst::FSub: | |||
783 | return true; | |||
784 | default: | |||
785 | return false; | |||
786 | } | |||
787 | } | |||
788 | ||||
789 | void setOperation(BinOp Operation) { | |||
790 | unsigned short SubclassData = getSubclassDataFromInstruction(); | |||
791 | setInstructionSubclassData((SubclassData & 31) | | |||
792 | (Operation << 5)); | |||
793 | } | |||
794 | ||||
795 | /// Return true if this is a RMW on a volatile memory location. | |||
796 | /// | |||
797 | bool isVolatile() const { | |||
798 | return getSubclassDataFromInstruction() & 1; | |||
799 | } | |||
800 | ||||
801 | /// Specify whether this is a volatile RMW or not. | |||
802 | /// | |||
803 | void setVolatile(bool V) { | |||
804 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | | |||
805 | (unsigned)V); | |||
806 | } | |||
807 | ||||
808 | /// Transparently provide more efficient getOperand methods. | |||
809 | 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; | |||
810 | ||||
811 | /// Returns the ordering constraint of this rmw instruction. | |||
812 | AtomicOrdering getOrdering() const { | |||
813 | return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7); | |||
814 | } | |||
815 | ||||
816 | /// Sets the ordering constraint of this rmw instruction. | |||
817 | void setOrdering(AtomicOrdering Ordering) { | |||
818 | assert(Ordering != AtomicOrdering::NotAtomic &&((Ordering != AtomicOrdering::NotAtomic && "atomicrmw instructions can only be atomic." ) ? static_cast<void> (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"atomicrmw instructions can only be atomic.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 819, __PRETTY_FUNCTION__)) | |||
819 | "atomicrmw instructions can only be atomic.")((Ordering != AtomicOrdering::NotAtomic && "atomicrmw instructions can only be atomic." ) ? static_cast<void> (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"atomicrmw instructions can only be atomic.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 819, __PRETTY_FUNCTION__)); | |||
820 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) | | |||
821 | ((unsigned)Ordering << 2)); | |||
822 | } | |||
823 | ||||
824 | /// Returns the synchronization scope ID of this rmw instruction. | |||
825 | SyncScope::ID getSyncScopeID() const { | |||
826 | return SSID; | |||
827 | } | |||
828 | ||||
829 | /// Sets the synchronization scope ID of this rmw instruction. | |||
830 | void setSyncScopeID(SyncScope::ID SSID) { | |||
831 | this->SSID = SSID; | |||
832 | } | |||
833 | ||||
834 | Value *getPointerOperand() { return getOperand(0); } | |||
835 | const Value *getPointerOperand() const { return getOperand(0); } | |||
836 | static unsigned getPointerOperandIndex() { return 0U; } | |||
837 | ||||
838 | Value *getValOperand() { return getOperand(1); } | |||
839 | const Value *getValOperand() const { return getOperand(1); } | |||
840 | ||||
841 | /// Returns the address space of the pointer operand. | |||
842 | unsigned getPointerAddressSpace() const { | |||
843 | return getPointerOperand()->getType()->getPointerAddressSpace(); | |||
844 | } | |||
845 | ||||
846 | bool isFloatingPointOperation() const { | |||
847 | return isFPOperation(getOperation()); | |||
848 | } | |||
849 | ||||
850 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
851 | static bool classof(const Instruction *I) { | |||
852 | return I->getOpcode() == Instruction::AtomicRMW; | |||
853 | } | |||
854 | static bool classof(const Value *V) { | |||
855 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
856 | } | |||
857 | ||||
858 | private: | |||
859 | void Init(BinOp Operation, Value *Ptr, Value *Val, | |||
860 | AtomicOrdering Ordering, SyncScope::ID SSID); | |||
861 | ||||
862 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
863 | // method so that subclasses cannot accidentally use it. | |||
864 | void setInstructionSubclassData(unsigned short D) { | |||
865 | Instruction::setInstructionSubclassData(D); | |||
866 | } | |||
867 | ||||
868 | /// The synchronization scope ID of this rmw instruction. Not quite enough | |||
869 | /// room in SubClassData for everything, so synchronization scope ID gets its | |||
870 | /// own field. | |||
871 | SyncScope::ID SSID; | |||
872 | }; | |||
873 | ||||
874 | template <> | |||
875 | struct OperandTraits<AtomicRMWInst> | |||
876 | : public FixedNumOperandTraits<AtomicRMWInst,2> { | |||
877 | }; | |||
878 | ||||
879 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)AtomicRMWInst::op_iterator AtomicRMWInst::op_begin() { return OperandTraits<AtomicRMWInst>::op_begin(this); } AtomicRMWInst ::const_op_iterator AtomicRMWInst::op_begin() const { return OperandTraits <AtomicRMWInst>::op_begin(const_cast<AtomicRMWInst*> (this)); } AtomicRMWInst::op_iterator AtomicRMWInst::op_end() { return OperandTraits<AtomicRMWInst>::op_end(this); } AtomicRMWInst::const_op_iterator AtomicRMWInst::op_end() const { return OperandTraits<AtomicRMWInst>::op_end(const_cast <AtomicRMWInst*>(this)); } Value *AtomicRMWInst::getOperand (unsigned i_nocapture) const { ((i_nocapture < OperandTraits <AtomicRMWInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicRMWInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 879, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<AtomicRMWInst>::op_begin(const_cast< AtomicRMWInst*>(this))[i_nocapture].get()); } void AtomicRMWInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<AtomicRMWInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicRMWInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 879, __PRETTY_FUNCTION__)); OperandTraits<AtomicRMWInst> ::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned AtomicRMWInst ::getNumOperands() const { return OperandTraits<AtomicRMWInst >::operands(this); } template <int Idx_nocapture> Use &AtomicRMWInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & AtomicRMWInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } | |||
880 | ||||
881 | //===----------------------------------------------------------------------===// | |||
882 | // GetElementPtrInst Class | |||
883 | //===----------------------------------------------------------------------===// | |||
884 | ||||
885 | // checkGEPType - Simple wrapper function to give a better assertion failure | |||
886 | // message on bad indexes for a gep instruction. | |||
887 | // | |||
888 | inline Type *checkGEPType(Type *Ty) { | |||
889 | assert(Ty && "Invalid GetElementPtrInst indices for type!")((Ty && "Invalid GetElementPtrInst indices for type!" ) ? static_cast<void> (0) : __assert_fail ("Ty && \"Invalid GetElementPtrInst indices for type!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 889, __PRETTY_FUNCTION__)); | |||
890 | return Ty; | |||
891 | } | |||
892 | ||||
893 | /// an instruction for type-safe pointer arithmetic to | |||
894 | /// access elements of arrays and structs | |||
895 | /// | |||
896 | class GetElementPtrInst : public Instruction { | |||
897 | Type *SourceElementType; | |||
898 | Type *ResultElementType; | |||
899 | ||||
900 | GetElementPtrInst(const GetElementPtrInst &GEPI); | |||
901 | ||||
902 | /// Constructors - Create a getelementptr instruction with a base pointer an | |||
903 | /// list of indices. The first ctor can optionally insert before an existing | |||
904 | /// instruction, the second appends the new instruction to the specified | |||
905 | /// BasicBlock. | |||
906 | inline GetElementPtrInst(Type *PointeeType, Value *Ptr, | |||
907 | ArrayRef<Value *> IdxList, unsigned Values, | |||
908 | const Twine &NameStr, Instruction *InsertBefore); | |||
909 | inline GetElementPtrInst(Type *PointeeType, Value *Ptr, | |||
910 | ArrayRef<Value *> IdxList, unsigned Values, | |||
911 | const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
912 | ||||
913 | void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr); | |||
914 | ||||
915 | protected: | |||
916 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
917 | friend class Instruction; | |||
918 | ||||
919 | GetElementPtrInst *cloneImpl() const; | |||
920 | ||||
921 | public: | |||
922 | static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr, | |||
923 | ArrayRef<Value *> IdxList, | |||
924 | const Twine &NameStr = "", | |||
925 | Instruction *InsertBefore = nullptr) { | |||
926 | unsigned Values = 1 + unsigned(IdxList.size()); | |||
927 | if (!PointeeType) | |||
928 | PointeeType = | |||
929 | cast<PointerType>(Ptr->getType()->getScalarType())->getElementType(); | |||
930 | else | |||
931 | assert(((PointeeType == cast<PointerType>(Ptr->getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("PointeeType == cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 933, __PRETTY_FUNCTION__)) | |||
932 | PointeeType ==((PointeeType == cast<PointerType>(Ptr->getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("PointeeType == cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 933, __PRETTY_FUNCTION__)) | |||
933 | cast<PointerType>(Ptr->getType()->getScalarType())->getElementType())((PointeeType == cast<PointerType>(Ptr->getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("PointeeType == cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 933, __PRETTY_FUNCTION__)); | |||
934 | return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values, | |||
935 | NameStr, InsertBefore); | |||
936 | } | |||
937 | ||||
938 | static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr, | |||
939 | ArrayRef<Value *> IdxList, | |||
940 | const Twine &NameStr, | |||
941 | BasicBlock *InsertAtEnd) { | |||
942 | unsigned Values = 1 + unsigned(IdxList.size()); | |||
943 | if (!PointeeType) | |||
944 | PointeeType = | |||
945 | cast<PointerType>(Ptr->getType()->getScalarType())->getElementType(); | |||
946 | else | |||
947 | assert(((PointeeType == cast<PointerType>(Ptr->getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("PointeeType == cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 949, __PRETTY_FUNCTION__)) | |||
948 | PointeeType ==((PointeeType == cast<PointerType>(Ptr->getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("PointeeType == cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 949, __PRETTY_FUNCTION__)) | |||
949 | cast<PointerType>(Ptr->getType()->getScalarType())->getElementType())((PointeeType == cast<PointerType>(Ptr->getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("PointeeType == cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 949, __PRETTY_FUNCTION__)); | |||
950 | return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values, | |||
951 | NameStr, InsertAtEnd); | |||
952 | } | |||
953 | ||||
954 | /// Create an "inbounds" getelementptr. See the documentation for the | |||
955 | /// "inbounds" flag in LangRef.html for details. | |||
956 | static GetElementPtrInst *CreateInBounds(Value *Ptr, | |||
957 | ArrayRef<Value *> IdxList, | |||
958 | const Twine &NameStr = "", | |||
959 | Instruction *InsertBefore = nullptr){ | |||
960 | return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertBefore); | |||
961 | } | |||
962 | ||||
963 | static GetElementPtrInst * | |||
964 | CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList, | |||
965 | const Twine &NameStr = "", | |||
966 | Instruction *InsertBefore = nullptr) { | |||
967 | GetElementPtrInst *GEP = | |||
968 | Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore); | |||
969 | GEP->setIsInBounds(true); | |||
970 | return GEP; | |||
971 | } | |||
972 | ||||
973 | static GetElementPtrInst *CreateInBounds(Value *Ptr, | |||
974 | ArrayRef<Value *> IdxList, | |||
975 | const Twine &NameStr, | |||
976 | BasicBlock *InsertAtEnd) { | |||
977 | return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertAtEnd); | |||
978 | } | |||
979 | ||||
980 | static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr, | |||
981 | ArrayRef<Value *> IdxList, | |||
982 | const Twine &NameStr, | |||
983 | BasicBlock *InsertAtEnd) { | |||
984 | GetElementPtrInst *GEP = | |||
985 | Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd); | |||
986 | GEP->setIsInBounds(true); | |||
987 | return GEP; | |||
988 | } | |||
989 | ||||
990 | /// Transparently provide more efficient getOperand methods. | |||
991 | 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; | |||
992 | ||||
993 | Type *getSourceElementType() const { return SourceElementType; } | |||
994 | ||||
995 | void setSourceElementType(Type *Ty) { SourceElementType = Ty; } | |||
996 | void setResultElementType(Type *Ty) { ResultElementType = Ty; } | |||
997 | ||||
998 | Type *getResultElementType() const { | |||
999 | assert(ResultElementType ==((ResultElementType == cast<PointerType>(getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1000, __PRETTY_FUNCTION__)) | |||
1000 | cast<PointerType>(getType()->getScalarType())->getElementType())((ResultElementType == cast<PointerType>(getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1000, __PRETTY_FUNCTION__)); | |||
1001 | return ResultElementType; | |||
1002 | } | |||
1003 | ||||
1004 | /// Returns the address space of this instruction's pointer type. | |||
1005 | unsigned getAddressSpace() const { | |||
1006 | // Note that this is always the same as the pointer operand's address space | |||
1007 | // and that is cheaper to compute, so cheat here. | |||
1008 | return getPointerAddressSpace(); | |||
1009 | } | |||
1010 | ||||
1011 | /// Returns the type of the element that would be loaded with | |||
1012 | /// a load instruction with the specified parameters. | |||
1013 | /// | |||
1014 | /// Null is returned if the indices are invalid for the specified | |||
1015 | /// pointer type. | |||
1016 | /// | |||
1017 | static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList); | |||
1018 | static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList); | |||
1019 | static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList); | |||
1020 | ||||
1021 | inline op_iterator idx_begin() { return op_begin()+1; } | |||
1022 | inline const_op_iterator idx_begin() const { return op_begin()+1; } | |||
1023 | inline op_iterator idx_end() { return op_end(); } | |||
1024 | inline const_op_iterator idx_end() const { return op_end(); } | |||
1025 | ||||
1026 | inline iterator_range<op_iterator> indices() { | |||
1027 | return make_range(idx_begin(), idx_end()); | |||
1028 | } | |||
1029 | ||||
1030 | inline iterator_range<const_op_iterator> indices() const { | |||
1031 | return make_range(idx_begin(), idx_end()); | |||
1032 | } | |||
1033 | ||||
1034 | Value *getPointerOperand() { | |||
1035 | return getOperand(0); | |||
1036 | } | |||
1037 | const Value *getPointerOperand() const { | |||
1038 | return getOperand(0); | |||
1039 | } | |||
1040 | static unsigned getPointerOperandIndex() { | |||
1041 | return 0U; // get index for modifying correct operand. | |||
1042 | } | |||
1043 | ||||
1044 | /// Method to return the pointer operand as a | |||
1045 | /// PointerType. | |||
1046 | Type *getPointerOperandType() const { | |||
1047 | return getPointerOperand()->getType(); | |||
1048 | } | |||
1049 | ||||
1050 | /// Returns the address space of the pointer operand. | |||
1051 | unsigned getPointerAddressSpace() const { | |||
1052 | return getPointerOperandType()->getPointerAddressSpace(); | |||
1053 | } | |||
1054 | ||||
1055 | /// Returns the pointer type returned by the GEP | |||
1056 | /// instruction, which may be a vector of pointers. | |||
1057 | static Type *getGEPReturnType(Type *ElTy, Value *Ptr, | |||
1058 | ArrayRef<Value *> IdxList) { | |||
1059 | Type *PtrTy = PointerType::get(checkGEPType(getIndexedType(ElTy, IdxList)), | |||
1060 | Ptr->getType()->getPointerAddressSpace()); | |||
1061 | // Vector GEP | |||
1062 | if (Ptr->getType()->isVectorTy()) { | |||
1063 | ElementCount EltCount = Ptr->getType()->getVectorElementCount(); | |||
1064 | return VectorType::get(PtrTy, EltCount); | |||
1065 | } | |||
1066 | for (Value *Index : IdxList) | |||
1067 | if (Index->getType()->isVectorTy()) { | |||
1068 | ElementCount EltCount = Index->getType()->getVectorElementCount(); | |||
1069 | return VectorType::get(PtrTy, EltCount); | |||
1070 | } | |||
1071 | // Scalar GEP | |||
1072 | return PtrTy; | |||
1073 | } | |||
1074 | ||||
1075 | unsigned getNumIndices() const { // Note: always non-negative | |||
1076 | return getNumOperands() - 1; | |||
1077 | } | |||
1078 | ||||
1079 | bool hasIndices() const { | |||
1080 | return getNumOperands() > 1; | |||
1081 | } | |||
1082 | ||||
1083 | /// Return true if all of the indices of this GEP are | |||
1084 | /// zeros. If so, the result pointer and the first operand have the same | |||
1085 | /// value, just potentially different types. | |||
1086 | bool hasAllZeroIndices() const; | |||
1087 | ||||
1088 | /// Return true if all of the indices of this GEP are | |||
1089 | /// constant integers. If so, the result pointer and the first operand have | |||
1090 | /// a constant offset between them. | |||
1091 | bool hasAllConstantIndices() const; | |||
1092 | ||||
1093 | /// Set or clear the inbounds flag on this GEP instruction. | |||
1094 | /// See LangRef.html for the meaning of inbounds on a getelementptr. | |||
1095 | void setIsInBounds(bool b = true); | |||
1096 | ||||
1097 | /// Determine whether the GEP has the inbounds flag. | |||
1098 | bool isInBounds() const; | |||
1099 | ||||
1100 | /// Accumulate the constant address offset of this GEP if possible. | |||
1101 | /// | |||
1102 | /// This routine accepts an APInt into which it will accumulate the constant | |||
1103 | /// offset of this GEP if the GEP is in fact constant. If the GEP is not | |||
1104 | /// all-constant, it returns false and the value of the offset APInt is | |||
1105 | /// undefined (it is *not* preserved!). The APInt passed into this routine | |||
1106 | /// must be at least as wide as the IntPtr type for the address space of | |||
1107 | /// the base GEP pointer. | |||
1108 | bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const; | |||
1109 | ||||
1110 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
1111 | static bool classof(const Instruction *I) { | |||
1112 | return (I->getOpcode() == Instruction::GetElementPtr); | |||
1113 | } | |||
1114 | static bool classof(const Value *V) { | |||
1115 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
1116 | } | |||
1117 | }; | |||
1118 | ||||
1119 | template <> | |||
1120 | struct OperandTraits<GetElementPtrInst> : | |||
1121 | public VariadicOperandTraits<GetElementPtrInst, 1> { | |||
1122 | }; | |||
1123 | ||||
1124 | GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr, | |||
1125 | ArrayRef<Value *> IdxList, unsigned Values, | |||
1126 | const Twine &NameStr, | |||
1127 | Instruction *InsertBefore) | |||
1128 | : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr, | |||
1129 | OperandTraits<GetElementPtrInst>::op_end(this) - Values, | |||
1130 | Values, InsertBefore), | |||
1131 | SourceElementType(PointeeType), | |||
1132 | ResultElementType(getIndexedType(PointeeType, IdxList)) { | |||
1133 | assert(ResultElementType ==((ResultElementType == cast<PointerType>(getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1134, __PRETTY_FUNCTION__)) | |||
1134 | cast<PointerType>(getType()->getScalarType())->getElementType())((ResultElementType == cast<PointerType>(getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1134, __PRETTY_FUNCTION__)); | |||
1135 | init(Ptr, IdxList, NameStr); | |||
1136 | } | |||
1137 | ||||
1138 | GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr, | |||
1139 | ArrayRef<Value *> IdxList, unsigned Values, | |||
1140 | const Twine &NameStr, | |||
1141 | BasicBlock *InsertAtEnd) | |||
1142 | : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr, | |||
1143 | OperandTraits<GetElementPtrInst>::op_end(this) - Values, | |||
1144 | Values, InsertAtEnd), | |||
1145 | SourceElementType(PointeeType), | |||
1146 | ResultElementType(getIndexedType(PointeeType, IdxList)) { | |||
1147 | assert(ResultElementType ==((ResultElementType == cast<PointerType>(getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1148, __PRETTY_FUNCTION__)) | |||
1148 | cast<PointerType>(getType()->getScalarType())->getElementType())((ResultElementType == cast<PointerType>(getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1148, __PRETTY_FUNCTION__)); | |||
1149 | init(Ptr, IdxList, NameStr); | |||
1150 | } | |||
1151 | ||||
1152 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)GetElementPtrInst::op_iterator GetElementPtrInst::op_begin() { return OperandTraits<GetElementPtrInst>::op_begin(this ); } GetElementPtrInst::const_op_iterator GetElementPtrInst:: op_begin() const { return OperandTraits<GetElementPtrInst> ::op_begin(const_cast<GetElementPtrInst*>(this)); } GetElementPtrInst ::op_iterator GetElementPtrInst::op_end() { return OperandTraits <GetElementPtrInst>::op_end(this); } GetElementPtrInst:: const_op_iterator GetElementPtrInst::op_end() const { return OperandTraits <GetElementPtrInst>::op_end(const_cast<GetElementPtrInst *>(this)); } Value *GetElementPtrInst::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<GetElementPtrInst >::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<GetElementPtrInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1152, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<GetElementPtrInst>::op_begin(const_cast <GetElementPtrInst*>(this))[i_nocapture].get()); } void GetElementPtrInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((i_nocapture < OperandTraits<GetElementPtrInst> ::operands(this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<GetElementPtrInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1152, __PRETTY_FUNCTION__)); OperandTraits<GetElementPtrInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned GetElementPtrInst::getNumOperands() const { return OperandTraits <GetElementPtrInst>::operands(this); } template <int Idx_nocapture> Use &GetElementPtrInst::Op() { return this ->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture > const Use &GetElementPtrInst::Op() const { return this ->OpFrom<Idx_nocapture>(this); } | |||
1153 | ||||
1154 | //===----------------------------------------------------------------------===// | |||
1155 | // ICmpInst Class | |||
1156 | //===----------------------------------------------------------------------===// | |||
1157 | ||||
1158 | /// This instruction compares its operands according to the predicate given | |||
1159 | /// to the constructor. It only operates on integers or pointers. The operands | |||
1160 | /// must be identical types. | |||
1161 | /// Represent an integer comparison operator. | |||
1162 | class ICmpInst: public CmpInst { | |||
1163 | void AssertOK() { | |||
1164 | assert(isIntPredicate() &&((isIntPredicate() && "Invalid ICmp predicate value") ? static_cast<void> (0) : __assert_fail ("isIntPredicate() && \"Invalid ICmp predicate value\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1165, __PRETTY_FUNCTION__)) | |||
1165 | "Invalid ICmp predicate value")((isIntPredicate() && "Invalid ICmp predicate value") ? static_cast<void> (0) : __assert_fail ("isIntPredicate() && \"Invalid ICmp predicate value\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1165, __PRETTY_FUNCTION__)); | |||
1166 | assert(getOperand(0)->getType() == getOperand(1)->getType() &&((getOperand(0)->getType() == getOperand(1)->getType() && "Both operands to ICmp instruction are not of the same type!" ) ? static_cast<void> (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to ICmp instruction are not of the same type!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1167, __PRETTY_FUNCTION__)) | |||
1167 | "Both operands to ICmp instruction are not of the same type!")((getOperand(0)->getType() == getOperand(1)->getType() && "Both operands to ICmp instruction are not of the same type!" ) ? static_cast<void> (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to ICmp instruction are not of the same type!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1167, __PRETTY_FUNCTION__)); | |||
1168 | // Check that the operands are the right type | |||
1169 | assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||(((getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand (0)->getType()->isPtrOrPtrVectorTy()) && "Invalid operand types for ICmp instruction" ) ? static_cast<void> (0) : __assert_fail ("(getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && \"Invalid operand types for ICmp instruction\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1171, __PRETTY_FUNCTION__)) | |||
1170 | getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&(((getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand (0)->getType()->isPtrOrPtrVectorTy()) && "Invalid operand types for ICmp instruction" ) ? static_cast<void> (0) : __assert_fail ("(getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && \"Invalid operand types for ICmp instruction\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1171, __PRETTY_FUNCTION__)) | |||
1171 | "Invalid operand types for ICmp instruction")(((getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand (0)->getType()->isPtrOrPtrVectorTy()) && "Invalid operand types for ICmp instruction" ) ? static_cast<void> (0) : __assert_fail ("(getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && \"Invalid operand types for ICmp instruction\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1171, __PRETTY_FUNCTION__)); | |||
1172 | } | |||
1173 | ||||
1174 | protected: | |||
1175 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
1176 | friend class Instruction; | |||
1177 | ||||
1178 | /// Clone an identical ICmpInst | |||
1179 | ICmpInst *cloneImpl() const; | |||
1180 | ||||
1181 | public: | |||
1182 | /// Constructor with insert-before-instruction semantics. | |||
1183 | ICmpInst( | |||
1184 | Instruction *InsertBefore, ///< Where to insert | |||
1185 | Predicate pred, ///< The predicate to use for the comparison | |||
1186 | Value *LHS, ///< The left-hand-side of the expression | |||
1187 | Value *RHS, ///< The right-hand-side of the expression | |||
1188 | const Twine &NameStr = "" ///< Name of the instruction | |||
1189 | ) : CmpInst(makeCmpResultType(LHS->getType()), | |||
1190 | Instruction::ICmp, pred, LHS, RHS, NameStr, | |||
1191 | InsertBefore) { | |||
1192 | #ifndef NDEBUG | |||
1193 | AssertOK(); | |||
1194 | #endif | |||
1195 | } | |||
1196 | ||||
1197 | /// Constructor with insert-at-end semantics. | |||
1198 | ICmpInst( | |||
1199 | BasicBlock &InsertAtEnd, ///< Block to insert into. | |||
1200 | Predicate pred, ///< The predicate to use for the comparison | |||
1201 | Value *LHS, ///< The left-hand-side of the expression | |||
1202 | Value *RHS, ///< The right-hand-side of the expression | |||
1203 | const Twine &NameStr = "" ///< Name of the instruction | |||
1204 | ) : CmpInst(makeCmpResultType(LHS->getType()), | |||
1205 | Instruction::ICmp, pred, LHS, RHS, NameStr, | |||
1206 | &InsertAtEnd) { | |||
1207 | #ifndef NDEBUG | |||
1208 | AssertOK(); | |||
1209 | #endif | |||
1210 | } | |||
1211 | ||||
1212 | /// Constructor with no-insertion semantics | |||
1213 | ICmpInst( | |||
1214 | Predicate pred, ///< The predicate to use for the comparison | |||
1215 | Value *LHS, ///< The left-hand-side of the expression | |||
1216 | Value *RHS, ///< The right-hand-side of the expression | |||
1217 | const Twine &NameStr = "" ///< Name of the instruction | |||
1218 | ) : CmpInst(makeCmpResultType(LHS->getType()), | |||
1219 | Instruction::ICmp, pred, LHS, RHS, NameStr) { | |||
1220 | #ifndef NDEBUG | |||
1221 | AssertOK(); | |||
1222 | #endif | |||
1223 | } | |||
1224 | ||||
1225 | /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc. | |||
1226 | /// @returns the predicate that would be the result if the operand were | |||
1227 | /// regarded as signed. | |||
1228 | /// Return the signed version of the predicate | |||
1229 | Predicate getSignedPredicate() const { | |||
1230 | return getSignedPredicate(getPredicate()); | |||
1231 | } | |||
1232 | ||||
1233 | /// This is a static version that you can use without an instruction. | |||
1234 | /// Return the signed version of the predicate. | |||
1235 | static Predicate getSignedPredicate(Predicate pred); | |||
1236 | ||||
1237 | /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc. | |||
1238 | /// @returns the predicate that would be the result if the operand were | |||
1239 | /// regarded as unsigned. | |||
1240 | /// Return the unsigned version of the predicate | |||
1241 | Predicate getUnsignedPredicate() const { | |||
1242 | return getUnsignedPredicate(getPredicate()); | |||
1243 | } | |||
1244 | ||||
1245 | /// This is a static version that you can use without an instruction. | |||
1246 | /// Return the unsigned version of the predicate. | |||
1247 | static Predicate getUnsignedPredicate(Predicate pred); | |||
1248 | ||||
1249 | /// Return true if this predicate is either EQ or NE. This also | |||
1250 | /// tests for commutativity. | |||
1251 | static bool isEquality(Predicate P) { | |||
1252 | return P == ICMP_EQ || P == ICMP_NE; | |||
1253 | } | |||
1254 | ||||
1255 | /// Return true if this predicate is either EQ or NE. This also | |||
1256 | /// tests for commutativity. | |||
1257 | bool isEquality() const { | |||
1258 | return isEquality(getPredicate()); | |||
1259 | } | |||
1260 | ||||
1261 | /// @returns true if the predicate of this ICmpInst is commutative | |||
1262 | /// Determine if this relation is commutative. | |||
1263 | bool isCommutative() const { return isEquality(); } | |||
1264 | ||||
1265 | /// Return true if the predicate is relational (not EQ or NE). | |||
1266 | /// | |||
1267 | bool isRelational() const { | |||
1268 | return !isEquality(); | |||
1269 | } | |||
1270 | ||||
1271 | /// Return true if the predicate is relational (not EQ or NE). | |||
1272 | /// | |||
1273 | static bool isRelational(Predicate P) { | |||
1274 | return !isEquality(P); | |||
1275 | } | |||
1276 | ||||
1277 | /// Exchange the two operands to this instruction in such a way that it does | |||
1278 | /// not modify the semantics of the instruction. The predicate value may be | |||
1279 | /// changed to retain the same result if the predicate is order dependent | |||
1280 | /// (e.g. ult). | |||
1281 | /// Swap operands and adjust predicate. | |||
1282 | void swapOperands() { | |||
1283 | setPredicate(getSwappedPredicate()); | |||
1284 | Op<0>().swap(Op<1>()); | |||
1285 | } | |||
1286 | ||||
1287 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
1288 | static bool classof(const Instruction *I) { | |||
1289 | return I->getOpcode() == Instruction::ICmp; | |||
1290 | } | |||
1291 | static bool classof(const Value *V) { | |||
1292 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
1293 | } | |||
1294 | }; | |||
1295 | ||||
1296 | //===----------------------------------------------------------------------===// | |||
1297 | // FCmpInst Class | |||
1298 | //===----------------------------------------------------------------------===// | |||
1299 | ||||
1300 | /// This instruction compares its operands according to the predicate given | |||
1301 | /// to the constructor. It only operates on floating point values or packed | |||
1302 | /// vectors of floating point values. The operands must be identical types. | |||
1303 | /// Represents a floating point comparison operator. | |||
1304 | class FCmpInst: public CmpInst { | |||
1305 | void AssertOK() { | |||
1306 | assert(isFPPredicate() && "Invalid FCmp predicate value")((isFPPredicate() && "Invalid FCmp predicate value") ? static_cast<void> (0) : __assert_fail ("isFPPredicate() && \"Invalid FCmp predicate value\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1306, __PRETTY_FUNCTION__)); | |||
1307 | assert(getOperand(0)->getType() == getOperand(1)->getType() &&((getOperand(0)->getType() == getOperand(1)->getType() && "Both operands to FCmp instruction are not of the same type!" ) ? static_cast<void> (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to FCmp instruction are not of the same type!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1308, __PRETTY_FUNCTION__)) | |||
1308 | "Both operands to FCmp instruction are not of the same type!")((getOperand(0)->getType() == getOperand(1)->getType() && "Both operands to FCmp instruction are not of the same type!" ) ? static_cast<void> (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to FCmp instruction are not of the same type!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1308, __PRETTY_FUNCTION__)); | |||
1309 | // Check that the operands are the right type | |||
1310 | assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&((getOperand(0)->getType()->isFPOrFPVectorTy() && "Invalid operand types for FCmp instruction") ? static_cast< void> (0) : __assert_fail ("getOperand(0)->getType()->isFPOrFPVectorTy() && \"Invalid operand types for FCmp instruction\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1311, __PRETTY_FUNCTION__)) | |||
1311 | "Invalid operand types for FCmp instruction")((getOperand(0)->getType()->isFPOrFPVectorTy() && "Invalid operand types for FCmp instruction") ? static_cast< void> (0) : __assert_fail ("getOperand(0)->getType()->isFPOrFPVectorTy() && \"Invalid operand types for FCmp instruction\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1311, __PRETTY_FUNCTION__)); | |||
1312 | } | |||
1313 | ||||
1314 | protected: | |||
1315 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
1316 | friend class Instruction; | |||
1317 | ||||
1318 | /// Clone an identical FCmpInst | |||
1319 | FCmpInst *cloneImpl() const; | |||
1320 | ||||
1321 | public: | |||
1322 | /// Constructor with insert-before-instruction semantics. | |||
1323 | FCmpInst( | |||
1324 | Instruction *InsertBefore, ///< Where to insert | |||
1325 | Predicate pred, ///< The predicate to use for the comparison | |||
1326 | Value *LHS, ///< The left-hand-side of the expression | |||
1327 | Value *RHS, ///< The right-hand-side of the expression | |||
1328 | const Twine &NameStr = "" ///< Name of the instruction | |||
1329 | ) : CmpInst(makeCmpResultType(LHS->getType()), | |||
1330 | Instruction::FCmp, pred, LHS, RHS, NameStr, | |||
1331 | InsertBefore) { | |||
1332 | AssertOK(); | |||
1333 | } | |||
1334 | ||||
1335 | /// Constructor with insert-at-end semantics. | |||
1336 | FCmpInst( | |||
1337 | BasicBlock &InsertAtEnd, ///< Block to insert into. | |||
1338 | Predicate pred, ///< The predicate to use for the comparison | |||
1339 | Value *LHS, ///< The left-hand-side of the expression | |||
1340 | Value *RHS, ///< The right-hand-side of the expression | |||
1341 | const Twine &NameStr = "" ///< Name of the instruction | |||
1342 | ) : CmpInst(makeCmpResultType(LHS->getType()), | |||
1343 | Instruction::FCmp, pred, LHS, RHS, NameStr, | |||
1344 | &InsertAtEnd) { | |||
1345 | AssertOK(); | |||
1346 | } | |||
1347 | ||||
1348 | /// Constructor with no-insertion semantics | |||
1349 | FCmpInst( | |||
1350 | Predicate Pred, ///< The predicate to use for the comparison | |||
1351 | Value *LHS, ///< The left-hand-side of the expression | |||
1352 | Value *RHS, ///< The right-hand-side of the expression | |||
1353 | const Twine &NameStr = "", ///< Name of the instruction | |||
1354 | Instruction *FlagsSource = nullptr | |||
1355 | ) : CmpInst(makeCmpResultType(LHS->getType()), Instruction::FCmp, Pred, LHS, | |||
1356 | RHS, NameStr, nullptr, FlagsSource) { | |||
1357 | AssertOK(); | |||
1358 | } | |||
1359 | ||||
1360 | /// @returns true if the predicate of this instruction is EQ or NE. | |||
1361 | /// Determine if this is an equality predicate. | |||
1362 | static bool isEquality(Predicate Pred) { | |||
1363 | return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ || | |||
1364 | Pred == FCMP_UNE; | |||
1365 | } | |||
1366 | ||||
1367 | /// @returns true if the predicate of this instruction is EQ or NE. | |||
1368 | /// Determine if this is an equality predicate. | |||
1369 | bool isEquality() const { return isEquality(getPredicate()); } | |||
1370 | ||||
1371 | /// @returns true if the predicate of this instruction is commutative. | |||
1372 | /// Determine if this is a commutative predicate. | |||
1373 | bool isCommutative() const { | |||
1374 | return isEquality() || | |||
1375 | getPredicate() == FCMP_FALSE || | |||
1376 | getPredicate() == FCMP_TRUE || | |||
1377 | getPredicate() == FCMP_ORD || | |||
1378 | getPredicate() == FCMP_UNO; | |||
1379 | } | |||
1380 | ||||
1381 | /// @returns true if the predicate is relational (not EQ or NE). | |||
1382 | /// Determine if this a relational predicate. | |||
1383 | bool isRelational() const { return !isEquality(); } | |||
1384 | ||||
1385 | /// Exchange the two operands to this instruction in such a way that it does | |||
1386 | /// not modify the semantics of the instruction. The predicate value may be | |||
1387 | /// changed to retain the same result if the predicate is order dependent | |||
1388 | /// (e.g. ult). | |||
1389 | /// Swap operands and adjust predicate. | |||
1390 | void swapOperands() { | |||
1391 | setPredicate(getSwappedPredicate()); | |||
1392 | Op<0>().swap(Op<1>()); | |||
1393 | } | |||
1394 | ||||
1395 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
1396 | static bool classof(const Instruction *I) { | |||
1397 | return I->getOpcode() == Instruction::FCmp; | |||
1398 | } | |||
1399 | static bool classof(const Value *V) { | |||
1400 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
1401 | } | |||
1402 | }; | |||
1403 | ||||
1404 | //===----------------------------------------------------------------------===// | |||
1405 | /// This class represents a function call, abstracting a target | |||
1406 | /// machine's calling convention. This class uses low bit of the SubClassData | |||
1407 | /// field to indicate whether or not this is a tail call. The rest of the bits | |||
1408 | /// hold the calling convention of the call. | |||
1409 | /// | |||
1410 | class CallInst : public CallBase { | |||
1411 | CallInst(const CallInst &CI); | |||
1412 | ||||
1413 | /// Construct a CallInst given a range of arguments. | |||
1414 | /// Construct a CallInst from a range of arguments | |||
1415 | inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, | |||
1416 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, | |||
1417 | Instruction *InsertBefore); | |||
1418 | ||||
1419 | inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, | |||
1420 | const Twine &NameStr, Instruction *InsertBefore) | |||
1421 | : CallInst(Ty, Func, Args, None, NameStr, InsertBefore) {} | |||
1422 | ||||
1423 | /// Construct a CallInst given a range of arguments. | |||
1424 | /// Construct a CallInst from a range of arguments | |||
1425 | inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, | |||
1426 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, | |||
1427 | BasicBlock *InsertAtEnd); | |||
1428 | ||||
1429 | explicit CallInst(FunctionType *Ty, Value *F, const Twine &NameStr, | |||
1430 | Instruction *InsertBefore); | |||
1431 | ||||
1432 | CallInst(FunctionType *ty, Value *F, const Twine &NameStr, | |||
1433 | BasicBlock *InsertAtEnd); | |||
1434 | ||||
1435 | void init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args, | |||
1436 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr); | |||
1437 | void init(FunctionType *FTy, Value *Func, const Twine &NameStr); | |||
1438 | ||||
1439 | /// Compute the number of operands to allocate. | |||
1440 | static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) { | |||
1441 | // We need one operand for the called function, plus the input operand | |||
1442 | // counts provided. | |||
1443 | return 1 + NumArgs + NumBundleInputs; | |||
1444 | } | |||
1445 | ||||
1446 | protected: | |||
1447 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
1448 | friend class Instruction; | |||
1449 | ||||
1450 | CallInst *cloneImpl() const; | |||
1451 | ||||
1452 | public: | |||
1453 | static CallInst *Create(FunctionType *Ty, Value *F, const Twine &NameStr = "", | |||
1454 | Instruction *InsertBefore = nullptr) { | |||
1455 | return new (ComputeNumOperands(0)) CallInst(Ty, F, NameStr, InsertBefore); | |||
1456 | } | |||
1457 | ||||
1458 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, | |||
1459 | const Twine &NameStr, | |||
1460 | Instruction *InsertBefore = nullptr) { | |||
1461 | return new (ComputeNumOperands(Args.size())) | |||
1462 | CallInst(Ty, Func, Args, None, NameStr, InsertBefore); | |||
1463 | } | |||
1464 | ||||
1465 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, | |||
1466 | ArrayRef<OperandBundleDef> Bundles = None, | |||
1467 | const Twine &NameStr = "", | |||
1468 | Instruction *InsertBefore = nullptr) { | |||
1469 | const int NumOperands = | |||
1470 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); | |||
1471 | const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); | |||
1472 | ||||
1473 | return new (NumOperands, DescriptorBytes) | |||
1474 | CallInst(Ty, Func, Args, Bundles, NameStr, InsertBefore); | |||
1475 | } | |||
1476 | ||||
1477 | static CallInst *Create(FunctionType *Ty, Value *F, const Twine &NameStr, | |||
1478 | BasicBlock *InsertAtEnd) { | |||
1479 | return new (ComputeNumOperands(0)) CallInst(Ty, F, NameStr, InsertAtEnd); | |||
1480 | } | |||
1481 | ||||
1482 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, | |||
1483 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
1484 | return new (ComputeNumOperands(Args.size())) | |||
1485 | CallInst(Ty, Func, Args, None, NameStr, InsertAtEnd); | |||
1486 | } | |||
1487 | ||||
1488 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, | |||
1489 | ArrayRef<OperandBundleDef> Bundles, | |||
1490 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
1491 | const int NumOperands = | |||
1492 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); | |||
1493 | const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); | |||
1494 | ||||
1495 | return new (NumOperands, DescriptorBytes) | |||
1496 | CallInst(Ty, Func, Args, Bundles, NameStr, InsertAtEnd); | |||
1497 | } | |||
1498 | ||||
1499 | static CallInst *Create(FunctionCallee Func, const Twine &NameStr = "", | |||
1500 | Instruction *InsertBefore = nullptr) { | |||
1501 | return Create(Func.getFunctionType(), Func.getCallee(), NameStr, | |||
1502 | InsertBefore); | |||
1503 | } | |||
1504 | ||||
1505 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, | |||
1506 | ArrayRef<OperandBundleDef> Bundles = None, | |||
1507 | const Twine &NameStr = "", | |||
1508 | Instruction *InsertBefore = nullptr) { | |||
1509 | return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles, | |||
1510 | NameStr, InsertBefore); | |||
1511 | } | |||
1512 | ||||
1513 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, | |||
1514 | const Twine &NameStr, | |||
1515 | Instruction *InsertBefore = nullptr) { | |||
1516 | return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr, | |||
1517 | InsertBefore); | |||
1518 | } | |||
1519 | ||||
1520 | static CallInst *Create(FunctionCallee Func, const Twine &NameStr, | |||
1521 | BasicBlock *InsertAtEnd) { | |||
1522 | return Create(Func.getFunctionType(), Func.getCallee(), NameStr, | |||
1523 | InsertAtEnd); | |||
1524 | } | |||
1525 | ||||
1526 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, | |||
1527 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
1528 | return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr, | |||
1529 | InsertAtEnd); | |||
1530 | } | |||
1531 | ||||
1532 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, | |||
1533 | ArrayRef<OperandBundleDef> Bundles, | |||
1534 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
1535 | return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles, | |||
1536 | NameStr, InsertAtEnd); | |||
1537 | } | |||
1538 | ||||
1539 | // Deprecated [opaque pointer types] | |||
1540 | static CallInst *Create(Value *Func, const Twine &NameStr = "", | |||
1541 | Instruction *InsertBefore = nullptr) { | |||
1542 | return Create(cast<FunctionType>( | |||
1543 | cast<PointerType>(Func->getType())->getElementType()), | |||
1544 | Func, NameStr, InsertBefore); | |||
1545 | } | |||
1546 | ||||
1547 | // Deprecated [opaque pointer types] | |||
1548 | static CallInst *Create(Value *Func, ArrayRef<Value *> Args, | |||
1549 | const Twine &NameStr, | |||
1550 | Instruction *InsertBefore = nullptr) { | |||
1551 | return Create(cast<FunctionType>( | |||
1552 | cast<PointerType>(Func->getType())->getElementType()), | |||
1553 | Func, Args, NameStr, InsertBefore); | |||
1554 | } | |||
1555 | ||||
1556 | // Deprecated [opaque pointer types] | |||
1557 | static CallInst *Create(Value *Func, ArrayRef<Value *> Args, | |||
1558 | ArrayRef<OperandBundleDef> Bundles = None, | |||
1559 | const Twine &NameStr = "", | |||
1560 | Instruction *InsertBefore = nullptr) { | |||
1561 | return Create(cast<FunctionType>( | |||
1562 | cast<PointerType>(Func->getType())->getElementType()), | |||
1563 | Func, Args, Bundles, NameStr, InsertBefore); | |||
1564 | } | |||
1565 | ||||
1566 | // Deprecated [opaque pointer types] | |||
1567 | static CallInst *Create(Value *Func, const Twine &NameStr, | |||
1568 | BasicBlock *InsertAtEnd) { | |||
1569 | return Create(cast<FunctionType>( | |||
1570 | cast<PointerType>(Func->getType())->getElementType()), | |||
1571 | Func, NameStr, InsertAtEnd); | |||
1572 | } | |||
1573 | ||||
1574 | // Deprecated [opaque pointer types] | |||
1575 | static CallInst *Create(Value *Func, ArrayRef<Value *> Args, | |||
1576 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
1577 | return Create(cast<FunctionType>( | |||
1578 | cast<PointerType>(Func->getType())->getElementType()), | |||
1579 | Func, Args, NameStr, InsertAtEnd); | |||
1580 | } | |||
1581 | ||||
1582 | // Deprecated [opaque pointer types] | |||
1583 | static CallInst *Create(Value *Func, ArrayRef<Value *> Args, | |||
1584 | ArrayRef<OperandBundleDef> Bundles, | |||
1585 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
1586 | return Create(cast<FunctionType>( | |||
1587 | cast<PointerType>(Func->getType())->getElementType()), | |||
1588 | Func, Args, Bundles, NameStr, InsertAtEnd); | |||
1589 | } | |||
1590 | ||||
1591 | /// Create a clone of \p CI with a different set of operand bundles and | |||
1592 | /// insert it before \p InsertPt. | |||
1593 | /// | |||
1594 | /// The returned call instruction is identical \p CI in every way except that | |||
1595 | /// the operand bundles for the new instruction are set to the operand bundles | |||
1596 | /// in \p Bundles. | |||
1597 | static CallInst *Create(CallInst *CI, ArrayRef<OperandBundleDef> Bundles, | |||
1598 | Instruction *InsertPt = nullptr); | |||
1599 | ||||
1600 | /// Generate the IR for a call to malloc: | |||
1601 | /// 1. Compute the malloc call's argument as the specified type's size, | |||
1602 | /// possibly multiplied by the array size if the array size is not | |||
1603 | /// constant 1. | |||
1604 | /// 2. Call malloc with that argument. | |||
1605 | /// 3. Bitcast the result of the malloc call to the specified type. | |||
1606 | static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy, | |||
1607 | Type *AllocTy, Value *AllocSize, | |||
1608 | Value *ArraySize = nullptr, | |||
1609 | Function *MallocF = nullptr, | |||
1610 | const Twine &Name = ""); | |||
1611 | static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy, | |||
1612 | Type *AllocTy, Value *AllocSize, | |||
1613 | Value *ArraySize = nullptr, | |||
1614 | Function *MallocF = nullptr, | |||
1615 | const Twine &Name = ""); | |||
1616 | static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy, | |||
1617 | Type *AllocTy, Value *AllocSize, | |||
1618 | Value *ArraySize = nullptr, | |||
1619 | ArrayRef<OperandBundleDef> Bundles = None, | |||
1620 | Function *MallocF = nullptr, | |||
1621 | const Twine &Name = ""); | |||
1622 | static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy, | |||
1623 | Type *AllocTy, Value *AllocSize, | |||
1624 | Value *ArraySize = nullptr, | |||
1625 | ArrayRef<OperandBundleDef> Bundles = None, | |||
1626 | Function *MallocF = nullptr, | |||
1627 | const Twine &Name = ""); | |||
1628 | /// Generate the IR for a call to the builtin free function. | |||
1629 | static Instruction *CreateFree(Value *Source, Instruction *InsertBefore); | |||
1630 | static Instruction *CreateFree(Value *Source, BasicBlock *InsertAtEnd); | |||
1631 | static Instruction *CreateFree(Value *Source, | |||
1632 | ArrayRef<OperandBundleDef> Bundles, | |||
1633 | Instruction *InsertBefore); | |||
1634 | static Instruction *CreateFree(Value *Source, | |||
1635 | ArrayRef<OperandBundleDef> Bundles, | |||
1636 | BasicBlock *InsertAtEnd); | |||
1637 | ||||
1638 | // Note that 'musttail' implies 'tail'. | |||
1639 | enum TailCallKind { | |||
1640 | TCK_None = 0, | |||
1641 | TCK_Tail = 1, | |||
1642 | TCK_MustTail = 2, | |||
1643 | TCK_NoTail = 3 | |||
1644 | }; | |||
1645 | TailCallKind getTailCallKind() const { | |||
1646 | return TailCallKind(getSubclassDataFromInstruction() & 3); | |||
1647 | } | |||
1648 | ||||
1649 | bool isTailCall() const { | |||
1650 | unsigned Kind = getSubclassDataFromInstruction() & 3; | |||
1651 | return Kind == TCK_Tail || Kind == TCK_MustTail; | |||
1652 | } | |||
1653 | ||||
1654 | bool isMustTailCall() const { | |||
1655 | return (getSubclassDataFromInstruction() & 3) == TCK_MustTail; | |||
1656 | } | |||
1657 | ||||
1658 | bool isNoTailCall() const { | |||
1659 | return (getSubclassDataFromInstruction() & 3) == TCK_NoTail; | |||
1660 | } | |||
1661 | ||||
1662 | void setTailCall(bool isTC = true) { | |||
1663 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) | | |||
1664 | unsigned(isTC ? TCK_Tail : TCK_None)); | |||
1665 | } | |||
1666 | ||||
1667 | void setTailCallKind(TailCallKind TCK) { | |||
1668 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) | | |||
1669 | unsigned(TCK)); | |||
1670 | } | |||
1671 | ||||
1672 | /// Return true if the call can return twice | |||
1673 | bool canReturnTwice() const { return hasFnAttr(Attribute::ReturnsTwice); } | |||
1674 | void setCanReturnTwice() { | |||
1675 | addAttribute(AttributeList::FunctionIndex, Attribute::ReturnsTwice); | |||
1676 | } | |||
1677 | ||||
1678 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
1679 | static bool classof(const Instruction *I) { | |||
1680 | return I->getOpcode() == Instruction::Call; | |||
1681 | } | |||
1682 | static bool classof(const Value *V) { | |||
1683 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
1684 | } | |||
1685 | ||||
1686 | /// Updates profile metadata by scaling it by \p S / \p T. | |||
1687 | void updateProfWeight(uint64_t S, uint64_t T); | |||
1688 | ||||
1689 | private: | |||
1690 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
1691 | // method so that subclasses cannot accidentally use it. | |||
1692 | void setInstructionSubclassData(unsigned short D) { | |||
1693 | Instruction::setInstructionSubclassData(D); | |||
1694 | } | |||
1695 | }; | |||
1696 | ||||
1697 | CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, | |||
1698 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, | |||
1699 | BasicBlock *InsertAtEnd) | |||
1700 | : CallBase(Ty->getReturnType(), Instruction::Call, | |||
1701 | OperandTraits<CallBase>::op_end(this) - | |||
1702 | (Args.size() + CountBundleInputs(Bundles) + 1), | |||
1703 | unsigned(Args.size() + CountBundleInputs(Bundles) + 1), | |||
1704 | InsertAtEnd) { | |||
1705 | init(Ty, Func, Args, Bundles, NameStr); | |||
1706 | } | |||
1707 | ||||
1708 | CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, | |||
1709 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, | |||
1710 | Instruction *InsertBefore) | |||
1711 | : CallBase(Ty->getReturnType(), Instruction::Call, | |||
1712 | OperandTraits<CallBase>::op_end(this) - | |||
1713 | (Args.size() + CountBundleInputs(Bundles) + 1), | |||
1714 | unsigned(Args.size() + CountBundleInputs(Bundles) + 1), | |||
1715 | InsertBefore) { | |||
1716 | init(Ty, Func, Args, Bundles, NameStr); | |||
1717 | } | |||
1718 | ||||
1719 | //===----------------------------------------------------------------------===// | |||
1720 | // SelectInst Class | |||
1721 | //===----------------------------------------------------------------------===// | |||
1722 | ||||
1723 | /// This class represents the LLVM 'select' instruction. | |||
1724 | /// | |||
1725 | class SelectInst : public Instruction { | |||
1726 | SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr, | |||
1727 | Instruction *InsertBefore) | |||
1728 | : Instruction(S1->getType(), Instruction::Select, | |||
| ||||
1729 | &Op<0>(), 3, InsertBefore) { | |||
1730 | init(C, S1, S2); | |||
1731 | setName(NameStr); | |||
1732 | } | |||
1733 | ||||
1734 | SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr, | |||
1735 | BasicBlock *InsertAtEnd) | |||
1736 | : Instruction(S1->getType(), Instruction::Select, | |||
1737 | &Op<0>(), 3, InsertAtEnd) { | |||
1738 | init(C, S1, S2); | |||
1739 | setName(NameStr); | |||
1740 | } | |||
1741 | ||||
1742 | void init(Value *C, Value *S1, Value *S2) { | |||
1743 | assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select")((!areInvalidOperands(C, S1, S2) && "Invalid operands for select" ) ? static_cast<void> (0) : __assert_fail ("!areInvalidOperands(C, S1, S2) && \"Invalid operands for select\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1743, __PRETTY_FUNCTION__)); | |||
1744 | Op<0>() = C; | |||
1745 | Op<1>() = S1; | |||
1746 | Op<2>() = S2; | |||
1747 | } | |||
1748 | ||||
1749 | protected: | |||
1750 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
1751 | friend class Instruction; | |||
1752 | ||||
1753 | SelectInst *cloneImpl() const; | |||
1754 | ||||
1755 | public: | |||
1756 | static SelectInst *Create(Value *C, Value *S1, Value *S2, | |||
1757 | const Twine &NameStr = "", | |||
1758 | Instruction *InsertBefore = nullptr, | |||
1759 | Instruction *MDFrom = nullptr) { | |||
1760 | SelectInst *Sel = new(3) SelectInst(C, S1, S2, NameStr, InsertBefore); | |||
1761 | if (MDFrom) | |||
1762 | Sel->copyMetadata(*MDFrom); | |||
1763 | return Sel; | |||
1764 | } | |||
1765 | ||||
1766 | static SelectInst *Create(Value *C, Value *S1, Value *S2, | |||
1767 | const Twine &NameStr, | |||
1768 | BasicBlock *InsertAtEnd) { | |||
1769 | return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd); | |||
1770 | } | |||
1771 | ||||
1772 | const Value *getCondition() const { return Op<0>(); } | |||
1773 | const Value *getTrueValue() const { return Op<1>(); } | |||
1774 | const Value *getFalseValue() const { return Op<2>(); } | |||
1775 | Value *getCondition() { return Op<0>(); } | |||
1776 | Value *getTrueValue() { return Op<1>(); } | |||
1777 | Value *getFalseValue() { return Op<2>(); } | |||
1778 | ||||
1779 | void setCondition(Value *V) { Op<0>() = V; } | |||
1780 | void setTrueValue(Value *V) { Op<1>() = V; } | |||
1781 | void setFalseValue(Value *V) { Op<2>() = V; } | |||
1782 | ||||
1783 | /// Swap the true and false values of the select instruction. | |||
1784 | /// This doesn't swap prof metadata. | |||
1785 | void swapValues() { Op<1>().swap(Op<2>()); } | |||
1786 | ||||
1787 | /// Return a string if the specified operands are invalid | |||
1788 | /// for a select operation, otherwise return null. | |||
1789 | static const char *areInvalidOperands(Value *Cond, Value *True, Value *False); | |||
1790 | ||||
1791 | /// Transparently provide more efficient getOperand methods. | |||
1792 | 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; | |||
1793 | ||||
1794 | OtherOps getOpcode() const { | |||
1795 | return static_cast<OtherOps>(Instruction::getOpcode()); | |||
1796 | } | |||
1797 | ||||
1798 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
1799 | static bool classof(const Instruction *I) { | |||
1800 | return I->getOpcode() == Instruction::Select; | |||
1801 | } | |||
1802 | static bool classof(const Value *V) { | |||
1803 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
1804 | } | |||
1805 | }; | |||
1806 | ||||
1807 | template <> | |||
1808 | struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> { | |||
1809 | }; | |||
1810 | ||||
1811 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)SelectInst::op_iterator SelectInst::op_begin() { return OperandTraits <SelectInst>::op_begin(this); } SelectInst::const_op_iterator SelectInst::op_begin() const { return OperandTraits<SelectInst >::op_begin(const_cast<SelectInst*>(this)); } SelectInst ::op_iterator SelectInst::op_end() { return OperandTraits< SelectInst>::op_end(this); } SelectInst::const_op_iterator SelectInst::op_end() const { return OperandTraits<SelectInst >::op_end(const_cast<SelectInst*>(this)); } Value *SelectInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<SelectInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<SelectInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1811, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<SelectInst>::op_begin(const_cast<SelectInst *>(this))[i_nocapture].get()); } void SelectInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<SelectInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<SelectInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1811, __PRETTY_FUNCTION__)); OperandTraits<SelectInst> ::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned SelectInst ::getNumOperands() const { return OperandTraits<SelectInst >::operands(this); } template <int Idx_nocapture> Use &SelectInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & SelectInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } | |||
1812 | ||||
1813 | //===----------------------------------------------------------------------===// | |||
1814 | // VAArgInst Class | |||
1815 | //===----------------------------------------------------------------------===// | |||
1816 | ||||
1817 | /// This class represents the va_arg llvm instruction, which returns | |||
1818 | /// an argument of the specified type given a va_list and increments that list | |||
1819 | /// | |||
1820 | class VAArgInst : public UnaryInstruction { | |||
1821 | protected: | |||
1822 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
1823 | friend class Instruction; | |||
1824 | ||||
1825 | VAArgInst *cloneImpl() const; | |||
1826 | ||||
1827 | public: | |||
1828 | VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "", | |||
1829 | Instruction *InsertBefore = nullptr) | |||
1830 | : UnaryInstruction(Ty, VAArg, List, InsertBefore) { | |||
1831 | setName(NameStr); | |||
1832 | } | |||
1833 | ||||
1834 | VAArgInst(Value *List, Type *Ty, const Twine &NameStr, | |||
1835 | BasicBlock *InsertAtEnd) | |||
1836 | : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) { | |||
1837 | setName(NameStr); | |||
1838 | } | |||
1839 | ||||
1840 | Value *getPointerOperand() { return getOperand(0); } | |||
1841 | const Value *getPointerOperand() const { return getOperand(0); } | |||
1842 | static unsigned getPointerOperandIndex() { return 0U; } | |||
1843 | ||||
1844 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
1845 | static bool classof(const Instruction *I) { | |||
1846 | return I->getOpcode() == VAArg; | |||
1847 | } | |||
1848 | static bool classof(const Value *V) { | |||
1849 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
1850 | } | |||
1851 | }; | |||
1852 | ||||
1853 | //===----------------------------------------------------------------------===// | |||
1854 | // ExtractElementInst Class | |||
1855 | //===----------------------------------------------------------------------===// | |||
1856 | ||||
1857 | /// This instruction extracts a single (scalar) | |||
1858 | /// element from a VectorType value | |||
1859 | /// | |||
1860 | class ExtractElementInst : public Instruction { | |||
1861 | ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "", | |||
1862 | Instruction *InsertBefore = nullptr); | |||
1863 | ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr, | |||
1864 | BasicBlock *InsertAtEnd); | |||
1865 | ||||
1866 | protected: | |||
1867 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
1868 | friend class Instruction; | |||
1869 | ||||
1870 | ExtractElementInst *cloneImpl() const; | |||
1871 | ||||
1872 | public: | |||
1873 | static ExtractElementInst *Create(Value *Vec, Value *Idx, | |||
1874 | const Twine &NameStr = "", | |||
1875 | Instruction *InsertBefore = nullptr) { | |||
1876 | return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore); | |||
1877 | } | |||
1878 | ||||
1879 | static ExtractElementInst *Create(Value *Vec, Value *Idx, | |||
1880 | const Twine &NameStr, | |||
1881 | BasicBlock *InsertAtEnd) { | |||
1882 | return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd); | |||
1883 | } | |||
1884 | ||||
1885 | /// Return true if an extractelement instruction can be | |||
1886 | /// formed with the specified operands. | |||
1887 | static bool isValidOperands(const Value *Vec, const Value *Idx); | |||
1888 | ||||
1889 | Value *getVectorOperand() { return Op<0>(); } | |||
1890 | Value *getIndexOperand() { return Op<1>(); } | |||
1891 | const Value *getVectorOperand() const { return Op<0>(); } | |||
1892 | const Value *getIndexOperand() const { return Op<1>(); } | |||
1893 | ||||
1894 | VectorType *getVectorOperandType() const { | |||
1895 | return cast<VectorType>(getVectorOperand()->getType()); | |||
1896 | } | |||
1897 | ||||
1898 | /// Transparently provide more efficient getOperand methods. | |||
1899 | 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; | |||
1900 | ||||
1901 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
1902 | static bool classof(const Instruction *I) { | |||
1903 | return I->getOpcode() == Instruction::ExtractElement; | |||
1904 | } | |||
1905 | static bool classof(const Value *V) { | |||
1906 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
1907 | } | |||
1908 | }; | |||
1909 | ||||
1910 | template <> | |||
1911 | struct OperandTraits<ExtractElementInst> : | |||
1912 | public FixedNumOperandTraits<ExtractElementInst, 2> { | |||
1913 | }; | |||
1914 | ||||
1915 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)ExtractElementInst::op_iterator ExtractElementInst::op_begin( ) { return OperandTraits<ExtractElementInst>::op_begin( this); } ExtractElementInst::const_op_iterator ExtractElementInst ::op_begin() const { return OperandTraits<ExtractElementInst >::op_begin(const_cast<ExtractElementInst*>(this)); } ExtractElementInst::op_iterator ExtractElementInst::op_end() { return OperandTraits<ExtractElementInst>::op_end(this ); } ExtractElementInst::const_op_iterator ExtractElementInst ::op_end() const { return OperandTraits<ExtractElementInst >::op_end(const_cast<ExtractElementInst*>(this)); } Value *ExtractElementInst::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<ExtractElementInst>:: operands(this) && "getOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<ExtractElementInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1915, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<ExtractElementInst>::op_begin(const_cast <ExtractElementInst*>(this))[i_nocapture].get()); } void ExtractElementInst::setOperand(unsigned i_nocapture, Value * Val_nocapture) { ((i_nocapture < OperandTraits<ExtractElementInst >::operands(this) && "setOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<ExtractElementInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1915, __PRETTY_FUNCTION__)); OperandTraits<ExtractElementInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned ExtractElementInst::getNumOperands() const { return OperandTraits <ExtractElementInst>::operands(this); } template <int Idx_nocapture> Use &ExtractElementInst::Op() { return this->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture> const Use &ExtractElementInst::Op() const { return this->OpFrom<Idx_nocapture>(this); } | |||
1916 | ||||
1917 | //===----------------------------------------------------------------------===// | |||
1918 | // InsertElementInst Class | |||
1919 | //===----------------------------------------------------------------------===// | |||
1920 | ||||
1921 | /// This instruction inserts a single (scalar) | |||
1922 | /// element into a VectorType value | |||
1923 | /// | |||
1924 | class InsertElementInst : public Instruction { | |||
1925 | InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, | |||
1926 | const Twine &NameStr = "", | |||
1927 | Instruction *InsertBefore = nullptr); | |||
1928 | InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr, | |||
1929 | BasicBlock *InsertAtEnd); | |||
1930 | ||||
1931 | protected: | |||
1932 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
1933 | friend class Instruction; | |||
1934 | ||||
1935 | InsertElementInst *cloneImpl() const; | |||
1936 | ||||
1937 | public: | |||
1938 | static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx, | |||
1939 | const Twine &NameStr = "", | |||
1940 | Instruction *InsertBefore = nullptr) { | |||
1941 | return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore); | |||
1942 | } | |||
1943 | ||||
1944 | static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx, | |||
1945 | const Twine &NameStr, | |||
1946 | BasicBlock *InsertAtEnd) { | |||
1947 | return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd); | |||
1948 | } | |||
1949 | ||||
1950 | /// Return true if an insertelement instruction can be | |||
1951 | /// formed with the specified operands. | |||
1952 | static bool isValidOperands(const Value *Vec, const Value *NewElt, | |||
1953 | const Value *Idx); | |||
1954 | ||||
1955 | /// Overload to return most specific vector type. | |||
1956 | /// | |||
1957 | VectorType *getType() const { | |||
1958 | return cast<VectorType>(Instruction::getType()); | |||
1959 | } | |||
1960 | ||||
1961 | /// Transparently provide more efficient getOperand methods. | |||
1962 | 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; | |||
1963 | ||||
1964 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
1965 | static bool classof(const Instruction *I) { | |||
1966 | return I->getOpcode() == Instruction::InsertElement; | |||
1967 | } | |||
1968 | static bool classof(const Value *V) { | |||
1969 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
1970 | } | |||
1971 | }; | |||
1972 | ||||
1973 | template <> | |||
1974 | struct OperandTraits<InsertElementInst> : | |||
1975 | public FixedNumOperandTraits<InsertElementInst, 3> { | |||
1976 | }; | |||
1977 | ||||
1978 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)InsertElementInst::op_iterator InsertElementInst::op_begin() { return OperandTraits<InsertElementInst>::op_begin(this ); } InsertElementInst::const_op_iterator InsertElementInst:: op_begin() const { return OperandTraits<InsertElementInst> ::op_begin(const_cast<InsertElementInst*>(this)); } InsertElementInst ::op_iterator InsertElementInst::op_end() { return OperandTraits <InsertElementInst>::op_end(this); } InsertElementInst:: const_op_iterator InsertElementInst::op_end() const { return OperandTraits <InsertElementInst>::op_end(const_cast<InsertElementInst *>(this)); } Value *InsertElementInst::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<InsertElementInst >::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<InsertElementInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1978, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<InsertElementInst>::op_begin(const_cast <InsertElementInst*>(this))[i_nocapture].get()); } void InsertElementInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((i_nocapture < OperandTraits<InsertElementInst> ::operands(this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<InsertElementInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 1978, __PRETTY_FUNCTION__)); OperandTraits<InsertElementInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned InsertElementInst::getNumOperands() const { return OperandTraits <InsertElementInst>::operands(this); } template <int Idx_nocapture> Use &InsertElementInst::Op() { return this ->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture > const Use &InsertElementInst::Op() const { return this ->OpFrom<Idx_nocapture>(this); } | |||
1979 | ||||
1980 | //===----------------------------------------------------------------------===// | |||
1981 | // ShuffleVectorInst Class | |||
1982 | //===----------------------------------------------------------------------===// | |||
1983 | ||||
1984 | /// This instruction constructs a fixed permutation of two | |||
1985 | /// input vectors. | |||
1986 | /// | |||
1987 | class ShuffleVectorInst : public Instruction { | |||
1988 | protected: | |||
1989 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
1990 | friend class Instruction; | |||
1991 | ||||
1992 | ShuffleVectorInst *cloneImpl() const; | |||
1993 | ||||
1994 | public: | |||
1995 | ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, | |||
1996 | const Twine &NameStr = "", | |||
1997 | Instruction *InsertBefor = nullptr); | |||
1998 | ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, | |||
1999 | const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
2000 | ||||
2001 | // allocate space for exactly three operands | |||
2002 | void *operator new(size_t s) { | |||
2003 | return User::operator new(s, 3); | |||
2004 | } | |||
2005 | ||||
2006 | /// Swap the first 2 operands and adjust the mask to preserve the semantics | |||
2007 | /// of the instruction. | |||
2008 | void commute(); | |||
2009 | ||||
2010 | /// Return true if a shufflevector instruction can be | |||
2011 | /// formed with the specified operands. | |||
2012 | static bool isValidOperands(const Value *V1, const Value *V2, | |||
2013 | const Value *Mask); | |||
2014 | ||||
2015 | /// Overload to return most specific vector type. | |||
2016 | /// | |||
2017 | VectorType *getType() const { | |||
2018 | return cast<VectorType>(Instruction::getType()); | |||
2019 | } | |||
2020 | ||||
2021 | /// Transparently provide more efficient getOperand methods. | |||
2022 | 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; | |||
2023 | ||||
2024 | Constant *getMask() const { | |||
2025 | return cast<Constant>(getOperand(2)); | |||
2026 | } | |||
2027 | ||||
2028 | /// Return the shuffle mask value for the specified element of the mask. | |||
2029 | /// Return -1 if the element is undef. | |||
2030 | static int getMaskValue(const Constant *Mask, unsigned Elt); | |||
2031 | ||||
2032 | /// Return the shuffle mask value of this instruction for the given element | |||
2033 | /// index. Return -1 if the element is undef. | |||
2034 | int getMaskValue(unsigned Elt) const { | |||
2035 | return getMaskValue(getMask(), Elt); | |||
2036 | } | |||
2037 | ||||
2038 | /// Convert the input shuffle mask operand to a vector of integers. Undefined | |||
2039 | /// elements of the mask are returned as -1. | |||
2040 | static void getShuffleMask(const Constant *Mask, | |||
2041 | SmallVectorImpl<int> &Result); | |||
2042 | ||||
2043 | /// Return the mask for this instruction as a vector of integers. Undefined | |||
2044 | /// elements of the mask are returned as -1. | |||
2045 | void getShuffleMask(SmallVectorImpl<int> &Result) const { | |||
2046 | return getShuffleMask(getMask(), Result); | |||
2047 | } | |||
2048 | ||||
2049 | SmallVector<int, 16> getShuffleMask() const { | |||
2050 | SmallVector<int, 16> Mask; | |||
2051 | getShuffleMask(Mask); | |||
2052 | return Mask; | |||
2053 | } | |||
2054 | ||||
2055 | /// Return true if this shuffle returns a vector with a different number of | |||
2056 | /// elements than its source vectors. | |||
2057 | /// Examples: shufflevector <4 x n> A, <4 x n> B, <1,2,3> | |||
2058 | /// shufflevector <4 x n> A, <4 x n> B, <1,2,3,4,5> | |||
2059 | bool changesLength() const { | |||
2060 | unsigned NumSourceElts = Op<0>()->getType()->getVectorNumElements(); | |||
2061 | unsigned NumMaskElts = getMask()->getType()->getVectorNumElements(); | |||
2062 | return NumSourceElts != NumMaskElts; | |||
2063 | } | |||
2064 | ||||
2065 | /// Return true if this shuffle returns a vector with a greater number of | |||
2066 | /// elements than its source vectors. | |||
2067 | /// Example: shufflevector <2 x n> A, <2 x n> B, <1,2,3> | |||
2068 | bool increasesLength() const { | |||
2069 | unsigned NumSourceElts = Op<0>()->getType()->getVectorNumElements(); | |||
2070 | unsigned NumMaskElts = getMask()->getType()->getVectorNumElements(); | |||
2071 | return NumSourceElts < NumMaskElts; | |||
2072 | } | |||
2073 | ||||
2074 | /// Return true if this shuffle mask chooses elements from exactly one source | |||
2075 | /// vector. | |||
2076 | /// Example: <7,5,undef,7> | |||
2077 | /// This assumes that vector operands are the same length as the mask. | |||
2078 | static bool isSingleSourceMask(ArrayRef<int> Mask); | |||
2079 | static bool isSingleSourceMask(const Constant *Mask) { | |||
2080 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2080, __PRETTY_FUNCTION__)); | |||
2081 | SmallVector<int, 16> MaskAsInts; | |||
2082 | getShuffleMask(Mask, MaskAsInts); | |||
2083 | return isSingleSourceMask(MaskAsInts); | |||
2084 | } | |||
2085 | ||||
2086 | /// Return true if this shuffle chooses elements from exactly one source | |||
2087 | /// vector without changing the length of that vector. | |||
2088 | /// Example: shufflevector <4 x n> A, <4 x n> B, <3,0,undef,3> | |||
2089 | /// TODO: Optionally allow length-changing shuffles. | |||
2090 | bool isSingleSource() const { | |||
2091 | return !changesLength() && isSingleSourceMask(getMask()); | |||
2092 | } | |||
2093 | ||||
2094 | /// Return true if this shuffle mask chooses elements from exactly one source | |||
2095 | /// vector without lane crossings. A shuffle using this mask is not | |||
2096 | /// necessarily a no-op because it may change the number of elements from its | |||
2097 | /// input vectors or it may provide demanded bits knowledge via undef lanes. | |||
2098 | /// Example: <undef,undef,2,3> | |||
2099 | static bool isIdentityMask(ArrayRef<int> Mask); | |||
2100 | static bool isIdentityMask(const Constant *Mask) { | |||
2101 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2101, __PRETTY_FUNCTION__)); | |||
2102 | SmallVector<int, 16> MaskAsInts; | |||
2103 | getShuffleMask(Mask, MaskAsInts); | |||
2104 | return isIdentityMask(MaskAsInts); | |||
2105 | } | |||
2106 | ||||
2107 | /// Return true if this shuffle chooses elements from exactly one source | |||
2108 | /// vector without lane crossings and does not change the number of elements | |||
2109 | /// from its input vectors. | |||
2110 | /// Example: shufflevector <4 x n> A, <4 x n> B, <4,undef,6,undef> | |||
2111 | bool isIdentity() const { | |||
2112 | return !changesLength() && isIdentityMask(getShuffleMask()); | |||
2113 | } | |||
2114 | ||||
2115 | /// Return true if this shuffle lengthens exactly one source vector with | |||
2116 | /// undefs in the high elements. | |||
2117 | bool isIdentityWithPadding() const; | |||
2118 | ||||
2119 | /// Return true if this shuffle extracts the first N elements of exactly one | |||
2120 | /// source vector. | |||
2121 | bool isIdentityWithExtract() const; | |||
2122 | ||||
2123 | /// Return true if this shuffle concatenates its 2 source vectors. This | |||
2124 | /// returns false if either input is undefined. In that case, the shuffle is | |||
2125 | /// is better classified as an identity with padding operation. | |||
2126 | bool isConcat() const; | |||
2127 | ||||
2128 | /// Return true if this shuffle mask chooses elements from its source vectors | |||
2129 | /// without lane crossings. A shuffle using this mask would be | |||
2130 | /// equivalent to a vector select with a constant condition operand. | |||
2131 | /// Example: <4,1,6,undef> | |||
2132 | /// This returns false if the mask does not choose from both input vectors. | |||
2133 | /// In that case, the shuffle is better classified as an identity shuffle. | |||
2134 | /// This assumes that vector operands are the same length as the mask | |||
2135 | /// (a length-changing shuffle can never be equivalent to a vector select). | |||
2136 | static bool isSelectMask(ArrayRef<int> Mask); | |||
2137 | static bool isSelectMask(const Constant *Mask) { | |||
2138 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2138, __PRETTY_FUNCTION__)); | |||
2139 | SmallVector<int, 16> MaskAsInts; | |||
2140 | getShuffleMask(Mask, MaskAsInts); | |||
2141 | return isSelectMask(MaskAsInts); | |||
2142 | } | |||
2143 | ||||
2144 | /// Return true if this shuffle chooses elements from its source vectors | |||
2145 | /// without lane crossings and all operands have the same number of elements. | |||
2146 | /// In other words, this shuffle is equivalent to a vector select with a | |||
2147 | /// constant condition operand. | |||
2148 | /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,1,6,3> | |||
2149 | /// This returns false if the mask does not choose from both input vectors. | |||
2150 | /// In that case, the shuffle is better classified as an identity shuffle. | |||
2151 | /// TODO: Optionally allow length-changing shuffles. | |||
2152 | bool isSelect() const { | |||
2153 | return !changesLength() && isSelectMask(getMask()); | |||
2154 | } | |||
2155 | ||||
2156 | /// Return true if this shuffle mask swaps the order of elements from exactly | |||
2157 | /// one source vector. | |||
2158 | /// Example: <7,6,undef,4> | |||
2159 | /// This assumes that vector operands are the same length as the mask. | |||
2160 | static bool isReverseMask(ArrayRef<int> Mask); | |||
2161 | static bool isReverseMask(const Constant *Mask) { | |||
2162 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2162, __PRETTY_FUNCTION__)); | |||
2163 | SmallVector<int, 16> MaskAsInts; | |||
2164 | getShuffleMask(Mask, MaskAsInts); | |||
2165 | return isReverseMask(MaskAsInts); | |||
2166 | } | |||
2167 | ||||
2168 | /// Return true if this shuffle swaps the order of elements from exactly | |||
2169 | /// one source vector. | |||
2170 | /// Example: shufflevector <4 x n> A, <4 x n> B, <3,undef,1,undef> | |||
2171 | /// TODO: Optionally allow length-changing shuffles. | |||
2172 | bool isReverse() const { | |||
2173 | return !changesLength() && isReverseMask(getMask()); | |||
2174 | } | |||
2175 | ||||
2176 | /// Return true if this shuffle mask chooses all elements with the same value | |||
2177 | /// as the first element of exactly one source vector. | |||
2178 | /// Example: <4,undef,undef,4> | |||
2179 | /// This assumes that vector operands are the same length as the mask. | |||
2180 | static bool isZeroEltSplatMask(ArrayRef<int> Mask); | |||
2181 | static bool isZeroEltSplatMask(const Constant *Mask) { | |||
2182 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2182, __PRETTY_FUNCTION__)); | |||
2183 | SmallVector<int, 16> MaskAsInts; | |||
2184 | getShuffleMask(Mask, MaskAsInts); | |||
2185 | return isZeroEltSplatMask(MaskAsInts); | |||
2186 | } | |||
2187 | ||||
2188 | /// Return true if all elements of this shuffle are the same value as the | |||
2189 | /// first element of exactly one source vector without changing the length | |||
2190 | /// of that vector. | |||
2191 | /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,0,undef,0> | |||
2192 | /// TODO: Optionally allow length-changing shuffles. | |||
2193 | /// TODO: Optionally allow splats from other elements. | |||
2194 | bool isZeroEltSplat() const { | |||
2195 | return !changesLength() && isZeroEltSplatMask(getMask()); | |||
2196 | } | |||
2197 | ||||
2198 | /// Return true if this shuffle mask is a transpose mask. | |||
2199 | /// Transpose vector masks transpose a 2xn matrix. They read corresponding | |||
2200 | /// even- or odd-numbered vector elements from two n-dimensional source | |||
2201 | /// vectors and write each result into consecutive elements of an | |||
2202 | /// n-dimensional destination vector. Two shuffles are necessary to complete | |||
2203 | /// the transpose, one for the even elements and another for the odd elements. | |||
2204 | /// This description closely follows how the TRN1 and TRN2 AArch64 | |||
2205 | /// instructions operate. | |||
2206 | /// | |||
2207 | /// For example, a simple 2x2 matrix can be transposed with: | |||
2208 | /// | |||
2209 | /// ; Original matrix | |||
2210 | /// m0 = < a, b > | |||
2211 | /// m1 = < c, d > | |||
2212 | /// | |||
2213 | /// ; Transposed matrix | |||
2214 | /// t0 = < a, c > = shufflevector m0, m1, < 0, 2 > | |||
2215 | /// t1 = < b, d > = shufflevector m0, m1, < 1, 3 > | |||
2216 | /// | |||
2217 | /// For matrices having greater than n columns, the resulting nx2 transposed | |||
2218 | /// matrix is stored in two result vectors such that one vector contains | |||
2219 | /// interleaved elements from all the even-numbered rows and the other vector | |||
2220 | /// contains interleaved elements from all the odd-numbered rows. For example, | |||
2221 | /// a 2x4 matrix can be transposed with: | |||
2222 | /// | |||
2223 | /// ; Original matrix | |||
2224 | /// m0 = < a, b, c, d > | |||
2225 | /// m1 = < e, f, g, h > | |||
2226 | /// | |||
2227 | /// ; Transposed matrix | |||
2228 | /// t0 = < a, e, c, g > = shufflevector m0, m1 < 0, 4, 2, 6 > | |||
2229 | /// t1 = < b, f, d, h > = shufflevector m0, m1 < 1, 5, 3, 7 > | |||
2230 | static bool isTransposeMask(ArrayRef<int> Mask); | |||
2231 | static bool isTransposeMask(const Constant *Mask) { | |||
2232 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2232, __PRETTY_FUNCTION__)); | |||
2233 | SmallVector<int, 16> MaskAsInts; | |||
2234 | getShuffleMask(Mask, MaskAsInts); | |||
2235 | return isTransposeMask(MaskAsInts); | |||
2236 | } | |||
2237 | ||||
2238 | /// Return true if this shuffle transposes the elements of its inputs without | |||
2239 | /// changing the length of the vectors. This operation may also be known as a | |||
2240 | /// merge or interleave. See the description for isTransposeMask() for the | |||
2241 | /// exact specification. | |||
2242 | /// Example: shufflevector <4 x n> A, <4 x n> B, <0,4,2,6> | |||
2243 | bool isTranspose() const { | |||
2244 | return !changesLength() && isTransposeMask(getMask()); | |||
2245 | } | |||
2246 | ||||
2247 | /// Return true if this shuffle mask is an extract subvector mask. | |||
2248 | /// A valid extract subvector mask returns a smaller vector from a single | |||
2249 | /// source operand. The base extraction index is returned as well. | |||
2250 | static bool isExtractSubvectorMask(ArrayRef<int> Mask, int NumSrcElts, | |||
2251 | int &Index); | |||
2252 | static bool isExtractSubvectorMask(const Constant *Mask, int NumSrcElts, | |||
2253 | int &Index) { | |||
2254 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2254, __PRETTY_FUNCTION__)); | |||
2255 | SmallVector<int, 16> MaskAsInts; | |||
2256 | getShuffleMask(Mask, MaskAsInts); | |||
2257 | return isExtractSubvectorMask(MaskAsInts, NumSrcElts, Index); | |||
2258 | } | |||
2259 | ||||
2260 | /// Return true if this shuffle mask is an extract subvector mask. | |||
2261 | bool isExtractSubvectorMask(int &Index) const { | |||
2262 | int NumSrcElts = Op<0>()->getType()->getVectorNumElements(); | |||
2263 | return isExtractSubvectorMask(getMask(), NumSrcElts, Index); | |||
2264 | } | |||
2265 | ||||
2266 | /// Change values in a shuffle permute mask assuming the two vector operands | |||
2267 | /// of length InVecNumElts have swapped position. | |||
2268 | static void commuteShuffleMask(MutableArrayRef<int> Mask, | |||
2269 | unsigned InVecNumElts) { | |||
2270 | for (int &Idx : Mask) { | |||
2271 | if (Idx == -1) | |||
2272 | continue; | |||
2273 | Idx = Idx < (int)InVecNumElts ? Idx + InVecNumElts : Idx - InVecNumElts; | |||
2274 | assert(Idx >= 0 && Idx < (int)InVecNumElts * 2 &&((Idx >= 0 && Idx < (int)InVecNumElts * 2 && "shufflevector mask index out of range") ? static_cast<void > (0) : __assert_fail ("Idx >= 0 && Idx < (int)InVecNumElts * 2 && \"shufflevector mask index out of range\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2275, __PRETTY_FUNCTION__)) | |||
2275 | "shufflevector mask index out of range")((Idx >= 0 && Idx < (int)InVecNumElts * 2 && "shufflevector mask index out of range") ? static_cast<void > (0) : __assert_fail ("Idx >= 0 && Idx < (int)InVecNumElts * 2 && \"shufflevector mask index out of range\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2275, __PRETTY_FUNCTION__)); | |||
2276 | } | |||
2277 | } | |||
2278 | ||||
2279 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
2280 | static bool classof(const Instruction *I) { | |||
2281 | return I->getOpcode() == Instruction::ShuffleVector; | |||
2282 | } | |||
2283 | static bool classof(const Value *V) { | |||
2284 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
2285 | } | |||
2286 | }; | |||
2287 | ||||
2288 | template <> | |||
2289 | struct OperandTraits<ShuffleVectorInst> : | |||
2290 | public FixedNumOperandTraits<ShuffleVectorInst, 3> { | |||
2291 | }; | |||
2292 | ||||
2293 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)ShuffleVectorInst::op_iterator ShuffleVectorInst::op_begin() { return OperandTraits<ShuffleVectorInst>::op_begin(this ); } ShuffleVectorInst::const_op_iterator ShuffleVectorInst:: op_begin() const { return OperandTraits<ShuffleVectorInst> ::op_begin(const_cast<ShuffleVectorInst*>(this)); } ShuffleVectorInst ::op_iterator ShuffleVectorInst::op_end() { return OperandTraits <ShuffleVectorInst>::op_end(this); } ShuffleVectorInst:: const_op_iterator ShuffleVectorInst::op_end() const { return OperandTraits <ShuffleVectorInst>::op_end(const_cast<ShuffleVectorInst *>(this)); } Value *ShuffleVectorInst::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<ShuffleVectorInst >::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<ShuffleVectorInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2293, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<ShuffleVectorInst>::op_begin(const_cast <ShuffleVectorInst*>(this))[i_nocapture].get()); } void ShuffleVectorInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((i_nocapture < OperandTraits<ShuffleVectorInst> ::operands(this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<ShuffleVectorInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2293, __PRETTY_FUNCTION__)); OperandTraits<ShuffleVectorInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned ShuffleVectorInst::getNumOperands() const { return OperandTraits <ShuffleVectorInst>::operands(this); } template <int Idx_nocapture> Use &ShuffleVectorInst::Op() { return this ->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture > const Use &ShuffleVectorInst::Op() const { return this ->OpFrom<Idx_nocapture>(this); } | |||
2294 | ||||
2295 | //===----------------------------------------------------------------------===// | |||
2296 | // ExtractValueInst Class | |||
2297 | //===----------------------------------------------------------------------===// | |||
2298 | ||||
2299 | /// This instruction extracts a struct member or array | |||
2300 | /// element value from an aggregate value. | |||
2301 | /// | |||
2302 | class ExtractValueInst : public UnaryInstruction { | |||
2303 | SmallVector<unsigned, 4> Indices; | |||
2304 | ||||
2305 | ExtractValueInst(const ExtractValueInst &EVI); | |||
2306 | ||||
2307 | /// Constructors - Create a extractvalue instruction with a base aggregate | |||
2308 | /// value and a list of indices. The first ctor can optionally insert before | |||
2309 | /// an existing instruction, the second appends the new instruction to the | |||
2310 | /// specified BasicBlock. | |||
2311 | inline ExtractValueInst(Value *Agg, | |||
2312 | ArrayRef<unsigned> Idxs, | |||
2313 | const Twine &NameStr, | |||
2314 | Instruction *InsertBefore); | |||
2315 | inline ExtractValueInst(Value *Agg, | |||
2316 | ArrayRef<unsigned> Idxs, | |||
2317 | const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
2318 | ||||
2319 | void init(ArrayRef<unsigned> Idxs, const Twine &NameStr); | |||
2320 | ||||
2321 | protected: | |||
2322 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
2323 | friend class Instruction; | |||
2324 | ||||
2325 | ExtractValueInst *cloneImpl() const; | |||
2326 | ||||
2327 | public: | |||
2328 | static ExtractValueInst *Create(Value *Agg, | |||
2329 | ArrayRef<unsigned> Idxs, | |||
2330 | const Twine &NameStr = "", | |||
2331 | Instruction *InsertBefore = nullptr) { | |||
2332 | return new | |||
2333 | ExtractValueInst(Agg, Idxs, NameStr, InsertBefore); | |||
2334 | } | |||
2335 | ||||
2336 | static ExtractValueInst *Create(Value *Agg, | |||
2337 | ArrayRef<unsigned> Idxs, | |||
2338 | const Twine &NameStr, | |||
2339 | BasicBlock *InsertAtEnd) { | |||
2340 | return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd); | |||
2341 | } | |||
2342 | ||||
2343 | /// Returns the type of the element that would be extracted | |||
2344 | /// with an extractvalue instruction with the specified parameters. | |||
2345 | /// | |||
2346 | /// Null is returned if the indices are invalid for the specified type. | |||
2347 | static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs); | |||
2348 | ||||
2349 | using idx_iterator = const unsigned*; | |||
2350 | ||||
2351 | inline idx_iterator idx_begin() const { return Indices.begin(); } | |||
2352 | inline idx_iterator idx_end() const { return Indices.end(); } | |||
2353 | inline iterator_range<idx_iterator> indices() const { | |||
2354 | return make_range(idx_begin(), idx_end()); | |||
2355 | } | |||
2356 | ||||
2357 | Value *getAggregateOperand() { | |||
2358 | return getOperand(0); | |||
2359 | } | |||
2360 | const Value *getAggregateOperand() const { | |||
2361 | return getOperand(0); | |||
2362 | } | |||
2363 | static unsigned getAggregateOperandIndex() { | |||
2364 | return 0U; // get index for modifying correct operand | |||
2365 | } | |||
2366 | ||||
2367 | ArrayRef<unsigned> getIndices() const { | |||
2368 | return Indices; | |||
2369 | } | |||
2370 | ||||
2371 | unsigned getNumIndices() const { | |||
2372 | return (unsigned)Indices.size(); | |||
2373 | } | |||
2374 | ||||
2375 | bool hasIndices() const { | |||
2376 | return true; | |||
2377 | } | |||
2378 | ||||
2379 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
2380 | static bool classof(const Instruction *I) { | |||
2381 | return I->getOpcode() == Instruction::ExtractValue; | |||
2382 | } | |||
2383 | static bool classof(const Value *V) { | |||
2384 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
2385 | } | |||
2386 | }; | |||
2387 | ||||
2388 | ExtractValueInst::ExtractValueInst(Value *Agg, | |||
2389 | ArrayRef<unsigned> Idxs, | |||
2390 | const Twine &NameStr, | |||
2391 | Instruction *InsertBefore) | |||
2392 | : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)), | |||
2393 | ExtractValue, Agg, InsertBefore) { | |||
2394 | init(Idxs, NameStr); | |||
2395 | } | |||
2396 | ||||
2397 | ExtractValueInst::ExtractValueInst(Value *Agg, | |||
2398 | ArrayRef<unsigned> Idxs, | |||
2399 | const Twine &NameStr, | |||
2400 | BasicBlock *InsertAtEnd) | |||
2401 | : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)), | |||
2402 | ExtractValue, Agg, InsertAtEnd) { | |||
2403 | init(Idxs, NameStr); | |||
2404 | } | |||
2405 | ||||
2406 | //===----------------------------------------------------------------------===// | |||
2407 | // InsertValueInst Class | |||
2408 | //===----------------------------------------------------------------------===// | |||
2409 | ||||
2410 | /// This instruction inserts a struct field of array element | |||
2411 | /// value into an aggregate value. | |||
2412 | /// | |||
2413 | class InsertValueInst : public Instruction { | |||
2414 | SmallVector<unsigned, 4> Indices; | |||
2415 | ||||
2416 | InsertValueInst(const InsertValueInst &IVI); | |||
2417 | ||||
2418 | /// Constructors - Create a insertvalue instruction with a base aggregate | |||
2419 | /// value, a value to insert, and a list of indices. The first ctor can | |||
2420 | /// optionally insert before an existing instruction, the second appends | |||
2421 | /// the new instruction to the specified BasicBlock. | |||
2422 | inline InsertValueInst(Value *Agg, Value *Val, | |||
2423 | ArrayRef<unsigned> Idxs, | |||
2424 | const Twine &NameStr, | |||
2425 | Instruction *InsertBefore); | |||
2426 | inline InsertValueInst(Value *Agg, Value *Val, | |||
2427 | ArrayRef<unsigned> Idxs, | |||
2428 | const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
2429 | ||||
2430 | /// Constructors - These two constructors are convenience methods because one | |||
2431 | /// and two index insertvalue instructions are so common. | |||
2432 | InsertValueInst(Value *Agg, Value *Val, unsigned Idx, | |||
2433 | const Twine &NameStr = "", | |||
2434 | Instruction *InsertBefore = nullptr); | |||
2435 | InsertValueInst(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr, | |||
2436 | BasicBlock *InsertAtEnd); | |||
2437 | ||||
2438 | void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, | |||
2439 | const Twine &NameStr); | |||
2440 | ||||
2441 | protected: | |||
2442 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
2443 | friend class Instruction; | |||
2444 | ||||
2445 | InsertValueInst *cloneImpl() const; | |||
2446 | ||||
2447 | public: | |||
2448 | // allocate space for exactly two operands | |||
2449 | void *operator new(size_t s) { | |||
2450 | return User::operator new(s, 2); | |||
2451 | } | |||
2452 | ||||
2453 | static InsertValueInst *Create(Value *Agg, Value *Val, | |||
2454 | ArrayRef<unsigned> Idxs, | |||
2455 | const Twine &NameStr = "", | |||
2456 | Instruction *InsertBefore = nullptr) { | |||
2457 | return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore); | |||
2458 | } | |||
2459 | ||||
2460 | static InsertValueInst *Create(Value *Agg, Value *Val, | |||
2461 | ArrayRef<unsigned> Idxs, | |||
2462 | const Twine &NameStr, | |||
2463 | BasicBlock *InsertAtEnd) { | |||
2464 | return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd); | |||
2465 | } | |||
2466 | ||||
2467 | /// Transparently provide more efficient getOperand methods. | |||
2468 | 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; | |||
2469 | ||||
2470 | using idx_iterator = const unsigned*; | |||
2471 | ||||
2472 | inline idx_iterator idx_begin() const { return Indices.begin(); } | |||
2473 | inline idx_iterator idx_end() const { return Indices.end(); } | |||
2474 | inline iterator_range<idx_iterator> indices() const { | |||
2475 | return make_range(idx_begin(), idx_end()); | |||
2476 | } | |||
2477 | ||||
2478 | Value *getAggregateOperand() { | |||
2479 | return getOperand(0); | |||
2480 | } | |||
2481 | const Value *getAggregateOperand() const { | |||
2482 | return getOperand(0); | |||
2483 | } | |||
2484 | static unsigned getAggregateOperandIndex() { | |||
2485 | return 0U; // get index for modifying correct operand | |||
2486 | } | |||
2487 | ||||
2488 | Value *getInsertedValueOperand() { | |||
2489 | return getOperand(1); | |||
2490 | } | |||
2491 | const Value *getInsertedValueOperand() const { | |||
2492 | return getOperand(1); | |||
2493 | } | |||
2494 | static unsigned getInsertedValueOperandIndex() { | |||
2495 | return 1U; // get index for modifying correct operand | |||
2496 | } | |||
2497 | ||||
2498 | ArrayRef<unsigned> getIndices() const { | |||
2499 | return Indices; | |||
2500 | } | |||
2501 | ||||
2502 | unsigned getNumIndices() const { | |||
2503 | return (unsigned)Indices.size(); | |||
2504 | } | |||
2505 | ||||
2506 | bool hasIndices() const { | |||
2507 | return true; | |||
2508 | } | |||
2509 | ||||
2510 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
2511 | static bool classof(const Instruction *I) { | |||
2512 | return I->getOpcode() == Instruction::InsertValue; | |||
2513 | } | |||
2514 | static bool classof(const Value *V) { | |||
2515 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
2516 | } | |||
2517 | }; | |||
2518 | ||||
2519 | template <> | |||
2520 | struct OperandTraits<InsertValueInst> : | |||
2521 | public FixedNumOperandTraits<InsertValueInst, 2> { | |||
2522 | }; | |||
2523 | ||||
2524 | InsertValueInst::InsertValueInst(Value *Agg, | |||
2525 | Value *Val, | |||
2526 | ArrayRef<unsigned> Idxs, | |||
2527 | const Twine &NameStr, | |||
2528 | Instruction *InsertBefore) | |||
2529 | : Instruction(Agg->getType(), InsertValue, | |||
2530 | OperandTraits<InsertValueInst>::op_begin(this), | |||
2531 | 2, InsertBefore) { | |||
2532 | init(Agg, Val, Idxs, NameStr); | |||
2533 | } | |||
2534 | ||||
2535 | InsertValueInst::InsertValueInst(Value *Agg, | |||
2536 | Value *Val, | |||
2537 | ArrayRef<unsigned> Idxs, | |||
2538 | const Twine &NameStr, | |||
2539 | BasicBlock *InsertAtEnd) | |||
2540 | : Instruction(Agg->getType(), InsertValue, | |||
2541 | OperandTraits<InsertValueInst>::op_begin(this), | |||
2542 | 2, InsertAtEnd) { | |||
2543 | init(Agg, Val, Idxs, NameStr); | |||
2544 | } | |||
2545 | ||||
2546 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)InsertValueInst::op_iterator InsertValueInst::op_begin() { return OperandTraits<InsertValueInst>::op_begin(this); } InsertValueInst ::const_op_iterator InsertValueInst::op_begin() const { return OperandTraits<InsertValueInst>::op_begin(const_cast< InsertValueInst*>(this)); } InsertValueInst::op_iterator InsertValueInst ::op_end() { return OperandTraits<InsertValueInst>::op_end (this); } InsertValueInst::const_op_iterator InsertValueInst:: op_end() const { return OperandTraits<InsertValueInst>:: op_end(const_cast<InsertValueInst*>(this)); } Value *InsertValueInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<InsertValueInst>::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<InsertValueInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2546, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<InsertValueInst>::op_begin(const_cast< InsertValueInst*>(this))[i_nocapture].get()); } void InsertValueInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<InsertValueInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<InsertValueInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2546, __PRETTY_FUNCTION__)); OperandTraits<InsertValueInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned InsertValueInst::getNumOperands() const { return OperandTraits <InsertValueInst>::operands(this); } template <int Idx_nocapture > Use &InsertValueInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &InsertValueInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } | |||
2547 | ||||
2548 | //===----------------------------------------------------------------------===// | |||
2549 | // PHINode Class | |||
2550 | //===----------------------------------------------------------------------===// | |||
2551 | ||||
2552 | // PHINode - The PHINode class is used to represent the magical mystical PHI | |||
2553 | // node, that can not exist in nature, but can be synthesized in a computer | |||
2554 | // scientist's overactive imagination. | |||
2555 | // | |||
2556 | class PHINode : public Instruction { | |||
2557 | /// The number of operands actually allocated. NumOperands is | |||
2558 | /// the number actually in use. | |||
2559 | unsigned ReservedSpace; | |||
2560 | ||||
2561 | PHINode(const PHINode &PN); | |||
2562 | ||||
2563 | explicit PHINode(Type *Ty, unsigned NumReservedValues, | |||
2564 | const Twine &NameStr = "", | |||
2565 | Instruction *InsertBefore = nullptr) | |||
2566 | : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore), | |||
2567 | ReservedSpace(NumReservedValues) { | |||
2568 | setName(NameStr); | |||
2569 | allocHungoffUses(ReservedSpace); | |||
2570 | } | |||
2571 | ||||
2572 | PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr, | |||
2573 | BasicBlock *InsertAtEnd) | |||
2574 | : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd), | |||
2575 | ReservedSpace(NumReservedValues) { | |||
2576 | setName(NameStr); | |||
2577 | allocHungoffUses(ReservedSpace); | |||
2578 | } | |||
2579 | ||||
2580 | protected: | |||
2581 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
2582 | friend class Instruction; | |||
2583 | ||||
2584 | PHINode *cloneImpl() const; | |||
2585 | ||||
2586 | // allocHungoffUses - this is more complicated than the generic | |||
2587 | // User::allocHungoffUses, because we have to allocate Uses for the incoming | |||
2588 | // values and pointers to the incoming blocks, all in one allocation. | |||
2589 | void allocHungoffUses(unsigned N) { | |||
2590 | User::allocHungoffUses(N, /* IsPhi */ true); | |||
2591 | } | |||
2592 | ||||
2593 | public: | |||
2594 | /// Constructors - NumReservedValues is a hint for the number of incoming | |||
2595 | /// edges that this phi node will have (use 0 if you really have no idea). | |||
2596 | static PHINode *Create(Type *Ty, unsigned NumReservedValues, | |||
2597 | const Twine &NameStr = "", | |||
2598 | Instruction *InsertBefore = nullptr) { | |||
2599 | return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore); | |||
2600 | } | |||
2601 | ||||
2602 | static PHINode *Create(Type *Ty, unsigned NumReservedValues, | |||
2603 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
2604 | return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd); | |||
2605 | } | |||
2606 | ||||
2607 | /// Provide fast operand accessors | |||
2608 | 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; | |||
2609 | ||||
2610 | // Block iterator interface. This provides access to the list of incoming | |||
2611 | // basic blocks, which parallels the list of incoming values. | |||
2612 | ||||
2613 | using block_iterator = BasicBlock **; | |||
2614 | using const_block_iterator = BasicBlock * const *; | |||
2615 | ||||
2616 | block_iterator block_begin() { | |||
2617 | Use::UserRef *ref = | |||
2618 | reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace); | |||
2619 | return reinterpret_cast<block_iterator>(ref + 1); | |||
2620 | } | |||
2621 | ||||
2622 | const_block_iterator block_begin() const { | |||
2623 | const Use::UserRef *ref = | |||
2624 | reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace); | |||
2625 | return reinterpret_cast<const_block_iterator>(ref + 1); | |||
2626 | } | |||
2627 | ||||
2628 | block_iterator block_end() { | |||
2629 | return block_begin() + getNumOperands(); | |||
2630 | } | |||
2631 | ||||
2632 | const_block_iterator block_end() const { | |||
2633 | return block_begin() + getNumOperands(); | |||
2634 | } | |||
2635 | ||||
2636 | iterator_range<block_iterator> blocks() { | |||
2637 | return make_range(block_begin(), block_end()); | |||
2638 | } | |||
2639 | ||||
2640 | iterator_range<const_block_iterator> blocks() const { | |||
2641 | return make_range(block_begin(), block_end()); | |||
2642 | } | |||
2643 | ||||
2644 | op_range incoming_values() { return operands(); } | |||
2645 | ||||
2646 | const_op_range incoming_values() const { return operands(); } | |||
2647 | ||||
2648 | /// Return the number of incoming edges | |||
2649 | /// | |||
2650 | unsigned getNumIncomingValues() const { return getNumOperands(); } | |||
2651 | ||||
2652 | /// Return incoming value number x | |||
2653 | /// | |||
2654 | Value *getIncomingValue(unsigned i) const { | |||
2655 | return getOperand(i); | |||
2656 | } | |||
2657 | void setIncomingValue(unsigned i, Value *V) { | |||
2658 | assert(V && "PHI node got a null value!")((V && "PHI node got a null value!") ? static_cast< void> (0) : __assert_fail ("V && \"PHI node got a null value!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2658, __PRETTY_FUNCTION__)); | |||
2659 | assert(getType() == V->getType() &&((getType() == V->getType() && "All operands to PHI node must be the same type as the PHI node!" ) ? static_cast<void> (0) : __assert_fail ("getType() == V->getType() && \"All operands to PHI node must be the same type as the PHI node!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2660, __PRETTY_FUNCTION__)) | |||
2660 | "All operands to PHI node must be the same type as the PHI node!")((getType() == V->getType() && "All operands to PHI node must be the same type as the PHI node!" ) ? static_cast<void> (0) : __assert_fail ("getType() == V->getType() && \"All operands to PHI node must be the same type as the PHI node!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2660, __PRETTY_FUNCTION__)); | |||
2661 | setOperand(i, V); | |||
2662 | } | |||
2663 | ||||
2664 | static unsigned getOperandNumForIncomingValue(unsigned i) { | |||
2665 | return i; | |||
2666 | } | |||
2667 | ||||
2668 | static unsigned getIncomingValueNumForOperand(unsigned i) { | |||
2669 | return i; | |||
2670 | } | |||
2671 | ||||
2672 | /// Return incoming basic block number @p i. | |||
2673 | /// | |||
2674 | BasicBlock *getIncomingBlock(unsigned i) const { | |||
2675 | return block_begin()[i]; | |||
2676 | } | |||
2677 | ||||
2678 | /// Return incoming basic block corresponding | |||
2679 | /// to an operand of the PHI. | |||
2680 | /// | |||
2681 | BasicBlock *getIncomingBlock(const Use &U) const { | |||
2682 | assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?")((this == U.getUser() && "Iterator doesn't point to PHI's Uses?" ) ? static_cast<void> (0) : __assert_fail ("this == U.getUser() && \"Iterator doesn't point to PHI's Uses?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2682, __PRETTY_FUNCTION__)); | |||
2683 | return getIncomingBlock(unsigned(&U - op_begin())); | |||
2684 | } | |||
2685 | ||||
2686 | /// Return incoming basic block corresponding | |||
2687 | /// to value use iterator. | |||
2688 | /// | |||
2689 | BasicBlock *getIncomingBlock(Value::const_user_iterator I) const { | |||
2690 | return getIncomingBlock(I.getUse()); | |||
2691 | } | |||
2692 | ||||
2693 | void setIncomingBlock(unsigned i, BasicBlock *BB) { | |||
2694 | assert(BB && "PHI node got a null basic block!")((BB && "PHI node got a null basic block!") ? static_cast <void> (0) : __assert_fail ("BB && \"PHI node got a null basic block!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2694, __PRETTY_FUNCTION__)); | |||
2695 | block_begin()[i] = BB; | |||
2696 | } | |||
2697 | ||||
2698 | /// Replace every incoming basic block \p Old to basic block \p New. | |||
2699 | void replaceIncomingBlockWith(const BasicBlock *Old, BasicBlock *New) { | |||
2700 | assert(New && Old && "PHI node got a null basic block!")((New && Old && "PHI node got a null basic block!" ) ? static_cast<void> (0) : __assert_fail ("New && Old && \"PHI node got a null basic block!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2700, __PRETTY_FUNCTION__)); | |||
2701 | for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op) | |||
2702 | if (getIncomingBlock(Op) == Old) | |||
2703 | setIncomingBlock(Op, New); | |||
2704 | } | |||
2705 | ||||
2706 | /// Add an incoming value to the end of the PHI list | |||
2707 | /// | |||
2708 | void addIncoming(Value *V, BasicBlock *BB) { | |||
2709 | if (getNumOperands() == ReservedSpace) | |||
2710 | growOperands(); // Get more space! | |||
2711 | // Initialize some new operands. | |||
2712 | setNumHungOffUseOperands(getNumOperands() + 1); | |||
2713 | setIncomingValue(getNumOperands() - 1, V); | |||
2714 | setIncomingBlock(getNumOperands() - 1, BB); | |||
2715 | } | |||
2716 | ||||
2717 | /// Remove an incoming value. This is useful if a | |||
2718 | /// predecessor basic block is deleted. The value removed is returned. | |||
2719 | /// | |||
2720 | /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty | |||
2721 | /// is true), the PHI node is destroyed and any uses of it are replaced with | |||
2722 | /// dummy values. The only time there should be zero incoming values to a PHI | |||
2723 | /// node is when the block is dead, so this strategy is sound. | |||
2724 | /// | |||
2725 | Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true); | |||
2726 | ||||
2727 | Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) { | |||
2728 | int Idx = getBasicBlockIndex(BB); | |||
2729 | assert(Idx >= 0 && "Invalid basic block argument to remove!")((Idx >= 0 && "Invalid basic block argument to remove!" ) ? static_cast<void> (0) : __assert_fail ("Idx >= 0 && \"Invalid basic block argument to remove!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2729, __PRETTY_FUNCTION__)); | |||
2730 | return removeIncomingValue(Idx, DeletePHIIfEmpty); | |||
2731 | } | |||
2732 | ||||
2733 | /// Return the first index of the specified basic | |||
2734 | /// block in the value list for this PHI. Returns -1 if no instance. | |||
2735 | /// | |||
2736 | int getBasicBlockIndex(const BasicBlock *BB) const { | |||
2737 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) | |||
2738 | if (block_begin()[i] == BB) | |||
2739 | return i; | |||
2740 | return -1; | |||
2741 | } | |||
2742 | ||||
2743 | Value *getIncomingValueForBlock(const BasicBlock *BB) const { | |||
2744 | int Idx = getBasicBlockIndex(BB); | |||
2745 | assert(Idx >= 0 && "Invalid basic block argument!")((Idx >= 0 && "Invalid basic block argument!") ? static_cast <void> (0) : __assert_fail ("Idx >= 0 && \"Invalid basic block argument!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2745, __PRETTY_FUNCTION__)); | |||
2746 | return getIncomingValue(Idx); | |||
2747 | } | |||
2748 | ||||
2749 | /// Set every incoming value(s) for block \p BB to \p V. | |||
2750 | void setIncomingValueForBlock(const BasicBlock *BB, Value *V) { | |||
2751 | assert(BB && "PHI node got a null basic block!")((BB && "PHI node got a null basic block!") ? static_cast <void> (0) : __assert_fail ("BB && \"PHI node got a null basic block!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2751, __PRETTY_FUNCTION__)); | |||
2752 | bool Found = false; | |||
2753 | for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op) | |||
2754 | if (getIncomingBlock(Op) == BB) { | |||
2755 | Found = true; | |||
2756 | setIncomingValue(Op, V); | |||
2757 | } | |||
2758 | (void)Found; | |||
2759 | assert(Found && "Invalid basic block argument to set!")((Found && "Invalid basic block argument to set!") ? static_cast <void> (0) : __assert_fail ("Found && \"Invalid basic block argument to set!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2759, __PRETTY_FUNCTION__)); | |||
2760 | } | |||
2761 | ||||
2762 | /// If the specified PHI node always merges together the | |||
2763 | /// same value, return the value, otherwise return null. | |||
2764 | Value *hasConstantValue() const; | |||
2765 | ||||
2766 | /// Whether the specified PHI node always merges | |||
2767 | /// together the same value, assuming undefs are equal to a unique | |||
2768 | /// non-undef value. | |||
2769 | bool hasConstantOrUndefValue() const; | |||
2770 | ||||
2771 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
2772 | static bool classof(const Instruction *I) { | |||
2773 | return I->getOpcode() == Instruction::PHI; | |||
2774 | } | |||
2775 | static bool classof(const Value *V) { | |||
2776 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
2777 | } | |||
2778 | ||||
2779 | private: | |||
2780 | void growOperands(); | |||
2781 | }; | |||
2782 | ||||
2783 | template <> | |||
2784 | struct OperandTraits<PHINode> : public HungoffOperandTraits<2> { | |||
2785 | }; | |||
2786 | ||||
2787 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)PHINode::op_iterator PHINode::op_begin() { return OperandTraits <PHINode>::op_begin(this); } PHINode::const_op_iterator PHINode::op_begin() const { return OperandTraits<PHINode> ::op_begin(const_cast<PHINode*>(this)); } PHINode::op_iterator PHINode::op_end() { return OperandTraits<PHINode>::op_end (this); } PHINode::const_op_iterator PHINode::op_end() const { return OperandTraits<PHINode>::op_end(const_cast<PHINode *>(this)); } Value *PHINode::getOperand(unsigned i_nocapture ) const { ((i_nocapture < OperandTraits<PHINode>::operands (this) && "getOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<PHINode>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2787, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<PHINode>::op_begin(const_cast<PHINode *>(this))[i_nocapture].get()); } void PHINode::setOperand( unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<PHINode>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<PHINode>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2787, __PRETTY_FUNCTION__)); OperandTraits<PHINode>:: op_begin(this)[i_nocapture] = Val_nocapture; } unsigned PHINode ::getNumOperands() const { return OperandTraits<PHINode> ::operands(this); } template <int Idx_nocapture> Use & PHINode::Op() { return this->OpFrom<Idx_nocapture>(this ); } template <int Idx_nocapture> const Use &PHINode ::Op() const { return this->OpFrom<Idx_nocapture>(this ); } | |||
2788 | ||||
2789 | //===----------------------------------------------------------------------===// | |||
2790 | // LandingPadInst Class | |||
2791 | //===----------------------------------------------------------------------===// | |||
2792 | ||||
2793 | //===--------------------------------------------------------------------------- | |||
2794 | /// The landingpad instruction holds all of the information | |||
2795 | /// necessary to generate correct exception handling. The landingpad instruction | |||
2796 | /// cannot be moved from the top of a landing pad block, which itself is | |||
2797 | /// accessible only from the 'unwind' edge of an invoke. This uses the | |||
2798 | /// SubclassData field in Value to store whether or not the landingpad is a | |||
2799 | /// cleanup. | |||
2800 | /// | |||
2801 | class LandingPadInst : public Instruction { | |||
2802 | /// The number of operands actually allocated. NumOperands is | |||
2803 | /// the number actually in use. | |||
2804 | unsigned ReservedSpace; | |||
2805 | ||||
2806 | LandingPadInst(const LandingPadInst &LP); | |||
2807 | ||||
2808 | public: | |||
2809 | enum ClauseType { Catch, Filter }; | |||
2810 | ||||
2811 | private: | |||
2812 | explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues, | |||
2813 | const Twine &NameStr, Instruction *InsertBefore); | |||
2814 | explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues, | |||
2815 | const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
2816 | ||||
2817 | // Allocate space for exactly zero operands. | |||
2818 | void *operator new(size_t s) { | |||
2819 | return User::operator new(s); | |||
2820 | } | |||
2821 | ||||
2822 | void growOperands(unsigned Size); | |||
2823 | void init(unsigned NumReservedValues, const Twine &NameStr); | |||
2824 | ||||
2825 | protected: | |||
2826 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
2827 | friend class Instruction; | |||
2828 | ||||
2829 | LandingPadInst *cloneImpl() const; | |||
2830 | ||||
2831 | public: | |||
2832 | /// Constructors - NumReservedClauses is a hint for the number of incoming | |||
2833 | /// clauses that this landingpad will have (use 0 if you really have no idea). | |||
2834 | static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses, | |||
2835 | const Twine &NameStr = "", | |||
2836 | Instruction *InsertBefore = nullptr); | |||
2837 | static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses, | |||
2838 | const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
2839 | ||||
2840 | /// Provide fast operand accessors | |||
2841 | 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; | |||
2842 | ||||
2843 | /// Return 'true' if this landingpad instruction is a | |||
2844 | /// cleanup. I.e., it should be run when unwinding even if its landing pad | |||
2845 | /// doesn't catch the exception. | |||
2846 | bool isCleanup() const { return getSubclassDataFromInstruction() & 1; } | |||
2847 | ||||
2848 | /// Indicate that this landingpad instruction is a cleanup. | |||
2849 | void setCleanup(bool V) { | |||
2850 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | | |||
2851 | (V ? 1 : 0)); | |||
2852 | } | |||
2853 | ||||
2854 | /// Add a catch or filter clause to the landing pad. | |||
2855 | void addClause(Constant *ClauseVal); | |||
2856 | ||||
2857 | /// Get the value of the clause at index Idx. Use isCatch/isFilter to | |||
2858 | /// determine what type of clause this is. | |||
2859 | Constant *getClause(unsigned Idx) const { | |||
2860 | return cast<Constant>(getOperandList()[Idx]); | |||
2861 | } | |||
2862 | ||||
2863 | /// Return 'true' if the clause and index Idx is a catch clause. | |||
2864 | bool isCatch(unsigned Idx) const { | |||
2865 | return !isa<ArrayType>(getOperandList()[Idx]->getType()); | |||
2866 | } | |||
2867 | ||||
2868 | /// Return 'true' if the clause and index Idx is a filter clause. | |||
2869 | bool isFilter(unsigned Idx) const { | |||
2870 | return isa<ArrayType>(getOperandList()[Idx]->getType()); | |||
2871 | } | |||
2872 | ||||
2873 | /// Get the number of clauses for this landing pad. | |||
2874 | unsigned getNumClauses() const { return getNumOperands(); } | |||
2875 | ||||
2876 | /// Grow the size of the operand list to accommodate the new | |||
2877 | /// number of clauses. | |||
2878 | void reserveClauses(unsigned Size) { growOperands(Size); } | |||
2879 | ||||
2880 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
2881 | static bool classof(const Instruction *I) { | |||
2882 | return I->getOpcode() == Instruction::LandingPad; | |||
2883 | } | |||
2884 | static bool classof(const Value *V) { | |||
2885 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
2886 | } | |||
2887 | }; | |||
2888 | ||||
2889 | template <> | |||
2890 | struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<1> { | |||
2891 | }; | |||
2892 | ||||
2893 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)LandingPadInst::op_iterator LandingPadInst::op_begin() { return OperandTraits<LandingPadInst>::op_begin(this); } LandingPadInst ::const_op_iterator LandingPadInst::op_begin() const { return OperandTraits<LandingPadInst>::op_begin(const_cast< LandingPadInst*>(this)); } LandingPadInst::op_iterator LandingPadInst ::op_end() { return OperandTraits<LandingPadInst>::op_end (this); } LandingPadInst::const_op_iterator LandingPadInst::op_end () const { return OperandTraits<LandingPadInst>::op_end (const_cast<LandingPadInst*>(this)); } Value *LandingPadInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<LandingPadInst>::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<LandingPadInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2893, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<LandingPadInst>::op_begin(const_cast< LandingPadInst*>(this))[i_nocapture].get()); } void LandingPadInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<LandingPadInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<LandingPadInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2893, __PRETTY_FUNCTION__)); OperandTraits<LandingPadInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned LandingPadInst::getNumOperands() const { return OperandTraits <LandingPadInst>::operands(this); } template <int Idx_nocapture > Use &LandingPadInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &LandingPadInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } | |||
2894 | ||||
2895 | //===----------------------------------------------------------------------===// | |||
2896 | // ReturnInst Class | |||
2897 | //===----------------------------------------------------------------------===// | |||
2898 | ||||
2899 | //===--------------------------------------------------------------------------- | |||
2900 | /// Return a value (possibly void), from a function. Execution | |||
2901 | /// does not continue in this function any longer. | |||
2902 | /// | |||
2903 | class ReturnInst : public Instruction { | |||
2904 | ReturnInst(const ReturnInst &RI); | |||
2905 | ||||
2906 | private: | |||
2907 | // ReturnInst constructors: | |||
2908 | // ReturnInst() - 'ret void' instruction | |||
2909 | // ReturnInst( null) - 'ret void' instruction | |||
2910 | // ReturnInst(Value* X) - 'ret X' instruction | |||
2911 | // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I | |||
2912 | // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I | |||
2913 | // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B | |||
2914 | // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B | |||
2915 | // | |||
2916 | // NOTE: If the Value* passed is of type void then the constructor behaves as | |||
2917 | // if it was passed NULL. | |||
2918 | explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr, | |||
2919 | Instruction *InsertBefore = nullptr); | |||
2920 | ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd); | |||
2921 | explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd); | |||
2922 | ||||
2923 | protected: | |||
2924 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
2925 | friend class Instruction; | |||
2926 | ||||
2927 | ReturnInst *cloneImpl() const; | |||
2928 | ||||
2929 | public: | |||
2930 | static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr, | |||
2931 | Instruction *InsertBefore = nullptr) { | |||
2932 | return new(!!retVal) ReturnInst(C, retVal, InsertBefore); | |||
2933 | } | |||
2934 | ||||
2935 | static ReturnInst* Create(LLVMContext &C, Value *retVal, | |||
2936 | BasicBlock *InsertAtEnd) { | |||
2937 | return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd); | |||
2938 | } | |||
2939 | ||||
2940 | static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) { | |||
2941 | return new(0) ReturnInst(C, InsertAtEnd); | |||
2942 | } | |||
2943 | ||||
2944 | /// Provide fast operand accessors | |||
2945 | 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; | |||
2946 | ||||
2947 | /// Convenience accessor. Returns null if there is no return value. | |||
2948 | Value *getReturnValue() const { | |||
2949 | return getNumOperands() != 0 ? getOperand(0) : nullptr; | |||
2950 | } | |||
2951 | ||||
2952 | unsigned getNumSuccessors() const { return 0; } | |||
2953 | ||||
2954 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
2955 | static bool classof(const Instruction *I) { | |||
2956 | return (I->getOpcode() == Instruction::Ret); | |||
2957 | } | |||
2958 | static bool classof(const Value *V) { | |||
2959 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
2960 | } | |||
2961 | ||||
2962 | private: | |||
2963 | BasicBlock *getSuccessor(unsigned idx) const { | |||
2964 | llvm_unreachable("ReturnInst has no successors!")::llvm::llvm_unreachable_internal("ReturnInst has no successors!" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2964); | |||
2965 | } | |||
2966 | ||||
2967 | void setSuccessor(unsigned idx, BasicBlock *B) { | |||
2968 | llvm_unreachable("ReturnInst has no successors!")::llvm::llvm_unreachable_internal("ReturnInst has no successors!" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2968); | |||
2969 | } | |||
2970 | }; | |||
2971 | ||||
2972 | template <> | |||
2973 | struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> { | |||
2974 | }; | |||
2975 | ||||
2976 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)ReturnInst::op_iterator ReturnInst::op_begin() { return OperandTraits <ReturnInst>::op_begin(this); } ReturnInst::const_op_iterator ReturnInst::op_begin() const { return OperandTraits<ReturnInst >::op_begin(const_cast<ReturnInst*>(this)); } ReturnInst ::op_iterator ReturnInst::op_end() { return OperandTraits< ReturnInst>::op_end(this); } ReturnInst::const_op_iterator ReturnInst::op_end() const { return OperandTraits<ReturnInst >::op_end(const_cast<ReturnInst*>(this)); } Value *ReturnInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<ReturnInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<ReturnInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2976, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<ReturnInst>::op_begin(const_cast<ReturnInst *>(this))[i_nocapture].get()); } void ReturnInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<ReturnInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<ReturnInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 2976, __PRETTY_FUNCTION__)); OperandTraits<ReturnInst> ::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned ReturnInst ::getNumOperands() const { return OperandTraits<ReturnInst >::operands(this); } template <int Idx_nocapture> Use &ReturnInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & ReturnInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } | |||
2977 | ||||
2978 | //===----------------------------------------------------------------------===// | |||
2979 | // BranchInst Class | |||
2980 | //===----------------------------------------------------------------------===// | |||
2981 | ||||
2982 | //===--------------------------------------------------------------------------- | |||
2983 | /// Conditional or Unconditional Branch instruction. | |||
2984 | /// | |||
2985 | class BranchInst : public Instruction { | |||
2986 | /// Ops list - Branches are strange. The operands are ordered: | |||
2987 | /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because | |||
2988 | /// they don't have to check for cond/uncond branchness. These are mostly | |||
2989 | /// accessed relative from op_end(). | |||
2990 | BranchInst(const BranchInst &BI); | |||
2991 | // BranchInst constructors (where {B, T, F} are blocks, and C is a condition): | |||
2992 | // BranchInst(BB *B) - 'br B' | |||
2993 | // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F' | |||
2994 | // BranchInst(BB* B, Inst *I) - 'br B' insert before I | |||
2995 | // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I | |||
2996 | // BranchInst(BB* B, BB *I) - 'br B' insert at end | |||
2997 | // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end | |||
2998 | explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr); | |||
2999 | BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, | |||
3000 | Instruction *InsertBefore = nullptr); | |||
3001 | BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd); | |||
3002 | BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, | |||
3003 | BasicBlock *InsertAtEnd); | |||
3004 | ||||
3005 | void AssertOK(); | |||
3006 | ||||
3007 | protected: | |||
3008 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
3009 | friend class Instruction; | |||
3010 | ||||
3011 | BranchInst *cloneImpl() const; | |||
3012 | ||||
3013 | public: | |||
3014 | /// Iterator type that casts an operand to a basic block. | |||
3015 | /// | |||
3016 | /// This only makes sense because the successors are stored as adjacent | |||
3017 | /// operands for branch instructions. | |||
3018 | struct succ_op_iterator | |||
3019 | : iterator_adaptor_base<succ_op_iterator, value_op_iterator, | |||
3020 | std::random_access_iterator_tag, BasicBlock *, | |||
3021 | ptrdiff_t, BasicBlock *, BasicBlock *> { | |||
3022 | explicit succ_op_iterator(value_op_iterator I) : iterator_adaptor_base(I) {} | |||
3023 | ||||
3024 | BasicBlock *operator*() const { return cast<BasicBlock>(*I); } | |||
3025 | BasicBlock *operator->() const { return operator*(); } | |||
3026 | }; | |||
3027 | ||||
3028 | /// The const version of `succ_op_iterator`. | |||
3029 | struct const_succ_op_iterator | |||
3030 | : iterator_adaptor_base<const_succ_op_iterator, const_value_op_iterator, | |||
3031 | std::random_access_iterator_tag, | |||
3032 | const BasicBlock *, ptrdiff_t, const BasicBlock *, | |||
3033 | const BasicBlock *> { | |||
3034 | explicit const_succ_op_iterator(const_value_op_iterator I) | |||
3035 | : iterator_adaptor_base(I) {} | |||
3036 | ||||
3037 | const BasicBlock *operator*() const { return cast<BasicBlock>(*I); } | |||
3038 | const BasicBlock *operator->() const { return operator*(); } | |||
3039 | }; | |||
3040 | ||||
3041 | static BranchInst *Create(BasicBlock *IfTrue, | |||
3042 | Instruction *InsertBefore = nullptr) { | |||
3043 | return new(1) BranchInst(IfTrue, InsertBefore); | |||
3044 | } | |||
3045 | ||||
3046 | static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, | |||
3047 | Value *Cond, Instruction *InsertBefore = nullptr) { | |||
3048 | return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore); | |||
3049 | } | |||
3050 | ||||
3051 | static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) { | |||
3052 | return new(1) BranchInst(IfTrue, InsertAtEnd); | |||
3053 | } | |||
3054 | ||||
3055 | static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, | |||
3056 | Value *Cond, BasicBlock *InsertAtEnd) { | |||
3057 | return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd); | |||
3058 | } | |||
3059 | ||||
3060 | /// Transparently provide more efficient getOperand methods. | |||
3061 | 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; | |||
3062 | ||||
3063 | bool isUnconditional() const { return getNumOperands() == 1; } | |||
3064 | bool isConditional() const { return getNumOperands() == 3; } | |||
3065 | ||||
3066 | Value *getCondition() const { | |||
3067 | assert(isConditional() && "Cannot get condition of an uncond branch!")((isConditional() && "Cannot get condition of an uncond branch!" ) ? static_cast<void> (0) : __assert_fail ("isConditional() && \"Cannot get condition of an uncond branch!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3067, __PRETTY_FUNCTION__)); | |||
3068 | return Op<-3>(); | |||
3069 | } | |||
3070 | ||||
3071 | void setCondition(Value *V) { | |||
3072 | assert(isConditional() && "Cannot set condition of unconditional branch!")((isConditional() && "Cannot set condition of unconditional branch!" ) ? static_cast<void> (0) : __assert_fail ("isConditional() && \"Cannot set condition of unconditional branch!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3072, __PRETTY_FUNCTION__)); | |||
3073 | Op<-3>() = V; | |||
3074 | } | |||
3075 | ||||
3076 | unsigned getNumSuccessors() const { return 1+isConditional(); } | |||
3077 | ||||
3078 | BasicBlock *getSuccessor(unsigned i) const { | |||
3079 | assert(i < getNumSuccessors() && "Successor # out of range for Branch!")((i < getNumSuccessors() && "Successor # out of range for Branch!" ) ? static_cast<void> (0) : __assert_fail ("i < getNumSuccessors() && \"Successor # out of range for Branch!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3079, __PRETTY_FUNCTION__)); | |||
3080 | return cast_or_null<BasicBlock>((&Op<-1>() - i)->get()); | |||
3081 | } | |||
3082 | ||||
3083 | void setSuccessor(unsigned idx, BasicBlock *NewSucc) { | |||
3084 | assert(idx < getNumSuccessors() && "Successor # out of range for Branch!")((idx < getNumSuccessors() && "Successor # out of range for Branch!" ) ? static_cast<void> (0) : __assert_fail ("idx < getNumSuccessors() && \"Successor # out of range for Branch!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3084, __PRETTY_FUNCTION__)); | |||
3085 | *(&Op<-1>() - idx) = NewSucc; | |||
3086 | } | |||
3087 | ||||
3088 | /// Swap the successors of this branch instruction. | |||
3089 | /// | |||
3090 | /// Swaps the successors of the branch instruction. This also swaps any | |||
3091 | /// branch weight metadata associated with the instruction so that it | |||
3092 | /// continues to map correctly to each operand. | |||
3093 | void swapSuccessors(); | |||
3094 | ||||
3095 | iterator_range<succ_op_iterator> successors() { | |||
3096 | return make_range( | |||
3097 | succ_op_iterator(std::next(value_op_begin(), isConditional() ? 1 : 0)), | |||
3098 | succ_op_iterator(value_op_end())); | |||
3099 | } | |||
3100 | ||||
3101 | iterator_range<const_succ_op_iterator> successors() const { | |||
3102 | return make_range(const_succ_op_iterator( | |||
3103 | std::next(value_op_begin(), isConditional() ? 1 : 0)), | |||
3104 | const_succ_op_iterator(value_op_end())); | |||
3105 | } | |||
3106 | ||||
3107 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
3108 | static bool classof(const Instruction *I) { | |||
3109 | return (I->getOpcode() == Instruction::Br); | |||
3110 | } | |||
3111 | static bool classof(const Value *V) { | |||
3112 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
3113 | } | |||
3114 | }; | |||
3115 | ||||
3116 | template <> | |||
3117 | struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> { | |||
3118 | }; | |||
3119 | ||||
3120 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)BranchInst::op_iterator BranchInst::op_begin() { return OperandTraits <BranchInst>::op_begin(this); } BranchInst::const_op_iterator BranchInst::op_begin() const { return OperandTraits<BranchInst >::op_begin(const_cast<BranchInst*>(this)); } BranchInst ::op_iterator BranchInst::op_end() { return OperandTraits< BranchInst>::op_end(this); } BranchInst::const_op_iterator BranchInst::op_end() const { return OperandTraits<BranchInst >::op_end(const_cast<BranchInst*>(this)); } Value *BranchInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<BranchInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<BranchInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3120, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<BranchInst>::op_begin(const_cast<BranchInst *>(this))[i_nocapture].get()); } void BranchInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<BranchInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<BranchInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3120, __PRETTY_FUNCTION__)); OperandTraits<BranchInst> ::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned BranchInst ::getNumOperands() const { return OperandTraits<BranchInst >::operands(this); } template <int Idx_nocapture> Use &BranchInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & BranchInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } | |||
3121 | ||||
3122 | //===----------------------------------------------------------------------===// | |||
3123 | // SwitchInst Class | |||
3124 | //===----------------------------------------------------------------------===// | |||
3125 | ||||
3126 | //===--------------------------------------------------------------------------- | |||
3127 | /// Multiway switch | |||
3128 | /// | |||
3129 | class SwitchInst : public Instruction { | |||
3130 | unsigned ReservedSpace; | |||
3131 | ||||
3132 | // Operand[0] = Value to switch on | |||
3133 | // Operand[1] = Default basic block destination | |||
3134 | // Operand[2n ] = Value to match | |||
3135 | // Operand[2n+1] = BasicBlock to go to on match | |||
3136 | SwitchInst(const SwitchInst &SI); | |||
3137 | ||||
3138 | /// Create a new switch instruction, specifying a value to switch on and a | |||
3139 | /// default destination. The number of additional cases can be specified here | |||
3140 | /// to make memory allocation more efficient. This constructor can also | |||
3141 | /// auto-insert before another instruction. | |||
3142 | SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, | |||
3143 | Instruction *InsertBefore); | |||
3144 | ||||
3145 | /// Create a new switch instruction, specifying a value to switch on and a | |||
3146 | /// default destination. The number of additional cases can be specified here | |||
3147 | /// to make memory allocation more efficient. This constructor also | |||
3148 | /// auto-inserts at the end of the specified BasicBlock. | |||
3149 | SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, | |||
3150 | BasicBlock *InsertAtEnd); | |||
3151 | ||||
3152 | // allocate space for exactly zero operands | |||
3153 | void *operator new(size_t s) { | |||
3154 | return User::operator new(s); | |||
3155 | } | |||
3156 | ||||
3157 | void init(Value *Value, BasicBlock *Default, unsigned NumReserved); | |||
3158 | void growOperands(); | |||
3159 | ||||
3160 | protected: | |||
3161 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
3162 | friend class Instruction; | |||
3163 | ||||
3164 | SwitchInst *cloneImpl() const; | |||
3165 | ||||
3166 | public: | |||
3167 | // -2 | |||
3168 | static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1); | |||
3169 | ||||
3170 | template <typename CaseHandleT> class CaseIteratorImpl; | |||
3171 | ||||
3172 | /// A handle to a particular switch case. It exposes a convenient interface | |||
3173 | /// to both the case value and the successor block. | |||
3174 | /// | |||
3175 | /// We define this as a template and instantiate it to form both a const and | |||
3176 | /// non-const handle. | |||
3177 | template <typename SwitchInstT, typename ConstantIntT, typename BasicBlockT> | |||
3178 | class CaseHandleImpl { | |||
3179 | // Directly befriend both const and non-const iterators. | |||
3180 | friend class SwitchInst::CaseIteratorImpl< | |||
3181 | CaseHandleImpl<SwitchInstT, ConstantIntT, BasicBlockT>>; | |||
3182 | ||||
3183 | protected: | |||
3184 | // Expose the switch type we're parameterized with to the iterator. | |||
3185 | using SwitchInstType = SwitchInstT; | |||
3186 | ||||
3187 | SwitchInstT *SI; | |||
3188 | ptrdiff_t Index; | |||
3189 | ||||
3190 | CaseHandleImpl() = default; | |||
3191 | CaseHandleImpl(SwitchInstT *SI, ptrdiff_t Index) : SI(SI), Index(Index) {} | |||
3192 | ||||
3193 | public: | |||
3194 | /// Resolves case value for current case. | |||
3195 | ConstantIntT *getCaseValue() const { | |||
3196 | assert((unsigned)Index < SI->getNumCases() &&(((unsigned)Index < SI->getNumCases() && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3197, __PRETTY_FUNCTION__)) | |||
3197 | "Index out the number of cases.")(((unsigned)Index < SI->getNumCases() && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3197, __PRETTY_FUNCTION__)); | |||
3198 | return reinterpret_cast<ConstantIntT *>(SI->getOperand(2 + Index * 2)); | |||
3199 | } | |||
3200 | ||||
3201 | /// Resolves successor for current case. | |||
3202 | BasicBlockT *getCaseSuccessor() const { | |||
3203 | assert(((unsigned)Index < SI->getNumCases() ||((((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3205, __PRETTY_FUNCTION__)) | |||
3204 | (unsigned)Index == DefaultPseudoIndex) &&((((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3205, __PRETTY_FUNCTION__)) | |||
3205 | "Index out the number of cases.")((((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3205, __PRETTY_FUNCTION__)); | |||
3206 | return SI->getSuccessor(getSuccessorIndex()); | |||
3207 | } | |||
3208 | ||||
3209 | /// Returns number of current case. | |||
3210 | unsigned getCaseIndex() const { return Index; } | |||
3211 | ||||
3212 | /// Returns successor index for current case successor. | |||
3213 | unsigned getSuccessorIndex() const { | |||
3214 | assert(((unsigned)Index == DefaultPseudoIndex ||((((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3216, __PRETTY_FUNCTION__)) | |||
3215 | (unsigned)Index < SI->getNumCases()) &&((((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3216, __PRETTY_FUNCTION__)) | |||
3216 | "Index out the number of cases.")((((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3216, __PRETTY_FUNCTION__)); | |||
3217 | return (unsigned)Index != DefaultPseudoIndex ? Index + 1 : 0; | |||
3218 | } | |||
3219 | ||||
3220 | bool operator==(const CaseHandleImpl &RHS) const { | |||
3221 | assert(SI == RHS.SI && "Incompatible operators.")((SI == RHS.SI && "Incompatible operators.") ? static_cast <void> (0) : __assert_fail ("SI == RHS.SI && \"Incompatible operators.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3221, __PRETTY_FUNCTION__)); | |||
3222 | return Index == RHS.Index; | |||
3223 | } | |||
3224 | }; | |||
3225 | ||||
3226 | using ConstCaseHandle = | |||
3227 | CaseHandleImpl<const SwitchInst, const ConstantInt, const BasicBlock>; | |||
3228 | ||||
3229 | class CaseHandle | |||
3230 | : public CaseHandleImpl<SwitchInst, ConstantInt, BasicBlock> { | |||
3231 | friend class SwitchInst::CaseIteratorImpl<CaseHandle>; | |||
3232 | ||||
3233 | public: | |||
3234 | CaseHandle(SwitchInst *SI, ptrdiff_t Index) : CaseHandleImpl(SI, Index) {} | |||
3235 | ||||
3236 | /// Sets the new value for current case. | |||
3237 | void setValue(ConstantInt *V) { | |||
3238 | assert((unsigned)Index < SI->getNumCases() &&(((unsigned)Index < SI->getNumCases() && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3239, __PRETTY_FUNCTION__)) | |||
3239 | "Index out the number of cases.")(((unsigned)Index < SI->getNumCases() && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3239, __PRETTY_FUNCTION__)); | |||
3240 | SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V)); | |||
3241 | } | |||
3242 | ||||
3243 | /// Sets the new successor for current case. | |||
3244 | void setSuccessor(BasicBlock *S) { | |||
3245 | SI->setSuccessor(getSuccessorIndex(), S); | |||
3246 | } | |||
3247 | }; | |||
3248 | ||||
3249 | template <typename CaseHandleT> | |||
3250 | class CaseIteratorImpl | |||
3251 | : public iterator_facade_base<CaseIteratorImpl<CaseHandleT>, | |||
3252 | std::random_access_iterator_tag, | |||
3253 | CaseHandleT> { | |||
3254 | using SwitchInstT = typename CaseHandleT::SwitchInstType; | |||
3255 | ||||
3256 | CaseHandleT Case; | |||
3257 | ||||
3258 | public: | |||
3259 | /// Default constructed iterator is in an invalid state until assigned to | |||
3260 | /// a case for a particular switch. | |||
3261 | CaseIteratorImpl() = default; | |||
3262 | ||||
3263 | /// Initializes case iterator for given SwitchInst and for given | |||
3264 | /// case number. | |||
3265 | CaseIteratorImpl(SwitchInstT *SI, unsigned CaseNum) : Case(SI, CaseNum) {} | |||
3266 | ||||
3267 | /// Initializes case iterator for given SwitchInst and for given | |||
3268 | /// successor index. | |||
3269 | static CaseIteratorImpl fromSuccessorIndex(SwitchInstT *SI, | |||
3270 | unsigned SuccessorIndex) { | |||
3271 | assert(SuccessorIndex < SI->getNumSuccessors() &&((SuccessorIndex < SI->getNumSuccessors() && "Successor index # out of range!" ) ? static_cast<void> (0) : __assert_fail ("SuccessorIndex < SI->getNumSuccessors() && \"Successor index # out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3272, __PRETTY_FUNCTION__)) | |||
3272 | "Successor index # out of range!")((SuccessorIndex < SI->getNumSuccessors() && "Successor index # out of range!" ) ? static_cast<void> (0) : __assert_fail ("SuccessorIndex < SI->getNumSuccessors() && \"Successor index # out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3272, __PRETTY_FUNCTION__)); | |||
3273 | return SuccessorIndex != 0 ? CaseIteratorImpl(SI, SuccessorIndex - 1) | |||
3274 | : CaseIteratorImpl(SI, DefaultPseudoIndex); | |||
3275 | } | |||
3276 | ||||
3277 | /// Support converting to the const variant. This will be a no-op for const | |||
3278 | /// variant. | |||
3279 | operator CaseIteratorImpl<ConstCaseHandle>() const { | |||
3280 | return CaseIteratorImpl<ConstCaseHandle>(Case.SI, Case.Index); | |||
3281 | } | |||
3282 | ||||
3283 | CaseIteratorImpl &operator+=(ptrdiff_t N) { | |||
3284 | // Check index correctness after addition. | |||
3285 | // Note: Index == getNumCases() means end(). | |||
3286 | assert(Case.Index + N >= 0 &&((Case.Index + N >= 0 && (unsigned)(Case.Index + N ) <= Case.SI->getNumCases() && "Case.Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("Case.Index + N >= 0 && (unsigned)(Case.Index + N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3288, __PRETTY_FUNCTION__)) | |||
3287 | (unsigned)(Case.Index + N) <= Case.SI->getNumCases() &&((Case.Index + N >= 0 && (unsigned)(Case.Index + N ) <= Case.SI->getNumCases() && "Case.Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("Case.Index + N >= 0 && (unsigned)(Case.Index + N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3288, __PRETTY_FUNCTION__)) | |||
3288 | "Case.Index out the number of cases.")((Case.Index + N >= 0 && (unsigned)(Case.Index + N ) <= Case.SI->getNumCases() && "Case.Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("Case.Index + N >= 0 && (unsigned)(Case.Index + N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3288, __PRETTY_FUNCTION__)); | |||
3289 | Case.Index += N; | |||
3290 | return *this; | |||
3291 | } | |||
3292 | CaseIteratorImpl &operator-=(ptrdiff_t N) { | |||
3293 | // Check index correctness after subtraction. | |||
3294 | // Note: Case.Index == getNumCases() means end(). | |||
3295 | assert(Case.Index - N >= 0 &&((Case.Index - N >= 0 && (unsigned)(Case.Index - N ) <= Case.SI->getNumCases() && "Case.Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("Case.Index - N >= 0 && (unsigned)(Case.Index - N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3297, __PRETTY_FUNCTION__)) | |||
3296 | (unsigned)(Case.Index - N) <= Case.SI->getNumCases() &&((Case.Index - N >= 0 && (unsigned)(Case.Index - N ) <= Case.SI->getNumCases() && "Case.Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("Case.Index - N >= 0 && (unsigned)(Case.Index - N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3297, __PRETTY_FUNCTION__)) | |||
3297 | "Case.Index out the number of cases.")((Case.Index - N >= 0 && (unsigned)(Case.Index - N ) <= Case.SI->getNumCases() && "Case.Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("Case.Index - N >= 0 && (unsigned)(Case.Index - N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3297, __PRETTY_FUNCTION__)); | |||
3298 | Case.Index -= N; | |||
3299 | return *this; | |||
3300 | } | |||
3301 | ptrdiff_t operator-(const CaseIteratorImpl &RHS) const { | |||
3302 | assert(Case.SI == RHS.Case.SI && "Incompatible operators.")((Case.SI == RHS.Case.SI && "Incompatible operators." ) ? static_cast<void> (0) : __assert_fail ("Case.SI == RHS.Case.SI && \"Incompatible operators.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3302, __PRETTY_FUNCTION__)); | |||
3303 | return Case.Index - RHS.Case.Index; | |||
3304 | } | |||
3305 | bool operator==(const CaseIteratorImpl &RHS) const { | |||
3306 | return Case == RHS.Case; | |||
3307 | } | |||
3308 | bool operator<(const CaseIteratorImpl &RHS) const { | |||
3309 | assert(Case.SI == RHS.Case.SI && "Incompatible operators.")((Case.SI == RHS.Case.SI && "Incompatible operators." ) ? static_cast<void> (0) : __assert_fail ("Case.SI == RHS.Case.SI && \"Incompatible operators.\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3309, __PRETTY_FUNCTION__)); | |||
3310 | return Case.Index < RHS.Case.Index; | |||
3311 | } | |||
3312 | CaseHandleT &operator*() { return Case; } | |||
3313 | const CaseHandleT &operator*() const { return Case; } | |||
3314 | }; | |||
3315 | ||||
3316 | using CaseIt = CaseIteratorImpl<CaseHandle>; | |||
3317 | using ConstCaseIt = CaseIteratorImpl<ConstCaseHandle>; | |||
3318 | ||||
3319 | static SwitchInst *Create(Value *Value, BasicBlock *Default, | |||
3320 | unsigned NumCases, | |||
3321 | Instruction *InsertBefore = nullptr) { | |||
3322 | return new SwitchInst(Value, Default, NumCases, InsertBefore); | |||
3323 | } | |||
3324 | ||||
3325 | static SwitchInst *Create(Value *Value, BasicBlock *Default, | |||
3326 | unsigned NumCases, BasicBlock *InsertAtEnd) { | |||
3327 | return new SwitchInst(Value, Default, NumCases, InsertAtEnd); | |||
3328 | } | |||
3329 | ||||
3330 | /// Provide fast operand accessors | |||
3331 | 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; | |||
3332 | ||||
3333 | // Accessor Methods for Switch stmt | |||
3334 | Value *getCondition() const { return getOperand(0); } | |||
3335 | void setCondition(Value *V) { setOperand(0, V); } | |||
3336 | ||||
3337 | BasicBlock *getDefaultDest() const { | |||
3338 | return cast<BasicBlock>(getOperand(1)); | |||
3339 | } | |||
3340 | ||||
3341 | void setDefaultDest(BasicBlock *DefaultCase) { | |||
3342 | setOperand(1, reinterpret_cast<Value*>(DefaultCase)); | |||
3343 | } | |||
3344 | ||||
3345 | /// Return the number of 'cases' in this switch instruction, excluding the | |||
3346 | /// default case. | |||
3347 | unsigned getNumCases() const { | |||
3348 | return getNumOperands()/2 - 1; | |||
3349 | } | |||
3350 | ||||
3351 | /// Returns a read/write iterator that points to the first case in the | |||
3352 | /// SwitchInst. | |||
3353 | CaseIt case_begin() { | |||
3354 | return CaseIt(this, 0); | |||
3355 | } | |||
3356 | ||||
3357 | /// Returns a read-only iterator that points to the first case in the | |||
3358 | /// SwitchInst. | |||
3359 | ConstCaseIt case_begin() const { | |||
3360 | return ConstCaseIt(this, 0); | |||
3361 | } | |||
3362 | ||||
3363 | /// Returns a read/write iterator that points one past the last in the | |||
3364 | /// SwitchInst. | |||
3365 | CaseIt case_end() { | |||
3366 | return CaseIt(this, getNumCases()); | |||
3367 | } | |||
3368 | ||||
3369 | /// Returns a read-only iterator that points one past the last in the | |||
3370 | /// SwitchInst. | |||
3371 | ConstCaseIt case_end() const { | |||
3372 | return ConstCaseIt(this, getNumCases()); | |||
3373 | } | |||
3374 | ||||
3375 | /// Iteration adapter for range-for loops. | |||
3376 | iterator_range<CaseIt> cases() { | |||
3377 | return make_range(case_begin(), case_end()); | |||
3378 | } | |||
3379 | ||||
3380 | /// Constant iteration adapter for range-for loops. | |||
3381 | iterator_range<ConstCaseIt> cases() const { | |||
3382 | return make_range(case_begin(), case_end()); | |||
3383 | } | |||
3384 | ||||
3385 | /// Returns an iterator that points to the default case. | |||
3386 | /// Note: this iterator allows to resolve successor only. Attempt | |||
3387 | /// to resolve case value causes an assertion. | |||
3388 | /// Also note, that increment and decrement also causes an assertion and | |||
3389 | /// makes iterator invalid. | |||
3390 | CaseIt case_default() { | |||
3391 | return CaseIt(this, DefaultPseudoIndex); | |||
3392 | } | |||
3393 | ConstCaseIt case_default() const { | |||
3394 | return ConstCaseIt(this, DefaultPseudoIndex); | |||
3395 | } | |||
3396 | ||||
3397 | /// Search all of the case values for the specified constant. If it is | |||
3398 | /// explicitly handled, return the case iterator of it, otherwise return | |||
3399 | /// default case iterator to indicate that it is handled by the default | |||
3400 | /// handler. | |||
3401 | CaseIt findCaseValue(const ConstantInt *C) { | |||
3402 | CaseIt I = llvm::find_if( | |||
3403 | cases(), [C](CaseHandle &Case) { return Case.getCaseValue() == C; }); | |||
3404 | if (I != case_end()) | |||
3405 | return I; | |||
3406 | ||||
3407 | return case_default(); | |||
3408 | } | |||
3409 | ConstCaseIt findCaseValue(const ConstantInt *C) const { | |||
3410 | ConstCaseIt I = llvm::find_if(cases(), [C](ConstCaseHandle &Case) { | |||
3411 | return Case.getCaseValue() == C; | |||
3412 | }); | |||
3413 | if (I != case_end()) | |||
3414 | return I; | |||
3415 | ||||
3416 | return case_default(); | |||
3417 | } | |||
3418 | ||||
3419 | /// Finds the unique case value for a given successor. Returns null if the | |||
3420 | /// successor is not found, not unique, or is the default case. | |||
3421 | ConstantInt *findCaseDest(BasicBlock *BB) { | |||
3422 | if (BB == getDefaultDest()) | |||
3423 | return nullptr; | |||
3424 | ||||
3425 | ConstantInt *CI = nullptr; | |||
3426 | for (auto Case : cases()) { | |||
3427 | if (Case.getCaseSuccessor() != BB) | |||
3428 | continue; | |||
3429 | ||||
3430 | if (CI) | |||
3431 | return nullptr; // Multiple cases lead to BB. | |||
3432 | ||||
3433 | CI = Case.getCaseValue(); | |||
3434 | } | |||
3435 | ||||
3436 | return CI; | |||
3437 | } | |||
3438 | ||||
3439 | /// Add an entry to the switch instruction. | |||
3440 | /// Note: | |||
3441 | /// This action invalidates case_end(). Old case_end() iterator will | |||
3442 | /// point to the added case. | |||
3443 | void addCase(ConstantInt *OnVal, BasicBlock *Dest); | |||
3444 | ||||
3445 | /// This method removes the specified case and its successor from the switch | |||
3446 | /// instruction. Note that this operation may reorder the remaining cases at | |||
3447 | /// index idx and above. | |||
3448 | /// Note: | |||
3449 | /// This action invalidates iterators for all cases following the one removed, | |||
3450 | /// including the case_end() iterator. It returns an iterator for the next | |||
3451 | /// case. | |||
3452 | CaseIt removeCase(CaseIt I); | |||
3453 | ||||
3454 | unsigned getNumSuccessors() const { return getNumOperands()/2; } | |||
3455 | BasicBlock *getSuccessor(unsigned idx) const { | |||
3456 | assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!")((idx < getNumSuccessors() &&"Successor idx out of range for switch!" ) ? static_cast<void> (0) : __assert_fail ("idx < getNumSuccessors() &&\"Successor idx out of range for switch!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3456, __PRETTY_FUNCTION__)); | |||
3457 | return cast<BasicBlock>(getOperand(idx*2+1)); | |||
3458 | } | |||
3459 | void setSuccessor(unsigned idx, BasicBlock *NewSucc) { | |||
3460 | assert(idx < getNumSuccessors() && "Successor # out of range for switch!")((idx < getNumSuccessors() && "Successor # out of range for switch!" ) ? static_cast<void> (0) : __assert_fail ("idx < getNumSuccessors() && \"Successor # out of range for switch!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3460, __PRETTY_FUNCTION__)); | |||
3461 | setOperand(idx * 2 + 1, NewSucc); | |||
3462 | } | |||
3463 | ||||
3464 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
3465 | static bool classof(const Instruction *I) { | |||
3466 | return I->getOpcode() == Instruction::Switch; | |||
3467 | } | |||
3468 | static bool classof(const Value *V) { | |||
3469 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
3470 | } | |||
3471 | }; | |||
3472 | ||||
3473 | /// A wrapper class to simplify modification of SwitchInst cases along with | |||
3474 | /// their prof branch_weights metadata. | |||
3475 | class SwitchInstProfUpdateWrapper { | |||
3476 | SwitchInst &SI; | |||
3477 | Optional<SmallVector<uint32_t, 8> > Weights = None; | |||
3478 | bool Changed = false; | |||
3479 | ||||
3480 | protected: | |||
3481 | static MDNode *getProfBranchWeightsMD(const SwitchInst &SI); | |||
3482 | ||||
3483 | MDNode *buildProfBranchWeightsMD(); | |||
3484 | ||||
3485 | void init(); | |||
3486 | ||||
3487 | public: | |||
3488 | using CaseWeightOpt = Optional<uint32_t>; | |||
3489 | SwitchInst *operator->() { return &SI; } | |||
3490 | SwitchInst &operator*() { return SI; } | |||
3491 | operator SwitchInst *() { return &SI; } | |||
3492 | ||||
3493 | SwitchInstProfUpdateWrapper(SwitchInst &SI) : SI(SI) { init(); } | |||
3494 | ||||
3495 | ~SwitchInstProfUpdateWrapper() { | |||
3496 | if (Changed) | |||
3497 | SI.setMetadata(LLVMContext::MD_prof, buildProfBranchWeightsMD()); | |||
3498 | } | |||
3499 | ||||
3500 | /// Delegate the call to the underlying SwitchInst::removeCase() and remove | |||
3501 | /// correspondent branch weight. | |||
3502 | SwitchInst::CaseIt removeCase(SwitchInst::CaseIt I); | |||
3503 | ||||
3504 | /// Delegate the call to the underlying SwitchInst::addCase() and set the | |||
3505 | /// specified branch weight for the added case. | |||
3506 | void addCase(ConstantInt *OnVal, BasicBlock *Dest, CaseWeightOpt W); | |||
3507 | ||||
3508 | /// Delegate the call to the underlying SwitchInst::eraseFromParent() and mark | |||
3509 | /// this object to not touch the underlying SwitchInst in destructor. | |||
3510 | SymbolTableList<Instruction>::iterator eraseFromParent(); | |||
3511 | ||||
3512 | void setSuccessorWeight(unsigned idx, CaseWeightOpt W); | |||
3513 | CaseWeightOpt getSuccessorWeight(unsigned idx); | |||
3514 | ||||
3515 | static CaseWeightOpt getSuccessorWeight(const SwitchInst &SI, unsigned idx); | |||
3516 | }; | |||
3517 | ||||
3518 | template <> | |||
3519 | struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> { | |||
3520 | }; | |||
3521 | ||||
3522 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)SwitchInst::op_iterator SwitchInst::op_begin() { return OperandTraits <SwitchInst>::op_begin(this); } SwitchInst::const_op_iterator SwitchInst::op_begin() const { return OperandTraits<SwitchInst >::op_begin(const_cast<SwitchInst*>(this)); } SwitchInst ::op_iterator SwitchInst::op_end() { return OperandTraits< SwitchInst>::op_end(this); } SwitchInst::const_op_iterator SwitchInst::op_end() const { return OperandTraits<SwitchInst >::op_end(const_cast<SwitchInst*>(this)); } Value *SwitchInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<SwitchInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<SwitchInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3522, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<SwitchInst>::op_begin(const_cast<SwitchInst *>(this))[i_nocapture].get()); } void SwitchInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<SwitchInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<SwitchInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3522, __PRETTY_FUNCTION__)); OperandTraits<SwitchInst> ::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned SwitchInst ::getNumOperands() const { return OperandTraits<SwitchInst >::operands(this); } template <int Idx_nocapture> Use &SwitchInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & SwitchInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } | |||
3523 | ||||
3524 | //===----------------------------------------------------------------------===// | |||
3525 | // IndirectBrInst Class | |||
3526 | //===----------------------------------------------------------------------===// | |||
3527 | ||||
3528 | //===--------------------------------------------------------------------------- | |||
3529 | /// Indirect Branch Instruction. | |||
3530 | /// | |||
3531 | class IndirectBrInst : public Instruction { | |||
3532 | unsigned ReservedSpace; | |||
3533 | ||||
3534 | // Operand[0] = Address to jump to | |||
3535 | // Operand[n+1] = n-th destination | |||
3536 | IndirectBrInst(const IndirectBrInst &IBI); | |||
3537 | ||||
3538 | /// Create a new indirectbr instruction, specifying an | |||
3539 | /// Address to jump to. The number of expected destinations can be specified | |||
3540 | /// here to make memory allocation more efficient. This constructor can also | |||
3541 | /// autoinsert before another instruction. | |||
3542 | IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore); | |||
3543 | ||||
3544 | /// Create a new indirectbr instruction, specifying an | |||
3545 | /// Address to jump to. The number of expected destinations can be specified | |||
3546 | /// here to make memory allocation more efficient. This constructor also | |||
3547 | /// autoinserts at the end of the specified BasicBlock. | |||
3548 | IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd); | |||
3549 | ||||
3550 | // allocate space for exactly zero operands | |||
3551 | void *operator new(size_t s) { | |||
3552 | return User::operator new(s); | |||
3553 | } | |||
3554 | ||||
3555 | void init(Value *Address, unsigned NumDests); | |||
3556 | void growOperands(); | |||
3557 | ||||
3558 | protected: | |||
3559 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
3560 | friend class Instruction; | |||
3561 | ||||
3562 | IndirectBrInst *cloneImpl() const; | |||
3563 | ||||
3564 | public: | |||
3565 | /// Iterator type that casts an operand to a basic block. | |||
3566 | /// | |||
3567 | /// This only makes sense because the successors are stored as adjacent | |||
3568 | /// operands for indirectbr instructions. | |||
3569 | struct succ_op_iterator | |||
3570 | : iterator_adaptor_base<succ_op_iterator, value_op_iterator, | |||
3571 | std::random_access_iterator_tag, BasicBlock *, | |||
3572 | ptrdiff_t, BasicBlock *, BasicBlock *> { | |||
3573 | explicit succ_op_iterator(value_op_iterator I) : iterator_adaptor_base(I) {} | |||
3574 | ||||
3575 | BasicBlock *operator*() const { return cast<BasicBlock>(*I); } | |||
3576 | BasicBlock *operator->() const { return operator*(); } | |||
3577 | }; | |||
3578 | ||||
3579 | /// The const version of `succ_op_iterator`. | |||
3580 | struct const_succ_op_iterator | |||
3581 | : iterator_adaptor_base<const_succ_op_iterator, const_value_op_iterator, | |||
3582 | std::random_access_iterator_tag, | |||
3583 | const BasicBlock *, ptrdiff_t, const BasicBlock *, | |||
3584 | const BasicBlock *> { | |||
3585 | explicit const_succ_op_iterator(const_value_op_iterator I) | |||
3586 | : iterator_adaptor_base(I) {} | |||
3587 | ||||
3588 | const BasicBlock *operator*() const { return cast<BasicBlock>(*I); } | |||
3589 | const BasicBlock *operator->() const { return operator*(); } | |||
3590 | }; | |||
3591 | ||||
3592 | static IndirectBrInst *Create(Value *Address, unsigned NumDests, | |||
3593 | Instruction *InsertBefore = nullptr) { | |||
3594 | return new IndirectBrInst(Address, NumDests, InsertBefore); | |||
3595 | } | |||
3596 | ||||
3597 | static IndirectBrInst *Create(Value *Address, unsigned NumDests, | |||
3598 | BasicBlock *InsertAtEnd) { | |||
3599 | return new IndirectBrInst(Address, NumDests, InsertAtEnd); | |||
3600 | } | |||
3601 | ||||
3602 | /// Provide fast operand accessors. | |||
3603 | 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; | |||
3604 | ||||
3605 | // Accessor Methods for IndirectBrInst instruction. | |||
3606 | Value *getAddress() { return getOperand(0); } | |||
3607 | const Value *getAddress() const { return getOperand(0); } | |||
3608 | void setAddress(Value *V) { setOperand(0, V); } | |||
3609 | ||||
3610 | /// return the number of possible destinations in this | |||
3611 | /// indirectbr instruction. | |||
3612 | unsigned getNumDestinations() const { return getNumOperands()-1; } | |||
3613 | ||||
3614 | /// Return the specified destination. | |||
3615 | BasicBlock *getDestination(unsigned i) { return getSuccessor(i); } | |||
3616 | const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); } | |||
3617 | ||||
3618 | /// Add a destination. | |||
3619 | /// | |||
3620 | void addDestination(BasicBlock *Dest); | |||
3621 | ||||
3622 | /// This method removes the specified successor from the | |||
3623 | /// indirectbr instruction. | |||
3624 | void removeDestination(unsigned i); | |||
3625 | ||||
3626 | unsigned getNumSuccessors() const { return getNumOperands()-1; } | |||
3627 | BasicBlock *getSuccessor(unsigned i) const { | |||
3628 | return cast<BasicBlock>(getOperand(i+1)); | |||
3629 | } | |||
3630 | void setSuccessor(unsigned i, BasicBlock *NewSucc) { | |||
3631 | setOperand(i + 1, NewSucc); | |||
3632 | } | |||
3633 | ||||
3634 | iterator_range<succ_op_iterator> successors() { | |||
3635 | return make_range(succ_op_iterator(std::next(value_op_begin())), | |||
3636 | succ_op_iterator(value_op_end())); | |||
3637 | } | |||
3638 | ||||
3639 | iterator_range<const_succ_op_iterator> successors() const { | |||
3640 | return make_range(const_succ_op_iterator(std::next(value_op_begin())), | |||
3641 | const_succ_op_iterator(value_op_end())); | |||
3642 | } | |||
3643 | ||||
3644 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
3645 | static bool classof(const Instruction *I) { | |||
3646 | return I->getOpcode() == Instruction::IndirectBr; | |||
3647 | } | |||
3648 | static bool classof(const Value *V) { | |||
3649 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
3650 | } | |||
3651 | }; | |||
3652 | ||||
3653 | template <> | |||
3654 | struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> { | |||
3655 | }; | |||
3656 | ||||
3657 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)IndirectBrInst::op_iterator IndirectBrInst::op_begin() { return OperandTraits<IndirectBrInst>::op_begin(this); } IndirectBrInst ::const_op_iterator IndirectBrInst::op_begin() const { return OperandTraits<IndirectBrInst>::op_begin(const_cast< IndirectBrInst*>(this)); } IndirectBrInst::op_iterator IndirectBrInst ::op_end() { return OperandTraits<IndirectBrInst>::op_end (this); } IndirectBrInst::const_op_iterator IndirectBrInst::op_end () const { return OperandTraits<IndirectBrInst>::op_end (const_cast<IndirectBrInst*>(this)); } Value *IndirectBrInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<IndirectBrInst>::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<IndirectBrInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3657, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<IndirectBrInst>::op_begin(const_cast< IndirectBrInst*>(this))[i_nocapture].get()); } void IndirectBrInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<IndirectBrInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<IndirectBrInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3657, __PRETTY_FUNCTION__)); OperandTraits<IndirectBrInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned IndirectBrInst::getNumOperands() const { return OperandTraits <IndirectBrInst>::operands(this); } template <int Idx_nocapture > Use &IndirectBrInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &IndirectBrInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } | |||
3658 | ||||
3659 | //===----------------------------------------------------------------------===// | |||
3660 | // InvokeInst Class | |||
3661 | //===----------------------------------------------------------------------===// | |||
3662 | ||||
3663 | /// Invoke instruction. The SubclassData field is used to hold the | |||
3664 | /// calling convention of the call. | |||
3665 | /// | |||
3666 | class InvokeInst : public CallBase { | |||
3667 | /// The number of operands for this call beyond the called function, | |||
3668 | /// arguments, and operand bundles. | |||
3669 | static constexpr int NumExtraOperands = 2; | |||
3670 | ||||
3671 | /// The index from the end of the operand array to the normal destination. | |||
3672 | static constexpr int NormalDestOpEndIdx = -3; | |||
3673 | ||||
3674 | /// The index from the end of the operand array to the unwind destination. | |||
3675 | static constexpr int UnwindDestOpEndIdx = -2; | |||
3676 | ||||
3677 | InvokeInst(const InvokeInst &BI); | |||
3678 | ||||
3679 | /// Construct an InvokeInst given a range of arguments. | |||
3680 | /// | |||
3681 | /// Construct an InvokeInst from a range of arguments | |||
3682 | inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, | |||
3683 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3684 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, | |||
3685 | const Twine &NameStr, Instruction *InsertBefore); | |||
3686 | ||||
3687 | inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, | |||
3688 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3689 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, | |||
3690 | const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
3691 | ||||
3692 | void init(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, | |||
3693 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3694 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr); | |||
3695 | ||||
3696 | /// Compute the number of operands to allocate. | |||
3697 | static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) { | |||
3698 | // We need one operand for the called function, plus our extra operands and | |||
3699 | // the input operand counts provided. | |||
3700 | return 1 + NumExtraOperands + NumArgs + NumBundleInputs; | |||
3701 | } | |||
3702 | ||||
3703 | protected: | |||
3704 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
3705 | friend class Instruction; | |||
3706 | ||||
3707 | InvokeInst *cloneImpl() const; | |||
3708 | ||||
3709 | public: | |||
3710 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, | |||
3711 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3712 | const Twine &NameStr, | |||
3713 | Instruction *InsertBefore = nullptr) { | |||
3714 | int NumOperands = ComputeNumOperands(Args.size()); | |||
3715 | return new (NumOperands) | |||
3716 | InvokeInst(Ty, Func, IfNormal, IfException, Args, None, NumOperands, | |||
3717 | NameStr, InsertBefore); | |||
3718 | } | |||
3719 | ||||
3720 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, | |||
3721 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3722 | ArrayRef<OperandBundleDef> Bundles = None, | |||
3723 | const Twine &NameStr = "", | |||
3724 | Instruction *InsertBefore = nullptr) { | |||
3725 | int NumOperands = | |||
3726 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); | |||
3727 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); | |||
3728 | ||||
3729 | return new (NumOperands, DescriptorBytes) | |||
3730 | InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, NumOperands, | |||
3731 | NameStr, InsertBefore); | |||
3732 | } | |||
3733 | ||||
3734 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, | |||
3735 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3736 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
3737 | int NumOperands = ComputeNumOperands(Args.size()); | |||
3738 | return new (NumOperands) | |||
3739 | InvokeInst(Ty, Func, IfNormal, IfException, Args, None, NumOperands, | |||
3740 | NameStr, InsertAtEnd); | |||
3741 | } | |||
3742 | ||||
3743 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, | |||
3744 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3745 | ArrayRef<OperandBundleDef> Bundles, | |||
3746 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
3747 | int NumOperands = | |||
3748 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); | |||
3749 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); | |||
3750 | ||||
3751 | return new (NumOperands, DescriptorBytes) | |||
3752 | InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, NumOperands, | |||
3753 | NameStr, InsertAtEnd); | |||
3754 | } | |||
3755 | ||||
3756 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, | |||
3757 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3758 | const Twine &NameStr, | |||
3759 | Instruction *InsertBefore = nullptr) { | |||
3760 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, | |||
3761 | IfException, Args, None, NameStr, InsertBefore); | |||
3762 | } | |||
3763 | ||||
3764 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, | |||
3765 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3766 | ArrayRef<OperandBundleDef> Bundles = None, | |||
3767 | const Twine &NameStr = "", | |||
3768 | Instruction *InsertBefore = nullptr) { | |||
3769 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, | |||
3770 | IfException, Args, Bundles, NameStr, InsertBefore); | |||
3771 | } | |||
3772 | ||||
3773 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, | |||
3774 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3775 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
3776 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, | |||
3777 | IfException, Args, NameStr, InsertAtEnd); | |||
3778 | } | |||
3779 | ||||
3780 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, | |||
3781 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3782 | ArrayRef<OperandBundleDef> Bundles, | |||
3783 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
3784 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, | |||
3785 | IfException, Args, Bundles, NameStr, InsertAtEnd); | |||
3786 | } | |||
3787 | ||||
3788 | // Deprecated [opaque pointer types] | |||
3789 | static InvokeInst *Create(Value *Func, BasicBlock *IfNormal, | |||
3790 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3791 | const Twine &NameStr, | |||
3792 | Instruction *InsertBefore = nullptr) { | |||
3793 | return Create(cast<FunctionType>( | |||
3794 | cast<PointerType>(Func->getType())->getElementType()), | |||
3795 | Func, IfNormal, IfException, Args, None, NameStr, | |||
3796 | InsertBefore); | |||
3797 | } | |||
3798 | ||||
3799 | // Deprecated [opaque pointer types] | |||
3800 | static InvokeInst *Create(Value *Func, BasicBlock *IfNormal, | |||
3801 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3802 | ArrayRef<OperandBundleDef> Bundles = None, | |||
3803 | const Twine &NameStr = "", | |||
3804 | Instruction *InsertBefore = nullptr) { | |||
3805 | return Create(cast<FunctionType>( | |||
3806 | cast<PointerType>(Func->getType())->getElementType()), | |||
3807 | Func, IfNormal, IfException, Args, Bundles, NameStr, | |||
3808 | InsertBefore); | |||
3809 | } | |||
3810 | ||||
3811 | // Deprecated [opaque pointer types] | |||
3812 | static InvokeInst *Create(Value *Func, BasicBlock *IfNormal, | |||
3813 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3814 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
3815 | return Create(cast<FunctionType>( | |||
3816 | cast<PointerType>(Func->getType())->getElementType()), | |||
3817 | Func, IfNormal, IfException, Args, NameStr, InsertAtEnd); | |||
3818 | } | |||
3819 | ||||
3820 | // Deprecated [opaque pointer types] | |||
3821 | static InvokeInst *Create(Value *Func, BasicBlock *IfNormal, | |||
3822 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3823 | ArrayRef<OperandBundleDef> Bundles, | |||
3824 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
3825 | return Create(cast<FunctionType>( | |||
3826 | cast<PointerType>(Func->getType())->getElementType()), | |||
3827 | Func, IfNormal, IfException, Args, Bundles, NameStr, | |||
3828 | InsertAtEnd); | |||
3829 | } | |||
3830 | ||||
3831 | /// Create a clone of \p II with a different set of operand bundles and | |||
3832 | /// insert it before \p InsertPt. | |||
3833 | /// | |||
3834 | /// The returned invoke instruction is identical to \p II in every way except | |||
3835 | /// that the operand bundles for the new instruction are set to the operand | |||
3836 | /// bundles in \p Bundles. | |||
3837 | static InvokeInst *Create(InvokeInst *II, ArrayRef<OperandBundleDef> Bundles, | |||
3838 | Instruction *InsertPt = nullptr); | |||
3839 | ||||
3840 | /// Determine if the call should not perform indirect branch tracking. | |||
3841 | bool doesNoCfCheck() const { return hasFnAttr(Attribute::NoCfCheck); } | |||
3842 | ||||
3843 | /// Determine if the call cannot unwind. | |||
3844 | bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); } | |||
3845 | void setDoesNotThrow() { | |||
3846 | addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind); | |||
3847 | } | |||
3848 | ||||
3849 | // get*Dest - Return the destination basic blocks... | |||
3850 | BasicBlock *getNormalDest() const { | |||
3851 | return cast<BasicBlock>(Op<NormalDestOpEndIdx>()); | |||
3852 | } | |||
3853 | BasicBlock *getUnwindDest() const { | |||
3854 | return cast<BasicBlock>(Op<UnwindDestOpEndIdx>()); | |||
3855 | } | |||
3856 | void setNormalDest(BasicBlock *B) { | |||
3857 | Op<NormalDestOpEndIdx>() = reinterpret_cast<Value *>(B); | |||
3858 | } | |||
3859 | void setUnwindDest(BasicBlock *B) { | |||
3860 | Op<UnwindDestOpEndIdx>() = reinterpret_cast<Value *>(B); | |||
3861 | } | |||
3862 | ||||
3863 | /// Get the landingpad instruction from the landing pad | |||
3864 | /// block (the unwind destination). | |||
3865 | LandingPadInst *getLandingPadInst() const; | |||
3866 | ||||
3867 | BasicBlock *getSuccessor(unsigned i) const { | |||
3868 | assert(i < 2 && "Successor # out of range for invoke!")((i < 2 && "Successor # out of range for invoke!") ? static_cast<void> (0) : __assert_fail ("i < 2 && \"Successor # out of range for invoke!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3868, __PRETTY_FUNCTION__)); | |||
3869 | return i == 0 ? getNormalDest() : getUnwindDest(); | |||
3870 | } | |||
3871 | ||||
3872 | void setSuccessor(unsigned i, BasicBlock *NewSucc) { | |||
3873 | assert(i < 2 && "Successor # out of range for invoke!")((i < 2 && "Successor # out of range for invoke!") ? static_cast<void> (0) : __assert_fail ("i < 2 && \"Successor # out of range for invoke!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 3873, __PRETTY_FUNCTION__)); | |||
3874 | if (i == 0) | |||
3875 | setNormalDest(NewSucc); | |||
3876 | else | |||
3877 | setUnwindDest(NewSucc); | |||
3878 | } | |||
3879 | ||||
3880 | unsigned getNumSuccessors() const { return 2; } | |||
3881 | ||||
3882 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
3883 | static bool classof(const Instruction *I) { | |||
3884 | return (I->getOpcode() == Instruction::Invoke); | |||
3885 | } | |||
3886 | static bool classof(const Value *V) { | |||
3887 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
3888 | } | |||
3889 | ||||
3890 | private: | |||
3891 | ||||
3892 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
3893 | // method so that subclasses cannot accidentally use it. | |||
3894 | void setInstructionSubclassData(unsigned short D) { | |||
3895 | Instruction::setInstructionSubclassData(D); | |||
3896 | } | |||
3897 | }; | |||
3898 | ||||
3899 | InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, | |||
3900 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3901 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, | |||
3902 | const Twine &NameStr, Instruction *InsertBefore) | |||
3903 | : CallBase(Ty->getReturnType(), Instruction::Invoke, | |||
3904 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, | |||
3905 | InsertBefore) { | |||
3906 | init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr); | |||
3907 | } | |||
3908 | ||||
3909 | InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, | |||
3910 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3911 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, | |||
3912 | const Twine &NameStr, BasicBlock *InsertAtEnd) | |||
3913 | : CallBase(Ty->getReturnType(), Instruction::Invoke, | |||
3914 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, | |||
3915 | InsertAtEnd) { | |||
3916 | init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr); | |||
3917 | } | |||
3918 | ||||
3919 | //===----------------------------------------------------------------------===// | |||
3920 | // CallBrInst Class | |||
3921 | //===----------------------------------------------------------------------===// | |||
3922 | ||||
3923 | /// CallBr instruction, tracking function calls that may not return control but | |||
3924 | /// instead transfer it to a third location. The SubclassData field is used to | |||
3925 | /// hold the calling convention of the call. | |||
3926 | /// | |||
3927 | class CallBrInst : public CallBase { | |||
3928 | ||||
3929 | unsigned NumIndirectDests; | |||
3930 | ||||
3931 | CallBrInst(const CallBrInst &BI); | |||
3932 | ||||
3933 | /// Construct a CallBrInst given a range of arguments. | |||
3934 | /// | |||
3935 | /// Construct a CallBrInst from a range of arguments | |||
3936 | inline CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, | |||
3937 | ArrayRef<BasicBlock *> IndirectDests, | |||
3938 | ArrayRef<Value *> Args, | |||
3939 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, | |||
3940 | const Twine &NameStr, Instruction *InsertBefore); | |||
3941 | ||||
3942 | inline CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, | |||
3943 | ArrayRef<BasicBlock *> IndirectDests, | |||
3944 | ArrayRef<Value *> Args, | |||
3945 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, | |||
3946 | const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
3947 | ||||
3948 | void init(FunctionType *FTy, Value *Func, BasicBlock *DefaultDest, | |||
3949 | ArrayRef<BasicBlock *> IndirectDests, ArrayRef<Value *> Args, | |||
3950 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr); | |||
3951 | ||||
3952 | /// Should the Indirect Destinations change, scan + update the Arg list. | |||
3953 | void updateArgBlockAddresses(unsigned i, BasicBlock *B); | |||
3954 | ||||
3955 | /// Compute the number of operands to allocate. | |||
3956 | static int ComputeNumOperands(int NumArgs, int NumIndirectDests, | |||
3957 | int NumBundleInputs = 0) { | |||
3958 | // We need one operand for the called function, plus our extra operands and | |||
3959 | // the input operand counts provided. | |||
3960 | return 2 + NumIndirectDests + NumArgs + NumBundleInputs; | |||
3961 | } | |||
3962 | ||||
3963 | protected: | |||
3964 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
3965 | friend class Instruction; | |||
3966 | ||||
3967 | CallBrInst *cloneImpl() const; | |||
3968 | ||||
3969 | public: | |||
3970 | static CallBrInst *Create(FunctionType *Ty, Value *Func, | |||
3971 | BasicBlock *DefaultDest, | |||
3972 | ArrayRef<BasicBlock *> IndirectDests, | |||
3973 | ArrayRef<Value *> Args, const Twine &NameStr, | |||
3974 | Instruction *InsertBefore = nullptr) { | |||
3975 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size()); | |||
3976 | return new (NumOperands) | |||
3977 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, None, | |||
3978 | NumOperands, NameStr, InsertBefore); | |||
3979 | } | |||
3980 | ||||
3981 | static CallBrInst *Create(FunctionType *Ty, Value *Func, | |||
3982 | BasicBlock *DefaultDest, | |||
3983 | ArrayRef<BasicBlock *> IndirectDests, | |||
3984 | ArrayRef<Value *> Args, | |||
3985 | ArrayRef<OperandBundleDef> Bundles = None, | |||
3986 | const Twine &NameStr = "", | |||
3987 | Instruction *InsertBefore = nullptr) { | |||
3988 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size(), | |||
3989 | CountBundleInputs(Bundles)); | |||
3990 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); | |||
3991 | ||||
3992 | return new (NumOperands, DescriptorBytes) | |||
3993 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, | |||
3994 | NumOperands, NameStr, InsertBefore); | |||
3995 | } | |||
3996 | ||||
3997 | static CallBrInst *Create(FunctionType *Ty, Value *Func, | |||
3998 | BasicBlock *DefaultDest, | |||
3999 | ArrayRef<BasicBlock *> IndirectDests, | |||
4000 | ArrayRef<Value *> Args, const Twine &NameStr, | |||
4001 | BasicBlock *InsertAtEnd) { | |||
4002 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size()); | |||
4003 | return new (NumOperands) | |||
4004 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, None, | |||
4005 | NumOperands, NameStr, InsertAtEnd); | |||
4006 | } | |||
4007 | ||||
4008 | static CallBrInst *Create(FunctionType *Ty, Value *Func, | |||
4009 | BasicBlock *DefaultDest, | |||
4010 | ArrayRef<BasicBlock *> IndirectDests, | |||
4011 | ArrayRef<Value *> Args, | |||
4012 | ArrayRef<OperandBundleDef> Bundles, | |||
4013 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
4014 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size(), | |||
4015 | CountBundleInputs(Bundles)); | |||
4016 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); | |||
4017 | ||||
4018 | return new (NumOperands, DescriptorBytes) | |||
4019 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, | |||
4020 | NumOperands, NameStr, InsertAtEnd); | |||
4021 | } | |||
4022 | ||||
4023 | static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest, | |||
4024 | ArrayRef<BasicBlock *> IndirectDests, | |||
4025 | ArrayRef<Value *> Args, const Twine &NameStr, | |||
4026 | Instruction *InsertBefore = nullptr) { | |||
4027 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, | |||
4028 | IndirectDests, Args, NameStr, InsertBefore); | |||
4029 | } | |||
4030 | ||||
4031 | static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest, | |||
4032 | ArrayRef<BasicBlock *> IndirectDests, | |||
4033 | ArrayRef<Value *> Args, | |||
4034 | ArrayRef<OperandBundleDef> Bundles = None, | |||
4035 | const Twine &NameStr = "", | |||
4036 | Instruction *InsertBefore = nullptr) { | |||
4037 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, | |||
4038 | IndirectDests, Args, Bundles, NameStr, InsertBefore); | |||
4039 | } | |||
4040 | ||||
4041 | static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest, | |||
4042 | ArrayRef<BasicBlock *> IndirectDests, | |||
4043 | ArrayRef<Value *> Args, const Twine &NameStr, | |||
4044 | BasicBlock *InsertAtEnd) { | |||
4045 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, | |||
4046 | IndirectDests, Args, NameStr, InsertAtEnd); | |||
4047 | } | |||
4048 | ||||
4049 | static CallBrInst *Create(FunctionCallee Func, | |||
4050 | BasicBlock *DefaultDest, | |||
4051 | ArrayRef<BasicBlock *> IndirectDests, | |||
4052 | ArrayRef<Value *> Args, | |||
4053 | ArrayRef<OperandBundleDef> Bundles, | |||
4054 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
4055 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, | |||
4056 | IndirectDests, Args, Bundles, NameStr, InsertAtEnd); | |||
4057 | } | |||
4058 | ||||
4059 | /// Create a clone of \p CBI with a different set of operand bundles and | |||
4060 | /// insert it before \p InsertPt. | |||
4061 | /// | |||
4062 | /// The returned callbr instruction is identical to \p CBI in every way | |||
4063 | /// except that the operand bundles for the new instruction are set to the | |||
4064 | /// operand bundles in \p Bundles. | |||
4065 | static CallBrInst *Create(CallBrInst *CBI, | |||
4066 | ArrayRef<OperandBundleDef> Bundles, | |||
4067 | Instruction *InsertPt = nullptr); | |||
4068 | ||||
4069 | /// Return the number of callbr indirect dest labels. | |||
4070 | /// | |||
4071 | unsigned getNumIndirectDests() const { return NumIndirectDests; } | |||
4072 | ||||
4073 | /// getIndirectDestLabel - Return the i-th indirect dest label. | |||
4074 | /// | |||
4075 | Value *getIndirectDestLabel(unsigned i) const { | |||
4076 | assert(i < getNumIndirectDests() && "Out of bounds!")((i < getNumIndirectDests() && "Out of bounds!") ? static_cast<void> (0) : __assert_fail ("i < getNumIndirectDests() && \"Out of bounds!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4076, __PRETTY_FUNCTION__)); | |||
4077 | return getOperand(i + getNumArgOperands() + getNumTotalBundleOperands() + | |||
4078 | 1); | |||
4079 | } | |||
4080 | ||||
4081 | Value *getIndirectDestLabelUse(unsigned i) const { | |||
4082 | assert(i < getNumIndirectDests() && "Out of bounds!")((i < getNumIndirectDests() && "Out of bounds!") ? static_cast<void> (0) : __assert_fail ("i < getNumIndirectDests() && \"Out of bounds!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4082, __PRETTY_FUNCTION__)); | |||
4083 | return getOperandUse(i + getNumArgOperands() + getNumTotalBundleOperands() + | |||
4084 | 1); | |||
4085 | } | |||
4086 | ||||
4087 | // Return the destination basic blocks... | |||
4088 | BasicBlock *getDefaultDest() const { | |||
4089 | return cast<BasicBlock>(*(&Op<-1>() - getNumIndirectDests() - 1)); | |||
4090 | } | |||
4091 | BasicBlock *getIndirectDest(unsigned i) const { | |||
4092 | return cast_or_null<BasicBlock>(*(&Op<-1>() - getNumIndirectDests() + i)); | |||
4093 | } | |||
4094 | SmallVector<BasicBlock *, 16> getIndirectDests() const { | |||
4095 | SmallVector<BasicBlock *, 16> IndirectDests; | |||
4096 | for (unsigned i = 0, e = getNumIndirectDests(); i < e; ++i) | |||
4097 | IndirectDests.push_back(getIndirectDest(i)); | |||
4098 | return IndirectDests; | |||
4099 | } | |||
4100 | void setDefaultDest(BasicBlock *B) { | |||
4101 | *(&Op<-1>() - getNumIndirectDests() - 1) = reinterpret_cast<Value *>(B); | |||
4102 | } | |||
4103 | void setIndirectDest(unsigned i, BasicBlock *B) { | |||
4104 | updateArgBlockAddresses(i, B); | |||
4105 | *(&Op<-1>() - getNumIndirectDests() + i) = reinterpret_cast<Value *>(B); | |||
4106 | } | |||
4107 | ||||
4108 | BasicBlock *getSuccessor(unsigned i) const { | |||
4109 | assert(i < getNumSuccessors() + 1 &&((i < getNumSuccessors() + 1 && "Successor # out of range for callbr!" ) ? static_cast<void> (0) : __assert_fail ("i < getNumSuccessors() + 1 && \"Successor # out of range for callbr!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4110, __PRETTY_FUNCTION__)) | |||
4110 | "Successor # out of range for callbr!")((i < getNumSuccessors() + 1 && "Successor # out of range for callbr!" ) ? static_cast<void> (0) : __assert_fail ("i < getNumSuccessors() + 1 && \"Successor # out of range for callbr!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4110, __PRETTY_FUNCTION__)); | |||
4111 | return i == 0 ? getDefaultDest() : getIndirectDest(i - 1); | |||
4112 | } | |||
4113 | ||||
4114 | void setSuccessor(unsigned i, BasicBlock *NewSucc) { | |||
4115 | assert(i < getNumIndirectDests() + 1 &&((i < getNumIndirectDests() + 1 && "Successor # out of range for callbr!" ) ? static_cast<void> (0) : __assert_fail ("i < getNumIndirectDests() + 1 && \"Successor # out of range for callbr!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4116, __PRETTY_FUNCTION__)) | |||
4116 | "Successor # out of range for callbr!")((i < getNumIndirectDests() + 1 && "Successor # out of range for callbr!" ) ? static_cast<void> (0) : __assert_fail ("i < getNumIndirectDests() + 1 && \"Successor # out of range for callbr!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4116, __PRETTY_FUNCTION__)); | |||
4117 | return i == 0 ? setDefaultDest(NewSucc) : setIndirectDest(i - 1, NewSucc); | |||
4118 | } | |||
4119 | ||||
4120 | unsigned getNumSuccessors() const { return getNumIndirectDests() + 1; } | |||
4121 | ||||
4122 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4123 | static bool classof(const Instruction *I) { | |||
4124 | return (I->getOpcode() == Instruction::CallBr); | |||
4125 | } | |||
4126 | static bool classof(const Value *V) { | |||
4127 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4128 | } | |||
4129 | ||||
4130 | private: | |||
4131 | ||||
4132 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
4133 | // method so that subclasses cannot accidentally use it. | |||
4134 | void setInstructionSubclassData(unsigned short D) { | |||
4135 | Instruction::setInstructionSubclassData(D); | |||
4136 | } | |||
4137 | }; | |||
4138 | ||||
4139 | CallBrInst::CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, | |||
4140 | ArrayRef<BasicBlock *> IndirectDests, | |||
4141 | ArrayRef<Value *> Args, | |||
4142 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, | |||
4143 | const Twine &NameStr, Instruction *InsertBefore) | |||
4144 | : CallBase(Ty->getReturnType(), Instruction::CallBr, | |||
4145 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, | |||
4146 | InsertBefore) { | |||
4147 | init(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, NameStr); | |||
4148 | } | |||
4149 | ||||
4150 | CallBrInst::CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, | |||
4151 | ArrayRef<BasicBlock *> IndirectDests, | |||
4152 | ArrayRef<Value *> Args, | |||
4153 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, | |||
4154 | const Twine &NameStr, BasicBlock *InsertAtEnd) | |||
4155 | : CallBase(Ty->getReturnType(), Instruction::CallBr, | |||
4156 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, | |||
4157 | InsertAtEnd) { | |||
4158 | init(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, NameStr); | |||
4159 | } | |||
4160 | ||||
4161 | //===----------------------------------------------------------------------===// | |||
4162 | // ResumeInst Class | |||
4163 | //===----------------------------------------------------------------------===// | |||
4164 | ||||
4165 | //===--------------------------------------------------------------------------- | |||
4166 | /// Resume the propagation of an exception. | |||
4167 | /// | |||
4168 | class ResumeInst : public Instruction { | |||
4169 | ResumeInst(const ResumeInst &RI); | |||
4170 | ||||
4171 | explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr); | |||
4172 | ResumeInst(Value *Exn, BasicBlock *InsertAtEnd); | |||
4173 | ||||
4174 | protected: | |||
4175 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4176 | friend class Instruction; | |||
4177 | ||||
4178 | ResumeInst *cloneImpl() const; | |||
4179 | ||||
4180 | public: | |||
4181 | static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) { | |||
4182 | return new(1) ResumeInst(Exn, InsertBefore); | |||
4183 | } | |||
4184 | ||||
4185 | static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) { | |||
4186 | return new(1) ResumeInst(Exn, InsertAtEnd); | |||
4187 | } | |||
4188 | ||||
4189 | /// Provide fast operand accessors | |||
4190 | 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; | |||
4191 | ||||
4192 | /// Convenience accessor. | |||
4193 | Value *getValue() const { return Op<0>(); } | |||
4194 | ||||
4195 | unsigned getNumSuccessors() const { return 0; } | |||
4196 | ||||
4197 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4198 | static bool classof(const Instruction *I) { | |||
4199 | return I->getOpcode() == Instruction::Resume; | |||
4200 | } | |||
4201 | static bool classof(const Value *V) { | |||
4202 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4203 | } | |||
4204 | ||||
4205 | private: | |||
4206 | BasicBlock *getSuccessor(unsigned idx) const { | |||
4207 | llvm_unreachable("ResumeInst has no successors!")::llvm::llvm_unreachable_internal("ResumeInst has no successors!" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4207); | |||
4208 | } | |||
4209 | ||||
4210 | void setSuccessor(unsigned idx, BasicBlock *NewSucc) { | |||
4211 | llvm_unreachable("ResumeInst has no successors!")::llvm::llvm_unreachable_internal("ResumeInst has no successors!" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4211); | |||
4212 | } | |||
4213 | }; | |||
4214 | ||||
4215 | template <> | |||
4216 | struct OperandTraits<ResumeInst> : | |||
4217 | public FixedNumOperandTraits<ResumeInst, 1> { | |||
4218 | }; | |||
4219 | ||||
4220 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)ResumeInst::op_iterator ResumeInst::op_begin() { return OperandTraits <ResumeInst>::op_begin(this); } ResumeInst::const_op_iterator ResumeInst::op_begin() const { return OperandTraits<ResumeInst >::op_begin(const_cast<ResumeInst*>(this)); } ResumeInst ::op_iterator ResumeInst::op_end() { return OperandTraits< ResumeInst>::op_end(this); } ResumeInst::const_op_iterator ResumeInst::op_end() const { return OperandTraits<ResumeInst >::op_end(const_cast<ResumeInst*>(this)); } Value *ResumeInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<ResumeInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<ResumeInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4220, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<ResumeInst>::op_begin(const_cast<ResumeInst *>(this))[i_nocapture].get()); } void ResumeInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<ResumeInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<ResumeInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4220, __PRETTY_FUNCTION__)); OperandTraits<ResumeInst> ::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned ResumeInst ::getNumOperands() const { return OperandTraits<ResumeInst >::operands(this); } template <int Idx_nocapture> Use &ResumeInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & ResumeInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } | |||
4221 | ||||
4222 | //===----------------------------------------------------------------------===// | |||
4223 | // CatchSwitchInst Class | |||
4224 | //===----------------------------------------------------------------------===// | |||
4225 | class CatchSwitchInst : public Instruction { | |||
4226 | /// The number of operands actually allocated. NumOperands is | |||
4227 | /// the number actually in use. | |||
4228 | unsigned ReservedSpace; | |||
4229 | ||||
4230 | // Operand[0] = Outer scope | |||
4231 | // Operand[1] = Unwind block destination | |||
4232 | // Operand[n] = BasicBlock to go to on match | |||
4233 | CatchSwitchInst(const CatchSwitchInst &CSI); | |||
4234 | ||||
4235 | /// Create a new switch instruction, specifying a | |||
4236 | /// default destination. The number of additional handlers can be specified | |||
4237 | /// here to make memory allocation more efficient. | |||
4238 | /// This constructor can also autoinsert before another instruction. | |||
4239 | CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, | |||
4240 | unsigned NumHandlers, const Twine &NameStr, | |||
4241 | Instruction *InsertBefore); | |||
4242 | ||||
4243 | /// Create a new switch instruction, specifying a | |||
4244 | /// default destination. The number of additional handlers can be specified | |||
4245 | /// here to make memory allocation more efficient. | |||
4246 | /// This constructor also autoinserts at the end of the specified BasicBlock. | |||
4247 | CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, | |||
4248 | unsigned NumHandlers, const Twine &NameStr, | |||
4249 | BasicBlock *InsertAtEnd); | |||
4250 | ||||
4251 | // allocate space for exactly zero operands | |||
4252 | void *operator new(size_t s) { return User::operator new(s); } | |||
4253 | ||||
4254 | void init(Value *ParentPad, BasicBlock *UnwindDest, unsigned NumReserved); | |||
4255 | void growOperands(unsigned Size); | |||
4256 | ||||
4257 | protected: | |||
4258 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4259 | friend class Instruction; | |||
4260 | ||||
4261 | CatchSwitchInst *cloneImpl() const; | |||
4262 | ||||
4263 | public: | |||
4264 | static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest, | |||
4265 | unsigned NumHandlers, | |||
4266 | const Twine &NameStr = "", | |||
4267 | Instruction *InsertBefore = nullptr) { | |||
4268 | return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr, | |||
4269 | InsertBefore); | |||
4270 | } | |||
4271 | ||||
4272 | static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest, | |||
4273 | unsigned NumHandlers, const Twine &NameStr, | |||
4274 | BasicBlock *InsertAtEnd) { | |||
4275 | return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr, | |||
4276 | InsertAtEnd); | |||
4277 | } | |||
4278 | ||||
4279 | /// Provide fast operand accessors | |||
4280 | 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; | |||
4281 | ||||
4282 | // Accessor Methods for CatchSwitch stmt | |||
4283 | Value *getParentPad() const { return getOperand(0); } | |||
4284 | void setParentPad(Value *ParentPad) { setOperand(0, ParentPad); } | |||
4285 | ||||
4286 | // Accessor Methods for CatchSwitch stmt | |||
4287 | bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; } | |||
4288 | bool unwindsToCaller() const { return !hasUnwindDest(); } | |||
4289 | BasicBlock *getUnwindDest() const { | |||
4290 | if (hasUnwindDest()) | |||
4291 | return cast<BasicBlock>(getOperand(1)); | |||
4292 | return nullptr; | |||
4293 | } | |||
4294 | void setUnwindDest(BasicBlock *UnwindDest) { | |||
4295 | assert(UnwindDest)((UnwindDest) ? static_cast<void> (0) : __assert_fail ( "UnwindDest", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4295, __PRETTY_FUNCTION__)); | |||
4296 | assert(hasUnwindDest())((hasUnwindDest()) ? static_cast<void> (0) : __assert_fail ("hasUnwindDest()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4296, __PRETTY_FUNCTION__)); | |||
4297 | setOperand(1, UnwindDest); | |||
4298 | } | |||
4299 | ||||
4300 | /// return the number of 'handlers' in this catchswitch | |||
4301 | /// instruction, except the default handler | |||
4302 | unsigned getNumHandlers() const { | |||
4303 | if (hasUnwindDest()) | |||
4304 | return getNumOperands() - 2; | |||
4305 | return getNumOperands() - 1; | |||
4306 | } | |||
4307 | ||||
4308 | private: | |||
4309 | static BasicBlock *handler_helper(Value *V) { return cast<BasicBlock>(V); } | |||
4310 | static const BasicBlock *handler_helper(const Value *V) { | |||
4311 | return cast<BasicBlock>(V); | |||
4312 | } | |||
4313 | ||||
4314 | public: | |||
4315 | using DerefFnTy = BasicBlock *(*)(Value *); | |||
4316 | using handler_iterator = mapped_iterator<op_iterator, DerefFnTy>; | |||
4317 | using handler_range = iterator_range<handler_iterator>; | |||
4318 | using ConstDerefFnTy = const BasicBlock *(*)(const Value *); | |||
4319 | using const_handler_iterator = | |||
4320 | mapped_iterator<const_op_iterator, ConstDerefFnTy>; | |||
4321 | using const_handler_range = iterator_range<const_handler_iterator>; | |||
4322 | ||||
4323 | /// Returns an iterator that points to the first handler in CatchSwitchInst. | |||
4324 | handler_iterator handler_begin() { | |||
4325 | op_iterator It = op_begin() + 1; | |||
4326 | if (hasUnwindDest()) | |||
4327 | ++It; | |||
4328 | return handler_iterator(It, DerefFnTy(handler_helper)); | |||
4329 | } | |||
4330 | ||||
4331 | /// Returns an iterator that points to the first handler in the | |||
4332 | /// CatchSwitchInst. | |||
4333 | const_handler_iterator handler_begin() const { | |||
4334 | const_op_iterator It = op_begin() + 1; | |||
4335 | if (hasUnwindDest()) | |||
4336 | ++It; | |||
4337 | return const_handler_iterator(It, ConstDerefFnTy(handler_helper)); | |||
4338 | } | |||
4339 | ||||
4340 | /// Returns a read-only iterator that points one past the last | |||
4341 | /// handler in the CatchSwitchInst. | |||
4342 | handler_iterator handler_end() { | |||
4343 | return handler_iterator(op_end(), DerefFnTy(handler_helper)); | |||
4344 | } | |||
4345 | ||||
4346 | /// Returns an iterator that points one past the last handler in the | |||
4347 | /// CatchSwitchInst. | |||
4348 | const_handler_iterator handler_end() const { | |||
4349 | return const_handler_iterator(op_end(), ConstDerefFnTy(handler_helper)); | |||
4350 | } | |||
4351 | ||||
4352 | /// iteration adapter for range-for loops. | |||
4353 | handler_range handlers() { | |||
4354 | return make_range(handler_begin(), handler_end()); | |||
4355 | } | |||
4356 | ||||
4357 | /// iteration adapter for range-for loops. | |||
4358 | const_handler_range handlers() const { | |||
4359 | return make_range(handler_begin(), handler_end()); | |||
4360 | } | |||
4361 | ||||
4362 | /// Add an entry to the switch instruction... | |||
4363 | /// Note: | |||
4364 | /// This action invalidates handler_end(). Old handler_end() iterator will | |||
4365 | /// point to the added handler. | |||
4366 | void addHandler(BasicBlock *Dest); | |||
4367 | ||||
4368 | void removeHandler(handler_iterator HI); | |||
4369 | ||||
4370 | unsigned getNumSuccessors() const { return getNumOperands() - 1; } | |||
4371 | BasicBlock *getSuccessor(unsigned Idx) const { | |||
4372 | assert(Idx < getNumSuccessors() &&((Idx < getNumSuccessors() && "Successor # out of range for catchswitch!" ) ? static_cast<void> (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchswitch!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4373, __PRETTY_FUNCTION__)) | |||
4373 | "Successor # out of range for catchswitch!")((Idx < getNumSuccessors() && "Successor # out of range for catchswitch!" ) ? static_cast<void> (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchswitch!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4373, __PRETTY_FUNCTION__)); | |||
4374 | return cast<BasicBlock>(getOperand(Idx + 1)); | |||
4375 | } | |||
4376 | void setSuccessor(unsigned Idx, BasicBlock *NewSucc) { | |||
4377 | assert(Idx < getNumSuccessors() &&((Idx < getNumSuccessors() && "Successor # out of range for catchswitch!" ) ? static_cast<void> (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchswitch!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4378, __PRETTY_FUNCTION__)) | |||
4378 | "Successor # out of range for catchswitch!")((Idx < getNumSuccessors() && "Successor # out of range for catchswitch!" ) ? static_cast<void> (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchswitch!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4378, __PRETTY_FUNCTION__)); | |||
4379 | setOperand(Idx + 1, NewSucc); | |||
4380 | } | |||
4381 | ||||
4382 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4383 | static bool classof(const Instruction *I) { | |||
4384 | return I->getOpcode() == Instruction::CatchSwitch; | |||
4385 | } | |||
4386 | static bool classof(const Value *V) { | |||
4387 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4388 | } | |||
4389 | }; | |||
4390 | ||||
4391 | template <> | |||
4392 | struct OperandTraits<CatchSwitchInst> : public HungoffOperandTraits<2> {}; | |||
4393 | ||||
4394 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchSwitchInst, Value)CatchSwitchInst::op_iterator CatchSwitchInst::op_begin() { return OperandTraits<CatchSwitchInst>::op_begin(this); } CatchSwitchInst ::const_op_iterator CatchSwitchInst::op_begin() const { return OperandTraits<CatchSwitchInst>::op_begin(const_cast< CatchSwitchInst*>(this)); } CatchSwitchInst::op_iterator CatchSwitchInst ::op_end() { return OperandTraits<CatchSwitchInst>::op_end (this); } CatchSwitchInst::const_op_iterator CatchSwitchInst:: op_end() const { return OperandTraits<CatchSwitchInst>:: op_end(const_cast<CatchSwitchInst*>(this)); } Value *CatchSwitchInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<CatchSwitchInst>::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<CatchSwitchInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4394, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<CatchSwitchInst>::op_begin(const_cast< CatchSwitchInst*>(this))[i_nocapture].get()); } void CatchSwitchInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<CatchSwitchInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<CatchSwitchInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4394, __PRETTY_FUNCTION__)); OperandTraits<CatchSwitchInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned CatchSwitchInst::getNumOperands() const { return OperandTraits <CatchSwitchInst>::operands(this); } template <int Idx_nocapture > Use &CatchSwitchInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &CatchSwitchInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } | |||
4395 | ||||
4396 | //===----------------------------------------------------------------------===// | |||
4397 | // CleanupPadInst Class | |||
4398 | //===----------------------------------------------------------------------===// | |||
4399 | class CleanupPadInst : public FuncletPadInst { | |||
4400 | private: | |||
4401 | explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args, | |||
4402 | unsigned Values, const Twine &NameStr, | |||
4403 | Instruction *InsertBefore) | |||
4404 | : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values, | |||
4405 | NameStr, InsertBefore) {} | |||
4406 | explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args, | |||
4407 | unsigned Values, const Twine &NameStr, | |||
4408 | BasicBlock *InsertAtEnd) | |||
4409 | : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values, | |||
4410 | NameStr, InsertAtEnd) {} | |||
4411 | ||||
4412 | public: | |||
4413 | static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args = None, | |||
4414 | const Twine &NameStr = "", | |||
4415 | Instruction *InsertBefore = nullptr) { | |||
4416 | unsigned Values = 1 + Args.size(); | |||
4417 | return new (Values) | |||
4418 | CleanupPadInst(ParentPad, Args, Values, NameStr, InsertBefore); | |||
4419 | } | |||
4420 | ||||
4421 | static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args, | |||
4422 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
4423 | unsigned Values = 1 + Args.size(); | |||
4424 | return new (Values) | |||
4425 | CleanupPadInst(ParentPad, Args, Values, NameStr, InsertAtEnd); | |||
4426 | } | |||
4427 | ||||
4428 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4429 | static bool classof(const Instruction *I) { | |||
4430 | return I->getOpcode() == Instruction::CleanupPad; | |||
4431 | } | |||
4432 | static bool classof(const Value *V) { | |||
4433 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4434 | } | |||
4435 | }; | |||
4436 | ||||
4437 | //===----------------------------------------------------------------------===// | |||
4438 | // CatchPadInst Class | |||
4439 | //===----------------------------------------------------------------------===// | |||
4440 | class CatchPadInst : public FuncletPadInst { | |||
4441 | private: | |||
4442 | explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args, | |||
4443 | unsigned Values, const Twine &NameStr, | |||
4444 | Instruction *InsertBefore) | |||
4445 | : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values, | |||
4446 | NameStr, InsertBefore) {} | |||
4447 | explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args, | |||
4448 | unsigned Values, const Twine &NameStr, | |||
4449 | BasicBlock *InsertAtEnd) | |||
4450 | : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values, | |||
4451 | NameStr, InsertAtEnd) {} | |||
4452 | ||||
4453 | public: | |||
4454 | static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args, | |||
4455 | const Twine &NameStr = "", | |||
4456 | Instruction *InsertBefore = nullptr) { | |||
4457 | unsigned Values = 1 + Args.size(); | |||
4458 | return new (Values) | |||
4459 | CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertBefore); | |||
4460 | } | |||
4461 | ||||
4462 | static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args, | |||
4463 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
4464 | unsigned Values = 1 + Args.size(); | |||
4465 | return new (Values) | |||
4466 | CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertAtEnd); | |||
4467 | } | |||
4468 | ||||
4469 | /// Convenience accessors | |||
4470 | CatchSwitchInst *getCatchSwitch() const { | |||
4471 | return cast<CatchSwitchInst>(Op<-1>()); | |||
4472 | } | |||
4473 | void setCatchSwitch(Value *CatchSwitch) { | |||
4474 | assert(CatchSwitch)((CatchSwitch) ? static_cast<void> (0) : __assert_fail ( "CatchSwitch", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4474, __PRETTY_FUNCTION__)); | |||
4475 | Op<-1>() = CatchSwitch; | |||
4476 | } | |||
4477 | ||||
4478 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4479 | static bool classof(const Instruction *I) { | |||
4480 | return I->getOpcode() == Instruction::CatchPad; | |||
4481 | } | |||
4482 | static bool classof(const Value *V) { | |||
4483 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4484 | } | |||
4485 | }; | |||
4486 | ||||
4487 | //===----------------------------------------------------------------------===// | |||
4488 | // CatchReturnInst Class | |||
4489 | //===----------------------------------------------------------------------===// | |||
4490 | ||||
4491 | class CatchReturnInst : public Instruction { | |||
4492 | CatchReturnInst(const CatchReturnInst &RI); | |||
4493 | CatchReturnInst(Value *CatchPad, BasicBlock *BB, Instruction *InsertBefore); | |||
4494 | CatchReturnInst(Value *CatchPad, BasicBlock *BB, BasicBlock *InsertAtEnd); | |||
4495 | ||||
4496 | void init(Value *CatchPad, BasicBlock *BB); | |||
4497 | ||||
4498 | protected: | |||
4499 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4500 | friend class Instruction; | |||
4501 | ||||
4502 | CatchReturnInst *cloneImpl() const; | |||
4503 | ||||
4504 | public: | |||
4505 | static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB, | |||
4506 | Instruction *InsertBefore = nullptr) { | |||
4507 | assert(CatchPad)((CatchPad) ? static_cast<void> (0) : __assert_fail ("CatchPad" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4507, __PRETTY_FUNCTION__)); | |||
4508 | assert(BB)((BB) ? static_cast<void> (0) : __assert_fail ("BB", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4508, __PRETTY_FUNCTION__)); | |||
4509 | return new (2) CatchReturnInst(CatchPad, BB, InsertBefore); | |||
4510 | } | |||
4511 | ||||
4512 | static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB, | |||
4513 | BasicBlock *InsertAtEnd) { | |||
4514 | assert(CatchPad)((CatchPad) ? static_cast<void> (0) : __assert_fail ("CatchPad" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4514, __PRETTY_FUNCTION__)); | |||
4515 | assert(BB)((BB) ? static_cast<void> (0) : __assert_fail ("BB", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4515, __PRETTY_FUNCTION__)); | |||
4516 | return new (2) CatchReturnInst(CatchPad, BB, InsertAtEnd); | |||
4517 | } | |||
4518 | ||||
4519 | /// Provide fast operand accessors | |||
4520 | 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; | |||
4521 | ||||
4522 | /// Convenience accessors. | |||
4523 | CatchPadInst *getCatchPad() const { return cast<CatchPadInst>(Op<0>()); } | |||
4524 | void setCatchPad(CatchPadInst *CatchPad) { | |||
4525 | assert(CatchPad)((CatchPad) ? static_cast<void> (0) : __assert_fail ("CatchPad" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4525, __PRETTY_FUNCTION__)); | |||
4526 | Op<0>() = CatchPad; | |||
4527 | } | |||
4528 | ||||
4529 | BasicBlock *getSuccessor() const { return cast<BasicBlock>(Op<1>()); } | |||
4530 | void setSuccessor(BasicBlock *NewSucc) { | |||
4531 | assert(NewSucc)((NewSucc) ? static_cast<void> (0) : __assert_fail ("NewSucc" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4531, __PRETTY_FUNCTION__)); | |||
4532 | Op<1>() = NewSucc; | |||
4533 | } | |||
4534 | unsigned getNumSuccessors() const { return 1; } | |||
4535 | ||||
4536 | /// Get the parentPad of this catchret's catchpad's catchswitch. | |||
4537 | /// The successor block is implicitly a member of this funclet. | |||
4538 | Value *getCatchSwitchParentPad() const { | |||
4539 | return getCatchPad()->getCatchSwitch()->getParentPad(); | |||
4540 | } | |||
4541 | ||||
4542 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4543 | static bool classof(const Instruction *I) { | |||
4544 | return (I->getOpcode() == Instruction::CatchRet); | |||
4545 | } | |||
4546 | static bool classof(const Value *V) { | |||
4547 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4548 | } | |||
4549 | ||||
4550 | private: | |||
4551 | BasicBlock *getSuccessor(unsigned Idx) const { | |||
4552 | assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!")((Idx < getNumSuccessors() && "Successor # out of range for catchret!" ) ? static_cast<void> (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchret!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4552, __PRETTY_FUNCTION__)); | |||
4553 | return getSuccessor(); | |||
4554 | } | |||
4555 | ||||
4556 | void setSuccessor(unsigned Idx, BasicBlock *B) { | |||
4557 | assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!")((Idx < getNumSuccessors() && "Successor # out of range for catchret!" ) ? static_cast<void> (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchret!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4557, __PRETTY_FUNCTION__)); | |||
4558 | setSuccessor(B); | |||
4559 | } | |||
4560 | }; | |||
4561 | ||||
4562 | template <> | |||
4563 | struct OperandTraits<CatchReturnInst> | |||
4564 | : public FixedNumOperandTraits<CatchReturnInst, 2> {}; | |||
4565 | ||||
4566 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchReturnInst, Value)CatchReturnInst::op_iterator CatchReturnInst::op_begin() { return OperandTraits<CatchReturnInst>::op_begin(this); } CatchReturnInst ::const_op_iterator CatchReturnInst::op_begin() const { return OperandTraits<CatchReturnInst>::op_begin(const_cast< CatchReturnInst*>(this)); } CatchReturnInst::op_iterator CatchReturnInst ::op_end() { return OperandTraits<CatchReturnInst>::op_end (this); } CatchReturnInst::const_op_iterator CatchReturnInst:: op_end() const { return OperandTraits<CatchReturnInst>:: op_end(const_cast<CatchReturnInst*>(this)); } Value *CatchReturnInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<CatchReturnInst>::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<CatchReturnInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4566, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<CatchReturnInst>::op_begin(const_cast< CatchReturnInst*>(this))[i_nocapture].get()); } void CatchReturnInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<CatchReturnInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<CatchReturnInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4566, __PRETTY_FUNCTION__)); OperandTraits<CatchReturnInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned CatchReturnInst::getNumOperands() const { return OperandTraits <CatchReturnInst>::operands(this); } template <int Idx_nocapture > Use &CatchReturnInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &CatchReturnInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } | |||
4567 | ||||
4568 | //===----------------------------------------------------------------------===// | |||
4569 | // CleanupReturnInst Class | |||
4570 | //===----------------------------------------------------------------------===// | |||
4571 | ||||
4572 | class CleanupReturnInst : public Instruction { | |||
4573 | private: | |||
4574 | CleanupReturnInst(const CleanupReturnInst &RI); | |||
4575 | CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values, | |||
4576 | Instruction *InsertBefore = nullptr); | |||
4577 | CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values, | |||
4578 | BasicBlock *InsertAtEnd); | |||
4579 | ||||
4580 | void init(Value *CleanupPad, BasicBlock *UnwindBB); | |||
4581 | ||||
4582 | protected: | |||
4583 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4584 | friend class Instruction; | |||
4585 | ||||
4586 | CleanupReturnInst *cloneImpl() const; | |||
4587 | ||||
4588 | public: | |||
4589 | static CleanupReturnInst *Create(Value *CleanupPad, | |||
4590 | BasicBlock *UnwindBB = nullptr, | |||
4591 | Instruction *InsertBefore = nullptr) { | |||
4592 | assert(CleanupPad)((CleanupPad) ? static_cast<void> (0) : __assert_fail ( "CleanupPad", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4592, __PRETTY_FUNCTION__)); | |||
4593 | unsigned Values = 1; | |||
4594 | if (UnwindBB) | |||
4595 | ++Values; | |||
4596 | return new (Values) | |||
4597 | CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertBefore); | |||
4598 | } | |||
4599 | ||||
4600 | static CleanupReturnInst *Create(Value *CleanupPad, BasicBlock *UnwindBB, | |||
4601 | BasicBlock *InsertAtEnd) { | |||
4602 | assert(CleanupPad)((CleanupPad) ? static_cast<void> (0) : __assert_fail ( "CleanupPad", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4602, __PRETTY_FUNCTION__)); | |||
4603 | unsigned Values = 1; | |||
4604 | if (UnwindBB) | |||
4605 | ++Values; | |||
4606 | return new (Values) | |||
4607 | CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertAtEnd); | |||
4608 | } | |||
4609 | ||||
4610 | /// Provide fast operand accessors | |||
4611 | 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; | |||
4612 | ||||
4613 | bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; } | |||
4614 | bool unwindsToCaller() const { return !hasUnwindDest(); } | |||
4615 | ||||
4616 | /// Convenience accessor. | |||
4617 | CleanupPadInst *getCleanupPad() const { | |||
4618 | return cast<CleanupPadInst>(Op<0>()); | |||
4619 | } | |||
4620 | void setCleanupPad(CleanupPadInst *CleanupPad) { | |||
4621 | assert(CleanupPad)((CleanupPad) ? static_cast<void> (0) : __assert_fail ( "CleanupPad", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4621, __PRETTY_FUNCTION__)); | |||
4622 | Op<0>() = CleanupPad; | |||
4623 | } | |||
4624 | ||||
4625 | unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; } | |||
4626 | ||||
4627 | BasicBlock *getUnwindDest() const { | |||
4628 | return hasUnwindDest() ? cast<BasicBlock>(Op<1>()) : nullptr; | |||
4629 | } | |||
4630 | void setUnwindDest(BasicBlock *NewDest) { | |||
4631 | assert(NewDest)((NewDest) ? static_cast<void> (0) : __assert_fail ("NewDest" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4631, __PRETTY_FUNCTION__)); | |||
4632 | assert(hasUnwindDest())((hasUnwindDest()) ? static_cast<void> (0) : __assert_fail ("hasUnwindDest()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4632, __PRETTY_FUNCTION__)); | |||
4633 | Op<1>() = NewDest; | |||
4634 | } | |||
4635 | ||||
4636 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4637 | static bool classof(const Instruction *I) { | |||
4638 | return (I->getOpcode() == Instruction::CleanupRet); | |||
4639 | } | |||
4640 | static bool classof(const Value *V) { | |||
4641 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4642 | } | |||
4643 | ||||
4644 | private: | |||
4645 | BasicBlock *getSuccessor(unsigned Idx) const { | |||
4646 | assert(Idx == 0)((Idx == 0) ? static_cast<void> (0) : __assert_fail ("Idx == 0" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4646, __PRETTY_FUNCTION__)); | |||
4647 | return getUnwindDest(); | |||
4648 | } | |||
4649 | ||||
4650 | void setSuccessor(unsigned Idx, BasicBlock *B) { | |||
4651 | assert(Idx == 0)((Idx == 0) ? static_cast<void> (0) : __assert_fail ("Idx == 0" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4651, __PRETTY_FUNCTION__)); | |||
4652 | setUnwindDest(B); | |||
4653 | } | |||
4654 | ||||
4655 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
4656 | // method so that subclasses cannot accidentally use it. | |||
4657 | void setInstructionSubclassData(unsigned short D) { | |||
4658 | Instruction::setInstructionSubclassData(D); | |||
4659 | } | |||
4660 | }; | |||
4661 | ||||
4662 | template <> | |||
4663 | struct OperandTraits<CleanupReturnInst> | |||
4664 | : public VariadicOperandTraits<CleanupReturnInst, /*MINARITY=*/1> {}; | |||
4665 | ||||
4666 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CleanupReturnInst, Value)CleanupReturnInst::op_iterator CleanupReturnInst::op_begin() { return OperandTraits<CleanupReturnInst>::op_begin(this ); } CleanupReturnInst::const_op_iterator CleanupReturnInst:: op_begin() const { return OperandTraits<CleanupReturnInst> ::op_begin(const_cast<CleanupReturnInst*>(this)); } CleanupReturnInst ::op_iterator CleanupReturnInst::op_end() { return OperandTraits <CleanupReturnInst>::op_end(this); } CleanupReturnInst:: const_op_iterator CleanupReturnInst::op_end() const { return OperandTraits <CleanupReturnInst>::op_end(const_cast<CleanupReturnInst *>(this)); } Value *CleanupReturnInst::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<CleanupReturnInst >::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<CleanupReturnInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4666, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<CleanupReturnInst>::op_begin(const_cast <CleanupReturnInst*>(this))[i_nocapture].get()); } void CleanupReturnInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((i_nocapture < OperandTraits<CleanupReturnInst> ::operands(this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<CleanupReturnInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4666, __PRETTY_FUNCTION__)); OperandTraits<CleanupReturnInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned CleanupReturnInst::getNumOperands() const { return OperandTraits <CleanupReturnInst>::operands(this); } template <int Idx_nocapture> Use &CleanupReturnInst::Op() { return this ->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture > const Use &CleanupReturnInst::Op() const { return this ->OpFrom<Idx_nocapture>(this); } | |||
4667 | ||||
4668 | //===----------------------------------------------------------------------===// | |||
4669 | // UnreachableInst Class | |||
4670 | //===----------------------------------------------------------------------===// | |||
4671 | ||||
4672 | //===--------------------------------------------------------------------------- | |||
4673 | /// This function has undefined behavior. In particular, the | |||
4674 | /// presence of this instruction indicates some higher level knowledge that the | |||
4675 | /// end of the block cannot be reached. | |||
4676 | /// | |||
4677 | class UnreachableInst : public Instruction { | |||
4678 | protected: | |||
4679 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4680 | friend class Instruction; | |||
4681 | ||||
4682 | UnreachableInst *cloneImpl() const; | |||
4683 | ||||
4684 | public: | |||
4685 | explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr); | |||
4686 | explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd); | |||
4687 | ||||
4688 | // allocate space for exactly zero operands | |||
4689 | void *operator new(size_t s) { | |||
4690 | return User::operator new(s, 0); | |||
4691 | } | |||
4692 | ||||
4693 | unsigned getNumSuccessors() const { return 0; } | |||
4694 | ||||
4695 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4696 | static bool classof(const Instruction *I) { | |||
4697 | return I->getOpcode() == Instruction::Unreachable; | |||
4698 | } | |||
4699 | static bool classof(const Value *V) { | |||
4700 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4701 | } | |||
4702 | ||||
4703 | private: | |||
4704 | BasicBlock *getSuccessor(unsigned idx) const { | |||
4705 | llvm_unreachable("UnreachableInst has no successors!")::llvm::llvm_unreachable_internal("UnreachableInst has no successors!" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4705); | |||
4706 | } | |||
4707 | ||||
4708 | void setSuccessor(unsigned idx, BasicBlock *B) { | |||
4709 | llvm_unreachable("UnreachableInst has no successors!")::llvm::llvm_unreachable_internal("UnreachableInst has no successors!" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 4709); | |||
4710 | } | |||
4711 | }; | |||
4712 | ||||
4713 | //===----------------------------------------------------------------------===// | |||
4714 | // TruncInst Class | |||
4715 | //===----------------------------------------------------------------------===// | |||
4716 | ||||
4717 | /// This class represents a truncation of integer types. | |||
4718 | class TruncInst : public CastInst { | |||
4719 | protected: | |||
4720 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4721 | friend class Instruction; | |||
4722 | ||||
4723 | /// Clone an identical TruncInst | |||
4724 | TruncInst *cloneImpl() const; | |||
4725 | ||||
4726 | public: | |||
4727 | /// Constructor with insert-before-instruction semantics | |||
4728 | TruncInst( | |||
4729 | Value *S, ///< The value to be truncated | |||
4730 | Type *Ty, ///< The (smaller) type to truncate to | |||
4731 | const Twine &NameStr = "", ///< A name for the new instruction | |||
4732 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
4733 | ); | |||
4734 | ||||
4735 | /// Constructor with insert-at-end-of-block semantics | |||
4736 | TruncInst( | |||
4737 | Value *S, ///< The value to be truncated | |||
4738 | Type *Ty, ///< The (smaller) type to truncate to | |||
4739 | const Twine &NameStr, ///< A name for the new instruction | |||
4740 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
4741 | ); | |||
4742 | ||||
4743 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4744 | static bool classof(const Instruction *I) { | |||
4745 | return I->getOpcode() == Trunc; | |||
4746 | } | |||
4747 | static bool classof(const Value *V) { | |||
4748 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4749 | } | |||
4750 | }; | |||
4751 | ||||
4752 | //===----------------------------------------------------------------------===// | |||
4753 | // ZExtInst Class | |||
4754 | //===----------------------------------------------------------------------===// | |||
4755 | ||||
4756 | /// This class represents zero extension of integer types. | |||
4757 | class ZExtInst : public CastInst { | |||
4758 | protected: | |||
4759 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4760 | friend class Instruction; | |||
4761 | ||||
4762 | /// Clone an identical ZExtInst | |||
4763 | ZExtInst *cloneImpl() const; | |||
4764 | ||||
4765 | public: | |||
4766 | /// Constructor with insert-before-instruction semantics | |||
4767 | ZExtInst( | |||
4768 | Value *S, ///< The value to be zero extended | |||
4769 | Type *Ty, ///< The type to zero extend to | |||
4770 | const Twine &NameStr = "", ///< A name for the new instruction | |||
4771 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
4772 | ); | |||
4773 | ||||
4774 | /// Constructor with insert-at-end semantics. | |||
4775 | ZExtInst( | |||
4776 | Value *S, ///< The value to be zero extended | |||
4777 | Type *Ty, ///< The type to zero extend to | |||
4778 | const Twine &NameStr, ///< A name for the new instruction | |||
4779 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
4780 | ); | |||
4781 | ||||
4782 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4783 | static bool classof(const Instruction *I) { | |||
4784 | return I->getOpcode() == ZExt; | |||
4785 | } | |||
4786 | static bool classof(const Value *V) { | |||
4787 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4788 | } | |||
4789 | }; | |||
4790 | ||||
4791 | //===----------------------------------------------------------------------===// | |||
4792 | // SExtInst Class | |||
4793 | //===----------------------------------------------------------------------===// | |||
4794 | ||||
4795 | /// This class represents a sign extension of integer types. | |||
4796 | class SExtInst : public CastInst { | |||
4797 | protected: | |||
4798 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4799 | friend class Instruction; | |||
4800 | ||||
4801 | /// Clone an identical SExtInst | |||
4802 | SExtInst *cloneImpl() const; | |||
4803 | ||||
4804 | public: | |||
4805 | /// Constructor with insert-before-instruction semantics | |||
4806 | SExtInst( | |||
4807 | Value *S, ///< The value to be sign extended | |||
4808 | Type *Ty, ///< The type to sign extend to | |||
4809 | const Twine &NameStr = "", ///< A name for the new instruction | |||
4810 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
4811 | ); | |||
4812 | ||||
4813 | /// Constructor with insert-at-end-of-block semantics | |||
4814 | SExtInst( | |||
4815 | Value *S, ///< The value to be sign extended | |||
4816 | Type *Ty, ///< The type to sign extend to | |||
4817 | const Twine &NameStr, ///< A name for the new instruction | |||
4818 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
4819 | ); | |||
4820 | ||||
4821 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4822 | static bool classof(const Instruction *I) { | |||
4823 | return I->getOpcode() == SExt; | |||
4824 | } | |||
4825 | static bool classof(const Value *V) { | |||
4826 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4827 | } | |||
4828 | }; | |||
4829 | ||||
4830 | //===----------------------------------------------------------------------===// | |||
4831 | // FPTruncInst Class | |||
4832 | //===----------------------------------------------------------------------===// | |||
4833 | ||||
4834 | /// This class represents a truncation of floating point types. | |||
4835 | class FPTruncInst : public CastInst { | |||
4836 | protected: | |||
4837 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4838 | friend class Instruction; | |||
4839 | ||||
4840 | /// Clone an identical FPTruncInst | |||
4841 | FPTruncInst *cloneImpl() const; | |||
4842 | ||||
4843 | public: | |||
4844 | /// Constructor with insert-before-instruction semantics | |||
4845 | FPTruncInst( | |||
4846 | Value *S, ///< The value to be truncated | |||
4847 | Type *Ty, ///< The type to truncate to | |||
4848 | const Twine &NameStr = "", ///< A name for the new instruction | |||
4849 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
4850 | ); | |||
4851 | ||||
4852 | /// Constructor with insert-before-instruction semantics | |||
4853 | FPTruncInst( | |||
4854 | Value *S, ///< The value to be truncated | |||
4855 | Type *Ty, ///< The type to truncate to | |||
4856 | const Twine &NameStr, ///< A name for the new instruction | |||
4857 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
4858 | ); | |||
4859 | ||||
4860 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4861 | static bool classof(const Instruction *I) { | |||
4862 | return I->getOpcode() == FPTrunc; | |||
4863 | } | |||
4864 | static bool classof(const Value *V) { | |||
4865 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4866 | } | |||
4867 | }; | |||
4868 | ||||
4869 | //===----------------------------------------------------------------------===// | |||
4870 | // FPExtInst Class | |||
4871 | //===----------------------------------------------------------------------===// | |||
4872 | ||||
4873 | /// This class represents an extension of floating point types. | |||
4874 | class FPExtInst : public CastInst { | |||
4875 | protected: | |||
4876 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4877 | friend class Instruction; | |||
4878 | ||||
4879 | /// Clone an identical FPExtInst | |||
4880 | FPExtInst *cloneImpl() const; | |||
4881 | ||||
4882 | public: | |||
4883 | /// Constructor with insert-before-instruction semantics | |||
4884 | FPExtInst( | |||
4885 | Value *S, ///< The value to be extended | |||
4886 | Type *Ty, ///< The type to extend to | |||
4887 | const Twine &NameStr = "", ///< A name for the new instruction | |||
4888 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
4889 | ); | |||
4890 | ||||
4891 | /// Constructor with insert-at-end-of-block semantics | |||
4892 | FPExtInst( | |||
4893 | Value *S, ///< The value to be extended | |||
4894 | Type *Ty, ///< The type to extend to | |||
4895 | const Twine &NameStr, ///< A name for the new instruction | |||
4896 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
4897 | ); | |||
4898 | ||||
4899 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4900 | static bool classof(const Instruction *I) { | |||
4901 | return I->getOpcode() == FPExt; | |||
4902 | } | |||
4903 | static bool classof(const Value *V) { | |||
4904 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4905 | } | |||
4906 | }; | |||
4907 | ||||
4908 | //===----------------------------------------------------------------------===// | |||
4909 | // UIToFPInst Class | |||
4910 | //===----------------------------------------------------------------------===// | |||
4911 | ||||
4912 | /// This class represents a cast unsigned integer to floating point. | |||
4913 | class UIToFPInst : public CastInst { | |||
4914 | protected: | |||
4915 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4916 | friend class Instruction; | |||
4917 | ||||
4918 | /// Clone an identical UIToFPInst | |||
4919 | UIToFPInst *cloneImpl() const; | |||
4920 | ||||
4921 | public: | |||
4922 | /// Constructor with insert-before-instruction semantics | |||
4923 | UIToFPInst( | |||
4924 | Value *S, ///< The value to be converted | |||
4925 | Type *Ty, ///< The type to convert to | |||
4926 | const Twine &NameStr = "", ///< A name for the new instruction | |||
4927 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
4928 | ); | |||
4929 | ||||
4930 | /// Constructor with insert-at-end-of-block semantics | |||
4931 | UIToFPInst( | |||
4932 | Value *S, ///< The value to be converted | |||
4933 | Type *Ty, ///< The type to convert to | |||
4934 | const Twine &NameStr, ///< A name for the new instruction | |||
4935 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
4936 | ); | |||
4937 | ||||
4938 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4939 | static bool classof(const Instruction *I) { | |||
4940 | return I->getOpcode() == UIToFP; | |||
4941 | } | |||
4942 | static bool classof(const Value *V) { | |||
4943 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4944 | } | |||
4945 | }; | |||
4946 | ||||
4947 | //===----------------------------------------------------------------------===// | |||
4948 | // SIToFPInst Class | |||
4949 | //===----------------------------------------------------------------------===// | |||
4950 | ||||
4951 | /// This class represents a cast from signed integer to floating point. | |||
4952 | class SIToFPInst : public CastInst { | |||
4953 | protected: | |||
4954 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4955 | friend class Instruction; | |||
4956 | ||||
4957 | /// Clone an identical SIToFPInst | |||
4958 | SIToFPInst *cloneImpl() const; | |||
4959 | ||||
4960 | public: | |||
4961 | /// Constructor with insert-before-instruction semantics | |||
4962 | SIToFPInst( | |||
4963 | Value *S, ///< The value to be converted | |||
4964 | Type *Ty, ///< The type to convert to | |||
4965 | const Twine &NameStr = "", ///< A name for the new instruction | |||
4966 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
4967 | ); | |||
4968 | ||||
4969 | /// Constructor with insert-at-end-of-block semantics | |||
4970 | SIToFPInst( | |||
4971 | Value *S, ///< The value to be converted | |||
4972 | Type *Ty, ///< The type to convert to | |||
4973 | const Twine &NameStr, ///< A name for the new instruction | |||
4974 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
4975 | ); | |||
4976 | ||||
4977 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4978 | static bool classof(const Instruction *I) { | |||
4979 | return I->getOpcode() == SIToFP; | |||
4980 | } | |||
4981 | static bool classof(const Value *V) { | |||
4982 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4983 | } | |||
4984 | }; | |||
4985 | ||||
4986 | //===----------------------------------------------------------------------===// | |||
4987 | // FPToUIInst Class | |||
4988 | //===----------------------------------------------------------------------===// | |||
4989 | ||||
4990 | /// This class represents a cast from floating point to unsigned integer | |||
4991 | class FPToUIInst : public CastInst { | |||
4992 | protected: | |||
4993 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4994 | friend class Instruction; | |||
4995 | ||||
4996 | /// Clone an identical FPToUIInst | |||
4997 | FPToUIInst *cloneImpl() const; | |||
4998 | ||||
4999 | public: | |||
5000 | /// Constructor with insert-before-instruction semantics | |||
5001 | FPToUIInst( | |||
5002 | Value *S, ///< The value to be converted | |||
5003 | Type *Ty, ///< The type to convert to | |||
5004 | const Twine &NameStr = "", ///< A name for the new instruction | |||
5005 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
5006 | ); | |||
5007 | ||||
5008 | /// Constructor with insert-at-end-of-block semantics | |||
5009 | FPToUIInst( | |||
5010 | Value *S, ///< The value to be converted | |||
5011 | Type *Ty, ///< The type to convert to | |||
5012 | const Twine &NameStr, ///< A name for the new instruction | |||
5013 | BasicBlock *InsertAtEnd ///< Where to insert the new instruction | |||
5014 | ); | |||
5015 | ||||
5016 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
5017 | static bool classof(const Instruction *I) { | |||
5018 | return I->getOpcode() == FPToUI; | |||
5019 | } | |||
5020 | static bool classof(const Value *V) { | |||
5021 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
5022 | } | |||
5023 | }; | |||
5024 | ||||
5025 | //===----------------------------------------------------------------------===// | |||
5026 | // FPToSIInst Class | |||
5027 | //===----------------------------------------------------------------------===// | |||
5028 | ||||
5029 | /// This class represents a cast from floating point to signed integer. | |||
5030 | class FPToSIInst : public CastInst { | |||
5031 | protected: | |||
5032 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
5033 | friend class Instruction; | |||
5034 | ||||
5035 | /// Clone an identical FPToSIInst | |||
5036 | FPToSIInst *cloneImpl() const; | |||
5037 | ||||
5038 | public: | |||
5039 | /// Constructor with insert-before-instruction semantics | |||
5040 | FPToSIInst( | |||
5041 | Value *S, ///< The value to be converted | |||
5042 | Type *Ty, ///< The type to convert to | |||
5043 | const Twine &NameStr = "", ///< A name for the new instruction | |||
5044 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
5045 | ); | |||
5046 | ||||
5047 | /// Constructor with insert-at-end-of-block semantics | |||
5048 | FPToSIInst( | |||
5049 | Value *S, ///< The value to be converted | |||
5050 | Type *Ty, ///< The type to convert to | |||
5051 | const Twine &NameStr, ///< A name for the new instruction | |||
5052 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
5053 | ); | |||
5054 | ||||
5055 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
5056 | static bool classof(const Instruction *I) { | |||
5057 | return I->getOpcode() == FPToSI; | |||
5058 | } | |||
5059 | static bool classof(const Value *V) { | |||
5060 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
5061 | } | |||
5062 | }; | |||
5063 | ||||
5064 | //===----------------------------------------------------------------------===// | |||
5065 | // IntToPtrInst Class | |||
5066 | //===----------------------------------------------------------------------===// | |||
5067 | ||||
5068 | /// This class represents a cast from an integer to a pointer. | |||
5069 | class IntToPtrInst : public CastInst { | |||
5070 | public: | |||
5071 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
5072 | friend class Instruction; | |||
5073 | ||||
5074 | /// Constructor with insert-before-instruction semantics | |||
5075 | IntToPtrInst( | |||
5076 | Value *S, ///< The value to be converted | |||
5077 | Type *Ty, ///< The type to convert to | |||
5078 | const Twine &NameStr = "", ///< A name for the new instruction | |||
5079 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
5080 | ); | |||
5081 | ||||
5082 | /// Constructor with insert-at-end-of-block semantics | |||
5083 | IntToPtrInst( | |||
5084 | Value *S, ///< The value to be converted | |||
5085 | Type *Ty, ///< The type to convert to | |||
5086 | const Twine &NameStr, ///< A name for the new instruction | |||
5087 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
5088 | ); | |||
5089 | ||||
5090 | /// Clone an identical IntToPtrInst. | |||
5091 | IntToPtrInst *cloneImpl() const; | |||
5092 | ||||
5093 | /// Returns the address space of this instruction's pointer type. | |||
5094 | unsigned getAddressSpace() const { | |||
5095 | return getType()->getPointerAddressSpace(); | |||
5096 | } | |||
5097 | ||||
5098 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
5099 | static bool classof(const Instruction *I) { | |||
5100 | return I->getOpcode() == IntToPtr; | |||
5101 | } | |||
5102 | static bool classof(const Value *V) { | |||
5103 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
5104 | } | |||
5105 | }; | |||
5106 | ||||
5107 | //===----------------------------------------------------------------------===// | |||
5108 | // PtrToIntInst Class | |||
5109 | //===----------------------------------------------------------------------===// | |||
5110 | ||||
5111 | /// This class represents a cast from a pointer to an integer. | |||
5112 | class PtrToIntInst : public CastInst { | |||
5113 | protected: | |||
5114 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
5115 | friend class Instruction; | |||
5116 | ||||
5117 | /// Clone an identical PtrToIntInst. | |||
5118 | PtrToIntInst *cloneImpl() const; | |||
5119 | ||||
5120 | public: | |||
5121 | /// Constructor with insert-before-instruction semantics | |||
5122 | PtrToIntInst( | |||
5123 | Value *S, ///< The value to be converted | |||
5124 | Type *Ty, ///< The type to convert to | |||
5125 | const Twine &NameStr = "", ///< A name for the new instruction | |||
5126 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
5127 | ); | |||
5128 | ||||
5129 | /// Constructor with insert-at-end-of-block semantics | |||
5130 | PtrToIntInst( | |||
5131 | Value *S, ///< The value to be converted | |||
5132 | Type *Ty, ///< The type to convert to | |||
5133 | const Twine &NameStr, ///< A name for the new instruction | |||
5134 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
5135 | ); | |||
5136 | ||||
5137 | /// Gets the pointer operand. | |||
5138 | Value *getPointerOperand() { return getOperand(0); } | |||
5139 | /// Gets the pointer operand. | |||
5140 | const Value *getPointerOperand() const { return getOperand(0); } | |||
5141 | /// Gets the operand index of the pointer operand. | |||
5142 | static unsigned getPointerOperandIndex() { return 0U; } | |||
5143 | ||||
5144 | /// Returns the address space of the pointer operand. | |||
5145 | unsigned getPointerAddressSpace() const { | |||
5146 | return getPointerOperand()->getType()->getPointerAddressSpace(); | |||
5147 | } | |||
5148 | ||||
5149 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
5150 | static bool classof(const Instruction *I) { | |||
5151 | return I->getOpcode() == PtrToInt; | |||
5152 | } | |||
5153 | static bool classof(const Value *V) { | |||
5154 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
5155 | } | |||
5156 | }; | |||
5157 | ||||
5158 | //===----------------------------------------------------------------------===// | |||
5159 | // BitCastInst Class | |||
5160 | //===----------------------------------------------------------------------===// | |||
5161 | ||||
5162 | /// This class represents a no-op cast from one type to another. | |||
5163 | class BitCastInst : public CastInst { | |||
5164 | protected: | |||
5165 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
5166 | friend class Instruction; | |||
5167 | ||||
5168 | /// Clone an identical BitCastInst. | |||
5169 | BitCastInst *cloneImpl() const; | |||
5170 | ||||
5171 | public: | |||
5172 | /// Constructor with insert-before-instruction semantics | |||
5173 | BitCastInst( | |||
5174 | Value *S, ///< The value to be casted | |||
5175 | Type *Ty, ///< The type to casted to | |||
5176 | const Twine &NameStr = "", ///< A name for the new instruction | |||
5177 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
5178 | ); | |||
5179 | ||||
5180 | /// Constructor with insert-at-end-of-block semantics | |||
5181 | BitCastInst( | |||
5182 | Value *S, ///< The value to be casted | |||
5183 | Type *Ty, ///< The type to casted to | |||
5184 | const Twine &NameStr, ///< A name for the new instruction | |||
5185 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
5186 | ); | |||
5187 | ||||
5188 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
5189 | static bool classof(const Instruction *I) { | |||
5190 | return I->getOpcode() == BitCast; | |||
5191 | } | |||
5192 | static bool classof(const Value *V) { | |||
5193 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
5194 | } | |||
5195 | }; | |||
5196 | ||||
5197 | //===----------------------------------------------------------------------===// | |||
5198 | // AddrSpaceCastInst Class | |||
5199 | //===----------------------------------------------------------------------===// | |||
5200 | ||||
5201 | /// This class represents a conversion between pointers from one address space | |||
5202 | /// to another. | |||
5203 | class AddrSpaceCastInst : public CastInst { | |||
5204 | protected: | |||
5205 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
5206 | friend class Instruction; | |||
5207 | ||||
5208 | /// Clone an identical AddrSpaceCastInst. | |||
5209 | AddrSpaceCastInst *cloneImpl() const; | |||
5210 | ||||
5211 | public: | |||
5212 | /// Constructor with insert-before-instruction semantics | |||
5213 | AddrSpaceCastInst( | |||
5214 | Value *S, ///< The value to be casted | |||
5215 | Type *Ty, ///< The type to casted to | |||
5216 | const Twine &NameStr = "", ///< A name for the new instruction | |||
5217 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
5218 | ); | |||
5219 | ||||
5220 | /// Constructor with insert-at-end-of-block semantics | |||
5221 | AddrSpaceCastInst( | |||
5222 | Value *S, ///< The value to be casted | |||
5223 | Type *Ty, ///< The type to casted to | |||
5224 | const Twine &NameStr, ///< A name for the new instruction | |||
5225 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
5226 | ); | |||
5227 | ||||
5228 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
5229 | static bool classof(const Instruction *I) { | |||
5230 | return I->getOpcode() == AddrSpaceCast; | |||
5231 | } | |||
5232 | static bool classof(const Value *V) { | |||
5233 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
5234 | } | |||
5235 | ||||
5236 | /// Gets the pointer operand. | |||
5237 | Value *getPointerOperand() { | |||
5238 | return getOperand(0); | |||
5239 | } | |||
5240 | ||||
5241 | /// Gets the pointer operand. | |||
5242 | const Value *getPointerOperand() const { | |||
5243 | return getOperand(0); | |||
5244 | } | |||
5245 | ||||
5246 | /// Gets the operand index of the pointer operand. | |||
5247 | static unsigned getPointerOperandIndex() { | |||
5248 | return 0U; | |||
5249 | } | |||
5250 | ||||
5251 | /// Returns the address space of the pointer operand. | |||
5252 | unsigned getSrcAddressSpace() const { | |||
5253 | return getPointerOperand()->getType()->getPointerAddressSpace(); | |||
5254 | } | |||
5255 | ||||
5256 | /// Returns the address space of the result. | |||
5257 | unsigned getDestAddressSpace() const { | |||
5258 | return getType()->getPointerAddressSpace(); | |||
5259 | } | |||
5260 | }; | |||
5261 | ||||
5262 | /// A helper function that returns the pointer operand of a load or store | |||
5263 | /// instruction. Returns nullptr if not load or store. | |||
5264 | inline const Value *getLoadStorePointerOperand(const Value *V) { | |||
5265 | if (auto *Load = dyn_cast<LoadInst>(V)) | |||
5266 | return Load->getPointerOperand(); | |||
5267 | if (auto *Store = dyn_cast<StoreInst>(V)) | |||
5268 | return Store->getPointerOperand(); | |||
5269 | return nullptr; | |||
5270 | } | |||
5271 | inline Value *getLoadStorePointerOperand(Value *V) { | |||
5272 | return const_cast<Value *>( | |||
5273 | getLoadStorePointerOperand(static_cast<const Value *>(V))); | |||
5274 | } | |||
5275 | ||||
5276 | /// A helper function that returns the pointer operand of a load, store | |||
5277 | /// or GEP instruction. Returns nullptr if not load, store, or GEP. | |||
5278 | inline const Value *getPointerOperand(const Value *V) { | |||
5279 | if (auto *Ptr = getLoadStorePointerOperand(V)) | |||
5280 | return Ptr; | |||
5281 | if (auto *Gep = dyn_cast<GetElementPtrInst>(V)) | |||
5282 | return Gep->getPointerOperand(); | |||
5283 | return nullptr; | |||
5284 | } | |||
5285 | inline Value *getPointerOperand(Value *V) { | |||
5286 | return const_cast<Value *>(getPointerOperand(static_cast<const Value *>(V))); | |||
5287 | } | |||
5288 | ||||
5289 | /// A helper function that returns the alignment of load or store instruction. | |||
5290 | inline MaybeAlign getLoadStoreAlignment(Value *I) { | |||
5291 | assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&(((isa<LoadInst>(I) || isa<StoreInst>(I)) && "Expected Load or Store instruction") ? static_cast<void> (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 5292, __PRETTY_FUNCTION__)) | |||
5292 | "Expected Load or Store instruction")(((isa<LoadInst>(I) || isa<StoreInst>(I)) && "Expected Load or Store instruction") ? static_cast<void> (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 5292, __PRETTY_FUNCTION__)); | |||
5293 | if (auto *LI = dyn_cast<LoadInst>(I)) | |||
5294 | return MaybeAlign(LI->getAlignment()); | |||
5295 | return MaybeAlign(cast<StoreInst>(I)->getAlignment()); | |||
5296 | } | |||
5297 | ||||
5298 | /// A helper function that returns the address space of the pointer operand of | |||
5299 | /// load or store instruction. | |||
5300 | inline unsigned getLoadStoreAddressSpace(Value *I) { | |||
5301 | assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&(((isa<LoadInst>(I) || isa<StoreInst>(I)) && "Expected Load or Store instruction") ? static_cast<void> (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 5302, __PRETTY_FUNCTION__)) | |||
5302 | "Expected Load or Store instruction")(((isa<LoadInst>(I) || isa<StoreInst>(I)) && "Expected Load or Store instruction") ? static_cast<void> (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/IR/Instructions.h" , 5302, __PRETTY_FUNCTION__)); | |||
5303 | if (auto *LI = dyn_cast<LoadInst>(I)) | |||
5304 | return LI->getPointerAddressSpace(); | |||
5305 | return cast<StoreInst>(I)->getPointerAddressSpace(); | |||
5306 | } | |||
5307 | ||||
5308 | //===----------------------------------------------------------------------===// | |||
5309 | // FreezeInst Class | |||
5310 | //===----------------------------------------------------------------------===// | |||
5311 | ||||
5312 | /// This class represents a freeze function that returns random concrete | |||
5313 | /// value if an operand is either a poison value or an undef value | |||
5314 | class FreezeInst : public UnaryInstruction { | |||
5315 | protected: | |||
5316 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
5317 | friend class Instruction; | |||
5318 | ||||
5319 | /// Clone an identical FreezeInst | |||
5320 | FreezeInst *cloneImpl() const; | |||
5321 | ||||
5322 | public: | |||
5323 | explicit FreezeInst(Value *S, | |||
5324 | const Twine &NameStr = "", | |||
5325 | Instruction *InsertBefore = nullptr); | |||
5326 | FreezeInst(Value *S, const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
5327 | ||||
5328 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
5329 | static inline bool classof(const Instruction *I) { | |||
5330 | return I->getOpcode() == Freeze; | |||
5331 | } | |||
5332 | static inline bool classof(const Value *V) { | |||
5333 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
5334 | } | |||
5335 | }; | |||
5336 | ||||
5337 | } // end namespace llvm | |||
5338 | ||||
5339 | #endif // LLVM_IR_INSTRUCTIONS_H |