Bug Summary

File:build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/lib/CodeGen/WinEHPrepare.cpp
Warning:line 212, column 30
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name WinEHPrepare.cpp -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 -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm -resource-dir /usr/lib/llvm-16/lib/clang/16.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I lib/CodeGen -I /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/lib/CodeGen -I include -I /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-16/lib/clang/16.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm=build-llvm -fmacro-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm=build-llvm -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -Wno-misleading-indentation -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm=build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-09-04-125545-48738-1 -x c++ /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/lib/CodeGen/WinEHPrepare.cpp

/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/lib/CodeGen/WinEHPrepare.cpp

1//===-- WinEHPrepare - Prepare exception handling for code generation ---===//
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 pass lowers LLVM IR exception handling into something closer to what the
10// backend wants for functions using a personality function from a runtime
11// provided by MSVC. Functions with other personality functions are left alone
12// and may be prepared by other passes. In particular, all supported MSVC
13// personality functions require cleanup code to be outlined, and the C++
14// personality requires catch handler code to be outlined.
15//
16//===----------------------------------------------------------------------===//
17
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/MapVector.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/Triple.h"
22#include "llvm/Analysis/EHPersonalities.h"
23#include "llvm/CodeGen/MachineBasicBlock.h"
24#include "llvm/CodeGen/Passes.h"
25#include "llvm/CodeGen/WinEHFuncInfo.h"
26#include "llvm/IR/Constants.h"
27#include "llvm/IR/Instructions.h"
28#include "llvm/IR/Verifier.h"
29#include "llvm/InitializePasses.h"
30#include "llvm/Pass.h"
31#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/Support/raw_ostream.h"
34#include "llvm/Transforms/Utils/BasicBlockUtils.h"
35#include "llvm/Transforms/Utils/Cloning.h"
36#include "llvm/Transforms/Utils/Local.h"
37#include "llvm/Transforms/Utils/SSAUpdater.h"
38
39using namespace llvm;
40
41#define DEBUG_TYPE"winehprepare" "winehprepare"
42
43static cl::opt<bool> DisableDemotion(
44 "disable-demotion", cl::Hidden,
45 cl::desc(
46 "Clone multicolor basic blocks but do not demote cross scopes"),
47 cl::init(false));
48
49static cl::opt<bool> DisableCleanups(
50 "disable-cleanups", cl::Hidden,
51 cl::desc("Do not remove implausible terminators or other similar cleanups"),
52 cl::init(false));
53
54static cl::opt<bool> DemoteCatchSwitchPHIOnlyOpt(
55 "demote-catchswitch-only", cl::Hidden,
56 cl::desc("Demote catchswitch BBs only (for wasm EH)"), cl::init(false));
57
58namespace {
59
60class WinEHPrepare : public FunctionPass {
61public:
62 static char ID; // Pass identification, replacement for typeid.
63 WinEHPrepare(bool DemoteCatchSwitchPHIOnly = false)
64 : FunctionPass(ID), DemoteCatchSwitchPHIOnly(DemoteCatchSwitchPHIOnly) {}
65
66 bool runOnFunction(Function &Fn) override;
67
68 bool doFinalization(Module &M) override;
69
70 void getAnalysisUsage(AnalysisUsage &AU) const override;
71
72 StringRef getPassName() const override {
73 return "Windows exception handling preparation";
74 }
75
76private:
77 void insertPHIStores(PHINode *OriginalPHI, AllocaInst *SpillSlot);
78 void
79 insertPHIStore(BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
80 SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist);
81 AllocaInst *insertPHILoads(PHINode *PN, Function &F);
82 void replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
83 DenseMap<BasicBlock *, Value *> &Loads, Function &F);
84 bool prepareExplicitEH(Function &F);
85 void colorFunclets(Function &F);
86
87 void demotePHIsOnFunclets(Function &F, bool DemoteCatchSwitchPHIOnly);
88 void cloneCommonBlocks(Function &F);
89 void removeImplausibleInstructions(Function &F);
90 void cleanupPreparedFunclets(Function &F);
91 void verifyPreparedFunclets(Function &F);
92
93 bool DemoteCatchSwitchPHIOnly;
94
95 // All fields are reset by runOnFunction.
96 EHPersonality Personality = EHPersonality::Unknown;
97
98 const DataLayout *DL = nullptr;
99 DenseMap<BasicBlock *, ColorVector> BlockColors;
100 MapVector<BasicBlock *, std::vector<BasicBlock *>> FuncletBlocks;
101};
102
103} // end anonymous namespace
104
105char WinEHPrepare::ID = 0;
106INITIALIZE_PASS(WinEHPrepare, DEBUG_TYPE, "Prepare Windows exceptions",static void *initializeWinEHPreparePassOnce(PassRegistry &
Registry) { PassInfo *PI = new PassInfo( "Prepare Windows exceptions"
, "winehprepare", &WinEHPrepare::ID, PassInfo::NormalCtor_t
(callDefaultCtor<WinEHPrepare>), false, false); Registry
.registerPass(*PI, true); return PI; } static llvm::once_flag
InitializeWinEHPreparePassFlag; void llvm::initializeWinEHPreparePass
(PassRegistry &Registry) { llvm::call_once(InitializeWinEHPreparePassFlag
, initializeWinEHPreparePassOnce, std::ref(Registry)); }
107 false, false)static void *initializeWinEHPreparePassOnce(PassRegistry &
Registry) { PassInfo *PI = new PassInfo( "Prepare Windows exceptions"
, "winehprepare", &WinEHPrepare::ID, PassInfo::NormalCtor_t
(callDefaultCtor<WinEHPrepare>), false, false); Registry
.registerPass(*PI, true); return PI; } static llvm::once_flag
InitializeWinEHPreparePassFlag; void llvm::initializeWinEHPreparePass
(PassRegistry &Registry) { llvm::call_once(InitializeWinEHPreparePassFlag
, initializeWinEHPreparePassOnce, std::ref(Registry)); }
108
109FunctionPass *llvm::createWinEHPass(bool DemoteCatchSwitchPHIOnly) {
110 return new WinEHPrepare(DemoteCatchSwitchPHIOnly);
111}
112
113bool WinEHPrepare::runOnFunction(Function &Fn) {
114 if (!Fn.hasPersonalityFn())
115 return false;
116
117 // Classify the personality to see what kind of preparation we need.
118 Personality = classifyEHPersonality(Fn.getPersonalityFn());
119
120 // Do nothing if this is not a scope-based personality.
121 if (!isScopedEHPersonality(Personality))
122 return false;
123
124 DL = &Fn.getParent()->getDataLayout();
125 return prepareExplicitEH(Fn);
126}
127
128bool WinEHPrepare::doFinalization(Module &M) { return false; }
129
130void WinEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {}
131
132static int addUnwindMapEntry(WinEHFuncInfo &FuncInfo, int ToState,
133 const BasicBlock *BB) {
134 CxxUnwindMapEntry UME;
135 UME.ToState = ToState;
136 UME.Cleanup = BB;
137 FuncInfo.CxxUnwindMap.push_back(UME);
138 return FuncInfo.getLastStateNumber();
139}
140
141static void addTryBlockMapEntry(WinEHFuncInfo &FuncInfo, int TryLow,
142 int TryHigh, int CatchHigh,
143 ArrayRef<const CatchPadInst *> Handlers) {
144 WinEHTryBlockMapEntry TBME;
145 TBME.TryLow = TryLow;
146 TBME.TryHigh = TryHigh;
147 TBME.CatchHigh = CatchHigh;
148 assert(TBME.TryLow <= TBME.TryHigh)(static_cast <bool> (TBME.TryLow <= TBME.TryHigh) ? void
(0) : __assert_fail ("TBME.TryLow <= TBME.TryHigh", "llvm/lib/CodeGen/WinEHPrepare.cpp"
, 148, __extension__ __PRETTY_FUNCTION__))
;
149 for (const CatchPadInst *CPI : Handlers) {
150 WinEHHandlerType HT;
151 Constant *TypeInfo = cast<Constant>(CPI->getArgOperand(0));
152 if (TypeInfo->isNullValue())
153 HT.TypeDescriptor = nullptr;
154 else
155 HT.TypeDescriptor = cast<GlobalVariable>(TypeInfo->stripPointerCasts());
156 HT.Adjectives = cast<ConstantInt>(CPI->getArgOperand(1))->getZExtValue();
157 HT.Handler = CPI->getParent();
158 if (auto *AI =
159 dyn_cast<AllocaInst>(CPI->getArgOperand(2)->stripPointerCasts()))
160 HT.CatchObj.Alloca = AI;
161 else
162 HT.CatchObj.Alloca = nullptr;
163 TBME.HandlerArray.push_back(HT);
164 }
165 FuncInfo.TryBlockMap.push_back(TBME);
166}
167
168static BasicBlock *getCleanupRetUnwindDest(const CleanupPadInst *CleanupPad) {
169 for (const User *U : CleanupPad->users())
170 if (const auto *CRI = dyn_cast<CleanupReturnInst>(U))
171 return CRI->getUnwindDest();
172 return nullptr;
173}
174
175static void calculateStateNumbersForInvokes(const Function *Fn,
176 WinEHFuncInfo &FuncInfo) {
177 auto *F = const_cast<Function *>(Fn);
178 DenseMap<BasicBlock *, ColorVector> BlockColors = colorEHFunclets(*F);
179 for (BasicBlock &BB : *F) {
180 auto *II = dyn_cast<InvokeInst>(BB.getTerminator());
5
Assuming the object is a 'CastReturnType'
181 if (!II
5.1
'II' is non-null
5.1
'II' is non-null
5.1
'II' is non-null
)
6
Taking false branch
182 continue;
183
184 auto &BBColors = BlockColors[&BB];
185 assert(BBColors.size() == 1 && "multi-color BB not removed by preparation")(static_cast <bool> (BBColors.size() == 1 && "multi-color BB not removed by preparation"
) ? void (0) : __assert_fail ("BBColors.size() == 1 && \"multi-color BB not removed by preparation\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 185, __extension__ __PRETTY_FUNCTION__
))
;
7
Assuming the condition is true
8
'?' condition is true
186 BasicBlock *FuncletEntryBB = BBColors.front();
187
188 BasicBlock *FuncletUnwindDest;
189 auto *FuncletPad =
190 dyn_cast<FuncletPadInst>(FuncletEntryBB->getFirstNonPHI());
9
Assuming the object is not a 'CastReturnType'
191 assert(FuncletPad || FuncletEntryBB == &Fn->getEntryBlock())(static_cast <bool> (FuncletPad || FuncletEntryBB == &
Fn->getEntryBlock()) ? void (0) : __assert_fail ("FuncletPad || FuncletEntryBB == &Fn->getEntryBlock()"
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 191, __extension__ __PRETTY_FUNCTION__
))
;
10
Assuming the condition is true
11
'?' condition is true
192 if (!FuncletPad
11.1
'FuncletPad' is null
11.1
'FuncletPad' is null
11.1
'FuncletPad' is null
)
12
Taking true branch
193 FuncletUnwindDest = nullptr;
194 else if (auto *CatchPad = dyn_cast<CatchPadInst>(FuncletPad))
195 FuncletUnwindDest = CatchPad->getCatchSwitch()->getUnwindDest();
196 else if (auto *CleanupPad = dyn_cast<CleanupPadInst>(FuncletPad))
197 FuncletUnwindDest = getCleanupRetUnwindDest(CleanupPad);
198 else
199 llvm_unreachable("unexpected funclet pad!")::llvm::llvm_unreachable_internal("unexpected funclet pad!", "llvm/lib/CodeGen/WinEHPrepare.cpp"
, 199)
;
200
201 BasicBlock *InvokeUnwindDest = II->getUnwindDest();
13
Calling 'InvokeInst::getUnwindDest'
29
Returning from 'InvokeInst::getUnwindDest'
30
'InvokeUnwindDest' initialized here
202 int BaseState = -1;
203 if (FuncletUnwindDest == InvokeUnwindDest) {
31
Assuming 'FuncletUnwindDest' is equal to 'InvokeUnwindDest'
32
Taking true branch
204 auto BaseStateI = FuncInfo.FuncletBaseStateMap.find(FuncletPad);
205 if (BaseStateI != FuncInfo.FuncletBaseStateMap.end())
33
Taking false branch
206 BaseState = BaseStateI->second;
207 }
208
209 if (BaseState != -1) {
34
Taking false branch
210 FuncInfo.InvokeStateMap[II] = BaseState;
211 } else {
212 Instruction *PadInst = InvokeUnwindDest->getFirstNonPHI();
35
Called C++ object pointer is null
213 assert(FuncInfo.EHPadStateMap.count(PadInst) && "EH Pad has no state!")(static_cast <bool> (FuncInfo.EHPadStateMap.count(PadInst
) && "EH Pad has no state!") ? void (0) : __assert_fail
("FuncInfo.EHPadStateMap.count(PadInst) && \"EH Pad has no state!\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 213, __extension__ __PRETTY_FUNCTION__
))
;
214 FuncInfo.InvokeStateMap[II] = FuncInfo.EHPadStateMap[PadInst];
215 }
216 }
217}
218
219// Given BB which ends in an unwind edge, return the EHPad that this BB belongs
220// to. If the unwind edge came from an invoke, return null.
221static const BasicBlock *getEHPadFromPredecessor(const BasicBlock *BB,
222 Value *ParentPad) {
223 const Instruction *TI = BB->getTerminator();
224 if (isa<InvokeInst>(TI))
225 return nullptr;
226 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(TI)) {
227 if (CatchSwitch->getParentPad() != ParentPad)
228 return nullptr;
229 return BB;
230 }
231 assert(!TI->isEHPad() && "unexpected EHPad!")(static_cast <bool> (!TI->isEHPad() && "unexpected EHPad!"
) ? void (0) : __assert_fail ("!TI->isEHPad() && \"unexpected EHPad!\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 231, __extension__ __PRETTY_FUNCTION__
))
;
232 auto *CleanupPad = cast<CleanupReturnInst>(TI)->getCleanupPad();
233 if (CleanupPad->getParentPad() != ParentPad)
234 return nullptr;
235 return CleanupPad->getParent();
236}
237
238// Starting from a EHPad, Backward walk through control-flow graph
239// to produce two primary outputs:
240// FuncInfo.EHPadStateMap[] and FuncInfo.CxxUnwindMap[]
241static void calculateCXXStateNumbers(WinEHFuncInfo &FuncInfo,
242 const Instruction *FirstNonPHI,
243 int ParentState) {
244 const BasicBlock *BB = FirstNonPHI->getParent();
245 assert(BB->isEHPad() && "not a funclet!")(static_cast <bool> (BB->isEHPad() && "not a funclet!"
) ? void (0) : __assert_fail ("BB->isEHPad() && \"not a funclet!\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 245, __extension__ __PRETTY_FUNCTION__
))
;
246
247 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FirstNonPHI)) {
248 assert(FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 &&(static_cast <bool> (FuncInfo.EHPadStateMap.count(CatchSwitch
) == 0 && "shouldn't revist catch funclets!") ? void (
0) : __assert_fail ("FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 && \"shouldn't revist catch funclets!\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 249, __extension__ __PRETTY_FUNCTION__
))
249 "shouldn't revist catch funclets!")(static_cast <bool> (FuncInfo.EHPadStateMap.count(CatchSwitch
) == 0 && "shouldn't revist catch funclets!") ? void (
0) : __assert_fail ("FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 && \"shouldn't revist catch funclets!\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 249, __extension__ __PRETTY_FUNCTION__
))
;
250
251 SmallVector<const CatchPadInst *, 2> Handlers;
252 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
253 auto *CatchPad = cast<CatchPadInst>(CatchPadBB->getFirstNonPHI());
254 Handlers.push_back(CatchPad);
255 }
256 int TryLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
257 FuncInfo.EHPadStateMap[CatchSwitch] = TryLow;
258 for (const BasicBlock *PredBlock : predecessors(BB))
259 if ((PredBlock = getEHPadFromPredecessor(PredBlock,
260 CatchSwitch->getParentPad())))
261 calculateCXXStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(),
262 TryLow);
263 int CatchLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
264
265 // catchpads are separate funclets in C++ EH due to the way rethrow works.
266 int TryHigh = CatchLow - 1;
267
268 // MSVC FrameHandler3/4 on x64&Arm64 expect Catch Handlers in $tryMap$
269 // stored in pre-order (outer first, inner next), not post-order
270 // Add to map here. Fix the CatchHigh after children are processed
271 const Module *Mod = BB->getParent()->getParent();
272 bool IsPreOrder = Triple(Mod->getTargetTriple()).isArch64Bit();
273 if (IsPreOrder)
274 addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchLow, Handlers);
275 unsigned TBMEIdx = FuncInfo.TryBlockMap.size() - 1;
276
277 for (const auto *CatchPad : Handlers) {
278 FuncInfo.FuncletBaseStateMap[CatchPad] = CatchLow;
279 for (const User *U : CatchPad->users()) {
280 const auto *UserI = cast<Instruction>(U);
281 if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(UserI)) {
282 BasicBlock *UnwindDest = InnerCatchSwitch->getUnwindDest();
283 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest())
284 calculateCXXStateNumbers(FuncInfo, UserI, CatchLow);
285 }
286 if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(UserI)) {
287 BasicBlock *UnwindDest = getCleanupRetUnwindDest(InnerCleanupPad);
288 // If a nested cleanup pad reports a null unwind destination and the
289 // enclosing catch pad doesn't it must be post-dominated by an
290 // unreachable instruction.
291 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest())
292 calculateCXXStateNumbers(FuncInfo, UserI, CatchLow);
293 }
294 }
295 }
296 int CatchHigh = FuncInfo.getLastStateNumber();
297 // Now child Catches are processed, update CatchHigh
298 if (IsPreOrder)
299 FuncInfo.TryBlockMap[TBMEIdx].CatchHigh = CatchHigh;
300 else // PostOrder
301 addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchHigh, Handlers);
302
303 LLVM_DEBUG(dbgs() << "TryLow[" << BB->getName() << "]: " << TryLow << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare")) { dbgs() << "TryLow[" << BB->
getName() << "]: " << TryLow << '\n'; } } while
(false)
;
304 LLVM_DEBUG(dbgs() << "TryHigh[" << BB->getName() << "]: " << TryHighdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare")) { dbgs() << "TryHigh[" << BB->
getName() << "]: " << TryHigh << '\n'; } } while
(false)
305 << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare")) { dbgs() << "TryHigh[" << BB->
getName() << "]: " << TryHigh << '\n'; } } while
(false)
;
306 LLVM_DEBUG(dbgs() << "CatchHigh[" << BB->getName() << "]: " << CatchHighdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare")) { dbgs() << "CatchHigh[" << BB->
getName() << "]: " << CatchHigh << '\n'; } }
while (false)
307 << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare")) { dbgs() << "CatchHigh[" << BB->
getName() << "]: " << CatchHigh << '\n'; } }
while (false)
;
308 } else {
309 auto *CleanupPad = cast<CleanupPadInst>(FirstNonPHI);
310
311 // It's possible for a cleanup to be visited twice: it might have multiple
312 // cleanupret instructions.
313 if (FuncInfo.EHPadStateMap.count(CleanupPad))
314 return;
315
316 int CleanupState = addUnwindMapEntry(FuncInfo, ParentState, BB);
317 FuncInfo.EHPadStateMap[CleanupPad] = CleanupState;
318 LLVM_DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare")) { dbgs() << "Assigning state #" <<
CleanupState << " to BB " << BB->getName() <<
'\n'; } } while (false)
319 << BB->getName() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare")) { dbgs() << "Assigning state #" <<
CleanupState << " to BB " << BB->getName() <<
'\n'; } } while (false)
;
320 for (const BasicBlock *PredBlock : predecessors(BB)) {
321 if ((PredBlock = getEHPadFromPredecessor(PredBlock,
322 CleanupPad->getParentPad()))) {
323 calculateCXXStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(),
324 CleanupState);
325 }
326 }
327 for (const User *U : CleanupPad->users()) {
328 const auto *UserI = cast<Instruction>(U);
329 if (UserI->isEHPad())
330 report_fatal_error("Cleanup funclets for the MSVC++ personality cannot "
331 "contain exceptional actions");
332 }
333 }
334}
335
336static int addSEHExcept(WinEHFuncInfo &FuncInfo, int ParentState,
337 const Function *Filter, const BasicBlock *Handler) {
338 SEHUnwindMapEntry Entry;
339 Entry.ToState = ParentState;
340 Entry.IsFinally = false;
341 Entry.Filter = Filter;
342 Entry.Handler = Handler;
343 FuncInfo.SEHUnwindMap.push_back(Entry);
344 return FuncInfo.SEHUnwindMap.size() - 1;
345}
346
347static int addSEHFinally(WinEHFuncInfo &FuncInfo, int ParentState,
348 const BasicBlock *Handler) {
349 SEHUnwindMapEntry Entry;
350 Entry.ToState = ParentState;
351 Entry.IsFinally = true;
352 Entry.Filter = nullptr;
353 Entry.Handler = Handler;
354 FuncInfo.SEHUnwindMap.push_back(Entry);
355 return FuncInfo.SEHUnwindMap.size() - 1;
356}
357
358// Starting from a EHPad, Backward walk through control-flow graph
359// to produce two primary outputs:
360// FuncInfo.EHPadStateMap[] and FuncInfo.SEHUnwindMap[]
361static void calculateSEHStateNumbers(WinEHFuncInfo &FuncInfo,
362 const Instruction *FirstNonPHI,
363 int ParentState) {
364 const BasicBlock *BB = FirstNonPHI->getParent();
365 assert(BB->isEHPad() && "no a funclet!")(static_cast <bool> (BB->isEHPad() && "no a funclet!"
) ? void (0) : __assert_fail ("BB->isEHPad() && \"no a funclet!\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 365, __extension__ __PRETTY_FUNCTION__
))
;
366
367 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FirstNonPHI)) {
368 assert(FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 &&(static_cast <bool> (FuncInfo.EHPadStateMap.count(CatchSwitch
) == 0 && "shouldn't revist catch funclets!") ? void (
0) : __assert_fail ("FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 && \"shouldn't revist catch funclets!\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 369, __extension__ __PRETTY_FUNCTION__
))
369 "shouldn't revist catch funclets!")(static_cast <bool> (FuncInfo.EHPadStateMap.count(CatchSwitch
) == 0 && "shouldn't revist catch funclets!") ? void (
0) : __assert_fail ("FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 && \"shouldn't revist catch funclets!\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 369, __extension__ __PRETTY_FUNCTION__
))
;
370
371 // Extract the filter function and the __except basic block and create a
372 // state for them.
373 assert(CatchSwitch->getNumHandlers() == 1 &&(static_cast <bool> (CatchSwitch->getNumHandlers() ==
1 && "SEH doesn't have multiple handlers per __try")
? void (0) : __assert_fail ("CatchSwitch->getNumHandlers() == 1 && \"SEH doesn't have multiple handlers per __try\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 374, __extension__ __PRETTY_FUNCTION__
))
374 "SEH doesn't have multiple handlers per __try")(static_cast <bool> (CatchSwitch->getNumHandlers() ==
1 && "SEH doesn't have multiple handlers per __try")
? void (0) : __assert_fail ("CatchSwitch->getNumHandlers() == 1 && \"SEH doesn't have multiple handlers per __try\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 374, __extension__ __PRETTY_FUNCTION__
))
;
375 const auto *CatchPad =
376 cast<CatchPadInst>((*CatchSwitch->handler_begin())->getFirstNonPHI());
377 const BasicBlock *CatchPadBB = CatchPad->getParent();
378 const Constant *FilterOrNull =
379 cast<Constant>(CatchPad->getArgOperand(0)->stripPointerCasts());
380 const Function *Filter = dyn_cast<Function>(FilterOrNull);
381 assert((Filter || FilterOrNull->isNullValue()) &&(static_cast <bool> ((Filter || FilterOrNull->isNullValue
()) && "unexpected filter value") ? void (0) : __assert_fail
("(Filter || FilterOrNull->isNullValue()) && \"unexpected filter value\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 382, __extension__ __PRETTY_FUNCTION__
))
382 "unexpected filter value")(static_cast <bool> ((Filter || FilterOrNull->isNullValue
()) && "unexpected filter value") ? void (0) : __assert_fail
("(Filter || FilterOrNull->isNullValue()) && \"unexpected filter value\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 382, __extension__ __PRETTY_FUNCTION__
))
;
383 int TryState = addSEHExcept(FuncInfo, ParentState, Filter, CatchPadBB);
384
385 // Everything in the __try block uses TryState as its parent state.
386 FuncInfo.EHPadStateMap[CatchSwitch] = TryState;
387 LLVM_DEBUG(dbgs() << "Assigning state #" << TryState << " to BB "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare")) { dbgs() << "Assigning state #" <<
TryState << " to BB " << CatchPadBB->getName(
) << '\n'; } } while (false)
388 << CatchPadBB->getName() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare")) { dbgs() << "Assigning state #" <<
TryState << " to BB " << CatchPadBB->getName(
) << '\n'; } } while (false)
;
389 for (const BasicBlock *PredBlock : predecessors(BB))
390 if ((PredBlock = getEHPadFromPredecessor(PredBlock,
391 CatchSwitch->getParentPad())))
392 calculateSEHStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(),
393 TryState);
394
395 // Everything in the __except block unwinds to ParentState, just like code
396 // outside the __try.
397 for (const User *U : CatchPad->users()) {
398 const auto *UserI = cast<Instruction>(U);
399 if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(UserI)) {
400 BasicBlock *UnwindDest = InnerCatchSwitch->getUnwindDest();
401 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest())
402 calculateSEHStateNumbers(FuncInfo, UserI, ParentState);
403 }
404 if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(UserI)) {
405 BasicBlock *UnwindDest = getCleanupRetUnwindDest(InnerCleanupPad);
406 // If a nested cleanup pad reports a null unwind destination and the
407 // enclosing catch pad doesn't it must be post-dominated by an
408 // unreachable instruction.
409 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest())
410 calculateSEHStateNumbers(FuncInfo, UserI, ParentState);
411 }
412 }
413 } else {
414 auto *CleanupPad = cast<CleanupPadInst>(FirstNonPHI);
415
416 // It's possible for a cleanup to be visited twice: it might have multiple
417 // cleanupret instructions.
418 if (FuncInfo.EHPadStateMap.count(CleanupPad))
419 return;
420
421 int CleanupState = addSEHFinally(FuncInfo, ParentState, BB);
422 FuncInfo.EHPadStateMap[CleanupPad] = CleanupState;
423 LLVM_DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare")) { dbgs() << "Assigning state #" <<
CleanupState << " to BB " << BB->getName() <<
'\n'; } } while (false)
424 << BB->getName() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare")) { dbgs() << "Assigning state #" <<
CleanupState << " to BB " << BB->getName() <<
'\n'; } } while (false)
;
425 for (const BasicBlock *PredBlock : predecessors(BB))
426 if ((PredBlock =
427 getEHPadFromPredecessor(PredBlock, CleanupPad->getParentPad())))
428 calculateSEHStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(),
429 CleanupState);
430 for (const User *U : CleanupPad->users()) {
431 const auto *UserI = cast<Instruction>(U);
432 if (UserI->isEHPad())
433 report_fatal_error("Cleanup funclets for the SEH personality cannot "
434 "contain exceptional actions");
435 }
436 }
437}
438
439static bool isTopLevelPadForMSVC(const Instruction *EHPad) {
440 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(EHPad))
441 return isa<ConstantTokenNone>(CatchSwitch->getParentPad()) &&
442 CatchSwitch->unwindsToCaller();
443 if (auto *CleanupPad = dyn_cast<CleanupPadInst>(EHPad))
444 return isa<ConstantTokenNone>(CleanupPad->getParentPad()) &&
445 getCleanupRetUnwindDest(CleanupPad) == nullptr;
446 if (isa<CatchPadInst>(EHPad))
447 return false;
448 llvm_unreachable("unexpected EHPad!")::llvm::llvm_unreachable_internal("unexpected EHPad!", "llvm/lib/CodeGen/WinEHPrepare.cpp"
, 448)
;
449}
450
451void llvm::calculateSEHStateNumbers(const Function *Fn,
452 WinEHFuncInfo &FuncInfo) {
453 // Don't compute state numbers twice.
454 if (!FuncInfo.SEHUnwindMap.empty())
455 return;
456
457 for (const BasicBlock &BB : *Fn) {
458 if (!BB.isEHPad())
459 continue;
460 const Instruction *FirstNonPHI = BB.getFirstNonPHI();
461 if (!isTopLevelPadForMSVC(FirstNonPHI))
462 continue;
463 ::calculateSEHStateNumbers(FuncInfo, FirstNonPHI, -1);
464 }
465
466 calculateStateNumbersForInvokes(Fn, FuncInfo);
467}
468
469void llvm::calculateWinCXXEHStateNumbers(const Function *Fn,
470 WinEHFuncInfo &FuncInfo) {
471 // Return if it's already been done.
472 if (!FuncInfo.EHPadStateMap.empty())
473 return;
474
475 for (const BasicBlock &BB : *Fn) {
476 if (!BB.isEHPad())
477 continue;
478 const Instruction *FirstNonPHI = BB.getFirstNonPHI();
479 if (!isTopLevelPadForMSVC(FirstNonPHI))
480 continue;
481 calculateCXXStateNumbers(FuncInfo, FirstNonPHI, -1);
482 }
483
484 calculateStateNumbersForInvokes(Fn, FuncInfo);
485}
486
487static int addClrEHHandler(WinEHFuncInfo &FuncInfo, int HandlerParentState,
488 int TryParentState, ClrHandlerType HandlerType,
489 uint32_t TypeToken, const BasicBlock *Handler) {
490 ClrEHUnwindMapEntry Entry;
491 Entry.HandlerParentState = HandlerParentState;
492 Entry.TryParentState = TryParentState;
493 Entry.Handler = Handler;
494 Entry.HandlerType = HandlerType;
495 Entry.TypeToken = TypeToken;
496 FuncInfo.ClrEHUnwindMap.push_back(Entry);
497 return FuncInfo.ClrEHUnwindMap.size() - 1;
498}
499
500void llvm::calculateClrEHStateNumbers(const Function *Fn,
501 WinEHFuncInfo &FuncInfo) {
502 // Return if it's already been done.
503 if (!FuncInfo.EHPadStateMap.empty())
1
Assuming the condition is false
2
Taking false branch
504 return;
505
506 // This numbering assigns one state number to each catchpad and cleanuppad.
507 // It also computes two tree-like relations over states:
508 // 1) Each state has a "HandlerParentState", which is the state of the next
509 // outer handler enclosing this state's handler (same as nearest ancestor
510 // per the ParentPad linkage on EH pads, but skipping over catchswitches).
511 // 2) Each state has a "TryParentState", which:
512 // a) for a catchpad that's not the last handler on its catchswitch, is
513 // the state of the next catchpad on that catchswitch
514 // b) for all other pads, is the state of the pad whose try region is the
515 // next outer try region enclosing this state's try region. The "try
516 // regions are not present as such in the IR, but will be inferred
517 // based on the placement of invokes and pads which reach each other
518 // by exceptional exits
519 // Catchswitches do not get their own states, but each gets mapped to the
520 // state of its first catchpad.
521
522 // Step one: walk down from outermost to innermost funclets, assigning each
523 // catchpad and cleanuppad a state number. Add an entry to the
524 // ClrEHUnwindMap for each state, recording its HandlerParentState and
525 // handler attributes. Record the TryParentState as well for each catchpad
526 // that's not the last on its catchswitch, but initialize all other entries'
527 // TryParentStates to a sentinel -1 value that the next pass will update.
528
529 // Seed a worklist with pads that have no parent.
530 SmallVector<std::pair<const Instruction *, int>, 8> Worklist;
531 for (const BasicBlock &BB : *Fn) {
532 const Instruction *FirstNonPHI = BB.getFirstNonPHI();
533 const Value *ParentPad;
534 if (const auto *CPI = dyn_cast<CleanupPadInst>(FirstNonPHI))
535 ParentPad = CPI->getParentPad();
536 else if (const auto *CSI = dyn_cast<CatchSwitchInst>(FirstNonPHI))
537 ParentPad = CSI->getParentPad();
538 else
539 continue;
540 if (isa<ConstantTokenNone>(ParentPad))
541 Worklist.emplace_back(FirstNonPHI, -1);
542 }
543
544 // Use the worklist to visit all pads, from outer to inner. Record
545 // HandlerParentState for all pads. Record TryParentState only for catchpads
546 // that aren't the last on their catchswitch (setting all other entries'
547 // TryParentStates to an initial value of -1). This loop is also responsible
548 // for setting the EHPadStateMap entry for all catchpads, cleanuppads, and
549 // catchswitches.
550 while (!Worklist.empty()) {
3
Loop condition is false. Execution continues on line 603
551 const Instruction *Pad;
552 int HandlerParentState;
553 std::tie(Pad, HandlerParentState) = Worklist.pop_back_val();
554
555 if (const auto *Cleanup = dyn_cast<CleanupPadInst>(Pad)) {
556 // Create the entry for this cleanup with the appropriate handler
557 // properties. Finally and fault handlers are distinguished by arity.
558 ClrHandlerType HandlerType =
559 (Cleanup->getNumArgOperands() ? ClrHandlerType::Fault
560 : ClrHandlerType::Finally);
561 int CleanupState = addClrEHHandler(FuncInfo, HandlerParentState, -1,
562 HandlerType, 0, Pad->getParent());
563 // Queue any child EH pads on the worklist.
564 for (const User *U : Cleanup->users())
565 if (const auto *I = dyn_cast<Instruction>(U))
566 if (I->isEHPad())
567 Worklist.emplace_back(I, CleanupState);
568 // Remember this pad's state.
569 FuncInfo.EHPadStateMap[Cleanup] = CleanupState;
570 } else {
571 // Walk the handlers of this catchswitch in reverse order since all but
572 // the last need to set the following one as its TryParentState.
573 const auto *CatchSwitch = cast<CatchSwitchInst>(Pad);
574 int CatchState = -1, FollowerState = -1;
575 SmallVector<const BasicBlock *, 4> CatchBlocks(CatchSwitch->handlers());
576 for (const BasicBlock *CatchBlock : llvm::reverse(CatchBlocks)) {
577 // Create the entry for this catch with the appropriate handler
578 // properties.
579 const auto *Catch = cast<CatchPadInst>(CatchBlock->getFirstNonPHI());
580 uint32_t TypeToken = static_cast<uint32_t>(
581 cast<ConstantInt>(Catch->getArgOperand(0))->getZExtValue());
582 CatchState =
583 addClrEHHandler(FuncInfo, HandlerParentState, FollowerState,
584 ClrHandlerType::Catch, TypeToken, CatchBlock);
585 // Queue any child EH pads on the worklist.
586 for (const User *U : Catch->users())
587 if (const auto *I = dyn_cast<Instruction>(U))
588 if (I->isEHPad())
589 Worklist.emplace_back(I, CatchState);
590 // Remember this catch's state.
591 FuncInfo.EHPadStateMap[Catch] = CatchState;
592 FollowerState = CatchState;
593 }
594 // Associate the catchswitch with the state of its first catch.
595 assert(CatchSwitch->getNumHandlers())(static_cast <bool> (CatchSwitch->getNumHandlers()) ?
void (0) : __assert_fail ("CatchSwitch->getNumHandlers()"
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 595, __extension__ __PRETTY_FUNCTION__
))
;
596 FuncInfo.EHPadStateMap[CatchSwitch] = CatchState;
597 }
598 }
599
600 // Step two: record the TryParentState of each state. For cleanuppads that
601 // don't have cleanuprets, we may need to infer this from their child pads,
602 // so visit pads in descendant-most to ancestor-most order.
603 for (ClrEHUnwindMapEntry &Entry : llvm::reverse(FuncInfo.ClrEHUnwindMap)) {
604 const Instruction *Pad =
605 Entry.Handler.get<const BasicBlock *>()->getFirstNonPHI();
606 // For most pads, the TryParentState is the state associated with the
607 // unwind dest of exceptional exits from it.
608 const BasicBlock *UnwindDest;
609 if (const auto *Catch = dyn_cast<CatchPadInst>(Pad)) {
610 // If a catch is not the last in its catchswitch, its TryParentState is
611 // the state associated with the next catch in the switch, even though
612 // that's not the unwind dest of exceptions escaping the catch. Those
613 // cases were already assigned a TryParentState in the first pass, so
614 // skip them.
615 if (Entry.TryParentState != -1)
616 continue;
617 // Otherwise, get the unwind dest from the catchswitch.
618 UnwindDest = Catch->getCatchSwitch()->getUnwindDest();
619 } else {
620 const auto *Cleanup = cast<CleanupPadInst>(Pad);
621 UnwindDest = nullptr;
622 for (const User *U : Cleanup->users()) {
623 if (auto *CleanupRet = dyn_cast<CleanupReturnInst>(U)) {
624 // Common and unambiguous case -- cleanupret indicates cleanup's
625 // unwind dest.
626 UnwindDest = CleanupRet->getUnwindDest();
627 break;
628 }
629
630 // Get an unwind dest for the user
631 const BasicBlock *UserUnwindDest = nullptr;
632 if (auto *Invoke = dyn_cast<InvokeInst>(U)) {
633 UserUnwindDest = Invoke->getUnwindDest();
634 } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(U)) {
635 UserUnwindDest = CatchSwitch->getUnwindDest();
636 } else if (auto *ChildCleanup = dyn_cast<CleanupPadInst>(U)) {
637 int UserState = FuncInfo.EHPadStateMap[ChildCleanup];
638 int UserUnwindState =
639 FuncInfo.ClrEHUnwindMap[UserState].TryParentState;
640 if (UserUnwindState != -1)
641 UserUnwindDest = FuncInfo.ClrEHUnwindMap[UserUnwindState]
642 .Handler.get<const BasicBlock *>();
643 }
644
645 // Not having an unwind dest for this user might indicate that it
646 // doesn't unwind, so can't be taken as proof that the cleanup itself
647 // may unwind to caller (see e.g. SimplifyUnreachable and
648 // RemoveUnwindEdge).
649 if (!UserUnwindDest)
650 continue;
651
652 // Now we have an unwind dest for the user, but we need to see if it
653 // unwinds all the way out of the cleanup or if it stays within it.
654 const Instruction *UserUnwindPad = UserUnwindDest->getFirstNonPHI();
655 const Value *UserUnwindParent;
656 if (auto *CSI = dyn_cast<CatchSwitchInst>(UserUnwindPad))
657 UserUnwindParent = CSI->getParentPad();
658 else
659 UserUnwindParent =
660 cast<CleanupPadInst>(UserUnwindPad)->getParentPad();
661
662 // The unwind stays within the cleanup iff it targets a child of the
663 // cleanup.
664 if (UserUnwindParent == Cleanup)
665 continue;
666
667 // This unwind exits the cleanup, so its dest is the cleanup's dest.
668 UnwindDest = UserUnwindDest;
669 break;
670 }
671 }
672
673 // Record the state of the unwind dest as the TryParentState.
674 int UnwindDestState;
675
676 // If UnwindDest is null at this point, either the pad in question can
677 // be exited by unwind to caller, or it cannot be exited by unwind. In
678 // either case, reporting such cases as unwinding to caller is correct.
679 // This can lead to EH tables that "look strange" -- if this pad's is in
680 // a parent funclet which has other children that do unwind to an enclosing
681 // pad, the try region for this pad will be missing the "duplicate" EH
682 // clause entries that you'd expect to see covering the whole parent. That
683 // should be benign, since the unwind never actually happens. If it were
684 // an issue, we could add a subsequent pass that pushes unwind dests down
685 // from parents that have them to children that appear to unwind to caller.
686 if (!UnwindDest) {
687 UnwindDestState = -1;
688 } else {
689 UnwindDestState = FuncInfo.EHPadStateMap[UnwindDest->getFirstNonPHI()];
690 }
691
692 Entry.TryParentState = UnwindDestState;
693 }
694
695 // Step three: transfer information from pads to invokes.
696 calculateStateNumbersForInvokes(Fn, FuncInfo);
4
Calling 'calculateStateNumbersForInvokes'
697}
698
699void WinEHPrepare::colorFunclets(Function &F) {
700 BlockColors = colorEHFunclets(F);
701
702 // Invert the map from BB to colors to color to BBs.
703 for (BasicBlock &BB : F) {
704 ColorVector &Colors = BlockColors[&BB];
705 for (BasicBlock *Color : Colors)
706 FuncletBlocks[Color].push_back(&BB);
707 }
708}
709
710void WinEHPrepare::demotePHIsOnFunclets(Function &F,
711 bool DemoteCatchSwitchPHIOnly) {
712 // Strip PHI nodes off of EH pads.
713 SmallVector<PHINode *, 16> PHINodes;
714 for (BasicBlock &BB : make_early_inc_range(F)) {
715 if (!BB.isEHPad())
716 continue;
717 if (DemoteCatchSwitchPHIOnly && !isa<CatchSwitchInst>(BB.getFirstNonPHI()))
718 continue;
719
720 for (Instruction &I : make_early_inc_range(BB)) {
721 auto *PN = dyn_cast<PHINode>(&I);
722 // Stop at the first non-PHI.
723 if (!PN)
724 break;
725
726 AllocaInst *SpillSlot = insertPHILoads(PN, F);
727 if (SpillSlot)
728 insertPHIStores(PN, SpillSlot);
729
730 PHINodes.push_back(PN);
731 }
732 }
733
734 for (auto *PN : PHINodes) {
735 // There may be lingering uses on other EH PHIs being removed
736 PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
737 PN->eraseFromParent();
738 }
739}
740
741void WinEHPrepare::cloneCommonBlocks(Function &F) {
742 // We need to clone all blocks which belong to multiple funclets. Values are
743 // remapped throughout the funclet to propagate both the new instructions
744 // *and* the new basic blocks themselves.
745 for (auto &Funclets : FuncletBlocks) {
746 BasicBlock *FuncletPadBB = Funclets.first;
747 std::vector<BasicBlock *> &BlocksInFunclet = Funclets.second;
748 Value *FuncletToken;
749 if (FuncletPadBB == &F.getEntryBlock())
750 FuncletToken = ConstantTokenNone::get(F.getContext());
751 else
752 FuncletToken = FuncletPadBB->getFirstNonPHI();
753
754 std::vector<std::pair<BasicBlock *, BasicBlock *>> Orig2Clone;
755 ValueToValueMapTy VMap;
756 for (BasicBlock *BB : BlocksInFunclet) {
757 ColorVector &ColorsForBB = BlockColors[BB];
758 // We don't need to do anything if the block is monochromatic.
759 size_t NumColorsForBB = ColorsForBB.size();
760 if (NumColorsForBB == 1)
761 continue;
762
763 DEBUG_WITH_TYPE("winehprepare-coloring",do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare-coloring")) { dbgs() << " Cloning block \'"
<< BB->getName() << "\' for funclet \'" <<
FuncletPadBB->getName() << "\'.\n"; } } while (false
)
764 dbgs() << " Cloning block \'" << BB->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare-coloring")) { dbgs() << " Cloning block \'"
<< BB->getName() << "\' for funclet \'" <<
FuncletPadBB->getName() << "\'.\n"; } } while (false
)
765 << "\' for funclet \'" << FuncletPadBB->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare-coloring")) { dbgs() << " Cloning block \'"
<< BB->getName() << "\' for funclet \'" <<
FuncletPadBB->getName() << "\'.\n"; } } while (false
)
766 << "\'.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare-coloring")) { dbgs() << " Cloning block \'"
<< BB->getName() << "\' for funclet \'" <<
FuncletPadBB->getName() << "\'.\n"; } } while (false
)
;
767
768 // Create a new basic block and copy instructions into it!
769 BasicBlock *CBB =
770 CloneBasicBlock(BB, VMap, Twine(".for.", FuncletPadBB->getName()));
771 // Insert the clone immediately after the original to ensure determinism
772 // and to keep the same relative ordering of any funclet's blocks.
773 CBB->insertInto(&F, BB->getNextNode());
774
775 // Add basic block mapping.
776 VMap[BB] = CBB;
777
778 // Record delta operations that we need to perform to our color mappings.
779 Orig2Clone.emplace_back(BB, CBB);
780 }
781
782 // If nothing was cloned, we're done cloning in this funclet.
783 if (Orig2Clone.empty())
784 continue;
785
786 // Update our color mappings to reflect that one block has lost a color and
787 // another has gained a color.
788 for (auto &BBMapping : Orig2Clone) {
789 BasicBlock *OldBlock = BBMapping.first;
790 BasicBlock *NewBlock = BBMapping.second;
791
792 BlocksInFunclet.push_back(NewBlock);
793 ColorVector &NewColors = BlockColors[NewBlock];
794 assert(NewColors.empty() && "A new block should only have one color!")(static_cast <bool> (NewColors.empty() && "A new block should only have one color!"
) ? void (0) : __assert_fail ("NewColors.empty() && \"A new block should only have one color!\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 794, __extension__ __PRETTY_FUNCTION__
))
;
795 NewColors.push_back(FuncletPadBB);
796
797 DEBUG_WITH_TYPE("winehprepare-coloring",do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare-coloring")) { dbgs() << " Assigned color \'"
<< FuncletPadBB->getName() << "\' to block \'"
<< NewBlock->getName() << "\'.\n"; } } while (
false)
798 dbgs() << " Assigned color \'" << FuncletPadBB->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare-coloring")) { dbgs() << " Assigned color \'"
<< FuncletPadBB->getName() << "\' to block \'"
<< NewBlock->getName() << "\'.\n"; } } while (
false)
799 << "\' to block \'" << NewBlock->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare-coloring")) { dbgs() << " Assigned color \'"
<< FuncletPadBB->getName() << "\' to block \'"
<< NewBlock->getName() << "\'.\n"; } } while (
false)
800 << "\'.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare-coloring")) { dbgs() << " Assigned color \'"
<< FuncletPadBB->getName() << "\' to block \'"
<< NewBlock->getName() << "\'.\n"; } } while (
false)
;
801
802 llvm::erase_value(BlocksInFunclet, OldBlock);
803 ColorVector &OldColors = BlockColors[OldBlock];
804 llvm::erase_value(OldColors, FuncletPadBB);
805
806 DEBUG_WITH_TYPE("winehprepare-coloring",do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare-coloring")) { dbgs() << " Removed color \'"
<< FuncletPadBB->getName() << "\' from block \'"
<< OldBlock->getName() << "\'.\n"; } } while (
false)
807 dbgs() << " Removed color \'" << FuncletPadBB->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare-coloring")) { dbgs() << " Removed color \'"
<< FuncletPadBB->getName() << "\' from block \'"
<< OldBlock->getName() << "\'.\n"; } } while (
false)
808 << "\' from block \'" << OldBlock->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare-coloring")) { dbgs() << " Removed color \'"
<< FuncletPadBB->getName() << "\' from block \'"
<< OldBlock->getName() << "\'.\n"; } } while (
false)
809 << "\'.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare-coloring")) { dbgs() << " Removed color \'"
<< FuncletPadBB->getName() << "\' from block \'"
<< OldBlock->getName() << "\'.\n"; } } while (
false)
;
810 }
811
812 // Loop over all of the instructions in this funclet, fixing up operand
813 // references as we go. This uses VMap to do all the hard work.
814 for (BasicBlock *BB : BlocksInFunclet)
815 // Loop over all instructions, fixing each one as we find it...
816 for (Instruction &I : *BB)
817 RemapInstruction(&I, VMap,
818 RF_IgnoreMissingLocals | RF_NoModuleLevelChanges);
819
820 // Catchrets targeting cloned blocks need to be updated separately from
821 // the loop above because they are not in the current funclet.
822 SmallVector<CatchReturnInst *, 2> FixupCatchrets;
823 for (auto &BBMapping : Orig2Clone) {
824 BasicBlock *OldBlock = BBMapping.first;
825 BasicBlock *NewBlock = BBMapping.second;
826
827 FixupCatchrets.clear();
828 for (BasicBlock *Pred : predecessors(OldBlock))
829 if (auto *CatchRet = dyn_cast<CatchReturnInst>(Pred->getTerminator()))
830 if (CatchRet->getCatchSwitchParentPad() == FuncletToken)
831 FixupCatchrets.push_back(CatchRet);
832
833 for (CatchReturnInst *CatchRet : FixupCatchrets)
834 CatchRet->setSuccessor(NewBlock);
835 }
836
837 auto UpdatePHIOnClonedBlock = [&](PHINode *PN, bool IsForOldBlock) {
838 unsigned NumPreds = PN->getNumIncomingValues();
839 for (unsigned PredIdx = 0, PredEnd = NumPreds; PredIdx != PredEnd;
840 ++PredIdx) {
841 BasicBlock *IncomingBlock = PN->getIncomingBlock(PredIdx);
842 bool EdgeTargetsFunclet;
843 if (auto *CRI =
844 dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) {
845 EdgeTargetsFunclet = (CRI->getCatchSwitchParentPad() == FuncletToken);
846 } else {
847 ColorVector &IncomingColors = BlockColors[IncomingBlock];
848 assert(!IncomingColors.empty() && "Block not colored!")(static_cast <bool> (!IncomingColors.empty() &&
"Block not colored!") ? void (0) : __assert_fail ("!IncomingColors.empty() && \"Block not colored!\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 848, __extension__ __PRETTY_FUNCTION__
))
;
849 assert((IncomingColors.size() == 1 ||(static_cast <bool> ((IncomingColors.size() == 1 || !llvm
::is_contained(IncomingColors, FuncletPadBB)) && "Cloning should leave this funclet's blocks monochromatic"
) ? void (0) : __assert_fail ("(IncomingColors.size() == 1 || !llvm::is_contained(IncomingColors, FuncletPadBB)) && \"Cloning should leave this funclet's blocks monochromatic\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 851, __extension__ __PRETTY_FUNCTION__
))
850 !llvm::is_contained(IncomingColors, FuncletPadBB)) &&(static_cast <bool> ((IncomingColors.size() == 1 || !llvm
::is_contained(IncomingColors, FuncletPadBB)) && "Cloning should leave this funclet's blocks monochromatic"
) ? void (0) : __assert_fail ("(IncomingColors.size() == 1 || !llvm::is_contained(IncomingColors, FuncletPadBB)) && \"Cloning should leave this funclet's blocks monochromatic\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 851, __extension__ __PRETTY_FUNCTION__
))
851 "Cloning should leave this funclet's blocks monochromatic")(static_cast <bool> ((IncomingColors.size() == 1 || !llvm
::is_contained(IncomingColors, FuncletPadBB)) && "Cloning should leave this funclet's blocks monochromatic"
) ? void (0) : __assert_fail ("(IncomingColors.size() == 1 || !llvm::is_contained(IncomingColors, FuncletPadBB)) && \"Cloning should leave this funclet's blocks monochromatic\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 851, __extension__ __PRETTY_FUNCTION__
))
;
852 EdgeTargetsFunclet = (IncomingColors.front() == FuncletPadBB);
853 }
854 if (IsForOldBlock != EdgeTargetsFunclet)
855 continue;
856 PN->removeIncomingValue(IncomingBlock, /*DeletePHIIfEmpty=*/false);
857 // Revisit the next entry.
858 --PredIdx;
859 --PredEnd;
860 }
861 };
862
863 for (auto &BBMapping : Orig2Clone) {
864 BasicBlock *OldBlock = BBMapping.first;
865 BasicBlock *NewBlock = BBMapping.second;
866 for (PHINode &OldPN : OldBlock->phis()) {
867 UpdatePHIOnClonedBlock(&OldPN, /*IsForOldBlock=*/true);
868 }
869 for (PHINode &NewPN : NewBlock->phis()) {
870 UpdatePHIOnClonedBlock(&NewPN, /*IsForOldBlock=*/false);
871 }
872 }
873
874 // Check to see if SuccBB has PHI nodes. If so, we need to add entries to
875 // the PHI nodes for NewBB now.
876 for (auto &BBMapping : Orig2Clone) {
877 BasicBlock *OldBlock = BBMapping.first;
878 BasicBlock *NewBlock = BBMapping.second;
879 for (BasicBlock *SuccBB : successors(NewBlock)) {
880 for (PHINode &SuccPN : SuccBB->phis()) {
881 // Ok, we have a PHI node. Figure out what the incoming value was for
882 // the OldBlock.
883 int OldBlockIdx = SuccPN.getBasicBlockIndex(OldBlock);
884 if (OldBlockIdx == -1)
885 break;
886 Value *IV = SuccPN.getIncomingValue(OldBlockIdx);
887
888 // Remap the value if necessary.
889 if (auto *Inst = dyn_cast<Instruction>(IV)) {
890 ValueToValueMapTy::iterator I = VMap.find(Inst);
891 if (I != VMap.end())
892 IV = I->second;
893 }
894
895 SuccPN.addIncoming(IV, NewBlock);
896 }
897 }
898 }
899
900 for (ValueToValueMapTy::value_type VT : VMap) {
901 // If there were values defined in BB that are used outside the funclet,
902 // then we now have to update all uses of the value to use either the
903 // original value, the cloned value, or some PHI derived value. This can
904 // require arbitrary PHI insertion, of which we are prepared to do, clean
905 // these up now.
906 SmallVector<Use *, 16> UsesToRename;
907
908 auto *OldI = dyn_cast<Instruction>(const_cast<Value *>(VT.first));
909 if (!OldI)
910 continue;
911 auto *NewI = cast<Instruction>(VT.second);
912 // Scan all uses of this instruction to see if it is used outside of its
913 // funclet, and if so, record them in UsesToRename.
914 for (Use &U : OldI->uses()) {
915 Instruction *UserI = cast<Instruction>(U.getUser());
916 BasicBlock *UserBB = UserI->getParent();
917 ColorVector &ColorsForUserBB = BlockColors[UserBB];
918 assert(!ColorsForUserBB.empty())(static_cast <bool> (!ColorsForUserBB.empty()) ? void (
0) : __assert_fail ("!ColorsForUserBB.empty()", "llvm/lib/CodeGen/WinEHPrepare.cpp"
, 918, __extension__ __PRETTY_FUNCTION__))
;
919 if (ColorsForUserBB.size() > 1 ||
920 *ColorsForUserBB.begin() != FuncletPadBB)
921 UsesToRename.push_back(&U);
922 }
923
924 // If there are no uses outside the block, we're done with this
925 // instruction.
926 if (UsesToRename.empty())
927 continue;
928
929 // We found a use of OldI outside of the funclet. Rename all uses of OldI
930 // that are outside its funclet to be uses of the appropriate PHI node
931 // etc.
932 SSAUpdater SSAUpdate;
933 SSAUpdate.Initialize(OldI->getType(), OldI->getName());
934 SSAUpdate.AddAvailableValue(OldI->getParent(), OldI);
935 SSAUpdate.AddAvailableValue(NewI->getParent(), NewI);
936
937 while (!UsesToRename.empty())
938 SSAUpdate.RewriteUseAfterInsertions(*UsesToRename.pop_back_val());
939 }
940 }
941}
942
943void WinEHPrepare::removeImplausibleInstructions(Function &F) {
944 // Remove implausible terminators and replace them with UnreachableInst.
945 for (auto &Funclet : FuncletBlocks) {
946 BasicBlock *FuncletPadBB = Funclet.first;
947 std::vector<BasicBlock *> &BlocksInFunclet = Funclet.second;
948 Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI();
949 auto *FuncletPad = dyn_cast<FuncletPadInst>(FirstNonPHI);
950 auto *CatchPad = dyn_cast_or_null<CatchPadInst>(FuncletPad);
951 auto *CleanupPad = dyn_cast_or_null<CleanupPadInst>(FuncletPad);
952
953 for (BasicBlock *BB : BlocksInFunclet) {
954 for (Instruction &I : *BB) {
955 auto *CB = dyn_cast<CallBase>(&I);
956 if (!CB)
957 continue;
958
959 Value *FuncletBundleOperand = nullptr;
960 if (auto BU = CB->getOperandBundle(LLVMContext::OB_funclet))
961 FuncletBundleOperand = BU->Inputs.front();
962
963 if (FuncletBundleOperand == FuncletPad)
964 continue;
965
966 // Skip call sites which are nounwind intrinsics or inline asm.
967 auto *CalledFn =
968 dyn_cast<Function>(CB->getCalledOperand()->stripPointerCasts());
969 if (CalledFn && ((CalledFn->isIntrinsic() && CB->doesNotThrow()) ||
970 CB->isInlineAsm()))
971 continue;
972
973 // This call site was not part of this funclet, remove it.
974 if (isa<InvokeInst>(CB)) {
975 // Remove the unwind edge if it was an invoke.
976 removeUnwindEdge(BB);
977 // Get a pointer to the new call.
978 BasicBlock::iterator CallI =
979 std::prev(BB->getTerminator()->getIterator());
980 auto *CI = cast<CallInst>(&*CallI);
981 changeToUnreachable(CI);
982 } else {
983 changeToUnreachable(&I);
984 }
985
986 // There are no more instructions in the block (except for unreachable),
987 // we are done.
988 break;
989 }
990
991 Instruction *TI = BB->getTerminator();
992 // CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst.
993 bool IsUnreachableRet = isa<ReturnInst>(TI) && FuncletPad;
994 // The token consumed by a CatchReturnInst must match the funclet token.
995 bool IsUnreachableCatchret = false;
996 if (auto *CRI = dyn_cast<CatchReturnInst>(TI))
997 IsUnreachableCatchret = CRI->getCatchPad() != CatchPad;
998 // The token consumed by a CleanupReturnInst must match the funclet token.
999 bool IsUnreachableCleanupret = false;
1000 if (auto *CRI = dyn_cast<CleanupReturnInst>(TI))
1001 IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad;
1002 if (IsUnreachableRet || IsUnreachableCatchret ||
1003 IsUnreachableCleanupret) {
1004 changeToUnreachable(TI);
1005 } else if (isa<InvokeInst>(TI)) {
1006 if (Personality == EHPersonality::MSVC_CXX && CleanupPad) {
1007 // Invokes within a cleanuppad for the MSVC++ personality never
1008 // transfer control to their unwind edge: the personality will
1009 // terminate the program.
1010 removeUnwindEdge(BB);
1011 }
1012 }
1013 }
1014 }
1015}
1016
1017void WinEHPrepare::cleanupPreparedFunclets(Function &F) {
1018 // Clean-up some of the mess we made by removing useles PHI nodes, trivial
1019 // branches, etc.
1020 for (BasicBlock &BB : llvm::make_early_inc_range(F)) {
1021 SimplifyInstructionsInBlock(&BB);
1022 ConstantFoldTerminator(&BB, /*DeleteDeadConditions=*/true);
1023 MergeBlockIntoPredecessor(&BB);
1024 }
1025
1026 // We might have some unreachable blocks after cleaning up some impossible
1027 // control flow.
1028 removeUnreachableBlocks(F);
1029}
1030
1031#ifndef NDEBUG
1032void WinEHPrepare::verifyPreparedFunclets(Function &F) {
1033 for (BasicBlock &BB : F) {
1034 size_t NumColors = BlockColors[&BB].size();
1035 assert(NumColors == 1 && "Expected monochromatic BB!")(static_cast <bool> (NumColors == 1 && "Expected monochromatic BB!"
) ? void (0) : __assert_fail ("NumColors == 1 && \"Expected monochromatic BB!\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 1035, __extension__ __PRETTY_FUNCTION__
))
;
1036 if (NumColors == 0)
1037 report_fatal_error("Uncolored BB!");
1038 if (NumColors > 1)
1039 report_fatal_error("Multicolor BB!");
1040 assert((DisableDemotion || !(BB.isEHPad() && isa<PHINode>(BB.begin()))) &&(static_cast <bool> ((DisableDemotion || !(BB.isEHPad()
&& isa<PHINode>(BB.begin()))) && "EH Pad still has a PHI!"
) ? void (0) : __assert_fail ("(DisableDemotion || !(BB.isEHPad() && isa<PHINode>(BB.begin()))) && \"EH Pad still has a PHI!\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 1041, __extension__ __PRETTY_FUNCTION__
))
1041 "EH Pad still has a PHI!")(static_cast <bool> ((DisableDemotion || !(BB.isEHPad()
&& isa<PHINode>(BB.begin()))) && "EH Pad still has a PHI!"
) ? void (0) : __assert_fail ("(DisableDemotion || !(BB.isEHPad() && isa<PHINode>(BB.begin()))) && \"EH Pad still has a PHI!\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 1041, __extension__ __PRETTY_FUNCTION__
))
;
1042 }
1043}
1044#endif
1045
1046bool WinEHPrepare::prepareExplicitEH(Function &F) {
1047 // Remove unreachable blocks. It is not valuable to assign them a color and
1048 // their existence can trick us into thinking values are alive when they are
1049 // not.
1050 removeUnreachableBlocks(F);
1051
1052 // Determine which blocks are reachable from which funclet entries.
1053 colorFunclets(F);
1054
1055 cloneCommonBlocks(F);
1056
1057 if (!DisableDemotion)
1058 demotePHIsOnFunclets(F, DemoteCatchSwitchPHIOnly ||
1059 DemoteCatchSwitchPHIOnlyOpt);
1060
1061 if (!DisableCleanups) {
1062 assert(!verifyFunction(F, &dbgs()))(static_cast <bool> (!verifyFunction(F, &dbgs())) ?
void (0) : __assert_fail ("!verifyFunction(F, &dbgs())",
"llvm/lib/CodeGen/WinEHPrepare.cpp", 1062, __extension__ __PRETTY_FUNCTION__
))
;
1063 removeImplausibleInstructions(F);
1064
1065 assert(!verifyFunction(F, &dbgs()))(static_cast <bool> (!verifyFunction(F, &dbgs())) ?
void (0) : __assert_fail ("!verifyFunction(F, &dbgs())",
"llvm/lib/CodeGen/WinEHPrepare.cpp", 1065, __extension__ __PRETTY_FUNCTION__
))
;
1066 cleanupPreparedFunclets(F);
1067 }
1068
1069 LLVM_DEBUG(verifyPreparedFunclets(F))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare")) { verifyPreparedFunclets(F); } } while (false
)
;
1070 // Recolor the CFG to verify that all is well.
1071 LLVM_DEBUG(colorFunclets(F))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare")) { colorFunclets(F); } } while (false)
;
1072 LLVM_DEBUG(verifyPreparedFunclets(F))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare")) { verifyPreparedFunclets(F); } } while (false
)
;
1073
1074 BlockColors.clear();
1075 FuncletBlocks.clear();
1076
1077 return true;
1078}
1079
1080// TODO: Share loads when one use dominates another, or when a catchpad exit
1081// dominates uses (needs dominators).
1082AllocaInst *WinEHPrepare::insertPHILoads(PHINode *PN, Function &F) {
1083 BasicBlock *PHIBlock = PN->getParent();
1084 AllocaInst *SpillSlot = nullptr;
1085 Instruction *EHPad = PHIBlock->getFirstNonPHI();
1086
1087 if (!EHPad->isTerminator()) {
1088 // If the EHPad isn't a terminator, then we can insert a load in this block
1089 // that will dominate all uses.
1090 SpillSlot = new AllocaInst(PN->getType(), DL->getAllocaAddrSpace(), nullptr,
1091 Twine(PN->getName(), ".wineh.spillslot"),
1092 &F.getEntryBlock().front());
1093 Value *V = new LoadInst(PN->getType(), SpillSlot,
1094 Twine(PN->getName(), ".wineh.reload"),
1095 &*PHIBlock->getFirstInsertionPt());
1096 PN->replaceAllUsesWith(V);
1097 return SpillSlot;
1098 }
1099
1100 // Otherwise, we have a PHI on a terminator EHPad, and we give up and insert
1101 // loads of the slot before every use.
1102 DenseMap<BasicBlock *, Value *> Loads;
1103 for (Use &U : llvm::make_early_inc_range(PN->uses())) {
1104 auto *UsingInst = cast<Instruction>(U.getUser());
1105 if (isa<PHINode>(UsingInst) && UsingInst->getParent()->isEHPad()) {
1106 // Use is on an EH pad phi. Leave it alone; we'll insert loads and
1107 // stores for it separately.
1108 continue;
1109 }
1110 replaceUseWithLoad(PN, U, SpillSlot, Loads, F);
1111 }
1112 return SpillSlot;
1113}
1114
1115// TODO: improve store placement. Inserting at def is probably good, but need
1116// to be careful not to introduce interfering stores (needs liveness analysis).
1117// TODO: identify related phi nodes that can share spill slots, and share them
1118// (also needs liveness).
1119void WinEHPrepare::insertPHIStores(PHINode *OriginalPHI,
1120 AllocaInst *SpillSlot) {
1121 // Use a worklist of (Block, Value) pairs -- the given Value needs to be
1122 // stored to the spill slot by the end of the given Block.
1123 SmallVector<std::pair<BasicBlock *, Value *>, 4> Worklist;
1124
1125 Worklist.push_back({OriginalPHI->getParent(), OriginalPHI});
1126
1127 while (!Worklist.empty()) {
1128 BasicBlock *EHBlock;
1129 Value *InVal;
1130 std::tie(EHBlock, InVal) = Worklist.pop_back_val();
1131
1132 PHINode *PN = dyn_cast<PHINode>(InVal);
1133 if (PN && PN->getParent() == EHBlock) {
1134 // The value is defined by another PHI we need to remove, with no room to
1135 // insert a store after the PHI, so each predecessor needs to store its
1136 // incoming value.
1137 for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
1138 Value *PredVal = PN->getIncomingValue(i);
1139
1140 // Undef can safely be skipped.
1141 if (isa<UndefValue>(PredVal))
1142 continue;
1143
1144 insertPHIStore(PN->getIncomingBlock(i), PredVal, SpillSlot, Worklist);
1145 }
1146 } else {
1147 // We need to store InVal, which dominates EHBlock, but can't put a store
1148 // in EHBlock, so need to put stores in each predecessor.
1149 for (BasicBlock *PredBlock : predecessors(EHBlock)) {
1150 insertPHIStore(PredBlock, InVal, SpillSlot, Worklist);
1151 }
1152 }
1153 }
1154}
1155
1156void WinEHPrepare::insertPHIStore(
1157 BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
1158 SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist) {
1159
1160 if (PredBlock->isEHPad() && PredBlock->getFirstNonPHI()->isTerminator()) {
1161 // Pred is unsplittable, so we need to queue it on the worklist.
1162 Worklist.push_back({PredBlock, PredVal});
1163 return;
1164 }
1165
1166 // Otherwise, insert the store at the end of the basic block.
1167 new StoreInst(PredVal, SpillSlot, PredBlock->getTerminator());
1168}
1169
1170void WinEHPrepare::replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
1171 DenseMap<BasicBlock *, Value *> &Loads,
1172 Function &F) {
1173 // Lazilly create the spill slot.
1174 if (!SpillSlot)
1175 SpillSlot = new AllocaInst(V->getType(), DL->getAllocaAddrSpace(), nullptr,
1176 Twine(V->getName(), ".wineh.spillslot"),
1177 &F.getEntryBlock().front());
1178
1179 auto *UsingInst = cast<Instruction>(U.getUser());
1180 if (auto *UsingPHI = dyn_cast<PHINode>(UsingInst)) {
1181 // If this is a PHI node, we can't insert a load of the value before
1182 // the use. Instead insert the load in the predecessor block
1183 // corresponding to the incoming value.
1184 //
1185 // Note that if there are multiple edges from a basic block to this
1186 // PHI node that we cannot have multiple loads. The problem is that
1187 // the resulting PHI node will have multiple values (from each load)
1188 // coming in from the same block, which is illegal SSA form.
1189 // For this reason, we keep track of and reuse loads we insert.
1190 BasicBlock *IncomingBlock = UsingPHI->getIncomingBlock(U);
1191 if (auto *CatchRet =
1192 dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) {
1193 // Putting a load above a catchret and use on the phi would still leave
1194 // a cross-funclet def/use. We need to split the edge, change the
1195 // catchret to target the new block, and put the load there.
1196 BasicBlock *PHIBlock = UsingInst->getParent();
1197 BasicBlock *NewBlock = SplitEdge(IncomingBlock, PHIBlock);
1198 // SplitEdge gives us:
1199 // IncomingBlock:
1200 // ...
1201 // br label %NewBlock
1202 // NewBlock:
1203 // catchret label %PHIBlock
1204 // But we need:
1205 // IncomingBlock:
1206 // ...
1207 // catchret label %NewBlock
1208 // NewBlock:
1209 // br label %PHIBlock
1210 // So move the terminators to each others' blocks and swap their
1211 // successors.
1212 BranchInst *Goto = cast<BranchInst>(IncomingBlock->getTerminator());
1213 Goto->removeFromParent();
1214 CatchRet->removeFromParent();
1215 IncomingBlock->getInstList().push_back(CatchRet);
1216 NewBlock->getInstList().push_back(Goto);
1217 Goto->setSuccessor(0, PHIBlock);
1218 CatchRet->setSuccessor(NewBlock);
1219 // Update the color mapping for the newly split edge.
1220 // Grab a reference to the ColorVector to be inserted before getting the
1221 // reference to the vector we are copying because inserting the new
1222 // element in BlockColors might cause the map to be reallocated.
1223 ColorVector &ColorsForNewBlock = BlockColors[NewBlock];
1224 ColorVector &ColorsForPHIBlock = BlockColors[PHIBlock];
1225 ColorsForNewBlock = ColorsForPHIBlock;
1226 for (BasicBlock *FuncletPad : ColorsForPHIBlock)
1227 FuncletBlocks[FuncletPad].push_back(NewBlock);
1228 // Treat the new block as incoming for load insertion.
1229 IncomingBlock = NewBlock;
1230 }
1231 Value *&Load = Loads[IncomingBlock];
1232 // Insert the load into the predecessor block
1233 if (!Load)
1234 Load = new LoadInst(V->getType(), SpillSlot,
1235 Twine(V->getName(), ".wineh.reload"),
1236 /*isVolatile=*/false, IncomingBlock->getTerminator());
1237
1238 U.set(Load);
1239 } else {
1240 // Reload right before the old use.
1241 auto *Load = new LoadInst(V->getType(), SpillSlot,
1242 Twine(V->getName(), ".wineh.reload"),
1243 /*isVolatile=*/false, UsingInst);
1244 U.set(Load);
1245 }
1246}
1247
1248void WinEHFuncInfo::addIPToStateRange(const InvokeInst *II,
1249 MCSymbol *InvokeBegin,
1250 MCSymbol *InvokeEnd) {
1251 assert(InvokeStateMap.count(II) &&(static_cast <bool> (InvokeStateMap.count(II) &&
"should get invoke with precomputed state") ? void (0) : __assert_fail
("InvokeStateMap.count(II) && \"should get invoke with precomputed state\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 1252, __extension__ __PRETTY_FUNCTION__
))
1252 "should get invoke with precomputed state")(static_cast <bool> (InvokeStateMap.count(II) &&
"should get invoke with precomputed state") ? void (0) : __assert_fail
("InvokeStateMap.count(II) && \"should get invoke with precomputed state\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 1252, __extension__ __PRETTY_FUNCTION__
))
;
1253 LabelToStateMap[InvokeBegin] = std::make_pair(InvokeStateMap[II], InvokeEnd);
1254}
1255
1256WinEHFuncInfo::WinEHFuncInfo() = default;

