File: | lib/Transforms/Scalar/LICM.cpp |
Warning: | line 1236, column 20 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===-- LICM.cpp - Loop Invariant Code Motion Pass ------------------------===// | ||||
2 | // | ||||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
4 | // See https://llvm.org/LICENSE.txt for license information. | ||||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
6 | // | ||||
7 | //===----------------------------------------------------------------------===// | ||||
8 | // | ||||
9 | // This pass performs loop invariant code motion, attempting to remove as much | ||||
10 | // code from the body of a loop as possible. It does this by either hoisting | ||||
11 | // code into the preheader block, or by sinking code to the exit blocks if it is | ||||
12 | // safe. This pass also promotes must-aliased memory locations in the loop to | ||||
13 | // live in registers, thus hoisting and sinking "invariant" loads and stores. | ||||
14 | // | ||||
15 | // This pass uses alias analysis for two purposes: | ||||
16 | // | ||||
17 | // 1. Moving loop invariant loads and calls out of loops. If we can determine | ||||
18 | // that a load or call inside of a loop never aliases anything stored to, | ||||
19 | // we can hoist it or sink it like any other instruction. | ||||
20 | // 2. Scalar Promotion of Memory - If there is a store instruction inside of | ||||
21 | // the loop, we try to move the store to happen AFTER the loop instead of | ||||
22 | // inside of the loop. This can only happen if a few conditions are true: | ||||
23 | // A. The pointer stored through is loop invariant | ||||
24 | // B. There are no stores or loads in the loop which _may_ alias the | ||||
25 | // pointer. There are no calls in the loop which mod/ref the pointer. | ||||
26 | // If these conditions are true, we can promote the loads and stores in the | ||||
27 | // loop of the pointer to use a temporary alloca'd variable. We then use | ||||
28 | // the SSAUpdater to construct the appropriate SSA form for the value. | ||||
29 | // | ||||
30 | //===----------------------------------------------------------------------===// | ||||
31 | |||||
32 | #include "llvm/Transforms/Scalar/LICM.h" | ||||
33 | #include "llvm/ADT/SetOperations.h" | ||||
34 | #include "llvm/ADT/Statistic.h" | ||||
35 | #include "llvm/Analysis/AliasAnalysis.h" | ||||
36 | #include "llvm/Analysis/AliasSetTracker.h" | ||||
37 | #include "llvm/Analysis/BasicAliasAnalysis.h" | ||||
38 | #include "llvm/Analysis/CaptureTracking.h" | ||||
39 | #include "llvm/Analysis/ConstantFolding.h" | ||||
40 | #include "llvm/Analysis/GlobalsModRef.h" | ||||
41 | #include "llvm/Analysis/GuardUtils.h" | ||||
42 | #include "llvm/Analysis/Loads.h" | ||||
43 | #include "llvm/Analysis/LoopInfo.h" | ||||
44 | #include "llvm/Analysis/LoopIterator.h" | ||||
45 | #include "llvm/Analysis/LoopPass.h" | ||||
46 | #include "llvm/Analysis/MemoryBuiltins.h" | ||||
47 | #include "llvm/Analysis/MemorySSA.h" | ||||
48 | #include "llvm/Analysis/MemorySSAUpdater.h" | ||||
49 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" | ||||
50 | #include "llvm/Analysis/ScalarEvolution.h" | ||||
51 | #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" | ||||
52 | #include "llvm/Analysis/TargetLibraryInfo.h" | ||||
53 | #include "llvm/Analysis/ValueTracking.h" | ||||
54 | #include "llvm/IR/CFG.h" | ||||
55 | #include "llvm/IR/Constants.h" | ||||
56 | #include "llvm/IR/DataLayout.h" | ||||
57 | #include "llvm/IR/DebugInfoMetadata.h" | ||||
58 | #include "llvm/IR/DerivedTypes.h" | ||||
59 | #include "llvm/IR/Dominators.h" | ||||
60 | #include "llvm/IR/Instructions.h" | ||||
61 | #include "llvm/IR/IntrinsicInst.h" | ||||
62 | #include "llvm/IR/LLVMContext.h" | ||||
63 | #include "llvm/IR/Metadata.h" | ||||
64 | #include "llvm/IR/PatternMatch.h" | ||||
65 | #include "llvm/IR/PredIteratorCache.h" | ||||
66 | #include "llvm/Support/CommandLine.h" | ||||
67 | #include "llvm/Support/Debug.h" | ||||
68 | #include "llvm/Support/raw_ostream.h" | ||||
69 | #include "llvm/Transforms/Scalar.h" | ||||
70 | #include "llvm/Transforms/Scalar/LoopPassManager.h" | ||||
71 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" | ||||
72 | #include "llvm/Transforms/Utils/Local.h" | ||||
73 | #include "llvm/Transforms/Utils/LoopUtils.h" | ||||
74 | #include "llvm/Transforms/Utils/SSAUpdater.h" | ||||
75 | #include <algorithm> | ||||
76 | #include <utility> | ||||
77 | using namespace llvm; | ||||
78 | |||||
79 | #define DEBUG_TYPE"licm" "licm" | ||||
80 | |||||
81 | STATISTIC(NumCreatedBlocks, "Number of blocks created")static llvm::Statistic NumCreatedBlocks = {"licm", "NumCreatedBlocks" , "Number of blocks created"}; | ||||
82 | STATISTIC(NumClonedBranches, "Number of branches cloned")static llvm::Statistic NumClonedBranches = {"licm", "NumClonedBranches" , "Number of branches cloned"}; | ||||
83 | STATISTIC(NumSunk, "Number of instructions sunk out of loop")static llvm::Statistic NumSunk = {"licm", "NumSunk", "Number of instructions sunk out of loop" }; | ||||
84 | STATISTIC(NumHoisted, "Number of instructions hoisted out of loop")static llvm::Statistic NumHoisted = {"licm", "NumHoisted", "Number of instructions hoisted out of loop" }; | ||||
85 | STATISTIC(NumMovedLoads, "Number of load insts hoisted or sunk")static llvm::Statistic NumMovedLoads = {"licm", "NumMovedLoads" , "Number of load insts hoisted or sunk"}; | ||||
86 | STATISTIC(NumMovedCalls, "Number of call insts hoisted or sunk")static llvm::Statistic NumMovedCalls = {"licm", "NumMovedCalls" , "Number of call insts hoisted or sunk"}; | ||||
87 | STATISTIC(NumPromoted, "Number of memory locations promoted to registers")static llvm::Statistic NumPromoted = {"licm", "NumPromoted", "Number of memory locations promoted to registers" }; | ||||
88 | |||||
89 | /// Memory promotion is enabled by default. | ||||
90 | static cl::opt<bool> | ||||
91 | DisablePromotion("disable-licm-promotion", cl::Hidden, cl::init(false), | ||||
92 | cl::desc("Disable memory promotion in LICM pass")); | ||||
93 | |||||
94 | static cl::opt<bool> ControlFlowHoisting( | ||||
95 | "licm-control-flow-hoisting", cl::Hidden, cl::init(false), | ||||
96 | cl::desc("Enable control flow (and PHI) hoisting in LICM")); | ||||
97 | |||||
98 | static cl::opt<uint32_t> MaxNumUsesTraversed( | ||||
99 | "licm-max-num-uses-traversed", cl::Hidden, cl::init(8), | ||||
100 | cl::desc("Max num uses visited for identifying load " | ||||
101 | "invariance in loop using invariant start (default = 8)")); | ||||
102 | |||||
103 | // Default value of zero implies we use the regular alias set tracker mechanism | ||||
104 | // instead of the cross product using AA to identify aliasing of the memory | ||||
105 | // location we are interested in. | ||||
106 | static cl::opt<int> | ||||
107 | LICMN2Theshold("licm-n2-threshold", cl::Hidden, cl::init(0), | ||||
108 | cl::desc("How many instruction to cross product using AA")); | ||||
109 | |||||
110 | // Experimental option to allow imprecision in LICM in pathological cases, in | ||||
111 | // exchange for faster compile. This is to be removed if MemorySSA starts to | ||||
112 | // address the same issue. This flag applies only when LICM uses MemorySSA | ||||
113 | // instead on AliasSetTracker. LICM calls MemorySSAWalker's | ||||
114 | // getClobberingMemoryAccess, up to the value of the Cap, getting perfect | ||||
115 | // accuracy. Afterwards, LICM will call into MemorySSA's getDefiningAccess, | ||||
116 | // which may not be precise, since optimizeUses is capped. The result is | ||||
117 | // correct, but we may not get as "far up" as possible to get which access is | ||||
118 | // clobbering the one queried. | ||||
119 | cl::opt<unsigned> llvm::SetLicmMssaOptCap( | ||||
120 | "licm-mssa-optimization-cap", cl::init(100), cl::Hidden, | ||||
121 | cl::desc("Enable imprecision in LICM in pathological cases, in exchange " | ||||
122 | "for faster compile. Caps the MemorySSA clobbering calls.")); | ||||
123 | |||||
124 | // Experimentally, memory promotion carries less importance than sinking and | ||||
125 | // hoisting. Limit when we do promotion when using MemorySSA, in order to save | ||||
126 | // compile time. | ||||
127 | cl::opt<unsigned> llvm::SetLicmMssaNoAccForPromotionCap( | ||||
128 | "licm-mssa-max-acc-promotion", cl::init(250), cl::Hidden, | ||||
129 | cl::desc("[LICM & MemorySSA] When MSSA in LICM is disabled, this has no " | ||||
130 | "effect. When MSSA in LICM is enabled, then this is the maximum " | ||||
131 | "number of accesses allowed to be present in a loop in order to " | ||||
132 | "enable memory promotion.")); | ||||
133 | |||||
134 | static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI); | ||||
135 | static bool isNotUsedOrFreeInLoop(const Instruction &I, const Loop *CurLoop, | ||||
136 | const LoopSafetyInfo *SafetyInfo, | ||||
137 | TargetTransformInfo *TTI, bool &FreeInLoop); | ||||
138 | static void hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop, | ||||
139 | BasicBlock *Dest, ICFLoopSafetyInfo *SafetyInfo, | ||||
140 | MemorySSAUpdater *MSSAU, OptimizationRemarkEmitter *ORE); | ||||
141 | static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT, | ||||
142 | const Loop *CurLoop, ICFLoopSafetyInfo *SafetyInfo, | ||||
143 | MemorySSAUpdater *MSSAU, OptimizationRemarkEmitter *ORE); | ||||
144 | static bool isSafeToExecuteUnconditionally(Instruction &Inst, | ||||
145 | const DominatorTree *DT, | ||||
146 | const Loop *CurLoop, | ||||
147 | const LoopSafetyInfo *SafetyInfo, | ||||
148 | OptimizationRemarkEmitter *ORE, | ||||
149 | const Instruction *CtxI = nullptr); | ||||
150 | static bool pointerInvalidatedByLoop(MemoryLocation MemLoc, | ||||
151 | AliasSetTracker *CurAST, Loop *CurLoop, | ||||
152 | AliasAnalysis *AA); | ||||
153 | static bool pointerInvalidatedByLoopWithMSSA(MemorySSA *MSSA, MemoryUse *MU, | ||||
154 | Loop *CurLoop, | ||||
155 | SinkAndHoistLICMFlags &Flags); | ||||
156 | static Instruction *CloneInstructionInExitBlock( | ||||
157 | Instruction &I, BasicBlock &ExitBlock, PHINode &PN, const LoopInfo *LI, | ||||
158 | const LoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU); | ||||
159 | |||||
160 | static void eraseInstruction(Instruction &I, ICFLoopSafetyInfo &SafetyInfo, | ||||
161 | AliasSetTracker *AST, MemorySSAUpdater *MSSAU); | ||||
162 | |||||
163 | static void moveInstructionBefore(Instruction &I, Instruction &Dest, | ||||
164 | ICFLoopSafetyInfo &SafetyInfo, | ||||
165 | MemorySSAUpdater *MSSAU); | ||||
166 | |||||
167 | namespace { | ||||
168 | struct LoopInvariantCodeMotion { | ||||
169 | using ASTrackerMapTy = DenseMap<Loop *, std::unique_ptr<AliasSetTracker>>; | ||||
170 | bool runOnLoop(Loop *L, AliasAnalysis *AA, LoopInfo *LI, DominatorTree *DT, | ||||
171 | TargetLibraryInfo *TLI, TargetTransformInfo *TTI, | ||||
172 | ScalarEvolution *SE, MemorySSA *MSSA, | ||||
173 | OptimizationRemarkEmitter *ORE, bool DeleteAST); | ||||
174 | |||||
175 | ASTrackerMapTy &getLoopToAliasSetMap() { return LoopToAliasSetMap; } | ||||
176 | LoopInvariantCodeMotion(unsigned LicmMssaOptCap, | ||||
177 | unsigned LicmMssaNoAccForPromotionCap) | ||||
178 | : LicmMssaOptCap(LicmMssaOptCap), | ||||
179 | LicmMssaNoAccForPromotionCap(LicmMssaNoAccForPromotionCap) {} | ||||
180 | |||||
181 | private: | ||||
182 | ASTrackerMapTy LoopToAliasSetMap; | ||||
183 | unsigned LicmMssaOptCap; | ||||
184 | unsigned LicmMssaNoAccForPromotionCap; | ||||
185 | |||||
186 | std::unique_ptr<AliasSetTracker> | ||||
187 | collectAliasInfoForLoop(Loop *L, LoopInfo *LI, AliasAnalysis *AA); | ||||
188 | std::unique_ptr<AliasSetTracker> | ||||
189 | collectAliasInfoForLoopWithMSSA(Loop *L, AliasAnalysis *AA, | ||||
190 | MemorySSAUpdater *MSSAU); | ||||
191 | }; | ||||
192 | |||||
193 | struct LegacyLICMPass : public LoopPass { | ||||
194 | static char ID; // Pass identification, replacement for typeid | ||||
195 | LegacyLICMPass( | ||||
196 | unsigned LicmMssaOptCap = SetLicmMssaOptCap, | ||||
197 | unsigned LicmMssaNoAccForPromotionCap = SetLicmMssaNoAccForPromotionCap) | ||||
198 | : LoopPass(ID), LICM(LicmMssaOptCap, LicmMssaNoAccForPromotionCap) { | ||||
199 | initializeLegacyLICMPassPass(*PassRegistry::getPassRegistry()); | ||||
200 | } | ||||
201 | |||||
202 | bool runOnLoop(Loop *L, LPPassManager &LPM) override { | ||||
203 | if (skipLoop(L)) { | ||||
204 | // If we have run LICM on a previous loop but now we are skipping | ||||
205 | // (because we've hit the opt-bisect limit), we need to clear the | ||||
206 | // loop alias information. | ||||
207 | LICM.getLoopToAliasSetMap().clear(); | ||||
208 | return false; | ||||
209 | } | ||||
210 | |||||
211 | auto *SE = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>(); | ||||
212 | MemorySSA *MSSA = EnableMSSALoopDependency | ||||
213 | ? (&getAnalysis<MemorySSAWrapperPass>().getMSSA()) | ||||
214 | : nullptr; | ||||
215 | // For the old PM, we can't use OptimizationRemarkEmitter as an analysis | ||||
216 | // pass. Function analyses need to be preserved across loop transformations | ||||
217 | // but ORE cannot be preserved (see comment before the pass definition). | ||||
218 | OptimizationRemarkEmitter ORE(L->getHeader()->getParent()); | ||||
219 | return LICM.runOnLoop(L, | ||||
220 | &getAnalysis<AAResultsWrapperPass>().getAAResults(), | ||||
221 | &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(), | ||||
222 | &getAnalysis<DominatorTreeWrapperPass>().getDomTree(), | ||||
223 | &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI( | ||||
224 | *L->getHeader()->getParent()), | ||||
225 | &getAnalysis<TargetTransformInfoWrapperPass>().getTTI( | ||||
226 | *L->getHeader()->getParent()), | ||||
227 | SE ? &SE->getSE() : nullptr, MSSA, &ORE, false); | ||||
228 | } | ||||
229 | |||||
230 | /// This transformation requires natural loop information & requires that | ||||
231 | /// loop preheaders be inserted into the CFG... | ||||
232 | /// | ||||
233 | void getAnalysisUsage(AnalysisUsage &AU) const override { | ||||
234 | AU.addPreserved<DominatorTreeWrapperPass>(); | ||||
235 | AU.addPreserved<LoopInfoWrapperPass>(); | ||||
236 | AU.addRequired<TargetLibraryInfoWrapperPass>(); | ||||
237 | if (EnableMSSALoopDependency) { | ||||
238 | AU.addRequired<MemorySSAWrapperPass>(); | ||||
239 | AU.addPreserved<MemorySSAWrapperPass>(); | ||||
240 | } | ||||
241 | AU.addRequired<TargetTransformInfoWrapperPass>(); | ||||
242 | getLoopAnalysisUsage(AU); | ||||
243 | } | ||||
244 | |||||
245 | using llvm::Pass::doFinalization; | ||||
246 | |||||
247 | bool doFinalization() override { | ||||
248 | auto &AliasSetMap = LICM.getLoopToAliasSetMap(); | ||||
249 | // All loops in the AliasSetMap should be cleaned up already. The only case | ||||
250 | // where we fail to do so is if an outer loop gets deleted before LICM | ||||
251 | // visits it. | ||||
252 | assert(all_of(AliasSetMap,((all_of(AliasSetMap, [](LoopInvariantCodeMotion::ASTrackerMapTy ::value_type &KV) { return !KV.first->getParentLoop(); }) && "Didn't free loop alias sets") ? static_cast< void> (0) : __assert_fail ("all_of(AliasSetMap, [](LoopInvariantCodeMotion::ASTrackerMapTy::value_type &KV) { return !KV.first->getParentLoop(); }) && \"Didn't free loop alias sets\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 256, __PRETTY_FUNCTION__)) | ||||
253 | [](LoopInvariantCodeMotion::ASTrackerMapTy::value_type &KV) {((all_of(AliasSetMap, [](LoopInvariantCodeMotion::ASTrackerMapTy ::value_type &KV) { return !KV.first->getParentLoop(); }) && "Didn't free loop alias sets") ? static_cast< void> (0) : __assert_fail ("all_of(AliasSetMap, [](LoopInvariantCodeMotion::ASTrackerMapTy::value_type &KV) { return !KV.first->getParentLoop(); }) && \"Didn't free loop alias sets\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 256, __PRETTY_FUNCTION__)) | ||||
254 | return !KV.first->getParentLoop();((all_of(AliasSetMap, [](LoopInvariantCodeMotion::ASTrackerMapTy ::value_type &KV) { return !KV.first->getParentLoop(); }) && "Didn't free loop alias sets") ? static_cast< void> (0) : __assert_fail ("all_of(AliasSetMap, [](LoopInvariantCodeMotion::ASTrackerMapTy::value_type &KV) { return !KV.first->getParentLoop(); }) && \"Didn't free loop alias sets\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 256, __PRETTY_FUNCTION__)) | ||||
255 | }) &&((all_of(AliasSetMap, [](LoopInvariantCodeMotion::ASTrackerMapTy ::value_type &KV) { return !KV.first->getParentLoop(); }) && "Didn't free loop alias sets") ? static_cast< void> (0) : __assert_fail ("all_of(AliasSetMap, [](LoopInvariantCodeMotion::ASTrackerMapTy::value_type &KV) { return !KV.first->getParentLoop(); }) && \"Didn't free loop alias sets\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 256, __PRETTY_FUNCTION__)) | ||||
256 | "Didn't free loop alias sets")((all_of(AliasSetMap, [](LoopInvariantCodeMotion::ASTrackerMapTy ::value_type &KV) { return !KV.first->getParentLoop(); }) && "Didn't free loop alias sets") ? static_cast< void> (0) : __assert_fail ("all_of(AliasSetMap, [](LoopInvariantCodeMotion::ASTrackerMapTy::value_type &KV) { return !KV.first->getParentLoop(); }) && \"Didn't free loop alias sets\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 256, __PRETTY_FUNCTION__)); | ||||
257 | AliasSetMap.clear(); | ||||
258 | return false; | ||||
259 | } | ||||
260 | |||||
261 | private: | ||||
262 | LoopInvariantCodeMotion LICM; | ||||
263 | |||||
264 | /// cloneBasicBlockAnalysis - Simple Analysis hook. Clone alias set info. | ||||
265 | void cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To, | ||||
266 | Loop *L) override; | ||||
267 | |||||
268 | /// deleteAnalysisValue - Simple Analysis hook. Delete value V from alias | ||||
269 | /// set. | ||||
270 | void deleteAnalysisValue(Value *V, Loop *L) override; | ||||
271 | |||||
272 | /// Simple Analysis hook. Delete loop L from alias set map. | ||||
273 | void deleteAnalysisLoop(Loop *L) override; | ||||
274 | }; | ||||
275 | } // namespace | ||||
276 | |||||
277 | PreservedAnalyses LICMPass::run(Loop &L, LoopAnalysisManager &AM, | ||||
278 | LoopStandardAnalysisResults &AR, LPMUpdater &) { | ||||
279 | const auto &FAM = | ||||
280 | AM.getResult<FunctionAnalysisManagerLoopProxy>(L, AR).getManager(); | ||||
281 | Function *F = L.getHeader()->getParent(); | ||||
282 | |||||
283 | auto *ORE = FAM.getCachedResult<OptimizationRemarkEmitterAnalysis>(*F); | ||||
284 | // FIXME: This should probably be optional rather than required. | ||||
285 | if (!ORE) | ||||
286 | report_fatal_error("LICM: OptimizationRemarkEmitterAnalysis not " | ||||
287 | "cached at a higher level"); | ||||
288 | |||||
289 | LoopInvariantCodeMotion LICM(LicmMssaOptCap, LicmMssaNoAccForPromotionCap); | ||||
290 | if (!LICM.runOnLoop(&L, &AR.AA, &AR.LI, &AR.DT, &AR.TLI, &AR.TTI, &AR.SE, | ||||
291 | AR.MSSA, ORE, true)) | ||||
292 | return PreservedAnalyses::all(); | ||||
293 | |||||
294 | auto PA = getLoopPassPreservedAnalyses(); | ||||
295 | |||||
296 | PA.preserve<DominatorTreeAnalysis>(); | ||||
297 | PA.preserve<LoopAnalysis>(); | ||||
298 | if (AR.MSSA) | ||||
299 | PA.preserve<MemorySSAAnalysis>(); | ||||
300 | |||||
301 | return PA; | ||||
302 | } | ||||
303 | |||||
304 | char LegacyLICMPass::ID = 0; | ||||
305 | INITIALIZE_PASS_BEGIN(LegacyLICMPass, "licm", "Loop Invariant Code Motion",static void *initializeLegacyLICMPassPassOnce(PassRegistry & Registry) { | ||||
306 | false, false)static void *initializeLegacyLICMPassPassOnce(PassRegistry & Registry) { | ||||
307 | INITIALIZE_PASS_DEPENDENCY(LoopPass)initializeLoopPassPass(Registry); | ||||
308 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)initializeTargetLibraryInfoWrapperPassPass(Registry); | ||||
309 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)initializeTargetTransformInfoWrapperPassPass(Registry); | ||||
310 | INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)initializeMemorySSAWrapperPassPass(Registry); | ||||
311 | INITIALIZE_PASS_END(LegacyLICMPass, "licm", "Loop Invariant Code Motion", false,PassInfo *PI = new PassInfo( "Loop Invariant Code Motion", "licm" , &LegacyLICMPass::ID, PassInfo::NormalCtor_t(callDefaultCtor <LegacyLICMPass>), false, false); Registry.registerPass (*PI, true); return PI; } static llvm::once_flag InitializeLegacyLICMPassPassFlag ; void llvm::initializeLegacyLICMPassPass(PassRegistry &Registry ) { llvm::call_once(InitializeLegacyLICMPassPassFlag, initializeLegacyLICMPassPassOnce , std::ref(Registry)); } | ||||
312 | false)PassInfo *PI = new PassInfo( "Loop Invariant Code Motion", "licm" , &LegacyLICMPass::ID, PassInfo::NormalCtor_t(callDefaultCtor <LegacyLICMPass>), false, false); Registry.registerPass (*PI, true); return PI; } static llvm::once_flag InitializeLegacyLICMPassPassFlag ; void llvm::initializeLegacyLICMPassPass(PassRegistry &Registry ) { llvm::call_once(InitializeLegacyLICMPassPassFlag, initializeLegacyLICMPassPassOnce , std::ref(Registry)); } | ||||
313 | |||||
314 | Pass *llvm::createLICMPass() { return new LegacyLICMPass(); } | ||||
315 | Pass *llvm::createLICMPass(unsigned LicmMssaOptCap, | ||||
316 | unsigned LicmMssaNoAccForPromotionCap) { | ||||
317 | return new LegacyLICMPass(LicmMssaOptCap, LicmMssaNoAccForPromotionCap); | ||||
318 | } | ||||
319 | |||||
320 | /// Hoist expressions out of the specified loop. Note, alias info for inner | ||||
321 | /// loop is not preserved so it is not a good idea to run LICM multiple | ||||
322 | /// times on one loop. | ||||
323 | /// We should delete AST for inner loops in the new pass manager to avoid | ||||
324 | /// memory leak. | ||||
325 | /// | ||||
326 | bool LoopInvariantCodeMotion::runOnLoop( | ||||
327 | Loop *L, AliasAnalysis *AA, LoopInfo *LI, DominatorTree *DT, | ||||
328 | TargetLibraryInfo *TLI, TargetTransformInfo *TTI, ScalarEvolution *SE, | ||||
329 | MemorySSA *MSSA, OptimizationRemarkEmitter *ORE, bool DeleteAST) { | ||||
330 | bool Changed = false; | ||||
331 | |||||
332 | assert(L->isLCSSAForm(*DT) && "Loop is not in LCSSA form.")((L->isLCSSAForm(*DT) && "Loop is not in LCSSA form." ) ? static_cast<void> (0) : __assert_fail ("L->isLCSSAForm(*DT) && \"Loop is not in LCSSA form.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 332, __PRETTY_FUNCTION__)); | ||||
333 | |||||
334 | // If this loop has metadata indicating that LICM is not to be performed then | ||||
335 | // just exit. | ||||
336 | if (hasDisableLICMTransformsHint(L)) { | ||||
337 | return false; | ||||
338 | } | ||||
339 | |||||
340 | std::unique_ptr<AliasSetTracker> CurAST; | ||||
341 | std::unique_ptr<MemorySSAUpdater> MSSAU; | ||||
342 | bool NoOfMemAccTooLarge = false; | ||||
343 | unsigned LicmMssaOptCounter = 0; | ||||
344 | |||||
345 | if (!MSSA) { | ||||
346 | LLVM_DEBUG(dbgs() << "LICM: Using Alias Set Tracker.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM: Using Alias Set Tracker.\n" ; } } while (false); | ||||
347 | CurAST = collectAliasInfoForLoop(L, LI, AA); | ||||
348 | } else { | ||||
349 | LLVM_DEBUG(dbgs() << "LICM: Using MemorySSA.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM: Using MemorySSA.\n"; } } while (false); | ||||
350 | MSSAU = std::make_unique<MemorySSAUpdater>(MSSA); | ||||
351 | |||||
352 | unsigned AccessCapCount = 0; | ||||
353 | for (auto *BB : L->getBlocks()) { | ||||
354 | if (auto *Accesses = MSSA->getBlockAccesses(BB)) { | ||||
355 | for (const auto &MA : *Accesses) { | ||||
356 | (void)MA; | ||||
357 | AccessCapCount++; | ||||
358 | if (AccessCapCount > LicmMssaNoAccForPromotionCap) { | ||||
359 | NoOfMemAccTooLarge = true; | ||||
360 | break; | ||||
361 | } | ||||
362 | } | ||||
363 | } | ||||
364 | if (NoOfMemAccTooLarge) | ||||
365 | break; | ||||
366 | } | ||||
367 | } | ||||
368 | |||||
369 | // Get the preheader block to move instructions into... | ||||
370 | BasicBlock *Preheader = L->getLoopPreheader(); | ||||
371 | |||||
372 | // Compute loop safety information. | ||||
373 | ICFLoopSafetyInfo SafetyInfo(DT); | ||||
374 | SafetyInfo.computeLoopSafetyInfo(L); | ||||
375 | |||||
376 | // We want to visit all of the instructions in this loop... that are not parts | ||||
377 | // of our subloops (they have already had their invariants hoisted out of | ||||
378 | // their loop, into this loop, so there is no need to process the BODIES of | ||||
379 | // the subloops). | ||||
380 | // | ||||
381 | // Traverse the body of the loop in depth first order on the dominator tree so | ||||
382 | // that we are guaranteed to see definitions before we see uses. This allows | ||||
383 | // us to sink instructions in one pass, without iteration. After sinking | ||||
384 | // instructions, we perform another pass to hoist them out of the loop. | ||||
385 | SinkAndHoistLICMFlags Flags = {NoOfMemAccTooLarge, LicmMssaOptCounter, | ||||
386 | LicmMssaOptCap, LicmMssaNoAccForPromotionCap, | ||||
387 | /*IsSink=*/true}; | ||||
388 | if (L->hasDedicatedExits()) | ||||
389 | Changed |= sinkRegion(DT->getNode(L->getHeader()), AA, LI, DT, TLI, TTI, L, | ||||
390 | CurAST.get(), MSSAU.get(), &SafetyInfo, Flags, ORE); | ||||
391 | Flags.IsSink = false; | ||||
392 | if (Preheader) | ||||
393 | Changed |= hoistRegion(DT->getNode(L->getHeader()), AA, LI, DT, TLI, L, | ||||
394 | CurAST.get(), MSSAU.get(), &SafetyInfo, Flags, ORE); | ||||
395 | |||||
396 | // Now that all loop invariants have been removed from the loop, promote any | ||||
397 | // memory references to scalars that we can. | ||||
398 | // Don't sink stores from loops without dedicated block exits. Exits | ||||
399 | // containing indirect branches are not transformed by loop simplify, | ||||
400 | // make sure we catch that. An additional load may be generated in the | ||||
401 | // preheader for SSA updater, so also avoid sinking when no preheader | ||||
402 | // is available. | ||||
403 | if (!DisablePromotion && Preheader && L->hasDedicatedExits() && | ||||
404 | !NoOfMemAccTooLarge) { | ||||
405 | // Figure out the loop exits and their insertion points | ||||
406 | SmallVector<BasicBlock *, 8> ExitBlocks; | ||||
407 | L->getUniqueExitBlocks(ExitBlocks); | ||||
408 | |||||
409 | // We can't insert into a catchswitch. | ||||
410 | bool HasCatchSwitch = llvm::any_of(ExitBlocks, [](BasicBlock *Exit) { | ||||
411 | return isa<CatchSwitchInst>(Exit->getTerminator()); | ||||
412 | }); | ||||
413 | |||||
414 | if (!HasCatchSwitch) { | ||||
415 | SmallVector<Instruction *, 8> InsertPts; | ||||
416 | SmallVector<MemoryAccess *, 8> MSSAInsertPts; | ||||
417 | InsertPts.reserve(ExitBlocks.size()); | ||||
418 | if (MSSAU) | ||||
419 | MSSAInsertPts.reserve(ExitBlocks.size()); | ||||
420 | for (BasicBlock *ExitBlock : ExitBlocks) { | ||||
421 | InsertPts.push_back(&*ExitBlock->getFirstInsertionPt()); | ||||
422 | if (MSSAU) | ||||
423 | MSSAInsertPts.push_back(nullptr); | ||||
424 | } | ||||
425 | |||||
426 | PredIteratorCache PIC; | ||||
427 | |||||
428 | bool Promoted = false; | ||||
429 | |||||
430 | // Build an AST using MSSA. | ||||
431 | if (!CurAST.get()) | ||||
432 | CurAST = collectAliasInfoForLoopWithMSSA(L, AA, MSSAU.get()); | ||||
433 | |||||
434 | // Loop over all of the alias sets in the tracker object. | ||||
435 | for (AliasSet &AS : *CurAST) { | ||||
436 | // We can promote this alias set if it has a store, if it is a "Must" | ||||
437 | // alias set, if the pointer is loop invariant, and if we are not | ||||
438 | // eliminating any volatile loads or stores. | ||||
439 | if (AS.isForwardingAliasSet() || !AS.isMod() || !AS.isMustAlias() || | ||||
440 | !L->isLoopInvariant(AS.begin()->getValue())) | ||||
441 | continue; | ||||
442 | |||||
443 | assert(((!AS.empty() && "Must alias set should have at least one pointer element in it!" ) ? static_cast<void> (0) : __assert_fail ("!AS.empty() && \"Must alias set should have at least one pointer element in it!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 445, __PRETTY_FUNCTION__)) | ||||
444 | !AS.empty() &&((!AS.empty() && "Must alias set should have at least one pointer element in it!" ) ? static_cast<void> (0) : __assert_fail ("!AS.empty() && \"Must alias set should have at least one pointer element in it!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 445, __PRETTY_FUNCTION__)) | ||||
445 | "Must alias set should have at least one pointer element in it!")((!AS.empty() && "Must alias set should have at least one pointer element in it!" ) ? static_cast<void> (0) : __assert_fail ("!AS.empty() && \"Must alias set should have at least one pointer element in it!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 445, __PRETTY_FUNCTION__)); | ||||
446 | |||||
447 | SmallSetVector<Value *, 8> PointerMustAliases; | ||||
448 | for (const auto &ASI : AS) | ||||
449 | PointerMustAliases.insert(ASI.getValue()); | ||||
450 | |||||
451 | Promoted |= promoteLoopAccessesToScalars( | ||||
452 | PointerMustAliases, ExitBlocks, InsertPts, MSSAInsertPts, PIC, LI, | ||||
453 | DT, TLI, L, CurAST.get(), MSSAU.get(), &SafetyInfo, ORE); | ||||
454 | } | ||||
455 | |||||
456 | // Once we have promoted values across the loop body we have to | ||||
457 | // recursively reform LCSSA as any nested loop may now have values defined | ||||
458 | // within the loop used in the outer loop. | ||||
459 | // FIXME: This is really heavy handed. It would be a bit better to use an | ||||
460 | // SSAUpdater strategy during promotion that was LCSSA aware and reformed | ||||
461 | // it as it went. | ||||
462 | if (Promoted) | ||||
463 | formLCSSARecursively(*L, *DT, LI, SE); | ||||
464 | |||||
465 | Changed |= Promoted; | ||||
466 | } | ||||
467 | } | ||||
468 | |||||
469 | // Check that neither this loop nor its parent have had LCSSA broken. LICM is | ||||
470 | // specifically moving instructions across the loop boundary and so it is | ||||
471 | // especially in need of sanity checking here. | ||||
472 | assert(L->isLCSSAForm(*DT) && "Loop not left in LCSSA form after LICM!")((L->isLCSSAForm(*DT) && "Loop not left in LCSSA form after LICM!" ) ? static_cast<void> (0) : __assert_fail ("L->isLCSSAForm(*DT) && \"Loop not left in LCSSA form after LICM!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 472, __PRETTY_FUNCTION__)); | ||||
473 | assert((!L->getParentLoop() || L->getParentLoop()->isLCSSAForm(*DT)) &&(((!L->getParentLoop() || L->getParentLoop()->isLCSSAForm (*DT)) && "Parent loop not left in LCSSA form after LICM!" ) ? static_cast<void> (0) : __assert_fail ("(!L->getParentLoop() || L->getParentLoop()->isLCSSAForm(*DT)) && \"Parent loop not left in LCSSA form after LICM!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 474, __PRETTY_FUNCTION__)) | ||||
474 | "Parent loop not left in LCSSA form after LICM!")(((!L->getParentLoop() || L->getParentLoop()->isLCSSAForm (*DT)) && "Parent loop not left in LCSSA form after LICM!" ) ? static_cast<void> (0) : __assert_fail ("(!L->getParentLoop() || L->getParentLoop()->isLCSSAForm(*DT)) && \"Parent loop not left in LCSSA form after LICM!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 474, __PRETTY_FUNCTION__)); | ||||
475 | |||||
476 | // If this loop is nested inside of another one, save the alias information | ||||
477 | // for when we process the outer loop. | ||||
478 | if (!MSSAU.get() && CurAST.get() && L->getParentLoop() && !DeleteAST) | ||||
479 | LoopToAliasSetMap[L] = std::move(CurAST); | ||||
480 | |||||
481 | if (MSSAU.get() && VerifyMemorySSA) | ||||
482 | MSSAU->getMemorySSA()->verifyMemorySSA(); | ||||
483 | |||||
484 | if (Changed && SE) | ||||
485 | SE->forgetLoopDispositions(L); | ||||
486 | return Changed; | ||||
487 | } | ||||
488 | |||||
489 | /// Walk the specified region of the CFG (defined by all blocks dominated by | ||||
490 | /// the specified block, and that are in the current loop) in reverse depth | ||||
491 | /// first order w.r.t the DominatorTree. This allows us to visit uses before | ||||
492 | /// definitions, allowing us to sink a loop body in one pass without iteration. | ||||
493 | /// | ||||
494 | bool llvm::sinkRegion(DomTreeNode *N, AliasAnalysis *AA, LoopInfo *LI, | ||||
495 | DominatorTree *DT, TargetLibraryInfo *TLI, | ||||
496 | TargetTransformInfo *TTI, Loop *CurLoop, | ||||
497 | AliasSetTracker *CurAST, MemorySSAUpdater *MSSAU, | ||||
498 | ICFLoopSafetyInfo *SafetyInfo, | ||||
499 | SinkAndHoistLICMFlags &Flags, | ||||
500 | OptimizationRemarkEmitter *ORE) { | ||||
501 | |||||
502 | // Verify inputs. | ||||
503 | assert(N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr &&((N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && "Unexpected input to sinkRegion." ) ? static_cast<void> (0) : __assert_fail ("N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected input to sinkRegion.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 505, __PRETTY_FUNCTION__)) | ||||
504 | CurLoop != nullptr && SafetyInfo != nullptr &&((N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && "Unexpected input to sinkRegion." ) ? static_cast<void> (0) : __assert_fail ("N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected input to sinkRegion.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 505, __PRETTY_FUNCTION__)) | ||||
505 | "Unexpected input to sinkRegion.")((N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && "Unexpected input to sinkRegion." ) ? static_cast<void> (0) : __assert_fail ("N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected input to sinkRegion.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 505, __PRETTY_FUNCTION__)); | ||||
506 | assert(((CurAST != nullptr) ^ (MSSAU != nullptr)) &&((((CurAST != nullptr) ^ (MSSAU != nullptr)) && "Either AliasSetTracker or MemorySSA should be initialized." ) ? static_cast<void> (0) : __assert_fail ("((CurAST != nullptr) ^ (MSSAU != nullptr)) && \"Either AliasSetTracker or MemorySSA should be initialized.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 507, __PRETTY_FUNCTION__)) | ||||
507 | "Either AliasSetTracker or MemorySSA should be initialized.")((((CurAST != nullptr) ^ (MSSAU != nullptr)) && "Either AliasSetTracker or MemorySSA should be initialized." ) ? static_cast<void> (0) : __assert_fail ("((CurAST != nullptr) ^ (MSSAU != nullptr)) && \"Either AliasSetTracker or MemorySSA should be initialized.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 507, __PRETTY_FUNCTION__)); | ||||
508 | |||||
509 | // We want to visit children before parents. We will enque all the parents | ||||
510 | // before their children in the worklist and process the worklist in reverse | ||||
511 | // order. | ||||
512 | SmallVector<DomTreeNode *, 16> Worklist = collectChildrenInLoop(N, CurLoop); | ||||
513 | |||||
514 | bool Changed = false; | ||||
515 | for (DomTreeNode *DTN : reverse(Worklist)) { | ||||
516 | BasicBlock *BB = DTN->getBlock(); | ||||
517 | // Only need to process the contents of this block if it is not part of a | ||||
518 | // subloop (which would already have been processed). | ||||
519 | if (inSubLoop(BB, CurLoop, LI)) | ||||
520 | continue; | ||||
521 | |||||
522 | for (BasicBlock::iterator II = BB->end(); II != BB->begin();) { | ||||
523 | Instruction &I = *--II; | ||||
524 | |||||
525 | // If the instruction is dead, we would try to sink it because it isn't | ||||
526 | // used in the loop, instead, just delete it. | ||||
527 | if (isInstructionTriviallyDead(&I, TLI)) { | ||||
528 | LLVM_DEBUG(dbgs() << "LICM deleting dead inst: " << I << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM deleting dead inst: " << I << '\n'; } } while (false); | ||||
529 | salvageDebugInfo(I); | ||||
530 | ++II; | ||||
531 | eraseInstruction(I, *SafetyInfo, CurAST, MSSAU); | ||||
532 | Changed = true; | ||||
533 | continue; | ||||
534 | } | ||||
535 | |||||
536 | // Check to see if we can sink this instruction to the exit blocks | ||||
537 | // of the loop. We can do this if the all users of the instruction are | ||||
538 | // outside of the loop. In this case, it doesn't even matter if the | ||||
539 | // operands of the instruction are loop invariant. | ||||
540 | // | ||||
541 | bool FreeInLoop = false; | ||||
542 | if (isNotUsedOrFreeInLoop(I, CurLoop, SafetyInfo, TTI, FreeInLoop) && | ||||
543 | canSinkOrHoistInst(I, AA, DT, CurLoop, CurAST, MSSAU, true, &Flags, | ||||
544 | ORE) && | ||||
545 | !I.mayHaveSideEffects()) { | ||||
546 | if (sink(I, LI, DT, CurLoop, SafetyInfo, MSSAU, ORE)) { | ||||
547 | if (!FreeInLoop) { | ||||
548 | ++II; | ||||
549 | eraseInstruction(I, *SafetyInfo, CurAST, MSSAU); | ||||
550 | } | ||||
551 | Changed = true; | ||||
552 | } | ||||
553 | } | ||||
554 | } | ||||
555 | } | ||||
556 | if (MSSAU && VerifyMemorySSA) | ||||
557 | MSSAU->getMemorySSA()->verifyMemorySSA(); | ||||
558 | return Changed; | ||||
559 | } | ||||
560 | |||||
561 | namespace { | ||||
562 | // This is a helper class for hoistRegion to make it able to hoist control flow | ||||
563 | // in order to be able to hoist phis. The way this works is that we initially | ||||
564 | // start hoisting to the loop preheader, and when we see a loop invariant branch | ||||
565 | // we make note of this. When we then come to hoist an instruction that's | ||||
566 | // conditional on such a branch we duplicate the branch and the relevant control | ||||
567 | // flow, then hoist the instruction into the block corresponding to its original | ||||
568 | // block in the duplicated control flow. | ||||
569 | class ControlFlowHoister { | ||||
570 | private: | ||||
571 | // Information about the loop we are hoisting from | ||||
572 | LoopInfo *LI; | ||||
573 | DominatorTree *DT; | ||||
574 | Loop *CurLoop; | ||||
575 | MemorySSAUpdater *MSSAU; | ||||
576 | |||||
577 | // A map of blocks in the loop to the block their instructions will be hoisted | ||||
578 | // to. | ||||
579 | DenseMap<BasicBlock *, BasicBlock *> HoistDestinationMap; | ||||
580 | |||||
581 | // The branches that we can hoist, mapped to the block that marks a | ||||
582 | // convergence point of their control flow. | ||||
583 | DenseMap<BranchInst *, BasicBlock *> HoistableBranches; | ||||
584 | |||||
585 | public: | ||||
586 | ControlFlowHoister(LoopInfo *LI, DominatorTree *DT, Loop *CurLoop, | ||||
587 | MemorySSAUpdater *MSSAU) | ||||
588 | : LI(LI), DT(DT), CurLoop(CurLoop), MSSAU(MSSAU) {} | ||||
589 | |||||
590 | void registerPossiblyHoistableBranch(BranchInst *BI) { | ||||
591 | // We can only hoist conditional branches with loop invariant operands. | ||||
592 | if (!ControlFlowHoisting || !BI->isConditional() || | ||||
593 | !CurLoop->hasLoopInvariantOperands(BI)) | ||||
594 | return; | ||||
595 | |||||
596 | // The branch destinations need to be in the loop, and we don't gain | ||||
597 | // anything by duplicating conditional branches with duplicate successors, | ||||
598 | // as it's essentially the same as an unconditional branch. | ||||
599 | BasicBlock *TrueDest = BI->getSuccessor(0); | ||||
600 | BasicBlock *FalseDest = BI->getSuccessor(1); | ||||
601 | if (!CurLoop->contains(TrueDest) || !CurLoop->contains(FalseDest) || | ||||
602 | TrueDest == FalseDest) | ||||
603 | return; | ||||
604 | |||||
605 | // We can hoist BI if one branch destination is the successor of the other, | ||||
606 | // or both have common successor which we check by seeing if the | ||||
607 | // intersection of their successors is non-empty. | ||||
608 | // TODO: This could be expanded to allowing branches where both ends | ||||
609 | // eventually converge to a single block. | ||||
610 | SmallPtrSet<BasicBlock *, 4> TrueDestSucc, FalseDestSucc; | ||||
611 | TrueDestSucc.insert(succ_begin(TrueDest), succ_end(TrueDest)); | ||||
612 | FalseDestSucc.insert(succ_begin(FalseDest), succ_end(FalseDest)); | ||||
613 | BasicBlock *CommonSucc = nullptr; | ||||
614 | if (TrueDestSucc.count(FalseDest)) { | ||||
615 | CommonSucc = FalseDest; | ||||
616 | } else if (FalseDestSucc.count(TrueDest)) { | ||||
617 | CommonSucc = TrueDest; | ||||
618 | } else { | ||||
619 | set_intersect(TrueDestSucc, FalseDestSucc); | ||||
620 | // If there's one common successor use that. | ||||
621 | if (TrueDestSucc.size() == 1) | ||||
622 | CommonSucc = *TrueDestSucc.begin(); | ||||
623 | // If there's more than one pick whichever appears first in the block list | ||||
624 | // (we can't use the value returned by TrueDestSucc.begin() as it's | ||||
625 | // unpredicatable which element gets returned). | ||||
626 | else if (!TrueDestSucc.empty()) { | ||||
627 | Function *F = TrueDest->getParent(); | ||||
628 | auto IsSucc = [&](BasicBlock &BB) { return TrueDestSucc.count(&BB); }; | ||||
629 | auto It = std::find_if(F->begin(), F->end(), IsSucc); | ||||
630 | assert(It != F->end() && "Could not find successor in function")((It != F->end() && "Could not find successor in function" ) ? static_cast<void> (0) : __assert_fail ("It != F->end() && \"Could not find successor in function\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 630, __PRETTY_FUNCTION__)); | ||||
631 | CommonSucc = &*It; | ||||
632 | } | ||||
633 | } | ||||
634 | // The common successor has to be dominated by the branch, as otherwise | ||||
635 | // there will be some other path to the successor that will not be | ||||
636 | // controlled by this branch so any phi we hoist would be controlled by the | ||||
637 | // wrong condition. This also takes care of avoiding hoisting of loop back | ||||
638 | // edges. | ||||
639 | // TODO: In some cases this could be relaxed if the successor is dominated | ||||
640 | // by another block that's been hoisted and we can guarantee that the | ||||
641 | // control flow has been replicated exactly. | ||||
642 | if (CommonSucc && DT->dominates(BI, CommonSucc)) | ||||
643 | HoistableBranches[BI] = CommonSucc; | ||||
644 | } | ||||
645 | |||||
646 | bool canHoistPHI(PHINode *PN) { | ||||
647 | // The phi must have loop invariant operands. | ||||
648 | if (!ControlFlowHoisting || !CurLoop->hasLoopInvariantOperands(PN)) | ||||
649 | return false; | ||||
650 | // We can hoist phis if the block they are in is the target of hoistable | ||||
651 | // branches which cover all of the predecessors of the block. | ||||
652 | SmallPtrSet<BasicBlock *, 8> PredecessorBlocks; | ||||
653 | BasicBlock *BB = PN->getParent(); | ||||
654 | for (BasicBlock *PredBB : predecessors(BB)) | ||||
655 | PredecessorBlocks.insert(PredBB); | ||||
656 | // If we have less predecessor blocks than predecessors then the phi will | ||||
657 | // have more than one incoming value for the same block which we can't | ||||
658 | // handle. | ||||
659 | // TODO: This could be handled be erasing some of the duplicate incoming | ||||
660 | // values. | ||||
661 | if (PredecessorBlocks.size() != pred_size(BB)) | ||||
662 | return false; | ||||
663 | for (auto &Pair : HoistableBranches) { | ||||
664 | if (Pair.second == BB) { | ||||
665 | // Which blocks are predecessors via this branch depends on if the | ||||
666 | // branch is triangle-like or diamond-like. | ||||
667 | if (Pair.first->getSuccessor(0) == BB) { | ||||
668 | PredecessorBlocks.erase(Pair.first->getParent()); | ||||
669 | PredecessorBlocks.erase(Pair.first->getSuccessor(1)); | ||||
670 | } else if (Pair.first->getSuccessor(1) == BB) { | ||||
671 | PredecessorBlocks.erase(Pair.first->getParent()); | ||||
672 | PredecessorBlocks.erase(Pair.first->getSuccessor(0)); | ||||
673 | } else { | ||||
674 | PredecessorBlocks.erase(Pair.first->getSuccessor(0)); | ||||
675 | PredecessorBlocks.erase(Pair.first->getSuccessor(1)); | ||||
676 | } | ||||
677 | } | ||||
678 | } | ||||
679 | // PredecessorBlocks will now be empty if for every predecessor of BB we | ||||
680 | // found a hoistable branch source. | ||||
681 | return PredecessorBlocks.empty(); | ||||
682 | } | ||||
683 | |||||
684 | BasicBlock *getOrCreateHoistedBlock(BasicBlock *BB) { | ||||
685 | if (!ControlFlowHoisting) | ||||
686 | return CurLoop->getLoopPreheader(); | ||||
687 | // If BB has already been hoisted, return that | ||||
688 | if (HoistDestinationMap.count(BB)) | ||||
689 | return HoistDestinationMap[BB]; | ||||
690 | |||||
691 | // Check if this block is conditional based on a pending branch | ||||
692 | auto HasBBAsSuccessor = | ||||
693 | [&](DenseMap<BranchInst *, BasicBlock *>::value_type &Pair) { | ||||
694 | return BB != Pair.second && (Pair.first->getSuccessor(0) == BB || | ||||
695 | Pair.first->getSuccessor(1) == BB); | ||||
696 | }; | ||||
697 | auto It = std::find_if(HoistableBranches.begin(), HoistableBranches.end(), | ||||
698 | HasBBAsSuccessor); | ||||
699 | |||||
700 | // If not involved in a pending branch, hoist to preheader | ||||
701 | BasicBlock *InitialPreheader = CurLoop->getLoopPreheader(); | ||||
702 | if (It == HoistableBranches.end()) { | ||||
703 | LLVM_DEBUG(dbgs() << "LICM using " << InitialPreheader->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM using " << InitialPreheader ->getName() << " as hoist destination for " << BB->getName() << "\n"; } } while (false) | ||||
704 | << " as hoist destination for " << BB->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM using " << InitialPreheader ->getName() << " as hoist destination for " << BB->getName() << "\n"; } } while (false) | ||||
705 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM using " << InitialPreheader ->getName() << " as hoist destination for " << BB->getName() << "\n"; } } while (false); | ||||
706 | HoistDestinationMap[BB] = InitialPreheader; | ||||
707 | return InitialPreheader; | ||||
708 | } | ||||
709 | BranchInst *BI = It->first; | ||||
710 | assert(std::find_if(++It, HoistableBranches.end(), HasBBAsSuccessor) ==((std::find_if(++It, HoistableBranches.end(), HasBBAsSuccessor ) == HoistableBranches.end() && "BB is expected to be the target of at most one branch" ) ? static_cast<void> (0) : __assert_fail ("std::find_if(++It, HoistableBranches.end(), HasBBAsSuccessor) == HoistableBranches.end() && \"BB is expected to be the target of at most one branch\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 712, __PRETTY_FUNCTION__)) | ||||
711 | HoistableBranches.end() &&((std::find_if(++It, HoistableBranches.end(), HasBBAsSuccessor ) == HoistableBranches.end() && "BB is expected to be the target of at most one branch" ) ? static_cast<void> (0) : __assert_fail ("std::find_if(++It, HoistableBranches.end(), HasBBAsSuccessor) == HoistableBranches.end() && \"BB is expected to be the target of at most one branch\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 712, __PRETTY_FUNCTION__)) | ||||
712 | "BB is expected to be the target of at most one branch")((std::find_if(++It, HoistableBranches.end(), HasBBAsSuccessor ) == HoistableBranches.end() && "BB is expected to be the target of at most one branch" ) ? static_cast<void> (0) : __assert_fail ("std::find_if(++It, HoistableBranches.end(), HasBBAsSuccessor) == HoistableBranches.end() && \"BB is expected to be the target of at most one branch\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 712, __PRETTY_FUNCTION__)); | ||||
713 | |||||
714 | LLVMContext &C = BB->getContext(); | ||||
715 | BasicBlock *TrueDest = BI->getSuccessor(0); | ||||
716 | BasicBlock *FalseDest = BI->getSuccessor(1); | ||||
717 | BasicBlock *CommonSucc = HoistableBranches[BI]; | ||||
718 | BasicBlock *HoistTarget = getOrCreateHoistedBlock(BI->getParent()); | ||||
719 | |||||
720 | // Create hoisted versions of blocks that currently don't have them | ||||
721 | auto CreateHoistedBlock = [&](BasicBlock *Orig) { | ||||
722 | if (HoistDestinationMap.count(Orig)) | ||||
723 | return HoistDestinationMap[Orig]; | ||||
724 | BasicBlock *New = | ||||
725 | BasicBlock::Create(C, Orig->getName() + ".licm", Orig->getParent()); | ||||
726 | HoistDestinationMap[Orig] = New; | ||||
727 | DT->addNewBlock(New, HoistTarget); | ||||
728 | if (CurLoop->getParentLoop()) | ||||
729 | CurLoop->getParentLoop()->addBasicBlockToLoop(New, *LI); | ||||
730 | ++NumCreatedBlocks; | ||||
731 | LLVM_DEBUG(dbgs() << "LICM created " << New->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM created " << New-> getName() << " as hoist destination for " << Orig ->getName() << "\n"; } } while (false) | ||||
732 | << " as hoist destination for " << Orig->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM created " << New-> getName() << " as hoist destination for " << Orig ->getName() << "\n"; } } while (false) | ||||
733 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM created " << New-> getName() << " as hoist destination for " << Orig ->getName() << "\n"; } } while (false); | ||||
734 | return New; | ||||
735 | }; | ||||
736 | BasicBlock *HoistTrueDest = CreateHoistedBlock(TrueDest); | ||||
737 | BasicBlock *HoistFalseDest = CreateHoistedBlock(FalseDest); | ||||
738 | BasicBlock *HoistCommonSucc = CreateHoistedBlock(CommonSucc); | ||||
739 | |||||
740 | // Link up these blocks with branches. | ||||
741 | if (!HoistCommonSucc->getTerminator()) { | ||||
742 | // The new common successor we've generated will branch to whatever that | ||||
743 | // hoist target branched to. | ||||
744 | BasicBlock *TargetSucc = HoistTarget->getSingleSuccessor(); | ||||
745 | assert(TargetSucc && "Expected hoist target to have a single successor")((TargetSucc && "Expected hoist target to have a single successor" ) ? static_cast<void> (0) : __assert_fail ("TargetSucc && \"Expected hoist target to have a single successor\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 745, __PRETTY_FUNCTION__)); | ||||
746 | HoistCommonSucc->moveBefore(TargetSucc); | ||||
747 | BranchInst::Create(TargetSucc, HoistCommonSucc); | ||||
748 | } | ||||
749 | if (!HoistTrueDest->getTerminator()) { | ||||
750 | HoistTrueDest->moveBefore(HoistCommonSucc); | ||||
751 | BranchInst::Create(HoistCommonSucc, HoistTrueDest); | ||||
752 | } | ||||
753 | if (!HoistFalseDest->getTerminator()) { | ||||
754 | HoistFalseDest->moveBefore(HoistCommonSucc); | ||||
755 | BranchInst::Create(HoistCommonSucc, HoistFalseDest); | ||||
756 | } | ||||
757 | |||||
758 | // If BI is being cloned to what was originally the preheader then | ||||
759 | // HoistCommonSucc will now be the new preheader. | ||||
760 | if (HoistTarget == InitialPreheader) { | ||||
761 | // Phis in the loop header now need to use the new preheader. | ||||
762 | InitialPreheader->replaceSuccessorsPhiUsesWith(HoistCommonSucc); | ||||
763 | if (MSSAU) | ||||
764 | MSSAU->wireOldPredecessorsToNewImmediatePredecessor( | ||||
765 | HoistTarget->getSingleSuccessor(), HoistCommonSucc, {HoistTarget}); | ||||
766 | // The new preheader dominates the loop header. | ||||
767 | DomTreeNode *PreheaderNode = DT->getNode(HoistCommonSucc); | ||||
768 | DomTreeNode *HeaderNode = DT->getNode(CurLoop->getHeader()); | ||||
769 | DT->changeImmediateDominator(HeaderNode, PreheaderNode); | ||||
770 | // The preheader hoist destination is now the new preheader, with the | ||||
771 | // exception of the hoist destination of this branch. | ||||
772 | for (auto &Pair : HoistDestinationMap) | ||||
773 | if (Pair.second == InitialPreheader && Pair.first != BI->getParent()) | ||||
774 | Pair.second = HoistCommonSucc; | ||||
775 | } | ||||
776 | |||||
777 | // Now finally clone BI. | ||||
778 | ReplaceInstWithInst( | ||||
779 | HoistTarget->getTerminator(), | ||||
780 | BranchInst::Create(HoistTrueDest, HoistFalseDest, BI->getCondition())); | ||||
781 | ++NumClonedBranches; | ||||
782 | |||||
783 | assert(CurLoop->getLoopPreheader() &&((CurLoop->getLoopPreheader() && "Hoisting blocks should not have destroyed preheader" ) ? static_cast<void> (0) : __assert_fail ("CurLoop->getLoopPreheader() && \"Hoisting blocks should not have destroyed preheader\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 784, __PRETTY_FUNCTION__)) | ||||
784 | "Hoisting blocks should not have destroyed preheader")((CurLoop->getLoopPreheader() && "Hoisting blocks should not have destroyed preheader" ) ? static_cast<void> (0) : __assert_fail ("CurLoop->getLoopPreheader() && \"Hoisting blocks should not have destroyed preheader\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 784, __PRETTY_FUNCTION__)); | ||||
785 | return HoistDestinationMap[BB]; | ||||
786 | } | ||||
787 | }; | ||||
788 | } // namespace | ||||
789 | |||||
790 | /// Walk the specified region of the CFG (defined by all blocks dominated by | ||||
791 | /// the specified block, and that are in the current loop) in depth first | ||||
792 | /// order w.r.t the DominatorTree. This allows us to visit definitions before | ||||
793 | /// uses, allowing us to hoist a loop body in one pass without iteration. | ||||
794 | /// | ||||
795 | bool llvm::hoistRegion(DomTreeNode *N, AliasAnalysis *AA, LoopInfo *LI, | ||||
796 | DominatorTree *DT, TargetLibraryInfo *TLI, Loop *CurLoop, | ||||
797 | AliasSetTracker *CurAST, MemorySSAUpdater *MSSAU, | ||||
798 | ICFLoopSafetyInfo *SafetyInfo, | ||||
799 | SinkAndHoistLICMFlags &Flags, | ||||
800 | OptimizationRemarkEmitter *ORE) { | ||||
801 | // Verify inputs. | ||||
802 | assert(N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr &&((N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && "Unexpected input to hoistRegion." ) ? static_cast<void> (0) : __assert_fail ("N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected input to hoistRegion.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 804, __PRETTY_FUNCTION__)) | ||||
803 | CurLoop != nullptr && SafetyInfo != nullptr &&((N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && "Unexpected input to hoistRegion." ) ? static_cast<void> (0) : __assert_fail ("N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected input to hoistRegion.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 804, __PRETTY_FUNCTION__)) | ||||
804 | "Unexpected input to hoistRegion.")((N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && "Unexpected input to hoistRegion." ) ? static_cast<void> (0) : __assert_fail ("N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected input to hoistRegion.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 804, __PRETTY_FUNCTION__)); | ||||
805 | assert(((CurAST != nullptr) ^ (MSSAU != nullptr)) &&((((CurAST != nullptr) ^ (MSSAU != nullptr)) && "Either AliasSetTracker or MemorySSA should be initialized." ) ? static_cast<void> (0) : __assert_fail ("((CurAST != nullptr) ^ (MSSAU != nullptr)) && \"Either AliasSetTracker or MemorySSA should be initialized.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 806, __PRETTY_FUNCTION__)) | ||||
806 | "Either AliasSetTracker or MemorySSA should be initialized.")((((CurAST != nullptr) ^ (MSSAU != nullptr)) && "Either AliasSetTracker or MemorySSA should be initialized." ) ? static_cast<void> (0) : __assert_fail ("((CurAST != nullptr) ^ (MSSAU != nullptr)) && \"Either AliasSetTracker or MemorySSA should be initialized.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 806, __PRETTY_FUNCTION__)); | ||||
807 | |||||
808 | ControlFlowHoister CFH(LI, DT, CurLoop, MSSAU); | ||||
809 | |||||
810 | // Keep track of instructions that have been hoisted, as they may need to be | ||||
811 | // re-hoisted if they end up not dominating all of their uses. | ||||
812 | SmallVector<Instruction *, 16> HoistedInstructions; | ||||
813 | |||||
814 | // For PHI hoisting to work we need to hoist blocks before their successors. | ||||
815 | // We can do this by iterating through the blocks in the loop in reverse | ||||
816 | // post-order. | ||||
817 | LoopBlocksRPO Worklist(CurLoop); | ||||
818 | Worklist.perform(LI); | ||||
819 | bool Changed = false; | ||||
820 | for (BasicBlock *BB : Worklist) { | ||||
821 | // Only need to process the contents of this block if it is not part of a | ||||
822 | // subloop (which would already have been processed). | ||||
823 | if (inSubLoop(BB, CurLoop, LI)) | ||||
824 | continue; | ||||
825 | |||||
826 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) { | ||||
827 | Instruction &I = *II++; | ||||
828 | // Try constant folding this instruction. If all the operands are | ||||
829 | // constants, it is technically hoistable, but it would be better to | ||||
830 | // just fold it. | ||||
831 | if (Constant *C = ConstantFoldInstruction( | ||||
832 | &I, I.getModule()->getDataLayout(), TLI)) { | ||||
833 | LLVM_DEBUG(dbgs() << "LICM folding inst: " << I << " --> " << *Cdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM folding inst: " << I << " --> " << *C << '\n'; } } while (false) | ||||
834 | << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM folding inst: " << I << " --> " << *C << '\n'; } } while (false); | ||||
835 | if (CurAST) | ||||
836 | CurAST->copyValue(&I, C); | ||||
837 | // FIXME MSSA: Such replacements may make accesses unoptimized (D51960). | ||||
838 | I.replaceAllUsesWith(C); | ||||
839 | if (isInstructionTriviallyDead(&I, TLI)) | ||||
840 | eraseInstruction(I, *SafetyInfo, CurAST, MSSAU); | ||||
841 | Changed = true; | ||||
842 | continue; | ||||
843 | } | ||||
844 | |||||
845 | // Try hoisting the instruction out to the preheader. We can only do | ||||
846 | // this if all of the operands of the instruction are loop invariant and | ||||
847 | // if it is safe to hoist the instruction. | ||||
848 | // TODO: It may be safe to hoist if we are hoisting to a conditional block | ||||
849 | // and we have accurately duplicated the control flow from the loop header | ||||
850 | // to that block. | ||||
851 | if (CurLoop->hasLoopInvariantOperands(&I) && | ||||
852 | canSinkOrHoistInst(I, AA, DT, CurLoop, CurAST, MSSAU, true, &Flags, | ||||
853 | ORE) && | ||||
854 | isSafeToExecuteUnconditionally( | ||||
855 | I, DT, CurLoop, SafetyInfo, ORE, | ||||
856 | CurLoop->getLoopPreheader()->getTerminator())) { | ||||
857 | hoist(I, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo, | ||||
858 | MSSAU, ORE); | ||||
859 | HoistedInstructions.push_back(&I); | ||||
860 | Changed = true; | ||||
861 | continue; | ||||
862 | } | ||||
863 | |||||
864 | // Attempt to remove floating point division out of the loop by | ||||
865 | // converting it to a reciprocal multiplication. | ||||
866 | if (I.getOpcode() == Instruction::FDiv && | ||||
867 | CurLoop->isLoopInvariant(I.getOperand(1)) && | ||||
868 | I.hasAllowReciprocal()) { | ||||
869 | auto Divisor = I.getOperand(1); | ||||
870 | auto One = llvm::ConstantFP::get(Divisor->getType(), 1.0); | ||||
871 | auto ReciprocalDivisor = BinaryOperator::CreateFDiv(One, Divisor); | ||||
872 | ReciprocalDivisor->setFastMathFlags(I.getFastMathFlags()); | ||||
873 | SafetyInfo->insertInstructionTo(ReciprocalDivisor, I.getParent()); | ||||
874 | ReciprocalDivisor->insertBefore(&I); | ||||
875 | |||||
876 | auto Product = | ||||
877 | BinaryOperator::CreateFMul(I.getOperand(0), ReciprocalDivisor); | ||||
878 | Product->setFastMathFlags(I.getFastMathFlags()); | ||||
879 | SafetyInfo->insertInstructionTo(Product, I.getParent()); | ||||
880 | Product->insertAfter(&I); | ||||
881 | I.replaceAllUsesWith(Product); | ||||
882 | eraseInstruction(I, *SafetyInfo, CurAST, MSSAU); | ||||
883 | |||||
884 | hoist(*ReciprocalDivisor, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), | ||||
885 | SafetyInfo, MSSAU, ORE); | ||||
886 | HoistedInstructions.push_back(ReciprocalDivisor); | ||||
887 | Changed = true; | ||||
888 | continue; | ||||
889 | } | ||||
890 | |||||
891 | auto IsInvariantStart = [&](Instruction &I) { | ||||
892 | using namespace PatternMatch; | ||||
893 | return I.use_empty() && | ||||
894 | match(&I, m_Intrinsic<Intrinsic::invariant_start>()); | ||||
895 | }; | ||||
896 | auto MustExecuteWithoutWritesBefore = [&](Instruction &I) { | ||||
897 | return SafetyInfo->isGuaranteedToExecute(I, DT, CurLoop) && | ||||
898 | SafetyInfo->doesNotWriteMemoryBefore(I, CurLoop); | ||||
899 | }; | ||||
900 | if ((IsInvariantStart(I) || isGuard(&I)) && | ||||
901 | CurLoop->hasLoopInvariantOperands(&I) && | ||||
902 | MustExecuteWithoutWritesBefore(I)) { | ||||
903 | hoist(I, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo, | ||||
904 | MSSAU, ORE); | ||||
905 | HoistedInstructions.push_back(&I); | ||||
906 | Changed = true; | ||||
907 | continue; | ||||
908 | } | ||||
909 | |||||
910 | if (PHINode *PN = dyn_cast<PHINode>(&I)) { | ||||
911 | if (CFH.canHoistPHI(PN)) { | ||||
912 | // Redirect incoming blocks first to ensure that we create hoisted | ||||
913 | // versions of those blocks before we hoist the phi. | ||||
914 | for (unsigned int i = 0; i < PN->getNumIncomingValues(); ++i) | ||||
915 | PN->setIncomingBlock( | ||||
916 | i, CFH.getOrCreateHoistedBlock(PN->getIncomingBlock(i))); | ||||
917 | hoist(*PN, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo, | ||||
918 | MSSAU, ORE); | ||||
919 | assert(DT->dominates(PN, BB) && "Conditional PHIs not expected")((DT->dominates(PN, BB) && "Conditional PHIs not expected" ) ? static_cast<void> (0) : __assert_fail ("DT->dominates(PN, BB) && \"Conditional PHIs not expected\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 919, __PRETTY_FUNCTION__)); | ||||
920 | Changed = true; | ||||
921 | continue; | ||||
922 | } | ||||
923 | } | ||||
924 | |||||
925 | // Remember possibly hoistable branches so we can actually hoist them | ||||
926 | // later if needed. | ||||
927 | if (BranchInst *BI = dyn_cast<BranchInst>(&I)) | ||||
928 | CFH.registerPossiblyHoistableBranch(BI); | ||||
929 | } | ||||
930 | } | ||||
931 | |||||
932 | // If we hoisted instructions to a conditional block they may not dominate | ||||
933 | // their uses that weren't hoisted (such as phis where some operands are not | ||||
934 | // loop invariant). If so make them unconditional by moving them to their | ||||
935 | // immediate dominator. We iterate through the instructions in reverse order | ||||
936 | // which ensures that when we rehoist an instruction we rehoist its operands, | ||||
937 | // and also keep track of where in the block we are rehoisting to to make sure | ||||
938 | // that we rehoist instructions before the instructions that use them. | ||||
939 | Instruction *HoistPoint = nullptr; | ||||
940 | if (ControlFlowHoisting) { | ||||
941 | for (Instruction *I : reverse(HoistedInstructions)) { | ||||
942 | if (!llvm::all_of(I->uses(), | ||||
943 | [&](Use &U) { return DT->dominates(I, U); })) { | ||||
944 | BasicBlock *Dominator = | ||||
945 | DT->getNode(I->getParent())->getIDom()->getBlock(); | ||||
946 | if (!HoistPoint || !DT->dominates(HoistPoint->getParent(), Dominator)) { | ||||
947 | if (HoistPoint) | ||||
948 | assert(DT->dominates(Dominator, HoistPoint->getParent()) &&((DT->dominates(Dominator, HoistPoint->getParent()) && "New hoist point expected to dominate old hoist point") ? static_cast <void> (0) : __assert_fail ("DT->dominates(Dominator, HoistPoint->getParent()) && \"New hoist point expected to dominate old hoist point\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 949, __PRETTY_FUNCTION__)) | ||||
949 | "New hoist point expected to dominate old hoist point")((DT->dominates(Dominator, HoistPoint->getParent()) && "New hoist point expected to dominate old hoist point") ? static_cast <void> (0) : __assert_fail ("DT->dominates(Dominator, HoistPoint->getParent()) && \"New hoist point expected to dominate old hoist point\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 949, __PRETTY_FUNCTION__)); | ||||
950 | HoistPoint = Dominator->getTerminator(); | ||||
951 | } | ||||
952 | LLVM_DEBUG(dbgs() << "LICM rehoisting to "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM rehoisting to " << HoistPoint ->getParent()->getName() << ": " << *I << "\n"; } } while (false) | ||||
953 | << HoistPoint->getParent()->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM rehoisting to " << HoistPoint ->getParent()->getName() << ": " << *I << "\n"; } } while (false) | ||||
954 | << ": " << *I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM rehoisting to " << HoistPoint ->getParent()->getName() << ": " << *I << "\n"; } } while (false); | ||||
955 | moveInstructionBefore(*I, *HoistPoint, *SafetyInfo, MSSAU); | ||||
956 | HoistPoint = I; | ||||
957 | Changed = true; | ||||
958 | } | ||||
959 | } | ||||
960 | } | ||||
961 | if (MSSAU && VerifyMemorySSA) | ||||
962 | MSSAU->getMemorySSA()->verifyMemorySSA(); | ||||
963 | |||||
964 | // Now that we've finished hoisting make sure that LI and DT are still | ||||
965 | // valid. | ||||
966 | #ifdef EXPENSIVE_CHECKS | ||||
967 | if (Changed) { | ||||
968 | assert(DT->verify(DominatorTree::VerificationLevel::Fast) &&((DT->verify(DominatorTree::VerificationLevel::Fast) && "Dominator tree verification failed") ? static_cast<void> (0) : __assert_fail ("DT->verify(DominatorTree::VerificationLevel::Fast) && \"Dominator tree verification failed\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 969, __PRETTY_FUNCTION__)) | ||||
969 | "Dominator tree verification failed")((DT->verify(DominatorTree::VerificationLevel::Fast) && "Dominator tree verification failed") ? static_cast<void> (0) : __assert_fail ("DT->verify(DominatorTree::VerificationLevel::Fast) && \"Dominator tree verification failed\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 969, __PRETTY_FUNCTION__)); | ||||
970 | LI->verify(*DT); | ||||
971 | } | ||||
972 | #endif | ||||
973 | |||||
974 | return Changed; | ||||
975 | } | ||||
976 | |||||
977 | // Return true if LI is invariant within scope of the loop. LI is invariant if | ||||
978 | // CurLoop is dominated by an invariant.start representing the same memory | ||||
979 | // location and size as the memory location LI loads from, and also the | ||||
980 | // invariant.start has no uses. | ||||
981 | static bool isLoadInvariantInLoop(LoadInst *LI, DominatorTree *DT, | ||||
982 | Loop *CurLoop) { | ||||
983 | Value *Addr = LI->getOperand(0); | ||||
984 | const DataLayout &DL = LI->getModule()->getDataLayout(); | ||||
985 | const uint32_t LocSizeInBits = DL.getTypeSizeInBits(LI->getType()); | ||||
986 | |||||
987 | // if the type is i8 addrspace(x)*, we know this is the type of | ||||
988 | // llvm.invariant.start operand | ||||
989 | auto *PtrInt8Ty = PointerType::get(Type::getInt8Ty(LI->getContext()), | ||||
990 | LI->getPointerAddressSpace()); | ||||
991 | unsigned BitcastsVisited = 0; | ||||
992 | // Look through bitcasts until we reach the i8* type (this is invariant.start | ||||
993 | // operand type). | ||||
994 | while (Addr->getType() != PtrInt8Ty) { | ||||
995 | auto *BC = dyn_cast<BitCastInst>(Addr); | ||||
996 | // Avoid traversing high number of bitcast uses. | ||||
997 | if (++BitcastsVisited > MaxNumUsesTraversed || !BC) | ||||
998 | return false; | ||||
999 | Addr = BC->getOperand(0); | ||||
1000 | } | ||||
1001 | |||||
1002 | unsigned UsesVisited = 0; | ||||
1003 | // Traverse all uses of the load operand value, to see if invariant.start is | ||||
1004 | // one of the uses, and whether it dominates the load instruction. | ||||
1005 | for (auto *U : Addr->users()) { | ||||
1006 | // Avoid traversing for Load operand with high number of users. | ||||
1007 | if (++UsesVisited > MaxNumUsesTraversed) | ||||
1008 | return false; | ||||
1009 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(U); | ||||
1010 | // If there are escaping uses of invariant.start instruction, the load maybe | ||||
1011 | // non-invariant. | ||||
1012 | if (!II || II->getIntrinsicID() != Intrinsic::invariant_start || | ||||
1013 | !II->use_empty()) | ||||
1014 | continue; | ||||
1015 | unsigned InvariantSizeInBits = | ||||
1016 | cast<ConstantInt>(II->getArgOperand(0))->getSExtValue() * 8; | ||||
1017 | // Confirm the invariant.start location size contains the load operand size | ||||
1018 | // in bits. Also, the invariant.start should dominate the load, and we | ||||
1019 | // should not hoist the load out of a loop that contains this dominating | ||||
1020 | // invariant.start. | ||||
1021 | if (LocSizeInBits <= InvariantSizeInBits && | ||||
1022 | DT->properlyDominates(II->getParent(), CurLoop->getHeader())) | ||||
1023 | return true; | ||||
1024 | } | ||||
1025 | |||||
1026 | return false; | ||||
1027 | } | ||||
1028 | |||||
1029 | namespace { | ||||
1030 | /// Return true if-and-only-if we know how to (mechanically) both hoist and | ||||
1031 | /// sink a given instruction out of a loop. Does not address legality | ||||
1032 | /// concerns such as aliasing or speculation safety. | ||||
1033 | bool isHoistableAndSinkableInst(Instruction &I) { | ||||
1034 | // Only these instructions are hoistable/sinkable. | ||||
1035 | return (isa<LoadInst>(I) || isa<StoreInst>(I) || isa<CallInst>(I) || | ||||
1036 | isa<FenceInst>(I) || isa<CastInst>(I) || | ||||
1037 | isa<UnaryOperator>(I) || isa<BinaryOperator>(I) || | ||||
1038 | isa<SelectInst>(I) || isa<GetElementPtrInst>(I) || isa<CmpInst>(I) || | ||||
1039 | isa<InsertElementInst>(I) || isa<ExtractElementInst>(I) || | ||||
1040 | isa<ShuffleVectorInst>(I) || isa<ExtractValueInst>(I) || | ||||
1041 | isa<InsertValueInst>(I)); | ||||
1042 | } | ||||
1043 | /// Return true if all of the alias sets within this AST are known not to | ||||
1044 | /// contain a Mod, or if MSSA knows thare are no MemoryDefs in the loop. | ||||
1045 | bool isReadOnly(AliasSetTracker *CurAST, const MemorySSAUpdater *MSSAU, | ||||
1046 | const Loop *L) { | ||||
1047 | if (CurAST) { | ||||
1048 | for (AliasSet &AS : *CurAST) { | ||||
1049 | if (!AS.isForwardingAliasSet() && AS.isMod()) { | ||||
1050 | return false; | ||||
1051 | } | ||||
1052 | } | ||||
1053 | return true; | ||||
1054 | } else { /*MSSAU*/ | ||||
1055 | for (auto *BB : L->getBlocks()) | ||||
1056 | if (MSSAU->getMemorySSA()->getBlockDefs(BB)) | ||||
1057 | return false; | ||||
1058 | return true; | ||||
1059 | } | ||||
1060 | } | ||||
1061 | |||||
1062 | /// Return true if I is the only Instruction with a MemoryAccess in L. | ||||
1063 | bool isOnlyMemoryAccess(const Instruction *I, const Loop *L, | ||||
1064 | const MemorySSAUpdater *MSSAU) { | ||||
1065 | for (auto *BB : L->getBlocks()) | ||||
1066 | if (auto *Accs = MSSAU->getMemorySSA()->getBlockAccesses(BB)) { | ||||
1067 | int NotAPhi = 0; | ||||
1068 | for (const auto &Acc : *Accs) { | ||||
1069 | if (isa<MemoryPhi>(&Acc)) | ||||
1070 | continue; | ||||
1071 | const auto *MUD = cast<MemoryUseOrDef>(&Acc); | ||||
1072 | if (MUD->getMemoryInst() != I || NotAPhi++ == 1) | ||||
1073 | return false; | ||||
1074 | } | ||||
1075 | } | ||||
1076 | return true; | ||||
1077 | } | ||||
1078 | } | ||||
1079 | |||||
1080 | bool llvm::canSinkOrHoistInst(Instruction &I, AAResults *AA, DominatorTree *DT, | ||||
1081 | Loop *CurLoop, AliasSetTracker *CurAST, | ||||
1082 | MemorySSAUpdater *MSSAU, | ||||
1083 | bool TargetExecutesOncePerLoop, | ||||
1084 | SinkAndHoistLICMFlags *Flags, | ||||
1085 | OptimizationRemarkEmitter *ORE) { | ||||
1086 | // If we don't understand the instruction, bail early. | ||||
1087 | if (!isHoistableAndSinkableInst(I)) | ||||
| |||||
1088 | return false; | ||||
1089 | |||||
1090 | MemorySSA *MSSA = MSSAU ? MSSAU->getMemorySSA() : nullptr; | ||||
1091 | if (MSSA) | ||||
1092 | assert(Flags != nullptr && "Flags cannot be null.")((Flags != nullptr && "Flags cannot be null.") ? static_cast <void> (0) : __assert_fail ("Flags != nullptr && \"Flags cannot be null.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 1092, __PRETTY_FUNCTION__)); | ||||
1093 | |||||
1094 | // Loads have extra constraints we have to verify before we can hoist them. | ||||
1095 | if (LoadInst *LI
| ||||
1096 | if (!LI->isUnordered()) | ||||
1097 | return false; // Don't sink/hoist volatile or ordered atomic loads! | ||||
1098 | |||||
1099 | // Loads from constant memory are always safe to move, even if they end up | ||||
1100 | // in the same alias set as something that ends up being modified. | ||||
1101 | if (AA->pointsToConstantMemory(LI->getOperand(0))) | ||||
1102 | return true; | ||||
1103 | if (LI->hasMetadata(LLVMContext::MD_invariant_load)) | ||||
1104 | return true; | ||||
1105 | |||||
1106 | if (LI->isAtomic() && !TargetExecutesOncePerLoop) | ||||
1107 | return false; // Don't risk duplicating unordered loads | ||||
1108 | |||||
1109 | // This checks for an invariant.start dominating the load. | ||||
1110 | if (isLoadInvariantInLoop(LI, DT, CurLoop)) | ||||
1111 | return true; | ||||
1112 | |||||
1113 | bool Invalidated; | ||||
1114 | if (CurAST) | ||||
1115 | Invalidated = pointerInvalidatedByLoop(MemoryLocation::get(LI), CurAST, | ||||
1116 | CurLoop, AA); | ||||
1117 | else | ||||
1118 | Invalidated = pointerInvalidatedByLoopWithMSSA( | ||||
1119 | MSSA, cast<MemoryUse>(MSSA->getMemoryAccess(LI)), CurLoop, *Flags); | ||||
1120 | // Check loop-invariant address because this may also be a sinkable load | ||||
1121 | // whose address is not necessarily loop-invariant. | ||||
1122 | if (ORE && Invalidated && CurLoop->isLoopInvariant(LI->getPointerOperand())) | ||||
1123 | ORE->emit([&]() { | ||||
1124 | return OptimizationRemarkMissed( | ||||
1125 | DEBUG_TYPE"licm", "LoadWithLoopInvariantAddressInvalidated", LI) | ||||
1126 | << "failed to move load with loop-invariant address " | ||||
1127 | "because the loop may invalidate its value"; | ||||
1128 | }); | ||||
1129 | |||||
1130 | return !Invalidated; | ||||
1131 | } else if (CallInst *CI
| ||||
1132 | // Don't sink or hoist dbg info; it's legal, but not useful. | ||||
1133 | if (isa<DbgInfoIntrinsic>(I)) | ||||
1134 | return false; | ||||
1135 | |||||
1136 | // Don't sink calls which can throw. | ||||
1137 | if (CI->mayThrow()) | ||||
1138 | return false; | ||||
1139 | |||||
1140 | using namespace PatternMatch; | ||||
1141 | if (match(CI, m_Intrinsic<Intrinsic::assume>())) | ||||
1142 | // Assumes don't actually alias anything or throw | ||||
1143 | return true; | ||||
1144 | |||||
1145 | // Handle simple cases by querying alias analysis. | ||||
1146 | FunctionModRefBehavior Behavior = AA->getModRefBehavior(CI); | ||||
1147 | if (Behavior == FMRB_DoesNotAccessMemory) | ||||
1148 | return true; | ||||
1149 | if (AliasAnalysis::onlyReadsMemory(Behavior)) { | ||||
1150 | // A readonly argmemonly function only reads from memory pointed to by | ||||
1151 | // it's arguments with arbitrary offsets. If we can prove there are no | ||||
1152 | // writes to this memory in the loop, we can hoist or sink. | ||||
1153 | if (AliasAnalysis::onlyAccessesArgPointees(Behavior)) { | ||||
1154 | // TODO: expand to writeable arguments | ||||
1155 | for (Value *Op : CI->arg_operands()) | ||||
1156 | if (Op->getType()->isPointerTy()) { | ||||
1157 | bool Invalidated; | ||||
1158 | if (CurAST) | ||||
1159 | Invalidated = pointerInvalidatedByLoop( | ||||
1160 | MemoryLocation(Op, LocationSize::unknown(), AAMDNodes()), | ||||
1161 | CurAST, CurLoop, AA); | ||||
1162 | else | ||||
1163 | Invalidated = pointerInvalidatedByLoopWithMSSA( | ||||
1164 | MSSA, cast<MemoryUse>(MSSA->getMemoryAccess(CI)), CurLoop, | ||||
1165 | *Flags); | ||||
1166 | if (Invalidated) | ||||
1167 | return false; | ||||
1168 | } | ||||
1169 | return true; | ||||
1170 | } | ||||
1171 | |||||
1172 | // If this call only reads from memory and there are no writes to memory | ||||
1173 | // in the loop, we can hoist or sink the call as appropriate. | ||||
1174 | if (isReadOnly(CurAST, MSSAU, CurLoop)) | ||||
1175 | return true; | ||||
1176 | } | ||||
1177 | |||||
1178 | // FIXME: This should use mod/ref information to see if we can hoist or | ||||
1179 | // sink the call. | ||||
1180 | |||||
1181 | return false; | ||||
1182 | } else if (auto *FI
| ||||
1183 | // Fences alias (most) everything to provide ordering. For the moment, | ||||
1184 | // just give up if there are any other memory operations in the loop. | ||||
1185 | if (CurAST) { | ||||
1186 | auto Begin = CurAST->begin(); | ||||
1187 | assert(Begin != CurAST->end() && "must contain FI")((Begin != CurAST->end() && "must contain FI") ? static_cast <void> (0) : __assert_fail ("Begin != CurAST->end() && \"must contain FI\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 1187, __PRETTY_FUNCTION__)); | ||||
1188 | if (std::next(Begin) != CurAST->end()) | ||||
1189 | // constant memory for instance, TODO: handle better | ||||
1190 | return false; | ||||
1191 | auto *UniqueI = Begin->getUniqueInstruction(); | ||||
1192 | if (!UniqueI) | ||||
1193 | // other memory op, give up | ||||
1194 | return false; | ||||
1195 | (void)FI; // suppress unused variable warning | ||||
1196 | assert(UniqueI == FI && "AS must contain FI")((UniqueI == FI && "AS must contain FI") ? static_cast <void> (0) : __assert_fail ("UniqueI == FI && \"AS must contain FI\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 1196, __PRETTY_FUNCTION__)); | ||||
1197 | return true; | ||||
1198 | } else // MSSAU | ||||
1199 | return isOnlyMemoryAccess(FI, CurLoop, MSSAU); | ||||
1200 | } else if (auto *SI
| ||||
1201 | if (!SI->isUnordered()) | ||||
1202 | return false; // Don't sink/hoist volatile or ordered atomic store! | ||||
1203 | |||||
1204 | // We can only hoist a store that we can prove writes a value which is not | ||||
1205 | // read or overwritten within the loop. For those cases, we fallback to | ||||
1206 | // load store promotion instead. TODO: We can extend this to cases where | ||||
1207 | // there is exactly one write to the location and that write dominates an | ||||
1208 | // arbitrary number of reads in the loop. | ||||
1209 | if (CurAST) { | ||||
1210 | auto &AS = CurAST->getAliasSetFor(MemoryLocation::get(SI)); | ||||
1211 | |||||
1212 | if (AS.isRef() || !AS.isMustAlias()) | ||||
1213 | // Quick exit test, handled by the full path below as well. | ||||
1214 | return false; | ||||
1215 | auto *UniqueI = AS.getUniqueInstruction(); | ||||
1216 | if (!UniqueI) | ||||
1217 | // other memory op, give up | ||||
1218 | return false; | ||||
1219 | assert(UniqueI == SI && "AS must contain SI")((UniqueI == SI && "AS must contain SI") ? static_cast <void> (0) : __assert_fail ("UniqueI == SI && \"AS must contain SI\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 1219, __PRETTY_FUNCTION__)); | ||||
1220 | return true; | ||||
1221 | } else { // MSSAU | ||||
1222 | if (isOnlyMemoryAccess(SI, CurLoop, MSSAU)) | ||||
1223 | return true; | ||||
1224 | // If there are more accesses than the Promotion cap, give up, we're not | ||||
1225 | // walking a list that long. | ||||
1226 | if (Flags->NoOfMemAccTooLarge) | ||||
1227 | return false; | ||||
1228 | // Check store only if there's still "quota" to check clobber. | ||||
1229 | if (Flags->LicmMssaOptCounter >= Flags->LicmMssaOptCap) | ||||
1230 | return false; | ||||
1231 | // If there are interfering Uses (i.e. their defining access is in the | ||||
1232 | // loop), or ordered loads (stored as Defs!), don't move this store. | ||||
1233 | // Could do better here, but this is conservatively correct. | ||||
1234 | // TODO: Cache set of Uses on the first walk in runOnLoop, update when | ||||
1235 | // moving accesses. Can also extend to dominating uses. | ||||
1236 | auto *SIMD = MSSA->getMemoryAccess(SI); | ||||
| |||||
1237 | for (auto *BB : CurLoop->getBlocks()) | ||||
1238 | if (auto *Accesses = MSSA->getBlockAccesses(BB)) { | ||||
1239 | for (const auto &MA : *Accesses) | ||||
1240 | if (const auto *MU = dyn_cast<MemoryUse>(&MA)) { | ||||
1241 | auto *MD = MU->getDefiningAccess(); | ||||
1242 | if (!MSSA->isLiveOnEntryDef(MD) && | ||||
1243 | CurLoop->contains(MD->getBlock())) | ||||
1244 | return false; | ||||
1245 | // Disable hoisting past potentially interfering loads. Optimized | ||||
1246 | // Uses may point to an access outside the loop, as getClobbering | ||||
1247 | // checks the previous iteration when walking the backedge. | ||||
1248 | // FIXME: More precise: no Uses that alias SI. | ||||
1249 | if (!Flags->IsSink && !MSSA->dominates(SIMD, MU)) | ||||
1250 | return false; | ||||
1251 | } else if (const auto *MD = dyn_cast<MemoryDef>(&MA)) { | ||||
1252 | if (auto *LI = dyn_cast<LoadInst>(MD->getMemoryInst())) { | ||||
1253 | (void)LI; // Silence warning. | ||||
1254 | assert(!LI->isUnordered() && "Expected unordered load")((!LI->isUnordered() && "Expected unordered load") ? static_cast<void> (0) : __assert_fail ("!LI->isUnordered() && \"Expected unordered load\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 1254, __PRETTY_FUNCTION__)); | ||||
1255 | return false; | ||||
1256 | } | ||||
1257 | // Any call, while it may not be clobbering SI, it may be a use. | ||||
1258 | if (auto *CI = dyn_cast<CallInst>(MD->getMemoryInst())) { | ||||
1259 | // Check if the call may read from the memory locattion written | ||||
1260 | // to by SI. Check CI's attributes and arguments; the number of | ||||
1261 | // such checks performed is limited above by NoOfMemAccTooLarge. | ||||
1262 | ModRefInfo MRI = AA->getModRefInfo(CI, MemoryLocation::get(SI)); | ||||
1263 | if (isModOrRefSet(MRI)) | ||||
1264 | return false; | ||||
1265 | } | ||||
1266 | } | ||||
1267 | } | ||||
1268 | |||||
1269 | auto *Source = MSSA->getSkipSelfWalker()->getClobberingMemoryAccess(SI); | ||||
1270 | Flags->LicmMssaOptCounter++; | ||||
1271 | // If there are no clobbering Defs in the loop, store is safe to hoist. | ||||
1272 | return MSSA->isLiveOnEntryDef(Source) || | ||||
1273 | !CurLoop->contains(Source->getBlock()); | ||||
1274 | } | ||||
1275 | } | ||||
1276 | |||||
1277 | assert(!I.mayReadOrWriteMemory() && "unhandled aliasing")((!I.mayReadOrWriteMemory() && "unhandled aliasing") ? static_cast<void> (0) : __assert_fail ("!I.mayReadOrWriteMemory() && \"unhandled aliasing\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 1277, __PRETTY_FUNCTION__)); | ||||
1278 | |||||
1279 | // We've established mechanical ability and aliasing, it's up to the caller | ||||
1280 | // to check fault safety | ||||
1281 | return true; | ||||
1282 | } | ||||
1283 | |||||
1284 | /// Returns true if a PHINode is a trivially replaceable with an | ||||
1285 | /// Instruction. | ||||
1286 | /// This is true when all incoming values are that instruction. | ||||
1287 | /// This pattern occurs most often with LCSSA PHI nodes. | ||||
1288 | /// | ||||
1289 | static bool isTriviallyReplaceablePHI(const PHINode &PN, const Instruction &I) { | ||||
1290 | for (const Value *IncValue : PN.incoming_values()) | ||||
1291 | if (IncValue != &I) | ||||
1292 | return false; | ||||
1293 | |||||
1294 | return true; | ||||
1295 | } | ||||
1296 | |||||
1297 | /// Return true if the instruction is free in the loop. | ||||
1298 | static bool isFreeInLoop(const Instruction &I, const Loop *CurLoop, | ||||
1299 | const TargetTransformInfo *TTI) { | ||||
1300 | |||||
1301 | if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) { | ||||
1302 | if (TTI->getUserCost(GEP) != TargetTransformInfo::TCC_Free) | ||||
1303 | return false; | ||||
1304 | // For a GEP, we cannot simply use getUserCost because currently it | ||||
1305 | // optimistically assume that a GEP will fold into addressing mode | ||||
1306 | // regardless of its users. | ||||
1307 | const BasicBlock *BB = GEP->getParent(); | ||||
1308 | for (const User *U : GEP->users()) { | ||||
1309 | const Instruction *UI = cast<Instruction>(U); | ||||
1310 | if (CurLoop->contains(UI) && | ||||
1311 | (BB != UI->getParent() || | ||||
1312 | (!isa<StoreInst>(UI) && !isa<LoadInst>(UI)))) | ||||
1313 | return false; | ||||
1314 | } | ||||
1315 | return true; | ||||
1316 | } else | ||||
1317 | return TTI->getUserCost(&I) == TargetTransformInfo::TCC_Free; | ||||
1318 | } | ||||
1319 | |||||
1320 | /// Return true if the only users of this instruction are outside of | ||||
1321 | /// the loop. If this is true, we can sink the instruction to the exit | ||||
1322 | /// blocks of the loop. | ||||
1323 | /// | ||||
1324 | /// We also return true if the instruction could be folded away in lowering. | ||||
1325 | /// (e.g., a GEP can be folded into a load as an addressing mode in the loop). | ||||
1326 | static bool isNotUsedOrFreeInLoop(const Instruction &I, const Loop *CurLoop, | ||||
1327 | const LoopSafetyInfo *SafetyInfo, | ||||
1328 | TargetTransformInfo *TTI, bool &FreeInLoop) { | ||||
1329 | const auto &BlockColors = SafetyInfo->getBlockColors(); | ||||
1330 | bool IsFree = isFreeInLoop(I, CurLoop, TTI); | ||||
1331 | for (const User *U : I.users()) { | ||||
1332 | const Instruction *UI = cast<Instruction>(U); | ||||
1333 | if (const PHINode *PN = dyn_cast<PHINode>(UI)) { | ||||
1334 | const BasicBlock *BB = PN->getParent(); | ||||
1335 | // We cannot sink uses in catchswitches. | ||||
1336 | if (isa<CatchSwitchInst>(BB->getTerminator())) | ||||
1337 | return false; | ||||
1338 | |||||
1339 | // We need to sink a callsite to a unique funclet. Avoid sinking if the | ||||
1340 | // phi use is too muddled. | ||||
1341 | if (isa<CallInst>(I)) | ||||
1342 | if (!BlockColors.empty() && | ||||
1343 | BlockColors.find(const_cast<BasicBlock *>(BB))->second.size() != 1) | ||||
1344 | return false; | ||||
1345 | } | ||||
1346 | |||||
1347 | if (CurLoop->contains(UI)) { | ||||
1348 | if (IsFree) { | ||||
1349 | FreeInLoop = true; | ||||
1350 | continue; | ||||
1351 | } | ||||
1352 | return false; | ||||
1353 | } | ||||
1354 | } | ||||
1355 | return true; | ||||
1356 | } | ||||
1357 | |||||
1358 | static Instruction *CloneInstructionInExitBlock( | ||||
1359 | Instruction &I, BasicBlock &ExitBlock, PHINode &PN, const LoopInfo *LI, | ||||
1360 | const LoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU) { | ||||
1361 | Instruction *New; | ||||
1362 | if (auto *CI = dyn_cast<CallInst>(&I)) { | ||||
1363 | const auto &BlockColors = SafetyInfo->getBlockColors(); | ||||
1364 | |||||
1365 | // Sinking call-sites need to be handled differently from other | ||||
1366 | // instructions. The cloned call-site needs a funclet bundle operand | ||||
1367 | // appropriate for its location in the CFG. | ||||
1368 | SmallVector<OperandBundleDef, 1> OpBundles; | ||||
1369 | for (unsigned BundleIdx = 0, BundleEnd = CI->getNumOperandBundles(); | ||||
1370 | BundleIdx != BundleEnd; ++BundleIdx) { | ||||
1371 | OperandBundleUse Bundle = CI->getOperandBundleAt(BundleIdx); | ||||
1372 | if (Bundle.getTagID() == LLVMContext::OB_funclet) | ||||
1373 | continue; | ||||
1374 | |||||
1375 | OpBundles.emplace_back(Bundle); | ||||
1376 | } | ||||
1377 | |||||
1378 | if (!BlockColors.empty()) { | ||||
1379 | const ColorVector &CV = BlockColors.find(&ExitBlock)->second; | ||||
1380 | assert(CV.size() == 1 && "non-unique color for exit block!")((CV.size() == 1 && "non-unique color for exit block!" ) ? static_cast<void> (0) : __assert_fail ("CV.size() == 1 && \"non-unique color for exit block!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 1380, __PRETTY_FUNCTION__)); | ||||
1381 | BasicBlock *BBColor = CV.front(); | ||||
1382 | Instruction *EHPad = BBColor->getFirstNonPHI(); | ||||
1383 | if (EHPad->isEHPad()) | ||||
1384 | OpBundles.emplace_back("funclet", EHPad); | ||||
1385 | } | ||||
1386 | |||||
1387 | New = CallInst::Create(CI, OpBundles); | ||||
1388 | } else { | ||||
1389 | New = I.clone(); | ||||
1390 | } | ||||
1391 | |||||
1392 | ExitBlock.getInstList().insert(ExitBlock.getFirstInsertionPt(), New); | ||||
1393 | if (!I.getName().empty()) | ||||
1394 | New->setName(I.getName() + ".le"); | ||||
1395 | |||||
1396 | if (MSSAU && MSSAU->getMemorySSA()->getMemoryAccess(&I)) { | ||||
1397 | // Create a new MemoryAccess and let MemorySSA set its defining access. | ||||
1398 | MemoryAccess *NewMemAcc = MSSAU->createMemoryAccessInBB( | ||||
1399 | New, nullptr, New->getParent(), MemorySSA::Beginning); | ||||
1400 | if (NewMemAcc) { | ||||
1401 | if (auto *MemDef = dyn_cast<MemoryDef>(NewMemAcc)) | ||||
1402 | MSSAU->insertDef(MemDef, /*RenameUses=*/true); | ||||
1403 | else { | ||||
1404 | auto *MemUse = cast<MemoryUse>(NewMemAcc); | ||||
1405 | MSSAU->insertUse(MemUse, /*RenameUses=*/true); | ||||
1406 | } | ||||
1407 | } | ||||
1408 | } | ||||
1409 | |||||
1410 | // Build LCSSA PHI nodes for any in-loop operands. Note that this is | ||||
1411 | // particularly cheap because we can rip off the PHI node that we're | ||||
1412 | // replacing for the number and blocks of the predecessors. | ||||
1413 | // OPT: If this shows up in a profile, we can instead finish sinking all | ||||
1414 | // invariant instructions, and then walk their operands to re-establish | ||||
1415 | // LCSSA. That will eliminate creating PHI nodes just to nuke them when | ||||
1416 | // sinking bottom-up. | ||||
1417 | for (User::op_iterator OI = New->op_begin(), OE = New->op_end(); OI != OE; | ||||
1418 | ++OI) | ||||
1419 | if (Instruction *OInst = dyn_cast<Instruction>(*OI)) | ||||
1420 | if (Loop *OLoop = LI->getLoopFor(OInst->getParent())) | ||||
1421 | if (!OLoop->contains(&PN)) { | ||||
1422 | PHINode *OpPN = | ||||
1423 | PHINode::Create(OInst->getType(), PN.getNumIncomingValues(), | ||||
1424 | OInst->getName() + ".lcssa", &ExitBlock.front()); | ||||
1425 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) | ||||
1426 | OpPN->addIncoming(OInst, PN.getIncomingBlock(i)); | ||||
1427 | *OI = OpPN; | ||||
1428 | } | ||||
1429 | return New; | ||||
1430 | } | ||||
1431 | |||||
1432 | static void eraseInstruction(Instruction &I, ICFLoopSafetyInfo &SafetyInfo, | ||||
1433 | AliasSetTracker *AST, MemorySSAUpdater *MSSAU) { | ||||
1434 | if (AST) | ||||
1435 | AST->deleteValue(&I); | ||||
1436 | if (MSSAU) | ||||
1437 | MSSAU->removeMemoryAccess(&I); | ||||
1438 | SafetyInfo.removeInstruction(&I); | ||||
1439 | I.eraseFromParent(); | ||||
1440 | } | ||||
1441 | |||||
1442 | static void moveInstructionBefore(Instruction &I, Instruction &Dest, | ||||
1443 | ICFLoopSafetyInfo &SafetyInfo, | ||||
1444 | MemorySSAUpdater *MSSAU) { | ||||
1445 | SafetyInfo.removeInstruction(&I); | ||||
1446 | SafetyInfo.insertInstructionTo(&I, Dest.getParent()); | ||||
1447 | I.moveBefore(&Dest); | ||||
1448 | if (MSSAU) | ||||
1449 | if (MemoryUseOrDef *OldMemAcc = cast_or_null<MemoryUseOrDef>( | ||||
1450 | MSSAU->getMemorySSA()->getMemoryAccess(&I))) | ||||
1451 | MSSAU->moveToPlace(OldMemAcc, Dest.getParent(), MemorySSA::End); | ||||
1452 | } | ||||
1453 | |||||
1454 | static Instruction *sinkThroughTriviallyReplaceablePHI( | ||||
1455 | PHINode *TPN, Instruction *I, LoopInfo *LI, | ||||
1456 | SmallDenseMap<BasicBlock *, Instruction *, 32> &SunkCopies, | ||||
1457 | const LoopSafetyInfo *SafetyInfo, const Loop *CurLoop, | ||||
1458 | MemorySSAUpdater *MSSAU) { | ||||
1459 | assert(isTriviallyReplaceablePHI(*TPN, *I) &&((isTriviallyReplaceablePHI(*TPN, *I) && "Expect only trivially replaceable PHI" ) ? static_cast<void> (0) : __assert_fail ("isTriviallyReplaceablePHI(*TPN, *I) && \"Expect only trivially replaceable PHI\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 1460, __PRETTY_FUNCTION__)) | ||||
1460 | "Expect only trivially replaceable PHI")((isTriviallyReplaceablePHI(*TPN, *I) && "Expect only trivially replaceable PHI" ) ? static_cast<void> (0) : __assert_fail ("isTriviallyReplaceablePHI(*TPN, *I) && \"Expect only trivially replaceable PHI\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 1460, __PRETTY_FUNCTION__)); | ||||
1461 | BasicBlock *ExitBlock = TPN->getParent(); | ||||
1462 | Instruction *New; | ||||
1463 | auto It = SunkCopies.find(ExitBlock); | ||||
1464 | if (It != SunkCopies.end()) | ||||
1465 | New = It->second; | ||||
1466 | else | ||||
1467 | New = SunkCopies[ExitBlock] = CloneInstructionInExitBlock( | ||||
1468 | *I, *ExitBlock, *TPN, LI, SafetyInfo, MSSAU); | ||||
1469 | return New; | ||||
1470 | } | ||||
1471 | |||||
1472 | static bool canSplitPredecessors(PHINode *PN, LoopSafetyInfo *SafetyInfo) { | ||||
1473 | BasicBlock *BB = PN->getParent(); | ||||
1474 | if (!BB->canSplitPredecessors()) | ||||
1475 | return false; | ||||
1476 | // It's not impossible to split EHPad blocks, but if BlockColors already exist | ||||
1477 | // it require updating BlockColors for all offspring blocks accordingly. By | ||||
1478 | // skipping such corner case, we can make updating BlockColors after splitting | ||||
1479 | // predecessor fairly simple. | ||||
1480 | if (!SafetyInfo->getBlockColors().empty() && BB->getFirstNonPHI()->isEHPad()) | ||||
1481 | return false; | ||||
1482 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { | ||||
1483 | BasicBlock *BBPred = *PI; | ||||
1484 | if (isa<IndirectBrInst>(BBPred->getTerminator())) | ||||
1485 | return false; | ||||
1486 | } | ||||
1487 | return true; | ||||
1488 | } | ||||
1489 | |||||
1490 | static void splitPredecessorsOfLoopExit(PHINode *PN, DominatorTree *DT, | ||||
1491 | LoopInfo *LI, const Loop *CurLoop, | ||||
1492 | LoopSafetyInfo *SafetyInfo, | ||||
1493 | MemorySSAUpdater *MSSAU) { | ||||
1494 | #ifndef NDEBUG | ||||
1495 | SmallVector<BasicBlock *, 32> ExitBlocks; | ||||
1496 | CurLoop->getUniqueExitBlocks(ExitBlocks); | ||||
1497 | SmallPtrSet<BasicBlock *, 32> ExitBlockSet(ExitBlocks.begin(), | ||||
1498 | ExitBlocks.end()); | ||||
1499 | #endif | ||||
1500 | BasicBlock *ExitBB = PN->getParent(); | ||||
1501 | assert(ExitBlockSet.count(ExitBB) && "Expect the PHI is in an exit block.")((ExitBlockSet.count(ExitBB) && "Expect the PHI is in an exit block." ) ? static_cast<void> (0) : __assert_fail ("ExitBlockSet.count(ExitBB) && \"Expect the PHI is in an exit block.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 1501, __PRETTY_FUNCTION__)); | ||||
1502 | |||||
1503 | // Split predecessors of the loop exit to make instructions in the loop are | ||||
1504 | // exposed to exit blocks through trivially replaceable PHIs while keeping the | ||||
1505 | // loop in the canonical form where each predecessor of each exit block should | ||||
1506 | // be contained within the loop. For example, this will convert the loop below | ||||
1507 | // from | ||||
1508 | // | ||||
1509 | // LB1: | ||||
1510 | // %v1 = | ||||
1511 | // br %LE, %LB2 | ||||
1512 | // LB2: | ||||
1513 | // %v2 = | ||||
1514 | // br %LE, %LB1 | ||||
1515 | // LE: | ||||
1516 | // %p = phi [%v1, %LB1], [%v2, %LB2] <-- non-trivially replaceable | ||||
1517 | // | ||||
1518 | // to | ||||
1519 | // | ||||
1520 | // LB1: | ||||
1521 | // %v1 = | ||||
1522 | // br %LE.split, %LB2 | ||||
1523 | // LB2: | ||||
1524 | // %v2 = | ||||
1525 | // br %LE.split2, %LB1 | ||||
1526 | // LE.split: | ||||
1527 | // %p1 = phi [%v1, %LB1] <-- trivially replaceable | ||||
1528 | // br %LE | ||||
1529 | // LE.split2: | ||||
1530 | // %p2 = phi [%v2, %LB2] <-- trivially replaceable | ||||
1531 | // br %LE | ||||
1532 | // LE: | ||||
1533 | // %p = phi [%p1, %LE.split], [%p2, %LE.split2] | ||||
1534 | // | ||||
1535 | const auto &BlockColors = SafetyInfo->getBlockColors(); | ||||
1536 | SmallSetVector<BasicBlock *, 8> PredBBs(pred_begin(ExitBB), pred_end(ExitBB)); | ||||
1537 | while (!PredBBs.empty()) { | ||||
1538 | BasicBlock *PredBB = *PredBBs.begin(); | ||||
1539 | assert(CurLoop->contains(PredBB) &&((CurLoop->contains(PredBB) && "Expect all predecessors are in the loop" ) ? static_cast<void> (0) : __assert_fail ("CurLoop->contains(PredBB) && \"Expect all predecessors are in the loop\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 1540, __PRETTY_FUNCTION__)) | ||||
1540 | "Expect all predecessors are in the loop")((CurLoop->contains(PredBB) && "Expect all predecessors are in the loop" ) ? static_cast<void> (0) : __assert_fail ("CurLoop->contains(PredBB) && \"Expect all predecessors are in the loop\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 1540, __PRETTY_FUNCTION__)); | ||||
1541 | if (PN->getBasicBlockIndex(PredBB) >= 0) { | ||||
1542 | BasicBlock *NewPred = SplitBlockPredecessors( | ||||
1543 | ExitBB, PredBB, ".split.loop.exit", DT, LI, MSSAU, true); | ||||
1544 | // Since we do not allow splitting EH-block with BlockColors in | ||||
1545 | // canSplitPredecessors(), we can simply assign predecessor's color to | ||||
1546 | // the new block. | ||||
1547 | if (!BlockColors.empty()) | ||||
1548 | // Grab a reference to the ColorVector to be inserted before getting the | ||||
1549 | // reference to the vector we are copying because inserting the new | ||||
1550 | // element in BlockColors might cause the map to be reallocated. | ||||
1551 | SafetyInfo->copyColors(NewPred, PredBB); | ||||
1552 | } | ||||
1553 | PredBBs.remove(PredBB); | ||||
1554 | } | ||||
1555 | } | ||||
1556 | |||||
1557 | /// When an instruction is found to only be used outside of the loop, this | ||||
1558 | /// function moves it to the exit blocks and patches up SSA form as needed. | ||||
1559 | /// This method is guaranteed to remove the original instruction from its | ||||
1560 | /// position, and may either delete it or move it to outside of the loop. | ||||
1561 | /// | ||||
1562 | static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT, | ||||
1563 | const Loop *CurLoop, ICFLoopSafetyInfo *SafetyInfo, | ||||
1564 | MemorySSAUpdater *MSSAU, OptimizationRemarkEmitter *ORE) { | ||||
1565 | LLVM_DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM sinking instruction: " << I << "\n"; } } while (false); | ||||
1566 | ORE->emit([&]() { | ||||
1567 | return OptimizationRemark(DEBUG_TYPE"licm", "InstSunk", &I) | ||||
1568 | << "sinking " << ore::NV("Inst", &I); | ||||
1569 | }); | ||||
1570 | bool Changed = false; | ||||
1571 | if (isa<LoadInst>(I)) | ||||
1572 | ++NumMovedLoads; | ||||
1573 | else if (isa<CallInst>(I)) | ||||
1574 | ++NumMovedCalls; | ||||
1575 | ++NumSunk; | ||||
1576 | |||||
1577 | // Iterate over users to be ready for actual sinking. Replace users via | ||||
1578 | // unreachable blocks with undef and make all user PHIs trivially replaceable. | ||||
1579 | SmallPtrSet<Instruction *, 8> VisitedUsers; | ||||
1580 | for (Value::user_iterator UI = I.user_begin(), UE = I.user_end(); UI != UE;) { | ||||
1581 | auto *User = cast<Instruction>(*UI); | ||||
1582 | Use &U = UI.getUse(); | ||||
1583 | ++UI; | ||||
1584 | |||||
1585 | if (VisitedUsers.count(User) || CurLoop->contains(User)) | ||||
1586 | continue; | ||||
1587 | |||||
1588 | if (!DT->isReachableFromEntry(User->getParent())) { | ||||
1589 | U = UndefValue::get(I.getType()); | ||||
1590 | Changed = true; | ||||
1591 | continue; | ||||
1592 | } | ||||
1593 | |||||
1594 | // The user must be a PHI node. | ||||
1595 | PHINode *PN = cast<PHINode>(User); | ||||
1596 | |||||
1597 | // Surprisingly, instructions can be used outside of loops without any | ||||
1598 | // exits. This can only happen in PHI nodes if the incoming block is | ||||
1599 | // unreachable. | ||||
1600 | BasicBlock *BB = PN->getIncomingBlock(U); | ||||
1601 | if (!DT->isReachableFromEntry(BB)) { | ||||
1602 | U = UndefValue::get(I.getType()); | ||||
1603 | Changed = true; | ||||
1604 | continue; | ||||
1605 | } | ||||
1606 | |||||
1607 | VisitedUsers.insert(PN); | ||||
1608 | if (isTriviallyReplaceablePHI(*PN, I)) | ||||
1609 | continue; | ||||
1610 | |||||
1611 | if (!canSplitPredecessors(PN, SafetyInfo)) | ||||
1612 | return Changed; | ||||
1613 | |||||
1614 | // Split predecessors of the PHI so that we can make users trivially | ||||
1615 | // replaceable. | ||||
1616 | splitPredecessorsOfLoopExit(PN, DT, LI, CurLoop, SafetyInfo, MSSAU); | ||||
1617 | |||||
1618 | // Should rebuild the iterators, as they may be invalidated by | ||||
1619 | // splitPredecessorsOfLoopExit(). | ||||
1620 | UI = I.user_begin(); | ||||
1621 | UE = I.user_end(); | ||||
1622 | } | ||||
1623 | |||||
1624 | if (VisitedUsers.empty()) | ||||
1625 | return Changed; | ||||
1626 | |||||
1627 | #ifndef NDEBUG | ||||
1628 | SmallVector<BasicBlock *, 32> ExitBlocks; | ||||
1629 | CurLoop->getUniqueExitBlocks(ExitBlocks); | ||||
1630 | SmallPtrSet<BasicBlock *, 32> ExitBlockSet(ExitBlocks.begin(), | ||||
1631 | ExitBlocks.end()); | ||||
1632 | #endif | ||||
1633 | |||||
1634 | // Clones of this instruction. Don't create more than one per exit block! | ||||
1635 | SmallDenseMap<BasicBlock *, Instruction *, 32> SunkCopies; | ||||
1636 | |||||
1637 | // If this instruction is only used outside of the loop, then all users are | ||||
1638 | // PHI nodes in exit blocks due to LCSSA form. Just RAUW them with clones of | ||||
1639 | // the instruction. | ||||
1640 | SmallSetVector<User*, 8> Users(I.user_begin(), I.user_end()); | ||||
1641 | for (auto *UI : Users) { | ||||
1642 | auto *User = cast<Instruction>(UI); | ||||
1643 | |||||
1644 | if (CurLoop->contains(User)) | ||||
1645 | continue; | ||||
1646 | |||||
1647 | PHINode *PN = cast<PHINode>(User); | ||||
1648 | assert(ExitBlockSet.count(PN->getParent()) &&((ExitBlockSet.count(PN->getParent()) && "The LCSSA PHI is not in an exit block!" ) ? static_cast<void> (0) : __assert_fail ("ExitBlockSet.count(PN->getParent()) && \"The LCSSA PHI is not in an exit block!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 1649, __PRETTY_FUNCTION__)) | ||||
1649 | "The LCSSA PHI is not in an exit block!")((ExitBlockSet.count(PN->getParent()) && "The LCSSA PHI is not in an exit block!" ) ? static_cast<void> (0) : __assert_fail ("ExitBlockSet.count(PN->getParent()) && \"The LCSSA PHI is not in an exit block!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 1649, __PRETTY_FUNCTION__)); | ||||
1650 | // The PHI must be trivially replaceable. | ||||
1651 | Instruction *New = sinkThroughTriviallyReplaceablePHI( | ||||
1652 | PN, &I, LI, SunkCopies, SafetyInfo, CurLoop, MSSAU); | ||||
1653 | PN->replaceAllUsesWith(New); | ||||
1654 | eraseInstruction(*PN, *SafetyInfo, nullptr, nullptr); | ||||
1655 | Changed = true; | ||||
1656 | } | ||||
1657 | return Changed; | ||||
1658 | } | ||||
1659 | |||||
1660 | /// When an instruction is found to only use loop invariant operands that | ||||
1661 | /// is safe to hoist, this instruction is called to do the dirty work. | ||||
1662 | /// | ||||
1663 | static void hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop, | ||||
1664 | BasicBlock *Dest, ICFLoopSafetyInfo *SafetyInfo, | ||||
1665 | MemorySSAUpdater *MSSAU, OptimizationRemarkEmitter *ORE) { | ||||
1666 | LLVM_DEBUG(dbgs() << "LICM hoisting to " << Dest->getName() << ": " << Ido { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM hoisting to " << Dest ->getName() << ": " << I << "\n"; } } while (false) | ||||
1667 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM hoisting to " << Dest ->getName() << ": " << I << "\n"; } } while (false); | ||||
1668 | ORE->emit([&]() { | ||||
1669 | return OptimizationRemark(DEBUG_TYPE"licm", "Hoisted", &I) << "hoisting " | ||||
1670 | << ore::NV("Inst", &I); | ||||
1671 | }); | ||||
1672 | |||||
1673 | // Metadata can be dependent on conditions we are hoisting above. | ||||
1674 | // Conservatively strip all metadata on the instruction unless we were | ||||
1675 | // guaranteed to execute I if we entered the loop, in which case the metadata | ||||
1676 | // is valid in the loop preheader. | ||||
1677 | if (I.hasMetadataOtherThanDebugLoc() && | ||||
1678 | // The check on hasMetadataOtherThanDebugLoc is to prevent us from burning | ||||
1679 | // time in isGuaranteedToExecute if we don't actually have anything to | ||||
1680 | // drop. It is a compile time optimization, not required for correctness. | ||||
1681 | !SafetyInfo->isGuaranteedToExecute(I, DT, CurLoop)) | ||||
1682 | I.dropUnknownNonDebugMetadata(); | ||||
1683 | |||||
1684 | if (isa<PHINode>(I)) | ||||
1685 | // Move the new node to the end of the phi list in the destination block. | ||||
1686 | moveInstructionBefore(I, *Dest->getFirstNonPHI(), *SafetyInfo, MSSAU); | ||||
1687 | else | ||||
1688 | // Move the new node to the destination block, before its terminator. | ||||
1689 | moveInstructionBefore(I, *Dest->getTerminator(), *SafetyInfo, MSSAU); | ||||
1690 | |||||
1691 | // Apply line 0 debug locations when we are moving instructions to different | ||||
1692 | // basic blocks because we want to avoid jumpy line tables. | ||||
1693 | if (const DebugLoc &DL = I.getDebugLoc()) | ||||
1694 | I.setDebugLoc(DebugLoc::get(0, 0, DL.getScope(), DL.getInlinedAt())); | ||||
1695 | |||||
1696 | if (isa<LoadInst>(I)) | ||||
1697 | ++NumMovedLoads; | ||||
1698 | else if (isa<CallInst>(I)) | ||||
1699 | ++NumMovedCalls; | ||||
1700 | ++NumHoisted; | ||||
1701 | } | ||||
1702 | |||||
1703 | /// Only sink or hoist an instruction if it is not a trapping instruction, | ||||
1704 | /// or if the instruction is known not to trap when moved to the preheader. | ||||
1705 | /// or if it is a trapping instruction and is guaranteed to execute. | ||||
1706 | static bool isSafeToExecuteUnconditionally(Instruction &Inst, | ||||
1707 | const DominatorTree *DT, | ||||
1708 | const Loop *CurLoop, | ||||
1709 | const LoopSafetyInfo *SafetyInfo, | ||||
1710 | OptimizationRemarkEmitter *ORE, | ||||
1711 | const Instruction *CtxI) { | ||||
1712 | if (isSafeToSpeculativelyExecute(&Inst, CtxI, DT)) | ||||
1713 | return true; | ||||
1714 | |||||
1715 | bool GuaranteedToExecute = | ||||
1716 | SafetyInfo->isGuaranteedToExecute(Inst, DT, CurLoop); | ||||
1717 | |||||
1718 | if (!GuaranteedToExecute) { | ||||
1719 | auto *LI = dyn_cast<LoadInst>(&Inst); | ||||
1720 | if (LI && CurLoop->isLoopInvariant(LI->getPointerOperand())) | ||||
1721 | ORE->emit([&]() { | ||||
1722 | return OptimizationRemarkMissed( | ||||
1723 | DEBUG_TYPE"licm", "LoadWithLoopInvariantAddressCondExecuted", LI) | ||||
1724 | << "failed to hoist load with loop-invariant address " | ||||
1725 | "because load is conditionally executed"; | ||||
1726 | }); | ||||
1727 | } | ||||
1728 | |||||
1729 | return GuaranteedToExecute; | ||||
1730 | } | ||||
1731 | |||||
1732 | namespace { | ||||
1733 | class LoopPromoter : public LoadAndStorePromoter { | ||||
1734 | Value *SomePtr; // Designated pointer to store to. | ||||
1735 | const SmallSetVector<Value *, 8> &PointerMustAliases; | ||||
1736 | SmallVectorImpl<BasicBlock *> &LoopExitBlocks; | ||||
1737 | SmallVectorImpl<Instruction *> &LoopInsertPts; | ||||
1738 | SmallVectorImpl<MemoryAccess *> &MSSAInsertPts; | ||||
1739 | PredIteratorCache &PredCache; | ||||
1740 | AliasSetTracker &AST; | ||||
1741 | MemorySSAUpdater *MSSAU; | ||||
1742 | LoopInfo &LI; | ||||
1743 | DebugLoc DL; | ||||
1744 | int Alignment; | ||||
1745 | bool UnorderedAtomic; | ||||
1746 | AAMDNodes AATags; | ||||
1747 | ICFLoopSafetyInfo &SafetyInfo; | ||||
1748 | |||||
1749 | Value *maybeInsertLCSSAPHI(Value *V, BasicBlock *BB) const { | ||||
1750 | if (Instruction *I = dyn_cast<Instruction>(V)) | ||||
1751 | if (Loop *L = LI.getLoopFor(I->getParent())) | ||||
1752 | if (!L->contains(BB)) { | ||||
1753 | // We need to create an LCSSA PHI node for the incoming value and | ||||
1754 | // store that. | ||||
1755 | PHINode *PN = PHINode::Create(I->getType(), PredCache.size(BB), | ||||
1756 | I->getName() + ".lcssa", &BB->front()); | ||||
1757 | for (BasicBlock *Pred : PredCache.get(BB)) | ||||
1758 | PN->addIncoming(I, Pred); | ||||
1759 | return PN; | ||||
1760 | } | ||||
1761 | return V; | ||||
1762 | } | ||||
1763 | |||||
1764 | public: | ||||
1765 | LoopPromoter(Value *SP, ArrayRef<const Instruction *> Insts, SSAUpdater &S, | ||||
1766 | const SmallSetVector<Value *, 8> &PMA, | ||||
1767 | SmallVectorImpl<BasicBlock *> &LEB, | ||||
1768 | SmallVectorImpl<Instruction *> &LIP, | ||||
1769 | SmallVectorImpl<MemoryAccess *> &MSSAIP, PredIteratorCache &PIC, | ||||
1770 | AliasSetTracker &ast, MemorySSAUpdater *MSSAU, LoopInfo &li, | ||||
1771 | DebugLoc dl, int alignment, bool UnorderedAtomic, | ||||
1772 | const AAMDNodes &AATags, ICFLoopSafetyInfo &SafetyInfo) | ||||
1773 | : LoadAndStorePromoter(Insts, S), SomePtr(SP), PointerMustAliases(PMA), | ||||
1774 | LoopExitBlocks(LEB), LoopInsertPts(LIP), MSSAInsertPts(MSSAIP), | ||||
1775 | PredCache(PIC), AST(ast), MSSAU(MSSAU), LI(li), DL(std::move(dl)), | ||||
1776 | Alignment(alignment), UnorderedAtomic(UnorderedAtomic), AATags(AATags), | ||||
1777 | SafetyInfo(SafetyInfo) {} | ||||
1778 | |||||
1779 | bool isInstInList(Instruction *I, | ||||
1780 | const SmallVectorImpl<Instruction *> &) const override { | ||||
1781 | Value *Ptr; | ||||
1782 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) | ||||
1783 | Ptr = LI->getOperand(0); | ||||
1784 | else | ||||
1785 | Ptr = cast<StoreInst>(I)->getPointerOperand(); | ||||
1786 | return PointerMustAliases.count(Ptr); | ||||
1787 | } | ||||
1788 | |||||
1789 | void doExtraRewritesBeforeFinalDeletion() override { | ||||
1790 | // Insert stores after in the loop exit blocks. Each exit block gets a | ||||
1791 | // store of the live-out values that feed them. Since we've already told | ||||
1792 | // the SSA updater about the defs in the loop and the preheader | ||||
1793 | // definition, it is all set and we can start using it. | ||||
1794 | for (unsigned i = 0, e = LoopExitBlocks.size(); i != e; ++i) { | ||||
1795 | BasicBlock *ExitBlock = LoopExitBlocks[i]; | ||||
1796 | Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock); | ||||
1797 | LiveInValue = maybeInsertLCSSAPHI(LiveInValue, ExitBlock); | ||||
1798 | Value *Ptr = maybeInsertLCSSAPHI(SomePtr, ExitBlock); | ||||
1799 | Instruction *InsertPos = LoopInsertPts[i]; | ||||
1800 | StoreInst *NewSI = new StoreInst(LiveInValue, Ptr, InsertPos); | ||||
1801 | if (UnorderedAtomic) | ||||
1802 | NewSI->setOrdering(AtomicOrdering::Unordered); | ||||
1803 | NewSI->setAlignment(MaybeAlign(Alignment)); | ||||
1804 | NewSI->setDebugLoc(DL); | ||||
1805 | if (AATags) | ||||
1806 | NewSI->setAAMetadata(AATags); | ||||
1807 | |||||
1808 | if (MSSAU) { | ||||
1809 | MemoryAccess *MSSAInsertPoint = MSSAInsertPts[i]; | ||||
1810 | MemoryAccess *NewMemAcc; | ||||
1811 | if (!MSSAInsertPoint) { | ||||
1812 | NewMemAcc = MSSAU->createMemoryAccessInBB( | ||||
1813 | NewSI, nullptr, NewSI->getParent(), MemorySSA::Beginning); | ||||
1814 | } else { | ||||
1815 | NewMemAcc = | ||||
1816 | MSSAU->createMemoryAccessAfter(NewSI, nullptr, MSSAInsertPoint); | ||||
1817 | } | ||||
1818 | MSSAInsertPts[i] = NewMemAcc; | ||||
1819 | MSSAU->insertDef(cast<MemoryDef>(NewMemAcc), true); | ||||
1820 | // FIXME: true for safety, false may still be correct. | ||||
1821 | } | ||||
1822 | } | ||||
1823 | } | ||||
1824 | |||||
1825 | void replaceLoadWithValue(LoadInst *LI, Value *V) const override { | ||||
1826 | // Update alias analysis. | ||||
1827 | AST.copyValue(LI, V); | ||||
1828 | } | ||||
1829 | void instructionDeleted(Instruction *I) const override { | ||||
1830 | SafetyInfo.removeInstruction(I); | ||||
1831 | AST.deleteValue(I); | ||||
1832 | if (MSSAU) | ||||
1833 | MSSAU->removeMemoryAccess(I); | ||||
1834 | } | ||||
1835 | }; | ||||
1836 | |||||
1837 | |||||
1838 | /// Return true iff we can prove that a caller of this function can not inspect | ||||
1839 | /// the contents of the provided object in a well defined program. | ||||
1840 | bool isKnownNonEscaping(Value *Object, const TargetLibraryInfo *TLI) { | ||||
1841 | if (isa<AllocaInst>(Object)) | ||||
1842 | // Since the alloca goes out of scope, we know the caller can't retain a | ||||
1843 | // reference to it and be well defined. Thus, we don't need to check for | ||||
1844 | // capture. | ||||
1845 | return true; | ||||
1846 | |||||
1847 | // For all other objects we need to know that the caller can't possibly | ||||
1848 | // have gotten a reference to the object. There are two components of | ||||
1849 | // that: | ||||
1850 | // 1) Object can't be escaped by this function. This is what | ||||
1851 | // PointerMayBeCaptured checks. | ||||
1852 | // 2) Object can't have been captured at definition site. For this, we | ||||
1853 | // need to know the return value is noalias. At the moment, we use a | ||||
1854 | // weaker condition and handle only AllocLikeFunctions (which are | ||||
1855 | // known to be noalias). TODO | ||||
1856 | return isAllocLikeFn(Object, TLI) && | ||||
1857 | !PointerMayBeCaptured(Object, true, true); | ||||
1858 | } | ||||
1859 | |||||
1860 | } // namespace | ||||
1861 | |||||
1862 | /// Try to promote memory values to scalars by sinking stores out of the | ||||
1863 | /// loop and moving loads to before the loop. We do this by looping over | ||||
1864 | /// the stores in the loop, looking for stores to Must pointers which are | ||||
1865 | /// loop invariant. | ||||
1866 | /// | ||||
1867 | bool llvm::promoteLoopAccessesToScalars( | ||||
1868 | const SmallSetVector<Value *, 8> &PointerMustAliases, | ||||
1869 | SmallVectorImpl<BasicBlock *> &ExitBlocks, | ||||
1870 | SmallVectorImpl<Instruction *> &InsertPts, | ||||
1871 | SmallVectorImpl<MemoryAccess *> &MSSAInsertPts, PredIteratorCache &PIC, | ||||
1872 | LoopInfo *LI, DominatorTree *DT, const TargetLibraryInfo *TLI, | ||||
1873 | Loop *CurLoop, AliasSetTracker *CurAST, MemorySSAUpdater *MSSAU, | ||||
1874 | ICFLoopSafetyInfo *SafetyInfo, OptimizationRemarkEmitter *ORE) { | ||||
1875 | // Verify inputs. | ||||
1876 | assert(LI != nullptr && DT != nullptr && CurLoop != nullptr &&((LI != nullptr && DT != nullptr && CurLoop != nullptr && CurAST != nullptr && SafetyInfo != nullptr && "Unexpected Input to promoteLoopAccessesToScalars" ) ? static_cast<void> (0) : __assert_fail ("LI != nullptr && DT != nullptr && CurLoop != nullptr && CurAST != nullptr && SafetyInfo != nullptr && \"Unexpected Input to promoteLoopAccessesToScalars\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 1878, __PRETTY_FUNCTION__)) | ||||
1877 | CurAST != nullptr && SafetyInfo != nullptr &&((LI != nullptr && DT != nullptr && CurLoop != nullptr && CurAST != nullptr && SafetyInfo != nullptr && "Unexpected Input to promoteLoopAccessesToScalars" ) ? static_cast<void> (0) : __assert_fail ("LI != nullptr && DT != nullptr && CurLoop != nullptr && CurAST != nullptr && SafetyInfo != nullptr && \"Unexpected Input to promoteLoopAccessesToScalars\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 1878, __PRETTY_FUNCTION__)) | ||||
1878 | "Unexpected Input to promoteLoopAccessesToScalars")((LI != nullptr && DT != nullptr && CurLoop != nullptr && CurAST != nullptr && SafetyInfo != nullptr && "Unexpected Input to promoteLoopAccessesToScalars" ) ? static_cast<void> (0) : __assert_fail ("LI != nullptr && DT != nullptr && CurLoop != nullptr && CurAST != nullptr && SafetyInfo != nullptr && \"Unexpected Input to promoteLoopAccessesToScalars\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 1878, __PRETTY_FUNCTION__)); | ||||
1879 | |||||
1880 | Value *SomePtr = *PointerMustAliases.begin(); | ||||
1881 | BasicBlock *Preheader = CurLoop->getLoopPreheader(); | ||||
1882 | |||||
1883 | // It is not safe to promote a load/store from the loop if the load/store is | ||||
1884 | // conditional. For example, turning: | ||||
1885 | // | ||||
1886 | // for () { if (c) *P += 1; } | ||||
1887 | // | ||||
1888 | // into: | ||||
1889 | // | ||||
1890 | // tmp = *P; for () { if (c) tmp +=1; } *P = tmp; | ||||
1891 | // | ||||
1892 | // is not safe, because *P may only be valid to access if 'c' is true. | ||||
1893 | // | ||||
1894 | // The safety property divides into two parts: | ||||
1895 | // p1) The memory may not be dereferenceable on entry to the loop. In this | ||||
1896 | // case, we can't insert the required load in the preheader. | ||||
1897 | // p2) The memory model does not allow us to insert a store along any dynamic | ||||
1898 | // path which did not originally have one. | ||||
1899 | // | ||||
1900 | // If at least one store is guaranteed to execute, both properties are | ||||
1901 | // satisfied, and promotion is legal. | ||||
1902 | // | ||||
1903 | // This, however, is not a necessary condition. Even if no store/load is | ||||
1904 | // guaranteed to execute, we can still establish these properties. | ||||
1905 | // We can establish (p1) by proving that hoisting the load into the preheader | ||||
1906 | // is safe (i.e. proving dereferenceability on all paths through the loop). We | ||||
1907 | // can use any access within the alias set to prove dereferenceability, | ||||
1908 | // since they're all must alias. | ||||
1909 | // | ||||
1910 | // There are two ways establish (p2): | ||||
1911 | // a) Prove the location is thread-local. In this case the memory model | ||||
1912 | // requirement does not apply, and stores are safe to insert. | ||||
1913 | // b) Prove a store dominates every exit block. In this case, if an exit | ||||
1914 | // blocks is reached, the original dynamic path would have taken us through | ||||
1915 | // the store, so inserting a store into the exit block is safe. Note that this | ||||
1916 | // is different from the store being guaranteed to execute. For instance, | ||||
1917 | // if an exception is thrown on the first iteration of the loop, the original | ||||
1918 | // store is never executed, but the exit blocks are not executed either. | ||||
1919 | |||||
1920 | bool DereferenceableInPH = false; | ||||
1921 | bool SafeToInsertStore = false; | ||||
1922 | |||||
1923 | SmallVector<Instruction *, 64> LoopUses; | ||||
1924 | |||||
1925 | // We start with an alignment of one and try to find instructions that allow | ||||
1926 | // us to prove better alignment. | ||||
1927 | unsigned Alignment = 1; | ||||
1928 | // Keep track of which types of access we see | ||||
1929 | bool SawUnorderedAtomic = false; | ||||
1930 | bool SawNotAtomic = false; | ||||
1931 | AAMDNodes AATags; | ||||
1932 | |||||
1933 | const DataLayout &MDL = Preheader->getModule()->getDataLayout(); | ||||
1934 | |||||
1935 | bool IsKnownThreadLocalObject = false; | ||||
1936 | if (SafetyInfo->anyBlockMayThrow()) { | ||||
1937 | // If a loop can throw, we have to insert a store along each unwind edge. | ||||
1938 | // That said, we can't actually make the unwind edge explicit. Therefore, | ||||
1939 | // we have to prove that the store is dead along the unwind edge. We do | ||||
1940 | // this by proving that the caller can't have a reference to the object | ||||
1941 | // after return and thus can't possibly load from the object. | ||||
1942 | Value *Object = GetUnderlyingObject(SomePtr, MDL); | ||||
1943 | if (!isKnownNonEscaping(Object, TLI)) | ||||
1944 | return false; | ||||
1945 | // Subtlety: Alloca's aren't visible to callers, but *are* potentially | ||||
1946 | // visible to other threads if captured and used during their lifetimes. | ||||
1947 | IsKnownThreadLocalObject = !isa<AllocaInst>(Object); | ||||
1948 | } | ||||
1949 | |||||
1950 | // Check that all of the pointers in the alias set have the same type. We | ||||
1951 | // cannot (yet) promote a memory location that is loaded and stored in | ||||
1952 | // different sizes. While we are at it, collect alignment and AA info. | ||||
1953 | for (Value *ASIV : PointerMustAliases) { | ||||
1954 | // Check that all of the pointers in the alias set have the same type. We | ||||
1955 | // cannot (yet) promote a memory location that is loaded and stored in | ||||
1956 | // different sizes. | ||||
1957 | if (SomePtr->getType() != ASIV->getType()) | ||||
1958 | return false; | ||||
1959 | |||||
1960 | for (User *U : ASIV->users()) { | ||||
1961 | // Ignore instructions that are outside the loop. | ||||
1962 | Instruction *UI = dyn_cast<Instruction>(U); | ||||
1963 | if (!UI || !CurLoop->contains(UI)) | ||||
1964 | continue; | ||||
1965 | |||||
1966 | // If there is an non-load/store instruction in the loop, we can't promote | ||||
1967 | // it. | ||||
1968 | if (LoadInst *Load = dyn_cast<LoadInst>(UI)) { | ||||
1969 | if (!Load->isUnordered()) | ||||
1970 | return false; | ||||
1971 | |||||
1972 | SawUnorderedAtomic |= Load->isAtomic(); | ||||
1973 | SawNotAtomic |= !Load->isAtomic(); | ||||
1974 | |||||
1975 | unsigned InstAlignment = Load->getAlignment(); | ||||
1976 | if (!InstAlignment) | ||||
1977 | InstAlignment = | ||||
1978 | MDL.getABITypeAlignment(Load->getType()); | ||||
1979 | |||||
1980 | // Note that proving a load safe to speculate requires proving | ||||
1981 | // sufficient alignment at the target location. Proving it guaranteed | ||||
1982 | // to execute does as well. Thus we can increase our guaranteed | ||||
1983 | // alignment as well. | ||||
1984 | if (!DereferenceableInPH || (InstAlignment > Alignment)) | ||||
1985 | if (isSafeToExecuteUnconditionally(*Load, DT, CurLoop, SafetyInfo, | ||||
1986 | ORE, Preheader->getTerminator())) { | ||||
1987 | DereferenceableInPH = true; | ||||
1988 | Alignment = std::max(Alignment, InstAlignment); | ||||
1989 | } | ||||
1990 | } else if (const StoreInst *Store = dyn_cast<StoreInst>(UI)) { | ||||
1991 | // Stores *of* the pointer are not interesting, only stores *to* the | ||||
1992 | // pointer. | ||||
1993 | if (UI->getOperand(1) != ASIV) | ||||
1994 | continue; | ||||
1995 | if (!Store->isUnordered()) | ||||
1996 | return false; | ||||
1997 | |||||
1998 | SawUnorderedAtomic |= Store->isAtomic(); | ||||
1999 | SawNotAtomic |= !Store->isAtomic(); | ||||
2000 | |||||
2001 | // If the store is guaranteed to execute, both properties are satisfied. | ||||
2002 | // We may want to check if a store is guaranteed to execute even if we | ||||
2003 | // already know that promotion is safe, since it may have higher | ||||
2004 | // alignment than any other guaranteed stores, in which case we can | ||||
2005 | // raise the alignment on the promoted store. | ||||
2006 | unsigned InstAlignment = Store->getAlignment(); | ||||
2007 | if (!InstAlignment) | ||||
2008 | InstAlignment = | ||||
2009 | MDL.getABITypeAlignment(Store->getValueOperand()->getType()); | ||||
2010 | |||||
2011 | if (!DereferenceableInPH || !SafeToInsertStore || | ||||
2012 | (InstAlignment > Alignment)) { | ||||
2013 | if (SafetyInfo->isGuaranteedToExecute(*UI, DT, CurLoop)) { | ||||
2014 | DereferenceableInPH = true; | ||||
2015 | SafeToInsertStore = true; | ||||
2016 | Alignment = std::max(Alignment, InstAlignment); | ||||
2017 | } | ||||
2018 | } | ||||
2019 | |||||
2020 | // If a store dominates all exit blocks, it is safe to sink. | ||||
2021 | // As explained above, if an exit block was executed, a dominating | ||||
2022 | // store must have been executed at least once, so we are not | ||||
2023 | // introducing stores on paths that did not have them. | ||||
2024 | // Note that this only looks at explicit exit blocks. If we ever | ||||
2025 | // start sinking stores into unwind edges (see above), this will break. | ||||
2026 | if (!SafeToInsertStore) | ||||
2027 | SafeToInsertStore = llvm::all_of(ExitBlocks, [&](BasicBlock *Exit) { | ||||
2028 | return DT->dominates(Store->getParent(), Exit); | ||||
2029 | }); | ||||
2030 | |||||
2031 | // If the store is not guaranteed to execute, we may still get | ||||
2032 | // deref info through it. | ||||
2033 | if (!DereferenceableInPH) { | ||||
2034 | DereferenceableInPH = isDereferenceableAndAlignedPointer( | ||||
2035 | Store->getPointerOperand(), Store->getValueOperand()->getType(), | ||||
2036 | Store->getAlignment(), MDL, Preheader->getTerminator(), DT); | ||||
2037 | } | ||||
2038 | } else | ||||
2039 | return false; // Not a load or store. | ||||
2040 | |||||
2041 | // Merge the AA tags. | ||||
2042 | if (LoopUses.empty()) { | ||||
2043 | // On the first load/store, just take its AA tags. | ||||
2044 | UI->getAAMetadata(AATags); | ||||
2045 | } else if (AATags) { | ||||
2046 | UI->getAAMetadata(AATags, /* Merge = */ true); | ||||
2047 | } | ||||
2048 | |||||
2049 | LoopUses.push_back(UI); | ||||
2050 | } | ||||
2051 | } | ||||
2052 | |||||
2053 | // If we found both an unordered atomic instruction and a non-atomic memory | ||||
2054 | // access, bail. We can't blindly promote non-atomic to atomic since we | ||||
2055 | // might not be able to lower the result. We can't downgrade since that | ||||
2056 | // would violate memory model. Also, align 0 is an error for atomics. | ||||
2057 | if (SawUnorderedAtomic && SawNotAtomic) | ||||
2058 | return false; | ||||
2059 | |||||
2060 | // If we're inserting an atomic load in the preheader, we must be able to | ||||
2061 | // lower it. We're only guaranteed to be able to lower naturally aligned | ||||
2062 | // atomics. | ||||
2063 | auto *SomePtrElemType = SomePtr->getType()->getPointerElementType(); | ||||
2064 | if (SawUnorderedAtomic && | ||||
2065 | Alignment < MDL.getTypeStoreSize(SomePtrElemType)) | ||||
2066 | return false; | ||||
2067 | |||||
2068 | // If we couldn't prove we can hoist the load, bail. | ||||
2069 | if (!DereferenceableInPH) | ||||
2070 | return false; | ||||
2071 | |||||
2072 | // We know we can hoist the load, but don't have a guaranteed store. | ||||
2073 | // Check whether the location is thread-local. If it is, then we can insert | ||||
2074 | // stores along paths which originally didn't have them without violating the | ||||
2075 | // memory model. | ||||
2076 | if (!SafeToInsertStore) { | ||||
2077 | if (IsKnownThreadLocalObject) | ||||
2078 | SafeToInsertStore = true; | ||||
2079 | else { | ||||
2080 | Value *Object = GetUnderlyingObject(SomePtr, MDL); | ||||
2081 | SafeToInsertStore = | ||||
2082 | (isAllocLikeFn(Object, TLI) || isa<AllocaInst>(Object)) && | ||||
2083 | !PointerMayBeCaptured(Object, true, true); | ||||
2084 | } | ||||
2085 | } | ||||
2086 | |||||
2087 | // If we've still failed to prove we can sink the store, give up. | ||||
2088 | if (!SafeToInsertStore) | ||||
2089 | return false; | ||||
2090 | |||||
2091 | // Otherwise, this is safe to promote, lets do it! | ||||
2092 | LLVM_DEBUG(dbgs() << "LICM: Promoting value stored to in loop: " << *SomePtrdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM: Promoting value stored to in loop: " << *SomePtr << '\n'; } } while (false) | ||||
2093 | << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM: Promoting value stored to in loop: " << *SomePtr << '\n'; } } while (false); | ||||
2094 | ORE->emit([&]() { | ||||
2095 | return OptimizationRemark(DEBUG_TYPE"licm", "PromoteLoopAccessesToScalar", | ||||
2096 | LoopUses[0]) | ||||
2097 | << "Moving accesses to memory location out of the loop"; | ||||
2098 | }); | ||||
2099 | ++NumPromoted; | ||||
2100 | |||||
2101 | // Grab a debug location for the inserted loads/stores; given that the | ||||
2102 | // inserted loads/stores have little relation to the original loads/stores, | ||||
2103 | // this code just arbitrarily picks a location from one, since any debug | ||||
2104 | // location is better than none. | ||||
2105 | DebugLoc DL = LoopUses[0]->getDebugLoc(); | ||||
2106 | |||||
2107 | // We use the SSAUpdater interface to insert phi nodes as required. | ||||
2108 | SmallVector<PHINode *, 16> NewPHIs; | ||||
2109 | SSAUpdater SSA(&NewPHIs); | ||||
2110 | LoopPromoter Promoter(SomePtr, LoopUses, SSA, PointerMustAliases, ExitBlocks, | ||||
2111 | InsertPts, MSSAInsertPts, PIC, *CurAST, MSSAU, *LI, DL, | ||||
2112 | Alignment, SawUnorderedAtomic, AATags, *SafetyInfo); | ||||
2113 | |||||
2114 | // Set up the preheader to have a definition of the value. It is the live-out | ||||
2115 | // value from the preheader that uses in the loop will use. | ||||
2116 | LoadInst *PreheaderLoad = new LoadInst( | ||||
2117 | SomePtr->getType()->getPointerElementType(), SomePtr, | ||||
2118 | SomePtr->getName() + ".promoted", Preheader->getTerminator()); | ||||
2119 | if (SawUnorderedAtomic) | ||||
2120 | PreheaderLoad->setOrdering(AtomicOrdering::Unordered); | ||||
2121 | PreheaderLoad->setAlignment(MaybeAlign(Alignment)); | ||||
2122 | PreheaderLoad->setDebugLoc(DL); | ||||
2123 | if (AATags) | ||||
2124 | PreheaderLoad->setAAMetadata(AATags); | ||||
2125 | SSA.AddAvailableValue(Preheader, PreheaderLoad); | ||||
2126 | |||||
2127 | if (MSSAU) { | ||||
2128 | MemoryAccess *PreheaderLoadMemoryAccess = MSSAU->createMemoryAccessInBB( | ||||
2129 | PreheaderLoad, nullptr, PreheaderLoad->getParent(), MemorySSA::End); | ||||
2130 | MemoryUse *NewMemUse = cast<MemoryUse>(PreheaderLoadMemoryAccess); | ||||
2131 | MSSAU->insertUse(NewMemUse, /*RenameUses=*/true); | ||||
2132 | } | ||||
2133 | |||||
2134 | if (MSSAU && VerifyMemorySSA) | ||||
2135 | MSSAU->getMemorySSA()->verifyMemorySSA(); | ||||
2136 | // Rewrite all the loads in the loop and remember all the definitions from | ||||
2137 | // stores in the loop. | ||||
2138 | Promoter.run(LoopUses); | ||||
2139 | |||||
2140 | if (MSSAU && VerifyMemorySSA) | ||||
2141 | MSSAU->getMemorySSA()->verifyMemorySSA(); | ||||
2142 | // If the SSAUpdater didn't use the load in the preheader, just zap it now. | ||||
2143 | if (PreheaderLoad->use_empty()) | ||||
2144 | eraseInstruction(*PreheaderLoad, *SafetyInfo, CurAST, MSSAU); | ||||
2145 | |||||
2146 | return true; | ||||
2147 | } | ||||
2148 | |||||
2149 | /// Returns an owning pointer to an alias set which incorporates aliasing info | ||||
2150 | /// from L and all subloops of L. | ||||
2151 | /// FIXME: In new pass manager, there is no helper function to handle loop | ||||
2152 | /// analysis such as cloneBasicBlockAnalysis, so the AST needs to be recomputed | ||||
2153 | /// from scratch for every loop. Hook up with the helper functions when | ||||
2154 | /// available in the new pass manager to avoid redundant computation. | ||||
2155 | std::unique_ptr<AliasSetTracker> | ||||
2156 | LoopInvariantCodeMotion::collectAliasInfoForLoop(Loop *L, LoopInfo *LI, | ||||
2157 | AliasAnalysis *AA) { | ||||
2158 | std::unique_ptr<AliasSetTracker> CurAST; | ||||
2159 | SmallVector<Loop *, 4> RecomputeLoops; | ||||
2160 | for (Loop *InnerL : L->getSubLoops()) { | ||||
2161 | auto MapI = LoopToAliasSetMap.find(InnerL); | ||||
2162 | // If the AST for this inner loop is missing it may have been merged into | ||||
2163 | // some other loop's AST and then that loop unrolled, and so we need to | ||||
2164 | // recompute it. | ||||
2165 | if (MapI == LoopToAliasSetMap.end()) { | ||||
2166 | RecomputeLoops.push_back(InnerL); | ||||
2167 | continue; | ||||
2168 | } | ||||
2169 | std::unique_ptr<AliasSetTracker> InnerAST = std::move(MapI->second); | ||||
2170 | |||||
2171 | if (CurAST) { | ||||
2172 | // What if InnerLoop was modified by other passes ? | ||||
2173 | // Once we've incorporated the inner loop's AST into ours, we don't need | ||||
2174 | // the subloop's anymore. | ||||
2175 | CurAST->add(*InnerAST); | ||||
2176 | } else { | ||||
2177 | CurAST = std::move(InnerAST); | ||||
2178 | } | ||||
2179 | LoopToAliasSetMap.erase(MapI); | ||||
2180 | } | ||||
2181 | if (!CurAST) | ||||
2182 | CurAST = std::make_unique<AliasSetTracker>(*AA); | ||||
2183 | |||||
2184 | // Add everything from the sub loops that are no longer directly available. | ||||
2185 | for (Loop *InnerL : RecomputeLoops) | ||||
2186 | for (BasicBlock *BB : InnerL->blocks()) | ||||
2187 | CurAST->add(*BB); | ||||
2188 | |||||
2189 | // And merge in this loop (without anything from inner loops). | ||||
2190 | for (BasicBlock *BB : L->blocks()) | ||||
2191 | if (LI->getLoopFor(BB) == L) | ||||
2192 | CurAST->add(*BB); | ||||
2193 | |||||
2194 | return CurAST; | ||||
2195 | } | ||||
2196 | |||||
2197 | std::unique_ptr<AliasSetTracker> | ||||
2198 | LoopInvariantCodeMotion::collectAliasInfoForLoopWithMSSA( | ||||
2199 | Loop *L, AliasAnalysis *AA, MemorySSAUpdater *MSSAU) { | ||||
2200 | auto *MSSA = MSSAU->getMemorySSA(); | ||||
2201 | auto CurAST = std::make_unique<AliasSetTracker>(*AA, MSSA, L); | ||||
2202 | CurAST->addAllInstructionsInLoopUsingMSSA(); | ||||
2203 | return CurAST; | ||||
2204 | } | ||||
2205 | |||||
2206 | /// Simple analysis hook. Clone alias set info. | ||||
2207 | /// | ||||
2208 | void LegacyLICMPass::cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To, | ||||
2209 | Loop *L) { | ||||
2210 | auto ASTIt = LICM.getLoopToAliasSetMap().find(L); | ||||
2211 | if (ASTIt == LICM.getLoopToAliasSetMap().end()) | ||||
2212 | return; | ||||
2213 | |||||
2214 | ASTIt->second->copyValue(From, To); | ||||
2215 | } | ||||
2216 | |||||
2217 | /// Simple Analysis hook. Delete value V from alias set | ||||
2218 | /// | ||||
2219 | void LegacyLICMPass::deleteAnalysisValue(Value *V, Loop *L) { | ||||
2220 | auto ASTIt = LICM.getLoopToAliasSetMap().find(L); | ||||
2221 | if (ASTIt == LICM.getLoopToAliasSetMap().end()) | ||||
2222 | return; | ||||
2223 | |||||
2224 | ASTIt->second->deleteValue(V); | ||||
2225 | } | ||||
2226 | |||||
2227 | /// Simple Analysis hook. Delete value L from alias set map. | ||||
2228 | /// | ||||
2229 | void LegacyLICMPass::deleteAnalysisLoop(Loop *L) { | ||||
2230 | if (!LICM.getLoopToAliasSetMap().count(L)) | ||||
2231 | return; | ||||
2232 | |||||
2233 | LICM.getLoopToAliasSetMap().erase(L); | ||||
2234 | } | ||||
2235 | |||||
2236 | static bool pointerInvalidatedByLoop(MemoryLocation MemLoc, | ||||
2237 | AliasSetTracker *CurAST, Loop *CurLoop, | ||||
2238 | AliasAnalysis *AA) { | ||||
2239 | // First check to see if any of the basic blocks in CurLoop invalidate *V. | ||||
2240 | bool isInvalidatedAccordingToAST = CurAST->getAliasSetFor(MemLoc).isMod(); | ||||
2241 | |||||
2242 | if (!isInvalidatedAccordingToAST || !LICMN2Theshold) | ||||
2243 | return isInvalidatedAccordingToAST; | ||||
2244 | |||||
2245 | // Check with a diagnostic analysis if we can refine the information above. | ||||
2246 | // This is to identify the limitations of using the AST. | ||||
2247 | // The alias set mechanism used by LICM has a major weakness in that it | ||||
2248 | // combines all things which may alias into a single set *before* asking | ||||
2249 | // modref questions. As a result, a single readonly call within a loop will | ||||
2250 | // collapse all loads and stores into a single alias set and report | ||||
2251 | // invalidation if the loop contains any store. For example, readonly calls | ||||
2252 | // with deopt states have this form and create a general alias set with all | ||||
2253 | // loads and stores. In order to get any LICM in loops containing possible | ||||
2254 | // deopt states we need a more precise invalidation of checking the mod ref | ||||
2255 | // info of each instruction within the loop and LI. This has a complexity of | ||||
2256 | // O(N^2), so currently, it is used only as a diagnostic tool since the | ||||
2257 | // default value of LICMN2Threshold is zero. | ||||
2258 | |||||
2259 | // Don't look at nested loops. | ||||
2260 | if (CurLoop->begin() != CurLoop->end()) | ||||
2261 | return true; | ||||
2262 | |||||
2263 | int N = 0; | ||||
2264 | for (BasicBlock *BB : CurLoop->getBlocks()) | ||||
2265 | for (Instruction &I : *BB) { | ||||
2266 | if (N >= LICMN2Theshold) { | ||||
2267 | LLVM_DEBUG(dbgs() << "Alasing N2 threshold exhausted for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "Alasing N2 threshold exhausted for " << *(MemLoc.Ptr) << "\n"; } } while (false) | ||||
2268 | << *(MemLoc.Ptr) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "Alasing N2 threshold exhausted for " << *(MemLoc.Ptr) << "\n"; } } while (false); | ||||
2269 | return true; | ||||
2270 | } | ||||
2271 | N++; | ||||
2272 | auto Res = AA->getModRefInfo(&I, MemLoc); | ||||
2273 | if (isModSet(Res)) { | ||||
2274 | LLVM_DEBUG(dbgs() << "Aliasing failed on " << I << " for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "Aliasing failed on " << I << " for " << *(MemLoc.Ptr) << "\n"; } } while (false ) | ||||
2275 | << *(MemLoc.Ptr) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "Aliasing failed on " << I << " for " << *(MemLoc.Ptr) << "\n"; } } while (false ); | ||||
2276 | return true; | ||||
2277 | } | ||||
2278 | } | ||||
2279 | LLVM_DEBUG(dbgs() << "Aliasing okay for " << *(MemLoc.Ptr) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "Aliasing okay for " << *(MemLoc .Ptr) << "\n"; } } while (false); | ||||
2280 | return false; | ||||
2281 | } | ||||
2282 | |||||
2283 | static bool pointerInvalidatedByLoopWithMSSA(MemorySSA *MSSA, MemoryUse *MU, | ||||
2284 | Loop *CurLoop, | ||||
2285 | SinkAndHoistLICMFlags &Flags) { | ||||
2286 | // For hoisting, use the walker to determine safety | ||||
2287 | if (!Flags.IsSink) { | ||||
2288 | MemoryAccess *Source; | ||||
2289 | // See declaration of SetLicmMssaOptCap for usage details. | ||||
2290 | if (Flags.LicmMssaOptCounter >= Flags.LicmMssaOptCap) | ||||
2291 | Source = MU->getDefiningAccess(); | ||||
2292 | else { | ||||
2293 | Source = MSSA->getSkipSelfWalker()->getClobberingMemoryAccess(MU); | ||||
2294 | Flags.LicmMssaOptCounter++; | ||||
2295 | } | ||||
2296 | return !MSSA->isLiveOnEntryDef(Source) && | ||||
2297 | CurLoop->contains(Source->getBlock()); | ||||
2298 | } | ||||
2299 | |||||
2300 | // For sinking, we'd need to check all Defs below this use. The getClobbering | ||||
2301 | // call will look on the backedge of the loop, but will check aliasing with | ||||
2302 | // the instructions on the previous iteration. | ||||
2303 | // For example: | ||||
2304 | // for (i ... ) | ||||
2305 | // load a[i] ( Use (LoE) | ||||
2306 | // store a[i] ( 1 = Def (2), with 2 = Phi for the loop. | ||||
2307 | // i++; | ||||
2308 | // The load sees no clobbering inside the loop, as the backedge alias check | ||||
2309 | // does phi translation, and will check aliasing against store a[i-1]. | ||||
2310 | // However sinking the load outside the loop, below the store is incorrect. | ||||
2311 | |||||
2312 | // For now, only sink if there are no Defs in the loop, and the existing ones | ||||
2313 | // precede the use and are in the same block. | ||||
2314 | // FIXME: Increase precision: Safe to sink if Use post dominates the Def; | ||||
2315 | // needs PostDominatorTreeAnalysis. | ||||
2316 | // FIXME: More precise: no Defs that alias this Use. | ||||
2317 | if (Flags.NoOfMemAccTooLarge) | ||||
2318 | return true; | ||||
2319 | for (auto *BB : CurLoop->getBlocks()) | ||||
2320 | if (auto *Accesses = MSSA->getBlockDefs(BB)) | ||||
2321 | for (const auto &MA : *Accesses) | ||||
2322 | if (const auto *MD = dyn_cast<MemoryDef>(&MA)) | ||||
2323 | if (MU->getBlock() != MD->getBlock() || | ||||
2324 | !MSSA->locallyDominates(MD, MU)) | ||||
2325 | return true; | ||||
2326 | return false; | ||||
2327 | } | ||||
2328 | |||||
2329 | /// Little predicate that returns true if the specified basic block is in | ||||
2330 | /// a subloop of the current one, not the current one itself. | ||||
2331 | /// | ||||
2332 | static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI) { | ||||
2333 | assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop")((CurLoop->contains(BB) && "Only valid if BB is IN the loop" ) ? static_cast<void> (0) : __assert_fail ("CurLoop->contains(BB) && \"Only valid if BB is IN the loop\"" , "/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp" , 2333, __PRETTY_FUNCTION__)); | ||||
2334 | return LI->getLoopFor(BB) != CurLoop; | ||||
2335 | } |
1 | //===- llvm/Instructions.h - Instruction subclass definitions ---*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file exposes the class definitions of all of the subclasses of the |
10 | // Instruction class. This is meant to be an easy way to get access to all |
11 | // instruction subclasses. |
12 | // |
13 | //===----------------------------------------------------------------------===// |
14 | |
15 | #ifndef LLVM_IR_INSTRUCTIONS_H |
16 | #define LLVM_IR_INSTRUCTIONS_H |
17 | |
18 | #include "llvm/ADT/ArrayRef.h" |
19 | #include "llvm/ADT/None.h" |
20 | #include "llvm/ADT/STLExtras.h" |
21 | #include "llvm/ADT/SmallVector.h" |
22 | #include "llvm/ADT/StringRef.h" |
23 | #include "llvm/ADT/Twine.h" |
24 | #include "llvm/ADT/iterator.h" |
25 | #include "llvm/ADT/iterator_range.h" |
26 | #include "llvm/IR/Attributes.h" |
27 | #include "llvm/IR/BasicBlock.h" |
28 | #include "llvm/IR/CallingConv.h" |
29 | #include "llvm/IR/Constant.h" |
30 | #include "llvm/IR/DerivedTypes.h" |
31 | #include "llvm/IR/Function.h" |
32 | #include "llvm/IR/InstrTypes.h" |
33 | #include "llvm/IR/Instruction.h" |
34 | #include "llvm/IR/OperandTraits.h" |
35 | #include "llvm/IR/Type.h" |
36 | #include "llvm/IR/Use.h" |
37 | #include "llvm/IR/User.h" |
38 | #include "llvm/IR/Value.h" |
39 | #include "llvm/Support/AtomicOrdering.h" |
40 | #include "llvm/Support/Casting.h" |
41 | #include "llvm/Support/ErrorHandling.h" |
42 | #include <cassert> |
43 | #include <cstddef> |
44 | #include <cstdint> |
45 | #include <iterator> |
46 | |
47 | namespace llvm { |
48 | |
49 | class APInt; |
50 | class ConstantInt; |
51 | class DataLayout; |
52 | class LLVMContext; |
53 | |
54 | //===----------------------------------------------------------------------===// |
55 | // AllocaInst Class |
56 | //===----------------------------------------------------------------------===// |
57 | |
58 | /// an instruction to allocate memory on the stack |
59 | class AllocaInst : public UnaryInstruction { |
60 | Type *AllocatedType; |
61 | |
62 | protected: |
63 | // Note: Instruction needs to be a friend here to call cloneImpl. |
64 | friend class Instruction; |
65 | |
66 | AllocaInst *cloneImpl() const; |
67 | |
68 | public: |
69 | explicit AllocaInst(Type *Ty, unsigned AddrSpace, |
70 | Value *ArraySize = nullptr, |
71 | const Twine &Name = "", |
72 | Instruction *InsertBefore = nullptr); |
73 | AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
74 | const Twine &Name, BasicBlock *InsertAtEnd); |
75 | |
76 | AllocaInst(Type *Ty, unsigned AddrSpace, |
77 | const Twine &Name, Instruction *InsertBefore = nullptr); |
78 | AllocaInst(Type *Ty, unsigned AddrSpace, |
79 | const Twine &Name, BasicBlock *InsertAtEnd); |
80 | |
81 | AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, unsigned Align, |
82 | const Twine &Name = "", Instruction *InsertBefore = nullptr); |
83 | AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, unsigned Align, |
84 | const Twine &Name, BasicBlock *InsertAtEnd); |
85 | |
86 | /// Return true if there is an allocation size parameter to the allocation |
87 | /// instruction that is not 1. |
88 | bool isArrayAllocation() const; |
89 | |
90 | /// Get the number of elements allocated. For a simple allocation of a single |
91 | /// element, this will return a constant 1 value. |
92 | const Value *getArraySize() const { return getOperand(0); } |
93 | Value *getArraySize() { return getOperand(0); } |
94 | |
95 | /// Overload to return most specific pointer type. |
96 | PointerType *getType() const { |
97 | return cast<PointerType>(Instruction::getType()); |
98 | } |
99 | |
100 | /// Get allocation size in bits. Returns None if size can't be determined, |
101 | /// e.g. in case of a VLA. |
102 | Optional<uint64_t> getAllocationSizeInBits(const DataLayout &DL) const; |
103 | |
104 | /// Return the type that is being allocated by the instruction. |
105 | Type *getAllocatedType() const { return AllocatedType; } |
106 | /// for use only in special circumstances that need to generically |
107 | /// transform a whole instruction (eg: IR linking and vectorization). |
108 | void setAllocatedType(Type *Ty) { AllocatedType = Ty; } |
109 | |
110 | /// Return the alignment of the memory that is being allocated by the |
111 | /// instruction. |
112 | unsigned getAlignment() const { |
113 | if (const auto MA = decodeMaybeAlign(getSubclassDataFromInstruction() & 31)) |
114 | return MA->value(); |
115 | return 0; |
116 | } |
117 | void setAlignment(MaybeAlign Align); |
118 | |
119 | /// Return true if this alloca is in the entry block of the function and is a |
120 | /// constant size. If so, the code generator will fold it into the |
121 | /// prolog/epilog code, so it is basically free. |
122 | bool isStaticAlloca() const; |
123 | |
124 | /// Return true if this alloca is used as an inalloca argument to a call. Such |
125 | /// allocas are never considered static even if they are in the entry block. |
126 | bool isUsedWithInAlloca() const { |
127 | return getSubclassDataFromInstruction() & 32; |
128 | } |
129 | |
130 | /// Specify whether this alloca is used to represent the arguments to a call. |
131 | void setUsedWithInAlloca(bool V) { |
132 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) | |
133 | (V ? 32 : 0)); |
134 | } |
135 | |
136 | /// Return true if this alloca is used as a swifterror argument to a call. |
137 | bool isSwiftError() const { |
138 | return getSubclassDataFromInstruction() & 64; |
139 | } |
140 | |
141 | /// Specify whether this alloca is used to represent a swifterror. |
142 | void setSwiftError(bool V) { |
143 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~64) | |
144 | (V ? 64 : 0)); |
145 | } |
146 | |
147 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
148 | static bool classof(const Instruction *I) { |
149 | return (I->getOpcode() == Instruction::Alloca); |
150 | } |
151 | static bool classof(const Value *V) { |
152 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
153 | } |
154 | |
155 | private: |
156 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
157 | // method so that subclasses cannot accidentally use it. |
158 | void setInstructionSubclassData(unsigned short D) { |
159 | Instruction::setInstructionSubclassData(D); |
160 | } |
161 | }; |
162 | |
163 | //===----------------------------------------------------------------------===// |
164 | // LoadInst Class |
165 | //===----------------------------------------------------------------------===// |
166 | |
167 | /// An instruction for reading from memory. This uses the SubclassData field in |
168 | /// Value to store whether or not the load is volatile. |
169 | class LoadInst : public UnaryInstruction { |
170 | void AssertOK(); |
171 | |
172 | protected: |
173 | // Note: Instruction needs to be a friend here to call cloneImpl. |
174 | friend class Instruction; |
175 | |
176 | LoadInst *cloneImpl() const; |
177 | |
178 | public: |
179 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr = "", |
180 | Instruction *InsertBefore = nullptr); |
181 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd); |
182 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, |
183 | Instruction *InsertBefore = nullptr); |
184 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, |
185 | BasicBlock *InsertAtEnd); |
186 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, |
187 | unsigned Align, Instruction *InsertBefore = nullptr); |
188 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, |
189 | unsigned Align, BasicBlock *InsertAtEnd); |
190 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, |
191 | unsigned Align, AtomicOrdering Order, |
192 | SyncScope::ID SSID = SyncScope::System, |
193 | Instruction *InsertBefore = nullptr); |
194 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, |
195 | unsigned Align, AtomicOrdering Order, SyncScope::ID SSID, |
196 | BasicBlock *InsertAtEnd); |
197 | |
198 | // Deprecated [opaque pointer types] |
199 | explicit LoadInst(Value *Ptr, const Twine &NameStr = "", |
200 | Instruction *InsertBefore = nullptr) |
201 | : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr, |
202 | InsertBefore) {} |
203 | LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd) |
204 | : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr, |
205 | InsertAtEnd) {} |
206 | LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, |
207 | Instruction *InsertBefore = nullptr) |
208 | : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr, |
209 | isVolatile, InsertBefore) {} |
210 | LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, |
211 | BasicBlock *InsertAtEnd) |
212 | : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr, |
213 | isVolatile, InsertAtEnd) {} |
214 | LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align, |
215 | Instruction *InsertBefore = nullptr) |
216 | : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr, |
217 | isVolatile, Align, InsertBefore) {} |
218 | LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align, |
219 | BasicBlock *InsertAtEnd) |
220 | : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr, |
221 | isVolatile, Align, InsertAtEnd) {} |
222 | LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align, |
223 | AtomicOrdering Order, SyncScope::ID SSID = SyncScope::System, |
224 | Instruction *InsertBefore = nullptr) |
225 | : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr, |
226 | isVolatile, Align, Order, SSID, InsertBefore) {} |
227 | LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align, |
228 | AtomicOrdering Order, SyncScope::ID SSID, BasicBlock *InsertAtEnd) |
229 | : LoadInst(Ptr->getType()->getPointerElementType(), Ptr, NameStr, |
230 | isVolatile, Align, Order, SSID, InsertAtEnd) {} |
231 | |
232 | /// Return true if this is a load from a volatile memory location. |
233 | bool isVolatile() const { return getSubclassDataFromInstruction() & 1; } |
234 | |
235 | /// Specify whether this is a volatile load or not. |
236 | void setVolatile(bool V) { |
237 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | |
238 | (V ? 1 : 0)); |
239 | } |
240 | |
241 | /// Return the alignment of the access that is being performed. |
242 | unsigned getAlignment() const { |
243 | if (const auto MA = |
244 | decodeMaybeAlign((getSubclassDataFromInstruction() >> 1) & 31)) |
245 | return MA->value(); |
246 | return 0; |
247 | } |
248 | |
249 | void setAlignment(MaybeAlign Align); |
250 | |
251 | /// Returns the ordering constraint of this load instruction. |
252 | AtomicOrdering getOrdering() const { |
253 | return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7); |
254 | } |
255 | |
256 | /// Sets the ordering constraint of this load instruction. May not be Release |
257 | /// or AcquireRelease. |
258 | void setOrdering(AtomicOrdering Ordering) { |
259 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) | |
260 | ((unsigned)Ordering << 7)); |
261 | } |
262 | |
263 | /// Returns the synchronization scope ID of this load instruction. |
264 | SyncScope::ID getSyncScopeID() const { |
265 | return SSID; |
266 | } |
267 | |
268 | /// Sets the synchronization scope ID of this load instruction. |
269 | void setSyncScopeID(SyncScope::ID SSID) { |
270 | this->SSID = SSID; |
271 | } |
272 | |
273 | /// Sets the ordering constraint and the synchronization scope ID of this load |
274 | /// instruction. |
275 | void setAtomic(AtomicOrdering Ordering, |
276 | SyncScope::ID SSID = SyncScope::System) { |
277 | setOrdering(Ordering); |
278 | setSyncScopeID(SSID); |
279 | } |
280 | |
281 | bool isSimple() const { return !isAtomic() && !isVolatile(); } |
282 | |
283 | bool isUnordered() const { |
284 | return (getOrdering() == AtomicOrdering::NotAtomic || |
285 | getOrdering() == AtomicOrdering::Unordered) && |
286 | !isVolatile(); |
287 | } |
288 | |
289 | Value *getPointerOperand() { return getOperand(0); } |
290 | const Value *getPointerOperand() const { return getOperand(0); } |
291 | static unsigned getPointerOperandIndex() { return 0U; } |
292 | Type *getPointerOperandType() const { return getPointerOperand()->getType(); } |
293 | |
294 | /// Returns the address space of the pointer operand. |
295 | unsigned getPointerAddressSpace() const { |
296 | return getPointerOperandType()->getPointerAddressSpace(); |
297 | } |
298 | |
299 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
300 | static bool classof(const Instruction *I) { |
301 | return I->getOpcode() == Instruction::Load; |
302 | } |
303 | static bool classof(const Value *V) { |
304 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
305 | } |
306 | |
307 | private: |
308 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
309 | // method so that subclasses cannot accidentally use it. |
310 | void setInstructionSubclassData(unsigned short D) { |
311 | Instruction::setInstructionSubclassData(D); |
312 | } |
313 | |
314 | /// The synchronization scope ID of this load instruction. Not quite enough |
315 | /// room in SubClassData for everything, so synchronization scope ID gets its |
316 | /// own field. |
317 | SyncScope::ID SSID; |
318 | }; |
319 | |
320 | //===----------------------------------------------------------------------===// |
321 | // StoreInst Class |
322 | //===----------------------------------------------------------------------===// |
323 | |
324 | /// An instruction for storing to memory. |
325 | class StoreInst : public Instruction { |
326 | void AssertOK(); |
327 | |
328 | protected: |
329 | // Note: Instruction needs to be a friend here to call cloneImpl. |
330 | friend class Instruction; |
331 | |
332 | StoreInst *cloneImpl() const; |
333 | |
334 | public: |
335 | StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore); |
336 | StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd); |
337 | StoreInst(Value *Val, Value *Ptr, bool isVolatile = false, |
338 | Instruction *InsertBefore = nullptr); |
339 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd); |
340 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, |
341 | unsigned Align, Instruction *InsertBefore = nullptr); |
342 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, |
343 | unsigned Align, BasicBlock *InsertAtEnd); |
344 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, |
345 | unsigned Align, AtomicOrdering Order, |
346 | SyncScope::ID SSID = SyncScope::System, |
347 | Instruction *InsertBefore = nullptr); |
348 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, |
349 | unsigned Align, AtomicOrdering Order, SyncScope::ID SSID, |
350 | BasicBlock *InsertAtEnd); |
351 | |
352 | // allocate space for exactly two operands |
353 | void *operator new(size_t s) { |
354 | return User::operator new(s, 2); |
355 | } |
356 | |
357 | /// Return true if this is a store to a volatile memory location. |
358 | bool isVolatile() const { return getSubclassDataFromInstruction() & 1; } |
359 | |
360 | /// Specify whether this is a volatile store or not. |
361 | void setVolatile(bool V) { |
362 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | |
363 | (V ? 1 : 0)); |
364 | } |
365 | |
366 | /// Transparently provide more efficient getOperand methods. |
367 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
368 | |
369 | /// Return the alignment of the access that is being performed |
370 | unsigned getAlignment() const { |
371 | if (const auto MA = |
372 | decodeMaybeAlign((getSubclassDataFromInstruction() >> 1) & 31)) |
373 | return MA->value(); |
374 | return 0; |
375 | } |
376 | |
377 | void setAlignment(MaybeAlign Align); |
378 | |
379 | /// Returns the ordering constraint of this store instruction. |
380 | AtomicOrdering getOrdering() const { |
381 | return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7); |
382 | } |
383 | |
384 | /// Sets the ordering constraint of this store instruction. May not be |
385 | /// Acquire or AcquireRelease. |
386 | void setOrdering(AtomicOrdering Ordering) { |
387 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) | |
388 | ((unsigned)Ordering << 7)); |
389 | } |
390 | |
391 | /// Returns the synchronization scope ID of this store instruction. |
392 | SyncScope::ID getSyncScopeID() const { |
393 | return SSID; |
394 | } |
395 | |
396 | /// Sets the synchronization scope ID of this store instruction. |
397 | void setSyncScopeID(SyncScope::ID SSID) { |
398 | this->SSID = SSID; |
399 | } |
400 | |
401 | /// Sets the ordering constraint and the synchronization scope ID of this |
402 | /// store instruction. |
403 | void setAtomic(AtomicOrdering Ordering, |
404 | SyncScope::ID SSID = SyncScope::System) { |
405 | setOrdering(Ordering); |
406 | setSyncScopeID(SSID); |
407 | } |
408 | |
409 | bool isSimple() const { return !isAtomic() && !isVolatile(); } |
410 | |
411 | bool isUnordered() const { |
412 | return (getOrdering() == AtomicOrdering::NotAtomic || |
413 | getOrdering() == AtomicOrdering::Unordered) && |
414 | !isVolatile(); |
415 | } |
416 | |
417 | Value *getValueOperand() { return getOperand(0); } |
418 | const Value *getValueOperand() const { return getOperand(0); } |
419 | |
420 | Value *getPointerOperand() { return getOperand(1); } |
421 | const Value *getPointerOperand() const { return getOperand(1); } |
422 | static unsigned getPointerOperandIndex() { return 1U; } |
423 | Type *getPointerOperandType() const { return getPointerOperand()->getType(); } |
424 | |
425 | /// Returns the address space of the pointer operand. |
426 | unsigned getPointerAddressSpace() const { |
427 | return getPointerOperandType()->getPointerAddressSpace(); |
428 | } |
429 | |
430 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
431 | static bool classof(const Instruction *I) { |
432 | return I->getOpcode() == Instruction::Store; |
433 | } |
434 | static bool classof(const Value *V) { |
435 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
436 | } |
437 | |
438 | private: |
439 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
440 | // method so that subclasses cannot accidentally use it. |
441 | void setInstructionSubclassData(unsigned short D) { |
442 | Instruction::setInstructionSubclassData(D); |
443 | } |
444 | |
445 | /// The synchronization scope ID of this store instruction. Not quite enough |
446 | /// room in SubClassData for everything, so synchronization scope ID gets its |
447 | /// own field. |
448 | SyncScope::ID SSID; |
449 | }; |
450 | |
451 | template <> |
452 | struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> { |
453 | }; |
454 | |
455 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)StoreInst::op_iterator StoreInst::op_begin() { return OperandTraits <StoreInst>::op_begin(this); } StoreInst::const_op_iterator StoreInst::op_begin() const { return OperandTraits<StoreInst >::op_begin(const_cast<StoreInst*>(this)); } StoreInst ::op_iterator StoreInst::op_end() { return OperandTraits<StoreInst >::op_end(this); } StoreInst::const_op_iterator StoreInst:: op_end() const { return OperandTraits<StoreInst>::op_end (const_cast<StoreInst*>(this)); } Value *StoreInst::getOperand (unsigned i_nocapture) const { ((i_nocapture < OperandTraits <StoreInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<StoreInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 455, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<StoreInst>::op_begin(const_cast<StoreInst *>(this))[i_nocapture].get()); } void StoreInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<StoreInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<StoreInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 455, __PRETTY_FUNCTION__)); OperandTraits<StoreInst>:: op_begin(this)[i_nocapture] = Val_nocapture; } unsigned StoreInst ::getNumOperands() const { return OperandTraits<StoreInst> ::operands(this); } template <int Idx_nocapture> Use & StoreInst::Op() { return this->OpFrom<Idx_nocapture> (this); } template <int Idx_nocapture> const Use &StoreInst ::Op() const { return this->OpFrom<Idx_nocapture>(this ); } |
456 | |
457 | //===----------------------------------------------------------------------===// |
458 | // FenceInst Class |
459 | //===----------------------------------------------------------------------===// |
460 | |
461 | /// An instruction for ordering other memory operations. |
462 | class FenceInst : public Instruction { |
463 | void Init(AtomicOrdering Ordering, SyncScope::ID SSID); |
464 | |
465 | protected: |
466 | // Note: Instruction needs to be a friend here to call cloneImpl. |
467 | friend class Instruction; |
468 | |
469 | FenceInst *cloneImpl() const; |
470 | |
471 | public: |
472 | // Ordering may only be Acquire, Release, AcquireRelease, or |
473 | // SequentiallyConsistent. |
474 | FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
475 | SyncScope::ID SSID = SyncScope::System, |
476 | Instruction *InsertBefore = nullptr); |
477 | FenceInst(LLVMContext &C, AtomicOrdering Ordering, SyncScope::ID SSID, |
478 | BasicBlock *InsertAtEnd); |
479 | |
480 | // allocate space for exactly zero operands |
481 | void *operator new(size_t s) { |
482 | return User::operator new(s, 0); |
483 | } |
484 | |
485 | /// Returns the ordering constraint of this fence instruction. |
486 | AtomicOrdering getOrdering() const { |
487 | return AtomicOrdering(getSubclassDataFromInstruction() >> 1); |
488 | } |
489 | |
490 | /// Sets the ordering constraint of this fence instruction. May only be |
491 | /// Acquire, Release, AcquireRelease, or SequentiallyConsistent. |
492 | void setOrdering(AtomicOrdering Ordering) { |
493 | setInstructionSubclassData((getSubclassDataFromInstruction() & 1) | |
494 | ((unsigned)Ordering << 1)); |
495 | } |
496 | |
497 | /// Returns the synchronization scope ID of this fence instruction. |
498 | SyncScope::ID getSyncScopeID() const { |
499 | return SSID; |
500 | } |
501 | |
502 | /// Sets the synchronization scope ID of this fence instruction. |
503 | void setSyncScopeID(SyncScope::ID SSID) { |
504 | this->SSID = SSID; |
505 | } |
506 | |
507 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
508 | static bool classof(const Instruction *I) { |
509 | return I->getOpcode() == Instruction::Fence; |
510 | } |
511 | static bool classof(const Value *V) { |
512 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
513 | } |
514 | |
515 | private: |
516 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
517 | // method so that subclasses cannot accidentally use it. |
518 | void setInstructionSubclassData(unsigned short D) { |
519 | Instruction::setInstructionSubclassData(D); |
520 | } |
521 | |
522 | /// The synchronization scope ID of this fence instruction. Not quite enough |
523 | /// room in SubClassData for everything, so synchronization scope ID gets its |
524 | /// own field. |
525 | SyncScope::ID SSID; |
526 | }; |
527 | |
528 | //===----------------------------------------------------------------------===// |
529 | // AtomicCmpXchgInst Class |
530 | //===----------------------------------------------------------------------===// |
531 | |
532 | /// An instruction that atomically checks whether a |
533 | /// specified value is in a memory location, and, if it is, stores a new value |
534 | /// there. The value returned by this instruction is a pair containing the |
535 | /// original value as first element, and an i1 indicating success (true) or |
536 | /// failure (false) as second element. |
537 | /// |
538 | class AtomicCmpXchgInst : public Instruction { |
539 | void Init(Value *Ptr, Value *Cmp, Value *NewVal, |
540 | AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering, |
541 | SyncScope::ID SSID); |
542 | |
543 | protected: |
544 | // Note: Instruction needs to be a friend here to call cloneImpl. |
545 | friend class Instruction; |
546 | |
547 | AtomicCmpXchgInst *cloneImpl() const; |
548 | |
549 | public: |
550 | AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
551 | AtomicOrdering SuccessOrdering, |
552 | AtomicOrdering FailureOrdering, |
553 | SyncScope::ID SSID, Instruction *InsertBefore = nullptr); |
554 | AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
555 | AtomicOrdering SuccessOrdering, |
556 | AtomicOrdering FailureOrdering, |
557 | SyncScope::ID SSID, BasicBlock *InsertAtEnd); |
558 | |
559 | // allocate space for exactly three operands |
560 | void *operator new(size_t s) { |
561 | return User::operator new(s, 3); |
562 | } |
563 | |
564 | /// Return true if this is a cmpxchg from a volatile memory |
565 | /// location. |
566 | /// |
567 | bool isVolatile() const { |
568 | return getSubclassDataFromInstruction() & 1; |
569 | } |
570 | |
571 | /// Specify whether this is a volatile cmpxchg. |
572 | /// |
573 | void setVolatile(bool V) { |
574 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | |
575 | (unsigned)V); |
576 | } |
577 | |
578 | /// Return true if this cmpxchg may spuriously fail. |
579 | bool isWeak() const { |
580 | return getSubclassDataFromInstruction() & 0x100; |
581 | } |
582 | |
583 | void setWeak(bool IsWeak) { |
584 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x100) | |
585 | (IsWeak << 8)); |
586 | } |
587 | |
588 | /// Transparently provide more efficient getOperand methods. |
589 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
590 | |
591 | /// Returns the success ordering constraint of this cmpxchg instruction. |
592 | AtomicOrdering getSuccessOrdering() const { |
593 | return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7); |
594 | } |
595 | |
596 | /// Sets the success ordering constraint of this cmpxchg instruction. |
597 | void setSuccessOrdering(AtomicOrdering Ordering) { |
598 | assert(Ordering != AtomicOrdering::NotAtomic &&((Ordering != AtomicOrdering::NotAtomic && "CmpXchg instructions can only be atomic." ) ? static_cast<void> (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"CmpXchg instructions can only be atomic.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 599, __PRETTY_FUNCTION__)) |
599 | "CmpXchg instructions can only be atomic.")((Ordering != AtomicOrdering::NotAtomic && "CmpXchg instructions can only be atomic." ) ? static_cast<void> (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"CmpXchg instructions can only be atomic.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 599, __PRETTY_FUNCTION__)); |
600 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) | |
601 | ((unsigned)Ordering << 2)); |
602 | } |
603 | |
604 | /// Returns the failure ordering constraint of this cmpxchg instruction. |
605 | AtomicOrdering getFailureOrdering() const { |
606 | return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7); |
607 | } |
608 | |
609 | /// Sets the failure ordering constraint of this cmpxchg instruction. |
610 | void setFailureOrdering(AtomicOrdering Ordering) { |
611 | assert(Ordering != AtomicOrdering::NotAtomic &&((Ordering != AtomicOrdering::NotAtomic && "CmpXchg instructions can only be atomic." ) ? static_cast<void> (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"CmpXchg instructions can only be atomic.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 612, __PRETTY_FUNCTION__)) |
612 | "CmpXchg instructions can only be atomic.")((Ordering != AtomicOrdering::NotAtomic && "CmpXchg instructions can only be atomic." ) ? static_cast<void> (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"CmpXchg instructions can only be atomic.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 612, __PRETTY_FUNCTION__)); |
613 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) | |
614 | ((unsigned)Ordering << 5)); |
615 | } |
616 | |
617 | /// Returns the synchronization scope ID of this cmpxchg instruction. |
618 | SyncScope::ID getSyncScopeID() const { |
619 | return SSID; |
620 | } |
621 | |
622 | /// Sets the synchronization scope ID of this cmpxchg instruction. |
623 | void setSyncScopeID(SyncScope::ID SSID) { |
624 | this->SSID = SSID; |
625 | } |
626 | |
627 | Value *getPointerOperand() { return getOperand(0); } |
628 | const Value *getPointerOperand() const { return getOperand(0); } |
629 | static unsigned getPointerOperandIndex() { return 0U; } |
630 | |
631 | Value *getCompareOperand() { return getOperand(1); } |
632 | const Value *getCompareOperand() const { return getOperand(1); } |
633 | |
634 | Value *getNewValOperand() { return getOperand(2); } |
635 | const Value *getNewValOperand() const { return getOperand(2); } |
636 | |
637 | /// Returns the address space of the pointer operand. |
638 | unsigned getPointerAddressSpace() const { |
639 | return getPointerOperand()->getType()->getPointerAddressSpace(); |
640 | } |
641 | |
642 | /// Returns the strongest permitted ordering on failure, given the |
643 | /// desired ordering on success. |
644 | /// |
645 | /// If the comparison in a cmpxchg operation fails, there is no atomic store |
646 | /// so release semantics cannot be provided. So this function drops explicit |
647 | /// Release requests from the AtomicOrdering. A SequentiallyConsistent |
648 | /// operation would remain SequentiallyConsistent. |
649 | static AtomicOrdering |
650 | getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) { |
651 | switch (SuccessOrdering) { |
652 | default: |
653 | llvm_unreachable("invalid cmpxchg success ordering")::llvm::llvm_unreachable_internal("invalid cmpxchg success ordering" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 653); |
654 | case AtomicOrdering::Release: |
655 | case AtomicOrdering::Monotonic: |
656 | return AtomicOrdering::Monotonic; |
657 | case AtomicOrdering::AcquireRelease: |
658 | case AtomicOrdering::Acquire: |
659 | return AtomicOrdering::Acquire; |
660 | case AtomicOrdering::SequentiallyConsistent: |
661 | return AtomicOrdering::SequentiallyConsistent; |
662 | } |
663 | } |
664 | |
665 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
666 | static bool classof(const Instruction *I) { |
667 | return I->getOpcode() == Instruction::AtomicCmpXchg; |
668 | } |
669 | static bool classof(const Value *V) { |
670 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
671 | } |
672 | |
673 | private: |
674 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
675 | // method so that subclasses cannot accidentally use it. |
676 | void setInstructionSubclassData(unsigned short D) { |
677 | Instruction::setInstructionSubclassData(D); |
678 | } |
679 | |
680 | /// The synchronization scope ID of this cmpxchg instruction. Not quite |
681 | /// enough room in SubClassData for everything, so synchronization scope ID |
682 | /// gets its own field. |
683 | SyncScope::ID SSID; |
684 | }; |
685 | |
686 | template <> |
687 | struct OperandTraits<AtomicCmpXchgInst> : |
688 | public FixedNumOperandTraits<AtomicCmpXchgInst, 3> { |
689 | }; |
690 | |
691 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)AtomicCmpXchgInst::op_iterator AtomicCmpXchgInst::op_begin() { return OperandTraits<AtomicCmpXchgInst>::op_begin(this ); } AtomicCmpXchgInst::const_op_iterator AtomicCmpXchgInst:: op_begin() const { return OperandTraits<AtomicCmpXchgInst> ::op_begin(const_cast<AtomicCmpXchgInst*>(this)); } AtomicCmpXchgInst ::op_iterator AtomicCmpXchgInst::op_end() { return OperandTraits <AtomicCmpXchgInst>::op_end(this); } AtomicCmpXchgInst:: const_op_iterator AtomicCmpXchgInst::op_end() const { return OperandTraits <AtomicCmpXchgInst>::op_end(const_cast<AtomicCmpXchgInst *>(this)); } Value *AtomicCmpXchgInst::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<AtomicCmpXchgInst >::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicCmpXchgInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 691, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<AtomicCmpXchgInst>::op_begin(const_cast <AtomicCmpXchgInst*>(this))[i_nocapture].get()); } void AtomicCmpXchgInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((i_nocapture < OperandTraits<AtomicCmpXchgInst> ::operands(this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicCmpXchgInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 691, __PRETTY_FUNCTION__)); OperandTraits<AtomicCmpXchgInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned AtomicCmpXchgInst::getNumOperands() const { return OperandTraits <AtomicCmpXchgInst>::operands(this); } template <int Idx_nocapture> Use &AtomicCmpXchgInst::Op() { return this ->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture > const Use &AtomicCmpXchgInst::Op() const { return this ->OpFrom<Idx_nocapture>(this); } |
692 | |
693 | //===----------------------------------------------------------------------===// |
694 | // AtomicRMWInst Class |
695 | //===----------------------------------------------------------------------===// |
696 | |
697 | /// an instruction that atomically reads a memory location, |
698 | /// combines it with another value, and then stores the result back. Returns |
699 | /// the old value. |
700 | /// |
701 | class AtomicRMWInst : public Instruction { |
702 | protected: |
703 | // Note: Instruction needs to be a friend here to call cloneImpl. |
704 | friend class Instruction; |
705 | |
706 | AtomicRMWInst *cloneImpl() const; |
707 | |
708 | public: |
709 | /// This enumeration lists the possible modifications atomicrmw can make. In |
710 | /// the descriptions, 'p' is the pointer to the instruction's memory location, |
711 | /// 'old' is the initial value of *p, and 'v' is the other value passed to the |
712 | /// instruction. These instructions always return 'old'. |
713 | enum BinOp { |
714 | /// *p = v |
715 | Xchg, |
716 | /// *p = old + v |
717 | Add, |
718 | /// *p = old - v |
719 | Sub, |
720 | /// *p = old & v |
721 | And, |
722 | /// *p = ~(old & v) |
723 | Nand, |
724 | /// *p = old | v |
725 | Or, |
726 | /// *p = old ^ v |
727 | Xor, |
728 | /// *p = old >signed v ? old : v |
729 | Max, |
730 | /// *p = old <signed v ? old : v |
731 | Min, |
732 | /// *p = old >unsigned v ? old : v |
733 | UMax, |
734 | /// *p = old <unsigned v ? old : v |
735 | UMin, |
736 | |
737 | /// *p = old + v |
738 | FAdd, |
739 | |
740 | /// *p = old - v |
741 | FSub, |
742 | |
743 | FIRST_BINOP = Xchg, |
744 | LAST_BINOP = FSub, |
745 | BAD_BINOP |
746 | }; |
747 | |
748 | AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
749 | AtomicOrdering Ordering, SyncScope::ID SSID, |
750 | Instruction *InsertBefore = nullptr); |
751 | AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
752 | AtomicOrdering Ordering, SyncScope::ID SSID, |
753 | BasicBlock *InsertAtEnd); |
754 | |
755 | // allocate space for exactly two operands |
756 | void *operator new(size_t s) { |
757 | return User::operator new(s, 2); |
758 | } |
759 | |
760 | BinOp getOperation() const { |
761 | return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5); |
762 | } |
763 | |
764 | static StringRef getOperationName(BinOp Op); |
765 | |
766 | static bool isFPOperation(BinOp Op) { |
767 | switch (Op) { |
768 | case AtomicRMWInst::FAdd: |
769 | case AtomicRMWInst::FSub: |
770 | return true; |
771 | default: |
772 | return false; |
773 | } |
774 | } |
775 | |
776 | void setOperation(BinOp Operation) { |
777 | unsigned short SubclassData = getSubclassDataFromInstruction(); |
778 | setInstructionSubclassData((SubclassData & 31) | |
779 | (Operation << 5)); |
780 | } |
781 | |
782 | /// Return true if this is a RMW on a volatile memory location. |
783 | /// |
784 | bool isVolatile() const { |
785 | return getSubclassDataFromInstruction() & 1; |
786 | } |
787 | |
788 | /// Specify whether this is a volatile RMW or not. |
789 | /// |
790 | void setVolatile(bool V) { |
791 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | |
792 | (unsigned)V); |
793 | } |
794 | |
795 | /// Transparently provide more efficient getOperand methods. |
796 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
797 | |
798 | /// Returns the ordering constraint of this rmw instruction. |
799 | AtomicOrdering getOrdering() const { |
800 | return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7); |
801 | } |
802 | |
803 | /// Sets the ordering constraint of this rmw instruction. |
804 | void setOrdering(AtomicOrdering Ordering) { |
805 | assert(Ordering != AtomicOrdering::NotAtomic &&((Ordering != AtomicOrdering::NotAtomic && "atomicrmw instructions can only be atomic." ) ? static_cast<void> (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"atomicrmw instructions can only be atomic.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 806, __PRETTY_FUNCTION__)) |
806 | "atomicrmw instructions can only be atomic.")((Ordering != AtomicOrdering::NotAtomic && "atomicrmw instructions can only be atomic." ) ? static_cast<void> (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"atomicrmw instructions can only be atomic.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 806, __PRETTY_FUNCTION__)); |
807 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) | |
808 | ((unsigned)Ordering << 2)); |
809 | } |
810 | |
811 | /// Returns the synchronization scope ID of this rmw instruction. |
812 | SyncScope::ID getSyncScopeID() const { |
813 | return SSID; |
814 | } |
815 | |
816 | /// Sets the synchronization scope ID of this rmw instruction. |
817 | void setSyncScopeID(SyncScope::ID SSID) { |
818 | this->SSID = SSID; |
819 | } |
820 | |
821 | Value *getPointerOperand() { return getOperand(0); } |
822 | const Value *getPointerOperand() const { return getOperand(0); } |
823 | static unsigned getPointerOperandIndex() { return 0U; } |
824 | |
825 | Value *getValOperand() { return getOperand(1); } |
826 | const Value *getValOperand() const { return getOperand(1); } |
827 | |
828 | /// Returns the address space of the pointer operand. |
829 | unsigned getPointerAddressSpace() const { |
830 | return getPointerOperand()->getType()->getPointerAddressSpace(); |
831 | } |
832 | |
833 | bool isFloatingPointOperation() const { |
834 | return isFPOperation(getOperation()); |
835 | } |
836 | |
837 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
838 | static bool classof(const Instruction *I) { |
839 | return I->getOpcode() == Instruction::AtomicRMW; |
840 | } |
841 | static bool classof(const Value *V) { |
842 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
843 | } |
844 | |
845 | private: |
846 | void Init(BinOp Operation, Value *Ptr, Value *Val, |
847 | AtomicOrdering Ordering, SyncScope::ID SSID); |
848 | |
849 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
850 | // method so that subclasses cannot accidentally use it. |
851 | void setInstructionSubclassData(unsigned short D) { |
852 | Instruction::setInstructionSubclassData(D); |
853 | } |
854 | |
855 | /// The synchronization scope ID of this rmw instruction. Not quite enough |
856 | /// room in SubClassData for everything, so synchronization scope ID gets its |
857 | /// own field. |
858 | SyncScope::ID SSID; |
859 | }; |
860 | |
861 | template <> |
862 | struct OperandTraits<AtomicRMWInst> |
863 | : public FixedNumOperandTraits<AtomicRMWInst,2> { |
864 | }; |
865 | |
866 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)AtomicRMWInst::op_iterator AtomicRMWInst::op_begin() { return OperandTraits<AtomicRMWInst>::op_begin(this); } AtomicRMWInst ::const_op_iterator AtomicRMWInst::op_begin() const { return OperandTraits <AtomicRMWInst>::op_begin(const_cast<AtomicRMWInst*> (this)); } AtomicRMWInst::op_iterator AtomicRMWInst::op_end() { return OperandTraits<AtomicRMWInst>::op_end(this); } AtomicRMWInst::const_op_iterator AtomicRMWInst::op_end() const { return OperandTraits<AtomicRMWInst>::op_end(const_cast <AtomicRMWInst*>(this)); } Value *AtomicRMWInst::getOperand (unsigned i_nocapture) const { ((i_nocapture < OperandTraits <AtomicRMWInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicRMWInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 866, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<AtomicRMWInst>::op_begin(const_cast< AtomicRMWInst*>(this))[i_nocapture].get()); } void AtomicRMWInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<AtomicRMWInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicRMWInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 866, __PRETTY_FUNCTION__)); OperandTraits<AtomicRMWInst> ::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned AtomicRMWInst ::getNumOperands() const { return OperandTraits<AtomicRMWInst >::operands(this); } template <int Idx_nocapture> Use &AtomicRMWInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & AtomicRMWInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } |
867 | |
868 | //===----------------------------------------------------------------------===// |
869 | // GetElementPtrInst Class |
870 | //===----------------------------------------------------------------------===// |
871 | |
872 | // checkGEPType - Simple wrapper function to give a better assertion failure |
873 | // message on bad indexes for a gep instruction. |
874 | // |
875 | inline Type *checkGEPType(Type *Ty) { |
876 | assert(Ty && "Invalid GetElementPtrInst indices for type!")((Ty && "Invalid GetElementPtrInst indices for type!" ) ? static_cast<void> (0) : __assert_fail ("Ty && \"Invalid GetElementPtrInst indices for type!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 876, __PRETTY_FUNCTION__)); |
877 | return Ty; |
878 | } |
879 | |
880 | /// an instruction for type-safe pointer arithmetic to |
881 | /// access elements of arrays and structs |
882 | /// |
883 | class GetElementPtrInst : public Instruction { |
884 | Type *SourceElementType; |
885 | Type *ResultElementType; |
886 | |
887 | GetElementPtrInst(const GetElementPtrInst &GEPI); |
888 | |
889 | /// Constructors - Create a getelementptr instruction with a base pointer an |
890 | /// list of indices. The first ctor can optionally insert before an existing |
891 | /// instruction, the second appends the new instruction to the specified |
892 | /// BasicBlock. |
893 | inline GetElementPtrInst(Type *PointeeType, Value *Ptr, |
894 | ArrayRef<Value *> IdxList, unsigned Values, |
895 | const Twine &NameStr, Instruction *InsertBefore); |
896 | inline GetElementPtrInst(Type *PointeeType, Value *Ptr, |
897 | ArrayRef<Value *> IdxList, unsigned Values, |
898 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
899 | |
900 | void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr); |
901 | |
902 | protected: |
903 | // Note: Instruction needs to be a friend here to call cloneImpl. |
904 | friend class Instruction; |
905 | |
906 | GetElementPtrInst *cloneImpl() const; |
907 | |
908 | public: |
909 | static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr, |
910 | ArrayRef<Value *> IdxList, |
911 | const Twine &NameStr = "", |
912 | Instruction *InsertBefore = nullptr) { |
913 | unsigned Values = 1 + unsigned(IdxList.size()); |
914 | if (!PointeeType) |
915 | PointeeType = |
916 | cast<PointerType>(Ptr->getType()->getScalarType())->getElementType(); |
917 | else |
918 | assert(((PointeeType == cast<PointerType>(Ptr->getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("PointeeType == cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 920, __PRETTY_FUNCTION__)) |
919 | PointeeType ==((PointeeType == cast<PointerType>(Ptr->getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("PointeeType == cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 920, __PRETTY_FUNCTION__)) |
920 | cast<PointerType>(Ptr->getType()->getScalarType())->getElementType())((PointeeType == cast<PointerType>(Ptr->getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("PointeeType == cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 920, __PRETTY_FUNCTION__)); |
921 | return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values, |
922 | NameStr, InsertBefore); |
923 | } |
924 | |
925 | static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr, |
926 | ArrayRef<Value *> IdxList, |
927 | const Twine &NameStr, |
928 | BasicBlock *InsertAtEnd) { |
929 | unsigned Values = 1 + unsigned(IdxList.size()); |
930 | if (!PointeeType) |
931 | PointeeType = |
932 | cast<PointerType>(Ptr->getType()->getScalarType())->getElementType(); |
933 | else |
934 | assert(((PointeeType == cast<PointerType>(Ptr->getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("PointeeType == cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 936, __PRETTY_FUNCTION__)) |
935 | PointeeType ==((PointeeType == cast<PointerType>(Ptr->getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("PointeeType == cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 936, __PRETTY_FUNCTION__)) |
936 | cast<PointerType>(Ptr->getType()->getScalarType())->getElementType())((PointeeType == cast<PointerType>(Ptr->getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("PointeeType == cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 936, __PRETTY_FUNCTION__)); |
937 | return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values, |
938 | NameStr, InsertAtEnd); |
939 | } |
940 | |
941 | /// Create an "inbounds" getelementptr. See the documentation for the |
942 | /// "inbounds" flag in LangRef.html for details. |
943 | static GetElementPtrInst *CreateInBounds(Value *Ptr, |
944 | ArrayRef<Value *> IdxList, |
945 | const Twine &NameStr = "", |
946 | Instruction *InsertBefore = nullptr){ |
947 | return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertBefore); |
948 | } |
949 | |
950 | static GetElementPtrInst * |
951 | CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList, |
952 | const Twine &NameStr = "", |
953 | Instruction *InsertBefore = nullptr) { |
954 | GetElementPtrInst *GEP = |
955 | Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore); |
956 | GEP->setIsInBounds(true); |
957 | return GEP; |
958 | } |
959 | |
960 | static GetElementPtrInst *CreateInBounds(Value *Ptr, |
961 | ArrayRef<Value *> IdxList, |
962 | const Twine &NameStr, |
963 | BasicBlock *InsertAtEnd) { |
964 | return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertAtEnd); |
965 | } |
966 | |
967 | static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr, |
968 | ArrayRef<Value *> IdxList, |
969 | const Twine &NameStr, |
970 | BasicBlock *InsertAtEnd) { |
971 | GetElementPtrInst *GEP = |
972 | Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd); |
973 | GEP->setIsInBounds(true); |
974 | return GEP; |
975 | } |
976 | |
977 | /// Transparently provide more efficient getOperand methods. |
978 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
979 | |
980 | Type *getSourceElementType() const { return SourceElementType; } |
981 | |
982 | void setSourceElementType(Type *Ty) { SourceElementType = Ty; } |
983 | void setResultElementType(Type *Ty) { ResultElementType = Ty; } |
984 | |
985 | Type *getResultElementType() const { |
986 | assert(ResultElementType ==((ResultElementType == cast<PointerType>(getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 987, __PRETTY_FUNCTION__)) |
987 | cast<PointerType>(getType()->getScalarType())->getElementType())((ResultElementType == cast<PointerType>(getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 987, __PRETTY_FUNCTION__)); |
988 | return ResultElementType; |
989 | } |
990 | |
991 | /// Returns the address space of this instruction's pointer type. |
992 | unsigned getAddressSpace() const { |
993 | // Note that this is always the same as the pointer operand's address space |
994 | // and that is cheaper to compute, so cheat here. |
995 | return getPointerAddressSpace(); |
996 | } |
997 | |
998 | /// Returns the type of the element that would be loaded with |
999 | /// a load instruction with the specified parameters. |
1000 | /// |
1001 | /// Null is returned if the indices are invalid for the specified |
1002 | /// pointer type. |
1003 | /// |
1004 | static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList); |
1005 | static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList); |
1006 | static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList); |
1007 | |
1008 | inline op_iterator idx_begin() { return op_begin()+1; } |
1009 | inline const_op_iterator idx_begin() const { return op_begin()+1; } |
1010 | inline op_iterator idx_end() { return op_end(); } |
1011 | inline const_op_iterator idx_end() const { return op_end(); } |
1012 | |
1013 | inline iterator_range<op_iterator> indices() { |
1014 | return make_range(idx_begin(), idx_end()); |
1015 | } |
1016 | |
1017 | inline iterator_range<const_op_iterator> indices() const { |
1018 | return make_range(idx_begin(), idx_end()); |
1019 | } |
1020 | |
1021 | Value *getPointerOperand() { |
1022 | return getOperand(0); |
1023 | } |
1024 | const Value *getPointerOperand() const { |
1025 | return getOperand(0); |
1026 | } |
1027 | static unsigned getPointerOperandIndex() { |
1028 | return 0U; // get index for modifying correct operand. |
1029 | } |
1030 | |
1031 | /// Method to return the pointer operand as a |
1032 | /// PointerType. |
1033 | Type *getPointerOperandType() const { |
1034 | return getPointerOperand()->getType(); |
1035 | } |
1036 | |
1037 | /// Returns the address space of the pointer operand. |
1038 | unsigned getPointerAddressSpace() const { |
1039 | return getPointerOperandType()->getPointerAddressSpace(); |
1040 | } |
1041 | |
1042 | /// Returns the pointer type returned by the GEP |
1043 | /// instruction, which may be a vector of pointers. |
1044 | static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) { |
1045 | return getGEPReturnType( |
1046 | cast<PointerType>(Ptr->getType()->getScalarType())->getElementType(), |
1047 | Ptr, IdxList); |
1048 | } |
1049 | static Type *getGEPReturnType(Type *ElTy, Value *Ptr, |
1050 | ArrayRef<Value *> IdxList) { |
1051 | Type *PtrTy = PointerType::get(checkGEPType(getIndexedType(ElTy, IdxList)), |
1052 | Ptr->getType()->getPointerAddressSpace()); |
1053 | // Vector GEP |
1054 | if (Ptr->getType()->isVectorTy()) { |
1055 | unsigned NumElem = Ptr->getType()->getVectorNumElements(); |
1056 | return VectorType::get(PtrTy, NumElem); |
1057 | } |
1058 | for (Value *Index : IdxList) |
1059 | if (Index->getType()->isVectorTy()) { |
1060 | unsigned NumElem = Index->getType()->getVectorNumElements(); |
1061 | return VectorType::get(PtrTy, NumElem); |
1062 | } |
1063 | // Scalar GEP |
1064 | return PtrTy; |
1065 | } |
1066 | |
1067 | unsigned getNumIndices() const { // Note: always non-negative |
1068 | return getNumOperands() - 1; |
1069 | } |
1070 | |
1071 | bool hasIndices() const { |
1072 | return getNumOperands() > 1; |
1073 | } |
1074 | |
1075 | /// Return true if all of the indices of this GEP are |
1076 | /// zeros. If so, the result pointer and the first operand have the same |
1077 | /// value, just potentially different types. |
1078 | bool hasAllZeroIndices() const; |
1079 | |
1080 | /// Return true if all of the indices of this GEP are |
1081 | /// constant integers. If so, the result pointer and the first operand have |
1082 | /// a constant offset between them. |
1083 | bool hasAllConstantIndices() const; |
1084 | |
1085 | /// Set or clear the inbounds flag on this GEP instruction. |
1086 | /// See LangRef.html for the meaning of inbounds on a getelementptr. |
1087 | void setIsInBounds(bool b = true); |
1088 | |
1089 | /// Determine whether the GEP has the inbounds flag. |
1090 | bool isInBounds() const; |
1091 | |
1092 | /// Accumulate the constant address offset of this GEP if possible. |
1093 | /// |
1094 | /// This routine accepts an APInt into which it will accumulate the constant |
1095 | /// offset of this GEP if the GEP is in fact constant. If the GEP is not |
1096 | /// all-constant, it returns false and the value of the offset APInt is |
1097 | /// undefined (it is *not* preserved!). The APInt passed into this routine |
1098 | /// must be at least as wide as the IntPtr type for the address space of |
1099 | /// the base GEP pointer. |
1100 | bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const; |
1101 | |
1102 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
1103 | static bool classof(const Instruction *I) { |
1104 | return (I->getOpcode() == Instruction::GetElementPtr); |
1105 | } |
1106 | static bool classof(const Value *V) { |
1107 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1108 | } |
1109 | }; |
1110 | |
1111 | template <> |
1112 | struct OperandTraits<GetElementPtrInst> : |
1113 | public VariadicOperandTraits<GetElementPtrInst, 1> { |
1114 | }; |
1115 | |
1116 | GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr, |
1117 | ArrayRef<Value *> IdxList, unsigned Values, |
1118 | const Twine &NameStr, |
1119 | Instruction *InsertBefore) |
1120 | : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr, |
1121 | OperandTraits<GetElementPtrInst>::op_end(this) - Values, |
1122 | Values, InsertBefore), |
1123 | SourceElementType(PointeeType), |
1124 | ResultElementType(getIndexedType(PointeeType, IdxList)) { |
1125 | assert(ResultElementType ==((ResultElementType == cast<PointerType>(getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1126, __PRETTY_FUNCTION__)) |
1126 | cast<PointerType>(getType()->getScalarType())->getElementType())((ResultElementType == cast<PointerType>(getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1126, __PRETTY_FUNCTION__)); |
1127 | init(Ptr, IdxList, NameStr); |
1128 | } |
1129 | |
1130 | GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr, |
1131 | ArrayRef<Value *> IdxList, unsigned Values, |
1132 | const Twine &NameStr, |
1133 | BasicBlock *InsertAtEnd) |
1134 | : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr, |
1135 | OperandTraits<GetElementPtrInst>::op_end(this) - Values, |
1136 | Values, InsertAtEnd), |
1137 | SourceElementType(PointeeType), |
1138 | ResultElementType(getIndexedType(PointeeType, IdxList)) { |
1139 | assert(ResultElementType ==((ResultElementType == cast<PointerType>(getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1140, __PRETTY_FUNCTION__)) |
1140 | cast<PointerType>(getType()->getScalarType())->getElementType())((ResultElementType == cast<PointerType>(getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1140, __PRETTY_FUNCTION__)); |
1141 | init(Ptr, IdxList, NameStr); |
1142 | } |
1143 | |
1144 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)GetElementPtrInst::op_iterator GetElementPtrInst::op_begin() { return OperandTraits<GetElementPtrInst>::op_begin(this ); } GetElementPtrInst::const_op_iterator GetElementPtrInst:: op_begin() const { return OperandTraits<GetElementPtrInst> ::op_begin(const_cast<GetElementPtrInst*>(this)); } GetElementPtrInst ::op_iterator GetElementPtrInst::op_end() { return OperandTraits <GetElementPtrInst>::op_end(this); } GetElementPtrInst:: const_op_iterator GetElementPtrInst::op_end() const { return OperandTraits <GetElementPtrInst>::op_end(const_cast<GetElementPtrInst *>(this)); } Value *GetElementPtrInst::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<GetElementPtrInst >::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<GetElementPtrInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1144, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<GetElementPtrInst>::op_begin(const_cast <GetElementPtrInst*>(this))[i_nocapture].get()); } void GetElementPtrInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((i_nocapture < OperandTraits<GetElementPtrInst> ::operands(this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<GetElementPtrInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1144, __PRETTY_FUNCTION__)); OperandTraits<GetElementPtrInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned GetElementPtrInst::getNumOperands() const { return OperandTraits <GetElementPtrInst>::operands(this); } template <int Idx_nocapture> Use &GetElementPtrInst::Op() { return this ->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture > const Use &GetElementPtrInst::Op() const { return this ->OpFrom<Idx_nocapture>(this); } |
1145 | |
1146 | //===----------------------------------------------------------------------===// |
1147 | // ICmpInst Class |
1148 | //===----------------------------------------------------------------------===// |
1149 | |
1150 | /// This instruction compares its operands according to the predicate given |
1151 | /// to the constructor. It only operates on integers or pointers. The operands |
1152 | /// must be identical types. |
1153 | /// Represent an integer comparison operator. |
1154 | class ICmpInst: public CmpInst { |
1155 | void AssertOK() { |
1156 | assert(isIntPredicate() &&((isIntPredicate() && "Invalid ICmp predicate value") ? static_cast<void> (0) : __assert_fail ("isIntPredicate() && \"Invalid ICmp predicate value\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1157, __PRETTY_FUNCTION__)) |
1157 | "Invalid ICmp predicate value")((isIntPredicate() && "Invalid ICmp predicate value") ? static_cast<void> (0) : __assert_fail ("isIntPredicate() && \"Invalid ICmp predicate value\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1157, __PRETTY_FUNCTION__)); |
1158 | assert(getOperand(0)->getType() == getOperand(1)->getType() &&((getOperand(0)->getType() == getOperand(1)->getType() && "Both operands to ICmp instruction are not of the same type!" ) ? static_cast<void> (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to ICmp instruction are not of the same type!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1159, __PRETTY_FUNCTION__)) |
1159 | "Both operands to ICmp instruction are not of the same type!")((getOperand(0)->getType() == getOperand(1)->getType() && "Both operands to ICmp instruction are not of the same type!" ) ? static_cast<void> (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to ICmp instruction are not of the same type!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1159, __PRETTY_FUNCTION__)); |
1160 | // Check that the operands are the right type |
1161 | assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||(((getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand (0)->getType()->isPtrOrPtrVectorTy()) && "Invalid operand types for ICmp instruction" ) ? static_cast<void> (0) : __assert_fail ("(getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && \"Invalid operand types for ICmp instruction\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1163, __PRETTY_FUNCTION__)) |
1162 | getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&(((getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand (0)->getType()->isPtrOrPtrVectorTy()) && "Invalid operand types for ICmp instruction" ) ? static_cast<void> (0) : __assert_fail ("(getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && \"Invalid operand types for ICmp instruction\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1163, __PRETTY_FUNCTION__)) |
1163 | "Invalid operand types for ICmp instruction")(((getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand (0)->getType()->isPtrOrPtrVectorTy()) && "Invalid operand types for ICmp instruction" ) ? static_cast<void> (0) : __assert_fail ("(getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && \"Invalid operand types for ICmp instruction\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1163, __PRETTY_FUNCTION__)); |
1164 | } |
1165 | |
1166 | protected: |
1167 | // Note: Instruction needs to be a friend here to call cloneImpl. |
1168 | friend class Instruction; |
1169 | |
1170 | /// Clone an identical ICmpInst |
1171 | ICmpInst *cloneImpl() const; |
1172 | |
1173 | public: |
1174 | /// Constructor with insert-before-instruction semantics. |
1175 | ICmpInst( |
1176 | Instruction *InsertBefore, ///< Where to insert |
1177 | Predicate pred, ///< The predicate to use for the comparison |
1178 | Value *LHS, ///< The left-hand-side of the expression |
1179 | Value *RHS, ///< The right-hand-side of the expression |
1180 | const Twine &NameStr = "" ///< Name of the instruction |
1181 | ) : CmpInst(makeCmpResultType(LHS->getType()), |
1182 | Instruction::ICmp, pred, LHS, RHS, NameStr, |
1183 | InsertBefore) { |
1184 | #ifndef NDEBUG |
1185 | AssertOK(); |
1186 | #endif |
1187 | } |
1188 | |
1189 | /// Constructor with insert-at-end semantics. |
1190 | ICmpInst( |
1191 | BasicBlock &InsertAtEnd, ///< Block to insert into. |
1192 | Predicate pred, ///< The predicate to use for the comparison |
1193 | Value *LHS, ///< The left-hand-side of the expression |
1194 | Value *RHS, ///< The right-hand-side of the expression |
1195 | const Twine &NameStr = "" ///< Name of the instruction |
1196 | ) : CmpInst(makeCmpResultType(LHS->getType()), |
1197 | Instruction::ICmp, pred, LHS, RHS, NameStr, |
1198 | &InsertAtEnd) { |
1199 | #ifndef NDEBUG |
1200 | AssertOK(); |
1201 | #endif |
1202 | } |
1203 | |
1204 | /// Constructor with no-insertion semantics |
1205 | ICmpInst( |
1206 | Predicate pred, ///< The predicate to use for the comparison |
1207 | Value *LHS, ///< The left-hand-side of the expression |
1208 | Value *RHS, ///< The right-hand-side of the expression |
1209 | const Twine &NameStr = "" ///< Name of the instruction |
1210 | ) : CmpInst(makeCmpResultType(LHS->getType()), |
1211 | Instruction::ICmp, pred, LHS, RHS, NameStr) { |
1212 | #ifndef NDEBUG |
1213 | AssertOK(); |
1214 | #endif |
1215 | } |
1216 | |
1217 | /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc. |
1218 | /// @returns the predicate that would be the result if the operand were |
1219 | /// regarded as signed. |
1220 | /// Return the signed version of the predicate |
1221 | Predicate getSignedPredicate() const { |
1222 | return getSignedPredicate(getPredicate()); |
1223 | } |
1224 | |
1225 | /// This is a static version that you can use without an instruction. |
1226 | /// Return the signed version of the predicate. |
1227 | static Predicate getSignedPredicate(Predicate pred); |
1228 | |
1229 | /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc. |
1230 | /// @returns the predicate that would be the result if the operand were |
1231 | /// regarded as unsigned. |
1232 | /// Return the unsigned version of the predicate |
1233 | Predicate getUnsignedPredicate() const { |
1234 | return getUnsignedPredicate(getPredicate()); |
1235 | } |
1236 | |
1237 | /// This is a static version that you can use without an instruction. |
1238 | /// Return the unsigned version of the predicate. |
1239 | static Predicate getUnsignedPredicate(Predicate pred); |
1240 | |
1241 | /// Return true if this predicate is either EQ or NE. This also |
1242 | /// tests for commutativity. |
1243 | static bool isEquality(Predicate P) { |
1244 | return P == ICMP_EQ || P == ICMP_NE; |
1245 | } |
1246 | |
1247 | /// Return true if this predicate is either EQ or NE. This also |
1248 | /// tests for commutativity. |
1249 | bool isEquality() const { |
1250 | return isEquality(getPredicate()); |
1251 | } |
1252 | |
1253 | /// @returns true if the predicate of this ICmpInst is commutative |
1254 | /// Determine if this relation is commutative. |
1255 | bool isCommutative() const { return isEquality(); } |
1256 | |
1257 | /// Return true if the predicate is relational (not EQ or NE). |
1258 | /// |
1259 | bool isRelational() const { |
1260 | return !isEquality(); |
1261 | } |
1262 | |
1263 | /// Return true if the predicate is relational (not EQ or NE). |
1264 | /// |
1265 | static bool isRelational(Predicate P) { |
1266 | return !isEquality(P); |
1267 | } |
1268 | |
1269 | /// Exchange the two operands to this instruction in such a way that it does |
1270 | /// not modify the semantics of the instruction. The predicate value may be |
1271 | /// changed to retain the same result if the predicate is order dependent |
1272 | /// (e.g. ult). |
1273 | /// Swap operands and adjust predicate. |
1274 | void swapOperands() { |
1275 | setPredicate(getSwappedPredicate()); |
1276 | Op<0>().swap(Op<1>()); |
1277 | } |
1278 | |
1279 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
1280 | static bool classof(const Instruction *I) { |
1281 | return I->getOpcode() == Instruction::ICmp; |
1282 | } |
1283 | static bool classof(const Value *V) { |
1284 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1285 | } |
1286 | }; |
1287 | |
1288 | //===----------------------------------------------------------------------===// |
1289 | // FCmpInst Class |
1290 | //===----------------------------------------------------------------------===// |
1291 | |
1292 | /// This instruction compares its operands according to the predicate given |
1293 | /// to the constructor. It only operates on floating point values or packed |
1294 | /// vectors of floating point values. The operands must be identical types. |
1295 | /// Represents a floating point comparison operator. |
1296 | class FCmpInst: public CmpInst { |
1297 | void AssertOK() { |
1298 | assert(isFPPredicate() && "Invalid FCmp predicate value")((isFPPredicate() && "Invalid FCmp predicate value") ? static_cast<void> (0) : __assert_fail ("isFPPredicate() && \"Invalid FCmp predicate value\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1298, __PRETTY_FUNCTION__)); |
1299 | assert(getOperand(0)->getType() == getOperand(1)->getType() &&((getOperand(0)->getType() == getOperand(1)->getType() && "Both operands to FCmp instruction are not of the same type!" ) ? static_cast<void> (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to FCmp instruction are not of the same type!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1300, __PRETTY_FUNCTION__)) |
1300 | "Both operands to FCmp instruction are not of the same type!")((getOperand(0)->getType() == getOperand(1)->getType() && "Both operands to FCmp instruction are not of the same type!" ) ? static_cast<void> (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to FCmp instruction are not of the same type!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1300, __PRETTY_FUNCTION__)); |
1301 | // Check that the operands are the right type |
1302 | assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&((getOperand(0)->getType()->isFPOrFPVectorTy() && "Invalid operand types for FCmp instruction") ? static_cast< void> (0) : __assert_fail ("getOperand(0)->getType()->isFPOrFPVectorTy() && \"Invalid operand types for FCmp instruction\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1303, __PRETTY_FUNCTION__)) |
1303 | "Invalid operand types for FCmp instruction")((getOperand(0)->getType()->isFPOrFPVectorTy() && "Invalid operand types for FCmp instruction") ? static_cast< void> (0) : __assert_fail ("getOperand(0)->getType()->isFPOrFPVectorTy() && \"Invalid operand types for FCmp instruction\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1303, __PRETTY_FUNCTION__)); |
1304 | } |
1305 | |
1306 | protected: |
1307 | // Note: Instruction needs to be a friend here to call cloneImpl. |
1308 | friend class Instruction; |
1309 | |
1310 | /// Clone an identical FCmpInst |
1311 | FCmpInst *cloneImpl() const; |
1312 | |
1313 | public: |
1314 | /// Constructor with insert-before-instruction semantics. |
1315 | FCmpInst( |
1316 | Instruction *InsertBefore, ///< Where to insert |
1317 | Predicate pred, ///< The predicate to use for the comparison |
1318 | Value *LHS, ///< The left-hand-side of the expression |
1319 | Value *RHS, ///< The right-hand-side of the expression |
1320 | const Twine &NameStr = "" ///< Name of the instruction |
1321 | ) : CmpInst(makeCmpResultType(LHS->getType()), |
1322 | Instruction::FCmp, pred, LHS, RHS, NameStr, |
1323 | InsertBefore) { |
1324 | AssertOK(); |
1325 | } |
1326 | |
1327 | /// Constructor with insert-at-end semantics. |
1328 | FCmpInst( |
1329 | BasicBlock &InsertAtEnd, ///< Block to insert into. |
1330 | Predicate pred, ///< The predicate to use for the comparison |
1331 | Value *LHS, ///< The left-hand-side of the expression |
1332 | Value *RHS, ///< The right-hand-side of the expression |
1333 | const Twine &NameStr = "" ///< Name of the instruction |
1334 | ) : CmpInst(makeCmpResultType(LHS->getType()), |
1335 | Instruction::FCmp, pred, LHS, RHS, NameStr, |
1336 | &InsertAtEnd) { |
1337 | AssertOK(); |
1338 | } |
1339 | |
1340 | /// Constructor with no-insertion semantics |
1341 | FCmpInst( |
1342 | Predicate Pred, ///< The predicate to use for the comparison |
1343 | Value *LHS, ///< The left-hand-side of the expression |
1344 | Value *RHS, ///< The right-hand-side of the expression |
1345 | const Twine &NameStr = "", ///< Name of the instruction |
1346 | Instruction *FlagsSource = nullptr |
1347 | ) : CmpInst(makeCmpResultType(LHS->getType()), Instruction::FCmp, Pred, LHS, |
1348 | RHS, NameStr, nullptr, FlagsSource) { |
1349 | AssertOK(); |
1350 | } |
1351 | |
1352 | /// @returns true if the predicate of this instruction is EQ or NE. |
1353 | /// Determine if this is an equality predicate. |
1354 | static bool isEquality(Predicate Pred) { |
1355 | return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ || |
1356 | Pred == FCMP_UNE; |
1357 | } |
1358 | |
1359 | /// @returns true if the predicate of this instruction is EQ or NE. |
1360 | /// Determine if this is an equality predicate. |
1361 | bool isEquality() const { return isEquality(getPredicate()); } |
1362 | |
1363 | /// @returns true if the predicate of this instruction is commutative. |
1364 | /// Determine if this is a commutative predicate. |
1365 | bool isCommutative() const { |
1366 | return isEquality() || |
1367 | getPredicate() == FCMP_FALSE || |
1368 | getPredicate() == FCMP_TRUE || |
1369 | getPredicate() == FCMP_ORD || |
1370 | getPredicate() == FCMP_UNO; |
1371 | } |
1372 | |
1373 | /// @returns true if the predicate is relational (not EQ or NE). |
1374 | /// Determine if this a relational predicate. |
1375 | bool isRelational() const { return !isEquality(); } |
1376 | |
1377 | /// Exchange the two operands to this instruction in such a way that it does |
1378 | /// not modify the semantics of the instruction. The predicate value may be |
1379 | /// changed to retain the same result if the predicate is order dependent |
1380 | /// (e.g. ult). |
1381 | /// Swap operands and adjust predicate. |
1382 | void swapOperands() { |
1383 | setPredicate(getSwappedPredicate()); |
1384 | Op<0>().swap(Op<1>()); |
1385 | } |
1386 | |
1387 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
1388 | static bool classof(const Instruction *I) { |
1389 | return I->getOpcode() == Instruction::FCmp; |
1390 | } |
1391 | static bool classof(const Value *V) { |
1392 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1393 | } |
1394 | }; |
1395 | |
1396 | //===----------------------------------------------------------------------===// |
1397 | /// This class represents a function call, abstracting a target |
1398 | /// machine's calling convention. This class uses low bit of the SubClassData |
1399 | /// field to indicate whether or not this is a tail call. The rest of the bits |
1400 | /// hold the calling convention of the call. |
1401 | /// |
1402 | class CallInst : public CallBase { |
1403 | CallInst(const CallInst &CI); |
1404 | |
1405 | /// Construct a CallInst given a range of arguments. |
1406 | /// Construct a CallInst from a range of arguments |
1407 | inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
1408 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, |
1409 | Instruction *InsertBefore); |
1410 | |
1411 | inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
1412 | const Twine &NameStr, Instruction *InsertBefore) |
1413 | : CallInst(Ty, Func, Args, None, NameStr, InsertBefore) {} |
1414 | |
1415 | /// Construct a CallInst given a range of arguments. |
1416 | /// Construct a CallInst from a range of arguments |
1417 | inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
1418 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, |
1419 | BasicBlock *InsertAtEnd); |
1420 | |
1421 | explicit CallInst(FunctionType *Ty, Value *F, const Twine &NameStr, |
1422 | Instruction *InsertBefore); |
1423 | |
1424 | CallInst(FunctionType *ty, Value *F, const Twine &NameStr, |
1425 | BasicBlock *InsertAtEnd); |
1426 | |
1427 | void init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args, |
1428 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr); |
1429 | void init(FunctionType *FTy, Value *Func, const Twine &NameStr); |
1430 | |
1431 | /// Compute the number of operands to allocate. |
1432 | static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) { |
1433 | // We need one operand for the called function, plus the input operand |
1434 | // counts provided. |
1435 | return 1 + NumArgs + NumBundleInputs; |
1436 | } |
1437 | |
1438 | protected: |
1439 | // Note: Instruction needs to be a friend here to call cloneImpl. |
1440 | friend class Instruction; |
1441 | |
1442 | CallInst *cloneImpl() const; |
1443 | |
1444 | public: |
1445 | static CallInst *Create(FunctionType *Ty, Value *F, const Twine &NameStr = "", |
1446 | Instruction *InsertBefore = nullptr) { |
1447 | return new (ComputeNumOperands(0)) CallInst(Ty, F, NameStr, InsertBefore); |
1448 | } |
1449 | |
1450 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
1451 | const Twine &NameStr, |
1452 | Instruction *InsertBefore = nullptr) { |
1453 | return new (ComputeNumOperands(Args.size())) |
1454 | CallInst(Ty, Func, Args, None, NameStr, InsertBefore); |
1455 | } |
1456 | |
1457 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
1458 | ArrayRef<OperandBundleDef> Bundles = None, |
1459 | const Twine &NameStr = "", |
1460 | Instruction *InsertBefore = nullptr) { |
1461 | const int NumOperands = |
1462 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); |
1463 | const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); |
1464 | |
1465 | return new (NumOperands, DescriptorBytes) |
1466 | CallInst(Ty, Func, Args, Bundles, NameStr, InsertBefore); |
1467 | } |
1468 | |
1469 | static CallInst *Create(FunctionType *Ty, Value *F, const Twine &NameStr, |
1470 | BasicBlock *InsertAtEnd) { |
1471 | return new (ComputeNumOperands(0)) CallInst(Ty, F, NameStr, InsertAtEnd); |
1472 | } |
1473 | |
1474 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
1475 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
1476 | return new (ComputeNumOperands(Args.size())) |
1477 | CallInst(Ty, Func, Args, None, NameStr, InsertAtEnd); |
1478 | } |
1479 | |
1480 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
1481 | ArrayRef<OperandBundleDef> Bundles, |
1482 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
1483 | const int NumOperands = |
1484 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); |
1485 | const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); |
1486 | |
1487 | return new (NumOperands, DescriptorBytes) |
1488 | CallInst(Ty, Func, Args, Bundles, NameStr, InsertAtEnd); |
1489 | } |
1490 | |
1491 | static CallInst *Create(FunctionCallee Func, const Twine &NameStr = "", |
1492 | Instruction *InsertBefore = nullptr) { |
1493 | return Create(Func.getFunctionType(), Func.getCallee(), NameStr, |
1494 | InsertBefore); |
1495 | } |
1496 | |
1497 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, |
1498 | ArrayRef<OperandBundleDef> Bundles = None, |
1499 | const Twine &NameStr = "", |
1500 | Instruction *InsertBefore = nullptr) { |
1501 | return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles, |
1502 | NameStr, InsertBefore); |
1503 | } |
1504 | |
1505 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, |
1506 | const Twine &NameStr, |
1507 | Instruction *InsertBefore = nullptr) { |
1508 | return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr, |
1509 | InsertBefore); |
1510 | } |
1511 | |
1512 | static CallInst *Create(FunctionCallee Func, const Twine &NameStr, |
1513 | BasicBlock *InsertAtEnd) { |
1514 | return Create(Func.getFunctionType(), Func.getCallee(), NameStr, |
1515 | InsertAtEnd); |
1516 | } |
1517 | |
1518 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, |
1519 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
1520 | return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr, |
1521 | InsertAtEnd); |
1522 | } |
1523 | |
1524 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, |
1525 | ArrayRef<OperandBundleDef> Bundles, |
1526 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
1527 | return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles, |
1528 | NameStr, InsertAtEnd); |
1529 | } |
1530 | |
1531 | // Deprecated [opaque pointer types] |
1532 | static CallInst *Create(Value *Func, const Twine &NameStr = "", |
1533 | Instruction *InsertBefore = nullptr) { |
1534 | return Create(cast<FunctionType>( |
1535 | cast<PointerType>(Func->getType())->getElementType()), |
1536 | Func, NameStr, InsertBefore); |
1537 | } |
1538 | |
1539 | // Deprecated [opaque pointer types] |
1540 | static CallInst *Create(Value *Func, ArrayRef<Value *> Args, |
1541 | const Twine &NameStr, |
1542 | Instruction *InsertBefore = nullptr) { |
1543 | return Create(cast<FunctionType>( |
1544 | cast<PointerType>(Func->getType())->getElementType()), |
1545 | Func, Args, NameStr, InsertBefore); |
1546 | } |
1547 | |
1548 | // Deprecated [opaque pointer types] |
1549 | static CallInst *Create(Value *Func, ArrayRef<Value *> Args, |
1550 | ArrayRef<OperandBundleDef> Bundles = None, |
1551 | const Twine &NameStr = "", |
1552 | Instruction *InsertBefore = nullptr) { |
1553 | return Create(cast<FunctionType>( |
1554 | cast<PointerType>(Func->getType())->getElementType()), |
1555 | Func, Args, Bundles, NameStr, InsertBefore); |
1556 | } |
1557 | |
1558 | // Deprecated [opaque pointer types] |
1559 | static CallInst *Create(Value *Func, const Twine &NameStr, |
1560 | BasicBlock *InsertAtEnd) { |
1561 | return Create(cast<FunctionType>( |
1562 | cast<PointerType>(Func->getType())->getElementType()), |
1563 | Func, NameStr, InsertAtEnd); |
1564 | } |
1565 | |
1566 | // Deprecated [opaque pointer types] |
1567 | static CallInst *Create(Value *Func, ArrayRef<Value *> Args, |
1568 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
1569 | return Create(cast<FunctionType>( |
1570 | cast<PointerType>(Func->getType())->getElementType()), |
1571 | Func, Args, NameStr, InsertAtEnd); |
1572 | } |
1573 | |
1574 | // Deprecated [opaque pointer types] |
1575 | static CallInst *Create(Value *Func, ArrayRef<Value *> Args, |
1576 | ArrayRef<OperandBundleDef> Bundles, |
1577 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
1578 | return Create(cast<FunctionType>( |
1579 | cast<PointerType>(Func->getType())->getElementType()), |
1580 | Func, Args, Bundles, NameStr, InsertAtEnd); |
1581 | } |
1582 | |
1583 | /// Create a clone of \p CI with a different set of operand bundles and |
1584 | /// insert it before \p InsertPt. |
1585 | /// |
1586 | /// The returned call instruction is identical \p CI in every way except that |
1587 | /// the operand bundles for the new instruction are set to the operand bundles |
1588 | /// in \p Bundles. |
1589 | static CallInst *Create(CallInst *CI, ArrayRef<OperandBundleDef> Bundles, |
1590 | Instruction *InsertPt = nullptr); |
1591 | |
1592 | /// Generate the IR for a call to malloc: |
1593 | /// 1. Compute the malloc call's argument as the specified type's size, |
1594 | /// possibly multiplied by the array size if the array size is not |
1595 | /// constant 1. |
1596 | /// 2. Call malloc with that argument. |
1597 | /// 3. Bitcast the result of the malloc call to the specified type. |
1598 | static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy, |
1599 | Type *AllocTy, Value *AllocSize, |
1600 | Value *ArraySize = nullptr, |
1601 | Function *MallocF = nullptr, |
1602 | const Twine &Name = ""); |
1603 | static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy, |
1604 | Type *AllocTy, Value *AllocSize, |
1605 | Value *ArraySize = nullptr, |
1606 | Function *MallocF = nullptr, |
1607 | const Twine &Name = ""); |
1608 | static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy, |
1609 | Type *AllocTy, Value *AllocSize, |
1610 | Value *ArraySize = nullptr, |
1611 | ArrayRef<OperandBundleDef> Bundles = None, |
1612 | Function *MallocF = nullptr, |
1613 | const Twine &Name = ""); |
1614 | static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy, |
1615 | Type *AllocTy, Value *AllocSize, |
1616 | Value *ArraySize = nullptr, |
1617 | ArrayRef<OperandBundleDef> Bundles = None, |
1618 | Function *MallocF = nullptr, |
1619 | const Twine &Name = ""); |
1620 | /// Generate the IR for a call to the builtin free function. |
1621 | static Instruction *CreateFree(Value *Source, Instruction *InsertBefore); |
1622 | static Instruction *CreateFree(Value *Source, BasicBlock *InsertAtEnd); |
1623 | static Instruction *CreateFree(Value *Source, |
1624 | ArrayRef<OperandBundleDef> Bundles, |
1625 | Instruction *InsertBefore); |
1626 | static Instruction *CreateFree(Value *Source, |
1627 | ArrayRef<OperandBundleDef> Bundles, |
1628 | BasicBlock *InsertAtEnd); |
1629 | |
1630 | // Note that 'musttail' implies 'tail'. |
1631 | enum TailCallKind { |
1632 | TCK_None = 0, |
1633 | TCK_Tail = 1, |
1634 | TCK_MustTail = 2, |
1635 | TCK_NoTail = 3 |
1636 | }; |
1637 | TailCallKind getTailCallKind() const { |
1638 | return TailCallKind(getSubclassDataFromInstruction() & 3); |
1639 | } |
1640 | |
1641 | bool isTailCall() const { |
1642 | unsigned Kind = getSubclassDataFromInstruction() & 3; |
1643 | return Kind == TCK_Tail || Kind == TCK_MustTail; |
1644 | } |
1645 | |
1646 | bool isMustTailCall() const { |
1647 | return (getSubclassDataFromInstruction() & 3) == TCK_MustTail; |
1648 | } |
1649 | |
1650 | bool isNoTailCall() const { |
1651 | return (getSubclassDataFromInstruction() & 3) == TCK_NoTail; |
1652 | } |
1653 | |
1654 | void setTailCall(bool isTC = true) { |
1655 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) | |
1656 | unsigned(isTC ? TCK_Tail : TCK_None)); |
1657 | } |
1658 | |
1659 | void setTailCallKind(TailCallKind TCK) { |
1660 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) | |
1661 | unsigned(TCK)); |
1662 | } |
1663 | |
1664 | /// Return true if the call can return twice |
1665 | bool canReturnTwice() const { return hasFnAttr(Attribute::ReturnsTwice); } |
1666 | void setCanReturnTwice() { |
1667 | addAttribute(AttributeList::FunctionIndex, Attribute::ReturnsTwice); |
1668 | } |
1669 | |
1670 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
1671 | static bool classof(const Instruction *I) { |
1672 | return I->getOpcode() == Instruction::Call; |
1673 | } |
1674 | static bool classof(const Value *V) { |
1675 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1676 | } |
1677 | |
1678 | /// Updates profile metadata by scaling it by \p S / \p T. |
1679 | void updateProfWeight(uint64_t S, uint64_t T); |
1680 | |
1681 | private: |
1682 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
1683 | // method so that subclasses cannot accidentally use it. |
1684 | void setInstructionSubclassData(unsigned short D) { |
1685 | Instruction::setInstructionSubclassData(D); |
1686 | } |
1687 | }; |
1688 | |
1689 | CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
1690 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, |
1691 | BasicBlock *InsertAtEnd) |
1692 | : CallBase(Ty->getReturnType(), Instruction::Call, |
1693 | OperandTraits<CallBase>::op_end(this) - |
1694 | (Args.size() + CountBundleInputs(Bundles) + 1), |
1695 | unsigned(Args.size() + CountBundleInputs(Bundles) + 1), |
1696 | InsertAtEnd) { |
1697 | init(Ty, Func, Args, Bundles, NameStr); |
1698 | } |
1699 | |
1700 | CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
1701 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, |
1702 | Instruction *InsertBefore) |
1703 | : CallBase(Ty->getReturnType(), Instruction::Call, |
1704 | OperandTraits<CallBase>::op_end(this) - |
1705 | (Args.size() + CountBundleInputs(Bundles) + 1), |
1706 | unsigned(Args.size() + CountBundleInputs(Bundles) + 1), |
1707 | InsertBefore) { |
1708 | init(Ty, Func, Args, Bundles, NameStr); |
1709 | } |
1710 | |
1711 | //===----------------------------------------------------------------------===// |
1712 | // SelectInst Class |
1713 | //===----------------------------------------------------------------------===// |
1714 | |
1715 | /// This class represents the LLVM 'select' instruction. |
1716 | /// |
1717 | class SelectInst : public Instruction { |
1718 | SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr, |
1719 | Instruction *InsertBefore) |
1720 | : Instruction(S1->getType(), Instruction::Select, |
1721 | &Op<0>(), 3, InsertBefore) { |
1722 | init(C, S1, S2); |
1723 | setName(NameStr); |
1724 | } |
1725 | |
1726 | SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr, |
1727 | BasicBlock *InsertAtEnd) |
1728 | : Instruction(S1->getType(), Instruction::Select, |
1729 | &Op<0>(), 3, InsertAtEnd) { |
1730 | init(C, S1, S2); |
1731 | setName(NameStr); |
1732 | } |
1733 | |
1734 | void init(Value *C, Value *S1, Value *S2) { |
1735 | assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select")((!areInvalidOperands(C, S1, S2) && "Invalid operands for select" ) ? static_cast<void> (0) : __assert_fail ("!areInvalidOperands(C, S1, S2) && \"Invalid operands for select\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1735, __PRETTY_FUNCTION__)); |
1736 | Op<0>() = C; |
1737 | Op<1>() = S1; |
1738 | Op<2>() = S2; |
1739 | } |
1740 | |
1741 | protected: |
1742 | // Note: Instruction needs to be a friend here to call cloneImpl. |
1743 | friend class Instruction; |
1744 | |
1745 | SelectInst *cloneImpl() const; |
1746 | |
1747 | public: |
1748 | static SelectInst *Create(Value *C, Value *S1, Value *S2, |
1749 | const Twine &NameStr = "", |
1750 | Instruction *InsertBefore = nullptr, |
1751 | Instruction *MDFrom = nullptr) { |
1752 | SelectInst *Sel = new(3) SelectInst(C, S1, S2, NameStr, InsertBefore); |
1753 | if (MDFrom) |
1754 | Sel->copyMetadata(*MDFrom); |
1755 | return Sel; |
1756 | } |
1757 | |
1758 | static SelectInst *Create(Value *C, Value *S1, Value *S2, |
1759 | const Twine &NameStr, |
1760 | BasicBlock *InsertAtEnd) { |
1761 | return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd); |
1762 | } |
1763 | |
1764 | const Value *getCondition() const { return Op<0>(); } |
1765 | const Value *getTrueValue() const { return Op<1>(); } |
1766 | const Value *getFalseValue() const { return Op<2>(); } |
1767 | Value *getCondition() { return Op<0>(); } |
1768 | Value *getTrueValue() { return Op<1>(); } |
1769 | Value *getFalseValue() { return Op<2>(); } |
1770 | |
1771 | void setCondition(Value *V) { Op<0>() = V; } |
1772 | void setTrueValue(Value *V) { Op<1>() = V; } |
1773 | void setFalseValue(Value *V) { Op<2>() = V; } |
1774 | |
1775 | /// Swap the true and false values of the select instruction. |
1776 | /// This doesn't swap prof metadata. |
1777 | void swapValues() { Op<1>().swap(Op<2>()); } |
1778 | |
1779 | /// Return a string if the specified operands are invalid |
1780 | /// for a select operation, otherwise return null. |
1781 | static const char *areInvalidOperands(Value *Cond, Value *True, Value *False); |
1782 | |
1783 | /// Transparently provide more efficient getOperand methods. |
1784 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
1785 | |
1786 | OtherOps getOpcode() const { |
1787 | return static_cast<OtherOps>(Instruction::getOpcode()); |
1788 | } |
1789 | |
1790 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
1791 | static bool classof(const Instruction *I) { |
1792 | return I->getOpcode() == Instruction::Select; |
1793 | } |
1794 | static bool classof(const Value *V) { |
1795 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1796 | } |
1797 | }; |
1798 | |
1799 | template <> |
1800 | struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> { |
1801 | }; |
1802 | |
1803 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)SelectInst::op_iterator SelectInst::op_begin() { return OperandTraits <SelectInst>::op_begin(this); } SelectInst::const_op_iterator SelectInst::op_begin() const { return OperandTraits<SelectInst >::op_begin(const_cast<SelectInst*>(this)); } SelectInst ::op_iterator SelectInst::op_end() { return OperandTraits< SelectInst>::op_end(this); } SelectInst::const_op_iterator SelectInst::op_end() const { return OperandTraits<SelectInst >::op_end(const_cast<SelectInst*>(this)); } Value *SelectInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<SelectInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<SelectInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1803, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<SelectInst>::op_begin(const_cast<SelectInst *>(this))[i_nocapture].get()); } void SelectInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<SelectInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<SelectInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1803, __PRETTY_FUNCTION__)); OperandTraits<SelectInst> ::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned SelectInst ::getNumOperands() const { return OperandTraits<SelectInst >::operands(this); } template <int Idx_nocapture> Use &SelectInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & SelectInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } |
1804 | |
1805 | //===----------------------------------------------------------------------===// |
1806 | // VAArgInst Class |
1807 | //===----------------------------------------------------------------------===// |
1808 | |
1809 | /// This class represents the va_arg llvm instruction, which returns |
1810 | /// an argument of the specified type given a va_list and increments that list |
1811 | /// |
1812 | class VAArgInst : public UnaryInstruction { |
1813 | protected: |
1814 | // Note: Instruction needs to be a friend here to call cloneImpl. |
1815 | friend class Instruction; |
1816 | |
1817 | VAArgInst *cloneImpl() const; |
1818 | |
1819 | public: |
1820 | VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "", |
1821 | Instruction *InsertBefore = nullptr) |
1822 | : UnaryInstruction(Ty, VAArg, List, InsertBefore) { |
1823 | setName(NameStr); |
1824 | } |
1825 | |
1826 | VAArgInst(Value *List, Type *Ty, const Twine &NameStr, |
1827 | BasicBlock *InsertAtEnd) |
1828 | : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) { |
1829 | setName(NameStr); |
1830 | } |
1831 | |
1832 | Value *getPointerOperand() { return getOperand(0); } |
1833 | const Value *getPointerOperand() const { return getOperand(0); } |
1834 | static unsigned getPointerOperandIndex() { return 0U; } |
1835 | |
1836 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
1837 | static bool classof(const Instruction *I) { |
1838 | return I->getOpcode() == VAArg; |
1839 | } |
1840 | static bool classof(const Value *V) { |
1841 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1842 | } |
1843 | }; |
1844 | |
1845 | //===----------------------------------------------------------------------===// |
1846 | // ExtractElementInst Class |
1847 | //===----------------------------------------------------------------------===// |
1848 | |
1849 | /// This instruction extracts a single (scalar) |
1850 | /// element from a VectorType value |
1851 | /// |
1852 | class ExtractElementInst : public Instruction { |
1853 | ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "", |
1854 | Instruction *InsertBefore = nullptr); |
1855 | ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr, |
1856 | BasicBlock *InsertAtEnd); |
1857 | |
1858 | protected: |
1859 | // Note: Instruction needs to be a friend here to call cloneImpl. |
1860 | friend class Instruction; |
1861 | |
1862 | ExtractElementInst *cloneImpl() const; |
1863 | |
1864 | public: |
1865 | static ExtractElementInst *Create(Value *Vec, Value *Idx, |
1866 | const Twine &NameStr = "", |
1867 | Instruction *InsertBefore = nullptr) { |
1868 | return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore); |
1869 | } |
1870 | |
1871 | static ExtractElementInst *Create(Value *Vec, Value *Idx, |
1872 | const Twine &NameStr, |
1873 | BasicBlock *InsertAtEnd) { |
1874 | return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd); |
1875 | } |
1876 | |
1877 | /// Return true if an extractelement instruction can be |
1878 | /// formed with the specified operands. |
1879 | static bool isValidOperands(const Value *Vec, const Value *Idx); |
1880 | |
1881 | Value *getVectorOperand() { return Op<0>(); } |
1882 | Value *getIndexOperand() { return Op<1>(); } |
1883 | const Value *getVectorOperand() const { return Op<0>(); } |
1884 | const Value *getIndexOperand() const { return Op<1>(); } |
1885 | |
1886 | VectorType *getVectorOperandType() const { |
1887 | return cast<VectorType>(getVectorOperand()->getType()); |
1888 | } |
1889 | |
1890 | /// Transparently provide more efficient getOperand methods. |
1891 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
1892 | |
1893 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
1894 | static bool classof(const Instruction *I) { |
1895 | return I->getOpcode() == Instruction::ExtractElement; |
1896 | } |
1897 | static bool classof(const Value *V) { |
1898 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1899 | } |
1900 | }; |
1901 | |
1902 | template <> |
1903 | struct OperandTraits<ExtractElementInst> : |
1904 | public FixedNumOperandTraits<ExtractElementInst, 2> { |
1905 | }; |
1906 | |
1907 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)ExtractElementInst::op_iterator ExtractElementInst::op_begin( ) { return OperandTraits<ExtractElementInst>::op_begin( this); } ExtractElementInst::const_op_iterator ExtractElementInst ::op_begin() const { return OperandTraits<ExtractElementInst >::op_begin(const_cast<ExtractElementInst*>(this)); } ExtractElementInst::op_iterator ExtractElementInst::op_end() { return OperandTraits<ExtractElementInst>::op_end(this ); } ExtractElementInst::const_op_iterator ExtractElementInst ::op_end() const { return OperandTraits<ExtractElementInst >::op_end(const_cast<ExtractElementInst*>(this)); } Value *ExtractElementInst::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<ExtractElementInst>:: operands(this) && "getOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<ExtractElementInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1907, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<ExtractElementInst>::op_begin(const_cast <ExtractElementInst*>(this))[i_nocapture].get()); } void ExtractElementInst::setOperand(unsigned i_nocapture, Value * Val_nocapture) { ((i_nocapture < OperandTraits<ExtractElementInst >::operands(this) && "setOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<ExtractElementInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1907, __PRETTY_FUNCTION__)); OperandTraits<ExtractElementInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned ExtractElementInst::getNumOperands() const { return OperandTraits <ExtractElementInst>::operands(this); } template <int Idx_nocapture> Use &ExtractElementInst::Op() { return this->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture> const Use &ExtractElementInst::Op() const { return this->OpFrom<Idx_nocapture>(this); } |
1908 | |
1909 | //===----------------------------------------------------------------------===// |
1910 | // InsertElementInst Class |
1911 | //===----------------------------------------------------------------------===// |
1912 | |
1913 | /// This instruction inserts a single (scalar) |
1914 | /// element into a VectorType value |
1915 | /// |
1916 | class InsertElementInst : public Instruction { |
1917 | InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, |
1918 | const Twine &NameStr = "", |
1919 | Instruction *InsertBefore = nullptr); |
1920 | InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr, |
1921 | BasicBlock *InsertAtEnd); |
1922 | |
1923 | protected: |
1924 | // Note: Instruction needs to be a friend here to call cloneImpl. |
1925 | friend class Instruction; |
1926 | |
1927 | InsertElementInst *cloneImpl() const; |
1928 | |
1929 | public: |
1930 | static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx, |
1931 | const Twine &NameStr = "", |
1932 | Instruction *InsertBefore = nullptr) { |
1933 | return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore); |
1934 | } |
1935 | |
1936 | static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx, |
1937 | const Twine &NameStr, |
1938 | BasicBlock *InsertAtEnd) { |
1939 | return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd); |
1940 | } |
1941 | |
1942 | /// Return true if an insertelement instruction can be |
1943 | /// formed with the specified operands. |
1944 | static bool isValidOperands(const Value *Vec, const Value *NewElt, |
1945 | const Value *Idx); |
1946 | |
1947 | /// Overload to return most specific vector type. |
1948 | /// |
1949 | VectorType *getType() const { |
1950 | return cast<VectorType>(Instruction::getType()); |
1951 | } |
1952 | |
1953 | /// Transparently provide more efficient getOperand methods. |
1954 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
1955 | |
1956 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
1957 | static bool classof(const Instruction *I) { |
1958 | return I->getOpcode() == Instruction::InsertElement; |
1959 | } |
1960 | static bool classof(const Value *V) { |
1961 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1962 | } |
1963 | }; |
1964 | |
1965 | template <> |
1966 | struct OperandTraits<InsertElementInst> : |
1967 | public FixedNumOperandTraits<InsertElementInst, 3> { |
1968 | }; |
1969 | |
1970 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)InsertElementInst::op_iterator InsertElementInst::op_begin() { return OperandTraits<InsertElementInst>::op_begin(this ); } InsertElementInst::const_op_iterator InsertElementInst:: op_begin() const { return OperandTraits<InsertElementInst> ::op_begin(const_cast<InsertElementInst*>(this)); } InsertElementInst ::op_iterator InsertElementInst::op_end() { return OperandTraits <InsertElementInst>::op_end(this); } InsertElementInst:: const_op_iterator InsertElementInst::op_end() const { return OperandTraits <InsertElementInst>::op_end(const_cast<InsertElementInst *>(this)); } Value *InsertElementInst::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<InsertElementInst >::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<InsertElementInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1970, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<InsertElementInst>::op_begin(const_cast <InsertElementInst*>(this))[i_nocapture].get()); } void InsertElementInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((i_nocapture < OperandTraits<InsertElementInst> ::operands(this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<InsertElementInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 1970, __PRETTY_FUNCTION__)); OperandTraits<InsertElementInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned InsertElementInst::getNumOperands() const { return OperandTraits <InsertElementInst>::operands(this); } template <int Idx_nocapture> Use &InsertElementInst::Op() { return this ->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture > const Use &InsertElementInst::Op() const { return this ->OpFrom<Idx_nocapture>(this); } |
1971 | |
1972 | //===----------------------------------------------------------------------===// |
1973 | // ShuffleVectorInst Class |
1974 | //===----------------------------------------------------------------------===// |
1975 | |
1976 | /// This instruction constructs a fixed permutation of two |
1977 | /// input vectors. |
1978 | /// |
1979 | class ShuffleVectorInst : public Instruction { |
1980 | protected: |
1981 | // Note: Instruction needs to be a friend here to call cloneImpl. |
1982 | friend class Instruction; |
1983 | |
1984 | ShuffleVectorInst *cloneImpl() const; |
1985 | |
1986 | public: |
1987 | ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
1988 | const Twine &NameStr = "", |
1989 | Instruction *InsertBefor = nullptr); |
1990 | ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
1991 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
1992 | |
1993 | // allocate space for exactly three operands |
1994 | void *operator new(size_t s) { |
1995 | return User::operator new(s, 3); |
1996 | } |
1997 | |
1998 | /// Swap the first 2 operands and adjust the mask to preserve the semantics |
1999 | /// of the instruction. |
2000 | void commute(); |
2001 | |
2002 | /// Return true if a shufflevector instruction can be |
2003 | /// formed with the specified operands. |
2004 | static bool isValidOperands(const Value *V1, const Value *V2, |
2005 | const Value *Mask); |
2006 | |
2007 | /// Overload to return most specific vector type. |
2008 | /// |
2009 | VectorType *getType() const { |
2010 | return cast<VectorType>(Instruction::getType()); |
2011 | } |
2012 | |
2013 | /// Transparently provide more efficient getOperand methods. |
2014 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
2015 | |
2016 | Constant *getMask() const { |
2017 | return cast<Constant>(getOperand(2)); |
2018 | } |
2019 | |
2020 | /// Return the shuffle mask value for the specified element of the mask. |
2021 | /// Return -1 if the element is undef. |
2022 | static int getMaskValue(const Constant *Mask, unsigned Elt); |
2023 | |
2024 | /// Return the shuffle mask value of this instruction for the given element |
2025 | /// index. Return -1 if the element is undef. |
2026 | int getMaskValue(unsigned Elt) const { |
2027 | return getMaskValue(getMask(), Elt); |
2028 | } |
2029 | |
2030 | /// Convert the input shuffle mask operand to a vector of integers. Undefined |
2031 | /// elements of the mask are returned as -1. |
2032 | static void getShuffleMask(const Constant *Mask, |
2033 | SmallVectorImpl<int> &Result); |
2034 | |
2035 | /// Return the mask for this instruction as a vector of integers. Undefined |
2036 | /// elements of the mask are returned as -1. |
2037 | void getShuffleMask(SmallVectorImpl<int> &Result) const { |
2038 | return getShuffleMask(getMask(), Result); |
2039 | } |
2040 | |
2041 | SmallVector<int, 16> getShuffleMask() const { |
2042 | SmallVector<int, 16> Mask; |
2043 | getShuffleMask(Mask); |
2044 | return Mask; |
2045 | } |
2046 | |
2047 | /// Return true if this shuffle returns a vector with a different number of |
2048 | /// elements than its source vectors. |
2049 | /// Examples: shufflevector <4 x n> A, <4 x n> B, <1,2,3> |
2050 | /// shufflevector <4 x n> A, <4 x n> B, <1,2,3,4,5> |
2051 | bool changesLength() const { |
2052 | unsigned NumSourceElts = Op<0>()->getType()->getVectorNumElements(); |
2053 | unsigned NumMaskElts = getMask()->getType()->getVectorNumElements(); |
2054 | return NumSourceElts != NumMaskElts; |
2055 | } |
2056 | |
2057 | /// Return true if this shuffle returns a vector with a greater number of |
2058 | /// elements than its source vectors. |
2059 | /// Example: shufflevector <2 x n> A, <2 x n> B, <1,2,3> |
2060 | bool increasesLength() const { |
2061 | unsigned NumSourceElts = Op<0>()->getType()->getVectorNumElements(); |
2062 | unsigned NumMaskElts = getMask()->getType()->getVectorNumElements(); |
2063 | return NumSourceElts < NumMaskElts; |
2064 | } |
2065 | |
2066 | /// Return true if this shuffle mask chooses elements from exactly one source |
2067 | /// vector. |
2068 | /// Example: <7,5,undef,7> |
2069 | /// This assumes that vector operands are the same length as the mask. |
2070 | static bool isSingleSourceMask(ArrayRef<int> Mask); |
2071 | static bool isSingleSourceMask(const Constant *Mask) { |
2072 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2072, __PRETTY_FUNCTION__)); |
2073 | SmallVector<int, 16> MaskAsInts; |
2074 | getShuffleMask(Mask, MaskAsInts); |
2075 | return isSingleSourceMask(MaskAsInts); |
2076 | } |
2077 | |
2078 | /// Return true if this shuffle chooses elements from exactly one source |
2079 | /// vector without changing the length of that vector. |
2080 | /// Example: shufflevector <4 x n> A, <4 x n> B, <3,0,undef,3> |
2081 | /// TODO: Optionally allow length-changing shuffles. |
2082 | bool isSingleSource() const { |
2083 | return !changesLength() && isSingleSourceMask(getMask()); |
2084 | } |
2085 | |
2086 | /// Return true if this shuffle mask chooses elements from exactly one source |
2087 | /// vector without lane crossings. A shuffle using this mask is not |
2088 | /// necessarily a no-op because it may change the number of elements from its |
2089 | /// input vectors or it may provide demanded bits knowledge via undef lanes. |
2090 | /// Example: <undef,undef,2,3> |
2091 | static bool isIdentityMask(ArrayRef<int> Mask); |
2092 | static bool isIdentityMask(const Constant *Mask) { |
2093 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2093, __PRETTY_FUNCTION__)); |
2094 | SmallVector<int, 16> MaskAsInts; |
2095 | getShuffleMask(Mask, MaskAsInts); |
2096 | return isIdentityMask(MaskAsInts); |
2097 | } |
2098 | |
2099 | /// Return true if this shuffle chooses elements from exactly one source |
2100 | /// vector without lane crossings and does not change the number of elements |
2101 | /// from its input vectors. |
2102 | /// Example: shufflevector <4 x n> A, <4 x n> B, <4,undef,6,undef> |
2103 | bool isIdentity() const { |
2104 | return !changesLength() && isIdentityMask(getShuffleMask()); |
2105 | } |
2106 | |
2107 | /// Return true if this shuffle lengthens exactly one source vector with |
2108 | /// undefs in the high elements. |
2109 | bool isIdentityWithPadding() const; |
2110 | |
2111 | /// Return true if this shuffle extracts the first N elements of exactly one |
2112 | /// source vector. |
2113 | bool isIdentityWithExtract() const; |
2114 | |
2115 | /// Return true if this shuffle concatenates its 2 source vectors. This |
2116 | /// returns false if either input is undefined. In that case, the shuffle is |
2117 | /// is better classified as an identity with padding operation. |
2118 | bool isConcat() const; |
2119 | |
2120 | /// Return true if this shuffle mask chooses elements from its source vectors |
2121 | /// without lane crossings. A shuffle using this mask would be |
2122 | /// equivalent to a vector select with a constant condition operand. |
2123 | /// Example: <4,1,6,undef> |
2124 | /// This returns false if the mask does not choose from both input vectors. |
2125 | /// In that case, the shuffle is better classified as an identity shuffle. |
2126 | /// This assumes that vector operands are the same length as the mask |
2127 | /// (a length-changing shuffle can never be equivalent to a vector select). |
2128 | static bool isSelectMask(ArrayRef<int> Mask); |
2129 | static bool isSelectMask(const Constant *Mask) { |
2130 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2130, __PRETTY_FUNCTION__)); |
2131 | SmallVector<int, 16> MaskAsInts; |
2132 | getShuffleMask(Mask, MaskAsInts); |
2133 | return isSelectMask(MaskAsInts); |
2134 | } |
2135 | |
2136 | /// Return true if this shuffle chooses elements from its source vectors |
2137 | /// without lane crossings and all operands have the same number of elements. |
2138 | /// In other words, this shuffle is equivalent to a vector select with a |
2139 | /// constant condition operand. |
2140 | /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,1,6,3> |
2141 | /// This returns false if the mask does not choose from both input vectors. |
2142 | /// In that case, the shuffle is better classified as an identity shuffle. |
2143 | /// TODO: Optionally allow length-changing shuffles. |
2144 | bool isSelect() const { |
2145 | return !changesLength() && isSelectMask(getMask()); |
2146 | } |
2147 | |
2148 | /// Return true if this shuffle mask swaps the order of elements from exactly |
2149 | /// one source vector. |
2150 | /// Example: <7,6,undef,4> |
2151 | /// This assumes that vector operands are the same length as the mask. |
2152 | static bool isReverseMask(ArrayRef<int> Mask); |
2153 | static bool isReverseMask(const Constant *Mask) { |
2154 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2154, __PRETTY_FUNCTION__)); |
2155 | SmallVector<int, 16> MaskAsInts; |
2156 | getShuffleMask(Mask, MaskAsInts); |
2157 | return isReverseMask(MaskAsInts); |
2158 | } |
2159 | |
2160 | /// Return true if this shuffle swaps the order of elements from exactly |
2161 | /// one source vector. |
2162 | /// Example: shufflevector <4 x n> A, <4 x n> B, <3,undef,1,undef> |
2163 | /// TODO: Optionally allow length-changing shuffles. |
2164 | bool isReverse() const { |
2165 | return !changesLength() && isReverseMask(getMask()); |
2166 | } |
2167 | |
2168 | /// Return true if this shuffle mask chooses all elements with the same value |
2169 | /// as the first element of exactly one source vector. |
2170 | /// Example: <4,undef,undef,4> |
2171 | /// This assumes that vector operands are the same length as the mask. |
2172 | static bool isZeroEltSplatMask(ArrayRef<int> Mask); |
2173 | static bool isZeroEltSplatMask(const Constant *Mask) { |
2174 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2174, __PRETTY_FUNCTION__)); |
2175 | SmallVector<int, 16> MaskAsInts; |
2176 | getShuffleMask(Mask, MaskAsInts); |
2177 | return isZeroEltSplatMask(MaskAsInts); |
2178 | } |
2179 | |
2180 | /// Return true if all elements of this shuffle are the same value as the |
2181 | /// first element of exactly one source vector without changing the length |
2182 | /// of that vector. |
2183 | /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,0,undef,0> |
2184 | /// TODO: Optionally allow length-changing shuffles. |
2185 | /// TODO: Optionally allow splats from other elements. |
2186 | bool isZeroEltSplat() const { |
2187 | return !changesLength() && isZeroEltSplatMask(getMask()); |
2188 | } |
2189 | |
2190 | /// Return true if this shuffle mask is a transpose mask. |
2191 | /// Transpose vector masks transpose a 2xn matrix. They read corresponding |
2192 | /// even- or odd-numbered vector elements from two n-dimensional source |
2193 | /// vectors and write each result into consecutive elements of an |
2194 | /// n-dimensional destination vector. Two shuffles are necessary to complete |
2195 | /// the transpose, one for the even elements and another for the odd elements. |
2196 | /// This description closely follows how the TRN1 and TRN2 AArch64 |
2197 | /// instructions operate. |
2198 | /// |
2199 | /// For example, a simple 2x2 matrix can be transposed with: |
2200 | /// |
2201 | /// ; Original matrix |
2202 | /// m0 = < a, b > |
2203 | /// m1 = < c, d > |
2204 | /// |
2205 | /// ; Transposed matrix |
2206 | /// t0 = < a, c > = shufflevector m0, m1, < 0, 2 > |
2207 | /// t1 = < b, d > = shufflevector m0, m1, < 1, 3 > |
2208 | /// |
2209 | /// For matrices having greater than n columns, the resulting nx2 transposed |
2210 | /// matrix is stored in two result vectors such that one vector contains |
2211 | /// interleaved elements from all the even-numbered rows and the other vector |
2212 | /// contains interleaved elements from all the odd-numbered rows. For example, |
2213 | /// a 2x4 matrix can be transposed with: |
2214 | /// |
2215 | /// ; Original matrix |
2216 | /// m0 = < a, b, c, d > |
2217 | /// m1 = < e, f, g, h > |
2218 | /// |
2219 | /// ; Transposed matrix |
2220 | /// t0 = < a, e, c, g > = shufflevector m0, m1 < 0, 4, 2, 6 > |
2221 | /// t1 = < b, f, d, h > = shufflevector m0, m1 < 1, 5, 3, 7 > |
2222 | static bool isTransposeMask(ArrayRef<int> Mask); |
2223 | static bool isTransposeMask(const Constant *Mask) { |
2224 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2224, __PRETTY_FUNCTION__)); |
2225 | SmallVector<int, 16> MaskAsInts; |
2226 | getShuffleMask(Mask, MaskAsInts); |
2227 | return isTransposeMask(MaskAsInts); |
2228 | } |
2229 | |
2230 | /// Return true if this shuffle transposes the elements of its inputs without |
2231 | /// changing the length of the vectors. This operation may also be known as a |
2232 | /// merge or interleave. See the description for isTransposeMask() for the |
2233 | /// exact specification. |
2234 | /// Example: shufflevector <4 x n> A, <4 x n> B, <0,4,2,6> |
2235 | bool isTranspose() const { |
2236 | return !changesLength() && isTransposeMask(getMask()); |
2237 | } |
2238 | |
2239 | /// Return true if this shuffle mask is an extract subvector mask. |
2240 | /// A valid extract subvector mask returns a smaller vector from a single |
2241 | /// source operand. The base extraction index is returned as well. |
2242 | static bool isExtractSubvectorMask(ArrayRef<int> Mask, int NumSrcElts, |
2243 | int &Index); |
2244 | static bool isExtractSubvectorMask(const Constant *Mask, int NumSrcElts, |
2245 | int &Index) { |
2246 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2246, __PRETTY_FUNCTION__)); |
2247 | SmallVector<int, 16> MaskAsInts; |
2248 | getShuffleMask(Mask, MaskAsInts); |
2249 | return isExtractSubvectorMask(MaskAsInts, NumSrcElts, Index); |
2250 | } |
2251 | |
2252 | /// Return true if this shuffle mask is an extract subvector mask. |
2253 | bool isExtractSubvectorMask(int &Index) const { |
2254 | int NumSrcElts = Op<0>()->getType()->getVectorNumElements(); |
2255 | return isExtractSubvectorMask(getMask(), NumSrcElts, Index); |
2256 | } |
2257 | |
2258 | /// Change values in a shuffle permute mask assuming the two vector operands |
2259 | /// of length InVecNumElts have swapped position. |
2260 | static void commuteShuffleMask(MutableArrayRef<int> Mask, |
2261 | unsigned InVecNumElts) { |
2262 | for (int &Idx : Mask) { |
2263 | if (Idx == -1) |
2264 | continue; |
2265 | Idx = Idx < (int)InVecNumElts ? Idx + InVecNumElts : Idx - InVecNumElts; |
2266 | assert(Idx >= 0 && Idx < (int)InVecNumElts * 2 &&((Idx >= 0 && Idx < (int)InVecNumElts * 2 && "shufflevector mask index out of range") ? static_cast<void > (0) : __assert_fail ("Idx >= 0 && Idx < (int)InVecNumElts * 2 && \"shufflevector mask index out of range\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2267, __PRETTY_FUNCTION__)) |
2267 | "shufflevector mask index out of range")((Idx >= 0 && Idx < (int)InVecNumElts * 2 && "shufflevector mask index out of range") ? static_cast<void > (0) : __assert_fail ("Idx >= 0 && Idx < (int)InVecNumElts * 2 && \"shufflevector mask index out of range\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2267, __PRETTY_FUNCTION__)); |
2268 | } |
2269 | } |
2270 | |
2271 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
2272 | static bool classof(const Instruction *I) { |
2273 | return I->getOpcode() == Instruction::ShuffleVector; |
2274 | } |
2275 | static bool classof(const Value *V) { |
2276 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
2277 | } |
2278 | }; |
2279 | |
2280 | template <> |
2281 | struct OperandTraits<ShuffleVectorInst> : |
2282 | public FixedNumOperandTraits<ShuffleVectorInst, 3> { |
2283 | }; |
2284 | |
2285 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)ShuffleVectorInst::op_iterator ShuffleVectorInst::op_begin() { return OperandTraits<ShuffleVectorInst>::op_begin(this ); } ShuffleVectorInst::const_op_iterator ShuffleVectorInst:: op_begin() const { return OperandTraits<ShuffleVectorInst> ::op_begin(const_cast<ShuffleVectorInst*>(this)); } ShuffleVectorInst ::op_iterator ShuffleVectorInst::op_end() { return OperandTraits <ShuffleVectorInst>::op_end(this); } ShuffleVectorInst:: const_op_iterator ShuffleVectorInst::op_end() const { return OperandTraits <ShuffleVectorInst>::op_end(const_cast<ShuffleVectorInst *>(this)); } Value *ShuffleVectorInst::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<ShuffleVectorInst >::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<ShuffleVectorInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2285, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<ShuffleVectorInst>::op_begin(const_cast <ShuffleVectorInst*>(this))[i_nocapture].get()); } void ShuffleVectorInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((i_nocapture < OperandTraits<ShuffleVectorInst> ::operands(this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<ShuffleVectorInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2285, __PRETTY_FUNCTION__)); OperandTraits<ShuffleVectorInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned ShuffleVectorInst::getNumOperands() const { return OperandTraits <ShuffleVectorInst>::operands(this); } template <int Idx_nocapture> Use &ShuffleVectorInst::Op() { return this ->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture > const Use &ShuffleVectorInst::Op() const { return this ->OpFrom<Idx_nocapture>(this); } |
2286 | |
2287 | //===----------------------------------------------------------------------===// |
2288 | // ExtractValueInst Class |
2289 | //===----------------------------------------------------------------------===// |
2290 | |
2291 | /// This instruction extracts a struct member or array |
2292 | /// element value from an aggregate value. |
2293 | /// |
2294 | class ExtractValueInst : public UnaryInstruction { |
2295 | SmallVector<unsigned, 4> Indices; |
2296 | |
2297 | ExtractValueInst(const ExtractValueInst &EVI); |
2298 | |
2299 | /// Constructors - Create a extractvalue instruction with a base aggregate |
2300 | /// value and a list of indices. The first ctor can optionally insert before |
2301 | /// an existing instruction, the second appends the new instruction to the |
2302 | /// specified BasicBlock. |
2303 | inline ExtractValueInst(Value *Agg, |
2304 | ArrayRef<unsigned> Idxs, |
2305 | const Twine &NameStr, |
2306 | Instruction *InsertBefore); |
2307 | inline ExtractValueInst(Value *Agg, |
2308 | ArrayRef<unsigned> Idxs, |
2309 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
2310 | |
2311 | void init(ArrayRef<unsigned> Idxs, const Twine &NameStr); |
2312 | |
2313 | protected: |
2314 | // Note: Instruction needs to be a friend here to call cloneImpl. |
2315 | friend class Instruction; |
2316 | |
2317 | ExtractValueInst *cloneImpl() const; |
2318 | |
2319 | public: |
2320 | static ExtractValueInst *Create(Value *Agg, |
2321 | ArrayRef<unsigned> Idxs, |
2322 | const Twine &NameStr = "", |
2323 | Instruction *InsertBefore = nullptr) { |
2324 | return new |
2325 | ExtractValueInst(Agg, Idxs, NameStr, InsertBefore); |
2326 | } |
2327 | |
2328 | static ExtractValueInst *Create(Value *Agg, |
2329 | ArrayRef<unsigned> Idxs, |
2330 | const Twine &NameStr, |
2331 | BasicBlock *InsertAtEnd) { |
2332 | return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd); |
2333 | } |
2334 | |
2335 | /// Returns the type of the element that would be extracted |
2336 | /// with an extractvalue instruction with the specified parameters. |
2337 | /// |
2338 | /// Null is returned if the indices are invalid for the specified type. |
2339 | static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs); |
2340 | |
2341 | using idx_iterator = const unsigned*; |
2342 | |
2343 | inline idx_iterator idx_begin() const { return Indices.begin(); } |
2344 | inline idx_iterator idx_end() const { return Indices.end(); } |
2345 | inline iterator_range<idx_iterator> indices() const { |
2346 | return make_range(idx_begin(), idx_end()); |
2347 | } |
2348 | |
2349 | Value *getAggregateOperand() { |
2350 | return getOperand(0); |
2351 | } |
2352 | const Value *getAggregateOperand() const { |
2353 | return getOperand(0); |
2354 | } |
2355 | static unsigned getAggregateOperandIndex() { |
2356 | return 0U; // get index for modifying correct operand |
2357 | } |
2358 | |
2359 | ArrayRef<unsigned> getIndices() const { |
2360 | return Indices; |
2361 | } |
2362 | |
2363 | unsigned getNumIndices() const { |
2364 | return (unsigned)Indices.size(); |
2365 | } |
2366 | |
2367 | bool hasIndices() const { |
2368 | return true; |
2369 | } |
2370 | |
2371 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
2372 | static bool classof(const Instruction *I) { |
2373 | return I->getOpcode() == Instruction::ExtractValue; |
2374 | } |
2375 | static bool classof(const Value *V) { |
2376 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
2377 | } |
2378 | }; |
2379 | |
2380 | ExtractValueInst::ExtractValueInst(Value *Agg, |
2381 | ArrayRef<unsigned> Idxs, |
2382 | const Twine &NameStr, |
2383 | Instruction *InsertBefore) |
2384 | : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)), |
2385 | ExtractValue, Agg, InsertBefore) { |
2386 | init(Idxs, NameStr); |
2387 | } |
2388 | |
2389 | ExtractValueInst::ExtractValueInst(Value *Agg, |
2390 | ArrayRef<unsigned> Idxs, |
2391 | const Twine &NameStr, |
2392 | BasicBlock *InsertAtEnd) |
2393 | : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)), |
2394 | ExtractValue, Agg, InsertAtEnd) { |
2395 | init(Idxs, NameStr); |
2396 | } |
2397 | |
2398 | //===----------------------------------------------------------------------===// |
2399 | // InsertValueInst Class |
2400 | //===----------------------------------------------------------------------===// |
2401 | |
2402 | /// This instruction inserts a struct field of array element |
2403 | /// value into an aggregate value. |
2404 | /// |
2405 | class InsertValueInst : public Instruction { |
2406 | SmallVector<unsigned, 4> Indices; |
2407 | |
2408 | InsertValueInst(const InsertValueInst &IVI); |
2409 | |
2410 | /// Constructors - Create a insertvalue instruction with a base aggregate |
2411 | /// value, a value to insert, and a list of indices. The first ctor can |
2412 | /// optionally insert before an existing instruction, the second appends |
2413 | /// the new instruction to the specified BasicBlock. |
2414 | inline InsertValueInst(Value *Agg, Value *Val, |
2415 | ArrayRef<unsigned> Idxs, |
2416 | const Twine &NameStr, |
2417 | Instruction *InsertBefore); |
2418 | inline InsertValueInst(Value *Agg, Value *Val, |
2419 | ArrayRef<unsigned> Idxs, |
2420 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
2421 | |
2422 | /// Constructors - These two constructors are convenience methods because one |
2423 | /// and two index insertvalue instructions are so common. |
2424 | InsertValueInst(Value *Agg, Value *Val, unsigned Idx, |
2425 | const Twine &NameStr = "", |
2426 | Instruction *InsertBefore = nullptr); |
2427 | InsertValueInst(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr, |
2428 | BasicBlock *InsertAtEnd); |
2429 | |
2430 | void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, |
2431 | const Twine &NameStr); |
2432 | |
2433 | protected: |
2434 | // Note: Instruction needs to be a friend here to call cloneImpl. |
2435 | friend class Instruction; |
2436 | |
2437 | InsertValueInst *cloneImpl() const; |
2438 | |
2439 | public: |
2440 | // allocate space for exactly two operands |
2441 | void *operator new(size_t s) { |
2442 | return User::operator new(s, 2); |
2443 | } |
2444 | |
2445 | static InsertValueInst *Create(Value *Agg, Value *Val, |
2446 | ArrayRef<unsigned> Idxs, |
2447 | const Twine &NameStr = "", |
2448 | Instruction *InsertBefore = nullptr) { |
2449 | return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore); |
2450 | } |
2451 | |
2452 | static InsertValueInst *Create(Value *Agg, Value *Val, |
2453 | ArrayRef<unsigned> Idxs, |
2454 | const Twine &NameStr, |
2455 | BasicBlock *InsertAtEnd) { |
2456 | return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd); |
2457 | } |
2458 | |
2459 | /// Transparently provide more efficient getOperand methods. |
2460 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
2461 | |
2462 | using idx_iterator = const unsigned*; |
2463 | |
2464 | inline idx_iterator idx_begin() const { return Indices.begin(); } |
2465 | inline idx_iterator idx_end() const { return Indices.end(); } |
2466 | inline iterator_range<idx_iterator> indices() const { |
2467 | return make_range(idx_begin(), idx_end()); |
2468 | } |
2469 | |
2470 | Value *getAggregateOperand() { |
2471 | return getOperand(0); |
2472 | } |
2473 | const Value *getAggregateOperand() const { |
2474 | return getOperand(0); |
2475 | } |
2476 | static unsigned getAggregateOperandIndex() { |
2477 | return 0U; // get index for modifying correct operand |
2478 | } |
2479 | |
2480 | Value *getInsertedValueOperand() { |
2481 | return getOperand(1); |
2482 | } |
2483 | const Value *getInsertedValueOperand() const { |
2484 | return getOperand(1); |
2485 | } |
2486 | static unsigned getInsertedValueOperandIndex() { |
2487 | return 1U; // get index for modifying correct operand |
2488 | } |
2489 | |
2490 | ArrayRef<unsigned> getIndices() const { |
2491 | return Indices; |
2492 | } |
2493 | |
2494 | unsigned getNumIndices() const { |
2495 | return (unsigned)Indices.size(); |
2496 | } |
2497 | |
2498 | bool hasIndices() const { |
2499 | return true; |
2500 | } |
2501 | |
2502 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
2503 | static bool classof(const Instruction *I) { |
2504 | return I->getOpcode() == Instruction::InsertValue; |
2505 | } |
2506 | static bool classof(const Value *V) { |
2507 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
2508 | } |
2509 | }; |
2510 | |
2511 | template <> |
2512 | struct OperandTraits<InsertValueInst> : |
2513 | public FixedNumOperandTraits<InsertValueInst, 2> { |
2514 | }; |
2515 | |
2516 | InsertValueInst::InsertValueInst(Value *Agg, |
2517 | Value *Val, |
2518 | ArrayRef<unsigned> Idxs, |
2519 | const Twine &NameStr, |
2520 | Instruction *InsertBefore) |
2521 | : Instruction(Agg->getType(), InsertValue, |
2522 | OperandTraits<InsertValueInst>::op_begin(this), |
2523 | 2, InsertBefore) { |
2524 | init(Agg, Val, Idxs, NameStr); |
2525 | } |
2526 | |
2527 | InsertValueInst::InsertValueInst(Value *Agg, |
2528 | Value *Val, |
2529 | ArrayRef<unsigned> Idxs, |
2530 | const Twine &NameStr, |
2531 | BasicBlock *InsertAtEnd) |
2532 | : Instruction(Agg->getType(), InsertValue, |
2533 | OperandTraits<InsertValueInst>::op_begin(this), |
2534 | 2, InsertAtEnd) { |
2535 | init(Agg, Val, Idxs, NameStr); |
2536 | } |
2537 | |
2538 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)InsertValueInst::op_iterator InsertValueInst::op_begin() { return OperandTraits<InsertValueInst>::op_begin(this); } InsertValueInst ::const_op_iterator InsertValueInst::op_begin() const { return OperandTraits<InsertValueInst>::op_begin(const_cast< InsertValueInst*>(this)); } InsertValueInst::op_iterator InsertValueInst ::op_end() { return OperandTraits<InsertValueInst>::op_end (this); } InsertValueInst::const_op_iterator InsertValueInst:: op_end() const { return OperandTraits<InsertValueInst>:: op_end(const_cast<InsertValueInst*>(this)); } Value *InsertValueInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<InsertValueInst>::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<InsertValueInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2538, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<InsertValueInst>::op_begin(const_cast< InsertValueInst*>(this))[i_nocapture].get()); } void InsertValueInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<InsertValueInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<InsertValueInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2538, __PRETTY_FUNCTION__)); OperandTraits<InsertValueInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned InsertValueInst::getNumOperands() const { return OperandTraits <InsertValueInst>::operands(this); } template <int Idx_nocapture > Use &InsertValueInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &InsertValueInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } |
2539 | |
2540 | //===----------------------------------------------------------------------===// |
2541 | // PHINode Class |
2542 | //===----------------------------------------------------------------------===// |
2543 | |
2544 | // PHINode - The PHINode class is used to represent the magical mystical PHI |
2545 | // node, that can not exist in nature, but can be synthesized in a computer |
2546 | // scientist's overactive imagination. |
2547 | // |
2548 | class PHINode : public Instruction { |
2549 | /// The number of operands actually allocated. NumOperands is |
2550 | /// the number actually in use. |
2551 | unsigned ReservedSpace; |
2552 | |
2553 | PHINode(const PHINode &PN); |
2554 | |
2555 | explicit PHINode(Type *Ty, unsigned NumReservedValues, |
2556 | const Twine &NameStr = "", |
2557 | Instruction *InsertBefore = nullptr) |
2558 | : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore), |
2559 | ReservedSpace(NumReservedValues) { |
2560 | setName(NameStr); |
2561 | allocHungoffUses(ReservedSpace); |
2562 | } |
2563 | |
2564 | PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr, |
2565 | BasicBlock *InsertAtEnd) |
2566 | : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd), |
2567 | ReservedSpace(NumReservedValues) { |
2568 | setName(NameStr); |
2569 | allocHungoffUses(ReservedSpace); |
2570 | } |
2571 | |
2572 | protected: |
2573 | // Note: Instruction needs to be a friend here to call cloneImpl. |
2574 | friend class Instruction; |
2575 | |
2576 | PHINode *cloneImpl() const; |
2577 | |
2578 | // allocHungoffUses - this is more complicated than the generic |
2579 | // User::allocHungoffUses, because we have to allocate Uses for the incoming |
2580 | // values and pointers to the incoming blocks, all in one allocation. |
2581 | void allocHungoffUses(unsigned N) { |
2582 | User::allocHungoffUses(N, /* IsPhi */ true); |
2583 | } |
2584 | |
2585 | public: |
2586 | /// Constructors - NumReservedValues is a hint for the number of incoming |
2587 | /// edges that this phi node will have (use 0 if you really have no idea). |
2588 | static PHINode *Create(Type *Ty, unsigned NumReservedValues, |
2589 | const Twine &NameStr = "", |
2590 | Instruction *InsertBefore = nullptr) { |
2591 | return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore); |
2592 | } |
2593 | |
2594 | static PHINode *Create(Type *Ty, unsigned NumReservedValues, |
2595 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
2596 | return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd); |
2597 | } |
2598 | |
2599 | /// Provide fast operand accessors |
2600 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
2601 | |
2602 | // Block iterator interface. This provides access to the list of incoming |
2603 | // basic blocks, which parallels the list of incoming values. |
2604 | |
2605 | using block_iterator = BasicBlock **; |
2606 | using const_block_iterator = BasicBlock * const *; |
2607 | |
2608 | block_iterator block_begin() { |
2609 | Use::UserRef *ref = |
2610 | reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace); |
2611 | return reinterpret_cast<block_iterator>(ref + 1); |
2612 | } |
2613 | |
2614 | const_block_iterator block_begin() const { |
2615 | const Use::UserRef *ref = |
2616 | reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace); |
2617 | return reinterpret_cast<const_block_iterator>(ref + 1); |
2618 | } |
2619 | |
2620 | block_iterator block_end() { |
2621 | return block_begin() + getNumOperands(); |
2622 | } |
2623 | |
2624 | const_block_iterator block_end() const { |
2625 | return block_begin() + getNumOperands(); |
2626 | } |
2627 | |
2628 | iterator_range<block_iterator> blocks() { |
2629 | return make_range(block_begin(), block_end()); |
2630 | } |
2631 | |
2632 | iterator_range<const_block_iterator> blocks() const { |
2633 | return make_range(block_begin(), block_end()); |
2634 | } |
2635 | |
2636 | op_range incoming_values() { return operands(); } |
2637 | |
2638 | const_op_range incoming_values() const { return operands(); } |
2639 | |
2640 | /// Return the number of incoming edges |
2641 | /// |
2642 | unsigned getNumIncomingValues() const { return getNumOperands(); } |
2643 | |
2644 | /// Return incoming value number x |
2645 | /// |
2646 | Value *getIncomingValue(unsigned i) const { |
2647 | return getOperand(i); |
2648 | } |
2649 | void setIncomingValue(unsigned i, Value *V) { |
2650 | assert(V && "PHI node got a null value!")((V && "PHI node got a null value!") ? static_cast< void> (0) : __assert_fail ("V && \"PHI node got a null value!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2650, __PRETTY_FUNCTION__)); |
2651 | assert(getType() == V->getType() &&((getType() == V->getType() && "All operands to PHI node must be the same type as the PHI node!" ) ? static_cast<void> (0) : __assert_fail ("getType() == V->getType() && \"All operands to PHI node must be the same type as the PHI node!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2652, __PRETTY_FUNCTION__)) |
2652 | "All operands to PHI node must be the same type as the PHI node!")((getType() == V->getType() && "All operands to PHI node must be the same type as the PHI node!" ) ? static_cast<void> (0) : __assert_fail ("getType() == V->getType() && \"All operands to PHI node must be the same type as the PHI node!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2652, __PRETTY_FUNCTION__)); |
2653 | setOperand(i, V); |
2654 | } |
2655 | |
2656 | static unsigned getOperandNumForIncomingValue(unsigned i) { |
2657 | return i; |
2658 | } |
2659 | |
2660 | static unsigned getIncomingValueNumForOperand(unsigned i) { |
2661 | return i; |
2662 | } |
2663 | |
2664 | /// Return incoming basic block number @p i. |
2665 | /// |
2666 | BasicBlock *getIncomingBlock(unsigned i) const { |
2667 | return block_begin()[i]; |
2668 | } |
2669 | |
2670 | /// Return incoming basic block corresponding |
2671 | /// to an operand of the PHI. |
2672 | /// |
2673 | BasicBlock *getIncomingBlock(const Use &U) const { |
2674 | assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?")((this == U.getUser() && "Iterator doesn't point to PHI's Uses?" ) ? static_cast<void> (0) : __assert_fail ("this == U.getUser() && \"Iterator doesn't point to PHI's Uses?\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2674, __PRETTY_FUNCTION__)); |
2675 | return getIncomingBlock(unsigned(&U - op_begin())); |
2676 | } |
2677 | |
2678 | /// Return incoming basic block corresponding |
2679 | /// to value use iterator. |
2680 | /// |
2681 | BasicBlock *getIncomingBlock(Value::const_user_iterator I) const { |
2682 | return getIncomingBlock(I.getUse()); |
2683 | } |
2684 | |
2685 | void setIncomingBlock(unsigned i, BasicBlock *BB) { |
2686 | assert(BB && "PHI node got a null basic block!")((BB && "PHI node got a null basic block!") ? static_cast <void> (0) : __assert_fail ("BB && \"PHI node got a null basic block!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2686, __PRETTY_FUNCTION__)); |
2687 | block_begin()[i] = BB; |
2688 | } |
2689 | |
2690 | /// Replace every incoming basic block \p Old to basic block \p New. |
2691 | void replaceIncomingBlockWith(const BasicBlock *Old, BasicBlock *New) { |
2692 | assert(New && Old && "PHI node got a null basic block!")((New && Old && "PHI node got a null basic block!" ) ? static_cast<void> (0) : __assert_fail ("New && Old && \"PHI node got a null basic block!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2692, __PRETTY_FUNCTION__)); |
2693 | for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op) |
2694 | if (getIncomingBlock(Op) == Old) |
2695 | setIncomingBlock(Op, New); |
2696 | } |
2697 | |
2698 | /// Add an incoming value to the end of the PHI list |
2699 | /// |
2700 | void addIncoming(Value *V, BasicBlock *BB) { |
2701 | if (getNumOperands() == ReservedSpace) |
2702 | growOperands(); // Get more space! |
2703 | // Initialize some new operands. |
2704 | setNumHungOffUseOperands(getNumOperands() + 1); |
2705 | setIncomingValue(getNumOperands() - 1, V); |
2706 | setIncomingBlock(getNumOperands() - 1, BB); |
2707 | } |
2708 | |
2709 | /// Remove an incoming value. This is useful if a |
2710 | /// predecessor basic block is deleted. The value removed is returned. |
2711 | /// |
2712 | /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty |
2713 | /// is true), the PHI node is destroyed and any uses of it are replaced with |
2714 | /// dummy values. The only time there should be zero incoming values to a PHI |
2715 | /// node is when the block is dead, so this strategy is sound. |
2716 | /// |
2717 | Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true); |
2718 | |
2719 | Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) { |
2720 | int Idx = getBasicBlockIndex(BB); |
2721 | assert(Idx >= 0 && "Invalid basic block argument to remove!")((Idx >= 0 && "Invalid basic block argument to remove!" ) ? static_cast<void> (0) : __assert_fail ("Idx >= 0 && \"Invalid basic block argument to remove!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2721, __PRETTY_FUNCTION__)); |
2722 | return removeIncomingValue(Idx, DeletePHIIfEmpty); |
2723 | } |
2724 | |
2725 | /// Return the first index of the specified basic |
2726 | /// block in the value list for this PHI. Returns -1 if no instance. |
2727 | /// |
2728 | int getBasicBlockIndex(const BasicBlock *BB) const { |
2729 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
2730 | if (block_begin()[i] == BB) |
2731 | return i; |
2732 | return -1; |
2733 | } |
2734 | |
2735 | Value *getIncomingValueForBlock(const BasicBlock *BB) const { |
2736 | int Idx = getBasicBlockIndex(BB); |
2737 | assert(Idx >= 0 && "Invalid basic block argument!")((Idx >= 0 && "Invalid basic block argument!") ? static_cast <void> (0) : __assert_fail ("Idx >= 0 && \"Invalid basic block argument!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2737, __PRETTY_FUNCTION__)); |
2738 | return getIncomingValue(Idx); |
2739 | } |
2740 | |
2741 | /// Set every incoming value(s) for block \p BB to \p V. |
2742 | void setIncomingValueForBlock(const BasicBlock *BB, Value *V) { |
2743 | assert(BB && "PHI node got a null basic block!")((BB && "PHI node got a null basic block!") ? static_cast <void> (0) : __assert_fail ("BB && \"PHI node got a null basic block!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2743, __PRETTY_FUNCTION__)); |
2744 | bool Found = false; |
2745 | for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op) |
2746 | if (getIncomingBlock(Op) == BB) { |
2747 | Found = true; |
2748 | setIncomingValue(Op, V); |
2749 | } |
2750 | (void)Found; |
2751 | assert(Found && "Invalid basic block argument to set!")((Found && "Invalid basic block argument to set!") ? static_cast <void> (0) : __assert_fail ("Found && \"Invalid basic block argument to set!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2751, __PRETTY_FUNCTION__)); |
2752 | } |
2753 | |
2754 | /// If the specified PHI node always merges together the |
2755 | /// same value, return the value, otherwise return null. |
2756 | Value *hasConstantValue() const; |
2757 | |
2758 | /// Whether the specified PHI node always merges |
2759 | /// together the same value, assuming undefs are equal to a unique |
2760 | /// non-undef value. |
2761 | bool hasConstantOrUndefValue() const; |
2762 | |
2763 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
2764 | static bool classof(const Instruction *I) { |
2765 | return I->getOpcode() == Instruction::PHI; |
2766 | } |
2767 | static bool classof(const Value *V) { |
2768 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
2769 | } |
2770 | |
2771 | private: |
2772 | void growOperands(); |
2773 | }; |
2774 | |
2775 | template <> |
2776 | struct OperandTraits<PHINode> : public HungoffOperandTraits<2> { |
2777 | }; |
2778 | |
2779 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)PHINode::op_iterator PHINode::op_begin() { return OperandTraits <PHINode>::op_begin(this); } PHINode::const_op_iterator PHINode::op_begin() const { return OperandTraits<PHINode> ::op_begin(const_cast<PHINode*>(this)); } PHINode::op_iterator PHINode::op_end() { return OperandTraits<PHINode>::op_end (this); } PHINode::const_op_iterator PHINode::op_end() const { return OperandTraits<PHINode>::op_end(const_cast<PHINode *>(this)); } Value *PHINode::getOperand(unsigned i_nocapture ) const { ((i_nocapture < OperandTraits<PHINode>::operands (this) && "getOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<PHINode>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2779, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<PHINode>::op_begin(const_cast<PHINode *>(this))[i_nocapture].get()); } void PHINode::setOperand( unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<PHINode>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<PHINode>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2779, __PRETTY_FUNCTION__)); OperandTraits<PHINode>:: op_begin(this)[i_nocapture] = Val_nocapture; } unsigned PHINode ::getNumOperands() const { return OperandTraits<PHINode> ::operands(this); } template <int Idx_nocapture> Use & PHINode::Op() { return this->OpFrom<Idx_nocapture>(this ); } template <int Idx_nocapture> const Use &PHINode ::Op() const { return this->OpFrom<Idx_nocapture>(this ); } |
2780 | |
2781 | //===----------------------------------------------------------------------===// |
2782 | // LandingPadInst Class |
2783 | //===----------------------------------------------------------------------===// |
2784 | |
2785 | //===--------------------------------------------------------------------------- |
2786 | /// The landingpad instruction holds all of the information |
2787 | /// necessary to generate correct exception handling. The landingpad instruction |
2788 | /// cannot be moved from the top of a landing pad block, which itself is |
2789 | /// accessible only from the 'unwind' edge of an invoke. This uses the |
2790 | /// SubclassData field in Value to store whether or not the landingpad is a |
2791 | /// cleanup. |
2792 | /// |
2793 | class LandingPadInst : public Instruction { |
2794 | /// The number of operands actually allocated. NumOperands is |
2795 | /// the number actually in use. |
2796 | unsigned ReservedSpace; |
2797 | |
2798 | LandingPadInst(const LandingPadInst &LP); |
2799 | |
2800 | public: |
2801 | enum ClauseType { Catch, Filter }; |
2802 | |
2803 | private: |
2804 | explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues, |
2805 | const Twine &NameStr, Instruction *InsertBefore); |
2806 | explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues, |
2807 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
2808 | |
2809 | // Allocate space for exactly zero operands. |
2810 | void *operator new(size_t s) { |
2811 | return User::operator new(s); |
2812 | } |
2813 | |
2814 | void growOperands(unsigned Size); |
2815 | void init(unsigned NumReservedValues, const Twine &NameStr); |
2816 | |
2817 | protected: |
2818 | // Note: Instruction needs to be a friend here to call cloneImpl. |
2819 | friend class Instruction; |
2820 | |
2821 | LandingPadInst *cloneImpl() const; |
2822 | |
2823 | public: |
2824 | /// Constructors - NumReservedClauses is a hint for the number of incoming |
2825 | /// clauses that this landingpad will have (use 0 if you really have no idea). |
2826 | static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses, |
2827 | const Twine &NameStr = "", |
2828 | Instruction *InsertBefore = nullptr); |
2829 | static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses, |
2830 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
2831 | |
2832 | /// Provide fast operand accessors |
2833 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
2834 | |
2835 | /// Return 'true' if this landingpad instruction is a |
2836 | /// cleanup. I.e., it should be run when unwinding even if its landing pad |
2837 | /// doesn't catch the exception. |
2838 | bool isCleanup() const { return getSubclassDataFromInstruction() & 1; } |
2839 | |
2840 | /// Indicate that this landingpad instruction is a cleanup. |
2841 | void setCleanup(bool V) { |
2842 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) | |
2843 | (V ? 1 : 0)); |
2844 | } |
2845 | |
2846 | /// Add a catch or filter clause to the landing pad. |
2847 | void addClause(Constant *ClauseVal); |
2848 | |
2849 | /// Get the value of the clause at index Idx. Use isCatch/isFilter to |
2850 | /// determine what type of clause this is. |
2851 | Constant *getClause(unsigned Idx) const { |
2852 | return cast<Constant>(getOperandList()[Idx]); |
2853 | } |
2854 | |
2855 | /// Return 'true' if the clause and index Idx is a catch clause. |
2856 | bool isCatch(unsigned Idx) const { |
2857 | return !isa<ArrayType>(getOperandList()[Idx]->getType()); |
2858 | } |
2859 | |
2860 | /// Return 'true' if the clause and index Idx is a filter clause. |
2861 | bool isFilter(unsigned Idx) const { |
2862 | return isa<ArrayType>(getOperandList()[Idx]->getType()); |
2863 | } |
2864 | |
2865 | /// Get the number of clauses for this landing pad. |
2866 | unsigned getNumClauses() const { return getNumOperands(); } |
2867 | |
2868 | /// Grow the size of the operand list to accommodate the new |
2869 | /// number of clauses. |
2870 | void reserveClauses(unsigned Size) { growOperands(Size); } |
2871 | |
2872 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
2873 | static bool classof(const Instruction *I) { |
2874 | return I->getOpcode() == Instruction::LandingPad; |
2875 | } |
2876 | static bool classof(const Value *V) { |
2877 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
2878 | } |
2879 | }; |
2880 | |
2881 | template <> |
2882 | struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<1> { |
2883 | }; |
2884 | |
2885 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)LandingPadInst::op_iterator LandingPadInst::op_begin() { return OperandTraits<LandingPadInst>::op_begin(this); } LandingPadInst ::const_op_iterator LandingPadInst::op_begin() const { return OperandTraits<LandingPadInst>::op_begin(const_cast< LandingPadInst*>(this)); } LandingPadInst::op_iterator LandingPadInst ::op_end() { return OperandTraits<LandingPadInst>::op_end (this); } LandingPadInst::const_op_iterator LandingPadInst::op_end () const { return OperandTraits<LandingPadInst>::op_end (const_cast<LandingPadInst*>(this)); } Value *LandingPadInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<LandingPadInst>::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<LandingPadInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2885, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<LandingPadInst>::op_begin(const_cast< LandingPadInst*>(this))[i_nocapture].get()); } void LandingPadInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<LandingPadInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<LandingPadInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2885, __PRETTY_FUNCTION__)); OperandTraits<LandingPadInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned LandingPadInst::getNumOperands() const { return OperandTraits <LandingPadInst>::operands(this); } template <int Idx_nocapture > Use &LandingPadInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &LandingPadInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } |
2886 | |
2887 | //===----------------------------------------------------------------------===// |
2888 | // ReturnInst Class |
2889 | //===----------------------------------------------------------------------===// |
2890 | |
2891 | //===--------------------------------------------------------------------------- |
2892 | /// Return a value (possibly void), from a function. Execution |
2893 | /// does not continue in this function any longer. |
2894 | /// |
2895 | class ReturnInst : public Instruction { |
2896 | ReturnInst(const ReturnInst &RI); |
2897 | |
2898 | private: |
2899 | // ReturnInst constructors: |
2900 | // ReturnInst() - 'ret void' instruction |
2901 | // ReturnInst( null) - 'ret void' instruction |
2902 | // ReturnInst(Value* X) - 'ret X' instruction |
2903 | // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I |
2904 | // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I |
2905 | // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B |
2906 | // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B |
2907 | // |
2908 | // NOTE: If the Value* passed is of type void then the constructor behaves as |
2909 | // if it was passed NULL. |
2910 | explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr, |
2911 | Instruction *InsertBefore = nullptr); |
2912 | ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd); |
2913 | explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd); |
2914 | |
2915 | protected: |
2916 | // Note: Instruction needs to be a friend here to call cloneImpl. |
2917 | friend class Instruction; |
2918 | |
2919 | ReturnInst *cloneImpl() const; |
2920 | |
2921 | public: |
2922 | static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr, |
2923 | Instruction *InsertBefore = nullptr) { |
2924 | return new(!!retVal) ReturnInst(C, retVal, InsertBefore); |
2925 | } |
2926 | |
2927 | static ReturnInst* Create(LLVMContext &C, Value *retVal, |
2928 | BasicBlock *InsertAtEnd) { |
2929 | return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd); |
2930 | } |
2931 | |
2932 | static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) { |
2933 | return new(0) ReturnInst(C, InsertAtEnd); |
2934 | } |
2935 | |
2936 | /// Provide fast operand accessors |
2937 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
2938 | |
2939 | /// Convenience accessor. Returns null if there is no return value. |
2940 | Value *getReturnValue() const { |
2941 | return getNumOperands() != 0 ? getOperand(0) : nullptr; |
2942 | } |
2943 | |
2944 | unsigned getNumSuccessors() const { return 0; } |
2945 | |
2946 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
2947 | static bool classof(const Instruction *I) { |
2948 | return (I->getOpcode() == Instruction::Ret); |
2949 | } |
2950 | static bool classof(const Value *V) { |
2951 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
2952 | } |
2953 | |
2954 | private: |
2955 | BasicBlock *getSuccessor(unsigned idx) const { |
2956 | llvm_unreachable("ReturnInst has no successors!")::llvm::llvm_unreachable_internal("ReturnInst has no successors!" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2956); |
2957 | } |
2958 | |
2959 | void setSuccessor(unsigned idx, BasicBlock *B) { |
2960 | llvm_unreachable("ReturnInst has no successors!")::llvm::llvm_unreachable_internal("ReturnInst has no successors!" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2960); |
2961 | } |
2962 | }; |
2963 | |
2964 | template <> |
2965 | struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> { |
2966 | }; |
2967 | |
2968 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)ReturnInst::op_iterator ReturnInst::op_begin() { return OperandTraits <ReturnInst>::op_begin(this); } ReturnInst::const_op_iterator ReturnInst::op_begin() const { return OperandTraits<ReturnInst >::op_begin(const_cast<ReturnInst*>(this)); } ReturnInst ::op_iterator ReturnInst::op_end() { return OperandTraits< ReturnInst>::op_end(this); } ReturnInst::const_op_iterator ReturnInst::op_end() const { return OperandTraits<ReturnInst >::op_end(const_cast<ReturnInst*>(this)); } Value *ReturnInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<ReturnInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<ReturnInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2968, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<ReturnInst>::op_begin(const_cast<ReturnInst *>(this))[i_nocapture].get()); } void ReturnInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<ReturnInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<ReturnInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 2968, __PRETTY_FUNCTION__)); OperandTraits<ReturnInst> ::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned ReturnInst ::getNumOperands() const { return OperandTraits<ReturnInst >::operands(this); } template <int Idx_nocapture> Use &ReturnInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & ReturnInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } |
2969 | |
2970 | //===----------------------------------------------------------------------===// |
2971 | // BranchInst Class |
2972 | //===----------------------------------------------------------------------===// |
2973 | |
2974 | //===--------------------------------------------------------------------------- |
2975 | /// Conditional or Unconditional Branch instruction. |
2976 | /// |
2977 | class BranchInst : public Instruction { |
2978 | /// Ops list - Branches are strange. The operands are ordered: |
2979 | /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because |
2980 | /// they don't have to check for cond/uncond branchness. These are mostly |
2981 | /// accessed relative from op_end(). |
2982 | BranchInst(const BranchInst &BI); |
2983 | // BranchInst constructors (where {B, T, F} are blocks, and C is a condition): |
2984 | // BranchInst(BB *B) - 'br B' |
2985 | // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F' |
2986 | // BranchInst(BB* B, Inst *I) - 'br B' insert before I |
2987 | // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I |
2988 | // BranchInst(BB* B, BB *I) - 'br B' insert at end |
2989 | // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end |
2990 | explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr); |
2991 | BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
2992 | Instruction *InsertBefore = nullptr); |
2993 | BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd); |
2994 | BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
2995 | BasicBlock *InsertAtEnd); |
2996 | |
2997 | void AssertOK(); |
2998 | |
2999 | protected: |
3000 | // Note: Instruction needs to be a friend here to call cloneImpl. |
3001 | friend class Instruction; |
3002 | |
3003 | BranchInst *cloneImpl() const; |
3004 | |
3005 | public: |
3006 | /// Iterator type that casts an operand to a basic block. |
3007 | /// |
3008 | /// This only makes sense because the successors are stored as adjacent |
3009 | /// operands for branch instructions. |
3010 | struct succ_op_iterator |
3011 | : iterator_adaptor_base<succ_op_iterator, value_op_iterator, |
3012 | std::random_access_iterator_tag, BasicBlock *, |
3013 | ptrdiff_t, BasicBlock *, BasicBlock *> { |
3014 | explicit succ_op_iterator(value_op_iterator I) : iterator_adaptor_base(I) {} |
3015 | |
3016 | BasicBlock *operator*() const { return cast<BasicBlock>(*I); } |
3017 | BasicBlock *operator->() const { return operator*(); } |
3018 | }; |
3019 | |
3020 | /// The const version of `succ_op_iterator`. |
3021 | struct const_succ_op_iterator |
3022 | : iterator_adaptor_base<const_succ_op_iterator, const_value_op_iterator, |
3023 | std::random_access_iterator_tag, |
3024 | const BasicBlock *, ptrdiff_t, const BasicBlock *, |
3025 | const BasicBlock *> { |
3026 | explicit const_succ_op_iterator(const_value_op_iterator I) |
3027 | : iterator_adaptor_base(I) {} |
3028 | |
3029 | const BasicBlock *operator*() const { return cast<BasicBlock>(*I); } |
3030 | const BasicBlock *operator->() const { return operator*(); } |
3031 | }; |
3032 | |
3033 | static BranchInst *Create(BasicBlock *IfTrue, |
3034 | Instruction *InsertBefore = nullptr) { |
3035 | return new(1) BranchInst(IfTrue, InsertBefore); |
3036 | } |
3037 | |
3038 | static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, |
3039 | Value *Cond, Instruction *InsertBefore = nullptr) { |
3040 | return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore); |
3041 | } |
3042 | |
3043 | static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) { |
3044 | return new(1) BranchInst(IfTrue, InsertAtEnd); |
3045 | } |
3046 | |
3047 | static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, |
3048 | Value *Cond, BasicBlock *InsertAtEnd) { |
3049 | return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd); |
3050 | } |
3051 | |
3052 | /// Transparently provide more efficient getOperand methods. |
3053 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
3054 | |
3055 | bool isUnconditional() const { return getNumOperands() == 1; } |
3056 | bool isConditional() const { return getNumOperands() == 3; } |
3057 | |
3058 | Value *getCondition() const { |
3059 | assert(isConditional() && "Cannot get condition of an uncond branch!")((isConditional() && "Cannot get condition of an uncond branch!" ) ? static_cast<void> (0) : __assert_fail ("isConditional() && \"Cannot get condition of an uncond branch!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3059, __PRETTY_FUNCTION__)); |
3060 | return Op<-3>(); |
3061 | } |
3062 | |
3063 | void setCondition(Value *V) { |
3064 | assert(isConditional() && "Cannot set condition of unconditional branch!")((isConditional() && "Cannot set condition of unconditional branch!" ) ? static_cast<void> (0) : __assert_fail ("isConditional() && \"Cannot set condition of unconditional branch!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3064, __PRETTY_FUNCTION__)); |
3065 | Op<-3>() = V; |
3066 | } |
3067 | |
3068 | unsigned getNumSuccessors() const { return 1+isConditional(); } |
3069 | |
3070 | BasicBlock *getSuccessor(unsigned i) const { |
3071 | assert(i < getNumSuccessors() && "Successor # out of range for Branch!")((i < getNumSuccessors() && "Successor # out of range for Branch!" ) ? static_cast<void> (0) : __assert_fail ("i < getNumSuccessors() && \"Successor # out of range for Branch!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3071, __PRETTY_FUNCTION__)); |
3072 | return cast_or_null<BasicBlock>((&Op<-1>() - i)->get()); |
3073 | } |
3074 | |
3075 | void setSuccessor(unsigned idx, BasicBlock *NewSucc) { |
3076 | assert(idx < getNumSuccessors() && "Successor # out of range for Branch!")((idx < getNumSuccessors() && "Successor # out of range for Branch!" ) ? static_cast<void> (0) : __assert_fail ("idx < getNumSuccessors() && \"Successor # out of range for Branch!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3076, __PRETTY_FUNCTION__)); |
3077 | *(&Op<-1>() - idx) = NewSucc; |
3078 | } |
3079 | |
3080 | /// Swap the successors of this branch instruction. |
3081 | /// |
3082 | /// Swaps the successors of the branch instruction. This also swaps any |
3083 | /// branch weight metadata associated with the instruction so that it |
3084 | /// continues to map correctly to each operand. |
3085 | void swapSuccessors(); |
3086 | |
3087 | iterator_range<succ_op_iterator> successors() { |
3088 | return make_range( |
3089 | succ_op_iterator(std::next(value_op_begin(), isConditional() ? 1 : 0)), |
3090 | succ_op_iterator(value_op_end())); |
3091 | } |
3092 | |
3093 | iterator_range<const_succ_op_iterator> successors() const { |
3094 | return make_range(const_succ_op_iterator( |
3095 | std::next(value_op_begin(), isConditional() ? 1 : 0)), |
3096 | const_succ_op_iterator(value_op_end())); |
3097 | } |
3098 | |
3099 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
3100 | static bool classof(const Instruction *I) { |
3101 | return (I->getOpcode() == Instruction::Br); |
3102 | } |
3103 | static bool classof(const Value *V) { |
3104 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
3105 | } |
3106 | }; |
3107 | |
3108 | template <> |
3109 | struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> { |
3110 | }; |
3111 | |
3112 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)BranchInst::op_iterator BranchInst::op_begin() { return OperandTraits <BranchInst>::op_begin(this); } BranchInst::const_op_iterator BranchInst::op_begin() const { return OperandTraits<BranchInst >::op_begin(const_cast<BranchInst*>(this)); } BranchInst ::op_iterator BranchInst::op_end() { return OperandTraits< BranchInst>::op_end(this); } BranchInst::const_op_iterator BranchInst::op_end() const { return OperandTraits<BranchInst >::op_end(const_cast<BranchInst*>(this)); } Value *BranchInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<BranchInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<BranchInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3112, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<BranchInst>::op_begin(const_cast<BranchInst *>(this))[i_nocapture].get()); } void BranchInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<BranchInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<BranchInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3112, __PRETTY_FUNCTION__)); OperandTraits<BranchInst> ::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned BranchInst ::getNumOperands() const { return OperandTraits<BranchInst >::operands(this); } template <int Idx_nocapture> Use &BranchInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & BranchInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } |
3113 | |
3114 | //===----------------------------------------------------------------------===// |
3115 | // SwitchInst Class |
3116 | //===----------------------------------------------------------------------===// |
3117 | |
3118 | //===--------------------------------------------------------------------------- |
3119 | /// Multiway switch |
3120 | /// |
3121 | class SwitchInst : public Instruction { |
3122 | unsigned ReservedSpace; |
3123 | |
3124 | // Operand[0] = Value to switch on |
3125 | // Operand[1] = Default basic block destination |
3126 | // Operand[2n ] = Value to match |
3127 | // Operand[2n+1] = BasicBlock to go to on match |
3128 | SwitchInst(const SwitchInst &SI); |
3129 | |
3130 | /// Create a new switch instruction, specifying a value to switch on and a |
3131 | /// default destination. The number of additional cases can be specified here |
3132 | /// to make memory allocation more efficient. This constructor can also |
3133 | /// auto-insert before another instruction. |
3134 | SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
3135 | Instruction *InsertBefore); |
3136 | |
3137 | /// Create a new switch instruction, specifying a value to switch on and a |
3138 | /// default destination. The number of additional cases can be specified here |
3139 | /// to make memory allocation more efficient. This constructor also |
3140 | /// auto-inserts at the end of the specified BasicBlock. |
3141 | SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
3142 | BasicBlock *InsertAtEnd); |
3143 | |
3144 | // allocate space for exactly zero operands |
3145 | void *operator new(size_t s) { |
3146 | return User::operator new(s); |
3147 | } |
3148 | |
3149 | void init(Value *Value, BasicBlock *Default, unsigned NumReserved); |
3150 | void growOperands(); |
3151 | |
3152 | protected: |
3153 | // Note: Instruction needs to be a friend here to call cloneImpl. |
3154 | friend class Instruction; |
3155 | |
3156 | SwitchInst *cloneImpl() const; |
3157 | |
3158 | public: |
3159 | // -2 |
3160 | static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1); |
3161 | |
3162 | template <typename CaseHandleT> class CaseIteratorImpl; |
3163 | |
3164 | /// A handle to a particular switch case. It exposes a convenient interface |
3165 | /// to both the case value and the successor block. |
3166 | /// |
3167 | /// We define this as a template and instantiate it to form both a const and |
3168 | /// non-const handle. |
3169 | template <typename SwitchInstT, typename ConstantIntT, typename BasicBlockT> |
3170 | class CaseHandleImpl { |
3171 | // Directly befriend both const and non-const iterators. |
3172 | friend class SwitchInst::CaseIteratorImpl< |
3173 | CaseHandleImpl<SwitchInstT, ConstantIntT, BasicBlockT>>; |
3174 | |
3175 | protected: |
3176 | // Expose the switch type we're parameterized with to the iterator. |
3177 | using SwitchInstType = SwitchInstT; |
3178 | |
3179 | SwitchInstT *SI; |
3180 | ptrdiff_t Index; |
3181 | |
3182 | CaseHandleImpl() = default; |
3183 | CaseHandleImpl(SwitchInstT *SI, ptrdiff_t Index) : SI(SI), Index(Index) {} |
3184 | |
3185 | public: |
3186 | /// Resolves case value for current case. |
3187 | ConstantIntT *getCaseValue() const { |
3188 | assert((unsigned)Index < SI->getNumCases() &&(((unsigned)Index < SI->getNumCases() && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3189, __PRETTY_FUNCTION__)) |
3189 | "Index out the number of cases.")(((unsigned)Index < SI->getNumCases() && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3189, __PRETTY_FUNCTION__)); |
3190 | return reinterpret_cast<ConstantIntT *>(SI->getOperand(2 + Index * 2)); |
3191 | } |
3192 | |
3193 | /// Resolves successor for current case. |
3194 | BasicBlockT *getCaseSuccessor() const { |
3195 | assert(((unsigned)Index < SI->getNumCases() ||((((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3197, __PRETTY_FUNCTION__)) |
3196 | (unsigned)Index == DefaultPseudoIndex) &&((((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3197, __PRETTY_FUNCTION__)) |
3197 | "Index out the number of cases.")((((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3197, __PRETTY_FUNCTION__)); |
3198 | return SI->getSuccessor(getSuccessorIndex()); |
3199 | } |
3200 | |
3201 | /// Returns number of current case. |
3202 | unsigned getCaseIndex() const { return Index; } |
3203 | |
3204 | /// Returns successor index for current case successor. |
3205 | unsigned getSuccessorIndex() const { |
3206 | assert(((unsigned)Index == DefaultPseudoIndex ||((((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3208, __PRETTY_FUNCTION__)) |
3207 | (unsigned)Index < SI->getNumCases()) &&((((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3208, __PRETTY_FUNCTION__)) |
3208 | "Index out the number of cases.")((((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3208, __PRETTY_FUNCTION__)); |
3209 | return (unsigned)Index != DefaultPseudoIndex ? Index + 1 : 0; |
3210 | } |
3211 | |
3212 | bool operator==(const CaseHandleImpl &RHS) const { |
3213 | assert(SI == RHS.SI && "Incompatible operators.")((SI == RHS.SI && "Incompatible operators.") ? static_cast <void> (0) : __assert_fail ("SI == RHS.SI && \"Incompatible operators.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3213, __PRETTY_FUNCTION__)); |
3214 | return Index == RHS.Index; |
3215 | } |
3216 | }; |
3217 | |
3218 | using ConstCaseHandle = |
3219 | CaseHandleImpl<const SwitchInst, const ConstantInt, const BasicBlock>; |
3220 | |
3221 | class CaseHandle |
3222 | : public CaseHandleImpl<SwitchInst, ConstantInt, BasicBlock> { |
3223 | friend class SwitchInst::CaseIteratorImpl<CaseHandle>; |
3224 | |
3225 | public: |
3226 | CaseHandle(SwitchInst *SI, ptrdiff_t Index) : CaseHandleImpl(SI, Index) {} |
3227 | |
3228 | /// Sets the new value for current case. |
3229 | void setValue(ConstantInt *V) { |
3230 | assert((unsigned)Index < SI->getNumCases() &&(((unsigned)Index < SI->getNumCases() && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3231, __PRETTY_FUNCTION__)) |
3231 | "Index out the number of cases.")(((unsigned)Index < SI->getNumCases() && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3231, __PRETTY_FUNCTION__)); |
3232 | SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V)); |
3233 | } |
3234 | |
3235 | /// Sets the new successor for current case. |
3236 | void setSuccessor(BasicBlock *S) { |
3237 | SI->setSuccessor(getSuccessorIndex(), S); |
3238 | } |
3239 | }; |
3240 | |
3241 | template <typename CaseHandleT> |
3242 | class CaseIteratorImpl |
3243 | : public iterator_facade_base<CaseIteratorImpl<CaseHandleT>, |
3244 | std::random_access_iterator_tag, |
3245 | CaseHandleT> { |
3246 | using SwitchInstT = typename CaseHandleT::SwitchInstType; |
3247 | |
3248 | CaseHandleT Case; |
3249 | |
3250 | public: |
3251 | /// Default constructed iterator is in an invalid state until assigned to |
3252 | /// a case for a particular switch. |
3253 | CaseIteratorImpl() = default; |
3254 | |
3255 | /// Initializes case iterator for given SwitchInst and for given |
3256 | /// case number. |
3257 | CaseIteratorImpl(SwitchInstT *SI, unsigned CaseNum) : Case(SI, CaseNum) {} |
3258 | |
3259 | /// Initializes case iterator for given SwitchInst and for given |
3260 | /// successor index. |
3261 | static CaseIteratorImpl fromSuccessorIndex(SwitchInstT *SI, |
3262 | unsigned SuccessorIndex) { |
3263 | assert(SuccessorIndex < SI->getNumSuccessors() &&((SuccessorIndex < SI->getNumSuccessors() && "Successor index # out of range!" ) ? static_cast<void> (0) : __assert_fail ("SuccessorIndex < SI->getNumSuccessors() && \"Successor index # out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3264, __PRETTY_FUNCTION__)) |
3264 | "Successor index # out of range!")((SuccessorIndex < SI->getNumSuccessors() && "Successor index # out of range!" ) ? static_cast<void> (0) : __assert_fail ("SuccessorIndex < SI->getNumSuccessors() && \"Successor index # out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3264, __PRETTY_FUNCTION__)); |
3265 | return SuccessorIndex != 0 ? CaseIteratorImpl(SI, SuccessorIndex - 1) |
3266 | : CaseIteratorImpl(SI, DefaultPseudoIndex); |
3267 | } |
3268 | |
3269 | /// Support converting to the const variant. This will be a no-op for const |
3270 | /// variant. |
3271 | operator CaseIteratorImpl<ConstCaseHandle>() const { |
3272 | return CaseIteratorImpl<ConstCaseHandle>(Case.SI, Case.Index); |
3273 | } |
3274 | |
3275 | CaseIteratorImpl &operator+=(ptrdiff_t N) { |
3276 | // Check index correctness after addition. |
3277 | // Note: Index == getNumCases() means end(). |
3278 | assert(Case.Index + N >= 0 &&((Case.Index + N >= 0 && (unsigned)(Case.Index + N ) <= Case.SI->getNumCases() && "Case.Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("Case.Index + N >= 0 && (unsigned)(Case.Index + N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3280, __PRETTY_FUNCTION__)) |
3279 | (unsigned)(Case.Index + N) <= Case.SI->getNumCases() &&((Case.Index + N >= 0 && (unsigned)(Case.Index + N ) <= Case.SI->getNumCases() && "Case.Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("Case.Index + N >= 0 && (unsigned)(Case.Index + N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3280, __PRETTY_FUNCTION__)) |
3280 | "Case.Index out the number of cases.")((Case.Index + N >= 0 && (unsigned)(Case.Index + N ) <= Case.SI->getNumCases() && "Case.Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("Case.Index + N >= 0 && (unsigned)(Case.Index + N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3280, __PRETTY_FUNCTION__)); |
3281 | Case.Index += N; |
3282 | return *this; |
3283 | } |
3284 | CaseIteratorImpl &operator-=(ptrdiff_t N) { |
3285 | // Check index correctness after subtraction. |
3286 | // Note: Case.Index == getNumCases() means end(). |
3287 | assert(Case.Index - N >= 0 &&((Case.Index - N >= 0 && (unsigned)(Case.Index - N ) <= Case.SI->getNumCases() && "Case.Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("Case.Index - N >= 0 && (unsigned)(Case.Index - N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3289, __PRETTY_FUNCTION__)) |
3288 | (unsigned)(Case.Index - N) <= Case.SI->getNumCases() &&((Case.Index - N >= 0 && (unsigned)(Case.Index - N ) <= Case.SI->getNumCases() && "Case.Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("Case.Index - N >= 0 && (unsigned)(Case.Index - N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3289, __PRETTY_FUNCTION__)) |
3289 | "Case.Index out the number of cases.")((Case.Index - N >= 0 && (unsigned)(Case.Index - N ) <= Case.SI->getNumCases() && "Case.Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("Case.Index - N >= 0 && (unsigned)(Case.Index - N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3289, __PRETTY_FUNCTION__)); |
3290 | Case.Index -= N; |
3291 | return *this; |
3292 | } |
3293 | ptrdiff_t operator-(const CaseIteratorImpl &RHS) const { |
3294 | assert(Case.SI == RHS.Case.SI && "Incompatible operators.")((Case.SI == RHS.Case.SI && "Incompatible operators." ) ? static_cast<void> (0) : __assert_fail ("Case.SI == RHS.Case.SI && \"Incompatible operators.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3294, __PRETTY_FUNCTION__)); |
3295 | return Case.Index - RHS.Case.Index; |
3296 | } |
3297 | bool operator==(const CaseIteratorImpl &RHS) const { |
3298 | return Case == RHS.Case; |
3299 | } |
3300 | bool operator<(const CaseIteratorImpl &RHS) const { |
3301 | assert(Case.SI == RHS.Case.SI && "Incompatible operators.")((Case.SI == RHS.Case.SI && "Incompatible operators." ) ? static_cast<void> (0) : __assert_fail ("Case.SI == RHS.Case.SI && \"Incompatible operators.\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3301, __PRETTY_FUNCTION__)); |
3302 | return Case.Index < RHS.Case.Index; |
3303 | } |
3304 | CaseHandleT &operator*() { return Case; } |
3305 | const CaseHandleT &operator*() const { return Case; } |
3306 | }; |
3307 | |
3308 | using CaseIt = CaseIteratorImpl<CaseHandle>; |
3309 | using ConstCaseIt = CaseIteratorImpl<ConstCaseHandle>; |
3310 | |
3311 | static SwitchInst *Create(Value *Value, BasicBlock *Default, |
3312 | unsigned NumCases, |
3313 | Instruction *InsertBefore = nullptr) { |
3314 | return new SwitchInst(Value, Default, NumCases, InsertBefore); |
3315 | } |
3316 | |
3317 | static SwitchInst *Create(Value *Value, BasicBlock *Default, |
3318 | unsigned NumCases, BasicBlock *InsertAtEnd) { |
3319 | return new SwitchInst(Value, Default, NumCases, InsertAtEnd); |
3320 | } |
3321 | |
3322 | /// Provide fast operand accessors |
3323 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
3324 | |
3325 | // Accessor Methods for Switch stmt |
3326 | Value *getCondition() const { return getOperand(0); } |
3327 | void setCondition(Value *V) { setOperand(0, V); } |
3328 | |
3329 | BasicBlock *getDefaultDest() const { |
3330 | return cast<BasicBlock>(getOperand(1)); |
3331 | } |
3332 | |
3333 | void setDefaultDest(BasicBlock *DefaultCase) { |
3334 | setOperand(1, reinterpret_cast<Value*>(DefaultCase)); |
3335 | } |
3336 | |
3337 | /// Return the number of 'cases' in this switch instruction, excluding the |
3338 | /// default case. |
3339 | unsigned getNumCases() const { |
3340 | return getNumOperands()/2 - 1; |
3341 | } |
3342 | |
3343 | /// Returns a read/write iterator that points to the first case in the |
3344 | /// SwitchInst. |
3345 | CaseIt case_begin() { |
3346 | return CaseIt(this, 0); |
3347 | } |
3348 | |
3349 | /// Returns a read-only iterator that points to the first case in the |
3350 | /// SwitchInst. |
3351 | ConstCaseIt case_begin() const { |
3352 | return ConstCaseIt(this, 0); |
3353 | } |
3354 | |
3355 | /// Returns a read/write iterator that points one past the last in the |
3356 | /// SwitchInst. |
3357 | CaseIt case_end() { |
3358 | return CaseIt(this, getNumCases()); |
3359 | } |
3360 | |
3361 | /// Returns a read-only iterator that points one past the last in the |
3362 | /// SwitchInst. |
3363 | ConstCaseIt case_end() const { |
3364 | return ConstCaseIt(this, getNumCases()); |
3365 | } |
3366 | |
3367 | /// Iteration adapter for range-for loops. |
3368 | iterator_range<CaseIt> cases() { |
3369 | return make_range(case_begin(), case_end()); |
3370 | } |
3371 | |
3372 | /// Constant iteration adapter for range-for loops. |
3373 | iterator_range<ConstCaseIt> cases() const { |
3374 | return make_range(case_begin(), case_end()); |
3375 | } |
3376 | |
3377 | /// Returns an iterator that points to the default case. |
3378 | /// Note: this iterator allows to resolve successor only. Attempt |
3379 | /// to resolve case value causes an assertion. |
3380 | /// Also note, that increment and decrement also causes an assertion and |
3381 | /// makes iterator invalid. |
3382 | CaseIt case_default() { |
3383 | return CaseIt(this, DefaultPseudoIndex); |
3384 | } |
3385 | ConstCaseIt case_default() const { |
3386 | return ConstCaseIt(this, DefaultPseudoIndex); |
3387 | } |
3388 | |
3389 | /// Search all of the case values for the specified constant. If it is |
3390 | /// explicitly handled, return the case iterator of it, otherwise return |
3391 | /// default case iterator to indicate that it is handled by the default |
3392 | /// handler. |
3393 | CaseIt findCaseValue(const ConstantInt *C) { |
3394 | CaseIt I = llvm::find_if( |
3395 | cases(), [C](CaseHandle &Case) { return Case.getCaseValue() == C; }); |
3396 | if (I != case_end()) |
3397 | return I; |
3398 | |
3399 | return case_default(); |
3400 | } |
3401 | ConstCaseIt findCaseValue(const ConstantInt *C) const { |
3402 | ConstCaseIt I = llvm::find_if(cases(), [C](ConstCaseHandle &Case) { |
3403 | return Case.getCaseValue() == C; |
3404 | }); |
3405 | if (I != case_end()) |
3406 | return I; |
3407 | |
3408 | return case_default(); |
3409 | } |
3410 | |
3411 | /// Finds the unique case value for a given successor. Returns null if the |
3412 | /// successor is not found, not unique, or is the default case. |
3413 | ConstantInt *findCaseDest(BasicBlock *BB) { |
3414 | if (BB == getDefaultDest()) |
3415 | return nullptr; |
3416 | |
3417 | ConstantInt *CI = nullptr; |
3418 | for (auto Case : cases()) { |
3419 | if (Case.getCaseSuccessor() != BB) |
3420 | continue; |
3421 | |
3422 | if (CI) |
3423 | return nullptr; // Multiple cases lead to BB. |
3424 | |
3425 | CI = Case.getCaseValue(); |
3426 | } |
3427 | |
3428 | return CI; |
3429 | } |
3430 | |
3431 | /// Add an entry to the switch instruction. |
3432 | /// Note: |
3433 | /// This action invalidates case_end(). Old case_end() iterator will |
3434 | /// point to the added case. |
3435 | void addCase(ConstantInt *OnVal, BasicBlock *Dest); |
3436 | |
3437 | /// This method removes the specified case and its successor from the switch |
3438 | /// instruction. Note that this operation may reorder the remaining cases at |
3439 | /// index idx and above. |
3440 | /// Note: |
3441 | /// This action invalidates iterators for all cases following the one removed, |
3442 | /// including the case_end() iterator. It returns an iterator for the next |
3443 | /// case. |
3444 | CaseIt removeCase(CaseIt I); |
3445 | |
3446 | unsigned getNumSuccessors() const { return getNumOperands()/2; } |
3447 | BasicBlock *getSuccessor(unsigned idx) const { |
3448 | assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!")((idx < getNumSuccessors() &&"Successor idx out of range for switch!" ) ? static_cast<void> (0) : __assert_fail ("idx < getNumSuccessors() &&\"Successor idx out of range for switch!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3448, __PRETTY_FUNCTION__)); |
3449 | return cast<BasicBlock>(getOperand(idx*2+1)); |
3450 | } |
3451 | void setSuccessor(unsigned idx, BasicBlock *NewSucc) { |
3452 | assert(idx < getNumSuccessors() && "Successor # out of range for switch!")((idx < getNumSuccessors() && "Successor # out of range for switch!" ) ? static_cast<void> (0) : __assert_fail ("idx < getNumSuccessors() && \"Successor # out of range for switch!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3452, __PRETTY_FUNCTION__)); |
3453 | setOperand(idx * 2 + 1, NewSucc); |
3454 | } |
3455 | |
3456 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
3457 | static bool classof(const Instruction *I) { |
3458 | return I->getOpcode() == Instruction::Switch; |
3459 | } |
3460 | static bool classof(const Value *V) { |
3461 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
3462 | } |
3463 | }; |
3464 | |
3465 | /// A wrapper class to simplify modification of SwitchInst cases along with |
3466 | /// their prof branch_weights metadata. |
3467 | class SwitchInstProfUpdateWrapper { |
3468 | SwitchInst &SI; |
3469 | Optional<SmallVector<uint32_t, 8> > Weights = None; |
3470 | bool Changed = false; |
3471 | |
3472 | protected: |
3473 | static MDNode *getProfBranchWeightsMD(const SwitchInst &SI); |
3474 | |
3475 | MDNode *buildProfBranchWeightsMD(); |
3476 | |
3477 | void init(); |
3478 | |
3479 | public: |
3480 | using CaseWeightOpt = Optional<uint32_t>; |
3481 | SwitchInst *operator->() { return &SI; } |
3482 | SwitchInst &operator*() { return SI; } |
3483 | operator SwitchInst *() { return &SI; } |
3484 | |
3485 | SwitchInstProfUpdateWrapper(SwitchInst &SI) : SI(SI) { init(); } |
3486 | |
3487 | ~SwitchInstProfUpdateWrapper() { |
3488 | if (Changed) |
3489 | SI.setMetadata(LLVMContext::MD_prof, buildProfBranchWeightsMD()); |
3490 | } |
3491 | |
3492 | /// Delegate the call to the underlying SwitchInst::removeCase() and remove |
3493 | /// correspondent branch weight. |
3494 | SwitchInst::CaseIt removeCase(SwitchInst::CaseIt I); |
3495 | |
3496 | /// Delegate the call to the underlying SwitchInst::addCase() and set the |
3497 | /// specified branch weight for the added case. |
3498 | void addCase(ConstantInt *OnVal, BasicBlock *Dest, CaseWeightOpt W); |
3499 | |
3500 | /// Delegate the call to the underlying SwitchInst::eraseFromParent() and mark |
3501 | /// this object to not touch the underlying SwitchInst in destructor. |
3502 | SymbolTableList<Instruction>::iterator eraseFromParent(); |
3503 | |
3504 | void setSuccessorWeight(unsigned idx, CaseWeightOpt W); |
3505 | CaseWeightOpt getSuccessorWeight(unsigned idx); |
3506 | |
3507 | static CaseWeightOpt getSuccessorWeight(const SwitchInst &SI, unsigned idx); |
3508 | }; |
3509 | |
3510 | template <> |
3511 | struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> { |
3512 | }; |
3513 | |
3514 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)SwitchInst::op_iterator SwitchInst::op_begin() { return OperandTraits <SwitchInst>::op_begin(this); } SwitchInst::const_op_iterator SwitchInst::op_begin() const { return OperandTraits<SwitchInst >::op_begin(const_cast<SwitchInst*>(this)); } SwitchInst ::op_iterator SwitchInst::op_end() { return OperandTraits< SwitchInst>::op_end(this); } SwitchInst::const_op_iterator SwitchInst::op_end() const { return OperandTraits<SwitchInst >::op_end(const_cast<SwitchInst*>(this)); } Value *SwitchInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<SwitchInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<SwitchInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3514, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<SwitchInst>::op_begin(const_cast<SwitchInst *>(this))[i_nocapture].get()); } void SwitchInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<SwitchInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<SwitchInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3514, __PRETTY_FUNCTION__)); OperandTraits<SwitchInst> ::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned SwitchInst ::getNumOperands() const { return OperandTraits<SwitchInst >::operands(this); } template <int Idx_nocapture> Use &SwitchInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & SwitchInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } |
3515 | |
3516 | //===----------------------------------------------------------------------===// |
3517 | // IndirectBrInst Class |
3518 | //===----------------------------------------------------------------------===// |
3519 | |
3520 | //===--------------------------------------------------------------------------- |
3521 | /// Indirect Branch Instruction. |
3522 | /// |
3523 | class IndirectBrInst : public Instruction { |
3524 | unsigned ReservedSpace; |
3525 | |
3526 | // Operand[0] = Address to jump to |
3527 | // Operand[n+1] = n-th destination |
3528 | IndirectBrInst(const IndirectBrInst &IBI); |
3529 | |
3530 | /// Create a new indirectbr instruction, specifying an |
3531 | /// Address to jump to. The number of expected destinations can be specified |
3532 | /// here to make memory allocation more efficient. This constructor can also |
3533 | /// autoinsert before another instruction. |
3534 | IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore); |
3535 | |
3536 | /// Create a new indirectbr instruction, specifying an |
3537 | /// Address to jump to. The number of expected destinations can be specified |
3538 | /// here to make memory allocation more efficient. This constructor also |
3539 | /// autoinserts at the end of the specified BasicBlock. |
3540 | IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd); |
3541 | |
3542 | // allocate space for exactly zero operands |
3543 | void *operator new(size_t s) { |
3544 | return User::operator new(s); |
3545 | } |
3546 | |
3547 | void init(Value *Address, unsigned NumDests); |
3548 | void growOperands(); |
3549 | |
3550 | protected: |
3551 | // Note: Instruction needs to be a friend here to call cloneImpl. |
3552 | friend class Instruction; |
3553 | |
3554 | IndirectBrInst *cloneImpl() const; |
3555 | |
3556 | public: |
3557 | /// Iterator type that casts an operand to a basic block. |
3558 | /// |
3559 | /// This only makes sense because the successors are stored as adjacent |
3560 | /// operands for indirectbr instructions. |
3561 | struct succ_op_iterator |
3562 | : iterator_adaptor_base<succ_op_iterator, value_op_iterator, |
3563 | std::random_access_iterator_tag, BasicBlock *, |
3564 | ptrdiff_t, BasicBlock *, BasicBlock *> { |
3565 | explicit succ_op_iterator(value_op_iterator I) : iterator_adaptor_base(I) {} |
3566 | |
3567 | BasicBlock *operator*() const { return cast<BasicBlock>(*I); } |
3568 | BasicBlock *operator->() const { return operator*(); } |
3569 | }; |
3570 | |
3571 | /// The const version of `succ_op_iterator`. |
3572 | struct const_succ_op_iterator |
3573 | : iterator_adaptor_base<const_succ_op_iterator, const_value_op_iterator, |
3574 | std::random_access_iterator_tag, |
3575 | const BasicBlock *, ptrdiff_t, const BasicBlock *, |
3576 | const BasicBlock *> { |
3577 | explicit const_succ_op_iterator(const_value_op_iterator I) |
3578 | : iterator_adaptor_base(I) {} |
3579 | |
3580 | const BasicBlock *operator*() const { return cast<BasicBlock>(*I); } |
3581 | const BasicBlock *operator->() const { return operator*(); } |
3582 | }; |
3583 | |
3584 | static IndirectBrInst *Create(Value *Address, unsigned NumDests, |
3585 | Instruction *InsertBefore = nullptr) { |
3586 | return new IndirectBrInst(Address, NumDests, InsertBefore); |
3587 | } |
3588 | |
3589 | static IndirectBrInst *Create(Value *Address, unsigned NumDests, |
3590 | BasicBlock *InsertAtEnd) { |
3591 | return new IndirectBrInst(Address, NumDests, InsertAtEnd); |
3592 | } |
3593 | |
3594 | /// Provide fast operand accessors. |
3595 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
3596 | |
3597 | // Accessor Methods for IndirectBrInst instruction. |
3598 | Value *getAddress() { return getOperand(0); } |
3599 | const Value *getAddress() const { return getOperand(0); } |
3600 | void setAddress(Value *V) { setOperand(0, V); } |
3601 | |
3602 | /// return the number of possible destinations in this |
3603 | /// indirectbr instruction. |
3604 | unsigned getNumDestinations() const { return getNumOperands()-1; } |
3605 | |
3606 | /// Return the specified destination. |
3607 | BasicBlock *getDestination(unsigned i) { return getSuccessor(i); } |
3608 | const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); } |
3609 | |
3610 | /// Add a destination. |
3611 | /// |
3612 | void addDestination(BasicBlock *Dest); |
3613 | |
3614 | /// This method removes the specified successor from the |
3615 | /// indirectbr instruction. |
3616 | void removeDestination(unsigned i); |
3617 | |
3618 | unsigned getNumSuccessors() const { return getNumOperands()-1; } |
3619 | BasicBlock *getSuccessor(unsigned i) const { |
3620 | return cast<BasicBlock>(getOperand(i+1)); |
3621 | } |
3622 | void setSuccessor(unsigned i, BasicBlock *NewSucc) { |
3623 | setOperand(i + 1, NewSucc); |
3624 | } |
3625 | |
3626 | iterator_range<succ_op_iterator> successors() { |
3627 | return make_range(succ_op_iterator(std::next(value_op_begin())), |
3628 | succ_op_iterator(value_op_end())); |
3629 | } |
3630 | |
3631 | iterator_range<const_succ_op_iterator> successors() const { |
3632 | return make_range(const_succ_op_iterator(std::next(value_op_begin())), |
3633 | const_succ_op_iterator(value_op_end())); |
3634 | } |
3635 | |
3636 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
3637 | static bool classof(const Instruction *I) { |
3638 | return I->getOpcode() == Instruction::IndirectBr; |
3639 | } |
3640 | static bool classof(const Value *V) { |
3641 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
3642 | } |
3643 | }; |
3644 | |
3645 | template <> |
3646 | struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> { |
3647 | }; |
3648 | |
3649 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)IndirectBrInst::op_iterator IndirectBrInst::op_begin() { return OperandTraits<IndirectBrInst>::op_begin(this); } IndirectBrInst ::const_op_iterator IndirectBrInst::op_begin() const { return OperandTraits<IndirectBrInst>::op_begin(const_cast< IndirectBrInst*>(this)); } IndirectBrInst::op_iterator IndirectBrInst ::op_end() { return OperandTraits<IndirectBrInst>::op_end (this); } IndirectBrInst::const_op_iterator IndirectBrInst::op_end () const { return OperandTraits<IndirectBrInst>::op_end (const_cast<IndirectBrInst*>(this)); } Value *IndirectBrInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<IndirectBrInst>::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<IndirectBrInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3649, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<IndirectBrInst>::op_begin(const_cast< IndirectBrInst*>(this))[i_nocapture].get()); } void IndirectBrInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<IndirectBrInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<IndirectBrInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3649, __PRETTY_FUNCTION__)); OperandTraits<IndirectBrInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned IndirectBrInst::getNumOperands() const { return OperandTraits <IndirectBrInst>::operands(this); } template <int Idx_nocapture > Use &IndirectBrInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &IndirectBrInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } |
3650 | |
3651 | //===----------------------------------------------------------------------===// |
3652 | // InvokeInst Class |
3653 | //===----------------------------------------------------------------------===// |
3654 | |
3655 | /// Invoke instruction. The SubclassData field is used to hold the |
3656 | /// calling convention of the call. |
3657 | /// |
3658 | class InvokeInst : public CallBase { |
3659 | /// The number of operands for this call beyond the called function, |
3660 | /// arguments, and operand bundles. |
3661 | static constexpr int NumExtraOperands = 2; |
3662 | |
3663 | /// The index from the end of the operand array to the normal destination. |
3664 | static constexpr int NormalDestOpEndIdx = -3; |
3665 | |
3666 | /// The index from the end of the operand array to the unwind destination. |
3667 | static constexpr int UnwindDestOpEndIdx = -2; |
3668 | |
3669 | InvokeInst(const InvokeInst &BI); |
3670 | |
3671 | /// Construct an InvokeInst given a range of arguments. |
3672 | /// |
3673 | /// Construct an InvokeInst from a range of arguments |
3674 | inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
3675 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3676 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
3677 | const Twine &NameStr, Instruction *InsertBefore); |
3678 | |
3679 | inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
3680 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3681 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
3682 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
3683 | |
3684 | void init(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
3685 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3686 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr); |
3687 | |
3688 | /// Compute the number of operands to allocate. |
3689 | static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) { |
3690 | // We need one operand for the called function, plus our extra operands and |
3691 | // the input operand counts provided. |
3692 | return 1 + NumExtraOperands + NumArgs + NumBundleInputs; |
3693 | } |
3694 | |
3695 | protected: |
3696 | // Note: Instruction needs to be a friend here to call cloneImpl. |
3697 | friend class Instruction; |
3698 | |
3699 | InvokeInst *cloneImpl() const; |
3700 | |
3701 | public: |
3702 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
3703 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3704 | const Twine &NameStr, |
3705 | Instruction *InsertBefore = nullptr) { |
3706 | int NumOperands = ComputeNumOperands(Args.size()); |
3707 | return new (NumOperands) |
3708 | InvokeInst(Ty, Func, IfNormal, IfException, Args, None, NumOperands, |
3709 | NameStr, InsertBefore); |
3710 | } |
3711 | |
3712 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
3713 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3714 | ArrayRef<OperandBundleDef> Bundles = None, |
3715 | const Twine &NameStr = "", |
3716 | Instruction *InsertBefore = nullptr) { |
3717 | int NumOperands = |
3718 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); |
3719 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); |
3720 | |
3721 | return new (NumOperands, DescriptorBytes) |
3722 | InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, NumOperands, |
3723 | NameStr, InsertBefore); |
3724 | } |
3725 | |
3726 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
3727 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3728 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
3729 | int NumOperands = ComputeNumOperands(Args.size()); |
3730 | return new (NumOperands) |
3731 | InvokeInst(Ty, Func, IfNormal, IfException, Args, None, NumOperands, |
3732 | NameStr, InsertAtEnd); |
3733 | } |
3734 | |
3735 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
3736 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3737 | ArrayRef<OperandBundleDef> Bundles, |
3738 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
3739 | int NumOperands = |
3740 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); |
3741 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); |
3742 | |
3743 | return new (NumOperands, DescriptorBytes) |
3744 | InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, NumOperands, |
3745 | NameStr, InsertAtEnd); |
3746 | } |
3747 | |
3748 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, |
3749 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3750 | const Twine &NameStr, |
3751 | Instruction *InsertBefore = nullptr) { |
3752 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, |
3753 | IfException, Args, None, NameStr, InsertBefore); |
3754 | } |
3755 | |
3756 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, |
3757 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3758 | ArrayRef<OperandBundleDef> Bundles = None, |
3759 | const Twine &NameStr = "", |
3760 | Instruction *InsertBefore = nullptr) { |
3761 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, |
3762 | IfException, Args, Bundles, NameStr, InsertBefore); |
3763 | } |
3764 | |
3765 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, |
3766 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3767 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
3768 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, |
3769 | IfException, Args, NameStr, InsertAtEnd); |
3770 | } |
3771 | |
3772 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, |
3773 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3774 | ArrayRef<OperandBundleDef> Bundles, |
3775 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
3776 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, |
3777 | IfException, Args, Bundles, NameStr, InsertAtEnd); |
3778 | } |
3779 | |
3780 | // Deprecated [opaque pointer types] |
3781 | static InvokeInst *Create(Value *Func, BasicBlock *IfNormal, |
3782 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3783 | const Twine &NameStr, |
3784 | Instruction *InsertBefore = nullptr) { |
3785 | return Create(cast<FunctionType>( |
3786 | cast<PointerType>(Func->getType())->getElementType()), |
3787 | Func, IfNormal, IfException, Args, None, NameStr, |
3788 | InsertBefore); |
3789 | } |
3790 | |
3791 | // Deprecated [opaque pointer types] |
3792 | static InvokeInst *Create(Value *Func, BasicBlock *IfNormal, |
3793 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3794 | ArrayRef<OperandBundleDef> Bundles = None, |
3795 | const Twine &NameStr = "", |
3796 | Instruction *InsertBefore = nullptr) { |
3797 | return Create(cast<FunctionType>( |
3798 | cast<PointerType>(Func->getType())->getElementType()), |
3799 | Func, IfNormal, IfException, Args, Bundles, NameStr, |
3800 | InsertBefore); |
3801 | } |
3802 | |
3803 | // Deprecated [opaque pointer types] |
3804 | static InvokeInst *Create(Value *Func, BasicBlock *IfNormal, |
3805 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3806 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
3807 | return Create(cast<FunctionType>( |
3808 | cast<PointerType>(Func->getType())->getElementType()), |
3809 | Func, IfNormal, IfException, Args, NameStr, InsertAtEnd); |
3810 | } |
3811 | |
3812 | // Deprecated [opaque pointer types] |
3813 | static InvokeInst *Create(Value *Func, BasicBlock *IfNormal, |
3814 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3815 | ArrayRef<OperandBundleDef> Bundles, |
3816 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
3817 | return Create(cast<FunctionType>( |
3818 | cast<PointerType>(Func->getType())->getElementType()), |
3819 | Func, IfNormal, IfException, Args, Bundles, NameStr, |
3820 | InsertAtEnd); |
3821 | } |
3822 | |
3823 | /// Create a clone of \p II with a different set of operand bundles and |
3824 | /// insert it before \p InsertPt. |
3825 | /// |
3826 | /// The returned invoke instruction is identical to \p II in every way except |
3827 | /// that the operand bundles for the new instruction are set to the operand |
3828 | /// bundles in \p Bundles. |
3829 | static InvokeInst *Create(InvokeInst *II, ArrayRef<OperandBundleDef> Bundles, |
3830 | Instruction *InsertPt = nullptr); |
3831 | |
3832 | /// Determine if the call should not perform indirect branch tracking. |
3833 | bool doesNoCfCheck() const { return hasFnAttr(Attribute::NoCfCheck); } |
3834 | |
3835 | /// Determine if the call cannot unwind. |
3836 | bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); } |
3837 | void setDoesNotThrow() { |
3838 | addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind); |
3839 | } |
3840 | |
3841 | // get*Dest - Return the destination basic blocks... |
3842 | BasicBlock *getNormalDest() const { |
3843 | return cast<BasicBlock>(Op<NormalDestOpEndIdx>()); |
3844 | } |
3845 | BasicBlock *getUnwindDest() const { |
3846 | return cast<BasicBlock>(Op<UnwindDestOpEndIdx>()); |
3847 | } |
3848 | void setNormalDest(BasicBlock *B) { |
3849 | Op<NormalDestOpEndIdx>() = reinterpret_cast<Value *>(B); |
3850 | } |
3851 | void setUnwindDest(BasicBlock *B) { |
3852 | Op<UnwindDestOpEndIdx>() = reinterpret_cast<Value *>(B); |
3853 | } |
3854 | |
3855 | /// Get the landingpad instruction from the landing pad |
3856 | /// block (the unwind destination). |
3857 | LandingPadInst *getLandingPadInst() const; |
3858 | |
3859 | BasicBlock *getSuccessor(unsigned i) const { |
3860 | assert(i < 2 && "Successor # out of range for invoke!")((i < 2 && "Successor # out of range for invoke!") ? static_cast<void> (0) : __assert_fail ("i < 2 && \"Successor # out of range for invoke!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3860, __PRETTY_FUNCTION__)); |
3861 | return i == 0 ? getNormalDest() : getUnwindDest(); |
3862 | } |
3863 | |
3864 | void setSuccessor(unsigned i, BasicBlock *NewSucc) { |
3865 | assert(i < 2 && "Successor # out of range for invoke!")((i < 2 && "Successor # out of range for invoke!") ? static_cast<void> (0) : __assert_fail ("i < 2 && \"Successor # out of range for invoke!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 3865, __PRETTY_FUNCTION__)); |
3866 | if (i == 0) |
3867 | setNormalDest(NewSucc); |
3868 | else |
3869 | setUnwindDest(NewSucc); |
3870 | } |
3871 | |
3872 | unsigned getNumSuccessors() const { return 2; } |
3873 | |
3874 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
3875 | static bool classof(const Instruction *I) { |
3876 | return (I->getOpcode() == Instruction::Invoke); |
3877 | } |
3878 | static bool classof(const Value *V) { |
3879 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
3880 | } |
3881 | |
3882 | private: |
3883 | |
3884 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
3885 | // method so that subclasses cannot accidentally use it. |
3886 | void setInstructionSubclassData(unsigned short D) { |
3887 | Instruction::setInstructionSubclassData(D); |
3888 | } |
3889 | }; |
3890 | |
3891 | InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
3892 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3893 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
3894 | const Twine &NameStr, Instruction *InsertBefore) |
3895 | : CallBase(Ty->getReturnType(), Instruction::Invoke, |
3896 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, |
3897 | InsertBefore) { |
3898 | init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr); |
3899 | } |
3900 | |
3901 | InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
3902 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3903 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
3904 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
3905 | : CallBase(Ty->getReturnType(), Instruction::Invoke, |
3906 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, |
3907 | InsertAtEnd) { |
3908 | init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr); |
3909 | } |
3910 | |
3911 | //===----------------------------------------------------------------------===// |
3912 | // CallBrInst Class |
3913 | //===----------------------------------------------------------------------===// |
3914 | |
3915 | /// CallBr instruction, tracking function calls that may not return control but |
3916 | /// instead transfer it to a third location. The SubclassData field is used to |
3917 | /// hold the calling convention of the call. |
3918 | /// |
3919 | class CallBrInst : public CallBase { |
3920 | |
3921 | unsigned NumIndirectDests; |
3922 | |
3923 | CallBrInst(const CallBrInst &BI); |
3924 | |
3925 | /// Construct a CallBrInst given a range of arguments. |
3926 | /// |
3927 | /// Construct a CallBrInst from a range of arguments |
3928 | inline CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, |
3929 | ArrayRef<BasicBlock *> IndirectDests, |
3930 | ArrayRef<Value *> Args, |
3931 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
3932 | const Twine &NameStr, Instruction *InsertBefore); |
3933 | |
3934 | inline CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, |
3935 | ArrayRef<BasicBlock *> IndirectDests, |
3936 | ArrayRef<Value *> Args, |
3937 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
3938 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
3939 | |
3940 | void init(FunctionType *FTy, Value *Func, BasicBlock *DefaultDest, |
3941 | ArrayRef<BasicBlock *> IndirectDests, ArrayRef<Value *> Args, |
3942 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr); |
3943 | |
3944 | /// Should the Indirect Destinations change, scan + update the Arg list. |
3945 | void updateArgBlockAddresses(unsigned i, BasicBlock *B); |
3946 | |
3947 | /// Compute the number of operands to allocate. |
3948 | static int ComputeNumOperands(int NumArgs, int NumIndirectDests, |
3949 | int NumBundleInputs = 0) { |
3950 | // We need one operand for the called function, plus our extra operands and |
3951 | // the input operand counts provided. |
3952 | return 2 + NumIndirectDests + NumArgs + NumBundleInputs; |
3953 | } |
3954 | |
3955 | protected: |
3956 | // Note: Instruction needs to be a friend here to call cloneImpl. |
3957 | friend class Instruction; |
3958 | |
3959 | CallBrInst *cloneImpl() const; |
3960 | |
3961 | public: |
3962 | static CallBrInst *Create(FunctionType *Ty, Value *Func, |
3963 | BasicBlock *DefaultDest, |
3964 | ArrayRef<BasicBlock *> IndirectDests, |
3965 | ArrayRef<Value *> Args, const Twine &NameStr, |
3966 | Instruction *InsertBefore = nullptr) { |
3967 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size()); |
3968 | return new (NumOperands) |
3969 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, None, |
3970 | NumOperands, NameStr, InsertBefore); |
3971 | } |
3972 | |
3973 | static CallBrInst *Create(FunctionType *Ty, Value *Func, |
3974 | BasicBlock *DefaultDest, |
3975 | ArrayRef<BasicBlock *> IndirectDests, |
3976 | ArrayRef<Value *> Args, |
3977 | ArrayRef<OperandBundleDef> Bundles = None, |
3978 | const Twine &NameStr = "", |
3979 | Instruction *InsertBefore = nullptr) { |
3980 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size(), |
3981 | CountBundleInputs(Bundles)); |
3982 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); |
3983 | |
3984 | return new (NumOperands, DescriptorBytes) |
3985 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, |
3986 | NumOperands, NameStr, InsertBefore); |
3987 | } |
3988 | |
3989 | static CallBrInst *Create(FunctionType *Ty, Value *Func, |
3990 | BasicBlock *DefaultDest, |
3991 | ArrayRef<BasicBlock *> IndirectDests, |
3992 | ArrayRef<Value *> Args, const Twine &NameStr, |
3993 | BasicBlock *InsertAtEnd) { |
3994 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size()); |
3995 | return new (NumOperands) |
3996 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, None, |
3997 | NumOperands, NameStr, InsertAtEnd); |
3998 | } |
3999 | |
4000 | static CallBrInst *Create(FunctionType *Ty, Value *Func, |
4001 | BasicBlock *DefaultDest, |
4002 | ArrayRef<BasicBlock *> IndirectDests, |
4003 | ArrayRef<Value *> Args, |
4004 | ArrayRef<OperandBundleDef> Bundles, |
4005 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
4006 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size(), |
4007 | CountBundleInputs(Bundles)); |
4008 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); |
4009 | |
4010 | return new (NumOperands, DescriptorBytes) |
4011 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, |
4012 | NumOperands, NameStr, InsertAtEnd); |
4013 | } |
4014 | |
4015 | static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest, |
4016 | ArrayRef<BasicBlock *> IndirectDests, |
4017 | ArrayRef<Value *> Args, const Twine &NameStr, |
4018 | Instruction *InsertBefore = nullptr) { |
4019 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, |
4020 | IndirectDests, Args, NameStr, InsertBefore); |
4021 | } |
4022 | |
4023 | static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest, |
4024 | ArrayRef<BasicBlock *> IndirectDests, |
4025 | ArrayRef<Value *> Args, |
4026 | ArrayRef<OperandBundleDef> Bundles = None, |
4027 | const Twine &NameStr = "", |
4028 | Instruction *InsertBefore = nullptr) { |
4029 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, |
4030 | IndirectDests, Args, Bundles, NameStr, InsertBefore); |
4031 | } |
4032 | |
4033 | static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest, |
4034 | ArrayRef<BasicBlock *> IndirectDests, |
4035 | ArrayRef<Value *> Args, const Twine &NameStr, |
4036 | BasicBlock *InsertAtEnd) { |
4037 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, |
4038 | IndirectDests, Args, NameStr, InsertAtEnd); |
4039 | } |
4040 | |
4041 | static CallBrInst *Create(FunctionCallee Func, |
4042 | BasicBlock *DefaultDest, |
4043 | ArrayRef<BasicBlock *> IndirectDests, |
4044 | ArrayRef<Value *> Args, |
4045 | ArrayRef<OperandBundleDef> Bundles, |
4046 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
4047 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, |
4048 | IndirectDests, Args, Bundles, NameStr, InsertAtEnd); |
4049 | } |
4050 | |
4051 | /// Create a clone of \p CBI with a different set of operand bundles and |
4052 | /// insert it before \p InsertPt. |
4053 | /// |
4054 | /// The returned callbr instruction is identical to \p CBI in every way |
4055 | /// except that the operand bundles for the new instruction are set to the |
4056 | /// operand bundles in \p Bundles. |
4057 | static CallBrInst *Create(CallBrInst *CBI, |
4058 | ArrayRef<OperandBundleDef> Bundles, |
4059 | Instruction *InsertPt = nullptr); |
4060 | |
4061 | /// Return the number of callbr indirect dest labels. |
4062 | /// |
4063 | unsigned getNumIndirectDests() const { return NumIndirectDests; } |
4064 | |
4065 | /// getIndirectDestLabel - Return the i-th indirect dest label. |
4066 | /// |
4067 | Value *getIndirectDestLabel(unsigned i) const { |
4068 | assert(i < getNumIndirectDests() && "Out of bounds!")((i < getNumIndirectDests() && "Out of bounds!") ? static_cast<void> (0) : __assert_fail ("i < getNumIndirectDests() && \"Out of bounds!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4068, __PRETTY_FUNCTION__)); |
4069 | return getOperand(i + getNumArgOperands() + getNumTotalBundleOperands() + |
4070 | 1); |
4071 | } |
4072 | |
4073 | Value *getIndirectDestLabelUse(unsigned i) const { |
4074 | assert(i < getNumIndirectDests() && "Out of bounds!")((i < getNumIndirectDests() && "Out of bounds!") ? static_cast<void> (0) : __assert_fail ("i < getNumIndirectDests() && \"Out of bounds!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4074, __PRETTY_FUNCTION__)); |
4075 | return getOperandUse(i + getNumArgOperands() + getNumTotalBundleOperands() + |
4076 | 1); |
4077 | } |
4078 | |
4079 | // Return the destination basic blocks... |
4080 | BasicBlock *getDefaultDest() const { |
4081 | return cast<BasicBlock>(*(&Op<-1>() - getNumIndirectDests() - 1)); |
4082 | } |
4083 | BasicBlock *getIndirectDest(unsigned i) const { |
4084 | return cast_or_null<BasicBlock>(*(&Op<-1>() - getNumIndirectDests() + i)); |
4085 | } |
4086 | SmallVector<BasicBlock *, 16> getIndirectDests() const { |
4087 | SmallVector<BasicBlock *, 16> IndirectDests; |
4088 | for (unsigned i = 0, e = getNumIndirectDests(); i < e; ++i) |
4089 | IndirectDests.push_back(getIndirectDest(i)); |
4090 | return IndirectDests; |
4091 | } |
4092 | void setDefaultDest(BasicBlock *B) { |
4093 | *(&Op<-1>() - getNumIndirectDests() - 1) = reinterpret_cast<Value *>(B); |
4094 | } |
4095 | void setIndirectDest(unsigned i, BasicBlock *B) { |
4096 | updateArgBlockAddresses(i, B); |
4097 | *(&Op<-1>() - getNumIndirectDests() + i) = reinterpret_cast<Value *>(B); |
4098 | } |
4099 | |
4100 | BasicBlock *getSuccessor(unsigned i) const { |
4101 | assert(i < getNumSuccessors() + 1 &&((i < getNumSuccessors() + 1 && "Successor # out of range for callbr!" ) ? static_cast<void> (0) : __assert_fail ("i < getNumSuccessors() + 1 && \"Successor # out of range for callbr!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4102, __PRETTY_FUNCTION__)) |
4102 | "Successor # out of range for callbr!")((i < getNumSuccessors() + 1 && "Successor # out of range for callbr!" ) ? static_cast<void> (0) : __assert_fail ("i < getNumSuccessors() + 1 && \"Successor # out of range for callbr!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4102, __PRETTY_FUNCTION__)); |
4103 | return i == 0 ? getDefaultDest() : getIndirectDest(i - 1); |
4104 | } |
4105 | |
4106 | void setSuccessor(unsigned i, BasicBlock *NewSucc) { |
4107 | assert(i < getNumIndirectDests() + 1 &&((i < getNumIndirectDests() + 1 && "Successor # out of range for callbr!" ) ? static_cast<void> (0) : __assert_fail ("i < getNumIndirectDests() + 1 && \"Successor # out of range for callbr!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4108, __PRETTY_FUNCTION__)) |
4108 | "Successor # out of range for callbr!")((i < getNumIndirectDests() + 1 && "Successor # out of range for callbr!" ) ? static_cast<void> (0) : __assert_fail ("i < getNumIndirectDests() + 1 && \"Successor # out of range for callbr!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4108, __PRETTY_FUNCTION__)); |
4109 | return i == 0 ? setDefaultDest(NewSucc) : setIndirectDest(i - 1, NewSucc); |
4110 | } |
4111 | |
4112 | unsigned getNumSuccessors() const { return getNumIndirectDests() + 1; } |
4113 | |
4114 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
4115 | static bool classof(const Instruction *I) { |
4116 | return (I->getOpcode() == Instruction::CallBr); |
4117 | } |
4118 | static bool classof(const Value *V) { |
4119 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4120 | } |
4121 | |
4122 | private: |
4123 | |
4124 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
4125 | // method so that subclasses cannot accidentally use it. |
4126 | void setInstructionSubclassData(unsigned short D) { |
4127 | Instruction::setInstructionSubclassData(D); |
4128 | } |
4129 | }; |
4130 | |
4131 | CallBrInst::CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, |
4132 | ArrayRef<BasicBlock *> IndirectDests, |
4133 | ArrayRef<Value *> Args, |
4134 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
4135 | const Twine &NameStr, Instruction *InsertBefore) |
4136 | : CallBase(Ty->getReturnType(), Instruction::CallBr, |
4137 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, |
4138 | InsertBefore) { |
4139 | init(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, NameStr); |
4140 | } |
4141 | |
4142 | CallBrInst::CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, |
4143 | ArrayRef<BasicBlock *> IndirectDests, |
4144 | ArrayRef<Value *> Args, |
4145 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
4146 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
4147 | : CallBase( |
4148 | cast<FunctionType>( |
4149 | cast<PointerType>(Func->getType())->getElementType()) |
4150 | ->getReturnType(), |
4151 | Instruction::CallBr, |
4152 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, |
4153 | InsertAtEnd) { |
4154 | init(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, NameStr); |
4155 | } |
4156 | |
4157 | //===----------------------------------------------------------------------===// |
4158 | // ResumeInst Class |
4159 | //===----------------------------------------------------------------------===// |
4160 | |
4161 | //===--------------------------------------------------------------------------- |
4162 | /// Resume the propagation of an exception. |
4163 | /// |
4164 | class ResumeInst : public Instruction { |
4165 | ResumeInst(const ResumeInst &RI); |
4166 | |
4167 | explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr); |
4168 | ResumeInst(Value *Exn, BasicBlock *InsertAtEnd); |
4169 | |
4170 | protected: |
4171 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4172 | friend class Instruction; |
4173 | |
4174 | ResumeInst *cloneImpl() const; |
4175 | |
4176 | public: |
4177 | static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) { |
4178 | return new(1) ResumeInst(Exn, InsertBefore); |
4179 | } |
4180 | |
4181 | static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) { |
4182 | return new(1) ResumeInst(Exn, InsertAtEnd); |
4183 | } |
4184 | |
4185 | /// Provide fast operand accessors |
4186 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
4187 | |
4188 | /// Convenience accessor. |
4189 | Value *getValue() const { return Op<0>(); } |
4190 | |
4191 | unsigned getNumSuccessors() const { return 0; } |
4192 | |
4193 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
4194 | static bool classof(const Instruction *I) { |
4195 | return I->getOpcode() == Instruction::Resume; |
4196 | } |
4197 | static bool classof(const Value *V) { |
4198 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4199 | } |
4200 | |
4201 | private: |
4202 | BasicBlock *getSuccessor(unsigned idx) const { |
4203 | llvm_unreachable("ResumeInst has no successors!")::llvm::llvm_unreachable_internal("ResumeInst has no successors!" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4203); |
4204 | } |
4205 | |
4206 | void setSuccessor(unsigned idx, BasicBlock *NewSucc) { |
4207 | llvm_unreachable("ResumeInst has no successors!")::llvm::llvm_unreachable_internal("ResumeInst has no successors!" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4207); |
4208 | } |
4209 | }; |
4210 | |
4211 | template <> |
4212 | struct OperandTraits<ResumeInst> : |
4213 | public FixedNumOperandTraits<ResumeInst, 1> { |
4214 | }; |
4215 | |
4216 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)ResumeInst::op_iterator ResumeInst::op_begin() { return OperandTraits <ResumeInst>::op_begin(this); } ResumeInst::const_op_iterator ResumeInst::op_begin() const { return OperandTraits<ResumeInst >::op_begin(const_cast<ResumeInst*>(this)); } ResumeInst ::op_iterator ResumeInst::op_end() { return OperandTraits< ResumeInst>::op_end(this); } ResumeInst::const_op_iterator ResumeInst::op_end() const { return OperandTraits<ResumeInst >::op_end(const_cast<ResumeInst*>(this)); } Value *ResumeInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<ResumeInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<ResumeInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4216, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<ResumeInst>::op_begin(const_cast<ResumeInst *>(this))[i_nocapture].get()); } void ResumeInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<ResumeInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<ResumeInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4216, __PRETTY_FUNCTION__)); OperandTraits<ResumeInst> ::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned ResumeInst ::getNumOperands() const { return OperandTraits<ResumeInst >::operands(this); } template <int Idx_nocapture> Use &ResumeInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & ResumeInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } |
4217 | |
4218 | //===----------------------------------------------------------------------===// |
4219 | // CatchSwitchInst Class |
4220 | //===----------------------------------------------------------------------===// |
4221 | class CatchSwitchInst : public Instruction { |
4222 | /// The number of operands actually allocated. NumOperands is |
4223 | /// the number actually in use. |
4224 | unsigned ReservedSpace; |
4225 | |
4226 | // Operand[0] = Outer scope |
4227 | // Operand[1] = Unwind block destination |
4228 | // Operand[n] = BasicBlock to go to on match |
4229 | CatchSwitchInst(const CatchSwitchInst &CSI); |
4230 | |
4231 | /// Create a new switch instruction, specifying a |
4232 | /// default destination. The number of additional handlers can be specified |
4233 | /// here to make memory allocation more efficient. |
4234 | /// This constructor can also autoinsert before another instruction. |
4235 | CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, |
4236 | unsigned NumHandlers, const Twine &NameStr, |
4237 | Instruction *InsertBefore); |
4238 | |
4239 | /// Create a new switch instruction, specifying a |
4240 | /// default destination. The number of additional handlers can be specified |
4241 | /// here to make memory allocation more efficient. |
4242 | /// This constructor also autoinserts at the end of the specified BasicBlock. |
4243 | CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, |
4244 | unsigned NumHandlers, const Twine &NameStr, |
4245 | BasicBlock *InsertAtEnd); |
4246 | |
4247 | // allocate space for exactly zero operands |
4248 | void *operator new(size_t s) { return User::operator new(s); } |
4249 | |
4250 | void init(Value *ParentPad, BasicBlock *UnwindDest, unsigned NumReserved); |
4251 | void growOperands(unsigned Size); |
4252 | |
4253 | protected: |
4254 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4255 | friend class Instruction; |
4256 | |
4257 | CatchSwitchInst *cloneImpl() const; |
4258 | |
4259 | public: |
4260 | static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest, |
4261 | unsigned NumHandlers, |
4262 | const Twine &NameStr = "", |
4263 | Instruction *InsertBefore = nullptr) { |
4264 | return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr, |
4265 | InsertBefore); |
4266 | } |
4267 | |
4268 | static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest, |
4269 | unsigned NumHandlers, const Twine &NameStr, |
4270 | BasicBlock *InsertAtEnd) { |
4271 | return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr, |
4272 | InsertAtEnd); |
4273 | } |
4274 | |
4275 | /// Provide fast operand accessors |
4276 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
4277 | |
4278 | // Accessor Methods for CatchSwitch stmt |
4279 | Value *getParentPad() const { return getOperand(0); } |
4280 | void setParentPad(Value *ParentPad) { setOperand(0, ParentPad); } |
4281 | |
4282 | // Accessor Methods for CatchSwitch stmt |
4283 | bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; } |
4284 | bool unwindsToCaller() const { return !hasUnwindDest(); } |
4285 | BasicBlock *getUnwindDest() const { |
4286 | if (hasUnwindDest()) |
4287 | return cast<BasicBlock>(getOperand(1)); |
4288 | return nullptr; |
4289 | } |
4290 | void setUnwindDest(BasicBlock *UnwindDest) { |
4291 | assert(UnwindDest)((UnwindDest) ? static_cast<void> (0) : __assert_fail ( "UnwindDest", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4291, __PRETTY_FUNCTION__)); |
4292 | assert(hasUnwindDest())((hasUnwindDest()) ? static_cast<void> (0) : __assert_fail ("hasUnwindDest()", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4292, __PRETTY_FUNCTION__)); |
4293 | setOperand(1, UnwindDest); |
4294 | } |
4295 | |
4296 | /// return the number of 'handlers' in this catchswitch |
4297 | /// instruction, except the default handler |
4298 | unsigned getNumHandlers() const { |
4299 | if (hasUnwindDest()) |
4300 | return getNumOperands() - 2; |
4301 | return getNumOperands() - 1; |
4302 | } |
4303 | |
4304 | private: |
4305 | static BasicBlock *handler_helper(Value *V) { return cast<BasicBlock>(V); } |
4306 | static const BasicBlock *handler_helper(const Value *V) { |
4307 | return cast<BasicBlock>(V); |
4308 | } |
4309 | |
4310 | public: |
4311 | using DerefFnTy = BasicBlock *(*)(Value *); |
4312 | using handler_iterator = mapped_iterator<op_iterator, DerefFnTy>; |
4313 | using handler_range = iterator_range<handler_iterator>; |
4314 | using ConstDerefFnTy = const BasicBlock *(*)(const Value *); |
4315 | using const_handler_iterator = |
4316 | mapped_iterator<const_op_iterator, ConstDerefFnTy>; |
4317 | using const_handler_range = iterator_range<const_handler_iterator>; |
4318 | |
4319 | /// Returns an iterator that points to the first handler in CatchSwitchInst. |
4320 | handler_iterator handler_begin() { |
4321 | op_iterator It = op_begin() + 1; |
4322 | if (hasUnwindDest()) |
4323 | ++It; |
4324 | return handler_iterator(It, DerefFnTy(handler_helper)); |
4325 | } |
4326 | |
4327 | /// Returns an iterator that points to the first handler in the |
4328 | /// CatchSwitchInst. |
4329 | const_handler_iterator handler_begin() const { |
4330 | const_op_iterator It = op_begin() + 1; |
4331 | if (hasUnwindDest()) |
4332 | ++It; |
4333 | return const_handler_iterator(It, ConstDerefFnTy(handler_helper)); |
4334 | } |
4335 | |
4336 | /// Returns a read-only iterator that points one past the last |
4337 | /// handler in the CatchSwitchInst. |
4338 | handler_iterator handler_end() { |
4339 | return handler_iterator(op_end(), DerefFnTy(handler_helper)); |
4340 | } |
4341 | |
4342 | /// Returns an iterator that points one past the last handler in the |
4343 | /// CatchSwitchInst. |
4344 | const_handler_iterator handler_end() const { |
4345 | return const_handler_iterator(op_end(), ConstDerefFnTy(handler_helper)); |
4346 | } |
4347 | |
4348 | /// iteration adapter for range-for loops. |
4349 | handler_range handlers() { |
4350 | return make_range(handler_begin(), handler_end()); |
4351 | } |
4352 | |
4353 | /// iteration adapter for range-for loops. |
4354 | const_handler_range handlers() const { |
4355 | return make_range(handler_begin(), handler_end()); |
4356 | } |
4357 | |
4358 | /// Add an entry to the switch instruction... |
4359 | /// Note: |
4360 | /// This action invalidates handler_end(). Old handler_end() iterator will |
4361 | /// point to the added handler. |
4362 | void addHandler(BasicBlock *Dest); |
4363 | |
4364 | void removeHandler(handler_iterator HI); |
4365 | |
4366 | unsigned getNumSuccessors() const { return getNumOperands() - 1; } |
4367 | BasicBlock *getSuccessor(unsigned Idx) const { |
4368 | assert(Idx < getNumSuccessors() &&((Idx < getNumSuccessors() && "Successor # out of range for catchswitch!" ) ? static_cast<void> (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchswitch!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4369, __PRETTY_FUNCTION__)) |
4369 | "Successor # out of range for catchswitch!")((Idx < getNumSuccessors() && "Successor # out of range for catchswitch!" ) ? static_cast<void> (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchswitch!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4369, __PRETTY_FUNCTION__)); |
4370 | return cast<BasicBlock>(getOperand(Idx + 1)); |
4371 | } |
4372 | void setSuccessor(unsigned Idx, BasicBlock *NewSucc) { |
4373 | assert(Idx < getNumSuccessors() &&((Idx < getNumSuccessors() && "Successor # out of range for catchswitch!" ) ? static_cast<void> (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchswitch!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4374, __PRETTY_FUNCTION__)) |
4374 | "Successor # out of range for catchswitch!")((Idx < getNumSuccessors() && "Successor # out of range for catchswitch!" ) ? static_cast<void> (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchswitch!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4374, __PRETTY_FUNCTION__)); |
4375 | setOperand(Idx + 1, NewSucc); |
4376 | } |
4377 | |
4378 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
4379 | static bool classof(const Instruction *I) { |
4380 | return I->getOpcode() == Instruction::CatchSwitch; |
4381 | } |
4382 | static bool classof(const Value *V) { |
4383 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4384 | } |
4385 | }; |
4386 | |
4387 | template <> |
4388 | struct OperandTraits<CatchSwitchInst> : public HungoffOperandTraits<2> {}; |
4389 | |
4390 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchSwitchInst, Value)CatchSwitchInst::op_iterator CatchSwitchInst::op_begin() { return OperandTraits<CatchSwitchInst>::op_begin(this); } CatchSwitchInst ::const_op_iterator CatchSwitchInst::op_begin() const { return OperandTraits<CatchSwitchInst>::op_begin(const_cast< CatchSwitchInst*>(this)); } CatchSwitchInst::op_iterator CatchSwitchInst ::op_end() { return OperandTraits<CatchSwitchInst>::op_end (this); } CatchSwitchInst::const_op_iterator CatchSwitchInst:: op_end() const { return OperandTraits<CatchSwitchInst>:: op_end(const_cast<CatchSwitchInst*>(this)); } Value *CatchSwitchInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<CatchSwitchInst>::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<CatchSwitchInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4390, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<CatchSwitchInst>::op_begin(const_cast< CatchSwitchInst*>(this))[i_nocapture].get()); } void CatchSwitchInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<CatchSwitchInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<CatchSwitchInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4390, __PRETTY_FUNCTION__)); OperandTraits<CatchSwitchInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned CatchSwitchInst::getNumOperands() const { return OperandTraits <CatchSwitchInst>::operands(this); } template <int Idx_nocapture > Use &CatchSwitchInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &CatchSwitchInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } |
4391 | |
4392 | //===----------------------------------------------------------------------===// |
4393 | // CleanupPadInst Class |
4394 | //===----------------------------------------------------------------------===// |
4395 | class CleanupPadInst : public FuncletPadInst { |
4396 | private: |
4397 | explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args, |
4398 | unsigned Values, const Twine &NameStr, |
4399 | Instruction *InsertBefore) |
4400 | : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values, |
4401 | NameStr, InsertBefore) {} |
4402 | explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args, |
4403 | unsigned Values, const Twine &NameStr, |
4404 | BasicBlock *InsertAtEnd) |
4405 | : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values, |
4406 | NameStr, InsertAtEnd) {} |
4407 | |
4408 | public: |
4409 | static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args = None, |
4410 | const Twine &NameStr = "", |
4411 | Instruction *InsertBefore = nullptr) { |
4412 | unsigned Values = 1 + Args.size(); |
4413 | return new (Values) |
4414 | CleanupPadInst(ParentPad, Args, Values, NameStr, InsertBefore); |
4415 | } |
4416 | |
4417 | static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args, |
4418 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
4419 | unsigned Values = 1 + Args.size(); |
4420 | return new (Values) |
4421 | CleanupPadInst(ParentPad, Args, Values, NameStr, InsertAtEnd); |
4422 | } |
4423 | |
4424 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
4425 | static bool classof(const Instruction *I) { |
4426 | return I->getOpcode() == Instruction::CleanupPad; |
4427 | } |
4428 | static bool classof(const Value *V) { |
4429 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4430 | } |
4431 | }; |
4432 | |
4433 | //===----------------------------------------------------------------------===// |
4434 | // CatchPadInst Class |
4435 | //===----------------------------------------------------------------------===// |
4436 | class CatchPadInst : public FuncletPadInst { |
4437 | private: |
4438 | explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args, |
4439 | unsigned Values, const Twine &NameStr, |
4440 | Instruction *InsertBefore) |
4441 | : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values, |
4442 | NameStr, InsertBefore) {} |
4443 | explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args, |
4444 | unsigned Values, const Twine &NameStr, |
4445 | BasicBlock *InsertAtEnd) |
4446 | : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values, |
4447 | NameStr, InsertAtEnd) {} |
4448 | |
4449 | public: |
4450 | static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args, |
4451 | const Twine &NameStr = "", |
4452 | Instruction *InsertBefore = nullptr) { |
4453 | unsigned Values = 1 + Args.size(); |
4454 | return new (Values) |
4455 | CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertBefore); |
4456 | } |
4457 | |
4458 | static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args, |
4459 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
4460 | unsigned Values = 1 + Args.size(); |
4461 | return new (Values) |
4462 | CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertAtEnd); |
4463 | } |
4464 | |
4465 | /// Convenience accessors |
4466 | CatchSwitchInst *getCatchSwitch() const { |
4467 | return cast<CatchSwitchInst>(Op<-1>()); |
4468 | } |
4469 | void setCatchSwitch(Value *CatchSwitch) { |
4470 | assert(CatchSwitch)((CatchSwitch) ? static_cast<void> (0) : __assert_fail ( "CatchSwitch", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4470, __PRETTY_FUNCTION__)); |
4471 | Op<-1>() = CatchSwitch; |
4472 | } |
4473 | |
4474 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
4475 | static bool classof(const Instruction *I) { |
4476 | return I->getOpcode() == Instruction::CatchPad; |
4477 | } |
4478 | static bool classof(const Value *V) { |
4479 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4480 | } |
4481 | }; |
4482 | |
4483 | //===----------------------------------------------------------------------===// |
4484 | // CatchReturnInst Class |
4485 | //===----------------------------------------------------------------------===// |
4486 | |
4487 | class CatchReturnInst : public Instruction { |
4488 | CatchReturnInst(const CatchReturnInst &RI); |
4489 | CatchReturnInst(Value *CatchPad, BasicBlock *BB, Instruction *InsertBefore); |
4490 | CatchReturnInst(Value *CatchPad, BasicBlock *BB, BasicBlock *InsertAtEnd); |
4491 | |
4492 | void init(Value *CatchPad, BasicBlock *BB); |
4493 | |
4494 | protected: |
4495 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4496 | friend class Instruction; |
4497 | |
4498 | CatchReturnInst *cloneImpl() const; |
4499 | |
4500 | public: |
4501 | static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB, |
4502 | Instruction *InsertBefore = nullptr) { |
4503 | assert(CatchPad)((CatchPad) ? static_cast<void> (0) : __assert_fail ("CatchPad" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4503, __PRETTY_FUNCTION__)); |
4504 | assert(BB)((BB) ? static_cast<void> (0) : __assert_fail ("BB", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4504, __PRETTY_FUNCTION__)); |
4505 | return new (2) CatchReturnInst(CatchPad, BB, InsertBefore); |
4506 | } |
4507 | |
4508 | static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB, |
4509 | BasicBlock *InsertAtEnd) { |
4510 | assert(CatchPad)((CatchPad) ? static_cast<void> (0) : __assert_fail ("CatchPad" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4510, __PRETTY_FUNCTION__)); |
4511 | assert(BB)((BB) ? static_cast<void> (0) : __assert_fail ("BB", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4511, __PRETTY_FUNCTION__)); |
4512 | return new (2) CatchReturnInst(CatchPad, BB, InsertAtEnd); |
4513 | } |
4514 | |
4515 | /// Provide fast operand accessors |
4516 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
4517 | |
4518 | /// Convenience accessors. |
4519 | CatchPadInst *getCatchPad() const { return cast<CatchPadInst>(Op<0>()); } |
4520 | void setCatchPad(CatchPadInst *CatchPad) { |
4521 | assert(CatchPad)((CatchPad) ? static_cast<void> (0) : __assert_fail ("CatchPad" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4521, __PRETTY_FUNCTION__)); |
4522 | Op<0>() = CatchPad; |
4523 | } |
4524 | |
4525 | BasicBlock *getSuccessor() const { return cast<BasicBlock>(Op<1>()); } |
4526 | void setSuccessor(BasicBlock *NewSucc) { |
4527 | assert(NewSucc)((NewSucc) ? static_cast<void> (0) : __assert_fail ("NewSucc" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4527, __PRETTY_FUNCTION__)); |
4528 | Op<1>() = NewSucc; |
4529 | } |
4530 | unsigned getNumSuccessors() const { return 1; } |
4531 | |
4532 | /// Get the parentPad of this catchret's catchpad's catchswitch. |
4533 | /// The successor block is implicitly a member of this funclet. |
4534 | Value *getCatchSwitchParentPad() const { |
4535 | return getCatchPad()->getCatchSwitch()->getParentPad(); |
4536 | } |
4537 | |
4538 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
4539 | static bool classof(const Instruction *I) { |
4540 | return (I->getOpcode() == Instruction::CatchRet); |
4541 | } |
4542 | static bool classof(const Value *V) { |
4543 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4544 | } |
4545 | |
4546 | private: |
4547 | BasicBlock *getSuccessor(unsigned Idx) const { |
4548 | assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!")((Idx < getNumSuccessors() && "Successor # out of range for catchret!" ) ? static_cast<void> (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchret!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4548, __PRETTY_FUNCTION__)); |
4549 | return getSuccessor(); |
4550 | } |
4551 | |
4552 | void setSuccessor(unsigned Idx, BasicBlock *B) { |
4553 | assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!")((Idx < getNumSuccessors() && "Successor # out of range for catchret!" ) ? static_cast<void> (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchret!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4553, __PRETTY_FUNCTION__)); |
4554 | setSuccessor(B); |
4555 | } |
4556 | }; |
4557 | |
4558 | template <> |
4559 | struct OperandTraits<CatchReturnInst> |
4560 | : public FixedNumOperandTraits<CatchReturnInst, 2> {}; |
4561 | |
4562 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchReturnInst, Value)CatchReturnInst::op_iterator CatchReturnInst::op_begin() { return OperandTraits<CatchReturnInst>::op_begin(this); } CatchReturnInst ::const_op_iterator CatchReturnInst::op_begin() const { return OperandTraits<CatchReturnInst>::op_begin(const_cast< CatchReturnInst*>(this)); } CatchReturnInst::op_iterator CatchReturnInst ::op_end() { return OperandTraits<CatchReturnInst>::op_end (this); } CatchReturnInst::const_op_iterator CatchReturnInst:: op_end() const { return OperandTraits<CatchReturnInst>:: op_end(const_cast<CatchReturnInst*>(this)); } Value *CatchReturnInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<CatchReturnInst>::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<CatchReturnInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4562, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<CatchReturnInst>::op_begin(const_cast< CatchReturnInst*>(this))[i_nocapture].get()); } void CatchReturnInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<CatchReturnInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<CatchReturnInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4562, __PRETTY_FUNCTION__)); OperandTraits<CatchReturnInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned CatchReturnInst::getNumOperands() const { return OperandTraits <CatchReturnInst>::operands(this); } template <int Idx_nocapture > Use &CatchReturnInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &CatchReturnInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } |
4563 | |
4564 | //===----------------------------------------------------------------------===// |
4565 | // CleanupReturnInst Class |
4566 | //===----------------------------------------------------------------------===// |
4567 | |
4568 | class CleanupReturnInst : public Instruction { |
4569 | private: |
4570 | CleanupReturnInst(const CleanupReturnInst &RI); |
4571 | CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values, |
4572 | Instruction *InsertBefore = nullptr); |
4573 | CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values, |
4574 | BasicBlock *InsertAtEnd); |
4575 | |
4576 | void init(Value *CleanupPad, BasicBlock *UnwindBB); |
4577 | |
4578 | protected: |
4579 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4580 | friend class Instruction; |
4581 | |
4582 | CleanupReturnInst *cloneImpl() const; |
4583 | |
4584 | public: |
4585 | static CleanupReturnInst *Create(Value *CleanupPad, |
4586 | BasicBlock *UnwindBB = nullptr, |
4587 | Instruction *InsertBefore = nullptr) { |
4588 | assert(CleanupPad)((CleanupPad) ? static_cast<void> (0) : __assert_fail ( "CleanupPad", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4588, __PRETTY_FUNCTION__)); |
4589 | unsigned Values = 1; |
4590 | if (UnwindBB) |
4591 | ++Values; |
4592 | return new (Values) |
4593 | CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertBefore); |
4594 | } |
4595 | |
4596 | static CleanupReturnInst *Create(Value *CleanupPad, BasicBlock *UnwindBB, |
4597 | BasicBlock *InsertAtEnd) { |
4598 | assert(CleanupPad)((CleanupPad) ? static_cast<void> (0) : __assert_fail ( "CleanupPad", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4598, __PRETTY_FUNCTION__)); |
4599 | unsigned Values = 1; |
4600 | if (UnwindBB) |
4601 | ++Values; |
4602 | return new (Values) |
4603 | CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertAtEnd); |
4604 | } |
4605 | |
4606 | /// Provide fast operand accessors |
4607 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
4608 | |
4609 | bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; } |
4610 | bool unwindsToCaller() const { return !hasUnwindDest(); } |
4611 | |
4612 | /// Convenience accessor. |
4613 | CleanupPadInst *getCleanupPad() const { |
4614 | return cast<CleanupPadInst>(Op<0>()); |
4615 | } |
4616 | void setCleanupPad(CleanupPadInst *CleanupPad) { |
4617 | assert(CleanupPad)((CleanupPad) ? static_cast<void> (0) : __assert_fail ( "CleanupPad", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4617, __PRETTY_FUNCTION__)); |
4618 | Op<0>() = CleanupPad; |
4619 | } |
4620 | |
4621 | unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; } |
4622 | |
4623 | BasicBlock *getUnwindDest() const { |
4624 | return hasUnwindDest() ? cast<BasicBlock>(Op<1>()) : nullptr; |
4625 | } |
4626 | void setUnwindDest(BasicBlock *NewDest) { |
4627 | assert(NewDest)((NewDest) ? static_cast<void> (0) : __assert_fail ("NewDest" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4627, __PRETTY_FUNCTION__)); |
4628 | assert(hasUnwindDest())((hasUnwindDest()) ? static_cast<void> (0) : __assert_fail ("hasUnwindDest()", "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4628, __PRETTY_FUNCTION__)); |
4629 | Op<1>() = NewDest; |
4630 | } |
4631 | |
4632 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
4633 | static bool classof(const Instruction *I) { |
4634 | return (I->getOpcode() == Instruction::CleanupRet); |
4635 | } |
4636 | static bool classof(const Value *V) { |
4637 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4638 | } |
4639 | |
4640 | private: |
4641 | BasicBlock *getSuccessor(unsigned Idx) const { |
4642 | assert(Idx == 0)((Idx == 0) ? static_cast<void> (0) : __assert_fail ("Idx == 0" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4642, __PRETTY_FUNCTION__)); |
4643 | return getUnwindDest(); |
4644 | } |
4645 | |
4646 | void setSuccessor(unsigned Idx, BasicBlock *B) { |
4647 | assert(Idx == 0)((Idx == 0) ? static_cast<void> (0) : __assert_fail ("Idx == 0" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4647, __PRETTY_FUNCTION__)); |
4648 | setUnwindDest(B); |
4649 | } |
4650 | |
4651 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
4652 | // method so that subclasses cannot accidentally use it. |
4653 | void setInstructionSubclassData(unsigned short D) { |
4654 | Instruction::setInstructionSubclassData(D); |
4655 | } |
4656 | }; |
4657 | |
4658 | template <> |
4659 | struct OperandTraits<CleanupReturnInst> |
4660 | : public VariadicOperandTraits<CleanupReturnInst, /*MINARITY=*/1> {}; |
4661 | |
4662 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CleanupReturnInst, Value)CleanupReturnInst::op_iterator CleanupReturnInst::op_begin() { return OperandTraits<CleanupReturnInst>::op_begin(this ); } CleanupReturnInst::const_op_iterator CleanupReturnInst:: op_begin() const { return OperandTraits<CleanupReturnInst> ::op_begin(const_cast<CleanupReturnInst*>(this)); } CleanupReturnInst ::op_iterator CleanupReturnInst::op_end() { return OperandTraits <CleanupReturnInst>::op_end(this); } CleanupReturnInst:: const_op_iterator CleanupReturnInst::op_end() const { return OperandTraits <CleanupReturnInst>::op_end(const_cast<CleanupReturnInst *>(this)); } Value *CleanupReturnInst::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<CleanupReturnInst >::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<CleanupReturnInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4662, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<CleanupReturnInst>::op_begin(const_cast <CleanupReturnInst*>(this))[i_nocapture].get()); } void CleanupReturnInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((i_nocapture < OperandTraits<CleanupReturnInst> ::operands(this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<CleanupReturnInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4662, __PRETTY_FUNCTION__)); OperandTraits<CleanupReturnInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned CleanupReturnInst::getNumOperands() const { return OperandTraits <CleanupReturnInst>::operands(this); } template <int Idx_nocapture> Use &CleanupReturnInst::Op() { return this ->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture > const Use &CleanupReturnInst::Op() const { return this ->OpFrom<Idx_nocapture>(this); } |
4663 | |
4664 | //===----------------------------------------------------------------------===// |
4665 | // UnreachableInst Class |
4666 | //===----------------------------------------------------------------------===// |
4667 | |
4668 | //===--------------------------------------------------------------------------- |
4669 | /// This function has undefined behavior. In particular, the |
4670 | /// presence of this instruction indicates some higher level knowledge that the |
4671 | /// end of the block cannot be reached. |
4672 | /// |
4673 | class UnreachableInst : public Instruction { |
4674 | protected: |
4675 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4676 | friend class Instruction; |
4677 | |
4678 | UnreachableInst *cloneImpl() const; |
4679 | |
4680 | public: |
4681 | explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr); |
4682 | explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd); |
4683 | |
4684 | // allocate space for exactly zero operands |
4685 | void *operator new(size_t s) { |
4686 | return User::operator new(s, 0); |
4687 | } |
4688 | |
4689 | unsigned getNumSuccessors() const { return 0; } |
4690 | |
4691 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
4692 | static bool classof(const Instruction *I) { |
4693 | return I->getOpcode() == Instruction::Unreachable; |
4694 | } |
4695 | static bool classof(const Value *V) { |
4696 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4697 | } |
4698 | |
4699 | private: |
4700 | BasicBlock *getSuccessor(unsigned idx) const { |
4701 | llvm_unreachable("UnreachableInst has no successors!")::llvm::llvm_unreachable_internal("UnreachableInst has no successors!" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4701); |
4702 | } |
4703 | |
4704 | void setSuccessor(unsigned idx, BasicBlock *B) { |
4705 | llvm_unreachable("UnreachableInst has no successors!")::llvm::llvm_unreachable_internal("UnreachableInst has no successors!" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 4705); |
4706 | } |
4707 | }; |
4708 | |
4709 | //===----------------------------------------------------------------------===// |
4710 | // TruncInst Class |
4711 | //===----------------------------------------------------------------------===// |
4712 | |
4713 | /// This class represents a truncation of integer types. |
4714 | class TruncInst : public CastInst { |
4715 | protected: |
4716 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4717 | friend class Instruction; |
4718 | |
4719 | /// Clone an identical TruncInst |
4720 | TruncInst *cloneImpl() const; |
4721 | |
4722 | public: |
4723 | /// Constructor with insert-before-instruction semantics |
4724 | TruncInst( |
4725 | Value *S, ///< The value to be truncated |
4726 | Type *Ty, ///< The (smaller) type to truncate to |
4727 | const Twine &NameStr = "", ///< A name for the new instruction |
4728 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
4729 | ); |
4730 | |
4731 | /// Constructor with insert-at-end-of-block semantics |
4732 | TruncInst( |
4733 | Value *S, ///< The value to be truncated |
4734 | Type *Ty, ///< The (smaller) type to truncate to |
4735 | const Twine &NameStr, ///< A name for the new instruction |
4736 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
4737 | ); |
4738 | |
4739 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
4740 | static bool classof(const Instruction *I) { |
4741 | return I->getOpcode() == Trunc; |
4742 | } |
4743 | static bool classof(const Value *V) { |
4744 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4745 | } |
4746 | }; |
4747 | |
4748 | //===----------------------------------------------------------------------===// |
4749 | // ZExtInst Class |
4750 | //===----------------------------------------------------------------------===// |
4751 | |
4752 | /// This class represents zero extension of integer types. |
4753 | class ZExtInst : public CastInst { |
4754 | protected: |
4755 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4756 | friend class Instruction; |
4757 | |
4758 | /// Clone an identical ZExtInst |
4759 | ZExtInst *cloneImpl() const; |
4760 | |
4761 | public: |
4762 | /// Constructor with insert-before-instruction semantics |
4763 | ZExtInst( |
4764 | Value *S, ///< The value to be zero extended |
4765 | Type *Ty, ///< The type to zero extend to |
4766 | const Twine &NameStr = "", ///< A name for the new instruction |
4767 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
4768 | ); |
4769 | |
4770 | /// Constructor with insert-at-end semantics. |
4771 | ZExtInst( |
4772 | Value *S, ///< The value to be zero extended |
4773 | Type *Ty, ///< The type to zero extend to |
4774 | const Twine &NameStr, ///< A name for the new instruction |
4775 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
4776 | ); |
4777 | |
4778 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
4779 | static bool classof(const Instruction *I) { |
4780 | return I->getOpcode() == ZExt; |
4781 | } |
4782 | static bool classof(const Value *V) { |
4783 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4784 | } |
4785 | }; |
4786 | |
4787 | //===----------------------------------------------------------------------===// |
4788 | // SExtInst Class |
4789 | //===----------------------------------------------------------------------===// |
4790 | |
4791 | /// This class represents a sign extension of integer types. |
4792 | class SExtInst : public CastInst { |
4793 | protected: |
4794 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4795 | friend class Instruction; |
4796 | |
4797 | /// Clone an identical SExtInst |
4798 | SExtInst *cloneImpl() const; |
4799 | |
4800 | public: |
4801 | /// Constructor with insert-before-instruction semantics |
4802 | SExtInst( |
4803 | Value *S, ///< The value to be sign extended |
4804 | Type *Ty, ///< The type to sign extend to |
4805 | const Twine &NameStr = "", ///< A name for the new instruction |
4806 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
4807 | ); |
4808 | |
4809 | /// Constructor with insert-at-end-of-block semantics |
4810 | SExtInst( |
4811 | Value *S, ///< The value to be sign extended |
4812 | Type *Ty, ///< The type to sign extend to |
4813 | const Twine &NameStr, ///< A name for the new instruction |
4814 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
4815 | ); |
4816 | |
4817 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
4818 | static bool classof(const Instruction *I) { |
4819 | return I->getOpcode() == SExt; |
4820 | } |
4821 | static bool classof(const Value *V) { |
4822 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4823 | } |
4824 | }; |
4825 | |
4826 | //===----------------------------------------------------------------------===// |
4827 | // FPTruncInst Class |
4828 | //===----------------------------------------------------------------------===// |
4829 | |
4830 | /// This class represents a truncation of floating point types. |
4831 | class FPTruncInst : public CastInst { |
4832 | protected: |
4833 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4834 | friend class Instruction; |
4835 | |
4836 | /// Clone an identical FPTruncInst |
4837 | FPTruncInst *cloneImpl() const; |
4838 | |
4839 | public: |
4840 | /// Constructor with insert-before-instruction semantics |
4841 | FPTruncInst( |
4842 | Value *S, ///< The value to be truncated |
4843 | Type *Ty, ///< The type to truncate to |
4844 | const Twine &NameStr = "", ///< A name for the new instruction |
4845 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
4846 | ); |
4847 | |
4848 | /// Constructor with insert-before-instruction semantics |
4849 | FPTruncInst( |
4850 | Value *S, ///< The value to be truncated |
4851 | Type *Ty, ///< The type to truncate to |
4852 | const Twine &NameStr, ///< A name for the new instruction |
4853 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
4854 | ); |
4855 | |
4856 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
4857 | static bool classof(const Instruction *I) { |
4858 | return I->getOpcode() == FPTrunc; |
4859 | } |
4860 | static bool classof(const Value *V) { |
4861 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4862 | } |
4863 | }; |
4864 | |
4865 | //===----------------------------------------------------------------------===// |
4866 | // FPExtInst Class |
4867 | //===----------------------------------------------------------------------===// |
4868 | |
4869 | /// This class represents an extension of floating point types. |
4870 | class FPExtInst : public CastInst { |
4871 | protected: |
4872 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4873 | friend class Instruction; |
4874 | |
4875 | /// Clone an identical FPExtInst |
4876 | FPExtInst *cloneImpl() const; |
4877 | |
4878 | public: |
4879 | /// Constructor with insert-before-instruction semantics |
4880 | FPExtInst( |
4881 | Value *S, ///< The value to be extended |
4882 | Type *Ty, ///< The type to extend to |
4883 | const Twine &NameStr = "", ///< A name for the new instruction |
4884 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
4885 | ); |
4886 | |
4887 | /// Constructor with insert-at-end-of-block semantics |
4888 | FPExtInst( |
4889 | Value *S, ///< The value to be extended |
4890 | Type *Ty, ///< The type to extend to |
4891 | const Twine &NameStr, ///< A name for the new instruction |
4892 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
4893 | ); |
4894 | |
4895 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
4896 | static bool classof(const Instruction *I) { |
4897 | return I->getOpcode() == FPExt; |
4898 | } |
4899 | static bool classof(const Value *V) { |
4900 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4901 | } |
4902 | }; |
4903 | |
4904 | //===----------------------------------------------------------------------===// |
4905 | // UIToFPInst Class |
4906 | //===----------------------------------------------------------------------===// |
4907 | |
4908 | /// This class represents a cast unsigned integer to floating point. |
4909 | class UIToFPInst : public CastInst { |
4910 | protected: |
4911 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4912 | friend class Instruction; |
4913 | |
4914 | /// Clone an identical UIToFPInst |
4915 | UIToFPInst *cloneImpl() const; |
4916 | |
4917 | public: |
4918 | /// Constructor with insert-before-instruction semantics |
4919 | UIToFPInst( |
4920 | Value *S, ///< The value to be converted |
4921 | Type *Ty, ///< The type to convert to |
4922 | const Twine &NameStr = "", ///< A name for the new instruction |
4923 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
4924 | ); |
4925 | |
4926 | /// Constructor with insert-at-end-of-block semantics |
4927 | UIToFPInst( |
4928 | Value *S, ///< The value to be converted |
4929 | Type *Ty, ///< The type to convert to |
4930 | const Twine &NameStr, ///< A name for the new instruction |
4931 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
4932 | ); |
4933 | |
4934 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
4935 | static bool classof(const Instruction *I) { |
4936 | return I->getOpcode() == UIToFP; |
4937 | } |
4938 | static bool classof(const Value *V) { |
4939 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4940 | } |
4941 | }; |
4942 | |
4943 | //===----------------------------------------------------------------------===// |
4944 | // SIToFPInst Class |
4945 | //===----------------------------------------------------------------------===// |
4946 | |
4947 | /// This class represents a cast from signed integer to floating point. |
4948 | class SIToFPInst : public CastInst { |
4949 | protected: |
4950 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4951 | friend class Instruction; |
4952 | |
4953 | /// Clone an identical SIToFPInst |
4954 | SIToFPInst *cloneImpl() const; |
4955 | |
4956 | public: |
4957 | /// Constructor with insert-before-instruction semantics |
4958 | SIToFPInst( |
4959 | Value *S, ///< The value to be converted |
4960 | Type *Ty, ///< The type to convert to |
4961 | const Twine &NameStr = "", ///< A name for the new instruction |
4962 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
4963 | ); |
4964 | |
4965 | /// Constructor with insert-at-end-of-block semantics |
4966 | SIToFPInst( |
4967 | Value *S, ///< The value to be converted |
4968 | Type *Ty, ///< The type to convert to |
4969 | const Twine &NameStr, ///< A name for the new instruction |
4970 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
4971 | ); |
4972 | |
4973 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
4974 | static bool classof(const Instruction *I) { |
4975 | return I->getOpcode() == SIToFP; |
4976 | } |
4977 | static bool classof(const Value *V) { |
4978 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4979 | } |
4980 | }; |
4981 | |
4982 | //===----------------------------------------------------------------------===// |
4983 | // FPToUIInst Class |
4984 | //===----------------------------------------------------------------------===// |
4985 | |
4986 | /// This class represents a cast from floating point to unsigned integer |
4987 | class FPToUIInst : public CastInst { |
4988 | protected: |
4989 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4990 | friend class Instruction; |
4991 | |
4992 | /// Clone an identical FPToUIInst |
4993 | FPToUIInst *cloneImpl() const; |
4994 | |
4995 | public: |
4996 | /// Constructor with insert-before-instruction semantics |
4997 | FPToUIInst( |
4998 | Value *S, ///< The value to be converted |
4999 | Type *Ty, ///< The type to convert to |
5000 | const Twine &NameStr = "", ///< A name for the new instruction |
5001 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
5002 | ); |
5003 | |
5004 | /// Constructor with insert-at-end-of-block semantics |
5005 | FPToUIInst( |
5006 | Value *S, ///< The value to be converted |
5007 | Type *Ty, ///< The type to convert to |
5008 | const Twine &NameStr, ///< A name for the new instruction |
5009 | BasicBlock *InsertAtEnd ///< Where to insert the new instruction |
5010 | ); |
5011 | |
5012 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
5013 | static bool classof(const Instruction *I) { |
5014 | return I->getOpcode() == FPToUI; |
5015 | } |
5016 | static bool classof(const Value *V) { |
5017 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
5018 | } |
5019 | }; |
5020 | |
5021 | //===----------------------------------------------------------------------===// |
5022 | // FPToSIInst Class |
5023 | //===----------------------------------------------------------------------===// |
5024 | |
5025 | /// This class represents a cast from floating point to signed integer. |
5026 | class FPToSIInst : public CastInst { |
5027 | protected: |
5028 | // Note: Instruction needs to be a friend here to call cloneImpl. |
5029 | friend class Instruction; |
5030 | |
5031 | /// Clone an identical FPToSIInst |
5032 | FPToSIInst *cloneImpl() const; |
5033 | |
5034 | public: |
5035 | /// Constructor with insert-before-instruction semantics |
5036 | FPToSIInst( |
5037 | Value *S, ///< The value to be converted |
5038 | Type *Ty, ///< The type to convert to |
5039 | const Twine &NameStr = "", ///< A name for the new instruction |
5040 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
5041 | ); |
5042 | |
5043 | /// Constructor with insert-at-end-of-block semantics |
5044 | FPToSIInst( |
5045 | Value *S, ///< The value to be converted |
5046 | Type *Ty, ///< The type to convert to |
5047 | const Twine &NameStr, ///< A name for the new instruction |
5048 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
5049 | ); |
5050 | |
5051 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
5052 | static bool classof(const Instruction *I) { |
5053 | return I->getOpcode() == FPToSI; |
5054 | } |
5055 | static bool classof(const Value *V) { |
5056 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
5057 | } |
5058 | }; |
5059 | |
5060 | //===----------------------------------------------------------------------===// |
5061 | // IntToPtrInst Class |
5062 | //===----------------------------------------------------------------------===// |
5063 | |
5064 | /// This class represents a cast from an integer to a pointer. |
5065 | class IntToPtrInst : public CastInst { |
5066 | public: |
5067 | // Note: Instruction needs to be a friend here to call cloneImpl. |
5068 | friend class Instruction; |
5069 | |
5070 | /// Constructor with insert-before-instruction semantics |
5071 | IntToPtrInst( |
5072 | Value *S, ///< The value to be converted |
5073 | Type *Ty, ///< The type to convert to |
5074 | const Twine &NameStr = "", ///< A name for the new instruction |
5075 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
5076 | ); |
5077 | |
5078 | /// Constructor with insert-at-end-of-block semantics |
5079 | IntToPtrInst( |
5080 | Value *S, ///< The value to be converted |
5081 | Type *Ty, ///< The type to convert to |
5082 | const Twine &NameStr, ///< A name for the new instruction |
5083 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
5084 | ); |
5085 | |
5086 | /// Clone an identical IntToPtrInst. |
5087 | IntToPtrInst *cloneImpl() const; |
5088 | |
5089 | /// Returns the address space of this instruction's pointer type. |
5090 | unsigned getAddressSpace() const { |
5091 | return getType()->getPointerAddressSpace(); |
5092 | } |
5093 | |
5094 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
5095 | static bool classof(const Instruction *I) { |
5096 | return I->getOpcode() == IntToPtr; |
5097 | } |
5098 | static bool classof(const Value *V) { |
5099 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
5100 | } |
5101 | }; |
5102 | |
5103 | //===----------------------------------------------------------------------===// |
5104 | // PtrToIntInst Class |
5105 | //===----------------------------------------------------------------------===// |
5106 | |
5107 | /// This class represents a cast from a pointer to an integer. |
5108 | class PtrToIntInst : public CastInst { |
5109 | protected: |
5110 | // Note: Instruction needs to be a friend here to call cloneImpl. |
5111 | friend class Instruction; |
5112 | |
5113 | /// Clone an identical PtrToIntInst. |
5114 | PtrToIntInst *cloneImpl() const; |
5115 | |
5116 | public: |
5117 | /// Constructor with insert-before-instruction semantics |
5118 | PtrToIntInst( |
5119 | Value *S, ///< The value to be converted |
5120 | Type *Ty, ///< The type to convert to |
5121 | const Twine &NameStr = "", ///< A name for the new instruction |
5122 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
5123 | ); |
5124 | |
5125 | /// Constructor with insert-at-end-of-block semantics |
5126 | PtrToIntInst( |
5127 | Value *S, ///< The value to be converted |
5128 | Type *Ty, ///< The type to convert to |
5129 | const Twine &NameStr, ///< A name for the new instruction |
5130 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
5131 | ); |
5132 | |
5133 | /// Gets the pointer operand. |
5134 | Value *getPointerOperand() { return getOperand(0); } |
5135 | /// Gets the pointer operand. |
5136 | const Value *getPointerOperand() const { return getOperand(0); } |
5137 | /// Gets the operand index of the pointer operand. |
5138 | static unsigned getPointerOperandIndex() { return 0U; } |
5139 | |
5140 | /// Returns the address space of the pointer operand. |
5141 | unsigned getPointerAddressSpace() const { |
5142 | return getPointerOperand()->getType()->getPointerAddressSpace(); |
5143 | } |
5144 | |
5145 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
5146 | static bool classof(const Instruction *I) { |
5147 | return I->getOpcode() == PtrToInt; |
5148 | } |
5149 | static bool classof(const Value *V) { |
5150 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
5151 | } |
5152 | }; |
5153 | |
5154 | //===----------------------------------------------------------------------===// |
5155 | // BitCastInst Class |
5156 | //===----------------------------------------------------------------------===// |
5157 | |
5158 | /// This class represents a no-op cast from one type to another. |
5159 | class BitCastInst : public CastInst { |
5160 | protected: |
5161 | // Note: Instruction needs to be a friend here to call cloneImpl. |
5162 | friend class Instruction; |
5163 | |
5164 | /// Clone an identical BitCastInst. |
5165 | BitCastInst *cloneImpl() const; |
5166 | |
5167 | public: |
5168 | /// Constructor with insert-before-instruction semantics |
5169 | BitCastInst( |
5170 | Value *S, ///< The value to be casted |
5171 | Type *Ty, ///< The type to casted to |
5172 | const Twine &NameStr = "", ///< A name for the new instruction |
5173 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
5174 | ); |
5175 | |
5176 | /// Constructor with insert-at-end-of-block semantics |
5177 | BitCastInst( |
5178 | Value *S, ///< The value to be casted |
5179 | Type *Ty, ///< The type to casted to |
5180 | const Twine &NameStr, ///< A name for the new instruction |
5181 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
5182 | ); |
5183 | |
5184 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
5185 | static bool classof(const Instruction *I) { |
5186 | return I->getOpcode() == BitCast; |
5187 | } |
5188 | static bool classof(const Value *V) { |
5189 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
5190 | } |
5191 | }; |
5192 | |
5193 | //===----------------------------------------------------------------------===// |
5194 | // AddrSpaceCastInst Class |
5195 | //===----------------------------------------------------------------------===// |
5196 | |
5197 | /// This class represents a conversion between pointers from one address space |
5198 | /// to another. |
5199 | class AddrSpaceCastInst : public CastInst { |
5200 | protected: |
5201 | // Note: Instruction needs to be a friend here to call cloneImpl. |
5202 | friend class Instruction; |
5203 | |
5204 | /// Clone an identical AddrSpaceCastInst. |
5205 | AddrSpaceCastInst *cloneImpl() const; |
5206 | |
5207 | public: |
5208 | /// Constructor with insert-before-instruction semantics |
5209 | AddrSpaceCastInst( |
5210 | Value *S, ///< The value to be casted |
5211 | Type *Ty, ///< The type to casted to |
5212 | const Twine &NameStr = "", ///< A name for the new instruction |
5213 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
5214 | ); |
5215 | |
5216 | /// Constructor with insert-at-end-of-block semantics |
5217 | AddrSpaceCastInst( |
5218 | Value *S, ///< The value to be casted |
5219 | Type *Ty, ///< The type to casted to |
5220 | const Twine &NameStr, ///< A name for the new instruction |
5221 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
5222 | ); |
5223 | |
5224 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
5225 | static bool classof(const Instruction *I) { |
5226 | return I->getOpcode() == AddrSpaceCast; |
5227 | } |
5228 | static bool classof(const Value *V) { |
5229 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
5230 | } |
5231 | |
5232 | /// Gets the pointer operand. |
5233 | Value *getPointerOperand() { |
5234 | return getOperand(0); |
5235 | } |
5236 | |
5237 | /// Gets the pointer operand. |
5238 | const Value *getPointerOperand() const { |
5239 | return getOperand(0); |
5240 | } |
5241 | |
5242 | /// Gets the operand index of the pointer operand. |
5243 | static unsigned getPointerOperandIndex() { |
5244 | return 0U; |
5245 | } |
5246 | |
5247 | /// Returns the address space of the pointer operand. |
5248 | unsigned getSrcAddressSpace() const { |
5249 | return getPointerOperand()->getType()->getPointerAddressSpace(); |
5250 | } |
5251 | |
5252 | /// Returns the address space of the result. |
5253 | unsigned getDestAddressSpace() const { |
5254 | return getType()->getPointerAddressSpace(); |
5255 | } |
5256 | }; |
5257 | |
5258 | /// A helper function that returns the pointer operand of a load or store |
5259 | /// instruction. Returns nullptr if not load or store. |
5260 | inline const Value *getLoadStorePointerOperand(const Value *V) { |
5261 | if (auto *Load = dyn_cast<LoadInst>(V)) |
5262 | return Load->getPointerOperand(); |
5263 | if (auto *Store = dyn_cast<StoreInst>(V)) |
5264 | return Store->getPointerOperand(); |
5265 | return nullptr; |
5266 | } |
5267 | inline Value *getLoadStorePointerOperand(Value *V) { |
5268 | return const_cast<Value *>( |
5269 | getLoadStorePointerOperand(static_cast<const Value *>(V))); |
5270 | } |
5271 | |
5272 | /// A helper function that returns the pointer operand of a load, store |
5273 | /// or GEP instruction. Returns nullptr if not load, store, or GEP. |
5274 | inline const Value *getPointerOperand(const Value *V) { |
5275 | if (auto *Ptr = getLoadStorePointerOperand(V)) |
5276 | return Ptr; |
5277 | if (auto *Gep = dyn_cast<GetElementPtrInst>(V)) |
5278 | return Gep->getPointerOperand(); |
5279 | return nullptr; |
5280 | } |
5281 | inline Value *getPointerOperand(Value *V) { |
5282 | return const_cast<Value *>(getPointerOperand(static_cast<const Value *>(V))); |
5283 | } |
5284 | |
5285 | /// A helper function that returns the alignment of load or store instruction. |
5286 | inline unsigned getLoadStoreAlignment(Value *I) { |
5287 | assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&(((isa<LoadInst>(I) || isa<StoreInst>(I)) && "Expected Load or Store instruction") ? static_cast<void> (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 5288, __PRETTY_FUNCTION__)) |
5288 | "Expected Load or Store instruction")(((isa<LoadInst>(I) || isa<StoreInst>(I)) && "Expected Load or Store instruction") ? static_cast<void> (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 5288, __PRETTY_FUNCTION__)); |
5289 | if (auto *LI = dyn_cast<LoadInst>(I)) |
5290 | return LI->getAlignment(); |
5291 | return cast<StoreInst>(I)->getAlignment(); |
5292 | } |
5293 | |
5294 | /// A helper function that returns the address space of the pointer operand of |
5295 | /// load or store instruction. |
5296 | inline unsigned getLoadStoreAddressSpace(Value *I) { |
5297 | assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&(((isa<LoadInst>(I) || isa<StoreInst>(I)) && "Expected Load or Store instruction") ? static_cast<void> (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 5298, __PRETTY_FUNCTION__)) |
5298 | "Expected Load or Store instruction")(((isa<LoadInst>(I) || isa<StoreInst>(I)) && "Expected Load or Store instruction") ? static_cast<void> (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\"" , "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/IR/Instructions.h" , 5298, __PRETTY_FUNCTION__)); |
5299 | if (auto *LI = dyn_cast<LoadInst>(I)) |
5300 | return LI->getPointerAddressSpace(); |
5301 | return cast<StoreInst>(I)->getPointerAddressSpace(); |
5302 | } |
5303 | |
5304 | } // end namespace llvm |
5305 | |
5306 | #endif // LLVM_IR_INSTRUCTIONS_H |