File: | llvm/lib/Transforms/Scalar/LICM.cpp |
Warning: | line 1225, column 41 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===-- LICM.cpp - Loop Invariant Code Motion Pass ------------------------===// | ||||||||||
2 | // | ||||||||||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||||||||
4 | // See https://llvm.org/LICENSE.txt for license information. | ||||||||||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||||||||
6 | // | ||||||||||
7 | //===----------------------------------------------------------------------===// | ||||||||||
8 | // | ||||||||||
9 | // This pass performs loop invariant code motion, attempting to remove as much | ||||||||||
10 | // code from the body of a loop as possible. It does this by either hoisting | ||||||||||
11 | // code into the preheader block, or by sinking code to the exit blocks if it is | ||||||||||
12 | // safe. This pass also promotes must-aliased memory locations in the loop to | ||||||||||
13 | // live in registers, thus hoisting and sinking "invariant" loads and stores. | ||||||||||
14 | // | ||||||||||
15 | // Hoisting operations out of loops is a canonicalization transform. It | ||||||||||
16 | // enables and simplifies subsequent optimizations in the middle-end. | ||||||||||
17 | // Rematerialization of hoisted instructions to reduce register pressure is the | ||||||||||
18 | // responsibility of the back-end, which has more accurate information about | ||||||||||
19 | // register pressure and also handles other optimizations than LICM that | ||||||||||
20 | // increase live-ranges. | ||||||||||
21 | // | ||||||||||
22 | // This pass uses alias analysis for two purposes: | ||||||||||
23 | // | ||||||||||
24 | // 1. Moving loop invariant loads and calls out of loops. If we can determine | ||||||||||
25 | // that a load or call inside of a loop never aliases anything stored to, | ||||||||||
26 | // we can hoist it or sink it like any other instruction. | ||||||||||
27 | // 2. Scalar Promotion of Memory - If there is a store instruction inside of | ||||||||||
28 | // the loop, we try to move the store to happen AFTER the loop instead of | ||||||||||
29 | // inside of the loop. This can only happen if a few conditions are true: | ||||||||||
30 | // A. The pointer stored through is loop invariant | ||||||||||
31 | // B. There are no stores or loads in the loop which _may_ alias the | ||||||||||
32 | // pointer. There are no calls in the loop which mod/ref the pointer. | ||||||||||
33 | // If these conditions are true, we can promote the loads and stores in the | ||||||||||
34 | // loop of the pointer to use a temporary alloca'd variable. We then use | ||||||||||
35 | // the SSAUpdater to construct the appropriate SSA form for the value. | ||||||||||
36 | // | ||||||||||
37 | //===----------------------------------------------------------------------===// | ||||||||||
38 | |||||||||||
39 | #include "llvm/Transforms/Scalar/LICM.h" | ||||||||||
40 | #include "llvm/ADT/SetOperations.h" | ||||||||||
41 | #include "llvm/ADT/Statistic.h" | ||||||||||
42 | #include "llvm/Analysis/AliasAnalysis.h" | ||||||||||
43 | #include "llvm/Analysis/AliasSetTracker.h" | ||||||||||
44 | #include "llvm/Analysis/BasicAliasAnalysis.h" | ||||||||||
45 | #include "llvm/Analysis/BlockFrequencyInfo.h" | ||||||||||
46 | #include "llvm/Analysis/CaptureTracking.h" | ||||||||||
47 | #include "llvm/Analysis/ConstantFolding.h" | ||||||||||
48 | #include "llvm/Analysis/GlobalsModRef.h" | ||||||||||
49 | #include "llvm/Analysis/GuardUtils.h" | ||||||||||
50 | #include "llvm/Analysis/LazyBlockFrequencyInfo.h" | ||||||||||
51 | #include "llvm/Analysis/Loads.h" | ||||||||||
52 | #include "llvm/Analysis/LoopInfo.h" | ||||||||||
53 | #include "llvm/Analysis/LoopIterator.h" | ||||||||||
54 | #include "llvm/Analysis/LoopPass.h" | ||||||||||
55 | #include "llvm/Analysis/MemoryBuiltins.h" | ||||||||||
56 | #include "llvm/Analysis/MemorySSA.h" | ||||||||||
57 | #include "llvm/Analysis/MemorySSAUpdater.h" | ||||||||||
58 | #include "llvm/Analysis/MustExecute.h" | ||||||||||
59 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" | ||||||||||
60 | #include "llvm/Analysis/ScalarEvolution.h" | ||||||||||
61 | #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" | ||||||||||
62 | #include "llvm/Analysis/TargetLibraryInfo.h" | ||||||||||
63 | #include "llvm/Analysis/ValueTracking.h" | ||||||||||
64 | #include "llvm/IR/CFG.h" | ||||||||||
65 | #include "llvm/IR/Constants.h" | ||||||||||
66 | #include "llvm/IR/DataLayout.h" | ||||||||||
67 | #include "llvm/IR/DebugInfoMetadata.h" | ||||||||||
68 | #include "llvm/IR/DerivedTypes.h" | ||||||||||
69 | #include "llvm/IR/Dominators.h" | ||||||||||
70 | #include "llvm/IR/Instructions.h" | ||||||||||
71 | #include "llvm/IR/IntrinsicInst.h" | ||||||||||
72 | #include "llvm/IR/LLVMContext.h" | ||||||||||
73 | #include "llvm/IR/Metadata.h" | ||||||||||
74 | #include "llvm/IR/PatternMatch.h" | ||||||||||
75 | #include "llvm/IR/PredIteratorCache.h" | ||||||||||
76 | #include "llvm/InitializePasses.h" | ||||||||||
77 | #include "llvm/Support/CommandLine.h" | ||||||||||
78 | #include "llvm/Support/Debug.h" | ||||||||||
79 | #include "llvm/Support/raw_ostream.h" | ||||||||||
80 | #include "llvm/Transforms/Scalar.h" | ||||||||||
81 | #include "llvm/Transforms/Scalar/LoopPassManager.h" | ||||||||||
82 | #include "llvm/Transforms/Utils/AssumeBundleBuilder.h" | ||||||||||
83 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" | ||||||||||
84 | #include "llvm/Transforms/Utils/Local.h" | ||||||||||
85 | #include "llvm/Transforms/Utils/LoopUtils.h" | ||||||||||
86 | #include "llvm/Transforms/Utils/SSAUpdater.h" | ||||||||||
87 | #include <algorithm> | ||||||||||
88 | #include <utility> | ||||||||||
89 | using namespace llvm; | ||||||||||
90 | |||||||||||
91 | #define DEBUG_TYPE"licm" "licm" | ||||||||||
92 | |||||||||||
93 | STATISTIC(NumCreatedBlocks, "Number of blocks created")static llvm::Statistic NumCreatedBlocks = {"licm", "NumCreatedBlocks" , "Number of blocks created"}; | ||||||||||
94 | STATISTIC(NumClonedBranches, "Number of branches cloned")static llvm::Statistic NumClonedBranches = {"licm", "NumClonedBranches" , "Number of branches cloned"}; | ||||||||||
95 | STATISTIC(NumSunk, "Number of instructions sunk out of loop")static llvm::Statistic NumSunk = {"licm", "NumSunk", "Number of instructions sunk out of loop" }; | ||||||||||
96 | STATISTIC(NumHoisted, "Number of instructions hoisted out of loop")static llvm::Statistic NumHoisted = {"licm", "NumHoisted", "Number of instructions hoisted out of loop" }; | ||||||||||
97 | STATISTIC(NumMovedLoads, "Number of load insts hoisted or sunk")static llvm::Statistic NumMovedLoads = {"licm", "NumMovedLoads" , "Number of load insts hoisted or sunk"}; | ||||||||||
98 | STATISTIC(NumMovedCalls, "Number of call insts hoisted or sunk")static llvm::Statistic NumMovedCalls = {"licm", "NumMovedCalls" , "Number of call insts hoisted or sunk"}; | ||||||||||
99 | STATISTIC(NumPromoted, "Number of memory locations promoted to registers")static llvm::Statistic NumPromoted = {"licm", "NumPromoted", "Number of memory locations promoted to registers" }; | ||||||||||
100 | |||||||||||
101 | /// Memory promotion is enabled by default. | ||||||||||
102 | static cl::opt<bool> | ||||||||||
103 | DisablePromotion("disable-licm-promotion", cl::Hidden, cl::init(false), | ||||||||||
104 | cl::desc("Disable memory promotion in LICM pass")); | ||||||||||
105 | |||||||||||
106 | static cl::opt<bool> ControlFlowHoisting( | ||||||||||
107 | "licm-control-flow-hoisting", cl::Hidden, cl::init(false), | ||||||||||
108 | cl::desc("Enable control flow (and PHI) hoisting in LICM")); | ||||||||||
109 | |||||||||||
110 | static cl::opt<unsigned> HoistSinkColdnessThreshold( | ||||||||||
111 | "licm-coldness-threshold", cl::Hidden, cl::init(4), | ||||||||||
112 | cl::desc("Relative coldness Threshold of hoisting/sinking destination " | ||||||||||
113 | "block for LICM to be considered beneficial")); | ||||||||||
114 | |||||||||||
115 | static cl::opt<uint32_t> MaxNumUsesTraversed( | ||||||||||
116 | "licm-max-num-uses-traversed", cl::Hidden, cl::init(8), | ||||||||||
117 | cl::desc("Max num uses visited for identifying load " | ||||||||||
118 | "invariance in loop using invariant start (default = 8)")); | ||||||||||
119 | |||||||||||
120 | // Default value of zero implies we use the regular alias set tracker mechanism | ||||||||||
121 | // instead of the cross product using AA to identify aliasing of the memory | ||||||||||
122 | // location we are interested in. | ||||||||||
123 | static cl::opt<int> | ||||||||||
124 | LICMN2Theshold("licm-n2-threshold", cl::Hidden, cl::init(0), | ||||||||||
125 | cl::desc("How many instruction to cross product using AA")); | ||||||||||
126 | |||||||||||
127 | // Experimental option to allow imprecision in LICM in pathological cases, in | ||||||||||
128 | // exchange for faster compile. This is to be removed if MemorySSA starts to | ||||||||||
129 | // address the same issue. This flag applies only when LICM uses MemorySSA | ||||||||||
130 | // instead on AliasSetTracker. LICM calls MemorySSAWalker's | ||||||||||
131 | // getClobberingMemoryAccess, up to the value of the Cap, getting perfect | ||||||||||
132 | // accuracy. Afterwards, LICM will call into MemorySSA's getDefiningAccess, | ||||||||||
133 | // which may not be precise, since optimizeUses is capped. The result is | ||||||||||
134 | // correct, but we may not get as "far up" as possible to get which access is | ||||||||||
135 | // clobbering the one queried. | ||||||||||
136 | cl::opt<unsigned> llvm::SetLicmMssaOptCap( | ||||||||||
137 | "licm-mssa-optimization-cap", cl::init(100), cl::Hidden, | ||||||||||
138 | cl::desc("Enable imprecision in LICM in pathological cases, in exchange " | ||||||||||
139 | "for faster compile. Caps the MemorySSA clobbering calls.")); | ||||||||||
140 | |||||||||||
141 | // Experimentally, memory promotion carries less importance than sinking and | ||||||||||
142 | // hoisting. Limit when we do promotion when using MemorySSA, in order to save | ||||||||||
143 | // compile time. | ||||||||||
144 | cl::opt<unsigned> llvm::SetLicmMssaNoAccForPromotionCap( | ||||||||||
145 | "licm-mssa-max-acc-promotion", cl::init(250), cl::Hidden, | ||||||||||
146 | cl::desc("[LICM & MemorySSA] When MSSA in LICM is disabled, this has no " | ||||||||||
147 | "effect. When MSSA in LICM is enabled, then this is the maximum " | ||||||||||
148 | "number of accesses allowed to be present in a loop in order to " | ||||||||||
149 | "enable memory promotion.")); | ||||||||||
150 | |||||||||||
151 | static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI); | ||||||||||
152 | static bool isNotUsedOrFreeInLoop(const Instruction &I, const Loop *CurLoop, | ||||||||||
153 | const LoopSafetyInfo *SafetyInfo, | ||||||||||
154 | TargetTransformInfo *TTI, bool &FreeInLoop); | ||||||||||
155 | static void hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop, | ||||||||||
156 | BasicBlock *Dest, ICFLoopSafetyInfo *SafetyInfo, | ||||||||||
157 | MemorySSAUpdater *MSSAU, ScalarEvolution *SE, | ||||||||||
158 | OptimizationRemarkEmitter *ORE); | ||||||||||
159 | static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT, | ||||||||||
160 | BlockFrequencyInfo *BFI, const Loop *CurLoop, | ||||||||||
161 | ICFLoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU, | ||||||||||
162 | OptimizationRemarkEmitter *ORE); | ||||||||||
163 | static bool isSafeToExecuteUnconditionally(Instruction &Inst, | ||||||||||
164 | const DominatorTree *DT, | ||||||||||
165 | const Loop *CurLoop, | ||||||||||
166 | const LoopSafetyInfo *SafetyInfo, | ||||||||||
167 | OptimizationRemarkEmitter *ORE, | ||||||||||
168 | const Instruction *CtxI = nullptr); | ||||||||||
169 | static bool pointerInvalidatedByLoop(MemoryLocation MemLoc, | ||||||||||
170 | AliasSetTracker *CurAST, Loop *CurLoop, | ||||||||||
171 | AAResults *AA); | ||||||||||
172 | static bool pointerInvalidatedByLoopWithMSSA(MemorySSA *MSSA, MemoryUse *MU, | ||||||||||
173 | Loop *CurLoop, Instruction &I, | ||||||||||
174 | SinkAndHoistLICMFlags &Flags); | ||||||||||
175 | static bool pointerInvalidatedByBlockWithMSSA(BasicBlock &BB, MemorySSA &MSSA, | ||||||||||
176 | MemoryUse &MU); | ||||||||||
177 | static Instruction *cloneInstructionInExitBlock( | ||||||||||
178 | Instruction &I, BasicBlock &ExitBlock, PHINode &PN, const LoopInfo *LI, | ||||||||||
179 | const LoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU); | ||||||||||
180 | |||||||||||
181 | static void eraseInstruction(Instruction &I, ICFLoopSafetyInfo &SafetyInfo, | ||||||||||
182 | AliasSetTracker *AST, MemorySSAUpdater *MSSAU); | ||||||||||
183 | |||||||||||
184 | static void moveInstructionBefore(Instruction &I, Instruction &Dest, | ||||||||||
185 | ICFLoopSafetyInfo &SafetyInfo, | ||||||||||
186 | MemorySSAUpdater *MSSAU, ScalarEvolution *SE); | ||||||||||
187 | |||||||||||
188 | namespace { | ||||||||||
189 | struct LoopInvariantCodeMotion { | ||||||||||
190 | bool runOnLoop(Loop *L, AAResults *AA, LoopInfo *LI, DominatorTree *DT, | ||||||||||
191 | BlockFrequencyInfo *BFI, TargetLibraryInfo *TLI, | ||||||||||
192 | TargetTransformInfo *TTI, ScalarEvolution *SE, MemorySSA *MSSA, | ||||||||||
193 | OptimizationRemarkEmitter *ORE); | ||||||||||
194 | |||||||||||
195 | LoopInvariantCodeMotion(unsigned LicmMssaOptCap, | ||||||||||
196 | unsigned LicmMssaNoAccForPromotionCap) | ||||||||||
197 | : LicmMssaOptCap(LicmMssaOptCap), | ||||||||||
198 | LicmMssaNoAccForPromotionCap(LicmMssaNoAccForPromotionCap) {} | ||||||||||
199 | |||||||||||
200 | private: | ||||||||||
201 | unsigned LicmMssaOptCap; | ||||||||||
202 | unsigned LicmMssaNoAccForPromotionCap; | ||||||||||
203 | |||||||||||
204 | std::unique_ptr<AliasSetTracker> | ||||||||||
205 | collectAliasInfoForLoop(Loop *L, LoopInfo *LI, AAResults *AA); | ||||||||||
206 | std::unique_ptr<AliasSetTracker> | ||||||||||
207 | collectAliasInfoForLoopWithMSSA(Loop *L, AAResults *AA, | ||||||||||
208 | MemorySSAUpdater *MSSAU); | ||||||||||
209 | }; | ||||||||||
210 | |||||||||||
211 | struct LegacyLICMPass : public LoopPass { | ||||||||||
212 | static char ID; // Pass identification, replacement for typeid | ||||||||||
213 | LegacyLICMPass( | ||||||||||
214 | unsigned LicmMssaOptCap = SetLicmMssaOptCap, | ||||||||||
215 | unsigned LicmMssaNoAccForPromotionCap = SetLicmMssaNoAccForPromotionCap) | ||||||||||
216 | : LoopPass(ID), LICM(LicmMssaOptCap, LicmMssaNoAccForPromotionCap) { | ||||||||||
217 | initializeLegacyLICMPassPass(*PassRegistry::getPassRegistry()); | ||||||||||
218 | } | ||||||||||
219 | |||||||||||
220 | bool runOnLoop(Loop *L, LPPassManager &LPM) override { | ||||||||||
221 | if (skipLoop(L)) | ||||||||||
222 | return false; | ||||||||||
223 | |||||||||||
224 | LLVM_DEBUG(dbgs() << "Perform LICM on Loop with header at block "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "Perform LICM on Loop with header at block " << L->getHeader()->getNameOrAsOperand() << "\n"; } } while (false) | ||||||||||
225 | << L->getHeader()->getNameOrAsOperand() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "Perform LICM on Loop with header at block " << L->getHeader()->getNameOrAsOperand() << "\n"; } } while (false); | ||||||||||
226 | |||||||||||
227 | auto *SE = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>(); | ||||||||||
228 | MemorySSA *MSSA = EnableMSSALoopDependency | ||||||||||
229 | ? (&getAnalysis<MemorySSAWrapperPass>().getMSSA()) | ||||||||||
230 | : nullptr; | ||||||||||
231 | bool hasProfileData = L->getHeader()->getParent()->hasProfileData(); | ||||||||||
232 | BlockFrequencyInfo *BFI = | ||||||||||
233 | hasProfileData ? &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI() | ||||||||||
234 | : nullptr; | ||||||||||
235 | // For the old PM, we can't use OptimizationRemarkEmitter as an analysis | ||||||||||
236 | // pass. Function analyses need to be preserved across loop transformations | ||||||||||
237 | // but ORE cannot be preserved (see comment before the pass definition). | ||||||||||
238 | OptimizationRemarkEmitter ORE(L->getHeader()->getParent()); | ||||||||||
239 | return LICM.runOnLoop( | ||||||||||
240 | L, &getAnalysis<AAResultsWrapperPass>().getAAResults(), | ||||||||||
241 | &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(), | ||||||||||
242 | &getAnalysis<DominatorTreeWrapperPass>().getDomTree(), BFI, | ||||||||||
243 | &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI( | ||||||||||
244 | *L->getHeader()->getParent()), | ||||||||||
245 | &getAnalysis<TargetTransformInfoWrapperPass>().getTTI( | ||||||||||
246 | *L->getHeader()->getParent()), | ||||||||||
247 | SE ? &SE->getSE() : nullptr, MSSA, &ORE); | ||||||||||
248 | } | ||||||||||
249 | |||||||||||
250 | /// This transformation requires natural loop information & requires that | ||||||||||
251 | /// loop preheaders be inserted into the CFG... | ||||||||||
252 | /// | ||||||||||
253 | void getAnalysisUsage(AnalysisUsage &AU) const override { | ||||||||||
254 | AU.addPreserved<DominatorTreeWrapperPass>(); | ||||||||||
255 | AU.addPreserved<LoopInfoWrapperPass>(); | ||||||||||
256 | AU.addRequired<TargetLibraryInfoWrapperPass>(); | ||||||||||
257 | if (EnableMSSALoopDependency) { | ||||||||||
258 | AU.addRequired<MemorySSAWrapperPass>(); | ||||||||||
259 | AU.addPreserved<MemorySSAWrapperPass>(); | ||||||||||
260 | } | ||||||||||
261 | AU.addRequired<TargetTransformInfoWrapperPass>(); | ||||||||||
262 | getLoopAnalysisUsage(AU); | ||||||||||
263 | LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU); | ||||||||||
264 | AU.addPreserved<LazyBlockFrequencyInfoPass>(); | ||||||||||
265 | AU.addPreserved<LazyBranchProbabilityInfoPass>(); | ||||||||||
266 | } | ||||||||||
267 | |||||||||||
268 | private: | ||||||||||
269 | LoopInvariantCodeMotion LICM; | ||||||||||
270 | }; | ||||||||||
271 | } // namespace | ||||||||||
272 | |||||||||||
273 | PreservedAnalyses LICMPass::run(Loop &L, LoopAnalysisManager &AM, | ||||||||||
274 | LoopStandardAnalysisResults &AR, LPMUpdater &) { | ||||||||||
275 | // For the new PM, we also can't use OptimizationRemarkEmitter as an analysis | ||||||||||
276 | // pass. Function analyses need to be preserved across loop transformations | ||||||||||
277 | // but ORE cannot be preserved (see comment before the pass definition). | ||||||||||
278 | OptimizationRemarkEmitter ORE(L.getHeader()->getParent()); | ||||||||||
279 | |||||||||||
280 | LoopInvariantCodeMotion LICM(LicmMssaOptCap, LicmMssaNoAccForPromotionCap); | ||||||||||
281 | if (!LICM.runOnLoop(&L, &AR.AA, &AR.LI, &AR.DT, AR.BFI, &AR.TLI, &AR.TTI, | ||||||||||
282 | &AR.SE, AR.MSSA, &ORE)) | ||||||||||
283 | return PreservedAnalyses::all(); | ||||||||||
284 | |||||||||||
285 | auto PA = getLoopPassPreservedAnalyses(); | ||||||||||
286 | |||||||||||
287 | PA.preserve<DominatorTreeAnalysis>(); | ||||||||||
288 | PA.preserve<LoopAnalysis>(); | ||||||||||
289 | if (AR.MSSA) | ||||||||||
290 | PA.preserve<MemorySSAAnalysis>(); | ||||||||||
291 | |||||||||||
292 | return PA; | ||||||||||
293 | } | ||||||||||
294 | |||||||||||
295 | char LegacyLICMPass::ID = 0; | ||||||||||
296 | INITIALIZE_PASS_BEGIN(LegacyLICMPass, "licm", "Loop Invariant Code Motion",static void *initializeLegacyLICMPassPassOnce(PassRegistry & Registry) { | ||||||||||
297 | false, false)static void *initializeLegacyLICMPassPassOnce(PassRegistry & Registry) { | ||||||||||
298 | INITIALIZE_PASS_DEPENDENCY(LoopPass)initializeLoopPassPass(Registry); | ||||||||||
299 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)initializeTargetLibraryInfoWrapperPassPass(Registry); | ||||||||||
300 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)initializeTargetTransformInfoWrapperPassPass(Registry); | ||||||||||
301 | INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)initializeMemorySSAWrapperPassPass(Registry); | ||||||||||
302 | INITIALIZE_PASS_DEPENDENCY(LazyBFIPass)initializeLazyBFIPassPass(Registry); | ||||||||||
303 | INITIALIZE_PASS_END(LegacyLICMPass, "licm", "Loop Invariant Code Motion", false,PassInfo *PI = new PassInfo( "Loop Invariant Code Motion", "licm" , &LegacyLICMPass::ID, PassInfo::NormalCtor_t(callDefaultCtor <LegacyLICMPass>), false, false); Registry.registerPass (*PI, true); return PI; } static llvm::once_flag InitializeLegacyLICMPassPassFlag ; void llvm::initializeLegacyLICMPassPass(PassRegistry &Registry ) { llvm::call_once(InitializeLegacyLICMPassPassFlag, initializeLegacyLICMPassPassOnce , std::ref(Registry)); } | ||||||||||
304 | 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)); } | ||||||||||
305 | |||||||||||
306 | Pass *llvm::createLICMPass() { return new LegacyLICMPass(); } | ||||||||||
307 | Pass *llvm::createLICMPass(unsigned LicmMssaOptCap, | ||||||||||
308 | unsigned LicmMssaNoAccForPromotionCap) { | ||||||||||
309 | return new LegacyLICMPass(LicmMssaOptCap, LicmMssaNoAccForPromotionCap); | ||||||||||
310 | } | ||||||||||
311 | |||||||||||
312 | llvm::SinkAndHoistLICMFlags::SinkAndHoistLICMFlags(bool IsSink, Loop *L, | ||||||||||
313 | MemorySSA *MSSA) | ||||||||||
314 | : SinkAndHoistLICMFlags(SetLicmMssaOptCap, SetLicmMssaNoAccForPromotionCap, | ||||||||||
315 | IsSink, L, MSSA) {} | ||||||||||
316 | |||||||||||
317 | llvm::SinkAndHoistLICMFlags::SinkAndHoistLICMFlags( | ||||||||||
318 | unsigned LicmMssaOptCap, unsigned LicmMssaNoAccForPromotionCap, bool IsSink, | ||||||||||
319 | Loop *L, MemorySSA *MSSA) | ||||||||||
320 | : LicmMssaOptCap(LicmMssaOptCap), | ||||||||||
321 | LicmMssaNoAccForPromotionCap(LicmMssaNoAccForPromotionCap), | ||||||||||
322 | IsSink(IsSink) { | ||||||||||
323 | assert(((L != nullptr) == (MSSA != nullptr)) &&((((L != nullptr) == (MSSA != nullptr)) && "Unexpected values for SinkAndHoistLICMFlags" ) ? static_cast<void> (0) : __assert_fail ("((L != nullptr) == (MSSA != nullptr)) && \"Unexpected values for SinkAndHoistLICMFlags\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 324, __PRETTY_FUNCTION__)) | ||||||||||
324 | "Unexpected values for SinkAndHoistLICMFlags")((((L != nullptr) == (MSSA != nullptr)) && "Unexpected values for SinkAndHoistLICMFlags" ) ? static_cast<void> (0) : __assert_fail ("((L != nullptr) == (MSSA != nullptr)) && \"Unexpected values for SinkAndHoistLICMFlags\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 324, __PRETTY_FUNCTION__)); | ||||||||||
325 | if (!MSSA) | ||||||||||
326 | return; | ||||||||||
327 | |||||||||||
328 | unsigned AccessCapCount = 0; | ||||||||||
329 | for (auto *BB : L->getBlocks()) | ||||||||||
330 | if (const auto *Accesses = MSSA->getBlockAccesses(BB)) | ||||||||||
331 | for (const auto &MA : *Accesses) { | ||||||||||
332 | (void)MA; | ||||||||||
333 | ++AccessCapCount; | ||||||||||
334 | if (AccessCapCount > LicmMssaNoAccForPromotionCap) { | ||||||||||
335 | NoOfMemAccTooLarge = true; | ||||||||||
336 | return; | ||||||||||
337 | } | ||||||||||
338 | } | ||||||||||
339 | } | ||||||||||
340 | |||||||||||
341 | /// Hoist expressions out of the specified loop. Note, alias info for inner | ||||||||||
342 | /// loop is not preserved so it is not a good idea to run LICM multiple | ||||||||||
343 | /// times on one loop. | ||||||||||
344 | bool LoopInvariantCodeMotion::runOnLoop( | ||||||||||
345 | Loop *L, AAResults *AA, LoopInfo *LI, DominatorTree *DT, | ||||||||||
346 | BlockFrequencyInfo *BFI, TargetLibraryInfo *TLI, TargetTransformInfo *TTI, | ||||||||||
347 | ScalarEvolution *SE, MemorySSA *MSSA, OptimizationRemarkEmitter *ORE) { | ||||||||||
348 | bool Changed = false; | ||||||||||
349 | |||||||||||
350 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 350, __PRETTY_FUNCTION__)); | ||||||||||
351 | |||||||||||
352 | // If this loop has metadata indicating that LICM is not to be performed then | ||||||||||
353 | // just exit. | ||||||||||
354 | if (hasDisableLICMTransformsHint(L)) { | ||||||||||
355 | return false; | ||||||||||
356 | } | ||||||||||
357 | |||||||||||
358 | std::unique_ptr<AliasSetTracker> CurAST; | ||||||||||
359 | std::unique_ptr<MemorySSAUpdater> MSSAU; | ||||||||||
360 | std::unique_ptr<SinkAndHoistLICMFlags> Flags; | ||||||||||
361 | |||||||||||
362 | if (!MSSA) { | ||||||||||
363 | 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); | ||||||||||
364 | CurAST = collectAliasInfoForLoop(L, LI, AA); | ||||||||||
365 | Flags = std::make_unique<SinkAndHoistLICMFlags>( | ||||||||||
366 | LicmMssaOptCap, LicmMssaNoAccForPromotionCap, /*IsSink=*/true); | ||||||||||
367 | } else { | ||||||||||
368 | LLVM_DEBUG(dbgs() << "LICM: Using MemorySSA.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM: Using MemorySSA.\n"; } } while (false); | ||||||||||
369 | MSSAU = std::make_unique<MemorySSAUpdater>(MSSA); | ||||||||||
370 | Flags = std::make_unique<SinkAndHoistLICMFlags>( | ||||||||||
371 | LicmMssaOptCap, LicmMssaNoAccForPromotionCap, /*IsSink=*/true, L, MSSA); | ||||||||||
372 | } | ||||||||||
373 | |||||||||||
374 | // Get the preheader block to move instructions into... | ||||||||||
375 | BasicBlock *Preheader = L->getLoopPreheader(); | ||||||||||
376 | |||||||||||
377 | // Compute loop safety information. | ||||||||||
378 | ICFLoopSafetyInfo SafetyInfo; | ||||||||||
379 | SafetyInfo.computeLoopSafetyInfo(L); | ||||||||||
380 | |||||||||||
381 | // We want to visit all of the instructions in this loop... that are not parts | ||||||||||
382 | // of our subloops (they have already had their invariants hoisted out of | ||||||||||
383 | // their loop, into this loop, so there is no need to process the BODIES of | ||||||||||
384 | // the subloops). | ||||||||||
385 | // | ||||||||||
386 | // Traverse the body of the loop in depth first order on the dominator tree so | ||||||||||
387 | // that we are guaranteed to see definitions before we see uses. This allows | ||||||||||
388 | // us to sink instructions in one pass, without iteration. After sinking | ||||||||||
389 | // instructions, we perform another pass to hoist them out of the loop. | ||||||||||
390 | if (L->hasDedicatedExits()) | ||||||||||
391 | Changed |= | ||||||||||
392 | sinkRegion(DT->getNode(L->getHeader()), AA, LI, DT, BFI, TLI, TTI, L, | ||||||||||
393 | CurAST.get(), MSSAU.get(), &SafetyInfo, *Flags.get(), ORE); | ||||||||||
394 | Flags->setIsSink(false); | ||||||||||
395 | if (Preheader) | ||||||||||
396 | Changed |= hoistRegion(DT->getNode(L->getHeader()), AA, LI, DT, BFI, TLI, L, | ||||||||||
397 | CurAST.get(), MSSAU.get(), SE, &SafetyInfo, | ||||||||||
398 | *Flags.get(), ORE); | ||||||||||
399 | |||||||||||
400 | // Now that all loop invariants have been removed from the loop, promote any | ||||||||||
401 | // memory references to scalars that we can. | ||||||||||
402 | // Don't sink stores from loops without dedicated block exits. Exits | ||||||||||
403 | // containing indirect branches are not transformed by loop simplify, | ||||||||||
404 | // make sure we catch that. An additional load may be generated in the | ||||||||||
405 | // preheader for SSA updater, so also avoid sinking when no preheader | ||||||||||
406 | // is available. | ||||||||||
407 | if (!DisablePromotion && Preheader && L->hasDedicatedExits() && | ||||||||||
408 | !Flags->tooManyMemoryAccesses()) { | ||||||||||
409 | // Figure out the loop exits and their insertion points | ||||||||||
410 | SmallVector<BasicBlock *, 8> ExitBlocks; | ||||||||||
411 | L->getUniqueExitBlocks(ExitBlocks); | ||||||||||
412 | |||||||||||
413 | // We can't insert into a catchswitch. | ||||||||||
414 | bool HasCatchSwitch = llvm::any_of(ExitBlocks, [](BasicBlock *Exit) { | ||||||||||
415 | return isa<CatchSwitchInst>(Exit->getTerminator()); | ||||||||||
416 | }); | ||||||||||
417 | |||||||||||
418 | if (!HasCatchSwitch) { | ||||||||||
419 | SmallVector<Instruction *, 8> InsertPts; | ||||||||||
420 | SmallVector<MemoryAccess *, 8> MSSAInsertPts; | ||||||||||
421 | InsertPts.reserve(ExitBlocks.size()); | ||||||||||
422 | if (MSSAU) | ||||||||||
423 | MSSAInsertPts.reserve(ExitBlocks.size()); | ||||||||||
424 | for (BasicBlock *ExitBlock : ExitBlocks) { | ||||||||||
425 | InsertPts.push_back(&*ExitBlock->getFirstInsertionPt()); | ||||||||||
426 | if (MSSAU) | ||||||||||
427 | MSSAInsertPts.push_back(nullptr); | ||||||||||
428 | } | ||||||||||
429 | |||||||||||
430 | PredIteratorCache PIC; | ||||||||||
431 | |||||||||||
432 | bool Promoted = false; | ||||||||||
433 | |||||||||||
434 | // Build an AST using MSSA. | ||||||||||
435 | if (!CurAST.get()) | ||||||||||
436 | CurAST = collectAliasInfoForLoopWithMSSA(L, AA, MSSAU.get()); | ||||||||||
437 | |||||||||||
438 | // Loop over all of the alias sets in the tracker object. | ||||||||||
439 | for (AliasSet &AS : *CurAST) { | ||||||||||
440 | // We can promote this alias set if it has a store, if it is a "Must" | ||||||||||
441 | // alias set, if the pointer is loop invariant, and if we are not | ||||||||||
442 | // eliminating any volatile loads or stores. | ||||||||||
443 | if (AS.isForwardingAliasSet() || !AS.isMod() || !AS.isMustAlias() || | ||||||||||
444 | !L->isLoopInvariant(AS.begin()->getValue())) | ||||||||||
445 | continue; | ||||||||||
446 | |||||||||||
447 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 449, __PRETTY_FUNCTION__)) | ||||||||||
448 | !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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 449, __PRETTY_FUNCTION__)) | ||||||||||
449 | "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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 449, __PRETTY_FUNCTION__)); | ||||||||||
450 | |||||||||||
451 | SmallSetVector<Value *, 8> PointerMustAliases; | ||||||||||
452 | for (const auto &ASI : AS) | ||||||||||
453 | PointerMustAliases.insert(ASI.getValue()); | ||||||||||
454 | |||||||||||
455 | Promoted |= promoteLoopAccessesToScalars( | ||||||||||
456 | PointerMustAliases, ExitBlocks, InsertPts, MSSAInsertPts, PIC, LI, | ||||||||||
457 | DT, TLI, L, CurAST.get(), MSSAU.get(), &SafetyInfo, ORE); | ||||||||||
458 | } | ||||||||||
459 | |||||||||||
460 | // Once we have promoted values across the loop body we have to | ||||||||||
461 | // recursively reform LCSSA as any nested loop may now have values defined | ||||||||||
462 | // within the loop used in the outer loop. | ||||||||||
463 | // FIXME: This is really heavy handed. It would be a bit better to use an | ||||||||||
464 | // SSAUpdater strategy during promotion that was LCSSA aware and reformed | ||||||||||
465 | // it as it went. | ||||||||||
466 | if (Promoted) | ||||||||||
467 | formLCSSARecursively(*L, *DT, LI, SE); | ||||||||||
468 | |||||||||||
469 | Changed |= Promoted; | ||||||||||
470 | } | ||||||||||
471 | } | ||||||||||
472 | |||||||||||
473 | // Check that neither this loop nor its parent have had LCSSA broken. LICM is | ||||||||||
474 | // specifically moving instructions across the loop boundary and so it is | ||||||||||
475 | // especially in need of sanity checking here. | ||||||||||
476 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 476, __PRETTY_FUNCTION__)); | ||||||||||
477 | assert((L->isOutermost() || L->getParentLoop()->isLCSSAForm(*DT)) &&(((L->isOutermost() || L->getParentLoop()->isLCSSAForm (*DT)) && "Parent loop not left in LCSSA form after LICM!" ) ? static_cast<void> (0) : __assert_fail ("(L->isOutermost() || L->getParentLoop()->isLCSSAForm(*DT)) && \"Parent loop not left in LCSSA form after LICM!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 478, __PRETTY_FUNCTION__)) | ||||||||||
478 | "Parent loop not left in LCSSA form after LICM!")(((L->isOutermost() || L->getParentLoop()->isLCSSAForm (*DT)) && "Parent loop not left in LCSSA form after LICM!" ) ? static_cast<void> (0) : __assert_fail ("(L->isOutermost() || L->getParentLoop()->isLCSSAForm(*DT)) && \"Parent loop not left in LCSSA form after LICM!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 478, __PRETTY_FUNCTION__)); | ||||||||||
479 | |||||||||||
480 | if (MSSAU.get() && VerifyMemorySSA) | ||||||||||
481 | MSSAU->getMemorySSA()->verifyMemorySSA(); | ||||||||||
482 | |||||||||||
483 | if (Changed && SE) | ||||||||||
484 | SE->forgetLoopDispositions(L); | ||||||||||
485 | return Changed; | ||||||||||
486 | } | ||||||||||
487 | |||||||||||
488 | /// Walk the specified region of the CFG (defined by all blocks dominated by | ||||||||||
489 | /// the specified block, and that are in the current loop) in reverse depth | ||||||||||
490 | /// first order w.r.t the DominatorTree. This allows us to visit uses before | ||||||||||
491 | /// definitions, allowing us to sink a loop body in one pass without iteration. | ||||||||||
492 | /// | ||||||||||
493 | bool llvm::sinkRegion(DomTreeNode *N, AAResults *AA, LoopInfo *LI, | ||||||||||
494 | DominatorTree *DT, BlockFrequencyInfo *BFI, | ||||||||||
495 | TargetLibraryInfo *TLI, TargetTransformInfo *TTI, | ||||||||||
496 | Loop *CurLoop, AliasSetTracker *CurAST, | ||||||||||
497 | MemorySSAUpdater *MSSAU, ICFLoopSafetyInfo *SafetyInfo, | ||||||||||
498 | SinkAndHoistLICMFlags &Flags, | ||||||||||
499 | OptimizationRemarkEmitter *ORE) { | ||||||||||
500 | |||||||||||
501 | // Verify inputs. | ||||||||||
502 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 504, __PRETTY_FUNCTION__)) | ||||||||||
503 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 504, __PRETTY_FUNCTION__)) | ||||||||||
504 | "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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 504, __PRETTY_FUNCTION__)); | ||||||||||
505 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 506, __PRETTY_FUNCTION__)) | ||||||||||
506 | "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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 506, __PRETTY_FUNCTION__)); | ||||||||||
507 | |||||||||||
508 | // We want to visit children before parents. We will enque all the parents | ||||||||||
509 | // before their children in the worklist and process the worklist in reverse | ||||||||||
510 | // order. | ||||||||||
511 | SmallVector<DomTreeNode *, 16> Worklist = collectChildrenInLoop(N, CurLoop); | ||||||||||
512 | |||||||||||
513 | bool Changed = false; | ||||||||||
514 | for (DomTreeNode *DTN : reverse(Worklist)) { | ||||||||||
515 | BasicBlock *BB = DTN->getBlock(); | ||||||||||
516 | // Only need to process the contents of this block if it is not part of a | ||||||||||
517 | // subloop (which would already have been processed). | ||||||||||
518 | if (inSubLoop(BB, CurLoop, LI)) | ||||||||||
519 | continue; | ||||||||||
520 | |||||||||||
521 | for (BasicBlock::iterator II = BB->end(); II != BB->begin();) { | ||||||||||
522 | Instruction &I = *--II; | ||||||||||
523 | |||||||||||
524 | // If the instruction is dead, we would try to sink it because it isn't | ||||||||||
525 | // used in the loop, instead, just delete it. | ||||||||||
526 | if (isInstructionTriviallyDead(&I, TLI)) { | ||||||||||
527 | 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); | ||||||||||
528 | salvageKnowledge(&I); | ||||||||||
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 (!I.mayHaveSideEffects() && | ||||||||||
543 | isNotUsedOrFreeInLoop(I, CurLoop, SafetyInfo, TTI, FreeInLoop) && | ||||||||||
544 | canSinkOrHoistInst(I, AA, DT, CurLoop, CurAST, MSSAU, true, &Flags, | ||||||||||
545 | ORE)) { | ||||||||||
546 | if (sink(I, LI, DT, BFI, CurLoop, SafetyInfo, MSSAU, ORE)) { | ||||||||||
547 | if (!FreeInLoop) { | ||||||||||
548 | ++II; | ||||||||||
549 | salvageDebugInfo(I); | ||||||||||
550 | eraseInstruction(I, *SafetyInfo, CurAST, MSSAU); | ||||||||||
551 | } | ||||||||||
552 | Changed = true; | ||||||||||
553 | } | ||||||||||
554 | } | ||||||||||
555 | } | ||||||||||
556 | } | ||||||||||
557 | if (MSSAU && VerifyMemorySSA) | ||||||||||
558 | MSSAU->getMemorySSA()->verifyMemorySSA(); | ||||||||||
559 | return Changed; | ||||||||||
560 | } | ||||||||||
561 | |||||||||||
562 | namespace { | ||||||||||
563 | // This is a helper class for hoistRegion to make it able to hoist control flow | ||||||||||
564 | // in order to be able to hoist phis. The way this works is that we initially | ||||||||||
565 | // start hoisting to the loop preheader, and when we see a loop invariant branch | ||||||||||
566 | // we make note of this. When we then come to hoist an instruction that's | ||||||||||
567 | // conditional on such a branch we duplicate the branch and the relevant control | ||||||||||
568 | // flow, then hoist the instruction into the block corresponding to its original | ||||||||||
569 | // block in the duplicated control flow. | ||||||||||
570 | class ControlFlowHoister { | ||||||||||
571 | private: | ||||||||||
572 | // Information about the loop we are hoisting from | ||||||||||
573 | LoopInfo *LI; | ||||||||||
574 | DominatorTree *DT; | ||||||||||
575 | Loop *CurLoop; | ||||||||||
576 | MemorySSAUpdater *MSSAU; | ||||||||||
577 | |||||||||||
578 | // A map of blocks in the loop to the block their instructions will be hoisted | ||||||||||
579 | // to. | ||||||||||
580 | DenseMap<BasicBlock *, BasicBlock *> HoistDestinationMap; | ||||||||||
581 | |||||||||||
582 | // The branches that we can hoist, mapped to the block that marks a | ||||||||||
583 | // convergence point of their control flow. | ||||||||||
584 | DenseMap<BranchInst *, BasicBlock *> HoistableBranches; | ||||||||||
585 | |||||||||||
586 | public: | ||||||||||
587 | ControlFlowHoister(LoopInfo *LI, DominatorTree *DT, Loop *CurLoop, | ||||||||||
588 | MemorySSAUpdater *MSSAU) | ||||||||||
589 | : LI(LI), DT(DT), CurLoop(CurLoop), MSSAU(MSSAU) {} | ||||||||||
590 | |||||||||||
591 | void registerPossiblyHoistableBranch(BranchInst *BI) { | ||||||||||
592 | // We can only hoist conditional branches with loop invariant operands. | ||||||||||
593 | if (!ControlFlowHoisting || !BI->isConditional() || | ||||||||||
594 | !CurLoop->hasLoopInvariantOperands(BI)) | ||||||||||
595 | return; | ||||||||||
596 | |||||||||||
597 | // The branch destinations need to be in the loop, and we don't gain | ||||||||||
598 | // anything by duplicating conditional branches with duplicate successors, | ||||||||||
599 | // as it's essentially the same as an unconditional branch. | ||||||||||
600 | BasicBlock *TrueDest = BI->getSuccessor(0); | ||||||||||
601 | BasicBlock *FalseDest = BI->getSuccessor(1); | ||||||||||
602 | if (!CurLoop->contains(TrueDest) || !CurLoop->contains(FalseDest) || | ||||||||||
603 | TrueDest == FalseDest) | ||||||||||
604 | return; | ||||||||||
605 | |||||||||||
606 | // We can hoist BI if one branch destination is the successor of the other, | ||||||||||
607 | // or both have common successor which we check by seeing if the | ||||||||||
608 | // intersection of their successors is non-empty. | ||||||||||
609 | // TODO: This could be expanded to allowing branches where both ends | ||||||||||
610 | // eventually converge to a single block. | ||||||||||
611 | SmallPtrSet<BasicBlock *, 4> TrueDestSucc, FalseDestSucc; | ||||||||||
612 | TrueDestSucc.insert(succ_begin(TrueDest), succ_end(TrueDest)); | ||||||||||
613 | FalseDestSucc.insert(succ_begin(FalseDest), succ_end(FalseDest)); | ||||||||||
614 | BasicBlock *CommonSucc = nullptr; | ||||||||||
615 | if (TrueDestSucc.count(FalseDest)) { | ||||||||||
616 | CommonSucc = FalseDest; | ||||||||||
617 | } else if (FalseDestSucc.count(TrueDest)) { | ||||||||||
618 | CommonSucc = TrueDest; | ||||||||||
619 | } else { | ||||||||||
620 | set_intersect(TrueDestSucc, FalseDestSucc); | ||||||||||
621 | // If there's one common successor use that. | ||||||||||
622 | if (TrueDestSucc.size() == 1) | ||||||||||
623 | CommonSucc = *TrueDestSucc.begin(); | ||||||||||
624 | // If there's more than one pick whichever appears first in the block list | ||||||||||
625 | // (we can't use the value returned by TrueDestSucc.begin() as it's | ||||||||||
626 | // unpredicatable which element gets returned). | ||||||||||
627 | else if (!TrueDestSucc.empty()) { | ||||||||||
628 | Function *F = TrueDest->getParent(); | ||||||||||
629 | auto IsSucc = [&](BasicBlock &BB) { return TrueDestSucc.count(&BB); }; | ||||||||||
630 | auto It = llvm::find_if(*F, IsSucc); | ||||||||||
631 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 631, __PRETTY_FUNCTION__)); | ||||||||||
632 | CommonSucc = &*It; | ||||||||||
633 | } | ||||||||||
634 | } | ||||||||||
635 | // The common successor has to be dominated by the branch, as otherwise | ||||||||||
636 | // there will be some other path to the successor that will not be | ||||||||||
637 | // controlled by this branch so any phi we hoist would be controlled by the | ||||||||||
638 | // wrong condition. This also takes care of avoiding hoisting of loop back | ||||||||||
639 | // edges. | ||||||||||
640 | // TODO: In some cases this could be relaxed if the successor is dominated | ||||||||||
641 | // by another block that's been hoisted and we can guarantee that the | ||||||||||
642 | // control flow has been replicated exactly. | ||||||||||
643 | if (CommonSucc && DT->dominates(BI, CommonSucc)) | ||||||||||
644 | HoistableBranches[BI] = CommonSucc; | ||||||||||
645 | } | ||||||||||
646 | |||||||||||
647 | bool canHoistPHI(PHINode *PN) { | ||||||||||
648 | // The phi must have loop invariant operands. | ||||||||||
649 | if (!ControlFlowHoisting || !CurLoop->hasLoopInvariantOperands(PN)) | ||||||||||
650 | return false; | ||||||||||
651 | // We can hoist phis if the block they are in is the target of hoistable | ||||||||||
652 | // branches which cover all of the predecessors of the block. | ||||||||||
653 | SmallPtrSet<BasicBlock *, 8> PredecessorBlocks; | ||||||||||
654 | BasicBlock *BB = PN->getParent(); | ||||||||||
655 | for (BasicBlock *PredBB : predecessors(BB)) | ||||||||||
656 | PredecessorBlocks.insert(PredBB); | ||||||||||
657 | // If we have less predecessor blocks than predecessors then the phi will | ||||||||||
658 | // have more than one incoming value for the same block which we can't | ||||||||||
659 | // handle. | ||||||||||
660 | // TODO: This could be handled be erasing some of the duplicate incoming | ||||||||||
661 | // values. | ||||||||||
662 | if (PredecessorBlocks.size() != pred_size(BB)) | ||||||||||
663 | return false; | ||||||||||
664 | for (auto &Pair : HoistableBranches) { | ||||||||||
665 | if (Pair.second == BB) { | ||||||||||
666 | // Which blocks are predecessors via this branch depends on if the | ||||||||||
667 | // branch is triangle-like or diamond-like. | ||||||||||
668 | if (Pair.first->getSuccessor(0) == BB) { | ||||||||||
669 | PredecessorBlocks.erase(Pair.first->getParent()); | ||||||||||
670 | PredecessorBlocks.erase(Pair.first->getSuccessor(1)); | ||||||||||
671 | } else if (Pair.first->getSuccessor(1) == BB) { | ||||||||||
672 | PredecessorBlocks.erase(Pair.first->getParent()); | ||||||||||
673 | PredecessorBlocks.erase(Pair.first->getSuccessor(0)); | ||||||||||
674 | } else { | ||||||||||
675 | PredecessorBlocks.erase(Pair.first->getSuccessor(0)); | ||||||||||
676 | PredecessorBlocks.erase(Pair.first->getSuccessor(1)); | ||||||||||
677 | } | ||||||||||
678 | } | ||||||||||
679 | } | ||||||||||
680 | // PredecessorBlocks will now be empty if for every predecessor of BB we | ||||||||||
681 | // found a hoistable branch source. | ||||||||||
682 | return PredecessorBlocks.empty(); | ||||||||||
683 | } | ||||||||||
684 | |||||||||||
685 | BasicBlock *getOrCreateHoistedBlock(BasicBlock *BB) { | ||||||||||
686 | if (!ControlFlowHoisting) | ||||||||||
687 | return CurLoop->getLoopPreheader(); | ||||||||||
688 | // If BB has already been hoisted, return that | ||||||||||
689 | if (HoistDestinationMap.count(BB)) | ||||||||||
690 | return HoistDestinationMap[BB]; | ||||||||||
691 | |||||||||||
692 | // Check if this block is conditional based on a pending branch | ||||||||||
693 | auto HasBBAsSuccessor = | ||||||||||
694 | [&](DenseMap<BranchInst *, BasicBlock *>::value_type &Pair) { | ||||||||||
695 | return BB != Pair.second && (Pair.first->getSuccessor(0) == BB || | ||||||||||
696 | Pair.first->getSuccessor(1) == BB); | ||||||||||
697 | }; | ||||||||||
698 | auto It = llvm::find_if(HoistableBranches, 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 "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM using " << InitialPreheader ->getNameOrAsOperand() << " as hoist destination for " << BB->getNameOrAsOperand() << "\n"; } } while (false) | ||||||||||
704 | << InitialPreheader->getNameOrAsOperand()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM using " << InitialPreheader ->getNameOrAsOperand() << " as hoist destination for " << BB->getNameOrAsOperand() << "\n"; } } while (false) | ||||||||||
705 | << " as hoist destination for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM using " << InitialPreheader ->getNameOrAsOperand() << " as hoist destination for " << BB->getNameOrAsOperand() << "\n"; } } while (false) | ||||||||||
706 | << BB->getNameOrAsOperand() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM using " << InitialPreheader ->getNameOrAsOperand() << " as hoist destination for " << BB->getNameOrAsOperand() << "\n"; } } while (false); | ||||||||||
707 | HoistDestinationMap[BB] = InitialPreheader; | ||||||||||
708 | return InitialPreheader; | ||||||||||
709 | } | ||||||||||
710 | BranchInst *BI = It->first; | ||||||||||
711 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 713, __PRETTY_FUNCTION__)) | ||||||||||
712 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 713, __PRETTY_FUNCTION__)) | ||||||||||
713 | "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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 713, __PRETTY_FUNCTION__)); | ||||||||||
714 | |||||||||||
715 | LLVMContext &C = BB->getContext(); | ||||||||||
716 | BasicBlock *TrueDest = BI->getSuccessor(0); | ||||||||||
717 | BasicBlock *FalseDest = BI->getSuccessor(1); | ||||||||||
718 | BasicBlock *CommonSucc = HoistableBranches[BI]; | ||||||||||
719 | BasicBlock *HoistTarget = getOrCreateHoistedBlock(BI->getParent()); | ||||||||||
720 | |||||||||||
721 | // Create hoisted versions of blocks that currently don't have them | ||||||||||
722 | auto CreateHoistedBlock = [&](BasicBlock *Orig) { | ||||||||||
723 | if (HoistDestinationMap.count(Orig)) | ||||||||||
724 | return HoistDestinationMap[Orig]; | ||||||||||
725 | BasicBlock *New = | ||||||||||
726 | BasicBlock::Create(C, Orig->getName() + ".licm", Orig->getParent()); | ||||||||||
727 | HoistDestinationMap[Orig] = New; | ||||||||||
728 | DT->addNewBlock(New, HoistTarget); | ||||||||||
729 | if (CurLoop->getParentLoop()) | ||||||||||
730 | CurLoop->getParentLoop()->addBasicBlockToLoop(New, *LI); | ||||||||||
731 | ++NumCreatedBlocks; | ||||||||||
732 | 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) | ||||||||||
733 | << " 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) | ||||||||||
734 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM created " << New-> getName() << " as hoist destination for " << Orig ->getName() << "\n"; } } while (false); | ||||||||||
735 | return New; | ||||||||||
736 | }; | ||||||||||
737 | BasicBlock *HoistTrueDest = CreateHoistedBlock(TrueDest); | ||||||||||
738 | BasicBlock *HoistFalseDest = CreateHoistedBlock(FalseDest); | ||||||||||
739 | BasicBlock *HoistCommonSucc = CreateHoistedBlock(CommonSucc); | ||||||||||
740 | |||||||||||
741 | // Link up these blocks with branches. | ||||||||||
742 | if (!HoistCommonSucc->getTerminator()) { | ||||||||||
743 | // The new common successor we've generated will branch to whatever that | ||||||||||
744 | // hoist target branched to. | ||||||||||
745 | BasicBlock *TargetSucc = HoistTarget->getSingleSuccessor(); | ||||||||||
746 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 746, __PRETTY_FUNCTION__)); | ||||||||||
747 | HoistCommonSucc->moveBefore(TargetSucc); | ||||||||||
748 | BranchInst::Create(TargetSucc, HoistCommonSucc); | ||||||||||
749 | } | ||||||||||
750 | if (!HoistTrueDest->getTerminator()) { | ||||||||||
751 | HoistTrueDest->moveBefore(HoistCommonSucc); | ||||||||||
752 | BranchInst::Create(HoistCommonSucc, HoistTrueDest); | ||||||||||
753 | } | ||||||||||
754 | if (!HoistFalseDest->getTerminator()) { | ||||||||||
755 | HoistFalseDest->moveBefore(HoistCommonSucc); | ||||||||||
756 | BranchInst::Create(HoistCommonSucc, HoistFalseDest); | ||||||||||
757 | } | ||||||||||
758 | |||||||||||
759 | // If BI is being cloned to what was originally the preheader then | ||||||||||
760 | // HoistCommonSucc will now be the new preheader. | ||||||||||
761 | if (HoistTarget == InitialPreheader) { | ||||||||||
762 | // Phis in the loop header now need to use the new preheader. | ||||||||||
763 | InitialPreheader->replaceSuccessorsPhiUsesWith(HoistCommonSucc); | ||||||||||
764 | if (MSSAU) | ||||||||||
765 | MSSAU->wireOldPredecessorsToNewImmediatePredecessor( | ||||||||||
766 | HoistTarget->getSingleSuccessor(), HoistCommonSucc, {HoistTarget}); | ||||||||||
767 | // The new preheader dominates the loop header. | ||||||||||
768 | DomTreeNode *PreheaderNode = DT->getNode(HoistCommonSucc); | ||||||||||
769 | DomTreeNode *HeaderNode = DT->getNode(CurLoop->getHeader()); | ||||||||||
770 | DT->changeImmediateDominator(HeaderNode, PreheaderNode); | ||||||||||
771 | // The preheader hoist destination is now the new preheader, with the | ||||||||||
772 | // exception of the hoist destination of this branch. | ||||||||||
773 | for (auto &Pair : HoistDestinationMap) | ||||||||||
774 | if (Pair.second == InitialPreheader && Pair.first != BI->getParent()) | ||||||||||
775 | Pair.second = HoistCommonSucc; | ||||||||||
776 | } | ||||||||||
777 | |||||||||||
778 | // Now finally clone BI. | ||||||||||
779 | ReplaceInstWithInst( | ||||||||||
780 | HoistTarget->getTerminator(), | ||||||||||
781 | BranchInst::Create(HoistTrueDest, HoistFalseDest, BI->getCondition())); | ||||||||||
782 | ++NumClonedBranches; | ||||||||||
783 | |||||||||||
784 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 785, __PRETTY_FUNCTION__)) | ||||||||||
785 | "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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 785, __PRETTY_FUNCTION__)); | ||||||||||
786 | return HoistDestinationMap[BB]; | ||||||||||
787 | } | ||||||||||
788 | }; | ||||||||||
789 | } // namespace | ||||||||||
790 | |||||||||||
791 | // Hoisting/sinking instruction out of a loop isn't always beneficial. It's only | ||||||||||
792 | // only worthwhile if the destination block is actually colder than current | ||||||||||
793 | // block. | ||||||||||
794 | static bool worthSinkOrHoistInst(Instruction &I, BasicBlock *DstBlock, | ||||||||||
795 | OptimizationRemarkEmitter *ORE, | ||||||||||
796 | BlockFrequencyInfo *BFI) { | ||||||||||
797 | // Check block frequency only when runtime profile is available | ||||||||||
798 | // to avoid pathological cases. With static profile, lean towards | ||||||||||
799 | // hosting because it helps canonicalize the loop for vectorizer. | ||||||||||
800 | if (!DstBlock->getParent()->hasProfileData()) | ||||||||||
801 | return true; | ||||||||||
802 | |||||||||||
803 | if (!HoistSinkColdnessThreshold || !BFI) | ||||||||||
804 | return true; | ||||||||||
805 | |||||||||||
806 | BasicBlock *SrcBlock = I.getParent(); | ||||||||||
807 | if (BFI->getBlockFreq(DstBlock).getFrequency() / HoistSinkColdnessThreshold > | ||||||||||
808 | BFI->getBlockFreq(SrcBlock).getFrequency()) { | ||||||||||
809 | ORE->emit([&]() { | ||||||||||
810 | return OptimizationRemarkMissed(DEBUG_TYPE"licm", "SinkHoistInst", &I) | ||||||||||
811 | << "failed to sink or hoist instruction because containing block " | ||||||||||
812 | "has lower frequency than destination block"; | ||||||||||
813 | }); | ||||||||||
814 | return false; | ||||||||||
815 | } | ||||||||||
816 | |||||||||||
817 | return true; | ||||||||||
818 | } | ||||||||||
819 | |||||||||||
820 | /// Walk the specified region of the CFG (defined by all blocks dominated by | ||||||||||
821 | /// the specified block, and that are in the current loop) in depth first | ||||||||||
822 | /// order w.r.t the DominatorTree. This allows us to visit definitions before | ||||||||||
823 | /// uses, allowing us to hoist a loop body in one pass without iteration. | ||||||||||
824 | /// | ||||||||||
825 | bool llvm::hoistRegion(DomTreeNode *N, AAResults *AA, LoopInfo *LI, | ||||||||||
826 | DominatorTree *DT, BlockFrequencyInfo *BFI, | ||||||||||
827 | TargetLibraryInfo *TLI, Loop *CurLoop, | ||||||||||
828 | AliasSetTracker *CurAST, MemorySSAUpdater *MSSAU, | ||||||||||
829 | ScalarEvolution *SE, ICFLoopSafetyInfo *SafetyInfo, | ||||||||||
830 | SinkAndHoistLICMFlags &Flags, | ||||||||||
831 | OptimizationRemarkEmitter *ORE) { | ||||||||||
832 | // Verify inputs. | ||||||||||
833 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 835, __PRETTY_FUNCTION__)) | ||||||||||
834 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 835, __PRETTY_FUNCTION__)) | ||||||||||
835 | "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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 835, __PRETTY_FUNCTION__)); | ||||||||||
836 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 837, __PRETTY_FUNCTION__)) | ||||||||||
837 | "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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 837, __PRETTY_FUNCTION__)); | ||||||||||
838 | |||||||||||
839 | ControlFlowHoister CFH(LI, DT, CurLoop, MSSAU); | ||||||||||
840 | |||||||||||
841 | // Keep track of instructions that have been hoisted, as they may need to be | ||||||||||
842 | // re-hoisted if they end up not dominating all of their uses. | ||||||||||
843 | SmallVector<Instruction *, 16> HoistedInstructions; | ||||||||||
844 | |||||||||||
845 | // For PHI hoisting to work we need to hoist blocks before their successors. | ||||||||||
846 | // We can do this by iterating through the blocks in the loop in reverse | ||||||||||
847 | // post-order. | ||||||||||
848 | LoopBlocksRPO Worklist(CurLoop); | ||||||||||
849 | Worklist.perform(LI); | ||||||||||
850 | bool Changed = false; | ||||||||||
851 | for (BasicBlock *BB : Worklist) { | ||||||||||
852 | // Only need to process the contents of this block if it is not part of a | ||||||||||
853 | // subloop (which would already have been processed). | ||||||||||
854 | if (inSubLoop(BB, CurLoop, LI)) | ||||||||||
855 | continue; | ||||||||||
856 | |||||||||||
857 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) { | ||||||||||
858 | Instruction &I = *II++; | ||||||||||
859 | // Try constant folding this instruction. If all the operands are | ||||||||||
860 | // constants, it is technically hoistable, but it would be better to | ||||||||||
861 | // just fold it. | ||||||||||
862 | if (Constant *C = ConstantFoldInstruction( | ||||||||||
863 | &I, I.getModule()->getDataLayout(), TLI)) { | ||||||||||
864 | LLVM_DEBUG(dbgs() << "LICM folding inst: " << I << " --> " << *Cdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM folding inst: " << I << " --> " << *C << '\n'; } } while (false) | ||||||||||
865 | << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM folding inst: " << I << " --> " << *C << '\n'; } } while (false); | ||||||||||
866 | if (CurAST) | ||||||||||
867 | CurAST->copyValue(&I, C); | ||||||||||
868 | // FIXME MSSA: Such replacements may make accesses unoptimized (D51960). | ||||||||||
869 | I.replaceAllUsesWith(C); | ||||||||||
870 | if (isInstructionTriviallyDead(&I, TLI)) | ||||||||||
871 | eraseInstruction(I, *SafetyInfo, CurAST, MSSAU); | ||||||||||
872 | Changed = true; | ||||||||||
873 | continue; | ||||||||||
874 | } | ||||||||||
875 | |||||||||||
876 | // Try hoisting the instruction out to the preheader. We can only do | ||||||||||
877 | // this if all of the operands of the instruction are loop invariant and | ||||||||||
878 | // if it is safe to hoist the instruction. We also check block frequency | ||||||||||
879 | // to make sure instruction only gets hoisted into colder blocks. | ||||||||||
880 | // TODO: It may be safe to hoist if we are hoisting to a conditional block | ||||||||||
881 | // and we have accurately duplicated the control flow from the loop header | ||||||||||
882 | // to that block. | ||||||||||
883 | if (CurLoop->hasLoopInvariantOperands(&I) && | ||||||||||
884 | canSinkOrHoistInst(I, AA, DT, CurLoop, CurAST, MSSAU, true, &Flags, | ||||||||||
885 | ORE) && | ||||||||||
886 | worthSinkOrHoistInst(I, CurLoop->getLoopPreheader(), ORE, BFI) && | ||||||||||
887 | isSafeToExecuteUnconditionally( | ||||||||||
888 | I, DT, CurLoop, SafetyInfo, ORE, | ||||||||||
889 | CurLoop->getLoopPreheader()->getTerminator())) { | ||||||||||
890 | hoist(I, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo, | ||||||||||
891 | MSSAU, SE, ORE); | ||||||||||
892 | HoistedInstructions.push_back(&I); | ||||||||||
893 | Changed = true; | ||||||||||
894 | continue; | ||||||||||
895 | } | ||||||||||
896 | |||||||||||
897 | // Attempt to remove floating point division out of the loop by | ||||||||||
898 | // converting it to a reciprocal multiplication. | ||||||||||
899 | if (I.getOpcode() == Instruction::FDiv && I.hasAllowReciprocal() && | ||||||||||
900 | CurLoop->isLoopInvariant(I.getOperand(1))) { | ||||||||||
901 | auto Divisor = I.getOperand(1); | ||||||||||
902 | auto One = llvm::ConstantFP::get(Divisor->getType(), 1.0); | ||||||||||
903 | auto ReciprocalDivisor = BinaryOperator::CreateFDiv(One, Divisor); | ||||||||||
904 | ReciprocalDivisor->setFastMathFlags(I.getFastMathFlags()); | ||||||||||
905 | SafetyInfo->insertInstructionTo(ReciprocalDivisor, I.getParent()); | ||||||||||
906 | ReciprocalDivisor->insertBefore(&I); | ||||||||||
907 | |||||||||||
908 | auto Product = | ||||||||||
909 | BinaryOperator::CreateFMul(I.getOperand(0), ReciprocalDivisor); | ||||||||||
910 | Product->setFastMathFlags(I.getFastMathFlags()); | ||||||||||
911 | SafetyInfo->insertInstructionTo(Product, I.getParent()); | ||||||||||
912 | Product->insertAfter(&I); | ||||||||||
913 | I.replaceAllUsesWith(Product); | ||||||||||
914 | eraseInstruction(I, *SafetyInfo, CurAST, MSSAU); | ||||||||||
915 | |||||||||||
916 | hoist(*ReciprocalDivisor, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), | ||||||||||
917 | SafetyInfo, MSSAU, SE, ORE); | ||||||||||
918 | HoistedInstructions.push_back(ReciprocalDivisor); | ||||||||||
919 | Changed = true; | ||||||||||
920 | continue; | ||||||||||
921 | } | ||||||||||
922 | |||||||||||
923 | auto IsInvariantStart = [&](Instruction &I) { | ||||||||||
924 | using namespace PatternMatch; | ||||||||||
925 | return I.use_empty() && | ||||||||||
926 | match(&I, m_Intrinsic<Intrinsic::invariant_start>()); | ||||||||||
927 | }; | ||||||||||
928 | auto MustExecuteWithoutWritesBefore = [&](Instruction &I) { | ||||||||||
929 | return SafetyInfo->isGuaranteedToExecute(I, DT, CurLoop) && | ||||||||||
930 | SafetyInfo->doesNotWriteMemoryBefore(I, CurLoop); | ||||||||||
931 | }; | ||||||||||
932 | if ((IsInvariantStart(I) || isGuard(&I)) && | ||||||||||
933 | CurLoop->hasLoopInvariantOperands(&I) && | ||||||||||
934 | MustExecuteWithoutWritesBefore(I)) { | ||||||||||
935 | hoist(I, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo, | ||||||||||
936 | MSSAU, SE, ORE); | ||||||||||
937 | HoistedInstructions.push_back(&I); | ||||||||||
938 | Changed = true; | ||||||||||
939 | continue; | ||||||||||
940 | } | ||||||||||
941 | |||||||||||
942 | if (PHINode *PN = dyn_cast<PHINode>(&I)) { | ||||||||||
943 | if (CFH.canHoistPHI(PN)) { | ||||||||||
944 | // Redirect incoming blocks first to ensure that we create hoisted | ||||||||||
945 | // versions of those blocks before we hoist the phi. | ||||||||||
946 | for (unsigned int i = 0; i < PN->getNumIncomingValues(); ++i) | ||||||||||
947 | PN->setIncomingBlock( | ||||||||||
948 | i, CFH.getOrCreateHoistedBlock(PN->getIncomingBlock(i))); | ||||||||||
949 | hoist(*PN, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo, | ||||||||||
950 | MSSAU, SE, ORE); | ||||||||||
951 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 951, __PRETTY_FUNCTION__)); | ||||||||||
952 | Changed = true; | ||||||||||
953 | continue; | ||||||||||
954 | } | ||||||||||
955 | } | ||||||||||
956 | |||||||||||
957 | // Remember possibly hoistable branches so we can actually hoist them | ||||||||||
958 | // later if needed. | ||||||||||
959 | if (BranchInst *BI = dyn_cast<BranchInst>(&I)) | ||||||||||
960 | CFH.registerPossiblyHoistableBranch(BI); | ||||||||||
961 | } | ||||||||||
962 | } | ||||||||||
963 | |||||||||||
964 | // If we hoisted instructions to a conditional block they may not dominate | ||||||||||
965 | // their uses that weren't hoisted (such as phis where some operands are not | ||||||||||
966 | // loop invariant). If so make them unconditional by moving them to their | ||||||||||
967 | // immediate dominator. We iterate through the instructions in reverse order | ||||||||||
968 | // which ensures that when we rehoist an instruction we rehoist its operands, | ||||||||||
969 | // and also keep track of where in the block we are rehoisting to to make sure | ||||||||||
970 | // that we rehoist instructions before the instructions that use them. | ||||||||||
971 | Instruction *HoistPoint = nullptr; | ||||||||||
972 | if (ControlFlowHoisting) { | ||||||||||
973 | for (Instruction *I : reverse(HoistedInstructions)) { | ||||||||||
974 | if (!llvm::all_of(I->uses(), | ||||||||||
975 | [&](Use &U) { return DT->dominates(I, U); })) { | ||||||||||
976 | BasicBlock *Dominator = | ||||||||||
977 | DT->getNode(I->getParent())->getIDom()->getBlock(); | ||||||||||
978 | if (!HoistPoint || !DT->dominates(HoistPoint->getParent(), Dominator)) { | ||||||||||
979 | if (HoistPoint) | ||||||||||
980 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 981, __PRETTY_FUNCTION__)) | ||||||||||
981 | "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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 981, __PRETTY_FUNCTION__)); | ||||||||||
982 | HoistPoint = Dominator->getTerminator(); | ||||||||||
983 | } | ||||||||||
984 | LLVM_DEBUG(dbgs() << "LICM rehoisting to "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM rehoisting to " << HoistPoint ->getParent()->getNameOrAsOperand() << ": " << *I << "\n"; } } while (false) | ||||||||||
985 | << HoistPoint->getParent()->getNameOrAsOperand()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM rehoisting to " << HoistPoint ->getParent()->getNameOrAsOperand() << ": " << *I << "\n"; } } while (false) | ||||||||||
986 | << ": " << *I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM rehoisting to " << HoistPoint ->getParent()->getNameOrAsOperand() << ": " << *I << "\n"; } } while (false); | ||||||||||
987 | moveInstructionBefore(*I, *HoistPoint, *SafetyInfo, MSSAU, SE); | ||||||||||
988 | HoistPoint = I; | ||||||||||
989 | Changed = true; | ||||||||||
990 | } | ||||||||||
991 | } | ||||||||||
992 | } | ||||||||||
993 | if (MSSAU && VerifyMemorySSA) | ||||||||||
994 | MSSAU->getMemorySSA()->verifyMemorySSA(); | ||||||||||
995 | |||||||||||
996 | // Now that we've finished hoisting make sure that LI and DT are still | ||||||||||
997 | // valid. | ||||||||||
998 | #ifdef EXPENSIVE_CHECKS | ||||||||||
999 | if (Changed) { | ||||||||||
1000 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1001, __PRETTY_FUNCTION__)) | ||||||||||
1001 | "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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1001, __PRETTY_FUNCTION__)); | ||||||||||
1002 | LI->verify(*DT); | ||||||||||
1003 | } | ||||||||||
1004 | #endif | ||||||||||
1005 | |||||||||||
1006 | return Changed; | ||||||||||
1007 | } | ||||||||||
1008 | |||||||||||
1009 | // Return true if LI is invariant within scope of the loop. LI is invariant if | ||||||||||
1010 | // CurLoop is dominated by an invariant.start representing the same memory | ||||||||||
1011 | // location and size as the memory location LI loads from, and also the | ||||||||||
1012 | // invariant.start has no uses. | ||||||||||
1013 | static bool isLoadInvariantInLoop(LoadInst *LI, DominatorTree *DT, | ||||||||||
1014 | Loop *CurLoop) { | ||||||||||
1015 | Value *Addr = LI->getOperand(0); | ||||||||||
1016 | const DataLayout &DL = LI->getModule()->getDataLayout(); | ||||||||||
1017 | const TypeSize LocSizeInBits = DL.getTypeSizeInBits(LI->getType()); | ||||||||||
1018 | |||||||||||
1019 | // It is not currently possible for clang to generate an invariant.start | ||||||||||
1020 | // intrinsic with scalable vector types because we don't support thread local | ||||||||||
1021 | // sizeless types and we don't permit sizeless types in structs or classes. | ||||||||||
1022 | // Furthermore, even if support is added for this in future the intrinsic | ||||||||||
1023 | // itself is defined to have a size of -1 for variable sized objects. This | ||||||||||
1024 | // makes it impossible to verify if the intrinsic envelops our region of | ||||||||||
1025 | // interest. For example, both <vscale x 32 x i8> and <vscale x 16 x i8> | ||||||||||
1026 | // types would have a -1 parameter, but the former is clearly double the size | ||||||||||
1027 | // of the latter. | ||||||||||
1028 | if (LocSizeInBits.isScalable()) | ||||||||||
1029 | return false; | ||||||||||
1030 | |||||||||||
1031 | // if the type is i8 addrspace(x)*, we know this is the type of | ||||||||||
1032 | // llvm.invariant.start operand | ||||||||||
1033 | auto *PtrInt8Ty = PointerType::get(Type::getInt8Ty(LI->getContext()), | ||||||||||
1034 | LI->getPointerAddressSpace()); | ||||||||||
1035 | unsigned BitcastsVisited = 0; | ||||||||||
1036 | // Look through bitcasts until we reach the i8* type (this is invariant.start | ||||||||||
1037 | // operand type). | ||||||||||
1038 | while (Addr->getType() != PtrInt8Ty) { | ||||||||||
1039 | auto *BC = dyn_cast<BitCastInst>(Addr); | ||||||||||
1040 | // Avoid traversing high number of bitcast uses. | ||||||||||
1041 | if (++BitcastsVisited > MaxNumUsesTraversed || !BC) | ||||||||||
1042 | return false; | ||||||||||
1043 | Addr = BC->getOperand(0); | ||||||||||
1044 | } | ||||||||||
1045 | |||||||||||
1046 | unsigned UsesVisited = 0; | ||||||||||
1047 | // Traverse all uses of the load operand value, to see if invariant.start is | ||||||||||
1048 | // one of the uses, and whether it dominates the load instruction. | ||||||||||
1049 | for (auto *U : Addr->users()) { | ||||||||||
1050 | // Avoid traversing for Load operand with high number of users. | ||||||||||
1051 | if (++UsesVisited > MaxNumUsesTraversed) | ||||||||||
1052 | return false; | ||||||||||
1053 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(U); | ||||||||||
1054 | // If there are escaping uses of invariant.start instruction, the load maybe | ||||||||||
1055 | // non-invariant. | ||||||||||
1056 | if (!II || II->getIntrinsicID() != Intrinsic::invariant_start || | ||||||||||
1057 | !II->use_empty()) | ||||||||||
1058 | continue; | ||||||||||
1059 | ConstantInt *InvariantSize = cast<ConstantInt>(II->getArgOperand(0)); | ||||||||||
1060 | // The intrinsic supports having a -1 argument for variable sized objects | ||||||||||
1061 | // so we should check for that here. | ||||||||||
1062 | if (InvariantSize->isNegative()) | ||||||||||
1063 | continue; | ||||||||||
1064 | uint64_t InvariantSizeInBits = InvariantSize->getSExtValue() * 8; | ||||||||||
1065 | // Confirm the invariant.start location size contains the load operand size | ||||||||||
1066 | // in bits. Also, the invariant.start should dominate the load, and we | ||||||||||
1067 | // should not hoist the load out of a loop that contains this dominating | ||||||||||
1068 | // invariant.start. | ||||||||||
1069 | if (LocSizeInBits.getFixedSize() <= InvariantSizeInBits && | ||||||||||
1070 | DT->properlyDominates(II->getParent(), CurLoop->getHeader())) | ||||||||||
1071 | return true; | ||||||||||
1072 | } | ||||||||||
1073 | |||||||||||
1074 | return false; | ||||||||||
1075 | } | ||||||||||
1076 | |||||||||||
1077 | namespace { | ||||||||||
1078 | /// Return true if-and-only-if we know how to (mechanically) both hoist and | ||||||||||
1079 | /// sink a given instruction out of a loop. Does not address legality | ||||||||||
1080 | /// concerns such as aliasing or speculation safety. | ||||||||||
1081 | bool isHoistableAndSinkableInst(Instruction &I) { | ||||||||||
1082 | // Only these instructions are hoistable/sinkable. | ||||||||||
1083 | return (isa<LoadInst>(I) || isa<StoreInst>(I) || isa<CallInst>(I) || | ||||||||||
1084 | isa<FenceInst>(I) || isa<CastInst>(I) || isa<UnaryOperator>(I) || | ||||||||||
1085 | isa<BinaryOperator>(I) || isa<SelectInst>(I) || | ||||||||||
1086 | isa<GetElementPtrInst>(I) || isa<CmpInst>(I) || | ||||||||||
1087 | isa<InsertElementInst>(I) || isa<ExtractElementInst>(I) || | ||||||||||
1088 | isa<ShuffleVectorInst>(I) || isa<ExtractValueInst>(I) || | ||||||||||
1089 | isa<InsertValueInst>(I) || isa<FreezeInst>(I)); | ||||||||||
1090 | } | ||||||||||
1091 | /// Return true if all of the alias sets within this AST are known not to | ||||||||||
1092 | /// contain a Mod, or if MSSA knows thare are no MemoryDefs in the loop. | ||||||||||
1093 | bool isReadOnly(AliasSetTracker *CurAST, const MemorySSAUpdater *MSSAU, | ||||||||||
1094 | const Loop *L) { | ||||||||||
1095 | if (CurAST) { | ||||||||||
1096 | for (AliasSet &AS : *CurAST) { | ||||||||||
1097 | if (!AS.isForwardingAliasSet() && AS.isMod()) { | ||||||||||
1098 | return false; | ||||||||||
1099 | } | ||||||||||
1100 | } | ||||||||||
1101 | return true; | ||||||||||
1102 | } else { /*MSSAU*/ | ||||||||||
1103 | for (auto *BB : L->getBlocks()) | ||||||||||
1104 | if (MSSAU->getMemorySSA()->getBlockDefs(BB)) | ||||||||||
1105 | return false; | ||||||||||
1106 | return true; | ||||||||||
1107 | } | ||||||||||
1108 | } | ||||||||||
1109 | |||||||||||
1110 | /// Return true if I is the only Instruction with a MemoryAccess in L. | ||||||||||
1111 | bool isOnlyMemoryAccess(const Instruction *I, const Loop *L, | ||||||||||
1112 | const MemorySSAUpdater *MSSAU) { | ||||||||||
1113 | for (auto *BB : L->getBlocks()) | ||||||||||
1114 | if (auto *Accs = MSSAU->getMemorySSA()->getBlockAccesses(BB)) { | ||||||||||
1115 | int NotAPhi = 0; | ||||||||||
1116 | for (const auto &Acc : *Accs) { | ||||||||||
1117 | if (isa<MemoryPhi>(&Acc)) | ||||||||||
1118 | continue; | ||||||||||
1119 | const auto *MUD = cast<MemoryUseOrDef>(&Acc); | ||||||||||
1120 | if (MUD->getMemoryInst() != I || NotAPhi++ == 1) | ||||||||||
1121 | return false; | ||||||||||
1122 | } | ||||||||||
1123 | } | ||||||||||
1124 | return true; | ||||||||||
1125 | } | ||||||||||
1126 | } | ||||||||||
1127 | |||||||||||
1128 | bool llvm::canSinkOrHoistInst(Instruction &I, AAResults *AA, DominatorTree *DT, | ||||||||||
1129 | Loop *CurLoop, AliasSetTracker *CurAST, | ||||||||||
1130 | MemorySSAUpdater *MSSAU, | ||||||||||
1131 | bool TargetExecutesOncePerLoop, | ||||||||||
1132 | SinkAndHoistLICMFlags *Flags, | ||||||||||
1133 | OptimizationRemarkEmitter *ORE) { | ||||||||||
1134 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1135, __PRETTY_FUNCTION__)) | ||||||||||
| |||||||||||
1135 | "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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1135, __PRETTY_FUNCTION__)); | ||||||||||
1136 | |||||||||||
1137 | // If we don't understand the instruction, bail early. | ||||||||||
1138 | if (!isHoistableAndSinkableInst(I)) | ||||||||||
1139 | return false; | ||||||||||
1140 | |||||||||||
1141 | MemorySSA *MSSA = MSSAU
| ||||||||||
1142 | if (MSSA) | ||||||||||
1143 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1143, __PRETTY_FUNCTION__)); | ||||||||||
1144 | |||||||||||
1145 | // Loads have extra constraints we have to verify before we can hoist them. | ||||||||||
1146 | if (LoadInst *LI
| ||||||||||
1147 | if (!LI->isUnordered()) | ||||||||||
1148 | return false; // Don't sink/hoist volatile or ordered atomic loads! | ||||||||||
1149 | |||||||||||
1150 | // Loads from constant memory are always safe to move, even if they end up | ||||||||||
1151 | // in the same alias set as something that ends up being modified. | ||||||||||
1152 | if (AA->pointsToConstantMemory(LI->getOperand(0))) | ||||||||||
1153 | return true; | ||||||||||
1154 | if (LI->hasMetadata(LLVMContext::MD_invariant_load)) | ||||||||||
1155 | return true; | ||||||||||
1156 | |||||||||||
1157 | if (LI->isAtomic() && !TargetExecutesOncePerLoop) | ||||||||||
1158 | return false; // Don't risk duplicating unordered loads | ||||||||||
1159 | |||||||||||
1160 | // This checks for an invariant.start dominating the load. | ||||||||||
1161 | if (isLoadInvariantInLoop(LI, DT, CurLoop)) | ||||||||||
1162 | return true; | ||||||||||
1163 | |||||||||||
1164 | bool Invalidated; | ||||||||||
1165 | if (CurAST) | ||||||||||
1166 | Invalidated = pointerInvalidatedByLoop(MemoryLocation::get(LI), CurAST, | ||||||||||
1167 | CurLoop, AA); | ||||||||||
1168 | else | ||||||||||
1169 | Invalidated = pointerInvalidatedByLoopWithMSSA( | ||||||||||
1170 | MSSA, cast<MemoryUse>(MSSA->getMemoryAccess(LI)), CurLoop, I, *Flags); | ||||||||||
1171 | // Check loop-invariant address because this may also be a sinkable load | ||||||||||
1172 | // whose address is not necessarily loop-invariant. | ||||||||||
1173 | if (ORE && Invalidated && CurLoop->isLoopInvariant(LI->getPointerOperand())) | ||||||||||
1174 | ORE->emit([&]() { | ||||||||||
1175 | return OptimizationRemarkMissed( | ||||||||||
1176 | DEBUG_TYPE"licm", "LoadWithLoopInvariantAddressInvalidated", LI) | ||||||||||
1177 | << "failed to move load with loop-invariant address " | ||||||||||
1178 | "because the loop may invalidate its value"; | ||||||||||
1179 | }); | ||||||||||
1180 | |||||||||||
1181 | return !Invalidated; | ||||||||||
1182 | } else if (CallInst *CI
| ||||||||||
1183 | // Don't sink or hoist dbg info; it's legal, but not useful. | ||||||||||
1184 | if (isa<DbgInfoIntrinsic>(I)) | ||||||||||
1185 | return false; | ||||||||||
1186 | |||||||||||
1187 | // Don't sink calls which can throw. | ||||||||||
1188 | if (CI->mayThrow()) | ||||||||||
1189 | return false; | ||||||||||
1190 | |||||||||||
1191 | // Convergent attribute has been used on operations that involve | ||||||||||
1192 | // inter-thread communication which results are implicitly affected by the | ||||||||||
1193 | // enclosing control flows. It is not safe to hoist or sink such operations | ||||||||||
1194 | // across control flow. | ||||||||||
1195 | if (CI->isConvergent()) | ||||||||||
1196 | return false; | ||||||||||
1197 | |||||||||||
1198 | using namespace PatternMatch; | ||||||||||
1199 | if (match(CI, m_Intrinsic<Intrinsic::assume>())) | ||||||||||
1200 | // Assumes don't actually alias anything or throw | ||||||||||
1201 | return true; | ||||||||||
1202 | |||||||||||
1203 | if (match(CI, m_Intrinsic<Intrinsic::experimental_widenable_condition>())) | ||||||||||
1204 | // Widenable conditions don't actually alias anything or throw | ||||||||||
1205 | return true; | ||||||||||
1206 | |||||||||||
1207 | // Handle simple cases by querying alias analysis. | ||||||||||
1208 | FunctionModRefBehavior Behavior = AA->getModRefBehavior(CI); | ||||||||||
1209 | if (Behavior == FMRB_DoesNotAccessMemory) | ||||||||||
1210 | return true; | ||||||||||
1211 | if (AAResults::onlyReadsMemory(Behavior)) { | ||||||||||
1212 | // A readonly argmemonly function only reads from memory pointed to by | ||||||||||
1213 | // it's arguments with arbitrary offsets. If we can prove there are no | ||||||||||
1214 | // writes to this memory in the loop, we can hoist or sink. | ||||||||||
1215 | if (AAResults::onlyAccessesArgPointees(Behavior)) { | ||||||||||
1216 | // TODO: expand to writeable arguments | ||||||||||
1217 | for (Value *Op : CI->arg_operands()) | ||||||||||
1218 | if (Op->getType()->isPointerTy()) { | ||||||||||
1219 | bool Invalidated; | ||||||||||
1220 | if (CurAST
| ||||||||||
1221 | Invalidated = pointerInvalidatedByLoop( | ||||||||||
1222 | MemoryLocation::getBeforeOrAfter(Op), CurAST, CurLoop, AA); | ||||||||||
1223 | else | ||||||||||
1224 | Invalidated = pointerInvalidatedByLoopWithMSSA( | ||||||||||
1225 | MSSA, cast<MemoryUse>(MSSA->getMemoryAccess(CI)), CurLoop, I, | ||||||||||
| |||||||||||
1226 | *Flags); | ||||||||||
1227 | if (Invalidated) | ||||||||||
1228 | return false; | ||||||||||
1229 | } | ||||||||||
1230 | return true; | ||||||||||
1231 | } | ||||||||||
1232 | |||||||||||
1233 | // If this call only reads from memory and there are no writes to memory | ||||||||||
1234 | // in the loop, we can hoist or sink the call as appropriate. | ||||||||||
1235 | if (isReadOnly(CurAST, MSSAU, CurLoop)) | ||||||||||
1236 | return true; | ||||||||||
1237 | } | ||||||||||
1238 | |||||||||||
1239 | // FIXME: This should use mod/ref information to see if we can hoist or | ||||||||||
1240 | // sink the call. | ||||||||||
1241 | |||||||||||
1242 | return false; | ||||||||||
1243 | } else if (auto *FI = dyn_cast<FenceInst>(&I)) { | ||||||||||
1244 | // Fences alias (most) everything to provide ordering. For the moment, | ||||||||||
1245 | // just give up if there are any other memory operations in the loop. | ||||||||||
1246 | if (CurAST) { | ||||||||||
1247 | auto Begin = CurAST->begin(); | ||||||||||
1248 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1248, __PRETTY_FUNCTION__)); | ||||||||||
1249 | if (std::next(Begin) != CurAST->end()) | ||||||||||
1250 | // constant memory for instance, TODO: handle better | ||||||||||
1251 | return false; | ||||||||||
1252 | auto *UniqueI = Begin->getUniqueInstruction(); | ||||||||||
1253 | if (!UniqueI) | ||||||||||
1254 | // other memory op, give up | ||||||||||
1255 | return false; | ||||||||||
1256 | (void)FI; // suppress unused variable warning | ||||||||||
1257 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1257, __PRETTY_FUNCTION__)); | ||||||||||
1258 | return true; | ||||||||||
1259 | } else // MSSAU | ||||||||||
1260 | return isOnlyMemoryAccess(FI, CurLoop, MSSAU); | ||||||||||
1261 | } else if (auto *SI = dyn_cast<StoreInst>(&I)) { | ||||||||||
1262 | if (!SI->isUnordered()) | ||||||||||
1263 | return false; // Don't sink/hoist volatile or ordered atomic store! | ||||||||||
1264 | |||||||||||
1265 | // We can only hoist a store that we can prove writes a value which is not | ||||||||||
1266 | // read or overwritten within the loop. For those cases, we fallback to | ||||||||||
1267 | // load store promotion instead. TODO: We can extend this to cases where | ||||||||||
1268 | // there is exactly one write to the location and that write dominates an | ||||||||||
1269 | // arbitrary number of reads in the loop. | ||||||||||
1270 | if (CurAST) { | ||||||||||
1271 | auto &AS = CurAST->getAliasSetFor(MemoryLocation::get(SI)); | ||||||||||
1272 | |||||||||||
1273 | if (AS.isRef() || !AS.isMustAlias()) | ||||||||||
1274 | // Quick exit test, handled by the full path below as well. | ||||||||||
1275 | return false; | ||||||||||
1276 | auto *UniqueI = AS.getUniqueInstruction(); | ||||||||||
1277 | if (!UniqueI) | ||||||||||
1278 | // other memory op, give up | ||||||||||
1279 | return false; | ||||||||||
1280 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1280, __PRETTY_FUNCTION__)); | ||||||||||
1281 | return true; | ||||||||||
1282 | } else { // MSSAU | ||||||||||
1283 | if (isOnlyMemoryAccess(SI, CurLoop, MSSAU)) | ||||||||||
1284 | return true; | ||||||||||
1285 | // If there are more accesses than the Promotion cap or no "quota" to | ||||||||||
1286 | // check clobber, then give up as we're not walking a list that long. | ||||||||||
1287 | if (Flags->tooManyMemoryAccesses() || Flags->tooManyClobberingCalls()) | ||||||||||
1288 | return false; | ||||||||||
1289 | // If there are interfering Uses (i.e. their defining access is in the | ||||||||||
1290 | // loop), or ordered loads (stored as Defs!), don't move this store. | ||||||||||
1291 | // Could do better here, but this is conservatively correct. | ||||||||||
1292 | // TODO: Cache set of Uses on the first walk in runOnLoop, update when | ||||||||||
1293 | // moving accesses. Can also extend to dominating uses. | ||||||||||
1294 | auto *SIMD = MSSA->getMemoryAccess(SI); | ||||||||||
1295 | for (auto *BB : CurLoop->getBlocks()) | ||||||||||
1296 | if (auto *Accesses = MSSA->getBlockAccesses(BB)) { | ||||||||||
1297 | for (const auto &MA : *Accesses) | ||||||||||
1298 | if (const auto *MU = dyn_cast<MemoryUse>(&MA)) { | ||||||||||
1299 | auto *MD = MU->getDefiningAccess(); | ||||||||||
1300 | if (!MSSA->isLiveOnEntryDef(MD) && | ||||||||||
1301 | CurLoop->contains(MD->getBlock())) | ||||||||||
1302 | return false; | ||||||||||
1303 | // Disable hoisting past potentially interfering loads. Optimized | ||||||||||
1304 | // Uses may point to an access outside the loop, as getClobbering | ||||||||||
1305 | // checks the previous iteration when walking the backedge. | ||||||||||
1306 | // FIXME: More precise: no Uses that alias SI. | ||||||||||
1307 | if (!Flags->getIsSink() && !MSSA->dominates(SIMD, MU)) | ||||||||||
1308 | return false; | ||||||||||
1309 | } else if (const auto *MD = dyn_cast<MemoryDef>(&MA)) { | ||||||||||
1310 | if (auto *LI = dyn_cast<LoadInst>(MD->getMemoryInst())) { | ||||||||||
1311 | (void)LI; // Silence warning. | ||||||||||
1312 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1312, __PRETTY_FUNCTION__)); | ||||||||||
1313 | return false; | ||||||||||
1314 | } | ||||||||||
1315 | // Any call, while it may not be clobbering SI, it may be a use. | ||||||||||
1316 | if (auto *CI = dyn_cast<CallInst>(MD->getMemoryInst())) { | ||||||||||
1317 | // Check if the call may read from the memory locattion written | ||||||||||
1318 | // to by SI. Check CI's attributes and arguments; the number of | ||||||||||
1319 | // such checks performed is limited above by NoOfMemAccTooLarge. | ||||||||||
1320 | ModRefInfo MRI = AA->getModRefInfo(CI, MemoryLocation::get(SI)); | ||||||||||
1321 | if (isModOrRefSet(MRI)) | ||||||||||
1322 | return false; | ||||||||||
1323 | } | ||||||||||
1324 | } | ||||||||||
1325 | } | ||||||||||
1326 | auto *Source = MSSA->getSkipSelfWalker()->getClobberingMemoryAccess(SI); | ||||||||||
1327 | Flags->incrementClobberingCalls(); | ||||||||||
1328 | // If there are no clobbering Defs in the loop, store is safe to hoist. | ||||||||||
1329 | return MSSA->isLiveOnEntryDef(Source) || | ||||||||||
1330 | !CurLoop->contains(Source->getBlock()); | ||||||||||
1331 | } | ||||||||||
1332 | } | ||||||||||
1333 | |||||||||||
1334 | assert(!I.mayReadOrWriteMemory() && "unhandled aliasing")((!I.mayReadOrWriteMemory() && "unhandled aliasing") ? static_cast<void> (0) : __assert_fail ("!I.mayReadOrWriteMemory() && \"unhandled aliasing\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1334, __PRETTY_FUNCTION__)); | ||||||||||
1335 | |||||||||||
1336 | // We've established mechanical ability and aliasing, it's up to the caller | ||||||||||
1337 | // to check fault safety | ||||||||||
1338 | return true; | ||||||||||
1339 | } | ||||||||||
1340 | |||||||||||
1341 | /// Returns true if a PHINode is a trivially replaceable with an | ||||||||||
1342 | /// Instruction. | ||||||||||
1343 | /// This is true when all incoming values are that instruction. | ||||||||||
1344 | /// This pattern occurs most often with LCSSA PHI nodes. | ||||||||||
1345 | /// | ||||||||||
1346 | static bool isTriviallyReplaceablePHI(const PHINode &PN, const Instruction &I) { | ||||||||||
1347 | for (const Value *IncValue : PN.incoming_values()) | ||||||||||
1348 | if (IncValue != &I) | ||||||||||
1349 | return false; | ||||||||||
1350 | |||||||||||
1351 | return true; | ||||||||||
1352 | } | ||||||||||
1353 | |||||||||||
1354 | /// Return true if the instruction is free in the loop. | ||||||||||
1355 | static bool isFreeInLoop(const Instruction &I, const Loop *CurLoop, | ||||||||||
1356 | const TargetTransformInfo *TTI) { | ||||||||||
1357 | |||||||||||
1358 | if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) { | ||||||||||
1359 | if (TTI->getUserCost(GEP, TargetTransformInfo::TCK_SizeAndLatency) != | ||||||||||
1360 | TargetTransformInfo::TCC_Free) | ||||||||||
1361 | return false; | ||||||||||
1362 | // For a GEP, we cannot simply use getUserCost because currently it | ||||||||||
1363 | // optimistically assume that a GEP will fold into addressing mode | ||||||||||
1364 | // regardless of its users. | ||||||||||
1365 | const BasicBlock *BB = GEP->getParent(); | ||||||||||
1366 | for (const User *U : GEP->users()) { | ||||||||||
1367 | const Instruction *UI = cast<Instruction>(U); | ||||||||||
1368 | if (CurLoop->contains(UI) && | ||||||||||
1369 | (BB != UI->getParent() || | ||||||||||
1370 | (!isa<StoreInst>(UI) && !isa<LoadInst>(UI)))) | ||||||||||
1371 | return false; | ||||||||||
1372 | } | ||||||||||
1373 | return true; | ||||||||||
1374 | } else | ||||||||||
1375 | return TTI->getUserCost(&I, TargetTransformInfo::TCK_SizeAndLatency) == | ||||||||||
1376 | TargetTransformInfo::TCC_Free; | ||||||||||
1377 | } | ||||||||||
1378 | |||||||||||
1379 | /// Return true if the only users of this instruction are outside of | ||||||||||
1380 | /// the loop. If this is true, we can sink the instruction to the exit | ||||||||||
1381 | /// blocks of the loop. | ||||||||||
1382 | /// | ||||||||||
1383 | /// We also return true if the instruction could be folded away in lowering. | ||||||||||
1384 | /// (e.g., a GEP can be folded into a load as an addressing mode in the loop). | ||||||||||
1385 | static bool isNotUsedOrFreeInLoop(const Instruction &I, const Loop *CurLoop, | ||||||||||
1386 | const LoopSafetyInfo *SafetyInfo, | ||||||||||
1387 | TargetTransformInfo *TTI, bool &FreeInLoop) { | ||||||||||
1388 | const auto &BlockColors = SafetyInfo->getBlockColors(); | ||||||||||
1389 | bool IsFree = isFreeInLoop(I, CurLoop, TTI); | ||||||||||
1390 | for (const User *U : I.users()) { | ||||||||||
1391 | const Instruction *UI = cast<Instruction>(U); | ||||||||||
1392 | if (const PHINode *PN = dyn_cast<PHINode>(UI)) { | ||||||||||
1393 | const BasicBlock *BB = PN->getParent(); | ||||||||||
1394 | // We cannot sink uses in catchswitches. | ||||||||||
1395 | if (isa<CatchSwitchInst>(BB->getTerminator())) | ||||||||||
1396 | return false; | ||||||||||
1397 | |||||||||||
1398 | // We need to sink a callsite to a unique funclet. Avoid sinking if the | ||||||||||
1399 | // phi use is too muddled. | ||||||||||
1400 | if (isa<CallInst>(I)) | ||||||||||
1401 | if (!BlockColors.empty() && | ||||||||||
1402 | BlockColors.find(const_cast<BasicBlock *>(BB))->second.size() != 1) | ||||||||||
1403 | return false; | ||||||||||
1404 | } | ||||||||||
1405 | |||||||||||
1406 | if (CurLoop->contains(UI)) { | ||||||||||
1407 | if (IsFree) { | ||||||||||
1408 | FreeInLoop = true; | ||||||||||
1409 | continue; | ||||||||||
1410 | } | ||||||||||
1411 | return false; | ||||||||||
1412 | } | ||||||||||
1413 | } | ||||||||||
1414 | return true; | ||||||||||
1415 | } | ||||||||||
1416 | |||||||||||
1417 | static Instruction *cloneInstructionInExitBlock( | ||||||||||
1418 | Instruction &I, BasicBlock &ExitBlock, PHINode &PN, const LoopInfo *LI, | ||||||||||
1419 | const LoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU) { | ||||||||||
1420 | Instruction *New; | ||||||||||
1421 | if (auto *CI = dyn_cast<CallInst>(&I)) { | ||||||||||
1422 | const auto &BlockColors = SafetyInfo->getBlockColors(); | ||||||||||
1423 | |||||||||||
1424 | // Sinking call-sites need to be handled differently from other | ||||||||||
1425 | // instructions. The cloned call-site needs a funclet bundle operand | ||||||||||
1426 | // appropriate for its location in the CFG. | ||||||||||
1427 | SmallVector<OperandBundleDef, 1> OpBundles; | ||||||||||
1428 | for (unsigned BundleIdx = 0, BundleEnd = CI->getNumOperandBundles(); | ||||||||||
1429 | BundleIdx != BundleEnd; ++BundleIdx) { | ||||||||||
1430 | OperandBundleUse Bundle = CI->getOperandBundleAt(BundleIdx); | ||||||||||
1431 | if (Bundle.getTagID() == LLVMContext::OB_funclet) | ||||||||||
1432 | continue; | ||||||||||
1433 | |||||||||||
1434 | OpBundles.emplace_back(Bundle); | ||||||||||
1435 | } | ||||||||||
1436 | |||||||||||
1437 | if (!BlockColors.empty()) { | ||||||||||
1438 | const ColorVector &CV = BlockColors.find(&ExitBlock)->second; | ||||||||||
1439 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1439, __PRETTY_FUNCTION__)); | ||||||||||
1440 | BasicBlock *BBColor = CV.front(); | ||||||||||
1441 | Instruction *EHPad = BBColor->getFirstNonPHI(); | ||||||||||
1442 | if (EHPad->isEHPad()) | ||||||||||
1443 | OpBundles.emplace_back("funclet", EHPad); | ||||||||||
1444 | } | ||||||||||
1445 | |||||||||||
1446 | New = CallInst::Create(CI, OpBundles); | ||||||||||
1447 | } else { | ||||||||||
1448 | New = I.clone(); | ||||||||||
1449 | } | ||||||||||
1450 | |||||||||||
1451 | ExitBlock.getInstList().insert(ExitBlock.getFirstInsertionPt(), New); | ||||||||||
1452 | if (!I.getName().empty()) | ||||||||||
1453 | New->setName(I.getName() + ".le"); | ||||||||||
1454 | |||||||||||
1455 | if (MSSAU && MSSAU->getMemorySSA()->getMemoryAccess(&I)) { | ||||||||||
1456 | // Create a new MemoryAccess and let MemorySSA set its defining access. | ||||||||||
1457 | MemoryAccess *NewMemAcc = MSSAU->createMemoryAccessInBB( | ||||||||||
1458 | New, nullptr, New->getParent(), MemorySSA::Beginning); | ||||||||||
1459 | if (NewMemAcc) { | ||||||||||
1460 | if (auto *MemDef = dyn_cast<MemoryDef>(NewMemAcc)) | ||||||||||
1461 | MSSAU->insertDef(MemDef, /*RenameUses=*/true); | ||||||||||
1462 | else { | ||||||||||
1463 | auto *MemUse = cast<MemoryUse>(NewMemAcc); | ||||||||||
1464 | MSSAU->insertUse(MemUse, /*RenameUses=*/true); | ||||||||||
1465 | } | ||||||||||
1466 | } | ||||||||||
1467 | } | ||||||||||
1468 | |||||||||||
1469 | // Build LCSSA PHI nodes for any in-loop operands. Note that this is | ||||||||||
1470 | // particularly cheap because we can rip off the PHI node that we're | ||||||||||
1471 | // replacing for the number and blocks of the predecessors. | ||||||||||
1472 | // OPT: If this shows up in a profile, we can instead finish sinking all | ||||||||||
1473 | // invariant instructions, and then walk their operands to re-establish | ||||||||||
1474 | // LCSSA. That will eliminate creating PHI nodes just to nuke them when | ||||||||||
1475 | // sinking bottom-up. | ||||||||||
1476 | for (User::op_iterator OI = New->op_begin(), OE = New->op_end(); OI != OE; | ||||||||||
1477 | ++OI) | ||||||||||
1478 | if (Instruction *OInst = dyn_cast<Instruction>(*OI)) | ||||||||||
1479 | if (Loop *OLoop = LI->getLoopFor(OInst->getParent())) | ||||||||||
1480 | if (!OLoop->contains(&PN)) { | ||||||||||
1481 | PHINode *OpPN = | ||||||||||
1482 | PHINode::Create(OInst->getType(), PN.getNumIncomingValues(), | ||||||||||
1483 | OInst->getName() + ".lcssa", &ExitBlock.front()); | ||||||||||
1484 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) | ||||||||||
1485 | OpPN->addIncoming(OInst, PN.getIncomingBlock(i)); | ||||||||||
1486 | *OI = OpPN; | ||||||||||
1487 | } | ||||||||||
1488 | return New; | ||||||||||
1489 | } | ||||||||||
1490 | |||||||||||
1491 | static void eraseInstruction(Instruction &I, ICFLoopSafetyInfo &SafetyInfo, | ||||||||||
1492 | AliasSetTracker *AST, MemorySSAUpdater *MSSAU) { | ||||||||||
1493 | if (AST) | ||||||||||
1494 | AST->deleteValue(&I); | ||||||||||
1495 | if (MSSAU) | ||||||||||
1496 | MSSAU->removeMemoryAccess(&I); | ||||||||||
1497 | SafetyInfo.removeInstruction(&I); | ||||||||||
1498 | I.eraseFromParent(); | ||||||||||
1499 | } | ||||||||||
1500 | |||||||||||
1501 | static void moveInstructionBefore(Instruction &I, Instruction &Dest, | ||||||||||
1502 | ICFLoopSafetyInfo &SafetyInfo, | ||||||||||
1503 | MemorySSAUpdater *MSSAU, | ||||||||||
1504 | ScalarEvolution *SE) { | ||||||||||
1505 | SafetyInfo.removeInstruction(&I); | ||||||||||
1506 | SafetyInfo.insertInstructionTo(&I, Dest.getParent()); | ||||||||||
1507 | I.moveBefore(&Dest); | ||||||||||
1508 | if (MSSAU) | ||||||||||
1509 | if (MemoryUseOrDef *OldMemAcc = cast_or_null<MemoryUseOrDef>( | ||||||||||
1510 | MSSAU->getMemorySSA()->getMemoryAccess(&I))) | ||||||||||
1511 | MSSAU->moveToPlace(OldMemAcc, Dest.getParent(), | ||||||||||
1512 | MemorySSA::BeforeTerminator); | ||||||||||
1513 | if (SE) | ||||||||||
1514 | SE->forgetValue(&I); | ||||||||||
1515 | } | ||||||||||
1516 | |||||||||||
1517 | static Instruction *sinkThroughTriviallyReplaceablePHI( | ||||||||||
1518 | PHINode *TPN, Instruction *I, LoopInfo *LI, | ||||||||||
1519 | SmallDenseMap<BasicBlock *, Instruction *, 32> &SunkCopies, | ||||||||||
1520 | const LoopSafetyInfo *SafetyInfo, const Loop *CurLoop, | ||||||||||
1521 | MemorySSAUpdater *MSSAU) { | ||||||||||
1522 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1523, __PRETTY_FUNCTION__)) | ||||||||||
1523 | "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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1523, __PRETTY_FUNCTION__)); | ||||||||||
1524 | BasicBlock *ExitBlock = TPN->getParent(); | ||||||||||
1525 | Instruction *New; | ||||||||||
1526 | auto It = SunkCopies.find(ExitBlock); | ||||||||||
1527 | if (It != SunkCopies.end()) | ||||||||||
1528 | New = It->second; | ||||||||||
1529 | else | ||||||||||
1530 | New = SunkCopies[ExitBlock] = cloneInstructionInExitBlock( | ||||||||||
1531 | *I, *ExitBlock, *TPN, LI, SafetyInfo, MSSAU); | ||||||||||
1532 | return New; | ||||||||||
1533 | } | ||||||||||
1534 | |||||||||||
1535 | static bool canSplitPredecessors(PHINode *PN, LoopSafetyInfo *SafetyInfo) { | ||||||||||
1536 | BasicBlock *BB = PN->getParent(); | ||||||||||
1537 | if (!BB->canSplitPredecessors()) | ||||||||||
1538 | return false; | ||||||||||
1539 | // It's not impossible to split EHPad blocks, but if BlockColors already exist | ||||||||||
1540 | // it require updating BlockColors for all offspring blocks accordingly. By | ||||||||||
1541 | // skipping such corner case, we can make updating BlockColors after splitting | ||||||||||
1542 | // predecessor fairly simple. | ||||||||||
1543 | if (!SafetyInfo->getBlockColors().empty() && BB->getFirstNonPHI()->isEHPad()) | ||||||||||
1544 | return false; | ||||||||||
1545 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { | ||||||||||
1546 | BasicBlock *BBPred = *PI; | ||||||||||
1547 | if (isa<IndirectBrInst>(BBPred->getTerminator()) || | ||||||||||
1548 | isa<CallBrInst>(BBPred->getTerminator())) | ||||||||||
1549 | return false; | ||||||||||
1550 | } | ||||||||||
1551 | return true; | ||||||||||
1552 | } | ||||||||||
1553 | |||||||||||
1554 | static void splitPredecessorsOfLoopExit(PHINode *PN, DominatorTree *DT, | ||||||||||
1555 | LoopInfo *LI, const Loop *CurLoop, | ||||||||||
1556 | LoopSafetyInfo *SafetyInfo, | ||||||||||
1557 | MemorySSAUpdater *MSSAU) { | ||||||||||
1558 | #ifndef NDEBUG | ||||||||||
1559 | SmallVector<BasicBlock *, 32> ExitBlocks; | ||||||||||
1560 | CurLoop->getUniqueExitBlocks(ExitBlocks); | ||||||||||
1561 | SmallPtrSet<BasicBlock *, 32> ExitBlockSet(ExitBlocks.begin(), | ||||||||||
1562 | ExitBlocks.end()); | ||||||||||
1563 | #endif | ||||||||||
1564 | BasicBlock *ExitBB = PN->getParent(); | ||||||||||
1565 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1565, __PRETTY_FUNCTION__)); | ||||||||||
1566 | |||||||||||
1567 | // Split predecessors of the loop exit to make instructions in the loop are | ||||||||||
1568 | // exposed to exit blocks through trivially replaceable PHIs while keeping the | ||||||||||
1569 | // loop in the canonical form where each predecessor of each exit block should | ||||||||||
1570 | // be contained within the loop. For example, this will convert the loop below | ||||||||||
1571 | // from | ||||||||||
1572 | // | ||||||||||
1573 | // LB1: | ||||||||||
1574 | // %v1 = | ||||||||||
1575 | // br %LE, %LB2 | ||||||||||
1576 | // LB2: | ||||||||||
1577 | // %v2 = | ||||||||||
1578 | // br %LE, %LB1 | ||||||||||
1579 | // LE: | ||||||||||
1580 | // %p = phi [%v1, %LB1], [%v2, %LB2] <-- non-trivially replaceable | ||||||||||
1581 | // | ||||||||||
1582 | // to | ||||||||||
1583 | // | ||||||||||
1584 | // LB1: | ||||||||||
1585 | // %v1 = | ||||||||||
1586 | // br %LE.split, %LB2 | ||||||||||
1587 | // LB2: | ||||||||||
1588 | // %v2 = | ||||||||||
1589 | // br %LE.split2, %LB1 | ||||||||||
1590 | // LE.split: | ||||||||||
1591 | // %p1 = phi [%v1, %LB1] <-- trivially replaceable | ||||||||||
1592 | // br %LE | ||||||||||
1593 | // LE.split2: | ||||||||||
1594 | // %p2 = phi [%v2, %LB2] <-- trivially replaceable | ||||||||||
1595 | // br %LE | ||||||||||
1596 | // LE: | ||||||||||
1597 | // %p = phi [%p1, %LE.split], [%p2, %LE.split2] | ||||||||||
1598 | // | ||||||||||
1599 | const auto &BlockColors = SafetyInfo->getBlockColors(); | ||||||||||
1600 | SmallSetVector<BasicBlock *, 8> PredBBs(pred_begin(ExitBB), pred_end(ExitBB)); | ||||||||||
1601 | while (!PredBBs.empty()) { | ||||||||||
1602 | BasicBlock *PredBB = *PredBBs.begin(); | ||||||||||
1603 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1604, __PRETTY_FUNCTION__)) | ||||||||||
1604 | "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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1604, __PRETTY_FUNCTION__)); | ||||||||||
1605 | if (PN->getBasicBlockIndex(PredBB) >= 0) { | ||||||||||
1606 | BasicBlock *NewPred = SplitBlockPredecessors( | ||||||||||
1607 | ExitBB, PredBB, ".split.loop.exit", DT, LI, MSSAU, true); | ||||||||||
1608 | // Since we do not allow splitting EH-block with BlockColors in | ||||||||||
1609 | // canSplitPredecessors(), we can simply assign predecessor's color to | ||||||||||
1610 | // the new block. | ||||||||||
1611 | if (!BlockColors.empty()) | ||||||||||
1612 | // Grab a reference to the ColorVector to be inserted before getting the | ||||||||||
1613 | // reference to the vector we are copying because inserting the new | ||||||||||
1614 | // element in BlockColors might cause the map to be reallocated. | ||||||||||
1615 | SafetyInfo->copyColors(NewPred, PredBB); | ||||||||||
1616 | } | ||||||||||
1617 | PredBBs.remove(PredBB); | ||||||||||
1618 | } | ||||||||||
1619 | } | ||||||||||
1620 | |||||||||||
1621 | /// When an instruction is found to only be used outside of the loop, this | ||||||||||
1622 | /// function moves it to the exit blocks and patches up SSA form as needed. | ||||||||||
1623 | /// This method is guaranteed to remove the original instruction from its | ||||||||||
1624 | /// position, and may either delete it or move it to outside of the loop. | ||||||||||
1625 | /// | ||||||||||
1626 | static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT, | ||||||||||
1627 | BlockFrequencyInfo *BFI, const Loop *CurLoop, | ||||||||||
1628 | ICFLoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU, | ||||||||||
1629 | OptimizationRemarkEmitter *ORE) { | ||||||||||
1630 | LLVM_DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM sinking instruction: " << I << "\n"; } } while (false); | ||||||||||
1631 | ORE->emit([&]() { | ||||||||||
1632 | return OptimizationRemark(DEBUG_TYPE"licm", "InstSunk", &I) | ||||||||||
1633 | << "sinking " << ore::NV("Inst", &I); | ||||||||||
1634 | }); | ||||||||||
1635 | bool Changed = false; | ||||||||||
1636 | if (isa<LoadInst>(I)) | ||||||||||
1637 | ++NumMovedLoads; | ||||||||||
1638 | else if (isa<CallInst>(I)) | ||||||||||
1639 | ++NumMovedCalls; | ||||||||||
1640 | ++NumSunk; | ||||||||||
1641 | |||||||||||
1642 | // Iterate over users to be ready for actual sinking. Replace users via | ||||||||||
1643 | // unreachable blocks with undef and make all user PHIs trivially replaceable. | ||||||||||
1644 | SmallPtrSet<Instruction *, 8> VisitedUsers; | ||||||||||
1645 | for (Value::user_iterator UI = I.user_begin(), UE = I.user_end(); UI != UE;) { | ||||||||||
1646 | auto *User = cast<Instruction>(*UI); | ||||||||||
1647 | Use &U = UI.getUse(); | ||||||||||
1648 | ++UI; | ||||||||||
1649 | |||||||||||
1650 | if (VisitedUsers.count(User) || CurLoop->contains(User)) | ||||||||||
1651 | continue; | ||||||||||
1652 | |||||||||||
1653 | if (!DT->isReachableFromEntry(User->getParent())) { | ||||||||||
1654 | U = UndefValue::get(I.getType()); | ||||||||||
1655 | Changed = true; | ||||||||||
1656 | continue; | ||||||||||
1657 | } | ||||||||||
1658 | |||||||||||
1659 | // The user must be a PHI node. | ||||||||||
1660 | PHINode *PN = cast<PHINode>(User); | ||||||||||
1661 | |||||||||||
1662 | // Surprisingly, instructions can be used outside of loops without any | ||||||||||
1663 | // exits. This can only happen in PHI nodes if the incoming block is | ||||||||||
1664 | // unreachable. | ||||||||||
1665 | BasicBlock *BB = PN->getIncomingBlock(U); | ||||||||||
1666 | if (!DT->isReachableFromEntry(BB)) { | ||||||||||
1667 | U = UndefValue::get(I.getType()); | ||||||||||
1668 | Changed = true; | ||||||||||
1669 | continue; | ||||||||||
1670 | } | ||||||||||
1671 | |||||||||||
1672 | VisitedUsers.insert(PN); | ||||||||||
1673 | if (isTriviallyReplaceablePHI(*PN, I)) | ||||||||||
1674 | continue; | ||||||||||
1675 | |||||||||||
1676 | if (!canSplitPredecessors(PN, SafetyInfo)) | ||||||||||
1677 | return Changed; | ||||||||||
1678 | |||||||||||
1679 | // Split predecessors of the PHI so that we can make users trivially | ||||||||||
1680 | // replaceable. | ||||||||||
1681 | splitPredecessorsOfLoopExit(PN, DT, LI, CurLoop, SafetyInfo, MSSAU); | ||||||||||
1682 | |||||||||||
1683 | // Should rebuild the iterators, as they may be invalidated by | ||||||||||
1684 | // splitPredecessorsOfLoopExit(). | ||||||||||
1685 | UI = I.user_begin(); | ||||||||||
1686 | UE = I.user_end(); | ||||||||||
1687 | } | ||||||||||
1688 | |||||||||||
1689 | if (VisitedUsers.empty()) | ||||||||||
1690 | return Changed; | ||||||||||
1691 | |||||||||||
1692 | #ifndef NDEBUG | ||||||||||
1693 | SmallVector<BasicBlock *, 32> ExitBlocks; | ||||||||||
1694 | CurLoop->getUniqueExitBlocks(ExitBlocks); | ||||||||||
1695 | SmallPtrSet<BasicBlock *, 32> ExitBlockSet(ExitBlocks.begin(), | ||||||||||
1696 | ExitBlocks.end()); | ||||||||||
1697 | #endif | ||||||||||
1698 | |||||||||||
1699 | // Clones of this instruction. Don't create more than one per exit block! | ||||||||||
1700 | SmallDenseMap<BasicBlock *, Instruction *, 32> SunkCopies; | ||||||||||
1701 | |||||||||||
1702 | // If this instruction is only used outside of the loop, then all users are | ||||||||||
1703 | // PHI nodes in exit blocks due to LCSSA form. Just RAUW them with clones of | ||||||||||
1704 | // the instruction. | ||||||||||
1705 | // First check if I is worth sinking for all uses. Sink only when it is worth | ||||||||||
1706 | // across all uses. | ||||||||||
1707 | SmallSetVector<User*, 8> Users(I.user_begin(), I.user_end()); | ||||||||||
1708 | SmallVector<PHINode *, 8> ExitPNs; | ||||||||||
1709 | for (auto *UI : Users) { | ||||||||||
1710 | auto *User = cast<Instruction>(UI); | ||||||||||
1711 | |||||||||||
1712 | if (CurLoop->contains(User)) | ||||||||||
1713 | continue; | ||||||||||
1714 | |||||||||||
1715 | PHINode *PN = cast<PHINode>(User); | ||||||||||
1716 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1717, __PRETTY_FUNCTION__)) | ||||||||||
1717 | "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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1717, __PRETTY_FUNCTION__)); | ||||||||||
1718 | if (!worthSinkOrHoistInst(I, PN->getParent(), ORE, BFI)) { | ||||||||||
1719 | return Changed; | ||||||||||
1720 | } | ||||||||||
1721 | |||||||||||
1722 | ExitPNs.push_back(PN); | ||||||||||
1723 | } | ||||||||||
1724 | |||||||||||
1725 | for (auto *PN : ExitPNs) { | ||||||||||
1726 | |||||||||||
1727 | // The PHI must be trivially replaceable. | ||||||||||
1728 | Instruction *New = sinkThroughTriviallyReplaceablePHI( | ||||||||||
1729 | PN, &I, LI, SunkCopies, SafetyInfo, CurLoop, MSSAU); | ||||||||||
1730 | PN->replaceAllUsesWith(New); | ||||||||||
1731 | eraseInstruction(*PN, *SafetyInfo, nullptr, nullptr); | ||||||||||
1732 | Changed = true; | ||||||||||
1733 | } | ||||||||||
1734 | return Changed; | ||||||||||
1735 | } | ||||||||||
1736 | |||||||||||
1737 | /// When an instruction is found to only use loop invariant operands that | ||||||||||
1738 | /// is safe to hoist, this instruction is called to do the dirty work. | ||||||||||
1739 | /// | ||||||||||
1740 | static void hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop, | ||||||||||
1741 | BasicBlock *Dest, ICFLoopSafetyInfo *SafetyInfo, | ||||||||||
1742 | MemorySSAUpdater *MSSAU, ScalarEvolution *SE, | ||||||||||
1743 | OptimizationRemarkEmitter *ORE) { | ||||||||||
1744 | LLVM_DEBUG(dbgs() << "LICM hoisting to " << Dest->getNameOrAsOperand() << ": "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM hoisting to " << Dest ->getNameOrAsOperand() << ": " << I << "\n" ; } } while (false) | ||||||||||
1745 | << I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM hoisting to " << Dest ->getNameOrAsOperand() << ": " << I << "\n" ; } } while (false); | ||||||||||
1746 | ORE->emit([&]() { | ||||||||||
1747 | return OptimizationRemark(DEBUG_TYPE"licm", "Hoisted", &I) << "hoisting " | ||||||||||
1748 | << ore::NV("Inst", &I); | ||||||||||
1749 | }); | ||||||||||
1750 | |||||||||||
1751 | // Metadata can be dependent on conditions we are hoisting above. | ||||||||||
1752 | // Conservatively strip all metadata on the instruction unless we were | ||||||||||
1753 | // guaranteed to execute I if we entered the loop, in which case the metadata | ||||||||||
1754 | // is valid in the loop preheader. | ||||||||||
1755 | if (I.hasMetadataOtherThanDebugLoc() && | ||||||||||
1756 | // The check on hasMetadataOtherThanDebugLoc is to prevent us from burning | ||||||||||
1757 | // time in isGuaranteedToExecute if we don't actually have anything to | ||||||||||
1758 | // drop. It is a compile time optimization, not required for correctness. | ||||||||||
1759 | !SafetyInfo->isGuaranteedToExecute(I, DT, CurLoop)) | ||||||||||
1760 | I.dropUnknownNonDebugMetadata(); | ||||||||||
1761 | |||||||||||
1762 | if (isa<PHINode>(I)) | ||||||||||
1763 | // Move the new node to the end of the phi list in the destination block. | ||||||||||
1764 | moveInstructionBefore(I, *Dest->getFirstNonPHI(), *SafetyInfo, MSSAU, SE); | ||||||||||
1765 | else | ||||||||||
1766 | // Move the new node to the destination block, before its terminator. | ||||||||||
1767 | moveInstructionBefore(I, *Dest->getTerminator(), *SafetyInfo, MSSAU, SE); | ||||||||||
1768 | |||||||||||
1769 | I.updateLocationAfterHoist(); | ||||||||||
1770 | |||||||||||
1771 | if (isa<LoadInst>(I)) | ||||||||||
1772 | ++NumMovedLoads; | ||||||||||
1773 | else if (isa<CallInst>(I)) | ||||||||||
1774 | ++NumMovedCalls; | ||||||||||
1775 | ++NumHoisted; | ||||||||||
1776 | } | ||||||||||
1777 | |||||||||||
1778 | /// Only sink or hoist an instruction if it is not a trapping instruction, | ||||||||||
1779 | /// or if the instruction is known not to trap when moved to the preheader. | ||||||||||
1780 | /// or if it is a trapping instruction and is guaranteed to execute. | ||||||||||
1781 | static bool isSafeToExecuteUnconditionally(Instruction &Inst, | ||||||||||
1782 | const DominatorTree *DT, | ||||||||||
1783 | const Loop *CurLoop, | ||||||||||
1784 | const LoopSafetyInfo *SafetyInfo, | ||||||||||
1785 | OptimizationRemarkEmitter *ORE, | ||||||||||
1786 | const Instruction *CtxI) { | ||||||||||
1787 | if (isSafeToSpeculativelyExecute(&Inst, CtxI, DT)) | ||||||||||
1788 | return true; | ||||||||||
1789 | |||||||||||
1790 | bool GuaranteedToExecute = | ||||||||||
1791 | SafetyInfo->isGuaranteedToExecute(Inst, DT, CurLoop); | ||||||||||
1792 | |||||||||||
1793 | if (!GuaranteedToExecute) { | ||||||||||
1794 | auto *LI = dyn_cast<LoadInst>(&Inst); | ||||||||||
1795 | if (LI && CurLoop->isLoopInvariant(LI->getPointerOperand())) | ||||||||||
1796 | ORE->emit([&]() { | ||||||||||
1797 | return OptimizationRemarkMissed( | ||||||||||
1798 | DEBUG_TYPE"licm", "LoadWithLoopInvariantAddressCondExecuted", LI) | ||||||||||
1799 | << "failed to hoist load with loop-invariant address " | ||||||||||
1800 | "because load is conditionally executed"; | ||||||||||
1801 | }); | ||||||||||
1802 | } | ||||||||||
1803 | |||||||||||
1804 | return GuaranteedToExecute; | ||||||||||
1805 | } | ||||||||||
1806 | |||||||||||
1807 | namespace { | ||||||||||
1808 | class LoopPromoter : public LoadAndStorePromoter { | ||||||||||
1809 | Value *SomePtr; // Designated pointer to store to. | ||||||||||
1810 | const SmallSetVector<Value *, 8> &PointerMustAliases; | ||||||||||
1811 | SmallVectorImpl<BasicBlock *> &LoopExitBlocks; | ||||||||||
1812 | SmallVectorImpl<Instruction *> &LoopInsertPts; | ||||||||||
1813 | SmallVectorImpl<MemoryAccess *> &MSSAInsertPts; | ||||||||||
1814 | PredIteratorCache &PredCache; | ||||||||||
1815 | AliasSetTracker *AST; | ||||||||||
1816 | MemorySSAUpdater *MSSAU; | ||||||||||
1817 | LoopInfo &LI; | ||||||||||
1818 | DebugLoc DL; | ||||||||||
1819 | int Alignment; | ||||||||||
1820 | bool UnorderedAtomic; | ||||||||||
1821 | AAMDNodes AATags; | ||||||||||
1822 | ICFLoopSafetyInfo &SafetyInfo; | ||||||||||
1823 | |||||||||||
1824 | Value *maybeInsertLCSSAPHI(Value *V, BasicBlock *BB) const { | ||||||||||
1825 | if (Instruction *I = dyn_cast<Instruction>(V)) | ||||||||||
1826 | if (Loop *L = LI.getLoopFor(I->getParent())) | ||||||||||
1827 | if (!L->contains(BB)) { | ||||||||||
1828 | // We need to create an LCSSA PHI node for the incoming value and | ||||||||||
1829 | // store that. | ||||||||||
1830 | PHINode *PN = PHINode::Create(I->getType(), PredCache.size(BB), | ||||||||||
1831 | I->getName() + ".lcssa", &BB->front()); | ||||||||||
1832 | for (BasicBlock *Pred : PredCache.get(BB)) | ||||||||||
1833 | PN->addIncoming(I, Pred); | ||||||||||
1834 | return PN; | ||||||||||
1835 | } | ||||||||||
1836 | return V; | ||||||||||
1837 | } | ||||||||||
1838 | |||||||||||
1839 | public: | ||||||||||
1840 | LoopPromoter(Value *SP, ArrayRef<const Instruction *> Insts, SSAUpdater &S, | ||||||||||
1841 | const SmallSetVector<Value *, 8> &PMA, | ||||||||||
1842 | SmallVectorImpl<BasicBlock *> &LEB, | ||||||||||
1843 | SmallVectorImpl<Instruction *> &LIP, | ||||||||||
1844 | SmallVectorImpl<MemoryAccess *> &MSSAIP, PredIteratorCache &PIC, | ||||||||||
1845 | AliasSetTracker *ast, MemorySSAUpdater *MSSAU, LoopInfo &li, | ||||||||||
1846 | DebugLoc dl, int alignment, bool UnorderedAtomic, | ||||||||||
1847 | const AAMDNodes &AATags, ICFLoopSafetyInfo &SafetyInfo) | ||||||||||
1848 | : LoadAndStorePromoter(Insts, S), SomePtr(SP), PointerMustAliases(PMA), | ||||||||||
1849 | LoopExitBlocks(LEB), LoopInsertPts(LIP), MSSAInsertPts(MSSAIP), | ||||||||||
1850 | PredCache(PIC), AST(ast), MSSAU(MSSAU), LI(li), DL(std::move(dl)), | ||||||||||
1851 | Alignment(alignment), UnorderedAtomic(UnorderedAtomic), AATags(AATags), | ||||||||||
1852 | SafetyInfo(SafetyInfo) {} | ||||||||||
1853 | |||||||||||
1854 | bool isInstInList(Instruction *I, | ||||||||||
1855 | const SmallVectorImpl<Instruction *> &) const override { | ||||||||||
1856 | Value *Ptr; | ||||||||||
1857 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) | ||||||||||
1858 | Ptr = LI->getOperand(0); | ||||||||||
1859 | else | ||||||||||
1860 | Ptr = cast<StoreInst>(I)->getPointerOperand(); | ||||||||||
1861 | return PointerMustAliases.count(Ptr); | ||||||||||
1862 | } | ||||||||||
1863 | |||||||||||
1864 | void doExtraRewritesBeforeFinalDeletion() override { | ||||||||||
1865 | // Insert stores after in the loop exit blocks. Each exit block gets a | ||||||||||
1866 | // store of the live-out values that feed them. Since we've already told | ||||||||||
1867 | // the SSA updater about the defs in the loop and the preheader | ||||||||||
1868 | // definition, it is all set and we can start using it. | ||||||||||
1869 | for (unsigned i = 0, e = LoopExitBlocks.size(); i != e; ++i) { | ||||||||||
1870 | BasicBlock *ExitBlock = LoopExitBlocks[i]; | ||||||||||
1871 | Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock); | ||||||||||
1872 | LiveInValue = maybeInsertLCSSAPHI(LiveInValue, ExitBlock); | ||||||||||
1873 | Value *Ptr = maybeInsertLCSSAPHI(SomePtr, ExitBlock); | ||||||||||
1874 | Instruction *InsertPos = LoopInsertPts[i]; | ||||||||||
1875 | StoreInst *NewSI = new StoreInst(LiveInValue, Ptr, InsertPos); | ||||||||||
1876 | if (UnorderedAtomic) | ||||||||||
1877 | NewSI->setOrdering(AtomicOrdering::Unordered); | ||||||||||
1878 | NewSI->setAlignment(Align(Alignment)); | ||||||||||
1879 | NewSI->setDebugLoc(DL); | ||||||||||
1880 | if (AATags) | ||||||||||
1881 | NewSI->setAAMetadata(AATags); | ||||||||||
1882 | |||||||||||
1883 | if (MSSAU) { | ||||||||||
1884 | MemoryAccess *MSSAInsertPoint = MSSAInsertPts[i]; | ||||||||||
1885 | MemoryAccess *NewMemAcc; | ||||||||||
1886 | if (!MSSAInsertPoint) { | ||||||||||
1887 | NewMemAcc = MSSAU->createMemoryAccessInBB( | ||||||||||
1888 | NewSI, nullptr, NewSI->getParent(), MemorySSA::Beginning); | ||||||||||
1889 | } else { | ||||||||||
1890 | NewMemAcc = | ||||||||||
1891 | MSSAU->createMemoryAccessAfter(NewSI, nullptr, MSSAInsertPoint); | ||||||||||
1892 | } | ||||||||||
1893 | MSSAInsertPts[i] = NewMemAcc; | ||||||||||
1894 | MSSAU->insertDef(cast<MemoryDef>(NewMemAcc), true); | ||||||||||
1895 | // FIXME: true for safety, false may still be correct. | ||||||||||
1896 | } | ||||||||||
1897 | } | ||||||||||
1898 | } | ||||||||||
1899 | |||||||||||
1900 | void replaceLoadWithValue(LoadInst *LI, Value *V) const override { | ||||||||||
1901 | // Update alias analysis. | ||||||||||
1902 | if (AST) | ||||||||||
1903 | AST->copyValue(LI, V); | ||||||||||
1904 | } | ||||||||||
1905 | void instructionDeleted(Instruction *I) const override { | ||||||||||
1906 | SafetyInfo.removeInstruction(I); | ||||||||||
1907 | if (AST) | ||||||||||
1908 | AST->deleteValue(I); | ||||||||||
1909 | if (MSSAU) | ||||||||||
1910 | MSSAU->removeMemoryAccess(I); | ||||||||||
1911 | } | ||||||||||
1912 | }; | ||||||||||
1913 | |||||||||||
1914 | |||||||||||
1915 | /// Return true iff we can prove that a caller of this function can not inspect | ||||||||||
1916 | /// the contents of the provided object in a well defined program. | ||||||||||
1917 | bool isKnownNonEscaping(Value *Object, const TargetLibraryInfo *TLI) { | ||||||||||
1918 | if (isa<AllocaInst>(Object)) | ||||||||||
1919 | // Since the alloca goes out of scope, we know the caller can't retain a | ||||||||||
1920 | // reference to it and be well defined. Thus, we don't need to check for | ||||||||||
1921 | // capture. | ||||||||||
1922 | return true; | ||||||||||
1923 | |||||||||||
1924 | // For all other objects we need to know that the caller can't possibly | ||||||||||
1925 | // have gotten a reference to the object. There are two components of | ||||||||||
1926 | // that: | ||||||||||
1927 | // 1) Object can't be escaped by this function. This is what | ||||||||||
1928 | // PointerMayBeCaptured checks. | ||||||||||
1929 | // 2) Object can't have been captured at definition site. For this, we | ||||||||||
1930 | // need to know the return value is noalias. At the moment, we use a | ||||||||||
1931 | // weaker condition and handle only AllocLikeFunctions (which are | ||||||||||
1932 | // known to be noalias). TODO | ||||||||||
1933 | return isAllocLikeFn(Object, TLI) && | ||||||||||
1934 | !PointerMayBeCaptured(Object, true, true); | ||||||||||
1935 | } | ||||||||||
1936 | |||||||||||
1937 | } // namespace | ||||||||||
1938 | |||||||||||
1939 | /// Try to promote memory values to scalars by sinking stores out of the | ||||||||||
1940 | /// loop and moving loads to before the loop. We do this by looping over | ||||||||||
1941 | /// the stores in the loop, looking for stores to Must pointers which are | ||||||||||
1942 | /// loop invariant. | ||||||||||
1943 | /// | ||||||||||
1944 | bool llvm::promoteLoopAccessesToScalars( | ||||||||||
1945 | const SmallSetVector<Value *, 8> &PointerMustAliases, | ||||||||||
1946 | SmallVectorImpl<BasicBlock *> &ExitBlocks, | ||||||||||
1947 | SmallVectorImpl<Instruction *> &InsertPts, | ||||||||||
1948 | SmallVectorImpl<MemoryAccess *> &MSSAInsertPts, PredIteratorCache &PIC, | ||||||||||
1949 | LoopInfo *LI, DominatorTree *DT, const TargetLibraryInfo *TLI, | ||||||||||
1950 | Loop *CurLoop, AliasSetTracker *CurAST, MemorySSAUpdater *MSSAU, | ||||||||||
1951 | ICFLoopSafetyInfo *SafetyInfo, OptimizationRemarkEmitter *ORE) { | ||||||||||
1952 | // Verify inputs. | ||||||||||
1953 | assert(LI != nullptr && DT != nullptr && CurLoop != nullptr &&((LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && "Unexpected Input to promoteLoopAccessesToScalars" ) ? static_cast<void> (0) : __assert_fail ("LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected Input to promoteLoopAccessesToScalars\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1955, __PRETTY_FUNCTION__)) | ||||||||||
1954 | SafetyInfo != nullptr &&((LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && "Unexpected Input to promoteLoopAccessesToScalars" ) ? static_cast<void> (0) : __assert_fail ("LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected Input to promoteLoopAccessesToScalars\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1955, __PRETTY_FUNCTION__)) | ||||||||||
1955 | "Unexpected Input to promoteLoopAccessesToScalars")((LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && "Unexpected Input to promoteLoopAccessesToScalars" ) ? static_cast<void> (0) : __assert_fail ("LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected Input to promoteLoopAccessesToScalars\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 1955, __PRETTY_FUNCTION__)); | ||||||||||
1956 | |||||||||||
1957 | Value *SomePtr = *PointerMustAliases.begin(); | ||||||||||
1958 | BasicBlock *Preheader = CurLoop->getLoopPreheader(); | ||||||||||
1959 | |||||||||||
1960 | // It is not safe to promote a load/store from the loop if the load/store is | ||||||||||
1961 | // conditional. For example, turning: | ||||||||||
1962 | // | ||||||||||
1963 | // for () { if (c) *P += 1; } | ||||||||||
1964 | // | ||||||||||
1965 | // into: | ||||||||||
1966 | // | ||||||||||
1967 | // tmp = *P; for () { if (c) tmp +=1; } *P = tmp; | ||||||||||
1968 | // | ||||||||||
1969 | // is not safe, because *P may only be valid to access if 'c' is true. | ||||||||||
1970 | // | ||||||||||
1971 | // The safety property divides into two parts: | ||||||||||
1972 | // p1) The memory may not be dereferenceable on entry to the loop. In this | ||||||||||
1973 | // case, we can't insert the required load in the preheader. | ||||||||||
1974 | // p2) The memory model does not allow us to insert a store along any dynamic | ||||||||||
1975 | // path which did not originally have one. | ||||||||||
1976 | // | ||||||||||
1977 | // If at least one store is guaranteed to execute, both properties are | ||||||||||
1978 | // satisfied, and promotion is legal. | ||||||||||
1979 | // | ||||||||||
1980 | // This, however, is not a necessary condition. Even if no store/load is | ||||||||||
1981 | // guaranteed to execute, we can still establish these properties. | ||||||||||
1982 | // We can establish (p1) by proving that hoisting the load into the preheader | ||||||||||
1983 | // is safe (i.e. proving dereferenceability on all paths through the loop). We | ||||||||||
1984 | // can use any access within the alias set to prove dereferenceability, | ||||||||||
1985 | // since they're all must alias. | ||||||||||
1986 | // | ||||||||||
1987 | // There are two ways establish (p2): | ||||||||||
1988 | // a) Prove the location is thread-local. In this case the memory model | ||||||||||
1989 | // requirement does not apply, and stores are safe to insert. | ||||||||||
1990 | // b) Prove a store dominates every exit block. In this case, if an exit | ||||||||||
1991 | // blocks is reached, the original dynamic path would have taken us through | ||||||||||
1992 | // the store, so inserting a store into the exit block is safe. Note that this | ||||||||||
1993 | // is different from the store being guaranteed to execute. For instance, | ||||||||||
1994 | // if an exception is thrown on the first iteration of the loop, the original | ||||||||||
1995 | // store is never executed, but the exit blocks are not executed either. | ||||||||||
1996 | |||||||||||
1997 | bool DereferenceableInPH = false; | ||||||||||
1998 | bool SafeToInsertStore = false; | ||||||||||
1999 | |||||||||||
2000 | SmallVector<Instruction *, 64> LoopUses; | ||||||||||
2001 | |||||||||||
2002 | // We start with an alignment of one and try to find instructions that allow | ||||||||||
2003 | // us to prove better alignment. | ||||||||||
2004 | Align Alignment; | ||||||||||
2005 | // Keep track of which types of access we see | ||||||||||
2006 | bool SawUnorderedAtomic = false; | ||||||||||
2007 | bool SawNotAtomic = false; | ||||||||||
2008 | AAMDNodes AATags; | ||||||||||
2009 | |||||||||||
2010 | const DataLayout &MDL = Preheader->getModule()->getDataLayout(); | ||||||||||
2011 | |||||||||||
2012 | bool IsKnownThreadLocalObject = false; | ||||||||||
2013 | if (SafetyInfo->anyBlockMayThrow()) { | ||||||||||
2014 | // If a loop can throw, we have to insert a store along each unwind edge. | ||||||||||
2015 | // That said, we can't actually make the unwind edge explicit. Therefore, | ||||||||||
2016 | // we have to prove that the store is dead along the unwind edge. We do | ||||||||||
2017 | // this by proving that the caller can't have a reference to the object | ||||||||||
2018 | // after return and thus can't possibly load from the object. | ||||||||||
2019 | Value *Object = getUnderlyingObject(SomePtr); | ||||||||||
2020 | if (!isKnownNonEscaping(Object, TLI)) | ||||||||||
2021 | return false; | ||||||||||
2022 | // Subtlety: Alloca's aren't visible to callers, but *are* potentially | ||||||||||
2023 | // visible to other threads if captured and used during their lifetimes. | ||||||||||
2024 | IsKnownThreadLocalObject = !isa<AllocaInst>(Object); | ||||||||||
2025 | } | ||||||||||
2026 | |||||||||||
2027 | // Check that all of the pointers in the alias set have the same type. We | ||||||||||
2028 | // cannot (yet) promote a memory location that is loaded and stored in | ||||||||||
2029 | // different sizes. While we are at it, collect alignment and AA info. | ||||||||||
2030 | for (Value *ASIV : PointerMustAliases) { | ||||||||||
2031 | // Check that all of the pointers in the alias set have the same type. We | ||||||||||
2032 | // cannot (yet) promote a memory location that is loaded and stored in | ||||||||||
2033 | // different sizes. | ||||||||||
2034 | if (SomePtr->getType() != ASIV->getType()) | ||||||||||
2035 | return false; | ||||||||||
2036 | |||||||||||
2037 | for (User *U : ASIV->users()) { | ||||||||||
2038 | // Ignore instructions that are outside the loop. | ||||||||||
2039 | Instruction *UI = dyn_cast<Instruction>(U); | ||||||||||
2040 | if (!UI || !CurLoop->contains(UI)) | ||||||||||
2041 | continue; | ||||||||||
2042 | |||||||||||
2043 | // If there is an non-load/store instruction in the loop, we can't promote | ||||||||||
2044 | // it. | ||||||||||
2045 | if (LoadInst *Load = dyn_cast<LoadInst>(UI)) { | ||||||||||
2046 | if (!Load->isUnordered()) | ||||||||||
2047 | return false; | ||||||||||
2048 | |||||||||||
2049 | SawUnorderedAtomic |= Load->isAtomic(); | ||||||||||
2050 | SawNotAtomic |= !Load->isAtomic(); | ||||||||||
2051 | |||||||||||
2052 | Align InstAlignment = Load->getAlign(); | ||||||||||
2053 | |||||||||||
2054 | // Note that proving a load safe to speculate requires proving | ||||||||||
2055 | // sufficient alignment at the target location. Proving it guaranteed | ||||||||||
2056 | // to execute does as well. Thus we can increase our guaranteed | ||||||||||
2057 | // alignment as well. | ||||||||||
2058 | if (!DereferenceableInPH || (InstAlignment > Alignment)) | ||||||||||
2059 | if (isSafeToExecuteUnconditionally(*Load, DT, CurLoop, SafetyInfo, | ||||||||||
2060 | ORE, Preheader->getTerminator())) { | ||||||||||
2061 | DereferenceableInPH = true; | ||||||||||
2062 | Alignment = std::max(Alignment, InstAlignment); | ||||||||||
2063 | } | ||||||||||
2064 | } else if (const StoreInst *Store = dyn_cast<StoreInst>(UI)) { | ||||||||||
2065 | // Stores *of* the pointer are not interesting, only stores *to* the | ||||||||||
2066 | // pointer. | ||||||||||
2067 | if (UI->getOperand(1) != ASIV) | ||||||||||
2068 | continue; | ||||||||||
2069 | if (!Store->isUnordered()) | ||||||||||
2070 | return false; | ||||||||||
2071 | |||||||||||
2072 | SawUnorderedAtomic |= Store->isAtomic(); | ||||||||||
2073 | SawNotAtomic |= !Store->isAtomic(); | ||||||||||
2074 | |||||||||||
2075 | // If the store is guaranteed to execute, both properties are satisfied. | ||||||||||
2076 | // We may want to check if a store is guaranteed to execute even if we | ||||||||||
2077 | // already know that promotion is safe, since it may have higher | ||||||||||
2078 | // alignment than any other guaranteed stores, in which case we can | ||||||||||
2079 | // raise the alignment on the promoted store. | ||||||||||
2080 | Align InstAlignment = Store->getAlign(); | ||||||||||
2081 | |||||||||||
2082 | if (!DereferenceableInPH || !SafeToInsertStore || | ||||||||||
2083 | (InstAlignment > Alignment)) { | ||||||||||
2084 | if (SafetyInfo->isGuaranteedToExecute(*UI, DT, CurLoop)) { | ||||||||||
2085 | DereferenceableInPH = true; | ||||||||||
2086 | SafeToInsertStore = true; | ||||||||||
2087 | Alignment = std::max(Alignment, InstAlignment); | ||||||||||
2088 | } | ||||||||||
2089 | } | ||||||||||
2090 | |||||||||||
2091 | // If a store dominates all exit blocks, it is safe to sink. | ||||||||||
2092 | // As explained above, if an exit block was executed, a dominating | ||||||||||
2093 | // store must have been executed at least once, so we are not | ||||||||||
2094 | // introducing stores on paths that did not have them. | ||||||||||
2095 | // Note that this only looks at explicit exit blocks. If we ever | ||||||||||
2096 | // start sinking stores into unwind edges (see above), this will break. | ||||||||||
2097 | if (!SafeToInsertStore) | ||||||||||
2098 | SafeToInsertStore = llvm::all_of(ExitBlocks, [&](BasicBlock *Exit) { | ||||||||||
2099 | return DT->dominates(Store->getParent(), Exit); | ||||||||||
2100 | }); | ||||||||||
2101 | |||||||||||
2102 | // If the store is not guaranteed to execute, we may still get | ||||||||||
2103 | // deref info through it. | ||||||||||
2104 | if (!DereferenceableInPH) { | ||||||||||
2105 | DereferenceableInPH = isDereferenceableAndAlignedPointer( | ||||||||||
2106 | Store->getPointerOperand(), Store->getValueOperand()->getType(), | ||||||||||
2107 | Store->getAlign(), MDL, Preheader->getTerminator(), DT); | ||||||||||
2108 | } | ||||||||||
2109 | } else | ||||||||||
2110 | return false; // Not a load or store. | ||||||||||
2111 | |||||||||||
2112 | // Merge the AA tags. | ||||||||||
2113 | if (LoopUses.empty()) { | ||||||||||
2114 | // On the first load/store, just take its AA tags. | ||||||||||
2115 | UI->getAAMetadata(AATags); | ||||||||||
2116 | } else if (AATags) { | ||||||||||
2117 | UI->getAAMetadata(AATags, /* Merge = */ true); | ||||||||||
2118 | } | ||||||||||
2119 | |||||||||||
2120 | LoopUses.push_back(UI); | ||||||||||
2121 | } | ||||||||||
2122 | } | ||||||||||
2123 | |||||||||||
2124 | // If we found both an unordered atomic instruction and a non-atomic memory | ||||||||||
2125 | // access, bail. We can't blindly promote non-atomic to atomic since we | ||||||||||
2126 | // might not be able to lower the result. We can't downgrade since that | ||||||||||
2127 | // would violate memory model. Also, align 0 is an error for atomics. | ||||||||||
2128 | if (SawUnorderedAtomic && SawNotAtomic) | ||||||||||
2129 | return false; | ||||||||||
2130 | |||||||||||
2131 | // If we're inserting an atomic load in the preheader, we must be able to | ||||||||||
2132 | // lower it. We're only guaranteed to be able to lower naturally aligned | ||||||||||
2133 | // atomics. | ||||||||||
2134 | auto *SomePtrElemType = SomePtr->getType()->getPointerElementType(); | ||||||||||
2135 | if (SawUnorderedAtomic && | ||||||||||
2136 | Alignment < MDL.getTypeStoreSize(SomePtrElemType)) | ||||||||||
2137 | return false; | ||||||||||
2138 | |||||||||||
2139 | // If we couldn't prove we can hoist the load, bail. | ||||||||||
2140 | if (!DereferenceableInPH) | ||||||||||
2141 | return false; | ||||||||||
2142 | |||||||||||
2143 | // We know we can hoist the load, but don't have a guaranteed store. | ||||||||||
2144 | // Check whether the location is thread-local. If it is, then we can insert | ||||||||||
2145 | // stores along paths which originally didn't have them without violating the | ||||||||||
2146 | // memory model. | ||||||||||
2147 | if (!SafeToInsertStore) { | ||||||||||
2148 | if (IsKnownThreadLocalObject) | ||||||||||
2149 | SafeToInsertStore = true; | ||||||||||
2150 | else { | ||||||||||
2151 | Value *Object = getUnderlyingObject(SomePtr); | ||||||||||
2152 | SafeToInsertStore = | ||||||||||
2153 | (isAllocLikeFn(Object, TLI) || isa<AllocaInst>(Object)) && | ||||||||||
2154 | !PointerMayBeCaptured(Object, true, true); | ||||||||||
2155 | } | ||||||||||
2156 | } | ||||||||||
2157 | |||||||||||
2158 | // If we've still failed to prove we can sink the store, give up. | ||||||||||
2159 | if (!SafeToInsertStore) | ||||||||||
2160 | return false; | ||||||||||
2161 | |||||||||||
2162 | // Otherwise, this is safe to promote, lets do it! | ||||||||||
2163 | 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) | ||||||||||
2164 | << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM: Promoting value stored to in loop: " << *SomePtr << '\n'; } } while (false); | ||||||||||
2165 | ORE->emit([&]() { | ||||||||||
2166 | return OptimizationRemark(DEBUG_TYPE"licm", "PromoteLoopAccessesToScalar", | ||||||||||
2167 | LoopUses[0]) | ||||||||||
2168 | << "Moving accesses to memory location out of the loop"; | ||||||||||
2169 | }); | ||||||||||
2170 | ++NumPromoted; | ||||||||||
2171 | |||||||||||
2172 | // Look at all the loop uses, and try to merge their locations. | ||||||||||
2173 | std::vector<const DILocation *> LoopUsesLocs; | ||||||||||
2174 | for (auto U : LoopUses) | ||||||||||
2175 | LoopUsesLocs.push_back(U->getDebugLoc().get()); | ||||||||||
2176 | auto DL = DebugLoc(DILocation::getMergedLocations(LoopUsesLocs)); | ||||||||||
2177 | |||||||||||
2178 | // We use the SSAUpdater interface to insert phi nodes as required. | ||||||||||
2179 | SmallVector<PHINode *, 16> NewPHIs; | ||||||||||
2180 | SSAUpdater SSA(&NewPHIs); | ||||||||||
2181 | LoopPromoter Promoter(SomePtr, LoopUses, SSA, PointerMustAliases, ExitBlocks, | ||||||||||
2182 | InsertPts, MSSAInsertPts, PIC, CurAST, MSSAU, *LI, DL, | ||||||||||
2183 | Alignment.value(), SawUnorderedAtomic, AATags, | ||||||||||
2184 | *SafetyInfo); | ||||||||||
2185 | |||||||||||
2186 | // Set up the preheader to have a definition of the value. It is the live-out | ||||||||||
2187 | // value from the preheader that uses in the loop will use. | ||||||||||
2188 | LoadInst *PreheaderLoad = new LoadInst( | ||||||||||
2189 | SomePtr->getType()->getPointerElementType(), SomePtr, | ||||||||||
2190 | SomePtr->getName() + ".promoted", Preheader->getTerminator()); | ||||||||||
2191 | if (SawUnorderedAtomic) | ||||||||||
2192 | PreheaderLoad->setOrdering(AtomicOrdering::Unordered); | ||||||||||
2193 | PreheaderLoad->setAlignment(Alignment); | ||||||||||
2194 | PreheaderLoad->setDebugLoc(DebugLoc()); | ||||||||||
2195 | if (AATags) | ||||||||||
2196 | PreheaderLoad->setAAMetadata(AATags); | ||||||||||
2197 | SSA.AddAvailableValue(Preheader, PreheaderLoad); | ||||||||||
2198 | |||||||||||
2199 | if (MSSAU) { | ||||||||||
2200 | MemoryAccess *PreheaderLoadMemoryAccess = MSSAU->createMemoryAccessInBB( | ||||||||||
2201 | PreheaderLoad, nullptr, PreheaderLoad->getParent(), MemorySSA::End); | ||||||||||
2202 | MemoryUse *NewMemUse = cast<MemoryUse>(PreheaderLoadMemoryAccess); | ||||||||||
2203 | MSSAU->insertUse(NewMemUse, /*RenameUses=*/true); | ||||||||||
2204 | } | ||||||||||
2205 | |||||||||||
2206 | if (MSSAU && VerifyMemorySSA) | ||||||||||
2207 | MSSAU->getMemorySSA()->verifyMemorySSA(); | ||||||||||
2208 | // Rewrite all the loads in the loop and remember all the definitions from | ||||||||||
2209 | // stores in the loop. | ||||||||||
2210 | Promoter.run(LoopUses); | ||||||||||
2211 | |||||||||||
2212 | if (MSSAU && VerifyMemorySSA) | ||||||||||
2213 | MSSAU->getMemorySSA()->verifyMemorySSA(); | ||||||||||
2214 | // If the SSAUpdater didn't use the load in the preheader, just zap it now. | ||||||||||
2215 | if (PreheaderLoad->use_empty()) | ||||||||||
2216 | eraseInstruction(*PreheaderLoad, *SafetyInfo, CurAST, MSSAU); | ||||||||||
2217 | |||||||||||
2218 | return true; | ||||||||||
2219 | } | ||||||||||
2220 | |||||||||||
2221 | /// Returns an owning pointer to an alias set which incorporates aliasing info | ||||||||||
2222 | /// from L and all subloops of L. | ||||||||||
2223 | std::unique_ptr<AliasSetTracker> | ||||||||||
2224 | LoopInvariantCodeMotion::collectAliasInfoForLoop(Loop *L, LoopInfo *LI, | ||||||||||
2225 | AAResults *AA) { | ||||||||||
2226 | auto CurAST = std::make_unique<AliasSetTracker>(*AA); | ||||||||||
2227 | |||||||||||
2228 | // Add everything from all the sub loops. | ||||||||||
2229 | for (Loop *InnerL : L->getSubLoops()) | ||||||||||
2230 | for (BasicBlock *BB : InnerL->blocks()) | ||||||||||
2231 | CurAST->add(*BB); | ||||||||||
2232 | |||||||||||
2233 | // And merge in this loop (without anything from inner loops). | ||||||||||
2234 | for (BasicBlock *BB : L->blocks()) | ||||||||||
2235 | if (LI->getLoopFor(BB) == L) | ||||||||||
2236 | CurAST->add(*BB); | ||||||||||
2237 | |||||||||||
2238 | return CurAST; | ||||||||||
2239 | } | ||||||||||
2240 | |||||||||||
2241 | std::unique_ptr<AliasSetTracker> | ||||||||||
2242 | LoopInvariantCodeMotion::collectAliasInfoForLoopWithMSSA( | ||||||||||
2243 | Loop *L, AAResults *AA, MemorySSAUpdater *MSSAU) { | ||||||||||
2244 | auto *MSSA = MSSAU->getMemorySSA(); | ||||||||||
2245 | auto CurAST = std::make_unique<AliasSetTracker>(*AA, MSSA, L); | ||||||||||
2246 | CurAST->addAllInstructionsInLoopUsingMSSA(); | ||||||||||
2247 | return CurAST; | ||||||||||
2248 | } | ||||||||||
2249 | |||||||||||
2250 | static bool pointerInvalidatedByLoop(MemoryLocation MemLoc, | ||||||||||
2251 | AliasSetTracker *CurAST, Loop *CurLoop, | ||||||||||
2252 | AAResults *AA) { | ||||||||||
2253 | // First check to see if any of the basic blocks in CurLoop invalidate *V. | ||||||||||
2254 | bool isInvalidatedAccordingToAST = CurAST->getAliasSetFor(MemLoc).isMod(); | ||||||||||
2255 | |||||||||||
2256 | if (!isInvalidatedAccordingToAST || !LICMN2Theshold) | ||||||||||
2257 | return isInvalidatedAccordingToAST; | ||||||||||
2258 | |||||||||||
2259 | // Check with a diagnostic analysis if we can refine the information above. | ||||||||||
2260 | // This is to identify the limitations of using the AST. | ||||||||||
2261 | // The alias set mechanism used by LICM has a major weakness in that it | ||||||||||
2262 | // combines all things which may alias into a single set *before* asking | ||||||||||
2263 | // modref questions. As a result, a single readonly call within a loop will | ||||||||||
2264 | // collapse all loads and stores into a single alias set and report | ||||||||||
2265 | // invalidation if the loop contains any store. For example, readonly calls | ||||||||||
2266 | // with deopt states have this form and create a general alias set with all | ||||||||||
2267 | // loads and stores. In order to get any LICM in loops containing possible | ||||||||||
2268 | // deopt states we need a more precise invalidation of checking the mod ref | ||||||||||
2269 | // info of each instruction within the loop and LI. This has a complexity of | ||||||||||
2270 | // O(N^2), so currently, it is used only as a diagnostic tool since the | ||||||||||
2271 | // default value of LICMN2Threshold is zero. | ||||||||||
2272 | |||||||||||
2273 | // Don't look at nested loops. | ||||||||||
2274 | if (CurLoop->begin() != CurLoop->end()) | ||||||||||
2275 | return true; | ||||||||||
2276 | |||||||||||
2277 | int N = 0; | ||||||||||
2278 | for (BasicBlock *BB : CurLoop->getBlocks()) | ||||||||||
2279 | for (Instruction &I : *BB) { | ||||||||||
2280 | if (N >= LICMN2Theshold) { | ||||||||||
2281 | 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) | ||||||||||
2282 | << *(MemLoc.Ptr) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "Alasing N2 threshold exhausted for " << *(MemLoc.Ptr) << "\n"; } } while (false); | ||||||||||
2283 | return true; | ||||||||||
2284 | } | ||||||||||
2285 | N++; | ||||||||||
2286 | auto Res = AA->getModRefInfo(&I, MemLoc); | ||||||||||
2287 | if (isModSet(Res)) { | ||||||||||
2288 | 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 ) | ||||||||||
2289 | << *(MemLoc.Ptr) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "Aliasing failed on " << I << " for " << *(MemLoc.Ptr) << "\n"; } } while (false ); | ||||||||||
2290 | return true; | ||||||||||
2291 | } | ||||||||||
2292 | } | ||||||||||
2293 | 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); | ||||||||||
2294 | return false; | ||||||||||
2295 | } | ||||||||||
2296 | |||||||||||
2297 | bool pointerInvalidatedByLoopWithMSSA(MemorySSA *MSSA, MemoryUse *MU, | ||||||||||
2298 | Loop *CurLoop, Instruction &I, | ||||||||||
2299 | SinkAndHoistLICMFlags &Flags) { | ||||||||||
2300 | // For hoisting, use the walker to determine safety | ||||||||||
2301 | if (!Flags.getIsSink()) { | ||||||||||
2302 | MemoryAccess *Source; | ||||||||||
2303 | // See declaration of SetLicmMssaOptCap for usage details. | ||||||||||
2304 | if (Flags.tooManyClobberingCalls()) | ||||||||||
2305 | Source = MU->getDefiningAccess(); | ||||||||||
2306 | else { | ||||||||||
2307 | Source = MSSA->getSkipSelfWalker()->getClobberingMemoryAccess(MU); | ||||||||||
2308 | Flags.incrementClobberingCalls(); | ||||||||||
2309 | } | ||||||||||
2310 | return !MSSA->isLiveOnEntryDef(Source) && | ||||||||||
2311 | CurLoop->contains(Source->getBlock()); | ||||||||||
2312 | } | ||||||||||
2313 | |||||||||||
2314 | // For sinking, we'd need to check all Defs below this use. The getClobbering | ||||||||||
2315 | // call will look on the backedge of the loop, but will check aliasing with | ||||||||||
2316 | // the instructions on the previous iteration. | ||||||||||
2317 | // For example: | ||||||||||
2318 | // for (i ... ) | ||||||||||
2319 | // load a[i] ( Use (LoE) | ||||||||||
2320 | // store a[i] ( 1 = Def (2), with 2 = Phi for the loop. | ||||||||||
2321 | // i++; | ||||||||||
2322 | // The load sees no clobbering inside the loop, as the backedge alias check | ||||||||||
2323 | // does phi translation, and will check aliasing against store a[i-1]. | ||||||||||
2324 | // However sinking the load outside the loop, below the store is incorrect. | ||||||||||
2325 | |||||||||||
2326 | // For now, only sink if there are no Defs in the loop, and the existing ones | ||||||||||
2327 | // precede the use and are in the same block. | ||||||||||
2328 | // FIXME: Increase precision: Safe to sink if Use post dominates the Def; | ||||||||||
2329 | // needs PostDominatorTreeAnalysis. | ||||||||||
2330 | // FIXME: More precise: no Defs that alias this Use. | ||||||||||
2331 | if (Flags.tooManyMemoryAccesses()) | ||||||||||
2332 | return true; | ||||||||||
2333 | for (auto *BB : CurLoop->getBlocks()) | ||||||||||
2334 | if (pointerInvalidatedByBlockWithMSSA(*BB, *MSSA, *MU)) | ||||||||||
2335 | return true; | ||||||||||
2336 | // When sinking, the source block may not be part of the loop so check it. | ||||||||||
2337 | if (!CurLoop->contains(&I)) | ||||||||||
2338 | return pointerInvalidatedByBlockWithMSSA(*I.getParent(), *MSSA, *MU); | ||||||||||
2339 | |||||||||||
2340 | return false; | ||||||||||
2341 | } | ||||||||||
2342 | |||||||||||
2343 | bool pointerInvalidatedByBlockWithMSSA(BasicBlock &BB, MemorySSA &MSSA, | ||||||||||
2344 | MemoryUse &MU) { | ||||||||||
2345 | if (const auto *Accesses = MSSA.getBlockDefs(&BB)) | ||||||||||
2346 | for (const auto &MA : *Accesses) | ||||||||||
2347 | if (const auto *MD = dyn_cast<MemoryDef>(&MA)) | ||||||||||
2348 | if (MU.getBlock() != MD->getBlock() || !MSSA.locallyDominates(MD, &MU)) | ||||||||||
2349 | return true; | ||||||||||
2350 | return false; | ||||||||||
2351 | } | ||||||||||
2352 | |||||||||||
2353 | /// Little predicate that returns true if the specified basic block is in | ||||||||||
2354 | /// a subloop of the current one, not the current one itself. | ||||||||||
2355 | /// | ||||||||||
2356 | static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI) { | ||||||||||
2357 | 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-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/LICM.cpp" , 2357, __PRETTY_FUNCTION__)); | ||||||||||
2358 | return LI->getLoopFor(BB) != CurLoop; | ||||||||||
2359 | } |
1 | //===- llvm/InstrTypes.h - Important Instruction subclasses -----*- 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 defines various meta classes of instructions that exist in the VM |
10 | // representation. Specific concrete subclasses of these may be found in the |
11 | // i*.h files... |
12 | // |
13 | //===----------------------------------------------------------------------===// |
14 | |
15 | #ifndef LLVM_IR_INSTRTYPES_H |
16 | #define LLVM_IR_INSTRTYPES_H |
17 | |
18 | #include "llvm/ADT/ArrayRef.h" |
19 | #include "llvm/ADT/None.h" |
20 | #include "llvm/ADT/Optional.h" |
21 | #include "llvm/ADT/STLExtras.h" |
22 | #include "llvm/ADT/StringMap.h" |
23 | #include "llvm/ADT/StringRef.h" |
24 | #include "llvm/ADT/Twine.h" |
25 | #include "llvm/ADT/iterator_range.h" |
26 | #include "llvm/IR/Attributes.h" |
27 | #include "llvm/IR/CallingConv.h" |
28 | #include "llvm/IR/Constants.h" |
29 | #include "llvm/IR/DerivedTypes.h" |
30 | #include "llvm/IR/Function.h" |
31 | #include "llvm/IR/Instruction.h" |
32 | #include "llvm/IR/LLVMContext.h" |
33 | #include "llvm/IR/OperandTraits.h" |
34 | #include "llvm/IR/Type.h" |
35 | #include "llvm/IR/User.h" |
36 | #include "llvm/IR/Value.h" |
37 | #include "llvm/Support/Casting.h" |
38 | #include "llvm/Support/ErrorHandling.h" |
39 | #include <algorithm> |
40 | #include <cassert> |
41 | #include <cstddef> |
42 | #include <cstdint> |
43 | #include <iterator> |
44 | #include <string> |
45 | #include <vector> |
46 | |
47 | namespace llvm { |
48 | |
49 | namespace Intrinsic { |
50 | typedef unsigned ID; |
51 | } |
52 | |
53 | //===----------------------------------------------------------------------===// |
54 | // UnaryInstruction Class |
55 | //===----------------------------------------------------------------------===// |
56 | |
57 | class UnaryInstruction : public Instruction { |
58 | protected: |
59 | UnaryInstruction(Type *Ty, unsigned iType, Value *V, |
60 | Instruction *IB = nullptr) |
61 | : Instruction(Ty, iType, &Op<0>(), 1, IB) { |
62 | Op<0>() = V; |
63 | } |
64 | UnaryInstruction(Type *Ty, unsigned iType, Value *V, BasicBlock *IAE) |
65 | : Instruction(Ty, iType, &Op<0>(), 1, IAE) { |
66 | Op<0>() = V; |
67 | } |
68 | |
69 | public: |
70 | // allocate space for exactly one operand |
71 | void *operator new(size_t s) { |
72 | return User::operator new(s, 1); |
73 | } |
74 | |
75 | /// Transparently provide more efficient getOperand methods. |
76 | 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; |
77 | |
78 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
79 | static bool classof(const Instruction *I) { |
80 | return I->isUnaryOp() || |
81 | I->getOpcode() == Instruction::Alloca || |
82 | I->getOpcode() == Instruction::Load || |
83 | I->getOpcode() == Instruction::VAArg || |
84 | I->getOpcode() == Instruction::ExtractValue || |
85 | (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd); |
86 | } |
87 | static bool classof(const Value *V) { |
88 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
89 | } |
90 | }; |
91 | |
92 | template <> |
93 | struct OperandTraits<UnaryInstruction> : |
94 | public FixedNumOperandTraits<UnaryInstruction, 1> { |
95 | }; |
96 | |
97 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)UnaryInstruction::op_iterator UnaryInstruction::op_begin() { return OperandTraits<UnaryInstruction>::op_begin(this); } UnaryInstruction ::const_op_iterator UnaryInstruction::op_begin() const { return OperandTraits<UnaryInstruction>::op_begin(const_cast< UnaryInstruction*>(this)); } UnaryInstruction::op_iterator UnaryInstruction::op_end() { return OperandTraits<UnaryInstruction >::op_end(this); } UnaryInstruction::const_op_iterator UnaryInstruction ::op_end() const { return OperandTraits<UnaryInstruction> ::op_end(const_cast<UnaryInstruction*>(this)); } Value * UnaryInstruction::getOperand(unsigned i_nocapture) const { (( i_nocapture < OperandTraits<UnaryInstruction>::operands (this) && "getOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<UnaryInstruction>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 97, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<UnaryInstruction>::op_begin(const_cast< UnaryInstruction*>(this))[i_nocapture].get()); } void UnaryInstruction ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<UnaryInstruction>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<UnaryInstruction>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 97, __PRETTY_FUNCTION__)); OperandTraits<UnaryInstruction >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned UnaryInstruction::getNumOperands() const { return OperandTraits <UnaryInstruction>::operands(this); } template <int Idx_nocapture > Use &UnaryInstruction::Op() { return this->OpFrom <Idx_nocapture>(this); } template <int Idx_nocapture > const Use &UnaryInstruction::Op() const { return this ->OpFrom<Idx_nocapture>(this); } |
98 | |
99 | //===----------------------------------------------------------------------===// |
100 | // UnaryOperator Class |
101 | //===----------------------------------------------------------------------===// |
102 | |
103 | class UnaryOperator : public UnaryInstruction { |
104 | void AssertOK(); |
105 | |
106 | protected: |
107 | UnaryOperator(UnaryOps iType, Value *S, Type *Ty, |
108 | const Twine &Name, Instruction *InsertBefore); |
109 | UnaryOperator(UnaryOps iType, Value *S, Type *Ty, |
110 | const Twine &Name, BasicBlock *InsertAtEnd); |
111 | |
112 | // Note: Instruction needs to be a friend here to call cloneImpl. |
113 | friend class Instruction; |
114 | |
115 | UnaryOperator *cloneImpl() const; |
116 | |
117 | public: |
118 | |
119 | /// Construct a unary instruction, given the opcode and an operand. |
120 | /// Optionally (if InstBefore is specified) insert the instruction |
121 | /// into a BasicBlock right before the specified instruction. The specified |
122 | /// Instruction is allowed to be a dereferenced end iterator. |
123 | /// |
124 | static UnaryOperator *Create(UnaryOps Op, Value *S, |
125 | const Twine &Name = Twine(), |
126 | Instruction *InsertBefore = nullptr); |
127 | |
128 | /// Construct a unary instruction, given the opcode and an operand. |
129 | /// Also automatically insert this instruction to the end of the |
130 | /// BasicBlock specified. |
131 | /// |
132 | static UnaryOperator *Create(UnaryOps Op, Value *S, |
133 | const Twine &Name, |
134 | BasicBlock *InsertAtEnd); |
135 | |
136 | /// These methods just forward to Create, and are useful when you |
137 | /// statically know what type of instruction you're going to create. These |
138 | /// helpers just save some typing. |
139 | #define HANDLE_UNARY_INST(N, OPC, CLASS) \ |
140 | static UnaryOperator *Create##OPC(Value *V, const Twine &Name = "") {\ |
141 | return Create(Instruction::OPC, V, Name);\ |
142 | } |
143 | #include "llvm/IR/Instruction.def" |
144 | #define HANDLE_UNARY_INST(N, OPC, CLASS) \ |
145 | static UnaryOperator *Create##OPC(Value *V, const Twine &Name, \ |
146 | BasicBlock *BB) {\ |
147 | return Create(Instruction::OPC, V, Name, BB);\ |
148 | } |
149 | #include "llvm/IR/Instruction.def" |
150 | #define HANDLE_UNARY_INST(N, OPC, CLASS) \ |
151 | static UnaryOperator *Create##OPC(Value *V, const Twine &Name, \ |
152 | Instruction *I) {\ |
153 | return Create(Instruction::OPC, V, Name, I);\ |
154 | } |
155 | #include "llvm/IR/Instruction.def" |
156 | |
157 | static UnaryOperator * |
158 | CreateWithCopiedFlags(UnaryOps Opc, Value *V, Instruction *CopyO, |
159 | const Twine &Name = "", |
160 | Instruction *InsertBefore = nullptr) { |
161 | UnaryOperator *UO = Create(Opc, V, Name, InsertBefore); |
162 | UO->copyIRFlags(CopyO); |
163 | return UO; |
164 | } |
165 | |
166 | static UnaryOperator *CreateFNegFMF(Value *Op, Instruction *FMFSource, |
167 | const Twine &Name = "", |
168 | Instruction *InsertBefore = nullptr) { |
169 | return CreateWithCopiedFlags(Instruction::FNeg, Op, FMFSource, Name, |
170 | InsertBefore); |
171 | } |
172 | |
173 | UnaryOps getOpcode() const { |
174 | return static_cast<UnaryOps>(Instruction::getOpcode()); |
175 | } |
176 | |
177 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
178 | static bool classof(const Instruction *I) { |
179 | return I->isUnaryOp(); |
180 | } |
181 | static bool classof(const Value *V) { |
182 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
183 | } |
184 | }; |
185 | |
186 | //===----------------------------------------------------------------------===// |
187 | // BinaryOperator Class |
188 | //===----------------------------------------------------------------------===// |
189 | |
190 | class BinaryOperator : public Instruction { |
191 | void AssertOK(); |
192 | |
193 | protected: |
194 | BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty, |
195 | const Twine &Name, Instruction *InsertBefore); |
196 | BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty, |
197 | const Twine &Name, BasicBlock *InsertAtEnd); |
198 | |
199 | // Note: Instruction needs to be a friend here to call cloneImpl. |
200 | friend class Instruction; |
201 | |
202 | BinaryOperator *cloneImpl() const; |
203 | |
204 | public: |
205 | // allocate space for exactly two operands |
206 | void *operator new(size_t s) { |
207 | return User::operator new(s, 2); |
208 | } |
209 | |
210 | /// Transparently provide more efficient getOperand methods. |
211 | 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; |
212 | |
213 | /// Construct a binary instruction, given the opcode and the two |
214 | /// operands. Optionally (if InstBefore is specified) insert the instruction |
215 | /// into a BasicBlock right before the specified instruction. The specified |
216 | /// Instruction is allowed to be a dereferenced end iterator. |
217 | /// |
218 | static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2, |
219 | const Twine &Name = Twine(), |
220 | Instruction *InsertBefore = nullptr); |
221 | |
222 | /// Construct a binary instruction, given the opcode and the two |
223 | /// operands. Also automatically insert this instruction to the end of the |
224 | /// BasicBlock specified. |
225 | /// |
226 | static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2, |
227 | const Twine &Name, BasicBlock *InsertAtEnd); |
228 | |
229 | /// These methods just forward to Create, and are useful when you |
230 | /// statically know what type of instruction you're going to create. These |
231 | /// helpers just save some typing. |
232 | #define HANDLE_BINARY_INST(N, OPC, CLASS) \ |
233 | static BinaryOperator *Create##OPC(Value *V1, Value *V2, \ |
234 | const Twine &Name = "") {\ |
235 | return Create(Instruction::OPC, V1, V2, Name);\ |
236 | } |
237 | #include "llvm/IR/Instruction.def" |
238 | #define HANDLE_BINARY_INST(N, OPC, CLASS) \ |
239 | static BinaryOperator *Create##OPC(Value *V1, Value *V2, \ |
240 | const Twine &Name, BasicBlock *BB) {\ |
241 | return Create(Instruction::OPC, V1, V2, Name, BB);\ |
242 | } |
243 | #include "llvm/IR/Instruction.def" |
244 | #define HANDLE_BINARY_INST(N, OPC, CLASS) \ |
245 | static BinaryOperator *Create##OPC(Value *V1, Value *V2, \ |
246 | const Twine &Name, Instruction *I) {\ |
247 | return Create(Instruction::OPC, V1, V2, Name, I);\ |
248 | } |
249 | #include "llvm/IR/Instruction.def" |
250 | |
251 | static BinaryOperator *CreateWithCopiedFlags(BinaryOps Opc, |
252 | Value *V1, Value *V2, |
253 | Instruction *CopyO, |
254 | const Twine &Name = "") { |
255 | BinaryOperator *BO = Create(Opc, V1, V2, Name); |
256 | BO->copyIRFlags(CopyO); |
257 | return BO; |
258 | } |
259 | |
260 | static BinaryOperator *CreateFAddFMF(Value *V1, Value *V2, |
261 | Instruction *FMFSource, |
262 | const Twine &Name = "") { |
263 | return CreateWithCopiedFlags(Instruction::FAdd, V1, V2, FMFSource, Name); |
264 | } |
265 | static BinaryOperator *CreateFSubFMF(Value *V1, Value *V2, |
266 | Instruction *FMFSource, |
267 | const Twine &Name = "") { |
268 | return CreateWithCopiedFlags(Instruction::FSub, V1, V2, FMFSource, Name); |
269 | } |
270 | static BinaryOperator *CreateFMulFMF(Value *V1, Value *V2, |
271 | Instruction *FMFSource, |
272 | const Twine &Name = "") { |
273 | return CreateWithCopiedFlags(Instruction::FMul, V1, V2, FMFSource, Name); |
274 | } |
275 | static BinaryOperator *CreateFDivFMF(Value *V1, Value *V2, |
276 | Instruction *FMFSource, |
277 | const Twine &Name = "") { |
278 | return CreateWithCopiedFlags(Instruction::FDiv, V1, V2, FMFSource, Name); |
279 | } |
280 | static BinaryOperator *CreateFRemFMF(Value *V1, Value *V2, |
281 | Instruction *FMFSource, |
282 | const Twine &Name = "") { |
283 | return CreateWithCopiedFlags(Instruction::FRem, V1, V2, FMFSource, Name); |
284 | } |
285 | |
286 | static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2, |
287 | const Twine &Name = "") { |
288 | BinaryOperator *BO = Create(Opc, V1, V2, Name); |
289 | BO->setHasNoSignedWrap(true); |
290 | return BO; |
291 | } |
292 | static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2, |
293 | const Twine &Name, BasicBlock *BB) { |
294 | BinaryOperator *BO = Create(Opc, V1, V2, Name, BB); |
295 | BO->setHasNoSignedWrap(true); |
296 | return BO; |
297 | } |
298 | static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2, |
299 | const Twine &Name, Instruction *I) { |
300 | BinaryOperator *BO = Create(Opc, V1, V2, Name, I); |
301 | BO->setHasNoSignedWrap(true); |
302 | return BO; |
303 | } |
304 | |
305 | static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2, |
306 | const Twine &Name = "") { |
307 | BinaryOperator *BO = Create(Opc, V1, V2, Name); |
308 | BO->setHasNoUnsignedWrap(true); |
309 | return BO; |
310 | } |
311 | static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2, |
312 | const Twine &Name, BasicBlock *BB) { |
313 | BinaryOperator *BO = Create(Opc, V1, V2, Name, BB); |
314 | BO->setHasNoUnsignedWrap(true); |
315 | return BO; |
316 | } |
317 | static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2, |
318 | const Twine &Name, Instruction *I) { |
319 | BinaryOperator *BO = Create(Opc, V1, V2, Name, I); |
320 | BO->setHasNoUnsignedWrap(true); |
321 | return BO; |
322 | } |
323 | |
324 | static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2, |
325 | const Twine &Name = "") { |
326 | BinaryOperator *BO = Create(Opc, V1, V2, Name); |
327 | BO->setIsExact(true); |
328 | return BO; |
329 | } |
330 | static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2, |
331 | const Twine &Name, BasicBlock *BB) { |
332 | BinaryOperator *BO = Create(Opc, V1, V2, Name, BB); |
333 | BO->setIsExact(true); |
334 | return BO; |
335 | } |
336 | static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2, |
337 | const Twine &Name, Instruction *I) { |
338 | BinaryOperator *BO = Create(Opc, V1, V2, Name, I); |
339 | BO->setIsExact(true); |
340 | return BO; |
341 | } |
342 | |
343 | #define DEFINE_HELPERS(OPC, NUWNSWEXACT) \ |
344 | static BinaryOperator *Create##NUWNSWEXACT##OPC(Value *V1, Value *V2, \ |
345 | const Twine &Name = "") { \ |
346 | return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name); \ |
347 | } \ |
348 | static BinaryOperator *Create##NUWNSWEXACT##OPC( \ |
349 | Value *V1, Value *V2, const Twine &Name, BasicBlock *BB) { \ |
350 | return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name, BB); \ |
351 | } \ |
352 | static BinaryOperator *Create##NUWNSWEXACT##OPC( \ |
353 | Value *V1, Value *V2, const Twine &Name, Instruction *I) { \ |
354 | return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name, I); \ |
355 | } |
356 | |
357 | DEFINE_HELPERS(Add, NSW) // CreateNSWAdd |
358 | DEFINE_HELPERS(Add, NUW) // CreateNUWAdd |
359 | DEFINE_HELPERS(Sub, NSW) // CreateNSWSub |
360 | DEFINE_HELPERS(Sub, NUW) // CreateNUWSub |
361 | DEFINE_HELPERS(Mul, NSW) // CreateNSWMul |
362 | DEFINE_HELPERS(Mul, NUW) // CreateNUWMul |
363 | DEFINE_HELPERS(Shl, NSW) // CreateNSWShl |
364 | DEFINE_HELPERS(Shl, NUW) // CreateNUWShl |
365 | |
366 | DEFINE_HELPERS(SDiv, Exact) // CreateExactSDiv |
367 | DEFINE_HELPERS(UDiv, Exact) // CreateExactUDiv |
368 | DEFINE_HELPERS(AShr, Exact) // CreateExactAShr |
369 | DEFINE_HELPERS(LShr, Exact) // CreateExactLShr |
370 | |
371 | #undef DEFINE_HELPERS |
372 | |
373 | /// Helper functions to construct and inspect unary operations (NEG and NOT) |
374 | /// via binary operators SUB and XOR: |
375 | /// |
376 | /// Create the NEG and NOT instructions out of SUB and XOR instructions. |
377 | /// |
378 | static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "", |
379 | Instruction *InsertBefore = nullptr); |
380 | static BinaryOperator *CreateNeg(Value *Op, const Twine &Name, |
381 | BasicBlock *InsertAtEnd); |
382 | static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "", |
383 | Instruction *InsertBefore = nullptr); |
384 | static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name, |
385 | BasicBlock *InsertAtEnd); |
386 | static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "", |
387 | Instruction *InsertBefore = nullptr); |
388 | static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name, |
389 | BasicBlock *InsertAtEnd); |
390 | static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "", |
391 | Instruction *InsertBefore = nullptr); |
392 | static BinaryOperator *CreateNot(Value *Op, const Twine &Name, |
393 | BasicBlock *InsertAtEnd); |
394 | |
395 | BinaryOps getOpcode() const { |
396 | return static_cast<BinaryOps>(Instruction::getOpcode()); |
397 | } |
398 | |
399 | /// Exchange the two operands to this instruction. |
400 | /// This instruction is safe to use on any binary instruction and |
401 | /// does not modify the semantics of the instruction. If the instruction |
402 | /// cannot be reversed (ie, it's a Div), then return true. |
403 | /// |
404 | bool swapOperands(); |
405 | |
406 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
407 | static bool classof(const Instruction *I) { |
408 | return I->isBinaryOp(); |
409 | } |
410 | static bool classof(const Value *V) { |
411 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
412 | } |
413 | }; |
414 | |
415 | template <> |
416 | struct OperandTraits<BinaryOperator> : |
417 | public FixedNumOperandTraits<BinaryOperator, 2> { |
418 | }; |
419 | |
420 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)BinaryOperator::op_iterator BinaryOperator::op_begin() { return OperandTraits<BinaryOperator>::op_begin(this); } BinaryOperator ::const_op_iterator BinaryOperator::op_begin() const { return OperandTraits<BinaryOperator>::op_begin(const_cast< BinaryOperator*>(this)); } BinaryOperator::op_iterator BinaryOperator ::op_end() { return OperandTraits<BinaryOperator>::op_end (this); } BinaryOperator::const_op_iterator BinaryOperator::op_end () const { return OperandTraits<BinaryOperator>::op_end (const_cast<BinaryOperator*>(this)); } Value *BinaryOperator ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<BinaryOperator>::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<BinaryOperator>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 420, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<BinaryOperator>::op_begin(const_cast< BinaryOperator*>(this))[i_nocapture].get()); } void BinaryOperator ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<BinaryOperator>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<BinaryOperator>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 420, __PRETTY_FUNCTION__)); OperandTraits<BinaryOperator >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned BinaryOperator::getNumOperands() const { return OperandTraits <BinaryOperator>::operands(this); } template <int Idx_nocapture > Use &BinaryOperator::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &BinaryOperator::Op() const { return this-> OpFrom<Idx_nocapture>(this); } |
421 | |
422 | //===----------------------------------------------------------------------===// |
423 | // CastInst Class |
424 | //===----------------------------------------------------------------------===// |
425 | |
426 | /// This is the base class for all instructions that perform data |
427 | /// casts. It is simply provided so that instruction category testing |
428 | /// can be performed with code like: |
429 | /// |
430 | /// if (isa<CastInst>(Instr)) { ... } |
431 | /// Base class of casting instructions. |
432 | class CastInst : public UnaryInstruction { |
433 | protected: |
434 | /// Constructor with insert-before-instruction semantics for subclasses |
435 | CastInst(Type *Ty, unsigned iType, Value *S, |
436 | const Twine &NameStr = "", Instruction *InsertBefore = nullptr) |
437 | : UnaryInstruction(Ty, iType, S, InsertBefore) { |
438 | setName(NameStr); |
439 | } |
440 | /// Constructor with insert-at-end-of-block semantics for subclasses |
441 | CastInst(Type *Ty, unsigned iType, Value *S, |
442 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
443 | : UnaryInstruction(Ty, iType, S, InsertAtEnd) { |
444 | setName(NameStr); |
445 | } |
446 | |
447 | public: |
448 | /// Provides a way to construct any of the CastInst subclasses using an |
449 | /// opcode instead of the subclass's constructor. The opcode must be in the |
450 | /// CastOps category (Instruction::isCast(opcode) returns true). This |
451 | /// constructor has insert-before-instruction semantics to automatically |
452 | /// insert the new CastInst before InsertBefore (if it is non-null). |
453 | /// Construct any of the CastInst subclasses |
454 | static CastInst *Create( |
455 | Instruction::CastOps, ///< The opcode of the cast instruction |
456 | Value *S, ///< The value to be casted (operand 0) |
457 | Type *Ty, ///< The type to which cast should be made |
458 | const Twine &Name = "", ///< Name for the instruction |
459 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
460 | ); |
461 | /// Provides a way to construct any of the CastInst subclasses using an |
462 | /// opcode instead of the subclass's constructor. The opcode must be in the |
463 | /// CastOps category. This constructor has insert-at-end-of-block semantics |
464 | /// to automatically insert the new CastInst at the end of InsertAtEnd (if |
465 | /// its non-null). |
466 | /// Construct any of the CastInst subclasses |
467 | static CastInst *Create( |
468 | Instruction::CastOps, ///< The opcode for the cast instruction |
469 | Value *S, ///< The value to be casted (operand 0) |
470 | Type *Ty, ///< The type to which operand is casted |
471 | const Twine &Name, ///< The name for the instruction |
472 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
473 | ); |
474 | |
475 | /// Create a ZExt or BitCast cast instruction |
476 | static CastInst *CreateZExtOrBitCast( |
477 | Value *S, ///< The value to be casted (operand 0) |
478 | Type *Ty, ///< The type to which cast should be made |
479 | const Twine &Name = "", ///< Name for the instruction |
480 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
481 | ); |
482 | |
483 | /// Create a ZExt or BitCast cast instruction |
484 | static CastInst *CreateZExtOrBitCast( |
485 | Value *S, ///< The value to be casted (operand 0) |
486 | Type *Ty, ///< The type to which operand is casted |
487 | const Twine &Name, ///< The name for the instruction |
488 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
489 | ); |
490 | |
491 | /// Create a SExt or BitCast cast instruction |
492 | static CastInst *CreateSExtOrBitCast( |
493 | Value *S, ///< The value to be casted (operand 0) |
494 | Type *Ty, ///< The type to which cast should be made |
495 | const Twine &Name = "", ///< Name for the instruction |
496 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
497 | ); |
498 | |
499 | /// Create a SExt or BitCast cast instruction |
500 | static CastInst *CreateSExtOrBitCast( |
501 | Value *S, ///< The value to be casted (operand 0) |
502 | Type *Ty, ///< The type to which operand is casted |
503 | const Twine &Name, ///< The name for the instruction |
504 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
505 | ); |
506 | |
507 | /// Create a BitCast AddrSpaceCast, or a PtrToInt cast instruction. |
508 | static CastInst *CreatePointerCast( |
509 | Value *S, ///< The pointer value to be casted (operand 0) |
510 | Type *Ty, ///< The type to which operand is casted |
511 | const Twine &Name, ///< The name for the instruction |
512 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
513 | ); |
514 | |
515 | /// Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction. |
516 | static CastInst *CreatePointerCast( |
517 | Value *S, ///< The pointer value to be casted (operand 0) |
518 | Type *Ty, ///< The type to which cast should be made |
519 | const Twine &Name = "", ///< Name for the instruction |
520 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
521 | ); |
522 | |
523 | /// Create a BitCast or an AddrSpaceCast cast instruction. |
524 | static CastInst *CreatePointerBitCastOrAddrSpaceCast( |
525 | Value *S, ///< The pointer value to be casted (operand 0) |
526 | Type *Ty, ///< The type to which operand is casted |
527 | const Twine &Name, ///< The name for the instruction |
528 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
529 | ); |
530 | |
531 | /// Create a BitCast or an AddrSpaceCast cast instruction. |
532 | static CastInst *CreatePointerBitCastOrAddrSpaceCast( |
533 | Value *S, ///< The pointer value to be casted (operand 0) |
534 | Type *Ty, ///< The type to which cast should be made |
535 | const Twine &Name = "", ///< Name for the instruction |
536 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
537 | ); |
538 | |
539 | /// Create a BitCast, a PtrToInt, or an IntToPTr cast instruction. |
540 | /// |
541 | /// If the value is a pointer type and the destination an integer type, |
542 | /// creates a PtrToInt cast. If the value is an integer type and the |
543 | /// destination a pointer type, creates an IntToPtr cast. Otherwise, creates |
544 | /// a bitcast. |
545 | static CastInst *CreateBitOrPointerCast( |
546 | Value *S, ///< The pointer value to be casted (operand 0) |
547 | Type *Ty, ///< The type to which cast should be made |
548 | const Twine &Name = "", ///< Name for the instruction |
549 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
550 | ); |
551 | |
552 | /// Create a ZExt, BitCast, or Trunc for int -> int casts. |
553 | static CastInst *CreateIntegerCast( |
554 | Value *S, ///< The pointer value to be casted (operand 0) |
555 | Type *Ty, ///< The type to which cast should be made |
556 | bool isSigned, ///< Whether to regard S as signed or not |
557 | const Twine &Name = "", ///< Name for the instruction |
558 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
559 | ); |
560 | |
561 | /// Create a ZExt, BitCast, or Trunc for int -> int casts. |
562 | static CastInst *CreateIntegerCast( |
563 | Value *S, ///< The integer value to be casted (operand 0) |
564 | Type *Ty, ///< The integer type to which operand is casted |
565 | bool isSigned, ///< Whether to regard S as signed or not |
566 | const Twine &Name, ///< The name for the instruction |
567 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
568 | ); |
569 | |
570 | /// Create an FPExt, BitCast, or FPTrunc for fp -> fp casts |
571 | static CastInst *CreateFPCast( |
572 | Value *S, ///< The floating point value to be casted |
573 | Type *Ty, ///< The floating point type to cast to |
574 | const Twine &Name = "", ///< Name for the instruction |
575 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
576 | ); |
577 | |
578 | /// Create an FPExt, BitCast, or FPTrunc for fp -> fp casts |
579 | static CastInst *CreateFPCast( |
580 | Value *S, ///< The floating point value to be casted |
581 | Type *Ty, ///< The floating point type to cast to |
582 | const Twine &Name, ///< The name for the instruction |
583 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
584 | ); |
585 | |
586 | /// Create a Trunc or BitCast cast instruction |
587 | static CastInst *CreateTruncOrBitCast( |
588 | Value *S, ///< The value to be casted (operand 0) |
589 | Type *Ty, ///< The type to which cast should be made |
590 | const Twine &Name = "", ///< Name for the instruction |
591 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
592 | ); |
593 | |
594 | /// Create a Trunc or BitCast cast instruction |
595 | static CastInst *CreateTruncOrBitCast( |
596 | Value *S, ///< The value to be casted (operand 0) |
597 | Type *Ty, ///< The type to which operand is casted |
598 | const Twine &Name, ///< The name for the instruction |
599 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
600 | ); |
601 | |
602 | /// Check whether a bitcast between these types is valid |
603 | static bool isBitCastable( |
604 | Type *SrcTy, ///< The Type from which the value should be cast. |
605 | Type *DestTy ///< The Type to which the value should be cast. |
606 | ); |
607 | |
608 | /// Check whether a bitcast, inttoptr, or ptrtoint cast between these |
609 | /// types is valid and a no-op. |
610 | /// |
611 | /// This ensures that any pointer<->integer cast has enough bits in the |
612 | /// integer and any other cast is a bitcast. |
613 | static bool isBitOrNoopPointerCastable( |
614 | Type *SrcTy, ///< The Type from which the value should be cast. |
615 | Type *DestTy, ///< The Type to which the value should be cast. |
616 | const DataLayout &DL); |
617 | |
618 | /// Returns the opcode necessary to cast Val into Ty using usual casting |
619 | /// rules. |
620 | /// Infer the opcode for cast operand and type |
621 | static Instruction::CastOps getCastOpcode( |
622 | const Value *Val, ///< The value to cast |
623 | bool SrcIsSigned, ///< Whether to treat the source as signed |
624 | Type *Ty, ///< The Type to which the value should be casted |
625 | bool DstIsSigned ///< Whether to treate the dest. as signed |
626 | ); |
627 | |
628 | /// There are several places where we need to know if a cast instruction |
629 | /// only deals with integer source and destination types. To simplify that |
630 | /// logic, this method is provided. |
631 | /// @returns true iff the cast has only integral typed operand and dest type. |
632 | /// Determine if this is an integer-only cast. |
633 | bool isIntegerCast() const; |
634 | |
635 | /// A lossless cast is one that does not alter the basic value. It implies |
636 | /// a no-op cast but is more stringent, preventing things like int->float, |
637 | /// long->double, or int->ptr. |
638 | /// @returns true iff the cast is lossless. |
639 | /// Determine if this is a lossless cast. |
640 | bool isLosslessCast() const; |
641 | |
642 | /// A no-op cast is one that can be effected without changing any bits. |
643 | /// It implies that the source and destination types are the same size. The |
644 | /// DataLayout argument is to determine the pointer size when examining casts |
645 | /// involving Integer and Pointer types. They are no-op casts if the integer |
646 | /// is the same size as the pointer. However, pointer size varies with |
647 | /// platform. Note that a precondition of this method is that the cast is |
648 | /// legal - i.e. the instruction formed with these operands would verify. |
649 | static bool isNoopCast( |
650 | Instruction::CastOps Opcode, ///< Opcode of cast |
651 | Type *SrcTy, ///< SrcTy of cast |
652 | Type *DstTy, ///< DstTy of cast |
653 | const DataLayout &DL ///< DataLayout to get the Int Ptr type from. |
654 | ); |
655 | |
656 | /// Determine if this cast is a no-op cast. |
657 | /// |
658 | /// \param DL is the DataLayout to determine pointer size. |
659 | bool isNoopCast(const DataLayout &DL) const; |
660 | |
661 | /// Determine how a pair of casts can be eliminated, if they can be at all. |
662 | /// This is a helper function for both CastInst and ConstantExpr. |
663 | /// @returns 0 if the CastInst pair can't be eliminated, otherwise |
664 | /// returns Instruction::CastOps value for a cast that can replace |
665 | /// the pair, casting SrcTy to DstTy. |
666 | /// Determine if a cast pair is eliminable |
667 | static unsigned isEliminableCastPair( |
668 | Instruction::CastOps firstOpcode, ///< Opcode of first cast |
669 | Instruction::CastOps secondOpcode, ///< Opcode of second cast |
670 | Type *SrcTy, ///< SrcTy of 1st cast |
671 | Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast |
672 | Type *DstTy, ///< DstTy of 2nd cast |
673 | Type *SrcIntPtrTy, ///< Integer type corresponding to Ptr SrcTy, or null |
674 | Type *MidIntPtrTy, ///< Integer type corresponding to Ptr MidTy, or null |
675 | Type *DstIntPtrTy ///< Integer type corresponding to Ptr DstTy, or null |
676 | ); |
677 | |
678 | /// Return the opcode of this CastInst |
679 | Instruction::CastOps getOpcode() const { |
680 | return Instruction::CastOps(Instruction::getOpcode()); |
681 | } |
682 | |
683 | /// Return the source type, as a convenience |
684 | Type* getSrcTy() const { return getOperand(0)->getType(); } |
685 | /// Return the destination type, as a convenience |
686 | Type* getDestTy() const { return getType(); } |
687 | |
688 | /// This method can be used to determine if a cast from SrcTy to DstTy using |
689 | /// Opcode op is valid or not. |
690 | /// @returns true iff the proposed cast is valid. |
691 | /// Determine if a cast is valid without creating one. |
692 | static bool castIsValid(Instruction::CastOps op, Type *SrcTy, Type *DstTy); |
693 | static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) { |
694 | return castIsValid(op, S->getType(), DstTy); |
695 | } |
696 | |
697 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
698 | static bool classof(const Instruction *I) { |
699 | return I->isCast(); |
700 | } |
701 | static bool classof(const Value *V) { |
702 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
703 | } |
704 | }; |
705 | |
706 | //===----------------------------------------------------------------------===// |
707 | // CmpInst Class |
708 | //===----------------------------------------------------------------------===// |
709 | |
710 | /// This class is the base class for the comparison instructions. |
711 | /// Abstract base class of comparison instructions. |
712 | class CmpInst : public Instruction { |
713 | public: |
714 | /// This enumeration lists the possible predicates for CmpInst subclasses. |
715 | /// Values in the range 0-31 are reserved for FCmpInst, while values in the |
716 | /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the |
717 | /// predicate values are not overlapping between the classes. |
718 | /// |
719 | /// Some passes (e.g. InstCombine) depend on the bit-wise characteristics of |
720 | /// FCMP_* values. Changing the bit patterns requires a potential change to |
721 | /// those passes. |
722 | enum Predicate : unsigned { |
723 | // Opcode U L G E Intuitive operation |
724 | FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded) |
725 | FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal |
726 | FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than |
727 | FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal |
728 | FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than |
729 | FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal |
730 | FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal |
731 | FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans) |
732 | FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y) |
733 | FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal |
734 | FCMP_UGT = 10, ///< 1 0 1 0 True if unordered or greater than |
735 | FCMP_UGE = 11, ///< 1 0 1 1 True if unordered, greater than, or equal |
736 | FCMP_ULT = 12, ///< 1 1 0 0 True if unordered or less than |
737 | FCMP_ULE = 13, ///< 1 1 0 1 True if unordered, less than, or equal |
738 | FCMP_UNE = 14, ///< 1 1 1 0 True if unordered or not equal |
739 | FCMP_TRUE = 15, ///< 1 1 1 1 Always true (always folded) |
740 | FIRST_FCMP_PREDICATE = FCMP_FALSE, |
741 | LAST_FCMP_PREDICATE = FCMP_TRUE, |
742 | BAD_FCMP_PREDICATE = FCMP_TRUE + 1, |
743 | ICMP_EQ = 32, ///< equal |
744 | ICMP_NE = 33, ///< not equal |
745 | ICMP_UGT = 34, ///< unsigned greater than |
746 | ICMP_UGE = 35, ///< unsigned greater or equal |
747 | ICMP_ULT = 36, ///< unsigned less than |
748 | ICMP_ULE = 37, ///< unsigned less or equal |
749 | ICMP_SGT = 38, ///< signed greater than |
750 | ICMP_SGE = 39, ///< signed greater or equal |
751 | ICMP_SLT = 40, ///< signed less than |
752 | ICMP_SLE = 41, ///< signed less or equal |
753 | FIRST_ICMP_PREDICATE = ICMP_EQ, |
754 | LAST_ICMP_PREDICATE = ICMP_SLE, |
755 | BAD_ICMP_PREDICATE = ICMP_SLE + 1 |
756 | }; |
757 | using PredicateField = |
758 | Bitfield::Element<Predicate, 0, 6, LAST_ICMP_PREDICATE>; |
759 | |
760 | protected: |
761 | CmpInst(Type *ty, Instruction::OtherOps op, Predicate pred, |
762 | Value *LHS, Value *RHS, const Twine &Name = "", |
763 | Instruction *InsertBefore = nullptr, |
764 | Instruction *FlagsSource = nullptr); |
765 | |
766 | CmpInst(Type *ty, Instruction::OtherOps op, Predicate pred, |
767 | Value *LHS, Value *RHS, const Twine &Name, |
768 | BasicBlock *InsertAtEnd); |
769 | |
770 | public: |
771 | // allocate space for exactly two operands |
772 | void *operator new(size_t s) { |
773 | return User::operator new(s, 2); |
774 | } |
775 | |
776 | /// Construct a compare instruction, given the opcode, the predicate and |
777 | /// the two operands. Optionally (if InstBefore is specified) insert the |
778 | /// instruction into a BasicBlock right before the specified instruction. |
779 | /// The specified Instruction is allowed to be a dereferenced end iterator. |
780 | /// Create a CmpInst |
781 | static CmpInst *Create(OtherOps Op, |
782 | Predicate predicate, Value *S1, |
783 | Value *S2, const Twine &Name = "", |
784 | Instruction *InsertBefore = nullptr); |
785 | |
786 | /// Construct a compare instruction, given the opcode, the predicate and the |
787 | /// two operands. Also automatically insert this instruction to the end of |
788 | /// the BasicBlock specified. |
789 | /// Create a CmpInst |
790 | static CmpInst *Create(OtherOps Op, Predicate predicate, Value *S1, |
791 | Value *S2, const Twine &Name, BasicBlock *InsertAtEnd); |
792 | |
793 | /// Get the opcode casted to the right type |
794 | OtherOps getOpcode() const { |
795 | return static_cast<OtherOps>(Instruction::getOpcode()); |
796 | } |
797 | |
798 | /// Return the predicate for this instruction. |
799 | Predicate getPredicate() const { return getSubclassData<PredicateField>(); } |
800 | |
801 | /// Set the predicate for this instruction to the specified value. |
802 | void setPredicate(Predicate P) { setSubclassData<PredicateField>(P); } |
803 | |
804 | static bool isFPPredicate(Predicate P) { |
805 | static_assert(FIRST_FCMP_PREDICATE == 0, |
806 | "FIRST_FCMP_PREDICATE is required to be 0"); |
807 | return P <= LAST_FCMP_PREDICATE; |
808 | } |
809 | |
810 | static bool isIntPredicate(Predicate P) { |
811 | return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE; |
812 | } |
813 | |
814 | static StringRef getPredicateName(Predicate P); |
815 | |
816 | bool isFPPredicate() const { return isFPPredicate(getPredicate()); } |
817 | bool isIntPredicate() const { return isIntPredicate(getPredicate()); } |
818 | |
819 | /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, |
820 | /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc. |
821 | /// @returns the inverse predicate for the instruction's current predicate. |
822 | /// Return the inverse of the instruction's predicate. |
823 | Predicate getInversePredicate() const { |
824 | return getInversePredicate(getPredicate()); |
825 | } |
826 | |
827 | /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, |
828 | /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc. |
829 | /// @returns the inverse predicate for predicate provided in \p pred. |
830 | /// Return the inverse of a given predicate |
831 | static Predicate getInversePredicate(Predicate pred); |
832 | |
833 | /// For example, EQ->EQ, SLE->SGE, ULT->UGT, |
834 | /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc. |
835 | /// @returns the predicate that would be the result of exchanging the two |
836 | /// operands of the CmpInst instruction without changing the result |
837 | /// produced. |
838 | /// Return the predicate as if the operands were swapped |
839 | Predicate getSwappedPredicate() const { |
840 | return getSwappedPredicate(getPredicate()); |
841 | } |
842 | |
843 | /// This is a static version that you can use without an instruction |
844 | /// available. |
845 | /// Return the predicate as if the operands were swapped. |
846 | static Predicate getSwappedPredicate(Predicate pred); |
847 | |
848 | /// This is a static version that you can use without an instruction |
849 | /// available. |
850 | /// @returns true if the comparison predicate is strict, false otherwise. |
851 | static bool isStrictPredicate(Predicate predicate); |
852 | |
853 | /// @returns true if the comparison predicate is strict, false otherwise. |
854 | /// Determine if this instruction is using an strict comparison predicate. |
855 | bool isStrictPredicate() const { return isStrictPredicate(getPredicate()); } |
856 | |
857 | /// This is a static version that you can use without an instruction |
858 | /// available. |
859 | /// @returns true if the comparison predicate is non-strict, false otherwise. |
860 | static bool isNonStrictPredicate(Predicate predicate); |
861 | |
862 | /// @returns true if the comparison predicate is non-strict, false otherwise. |
863 | /// Determine if this instruction is using an non-strict comparison predicate. |
864 | bool isNonStrictPredicate() const { |
865 | return isNonStrictPredicate(getPredicate()); |
866 | } |
867 | |
868 | /// For example, SGE -> SGT, SLE -> SLT, ULE -> ULT, UGE -> UGT. |
869 | /// Returns the strict version of non-strict comparisons. |
870 | Predicate getStrictPredicate() const { |
871 | return getStrictPredicate(getPredicate()); |
872 | } |
873 | |
874 | /// This is a static version that you can use without an instruction |
875 | /// available. |
876 | /// @returns the strict version of comparison provided in \p pred. |
877 | /// If \p pred is not a strict comparison predicate, returns \p pred. |
878 | /// Returns the strict version of non-strict comparisons. |
879 | static Predicate getStrictPredicate(Predicate pred); |
880 | |
881 | /// For example, SGT -> SGE, SLT -> SLE, ULT -> ULE, UGT -> UGE. |
882 | /// Returns the non-strict version of strict comparisons. |
883 | Predicate getNonStrictPredicate() const { |
884 | return getNonStrictPredicate(getPredicate()); |
885 | } |
886 | |
887 | /// This is a static version that you can use without an instruction |
888 | /// available. |
889 | /// @returns the non-strict version of comparison provided in \p pred. |
890 | /// If \p pred is not a strict comparison predicate, returns \p pred. |
891 | /// Returns the non-strict version of strict comparisons. |
892 | static Predicate getNonStrictPredicate(Predicate pred); |
893 | |
894 | /// This is a static version that you can use without an instruction |
895 | /// available. |
896 | /// Return the flipped strictness of predicate |
897 | static Predicate getFlippedStrictnessPredicate(Predicate pred); |
898 | |
899 | /// For predicate of kind "is X or equal to 0" returns the predicate "is X". |
900 | /// For predicate of kind "is X" returns the predicate "is X or equal to 0". |
901 | /// does not support other kind of predicates. |
902 | /// @returns the predicate that does not contains is equal to zero if |
903 | /// it had and vice versa. |
904 | /// Return the flipped strictness of predicate |
905 | Predicate getFlippedStrictnessPredicate() const { |
906 | return getFlippedStrictnessPredicate(getPredicate()); |
907 | } |
908 | |
909 | /// Provide more efficient getOperand methods. |
910 | 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; |
911 | |
912 | /// This is just a convenience that dispatches to the subclasses. |
913 | /// Swap the operands and adjust predicate accordingly to retain |
914 | /// the same comparison. |
915 | void swapOperands(); |
916 | |
917 | /// This is just a convenience that dispatches to the subclasses. |
918 | /// Determine if this CmpInst is commutative. |
919 | bool isCommutative() const; |
920 | |
921 | /// Determine if this is an equals/not equals predicate. |
922 | /// This is a static version that you can use without an instruction |
923 | /// available. |
924 | static bool isEquality(Predicate pred); |
925 | |
926 | /// Determine if this is an equals/not equals predicate. |
927 | bool isEquality() const { return isEquality(getPredicate()); } |
928 | |
929 | /// Return true if the predicate is relational (not EQ or NE). |
930 | static bool isRelational(Predicate P) { return !isEquality(P); } |
931 | |
932 | /// Return true if the predicate is relational (not EQ or NE). |
933 | bool isRelational() const { return !isEquality(); } |
934 | |
935 | /// @returns true if the comparison is signed, false otherwise. |
936 | /// Determine if this instruction is using a signed comparison. |
937 | bool isSigned() const { |
938 | return isSigned(getPredicate()); |
939 | } |
940 | |
941 | /// @returns true if the comparison is unsigned, false otherwise. |
942 | /// Determine if this instruction is using an unsigned comparison. |
943 | bool isUnsigned() const { |
944 | return isUnsigned(getPredicate()); |
945 | } |
946 | |
947 | /// For example, ULT->SLT, ULE->SLE, UGT->SGT, UGE->SGE, SLT->Failed assert |
948 | /// @returns the signed version of the unsigned predicate pred. |
949 | /// return the signed version of a predicate |
950 | static Predicate getSignedPredicate(Predicate pred); |
951 | |
952 | /// For example, ULT->SLT, ULE->SLE, UGT->SGT, UGE->SGE, SLT->Failed assert |
953 | /// @returns the signed version of the predicate for this instruction (which |
954 | /// has to be an unsigned predicate). |
955 | /// return the signed version of a predicate |
956 | Predicate getSignedPredicate() { |
957 | return getSignedPredicate(getPredicate()); |
958 | } |
959 | |
960 | /// For example, SLT->ULT, SLE->ULE, SGT->UGT, SGE->UGE, ULT->Failed assert |
961 | /// @returns the unsigned version of the signed predicate pred. |
962 | static Predicate getUnsignedPredicate(Predicate pred); |
963 | |
964 | /// For example, SLT->ULT, SLE->ULE, SGT->UGT, SGE->UGE, ULT->Failed assert |
965 | /// @returns the unsigned version of the predicate for this instruction (which |
966 | /// has to be an signed predicate). |
967 | /// return the unsigned version of a predicate |
968 | Predicate getUnsignedPredicate() { |
969 | return getUnsignedPredicate(getPredicate()); |
970 | } |
971 | |
972 | /// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert |
973 | /// @returns the unsigned version of the signed predicate pred or |
974 | /// the signed version of the signed predicate pred. |
975 | static Predicate getFlippedSignednessPredicate(Predicate pred); |
976 | |
977 | /// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert |
978 | /// @returns the unsigned version of the signed predicate pred or |
979 | /// the signed version of the signed predicate pred. |
980 | Predicate getFlippedSignednessPredicate() { |
981 | return getFlippedSignednessPredicate(getPredicate()); |
982 | } |
983 | |
984 | /// This is just a convenience. |
985 | /// Determine if this is true when both operands are the same. |
986 | bool isTrueWhenEqual() const { |
987 | return isTrueWhenEqual(getPredicate()); |
988 | } |
989 | |
990 | /// This is just a convenience. |
991 | /// Determine if this is false when both operands are the same. |
992 | bool isFalseWhenEqual() const { |
993 | return isFalseWhenEqual(getPredicate()); |
994 | } |
995 | |
996 | /// @returns true if the predicate is unsigned, false otherwise. |
997 | /// Determine if the predicate is an unsigned operation. |
998 | static bool isUnsigned(Predicate predicate); |
999 | |
1000 | /// @returns true if the predicate is signed, false otherwise. |
1001 | /// Determine if the predicate is an signed operation. |
1002 | static bool isSigned(Predicate predicate); |
1003 | |
1004 | /// Determine if the predicate is an ordered operation. |
1005 | static bool isOrdered(Predicate predicate); |
1006 | |
1007 | /// Determine if the predicate is an unordered operation. |
1008 | static bool isUnordered(Predicate predicate); |
1009 | |
1010 | /// Determine if the predicate is true when comparing a value with itself. |
1011 | static bool isTrueWhenEqual(Predicate predicate); |
1012 | |
1013 | /// Determine if the predicate is false when comparing a value with itself. |
1014 | static bool isFalseWhenEqual(Predicate predicate); |
1015 | |
1016 | /// Determine if Pred1 implies Pred2 is true when two compares have matching |
1017 | /// operands. |
1018 | static bool isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2); |
1019 | |
1020 | /// Determine if Pred1 implies Pred2 is false when two compares have matching |
1021 | /// operands. |
1022 | static bool isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2); |
1023 | |
1024 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
1025 | static bool classof(const Instruction *I) { |
1026 | return I->getOpcode() == Instruction::ICmp || |
1027 | I->getOpcode() == Instruction::FCmp; |
1028 | } |
1029 | static bool classof(const Value *V) { |
1030 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1031 | } |
1032 | |
1033 | /// Create a result type for fcmp/icmp |
1034 | static Type* makeCmpResultType(Type* opnd_type) { |
1035 | if (VectorType* vt = dyn_cast<VectorType>(opnd_type)) { |
1036 | return VectorType::get(Type::getInt1Ty(opnd_type->getContext()), |
1037 | vt->getElementCount()); |
1038 | } |
1039 | return Type::getInt1Ty(opnd_type->getContext()); |
1040 | } |
1041 | |
1042 | private: |
1043 | // Shadow Value::setValueSubclassData with a private forwarding method so that |
1044 | // subclasses cannot accidentally use it. |
1045 | void setValueSubclassData(unsigned short D) { |
1046 | Value::setValueSubclassData(D); |
1047 | } |
1048 | }; |
1049 | |
1050 | // FIXME: these are redundant if CmpInst < BinaryOperator |
1051 | template <> |
1052 | struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> { |
1053 | }; |
1054 | |
1055 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)CmpInst::op_iterator CmpInst::op_begin() { return OperandTraits <CmpInst>::op_begin(this); } CmpInst::const_op_iterator CmpInst::op_begin() const { return OperandTraits<CmpInst> ::op_begin(const_cast<CmpInst*>(this)); } CmpInst::op_iterator CmpInst::op_end() { return OperandTraits<CmpInst>::op_end (this); } CmpInst::const_op_iterator CmpInst::op_end() const { return OperandTraits<CmpInst>::op_end(const_cast<CmpInst *>(this)); } Value *CmpInst::getOperand(unsigned i_nocapture ) const { ((i_nocapture < OperandTraits<CmpInst>::operands (this) && "getOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<CmpInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1055, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<CmpInst>::op_begin(const_cast<CmpInst *>(this))[i_nocapture].get()); } void CmpInst::setOperand( unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<CmpInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<CmpInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1055, __PRETTY_FUNCTION__)); OperandTraits<CmpInst>:: op_begin(this)[i_nocapture] = Val_nocapture; } unsigned CmpInst ::getNumOperands() const { return OperandTraits<CmpInst> ::operands(this); } template <int Idx_nocapture> Use & CmpInst::Op() { return this->OpFrom<Idx_nocapture>(this ); } template <int Idx_nocapture> const Use &CmpInst ::Op() const { return this->OpFrom<Idx_nocapture>(this ); } |
1056 | |
1057 | /// A lightweight accessor for an operand bundle meant to be passed |
1058 | /// around by value. |
1059 | struct OperandBundleUse { |
1060 | ArrayRef<Use> Inputs; |
1061 | |
1062 | OperandBundleUse() = default; |
1063 | explicit OperandBundleUse(StringMapEntry<uint32_t> *Tag, ArrayRef<Use> Inputs) |
1064 | : Inputs(Inputs), Tag(Tag) {} |
1065 | |
1066 | /// Return true if the operand at index \p Idx in this operand bundle |
1067 | /// has the attribute A. |
1068 | bool operandHasAttr(unsigned Idx, Attribute::AttrKind A) const { |
1069 | if (isDeoptOperandBundle()) |
1070 | if (A == Attribute::ReadOnly || A == Attribute::NoCapture) |
1071 | return Inputs[Idx]->getType()->isPointerTy(); |
1072 | |
1073 | // Conservative answer: no operands have any attributes. |
1074 | return false; |
1075 | } |
1076 | |
1077 | /// Return the tag of this operand bundle as a string. |
1078 | StringRef getTagName() const { |
1079 | return Tag->getKey(); |
1080 | } |
1081 | |
1082 | /// Return the tag of this operand bundle as an integer. |
1083 | /// |
1084 | /// Operand bundle tags are interned by LLVMContextImpl::getOrInsertBundleTag, |
1085 | /// and this function returns the unique integer getOrInsertBundleTag |
1086 | /// associated the tag of this operand bundle to. |
1087 | uint32_t getTagID() const { |
1088 | return Tag->getValue(); |
1089 | } |
1090 | |
1091 | /// Return true if this is a "deopt" operand bundle. |
1092 | bool isDeoptOperandBundle() const { |
1093 | return getTagID() == LLVMContext::OB_deopt; |
1094 | } |
1095 | |
1096 | /// Return true if this is a "funclet" operand bundle. |
1097 | bool isFuncletOperandBundle() const { |
1098 | return getTagID() == LLVMContext::OB_funclet; |
1099 | } |
1100 | |
1101 | /// Return true if this is a "cfguardtarget" operand bundle. |
1102 | bool isCFGuardTargetOperandBundle() const { |
1103 | return getTagID() == LLVMContext::OB_cfguardtarget; |
1104 | } |
1105 | |
1106 | private: |
1107 | /// Pointer to an entry in LLVMContextImpl::getOrInsertBundleTag. |
1108 | StringMapEntry<uint32_t> *Tag; |
1109 | }; |
1110 | |
1111 | /// A container for an operand bundle being viewed as a set of values |
1112 | /// rather than a set of uses. |
1113 | /// |
1114 | /// Unlike OperandBundleUse, OperandBundleDefT owns the memory it carries, and |
1115 | /// so it is possible to create and pass around "self-contained" instances of |
1116 | /// OperandBundleDef and ConstOperandBundleDef. |
1117 | template <typename InputTy> class OperandBundleDefT { |
1118 | std::string Tag; |
1119 | std::vector<InputTy> Inputs; |
1120 | |
1121 | public: |
1122 | explicit OperandBundleDefT(std::string Tag, std::vector<InputTy> Inputs) |
1123 | : Tag(std::move(Tag)), Inputs(std::move(Inputs)) {} |
1124 | explicit OperandBundleDefT(std::string Tag, ArrayRef<InputTy> Inputs) |
1125 | : Tag(std::move(Tag)), Inputs(Inputs) {} |
1126 | |
1127 | explicit OperandBundleDefT(const OperandBundleUse &OBU) { |
1128 | Tag = std::string(OBU.getTagName()); |
1129 | llvm::append_range(Inputs, OBU.Inputs); |
1130 | } |
1131 | |
1132 | ArrayRef<InputTy> inputs() const { return Inputs; } |
1133 | |
1134 | using input_iterator = typename std::vector<InputTy>::const_iterator; |
1135 | |
1136 | size_t input_size() const { return Inputs.size(); } |
1137 | input_iterator input_begin() const { return Inputs.begin(); } |
1138 | input_iterator input_end() const { return Inputs.end(); } |
1139 | |
1140 | StringRef getTag() const { return Tag; } |
1141 | }; |
1142 | |
1143 | using OperandBundleDef = OperandBundleDefT<Value *>; |
1144 | using ConstOperandBundleDef = OperandBundleDefT<const Value *>; |
1145 | |
1146 | //===----------------------------------------------------------------------===// |
1147 | // CallBase Class |
1148 | //===----------------------------------------------------------------------===// |
1149 | |
1150 | /// Base class for all callable instructions (InvokeInst and CallInst) |
1151 | /// Holds everything related to calling a function. |
1152 | /// |
1153 | /// All call-like instructions are required to use a common operand layout: |
1154 | /// - Zero or more arguments to the call, |
1155 | /// - Zero or more operand bundles with zero or more operand inputs each |
1156 | /// bundle, |
1157 | /// - Zero or more subclass controlled operands |
1158 | /// - The called function. |
1159 | /// |
1160 | /// This allows this base class to easily access the called function and the |
1161 | /// start of the arguments without knowing how many other operands a particular |
1162 | /// subclass requires. Note that accessing the end of the argument list isn't |
1163 | /// as cheap as most other operations on the base class. |
1164 | class CallBase : public Instruction { |
1165 | protected: |
1166 | // The first two bits are reserved by CallInst for fast retrieval, |
1167 | using CallInstReservedField = Bitfield::Element<unsigned, 0, 2>; |
1168 | using CallingConvField = |
1169 | Bitfield::Element<CallingConv::ID, CallInstReservedField::NextBit, 10, |
1170 | CallingConv::MaxID>; |
1171 | static_assert( |
1172 | Bitfield::areContiguous<CallInstReservedField, CallingConvField>(), |
1173 | "Bitfields must be contiguous"); |
1174 | |
1175 | /// The last operand is the called operand. |
1176 | static constexpr int CalledOperandOpEndIdx = -1; |
1177 | |
1178 | AttributeList Attrs; ///< parameter attributes for callable |
1179 | FunctionType *FTy; |
1180 | |
1181 | template <class... ArgsTy> |
1182 | CallBase(AttributeList const &A, FunctionType *FT, ArgsTy &&... Args) |
1183 | : Instruction(std::forward<ArgsTy>(Args)...), Attrs(A), FTy(FT) {} |
1184 | |
1185 | using Instruction::Instruction; |
1186 | |
1187 | bool hasDescriptor() const { return Value::HasDescriptor; } |
1188 | |
1189 | unsigned getNumSubclassExtraOperands() const { |
1190 | switch (getOpcode()) { |
1191 | case Instruction::Call: |
1192 | return 0; |
1193 | case Instruction::Invoke: |
1194 | return 2; |
1195 | case Instruction::CallBr: |
1196 | return getNumSubclassExtraOperandsDynamic(); |
1197 | } |
1198 | llvm_unreachable("Invalid opcode!")::llvm::llvm_unreachable_internal("Invalid opcode!", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1198); |
1199 | } |
1200 | |
1201 | /// Get the number of extra operands for instructions that don't have a fixed |
1202 | /// number of extra operands. |
1203 | unsigned getNumSubclassExtraOperandsDynamic() const; |
1204 | |
1205 | public: |
1206 | using Instruction::getContext; |
1207 | |
1208 | /// Create a clone of \p CB with a different set of operand bundles and |
1209 | /// insert it before \p InsertPt. |
1210 | /// |
1211 | /// The returned call instruction is identical \p CB in every way except that |
1212 | /// the operand bundles for the new instruction are set to the operand bundles |
1213 | /// in \p Bundles. |
1214 | static CallBase *Create(CallBase *CB, ArrayRef<OperandBundleDef> Bundles, |
1215 | Instruction *InsertPt = nullptr); |
1216 | |
1217 | static bool classof(const Instruction *I) { |
1218 | return I->getOpcode() == Instruction::Call || |
1219 | I->getOpcode() == Instruction::Invoke || |
1220 | I->getOpcode() == Instruction::CallBr; |
1221 | } |
1222 | static bool classof(const Value *V) { |
1223 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1224 | } |
1225 | |
1226 | FunctionType *getFunctionType() const { return FTy; } |
1227 | |
1228 | void mutateFunctionType(FunctionType *FTy) { |
1229 | Value::mutateType(FTy->getReturnType()); |
1230 | this->FTy = FTy; |
1231 | } |
1232 | |
1233 | 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; |
1234 | |
1235 | /// data_operands_begin/data_operands_end - Return iterators iterating over |
1236 | /// the call / invoke argument list and bundle operands. For invokes, this is |
1237 | /// the set of instruction operands except the invoke target and the two |
1238 | /// successor blocks; and for calls this is the set of instruction operands |
1239 | /// except the call target. |
1240 | User::op_iterator data_operands_begin() { return op_begin(); } |
1241 | User::const_op_iterator data_operands_begin() const { |
1242 | return const_cast<CallBase *>(this)->data_operands_begin(); |
1243 | } |
1244 | User::op_iterator data_operands_end() { |
1245 | // Walk from the end of the operands over the called operand and any |
1246 | // subclass operands. |
1247 | return op_end() - getNumSubclassExtraOperands() - 1; |
1248 | } |
1249 | User::const_op_iterator data_operands_end() const { |
1250 | return const_cast<CallBase *>(this)->data_operands_end(); |
1251 | } |
1252 | iterator_range<User::op_iterator> data_ops() { |
1253 | return make_range(data_operands_begin(), data_operands_end()); |
1254 | } |
1255 | iterator_range<User::const_op_iterator> data_ops() const { |
1256 | return make_range(data_operands_begin(), data_operands_end()); |
1257 | } |
1258 | bool data_operands_empty() const { |
1259 | return data_operands_end() == data_operands_begin(); |
1260 | } |
1261 | unsigned data_operands_size() const { |
1262 | return std::distance(data_operands_begin(), data_operands_end()); |
1263 | } |
1264 | |
1265 | bool isDataOperand(const Use *U) const { |
1266 | assert(this == U->getUser() &&((this == U->getUser() && "Only valid to query with a use of this instruction!" ) ? static_cast<void> (0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1267, __PRETTY_FUNCTION__)) |
1267 | "Only valid to query with a use of this instruction!")((this == U->getUser() && "Only valid to query with a use of this instruction!" ) ? static_cast<void> (0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1267, __PRETTY_FUNCTION__)); |
1268 | return data_operands_begin() <= U && U < data_operands_end(); |
1269 | } |
1270 | bool isDataOperand(Value::const_user_iterator UI) const { |
1271 | return isDataOperand(&UI.getUse()); |
1272 | } |
1273 | |
1274 | /// Given a value use iterator, return the data operand corresponding to it. |
1275 | /// Iterator must actually correspond to a data operand. |
1276 | unsigned getDataOperandNo(Value::const_user_iterator UI) const { |
1277 | return getDataOperandNo(&UI.getUse()); |
1278 | } |
1279 | |
1280 | /// Given a use for a data operand, get the data operand number that |
1281 | /// corresponds to it. |
1282 | unsigned getDataOperandNo(const Use *U) const { |
1283 | assert(isDataOperand(U) && "Data operand # out of range!")((isDataOperand(U) && "Data operand # out of range!") ? static_cast<void> (0) : __assert_fail ("isDataOperand(U) && \"Data operand # out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1283, __PRETTY_FUNCTION__)); |
1284 | return U - data_operands_begin(); |
1285 | } |
1286 | |
1287 | /// Return the iterator pointing to the beginning of the argument list. |
1288 | User::op_iterator arg_begin() { return op_begin(); } |
1289 | User::const_op_iterator arg_begin() const { |
1290 | return const_cast<CallBase *>(this)->arg_begin(); |
1291 | } |
1292 | |
1293 | /// Return the iterator pointing to the end of the argument list. |
1294 | User::op_iterator arg_end() { |
1295 | // From the end of the data operands, walk backwards past the bundle |
1296 | // operands. |
1297 | return data_operands_end() - getNumTotalBundleOperands(); |
1298 | } |
1299 | User::const_op_iterator arg_end() const { |
1300 | return const_cast<CallBase *>(this)->arg_end(); |
1301 | } |
1302 | |
1303 | /// Iteration adapter for range-for loops. |
1304 | iterator_range<User::op_iterator> args() { |
1305 | return make_range(arg_begin(), arg_end()); |
1306 | } |
1307 | iterator_range<User::const_op_iterator> args() const { |
1308 | return make_range(arg_begin(), arg_end()); |
1309 | } |
1310 | bool arg_empty() const { return arg_end() == arg_begin(); } |
1311 | unsigned arg_size() const { return arg_end() - arg_begin(); } |
1312 | |
1313 | // Legacy API names that duplicate the above and will be removed once users |
1314 | // are migrated. |
1315 | iterator_range<User::op_iterator> arg_operands() { |
1316 | return make_range(arg_begin(), arg_end()); |
1317 | } |
1318 | iterator_range<User::const_op_iterator> arg_operands() const { |
1319 | return make_range(arg_begin(), arg_end()); |
1320 | } |
1321 | unsigned getNumArgOperands() const { return arg_size(); } |
1322 | |
1323 | Value *getArgOperand(unsigned i) const { |
1324 | assert(i < getNumArgOperands() && "Out of bounds!")((i < getNumArgOperands() && "Out of bounds!") ? static_cast <void> (0) : __assert_fail ("i < getNumArgOperands() && \"Out of bounds!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1324, __PRETTY_FUNCTION__)); |
1325 | return getOperand(i); |
1326 | } |
1327 | |
1328 | void setArgOperand(unsigned i, Value *v) { |
1329 | assert(i < getNumArgOperands() && "Out of bounds!")((i < getNumArgOperands() && "Out of bounds!") ? static_cast <void> (0) : __assert_fail ("i < getNumArgOperands() && \"Out of bounds!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1329, __PRETTY_FUNCTION__)); |
1330 | setOperand(i, v); |
1331 | } |
1332 | |
1333 | /// Wrappers for getting the \c Use of a call argument. |
1334 | const Use &getArgOperandUse(unsigned i) const { |
1335 | assert(i < getNumArgOperands() && "Out of bounds!")((i < getNumArgOperands() && "Out of bounds!") ? static_cast <void> (0) : __assert_fail ("i < getNumArgOperands() && \"Out of bounds!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1335, __PRETTY_FUNCTION__)); |
1336 | return User::getOperandUse(i); |
1337 | } |
1338 | Use &getArgOperandUse(unsigned i) { |
1339 | assert(i < getNumArgOperands() && "Out of bounds!")((i < getNumArgOperands() && "Out of bounds!") ? static_cast <void> (0) : __assert_fail ("i < getNumArgOperands() && \"Out of bounds!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1339, __PRETTY_FUNCTION__)); |
1340 | return User::getOperandUse(i); |
1341 | } |
1342 | |
1343 | bool isArgOperand(const Use *U) const { |
1344 | assert(this == U->getUser() &&((this == U->getUser() && "Only valid to query with a use of this instruction!" ) ? static_cast<void> (0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1345, __PRETTY_FUNCTION__)) |
1345 | "Only valid to query with a use of this instruction!")((this == U->getUser() && "Only valid to query with a use of this instruction!" ) ? static_cast<void> (0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1345, __PRETTY_FUNCTION__)); |
1346 | return arg_begin() <= U && U < arg_end(); |
1347 | } |
1348 | bool isArgOperand(Value::const_user_iterator UI) const { |
1349 | return isArgOperand(&UI.getUse()); |
1350 | } |
1351 | |
1352 | /// Given a use for a arg operand, get the arg operand number that |
1353 | /// corresponds to it. |
1354 | unsigned getArgOperandNo(const Use *U) const { |
1355 | assert(isArgOperand(U) && "Arg operand # out of range!")((isArgOperand(U) && "Arg operand # out of range!") ? static_cast<void> (0) : __assert_fail ("isArgOperand(U) && \"Arg operand # out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1355, __PRETTY_FUNCTION__)); |
1356 | return U - arg_begin(); |
1357 | } |
1358 | |
1359 | /// Given a value use iterator, return the arg operand number corresponding to |
1360 | /// it. Iterator must actually correspond to a data operand. |
1361 | unsigned getArgOperandNo(Value::const_user_iterator UI) const { |
1362 | return getArgOperandNo(&UI.getUse()); |
1363 | } |
1364 | |
1365 | /// Returns true if this CallSite passes the given Value* as an argument to |
1366 | /// the called function. |
1367 | bool hasArgument(const Value *V) const { |
1368 | return llvm::is_contained(args(), V); |
1369 | } |
1370 | |
1371 | Value *getCalledOperand() const { return Op<CalledOperandOpEndIdx>(); } |
1372 | |
1373 | const Use &getCalledOperandUse() const { return Op<CalledOperandOpEndIdx>(); } |
1374 | Use &getCalledOperandUse() { return Op<CalledOperandOpEndIdx>(); } |
1375 | |
1376 | /// Returns the function called, or null if this is an |
1377 | /// indirect function invocation. |
1378 | Function *getCalledFunction() const { |
1379 | return dyn_cast_or_null<Function>(getCalledOperand()); |
1380 | } |
1381 | |
1382 | /// Return true if the callsite is an indirect call. |
1383 | bool isIndirectCall() const; |
1384 | |
1385 | /// Determine whether the passed iterator points to the callee operand's Use. |
1386 | bool isCallee(Value::const_user_iterator UI) const { |
1387 | return isCallee(&UI.getUse()); |
1388 | } |
1389 | |
1390 | /// Determine whether this Use is the callee operand's Use. |
1391 | bool isCallee(const Use *U) const { return &getCalledOperandUse() == U; } |
1392 | |
1393 | /// Helper to get the caller (the parent function). |
1394 | Function *getCaller(); |
1395 | const Function *getCaller() const { |
1396 | return const_cast<CallBase *>(this)->getCaller(); |
1397 | } |
1398 | |
1399 | /// Tests if this call site must be tail call optimized. Only a CallInst can |
1400 | /// be tail call optimized. |
1401 | bool isMustTailCall() const; |
1402 | |
1403 | /// Tests if this call site is marked as a tail call. |
1404 | bool isTailCall() const; |
1405 | |
1406 | /// Returns the intrinsic ID of the intrinsic called or |
1407 | /// Intrinsic::not_intrinsic if the called function is not an intrinsic, or if |
1408 | /// this is an indirect call. |
1409 | Intrinsic::ID getIntrinsicID() const; |
1410 | |
1411 | void setCalledOperand(Value *V) { Op<CalledOperandOpEndIdx>() = V; } |
1412 | |
1413 | /// Sets the function called, including updating the function type. |
1414 | void setCalledFunction(Function *Fn) { |
1415 | setCalledFunction(Fn->getFunctionType(), Fn); |
1416 | } |
1417 | |
1418 | /// Sets the function called, including updating the function type. |
1419 | void setCalledFunction(FunctionCallee Fn) { |
1420 | setCalledFunction(Fn.getFunctionType(), Fn.getCallee()); |
1421 | } |
1422 | |
1423 | /// Sets the function called, including updating to the specified function |
1424 | /// type. |
1425 | void setCalledFunction(FunctionType *FTy, Value *Fn) { |
1426 | this->FTy = FTy; |
1427 | assert(FTy == cast<FunctionType>(((FTy == cast<FunctionType>( cast<PointerType>(Fn ->getType())->getElementType())) ? static_cast<void> (0) : __assert_fail ("FTy == cast<FunctionType>( cast<PointerType>(Fn->getType())->getElementType())" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1428, __PRETTY_FUNCTION__)) |
1428 | cast<PointerType>(Fn->getType())->getElementType()))((FTy == cast<FunctionType>( cast<PointerType>(Fn ->getType())->getElementType())) ? static_cast<void> (0) : __assert_fail ("FTy == cast<FunctionType>( cast<PointerType>(Fn->getType())->getElementType())" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1428, __PRETTY_FUNCTION__)); |
1429 | // This function doesn't mutate the return type, only the function |
1430 | // type. Seems broken, but I'm just gonna stick an assert in for now. |
1431 | assert(getType() == FTy->getReturnType())((getType() == FTy->getReturnType()) ? static_cast<void > (0) : __assert_fail ("getType() == FTy->getReturnType()" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1431, __PRETTY_FUNCTION__)); |
1432 | setCalledOperand(Fn); |
1433 | } |
1434 | |
1435 | CallingConv::ID getCallingConv() const { |
1436 | return getSubclassData<CallingConvField>(); |
1437 | } |
1438 | |
1439 | void setCallingConv(CallingConv::ID CC) { |
1440 | setSubclassData<CallingConvField>(CC); |
1441 | } |
1442 | |
1443 | /// Check if this call is an inline asm statement. |
1444 | bool isInlineAsm() const { return isa<InlineAsm>(getCalledOperand()); } |
1445 | |
1446 | /// \name Attribute API |
1447 | /// |
1448 | /// These methods access and modify attributes on this call (including |
1449 | /// looking through to the attributes on the called function when necessary). |
1450 | ///@{ |
1451 | |
1452 | /// Return the parameter attributes for this call. |
1453 | /// |
1454 | AttributeList getAttributes() const { return Attrs; } |
1455 | |
1456 | /// Set the parameter attributes for this call. |
1457 | /// |
1458 | void setAttributes(AttributeList A) { Attrs = A; } |
1459 | |
1460 | /// Determine whether this call has the given attribute. If it does not |
1461 | /// then determine if the called function has the attribute, but only if |
1462 | /// the attribute is allowed for the call. |
1463 | bool hasFnAttr(Attribute::AttrKind Kind) const { |
1464 | assert(Kind != Attribute::NoBuiltin &&((Kind != Attribute::NoBuiltin && "Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin" ) ? static_cast<void> (0) : __assert_fail ("Kind != Attribute::NoBuiltin && \"Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1465, __PRETTY_FUNCTION__)) |
1465 | "Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin")((Kind != Attribute::NoBuiltin && "Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin" ) ? static_cast<void> (0) : __assert_fail ("Kind != Attribute::NoBuiltin && \"Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1465, __PRETTY_FUNCTION__)); |
1466 | return hasFnAttrImpl(Kind); |
1467 | } |
1468 | |
1469 | /// Determine whether this call has the given attribute. If it does not |
1470 | /// then determine if the called function has the attribute, but only if |
1471 | /// the attribute is allowed for the call. |
1472 | bool hasFnAttr(StringRef Kind) const { return hasFnAttrImpl(Kind); } |
1473 | |
1474 | /// adds the attribute to the list of attributes. |
1475 | void addAttribute(unsigned i, Attribute::AttrKind Kind) { |
1476 | AttributeList PAL = getAttributes(); |
1477 | PAL = PAL.addAttribute(getContext(), i, Kind); |
1478 | setAttributes(PAL); |
1479 | } |
1480 | |
1481 | /// adds the attribute to the list of attributes. |
1482 | void addAttribute(unsigned i, Attribute Attr) { |
1483 | AttributeList PAL = getAttributes(); |
1484 | PAL = PAL.addAttribute(getContext(), i, Attr); |
1485 | setAttributes(PAL); |
1486 | } |
1487 | |
1488 | /// Adds the attribute to the indicated argument |
1489 | void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { |
1490 | assert(ArgNo < getNumArgOperands() && "Out of bounds")((ArgNo < getNumArgOperands() && "Out of bounds") ? static_cast<void> (0) : __assert_fail ("ArgNo < getNumArgOperands() && \"Out of bounds\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1490, __PRETTY_FUNCTION__)); |
1491 | AttributeList PAL = getAttributes(); |
1492 | PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind); |
1493 | setAttributes(PAL); |
1494 | } |
1495 | |
1496 | /// Adds the attribute to the indicated argument |
1497 | void addParamAttr(unsigned ArgNo, Attribute Attr) { |
1498 | assert(ArgNo < getNumArgOperands() && "Out of bounds")((ArgNo < getNumArgOperands() && "Out of bounds") ? static_cast<void> (0) : __assert_fail ("ArgNo < getNumArgOperands() && \"Out of bounds\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1498, __PRETTY_FUNCTION__)); |
1499 | AttributeList PAL = getAttributes(); |
1500 | PAL = PAL.addParamAttribute(getContext(), ArgNo, Attr); |
1501 | setAttributes(PAL); |
1502 | } |
1503 | |
1504 | /// removes the attribute from the list of attributes. |
1505 | void removeAttribute(unsigned i, Attribute::AttrKind Kind) { |
1506 | AttributeList PAL = getAttributes(); |
1507 | PAL = PAL.removeAttribute(getContext(), i, Kind); |
1508 | setAttributes(PAL); |
1509 | } |
1510 | |
1511 | /// removes the attribute from the list of attributes. |
1512 | void removeAttribute(unsigned i, StringRef Kind) { |
1513 | AttributeList PAL = getAttributes(); |
1514 | PAL = PAL.removeAttribute(getContext(), i, Kind); |
1515 | setAttributes(PAL); |
1516 | } |
1517 | |
1518 | void removeAttributes(unsigned i, const AttrBuilder &Attrs) { |
1519 | AttributeList PAL = getAttributes(); |
1520 | PAL = PAL.removeAttributes(getContext(), i, Attrs); |
1521 | setAttributes(PAL); |
1522 | } |
1523 | |
1524 | /// Removes the attribute from the given argument |
1525 | void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { |
1526 | assert(ArgNo < getNumArgOperands() && "Out of bounds")((ArgNo < getNumArgOperands() && "Out of bounds") ? static_cast<void> (0) : __assert_fail ("ArgNo < getNumArgOperands() && \"Out of bounds\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1526, __PRETTY_FUNCTION__)); |
1527 | AttributeList PAL = getAttributes(); |
1528 | PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind); |
1529 | setAttributes(PAL); |
1530 | } |
1531 | |
1532 | /// Removes the attribute from the given argument |
1533 | void removeParamAttr(unsigned ArgNo, StringRef Kind) { |
1534 | assert(ArgNo < getNumArgOperands() && "Out of bounds")((ArgNo < getNumArgOperands() && "Out of bounds") ? static_cast<void> (0) : __assert_fail ("ArgNo < getNumArgOperands() && \"Out of bounds\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1534, __PRETTY_FUNCTION__)); |
1535 | AttributeList PAL = getAttributes(); |
1536 | PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind); |
1537 | setAttributes(PAL); |
1538 | } |
1539 | |
1540 | /// adds the dereferenceable attribute to the list of attributes. |
1541 | void addDereferenceableAttr(unsigned i, uint64_t Bytes) { |
1542 | AttributeList PAL = getAttributes(); |
1543 | PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes); |
1544 | setAttributes(PAL); |
1545 | } |
1546 | |
1547 | /// adds the dereferenceable_or_null attribute to the list of |
1548 | /// attributes. |
1549 | void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) { |
1550 | AttributeList PAL = getAttributes(); |
1551 | PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes); |
1552 | setAttributes(PAL); |
1553 | } |
1554 | |
1555 | /// Determine whether the return value has the given attribute. |
1556 | bool hasRetAttr(Attribute::AttrKind Kind) const { |
1557 | return hasRetAttrImpl(Kind); |
1558 | } |
1559 | /// Determine whether the return value has the given attribute. |
1560 | bool hasRetAttr(StringRef Kind) const { return hasRetAttrImpl(Kind); } |
1561 | |
1562 | /// Determine whether the argument or parameter has the given attribute. |
1563 | bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const; |
1564 | |
1565 | /// Get the attribute of a given kind at a position. |
1566 | Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const { |
1567 | return getAttributes().getAttribute(i, Kind); |
1568 | } |
1569 | |
1570 | /// Get the attribute of a given kind at a position. |
1571 | Attribute getAttribute(unsigned i, StringRef Kind) const { |
1572 | return getAttributes().getAttribute(i, Kind); |
1573 | } |
1574 | |
1575 | /// Get the attribute of a given kind from a given arg |
1576 | Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const { |
1577 | assert(ArgNo < getNumArgOperands() && "Out of bounds")((ArgNo < getNumArgOperands() && "Out of bounds") ? static_cast<void> (0) : __assert_fail ("ArgNo < getNumArgOperands() && \"Out of bounds\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1577, __PRETTY_FUNCTION__)); |
1578 | return getAttributes().getParamAttr(ArgNo, Kind); |
1579 | } |
1580 | |
1581 | /// Get the attribute of a given kind from a given arg |
1582 | Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const { |
1583 | assert(ArgNo < getNumArgOperands() && "Out of bounds")((ArgNo < getNumArgOperands() && "Out of bounds") ? static_cast<void> (0) : __assert_fail ("ArgNo < getNumArgOperands() && \"Out of bounds\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1583, __PRETTY_FUNCTION__)); |
1584 | return getAttributes().getParamAttr(ArgNo, Kind); |
1585 | } |
1586 | |
1587 | /// Return true if the data operand at index \p i has the attribute \p |
1588 | /// A. |
1589 | /// |
1590 | /// Data operands include call arguments and values used in operand bundles, |
1591 | /// but does not include the callee operand. This routine dispatches to the |
1592 | /// underlying AttributeList or the OperandBundleUser as appropriate. |
1593 | /// |
1594 | /// The index \p i is interpreted as |
1595 | /// |
1596 | /// \p i == Attribute::ReturnIndex -> the return value |
1597 | /// \p i in [1, arg_size + 1) -> argument number (\p i - 1) |
1598 | /// \p i in [arg_size + 1, data_operand_size + 1) -> bundle operand at index |
1599 | /// (\p i - 1) in the operand list. |
1600 | bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const { |
1601 | // Note that we have to add one because `i` isn't zero-indexed. |
1602 | assert(i < (getNumArgOperands() + getNumTotalBundleOperands() + 1) &&((i < (getNumArgOperands() + getNumTotalBundleOperands() + 1) && "Data operand index out of bounds!") ? static_cast <void> (0) : __assert_fail ("i < (getNumArgOperands() + getNumTotalBundleOperands() + 1) && \"Data operand index out of bounds!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1603, __PRETTY_FUNCTION__)) |
1603 | "Data operand index out of bounds!")((i < (getNumArgOperands() + getNumTotalBundleOperands() + 1) && "Data operand index out of bounds!") ? static_cast <void> (0) : __assert_fail ("i < (getNumArgOperands() + getNumTotalBundleOperands() + 1) && \"Data operand index out of bounds!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1603, __PRETTY_FUNCTION__)); |
1604 | |
1605 | // The attribute A can either be directly specified, if the operand in |
1606 | // question is a call argument; or be indirectly implied by the kind of its |
1607 | // containing operand bundle, if the operand is a bundle operand. |
1608 | |
1609 | if (i == AttributeList::ReturnIndex) |
1610 | return hasRetAttr(Kind); |
1611 | |
1612 | // FIXME: Avoid these i - 1 calculations and update the API to use |
1613 | // zero-based indices. |
1614 | if (i < (getNumArgOperands() + 1)) |
1615 | return paramHasAttr(i - 1, Kind); |
1616 | |
1617 | assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&((hasOperandBundles() && i >= (getBundleOperandsStartIndex () + 1) && "Must be either a call argument or an operand bundle!" ) ? static_cast<void> (0) : __assert_fail ("hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) && \"Must be either a call argument or an operand bundle!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1618, __PRETTY_FUNCTION__)) |
1618 | "Must be either a call argument or an operand bundle!")((hasOperandBundles() && i >= (getBundleOperandsStartIndex () + 1) && "Must be either a call argument or an operand bundle!" ) ? static_cast<void> (0) : __assert_fail ("hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) && \"Must be either a call argument or an operand bundle!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1618, __PRETTY_FUNCTION__)); |
1619 | return bundleOperandHasAttr(i - 1, Kind); |
1620 | } |
1621 | |
1622 | /// Determine whether this data operand is not captured. |
1623 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1624 | // better indicate that this may return a conservative answer. |
1625 | bool doesNotCapture(unsigned OpNo) const { |
1626 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::NoCapture); |
1627 | } |
1628 | |
1629 | /// Determine whether this argument is passed by value. |
1630 | bool isByValArgument(unsigned ArgNo) const { |
1631 | return paramHasAttr(ArgNo, Attribute::ByVal); |
1632 | } |
1633 | |
1634 | /// Determine whether this argument is passed in an alloca. |
1635 | bool isInAllocaArgument(unsigned ArgNo) const { |
1636 | return paramHasAttr(ArgNo, Attribute::InAlloca); |
1637 | } |
1638 | |
1639 | /// Determine whether this argument is passed by value, in an alloca, or is |
1640 | /// preallocated. |
1641 | bool isPassPointeeByValueArgument(unsigned ArgNo) const { |
1642 | return paramHasAttr(ArgNo, Attribute::ByVal) || |
1643 | paramHasAttr(ArgNo, Attribute::InAlloca) || |
1644 | paramHasAttr(ArgNo, Attribute::Preallocated); |
1645 | } |
1646 | |
1647 | /// Determine if there are is an inalloca argument. Only the last argument can |
1648 | /// have the inalloca attribute. |
1649 | bool hasInAllocaArgument() const { |
1650 | return !arg_empty() && paramHasAttr(arg_size() - 1, Attribute::InAlloca); |
1651 | } |
1652 | |
1653 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1654 | // better indicate that this may return a conservative answer. |
1655 | bool doesNotAccessMemory(unsigned OpNo) const { |
1656 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); |
1657 | } |
1658 | |
1659 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1660 | // better indicate that this may return a conservative answer. |
1661 | bool onlyReadsMemory(unsigned OpNo) const { |
1662 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadOnly) || |
1663 | dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); |
1664 | } |
1665 | |
1666 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1667 | // better indicate that this may return a conservative answer. |
1668 | bool doesNotReadMemory(unsigned OpNo) const { |
1669 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::WriteOnly) || |
1670 | dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); |
1671 | } |
1672 | |
1673 | LLVM_ATTRIBUTE_DEPRECATED(unsigned getRetAlignment() const,[[deprecated("Use getRetAlign() instead")]] unsigned getRetAlignment () const |
1674 | "Use getRetAlign() instead")[[deprecated("Use getRetAlign() instead")]] unsigned getRetAlignment () const { |
1675 | if (const auto MA = Attrs.getRetAlignment()) |
1676 | return MA->value(); |
1677 | return 0; |
1678 | } |
1679 | |
1680 | /// Extract the alignment of the return value. |
1681 | MaybeAlign getRetAlign() const { return Attrs.getRetAlignment(); } |
1682 | |
1683 | /// Extract the alignment for a call or parameter (0=unknown). |
1684 | LLVM_ATTRIBUTE_DEPRECATED(unsigned getParamAlignment(unsigned ArgNo) const,[[deprecated("Use getParamAlign() instead")]] unsigned getParamAlignment (unsigned ArgNo) const |
1685 | "Use getParamAlign() instead")[[deprecated("Use getParamAlign() instead")]] unsigned getParamAlignment (unsigned ArgNo) const { |
1686 | if (const auto MA = Attrs.getParamAlignment(ArgNo)) |
1687 | return MA->value(); |
1688 | return 0; |
1689 | } |
1690 | |
1691 | /// Extract the alignment for a call or parameter (0=unknown). |
1692 | MaybeAlign getParamAlign(unsigned ArgNo) const { |
1693 | return Attrs.getParamAlignment(ArgNo); |
1694 | } |
1695 | |
1696 | /// Extract the byval type for a call or parameter. |
1697 | Type *getParamByValType(unsigned ArgNo) const { |
1698 | Type *Ty = Attrs.getParamByValType(ArgNo); |
1699 | return Ty ? Ty : getArgOperand(ArgNo)->getType()->getPointerElementType(); |
1700 | } |
1701 | |
1702 | /// Extract the preallocated type for a call or parameter. |
1703 | Type *getParamPreallocatedType(unsigned ArgNo) const { |
1704 | Type *Ty = Attrs.getParamPreallocatedType(ArgNo); |
1705 | return Ty ? Ty : getArgOperand(ArgNo)->getType()->getPointerElementType(); |
1706 | } |
1707 | |
1708 | /// Extract the number of dereferenceable bytes for a call or |
1709 | /// parameter (0=unknown). |
1710 | uint64_t getDereferenceableBytes(unsigned i) const { |
1711 | return Attrs.getDereferenceableBytes(i); |
1712 | } |
1713 | |
1714 | /// Extract the number of dereferenceable_or_null bytes for a call or |
1715 | /// parameter (0=unknown). |
1716 | uint64_t getDereferenceableOrNullBytes(unsigned i) const { |
1717 | return Attrs.getDereferenceableOrNullBytes(i); |
1718 | } |
1719 | |
1720 | /// Return true if the return value is known to be not null. |
1721 | /// This may be because it has the nonnull attribute, or because at least |
1722 | /// one byte is dereferenceable and the pointer is in addrspace(0). |
1723 | bool isReturnNonNull() const; |
1724 | |
1725 | /// Determine if the return value is marked with NoAlias attribute. |
1726 | bool returnDoesNotAlias() const { |
1727 | return Attrs.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias); |
1728 | } |
1729 | |
1730 | /// If one of the arguments has the 'returned' attribute, returns its |
1731 | /// operand value. Otherwise, return nullptr. |
1732 | Value *getReturnedArgOperand() const; |
1733 | |
1734 | /// Return true if the call should not be treated as a call to a |
1735 | /// builtin. |
1736 | bool isNoBuiltin() const { |
1737 | return hasFnAttrImpl(Attribute::NoBuiltin) && |
1738 | !hasFnAttrImpl(Attribute::Builtin); |
1739 | } |
1740 | |
1741 | /// Determine if the call requires strict floating point semantics. |
1742 | bool isStrictFP() const { return hasFnAttr(Attribute::StrictFP); } |
1743 | |
1744 | /// Return true if the call should not be inlined. |
1745 | bool isNoInline() const { return hasFnAttr(Attribute::NoInline); } |
1746 | void setIsNoInline() { |
1747 | addAttribute(AttributeList::FunctionIndex, Attribute::NoInline); |
1748 | } |
1749 | /// Determine if the call does not access memory. |
1750 | bool doesNotAccessMemory() const { return hasFnAttr(Attribute::ReadNone); } |
1751 | void setDoesNotAccessMemory() { |
1752 | addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone); |
1753 | } |
1754 | |
1755 | /// Determine if the call does not access or only reads memory. |
1756 | bool onlyReadsMemory() const { |
1757 | return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly); |
1758 | } |
1759 | |
1760 | /// Returns true if this function is guaranteed to return. |
1761 | bool willReturn() const { return hasFnAttr(Attribute::WillReturn); } |
1762 | |
1763 | void setOnlyReadsMemory() { |
1764 | addAttribute(AttributeList::FunctionIndex, Attribute::ReadOnly); |
1765 | } |
1766 | |
1767 | /// Determine if the call does not access or only writes memory. |
1768 | bool doesNotReadMemory() const { |
1769 | return doesNotAccessMemory() || hasFnAttr(Attribute::WriteOnly); |
1770 | } |
1771 | void setDoesNotReadMemory() { |
1772 | addAttribute(AttributeList::FunctionIndex, Attribute::WriteOnly); |
1773 | } |
1774 | |
1775 | /// Determine if the call can access memmory only using pointers based |
1776 | /// on its arguments. |
1777 | bool onlyAccessesArgMemory() const { |
1778 | return hasFnAttr(Attribute::ArgMemOnly); |
1779 | } |
1780 | void setOnlyAccessesArgMemory() { |
1781 | addAttribute(AttributeList::FunctionIndex, Attribute::ArgMemOnly); |
1782 | } |
1783 | |
1784 | /// Determine if the function may only access memory that is |
1785 | /// inaccessible from the IR. |
1786 | bool onlyAccessesInaccessibleMemory() const { |
1787 | return hasFnAttr(Attribute::InaccessibleMemOnly); |
1788 | } |
1789 | void setOnlyAccessesInaccessibleMemory() { |
1790 | addAttribute(AttributeList::FunctionIndex, Attribute::InaccessibleMemOnly); |
1791 | } |
1792 | |
1793 | /// Determine if the function may only access memory that is |
1794 | /// either inaccessible from the IR or pointed to by its arguments. |
1795 | bool onlyAccessesInaccessibleMemOrArgMem() const { |
1796 | return hasFnAttr(Attribute::InaccessibleMemOrArgMemOnly); |
1797 | } |
1798 | void setOnlyAccessesInaccessibleMemOrArgMem() { |
1799 | addAttribute(AttributeList::FunctionIndex, |
1800 | Attribute::InaccessibleMemOrArgMemOnly); |
1801 | } |
1802 | /// Determine if the call cannot return. |
1803 | bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); } |
1804 | void setDoesNotReturn() { |
1805 | addAttribute(AttributeList::FunctionIndex, Attribute::NoReturn); |
1806 | } |
1807 | |
1808 | /// Determine if the call should not perform indirect branch tracking. |
1809 | bool doesNoCfCheck() const { return hasFnAttr(Attribute::NoCfCheck); } |
1810 | |
1811 | /// Determine if the call cannot unwind. |
1812 | bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); } |
1813 | void setDoesNotThrow() { |
1814 | addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind); |
1815 | } |
1816 | |
1817 | /// Determine if the invoke cannot be duplicated. |
1818 | bool cannotDuplicate() const { return hasFnAttr(Attribute::NoDuplicate); } |
1819 | void setCannotDuplicate() { |
1820 | addAttribute(AttributeList::FunctionIndex, Attribute::NoDuplicate); |
1821 | } |
1822 | |
1823 | /// Determine if the call cannot be tail merged. |
1824 | bool cannotMerge() const { return hasFnAttr(Attribute::NoMerge); } |
1825 | void setCannotMerge() { |
1826 | addAttribute(AttributeList::FunctionIndex, Attribute::NoMerge); |
1827 | } |
1828 | |
1829 | /// Determine if the invoke is convergent |
1830 | bool isConvergent() const { return hasFnAttr(Attribute::Convergent); } |
1831 | void setConvergent() { |
1832 | addAttribute(AttributeList::FunctionIndex, Attribute::Convergent); |
1833 | } |
1834 | void setNotConvergent() { |
1835 | removeAttribute(AttributeList::FunctionIndex, Attribute::Convergent); |
1836 | } |
1837 | |
1838 | /// Determine if the call returns a structure through first |
1839 | /// pointer argument. |
1840 | bool hasStructRetAttr() const { |
1841 | if (getNumArgOperands() == 0) |
1842 | return false; |
1843 | |
1844 | // Be friendly and also check the callee. |
1845 | return paramHasAttr(0, Attribute::StructRet); |
1846 | } |
1847 | |
1848 | /// Determine if any call argument is an aggregate passed by value. |
1849 | bool hasByValArgument() const { |
1850 | return Attrs.hasAttrSomewhere(Attribute::ByVal); |
1851 | } |
1852 | |
1853 | ///@{ |
1854 | // End of attribute API. |
1855 | |
1856 | /// \name Operand Bundle API |
1857 | /// |
1858 | /// This group of methods provides the API to access and manipulate operand |
1859 | /// bundles on this call. |
1860 | /// @{ |
1861 | |
1862 | /// Return the number of operand bundles associated with this User. |
1863 | unsigned getNumOperandBundles() const { |
1864 | return std::distance(bundle_op_info_begin(), bundle_op_info_end()); |
1865 | } |
1866 | |
1867 | /// Return true if this User has any operand bundles. |
1868 | bool hasOperandBundles() const { return getNumOperandBundles() != 0; } |
1869 | |
1870 | /// Return the index of the first bundle operand in the Use array. |
1871 | unsigned getBundleOperandsStartIndex() const { |
1872 | assert(hasOperandBundles() && "Don't call otherwise!")((hasOperandBundles() && "Don't call otherwise!") ? static_cast <void> (0) : __assert_fail ("hasOperandBundles() && \"Don't call otherwise!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1872, __PRETTY_FUNCTION__)); |
1873 | return bundle_op_info_begin()->Begin; |
1874 | } |
1875 | |
1876 | /// Return the index of the last bundle operand in the Use array. |
1877 | unsigned getBundleOperandsEndIndex() const { |
1878 | assert(hasOperandBundles() && "Don't call otherwise!")((hasOperandBundles() && "Don't call otherwise!") ? static_cast <void> (0) : __assert_fail ("hasOperandBundles() && \"Don't call otherwise!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1878, __PRETTY_FUNCTION__)); |
1879 | return bundle_op_info_end()[-1].End; |
1880 | } |
1881 | |
1882 | /// Return true if the operand at index \p Idx is a bundle operand. |
1883 | bool isBundleOperand(unsigned Idx) const { |
1884 | return hasOperandBundles() && Idx >= getBundleOperandsStartIndex() && |
1885 | Idx < getBundleOperandsEndIndex(); |
1886 | } |
1887 | |
1888 | /// Returns true if the use is a bundle operand. |
1889 | bool isBundleOperand(const Use *U) const { |
1890 | assert(this == U->getUser() &&((this == U->getUser() && "Only valid to query with a use of this instruction!" ) ? static_cast<void> (0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1891, __PRETTY_FUNCTION__)) |
1891 | "Only valid to query with a use of this instruction!")((this == U->getUser() && "Only valid to query with a use of this instruction!" ) ? static_cast<void> (0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1891, __PRETTY_FUNCTION__)); |
1892 | return hasOperandBundles() && isBundleOperand(U - op_begin()); |
1893 | } |
1894 | bool isBundleOperand(Value::const_user_iterator UI) const { |
1895 | return isBundleOperand(&UI.getUse()); |
1896 | } |
1897 | |
1898 | /// Return the total number operands (not operand bundles) used by |
1899 | /// every operand bundle in this OperandBundleUser. |
1900 | unsigned getNumTotalBundleOperands() const { |
1901 | if (!hasOperandBundles()) |
1902 | return 0; |
1903 | |
1904 | unsigned Begin = getBundleOperandsStartIndex(); |
1905 | unsigned End = getBundleOperandsEndIndex(); |
1906 | |
1907 | assert(Begin <= End && "Should be!")((Begin <= End && "Should be!") ? static_cast<void > (0) : __assert_fail ("Begin <= End && \"Should be!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1907, __PRETTY_FUNCTION__)); |
1908 | return End - Begin; |
1909 | } |
1910 | |
1911 | /// Return the operand bundle at a specific index. |
1912 | OperandBundleUse getOperandBundleAt(unsigned Index) const { |
1913 | assert(Index < getNumOperandBundles() && "Index out of bounds!")((Index < getNumOperandBundles() && "Index out of bounds!" ) ? static_cast<void> (0) : __assert_fail ("Index < getNumOperandBundles() && \"Index out of bounds!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1913, __PRETTY_FUNCTION__)); |
1914 | return operandBundleFromBundleOpInfo(*(bundle_op_info_begin() + Index)); |
1915 | } |
1916 | |
1917 | /// Return the number of operand bundles with the tag Name attached to |
1918 | /// this instruction. |
1919 | unsigned countOperandBundlesOfType(StringRef Name) const { |
1920 | unsigned Count = 0; |
1921 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) |
1922 | if (getOperandBundleAt(i).getTagName() == Name) |
1923 | Count++; |
1924 | |
1925 | return Count; |
1926 | } |
1927 | |
1928 | /// Return the number of operand bundles with the tag ID attached to |
1929 | /// this instruction. |
1930 | unsigned countOperandBundlesOfType(uint32_t ID) const { |
1931 | unsigned Count = 0; |
1932 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) |
1933 | if (getOperandBundleAt(i).getTagID() == ID) |
1934 | Count++; |
1935 | |
1936 | return Count; |
1937 | } |
1938 | |
1939 | /// Return an operand bundle by name, if present. |
1940 | /// |
1941 | /// It is an error to call this for operand bundle types that may have |
1942 | /// multiple instances of them on the same instruction. |
1943 | Optional<OperandBundleUse> getOperandBundle(StringRef Name) const { |
1944 | assert(countOperandBundlesOfType(Name) < 2 && "Precondition violated!")((countOperandBundlesOfType(Name) < 2 && "Precondition violated!" ) ? static_cast<void> (0) : __assert_fail ("countOperandBundlesOfType(Name) < 2 && \"Precondition violated!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1944, __PRETTY_FUNCTION__)); |
1945 | |
1946 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) { |
1947 | OperandBundleUse U = getOperandBundleAt(i); |
1948 | if (U.getTagName() == Name) |
1949 | return U; |
1950 | } |
1951 | |
1952 | return None; |
1953 | } |
1954 | |
1955 | /// Return an operand bundle by tag ID, if present. |
1956 | /// |
1957 | /// It is an error to call this for operand bundle types that may have |
1958 | /// multiple instances of them on the same instruction. |
1959 | Optional<OperandBundleUse> getOperandBundle(uint32_t ID) const { |
1960 | assert(countOperandBundlesOfType(ID) < 2 && "Precondition violated!")((countOperandBundlesOfType(ID) < 2 && "Precondition violated!" ) ? static_cast<void> (0) : __assert_fail ("countOperandBundlesOfType(ID) < 2 && \"Precondition violated!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 1960, __PRETTY_FUNCTION__)); |
1961 | |
1962 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) { |
1963 | OperandBundleUse U = getOperandBundleAt(i); |
1964 | if (U.getTagID() == ID) |
1965 | return U; |
1966 | } |
1967 | |
1968 | return None; |
1969 | } |
1970 | |
1971 | /// Return the list of operand bundles attached to this instruction as |
1972 | /// a vector of OperandBundleDefs. |
1973 | /// |
1974 | /// This function copies the OperandBundeUse instances associated with this |
1975 | /// OperandBundleUser to a vector of OperandBundleDefs. Note: |
1976 | /// OperandBundeUses and OperandBundleDefs are non-trivially *different* |
1977 | /// representations of operand bundles (see documentation above). |
1978 | void getOperandBundlesAsDefs(SmallVectorImpl<OperandBundleDef> &Defs) const; |
1979 | |
1980 | /// Return the operand bundle for the operand at index OpIdx. |
1981 | /// |
1982 | /// It is an error to call this with an OpIdx that does not correspond to an |
1983 | /// bundle operand. |
1984 | OperandBundleUse getOperandBundleForOperand(unsigned OpIdx) const { |
1985 | return operandBundleFromBundleOpInfo(getBundleOpInfoForOperand(OpIdx)); |
1986 | } |
1987 | |
1988 | /// Return true if this operand bundle user has operand bundles that |
1989 | /// may read from the heap. |
1990 | bool hasReadingOperandBundles() const { |
1991 | // Implementation note: this is a conservative implementation of operand |
1992 | // bundle semantics, where *any* operand bundle forces a callsite to be at |
1993 | // least readonly. |
1994 | return hasOperandBundles(); |
1995 | } |
1996 | |
1997 | /// Return true if this operand bundle user has operand bundles that |
1998 | /// may write to the heap. |
1999 | bool hasClobberingOperandBundles() const { |
2000 | for (auto &BOI : bundle_op_infos()) { |
2001 | if (BOI.Tag->second == LLVMContext::OB_deopt || |
2002 | BOI.Tag->second == LLVMContext::OB_funclet) |
2003 | continue; |
2004 | |
2005 | // This instruction has an operand bundle that is not known to us. |
2006 | // Assume the worst. |
2007 | return true; |
2008 | } |
2009 | |
2010 | return false; |
2011 | } |
2012 | |
2013 | /// Return true if the bundle operand at index \p OpIdx has the |
2014 | /// attribute \p A. |
2015 | bool bundleOperandHasAttr(unsigned OpIdx, Attribute::AttrKind A) const { |
2016 | auto &BOI = getBundleOpInfoForOperand(OpIdx); |
2017 | auto OBU = operandBundleFromBundleOpInfo(BOI); |
2018 | return OBU.operandHasAttr(OpIdx - BOI.Begin, A); |
2019 | } |
2020 | |
2021 | /// Return true if \p Other has the same sequence of operand bundle |
2022 | /// tags with the same number of operands on each one of them as this |
2023 | /// OperandBundleUser. |
2024 | bool hasIdenticalOperandBundleSchema(const CallBase &Other) const { |
2025 | if (getNumOperandBundles() != Other.getNumOperandBundles()) |
2026 | return false; |
2027 | |
2028 | return std::equal(bundle_op_info_begin(), bundle_op_info_end(), |
2029 | Other.bundle_op_info_begin()); |
2030 | } |
2031 | |
2032 | /// Return true if this operand bundle user contains operand bundles |
2033 | /// with tags other than those specified in \p IDs. |
2034 | bool hasOperandBundlesOtherThan(ArrayRef<uint32_t> IDs) const { |
2035 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) { |
2036 | uint32_t ID = getOperandBundleAt(i).getTagID(); |
2037 | if (!is_contained(IDs, ID)) |
2038 | return true; |
2039 | } |
2040 | return false; |
2041 | } |
2042 | |
2043 | /// Is the function attribute S disallowed by some operand bundle on |
2044 | /// this operand bundle user? |
2045 | bool isFnAttrDisallowedByOpBundle(StringRef S) const { |
2046 | // Operand bundles only possibly disallow readnone, readonly and argmemonly |
2047 | // attributes. All String attributes are fine. |
2048 | return false; |
2049 | } |
2050 | |
2051 | /// Is the function attribute A disallowed by some operand bundle on |
2052 | /// this operand bundle user? |
2053 | bool isFnAttrDisallowedByOpBundle(Attribute::AttrKind A) const { |
2054 | switch (A) { |
2055 | default: |
2056 | return false; |
2057 | |
2058 | case Attribute::InaccessibleMemOrArgMemOnly: |
2059 | return hasReadingOperandBundles(); |
2060 | |
2061 | case Attribute::InaccessibleMemOnly: |
2062 | return hasReadingOperandBundles(); |
2063 | |
2064 | case Attribute::ArgMemOnly: |
2065 | return hasReadingOperandBundles(); |
2066 | |
2067 | case Attribute::ReadNone: |
2068 | return hasReadingOperandBundles(); |
2069 | |
2070 | case Attribute::ReadOnly: |
2071 | return hasClobberingOperandBundles(); |
2072 | } |
2073 | |
2074 | llvm_unreachable("switch has a default case!")::llvm::llvm_unreachable_internal("switch has a default case!" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 2074); |
2075 | } |
2076 | |
2077 | /// Used to keep track of an operand bundle. See the main comment on |
2078 | /// OperandBundleUser above. |
2079 | struct BundleOpInfo { |
2080 | /// The operand bundle tag, interned by |
2081 | /// LLVMContextImpl::getOrInsertBundleTag. |
2082 | StringMapEntry<uint32_t> *Tag; |
2083 | |
2084 | /// The index in the Use& vector where operands for this operand |
2085 | /// bundle starts. |
2086 | uint32_t Begin; |
2087 | |
2088 | /// The index in the Use& vector where operands for this operand |
2089 | /// bundle ends. |
2090 | uint32_t End; |
2091 | |
2092 | bool operator==(const BundleOpInfo &Other) const { |
2093 | return Tag == Other.Tag && Begin == Other.Begin && End == Other.End; |
2094 | } |
2095 | }; |
2096 | |
2097 | /// Simple helper function to map a BundleOpInfo to an |
2098 | /// OperandBundleUse. |
2099 | OperandBundleUse |
2100 | operandBundleFromBundleOpInfo(const BundleOpInfo &BOI) const { |
2101 | auto begin = op_begin(); |
2102 | ArrayRef<Use> Inputs(begin + BOI.Begin, begin + BOI.End); |
2103 | return OperandBundleUse(BOI.Tag, Inputs); |
2104 | } |
2105 | |
2106 | using bundle_op_iterator = BundleOpInfo *; |
2107 | using const_bundle_op_iterator = const BundleOpInfo *; |
2108 | |
2109 | /// Return the start of the list of BundleOpInfo instances associated |
2110 | /// with this OperandBundleUser. |
2111 | /// |
2112 | /// OperandBundleUser uses the descriptor area co-allocated with the host User |
2113 | /// to store some meta information about which operands are "normal" operands, |
2114 | /// and which ones belong to some operand bundle. |
2115 | /// |
2116 | /// The layout of an operand bundle user is |
2117 | /// |
2118 | /// +-----------uint32_t End-------------------------------------+ |
2119 | /// | | |
2120 | /// | +--------uint32_t Begin--------------------+ | |
2121 | /// | | | | |
2122 | /// ^ ^ v v |
2123 | /// |------|------|----|----|----|----|----|---------|----|---------|----|----- |
2124 | /// | BOI0 | BOI1 | .. | DU | U0 | U1 | .. | BOI0_U0 | .. | BOI1_U0 | .. | Un |
2125 | /// |------|------|----|----|----|----|----|---------|----|---------|----|----- |
2126 | /// v v ^ ^ |
2127 | /// | | | | |
2128 | /// | +--------uint32_t Begin------------+ | |
2129 | /// | | |
2130 | /// +-----------uint32_t End-----------------------------+ |
2131 | /// |
2132 | /// |
2133 | /// BOI0, BOI1 ... are descriptions of operand bundles in this User's use |
2134 | /// list. These descriptions are installed and managed by this class, and |
2135 | /// they're all instances of OperandBundleUser<T>::BundleOpInfo. |
2136 | /// |
2137 | /// DU is an additional descriptor installed by User's 'operator new' to keep |
2138 | /// track of the 'BOI0 ... BOIN' co-allocation. OperandBundleUser does not |
2139 | /// access or modify DU in any way, it's an implementation detail private to |
2140 | /// User. |
2141 | /// |
2142 | /// The regular Use& vector for the User starts at U0. The operand bundle |
2143 | /// uses are part of the Use& vector, just like normal uses. In the diagram |
2144 | /// above, the operand bundle uses start at BOI0_U0. Each instance of |
2145 | /// BundleOpInfo has information about a contiguous set of uses constituting |
2146 | /// an operand bundle, and the total set of operand bundle uses themselves |
2147 | /// form a contiguous set of uses (i.e. there are no gaps between uses |
2148 | /// corresponding to individual operand bundles). |
2149 | /// |
2150 | /// This class does not know the location of the set of operand bundle uses |
2151 | /// within the use list -- that is decided by the User using this class via |
2152 | /// the BeginIdx argument in populateBundleOperandInfos. |
2153 | /// |
2154 | /// Currently operand bundle users with hung-off operands are not supported. |
2155 | bundle_op_iterator bundle_op_info_begin() { |
2156 | if (!hasDescriptor()) |
2157 | return nullptr; |
2158 | |
2159 | uint8_t *BytesBegin = getDescriptor().begin(); |
2160 | return reinterpret_cast<bundle_op_iterator>(BytesBegin); |
2161 | } |
2162 | |
2163 | /// Return the start of the list of BundleOpInfo instances associated |
2164 | /// with this OperandBundleUser. |
2165 | const_bundle_op_iterator bundle_op_info_begin() const { |
2166 | auto *NonConstThis = const_cast<CallBase *>(this); |
2167 | return NonConstThis->bundle_op_info_begin(); |
2168 | } |
2169 | |
2170 | /// Return the end of the list of BundleOpInfo instances associated |
2171 | /// with this OperandBundleUser. |
2172 | bundle_op_iterator bundle_op_info_end() { |
2173 | if (!hasDescriptor()) |
2174 | return nullptr; |
2175 | |
2176 | uint8_t *BytesEnd = getDescriptor().end(); |
2177 | return reinterpret_cast<bundle_op_iterator>(BytesEnd); |
2178 | } |
2179 | |
2180 | /// Return the end of the list of BundleOpInfo instances associated |
2181 | /// with this OperandBundleUser. |
2182 | const_bundle_op_iterator bundle_op_info_end() const { |
2183 | auto *NonConstThis = const_cast<CallBase *>(this); |
2184 | return NonConstThis->bundle_op_info_end(); |
2185 | } |
2186 | |
2187 | /// Return the range [\p bundle_op_info_begin, \p bundle_op_info_end). |
2188 | iterator_range<bundle_op_iterator> bundle_op_infos() { |
2189 | return make_range(bundle_op_info_begin(), bundle_op_info_end()); |
2190 | } |
2191 | |
2192 | /// Return the range [\p bundle_op_info_begin, \p bundle_op_info_end). |
2193 | iterator_range<const_bundle_op_iterator> bundle_op_infos() const { |
2194 | return make_range(bundle_op_info_begin(), bundle_op_info_end()); |
2195 | } |
2196 | |
2197 | /// Populate the BundleOpInfo instances and the Use& vector from \p |
2198 | /// Bundles. Return the op_iterator pointing to the Use& one past the last |
2199 | /// last bundle operand use. |
2200 | /// |
2201 | /// Each \p OperandBundleDef instance is tracked by a OperandBundleInfo |
2202 | /// instance allocated in this User's descriptor. |
2203 | op_iterator populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles, |
2204 | const unsigned BeginIndex); |
2205 | |
2206 | public: |
2207 | /// Return the BundleOpInfo for the operand at index OpIdx. |
2208 | /// |
2209 | /// It is an error to call this with an OpIdx that does not correspond to an |
2210 | /// bundle operand. |
2211 | BundleOpInfo &getBundleOpInfoForOperand(unsigned OpIdx); |
2212 | const BundleOpInfo &getBundleOpInfoForOperand(unsigned OpIdx) const { |
2213 | return const_cast<CallBase *>(this)->getBundleOpInfoForOperand(OpIdx); |
2214 | } |
2215 | |
2216 | protected: |
2217 | /// Return the total number of values used in \p Bundles. |
2218 | static unsigned CountBundleInputs(ArrayRef<OperandBundleDef> Bundles) { |
2219 | unsigned Total = 0; |
2220 | for (auto &B : Bundles) |
2221 | Total += B.input_size(); |
2222 | return Total; |
2223 | } |
2224 | |
2225 | /// @} |
2226 | // End of operand bundle API. |
2227 | |
2228 | private: |
2229 | bool hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const; |
2230 | bool hasFnAttrOnCalledFunction(StringRef Kind) const; |
2231 | |
2232 | template <typename AttrKind> bool hasFnAttrImpl(AttrKind Kind) const { |
2233 | if (Attrs.hasFnAttribute(Kind)) |
2234 | return true; |
2235 | |
2236 | // Operand bundles override attributes on the called function, but don't |
2237 | // override attributes directly present on the call instruction. |
2238 | if (isFnAttrDisallowedByOpBundle(Kind)) |
2239 | return false; |
2240 | |
2241 | return hasFnAttrOnCalledFunction(Kind); |
2242 | } |
2243 | |
2244 | /// Determine whether the return value has the given attribute. Supports |
2245 | /// Attribute::AttrKind and StringRef as \p AttrKind types. |
2246 | template <typename AttrKind> bool hasRetAttrImpl(AttrKind Kind) const { |
2247 | if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind)) |
2248 | return true; |
2249 | |
2250 | // Look at the callee, if available. |
2251 | if (const Function *F = getCalledFunction()) |
2252 | return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind); |
2253 | return false; |
2254 | } |
2255 | }; |
2256 | |
2257 | template <> |
2258 | struct OperandTraits<CallBase> : public VariadicOperandTraits<CallBase, 1> {}; |
2259 | |
2260 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallBase, Value)CallBase::op_iterator CallBase::op_begin() { return OperandTraits <CallBase>::op_begin(this); } CallBase::const_op_iterator CallBase::op_begin() const { return OperandTraits<CallBase >::op_begin(const_cast<CallBase*>(this)); } CallBase ::op_iterator CallBase::op_end() { return OperandTraits<CallBase >::op_end(this); } CallBase::const_op_iterator CallBase::op_end () const { return OperandTraits<CallBase>::op_end(const_cast <CallBase*>(this)); } Value *CallBase::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<CallBase >::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<CallBase>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 2260, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<CallBase>::op_begin(const_cast<CallBase *>(this))[i_nocapture].get()); } void CallBase::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<CallBase>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<CallBase>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 2260, __PRETTY_FUNCTION__)); OperandTraits<CallBase>:: op_begin(this)[i_nocapture] = Val_nocapture; } unsigned CallBase ::getNumOperands() const { return OperandTraits<CallBase> ::operands(this); } template <int Idx_nocapture> Use & CallBase::Op() { return this->OpFrom<Idx_nocapture>( this); } template <int Idx_nocapture> const Use &CallBase ::Op() const { return this->OpFrom<Idx_nocapture>(this ); } |
2261 | |
2262 | //===----------------------------------------------------------------------===// |
2263 | // FuncletPadInst Class |
2264 | //===----------------------------------------------------------------------===// |
2265 | class FuncletPadInst : public Instruction { |
2266 | private: |
2267 | FuncletPadInst(const FuncletPadInst &CPI); |
2268 | |
2269 | explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
2270 | ArrayRef<Value *> Args, unsigned Values, |
2271 | const Twine &NameStr, Instruction *InsertBefore); |
2272 | explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
2273 | ArrayRef<Value *> Args, unsigned Values, |
2274 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
2275 | |
2276 | void init(Value *ParentPad, ArrayRef<Value *> Args, const Twine &NameStr); |
2277 | |
2278 | protected: |
2279 | // Note: Instruction needs to be a friend here to call cloneImpl. |
2280 | friend class Instruction; |
2281 | friend class CatchPadInst; |
2282 | friend class CleanupPadInst; |
2283 | |
2284 | FuncletPadInst *cloneImpl() const; |
2285 | |
2286 | public: |
2287 | /// Provide fast operand accessors |
2288 | 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; |
2289 | |
2290 | /// getNumArgOperands - Return the number of funcletpad arguments. |
2291 | /// |
2292 | unsigned getNumArgOperands() const { return getNumOperands() - 1; } |
2293 | |
2294 | /// Convenience accessors |
2295 | |
2296 | /// Return the outer EH-pad this funclet is nested within. |
2297 | /// |
2298 | /// Note: This returns the associated CatchSwitchInst if this FuncletPadInst |
2299 | /// is a CatchPadInst. |
2300 | Value *getParentPad() const { return Op<-1>(); } |
2301 | void setParentPad(Value *ParentPad) { |
2302 | assert(ParentPad)((ParentPad) ? static_cast<void> (0) : __assert_fail ("ParentPad" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 2302, __PRETTY_FUNCTION__)); |
2303 | Op<-1>() = ParentPad; |
2304 | } |
2305 | |
2306 | /// getArgOperand/setArgOperand - Return/set the i-th funcletpad argument. |
2307 | /// |
2308 | Value *getArgOperand(unsigned i) const { return getOperand(i); } |
2309 | void setArgOperand(unsigned i, Value *v) { setOperand(i, v); } |
2310 | |
2311 | /// arg_operands - iteration adapter for range-for loops. |
2312 | op_range arg_operands() { return op_range(op_begin(), op_end() - 1); } |
2313 | |
2314 | /// arg_operands - iteration adapter for range-for loops. |
2315 | const_op_range arg_operands() const { |
2316 | return const_op_range(op_begin(), op_end() - 1); |
2317 | } |
2318 | |
2319 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
2320 | static bool classof(const Instruction *I) { return I->isFuncletPad(); } |
2321 | static bool classof(const Value *V) { |
2322 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
2323 | } |
2324 | }; |
2325 | |
2326 | template <> |
2327 | struct OperandTraits<FuncletPadInst> |
2328 | : public VariadicOperandTraits<FuncletPadInst, /*MINARITY=*/1> {}; |
2329 | |
2330 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(FuncletPadInst, Value)FuncletPadInst::op_iterator FuncletPadInst::op_begin() { return OperandTraits<FuncletPadInst>::op_begin(this); } FuncletPadInst ::const_op_iterator FuncletPadInst::op_begin() const { return OperandTraits<FuncletPadInst>::op_begin(const_cast< FuncletPadInst*>(this)); } FuncletPadInst::op_iterator FuncletPadInst ::op_end() { return OperandTraits<FuncletPadInst>::op_end (this); } FuncletPadInst::const_op_iterator FuncletPadInst::op_end () const { return OperandTraits<FuncletPadInst>::op_end (const_cast<FuncletPadInst*>(this)); } Value *FuncletPadInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<FuncletPadInst>::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<FuncletPadInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 2330, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<FuncletPadInst>::op_begin(const_cast< FuncletPadInst*>(this))[i_nocapture].get()); } void FuncletPadInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<FuncletPadInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<FuncletPadInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/InstrTypes.h" , 2330, __PRETTY_FUNCTION__)); OperandTraits<FuncletPadInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned FuncletPadInst::getNumOperands() const { return OperandTraits <FuncletPadInst>::operands(this); } template <int Idx_nocapture > Use &FuncletPadInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &FuncletPadInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } |
2331 | |
2332 | } // end namespace llvm |
2333 | |
2334 | #endif // LLVM_IR_INSTRTYPES_H |
1 | //===- PatternMatch.h - Match on the LLVM IR --------------------*- 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 provides a simple and efficient mechanism for performing general |
10 | // tree-based pattern matches on the LLVM IR. The power of these routines is |
11 | // that it allows you to write concise patterns that are expressive and easy to |
12 | // understand. The other major advantage of this is that it allows you to |
13 | // trivially capture/bind elements in the pattern to variables. For example, |
14 | // you can do something like this: |
15 | // |
16 | // Value *Exp = ... |
17 | // Value *X, *Y; ConstantInt *C1, *C2; // (X & C1) | (Y & C2) |
18 | // if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)), |
19 | // m_And(m_Value(Y), m_ConstantInt(C2))))) { |
20 | // ... Pattern is matched and variables are bound ... |
21 | // } |
22 | // |
23 | // This is primarily useful to things like the instruction combiner, but can |
24 | // also be useful for static analysis tools or code generators. |
25 | // |
26 | //===----------------------------------------------------------------------===// |
27 | |
28 | #ifndef LLVM_IR_PATTERNMATCH_H |
29 | #define LLVM_IR_PATTERNMATCH_H |
30 | |
31 | #include "llvm/ADT/APFloat.h" |
32 | #include "llvm/ADT/APInt.h" |
33 | #include "llvm/IR/Constant.h" |
34 | #include "llvm/IR/Constants.h" |
35 | #include "llvm/IR/DataLayout.h" |
36 | #include "llvm/IR/InstrTypes.h" |
37 | #include "llvm/IR/Instruction.h" |
38 | #include "llvm/IR/Instructions.h" |
39 | #include "llvm/IR/IntrinsicInst.h" |
40 | #include "llvm/IR/Intrinsics.h" |
41 | #include "llvm/IR/Operator.h" |
42 | #include "llvm/IR/Value.h" |
43 | #include "llvm/Support/Casting.h" |
44 | #include <cstdint> |
45 | |
46 | namespace llvm { |
47 | namespace PatternMatch { |
48 | |
49 | template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) { |
50 | return const_cast<Pattern &>(P).match(V); |
51 | } |
52 | |
53 | template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) { |
54 | return const_cast<Pattern &>(P).match(Mask); |
55 | } |
56 | |
57 | template <typename SubPattern_t> struct OneUse_match { |
58 | SubPattern_t SubPattern; |
59 | |
60 | OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {} |
61 | |
62 | template <typename OpTy> bool match(OpTy *V) { |
63 | return V->hasOneUse() && SubPattern.match(V); |
64 | } |
65 | }; |
66 | |
67 | template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) { |
68 | return SubPattern; |
69 | } |
70 | |
71 | template <typename Class> struct class_match { |
72 | template <typename ITy> bool match(ITy *V) { return isa<Class>(V); } |
73 | }; |
74 | |
75 | /// Match an arbitrary value and ignore it. |
76 | inline class_match<Value> m_Value() { return class_match<Value>(); } |
77 | |
78 | /// Match an arbitrary unary operation and ignore it. |
79 | inline class_match<UnaryOperator> m_UnOp() { |
80 | return class_match<UnaryOperator>(); |
81 | } |
82 | |
83 | /// Match an arbitrary binary operation and ignore it. |
84 | inline class_match<BinaryOperator> m_BinOp() { |
85 | return class_match<BinaryOperator>(); |
86 | } |
87 | |
88 | /// Matches any compare instruction and ignore it. |
89 | inline class_match<CmpInst> m_Cmp() { return class_match<CmpInst>(); } |
90 | |
91 | /// Match an arbitrary undef constant. |
92 | inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); } |
93 | |
94 | /// Match an arbitrary poison constant. |
95 | inline class_match<PoisonValue> m_Poison() { return class_match<PoisonValue>(); } |
96 | |
97 | /// Match an arbitrary Constant and ignore it. |
98 | inline class_match<Constant> m_Constant() { return class_match<Constant>(); } |
99 | |
100 | /// Match an arbitrary ConstantInt and ignore it. |
101 | inline class_match<ConstantInt> m_ConstantInt() { |
102 | return class_match<ConstantInt>(); |
103 | } |
104 | |
105 | /// Match an arbitrary ConstantFP and ignore it. |
106 | inline class_match<ConstantFP> m_ConstantFP() { |
107 | return class_match<ConstantFP>(); |
108 | } |
109 | |
110 | /// Match an arbitrary ConstantExpr and ignore it. |
111 | inline class_match<ConstantExpr> m_ConstantExpr() { |
112 | return class_match<ConstantExpr>(); |
113 | } |
114 | |
115 | /// Match an arbitrary basic block value and ignore it. |
116 | inline class_match<BasicBlock> m_BasicBlock() { |
117 | return class_match<BasicBlock>(); |
118 | } |
119 | |
120 | /// Inverting matcher |
121 | template <typename Ty> struct match_unless { |
122 | Ty M; |
123 | |
124 | match_unless(const Ty &Matcher) : M(Matcher) {} |
125 | |
126 | template <typename ITy> bool match(ITy *V) { return !M.match(V); } |
127 | }; |
128 | |
129 | /// Match if the inner matcher does *NOT* match. |
130 | template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) { |
131 | return match_unless<Ty>(M); |
132 | } |
133 | |
134 | /// Matching combinators |
135 | template <typename LTy, typename RTy> struct match_combine_or { |
136 | LTy L; |
137 | RTy R; |
138 | |
139 | match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {} |
140 | |
141 | template <typename ITy> bool match(ITy *V) { |
142 | if (L.match(V)) |
143 | return true; |
144 | if (R.match(V)) |
145 | return true; |
146 | return false; |
147 | } |
148 | }; |
149 | |
150 | template <typename LTy, typename RTy> struct match_combine_and { |
151 | LTy L; |
152 | RTy R; |
153 | |
154 | match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {} |
155 | |
156 | template <typename ITy> bool match(ITy *V) { |
157 | if (L.match(V)) |
158 | if (R.match(V)) |
159 | return true; |
160 | return false; |
161 | } |
162 | }; |
163 | |
164 | /// Combine two pattern matchers matching L || R |
165 | template <typename LTy, typename RTy> |
166 | inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) { |
167 | return match_combine_or<LTy, RTy>(L, R); |
168 | } |
169 | |
170 | /// Combine two pattern matchers matching L && R |
171 | template <typename LTy, typename RTy> |
172 | inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) { |
173 | return match_combine_and<LTy, RTy>(L, R); |
174 | } |
175 | |
176 | struct apint_match { |
177 | const APInt *&Res; |
178 | bool AllowUndef; |
179 | |
180 | apint_match(const APInt *&Res, bool AllowUndef) |
181 | : Res(Res), AllowUndef(AllowUndef) {} |
182 | |
183 | template <typename ITy> bool match(ITy *V) { |
184 | if (auto *CI = dyn_cast<ConstantInt>(V)) { |
185 | Res = &CI->getValue(); |
186 | return true; |
187 | } |
188 | if (V->getType()->isVectorTy()) |
189 | if (const auto *C = dyn_cast<Constant>(V)) |
190 | if (auto *CI = dyn_cast_or_null<ConstantInt>( |
191 | C->getSplatValue(AllowUndef))) { |
192 | Res = &CI->getValue(); |
193 | return true; |
194 | } |
195 | return false; |
196 | } |
197 | }; |
198 | // Either constexpr if or renaming ConstantFP::getValueAPF to |
199 | // ConstantFP::getValue is needed to do it via single template |
200 | // function for both apint/apfloat. |
201 | struct apfloat_match { |
202 | const APFloat *&Res; |
203 | bool AllowUndef; |
204 | |
205 | apfloat_match(const APFloat *&Res, bool AllowUndef) |
206 | : Res(Res), AllowUndef(AllowUndef) {} |
207 | |
208 | template <typename ITy> bool match(ITy *V) { |
209 | if (auto *CI = dyn_cast<ConstantFP>(V)) { |
210 | Res = &CI->getValueAPF(); |
211 | return true; |
212 | } |
213 | if (V->getType()->isVectorTy()) |
214 | if (const auto *C = dyn_cast<Constant>(V)) |
215 | if (auto *CI = dyn_cast_or_null<ConstantFP>( |
216 | C->getSplatValue(AllowUndef))) { |
217 | Res = &CI->getValueAPF(); |
218 | return true; |
219 | } |
220 | return false; |
221 | } |
222 | }; |
223 | |
224 | /// Match a ConstantInt or splatted ConstantVector, binding the |
225 | /// specified pointer to the contained APInt. |
226 | inline apint_match m_APInt(const APInt *&Res) { |
227 | // Forbid undefs by default to maintain previous behavior. |
228 | return apint_match(Res, /* AllowUndef */ false); |
229 | } |
230 | |
231 | /// Match APInt while allowing undefs in splat vector constants. |
232 | inline apint_match m_APIntAllowUndef(const APInt *&Res) { |
233 | return apint_match(Res, /* AllowUndef */ true); |
234 | } |
235 | |
236 | /// Match APInt while forbidding undefs in splat vector constants. |
237 | inline apint_match m_APIntForbidUndef(const APInt *&Res) { |
238 | return apint_match(Res, /* AllowUndef */ false); |
239 | } |
240 | |
241 | /// Match a ConstantFP or splatted ConstantVector, binding the |
242 | /// specified pointer to the contained APFloat. |
243 | inline apfloat_match m_APFloat(const APFloat *&Res) { |
244 | // Forbid undefs by default to maintain previous behavior. |
245 | return apfloat_match(Res, /* AllowUndef */ false); |
246 | } |
247 | |
248 | /// Match APFloat while allowing undefs in splat vector constants. |
249 | inline apfloat_match m_APFloatAllowUndef(const APFloat *&Res) { |
250 | return apfloat_match(Res, /* AllowUndef */ true); |
251 | } |
252 | |
253 | /// Match APFloat while forbidding undefs in splat vector constants. |
254 | inline apfloat_match m_APFloatForbidUndef(const APFloat *&Res) { |
255 | return apfloat_match(Res, /* AllowUndef */ false); |
256 | } |
257 | |
258 | template <int64_t Val> struct constantint_match { |
259 | template <typename ITy> bool match(ITy *V) { |
260 | if (const auto *CI = dyn_cast<ConstantInt>(V)) { |
261 | const APInt &CIV = CI->getValue(); |
262 | if (Val >= 0) |
263 | return CIV == static_cast<uint64_t>(Val); |
264 | // If Val is negative, and CI is shorter than it, truncate to the right |
265 | // number of bits. If it is larger, then we have to sign extend. Just |
266 | // compare their negated values. |
267 | return -CIV == -Val; |
268 | } |
269 | return false; |
270 | } |
271 | }; |
272 | |
273 | /// Match a ConstantInt with a specific value. |
274 | template <int64_t Val> inline constantint_match<Val> m_ConstantInt() { |
275 | return constantint_match<Val>(); |
276 | } |
277 | |
278 | /// This helper class is used to match constant scalars, vector splats, |
279 | /// and fixed width vectors that satisfy a specified predicate. |
280 | /// For fixed width vector constants, undefined elements are ignored. |
281 | template <typename Predicate, typename ConstantVal> |
282 | struct cstval_pred_ty : public Predicate { |
283 | template <typename ITy> bool match(ITy *V) { |
284 | if (const auto *CV = dyn_cast<ConstantVal>(V)) |
285 | return this->isValue(CV->getValue()); |
286 | if (const auto *VTy = dyn_cast<VectorType>(V->getType())) { |
287 | if (const auto *C = dyn_cast<Constant>(V)) { |
288 | if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue())) |
289 | return this->isValue(CV->getValue()); |
290 | |
291 | // Number of elements of a scalable vector unknown at compile time |
292 | auto *FVTy = dyn_cast<FixedVectorType>(VTy); |
293 | if (!FVTy) |
294 | return false; |
295 | |
296 | // Non-splat vector constant: check each element for a match. |
297 | unsigned NumElts = FVTy->getNumElements(); |
298 | assert(NumElts != 0 && "Constant vector with no elements?")((NumElts != 0 && "Constant vector with no elements?" ) ? static_cast<void> (0) : __assert_fail ("NumElts != 0 && \"Constant vector with no elements?\"" , "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/PatternMatch.h" , 298, __PRETTY_FUNCTION__)); |
299 | bool HasNonUndefElements = false; |
300 | for (unsigned i = 0; i != NumElts; ++i) { |
301 | Constant *Elt = C->getAggregateElement(i); |
302 | if (!Elt) |
303 | return false; |
304 | if (isa<UndefValue>(Elt)) |
305 | continue; |
306 | auto *CV = dyn_cast<ConstantVal>(Elt); |
307 | if (!CV || !this->isValue(CV->getValue())) |
308 | return false; |
309 | HasNonUndefElements = true; |
310 | } |
311 | return HasNonUndefElements; |
312 | } |
313 | } |
314 | return false; |
315 | } |
316 | }; |
317 | |
318 | /// specialization of cstval_pred_ty for ConstantInt |
319 | template <typename Predicate> |
320 | using cst_pred_ty = cstval_pred_ty<Predicate, ConstantInt>; |
321 | |
322 | /// specialization of cstval_pred_ty for ConstantFP |
323 | template <typename Predicate> |
324 | using cstfp_pred_ty = cstval_pred_ty<Predicate, ConstantFP>; |
325 | |
326 | /// This helper class is used to match scalar and vector constants that |
327 | /// satisfy a specified predicate, and bind them to an APInt. |
328 | template <typename Predicate> struct api_pred_ty : public Predicate { |
329 | const APInt *&Res; |
330 | |
331 | api_pred_ty(const APInt *&R) : Res(R) {} |
332 | |
333 | template <typename ITy> bool match(ITy *V) { |
334 | if (const auto *CI = dyn_cast<ConstantInt>(V)) |
335 | if (this->isValue(CI->getValue())) { |
336 | Res = &CI->getValue(); |
337 | return true; |
338 | } |
339 | if (V->getType()->isVectorTy()) |
340 | if (const auto *C = dyn_cast<Constant>(V)) |
341 | if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) |
342 | if (this->isValue(CI->getValue())) { |
343 | Res = &CI->getValue(); |
344 | return true; |
345 | } |
346 | |
347 | return false; |
348 | } |
349 | }; |
350 | |
351 | /// This helper class is used to match scalar and vector constants that |
352 | /// satisfy a specified predicate, and bind them to an APFloat. |
353 | /// Undefs are allowed in splat vector constants. |
354 | template <typename Predicate> struct apf_pred_ty : public Predicate { |
355 | const APFloat *&Res; |
356 | |
357 | apf_pred_ty(const APFloat *&R) : Res(R) {} |
358 | |
359 | template <typename ITy> bool match(ITy *V) { |
360 | if (const auto *CI = dyn_cast<ConstantFP>(V)) |
361 | if (this->isValue(CI->getValue())) { |
362 | Res = &CI->getValue(); |
363 | return true; |
364 | } |
365 | if (V->getType()->isVectorTy()) |
366 | if (const auto *C = dyn_cast<Constant>(V)) |
367 | if (auto *CI = dyn_cast_or_null<ConstantFP>( |
368 | C->getSplatValue(/* AllowUndef */ true))) |
369 | if (this->isValue(CI->getValue())) { |
370 | Res = &CI->getValue(); |
371 | return true; |
372 | } |
373 | |
374 | return false; |
375 | } |
376 | }; |
377 | |
378 | /////////////////////////////////////////////////////////////////////////////// |
379 | // |
380 | // Encapsulate constant value queries for use in templated predicate matchers. |
381 | // This allows checking if constants match using compound predicates and works |
382 | // with vector constants, possibly with relaxed constraints. For example, ignore |
383 | // undef values. |
384 | // |
385 | /////////////////////////////////////////////////////////////////////////////// |
386 | |
387 | struct is_any_apint { |
388 | bool isValue(const APInt &C) { return true; } |
389 | }; |
390 | /// Match an integer or vector with any integral constant. |
391 | /// For vectors, this includes constants with undefined elements. |
392 | inline cst_pred_ty<is_any_apint> m_AnyIntegralConstant() { |
393 | return cst_pred_ty<is_any_apint>(); |
394 | } |
395 | |
396 | struct is_all_ones { |
397 | bool isValue(const APInt &C) { return C.isAllOnesValue(); } |
398 | }; |
399 | /// Match an integer or vector with all bits set. |
400 | /// For vectors, this includes constants with undefined elements. |
401 | inline cst_pred_ty<is_all_ones> m_AllOnes() { |
402 | return cst_pred_ty<is_all_ones>(); |
403 | } |
404 | |
405 | struct is_maxsignedvalue { |
406 | bool isValue(const APInt &C) { return C.isMaxSignedValue(); } |
407 | }; |
408 | /// Match an integer or vector with values having all bits except for the high |
409 | /// bit set (0x7f...). |
410 | /// For vectors, this includes constants with undefined elements. |
411 | inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() { |
412 | return cst_pred_ty<is_maxsignedvalue>(); |
413 | } |
414 | inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) { |
415 | return V; |
416 | } |
417 | |
418 | struct is_negative { |
419 | bool isValue(const APInt &C) { return C.isNegative(); } |
420 | }; |
421 | /// Match an integer or vector of negative values. |
422 | /// For vectors, this includes constants with undefined elements. |
423 | inline cst_pred_ty<is_negative> m_Negative() { |
424 | return cst_pred_ty<is_negative>(); |
425 | } |
426 | inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { |
427 | return V; |
428 | } |
429 | |
430 | struct is_nonnegative { |
431 | bool isValue(const APInt &C) { return C.isNonNegative(); } |
432 | }; |
433 | /// Match an integer or vector of non-negative values. |
434 | /// For vectors, this includes constants with undefined elements. |
435 | inline cst_pred_ty<is_nonnegative> m_NonNegative() { |
436 | return cst_pred_ty<is_nonnegative>(); |
437 | } |
438 | inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { |
439 | return V; |
440 | } |
441 | |
442 | struct is_strictlypositive { |
443 | bool isValue(const APInt &C) { return C.isStrictlyPositive(); } |
444 | }; |
445 | /// Match an integer or vector of strictly positive values. |
446 | /// For vectors, this includes constants with undefined elements. |
447 | inline cst_pred_ty<is_strictlypositive> m_StrictlyPositive() { |
448 | return cst_pred_ty<is_strictlypositive>(); |
449 | } |
450 | inline api_pred_ty<is_strictlypositive> m_StrictlyPositive(const APInt *&V) { |
451 | return V; |
452 | } |
453 | |
454 | struct is_nonpositive { |
455 | bool isValue(const APInt &C) { return C.isNonPositive(); } |
456 | }; |
457 | /// Match an integer or vector of non-positive values. |
458 | /// For vectors, this includes constants with undefined elements. |
459 | inline cst_pred_ty<is_nonpositive> m_NonPositive() { |
460 | return cst_pred_ty<is_nonpositive>(); |
461 | } |
462 | inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; } |
463 | |
464 | struct is_one { |
465 | bool isValue(const APInt &C) { return C.isOneValue(); } |
466 | }; |
467 | /// Match an integer 1 or a vector with all elements equal to 1. |
468 | /// For vectors, this includes constants with undefined elements. |
469 | inline cst_pred_ty<is_one> m_One() { |
470 | return cst_pred_ty<is_one>(); |
471 | } |
472 | |
473 | struct is_zero_int { |
474 | bool isValue(const APInt &C) { return C.isNullValue(); } |
475 | }; |
476 | /// Match an integer 0 or a vector with all elements equal to 0. |
477 | /// For vectors, this includes constants with undefined elements. |
478 | inline cst_pred_ty<is_zero_int> m_ZeroInt() { |
479 | return cst_pred_ty<is_zero_int>(); |
480 | } |
481 | |
482 | struct is_zero { |
483 | template <typename ITy> bool match(ITy *V) { |
484 | auto *C = dyn_cast<Constant>(V); |
485 | // FIXME: this should be able to do something for scalable vectors |
486 | return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C)); |
487 | } |
488 | }; |
489 | /// Match any null constant or a vector with all elements equal to 0. |
490 | /// For vectors, this includes constants with undefined elements. |
491 | inline is_zero m_Zero() { |
492 | return is_zero(); |
493 | } |
494 | |
495 | struct is_power2 { |
496 | bool isValue(const APInt &C) { return C.isPowerOf2(); } |
497 | }; |
498 | /// Match an integer or vector power-of-2. |
499 | /// For vectors, this includes constants with undefined elements. |
500 | inline cst_pred_ty<is_power2> m_Power2() { |
501 | return cst_pred_ty<is_power2>(); |
502 | } |
503 | inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { |
504 | return V; |
505 | } |
506 | |
507 | struct is_negated_power2 { |
508 | bool isValue(const APInt &C) { return (-C).isPowerOf2(); } |
509 | }; |
510 | /// Match a integer or vector negated power-of-2. |
511 | /// For vectors, this includes constants with undefined elements. |
512 | inline cst_pred_ty<is_negated_power2> m_NegatedPower2() { |
513 | return cst_pred_ty<is_negated_power2>(); |
514 | } |
515 | inline api_pred_ty<is_negated_power2> m_NegatedPower2(const APInt *&V) { |
516 | return V; |
517 | } |
518 | |
519 | struct is_power2_or_zero { |
520 | bool isValue(const APInt &C) { return !C || C.isPowerOf2(); } |
521 | }; |
522 | /// Match an integer or vector of 0 or power-of-2 values. |
523 | /// For vectors, this includes constants with undefined elements. |
524 | inline cst_pred_ty<is_power2_or_zero> m_Power2OrZero() { |
525 | return cst_pred_ty<is_power2_or_zero>(); |
526 | } |
527 | inline api_pred_ty<is_power2_or_zero> m_Power2OrZero(const APInt *&V) { |
528 | return V; |
529 | } |
530 | |
531 | struct is_sign_mask { |
532 | bool isValue(const APInt &C) { return C.isSignMask(); } |
533 | }; |
534 | /// Match an integer or vector with only the sign bit(s) set. |
535 | /// For vectors, this includes constants with undefined elements. |
536 | inline cst_pred_ty<is_sign_mask> m_SignMask() { |
537 | return cst_pred_ty<is_sign_mask>(); |
538 | } |
539 | |
540 | struct is_lowbit_mask { |
541 | bool isValue(const APInt &C) { return C.isMask(); } |
542 | }; |
543 | /// Match an integer or vector with only the low bit(s) set. |
544 | /// For vectors, this includes constants with undefined elements. |
545 | inline cst_pred_ty<is_lowbit_mask> m_LowBitMask() { |
546 | return cst_pred_ty<is_lowbit_mask>(); |
547 | } |
548 | |
549 | struct icmp_pred_with_threshold { |
550 | ICmpInst::Predicate Pred; |
551 | const APInt *Thr; |
552 | bool isValue(const APInt &C) { |
553 | switch (Pred) { |
554 | case ICmpInst::Predicate::ICMP_EQ: |
555 | return C.eq(*Thr); |
556 | case ICmpInst::Predicate::ICMP_NE: |
557 | return C.ne(*Thr); |
558 | case ICmpInst::Predicate::ICMP_UGT: |
559 | return C.ugt(*Thr); |
560 | case ICmpInst::Predicate::ICMP_UGE: |
561 | return C.uge(*Thr); |
562 | case ICmpInst::Predicate::ICMP_ULT: |
563 | return C.ult(*Thr); |
564 | case ICmpInst::Predicate::ICMP_ULE: |
565 | return C.ule(*Thr); |
566 | case ICmpInst::Predicate::ICMP_SGT: |
567 | return C.sgt(*Thr); |
568 | case ICmpInst::Predicate::ICMP_SGE: |
569 | return C.sge(*Thr); |
570 | case ICmpInst::Predicate::ICMP_SLT: |
571 | return C.slt(*Thr); |
572 | case ICmpInst::Predicate::ICMP_SLE: |
573 | return C.sle(*Thr); |
574 | default: |
575 | llvm_unreachable("Unhandled ICmp predicate")::llvm::llvm_unreachable_internal("Unhandled ICmp predicate", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include/llvm/IR/PatternMatch.h" , 575); |
576 | } |
577 | } |
578 | }; |
579 | /// Match an integer or vector with every element comparing 'pred' (eg/ne/...) |
580 | /// to Threshold. For vectors, this includes constants with undefined elements. |
581 | inline cst_pred_ty<icmp_pred_with_threshold> |
582 | m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold) { |
583 | cst_pred_ty<icmp_pred_with_threshold> P; |
584 | P.Pred = Predicate; |
585 | P.Thr = &Threshold; |
586 | return P; |
587 | } |
588 | |
589 | struct is_nan { |
590 | bool isValue(const APFloat &C) { return C.isNaN(); } |
591 | }; |
592 | /// Match an arbitrary NaN constant. This includes quiet and signalling nans. |
593 | /// For vectors, this includes constants with undefined elements. |
594 | inline cstfp_pred_ty<is_nan> m_NaN() { |
595 | return cstfp_pred_ty<is_nan>(); |
596 | } |
597 | |
598 | struct is_nonnan { |
599 | bool isValue(const APFloat &C) { return !C.isNaN(); } |
600 | }; |
601 | /// Match a non-NaN FP constant. |
602 | /// For vectors, this includes constants with undefined elements. |
603 | inline cstfp_pred_ty<is_nonnan> m_NonNaN() { |
604 | return cstfp_pred_ty<is_nonnan>(); |
605 | } |
606 | |
607 | struct is_inf { |
608 | bool isValue(const APFloat &C) { return C.isInfinity(); } |
609 | }; |
610 | /// Match a positive or negative infinity FP constant. |
611 | /// For vectors, this includes constants with undefined elements. |
612 | inline cstfp_pred_ty<is_inf> m_Inf() { |
613 | return cstfp_pred_ty<is_inf>(); |
614 | } |
615 | |
616 | struct is_noninf { |
617 | bool isValue(const APFloat &C) { return !C.isInfinity(); } |
618 | }; |
619 | /// Match a non-infinity FP constant, i.e. finite or NaN. |
620 | /// For vectors, this includes constants with undefined elements. |
621 | inline cstfp_pred_ty<is_noninf> m_NonInf() { |
622 | return cstfp_pred_ty<is_noninf>(); |
623 | } |
624 | |
625 | struct is_finite { |
626 | bool isValue(const APFloat &C) { return C.isFinite(); } |
627 | }; |
628 | /// Match a finite FP constant, i.e. not infinity or NaN. |
629 | /// For vectors, this includes constants with undefined elements. |
630 | inline cstfp_pred_ty<is_finite> m_Finite() { |
631 | return cstfp_pred_ty<is_finite>(); |
632 | } |
633 | inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; } |
634 | |
635 | struct is_finitenonzero { |
636 | bool isValue(const APFloat &C) { return C.isFiniteNonZero(); } |
637 | }; |
638 | /// Match a finite non-zero FP constant. |
639 | /// For vectors, this includes constants with undefined elements. |
640 | inline cstfp_pred_ty<is_finitenonzero> m_FiniteNonZero() { |
641 | return cstfp_pred_ty<is_finitenonzero>(); |
642 | } |
643 | inline apf_pred_ty<is_finitenonzero> m_FiniteNonZero(const APFloat *&V) { |
644 | return V; |
645 | } |
646 | |
647 | struct is_any_zero_fp { |
648 | bool isValue(const APFloat &C) { return C.isZero(); } |
649 | }; |
650 | /// Match a floating-point negative zero or positive zero. |
651 | /// For vectors, this includes constants with undefined elements. |
652 | inline cstfp_pred_ty<is_any_zero_fp> m_AnyZeroFP() { |
653 | return cstfp_pred_ty<is_any_zero_fp>(); |
654 | } |
655 | |
656 | struct is_pos_zero_fp { |
657 | bool isValue(const APFloat &C) { return C.isPosZero(); } |
658 | }; |
659 | /// Match a floating-point positive zero. |
660 | /// For vectors, this includes constants with undefined elements. |
661 | inline cstfp_pred_ty<is_pos_zero_fp> m_PosZeroFP() { |
662 | return cstfp_pred_ty<is_pos_zero_fp>(); |
663 | } |
664 | |
665 | struct is_neg_zero_fp { |
666 | bool isValue(const APFloat &C) { return C.isNegZero(); } |
667 | }; |
668 | /// Match a floating-point negative zero. |
669 | /// For vectors, this includes constants with undefined elements. |
670 | inline cstfp_pred_ty<is_neg_zero_fp> m_NegZeroFP() { |
671 | return cstfp_pred_ty<is_neg_zero_fp>(); |
672 | } |
673 | |
674 | struct is_non_zero_fp { |
675 | bool isValue(const APFloat &C) { return C.isNonZero(); } |
676 | }; |
677 | /// Match a floating-point non-zero. |
678 | /// For vectors, this includes constants with undefined elements. |
679 | inline cstfp_pred_ty<is_non_zero_fp> m_NonZeroFP() { |
680 | return cstfp_pred_ty<is_non_zero_fp>(); |
681 | } |
682 | |
683 | /////////////////////////////////////////////////////////////////////////////// |
684 | |
685 | template <typename Class> struct bind_ty { |
686 | Class *&VR; |
687 | |
688 | bind_ty(Class *&V) : VR(V) {} |
689 | |
690 | template <typename ITy> bool match(ITy *V) { |
691 | if (auto *CV = dyn_cast<Class>(V)) { |
692 | VR = CV; |
693 | return true; |
694 | } |
695 | return false; |
696 | } |
697 | }; |
698 | |
699 | /// Match a value, capturing it if we match. |
700 | inline bind_ty<Value> m_Value(Value *&V) { return V; } |
701 | inline bind_ty<const Value> m_Value(const Value *&V) { return V; } |
702 | |
703 | /// Match an instruction, capturing it if we match. |
704 | inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; } |
705 | /// Match a unary operator, capturing it if we match. |
706 | inline bind_ty<UnaryOperator> m_UnOp(UnaryOperator *&I) { return I; } |
707 | /// Match a binary operator, capturing it if we match. |
708 | inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; } |
709 | /// Match a with overflow intrinsic, capturing it if we match. |
710 | inline bind_ty<WithOverflowInst> m_WithOverflowInst(WithOverflowInst *&I) { return I; } |
711 | |
712 | /// Match a Constant, capturing the value if we match. |
713 | inline bind_ty<Constant> m_Constant(Constant *&C) { return C; } |
714 | |
715 | /// Match a ConstantInt, capturing the value if we match. |
716 | inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; } |
717 | |
718 | /// Match a ConstantFP, capturing the value if we match. |
719 | inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; } |
720 | |
721 | /// Match a ConstantExpr, capturing the value if we match. |
722 | inline bind_ty<ConstantExpr> m_ConstantExpr(ConstantExpr *&C) { return C; } |
723 | |
724 | /// Match a basic block value, capturing it if we match. |
725 | inline bind_ty<BasicBlock> m_BasicBlock(BasicBlock *&V) { return V; } |
726 | inline bind_ty<const BasicBlock> m_BasicBlock(const BasicBlock *&V) { |
727 | return V; |
728 | } |
729 | |
730 | /// Match an arbitrary immediate Constant and ignore it. |
731 | inline match_combine_and<class_match<Constant>, |
732 | match_unless<class_match<ConstantExpr>>> |
733 | m_ImmConstant() { |
734 | return m_CombineAnd(m_Constant(), m_Unless(m_ConstantExpr())); |
735 | } |
736 | |
737 | /// Match an immediate Constant, capturing the value if we match. |
738 | inline match_combine_and<bind_ty<Constant>, |
739 | match_unless<class_match<ConstantExpr>>> |
740 | m_ImmConstant(Constant *&C) { |
741 | return m_CombineAnd(m_Constant(C), m_Unless(m_ConstantExpr())); |
742 | } |
743 | |
744 | /// Match a specified Value*. |
745 | struct specificval_ty { |
746 | const Value *Val; |
747 | |
748 | specificval_ty(const Value *V) : Val(V) {} |
749 | |
75 |