Bug Summary

File:lib/Transforms/Scalar/LoopIdiomRecognize.cpp
Warning:line 1443, column 48
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

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

/build/llvm-toolchain-snapshot-8~svn350071/lib/Transforms/Scalar/LoopIdiomRecognize.cpp

1//===- LoopIdiomRecognize.cpp - Loop idiom recognition --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass implements an idiom recognizer that transforms simple loops into a
11// non-loop form. In cases that this kicks in, it can be a significant
12// performance win.
13//
14// If compiling for code size we avoid idiom recognition if the resulting
15// code could be larger than the code for the original loop. One way this could
16// happen is if the loop is not removable after idiom recognition due to the
17// presence of non-idiom instructions. The initial implementation of the
18// heuristics applies to idioms in multi-block loops.
19//
20//===----------------------------------------------------------------------===//
21//
22// TODO List:
23//
24// Future loop memory idioms to recognize:
25// memcmp, memmove, strlen, etc.
26// Future floating point idioms to recognize in -ffast-math mode:
27// fpowi
28// Future integer operation idioms to recognize:
29// ctpop, ctlz, cttz
30//
31// Beware that isel's default lowering for ctpop is highly inefficient for
32// i64 and larger types when i64 is legal and the value has few bits set. It
33// would be good to enhance isel to emit a loop for ctpop in this case.
34//
35// This could recognize common matrix multiplies and dot product idioms and
36// replace them with calls to BLAS (if linked in??).
37//
38//===----------------------------------------------------------------------===//
39
40#include "llvm/ADT/APInt.h"
41#include "llvm/ADT/ArrayRef.h"
42#include "llvm/ADT/DenseMap.h"
43#include "llvm/ADT/MapVector.h"
44#include "llvm/ADT/SetVector.h"
45#include "llvm/ADT/SmallPtrSet.h"
46#include "llvm/ADT/SmallVector.h"
47#include "llvm/ADT/Statistic.h"
48#include "llvm/ADT/StringRef.h"
49#include "llvm/Analysis/AliasAnalysis.h"
50#include "llvm/Analysis/LoopAccessAnalysis.h"
51#include "llvm/Analysis/LoopInfo.h"
52#include "llvm/Analysis/LoopPass.h"
53#include "llvm/Analysis/MemoryLocation.h"
54#include "llvm/Analysis/ScalarEvolution.h"
55#include "llvm/Analysis/ScalarEvolutionExpander.h"
56#include "llvm/Analysis/ScalarEvolutionExpressions.h"
57#include "llvm/Analysis/TargetLibraryInfo.h"
58#include "llvm/Analysis/TargetTransformInfo.h"
59#include "llvm/Transforms/Utils/Local.h"
60#include "llvm/Analysis/ValueTracking.h"
61#include "llvm/IR/Attributes.h"
62#include "llvm/IR/BasicBlock.h"
63#include "llvm/IR/Constant.h"
64#include "llvm/IR/Constants.h"
65#include "llvm/IR/DataLayout.h"
66#include "llvm/IR/DebugLoc.h"
67#include "llvm/IR/DerivedTypes.h"
68#include "llvm/IR/Dominators.h"
69#include "llvm/IR/GlobalValue.h"
70#include "llvm/IR/GlobalVariable.h"
71#include "llvm/IR/IRBuilder.h"
72#include "llvm/IR/InstrTypes.h"
73#include "llvm/IR/Instruction.h"
74#include "llvm/IR/Instructions.h"
75#include "llvm/IR/IntrinsicInst.h"
76#include "llvm/IR/Intrinsics.h"
77#include "llvm/IR/LLVMContext.h"
78#include "llvm/IR/Module.h"
79#include "llvm/IR/PassManager.h"
80#include "llvm/IR/Type.h"
81#include "llvm/IR/User.h"
82#include "llvm/IR/Value.h"
83#include "llvm/IR/ValueHandle.h"
84#include "llvm/Pass.h"
85#include "llvm/Support/Casting.h"
86#include "llvm/Support/CommandLine.h"
87#include "llvm/Support/Debug.h"
88#include "llvm/Support/raw_ostream.h"
89#include "llvm/Transforms/Scalar.h"
90#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
91#include "llvm/Transforms/Utils/BuildLibCalls.h"
92#include "llvm/Transforms/Utils/LoopUtils.h"
93#include <algorithm>
94#include <cassert>
95#include <cstdint>
96#include <utility>
97#include <vector>
98
99using namespace llvm;
100
101#define DEBUG_TYPE"loop-idiom" "loop-idiom"
102
103STATISTIC(NumMemSet, "Number of memset's formed from loop stores")static llvm::Statistic NumMemSet = {"loop-idiom", "NumMemSet"
, "Number of memset's formed from loop stores", {0}, {false}}
;
104STATISTIC(NumMemCpy, "Number of memcpy's formed from loop load+stores")static llvm::Statistic NumMemCpy = {"loop-idiom", "NumMemCpy"
, "Number of memcpy's formed from loop load+stores", {0}, {false
}}
;
105
106static cl::opt<bool> UseLIRCodeSizeHeurs(
107 "use-lir-code-size-heurs",
108 cl::desc("Use loop idiom recognition code size heuristics when compiling"
109 "with -Os/-Oz"),
110 cl::init(true), cl::Hidden);
111
112namespace {
113
114class LoopIdiomRecognize {
115 Loop *CurLoop = nullptr;
116 AliasAnalysis *AA;
117 DominatorTree *DT;
118 LoopInfo *LI;
119 ScalarEvolution *SE;
120 TargetLibraryInfo *TLI;
121 const TargetTransformInfo *TTI;
122 const DataLayout *DL;
123 bool ApplyCodeSizeHeuristics;
124
125public:
126 explicit LoopIdiomRecognize(AliasAnalysis *AA, DominatorTree *DT,
127 LoopInfo *LI, ScalarEvolution *SE,
128 TargetLibraryInfo *TLI,
129 const TargetTransformInfo *TTI,
130 const DataLayout *DL)
131 : AA(AA), DT(DT), LI(LI), SE(SE), TLI(TLI), TTI(TTI), DL(DL) {}
132
133 bool runOnLoop(Loop *L);
134
135private:
136 using StoreList = SmallVector<StoreInst *, 8>;
137 using StoreListMap = MapVector<Value *, StoreList>;
138
139 StoreListMap StoreRefsForMemset;
140 StoreListMap StoreRefsForMemsetPattern;
141 StoreList StoreRefsForMemcpy;
142 bool HasMemset;
143 bool HasMemsetPattern;
144 bool HasMemcpy;
145
146 /// Return code for isLegalStore()
147 enum LegalStoreKind {
148 None = 0,
149 Memset,
150 MemsetPattern,
151 Memcpy,
152 UnorderedAtomicMemcpy,
153 DontUse // Dummy retval never to be used. Allows catching errors in retval
154 // handling.
155 };
156
157 /// \name Countable Loop Idiom Handling
158 /// @{
159
160 bool runOnCountableLoop();
161 bool runOnLoopBlock(BasicBlock *BB, const SCEV *BECount,
162 SmallVectorImpl<BasicBlock *> &ExitBlocks);
163
164 void collectStores(BasicBlock *BB);
165 LegalStoreKind isLegalStore(StoreInst *SI);
166 enum class ForMemset { No, Yes };
167 bool processLoopStores(SmallVectorImpl<StoreInst *> &SL, const SCEV *BECount,
168 ForMemset For);
169 bool processLoopMemSet(MemSetInst *MSI, const SCEV *BECount);
170
171 bool processLoopStridedStore(Value *DestPtr, unsigned StoreSize,
172 unsigned StoreAlignment, Value *StoredVal,
173 Instruction *TheStore,
174 SmallPtrSetImpl<Instruction *> &Stores,
175 const SCEVAddRecExpr *Ev, const SCEV *BECount,
176 bool NegStride, bool IsLoopMemset = false);
177 bool processLoopStoreOfLoopLoad(StoreInst *SI, const SCEV *BECount);
178 bool avoidLIRForMultiBlockLoop(bool IsMemset = false,
179 bool IsLoopMemset = false);
180
181 /// @}
182 /// \name Noncountable Loop Idiom Handling
183 /// @{
184
185 bool runOnNoncountableLoop();
186
187 bool recognizePopcount();
188 void transformLoopToPopcount(BasicBlock *PreCondBB, Instruction *CntInst,
189 PHINode *CntPhi, Value *Var);
190 bool recognizeAndInsertCTLZ();
191 void transformLoopToCountable(BasicBlock *PreCondBB, Instruction *CntInst,
192 PHINode *CntPhi, Value *Var, Instruction *DefX,
193 const DebugLoc &DL, bool ZeroCheck,
194 bool IsCntPhiUsedOutsideLoop);
195
196 /// @}
197};
198
199class LoopIdiomRecognizeLegacyPass : public LoopPass {
200public:
201 static char ID;
202
203 explicit LoopIdiomRecognizeLegacyPass() : LoopPass(ID) {
204 initializeLoopIdiomRecognizeLegacyPassPass(
205 *PassRegistry::getPassRegistry());
206 }
207
208 bool runOnLoop(Loop *L, LPPassManager &LPM) override {
209 if (skipLoop(L))
210 return false;
211
212 AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
213 DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
214 LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
215 ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
216 TargetLibraryInfo *TLI =
217 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
218 const TargetTransformInfo *TTI =
219 &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
220 *L->getHeader()->getParent());
221 const DataLayout *DL = &L->getHeader()->getModule()->getDataLayout();
222
223 LoopIdiomRecognize LIR(AA, DT, LI, SE, TLI, TTI, DL);
224 return LIR.runOnLoop(L);
225 }
226
227 /// This transformation requires natural loop information & requires that
228 /// loop preheaders be inserted into the CFG.
229 void getAnalysisUsage(AnalysisUsage &AU) const override {
230 AU.addRequired<TargetLibraryInfoWrapperPass>();
231 AU.addRequired<TargetTransformInfoWrapperPass>();
232 getLoopAnalysisUsage(AU);
233 }
234};
235
236} // end anonymous namespace
237
238char LoopIdiomRecognizeLegacyPass::ID = 0;
239
240PreservedAnalyses LoopIdiomRecognizePass::run(Loop &L, LoopAnalysisManager &AM,
241 LoopStandardAnalysisResults &AR,
242 LPMUpdater &) {
243 const auto *DL = &L.getHeader()->getModule()->getDataLayout();
244
245 LoopIdiomRecognize LIR(&AR.AA, &AR.DT, &AR.LI, &AR.SE, &AR.TLI, &AR.TTI, DL);
246 if (!LIR.runOnLoop(&L))
1
Calling 'LoopIdiomRecognize::runOnLoop'
247 return PreservedAnalyses::all();
248
249 return getLoopPassPreservedAnalyses();
250}
251
252INITIALIZE_PASS_BEGIN(LoopIdiomRecognizeLegacyPass, "loop-idiom",static void *initializeLoopIdiomRecognizeLegacyPassPassOnce(PassRegistry
&Registry) {
253 "Recognize loop idioms", false, false)static void *initializeLoopIdiomRecognizeLegacyPassPassOnce(PassRegistry
&Registry) {
254INITIALIZE_PASS_DEPENDENCY(LoopPass)initializeLoopPassPass(Registry);
255INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)initializeTargetLibraryInfoWrapperPassPass(Registry);
256INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)initializeTargetTransformInfoWrapperPassPass(Registry);
257INITIALIZE_PASS_END(LoopIdiomRecognizeLegacyPass, "loop-idiom",PassInfo *PI = new PassInfo( "Recognize loop idioms", "loop-idiom"
, &LoopIdiomRecognizeLegacyPass::ID, PassInfo::NormalCtor_t
(callDefaultCtor<LoopIdiomRecognizeLegacyPass>), false,
false); Registry.registerPass(*PI, true); return PI; } static
llvm::once_flag InitializeLoopIdiomRecognizeLegacyPassPassFlag
; void llvm::initializeLoopIdiomRecognizeLegacyPassPass(PassRegistry
&Registry) { llvm::call_once(InitializeLoopIdiomRecognizeLegacyPassPassFlag
, initializeLoopIdiomRecognizeLegacyPassPassOnce, std::ref(Registry
)); }
258 "Recognize loop idioms", false, false)PassInfo *PI = new PassInfo( "Recognize loop idioms", "loop-idiom"
, &LoopIdiomRecognizeLegacyPass::ID, PassInfo::NormalCtor_t
(callDefaultCtor<LoopIdiomRecognizeLegacyPass>), false,
false); Registry.registerPass(*PI, true); return PI; } static
llvm::once_flag InitializeLoopIdiomRecognizeLegacyPassPassFlag
; void llvm::initializeLoopIdiomRecognizeLegacyPassPass(PassRegistry
&Registry) { llvm::call_once(InitializeLoopIdiomRecognizeLegacyPassPassFlag
, initializeLoopIdiomRecognizeLegacyPassPassOnce, std::ref(Registry
)); }
259
260Pass *llvm::createLoopIdiomPass() { return new LoopIdiomRecognizeLegacyPass(); }
261
262static void deleteDeadInstruction(Instruction *I) {
263 I->replaceAllUsesWith(UndefValue::get(I->getType()));
264 I->eraseFromParent();
265}
266
267//===----------------------------------------------------------------------===//
268//
269// Implementation of LoopIdiomRecognize
270//
271//===----------------------------------------------------------------------===//
272
273bool LoopIdiomRecognize::runOnLoop(Loop *L) {
274 CurLoop = L;
275 // If the loop could not be converted to canonical form, it must have an
276 // indirectbr in it, just give up.
277 if (!L->getLoopPreheader())
2
Assuming the condition is false
3
Taking false branch
278 return false;
279
280 // Disable loop idiom recognition if the function's name is a common idiom.
281 StringRef Name = L->getHeader()->getParent()->getName();
282 if (Name == "memset" || Name == "memcpy")
4
Assuming the condition is false
5
Assuming the condition is false
6
Taking false branch
283 return false;
284
285 // Determine if code size heuristics need to be applied.
286 ApplyCodeSizeHeuristics =
287 L->getHeader()->getParent()->optForSize() && UseLIRCodeSizeHeurs;
7
Assuming the condition is false
288
289 HasMemset = TLI->has(LibFunc_memset);
290 HasMemsetPattern = TLI->has(LibFunc_memset_pattern16);
291 HasMemcpy = TLI->has(LibFunc_memcpy);
292
293 if (HasMemset || HasMemsetPattern || HasMemcpy)
8
Taking false branch
294 if (SE->hasLoopInvariantBackedgeTakenCount(L))
295 return runOnCountableLoop();
296
297 return runOnNoncountableLoop();
9
Calling 'LoopIdiomRecognize::runOnNoncountableLoop'
298}
299
300bool LoopIdiomRecognize::runOnCountableLoop() {
301 const SCEV *BECount = SE->getBackedgeTakenCount(CurLoop);
302 assert(!isa<SCEVCouldNotCompute>(BECount) &&((!isa<SCEVCouldNotCompute>(BECount) && "runOnCountableLoop() called on a loop without a predictable"
"backedge-taken count") ? static_cast<void> (0) : __assert_fail
("!isa<SCEVCouldNotCompute>(BECount) && \"runOnCountableLoop() called on a loop without a predictable\" \"backedge-taken count\""
, "/build/llvm-toolchain-snapshot-8~svn350071/lib/Transforms/Scalar/LoopIdiomRecognize.cpp"
, 304, __PRETTY_FUNCTION__))
303 "runOnCountableLoop() called on a loop without a predictable"((!isa<SCEVCouldNotCompute>(BECount) && "runOnCountableLoop() called on a loop without a predictable"
"backedge-taken count") ? static_cast<void> (0) : __assert_fail
("!isa<SCEVCouldNotCompute>(BECount) && \"runOnCountableLoop() called on a loop without a predictable\" \"backedge-taken count\""
, "/build/llvm-toolchain-snapshot-8~svn350071/lib/Transforms/Scalar/LoopIdiomRecognize.cpp"
, 304, __PRETTY_FUNCTION__))
304 "backedge-taken count")((!isa<SCEVCouldNotCompute>(BECount) && "runOnCountableLoop() called on a loop without a predictable"
"backedge-taken count") ? static_cast<void> (0) : __assert_fail
("!isa<SCEVCouldNotCompute>(BECount) && \"runOnCountableLoop() called on a loop without a predictable\" \"backedge-taken count\""
, "/build/llvm-toolchain-snapshot-8~svn350071/lib/Transforms/Scalar/LoopIdiomRecognize.cpp"
, 304, __PRETTY_FUNCTION__))
;
305
306 // If this loop executes exactly one time, then it should be peeled, not
307 // optimized by this pass.
308 if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount))
309 if (BECst->getAPInt() == 0)
310 return false;
311
312 SmallVector<BasicBlock *, 8> ExitBlocks;
313 CurLoop->getUniqueExitBlocks(ExitBlocks);
314
315 LLVM_DEBUG(dbgs() << "loop-idiom Scanning: F["do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-idiom")) { dbgs() << "loop-idiom Scanning: F[" <<
CurLoop->getHeader()->getParent()->getName() <<
"] Loop %" << CurLoop->getHeader()->getName() <<
"\n"; } } while (false)
316 << CurLoop->getHeader()->getParent()->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-idiom")) { dbgs() << "loop-idiom Scanning: F[" <<
CurLoop->getHeader()->getParent()->getName() <<
"] Loop %" << CurLoop->getHeader()->getName() <<
"\n"; } } while (false)
317 << "] Loop %" << CurLoop->getHeader()->getName() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-idiom")) { dbgs() << "loop-idiom Scanning: F[" <<
CurLoop->getHeader()->getParent()->getName() <<
"] Loop %" << CurLoop->getHeader()->getName() <<
"\n"; } } while (false)
;
318
319 bool MadeChange = false;
320
321 // The following transforms hoist stores/memsets into the loop pre-header.
322 // Give up if the loop has instructions may throw.
323 SimpleLoopSafetyInfo SafetyInfo;
324 SafetyInfo.computeLoopSafetyInfo(CurLoop);
325 if (SafetyInfo.anyBlockMayThrow())
326 return MadeChange;
327
328 // Scan all the blocks in the loop that are not in subloops.
329 for (auto *BB : CurLoop->getBlocks()) {
330 // Ignore blocks in subloops.
331 if (LI->getLoopFor(BB) != CurLoop)
332 continue;
333
334 MadeChange |= runOnLoopBlock(BB, BECount, ExitBlocks);
335 }
336 return MadeChange;
337}
338
339static APInt getStoreStride(const SCEVAddRecExpr *StoreEv) {
340 const SCEVConstant *ConstStride = cast<SCEVConstant>(StoreEv->getOperand(1));
341 return ConstStride->getAPInt();
342}
343
344/// getMemSetPatternValue - If a strided store of the specified value is safe to
345/// turn into a memset_pattern16, return a ConstantArray of 16 bytes that should
346/// be passed in. Otherwise, return null.
347///
348/// Note that we don't ever attempt to use memset_pattern8 or 4, because these
349/// just replicate their input array and then pass on to memset_pattern16.
350static Constant *getMemSetPatternValue(Value *V, const DataLayout *DL) {
351 // FIXME: This could check for UndefValue because it can be merged into any
352 // other valid pattern.
353
354 // If the value isn't a constant, we can't promote it to being in a constant
355 // array. We could theoretically do a store to an alloca or something, but
356 // that doesn't seem worthwhile.
357 Constant *C = dyn_cast<Constant>(V);
358 if (!C)
359 return nullptr;
360
361 // Only handle simple values that are a power of two bytes in size.
362 uint64_t Size = DL->getTypeSizeInBits(V->getType());
363 if (Size == 0 || (Size & 7) || (Size & (Size - 1)))
364 return nullptr;
365
366 // Don't care enough about darwin/ppc to implement this.
367 if (DL->isBigEndian())
368 return nullptr;
369
370 // Convert to size in bytes.
371 Size /= 8;
372
373 // TODO: If CI is larger than 16-bytes, we can try slicing it in half to see
374 // if the top and bottom are the same (e.g. for vectors and large integers).
375 if (Size > 16)
376 return nullptr;
377
378 // If the constant is exactly 16 bytes, just use it.
379 if (Size == 16)
380 return C;
381
382 // Otherwise, we'll use an array of the constants.
383 unsigned ArraySize = 16 / Size;
384 ArrayType *AT = ArrayType::get(V->getType(), ArraySize);
385 return ConstantArray::get(AT, std::vector<Constant *>(ArraySize, C));
386}
387
388LoopIdiomRecognize::LegalStoreKind
389LoopIdiomRecognize::isLegalStore(StoreInst *SI) {
390 // Don't touch volatile stores.
391 if (SI->isVolatile())
392 return LegalStoreKind::None;
393 // We only want simple or unordered-atomic stores.
394 if (!SI->isUnordered())
395 return LegalStoreKind::None;
396
397 // Don't convert stores of non-integral pointer types to memsets (which stores
398 // integers).
399 if (DL->isNonIntegralPointerType(SI->getValueOperand()->getType()))
400 return LegalStoreKind::None;
401
402 // Avoid merging nontemporal stores.
403 if (SI->getMetadata(LLVMContext::MD_nontemporal))
404 return LegalStoreKind::None;
405
406 Value *StoredVal = SI->getValueOperand();
407 Value *StorePtr = SI->getPointerOperand();
408
409 // Reject stores that are so large that they overflow an unsigned.
410 uint64_t SizeInBits = DL->getTypeSizeInBits(StoredVal->getType());
411 if ((SizeInBits & 7) || (SizeInBits >> 32) != 0)
412 return LegalStoreKind::None;
413
414 // See if the pointer expression is an AddRec like {base,+,1} on the current
415 // loop, which indicates a strided store. If we have something else, it's a
416 // random store we can't handle.
417 const SCEVAddRecExpr *StoreEv =
418 dyn_cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
419 if (!StoreEv || StoreEv->getLoop() != CurLoop || !StoreEv->isAffine())
420 return LegalStoreKind::None;
421
422 // Check to see if we have a constant stride.
423 if (!isa<SCEVConstant>(StoreEv->getOperand(1)))
424 return LegalStoreKind::None;
425
426 // See if the store can be turned into a memset.
427
428 // If the stored value is a byte-wise value (like i32 -1), then it may be
429 // turned into a memset of i8 -1, assuming that all the consecutive bytes
430 // are stored. A store of i32 0x01020304 can never be turned into a memset,
431 // but it can be turned into memset_pattern if the target supports it.
432 Value *SplatValue = isBytewiseValue(StoredVal);
433 Constant *PatternValue = nullptr;
434
435 // Note: memset and memset_pattern on unordered-atomic is yet not supported
436 bool UnorderedAtomic = SI->isUnordered() && !SI->isSimple();
437
438 // If we're allowed to form a memset, and the stored value would be
439 // acceptable for memset, use it.
440 if (!UnorderedAtomic && HasMemset && SplatValue &&
441 // Verify that the stored value is loop invariant. If not, we can't
442 // promote the memset.
443 CurLoop->isLoopInvariant(SplatValue)) {
444 // It looks like we can use SplatValue.
445 return LegalStoreKind::Memset;
446 } else if (!UnorderedAtomic && HasMemsetPattern &&
447 // Don't create memset_pattern16s with address spaces.
448 StorePtr->getType()->getPointerAddressSpace() == 0 &&
449 (PatternValue = getMemSetPatternValue(StoredVal, DL))) {
450 // It looks like we can use PatternValue!
451 return LegalStoreKind::MemsetPattern;
452 }
453
454 // Otherwise, see if the store can be turned into a memcpy.
455 if (HasMemcpy) {
456 // Check to see if the stride matches the size of the store. If so, then we
457 // know that every byte is touched in the loop.
458 APInt Stride = getStoreStride(StoreEv);
459 unsigned StoreSize = DL->getTypeStoreSize(SI->getValueOperand()->getType());
460 if (StoreSize != Stride && StoreSize != -Stride)
461 return LegalStoreKind::None;
462
463 // The store must be feeding a non-volatile load.
464 LoadInst *LI = dyn_cast<LoadInst>(SI->getValueOperand());
465
466 // Only allow non-volatile loads
467 if (!LI || LI->isVolatile())
468 return LegalStoreKind::None;
469 // Only allow simple or unordered-atomic loads
470 if (!LI->isUnordered())
471 return LegalStoreKind::None;
472
473 // See if the pointer expression is an AddRec like {base,+,1} on the current
474 // loop, which indicates a strided load. If we have something else, it's a
475 // random load we can't handle.
476 const SCEVAddRecExpr *LoadEv =
477 dyn_cast<SCEVAddRecExpr>(SE->getSCEV(LI->getPointerOperand()));
478 if (!LoadEv || LoadEv->getLoop() != CurLoop || !LoadEv->isAffine())
479 return LegalStoreKind::None;
480
481 // The store and load must share the same stride.
482 if (StoreEv->getOperand(1) != LoadEv->getOperand(1))
483 return LegalStoreKind::None;
484
485 // Success. This store can be converted into a memcpy.
486 UnorderedAtomic = UnorderedAtomic || LI->isAtomic();
487 return UnorderedAtomic ? LegalStoreKind::UnorderedAtomicMemcpy
488 : LegalStoreKind::Memcpy;
489 }
490 // This store can't be transformed into a memset/memcpy.
491 return LegalStoreKind::None;
492}
493
494void LoopIdiomRecognize::collectStores(BasicBlock *BB) {
495 StoreRefsForMemset.clear();
496 StoreRefsForMemsetPattern.clear();
497 StoreRefsForMemcpy.clear();
498 for (Instruction &I : *BB) {
499 StoreInst *SI = dyn_cast<StoreInst>(&I);
500 if (!SI)
501 continue;
502
503 // Make sure this is a strided store with a constant stride.
504 switch (isLegalStore(SI)) {
505 case LegalStoreKind::None:
506 // Nothing to do
507 break;
508 case LegalStoreKind::Memset: {
509 // Find the base pointer.
510 Value *Ptr = GetUnderlyingObject(SI->getPointerOperand(), *DL);
511 StoreRefsForMemset[Ptr].push_back(SI);
512 } break;
513 case LegalStoreKind::MemsetPattern: {
514 // Find the base pointer.
515 Value *Ptr = GetUnderlyingObject(SI->getPointerOperand(), *DL);
516 StoreRefsForMemsetPattern[Ptr].push_back(SI);
517 } break;
518 case LegalStoreKind::Memcpy:
519 case LegalStoreKind::UnorderedAtomicMemcpy:
520 StoreRefsForMemcpy.push_back(SI);
521 break;
522 default:
523 assert(false && "unhandled return value")((false && "unhandled return value") ? static_cast<
void> (0) : __assert_fail ("false && \"unhandled return value\""
, "/build/llvm-toolchain-snapshot-8~svn350071/lib/Transforms/Scalar/LoopIdiomRecognize.cpp"
, 523, __PRETTY_FUNCTION__))
;
524 break;
525 }
526 }
527}
528
529/// runOnLoopBlock - Process the specified block, which lives in a counted loop
530/// with the specified backedge count. This block is known to be in the current
531/// loop and not in any subloops.
532bool LoopIdiomRecognize::runOnLoopBlock(
533 BasicBlock *BB, const SCEV *BECount,
534 SmallVectorImpl<BasicBlock *> &ExitBlocks) {
535 // We can only promote stores in this block if they are unconditionally
536 // executed in the loop. For a block to be unconditionally executed, it has
537 // to dominate all the exit blocks of the loop. Verify this now.
538 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
539 if (!DT->dominates(BB, ExitBlocks[i]))
540 return false;
541
542 bool MadeChange = false;
543 // Look for store instructions, which may be optimized to memset/memcpy.
544 collectStores(BB);
545
546 // Look for a single store or sets of stores with a common base, which can be
547 // optimized into a memset (memset_pattern). The latter most commonly happens
548 // with structs and handunrolled loops.
549 for (auto &SL : StoreRefsForMemset)
550 MadeChange |= processLoopStores(SL.second, BECount, ForMemset::Yes);
551
552 for (auto &SL : StoreRefsForMemsetPattern)
553 MadeChange |= processLoopStores(SL.second, BECount, ForMemset::No);
554
555 // Optimize the store into a memcpy, if it feeds an similarly strided load.
556 for (auto &SI : StoreRefsForMemcpy)
557 MadeChange |= processLoopStoreOfLoopLoad(SI, BECount);
558
559 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) {
560 Instruction *Inst = &*I++;
561 // Look for memset instructions, which may be optimized to a larger memset.
562 if (MemSetInst *MSI = dyn_cast<MemSetInst>(Inst)) {
563 WeakTrackingVH InstPtr(&*I);
564 if (!processLoopMemSet(MSI, BECount))
565 continue;
566 MadeChange = true;
567
568 // If processing the memset invalidated our iterator, start over from the
569 // top of the block.
570 if (!InstPtr)
571 I = BB->begin();
572 continue;
573 }
574 }
575
576 return MadeChange;
577}
578
579/// See if this store(s) can be promoted to a memset.
580bool LoopIdiomRecognize::processLoopStores(SmallVectorImpl<StoreInst *> &SL,
581 const SCEV *BECount, ForMemset For) {
582 // Try to find consecutive stores that can be transformed into memsets.
583 SetVector<StoreInst *> Heads, Tails;
584 SmallDenseMap<StoreInst *, StoreInst *> ConsecutiveChain;
585
586 // Do a quadratic search on all of the given stores and find
587 // all of the pairs of stores that follow each other.
588 SmallVector<unsigned, 16> IndexQueue;
589 for (unsigned i = 0, e = SL.size(); i < e; ++i) {
590 assert(SL[i]->isSimple() && "Expected only non-volatile stores.")((SL[i]->isSimple() && "Expected only non-volatile stores."
) ? static_cast<void> (0) : __assert_fail ("SL[i]->isSimple() && \"Expected only non-volatile stores.\""
, "/build/llvm-toolchain-snapshot-8~svn350071/lib/Transforms/Scalar/LoopIdiomRecognize.cpp"
, 590, __PRETTY_FUNCTION__))
;
591
592 Value *FirstStoredVal = SL[i]->getValueOperand();
593 Value *FirstStorePtr = SL[i]->getPointerOperand();
594 const SCEVAddRecExpr *FirstStoreEv =
595 cast<SCEVAddRecExpr>(SE->getSCEV(FirstStorePtr));
596 APInt FirstStride = getStoreStride(FirstStoreEv);
597 unsigned FirstStoreSize = DL->getTypeStoreSize(SL[i]->getValueOperand()->getType());
598
599 // See if we can optimize just this store in isolation.
600 if (FirstStride == FirstStoreSize || -FirstStride == FirstStoreSize) {
601 Heads.insert(SL[i]);
602 continue;
603 }
604
605 Value *FirstSplatValue = nullptr;
606 Constant *FirstPatternValue = nullptr;
607
608 if (For == ForMemset::Yes)
609 FirstSplatValue = isBytewiseValue(FirstStoredVal);
610 else
611 FirstPatternValue = getMemSetPatternValue(FirstStoredVal, DL);
612
613 assert((FirstSplatValue || FirstPatternValue) &&(((FirstSplatValue || FirstPatternValue) && "Expected either splat value or pattern value."
) ? static_cast<void> (0) : __assert_fail ("(FirstSplatValue || FirstPatternValue) && \"Expected either splat value or pattern value.\""
, "/build/llvm-toolchain-snapshot-8~svn350071/lib/Transforms/Scalar/LoopIdiomRecognize.cpp"
, 614, __PRETTY_FUNCTION__))
614 "Expected either splat value or pattern value.")(((FirstSplatValue || FirstPatternValue) && "Expected either splat value or pattern value."
) ? static_cast<void> (0) : __assert_fail ("(FirstSplatValue || FirstPatternValue) && \"Expected either splat value or pattern value.\""
, "/build/llvm-toolchain-snapshot-8~svn350071/lib/Transforms/Scalar/LoopIdiomRecognize.cpp"
, 614, __PRETTY_FUNCTION__))
;
615
616 IndexQueue.clear();
617 // If a store has multiple consecutive store candidates, search Stores
618 // array according to the sequence: from i+1 to e, then from i-1 to 0.
619 // This is because usually pairing with immediate succeeding or preceding
620 // candidate create the best chance to find memset opportunity.
621 unsigned j = 0;
622 for (j = i + 1; j < e; ++j)
623 IndexQueue.push_back(j);
624 for (j = i; j > 0; --j)
625 IndexQueue.push_back(j - 1);
626
627 for (auto &k : IndexQueue) {
628 assert(SL[k]->isSimple() && "Expected only non-volatile stores.")((SL[k]->isSimple() && "Expected only non-volatile stores."
) ? static_cast<void> (0) : __assert_fail ("SL[k]->isSimple() && \"Expected only non-volatile stores.\""
, "/build/llvm-toolchain-snapshot-8~svn350071/lib/Transforms/Scalar/LoopIdiomRecognize.cpp"
, 628, __PRETTY_FUNCTION__))
;
629 Value *SecondStorePtr = SL[k]->getPointerOperand();
630 const SCEVAddRecExpr *SecondStoreEv =
631 cast<SCEVAddRecExpr>(SE->getSCEV(SecondStorePtr));
632 APInt SecondStride = getStoreStride(SecondStoreEv);
633
634 if (FirstStride != SecondStride)
635 continue;
636
637 Value *SecondStoredVal = SL[k]->getValueOperand();
638 Value *SecondSplatValue = nullptr;
639 Constant *SecondPatternValue = nullptr;
640
641 if (For == ForMemset::Yes)
642 SecondSplatValue = isBytewiseValue(SecondStoredVal);
643 else
644 SecondPatternValue = getMemSetPatternValue(SecondStoredVal, DL);
645
646 assert((SecondSplatValue || SecondPatternValue) &&(((SecondSplatValue || SecondPatternValue) && "Expected either splat value or pattern value."
) ? static_cast<void> (0) : __assert_fail ("(SecondSplatValue || SecondPatternValue) && \"Expected either splat value or pattern value.\""
, "/build/llvm-toolchain-snapshot-8~svn350071/lib/Transforms/Scalar/LoopIdiomRecognize.cpp"
, 647, __PRETTY_FUNCTION__))
647 "Expected either splat value or pattern value.")(((SecondSplatValue || SecondPatternValue) && "Expected either splat value or pattern value."
) ? static_cast<void> (0) : __assert_fail ("(SecondSplatValue || SecondPatternValue) && \"Expected either splat value or pattern value.\""
, "/build/llvm-toolchain-snapshot-8~svn350071/lib/Transforms/Scalar/LoopIdiomRecognize.cpp"
, 647, __PRETTY_FUNCTION__))
;
648
649 if (isConsecutiveAccess(SL[i], SL[k], *DL, *SE, false)) {
650 if (For == ForMemset::Yes) {
651 if (isa<UndefValue>(FirstSplatValue))
652 FirstSplatValue = SecondSplatValue;
653 if (FirstSplatValue != SecondSplatValue)
654 continue;
655 } else {
656 if (isa<UndefValue>(FirstPatternValue))
657 FirstPatternValue = SecondPatternValue;
658 if (FirstPatternValue != SecondPatternValue)
659 continue;
660 }
661 Tails.insert(SL[k]);
662 Heads.insert(SL[i]);
663 ConsecutiveChain[SL[i]] = SL[k];
664 break;
665 }
666 }
667 }
668
669 // We may run into multiple chains that merge into a single chain. We mark the
670 // stores that we transformed so that we don't visit the same store twice.
671 SmallPtrSet<Value *, 16> TransformedStores;
672 bool Changed = false;
673
674 // For stores that start but don't end a link in the chain:
675 for (SetVector<StoreInst *>::iterator it = Heads.begin(), e = Heads.end();
676 it != e; ++it) {
677 if (Tails.count(*it))
678 continue;
679
680 // We found a store instr that starts a chain. Now follow the chain and try
681 // to transform it.
682 SmallPtrSet<Instruction *, 8> AdjacentStores;
683 StoreInst *I = *it;
684
685 StoreInst *HeadStore = I;
686 unsigned StoreSize = 0;
687
688 // Collect the chain into a list.
689 while (Tails.count(I) || Heads.count(I)) {
690 if (TransformedStores.count(I))
691 break;
692 AdjacentStores.insert(I);
693
694 StoreSize += DL->getTypeStoreSize(I->getValueOperand()->getType());
695 // Move to the next value in the chain.
696 I = ConsecutiveChain[I];
697 }
698
699 Value *StoredVal = HeadStore->getValueOperand();
700 Value *StorePtr = HeadStore->getPointerOperand();
701 const SCEVAddRecExpr *StoreEv = cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
702 APInt Stride = getStoreStride(StoreEv);
703
704 // Check to see if the stride matches the size of the stores. If so, then
705 // we know that every byte is touched in the loop.
706 if (StoreSize != Stride && StoreSize != -Stride)
707 continue;
708
709 bool NegStride = StoreSize == -Stride;
710
711 if (processLoopStridedStore(StorePtr, StoreSize, HeadStore->getAlignment(),
712 StoredVal, HeadStore, AdjacentStores, StoreEv,
713 BECount, NegStride)) {
714 TransformedStores.insert(AdjacentStores.begin(), AdjacentStores.end());
715 Changed = true;
716 }
717 }
718
719 return Changed;
720}
721
722/// processLoopMemSet - See if this memset can be promoted to a large memset.
723bool LoopIdiomRecognize::processLoopMemSet(MemSetInst *MSI,
724 const SCEV *BECount) {
725 // We can only handle non-volatile memsets with a constant size.
726 if (MSI->isVolatile() || !isa<ConstantInt>(MSI->getLength()))
727 return false;
728
729 // If we're not allowed to hack on memset, we fail.
730 if (!HasMemset)
731 return false;
732
733 Value *Pointer = MSI->getDest();
734
735 // See if the pointer expression is an AddRec like {base,+,1} on the current
736 // loop, which indicates a strided store. If we have something else, it's a
737 // random store we can't handle.
738 const SCEVAddRecExpr *Ev = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Pointer));
739 if (!Ev || Ev->getLoop() != CurLoop || !Ev->isAffine())
740 return false;
741
742 // Reject memsets that are so large that they overflow an unsigned.
743 uint64_t SizeInBytes = cast<ConstantInt>(MSI->getLength())->getZExtValue();
744 if ((SizeInBytes >> 32) != 0)
745 return false;
746
747 // Check to see if the stride matches the size of the memset. If so, then we
748 // know that every byte is touched in the loop.
749 const SCEVConstant *ConstStride = dyn_cast<SCEVConstant>(Ev->getOperand(1));
750 if (!ConstStride)
751 return false;
752
753 APInt Stride = ConstStride->getAPInt();
754 if (SizeInBytes != Stride && SizeInBytes != -Stride)
755 return false;
756
757 // Verify that the memset value is loop invariant. If not, we can't promote
758 // the memset.
759 Value *SplatValue = MSI->getValue();
760 if (!SplatValue || !CurLoop->isLoopInvariant(SplatValue))
761 return false;
762
763 SmallPtrSet<Instruction *, 1> MSIs;
764 MSIs.insert(MSI);
765 bool NegStride = SizeInBytes == -Stride;
766 return processLoopStridedStore(Pointer, (unsigned)SizeInBytes,
767 MSI->getDestAlignment(), SplatValue, MSI, MSIs,
768 Ev, BECount, NegStride, /*IsLoopMemset=*/true);
769}
770
771/// mayLoopAccessLocation - Return true if the specified loop might access the
772/// specified pointer location, which is a loop-strided access. The 'Access'
773/// argument specifies what the verboten forms of access are (read or write).
774static bool
775mayLoopAccessLocation(Value *Ptr, ModRefInfo Access, Loop *L,
776 const SCEV *BECount, unsigned StoreSize,
777 AliasAnalysis &AA,
778 SmallPtrSetImpl<Instruction *> &IgnoredStores) {
779 // Get the location that may be stored across the loop. Since the access is
780 // strided positively through memory, we say that the modified location starts
781 // at the pointer and has infinite size.
782 LocationSize AccessSize = LocationSize::unknown();
783
784 // If the loop iterates a fixed number of times, we can refine the access size
785 // to be exactly the size of the memset, which is (BECount+1)*StoreSize
786 if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount))
787 AccessSize = LocationSize::precise((BECst->getValue()->getZExtValue() + 1) *
788 StoreSize);
789
790 // TODO: For this to be really effective, we have to dive into the pointer
791 // operand in the store. Store to &A[i] of 100 will always return may alias
792 // with store of &A[100], we need to StoreLoc to be "A" with size of 100,
793 // which will then no-alias a store to &A[100].
794 MemoryLocation StoreLoc(Ptr, AccessSize);
795
796 for (Loop::block_iterator BI = L->block_begin(), E = L->block_end(); BI != E;
797 ++BI)
798 for (Instruction &I : **BI)
799 if (IgnoredStores.count(&I) == 0 &&
800 isModOrRefSet(
801 intersectModRef(AA.getModRefInfo(&I, StoreLoc), Access)))
802 return true;
803
804 return false;
805}
806
807// If we have a negative stride, Start refers to the end of the memory location
808// we're trying to memset. Therefore, we need to recompute the base pointer,
809// which is just Start - BECount*Size.
810static const SCEV *getStartForNegStride(const SCEV *Start, const SCEV *BECount,
811 Type *IntPtr, unsigned StoreSize,
812 ScalarEvolution *SE) {
813 const SCEV *Index = SE->getTruncateOrZeroExtend(BECount, IntPtr);
814 if (StoreSize != 1)
815 Index = SE->getMulExpr(Index, SE->getConstant(IntPtr, StoreSize),
816 SCEV::FlagNUW);
817 return SE->getMinusSCEV(Start, Index);
818}
819
820/// Compute the number of bytes as a SCEV from the backedge taken count.
821///
822/// This also maps the SCEV into the provided type and tries to handle the
823/// computation in a way that will fold cleanly.
824static const SCEV *getNumBytes(const SCEV *BECount, Type *IntPtr,
825 unsigned StoreSize, Loop *CurLoop,
826 const DataLayout *DL, ScalarEvolution *SE) {
827 const SCEV *NumBytesS;
828 // The # stored bytes is (BECount+1)*Size. Expand the trip count out to
829 // pointer size if it isn't already.
830 //
831 // If we're going to need to zero extend the BE count, check if we can add
832 // one to it prior to zero extending without overflow. Provided this is safe,
833 // it allows better simplification of the +1.
834 if (DL->getTypeSizeInBits(BECount->getType()) <
835 DL->getTypeSizeInBits(IntPtr) &&
836 SE->isLoopEntryGuardedByCond(
837 CurLoop, ICmpInst::ICMP_NE, BECount,
838 SE->getNegativeSCEV(SE->getOne(BECount->getType())))) {
839 NumBytesS = SE->getZeroExtendExpr(
840 SE->getAddExpr(BECount, SE->getOne(BECount->getType()), SCEV::FlagNUW),
841 IntPtr);
842 } else {
843 NumBytesS = SE->getAddExpr(SE->getTruncateOrZeroExtend(BECount, IntPtr),
844 SE->getOne(IntPtr), SCEV::FlagNUW);
845 }
846
847 // And scale it based on the store size.
848 if (StoreSize != 1) {
849 NumBytesS = SE->getMulExpr(NumBytesS, SE->getConstant(IntPtr, StoreSize),
850 SCEV::FlagNUW);
851 }
852 return NumBytesS;
853}
854
855/// processLoopStridedStore - We see a strided store of some value. If we can
856/// transform this into a memset or memset_pattern in the loop preheader, do so.
857bool LoopIdiomRecognize::processLoopStridedStore(
858 Value *DestPtr, unsigned StoreSize, unsigned StoreAlignment,
859 Value *StoredVal, Instruction *TheStore,
860 SmallPtrSetImpl<Instruction *> &Stores, const SCEVAddRecExpr *Ev,
861 const SCEV *BECount, bool NegStride, bool IsLoopMemset) {
862 Value *SplatValue = isBytewiseValue(StoredVal);
863 Constant *PatternValue = nullptr;
864
865 if (!SplatValue)
866 PatternValue = getMemSetPatternValue(StoredVal, DL);
867
868 assert((SplatValue || PatternValue) &&(((SplatValue || PatternValue) && "Expected either splat value or pattern value."
) ? static_cast<void> (0) : __assert_fail ("(SplatValue || PatternValue) && \"Expected either splat value or pattern value.\""
, "/build/llvm-toolchain-snapshot-8~svn350071/lib/Transforms/Scalar/LoopIdiomRecognize.cpp"
, 869, __PRETTY_FUNCTION__))
869 "Expected either splat value or pattern value.")(((SplatValue || PatternValue) && "Expected either splat value or pattern value."
) ? static_cast<void> (0) : __assert_fail ("(SplatValue || PatternValue) && \"Expected either splat value or pattern value.\""
, "/build/llvm-toolchain-snapshot-8~svn350071/lib/Transforms/Scalar/LoopIdiomRecognize.cpp"
, 869, __PRETTY_FUNCTION__))
;
870
871 // The trip count of the loop and the base pointer of the addrec SCEV is
872 // guaranteed to be loop invariant, which means that it should dominate the
873 // header. This allows us to insert code for it in the preheader.
874 unsigned DestAS = DestPtr->getType()->getPointerAddressSpace();
875 BasicBlock *Preheader = CurLoop->getLoopPreheader();
876 IRBuilder<> Builder(Preheader->getTerminator());
877 SCEVExpander Expander(*SE, *DL, "loop-idiom");
878
879 Type *DestInt8PtrTy = Builder.getInt8PtrTy(DestAS);
880 Type *IntPtr = Builder.getIntPtrTy(*DL, DestAS);
881
882 const SCEV *Start = Ev->getStart();
883 // Handle negative strided loops.
884 if (NegStride)
885 Start = getStartForNegStride(Start, BECount, IntPtr, StoreSize, SE);
886
887 // TODO: ideally we should still be able to generate memset if SCEV expander
888 // is taught to generate the dependencies at the latest point.
889 if (!isSafeToExpand(Start, *SE))
890 return false;
891
892 // Okay, we have a strided store "p[i]" of a splattable value. We can turn
893 // this into a memset in the loop preheader now if we want. However, this
894 // would be unsafe to do if there is anything else in the loop that may read
895 // or write to the aliased location. Check for any overlap by generating the
896 // base pointer and checking the region.
897 Value *BasePtr =
898 Expander.expandCodeFor(Start, DestInt8PtrTy, Preheader->getTerminator());
899 if (mayLoopAccessLocation(BasePtr, ModRefInfo::ModRef, CurLoop, BECount,
900 StoreSize, *AA, Stores)) {
901 Expander.clear();
902 // If we generated new code for the base pointer, clean up.
903 RecursivelyDeleteTriviallyDeadInstructions(BasePtr, TLI);
904 return false;
905 }
906
907 if (avoidLIRForMultiBlockLoop(/*IsMemset=*/true, IsLoopMemset))
908 return false;
909
910 // Okay, everything looks good, insert the memset.
911
912 const SCEV *NumBytesS =
913 getNumBytes(BECount, IntPtr, StoreSize, CurLoop, DL, SE);
914
915 // TODO: ideally we should still be able to generate memset if SCEV expander
916 // is taught to generate the dependencies at the latest point.
917 if (!isSafeToExpand(NumBytesS, *SE))
918 return false;
919
920 Value *NumBytes =
921 Expander.expandCodeFor(NumBytesS, IntPtr, Preheader->getTerminator());
922
923 CallInst *NewCall;
924 if (SplatValue) {
925 NewCall =
926 Builder.CreateMemSet(BasePtr, SplatValue, NumBytes, StoreAlignment);
927 } else {
928 // Everything is emitted in default address space
929 Type *Int8PtrTy = DestInt8PtrTy;
930
931 Module *M = TheStore->getModule();
932 StringRef FuncName = "memset_pattern16";
933 Value *MSP =
934 M->getOrInsertFunction(FuncName, Builder.getVoidTy(),
935 Int8PtrTy, Int8PtrTy, IntPtr);
936 inferLibFuncAttributes(M, FuncName, *TLI);
937
938 // Otherwise we should form a memset_pattern16. PatternValue is known to be
939 // an constant array of 16-bytes. Plop the value into a mergable global.
940 GlobalVariable *GV = new GlobalVariable(*M, PatternValue->getType(), true,
941 GlobalValue::PrivateLinkage,
942 PatternValue, ".memset_pattern");
943 GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); // Ok to merge these.
944 GV->setAlignment(16);
945 Value *PatternPtr = ConstantExpr::getBitCast(GV, Int8PtrTy);
946 NewCall = Builder.CreateCall(MSP, {BasePtr, PatternPtr, NumBytes});
947 }
948
949 LLVM_DEBUG(dbgs() << " Formed memset: " << *NewCall << "\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-idiom")) { dbgs() << " Formed memset: " <<
*NewCall << "\n" << " from store to: " <<
*Ev << " at: " << *TheStore << "\n"; } } while
(false)
950 << " from store to: " << *Ev << " at: " << *TheStoredo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-idiom")) { dbgs() << " Formed memset: " <<
*NewCall << "\n" << " from store to: " <<
*Ev << " at: " << *TheStore << "\n"; } } while
(false)
951 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-idiom")) { dbgs() << " Formed memset: " <<
*NewCall << "\n" << " from store to: " <<
*Ev << " at: " << *TheStore << "\n"; } } while
(false)
;
952 NewCall->setDebugLoc(TheStore->getDebugLoc());
953
954 // Okay, the memset has been formed. Zap the original store and anything that
955 // feeds into it.
956 for (auto *I : Stores)
957 deleteDeadInstruction(I);
958 ++NumMemSet;
959 return true;
960}
961
962/// If the stored value is a strided load in the same loop with the same stride
963/// this may be transformable into a memcpy. This kicks in for stuff like
964/// for (i) A[i] = B[i];
965bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(StoreInst *SI,
966 const SCEV *BECount) {
967 assert(SI->isUnordered() && "Expected only non-volatile non-ordered stores.")((SI->isUnordered() && "Expected only non-volatile non-ordered stores."
) ? static_cast<void> (0) : __assert_fail ("SI->isUnordered() && \"Expected only non-volatile non-ordered stores.\""
, "/build/llvm-toolchain-snapshot-8~svn350071/lib/Transforms/Scalar/LoopIdiomRecognize.cpp"
, 967, __PRETTY_FUNCTION__))
;
968
969 Value *StorePtr = SI->getPointerOperand();
970 const SCEVAddRecExpr *StoreEv = cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
971 APInt Stride = getStoreStride(StoreEv);
972 unsigned StoreSize = DL->getTypeStoreSize(SI->getValueOperand()->getType());
973 bool NegStride = StoreSize == -Stride;
974
975 // The store must be feeding a non-volatile load.
976 LoadInst *LI = cast<LoadInst>(SI->getValueOperand());
977 assert(LI->isUnordered() && "Expected only non-volatile non-ordered loads.")((LI->isUnordered() && "Expected only non-volatile non-ordered loads."
) ? static_cast<void> (0) : __assert_fail ("LI->isUnordered() && \"Expected only non-volatile non-ordered loads.\""
, "/build/llvm-toolchain-snapshot-8~svn350071/lib/Transforms/Scalar/LoopIdiomRecognize.cpp"
, 977, __PRETTY_FUNCTION__))
;
978
979 // See if the pointer expression is an AddRec like {base,+,1} on the current
980 // loop, which indicates a strided load. If we have something else, it's a
981 // random load we can't handle.
982 const SCEVAddRecExpr *LoadEv =
983 cast<SCEVAddRecExpr>(SE->getSCEV(LI->getPointerOperand()));
984
985 // The trip count of the loop and the base pointer of the addrec SCEV is
986 // guaranteed to be loop invariant, which means that it should dominate the
987 // header. This allows us to insert code for it in the preheader.
988 BasicBlock *Preheader = CurLoop->getLoopPreheader();
989 IRBuilder<> Builder(Preheader->getTerminator());
990 SCEVExpander Expander(*SE, *DL, "loop-idiom");
991
992 const SCEV *StrStart = StoreEv->getStart();
993 unsigned StrAS = SI->getPointerAddressSpace();
994 Type *IntPtrTy = Builder.getIntPtrTy(*DL, StrAS);
995
996 // Handle negative strided loops.
997 if (NegStride)
998 StrStart = getStartForNegStride(StrStart, BECount, IntPtrTy, StoreSize, SE);
999
1000 // Okay, we have a strided store "p[i]" of a loaded value. We can turn
1001 // this into a memcpy in the loop preheader now if we want. However, this
1002 // would be unsafe to do if there is anything else in the loop that may read
1003 // or write the memory region we're storing to. This includes the load that
1004 // feeds the stores. Check for an alias by generating the base address and
1005 // checking everything.
1006 Value *StoreBasePtr = Expander.expandCodeFor(
1007 StrStart, Builder.getInt8PtrTy(StrAS), Preheader->getTerminator());
1008
1009 SmallPtrSet<Instruction *, 1> Stores;
1010 Stores.insert(SI);
1011 if (mayLoopAccessLocation(StoreBasePtr, ModRefInfo::ModRef, CurLoop, BECount,
1012 StoreSize, *AA, Stores)) {
1013 Expander.clear();
1014 // If we generated new code for the base pointer, clean up.
1015 RecursivelyDeleteTriviallyDeadInstructions(StoreBasePtr, TLI);
1016 return false;
1017 }
1018
1019 const SCEV *LdStart = LoadEv->getStart();
1020 unsigned LdAS = LI->getPointerAddressSpace();
1021
1022 // Handle negative strided loops.
1023 if (NegStride)
1024 LdStart = getStartForNegStride(LdStart, BECount, IntPtrTy, StoreSize, SE);
1025
1026 // For a memcpy, we have to make sure that the input array is not being
1027 // mutated by the loop.
1028 Value *LoadBasePtr = Expander.expandCodeFor(
1029 LdStart, Builder.getInt8PtrTy(LdAS), Preheader->getTerminator());
1030
1031 if (mayLoopAccessLocation(LoadBasePtr, ModRefInfo::Mod, CurLoop, BECount,
1032 StoreSize, *AA, Stores)) {
1033 Expander.clear();
1034 // If we generated new code for the base pointer, clean up.
1035 RecursivelyDeleteTriviallyDeadInstructions(LoadBasePtr, TLI);
1036 RecursivelyDeleteTriviallyDeadInstructions(StoreBasePtr, TLI);
1037 return false;
1038 }
1039
1040 if (avoidLIRForMultiBlockLoop())
1041 return false;
1042
1043 // Okay, everything is safe, we can transform this!
1044
1045 const SCEV *NumBytesS =
1046 getNumBytes(BECount, IntPtrTy, StoreSize, CurLoop, DL, SE);
1047
1048 Value *NumBytes =
1049 Expander.expandCodeFor(NumBytesS, IntPtrTy, Preheader->getTerminator());
1050
1051 CallInst *NewCall = nullptr;
1052 // Check whether to generate an unordered atomic memcpy:
1053 // If the load or store are atomic, then they must necessarily be unordered
1054 // by previous checks.
1055 if (!SI->isAtomic() && !LI->isAtomic())
1056 NewCall = Builder.CreateMemCpy(StoreBasePtr, SI->getAlignment(),
1057 LoadBasePtr, LI->getAlignment(), NumBytes);
1058 else {
1059 // We cannot allow unaligned ops for unordered load/store, so reject
1060 // anything where the alignment isn't at least the element size.
1061 unsigned Align = std::min(SI->getAlignment(), LI->getAlignment());
1062 if (Align < StoreSize)
1063 return false;
1064
1065 // If the element.atomic memcpy is not lowered into explicit
1066 // loads/stores later, then it will be lowered into an element-size
1067 // specific lib call. If the lib call doesn't exist for our store size, then
1068 // we shouldn't generate the memcpy.
1069 if (StoreSize > TTI->getAtomicMemIntrinsicMaxElementSize())
1070 return false;
1071
1072 // Create the call.
1073 // Note that unordered atomic loads/stores are *required* by the spec to
1074 // have an alignment but non-atomic loads/stores may not.
1075 NewCall = Builder.CreateElementUnorderedAtomicMemCpy(
1076 StoreBasePtr, SI->getAlignment(), LoadBasePtr, LI->getAlignment(),
1077 NumBytes, StoreSize);
1078 }
1079 NewCall->setDebugLoc(SI->getDebugLoc());
1080
1081 LLVM_DEBUG(dbgs() << " Formed memcpy: " << *NewCall << "\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-idiom")) { dbgs() << " Formed memcpy: " <<
*NewCall << "\n" << " from load ptr=" <<
*LoadEv << " at: " << *LI << "\n" <<
" from store ptr=" << *StoreEv << " at: " <<
*SI << "\n"; } } while (false)
1082 << " from load ptr=" << *LoadEv << " at: " << *LI << "\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-idiom")) { dbgs() << " Formed memcpy: " <<
*NewCall << "\n" << " from load ptr=" <<
*LoadEv << " at: " << *LI << "\n" <<
" from store ptr=" << *StoreEv << " at: " <<
*SI << "\n"; } } while (false)
1083 << " from store ptr=" << *StoreEv << " at: " << *SIdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-idiom")) { dbgs() << " Formed memcpy: " <<
*NewCall << "\n" << " from load ptr=" <<
*LoadEv << " at: " << *LI << "\n" <<
" from store ptr=" << *StoreEv << " at: " <<
*SI << "\n"; } } while (false)
1084 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-idiom")) { dbgs() << " Formed memcpy: " <<
*NewCall << "\n" << " from load ptr=" <<
*LoadEv << " at: " << *LI << "\n" <<
" from store ptr=" << *StoreEv << " at: " <<
*SI << "\n"; } } while (false)
;
1085
1086 // Okay, the memcpy has been formed. Zap the original store and anything that
1087 // feeds into it.
1088 deleteDeadInstruction(SI);
1089 ++NumMemCpy;
1090 return true;
1091}
1092
1093// When compiling for codesize we avoid idiom recognition for a multi-block loop
1094// unless it is a loop_memset idiom or a memset/memcpy idiom in a nested loop.
1095//
1096bool LoopIdiomRecognize::avoidLIRForMultiBlockLoop(bool IsMemset,
1097 bool IsLoopMemset) {
1098 if (ApplyCodeSizeHeuristics && CurLoop->getNumBlocks() > 1) {
1099 if (!CurLoop->getParentLoop() && (!IsMemset || !IsLoopMemset)) {
1100 LLVM_DEBUG(dbgs() << " " << CurLoop->getHeader()->getParent()->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-idiom")) { dbgs() << " " << CurLoop->getHeader
()->getParent()->getName() << " : LIR " << (
IsMemset ? "Memset" : "Memcpy") << " avoided: multi-block top-level loop\n"
; } } while (false)
1101 << " : LIR " << (IsMemset ? "Memset" : "Memcpy")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-idiom")) { dbgs() << " " << CurLoop->getHeader
()->getParent()->getName() << " : LIR " << (
IsMemset ? "Memset" : "Memcpy") << " avoided: multi-block top-level loop\n"
; } } while (false)
1102 << " avoided: multi-block top-level loop\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-idiom")) { dbgs() << " " << CurLoop->getHeader
()->getParent()->getName() << " : LIR " << (
IsMemset ? "Memset" : "Memcpy") << " avoided: multi-block top-level loop\n"
; } } while (false)
;
1103 return true;
1104 }
1105 }
1106
1107 return false;
1108}
1109
1110bool LoopIdiomRecognize::runOnNoncountableLoop() {
1111 return recognizePopcount() || recognizeAndInsertCTLZ();
10
Calling 'LoopIdiomRecognize::recognizeAndInsertCTLZ'
1112}
1113
1114/// Check if the given conditional branch is based on the comparison between
1115/// a variable and zero, and if the variable is non-zero, the control yields to
1116/// the loop entry. If the branch matches the behavior, the variable involved
1117/// in the comparison is returned. This function will be called to see if the
1118/// precondition and postcondition of the loop are in desirable form.
1119static Value *matchCondition(BranchInst *BI, BasicBlock *LoopEntry) {
1120 if (!BI || !BI->isConditional())
1121 return nullptr;
1122
1123 ICmpInst *Cond = dyn_cast<ICmpInst>(BI->getCondition());
1124 if (!Cond)
1125 return nullptr;
1126
1127 ConstantInt *CmpZero = dyn_cast<ConstantInt>(Cond->getOperand(1));
1128 if (!CmpZero || !CmpZero->isZero())
1129 return nullptr;
1130
1131 ICmpInst::Predicate Pred = Cond->getPredicate();
1132 if ((Pred == ICmpInst::ICMP_NE && BI->getSuccessor(0) == LoopEntry) ||
1133 (Pred == ICmpInst::ICMP_EQ && BI->getSuccessor(1) == LoopEntry))
1134 return Cond->getOperand(0);
1135
1136 return nullptr;
1137}
1138
1139// Check if the recurrence variable `VarX` is in the right form to create
1140// the idiom. Returns the value coerced to a PHINode if so.
1141static PHINode *getRecurrenceVar(Value *VarX, Instruction *DefX,
1142 BasicBlock *LoopEntry) {
1143 auto *PhiX = dyn_cast<PHINode>(VarX);
1144 if (PhiX && PhiX->getParent() == LoopEntry &&
1145 (PhiX->getOperand(0) == DefX || PhiX->getOperand(1) == DefX))
1146 return PhiX;
1147 return nullptr;
1148}
1149
1150/// Return true iff the idiom is detected in the loop.
1151///
1152/// Additionally:
1153/// 1) \p CntInst is set to the instruction counting the population bit.
1154/// 2) \p CntPhi is set to the corresponding phi node.
1155/// 3) \p Var is set to the value whose population bits are being counted.
1156///
1157/// The core idiom we are trying to detect is:
1158/// \code
1159/// if (x0 != 0)
1160/// goto loop-exit // the precondition of the loop
1161/// cnt0 = init-val;
1162/// do {
1163/// x1 = phi (x0, x2);
1164/// cnt1 = phi(cnt0, cnt2);
1165///
1166/// cnt2 = cnt1 + 1;
1167/// ...
1168/// x2 = x1 & (x1 - 1);
1169/// ...
1170/// } while(x != 0);
1171///
1172/// loop-exit:
1173/// \endcode
1174static bool detectPopcountIdiom(Loop *CurLoop, BasicBlock *PreCondBB,
1175 Instruction *&CntInst, PHINode *&CntPhi,
1176 Value *&Var) {
1177 // step 1: Check to see if the look-back branch match this pattern:
1178 // "if (a!=0) goto loop-entry".
1179 BasicBlock *LoopEntry;
1180 Instruction *DefX2, *CountInst;
1181 Value *VarX1, *VarX0;
1182 PHINode *PhiX, *CountPhi;
1183
1184 DefX2 = CountInst = nullptr;
1185 VarX1 = VarX0 = nullptr;
1186 PhiX = CountPhi = nullptr;
1187 LoopEntry = *(CurLoop->block_begin());
1188
1189 // step 1: Check if the loop-back branch is in desirable form.
1190 {
1191 if (Value *T = matchCondition(
1192 dyn_cast<BranchInst>(LoopEntry->getTerminator()), LoopEntry))
1193 DefX2 = dyn_cast<Instruction>(T);
1194 else
1195 return false;
1196 }
1197
1198 // step 2: detect instructions corresponding to "x2 = x1 & (x1 - 1)"
1199 {
1200 if (!DefX2 || DefX2->getOpcode() != Instruction::And)
1201 return false;
1202
1203 BinaryOperator *SubOneOp;
1204
1205 if ((SubOneOp = dyn_cast<BinaryOperator>(DefX2->getOperand(0))))
1206 VarX1 = DefX2->getOperand(1);
1207 else {
1208 VarX1 = DefX2->getOperand(0);
1209 SubOneOp = dyn_cast<BinaryOperator>(DefX2->getOperand(1));
1210 }
1211 if (!SubOneOp || SubOneOp->getOperand(0) != VarX1)
1212 return false;
1213
1214 ConstantInt *Dec = dyn_cast<ConstantInt>(SubOneOp->getOperand(1));
1215 if (!Dec ||
1216 !((SubOneOp->getOpcode() == Instruction::Sub && Dec->isOne()) ||
1217 (SubOneOp->getOpcode() == Instruction::Add &&
1218 Dec->isMinusOne()))) {
1219 return false;
1220 }
1221 }
1222
1223 // step 3: Check the recurrence of variable X
1224 PhiX = getRecurrenceVar(VarX1, DefX2, LoopEntry);
1225 if (!PhiX)
1226 return false;
1227
1228 // step 4: Find the instruction which count the population: cnt2 = cnt1 + 1
1229 {
1230 CountInst = nullptr;
1231 for (BasicBlock::iterator Iter = LoopEntry->getFirstNonPHI()->getIterator(),
1232 IterE = LoopEntry->end();
1233 Iter != IterE; Iter++) {
1234 Instruction *Inst = &*Iter;
1235 if (Inst->getOpcode() != Instruction::Add)
1236 continue;
1237
1238 ConstantInt *Inc = dyn_cast<ConstantInt>(Inst->getOperand(1));
1239 if (!Inc || !Inc->isOne())
1240 continue;
1241
1242 PHINode *Phi = getRecurrenceVar(Inst->getOperand(0), Inst, LoopEntry);
1243 if (!Phi)
1244 continue;
1245
1246 // Check if the result of the instruction is live of the loop.
1247 bool LiveOutLoop = false;
1248 for (User *U : Inst->users()) {
1249 if ((cast<Instruction>(U))->getParent() != LoopEntry) {
1250 LiveOutLoop = true;
1251 break;
1252 }
1253 }
1254
1255 if (LiveOutLoop) {
1256 CountInst = Inst;
1257 CountPhi = Phi;
1258 break;
1259 }
1260 }
1261
1262 if (!CountInst)
1263 return false;
1264 }
1265
1266 // step 5: check if the precondition is in this form:
1267 // "if (x != 0) goto loop-head ; else goto somewhere-we-don't-care;"
1268 {
1269 auto *PreCondBr = dyn_cast<BranchInst>(PreCondBB->getTerminator());
1270 Value *T = matchCondition(PreCondBr, CurLoop->getLoopPreheader());
1271 if (T != PhiX->getOperand(0) && T != PhiX->getOperand(1))
1272 return false;
1273
1274 CntInst = CountInst;
1275 CntPhi = CountPhi;
1276 Var = T;
1277 }
1278
1279 return true;
1280}
1281
1282/// Return true if the idiom is detected in the loop.
1283///
1284/// Additionally:
1285/// 1) \p CntInst is set to the instruction Counting Leading Zeros (CTLZ)
1286/// or nullptr if there is no such.
1287/// 2) \p CntPhi is set to the corresponding phi node
1288/// or nullptr if there is no such.
1289/// 3) \p Var is set to the value whose CTLZ could be used.
1290/// 4) \p DefX is set to the instruction calculating Loop exit condition.
1291///
1292/// The core idiom we are trying to detect is:
1293/// \code
1294/// if (x0 == 0)
1295/// goto loop-exit // the precondition of the loop
1296/// cnt0 = init-val;
1297/// do {
1298/// x = phi (x0, x.next); //PhiX
1299/// cnt = phi(cnt0, cnt.next);
1300///
1301/// cnt.next = cnt + 1;
1302/// ...
1303/// x.next = x >> 1; // DefX
1304/// ...
1305/// } while(x.next != 0);
1306///
1307/// loop-exit:
1308/// \endcode
1309static bool detectCTLZIdiom(Loop *CurLoop, PHINode *&PhiX,
1310 Instruction *&CntInst, PHINode *&CntPhi,
1311 Instruction *&DefX) {
1312 BasicBlock *LoopEntry;
1313 Value *VarX = nullptr;
1314
1315 DefX = nullptr;
1316 PhiX = nullptr;
1317 CntInst = nullptr;
1318 CntPhi = nullptr;
1319 LoopEntry = *(CurLoop->block_begin());
1320
1321 // step 1: Check if the loop-back branch is in desirable form.
1322 if (Value *T = matchCondition(
1323 dyn_cast<BranchInst>(LoopEntry->getTerminator()), LoopEntry))
1324 DefX = dyn_cast<Instruction>(T);
1325 else
1326 return false;
1327
1328 // step 2: detect instructions corresponding to "x.next = x >> 1"
1329 if (!DefX || (DefX->getOpcode() != Instruction::AShr &&
1330 DefX->getOpcode() != Instruction::LShr))
1331 return false;
1332 ConstantInt *Shft = dyn_cast<ConstantInt>(DefX->getOperand(1));
1333 if (!Shft || !Shft->isOne())
1334 return false;
1335 VarX = DefX->getOperand(0);
1336
1337 // step 3: Check the recurrence of variable X
1338 PhiX = getRecurrenceVar(VarX, DefX, LoopEntry);
1339 if (!PhiX)
1340 return false;
1341
1342 // step 4: Find the instruction which count the CTLZ: cnt.next = cnt + 1
1343 // TODO: We can skip the step. If loop trip count is known (CTLZ),
1344 // then all uses of "cnt.next" could be optimized to the trip count
1345 // plus "cnt0". Currently it is not optimized.
1346 // This step could be used to detect POPCNT instruction:
1347 // cnt.next = cnt + (x.next & 1)
1348 for (BasicBlock::iterator Iter = LoopEntry->getFirstNonPHI()->getIterator(),
1349 IterE = LoopEntry->end();
1350 Iter != IterE; Iter++) {
1351 Instruction *Inst = &*Iter;
1352 if (Inst->getOpcode() != Instruction::Add)
1353 continue;
1354
1355 ConstantInt *Inc = dyn_cast<ConstantInt>(Inst->getOperand(1));
1356 if (!Inc || !Inc->isOne())
1357 continue;
1358
1359 PHINode *Phi = getRecurrenceVar(Inst->getOperand(0), Inst, LoopEntry);
1360 if (!Phi)
1361 continue;
1362
1363 CntInst = Inst;
1364 CntPhi = Phi;
1365 break;
1366 }
1367 if (!CntInst)
1368 return false;
1369
1370 return true;
1371}
1372
1373/// Recognize CTLZ idiom in a non-countable loop and convert the loop
1374/// to countable (with CTLZ trip count).
1375/// If CTLZ inserted as a new trip count returns true; otherwise, returns false.
1376bool LoopIdiomRecognize::recognizeAndInsertCTLZ() {
1377 // Give up if the loop has multiple blocks or multiple backedges.
1378 if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1)
11
Assuming the condition is false
12
Assuming the condition is false
13
Taking false branch
1379 return false;
1380
1381 Instruction *CntInst, *DefX;
1382 PHINode *CntPhi, *PhiX;
1383 if (!detectCTLZIdiom(CurLoop, PhiX, CntInst, CntPhi, DefX))
14
Taking false branch
1384 return false;
1385
1386 bool IsCntPhiUsedOutsideLoop = false;
1387 for (User *U : CntPhi->users())
1388 if (!CurLoop->contains(cast<Instruction>(U))) {
1389 IsCntPhiUsedOutsideLoop = true;
1390 break;
1391 }
1392 bool IsCntInstUsedOutsideLoop = false;
1393 for (User *U : CntInst->users())
1394 if (!CurLoop->contains(cast<Instruction>(U))) {
1395 IsCntInstUsedOutsideLoop = true;
1396 break;
1397 }
1398 // If both CntInst and CntPhi are used outside the loop the profitability
1399 // is questionable.
1400 if (IsCntInstUsedOutsideLoop && IsCntPhiUsedOutsideLoop)
1401 return false;
1402
1403 // For some CPUs result of CTLZ(X) intrinsic is undefined
1404 // when X is 0. If we can not guarantee X != 0, we need to check this
1405 // when expand.
1406 bool ZeroCheck = false;
1407 // It is safe to assume Preheader exist as it was checked in
1408 // parent function RunOnLoop.
1409 BasicBlock *PH = CurLoop->getLoopPreheader();
1410 Value *InitX = PhiX->getIncomingValueForBlock(PH);
15
Calling 'PHINode::getIncomingValueForBlock'
24
Returning from 'PHINode::getIncomingValueForBlock'
25
'InitX' initialized here
1411
1412 // Make sure the initial value can't be negative otherwise the ashr in the
1413 // loop might never reach zero which would make the loop infinite.
1414 if (DefX->getOpcode() == Instruction::AShr && !isKnownNonNegative(InitX, *DL))
26
Assuming the condition is false
1415 return false;
1416
1417 // If we are using the count instruction outside the loop, make sure we
1418 // have a zero check as a precondition. Without the check the loop would run
1419 // one iteration for before any check of the input value. This means 0 and 1
1420 // would have identical behavior in the original loop and thus
1421 if (!IsCntPhiUsedOutsideLoop) {
27
Taking true branch
1422 auto *PreCondBB = PH->getSinglePredecessor();
1423 if (!PreCondBB)
28
Assuming 'PreCondBB' is non-null
29
Taking false branch
1424 return false;
1425 auto *PreCondBI = dyn_cast<BranchInst>(PreCondBB->getTerminator());
1426 if (!PreCondBI)
30
Assuming 'PreCondBI' is non-null
31
Taking false branch
1427 return false;
1428 if (matchCondition(PreCondBI, PH) != InitX)
32
Assuming the condition is false
33
Assuming pointer value is null
34
Taking false branch
1429 return false;
1430 ZeroCheck = true;
1431 }
1432
1433 // Check if CTLZ intrinsic is profitable. Assume it is always profitable
1434 // if we delete the loop (the loop has only 6 instructions):
1435 // %n.addr.0 = phi [ %n, %entry ], [ %shr, %while.cond ]
1436 // %i.0 = phi [ %i0, %entry ], [ %inc, %while.cond ]
1437 // %shr = ashr %n.addr.0, 1
1438 // %tobool = icmp eq %shr, 0
1439 // %inc = add nsw %i.0, 1
1440 // br i1 %tobool
1441
1442 const Value *Args[] =
1443 {InitX, ZeroCheck ? ConstantInt::getTrue(InitX->getContext())
35
'?' condition is true
36
Called C++ object pointer is null
1444 : ConstantInt::getFalse(InitX->getContext())};
1445 if (CurLoop->getHeader()->size() != 6 &&
1446 TTI->getIntrinsicCost(Intrinsic::ctlz, InitX->getType(), Args) >
1447 TargetTransformInfo::TCC_Basic)
1448 return false;
1449
1450 transformLoopToCountable(PH, CntInst, CntPhi, InitX, DefX,
1451 DefX->getDebugLoc(), ZeroCheck,
1452 IsCntPhiUsedOutsideLoop);
1453 return true;
1454}
1455
1456/// Recognizes a population count idiom in a non-countable loop.
1457///
1458/// If detected, transforms the relevant code to issue the popcount intrinsic
1459/// function call, and returns true; otherwise, returns false.
1460bool LoopIdiomRecognize::recognizePopcount() {
1461 if (TTI->getPopcntSupport(32) != TargetTransformInfo::PSK_FastHardware)
1462 return false;
1463
1464 // Counting population are usually conducted by few arithmetic instructions.
1465 // Such instructions can be easily "absorbed" by vacant slots in a
1466 // non-compact loop. Therefore, recognizing popcount idiom only makes sense
1467 // in a compact loop.
1468
1469 // Give up if the loop has multiple blocks or multiple backedges.
1470 if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1)
1471 return false;
1472
1473 BasicBlock *LoopBody = *(CurLoop->block_begin());
1474 if (LoopBody->size() >= 20) {
1475 // The loop is too big, bail out.
1476 return false;
1477 }
1478
1479 // It should have a preheader containing nothing but an unconditional branch.
1480 BasicBlock *PH = CurLoop->getLoopPreheader();
1481 if (!PH || &PH->front() != PH->getTerminator())
1482 return false;
1483 auto *EntryBI = dyn_cast<BranchInst>(PH->getTerminator());
1484 if (!EntryBI || EntryBI->isConditional())
1485 return false;
1486
1487 // It should have a precondition block where the generated popcount intrinsic
1488 // function can be inserted.
1489 auto *PreCondBB = PH->getSinglePredecessor();
1490 if (!PreCondBB)
1491 return false;
1492 auto *PreCondBI = dyn_cast<BranchInst>(PreCondBB->getTerminator());
1493 if (!PreCondBI || PreCondBI->isUnconditional())
1494 return false;
1495
1496 Instruction *CntInst;
1497 PHINode *CntPhi;
1498 Value *Val;
1499 if (!detectPopcountIdiom(CurLoop, PreCondBB, CntInst, CntPhi, Val))
1500 return false;
1501
1502 transformLoopToPopcount(PreCondBB, CntInst, CntPhi, Val);
1503 return true;
1504}
1505
1506static CallInst *createPopcntIntrinsic(IRBuilder<> &IRBuilder, Value *Val,
1507 const DebugLoc &DL) {
1508 Value *Ops[] = {Val};
1509 Type *Tys[] = {Val->getType()};
1510
1511 Module *M = IRBuilder.GetInsertBlock()->getParent()->getParent();
1512 Value *Func = Intrinsic::getDeclaration(M, Intrinsic::ctpop, Tys);
1513 CallInst *CI = IRBuilder.CreateCall(Func, Ops);
1514 CI->setDebugLoc(DL);
1515
1516 return CI;
1517}
1518
1519static CallInst *createCTLZIntrinsic(IRBuilder<> &IRBuilder, Value *Val,
1520 const DebugLoc &DL, bool ZeroCheck) {
1521 Value *Ops[] = {Val, ZeroCheck ? IRBuilder.getTrue() : IRBuilder.getFalse()};
1522 Type *Tys[] = {Val->getType()};
1523
1524 Module *M = IRBuilder.GetInsertBlock()->getParent()->getParent();
1525 Value *Func = Intrinsic::getDeclaration(M, Intrinsic::ctlz, Tys);
1526 CallInst *CI = IRBuilder.CreateCall(Func, Ops);
1527 CI->setDebugLoc(DL);
1528
1529 return CI;
1530}
1531
1532/// Transform the following loop:
1533/// loop:
1534/// CntPhi = PHI [Cnt0, CntInst]
1535/// PhiX = PHI [InitX, DefX]
1536/// CntInst = CntPhi + 1
1537/// DefX = PhiX >> 1
1538/// LOOP_BODY
1539/// Br: loop if (DefX != 0)
1540/// Use(CntPhi) or Use(CntInst)
1541///
1542/// Into:
1543/// If CntPhi used outside the loop:
1544/// CountPrev = BitWidth(InitX) - CTLZ(InitX >> 1)
1545/// Count = CountPrev + 1
1546/// else
1547/// Count = BitWidth(InitX) - CTLZ(InitX)
1548/// loop:
1549/// CntPhi = PHI [Cnt0, CntInst]
1550/// PhiX = PHI [InitX, DefX]
1551/// PhiCount = PHI [Count, Dec]
1552/// CntInst = CntPhi + 1
1553/// DefX = PhiX >> 1
1554/// Dec = PhiCount - 1
1555/// LOOP_BODY
1556/// Br: loop if (Dec != 0)
1557/// Use(CountPrev + Cnt0) // Use(CntPhi)
1558/// or
1559/// Use(Count + Cnt0) // Use(CntInst)
1560///
1561/// If LOOP_BODY is empty the loop will be deleted.
1562/// If CntInst and DefX are not used in LOOP_BODY they will be removed.
1563void LoopIdiomRecognize::transformLoopToCountable(
1564 BasicBlock *Preheader, Instruction *CntInst, PHINode *CntPhi, Value *InitX,
1565 Instruction *DefX, const DebugLoc &DL, bool ZeroCheck,
1566 bool IsCntPhiUsedOutsideLoop) {
1567 BranchInst *PreheaderBr = cast<BranchInst>(Preheader->getTerminator());
1568
1569 // Step 1: Insert the CTLZ instruction at the end of the preheader block
1570 // Count = BitWidth - CTLZ(InitX);
1571 // If there are uses of CntPhi create:
1572 // CountPrev = BitWidth - CTLZ(InitX >> 1);
1573 IRBuilder<> Builder(PreheaderBr);
1574 Builder.SetCurrentDebugLocation(DL);
1575 Value *CTLZ, *Count, *CountPrev, *NewCount, *InitXNext;
1576
1577 if (IsCntPhiUsedOutsideLoop) {
1578 if (DefX->getOpcode() == Instruction::AShr)
1579 InitXNext =
1580 Builder.CreateAShr(InitX, ConstantInt::get(InitX->getType(), 1));
1581 else if (DefX->getOpcode() == Instruction::LShr)
1582 InitXNext =
1583 Builder.CreateLShr(InitX, ConstantInt::get(InitX->getType(), 1));
1584 else
1585 llvm_unreachable("Unexpected opcode!")::llvm::llvm_unreachable_internal("Unexpected opcode!", "/build/llvm-toolchain-snapshot-8~svn350071/lib/Transforms/Scalar/LoopIdiomRecognize.cpp"
, 1585)
;
1586 } else
1587 InitXNext = InitX;
1588 CTLZ = createCTLZIntrinsic(Builder, InitXNext, DL, ZeroCheck);
1589 Count = Builder.CreateSub(
1590 ConstantInt::get(CTLZ->getType(),
1591 CTLZ->getType()->getIntegerBitWidth()),
1592 CTLZ);
1593 if (IsCntPhiUsedOutsideLoop) {
1594 CountPrev = Count;
1595 Count = Builder.CreateAdd(
1596 CountPrev,
1597 ConstantInt::get(CountPrev->getType(), 1));
1598 }
1599 if (IsCntPhiUsedOutsideLoop)
1600 NewCount = Builder.CreateZExtOrTrunc(CountPrev,
1601 cast<IntegerType>(CntInst->getType()));
1602 else
1603 NewCount = Builder.CreateZExtOrTrunc(Count,
1604 cast<IntegerType>(CntInst->getType()));
1605
1606 // If the CTLZ counter's initial value is not zero, insert Add Inst.
1607 Value *CntInitVal = CntPhi->getIncomingValueForBlock(Preheader);
1608 ConstantInt *InitConst = dyn_cast<ConstantInt>(CntInitVal);
1609 if (!InitConst || !InitConst->isZero())
1610 NewCount = Builder.CreateAdd(NewCount, CntInitVal);
1611
1612 // Step 2: Insert new IV and loop condition:
1613 // loop:
1614 // ...
1615 // PhiCount = PHI [Count, Dec]
1616 // ...
1617 // Dec = PhiCount - 1
1618 // ...
1619 // Br: loop if (Dec != 0)
1620 BasicBlock *Body = *(CurLoop->block_begin());
1621 auto *LbBr = cast<BranchInst>(Body->getTerminator());
1622 ICmpInst *LbCond = cast<ICmpInst>(LbBr->getCondition());
1623 Type *Ty = Count->getType();
1624
1625 PHINode *TcPhi = PHINode::Create(Ty, 2, "tcphi", &Body->front());
1626
1627 Builder.SetInsertPoint(LbCond);
1628 Instruction *TcDec = cast<Instruction>(
1629 Builder.CreateSub(TcPhi, ConstantInt::get(Ty, 1),
1630 "tcdec", false, true));
1631
1632 TcPhi->addIncoming(Count, Preheader);
1633 TcPhi->addIncoming(TcDec, Body);
1634
1635 CmpInst::Predicate Pred =
1636 (LbBr->getSuccessor(0) == Body) ? CmpInst::ICMP_NE : CmpInst::ICMP_EQ;
1637 LbCond->setPredicate(Pred);
1638 LbCond->setOperand(0, TcDec);
1639 LbCond->setOperand(1, ConstantInt::get(Ty, 0));
1640
1641 // Step 3: All the references to the original counter outside
1642 // the loop are replaced with the NewCount -- the value returned from
1643 // __builtin_ctlz(x).
1644 if (IsCntPhiUsedOutsideLoop)
1645 CntPhi->replaceUsesOutsideBlock(NewCount, Body);
1646 else
1647 CntInst->replaceUsesOutsideBlock(NewCount, Body);
1648
1649 // step 4: Forget the "non-computable" trip-count SCEV associated with the
1650 // loop. The loop would otherwise not be deleted even if it becomes empty.
1651 SE->forgetLoop(CurLoop);
1652}
1653
1654void LoopIdiomRecognize::transformLoopToPopcount(BasicBlock *PreCondBB,
1655 Instruction *CntInst,
1656 PHINode *CntPhi, Value *Var) {
1657 BasicBlock *PreHead = CurLoop->getLoopPreheader();
1658 auto *PreCondBr = cast<BranchInst>(PreCondBB->getTerminator());
1659 const DebugLoc &DL = CntInst->getDebugLoc();
1660
1661 // Assuming before transformation, the loop is following:
1662 // if (x) // the precondition
1663 // do { cnt++; x &= x - 1; } while(x);
1664
1665 // Step 1: Insert the ctpop instruction at the end of the precondition block
1666 IRBuilder<> Builder(PreCondBr);
1667 Value *PopCnt, *PopCntZext, *NewCount, *TripCnt;
1668 {
1669 PopCnt = createPopcntIntrinsic(Builder, Var, DL);
1670 NewCount = PopCntZext =
1671 Builder.CreateZExtOrTrunc(PopCnt, cast<IntegerType>(CntPhi->getType()));
1672
1673 if (NewCount != PopCnt)
1674 (cast<Instruction>(NewCount))->setDebugLoc(DL);
1675
1676 // TripCnt is exactly the number of iterations the loop has
1677 TripCnt = NewCount;
1678
1679 // If the population counter's initial value is not zero, insert Add Inst.
1680 Value *CntInitVal = CntPhi->getIncomingValueForBlock(PreHead);
1681 ConstantInt *InitConst = dyn_cast<ConstantInt>(CntInitVal);
1682 if (!InitConst || !InitConst->isZero()) {
1683 NewCount = Builder.CreateAdd(NewCount, CntInitVal);
1684 (cast<Instruction>(NewCount))->setDebugLoc(DL);
1685 }
1686 }
1687
1688 // Step 2: Replace the precondition from "if (x == 0) goto loop-exit" to
1689 // "if (NewCount == 0) loop-exit". Without this change, the intrinsic
1690 // function would be partial dead code, and downstream passes will drag
1691 // it back from the precondition block to the preheader.
1692 {
1693 ICmpInst *PreCond = cast<ICmpInst>(PreCondBr->getCondition());
1694
1695 Value *Opnd0 = PopCntZext;
1696 Value *Opnd1 = ConstantInt::get(PopCntZext->getType(), 0);
1697 if (PreCond->getOperand(0) != Var)
1698 std::swap(Opnd0, Opnd1);
1699
1700 ICmpInst *NewPreCond = cast<ICmpInst>(
1701 Builder.CreateICmp(PreCond->getPredicate(), Opnd0, Opnd1));
1702 PreCondBr->setCondition(NewPreCond);
1703
1704 RecursivelyDeleteTriviallyDeadInstructions(PreCond, TLI);
1705 }
1706
1707 // Step 3: Note that the population count is exactly the trip count of the
1708 // loop in question, which enable us to convert the loop from noncountable
1709 // loop into a countable one. The benefit is twofold:
1710 //
1711 // - If the loop only counts population, the entire loop becomes dead after
1712 // the transformation. It is a lot easier to prove a countable loop dead
1713 // than to prove a noncountable one. (In some C dialects, an infinite loop
1714 // isn't dead even if it computes nothing useful. In general, DCE needs
1715 // to prove a noncountable loop finite before safely delete it.)
1716 //
1717 // - If the loop also performs something else, it remains alive.
1718 // Since it is transformed to countable form, it can be aggressively
1719 // optimized by some optimizations which are in general not applicable
1720 // to a noncountable loop.
1721 //
1722 // After this step, this loop (conceptually) would look like following:
1723 // newcnt = __builtin_ctpop(x);
1724 // t = newcnt;
1725 // if (x)
1726 // do { cnt++; x &= x-1; t--) } while (t > 0);
1727 BasicBlock *Body = *(CurLoop->block_begin());
1728 {
1729 auto *LbBr = cast<BranchInst>(Body->getTerminator());
1730 ICmpInst *LbCond = cast<ICmpInst>(LbBr->getCondition());
1731 Type *Ty = TripCnt->getType();
1732
1733 PHINode *TcPhi = PHINode::Create(Ty, 2, "tcphi", &Body->front());
1734
1735 Builder.SetInsertPoint(LbCond);
1736 Instruction *TcDec = cast<Instruction>(
1737 Builder.CreateSub(TcPhi, ConstantInt::get(Ty, 1),
1738 "tcdec", false, true));
1739
1740 TcPhi->addIncoming(TripCnt, PreHead);
1741 TcPhi->addIncoming(TcDec, Body);
1742
1743 CmpInst::Predicate Pred =
1744 (LbBr->getSuccessor(0) == Body) ? CmpInst::ICMP_UGT : CmpInst::ICMP_SLE;
1745 LbCond->setPredicate(Pred);
1746 LbCond->setOperand(0, TcDec);
1747 LbCond->setOperand(1, ConstantInt::get(Ty, 0));
1748 }
1749
1750 // Step 4: All the references to the original population counter outside
1751 // the loop are replaced with the NewCount -- the value returned from
1752 // __builtin_ctpop().
1753 CntInst->replaceUsesOutsideBlock(NewCount, Body);
1754
1755 // step 5: Forget the "non-computable" trip-count SCEV associated with the
1756 // loop. The loop would otherwise not be deleted even if it becomes empty.
1757 SE->forgetLoop(CurLoop);
1758}

/build/llvm-toolchain-snapshot-8~svn350071/include/llvm/IR/Instructions.h

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