Bug Summary

File:lib/Transforms/Scalar/LICM.cpp
Warning:line 1236, column 20
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

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

/build/llvm-toolchain-snapshot-10~svn374877/lib/Transforms/Scalar/LICM.cpp

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>
77using namespace llvm;
78
79#define DEBUG_TYPE"licm" "licm"
80
81STATISTIC(NumCreatedBlocks, "Number of blocks created")static llvm::Statistic NumCreatedBlocks = {"licm", "NumCreatedBlocks"
, "Number of blocks created"}
;
82STATISTIC(NumClonedBranches, "Number of branches cloned")static llvm::Statistic NumClonedBranches = {"licm", "NumClonedBranches"
, "Number of branches cloned"}
;
83STATISTIC(NumSunk, "Number of instructions sunk out of loop")static llvm::Statistic NumSunk = {"licm", "NumSunk", "Number of instructions sunk out of loop"
}
;
84STATISTIC(NumHoisted, "Number of instructions hoisted out of loop")static llvm::Statistic NumHoisted = {"licm", "NumHoisted", "Number of instructions hoisted out of loop"
}
;
85STATISTIC(NumMovedLoads, "Number of load insts hoisted or sunk")static llvm::Statistic NumMovedLoads = {"licm", "NumMovedLoads"
, "Number of load insts hoisted or sunk"}
;
86STATISTIC(NumMovedCalls, "Number of call insts hoisted or sunk")static llvm::Statistic NumMovedCalls = {"licm", "NumMovedCalls"
, "Number of call insts hoisted or sunk"}
;
87STATISTIC(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.
90static cl::opt<bool>
91 DisablePromotion("disable-licm-promotion", cl::Hidden, cl::init(false),
92 cl::desc("Disable memory promotion in LICM pass"));
93
94static 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
98static 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.
106static cl::opt<int>
107LICMN2Theshold("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.
119cl::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.
127cl::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
134static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI);
135static bool isNotUsedOrFreeInLoop(const Instruction &I, const Loop *CurLoop,
136 const LoopSafetyInfo *SafetyInfo,
137 TargetTransformInfo *TTI, bool &FreeInLoop);
138static void hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop,
139 BasicBlock *Dest, ICFLoopSafetyInfo *SafetyInfo,
140 MemorySSAUpdater *MSSAU, OptimizationRemarkEmitter *ORE);
141static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT,
142 const Loop *CurLoop, ICFLoopSafetyInfo *SafetyInfo,
143 MemorySSAUpdater *MSSAU, OptimizationRemarkEmitter *ORE);
144static bool isSafeToExecuteUnconditionally(Instruction &Inst,
145 const DominatorTree *DT,
146 const Loop *CurLoop,
147 const LoopSafetyInfo *SafetyInfo,
148 OptimizationRemarkEmitter *ORE,
149 const Instruction *CtxI = nullptr);
150static bool pointerInvalidatedByLoop(MemoryLocation MemLoc,
151 AliasSetTracker *CurAST, Loop *CurLoop,
152 AliasAnalysis *AA);
153static bool pointerInvalidatedByLoopWithMSSA(MemorySSA *MSSA, MemoryUse *MU,
154 Loop *CurLoop,
155 SinkAndHoistLICMFlags &Flags);
156static Instruction *CloneInstructionInExitBlock(
157 Instruction &I, BasicBlock &ExitBlock, PHINode &PN, const LoopInfo *LI,
158 const LoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU);
159
160static void eraseInstruction(Instruction &I, ICFLoopSafetyInfo &SafetyInfo,
161 AliasSetTracker *AST, MemorySSAUpdater *MSSAU);
162
163static void moveInstructionBefore(Instruction &I, Instruction &Dest,
164 ICFLoopSafetyInfo &SafetyInfo,
165 MemorySSAUpdater *MSSAU);
166
167namespace {
168struct 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
181private:
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
193struct 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
261private:
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
277PreservedAnalyses 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
304char LegacyLICMPass::ID = 0;
305INITIALIZE_PASS_BEGIN(LegacyLICMPass, "licm", "Loop Invariant Code Motion",static void *initializeLegacyLICMPassPassOnce(PassRegistry &
Registry) {
306 false, false)static void *initializeLegacyLICMPassPassOnce(PassRegistry &
Registry) {
307INITIALIZE_PASS_DEPENDENCY(LoopPass)initializeLoopPassPass(Registry);
308INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)initializeTargetLibraryInfoWrapperPassPass(Registry);
309INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)initializeTargetTransformInfoWrapperPassPass(Registry);
310INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)initializeMemorySSAWrapperPassPass(Registry);
311INITIALIZE_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
314Pass *llvm::createLICMPass() { return new LegacyLICMPass(); }
315Pass *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///
326bool 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///
494bool 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
561namespace {
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.
569class ControlFlowHoister {
570private:
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
585public:
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///
795bool 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.
981static 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
1029namespace {
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.
1033bool isHoistableAndSinkableInst(Instruction &I) {
1034 // Only these instructions are hoistable/sinkable.
1035 return (isa<LoadInst>(I) || isa<StoreInst>(I) || isa<CallInst>(I) ||
2
Assuming 'I' is not a 'LoadInst'
3
Assuming 'I' is not a 'StoreInst'
4
Assuming 'I' is not a 'CallInst'
16
Returning the value 1, which participates in a condition later
1036 isa<FenceInst>(I) || isa<CastInst>(I) ||
5
Assuming 'I' is not a 'FenceInst'
6
Assuming 'I' is not a 'CastInst'
1037 isa<UnaryOperator>(I) || isa<BinaryOperator>(I) ||
7
Assuming 'I' is not a 'UnaryOperator'
8
Assuming 'I' is not a 'BinaryOperator'
1038 isa<SelectInst>(I) || isa<GetElementPtrInst>(I) || isa<CmpInst>(I) ||
9
Assuming 'I' is not a 'SelectInst'
10
Assuming 'I' is not a 'GetElementPtrInst'
11
Assuming 'I' is not a 'CmpInst'
1039 isa<InsertElementInst>(I) || isa<ExtractElementInst>(I) ||
12
Assuming 'I' is not a 'InsertElementInst'
13
Assuming 'I' is not a 'ExtractElementInst'
1040 isa<ShuffleVectorInst>(I) || isa<ExtractValueInst>(I) ||
14
Assuming 'I' is not a 'ShuffleVectorInst'
15
Assuming 'I' is a 'ExtractValueInst'
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.
1045bool 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.
1063bool 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
1080bool 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))
1
Calling 'isHoistableAndSinkableInst'
17
Returning from 'isHoistableAndSinkableInst'
18
Taking false branch
1088 return false;
1089
1090 MemorySSA *MSSA = MSSAU ? MSSAU->getMemorySSA() : nullptr;
19
Assuming 'MSSAU' is non-null
20
'?' condition is true
21
'MSSA' initialized here
1091 if (MSSA)
22
Assuming 'MSSA' is null
23
Taking false branch
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
24.1
'LI' is null
24.1
'LI' is null
= dyn_cast<LoadInst>(&I)) {
24
Assuming the object is not a 'LoadInst'
25
Taking false branch
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
26.1
'CI' is null
26.1
'CI' is null
= dyn_cast<CallInst>(&I)) {
26
Assuming the object is not a 'CallInst'
27
Taking false branch
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
28.1
'FI' is null
28.1
'FI' is null
= dyn_cast<FenceInst>(&I)) {
28
Assuming the object is not a 'FenceInst'
29
Taking false branch
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
30.1
'SI' is non-null
30.1
'SI' is non-null
= dyn_cast<StoreInst>(&I)) {
30
Assuming the object is a 'StoreInst'
31
Taking true branch
1201 if (!SI->isUnordered())
32
Calling 'StoreInst::isUnordered'
36
Returning from 'StoreInst::isUnordered'
37
Taking false branch
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) {
38
Assuming 'CurAST' is null
39
Taking false branch
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))
40
Assuming the condition is false
41
Taking false branch
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)
42
Assuming field 'NoOfMemAccTooLarge' is false
43
Taking false branch
1227 return false;
1228 // Check store only if there's still "quota" to check clobber.
1229 if (Flags->LicmMssaOptCounter >= Flags->LicmMssaOptCap)
44
Assuming field 'LicmMssaOptCounter' is < field 'LicmMssaOptCap'
45
Taking false branch
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);
46
Called C++ object pointer is null
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///
1289static 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.
1298static 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).
1326static 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
1358static 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
1432static 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
1442static 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
1454static 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
1472static 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
1490static 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///
1562static 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///
1663static 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.
1706static 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
1732namespace {
1733class 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
1764public:
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.
1840bool 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///
1867bool 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.
2155std::unique_ptr<AliasSetTracker>
2156LoopInvariantCodeMotion::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
2197std::unique_ptr<AliasSetTracker>
2198LoopInvariantCodeMotion::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///
2208void 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///
2219void 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///
2229void LegacyLICMPass::deleteAnalysisLoop(Loop *L) {
2230 if (!LICM.getLoopToAliasSetMap().count(L))
2231 return;
2232
2233 LICM.getLoopToAliasSetMap().erase(L);
2234}
2235
2236static 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
2283static 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///
2332static 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}

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

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