/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/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/Bitfields.h"
20#include "llvm/ADT/MapVector.h"
21#include "llvm/ADT/None.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/Twine.h"
25#include "llvm/ADT/iterator.h"
26#include "llvm/ADT/iterator_range.h"
27#include "llvm/IR/CFG.h"
28#include "llvm/IR/Constant.h"
29#include "llvm/IR/DerivedTypes.h"
30#include "llvm/IR/InstrTypes.h"
31#include "llvm/IR/Instruction.h"
32#include "llvm/IR/OperandTraits.h"
33#include "llvm/IR/Use.h"
34#include "llvm/IR/User.h"
35#include "llvm/Support/AtomicOrdering.h"
36#include "llvm/Support/ErrorHandling.h"
37#include <cassert>
38#include <cstddef>
39#include <cstdint>
40#include <iterator>
41
42namespace llvm {
43
44class APFloat;
45class APInt;
46class BasicBlock;
47class BlockAddress;
48class ConstantInt;
49class DataLayout;
50class StringRef;
51class Type;
52class Value;
53
54//===----------------------------------------------------------------------===//
55// AllocaInst Class
56//===----------------------------------------------------------------------===//
57
58/// an instruction to allocate memory on the stack
59class AllocaInst : public UnaryInstruction {
60 Type *AllocatedType;
61
62 using AlignmentField = AlignmentBitfieldElementT<0>;
63 using UsedWithInAllocaField = BoolBitfieldElementT<AlignmentField::NextBit>;
64 using SwiftErrorField = BoolBitfieldElementT<UsedWithInAllocaField::NextBit>;
65 static_assert(Bitfield::areContiguous<AlignmentField, UsedWithInAllocaField,
66 SwiftErrorField>(),
67 "Bitfields must be contiguous");
68
69protected:
70 // Note: Instruction needs to be a friend here to call cloneImpl.
71 friend class Instruction;
72
73 AllocaInst *cloneImpl() const;
74
75public:
76 explicit AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
77 const Twine &Name, Instruction *InsertBefore);
78 AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
79 const Twine &Name, BasicBlock *InsertAtEnd);
80
81 AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
82 Instruction *InsertBefore);
83 AllocaInst(Type *Ty, unsigned AddrSpace,
84 const Twine &Name, BasicBlock *InsertAtEnd);
85
86 AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, Align Align,
87 const Twine &Name = "", Instruction *InsertBefore = nullptr);
88 AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, Align Align,
89 const Twine &Name, BasicBlock *InsertAtEnd);
90
91 /// Return true if there is an allocation size parameter to the allocation
92 /// instruction that is not 1.
93 bool isArrayAllocation() const;
94
95 /// Get the number of elements allocated. For a simple allocation of a single
96 /// element, this will return a constant 1 value.
97 const Value *getArraySize() const { return getOperand(0); }
98 Value *getArraySize() { return getOperand(0); }
99
100 /// Overload to return most specific pointer type.
101 PointerType *getType() const {
102 return cast<PointerType>(Instruction::getType());
103 }
104
105 /// Return the address space for the allocation.
106 unsigned getAddressSpace() const {
107 return getType()->getAddressSpace();
108 }
109
110 /// Get allocation size in bits. Returns None if size can't be determined,
111 /// e.g. in case of a VLA.
112 Optional<TypeSize> getAllocationSizeInBits(const DataLayout &DL) const;
113
114 /// Return the type that is being allocated by the instruction.
115 Type *getAllocatedType() const { return AllocatedType; }
116 /// for use only in special circumstances that need to generically
117 /// transform a whole instruction (eg: IR linking and vectorization).
118 void setAllocatedType(Type *Ty) { AllocatedType = Ty; }
119
120 /// Return the alignment of the memory that is being allocated by the
121 /// instruction.
122 Align getAlign() const {
123 return Align(1ULL << getSubclassData<AlignmentField>());
124 }
125
126 void setAlignment(Align Align) {
127 setSubclassData<AlignmentField>(Log2(Align));
128 }
129
130 /// Return true if this alloca is in the entry block of the function and is a
131 /// constant size. If so, the code generator will fold it into the
132 /// prolog/epilog code, so it is basically free.
133 bool isStaticAlloca() const;
134
135 /// Return true if this alloca is used as an inalloca argument to a call. Such
136 /// allocas are never considered static even if they are in the entry block.
137 bool isUsedWithInAlloca() const {
138 return getSubclassData<UsedWithInAllocaField>();
139 }
140
141 /// Specify whether this alloca is used to represent the arguments to a call.
142 void setUsedWithInAlloca(bool V) {
143 setSubclassData<UsedWithInAllocaField>(V);
144 }
145
146 /// Return true if this alloca is used as a swifterror argument to a call.
147 bool isSwiftError() const { return getSubclassData<SwiftErrorField>(); }
148 /// Specify whether this alloca is used to represent a swifterror.
149 void setSwiftError(bool V) { setSubclassData<SwiftErrorField>(V); }
150
151 // Methods for support type inquiry through isa, cast, and dyn_cast:
152 static bool classof(const Instruction *I) {
153 return (I->getOpcode() == Instruction::Alloca);
154 }
155 static bool classof(const Value *V) {
156 return isa<Instruction>(V) && classof(cast<Instruction>(V));
157 }
158
159private:
160 // Shadow Instruction::setInstructionSubclassData with a private forwarding
161 // method so that subclasses cannot accidentally use it.
162 template <typename Bitfield>
163 void setSubclassData(typename Bitfield::Type Value) {
164 Instruction::setSubclassData<Bitfield>(Value);
165 }
166};
167
168//===----------------------------------------------------------------------===//
169// LoadInst Class
170//===----------------------------------------------------------------------===//
171
172/// An instruction for reading from memory. This uses the SubclassData field in
173/// Value to store whether or not the load is volatile.
174class LoadInst : public UnaryInstruction {
175 using VolatileField = BoolBitfieldElementT<0>;
176 using AlignmentField = AlignmentBitfieldElementT<VolatileField::NextBit>;
177 using OrderingField = AtomicOrderingBitfieldElementT<AlignmentField::NextBit>;
178 static_assert(
179 Bitfield::areContiguous<VolatileField, AlignmentField, OrderingField>(),
180 "Bitfields must be contiguous");
181
182 void AssertOK();
183
184protected:
185 // Note: Instruction needs to be a friend here to call cloneImpl.
186 friend class Instruction;
187
188 LoadInst *cloneImpl() const;
189
190public:
191 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr,
192 Instruction *InsertBefore);
193 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
194 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
195 Instruction *InsertBefore);
196 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
197 BasicBlock *InsertAtEnd);
198 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
199 Align Align, Instruction *InsertBefore = nullptr);
200 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
201 Align Align, BasicBlock *InsertAtEnd);
202 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
203 Align Align, AtomicOrdering Order,
204 SyncScope::ID SSID = SyncScope::System,
205 Instruction *InsertBefore = nullptr);
206 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
207 Align Align, AtomicOrdering Order, SyncScope::ID SSID,
208 BasicBlock *InsertAtEnd);
209
210 /// Return true if this is a load from a volatile memory location.
211 bool isVolatile() const { return getSubclassData<VolatileField>(); }
212
213 /// Specify whether this is a volatile load or not.
214 void setVolatile(bool V) { setSubclassData<VolatileField>(V); }
215
216 /// Return the alignment of the access that is being performed.
217 Align getAlign() const {
218 return Align(1ULL << (getSubclassData<AlignmentField>()));
219 }
220
221 void setAlignment(Align Align) {
222 setSubclassData<AlignmentField>(Log2(Align));
223 }
224
225 /// Returns the ordering constraint of this load instruction.
226 AtomicOrdering getOrdering() const {
227 return getSubclassData<OrderingField>();
228 }
229 /// Sets the ordering constraint of this load instruction. May not be Release
230 /// or AcquireRelease.
231 void setOrdering(AtomicOrdering Ordering) {
232 setSubclassData<OrderingField>(Ordering);
233 }
234
235 /// Returns the synchronization scope ID of this load instruction.
236 SyncScope::ID getSyncScopeID() const {
237 return SSID;
238 }
239
240 /// Sets the synchronization scope ID of this load instruction.
241 void setSyncScopeID(SyncScope::ID SSID) {
242 this->SSID = SSID;
243 }
244
245 /// Sets the ordering constraint and the synchronization scope ID of this load
246 /// instruction.
247 void setAtomic(AtomicOrdering Ordering,
248 SyncScope::ID SSID = SyncScope::System) {
249 setOrdering(Ordering);
250 setSyncScopeID(SSID);
251 }
252
253 bool isSimple() const { return !isAtomic() && !isVolatile(); }
254
255 bool isUnordered() const {
256 return (getOrdering() == AtomicOrdering::NotAtomic ||
257 getOrdering() == AtomicOrdering::Unordered) &&
258 !isVolatile();
259 }
260
261 Value *getPointerOperand() { return getOperand(0); }
262 const Value *getPointerOperand() const { return getOperand(0); }
263 static unsigned getPointerOperandIndex() { return 0U; }
264 Type *getPointerOperandType() const { return getPointerOperand()->getType(); }
265
266 /// Returns the address space of the pointer operand.
267 unsigned getPointerAddressSpace() const {
268 return getPointerOperandType()->getPointerAddressSpace();
269 }
270
271 // Methods for support type inquiry through isa, cast, and dyn_cast:
272 static bool classof(const Instruction *I) {
273 return I->getOpcode() == Instruction::Load;
274 }
275 static bool classof(const Value *V) {
276 return isa<Instruction>(V) && classof(cast<Instruction>(V));
277 }
278
279private:
280 // Shadow Instruction::setInstructionSubclassData with a private forwarding
281 // method so that subclasses cannot accidentally use it.
282 template <typename Bitfield>
283 void setSubclassData(typename Bitfield::Type Value) {
284 Instruction::setSubclassData<Bitfield>(Value);
285 }
286
287 /// The synchronization scope ID of this load instruction. Not quite enough
288 /// room in SubClassData for everything, so synchronization scope ID gets its
289 /// own field.
290 SyncScope::ID SSID;
291};
292
293//===----------------------------------------------------------------------===//
294// StoreInst Class
295//===----------------------------------------------------------------------===//
296
297/// An instruction for storing to memory.
298class StoreInst : public Instruction {
299 using VolatileField = BoolBitfieldElementT<0>;
300 using AlignmentField = AlignmentBitfieldElementT<VolatileField::NextBit>;
301 using OrderingField = AtomicOrderingBitfieldElementT<AlignmentField::NextBit>;
302 static_assert(
303 Bitfield::areContiguous<VolatileField, AlignmentField, OrderingField>(),
304 "Bitfields must be contiguous");
305
306 void AssertOK();
307
308protected:
309 // Note: Instruction needs to be a friend here to call cloneImpl.
310 friend class Instruction;
311
312 StoreInst *cloneImpl() const;
313
314public:
315 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
316 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
317 StoreInst(Value *Val, Value *Ptr, bool isVolatile, Instruction *InsertBefore);
318 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
319 StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align,
320 Instruction *InsertBefore = nullptr);
321 StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align,
322 BasicBlock *InsertAtEnd);
323 StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align,
324 AtomicOrdering Order, SyncScope::ID SSID = SyncScope::System,
325 Instruction *InsertBefore = nullptr);
326 StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align,
327 AtomicOrdering Order, SyncScope::ID SSID, BasicBlock *InsertAtEnd);
328
329 // allocate space for exactly two operands
330 void *operator new(size_t S) { return User::operator new(S, 2); }
331 void operator delete(void *Ptr) { User::operator delete(Ptr); }
332
333 /// Return true if this is a store to a volatile memory location.
334 bool isVolatile() const { return getSubclassData<VolatileField>(); }
335
336 /// Specify whether this is a volatile store or not.
337 void setVolatile(bool V) { setSubclassData<VolatileField>(V); }
338
339 /// Transparently provide more efficient getOperand methods.
340 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
;
341
342 Align getAlign() const {
343 return Align(1ULL << (getSubclassData<AlignmentField>()));
344 }
345
346 void setAlignment(Align Align) {
347 setSubclassData<AlignmentField>(Log2(Align));
348 }
349
350 /// Returns the ordering constraint of this store instruction.
351 AtomicOrdering getOrdering() const {
352 return getSubclassData<OrderingField>();
353 }
354
355 /// Sets the ordering constraint of this store instruction. May not be
356 /// Acquire or AcquireRelease.
357 void setOrdering(AtomicOrdering Ordering) {
358 setSubclassData<OrderingField>(Ordering);
359 }
360
361 /// Returns the synchronization scope ID of this store instruction.
362 SyncScope::ID getSyncScopeID() const {
363 return SSID;
364 }
365
366 /// Sets the synchronization scope ID of this store instruction.
367 void setSyncScopeID(SyncScope::ID SSID) {
368 this->SSID = SSID;
369 }
370
371 /// Sets the ordering constraint and the synchronization scope ID of this
372 /// store instruction.
373 void setAtomic(AtomicOrdering Ordering,
374 SyncScope::ID SSID = SyncScope::System) {
375 setOrdering(Ordering);
376 setSyncScopeID(SSID);
377 }
378
379 bool isSimple() const { return !isAtomic() && !isVolatile(); }
380
381 bool isUnordered() const {
382 return (getOrdering() == AtomicOrdering::NotAtomic ||
383 getOrdering() == AtomicOrdering::Unordered) &&
384 !isVolatile();
385 }
386
387 Value *getValueOperand() { return getOperand(0); }
388 const Value *getValueOperand() const { return getOperand(0); }
389
390 Value *getPointerOperand() { return getOperand(1); }
391 const Value *getPointerOperand() const { return getOperand(1); }
392 static unsigned getPointerOperandIndex() { return 1U; }
393 Type *getPointerOperandType() const { return getPointerOperand()->getType(); }
394
395 /// Returns the address space of the pointer operand.
396 unsigned getPointerAddressSpace() const {
397 return getPointerOperandType()->getPointerAddressSpace();
398 }
399
400 // Methods for support type inquiry through isa, cast, and dyn_cast:
401 static bool classof(const Instruction *I) {
402 return I->getOpcode() == Instruction::Store;
403 }
404 static bool classof(const Value *V) {
405 return isa<Instruction>(V) && classof(cast<Instruction>(V));
406 }
407
408private:
409 // Shadow Instruction::setInstructionSubclassData with a private forwarding
410 // method so that subclasses cannot accidentally use it.
411 template <typename Bitfield>
412 void setSubclassData(typename Bitfield::Type Value) {
413 Instruction::setSubclassData<Bitfield>(Value);
414 }
415
416 /// The synchronization scope ID of this store instruction. Not quite enough
417 /// room in SubClassData for everything, so synchronization scope ID gets its
418 /// own field.
419 SyncScope::ID SSID;
420};
421
422template <>
423struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
424};
425
426DEFINE_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 { (static_cast <bool> (i_nocapture
< OperandTraits<StoreInst>::operands(this) &&
"getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<StoreInst>::operands(this) && \"getOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 426, __extension__ __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) { (static_cast <bool> (i_nocapture <
OperandTraits<StoreInst>::operands(this) && "setOperand() out of range!"
) ? void (0) : __assert_fail ("i_nocapture < OperandTraits<StoreInst>::operands(this) && \"setOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 426, __extension__ __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); }
427
428//===----------------------------------------------------------------------===//
429// FenceInst Class
430//===----------------------------------------------------------------------===//
431
432/// An instruction for ordering other memory operations.
433class FenceInst : public Instruction {
434 using OrderingField = AtomicOrderingBitfieldElementT<0>;
435
436 void Init(AtomicOrdering Ordering, SyncScope::ID SSID);
437
438protected:
439 // Note: Instruction needs to be a friend here to call cloneImpl.
440 friend class Instruction;
441
442 FenceInst *cloneImpl() const;
443
444public:
445 // Ordering may only be Acquire, Release, AcquireRelease, or
446 // SequentiallyConsistent.
447 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
448 SyncScope::ID SSID = SyncScope::System,
449 Instruction *InsertBefore = nullptr);
450 FenceInst(LLVMContext &C, AtomicOrdering Ordering, SyncScope::ID SSID,
451 BasicBlock *InsertAtEnd);
452
453 // allocate space for exactly zero operands
454 void *operator new(size_t S) { return User::operator new(S, 0); }
455 void operator delete(void *Ptr) { User::operator delete(Ptr); }
456
457 /// Returns the ordering constraint of this fence instruction.
458 AtomicOrdering getOrdering() const {
459 return getSubclassData<OrderingField>();
460 }
461
462 /// Sets the ordering constraint of this fence instruction. May only be
463 /// Acquire, Release, AcquireRelease, or SequentiallyConsistent.
464 void setOrdering(AtomicOrdering Ordering) {
465 setSubclassData<OrderingField>(Ordering);
466 }
467
468 /// Returns the synchronization scope ID of this fence instruction.
469 SyncScope::ID getSyncScopeID() const {
470 return SSID;
471 }
472
473 /// Sets the synchronization scope ID of this fence instruction.
474 void setSyncScopeID(SyncScope::ID SSID) {
475 this->SSID = SSID;
476 }
477
478 // Methods for support type inquiry through isa, cast, and dyn_cast:
479 static bool classof(const Instruction *I) {
480 return I->getOpcode() == Instruction::Fence;
481 }
482 static bool classof(const Value *V) {
483 return isa<Instruction>(V) && classof(cast<Instruction>(V));
484 }
485
486private:
487 // Shadow Instruction::setInstructionSubclassData with a private forwarding
488 // method so that subclasses cannot accidentally use it.
489 template <typename Bitfield>
490 void setSubclassData(typename Bitfield::Type Value) {
491 Instruction::setSubclassData<Bitfield>(Value);
492 }
493
494 /// The synchronization scope ID of this fence instruction. Not quite enough
495 /// room in SubClassData for everything, so synchronization scope ID gets its
496 /// own field.
497 SyncScope::ID SSID;
498};
499
500//===----------------------------------------------------------------------===//
501// AtomicCmpXchgInst Class
502//===----------------------------------------------------------------------===//
503
504/// An instruction that atomically checks whether a
505/// specified value is in a memory location, and, if it is, stores a new value
506/// there. The value returned by this instruction is a pair containing the
507/// original value as first element, and an i1 indicating success (true) or
508/// failure (false) as second element.
509///
510class AtomicCmpXchgInst : public Instruction {
511 void Init(Value *Ptr, Value *Cmp, Value *NewVal, Align Align,
512 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
513 SyncScope::ID SSID);
514
515 template <unsigned Offset>
516 using AtomicOrderingBitfieldElement =
517 typename Bitfield::Element<AtomicOrdering, Offset, 3,
518 AtomicOrdering::LAST>;
519
520protected:
521 // Note: Instruction needs to be a friend here to call cloneImpl.
522 friend class Instruction;
523
524 AtomicCmpXchgInst *cloneImpl() const;
525
526public:
527 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, Align Alignment,
528 AtomicOrdering SuccessOrdering,
529 AtomicOrdering FailureOrdering, SyncScope::ID SSID,
530 Instruction *InsertBefore = nullptr);
531 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, Align Alignment,
532 AtomicOrdering SuccessOrdering,
533 AtomicOrdering FailureOrdering, SyncScope::ID SSID,
534 BasicBlock *InsertAtEnd);
535
536 // allocate space for exactly three operands
537 void *operator new(size_t S) { return User::operator new(S, 3); }
538 void operator delete(void *Ptr) { User::operator delete(Ptr); }
539
540 using VolatileField = BoolBitfieldElementT<0>;
541 using WeakField = BoolBitfieldElementT<VolatileField::NextBit>;
542 using SuccessOrderingField =
543 AtomicOrderingBitfieldElementT<WeakField::NextBit>;
544 using FailureOrderingField =
545 AtomicOrderingBitfieldElementT<SuccessOrderingField::NextBit>;
546 using AlignmentField =
547 AlignmentBitfieldElementT<FailureOrderingField::NextBit>;
548 static_assert(
549 Bitfield::areContiguous<VolatileField, WeakField, SuccessOrderingField,
550 FailureOrderingField, AlignmentField>(),
551 "Bitfields must be contiguous");
552
553 /// Return the alignment of the memory that is being allocated by the
554 /// instruction.
555 Align getAlign() const {
556 return Align(1ULL << getSubclassData<AlignmentField>());
557 }
558
559 void setAlignment(Align Align) {
560 setSubclassData<AlignmentField>(Log2(Align));
561 }
562
563 /// Return true if this is a cmpxchg from a volatile memory
564 /// location.
565 ///
566 bool isVolatile() const { return getSubclassData<VolatileField>(); }
567
568 /// Specify whether this is a volatile cmpxchg.
569 ///
570 void setVolatile(bool V) { setSubclassData<VolatileField>(V); }
571
572 /// Return true if this cmpxchg may spuriously fail.
573 bool isWeak() const { return getSubclassData<WeakField>(); }
574
575 void setWeak(bool IsWeak) { setSubclassData<WeakField>(IsWeak); }
576
577 /// Transparently provide more efficient getOperand methods.
578 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
;
579
580 static bool isValidSuccessOrdering(AtomicOrdering Ordering) {
581 return Ordering != AtomicOrdering::NotAtomic &&
582 Ordering != AtomicOrdering::Unordered;
583 }
584
585 static bool isValidFailureOrdering(AtomicOrdering Ordering) {
586 return Ordering != AtomicOrdering::NotAtomic &&
587 Ordering != AtomicOrdering::Unordered &&
588 Ordering != AtomicOrdering::AcquireRelease &&
589 Ordering != AtomicOrdering::Release;
590 }
591
592 /// Returns the success ordering constraint of this cmpxchg instruction.
593 AtomicOrdering getSuccessOrdering() const {
594 return getSubclassData<SuccessOrderingField>();
595 }
596
597 /// Sets the success ordering constraint of this cmpxchg instruction.
598 void setSuccessOrdering(AtomicOrdering Ordering) {
599 assert(isValidSuccessOrdering(Ordering) &&(static_cast <bool> (isValidSuccessOrdering(Ordering) &&
"invalid CmpXchg success ordering") ? void (0) : __assert_fail
("isValidSuccessOrdering(Ordering) && \"invalid CmpXchg success ordering\""
, "llvm/include/llvm/IR/Instructions.h", 600, __extension__ __PRETTY_FUNCTION__
))
600 "invalid CmpXchg success ordering")(static_cast <bool> (isValidSuccessOrdering(Ordering) &&
"invalid CmpXchg success ordering") ? void (0) : __assert_fail
("isValidSuccessOrdering(Ordering) && \"invalid CmpXchg success ordering\""
, "llvm/include/llvm/IR/Instructions.h", 600, __extension__ __PRETTY_FUNCTION__
))
;
601 setSubclassData<SuccessOrderingField>(Ordering);
602 }
603
604 /// Returns the failure ordering constraint of this cmpxchg instruction.
605 AtomicOrdering getFailureOrdering() const {
606 return getSubclassData<FailureOrderingField>();
607 }
608
609 /// Sets the failure ordering constraint of this cmpxchg instruction.
610 void setFailureOrdering(AtomicOrdering Ordering) {
611 assert(isValidFailureOrdering(Ordering) &&(static_cast <bool> (isValidFailureOrdering(Ordering) &&
"invalid CmpXchg failure ordering") ? void (0) : __assert_fail
("isValidFailureOrdering(Ordering) && \"invalid CmpXchg failure ordering\""
, "llvm/include/llvm/IR/Instructions.h", 612, __extension__ __PRETTY_FUNCTION__
))
612 "invalid CmpXchg failure ordering")(static_cast <bool> (isValidFailureOrdering(Ordering) &&
"invalid CmpXchg failure ordering") ? void (0) : __assert_fail
("isValidFailureOrdering(Ordering) && \"invalid CmpXchg failure ordering\""
, "llvm/include/llvm/IR/Instructions.h", 612, __extension__ __PRETTY_FUNCTION__
))
;
613 setSubclassData<FailureOrderingField>(Ordering);
614 }
615
616 /// Returns a single ordering which is at least as strong as both the
617 /// success and failure orderings for this cmpxchg.
618 AtomicOrdering getMergedOrdering() const {
619 if (getFailureOrdering() == AtomicOrdering::SequentiallyConsistent)
620 return AtomicOrdering::SequentiallyConsistent;
621 if (getFailureOrdering() == AtomicOrdering::Acquire) {
622 if (getSuccessOrdering() == AtomicOrdering::Monotonic)
623 return AtomicOrdering::Acquire;
624 if (getSuccessOrdering() == AtomicOrdering::Release)
625 return AtomicOrdering::AcquireRelease;
626 }
627 return getSuccessOrdering();
628 }
629
630 /// Returns the synchronization scope ID of this cmpxchg instruction.
631 SyncScope::ID getSyncScopeID() const {
632 return SSID;
633 }
634
635 /// Sets the synchronization scope ID of this cmpxchg instruction.
636 void setSyncScopeID(SyncScope::ID SSID) {
637 this->SSID = SSID;
638 }
639
640 Value *getPointerOperand() { return getOperand(0); }
641 const Value *getPointerOperand() const { return getOperand(0); }
642 static unsigned getPointerOperandIndex() { return 0U; }
643
644 Value *getCompareOperand() { return getOperand(1); }
645 const Value *getCompareOperand() const { return getOperand(1); }
646
647 Value *getNewValOperand() { return getOperand(2); }
648 const Value *getNewValOperand() const { return getOperand(2); }
649
650 /// Returns the address space of the pointer operand.
651 unsigned getPointerAddressSpace() const {
652 return getPointerOperand()->getType()->getPointerAddressSpace();
653 }
654
655 /// Returns the strongest permitted ordering on failure, given the
656 /// desired ordering on success.
657 ///
658 /// If the comparison in a cmpxchg operation fails, there is no atomic store
659 /// so release semantics cannot be provided. So this function drops explicit
660 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
661 /// operation would remain SequentiallyConsistent.
662 static AtomicOrdering
663 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) {
664 switch (SuccessOrdering) {
665 default:
666 llvm_unreachable("invalid cmpxchg success ordering")::llvm::llvm_unreachable_internal("invalid cmpxchg success ordering"
, "llvm/include/llvm/IR/Instructions.h", 666)
;
667 case AtomicOrdering::Release:
668 case AtomicOrdering::Monotonic:
669 return AtomicOrdering::Monotonic;
670 case AtomicOrdering::AcquireRelease:
671 case AtomicOrdering::Acquire:
672 return AtomicOrdering::Acquire;
673 case AtomicOrdering::SequentiallyConsistent:
674 return AtomicOrdering::SequentiallyConsistent;
675 }
676 }
677
678 // Methods for support type inquiry through isa, cast, and dyn_cast:
679 static bool classof(const Instruction *I) {
680 return I->getOpcode() == Instruction::AtomicCmpXchg;
681 }
682 static bool classof(const Value *V) {
683 return isa<Instruction>(V) && classof(cast<Instruction>(V));
684 }
685
686private:
687 // Shadow Instruction::setInstructionSubclassData with a private forwarding
688 // method so that subclasses cannot accidentally use it.
689 template <typename Bitfield>
690 void setSubclassData(typename Bitfield::Type Value) {
691 Instruction::setSubclassData<Bitfield>(Value);
692 }
693
694 /// The synchronization scope ID of this cmpxchg instruction. Not quite
695 /// enough room in SubClassData for everything, so synchronization scope ID
696 /// gets its own field.
697 SyncScope::ID SSID;
698};
699
700template <>
701struct OperandTraits<AtomicCmpXchgInst> :
702 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
703};
704
705DEFINE_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 { (static_cast <bool> (i_nocapture <
OperandTraits<AtomicCmpXchgInst>::operands(this) &&
"getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicCmpXchgInst>::operands(this) && \"getOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 705, __extension__ __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) { (static_cast <bool> (i_nocapture
< OperandTraits<AtomicCmpXchgInst>::operands(this) &&
"setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicCmpXchgInst>::operands(this) && \"setOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 705, __extension__ __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
); }
706
707//===----------------------------------------------------------------------===//
708// AtomicRMWInst Class
709//===----------------------------------------------------------------------===//
710
711/// an instruction that atomically reads a memory location,
712/// combines it with another value, and then stores the result back. Returns
713/// the old value.
714///
715class AtomicRMWInst : public Instruction {
716protected:
717 // Note: Instruction needs to be a friend here to call cloneImpl.
718 friend class Instruction;
719
720 AtomicRMWInst *cloneImpl() const;
721
722public:
723 /// This enumeration lists the possible modifications atomicrmw can make. In
724 /// the descriptions, 'p' is the pointer to the instruction's memory location,
725 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
726 /// instruction. These instructions always return 'old'.
727 enum BinOp : unsigned {
728 /// *p = v
729 Xchg,
730 /// *p = old + v
731 Add,
732 /// *p = old - v
733 Sub,
734 /// *p = old & v
735 And,
736 /// *p = ~(old & v)
737 Nand,
738 /// *p = old | v
739 Or,
740 /// *p = old ^ v
741 Xor,
742 /// *p = old >signed v ? old : v
743 Max,
744 /// *p = old <signed v ? old : v
745 Min,
746 /// *p = old >unsigned v ? old : v
747 UMax,
748 /// *p = old <unsigned v ? old : v
749 UMin,
750
751 /// *p = old + v
752 FAdd,
753
754 /// *p = old - v
755 FSub,
756
757 /// *p = maxnum(old, v)
758 /// \p maxnum matches the behavior of \p llvm.maxnum.*.
759 FMax,
760
761 /// *p = minnum(old, v)
762 /// \p minnum matches the behavior of \p llvm.minnum.*.
763 FMin,
764
765 FIRST_BINOP = Xchg,
766 LAST_BINOP = FMin,
767 BAD_BINOP
768 };
769
770private:
771 template <unsigned Offset>
772 using AtomicOrderingBitfieldElement =
773 typename Bitfield::Element<AtomicOrdering, Offset, 3,
774 AtomicOrdering::LAST>;
775
776 template <unsigned Offset>
777 using BinOpBitfieldElement =
778 typename Bitfield::Element<BinOp, Offset, 4, BinOp::LAST_BINOP>;
779
780public:
781 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, Align Alignment,
782 AtomicOrdering Ordering, SyncScope::ID SSID,
783 Instruction *InsertBefore = nullptr);
784 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, Align Alignment,
785 AtomicOrdering Ordering, SyncScope::ID SSID,
786 BasicBlock *InsertAtEnd);
787
788 // allocate space for exactly two operands
789 void *operator new(size_t S) { return User::operator new(S, 2); }
790 void operator delete(void *Ptr) { User::operator delete(Ptr); }
791
792 using VolatileField = BoolBitfieldElementT<0>;
793 using AtomicOrderingField =
794 AtomicOrderingBitfieldElementT<VolatileField::NextBit>;
795 using OperationField = BinOpBitfieldElement<AtomicOrderingField::NextBit>;
796 using AlignmentField = AlignmentBitfieldElementT<OperationField::NextBit>;
797 static_assert(Bitfield::areContiguous<VolatileField, AtomicOrderingField,
798 OperationField, AlignmentField>(),
799 "Bitfields must be contiguous");
800
801 BinOp getOperation() const { return getSubclassData<OperationField>(); }
802
803 static StringRef getOperationName(BinOp Op);
804
805 static bool isFPOperation(BinOp Op) {
806 switch (Op) {
807 case AtomicRMWInst::FAdd:
808 case AtomicRMWInst::FSub:
809 case AtomicRMWInst::FMax:
810 case AtomicRMWInst::FMin:
811 return true;
812 default:
813 return false;
814 }
815 }
816
817 void setOperation(BinOp Operation) {
818 setSubclassData<OperationField>(Operation);
819 }
820
821 /// Return the alignment of the memory that is being allocated by the
822 /// instruction.
823 Align getAlign() const {
824 return Align(1ULL << getSubclassData<AlignmentField>());
825 }
826
827 void setAlignment(Align Align) {
828 setSubclassData<AlignmentField>(Log2(Align));
829 }
830
831 /// Return true if this is a RMW on a volatile memory location.
832 ///
833 bool isVolatile() const { return getSubclassData<VolatileField>(); }
834
835 /// Specify whether this is a volatile RMW or not.
836 ///
837 void setVolatile(bool V) { setSubclassData<VolatileField>(V); }
838
839 /// Transparently provide more efficient getOperand methods.
840 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
;
841
842 /// Returns the ordering constraint of this rmw instruction.
843 AtomicOrdering getOrdering() const {
844 return getSubclassData<AtomicOrderingField>();
845 }
846
847 /// Sets the ordering constraint of this rmw instruction.
848 void setOrdering(AtomicOrdering Ordering) {
849 assert(Ordering != AtomicOrdering::NotAtomic &&(static_cast <bool> (Ordering != AtomicOrdering::NotAtomic
&& "atomicrmw instructions can only be atomic.") ? void
(0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"atomicrmw instructions can only be atomic.\""
, "llvm/include/llvm/IR/Instructions.h", 850, __extension__ __PRETTY_FUNCTION__
))
850 "atomicrmw instructions can only be atomic.")(static_cast <bool> (Ordering != AtomicOrdering::NotAtomic
&& "atomicrmw instructions can only be atomic.") ? void
(0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"atomicrmw instructions can only be atomic.\""
, "llvm/include/llvm/IR/Instructions.h", 850, __extension__ __PRETTY_FUNCTION__
))
;
851 assert(Ordering != AtomicOrdering::Unordered &&(static_cast <bool> (Ordering != AtomicOrdering::Unordered
&& "atomicrmw instructions cannot be unordered.") ? void
(0) : __assert_fail ("Ordering != AtomicOrdering::Unordered && \"atomicrmw instructions cannot be unordered.\""
, "llvm/include/llvm/IR/Instructions.h", 852, __extension__ __PRETTY_FUNCTION__
))
852 "atomicrmw instructions cannot be unordered.")(static_cast <bool> (Ordering != AtomicOrdering::Unordered
&& "atomicrmw instructions cannot be unordered.") ? void
(0) : __assert_fail ("Ordering != AtomicOrdering::Unordered && \"atomicrmw instructions cannot be unordered.\""
, "llvm/include/llvm/IR/Instructions.h", 852, __extension__ __PRETTY_FUNCTION__
))
;
853 setSubclassData<AtomicOrderingField>(Ordering);
854 }
855
856 /// Returns the synchronization scope ID of this rmw instruction.
857 SyncScope::ID getSyncScopeID() const {
858 return SSID;
859 }
860
861 /// Sets the synchronization scope ID of this rmw instruction.
862 void setSyncScopeID(SyncScope::ID SSID) {
863 this->SSID = SSID;
864 }
865
866 Value *getPointerOperand() { return getOperand(0); }
867 const Value *getPointerOperand() const { return getOperand(0); }
868 static unsigned getPointerOperandIndex() { return 0U; }
869
870 Value *getValOperand() { return getOperand(1); }
871 const Value *getValOperand() const { return getOperand(1); }
872
873 /// Returns the address space of the pointer operand.
874 unsigned getPointerAddressSpace() const {
875 return getPointerOperand()->getType()->getPointerAddressSpace();
876 }
877
878 bool isFloatingPointOperation() const {
879 return isFPOperation(getOperation());
880 }
881
882 // Methods for support type inquiry through isa, cast, and dyn_cast:
883 static bool classof(const Instruction *I) {
884 return I->getOpcode() == Instruction::AtomicRMW;
885 }
886 static bool classof(const Value *V) {
887 return isa<Instruction>(V) && classof(cast<Instruction>(V));
888 }
889
890private:
891 void Init(BinOp Operation, Value *Ptr, Value *Val, Align Align,
892 AtomicOrdering Ordering, SyncScope::ID SSID);
893
894 // Shadow Instruction::setInstructionSubclassData with a private forwarding
895 // method so that subclasses cannot accidentally use it.
896 template <typename Bitfield>
897 void setSubclassData(typename Bitfield::Type Value) {
898 Instruction::setSubclassData<Bitfield>(Value);
899 }
900
901 /// The synchronization scope ID of this rmw instruction. Not quite enough
902 /// room in SubClassData for everything, so synchronization scope ID gets its
903 /// own field.
904 SyncScope::ID SSID;
905};
906
907template <>
908struct OperandTraits<AtomicRMWInst>
909 : public FixedNumOperandTraits<AtomicRMWInst,2> {
910};
911
912DEFINE_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 { (static_cast <bool> (i_nocapture
< OperandTraits<AtomicRMWInst>::operands(this) &&
"getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicRMWInst>::operands(this) && \"getOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 912, __extension__ __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) { (static_cast <bool> (i_nocapture
< OperandTraits<AtomicRMWInst>::operands(this) &&
"setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicRMWInst>::operands(this) && \"setOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 912, __extension__ __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
); }
913
914//===----------------------------------------------------------------------===//
915// GetElementPtrInst Class
916//===----------------------------------------------------------------------===//
917
918// checkGEPType - Simple wrapper function to give a better assertion failure
919// message on bad indexes for a gep instruction.
920//
921inline Type *checkGEPType(Type *Ty) {
922 assert(Ty && "Invalid GetElementPtrInst indices for type!")(static_cast <bool> (Ty && "Invalid GetElementPtrInst indices for type!"
) ? void (0) : __assert_fail ("Ty && \"Invalid GetElementPtrInst indices for type!\""
, "llvm/include/llvm/IR/Instructions.h", 922, __extension__ __PRETTY_FUNCTION__
))
;
923 return Ty;
924}
925
926/// an instruction for type-safe pointer arithmetic to
927/// access elements of arrays and structs
928///
929class GetElementPtrInst : public Instruction {
930 Type *SourceElementType;
931 Type *ResultElementType;
932
933 GetElementPtrInst(const GetElementPtrInst &GEPI);
934
935 /// Constructors - Create a getelementptr instruction with a base pointer an
936 /// list of indices. The first ctor can optionally insert before an existing
937 /// instruction, the second appends the new instruction to the specified
938 /// BasicBlock.
939 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
940 ArrayRef<Value *> IdxList, unsigned Values,
941 const Twine &NameStr, Instruction *InsertBefore);
942 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
943 ArrayRef<Value *> IdxList, unsigned Values,
944 const Twine &NameStr, BasicBlock *InsertAtEnd);
945
946 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
947
948protected:
949 // Note: Instruction needs to be a friend here to call cloneImpl.
950 friend class Instruction;
951
952 GetElementPtrInst *cloneImpl() const;
953
954public:
955 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
956 ArrayRef<Value *> IdxList,
957 const Twine &NameStr = "",
958 Instruction *InsertBefore = nullptr) {
959 unsigned Values = 1 + unsigned(IdxList.size());
960 assert(PointeeType && "Must specify element type")(static_cast <bool> (PointeeType && "Must specify element type"
) ? void (0) : __assert_fail ("PointeeType && \"Must specify element type\""
, "llvm/include/llvm/IR/Instructions.h", 960, __extension__ __PRETTY_FUNCTION__
))
;
961 assert(cast<PointerType>(Ptr->getType()->getScalarType())(static_cast <bool> (cast<PointerType>(Ptr->getType
()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType
)) ? void (0) : __assert_fail ("cast<PointerType>(Ptr->getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType)"
, "llvm/include/llvm/IR/Instructions.h", 962, __extension__ __PRETTY_FUNCTION__
))
962 ->isOpaqueOrPointeeTypeMatches(PointeeType))(static_cast <bool> (cast<PointerType>(Ptr->getType
()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType
)) ? void (0) : __assert_fail ("cast<PointerType>(Ptr->getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType)"
, "llvm/include/llvm/IR/Instructions.h", 962, __extension__ __PRETTY_FUNCTION__
))
;
963 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
964 NameStr, InsertBefore);
965 }
966
967 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
968 ArrayRef<Value *> IdxList,
969 const Twine &NameStr,
970 BasicBlock *InsertAtEnd) {
971 unsigned Values = 1 + unsigned(IdxList.size());
972 assert(PointeeType && "Must specify element type")(static_cast <bool> (PointeeType && "Must specify element type"
) ? void (0) : __assert_fail ("PointeeType && \"Must specify element type\""
, "llvm/include/llvm/IR/Instructions.h", 972, __extension__ __PRETTY_FUNCTION__
))
;
973 assert(cast<PointerType>(Ptr->getType()->getScalarType())(static_cast <bool> (cast<PointerType>(Ptr->getType
()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType
)) ? void (0) : __assert_fail ("cast<PointerType>(Ptr->getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType)"
, "llvm/include/llvm/IR/Instructions.h", 974, __extension__ __PRETTY_FUNCTION__
))
974 ->isOpaqueOrPointeeTypeMatches(PointeeType))(static_cast <bool> (cast<PointerType>(Ptr->getType
()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType
)) ? void (0) : __assert_fail ("cast<PointerType>(Ptr->getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType)"
, "llvm/include/llvm/IR/Instructions.h", 974, __extension__ __PRETTY_FUNCTION__
))
;
975 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
976 NameStr, InsertAtEnd);
977 }
978
979 /// Create an "inbounds" getelementptr. See the documentation for the
980 /// "inbounds" flag in LangRef.html for details.
981 static GetElementPtrInst *
982 CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList,
983 const Twine &NameStr = "",
984 Instruction *InsertBefore = nullptr) {
985 GetElementPtrInst *GEP =
986 Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore);
987 GEP->setIsInBounds(true);
988 return GEP;
989 }
990
991 static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr,
992 ArrayRef<Value *> IdxList,
993 const Twine &NameStr,
994 BasicBlock *InsertAtEnd) {
995 GetElementPtrInst *GEP =
996 Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd);
997 GEP->setIsInBounds(true);
998 return GEP;
999 }
1000
1001 /// Transparently provide more efficient getOperand methods.
1002 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
;
1003
1004 Type *getSourceElementType() const { return SourceElementType; }
1005
1006 void setSourceElementType(Type *Ty) { SourceElementType = Ty; }
1007 void setResultElementType(Type *Ty) { ResultElementType = Ty; }
1008
1009 Type *getResultElementType() const {
1010 assert(cast<PointerType>(getType()->getScalarType())(static_cast <bool> (cast<PointerType>(getType()->
getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType
)) ? void (0) : __assert_fail ("cast<PointerType>(getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType)"
, "llvm/include/llvm/IR/Instructions.h", 1011, __extension__ __PRETTY_FUNCTION__
))
1011 ->isOpaqueOrPointeeTypeMatches(ResultElementType))(static_cast <bool> (cast<PointerType>(getType()->
getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType
)) ? void (0) : __assert_fail ("cast<PointerType>(getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType)"
, "llvm/include/llvm/IR/Instructions.h", 1011, __extension__ __PRETTY_FUNCTION__
))
;
1012 return ResultElementType;
1013 }
1014
1015 /// Returns the address space of this instruction's pointer type.
1016 unsigned getAddressSpace() const {
1017 // Note that this is always the same as the pointer operand's address space
1018 // and that is cheaper to compute, so cheat here.
1019 return getPointerAddressSpace();
1020 }
1021
1022 /// Returns the result type of a getelementptr with the given source
1023 /// element type and indexes.
1024 ///
1025 /// Null is returned if the indices are invalid for the specified
1026 /// source element type.
1027 static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList);
1028 static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList);
1029 static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList);
1030
1031 /// Return the type of the element at the given index of an indexable
1032 /// type. This is equivalent to "getIndexedType(Agg, {Zero, Idx})".
1033 ///
1034 /// Returns null if the type can't be indexed, or the given index is not
1035 /// legal for the given type.
1036 static Type *getTypeAtIndex(Type *Ty, Value *Idx);
1037 static Type *getTypeAtIndex(Type *Ty, uint64_t Idx);
1038
1039 inline op_iterator idx_begin() { return op_begin()+1; }
1040 inline const_op_iterator idx_begin() const { return op_begin()+1; }
1041 inline op_iterator idx_end() { return op_end(); }
1042 inline const_op_iterator idx_end() const { return op_end(); }
1043
1044 inline iterator_range<op_iterator> indices() {
1045 return make_range(idx_begin(), idx_end());
1046 }
1047
1048 inline iterator_range<const_op_iterator> indices() const {
1049 return make_range(idx_begin(), idx_end());
1050 }
1051
1052 Value *getPointerOperand() {
1053 return getOperand(0);
1054 }
1055 const Value *getPointerOperand() const {
1056 return getOperand(0);
1057 }
1058 static unsigned getPointerOperandIndex() {
1059 return 0U; // get index for modifying correct operand.
1060 }
1061
1062 /// Method to return the pointer operand as a
1063 /// PointerType.
1064 Type *getPointerOperandType() const {
1065 return getPointerOperand()->getType();
1066 }
1067
1068 /// Returns the address space of the pointer operand.
1069 unsigned getPointerAddressSpace() const {
1070 return getPointerOperandType()->getPointerAddressSpace();
1071 }
1072
1073 /// Returns the pointer type returned by the GEP
1074 /// instruction, which may be a vector of pointers.
1075 static Type *getGEPReturnType(Type *ElTy, Value *Ptr,
1076 ArrayRef<Value *> IdxList) {
1077 PointerType *OrigPtrTy = cast<PointerType>(Ptr->getType()->getScalarType());
1078 unsigned AddrSpace = OrigPtrTy->getAddressSpace();
1079 Type *ResultElemTy = checkGEPType(getIndexedType(ElTy, IdxList));
1080 Type *PtrTy = OrigPtrTy->isOpaque()
1081 ? PointerType::get(OrigPtrTy->getContext(), AddrSpace)
1082 : PointerType::get(ResultElemTy, AddrSpace);
1083 // Vector GEP
1084 if (auto *PtrVTy = dyn_cast<VectorType>(Ptr->getType())) {
1085 ElementCount EltCount = PtrVTy->getElementCount();
1086 return VectorType::get(PtrTy, EltCount);
1087 }
1088 for (Value *Index : IdxList)
1089 if (auto *IndexVTy = dyn_cast<VectorType>(Index->getType())) {
1090 ElementCount EltCount = IndexVTy->getElementCount();
1091 return VectorType::get(PtrTy, EltCount);
1092 }
1093 // Scalar GEP
1094 return PtrTy;
1095 }
1096
1097 unsigned getNumIndices() const { // Note: always non-negative
1098 return getNumOperands() - 1;
1099 }
1100
1101 bool hasIndices() const {
1102 return getNumOperands() > 1;
1103 }
1104
1105 /// Return true if all of the indices of this GEP are
1106 /// zeros. If so, the result pointer and the first operand have the same
1107 /// value, just potentially different types.
1108 bool hasAllZeroIndices() const;
1109
1110 /// Return true if all of the indices of this GEP are
1111 /// constant integers. If so, the result pointer and the first operand have
1112 /// a constant offset between them.
1113 bool hasAllConstantIndices() const;
1114
1115 /// Set or clear the inbounds flag on this GEP instruction.
1116 /// See LangRef.html for the meaning of inbounds on a getelementptr.
1117 void setIsInBounds(bool b = true);
1118
1119 /// Determine whether the GEP has the inbounds flag.
1120 bool isInBounds() const;
1121
1122 /// Accumulate the constant address offset of this GEP if possible.
1123 ///
1124 /// This routine accepts an APInt into which it will accumulate the constant
1125 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
1126 /// all-constant, it returns false and the value of the offset APInt is
1127 /// undefined (it is *not* preserved!). The APInt passed into this routine
1128 /// must be at least as wide as the IntPtr type for the address space of
1129 /// the base GEP pointer.
1130 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
1131 bool collectOffset(const DataLayout &DL, unsigned BitWidth,
1132 MapVector<Value *, APInt> &VariableOffsets,
1133 APInt &ConstantOffset) const;
1134 // Methods for support type inquiry through isa, cast, and dyn_cast:
1135 static bool classof(const Instruction *I) {
1136 return (I->getOpcode() == Instruction::GetElementPtr);
1137 }
1138 static bool classof(const Value *V) {
1139 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1140 }
1141};
1142
1143template <>
1144struct OperandTraits<GetElementPtrInst> :
1145 public VariadicOperandTraits<GetElementPtrInst, 1> {
1146};
1147
1148GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1149 ArrayRef<Value *> IdxList, unsigned Values,
1150 const Twine &NameStr,
1151 Instruction *InsertBefore)
1152 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1153 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1154 Values, InsertBefore),
1155 SourceElementType(PointeeType),
1156 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1157 assert(cast<PointerType>(getType()->getScalarType())(static_cast <bool> (cast<PointerType>(getType()->
getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType
)) ? void (0) : __assert_fail ("cast<PointerType>(getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType)"
, "llvm/include/llvm/IR/Instructions.h", 1158, __extension__ __PRETTY_FUNCTION__
))
1158 ->isOpaqueOrPointeeTypeMatches(ResultElementType))(static_cast <bool> (cast<PointerType>(getType()->
getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType
)) ? void (0) : __assert_fail ("cast<PointerType>(getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType)"
, "llvm/include/llvm/IR/Instructions.h", 1158, __extension__ __PRETTY_FUNCTION__
))
;
1159 init(Ptr, IdxList, NameStr);
1160}
1161
1162GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1163 ArrayRef<Value *> IdxList, unsigned Values,
1164 const Twine &NameStr,
1165 BasicBlock *InsertAtEnd)
1166 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1167 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1168 Values, InsertAtEnd),
1169 SourceElementType(PointeeType),
1170 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1171 assert(cast<PointerType>(getType()->getScalarType())(static_cast <bool> (cast<PointerType>(getType()->
getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType
)) ? void (0) : __assert_fail ("cast<PointerType>(getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType)"
, "llvm/include/llvm/IR/Instructions.h", 1172, __extension__ __PRETTY_FUNCTION__
))
1172 ->isOpaqueOrPointeeTypeMatches(ResultElementType))(static_cast <bool> (cast<PointerType>(getType()->
getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType
)) ? void (0) : __assert_fail ("cast<PointerType>(getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType)"
, "llvm/include/llvm/IR/Instructions.h", 1172, __extension__ __PRETTY_FUNCTION__
))
;
1173 init(Ptr, IdxList, NameStr);
1174}
1175
1176DEFINE_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 { (static_cast <bool> (i_nocapture <
OperandTraits<GetElementPtrInst>::operands(this) &&
"getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<GetElementPtrInst>::operands(this) && \"getOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 1176, __extension__ __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) { (static_cast <bool> (i_nocapture
< OperandTraits<GetElementPtrInst>::operands(this) &&
"setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<GetElementPtrInst>::operands(this) && \"setOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 1176, __extension__ __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
); }
1177
1178//===----------------------------------------------------------------------===//
1179// ICmpInst Class
1180//===----------------------------------------------------------------------===//
1181
1182/// This instruction compares its operands according to the predicate given
1183/// to the constructor. It only operates on integers or pointers. The operands
1184/// must be identical types.
1185/// Represent an integer comparison operator.
1186class ICmpInst: public CmpInst {
1187 void AssertOK() {
1188 assert(isIntPredicate() &&(static_cast <bool> (isIntPredicate() && "Invalid ICmp predicate value"
) ? void (0) : __assert_fail ("isIntPredicate() && \"Invalid ICmp predicate value\""
, "llvm/include/llvm/IR/Instructions.h", 1189, __extension__ __PRETTY_FUNCTION__
))
1189 "Invalid ICmp predicate value")(static_cast <bool> (isIntPredicate() && "Invalid ICmp predicate value"
) ? void (0) : __assert_fail ("isIntPredicate() && \"Invalid ICmp predicate value\""
, "llvm/include/llvm/IR/Instructions.h", 1189, __extension__ __PRETTY_FUNCTION__
))
;
1190 assert(getOperand(0)->getType() == getOperand(1)->getType() &&(static_cast <bool> (getOperand(0)->getType() == getOperand
(1)->getType() && "Both operands to ICmp instruction are not of the same type!"
) ? void (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to ICmp instruction are not of the same type!\""
, "llvm/include/llvm/IR/Instructions.h", 1191, __extension__ __PRETTY_FUNCTION__
))
1191 "Both operands to ICmp instruction are not of the same type!")(static_cast <bool> (getOperand(0)->getType() == getOperand
(1)->getType() && "Both operands to ICmp instruction are not of the same type!"
) ? void (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to ICmp instruction are not of the same type!\""
, "llvm/include/llvm/IR/Instructions.h", 1191, __extension__ __PRETTY_FUNCTION__
))
;
1192 // Check that the operands are the right type
1193 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||(static_cast <bool> ((getOperand(0)->getType()->isIntOrIntVectorTy
() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
"Invalid operand types for ICmp instruction") ? void (0) : __assert_fail
("(getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && \"Invalid operand types for ICmp instruction\""
, "llvm/include/llvm/IR/Instructions.h", 1195, __extension__ __PRETTY_FUNCTION__
))
1194 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&(static_cast <bool> ((getOperand(0)->getType()->isIntOrIntVectorTy
() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
"Invalid operand types for ICmp instruction") ? void (0) : __assert_fail
("(getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && \"Invalid operand types for ICmp instruction\""
, "llvm/include/llvm/IR/Instructions.h", 1195, __extension__ __PRETTY_FUNCTION__
))
1195 "Invalid operand types for ICmp instruction")(static_cast <bool> ((getOperand(0)->getType()->isIntOrIntVectorTy
() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
"Invalid operand types for ICmp instruction") ? void (0) : __assert_fail
("(getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && \"Invalid operand types for ICmp instruction\""
, "llvm/include/llvm/IR/Instructions.h", 1195, __extension__ __PRETTY_FUNCTION__
))
;
1196 }
1197
1198protected:
1199 // Note: Instruction needs to be a friend here to call cloneImpl.
1200 friend class Instruction;
1201
1202 /// Clone an identical ICmpInst
1203 ICmpInst *cloneImpl() const;
1204
1205public:
1206 /// Constructor with insert-before-instruction semantics.
1207 ICmpInst(
1208 Instruction *InsertBefore, ///< Where to insert
1209 Predicate pred, ///< The predicate to use for the comparison
1210 Value *LHS, ///< The left-hand-side of the expression
1211 Value *RHS, ///< The right-hand-side of the expression
1212 const Twine &NameStr = "" ///< Name of the instruction
1213 ) : CmpInst(makeCmpResultType(LHS->getType()),
1214 Instruction::ICmp, pred, LHS, RHS, NameStr,
1215 InsertBefore) {
1216#ifndef NDEBUG
1217 AssertOK();
1218#endif
1219 }
1220
1221 /// Constructor with insert-at-end semantics.
1222 ICmpInst(
1223 BasicBlock &InsertAtEnd, ///< Block to insert into.
1224 Predicate pred, ///< The predicate to use for the comparison
1225 Value *LHS, ///< The left-hand-side of the expression
1226 Value *RHS, ///< The right-hand-side of the expression
1227 const Twine &NameStr = "" ///< Name of the instruction
1228 ) : CmpInst(makeCmpResultType(LHS->getType()),
1229 Instruction::ICmp, pred, LHS, RHS, NameStr,
1230 &InsertAtEnd) {
1231#ifndef NDEBUG
1232 AssertOK();
1233#endif
1234 }
1235
1236 /// Constructor with no-insertion semantics
1237 ICmpInst(
1238 Predicate pred, ///< The predicate to use for the comparison
1239 Value *LHS, ///< The left-hand-side of the expression
1240 Value *RHS, ///< The right-hand-side of the expression
1241 const Twine &NameStr = "" ///< Name of the instruction
1242 ) : CmpInst(makeCmpResultType(LHS->getType()),
1243 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1244#ifndef NDEBUG
1245 AssertOK();
1246#endif
1247 }
1248
1249 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1250 /// @returns the predicate that would be the result if the operand were
1251 /// regarded as signed.
1252 /// Return the signed version of the predicate
1253 Predicate getSignedPredicate() const {
1254 return getSignedPredicate(getPredicate());
1255 }
1256
1257 /// This is a static version that you can use without an instruction.
1258 /// Return the signed version of the predicate.
1259 static Predicate getSignedPredicate(Predicate pred);
1260
1261 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1262 /// @returns the predicate that would be the result if the operand were
1263 /// regarded as unsigned.
1264 /// Return the unsigned version of the predicate
1265 Predicate getUnsignedPredicate() const {
1266 return getUnsignedPredicate(getPredicate());
1267 }
1268
1269 /// This is a static version that you can use without an instruction.
1270 /// Return the unsigned version of the predicate.
1271 static Predicate getUnsignedPredicate(Predicate pred);
1272
1273 /// Return true if this predicate is either EQ or NE. This also
1274 /// tests for commutativity.
1275 static bool isEquality(Predicate P) {
1276 return P == ICMP_EQ || P == ICMP_NE;
1277 }
1278
1279 /// Return true if this predicate is either EQ or NE. This also
1280 /// tests for commutativity.
1281 bool isEquality() const {
1282 return isEquality(getPredicate());
1283 }
1284
1285 /// @returns true if the predicate of this ICmpInst is commutative
1286 /// Determine if this relation is commutative.
1287 bool isCommutative() const { return isEquality(); }
1288
1289 /// Return true if the predicate is relational (not EQ or NE).
1290 ///
1291 bool isRelational() const {
1292 return !isEquality();
1293 }
1294
1295 /// Return true if the predicate is relational (not EQ or NE).
1296 ///
1297 static bool isRelational(Predicate P) {
1298 return !isEquality(P);
1299 }
1300
1301 /// Return true if the predicate is SGT or UGT.
1302 ///
1303 static bool isGT(Predicate P) {
1304 return P == ICMP_SGT || P == ICMP_UGT;
1305 }
1306
1307 /// Return true if the predicate is SLT or ULT.
1308 ///
1309 static bool isLT(Predicate P) {
1310 return P == ICMP_SLT || P == ICMP_ULT;
1311 }
1312
1313 /// Return true if the predicate is SGE or UGE.
1314 ///
1315 static bool isGE(Predicate P) {
1316 return P == ICMP_SGE || P == ICMP_UGE;
1317 }
1318
1319 /// Return true if the predicate is SLE or ULE.
1320 ///
1321 static bool isLE(Predicate P) {
1322 return P == ICMP_SLE || P == ICMP_ULE;
1323 }
1324
1325 /// Returns the sequence of all ICmp predicates.
1326 ///
1327 static auto predicates() { return ICmpPredicates(); }
1328
1329 /// Exchange the two operands to this instruction in such a way that it does
1330 /// not modify the semantics of the instruction. The predicate value may be
1331 /// changed to retain the same result if the predicate is order dependent
1332 /// (e.g. ult).
1333 /// Swap operands and adjust predicate.
1334 void swapOperands() {
1335 setPredicate(getSwappedPredicate());
1336 Op<0>().swap(Op<1>());
1337 }
1338
1339 /// Return result of `LHS Pred RHS` comparison.
1340 static bool compare(const APInt &LHS, const APInt &RHS,
1341 ICmpInst::Predicate Pred);
1342
1343 // Methods for support type inquiry through isa, cast, and dyn_cast:
1344 static bool classof(const Instruction *I) {
1345 return I->getOpcode() == Instruction::ICmp;
1346 }
1347 static bool classof(const Value *V) {
1348 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1349 }
1350};
1351
1352//===----------------------------------------------------------------------===//
1353// FCmpInst Class
1354//===----------------------------------------------------------------------===//
1355
1356/// This instruction compares its operands according to the predicate given
1357/// to the constructor. It only operates on floating point values or packed
1358/// vectors of floating point values. The operands must be identical types.
1359/// Represents a floating point comparison operator.
1360class FCmpInst: public CmpInst {
1361 void AssertOK() {
1362 assert(isFPPredicate() && "Invalid FCmp predicate value")(static_cast <bool> (isFPPredicate() && "Invalid FCmp predicate value"
) ? void (0) : __assert_fail ("isFPPredicate() && \"Invalid FCmp predicate value\""
, "llvm/include/llvm/IR/Instructions.h", 1362, __extension__ __PRETTY_FUNCTION__
))
;
1363 assert(getOperand(0)->getType() == getOperand(1)->getType() &&(static_cast <bool> (getOperand(0)->getType() == getOperand
(1)->getType() && "Both operands to FCmp instruction are not of the same type!"
) ? void (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to FCmp instruction are not of the same type!\""
, "llvm/include/llvm/IR/Instructions.h", 1364, __extension__ __PRETTY_FUNCTION__
))
1364 "Both operands to FCmp instruction are not of the same type!")(static_cast <bool> (getOperand(0)->getType() == getOperand
(1)->getType() && "Both operands to FCmp instruction are not of the same type!"
) ? void (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to FCmp instruction are not of the same type!\""
, "llvm/include/llvm/IR/Instructions.h", 1364, __extension__ __PRETTY_FUNCTION__
))
;
1365 // Check that the operands are the right type
1366 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&(static_cast <bool> (getOperand(0)->getType()->isFPOrFPVectorTy
() && "Invalid operand types for FCmp instruction") ?
void (0) : __assert_fail ("getOperand(0)->getType()->isFPOrFPVectorTy() && \"Invalid operand types for FCmp instruction\""
, "llvm/include/llvm/IR/Instructions.h", 1367, __extension__ __PRETTY_FUNCTION__
))
1367 "Invalid operand types for FCmp instruction")(static_cast <bool> (getOperand(0)->getType()->isFPOrFPVectorTy
() && "Invalid operand types for FCmp instruction") ?
void (0) : __assert_fail ("getOperand(0)->getType()->isFPOrFPVectorTy() && \"Invalid operand types for FCmp instruction\""
, "llvm/include/llvm/IR/Instructions.h", 1367, __extension__ __PRETTY_FUNCTION__
))
;
1368 }
1369
1370protected:
1371 // Note: Instruction needs to be a friend here to call cloneImpl.
1372 friend class Instruction;
1373
1374 /// Clone an identical FCmpInst
1375 FCmpInst *cloneImpl() const;
1376
1377public:
1378 /// Constructor with insert-before-instruction semantics.
1379 FCmpInst(
1380 Instruction *InsertBefore, ///< Where to insert
1381 Predicate pred, ///< The predicate to use for the comparison
1382 Value *LHS, ///< The left-hand-side of the expression
1383 Value *RHS, ///< The right-hand-side of the expression
1384 const Twine &NameStr = "" ///< Name of the instruction
1385 ) : CmpInst(makeCmpResultType(LHS->getType()),
1386 Instruction::FCmp, pred, LHS, RHS, NameStr,
1387 InsertBefore) {
1388 AssertOK();
1389 }
1390
1391 /// Constructor with insert-at-end semantics.
1392 FCmpInst(
1393 BasicBlock &InsertAtEnd, ///< Block to insert into.
1394 Predicate pred, ///< The predicate to use for the comparison
1395 Value *LHS, ///< The left-hand-side of the expression
1396 Value *RHS, ///< The right-hand-side of the expression
1397 const Twine &NameStr = "" ///< Name of the instruction
1398 ) : CmpInst(makeCmpResultType(LHS->getType()),
1399 Instruction::FCmp, pred, LHS, RHS, NameStr,
1400 &InsertAtEnd) {
1401 AssertOK();
1402 }
1403
1404 /// Constructor with no-insertion semantics
1405 FCmpInst(
1406 Predicate Pred, ///< The predicate to use for the comparison
1407 Value *LHS, ///< The left-hand-side of the expression
1408 Value *RHS, ///< The right-hand-side of the expression
1409 const Twine &NameStr = "", ///< Name of the instruction
1410 Instruction *FlagsSource = nullptr
1411 ) : CmpInst(makeCmpResultType(LHS->getType()), Instruction::FCmp, Pred, LHS,
1412 RHS, NameStr, nullptr, FlagsSource) {
1413 AssertOK();
1414 }
1415
1416 /// @returns true if the predicate of this instruction is EQ or NE.
1417 /// Determine if this is an equality predicate.
1418 static bool isEquality(Predicate Pred) {
1419 return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ ||
1420 Pred == FCMP_UNE;
1421 }
1422
1423 /// @returns true if the predicate of this instruction is EQ or NE.
1424 /// Determine if this is an equality predicate.
1425 bool isEquality() const { return isEquality(getPredicate()); }
1426
1427 /// @returns true if the predicate of this instruction is commutative.
1428 /// Determine if this is a commutative predicate.
1429 bool isCommutative() const {
1430 return isEquality() ||
1431 getPredicate() == FCMP_FALSE ||
1432 getPredicate() == FCMP_TRUE ||
1433 getPredicate() == FCMP_ORD ||
1434 getPredicate() == FCMP_UNO;
1435 }
1436
1437 /// @returns true if the predicate is relational (not EQ or NE).
1438 /// Determine if this a relational predicate.
1439 bool isRelational() const { return !isEquality(); }
1440
1441 /// Exchange the two operands to this instruction in such a way that it does
1442 /// not modify the semantics of the instruction. The predicate value may be
1443 /// changed to retain the same result if the predicate is order dependent
1444 /// (e.g. ult).
1445 /// Swap operands and adjust predicate.
1446 void swapOperands() {
1447 setPredicate(getSwappedPredicate());
1448 Op<0>().swap(Op<1>());
1449 }
1450
1451 /// Returns the sequence of all FCmp predicates.
1452 ///
1453 static auto predicates() { return FCmpPredicates(); }
1454
1455 /// Return result of `LHS Pred RHS` comparison.
1456 static bool compare(const APFloat &LHS, const APFloat &RHS,
1457 FCmpInst::Predicate Pred);
1458
1459 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1460 static bool classof(const Instruction *I) {
1461 return I->getOpcode() == Instruction::FCmp;
1462 }
1463 static bool classof(const Value *V) {
1464 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1465 }
1466};
1467
1468//===----------------------------------------------------------------------===//
1469/// This class represents a function call, abstracting a target
1470/// machine's calling convention. This class uses low bit of the SubClassData
1471/// field to indicate whether or not this is a tail call. The rest of the bits
1472/// hold the calling convention of the call.
1473///
1474class CallInst : public CallBase {
1475 CallInst(const CallInst &CI);
1476
1477 /// Construct a CallInst given a range of arguments.
1478 /// Construct a CallInst from a range of arguments
1479 inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1480 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1481 Instruction *InsertBefore);
1482
1483 inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1484 const Twine &NameStr, Instruction *InsertBefore)
1485 : CallInst(Ty, Func, Args, None, NameStr, InsertBefore) {}
1486
1487 /// Construct a CallInst given a range of arguments.
1488 /// Construct a CallInst from a range of arguments
1489 inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1490 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1491 BasicBlock *InsertAtEnd);
1492
1493 explicit CallInst(FunctionType *Ty, Value *F, const Twine &NameStr,
1494 Instruction *InsertBefore);
1495
1496 CallInst(FunctionType *ty, Value *F, const Twine &NameStr,
1497 BasicBlock *InsertAtEnd);
1498
1499 void init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
1500 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
1501 void init(FunctionType *FTy, Value *Func, const Twine &NameStr);
1502
1503 /// Compute the number of operands to allocate.
1504 static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) {
1505 // We need one operand for the called function, plus the input operand
1506 // counts provided.
1507 return 1 + NumArgs + NumBundleInputs;
1508 }
1509
1510protected:
1511 // Note: Instruction needs to be a friend here to call cloneImpl.
1512 friend class Instruction;
1513
1514 CallInst *cloneImpl() const;
1515
1516public:
1517 static CallInst *Create(FunctionType *Ty, Value *F, const Twine &NameStr = "",
1518 Instruction *InsertBefore = nullptr) {
1519 return new (ComputeNumOperands(0)) CallInst(Ty, F, NameStr, InsertBefore);
1520 }
1521
1522 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1523 const Twine &NameStr,
1524 Instruction *InsertBefore = nullptr) {
1525 return new (ComputeNumOperands(Args.size()))
1526 CallInst(Ty, Func, Args, None, NameStr, InsertBefore);
1527 }
1528
1529 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1530 ArrayRef<OperandBundleDef> Bundles = None,
1531 const Twine &NameStr = "",
1532 Instruction *InsertBefore = nullptr) {
1533 const int NumOperands =
1534 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles));
1535 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
1536
1537 return new (NumOperands, DescriptorBytes)
1538 CallInst(Ty, Func, Args, Bundles, NameStr, InsertBefore);
1539 }
1540
1541 static CallInst *Create(FunctionType *Ty, Value *F, const Twine &NameStr,
1542 BasicBlock *InsertAtEnd) {
1543 return new (ComputeNumOperands(0)) CallInst(Ty, F, NameStr, InsertAtEnd);
1544 }
1545
1546 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1547 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1548 return new (ComputeNumOperands(Args.size()))
1549 CallInst(Ty, Func, Args, None, NameStr, InsertAtEnd);
1550 }
1551
1552 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1553 ArrayRef<OperandBundleDef> Bundles,
1554 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1555 const int NumOperands =
1556 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles));
1557 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
1558
1559 return new (NumOperands, DescriptorBytes)
1560 CallInst(Ty, Func, Args, Bundles, NameStr, InsertAtEnd);
1561 }
1562
1563 static CallInst *Create(FunctionCallee Func, const Twine &NameStr = "",
1564 Instruction *InsertBefore = nullptr) {
1565 return Create(Func.getFunctionType(), Func.getCallee(), NameStr,
1566 InsertBefore);
1567 }
1568
1569 static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args,
1570 ArrayRef<OperandBundleDef> Bundles = None,
1571 const Twine &NameStr = "",
1572 Instruction *InsertBefore = nullptr) {
1573 return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles,
1574 NameStr, InsertBefore);
1575 }
1576
1577 static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args,
1578 const Twine &NameStr,
1579 Instruction *InsertBefore = nullptr) {
1580 return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr,
1581 InsertBefore);
1582 }
1583
1584 static CallInst *Create(FunctionCallee Func, const Twine &NameStr,
1585 BasicBlock *InsertAtEnd) {
1586 return Create(Func.getFunctionType(), Func.getCallee(), NameStr,
1587 InsertAtEnd);
1588 }
1589
1590 static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args,
1591 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1592 return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr,
1593 InsertAtEnd);
1594 }
1595
1596 static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args,
1597 ArrayRef<OperandBundleDef> Bundles,
1598 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1599 return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles,
1600 NameStr, InsertAtEnd);
1601 }
1602
1603 /// Create a clone of \p CI with a different set of operand bundles and
1604 /// insert it before \p InsertPt.
1605 ///
1606 /// The returned call instruction is identical \p CI in every way except that
1607 /// the operand bundles for the new instruction are set to the operand bundles
1608 /// in \p Bundles.
1609 static CallInst *Create(CallInst *CI, ArrayRef<OperandBundleDef> Bundles,
1610 Instruction *InsertPt = nullptr);
1611
1612 /// Generate the IR for a call to malloc:
1613 /// 1. Compute the malloc call's argument as the specified type's size,
1614 /// possibly multiplied by the array size if the array size is not
1615 /// constant 1.
1616 /// 2. Call malloc with that argument.
1617 /// 3. Bitcast the result of the malloc call to the specified type.
1618 static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy,
1619 Type *AllocTy, Value *AllocSize,
1620 Value *ArraySize = nullptr,
1621 Function *MallocF = nullptr,
1622 const Twine &Name = "");
1623 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy,
1624 Type *AllocTy, Value *AllocSize,
1625 Value *ArraySize = nullptr,
1626 Function *MallocF = nullptr,
1627 const Twine &Name = "");
1628 static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy,
1629 Type *AllocTy, Value *AllocSize,
1630 Value *ArraySize = nullptr,
1631 ArrayRef<OperandBundleDef> Bundles = None,
1632 Function *MallocF = nullptr,
1633 const Twine &Name = "");
1634 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy,
1635 Type *AllocTy, Value *AllocSize,
1636 Value *ArraySize = nullptr,
1637 ArrayRef<OperandBundleDef> Bundles = None,
1638 Function *MallocF = nullptr,
1639 const Twine &Name = "");
1640 /// Generate the IR for a call to the builtin free function.
1641 static Instruction *CreateFree(Value *Source, Instruction *InsertBefore);
1642 static Instruction *CreateFree(Value *Source, BasicBlock *InsertAtEnd);
1643 static Instruction *CreateFree(Value *Source,
1644 ArrayRef<OperandBundleDef> Bundles,
1645 Instruction *InsertBefore);
1646 static Instruction *CreateFree(Value *Source,
1647 ArrayRef<OperandBundleDef> Bundles,
1648 BasicBlock *InsertAtEnd);
1649
1650 // Note that 'musttail' implies 'tail'.
1651 enum TailCallKind : unsigned {
1652 TCK_None = 0,
1653 TCK_Tail = 1,
1654 TCK_MustTail = 2,
1655 TCK_NoTail = 3,
1656 TCK_LAST = TCK_NoTail
1657 };
1658
1659 using TailCallKindField = Bitfield::Element<TailCallKind, 0, 2, TCK_LAST>;
1660 static_assert(
1661 Bitfield::areContiguous<TailCallKindField, CallBase::CallingConvField>(),
1662 "Bitfields must be contiguous");
1663
1664 TailCallKind getTailCallKind() const {
1665 return getSubclassData<TailCallKindField>();
1666 }
1667
1668 bool isTailCall() const {
1669 TailCallKind Kind = getTailCallKind();
1670 return Kind == TCK_Tail || Kind == TCK_MustTail;
1671 }
1672
1673 bool isMustTailCall() const { return getTailCallKind() == TCK_MustTail; }
1674
1675 bool isNoTailCall() const { return getTailCallKind() == TCK_NoTail; }
1676
1677 void setTailCallKind(TailCallKind TCK) {
1678 setSubclassData<TailCallKindField>(TCK);
1679 }
1680
1681 void setTailCall(bool IsTc = true) {
1682 setTailCallKind(IsTc ? TCK_Tail : TCK_None);
1683 }
1684
1685 /// Return true if the call can return twice
1686 bool canReturnTwice() const { return hasFnAttr(Attribute::ReturnsTwice); }
1687 void setCanReturnTwice() { addFnAttr(Attribute::ReturnsTwice); }
1688
1689 // Methods for support type inquiry through isa, cast, and dyn_cast:
1690 static bool classof(const Instruction *I) {
1691 return I->getOpcode() == Instruction::Call;
1692 }
1693 static bool classof(const Value *V) {
1694 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1695 }
1696
1697 /// Updates profile metadata by scaling it by \p S / \p T.
1698 void updateProfWeight(uint64_t S, uint64_t T);
1699
1700private:
1701 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1702 // method so that subclasses cannot accidentally use it.
1703 template <typename Bitfield>
1704 void setSubclassData(typename Bitfield::Type Value) {
1705 Instruction::setSubclassData<Bitfield>(Value);
1706 }
1707};
1708
1709CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1710 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1711 BasicBlock *InsertAtEnd)
1712 : CallBase(Ty->getReturnType(), Instruction::Call,
1713 OperandTraits<CallBase>::op_end(this) -
1714 (Args.size() + CountBundleInputs(Bundles) + 1),
1715 unsigned(Args.size() + CountBundleInputs(Bundles) + 1),
1716 InsertAtEnd) {
1717 init(Ty, Func, Args, Bundles, NameStr);
1718}
1719
1720CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1721 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1722 Instruction *InsertBefore)
1723 : CallBase(Ty->getReturnType(), Instruction::Call,
1724 OperandTraits<CallBase>::op_end(this) -
1725 (Args.size() + CountBundleInputs(Bundles) + 1),
1726 unsigned(Args.size() + CountBundleInputs(Bundles) + 1),
1727 InsertBefore) {
1728 init(Ty, Func, Args, Bundles, NameStr);
1729}
1730
1731//===----------------------------------------------------------------------===//
1732// SelectInst Class
1733//===----------------------------------------------------------------------===//
1734
1735/// This class represents the LLVM 'select' instruction.
1736///
1737class SelectInst : public Instruction {
1738 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1739 Instruction *InsertBefore)
1740 : Instruction(S1->getType(), Instruction::Select,
1741 &Op<0>(), 3, InsertBefore) {
1742 init(C, S1, S2);
1743 setName(NameStr);
1744 }
1745
1746 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1747 BasicBlock *InsertAtEnd)
1748 : Instruction(S1->getType(), Instruction::Select,
1749 &Op<0>(), 3, InsertAtEnd) {
1750 init(C, S1, S2);
1751 setName(NameStr);
1752 }
1753
1754 void init(Value *C, Value *S1, Value *S2) {
1755 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select")(static_cast <bool> (!areInvalidOperands(C, S1, S2) &&
"Invalid operands for select") ? void (0) : __assert_fail ("!areInvalidOperands(C, S1, S2) && \"Invalid operands for select\""
, "llvm/include/llvm/IR/Instructions.h", 1755, __extension__ __PRETTY_FUNCTION__
))
;
1756 Op<0>() = C;
1757 Op<1>() = S1;
1758 Op<2>() = S2;
1759 }
1760
1761protected:
1762 // Note: Instruction needs to be a friend here to call cloneImpl.
1763 friend class Instruction;
1764
1765 SelectInst *cloneImpl() const;
1766
1767public:
1768 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1769 const Twine &NameStr = "",
1770 Instruction *InsertBefore = nullptr,
1771 Instruction *MDFrom = nullptr) {
1772 SelectInst *Sel = new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1773 if (MDFrom)
1774 Sel->copyMetadata(*MDFrom);
1775 return Sel;
1776 }
1777
1778 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1779 const Twine &NameStr,
1780 BasicBlock *InsertAtEnd) {
1781 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1782 }
1783
1784 const Value *getCondition() const { return Op<0>(); }
1785 const Value *getTrueValue() const { return Op<1>(); }
1786 const Value *getFalseValue() const { return Op<2>(); }
1787 Value *getCondition() { return Op<0>(); }
1788 Value *getTrueValue() { return Op<1>(); }
1789 Value *getFalseValue() { return Op<2>(); }
1790
1791 void setCondition(Value *V) { Op<0>() = V; }
1792 void setTrueValue(Value *V) { Op<1>() = V; }
1793 void setFalseValue(Value *V) { Op<2>() = V; }
1794
1795 /// Swap the true and false values of the select instruction.
1796 /// This doesn't swap prof metadata.
1797 void swapValues() { Op<1>().swap(Op<2>()); }
1798
1799 /// Return a string if the specified operands are invalid
1800 /// for a select operation, otherwise return null.
1801 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1802
1803 /// Transparently provide more efficient getOperand methods.
1804 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
;
1805
1806 OtherOps getOpcode() const {
1807 return static_cast<OtherOps>(Instruction::getOpcode());
1808 }
1809
1810 // Methods for support type inquiry through isa, cast, and dyn_cast:
1811 static bool classof(const Instruction *I) {
1812 return I->getOpcode() == Instruction::Select;
1813 }
1814 static bool classof(const Value *V) {
1815 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1816 }
1817};
1818
1819template <>
1820struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1821};
1822
1823DEFINE_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 { (static_cast <bool
> (i_nocapture < OperandTraits<SelectInst>::operands
(this) && "getOperand() out of range!") ? void (0) : __assert_fail
("i_nocapture < OperandTraits<SelectInst>::operands(this) && \"getOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 1823, __extension__ __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) { (static_cast <bool> (i_nocapture
< OperandTraits<SelectInst>::operands(this) &&
"setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<SelectInst>::operands(this) && \"setOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 1823, __extension__ __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); }
1824
1825//===----------------------------------------------------------------------===//
1826// VAArgInst Class
1827//===----------------------------------------------------------------------===//
1828
1829/// This class represents the va_arg llvm instruction, which returns
1830/// an argument of the specified type given a va_list and increments that list
1831///
1832class VAArgInst : public UnaryInstruction {
1833protected:
1834 // Note: Instruction needs to be a friend here to call cloneImpl.
1835 friend class Instruction;
1836
1837 VAArgInst *cloneImpl() const;
1838
1839public:
1840 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1841 Instruction *InsertBefore = nullptr)
1842 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1843 setName(NameStr);
1844 }
1845
1846 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1847 BasicBlock *InsertAtEnd)
1848 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1849 setName(NameStr);
1850 }
1851
1852 Value *getPointerOperand() { return getOperand(0); }
1853 const Value *getPointerOperand() const { return getOperand(0); }
1854 static unsigned getPointerOperandIndex() { return 0U; }
1855
1856 // Methods for support type inquiry through isa, cast, and dyn_cast:
1857 static bool classof(const Instruction *I) {
1858 return I->getOpcode() == VAArg;
1859 }
1860 static bool classof(const Value *V) {
1861 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1862 }
1863};
1864
1865//===----------------------------------------------------------------------===//
1866// ExtractElementInst Class
1867//===----------------------------------------------------------------------===//
1868
1869/// This instruction extracts a single (scalar)
1870/// element from a VectorType value
1871///
1872class ExtractElementInst : public Instruction {
1873 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1874 Instruction *InsertBefore = nullptr);
1875 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1876 BasicBlock *InsertAtEnd);
1877
1878protected:
1879 // Note: Instruction needs to be a friend here to call cloneImpl.
1880 friend class Instruction;
1881
1882 ExtractElementInst *cloneImpl() const;
1883
1884public:
1885 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1886 const Twine &NameStr = "",
1887 Instruction *InsertBefore = nullptr) {
1888 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1889 }
1890
1891 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1892 const Twine &NameStr,
1893 BasicBlock *InsertAtEnd) {
1894 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1895 }
1896
1897 /// Return true if an extractelement instruction can be
1898 /// formed with the specified operands.
1899 static bool isValidOperands(const Value *Vec, const Value *Idx);
1900
1901 Value *getVectorOperand() { return Op<0>(); }
1902 Value *getIndexOperand() { return Op<1>(); }
1903 const Value *getVectorOperand() const { return Op<0>(); }
1904 const Value *getIndexOperand() const { return Op<1>(); }
1905
1906 VectorType *getVectorOperandType() const {
1907 return cast<VectorType>(getVectorOperand()->getType());
1908 }
1909
1910 /// Transparently provide more efficient getOperand methods.
1911 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
;
1912
1913 // Methods for support type inquiry through isa, cast, and dyn_cast:
1914 static bool classof(const Instruction *I) {
1915 return I->getOpcode() == Instruction::ExtractElement;
1916 }
1917 static bool classof(const Value *V) {
1918 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1919 }
1920};
1921
1922template <>
1923struct OperandTraits<ExtractElementInst> :
1924 public FixedNumOperandTraits<ExtractElementInst, 2> {
1925};
1926
1927DEFINE_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 {
(static_cast <bool> (i_nocapture < OperandTraits<
ExtractElementInst>::operands(this) && "getOperand() out of range!"
) ? void (0) : __assert_fail ("i_nocapture < OperandTraits<ExtractElementInst>::operands(this) && \"getOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 1927, __extension__ __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) { (static_cast <bool> (i_nocapture
< OperandTraits<ExtractElementInst>::operands(this)
&& "setOperand() out of range!") ? void (0) : __assert_fail
("i_nocapture < OperandTraits<ExtractElementInst>::operands(this) && \"setOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 1927, __extension__ __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
); }
1928
1929//===----------------------------------------------------------------------===//
1930// InsertElementInst Class
1931//===----------------------------------------------------------------------===//
1932
1933/// This instruction inserts a single (scalar)
1934/// element into a VectorType value
1935///
1936class InsertElementInst : public Instruction {
1937 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1938 const Twine &NameStr = "",
1939 Instruction *InsertBefore = nullptr);
1940 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr,
1941 BasicBlock *InsertAtEnd);
1942
1943protected:
1944 // Note: Instruction needs to be a friend here to call cloneImpl.
1945 friend class Instruction;
1946
1947 InsertElementInst *cloneImpl() const;
1948
1949public:
1950 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1951 const Twine &NameStr = "",
1952 Instruction *InsertBefore = nullptr) {
1953 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1954 }
1955
1956 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1957 const Twine &NameStr,
1958 BasicBlock *InsertAtEnd) {
1959 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1960 }
1961
1962 /// Return true if an insertelement instruction can be
1963 /// formed with the specified operands.
1964 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1965 const Value *Idx);
1966
1967 /// Overload to return most specific vector type.
1968 ///
1969 VectorType *getType() const {
1970 return cast<VectorType>(Instruction::getType());
1971 }
1972
1973 /// Transparently provide more efficient getOperand methods.
1974 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
;
1975
1976 // Methods for support type inquiry through isa, cast, and dyn_cast:
1977 static bool classof(const Instruction *I) {
1978 return I->getOpcode() == Instruction::InsertElement;
1979 }
1980 static bool classof(const Value *V) {
1981 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1982 }
1983};
1984
1985template <>
1986struct OperandTraits<InsertElementInst> :
1987 public FixedNumOperandTraits<InsertElementInst, 3> {
1988};
1989
1990DEFINE_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 { (static_cast <bool> (i_nocapture <
OperandTraits<InsertElementInst>::operands(this) &&
"getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<InsertElementInst>::operands(this) && \"getOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 1990, __extension__ __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) { (static_cast <bool> (i_nocapture
< OperandTraits<InsertElementInst>::operands(this) &&
"setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<InsertElementInst>::operands(this) && \"setOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 1990, __extension__ __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
); }
1991
1992//===----------------------------------------------------------------------===//
1993// ShuffleVectorInst Class
1994//===----------------------------------------------------------------------===//
1995
1996constexpr int UndefMaskElem = -1;
1997
1998/// This instruction constructs a fixed permutation of two
1999/// input vectors.
2000///
2001/// For each element of the result vector, the shuffle mask selects an element
2002/// from one of the input vectors to copy to the result. Non-negative elements
2003/// in the mask represent an index into the concatenated pair of input vectors.
2004/// UndefMaskElem (-1) specifies that the result element is undefined.
2005///
2006/// For scalable vectors, all the elements of the mask must be 0 or -1. This
2007/// requirement may be relaxed in the future.
2008class ShuffleVectorInst : public Instruction {
2009 SmallVector<int, 4> ShuffleMask;
2010 Constant *ShuffleMaskForBitcode;
2011
2012protected:
2013 // Note: Instruction needs to be a friend here to call cloneImpl.
2014 friend class Instruction;
2015
2016 ShuffleVectorInst *cloneImpl() const;
2017
2018public:
2019 ShuffleVectorInst(Value *V1, Value *Mask, const Twine &NameStr = "",
2020 Instruction *InsertBefore = nullptr);
2021 ShuffleVectorInst(Value *V1, Value *Mask, const Twine &NameStr,
2022 BasicBlock *InsertAtEnd);
2023 ShuffleVectorInst(Value *V1, ArrayRef<int> Mask, const Twine &NameStr = "",
2024 Instruction *InsertBefore = nullptr);
2025 ShuffleVectorInst(Value *V1, ArrayRef<int> Mask, const Twine &NameStr,
2026 BasicBlock *InsertAtEnd);
2027 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
2028 const Twine &NameStr = "",
2029 Instruction *InsertBefor = nullptr);
2030 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
2031 const Twine &NameStr, BasicBlock *InsertAtEnd);
2032 ShuffleVectorInst(Value *V1, Value *V2, ArrayRef<int> Mask,
2033 const Twine &NameStr = "",
2034 Instruction *InsertBefor = nullptr);
2035 ShuffleVectorInst(Value *V1, Value *V2, ArrayRef<int> Mask,
2036 const Twine &NameStr, BasicBlock *InsertAtEnd);
2037
2038 void *operator new(size_t S) { return User::operator new(S, 2); }
2039 void operator delete(void *Ptr) { return User::operator delete(Ptr); }
2040
2041 /// Swap the operands and adjust the mask to preserve the semantics
2042 /// of the instruction.
2043 void commute();
2044
2045 /// Return true if a shufflevector instruction can be
2046 /// formed with the specified operands.
2047 static bool isValidOperands(const Value *V1, const Value *V2,
2048 const Value *Mask);
2049 static bool isValidOperands(const Value *V1, const Value *V2,
2050 ArrayRef<int> Mask);
2051
2052 /// Overload to return most specific vector type.
2053 ///
2054 VectorType *getType() const {
2055 return cast<VectorType>(Instruction::getType());
2056 }
2057
2058 /// Transparently provide more efficient getOperand methods.
2059 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
;
2060
2061 /// Return the shuffle mask value of this instruction for the given element
2062 /// index. Return UndefMaskElem if the element is undef.
2063 int getMaskValue(unsigned Elt) const { return ShuffleMask[Elt]; }
2064
2065 /// Convert the input shuffle mask operand to a vector of integers. Undefined
2066 /// elements of the mask are returned as UndefMaskElem.
2067 static void getShuffleMask(const Constant *Mask,
2068 SmallVectorImpl<int> &Result);
2069
2070 /// Return the mask for this instruction as a vector of integers. Undefined
2071 /// elements of the mask are returned as UndefMaskElem.
2072 void getShuffleMask(SmallVectorImpl<int> &Result) const {
2073 Result.assign(ShuffleMask.begin(), ShuffleMask.end());
2074 }
2075
2076 /// Return the mask for this instruction, for use in bitcode.
2077 ///
2078 /// TODO: This is temporary until we decide a new bitcode encoding for
2079 /// shufflevector.
2080 Constant *getShuffleMaskForBitcode() const { return ShuffleMaskForBitcode; }
2081
2082 static Constant *convertShuffleMaskForBitcode(ArrayRef<int> Mask,
2083 Type *ResultTy);
2084
2085 void setShuffleMask(ArrayRef<int> Mask);
2086
2087 ArrayRef<int> getShuffleMask() const { return ShuffleMask; }
2088
2089 /// Return true if this shuffle returns a vector with a different number of
2090 /// elements than its source vectors.
2091 /// Examples: shufflevector <4 x n> A, <4 x n> B, <1,2,3>
2092 /// shufflevector <4 x n> A, <4 x n> B, <1,2,3,4,5>
2093 bool changesLength() const {
2094 unsigned NumSourceElts = cast<VectorType>(Op<0>()->getType())
2095 ->getElementCount()
2096 .getKnownMinValue();
2097 unsigned NumMaskElts = ShuffleMask.size();
2098 return NumSourceElts != NumMaskElts;
2099 }
2100
2101 /// Return true if this shuffle returns a vector with a greater number of
2102 /// elements than its source vectors.
2103 /// Example: shufflevector <2 x n> A, <2 x n> B, <1,2,3>
2104 bool increasesLength() const {
2105 unsigned NumSourceElts = cast<VectorType>(Op<0>()->getType())
2106 ->getElementCount()
2107 .getKnownMinValue();
2108 unsigned NumMaskElts = ShuffleMask.size();
2109 return NumSourceElts < NumMaskElts;
2110 }
2111
2112 /// Return true if this shuffle mask chooses elements from exactly one source
2113 /// vector.
2114 /// Example: <7,5,undef,7>
2115 /// This assumes that vector operands are the same length as the mask.
2116 static bool isSingleSourceMask(ArrayRef<int> Mask);
2117 static bool isSingleSourceMask(const Constant *Mask) {
2118 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy(
) && "Shuffle needs vector constant.") ? void (0) : __assert_fail
("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\""
, "llvm/include/llvm/IR/Instructions.h", 2118, __extension__ __PRETTY_FUNCTION__
))
;
2119 SmallVector<int, 16> MaskAsInts;
2120 getShuffleMask(Mask, MaskAsInts);
2121 return isSingleSourceMask(MaskAsInts);
2122 }
2123
2124 /// Return true if this shuffle chooses elements from exactly one source
2125 /// vector without changing the length of that vector.
2126 /// Example: shufflevector <4 x n> A, <4 x n> B, <3,0,undef,3>
2127 /// TODO: Optionally allow length-changing shuffles.
2128 bool isSingleSource() const {
2129 return !changesLength() && isSingleSourceMask(ShuffleMask);
2130 }
2131
2132 /// Return true if this shuffle mask chooses elements from exactly one source
2133 /// vector without lane crossings. A shuffle using this mask is not
2134 /// necessarily a no-op because it may change the number of elements from its
2135 /// input vectors or it may provide demanded bits knowledge via undef lanes.
2136 /// Example: <undef,undef,2,3>
2137 static bool isIdentityMask(ArrayRef<int> Mask);
2138 static bool isIdentityMask(const Constant *Mask) {
2139 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy(
) && "Shuffle needs vector constant.") ? void (0) : __assert_fail
("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\""
, "llvm/include/llvm/IR/Instructions.h", 2139, __extension__ __PRETTY_FUNCTION__
))
;
2140
2141 // Not possible to express a shuffle mask for a scalable vector for this
2142 // case.
2143 if (isa<ScalableVectorType>(Mask->getType()))
2144 return false;
2145
2146 SmallVector<int, 16> MaskAsInts;
2147 getShuffleMask(Mask, MaskAsInts);
2148 return isIdentityMask(MaskAsInts);
2149 }
2150
2151 /// Return true if this shuffle chooses elements from exactly one source
2152 /// vector without lane crossings and does not change the number of elements
2153 /// from its input vectors.
2154 /// Example: shufflevector <4 x n> A, <4 x n> B, <4,undef,6,undef>
2155 bool isIdentity() const {
2156 // Not possible to express a shuffle mask for a scalable vector for this
2157 // case.
2158 if (isa<ScalableVectorType>(getType()))
2159 return false;
2160
2161 return !changesLength() && isIdentityMask(ShuffleMask);
2162 }
2163
2164 /// Return true if this shuffle lengthens exactly one source vector with
2165 /// undefs in the high elements.
2166 bool isIdentityWithPadding() const;
2167
2168 /// Return true if this shuffle extracts the first N elements of exactly one
2169 /// source vector.
2170 bool isIdentityWithExtract() const;
2171
2172 /// Return true if this shuffle concatenates its 2 source vectors. This
2173 /// returns false if either input is undefined. In that case, the shuffle is
2174 /// is better classified as an identity with padding operation.
2175 bool isConcat() const;
2176
2177 /// Return true if this shuffle mask chooses elements from its source vectors
2178 /// without lane crossings. A shuffle using this mask would be
2179 /// equivalent to a vector select with a constant condition operand.
2180 /// Example: <4,1,6,undef>
2181 /// This returns false if the mask does not choose from both input vectors.
2182 /// In that case, the shuffle is better classified as an identity shuffle.
2183 /// This assumes that vector operands are the same length as the mask
2184 /// (a length-changing shuffle can never be equivalent to a vector select).
2185 static bool isSelectMask(ArrayRef<int> Mask);
2186 static bool isSelectMask(const Constant *Mask) {
2187 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy(
) && "Shuffle needs vector constant.") ? void (0) : __assert_fail
("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\""
, "llvm/include/llvm/IR/Instructions.h", 2187, __extension__ __PRETTY_FUNCTION__
))
;
2188 SmallVector<int, 16> MaskAsInts;
2189 getShuffleMask(Mask, MaskAsInts);
2190 return isSelectMask(MaskAsInts);
2191 }
2192
2193 /// Return true if this shuffle chooses elements from its source vectors
2194 /// without lane crossings and all operands have the same number of elements.
2195 /// In other words, this shuffle is equivalent to a vector select with a
2196 /// constant condition operand.
2197 /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,1,6,3>
2198 /// This returns false if the mask does not choose from both input vectors.
2199 /// In that case, the shuffle is better classified as an identity shuffle.
2200 /// TODO: Optionally allow length-changing shuffles.
2201 bool isSelect() const {
2202 return !changesLength() && isSelectMask(ShuffleMask);
2203 }
2204
2205 /// Return true if this shuffle mask swaps the order of elements from exactly
2206 /// one source vector.
2207 /// Example: <7,6,undef,4>
2208 /// This assumes that vector operands are the same length as the mask.
2209 static bool isReverseMask(ArrayRef<int> Mask);
2210 static bool isReverseMask(const Constant *Mask) {
2211 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy(
) && "Shuffle needs vector constant.") ? void (0) : __assert_fail
("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\""
, "llvm/include/llvm/IR/Instructions.h", 2211, __extension__ __PRETTY_FUNCTION__
))
;
2212 SmallVector<int, 16> MaskAsInts;
2213 getShuffleMask(Mask, MaskAsInts);
2214 return isReverseMask(MaskAsInts);
2215 }
2216
2217 /// Return true if this shuffle swaps the order of elements from exactly
2218 /// one source vector.
2219 /// Example: shufflevector <4 x n> A, <4 x n> B, <3,undef,1,undef>
2220 /// TODO: Optionally allow length-changing shuffles.
2221 bool isReverse() const {
2222 return !changesLength() && isReverseMask(ShuffleMask);
2223 }
2224
2225 /// Return true if this shuffle mask chooses all elements with the same value
2226 /// as the first element of exactly one source vector.
2227 /// Example: <4,undef,undef,4>
2228 /// This assumes that vector operands are the same length as the mask.
2229 static bool isZeroEltSplatMask(ArrayRef<int> Mask);
2230 static bool isZeroEltSplatMask(const Constant *Mask) {
2231 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy(
) && "Shuffle needs vector constant.") ? void (0) : __assert_fail
("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\""
, "llvm/include/llvm/IR/Instructions.h", 2231, __extension__ __PRETTY_FUNCTION__
))
;
2232 SmallVector<int, 16> MaskAsInts;
2233 getShuffleMask(Mask, MaskAsInts);
2234 return isZeroEltSplatMask(MaskAsInts);
2235 }
2236
2237 /// Return true if all elements of this shuffle are the same value as the
2238 /// first element of exactly one source vector without changing the length
2239 /// of that vector.
2240 /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,0,undef,0>
2241 /// TODO: Optionally allow length-changing shuffles.
2242 /// TODO: Optionally allow splats from other elements.
2243 bool isZeroEltSplat() const {
2244 return !changesLength() && isZeroEltSplatMask(ShuffleMask);
2245 }
2246
2247 /// Return true if this shuffle mask is a transpose mask.
2248 /// Transpose vector masks transpose a 2xn matrix. They read corresponding
2249 /// even- or odd-numbered vector elements from two n-dimensional source
2250 /// vectors and write each result into consecutive elements of an
2251 /// n-dimensional destination vector. Two shuffles are necessary to complete
2252 /// the transpose, one for the even elements and another for the odd elements.
2253 /// This description closely follows how the TRN1 and TRN2 AArch64
2254 /// instructions operate.
2255 ///
2256 /// For example, a simple 2x2 matrix can be transposed with:
2257 ///
2258 /// ; Original matrix
2259 /// m0 = < a, b >
2260 /// m1 = < c, d >
2261 ///
2262 /// ; Transposed matrix
2263 /// t0 = < a, c > = shufflevector m0, m1, < 0, 2 >
2264 /// t1 = < b, d > = shufflevector m0, m1, < 1, 3 >
2265 ///
2266 /// For matrices having greater than n columns, the resulting nx2 transposed
2267 /// matrix is stored in two result vectors such that one vector contains
2268 /// interleaved elements from all the even-numbered rows and the other vector
2269 /// contains interleaved elements from all the odd-numbered rows. For example,
2270 /// a 2x4 matrix can be transposed with:
2271 ///
2272 /// ; Original matrix
2273 /// m0 = < a, b, c, d >
2274 /// m1 = < e, f, g, h >
2275 ///
2276 /// ; Transposed matrix
2277 /// t0 = < a, e, c, g > = shufflevector m0, m1 < 0, 4, 2, 6 >
2278 /// t1 = < b, f, d, h > = shufflevector m0, m1 < 1, 5, 3, 7 >
2279 static bool isTransposeMask(ArrayRef<int> Mask);
2280 static bool isTransposeMask(const Constant *Mask) {
2281 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy(
) && "Shuffle needs vector constant.") ? void (0) : __assert_fail
("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\""
, "llvm/include/llvm/IR/Instructions.h", 2281, __extension__ __PRETTY_FUNCTION__
))
;
2282 SmallVector<int, 16> MaskAsInts;
2283 getShuffleMask(Mask, MaskAsInts);
2284 return isTransposeMask(MaskAsInts);
2285 }
2286
2287 /// Return true if this shuffle transposes the elements of its inputs without
2288 /// changing the length of the vectors. This operation may also be known as a
2289 /// merge or interleave. See the description for isTransposeMask() for the
2290 /// exact specification.
2291 /// Example: shufflevector <4 x n> A, <4 x n> B, <0,4,2,6>
2292 bool isTranspose() const {
2293 return !changesLength() && isTransposeMask(ShuffleMask);
2294 }
2295
2296 /// Return true if this shuffle mask is a splice mask, concatenating the two
2297 /// inputs together and then extracts an original width vector starting from
2298 /// the splice index.
2299 /// Example: shufflevector <4 x n> A, <4 x n> B, <1,2,3,4>
2300 static bool isSpliceMask(ArrayRef<int> Mask, int &Index);
2301 static bool isSpliceMask(const Constant *Mask, int &Index) {
2302 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy(
) && "Shuffle needs vector constant.") ? void (0) : __assert_fail
("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\""
, "llvm/include/llvm/IR/Instructions.h", 2302, __extension__ __PRETTY_FUNCTION__
))
;
2303 SmallVector<int, 16> MaskAsInts;
2304 getShuffleMask(Mask, MaskAsInts);
2305 return isSpliceMask(MaskAsInts, Index);
2306 }
2307
2308 /// Return true if this shuffle splices two inputs without changing the length
2309 /// of the vectors. This operation concatenates the two inputs together and
2310 /// then extracts an original width vector starting from the splice index.
2311 /// Example: shufflevector <4 x n> A, <4 x n> B, <1,2,3,4>
2312 bool isSplice(int &Index) const {
2313 return !changesLength() && isSpliceMask(ShuffleMask, Index);
2314 }
2315
2316 /// Return true if this shuffle mask is an extract subvector mask.
2317 /// A valid extract subvector mask returns a smaller vector from a single
2318 /// source operand. The base extraction index is returned as well.
2319 static bool isExtractSubvectorMask(ArrayRef<int> Mask, int NumSrcElts,
2320 int &Index);
2321 static bool isExtractSubvectorMask(const Constant *Mask, int NumSrcElts,
2322 int &Index) {
2323 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy(
) && "Shuffle needs vector constant.") ? void (0) : __assert_fail
("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\""
, "llvm/include/llvm/IR/Instructions.h", 2323, __extension__ __PRETTY_FUNCTION__
))
;
2324 // Not possible to express a shuffle mask for a scalable vector for this
2325 // case.
2326 if (isa<ScalableVectorType>(Mask->getType()))
2327 return false;
2328 SmallVector<int, 16> MaskAsInts;
2329 getShuffleMask(Mask, MaskAsInts);
2330 return isExtractSubvectorMask(MaskAsInts, NumSrcElts, Index);
2331 }
2332
2333 /// Return true if this shuffle mask is an extract subvector mask.
2334 bool isExtractSubvectorMask(int &Index) const {
2335 // Not possible to express a shuffle mask for a scalable vector for this
2336 // case.
2337 if (isa<ScalableVectorType>(getType()))
2338 return false;
2339
2340 int NumSrcElts =
2341 cast<FixedVectorType>(Op<0>()->getType())->getNumElements();
2342 return isExtractSubvectorMask(ShuffleMask, NumSrcElts, Index);
2343 }
2344
2345 /// Return true if this shuffle mask is an insert subvector mask.
2346 /// A valid insert subvector mask inserts the lowest elements of a second
2347 /// source operand into an in-place first source operand operand.
2348 /// Both the sub vector width and the insertion index is returned.
2349 static bool isInsertSubvectorMask(ArrayRef<int> Mask, int NumSrcElts,
2350 int &NumSubElts, int &Index);
2351 static bool isInsertSubvectorMask(const Constant *Mask, int NumSrcElts,
2352 int &NumSubElts, int &Index) {
2353 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy(
) && "Shuffle needs vector constant.") ? void (0) : __assert_fail
("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\""
, "llvm/include/llvm/IR/Instructions.h", 2353, __extension__ __PRETTY_FUNCTION__
))
;
2354 // Not possible to express a shuffle mask for a scalable vector for this
2355 // case.
2356 if (isa<ScalableVectorType>(Mask->getType()))
2357 return false;
2358 SmallVector<int, 16> MaskAsInts;
2359 getShuffleMask(Mask, MaskAsInts);
2360 return isInsertSubvectorMask(MaskAsInts, NumSrcElts, NumSubElts, Index);
2361 }
2362
2363 /// Return true if this shuffle mask is an insert subvector mask.
2364 bool isInsertSubvectorMask(int &NumSubElts, int &Index) const {
2365 // Not possible to express a shuffle mask for a scalable vector for this
2366 // case.
2367 if (isa<ScalableVectorType>(getType()))
2368 return false;
2369
2370 int NumSrcElts =
2371 cast<FixedVectorType>(Op<0>()->getType())->getNumElements();
2372 return isInsertSubvectorMask(ShuffleMask, NumSrcElts, NumSubElts, Index);
2373 }
2374
2375 /// Return true if this shuffle mask replicates each of the \p VF elements
2376 /// in a vector \p ReplicationFactor times.
2377 /// For example, the mask for \p ReplicationFactor=3 and \p VF=4 is:
2378 /// <0,0,0,1,1,1,2,2,2,3,3,3>
2379 static bool isReplicationMask(ArrayRef<int> Mask, int &ReplicationFactor,
2380 int &VF);
2381 static bool isReplicationMask(const Constant *Mask, int &ReplicationFactor,
2382 int &VF) {
2383 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy(
) && "Shuffle needs vector constant.") ? void (0) : __assert_fail
("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\""
, "llvm/include/llvm/IR/Instructions.h", 2383, __extension__ __PRETTY_FUNCTION__
))
;
2384 // Not possible to express a shuffle mask for a scalable vector for this
2385 // case.
2386 if (isa<ScalableVectorType>(Mask->getType()))
2387 return false;
2388 SmallVector<int, 16> MaskAsInts;
2389 getShuffleMask(Mask, MaskAsInts);
2390 return isReplicationMask(MaskAsInts, ReplicationFactor, VF);
2391 }
2392
2393 /// Return true if this shuffle mask is a replication mask.
2394 bool isReplicationMask(int &ReplicationFactor, int &VF) const;
2395
2396 /// Change values in a shuffle permute mask assuming the two vector operands
2397 /// of length InVecNumElts have swapped position.
2398 static void commuteShuffleMask(MutableArrayRef<int> Mask,
2399 unsigned InVecNumElts) {
2400 for (int &Idx : Mask) {
2401 if (Idx == -1)
2402 continue;
2403 Idx = Idx < (int)InVecNumElts ? Idx + InVecNumElts : Idx - InVecNumElts;
2404 assert(Idx >= 0 && Idx < (int)InVecNumElts * 2 &&(static_cast <bool> (Idx >= 0 && Idx < (int
)InVecNumElts * 2 && "shufflevector mask index out of range"
) ? void (0) : __assert_fail ("Idx >= 0 && Idx < (int)InVecNumElts * 2 && \"shufflevector mask index out of range\""
, "llvm/include/llvm/IR/Instructions.h", 2405, __extension__ __PRETTY_FUNCTION__
))
2405 "shufflevector mask index out of range")(static_cast <bool> (Idx >= 0 && Idx < (int
)InVecNumElts * 2 && "shufflevector mask index out of range"
) ? void (0) : __assert_fail ("Idx >= 0 && Idx < (int)InVecNumElts * 2 && \"shufflevector mask index out of range\""
, "llvm/include/llvm/IR/Instructions.h", 2405, __extension__ __PRETTY_FUNCTION__
))
;
2406 }
2407 }
2408
2409 // Methods for support type inquiry through isa, cast, and dyn_cast:
2410 static bool classof(const Instruction *I) {
2411 return I->getOpcode() == Instruction::ShuffleVector;
2412 }
2413 static bool classof(const Value *V) {
2414 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2415 }
2416};
2417
2418template <>
2419struct OperandTraits<ShuffleVectorInst>
2420 : public FixedNumOperandTraits<ShuffleVectorInst, 2> {};
2421
2422DEFINE_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 { (static_cast <bool> (i_nocapture <
OperandTraits<ShuffleVectorInst>::operands(this) &&
"getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<ShuffleVectorInst>::operands(this) && \"getOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 2422, __extension__ __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) { (static_cast <bool> (i_nocapture
< OperandTraits<ShuffleVectorInst>::operands(this) &&
"setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<ShuffleVectorInst>::operands(this) && \"setOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 2422, __extension__ __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
); }
2423
2424//===----------------------------------------------------------------------===//
2425// ExtractValueInst Class
2426//===----------------------------------------------------------------------===//
2427
2428/// This instruction extracts a struct member or array
2429/// element value from an aggregate value.
2430///
2431class ExtractValueInst : public UnaryInstruction {
2432 SmallVector<unsigned, 4> Indices;
2433
2434 ExtractValueInst(const ExtractValueInst &EVI);
2435
2436 /// Constructors - Create a extractvalue instruction with a base aggregate
2437 /// value and a list of indices. The first ctor can optionally insert before
2438 /// an existing instruction, the second appends the new instruction to the
2439 /// specified BasicBlock.
2440 inline ExtractValueInst(Value *Agg,
2441 ArrayRef<unsigned> Idxs,
2442 const Twine &NameStr,
2443 Instruction *InsertBefore);
2444 inline ExtractValueInst(Value *Agg,
2445 ArrayRef<unsigned> Idxs,
2446 const Twine &NameStr, BasicBlock *InsertAtEnd);
2447
2448 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
2449
2450protected:
2451 // Note: Instruction needs to be a friend here to call cloneImpl.
2452 friend class Instruction;
2453
2454 ExtractValueInst *cloneImpl() const;
2455
2456public:
2457 static ExtractValueInst *Create(Value *Agg,
2458 ArrayRef<unsigned> Idxs,
2459 const Twine &NameStr = "",
2460 Instruction *InsertBefore = nullptr) {
2461 return new
2462 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
2463 }
2464
2465 static ExtractValueInst *Create(Value *Agg,
2466 ArrayRef<unsigned> Idxs,
2467 const Twine &NameStr,
2468 BasicBlock *InsertAtEnd) {
2469 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
2470 }
2471
2472 /// Returns the type of the element that would be extracted
2473 /// with an extractvalue instruction with the specified parameters.
2474 ///
2475 /// Null is returned if the indices are invalid for the specified type.
2476 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
2477
2478 using idx_iterator = const unsigned*;
2479
2480 inline idx_iterator idx_begin() const { return Indices.begin(); }
2481 inline idx_iterator idx_end() const { return Indices.end(); }
2482 inline iterator_range<idx_iterator> indices() const {
2483 return make_range(idx_begin(), idx_end());
2484 }
2485
2486 Value *getAggregateOperand() {
2487 return getOperand(0);
2488 }
2489 const Value *getAggregateOperand() const {
2490 return getOperand(0);
2491 }
2492 static unsigned getAggregateOperandIndex() {
2493 return 0U; // get index for modifying correct operand
2494 }
2495
2496 ArrayRef<unsigned> getIndices() const {
2497 return Indices;
2498 }
2499
2500 unsigned getNumIndices() const {
2501 return (unsigned)Indices.size();
2502 }
2503
2504 bool hasIndices() const {
2505 return true;
2506 }
2507
2508 // Methods for support type inquiry through isa, cast, and dyn_cast:
2509 static bool classof(const Instruction *I) {
2510 return I->getOpcode() == Instruction::ExtractValue;
2511 }
2512 static bool classof(const Value *V) {
2513 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2514 }
2515};
2516
2517ExtractValueInst::ExtractValueInst(Value *Agg,
2518 ArrayRef<unsigned> Idxs,
2519 const Twine &NameStr,
2520 Instruction *InsertBefore)
2521 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2522 ExtractValue, Agg, InsertBefore) {
2523 init(Idxs, NameStr);
2524}
2525
2526ExtractValueInst::ExtractValueInst(Value *Agg,
2527 ArrayRef<unsigned> Idxs,
2528 const Twine &NameStr,
2529 BasicBlock *InsertAtEnd)
2530 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2531 ExtractValue, Agg, InsertAtEnd) {
2532 init(Idxs, NameStr);
2533}
2534
2535//===----------------------------------------------------------------------===//
2536// InsertValueInst Class
2537//===----------------------------------------------------------------------===//
2538
2539/// This instruction inserts a struct field of array element
2540/// value into an aggregate value.
2541///
2542class InsertValueInst : public Instruction {
2543 SmallVector<unsigned, 4> Indices;
2544
2545 InsertValueInst(const InsertValueInst &IVI);
2546
2547 /// Constructors - Create a insertvalue instruction with a base aggregate
2548 /// value, a value to insert, and a list of indices. The first ctor can
2549 /// optionally insert before an existing instruction, the second appends
2550 /// the new instruction to the specified BasicBlock.
2551 inline InsertValueInst(Value *Agg, Value *Val,
2552 ArrayRef<unsigned> Idxs,
2553 const Twine &NameStr,
2554 Instruction *InsertBefore);
2555 inline InsertValueInst(Value *Agg, Value *Val,
2556 ArrayRef<unsigned> Idxs,
2557 const Twine &NameStr, BasicBlock *InsertAtEnd);
2558
2559 /// Constructors - These two constructors are convenience methods because one
2560 /// and two index insertvalue instructions are so common.
2561 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
2562 const Twine &NameStr = "",
2563 Instruction *InsertBefore = nullptr);
2564 InsertValueInst(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr,
2565 BasicBlock *InsertAtEnd);
2566
2567 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2568 const Twine &NameStr);
2569
2570protected:
2571 // Note: Instruction needs to be a friend here to call cloneImpl.
2572 friend class Instruction;
2573
2574 InsertValueInst *cloneImpl() const;
2575
2576public:
2577 // allocate space for exactly two operands
2578 void *operator new(size_t S) { return User::operator new(S, 2); }
2579 void operator delete(void *Ptr) { User::operator delete(Ptr); }
2580
2581 static InsertValueInst *Create(Value *Agg, Value *Val,
2582 ArrayRef<unsigned> Idxs,
2583 const Twine &NameStr = "",
2584 Instruction *InsertBefore = nullptr) {
2585 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
2586 }
2587
2588 static InsertValueInst *Create(Value *Agg, Value *Val,
2589 ArrayRef<unsigned> Idxs,
2590 const Twine &NameStr,
2591 BasicBlock *InsertAtEnd) {
2592 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
2593 }
2594
2595 /// Transparently provide more efficient getOperand methods.
2596 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
;
2597
2598 using idx_iterator = const unsigned*;
2599
2600 inline idx_iterator idx_begin() const { return Indices.begin(); }
2601 inline idx_iterator idx_end() const { return Indices.end(); }
2602 inline iterator_range<idx_iterator> indices() const {
2603 return make_range(idx_begin(), idx_end());
2604 }
2605
2606 Value *getAggregateOperand() {
2607 return getOperand(0);
2608 }
2609 const Value *getAggregateOperand() const {
2610 return getOperand(0);
2611 }
2612 static unsigned getAggregateOperandIndex() {
2613 return 0U; // get index for modifying correct operand
2614 }
2615
2616 Value *getInsertedValueOperand() {
2617 return getOperand(1);
2618 }
2619 const Value *getInsertedValueOperand() const {
2620 return getOperand(1);
2621 }
2622 static unsigned getInsertedValueOperandIndex() {
2623 return 1U; // get index for modifying correct operand
2624 }
2625
2626 ArrayRef<unsigned> getIndices() const {
2627 return Indices;
2628 }
2629
2630 unsigned getNumIndices() const {
2631 return (unsigned)Indices.size();
2632 }
2633
2634 bool hasIndices() const {
2635 return true;
2636 }
2637
2638 // Methods for support type inquiry through isa, cast, and dyn_cast:
2639 static bool classof(const Instruction *I) {
2640 return I->getOpcode() == Instruction::InsertValue;
2641 }
2642 static bool classof(const Value *V) {
2643 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2644 }
2645};
2646
2647template <>
2648struct OperandTraits<InsertValueInst> :
2649 public FixedNumOperandTraits<InsertValueInst, 2> {
2650};
2651
2652InsertValueInst::InsertValueInst(Value *Agg,
2653 Value *Val,
2654 ArrayRef<unsigned> Idxs,
2655 const Twine &NameStr,
2656 Instruction *InsertBefore)
2657 : Instruction(Agg->getType(), InsertValue,
2658 OperandTraits<InsertValueInst>::op_begin(this),
2659 2, InsertBefore) {
2660 init(Agg, Val, Idxs, NameStr);
2661}
2662
2663InsertValueInst::InsertValueInst(Value *Agg,
2664 Value *Val,
2665 ArrayRef<unsigned> Idxs,
2666 const Twine &NameStr,
2667 BasicBlock *InsertAtEnd)
2668 : Instruction(Agg->getType(), InsertValue,
2669 OperandTraits<InsertValueInst>::op_begin(this),
2670 2, InsertAtEnd) {
2671 init(Agg, Val, Idxs, NameStr);
2672}
2673
2674DEFINE_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 { (static_cast <bool
> (i_nocapture < OperandTraits<InsertValueInst>::
operands(this) && "getOperand() out of range!") ? void
(0) : __assert_fail ("i_nocapture < OperandTraits<InsertValueInst>::operands(this) && \"getOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 2674, __extension__ __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) { (static_cast <bool> (i_nocapture
< OperandTraits<InsertValueInst>::operands(this) &&
"setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<InsertValueInst>::operands(this) && \"setOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 2674, __extension__ __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
); }
2675
2676//===----------------------------------------------------------------------===//
2677// PHINode Class
2678//===----------------------------------------------------------------------===//
2679
2680// PHINode - The PHINode class is used to represent the magical mystical PHI
2681// node, that can not exist in nature, but can be synthesized in a computer
2682// scientist's overactive imagination.
2683//
2684class PHINode : public Instruction {
2685 /// The number of operands actually allocated. NumOperands is
2686 /// the number actually in use.
2687 unsigned ReservedSpace;
2688
2689 PHINode(const PHINode &PN);
2690
2691 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2692 const Twine &NameStr = "",
2693 Instruction *InsertBefore = nullptr)
2694 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2695 ReservedSpace(NumReservedValues) {
2696 assert(!Ty->isTokenTy() && "PHI nodes cannot have token type!")(static_cast <bool> (!Ty->isTokenTy() && "PHI nodes cannot have token type!"
) ? void (0) : __assert_fail ("!Ty->isTokenTy() && \"PHI nodes cannot have token type!\""
, "llvm/include/llvm/IR/Instructions.h", 2696, __extension__ __PRETTY_FUNCTION__
))
;
2697 setName(NameStr);
2698 allocHungoffUses(ReservedSpace);
2699 }
2700
2701 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2702 BasicBlock *InsertAtEnd)
2703 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2704 ReservedSpace(NumReservedValues) {
2705 assert(!Ty->isTokenTy() && "PHI nodes cannot have token type!")(static_cast <bool> (!Ty->isTokenTy() && "PHI nodes cannot have token type!"
) ? void (0) : __assert_fail ("!Ty->isTokenTy() && \"PHI nodes cannot have token type!\""
, "llvm/include/llvm/IR/Instructions.h", 2705, __extension__ __PRETTY_FUNCTION__
))
;
2706 setName(NameStr);
2707 allocHungoffUses(ReservedSpace);
2708 }
2709
2710protected:
2711 // Note: Instruction needs to be a friend here to call cloneImpl.
2712 friend class Instruction;
2713
2714 PHINode *cloneImpl() const;
2715
2716 // allocHungoffUses - this is more complicated than the generic
2717 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2718 // values and pointers to the incoming blocks, all in one allocation.
2719 void allocHungoffUses(unsigned N) {
2720 User::allocHungoffUses(N, /* IsPhi */ true);
2721 }
2722
2723public:
2724 /// Constructors - NumReservedValues is a hint for the number of incoming
2725 /// edges that this phi node will have (use 0 if you really have no idea).
2726 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2727 const Twine &NameStr = "",
2728 Instruction *InsertBefore = nullptr) {
2729 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2730 }
2731
2732 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2733 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2734 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2735 }
2736
2737 /// Provide fast operand accessors
2738 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
;
2739
2740 // Block iterator interface. This provides access to the list of incoming
2741 // basic blocks, which parallels the list of incoming values.
2742
2743 using block_iterator = BasicBlock **;
2744 using const_block_iterator = BasicBlock * const *;
2745
2746 block_iterator block_begin() {
2747 return reinterpret_cast<block_iterator>(op_begin() + ReservedSpace);
2748 }
2749
2750 const_block_iterator block_begin() const {
2751 return reinterpret_cast<const_block_iterator>(op_begin() + ReservedSpace);
2752 }
2753
2754 block_iterator block_end() {
2755 return block_begin() + getNumOperands();
2756 }
2757
2758 const_block_iterator block_end() const {
2759 return block_begin() + getNumOperands();
2760 }
2761
2762 iterator_range<block_iterator> blocks() {
2763 return make_range(block_begin(), block_end());
2764 }
2765
2766 iterator_range<const_block_iterator> blocks() const {
2767 return make_range(block_begin(), block_end());
2768 }
2769
2770 op_range incoming_values() { return operands(); }
2771
2772 const_op_range incoming_values() const { return operands(); }
2773
2774 /// Return the number of incoming edges
2775 ///
2776 unsigned getNumIncomingValues() const { return getNumOperands(); }
2777
2778 /// Return incoming value number x
2779 ///
2780 Value *getIncomingValue(unsigned i) const {
2781 return getOperand(i);
2782 }
2783 void setIncomingValue(unsigned i, Value *V) {
2784 assert(V && "PHI node got a null value!")(static_cast <bool> (V && "PHI node got a null value!"
) ? void (0) : __assert_fail ("V && \"PHI node got a null value!\""
, "llvm/include/llvm/IR/Instructions.h", 2784, __extension__ __PRETTY_FUNCTION__
))
;
2785 assert(getType() == V->getType() &&(static_cast <bool> (getType() == V->getType() &&
"All operands to PHI node must be the same type as the PHI node!"
) ? void (0) : __assert_fail ("getType() == V->getType() && \"All operands to PHI node must be the same type as the PHI node!\""
, "llvm/include/llvm/IR/Instructions.h", 2786, __extension__ __PRETTY_FUNCTION__
))
2786 "All operands to PHI node must be the same type as the PHI node!")(static_cast <bool> (getType() == V->getType() &&
"All operands to PHI node must be the same type as the PHI node!"
) ? void (0) : __assert_fail ("getType() == V->getType() && \"All operands to PHI node must be the same type as the PHI node!\""
, "llvm/include/llvm/IR/Instructions.h", 2786, __extension__ __PRETTY_FUNCTION__
))
;
2787 setOperand(i, V);
2788 }
2789
2790 static unsigned getOperandNumForIncomingValue(unsigned i) {
2791 return i;
2792 }
2793
2794 static unsigned getIncomingValueNumForOperand(unsigned i) {
2795 return i;
2796 }
2797
2798 /// Return incoming basic block number @p i.
2799 ///
2800 BasicBlock *getIncomingBlock(unsigned i) const {
2801 return block_begin()[i];
2802 }
2803
2804 /// Return incoming basic block corresponding
2805 /// to an operand of the PHI.
2806 ///
2807 BasicBlock *getIncomingBlock(const Use &U) const {
2808 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?")(static_cast <bool> (this == U.getUser() && "Iterator doesn't point to PHI's Uses?"
) ? void (0) : __assert_fail ("this == U.getUser() && \"Iterator doesn't point to PHI's Uses?\""
, "llvm/include/llvm/IR/Instructions.h", 2808, __extension__ __PRETTY_FUNCTION__
))
;
2809 return getIncomingBlock(unsigned(&U - op_begin()));
2810 }
2811
2812 /// Return incoming basic block corresponding
2813 /// to value use iterator.
2814 ///
2815 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2816 return getIncomingBlock(I.getUse());
2817 }
2818
2819 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2820 assert(BB && "PHI node got a null basic block!")(static_cast <bool> (BB && "PHI node got a null basic block!"
) ? void (0) : __assert_fail ("BB && \"PHI node got a null basic block!\""
, "llvm/include/llvm/IR/Instructions.h", 2820, __extension__ __PRETTY_FUNCTION__
))
;
2821 block_begin()[i] = BB;
2822 }
2823
2824 /// Replace every incoming basic block \p Old to basic block \p New.
2825 void replaceIncomingBlockWith(const BasicBlock *Old, BasicBlock *New) {
2826 assert(New && Old && "PHI node got a null basic block!")(static_cast <bool> (New && Old && "PHI node got a null basic block!"
) ? void (0) : __assert_fail ("New && Old && \"PHI node got a null basic block!\""
, "llvm/include/llvm/IR/Instructions.h", 2826, __extension__ __PRETTY_FUNCTION__
))
;
2827 for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op)
2828 if (getIncomingBlock(Op) == Old)
2829 setIncomingBlock(Op, New);
2830 }
2831
2832 /// Add an incoming value to the end of the PHI list
2833 ///
2834 void addIncoming(Value *V, BasicBlock *BB) {
2835 if (getNumOperands() == ReservedSpace)
2836 growOperands(); // Get more space!
2837 // Initialize some new operands.
2838 setNumHungOffUseOperands(getNumOperands() + 1);
2839 setIncomingValue(getNumOperands() - 1, V);
2840 setIncomingBlock(getNumOperands() - 1, BB);
2841 }
2842
2843 /// Remove an incoming value. This is useful if a
2844 /// predecessor basic block is deleted. The value removed is returned.
2845 ///
2846 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2847 /// is true), the PHI node is destroyed and any uses of it are replaced with
2848 /// dummy values. The only time there should be zero incoming values to a PHI
2849 /// node is when the block is dead, so this strategy is sound.
2850 ///
2851 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2852
2853 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2854 int Idx = getBasicBlockIndex(BB);
2855 assert(Idx >= 0 && "Invalid basic block argument to remove!")(static_cast <bool> (Idx >= 0 && "Invalid basic block argument to remove!"
) ? void (0) : __assert_fail ("Idx >= 0 && \"Invalid basic block argument to remove!\""
, "llvm/include/llvm/IR/Instructions.h", 2855, __extension__ __PRETTY_FUNCTION__
))
;
2856 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2857 }
2858
2859 /// Return the first index of the specified basic
2860 /// block in the value list for this PHI. Returns -1 if no instance.
2861 ///
2862 int getBasicBlockIndex(const BasicBlock *BB) const {
2863 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2864 if (block_begin()[i] == BB)
2865 return i;
2866 return -1;
2867 }
2868
2869 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2870 int Idx = getBasicBlockIndex(BB);
2871 assert(Idx >= 0 && "Invalid basic block argument!")(static_cast <bool> (Idx >= 0 && "Invalid basic block argument!"
) ? void (0) : __assert_fail ("Idx >= 0 && \"Invalid basic block argument!\""
, "llvm/include/llvm/IR/Instructions.h", 2871, __extension__ __PRETTY_FUNCTION__
))
;
2872 return getIncomingValue(Idx);
2873 }
2874
2875 /// Set every incoming value(s) for block \p BB to \p V.
2876 void setIncomingValueForBlock(const BasicBlock *BB, Value *V) {
2877 assert(BB && "PHI node got a null basic block!")(static_cast <bool> (BB && "PHI node got a null basic block!"
) ? void (0) : __assert_fail ("BB && \"PHI node got a null basic block!\""
, "llvm/include/llvm/IR/Instructions.h", 2877, __extension__ __PRETTY_FUNCTION__
))
;
2878 bool Found = false;
2879 for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op)
2880 if (getIncomingBlock(Op) == BB) {
2881 Found = true;
2882 setIncomingValue(Op, V);
2883 }
2884 (void)Found;
2885 assert(Found && "Invalid basic block argument to set!")(static_cast <bool> (Found && "Invalid basic block argument to set!"
) ? void (0) : __assert_fail ("Found && \"Invalid basic block argument to set!\""
, "llvm/include/llvm/IR/Instructions.h", 2885, __extension__ __PRETTY_FUNCTION__
))
;
2886 }
2887
2888 /// If the specified PHI node always merges together the
2889 /// same value, return the value, otherwise return null.
2890 Value *hasConstantValue() const;
2891
2892 /// Whether the specified PHI node always merges
2893 /// together the same value, assuming undefs are equal to a unique
2894 /// non-undef value.
2895 bool hasConstantOrUndefValue() const;
2896
2897 /// If the PHI node is complete which means all of its parent's predecessors
2898 /// have incoming value in this PHI, return true, otherwise return false.
2899 bool isComplete() const {
2900 return llvm::all_of(predecessors(getParent()),
2901 [this](const BasicBlock *Pred) {
2902 return getBasicBlockIndex(Pred) >= 0;
2903 });
2904 }
2905
2906 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2907 static bool classof(const Instruction *I) {
2908 return I->getOpcode() == Instruction::PHI;
2909 }
2910 static bool classof(const Value *V) {
2911 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2912 }
2913
2914private:
2915 void growOperands();
2916};
2917
2918template <>
2919struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2920};
2921
2922DEFINE_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 { (static_cast <bool> (i_nocapture < OperandTraits
<PHINode>::operands(this) && "getOperand() out of range!"
) ? void (0) : __assert_fail ("i_nocapture < OperandTraits<PHINode>::operands(this) && \"getOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 2922, __extension__ __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) { (static_cast <bool> (i_nocapture <
OperandTraits<PHINode>::operands(this) && "setOperand() out of range!"
) ? void (0) : __assert_fail ("i_nocapture < OperandTraits<PHINode>::operands(this) && \"setOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 2922, __extension__ __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); }
2923
2924//===----------------------------------------------------------------------===//
2925// LandingPadInst Class
2926//===----------------------------------------------------------------------===//
2927
2928//===---------------------------------------------------------------------------
2929/// The landingpad instruction holds all of the information
2930/// necessary to generate correct exception handling. The landingpad instruction
2931/// cannot be moved from the top of a landing pad block, which itself is
2932/// accessible only from the 'unwind' edge of an invoke. This uses the
2933/// SubclassData field in Value to store whether or not the landingpad is a
2934/// cleanup.
2935///
2936class LandingPadInst : public Instruction {
2937 using CleanupField = BoolBitfieldElementT<0>;
2938
2939 /// The number of operands actually allocated. NumOperands is
2940 /// the number actually in use.
2941 unsigned ReservedSpace;
2942
2943 LandingPadInst(const LandingPadInst &LP);
2944
2945public:
2946 enum ClauseType { Catch, Filter };
2947
2948private:
2949 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
2950 const Twine &NameStr, Instruction *InsertBefore);
2951 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
2952 const Twine &NameStr, BasicBlock *InsertAtEnd);
2953
2954 // Allocate space for exactly zero operands.
2955 void *operator new(size_t S) { return User::operator new(S); }
2956
2957 void growOperands(unsigned Size);
2958 void init(unsigned NumReservedValues, const Twine &NameStr);
2959
2960protected:
2961 // Note: Instruction needs to be a friend here to call cloneImpl.
2962 friend class Instruction;
2963
2964 LandingPadInst *cloneImpl() const;
2965
2966public:
2967 void operator delete(void *Ptr) { User::operator delete(Ptr); }
2968
2969 /// Constructors - NumReservedClauses is a hint for the number of incoming
2970 /// clauses that this landingpad will have (use 0 if you really have no idea).
2971 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
2972 const Twine &NameStr = "",
2973 Instruction *InsertBefore = nullptr);
2974 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
2975 const Twine &NameStr, BasicBlock *InsertAtEnd);
2976
2977 /// Provide fast operand accessors
2978 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
;
2979
2980 /// Return 'true' if this landingpad instruction is a
2981 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2982 /// doesn't catch the exception.
2983 bool isCleanup() const { return getSubclassData<CleanupField>(); }
2984
2985 /// Indicate that this landingpad instruction is a cleanup.
2986 void setCleanup(bool V) { setSubclassData<CleanupField>(V); }
2987
2988 /// Add a catch or filter clause to the landing pad.
2989 void addClause(Constant *ClauseVal);
2990
2991 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
2992 /// determine what type of clause this is.
2993 Constant *getClause(unsigned Idx) const {
2994 return cast<Constant>(getOperandList()[Idx]);
2995 }
2996
2997 /// Return 'true' if the clause and index Idx is a catch clause.
2998 bool isCatch(unsigned Idx) const {
2999 return !isa<ArrayType>(getOperandList()[Idx]->getType());
3000 }
3001
3002 /// Return 'true' if the clause and index Idx is a filter clause.
3003 bool isFilter(unsigned Idx) const {
3004 return isa<ArrayType>(getOperandList()[Idx]->getType());
3005 }
3006
3007 /// Get the number of clauses for this landing pad.
3008 unsigned getNumClauses() const { return getNumOperands(); }
3009
3010 /// Grow the size of the operand list to accommodate the new
3011 /// number of clauses.
3012 void reserveClauses(unsigned Size) { growOperands(Size); }
3013
3014 // Methods for support type inquiry through isa, cast, and dyn_cast:
3015 static bool classof(const Instruction *I) {
3016 return I->getOpcode() == Instruction::LandingPad;
3017 }
3018 static bool classof(const Value *V) {
3019 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3020 }
3021};
3022
3023template <>
3024struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<1> {
3025};
3026
3027DEFINE_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 { (static_cast <bool
> (i_nocapture < OperandTraits<LandingPadInst>::operands
(this) && "getOperand() out of range!") ? void (0) : __assert_fail
("i_nocapture < OperandTraits<LandingPadInst>::operands(this) && \"getOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 3027, __extension__ __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) { (static_cast <bool> (i_nocapture
< OperandTraits<LandingPadInst>::operands(this) &&
"setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<LandingPadInst>::operands(this) && \"setOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 3027, __extension__ __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
); }
3028
3029//===----------------------------------------------------------------------===//
3030// ReturnInst Class
3031//===----------------------------------------------------------------------===//
3032
3033//===---------------------------------------------------------------------------
3034/// Return a value (possibly void), from a function. Execution
3035/// does not continue in this function any longer.
3036///
3037class ReturnInst : public Instruction {
3038 ReturnInst(const ReturnInst &RI);
3039
3040private:
3041 // ReturnInst constructors:
3042 // ReturnInst() - 'ret void' instruction
3043 // ReturnInst( null) - 'ret void' instruction
3044 // ReturnInst(Value* X) - 'ret X' instruction
3045 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
3046 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
3047 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
3048 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
3049 //
3050 // NOTE: If the Value* passed is of type void then the constructor behaves as
3051 // if it was passed NULL.
3052 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
3053 Instruction *InsertBefore = nullptr);
3054 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
3055 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3056
3057protected:
3058 // Note: Instruction needs to be a friend here to call cloneImpl.
3059 friend class Instruction;
3060
3061 ReturnInst *cloneImpl() const;
3062
3063public:
3064 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
3065 Instruction *InsertBefore = nullptr) {
3066 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
3067 }
3068
3069 static ReturnInst* Create(LLVMContext &C, Value *retVal,
3070 BasicBlock *InsertAtEnd) {
3071 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
3072 }
3073
3074 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
3075 return new(0) ReturnInst(C, InsertAtEnd);
3076 }
3077
3078 /// Provide fast operand accessors
3079 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
;
3080
3081 /// Convenience accessor. Returns null if there is no return value.
3082 Value *getReturnValue() const {
3083 return getNumOperands() != 0 ? getOperand(0) : nullptr;
3084 }
3085
3086 unsigned getNumSuccessors() const { return 0; }
3087
3088 // Methods for support type inquiry through isa, cast, and dyn_cast:
3089 static bool classof(const Instruction *I) {
3090 return (I->getOpcode() == Instruction::Ret);
3091 }
3092 static bool classof(const Value *V) {
3093 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3094 }
3095
3096private:
3097 BasicBlock *getSuccessor(unsigned idx) const {
3098 llvm_unreachable("ReturnInst has no successors!")::llvm::llvm_unreachable_internal("ReturnInst has no successors!"
, "llvm/include/llvm/IR/Instructions.h", 3098)
;
3099 }
3100
3101 void setSuccessor(unsigned idx, BasicBlock *B) {
3102 llvm_unreachable("ReturnInst has no successors!")::llvm::llvm_unreachable_internal("ReturnInst has no successors!"
, "llvm/include/llvm/IR/Instructions.h", 3102)
;
3103 }
3104};
3105
3106template <>
3107struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
3108};
3109
3110DEFINE_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 { (static_cast <bool
> (i_nocapture < OperandTraits<ReturnInst>::operands
(this) && "getOperand() out of range!") ? void (0) : __assert_fail
("i_nocapture < OperandTraits<ReturnInst>::operands(this) && \"getOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 3110, __extension__ __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) { (static_cast <bool> (i_nocapture
< OperandTraits<ReturnInst>::operands(this) &&
"setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<ReturnInst>::operands(this) && \"setOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 3110, __extension__ __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); }
3111
3112//===----------------------------------------------------------------------===//
3113// BranchInst Class
3114//===----------------------------------------------------------------------===//
3115
3116//===---------------------------------------------------------------------------
3117/// Conditional or Unconditional Branch instruction.
3118///
3119class BranchInst : public Instruction {
3120 /// Ops list - Branches are strange. The operands are ordered:
3121 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
3122 /// they don't have to check for cond/uncond branchness. These are mostly
3123 /// accessed relative from op_end().
3124 BranchInst(const BranchInst &BI);
3125 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
3126 // BranchInst(BB *B) - 'br B'
3127 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
3128 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
3129 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
3130 // BranchInst(BB* B, BB *I) - 'br B' insert at end
3131 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
3132 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
3133 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
3134 Instruction *InsertBefore = nullptr);
3135 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
3136 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
3137 BasicBlock *InsertAtEnd);
3138
3139 void AssertOK();
3140
3141protected:
3142 // Note: Instruction needs to be a friend here to call cloneImpl.
3143 friend class Instruction;
3144
3145 BranchInst *cloneImpl() const;
3146
3147public:
3148 /// Iterator type that casts an operand to a basic block.
3149 ///
3150 /// This only makes sense because the successors are stored as adjacent
3151 /// operands for branch instructions.
3152 struct succ_op_iterator
3153 : iterator_adaptor_base<succ_op_iterator, value_op_iterator,
3154 std::random_access_iterator_tag, BasicBlock *,
3155 ptrdiff_t, BasicBlock *, BasicBlock *> {
3156 explicit succ_op_iterator(value_op_iterator I) : iterator_adaptor_base(I) {}
3157
3158 BasicBlock *operator*() const { return cast<BasicBlock>(*I); }
3159 BasicBlock *operator->() const { return operator*(); }
3160 };
3161
3162 /// The const version of `succ_op_iterator`.
3163 struct const_succ_op_iterator
3164 : iterator_adaptor_base<const_succ_op_iterator, const_value_op_iterator,
3165 std::random_access_iterator_tag,
3166 const BasicBlock *, ptrdiff_t, const BasicBlock *,
3167 const BasicBlock *> {
3168 explicit const_succ_op_iterator(const_value_op_iterator I)
3169 : iterator_adaptor_base(I) {}
3170
3171 const BasicBlock *operator*() const { return cast<BasicBlock>(*I); }
3172 const BasicBlock *operator->() const { return operator*(); }
3173 };
3174
3175 static BranchInst *Create(BasicBlock *IfTrue,
3176 Instruction *InsertBefore = nullptr) {
3177 return new(1) BranchInst(IfTrue, InsertBefore);
3178 }
3179
3180 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
3181 Value *Cond, Instruction *InsertBefore = nullptr) {
3182 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
3183 }
3184
3185 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
3186 return new(1) BranchInst(IfTrue, InsertAtEnd);
3187 }
3188
3189 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
3190 Value *Cond, BasicBlock *InsertAtEnd) {
3191 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
3192 }
3193
3194 /// Transparently provide more efficient getOperand methods.
3195 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
;
3196
3197 bool isUnconditional() const { return getNumOperands() == 1; }
3198 bool isConditional() const { return getNumOperands() == 3; }
3199
3200 Value *getCondition() const {
3201 assert(isConditional() && "Cannot get condition of an uncond branch!")(static_cast <bool> (isConditional() && "Cannot get condition of an uncond branch!"
) ? void (0) : __assert_fail ("isConditional() && \"Cannot get condition of an uncond branch!\""
, "llvm/include/llvm/IR/Instructions.h", 3201, __extension__ __PRETTY_FUNCTION__
))
;
3202 return Op<-3>();
3203 }
3204
3205 void setCondition(Value *V) {
3206 assert(isConditional() && "Cannot set condition of unconditional branch!")(static_cast <bool> (isConditional() && "Cannot set condition of unconditional branch!"
) ? void (0) : __assert_fail ("isConditional() && \"Cannot set condition of unconditional branch!\""
, "llvm/include/llvm/IR/Instructions.h", 3206, __extension__ __PRETTY_FUNCTION__
))
;
3207 Op<-3>() = V;
3208 }
3209
3210 unsigned getNumSuccessors() const { return 1+isConditional(); }
3211
3212 BasicBlock *getSuccessor(unsigned i) const {
3213 assert(i < getNumSuccessors() && "Successor # out of range for Branch!")(static_cast <bool> (i < getNumSuccessors() &&
"Successor # out of range for Branch!") ? void (0) : __assert_fail
("i < getNumSuccessors() && \"Successor # out of range for Branch!\""
, "llvm/include/llvm/IR/Instructions.h", 3213, __extension__ __PRETTY_FUNCTION__
))
;
3214 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
3215 }
3216
3217 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3218 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!")(static_cast <bool> (idx < getNumSuccessors() &&
"Successor # out of range for Branch!") ? void (0) : __assert_fail
("idx < getNumSuccessors() && \"Successor # out of range for Branch!\""
, "llvm/include/llvm/IR/Instructions.h", 3218, __extension__ __PRETTY_FUNCTION__
))
;
3219 *(&Op<-1>() - idx) = NewSucc;
3220 }
3221
3222 /// Swap the successors of this branch instruction.
3223 ///
3224 /// Swaps the successors of the branch instruction. This also swaps any
3225 /// branch weight metadata associated with the instruction so that it
3226 /// continues to map correctly to each operand.
3227 void swapSuccessors();
3228
3229 iterator_range<succ_op_iterator> successors() {
3230 return make_range(
3231 succ_op_iterator(std::next(value_op_begin(), isConditional() ? 1 : 0)),
3232 succ_op_iterator(value_op_end()));
3233 }
3234
3235 iterator_range<const_succ_op_iterator> successors() const {
3236 return make_range(const_succ_op_iterator(
3237 std::next(value_op_begin(), isConditional() ? 1 : 0)),
3238 const_succ_op_iterator(value_op_end()));
3239 }
3240
3241 // Methods for support type inquiry through isa, cast, and dyn_cast:
3242 static bool classof(const Instruction *I) {
3243 return (I->getOpcode() == Instruction::Br);
3244 }
3245 static bool classof(const Value *V) {
3246 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3247 }
3248};
3249
3250template <>
3251struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
3252};
3253
3254DEFINE_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 { (static_cast <bool
> (i_nocapture < OperandTraits<BranchInst>::operands
(this) && "getOperand() out of range!") ? void (0) : __assert_fail
("i_nocapture < OperandTraits<BranchInst>::operands(this) && \"getOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 3254, __extension__ __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) { (static_cast <bool> (i_nocapture
< OperandTraits<BranchInst>::operands(this) &&
"setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<BranchInst>::operands(this) && \"setOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 3254, __extension__ __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); }
3255
3256//===----------------------------------------------------------------------===//
3257// SwitchInst Class
3258//===----------------------------------------------------------------------===//
3259
3260//===---------------------------------------------------------------------------
3261/// Multiway switch
3262///
3263class SwitchInst : public Instruction {
3264 unsigned ReservedSpace;
3265
3266 // Operand[0] = Value to switch on
3267 // Operand[1] = Default basic block destination
3268 // Operand[2n ] = Value to match
3269 // Operand[2n+1] = BasicBlock to go to on match
3270 SwitchInst(const SwitchInst &SI);
3271
3272 /// Create a new switch instruction, specifying a value to switch on and a
3273 /// default destination. The number of additional cases can be specified here
3274 /// to make memory allocation more efficient. This constructor can also
3275 /// auto-insert before another instruction.
3276 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3277 Instruction *InsertBefore);
3278
3279 /// Create a new switch instruction, specifying a value to switch on and a
3280 /// default destination. The number of additional cases can be specified here
3281 /// to make memory allocation more efficient. This constructor also
3282 /// auto-inserts at the end of the specified BasicBlock.
3283 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3284 BasicBlock *InsertAtEnd);
3285
3286 // allocate space for exactly zero operands
3287 void *operator new(size_t S) { return User::operator new(S); }
3288
3289 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
3290 void growOperands();
3291
3292protected:
3293 // Note: Instruction needs to be a friend here to call cloneImpl.
3294 friend class Instruction;
3295
3296 SwitchInst *cloneImpl() const;
3297
3298public:
3299 void operator delete(void *Ptr) { User::operator delete(Ptr); }
3300
3301 // -2
3302 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
3303
3304 template <typename CaseHandleT> class CaseIteratorImpl;
3305
3306 /// A handle to a particular switch case. It exposes a convenient interface
3307 /// to both the case value and the successor block.
3308 ///
3309 /// We define this as a template and instantiate it to form both a const and
3310 /// non-const handle.
3311 template <typename SwitchInstT, typename ConstantIntT, typename BasicBlockT>
3312 class CaseHandleImpl {
3313 // Directly befriend both const and non-const iterators.
3314 friend class SwitchInst::CaseIteratorImpl<
3315 CaseHandleImpl<SwitchInstT, ConstantIntT, BasicBlockT>>;
3316
3317 protected:
3318 // Expose the switch type we're parameterized with to the iterator.
3319 using SwitchInstType = SwitchInstT;
3320
3321 SwitchInstT *SI;
3322 ptrdiff_t Index;
3323
3324 CaseHandleImpl() = default;
3325 CaseHandleImpl(SwitchInstT *SI, ptrdiff_t Index) : SI(SI), Index(Index) {}
3326
3327 public:
3328 /// Resolves case value for current case.
3329 ConstantIntT *getCaseValue() const {
3330 assert((unsigned)Index < SI->getNumCases() &&(static_cast <bool> ((unsigned)Index < SI->getNumCases
() && "Index out the number of cases.") ? void (0) : __assert_fail
("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\""
, "llvm/include/llvm/IR/Instructions.h", 3331, __extension__ __PRETTY_FUNCTION__
))
3331 "Index out the number of cases.")(static_cast <bool> ((unsigned)Index < SI->getNumCases
() && "Index out the number of cases.") ? void (0) : __assert_fail
("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\""
, "llvm/include/llvm/IR/Instructions.h", 3331, __extension__ __PRETTY_FUNCTION__
))
;
3332 return reinterpret_cast<ConstantIntT *>(SI->getOperand(2 + Index * 2));
3333 }
3334
3335 /// Resolves successor for current case.
3336 BasicBlockT *getCaseSuccessor() const {
3337 assert(((unsigned)Index < SI->getNumCases() ||(static_cast <bool> (((unsigned)Index < SI->getNumCases
() || (unsigned)Index == DefaultPseudoIndex) && "Index out the number of cases."
) ? void (0) : __assert_fail ("((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && \"Index out the number of cases.\""
, "llvm/include/llvm/IR/Instructions.h", 3339, __extension__ __PRETTY_FUNCTION__
))
3338 (unsigned)Index == DefaultPseudoIndex) &&(static_cast <bool> (((unsigned)Index < SI->getNumCases
() || (unsigned)Index == DefaultPseudoIndex) && "Index out the number of cases."
) ? void (0) : __assert_fail ("((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && \"Index out the number of cases.\""
, "llvm/include/llvm/IR/Instructions.h", 3339, __extension__ __PRETTY_FUNCTION__
))
3339 "Index out the number of cases.")(static_cast <bool> (((unsigned)Index < SI->getNumCases
() || (unsigned)Index == DefaultPseudoIndex) && "Index out the number of cases."
) ? void (0) : __assert_fail ("((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && \"Index out the number of cases.\""
, "llvm/include/llvm/IR/Instructions.h", 3339, __extension__ __PRETTY_FUNCTION__
))
;
3340 return SI->getSuccessor(getSuccessorIndex());
3341 }
3342
3343 /// Returns number of current case.
3344 unsigned getCaseIndex() const { return Index; }
3345
3346 /// Returns successor index for current case successor.
3347 unsigned getSuccessorIndex() const {
3348 assert(((unsigned)Index == DefaultPseudoIndex ||(static_cast <bool> (((unsigned)Index == DefaultPseudoIndex
|| (unsigned)Index < SI->getNumCases()) && "Index out the number of cases."
) ? void (0) : __assert_fail ("((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && \"Index out the number of cases.\""
, "llvm/include/llvm/IR/Instructions.h", 3350, __extension__ __PRETTY_FUNCTION__
))
3349 (unsigned)Index < SI->getNumCases()) &&(static_cast <bool> (((unsigned)Index == DefaultPseudoIndex
|| (unsigned)Index < SI->getNumCases()) && "Index out the number of cases."
) ? void (0) : __assert_fail ("((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && \"Index out the number of cases.\""
, "llvm/include/llvm/IR/Instructions.h", 3350, __extension__ __PRETTY_FUNCTION__
))
3350 "Index out the number of cases.")(static_cast <bool> (((unsigned)Index == DefaultPseudoIndex
|| (unsigned)Index < SI->getNumCases()) && "Index out the number of cases."
) ? void (0) : __assert_fail ("((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && \"Index out the number of cases.\""
, "llvm/include/llvm/IR/Instructions.h", 3350, __extension__ __PRETTY_FUNCTION__
))
;
3351 return (unsigned)Index != DefaultPseudoIndex ? Index + 1 : 0;
3352 }
3353
3354 bool operator==(const CaseHandleImpl &RHS) const {
3355 assert(SI == RHS.SI && "Incompatible operators.")(static_cast <bool> (SI == RHS.SI && "Incompatible operators."
) ? void (0) : __assert_fail ("SI == RHS.SI && \"Incompatible operators.\""
, "llvm/include/llvm/IR/Instructions.h", 3355, __extension__ __PRETTY_FUNCTION__
))
;
3356 return Index == RHS.Index;
3357 }
3358 };
3359
3360 using ConstCaseHandle =
3361 CaseHandleImpl<const SwitchInst, const ConstantInt, const BasicBlock>;
3362
3363 class CaseHandle
3364 : public CaseHandleImpl<SwitchInst, ConstantInt, BasicBlock> {
3365 friend class SwitchInst::CaseIteratorImpl<CaseHandle>;
3366
3367 public:
3368 CaseHandle(SwitchInst *SI, ptrdiff_t Index) : CaseHandleImpl(SI, Index) {}
3369
3370 /// Sets the new value for current case.
3371 void setValue(ConstantInt *V) const {
3372 assert((unsigned)Index < SI->getNumCases() &&(static_cast <bool> ((unsigned)Index < SI->getNumCases
() && "Index out the number of cases.") ? void (0) : __assert_fail
("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\""
, "llvm/include/llvm/IR/Instructions.h", 3373, __extension__ __PRETTY_FUNCTION__
))
3373 "Index out the number of cases.")(static_cast <bool> ((unsigned)Index < SI->getNumCases
() && "Index out the number of cases.") ? void (0) : __assert_fail
("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\""
, "llvm/include/llvm/IR/Instructions.h", 3373, __extension__ __PRETTY_FUNCTION__
))
;
3374 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
3375 }
3376
3377 /// Sets the new successor for current case.
3378 void setSuccessor(BasicBlock *S) const {
3379 SI->setSuccessor(getSuccessorIndex(), S);
3380 }
3381 };
3382
3383 template <typename CaseHandleT>
3384 class CaseIteratorImpl
3385 : public iterator_facade_base<CaseIteratorImpl<CaseHandleT>,
3386 std::random_access_iterator_tag,
3387 const CaseHandleT> {
3388 using SwitchInstT = typename CaseHandleT::SwitchInstType;
3389
3390 CaseHandleT Case;
3391
3392 public:
3393 /// Default constructed iterator is in an invalid state until assigned to
3394 /// a case for a particular switch.
3395 CaseIteratorImpl() = default;
3396
3397 /// Initializes case iterator for given SwitchInst and for given
3398 /// case number.
3399 CaseIteratorImpl(SwitchInstT *SI, unsigned CaseNum) : Case(SI, CaseNum) {}
3400
3401 /// Initializes case iterator for given SwitchInst and for given
3402 /// successor index.
3403 static CaseIteratorImpl fromSuccessorIndex(SwitchInstT *SI,
3404 unsigned SuccessorIndex) {
3405 assert(SuccessorIndex < SI->getNumSuccessors() &&(static_cast <bool> (SuccessorIndex < SI->getNumSuccessors
() && "Successor index # out of range!") ? void (0) :
__assert_fail ("SuccessorIndex < SI->getNumSuccessors() && \"Successor index # out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 3406, __extension__ __PRETTY_FUNCTION__
))
3406 "Successor index # out of range!")(static_cast <bool> (SuccessorIndex < SI->getNumSuccessors
() && "Successor index # out of range!") ? void (0) :
__assert_fail ("SuccessorIndex < SI->getNumSuccessors() && \"Successor index # out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 3406, __extension__ __PRETTY_FUNCTION__
))
;
3407 return SuccessorIndex != 0 ? CaseIteratorImpl(SI, SuccessorIndex - 1)
3408 : CaseIteratorImpl(SI, DefaultPseudoIndex);
3409 }
3410
3411 /// Support converting to the const variant. This will be a no-op for const
3412 /// variant.
3413 operator CaseIteratorImpl<ConstCaseHandle>() const {
3414 return CaseIteratorImpl<ConstCaseHandle>(Case.SI, Case.Index);
3415 }
3416
3417 CaseIteratorImpl &operator+=(ptrdiff_t N) {
3418 // Check index correctness after addition.
3419 // Note: Index == getNumCases() means end().
3420 assert(Case.Index + N >= 0 &&(static_cast <bool> (Case.Index + N >= 0 && (
unsigned)(Case.Index + N) <= Case.SI->getNumCases() &&
"Case.Index out the number of cases.") ? void (0) : __assert_fail
("Case.Index + N >= 0 && (unsigned)(Case.Index + N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\""
, "llvm/include/llvm/IR/Instructions.h", 3422, __extension__ __PRETTY_FUNCTION__
))
3421 (unsigned)(Case.Index + N) <= Case.SI->getNumCases() &&(static_cast <bool> (Case.Index + N >= 0 && (
unsigned)(Case.Index + N) <= Case.SI->getNumCases() &&
"Case.Index out the number of cases.") ? void (0) : __assert_fail
("Case.Index + N >= 0 && (unsigned)(Case.Index + N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\""
, "llvm/include/llvm/IR/Instructions.h", 3422, __extension__ __PRETTY_FUNCTION__
))
3422 "Case.Index out the number of cases.")(static_cast <bool> (Case.Index + N >= 0 && (
unsigned)(Case.Index + N) <= Case.SI->getNumCases() &&
"Case.Index out the number of cases.") ? void (0) : __assert_fail
("Case.Index + N >= 0 && (unsigned)(Case.Index + N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\""
, "llvm/include/llvm/IR/Instructions.h", 3422, __extension__ __PRETTY_FUNCTION__
))
;
3423 Case.Index += N;
3424 return *this;
3425 }
3426 CaseIteratorImpl &operator-=(ptrdiff_t N) {
3427 // Check index correctness after subtraction.
3428 // Note: Case.Index == getNumCases() means end().
3429 assert(Case.Index - N >= 0 &&(static_cast <bool> (Case.Index - N >= 0 && (
unsigned)(Case.Index - N) <= Case.SI->getNumCases() &&
"Case.Index out the number of cases.") ? void (0) : __assert_fail
("Case.Index - N >= 0 && (unsigned)(Case.Index - N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\""
, "llvm/include/llvm/IR/Instructions.h", 3431, __extension__ __PRETTY_FUNCTION__
))
3430 (unsigned)(Case.Index - N) <= Case.SI->getNumCases() &&(static_cast <bool> (Case.Index - N >= 0 && (
unsigned)(Case.Index - N) <= Case.SI->getNumCases() &&
"Case.Index out the number of cases.") ? void (0) : __assert_fail
("Case.Index - N >= 0 && (unsigned)(Case.Index - N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\""
, "llvm/include/llvm/IR/Instructions.h", 3431, __extension__ __PRETTY_FUNCTION__
))
3431 "Case.Index out the number of cases.")(static_cast <bool> (Case.Index - N >= 0 && (
unsigned)(Case.Index - N) <= Case.SI->getNumCases() &&
"Case.Index out the number of cases.") ? void (0) : __assert_fail
("Case.Index - N >= 0 && (unsigned)(Case.Index - N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\""
, "llvm/include/llvm/IR/Instructions.h", 3431, __extension__ __PRETTY_FUNCTION__
))
;
3432 Case.Index -= N;
3433 return *this;
3434 }
3435 ptrdiff_t operator-(const CaseIteratorImpl &RHS) const {
3436 assert(Case.SI == RHS.Case.SI && "Incompatible operators.")(static_cast <bool> (Case.SI == RHS.Case.SI && "Incompatible operators."
) ? void (0) : __assert_fail ("Case.SI == RHS.Case.SI && \"Incompatible operators.\""
, "llvm/include/llvm/IR/Instructions.h", 3436, __extension__ __PRETTY_FUNCTION__
))
;
3437 return Case.Index - RHS.Case.Index;
3438 }
3439 bool operator==(const CaseIteratorImpl &RHS) const {
3440 return Case == RHS.Case;
3441 }
3442 bool operator<(const CaseIteratorImpl &RHS) const {
3443 assert(Case.SI == RHS.Case.SI && "Incompatible operators.")(static_cast <bool> (Case.SI == RHS.Case.SI && "Incompatible operators."
) ? void (0) : __assert_fail ("Case.SI == RHS.Case.SI && \"Incompatible operators.\""
, "llvm/include/llvm/IR/Instructions.h", 3443, __extension__ __PRETTY_FUNCTION__
))
;
3444 return Case.Index < RHS.Case.Index;
3445 }
3446 const CaseHandleT &operator*() const { return Case; }
3447 };
3448
3449 using CaseIt = CaseIteratorImpl<CaseHandle>;
3450 using ConstCaseIt = CaseIteratorImpl<ConstCaseHandle>;
3451
3452 static SwitchInst *Create(Value *Value, BasicBlock *Default,
3453 unsigned NumCases,
3454 Instruction *InsertBefore = nullptr) {
3455 return new SwitchInst(Value, Default, NumCases, InsertBefore);
3456 }
3457
3458 static SwitchInst *Create(Value *Value, BasicBlock *Default,
3459 unsigned NumCases, BasicBlock *InsertAtEnd) {
3460 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
3461 }
3462
3463 /// Provide fast operand accessors
3464 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
;
3465
3466 // Accessor Methods for Switch stmt
3467 Value *getCondition() const { return getOperand(0); }
3468 void setCondition(Value *V) { setOperand(0, V); }
3469
3470 BasicBlock *getDefaultDest() const {
3471 return cast<BasicBlock>(getOperand(1));
3472 }
3473
3474 void setDefaultDest(BasicBlock *DefaultCase) {
3475 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
3476 }
3477
3478 /// Return the number of 'cases' in this switch instruction, excluding the
3479 /// default case.
3480 unsigned getNumCases() const {
3481 return getNumOperands()/2 - 1;
3482 }
3483
3484 /// Returns a read/write iterator that points to the first case in the
3485 /// SwitchInst.
3486 CaseIt case_begin() {
3487 return CaseIt(this, 0);
3488 }
3489
3490 /// Returns a read-only iterator that points to the first case in the
3491 /// SwitchInst.
3492 ConstCaseIt case_begin() const {
3493 return ConstCaseIt(this, 0);
3494 }
3495
3496 /// Returns a read/write iterator that points one past the last in the
3497 /// SwitchInst.
3498 CaseIt case_end() {
3499 return CaseIt(this, getNumCases());
3500 }
3501
3502 /// Returns a read-only iterator that points one past the last in the
3503 /// SwitchInst.
3504 ConstCaseIt case_end() const {
3505 return ConstCaseIt(this, getNumCases());
3506 }
3507
3508 /// Iteration adapter for range-for loops.
3509 iterator_range<CaseIt> cases() {
3510 return make_range(case_begin(), case_end());
3511 }
3512
3513 /// Constant iteration adapter for range-for loops.
3514 iterator_range<ConstCaseIt> cases() const {
3515 return make_range(case_begin(), case_end());
3516 }
3517
3518 /// Returns an iterator that points to the default case.
3519 /// Note: this iterator allows to resolve successor only. Attempt
3520 /// to resolve case value causes an assertion.
3521 /// Also note, that increment and decrement also causes an assertion and
3522 /// makes iterator invalid.
3523 CaseIt case_default() {
3524 return CaseIt(this, DefaultPseudoIndex);
3525 }
3526 ConstCaseIt case_default() const {
3527 return ConstCaseIt(this, DefaultPseudoIndex);
3528 }
3529
3530 /// Search all of the case values for the specified constant. If it is
3531 /// explicitly handled, return the case iterator of it, otherwise return
3532 /// default case iterator to indicate that it is handled by the default
3533 /// handler.
3534 CaseIt findCaseValue(const ConstantInt *C) {
3535 return CaseIt(
3536 this,
3537 const_cast<const SwitchInst *>(this)->findCaseValue(C)->getCaseIndex());
3538 }
3539 ConstCaseIt findCaseValue(const ConstantInt *C) const {
3540 ConstCaseIt I = llvm::find_if(cases(), [C](const ConstCaseHandle &Case) {
3541 return Case.getCaseValue() == C;
3542 });
3543 if (I != case_end())
3544 return I;
3545
3546 return case_default();
3547 }
3548
3549 /// Finds the unique case value for a given successor. Returns null if the
3550 /// successor is not found, not unique, or is the default case.
3551 ConstantInt *findCaseDest(BasicBlock *BB) {
3552 if (BB == getDefaultDest())
3553 return nullptr;
3554
3555 ConstantInt *CI = nullptr;
3556 for (auto Case : cases()) {
3557 if (Case.getCaseSuccessor() != BB)
3558 continue;
3559
3560 if (CI)
3561 return nullptr; // Multiple cases lead to BB.
3562
3563 CI = Case.getCaseValue();
3564 }
3565
3566 return CI;
3567 }
3568
3569 /// Add an entry to the switch instruction.
3570 /// Note:
3571 /// This action invalidates case_end(). Old case_end() iterator will
3572 /// point to the added case.
3573 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
3574
3575 /// This method removes the specified case and its successor from the switch
3576 /// instruction. Note that this operation may reorder the remaining cases at
3577 /// index idx and above.
3578 /// Note:
3579 /// This action invalidates iterators for all cases following the one removed,
3580 /// including the case_end() iterator. It returns an iterator for the next
3581 /// case.
3582 CaseIt removeCase(CaseIt I);
3583
3584 unsigned getNumSuccessors() const { return getNumOperands()/2; }
3585 BasicBlock *getSuccessor(unsigned idx) const {
3586 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!")(static_cast <bool> (idx < getNumSuccessors() &&
"Successor idx out of range for switch!") ? void (0) : __assert_fail
("idx < getNumSuccessors() &&\"Successor idx out of range for switch!\""
, "llvm/include/llvm/IR/Instructions.h", 3586, __extension__ __PRETTY_FUNCTION__
))
;
3587 return cast<BasicBlock>(getOperand(idx*2+1));
3588 }
3589 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3590 assert(idx < getNumSuccessors() && "Successor # out of range for switch!")(static_cast <bool> (idx < getNumSuccessors() &&
"Successor # out of range for switch!") ? void (0) : __assert_fail
("idx < getNumSuccessors() && \"Successor # out of range for switch!\""
, "llvm/include/llvm/IR/Instructions.h", 3590, __extension__ __PRETTY_FUNCTION__
))
;
3591 setOperand(idx * 2 + 1, NewSucc);
3592 }
3593
3594 // Methods for support type inquiry through isa, cast, and dyn_cast:
3595 static bool classof(const Instruction *I) {
3596 return I->getOpcode() == Instruction::Switch;
3597 }
3598 static bool classof(const Value *V) {
3599 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3600 }
3601};
3602
3603/// A wrapper class to simplify modification of SwitchInst cases along with
3604/// their prof branch_weights metadata.
3605class SwitchInstProfUpdateWrapper {
3606 SwitchInst &SI;
3607 Optional<SmallVector<uint32_t, 8> > Weights = None;
3608 bool Changed = false;
3609
3610protected:
3611 static MDNode *getProfBranchWeightsMD(const SwitchInst &SI);
3612
3613 MDNode *buildProfBranchWeightsMD();
3614
3615 void init();
3616
3617public:
3618 using CaseWeightOpt = Optional<uint32_t>;
3619 SwitchInst *operator->() { return &SI; }
3620 SwitchInst &operator*() { return SI; }
3621 operator SwitchInst *() { return &SI; }
3622
3623 SwitchInstProfUpdateWrapper(SwitchInst &SI) : SI(SI) { init(); }
3624
3625 ~SwitchInstProfUpdateWrapper() {
3626 if (Changed)
3627 SI.setMetadata(LLVMContext::MD_prof, buildProfBranchWeightsMD());
3628 }
3629
3630 /// Delegate the call to the underlying SwitchInst::removeCase() and remove
3631 /// correspondent branch weight.
3632 SwitchInst::CaseIt removeCase(SwitchInst::CaseIt I);
3633
3634 /// Delegate the call to the underlying SwitchInst::addCase() and set the
3635 /// specified branch weight for the added case.
3636 void addCase(ConstantInt *OnVal, BasicBlock *Dest, CaseWeightOpt W);
3637
3638 /// Delegate the call to the underlying SwitchInst::eraseFromParent() and mark
3639 /// this object to not touch the underlying SwitchInst in destructor.
3640 SymbolTableList<Instruction>::iterator eraseFromParent();
3641
3642 void setSuccessorWeight(unsigned idx, CaseWeightOpt W);
3643 CaseWeightOpt getSuccessorWeight(unsigned idx);
3644
3645 static CaseWeightOpt getSuccessorWeight(const SwitchInst &SI, unsigned idx);
3646};
3647
3648template <>
3649struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
3650};
3651
3652DEFINE_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 { (static_cast <bool
> (i_nocapture < OperandTraits<SwitchInst>::operands
(this) && "getOperand() out of range!") ? void (0) : __assert_fail
("i_nocapture < OperandTraits<SwitchInst>::operands(this) && \"getOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 3652, __extension__ __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) { (static_cast <bool> (i_nocapture
< OperandTraits<SwitchInst>::operands(this) &&
"setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<SwitchInst>::operands(this) && \"setOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 3652, __extension__ __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); }
3653
3654//===----------------------------------------------------------------------===//
3655// IndirectBrInst Class
3656//===----------------------------------------------------------------------===//
3657
3658//===---------------------------------------------------------------------------
3659/// Indirect Branch Instruction.
3660///
3661class IndirectBrInst : public Instruction {
3662 unsigned ReservedSpace;
3663
3664 // Operand[0] = Address to jump to
3665 // Operand[n+1] = n-th destination
3666 IndirectBrInst(const IndirectBrInst &IBI);
3667
3668 /// Create a new indirectbr instruction, specifying an
3669 /// Address to jump to. The number of expected destinations can be specified
3670 /// here to make memory allocation more efficient. This constructor can also
3671 /// autoinsert before another instruction.
3672 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
3673
3674 /// Create a new indirectbr instruction, specifying an
3675 /// Address to jump to. The number of expected destinations can be specified
3676 /// here to make memory allocation more efficient. This constructor also
3677 /// autoinserts at the end of the specified BasicBlock.
3678 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
3679
3680 // allocate space for exactly zero operands
3681 void *operator new(size_t S) { return User::operator new(S); }
3682
3683 void init(Value *Address, unsigned NumDests);
3684 void growOperands();
3685
3686protected:
3687 // Note: Instruction needs to be a friend here to call cloneImpl.
3688 friend class Instruction;
3689
3690 IndirectBrInst *cloneImpl() const;
3691
3692public:
3693 void operator delete(void *Ptr) { User::operator delete(Ptr); }
3694
3695 /// Iterator type that casts an operand to a basic block.
3696 ///
3697 /// This only makes sense because the successors are stored as adjacent
3698 /// operands for indirectbr instructions.
3699 struct succ_op_iterator
3700 : iterator_adaptor_base<succ_op_iterator, value_op_iterator,
3701 std::random_access_iterator_tag, BasicBlock *,
3702 ptrdiff_t, BasicBlock *, BasicBlock *> {
3703 explicit succ_op_iterator(value_op_iterator I) : iterator_adaptor_base(I) {}
3704
3705 BasicBlock *operator*() const { return cast<BasicBlock>(*I); }
3706 BasicBlock *operator->() const { return operator*(); }
3707 };
3708
3709 /// The const version of `succ_op_iterator`.
3710 struct const_succ_op_iterator
3711 : iterator_adaptor_base<const_succ_op_iterator, const_value_op_iterator,
3712 std::random_access_iterator_tag,
3713 const BasicBlock *, ptrdiff_t, const BasicBlock *,
3714 const BasicBlock *> {
3715 explicit const_succ_op_iterator(const_value_op_iterator I)
3716 : iterator_adaptor_base(I) {}
3717
3718 const BasicBlock *operator*() const { return cast<BasicBlock>(*I); }
3719 const BasicBlock *operator->() const { return operator*(); }
3720 };
3721
3722 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3723 Instruction *InsertBefore = nullptr) {
3724 return new IndirectBrInst(Address, NumDests, InsertBefore);
3725 }
3726
3727 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3728 BasicBlock *InsertAtEnd) {
3729 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
3730 }
3731
3732 /// Provide fast operand accessors.
3733 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
;
3734
3735 // Accessor Methods for IndirectBrInst instruction.
3736 Value *getAddress() { return getOperand(0); }
3737 const Value *getAddress() const { return getOperand(0); }
3738 void setAddress(Value *V) { setOperand(0, V); }
3739
3740 /// return the number of possible destinations in this
3741 /// indirectbr instruction.
3742 unsigned getNumDestinations() const { return getNumOperands()-1; }
3743
3744 /// Return the specified destination.
3745 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
3746 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
3747
3748 /// Add a destination.
3749 ///
3750 void addDestination(BasicBlock *Dest);
3751
3752 /// This method removes the specified successor from the
3753 /// indirectbr instruction.
3754 void removeDestination(unsigned i);
3755
3756 unsigned getNumSuccessors() const { return getNumOperands()-1; }
3757 BasicBlock *getSuccessor(unsigned i) const {
3758 return cast<BasicBlock>(getOperand(i+1));
3759 }
3760 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
3761 setOperand(i + 1, NewSucc);
3762 }
3763
3764 iterator_range<succ_op_iterator> successors() {
3765 return make_range(succ_op_iterator(std::next(value_op_begin())),
3766 succ_op_iterator(value_op_end()));
3767 }
3768
3769 iterator_range<const_succ_op_iterator> successors() const {
3770 return make_range(const_succ_op_iterator(std::next(value_op_begin())),
3771 const_succ_op_iterator(value_op_end()));
3772 }
3773
3774 // Methods for support type inquiry through isa, cast, and dyn_cast:
3775 static bool classof(const Instruction *I) {
3776 return I->getOpcode() == Instruction::IndirectBr;
3777 }
3778 static bool classof(const Value *V) {
3779 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3780 }
3781};
3782
3783template <>
3784struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
3785};
3786
3787DEFINE_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 { (static_cast <bool
> (i_nocapture < OperandTraits<IndirectBrInst>::operands
(this) && "getOperand() out of range!") ? void (0) : __assert_fail
("i_nocapture < OperandTraits<IndirectBrInst>::operands(this) && \"getOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 3787, __extension__ __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) { (static_cast <bool> (i_nocapture
< OperandTraits<IndirectBrInst>::operands(this) &&
"setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<IndirectBrInst>::operands(this) && \"setOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 3787, __extension__ __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
); }
3788
3789//===----------------------------------------------------------------------===//
3790// InvokeInst Class
3791//===----------------------------------------------------------------------===//
3792
3793/// Invoke instruction. The SubclassData field is used to hold the
3794/// calling convention of the call.
3795///
3796class InvokeInst : public CallBase {
3797 /// The number of operands for this call beyond the called function,
3798 /// arguments, and operand bundles.
3799 static constexpr int NumExtraOperands = 2;
3800
3801 /// The index from the end of the operand array to the normal destination.
3802 static constexpr int NormalDestOpEndIdx = -3;
3803
3804 /// The index from the end of the operand array to the unwind destination.
3805 static constexpr int UnwindDestOpEndIdx = -2;
3806
3807 InvokeInst(const InvokeInst &BI);
3808
3809 /// Construct an InvokeInst given a range of arguments.
3810 ///
3811 /// Construct an InvokeInst from a range of arguments
3812 inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3813 BasicBlock *IfException, ArrayRef<Value *> Args,
3814 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
3815 const Twine &NameStr, Instruction *InsertBefore);
3816
3817 inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3818 BasicBlock *IfException, ArrayRef<Value *> Args,
3819 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
3820 const Twine &NameStr, BasicBlock *InsertAtEnd);
3821
3822 void init(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3823 BasicBlock *IfException, ArrayRef<Value *> Args,
3824 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
3825
3826 /// Compute the number of operands to allocate.
3827 static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) {
3828 // We need one operand for the called function, plus our extra operands and
3829 // the input operand counts provided.
3830 return 1 + NumExtraOperands + NumArgs + NumBundleInputs;
3831 }
3832
3833protected:
3834 // Note: Instruction needs to be a friend here to call cloneImpl.
3835 friend class Instruction;
3836
3837 InvokeInst *cloneImpl() const;
3838
3839public:
3840 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3841 BasicBlock *IfException, ArrayRef<Value *> Args,
3842 const Twine &NameStr,
3843 Instruction *InsertBefore = nullptr) {
3844 int NumOperands = ComputeNumOperands(Args.size());
3845 return new (NumOperands)
3846 InvokeInst(Ty, Func, IfNormal, IfException, Args, None, NumOperands,
3847 NameStr, InsertBefore);
3848 }
3849
3850 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3851 BasicBlock *IfException, ArrayRef<Value *> Args,
3852 ArrayRef<OperandBundleDef> Bundles = None,
3853 const Twine &NameStr = "",
3854 Instruction *InsertBefore = nullptr) {
3855 int NumOperands =
3856 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles));
3857 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3858
3859 return new (NumOperands, DescriptorBytes)
3860 InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, NumOperands,
3861 NameStr, InsertBefore);
3862 }
3863
3864 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3865 BasicBlock *IfException, ArrayRef<Value *> Args,
3866 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3867 int NumOperands = ComputeNumOperands(Args.size());
3868 return new (NumOperands)
3869 InvokeInst(Ty, Func, IfNormal, IfException, Args, None, NumOperands,
3870 NameStr, InsertAtEnd);
3871 }
3872
3873 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3874 BasicBlock *IfException, ArrayRef<Value *> Args,
3875 ArrayRef<OperandBundleDef> Bundles,
3876 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3877 int NumOperands =
3878 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles));
3879 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3880
3881 return new (NumOperands, DescriptorBytes)
3882 InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, NumOperands,
3883 NameStr, InsertAtEnd);
3884 }
3885
3886 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
3887 BasicBlock *IfException, ArrayRef<Value *> Args,
3888 const Twine &NameStr,
3889 Instruction *InsertBefore = nullptr) {
3890 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3891 IfException, Args, None, NameStr, InsertBefore);
3892 }
3893
3894 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
3895 BasicBlock *IfException, ArrayRef<Value *> Args,
3896 ArrayRef<OperandBundleDef> Bundles = None,
3897 const Twine &NameStr = "",
3898 Instruction *InsertBefore = nullptr) {
3899 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3900 IfException, Args, Bundles, NameStr, InsertBefore);
3901 }
3902
3903 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
3904 BasicBlock *IfException, ArrayRef<Value *> Args,
3905 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3906 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3907 IfException, Args, NameStr, InsertAtEnd);
3908 }
3909
3910 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
3911 BasicBlock *IfException, ArrayRef<Value *> Args,
3912 ArrayRef<OperandBundleDef> Bundles,
3913 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3914 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3915 IfException, Args, Bundles, NameStr, InsertAtEnd);
3916 }
3917
3918 /// Create a clone of \p II with a different set of operand bundles and
3919 /// insert it before \p InsertPt.
3920 ///
3921 /// The returned invoke instruction is identical to \p II in every way except
3922 /// that the operand bundles for the new instruction are set to the operand
3923 /// bundles in \p Bundles.
3924 static InvokeInst *Create(InvokeInst *II, ArrayRef<OperandBundleDef> Bundles,
3925 Instruction *InsertPt = nullptr);
3926
3927 // get*Dest - Return the destination basic blocks...
3928 BasicBlock *getNormalDest() const {
3929 return cast<BasicBlock>(Op<NormalDestOpEndIdx>());
3930 }
3931 BasicBlock *getUnwindDest() const {
3932 return cast<BasicBlock>(Op<UnwindDestOpEndIdx>());
14
Calling 'cast<llvm::BasicBlock, llvm::Use>'
27
Returning from 'cast<llvm::BasicBlock, llvm::Use>'
28
Returning pointer
3933 }
3934 void setNormalDest(BasicBlock *B) {
3935 Op<NormalDestOpEndIdx>() = reinterpret_cast<Value *>(B);
3936 }
3937 void setUnwindDest(BasicBlock *B) {
3938 Op<UnwindDestOpEndIdx>() = reinterpret_cast<Value *>(B);
3939 }
3940
3941 /// Get the landingpad instruction from the landing pad
3942 /// block (the unwind destination).
3943 LandingPadInst *getLandingPadInst() const;
3944
3945 BasicBlock *getSuccessor(unsigned i) const {
3946 assert(i < 2 && "Successor # out of range for invoke!")(static_cast <bool> (i < 2 && "Successor # out of range for invoke!"
) ? void (0) : __assert_fail ("i < 2 && \"Successor # out of range for invoke!\""
, "llvm/include/llvm/IR/Instructions.h", 3946, __extension__ __PRETTY_FUNCTION__
))
;
3947 return i == 0 ? getNormalDest() : getUnwindDest();
3948 }
3949
3950 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
3951 assert(i < 2 && "Successor # out of range for invoke!")(static_cast <bool> (i < 2 && "Successor # out of range for invoke!"
) ? void (0) : __assert_fail ("i < 2 && \"Successor # out of range for invoke!\""
, "llvm/include/llvm/IR/Instructions.h", 3951, __extension__ __PRETTY_FUNCTION__
))
;
3952 if (i == 0)
3953 setNormalDest(NewSucc);
3954 else
3955 setUnwindDest(NewSucc);
3956 }
3957
3958 unsigned getNumSuccessors() const { return 2; }
3959
3960 // Methods for support type inquiry through isa, cast, and dyn_cast:
3961 static bool classof(const Instruction *I) {
3962 return (I->getOpcode() == Instruction::Invoke);
3963 }
3964 static bool classof(const Value *V) {
3965 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3966 }
3967
3968private:
3969 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3970 // method so that subclasses cannot accidentally use it.
3971 template <typename Bitfield>
3972 void setSubclassData(typename Bitfield::Type Value) {
3973 Instruction::setSubclassData<Bitfield>(Value);
3974 }
3975};
3976
3977InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3978 BasicBlock *IfException, ArrayRef<Value *> Args,
3979 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
3980 const Twine &NameStr, Instruction *InsertBefore)
3981 : CallBase(Ty->getReturnType(), Instruction::Invoke,
3982 OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands,
3983 InsertBefore) {
3984 init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr);
3985}
3986
3987InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3988 BasicBlock *IfException, ArrayRef<Value *> Args,
3989 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
3990 const Twine &NameStr, BasicBlock *InsertAtEnd)
3991 : CallBase(Ty->getReturnType(), Instruction::Invoke,
3992 OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands,
3993 InsertAtEnd) {
3994 init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr);
3995}
3996
3997//===----------------------------------------------------------------------===//
3998// CallBrInst Class
3999//===----------------------------------------------------------------------===//
4000
4001/// CallBr instruction, tracking function calls that may not return control but
4002/// instead transfer it to a third location. The SubclassData field is used to
4003/// hold the calling convention of the call.
4004///
4005class CallBrInst : public CallBase {
4006
4007 unsigned NumIndirectDests;
4008
4009 CallBrInst(const CallBrInst &BI);
4010
4011 /// Construct a CallBrInst given a range of arguments.
4012 ///
4013 /// Construct a CallBrInst from a range of arguments
4014 inline CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest,
4015 ArrayRef<BasicBlock *> IndirectDests,
4016 ArrayRef<Value *> Args,
4017 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
4018 const Twine &NameStr, Instruction *InsertBefore);
4019
4020 inline CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest,
4021 ArrayRef<BasicBlock *> IndirectDests,
4022 ArrayRef<Value *> Args,
4023 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
4024 const Twine &NameStr, BasicBlock *InsertAtEnd);
4025
4026 void init(FunctionType *FTy, Value *Func, BasicBlock *DefaultDest,
4027 ArrayRef<BasicBlock *> IndirectDests, ArrayRef<Value *> Args,
4028 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
4029
4030 /// Compute the number of operands to allocate.
4031 static int ComputeNumOperands(int NumArgs, int NumIndirectDests,
4032 int NumBundleInputs = 0) {
4033 // We need one operand for the called function, plus our extra operands and
4034 // the input operand counts provided.
4035 return 2 + NumIndirectDests + NumArgs + NumBundleInputs;
4036 }
4037
4038protected:
4039 // Note: Instruction needs to be a friend here to call cloneImpl.
4040 friend class Instruction;
4041
4042 CallBrInst *cloneImpl() const;
4043
4044public:
4045 static CallBrInst *Create(FunctionType *Ty, Value *Func,
4046 BasicBlock *DefaultDest,
4047 ArrayRef<BasicBlock *> IndirectDests,
4048 ArrayRef<Value *> Args, const Twine &NameStr,
4049 Instruction *InsertBefore = nullptr) {
4050 int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size());
4051 return new (NumOperands)
4052 CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, None,
4053 NumOperands, NameStr, InsertBefore);
4054 }
4055
4056 static CallBrInst *Create(FunctionType *Ty, Value *Func,
4057 BasicBlock *DefaultDest,
4058 ArrayRef<BasicBlock *> IndirectDests,
4059 ArrayRef<Value *> Args,
4060 ArrayRef<OperandBundleDef> Bundles = None,
4061 const Twine &NameStr = "",
4062 Instruction *InsertBefore = nullptr) {
4063 int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size(),
4064 CountBundleInputs(Bundles));
4065 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
4066