Bug Summary

File: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-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -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-14~++20220116100644+5f782d25a742/build-llvm -resource-dir /usr/lib/llvm-14/lib/clang/14.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-14~++20220116100644+5f782d25a742/llvm/lib/CodeGen -I include -I /build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/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-14/lib/clang/14.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-14~++20220116100644+5f782d25a742/build-llvm=build-llvm -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/build-llvm=build-llvm -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/= -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 -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/build-llvm=build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/= -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-01-16-232930-107970-1 -x c++ /build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/llvm/lib/CodeGen/WinEHPrepare.cpp

/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/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/CFG.h"
23#include "llvm/Analysis/EHPersonalities.h"
24#include "llvm/CodeGen/MachineBasicBlock.h"
25#include "llvm/CodeGen/Passes.h"
26#include "llvm/CodeGen/WinEHFuncInfo.h"
27#include "llvm/IR/Verifier.h"
28#include "llvm/InitializePasses.h"
29#include "llvm/MC/MCSymbol.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 'InvokeInst'
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 'FuncletPadInst'
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
::all_of(IncomingColors, [&](BasicBlock *Color) { return Color
!= FuncletPadBB; })) && "Cloning should leave this funclet's blocks monochromatic"
) ? void (0) : __assert_fail ("(IncomingColors.size() == 1 || llvm::all_of(IncomingColors, [&](BasicBlock *Color) { return Color != FuncletPadBB; })) && \"Cloning should leave this funclet's blocks monochromatic\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 854, __extension__ __PRETTY_FUNCTION__
))
850 llvm::all_of(IncomingColors,(static_cast <bool> ((IncomingColors.size() == 1 || llvm
::all_of(IncomingColors, [&](BasicBlock *Color) { return Color
!= FuncletPadBB; })) && "Cloning should leave this funclet's blocks monochromatic"
) ? void (0) : __assert_fail ("(IncomingColors.size() == 1 || llvm::all_of(IncomingColors, [&](BasicBlock *Color) { return Color != FuncletPadBB; })) && \"Cloning should leave this funclet's blocks monochromatic\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 854, __extension__ __PRETTY_FUNCTION__
))
851 [&](BasicBlock *Color) {(static_cast <bool> ((IncomingColors.size() == 1 || llvm
::all_of(IncomingColors, [&](BasicBlock *Color) { return Color
!= FuncletPadBB; })) && "Cloning should leave this funclet's blocks monochromatic"
) ? void (0) : __assert_fail ("(IncomingColors.size() == 1 || llvm::all_of(IncomingColors, [&](BasicBlock *Color) { return Color != FuncletPadBB; })) && \"Cloning should leave this funclet's blocks monochromatic\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 854, __extension__ __PRETTY_FUNCTION__
))
852 return Color != FuncletPadBB;(static_cast <bool> ((IncomingColors.size() == 1 || llvm
::all_of(IncomingColors, [&](BasicBlock *Color) { return Color
!= FuncletPadBB; })) && "Cloning should leave this funclet's blocks monochromatic"
) ? void (0) : __assert_fail ("(IncomingColors.size() == 1 || llvm::all_of(IncomingColors, [&](BasicBlock *Color) { return Color != FuncletPadBB; })) && \"Cloning should leave this funclet's blocks monochromatic\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 854, __extension__ __PRETTY_FUNCTION__
))
853 })) &&(static_cast <bool> ((IncomingColors.size() == 1 || llvm
::all_of(IncomingColors, [&](BasicBlock *Color) { return Color
!= FuncletPadBB; })) && "Cloning should leave this funclet's blocks monochromatic"
) ? void (0) : __assert_fail ("(IncomingColors.size() == 1 || llvm::all_of(IncomingColors, [&](BasicBlock *Color) { return Color != FuncletPadBB; })) && \"Cloning should leave this funclet's blocks monochromatic\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 854, __extension__ __PRETTY_FUNCTION__
))
854 "Cloning should leave this funclet's blocks monochromatic")(static_cast <bool> ((IncomingColors.size() == 1 || llvm
::all_of(IncomingColors, [&](BasicBlock *Color) { return Color
!= FuncletPadBB; })) && "Cloning should leave this funclet's blocks monochromatic"
) ? void (0) : __assert_fail ("(IncomingColors.size() == 1 || llvm::all_of(IncomingColors, [&](BasicBlock *Color) { return Color != FuncletPadBB; })) && \"Cloning should leave this funclet's blocks monochromatic\""
, "llvm/lib/CodeGen/WinEHPrepare.cpp", 854, __extension__ __PRETTY_FUNCTION__
))
;
855 EdgeTargetsFunclet = (IncomingColors.front() == FuncletPadBB);
856 }
857 if (IsForOldBlock != EdgeTargetsFunclet)
858 continue;
859 PN->removeIncomingValue(IncomingBlock, /*DeletePHIIfEmpty=*/false);
860 // Revisit the next entry.
861 --PredIdx;
862 --PredEnd;
863 }
864 };
865
866 for (auto &BBMapping : Orig2Clone) {
867 BasicBlock *OldBlock = BBMapping.first;
868 BasicBlock *NewBlock = BBMapping.second;
869 for (PHINode &OldPN : OldBlock->phis()) {
870 UpdatePHIOnClonedBlock(&OldPN, /*IsForOldBlock=*/true);
871 }
872 for (PHINode &NewPN : NewBlock->phis()) {
873 UpdatePHIOnClonedBlock(&NewPN, /*IsForOldBlock=*/false);
874 }
875 }
876
877 // Check to see if SuccBB has PHI nodes. If so, we need to add entries to
878 // the PHI nodes for NewBB now.
879 for (auto &BBMapping : Orig2Clone) {
880 BasicBlock *OldBlock = BBMapping.first;
881 BasicBlock *NewBlock = BBMapping.second;
882 for (BasicBlock *SuccBB : successors(NewBlock)) {
883 for (PHINode &SuccPN : SuccBB->phis()) {
884 // Ok, we have a PHI node. Figure out what the incoming value was for
885 // the OldBlock.
886 int OldBlockIdx = SuccPN.getBasicBlockIndex(OldBlock);
887 if (OldBlockIdx == -1)
888 break;
889 Value *IV = SuccPN.getIncomingValue(OldBlockIdx);
890
891 // Remap the value if necessary.
892 if (auto *Inst = dyn_cast<Instruction>(IV)) {
893 ValueToValueMapTy::iterator I = VMap.find(Inst);
894 if (I != VMap.end())
895 IV = I->second;
896 }
897
898 SuccPN.addIncoming(IV, NewBlock);
899 }
900 }
901 }
902
903 for (ValueToValueMapTy::value_type VT : VMap) {
904 // If there were values defined in BB that are used outside the funclet,
905 // then we now have to update all uses of the value to use either the
906 // original value, the cloned value, or some PHI derived value. This can
907 // require arbitrary PHI insertion, of which we are prepared to do, clean
908 // these up now.
909 SmallVector<Use *, 16> UsesToRename;
910
911 auto *OldI = dyn_cast<Instruction>(const_cast<Value *>(VT.first));
912 if (!OldI)
913 continue;
914 auto *NewI = cast<Instruction>(VT.second);
915 // Scan all uses of this instruction to see if it is used outside of its
916 // funclet, and if so, record them in UsesToRename.
917 for (Use &U : OldI->uses()) {
918 Instruction *UserI = cast<Instruction>(U.getUser());
919 BasicBlock *UserBB = UserI->getParent();
920 ColorVector &ColorsForUserBB = BlockColors[UserBB];
921 assert(!ColorsForUserBB.empty())(static_cast <bool> (!ColorsForUserBB.empty()) ? void (
0) : __assert_fail ("!ColorsForUserBB.empty()", "llvm/lib/CodeGen/WinEHPrepare.cpp"
, 921, __extension__ __PRETTY_FUNCTION__))
;
922 if (ColorsForUserBB.size() > 1 ||
923 *ColorsForUserBB.begin() != FuncletPadBB)
924 UsesToRename.push_back(&U);
925 }
926
927 // If there are no uses outside the block, we're done with this
928 // instruction.
929 if (UsesToRename.empty())
930 continue;
931
932 // We found a use of OldI outside of the funclet. Rename all uses of OldI
933 // that are outside its funclet to be uses of the appropriate PHI node
934 // etc.
935 SSAUpdater SSAUpdate;
936 SSAUpdate.Initialize(OldI->getType(), OldI->getName());
937 SSAUpdate.AddAvailableValue(OldI->getParent(), OldI);
938 SSAUpdate.AddAvailableValue(NewI->getParent(), NewI);
939
940 while (!UsesToRename.empty())
941 SSAUpdate.RewriteUseAfterInsertions(*UsesToRename.pop_back_val());
942 }
943 }
944}
945
946void WinEHPrepare::removeImplausibleInstructions(Function &F) {
947 // Remove implausible terminators and replace them with UnreachableInst.
948 for (auto &Funclet : FuncletBlocks) {
949 BasicBlock *FuncletPadBB = Funclet.first;
950 std::vector<BasicBlock *> &BlocksInFunclet = Funclet.second;
951 Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI();
952 auto *FuncletPad = dyn_cast<FuncletPadInst>(FirstNonPHI);
953 auto *CatchPad = dyn_cast_or_null<CatchPadInst>(FuncletPad);
954 auto *CleanupPad = dyn_cast_or_null<CleanupPadInst>(FuncletPad);
955
956 for (BasicBlock *BB : BlocksInFunclet) {
957 for (Instruction &I : *BB) {
958 auto *CB = dyn_cast<CallBase>(&I);
959 if (!CB)
960 continue;
961
962 Value *FuncletBundleOperand = nullptr;
963 if (auto BU = CB->getOperandBundle(LLVMContext::OB_funclet))
964 FuncletBundleOperand = BU->Inputs.front();
965
966 if (FuncletBundleOperand == FuncletPad)
967 continue;
968
969 // Skip call sites which are nounwind intrinsics or inline asm.
970 auto *CalledFn =
971 dyn_cast<Function>(CB->getCalledOperand()->stripPointerCasts());
972 if (CalledFn && ((CalledFn->isIntrinsic() && CB->doesNotThrow()) ||
973 CB->isInlineAsm()))
974 continue;
975
976 // This call site was not part of this funclet, remove it.
977 if (isa<InvokeInst>(CB)) {
978 // Remove the unwind edge if it was an invoke.
979 removeUnwindEdge(BB);
980 // Get a pointer to the new call.
981 BasicBlock::iterator CallI =
982 std::prev(BB->getTerminator()->getIterator());
983 auto *CI = cast<CallInst>(&*CallI);
984 changeToUnreachable(CI);
985 } else {
986 changeToUnreachable(&I);
987 }
988
989 // There are no more instructions in the block (except for unreachable),
990 // we are done.
991 break;
992 }
993
994 Instruction *TI = BB->getTerminator();
995 // CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst.
996 bool IsUnreachableRet = isa<ReturnInst>(TI) && FuncletPad;
997 // The token consumed by a CatchReturnInst must match the funclet token.
998 bool IsUnreachableCatchret = false;
999 if (auto *CRI = dyn_cast<CatchReturnInst>(TI))
1000 IsUnreachableCatchret = CRI->getCatchPad() != CatchPad;
1001 // The token consumed by a CleanupReturnInst must match the funclet token.
1002 bool IsUnreachableCleanupret = false;
1003 if (auto *CRI = dyn_cast<CleanupReturnInst>(TI))
1004 IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad;
1005 if (IsUnreachableRet || IsUnreachableCatchret ||
1006 IsUnreachableCleanupret) {
1007 changeToUnreachable(TI);
1008 } else if (isa<InvokeInst>(TI)) {
1009 if (Personality == EHPersonality::MSVC_CXX && CleanupPad) {
1010 // Invokes within a cleanuppad for the MSVC++ personality never
1011 // transfer control to their unwind edge: the personality will
1012 // terminate the program.
1013 removeUnwindEdge(BB);
1014 }
1015 }
1016 }
1017 }
1018}
1019
1020void WinEHPrepare::cleanupPreparedFunclets(Function &F) {
1021 // Clean-up some of the mess we made by removing useles PHI nodes, trivial
1022 // branches, etc.
1023 for (BasicBlock &BB : llvm::make_early_inc_range(F)) {
1024 SimplifyInstructionsInBlock(&BB);
1025 ConstantFoldTerminator(&BB, /*DeleteDeadConditions=*/true);
1026 MergeBlockIntoPredecessor(&BB);
1027 }
1028
1029 // We might have some unreachable blocks after cleaning up some impossible
1030 // control flow.
1031 removeUnreachableBlocks(F);
1032}
1033
1034#ifndef NDEBUG
1035void WinEHPrepare::verifyPreparedFunclets(Function &F) {
1036 for (BasicBlock &BB : F) {
1037 size_t NumColors = BlockColors[&BB].size();
1038 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", 1038, __extension__ __PRETTY_FUNCTION__
))
;
1039 if (NumColors == 0)
1040 report_fatal_error("Uncolored BB!");
1041 if (NumColors > 1)
1042 report_fatal_error("Multicolor BB!");
1043 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", 1044, __extension__ __PRETTY_FUNCTION__
))
1044 "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", 1044, __extension__ __PRETTY_FUNCTION__
))
;
1045 }
1046}
1047#endif
1048
1049bool WinEHPrepare::prepareExplicitEH(Function &F) {
1050 // Remove unreachable blocks. It is not valuable to assign them a color and
1051 // their existence can trick us into thinking values are alive when they are
1052 // not.
1053 removeUnreachableBlocks(F);
1054
1055 // Determine which blocks are reachable from which funclet entries.
1056 colorFunclets(F);
1057
1058 cloneCommonBlocks(F);
1059
1060 if (!DisableDemotion)
1061 demotePHIsOnFunclets(F, DemoteCatchSwitchPHIOnly ||
1062 DemoteCatchSwitchPHIOnlyOpt);
1063
1064 if (!DisableCleanups) {
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 removeImplausibleInstructions(F);
1067
1068 assert(!verifyFunction(F, &dbgs()))(static_cast <bool> (!verifyFunction(F, &dbgs())) ?
void (0) : __assert_fail ("!verifyFunction(F, &dbgs())",
"llvm/lib/CodeGen/WinEHPrepare.cpp", 1068, __extension__ __PRETTY_FUNCTION__
))
;
1069 cleanupPreparedFunclets(F);
1070 }
1071
1072 LLVM_DEBUG(verifyPreparedFunclets(F))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare")) { verifyPreparedFunclets(F); } } while (false
)
;
1073 // Recolor the CFG to verify that all is well.
1074 LLVM_DEBUG(colorFunclets(F))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare")) { colorFunclets(F); } } while (false)
;
1075 LLVM_DEBUG(verifyPreparedFunclets(F))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("winehprepare")) { verifyPreparedFunclets(F); } } while (false
)
;
1076
1077 BlockColors.clear();
1078 FuncletBlocks.clear();
1079
1080 return true;
1081}
1082
1083// TODO: Share loads when one use dominates another, or when a catchpad exit
1084// dominates uses (needs dominators).
1085AllocaInst *WinEHPrepare::insertPHILoads(PHINode *PN, Function &F) {
1086 BasicBlock *PHIBlock = PN->getParent();
1087 AllocaInst *SpillSlot = nullptr;
1088 Instruction *EHPad = PHIBlock->getFirstNonPHI();
1089
1090 if (!EHPad->isTerminator()) {
1091 // If the EHPad isn't a terminator, then we can insert a load in this block
1092 // that will dominate all uses.
1093 SpillSlot = new AllocaInst(PN->getType(), DL->getAllocaAddrSpace(), nullptr,
1094 Twine(PN->getName(), ".wineh.spillslot"),
1095 &F.getEntryBlock().front());
1096 Value *V = new LoadInst(PN->getType(), SpillSlot,
1097 Twine(PN->getName(), ".wineh.reload"),
1098 &*PHIBlock->getFirstInsertionPt());
1099 PN->replaceAllUsesWith(V);
1100 return SpillSlot;
1101 }
1102
1103 // Otherwise, we have a PHI on a terminator EHPad, and we give up and insert
1104 // loads of the slot before every use.
1105 DenseMap<BasicBlock *, Value *> Loads;
1106 for (Use &U : llvm::make_early_inc_range(PN->uses())) {
1107 auto *UsingInst = cast<Instruction>(U.getUser());
1108 if (isa<PHINode>(UsingInst) && UsingInst->getParent()->isEHPad()) {
1109 // Use is on an EH pad phi. Leave it alone; we'll insert loads and
1110 // stores for it separately.
1111 continue;
1112 }
1113 replaceUseWithLoad(PN, U, SpillSlot, Loads, F);
1114 }
1115 return SpillSlot;
1116}
1117
1118// TODO: improve store placement. Inserting at def is probably good, but need
1119// to be careful not to introduce interfering stores (needs liveness analysis).
1120// TODO: identify related phi nodes that can share spill slots, and share them
1121// (also needs liveness).
1122void WinEHPrepare::insertPHIStores(PHINode *OriginalPHI,
1123 AllocaInst *SpillSlot) {
1124 // Use a worklist of (Block, Value) pairs -- the given Value needs to be
1125 // stored to the spill slot by the end of the given Block.
1126 SmallVector<std::pair<BasicBlock *, Value *>, 4> Worklist;
1127
1128 Worklist.push_back({OriginalPHI->getParent(), OriginalPHI});
1129
1130 while (!Worklist.empty()) {
1131 BasicBlock *EHBlock;
1132 Value *InVal;
1133 std::tie(EHBlock, InVal) = Worklist.pop_back_val();
1134
1135 PHINode *PN = dyn_cast<PHINode>(InVal);
1136 if (PN && PN->getParent() == EHBlock) {
1137 // The value is defined by another PHI we need to remove, with no room to
1138 // insert a store after the PHI, so each predecessor needs to store its
1139 // incoming value.
1140 for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
1141 Value *PredVal = PN->getIncomingValue(i);
1142
1143 // Undef can safely be skipped.
1144 if (isa<UndefValue>(PredVal))
1145 continue;
1146
1147 insertPHIStore(PN->getIncomingBlock(i), PredVal, SpillSlot, Worklist);
1148 }
1149 } else {
1150 // We need to store InVal, which dominates EHBlock, but can't put a store
1151 // in EHBlock, so need to put stores in each predecessor.
1152 for (BasicBlock *PredBlock : predecessors(EHBlock)) {
1153 insertPHIStore(PredBlock, InVal, SpillSlot, Worklist);
1154 }
1155 }
1156 }
1157}
1158
1159void WinEHPrepare::insertPHIStore(
1160 BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
1161 SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist) {
1162
1163 if (PredBlock->isEHPad() && PredBlock->getFirstNonPHI()->isTerminator()) {
1164 // Pred is unsplittable, so we need to queue it on the worklist.
1165 Worklist.push_back({PredBlock, PredVal});
1166 return;
1167 }
1168
1169 // Otherwise, insert the store at the end of the basic block.
1170 new StoreInst(PredVal, SpillSlot, PredBlock->getTerminator());
1171}
1172
1173void WinEHPrepare::replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
1174 DenseMap<BasicBlock *, Value *> &Loads,
1175 Function &F) {
1176 // Lazilly create the spill slot.
1177 if (!SpillSlot)
1178 SpillSlot = new AllocaInst(V->getType(), DL->getAllocaAddrSpace(), nullptr,
1179 Twine(V->getName(), ".wineh.spillslot"),
1180 &F.getEntryBlock().front());
1181
1182 auto *UsingInst = cast<Instruction>(U.getUser());
1183 if (auto *UsingPHI = dyn_cast<PHINode>(UsingInst)) {
1184 // If this is a PHI node, we can't insert a load of the value before
1185 // the use. Instead insert the load in the predecessor block
1186 // corresponding to the incoming value.
1187 //
1188 // Note that if there are multiple edges from a basic block to this
1189 // PHI node that we cannot have multiple loads. The problem is that
1190 // the resulting PHI node will have multiple values (from each load)
1191 // coming in from the same block, which is illegal SSA form.
1192 // For this reason, we keep track of and reuse loads we insert.
1193 BasicBlock *IncomingBlock = UsingPHI->getIncomingBlock(U);
1194 if (auto *CatchRet =
1195 dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) {
1196 // Putting a load above a catchret and use on the phi would still leave
1197 // a cross-funclet def/use. We need to split the edge, change the
1198 // catchret to target the new block, and put the load there.
1199 BasicBlock *PHIBlock = UsingInst->getParent();
1200 BasicBlock *NewBlock = SplitEdge(IncomingBlock, PHIBlock);
1201 // SplitEdge gives us:
1202 // IncomingBlock:
1203 // ...
1204 // br label %NewBlock
1205 // NewBlock:
1206 // catchret label %PHIBlock
1207 // But we need:
1208 // IncomingBlock:
1209 // ...
1210 // catchret label %NewBlock
1211 // NewBlock:
1212 // br label %PHIBlock
1213 // So move the terminators to each others' blocks and swap their
1214 // successors.
1215 BranchInst *Goto = cast<BranchInst>(IncomingBlock->getTerminator());
1216 Goto->removeFromParent();
1217 CatchRet->removeFromParent();
1218 IncomingBlock->getInstList().push_back(CatchRet);
1219 NewBlock->getInstList().push_back(Goto);
1220 Goto->setSuccessor(0, PHIBlock);
1221 CatchRet->setSuccessor(NewBlock);
1222 // Update the color mapping for the newly split edge.
1223 // Grab a reference to the ColorVector to be inserted before getting the
1224 // reference to the vector we are copying because inserting the new
1225 // element in BlockColors might cause the map to be reallocated.
1226 ColorVector &ColorsForNewBlock = BlockColors[NewBlock];
1227 ColorVector &ColorsForPHIBlock = BlockColors[PHIBlock];
1228 ColorsForNewBlock = ColorsForPHIBlock;
1229 for (BasicBlock *FuncletPad : ColorsForPHIBlock)
1230 FuncletBlocks[FuncletPad].push_back(NewBlock);
1231 // Treat the new block as incoming for load insertion.
1232 IncomingBlock = NewBlock;
1233 }
1234 Value *&Load = Loads[IncomingBlock];
1235 // Insert the load into the predecessor block
1236 if (!Load)
1237 Load = new LoadInst(V->getType(), SpillSlot,
1238 Twine(V->getName(), ".wineh.reload"),
1239 /*isVolatile=*/false, IncomingBlock->getTerminator());
1240
1241 U.set(Load);
1242 } else {
1243 // Reload right before the old use.
1244 auto *Load = new LoadInst(V->getType(), SpillSlot,
1245 Twine(V->getName(), ".wineh.reload"),
1246 /*isVolatile=*/false, UsingInst);
1247 U.set(Load);
1248 }
1249}
1250
1251void WinEHFuncInfo::addIPToStateRange(const InvokeInst *II,
1252 MCSymbol *InvokeBegin,
1253 MCSymbol *InvokeEnd) {
1254 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", 1255, __extension__ __PRETTY_FUNCTION__
))
1255 "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", 1255, __extension__ __PRETTY_FUNCTION__
))
;
1256 LabelToStateMap[InvokeBegin] = std::make_pair(InvokeStateMap[II], InvokeEnd);
1257}
1258
1259WinEHFuncInfo::WinEHFuncInfo() {}

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