Bug Summary

File:include/llvm/IR/Instructions.h
Warning:line 1347, column 33
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name InstCombineSelect.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-10~svn374877/build-llvm/lib/Transforms/InstCombine -I /build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/InstCombine -I /build/llvm-toolchain-snapshot-10~svn374877/build-llvm/include -I /build/llvm-toolchain-snapshot-10~svn374877/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-10~svn374877/build-llvm/lib/Transforms/InstCombine -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~svn374877=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2019-10-15-233810-7101-1 -x c++ /build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/InstCombine/InstCombineSelect.cpp

/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/InstCombine/InstCombineSelect.cpp

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
44using namespace llvm;
45using namespace PatternMatch;
46
47#define DEBUG_TYPE"instcombine" "instcombine"
48
49static 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-10~svn374877/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.
58static Instruction *foldSelectBinOpIdentity(SelectInst &Sel,
59 const TargetLibraryInfo &TLI) {
60 // The select condition must be an equality compare with a constant operand.
61 Value *X;
62 Constant *C;
63 CmpInst::Predicate Pred;
64 if (!match(Sel.getCondition(), m_Cmp(Pred, m_Value(X), m_Constant(C))))
65 return nullptr;
66
67 bool IsEq;
68 if (ICmpInst::isEquality(Pred))
69 IsEq = Pred == ICmpInst::ICMP_EQ;
70 else if (Pred == FCmpInst::FCMP_OEQ)
71 IsEq = true;
72 else if (Pred == FCmpInst::FCMP_UNE)
73 IsEq = false;
74 else
75 return nullptr;
76
77 // A select operand must be a binop.
78 BinaryOperator *BO;
79 if (!match(Sel.getOperand(IsEq ? 1 : 2), m_BinOp(BO)))
80 return nullptr;
81
82 // The compare constant must be the identity constant for that binop.
83 // If this a floating-point compare with 0.0, any zero constant will do.
84 Type *Ty = BO->getType();
85 Constant *IdC = ConstantExpr::getBinOpIdentity(BO->getOpcode(), Ty, true);
86 if (IdC != C) {
87 if (!IdC || !CmpInst::isFPPredicate(Pred))
88 return nullptr;
89 if (!match(IdC, m_AnyZeroFP()) || !match(C, m_AnyZeroFP()))
90 return nullptr;
91 }
92
93 // Last, match the compare variable operand with a binop operand.
94 Value *Y;
95 if (!BO->isCommutative() && !match(BO, m_BinOp(m_Value(Y), m_Specific(X))))
96 return nullptr;
97 if (!match(BO, m_c_BinOp(m_Value(Y), m_Specific(X))))
98 return nullptr;
99
100 // +0.0 compares equal to -0.0, and so it does not behave as required for this
101 // transform. Bail out if we can not exclude that possibility.
102 if (isa<FPMathOperator>(BO))
103 if (!BO->hasNoSignedZeros() && !CannotBeNegativeZero(Y, &TLI))
104 return nullptr;
105
106 // BO = binop Y, X
107 // S = { select (cmp eq X, C), BO, ? } or { select (cmp ne X, C), ?, BO }
108 // =>
109 // S = { select (cmp eq X, C), Y, ? } or { select (cmp ne X, C), ?, Y }
110 Sel.setOperand(IsEq ? 1 : 2, Y);
111 return &Sel;
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.
123static 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-10~svn374877/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-10~svn374877/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.
243static 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.
263static 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-10~svn374877/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.
282Instruction *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 BinaryOperator::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-10~svn374877/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 411)
;
412 return nullptr;
413}
414
415static 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.
424Instruction *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.
501static 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)
539static 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
587static 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.
680static 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-10~svn374877/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-10~svn374877/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 705, __PRETTY_FUNCTION__))
;
706
707 // Account for swapped form of subtraction: ((a > b) ? b - a : 0).
708 bool IsNegative = false;
709 if (match(TrueVal, m_Sub(m_Specific(B), m_Specific(A))))
710 IsNegative = true;
711 else if (!match(TrueVal, m_Sub(m_Specific(A), m_Specific(B))))
712 return nullptr;
713
714 // If sub is used anywhere else, we wouldn't be able to eliminate it
715 // afterwards.
716 if (!TrueVal->hasOneUse())
717 return nullptr;
718
719 // (a > b) ? a - b : 0 -> usub.sat(a, b)
720 // (a > b) ? b - a : 0 -> -usub.sat(a, b)
721 Value *Result = Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, A, B);
722 if (IsNegative)
723 Result = Builder.CreateNeg(Result);
724 return Result;
725}
726
727static Value *canonicalizeSaturatedAdd(ICmpInst *Cmp, Value *TVal, Value *FVal,
728 InstCombiner::BuilderTy &Builder) {
729 if (!Cmp->hasOneUse())
730 return nullptr;
731
732 // Match unsigned saturated add with constant.
733 Value *Cmp0 = Cmp->getOperand(0);
734 Value *Cmp1 = Cmp->getOperand(1);
735 ICmpInst::Predicate Pred = Cmp->getPredicate();
736 Value *X;
737 const APInt *C, *CmpC;
738 if (Pred == ICmpInst::ICMP_ULT &&
739 match(TVal, m_Add(m_Value(X), m_APInt(C))) && X == Cmp0 &&
740 match(FVal, m_AllOnes()) && match(Cmp1, m_APInt(CmpC)) && *CmpC == ~*C) {
741 // (X u< ~C) ? (X + C) : -1 --> uadd.sat(X, C)
742 return Builder.CreateBinaryIntrinsic(
743 Intrinsic::uadd_sat, X, ConstantInt::get(X->getType(), *C));
744 }
745
746 // Match unsigned saturated add of 2 variables with an unnecessary 'not'.
747 // There are 8 commuted variants.
748 // Canonicalize -1 (saturated result) to true value of the select. Just
749 // swapping the compare operands is legal, because the selected value is the
750 // same in case of equality, so we can interchange u< and u<=.
751 if (match(FVal, m_AllOnes())) {
752 std::swap(TVal, FVal);
753 std::swap(Cmp0, Cmp1);
754 }
755 if (!match(TVal, m_AllOnes()))
756 return nullptr;
757
758 // Canonicalize predicate to 'ULT'.
759 if (Pred == ICmpInst::ICMP_UGT) {
760 Pred = ICmpInst::ICMP_ULT;
761 std::swap(Cmp0, Cmp1);
762 }
763 if (Pred != ICmpInst::ICMP_ULT)
764 return nullptr;
765
766 // Match unsigned saturated add of 2 variables with an unnecessary 'not'.
767 Value *Y;
768 if (match(Cmp0, m_Not(m_Value(X))) &&
769 match(FVal, m_c_Add(m_Specific(X), m_Value(Y))) && Y == Cmp1) {
770 // (~X u< Y) ? -1 : (X + Y) --> uadd.sat(X, Y)
771 // (~X u< Y) ? -1 : (Y + X) --> uadd.sat(X, Y)
772 return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, X, Y);
773 }
774 // The 'not' op may be included in the sum but not the compare.
775 X = Cmp0;
776 Y = Cmp1;
777 if (match(FVal, m_c_Add(m_Not(m_Specific(X)), m_Specific(Y)))) {
778 // (X u< Y) ? -1 : (~X + Y) --> uadd.sat(~X, Y)
779 // (X u< Y) ? -1 : (Y + ~X) --> uadd.sat(Y, ~X)
780 BinaryOperator *BO = cast<BinaryOperator>(FVal);
781 return Builder.CreateBinaryIntrinsic(
782 Intrinsic::uadd_sat, BO->getOperand(0), BO->getOperand(1));
783 }
784
785 return nullptr;
786}
787
788/// Fold the following code sequence:
789/// \code
790/// int a = ctlz(x & -x);
791// x ? 31 - a : a;
792/// \code
793///
794/// into:
795/// cttz(x)
796static Instruction *foldSelectCtlzToCttz(ICmpInst *ICI, Value *TrueVal,
797 Value *FalseVal,
798 InstCombiner::BuilderTy &Builder) {
799 unsigned BitWidth = TrueVal->getType()->getScalarSizeInBits();
800 if (!ICI->isEquality() || !match(ICI->getOperand(1), m_Zero()))
801 return nullptr;
802
803 if (ICI->getPredicate() == ICmpInst::ICMP_NE)
804 std::swap(TrueVal, FalseVal);
805
806 if (!match(FalseVal,
807 m_Xor(m_Deferred(TrueVal), m_SpecificInt(BitWidth - 1))))
808 return nullptr;
809
810 if (!match(TrueVal, m_Intrinsic<Intrinsic::ctlz>()))
811 return nullptr;
812
813 Value *X = ICI->getOperand(0);
814 auto *II = cast<IntrinsicInst>(TrueVal);
815 if (!match(II->getOperand(0), m_c_And(m_Specific(X), m_Neg(m_Specific(X)))))
816 return nullptr;
817
818 Function *F = Intrinsic::getDeclaration(II->getModule(), Intrinsic::cttz,
819 II->getType());
820 return CallInst::Create(F, {X, II->getArgOperand(1)});
821}
822
823/// Attempt to fold a cttz/ctlz followed by a icmp plus select into a single
824/// call to cttz/ctlz with flag 'is_zero_undef' cleared.
825///
826/// For example, we can fold the following code sequence:
827/// \code
828/// %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 true)
829/// %1 = icmp ne i32 %x, 0
830/// %2 = select i1 %1, i32 %0, i32 32
831/// \code
832///
833/// into:
834/// %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 false)
835static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal,
836 InstCombiner::BuilderTy &Builder) {
837 ICmpInst::Predicate Pred = ICI->getPredicate();
838 Value *CmpLHS = ICI->getOperand(0);
839 Value *CmpRHS = ICI->getOperand(1);
840
841 // Check if the condition value compares a value for equality against zero.
842 if (!ICI->isEquality() || !match(CmpRHS, m_Zero()))
843 return nullptr;
844
845 Value *Count = FalseVal;
846 Value *ValueOnZero = TrueVal;
847 if (Pred == ICmpInst::ICMP_NE)
848 std::swap(Count, ValueOnZero);
849
850 // Skip zero extend/truncate.
851 Value *V = nullptr;
852 if (match(Count, m_ZExt(m_Value(V))) ||
853 match(Count, m_Trunc(m_Value(V))))
854 Count = V;
855
856 // Check that 'Count' is a call to intrinsic cttz/ctlz. Also check that the
857 // input to the cttz/ctlz is used as LHS for the compare instruction.
858 if (!match(Count, m_Intrinsic<Intrinsic::cttz>(m_Specific(CmpLHS))) &&
859 !match(Count, m_Intrinsic<Intrinsic::ctlz>(m_Specific(CmpLHS))))
860 return nullptr;
861
862 IntrinsicInst *II = cast<IntrinsicInst>(Count);
863
864 // Check if the value propagated on zero is a constant number equal to the
865 // sizeof in bits of 'Count'.
866 unsigned SizeOfInBits = Count->getType()->getScalarSizeInBits();
867 if (match(ValueOnZero, m_SpecificInt(SizeOfInBits))) {
868 // Explicitly clear the 'undef_on_zero' flag.
869 IntrinsicInst *NewI = cast<IntrinsicInst>(II->clone());
870 NewI->setArgOperand(1, ConstantInt::getFalse(NewI->getContext()));
871 Builder.Insert(NewI);
872 return Builder.CreateZExtOrTrunc(NewI, ValueOnZero->getType());
873 }
874
875 // If the ValueOnZero is not the bitwidth, we can at least make use of the
876 // fact that the cttz/ctlz result will not be used if the input is zero, so
877 // it's okay to relax it to undef for that case.
878 if (II->hasOneUse() && !match(II->getArgOperand(1), m_One()))
879 II->setArgOperand(1, ConstantInt::getTrue(II->getContext()));
880
881 return nullptr;
882}
883
884/// Return true if we find and adjust an icmp+select pattern where the compare
885/// is with a constant that can be incremented or decremented to match the
886/// minimum or maximum idiom.
887static bool adjustMinMax(SelectInst &Sel, ICmpInst &Cmp) {
888 ICmpInst::Predicate Pred = Cmp.getPredicate();
889 Value *CmpLHS = Cmp.getOperand(0);
890 Value *CmpRHS = Cmp.getOperand(1);
891 Value *TrueVal = Sel.getTrueValue();
892 Value *FalseVal = Sel.getFalseValue();
893
894 // We may move or edit the compare, so make sure the select is the only user.
895 const APInt *CmpC;
896 if (!Cmp.hasOneUse() || !match(CmpRHS, m_APInt(CmpC)))
897 return false;
898
899 // These transforms only work for selects of integers or vector selects of
900 // integer vectors.
901 Type *SelTy = Sel.getType();
902 auto *SelEltTy = dyn_cast<IntegerType>(SelTy->getScalarType());
903 if (!SelEltTy || SelTy->isVectorTy() != Cmp.getType()->isVectorTy())
904 return false;
905
906 Constant *AdjustedRHS;
907 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SGT)
908 AdjustedRHS = ConstantInt::get(CmpRHS->getType(), *CmpC + 1);
909 else if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SLT)
910 AdjustedRHS = ConstantInt::get(CmpRHS->getType(), *CmpC - 1);
911 else
912 return false;
913
914 // X > C ? X : C+1 --> X < C+1 ? C+1 : X
915 // X < C ? X : C-1 --> X > C-1 ? C-1 : X
916 if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) ||
917 (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) {
918 ; // Nothing to do here. Values match without any sign/zero extension.
919 }
920 // Types do not match. Instead of calculating this with mixed types, promote
921 // all to the larger type. This enables scalar evolution to analyze this
922 // expression.
923 else if (CmpRHS->getType()->getScalarSizeInBits() < SelEltTy->getBitWidth()) {
924 Constant *SextRHS = ConstantExpr::getSExt(AdjustedRHS, SelTy);
925
926 // X = sext x; x >s c ? X : C+1 --> X = sext x; X <s C+1 ? C+1 : X
927 // X = sext x; x <s c ? X : C-1 --> X = sext x; X >s C-1 ? C-1 : X
928 // X = sext x; x >u c ? X : C+1 --> X = sext x; X <u C+1 ? C+1 : X
929 // X = sext x; x <u c ? X : C-1 --> X = sext x; X >u C-1 ? C-1 : X
930 if (match(TrueVal, m_SExt(m_Specific(CmpLHS))) && SextRHS == FalseVal) {
931 CmpLHS = TrueVal;
932 AdjustedRHS = SextRHS;
933 } else if (match(FalseVal, m_SExt(m_Specific(CmpLHS))) &&
934 SextRHS == TrueVal) {
935 CmpLHS = FalseVal;
936 AdjustedRHS = SextRHS;
937 } else if (Cmp.isUnsigned()) {
938 Constant *ZextRHS = ConstantExpr::getZExt(AdjustedRHS, SelTy);
939 // X = zext x; x >u c ? X : C+1 --> X = zext x; X <u C+1 ? C+1 : X
940 // X = zext x; x <u c ? X : C-1 --> X = zext x; X >u C-1 ? C-1 : X
941 // zext + signed compare cannot be changed:
942 // 0xff <s 0x00, but 0x00ff >s 0x0000
943 if (match(TrueVal, m_ZExt(m_Specific(CmpLHS))) && ZextRHS == FalseVal) {
944 CmpLHS = TrueVal;
945 AdjustedRHS = ZextRHS;
946 } else if (match(FalseVal, m_ZExt(m_Specific(CmpLHS))) &&
947 ZextRHS == TrueVal) {
948 CmpLHS = FalseVal;
949 AdjustedRHS = ZextRHS;
950 } else {
951 return false;
952 }
953 } else {
954 return false;
955 }
956 } else {
957 return false;
958 }
959
960 Pred = ICmpInst::getSwappedPredicate(Pred);
961 CmpRHS = AdjustedRHS;
962 std::swap(FalseVal, TrueVal);
963 Cmp.setPredicate(Pred);
964 Cmp.setOperand(0, CmpLHS);
965 Cmp.setOperand(1, CmpRHS);
966 Sel.setOperand(1, TrueVal);
967 Sel.setOperand(2, FalseVal);
968 Sel.swapProfMetadata();
969
970 // Move the compare instruction right before the select instruction. Otherwise
971 // the sext/zext value may be defined after the compare instruction uses it.
972 Cmp.moveBefore(&Sel);
973
974 return true;
975}
976
977/// If this is an integer min/max (icmp + select) with a constant operand,
978/// create the canonical icmp for the min/max operation and canonicalize the
979/// constant to the 'false' operand of the select:
980/// select (icmp Pred X, C1), C2, X --> select (icmp Pred' X, C2), X, C2
981/// Note: if C1 != C2, this will change the icmp constant to the existing
982/// constant operand of the select.
983static Instruction *
984canonicalizeMinMaxWithConstant(SelectInst &Sel, ICmpInst &Cmp,
985 InstCombiner::BuilderTy &Builder) {
986 if (!Cmp.hasOneUse() || !isa<Constant>(Cmp.getOperand(1)))
987 return nullptr;
988
989 // Canonicalize the compare predicate based on whether we have min or max.
990 Value *LHS, *RHS;
991 SelectPatternResult SPR = matchSelectPattern(&Sel, LHS, RHS);
992 if (!SelectPatternResult::isMinOrMax(SPR.Flavor))
993 return nullptr;
994
995 // Is this already canonical?
996 ICmpInst::Predicate CanonicalPred = getMinMaxPred(SPR.Flavor);
997 if (Cmp.getOperand(0) == LHS && Cmp.getOperand(1) == RHS &&
998 Cmp.getPredicate() == CanonicalPred)
999 return nullptr;
1000
1001 // Create the canonical compare and plug it into the select.
1002 Sel.setCondition(Builder.CreateICmp(CanonicalPred, LHS, RHS));
1003
1004 // If the select operands did not change, we're done.
1005 if (Sel.getTrueValue() == LHS && Sel.getFalseValue() == RHS)
1006 return &Sel;
1007
1008 // If we are swapping the select operands, swap the metadata too.
1009 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-10~svn374877/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 1010, __PRETTY_FUNCTION__))
1010 "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-10~svn374877/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 1010, __PRETTY_FUNCTION__))
;
1011 Sel.swapValues();
1012 Sel.swapProfMetadata();
1013 return &Sel;
1014}
1015
1016/// There are many select variants for each of ABS/NABS.
1017/// In matchSelectPattern(), there are different compare constants, compare
1018/// predicates/operands and select operands.
1019/// In isKnownNegation(), there are different formats of negated operands.
1020/// Canonicalize all these variants to 1 pattern.
1021/// This makes CSE more likely.
1022static Instruction *canonicalizeAbsNabs(SelectInst &Sel, ICmpInst &Cmp,
1023 InstCombiner::BuilderTy &Builder) {
1024 if (!Cmp.hasOneUse() || !isa<Constant>(Cmp.getOperand(1)))
1025 return nullptr;
1026
1027 // Choose a sign-bit check for the compare (likely simpler for codegen).
1028 // ABS: (X <s 0) ? -X : X
1029 // NABS: (X <s 0) ? X : -X
1030 Value *LHS, *RHS;
1031 SelectPatternFlavor SPF = matchSelectPattern(&Sel, LHS, RHS).Flavor;
1032 if (SPF != SelectPatternFlavor::SPF_ABS &&
1033 SPF != SelectPatternFlavor::SPF_NABS)
1034 return nullptr;
1035
1036 Value *TVal = Sel.getTrueValue();
1037 Value *FVal = Sel.getFalseValue();
1038 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-10~svn374877/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 1039, __PRETTY_FUNCTION__))
1039 "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-10~svn374877/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 1039, __PRETTY_FUNCTION__))
;
1040
1041 // The compare may use the negated abs()/nabs() operand, or it may use
1042 // negation in non-canonical form such as: sub A, B.
1043 bool CmpUsesNegatedOp = match(Cmp.getOperand(0), m_Neg(m_Specific(TVal))) ||
1044 match(Cmp.getOperand(0), m_Neg(m_Specific(FVal)));
1045
1046 bool CmpCanonicalized = !CmpUsesNegatedOp &&
1047 match(Cmp.getOperand(1), m_ZeroInt()) &&
1048 Cmp.getPredicate() == ICmpInst::ICMP_SLT;
1049 bool RHSCanonicalized = match(RHS, m_Neg(m_Specific(LHS)));
1050
1051 // Is this already canonical?
1052 if (CmpCanonicalized && RHSCanonicalized)
1053 return nullptr;
1054
1055 // If RHS is used by other instructions except compare and select, don't
1056 // canonicalize it to not increase the instruction count.
1057 if (!(RHS->hasOneUse() || (RHS->hasNUses(2) && CmpUsesNegatedOp)))
1058 return nullptr;
1059
1060 // Create the canonical compare: icmp slt LHS 0.
1061 if (!CmpCanonicalized) {
1062 Cmp.setPredicate(ICmpInst::ICMP_SLT);
1063 Cmp.setOperand(1, ConstantInt::getNullValue(Cmp.getOperand(0)->getType()));
1064 if (CmpUsesNegatedOp)
1065 Cmp.setOperand(0, LHS);
1066 }
1067
1068 // Create the canonical RHS: RHS = sub (0, LHS).
1069 if (!RHSCanonicalized) {
1070 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-10~svn374877/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 1070, __PRETTY_FUNCTION__))
;
1071 RHS = Builder.CreateNeg(LHS);
1072 if (TVal == LHS) {
1073 Sel.setFalseValue(RHS);
1074 FVal = RHS;
1075 } else {
1076 Sel.setTrueValue(RHS);
1077 TVal = RHS;
1078 }
1079 }
1080
1081 // If the select operands do not change, we're done.
1082 if (SPF == SelectPatternFlavor::SPF_NABS) {
1083 if (TVal == LHS)
1084 return &Sel;
1085 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-10~svn374877/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 1085, __PRETTY_FUNCTION__))
;
1086 } else {
1087 if (FVal == LHS)
1088 return &Sel;
1089 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-10~svn374877/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 1089, __PRETTY_FUNCTION__))
;
1090 }
1091
1092 // We are swapping the select operands, so swap the metadata too.
1093 Sel.swapValues();
1094 Sel.swapProfMetadata();
1095 return &Sel;
1096}
1097
1098static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *ReplaceOp,
1099 const SimplifyQuery &Q) {
1100 // If this is a binary operator, try to simplify it with the replaced op
1101 // because we know Op and ReplaceOp are equivalant.
1102 // For example: V = X + 1, Op = X, ReplaceOp = 42
1103 // Simplifies as: add(42, 1) --> 43
1104 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
1105 if (BO->getOperand(0) == Op)
1106 return SimplifyBinOp(BO->getOpcode(), ReplaceOp, BO->getOperand(1), Q);
1107 if (BO->getOperand(1) == Op)
1108 return SimplifyBinOp(BO->getOpcode(), BO->getOperand(0), ReplaceOp, Q);
1109 }
1110
1111 return nullptr;
1112}
1113
1114/// If we have a select with an equality comparison, then we know the value in
1115/// one of the arms of the select. See if substituting this value into an arm
1116/// and simplifying the result yields the same value as the other arm.
1117///
1118/// To make this transform safe, we must drop poison-generating flags
1119/// (nsw, etc) if we simplified to a binop because the select may be guarding
1120/// that poison from propagating. If the existing binop already had no
1121/// poison-generating flags, then this transform can be done by instsimplify.
1122///
1123/// Consider:
1124/// %cmp = icmp eq i32 %x, 2147483647
1125/// %add = add nsw i32 %x, 1
1126/// %sel = select i1 %cmp, i32 -2147483648, i32 %add
1127///
1128/// We can't replace %sel with %add unless we strip away the flags.
1129/// TODO: Wrapping flags could be preserved in some cases with better analysis.
1130static Value *foldSelectValueEquivalence(SelectInst &Sel, ICmpInst &Cmp,
1131 const SimplifyQuery &Q) {
1132 if (!Cmp.isEquality())
1133 return nullptr;
1134
1135 // Canonicalize the pattern to ICMP_EQ by swapping the select operands.
1136 Value *TrueVal = Sel.getTrueValue(), *FalseVal = Sel.getFalseValue();
1137 if (Cmp.getPredicate() == ICmpInst::ICMP_NE)
1138 std::swap(TrueVal, FalseVal);
1139
1140 // Try each equivalence substitution possibility.
1141 // We have an 'EQ' comparison, so the select's false value will propagate.
1142 // Example:
1143 // (X == 42) ? 43 : (X + 1) --> (X == 42) ? (X + 1) : (X + 1) --> X + 1
1144 // (X == 42) ? (X + 1) : 43 --> (X == 42) ? (42 + 1) : 43 --> 43
1145 Value *CmpLHS = Cmp.getOperand(0), *CmpRHS = Cmp.getOperand(1);
1146 if (simplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q) == TrueVal ||
1147 simplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q) == TrueVal ||
1148 simplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q) == FalseVal ||
1149 simplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q) == FalseVal) {
1150 if (auto *FalseInst = dyn_cast<Instruction>(FalseVal))
1151 FalseInst->dropPoisonGeneratingFlags();
1152 return FalseVal;
1153 }
1154 return nullptr;
1155}
1156
1157// See if this is a pattern like:
1158// %old_cmp1 = icmp slt i32 %x, C2
1159// %old_replacement = select i1 %old_cmp1, i32 %target_low, i32 %target_high
1160// %old_x_offseted = add i32 %x, C1
1161// %old_cmp0 = icmp ult i32 %old_x_offseted, C0
1162// %r = select i1 %old_cmp0, i32 %x, i32 %old_replacement
1163// This can be rewritten as more canonical pattern:
1164// %new_cmp1 = icmp slt i32 %x, -C1
1165// %new_cmp2 = icmp sge i32 %x, C0-C1
1166// %new_clamped_low = select i1 %new_cmp1, i32 %target_low, i32 %x
1167// %r = select i1 %new_cmp2, i32 %target_high, i32 %new_clamped_low
1168// Iff -C1 s<= C2 s<= C0-C1
1169// Also ULT predicate can also be UGT iff C0 != -1 (+invert result)
1170// SLT predicate can also be SGT iff C2 != INT_MAX (+invert res.)
1171static Instruction *canonicalizeClampLike(SelectInst &Sel0, ICmpInst &Cmp0,
1172 InstCombiner::BuilderTy &Builder) {
1173 Value *X = Sel0.getTrueValue();
1174 Value *Sel1 = Sel0.getFalseValue();
1175
1176 // First match the condition of the outermost select.
1177 // Said condition must be one-use.
1178 if (!Cmp0.hasOneUse())
1179 return nullptr;
1180 Value *Cmp00 = Cmp0.getOperand(0);
1181 Constant *C0;
1182 if (!match(Cmp0.getOperand(1),
1183 m_CombineAnd(m_AnyIntegralConstant(), m_Constant(C0))))
1184 return nullptr;
1185 // Canonicalize Cmp0 into the form we expect.
1186 // FIXME: we shouldn't care about lanes that are 'undef' in the end?
1187 switch (Cmp0.getPredicate()) {
1188 case ICmpInst::Predicate::ICMP_ULT:
1189 break; // Great!
1190 case ICmpInst::Predicate::ICMP_ULE:
1191 // We'd have to increment C0 by one, and for that it must not have all-ones
1192 // element, but then it would have been canonicalized to 'ult' before
1193 // we get here. So we can't do anything useful with 'ule'.
1194 return nullptr;
1195 case ICmpInst::Predicate::ICMP_UGT:
1196 // We want to canonicalize it to 'ult', so we'll need to increment C0,
1197 // which again means it must not have any all-ones elements.
1198 if (!match(C0,
1199 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE,
1200 APInt::getAllOnesValue(
1201 C0->getType()->getScalarSizeInBits()))))
1202 return nullptr; // Can't do, have all-ones element[s].
1203 C0 = AddOne(C0);
1204 std::swap(X, Sel1);
1205 break;
1206 case ICmpInst::Predicate::ICMP_UGE:
1207 // The only way we'd get this predicate if this `icmp` has extra uses,
1208 // but then we won't be able to do this fold.
1209 return nullptr;
1210 default:
1211 return nullptr; // Unknown predicate.
1212 }
1213
1214 // Now that we've canonicalized the ICmp, we know the X we expect;
1215 // the select in other hand should be one-use.
1216 if (!Sel1->hasOneUse())
1217 return nullptr;
1218
1219 // We now can finish matching the condition of the outermost select:
1220 // it should either be the X itself, or an addition of some constant to X.
1221 Constant *C1;
1222 if (Cmp00 == X)
1223 C1 = ConstantInt::getNullValue(Sel0.getType());
1224 else if (!match(Cmp00,
1225 m_Add(m_Specific(X),
1226 m_CombineAnd(m_AnyIntegralConstant(), m_Constant(C1)))))
1227 return nullptr;
1228
1229 Value *Cmp1;
1230 ICmpInst::Predicate Pred1;
1231 Constant *C2;
1232 Value *ReplacementLow, *ReplacementHigh;
1233 if (!match(Sel1, m_Select(m_Value(Cmp1), m_Value(ReplacementLow),
1234 m_Value(ReplacementHigh))) ||
1235 !match(Cmp1,
1236 m_ICmp(Pred1, m_Specific(X),
1237 m_CombineAnd(m_AnyIntegralConstant(), m_Constant(C2)))))
1238 return nullptr;
1239
1240 if (!Cmp1->hasOneUse() && (Cmp00 == X || !Cmp00->hasOneUse()))
1241 return nullptr; // Not enough one-use instructions for the fold.
1242 // FIXME: this restriction could be relaxed if Cmp1 can be reused as one of
1243 // two comparisons we'll need to build.
1244
1245 // Canonicalize Cmp1 into the form we expect.
1246 // FIXME: we shouldn't care about lanes that are 'undef' in the end?
1247 switch (Pred1) {
1248 case ICmpInst::Predicate::ICMP_SLT:
1249 break;
1250 case ICmpInst::Predicate::ICMP_SLE:
1251 // We'd have to increment C2 by one, and for that it must not have signed
1252 // max element, but then it would have been canonicalized to 'slt' before
1253 // we get here. So we can't do anything useful with 'sle'.
1254 return nullptr;
1255 case ICmpInst::Predicate::ICMP_SGT:
1256 // We want to canonicalize it to 'slt', so we'll need to increment C2,
1257 // which again means it must not have any signed max elements.
1258 if (!match(C2,
1259 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE,
1260 APInt::getSignedMaxValue(
1261 C2->getType()->getScalarSizeInBits()))))
1262 return nullptr; // Can't do, have signed max element[s].
1263 C2 = AddOne(C2);
1264 LLVM_FALLTHROUGH[[gnu::fallthrough]];
1265 case ICmpInst::Predicate::ICMP_SGE:
1266 // Also non-canonical, but here we don't need to change C2,
1267 // so we don't have any restrictions on C2, so we can just handle it.
1268 std::swap(ReplacementLow, ReplacementHigh);
1269 break;
1270 default:
1271 return nullptr; // Unknown predicate.
1272 }
1273
1274 // The thresholds of this clamp-like pattern.
1275 auto *ThresholdLowIncl = ConstantExpr::getNeg(C1);
1276 auto *ThresholdHighExcl = ConstantExpr::getSub(C0, C1);
1277
1278 // The fold has a precondition 1: C2 s>= ThresholdLow
1279 auto *Precond1 = ConstantExpr::getICmp(ICmpInst::Predicate::ICMP_SGE, C2,
1280 ThresholdLowIncl);
1281 if (!match(Precond1, m_One()))
1282 return nullptr;
1283 // The fold has a precondition 2: C2 s<= ThresholdHigh
1284 auto *Precond2 = ConstantExpr::getICmp(ICmpInst::Predicate::ICMP_SLE, C2,
1285 ThresholdHighExcl);
1286 if (!match(Precond2, m_One()))
1287 return nullptr;
1288
1289 // All good, finally emit the new pattern.
1290 Value *ShouldReplaceLow = Builder.CreateICmpSLT(X, ThresholdLowIncl);
1291 Value *ShouldReplaceHigh = Builder.CreateICmpSGE(X, ThresholdHighExcl);
1292 Value *MaybeReplacedLow =
1293 Builder.CreateSelect(ShouldReplaceLow, ReplacementLow, X);
1294 Instruction *MaybeReplacedHigh =
1295 SelectInst::Create(ShouldReplaceHigh, ReplacementHigh, MaybeReplacedLow);
1296
1297 return MaybeReplacedHigh;
1298}
1299
1300// If we have
1301// %cmp = icmp [canonical predicate] i32 %x, C0
1302// %r = select i1 %cmp, i32 %y, i32 C1
1303// Where C0 != C1 and %x may be different from %y, see if the constant that we
1304// will have if we flip the strictness of the predicate (i.e. without changing
1305// the result) is identical to the C1 in select. If it matches we can change
1306// original comparison to one with swapped predicate, reuse the constant,
1307// and swap the hands of select.
1308static Instruction *
1309tryToReuseConstantFromSelectInComparison(SelectInst &Sel, ICmpInst &Cmp,
1310 InstCombiner::BuilderTy &Builder) {
1311 ICmpInst::Predicate Pred;
1312 Value *X;
1313 Constant *C0;
1314 if (!match(&Cmp, m_OneUse(m_ICmp(
1315 Pred, m_Value(X),
1316 m_CombineAnd(m_AnyIntegralConstant(), m_Constant(C0))))))
1317 return nullptr;
1318
1319 // If comparison predicate is non-relational, we won't be able to do anything.
1320 if (ICmpInst::isEquality(Pred))
1321 return nullptr;
1322
1323 // If comparison predicate is non-canonical, then we certainly won't be able
1324 // to make it canonical; canonicalizeCmpWithConstant() already tried.
1325 if (!isCanonicalPredicate(Pred))
1326 return nullptr;
1327
1328 // If the [input] type of comparison and select type are different, lets abort
1329 // for now. We could try to compare constants with trunc/[zs]ext though.
1330 if (C0->getType() != Sel.getType())
1331 return nullptr;
1332
1333 // FIXME: are there any magic icmp predicate+constant pairs we must not touch?
1334
1335 Value *SelVal0, *SelVal1; // We do not care which one is from where.
1336 match(&Sel, m_Select(m_Value(), m_Value(SelVal0), m_Value(SelVal1)));
1337 // At least one of these values we are selecting between must be a constant
1338 // else we'll never succeed.
1339 if (!match(SelVal0, m_AnyIntegralConstant()) &&
1340 !match(SelVal1, m_AnyIntegralConstant()))
1341 return nullptr;
1342
1343 // Does this constant C match any of the `select` values?
1344 auto MatchesSelectValue = [SelVal0, SelVal1](Constant *C) {
1345 return C->isElementWiseEqual(SelVal0) || C->isElementWiseEqual(SelVal1);
1346 };
1347
1348 // If C0 *already* matches true/false value of select, we are done.
1349 if (MatchesSelectValue(C0))
1350 return nullptr;
1351
1352 // Check the constant we'd have with flipped-strictness predicate.
1353 auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(Pred, C0);
1354 if (!FlippedStrictness)
1355 return nullptr;
1356
1357 // If said constant doesn't match either, then there is no hope,
1358 if (!MatchesSelectValue(FlippedStrictness->second))
1359 return nullptr;
1360
1361 // It matched! Lets insert the new comparison just before select.
1362 InstCombiner::BuilderTy::InsertPointGuard Guard(Builder);
1363 Builder.SetInsertPoint(&Sel);
1364
1365 Pred = ICmpInst::getSwappedPredicate(Pred); // Yes, swapped.
1366 Value *NewCmp = Builder.CreateICmp(Pred, X, FlippedStrictness->second,
1367 Cmp.getName() + ".inv");
1368 Sel.setCondition(NewCmp);
1369 Sel.swapValues();
1370 Sel.swapProfMetadata();
1371
1372 return &Sel;
1373}
1374
1375/// Visit a SelectInst that has an ICmpInst as its first operand.
1376Instruction *InstCombiner::foldSelectInstWithICmp(SelectInst &SI,
1377 ICmpInst *ICI) {
1378 if (Value *V = foldSelectValueEquivalence(SI, *ICI, SQ))
1379 return replaceInstUsesWith(SI, V);
1380
1381 if (Instruction *NewSel = canonicalizeMinMaxWithConstant(SI, *ICI, Builder))
1382 return NewSel;
1383
1384 if (Instruction *NewAbs = canonicalizeAbsNabs(SI, *ICI, Builder))
1385 return NewAbs;
1386
1387 if (Instruction *NewAbs = canonicalizeClampLike(SI, *ICI, Builder))
1388 return NewAbs;
1389
1390 if (Instruction *NewSel =
1391 tryToReuseConstantFromSelectInComparison(SI, *ICI, Builder))
1392 return NewSel;
1393
1394 bool Changed = adjustMinMax(SI, *ICI);
1395
1396 if (Value *V = foldSelectICmpAnd(SI, ICI, Builder))
1397 return replaceInstUsesWith(SI, V);
1398
1399 // NOTE: if we wanted to, this is where to detect integer MIN/MAX
1400 Value *TrueVal = SI.getTrueValue();
1401 Value *FalseVal = SI.getFalseValue();
1402 ICmpInst::Predicate Pred = ICI->getPredicate();
1403 Value *CmpLHS = ICI->getOperand(0);
1404 Value *CmpRHS = ICI->getOperand(1);
1405 if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS)) {
1406 if (CmpLHS == TrueVal && Pred == ICmpInst::ICMP_EQ) {
1407 // Transform (X == C) ? X : Y -> (X == C) ? C : Y
1408 SI.setOperand(1, CmpRHS);
1409 Changed = true;
1410 } else if (CmpLHS == FalseVal && Pred == ICmpInst::ICMP_NE) {
1411 // Transform (X != C) ? Y : X -> (X != C) ? Y : C
1412 SI.setOperand(2, CmpRHS);
1413 Changed = true;
1414 }
1415 }
1416
1417 // FIXME: This code is nearly duplicated in InstSimplify. Using/refactoring
1418 // decomposeBitTestICmp() might help.
1419 {
1420 unsigned BitWidth =
1421 DL.getTypeSizeInBits(TrueVal->getType()->getScalarType());
1422 APInt MinSignedValue = APInt::getSignedMinValue(BitWidth);
1423 Value *X;
1424 const APInt *Y, *C;
1425 bool TrueWhenUnset;
1426 bool IsBitTest = false;
1427 if (ICmpInst::isEquality(Pred) &&
1428 match(CmpLHS, m_And(m_Value(X), m_Power2(Y))) &&
1429 match(CmpRHS, m_Zero())) {
1430 IsBitTest = true;
1431 TrueWhenUnset = Pred == ICmpInst::ICMP_EQ;
1432 } else if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, m_Zero())) {
1433 X = CmpLHS;
1434 Y = &MinSignedValue;
1435 IsBitTest = true;
1436 TrueWhenUnset = false;
1437 } else if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes())) {
1438 X = CmpLHS;
1439 Y = &MinSignedValue;
1440 IsBitTest = true;
1441 TrueWhenUnset = true;
1442 }
1443 if (IsBitTest) {
1444 Value *V = nullptr;
1445 // (X & Y) == 0 ? X : X ^ Y --> X & ~Y
1446 if (TrueWhenUnset && TrueVal == X &&
1447 match(FalseVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
1448 V = Builder.CreateAnd(X, ~(*Y));
1449 // (X & Y) != 0 ? X ^ Y : X --> X & ~Y
1450 else if (!TrueWhenUnset && FalseVal == X &&
1451 match(TrueVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
1452 V = Builder.CreateAnd(X, ~(*Y));
1453 // (X & Y) == 0 ? X ^ Y : X --> X | Y
1454 else if (TrueWhenUnset && FalseVal == X &&
1455 match(TrueVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
1456 V = Builder.CreateOr(X, *Y);
1457 // (X & Y) != 0 ? X : X ^ Y --> X | Y
1458 else if (!TrueWhenUnset && TrueVal == X &&
1459 match(FalseVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
1460 V = Builder.CreateOr(X, *Y);
1461
1462 if (V)
1463 return replaceInstUsesWith(SI, V);
1464 }
1465 }
1466
1467 if (Instruction *V =
1468 foldSelectICmpAndAnd(SI.getType(), ICI, TrueVal, FalseVal, Builder))
1469 return V;
1470
1471 if (Instruction *V = foldSelectCtlzToCttz(ICI, TrueVal, FalseVal, Builder))
1472 return V;
1473
1474 if (Value *V = foldSelectICmpAndOr(ICI, TrueVal, FalseVal, Builder))
1475 return replaceInstUsesWith(SI, V);
1476
1477 if (Value *V = foldSelectICmpLshrAshr(ICI, TrueVal, FalseVal, Builder))
1478 return replaceInstUsesWith(SI, V);
1479
1480 if (Value *V = foldSelectCttzCtlz(ICI, TrueVal, FalseVal, Builder))
1481 return replaceInstUsesWith(SI, V);
1482
1483 if (Value *V = canonicalizeSaturatedSubtract(ICI, TrueVal, FalseVal, Builder))
1484 return replaceInstUsesWith(SI, V);
1485
1486 if (Value *V = canonicalizeSaturatedAdd(ICI, TrueVal, FalseVal, Builder))
1487 return replaceInstUsesWith(SI, V);
1488
1489 return Changed ? &SI : nullptr;
1490}
1491
1492/// SI is a select whose condition is a PHI node (but the two may be in
1493/// different blocks). See if the true/false values (V) are live in all of the
1494/// predecessor blocks of the PHI. For example, cases like this can't be mapped:
1495///
1496/// X = phi [ C1, BB1], [C2, BB2]
1497/// Y = add
1498/// Z = select X, Y, 0
1499///
1500/// because Y is not live in BB1/BB2.
1501static bool canSelectOperandBeMappingIntoPredBlock(const Value *V,
1502 const SelectInst &SI) {
1503 // If the value is a non-instruction value like a constant or argument, it
1504 // can always be mapped.
1505 const Instruction *I = dyn_cast<Instruction>(V);
1506 if (!I) return true;
1507
1508 // If V is a PHI node defined in the same block as the condition PHI, we can
1509 // map the arguments.
1510 const PHINode *CondPHI = cast<PHINode>(SI.getCondition());
1511
1512 if (const PHINode *VP = dyn_cast<PHINode>(I))
1513 if (VP->getParent() == CondPHI->getParent())
1514 return true;
1515
1516 // Otherwise, if the PHI and select are defined in the same block and if V is
1517 // defined in a different block, then we can transform it.
1518 if (SI.getParent() == CondPHI->getParent() &&
1519 I->getParent() != CondPHI->getParent())
1520 return true;
1521
1522 // Otherwise we have a 'hard' case and we can't tell without doing more
1523 // detailed dominator based analysis, punt.
1524 return false;
1525}
1526
1527/// We have an SPF (e.g. a min or max) of an SPF of the form:
1528/// SPF2(SPF1(A, B), C)
1529Instruction *InstCombiner::foldSPFofSPF(Instruction *Inner,
1530 SelectPatternFlavor SPF1,
1531 Value *A, Value *B,
1532 Instruction &Outer,
1533 SelectPatternFlavor SPF2, Value *C) {
1534 if (Outer.getType() != Inner->getType())
1535 return nullptr;
1536
1537 if (C == A || C == B) {
1538 // MAX(MAX(A, B), B) -> MAX(A, B)
1539 // MIN(MIN(a, b), a) -> MIN(a, b)
1540 // TODO: This could be done in instsimplify.
1541 if (SPF1 == SPF2 && SelectPatternResult::isMinOrMax(SPF1))
1542 return replaceInstUsesWith(Outer, Inner);
1543
1544 // MAX(MIN(a, b), a) -> a
1545 // MIN(MAX(a, b), a) -> a
1546 // TODO: This could be done in instsimplify.
1547 if ((SPF1 == SPF_SMIN && SPF2 == SPF_SMAX) ||
1548 (SPF1 == SPF_SMAX && SPF2 == SPF_SMIN) ||
1549 (SPF1 == SPF_UMIN && SPF2 == SPF_UMAX) ||
1550 (SPF1 == SPF_UMAX && SPF2 == SPF_UMIN))
1551 return replaceInstUsesWith(Outer, C);
1552 }
1553
1554 if (SPF1 == SPF2) {
1555 const APInt *CB, *CC;
1556 if (match(B, m_APInt(CB)) && match(C, m_APInt(CC))) {
1557 // MIN(MIN(A, 23), 97) -> MIN(A, 23)
1558 // MAX(MAX(A, 97), 23) -> MAX(A, 97)
1559 // TODO: This could be done in instsimplify.
1560 if ((SPF1 == SPF_UMIN && CB->ule(*CC)) ||
1561 (SPF1 == SPF_SMIN && CB->sle(*CC)) ||
1562 (SPF1 == SPF_UMAX && CB->uge(*CC)) ||
1563 (SPF1 == SPF_SMAX && CB->sge(*CC)))
1564 return replaceInstUsesWith(Outer, Inner);
1565
1566 // MIN(MIN(A, 97), 23) -> MIN(A, 23)
1567 // MAX(MAX(A, 23), 97) -> MAX(A, 97)
1568 if ((SPF1 == SPF_UMIN && CB->ugt(*CC)) ||
1569 (SPF1 == SPF_SMIN && CB->sgt(*CC)) ||
1570 (SPF1 == SPF_UMAX && CB->ult(*CC)) ||
1571 (SPF1 == SPF_SMAX && CB->slt(*CC))) {
1572 Outer.replaceUsesOfWith(Inner, A);
1573 return &Outer;
1574 }
1575 }
1576 }
1577
1578 // max(max(A, B), min(A, B)) --> max(A, B)
1579 // min(min(A, B), max(A, B)) --> min(A, B)
1580 // TODO: This could be done in instsimplify.
1581 if (SPF1 == SPF2 &&
1582 ((SPF1 == SPF_UMIN && match(C, m_c_UMax(m_Specific(A), m_Specific(B)))) ||
1583 (SPF1 == SPF_SMIN && match(C, m_c_SMax(m_Specific(A), m_Specific(B)))) ||
1584 (SPF1 == SPF_UMAX && match(C, m_c_UMin(m_Specific(A), m_Specific(B)))) ||
1585 (SPF1 == SPF_SMAX && match(C, m_c_SMin(m_Specific(A), m_Specific(B))))))
1586 return replaceInstUsesWith(Outer, Inner);
1587
1588 // ABS(ABS(X)) -> ABS(X)
1589 // NABS(NABS(X)) -> NABS(X)
1590 // TODO: This could be done in instsimplify.
1591 if (SPF1 == SPF2 && (SPF1 == SPF_ABS || SPF1 == SPF_NABS)) {
1592 return replaceInstUsesWith(Outer, Inner);
1593 }
1594
1595 // ABS(NABS(X)) -> ABS(X)
1596 // NABS(ABS(X)) -> NABS(X)
1597 if ((SPF1 == SPF_ABS && SPF2 == SPF_NABS) ||
1598 (SPF1 == SPF_NABS && SPF2 == SPF_ABS)) {
1599 SelectInst *SI = cast<SelectInst>(Inner);
1600 Value *NewSI =
1601 Builder.CreateSelect(SI->getCondition(), SI->getFalseValue(),
1602 SI->getTrueValue(), SI->getName(), SI);
1603 return replaceInstUsesWith(Outer, NewSI);
1604 }
1605
1606 auto IsFreeOrProfitableToInvert =
1607 [&](Value *V, Value *&NotV, bool &ElidesXor) {
1608 if (match(V, m_Not(m_Value(NotV)))) {
1609 // If V has at most 2 uses then we can get rid of the xor operation
1610 // entirely.
1611 ElidesXor |= !V->hasNUsesOrMore(3);
1612 return true;
1613 }
1614
1615 if (isFreeToInvert(V, !V->hasNUsesOrMore(3))) {
1616 NotV = nullptr;
1617 return true;
1618 }
1619
1620 return false;
1621 };
1622
1623 Value *NotA, *NotB, *NotC;
1624 bool ElidesXor = false;
1625
1626 // MIN(MIN(~A, ~B), ~C) == ~MAX(MAX(A, B), C)
1627 // MIN(MAX(~A, ~B), ~C) == ~MAX(MIN(A, B), C)
1628 // MAX(MIN(~A, ~B), ~C) == ~MIN(MAX(A, B), C)
1629 // MAX(MAX(~A, ~B), ~C) == ~MIN(MIN(A, B), C)
1630 //
1631 // This transform is performance neutral if we can elide at least one xor from
1632 // the set of three operands, since we'll be tacking on an xor at the very
1633 // end.
1634 if (SelectPatternResult::isMinOrMax(SPF1) &&
1635 SelectPatternResult::isMinOrMax(SPF2) &&
1636 IsFreeOrProfitableToInvert(A, NotA, ElidesXor) &&
1637 IsFreeOrProfitableToInvert(B, NotB, ElidesXor) &&
1638 IsFreeOrProfitableToInvert(C, NotC, ElidesXor) && ElidesXor) {
1639 if (!NotA)
1640 NotA = Builder.CreateNot(A);
1641 if (!NotB)
1642 NotB = Builder.CreateNot(B);
1643 if (!NotC)
1644 NotC = Builder.CreateNot(C);
1645
1646 Value *NewInner = createMinMax(Builder, getInverseMinMaxFlavor(SPF1), NotA,
1647 NotB);
1648 Value *NewOuter = Builder.CreateNot(
1649 createMinMax(Builder, getInverseMinMaxFlavor(SPF2), NewInner, NotC));
1650 return replaceInstUsesWith(Outer, NewOuter);
1651 }
1652
1653 return nullptr;
1654}
1655
1656/// Turn select C, (X + Y), (X - Y) --> (X + (select C, Y, (-Y))).
1657/// This is even legal for FP.
1658static Instruction *foldAddSubSelect(SelectInst &SI,
1659 InstCombiner::BuilderTy &Builder) {
1660 Value *CondVal = SI.getCondition();
1661 Value *TrueVal = SI.getTrueValue();
1662 Value *FalseVal = SI.getFalseValue();
1663 auto *TI = dyn_cast<Instruction>(TrueVal);
1664 auto *FI = dyn_cast<Instruction>(FalseVal);
1665 if (!TI || !FI || !TI->hasOneUse() || !FI->hasOneUse())
1666 return nullptr;
1667
1668 Instruction *AddOp = nullptr, *SubOp = nullptr;
1669 if ((TI->getOpcode() == Instruction::Sub &&
1670 FI->getOpcode() == Instruction::Add) ||
1671 (TI->getOpcode() == Instruction::FSub &&
1672 FI->getOpcode() == Instruction::FAdd)) {
1673 AddOp = FI;
1674 SubOp = TI;
1675 } else if ((FI->getOpcode() == Instruction::Sub &&
1676 TI->getOpcode() == Instruction::Add) ||
1677 (FI->getOpcode() == Instruction::FSub &&
1678 TI->getOpcode() == Instruction::FAdd)) {
1679 AddOp = TI;
1680 SubOp = FI;
1681 }
1682
1683 if (AddOp) {
1684 Value *OtherAddOp = nullptr;
1685 if (SubOp->getOperand(0) == AddOp->getOperand(0)) {
1686 OtherAddOp = AddOp->getOperand(1);
1687 } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) {
1688 OtherAddOp = AddOp->getOperand(0);
1689 }
1690
1691 if (OtherAddOp) {
1692 // So at this point we know we have (Y -> OtherAddOp):
1693 // select C, (add X, Y), (sub X, Z)
1694 Value *NegVal; // Compute -Z
1695 if (SI.getType()->isFPOrFPVectorTy()) {
1696 NegVal = Builder.CreateFNeg(SubOp->getOperand(1));
1697 if (Instruction *NegInst = dyn_cast<Instruction>(NegVal)) {
1698 FastMathFlags Flags = AddOp->getFastMathFlags();
1699 Flags &= SubOp->getFastMathFlags();
1700 NegInst->setFastMathFlags(Flags);
1701 }
1702 } else {
1703 NegVal = Builder.CreateNeg(SubOp->getOperand(1));
1704 }
1705
1706 Value *NewTrueOp = OtherAddOp;
1707 Value *NewFalseOp = NegVal;
1708 if (AddOp != TI)
1709 std::swap(NewTrueOp, NewFalseOp);
1710 Value *NewSel = Builder.CreateSelect(CondVal, NewTrueOp, NewFalseOp,
1711 SI.getName() + ".p", &SI);
1712
1713 if (SI.getType()->isFPOrFPVectorTy()) {
1714 Instruction *RI =
1715 BinaryOperator::CreateFAdd(SubOp->getOperand(0), NewSel);
1716
1717 FastMathFlags Flags = AddOp->getFastMathFlags();
1718 Flags &= SubOp->getFastMathFlags();
1719 RI->setFastMathFlags(Flags);
1720 return RI;
1721 } else
1722 return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel);
1723 }
1724 }
1725 return nullptr;
1726}
1727
1728Instruction *InstCombiner::foldSelectExtConst(SelectInst &Sel) {
1729 Constant *C;
1730 if (!match(Sel.getTrueValue(), m_Constant(C)) &&
1731 !match(Sel.getFalseValue(), m_Constant(C)))
1732 return nullptr;
1733
1734 Instruction *ExtInst;
1735 if (!match(Sel.getTrueValue(), m_Instruction(ExtInst)) &&
1736 !match(Sel.getFalseValue(), m_Instruction(ExtInst)))
1737 return nullptr;
1738
1739 auto ExtOpcode = ExtInst->getOpcode();
1740 if (ExtOpcode != Instruction::ZExt && ExtOpcode != Instruction::SExt)
1741 return nullptr;
1742
1743 // If we are extending from a boolean type or if we can create a select that
1744 // has the same size operands as its condition, try to narrow the select.
1745 Value *X = ExtInst->getOperand(0);
1746 Type *SmallType = X->getType();
1747 Value *Cond = Sel.getCondition();
1748 auto *Cmp = dyn_cast<CmpInst>(Cond);
1749 if (!SmallType->isIntOrIntVectorTy(1) &&
1750 (!Cmp || Cmp->getOperand(0)->getType() != SmallType))
1751 return nullptr;
1752
1753 // If the constant is the same after truncation to the smaller type and
1754 // extension to the original type, we can narrow the select.
1755 Type *SelType = Sel.getType();
1756 Constant *TruncC = ConstantExpr::getTrunc(C, SmallType);
1757 Constant *ExtC = ConstantExpr::getCast(ExtOpcode, TruncC, SelType);
1758 if (ExtC == C) {
1759 Value *TruncCVal = cast<Value>(TruncC);
1760 if (ExtInst == Sel.getFalseValue())
1761 std::swap(X, TruncCVal);
1762
1763 // select Cond, (ext X), C --> ext(select Cond, X, C')
1764 // select Cond, C, (ext X) --> ext(select Cond, C', X)
1765 Value *NewSel = Builder.CreateSelect(Cond, X, TruncCVal, "narrow", &Sel);
1766 return CastInst::Create(Instruction::CastOps(ExtOpcode), NewSel, SelType);
1767 }
1768
1769 // If one arm of the select is the extend of the condition, replace that arm
1770 // with the extension of the appropriate known bool value.
1771 if (Cond == X) {
1772 if (ExtInst == Sel.getTrueValue()) {
1773 // select X, (sext X), C --> select X, -1, C
1774 // select X, (zext X), C --> select X, 1, C
1775 Constant *One = ConstantInt::getTrue(SmallType);
1776 Constant *AllOnesOrOne = ConstantExpr::getCast(ExtOpcode, One, SelType);
1777 return SelectInst::Create(Cond, AllOnesOrOne, C, "", nullptr, &Sel);
1778 } else {
1779 // select X, C, (sext X) --> select X, C, 0
1780 // select X, C, (zext X) --> select X, C, 0
1781 Constant *Zero = ConstantInt::getNullValue(SelType);
1782 return SelectInst::Create(Cond, C, Zero, "", nullptr, &Sel);
1783 }
1784 }
1785
1786 return nullptr;
1787}
1788
1789/// Try to transform a vector select with a constant condition vector into a
1790/// shuffle for easier combining with other shuffles and insert/extract.
1791static Instruction *canonicalizeSelectToShuffle(SelectInst &SI) {
1792 Value *CondVal = SI.getCondition();
1793 Constant *CondC;
1794 if (!CondVal->getType()->isVectorTy() || !match(CondVal, m_Constant(CondC)))
8
Taking true branch
1795 return nullptr;
9
Returning null pointer, which participates in a condition later
1796
1797 unsigned NumElts = CondVal->getType()->getVectorNumElements();
1798 SmallVector<Constant *, 16> Mask;
1799 Mask.reserve(NumElts);
1800 Type *Int32Ty = Type::getInt32Ty(CondVal->getContext());
1801 for (unsigned i = 0; i != NumElts; ++i) {
1802 Constant *Elt = CondC->getAggregateElement(i);
1803 if (!Elt)
1804 return nullptr;
1805
1806 if (Elt->isOneValue()) {
1807 // If the select condition element is true, choose from the 1st vector.
1808 Mask.push_back(ConstantInt::get(Int32Ty, i));
1809 } else if (Elt->isNullValue()) {
1810 // If the select condition element is false, choose from the 2nd vector.
1811 Mask.push_back(ConstantInt::get(Int32Ty, i + NumElts));
1812 } else if (isa<UndefValue>(Elt)) {
1813 // Undef in a select condition (choose one of the operands) does not mean
1814 // the same thing as undef in a shuffle mask (any value is acceptable), so
1815 // give up.
1816 return nullptr;
1817 } else {
1818 // Bail out on a constant expression.
1819 return nullptr;
1820 }
1821 }
1822
1823 return new ShuffleVectorInst(SI.getTrueValue(), SI.getFalseValue(),
1824 ConstantVector::get(Mask));
1825}
1826
1827/// If we have a select of vectors with a scalar condition, try to convert that
1828/// to a vector select by splatting the condition. A splat may get folded with
1829/// other operations in IR and having all operands of a select be vector types
1830/// is likely better for vector codegen.
1831static Instruction *canonicalizeScalarSelectOfVecs(
1832 SelectInst &Sel, InstCombiner::BuilderTy &Builder) {
1833 Type *Ty = Sel.getType();
1834 if (!Ty->isVectorTy())
13
Calling 'Type::isVectorTy'
16
Returning from 'Type::isVectorTy'
17
Taking true branch
1835 return nullptr;
18
Returning null pointer, which participates in a condition later
1836
1837 // We can replace a single-use extract with constant index.
1838 Value *Cond = Sel.getCondition();
1839 if (!match(Cond, m_OneUse(m_ExtractElement(m_Value(), m_ConstantInt()))))
1840 return nullptr;
1841
1842 // select (extelt V, Index), T, F --> select (splat V, Index), T, F
1843 // Splatting the extracted condition reduces code (we could directly create a
1844 // splat shuffle of the source vector to eliminate the intermediate step).
1845 unsigned NumElts = Ty->getVectorNumElements();
1846 Value *SplatCond = Builder.CreateVectorSplat(NumElts, Cond);
1847 Sel.setCondition(SplatCond);
1848 return &Sel;
1849}
1850
1851/// Reuse bitcasted operands between a compare and select:
1852/// select (cmp (bitcast C), (bitcast D)), (bitcast' C), (bitcast' D) -->
1853/// bitcast (select (cmp (bitcast C), (bitcast D)), (bitcast C), (bitcast D))
1854static Instruction *foldSelectCmpBitcasts(SelectInst &Sel,
1855 InstCombiner::BuilderTy &Builder) {
1856 Value *Cond = Sel.getCondition();
1857 Value *TVal = Sel.getTrueValue();
1858 Value *FVal = Sel.getFalseValue();
1859
1860 CmpInst::Predicate Pred;
1861 Value *A, *B;
1862 if (!match(Cond, m_Cmp(Pred, m_Value(A), m_Value(B))))
1863 return nullptr;
1864
1865 // The select condition is a compare instruction. If the select's true/false
1866 // values are already the same as the compare operands, there's nothing to do.
1867 if (TVal == A || TVal == B || FVal == A || FVal == B)
1868 return nullptr;
1869
1870 Value *C, *D;
1871 if (!match(A, m_BitCast(m_Value(C))) || !match(B, m_BitCast(m_Value(D))))
1872 return nullptr;
1873
1874 // select (cmp (bitcast C), (bitcast D)), (bitcast TSrc), (bitcast FSrc)
1875 Value *TSrc, *FSrc;
1876 if (!match(TVal, m_BitCast(m_Value(TSrc))) ||
1877 !match(FVal, m_BitCast(m_Value(FSrc))))
1878 return nullptr;
1879
1880 // If the select true/false values are *different bitcasts* of the same source
1881 // operands, make the select operands the same as the compare operands and
1882 // cast the result. This is the canonical select form for min/max.
1883 Value *NewSel;
1884 if (TSrc == C && FSrc == D) {
1885 // select (cmp (bitcast C), (bitcast D)), (bitcast' C), (bitcast' D) -->
1886 // bitcast (select (cmp A, B), A, B)
1887 NewSel = Builder.CreateSelect(Cond, A, B, "", &Sel);
1888 } else if (TSrc == D && FSrc == C) {
1889 // select (cmp (bitcast C), (bitcast D)), (bitcast' D), (bitcast' C) -->
1890 // bitcast (select (cmp A, B), B, A)
1891 NewSel = Builder.CreateSelect(Cond, B, A, "", &Sel);
1892 } else {
1893 return nullptr;
1894 }
1895 return CastInst::CreateBitOrPointerCast(NewSel, Sel.getType());
1896}
1897
1898/// Try to eliminate select instructions that test the returned flag of cmpxchg
1899/// instructions.
1900///
1901/// If a select instruction tests the returned flag of a cmpxchg instruction and
1902/// selects between the returned value of the cmpxchg instruction its compare
1903/// operand, the result of the select will always be equal to its false value.
1904/// For example:
1905///
1906/// %0 = cmpxchg i64* %ptr, i64 %compare, i64 %new_value seq_cst seq_cst
1907/// %1 = extractvalue { i64, i1 } %0, 1
1908/// %2 = extractvalue { i64, i1 } %0, 0
1909/// %3 = select i1 %1, i64 %compare, i64 %2
1910/// ret i64 %3
1911///
1912/// The returned value of the cmpxchg instruction (%2) is the original value
1913/// located at %ptr prior to any update. If the cmpxchg operation succeeds, %2
1914/// must have been equal to %compare. Thus, the result of the select is always
1915/// equal to %2, and the code can be simplified to:
1916///
1917/// %0 = cmpxchg i64* %ptr, i64 %compare, i64 %new_value seq_cst seq_cst
1918/// %1 = extractvalue { i64, i1 } %0, 0
1919/// ret i64 %1
1920///
1921static Instruction *foldSelectCmpXchg(SelectInst &SI) {
1922 // A helper that determines if V is an extractvalue instruction whose
1923 // aggregate operand is a cmpxchg instruction and whose single index is equal
1924 // to I. If such conditions are true, the helper returns the cmpxchg
1925 // instruction; otherwise, a nullptr is returned.
1926 auto isExtractFromCmpXchg = [](Value *V, unsigned I) -> AtomicCmpXchgInst * {
1927 auto *Extract = dyn_cast<ExtractValueInst>(V);
1928 if (!Extract)
1929 return nullptr;
1930 if (Extract->getIndices()[0] != I)
1931 return nullptr;
1932 return dyn_cast<AtomicCmpXchgInst>(Extract->getAggregateOperand());
1933 };
1934
1935 // If the select has a single user, and this user is a select instruction that
1936 // we can simplify, skip the cmpxchg simplification for now.
1937 if (SI.hasOneUse())
1938 if (auto *Select = dyn_cast<SelectInst>(SI.user_back()))
1939 if (Select->getCondition() == SI.getCondition())
1940 if (Select->getFalseValue() == SI.getTrueValue() ||
1941 Select->getTrueValue() == SI.getFalseValue())
1942 return nullptr;
1943
1944 // Ensure the select condition is the returned flag of a cmpxchg instruction.
1945 auto *CmpXchg = isExtractFromCmpXchg(SI.getCondition(), 1);
1946 if (!CmpXchg)
1947 return nullptr;
1948
1949 // Check the true value case: The true value of the select is the returned
1950 // value of the same cmpxchg used by the condition, and the false value is the
1951 // cmpxchg instruction's compare operand.
1952 if (auto *X = isExtractFromCmpXchg(SI.getTrueValue(), 0))
1953 if (X == CmpXchg && X->getCompareOperand() == SI.getFalseValue()) {
1954 SI.setTrueValue(SI.getFalseValue());
1955 return &SI;
1956 }
1957
1958 // Check the false value case: The false value of the select is the returned
1959 // value of the same cmpxchg used by the condition, and the true value is the
1960 // cmpxchg instruction's compare operand.
1961 if (auto *X = isExtractFromCmpXchg(SI.getFalseValue(), 0))
1962 if (X == CmpXchg && X->getCompareOperand() == SI.getTrueValue()) {
1963 SI.setTrueValue(SI.getFalseValue());
1964 return &SI;
1965 }
1966
1967 return nullptr;
1968}
1969
1970static Instruction *moveAddAfterMinMax(SelectPatternFlavor SPF, Value *X,
1971 Value *Y,
1972 InstCombiner::BuilderTy &Builder) {
1973 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-10~svn374877/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 1973, __PRETTY_FUNCTION__))
;
1974 bool IsUnsigned = SPF == SelectPatternFlavor::SPF_UMIN ||
1975 SPF == SelectPatternFlavor::SPF_UMAX;
1976 // TODO: If InstSimplify could fold all cases where C2 <= C1, we could change
1977 // the constant value check to an assert.
1978 Value *A;
1979 const APInt *C1, *C2;
1980 if (IsUnsigned && match(X, m_NUWAdd(m_Value(A), m_APInt(C1))) &&
1981 match(Y, m_APInt(C2)) && C2->uge(*C1) && X->hasNUses(2)) {
1982 // umin (add nuw A, C1), C2 --> add nuw (umin A, C2 - C1), C1
1983 // umax (add nuw A, C1), C2 --> add nuw (umax A, C2 - C1), C1
1984 Value *NewMinMax = createMinMax(Builder, SPF, A,
1985 ConstantInt::get(X->getType(), *C2 - *C1));
1986 return BinaryOperator::CreateNUW(BinaryOperator::Add, NewMinMax,
1987 ConstantInt::get(X->getType(), *C1));
1988 }
1989
1990 if (!IsUnsigned && match(X, m_NSWAdd(m_Value(A), m_APInt(C1))) &&
1991 match(Y, m_APInt(C2)) && X->hasNUses(2)) {
1992 bool Overflow;
1993 APInt Diff = C2->ssub_ov(*C1, Overflow);
1994 if (!Overflow) {
1995 // smin (add nsw A, C1), C2 --> add nsw (smin A, C2 - C1), C1
1996 // smax (add nsw A, C1), C2 --> add nsw (smax A, C2 - C1), C1
1997 Value *NewMinMax = createMinMax(Builder, SPF, A,
1998 ConstantInt::get(X->getType(), Diff));
1999 return BinaryOperator::CreateNSW(BinaryOperator::Add, NewMinMax,
2000 ConstantInt::get(X->getType(), *C1));
2001 }
2002 }
2003
2004 return nullptr;
2005}
2006
2007/// Reduce a sequence of min/max with a common operand.
2008static Instruction *factorizeMinMaxTree(SelectPatternFlavor SPF, Value *LHS,
2009 Value *RHS,
2010 InstCombiner::BuilderTy &Builder) {
2011 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-10~svn374877/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 2011, __PRETTY_FUNCTION__))
;
2012 // TODO: Allow FP min/max with nnan/nsz.
2013 if (!LHS->getType()->isIntOrIntVectorTy())
2014 return nullptr;
2015
2016 // Match 3 of the same min/max ops. Example: umin(umin(), umin()).
2017 Value *A, *B, *C, *D;
2018 SelectPatternResult L = matchSelectPattern(LHS, A, B);
2019 SelectPatternResult R = matchSelectPattern(RHS, C, D);
2020 if (SPF != L.Flavor || L.Flavor != R.Flavor)
2021 return nullptr;
2022
2023 // Look for a common operand. The use checks are different than usual because
2024 // a min/max pattern typically has 2 uses of each op: 1 by the cmp and 1 by
2025 // the select.
2026 Value *MinMaxOp = nullptr;
2027 Value *ThirdOp = nullptr;
2028 if (!LHS->hasNUsesOrMore(3) && RHS->hasNUsesOrMore(3)) {
2029 // If the LHS is only used in this chain and the RHS is used outside of it,
2030 // reuse the RHS min/max because that will eliminate the LHS.
2031 if (D == A || C == A) {
2032 // min(min(a, b), min(c, a)) --> min(min(c, a), b)
2033 // min(min(a, b), min(a, d)) --> min(min(a, d), b)
2034 MinMaxOp = RHS;
2035 ThirdOp = B;
2036 } else if (D == B || C == B) {
2037 // min(min(a, b), min(c, b)) --> min(min(c, b), a)
2038 // min(min(a, b), min(b, d)) --> min(min(b, d), a)
2039 MinMaxOp = RHS;
2040 ThirdOp = A;
2041 }
2042 } else if (!RHS->hasNUsesOrMore(3)) {
2043 // Reuse the LHS. This will eliminate the RHS.
2044 if (D == A || D == B) {
2045 // min(min(a, b), min(c, a)) --> min(min(a, b), c)
2046 // min(min(a, b), min(c, b)) --> min(min(a, b), c)
2047 MinMaxOp = LHS;
2048 ThirdOp = C;
2049 } else if (C == A || C == B) {
2050 // min(min(a, b), min(b, d)) --> min(min(a, b), d)
2051 // min(min(a, b), min(c, b)) --> min(min(a, b), d)
2052 MinMaxOp = LHS;
2053 ThirdOp = D;
2054 }
2055 }
2056 if (!MinMaxOp || !ThirdOp)
2057 return nullptr;
2058
2059 CmpInst::Predicate P = getMinMaxPred(SPF);
2060 Value *CmpABC = Builder.CreateICmp(P, MinMaxOp, ThirdOp);
2061 return SelectInst::Create(CmpABC, MinMaxOp, ThirdOp);
2062}
2063
2064/// Try to reduce a rotate pattern that includes a compare and select into a
2065/// funnel shift intrinsic. Example:
2066/// rotl32(a, b) --> (b == 0 ? a : ((a >> (32 - b)) | (a << b)))
2067/// --> call llvm.fshl.i32(a, a, b)
2068static Instruction *foldSelectRotate(SelectInst &Sel) {
2069 // The false value of the select must be a rotate of the true value.
2070 Value *Or0, *Or1;
2071 if (!match(Sel.getFalseValue(), m_OneUse(m_Or(m_Value(Or0), m_Value(Or1)))))
2072 return nullptr;
2073
2074 Value *TVal = Sel.getTrueValue();
2075 Value *SA0, *SA1;
2076 if (!match(Or0, m_OneUse(m_LogicalShift(m_Specific(TVal), m_Value(SA0)))) ||
2077 !match(Or1, m_OneUse(m_LogicalShift(m_Specific(TVal), m_Value(SA1)))))
2078 return nullptr;
2079
2080 auto ShiftOpcode0 = cast<BinaryOperator>(Or0)->getOpcode();
2081 auto ShiftOpcode1 = cast<BinaryOperator>(Or1)->getOpcode();
2082 if (ShiftOpcode0 == ShiftOpcode1)
2083 return nullptr;
2084
2085 // We have one of these patterns so far:
2086 // select ?, TVal, (or (lshr TVal, SA0), (shl TVal, SA1))
2087 // select ?, TVal, (or (shl TVal, SA0), (lshr TVal, SA1))
2088 // This must be a power-of-2 rotate for a bitmasking transform to be valid.
2089 unsigned Width = Sel.getType()->getScalarSizeInBits();
2090 if (!isPowerOf2_32(Width))
2091 return nullptr;
2092
2093 // Check the shift amounts to see if they are an opposite pair.
2094 Value *ShAmt;
2095 if (match(SA1, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(SA0)))))
2096 ShAmt = SA0;
2097 else if (match(SA0, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(SA1)))))
2098 ShAmt = SA1;
2099 else
2100 return nullptr;
2101
2102 // Finally, see if the select is filtering out a shift-by-zero.
2103 Value *Cond = Sel.getCondition();
2104 ICmpInst::Predicate Pred;
2105 if (!match(Cond, m_OneUse(m_ICmp(Pred, m_Specific(ShAmt), m_ZeroInt()))) ||
2106 Pred != ICmpInst::ICMP_EQ)
2107 return nullptr;
2108
2109 // This is a rotate that avoids shift-by-bitwidth UB in a suboptimal way.
2110 // Convert to funnel shift intrinsic.
2111 bool IsFshl = (ShAmt == SA0 && ShiftOpcode0 == BinaryOperator::Shl) ||
2112 (ShAmt == SA1 && ShiftOpcode1 == BinaryOperator::Shl);
2113 Intrinsic::ID IID = IsFshl ? Intrinsic::fshl : Intrinsic::fshr;
2114 Function *F = Intrinsic::getDeclaration(Sel.getModule(), IID, Sel.getType());
2115 return IntrinsicInst::Create(F, { TVal, TVal, ShAmt });
2116}
2117
2118Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
2119 Value *CondVal = SI.getCondition();
2120 Value *TrueVal = SI.getTrueValue();
1
'TrueVal' initialized here
2121 Value *FalseVal = SI.getFalseValue();
2122 Type *SelType = SI.getType();
2123
2124 // FIXME: Remove this workaround when freeze related patches are done.
2125 // For select with undef operand which feeds into an equality comparison,
2126 // don't simplify it so loop unswitch can know the equality comparison
2127 // may have an undef operand. This is a workaround for PR31652 caused by
2128 // descrepancy about branch on undef between LoopUnswitch and GVN.
2129 if (isa<UndefValue>(TrueVal) || isa<UndefValue>(FalseVal)) {
2
Assuming 'TrueVal' is not a 'UndefValue'
3
Assuming 'FalseVal' is not a 'UndefValue'
4
Taking false branch
2130 if (llvm::any_of(SI.users(), [&](User *U) {
2131 ICmpInst *CI = dyn_cast<ICmpInst>(U);
2132 if (CI && CI->isEquality())
2133 return true;
2134 return false;
2135 })) {
2136 return nullptr;
2137 }
2138 }
2139
2140 if (Value *V = SimplifySelectInst(CondVal, TrueVal, FalseVal,
5
Assuming 'V' is null
6
Taking false branch
2141 SQ.getWithInstruction(&SI)))
2142 return replaceInstUsesWith(SI, V);
2143
2144 if (Instruction *I
10.1
'I' is null
10.1
'I' is null
10.1
'I' is null
10.1
'I' is null
10.1
'I' is null
= canonicalizeSelectToShuffle(SI))
7
Calling 'canonicalizeSelectToShuffle'
10
Returning from 'canonicalizeSelectToShuffle'
11
Taking false branch
2145 return I;
2146
2147 if (Instruction *I
19.1
'I' is null
19.1
'I' is null
19.1
'I' is null
19.1
'I' is null
19.1
'I' is null
= canonicalizeScalarSelectOfVecs(SI, Builder))
12
Calling 'canonicalizeScalarSelectOfVecs'
19
Returning from 'canonicalizeScalarSelectOfVecs'
20
Taking false branch
2148 return I;
2149
2150 // Canonicalize a one-use integer compare with a non-canonical predicate by
2151 // inverting the predicate and swapping the select operands. This matches a
2152 // compare canonicalization for conditional branches.
2153 // TODO: Should we do the same for FP compares?
2154 CmpInst::Predicate Pred;
2155 if (match(CondVal, m_OneUse(m_ICmp(Pred, m_Value(), m_Value()))) &&
21
Taking false branch
2156 !isCanonicalPredicate(Pred)) {
2157 // Swap true/false values and condition.
2158 CmpInst *Cond = cast<CmpInst>(CondVal);
2159 Cond->setPredicate(CmpInst::getInversePredicate(Pred));
2160 SI.setOperand(1, FalseVal);
2161 SI.setOperand(2, TrueVal);
2162 SI.swapProfMetadata();
2163 Worklist.Add(Cond);
2164 return &SI;
2165 }
2166
2167 if (SelType->isIntOrIntVectorTy(1) &&
22
Assuming the condition is false
2168 TrueVal->getType() == CondVal->getType()) {
2169 if (match(TrueVal, m_One())) {
2170 // Change: A = select B, true, C --> A = or B, C
2171 return BinaryOperator::CreateOr(CondVal, FalseVal);
2172 }
2173 if (match(TrueVal, m_Zero())) {
2174 // Change: A = select B, false, C --> A = and !B, C
2175 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
2176 return BinaryOperator::CreateAnd(NotCond, FalseVal);
2177 }
2178 if (match(FalseVal, m_Zero())) {
2179 // Change: A = select B, C, false --> A = and B, C
2180 return BinaryOperator::CreateAnd(CondVal, TrueVal);
2181 }
2182 if (match(FalseVal, m_One())) {
2183 // Change: A = select B, C, true --> A = or !B, C
2184 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
2185 return BinaryOperator::CreateOr(NotCond, TrueVal);
2186 }
2187
2188 // select a, a, b -> a | b
2189 // select a, b, a -> a & b
2190 if (CondVal == TrueVal)
2191 return BinaryOperator::CreateOr(CondVal, FalseVal);
2192 if (CondVal == FalseVal)
2193 return BinaryOperator::CreateAnd(CondVal, TrueVal);
2194
2195 // select a, ~a, b -> (~a) & b
2196 // select a, b, ~a -> (~a) | b
2197 if (match(TrueVal, m_Not(m_Specific(CondVal))))
2198 return BinaryOperator::CreateAnd(TrueVal, FalseVal);
2199 if (match(FalseVal, m_Not(m_Specific(CondVal))))
2200 return BinaryOperator::CreateOr(TrueVal, FalseVal);
2201 }
2202
2203 // Selecting between two integer or vector splat integer constants?
2204 //
2205 // Note that we don't handle a scalar select of vectors:
2206 // select i1 %c, <2 x i8> <1, 1>, <2 x i8> <0, 0>
2207 // because that may need 3 instructions to splat the condition value:
2208 // extend, insertelement, shufflevector.
2209 if (SelType->isIntOrIntVectorTy() &&
23
Calling 'Type::isIntOrIntVectorTy'
29
Returning from 'Type::isIntOrIntVectorTy'
2210 CondVal->getType()->isVectorTy() == SelType->isVectorTy()) {
2211 // select C, 1, 0 -> zext C to int
2212 if (match(TrueVal, m_One()) && match(FalseVal, m_Zero()))
2213 return new ZExtInst(CondVal, SelType);
2214
2215 // select C, -1, 0 -> sext C to int
2216 if (match(TrueVal, m_AllOnes()) && match(FalseVal, m_Zero()))
2217 return new SExtInst(CondVal, SelType);
2218
2219 // select C, 0, 1 -> zext !C to int
2220 if (match(TrueVal, m_Zero()) && match(FalseVal, m_One())) {
2221 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
2222 return new ZExtInst(NotCond, SelType);
2223 }
2224
2225 // select C, 0, -1 -> sext !C to int
2226 if (match(TrueVal, m_Zero()) && match(FalseVal, m_AllOnes())) {
2227 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
2228 return new SExtInst(NotCond, SelType);
2229 }
2230 }
2231
2232 // See if we are selecting two values based on a comparison of the two values.
2233 if (FCmpInst *FCI
30.1
'FCI' is non-null
30.1
'FCI' is non-null
30.1
'FCI' is non-null
30.1
'FCI' is non-null
30.1
'FCI' is non-null
= dyn_cast<FCmpInst>(CondVal)) {
30
Assuming 'CondVal' is a 'FCmpInst'
31
Taking true branch
2234 if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) {
32
Assuming pointer value is null
33
Assuming the condition is true
34
Taking true branch
2235 // Canonicalize to use ordered comparisons by swapping the select
2236 // operands.
2237 //
2238 // e.g.
2239 // (X ugt Y) ? X : Y -> (X ole Y) ? Y : X
2240 if (FCI->hasOneUse() && FCmpInst::isUnordered(FCI->getPredicate())) {
35
Calling 'Value::hasOneUse'
39
Returning from 'Value::hasOneUse'
40
Assuming the condition is true
41
Assuming the condition is true
42
Taking true branch
2241 FCmpInst::Predicate InvPred = FCI->getInversePredicate();
2242 IRBuilder<>::FastMathFlagGuard FMFG(Builder);
2243 Builder.setFastMathFlags(FCI->getFastMathFlags());
2244 Value *NewCond = Builder.CreateFCmp(InvPred, TrueVal, FalseVal,
43
Passing null pointer value via 2nd parameter 'LHS'
44
Calling 'IRBuilder::CreateFCmp'
2245 FCI->getName() + ".inv");
2246
2247 return SelectInst::Create(NewCond, FalseVal, TrueVal,
2248 SI.getName() + ".p");
2249 }
2250
2251 // NOTE: if we wanted to, this is where to detect MIN/MAX
2252 } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){
2253 // Canonicalize to use ordered comparisons by swapping the select
2254 // operands.
2255 //
2256 // e.g.
2257 // (X ugt Y) ? X : Y -> (X ole Y) ? X : Y
2258 if (FCI->hasOneUse() && FCmpInst::isUnordered(FCI->getPredicate())) {
2259 FCmpInst::Predicate InvPred = FCI->getInversePredicate();
2260 IRBuilder<>::FastMathFlagGuard FMFG(Builder);
2261 Builder.setFastMathFlags(FCI->getFastMathFlags());
2262 Value *NewCond = Builder.CreateFCmp(InvPred, FalseVal, TrueVal,
2263 FCI->getName() + ".inv");
2264
2265 return SelectInst::Create(NewCond, FalseVal, TrueVal,
2266 SI.getName() + ".p");
2267 }
2268
2269 // NOTE: if we wanted to, this is where to detect MIN/MAX
2270 }
2271 }
2272
2273 // Canonicalize select with fcmp to fabs(). -0.0 makes this tricky. We need
2274 // fast-math-flags (nsz) or fsub with +0.0 (not fneg) for this to work. We
2275 // also require nnan because we do not want to unintentionally change the
2276 // sign of a NaN value.
2277 // FIXME: These folds should test/propagate FMF from the select, not the
2278 // fsub or fneg.
2279 // (X <= +/-0.0) ? (0.0 - X) : X --> fabs(X)
2280 Instruction *FSub;
2281 if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) &&
2282 match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(FalseVal))) &&
2283 match(TrueVal, m_Instruction(FSub)) && FSub->hasNoNaNs() &&
2284 (Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)) {
2285 Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, FSub);
2286 return replaceInstUsesWith(SI, Fabs);
2287 }
2288 // (X > +/-0.0) ? X : (0.0 - X) --> fabs(X)
2289 if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) &&
2290 match(FalseVal, m_FSub(m_PosZeroFP(), m_Specific(TrueVal))) &&
2291 match(FalseVal, m_Instruction(FSub)) && FSub->hasNoNaNs() &&
2292 (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_UGT)) {
2293 Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, FSub);
2294 return replaceInstUsesWith(SI, Fabs);
2295 }
2296 // With nnan and nsz:
2297 // (X < +/-0.0) ? -X : X --> fabs(X)
2298 // (X <= +/-0.0) ? -X : X --> fabs(X)
2299 Instruction *FNeg;
2300 if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) &&
2301 match(TrueVal, m_FNeg(m_Specific(FalseVal))) &&
2302 match(TrueVal, m_Instruction(FNeg)) &&
2303 FNeg->hasNoNaNs() && FNeg->hasNoSignedZeros() &&
2304 (Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_OLE ||
2305 Pred == FCmpInst::FCMP_ULT || Pred == FCmpInst::FCMP_ULE)) {
2306 Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, FNeg);
2307 return replaceInstUsesWith(SI, Fabs);
2308 }
2309 // With nnan and nsz:
2310 // (X > +/-0.0) ? X : -X --> fabs(X)
2311 // (X >= +/-0.0) ? X : -X --> fabs(X)
2312 if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) &&
2313 match(FalseVal, m_FNeg(m_Specific(TrueVal))) &&
2314 match(FalseVal, m_Instruction(FNeg)) &&
2315 FNeg->hasNoNaNs() && FNeg->hasNoSignedZeros() &&
2316 (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OGE ||
2317 Pred == FCmpInst::FCMP_UGT || Pred == FCmpInst::FCMP_UGE)) {
2318 Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, FNeg);
2319 return replaceInstUsesWith(SI, Fabs);
2320 }
2321
2322 // See if we are selecting two values based on a comparison of the two values.
2323 if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal))
2324 if (Instruction *Result = foldSelectInstWithICmp(SI, ICI))
2325 return Result;
2326
2327 if (Instruction *Add = foldAddSubSelect(SI, Builder))
2328 return Add;
2329
2330 // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z))
2331 auto *TI = dyn_cast<Instruction>(TrueVal);
2332 auto *FI = dyn_cast<Instruction>(FalseVal);
2333 if (TI && FI && TI->getOpcode() == FI->getOpcode())
2334 if (Instruction *IV = foldSelectOpOp(SI, TI, FI))
2335 return IV;
2336
2337 if (Instruction *I = foldSelectExtConst(SI))
2338 return I;
2339
2340 // See if we can fold the select into one of our operands.
2341 if (SelType->isIntOrIntVectorTy() || SelType->isFPOrFPVectorTy()) {
2342 if (Instruction *FoldI = foldSelectIntoOp(SI, TrueVal, FalseVal))
2343 return FoldI;
2344
2345 Value *LHS, *RHS;
2346 Instruction::CastOps CastOp;
2347 SelectPatternResult SPR = matchSelectPattern(&SI, LHS, RHS, &CastOp);
2348 auto SPF = SPR.Flavor;
2349 if (SPF) {
2350 Value *LHS2, *RHS2;
2351 if (SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor)
2352 if (Instruction *R = foldSPFofSPF(cast<Instruction>(LHS), SPF2, LHS2,
2353 RHS2, SI, SPF, RHS))
2354 return R;
2355 if (SelectPatternFlavor SPF2 = matchSelectPattern(RHS, LHS2, RHS2).Flavor)
2356 if (Instruction *R = foldSPFofSPF(cast<Instruction>(RHS), SPF2, LHS2,
2357 RHS2, SI, SPF, LHS))
2358 return R;
2359 // TODO.
2360 // ABS(-X) -> ABS(X)
2361 }
2362
2363 if (SelectPatternResult::isMinOrMax(SPF)) {
2364 // Canonicalize so that
2365 // - type casts are outside select patterns.
2366 // - float clamp is transformed to min/max pattern
2367
2368 bool IsCastNeeded = LHS->getType() != SelType;
2369 Value *CmpLHS = cast<CmpInst>(CondVal)->getOperand(0);
2370 Value *CmpRHS = cast<CmpInst>(CondVal)->getOperand(1);
2371 if (IsCastNeeded ||
2372 (LHS->getType()->isFPOrFPVectorTy() &&
2373 ((CmpLHS != LHS && CmpLHS != RHS) ||
2374 (CmpRHS != LHS && CmpRHS != RHS)))) {
2375 CmpInst::Predicate MinMaxPred = getMinMaxPred(SPF, SPR.Ordered);
2376
2377 Value *Cmp;
2378 if (CmpInst::isIntPredicate(MinMaxPred)) {
2379 Cmp = Builder.CreateICmp(MinMaxPred, LHS, RHS);
2380 } else {
2381 IRBuilder<>::FastMathFlagGuard FMFG(Builder);
2382 auto FMF =
2383 cast<FPMathOperator>(SI.getCondition())->getFastMathFlags();
2384 Builder.setFastMathFlags(FMF);
2385 Cmp = Builder.CreateFCmp(MinMaxPred, LHS, RHS);
2386 }
2387
2388 Value *NewSI = Builder.CreateSelect(Cmp, LHS, RHS, SI.getName(), &SI);
2389 if (!IsCastNeeded)
2390 return replaceInstUsesWith(SI, NewSI);
2391
2392 Value *NewCast = Builder.CreateCast(CastOp, NewSI, SelType);
2393 return replaceInstUsesWith(SI, NewCast);
2394 }
2395
2396 // MAX(~a, ~b) -> ~MIN(a, b)
2397 // MAX(~a, C) -> ~MIN(a, ~C)
2398 // MIN(~a, ~b) -> ~MAX(a, b)
2399 // MIN(~a, C) -> ~MAX(a, ~C)
2400 auto moveNotAfterMinMax = [&](Value *X, Value *Y) -> Instruction * {
2401 Value *A;
2402 if (match(X, m_Not(m_Value(A))) && !X->hasNUsesOrMore(3) &&
2403 !isFreeToInvert(A, A->hasOneUse()) &&
2404 // Passing false to only consider m_Not and constants.
2405 isFreeToInvert(Y, false)) {
2406 Value *B = Builder.CreateNot(Y);
2407 Value *NewMinMax = createMinMax(Builder, getInverseMinMaxFlavor(SPF),
2408 A, B);
2409 // Copy the profile metadata.
2410 if (MDNode *MD = SI.getMetadata(LLVMContext::MD_prof)) {
2411 cast<SelectInst>(NewMinMax)->setMetadata(LLVMContext::MD_prof, MD);
2412 // Swap the metadata if the operands are swapped.
2413 if (X == SI.getFalseValue() && Y == SI.getTrueValue())
2414 cast<SelectInst>(NewMinMax)->swapProfMetadata();
2415 }
2416
2417 return BinaryOperator::CreateNot(NewMinMax);
2418 }
2419
2420 return nullptr;
2421 };
2422
2423 if (Instruction *I = moveNotAfterMinMax(LHS, RHS))
2424 return I;
2425 if (Instruction *I = moveNotAfterMinMax(RHS, LHS))
2426 return I;
2427
2428 if (Instruction *I = moveAddAfterMinMax(SPF, LHS, RHS, Builder))
2429 return I;
2430
2431 if (Instruction *I = factorizeMinMaxTree(SPF, LHS, RHS, Builder))
2432 return I;
2433 }
2434 }
2435
2436 // Canonicalize select of FP values where NaN and -0.0 are not valid as
2437 // minnum/maxnum intrinsics.
2438 if (isa<FPMathOperator>(SI) && SI.hasNoNaNs() && SI.hasNoSignedZeros()) {
2439 Value *X, *Y;
2440 if (match(&SI, m_OrdFMax(m_Value(X), m_Value(Y))))
2441 return replaceInstUsesWith(
2442 SI, Builder.CreateBinaryIntrinsic(Intrinsic::maxnum, X, Y, &SI));
2443
2444 if (match(&SI, m_OrdFMin(m_Value(X), m_Value(Y))))
2445 return replaceInstUsesWith(
2446 SI, Builder.CreateBinaryIntrinsic(Intrinsic::minnum, X, Y, &SI));
2447 }
2448
2449 // See if we can fold the select into a phi node if the condition is a select.
2450 if (auto *PN = dyn_cast<PHINode>(SI.getCondition()))
2451 // The true/false values have to be live in the PHI predecessor's blocks.
2452 if (canSelectOperandBeMappingIntoPredBlock(TrueVal, SI) &&
2453 canSelectOperandBeMappingIntoPredBlock(FalseVal, SI))
2454 if (Instruction *NV = foldOpIntoPhi(SI, PN))
2455 return NV;
2456
2457 if (SelectInst *TrueSI = dyn_cast<SelectInst>(TrueVal)) {
2458 if (TrueSI->getCondition()->getType() == CondVal->getType()) {
2459 // select(C, select(C, a, b), c) -> select(C, a, c)
2460 if (TrueSI->getCondition() == CondVal) {
2461 if (SI.getTrueValue() == TrueSI->getTrueValue())
2462 return nullptr;
2463 SI.setOperand(1, TrueSI->getTrueValue());
2464 return &SI;
2465 }
2466 // select(C0, select(C1, a, b), b) -> select(C0&C1, a, b)
2467 // We choose this as normal form to enable folding on the And and shortening
2468 // paths for the values (this helps GetUnderlyingObjects() for example).
2469 if (TrueSI->getFalseValue() == FalseVal && TrueSI->hasOneUse()) {
2470 Value *And = Builder.CreateAnd(CondVal, TrueSI->getCondition());
2471 SI.setOperand(0, And);
2472 SI.setOperand(1, TrueSI->getTrueValue());
2473 return &SI;
2474 }
2475 }
2476 }
2477 if (SelectInst *FalseSI = dyn_cast<SelectInst>(FalseVal)) {
2478 if (FalseSI->getCondition()->getType() == CondVal->getType()) {
2479 // select(C, a, select(C, b, c)) -> select(C, a, c)
2480 if (FalseSI->getCondition() == CondVal) {
2481 if (SI.getFalseValue() == FalseSI->getFalseValue())
2482 return nullptr;
2483 SI.setOperand(2, FalseSI->getFalseValue());
2484 return &SI;
2485 }
2486 // select(C0, a, select(C1, a, b)) -> select(C0|C1, a, b)
2487 if (FalseSI->getTrueValue() == TrueVal && FalseSI->hasOneUse()) {
2488 Value *Or = Builder.CreateOr(CondVal, FalseSI->getCondition());
2489 SI.setOperand(0, Or);
2490 SI.setOperand(2, FalseSI->getFalseValue());
2491 return &SI;
2492 }
2493 }
2494 }
2495
2496 auto canMergeSelectThroughBinop = [](BinaryOperator *BO) {
2497 // The select might be preventing a division by 0.
2498 switch (BO->getOpcode()) {
2499 default:
2500 return true;
2501 case Instruction::SRem:
2502 case Instruction::URem:
2503 case Instruction::SDiv:
2504 case Instruction::UDiv:
2505 return false;
2506 }
2507 };
2508
2509 // Try to simplify a binop sandwiched between 2 selects with the same
2510 // condition.
2511 // select(C, binop(select(C, X, Y), W), Z) -> select(C, binop(X, W), Z)
2512 BinaryOperator *TrueBO;
2513 if (match(TrueVal, m_OneUse(m_BinOp(TrueBO))) &&
2514 canMergeSelectThroughBinop(TrueBO)) {
2515 if (auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->getOperand(0))) {
2516 if (TrueBOSI->getCondition() == CondVal) {
2517 TrueBO->setOperand(0, TrueBOSI->getTrueValue());
2518 Worklist.Add(TrueBO);
2519 return &SI;
2520 }
2521 }
2522 if (auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->getOperand(1))) {
2523 if (TrueBOSI->getCondition() == CondVal) {
2524 TrueBO->setOperand(1, TrueBOSI->getTrueValue());
2525 Worklist.Add(TrueBO);
2526 return &SI;
2527 }
2528 }
2529 }
2530
2531 // select(C, Z, binop(select(C, X, Y), W)) -> select(C, Z, binop(Y, W))
2532 BinaryOperator *FalseBO;
2533 if (match(FalseVal, m_OneUse(m_BinOp(FalseBO))) &&
2534 canMergeSelectThroughBinop(FalseBO)) {
2535 if (auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->getOperand(0))) {
2536 if (FalseBOSI->getCondition() == CondVal) {
2537 FalseBO->setOperand(0, FalseBOSI->getFalseValue());
2538 Worklist.Add(FalseBO);
2539 return &SI;
2540 }
2541 }
2542 if (auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->getOperand(1))) {
2543 if (FalseBOSI->getCondition() == CondVal) {
2544 FalseBO->setOperand(1, FalseBOSI->getFalseValue());
2545 Worklist.Add(FalseBO);
2546 return &SI;
2547 }
2548 }
2549 }
2550
2551 Value *NotCond;
2552 if (match(CondVal, m_Not(m_Value(NotCond)))) {
2553 SI.setOperand(0, NotCond);
2554 SI.setOperand(1, FalseVal);
2555 SI.setOperand(2, TrueVal);
2556 SI.swapProfMetadata();
2557 return &SI;
2558 }
2559
2560 if (VectorType *VecTy = dyn_cast<VectorType>(SelType)) {
2561 unsigned VWidth = VecTy->getNumElements();
2562 APInt UndefElts(VWidth, 0);
2563 APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
2564 if (Value *V = SimplifyDemandedVectorElts(&SI, AllOnesEltMask, UndefElts)) {
2565 if (V != &SI)
2566 return replaceInstUsesWith(SI, V);
2567 return &SI;
2568 }
2569 }
2570
2571 // If we can compute the condition, there's no need for a select.
2572 // Like the above fold, we are attempting to reduce compile-time cost by
2573 // putting this fold here with limitations rather than in InstSimplify.
2574 // The motivation for this call into value tracking is to take advantage of
2575 // the assumption cache, so make sure that is populated.
2576 if (!CondVal->getType()->isVectorTy() && !AC.assumptions().empty()) {
2577 KnownBits Known(1);
2578 computeKnownBits(CondVal, Known, 0, &SI);
2579 if (Known.One.isOneValue())
2580 return replaceInstUsesWith(SI, TrueVal);
2581 if (Known.Zero.isOneValue())
2582 return replaceInstUsesWith(SI, FalseVal);
2583 }
2584
2585 if (Instruction *BitCastSel = foldSelectCmpBitcasts(SI, Builder))
2586 return BitCastSel;
2587
2588 // Simplify selects that test the returned flag of cmpxchg instructions.
2589 if (Instruction *Select = foldSelectCmpXchg(SI))
2590 return Select;
2591
2592 if (Instruction *Select = foldSelectBinOpIdentity(SI, TLI))
2593 return Select;
2594
2595 if (Instruction *Rot = foldSelectRotate(SI))
2596 return Rot;
2597
2598 return nullptr;
2599}

/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Type.h

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
29namespace llvm {
30
31template<class GraphType> struct GraphTraits;
32class IntegerType;
33class LLVMContext;
34class PointerType;
35class raw_ostream;
36class 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///
46class Type {
47public:
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
79private:
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
88protected:
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-10~svn374877/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
117public:
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-10~svn374877/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; }
25
Assuming the condition is false
26
Returning zero, which participates in a condition later
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(); }
24
Calling 'Type::isIntegerTy'
27
Returning from 'Type::isIntegerTy'
28
Returning zero, which participates in a condition later
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; }
14
Assuming the condition is false
15
Returning zero, which participates in a condition later
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-10~svn374877/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-10~svn374877/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-10~svn374877/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 Type *getVectorElementType() const {
376 assert(getTypeID() == VectorTyID)((getTypeID() == VectorTyID) ? static_cast<void> (0) : __assert_fail
("getTypeID() == VectorTyID", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Type.h"
, 376, __PRETTY_FUNCTION__))
;
377 return ContainedTys[0];
378 }
379
380 Type *getPointerElementType() const {
381 assert(getTypeID() == PointerTyID)((getTypeID() == PointerTyID) ? static_cast<void> (0) :
__assert_fail ("getTypeID() == PointerTyID", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Type.h"
, 381, __PRETTY_FUNCTION__))
;
382 return ContainedTys[0];
383 }
384
385 /// Given scalar/vector integer type, returns a type with elements twice as
386 /// wide as in the original type. For vectors, preserves element count.
387 inline Type *getExtendedType() const;
388
389 /// Get the address space of this pointer or pointer vector type.
390 inline unsigned getPointerAddressSpace() const;
391
392 //===--------------------------------------------------------------------===//
393 // Static members exported by the Type class itself. Useful for getting
394 // instances of Type.
395 //
396
397 /// Return a type based on an identifier.
398 static Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber);
399
400 //===--------------------------------------------------------------------===//
401 // These are the builtin types that are always available.
402 //
403 static Type *getVoidTy(LLVMContext &C);
404 static Type *getLabelTy(LLVMContext &C);
405 static Type *getHalfTy(LLVMContext &C);
406 static Type *getFloatTy(LLVMContext &C);
407 static Type *getDoubleTy(LLVMContext &C);
408 static Type *getMetadataTy(LLVMContext &C);
409 static Type *getX86_FP80Ty(LLVMContext &C);
410 static Type *getFP128Ty(LLVMContext &C);
411 static Type *getPPC_FP128Ty(LLVMContext &C);
412 static Type *getX86_MMXTy(LLVMContext &C);
413 static Type *getTokenTy(LLVMContext &C);
414 static IntegerType *getIntNTy(LLVMContext &C, unsigned N);
415 static IntegerType *getInt1Ty(LLVMContext &C);
416 static IntegerType *getInt8Ty(LLVMContext &C);
417 static IntegerType *getInt16Ty(LLVMContext &C);
418 static IntegerType *getInt32Ty(LLVMContext &C);
419 static IntegerType *getInt64Ty(LLVMContext &C);
420 static IntegerType *getInt128Ty(LLVMContext &C);
421 template <typename ScalarTy> static Type *getScalarTy(LLVMContext &C) {
422 int noOfBits = sizeof(ScalarTy) * CHAR_BIT8;
423 if (std::is_integral<ScalarTy>::value) {
424 return (Type*) Type::getIntNTy(C, noOfBits);
425 } else if (std::is_floating_point<ScalarTy>::value) {
426 switch (noOfBits) {
427 case 32:
428 return Type::getFloatTy(C);
429 case 64:
430 return Type::getDoubleTy(C);
431 }
432 }
433 llvm_unreachable("Unsupported type in Type::getScalarTy")::llvm::llvm_unreachable_internal("Unsupported type in Type::getScalarTy"
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Type.h"
, 433)
;
434 }
435
436 //===--------------------------------------------------------------------===//
437 // Convenience methods for getting pointer types with one of the above builtin
438 // types as pointee.
439 //
440 static PointerType *getHalfPtrTy(LLVMContext &C, unsigned AS = 0);
441 static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0);
442 static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0);
443 static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0);
444 static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0);
445 static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0);
446 static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0);
447 static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0);
448 static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0);
449 static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0);
450 static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0);
451 static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0);
452 static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0);
453
454 /// Return a pointer to the current type. This is equivalent to
455 /// PointerType::get(Foo, AddrSpace).
456 PointerType *getPointerTo(unsigned AddrSpace = 0) const;
457
458private:
459 /// Derived types like structures and arrays are sized iff all of the members
460 /// of the type are sized as well. Since asking for their size is relatively
461 /// uncommon, move this operation out-of-line.
462 bool isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited = nullptr) const;
463};
464
465// Printing of types.
466inline raw_ostream &operator<<(raw_ostream &OS, const Type &T) {
467 T.print(OS);
468 return OS;
469}
470
471// allow isa<PointerType>(x) to work without DerivedTypes.h included.
472template <> struct isa_impl<PointerType, Type> {
473 static inline bool doit(const Type &Ty) {
474 return Ty.getTypeID() == Type::PointerTyID;
475 }
476};
477
478// Create wrappers for C Binding types (see CBindingWrapping.h).
479DEFINE_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)); }
480
481/* Specialized opaque type conversions.
482 */
483inline Type **unwrap(LLVMTypeRef* Tys) {
484 return reinterpret_cast<Type**>(Tys);
485}
486
487inline LLVMTypeRef *wrap(Type **Tys) {
488 return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys));
489}
490
491} // end namespace llvm
492
493#endif // LLVM_IR_TYPE_H

/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Value.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/CBindingWrapping.h"
21#include "llvm/Support/Casting.h"
22#include <cassert>
23#include <iterator>
24#include <memory>
25
26namespace llvm {
27
28class APInt;
29class Argument;
30class BasicBlock;
31class Constant;
32class ConstantData;
33class ConstantAggregate;
34class DataLayout;
35class Function;
36class GlobalAlias;
37class GlobalIFunc;
38class GlobalIndirectSymbol;
39class GlobalObject;
40class GlobalValue;
41class GlobalVariable;
42class InlineAsm;
43class Instruction;
44class LLVMContext;
45class Module;
46class ModuleSlotTracker;
47class raw_ostream;
48template<typename ValueTy> class StringMapEntry;
49class StringRef;
50class Twine;
51class Type;
52class User;
53
54using ValueName = StringMapEntry<Value *>;
55
56//===----------------------------------------------------------------------===//
57// Value Class
58//===----------------------------------------------------------------------===//
59
60/// LLVM Value Representation
61///
62/// This is a very important LLVM class. It is the base class of all values
63/// computed by a program that may be used as operands to other values. Value is
64/// the super class of other important classes such as Instruction and Function.
65/// All Values have a Type. Type is not a subclass of Value. Some values can
66/// have a name and they belong to some Module. Setting the name on the Value
67/// automatically updates the module's symbol table.
68///
69/// Every value has a "use list" that keeps track of which other Values are
70/// using this Value. A Value can also have an arbitrary number of ValueHandle
71/// objects that watch it and listen to RAUW and Destroy events. See
72/// llvm/IR/ValueHandle.h for details.
73class Value {
74 // The least-significant bit of the first word of Value *must* be zero:
75 // http://www.llvm.org/docs/ProgrammersManual.html#the-waymarking-algorithm
76 Type *VTy;
77 Use *UseList;
78
79 friend class ValueAsMetadata; // Allow access to IsUsedByMD.
80 friend class ValueHandleBase;
81
82 const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast)
83 unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
84
85protected:
86 /// Hold subclass data that can be dropped.
87 ///
88 /// This member is similar to SubclassData, however it is for holding
89 /// information which may be used to aid optimization, but which may be
90 /// cleared to zero without affecting conservative interpretation.
91 unsigned char SubclassOptionalData : 7;
92
93private:
94 /// Hold arbitrary subclass data.
95 ///
96 /// This member is defined by this class, but is not used for anything.
97 /// Subclasses can use it to hold whatever state they find useful. This
98 /// field is initialized to zero by the ctor.
99 unsigned short SubclassData;
100
101protected:
102 /// The number of operands in the subclass.
103 ///
104 /// This member is defined by this class, but not used for anything.
105 /// Subclasses can use it to store their number of operands, if they have
106 /// any.
107 ///
108 /// This is stored here to save space in User on 64-bit hosts. Since most
109 /// instances of Value have operands, 32-bit hosts aren't significantly
110 /// affected.
111 ///
112 /// Note, this should *NOT* be used directly by any class other than User.
113 /// User uses this value to find the Use list.
114 enum : unsigned { NumUserOperandsBits = 28 };
115 unsigned NumUserOperands : NumUserOperandsBits;
116
117 // Use the same type as the bitfield above so that MSVC will pack them.
118 unsigned IsUsedByMD : 1;
119 unsigned HasName : 1;
120 unsigned HasHungOffUses : 1;
121 unsigned HasDescriptor : 1;
122
123private:
124 template <typename UseT> // UseT == 'Use' or 'const Use'
125 class use_iterator_impl
126 : public std::iterator<std::forward_iterator_tag, UseT *> {
127 friend class Value;
128
129 UseT *U;
130
131 explicit use_iterator_impl(UseT *u) : U(u) {}
132
133 public:
134 use_iterator_impl() : U() {}
135
136 bool operator==(const use_iterator_impl &x) const { return U == x.U; }
137 bool operator!=(const use_iterator_impl &x) const { return !operator==(x); }
138
139 use_iterator_impl &operator++() { // Preincrement
140 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-10~svn374877/include/llvm/IR/Value.h"
, 140, __PRETTY_FUNCTION__))
;
141 U = U->getNext();
142 return *this;
143 }
144
145 use_iterator_impl operator++(int) { // Postincrement
146 auto tmp = *this;
147 ++*this;
148 return tmp;
149 }
150
151 UseT &operator*() const {
152 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-10~svn374877/include/llvm/IR/Value.h"
, 152, __PRETTY_FUNCTION__))
;
153 return *U;
154 }
155
156 UseT *operator->() const { return &operator*(); }
157
158 operator use_iterator_impl<const UseT>() const {
159 return use_iterator_impl<const UseT>(U);
160 }
161 };
162
163 template <typename UserTy> // UserTy == 'User' or 'const User'
164 class user_iterator_impl
165 : public std::iterator<std::forward_iterator_tag, UserTy *> {
166 use_iterator_impl<Use> UI;
167 explicit user_iterator_impl(Use *U) : UI(U) {}
168 friend class Value;
169
170 public:
171 user_iterator_impl() = default;
172
173 bool operator==(const user_iterator_impl &x) const { return UI == x.UI; }
174 bool operator!=(const user_iterator_impl &x) const { return !operator==(x); }
175
176 /// Returns true if this iterator is equal to user_end() on the value.
177 bool atEnd() const { return *this == user_iterator_impl(); }
178
179 user_iterator_impl &operator++() { // Preincrement
180 ++UI;
181 return *this;
182 }
183
184 user_iterator_impl operator++(int) { // Postincrement
185 auto tmp = *this;
186 ++*this;
187 return tmp;
188 }
189
190 // Retrieve a pointer to the current User.
191 UserTy *operator*() const {
192 return UI->getUser();
193 }
194
195 UserTy *operator->() const { return operator*(); }
196
197 operator user_iterator_impl<const UserTy>() const {
198 return user_iterator_impl<const UserTy>(*UI);
199 }
200
201 Use &getUse() const { return *UI; }
202 };
203
204protected:
205 Value(Type *Ty, unsigned scid);
206
207 /// Value's destructor should be virtual by design, but that would require
208 /// that Value and all of its subclasses have a vtable that effectively
209 /// duplicates the information in the value ID. As a size optimization, the
210 /// destructor has been protected, and the caller should manually call
211 /// deleteValue.
212 ~Value(); // Use deleteValue() to delete a generic Value.
213
214public:
215 Value(const Value &) = delete;
216 Value &operator=(const Value &) = delete;
217
218 /// Delete a pointer to a generic Value.
219 void deleteValue();
220
221 /// Support for debugging, callable in GDB: V->dump()
222 void dump() const;
223
224 /// Implement operator<< on Value.
225 /// @{
226 void print(raw_ostream &O, bool IsForDebug = false) const;
227 void print(raw_ostream &O, ModuleSlotTracker &MST,
228 bool IsForDebug = false) const;
229 /// @}
230
231 /// Print the name of this Value out to the specified raw_ostream.
232 ///
233 /// This is useful when you just want to print 'int %reg126', not the
234 /// instruction that generated it. If you specify a Module for context, then
235 /// even constanst get pretty-printed; for example, the type of a null
236 /// pointer is printed symbolically.
237 /// @{
238 void printAsOperand(raw_ostream &O, bool PrintType = true,
239 const Module *M = nullptr) const;
240 void printAsOperand(raw_ostream &O, bool PrintType,
241 ModuleSlotTracker &MST) const;
242 /// @}
243
244 /// All values are typed, get the type of this value.
245 Type *getType() const { return VTy; }
246
247 /// All values hold a context through their type.
248 LLVMContext &getContext() const;
249
250 // All values can potentially be named.
251 bool hasName() const { return HasName; }
252 ValueName *getValueName() const;
253 void setValueName(ValueName *VN);
254
255private:
256 void destroyValueName();
257 enum class ReplaceMetadataUses { No, Yes };
258 void doRAUW(Value *New, ReplaceMetadataUses);
259 void setNameImpl(const Twine &Name);
260
261public:
262 /// Return a constant reference to the value's name.
263 ///
264 /// This guaranteed to return the same reference as long as the value is not
265 /// modified. If the value has a name, this does a hashtable lookup, so it's
266 /// not free.
267 StringRef getName() const;
268
269 /// Change the name of the value.
270 ///
271 /// Choose a new unique name if the provided name is taken.
272 ///
273 /// \param Name The new name; or "" if the value's name should be removed.
274 void setName(const Twine &Name);
275
276 /// Transfer the name from V to this value.
277 ///
278 /// After taking V's name, sets V's name to empty.
279 ///
280 /// \note It is an error to call V->takeName(V).
281 void takeName(Value *V);
282
283 /// Change all uses of this to point to a new Value.
284 ///
285 /// Go through the uses list for this definition and make each use point to
286 /// "V" instead of "this". After this completes, 'this's use list is
287 /// guaranteed to be empty.
288 void replaceAllUsesWith(Value *V);
289
290 /// Change non-metadata uses of this to point to a new Value.
291 ///
292 /// Go through the uses list for this definition and make each use point to
293 /// "V" instead of "this". This function skips metadata entries in the list.
294 void replaceNonMetadataUsesWith(Value *V);
295
296 /// Go through the uses list for this definition and make each use point
297 /// to "V" if the callback ShouldReplace returns true for the given Use.
298 /// Unlike replaceAllUsesWith() this function does not support basic block
299 /// values or constant users.
300 void replaceUsesWithIf(Value *New,
301 llvm::function_ref<bool(Use &U)> ShouldReplace) {
302 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-10~svn374877/include/llvm/IR/Value.h"
, 302, __PRETTY_FUNCTION__))
;
303 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-10~svn374877/include/llvm/IR/Value.h"
, 304, __PRETTY_FUNCTION__))
304 "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-10~svn374877/include/llvm/IR/Value.h"
, 304, __PRETTY_FUNCTION__))
;
305
306 for (use_iterator UI = use_begin(), E = use_end(); UI != E;) {
307 Use &U = *UI;
308 ++UI;
309 if (!ShouldReplace(U))
310 continue;
311 U.set(New);
312 }
313 }
314
315 /// replaceUsesOutsideBlock - Go through the uses list for this definition and
316 /// make each use point to "V" instead of "this" when the use is outside the
317 /// block. 'This's use list is expected to have at least one element.
318 /// Unlike replaceAllUsesWith() this function does not support basic block
319 /// values or constant users.
320 void replaceUsesOutsideBlock(Value *V, BasicBlock *BB);
321
322 //----------------------------------------------------------------------
323 // Methods for handling the chain of uses of this Value.
324 //
325 // Materializing a function can introduce new uses, so these methods come in
326 // two variants:
327 // The methods that start with materialized_ check the uses that are
328 // currently known given which functions are materialized. Be very careful
329 // when using them since you might not get all uses.
330 // The methods that don't start with materialized_ assert that modules is
331 // fully materialized.
332 void assertModuleIsMaterializedImpl() const;
333 // This indirection exists so we can keep assertModuleIsMaterializedImpl()
334 // around in release builds of Value.cpp to be linked with other code built
335 // in debug mode. But this avoids calling it in any of the release built code.
336 void assertModuleIsMaterialized() const {
337#ifndef NDEBUG
338 assertModuleIsMaterializedImpl();
339#endif
340 }
341
342 bool use_empty() const {
343 assertModuleIsMaterialized();
344 return UseList == nullptr;
345 }
346
347 bool materialized_use_empty() const {
348 return UseList == nullptr;
349 }
350
351 using use_iterator = use_iterator_impl<Use>;
352 using const_use_iterator = use_iterator_impl<const Use>;
353
354 use_iterator materialized_use_begin() { return use_iterator(UseList); }
355 const_use_iterator materialized_use_begin() const {
356 return const_use_iterator(UseList);
357 }
358 use_iterator use_begin() {
359 assertModuleIsMaterialized();
360 return materialized_use_begin();
361 }
362 const_use_iterator use_begin() const {
363 assertModuleIsMaterialized();
364 return materialized_use_begin();
365 }
366 use_iterator use_end() { return use_iterator(); }
367 const_use_iterator use_end() const { return const_use_iterator(); }
368 iterator_range<use_iterator> materialized_uses() {
369 return make_range(materialized_use_begin(), use_end());
370 }
371 iterator_range<const_use_iterator> materialized_uses() const {
372 return make_range(materialized_use_begin(), use_end());
373 }
374 iterator_range<use_iterator> uses() {
375 assertModuleIsMaterialized();
376 return materialized_uses();
377 }
378 iterator_range<const_use_iterator> uses() const {
379 assertModuleIsMaterialized();
380 return materialized_uses();
381 }
382
383 bool user_empty() const {
384 assertModuleIsMaterialized();
385 return UseList == nullptr;
386 }
387
388 using user_iterator = user_iterator_impl<User>;
389 using const_user_iterator = user_iterator_impl<const User>;
390
391 user_iterator materialized_user_begin() { return user_iterator(UseList); }
392 const_user_iterator materialized_user_begin() const {
393 return const_user_iterator(UseList);
394 }
395 user_iterator user_begin() {
396 assertModuleIsMaterialized();
397 return materialized_user_begin();
398 }
399 const_user_iterator user_begin() const {
400 assertModuleIsMaterialized();
401 return materialized_user_begin();
402 }
403 user_iterator user_end() { return user_iterator(); }
404 const_user_iterator user_end() const { return const_user_iterator(); }
405 User *user_back() {
406 assertModuleIsMaterialized();
407 return *materialized_user_begin();
408 }
409 const User *user_back() const {
410 assertModuleIsMaterialized();
411 return *materialized_user_begin();
412 }
413 iterator_range<user_iterator> materialized_users() {
414 return make_range(materialized_user_begin(), user_end());
415 }
416 iterator_range<const_user_iterator> materialized_users() const {
417 return make_range(materialized_user_begin(), user_end());
418 }
419 iterator_range<user_iterator> users() {
420 assertModuleIsMaterialized();
421 return materialized_users();
422 }
423 iterator_range<const_user_iterator> users() const {
424 assertModuleIsMaterialized();
425 return materialized_users();
426 }
427
428 /// Return true if there is exactly one user of this value.
429 ///
430 /// This is specialized because it is a common request and does not require
431 /// traversing the whole use list.
432 bool hasOneUse() const {
433 const_use_iterator I = use_begin(), E = use_end();
434 if (I == E) return false;
36
Assuming the condition is false
37
Taking false branch
435 return ++I == E;
38
Returning value, which participates in a condition later
436 }
437
438 /// Return true if this Value has exactly N users.
439 bool hasNUses(unsigned N) const;
440
441 /// Return true if this value has N users or more.
442 ///
443 /// This is logically equivalent to getNumUses() >= N.
444 bool hasNUsesOrMore(unsigned N) const;
445
446 /// Check if this value is used in the specified basic block.
447 bool isUsedInBasicBlock(const BasicBlock *BB) const;
448
449 /// This method computes the number of uses of this Value.
450 ///
451 /// This is a linear time operation. Use hasOneUse, hasNUses, or
452 /// hasNUsesOrMore to check for specific values.
453 unsigned getNumUses() const;
454
455 /// This method should only be used by the Use class.
456 void addUse(Use &U) { U.addToList(&UseList); }
457
458 /// Concrete subclass of this.
459 ///
460 /// An enumeration for keeping track of the concrete subclass of Value that
461 /// is actually instantiated. Values of this enumeration are kept in the
462 /// Value classes SubclassID field. They are used for concrete type
463 /// identification.
464 enum ValueTy {
465#define HANDLE_VALUE(Name) Name##Val,
466#include "llvm/IR/Value.def"
467
468 // Markers:
469#define HANDLE_CONSTANT_MARKER(Marker, Constant) Marker = Constant##Val,
470#include "llvm/IR/Value.def"
471 };
472
473 /// Return an ID for the concrete type of this object.
474 ///
475 /// This is used to implement the classof checks. This should not be used
476 /// for any other purpose, as the values may change as LLVM evolves. Also,
477 /// note that for instructions, the Instruction's opcode is added to
478 /// InstructionVal. So this means three things:
479 /// # there is no value with code InstructionVal (no opcode==0).
480 /// # there are more possible values for the value type than in ValueTy enum.
481 /// # the InstructionVal enumerator must be the highest valued enumerator in
482 /// the ValueTy enum.
483 unsigned getValueID() const {
484 return SubclassID;
485 }
486
487 /// Return the raw optional flags value contained in this value.
488 ///
489 /// This should only be used when testing two Values for equivalence.
490 unsigned getRawSubclassOptionalData() const {
491 return SubclassOptionalData;
492 }
493
494 /// Clear the optional flags contained in this value.
495 void clearSubclassOptionalData() {
496 SubclassOptionalData = 0;
497 }
498
499 /// Check the optional flags for equality.
500 bool hasSameSubclassOptionalData(const Value *V) const {
501 return SubclassOptionalData == V->SubclassOptionalData;
502 }
503
504 /// Return true if there is a value handle associated with this value.
505 bool hasValueHandle() const { return HasValueHandle; }
506
507 /// Return true if there is metadata referencing this value.
508 bool isUsedByMetadata() const { return IsUsedByMD; }
509
510 /// Return true if this value is a swifterror value.
511 ///
512 /// swifterror values can be either a function argument or an alloca with a
513 /// swifterror attribute.
514 bool isSwiftError() const;
515
516 /// Strip off pointer casts, all-zero GEPs and address space casts.
517 ///
518 /// Returns the original uncasted value. If this is called on a non-pointer
519 /// value, it returns 'this'.
520 const Value *stripPointerCasts() const;
521 Value *stripPointerCasts() {
522 return const_cast<Value *>(
523 static_cast<const Value *>(this)->stripPointerCasts());
524 }
525
526 /// Strip off pointer casts, all-zero GEPs, address space casts, and aliases.
527 ///
528 /// Returns the original uncasted value. If this is called on a non-pointer
529 /// value, it returns 'this'.
530 const Value *stripPointerCastsAndAliases() const;
531 Value *stripPointerCastsAndAliases() {
532 return const_cast<Value *>(
533 static_cast<const Value *>(this)->stripPointerCastsAndAliases());
534 }
535
536 /// Strip off pointer casts, all-zero GEPs and address space casts
537 /// but ensures the representation of the result stays the same.
538 ///
539 /// Returns the original uncasted value with the same representation. If this
540 /// is called on a non-pointer value, it returns 'this'.
541 const Value *stripPointerCastsSameRepresentation() const;
542 Value *stripPointerCastsSameRepresentation() {
543 return const_cast<Value *>(static_cast<const Value *>(this)
544 ->stripPointerCastsSameRepresentation());
545 }
546
547 /// Strip off pointer casts, all-zero GEPs and invariant group info.
548 ///
549 /// Returns the original uncasted value. If this is called on a non-pointer
550 /// value, it returns 'this'. This function should be used only in
551 /// Alias analysis.
552 const Value *stripPointerCastsAndInvariantGroups() const;
553 Value *stripPointerCastsAndInvariantGroups() {
554 return const_cast<Value *>(static_cast<const Value *>(this)
555 ->stripPointerCastsAndInvariantGroups());
556 }
557
558 /// Strip off pointer casts and all-constant inbounds GEPs.
559 ///
560 /// Returns the original pointer value. If this is called on a non-pointer
561 /// value, it returns 'this'.
562 const Value *stripInBoundsConstantOffsets() const;
563 Value *stripInBoundsConstantOffsets() {
564 return const_cast<Value *>(
565 static_cast<const Value *>(this)->stripInBoundsConstantOffsets());
566 }
567
568 /// Accumulate the constant offset this value has compared to a base pointer.
569 /// Only 'getelementptr' instructions (GEPs) with constant indices are
570 /// accumulated but other instructions, e.g., casts, are stripped away as
571 /// well. The accumulated constant offset is added to \p Offset and the base
572 /// pointer is returned.
573 ///
574 /// The APInt \p Offset has to have a bit-width equal to the IntPtr type for
575 /// the address space of 'this' pointer value, e.g., use
576 /// DataLayout::getIndexTypeSizeInBits(Ty).
577 ///
578 /// If \p AllowNonInbounds is true, constant offsets in GEPs are stripped and
579 /// accumulated even if the GEP is not "inbounds".
580 ///
581 /// If this is called on a non-pointer value, it returns 'this' and the
582 /// \p Offset is not modified.
583 ///
584 /// Note that this function will never return a nullptr. It will also never
585 /// manipulate the \p Offset in a way that would not match the difference
586 /// between the underlying value and the returned one. Thus, if no constant
587 /// offset was found, the returned value is the underlying one and \p Offset
588 /// is unchanged.
589 const Value *stripAndAccumulateConstantOffsets(const DataLayout &DL,
590 APInt &Offset,
591 bool AllowNonInbounds) const;
592 Value *stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset,
593 bool AllowNonInbounds) {
594 return const_cast<Value *>(
595 static_cast<const Value *>(this)->stripAndAccumulateConstantOffsets(
596 DL, Offset, AllowNonInbounds));
597 }
598
599 /// This is a wrapper around stripAndAccumulateConstantOffsets with the
600 /// in-bounds requirement set to false.
601 const Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
602 APInt &Offset) const {
603 return stripAndAccumulateConstantOffsets(DL, Offset,
604 /* AllowNonInbounds */ false);
605 }
606 Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
607 APInt &Offset) {
608 return stripAndAccumulateConstantOffsets(DL, Offset,
609 /* AllowNonInbounds */ false);
610 }
611
612 /// Strip off pointer casts and inbounds GEPs.
613 ///
614 /// Returns the original pointer value. If this is called on a non-pointer
615 /// value, it returns 'this'.
616 const Value *stripInBoundsOffsets() const;
617 Value *stripInBoundsOffsets() {
618 return const_cast<Value *>(
619 static_cast<const Value *>(this)->stripInBoundsOffsets());
620 }
621
622 /// Returns the number of bytes known to be dereferenceable for the
623 /// pointer value.
624 ///
625 /// If CanBeNull is set by this function the pointer can either be null or be
626 /// dereferenceable up to the returned number of bytes.
627 uint64_t getPointerDereferenceableBytes(const DataLayout &DL,
628 bool &CanBeNull) const;
629
630 /// Returns an alignment of the pointer value.
631 ///
632 /// Returns an alignment which is either specified explicitly, e.g. via
633 /// align attribute of a function argument, or guaranteed by DataLayout.
634 unsigned getPointerAlignment(const DataLayout &DL) const;
635
636 /// Translate PHI node to its predecessor from the given basic block.
637 ///
638 /// If this value is a PHI node with CurBB as its parent, return the value in
639 /// the PHI node corresponding to PredBB. If not, return ourself. This is
640 /// useful if you want to know the value something has in a predecessor
641 /// block.
642 const Value *DoPHITranslation(const BasicBlock *CurBB,
643 const BasicBlock *PredBB) const;
644 Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) {
645 return const_cast<Value *>(
646 static_cast<const Value *>(this)->DoPHITranslation(CurBB, PredBB));
647 }
648
649 /// The maximum alignment for instructions.
650 ///
651 /// This is the greatest alignment value supported by load, store, and alloca
652 /// instructions, and global values.
653 static const unsigned MaxAlignmentExponent = 29;
654 static const unsigned MaximumAlignment = 1u << MaxAlignmentExponent;
655
656 /// Mutate the type of this Value to be of the specified type.
657 ///
658 /// Note that this is an extremely dangerous operation which can create
659 /// completely invalid IR very easily. It is strongly recommended that you
660 /// recreate IR objects with the right types instead of mutating them in
661 /// place.
662 void mutateType(Type *Ty) {
663 VTy = Ty;
664 }
665
666 /// Sort the use-list.
667 ///
668 /// Sorts the Value's use-list by Cmp using a stable mergesort. Cmp is
669 /// expected to compare two \a Use references.
670 template <class Compare> void sortUseList(Compare Cmp);
671
672 /// Reverse the use-list.
673 void reverseUseList();
674
675private:
676 /// Merge two lists together.
677 ///
678 /// Merges \c L and \c R using \c Cmp. To enable stable sorts, always pushes
679 /// "equal" items from L before items from R.
680 ///
681 /// \return the first element in the list.
682 ///
683 /// \note Completely ignores \a Use::Prev (doesn't read, doesn't update).
684 template <class Compare>
685 static Use *mergeUseLists(Use *L, Use *R, Compare Cmp) {
686 Use *Merged;
687 Use **Next = &Merged;
688
689 while (true) {
690 if (!L) {
691 *Next = R;
692 break;
693 }
694 if (!R) {
695 *Next = L;
696 break;
697 }
698 if (Cmp(*R, *L)) {
699 *Next = R;
700 Next = &R->Next;
701 R = R->Next;
702 } else {
703 *Next = L;
704 Next = &L->Next;
705 L = L->Next;
706 }
707 }
708
709 return Merged;
710 }
711
712protected:
713 unsigned short getSubclassDataFromValue() const { return SubclassData; }
714 void setValueSubclassData(unsigned short D) { SubclassData = D; }
715};
716
717struct ValueDeleter { void operator()(Value *V) { V->deleteValue(); } };
718
719/// Use this instead of std::unique_ptr<Value> or std::unique_ptr<Instruction>.
720/// Those don't work because Value and Instruction's destructors are protected,
721/// aren't virtual, and won't destroy the complete object.
722using unique_value = std::unique_ptr<Value, ValueDeleter>;
723
724inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
725 V.print(OS);
726 return OS;
727}
728
729void Use::set(Value *V) {
730 if (Val) removeFromList();
731 Val = V;
732 if (V) V->addUse(*this);
733}
734
735Value *Use::operator=(Value *RHS) {
736 set(RHS);
737 return RHS;
738}
739
740const Use &Use::operator=(const Use &RHS) {
741 set(RHS.Val);
742 return *this;
743}
744
745template <class Compare> void Value::sortUseList(Compare Cmp) {
746 if (!UseList || !UseList->Next)
747 // No need to sort 0 or 1 uses.
748 return;
749
750 // Note: this function completely ignores Prev pointers until the end when
751 // they're fixed en masse.
752
753 // Create a binomial vector of sorted lists, visiting uses one at a time and
754 // merging lists as necessary.
755 const unsigned MaxSlots = 32;
756 Use *Slots[MaxSlots];
757
758 // Collect the first use, turning it into a single-item list.
759 Use *Next = UseList->Next;
760 UseList->Next = nullptr;
761 unsigned NumSlots = 1;
762 Slots[0] = UseList;
763
764 // Collect all but the last use.
765 while (Next->Next) {
766 Use *Current = Next;
767 Next = Current->Next;
768
769 // Turn Current into a single-item list.
770 Current->Next = nullptr;
771
772 // Save Current in the first available slot, merging on collisions.
773 unsigned I;
774 for (I = 0; I < NumSlots; ++I) {
775 if (!Slots[I])
776 break;
777
778 // Merge two lists, doubling the size of Current and emptying slot I.
779 //
780 // Since the uses in Slots[I] originally preceded those in Current, send
781 // Slots[I] in as the left parameter to maintain a stable sort.
782 Current = mergeUseLists(Slots[I], Current, Cmp);
783 Slots[I] = nullptr;
784 }
785 // Check if this is a new slot.
786 if (I == NumSlots) {
787 ++NumSlots;
788 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-10~svn374877/include/llvm/IR/Value.h"
, 788, __PRETTY_FUNCTION__))
;
789 }
790
791 // Found an open slot.
792 Slots[I] = Current;
793 }
794
795 // Merge all the lists together.
796 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-10~svn374877/include/llvm/IR/Value.h"
, 796, __PRETTY_FUNCTION__))
;
797 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-10~svn374877/include/llvm/IR/Value.h"
, 797, __PRETTY_FUNCTION__))
;
798 UseList = Next;
799 for (unsigned I = 0; I < NumSlots; ++I)
800 if (Slots[I])
801 // Since the uses in Slots[I] originally preceded those in UseList, send
802 // Slots[I] in as the left parameter to maintain a stable sort.
803 UseList = mergeUseLists(Slots[I], UseList, Cmp);
804
805 // Fix the Prev pointers.
806 for (Use *I = UseList, **Prev = &UseList; I; I = I->Next) {
807 I->setPrev(Prev);
808 Prev = &I->Next;
809 }
810}
811
812// isa - Provide some specializations of isa so that we don't have to include
813// the subtype header files to test to see if the value is a subclass...
814//
815template <> struct isa_impl<Constant, Value> {
816 static inline bool doit(const Value &Val) {
817 static_assert(Value::ConstantFirstVal == 0, "Val.getValueID() >= Value::ConstantFirstVal");
818 return Val.getValueID() <= Value::ConstantLastVal;
819 }
820};
821
822template <> struct isa_impl<ConstantData, Value> {
823 static inline bool doit(const Value &Val) {
824 return Val.getValueID() >= Value::ConstantDataFirstVal &&
825 Val.getValueID() <= Value::ConstantDataLastVal;
826 }
827};
828
829template <> struct isa_impl<ConstantAggregate, Value> {
830 static inline bool doit(const Value &Val) {
831 return Val.getValueID() >= Value::ConstantAggregateFirstVal &&
832 Val.getValueID() <= Value::ConstantAggregateLastVal;
833 }
834};
835
836template <> struct isa_impl<Argument, Value> {
837 static inline bool doit (const Value &Val) {
838 return Val.getValueID() == Value::ArgumentVal;
839 }
840};
841
842template <> struct isa_impl<InlineAsm, Value> {
843 static inline bool doit(const Value &Val) {
844 return Val.getValueID() == Value::InlineAsmVal;
845 }
846};
847
848template <> struct isa_impl<Instruction, Value> {
849 static inline bool doit(const Value &Val) {
850 return Val.getValueID() >= Value::InstructionVal;
851 }
852};
853
854template <> struct isa_impl<BasicBlock, Value> {
855 static inline bool doit(const Value &Val) {
856 return Val.getValueID() == Value::BasicBlockVal;
857 }
858};
859
860template <> struct isa_impl<Function, Value> {
861 static inline bool doit(const Value &Val) {
862 return Val.getValueID() == Value::FunctionVal;
863 }
864};
865
866template <> struct isa_impl<GlobalVariable, Value> {
867 static inline bool doit(const Value &Val) {
868 return Val.getValueID() == Value::GlobalVariableVal;
869 }
870};
871
872template <> struct isa_impl<GlobalAlias, Value> {
873 static inline bool doit(const Value &Val) {
874 return Val.getValueID() == Value::GlobalAliasVal;
875 }
876};
877
878template <> struct isa_impl<GlobalIFunc, Value> {
879 static inline bool doit(const Value &Val) {
880 return Val.getValueID() == Value::GlobalIFuncVal;
881 }
882};
883
884template <> struct isa_impl<GlobalIndirectSymbol, Value> {
885 static inline bool doit(const Value &Val) {
886 return isa<GlobalAlias>(Val) || isa<GlobalIFunc>(Val);
887 }
888};
889
890template <> struct isa_impl<GlobalValue, Value> {
891 static inline bool doit(const Value &Val) {
892 return isa<GlobalObject>(Val) || isa<GlobalIndirectSymbol>(Val);
893 }
894};
895
896template <> struct isa_impl<GlobalObject, Value> {
897 static inline bool doit(const Value &Val) {
898 return isa<GlobalVariable>(Val) || isa<Function>(Val);
899 }
900};
901
902// Create wrappers for C Binding types (see CBindingWrapping.h).
903DEFINE_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)); }
904
905// Specialized opaque value conversions.
906inline Value **unwrap(LLVMValueRef *Vals) {
907 return reinterpret_cast<Value**>(Vals);
908}
909
910template<typename T>
911inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
912#ifndef NDEBUG
913 for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I)
914 unwrap<T>(*I); // For side effect of calling assert on invalid usage.
915#endif
916 (void)Length;
917 return reinterpret_cast<T**>(Vals);
918}
919
920inline LLVMValueRef *wrap(const Value **Vals) {
921 return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
922}
923
924} // end namespace llvm
925
926#endif // LLVM_IR_VALUE_H

/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/IRBuilder.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
50namespace llvm {
51
52class APInt;
53class MDNode;
54class 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.
61class IRBuilderDefaultInserter {
62protected:
63 void InsertHelper(Instruction *I, const Twine &Name,
64 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
65 if (BB) BB->getInstList().insert(InsertPt, I);
66 I->setName(Name);
67 }
68};
69
70/// Provides an 'InsertHelper' that calls a user-provided callback after
71/// performing the default insertion.
72class IRBuilderCallbackInserter : IRBuilderDefaultInserter {
73 std::function<void(Instruction *)> Callback;
74
75public:
76 IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback)
77 : Callback(std::move(Callback)) {}
78
79protected:
80 void InsertHelper(Instruction *I, const Twine &Name,
81 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
82 IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
83 Callback(I);
84 }
85};
86
87/// Common base class shared among various IRBuilders.
88class IRBuilderBase {
89 DebugLoc CurDbgLocation;
90
91protected:
92 BasicBlock *BB;
93 BasicBlock::iterator InsertPt;
94 LLVMContext &Context;
95
96 MDNode *DefaultFPMathTag;
97 FastMathFlags FMF;
98
99 bool IsFPConstrained;
100 ConstrainedFPIntrinsic::ExceptionBehavior DefaultConstrainedExcept;
101 ConstrainedFPIntrinsic::RoundingMode DefaultConstrainedRounding;
102
103 ArrayRef<OperandBundleDef> DefaultOperandBundles;
104
105public:
106 IRBuilderBase(LLVMContext &context, MDNode *FPMathTag = nullptr,
107 ArrayRef<OperandBundleDef> OpBundles = None)
108 : Context(context), DefaultFPMathTag(FPMathTag), IsFPConstrained(false),
109 DefaultConstrainedExcept(ConstrainedFPIntrinsic::ebStrict),
110 DefaultConstrainedRounding(ConstrainedFPIntrinsic::rmDynamic),
111 DefaultOperandBundles(OpBundles) {
112 ClearInsertionPoint();
113 }
114
115 //===--------------------------------------------------------------------===//
116 // Builder configuration methods
117 //===--------------------------------------------------------------------===//
118
119 /// Clear the insertion point: created instructions will not be
120 /// inserted into a block.
121 void ClearInsertionPoint() {
122 BB = nullptr;
123 InsertPt = BasicBlock::iterator();
124 }
125
126 BasicBlock *GetInsertBlock() const { return BB; }
127 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
128 LLVMContext &getContext() const { return Context; }
129
130 /// This specifies that created instructions should be appended to the
131 /// end of the specified block.
132 void SetInsertPoint(BasicBlock *TheBB) {
133 BB = TheBB;
134 InsertPt = BB->end();
135 }
136
137 /// This specifies that created instructions should be inserted before
138 /// the specified instruction.
139 void SetInsertPoint(Instruction *I) {
140 BB = I->getParent();
141 InsertPt = I->getIterator();
142 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 142, __PRETTY_FUNCTION__))
;
143 SetCurrentDebugLocation(I->getDebugLoc());
144 }
145
146 /// This specifies that created instructions should be inserted at the
147 /// specified point.
148 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
149 BB = TheBB;
150 InsertPt = IP;
151 if (IP != TheBB->end())
152 SetCurrentDebugLocation(IP->getDebugLoc());
153 }
154
155 /// Set location information used by debugging information.
156 void SetCurrentDebugLocation(DebugLoc L) { CurDbgLocation = std::move(L); }
157
158 /// Get location information used by debugging information.
159 const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
160
161 /// If this builder has a current debug location, set it on the
162 /// specified instruction.
163 void SetInstDebugLocation(Instruction *I) const {
164 if (CurDbgLocation)
165 I->setDebugLoc(CurDbgLocation);
166 }
167
168 /// Get the return type of the current function that we're emitting
169 /// into.
170 Type *getCurrentFunctionReturnType() const;
171
172 /// InsertPoint - A saved insertion point.
173 class InsertPoint {
174 BasicBlock *Block = nullptr;
175 BasicBlock::iterator Point;
176
177 public:
178 /// Creates a new insertion point which doesn't point to anything.
179 InsertPoint() = default;
180
181 /// Creates a new insertion point at the given location.
182 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
183 : Block(InsertBlock), Point(InsertPoint) {}
184
185 /// Returns true if this insert point is set.
186 bool isSet() const { return (Block != nullptr); }
187
188 BasicBlock *getBlock() const { return Block; }
189 BasicBlock::iterator getPoint() const { return Point; }
190 };
191
192 /// Returns the current insert point.
193 InsertPoint saveIP() const {
194 return InsertPoint(GetInsertBlock(), GetInsertPoint());
195 }
196
197 /// Returns the current insert point, clearing it in the process.
198 InsertPoint saveAndClearIP() {
199 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
200 ClearInsertionPoint();
201 return IP;
202 }
203
204 /// Sets the current insert point to a previously-saved location.
205 void restoreIP(InsertPoint IP) {
206 if (IP.isSet())
207 SetInsertPoint(IP.getBlock(), IP.getPoint());
208 else
209 ClearInsertionPoint();
210 }
211
212 /// Get the floating point math metadata being used.
213 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
214
215 /// Get the flags to be applied to created floating point ops
216 FastMathFlags getFastMathFlags() const { return FMF; }
217
218 /// Clear the fast-math flags.
219 void clearFastMathFlags() { FMF.clear(); }
220
221 /// Set the floating point math metadata to be used.
222 void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
223
224 /// Set the fast-math flags to be used with generated fp-math operators
225 void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
226
227 /// Enable/Disable use of constrained floating point math. When
228 /// enabled the CreateF<op>() calls instead create constrained
229 /// floating point intrinsic calls. Fast math flags are unaffected
230 /// by this setting.
231 void setIsFPConstrained(bool IsCon) { IsFPConstrained = IsCon; }
232
233 /// Query for the use of constrained floating point math
234 bool getIsFPConstrained() { return IsFPConstrained; }
235
236 /// Set the exception handling to be used with constrained floating point
237 void setDefaultConstrainedExcept(
238 ConstrainedFPIntrinsic::ExceptionBehavior NewExcept) {
239 DefaultConstrainedExcept = NewExcept;
240 }
241
242 /// Set the rounding mode handling to be used with constrained floating point
243 void setDefaultConstrainedRounding(
244 ConstrainedFPIntrinsic::RoundingMode NewRounding) {
245 DefaultConstrainedRounding = NewRounding;
246 }
247
248 /// Get the exception handling used with constrained floating point
249 ConstrainedFPIntrinsic::ExceptionBehavior getDefaultConstrainedExcept() {
250 return DefaultConstrainedExcept;
251 }
252
253 /// Get the rounding mode handling used with constrained floating point
254 ConstrainedFPIntrinsic::RoundingMode getDefaultConstrainedRounding() {
255 return DefaultConstrainedRounding;
256 }
257
258 //===--------------------------------------------------------------------===//
259 // RAII helpers.
260 //===--------------------------------------------------------------------===//
261
262 // RAII object that stores the current insertion point and restores it
263 // when the object is destroyed. This includes the debug location.
264 class InsertPointGuard {
265 IRBuilderBase &Builder;
266 AssertingVH<BasicBlock> Block;
267 BasicBlock::iterator Point;
268 DebugLoc DbgLoc;
269
270 public:
271 InsertPointGuard(IRBuilderBase &B)
272 : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
273 DbgLoc(B.getCurrentDebugLocation()) {}
274
275 InsertPointGuard(const InsertPointGuard &) = delete;
276 InsertPointGuard &operator=(const InsertPointGuard &) = delete;
277
278 ~InsertPointGuard() {
279 Builder.restoreIP(InsertPoint(Block, Point));
280 Builder.SetCurrentDebugLocation(DbgLoc);
281 }
282 };
283
284 // RAII object that stores the current fast math settings and restores
285 // them when the object is destroyed.
286 class FastMathFlagGuard {
287 IRBuilderBase &Builder;
288 FastMathFlags FMF;
289 MDNode *FPMathTag;
290
291 public:
292 FastMathFlagGuard(IRBuilderBase &B)
293 : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {}
294
295 FastMathFlagGuard(const FastMathFlagGuard &) = delete;
296 FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete;
297
298 ~FastMathFlagGuard() {
299 Builder.FMF = FMF;
300 Builder.DefaultFPMathTag = FPMathTag;
301 }
302 };
303
304 //===--------------------------------------------------------------------===//
305 // Miscellaneous creation methods.
306 //===--------------------------------------------------------------------===//
307
308 /// Make a new global variable with initializer type i8*
309 ///
310 /// Make a new global variable with an initializer that has array of i8 type
311 /// filled in with the null terminated string value specified. The new global
312 /// variable will be marked mergable with any others of the same contents. If
313 /// Name is specified, it is the name of the global variable created.
314 GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
315 unsigned AddressSpace = 0);
316
317 /// Get a constant value representing either true or false.
318 ConstantInt *getInt1(bool V) {
319 return ConstantInt::get(getInt1Ty(), V);
320 }
321
322 /// Get the constant value for i1 true.
323 ConstantInt *getTrue() {
324 return ConstantInt::getTrue(Context);
325 }
326
327 /// Get the constant value for i1 false.
328 ConstantInt *getFalse() {
329 return ConstantInt::getFalse(Context);
330 }
331
332 /// Get a constant 8-bit value.
333 ConstantInt *getInt8(uint8_t C) {
334 return ConstantInt::get(getInt8Ty(), C);
335 }
336
337 /// Get a constant 16-bit value.
338 ConstantInt *getInt16(uint16_t C) {
339 return ConstantInt::get(getInt16Ty(), C);
340 }
341
342 /// Get a constant 32-bit value.
343 ConstantInt *getInt32(uint32_t C) {
344 return ConstantInt::get(getInt32Ty(), C);
345 }
346
347 /// Get a constant 64-bit value.
348 ConstantInt *getInt64(uint64_t C) {
349 return ConstantInt::get(getInt64Ty(), C);
350 }
351
352 /// Get a constant N-bit value, zero extended or truncated from
353 /// a 64-bit value.
354 ConstantInt *getIntN(unsigned N, uint64_t C) {
355 return ConstantInt::get(getIntNTy(N), C);
356 }
357
358 /// Get a constant integer value.
359 ConstantInt *getInt(const APInt &AI) {
360 return ConstantInt::get(Context, AI);
361 }
362
363 //===--------------------------------------------------------------------===//
364 // Type creation methods
365 //===--------------------------------------------------------------------===//
366
367 /// Fetch the type representing a single bit
368 IntegerType *getInt1Ty() {
369 return Type::getInt1Ty(Context);
370 }
371
372 /// Fetch the type representing an 8-bit integer.
373 IntegerType *getInt8Ty() {
374 return Type::getInt8Ty(Context);
375 }
376
377 /// Fetch the type representing a 16-bit integer.
378 IntegerType *getInt16Ty() {
379 return Type::getInt16Ty(Context);
380 }
381
382 /// Fetch the type representing a 32-bit integer.
383 IntegerType *getInt32Ty() {
384 return Type::getInt32Ty(Context);
385 }
386
387 /// Fetch the type representing a 64-bit integer.
388 IntegerType *getInt64Ty() {
389 return Type::getInt64Ty(Context);
390 }
391
392 /// Fetch the type representing a 128-bit integer.
393 IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); }
394
395 /// Fetch the type representing an N-bit integer.
396 IntegerType *getIntNTy(unsigned N) {
397 return Type::getIntNTy(Context, N);
398 }
399
400 /// Fetch the type representing a 16-bit floating point value.
401 Type *getHalfTy() {
402 return Type::getHalfTy(Context);
403 }
404
405 /// Fetch the type representing a 32-bit floating point value.
406 Type *getFloatTy() {
407 return Type::getFloatTy(Context);
408 }
409
410 /// Fetch the type representing a 64-bit floating point value.
411 Type *getDoubleTy() {
412 return Type::getDoubleTy(Context);
413 }
414
415 /// Fetch the type representing void.
416 Type *getVoidTy() {
417 return Type::getVoidTy(Context);
418 }
419
420 /// Fetch the type representing a pointer to an 8-bit integer value.
421 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
422 return Type::getInt8PtrTy(Context, AddrSpace);
423 }
424
425 /// Fetch the type representing a pointer to an integer value.
426 IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
427 return DL.getIntPtrType(Context, AddrSpace);
428 }
429
430 //===--------------------------------------------------------------------===//
431 // Intrinsic creation methods
432 //===--------------------------------------------------------------------===//
433
434 /// Create and insert a memset to the specified pointer and the
435 /// specified value.
436 ///
437 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
438 /// specified, it will be added to the instruction. Likewise with alias.scope
439 /// and noalias tags.
440 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
441 bool isVolatile = false, MDNode *TBAATag = nullptr,
442 MDNode *ScopeTag = nullptr,
443 MDNode *NoAliasTag = nullptr) {
444 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
445 TBAATag, ScopeTag, NoAliasTag);
446 }
447
448 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
449 bool isVolatile = false, MDNode *TBAATag = nullptr,
450 MDNode *ScopeTag = nullptr,
451 MDNode *NoAliasTag = nullptr);
452
453 /// Create and insert an element unordered-atomic memset of the region of
454 /// memory starting at the given pointer to the given value.
455 ///
456 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
457 /// specified, it will be added to the instruction. Likewise with alias.scope
458 /// and noalias tags.
459 CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
460 uint64_t Size, unsigned Align,
461 uint32_t ElementSize,
462 MDNode *TBAATag = nullptr,
463 MDNode *ScopeTag = nullptr,
464 MDNode *NoAliasTag = nullptr) {
465 return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size), Align,
466 ElementSize, TBAATag, ScopeTag,
467 NoAliasTag);
468 }
469
470 CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
471 Value *Size, unsigned Align,
472 uint32_t ElementSize,
473 MDNode *TBAATag = nullptr,
474 MDNode *ScopeTag = nullptr,
475 MDNode *NoAliasTag = nullptr);
476
477 /// Create and insert a memcpy between the specified pointers.
478 ///
479 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
480 /// specified, it will be added to the instruction. Likewise with alias.scope
481 /// and noalias tags.
482 CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src,
483 unsigned SrcAlign, uint64_t Size,
484 bool isVolatile = false, MDNode *TBAATag = nullptr,
485 MDNode *TBAAStructTag = nullptr,
486 MDNode *ScopeTag = nullptr,
487 MDNode *NoAliasTag = nullptr) {
488 return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
489 isVolatile, TBAATag, TBAAStructTag, ScopeTag,
490 NoAliasTag);
491 }
492
493 CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src,
494 unsigned SrcAlign, Value *Size,
495 bool isVolatile = false, MDNode *TBAATag = nullptr,
496 MDNode *TBAAStructTag = nullptr,
497 MDNode *ScopeTag = nullptr,
498 MDNode *NoAliasTag = nullptr);
499
500 /// Create and insert an element unordered-atomic memcpy between the
501 /// specified pointers.
502 ///
503 /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively.
504 ///
505 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
506 /// specified, it will be added to the instruction. Likewise with alias.scope
507 /// and noalias tags.
508 CallInst *CreateElementUnorderedAtomicMemCpy(
509 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
510 uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr,
511 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
512 MDNode *NoAliasTag = nullptr) {
513 return CreateElementUnorderedAtomicMemCpy(
514 Dst, DstAlign, Src, SrcAlign, getInt64(Size), ElementSize, TBAATag,
515 TBAAStructTag, ScopeTag, NoAliasTag);
516 }
517
518 CallInst *CreateElementUnorderedAtomicMemCpy(
519 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size,
520 uint32_t ElementSize, MDNode *TBAATag = nullptr,
521 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
522 MDNode *NoAliasTag = nullptr);
523
524 /// Create and insert a memmove between the specified
525 /// pointers.
526 ///
527 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
528 /// specified, it will be added to the instruction. Likewise with alias.scope
529 /// and noalias tags.
530 CallInst *CreateMemMove(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
531 uint64_t Size, bool isVolatile = false,
532 MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr,
533 MDNode *NoAliasTag = nullptr) {
534 return CreateMemMove(Dst, DstAlign, Src, SrcAlign, getInt64(Size), isVolatile,
535 TBAATag, ScopeTag, NoAliasTag);
536 }
537
538 CallInst *CreateMemMove(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
539 Value *Size, bool isVolatile = false, MDNode *TBAATag = nullptr,
540 MDNode *ScopeTag = nullptr,
541 MDNode *NoAliasTag = nullptr);
542
543 /// \brief Create and insert an element unordered-atomic memmove between the
544 /// specified pointers.
545 ///
546 /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers,
547 /// respectively.
548 ///
549 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
550 /// specified, it will be added to the instruction. Likewise with alias.scope
551 /// and noalias tags.
552 CallInst *CreateElementUnorderedAtomicMemMove(
553 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
554 uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr,
555 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
556 MDNode *NoAliasTag = nullptr) {
557 return CreateElementUnorderedAtomicMemMove(
558 Dst, DstAlign, Src, SrcAlign, getInt64(Size), ElementSize, TBAATag,
559 TBAAStructTag, ScopeTag, NoAliasTag);
560 }
561
562 CallInst *CreateElementUnorderedAtomicMemMove(
563 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size,
564 uint32_t ElementSize, MDNode *TBAATag = nullptr,
565 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
566 MDNode *NoAliasTag = nullptr);
567
568 /// Create a vector fadd reduction intrinsic of the source vector.
569 /// The first parameter is a scalar accumulator value for ordered reductions.
570 CallInst *CreateFAddReduce(Value *Acc, Value *Src);
571
572 /// Create a vector fmul reduction intrinsic of the source vector.
573 /// The first parameter is a scalar accumulator value for ordered reductions.
574 CallInst *CreateFMulReduce(Value *Acc, Value *Src);
575
576 /// Create a vector int add reduction intrinsic of the source vector.
577 CallInst *CreateAddReduce(Value *Src);
578
579 /// Create a vector int mul reduction intrinsic of the source vector.
580 CallInst *CreateMulReduce(Value *Src);
581
582 /// Create a vector int AND reduction intrinsic of the source vector.
583 CallInst *CreateAndReduce(Value *Src);
584
585 /// Create a vector int OR reduction intrinsic of the source vector.
586 CallInst *CreateOrReduce(Value *Src);
587
588 /// Create a vector int XOR reduction intrinsic of the source vector.
589 CallInst *CreateXorReduce(Value *Src);
590
591 /// Create a vector integer max reduction intrinsic of the source
592 /// vector.
593 CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false);
594
595 /// Create a vector integer min reduction intrinsic of the source
596 /// vector.
597 CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false);
598
599 /// Create a vector float max reduction intrinsic of the source
600 /// vector.
601 CallInst *CreateFPMaxReduce(Value *Src, bool NoNaN = false);
602
603 /// Create a vector float min reduction intrinsic of the source
604 /// vector.
605 CallInst *CreateFPMinReduce(Value *Src, bool NoNaN = false);
606
607 /// Create a lifetime.start intrinsic.
608 ///
609 /// If the pointer isn't i8* it will be converted.
610 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
611
612 /// Create a lifetime.end intrinsic.
613 ///
614 /// If the pointer isn't i8* it will be converted.
615 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
616
617 /// Create a call to invariant.start intrinsic.
618 ///
619 /// If the pointer isn't i8* it will be converted.
620 CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr);
621
622 /// Create a call to Masked Load intrinsic
623 CallInst *CreateMaskedLoad(Value *Ptr, unsigned Align, Value *Mask,
624 Value *PassThru = nullptr, const Twine &Name = "");
625
626 /// Create a call to Masked Store intrinsic
627 CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Align,
628 Value *Mask);
629
630 /// Create a call to Masked Gather intrinsic
631 CallInst *CreateMaskedGather(Value *Ptrs, unsigned Align,
632 Value *Mask = nullptr,
633 Value *PassThru = nullptr,
634 const Twine& Name = "");
635
636 /// Create a call to Masked Scatter intrinsic
637 CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Align,
638 Value *Mask = nullptr);
639
640 /// Create an assume intrinsic call that allows the optimizer to
641 /// assume that the provided condition will be true.
642 CallInst *CreateAssumption(Value *Cond);
643
644 /// Create a call to the experimental.gc.statepoint intrinsic to
645 /// start a new statepoint sequence.
646 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
647 Value *ActualCallee,
648 ArrayRef<Value *> CallArgs,
649 ArrayRef<Value *> DeoptArgs,
650 ArrayRef<Value *> GCArgs,
651 const Twine &Name = "");
652
653 /// Create a call to the experimental.gc.statepoint intrinsic to
654 /// start a new statepoint sequence.
655 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
656 Value *ActualCallee, uint32_t Flags,
657 ArrayRef<Use> CallArgs,
658 ArrayRef<Use> TransitionArgs,
659 ArrayRef<Use> DeoptArgs,
660 ArrayRef<Value *> GCArgs,
661 const Twine &Name = "");
662
663 /// Conveninence function for the common case when CallArgs are filled
664 /// in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
665 /// .get()'ed to get the Value pointer.
666 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
667 Value *ActualCallee, ArrayRef<Use> CallArgs,
668 ArrayRef<Value *> DeoptArgs,
669 ArrayRef<Value *> GCArgs,
670 const Twine &Name = "");
671
672 /// Create an invoke to the experimental.gc.statepoint intrinsic to
673 /// start a new statepoint sequence.
674 InvokeInst *
675 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
676 Value *ActualInvokee, BasicBlock *NormalDest,
677 BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
678 ArrayRef<Value *> DeoptArgs,
679 ArrayRef<Value *> GCArgs, const Twine &Name = "");
680
681 /// Create an invoke to the experimental.gc.statepoint intrinsic to
682 /// start a new statepoint sequence.
683 InvokeInst *CreateGCStatepointInvoke(
684 uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
685 BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
686 ArrayRef<Use> InvokeArgs, ArrayRef<Use> TransitionArgs,
687 ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs,
688 const Twine &Name = "");
689
690 // Convenience function for the common case when CallArgs are filled in using
691 // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
692 // get the Value *.
693 InvokeInst *
694 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
695 Value *ActualInvokee, BasicBlock *NormalDest,
696 BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
697 ArrayRef<Value *> DeoptArgs,
698 ArrayRef<Value *> GCArgs, const Twine &Name = "");
699
700 /// Create a call to the experimental.gc.result intrinsic to extract
701 /// the result from a call wrapped in a statepoint.
702 CallInst *CreateGCResult(Instruction *Statepoint,
703 Type *ResultType,
704 const Twine &Name = "");
705
706 /// Create a call to the experimental.gc.relocate intrinsics to
707 /// project the relocated value of one pointer from the statepoint.
708 CallInst *CreateGCRelocate(Instruction *Statepoint,
709 int BaseOffset,
710 int DerivedOffset,
711 Type *ResultType,
712 const Twine &Name = "");
713
714 /// Create a call to intrinsic \p ID with 1 operand which is mangled on its
715 /// type.
716 CallInst *CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V,
717 Instruction *FMFSource = nullptr,
718 const Twine &Name = "");
719
720 /// Create a call to intrinsic \p ID with 2 operands which is mangled on the
721 /// first type.
722 CallInst *CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS,
723 Instruction *FMFSource = nullptr,
724 const Twine &Name = "");
725
726 /// Create a call to intrinsic \p ID with \p args, mangled using \p Types. If
727 /// \p FMFSource is provided, copy fast-math-flags from that instruction to
728 /// the intrinsic.
729 CallInst *CreateIntrinsic(Intrinsic::ID ID, ArrayRef<Type *> Types,
730 ArrayRef<Value *> Args,
731 Instruction *FMFSource = nullptr,
732 const Twine &Name = "");
733
734 /// Create call to the minnum intrinsic.
735 CallInst *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") {
736 return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, nullptr, Name);
737 }
738
739 /// Create call to the maxnum intrinsic.
740 CallInst *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") {
741 return CreateBinaryIntrinsic(Intrinsic::maxnum, LHS, RHS, nullptr, Name);
742 }
743
744 /// Create call to the minimum intrinsic.
745 CallInst *CreateMinimum(Value *LHS, Value *RHS, const Twine &Name = "") {
746 return CreateBinaryIntrinsic(Intrinsic::minimum, LHS, RHS, nullptr, Name);
747 }
748
749 /// Create call to the maximum intrinsic.
750 CallInst *CreateMaximum(Value *LHS, Value *RHS, const Twine &Name = "") {
751 return CreateBinaryIntrinsic(Intrinsic::maximum, LHS, RHS, nullptr, Name);
752 }
753
754private:
755 /// Create a call to a masked intrinsic with given Id.
756 CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
757 ArrayRef<Type *> OverloadedTypes,
758 const Twine &Name = "");
759
760 Value *getCastedInt8PtrValue(Value *Ptr);
761};
762
763/// This provides a uniform API for creating instructions and inserting
764/// them into a basic block: either at the end of a BasicBlock, or at a specific
765/// iterator location in a block.
766///
767/// Note that the builder does not expose the full generality of LLVM
768/// instructions. For access to extra instruction properties, use the mutators
769/// (e.g. setVolatile) on the instructions after they have been
770/// created. Convenience state exists to specify fast-math flags and fp-math
771/// tags.
772///
773/// The first template argument specifies a class to use for creating constants.
774/// This defaults to creating minimally folded constants. The second template
775/// argument allows clients to specify custom insertion hooks that are called on
776/// every newly created insertion.
777template <typename T = ConstantFolder,
778 typename Inserter = IRBuilderDefaultInserter>
779class IRBuilder : public IRBuilderBase, public Inserter {
780 T Folder;
781
782public:
783 IRBuilder(LLVMContext &C, const T &F, Inserter I = Inserter(),
784 MDNode *FPMathTag = nullptr,
785 ArrayRef<OperandBundleDef> OpBundles = None)
786 : IRBuilderBase(C, FPMathTag, OpBundles), Inserter(std::move(I)),
787 Folder(F) {}
788
789 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr,
790 ArrayRef<OperandBundleDef> OpBundles = None)
791 : IRBuilderBase(C, FPMathTag, OpBundles) {}
792
793 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr,
794 ArrayRef<OperandBundleDef> OpBundles = None)
795 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) {
796 SetInsertPoint(TheBB);
797 }
798
799 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr,
800 ArrayRef<OperandBundleDef> OpBundles = None)
801 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles) {
802 SetInsertPoint(TheBB);
803 }
804
805 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr,
806 ArrayRef<OperandBundleDef> OpBundles = None)
807 : IRBuilderBase(IP->getContext(), FPMathTag, OpBundles) {
808 SetInsertPoint(IP);
809 }
810
811 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T &F,
812 MDNode *FPMathTag = nullptr,
813 ArrayRef<OperandBundleDef> OpBundles = None)
814 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) {
815 SetInsertPoint(TheBB, IP);
816 }
817
818 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
819 MDNode *FPMathTag = nullptr,
820 ArrayRef<OperandBundleDef> OpBundles = None)
821 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles) {
822 SetInsertPoint(TheBB, IP);
823 }
824
825 /// Get the constant folder being used.
826 const T &getFolder() { return Folder; }
827
828 /// Insert and return the specified instruction.
829 template<typename InstTy>
830 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
831 this->InsertHelper(I, Name, BB, InsertPt);
832 this->SetInstDebugLocation(I);
833 return I;
834 }
835
836 /// No-op overload to handle constants.
837 Constant *Insert(Constant *C, const Twine& = "") const {
838 return C;
839 }
840
841 //===--------------------------------------------------------------------===//
842 // Instruction creation methods: Terminators
843 //===--------------------------------------------------------------------===//
844
845private:
846 /// Helper to add branch weight and unpredictable metadata onto an
847 /// instruction.
848 /// \returns The annotated instruction.
849 template <typename InstTy>
850 InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) {
851 if (Weights)
852 I->setMetadata(LLVMContext::MD_prof, Weights);
853 if (Unpredictable)
854 I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable);
855 return I;
856 }
857
858public:
859 /// Create a 'ret void' instruction.
860 ReturnInst *CreateRetVoid() {
861 return Insert(ReturnInst::Create(Context));
862 }
863
864 /// Create a 'ret <val>' instruction.
865 ReturnInst *CreateRet(Value *V) {
866 return Insert(ReturnInst::Create(Context, V));
867 }
868
869 /// Create a sequence of N insertvalue instructions,
870 /// with one Value from the retVals array each, that build a aggregate
871 /// return value one value at a time, and a ret instruction to return
872 /// the resulting aggregate value.
873 ///
874 /// This is a convenience function for code that uses aggregate return values
875 /// as a vehicle for having multiple return values.
876 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
877 Value *V = UndefValue::get(getCurrentFunctionReturnType());
878 for (unsigned i = 0; i != N; ++i)
879 V = CreateInsertValue(V, retVals[i], i, "mrv");
880 return Insert(ReturnInst::Create(Context, V));
881 }
882
883 /// Create an unconditional 'br label X' instruction.
884 BranchInst *CreateBr(BasicBlock *Dest) {
885 return Insert(BranchInst::Create(Dest));
886 }
887
888 /// Create a conditional 'br Cond, TrueDest, FalseDest'
889 /// instruction.
890 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
891 MDNode *BranchWeights = nullptr,
892 MDNode *Unpredictable = nullptr) {
893 return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond),
894 BranchWeights, Unpredictable));
895 }
896
897 /// Create a conditional 'br Cond, TrueDest, FalseDest'
898 /// instruction. Copy branch meta data if available.
899 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
900 Instruction *MDSrc) {
901 BranchInst *Br = BranchInst::Create(True, False, Cond);
902 if (MDSrc) {
903 unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable,
904 LLVMContext::MD_make_implicit, LLVMContext::MD_dbg};
905 Br->copyMetadata(*MDSrc, makeArrayRef(&WL[0], 4));
906 }
907 return Insert(Br);
908 }
909
910 /// Create a switch instruction with the specified value, default dest,
911 /// and with a hint for the number of cases that will be added (for efficient
912 /// allocation).
913 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
914 MDNode *BranchWeights = nullptr,
915 MDNode *Unpredictable = nullptr) {
916 return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases),
917 BranchWeights, Unpredictable));
918 }
919
920 /// Create an indirect branch instruction with the specified address
921 /// operand, with an optional hint for the number of destinations that will be
922 /// added (for efficient allocation).
923 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
924 return Insert(IndirectBrInst::Create(Addr, NumDests));
925 }
926
927 /// Create an invoke instruction.
928 InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
929 BasicBlock *NormalDest, BasicBlock *UnwindDest,
930 ArrayRef<Value *> Args,
931 ArrayRef<OperandBundleDef> OpBundles,
932 const Twine &Name = "") {
933 return Insert(
934 InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args, OpBundles),
935 Name);
936 }
937 InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
938 BasicBlock *NormalDest, BasicBlock *UnwindDest,
939 ArrayRef<Value *> Args = None,
940 const Twine &Name = "") {
941 return Insert(InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args),
942 Name);
943 }
944
945 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
946 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
947 ArrayRef<OperandBundleDef> OpBundles,
948 const Twine &Name = "") {
949 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
950 NormalDest, UnwindDest, Args, OpBundles, Name);
951 }
952
953 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
954 BasicBlock *UnwindDest,
955 ArrayRef<Value *> Args = None,
956 const Twine &Name = "") {
957 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
958 NormalDest, UnwindDest, Args, Name);
959 }
960
961 // Deprecated [opaque pointer types]
962 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
963 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
964 ArrayRef<OperandBundleDef> OpBundles,
965 const Twine &Name = "") {
966 return CreateInvoke(
967 cast<FunctionType>(
968 cast<PointerType>(Callee->getType())->getElementType()),
969 Callee, NormalDest, UnwindDest, Args, OpBundles, Name);
970 }
971
972 // Deprecated [opaque pointer types]
973 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
974 BasicBlock *UnwindDest,
975 ArrayRef<Value *> Args = None,
976 const Twine &Name = "") {
977 return CreateInvoke(
978 cast<FunctionType>(
979 cast<PointerType>(Callee->getType())->getElementType()),
980 Callee, NormalDest, UnwindDest, Args, Name);
981 }
982
983 /// \brief Create a callbr instruction.
984 CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
985 BasicBlock *DefaultDest,
986 ArrayRef<BasicBlock *> IndirectDests,
987 ArrayRef<Value *> Args = None,
988 const Twine &Name = "") {
989 return Insert(CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests,
990 Args), Name);
991 }
992 CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
993 BasicBlock *DefaultDest,
994 ArrayRef<BasicBlock *> IndirectDests,
995 ArrayRef<Value *> Args,
996 ArrayRef<OperandBundleDef> OpBundles,
997 const Twine &Name = "") {
998 return Insert(
999 CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args,
1000 OpBundles), Name);
1001 }
1002
1003 CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1004 ArrayRef<BasicBlock *> IndirectDests,
1005 ArrayRef<Value *> Args = None,
1006 const Twine &Name = "") {
1007 return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
1008 DefaultDest, IndirectDests, Args, Name);
1009 }
1010 CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1011 ArrayRef<BasicBlock *> IndirectDests,
1012 ArrayRef<Value *> Args,
1013 ArrayRef<OperandBundleDef> OpBundles,
1014 const Twine &Name = "") {
1015 return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
1016 DefaultDest, IndirectDests, Args, Name);
1017 }
1018
1019 ResumeInst *CreateResume(Value *Exn) {
1020 return Insert(ResumeInst::Create(Exn));
1021 }
1022
1023 CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad,
1024 BasicBlock *UnwindBB = nullptr) {
1025 return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB));
1026 }
1027
1028 CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB,
1029 unsigned NumHandlers,
1030 const Twine &Name = "") {
1031 return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers),
1032 Name);
1033 }
1034
1035 CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args,
1036 const Twine &Name = "") {
1037 return Insert(CatchPadInst::Create(ParentPad, Args), Name);
1038 }
1039
1040 CleanupPadInst *CreateCleanupPad(Value *ParentPad,
1041 ArrayRef<Value *> Args = None,
1042 const Twine &Name = "") {
1043 return Insert(CleanupPadInst::Create(ParentPad, Args), Name);
1044 }
1045
1046 CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) {
1047 return Insert(CatchReturnInst::Create(CatchPad, BB));
1048 }
1049
1050 UnreachableInst *CreateUnreachable() {
1051 return Insert(new UnreachableInst(Context));
1052 }
1053
1054 //===--------------------------------------------------------------------===//
1055 // Instruction creation methods: Binary Operators
1056 //===--------------------------------------------------------------------===//
1057private:
1058 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
1059 Value *LHS, Value *RHS,
1060 const Twine &Name,
1061 bool HasNUW, bool HasNSW) {
1062 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
1063 if (HasNUW) BO->setHasNoUnsignedWrap();
1064 if (HasNSW) BO->setHasNoSignedWrap();
1065 return BO;
1066 }
1067
1068 Instruction *setFPAttrs(Instruction *I, MDNode *FPMD,
1069 FastMathFlags FMF) const {
1070 if (!FPMD)
1071 FPMD = DefaultFPMathTag;
1072 if (FPMD)
1073 I->setMetadata(LLVMContext::MD_fpmath, FPMD);
1074 I->setFastMathFlags(FMF);
1075 return I;
1076 }
1077
1078 Value *foldConstant(Instruction::BinaryOps Opc, Value *L,
1079 Value *R, const Twine &Name) const {
1080 auto *LC = dyn_cast<Constant>(L);
1081 auto *RC = dyn_cast<Constant>(R);
1082 return (LC && RC) ? Insert(Folder.CreateBinOp(Opc, LC, RC), Name) : nullptr;
1083 }
1084
1085 Value *getConstrainedFPRounding(
1086 Optional<ConstrainedFPIntrinsic::RoundingMode> Rounding) {
1087 ConstrainedFPIntrinsic::RoundingMode UseRounding =
1088 DefaultConstrainedRounding;
1089
1090 if (Rounding.hasValue())
1091 UseRounding = Rounding.getValue();
1092
1093 Optional<StringRef> RoundingStr =
1094 ConstrainedFPIntrinsic::RoundingModeToStr(UseRounding);
1095 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 1095, __PRETTY_FUNCTION__))
;
1096 auto *RoundingMDS = MDString::get(Context, RoundingStr.getValue());
1097
1098 return MetadataAsValue::get(Context, RoundingMDS);
1099 }
1100
1101 Value *getConstrainedFPExcept(
1102 Optional<ConstrainedFPIntrinsic::ExceptionBehavior> Except) {
1103 ConstrainedFPIntrinsic::ExceptionBehavior UseExcept =
1104 DefaultConstrainedExcept;
1105
1106 if (Except.hasValue())
1107 UseExcept = Except.getValue();
1108
1109 Optional<StringRef> ExceptStr =
1110 ConstrainedFPIntrinsic::ExceptionBehaviorToStr(UseExcept);
1111 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 1111, __PRETTY_FUNCTION__))
;
1112 auto *ExceptMDS = MDString::get(Context, ExceptStr.getValue());
1113
1114 return MetadataAsValue::get(Context, ExceptMDS);
1115 }
1116
1117public:
1118 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
1119 bool HasNUW = false, bool HasNSW = false) {
1120 if (auto *LC = dyn_cast<Constant>(LHS))
1121 if (auto *RC = dyn_cast<Constant>(RHS))
1122 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
1123 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
1124 HasNUW, HasNSW);
1125 }
1126
1127 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1128 return CreateAdd(LHS, RHS, Name, false, true);
1129 }
1130
1131 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1132 return CreateAdd(LHS, RHS, Name, true, false);
1133 }
1134
1135 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
1136 bool HasNUW = false, bool HasNSW = false) {
1137 if (auto *LC = dyn_cast<Constant>(LHS))
1138 if (auto *RC = dyn_cast<Constant>(RHS))
1139 return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
1140 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
1141 HasNUW, HasNSW);
1142 }
1143
1144 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1145 return CreateSub(LHS, RHS, Name, false, true);
1146 }
1147
1148 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1149 return CreateSub(LHS, RHS, Name, true, false);
1150 }
1151
1152 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
1153 bool HasNUW = false, bool HasNSW = false) {
1154 if (auto *LC = dyn_cast<Constant>(LHS))
1155 if (auto *RC = dyn_cast<Constant>(RHS))
1156 return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
1157 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
1158 HasNUW, HasNSW);
1159 }
1160
1161 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1162 return CreateMul(LHS, RHS, Name, false, true);
1163 }
1164
1165 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1166 return CreateMul(LHS, RHS, Name, true, false);
1167 }
1168
1169 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1170 bool isExact = false) {
1171 if (auto *LC = dyn_cast<Constant>(LHS))
1172 if (auto *RC = dyn_cast<Constant>(RHS))
1173 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
1174 if (!isExact)
1175 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
1176 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
1177 }
1178
1179 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1180 return CreateUDiv(LHS, RHS, Name, true);
1181 }
1182
1183 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1184 bool isExact = false) {
1185 if (auto *LC = dyn_cast<Constant>(LHS))
1186 if (auto *RC = dyn_cast<Constant>(RHS))
1187 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
1188 if (!isExact)
1189 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
1190 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
1191 }
1192
1193 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1194 return CreateSDiv(LHS, RHS, Name, true);
1195 }
1196
1197 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
1198 if (Value *V = foldConstant(Instruction::URem, LHS, RHS, Name)) return V;
1199 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
1200 }
1201
1202 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
1203 if (Value *V = foldConstant(Instruction::SRem, LHS, RHS, Name)) return V;
1204 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
1205 }
1206
1207 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
1208 bool HasNUW = false, bool HasNSW = false) {
1209 if (auto *LC = dyn_cast<Constant>(LHS))
1210 if (auto *RC = dyn_cast<Constant>(RHS))
1211 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
1212 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
1213 HasNUW, HasNSW);
1214 }
1215
1216 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
1217 bool HasNUW = false, bool HasNSW = false) {
1218 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1219 HasNUW, HasNSW);
1220 }
1221
1222 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
1223 bool HasNUW = false, bool HasNSW = false) {
1224 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1225 HasNUW, HasNSW);
1226 }
1227
1228 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
1229 bool isExact = false) {
1230 if (auto *LC = dyn_cast<Constant>(LHS))
1231 if (auto *RC = dyn_cast<Constant>(RHS))
1232 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
1233 if (!isExact)
1234 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
1235 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
1236 }
1237
1238 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1239 bool isExact = false) {
1240 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1241 }
1242
1243 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1244 bool isExact = false) {
1245 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1246 }
1247
1248 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
1249 bool isExact = false) {
1250 if (auto *LC = dyn_cast<Constant>(LHS))
1251 if (auto *RC = dyn_cast<Constant>(RHS))
1252 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
1253 if (!isExact)
1254 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
1255 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
1256 }
1257
1258 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1259 bool isExact = false) {
1260 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1261 }
1262
1263 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1264 bool isExact = false) {
1265 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1266 }
1267
1268 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
1269 if (auto *RC = dyn_cast<Constant>(RHS)) {
1270 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isMinusOne())
1271 return LHS; // LHS & -1 -> LHS
1272 if (auto *LC = dyn_cast<Constant>(LHS))
1273 return Insert(Folder.CreateAnd(LC, RC), Name);
1274 }
1275 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
1276 }
1277
1278 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1279 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1280 }
1281
1282 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1283 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1284 }
1285
1286 Value *CreateAnd(ArrayRef<Value*> Ops) {
1287 assert(!Ops.empty())((!Ops.empty()) ? static_cast<void> (0) : __assert_fail
("!Ops.empty()", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/IRBuilder.h"
, 1287, __PRETTY_FUNCTION__))
;
1288 Value *Accum = Ops[0];
1289 for (unsigned i = 1; i < Ops.size(); i++)
1290 Accum = CreateAnd(Accum, Ops[i]);
1291 return Accum;
1292 }
1293
1294 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
1295 if (auto *RC = dyn_cast<Constant>(RHS)) {
1296 if (RC->isNullValue())
1297 return LHS; // LHS | 0 -> LHS
1298 if (auto *LC = dyn_cast<Constant>(LHS))
1299 return Insert(Folder.CreateOr(LC, RC), Name);
1300 }
1301 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
1302 }
1303
1304 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1305 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1306 }
1307
1308 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1309 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1310 }
1311
1312 Value *CreateOr(ArrayRef<Value*> Ops) {
1313 assert(!Ops.empty())((!Ops.empty()) ? static_cast<void> (0) : __assert_fail
("!Ops.empty()", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/IRBuilder.h"
, 1313, __PRETTY_FUNCTION__))
;
1314 Value *Accum = Ops[0];
1315 for (unsigned i = 1; i < Ops.size(); i++)
1316 Accum = CreateOr(Accum, Ops[i]);
1317 return Accum;
1318 }
1319
1320 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
1321 if (Value *V = foldConstant(Instruction::Xor, LHS, RHS, Name)) return V;
1322 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
1323 }
1324
1325 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1326 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1327 }
1328
1329 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1330 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1331 }
1332
1333 Value *CreateFAdd(Value *L, Value *R, const Twine &Name = "",
1334 MDNode *FPMD = nullptr) {
1335 if (IsFPConstrained)
1336 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd,
1337 L, R, nullptr, Name, FPMD);
1338
1339 if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V;
1340 Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD, FMF);
1341 return Insert(I, Name);
1342 }
1343
1344 /// Copy fast-math-flags from an instruction rather than using the builder's
1345 /// default FMF.
1346 Value *CreateFAddFMF(Value *L, Value *R, Instruction *FMFSource,
1347 const Twine &Name = "") {
1348 if (IsFPConstrained)
1349 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd,
1350 L, R, FMFSource, Name);
1351
1352 if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V;
1353 Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), nullptr,
1354 FMFSource->getFastMathFlags());
1355 return Insert(I, Name);
1356 }
1357
1358 Value *CreateFSub(Value *L, Value *R, const Twine &Name = "",
1359 MDNode *FPMD = nullptr) {
1360 if (IsFPConstrained)
1361 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub,
1362 L, R, nullptr, Name, FPMD);
1363
1364 if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V;
1365 Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD, FMF);
1366 return Insert(I, Name);
1367 }
1368
1369 /// Copy fast-math-flags from an instruction rather than using the builder's
1370 /// default FMF.
1371 Value *CreateFSubFMF(Value *L, Value *R, Instruction *FMFSource,
1372 const Twine &Name = "") {
1373 if (IsFPConstrained)
1374 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub,
1375 L, R, FMFSource, Name);
1376
1377 if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V;
1378 Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), nullptr,
1379 FMFSource->getFastMathFlags());
1380 return Insert(I, Name);
1381 }
1382
1383 Value *CreateFMul(Value *L, Value *R, const Twine &Name = "",
1384 MDNode *FPMD = nullptr) {
1385 if (IsFPConstrained)
1386 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul,
1387 L, R, nullptr, Name, FPMD);
1388
1389 if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V;
1390 Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD, FMF);
1391 return Insert(I, Name);
1392 }
1393
1394 /// Copy fast-math-flags from an instruction rather than using the builder's
1395 /// default FMF.
1396 Value *CreateFMulFMF(Value *L, Value *R, Instruction *FMFSource,
1397 const Twine &Name = "") {
1398 if (IsFPConstrained)
1399 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul,
1400 L, R, FMFSource, Name);
1401
1402 if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V;
1403 Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), nullptr,
1404 FMFSource->getFastMathFlags());
1405 return Insert(I, Name);
1406 }
1407
1408 Value *CreateFDiv(Value *L, Value *R, const Twine &Name = "",
1409 MDNode *FPMD = nullptr) {
1410 if (IsFPConstrained)
1411 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv,
1412 L, R, nullptr, Name, FPMD);
1413
1414 if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V;
1415 Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD, FMF);
1416 return Insert(I, Name);
1417 }
1418
1419 /// Copy fast-math-flags from an instruction rather than using the builder's
1420 /// default FMF.
1421 Value *CreateFDivFMF(Value *L, Value *R, Instruction *FMFSource,
1422 const Twine &Name = "") {
1423 if (IsFPConstrained)
1424 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv,
1425 L, R, FMFSource, Name);
1426
1427 if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V;
1428 Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), nullptr,
1429 FMFSource->getFastMathFlags());
1430 return Insert(I, Name);
1431 }
1432
1433 Value *CreateFRem(Value *L, Value *R, const Twine &Name = "",
1434 MDNode *FPMD = nullptr) {
1435 if (IsFPConstrained)
1436 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem,
1437 L, R, nullptr, Name, FPMD);
1438
1439 if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V;
1440 Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD, FMF);
1441 return Insert(I, Name);
1442 }
1443
1444 /// Copy fast-math-flags from an instruction rather than using the builder's
1445 /// default FMF.
1446 Value *CreateFRemFMF(Value *L, Value *R, Instruction *FMFSource,
1447 const Twine &Name = "") {
1448 if (IsFPConstrained)
1449 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem,
1450 L, R, FMFSource, Name);
1451
1452 if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V;
1453 Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), nullptr,
1454 FMFSource->getFastMathFlags());
1455 return Insert(I, Name);
1456 }
1457
1458 Value *CreateBinOp(Instruction::BinaryOps Opc,
1459 Value *LHS, Value *RHS, const Twine &Name = "",
1460 MDNode *FPMathTag = nullptr) {
1461 if (Value *V = foldConstant(Opc, LHS, RHS, Name)) return V;
1462 Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
1463 if (isa<FPMathOperator>(BinOp))
1464 BinOp = setFPAttrs(BinOp, FPMathTag, FMF);
1465 return Insert(BinOp, Name);
1466 }
1467
1468 CallInst *CreateConstrainedFPBinOp(
1469 Intrinsic::ID ID, Value *L, Value *R, Instruction *FMFSource = nullptr,
1470 const Twine &Name = "", MDNode *FPMathTag = nullptr,
1471 Optional<ConstrainedFPIntrinsic::RoundingMode> Rounding = None,
1472 Optional<ConstrainedFPIntrinsic::ExceptionBehavior> Except = None) {
1473 Value *RoundingV = getConstrainedFPRounding(Rounding);
1474 Value *ExceptV = getConstrainedFPExcept(Except);
1475
1476 FastMathFlags UseFMF = FMF;
1477 if (FMFSource)
1478 UseFMF = FMFSource->getFastMathFlags();
1479
1480 CallInst *C = CreateIntrinsic(ID, {L->getType()},
1481 {L, R, RoundingV, ExceptV}, nullptr, Name);
1482 return cast<CallInst>(setFPAttrs(C, FPMathTag, UseFMF));
1483 }
1484
1485 Value *CreateNeg(Value *V, const Twine &Name = "",
1486 bool HasNUW = false, bool HasNSW = false) {
1487 if (auto *VC = dyn_cast<Constant>(V))
1488 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
1489 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
1490 if (HasNUW) BO->setHasNoUnsignedWrap();
1491 if (HasNSW) BO->setHasNoSignedWrap();
1492 return BO;
1493 }
1494
1495 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
1496 return CreateNeg(V, Name, false, true);
1497 }
1498
1499 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
1500 return CreateNeg(V, Name, true, false);
1501 }
1502
1503 Value *CreateFNeg(Value *V, const Twine &Name = "",
1504 MDNode *FPMathTag = nullptr) {
1505 if (auto *VC = dyn_cast<Constant>(V))
1506 return Insert(Folder.CreateFNeg(VC), Name);
1507 return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), FPMathTag, FMF),
1508 Name);
1509 }
1510
1511 /// Copy fast-math-flags from an instruction rather than using the builder's
1512 /// default FMF.
1513 Value *CreateFNegFMF(Value *V, Instruction *FMFSource,
1514 const Twine &Name = "") {
1515 if (auto *VC = dyn_cast<Constant>(V))
1516 return Insert(Folder.CreateFNeg(VC), Name);
1517 return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), nullptr,
1518 FMFSource->getFastMathFlags()),
1519 Name);
1520 }
1521
1522 Value *CreateNot(Value *V, const Twine &Name = "") {
1523 if (auto *VC = dyn_cast<Constant>(V))
1524 return Insert(Folder.CreateNot(VC), Name);
1525 return Insert(BinaryOperator::CreateNot(V), Name);
1526 }
1527
1528 Value *CreateUnOp(Instruction::UnaryOps Opc,
1529 Value *V, const Twine &Name = "",
1530 MDNode *FPMathTag = nullptr) {
1531 if (auto *VC = dyn_cast<Constant>(V))
1532 return Insert(Folder.CreateUnOp(Opc, VC), Name);
1533 Instruction *UnOp = UnaryOperator::Create(Opc, V);
1534 if (isa<FPMathOperator>(UnOp))
1535 UnOp = setFPAttrs(UnOp, FPMathTag, FMF);
1536 return Insert(UnOp, Name);
1537 }
1538
1539 /// Create either a UnaryOperator or BinaryOperator depending on \p Opc.
1540 /// Correct number of operands must be passed accordingly.
1541 Value *CreateNAryOp(unsigned Opc, ArrayRef<Value *> Ops,
1542 const Twine &Name = "",
1543 MDNode *FPMathTag = nullptr) {
1544 if (Instruction::isBinaryOp(Opc)) {
1545 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 1545, __PRETTY_FUNCTION__))
;
1546 return CreateBinOp(static_cast<Instruction::BinaryOps>(Opc),
1547 Ops[0], Ops[1], Name, FPMathTag);
1548 }
1549 if (Instruction::isUnaryOp(Opc)) {
1550 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 1550, __PRETTY_FUNCTION__))
;
1551 return CreateUnOp(static_cast<Instruction::UnaryOps>(Opc),
1552 Ops[0], Name, FPMathTag);
1553 }
1554 llvm_unreachable("Unexpected opcode!")::llvm::llvm_unreachable_internal("Unexpected opcode!", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/IRBuilder.h"
, 1554)
;
1555 }
1556
1557 //===--------------------------------------------------------------------===//
1558 // Instruction creation methods: Memory Instructions
1559 //===--------------------------------------------------------------------===//
1560
1561 AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace,
1562 Value *ArraySize = nullptr, const Twine &Name = "") {
1563 return Insert(new AllocaInst(Ty, AddrSpace, ArraySize), Name);
1564 }
1565
1566 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
1567 const Twine &Name = "") {
1568 const DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
1569 return Insert(new AllocaInst(Ty, DL.getAllocaAddrSpace(), ArraySize), Name);
1570 }
1571
1572 /// Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of
1573 /// converting the string to 'bool' for the isVolatile parameter.
1574 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const char *Name) {
1575 return Insert(new LoadInst(Ty, Ptr), Name);
1576 }
1577
1578 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
1579 return Insert(new LoadInst(Ty, Ptr), Name);
1580 }
1581
1582 LoadInst *CreateLoad(Type *Ty, Value *Ptr, bool isVolatile,
1583 const Twine &Name = "") {
1584 return Insert(new LoadInst(Ty, Ptr, Twine(), isVolatile), Name);
1585 }
1586
1587 // Deprecated [opaque pointer types]
1588 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
1589 return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, Name);
1590 }
1591
1592 // Deprecated [opaque pointer types]
1593 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
1594 return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, Name);
1595 }
1596
1597 // Deprecated [opaque pointer types]
1598 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
1599 return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, isVolatile,
1600 Name);
1601 }
1602
1603 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
1604 return Insert(new StoreInst(Val, Ptr, isVolatile));
1605 }
1606
1607 /// Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
1608 /// correctly, instead of converting the string to 'bool' for the isVolatile
1609 /// parameter.
1610 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align,
1611 const char *Name) {
1612 LoadInst *LI = CreateLoad(Ty, Ptr, Name);
1613 LI->setAlignment(MaybeAlign(Align));
1614 return LI;
1615 }
1616 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align,
1617 const Twine &Name = "") {
1618 LoadInst *LI = CreateLoad(Ty, Ptr, Name);
1619 LI->setAlignment(MaybeAlign(Align));
1620 return LI;
1621 }
1622 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align,
1623 bool isVolatile, const Twine &Name = "") {
1624 LoadInst *LI = CreateLoad(Ty, Ptr, isVolatile, Name);
1625 LI->setAlignment(MaybeAlign(Align));
1626 return LI;
1627 }
1628
1629 // Deprecated [opaque pointer types]
1630 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
1631 return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
1632 Align, Name);
1633 }
1634 // Deprecated [opaque pointer types]
1635 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
1636 const Twine &Name = "") {
1637 return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
1638 Align, Name);
1639 }
1640 // Deprecated [opaque pointer types]
1641 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
1642 const Twine &Name = "") {
1643 return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
1644 Align, isVolatile, Name);
1645 }
1646
1647 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
1648 bool isVolatile = false) {
1649 StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
1650 SI->setAlignment(MaybeAlign(Align));
1651 return SI;
1652 }
1653
1654 FenceInst *CreateFence(AtomicOrdering Ordering,
1655 SyncScope::ID SSID = SyncScope::System,
1656 const Twine &Name = "") {
1657 return Insert(new FenceInst(Context, Ordering, SSID), Name);
1658 }
1659
1660 AtomicCmpXchgInst *
1661 CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
1662 AtomicOrdering SuccessOrdering,
1663 AtomicOrdering FailureOrdering,
1664 SyncScope::ID SSID = SyncScope::System) {
1665 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
1666 FailureOrdering, SSID));
1667 }
1668
1669 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
1670 AtomicOrdering Ordering,
1671 SyncScope::ID SSID = SyncScope::System) {
1672 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SSID));
1673 }
1674
1675 Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1676 const Twine &Name = "") {
1677 return CreateGEP(nullptr, Ptr, IdxList, Name);
1678 }
1679
1680 Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1681 const Twine &Name = "") {
1682 if (auto *PC = dyn_cast<Constant>(Ptr)) {
1683 // Every index must be constant.
1684 size_t i, e;
1685 for (i = 0, e = IdxList.size(); i != e; ++i)
1686 if (!isa<Constant>(IdxList[i]))
1687 break;
1688 if (i == e)
1689 return Insert(Folder.CreateGetElementPtr(Ty, PC, IdxList), Name);
1690 }
1691 return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name);
1692 }
1693
1694 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1695 const Twine &Name = "") {
1696 return CreateInBoundsGEP(nullptr, Ptr, IdxList, Name);
1697 }
1698
1699 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1700 const Twine &Name = "") {
1701 if (auto *PC = dyn_cast<Constant>(Ptr)) {
1702 // Every index must be constant.
1703 size_t i, e;
1704 for (i = 0, e = IdxList.size(); i != e; ++i)
1705 if (!isa<Constant>(IdxList[i]))
1706 break;
1707 if (i == e)
1708 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IdxList),
1709 Name);
1710 }
1711 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name);
1712 }
1713
1714 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
1715 return CreateGEP(nullptr, Ptr, Idx, Name);
1716 }
1717
1718 Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") {
1719 if (auto *PC = dyn_cast<Constant>(Ptr))
1720 if (auto *IC = dyn_cast<Constant>(Idx))
1721 return Insert(Folder.CreateGetElementPtr(Ty, PC, IC), Name);
1722 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1723 }
1724
1725 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx,
1726 const Twine &Name = "") {
1727 if (auto *PC = dyn_cast<Constant>(Ptr))
1728 if (auto *IC = dyn_cast<Constant>(Idx))
1729 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IC), Name);
1730 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1731 }
1732
1733 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
1734 return CreateConstGEP1_32(nullptr, Ptr, Idx0, Name);
1735 }
1736
1737 Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1738 const Twine &Name = "") {
1739 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1740
1741 if (auto *PC = dyn_cast<Constant>(Ptr))
1742 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
1743
1744 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1745 }
1746
1747 Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1748 const Twine &Name = "") {
1749 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1750
1751 if (auto *PC = dyn_cast<Constant>(Ptr))
1752 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
1753
1754 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1755 }
1756
1757 Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
1758 const Twine &Name = "") {
1759 Value *Idxs[] = {
1760 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1761 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1762 };
1763
1764 if (auto *PC = dyn_cast<Constant>(Ptr))
1765 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
1766
1767 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1768 }
1769
1770 Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
1771 unsigned Idx1, const Twine &Name = "") {
1772 Value *Idxs[] = {
1773 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1774 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1775 };
1776
1777 if (auto *PC = dyn_cast<Constant>(Ptr))
1778 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
1779
1780 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1781 }
1782
1783 Value *CreateConstGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1784 const Twine &Name = "") {
1785 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1786
1787 if (auto *PC = dyn_cast<Constant>(Ptr))
1788 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
1789
1790 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1791 }
1792
1793 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
1794 return CreateConstGEP1_64(nullptr, Ptr, Idx0, Name);
1795 }
1796
1797 Value *CreateConstInBoundsGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1798 const Twine &Name = "") {
1799 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1800
1801 if (auto *PC = dyn_cast<Constant>(Ptr))
1802 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
1803
1804 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1805 }
1806
1807 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
1808 const Twine &Name = "") {
1809 return CreateConstInBoundsGEP1_64(nullptr, Ptr, Idx0, Name);
1810 }
1811
1812 Value *CreateConstGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1813 const Twine &Name = "") {
1814 Value *Idxs[] = {
1815 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1816 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1817 };
1818
1819 if (auto *PC = dyn_cast<Constant>(Ptr))
1820 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
1821
1822 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1823 }
1824
1825 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1826 const Twine &Name = "") {
1827 return CreateConstGEP2_64(nullptr, Ptr, Idx0, Idx1, Name);
1828 }
1829
1830 Value *CreateConstInBoundsGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1831 uint64_t Idx1, const Twine &Name = "") {
1832 Value *Idxs[] = {
1833 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1834 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1835 };
1836
1837 if (auto *PC = dyn_cast<Constant>(Ptr))
1838 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
1839
1840 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1841 }
1842
1843 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1844 const Twine &Name = "") {
1845 return CreateConstInBoundsGEP2_64(nullptr, Ptr, Idx0, Idx1, Name);
1846 }
1847
1848 Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
1849 const Twine &Name = "") {
1850 return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name);
1851 }
1852
1853 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
1854 return CreateConstInBoundsGEP2_32(nullptr, Ptr, 0, Idx, Name);
1855 }
1856
1857 /// Same as CreateGlobalString, but return a pointer with "i8*" type
1858 /// instead of a pointer to array of i8.
1859 Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
1860 unsigned AddressSpace = 0) {
1861 GlobalVariable *GV = CreateGlobalString(Str, Name, AddressSpace);
1862 Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1863 Constant *Indices[] = {Zero, Zero};
1864 return ConstantExpr::getInBoundsGetElementPtr(GV->getValueType(), GV,
1865 Indices);
1866 }
1867
1868 //===--------------------------------------------------------------------===//
1869 // Instruction creation methods: Cast/Conversion Operators
1870 //===--------------------------------------------------------------------===//
1871
1872 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1873 return CreateCast(Instruction::Trunc, V, DestTy, Name);
1874 }
1875
1876 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1877 return CreateCast(Instruction::ZExt, V, DestTy, Name);
1878 }
1879
1880 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1881 return CreateCast(Instruction::SExt, V, DestTy, Name);
1882 }
1883
1884 /// Create a ZExt or Trunc from the integer value V to DestTy. Return
1885 /// the value untouched if the type of V is already DestTy.
1886 Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
1887 const Twine &Name = "") {
1888 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 1890, __PRETTY_FUNCTION__))
1889 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 1890, __PRETTY_FUNCTION__))
1890 "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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 1890, __PRETTY_FUNCTION__))
;
1891 Type *VTy = V->getType();
1892 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1893 return CreateZExt(V, DestTy, Name);
1894 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1895 return CreateTrunc(V, DestTy, Name);
1896 return V;
1897 }
1898
1899 /// Create a SExt or Trunc from the integer value V to DestTy. Return
1900 /// the value untouched if the type of V is already DestTy.
1901 Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
1902 const Twine &Name = "") {
1903 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 1905, __PRETTY_FUNCTION__))
1904 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 1905, __PRETTY_FUNCTION__))
1905 "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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 1905, __PRETTY_FUNCTION__))
;
1906 Type *VTy = V->getType();
1907 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1908 return CreateSExt(V, DestTy, Name);
1909 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1910 return CreateTrunc(V, DestTy, Name);
1911 return V;
1912 }
1913
1914 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = "") {
1915 if (IsFPConstrained)
1916 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptoui,
1917 V, DestTy, nullptr, Name);
1918 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1919 }
1920
1921 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = "") {
1922 if (IsFPConstrained)
1923 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptosi,
1924 V, DestTy, nullptr, Name);
1925 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1926 }
1927
1928 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1929 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1930 }
1931
1932 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1933 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1934 }
1935
1936 Value *CreateFPTrunc(Value *V, Type *DestTy,
1937 const Twine &Name = "") {
1938 if (IsFPConstrained)
1939 return CreateConstrainedFPCast(
1940 Intrinsic::experimental_constrained_fptrunc, V, DestTy, nullptr,
1941 Name);
1942 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1943 }
1944
1945 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1946 if (IsFPConstrained)
1947 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fpext,
1948 V, DestTy, nullptr, Name);
1949 return CreateCast(Instruction::FPExt, V, DestTy, Name);
1950 }
1951
1952 Value *CreatePtrToInt(Value *V, Type *DestTy,
1953 const Twine &Name = "") {
1954 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1955 }
1956
1957 Value *CreateIntToPtr(Value *V, Type *DestTy,
1958 const Twine &Name = "") {
1959 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1960 }
1961
1962 Value *CreateBitCast(Value *V, Type *DestTy,
1963 const Twine &Name = "") {
1964 return CreateCast(Instruction::BitCast, V, DestTy, Name);
1965 }
1966
1967 Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
1968 const Twine &Name = "") {
1969 return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
1970 }
1971
1972 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1973 const Twine &Name = "") {
1974 if (V->getType() == DestTy)
1975 return V;
1976 if (auto *VC = dyn_cast<Constant>(V))
1977 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1978 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1979 }
1980
1981 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1982 const Twine &Name = "") {
1983 if (V->getType() == DestTy)
1984 return V;
1985 if (auto *VC = dyn_cast<Constant>(V))
1986 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1987 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1988 }
1989
1990 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1991 const Twine &Name = "") {
1992 if (V->getType() == DestTy)
1993 return V;
1994 if (auto *VC = dyn_cast<Constant>(V))
1995 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1996 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1997 }
1998
1999 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
2000 const Twine &Name = "") {
2001 if (V->getType() == DestTy)
2002 return V;
2003 if (auto *VC = dyn_cast<Constant>(V))
2004 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
2005 return Insert(CastInst::Create(Op, V, DestTy), Name);
2006 }
2007
2008 Value *CreatePointerCast(Value *V, Type *DestTy,
2009 const Twine &Name = "") {
2010 if (V->getType() == DestTy)
2011 return V;
2012 if (auto *VC = dyn_cast<Constant>(V))
2013 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
2014 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
2015 }
2016
2017 Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
2018 const Twine &Name = "") {
2019 if (V->getType() == DestTy)
2020 return V;
2021
2022 if (auto *VC = dyn_cast<Constant>(V)) {
2023 return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
2024 Name);
2025 }
2026
2027 return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
2028 Name);
2029 }
2030
2031 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
2032 const Twine &Name = "") {
2033 if (V->getType() == DestTy)
2034 return V;
2035 if (auto *VC = dyn_cast<Constant>(V))
2036 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
2037 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
2038 }
2039
2040 Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
2041 const Twine &Name = "") {
2042 if (V->getType() == DestTy)
2043 return V;
2044 if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy())
2045 return CreatePtrToInt(V, DestTy, Name);
2046 if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy())
2047 return CreateIntToPtr(V, DestTy, Name);
2048
2049 return CreateBitCast(V, DestTy, Name);
2050 }
2051
2052 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
2053 if (V->getType() == DestTy)
2054 return V;
2055 if (auto *VC = dyn_cast<Constant>(V))
2056 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
2057 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
2058 }
2059
2060 CallInst *CreateConstrainedFPCast(
2061 Intrinsic::ID ID, Value *V, Type *DestTy,
2062 Instruction *FMFSource = nullptr, const Twine &Name = "",
2063 MDNode *FPMathTag = nullptr,
2064 Optional<ConstrainedFPIntrinsic::RoundingMode> Rounding = None,
2065 Optional<ConstrainedFPIntrinsic::ExceptionBehavior> Except = None) {
2066 Value *ExceptV = getConstrainedFPExcept(Except);
2067
2068 FastMathFlags UseFMF = FMF;
2069 if (FMFSource)
2070 UseFMF = FMFSource->getFastMathFlags();
2071
2072 CallInst *C;
2073 switch (ID) {
2074 default: {
2075 Value *RoundingV = getConstrainedFPRounding(Rounding);
2076 C = CreateIntrinsic(ID, {DestTy, V->getType()}, {V, RoundingV, ExceptV},
2077 nullptr, Name);
2078 } break;
2079 case Intrinsic::experimental_constrained_fpext:
2080 case Intrinsic::experimental_constrained_fptoui:
2081 case Intrinsic::experimental_constrained_fptosi:
2082 C = CreateIntrinsic(ID, {DestTy, V->getType()}, {V, ExceptV}, nullptr,
2083 Name);
2084 break;
2085 }
2086 if (isa<FPMathOperator>(C))
2087 C = cast<CallInst>(setFPAttrs(C, FPMathTag, UseFMF));
2088 return C;
2089 }
2090
2091 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
2092 // compile time error, instead of converting the string to bool for the
2093 // isSigned parameter.
2094 Value *CreateIntCast(Value *, Type *, const char *) = delete;
2095
2096 //===--------------------------------------------------------------------===//
2097 // Instruction creation methods: Compare Instructions
2098 //===--------------------------------------------------------------------===//
2099
2100 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
2101 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
2102 }
2103
2104 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
2105 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
2106 }
2107
2108 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
2109 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
2110 }
2111
2112 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
2113 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
2114 }
2115
2116 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
2117 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
2118 }
2119
2120 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
2121 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
2122 }
2123
2124 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
2125 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
2126 }
2127
2128 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
2129 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
2130 }
2131
2132 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
2133 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
2134 }
2135
2136 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
2137 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
2138 }
2139
2140 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "",
2141 MDNode *FPMathTag = nullptr) {
2142 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag);
2143 }
2144
2145 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "",
2146 MDNode *FPMathTag = nullptr) {
2147 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag);
2148 }
2149
2150 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "",
2151 MDNode *FPMathTag = nullptr) {
2152 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag);
2153 }
2154
2155 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "",
2156 MDNode *FPMathTag = nullptr) {
2157 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag);
2158 }
2159
2160 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "",
2161 MDNode *FPMathTag = nullptr) {
2162 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag);
2163 }
2164
2165 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "",
2166 MDNode *FPMathTag = nullptr) {
2167 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag);
2168 }
2169
2170 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "",
2171 MDNode *FPMathTag = nullptr) {
2172 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag);
2173 }
2174
2175 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "",
2176 MDNode *FPMathTag = nullptr) {
2177 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag);
2178 }
2179
2180 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "",
2181 MDNode *FPMathTag = nullptr) {
2182 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag);
2183 }
2184
2185 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "",
2186 MDNode *FPMathTag = nullptr) {
2187 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag);
2188 }
2189
2190 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "",
2191 MDNode *FPMathTag = nullptr) {
2192 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag);
2193 }
2194
2195 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "",
2196 MDNode *FPMathTag = nullptr) {
2197 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag);
2198 }
2199
2200 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "",
2201 MDNode *FPMathTag = nullptr) {
2202 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag);
2203 }
2204
2205 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "",
2206 MDNode *FPMathTag = nullptr) {
2207 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag);
2208 }
2209
2210 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
2211 const Twine &Name = "") {
2212 if (auto *LC = dyn_cast<Constant>(LHS))
2213 if (auto *RC = dyn_cast<Constant>(RHS))
2214 return Insert(Folder.CreateICmp(P, LC, RC), Name);
2215 return Insert(new ICmpInst(P, LHS, RHS), Name);
2216 }
2217
2218 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
2219 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2220 if (auto *LC = dyn_cast<Constant>(LHS))
45
Assuming 'LC' is null
46
Taking false branch
2221 if (auto *RC = dyn_cast<Constant>(RHS))
2222 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
2223 return Insert(setFPAttrs(new FCmpInst(P, LHS, RHS), FPMathTag, FMF), Name);
47
Passing null pointer value via 2nd parameter 'LHS'
48
Calling constructor for 'FCmpInst'
2224 }
2225
2226 //===--------------------------------------------------------------------===//
2227 // Instruction creation methods: Other Instructions
2228 //===--------------------------------------------------------------------===//
2229
2230 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
2231 const Twine &Name = "") {
2232 PHINode *Phi = PHINode::Create(Ty, NumReservedValues);
2233 if (isa<FPMathOperator>(Phi))
2234 Phi = cast<PHINode>(setFPAttrs(Phi, nullptr /* MDNode* */, FMF));
2235 return Insert(Phi, Name);
2236 }
2237
2238 CallInst *CreateCall(FunctionType *FTy, Value *Callee,
2239 ArrayRef<Value *> Args = None, const Twine &Name = "",
2240 MDNode *FPMathTag = nullptr) {
2241 CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles);
2242 if (isa<FPMathOperator>(CI))
2243 CI = cast<CallInst>(setFPAttrs(CI, FPMathTag, FMF));
2244 return Insert(CI, Name);
2245 }
2246
2247 CallInst *CreateCall(FunctionType *FTy, Value *Callee, ArrayRef<Value *> Args,
2248 ArrayRef<OperandBundleDef> OpBundles,
2249 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2250 CallInst *CI = CallInst::Create(FTy, Callee, Args, OpBundles);
2251 if (isa<FPMathOperator>(CI))
2252 CI = cast<CallInst>(setFPAttrs(CI, FPMathTag, FMF));
2253 return Insert(CI, Name);
2254 }
2255
2256 CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args = None,
2257 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2258 return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, Name,
2259 FPMathTag);
2260 }
2261
2262 CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args,
2263 ArrayRef<OperandBundleDef> OpBundles,
2264 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2265 return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args,
2266 OpBundles, Name, FPMathTag);
2267 }
2268
2269 // Deprecated [opaque pointer types]
2270 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args = None,
2271 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2272 return CreateCall(
2273 cast<FunctionType>(Callee->getType()->getPointerElementType()), Callee,
2274 Args, Name, FPMathTag);
2275 }
2276
2277 // Deprecated [opaque pointer types]
2278 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
2279 ArrayRef<OperandBundleDef> OpBundles,
2280 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2281 return CreateCall(
2282 cast<FunctionType>(Callee->getType()->getPointerElementType()), Callee,
2283 Args, OpBundles, Name, FPMathTag);
2284 }
2285
2286 Value *CreateSelect(Value *C, Value *True, Value *False,
2287 const Twine &Name = "", Instruction *MDFrom = nullptr) {
2288 if (auto *CC = dyn_cast<Constant>(C))
2289 if (auto *TC = dyn_cast<Constant>(True))
2290 if (auto *FC = dyn_cast<Constant>(False))
2291 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
2292
2293 SelectInst *Sel = SelectInst::Create(C, True, False);
2294 if (MDFrom) {
2295 MDNode *Prof = MDFrom->getMetadata(LLVMContext::MD_prof);
2296 MDNode *Unpred = MDFrom->getMetadata(LLVMContext::MD_unpredictable);
2297 Sel = addBranchMetadata(Sel, Prof, Unpred);
2298 }
2299 if (isa<FPMathOperator>(Sel))
2300 Sel = cast<SelectInst>(setFPAttrs(Sel, nullptr /* MDNode* */, FMF));
2301 return Insert(Sel, Name);
2302 }
2303
2304 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
2305 return Insert(new VAArgInst(List, Ty), Name);
2306 }
2307
2308 Value *CreateExtractElement(Value *Vec, Value *Idx,
2309 const Twine &Name = "") {
2310 if (auto *VC = dyn_cast<Constant>(Vec))
2311 if (auto *IC = dyn_cast<Constant>(Idx))
2312 return Insert(Folder.CreateExtractElement(VC, IC), Name);
2313 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
2314 }
2315
2316 Value *CreateExtractElement(Value *Vec, uint64_t Idx,
2317 const Twine &Name = "") {
2318 return CreateExtractElement(Vec, getInt64(Idx), Name);
2319 }
2320
2321 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
2322 const Twine &Name = "") {
2323 if (auto *VC = dyn_cast<Constant>(Vec))
2324 if (auto *NC = dyn_cast<Constant>(NewElt))
2325 if (auto *IC = dyn_cast<Constant>(Idx))
2326 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
2327 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
2328 }
2329
2330 Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
2331 const Twine &Name = "") {
2332 return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name);
2333 }
2334
2335 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
2336 const Twine &Name = "") {
2337 if (auto *V1C = dyn_cast<Constant>(V1))
2338 if (auto *V2C = dyn_cast<Constant>(V2))
2339 if (auto *MC = dyn_cast<Constant>(Mask))
2340 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
2341 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
2342 }
2343
2344 Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<uint32_t> IntMask,
2345 const Twine &Name = "") {
2346 Value *Mask = ConstantDataVector::get(Context, IntMask);
2347 return CreateShuffleVector(V1, V2, Mask, Name);
2348 }
2349
2350 Value *CreateExtractValue(Value *Agg,
2351 ArrayRef<unsigned> Idxs,
2352 const Twine &Name = "") {
2353 if (auto *AggC = dyn_cast<Constant>(Agg))
2354 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
2355 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
2356 }
2357
2358 Value *CreateInsertValue(Value *Agg, Value *Val,
2359 ArrayRef<unsigned> Idxs,
2360 const Twine &Name = "") {
2361 if (auto *AggC = dyn_cast<Constant>(Agg))
2362 if (auto *ValC = dyn_cast<Constant>(Val))
2363 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
2364 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
2365 }
2366
2367 LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
2368 const Twine &Name = "") {
2369 return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
2370 }
2371
2372 //===--------------------------------------------------------------------===//
2373 // Utility creation methods
2374 //===--------------------------------------------------------------------===//
2375
2376 /// Return an i1 value testing if \p Arg is null.
2377 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
2378 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
2379 Name);
2380 }
2381
2382 /// Return an i1 value testing if \p Arg is not null.
2383 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
2384 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
2385 Name);
2386 }
2387
2388 /// Return the i64 difference between two pointer values, dividing out
2389 /// the size of the pointed-to objects.
2390 ///
2391 /// This is intended to implement C-style pointer subtraction. As such, the
2392 /// pointers must be appropriately aligned for their element types and
2393 /// pointing into the same object.
2394 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
2395 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2396, __PRETTY_FUNCTION__))
2396 "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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2396, __PRETTY_FUNCTION__))
;
2397 auto *ArgType = cast<PointerType>(LHS->getType());
2398 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
2399 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
2400 Value *Difference = CreateSub(LHS_int, RHS_int);
2401 return CreateExactSDiv(Difference,
2402 ConstantExpr::getSizeOf(ArgType->getElementType()),
2403 Name);
2404 }
2405
2406 /// Create a launder.invariant.group intrinsic call. If Ptr type is
2407 /// different from pointer to i8, it's casted to pointer to i8 in the same
2408 /// address space before call and casted back to Ptr type after call.
2409 Value *CreateLaunderInvariantGroup(Value *Ptr) {
2410 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2411, __PRETTY_FUNCTION__))
2411 "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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2411, __PRETTY_FUNCTION__))
;
2412 // FIXME: we could potentially avoid casts to/from i8*.
2413 auto *PtrType = Ptr->getType();
2414 auto *Int8PtrTy = getInt8PtrTy(PtrType->getPointerAddressSpace());
2415 if (PtrType != Int8PtrTy)
2416 Ptr = CreateBitCast(Ptr, Int8PtrTy);
2417 Module *M = BB->getParent()->getParent();
2418 Function *FnLaunderInvariantGroup = Intrinsic::getDeclaration(
2419 M, Intrinsic::launder_invariant_group, {Int8PtrTy});
2420
2421 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2424, __PRETTY_FUNCTION__))
2422 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2424, __PRETTY_FUNCTION__))
2423 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2424, __PRETTY_FUNCTION__))
2424 "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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2424, __PRETTY_FUNCTION__))
;
2425
2426 CallInst *Fn = CreateCall(FnLaunderInvariantGroup, {Ptr});
2427
2428 if (PtrType != Int8PtrTy)
2429 return CreateBitCast(Fn, PtrType);
2430 return Fn;
2431 }
2432
2433 /// \brief Create a strip.invariant.group intrinsic call. If Ptr type is
2434 /// different from pointer to i8, it's casted to pointer to i8 in the same
2435 /// address space before call and casted back to Ptr type after call.
2436 Value *CreateStripInvariantGroup(Value *Ptr) {
2437 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2438, __PRETTY_FUNCTION__))
2438 "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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2438, __PRETTY_FUNCTION__))
;
2439
2440 // FIXME: we could potentially avoid casts to/from i8*.
2441 auto *PtrType = Ptr->getType();
2442 auto *Int8PtrTy = getInt8PtrTy(PtrType->getPointerAddressSpace());
2443 if (PtrType != Int8PtrTy)
2444 Ptr = CreateBitCast(Ptr, Int8PtrTy);
2445 Module *M = BB->getParent()->getParent();
2446 Function *FnStripInvariantGroup = Intrinsic::getDeclaration(
2447 M, Intrinsic::strip_invariant_group, {Int8PtrTy});
2448
2449 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2452, __PRETTY_FUNCTION__))
2450 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2452, __PRETTY_FUNCTION__))
2451 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2452, __PRETTY_FUNCTION__))
2452 "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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2452, __PRETTY_FUNCTION__))
;
2453
2454 CallInst *Fn = CreateCall(FnStripInvariantGroup, {Ptr});
2455
2456 if (PtrType != Int8PtrTy)
2457 return CreateBitCast(Fn, PtrType);
2458 return Fn;
2459 }
2460
2461 /// Return a vector value that contains \arg V broadcasted to \p
2462 /// NumElts elements.
2463 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
2464 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2464, __PRETTY_FUNCTION__))
;
2465
2466 // First insert it into an undef vector so we can shuffle it.
2467 Type *I32Ty = getInt32Ty();
2468 Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
2469 V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
2470 Name + ".splatinsert");
2471
2472 // Shuffle the value across the desired number of elements.
2473 Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
2474 return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
2475 }
2476
2477 /// Return a value that has been extracted from a larger integer type.
2478 Value *CreateExtractInteger(const DataLayout &DL, Value *From,
2479 IntegerType *ExtractedTy, uint64_t Offset,
2480 const Twine &Name) {
2481 auto *IntTy = cast<IntegerType>(From->getType());
2482 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2484, __PRETTY_FUNCTION__))
2483 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2484, __PRETTY_FUNCTION__))
2484 "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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2484, __PRETTY_FUNCTION__))
;
2485 uint64_t ShAmt = 8 * Offset;
2486 Value *V = From;
2487 if (DL.isBigEndian())
2488 ShAmt = 8 * (DL.getTypeStoreSize(IntTy) -
2489 DL.getTypeStoreSize(ExtractedTy) - Offset);
2490 if (ShAmt) {
2491 V = CreateLShr(V, ShAmt, Name + ".shift");
2492 }
2493 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2494, __PRETTY_FUNCTION__))
2494 "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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2494, __PRETTY_FUNCTION__))
;
2495 if (ExtractedTy != IntTy) {
2496 V = CreateTrunc(V, ExtractedTy, Name + ".trunc");
2497 }
2498 return V;
2499 }
2500
2501 Value *CreatePreserveArrayAccessIndex(Value *Base, unsigned Dimension,
2502 unsigned LastIndex, MDNode *DbgInfo) {
2503 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2504, __PRETTY_FUNCTION__))
2504 "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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2504, __PRETTY_FUNCTION__))
;
2505 auto *BaseType = Base->getType();
2506
2507 Value *LastIndexV = getInt32(LastIndex);
2508 Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
2509 SmallVector<Value *, 4> IdxList;
2510 for (unsigned I = 0; I < Dimension; ++I)
2511 IdxList.push_back(Zero);
2512 IdxList.push_back(LastIndexV);
2513
2514 Type *ResultType =
2515 GetElementPtrInst::getGEPReturnType(Base, IdxList);
2516
2517 Module *M = BB->getParent()->getParent();
2518 Function *FnPreserveArrayAccessIndex = Intrinsic::getDeclaration(
2519 M, Intrinsic::preserve_array_access_index, {ResultType, BaseType});
2520
2521 Value *DimV = getInt32(Dimension);
2522 CallInst *Fn =
2523 CreateCall(FnPreserveArrayAccessIndex, {Base, DimV, LastIndexV});
2524 if (DbgInfo)
2525 Fn->setMetadata(LLVMContext::MD_preserve_access_index, DbgInfo);
2526
2527 return Fn;
2528 }
2529
2530 Value *CreatePreserveUnionAccessIndex(Value *Base, unsigned FieldIndex,
2531 MDNode *DbgInfo) {
2532 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2533, __PRETTY_FUNCTION__))
2533 "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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2533, __PRETTY_FUNCTION__))
;
2534 auto *BaseType = Base->getType();
2535
2536 Module *M = BB->getParent()->getParent();
2537 Function *FnPreserveUnionAccessIndex = Intrinsic::getDeclaration(
2538 M, Intrinsic::preserve_union_access_index, {BaseType, BaseType});
2539
2540 Value *DIIndex = getInt32(FieldIndex);
2541 CallInst *Fn =
2542 CreateCall(FnPreserveUnionAccessIndex, {Base, DIIndex});
2543 if (DbgInfo)
2544 Fn->setMetadata(LLVMContext::MD_preserve_access_index, DbgInfo);
2545
2546 return Fn;
2547 }
2548
2549 Value *CreatePreserveStructAccessIndex(Value *Base, unsigned Index,
2550 unsigned FieldIndex, MDNode *DbgInfo) {
2551 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2552, __PRETTY_FUNCTION__))
2552 "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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2552, __PRETTY_FUNCTION__))
;
2553 auto *BaseType = Base->getType();
2554
2555 Value *GEPIndex = getInt32(Index);
2556 Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
2557 Type *ResultType =
2558 GetElementPtrInst::getGEPReturnType(Base, {Zero, GEPIndex});
2559
2560 Module *M = BB->getParent()->getParent();
2561 Function *FnPreserveStructAccessIndex = Intrinsic::getDeclaration(
2562 M, Intrinsic::preserve_struct_access_index, {ResultType, BaseType});
2563
2564 Value *DIIndex = getInt32(FieldIndex);
2565 CallInst *Fn = CreateCall(FnPreserveStructAccessIndex,
2566 {Base, GEPIndex, DIIndex});
2567 if (DbgInfo)
2568 Fn->setMetadata(LLVMContext::MD_preserve_access_index, DbgInfo);
2569
2570 return Fn;
2571 }
2572
2573private:
2574 /// Helper function that creates an assume intrinsic call that
2575 /// represents an alignment assumption on the provided Ptr, Mask, Type
2576 /// and Offset. It may be sometimes useful to do some other logic
2577 /// based on this alignment check, thus it can be stored into 'TheCheck'.
2578 CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL,
2579 Value *PtrValue, Value *Mask,
2580 Type *IntPtrTy, Value *OffsetValue,
2581 Value **TheCheck) {
2582 Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint");
2583
2584 if (OffsetValue) {
2585 bool IsOffsetZero = false;
2586 if (const auto *CI = dyn_cast<ConstantInt>(OffsetValue))
2587 IsOffsetZero = CI->isZero();
2588
2589 if (!IsOffsetZero) {
2590 if (OffsetValue->getType() != IntPtrTy)
2591 OffsetValue = CreateIntCast(OffsetValue, IntPtrTy, /*isSigned*/ true,
2592 "offsetcast");
2593 PtrIntValue = CreateSub(PtrIntValue, OffsetValue, "offsetptr");
2594 }
2595 }
2596
2597 Value *Zero = ConstantInt::get(IntPtrTy, 0);
2598 Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr");
2599 Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond");
2600 if (TheCheck)
2601 *TheCheck = InvCond;
2602
2603 return CreateAssumption(InvCond);
2604 }
2605
2606public:
2607 /// Create an assume intrinsic call that represents an alignment
2608 /// assumption on the provided pointer.
2609 ///
2610 /// An optional offset can be provided, and if it is provided, the offset
2611 /// must be subtracted from the provided pointer to get the pointer with the
2612 /// specified alignment.
2613 ///
2614 /// It may be sometimes useful to do some other logic
2615 /// based on this alignment check, thus it can be stored into 'TheCheck'.
2616 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2617 unsigned Alignment,
2618 Value *OffsetValue = nullptr,
2619 Value **TheCheck = nullptr) {
2620 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2621, __PRETTY_FUNCTION__))
2621 "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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2621, __PRETTY_FUNCTION__))
;
2622 assert(Alignment != 0 && "Invalid Alignment")((Alignment != 0 && "Invalid Alignment") ? static_cast
<void> (0) : __assert_fail ("Alignment != 0 && \"Invalid Alignment\""
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2622, __PRETTY_FUNCTION__))
;
2623 auto *PtrTy = cast<PointerType>(PtrValue->getType());
2624 Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
2625
2626 Value *Mask = ConstantInt::get(IntPtrTy, Alignment - 1);
2627 return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
2628 OffsetValue, TheCheck);
2629 }
2630
2631 /// Create an assume intrinsic call that represents an alignment
2632 /// assumption on the provided pointer.
2633 ///
2634 /// An optional offset can be provided, and if it is provided, the offset
2635 /// must be subtracted from the provided pointer to get the pointer with the
2636 /// specified alignment.
2637 ///
2638 /// It may be sometimes useful to do some other logic
2639 /// based on this alignment check, thus it can be stored into 'TheCheck'.
2640 ///
2641 /// This overload handles the condition where the Alignment is dependent
2642 /// on an existing value rather than a static value.
2643 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2644 Value *Alignment,
2645 Value *OffsetValue = nullptr,
2646 Value **TheCheck = nullptr) {
2647 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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2648, __PRETTY_FUNCTION__))
2648 "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-10~svn374877/include/llvm/IR/IRBuilder.h"
, 2648, __PRETTY_FUNCTION__))
;
2649 auto *PtrTy = cast<PointerType>(PtrValue->getType());
2650 Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
2651
2652 if (Alignment->getType() != IntPtrTy)
2653 Alignment = CreateIntCast(Alignment, IntPtrTy, /*isSigned*/ false,
2654 "alignmentcast");
2655
2656 Value *Mask = CreateSub(Alignment, ConstantInt::get(IntPtrTy, 1), "mask");
2657
2658 return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
2659 OffsetValue, TheCheck);
2660 }
2661};
2662
2663// Create wrappers for C Binding types (see CBindingWrapping.h).
2664DEFINE_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)); }
2665
2666} // end namespace llvm
2667
2668#endif // LLVM_IR_IRBUILDER_H

/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.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
47namespace llvm {
48
49class APInt;
50class ConstantInt;
51class DataLayout;
52class LLVMContext;
53
54//===----------------------------------------------------------------------===//
55// AllocaInst Class
56//===----------------------------------------------------------------------===//
57
58/// an instruction to allocate memory on the stack
59class AllocaInst : public UnaryInstruction {
60 Type *AllocatedType;
61
62protected:
63 // Note: Instruction needs to be a friend here to call cloneImpl.
64 friend class Instruction;
65
66 AllocaInst *cloneImpl() const;
67
68public:
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, unsigned Align,
82 const Twine &Name = "", Instruction *InsertBefore = nullptr);
83 AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, unsigned 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 unsigned getAlignment() const {
113 if (const auto MA = decodeMaybeAlign(getSubclassDataFromInstruction() & 31))
114 return MA->value();
115 return 0;
116 }
117 void setAlignment(MaybeAlign Align);
118
119 /// Return true if this alloca is in the entry block of the function and is a
120 /// constant size. If so, the code generator will fold it into the
121 /// prolog/epilog code, so it is basically free.
122 bool isStaticAlloca() const;
123
124 /// Return true if this alloca is used as an inalloca argument to a call. Such
125 /// allocas are never considered static even if they are in the entry block.
126 bool isUsedWithInAlloca() const {
127 return getSubclassDataFromInstruction() & 32;
128 }
129
130 /// Specify whether this alloca is used to represent the arguments to a call.
131 void setUsedWithInAlloca(bool V) {
132 setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) |
133 (V ? 32 : 0));
134 }
135
136 /// Return true if this alloca is used as a swifterror argument to a call.
137 bool isSwiftError() const {
138 return getSubclassDataFromInstruction() & 64;
139 }
140
141 /// Specify whether this alloca is used to represent a swifterror.
142 void setSwiftError(bool V) {
143 setInstructionSubclassData((getSubclassDataFromInstruction() & ~64) |
144 (V ? 64 : 0));
145 }
146
147 // Methods for support type inquiry through isa, cast, and dyn_cast:
148 static bool classof(const Instruction *I) {
149 return (I->getOpcode() == Instruction::Alloca);
150 }
151 static bool classof(const Value *V) {
152 return isa<Instruction>(V) && classof(cast<Instruction>(V));
153 }
154
155private:
156 // Shadow Instruction::setInstructionSubclassData with a private forwarding
157 // method so that subclasses cannot accidentally use it.
158 void setInstructionSubclassData(unsigned short D) {
159 Instruction::setInstructionSubclassData(D);
160 }
161};
162
163//===----------------------------------------------------------------------===//
164// LoadInst Class
165//===----------------------------------------------------------------------===//
166
167/// An instruction for reading from memory. This uses the SubclassData field in
168/// Value to store whether or not the load is volatile.
169class LoadInst : public UnaryInstruction {
170 void AssertOK();
171
172protected:
173 // Note: Instruction needs to be a friend here to call cloneImpl.
174 friend class Instruction;
175
176 LoadInst *cloneImpl() const;
177
178public:
179 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr = "",
180 Instruction *InsertBefore = nullptr);
181 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
182 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
183 Instruction *InsertBefore = nullptr);
184 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
185 BasicBlock *InsertAtEnd);
186 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
187 unsigned Align, Instruction *InsertBefore = nullptr);
188 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
189 unsigned Align, BasicBlock *InsertAtEnd);
190 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
191 unsigned Align, AtomicOrdering Order,
192 SyncScope::ID SSID = SyncScope::System,
193 Instruction *InsertBefore = nullptr);
194 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
195 unsigned Align, AtomicOrdering Order, SyncScope::ID SSID,
196 BasicBlock *InsertAtEnd);
197
198 // Deprecated [opaque pointer types]
199 explicit LoadInst(Value *Ptr, const Twine &NameStr = "",
200 Instruction *InsertBefore = nullptr)
201 : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr,
202 InsertBefore) {}
203 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd)
204 : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr,
205 InsertAtEnd) {}
206 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
207 Instruction *InsertBefore = nullptr)
208 : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr,
209 isVolatile, InsertBefore) {}
210 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
211 BasicBlock *InsertAtEnd)
212 : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr,
213 isVolatile, InsertAtEnd) {}
214 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align,
215 Instruction *InsertBefore = nullptr)
216 : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr,
217 isVolatile, Align, InsertBefore) {}
218 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align,
219 BasicBlock *InsertAtEnd)
220 : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr,
221 isVolatile, Align, InsertAtEnd) {}
222 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align,
223 AtomicOrdering Order, SyncScope::ID SSID = SyncScope::System,
224 Instruction *InsertBefore = nullptr)
225 : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr,
226 isVolatile, Align, Order, SSID, InsertBefore) {}
227 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align,
228 AtomicOrdering Order, SyncScope::ID SSID, BasicBlock *InsertAtEnd)
229 : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr,
230 isVolatile, Align, Order, SSID, InsertAtEnd) {}
231
232 /// Return true if this is a load from a volatile memory location.
233 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
234
235 /// Specify whether this is a volatile load or not.
236 void setVolatile(bool V) {
237 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
238 (V ? 1 : 0));
239 }
240
241 /// Return the alignment of the access that is being performed.
242 unsigned getAlignment() const {
243 if (const auto MA =
244 decodeMaybeAlign((getSubclassDataFromInstruction() >> 1) & 31))
245 return MA->value();
246 return 0;
247 }
248
249 void setAlignment(MaybeAlign Align);
250
251 /// Returns the ordering constraint of this load instruction.
252 AtomicOrdering getOrdering() const {
253 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
254 }
255
256 /// Sets the ordering constraint of this load instruction. May not be Release
257 /// or AcquireRelease.
258 void setOrdering(AtomicOrdering Ordering) {
259 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
260 ((unsigned)Ordering << 7));
261 }
262
263 /// Returns the synchronization scope ID of this load instruction.
264 SyncScope::ID getSyncScopeID() const {
265 return SSID;
266 }
267
268 /// Sets the synchronization scope ID of this load instruction.
269 void setSyncScopeID(SyncScope::ID SSID) {
270 this->SSID = SSID;
271 }
272
273 /// Sets the ordering constraint and the synchronization scope ID of this load
274 /// instruction.
275 void setAtomic(AtomicOrdering Ordering,
276 SyncScope::ID SSID = SyncScope::System) {
277 setOrdering(Ordering);
278 setSyncScopeID(SSID);
279 }
280
281 bool isSimple() const { return !isAtomic() && !isVolatile(); }
282
283 bool isUnordered() const {
284 return (getOrdering() == AtomicOrdering::NotAtomic ||
285 getOrdering() == AtomicOrdering::Unordered) &&
286 !isVolatile();
287 }
288
289 Value *getPointerOperand() { return getOperand(0); }
290 const Value *getPointerOperand() const { return getOperand(0); }
291 static unsigned getPointerOperandIndex() { return 0U; }
292 Type *getPointerOperandType() const { return getPointerOperand()->getType(); }
293
294 /// Returns the address space of the pointer operand.
295 unsigned getPointerAddressSpace() const {
296 return getPointerOperandType()->getPointerAddressSpace();
297 }
298
299 // Methods for support type inquiry through isa, cast, and dyn_cast:
300 static bool classof(const Instruction *I) {
301 return I->getOpcode() == Instruction::Load;
302 }
303 static bool classof(const Value *V) {
304 return isa<Instruction>(V) && classof(cast<Instruction>(V));
305 }
306
307private:
308 // Shadow Instruction::setInstructionSubclassData with a private forwarding
309 // method so that subclasses cannot accidentally use it.
310 void setInstructionSubclassData(unsigned short D) {
311 Instruction::setInstructionSubclassData(D);
312 }
313
314 /// The synchronization scope ID of this load instruction. Not quite enough
315 /// room in SubClassData for everything, so synchronization scope ID gets its
316 /// own field.
317 SyncScope::ID SSID;
318};
319
320//===----------------------------------------------------------------------===//
321// StoreInst Class
322//===----------------------------------------------------------------------===//
323
324/// An instruction for storing to memory.
325class StoreInst : public Instruction {
326 void AssertOK();
327
328protected:
329 // Note: Instruction needs to be a friend here to call cloneImpl.
330 friend class Instruction;
331
332 StoreInst *cloneImpl() const;
333
334public:
335 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
336 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
337 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
338 Instruction *InsertBefore = nullptr);
339 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
340 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
341 unsigned Align, Instruction *InsertBefore = nullptr);
342 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
343 unsigned Align, BasicBlock *InsertAtEnd);
344 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
345 unsigned Align, AtomicOrdering Order,
346 SyncScope::ID SSID = SyncScope::System,
347 Instruction *InsertBefore = nullptr);
348 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
349 unsigned Align, AtomicOrdering Order, SyncScope::ID SSID,
350 BasicBlock *InsertAtEnd);
351
352 // allocate space for exactly two operands
353 void *operator new(size_t s) {
354 return User::operator new(s, 2);
355 }
356
357 /// Return true if this is a store to a volatile memory location.
358 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
359
360 /// Specify whether this is a volatile store or not.
361 void setVolatile(bool V) {
362 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
363 (V ? 1 : 0));
364 }
365
366 /// Transparently provide more efficient getOperand methods.
367 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
;
368
369 /// Return the alignment of the access that is being performed
370 unsigned getAlignment() const {
371 if (const auto MA =
372 decodeMaybeAlign((getSubclassDataFromInstruction() >> 1) & 31))
373 return MA->value();
374 return 0;
375 }
376
377 void setAlignment(MaybeAlign Align);
378
379 /// Returns the ordering constraint of this store instruction.
380 AtomicOrdering getOrdering() const {
381 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
382 }
383
384 /// Sets the ordering constraint of this store instruction. May not be
385 /// Acquire or AcquireRelease.
386 void setOrdering(AtomicOrdering Ordering) {
387 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
388 ((unsigned)Ordering << 7));
389 }
390
391 /// Returns the synchronization scope ID of this store instruction.
392 SyncScope::ID getSyncScopeID() const {
393 return SSID;
394 }
395
396 /// Sets the synchronization scope ID of this store instruction.
397 void setSyncScopeID(SyncScope::ID SSID) {
398 this->SSID = SSID;
399 }
400
401 /// Sets the ordering constraint and the synchronization scope ID of this
402 /// store instruction.
403 void setAtomic(AtomicOrdering Ordering,
404 SyncScope::ID SSID = SyncScope::System) {
405 setOrdering(Ordering);
406 setSyncScopeID(SSID);
407 }
408
409 bool isSimple() const { return !isAtomic() && !isVolatile(); }
410
411 bool isUnordered() const {
412 return (getOrdering() == AtomicOrdering::NotAtomic ||
413 getOrdering() == AtomicOrdering::Unordered) &&
414 !isVolatile();
415 }
416
417 Value *getValueOperand() { return getOperand(0); }
418 const Value *getValueOperand() const { return getOperand(0); }
419
420 Value *getPointerOperand() { return getOperand(1); }
421 const Value *getPointerOperand() const { return getOperand(1); }
422 static unsigned getPointerOperandIndex() { return 1U; }
423 Type *getPointerOperandType() const { return getPointerOperand()->getType(); }
424
425 /// Returns the address space of the pointer operand.
426 unsigned getPointerAddressSpace() const {
427 return getPointerOperandType()->getPointerAddressSpace();
428 }
429
430 // Methods for support type inquiry through isa, cast, and dyn_cast:
431 static bool classof(const Instruction *I) {
432 return I->getOpcode() == Instruction::Store;
433 }
434 static bool classof(const Value *V) {
435 return isa<Instruction>(V) && classof(cast<Instruction>(V));
436 }
437
438private:
439 // Shadow Instruction::setInstructionSubclassData with a private forwarding
440 // method so that subclasses cannot accidentally use it.
441 void setInstructionSubclassData(unsigned short D) {
442 Instruction::setInstructionSubclassData(D);
443 }
444
445 /// The synchronization scope ID of this store instruction. Not quite enough
446 /// room in SubClassData for everything, so synchronization scope ID gets its
447 /// own field.
448 SyncScope::ID SSID;
449};
450
451template <>
452struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
453};
454
455DEFINE_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-10~svn374877/include/llvm/IR/Instructions.h"
, 455, __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-10~svn374877/include/llvm/IR/Instructions.h"
, 455, __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
); }
456
457//===----------------------------------------------------------------------===//
458// FenceInst Class
459//===----------------------------------------------------------------------===//
460
461/// An instruction for ordering other memory operations.
462class FenceInst : public Instruction {
463 void Init(AtomicOrdering Ordering, SyncScope::ID SSID);
464
465protected:
466 // Note: Instruction needs to be a friend here to call cloneImpl.
467 friend class Instruction;
468
469 FenceInst *cloneImpl() const;
470
471public:
472 // Ordering may only be Acquire, Release, AcquireRelease, or
473 // SequentiallyConsistent.
474 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
475 SyncScope::ID SSID = SyncScope::System,
476 Instruction *InsertBefore = nullptr);
477 FenceInst(LLVMContext &C, AtomicOrdering Ordering, SyncScope::ID SSID,
478 BasicBlock *InsertAtEnd);
479
480 // allocate space for exactly zero operands
481 void *operator new(size_t s) {
482 return User::operator new(s, 0);
483 }
484
485 /// Returns the ordering constraint of this fence instruction.
486 AtomicOrdering getOrdering() const {
487 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
488 }
489
490 /// Sets the ordering constraint of this fence instruction. May only be
491 /// Acquire, Release, AcquireRelease, or SequentiallyConsistent.
492 void setOrdering(AtomicOrdering Ordering) {
493 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
494 ((unsigned)Ordering << 1));
495 }
496
497 /// Returns the synchronization scope ID of this fence instruction.
498 SyncScope::ID getSyncScopeID() const {
499 return SSID;
500 }
501
502 /// Sets the synchronization scope ID of this fence instruction.
503 void setSyncScopeID(SyncScope::ID SSID) {
504 this->SSID = SSID;
505 }
506
507 // Methods for support type inquiry through isa, cast, and dyn_cast:
508 static bool classof(const Instruction *I) {
509 return I->getOpcode() == Instruction::Fence;
510 }
511 static bool classof(const Value *V) {
512 return isa<Instruction>(V) && classof(cast<Instruction>(V));
513 }
514
515private:
516 // Shadow Instruction::setInstructionSubclassData with a private forwarding
517 // method so that subclasses cannot accidentally use it.
518 void setInstructionSubclassData(unsigned short D) {
519 Instruction::setInstructionSubclassData(D);
520 }
521
522 /// The synchronization scope ID of this fence instruction. Not quite enough
523 /// room in SubClassData for everything, so synchronization scope ID gets its
524 /// own field.
525 SyncScope::ID SSID;
526};
527
528//===----------------------------------------------------------------------===//
529// AtomicCmpXchgInst Class
530//===----------------------------------------------------------------------===//
531
532/// An instruction that atomically checks whether a
533/// specified value is in a memory location, and, if it is, stores a new value
534/// there. The value returned by this instruction is a pair containing the
535/// original value as first element, and an i1 indicating success (true) or
536/// failure (false) as second element.
537///
538class AtomicCmpXchgInst : public Instruction {
539 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
540 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
541 SyncScope::ID SSID);
542
543protected:
544 // Note: Instruction needs to be a friend here to call cloneImpl.
545 friend class Instruction;
546
547 AtomicCmpXchgInst *cloneImpl() const;
548
549public:
550 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
551 AtomicOrdering SuccessOrdering,
552 AtomicOrdering FailureOrdering,
553 SyncScope::ID SSID, Instruction *InsertBefore = nullptr);
554 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
555 AtomicOrdering SuccessOrdering,
556 AtomicOrdering FailureOrdering,
557 SyncScope::ID SSID, BasicBlock *InsertAtEnd);
558
559 // allocate space for exactly three operands
560 void *operator new(size_t s) {
561 return User::operator new(s, 3);
562 }
563
564 /// Return true if this is a cmpxchg from a volatile memory
565 /// location.
566 ///
567 bool isVolatile() const {
568 return getSubclassDataFromInstruction() & 1;
569 }
570
571 /// Specify whether this is a volatile cmpxchg.
572 ///
573 void setVolatile(bool V) {
574 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
575 (unsigned)V);
576 }
577
578 /// Return true if this cmpxchg may spuriously fail.
579 bool isWeak() const {
580 return getSubclassDataFromInstruction() & 0x100;
581 }
582
583 void setWeak(bool IsWeak) {
584 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x100) |
585 (IsWeak << 8));
586 }
587
588 /// Transparently provide more efficient getOperand methods.
589 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
;
590
591 /// Returns the success ordering constraint of this cmpxchg instruction.
592 AtomicOrdering getSuccessOrdering() const {
593 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
594 }
595
596 /// Sets the success ordering constraint of this cmpxchg instruction.
597 void setSuccessOrdering(AtomicOrdering Ordering) {
598 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-10~svn374877/include/llvm/IR/Instructions.h"
, 599, __PRETTY_FUNCTION__))
599 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 599, __PRETTY_FUNCTION__))
;
600 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) |
601 ((unsigned)Ordering << 2));
602 }
603
604 /// Returns the failure ordering constraint of this cmpxchg instruction.
605 AtomicOrdering getFailureOrdering() const {
606 return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7);
607 }
608
609 /// Sets the failure ordering constraint of this cmpxchg instruction.
610 void setFailureOrdering(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-10~svn374877/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-10~svn374877/include/llvm/IR/Instructions.h"
, 612, __PRETTY_FUNCTION__))
;
613 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) |
614 ((unsigned)Ordering << 5));
615 }
616
617 /// Returns the synchronization scope ID of this cmpxchg instruction.
618 SyncScope::ID getSyncScopeID() const {
619 return SSID;
620 }
621
622 /// Sets the synchronization scope ID of this cmpxchg instruction.
623 void setSyncScopeID(SyncScope::ID SSID) {
624 this->SSID = SSID;
625 }
626
627 Value *getPointerOperand() { return getOperand(0); }
628 const Value *getPointerOperand() const { return getOperand(0); }
629 static unsigned getPointerOperandIndex() { return 0U; }
630
631 Value *getCompareOperand() { return getOperand(1); }
632 const Value *getCompareOperand() const { return getOperand(1); }
633
634 Value *getNewValOperand() { return getOperand(2); }
635 const Value *getNewValOperand() const { return getOperand(2); }
636
637 /// Returns the address space of the pointer operand.
638 unsigned getPointerAddressSpace() const {
639 return getPointerOperand()->getType()->getPointerAddressSpace();
640 }
641
642 /// Returns the strongest permitted ordering on failure, given the
643 /// desired ordering on success.
644 ///
645 /// If the comparison in a cmpxchg operation fails, there is no atomic store
646 /// so release semantics cannot be provided. So this function drops explicit
647 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
648 /// operation would remain SequentiallyConsistent.
649 static AtomicOrdering
650 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) {
651 switch (SuccessOrdering) {
652 default:
653 llvm_unreachable("invalid cmpxchg success ordering")::llvm::llvm_unreachable_internal("invalid cmpxchg success ordering"
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 653)
;
654 case AtomicOrdering::Release:
655 case AtomicOrdering::Monotonic:
656 return AtomicOrdering::Monotonic;
657 case AtomicOrdering::AcquireRelease:
658 case AtomicOrdering::Acquire:
659 return AtomicOrdering::Acquire;
660 case AtomicOrdering::SequentiallyConsistent:
661 return AtomicOrdering::SequentiallyConsistent;
662 }
663 }
664
665 // Methods for support type inquiry through isa, cast, and dyn_cast:
666 static bool classof(const Instruction *I) {
667 return I->getOpcode() == Instruction::AtomicCmpXchg;
668 }
669 static bool classof(const Value *V) {
670 return isa<Instruction>(V) && classof(cast<Instruction>(V));
671 }
672
673private:
674 // Shadow Instruction::setInstructionSubclassData with a private forwarding
675 // method so that subclasses cannot accidentally use it.
676 void setInstructionSubclassData(unsigned short D) {
677 Instruction::setInstructionSubclassData(D);
678 }
679
680 /// The synchronization scope ID of this cmpxchg instruction. Not quite
681 /// enough room in SubClassData for everything, so synchronization scope ID
682 /// gets its own field.
683 SyncScope::ID SSID;
684};
685
686template <>
687struct OperandTraits<AtomicCmpXchgInst> :
688 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
689};
690
691DEFINE_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-10~svn374877/include/llvm/IR/Instructions.h"
, 691, __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-10~svn374877/include/llvm/IR/Instructions.h"
, 691, __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); }
692
693//===----------------------------------------------------------------------===//
694// AtomicRMWInst Class
695//===----------------------------------------------------------------------===//
696
697/// an instruction that atomically reads a memory location,
698/// combines it with another value, and then stores the result back. Returns
699/// the old value.
700///
701class AtomicRMWInst : public Instruction {
702protected:
703 // Note: Instruction needs to be a friend here to call cloneImpl.
704 friend class Instruction;
705
706 AtomicRMWInst *cloneImpl() const;
707
708public:
709 /// This enumeration lists the possible modifications atomicrmw can make. In
710 /// the descriptions, 'p' is the pointer to the instruction's memory location,
711 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
712 /// instruction. These instructions always return 'old'.
713 enum BinOp {
714 /// *p = v
715 Xchg,
716 /// *p = old + v
717 Add,
718 /// *p = old - v
719 Sub,
720 /// *p = old & v
721 And,
722 /// *p = ~(old & v)
723 Nand,
724 /// *p = old | v
725 Or,
726 /// *p = old ^ v
727 Xor,
728 /// *p = old >signed v ? old : v
729 Max,
730 /// *p = old <signed v ? old : v
731 Min,
732 /// *p = old >unsigned v ? old : v
733 UMax,
734 /// *p = old <unsigned v ? old : v
735 UMin,
736
737 /// *p = old + v
738 FAdd,
739
740 /// *p = old - v
741 FSub,
742
743 FIRST_BINOP = Xchg,
744 LAST_BINOP = FSub,
745 BAD_BINOP
746 };
747
748 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
749 AtomicOrdering Ordering, SyncScope::ID SSID,
750 Instruction *InsertBefore = nullptr);
751 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
752 AtomicOrdering Ordering, SyncScope::ID SSID,
753 BasicBlock *InsertAtEnd);
754
755 // allocate space for exactly two operands
756 void *operator new(size_t s) {
757 return User::operator new(s, 2);
758 }
759
760 BinOp getOperation() const {
761 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
762 }
763
764 static StringRef getOperationName(BinOp Op);
765
766 static bool isFPOperation(BinOp Op) {
767 switch (Op) {
768 case AtomicRMWInst::FAdd:
769 case AtomicRMWInst::FSub:
770 return true;
771 default:
772 return false;
773 }
774 }
775
776 void setOperation(BinOp Operation) {
777 unsigned short SubclassData = getSubclassDataFromInstruction();
778 setInstructionSubclassData((SubclassData & 31) |
779 (Operation << 5));
780 }
781
782 /// Return true if this is a RMW on a volatile memory location.
783 ///
784 bool isVolatile() const {
785 return getSubclassDataFromInstruction() & 1;
786 }
787
788 /// Specify whether this is a volatile RMW or not.
789 ///
790 void setVolatile(bool V) {
791 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
792 (unsigned)V);
793 }
794
795 /// Transparently provide more efficient getOperand methods.
796 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
;
797
798 /// Returns the ordering constraint of this rmw instruction.
799 AtomicOrdering getOrdering() const {
800 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
801 }
802
803 /// Sets the ordering constraint of this rmw instruction.
804 void setOrdering(AtomicOrdering Ordering) {
805 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-10~svn374877/include/llvm/IR/Instructions.h"
, 806, __PRETTY_FUNCTION__))
806 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 806, __PRETTY_FUNCTION__))
;
807 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
808 ((unsigned)Ordering << 2));
809 }
810
811 /// Returns the synchronization scope ID of this rmw instruction.
812 SyncScope::ID getSyncScopeID() const {
813 return SSID;
814 }
815
816 /// Sets the synchronization scope ID of this rmw instruction.
817 void setSyncScopeID(SyncScope::ID SSID) {
818 this->SSID = SSID;
819 }
820
821 Value *getPointerOperand() { return getOperand(0); }
822 const Value *getPointerOperand() const { return getOperand(0); }
823 static unsigned getPointerOperandIndex() { return 0U; }
824
825 Value *getValOperand() { return getOperand(1); }
826 const Value *getValOperand() const { return getOperand(1); }
827
828 /// Returns the address space of the pointer operand.
829 unsigned getPointerAddressSpace() const {
830 return getPointerOperand()->getType()->getPointerAddressSpace();
831 }
832
833 bool isFloatingPointOperation() const {
834 return isFPOperation(getOperation());
835 }
836
837 // Methods for support type inquiry through isa, cast, and dyn_cast:
838 static bool classof(const Instruction *I) {
839 return I->getOpcode() == Instruction::AtomicRMW;
840 }
841 static bool classof(const Value *V) {
842 return isa<Instruction>(V) && classof(cast<Instruction>(V));
843 }
844
845private:
846 void Init(BinOp Operation, Value *Ptr, Value *Val,
847 AtomicOrdering Ordering, SyncScope::ID SSID);
848
849 // Shadow Instruction::setInstructionSubclassData with a private forwarding
850 // method so that subclasses cannot accidentally use it.
851 void setInstructionSubclassData(unsigned short D) {
852 Instruction::setInstructionSubclassData(D);
853 }
854
855 /// The synchronization scope ID of this rmw instruction. Not quite enough
856 /// room in SubClassData for everything, so synchronization scope ID gets its
857 /// own field.
858 SyncScope::ID SSID;
859};
860
861template <>
862struct OperandTraits<AtomicRMWInst>
863 : public FixedNumOperandTraits<AtomicRMWInst,2> {
864};
865
866DEFINE_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-10~svn374877/include/llvm/IR/Instructions.h"
, 866, __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-10~svn374877/include/llvm/IR/Instructions.h"
, 866, __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); }
867
868//===----------------------------------------------------------------------===//
869// GetElementPtrInst Class
870//===----------------------------------------------------------------------===//
871
872// checkGEPType - Simple wrapper function to give a better assertion failure
873// message on bad indexes for a gep instruction.
874//
875inline Type *checkGEPType(Type *Ty) {
876 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-10~svn374877/include/llvm/IR/Instructions.h"
, 876, __PRETTY_FUNCTION__))
;
877 return Ty;
878}
879
880/// an instruction for type-safe pointer arithmetic to
881/// access elements of arrays and structs
882///
883class GetElementPtrInst : public Instruction {
884 Type *SourceElementType;
885 Type *ResultElementType;
886
887 GetElementPtrInst(const GetElementPtrInst &GEPI);
888
889 /// Constructors - Create a getelementptr instruction with a base pointer an
890 /// list of indices. The first ctor can optionally insert before an existing
891 /// instruction, the second appends the new instruction to the specified
892 /// BasicBlock.
893 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
894 ArrayRef<Value *> IdxList, unsigned Values,
895 const Twine &NameStr, Instruction *InsertBefore);
896 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
897 ArrayRef<Value *> IdxList, unsigned Values,
898 const Twine &NameStr, BasicBlock *InsertAtEnd);
899
900 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
901
902protected:
903 // Note: Instruction needs to be a friend here to call cloneImpl.
904 friend class Instruction;
905
906 GetElementPtrInst *cloneImpl() const;
907
908public:
909 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
910 ArrayRef<Value *> IdxList,
911 const Twine &NameStr = "",
912 Instruction *InsertBefore = nullptr) {
913 unsigned Values = 1 + unsigned(IdxList.size());
914 if (!PointeeType)
915 PointeeType =
916 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
917 else
918 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-10~svn374877/include/llvm/IR/Instructions.h"
, 920, __PRETTY_FUNCTION__))
919 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-10~svn374877/include/llvm/IR/Instructions.h"
, 920, __PRETTY_FUNCTION__))
920 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-10~svn374877/include/llvm/IR/Instructions.h"
, 920, __PRETTY_FUNCTION__))
;
921 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
922 NameStr, InsertBefore);
923 }
924
925 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
926 ArrayRef<Value *> IdxList,
927 const Twine &NameStr,
928 BasicBlock *InsertAtEnd) {
929 unsigned Values = 1 + unsigned(IdxList.size());
930 if (!PointeeType)
931 PointeeType =
932 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
933 else
934 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-10~svn374877/include/llvm/IR/Instructions.h"
, 936, __PRETTY_FUNCTION__))
935 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-10~svn374877/include/llvm/IR/Instructions.h"
, 936, __PRETTY_FUNCTION__))
936 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-10~svn374877/include/llvm/IR/Instructions.h"
, 936, __PRETTY_FUNCTION__))
;
937 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
938 NameStr, InsertAtEnd);
939 }
940
941 /// Create an "inbounds" getelementptr. See the documentation for the
942 /// "inbounds" flag in LangRef.html for details.
943 static GetElementPtrInst *CreateInBounds(Value *Ptr,
944 ArrayRef<Value *> IdxList,
945 const Twine &NameStr = "",
946 Instruction *InsertBefore = nullptr){
947 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertBefore);
948 }
949
950 static GetElementPtrInst *
951 CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList,
952 const Twine &NameStr = "",
953 Instruction *InsertBefore = nullptr) {
954 GetElementPtrInst *GEP =
955 Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore);
956 GEP->setIsInBounds(true);
957 return GEP;
958 }
959
960 static GetElementPtrInst *CreateInBounds(Value *Ptr,
961 ArrayRef<Value *> IdxList,
962 const Twine &NameStr,
963 BasicBlock *InsertAtEnd) {
964 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertAtEnd);
965 }
966
967 static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr,
968 ArrayRef<Value *> IdxList,
969 const Twine &NameStr,
970 BasicBlock *InsertAtEnd) {
971 GetElementPtrInst *GEP =
972 Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd);
973 GEP->setIsInBounds(true);
974 return GEP;
975 }
976
977 /// Transparently provide more efficient getOperand methods.
978 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
;
979
980 Type *getSourceElementType() const { return SourceElementType; }
981
982 void setSourceElementType(Type *Ty) { SourceElementType = Ty; }
983 void setResultElementType(Type *Ty) { ResultElementType = Ty; }
984
985 Type *getResultElementType() const {
986 assert(ResultElementType ==((ResultElementType == cast<PointerType>(getType()->
getScalarType())->getElementType()) ? static_cast<void>
(0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()"
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 987, __PRETTY_FUNCTION__))
987 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-10~svn374877/include/llvm/IR/Instructions.h"
, 987, __PRETTY_FUNCTION__))
;
988 return ResultElementType;
989 }
990
991 /// Returns the address space of this instruction's pointer type.
992 unsigned getAddressSpace() const {
993 // Note that this is always the same as the pointer operand's address space
994 // and that is cheaper to compute, so cheat here.
995 return getPointerAddressSpace();
996 }
997
998 /// Returns the type of the element that would be loaded with
999 /// a load instruction with the specified parameters.
1000 ///
1001 /// Null is returned if the indices are invalid for the specified
1002 /// pointer type.
1003 ///
1004 static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList);
1005 static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList);
1006 static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList);
1007
1008 inline op_iterator idx_begin() { return op_begin()+1; }
1009 inline const_op_iterator idx_begin() const { return op_begin()+1; }
1010 inline op_iterator idx_end() { return op_end(); }
1011 inline const_op_iterator idx_end() const { return op_end(); }
1012
1013 inline iterator_range<op_iterator> indices() {
1014 return make_range(idx_begin(), idx_end());
1015 }
1016
1017 inline iterator_range<const_op_iterator> indices() const {
1018 return make_range(idx_begin(), idx_end());
1019 }
1020
1021 Value *getPointerOperand() {
1022 return getOperand(0);
1023 }
1024 const Value *getPointerOperand() const {
1025 return getOperand(0);
1026 }
1027 static unsigned getPointerOperandIndex() {
1028 return 0U; // get index for modifying correct operand.
1029 }
1030
1031 /// Method to return the pointer operand as a
1032 /// PointerType.
1033 Type *getPointerOperandType() const {
1034 return getPointerOperand()->getType();
1035 }
1036
1037 /// Returns the address space of the pointer operand.
1038 unsigned getPointerAddressSpace() const {
1039 return getPointerOperandType()->getPointerAddressSpace();
1040 }
1041
1042 /// Returns the pointer type returned by the GEP
1043 /// instruction, which may be a vector of pointers.
1044 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
1045 return getGEPReturnType(
1046 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType(),
1047 Ptr, IdxList);
1048 }
1049 static Type *getGEPReturnType(Type *ElTy, Value *Ptr,
1050 ArrayRef<Value *> IdxList) {
1051 Type *PtrTy = PointerType::get(checkGEPType(getIndexedType(ElTy, IdxList)),
1052 Ptr->getType()->getPointerAddressSpace());
1053 // Vector GEP
1054 if (Ptr->getType()->isVectorTy()) {
1055 unsigned NumElem = Ptr->getType()->getVectorNumElements();
1056 return VectorType::get(PtrTy, NumElem);
1057 }
1058 for (Value *Index : IdxList)
1059 if (Index->getType()->isVectorTy()) {
1060 unsigned NumElem = Index->getType()->getVectorNumElements();
1061 return VectorType::get(PtrTy, NumElem);
1062 }
1063 // Scalar GEP
1064 return PtrTy;
1065 }
1066
1067 unsigned getNumIndices() const { // Note: always non-negative
1068 return getNumOperands() - 1;
1069 }
1070
1071 bool hasIndices() const {
1072 return getNumOperands() > 1;
1073 }
1074
1075 /// Return true if all of the indices of this GEP are
1076 /// zeros. If so, the result pointer and the first operand have the same
1077 /// value, just potentially different types.
1078 bool hasAllZeroIndices() const;
1079
1080 /// Return true if all of the indices of this GEP are
1081 /// constant integers. If so, the result pointer and the first operand have
1082 /// a constant offset between them.
1083 bool hasAllConstantIndices() const;
1084
1085 /// Set or clear the inbounds flag on this GEP instruction.
1086 /// See LangRef.html for the meaning of inbounds on a getelementptr.
1087 void setIsInBounds(bool b = true);
1088
1089 /// Determine whether the GEP has the inbounds flag.
1090 bool isInBounds() const;
1091
1092 /// Accumulate the constant address offset of this GEP if possible.
1093 ///
1094 /// This routine accepts an APInt into which it will accumulate the constant
1095 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
1096 /// all-constant, it returns false and the value of the offset APInt is
1097 /// undefined (it is *not* preserved!). The APInt passed into this routine
1098 /// must be at least as wide as the IntPtr type for the address space of
1099 /// the base GEP pointer.
1100 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
1101
1102 // Methods for support type inquiry through isa, cast, and dyn_cast:
1103 static bool classof(const Instruction *I) {
1104 return (I->getOpcode() == Instruction::GetElementPtr);
1105 }
1106 static bool classof(const Value *V) {
1107 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1108 }
1109};
1110
1111template <>
1112struct OperandTraits<GetElementPtrInst> :
1113 public VariadicOperandTraits<GetElementPtrInst, 1> {
1114};
1115
1116GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1117 ArrayRef<Value *> IdxList, unsigned Values,
1118 const Twine &NameStr,
1119 Instruction *InsertBefore)
1120 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1121 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1122 Values, InsertBefore),
1123 SourceElementType(PointeeType),
1124 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1125 assert(ResultElementType ==((ResultElementType == cast<PointerType>(getType()->
getScalarType())->getElementType()) ? static_cast<void>
(0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()"
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 1126, __PRETTY_FUNCTION__))
1126 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-10~svn374877/include/llvm/IR/Instructions.h"
, 1126, __PRETTY_FUNCTION__))
;
1127 init(Ptr, IdxList, NameStr);
1128}
1129
1130GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1131 ArrayRef<Value *> IdxList, unsigned Values,
1132 const Twine &NameStr,
1133 BasicBlock *InsertAtEnd)
1134 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1135 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1136 Values, InsertAtEnd),
1137 SourceElementType(PointeeType),
1138 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1139 assert(ResultElementType ==((ResultElementType == cast<PointerType>(getType()->
getScalarType())->getElementType()) ? static_cast<void>
(0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()"
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 1140, __PRETTY_FUNCTION__))
1140 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-10~svn374877/include/llvm/IR/Instructions.h"
, 1140, __PRETTY_FUNCTION__))
;
1141 init(Ptr, IdxList, NameStr);
1142}
1143
1144DEFINE_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-10~svn374877/include/llvm/IR/Instructions.h"
, 1144, __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-10~svn374877/include/llvm/IR/Instructions.h"
, 1144, __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); }
1145
1146//===----------------------------------------------------------------------===//
1147// ICmpInst Class
1148//===----------------------------------------------------------------------===//
1149
1150/// This instruction compares its operands according to the predicate given
1151/// to the constructor. It only operates on integers or pointers. The operands
1152/// must be identical types.
1153/// Represent an integer comparison operator.
1154class ICmpInst: public CmpInst {
1155 void AssertOK() {
1156 assert(isIntPredicate() &&((isIntPredicate() && "Invalid ICmp predicate value")
? static_cast<void> (0) : __assert_fail ("isIntPredicate() && \"Invalid ICmp predicate value\""
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 1157, __PRETTY_FUNCTION__))
1157 "Invalid ICmp predicate value")((isIntPredicate() && "Invalid ICmp predicate value")
? static_cast<void> (0) : __assert_fail ("isIntPredicate() && \"Invalid ICmp predicate value\""
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 1157, __PRETTY_FUNCTION__))
;
1158 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-10~svn374877/include/llvm/IR/Instructions.h"
, 1159, __PRETTY_FUNCTION__))
1159 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 1159, __PRETTY_FUNCTION__))
;
1160 // Check that the operands are the right type
1161 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-10~svn374877/include/llvm/IR/Instructions.h"
, 1163, __PRETTY_FUNCTION__))
1162 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-10~svn374877/include/llvm/IR/Instructions.h"
, 1163, __PRETTY_FUNCTION__))
1163 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 1163, __PRETTY_FUNCTION__))
;
1164 }
1165
1166protected:
1167 // Note: Instruction needs to be a friend here to call cloneImpl.
1168 friend class Instruction;
1169
1170 /// Clone an identical ICmpInst
1171 ICmpInst *cloneImpl() const;
1172
1173public:
1174 /// Constructor with insert-before-instruction semantics.
1175 ICmpInst(
1176 Instruction *InsertBefore, ///< Where to insert
1177 Predicate pred, ///< The predicate to use for the comparison
1178 Value *LHS, ///< The left-hand-side of the expression
1179 Value *RHS, ///< The right-hand-side of the expression
1180 const Twine &NameStr = "" ///< Name of the instruction
1181 ) : CmpInst(makeCmpResultType(LHS->getType()),
1182 Instruction::ICmp, pred, LHS, RHS, NameStr,
1183 InsertBefore) {
1184#ifndef NDEBUG
1185 AssertOK();
1186#endif
1187 }
1188
1189 /// Constructor with insert-at-end semantics.
1190 ICmpInst(
1191 BasicBlock &InsertAtEnd, ///< Block to insert into.
1192 Predicate pred, ///< The predicate to use for the comparison
1193 Value *LHS, ///< The left-hand-side of the expression
1194 Value *RHS, ///< The right-hand-side of the expression
1195 const Twine &NameStr = "" ///< Name of the instruction
1196 ) : CmpInst(makeCmpResultType(LHS->getType()),
1197 Instruction::ICmp, pred, LHS, RHS, NameStr,
1198 &InsertAtEnd) {
1199#ifndef NDEBUG
1200 AssertOK();
1201#endif
1202 }
1203
1204 /// Constructor with no-insertion semantics
1205 ICmpInst(
1206 Predicate pred, ///< The predicate to use for the comparison
1207 Value *LHS, ///< The left-hand-side of the expression
1208 Value *RHS, ///< The right-hand-side of the expression
1209 const Twine &NameStr = "" ///< Name of the instruction
1210 ) : CmpInst(makeCmpResultType(LHS->getType()),
1211 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1212#ifndef NDEBUG
1213 AssertOK();
1214#endif
1215 }
1216
1217 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1218 /// @returns the predicate that would be the result if the operand were
1219 /// regarded as signed.
1220 /// Return the signed version of the predicate
1221 Predicate getSignedPredicate() const {
1222 return getSignedPredicate(getPredicate());
1223 }
1224
1225 /// This is a static version that you can use without an instruction.
1226 /// Return the signed version of the predicate.
1227 static Predicate getSignedPredicate(Predicate pred);
1228
1229 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1230 /// @returns the predicate that would be the result if the operand were
1231 /// regarded as unsigned.
1232 /// Return the unsigned version of the predicate
1233 Predicate getUnsignedPredicate() const {
1234 return getUnsignedPredicate(getPredicate());
1235 }
1236
1237 /// This is a static version that you can use without an instruction.
1238 /// Return the unsigned version of the predicate.
1239 static Predicate getUnsignedPredicate(Predicate pred);
1240
1241 /// Return true if this predicate is either EQ or NE. This also
1242 /// tests for commutativity.
1243 static bool isEquality(Predicate P) {
1244 return P == ICMP_EQ || P == ICMP_NE;
1245 }
1246
1247 /// Return true if this predicate is either EQ or NE. This also
1248 /// tests for commutativity.
1249 bool isEquality() const {
1250 return isEquality(getPredicate());
1251 }
1252
1253 /// @returns true if the predicate of this ICmpInst is commutative
1254 /// Determine if this relation is commutative.
1255 bool isCommutative() const { return isEquality(); }
1256
1257 /// Return true if the predicate is relational (not EQ or NE).
1258 ///
1259 bool isRelational() const {
1260 return !isEquality();
1261 }
1262
1263 /// Return true if the predicate is relational (not EQ or NE).
1264 ///
1265 static bool isRelational(Predicate P) {
1266 return !isEquality(P);
1267 }
1268
1269 /// Exchange the two operands to this instruction in such a way that it does
1270 /// not modify the semantics of the instruction. The predicate value may be
1271 /// changed to retain the same result if the predicate is order dependent
1272 /// (e.g. ult).
1273 /// Swap operands and adjust predicate.
1274 void swapOperands() {
1275 setPredicate(getSwappedPredicate());
1276 Op<0>().swap(Op<1>());
1277 }
1278
1279 // Methods for support type inquiry through isa, cast, and dyn_cast:
1280 static bool classof(const Instruction *I) {
1281 return I->getOpcode() == Instruction::ICmp;
1282 }
1283 static bool classof(const Value *V) {
1284 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1285 }
1286};
1287
1288//===----------------------------------------------------------------------===//
1289// FCmpInst Class
1290//===----------------------------------------------------------------------===//
1291
1292/// This instruction compares its operands according to the predicate given
1293/// to the constructor. It only operates on floating point values or packed
1294/// vectors of floating point values. The operands must be identical types.
1295/// Represents a floating point comparison operator.
1296class FCmpInst: public CmpInst {
1297 void AssertOK() {
1298 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-10~svn374877/include/llvm/IR/Instructions.h"
, 1298, __PRETTY_FUNCTION__))
;
1299 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-10~svn374877/include/llvm/IR/Instructions.h"
, 1300, __PRETTY_FUNCTION__))
1300 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 1300, __PRETTY_FUNCTION__))
;
1301 // Check that the operands are the right type
1302 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-10~svn374877/include/llvm/IR/Instructions.h"
, 1303, __PRETTY_FUNCTION__))
1303 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 1303, __PRETTY_FUNCTION__))
;
1304 }
1305
1306protected:
1307 // Note: Instruction needs to be a friend here to call cloneImpl.
1308 friend class Instruction;
1309
1310 /// Clone an identical FCmpInst
1311 FCmpInst *cloneImpl() const;
1312
1313public:
1314 /// Constructor with insert-before-instruction semantics.
1315 FCmpInst(
1316 Instruction *InsertBefore, ///< Where to insert
1317 Predicate pred, ///< The predicate to use for the comparison
1318 Value *LHS, ///< The left-hand-side of the expression
1319 Value *RHS, ///< The right-hand-side of the expression
1320 const Twine &NameStr = "" ///< Name of the instruction
1321 ) : CmpInst(makeCmpResultType(LHS->getType()),
1322 Instruction::FCmp, pred, LHS, RHS, NameStr,
1323 InsertBefore) {
1324 AssertOK();
1325 }
1326
1327 /// Constructor with insert-at-end semantics.
1328 FCmpInst(
1329 BasicBlock &InsertAtEnd, ///< Block to insert into.
1330 Predicate pred, ///< The predicate to use for the comparison
1331 Value *LHS, ///< The left-hand-side of the expression
1332 Value *RHS, ///< The right-hand-side of the expression
1333 const Twine &NameStr = "" ///< Name of the instruction
1334 ) : CmpInst(makeCmpResultType(LHS->getType()),
1335 Instruction::FCmp, pred, LHS, RHS, NameStr,
1336 &InsertAtEnd) {
1337 AssertOK();
1338 }
1339
1340 /// Constructor with no-insertion semantics
1341 FCmpInst(
1342 Predicate Pred, ///< The predicate to use for the comparison
1343 Value *LHS, ///< The left-hand-side of the expression
1344 Value *RHS, ///< The right-hand-side of the expression
1345 const Twine &NameStr = "", ///< Name of the instruction
1346 Instruction *FlagsSource = nullptr
1347 ) : CmpInst(makeCmpResultType(LHS->getType()), Instruction::FCmp, Pred, LHS,
49
Called C++ object pointer is null
1348 RHS, NameStr, nullptr, FlagsSource) {
1349 AssertOK();
1350 }
1351
1352 /// @returns true if the predicate of this instruction is EQ or NE.
1353 /// Determine if this is an equality predicate.
1354 static bool isEquality(Predicate Pred) {
1355 return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ ||
1356 Pred == FCMP_UNE;
1357 }
1358
1359 /// @returns true if the predicate of this instruction is EQ or NE.
1360 /// Determine if this is an equality predicate.
1361 bool isEquality() const { return isEquality(getPredicate()); }
1362
1363 /// @returns true if the predicate of this instruction is commutative.
1364 /// Determine if this is a commutative predicate.
1365 bool isCommutative() const {
1366 return isEquality() ||
1367 getPredicate() == FCMP_FALSE ||
1368 getPredicate() == FCMP_TRUE ||
1369 getPredicate() == FCMP_ORD ||
1370 getPredicate() == FCMP_UNO;
1371 }
1372
1373 /// @returns true if the predicate is relational (not EQ or NE).
1374 /// Determine if this a relational predicate.
1375 bool isRelational() const { return !isEquality(); }
1376
1377 /// Exchange the two operands to this instruction in such a way that it does
1378 /// not modify the semantics of the instruction. The predicate value may be
1379 /// changed to retain the same result if the predicate is order dependent
1380 /// (e.g. ult).
1381 /// Swap operands and adjust predicate.
1382 void swapOperands() {
1383 setPredicate(getSwappedPredicate());
1384 Op<0>().swap(Op<1>());
1385 }
1386
1387 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1388 static bool classof(const Instruction *I) {
1389 return I->getOpcode() == Instruction::FCmp;
1390 }
1391 static bool classof(const Value *V) {
1392 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1393 }
1394};
1395
1396//===----------------------------------------------------------------------===//
1397/// This class represents a function call, abstracting a target
1398/// machine's calling convention. This class uses low bit of the SubClassData
1399/// field to indicate whether or not this is a tail call. The rest of the bits
1400/// hold the calling convention of the call.
1401///
1402class CallInst : public CallBase {
1403 CallInst(const CallInst &CI);
1404
1405 /// Construct a CallInst given a range of arguments.
1406 /// Construct a CallInst from a range of arguments
1407 inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1408 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1409 Instruction *InsertBefore);
1410
1411 inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1412 const Twine &NameStr, Instruction *InsertBefore)
1413 : CallInst(Ty, Func, Args, None, NameStr, InsertBefore) {}
1414
1415 /// Construct a CallInst given a range of arguments.
1416 /// Construct a CallInst from a range of arguments
1417 inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1418 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1419 BasicBlock *InsertAtEnd);
1420
1421 explicit CallInst(FunctionType *Ty, Value *F, const Twine &NameStr,
1422 Instruction *InsertBefore);
1423
1424 CallInst(FunctionType *ty, Value *F, const Twine &NameStr,
1425 BasicBlock *InsertAtEnd);
1426
1427 void init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
1428 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
1429 void init(FunctionType *FTy, Value *Func, const Twine &NameStr);
1430
1431 /// Compute the number of operands to allocate.
1432 static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) {
1433 // We need one operand for the called function, plus the input operand
1434 // counts provided.
1435 return 1 + NumArgs + NumBundleInputs;
1436 }
1437
1438protected:
1439 // Note: Instruction needs to be a friend here to call cloneImpl.
1440 friend class Instruction;
1441
1442 CallInst *cloneImpl() const;
1443
1444public:
1445 static CallInst *Create(FunctionType *Ty, Value *F, const Twine &NameStr = "",
1446 Instruction *InsertBefore = nullptr) {
1447 return new (ComputeNumOperands(0)) CallInst(Ty, F, NameStr, InsertBefore);
1448 }
1449
1450 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1451 const Twine &NameStr,
1452 Instruction *InsertBefore = nullptr) {
1453 return new (ComputeNumOperands(Args.size()))
1454 CallInst(Ty, Func, Args, None, NameStr, InsertBefore);
1455 }
1456
1457 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1458 ArrayRef<OperandBundleDef> Bundles = None,
1459 const Twine &NameStr = "",
1460 Instruction *InsertBefore = nullptr) {
1461 const int NumOperands =
1462 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles));
1463 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
1464
1465 return new (NumOperands, DescriptorBytes)
1466 CallInst(Ty, Func, Args, Bundles, NameStr, InsertBefore);
1467 }
1468
1469 static CallInst *Create(FunctionType *Ty, Value *F, const Twine &NameStr,
1470 BasicBlock *InsertAtEnd) {
1471 return new (ComputeNumOperands(0)) CallInst(Ty, F, NameStr, InsertAtEnd);
1472 }
1473
1474 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1475 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1476 return new (ComputeNumOperands(Args.size()))
1477 CallInst(Ty, Func, Args, None, NameStr, InsertAtEnd);
1478 }
1479
1480 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1481 ArrayRef<OperandBundleDef> Bundles,
1482 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1483 const int NumOperands =
1484 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles));
1485 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
1486
1487 return new (NumOperands, DescriptorBytes)
1488 CallInst(Ty, Func, Args, Bundles, NameStr, InsertAtEnd);
1489 }
1490
1491 static CallInst *Create(FunctionCallee Func, const Twine &NameStr = "",
1492 Instruction *InsertBefore = nullptr) {
1493 return Create(Func.getFunctionType(), Func.getCallee(), NameStr,
1494 InsertBefore);
1495 }
1496
1497 static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args,
1498 ArrayRef<OperandBundleDef> Bundles = None,
1499 const Twine &NameStr = "",
1500 Instruction *InsertBefore = nullptr) {
1501 return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles,
1502 NameStr, InsertBefore);
1503 }
1504
1505 static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args,
1506 const Twine &NameStr,
1507 Instruction *InsertBefore = nullptr) {
1508 return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr,
1509 InsertBefore);
1510 }
1511
1512 static CallInst *Create(FunctionCallee Func, const Twine &NameStr,
1513 BasicBlock *InsertAtEnd) {
1514 return Create(Func.getFunctionType(), Func.getCallee(), NameStr,
1515 InsertAtEnd);
1516 }
1517
1518 static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args,
1519 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1520 return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr,
1521 InsertAtEnd);
1522 }
1523
1524 static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args,
1525 ArrayRef<OperandBundleDef> Bundles,
1526 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1527 return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles,
1528 NameStr, InsertAtEnd);
1529 }
1530
1531 // Deprecated [opaque pointer types]
1532 static CallInst *Create(Value *Func, const Twine &NameStr = "",
1533 Instruction *InsertBefore = nullptr) {
1534 return Create(cast<FunctionType>(
1535 cast<PointerType>(Func->getType())->getElementType()),
1536 Func, NameStr, InsertBefore);
1537 }
1538
1539 // Deprecated [opaque pointer types]
1540 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1541 const Twine &NameStr,
1542 Instruction *InsertBefore = nullptr) {
1543 return Create(cast<FunctionType>(
1544 cast<PointerType>(Func->getType())->getElementType()),
1545 Func, Args, NameStr, InsertBefore);
1546 }
1547
1548 // Deprecated [opaque pointer types]
1549 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1550 ArrayRef<OperandBundleDef> Bundles = None,
1551 const Twine &NameStr = "",
1552 Instruction *InsertBefore = nullptr) {
1553 return Create(cast<FunctionType>(
1554 cast<PointerType>(Func->getType())->getElementType()),
1555 Func, Args, Bundles, NameStr, InsertBefore);
1556 }
1557
1558 // Deprecated [opaque pointer types]
1559 static CallInst *Create(Value *Func, const Twine &NameStr,
1560 BasicBlock *InsertAtEnd) {
1561 return Create(cast<FunctionType>(
1562 cast<PointerType>(Func->getType())->getElementType()),
1563 Func, NameStr, InsertAtEnd);
1564 }
1565
1566 // Deprecated [opaque pointer types]
1567 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1568 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1569 return Create(cast<FunctionType>(
1570 cast<PointerType>(Func->getType())->getElementType()),
1571 Func, Args, NameStr, InsertAtEnd);
1572 }
1573
1574 // Deprecated [opaque pointer types]
1575 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1576 ArrayRef<OperandBundleDef> Bundles,
1577 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1578 return Create(cast<FunctionType>(
1579 cast<PointerType>(Func->getType())->getElementType()),
1580 Func, Args, Bundles, NameStr, InsertAtEnd);
1581 }
1582
1583 /// Create a clone of \p CI with a different set of operand bundles and
1584 /// insert it before \p InsertPt.
1585 ///
1586 /// The returned call instruction is identical \p CI in every way except that
1587 /// the operand bundles for the new instruction are set to the operand bundles
1588 /// in \p Bundles.
1589 static CallInst *Create(CallInst *CI, ArrayRef<OperandBundleDef> Bundles,
1590 Instruction *InsertPt = nullptr);
1591
1592 /// Generate the IR for a call to malloc:
1593 /// 1. Compute the malloc call's argument as the specified type's size,
1594 /// possibly multiplied by the array size if the array size is not
1595 /// constant 1.
1596 /// 2. Call malloc with that argument.
1597 /// 3. Bitcast the result of the malloc call to the specified type.
1598 static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy,
1599 Type *AllocTy, Value *AllocSize,
1600 Value *ArraySize = nullptr,
1601 Function *MallocF = nullptr,
1602 const Twine &Name = "");
1603 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy,
1604 Type *AllocTy, Value *AllocSize,
1605 Value *ArraySize = nullptr,
1606 Function *MallocF = nullptr,
1607 const Twine &Name = "");
1608 static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy,
1609 Type *AllocTy, Value *AllocSize,
1610 Value *ArraySize = nullptr,
1611 ArrayRef<OperandBundleDef> Bundles = None,
1612 Function *MallocF = nullptr,
1613 const Twine &Name = "");
1614 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy,
1615 Type *AllocTy, Value *AllocSize,
1616 Value *ArraySize = nullptr,
1617 ArrayRef<OperandBundleDef> Bundles = None,
1618 Function *MallocF = nullptr,
1619 const Twine &Name = "");
1620 /// Generate the IR for a call to the builtin free function.
1621 static Instruction *CreateFree(Value *Source, Instruction *InsertBefore);
1622 static Instruction *CreateFree(Value *Source, BasicBlock *InsertAtEnd);
1623 static Instruction *CreateFree(Value *Source,
1624 ArrayRef<OperandBundleDef> Bundles,
1625 Instruction *InsertBefore);
1626 static Instruction *CreateFree(Value *Source,
1627 ArrayRef<OperandBundleDef> Bundles,
1628 BasicBlock *InsertAtEnd);
1629
1630 // Note that 'musttail' implies 'tail'.
1631 enum TailCallKind {
1632 TCK_None = 0,
1633 TCK_Tail = 1,
1634 TCK_MustTail = 2,
1635 TCK_NoTail = 3
1636 };
1637 TailCallKind getTailCallKind() const {
1638 return TailCallKind(getSubclassDataFromInstruction() & 3);
1639 }
1640
1641 bool isTailCall() const {
1642 unsigned Kind = getSubclassDataFromInstruction() & 3;
1643 return Kind == TCK_Tail || Kind == TCK_MustTail;
1644 }
1645
1646 bool isMustTailCall() const {
1647 return (getSubclassDataFromInstruction() & 3) == TCK_MustTail;
1648 }
1649
1650 bool isNoTailCall() const {
1651 return (getSubclassDataFromInstruction() & 3) == TCK_NoTail;
1652 }
1653
1654 void setTailCall(bool isTC = true) {
1655 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1656 unsigned(isTC ? TCK_Tail : TCK_None));
1657 }
1658
1659 void setTailCallKind(TailCallKind TCK) {
1660 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1661 unsigned(TCK));
1662 }
1663
1664 /// Return true if the call can return twice
1665 bool canReturnTwice() const { return hasFnAttr(Attribute::ReturnsTwice); }
1666 void setCanReturnTwice() {
1667 addAttribute(AttributeList::FunctionIndex, Attribute::ReturnsTwice);
1668 }
1669
1670 // Methods for support type inquiry through isa, cast, and dyn_cast:
1671 static bool classof(const Instruction *I) {
1672 return I->getOpcode() == Instruction::Call;
1673 }
1674 static bool classof(const Value *V) {
1675 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1676 }
1677
1678 /// Updates profile metadata by scaling it by \p S / \p T.
1679 void updateProfWeight(uint64_t S, uint64_t T);
1680
1681private:
1682 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1683 // method so that subclasses cannot accidentally use it.
1684 void setInstructionSubclassData(unsigned short D) {
1685 Instruction::setInstructionSubclassData(D);
1686 }
1687};
1688
1689CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1690 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1691 BasicBlock *InsertAtEnd)
1692 : CallBase(Ty->getReturnType(), Instruction::Call,
1693 OperandTraits<CallBase>::op_end(this) -
1694 (Args.size() + CountBundleInputs(Bundles) + 1),
1695 unsigned(Args.size() + CountBundleInputs(Bundles) + 1),
1696 InsertAtEnd) {
1697 init(Ty, Func, Args, Bundles, NameStr);
1698}
1699
1700CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1701 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1702 Instruction *InsertBefore)
1703 : CallBase(Ty->getReturnType(), Instruction::Call,
1704 OperandTraits<CallBase>::op_end(this) -
1705 (Args.size() + CountBundleInputs(Bundles) + 1),
1706 unsigned(Args.size() + CountBundleInputs(Bundles) + 1),
1707 InsertBefore) {
1708 init(Ty, Func, Args, Bundles, NameStr);
1709}
1710
1711//===----------------------------------------------------------------------===//
1712// SelectInst Class
1713//===----------------------------------------------------------------------===//
1714
1715/// This class represents the LLVM 'select' instruction.
1716///
1717class SelectInst : public Instruction {
1718 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1719 Instruction *InsertBefore)
1720 : Instruction(S1->getType(), Instruction::Select,
1721 &Op<0>(), 3, InsertBefore) {
1722 init(C, S1, S2);
1723 setName(NameStr);
1724 }
1725
1726 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1727 BasicBlock *InsertAtEnd)
1728 : Instruction(S1->getType(), Instruction::Select,
1729 &Op<0>(), 3, InsertAtEnd) {
1730 init(C, S1, S2);
1731 setName(NameStr);
1732 }
1733
1734 void init(Value *C, Value *S1, Value *S2) {
1735 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-10~svn374877/include/llvm/IR/Instructions.h"
, 1735, __PRETTY_FUNCTION__))
;
1736 Op<0>() = C;
1737 Op<1>() = S1;
1738 Op<2>() = S2;
1739 }
1740
1741protected:
1742 // Note: Instruction needs to be a friend here to call cloneImpl.
1743 friend class Instruction;
1744
1745 SelectInst *cloneImpl() const;
1746
1747public:
1748 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1749 const Twine &NameStr = "",
1750 Instruction *InsertBefore = nullptr,
1751 Instruction *MDFrom = nullptr) {
1752 SelectInst *Sel = new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1753 if (MDFrom)
1754 Sel->copyMetadata(*MDFrom);
1755 return Sel;
1756 }
1757
1758 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1759 const Twine &NameStr,
1760 BasicBlock *InsertAtEnd) {
1761 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1762 }
1763
1764 const Value *getCondition() const { return Op<0>(); }
1765 const Value *getTrueValue() const { return Op<1>(); }
1766 const Value *getFalseValue() const { return Op<2>(); }
1767 Value *getCondition() { return Op<0>(); }
1768 Value *getTrueValue() { return Op<1>(); }
1769 Value *getFalseValue() { return Op<2>(); }
1770
1771 void setCondition(Value *V) { Op<0>() = V; }
1772 void setTrueValue(Value *V) { Op<1>() = V; }
1773 void setFalseValue(Value *V) { Op<2>() = V; }
1774
1775 /// Swap the true and false values of the select instruction.
1776 /// This doesn't swap prof metadata.
1777 void swapValues() { Op<1>().swap(Op<2>()); }
1778
1779 /// Return a string if the specified operands are invalid
1780 /// for a select operation, otherwise return null.
1781 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1782
1783 /// Transparently provide more efficient getOperand methods.
1784 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
;
1785
1786 OtherOps getOpcode() const {
1787 return static_cast<OtherOps>(Instruction::getOpcode());
1788 }
1789
1790 // Methods for support type inquiry through isa, cast, and dyn_cast:
1791 static bool classof(const Instruction *I) {
1792 return I->getOpcode() == Instruction::Select;
1793 }
1794 static bool classof(const Value *V) {
1795 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1796 }
1797};
1798
1799template <>
1800struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1801};
1802
1803DEFINE_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-10~svn374877/include/llvm/IR/Instructions.h"
, 1803, __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-10~svn374877/include/llvm/IR/Instructions.h"
, 1803, __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); }
1804
1805//===----------------------------------------------------------------------===//
1806// VAArgInst Class
1807//===----------------------------------------------------------------------===//
1808
1809/// This class represents the va_arg llvm instruction, which returns
1810/// an argument of the specified type given a va_list and increments that list
1811///
1812class VAArgInst : public UnaryInstruction {
1813protected:
1814 // Note: Instruction needs to be a friend here to call cloneImpl.
1815 friend class Instruction;
1816
1817 VAArgInst *cloneImpl() const;
1818
1819public:
1820 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1821 Instruction *InsertBefore = nullptr)
1822 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1823 setName(NameStr);
1824 }
1825
1826 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1827 BasicBlock *InsertAtEnd)
1828 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1829 setName(NameStr);
1830 }
1831
1832 Value *getPointerOperand() { return getOperand(0); }
1833 const Value *getPointerOperand() const { return getOperand(0); }
1834 static unsigned getPointerOperandIndex() { return 0U; }
1835
1836 // Methods for support type inquiry through isa, cast, and dyn_cast:
1837 static bool classof(const Instruction *I) {
1838 return I->getOpcode() == VAArg;
1839 }
1840 static bool classof(const Value *V) {
1841 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1842 }
1843};
1844
1845//===----------------------------------------------------------------------===//
1846// ExtractElementInst Class
1847//===----------------------------------------------------------------------===//
1848
1849/// This instruction extracts a single (scalar)
1850/// element from a VectorType value
1851///
1852class ExtractElementInst : public Instruction {
1853 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1854 Instruction *InsertBefore = nullptr);
1855 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1856 BasicBlock *InsertAtEnd);
1857
1858protected:
1859 // Note: Instruction needs to be a friend here to call cloneImpl.
1860 friend class Instruction;
1861
1862 ExtractElementInst *cloneImpl() const;
1863
1864public:
1865 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1866 const Twine &NameStr = "",
1867 Instruction *InsertBefore = nullptr) {
1868 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1869 }
1870
1871 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1872 const Twine &NameStr,
1873 BasicBlock *InsertAtEnd) {
1874 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1875 }
1876
1877 /// Return true if an extractelement instruction can be
1878 /// formed with the specified operands.
1879 static bool isValidOperands(const Value *Vec, const Value *Idx);
1880
1881 Value *getVectorOperand() { return Op<0>(); }
1882 Value *getIndexOperand() { return Op<1>(); }
1883 const Value *getVectorOperand() const { return Op<0>(); }
1884 const Value *getIndexOperand() const { return Op<1>(); }
1885
1886 VectorType *getVectorOperandType() const {
1887 return cast<VectorType>(getVectorOperand()->getType());
1888 }
1889
1890 /// Transparently provide more efficient getOperand methods.
1891 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
;
1892
1893 // Methods for support type inquiry through isa, cast, and dyn_cast:
1894 static bool classof(const Instruction *I) {
1895 return I->getOpcode() == Instruction::ExtractElement;
1896 }
1897 static bool classof(const Value *V) {
1898 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1899 }
1900};
1901
1902template <>
1903struct OperandTraits<ExtractElementInst> :
1904 public FixedNumOperandTraits<ExtractElementInst, 2> {
1905};
1906
1907DEFINE_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-10~svn374877/include/llvm/IR/Instructions.h"
, 1907, __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-10~svn374877/include/llvm/IR/Instructions.h"
, 1907, __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); }
1908
1909//===----------------------------------------------------------------------===//
1910// InsertElementInst Class
1911//===----------------------------------------------------------------------===//
1912
1913/// This instruction inserts a single (scalar)
1914/// element into a VectorType value
1915///
1916class InsertElementInst : public Instruction {
1917 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1918 const Twine &NameStr = "",
1919 Instruction *InsertBefore = nullptr);
1920 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr,
1921 BasicBlock *InsertAtEnd);
1922
1923protected:
1924 // Note: Instruction needs to be a friend here to call cloneImpl.
1925 friend class Instruction;
1926
1927 InsertElementInst *cloneImpl() const;
1928
1929public:
1930 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1931 const Twine &NameStr = "",
1932 Instruction *InsertBefore = nullptr) {
1933 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1934 }
1935
1936 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1937 const Twine &NameStr,
1938 BasicBlock *InsertAtEnd) {
1939 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1940 }
1941
1942 /// Return true if an insertelement instruction can be
1943 /// formed with the specified operands.
1944 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1945 const Value *Idx);
1946
1947 /// Overload to return most specific vector type.
1948 ///
1949 VectorType *getType() const {
1950 return cast<VectorType>(Instruction::getType());
1951 }
1952
1953 /// Transparently provide more efficient getOperand methods.
1954 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
;
1955
1956 // Methods for support type inquiry through isa, cast, and dyn_cast:
1957 static bool classof(const Instruction *I) {
1958 return I->getOpcode() == Instruction::InsertElement;
1959 }
1960 static bool classof(const Value *V) {
1961 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1962 }
1963};
1964
1965template <>
1966struct OperandTraits<InsertElementInst> :
1967 public FixedNumOperandTraits<InsertElementInst, 3> {
1968};
1969
1970DEFINE_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-10~svn374877/include/llvm/IR/Instructions.h"
, 1970, __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-10~svn374877/include/llvm/IR/Instructions.h"
, 1970, __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); }
1971
1972//===----------------------------------------------------------------------===//
1973// ShuffleVectorInst Class
1974//===----------------------------------------------------------------------===//
1975
1976/// This instruction constructs a fixed permutation of two
1977/// input vectors.
1978///
1979class ShuffleVectorInst : public Instruction {
1980protected:
1981 // Note: Instruction needs to be a friend here to call cloneImpl.
1982 friend class Instruction;
1983
1984 ShuffleVectorInst *cloneImpl() const;
1985
1986public:
1987 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1988 const Twine &NameStr = "",
1989 Instruction *InsertBefor = nullptr);
1990 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1991 const Twine &NameStr, BasicBlock *InsertAtEnd);
1992
1993 // allocate space for exactly three operands
1994 void *operator new(size_t s) {
1995 return User::operator new(s, 3);
1996 }
1997
1998 /// Swap the first 2 operands and adjust the mask to preserve the semantics
1999 /// of the instruction.
2000 void commute();
2001
2002 /// Return true if a shufflevector instruction can be
2003 /// formed with the specified operands.
2004 static bool isValidOperands(const Value *V1, const Value *V2,
2005 const Value *Mask);
2006
2007 /// Overload to return most specific vector type.
2008 ///
2009 VectorType *getType() const {
2010 return cast<VectorType>(Instruction::getType());
2011 }
2012
2013 /// Transparently provide more efficient getOperand methods.
2014 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
;
2015
2016 Constant *getMask() const {
2017 return cast<Constant>(getOperand(2));
2018 }
2019
2020 /// Return the shuffle mask value for the specified element of the mask.
2021 /// Return -1 if the element is undef.
2022 static int getMaskValue(const Constant *Mask, unsigned Elt);
2023
2024 /// Return the shuffle mask value of this instruction for the given element
2025 /// index. Return -1 if the element is undef.
2026 int getMaskValue(unsigned Elt) const {
2027 return getMaskValue(getMask(), Elt);
2028 }
2029
2030 /// Convert the input shuffle mask operand to a vector of integers. Undefined
2031 /// elements of the mask are returned as -1.
2032 static void getShuffleMask(const Constant *Mask,
2033 SmallVectorImpl<int> &Result);
2034
2035 /// Return the mask for this instruction as a vector of integers. Undefined
2036 /// elements of the mask are returned as -1.
2037 void getShuffleMask(SmallVectorImpl<int> &Result) const {
2038 return getShuffleMask(getMask(), Result);
2039 }
2040
2041 SmallVector<int, 16> getShuffleMask() const {
2042 SmallVector<int, 16> Mask;
2043 getShuffleMask(Mask);
2044 return Mask;
2045 }
2046
2047 /// Return true if this shuffle returns a vector with a different number of
2048 /// elements than its source vectors.
2049 /// Examples: shufflevector <4 x n> A, <4 x n> B, <1,2,3>
2050 /// shufflevector <4 x n> A, <4 x n> B, <1,2,3,4,5>
2051 bool changesLength() const {
2052 unsigned NumSourceElts = Op<0>()->getType()->getVectorNumElements();
2053 unsigned NumMaskElts = getMask()->getType()->getVectorNumElements();
2054 return NumSourceElts != NumMaskElts;
2055 }
2056
2057 /// Return true if this shuffle returns a vector with a greater number of
2058 /// elements than its source vectors.
2059 /// Example: shufflevector <2 x n> A, <2 x n> B, <1,2,3>
2060 bool increasesLength() const {
2061 unsigned NumSourceElts = Op<0>()->getType()->getVectorNumElements();
2062 unsigned NumMaskElts = getMask()->getType()->getVectorNumElements();
2063 return NumSourceElts < NumMaskElts;
2064 }
2065
2066 /// Return true if this shuffle mask chooses elements from exactly one source
2067 /// vector.
2068 /// Example: <7,5,undef,7>
2069 /// This assumes that vector operands are the same length as the mask.
2070 static bool isSingleSourceMask(ArrayRef<int> Mask);
2071 static bool isSingleSourceMask(const Constant *Mask) {
2072 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-10~svn374877/include/llvm/IR/Instructions.h"
, 2072, __PRETTY_FUNCTION__))
;
2073 SmallVector<int, 16> MaskAsInts;
2074 getShuffleMask(Mask, MaskAsInts);
2075 return isSingleSourceMask(MaskAsInts);
2076 }
2077
2078 /// Return true if this shuffle chooses elements from exactly one source
2079 /// vector without changing the length of that vector.
2080 /// Example: shufflevector <4 x n> A, <4 x n> B, <3,0,undef,3>
2081 /// TODO: Optionally allow length-changing shuffles.
2082 bool isSingleSource() const {
2083 return !changesLength() && isSingleSourceMask(getMask());
2084 }
2085
2086 /// Return true if this shuffle mask chooses elements from exactly one source
2087 /// vector without lane crossings. A shuffle using this mask is not
2088 /// necessarily a no-op because it may change the number of elements from its
2089 /// input vectors or it may provide demanded bits knowledge via undef lanes.
2090 /// Example: <undef,undef,2,3>
2091 static bool isIdentityMask(ArrayRef<int> Mask);
2092 static bool isIdentityMask(const Constant *Mask) {
2093 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-10~svn374877/include/llvm/IR/Instructions.h"
, 2093, __PRETTY_FUNCTION__))
;
2094 SmallVector<int, 16> MaskAsInts;
2095 getShuffleMask(Mask, MaskAsInts);
2096 return isIdentityMask(MaskAsInts);
2097 }
2098
2099 /// Return true if this shuffle chooses elements from exactly one source
2100 /// vector without lane crossings and does not change the number of elements
2101 /// from its input vectors.
2102 /// Example: shufflevector <4 x n> A, <4 x n> B, <4,undef,6,undef>
2103 bool isIdentity() const {
2104 return !changesLength() && isIdentityMask(getShuffleMask());
2105 }
2106
2107 /// Return true if this shuffle lengthens exactly one source vector with
2108 /// undefs in the high elements.
2109 bool isIdentityWithPadding() const;
2110
2111 /// Return true if this shuffle extracts the first N elements of exactly one
2112 /// source vector.
2113 bool isIdentityWithExtract() const;
2114
2115 /// Return true if this shuffle concatenates its 2 source vectors. This
2116 /// returns false if either input is undefined. In that case, the shuffle is
2117 /// is better classified as an identity with padding operation.
2118 bool isConcat() const;
2119
2120 /// Return true if this shuffle mask chooses elements from its source vectors
2121 /// without lane crossings. A shuffle using this mask would be
2122 /// equivalent to a vector select with a constant condition operand.
2123 /// Example: <4,1,6,undef>
2124 /// This returns false if the mask does not choose from both input vectors.
2125 /// In that case, the shuffle is better classified as an identity shuffle.
2126 /// This assumes that vector operands are the same length as the mask
2127 /// (a length-changing shuffle can never be equivalent to a vector select).
2128 static bool isSelectMask(ArrayRef<int> Mask);
2129 static bool isSelectMask(const Constant *Mask) {
2130 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-10~svn374877/include/llvm/IR/Instructions.h"
, 2130, __PRETTY_FUNCTION__))
;
2131 SmallVector<int, 16> MaskAsInts;
2132 getShuffleMask(Mask, MaskAsInts);
2133 return isSelectMask(MaskAsInts);
2134 }
2135
2136 /// Return true if this shuffle chooses elements from its source vectors
2137 /// without lane crossings and all operands have the same number of elements.
2138 /// In other words, this shuffle is equivalent to a vector select with a
2139 /// constant condition operand.
2140 /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,1,6,3>
2141 /// This returns false if the mask does not choose from both input vectors.
2142 /// In that case, the shuffle is better classified as an identity shuffle.
2143 /// TODO: Optionally allow length-changing shuffles.
2144 bool isSelect() const {
2145 return !changesLength() && isSelectMask(getMask());
2146 }
2147
2148 /// Return true if this shuffle mask swaps the order of elements from exactly
2149 /// one source vector.
2150 /// Example: <7,6,undef,4>
2151 /// This assumes that vector operands are the same length as the mask.
2152 static bool isReverseMask(ArrayRef<int> Mask);
2153 static bool isReverseMask(const Constant *Mask) {
2154 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-10~svn374877/include/llvm/IR/Instructions.h"
, 2154, __PRETTY_FUNCTION__))
;
2155 SmallVector<int, 16> MaskAsInts;
2156 getShuffleMask(Mask, MaskAsInts);
2157 return isReverseMask(MaskAsInts);
2158 }
2159
2160 /// Return true if this shuffle swaps the order of elements from exactly
2161 /// one source vector.
2162 /// Example: shufflevector <4 x n> A, <4 x n> B, <3,undef,1,undef>
2163 /// TODO: Optionally allow length-changing shuffles.
2164 bool isReverse() const {
2165 return !changesLength() && isReverseMask(getMask());
2166 }
2167
2168 /// Return true if this shuffle mask chooses all elements with the same value
2169 /// as the first element of exactly one source vector.
2170 /// Example: <4,undef,undef,4>
2171 /// This assumes that vector operands are the same length as the mask.
2172 static bool isZeroEltSplatMask(ArrayRef<int> Mask);
2173 static bool isZeroEltSplatMask(const Constant *Mask) {
2174 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-10~svn374877/include/llvm/IR/Instructions.h"
, 2174, __PRETTY_FUNCTION__))
;
2175 SmallVector<int, 16> MaskAsInts;
2176 getShuffleMask(Mask, MaskAsInts);
2177 return isZeroEltSplatMask(MaskAsInts);
2178 }
2179
2180 /// Return true if all elements of this shuffle are the same value as the
2181 /// first element of exactly one source vector without changing the length
2182 /// of that vector.
2183 /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,0,undef,0>
2184 /// TODO: Optionally allow length-changing shuffles.
2185 /// TODO: Optionally allow splats from other elements.
2186 bool isZeroEltSplat() const {
2187 return !changesLength() && isZeroEltSplatMask(getMask());
2188 }
2189
2190 /// Return true if this shuffle mask is a transpose mask.
2191 /// Transpose vector masks transpose a 2xn matrix. They read corresponding
2192 /// even- or odd-numbered vector elements from two n-dimensional source
2193 /// vectors and write each result into consecutive elements of an
2194 /// n-dimensional destination vector. Two shuffles are necessary to complete
2195 /// the transpose, one for the even elements and another for the odd elements.
2196 /// This description closely follows how the TRN1 and TRN2 AArch64
2197 /// instructions operate.
2198 ///
2199 /// For example, a simple 2x2 matrix can be transposed with:
2200 ///
2201 /// ; Original matrix
2202 /// m0 = < a, b >
2203 /// m1 = < c, d >
2204 ///
2205 /// ; Transposed matrix
2206 /// t0 = < a, c > = shufflevector m0, m1, < 0, 2 >
2207 /// t1 = < b, d > = shufflevector m0, m1, < 1, 3 >
2208 ///
2209 /// For matrices having greater than n columns, the resulting nx2 transposed
2210 /// matrix is stored in two result vectors such that one vector contains
2211 /// interleaved elements from all the even-numbered rows and the other vector
2212 /// contains interleaved elements from all the odd-numbered rows. For example,
2213 /// a 2x4 matrix can be transposed with:
2214 ///
2215 /// ; Original matrix
2216 /// m0 = < a, b, c, d >
2217 /// m1 = < e, f, g, h >
2218 ///
2219 /// ; Transposed matrix
2220 /// t0 = < a, e, c, g > = shufflevector m0, m1 < 0, 4, 2, 6 >
2221 /// t1 = < b, f, d, h > = shufflevector m0, m1 < 1, 5, 3, 7 >
2222 static bool isTransposeMask(ArrayRef<int> Mask);
2223 static bool isTransposeMask(const Constant *Mask) {
2224 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-10~svn374877/include/llvm/IR/Instructions.h"
, 2224, __PRETTY_FUNCTION__))
;
2225 SmallVector<int, 16> MaskAsInts;
2226 getShuffleMask(Mask, MaskAsInts);
2227 return isTransposeMask(MaskAsInts);
2228 }
2229
2230 /// Return true if this shuffle transposes the elements of its inputs without
2231 /// changing the length of the vectors. This operation may also be known as a
2232 /// merge or interleave. See the description for isTransposeMask() for the
2233 /// exact specification.
2234 /// Example: shufflevector <4 x n> A, <4 x n> B, <0,4,2,6>
2235 bool isTranspose() const {
2236 return !changesLength() && isTransposeMask(getMask());
2237 }
2238
2239 /// Return true if this shuffle mask is an extract subvector mask.
2240 /// A valid extract subvector mask returns a smaller vector from a single
2241 /// source operand. The base extraction index is returned as well.
2242 static bool isExtractSubvectorMask(ArrayRef<int> Mask, int NumSrcElts,
2243 int &Index);
2244 static bool isExtractSubvectorMask(const Constant *Mask, int NumSrcElts,
2245 int &Index) {
2246 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-10~svn374877/include/llvm/IR/Instructions.h"
, 2246, __PRETTY_FUNCTION__))
;
2247 SmallVector<int, 16> MaskAsInts;
2248 getShuffleMask(Mask, MaskAsInts);
2249 return isExtractSubvectorMask(MaskAsInts, NumSrcElts, Index);
2250 }
2251
2252 /// Return true if this shuffle mask is an extract subvector mask.
2253 bool isExtractSubvectorMask(int &Index) const {
2254 int NumSrcElts = Op<0>()->getType()->getVectorNumElements();
2255 return isExtractSubvectorMask(getMask(), NumSrcElts, Index);
2256 }
2257
2258 /// Change values in a shuffle permute mask assuming the two vector operands
2259 /// of length InVecNumElts have swapped position.
2260 static void commuteShuffleMask(MutableArrayRef<int> Mask,
2261 unsigned InVecNumElts) {
2262 for (int &Idx : Mask) {
2263 if (Idx == -1)
2264 continue;
2265 Idx = Idx < (int)InVecNumElts ? Idx + InVecNumElts : Idx - InVecNumElts;
2266 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-10~svn374877/include/llvm/IR/Instructions.h"
, 2267, __PRETTY_FUNCTION__))
2267 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 2267, __PRETTY_FUNCTION__))
;
2268 }
2269 }
2270
2271 // Methods for support type inquiry through isa, cast, and dyn_cast:
2272 static bool classof(const Instruction *I) {
2273 return I->getOpcode() == Instruction::ShuffleVector;
2274 }
2275 static bool classof(const Value *V) {
2276 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2277 }
2278};
2279
2280template <>
2281struct OperandTraits<ShuffleVectorInst> :
2282 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
2283};
2284
2285DEFINE_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-10~svn374877/include/llvm/IR/Instructions.h"
, 2285, __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-10~svn374877/include/llvm/IR/Instructions.h"
, 2285, __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); }
2286
2287//===----------------------------------------------------------------------===//
2288// ExtractValueInst Class
2289//===----------------------------------------------------------------------===//
2290
2291/// This instruction extracts a struct member or array
2292/// element value from an aggregate value.
2293///
2294class ExtractValueInst : public UnaryInstruction {
2295 SmallVector<unsigned, 4> Indices;
2296
2297 ExtractValueInst(const ExtractValueInst &EVI);
2298
2299 /// Constructors - Create a extractvalue instruction with a base aggregate
2300 /// value and a list of indices. The first ctor can optionally insert before
2301 /// an existing instruction, the second appends the new instruction to the
2302 /// specified BasicBlock.
2303 inline ExtractValueInst(Value *Agg,
2304 ArrayRef<unsigned> Idxs,
2305 const Twine &NameStr,
2306 Instruction *InsertBefore);
2307 inline ExtractValueInst(Value *Agg,
2308 ArrayRef<unsigned> Idxs,
2309 const Twine &NameStr, BasicBlock *InsertAtEnd);
2310
2311 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
2312
2313protected:
2314 // Note: Instruction needs to be a friend here to call cloneImpl.
2315 friend class Instruction;
2316
2317 ExtractValueInst *cloneImpl() const;
2318
2319public:
2320 static ExtractValueInst *Create(Value *Agg,
2321 ArrayRef<unsigned> Idxs,
2322 const Twine &NameStr = "",
2323 Instruction *InsertBefore = nullptr) {
2324 return new
2325 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
2326 }
2327
2328 static ExtractValueInst *Create(Value *Agg,
2329 ArrayRef<unsigned> Idxs,
2330 const Twine &NameStr,
2331 BasicBlock *InsertAtEnd) {
2332 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
2333 }
2334
2335 /// Returns the type of the element that would be extracted
2336 /// with an extractvalue instruction with the specified parameters.
2337 ///
2338 /// Null is returned if the indices are invalid for the specified type.
2339 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
2340
2341 using idx_iterator = const unsigned*;
2342
2343 inline idx_iterator idx_begin() const { return Indices.begin(); }
2344 inline idx_iterator idx_end() const { return Indices.end(); }
2345 inline iterator_range<idx_iterator> indices() const {
2346 return make_range(idx_begin(), idx_end());
2347 }
2348
2349 Value *getAggregateOperand() {
2350 return getOperand(0);
2351 }
2352 const Value *getAggregateOperand() const {
2353 return getOperand(0);
2354 }
2355 static unsigned getAggregateOperandIndex() {
2356 return 0U; // get index for modifying correct operand
2357 }
2358
2359 ArrayRef<unsigned> getIndices() const {
2360 return Indices;
2361 }
2362
2363 unsigned getNumIndices() const {
2364 return (unsigned)Indices.size();
2365 }
2366
2367 bool hasIndices() const {
2368 return true;
2369 }
2370
2371 // Methods for support type inquiry through isa, cast, and dyn_cast:
2372 static bool classof(const Instruction *I) {
2373 return I->getOpcode() == Instruction::ExtractValue;
2374 }
2375 static bool classof(const Value *V) {
2376 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2377 }
2378};
2379
2380ExtractValueInst::ExtractValueInst(Value *Agg,
2381 ArrayRef<unsigned> Idxs,
2382 const Twine &NameStr,
2383 Instruction *InsertBefore)
2384 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2385 ExtractValue, Agg, InsertBefore) {
2386 init(Idxs, NameStr);
2387}
2388
2389ExtractValueInst::ExtractValueInst(Value *Agg,
2390 ArrayRef<unsigned> Idxs,
2391 const Twine &NameStr,
2392 BasicBlock *InsertAtEnd)
2393 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2394 ExtractValue, Agg, InsertAtEnd) {
2395 init(Idxs, NameStr);
2396}
2397
2398//===----------------------------------------------------------------------===//
2399// InsertValueInst Class
2400//===----------------------------------------------------------------------===//
2401
2402/// This instruction inserts a struct field of array element
2403/// value into an aggregate value.
2404///
2405class InsertValueInst : public Instruction {
2406 SmallVector<unsigned, 4> Indices;
2407
2408 InsertValueInst(const InsertValueInst &IVI);
2409
2410 /// Constructors - Create a insertvalue instruction with a base aggregate
2411 /// value, a value to insert, and a list of indices. The first ctor can
2412 /// optionally insert before an existing instruction, the second appends
2413 /// the new instruction to the specified BasicBlock.
2414 inline InsertValueInst(Value *Agg, Value *Val,
2415 ArrayRef<unsigned> Idxs,
2416 const Twine &NameStr,
2417 Instruction *InsertBefore);
2418 inline InsertValueInst(Value *Agg, Value *Val,
2419 ArrayRef<unsigned> Idxs,
2420 const Twine &NameStr, BasicBlock *InsertAtEnd);
2421
2422 /// Constructors - These two constructors are convenience methods because one
2423 /// and two index insertvalue instructions are so common.
2424 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
2425 const Twine &NameStr = "",
2426 Instruction *InsertBefore = nullptr);
2427 InsertValueInst(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr,
2428 BasicBlock *InsertAtEnd);
2429
2430 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2431 const Twine &NameStr);
2432
2433protected:
2434 // Note: Instruction needs to be a friend here to call cloneImpl.
2435 friend class Instruction;
2436
2437 InsertValueInst *cloneImpl() const;
2438
2439public:
2440 // allocate space for exactly two operands
2441 void *operator new(size_t s) {
2442 return User::operator new(s, 2);
2443 }
2444
2445 static InsertValueInst *Create(Value *Agg, Value *Val,
2446 ArrayRef<unsigned> Idxs,
2447 const Twine &NameStr = "",
2448 Instruction *InsertBefore = nullptr) {
2449 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
2450 }
2451
2452 static InsertValueInst *Create(Value *Agg, Value *Val,
2453 ArrayRef<unsigned> Idxs,
2454 const Twine &NameStr,
2455 BasicBlock *InsertAtEnd) {
2456 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
2457 }
2458
2459 /// Transparently provide more efficient getOperand methods.
2460 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
;
2461
2462 using idx_iterator = const unsigned*;
2463
2464 inline idx_iterator idx_begin() const { return Indices.begin(); }
2465 inline idx_iterator idx_end() const { return Indices.end(); }
2466 inline iterator_range<idx_iterator> indices() const {
2467 return make_range(idx_begin(), idx_end());
2468 }
2469
2470 Value *getAggregateOperand() {
2471 return getOperand(0);
2472 }
2473 const Value *getAggregateOperand() const {
2474 return getOperand(0);
2475 }
2476 static unsigned getAggregateOperandIndex() {
2477 return 0U; // get index for modifying correct operand
2478 }
2479
2480 Value *getInsertedValueOperand() {
2481 return getOperand(1);
2482 }
2483 const Value *getInsertedValueOperand() const {
2484 return getOperand(1);
2485 }
2486 static unsigned getInsertedValueOperandIndex() {
2487 return 1U; // get index for modifying correct operand
2488 }
2489
2490 ArrayRef<unsigned> getIndices() const {
2491 return Indices;
2492 }
2493
2494 unsigned getNumIndices() const {
2495 return (unsigned)Indices.size();
2496 }
2497
2498 bool hasIndices() const {
2499 return true;
2500 }
2501
2502 // Methods for support type inquiry through isa, cast, and dyn_cast:
2503 static bool classof(const Instruction *I) {
2504 return I->getOpcode() == Instruction::InsertValue;
2505 }
2506 static bool classof(const Value *V) {
2507 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2508 }
2509};
2510
2511template <>
2512struct OperandTraits<InsertValueInst> :
2513 public FixedNumOperandTraits<InsertValueInst, 2> {
2514};
2515
2516InsertValueInst::InsertValueInst(Value *Agg,
2517 Value *Val,
2518 ArrayRef<unsigned> Idxs,
2519 const Twine &NameStr,
2520 Instruction *InsertBefore)
2521 : Instruction(Agg->getType(), InsertValue,
2522 OperandTraits<InsertValueInst>::op_begin(this),
2523 2, InsertBefore) {
2524 init(Agg, Val, Idxs, NameStr);
2525}
2526
2527InsertValueInst::InsertValueInst(Value *Agg,
2528 Value *Val,
2529 ArrayRef<unsigned> Idxs,
2530 const Twine &NameStr,
2531 BasicBlock *InsertAtEnd)
2532 : Instruction(Agg->getType(), InsertValue,
2533 OperandTraits<InsertValueInst>::op_begin(this),
2534 2, InsertAtEnd) {
2535 init(Agg, Val, Idxs, NameStr);
2536}
2537
2538DEFINE_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-10~svn374877/include/llvm/IR/Instructions.h"
, 2538, __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-10~svn374877/include/llvm/IR/Instructions.h"
, 2538, __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); }
2539
2540//===----------------------------------------------------------------------===//
2541// PHINode Class
2542//===----------------------------------------------------------------------===//
2543
2544// PHINode - The PHINode class is used to represent the magical mystical PHI
2545// node, that can not exist in nature, but can be synthesized in a computer
2546// scientist's overactive imagination.
2547//
2548class PHINode : public Instruction {
2549 /// The number of operands actually allocated. NumOperands is
2550 /// the number actually in use.
2551 unsigned ReservedSpace;
2552
2553 PHINode(const PHINode &PN);
2554
2555 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2556 const Twine &NameStr = "",
2557 Instruction *InsertBefore = nullptr)
2558 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2559 ReservedSpace(NumReservedValues) {
2560 setName(NameStr);
2561 allocHungoffUses(ReservedSpace);
2562 }
2563
2564 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2565 BasicBlock *InsertAtEnd)
2566 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2567 ReservedSpace(NumReservedValues) {
2568 setName(NameStr);
2569 allocHungoffUses(ReservedSpace);
2570 }
2571
2572protected:
2573 // Note: Instruction needs to be a friend here to call cloneImpl.
2574 friend class Instruction;
2575
2576 PHINode *cloneImpl() const;
2577
2578 // allocHungoffUses - this is more complicated than the generic
2579 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2580 // values and pointers to the incoming blocks, all in one allocation.
2581 void allocHungoffUses(unsigned N) {
2582 User::allocHungoffUses(N, /* IsPhi */ true);
2583 }
2584
2585public:
2586 /// Constructors - NumReservedValues is a hint for the number of incoming
2587 /// edges that this phi node will have (use 0 if you really have no idea).
2588 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2589 const Twine &NameStr = "",
2590 Instruction *InsertBefore = nullptr) {
2591 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2592 }
2593
2594 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2595 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2596 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2597 }
2598
2599 /// Provide fast operand accessors
2600 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
;
2601
2602 // Block iterator interface. This provides access to the list of incoming
2603 // basic blocks, which parallels the list of incoming values.
2604
2605 using block_iterator = BasicBlock **;
2606 using const_block_iterator = BasicBlock * const *;
2607
2608 block_iterator block_begin() {
2609 Use::UserRef *ref =
2610 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2611 return reinterpret_cast<block_iterator>(ref + 1);
2612 }
2613
2614 const_block_iterator block_begin() const {
2615 const Use::UserRef *ref =
2616 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2617 return reinterpret_cast<const_block_iterator>(ref + 1);
2618 }
2619
2620 block_iterator block_end() {
2621 return block_begin() + getNumOperands();
2622 }
2623
2624 const_block_iterator block_end() const {
2625 return block_begin() + getNumOperands();
2626 }
2627
2628 iterator_range<block_iterator> blocks() {
2629 return make_range(block_begin(), block_end());
2630 }
2631
2632 iterator_range<const_block_iterator> blocks() const {
2633 return make_range(block_begin(), block_end());
2634 }
2635
2636 op_range incoming_values() { return operands(); }
2637
2638 const_op_range incoming_values() const { return operands(); }
2639
2640 /// Return the number of incoming edges
2641 ///
2642 unsigned getNumIncomingValues() const { return getNumOperands(); }
2643
2644 /// Return incoming value number x
2645 ///
2646 Value *getIncomingValue(unsigned i) const {
2647 return getOperand(i);
2648 }
2649 void setIncomingValue(unsigned i, Value *V) {
2650 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-10~svn374877/include/llvm/IR/Instructions.h"
, 2650, __PRETTY_FUNCTION__))
;
2651 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-10~svn374877/include/llvm/IR/Instructions.h"
, 2652, __PRETTY_FUNCTION__))
2652 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 2652, __PRETTY_FUNCTION__))
;
2653 setOperand(i, V);
2654 }
2655
2656 static unsigned getOperandNumForIncomingValue(unsigned i) {
2657 return i;
2658 }
2659
2660 static unsigned getIncomingValueNumForOperand(unsigned i) {
2661 return i;
2662 }
2663
2664 /// Return incoming basic block number @p i.
2665 ///
2666 BasicBlock *getIncomingBlock(unsigned i) const {
2667 return block_begin()[i];
2668 }
2669
2670 /// Return incoming basic block corresponding
2671 /// to an operand of the PHI.
2672 ///
2673 BasicBlock *getIncomingBlock(const Use &U) const {
2674 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-10~svn374877/include/llvm/IR/Instructions.h"
, 2674, __PRETTY_FUNCTION__))
;
2675 return getIncomingBlock(unsigned(&U - op_begin()));
2676 }
2677
2678 /// Return incoming basic block corresponding
2679 /// to value use iterator.
2680 ///
2681 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2682 return getIncomingBlock(I.getUse());
2683 }
2684
2685 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2686 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-10~svn374877/include/llvm/IR/Instructions.h"
, 2686, __PRETTY_FUNCTION__))
;
2687 block_begin()[i] = BB;
2688 }
2689
2690 /// Replace every incoming basic block \p Old to basic block \p New.
2691 void replaceIncomingBlockWith(const BasicBlock *Old, BasicBlock *New) {
2692 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-10~svn374877/include/llvm/IR/Instructions.h"
, 2692, __PRETTY_FUNCTION__))
;
2693 for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op)
2694 if (getIncomingBlock(Op) == Old)
2695 setIncomingBlock(Op, New);
2696 }
2697
2698 /// Add an incoming value to the end of the PHI list
2699 ///
2700 void addIncoming(Value *V, BasicBlock *BB) {
2701 if (getNumOperands() == ReservedSpace)
2702 growOperands(); // Get more space!
2703 // Initialize some new operands.
2704 setNumHungOffUseOperands(getNumOperands() + 1);
2705 setIncomingValue(getNumOperands() - 1, V);
2706 setIncomingBlock(getNumOperands() - 1, BB);
2707 }
2708
2709 /// Remove an incoming value. This is useful if a
2710 /// predecessor basic block is deleted. The value removed is returned.
2711 ///
2712 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2713 /// is true), the PHI node is destroyed and any uses of it are replaced with
2714 /// dummy values. The only time there should be zero incoming values to a PHI
2715 /// node is when the block is dead, so this strategy is sound.
2716 ///
2717 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2718
2719 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2720 int Idx = getBasicBlockIndex(BB);
2721 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-10~svn374877/include/llvm/IR/Instructions.h"
, 2721, __PRETTY_FUNCTION__))
;
2722 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2723 }
2724
2725 /// Return the first index of the specified basic
2726 /// block in the value list for this PHI. Returns -1 if no instance.
2727 ///
2728 int getBasicBlockIndex(const BasicBlock *BB) const {
2729 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2730 if (block_begin()[i] == BB)
2731 return i;
2732 return -1;
2733 }
2734
2735 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2736 int Idx = getBasicBlockIndex(BB);
2737 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-10~svn374877/include/llvm/IR/Instructions.h"
, 2737, __PRETTY_FUNCTION__))
;
2738 return getIncomingValue(Idx);
2739 }
2740
2741 /// Set every incoming value(s) for block \p BB to \p V.
2742 void setIncomingValueForBlock(const BasicBlock *BB, Value *V) {
2743 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-10~svn374877/include/llvm/IR/Instructions.h"
, 2743, __PRETTY_FUNCTION__))
;
2744 bool Found = false;
2745 for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op)
2746 if (getIncomingBlock(Op) == BB) {
2747 Found = true;
2748 setIncomingValue(Op, V);
2749 }
2750 (void)Found;
2751 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-10~svn374877/include/llvm/IR/Instructions.h"
, 2751, __PRETTY_FUNCTION__))
;
2752 }
2753
2754 /// If the specified PHI node always merges together the
2755 /// same value, return the value, otherwise return null.
2756 Value *hasConstantValue() const;
2757
2758 /// Whether the specified PHI node always merges
2759 /// together the same value, assuming undefs are equal to a unique
2760 /// non-undef value.
2761 bool hasConstantOrUndefValue() const;
2762
2763 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2764 static bool classof(const Instruction *I) {
2765 return I->getOpcode() == Instruction::PHI;
2766 }
2767 static bool classof(const Value *V) {
2768 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2769 }
2770
2771private:
2772 void growOperands();
2773};
2774
2775template <>
2776struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2777};
2778
2779DEFINE_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-10~svn374877/include/llvm/IR/Instructions.h"
, 2779, __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-10~svn374877/include/llvm/IR/Instructions.h"
, 2779, __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
); }
2780
2781//===----------------------------------------------------------------------===//
2782// LandingPadInst Class
2783//===----------------------------------------------------------------------===//
2784
2785//===---------------------------------------------------------------------------
2786/// The landingpad instruction holds all of the information
2787/// necessary to generate correct exception handling. The landingpad instruction
2788/// cannot be moved from the top of a landing pad block, which itself is
2789/// accessible only from the 'unwind' edge of an invoke. This uses the
2790/// SubclassData field in Value to store whether or not the landingpad is a
2791/// cleanup.
2792///
2793class LandingPadInst : public Instruction {
2794 /// The number of operands actually allocated. NumOperands is
2795 /// the number actually in use.
2796 unsigned ReservedSpace;
2797
2798 LandingPadInst(const LandingPadInst &LP);
2799
2800public:
2801 enum ClauseType { Catch, Filter };
2802
2803private:
2804 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
2805 const Twine &NameStr, Instruction *InsertBefore);
2806 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
2807 const Twine &NameStr, BasicBlock *InsertAtEnd);
2808
2809 // Allocate space for exactly zero operands.
2810 void *operator new(size_t s) {
2811 return User::operator new(s);
2812 }
2813
2814 void growOperands(unsigned Size);
2815 void init(unsigned NumReservedValues, const Twine &NameStr);
2816
2817protected:
2818 // Note: Instruction needs to be a friend here to call cloneImpl.
2819 friend class Instruction;
2820
2821 LandingPadInst *cloneImpl() const;
2822
2823public:
2824 /// Constructors - NumReservedClauses is a hint for the number of incoming
2825 /// clauses that this landingpad will have (use 0 if you really have no idea).
2826 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
2827 const Twine &NameStr = "",
2828 Instruction *InsertBefore = nullptr);
2829 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
2830 const Twine &NameStr, BasicBlock *InsertAtEnd);
2831
2832 /// Provide fast operand accessors
2833 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
;
2834
2835 /// Return 'true' if this landingpad instruction is a
2836 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2837 /// doesn't catch the exception.
2838 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2839
2840 /// Indicate that this landingpad instruction is a cleanup.
2841 void setCleanup(bool V) {
2842 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2843 (V ? 1 : 0));
2844 }
2845
2846 /// Add a catch or filter clause to the landing pad.
2847 void addClause(Constant *ClauseVal);
2848
2849 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
2850 /// determine what type of clause this is.
2851 Constant *getClause(unsigned Idx) const {
2852 return cast<Constant>(getOperandList()[Idx]);
2853 }
2854
2855 /// Return 'true' if the clause and index Idx is a catch clause.
2856 bool isCatch(unsigned Idx) const {
2857 return !isa<ArrayType>(getOperandList()[Idx]->getType());
2858 }
2859
2860 /// Return 'true' if the clause and index Idx is a filter clause.
2861 bool isFilter(unsigned Idx) const {
2862 return isa<ArrayType>(getOperandList()[Idx]->getType());
2863 }
2864
2865 /// Get the number of clauses for this landing pad.
2866 unsigned getNumClauses() const { return getNumOperands(); }
2867
2868 /// Grow the size of the operand list to accommodate the new
2869 /// number of clauses.
2870 void reserveClauses(unsigned Size) { growOperands(Size); }
2871
2872 // Methods for support type inquiry through isa, cast, and dyn_cast:
2873 static bool classof(const Instruction *I) {
2874 return I->getOpcode() == Instruction::LandingPad;
2875 }
2876 static bool classof(const Value *V) {
2877 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2878 }
2879};
2880
2881template <>
2882struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<1> {
2883};
2884
2885DEFINE_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-10~svn374877/include/llvm/IR/Instructions.h"
, 2885, __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-10~svn374877/include/llvm/IR/Instructions.h"
, 2885, __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); }
2886
2887//===----------------------------------------------------------------------===//
2888// ReturnInst Class
2889//===----------------------------------------------------------------------===//
2890
2891//===---------------------------------------------------------------------------
2892/// Return a value (possibly void), from a function. Execution
2893/// does not continue in this function any longer.
2894///
2895class ReturnInst : public Instruction {
2896 ReturnInst(const ReturnInst &RI);
2897
2898private:
2899 // ReturnInst constructors:
2900 // ReturnInst() - 'ret void' instruction
2901 // ReturnInst( null) - 'ret void' instruction
2902 // ReturnInst(Value* X) - 'ret X' instruction
2903 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2904 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2905 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2906 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2907 //
2908 // NOTE: If the Value* passed is of type void then the constructor behaves as
2909 // if it was passed NULL.
2910 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
2911 Instruction *InsertBefore = nullptr);
2912 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2913 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2914
2915protected:
2916 // Note: Instruction needs to be a friend here to call cloneImpl.
2917 friend class Instruction;
2918
2919 ReturnInst *cloneImpl() const;
2920
2921public:
2922 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
2923 Instruction *InsertBefore = nullptr) {
2924 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2925 }
2926
2927 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2928 BasicBlock *InsertAtEnd) {
2929 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2930 }
2931
2932 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2933 return new(0) ReturnInst(C, InsertAtEnd);
2934 }
2935
2936 /// Provide fast operand accessors
2937 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
;
2938
2939 /// Convenience accessor. Returns null if there is no return value.
2940 Value *getReturnValue() const {
2941 return getNumOperands() != 0 ? getOperand(0) : nullptr;
2942 }
2943
2944 unsigned getNumSuccessors() const { return 0; }
2945
2946 // Methods for support type inquiry through isa, cast, and dyn_cast:
2947 static bool classof(const Instruction *I) {
2948 return (I->getOpcode() == Instruction::Ret);
2949 }
2950 static bool classof(const Value *V) {
2951 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2952 }
2953
2954private:
2955 BasicBlock *getSuccessor(unsigned idx) const {
2956 llvm_unreachable("ReturnInst has no successors!")::llvm::llvm_unreachable_internal("ReturnInst has no successors!"
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 2956)
;
2957 }
2958
2959 void setSuccessor(unsigned idx, BasicBlock *B) {
2960 llvm_unreachable("ReturnInst has no successors!")::llvm::llvm_unreachable_internal("ReturnInst has no successors!"
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 2960)
;
2961 }
2962};
2963
2964template <>
2965struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2966};
2967
2968DEFINE_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-10~svn374877/include/llvm/IR/Instructions.h"
, 2968, __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-10~svn374877/include/llvm/IR/Instructions.h"
, 2968, __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); }
2969
2970//===----------------------------------------------------------------------===//
2971// BranchInst Class
2972//===----------------------------------------------------------------------===//
2973
2974//===---------------------------------------------------------------------------
2975/// Conditional or Unconditional Branch instruction.
2976///
2977class BranchInst : public Instruction {
2978 /// Ops list - Branches are strange. The operands are ordered:
2979 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2980 /// they don't have to check for cond/uncond branchness. These are mostly
2981 /// accessed relative from op_end().
2982 BranchInst(const BranchInst &BI);
2983 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2984 // BranchInst(BB *B) - 'br B'
2985 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2986 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2987 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2988 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2989 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2990 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
2991 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2992 Instruction *InsertBefore = nullptr);
2993 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2994 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2995 BasicBlock *InsertAtEnd);
2996
2997 void AssertOK();
2998
2999protected:
3000 // Note: Instruction needs to be a friend here to call cloneImpl.
3001 friend class Instruction;
3002
3003 BranchInst *cloneImpl() const;
3004
3005public:
3006 /// Iterator type that casts an operand to a basic block.
3007 ///
3008 /// This only makes sense because the successors are stored as adjacent
3009 /// operands for branch instructions.
3010 struct succ_op_iterator
3011 : iterator_adaptor_base<succ_op_iterator, value_op_iterator,
3012 std::random_access_iterator_tag, BasicBlock *,
3013 ptrdiff_t, BasicBlock *, BasicBlock *> {
3014 explicit succ_op_iterator(value_op_iterator I) : iterator_adaptor_base(I) {}
3015
3016 BasicBlock *operator*() const { return cast<BasicBlock>(*I); }
3017 BasicBlock *operator->() const { return operator*(); }
3018 };
3019
3020 /// The const version of `succ_op_iterator`.
3021 struct const_succ_op_iterator
3022 : iterator_adaptor_base<const_succ_op_iterator, const_value_op_iterator,
3023 std::random_access_iterator_tag,
3024 const BasicBlock *, ptrdiff_t, const BasicBlock *,
3025 const BasicBlock *> {
3026 explicit const_succ_op_iterator(const_value_op_iterator I)
3027 : iterator_adaptor_base(I) {}
3028
3029 const BasicBlock *operator*() const { return cast<BasicBlock>(*I); }
3030 const BasicBlock *operator->() const { return operator*(); }
3031 };
3032
3033 static BranchInst *Create(BasicBlock *IfTrue,
3034 Instruction *InsertBefore = nullptr) {
3035 return new(1) BranchInst(IfTrue, InsertBefore);
3036 }
3037
3038 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
3039 Value *Cond, Instruction *InsertBefore = nullptr) {
3040 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
3041 }
3042
3043 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
3044 return new(1) BranchInst(IfTrue, InsertAtEnd);
3045 }
3046
3047 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
3048 Value *Cond, BasicBlock *InsertAtEnd) {
3049 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
3050 }
3051
3052 /// Transparently provide more efficient getOperand methods.
3053 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
;
3054
3055 bool isUnconditional() const { return getNumOperands() == 1; }
3056 bool isConditional() const { return getNumOperands() == 3; }
3057
3058 Value *getCondition() const {
3059 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-10~svn374877/include/llvm/IR/Instructions.h"
, 3059, __PRETTY_FUNCTION__))
;
3060 return Op<-3>();
3061 }
3062
3063 void setCondition(Value *V) {
3064 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-10~svn374877/include/llvm/IR/Instructions.h"
, 3064, __PRETTY_FUNCTION__))
;
3065 Op<-3>() = V;
3066 }
3067
3068 unsigned getNumSuccessors() const { return 1+isConditional(); }
3069
3070 BasicBlock *getSuccessor(unsigned i) const {
3071 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-10~svn374877/include/llvm/IR/Instructions.h"
, 3071, __PRETTY_FUNCTION__))
;
3072 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
3073 }
3074
3075 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3076 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-10~svn374877/include/llvm/IR/Instructions.h"
, 3076, __PRETTY_FUNCTION__))
;
3077 *(&Op<-1>() - idx) = NewSucc;
3078 }
3079
3080 /// Swap the successors of this branch instruction.
3081 ///
3082 /// Swaps the successors of the branch instruction. This also swaps any
3083 /// branch weight metadata associated with the instruction so that it
3084 /// continues to map correctly to each operand.
3085 void swapSuccessors();
3086
3087 iterator_range<succ_op_iterator> successors() {
3088 return make_range(
3089 succ_op_iterator(std::next(value_op_begin(), isConditional() ? 1 : 0)),
3090 succ_op_iterator(value_op_end()));
3091 }
3092
3093 iterator_range<const_succ_op_iterator> successors() const {
3094 return make_range(const_succ_op_iterator(
3095 std::next(value_op_begin(), isConditional() ? 1 : 0)),
3096 const_succ_op_iterator(value_op_end()));
3097 }
3098
3099 // Methods for support type inquiry through isa, cast, and dyn_cast:
3100 static bool classof(const Instruction *I) {
3101 return (I->getOpcode() == Instruction::Br);
3102 }
3103 static bool classof(const Value *V) {
3104 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3105 }
3106};
3107
3108template <>
3109struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
3110};
3111
3112DEFINE_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-10~svn374877/include/llvm/IR/Instructions.h"
, 3112, __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-10~svn374877/include/llvm/IR/Instructions.h"
, 3112, __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); }
3113
3114//===----------------------------------------------------------------------===//
3115// SwitchInst Class
3116//===----------------------------------------------------------------------===//
3117
3118//===---------------------------------------------------------------------------
3119/// Multiway switch
3120///
3121class SwitchInst : public Instruction {
3122 unsigned ReservedSpace;
3123
3124 // Operand[0] = Value to switch on
3125 // Operand[1] = Default basic block destination
3126 // Operand[2n ] = Value to match
3127 // Operand[2n+1] = BasicBlock to go to on match
3128 SwitchInst(const SwitchInst &SI);
3129
3130 /// Create a new switch instruction, specifying a value to switch on and a
3131 /// default destination. The number of additional cases can be specified here
3132 /// to make memory allocation more efficient. This constructor can also
3133 /// auto-insert before another instruction.
3134 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3135 Instruction *InsertBefore);
3136
3137 /// Create a new switch instruction, specifying a value to switch on and a
3138 /// default destination. The number of additional cases can be specified here
3139 /// to make memory allocation more efficient. This constructor also
3140 /// auto-inserts at the end of the specified BasicBlock.
3141 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3142 BasicBlock *InsertAtEnd);
3143
3144 // allocate space for exactly zero operands
3145 void *operator new(size_t s) {
3146 return User::operator new(s);
3147 }
3148
3149 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
3150 void growOperands();
3151
3152protected:
3153 // Note: Instruction needs to be a friend here to call cloneImpl.
3154 friend class Instruction;
3155
3156 SwitchInst *cloneImpl() const;
3157
3158public:
3159 // -2
3160 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
3161
3162 template <typename CaseHandleT> class CaseIteratorImpl;
3163
3164 /// A handle to a particular switch case. It exposes a convenient interface
3165 /// to both the case value and the successor block.
3166 ///
3167 /// We define this as a template and instantiate it to form both a const and
3168 /// non-const handle.
3169 template <typename SwitchInstT, typename ConstantIntT, typename BasicBlockT>
3170 class CaseHandleImpl {
3171 // Directly befriend both const and non-const iterators.
3172 friend class SwitchInst::CaseIteratorImpl<
3173 CaseHandleImpl<SwitchInstT, ConstantIntT, BasicBlockT>>;
3174
3175 protected:
3176 // Expose the switch type we're parameterized with to the iterator.
3177 using SwitchInstType = SwitchInstT;
3178
3179 SwitchInstT *SI;
3180 ptrdiff_t Index;
3181
3182 CaseHandleImpl() = default;
3183 CaseHandleImpl(SwitchInstT *SI, ptrdiff_t Index) : SI(SI), Index(Index) {}
3184
3185 public:
3186 /// Resolves case value for current case.
3187 ConstantIntT *getCaseValue() const {
3188 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-10~svn374877/include/llvm/IR/Instructions.h"
, 3189, __PRETTY_FUNCTION__))
3189 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 3189, __PRETTY_FUNCTION__))
;
3190 return reinterpret_cast<ConstantIntT *>(SI->getOperand(2 + Index * 2));
3191 }
3192
3193 /// Resolves successor for current case.
3194 BasicBlockT *getCaseSuccessor() const {
3195 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-10~svn374877/include/llvm/IR/Instructions.h"
, 3197, __PRETTY_FUNCTION__))
3196 (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-10~svn374877/include/llvm/IR/Instructions.h"
, 3197, __PRETTY_FUNCTION__))
3197 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 3197, __PRETTY_FUNCTION__))
;
3198 return SI->getSuccessor(getSuccessorIndex());
3199 }
3200
3201 /// Returns number of current case.
3202 unsigned getCaseIndex() const { return Index; }
3203
3204 /// Returns successor index for current case successor.
3205 unsigned getSuccessorIndex() const {
3206 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-10~svn374877/include/llvm/IR/Instructions.h"
, 3208, __PRETTY_FUNCTION__))
3207 (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-10~svn374877/include/llvm/IR/Instructions.h"
, 3208, __PRETTY_FUNCTION__))
3208 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 3208, __PRETTY_FUNCTION__))
;
3209 return (unsigned)Index != DefaultPseudoIndex ? Index + 1 : 0;
3210 }
3211
3212 bool operator==(const CaseHandleImpl &RHS) const {
3213 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-10~svn374877/include/llvm/IR/Instructions.h"
, 3213, __PRETTY_FUNCTION__))
;
3214 return Index == RHS.Index;
3215 }
3216 };
3217
3218 using ConstCaseHandle =
3219 CaseHandleImpl<const SwitchInst, const ConstantInt, const BasicBlock>;
3220
3221 class CaseHandle
3222 : public CaseHandleImpl<SwitchInst, ConstantInt, BasicBlock> {
3223 friend class SwitchInst::CaseIteratorImpl<CaseHandle>;
3224
3225 public:
3226 CaseHandle(SwitchInst *SI, ptrdiff_t Index) : CaseHandleImpl(SI, Index) {}
3227
3228 /// Sets the new value for current case.
3229 void setValue(ConstantInt *V) {
3230 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-10~svn374877/include/llvm/IR/Instructions.h"
, 3231, __PRETTY_FUNCTION__))
3231 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 3231, __PRETTY_FUNCTION__))
;
3232 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
3233 }
3234
3235 /// Sets the new successor for current case.
3236 void setSuccessor(BasicBlock *S) {
3237 SI->setSuccessor(getSuccessorIndex(), S);
3238 }
3239 };
3240
3241 template <typename CaseHandleT>
3242 class CaseIteratorImpl
3243 : public iterator_facade_base<CaseIteratorImpl<CaseHandleT>,
3244 std::random_access_iterator_tag,
3245 CaseHandleT> {
3246 using SwitchInstT = typename CaseHandleT::SwitchInstType;
3247
3248 CaseHandleT Case;
3249
3250 public:
3251 /// Default constructed iterator is in an invalid state until assigned to
3252 /// a case for a particular switch.
3253 CaseIteratorImpl() = default;
3254
3255 /// Initializes case iterator for given SwitchInst and for given
3256 /// case number.
3257 CaseIteratorImpl(SwitchInstT *SI, unsigned CaseNum) : Case(SI, CaseNum) {}
3258
3259 /// Initializes case iterator for given SwitchInst and for given
3260 /// successor index.
3261 static CaseIteratorImpl fromSuccessorIndex(SwitchInstT *SI,
3262 unsigned SuccessorIndex) {
3263 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-10~svn374877/include/llvm/IR/Instructions.h"
, 3264, __PRETTY_FUNCTION__))
3264 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 3264, __PRETTY_FUNCTION__))
;
3265 return SuccessorIndex != 0 ? CaseIteratorImpl(SI, SuccessorIndex - 1)
3266 : CaseIteratorImpl(SI, DefaultPseudoIndex);
3267 }
3268
3269 /// Support converting to the const variant. This will be a no-op for const
3270 /// variant.
3271 operator CaseIteratorImpl<ConstCaseHandle>() const {
3272 return CaseIteratorImpl<ConstCaseHandle>(Case.SI, Case.Index);
3273 }
3274
3275 CaseIteratorImpl &operator+=(ptrdiff_t N) {
3276 // Check index correctness after addition.
3277 // Note: Index == getNumCases() means end().
3278 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-10~svn374877/include/llvm/IR/Instructions.h"
, 3280, __PRETTY_FUNCTION__))
3279 (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-10~svn374877/include/llvm/IR/Instructions.h"
, 3280, __PRETTY_FUNCTION__))
3280 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 3280, __PRETTY_FUNCTION__))
;
3281 Case.Index += N;
3282 return *this;
3283 }
3284 CaseIteratorImpl &operator-=(ptrdiff_t N) {
3285 // Check index correctness after subtraction.
3286 // Note: Case.Index == getNumCases() means end().
3287 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-10~svn374877/include/llvm/IR/Instructions.h"
, 3289, __PRETTY_FUNCTION__))
3288 (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-10~svn374877/include/llvm/IR/Instructions.h"
, 3289, __PRETTY_FUNCTION__))
3289 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 3289, __PRETTY_FUNCTION__))
;
3290 Case.Index -= N;
3291 return *this;
3292 }
3293 ptrdiff_t operator-(const CaseIteratorImpl &RHS) const {
3294 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-10~svn374877/include/llvm/IR/Instructions.h"
, 3294, __PRETTY_FUNCTION__))
;
3295 return Case.Index - RHS.Case.Index;
3296 }
3297 bool operator==(const CaseIteratorImpl &RHS) const {
3298 return Case == RHS.Case;
3299 }
3300 bool operator<(const CaseIteratorImpl &RHS) const {
3301 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-10~svn374877/include/llvm/IR/Instructions.h"
, 3301, __PRETTY_FUNCTION__))
;
3302 return Case.Index < RHS.Case.Index;
3303 }
3304 CaseHandleT &operator*() { return Case; }
3305 const CaseHandleT &operator*() const { return Case; }
3306 };
3307
3308 using CaseIt = CaseIteratorImpl<CaseHandle>;
3309 using ConstCaseIt = CaseIteratorImpl<ConstCaseHandle>;
3310
3311 static SwitchInst *Create(Value *Value, BasicBlock *Default,
3312 unsigned NumCases,
3313 Instruction *InsertBefore = nullptr) {
3314 return new SwitchInst(Value, Default, NumCases, InsertBefore);
3315 }
3316
3317 static SwitchInst *Create(Value *Value, BasicBlock *Default,
3318 unsigned NumCases, BasicBlock *InsertAtEnd) {
3319 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
3320 }
3321
3322 /// Provide fast operand accessors
3323 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
;
3324
3325 // Accessor Methods for Switch stmt
3326 Value *getCondition() const { return getOperand(0); }
3327 void setCondition(Value *V) { setOperand(0, V); }
3328
3329 BasicBlock *getDefaultDest() const {
3330 return cast<BasicBlock>(getOperand(1));
3331 }
3332
3333 void setDefaultDest(BasicBlock *DefaultCase) {
3334 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
3335 }
3336
3337 /// Return the number of 'cases' in this switch instruction, excluding the
3338 /// default case.
3339 unsigned getNumCases() const {
3340 return getNumOperands()/2 - 1;
3341 }
3342
3343 /// Returns a read/write iterator that points to the first case in the
3344 /// SwitchInst.
3345 CaseIt case_begin() {
3346 return CaseIt(this, 0);
3347 }
3348
3349 /// Returns a read-only iterator that points to the first case in the
3350 /// SwitchInst.
3351 ConstCaseIt case_begin() const {
3352 return ConstCaseIt(this, 0);
3353 }
3354
3355 /// Returns a read/write iterator that points one past the last in the
3356 /// SwitchInst.
3357 CaseIt case_end() {
3358 return CaseIt(this, getNumCases());
3359 }
3360
3361 /// Returns a read-only iterator that points one past the last in the
3362 /// SwitchInst.
3363 ConstCaseIt case_end() const {
3364 return ConstCaseIt(this, getNumCases());
3365 }
3366
3367 /// Iteration adapter for range-for loops.
3368 iterator_range<CaseIt> cases() {
3369 return make_range(case_begin(), case_end());
3370 }
3371
3372 /// Constant iteration adapter for range-for loops.
3373 iterator_range<ConstCaseIt> cases() const {
3374 return make_range(case_begin(), case_end());
3375 }
3376
3377 /// Returns an iterator that points to the default case.
3378 /// Note: this iterator allows to resolve successor only. Attempt
3379 /// to resolve case value causes an assertion.
3380 /// Also note, that increment and decrement also causes an assertion and
3381 /// makes iterator invalid.
3382 CaseIt case_default() {
3383 return CaseIt(this, DefaultPseudoIndex);
3384 }
3385 ConstCaseIt case_default() const {
3386 return ConstCaseIt(this, DefaultPseudoIndex);
3387 }
3388
3389 /// Search all of the case values for the specified constant. If it is
3390 /// explicitly handled, return the case iterator of it, otherwise return
3391 /// default case iterator to indicate that it is handled by the default
3392 /// handler.
3393 CaseIt findCaseValue(const ConstantInt *C) {
3394 CaseIt I = llvm::find_if(
3395 cases(), [C](CaseHandle &Case) { return Case.getCaseValue() == C; });
3396 if (I != case_end())
3397 return I;
3398
3399 return case_default();
3400 }
3401 ConstCaseIt findCaseValue(const ConstantInt *C) const {
3402 ConstCaseIt I = llvm::find_if(cases(), [C](ConstCaseHandle &Case) {
3403 return Case.getCaseValue() == C;
3404 });
3405 if (I != case_end())
3406 return I;
3407
3408 return case_default();
3409 }
3410
3411 /// Finds the unique case value for a given successor. Returns null if the
3412 /// successor is not found, not unique, or is the default case.
3413 ConstantInt *findCaseDest(BasicBlock *BB) {
3414 if (BB == getDefaultDest())
3415 return nullptr;
3416
3417 ConstantInt *CI = nullptr;
3418 for (auto Case : cases()) {
3419 if (Case.getCaseSuccessor() != BB)
3420 continue;
3421
3422 if (CI)
3423 return nullptr; // Multiple cases lead to BB.
3424
3425 CI = Case.getCaseValue();
3426 }
3427
3428 return CI;
3429 }
3430
3431 /// Add an entry to the switch instruction.
3432 /// Note:
3433 /// This action invalidates case_end(). Old case_end() iterator will
3434 /// point to the added case.
3435 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
3436
3437 /// This method removes the specified case and its successor from the switch
3438 /// instruction. Note that this operation may reorder the remaining cases at
3439 /// index idx and above.
3440 /// Note:
3441 /// This action invalidates iterators for all cases following the one removed,
3442 /// including the case_end() iterator. It returns an iterator for the next
3443 /// case.
3444 CaseIt removeCase(CaseIt I);
3445
3446 unsigned getNumSuccessors() const { return getNumOperands()/2; }
3447 BasicBlock *getSuccessor(unsigned idx) const {
3448 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-10~svn374877/include/llvm/IR/Instructions.h"
, 3448, __PRETTY_FUNCTION__))
;
3449 return cast<BasicBlock>(getOperand(idx*2+1));
3450 }
3451 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3452 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-10~svn374877/include/llvm/IR/Instructions.h"
, 3452, __PRETTY_FUNCTION__))
;
3453 setOperand(idx * 2 + 1, NewSucc);
3454 }
3455
3456 // Methods for support type inquiry through isa, cast, and dyn_cast:
3457 static bool classof(const Instruction *I) {
3458 return I->getOpcode() == Instruction::Switch;
3459 }
3460 static bool classof(const Value *V) {
3461 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3462 }
3463};
3464
3465/// A wrapper class to simplify modification of SwitchInst cases along with
3466/// their prof branch_weights metadata.
3467class SwitchInstProfUpdateWrapper {
3468 SwitchInst &SI;
3469 Optional<SmallVector<uint32_t, 8> > Weights = None;
3470 bool Changed = false;
3471
3472protected:
3473 static MDNode *getProfBranchWeightsMD(const SwitchInst &SI);
3474
3475 MDNode *buildProfBranchWeightsMD();
3476
3477 void init();
3478
3479public:
3480 using CaseWeightOpt = Optional<uint32_t>;
3481 SwitchInst *operator->() { return &SI; }
3482 SwitchInst &operator*() { return SI; }
3483 operator SwitchInst *() { return &SI; }
3484
3485 SwitchInstProfUpdateWrapper(SwitchInst &SI) : SI(SI) { init(); }
3486
3487 ~SwitchInstProfUpdateWrapper() {
3488 if (Changed)
3489 SI.setMetadata(LLVMContext::MD_prof, buildProfBranchWeightsMD());
3490 }
3491
3492 /// Delegate the call to the underlying SwitchInst::removeCase() and remove
3493 /// correspondent branch weight.
3494 SwitchInst::CaseIt removeCase(SwitchInst::CaseIt I);
3495
3496 /// Delegate the call to the underlying SwitchInst::addCase() and set the
3497 /// specified branch weight for the added case.
3498 void addCase(ConstantInt *OnVal, BasicBlock *Dest, CaseWeightOpt W);
3499
3500 /// Delegate the call to the underlying SwitchInst::eraseFromParent() and mark
3501 /// this object to not touch the underlying SwitchInst in destructor.
3502 SymbolTableList<Instruction>::iterator eraseFromParent();
3503
3504 void setSuccessorWeight(unsigned idx, CaseWeightOpt W);
3505 CaseWeightOpt getSuccessorWeight(unsigned idx);
3506
3507 static CaseWeightOpt getSuccessorWeight(const SwitchInst &SI, unsigned idx);
3508};
3509
3510template <>
3511struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
3512};
3513
3514DEFINE_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-10~svn374877/include/llvm/IR/Instructions.h"
, 3514, __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-10~svn374877/include/llvm/IR/Instructions.h"
, 3514, __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); }
3515
3516//===----------------------------------------------------------------------===//
3517// IndirectBrInst Class
3518//===----------------------------------------------------------------------===//
3519
3520//===---------------------------------------------------------------------------
3521/// Indirect Branch Instruction.
3522///
3523class IndirectBrInst : public Instruction {
3524 unsigned ReservedSpace;
3525
3526 // Operand[0] = Address to jump to
3527 // Operand[n+1] = n-th destination
3528 IndirectBrInst(const IndirectBrInst &IBI);
3529
3530 /// Create a new indirectbr instruction, specifying an
3531 /// Address to jump to. The number of expected destinations can be specified
3532 /// here to make memory allocation more efficient. This constructor can also
3533 /// autoinsert before another instruction.
3534 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
3535
3536 /// Create a new indirectbr instruction, specifying an
3537 /// Address to jump to. The number of expected destinations can be specified
3538 /// here to make memory allocation more efficient. This constructor also
3539 /// autoinserts at the end of the specified BasicBlock.
3540 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
3541
3542 // allocate space for exactly zero operands
3543 void *operator new(size_t s) {
3544 return User::operator new(s);
3545 }
3546
3547 void init(Value *Address, unsigned NumDests);
3548 void growOperands();
3549
3550protected:
3551 // Note: Instruction needs to be a friend here to call cloneImpl.
3552 friend class Instruction;
3553
3554 IndirectBrInst *cloneImpl() const;
3555
3556public:
3557 /// Iterator type that casts an operand to a basic block.
3558 ///
3559 /// This only makes sense because the successors are stored as adjacent
3560 /// operands for indirectbr instructions.
3561 struct succ_op_iterator
3562 : iterator_adaptor_base<succ_op_iterator, value_op_iterator,
3563 std::random_access_iterator_tag, BasicBlock *,
3564 ptrdiff_t, BasicBlock *, BasicBlock *> {
3565 explicit succ_op_iterator(value_op_iterator I) : iterator_adaptor_base(I) {}
3566
3567 BasicBlock *operator*() const { return cast<BasicBlock>(*I); }
3568 BasicBlock *operator->() const { return operator*(); }
3569 };
3570
3571 /// The const version of `succ_op_iterator`.
3572 struct const_succ_op_iterator
3573 : iterator_adaptor_base<const_succ_op_iterator, const_value_op_iterator,
3574 std::random_access_iterator_tag,
3575 const BasicBlock *, ptrdiff_t, const BasicBlock *,
3576 const BasicBlock *> {
3577 explicit const_succ_op_iterator(const_value_op_iterator I)
3578 : iterator_adaptor_base(I) {}
3579
3580 const BasicBlock *operator*() const { return cast<BasicBlock>(*I); }
3581 const BasicBlock *operator->() const { return operator*(); }
3582 };
3583
3584 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3585 Instruction *InsertBefore = nullptr) {
3586 return new IndirectBrInst(Address, NumDests, InsertBefore);
3587 }
3588
3589 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3590 BasicBlock *InsertAtEnd) {
3591 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
3592 }
3593
3594 /// Provide fast operand accessors.
3595 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
;
3596
3597 // Accessor Methods for IndirectBrInst instruction.
3598 Value *getAddress() { return getOperand(0); }
3599 const Value *getAddress() const { return getOperand(0); }
3600 void setAddress(Value *V) { setOperand(0, V); }
3601
3602 /// return the number of possible destinations in this
3603 /// indirectbr instruction.
3604 unsigned getNumDestinations() const { return getNumOperands()-1; }
3605
3606 /// Return the specified destination.
3607 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
3608 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
3609
3610 /// Add a destination.
3611 ///
3612 void addDestination(BasicBlock *Dest);
3613
3614 /// This method removes the specified successor from the
3615 /// indirectbr instruction.
3616 void removeDestination(unsigned i);
3617
3618 unsigned getNumSuccessors() const { return getNumOperands()-1; }
3619 BasicBlock *getSuccessor(unsigned i) const {
3620 return cast<BasicBlock>(getOperand(i+1));
3621 }
3622 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
3623 setOperand(i + 1, NewSucc);
3624 }
3625
3626 iterator_range<succ_op_iterator> successors() {
3627 return make_range(succ_op_iterator(std::next(value_op_begin())),
3628 succ_op_iterator(value_op_end()));
3629 }
3630
3631 iterator_range<const_succ_op_iterator> successors() const {
3632 return make_range(const_succ_op_iterator(std::next(value_op_begin())),
3633 const_succ_op_iterator(value_op_end()));
3634 }
3635
3636 // Methods for support type inquiry through isa, cast, and dyn_cast:
3637 static bool classof(const Instruction *I) {
3638 return I->getOpcode() == Instruction::IndirectBr;
3639 }
3640 static bool classof(const Value *V) {
3641 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3642 }
3643};
3644
3645template <>
3646struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
3647};
3648
3649DEFINE_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-10~svn374877/include/llvm/IR/Instructions.h"
, 3649, __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-10~svn374877/include/llvm/IR/Instructions.h"
, 3649, __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); }
3650
3651//===----------------------------------------------------------------------===//
3652// InvokeInst Class
3653//===----------------------------------------------------------------------===//
3654
3655/// Invoke instruction. The SubclassData field is used to hold the
3656/// calling convention of the call.
3657///
3658class InvokeInst : public CallBase {
3659 /// The number of operands for this call beyond the called function,
3660 /// arguments, and operand bundles.
3661 static constexpr int NumExtraOperands = 2;
3662
3663 /// The index from the end of the operand array to the normal destination.
3664 static constexpr int NormalDestOpEndIdx = -3;
3665
3666 /// The index from the end of the operand array to the unwind destination.
3667 static constexpr int UnwindDestOpEndIdx = -2;
3668
3669 InvokeInst(const InvokeInst &BI);
3670
3671 /// Construct an InvokeInst given a range of arguments.
3672 ///
3673 /// Construct an InvokeInst from a range of arguments
3674 inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3675 BasicBlock *IfException, ArrayRef<Value *> Args,
3676 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
3677 const Twine &NameStr, Instruction *InsertBefore);
3678
3679 inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3680 BasicBlock *IfException, ArrayRef<Value *> Args,
3681 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
3682 const Twine &NameStr, BasicBlock *InsertAtEnd);
3683
3684 void init(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3685 BasicBlock *IfException, ArrayRef<Value *> Args,
3686 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
3687
3688 /// Compute the number of operands to allocate.
3689 static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) {
3690 // We need one operand for the called function, plus our extra operands and
3691 // the input operand counts provided.
3692 return 1 + NumExtraOperands + NumArgs + NumBundleInputs;
3693 }
3694
3695protected:
3696 // Note: Instruction needs to be a friend here to call cloneImpl.
3697 friend class Instruction;
3698
3699 InvokeInst *cloneImpl() const;
3700
3701public:
3702 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3703 BasicBlock *IfException, ArrayRef<Value *> Args,
3704 const Twine &NameStr,
3705 Instruction *InsertBefore = nullptr) {
3706 int NumOperands = ComputeNumOperands(Args.size());
3707 return new (NumOperands)
3708 InvokeInst(Ty, Func, IfNormal, IfException, Args, None, NumOperands,
3709 NameStr, InsertBefore);
3710 }
3711
3712 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3713 BasicBlock *IfException, ArrayRef<Value *> Args,
3714 ArrayRef<OperandBundleDef> Bundles = None,
3715 const Twine &NameStr = "",
3716 Instruction *InsertBefore = nullptr) {
3717 int NumOperands =
3718 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles));
3719 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3720
3721 return new (NumOperands, DescriptorBytes)
3722 InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, NumOperands,
3723 NameStr, InsertBefore);
3724 }
3725
3726 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3727 BasicBlock *IfException, ArrayRef<Value *> Args,
3728 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3729 int NumOperands = ComputeNumOperands(Args.size());
3730 return new (NumOperands)
3731 InvokeInst(Ty, Func, IfNormal, IfException, Args, None, NumOperands,
3732 NameStr, InsertAtEnd);
3733 }
3734
3735 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3736 BasicBlock *IfException, ArrayRef<Value *> Args,
3737 ArrayRef<OperandBundleDef> Bundles,
3738 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3739 int NumOperands =
3740 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles));
3741 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3742
3743 return new (NumOperands, DescriptorBytes)
3744 InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, NumOperands,
3745 NameStr, InsertAtEnd);
3746 }
3747
3748 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
3749 BasicBlock *IfException, ArrayRef<Value *> Args,
3750 const Twine &NameStr,
3751 Instruction *InsertBefore = nullptr) {
3752 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3753 IfException, Args, None, NameStr, InsertBefore);
3754 }
3755
3756 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
3757 BasicBlock *IfException, ArrayRef<Value *> Args,
3758 ArrayRef<OperandBundleDef> Bundles = None,
3759 const Twine &NameStr = "",
3760 Instruction *InsertBefore = nullptr) {
3761 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3762 IfException, Args, Bundles, NameStr, InsertBefore);
3763 }
3764
3765 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
3766 BasicBlock *IfException, ArrayRef<Value *> Args,
3767 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3768 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3769 IfException, Args, NameStr, InsertAtEnd);
3770 }
3771
3772 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
3773 BasicBlock *IfException, ArrayRef<Value *> Args,
3774 ArrayRef<OperandBundleDef> Bundles,
3775 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3776 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3777 IfException, Args, Bundles, NameStr, InsertAtEnd);
3778 }
3779
3780 // Deprecated [opaque pointer types]
3781 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3782 BasicBlock *IfException, ArrayRef<Value *> Args,
3783 const Twine &NameStr,
3784 Instruction *InsertBefore = nullptr) {
3785 return Create(cast<FunctionType>(
3786 cast<PointerType>(Func->getType())->getElementType()),
3787 Func, IfNormal, IfException, Args, None, NameStr,
3788 InsertBefore);
3789 }
3790
3791 // Deprecated [opaque pointer types]
3792 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3793 BasicBlock *IfException, ArrayRef<Value *> Args,
3794 ArrayRef<OperandBundleDef> Bundles = None,
3795 const Twine &NameStr = "",
3796 Instruction *InsertBefore = nullptr) {
3797 return Create(cast<FunctionType>(
3798 cast<PointerType>(Func->getType())->getElementType()),
3799 Func, IfNormal, IfException, Args, Bundles, NameStr,
3800 InsertBefore);
3801 }
3802
3803 // Deprecated [opaque pointer types]
3804 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3805 BasicBlock *IfException, ArrayRef<Value *> Args,
3806 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3807 return Create(cast<FunctionType>(
3808 cast<PointerType>(Func->getType())->getElementType()),
3809 Func, IfNormal, IfException, Args, NameStr, InsertAtEnd);
3810 }
3811
3812 // Deprecated [opaque pointer types]
3813 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3814 BasicBlock *IfException, ArrayRef<Value *> Args,
3815 ArrayRef<OperandBundleDef> Bundles,
3816 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3817 return Create(cast<FunctionType>(
3818 cast<PointerType>(Func->getType())->getElementType()),
3819 Func, IfNormal, IfException, Args, Bundles, NameStr,
3820 InsertAtEnd);
3821 }
3822
3823 /// Create a clone of \p II with a different set of operand bundles and
3824 /// insert it before \p InsertPt.
3825 ///
3826 /// The returned invoke instruction is identical to \p II in every way except
3827 /// that the operand bundles for the new instruction are set to the operand
3828 /// bundles in \p Bundles.
3829 static InvokeInst *Create(InvokeInst *II, ArrayRef<OperandBundleDef> Bundles,
3830 Instruction *InsertPt = nullptr);
3831
3832 /// Determine if the call should not perform indirect branch tracking.
3833 bool doesNoCfCheck() const { return hasFnAttr(Attribute::NoCfCheck); }
3834
3835 /// Determine if the call cannot unwind.
3836 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
3837 void setDoesNotThrow() {
3838 addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
3839 }
3840
3841 // get*Dest - Return the destination basic blocks...
3842 BasicBlock *getNormalDest() const {
3843 return cast<BasicBlock>(Op<NormalDestOpEndIdx>());
3844 }
3845 BasicBlock *getUnwindDest() const {
3846 return cast<BasicBlock>(Op<UnwindDestOpEndIdx>());
3847 }
3848 void setNormalDest(BasicBlock *B) {
3849 Op<NormalDestOpEndIdx>() = reinterpret_cast<Value *>(B);
3850 }
3851 void setUnwindDest(BasicBlock *B) {
3852 Op<UnwindDestOpEndIdx>() = reinterpret_cast<Value *>(B);
3853 }
3854
3855 /// Get the landingpad instruction from the landing pad
3856 /// block (the unwind destination).
3857 LandingPadInst *getLandingPadInst() const;
3858
3859 BasicBlock *getSuccessor(unsigned i) const {
3860 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-10~svn374877/include/llvm/IR/Instructions.h"
, 3860, __PRETTY_FUNCTION__))
;
3861 return i == 0 ? getNormalDest() : getUnwindDest();
3862 }
3863
3864 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
3865 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-10~svn374877/include/llvm/IR/Instructions.h"
, 3865, __PRETTY_FUNCTION__))
;
3866 if (i == 0)
3867 setNormalDest(NewSucc);
3868 else
3869 setUnwindDest(NewSucc);
3870 }
3871
3872 unsigned getNumSuccessors() const { return 2; }
3873
3874 // Methods for support type inquiry through isa, cast, and dyn_cast:
3875 static bool classof(const Instruction *I) {
3876 return (I->getOpcode() == Instruction::Invoke);
3877 }
3878 static bool classof(const Value *V) {
3879 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3880 }
3881
3882private:
3883
3884 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3885 // method so that subclasses cannot accidentally use it.
3886 void setInstructionSubclassData(unsigned short D) {
3887 Instruction::setInstructionSubclassData(D);
3888 }
3889};
3890
3891InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3892 BasicBlock *IfException, ArrayRef<Value *> Args,
3893 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
3894 const Twine &NameStr, Instruction *InsertBefore)
3895 : CallBase(Ty->getReturnType(), Instruction::Invoke,
3896 OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands,
3897 InsertBefore) {
3898 init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr);
3899}
3900
3901InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3902 BasicBlock *IfException, ArrayRef<Value *> Args,
3903 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
3904 const Twine &NameStr, BasicBlock *InsertAtEnd)
3905 : CallBase(Ty->getReturnType(), Instruction::Invoke,
3906 OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands,
3907 InsertAtEnd) {
3908 init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr);
3909}
3910
3911//===----------------------------------------------------------------------===//
3912// CallBrInst Class
3913//===----------------------------------------------------------------------===//
3914
3915/// CallBr instruction, tracking function calls that may not return control but
3916/// instead transfer it to a third location. The SubclassData field is used to
3917/// hold the calling convention of the call.
3918///
3919class CallBrInst : public CallBase {
3920
3921 unsigned NumIndirectDests;
3922
3923 CallBrInst(const CallBrInst &BI);
3924
3925 /// Construct a CallBrInst given a range of arguments.
3926 ///
3927 /// Construct a CallBrInst from a range of arguments
3928 inline CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest,
3929 ArrayRef<BasicBlock *> IndirectDests,
3930 ArrayRef<Value *> Args,
3931 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
3932 const Twine &NameStr, Instruction *InsertBefore);
3933
3934 inline CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest,
3935 ArrayRef<BasicBlock *> IndirectDests,
3936 ArrayRef<Value *> Args,
3937 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
3938 const Twine &NameStr, BasicBlock *InsertAtEnd);
3939
3940 void init(FunctionType *FTy, Value *Func, BasicBlock *DefaultDest,
3941 ArrayRef<BasicBlock *> IndirectDests, ArrayRef<Value *> Args,
3942 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
3943
3944 /// Should the Indirect Destinations change, scan + update the Arg list.
3945 void updateArgBlockAddresses(unsigned i, BasicBlock *B);
3946
3947 /// Compute the number of operands to allocate.
3948 static int ComputeNumOperands(int NumArgs, int NumIndirectDests,
3949 int NumBundleInputs = 0) {
3950 // We need one operand for the called function, plus our extra operands and
3951 // the input operand counts provided.
3952 return 2 + NumIndirectDests + NumArgs + NumBundleInputs;
3953 }
3954
3955protected:
3956 // Note: Instruction needs to be a friend here to call cloneImpl.
3957 friend class Instruction;
3958
3959 CallBrInst *cloneImpl() const;
3960
3961public:
3962 static CallBrInst *Create(FunctionType *Ty, Value *Func,
3963 BasicBlock *DefaultDest,
3964 ArrayRef<BasicBlock *> IndirectDests,
3965 ArrayRef<Value *> Args, const Twine &NameStr,
3966 Instruction *InsertBefore = nullptr) {
3967 int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size());
3968 return new (NumOperands)
3969 CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, None,
3970 NumOperands, NameStr, InsertBefore);
3971 }
3972
3973 static CallBrInst *Create(FunctionType *Ty, Value *Func,
3974 BasicBlock *DefaultDest,
3975 ArrayRef<BasicBlock *> IndirectDests,
3976 ArrayRef<Value *> Args,
3977 ArrayRef<OperandBundleDef> Bundles = None,
3978 const Twine &NameStr = "",
3979 Instruction *InsertBefore = nullptr) {
3980 int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size(),
3981 CountBundleInputs(Bundles));
3982 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3983
3984 return new (NumOperands, DescriptorBytes)
3985 CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, Bundles,
3986 NumOperands, NameStr, InsertBefore);
3987 }
3988
3989 static CallBrInst *Create(FunctionType *Ty, Value *Func,
3990 BasicBlock *DefaultDest,
3991 ArrayRef<BasicBlock *> IndirectDests,
3992 ArrayRef<Value *> Args, const Twine &NameStr,
3993 BasicBlock *InsertAtEnd) {
3994 int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size());
3995 return new (NumOperands)
3996 CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, None,
3997 NumOperands, NameStr, InsertAtEnd);
3998 }
3999
4000 static CallBrInst *Create(FunctionType *Ty, Value *Func,
4001 BasicBlock *DefaultDest,
4002 ArrayRef<BasicBlock *> IndirectDests,
4003 ArrayRef<Value *> Args,
4004 ArrayRef<OperandBundleDef> Bundles,
4005 const Twine &NameStr, BasicBlock *InsertAtEnd) {
4006 int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size(),
4007 CountBundleInputs(Bundles));
4008 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
4009
4010 return new (NumOperands, DescriptorBytes)
4011 CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, Bundles,
4012 NumOperands, NameStr, InsertAtEnd);
4013 }
4014
4015 static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest,
4016 ArrayRef<BasicBlock *> IndirectDests,
4017 ArrayRef<Value *> Args, const Twine &NameStr,
4018 Instruction *InsertBefore = nullptr) {
4019 return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest,
4020 IndirectDests, Args, NameStr, InsertBefore);
4021 }
4022
4023 static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest,
4024 ArrayRef<BasicBlock *> IndirectDests,
4025 ArrayRef<Value *> Args,
4026 ArrayRef<OperandBundleDef> Bundles = None,
4027 const Twine &NameStr = "",
4028 Instruction *InsertBefore = nullptr) {
4029 return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest,
4030 IndirectDests, Args, Bundles, NameStr, InsertBefore);
4031 }
4032
4033 static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest,
4034 ArrayRef<BasicBlock *> IndirectDests,
4035 ArrayRef<Value *> Args, const Twine &NameStr,
4036 BasicBlock *InsertAtEnd) {
4037 return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest,
4038 IndirectDests, Args, NameStr, InsertAtEnd);
4039 }
4040
4041 static CallBrInst *Create(FunctionCallee Func,
4042 BasicBlock *DefaultDest,
4043 ArrayRef<BasicBlock *> IndirectDests,
4044 ArrayRef<Value *> Args,
4045 ArrayRef<OperandBundleDef> Bundles,
4046 const Twine &NameStr, BasicBlock *InsertAtEnd) {
4047 return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest,
4048 IndirectDests, Args, Bundles, NameStr, InsertAtEnd);
4049 }
4050
4051 /// Create a clone of \p CBI with a different set of operand bundles and
4052 /// insert it before \p InsertPt.
4053 ///
4054 /// The returned callbr instruction is identical to \p CBI in every way
4055 /// except that the operand bundles for the new instruction are set to the
4056 /// operand bundles in \p Bundles.
4057 static CallBrInst *Create(CallBrInst *CBI,
4058 ArrayRef<OperandBundleDef> Bundles,
4059 Instruction *InsertPt = nullptr);
4060
4061 /// Return the number of callbr indirect dest labels.
4062 ///
4063 unsigned getNumIndirectDests() const { return NumIndirectDests; }
4064
4065 /// getIndirectDestLabel - Return the i-th indirect dest label.
4066 ///
4067 Value *getIndirectDestLabel(unsigned i) const {
4068 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-10~svn374877/include/llvm/IR/Instructions.h"
, 4068, __PRETTY_FUNCTION__))
;
4069 return getOperand(i + getNumArgOperands() + getNumTotalBundleOperands() +
4070 1);
4071 }
4072
4073 Value *getIndirectDestLabelUse(unsigned i) const {
4074 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-10~svn374877/include/llvm/IR/Instructions.h"
, 4074, __PRETTY_FUNCTION__))
;
4075 return getOperandUse(i + getNumArgOperands() + getNumTotalBundleOperands() +
4076 1);
4077 }
4078
4079 // Return the destination basic blocks...
4080 BasicBlock *getDefaultDest() const {
4081 return cast<BasicBlock>(*(&Op<-1>() - getNumIndirectDests() - 1));
4082 }
4083 BasicBlock *getIndirectDest(unsigned i) const {
4084 return cast_or_null<BasicBlock>(*(&Op<-1>() - getNumIndirectDests() + i));
4085 }
4086 SmallVector<BasicBlock *, 16> getIndirectDests() const {
4087 SmallVector<BasicBlock *, 16> IndirectDests;
4088 for (unsigned i = 0, e = getNumIndirectDests(); i < e; ++i)
4089 IndirectDests.push_back(getIndirectDest(i));
4090 return IndirectDests;
4091 }
4092 void setDefaultDest(BasicBlock *B) {
4093 *(&Op<-1>() - getNumIndirectDests() - 1) = reinterpret_cast<Value *>(B);
4094 }
4095 void setIndirectDest(unsigned i, BasicBlock *B) {
4096 updateArgBlockAddresses(i, B);
4097 *(&Op<-1>() - getNumIndirectDests() + i) = reinterpret_cast<Value *>(B);
4098 }
4099
4100 BasicBlock *getSuccessor(unsigned i) const {
4101 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-10~svn374877/include/llvm/IR/Instructions.h"
, 4102, __PRETTY_FUNCTION__))
4102 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 4102, __PRETTY_FUNCTION__))
;
4103 return i == 0 ? getDefaultDest() : getIndirectDest(i - 1);
4104 }
4105
4106 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
4107 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-10~svn374877/include/llvm/IR/Instructions.h"
, 4108, __PRETTY_FUNCTION__))
4108 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 4108, __PRETTY_FUNCTION__))
;
4109 return i == 0 ? setDefaultDest(NewSucc) : setIndirectDest(i - 1, NewSucc);
4110 }
4111
4112 unsigned getNumSuccessors() const { return getNumIndirectDests() + 1; }
4113
4114 // Methods for support type inquiry through isa, cast, and dyn_cast:
4115 static bool classof(const Instruction *I) {
4116 return (I->getOpcode() == Instruction::CallBr);
4117 }
4118 static bool classof(const Value *V) {
4119 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4120 }
4121
4122private:
4123
4124 // Shadow Instruction::setInstructionSubclassData with a private forwarding
4125 // method so that subclasses cannot accidentally use it.
4126 void setInstructionSubclassData(unsigned short D) {
4127 Instruction::setInstructionSubclassData(D);
4128 }
4129};
4130
4131CallBrInst::CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest,
4132 ArrayRef<BasicBlock *> IndirectDests,
4133 ArrayRef<Value *> Args,
4134 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
4135 const Twine &NameStr, Instruction *InsertBefore)
4136 : CallBase(Ty->getReturnType(), Instruction::CallBr,
4137 OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands,
4138 InsertBefore) {
4139 init(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, NameStr);
4140}
4141
4142CallBrInst::CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest,
4143 ArrayRef<BasicBlock *> IndirectDests,
4144 ArrayRef<Value *> Args,
4145 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
4146 const Twine &NameStr, BasicBlock *InsertAtEnd)
4147 : CallBase(
4148 cast<FunctionType>(
4149 cast<PointerType>(Func->getType())->getElementType())
4150 ->getReturnType(),
4151 Instruction::CallBr,
4152 OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands,
4153 InsertAtEnd) {
4154 init(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, NameStr);
4155}
4156
4157//===----------------------------------------------------------------------===//
4158// ResumeInst Class
4159//===----------------------------------------------------------------------===//
4160
4161//===---------------------------------------------------------------------------
4162/// Resume the propagation of an exception.
4163///
4164class ResumeInst : public Instruction {
4165 ResumeInst(const ResumeInst &RI);
4166
4167 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr);
4168 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
4169
4170protected:
4171 // Note: Instruction needs to be a friend here to call cloneImpl.
4172 friend class Instruction;
4173
4174 ResumeInst *cloneImpl() const;
4175
4176public:
4177 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) {
4178 return new(1) ResumeInst(Exn, InsertBefore);
4179 }
4180
4181 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
4182 return new(1) ResumeInst(Exn, InsertAtEnd);
4183 }
4184
4185 /// Provide fast operand accessors
4186 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
;
4187
4188 /// Convenience accessor.
4189 Value *getValue() const { return Op<0>(); }
4190
4191 unsigned getNumSuccessors() const { return 0; }
4192
4193 // Methods for support type inquiry through isa, cast, and dyn_cast:
4194 static bool classof(const Instruction *I) {
4195 return I->getOpcode() == Instruction::Resume;
4196 }
4197 static bool classof(const Value *V) {
4198 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4199 }
4200
4201private:
4202 BasicBlock *getSuccessor(unsigned idx) const {
4203 llvm_unreachable("ResumeInst has no successors!")::llvm::llvm_unreachable_internal("ResumeInst has no successors!"
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4203)
;
4204 }
4205
4206 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
4207 llvm_unreachable("ResumeInst has no successors!")::llvm::llvm_unreachable_internal("ResumeInst has no successors!"
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4207)
;
4208 }
4209};
4210
4211template <>
4212struct OperandTraits<ResumeInst> :
4213 public FixedNumOperandTraits<ResumeInst, 1> {
4214};
4215
4216DEFINE_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-10~svn374877/include/llvm/IR/Instructions.h"
, 4216, __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-10~svn374877/include/llvm/IR/Instructions.h"
, 4216, __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); }
4217
4218//===----------------------------------------------------------------------===//
4219// CatchSwitchInst Class
4220//===----------------------------------------------------------------------===//
4221class CatchSwitchInst : public Instruction {
4222 /// The number of operands actually allocated. NumOperands is
4223 /// the number actually in use.
4224 unsigned ReservedSpace;
4225
4226 // Operand[0] = Outer scope
4227 // Operand[1] = Unwind block destination
4228 // Operand[n] = BasicBlock to go to on match
4229 CatchSwitchInst(const CatchSwitchInst &CSI);
4230
4231 /// Create a new switch instruction, specifying a
4232 /// default destination. The number of additional handlers can be specified
4233 /// here to make memory allocation more efficient.
4234 /// This constructor can also autoinsert before another instruction.
4235 CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
4236 unsigned NumHandlers, const Twine &NameStr,
4237 Instruction *InsertBefore);
4238
4239 /// Create a new switch instruction, specifying a
4240 /// default destination. The number of additional handlers can be specified
4241 /// here to make memory allocation more efficient.
4242 /// This constructor also autoinserts at the end of the specified BasicBlock.
4243 CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
4244 unsigned NumHandlers, const Twine &NameStr,
4245 BasicBlock *InsertAtEnd);
4246
4247 // allocate space for exactly zero operands
4248 void *operator new(size_t s) { return User::operator new(s); }
4249
4250 void init(Value *ParentPad, BasicBlock *UnwindDest, unsigned NumReserved);
4251 void growOperands(unsigned Size);
4252
4253protected:
4254 // Note: Instruction needs to be a friend here to call cloneImpl.
4255 friend class Instruction;
4256
4257 CatchSwitchInst *cloneImpl() const;
4258
4259public:
4260 static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest,
4261 unsigned NumHandlers,
4262 const Twine &NameStr = "",
4263 Instruction *InsertBefore = nullptr) {
4264 return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr,
4265 InsertBefore);
4266 }
4267
4268 static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest,
4269 unsigned NumHandlers, const Twine &NameStr,
4270 BasicBlock *InsertAtEnd) {
4271 return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr,
4272 InsertAtEnd);
4273 }
4274
4275 /// Provide fast operand accessors
4276 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
;
4277
4278 // Accessor Methods for CatchSwitch stmt
4279 Value *getParentPad() const { return getOperand(0); }
4280 void setParentPad(Value *ParentPad) { setOperand(0, ParentPad); }
4281
4282 // Accessor Methods for CatchSwitch stmt
4283 bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; }
4284 bool unwindsToCaller() const { return !hasUnwindDest(); }
4285 BasicBlock *getUnwindDest() const {
4286 if (hasUnwindDest())
4287 return cast<BasicBlock>(getOperand(1));
4288 return nullptr;
4289 }
4290 void setUnwindDest(BasicBlock *UnwindDest) {
4291 assert(UnwindDest)((UnwindDest) ? static_cast<void> (0) : __assert_fail (
"UnwindDest", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4291, __PRETTY_FUNCTION__))
;
4292 assert(hasUnwindDest())((hasUnwindDest()) ? static_cast<void> (0) : __assert_fail
("hasUnwindDest()", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4292, __PRETTY_FUNCTION__))
;
4293 setOperand(1, UnwindDest);
4294 }
4295
4296 /// return the number of 'handlers' in this catchswitch
4297 /// instruction, except the default handler
4298 unsigned getNumHandlers() const {
4299 if (hasUnwindDest())
4300 return getNumOperands() - 2;
4301 return getNumOperands() - 1;
4302 }
4303
4304private:
4305 static BasicBlock *handler_helper(Value *V) { return cast<BasicBlock>(V); }
4306 static const BasicBlock *handler_helper(const Value *V) {
4307 return cast<BasicBlock>(V);
4308 }
4309
4310public:
4311 using DerefFnTy = BasicBlock *(*)(Value *);
4312 using handler_iterator = mapped_iterator<op_iterator, DerefFnTy>;
4313 using handler_range = iterator_range<handler_iterator>;
4314 using ConstDerefFnTy = const BasicBlock *(*)(const Value *);
4315 using const_handler_iterator =
4316 mapped_iterator<const_op_iterator, ConstDerefFnTy>;
4317 using const_handler_range = iterator_range<const_handler_iterator>;
4318
4319 /// Returns an iterator that points to the first handler in CatchSwitchInst.
4320 handler_iterator handler_begin() {
4321 op_iterator It = op_begin() + 1;
4322 if (hasUnwindDest())
4323 ++It;
4324 return handler_iterator(It, DerefFnTy(handler_helper));
4325 }
4326
4327 /// Returns an iterator that points to the first handler in the
4328 /// CatchSwitchInst.
4329 const_handler_iterator handler_begin() const {
4330 const_op_iterator It = op_begin() + 1;
4331 if (hasUnwindDest())
4332 ++It;
4333 return const_handler_iterator(It, ConstDerefFnTy(handler_helper));
4334 }
4335
4336 /// Returns a read-only iterator that points one past the last
4337 /// handler in the CatchSwitchInst.
4338 handler_iterator handler_end() {
4339 return handler_iterator(op_end(), DerefFnTy(handler_helper));
4340 }
4341
4342 /// Returns an iterator that points one past the last handler in the
4343 /// CatchSwitchInst.
4344 const_handler_iterator handler_end() const {
4345 return const_handler_iterator(op_end(), ConstDerefFnTy(handler_helper));
4346 }
4347
4348 /// iteration adapter for range-for loops.
4349 handler_range handlers() {
4350 return make_range(handler_begin(), handler_end());
4351 }
4352
4353 /// iteration adapter for range-for loops.
4354 const_handler_range handlers() const {
4355 return make_range(handler_begin(), handler_end());
4356 }
4357
4358 /// Add an entry to the switch instruction...
4359 /// Note:
4360 /// This action invalidates handler_end(). Old handler_end() iterator will
4361 /// point to the added handler.
4362 void addHandler(BasicBlock *Dest);
4363
4364 void removeHandler(handler_iterator HI);
4365
4366 unsigned getNumSuccessors() const { return getNumOperands() - 1; }
4367 BasicBlock *getSuccessor(unsigned Idx) const {
4368 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-10~svn374877/include/llvm/IR/Instructions.h"
, 4369, __PRETTY_FUNCTION__))
4369 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 4369, __PRETTY_FUNCTION__))
;
4370 return cast<BasicBlock>(getOperand(Idx + 1));
4371 }
4372 void setSuccessor(unsigned Idx, BasicBlock *NewSucc) {
4373 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-10~svn374877/include/llvm/IR/Instructions.h"
, 4374, __PRETTY_FUNCTION__))
4374 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 4374, __PRETTY_FUNCTION__))
;
4375 setOperand(Idx + 1, NewSucc);
4376 }
4377
4378 // Methods for support type inquiry through isa, cast, and dyn_cast:
4379 static bool classof(const Instruction *I) {
4380 return I->getOpcode() == Instruction::CatchSwitch;
4381 }
4382 static bool classof(const Value *V) {
4383 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4384 }
4385};
4386
4387template <>
4388struct OperandTraits<CatchSwitchInst> : public HungoffOperandTraits<2> {};
4389
4390DEFINE_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-10~svn374877/include/llvm/IR/Instructions.h"
, 4390, __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-10~svn374877/include/llvm/IR/Instructions.h"
, 4390, __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); }
4391
4392//===----------------------------------------------------------------------===//
4393// CleanupPadInst Class
4394//===----------------------------------------------------------------------===//
4395class CleanupPadInst : public FuncletPadInst {
4396private:
4397 explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args,
4398 unsigned Values, const Twine &NameStr,
4399 Instruction *InsertBefore)
4400 : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values,
4401 NameStr, InsertBefore) {}
4402 explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args,
4403 unsigned Values, const Twine &NameStr,
4404 BasicBlock *InsertAtEnd)
4405 : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values,
4406 NameStr, InsertAtEnd) {}
4407
4408public:
4409 static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args = None,
4410 const Twine &NameStr = "",
4411 Instruction *InsertBefore = nullptr) {
4412 unsigned Values = 1 + Args.size();
4413 return new (Values)
4414 CleanupPadInst(ParentPad, Args, Values, NameStr, InsertBefore);
4415 }
4416
4417 static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args,
4418 const Twine &NameStr, BasicBlock *InsertAtEnd) {
4419 unsigned Values = 1 + Args.size();
4420 return new (Values)
4421 CleanupPadInst(ParentPad, Args, Values, NameStr, InsertAtEnd);
4422 }
4423
4424 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4425 static bool classof(const Instruction *I) {
4426 return I->getOpcode() == Instruction::CleanupPad;
4427 }
4428 static bool classof(const Value *V) {
4429 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4430 }
4431};
4432
4433//===----------------------------------------------------------------------===//
4434// CatchPadInst Class
4435//===----------------------------------------------------------------------===//
4436class CatchPadInst : public FuncletPadInst {
4437private:
4438 explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args,
4439 unsigned Values, const Twine &NameStr,
4440 Instruction *InsertBefore)
4441 : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values,
4442 NameStr, InsertBefore) {}
4443 explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args,
4444 unsigned Values, const Twine &NameStr,
4445 BasicBlock *InsertAtEnd)
4446 : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values,
4447 NameStr, InsertAtEnd) {}
4448
4449public:
4450 static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args,
4451 const Twine &NameStr = "",
4452 Instruction *InsertBefore = nullptr) {
4453 unsigned Values = 1 + Args.size();
4454 return new (Values)
4455 CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertBefore);
4456 }
4457
4458 static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args,
4459 const Twine &NameStr, BasicBlock *InsertAtEnd) {
4460 unsigned Values = 1 + Args.size();
4461 return new (Values)
4462 CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertAtEnd);
4463 }
4464
4465 /// Convenience accessors
4466 CatchSwitchInst *getCatchSwitch() const {
4467 return cast<CatchSwitchInst>(Op<-1>());
4468 }
4469 void setCatchSwitch(Value *CatchSwitch) {
4470 assert(CatchSwitch)((CatchSwitch) ? static_cast<void> (0) : __assert_fail (
"CatchSwitch", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4470, __PRETTY_FUNCTION__))
;
4471 Op<-1>() = CatchSwitch;
4472 }
4473
4474 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4475 static bool classof(const Instruction *I) {
4476 return I->getOpcode() == Instruction::CatchPad;
4477 }
4478 static bool classof(const Value *V) {
4479 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4480 }
4481};
4482
4483//===----------------------------------------------------------------------===//
4484// CatchReturnInst Class
4485//===----------------------------------------------------------------------===//
4486
4487class CatchReturnInst : public Instruction {
4488 CatchReturnInst(const CatchReturnInst &RI);
4489 CatchReturnInst(Value *CatchPad, BasicBlock *BB, Instruction *InsertBefore);
4490 CatchReturnInst(Value *CatchPad, BasicBlock *BB, BasicBlock *InsertAtEnd);
4491
4492 void init(Value *CatchPad, BasicBlock *BB);
4493
4494protected:
4495 // Note: Instruction needs to be a friend here to call cloneImpl.
4496 friend class Instruction;
4497
4498 CatchReturnInst *cloneImpl() const;
4499
4500public:
4501 static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB,
4502 Instruction *InsertBefore = nullptr) {
4503 assert(CatchPad)((CatchPad) ? static_cast<void> (0) : __assert_fail ("CatchPad"
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4503, __PRETTY_FUNCTION__))
;
4504 assert(BB)((BB) ? static_cast<void> (0) : __assert_fail ("BB", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4504, __PRETTY_FUNCTION__))
;
4505 return new (2) CatchReturnInst(CatchPad, BB, InsertBefore);
4506 }
4507
4508 static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB,
4509 BasicBlock *InsertAtEnd) {
4510 assert(CatchPad)((CatchPad) ? static_cast<void> (0) : __assert_fail ("CatchPad"
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4510, __PRETTY_FUNCTION__))
;
4511 assert(BB)((BB) ? static_cast<void> (0) : __assert_fail ("BB", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4511, __PRETTY_FUNCTION__))
;
4512 return new (2) CatchReturnInst(CatchPad, BB, InsertAtEnd);
4513 }
4514
4515 /// Provide fast operand accessors
4516 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
;
4517
4518 /// Convenience accessors.
4519 CatchPadInst *getCatchPad() const { return cast<CatchPadInst>(Op<0>()); }
4520 void setCatchPad(CatchPadInst *CatchPad) {
4521 assert(CatchPad)((CatchPad) ? static_cast<void> (0) : __assert_fail ("CatchPad"
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4521, __PRETTY_FUNCTION__))
;
4522 Op<0>() = CatchPad;
4523 }
4524
4525 BasicBlock *getSuccessor() const { return cast<BasicBlock>(Op<1>()); }
4526 void setSuccessor(BasicBlock *NewSucc) {
4527 assert(NewSucc)((NewSucc) ? static_cast<void> (0) : __assert_fail ("NewSucc"
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4527, __PRETTY_FUNCTION__))
;
4528 Op<1>() = NewSucc;
4529 }
4530 unsigned getNumSuccessors() const { return 1; }
4531
4532 /// Get the parentPad of this catchret's catchpad's catchswitch.
4533 /// The successor block is implicitly a member of this funclet.
4534 Value *getCatchSwitchParentPad() const {
4535 return getCatchPad()->getCatchSwitch()->getParentPad();
4536 }
4537
4538 // Methods for support type inquiry through isa, cast, and dyn_cast:
4539 static bool classof(const Instruction *I) {
4540 return (I->getOpcode() == Instruction::CatchRet);
4541 }
4542 static bool classof(const Value *V) {
4543 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4544 }
4545
4546private:
4547 BasicBlock *getSuccessor(unsigned Idx) const {
4548 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-10~svn374877/include/llvm/IR/Instructions.h"
, 4548, __PRETTY_FUNCTION__))
;
4549 return getSuccessor();
4550 }
4551
4552 void setSuccessor(unsigned Idx, BasicBlock *B) {
4553 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-10~svn374877/include/llvm/IR/Instructions.h"
, 4553, __PRETTY_FUNCTION__))
;
4554 setSuccessor(B);
4555 }
4556};
4557
4558template <>
4559struct OperandTraits<CatchReturnInst>
4560 : public FixedNumOperandTraits<CatchReturnInst, 2> {};
4561
4562DEFINE_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-10~svn374877/include/llvm/IR/Instructions.h"
, 4562, __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-10~svn374877/include/llvm/IR/Instructions.h"
, 4562, __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); }
4563
4564//===----------------------------------------------------------------------===//
4565// CleanupReturnInst Class
4566//===----------------------------------------------------------------------===//
4567
4568class CleanupReturnInst : public Instruction {
4569private:
4570 CleanupReturnInst(const CleanupReturnInst &RI);
4571 CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values,
4572 Instruction *InsertBefore = nullptr);
4573 CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values,
4574 BasicBlock *InsertAtEnd);
4575
4576 void init(Value *CleanupPad, BasicBlock *UnwindBB);
4577
4578protected:
4579 // Note: Instruction needs to be a friend here to call cloneImpl.
4580 friend class Instruction;
4581
4582 CleanupReturnInst *cloneImpl() const;
4583
4584public:
4585 static CleanupReturnInst *Create(Value *CleanupPad,
4586 BasicBlock *UnwindBB = nullptr,
4587 Instruction *InsertBefore = nullptr) {
4588 assert(CleanupPad)((CleanupPad) ? static_cast<void> (0) : __assert_fail (
"CleanupPad", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4588, __PRETTY_FUNCTION__))
;
4589 unsigned Values = 1;
4590 if (UnwindBB)
4591 ++Values;
4592 return new (Values)
4593 CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertBefore);
4594 }
4595
4596 static CleanupReturnInst *Create(Value *CleanupPad, BasicBlock *UnwindBB,
4597 BasicBlock *InsertAtEnd) {
4598 assert(CleanupPad)((CleanupPad) ? static_cast<void> (0) : __assert_fail (
"CleanupPad", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4598, __PRETTY_FUNCTION__))
;
4599 unsigned Values = 1;
4600 if (UnwindBB)
4601 ++Values;
4602 return new (Values)
4603 CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertAtEnd);
4604 }
4605
4606 /// Provide fast operand accessors
4607 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
;
4608
4609 bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; }
4610 bool unwindsToCaller() const { return !hasUnwindDest(); }
4611
4612 /// Convenience accessor.
4613 CleanupPadInst *getCleanupPad() const {
4614 return cast<CleanupPadInst>(Op<0>());
4615 }
4616 void setCleanupPad(CleanupPadInst *CleanupPad) {
4617 assert(CleanupPad)((CleanupPad) ? static_cast<void> (0) : __assert_fail (
"CleanupPad", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4617, __PRETTY_FUNCTION__))
;
4618 Op<0>() = CleanupPad;
4619 }
4620
4621 unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; }
4622
4623 BasicBlock *getUnwindDest() const {
4624 return hasUnwindDest() ? cast<BasicBlock>(Op<1>()) : nullptr;
4625 }
4626 void setUnwindDest(BasicBlock *NewDest) {
4627 assert(NewDest)((NewDest) ? static_cast<void> (0) : __assert_fail ("NewDest"
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4627, __PRETTY_FUNCTION__))
;
4628 assert(hasUnwindDest())((hasUnwindDest()) ? static_cast<void> (0) : __assert_fail
("hasUnwindDest()", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4628, __PRETTY_FUNCTION__))
;
4629 Op<1>() = NewDest;
4630 }
4631
4632 // Methods for support type inquiry through isa, cast, and dyn_cast:
4633 static bool classof(const Instruction *I) {
4634 return (I->getOpcode() == Instruction::CleanupRet);
4635 }
4636 static bool classof(const Value *V) {
4637 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4638 }
4639
4640private:
4641 BasicBlock *getSuccessor(unsigned Idx) const {
4642 assert(Idx == 0)((Idx == 0) ? static_cast<void> (0) : __assert_fail ("Idx == 0"
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4642, __PRETTY_FUNCTION__))
;
4643 return getUnwindDest();
4644 }
4645
4646 void setSuccessor(unsigned Idx, BasicBlock *B) {
4647 assert(Idx == 0)((Idx == 0) ? static_cast<void> (0) : __assert_fail ("Idx == 0"
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4647, __PRETTY_FUNCTION__))
;
4648 setUnwindDest(B);
4649 }
4650
4651 // Shadow Instruction::setInstructionSubclassData with a private forwarding
4652 // method so that subclasses cannot accidentally use it.
4653 void setInstructionSubclassData(unsigned short D) {
4654 Instruction::setInstructionSubclassData(D);
4655 }
4656};
4657
4658template <>
4659struct OperandTraits<CleanupReturnInst>
4660 : public VariadicOperandTraits<CleanupReturnInst, /*MINARITY=*/1> {};
4661
4662DEFINE_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-10~svn374877/include/llvm/IR/Instructions.h"
, 4662, __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-10~svn374877/include/llvm/IR/Instructions.h"
, 4662, __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); }
4663
4664//===----------------------------------------------------------------------===//
4665// UnreachableInst Class
4666//===----------------------------------------------------------------------===//
4667
4668//===---------------------------------------------------------------------------
4669/// This function has undefined behavior. In particular, the
4670/// presence of this instruction indicates some higher level knowledge that the
4671/// end of the block cannot be reached.
4672///
4673class UnreachableInst : public Instruction {
4674protected:
4675 // Note: Instruction needs to be a friend here to call cloneImpl.
4676 friend class Instruction;
4677
4678 UnreachableInst *cloneImpl() const;
4679
4680public:
4681 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr);
4682 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
4683
4684 // allocate space for exactly zero operands
4685 void *operator new(size_t s) {
4686 return User::operator new(s, 0);
4687 }
4688
4689 unsigned getNumSuccessors() const { return 0; }
4690
4691 // Methods for support type inquiry through isa, cast, and dyn_cast:
4692 static bool classof(const Instruction *I) {
4693 return I->getOpcode() == Instruction::Unreachable;
4694 }
4695 static bool classof(const Value *V) {
4696 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4697 }
4698
4699private:
4700 BasicBlock *getSuccessor(unsigned idx) const {
4701 llvm_unreachable("UnreachableInst has no successors!")::llvm::llvm_unreachable_internal("UnreachableInst has no successors!"
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4701)
;
4702 }
4703
4704 void setSuccessor(unsigned idx, BasicBlock *B) {
4705 llvm_unreachable("UnreachableInst has no successors!")::llvm::llvm_unreachable_internal("UnreachableInst has no successors!"
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h"
, 4705)
;
4706 }
4707};
4708
4709//===----------------------------------------------------------------------===//
4710// TruncInst Class
4711//===----------------------------------------------------------------------===//
4712
4713/// This class represents a truncation of integer types.
4714class TruncInst : public CastInst {
4715protected:
4716 // Note: Instruction needs to be a friend here to call cloneImpl.
4717 friend class Instruction;
4718
4719 /// Clone an identical TruncInst
4720 TruncInst *cloneImpl() const;
4721
4722public:
4723 /// Constructor with insert-before-instruction semantics
4724 TruncInst(
4725 Value *S, ///< The value to be truncated
4726 Type *Ty, ///< The (smaller) type to truncate to
4727 const Twine &NameStr = "", ///< A name for the new instruction
4728 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4729 );
4730
4731 /// Constructor with insert-at-end-of-block semantics
4732 TruncInst(
4733 Value *S, ///< The value to be truncated
4734 Type *Ty, ///< The (smaller) type to truncate to
4735 const Twine &NameStr, ///< A name for the new instruction
4736 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4737 );
4738
4739 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4740 static bool classof(const Instruction *I) {
4741 return I->getOpcode() == Trunc;
4742 }
4743 static bool classof(const Value *V) {
4744 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4745 }
4746};
4747
4748//===----------------------------------------------------------------------===//
4749// ZExtInst Class
4750//===----------------------------------------------------------------------===//
4751
4752/// This class represents zero extension of integer types.
4753class ZExtInst : public CastInst {
4754protected:
4755 // Note: Instruction needs to be a friend here to call cloneImpl.
4756 friend class Instruction;
4757
4758 /// Clone an identical ZExtInst
4759 ZExtInst *cloneImpl() const;
4760
4761public:
4762 /// Constructor with insert-before-instruction semantics
4763 ZExtInst(
4764 Value *S, ///< The value to be zero extended
4765 Type *Ty, ///< The type to zero extend to
4766 const Twine &NameStr = "", ///< A name for the new instruction
4767 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4768 );
4769
4770 /// Constructor with insert-at-end semantics.
4771 ZExtInst(
4772 Value *S, ///< The value to be zero extended
4773 Type *Ty, ///< The type to zero extend to
4774 const Twine &NameStr, ///< A name for the new instruction
4775 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4776 );
4777
4778 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4779 static bool classof(const Instruction *I) {
4780 return I->getOpcode() == ZExt;
4781 }
4782 static bool classof(const Value *V) {
4783 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4784 }
4785};
4786
4787//===----------------------------------------------------------------------===//
4788// SExtInst Class
4789//===----------------------------------------------------------------------===//
4790
4791/// This class represents a sign extension of integer types.
4792class SExtInst : public CastInst {
4793protected:
4794 // Note: Instruction needs to be a friend here to call cloneImpl.
4795 friend class Instruction;
4796
4797 /// Clone an identical SExtInst
4798 SExtInst *cloneImpl() const;
4799
4800public:
4801 /// Constructor with insert-before-instruction semantics
4802 SExtInst(
4803 Value *S, ///< The value to be sign extended
4804 Type *Ty, ///< The type to sign extend to
4805 const Twine &NameStr = "", ///< A name for the new instruction
4806 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4807 );
4808
4809 /// Constructor with insert-at-end-of-block semantics
4810 SExtInst(
4811 Value *S, ///< The value to be sign extended
4812 Type *Ty, ///< The type to sign extend to
4813 const Twine &NameStr, ///< A name for the new instruction
4814 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4815 );
4816
4817 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4818 static bool classof(const Instruction *I) {
4819 return I->getOpcode() == SExt;
4820 }
4821 static bool classof(const Value *V) {
4822 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4823 }
4824};
4825
4826//===----------------------------------------------------------------------===//
4827// FPTruncInst Class
4828//===----------------------------------------------------------------------===//
4829
4830/// This class represents a truncation of floating point types.
4831class FPTruncInst : public CastInst {
4832protected:
4833 // Note: Instruction needs to be a friend here to call cloneImpl.
4834 friend class Instruction;
4835
4836 /// Clone an identical FPTruncInst
4837 FPTruncInst *cloneImpl() const;
4838
4839public:
4840 /// Constructor with insert-before-instruction semantics
4841 FPTruncInst(
4842 Value *S, ///< The value to be truncated
4843 Type *Ty, ///< The type to truncate to
4844 const Twine &NameStr = "", ///< A name for the new instruction
4845 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4846 );
4847
4848 /// Constructor with insert-before-instruction semantics
4849 FPTruncInst(
4850 Value *S, ///< The value to be truncated
4851 Type *Ty, ///< The type to truncate to
4852 const Twine &NameStr, ///< A name for the new instruction
4853 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4854 );
4855
4856 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4857 static bool classof(const Instruction *I) {
4858 return I->getOpcode() == FPTrunc;
4859 }
4860 static bool classof(const Value *V) {
4861 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4862 }
4863};
4864
4865//===----------------------------------------------------------------------===//
4866// FPExtInst Class
4867//===----------------------------------------------------------------------===//
4868
4869/// This class represents an extension of floating point types.
4870class FPExtInst : public CastInst {
4871protected:
4872 // Note: Instruction needs to be a friend here to call cloneImpl.
4873 friend class Instruction;
4874
4875 /// Clone an identical FPExtInst
4876 FPExtInst *cloneImpl() const;
4877
4878public:
4879 /// Constructor with insert-before-instruction semantics
4880 FPExtInst(
4881 Value *S, ///< The value to be extended
4882 Type *Ty, ///< The type to extend to
4883 const Twine &NameStr = "", ///< A name for the new instruction
4884 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4885 );
4886
4887 /// Constructor with insert-at-end-of-block semantics
4888 FPExtInst(
4889 Value *S, ///< The value to be extended
4890 Type *Ty, ///< The type to extend to
4891 const Twine &NameStr, ///< A name for the new instruction
4892 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4893 );
4894
4895 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4896 static bool classof(const Instruction *I) {
4897 return I->getOpcode() == FPExt;
4898 }
4899 static bool classof(const Value *V) {
4900 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4901 }
4902};
4903
4904//===----------------------------------------------------------------------===//
4905// UIToFPInst Class
4906//===----------------------------------------------------------------------===//
4907
4908/// This class represents a cast unsigned integer to floating point.
4909class UIToFPInst : public CastInst {
4910protected:
4911 // Note: Instruction needs to be a friend here to call cloneImpl.
4912 friend class Instruction;
4913
4914 /// Clone an identical UIToFPInst
4915 UIToFPInst *cloneImpl() const;
4916
4917public:
4918 /// Constructor with insert-before-instruction semantics
4919 UIToFPInst(
4920 Value *S, ///< The value to be converted
4921 Type *Ty, ///< The type to convert to
4922 const Twine &NameStr = "", ///< A name for the new instruction
4923 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4924 );
4925
4926 /// Constructor with insert-at-end-of-block semantics
4927 UIToFPInst(
4928 Value *S, ///< The value to be converted
4929 Type *Ty, ///< The type to convert to
4930 const Twine &NameStr, ///< A name for the new instruction
4931 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4932 );
4933
4934 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4935 static bool classof(const Instruction *I) {
4936 return I->getOpcode() == UIToFP;
4937 }
4938 static bool classof(const Value *V) {
4939 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4940 }
4941};
4942
4943//===----------------------------------------------------------------------===//
4944// SIToFPInst Class
4945//===----------------------------------------------------------------------===//
4946
4947/// This class represents a cast from signed integer to floating point.
4948class SIToFPInst : public CastInst {
4949protected:
4950 // Note: Instruction needs to be a friend here to call cloneImpl.
4951 friend class Instruction;
4952
4953 /// Clone an identical SIToFPInst
4954 SIToFPInst *cloneImpl() const;
4955
4956public:
4957 /// Constructor with insert-before-instruction semantics
4958 SIToFPInst(
4959 Value *S, ///< The value to be converted
4960 Type *Ty, ///< The type to convert to
4961 const Twine &NameStr = "", ///< A name for the new instruction
4962 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4963 );
4964
4965 /// Constructor with insert-at-end-of-block semantics
4966 SIToFPInst(
4967 Value *S, ///< The value to be converted
4968 Type *Ty, ///< The type to convert to
4969 const Twine &NameStr, ///< A name for the new instruction
4970 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4971 );
4972
4973 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4974 static bool classof(const Instruction *I) {
4975 return I->getOpcode() == SIToFP;
4976 }
4977 static bool classof(const Value *V) {
4978 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4979 }
4980};
4981
4982//===----------------------------------------------------------------------===//
4983// FPToUIInst Class
4984//===----------------------------------------------------------------------===//
4985
4986/// This class represents a cast from floating point to unsigned integer
4987class FPToUIInst : public CastInst {
4988protected:
4989 // Note: Instruction needs to be a friend here to call cloneImpl.
4990 friend class Instruction;
4991
4992 /// Clone an identical FPToUIInst
4993 FPToUIInst *cloneImpl() const;
4994
4995public:
4996 /// Constructor with insert-before-instruction semantics
4997 FPToUIInst(
4998 Value *S, ///< The value to be converted
4999 Type *Ty, ///< The type to convert to
5000 const Twine &NameStr = "", ///< A name for the new instruction
5001 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5002 );
5003
5004 /// Constructor with insert-at-end-of-block semantics
5005 FPToUIInst(
5006 Value *S, ///< The value to be converted
5007 Type *Ty, ///< The type to convert to
5008 const Twine &NameStr, ///< A name for the new instruction
5009 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
5010 );
5011
5012 /// Methods for support type inquiry through isa, cast, and dyn_cast:
5013 static bool classof(const Instruction *I) {
5014 return I->getOpcode() == FPToUI;
5015 }
5016 static bool classof(const Value *V) {
5017 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5018 }
5019};
5020
5021//===----------------------------------------------------------------------===//
5022// FPToSIInst Class
5023//===----------------------------------------------------------------------===//
5024
5025/// This class represents a cast from floating point to signed integer.
5026class FPToSIInst : public CastInst {
5027protected:
5028 // Note: Instruction needs to be a friend here to call cloneImpl.
5029 friend class Instruction;
5030
5031 /// Clone an identical FPToSIInst
5032 FPToSIInst *cloneImpl() const;
5033
5034public:
5035 /// Constructor with insert-before-instruction semantics
5036 FPToSIInst(
5037 Value *S, ///< The value to be converted
5038 Type *Ty, ///< The type to convert to
5039 const Twine &NameStr = "", ///< A name for the new instruction
5040 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5041 );
5042
5043 /// Constructor with insert-at-end-of-block semantics
5044 FPToSIInst(
5045 Value *S, ///< The value to be converted
5046 Type *Ty, ///< The type to convert to
5047 const Twine &NameStr, ///< A name for the new instruction
5048 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5049 );
5050
5051 /// Methods for support type inquiry through isa, cast, and dyn_cast:
5052 static bool classof(const Instruction *I) {
5053 return I->getOpcode() == FPToSI;
5054 }
5055 static bool classof(const Value *V) {
5056 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5057 }
5058};
5059
5060//===----------------------------------------------------------------------===//
5061// IntToPtrInst Class
5062//===----------------------------------------------------------------------===//
5063
5064/// This class represents a cast from an integer to a pointer.
5065class IntToPtrInst : public CastInst {
5066public:
5067 // Note: Instruction needs to be a friend here to call cloneImpl.
5068 friend class Instruction;
5069
5070 /// Constructor with insert-before-instruction semantics
5071 IntToPtrInst(
5072 Value *S, ///< The value to be converted
5073 Type *Ty, ///< The type to convert to
5074 const Twine &NameStr = "", ///< A name for the new instruction
5075 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5076 );
5077
5078 /// Constructor with insert-at-end-of-block semantics
5079 IntToPtrInst(
5080 Value *S, ///< The value to be converted
5081 Type *Ty, ///< The type to convert to
5082 const Twine &NameStr, ///< A name for the new instruction
5083 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5084 );
5085
5086 /// Clone an identical IntToPtrInst.
5087 IntToPtrInst *cloneImpl() const;
5088
5089 /// Returns the address space of this instruction's pointer type.
5090 unsigned getAddressSpace() const {
5091 return getType()->getPointerAddressSpace();
5092 }
5093
5094 // Methods for support type inquiry through isa, cast, and dyn_cast:
5095 static bool classof(const Instruction *I) {
5096 return I->getOpcode() == IntToPtr;
5097 }
5098 static bool classof(const Value *V) {
5099 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5100 }
5101};
5102
5103//===----------------------------------------------------------------------===//
5104// PtrToIntInst Class
5105//===----------------------------------------------------------------------===//
5106
5107/// This class represents a cast from a pointer to an integer.
5108class PtrToIntInst : public CastInst {
5109protected:
5110 // Note: Instruction needs to be a friend here to call cloneImpl.
5111 friend class Instruction;
5112
5113 /// Clone an identical PtrToIntInst.
5114 PtrToIntInst *cloneImpl() const;
5115
5116public:
5117 /// Constructor with insert-before-instruction semantics
5118 PtrToIntInst(
5119 Value *S, ///< The value to be converted
5120 Type *Ty, ///< The type to convert to
5121 const Twine &NameStr = "", ///< A name for the new instruction
5122 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5123 );
5124
5125 /// Constructor with insert-at-end-of-block semantics
5126 PtrToIntInst(
5127 Value *S, ///< The value to be converted
5128 Type *Ty, ///< The type to convert to
5129 const Twine &NameStr, ///< A name for the new instruction
5130 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5131 );
5132
5133 /// Gets the pointer operand.
5134 Value *getPointerOperand() { return getOperand(0); }
5135 /// Gets the pointer operand.
5136 const Value *getPointerOperand() const { return getOperand(0); }
5137 /// Gets the operand index of the pointer operand.
5138 static unsigned getPointerOperandIndex() { return 0U; }
5139
5140 /// Returns the address space of the pointer operand.
5141 unsigned getPointerAddressSpace() const {
5142 return getPointerOperand()->getType()->getPointerAddressSpace();
5143 }
5144
5145 // Methods for support type inquiry through isa, cast, and dyn_cast:
5146 static bool classof(const Instruction *I) {
5147 return I->getOpcode() == PtrToInt;
5148 }
5149 static bool classof(const Value *V) {
5150 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5151 }
5152};
5153
5154//===----------------------------------------------------------------------===//
5155// BitCastInst Class
5156//===----------------------------------------------------------------------===//
5157
5158/// This class represents a no-op cast from one type to another.
5159class BitCastInst : public CastInst {
5160protected:
5161 // Note: Instruction needs to be a friend here to call cloneImpl.
5162 friend class Instruction;
5163
5164 /// Clone an identical BitCastInst.
5165 BitCastInst *cloneImpl() const;
5166
5167public:
5168 /// Constructor with insert-before-instruction semantics
5169 BitCastInst(
5170 Value *S, ///< The value to be casted
5171 Type *Ty, ///< The type to casted to
5172 const Twine &NameStr = "", ///< A name for the new instruction
5173 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5174 );
5175
5176 /// Constructor with insert-at-end-of-block semantics
5177 BitCastInst(
5178 Value *S, ///< The value to be casted
5179 Type *Ty, ///< The type to casted to
5180 const Twine &NameStr, ///< A name for the new instruction
5181 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5182 );
5183
5184 // Methods for support type inquiry through isa, cast, and dyn_cast:
5185 static bool classof(const Instruction *I) {
5186 return I->getOpcode() == BitCast;
5187 }
5188 static bool classof(const Value *V) {
5189 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5190 }
5191};
5192
5193//===----------------------------------------------------------------------===//
5194// AddrSpaceCastInst Class
5195//===----------------------------------------------------------------------===//
5196
5197/// This class represents a conversion between pointers from one address space
5198/// to another.
5199class AddrSpaceCastInst : public CastInst {
5200protected:
5201 // Note: Instruction needs to be a friend here to call cloneImpl.
5202 friend class Instruction;
5203
5204 /// Clone an identical AddrSpaceCastInst.
5205 AddrSpaceCastInst *cloneImpl() const;
5206
5207public:
5208 /// Constructor with insert-before-instruction semantics
5209 AddrSpaceCastInst(
5210 Value *S, ///< The value to be casted
5211 Type *Ty, ///< The type to casted to
5212 const Twine &NameStr = "", ///< A name for the new instruction
5213 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5214 );
5215
5216 /// Constructor with insert-at-end-of-block semantics
5217 AddrSpaceCastInst(
5218 Value *S, ///< The value to be casted
5219 Type *Ty, ///< The type to casted to
5220 const Twine &NameStr, ///< A name for the new instruction
5221 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5222 );
5223
5224 // Methods for support type inquiry through isa, cast, and dyn_cast:
5225 static bool classof(const Instruction *I) {
5226 return I->getOpcode() == AddrSpaceCast;
5227 }
5228 static bool classof(const Value *V) {
5229 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5230 }
5231
5232 /// Gets the pointer operand.
5233 Value *getPointerOperand() {
5234 return getOperand(0);
5235 }
5236
5237 /// Gets the pointer operand.
5238 const Value *getPointerOperand() const {
5239 return getOperand(0);
5240 }
5241
5242 /// Gets the operand index of the pointer operand.
5243 static unsigned getPointerOperandIndex() {
5244 return 0U;
5245 }
5246
5247 /// Returns the address space of the pointer operand.
5248 unsigned getSrcAddressSpace() const {
5249 return getPointerOperand()->getType()->getPointerAddressSpace();
5250 }
5251
5252 /// Returns the address space of the result.
5253 unsigned getDestAddressSpace() const {
5254 return getType()->getPointerAddressSpace();
5255 }
5256};
5257
5258/// A helper function that returns the pointer operand of a load or store
5259/// instruction. Returns nullptr if not load or store.
5260inline const Value *getLoadStorePointerOperand(const Value *V) {
5261 if (auto *Load = dyn_cast<LoadInst>(V))
5262 return Load->getPointerOperand();
5263 if (auto *Store = dyn_cast<StoreInst>(V))
5264 return Store->getPointerOperand();
5265 return nullptr;
5266}
5267inline Value *getLoadStorePointerOperand(Value *V) {
5268 return const_cast<Value *>(
5269 getLoadStorePointerOperand(static_cast<const Value *>(V)));
5270}
5271
5272/// A helper function that returns the pointer operand of a load, store
5273/// or GEP instruction. Returns nullptr if not load, store, or GEP.
5274inline const Value *getPointerOperand(const Value *V) {
5275 if (auto *Ptr = getLoadStorePointerOperand(V))
5276 return Ptr;
5277 if (auto *Gep = dyn_cast<GetElementPtrInst>(V))
5278 return Gep->getPointerOperand();
5279 return nullptr;
5280}
5281inline Value *getPointerOperand(Value *V) {
5282 return const_cast<Value *>(getPointerOperand(static_cast<const Value *>(V)));
5283}
5284
5285/// A helper function that returns the alignment of load or store instruction.
5286inline unsigned getLoadStoreAlignment(Value *I) {
5287 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-10~svn374877/include/llvm/IR/Instructions.h"
, 5288, __PRETTY_FUNCTION__))
5288 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 5288, __PRETTY_FUNCTION__))
;
5289 if (auto *LI = dyn_cast<LoadInst>(I))
5290 return LI->getAlignment();
5291 return cast<StoreInst>(I)->getAlignment();
5292}
5293
5294/// A helper function that returns the address space of the pointer operand of
5295/// load or store instruction.
5296inline unsigned getLoadStoreAddressSpace(Value *I) {
5297 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-10~svn374877/include/llvm/IR/Instructions.h"
, 5298, __PRETTY_FUNCTION__))
5298 "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-10~svn374877/include/llvm/IR/Instructions.h"
, 5298, __PRETTY_FUNCTION__))
;
5299 if (auto *LI = dyn_cast<LoadInst>(I))
5300 return LI->getPointerAddressSpace();
5301 return cast<StoreInst>(I)->getPointerAddressSpace();
5302}
5303
5304} // end namespace llvm
5305
5306#endif // LLVM_IR_INSTRUCTIONS_H