File: | llvm/lib/Transforms/Scalar/LICM.cpp |
Warning: | line 1262, 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 TargetLibraryInfo *TLI, | ||||||||||
166 | const Loop *CurLoop, | ||||||||||
167 | const LoopSafetyInfo *SafetyInfo, | ||||||||||
168 | OptimizationRemarkEmitter *ORE, | ||||||||||
169 | const Instruction *CtxI = nullptr); | ||||||||||
170 | static bool pointerInvalidatedByLoop(MemoryLocation MemLoc, | ||||||||||
171 | AliasSetTracker *CurAST, Loop *CurLoop, | ||||||||||
172 | AAResults *AA); | ||||||||||
173 | static bool pointerInvalidatedByLoopWithMSSA(MemorySSA *MSSA, MemoryUse *MU, | ||||||||||
174 | Loop *CurLoop, Instruction &I, | ||||||||||
175 | SinkAndHoistLICMFlags &Flags); | ||||||||||
176 | static bool pointerInvalidatedByBlockWithMSSA(BasicBlock &BB, MemorySSA &MSSA, | ||||||||||
177 | MemoryUse &MU); | ||||||||||
178 | static Instruction *cloneInstructionInExitBlock( | ||||||||||
179 | Instruction &I, BasicBlock &ExitBlock, PHINode &PN, const LoopInfo *LI, | ||||||||||
180 | const LoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU); | ||||||||||
181 | |||||||||||
182 | static void eraseInstruction(Instruction &I, ICFLoopSafetyInfo &SafetyInfo, | ||||||||||
183 | AliasSetTracker *AST, MemorySSAUpdater *MSSAU); | ||||||||||
184 | |||||||||||
185 | static void moveInstructionBefore(Instruction &I, Instruction &Dest, | ||||||||||
186 | ICFLoopSafetyInfo &SafetyInfo, | ||||||||||
187 | MemorySSAUpdater *MSSAU, ScalarEvolution *SE); | ||||||||||
188 | |||||||||||
189 | static void foreachMemoryAccess(MemorySSA *MSSA, Loop *L, | ||||||||||
190 | function_ref<void(Instruction *)> Fn); | ||||||||||
191 | static SmallVector<SmallSetVector<Value *, 8>, 0> | ||||||||||
192 | collectPromotionCandidates(MemorySSA *MSSA, AliasAnalysis *AA, Loop *L, | ||||||||||
193 | SmallVectorImpl<Instruction *> &MaybePromotable); | ||||||||||
194 | |||||||||||
195 | namespace { | ||||||||||
196 | struct LoopInvariantCodeMotion { | ||||||||||
197 | bool runOnLoop(Loop *L, AAResults *AA, LoopInfo *LI, DominatorTree *DT, | ||||||||||
198 | BlockFrequencyInfo *BFI, TargetLibraryInfo *TLI, | ||||||||||
199 | TargetTransformInfo *TTI, ScalarEvolution *SE, MemorySSA *MSSA, | ||||||||||
200 | OptimizationRemarkEmitter *ORE); | ||||||||||
201 | |||||||||||
202 | LoopInvariantCodeMotion(unsigned LicmMssaOptCap, | ||||||||||
203 | unsigned LicmMssaNoAccForPromotionCap) | ||||||||||
204 | : LicmMssaOptCap(LicmMssaOptCap), | ||||||||||
205 | LicmMssaNoAccForPromotionCap(LicmMssaNoAccForPromotionCap) {} | ||||||||||
206 | |||||||||||
207 | private: | ||||||||||
208 | unsigned LicmMssaOptCap; | ||||||||||
209 | unsigned LicmMssaNoAccForPromotionCap; | ||||||||||
210 | |||||||||||
211 | std::unique_ptr<AliasSetTracker> | ||||||||||
212 | collectAliasInfoForLoop(Loop *L, LoopInfo *LI, AAResults *AA); | ||||||||||
213 | }; | ||||||||||
214 | |||||||||||
215 | struct LegacyLICMPass : public LoopPass { | ||||||||||
216 | static char ID; // Pass identification, replacement for typeid | ||||||||||
217 | LegacyLICMPass( | ||||||||||
218 | unsigned LicmMssaOptCap = SetLicmMssaOptCap, | ||||||||||
219 | unsigned LicmMssaNoAccForPromotionCap = SetLicmMssaNoAccForPromotionCap) | ||||||||||
220 | : LoopPass(ID), LICM(LicmMssaOptCap, LicmMssaNoAccForPromotionCap) { | ||||||||||
221 | initializeLegacyLICMPassPass(*PassRegistry::getPassRegistry()); | ||||||||||
222 | } | ||||||||||
223 | |||||||||||
224 | bool runOnLoop(Loop *L, LPPassManager &LPM) override { | ||||||||||
225 | if (skipLoop(L)) | ||||||||||
226 | return false; | ||||||||||
227 | |||||||||||
228 | 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) | ||||||||||
229 | << 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); | ||||||||||
230 | |||||||||||
231 | auto *SE = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>(); | ||||||||||
232 | MemorySSA *MSSA = EnableMSSALoopDependency | ||||||||||
233 | ? (&getAnalysis<MemorySSAWrapperPass>().getMSSA()) | ||||||||||
234 | : nullptr; | ||||||||||
235 | bool hasProfileData = L->getHeader()->getParent()->hasProfileData(); | ||||||||||
236 | BlockFrequencyInfo *BFI = | ||||||||||
237 | hasProfileData ? &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI() | ||||||||||
238 | : nullptr; | ||||||||||
239 | // For the old PM, we can't use OptimizationRemarkEmitter as an analysis | ||||||||||
240 | // pass. Function analyses need to be preserved across loop transformations | ||||||||||
241 | // but ORE cannot be preserved (see comment before the pass definition). | ||||||||||
242 | OptimizationRemarkEmitter ORE(L->getHeader()->getParent()); | ||||||||||
243 | return LICM.runOnLoop( | ||||||||||
244 | L, &getAnalysis<AAResultsWrapperPass>().getAAResults(), | ||||||||||
245 | &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(), | ||||||||||
246 | &getAnalysis<DominatorTreeWrapperPass>().getDomTree(), BFI, | ||||||||||
247 | &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI( | ||||||||||
248 | *L->getHeader()->getParent()), | ||||||||||
249 | &getAnalysis<TargetTransformInfoWrapperPass>().getTTI( | ||||||||||
250 | *L->getHeader()->getParent()), | ||||||||||
251 | SE ? &SE->getSE() : nullptr, MSSA, &ORE); | ||||||||||
252 | } | ||||||||||
253 | |||||||||||
254 | /// This transformation requires natural loop information & requires that | ||||||||||
255 | /// loop preheaders be inserted into the CFG... | ||||||||||
256 | /// | ||||||||||
257 | void getAnalysisUsage(AnalysisUsage &AU) const override { | ||||||||||
258 | AU.addPreserved<DominatorTreeWrapperPass>(); | ||||||||||
259 | AU.addPreserved<LoopInfoWrapperPass>(); | ||||||||||
260 | AU.addRequired<TargetLibraryInfoWrapperPass>(); | ||||||||||
261 | if (EnableMSSALoopDependency) { | ||||||||||
262 | AU.addRequired<MemorySSAWrapperPass>(); | ||||||||||
263 | AU.addPreserved<MemorySSAWrapperPass>(); | ||||||||||
264 | } | ||||||||||
265 | AU.addRequired<TargetTransformInfoWrapperPass>(); | ||||||||||
266 | getLoopAnalysisUsage(AU); | ||||||||||
267 | LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU); | ||||||||||
268 | AU.addPreserved<LazyBlockFrequencyInfoPass>(); | ||||||||||
269 | AU.addPreserved<LazyBranchProbabilityInfoPass>(); | ||||||||||
270 | } | ||||||||||
271 | |||||||||||
272 | private: | ||||||||||
273 | LoopInvariantCodeMotion LICM; | ||||||||||
274 | }; | ||||||||||
275 | } // namespace | ||||||||||
276 | |||||||||||
277 | PreservedAnalyses LICMPass::run(Loop &L, LoopAnalysisManager &AM, | ||||||||||
278 | LoopStandardAnalysisResults &AR, LPMUpdater &) { | ||||||||||
279 | // For the new PM, we also can't use OptimizationRemarkEmitter as an analysis | ||||||||||
280 | // pass. Function analyses need to be preserved across loop transformations | ||||||||||
281 | // but ORE cannot be preserved (see comment before the pass definition). | ||||||||||
282 | OptimizationRemarkEmitter ORE(L.getHeader()->getParent()); | ||||||||||
283 | |||||||||||
284 | LoopInvariantCodeMotion LICM(LicmMssaOptCap, LicmMssaNoAccForPromotionCap); | ||||||||||
285 | if (!LICM.runOnLoop(&L, &AR.AA, &AR.LI, &AR.DT, AR.BFI, &AR.TLI, &AR.TTI, | ||||||||||
286 | &AR.SE, AR.MSSA, &ORE)) | ||||||||||
287 | return PreservedAnalyses::all(); | ||||||||||
288 | |||||||||||
289 | auto PA = getLoopPassPreservedAnalyses(); | ||||||||||
290 | |||||||||||
291 | PA.preserve<DominatorTreeAnalysis>(); | ||||||||||
292 | PA.preserve<LoopAnalysis>(); | ||||||||||
293 | if (AR.MSSA) | ||||||||||
294 | PA.preserve<MemorySSAAnalysis>(); | ||||||||||
295 | |||||||||||
296 | return PA; | ||||||||||
297 | } | ||||||||||
298 | |||||||||||
299 | char LegacyLICMPass::ID = 0; | ||||||||||
300 | INITIALIZE_PASS_BEGIN(LegacyLICMPass, "licm", "Loop Invariant Code Motion",static void *initializeLegacyLICMPassPassOnce(PassRegistry & Registry) { | ||||||||||
301 | false, false)static void *initializeLegacyLICMPassPassOnce(PassRegistry & Registry) { | ||||||||||
302 | INITIALIZE_PASS_DEPENDENCY(LoopPass)initializeLoopPassPass(Registry); | ||||||||||
303 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)initializeTargetLibraryInfoWrapperPassPass(Registry); | ||||||||||
304 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)initializeTargetTransformInfoWrapperPassPass(Registry); | ||||||||||
305 | INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)initializeMemorySSAWrapperPassPass(Registry); | ||||||||||
306 | INITIALIZE_PASS_DEPENDENCY(LazyBFIPass)initializeLazyBFIPassPass(Registry); | ||||||||||
307 | 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)); } | ||||||||||
308 | 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)); } | ||||||||||
309 | |||||||||||
310 | Pass *llvm::createLICMPass() { return new LegacyLICMPass(); } | ||||||||||
311 | Pass *llvm::createLICMPass(unsigned LicmMssaOptCap, | ||||||||||
312 | unsigned LicmMssaNoAccForPromotionCap) { | ||||||||||
313 | return new LegacyLICMPass(LicmMssaOptCap, LicmMssaNoAccForPromotionCap); | ||||||||||
314 | } | ||||||||||
315 | |||||||||||
316 | llvm::SinkAndHoistLICMFlags::SinkAndHoistLICMFlags(bool IsSink, Loop *L, | ||||||||||
317 | MemorySSA *MSSA) | ||||||||||
318 | : SinkAndHoistLICMFlags(SetLicmMssaOptCap, SetLicmMssaNoAccForPromotionCap, | ||||||||||
319 | IsSink, L, MSSA) {} | ||||||||||
320 | |||||||||||
321 | llvm::SinkAndHoistLICMFlags::SinkAndHoistLICMFlags( | ||||||||||
322 | unsigned LicmMssaOptCap, unsigned LicmMssaNoAccForPromotionCap, bool IsSink, | ||||||||||
323 | Loop *L, MemorySSA *MSSA) | ||||||||||
324 | : LicmMssaOptCap(LicmMssaOptCap), | ||||||||||
325 | LicmMssaNoAccForPromotionCap(LicmMssaNoAccForPromotionCap), | ||||||||||
326 | IsSink(IsSink) { | ||||||||||
327 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 328, __PRETTY_FUNCTION__)) | ||||||||||
328 | "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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 328, __PRETTY_FUNCTION__)); | ||||||||||
329 | if (!MSSA) | ||||||||||
330 | return; | ||||||||||
331 | |||||||||||
332 | unsigned AccessCapCount = 0; | ||||||||||
333 | for (auto *BB : L->getBlocks()) | ||||||||||
334 | if (const auto *Accesses = MSSA->getBlockAccesses(BB)) | ||||||||||
335 | for (const auto &MA : *Accesses) { | ||||||||||
336 | (void)MA; | ||||||||||
337 | ++AccessCapCount; | ||||||||||
338 | if (AccessCapCount > LicmMssaNoAccForPromotionCap) { | ||||||||||
339 | NoOfMemAccTooLarge = true; | ||||||||||
340 | return; | ||||||||||
341 | } | ||||||||||
342 | } | ||||||||||
343 | } | ||||||||||
344 | |||||||||||
345 | /// Hoist expressions out of the specified loop. Note, alias info for inner | ||||||||||
346 | /// loop is not preserved so it is not a good idea to run LICM multiple | ||||||||||
347 | /// times on one loop. | ||||||||||
348 | bool LoopInvariantCodeMotion::runOnLoop( | ||||||||||
349 | Loop *L, AAResults *AA, LoopInfo *LI, DominatorTree *DT, | ||||||||||
350 | BlockFrequencyInfo *BFI, TargetLibraryInfo *TLI, TargetTransformInfo *TTI, | ||||||||||
351 | ScalarEvolution *SE, MemorySSA *MSSA, OptimizationRemarkEmitter *ORE) { | ||||||||||
352 | bool Changed = false; | ||||||||||
353 | |||||||||||
354 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 354, __PRETTY_FUNCTION__)); | ||||||||||
355 | |||||||||||
356 | // If this loop has metadata indicating that LICM is not to be performed then | ||||||||||
357 | // just exit. | ||||||||||
358 | if (hasDisableLICMTransformsHint(L)) { | ||||||||||
359 | return false; | ||||||||||
360 | } | ||||||||||
361 | |||||||||||
362 | std::unique_ptr<AliasSetTracker> CurAST; | ||||||||||
363 | std::unique_ptr<MemorySSAUpdater> MSSAU; | ||||||||||
364 | std::unique_ptr<SinkAndHoistLICMFlags> Flags; | ||||||||||
365 | |||||||||||
366 | // Don't sink stores from loops with coroutine suspend instructions. | ||||||||||
367 | // LICM would sink instructions into the default destination of | ||||||||||
368 | // the coroutine switch. The default destination of the switch is to | ||||||||||
369 | // handle the case where the coroutine is suspended, by which point the | ||||||||||
370 | // coroutine frame may have been destroyed. No instruction can be sunk there. | ||||||||||
371 | // FIXME: This would unfortunately hurt the performance of coroutines, however | ||||||||||
372 | // there is currently no general solution for this. Similar issues could also | ||||||||||
373 | // potentially happen in other passes where instructions are being moved | ||||||||||
374 | // across that edge. | ||||||||||
375 | bool HasCoroSuspendInst = llvm::any_of(L->getBlocks(), [](BasicBlock *BB) { | ||||||||||
376 | return llvm::any_of(*BB, [](Instruction &I) { | ||||||||||
377 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I); | ||||||||||
378 | return II && II->getIntrinsicID() == Intrinsic::coro_suspend; | ||||||||||
379 | }); | ||||||||||
380 | }); | ||||||||||
381 | |||||||||||
382 | if (!MSSA) { | ||||||||||
383 | 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); | ||||||||||
384 | CurAST = collectAliasInfoForLoop(L, LI, AA); | ||||||||||
385 | Flags = std::make_unique<SinkAndHoistLICMFlags>( | ||||||||||
386 | LicmMssaOptCap, LicmMssaNoAccForPromotionCap, /*IsSink=*/true); | ||||||||||
387 | } else { | ||||||||||
388 | LLVM_DEBUG(dbgs() << "LICM: Using MemorySSA.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM: Using MemorySSA.\n"; } } while (false); | ||||||||||
389 | MSSAU = std::make_unique<MemorySSAUpdater>(MSSA); | ||||||||||
390 | Flags = std::make_unique<SinkAndHoistLICMFlags>( | ||||||||||
391 | LicmMssaOptCap, LicmMssaNoAccForPromotionCap, /*IsSink=*/true, L, MSSA); | ||||||||||
392 | } | ||||||||||
393 | |||||||||||
394 | // Get the preheader block to move instructions into... | ||||||||||
395 | BasicBlock *Preheader = L->getLoopPreheader(); | ||||||||||
396 | |||||||||||
397 | // Compute loop safety information. | ||||||||||
398 | ICFLoopSafetyInfo SafetyInfo; | ||||||||||
399 | SafetyInfo.computeLoopSafetyInfo(L); | ||||||||||
400 | |||||||||||
401 | // We want to visit all of the instructions in this loop... that are not parts | ||||||||||
402 | // of our subloops (they have already had their invariants hoisted out of | ||||||||||
403 | // their loop, into this loop, so there is no need to process the BODIES of | ||||||||||
404 | // the subloops). | ||||||||||
405 | // | ||||||||||
406 | // Traverse the body of the loop in depth first order on the dominator tree so | ||||||||||
407 | // that we are guaranteed to see definitions before we see uses. This allows | ||||||||||
408 | // us to sink instructions in one pass, without iteration. After sinking | ||||||||||
409 | // instructions, we perform another pass to hoist them out of the loop. | ||||||||||
410 | if (L->hasDedicatedExits()) | ||||||||||
411 | Changed |= | ||||||||||
412 | sinkRegion(DT->getNode(L->getHeader()), AA, LI, DT, BFI, TLI, TTI, L, | ||||||||||
413 | CurAST.get(), MSSAU.get(), &SafetyInfo, *Flags.get(), ORE); | ||||||||||
414 | Flags->setIsSink(false); | ||||||||||
415 | if (Preheader) | ||||||||||
416 | Changed |= hoistRegion(DT->getNode(L->getHeader()), AA, LI, DT, BFI, TLI, L, | ||||||||||
417 | CurAST.get(), MSSAU.get(), SE, &SafetyInfo, | ||||||||||
418 | *Flags.get(), ORE); | ||||||||||
419 | |||||||||||
420 | // Now that all loop invariants have been removed from the loop, promote any | ||||||||||
421 | // memory references to scalars that we can. | ||||||||||
422 | // Don't sink stores from loops without dedicated block exits. Exits | ||||||||||
423 | // containing indirect branches are not transformed by loop simplify, | ||||||||||
424 | // make sure we catch that. An additional load may be generated in the | ||||||||||
425 | // preheader for SSA updater, so also avoid sinking when no preheader | ||||||||||
426 | // is available. | ||||||||||
427 | if (!DisablePromotion && Preheader && L->hasDedicatedExits() && | ||||||||||
428 | !Flags->tooManyMemoryAccesses() && !HasCoroSuspendInst) { | ||||||||||
429 | // Figure out the loop exits and their insertion points | ||||||||||
430 | SmallVector<BasicBlock *, 8> ExitBlocks; | ||||||||||
431 | L->getUniqueExitBlocks(ExitBlocks); | ||||||||||
432 | |||||||||||
433 | // We can't insert into a catchswitch. | ||||||||||
434 | bool HasCatchSwitch = llvm::any_of(ExitBlocks, [](BasicBlock *Exit) { | ||||||||||
435 | return isa<CatchSwitchInst>(Exit->getTerminator()); | ||||||||||
436 | }); | ||||||||||
437 | |||||||||||
438 | if (!HasCatchSwitch) { | ||||||||||
439 | SmallVector<Instruction *, 8> InsertPts; | ||||||||||
440 | SmallVector<MemoryAccess *, 8> MSSAInsertPts; | ||||||||||
441 | InsertPts.reserve(ExitBlocks.size()); | ||||||||||
442 | if (MSSAU) | ||||||||||
443 | MSSAInsertPts.reserve(ExitBlocks.size()); | ||||||||||
444 | for (BasicBlock *ExitBlock : ExitBlocks) { | ||||||||||
445 | InsertPts.push_back(&*ExitBlock->getFirstInsertionPt()); | ||||||||||
446 | if (MSSAU) | ||||||||||
447 | MSSAInsertPts.push_back(nullptr); | ||||||||||
448 | } | ||||||||||
449 | |||||||||||
450 | PredIteratorCache PIC; | ||||||||||
451 | |||||||||||
452 | bool Promoted = false; | ||||||||||
453 | if (CurAST.get()) { | ||||||||||
454 | // Loop over all of the alias sets in the tracker object. | ||||||||||
455 | for (AliasSet &AS : *CurAST) { | ||||||||||
456 | // We can promote this alias set if it has a store, if it is a "Must" | ||||||||||
457 | // alias set, if the pointer is loop invariant, and if we are not | ||||||||||
458 | // eliminating any volatile loads or stores. | ||||||||||
459 | if (AS.isForwardingAliasSet() || !AS.isMod() || !AS.isMustAlias() || | ||||||||||
460 | !L->isLoopInvariant(AS.begin()->getValue())) | ||||||||||
461 | continue; | ||||||||||
462 | |||||||||||
463 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 465, __PRETTY_FUNCTION__)) | ||||||||||
464 | !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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 465, __PRETTY_FUNCTION__)) | ||||||||||
465 | "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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 465, __PRETTY_FUNCTION__)); | ||||||||||
466 | |||||||||||
467 | SmallSetVector<Value *, 8> PointerMustAliases; | ||||||||||
468 | for (const auto &ASI : AS) | ||||||||||
469 | PointerMustAliases.insert(ASI.getValue()); | ||||||||||
470 | |||||||||||
471 | Promoted |= promoteLoopAccessesToScalars( | ||||||||||
472 | PointerMustAliases, ExitBlocks, InsertPts, MSSAInsertPts, PIC, LI, | ||||||||||
473 | DT, TLI, L, CurAST.get(), MSSAU.get(), &SafetyInfo, ORE); | ||||||||||
474 | } | ||||||||||
475 | } else { | ||||||||||
476 | SmallVector<Instruction *, 16> MaybePromotable; | ||||||||||
477 | foreachMemoryAccess(MSSA, L, [&](Instruction *I) { | ||||||||||
478 | MaybePromotable.push_back(I); | ||||||||||
479 | }); | ||||||||||
480 | |||||||||||
481 | // Promoting one set of accesses may make the pointers for another set | ||||||||||
482 | // loop invariant, so run this in a loop (with the MaybePromotable set | ||||||||||
483 | // decreasing in size over time). | ||||||||||
484 | bool LocalPromoted; | ||||||||||
485 | do { | ||||||||||
486 | LocalPromoted = false; | ||||||||||
487 | for (const SmallSetVector<Value *, 8> &PointerMustAliases : | ||||||||||
488 | collectPromotionCandidates(MSSA, AA, L, MaybePromotable)) { | ||||||||||
489 | LocalPromoted |= promoteLoopAccessesToScalars( | ||||||||||
490 | PointerMustAliases, ExitBlocks, InsertPts, MSSAInsertPts, PIC, | ||||||||||
491 | LI, DT, TLI, L, /*AST*/nullptr, MSSAU.get(), &SafetyInfo, ORE); | ||||||||||
492 | } | ||||||||||
493 | Promoted |= LocalPromoted; | ||||||||||
494 | } while (LocalPromoted); | ||||||||||
495 | } | ||||||||||
496 | |||||||||||
497 | // Once we have promoted values across the loop body we have to | ||||||||||
498 | // recursively reform LCSSA as any nested loop may now have values defined | ||||||||||
499 | // within the loop used in the outer loop. | ||||||||||
500 | // FIXME: This is really heavy handed. It would be a bit better to use an | ||||||||||
501 | // SSAUpdater strategy during promotion that was LCSSA aware and reformed | ||||||||||
502 | // it as it went. | ||||||||||
503 | if (Promoted) | ||||||||||
504 | formLCSSARecursively(*L, *DT, LI, SE); | ||||||||||
505 | |||||||||||
506 | Changed |= Promoted; | ||||||||||
507 | } | ||||||||||
508 | } | ||||||||||
509 | |||||||||||
510 | // Check that neither this loop nor its parent have had LCSSA broken. LICM is | ||||||||||
511 | // specifically moving instructions across the loop boundary and so it is | ||||||||||
512 | // especially in need of sanity checking here. | ||||||||||
513 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 513, __PRETTY_FUNCTION__)); | ||||||||||
514 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 515, __PRETTY_FUNCTION__)) | ||||||||||
515 | "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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 515, __PRETTY_FUNCTION__)); | ||||||||||
516 | |||||||||||
517 | if (MSSAU.get() && VerifyMemorySSA) | ||||||||||
518 | MSSAU->getMemorySSA()->verifyMemorySSA(); | ||||||||||
519 | |||||||||||
520 | if (Changed && SE) | ||||||||||
521 | SE->forgetLoopDispositions(L); | ||||||||||
522 | return Changed; | ||||||||||
523 | } | ||||||||||
524 | |||||||||||
525 | /// Walk the specified region of the CFG (defined by all blocks dominated by | ||||||||||
526 | /// the specified block, and that are in the current loop) in reverse depth | ||||||||||
527 | /// first order w.r.t the DominatorTree. This allows us to visit uses before | ||||||||||
528 | /// definitions, allowing us to sink a loop body in one pass without iteration. | ||||||||||
529 | /// | ||||||||||
530 | bool llvm::sinkRegion(DomTreeNode *N, AAResults *AA, LoopInfo *LI, | ||||||||||
531 | DominatorTree *DT, BlockFrequencyInfo *BFI, | ||||||||||
532 | TargetLibraryInfo *TLI, TargetTransformInfo *TTI, | ||||||||||
533 | Loop *CurLoop, AliasSetTracker *CurAST, | ||||||||||
534 | MemorySSAUpdater *MSSAU, ICFLoopSafetyInfo *SafetyInfo, | ||||||||||
535 | SinkAndHoistLICMFlags &Flags, | ||||||||||
536 | OptimizationRemarkEmitter *ORE) { | ||||||||||
537 | |||||||||||
538 | // Verify inputs. | ||||||||||
539 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 541, __PRETTY_FUNCTION__)) | ||||||||||
540 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 541, __PRETTY_FUNCTION__)) | ||||||||||
541 | "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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 541, __PRETTY_FUNCTION__)); | ||||||||||
542 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 543, __PRETTY_FUNCTION__)) | ||||||||||
543 | "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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 543, __PRETTY_FUNCTION__)); | ||||||||||
544 | |||||||||||
545 | // We want to visit children before parents. We will enque all the parents | ||||||||||
546 | // before their children in the worklist and process the worklist in reverse | ||||||||||
547 | // order. | ||||||||||
548 | SmallVector<DomTreeNode *, 16> Worklist = collectChildrenInLoop(N, CurLoop); | ||||||||||
549 | |||||||||||
550 | bool Changed = false; | ||||||||||
551 | for (DomTreeNode *DTN : reverse(Worklist)) { | ||||||||||
552 | BasicBlock *BB = DTN->getBlock(); | ||||||||||
553 | // Only need to process the contents of this block if it is not part of a | ||||||||||
554 | // subloop (which would already have been processed). | ||||||||||
555 | if (inSubLoop(BB, CurLoop, LI)) | ||||||||||
556 | continue; | ||||||||||
557 | |||||||||||
558 | for (BasicBlock::iterator II = BB->end(); II != BB->begin();) { | ||||||||||
559 | Instruction &I = *--II; | ||||||||||
560 | |||||||||||
561 | // If the instruction is dead, we would try to sink it because it isn't | ||||||||||
562 | // used in the loop, instead, just delete it. | ||||||||||
563 | if (isInstructionTriviallyDead(&I, TLI)) { | ||||||||||
564 | 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); | ||||||||||
565 | salvageKnowledge(&I); | ||||||||||
566 | salvageDebugInfo(I); | ||||||||||
567 | ++II; | ||||||||||
568 | eraseInstruction(I, *SafetyInfo, CurAST, MSSAU); | ||||||||||
569 | Changed = true; | ||||||||||
570 | continue; | ||||||||||
571 | } | ||||||||||
572 | |||||||||||
573 | // Check to see if we can sink this instruction to the exit blocks | ||||||||||
574 | // of the loop. We can do this if the all users of the instruction are | ||||||||||
575 | // outside of the loop. In this case, it doesn't even matter if the | ||||||||||
576 | // operands of the instruction are loop invariant. | ||||||||||
577 | // | ||||||||||
578 | bool FreeInLoop = false; | ||||||||||
579 | if (!I.mayHaveSideEffects() && | ||||||||||
580 | isNotUsedOrFreeInLoop(I, CurLoop, SafetyInfo, TTI, FreeInLoop) && | ||||||||||
581 | canSinkOrHoistInst(I, AA, DT, CurLoop, CurAST, MSSAU, true, &Flags, | ||||||||||
582 | ORE)) { | ||||||||||
583 | if (sink(I, LI, DT, BFI, CurLoop, SafetyInfo, MSSAU, ORE)) { | ||||||||||
584 | if (!FreeInLoop) { | ||||||||||
585 | ++II; | ||||||||||
586 | salvageDebugInfo(I); | ||||||||||
587 | eraseInstruction(I, *SafetyInfo, CurAST, MSSAU); | ||||||||||
588 | } | ||||||||||
589 | Changed = true; | ||||||||||
590 | } | ||||||||||
591 | } | ||||||||||
592 | } | ||||||||||
593 | } | ||||||||||
594 | if (MSSAU && VerifyMemorySSA) | ||||||||||
595 | MSSAU->getMemorySSA()->verifyMemorySSA(); | ||||||||||
596 | return Changed; | ||||||||||
597 | } | ||||||||||
598 | |||||||||||
599 | namespace { | ||||||||||
600 | // This is a helper class for hoistRegion to make it able to hoist control flow | ||||||||||
601 | // in order to be able to hoist phis. The way this works is that we initially | ||||||||||
602 | // start hoisting to the loop preheader, and when we see a loop invariant branch | ||||||||||
603 | // we make note of this. When we then come to hoist an instruction that's | ||||||||||
604 | // conditional on such a branch we duplicate the branch and the relevant control | ||||||||||
605 | // flow, then hoist the instruction into the block corresponding to its original | ||||||||||
606 | // block in the duplicated control flow. | ||||||||||
607 | class ControlFlowHoister { | ||||||||||
608 | private: | ||||||||||
609 | // Information about the loop we are hoisting from | ||||||||||
610 | LoopInfo *LI; | ||||||||||
611 | DominatorTree *DT; | ||||||||||
612 | Loop *CurLoop; | ||||||||||
613 | MemorySSAUpdater *MSSAU; | ||||||||||
614 | |||||||||||
615 | // A map of blocks in the loop to the block their instructions will be hoisted | ||||||||||
616 | // to. | ||||||||||
617 | DenseMap<BasicBlock *, BasicBlock *> HoistDestinationMap; | ||||||||||
618 | |||||||||||
619 | // The branches that we can hoist, mapped to the block that marks a | ||||||||||
620 | // convergence point of their control flow. | ||||||||||
621 | DenseMap<BranchInst *, BasicBlock *> HoistableBranches; | ||||||||||
622 | |||||||||||
623 | public: | ||||||||||
624 | ControlFlowHoister(LoopInfo *LI, DominatorTree *DT, Loop *CurLoop, | ||||||||||
625 | MemorySSAUpdater *MSSAU) | ||||||||||
626 | : LI(LI), DT(DT), CurLoop(CurLoop), MSSAU(MSSAU) {} | ||||||||||
627 | |||||||||||
628 | void registerPossiblyHoistableBranch(BranchInst *BI) { | ||||||||||
629 | // We can only hoist conditional branches with loop invariant operands. | ||||||||||
630 | if (!ControlFlowHoisting || !BI->isConditional() || | ||||||||||
631 | !CurLoop->hasLoopInvariantOperands(BI)) | ||||||||||
632 | return; | ||||||||||
633 | |||||||||||
634 | // The branch destinations need to be in the loop, and we don't gain | ||||||||||
635 | // anything by duplicating conditional branches with duplicate successors, | ||||||||||
636 | // as it's essentially the same as an unconditional branch. | ||||||||||
637 | BasicBlock *TrueDest = BI->getSuccessor(0); | ||||||||||
638 | BasicBlock *FalseDest = BI->getSuccessor(1); | ||||||||||
639 | if (!CurLoop->contains(TrueDest) || !CurLoop->contains(FalseDest) || | ||||||||||
640 | TrueDest == FalseDest) | ||||||||||
641 | return; | ||||||||||
642 | |||||||||||
643 | // We can hoist BI if one branch destination is the successor of the other, | ||||||||||
644 | // or both have common successor which we check by seeing if the | ||||||||||
645 | // intersection of their successors is non-empty. | ||||||||||
646 | // TODO: This could be expanded to allowing branches where both ends | ||||||||||
647 | // eventually converge to a single block. | ||||||||||
648 | SmallPtrSet<BasicBlock *, 4> TrueDestSucc, FalseDestSucc; | ||||||||||
649 | TrueDestSucc.insert(succ_begin(TrueDest), succ_end(TrueDest)); | ||||||||||
650 | FalseDestSucc.insert(succ_begin(FalseDest), succ_end(FalseDest)); | ||||||||||
651 | BasicBlock *CommonSucc = nullptr; | ||||||||||
652 | if (TrueDestSucc.count(FalseDest)) { | ||||||||||
653 | CommonSucc = FalseDest; | ||||||||||
654 | } else if (FalseDestSucc.count(TrueDest)) { | ||||||||||
655 | CommonSucc = TrueDest; | ||||||||||
656 | } else { | ||||||||||
657 | set_intersect(TrueDestSucc, FalseDestSucc); | ||||||||||
658 | // If there's one common successor use that. | ||||||||||
659 | if (TrueDestSucc.size() == 1) | ||||||||||
660 | CommonSucc = *TrueDestSucc.begin(); | ||||||||||
661 | // If there's more than one pick whichever appears first in the block list | ||||||||||
662 | // (we can't use the value returned by TrueDestSucc.begin() as it's | ||||||||||
663 | // unpredicatable which element gets returned). | ||||||||||
664 | else if (!TrueDestSucc.empty()) { | ||||||||||
665 | Function *F = TrueDest->getParent(); | ||||||||||
666 | auto IsSucc = [&](BasicBlock &BB) { return TrueDestSucc.count(&BB); }; | ||||||||||
667 | auto It = llvm::find_if(*F, IsSucc); | ||||||||||
668 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 668, __PRETTY_FUNCTION__)); | ||||||||||
669 | CommonSucc = &*It; | ||||||||||
670 | } | ||||||||||
671 | } | ||||||||||
672 | // The common successor has to be dominated by the branch, as otherwise | ||||||||||
673 | // there will be some other path to the successor that will not be | ||||||||||
674 | // controlled by this branch so any phi we hoist would be controlled by the | ||||||||||
675 | // wrong condition. This also takes care of avoiding hoisting of loop back | ||||||||||
676 | // edges. | ||||||||||
677 | // TODO: In some cases this could be relaxed if the successor is dominated | ||||||||||
678 | // by another block that's been hoisted and we can guarantee that the | ||||||||||
679 | // control flow has been replicated exactly. | ||||||||||
680 | if (CommonSucc && DT->dominates(BI, CommonSucc)) | ||||||||||
681 | HoistableBranches[BI] = CommonSucc; | ||||||||||
682 | } | ||||||||||
683 | |||||||||||
684 | bool canHoistPHI(PHINode *PN) { | ||||||||||
685 | // The phi must have loop invariant operands. | ||||||||||
686 | if (!ControlFlowHoisting || !CurLoop->hasLoopInvariantOperands(PN)) | ||||||||||
687 | return false; | ||||||||||
688 | // We can hoist phis if the block they are in is the target of hoistable | ||||||||||
689 | // branches which cover all of the predecessors of the block. | ||||||||||
690 | SmallPtrSet<BasicBlock *, 8> PredecessorBlocks; | ||||||||||
691 | BasicBlock *BB = PN->getParent(); | ||||||||||
692 | for (BasicBlock *PredBB : predecessors(BB)) | ||||||||||
693 | PredecessorBlocks.insert(PredBB); | ||||||||||
694 | // If we have less predecessor blocks than predecessors then the phi will | ||||||||||
695 | // have more than one incoming value for the same block which we can't | ||||||||||
696 | // handle. | ||||||||||
697 | // TODO: This could be handled be erasing some of the duplicate incoming | ||||||||||
698 | // values. | ||||||||||
699 | if (PredecessorBlocks.size() != pred_size(BB)) | ||||||||||
700 | return false; | ||||||||||
701 | for (auto &Pair : HoistableBranches) { | ||||||||||
702 | if (Pair.second == BB) { | ||||||||||
703 | // Which blocks are predecessors via this branch depends on if the | ||||||||||
704 | // branch is triangle-like or diamond-like. | ||||||||||
705 | if (Pair.first->getSuccessor(0) == BB) { | ||||||||||
706 | PredecessorBlocks.erase(Pair.first->getParent()); | ||||||||||
707 | PredecessorBlocks.erase(Pair.first->getSuccessor(1)); | ||||||||||
708 | } else if (Pair.first->getSuccessor(1) == BB) { | ||||||||||
709 | PredecessorBlocks.erase(Pair.first->getParent()); | ||||||||||
710 | PredecessorBlocks.erase(Pair.first->getSuccessor(0)); | ||||||||||
711 | } else { | ||||||||||
712 | PredecessorBlocks.erase(Pair.first->getSuccessor(0)); | ||||||||||
713 | PredecessorBlocks.erase(Pair.first->getSuccessor(1)); | ||||||||||
714 | } | ||||||||||
715 | } | ||||||||||
716 | } | ||||||||||
717 | // PredecessorBlocks will now be empty if for every predecessor of BB we | ||||||||||
718 | // found a hoistable branch source. | ||||||||||
719 | return PredecessorBlocks.empty(); | ||||||||||
720 | } | ||||||||||
721 | |||||||||||
722 | BasicBlock *getOrCreateHoistedBlock(BasicBlock *BB) { | ||||||||||
723 | if (!ControlFlowHoisting) | ||||||||||
724 | return CurLoop->getLoopPreheader(); | ||||||||||
725 | // If BB has already been hoisted, return that | ||||||||||
726 | if (HoistDestinationMap.count(BB)) | ||||||||||
727 | return HoistDestinationMap[BB]; | ||||||||||
728 | |||||||||||
729 | // Check if this block is conditional based on a pending branch | ||||||||||
730 | auto HasBBAsSuccessor = | ||||||||||
731 | [&](DenseMap<BranchInst *, BasicBlock *>::value_type &Pair) { | ||||||||||
732 | return BB != Pair.second && (Pair.first->getSuccessor(0) == BB || | ||||||||||
733 | Pair.first->getSuccessor(1) == BB); | ||||||||||
734 | }; | ||||||||||
735 | auto It = llvm::find_if(HoistableBranches, HasBBAsSuccessor); | ||||||||||
736 | |||||||||||
737 | // If not involved in a pending branch, hoist to preheader | ||||||||||
738 | BasicBlock *InitialPreheader = CurLoop->getLoopPreheader(); | ||||||||||
739 | if (It == HoistableBranches.end()) { | ||||||||||
740 | 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) | ||||||||||
741 | << InitialPreheader->getNameOrAsOperand()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM using " << InitialPreheader ->getNameOrAsOperand() << " as hoist destination for " << BB->getNameOrAsOperand() << "\n"; } } while (false) | ||||||||||
742 | << " 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) | ||||||||||
743 | << BB->getNameOrAsOperand() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM using " << InitialPreheader ->getNameOrAsOperand() << " as hoist destination for " << BB->getNameOrAsOperand() << "\n"; } } while (false); | ||||||||||
744 | HoistDestinationMap[BB] = InitialPreheader; | ||||||||||
745 | return InitialPreheader; | ||||||||||
746 | } | ||||||||||
747 | BranchInst *BI = It->first; | ||||||||||
748 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 750, __PRETTY_FUNCTION__)) | ||||||||||
749 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 750, __PRETTY_FUNCTION__)) | ||||||||||
750 | "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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 750, __PRETTY_FUNCTION__)); | ||||||||||
751 | |||||||||||
752 | LLVMContext &C = BB->getContext(); | ||||||||||
753 | BasicBlock *TrueDest = BI->getSuccessor(0); | ||||||||||
754 | BasicBlock *FalseDest = BI->getSuccessor(1); | ||||||||||
755 | BasicBlock *CommonSucc = HoistableBranches[BI]; | ||||||||||
756 | BasicBlock *HoistTarget = getOrCreateHoistedBlock(BI->getParent()); | ||||||||||
757 | |||||||||||
758 | // Create hoisted versions of blocks that currently don't have them | ||||||||||
759 | auto CreateHoistedBlock = [&](BasicBlock *Orig) { | ||||||||||
760 | if (HoistDestinationMap.count(Orig)) | ||||||||||
761 | return HoistDestinationMap[Orig]; | ||||||||||
762 | BasicBlock *New = | ||||||||||
763 | BasicBlock::Create(C, Orig->getName() + ".licm", Orig->getParent()); | ||||||||||
764 | HoistDestinationMap[Orig] = New; | ||||||||||
765 | DT->addNewBlock(New, HoistTarget); | ||||||||||
766 | if (CurLoop->getParentLoop()) | ||||||||||
767 | CurLoop->getParentLoop()->addBasicBlockToLoop(New, *LI); | ||||||||||
768 | ++NumCreatedBlocks; | ||||||||||
769 | 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) | ||||||||||
770 | << " 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) | ||||||||||
771 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM created " << New-> getName() << " as hoist destination for " << Orig ->getName() << "\n"; } } while (false); | ||||||||||
772 | return New; | ||||||||||
773 | }; | ||||||||||
774 | BasicBlock *HoistTrueDest = CreateHoistedBlock(TrueDest); | ||||||||||
775 | BasicBlock *HoistFalseDest = CreateHoistedBlock(FalseDest); | ||||||||||
776 | BasicBlock *HoistCommonSucc = CreateHoistedBlock(CommonSucc); | ||||||||||
777 | |||||||||||
778 | // Link up these blocks with branches. | ||||||||||
779 | if (!HoistCommonSucc->getTerminator()) { | ||||||||||
780 | // The new common successor we've generated will branch to whatever that | ||||||||||
781 | // hoist target branched to. | ||||||||||
782 | BasicBlock *TargetSucc = HoistTarget->getSingleSuccessor(); | ||||||||||
783 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 783, __PRETTY_FUNCTION__)); | ||||||||||
784 | HoistCommonSucc->moveBefore(TargetSucc); | ||||||||||
785 | BranchInst::Create(TargetSucc, HoistCommonSucc); | ||||||||||
786 | } | ||||||||||
787 | if (!HoistTrueDest->getTerminator()) { | ||||||||||
788 | HoistTrueDest->moveBefore(HoistCommonSucc); | ||||||||||
789 | BranchInst::Create(HoistCommonSucc, HoistTrueDest); | ||||||||||
790 | } | ||||||||||
791 | if (!HoistFalseDest->getTerminator()) { | ||||||||||
792 | HoistFalseDest->moveBefore(HoistCommonSucc); | ||||||||||
793 | BranchInst::Create(HoistCommonSucc, HoistFalseDest); | ||||||||||
794 | } | ||||||||||
795 | |||||||||||
796 | // If BI is being cloned to what was originally the preheader then | ||||||||||
797 | // HoistCommonSucc will now be the new preheader. | ||||||||||
798 | if (HoistTarget == InitialPreheader) { | ||||||||||
799 | // Phis in the loop header now need to use the new preheader. | ||||||||||
800 | InitialPreheader->replaceSuccessorsPhiUsesWith(HoistCommonSucc); | ||||||||||
801 | if (MSSAU) | ||||||||||
802 | MSSAU->wireOldPredecessorsToNewImmediatePredecessor( | ||||||||||
803 | HoistTarget->getSingleSuccessor(), HoistCommonSucc, {HoistTarget}); | ||||||||||
804 | // The new preheader dominates the loop header. | ||||||||||
805 | DomTreeNode *PreheaderNode = DT->getNode(HoistCommonSucc); | ||||||||||
806 | DomTreeNode *HeaderNode = DT->getNode(CurLoop->getHeader()); | ||||||||||
807 | DT->changeImmediateDominator(HeaderNode, PreheaderNode); | ||||||||||
808 | // The preheader hoist destination is now the new preheader, with the | ||||||||||
809 | // exception of the hoist destination of this branch. | ||||||||||
810 | for (auto &Pair : HoistDestinationMap) | ||||||||||
811 | if (Pair.second == InitialPreheader && Pair.first != BI->getParent()) | ||||||||||
812 | Pair.second = HoistCommonSucc; | ||||||||||
813 | } | ||||||||||
814 | |||||||||||
815 | // Now finally clone BI. | ||||||||||
816 | ReplaceInstWithInst( | ||||||||||
817 | HoistTarget->getTerminator(), | ||||||||||
818 | BranchInst::Create(HoistTrueDest, HoistFalseDest, BI->getCondition())); | ||||||||||
819 | ++NumClonedBranches; | ||||||||||
820 | |||||||||||
821 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 822, __PRETTY_FUNCTION__)) | ||||||||||
822 | "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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 822, __PRETTY_FUNCTION__)); | ||||||||||
823 | return HoistDestinationMap[BB]; | ||||||||||
824 | } | ||||||||||
825 | }; | ||||||||||
826 | } // namespace | ||||||||||
827 | |||||||||||
828 | // Hoisting/sinking instruction out of a loop isn't always beneficial. It's only | ||||||||||
829 | // only worthwhile if the destination block is actually colder than current | ||||||||||
830 | // block. | ||||||||||
831 | static bool worthSinkOrHoistInst(Instruction &I, BasicBlock *DstBlock, | ||||||||||
832 | OptimizationRemarkEmitter *ORE, | ||||||||||
833 | BlockFrequencyInfo *BFI) { | ||||||||||
834 | // Check block frequency only when runtime profile is available | ||||||||||
835 | // to avoid pathological cases. With static profile, lean towards | ||||||||||
836 | // hosting because it helps canonicalize the loop for vectorizer. | ||||||||||
837 | if (!DstBlock->getParent()->hasProfileData()) | ||||||||||
838 | return true; | ||||||||||
839 | |||||||||||
840 | if (!HoistSinkColdnessThreshold || !BFI) | ||||||||||
841 | return true; | ||||||||||
842 | |||||||||||
843 | BasicBlock *SrcBlock = I.getParent(); | ||||||||||
844 | if (BFI->getBlockFreq(DstBlock).getFrequency() / HoistSinkColdnessThreshold > | ||||||||||
845 | BFI->getBlockFreq(SrcBlock).getFrequency()) { | ||||||||||
846 | ORE->emit([&]() { | ||||||||||
847 | return OptimizationRemarkMissed(DEBUG_TYPE"licm", "SinkHoistInst", &I) | ||||||||||
848 | << "failed to sink or hoist instruction because containing block " | ||||||||||
849 | "has lower frequency than destination block"; | ||||||||||
850 | }); | ||||||||||
851 | return false; | ||||||||||
852 | } | ||||||||||
853 | |||||||||||
854 | return true; | ||||||||||
855 | } | ||||||||||
856 | |||||||||||
857 | /// Walk the specified region of the CFG (defined by all blocks dominated by | ||||||||||
858 | /// the specified block, and that are in the current loop) in depth first | ||||||||||
859 | /// order w.r.t the DominatorTree. This allows us to visit definitions before | ||||||||||
860 | /// uses, allowing us to hoist a loop body in one pass without iteration. | ||||||||||
861 | /// | ||||||||||
862 | bool llvm::hoistRegion(DomTreeNode *N, AAResults *AA, LoopInfo *LI, | ||||||||||
863 | DominatorTree *DT, BlockFrequencyInfo *BFI, | ||||||||||
864 | TargetLibraryInfo *TLI, Loop *CurLoop, | ||||||||||
865 | AliasSetTracker *CurAST, MemorySSAUpdater *MSSAU, | ||||||||||
866 | ScalarEvolution *SE, ICFLoopSafetyInfo *SafetyInfo, | ||||||||||
867 | SinkAndHoistLICMFlags &Flags, | ||||||||||
868 | OptimizationRemarkEmitter *ORE) { | ||||||||||
869 | // Verify inputs. | ||||||||||
870 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 872, __PRETTY_FUNCTION__)) | ||||||||||
871 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 872, __PRETTY_FUNCTION__)) | ||||||||||
872 | "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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 872, __PRETTY_FUNCTION__)); | ||||||||||
873 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 874, __PRETTY_FUNCTION__)) | ||||||||||
874 | "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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 874, __PRETTY_FUNCTION__)); | ||||||||||
875 | |||||||||||
876 | ControlFlowHoister CFH(LI, DT, CurLoop, MSSAU); | ||||||||||
877 | |||||||||||
878 | // Keep track of instructions that have been hoisted, as they may need to be | ||||||||||
879 | // re-hoisted if they end up not dominating all of their uses. | ||||||||||
880 | SmallVector<Instruction *, 16> HoistedInstructions; | ||||||||||
881 | |||||||||||
882 | // For PHI hoisting to work we need to hoist blocks before their successors. | ||||||||||
883 | // We can do this by iterating through the blocks in the loop in reverse | ||||||||||
884 | // post-order. | ||||||||||
885 | LoopBlocksRPO Worklist(CurLoop); | ||||||||||
886 | Worklist.perform(LI); | ||||||||||
887 | bool Changed = false; | ||||||||||
888 | for (BasicBlock *BB : Worklist) { | ||||||||||
889 | // Only need to process the contents of this block if it is not part of a | ||||||||||
890 | // subloop (which would already have been processed). | ||||||||||
891 | if (inSubLoop(BB, CurLoop, LI)) | ||||||||||
892 | continue; | ||||||||||
893 | |||||||||||
894 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) { | ||||||||||
895 | Instruction &I = *II++; | ||||||||||
896 | // Try constant folding this instruction. If all the operands are | ||||||||||
897 | // constants, it is technically hoistable, but it would be better to | ||||||||||
898 | // just fold it. | ||||||||||
899 | if (Constant *C = ConstantFoldInstruction( | ||||||||||
900 | &I, I.getModule()->getDataLayout(), TLI)) { | ||||||||||
901 | LLVM_DEBUG(dbgs() << "LICM folding inst: " << I << " --> " << *Cdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM folding inst: " << I << " --> " << *C << '\n'; } } while (false) | ||||||||||
902 | << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM folding inst: " << I << " --> " << *C << '\n'; } } while (false); | ||||||||||
903 | if (CurAST) | ||||||||||
904 | CurAST->copyValue(&I, C); | ||||||||||
905 | // FIXME MSSA: Such replacements may make accesses unoptimized (D51960). | ||||||||||
906 | I.replaceAllUsesWith(C); | ||||||||||
907 | if (isInstructionTriviallyDead(&I, TLI)) | ||||||||||
908 | eraseInstruction(I, *SafetyInfo, CurAST, MSSAU); | ||||||||||
909 | Changed = true; | ||||||||||
910 | continue; | ||||||||||
911 | } | ||||||||||
912 | |||||||||||
913 | // Try hoisting the instruction out to the preheader. We can only do | ||||||||||
914 | // this if all of the operands of the instruction are loop invariant and | ||||||||||
915 | // if it is safe to hoist the instruction. We also check block frequency | ||||||||||
916 | // to make sure instruction only gets hoisted into colder blocks. | ||||||||||
917 | // TODO: It may be safe to hoist if we are hoisting to a conditional block | ||||||||||
918 | // and we have accurately duplicated the control flow from the loop header | ||||||||||
919 | // to that block. | ||||||||||
920 | if (CurLoop->hasLoopInvariantOperands(&I) && | ||||||||||
921 | canSinkOrHoistInst(I, AA, DT, CurLoop, CurAST, MSSAU, true, &Flags, | ||||||||||
922 | ORE) && | ||||||||||
923 | worthSinkOrHoistInst(I, CurLoop->getLoopPreheader(), ORE, BFI) && | ||||||||||
924 | isSafeToExecuteUnconditionally( | ||||||||||
925 | I, DT, TLI, CurLoop, SafetyInfo, ORE, | ||||||||||
926 | CurLoop->getLoopPreheader()->getTerminator())) { | ||||||||||
927 | hoist(I, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo, | ||||||||||
928 | MSSAU, SE, ORE); | ||||||||||
929 | HoistedInstructions.push_back(&I); | ||||||||||
930 | Changed = true; | ||||||||||
931 | continue; | ||||||||||
932 | } | ||||||||||
933 | |||||||||||
934 | // Attempt to remove floating point division out of the loop by | ||||||||||
935 | // converting it to a reciprocal multiplication. | ||||||||||
936 | if (I.getOpcode() == Instruction::FDiv && I.hasAllowReciprocal() && | ||||||||||
937 | CurLoop->isLoopInvariant(I.getOperand(1))) { | ||||||||||
938 | auto Divisor = I.getOperand(1); | ||||||||||
939 | auto One = llvm::ConstantFP::get(Divisor->getType(), 1.0); | ||||||||||
940 | auto ReciprocalDivisor = BinaryOperator::CreateFDiv(One, Divisor); | ||||||||||
941 | ReciprocalDivisor->setFastMathFlags(I.getFastMathFlags()); | ||||||||||
942 | SafetyInfo->insertInstructionTo(ReciprocalDivisor, I.getParent()); | ||||||||||
943 | ReciprocalDivisor->insertBefore(&I); | ||||||||||
944 | |||||||||||
945 | auto Product = | ||||||||||
946 | BinaryOperator::CreateFMul(I.getOperand(0), ReciprocalDivisor); | ||||||||||
947 | Product->setFastMathFlags(I.getFastMathFlags()); | ||||||||||
948 | SafetyInfo->insertInstructionTo(Product, I.getParent()); | ||||||||||
949 | Product->insertAfter(&I); | ||||||||||
950 | I.replaceAllUsesWith(Product); | ||||||||||
951 | eraseInstruction(I, *SafetyInfo, CurAST, MSSAU); | ||||||||||
952 | |||||||||||
953 | hoist(*ReciprocalDivisor, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), | ||||||||||
954 | SafetyInfo, MSSAU, SE, ORE); | ||||||||||
955 | HoistedInstructions.push_back(ReciprocalDivisor); | ||||||||||
956 | Changed = true; | ||||||||||
957 | continue; | ||||||||||
958 | } | ||||||||||
959 | |||||||||||
960 | auto IsInvariantStart = [&](Instruction &I) { | ||||||||||
961 | using namespace PatternMatch; | ||||||||||
962 | return I.use_empty() && | ||||||||||
963 | match(&I, m_Intrinsic<Intrinsic::invariant_start>()); | ||||||||||
964 | }; | ||||||||||
965 | auto MustExecuteWithoutWritesBefore = [&](Instruction &I) { | ||||||||||
966 | return SafetyInfo->isGuaranteedToExecute(I, DT, CurLoop) && | ||||||||||
967 | SafetyInfo->doesNotWriteMemoryBefore(I, CurLoop); | ||||||||||
968 | }; | ||||||||||
969 | if ((IsInvariantStart(I) || isGuard(&I)) && | ||||||||||
970 | CurLoop->hasLoopInvariantOperands(&I) && | ||||||||||
971 | MustExecuteWithoutWritesBefore(I)) { | ||||||||||
972 | hoist(I, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo, | ||||||||||
973 | MSSAU, SE, ORE); | ||||||||||
974 | HoistedInstructions.push_back(&I); | ||||||||||
975 | Changed = true; | ||||||||||
976 | continue; | ||||||||||
977 | } | ||||||||||
978 | |||||||||||
979 | if (PHINode *PN = dyn_cast<PHINode>(&I)) { | ||||||||||
980 | if (CFH.canHoistPHI(PN)) { | ||||||||||
981 | // Redirect incoming blocks first to ensure that we create hoisted | ||||||||||
982 | // versions of those blocks before we hoist the phi. | ||||||||||
983 | for (unsigned int i = 0; i < PN->getNumIncomingValues(); ++i) | ||||||||||
984 | PN->setIncomingBlock( | ||||||||||
985 | i, CFH.getOrCreateHoistedBlock(PN->getIncomingBlock(i))); | ||||||||||
986 | hoist(*PN, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo, | ||||||||||
987 | MSSAU, SE, ORE); | ||||||||||
988 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 988, __PRETTY_FUNCTION__)); | ||||||||||
989 | Changed = true; | ||||||||||
990 | continue; | ||||||||||
991 | } | ||||||||||
992 | } | ||||||||||
993 | |||||||||||
994 | // Remember possibly hoistable branches so we can actually hoist them | ||||||||||
995 | // later if needed. | ||||||||||
996 | if (BranchInst *BI = dyn_cast<BranchInst>(&I)) | ||||||||||
997 | CFH.registerPossiblyHoistableBranch(BI); | ||||||||||
998 | } | ||||||||||
999 | } | ||||||||||
1000 | |||||||||||
1001 | // If we hoisted instructions to a conditional block they may not dominate | ||||||||||
1002 | // their uses that weren't hoisted (such as phis where some operands are not | ||||||||||
1003 | // loop invariant). If so make them unconditional by moving them to their | ||||||||||
1004 | // immediate dominator. We iterate through the instructions in reverse order | ||||||||||
1005 | // which ensures that when we rehoist an instruction we rehoist its operands, | ||||||||||
1006 | // and also keep track of where in the block we are rehoisting to to make sure | ||||||||||
1007 | // that we rehoist instructions before the instructions that use them. | ||||||||||
1008 | Instruction *HoistPoint = nullptr; | ||||||||||
1009 | if (ControlFlowHoisting) { | ||||||||||
1010 | for (Instruction *I : reverse(HoistedInstructions)) { | ||||||||||
1011 | if (!llvm::all_of(I->uses(), | ||||||||||
1012 | [&](Use &U) { return DT->dominates(I, U); })) { | ||||||||||
1013 | BasicBlock *Dominator = | ||||||||||
1014 | DT->getNode(I->getParent())->getIDom()->getBlock(); | ||||||||||
1015 | if (!HoistPoint || !DT->dominates(HoistPoint->getParent(), Dominator)) { | ||||||||||
1016 | if (HoistPoint) | ||||||||||
1017 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1018, __PRETTY_FUNCTION__)) | ||||||||||
1018 | "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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1018, __PRETTY_FUNCTION__)); | ||||||||||
1019 | HoistPoint = Dominator->getTerminator(); | ||||||||||
1020 | } | ||||||||||
1021 | LLVM_DEBUG(dbgs() << "LICM rehoisting to "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM rehoisting to " << HoistPoint ->getParent()->getNameOrAsOperand() << ": " << *I << "\n"; } } while (false) | ||||||||||
1022 | << HoistPoint->getParent()->getNameOrAsOperand()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM rehoisting to " << HoistPoint ->getParent()->getNameOrAsOperand() << ": " << *I << "\n"; } } while (false) | ||||||||||
1023 | << ": " << *I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM rehoisting to " << HoistPoint ->getParent()->getNameOrAsOperand() << ": " << *I << "\n"; } } while (false); | ||||||||||
1024 | moveInstructionBefore(*I, *HoistPoint, *SafetyInfo, MSSAU, SE); | ||||||||||
1025 | HoistPoint = I; | ||||||||||
1026 | Changed = true; | ||||||||||
1027 | } | ||||||||||
1028 | } | ||||||||||
1029 | } | ||||||||||
1030 | if (MSSAU && VerifyMemorySSA) | ||||||||||
1031 | MSSAU->getMemorySSA()->verifyMemorySSA(); | ||||||||||
1032 | |||||||||||
1033 | // Now that we've finished hoisting make sure that LI and DT are still | ||||||||||
1034 | // valid. | ||||||||||
1035 | #ifdef EXPENSIVE_CHECKS | ||||||||||
1036 | if (Changed) { | ||||||||||
1037 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1038, __PRETTY_FUNCTION__)) | ||||||||||
1038 | "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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1038, __PRETTY_FUNCTION__)); | ||||||||||
1039 | LI->verify(*DT); | ||||||||||
1040 | } | ||||||||||
1041 | #endif | ||||||||||
1042 | |||||||||||
1043 | return Changed; | ||||||||||
1044 | } | ||||||||||
1045 | |||||||||||
1046 | // Return true if LI is invariant within scope of the loop. LI is invariant if | ||||||||||
1047 | // CurLoop is dominated by an invariant.start representing the same memory | ||||||||||
1048 | // location and size as the memory location LI loads from, and also the | ||||||||||
1049 | // invariant.start has no uses. | ||||||||||
1050 | static bool isLoadInvariantInLoop(LoadInst *LI, DominatorTree *DT, | ||||||||||
1051 | Loop *CurLoop) { | ||||||||||
1052 | Value *Addr = LI->getOperand(0); | ||||||||||
1053 | const DataLayout &DL = LI->getModule()->getDataLayout(); | ||||||||||
1054 | const TypeSize LocSizeInBits = DL.getTypeSizeInBits(LI->getType()); | ||||||||||
1055 | |||||||||||
1056 | // It is not currently possible for clang to generate an invariant.start | ||||||||||
1057 | // intrinsic with scalable vector types because we don't support thread local | ||||||||||
1058 | // sizeless types and we don't permit sizeless types in structs or classes. | ||||||||||
1059 | // Furthermore, even if support is added for this in future the intrinsic | ||||||||||
1060 | // itself is defined to have a size of -1 for variable sized objects. This | ||||||||||
1061 | // makes it impossible to verify if the intrinsic envelops our region of | ||||||||||
1062 | // interest. For example, both <vscale x 32 x i8> and <vscale x 16 x i8> | ||||||||||
1063 | // types would have a -1 parameter, but the former is clearly double the size | ||||||||||
1064 | // of the latter. | ||||||||||
1065 | if (LocSizeInBits.isScalable()) | ||||||||||
1066 | return false; | ||||||||||
1067 | |||||||||||
1068 | // if the type is i8 addrspace(x)*, we know this is the type of | ||||||||||
1069 | // llvm.invariant.start operand | ||||||||||
1070 | auto *PtrInt8Ty = PointerType::get(Type::getInt8Ty(LI->getContext()), | ||||||||||
1071 | LI->getPointerAddressSpace()); | ||||||||||
1072 | unsigned BitcastsVisited = 0; | ||||||||||
1073 | // Look through bitcasts until we reach the i8* type (this is invariant.start | ||||||||||
1074 | // operand type). | ||||||||||
1075 | while (Addr->getType() != PtrInt8Ty) { | ||||||||||
1076 | auto *BC = dyn_cast<BitCastInst>(Addr); | ||||||||||
1077 | // Avoid traversing high number of bitcast uses. | ||||||||||
1078 | if (++BitcastsVisited > MaxNumUsesTraversed || !BC) | ||||||||||
1079 | return false; | ||||||||||
1080 | Addr = BC->getOperand(0); | ||||||||||
1081 | } | ||||||||||
1082 | |||||||||||
1083 | unsigned UsesVisited = 0; | ||||||||||
1084 | // Traverse all uses of the load operand value, to see if invariant.start is | ||||||||||
1085 | // one of the uses, and whether it dominates the load instruction. | ||||||||||
1086 | for (auto *U : Addr->users()) { | ||||||||||
1087 | // Avoid traversing for Load operand with high number of users. | ||||||||||
1088 | if (++UsesVisited > MaxNumUsesTraversed) | ||||||||||
1089 | return false; | ||||||||||
1090 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(U); | ||||||||||
1091 | // If there are escaping uses of invariant.start instruction, the load maybe | ||||||||||
1092 | // non-invariant. | ||||||||||
1093 | if (!II || II->getIntrinsicID() != Intrinsic::invariant_start || | ||||||||||
1094 | !II->use_empty()) | ||||||||||
1095 | continue; | ||||||||||
1096 | ConstantInt *InvariantSize = cast<ConstantInt>(II->getArgOperand(0)); | ||||||||||
1097 | // The intrinsic supports having a -1 argument for variable sized objects | ||||||||||
1098 | // so we should check for that here. | ||||||||||
1099 | if (InvariantSize->isNegative()) | ||||||||||
1100 | continue; | ||||||||||
1101 | uint64_t InvariantSizeInBits = InvariantSize->getSExtValue() * 8; | ||||||||||
1102 | // Confirm the invariant.start location size contains the load operand size | ||||||||||
1103 | // in bits. Also, the invariant.start should dominate the load, and we | ||||||||||
1104 | // should not hoist the load out of a loop that contains this dominating | ||||||||||
1105 | // invariant.start. | ||||||||||
1106 | if (LocSizeInBits.getFixedSize() <= InvariantSizeInBits && | ||||||||||
1107 | DT->properlyDominates(II->getParent(), CurLoop->getHeader())) | ||||||||||
1108 | return true; | ||||||||||
1109 | } | ||||||||||
1110 | |||||||||||
1111 | return false; | ||||||||||
1112 | } | ||||||||||
1113 | |||||||||||
1114 | namespace { | ||||||||||
1115 | /// Return true if-and-only-if we know how to (mechanically) both hoist and | ||||||||||
1116 | /// sink a given instruction out of a loop. Does not address legality | ||||||||||
1117 | /// concerns such as aliasing or speculation safety. | ||||||||||
1118 | bool isHoistableAndSinkableInst(Instruction &I) { | ||||||||||
1119 | // Only these instructions are hoistable/sinkable. | ||||||||||
1120 | return (isa<LoadInst>(I) || isa<StoreInst>(I) || isa<CallInst>(I) || | ||||||||||
1121 | isa<FenceInst>(I) || isa<CastInst>(I) || isa<UnaryOperator>(I) || | ||||||||||
1122 | isa<BinaryOperator>(I) || isa<SelectInst>(I) || | ||||||||||
1123 | isa<GetElementPtrInst>(I) || isa<CmpInst>(I) || | ||||||||||
1124 | isa<InsertElementInst>(I) || isa<ExtractElementInst>(I) || | ||||||||||
1125 | isa<ShuffleVectorInst>(I) || isa<ExtractValueInst>(I) || | ||||||||||
1126 | isa<InsertValueInst>(I) || isa<FreezeInst>(I)); | ||||||||||
1127 | } | ||||||||||
1128 | /// Return true if all of the alias sets within this AST are known not to | ||||||||||
1129 | /// contain a Mod, or if MSSA knows thare are no MemoryDefs in the loop. | ||||||||||
1130 | bool isReadOnly(AliasSetTracker *CurAST, const MemorySSAUpdater *MSSAU, | ||||||||||
1131 | const Loop *L) { | ||||||||||
1132 | if (CurAST) { | ||||||||||
1133 | for (AliasSet &AS : *CurAST) { | ||||||||||
1134 | if (!AS.isForwardingAliasSet() && AS.isMod()) { | ||||||||||
1135 | return false; | ||||||||||
1136 | } | ||||||||||
1137 | } | ||||||||||
1138 | return true; | ||||||||||
1139 | } else { /*MSSAU*/ | ||||||||||
1140 | for (auto *BB : L->getBlocks()) | ||||||||||
1141 | if (MSSAU->getMemorySSA()->getBlockDefs(BB)) | ||||||||||
1142 | return false; | ||||||||||
1143 | return true; | ||||||||||
1144 | } | ||||||||||
1145 | } | ||||||||||
1146 | |||||||||||
1147 | /// Return true if I is the only Instruction with a MemoryAccess in L. | ||||||||||
1148 | bool isOnlyMemoryAccess(const Instruction *I, const Loop *L, | ||||||||||
1149 | const MemorySSAUpdater *MSSAU) { | ||||||||||
1150 | for (auto *BB : L->getBlocks()) | ||||||||||
1151 | if (auto *Accs = MSSAU->getMemorySSA()->getBlockAccesses(BB)) { | ||||||||||
1152 | int NotAPhi = 0; | ||||||||||
1153 | for (const auto &Acc : *Accs) { | ||||||||||
1154 | if (isa<MemoryPhi>(&Acc)) | ||||||||||
1155 | continue; | ||||||||||
1156 | const auto *MUD = cast<MemoryUseOrDef>(&Acc); | ||||||||||
1157 | if (MUD->getMemoryInst() != I || NotAPhi++ == 1) | ||||||||||
1158 | return false; | ||||||||||
1159 | } | ||||||||||
1160 | } | ||||||||||
1161 | return true; | ||||||||||
1162 | } | ||||||||||
1163 | } | ||||||||||
1164 | |||||||||||
1165 | bool llvm::canSinkOrHoistInst(Instruction &I, AAResults *AA, DominatorTree *DT, | ||||||||||
1166 | Loop *CurLoop, AliasSetTracker *CurAST, | ||||||||||
1167 | MemorySSAUpdater *MSSAU, | ||||||||||
1168 | bool TargetExecutesOncePerLoop, | ||||||||||
1169 | SinkAndHoistLICMFlags *Flags, | ||||||||||
1170 | OptimizationRemarkEmitter *ORE) { | ||||||||||
1171 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1172, __PRETTY_FUNCTION__)) | ||||||||||
| |||||||||||
1172 | "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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1172, __PRETTY_FUNCTION__)); | ||||||||||
1173 | |||||||||||
1174 | // If we don't understand the instruction, bail early. | ||||||||||
1175 | if (!isHoistableAndSinkableInst(I)) | ||||||||||
1176 | return false; | ||||||||||
1177 | |||||||||||
1178 | MemorySSA *MSSA = MSSAU
| ||||||||||
1179 | if (MSSA) | ||||||||||
1180 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1180, __PRETTY_FUNCTION__)); | ||||||||||
1181 | |||||||||||
1182 | // Loads have extra constraints we have to verify before we can hoist them. | ||||||||||
1183 | if (LoadInst *LI
| ||||||||||
1184 | if (!LI->isUnordered()) | ||||||||||
1185 | return false; // Don't sink/hoist volatile or ordered atomic loads! | ||||||||||
1186 | |||||||||||
1187 | // Loads from constant memory are always safe to move, even if they end up | ||||||||||
1188 | // in the same alias set as something that ends up being modified. | ||||||||||
1189 | if (AA->pointsToConstantMemory(LI->getOperand(0))) | ||||||||||
1190 | return true; | ||||||||||
1191 | if (LI->hasMetadata(LLVMContext::MD_invariant_load)) | ||||||||||
1192 | return true; | ||||||||||
1193 | |||||||||||
1194 | if (LI->isAtomic() && !TargetExecutesOncePerLoop) | ||||||||||
1195 | return false; // Don't risk duplicating unordered loads | ||||||||||
1196 | |||||||||||
1197 | // This checks for an invariant.start dominating the load. | ||||||||||
1198 | if (isLoadInvariantInLoop(LI, DT, CurLoop)) | ||||||||||
1199 | return true; | ||||||||||
1200 | |||||||||||
1201 | bool Invalidated; | ||||||||||
1202 | if (CurAST) | ||||||||||
1203 | Invalidated = pointerInvalidatedByLoop(MemoryLocation::get(LI), CurAST, | ||||||||||
1204 | CurLoop, AA); | ||||||||||
1205 | else | ||||||||||
1206 | Invalidated = pointerInvalidatedByLoopWithMSSA( | ||||||||||
1207 | MSSA, cast<MemoryUse>(MSSA->getMemoryAccess(LI)), CurLoop, I, *Flags); | ||||||||||
1208 | // Check loop-invariant address because this may also be a sinkable load | ||||||||||
1209 | // whose address is not necessarily loop-invariant. | ||||||||||
1210 | if (ORE && Invalidated && CurLoop->isLoopInvariant(LI->getPointerOperand())) | ||||||||||
1211 | ORE->emit([&]() { | ||||||||||
1212 | return OptimizationRemarkMissed( | ||||||||||
1213 | DEBUG_TYPE"licm", "LoadWithLoopInvariantAddressInvalidated", LI) | ||||||||||
1214 | << "failed to move load with loop-invariant address " | ||||||||||
1215 | "because the loop may invalidate its value"; | ||||||||||
1216 | }); | ||||||||||
1217 | |||||||||||
1218 | return !Invalidated; | ||||||||||
1219 | } else if (CallInst *CI
| ||||||||||
1220 | // Don't sink or hoist dbg info; it's legal, but not useful. | ||||||||||
1221 | if (isa<DbgInfoIntrinsic>(I)) | ||||||||||
1222 | return false; | ||||||||||
1223 | |||||||||||
1224 | // Don't sink calls which can throw. | ||||||||||
1225 | if (CI->mayThrow()) | ||||||||||
1226 | return false; | ||||||||||
1227 | |||||||||||
1228 | // Convergent attribute has been used on operations that involve | ||||||||||
1229 | // inter-thread communication which results are implicitly affected by the | ||||||||||
1230 | // enclosing control flows. It is not safe to hoist or sink such operations | ||||||||||
1231 | // across control flow. | ||||||||||
1232 | if (CI->isConvergent()) | ||||||||||
1233 | return false; | ||||||||||
1234 | |||||||||||
1235 | using namespace PatternMatch; | ||||||||||
1236 | if (match(CI, m_Intrinsic<Intrinsic::assume>())) | ||||||||||
1237 | // Assumes don't actually alias anything or throw | ||||||||||
1238 | return true; | ||||||||||
1239 | |||||||||||
1240 | if (match(CI, m_Intrinsic<Intrinsic::experimental_widenable_condition>())) | ||||||||||
1241 | // Widenable conditions don't actually alias anything or throw | ||||||||||
1242 | return true; | ||||||||||
1243 | |||||||||||
1244 | // Handle simple cases by querying alias analysis. | ||||||||||
1245 | FunctionModRefBehavior Behavior = AA->getModRefBehavior(CI); | ||||||||||
1246 | if (Behavior == FMRB_DoesNotAccessMemory) | ||||||||||
1247 | return true; | ||||||||||
1248 | if (AAResults::onlyReadsMemory(Behavior)) { | ||||||||||
1249 | // A readonly argmemonly function only reads from memory pointed to by | ||||||||||
1250 | // it's arguments with arbitrary offsets. If we can prove there are no | ||||||||||
1251 | // writes to this memory in the loop, we can hoist or sink. | ||||||||||
1252 | if (AAResults::onlyAccessesArgPointees(Behavior)) { | ||||||||||
1253 | // TODO: expand to writeable arguments | ||||||||||
1254 | for (Value *Op : CI->arg_operands()) | ||||||||||
1255 | if (Op->getType()->isPointerTy()) { | ||||||||||
1256 | bool Invalidated; | ||||||||||
1257 | if (CurAST
| ||||||||||
1258 | Invalidated = pointerInvalidatedByLoop( | ||||||||||
1259 | MemoryLocation::getBeforeOrAfter(Op), CurAST, CurLoop, AA); | ||||||||||
1260 | else | ||||||||||
1261 | Invalidated = pointerInvalidatedByLoopWithMSSA( | ||||||||||
1262 | MSSA, cast<MemoryUse>(MSSA->getMemoryAccess(CI)), CurLoop, I, | ||||||||||
| |||||||||||
1263 | *Flags); | ||||||||||
1264 | if (Invalidated) | ||||||||||
1265 | return false; | ||||||||||
1266 | } | ||||||||||
1267 | return true; | ||||||||||
1268 | } | ||||||||||
1269 | |||||||||||
1270 | // If this call only reads from memory and there are no writes to memory | ||||||||||
1271 | // in the loop, we can hoist or sink the call as appropriate. | ||||||||||
1272 | if (isReadOnly(CurAST, MSSAU, CurLoop)) | ||||||||||
1273 | return true; | ||||||||||
1274 | } | ||||||||||
1275 | |||||||||||
1276 | // FIXME: This should use mod/ref information to see if we can hoist or | ||||||||||
1277 | // sink the call. | ||||||||||
1278 | |||||||||||
1279 | return false; | ||||||||||
1280 | } else if (auto *FI = dyn_cast<FenceInst>(&I)) { | ||||||||||
1281 | // Fences alias (most) everything to provide ordering. For the moment, | ||||||||||
1282 | // just give up if there are any other memory operations in the loop. | ||||||||||
1283 | if (CurAST) { | ||||||||||
1284 | auto Begin = CurAST->begin(); | ||||||||||
1285 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1285, __PRETTY_FUNCTION__)); | ||||||||||
1286 | if (std::next(Begin) != CurAST->end()) | ||||||||||
1287 | // constant memory for instance, TODO: handle better | ||||||||||
1288 | return false; | ||||||||||
1289 | auto *UniqueI = Begin->getUniqueInstruction(); | ||||||||||
1290 | if (!UniqueI) | ||||||||||
1291 | // other memory op, give up | ||||||||||
1292 | return false; | ||||||||||
1293 | (void)FI; // suppress unused variable warning | ||||||||||
1294 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1294, __PRETTY_FUNCTION__)); | ||||||||||
1295 | return true; | ||||||||||
1296 | } else // MSSAU | ||||||||||
1297 | return isOnlyMemoryAccess(FI, CurLoop, MSSAU); | ||||||||||
1298 | } else if (auto *SI = dyn_cast<StoreInst>(&I)) { | ||||||||||
1299 | if (!SI->isUnordered()) | ||||||||||
1300 | return false; // Don't sink/hoist volatile or ordered atomic store! | ||||||||||
1301 | |||||||||||
1302 | // We can only hoist a store that we can prove writes a value which is not | ||||||||||
1303 | // read or overwritten within the loop. For those cases, we fallback to | ||||||||||
1304 | // load store promotion instead. TODO: We can extend this to cases where | ||||||||||
1305 | // there is exactly one write to the location and that write dominates an | ||||||||||
1306 | // arbitrary number of reads in the loop. | ||||||||||
1307 | if (CurAST) { | ||||||||||
1308 | auto &AS = CurAST->getAliasSetFor(MemoryLocation::get(SI)); | ||||||||||
1309 | |||||||||||
1310 | if (AS.isRef() || !AS.isMustAlias()) | ||||||||||
1311 | // Quick exit test, handled by the full path below as well. | ||||||||||
1312 | return false; | ||||||||||
1313 | auto *UniqueI = AS.getUniqueInstruction(); | ||||||||||
1314 | if (!UniqueI) | ||||||||||
1315 | // other memory op, give up | ||||||||||
1316 | return false; | ||||||||||
1317 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1317, __PRETTY_FUNCTION__)); | ||||||||||
1318 | return true; | ||||||||||
1319 | } else { // MSSAU | ||||||||||
1320 | if (isOnlyMemoryAccess(SI, CurLoop, MSSAU)) | ||||||||||
1321 | return true; | ||||||||||
1322 | // If there are more accesses than the Promotion cap or no "quota" to | ||||||||||
1323 | // check clobber, then give up as we're not walking a list that long. | ||||||||||
1324 | if (Flags->tooManyMemoryAccesses() || Flags->tooManyClobberingCalls()) | ||||||||||
1325 | return false; | ||||||||||
1326 | // If there are interfering Uses (i.e. their defining access is in the | ||||||||||
1327 | // loop), or ordered loads (stored as Defs!), don't move this store. | ||||||||||
1328 | // Could do better here, but this is conservatively correct. | ||||||||||
1329 | // TODO: Cache set of Uses on the first walk in runOnLoop, update when | ||||||||||
1330 | // moving accesses. Can also extend to dominating uses. | ||||||||||
1331 | auto *SIMD = MSSA->getMemoryAccess(SI); | ||||||||||
1332 | for (auto *BB : CurLoop->getBlocks()) | ||||||||||
1333 | if (auto *Accesses = MSSA->getBlockAccesses(BB)) { | ||||||||||
1334 | for (const auto &MA : *Accesses) | ||||||||||
1335 | if (const auto *MU = dyn_cast<MemoryUse>(&MA)) { | ||||||||||
1336 | auto *MD = MU->getDefiningAccess(); | ||||||||||
1337 | if (!MSSA->isLiveOnEntryDef(MD) && | ||||||||||
1338 | CurLoop->contains(MD->getBlock())) | ||||||||||
1339 | return false; | ||||||||||
1340 | // Disable hoisting past potentially interfering loads. Optimized | ||||||||||
1341 | // Uses may point to an access outside the loop, as getClobbering | ||||||||||
1342 | // checks the previous iteration when walking the backedge. | ||||||||||
1343 | // FIXME: More precise: no Uses that alias SI. | ||||||||||
1344 | if (!Flags->getIsSink() && !MSSA->dominates(SIMD, MU)) | ||||||||||
1345 | return false; | ||||||||||
1346 | } else if (const auto *MD = dyn_cast<MemoryDef>(&MA)) { | ||||||||||
1347 | if (auto *LI = dyn_cast<LoadInst>(MD->getMemoryInst())) { | ||||||||||
1348 | (void)LI; // Silence warning. | ||||||||||
1349 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1349, __PRETTY_FUNCTION__)); | ||||||||||
1350 | return false; | ||||||||||
1351 | } | ||||||||||
1352 | // Any call, while it may not be clobbering SI, it may be a use. | ||||||||||
1353 | if (auto *CI = dyn_cast<CallInst>(MD->getMemoryInst())) { | ||||||||||
1354 | // Check if the call may read from the memory locattion written | ||||||||||
1355 | // to by SI. Check CI's attributes and arguments; the number of | ||||||||||
1356 | // such checks performed is limited above by NoOfMemAccTooLarge. | ||||||||||
1357 | ModRefInfo MRI = AA->getModRefInfo(CI, MemoryLocation::get(SI)); | ||||||||||
1358 | if (isModOrRefSet(MRI)) | ||||||||||
1359 | return false; | ||||||||||
1360 | } | ||||||||||
1361 | } | ||||||||||
1362 | } | ||||||||||
1363 | auto *Source = MSSA->getSkipSelfWalker()->getClobberingMemoryAccess(SI); | ||||||||||
1364 | Flags->incrementClobberingCalls(); | ||||||||||
1365 | // If there are no clobbering Defs in the loop, store is safe to hoist. | ||||||||||
1366 | return MSSA->isLiveOnEntryDef(Source) || | ||||||||||
1367 | !CurLoop->contains(Source->getBlock()); | ||||||||||
1368 | } | ||||||||||
1369 | } | ||||||||||
1370 | |||||||||||
1371 | assert(!I.mayReadOrWriteMemory() && "unhandled aliasing")((!I.mayReadOrWriteMemory() && "unhandled aliasing") ? static_cast<void> (0) : __assert_fail ("!I.mayReadOrWriteMemory() && \"unhandled aliasing\"" , "/build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1371, __PRETTY_FUNCTION__)); | ||||||||||
1372 | |||||||||||
1373 | // We've established mechanical ability and aliasing, it's up to the caller | ||||||||||
1374 | // to check fault safety | ||||||||||
1375 | return true; | ||||||||||
1376 | } | ||||||||||
1377 | |||||||||||
1378 | /// Returns true if a PHINode is a trivially replaceable with an | ||||||||||
1379 | /// Instruction. | ||||||||||
1380 | /// This is true when all incoming values are that instruction. | ||||||||||
1381 | /// This pattern occurs most often with LCSSA PHI nodes. | ||||||||||
1382 | /// | ||||||||||
1383 | static bool isTriviallyReplaceablePHI(const PHINode &PN, const Instruction &I) { | ||||||||||
1384 | for (const Value *IncValue : PN.incoming_values()) | ||||||||||
1385 | if (IncValue != &I) | ||||||||||
1386 | return false; | ||||||||||
1387 | |||||||||||
1388 | return true; | ||||||||||
1389 | } | ||||||||||
1390 | |||||||||||
1391 | /// Return true if the instruction is free in the loop. | ||||||||||
1392 | static bool isFreeInLoop(const Instruction &I, const Loop *CurLoop, | ||||||||||
1393 | const TargetTransformInfo *TTI) { | ||||||||||
1394 | |||||||||||
1395 | if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) { | ||||||||||
1396 | if (TTI->getUserCost(GEP, TargetTransformInfo::TCK_SizeAndLatency) != | ||||||||||
1397 | TargetTransformInfo::TCC_Free) | ||||||||||
1398 | return false; | ||||||||||
1399 | // For a GEP, we cannot simply use getUserCost because currently it | ||||||||||
1400 | // optimistically assume that a GEP will fold into addressing mode | ||||||||||
1401 | // regardless of its users. | ||||||||||
1402 | const BasicBlock *BB = GEP->getParent(); | ||||||||||
1403 | for (const User *U : GEP->users()) { | ||||||||||
1404 | const Instruction *UI = cast<Instruction>(U); | ||||||||||
1405 | if (CurLoop->contains(UI) && | ||||||||||
1406 | (BB != UI->getParent() || | ||||||||||
1407 | (!isa<StoreInst>(UI) && !isa<LoadInst>(UI)))) | ||||||||||
1408 | return false; | ||||||||||
1409 | } | ||||||||||
1410 | return true; | ||||||||||
1411 | } else | ||||||||||
1412 | return TTI->getUserCost(&I, TargetTransformInfo::TCK_SizeAndLatency) == | ||||||||||
1413 | TargetTransformInfo::TCC_Free; | ||||||||||
1414 | } | ||||||||||
1415 | |||||||||||
1416 | /// Return true if the only users of this instruction are outside of | ||||||||||
1417 | /// the loop. If this is true, we can sink the instruction to the exit | ||||||||||
1418 | /// blocks of the loop. | ||||||||||
1419 | /// | ||||||||||
1420 | /// We also return true if the instruction could be folded away in lowering. | ||||||||||
1421 | /// (e.g., a GEP can be folded into a load as an addressing mode in the loop). | ||||||||||
1422 | static bool isNotUsedOrFreeInLoop(const Instruction &I, const Loop *CurLoop, | ||||||||||
1423 | const LoopSafetyInfo *SafetyInfo, | ||||||||||
1424 | TargetTransformInfo *TTI, bool &FreeInLoop) { | ||||||||||
1425 | const auto &BlockColors = SafetyInfo->getBlockColors(); | ||||||||||
1426 | bool IsFree = isFreeInLoop(I, CurLoop, TTI); | ||||||||||
1427 | for (const User *U : I.users()) { | ||||||||||
1428 | const Instruction *UI = cast<Instruction>(U); | ||||||||||
1429 | if (const PHINode *PN = dyn_cast<PHINode>(UI)) { | ||||||||||
1430 | const BasicBlock *BB = PN->getParent(); | ||||||||||
1431 | // We cannot sink uses in catchswitches. | ||||||||||
1432 | if (isa<CatchSwitchInst>(BB->getTerminator())) | ||||||||||
1433 | return false; | ||||||||||
1434 | |||||||||||
1435 | // We need to sink a callsite to a unique funclet. Avoid sinking if the | ||||||||||
1436 | // phi use is too muddled. | ||||||||||
1437 | if (isa<CallInst>(I)) | ||||||||||
1438 | if (!BlockColors.empty() && | ||||||||||
1439 | BlockColors.find(const_cast<BasicBlock *>(BB))->second.size() != 1) | ||||||||||
1440 | return false; | ||||||||||
1441 | } | ||||||||||
1442 | |||||||||||
1443 | if (CurLoop->contains(UI)) { | ||||||||||
1444 | if (IsFree) { | ||||||||||
1445 | FreeInLoop = true; | ||||||||||
1446 | continue; | ||||||||||
1447 | } | ||||||||||
1448 | return false; | ||||||||||
1449 | } | ||||||||||
1450 | } | ||||||||||
1451 | return true; | ||||||||||
1452 | } | ||||||||||
1453 | |||||||||||
1454 | static Instruction *cloneInstructionInExitBlock( | ||||||||||
1455 | Instruction &I, BasicBlock &ExitBlock, PHINode &PN, const LoopInfo *LI, | ||||||||||
1456 | const LoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU) { | ||||||||||
1457 | Instruction *New; | ||||||||||
1458 | if (auto *CI = dyn_cast<CallInst>(&I)) { | ||||||||||
1459 | const auto &BlockColors = SafetyInfo->getBlockColors(); | ||||||||||
1460 | |||||||||||
1461 | // Sinking call-sites need to be handled differently from other | ||||||||||
1462 | // instructions. The cloned call-site needs a funclet bundle operand | ||||||||||
1463 | // appropriate for its location in the CFG. | ||||||||||
1464 | SmallVector<OperandBundleDef, 1> OpBundles; | ||||||||||
1465 | for (unsigned BundleIdx = 0, BundleEnd = CI->getNumOperandBundles(); | ||||||||||
1466 | BundleIdx != BundleEnd; ++BundleIdx) { | ||||||||||
1467 | OperandBundleUse Bundle = CI->getOperandBundleAt(BundleIdx); | ||||||||||
1468 | if (Bundle.getTagID() == LLVMContext::OB_funclet) | ||||||||||
1469 | continue; | ||||||||||
1470 | |||||||||||
1471 | OpBundles.emplace_back(Bundle); | ||||||||||
1472 | } | ||||||||||
1473 | |||||||||||
1474 | if (!BlockColors.empty()) { | ||||||||||
1475 | const ColorVector &CV = BlockColors.find(&ExitBlock)->second; | ||||||||||
1476 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1476, __PRETTY_FUNCTION__)); | ||||||||||
1477 | BasicBlock *BBColor = CV.front(); | ||||||||||
1478 | Instruction *EHPad = BBColor->getFirstNonPHI(); | ||||||||||
1479 | if (EHPad->isEHPad()) | ||||||||||
1480 | OpBundles.emplace_back("funclet", EHPad); | ||||||||||
1481 | } | ||||||||||
1482 | |||||||||||
1483 | New = CallInst::Create(CI, OpBundles); | ||||||||||
1484 | } else { | ||||||||||
1485 | New = I.clone(); | ||||||||||
1486 | } | ||||||||||
1487 | |||||||||||
1488 | ExitBlock.getInstList().insert(ExitBlock.getFirstInsertionPt(), New); | ||||||||||
1489 | if (!I.getName().empty()) | ||||||||||
1490 | New->setName(I.getName() + ".le"); | ||||||||||
1491 | |||||||||||
1492 | if (MSSAU && MSSAU->getMemorySSA()->getMemoryAccess(&I)) { | ||||||||||
1493 | // Create a new MemoryAccess and let MemorySSA set its defining access. | ||||||||||
1494 | MemoryAccess *NewMemAcc = MSSAU->createMemoryAccessInBB( | ||||||||||
1495 | New, nullptr, New->getParent(), MemorySSA::Beginning); | ||||||||||
1496 | if (NewMemAcc) { | ||||||||||
1497 | if (auto *MemDef = dyn_cast<MemoryDef>(NewMemAcc)) | ||||||||||
1498 | MSSAU->insertDef(MemDef, /*RenameUses=*/true); | ||||||||||
1499 | else { | ||||||||||
1500 | auto *MemUse = cast<MemoryUse>(NewMemAcc); | ||||||||||
1501 | MSSAU->insertUse(MemUse, /*RenameUses=*/true); | ||||||||||
1502 | } | ||||||||||
1503 | } | ||||||||||
1504 | } | ||||||||||
1505 | |||||||||||
1506 | // Build LCSSA PHI nodes for any in-loop operands (if legal). Note that | ||||||||||
1507 | // this is particularly cheap because we can rip off the PHI node that we're | ||||||||||
1508 | // replacing for the number and blocks of the predecessors. | ||||||||||
1509 | // OPT: If this shows up in a profile, we can instead finish sinking all | ||||||||||
1510 | // invariant instructions, and then walk their operands to re-establish | ||||||||||
1511 | // LCSSA. That will eliminate creating PHI nodes just to nuke them when | ||||||||||
1512 | // sinking bottom-up. | ||||||||||
1513 | for (Use &Op : New->operands()) | ||||||||||
1514 | if (LI->wouldBeOutOfLoopUseRequiringLCSSA(Op.get(), PN.getParent())) { | ||||||||||
1515 | auto *OInst = cast<Instruction>(Op.get()); | ||||||||||
1516 | PHINode *OpPN = | ||||||||||
1517 | PHINode::Create(OInst->getType(), PN.getNumIncomingValues(), | ||||||||||
1518 | OInst->getName() + ".lcssa", &ExitBlock.front()); | ||||||||||
1519 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) | ||||||||||
1520 | OpPN->addIncoming(OInst, PN.getIncomingBlock(i)); | ||||||||||
1521 | Op = OpPN; | ||||||||||
1522 | } | ||||||||||
1523 | return New; | ||||||||||
1524 | } | ||||||||||
1525 | |||||||||||
1526 | static void eraseInstruction(Instruction &I, ICFLoopSafetyInfo &SafetyInfo, | ||||||||||
1527 | AliasSetTracker *AST, MemorySSAUpdater *MSSAU) { | ||||||||||
1528 | if (AST) | ||||||||||
1529 | AST->deleteValue(&I); | ||||||||||
1530 | if (MSSAU) | ||||||||||
1531 | MSSAU->removeMemoryAccess(&I); | ||||||||||
1532 | SafetyInfo.removeInstruction(&I); | ||||||||||
1533 | I.eraseFromParent(); | ||||||||||
1534 | } | ||||||||||
1535 | |||||||||||
1536 | static void moveInstructionBefore(Instruction &I, Instruction &Dest, | ||||||||||
1537 | ICFLoopSafetyInfo &SafetyInfo, | ||||||||||
1538 | MemorySSAUpdater *MSSAU, | ||||||||||
1539 | ScalarEvolution *SE) { | ||||||||||
1540 | SafetyInfo.removeInstruction(&I); | ||||||||||
1541 | SafetyInfo.insertInstructionTo(&I, Dest.getParent()); | ||||||||||
1542 | I.moveBefore(&Dest); | ||||||||||
1543 | if (MSSAU) | ||||||||||
1544 | if (MemoryUseOrDef *OldMemAcc = cast_or_null<MemoryUseOrDef>( | ||||||||||
1545 | MSSAU->getMemorySSA()->getMemoryAccess(&I))) | ||||||||||
1546 | MSSAU->moveToPlace(OldMemAcc, Dest.getParent(), | ||||||||||
1547 | MemorySSA::BeforeTerminator); | ||||||||||
1548 | if (SE) | ||||||||||
1549 | SE->forgetValue(&I); | ||||||||||
1550 | } | ||||||||||
1551 | |||||||||||
1552 | static Instruction *sinkThroughTriviallyReplaceablePHI( | ||||||||||
1553 | PHINode *TPN, Instruction *I, LoopInfo *LI, | ||||||||||
1554 | SmallDenseMap<BasicBlock *, Instruction *, 32> &SunkCopies, | ||||||||||
1555 | const LoopSafetyInfo *SafetyInfo, const Loop *CurLoop, | ||||||||||
1556 | MemorySSAUpdater *MSSAU) { | ||||||||||
1557 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1558, __PRETTY_FUNCTION__)) | ||||||||||
1558 | "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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1558, __PRETTY_FUNCTION__)); | ||||||||||
1559 | BasicBlock *ExitBlock = TPN->getParent(); | ||||||||||
1560 | Instruction *New; | ||||||||||
1561 | auto It = SunkCopies.find(ExitBlock); | ||||||||||
1562 | if (It != SunkCopies.end()) | ||||||||||
1563 | New = It->second; | ||||||||||
1564 | else | ||||||||||
1565 | New = SunkCopies[ExitBlock] = cloneInstructionInExitBlock( | ||||||||||
1566 | *I, *ExitBlock, *TPN, LI, SafetyInfo, MSSAU); | ||||||||||
1567 | return New; | ||||||||||
1568 | } | ||||||||||
1569 | |||||||||||
1570 | static bool canSplitPredecessors(PHINode *PN, LoopSafetyInfo *SafetyInfo) { | ||||||||||
1571 | BasicBlock *BB = PN->getParent(); | ||||||||||
1572 | if (!BB->canSplitPredecessors()) | ||||||||||
1573 | return false; | ||||||||||
1574 | // It's not impossible to split EHPad blocks, but if BlockColors already exist | ||||||||||
1575 | // it require updating BlockColors for all offspring blocks accordingly. By | ||||||||||
1576 | // skipping such corner case, we can make updating BlockColors after splitting | ||||||||||
1577 | // predecessor fairly simple. | ||||||||||
1578 | if (!SafetyInfo->getBlockColors().empty() && BB->getFirstNonPHI()->isEHPad()) | ||||||||||
1579 | return false; | ||||||||||
1580 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { | ||||||||||
1581 | BasicBlock *BBPred = *PI; | ||||||||||
1582 | if (isa<IndirectBrInst>(BBPred->getTerminator()) || | ||||||||||
1583 | isa<CallBrInst>(BBPred->getTerminator())) | ||||||||||
1584 | return false; | ||||||||||
1585 | } | ||||||||||
1586 | return true; | ||||||||||
1587 | } | ||||||||||
1588 | |||||||||||
1589 | static void splitPredecessorsOfLoopExit(PHINode *PN, DominatorTree *DT, | ||||||||||
1590 | LoopInfo *LI, const Loop *CurLoop, | ||||||||||
1591 | LoopSafetyInfo *SafetyInfo, | ||||||||||
1592 | MemorySSAUpdater *MSSAU) { | ||||||||||
1593 | #ifndef NDEBUG | ||||||||||
1594 | SmallVector<BasicBlock *, 32> ExitBlocks; | ||||||||||
1595 | CurLoop->getUniqueExitBlocks(ExitBlocks); | ||||||||||
1596 | SmallPtrSet<BasicBlock *, 32> ExitBlockSet(ExitBlocks.begin(), | ||||||||||
1597 | ExitBlocks.end()); | ||||||||||
1598 | #endif | ||||||||||
1599 | BasicBlock *ExitBB = PN->getParent(); | ||||||||||
1600 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1600, __PRETTY_FUNCTION__)); | ||||||||||
1601 | |||||||||||
1602 | // Split predecessors of the loop exit to make instructions in the loop are | ||||||||||
1603 | // exposed to exit blocks through trivially replaceable PHIs while keeping the | ||||||||||
1604 | // loop in the canonical form where each predecessor of each exit block should | ||||||||||
1605 | // be contained within the loop. For example, this will convert the loop below | ||||||||||
1606 | // from | ||||||||||
1607 | // | ||||||||||
1608 | // LB1: | ||||||||||
1609 | // %v1 = | ||||||||||
1610 | // br %LE, %LB2 | ||||||||||
1611 | // LB2: | ||||||||||
1612 | // %v2 = | ||||||||||
1613 | // br %LE, %LB1 | ||||||||||
1614 | // LE: | ||||||||||
1615 | // %p = phi [%v1, %LB1], [%v2, %LB2] <-- non-trivially replaceable | ||||||||||
1616 | // | ||||||||||
1617 | // to | ||||||||||
1618 | // | ||||||||||
1619 | // LB1: | ||||||||||
1620 | // %v1 = | ||||||||||
1621 | // br %LE.split, %LB2 | ||||||||||
1622 | // LB2: | ||||||||||
1623 | // %v2 = | ||||||||||
1624 | // br %LE.split2, %LB1 | ||||||||||
1625 | // LE.split: | ||||||||||
1626 | // %p1 = phi [%v1, %LB1] <-- trivially replaceable | ||||||||||
1627 | // br %LE | ||||||||||
1628 | // LE.split2: | ||||||||||
1629 | // %p2 = phi [%v2, %LB2] <-- trivially replaceable | ||||||||||
1630 | // br %LE | ||||||||||
1631 | // LE: | ||||||||||
1632 | // %p = phi [%p1, %LE.split], [%p2, %LE.split2] | ||||||||||
1633 | // | ||||||||||
1634 | const auto &BlockColors = SafetyInfo->getBlockColors(); | ||||||||||
1635 | SmallSetVector<BasicBlock *, 8> PredBBs(pred_begin(ExitBB), pred_end(ExitBB)); | ||||||||||
1636 | while (!PredBBs.empty()) { | ||||||||||
1637 | BasicBlock *PredBB = *PredBBs.begin(); | ||||||||||
1638 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1639, __PRETTY_FUNCTION__)) | ||||||||||
1639 | "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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1639, __PRETTY_FUNCTION__)); | ||||||||||
1640 | if (PN->getBasicBlockIndex(PredBB) >= 0) { | ||||||||||
1641 | BasicBlock *NewPred = SplitBlockPredecessors( | ||||||||||
1642 | ExitBB, PredBB, ".split.loop.exit", DT, LI, MSSAU, true); | ||||||||||
1643 | // Since we do not allow splitting EH-block with BlockColors in | ||||||||||
1644 | // canSplitPredecessors(), we can simply assign predecessor's color to | ||||||||||
1645 | // the new block. | ||||||||||
1646 | if (!BlockColors.empty()) | ||||||||||
1647 | // Grab a reference to the ColorVector to be inserted before getting the | ||||||||||
1648 | // reference to the vector we are copying because inserting the new | ||||||||||
1649 | // element in BlockColors might cause the map to be reallocated. | ||||||||||
1650 | SafetyInfo->copyColors(NewPred, PredBB); | ||||||||||
1651 | } | ||||||||||
1652 | PredBBs.remove(PredBB); | ||||||||||
1653 | } | ||||||||||
1654 | } | ||||||||||
1655 | |||||||||||
1656 | /// When an instruction is found to only be used outside of the loop, this | ||||||||||
1657 | /// function moves it to the exit blocks and patches up SSA form as needed. | ||||||||||
1658 | /// This method is guaranteed to remove the original instruction from its | ||||||||||
1659 | /// position, and may either delete it or move it to outside of the loop. | ||||||||||
1660 | /// | ||||||||||
1661 | static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT, | ||||||||||
1662 | BlockFrequencyInfo *BFI, const Loop *CurLoop, | ||||||||||
1663 | ICFLoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU, | ||||||||||
1664 | OptimizationRemarkEmitter *ORE) { | ||||||||||
1665 | bool Changed = false; | ||||||||||
1666 | LLVM_DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM sinking instruction: " << I << "\n"; } } while (false); | ||||||||||
1667 | |||||||||||
1668 | // Iterate over users to be ready for actual sinking. Replace users via | ||||||||||
1669 | // unreachable blocks with undef and make all user PHIs trivially replaceable. | ||||||||||
1670 | SmallPtrSet<Instruction *, 8> VisitedUsers; | ||||||||||
1671 | for (Value::user_iterator UI = I.user_begin(), UE = I.user_end(); UI != UE;) { | ||||||||||
1672 | auto *User = cast<Instruction>(*UI); | ||||||||||
1673 | Use &U = UI.getUse(); | ||||||||||
1674 | ++UI; | ||||||||||
1675 | |||||||||||
1676 | if (VisitedUsers.count(User) || CurLoop->contains(User)) | ||||||||||
1677 | continue; | ||||||||||
1678 | |||||||||||
1679 | if (!DT->isReachableFromEntry(User->getParent())) { | ||||||||||
1680 | U = UndefValue::get(I.getType()); | ||||||||||
1681 | Changed = true; | ||||||||||
1682 | continue; | ||||||||||
1683 | } | ||||||||||
1684 | |||||||||||
1685 | // The user must be a PHI node. | ||||||||||
1686 | PHINode *PN = cast<PHINode>(User); | ||||||||||
1687 | |||||||||||
1688 | // Surprisingly, instructions can be used outside of loops without any | ||||||||||
1689 | // exits. This can only happen in PHI nodes if the incoming block is | ||||||||||
1690 | // unreachable. | ||||||||||
1691 | BasicBlock *BB = PN->getIncomingBlock(U); | ||||||||||
1692 | if (!DT->isReachableFromEntry(BB)) { | ||||||||||
1693 | U = UndefValue::get(I.getType()); | ||||||||||
1694 | Changed = true; | ||||||||||
1695 | continue; | ||||||||||
1696 | } | ||||||||||
1697 | |||||||||||
1698 | VisitedUsers.insert(PN); | ||||||||||
1699 | if (isTriviallyReplaceablePHI(*PN, I)) | ||||||||||
1700 | continue; | ||||||||||
1701 | |||||||||||
1702 | if (!canSplitPredecessors(PN, SafetyInfo)) | ||||||||||
1703 | return Changed; | ||||||||||
1704 | |||||||||||
1705 | // Split predecessors of the PHI so that we can make users trivially | ||||||||||
1706 | // replaceable. | ||||||||||
1707 | splitPredecessorsOfLoopExit(PN, DT, LI, CurLoop, SafetyInfo, MSSAU); | ||||||||||
1708 | |||||||||||
1709 | // Should rebuild the iterators, as they may be invalidated by | ||||||||||
1710 | // splitPredecessorsOfLoopExit(). | ||||||||||
1711 | UI = I.user_begin(); | ||||||||||
1712 | UE = I.user_end(); | ||||||||||
1713 | } | ||||||||||
1714 | |||||||||||
1715 | if (VisitedUsers.empty()) | ||||||||||
1716 | return Changed; | ||||||||||
1717 | |||||||||||
1718 | ORE->emit([&]() { | ||||||||||
1719 | return OptimizationRemark(DEBUG_TYPE"licm", "InstSunk", &I) | ||||||||||
1720 | << "sinking " << ore::NV("Inst", &I); | ||||||||||
1721 | }); | ||||||||||
1722 | if (isa<LoadInst>(I)) | ||||||||||
1723 | ++NumMovedLoads; | ||||||||||
1724 | else if (isa<CallInst>(I)) | ||||||||||
1725 | ++NumMovedCalls; | ||||||||||
1726 | ++NumSunk; | ||||||||||
1727 | |||||||||||
1728 | #ifndef NDEBUG | ||||||||||
1729 | SmallVector<BasicBlock *, 32> ExitBlocks; | ||||||||||
1730 | CurLoop->getUniqueExitBlocks(ExitBlocks); | ||||||||||
1731 | SmallPtrSet<BasicBlock *, 32> ExitBlockSet(ExitBlocks.begin(), | ||||||||||
1732 | ExitBlocks.end()); | ||||||||||
1733 | #endif | ||||||||||
1734 | |||||||||||
1735 | // Clones of this instruction. Don't create more than one per exit block! | ||||||||||
1736 | SmallDenseMap<BasicBlock *, Instruction *, 32> SunkCopies; | ||||||||||
1737 | |||||||||||
1738 | // If this instruction is only used outside of the loop, then all users are | ||||||||||
1739 | // PHI nodes in exit blocks due to LCSSA form. Just RAUW them with clones of | ||||||||||
1740 | // the instruction. | ||||||||||
1741 | // First check if I is worth sinking for all uses. Sink only when it is worth | ||||||||||
1742 | // across all uses. | ||||||||||
1743 | SmallSetVector<User*, 8> Users(I.user_begin(), I.user_end()); | ||||||||||
1744 | SmallVector<PHINode *, 8> ExitPNs; | ||||||||||
1745 | for (auto *UI : Users) { | ||||||||||
1746 | auto *User = cast<Instruction>(UI); | ||||||||||
1747 | |||||||||||
1748 | if (CurLoop->contains(User)) | ||||||||||
1749 | continue; | ||||||||||
1750 | |||||||||||
1751 | PHINode *PN = cast<PHINode>(User); | ||||||||||
1752 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1753, __PRETTY_FUNCTION__)) | ||||||||||
1753 | "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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1753, __PRETTY_FUNCTION__)); | ||||||||||
1754 | if (!worthSinkOrHoistInst(I, PN->getParent(), ORE, BFI)) { | ||||||||||
1755 | return Changed; | ||||||||||
1756 | } | ||||||||||
1757 | |||||||||||
1758 | ExitPNs.push_back(PN); | ||||||||||
1759 | } | ||||||||||
1760 | |||||||||||
1761 | for (auto *PN : ExitPNs) { | ||||||||||
1762 | |||||||||||
1763 | // The PHI must be trivially replaceable. | ||||||||||
1764 | Instruction *New = sinkThroughTriviallyReplaceablePHI( | ||||||||||
1765 | PN, &I, LI, SunkCopies, SafetyInfo, CurLoop, MSSAU); | ||||||||||
1766 | PN->replaceAllUsesWith(New); | ||||||||||
1767 | eraseInstruction(*PN, *SafetyInfo, nullptr, nullptr); | ||||||||||
1768 | Changed = true; | ||||||||||
1769 | } | ||||||||||
1770 | return Changed; | ||||||||||
1771 | } | ||||||||||
1772 | |||||||||||
1773 | /// When an instruction is found to only use loop invariant operands that | ||||||||||
1774 | /// is safe to hoist, this instruction is called to do the dirty work. | ||||||||||
1775 | /// | ||||||||||
1776 | static void hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop, | ||||||||||
1777 | BasicBlock *Dest, ICFLoopSafetyInfo *SafetyInfo, | ||||||||||
1778 | MemorySSAUpdater *MSSAU, ScalarEvolution *SE, | ||||||||||
1779 | OptimizationRemarkEmitter *ORE) { | ||||||||||
1780 | 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) | ||||||||||
1781 | << I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM hoisting to " << Dest ->getNameOrAsOperand() << ": " << I << "\n" ; } } while (false); | ||||||||||
1782 | ORE->emit([&]() { | ||||||||||
1783 | return OptimizationRemark(DEBUG_TYPE"licm", "Hoisted", &I) << "hoisting " | ||||||||||
1784 | << ore::NV("Inst", &I); | ||||||||||
1785 | }); | ||||||||||
1786 | |||||||||||
1787 | // Metadata can be dependent on conditions we are hoisting above. | ||||||||||
1788 | // Conservatively strip all metadata on the instruction unless we were | ||||||||||
1789 | // guaranteed to execute I if we entered the loop, in which case the metadata | ||||||||||
1790 | // is valid in the loop preheader. | ||||||||||
1791 | if (I.hasMetadataOtherThanDebugLoc() && | ||||||||||
1792 | // The check on hasMetadataOtherThanDebugLoc is to prevent us from burning | ||||||||||
1793 | // time in isGuaranteedToExecute if we don't actually have anything to | ||||||||||
1794 | // drop. It is a compile time optimization, not required for correctness. | ||||||||||
1795 | !SafetyInfo->isGuaranteedToExecute(I, DT, CurLoop)) | ||||||||||
1796 | I.dropUnknownNonDebugMetadata(); | ||||||||||
1797 | |||||||||||
1798 | if (isa<PHINode>(I)) | ||||||||||
1799 | // Move the new node to the end of the phi list in the destination block. | ||||||||||
1800 | moveInstructionBefore(I, *Dest->getFirstNonPHI(), *SafetyInfo, MSSAU, SE); | ||||||||||
1801 | else | ||||||||||
1802 | // Move the new node to the destination block, before its terminator. | ||||||||||
1803 | moveInstructionBefore(I, *Dest->getTerminator(), *SafetyInfo, MSSAU, SE); | ||||||||||
1804 | |||||||||||
1805 | I.updateLocationAfterHoist(); | ||||||||||
1806 | |||||||||||
1807 | if (isa<LoadInst>(I)) | ||||||||||
1808 | ++NumMovedLoads; | ||||||||||
1809 | else if (isa<CallInst>(I)) | ||||||||||
1810 | ++NumMovedCalls; | ||||||||||
1811 | ++NumHoisted; | ||||||||||
1812 | } | ||||||||||
1813 | |||||||||||
1814 | /// Only sink or hoist an instruction if it is not a trapping instruction, | ||||||||||
1815 | /// or if the instruction is known not to trap when moved to the preheader. | ||||||||||
1816 | /// or if it is a trapping instruction and is guaranteed to execute. | ||||||||||
1817 | static bool isSafeToExecuteUnconditionally(Instruction &Inst, | ||||||||||
1818 | const DominatorTree *DT, | ||||||||||
1819 | const TargetLibraryInfo *TLI, | ||||||||||
1820 | const Loop *CurLoop, | ||||||||||
1821 | const LoopSafetyInfo *SafetyInfo, | ||||||||||
1822 | OptimizationRemarkEmitter *ORE, | ||||||||||
1823 | const Instruction *CtxI) { | ||||||||||
1824 | if (isSafeToSpeculativelyExecute(&Inst, CtxI, DT, TLI)) | ||||||||||
1825 | return true; | ||||||||||
1826 | |||||||||||
1827 | bool GuaranteedToExecute = | ||||||||||
1828 | SafetyInfo->isGuaranteedToExecute(Inst, DT, CurLoop); | ||||||||||
1829 | |||||||||||
1830 | if (!GuaranteedToExecute) { | ||||||||||
1831 | auto *LI = dyn_cast<LoadInst>(&Inst); | ||||||||||
1832 | if (LI && CurLoop->isLoopInvariant(LI->getPointerOperand())) | ||||||||||
1833 | ORE->emit([&]() { | ||||||||||
1834 | return OptimizationRemarkMissed( | ||||||||||
1835 | DEBUG_TYPE"licm", "LoadWithLoopInvariantAddressCondExecuted", LI) | ||||||||||
1836 | << "failed to hoist load with loop-invariant address " | ||||||||||
1837 | "because load is conditionally executed"; | ||||||||||
1838 | }); | ||||||||||
1839 | } | ||||||||||
1840 | |||||||||||
1841 | return GuaranteedToExecute; | ||||||||||
1842 | } | ||||||||||
1843 | |||||||||||
1844 | namespace { | ||||||||||
1845 | class LoopPromoter : public LoadAndStorePromoter { | ||||||||||
1846 | Value *SomePtr; // Designated pointer to store to. | ||||||||||
1847 | const SmallSetVector<Value *, 8> &PointerMustAliases; | ||||||||||
1848 | SmallVectorImpl<BasicBlock *> &LoopExitBlocks; | ||||||||||
1849 | SmallVectorImpl<Instruction *> &LoopInsertPts; | ||||||||||
1850 | SmallVectorImpl<MemoryAccess *> &MSSAInsertPts; | ||||||||||
1851 | PredIteratorCache &PredCache; | ||||||||||
1852 | AliasSetTracker *AST; | ||||||||||
1853 | MemorySSAUpdater *MSSAU; | ||||||||||
1854 | LoopInfo &LI; | ||||||||||
1855 | DebugLoc DL; | ||||||||||
1856 | int Alignment; | ||||||||||
1857 | bool UnorderedAtomic; | ||||||||||
1858 | AAMDNodes AATags; | ||||||||||
1859 | ICFLoopSafetyInfo &SafetyInfo; | ||||||||||
1860 | |||||||||||
1861 | // We're about to add a use of V in a loop exit block. Insert an LCSSA phi | ||||||||||
1862 | // (if legal) if doing so would add an out-of-loop use to an instruction | ||||||||||
1863 | // defined in-loop. | ||||||||||
1864 | Value *maybeInsertLCSSAPHI(Value *V, BasicBlock *BB) const { | ||||||||||
1865 | if (!LI.wouldBeOutOfLoopUseRequiringLCSSA(V, BB)) | ||||||||||
1866 | return V; | ||||||||||
1867 | |||||||||||
1868 | Instruction *I = cast<Instruction>(V); | ||||||||||
1869 | // We need to create an LCSSA PHI node for the incoming value and | ||||||||||
1870 | // store that. | ||||||||||
1871 | PHINode *PN = PHINode::Create(I->getType(), PredCache.size(BB), | ||||||||||
1872 | I->getName() + ".lcssa", &BB->front()); | ||||||||||
1873 | for (BasicBlock *Pred : PredCache.get(BB)) | ||||||||||
1874 | PN->addIncoming(I, Pred); | ||||||||||
1875 | return PN; | ||||||||||
1876 | } | ||||||||||
1877 | |||||||||||
1878 | public: | ||||||||||
1879 | LoopPromoter(Value *SP, ArrayRef<const Instruction *> Insts, SSAUpdater &S, | ||||||||||
1880 | const SmallSetVector<Value *, 8> &PMA, | ||||||||||
1881 | SmallVectorImpl<BasicBlock *> &LEB, | ||||||||||
1882 | SmallVectorImpl<Instruction *> &LIP, | ||||||||||
1883 | SmallVectorImpl<MemoryAccess *> &MSSAIP, PredIteratorCache &PIC, | ||||||||||
1884 | AliasSetTracker *ast, MemorySSAUpdater *MSSAU, LoopInfo &li, | ||||||||||
1885 | DebugLoc dl, int alignment, bool UnorderedAtomic, | ||||||||||
1886 | const AAMDNodes &AATags, ICFLoopSafetyInfo &SafetyInfo) | ||||||||||
1887 | : LoadAndStorePromoter(Insts, S), SomePtr(SP), PointerMustAliases(PMA), | ||||||||||
1888 | LoopExitBlocks(LEB), LoopInsertPts(LIP), MSSAInsertPts(MSSAIP), | ||||||||||
1889 | PredCache(PIC), AST(ast), MSSAU(MSSAU), LI(li), DL(std::move(dl)), | ||||||||||
1890 | Alignment(alignment), UnorderedAtomic(UnorderedAtomic), AATags(AATags), | ||||||||||
1891 | SafetyInfo(SafetyInfo) {} | ||||||||||
1892 | |||||||||||
1893 | bool isInstInList(Instruction *I, | ||||||||||
1894 | const SmallVectorImpl<Instruction *> &) const override { | ||||||||||
1895 | Value *Ptr; | ||||||||||
1896 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) | ||||||||||
1897 | Ptr = LI->getOperand(0); | ||||||||||
1898 | else | ||||||||||
1899 | Ptr = cast<StoreInst>(I)->getPointerOperand(); | ||||||||||
1900 | return PointerMustAliases.count(Ptr); | ||||||||||
1901 | } | ||||||||||
1902 | |||||||||||
1903 | void doExtraRewritesBeforeFinalDeletion() override { | ||||||||||
1904 | // Insert stores after in the loop exit blocks. Each exit block gets a | ||||||||||
1905 | // store of the live-out values that feed them. Since we've already told | ||||||||||
1906 | // the SSA updater about the defs in the loop and the preheader | ||||||||||
1907 | // definition, it is all set and we can start using it. | ||||||||||
1908 | for (unsigned i = 0, e = LoopExitBlocks.size(); i != e; ++i) { | ||||||||||
1909 | BasicBlock *ExitBlock = LoopExitBlocks[i]; | ||||||||||
1910 | Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock); | ||||||||||
1911 | LiveInValue = maybeInsertLCSSAPHI(LiveInValue, ExitBlock); | ||||||||||
1912 | Value *Ptr = maybeInsertLCSSAPHI(SomePtr, ExitBlock); | ||||||||||
1913 | Instruction *InsertPos = LoopInsertPts[i]; | ||||||||||
1914 | StoreInst *NewSI = new StoreInst(LiveInValue, Ptr, InsertPos); | ||||||||||
1915 | if (UnorderedAtomic) | ||||||||||
1916 | NewSI->setOrdering(AtomicOrdering::Unordered); | ||||||||||
1917 | NewSI->setAlignment(Align(Alignment)); | ||||||||||
1918 | NewSI->setDebugLoc(DL); | ||||||||||
1919 | if (AATags) | ||||||||||
1920 | NewSI->setAAMetadata(AATags); | ||||||||||
1921 | |||||||||||
1922 | if (MSSAU) { | ||||||||||
1923 | MemoryAccess *MSSAInsertPoint = MSSAInsertPts[i]; | ||||||||||
1924 | MemoryAccess *NewMemAcc; | ||||||||||
1925 | if (!MSSAInsertPoint) { | ||||||||||
1926 | NewMemAcc = MSSAU->createMemoryAccessInBB( | ||||||||||
1927 | NewSI, nullptr, NewSI->getParent(), MemorySSA::Beginning); | ||||||||||
1928 | } else { | ||||||||||
1929 | NewMemAcc = | ||||||||||
1930 | MSSAU->createMemoryAccessAfter(NewSI, nullptr, MSSAInsertPoint); | ||||||||||
1931 | } | ||||||||||
1932 | MSSAInsertPts[i] = NewMemAcc; | ||||||||||
1933 | MSSAU->insertDef(cast<MemoryDef>(NewMemAcc), true); | ||||||||||
1934 | // FIXME: true for safety, false may still be correct. | ||||||||||
1935 | } | ||||||||||
1936 | } | ||||||||||
1937 | } | ||||||||||
1938 | |||||||||||
1939 | void replaceLoadWithValue(LoadInst *LI, Value *V) const override { | ||||||||||
1940 | // Update alias analysis. | ||||||||||
1941 | if (AST) | ||||||||||
1942 | AST->copyValue(LI, V); | ||||||||||
1943 | } | ||||||||||
1944 | void instructionDeleted(Instruction *I) const override { | ||||||||||
1945 | SafetyInfo.removeInstruction(I); | ||||||||||
1946 | if (AST) | ||||||||||
1947 | AST->deleteValue(I); | ||||||||||
1948 | if (MSSAU) | ||||||||||
1949 | MSSAU->removeMemoryAccess(I); | ||||||||||
1950 | } | ||||||||||
1951 | }; | ||||||||||
1952 | |||||||||||
1953 | |||||||||||
1954 | /// Return true iff we can prove that a caller of this function can not inspect | ||||||||||
1955 | /// the contents of the provided object in a well defined program. | ||||||||||
1956 | bool isKnownNonEscaping(Value *Object, const TargetLibraryInfo *TLI) { | ||||||||||
1957 | if (isa<AllocaInst>(Object)) | ||||||||||
1958 | // Since the alloca goes out of scope, we know the caller can't retain a | ||||||||||
1959 | // reference to it and be well defined. Thus, we don't need to check for | ||||||||||
1960 | // capture. | ||||||||||
1961 | return true; | ||||||||||
1962 | |||||||||||
1963 | // For all other objects we need to know that the caller can't possibly | ||||||||||
1964 | // have gotten a reference to the object. There are two components of | ||||||||||
1965 | // that: | ||||||||||
1966 | // 1) Object can't be escaped by this function. This is what | ||||||||||
1967 | // PointerMayBeCaptured checks. | ||||||||||
1968 | // 2) Object can't have been captured at definition site. For this, we | ||||||||||
1969 | // need to know the return value is noalias. At the moment, we use a | ||||||||||
1970 | // weaker condition and handle only AllocLikeFunctions (which are | ||||||||||
1971 | // known to be noalias). TODO | ||||||||||
1972 | return isAllocLikeFn(Object, TLI) && | ||||||||||
1973 | !PointerMayBeCaptured(Object, true, true); | ||||||||||
1974 | } | ||||||||||
1975 | |||||||||||
1976 | } // namespace | ||||||||||
1977 | |||||||||||
1978 | /// Try to promote memory values to scalars by sinking stores out of the | ||||||||||
1979 | /// loop and moving loads to before the loop. We do this by looping over | ||||||||||
1980 | /// the stores in the loop, looking for stores to Must pointers which are | ||||||||||
1981 | /// loop invariant. | ||||||||||
1982 | /// | ||||||||||
1983 | bool llvm::promoteLoopAccessesToScalars( | ||||||||||
1984 | const SmallSetVector<Value *, 8> &PointerMustAliases, | ||||||||||
1985 | SmallVectorImpl<BasicBlock *> &ExitBlocks, | ||||||||||
1986 | SmallVectorImpl<Instruction *> &InsertPts, | ||||||||||
1987 | SmallVectorImpl<MemoryAccess *> &MSSAInsertPts, PredIteratorCache &PIC, | ||||||||||
1988 | LoopInfo *LI, DominatorTree *DT, const TargetLibraryInfo *TLI, | ||||||||||
1989 | Loop *CurLoop, AliasSetTracker *CurAST, MemorySSAUpdater *MSSAU, | ||||||||||
1990 | ICFLoopSafetyInfo *SafetyInfo, OptimizationRemarkEmitter *ORE) { | ||||||||||
1991 | // Verify inputs. | ||||||||||
1992 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1994, __PRETTY_FUNCTION__)) | ||||||||||
1993 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1994, __PRETTY_FUNCTION__)) | ||||||||||
1994 | "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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 1994, __PRETTY_FUNCTION__)); | ||||||||||
1995 | |||||||||||
1996 | Value *SomePtr = *PointerMustAliases.begin(); | ||||||||||
1997 | BasicBlock *Preheader = CurLoop->getLoopPreheader(); | ||||||||||
1998 | |||||||||||
1999 | // It is not safe to promote a load/store from the loop if the load/store is | ||||||||||
2000 | // conditional. For example, turning: | ||||||||||
2001 | // | ||||||||||
2002 | // for () { if (c) *P += 1; } | ||||||||||
2003 | // | ||||||||||
2004 | // into: | ||||||||||
2005 | // | ||||||||||
2006 | // tmp = *P; for () { if (c) tmp +=1; } *P = tmp; | ||||||||||
2007 | // | ||||||||||
2008 | // is not safe, because *P may only be valid to access if 'c' is true. | ||||||||||
2009 | // | ||||||||||
2010 | // The safety property divides into two parts: | ||||||||||
2011 | // p1) The memory may not be dereferenceable on entry to the loop. In this | ||||||||||
2012 | // case, we can't insert the required load in the preheader. | ||||||||||
2013 | // p2) The memory model does not allow us to insert a store along any dynamic | ||||||||||
2014 | // path which did not originally have one. | ||||||||||
2015 | // | ||||||||||
2016 | // If at least one store is guaranteed to execute, both properties are | ||||||||||
2017 | // satisfied, and promotion is legal. | ||||||||||
2018 | // | ||||||||||
2019 | // This, however, is not a necessary condition. Even if no store/load is | ||||||||||
2020 | // guaranteed to execute, we can still establish these properties. | ||||||||||
2021 | // We can establish (p1) by proving that hoisting the load into the preheader | ||||||||||
2022 | // is safe (i.e. proving dereferenceability on all paths through the loop). We | ||||||||||
2023 | // can use any access within the alias set to prove dereferenceability, | ||||||||||
2024 | // since they're all must alias. | ||||||||||
2025 | // | ||||||||||
2026 | // There are two ways establish (p2): | ||||||||||
2027 | // a) Prove the location is thread-local. In this case the memory model | ||||||||||
2028 | // requirement does not apply, and stores are safe to insert. | ||||||||||
2029 | // b) Prove a store dominates every exit block. In this case, if an exit | ||||||||||
2030 | // blocks is reached, the original dynamic path would have taken us through | ||||||||||
2031 | // the store, so inserting a store into the exit block is safe. Note that this | ||||||||||
2032 | // is different from the store being guaranteed to execute. For instance, | ||||||||||
2033 | // if an exception is thrown on the first iteration of the loop, the original | ||||||||||
2034 | // store is never executed, but the exit blocks are not executed either. | ||||||||||
2035 | |||||||||||
2036 | bool DereferenceableInPH = false; | ||||||||||
2037 | bool SafeToInsertStore = false; | ||||||||||
2038 | |||||||||||
2039 | SmallVector<Instruction *, 64> LoopUses; | ||||||||||
2040 | |||||||||||
2041 | // We start with an alignment of one and try to find instructions that allow | ||||||||||
2042 | // us to prove better alignment. | ||||||||||
2043 | Align Alignment; | ||||||||||
2044 | // Keep track of which types of access we see | ||||||||||
2045 | bool SawUnorderedAtomic = false; | ||||||||||
2046 | bool SawNotAtomic = false; | ||||||||||
2047 | AAMDNodes AATags; | ||||||||||
2048 | |||||||||||
2049 | const DataLayout &MDL = Preheader->getModule()->getDataLayout(); | ||||||||||
2050 | |||||||||||
2051 | bool IsKnownThreadLocalObject = false; | ||||||||||
2052 | if (SafetyInfo->anyBlockMayThrow()) { | ||||||||||
2053 | // If a loop can throw, we have to insert a store along each unwind edge. | ||||||||||
2054 | // That said, we can't actually make the unwind edge explicit. Therefore, | ||||||||||
2055 | // we have to prove that the store is dead along the unwind edge. We do | ||||||||||
2056 | // this by proving that the caller can't have a reference to the object | ||||||||||
2057 | // after return and thus can't possibly load from the object. | ||||||||||
2058 | Value *Object = getUnderlyingObject(SomePtr); | ||||||||||
2059 | if (!isKnownNonEscaping(Object, TLI)) | ||||||||||
2060 | return false; | ||||||||||
2061 | // Subtlety: Alloca's aren't visible to callers, but *are* potentially | ||||||||||
2062 | // visible to other threads if captured and used during their lifetimes. | ||||||||||
2063 | IsKnownThreadLocalObject = !isa<AllocaInst>(Object); | ||||||||||
2064 | } | ||||||||||
2065 | |||||||||||
2066 | // Check that all of the pointers in the alias set have the same type. We | ||||||||||
2067 | // cannot (yet) promote a memory location that is loaded and stored in | ||||||||||
2068 | // different sizes. While we are at it, collect alignment and AA info. | ||||||||||
2069 | for (Value *ASIV : PointerMustAliases) { | ||||||||||
2070 | // Check that all of the pointers in the alias set have the same type. We | ||||||||||
2071 | // cannot (yet) promote a memory location that is loaded and stored in | ||||||||||
2072 | // different sizes. | ||||||||||
2073 | if (SomePtr->getType() != ASIV->getType()) | ||||||||||
2074 | return false; | ||||||||||
2075 | |||||||||||
2076 | for (User *U : ASIV->users()) { | ||||||||||
2077 | // Ignore instructions that are outside the loop. | ||||||||||
2078 | Instruction *UI = dyn_cast<Instruction>(U); | ||||||||||
2079 | if (!UI || !CurLoop->contains(UI)) | ||||||||||
2080 | continue; | ||||||||||
2081 | |||||||||||
2082 | // If there is an non-load/store instruction in the loop, we can't promote | ||||||||||
2083 | // it. | ||||||||||
2084 | if (LoadInst *Load = dyn_cast<LoadInst>(UI)) { | ||||||||||
2085 | if (!Load->isUnordered()) | ||||||||||
2086 | return false; | ||||||||||
2087 | |||||||||||
2088 | SawUnorderedAtomic |= Load->isAtomic(); | ||||||||||
2089 | SawNotAtomic |= !Load->isAtomic(); | ||||||||||
2090 | |||||||||||
2091 | Align InstAlignment = Load->getAlign(); | ||||||||||
2092 | |||||||||||
2093 | // Note that proving a load safe to speculate requires proving | ||||||||||
2094 | // sufficient alignment at the target location. Proving it guaranteed | ||||||||||
2095 | // to execute does as well. Thus we can increase our guaranteed | ||||||||||
2096 | // alignment as well. | ||||||||||
2097 | if (!DereferenceableInPH || (InstAlignment > Alignment)) | ||||||||||
2098 | if (isSafeToExecuteUnconditionally(*Load, DT, TLI, CurLoop, | ||||||||||
2099 | SafetyInfo, ORE, | ||||||||||
2100 | Preheader->getTerminator())) { | ||||||||||
2101 | DereferenceableInPH = true; | ||||||||||
2102 | Alignment = std::max(Alignment, InstAlignment); | ||||||||||
2103 | } | ||||||||||
2104 | } else if (const StoreInst *Store = dyn_cast<StoreInst>(UI)) { | ||||||||||
2105 | // Stores *of* the pointer are not interesting, only stores *to* the | ||||||||||
2106 | // pointer. | ||||||||||
2107 | if (UI->getOperand(1) != ASIV) | ||||||||||
2108 | continue; | ||||||||||
2109 | if (!Store->isUnordered()) | ||||||||||
2110 | return false; | ||||||||||
2111 | |||||||||||
2112 | SawUnorderedAtomic |= Store->isAtomic(); | ||||||||||
2113 | SawNotAtomic |= !Store->isAtomic(); | ||||||||||
2114 | |||||||||||
2115 | // If the store is guaranteed to execute, both properties are satisfied. | ||||||||||
2116 | // We may want to check if a store is guaranteed to execute even if we | ||||||||||
2117 | // already know that promotion is safe, since it may have higher | ||||||||||
2118 | // alignment than any other guaranteed stores, in which case we can | ||||||||||
2119 | // raise the alignment on the promoted store. | ||||||||||
2120 | Align InstAlignment = Store->getAlign(); | ||||||||||
2121 | |||||||||||
2122 | if (!DereferenceableInPH || !SafeToInsertStore || | ||||||||||
2123 | (InstAlignment > Alignment)) { | ||||||||||
2124 | if (SafetyInfo->isGuaranteedToExecute(*UI, DT, CurLoop)) { | ||||||||||
2125 | DereferenceableInPH = true; | ||||||||||
2126 | SafeToInsertStore = true; | ||||||||||
2127 | Alignment = std::max(Alignment, InstAlignment); | ||||||||||
2128 | } | ||||||||||
2129 | } | ||||||||||
2130 | |||||||||||
2131 | // If a store dominates all exit blocks, it is safe to sink. | ||||||||||
2132 | // As explained above, if an exit block was executed, a dominating | ||||||||||
2133 | // store must have been executed at least once, so we are not | ||||||||||
2134 | // introducing stores on paths that did not have them. | ||||||||||
2135 | // Note that this only looks at explicit exit blocks. If we ever | ||||||||||
2136 | // start sinking stores into unwind edges (see above), this will break. | ||||||||||
2137 | if (!SafeToInsertStore) | ||||||||||
2138 | SafeToInsertStore = llvm::all_of(ExitBlocks, [&](BasicBlock *Exit) { | ||||||||||
2139 | return DT->dominates(Store->getParent(), Exit); | ||||||||||
2140 | }); | ||||||||||
2141 | |||||||||||
2142 | // If the store is not guaranteed to execute, we may still get | ||||||||||
2143 | // deref info through it. | ||||||||||
2144 | if (!DereferenceableInPH) { | ||||||||||
2145 | DereferenceableInPH = isDereferenceableAndAlignedPointer( | ||||||||||
2146 | Store->getPointerOperand(), Store->getValueOperand()->getType(), | ||||||||||
2147 | Store->getAlign(), MDL, Preheader->getTerminator(), DT, TLI); | ||||||||||
2148 | } | ||||||||||
2149 | } else | ||||||||||
2150 | return false; // Not a load or store. | ||||||||||
2151 | |||||||||||
2152 | // Merge the AA tags. | ||||||||||
2153 | if (LoopUses.empty()) { | ||||||||||
2154 | // On the first load/store, just take its AA tags. | ||||||||||
2155 | UI->getAAMetadata(AATags); | ||||||||||
2156 | } else if (AATags) { | ||||||||||
2157 | UI->getAAMetadata(AATags, /* Merge = */ true); | ||||||||||
2158 | } | ||||||||||
2159 | |||||||||||
2160 | LoopUses.push_back(UI); | ||||||||||
2161 | } | ||||||||||
2162 | } | ||||||||||
2163 | |||||||||||
2164 | // If we found both an unordered atomic instruction and a non-atomic memory | ||||||||||
2165 | // access, bail. We can't blindly promote non-atomic to atomic since we | ||||||||||
2166 | // might not be able to lower the result. We can't downgrade since that | ||||||||||
2167 | // would violate memory model. Also, align 0 is an error for atomics. | ||||||||||
2168 | if (SawUnorderedAtomic && SawNotAtomic) | ||||||||||
2169 | return false; | ||||||||||
2170 | |||||||||||
2171 | // If we're inserting an atomic load in the preheader, we must be able to | ||||||||||
2172 | // lower it. We're only guaranteed to be able to lower naturally aligned | ||||||||||
2173 | // atomics. | ||||||||||
2174 | auto *SomePtrElemType = SomePtr->getType()->getPointerElementType(); | ||||||||||
2175 | if (SawUnorderedAtomic && | ||||||||||
2176 | Alignment < MDL.getTypeStoreSize(SomePtrElemType)) | ||||||||||
2177 | return false; | ||||||||||
2178 | |||||||||||
2179 | // If we couldn't prove we can hoist the load, bail. | ||||||||||
2180 | if (!DereferenceableInPH) | ||||||||||
2181 | return false; | ||||||||||
2182 | |||||||||||
2183 | // We know we can hoist the load, but don't have a guaranteed store. | ||||||||||
2184 | // Check whether the location is thread-local. If it is, then we can insert | ||||||||||
2185 | // stores along paths which originally didn't have them without violating the | ||||||||||
2186 | // memory model. | ||||||||||
2187 | if (!SafeToInsertStore) { | ||||||||||
2188 | if (IsKnownThreadLocalObject) | ||||||||||
2189 | SafeToInsertStore = true; | ||||||||||
2190 | else { | ||||||||||
2191 | Value *Object = getUnderlyingObject(SomePtr); | ||||||||||
2192 | SafeToInsertStore = | ||||||||||
2193 | (isAllocLikeFn(Object, TLI) || isa<AllocaInst>(Object)) && | ||||||||||
2194 | !PointerMayBeCaptured(Object, true, true); | ||||||||||
2195 | } | ||||||||||
2196 | } | ||||||||||
2197 | |||||||||||
2198 | // If we've still failed to prove we can sink the store, give up. | ||||||||||
2199 | if (!SafeToInsertStore) | ||||||||||
2200 | return false; | ||||||||||
2201 | |||||||||||
2202 | // Otherwise, this is safe to promote, lets do it! | ||||||||||
2203 | 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) | ||||||||||
2204 | << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM: Promoting value stored to in loop: " << *SomePtr << '\n'; } } while (false); | ||||||||||
2205 | ORE->emit([&]() { | ||||||||||
2206 | return OptimizationRemark(DEBUG_TYPE"licm", "PromoteLoopAccessesToScalar", | ||||||||||
2207 | LoopUses[0]) | ||||||||||
2208 | << "Moving accesses to memory location out of the loop"; | ||||||||||
2209 | }); | ||||||||||
2210 | ++NumPromoted; | ||||||||||
2211 | |||||||||||
2212 | // Look at all the loop uses, and try to merge their locations. | ||||||||||
2213 | std::vector<const DILocation *> LoopUsesLocs; | ||||||||||
2214 | for (auto U : LoopUses) | ||||||||||
2215 | LoopUsesLocs.push_back(U->getDebugLoc().get()); | ||||||||||
2216 | auto DL = DebugLoc(DILocation::getMergedLocations(LoopUsesLocs)); | ||||||||||
2217 | |||||||||||
2218 | // We use the SSAUpdater interface to insert phi nodes as required. | ||||||||||
2219 | SmallVector<PHINode *, 16> NewPHIs; | ||||||||||
2220 | SSAUpdater SSA(&NewPHIs); | ||||||||||
2221 | LoopPromoter Promoter(SomePtr, LoopUses, SSA, PointerMustAliases, ExitBlocks, | ||||||||||
2222 | InsertPts, MSSAInsertPts, PIC, CurAST, MSSAU, *LI, DL, | ||||||||||
2223 | Alignment.value(), SawUnorderedAtomic, AATags, | ||||||||||
2224 | *SafetyInfo); | ||||||||||
2225 | |||||||||||
2226 | // Set up the preheader to have a definition of the value. It is the live-out | ||||||||||
2227 | // value from the preheader that uses in the loop will use. | ||||||||||
2228 | LoadInst *PreheaderLoad = new LoadInst( | ||||||||||
2229 | SomePtr->getType()->getPointerElementType(), SomePtr, | ||||||||||
2230 | SomePtr->getName() + ".promoted", Preheader->getTerminator()); | ||||||||||
2231 | if (SawUnorderedAtomic) | ||||||||||
2232 | PreheaderLoad->setOrdering(AtomicOrdering::Unordered); | ||||||||||
2233 | PreheaderLoad->setAlignment(Alignment); | ||||||||||
2234 | PreheaderLoad->setDebugLoc(DebugLoc()); | ||||||||||
2235 | if (AATags) | ||||||||||
2236 | PreheaderLoad->setAAMetadata(AATags); | ||||||||||
2237 | SSA.AddAvailableValue(Preheader, PreheaderLoad); | ||||||||||
2238 | |||||||||||
2239 | if (MSSAU) { | ||||||||||
2240 | MemoryAccess *PreheaderLoadMemoryAccess = MSSAU->createMemoryAccessInBB( | ||||||||||
2241 | PreheaderLoad, nullptr, PreheaderLoad->getParent(), MemorySSA::End); | ||||||||||
2242 | MemoryUse *NewMemUse = cast<MemoryUse>(PreheaderLoadMemoryAccess); | ||||||||||
2243 | MSSAU->insertUse(NewMemUse, /*RenameUses=*/true); | ||||||||||
2244 | } | ||||||||||
2245 | |||||||||||
2246 | if (MSSAU && VerifyMemorySSA) | ||||||||||
2247 | MSSAU->getMemorySSA()->verifyMemorySSA(); | ||||||||||
2248 | // Rewrite all the loads in the loop and remember all the definitions from | ||||||||||
2249 | // stores in the loop. | ||||||||||
2250 | Promoter.run(LoopUses); | ||||||||||
2251 | |||||||||||
2252 | if (MSSAU && VerifyMemorySSA) | ||||||||||
2253 | MSSAU->getMemorySSA()->verifyMemorySSA(); | ||||||||||
2254 | // If the SSAUpdater didn't use the load in the preheader, just zap it now. | ||||||||||
2255 | if (PreheaderLoad->use_empty()) | ||||||||||
2256 | eraseInstruction(*PreheaderLoad, *SafetyInfo, CurAST, MSSAU); | ||||||||||
2257 | |||||||||||
2258 | return true; | ||||||||||
2259 | } | ||||||||||
2260 | |||||||||||
2261 | static void foreachMemoryAccess(MemorySSA *MSSA, Loop *L, | ||||||||||
2262 | function_ref<void(Instruction *)> Fn) { | ||||||||||
2263 | for (const BasicBlock *BB : L->blocks()) | ||||||||||
2264 | if (const auto *Accesses = MSSA->getBlockAccesses(BB)) | ||||||||||
2265 | for (const auto &Access : *Accesses) | ||||||||||
2266 | if (const auto *MUD = dyn_cast<MemoryUseOrDef>(&Access)) | ||||||||||
2267 | Fn(MUD->getMemoryInst()); | ||||||||||
2268 | } | ||||||||||
2269 | |||||||||||
2270 | static SmallVector<SmallSetVector<Value *, 8>, 0> | ||||||||||
2271 | collectPromotionCandidates(MemorySSA *MSSA, AliasAnalysis *AA, Loop *L, | ||||||||||
2272 | SmallVectorImpl<Instruction *> &MaybePromotable) { | ||||||||||
2273 | AliasSetTracker AST(*AA); | ||||||||||
2274 | |||||||||||
2275 | auto IsPotentiallyPromotable = [L](const Instruction *I) { | ||||||||||
2276 | if (const auto *SI = dyn_cast<StoreInst>(I)) | ||||||||||
2277 | return L->isLoopInvariant(SI->getPointerOperand()); | ||||||||||
2278 | if (const auto *LI = dyn_cast<LoadInst>(I)) | ||||||||||
2279 | return L->isLoopInvariant(LI->getPointerOperand()); | ||||||||||
2280 | return false; | ||||||||||
2281 | }; | ||||||||||
2282 | |||||||||||
2283 | // Populate AST with potentially promotable accesses and remove them from | ||||||||||
2284 | // MaybePromotable, so they will not be checked again on the next iteration. | ||||||||||
2285 | SmallPtrSet<Value *, 16> AttemptingPromotion; | ||||||||||
2286 | llvm::erase_if(MaybePromotable, [&](Instruction *I) { | ||||||||||
2287 | if (IsPotentiallyPromotable(I)) { | ||||||||||
2288 | AttemptingPromotion.insert(I); | ||||||||||
2289 | AST.add(I); | ||||||||||
2290 | return true; | ||||||||||
2291 | } | ||||||||||
2292 | return false; | ||||||||||
2293 | }); | ||||||||||
2294 | |||||||||||
2295 | // We're only interested in must-alias sets that contain a mod. | ||||||||||
2296 | SmallVector<const AliasSet *, 8> Sets; | ||||||||||
2297 | for (AliasSet &AS : AST) | ||||||||||
2298 | if (!AS.isForwardingAliasSet() && AS.isMod() && AS.isMustAlias()) | ||||||||||
2299 | Sets.push_back(&AS); | ||||||||||
2300 | |||||||||||
2301 | if (Sets.empty()) | ||||||||||
2302 | return {}; // Nothing to promote... | ||||||||||
2303 | |||||||||||
2304 | // Discard any sets for which there is an aliasing non-promotable access. | ||||||||||
2305 | foreachMemoryAccess(MSSA, L, [&](Instruction *I) { | ||||||||||
2306 | if (AttemptingPromotion.contains(I)) | ||||||||||
2307 | return; | ||||||||||
2308 | |||||||||||
2309 | llvm::erase_if(Sets, [&](const AliasSet *AS) { | ||||||||||
2310 | return AS->aliasesUnknownInst(I, *AA); | ||||||||||
2311 | }); | ||||||||||
2312 | }); | ||||||||||
2313 | |||||||||||
2314 | SmallVector<SmallSetVector<Value *, 8>, 0> Result; | ||||||||||
2315 | for (const AliasSet *Set : Sets) { | ||||||||||
2316 | SmallSetVector<Value *, 8> PointerMustAliases; | ||||||||||
2317 | for (const auto &ASI : *Set) | ||||||||||
2318 | PointerMustAliases.insert(ASI.getValue()); | ||||||||||
2319 | Result.push_back(std::move(PointerMustAliases)); | ||||||||||
2320 | } | ||||||||||
2321 | |||||||||||
2322 | return Result; | ||||||||||
2323 | } | ||||||||||
2324 | |||||||||||
2325 | /// Returns an owning pointer to an alias set which incorporates aliasing info | ||||||||||
2326 | /// from L and all subloops of L. | ||||||||||
2327 | std::unique_ptr<AliasSetTracker> | ||||||||||
2328 | LoopInvariantCodeMotion::collectAliasInfoForLoop(Loop *L, LoopInfo *LI, | ||||||||||
2329 | AAResults *AA) { | ||||||||||
2330 | auto CurAST = std::make_unique<AliasSetTracker>(*AA); | ||||||||||
2331 | |||||||||||
2332 | // Add everything from all the sub loops. | ||||||||||
2333 | for (Loop *InnerL : L->getSubLoops()) | ||||||||||
2334 | for (BasicBlock *BB : InnerL->blocks()) | ||||||||||
2335 | CurAST->add(*BB); | ||||||||||
2336 | |||||||||||
2337 | // And merge in this loop (without anything from inner loops). | ||||||||||
2338 | for (BasicBlock *BB : L->blocks()) | ||||||||||
2339 | if (LI->getLoopFor(BB) == L) | ||||||||||
2340 | CurAST->add(*BB); | ||||||||||
2341 | |||||||||||
2342 | return CurAST; | ||||||||||
2343 | } | ||||||||||
2344 | |||||||||||
2345 | static bool pointerInvalidatedByLoop(MemoryLocation MemLoc, | ||||||||||
2346 | AliasSetTracker *CurAST, Loop *CurLoop, | ||||||||||
2347 | AAResults *AA) { | ||||||||||
2348 | // First check to see if any of the basic blocks in CurLoop invalidate *V. | ||||||||||
2349 | bool isInvalidatedAccordingToAST = CurAST->getAliasSetFor(MemLoc).isMod(); | ||||||||||
2350 | |||||||||||
2351 | if (!isInvalidatedAccordingToAST || !LICMN2Theshold) | ||||||||||
2352 | return isInvalidatedAccordingToAST; | ||||||||||
2353 | |||||||||||
2354 | // Check with a diagnostic analysis if we can refine the information above. | ||||||||||
2355 | // This is to identify the limitations of using the AST. | ||||||||||
2356 | // The alias set mechanism used by LICM has a major weakness in that it | ||||||||||
2357 | // combines all things which may alias into a single set *before* asking | ||||||||||
2358 | // modref questions. As a result, a single readonly call within a loop will | ||||||||||
2359 | // collapse all loads and stores into a single alias set and report | ||||||||||
2360 | // invalidation if the loop contains any store. For example, readonly calls | ||||||||||
2361 | // with deopt states have this form and create a general alias set with all | ||||||||||
2362 | // loads and stores. In order to get any LICM in loops containing possible | ||||||||||
2363 | // deopt states we need a more precise invalidation of checking the mod ref | ||||||||||
2364 | // info of each instruction within the loop and LI. This has a complexity of | ||||||||||
2365 | // O(N^2), so currently, it is used only as a diagnostic tool since the | ||||||||||
2366 | // default value of LICMN2Threshold is zero. | ||||||||||
2367 | |||||||||||
2368 | // Don't look at nested loops. | ||||||||||
2369 | if (CurLoop->begin() != CurLoop->end()) | ||||||||||
2370 | return true; | ||||||||||
2371 | |||||||||||
2372 | int N = 0; | ||||||||||
2373 | for (BasicBlock *BB : CurLoop->getBlocks()) | ||||||||||
2374 | for (Instruction &I : *BB) { | ||||||||||
2375 | if (N >= LICMN2Theshold) { | ||||||||||
2376 | 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) | ||||||||||
2377 | << *(MemLoc.Ptr) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "Alasing N2 threshold exhausted for " << *(MemLoc.Ptr) << "\n"; } } while (false); | ||||||||||
2378 | return true; | ||||||||||
2379 | } | ||||||||||
2380 | N++; | ||||||||||
2381 | auto Res = AA->getModRefInfo(&I, MemLoc); | ||||||||||
2382 | if (isModSet(Res)) { | ||||||||||
2383 | 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 ) | ||||||||||
2384 | << *(MemLoc.Ptr) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "Aliasing failed on " << I << " for " << *(MemLoc.Ptr) << "\n"; } } while (false ); | ||||||||||
2385 | return true; | ||||||||||
2386 | } | ||||||||||
2387 | } | ||||||||||
2388 | 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); | ||||||||||
2389 | return false; | ||||||||||
2390 | } | ||||||||||
2391 | |||||||||||
2392 | bool pointerInvalidatedByLoopWithMSSA(MemorySSA *MSSA, MemoryUse *MU, | ||||||||||
2393 | Loop *CurLoop, Instruction &I, | ||||||||||
2394 | SinkAndHoistLICMFlags &Flags) { | ||||||||||
2395 | // For hoisting, use the walker to determine safety | ||||||||||
2396 | if (!Flags.getIsSink()) { | ||||||||||
2397 | MemoryAccess *Source; | ||||||||||
2398 | // See declaration of SetLicmMssaOptCap for usage details. | ||||||||||
2399 | if (Flags.tooManyClobberingCalls()) | ||||||||||
2400 | Source = MU->getDefiningAccess(); | ||||||||||
2401 | else { | ||||||||||
2402 | Source = MSSA->getSkipSelfWalker()->getClobberingMemoryAccess(MU); | ||||||||||
2403 | Flags.incrementClobberingCalls(); | ||||||||||
2404 | } | ||||||||||
2405 | return !MSSA->isLiveOnEntryDef(Source) && | ||||||||||
2406 | CurLoop->contains(Source->getBlock()); | ||||||||||
2407 | } | ||||||||||
2408 | |||||||||||
2409 | // For sinking, we'd need to check all Defs below this use. The getClobbering | ||||||||||
2410 | // call will look on the backedge of the loop, but will check aliasing with | ||||||||||
2411 | // the instructions on the previous iteration. | ||||||||||
2412 | // For example: | ||||||||||
2413 | // for (i ... ) | ||||||||||
2414 | // load a[i] ( Use (LoE) | ||||||||||
2415 | // store a[i] ( 1 = Def (2), with 2 = Phi for the loop. | ||||||||||
2416 | // i++; | ||||||||||
2417 | // The load sees no clobbering inside the loop, as the backedge alias check | ||||||||||
2418 | // does phi translation, and will check aliasing against store a[i-1]. | ||||||||||
2419 | // However sinking the load outside the loop, below the store is incorrect. | ||||||||||
2420 | |||||||||||
2421 | // For now, only sink if there are no Defs in the loop, and the existing ones | ||||||||||
2422 | // precede the use and are in the same block. | ||||||||||
2423 | // FIXME: Increase precision: Safe to sink if Use post dominates the Def; | ||||||||||
2424 | // needs PostDominatorTreeAnalysis. | ||||||||||
2425 | // FIXME: More precise: no Defs that alias this Use. | ||||||||||
2426 | if (Flags.tooManyMemoryAccesses()) | ||||||||||
2427 | return true; | ||||||||||
2428 | for (auto *BB : CurLoop->getBlocks()) | ||||||||||
2429 | if (pointerInvalidatedByBlockWithMSSA(*BB, *MSSA, *MU)) | ||||||||||
2430 | return true; | ||||||||||
2431 | // When sinking, the source block may not be part of the loop so check it. | ||||||||||
2432 | if (!CurLoop->contains(&I)) | ||||||||||
2433 | return pointerInvalidatedByBlockWithMSSA(*I.getParent(), *MSSA, *MU); | ||||||||||
2434 | |||||||||||
2435 | return false; | ||||||||||
2436 | } | ||||||||||
2437 | |||||||||||
2438 | bool pointerInvalidatedByBlockWithMSSA(BasicBlock &BB, MemorySSA &MSSA, | ||||||||||
2439 | MemoryUse &MU) { | ||||||||||
2440 | if (const auto *Accesses = MSSA.getBlockDefs(&BB)) | ||||||||||
2441 | for (const auto &MA : *Accesses) | ||||||||||
2442 | if (const auto *MD = dyn_cast<MemoryDef>(&MA)) | ||||||||||
2443 | if (MU.getBlock() != MD->getBlock() || !MSSA.locallyDominates(MD, &MU)) | ||||||||||
2444 | return true; | ||||||||||
2445 | return false; | ||||||||||
2446 | } | ||||||||||
2447 | |||||||||||
2448 | /// Little predicate that returns true if the specified basic block is in | ||||||||||
2449 | /// a subloop of the current one, not the current one itself. | ||||||||||
2450 | /// | ||||||||||
2451 | static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI) { | ||||||||||
2452 | 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-13~++20210405022414+5f57793c4fe4/llvm/lib/Transforms/Scalar/LICM.cpp" , 2452, __PRETTY_FUNCTION__)); | ||||||||||
2453 | return LI->getLoopFor(BB) != CurLoop; | ||||||||||
2454 | } |
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-13~++20210405022414+5f57793c4fe4/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-13~++20210405022414+5f57793c4fe4/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-13~++20210405022414+5f57793c4fe4/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-13~++20210405022414+5f57793c4fe4/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-13~++20210405022414+5f57793c4fe4/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-13~++20210405022414+5f57793c4fe4/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-13~++20210405022414+5f57793c4fe4/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 | /// Create a clone of \p CB with the operand bundle with the tag matching |
1218 | /// \p Bundle's tag replaced with Bundle, and insert it before \p InsertPt. |
1219 | /// |
1220 | /// The returned call instruction is identical \p CI in every way except that |
1221 | /// the specified operand bundle has been replaced. |
1222 | static CallBase *Create(CallBase *CB, |
1223 | OperandBundleDef Bundle, |
1224 | Instruction *InsertPt = nullptr); |
1225 | |
1226 | /// Create a clone of \p CB with operand bundle \p OB added. |
1227 | static CallBase *addOperandBundle(CallBase *CB, uint32_t ID, |
1228 | OperandBundleDef OB, |
1229 | Instruction *InsertPt = nullptr); |
1230 | |
1231 | /// Create a clone of \p CB with operand bundle \p ID removed. |
1232 | static CallBase *removeOperandBundle(CallBase *CB, uint32_t ID, |
1233 | Instruction *InsertPt = nullptr); |
1234 | |
1235 | static bool classof(const Instruction *I) { |
1236 | return I->getOpcode() == Instruction::Call || |
1237 | I->getOpcode() == Instruction::Invoke || |
1238 | I->getOpcode() == Instruction::CallBr; |
1239 | } |
1240 | static bool classof(const Value *V) { |
1241 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1242 | } |
1243 | |
1244 | FunctionType *getFunctionType() const { return FTy; } |
1245 | |
1246 | void mutateFunctionType(FunctionType *FTy) { |
1247 | Value::mutateType(FTy->getReturnType()); |
1248 | this->FTy = FTy; |
1249 | } |
1250 | |
1251 | 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; |
1252 | |
1253 | /// data_operands_begin/data_operands_end - Return iterators iterating over |
1254 | /// the call / invoke argument list and bundle operands. For invokes, this is |
1255 | /// the set of instruction operands except the invoke target and the two |
1256 | /// successor blocks; and for calls this is the set of instruction operands |
1257 | /// except the call target. |
1258 | User::op_iterator data_operands_begin() { return op_begin(); } |
1259 | User::const_op_iterator data_operands_begin() const { |
1260 | return const_cast<CallBase *>(this)->data_operands_begin(); |
1261 | } |
1262 | User::op_iterator data_operands_end() { |
1263 | // Walk from the end of the operands over the called operand and any |
1264 | // subclass operands. |
1265 | return op_end() - getNumSubclassExtraOperands() - 1; |
1266 | } |
1267 | User::const_op_iterator data_operands_end() const { |
1268 | return const_cast<CallBase *>(this)->data_operands_end(); |
1269 | } |
1270 | iterator_range<User::op_iterator> data_ops() { |
1271 | return make_range(data_operands_begin(), data_operands_end()); |
1272 | } |
1273 | iterator_range<User::const_op_iterator> data_ops() const { |
1274 | return make_range(data_operands_begin(), data_operands_end()); |
1275 | } |
1276 | bool data_operands_empty() const { |
1277 | return data_operands_end() == data_operands_begin(); |
1278 | } |
1279 | unsigned data_operands_size() const { |
1280 | return std::distance(data_operands_begin(), data_operands_end()); |
1281 | } |
1282 | |
1283 | bool isDataOperand(const Use *U) const { |
1284 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1285, __PRETTY_FUNCTION__)) |
1285 | "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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1285, __PRETTY_FUNCTION__)); |
1286 | return data_operands_begin() <= U && U < data_operands_end(); |
1287 | } |
1288 | bool isDataOperand(Value::const_user_iterator UI) const { |
1289 | return isDataOperand(&UI.getUse()); |
1290 | } |
1291 | |
1292 | /// Given a value use iterator, return the data operand corresponding to it. |
1293 | /// Iterator must actually correspond to a data operand. |
1294 | unsigned getDataOperandNo(Value::const_user_iterator UI) const { |
1295 | return getDataOperandNo(&UI.getUse()); |
1296 | } |
1297 | |
1298 | /// Given a use for a data operand, get the data operand number that |
1299 | /// corresponds to it. |
1300 | unsigned getDataOperandNo(const Use *U) const { |
1301 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1301, __PRETTY_FUNCTION__)); |
1302 | return U - data_operands_begin(); |
1303 | } |
1304 | |
1305 | /// Return the iterator pointing to the beginning of the argument list. |
1306 | User::op_iterator arg_begin() { return op_begin(); } |
1307 | User::const_op_iterator arg_begin() const { |
1308 | return const_cast<CallBase *>(this)->arg_begin(); |
1309 | } |
1310 | |
1311 | /// Return the iterator pointing to the end of the argument list. |
1312 | User::op_iterator arg_end() { |
1313 | // From the end of the data operands, walk backwards past the bundle |
1314 | // operands. |
1315 | return data_operands_end() - getNumTotalBundleOperands(); |
1316 | } |
1317 | User::const_op_iterator arg_end() const { |
1318 | return const_cast<CallBase *>(this)->arg_end(); |
1319 | } |
1320 | |
1321 | /// Iteration adapter for range-for loops. |
1322 | iterator_range<User::op_iterator> args() { |
1323 | return make_range(arg_begin(), arg_end()); |
1324 | } |
1325 | iterator_range<User::const_op_iterator> args() const { |
1326 | return make_range(arg_begin(), arg_end()); |
1327 | } |
1328 | bool arg_empty() const { return arg_end() == arg_begin(); } |
1329 | unsigned arg_size() const { return arg_end() - arg_begin(); } |
1330 | |
1331 | // Legacy API names that duplicate the above and will be removed once users |
1332 | // are migrated. |
1333 | iterator_range<User::op_iterator> arg_operands() { |
1334 | return make_range(arg_begin(), arg_end()); |
1335 | } |
1336 | iterator_range<User::const_op_iterator> arg_operands() const { |
1337 | return make_range(arg_begin(), arg_end()); |
1338 | } |
1339 | unsigned getNumArgOperands() const { return arg_size(); } |
1340 | |
1341 | Value *getArgOperand(unsigned i) const { |
1342 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1342, __PRETTY_FUNCTION__)); |
1343 | return getOperand(i); |
1344 | } |
1345 | |
1346 | void setArgOperand(unsigned i, Value *v) { |
1347 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1347, __PRETTY_FUNCTION__)); |
1348 | setOperand(i, v); |
1349 | } |
1350 | |
1351 | /// Wrappers for getting the \c Use of a call argument. |
1352 | const Use &getArgOperandUse(unsigned i) const { |
1353 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1353, __PRETTY_FUNCTION__)); |
1354 | return User::getOperandUse(i); |
1355 | } |
1356 | Use &getArgOperandUse(unsigned i) { |
1357 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1357, __PRETTY_FUNCTION__)); |
1358 | return User::getOperandUse(i); |
1359 | } |
1360 | |
1361 | bool isArgOperand(const Use *U) const { |
1362 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1363, __PRETTY_FUNCTION__)) |
1363 | "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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1363, __PRETTY_FUNCTION__)); |
1364 | return arg_begin() <= U && U < arg_end(); |
1365 | } |
1366 | bool isArgOperand(Value::const_user_iterator UI) const { |
1367 | return isArgOperand(&UI.getUse()); |
1368 | } |
1369 | |
1370 | /// Given a use for a arg operand, get the arg operand number that |
1371 | /// corresponds to it. |
1372 | unsigned getArgOperandNo(const Use *U) const { |
1373 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1373, __PRETTY_FUNCTION__)); |
1374 | return U - arg_begin(); |
1375 | } |
1376 | |
1377 | /// Given a value use iterator, return the arg operand number corresponding to |
1378 | /// it. Iterator must actually correspond to a data operand. |
1379 | unsigned getArgOperandNo(Value::const_user_iterator UI) const { |
1380 | return getArgOperandNo(&UI.getUse()); |
1381 | } |
1382 | |
1383 | /// Returns true if this CallSite passes the given Value* as an argument to |
1384 | /// the called function. |
1385 | bool hasArgument(const Value *V) const { |
1386 | return llvm::is_contained(args(), V); |
1387 | } |
1388 | |
1389 | Value *getCalledOperand() const { return Op<CalledOperandOpEndIdx>(); } |
1390 | |
1391 | const Use &getCalledOperandUse() const { return Op<CalledOperandOpEndIdx>(); } |
1392 | Use &getCalledOperandUse() { return Op<CalledOperandOpEndIdx>(); } |
1393 | |
1394 | /// Returns the function called, or null if this is an |
1395 | /// indirect function invocation. |
1396 | Function *getCalledFunction() const { |
1397 | return dyn_cast_or_null<Function>(getCalledOperand()); |
1398 | } |
1399 | |
1400 | /// Return true if the callsite is an indirect call. |
1401 | bool isIndirectCall() const; |
1402 | |
1403 | /// Determine whether the passed iterator points to the callee operand's Use. |
1404 | bool isCallee(Value::const_user_iterator UI) const { |
1405 | return isCallee(&UI.getUse()); |
1406 | } |
1407 | |
1408 | /// Determine whether this Use is the callee operand's Use. |
1409 | bool isCallee(const Use *U) const { return &getCalledOperandUse() == U; } |
1410 | |
1411 | /// Helper to get the caller (the parent function). |
1412 | Function *getCaller(); |
1413 | const Function *getCaller() const { |
1414 | return const_cast<CallBase *>(this)->getCaller(); |
1415 | } |
1416 | |
1417 | /// Tests if this call site must be tail call optimized. Only a CallInst can |
1418 | /// be tail call optimized. |
1419 | bool isMustTailCall() const; |
1420 | |
1421 | /// Tests if this call site is marked as a tail call. |
1422 | bool isTailCall() const; |
1423 | |
1424 | /// Returns the intrinsic ID of the intrinsic called or |
1425 | /// Intrinsic::not_intrinsic if the called function is not an intrinsic, or if |
1426 | /// this is an indirect call. |
1427 | Intrinsic::ID getIntrinsicID() const; |
1428 | |
1429 | void setCalledOperand(Value *V) { Op<CalledOperandOpEndIdx>() = V; } |
1430 | |
1431 | /// Sets the function called, including updating the function type. |
1432 | void setCalledFunction(Function *Fn) { |
1433 | setCalledFunction(Fn->getFunctionType(), Fn); |
1434 | } |
1435 | |
1436 | /// Sets the function called, including updating the function type. |
1437 | void setCalledFunction(FunctionCallee Fn) { |
1438 | setCalledFunction(Fn.getFunctionType(), Fn.getCallee()); |
1439 | } |
1440 | |
1441 | /// Sets the function called, including updating to the specified function |
1442 | /// type. |
1443 | void setCalledFunction(FunctionType *FTy, Value *Fn) { |
1444 | this->FTy = FTy; |
1445 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1446, __PRETTY_FUNCTION__)) |
1446 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1446, __PRETTY_FUNCTION__)); |
1447 | // This function doesn't mutate the return type, only the function |
1448 | // type. Seems broken, but I'm just gonna stick an assert in for now. |
1449 | assert(getType() == FTy->getReturnType())((getType() == FTy->getReturnType()) ? static_cast<void > (0) : __assert_fail ("getType() == FTy->getReturnType()" , "/build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1449, __PRETTY_FUNCTION__)); |
1450 | setCalledOperand(Fn); |
1451 | } |
1452 | |
1453 | CallingConv::ID getCallingConv() const { |
1454 | return getSubclassData<CallingConvField>(); |
1455 | } |
1456 | |
1457 | void setCallingConv(CallingConv::ID CC) { |
1458 | setSubclassData<CallingConvField>(CC); |
1459 | } |
1460 | |
1461 | /// Check if this call is an inline asm statement. |
1462 | bool isInlineAsm() const { return isa<InlineAsm>(getCalledOperand()); } |
1463 | |
1464 | /// \name Attribute API |
1465 | /// |
1466 | /// These methods access and modify attributes on this call (including |
1467 | /// looking through to the attributes on the called function when necessary). |
1468 | ///@{ |
1469 | |
1470 | /// Return the parameter attributes for this call. |
1471 | /// |
1472 | AttributeList getAttributes() const { return Attrs; } |
1473 | |
1474 | /// Set the parameter attributes for this call. |
1475 | /// |
1476 | void setAttributes(AttributeList A) { Attrs = A; } |
1477 | |
1478 | /// Determine whether this call has the given attribute. If it does not |
1479 | /// then determine if the called function has the attribute, but only if |
1480 | /// the attribute is allowed for the call. |
1481 | bool hasFnAttr(Attribute::AttrKind Kind) const { |
1482 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1483, __PRETTY_FUNCTION__)) |
1483 | "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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1483, __PRETTY_FUNCTION__)); |
1484 | return hasFnAttrImpl(Kind); |
1485 | } |
1486 | |
1487 | /// Determine whether this call has the given attribute. If it does not |
1488 | /// then determine if the called function has the attribute, but only if |
1489 | /// the attribute is allowed for the call. |
1490 | bool hasFnAttr(StringRef Kind) const { return hasFnAttrImpl(Kind); } |
1491 | |
1492 | /// adds the attribute to the list of attributes. |
1493 | void addAttribute(unsigned i, Attribute::AttrKind Kind) { |
1494 | AttributeList PAL = getAttributes(); |
1495 | PAL = PAL.addAttribute(getContext(), i, Kind); |
1496 | setAttributes(PAL); |
1497 | } |
1498 | |
1499 | /// adds the attribute to the list of attributes. |
1500 | void addAttribute(unsigned i, Attribute Attr) { |
1501 | AttributeList PAL = getAttributes(); |
1502 | PAL = PAL.addAttribute(getContext(), i, Attr); |
1503 | setAttributes(PAL); |
1504 | } |
1505 | |
1506 | /// Adds the attribute to the indicated argument |
1507 | void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { |
1508 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1508, __PRETTY_FUNCTION__)); |
1509 | AttributeList PAL = getAttributes(); |
1510 | PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind); |
1511 | setAttributes(PAL); |
1512 | } |
1513 | |
1514 | /// Adds the attribute to the indicated argument |
1515 | void addParamAttr(unsigned ArgNo, Attribute Attr) { |
1516 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1516, __PRETTY_FUNCTION__)); |
1517 | AttributeList PAL = getAttributes(); |
1518 | PAL = PAL.addParamAttribute(getContext(), ArgNo, Attr); |
1519 | setAttributes(PAL); |
1520 | } |
1521 | |
1522 | /// removes the attribute from the list of attributes. |
1523 | void removeAttribute(unsigned i, Attribute::AttrKind Kind) { |
1524 | AttributeList PAL = getAttributes(); |
1525 | PAL = PAL.removeAttribute(getContext(), i, Kind); |
1526 | setAttributes(PAL); |
1527 | } |
1528 | |
1529 | /// removes the attribute from the list of attributes. |
1530 | void removeAttribute(unsigned i, StringRef Kind) { |
1531 | AttributeList PAL = getAttributes(); |
1532 | PAL = PAL.removeAttribute(getContext(), i, Kind); |
1533 | setAttributes(PAL); |
1534 | } |
1535 | |
1536 | void removeAttributes(unsigned i, const AttrBuilder &Attrs) { |
1537 | AttributeList PAL = getAttributes(); |
1538 | PAL = PAL.removeAttributes(getContext(), i, Attrs); |
1539 | setAttributes(PAL); |
1540 | } |
1541 | |
1542 | /// Removes the attribute from the given argument |
1543 | void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { |
1544 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1544, __PRETTY_FUNCTION__)); |
1545 | AttributeList PAL = getAttributes(); |
1546 | PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind); |
1547 | setAttributes(PAL); |
1548 | } |
1549 | |
1550 | /// Removes the attribute from the given argument |
1551 | void removeParamAttr(unsigned ArgNo, StringRef Kind) { |
1552 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1552, __PRETTY_FUNCTION__)); |
1553 | AttributeList PAL = getAttributes(); |
1554 | PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind); |
1555 | setAttributes(PAL); |
1556 | } |
1557 | |
1558 | /// Removes noundef and other attributes that imply undefined behavior if a |
1559 | /// `undef` or `poison` value is passed from the given argument. |
1560 | void removeParamUndefImplyingAttrs(unsigned ArgNo) { |
1561 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1561, __PRETTY_FUNCTION__)); |
1562 | AttributeList PAL = getAttributes(); |
1563 | PAL = PAL.removeParamUndefImplyingAttributes(getContext(), ArgNo); |
1564 | setAttributes(PAL); |
1565 | } |
1566 | |
1567 | /// adds the dereferenceable attribute to the list of attributes. |
1568 | void addDereferenceableAttr(unsigned i, uint64_t Bytes) { |
1569 | AttributeList PAL = getAttributes(); |
1570 | PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes); |
1571 | setAttributes(PAL); |
1572 | } |
1573 | |
1574 | /// adds the dereferenceable_or_null attribute to the list of |
1575 | /// attributes. |
1576 | void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) { |
1577 | AttributeList PAL = getAttributes(); |
1578 | PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes); |
1579 | setAttributes(PAL); |
1580 | } |
1581 | |
1582 | /// Determine whether the return value has the given attribute. |
1583 | bool hasRetAttr(Attribute::AttrKind Kind) const { |
1584 | return hasRetAttrImpl(Kind); |
1585 | } |
1586 | /// Determine whether the return value has the given attribute. |
1587 | bool hasRetAttr(StringRef Kind) const { return hasRetAttrImpl(Kind); } |
1588 | |
1589 | /// Determine whether the argument or parameter has the given attribute. |
1590 | bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const; |
1591 | |
1592 | /// Get the attribute of a given kind at a position. |
1593 | Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const { |
1594 | return getAttributes().getAttribute(i, Kind); |
1595 | } |
1596 | |
1597 | /// Get the attribute of a given kind at a position. |
1598 | Attribute getAttribute(unsigned i, StringRef Kind) const { |
1599 | return getAttributes().getAttribute(i, Kind); |
1600 | } |
1601 | |
1602 | /// Get the attribute of a given kind from a given arg |
1603 | Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const { |
1604 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1604, __PRETTY_FUNCTION__)); |
1605 | return getAttributes().getParamAttr(ArgNo, Kind); |
1606 | } |
1607 | |
1608 | /// Get the attribute of a given kind from a given arg |
1609 | Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const { |
1610 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1610, __PRETTY_FUNCTION__)); |
1611 | return getAttributes().getParamAttr(ArgNo, Kind); |
1612 | } |
1613 | |
1614 | /// Return true if the data operand at index \p i has the attribute \p |
1615 | /// A. |
1616 | /// |
1617 | /// Data operands include call arguments and values used in operand bundles, |
1618 | /// but does not include the callee operand. This routine dispatches to the |
1619 | /// underlying AttributeList or the OperandBundleUser as appropriate. |
1620 | /// |
1621 | /// The index \p i is interpreted as |
1622 | /// |
1623 | /// \p i == Attribute::ReturnIndex -> the return value |
1624 | /// \p i in [1, arg_size + 1) -> argument number (\p i - 1) |
1625 | /// \p i in [arg_size + 1, data_operand_size + 1) -> bundle operand at index |
1626 | /// (\p i - 1) in the operand list. |
1627 | bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const { |
1628 | // Note that we have to add one because `i` isn't zero-indexed. |
1629 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1630, __PRETTY_FUNCTION__)) |
1630 | "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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1630, __PRETTY_FUNCTION__)); |
1631 | |
1632 | // The attribute A can either be directly specified, if the operand in |
1633 | // question is a call argument; or be indirectly implied by the kind of its |
1634 | // containing operand bundle, if the operand is a bundle operand. |
1635 | |
1636 | if (i == AttributeList::ReturnIndex) |
1637 | return hasRetAttr(Kind); |
1638 | |
1639 | // FIXME: Avoid these i - 1 calculations and update the API to use |
1640 | // zero-based indices. |
1641 | if (i < (getNumArgOperands() + 1)) |
1642 | return paramHasAttr(i - 1, Kind); |
1643 | |
1644 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1645, __PRETTY_FUNCTION__)) |
1645 | "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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1645, __PRETTY_FUNCTION__)); |
1646 | return bundleOperandHasAttr(i - 1, Kind); |
1647 | } |
1648 | |
1649 | /// Determine whether this data operand is not captured. |
1650 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1651 | // better indicate that this may return a conservative answer. |
1652 | bool doesNotCapture(unsigned OpNo) const { |
1653 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::NoCapture); |
1654 | } |
1655 | |
1656 | /// Determine whether this argument is passed by value. |
1657 | bool isByValArgument(unsigned ArgNo) const { |
1658 | return paramHasAttr(ArgNo, Attribute::ByVal); |
1659 | } |
1660 | |
1661 | /// Determine whether this argument is passed in an alloca. |
1662 | bool isInAllocaArgument(unsigned ArgNo) const { |
1663 | return paramHasAttr(ArgNo, Attribute::InAlloca); |
1664 | } |
1665 | |
1666 | /// Determine whether this argument is passed by value, in an alloca, or is |
1667 | /// preallocated. |
1668 | bool isPassPointeeByValueArgument(unsigned ArgNo) const { |
1669 | return paramHasAttr(ArgNo, Attribute::ByVal) || |
1670 | paramHasAttr(ArgNo, Attribute::InAlloca) || |
1671 | paramHasAttr(ArgNo, Attribute::Preallocated); |
1672 | } |
1673 | |
1674 | /// Determine whether passing undef to this argument is undefined behavior. |
1675 | /// If passing undef to this argument is UB, passing poison is UB as well |
1676 | /// because poison is more undefined than undef. |
1677 | bool isPassingUndefUB(unsigned ArgNo) const { |
1678 | return paramHasAttr(ArgNo, Attribute::NoUndef) || |
1679 | // dereferenceable implies noundef. |
1680 | paramHasAttr(ArgNo, Attribute::Dereferenceable) || |
1681 | // dereferenceable implies noundef, and null is a well-defined value. |
1682 | paramHasAttr(ArgNo, Attribute::DereferenceableOrNull); |
1683 | } |
1684 | |
1685 | /// Determine if there are is an inalloca argument. Only the last argument can |
1686 | /// have the inalloca attribute. |
1687 | bool hasInAllocaArgument() const { |
1688 | return !arg_empty() && paramHasAttr(arg_size() - 1, Attribute::InAlloca); |
1689 | } |
1690 | |
1691 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1692 | // better indicate that this may return a conservative answer. |
1693 | bool doesNotAccessMemory(unsigned OpNo) const { |
1694 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); |
1695 | } |
1696 | |
1697 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1698 | // better indicate that this may return a conservative answer. |
1699 | bool onlyReadsMemory(unsigned OpNo) const { |
1700 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadOnly) || |
1701 | dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); |
1702 | } |
1703 | |
1704 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1705 | // better indicate that this may return a conservative answer. |
1706 | bool doesNotReadMemory(unsigned OpNo) const { |
1707 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::WriteOnly) || |
1708 | dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); |
1709 | } |
1710 | |
1711 | LLVM_ATTRIBUTE_DEPRECATED(unsigned getRetAlignment() const,[[deprecated("Use getRetAlign() instead")]] unsigned getRetAlignment () const |
1712 | "Use getRetAlign() instead")[[deprecated("Use getRetAlign() instead")]] unsigned getRetAlignment () const { |
1713 | if (const auto MA = Attrs.getRetAlignment()) |
1714 | return MA->value(); |
1715 | return 0; |
1716 | } |
1717 | |
1718 | /// Extract the alignment of the return value. |
1719 | MaybeAlign getRetAlign() const { return Attrs.getRetAlignment(); } |
1720 | |
1721 | /// Extract the alignment for a call or parameter (0=unknown). |
1722 | LLVM_ATTRIBUTE_DEPRECATED(unsigned getParamAlignment(unsigned ArgNo) const,[[deprecated("Use getParamAlign() instead")]] unsigned getParamAlignment (unsigned ArgNo) const |
1723 | "Use getParamAlign() instead")[[deprecated("Use getParamAlign() instead")]] unsigned getParamAlignment (unsigned ArgNo) const { |
1724 | if (const auto MA = Attrs.getParamAlignment(ArgNo)) |
1725 | return MA->value(); |
1726 | return 0; |
1727 | } |
1728 | |
1729 | /// Extract the alignment for a call or parameter (0=unknown). |
1730 | MaybeAlign getParamAlign(unsigned ArgNo) const { |
1731 | return Attrs.getParamAlignment(ArgNo); |
1732 | } |
1733 | |
1734 | /// Extract the byval type for a call or parameter. |
1735 | Type *getParamByValType(unsigned ArgNo) const { |
1736 | Type *Ty = Attrs.getParamByValType(ArgNo); |
1737 | return Ty ? Ty : getArgOperand(ArgNo)->getType()->getPointerElementType(); |
1738 | } |
1739 | |
1740 | /// Extract the preallocated type for a call or parameter. |
1741 | Type *getParamPreallocatedType(unsigned ArgNo) const { |
1742 | Type *Ty = Attrs.getParamPreallocatedType(ArgNo); |
1743 | return Ty ? Ty : getArgOperand(ArgNo)->getType()->getPointerElementType(); |
1744 | } |
1745 | |
1746 | /// Extract the number of dereferenceable bytes for a call or |
1747 | /// parameter (0=unknown). |
1748 | uint64_t getDereferenceableBytes(unsigned i) const { |
1749 | return Attrs.getDereferenceableBytes(i); |
1750 | } |
1751 | |
1752 | /// Extract the number of dereferenceable_or_null bytes for a call or |
1753 | /// parameter (0=unknown). |
1754 | uint64_t getDereferenceableOrNullBytes(unsigned i) const { |
1755 | return Attrs.getDereferenceableOrNullBytes(i); |
1756 | } |
1757 | |
1758 | /// Return true if the return value is known to be not null. |
1759 | /// This may be because it has the nonnull attribute, or because at least |
1760 | /// one byte is dereferenceable and the pointer is in addrspace(0). |
1761 | bool isReturnNonNull() const; |
1762 | |
1763 | /// Determine if the return value is marked with NoAlias attribute. |
1764 | bool returnDoesNotAlias() const { |
1765 | return Attrs.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias); |
1766 | } |
1767 | |
1768 | /// If one of the arguments has the 'returned' attribute, returns its |
1769 | /// operand value. Otherwise, return nullptr. |
1770 | Value *getReturnedArgOperand() const; |
1771 | |
1772 | /// Return true if the call should not be treated as a call to a |
1773 | /// builtin. |
1774 | bool isNoBuiltin() const { |
1775 | return hasFnAttrImpl(Attribute::NoBuiltin) && |
1776 | !hasFnAttrImpl(Attribute::Builtin); |
1777 | } |
1778 | |
1779 | /// Determine if the call requires strict floating point semantics. |
1780 | bool isStrictFP() const { return hasFnAttr(Attribute::StrictFP); } |
1781 | |
1782 | /// Return true if the call should not be inlined. |
1783 | bool isNoInline() const { return hasFnAttr(Attribute::NoInline); } |
1784 | void setIsNoInline() { |
1785 | addAttribute(AttributeList::FunctionIndex, Attribute::NoInline); |
1786 | } |
1787 | /// Determine if the call does not access memory. |
1788 | bool doesNotAccessMemory() const { return hasFnAttr(Attribute::ReadNone); } |
1789 | void setDoesNotAccessMemory() { |
1790 | addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone); |
1791 | } |
1792 | |
1793 | /// Determine if the call does not access or only reads memory. |
1794 | bool onlyReadsMemory() const { |
1795 | return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly); |
1796 | } |
1797 | |
1798 | void setOnlyReadsMemory() { |
1799 | addAttribute(AttributeList::FunctionIndex, Attribute::ReadOnly); |
1800 | } |
1801 | |
1802 | /// Determine if the call does not access or only writes memory. |
1803 | bool doesNotReadMemory() const { |
1804 | return doesNotAccessMemory() || hasFnAttr(Attribute::WriteOnly); |
1805 | } |
1806 | void setDoesNotReadMemory() { |
1807 | addAttribute(AttributeList::FunctionIndex, Attribute::WriteOnly); |
1808 | } |
1809 | |
1810 | /// Determine if the call can access memmory only using pointers based |
1811 | /// on its arguments. |
1812 | bool onlyAccessesArgMemory() const { |
1813 | return hasFnAttr(Attribute::ArgMemOnly); |
1814 | } |
1815 | void setOnlyAccessesArgMemory() { |
1816 | addAttribute(AttributeList::FunctionIndex, Attribute::ArgMemOnly); |
1817 | } |
1818 | |
1819 | /// Determine if the function may only access memory that is |
1820 | /// inaccessible from the IR. |
1821 | bool onlyAccessesInaccessibleMemory() const { |
1822 | return hasFnAttr(Attribute::InaccessibleMemOnly); |
1823 | } |
1824 | void setOnlyAccessesInaccessibleMemory() { |
1825 | addAttribute(AttributeList::FunctionIndex, Attribute::InaccessibleMemOnly); |
1826 | } |
1827 | |
1828 | /// Determine if the function may only access memory that is |
1829 | /// either inaccessible from the IR or pointed to by its arguments. |
1830 | bool onlyAccessesInaccessibleMemOrArgMem() const { |
1831 | return hasFnAttr(Attribute::InaccessibleMemOrArgMemOnly); |
1832 | } |
1833 | void setOnlyAccessesInaccessibleMemOrArgMem() { |
1834 | addAttribute(AttributeList::FunctionIndex, |
1835 | Attribute::InaccessibleMemOrArgMemOnly); |
1836 | } |
1837 | /// Determine if the call cannot return. |
1838 | bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); } |
1839 | void setDoesNotReturn() { |
1840 | addAttribute(AttributeList::FunctionIndex, Attribute::NoReturn); |
1841 | } |
1842 | |
1843 | /// Determine if the call should not perform indirect branch tracking. |
1844 | bool doesNoCfCheck() const { return hasFnAttr(Attribute::NoCfCheck); } |
1845 | |
1846 | /// Determine if the call cannot unwind. |
1847 | bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); } |
1848 | void setDoesNotThrow() { |
1849 | addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind); |
1850 | } |
1851 | |
1852 | /// Determine if the invoke cannot be duplicated. |
1853 | bool cannotDuplicate() const { return hasFnAttr(Attribute::NoDuplicate); } |
1854 | void setCannotDuplicate() { |
1855 | addAttribute(AttributeList::FunctionIndex, Attribute::NoDuplicate); |
1856 | } |
1857 | |
1858 | /// Determine if the call cannot be tail merged. |
1859 | bool cannotMerge() const { return hasFnAttr(Attribute::NoMerge); } |
1860 | void setCannotMerge() { |
1861 | addAttribute(AttributeList::FunctionIndex, Attribute::NoMerge); |
1862 | } |
1863 | |
1864 | /// Determine if the invoke is convergent |
1865 | bool isConvergent() const { return hasFnAttr(Attribute::Convergent); } |
1866 | void setConvergent() { |
1867 | addAttribute(AttributeList::FunctionIndex, Attribute::Convergent); |
1868 | } |
1869 | void setNotConvergent() { |
1870 | removeAttribute(AttributeList::FunctionIndex, Attribute::Convergent); |
1871 | } |
1872 | |
1873 | /// Determine if the call returns a structure through first |
1874 | /// pointer argument. |
1875 | bool hasStructRetAttr() const { |
1876 | if (getNumArgOperands() == 0) |
1877 | return false; |
1878 | |
1879 | // Be friendly and also check the callee. |
1880 | return paramHasAttr(0, Attribute::StructRet); |
1881 | } |
1882 | |
1883 | /// Determine if any call argument is an aggregate passed by value. |
1884 | bool hasByValArgument() const { |
1885 | return Attrs.hasAttrSomewhere(Attribute::ByVal); |
1886 | } |
1887 | |
1888 | ///@{ |
1889 | // End of attribute API. |
1890 | |
1891 | /// \name Operand Bundle API |
1892 | /// |
1893 | /// This group of methods provides the API to access and manipulate operand |
1894 | /// bundles on this call. |
1895 | /// @{ |
1896 | |
1897 | /// Return the number of operand bundles associated with this User. |
1898 | unsigned getNumOperandBundles() const { |
1899 | return std::distance(bundle_op_info_begin(), bundle_op_info_end()); |
1900 | } |
1901 | |
1902 | /// Return true if this User has any operand bundles. |
1903 | bool hasOperandBundles() const { return getNumOperandBundles() != 0; } |
1904 | |
1905 | /// Return the index of the first bundle operand in the Use array. |
1906 | unsigned getBundleOperandsStartIndex() const { |
1907 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1907, __PRETTY_FUNCTION__)); |
1908 | return bundle_op_info_begin()->Begin; |
1909 | } |
1910 | |
1911 | /// Return the index of the last bundle operand in the Use array. |
1912 | unsigned getBundleOperandsEndIndex() const { |
1913 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1913, __PRETTY_FUNCTION__)); |
1914 | return bundle_op_info_end()[-1].End; |
1915 | } |
1916 | |
1917 | /// Return true if the operand at index \p Idx is a bundle operand. |
1918 | bool isBundleOperand(unsigned Idx) const { |
1919 | return hasOperandBundles() && Idx >= getBundleOperandsStartIndex() && |
1920 | Idx < getBundleOperandsEndIndex(); |
1921 | } |
1922 | |
1923 | /// Returns true if the use is a bundle operand. |
1924 | bool isBundleOperand(const Use *U) const { |
1925 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1926, __PRETTY_FUNCTION__)) |
1926 | "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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1926, __PRETTY_FUNCTION__)); |
1927 | return hasOperandBundles() && isBundleOperand(U - op_begin()); |
1928 | } |
1929 | bool isBundleOperand(Value::const_user_iterator UI) const { |
1930 | return isBundleOperand(&UI.getUse()); |
1931 | } |
1932 | |
1933 | /// Return the total number operands (not operand bundles) used by |
1934 | /// every operand bundle in this OperandBundleUser. |
1935 | unsigned getNumTotalBundleOperands() const { |
1936 | if (!hasOperandBundles()) |
1937 | return 0; |
1938 | |
1939 | unsigned Begin = getBundleOperandsStartIndex(); |
1940 | unsigned End = getBundleOperandsEndIndex(); |
1941 | |
1942 | assert(Begin <= End && "Should be!")((Begin <= End && "Should be!") ? static_cast<void > (0) : __assert_fail ("Begin <= End && \"Should be!\"" , "/build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1942, __PRETTY_FUNCTION__)); |
1943 | return End - Begin; |
1944 | } |
1945 | |
1946 | /// Return the operand bundle at a specific index. |
1947 | OperandBundleUse getOperandBundleAt(unsigned Index) const { |
1948 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1948, __PRETTY_FUNCTION__)); |
1949 | return operandBundleFromBundleOpInfo(*(bundle_op_info_begin() + Index)); |
1950 | } |
1951 | |
1952 | /// Return the number of operand bundles with the tag Name attached to |
1953 | /// this instruction. |
1954 | unsigned countOperandBundlesOfType(StringRef Name) const { |
1955 | unsigned Count = 0; |
1956 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) |
1957 | if (getOperandBundleAt(i).getTagName() == Name) |
1958 | Count++; |
1959 | |
1960 | return Count; |
1961 | } |
1962 | |
1963 | /// Return the number of operand bundles with the tag ID attached to |
1964 | /// this instruction. |
1965 | unsigned countOperandBundlesOfType(uint32_t ID) const { |
1966 | unsigned Count = 0; |
1967 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) |
1968 | if (getOperandBundleAt(i).getTagID() == ID) |
1969 | Count++; |
1970 | |
1971 | return Count; |
1972 | } |
1973 | |
1974 | /// Return an operand bundle by name, if present. |
1975 | /// |
1976 | /// It is an error to call this for operand bundle types that may have |
1977 | /// multiple instances of them on the same instruction. |
1978 | Optional<OperandBundleUse> getOperandBundle(StringRef Name) const { |
1979 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1979, __PRETTY_FUNCTION__)); |
1980 | |
1981 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) { |
1982 | OperandBundleUse U = getOperandBundleAt(i); |
1983 | if (U.getTagName() == Name) |
1984 | return U; |
1985 | } |
1986 | |
1987 | return None; |
1988 | } |
1989 | |
1990 | /// Return an operand bundle by tag ID, if present. |
1991 | /// |
1992 | /// It is an error to call this for operand bundle types that may have |
1993 | /// multiple instances of them on the same instruction. |
1994 | Optional<OperandBundleUse> getOperandBundle(uint32_t ID) const { |
1995 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 1995, __PRETTY_FUNCTION__)); |
1996 | |
1997 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) { |
1998 | OperandBundleUse U = getOperandBundleAt(i); |
1999 | if (U.getTagID() == ID) |
2000 | return U; |
2001 | } |
2002 | |
2003 | return None; |
2004 | } |
2005 | |
2006 | /// Return the list of operand bundles attached to this instruction as |
2007 | /// a vector of OperandBundleDefs. |
2008 | /// |
2009 | /// This function copies the OperandBundeUse instances associated with this |
2010 | /// OperandBundleUser to a vector of OperandBundleDefs. Note: |
2011 | /// OperandBundeUses and OperandBundleDefs are non-trivially *different* |
2012 | /// representations of operand bundles (see documentation above). |
2013 | void getOperandBundlesAsDefs(SmallVectorImpl<OperandBundleDef> &Defs) const; |
2014 | |
2015 | /// Return the operand bundle for the operand at index OpIdx. |
2016 | /// |
2017 | /// It is an error to call this with an OpIdx that does not correspond to an |
2018 | /// bundle operand. |
2019 | OperandBundleUse getOperandBundleForOperand(unsigned OpIdx) const { |
2020 | return operandBundleFromBundleOpInfo(getBundleOpInfoForOperand(OpIdx)); |
2021 | } |
2022 | |
2023 | /// Return true if this operand bundle user has operand bundles that |
2024 | /// may read from the heap. |
2025 | bool hasReadingOperandBundles() const; |
2026 | |
2027 | /// Return true if this operand bundle user has operand bundles that |
2028 | /// may write to the heap. |
2029 | bool hasClobberingOperandBundles() const { |
2030 | for (auto &BOI : bundle_op_infos()) { |
2031 | if (BOI.Tag->second == LLVMContext::OB_deopt || |
2032 | BOI.Tag->second == LLVMContext::OB_funclet) |
2033 | continue; |
2034 | |
2035 | // This instruction has an operand bundle that is not known to us. |
2036 | // Assume the worst. |
2037 | return true; |
2038 | } |
2039 | |
2040 | return false; |
2041 | } |
2042 | |
2043 | /// Return true if the bundle operand at index \p OpIdx has the |
2044 | /// attribute \p A. |
2045 | bool bundleOperandHasAttr(unsigned OpIdx, Attribute::AttrKind A) const { |
2046 | auto &BOI = getBundleOpInfoForOperand(OpIdx); |
2047 | auto OBU = operandBundleFromBundleOpInfo(BOI); |
2048 | return OBU.operandHasAttr(OpIdx - BOI.Begin, A); |
2049 | } |
2050 | |
2051 | /// Return true if \p Other has the same sequence of operand bundle |
2052 | /// tags with the same number of operands on each one of them as this |
2053 | /// OperandBundleUser. |
2054 | bool hasIdenticalOperandBundleSchema(const CallBase &Other) const { |
2055 | if (getNumOperandBundles() != Other.getNumOperandBundles()) |
2056 | return false; |
2057 | |
2058 | return std::equal(bundle_op_info_begin(), bundle_op_info_end(), |
2059 | Other.bundle_op_info_begin()); |
2060 | } |
2061 | |
2062 | /// Return true if this operand bundle user contains operand bundles |
2063 | /// with tags other than those specified in \p IDs. |
2064 | bool hasOperandBundlesOtherThan(ArrayRef<uint32_t> IDs) const { |
2065 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) { |
2066 | uint32_t ID = getOperandBundleAt(i).getTagID(); |
2067 | if (!is_contained(IDs, ID)) |
2068 | return true; |
2069 | } |
2070 | return false; |
2071 | } |
2072 | |
2073 | /// Is the function attribute S disallowed by some operand bundle on |
2074 | /// this operand bundle user? |
2075 | bool isFnAttrDisallowedByOpBundle(StringRef S) const { |
2076 | // Operand bundles only possibly disallow readnone, readonly and argmemonly |
2077 | // attributes. All String attributes are fine. |
2078 | return false; |
2079 | } |
2080 | |
2081 | /// Is the function attribute A disallowed by some operand bundle on |
2082 | /// this operand bundle user? |
2083 | bool isFnAttrDisallowedByOpBundle(Attribute::AttrKind A) const { |
2084 | switch (A) { |
2085 | default: |
2086 | return false; |
2087 | |
2088 | case Attribute::InaccessibleMemOrArgMemOnly: |
2089 | return hasReadingOperandBundles(); |
2090 | |
2091 | case Attribute::InaccessibleMemOnly: |
2092 | return hasReadingOperandBundles(); |
2093 | |
2094 | case Attribute::ArgMemOnly: |
2095 | return hasReadingOperandBundles(); |
2096 | |
2097 | case Attribute::ReadNone: |
2098 | return hasReadingOperandBundles(); |
2099 | |
2100 | case Attribute::ReadOnly: |
2101 | return hasClobberingOperandBundles(); |
2102 | } |
2103 | |
2104 | llvm_unreachable("switch has a default case!")::llvm::llvm_unreachable_internal("switch has a default case!" , "/build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 2104); |
2105 | } |
2106 | |
2107 | /// Used to keep track of an operand bundle. See the main comment on |
2108 | /// OperandBundleUser above. |
2109 | struct BundleOpInfo { |
2110 | /// The operand bundle tag, interned by |
2111 | /// LLVMContextImpl::getOrInsertBundleTag. |
2112 | StringMapEntry<uint32_t> *Tag; |
2113 | |
2114 | /// The index in the Use& vector where operands for this operand |
2115 | /// bundle starts. |
2116 | uint32_t Begin; |
2117 | |
2118 | /// The index in the Use& vector where operands for this operand |
2119 | /// bundle ends. |
2120 | uint32_t End; |
2121 | |
2122 | bool operator==(const BundleOpInfo &Other) const { |
2123 | return Tag == Other.Tag && Begin == Other.Begin && End == Other.End; |
2124 | } |
2125 | }; |
2126 | |
2127 | /// Simple helper function to map a BundleOpInfo to an |
2128 | /// OperandBundleUse. |
2129 | OperandBundleUse |
2130 | operandBundleFromBundleOpInfo(const BundleOpInfo &BOI) const { |
2131 | auto begin = op_begin(); |
2132 | ArrayRef<Use> Inputs(begin + BOI.Begin, begin + BOI.End); |
2133 | return OperandBundleUse(BOI.Tag, Inputs); |
2134 | } |
2135 | |
2136 | using bundle_op_iterator = BundleOpInfo *; |
2137 | using const_bundle_op_iterator = const BundleOpInfo *; |
2138 | |
2139 | /// Return the start of the list of BundleOpInfo instances associated |
2140 | /// with this OperandBundleUser. |
2141 | /// |
2142 | /// OperandBundleUser uses the descriptor area co-allocated with the host User |
2143 | /// to store some meta information about which operands are "normal" operands, |
2144 | /// and which ones belong to some operand bundle. |
2145 | /// |
2146 | /// The layout of an operand bundle user is |
2147 | /// |
2148 | /// +-----------uint32_t End-------------------------------------+ |
2149 | /// | | |
2150 | /// | +--------uint32_t Begin--------------------+ | |
2151 | /// | | | | |
2152 | /// ^ ^ v v |
2153 | /// |------|------|----|----|----|----|----|---------|----|---------|----|----- |
2154 | /// | BOI0 | BOI1 | .. | DU | U0 | U1 | .. | BOI0_U0 | .. | BOI1_U0 | .. | Un |
2155 | /// |------|------|----|----|----|----|----|---------|----|---------|----|----- |
2156 | /// v v ^ ^ |
2157 | /// | | | | |
2158 | /// | +--------uint32_t Begin------------+ | |
2159 | /// | | |
2160 | /// +-----------uint32_t End-----------------------------+ |
2161 | /// |
2162 | /// |
2163 | /// BOI0, BOI1 ... are descriptions of operand bundles in this User's use |
2164 | /// list. These descriptions are installed and managed by this class, and |
2165 | /// they're all instances of OperandBundleUser<T>::BundleOpInfo. |
2166 | /// |
2167 | /// DU is an additional descriptor installed by User's 'operator new' to keep |
2168 | /// track of the 'BOI0 ... BOIN' co-allocation. OperandBundleUser does not |
2169 | /// access or modify DU in any way, it's an implementation detail private to |
2170 | /// User. |
2171 | /// |
2172 | /// The regular Use& vector for the User starts at U0. The operand bundle |
2173 | /// uses are part of the Use& vector, just like normal uses. In the diagram |
2174 | /// above, the operand bundle uses start at BOI0_U0. Each instance of |
2175 | /// BundleOpInfo has information about a contiguous set of uses constituting |
2176 | /// an operand bundle, and the total set of operand bundle uses themselves |
2177 | /// form a contiguous set of uses (i.e. there are no gaps between uses |
2178 | /// corresponding to individual operand bundles). |
2179 | /// |
2180 | /// This class does not know the location of the set of operand bundle uses |
2181 | /// within the use list -- that is decided by the User using this class via |
2182 | /// the BeginIdx argument in populateBundleOperandInfos. |
2183 | /// |
2184 | /// Currently operand bundle users with hung-off operands are not supported. |
2185 | bundle_op_iterator bundle_op_info_begin() { |
2186 | if (!hasDescriptor()) |
2187 | return nullptr; |
2188 | |
2189 | uint8_t *BytesBegin = getDescriptor().begin(); |
2190 | return reinterpret_cast<bundle_op_iterator>(BytesBegin); |
2191 | } |
2192 | |
2193 | /// Return the start of the list of BundleOpInfo instances associated |
2194 | /// with this OperandBundleUser. |
2195 | const_bundle_op_iterator bundle_op_info_begin() const { |
2196 | auto *NonConstThis = const_cast<CallBase *>(this); |
2197 | return NonConstThis->bundle_op_info_begin(); |
2198 | } |
2199 | |
2200 | /// Return the end of the list of BundleOpInfo instances associated |
2201 | /// with this OperandBundleUser. |
2202 | bundle_op_iterator bundle_op_info_end() { |
2203 | if (!hasDescriptor()) |
2204 | return nullptr; |
2205 | |
2206 | uint8_t *BytesEnd = getDescriptor().end(); |
2207 | return reinterpret_cast<bundle_op_iterator>(BytesEnd); |
2208 | } |
2209 | |
2210 | /// Return the end of the list of BundleOpInfo instances associated |
2211 | /// with this OperandBundleUser. |
2212 | const_bundle_op_iterator bundle_op_info_end() const { |
2213 | auto *NonConstThis = const_cast<CallBase *>(this); |
2214 | return NonConstThis->bundle_op_info_end(); |
2215 | } |
2216 | |
2217 | /// Return the range [\p bundle_op_info_begin, \p bundle_op_info_end). |
2218 | iterator_range<bundle_op_iterator> bundle_op_infos() { |
2219 | return make_range(bundle_op_info_begin(), bundle_op_info_end()); |
2220 | } |
2221 | |
2222 | /// Return the range [\p bundle_op_info_begin, \p bundle_op_info_end). |
2223 | iterator_range<const_bundle_op_iterator> bundle_op_infos() const { |
2224 | return make_range(bundle_op_info_begin(), bundle_op_info_end()); |
2225 | } |
2226 | |
2227 | /// Populate the BundleOpInfo instances and the Use& vector from \p |
2228 | /// Bundles. Return the op_iterator pointing to the Use& one past the last |
2229 | /// last bundle operand use. |
2230 | /// |
2231 | /// Each \p OperandBundleDef instance is tracked by a OperandBundleInfo |
2232 | /// instance allocated in this User's descriptor. |
2233 | op_iterator populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles, |
2234 | const unsigned BeginIndex); |
2235 | |
2236 | public: |
2237 | /// Return the BundleOpInfo for the operand at index OpIdx. |
2238 | /// |
2239 | /// It is an error to call this with an OpIdx that does not correspond to an |
2240 | /// bundle operand. |
2241 | BundleOpInfo &getBundleOpInfoForOperand(unsigned OpIdx); |
2242 | const BundleOpInfo &getBundleOpInfoForOperand(unsigned OpIdx) const { |
2243 | return const_cast<CallBase *>(this)->getBundleOpInfoForOperand(OpIdx); |
2244 | } |
2245 | |
2246 | protected: |
2247 | /// Return the total number of values used in \p Bundles. |
2248 | static unsigned CountBundleInputs(ArrayRef<OperandBundleDef> Bundles) { |
2249 | unsigned Total = 0; |
2250 | for (auto &B : Bundles) |
2251 | Total += B.input_size(); |
2252 | return Total; |
2253 | } |
2254 | |
2255 | /// @} |
2256 | // End of operand bundle API. |
2257 | |
2258 | private: |
2259 | bool hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const; |
2260 | bool hasFnAttrOnCalledFunction(StringRef Kind) const; |
2261 | |
2262 | template <typename AttrKind> bool hasFnAttrImpl(AttrKind Kind) const { |
2263 | if (Attrs.hasFnAttribute(Kind)) |
2264 | return true; |
2265 | |
2266 | // Operand bundles override attributes on the called function, but don't |
2267 | // override attributes directly present on the call instruction. |
2268 | if (isFnAttrDisallowedByOpBundle(Kind)) |
2269 | return false; |
2270 | |
2271 | return hasFnAttrOnCalledFunction(Kind); |
2272 | } |
2273 | |
2274 | /// Determine whether the return value has the given attribute. Supports |
2275 | /// Attribute::AttrKind and StringRef as \p AttrKind types. |
2276 | template <typename AttrKind> bool hasRetAttrImpl(AttrKind Kind) const { |
2277 | if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind)) |
2278 | return true; |
2279 | |
2280 | // Look at the callee, if available. |
2281 | if (const Function *F = getCalledFunction()) |
2282 | return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind); |
2283 | return false; |
2284 | } |
2285 | }; |
2286 | |
2287 | template <> |
2288 | struct OperandTraits<CallBase> : public VariadicOperandTraits<CallBase, 1> {}; |
2289 | |
2290 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 2290, __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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 2290, __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 ); } |
2291 | |
2292 | //===----------------------------------------------------------------------===// |
2293 | // FuncletPadInst Class |
2294 | //===----------------------------------------------------------------------===// |
2295 | class FuncletPadInst : public Instruction { |
2296 | private: |
2297 | FuncletPadInst(const FuncletPadInst &CPI); |
2298 | |
2299 | explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
2300 | ArrayRef<Value *> Args, unsigned Values, |
2301 | const Twine &NameStr, Instruction *InsertBefore); |
2302 | explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
2303 | ArrayRef<Value *> Args, unsigned Values, |
2304 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
2305 | |
2306 | void init(Value *ParentPad, ArrayRef<Value *> Args, const Twine &NameStr); |
2307 | |
2308 | protected: |
2309 | // Note: Instruction needs to be a friend here to call cloneImpl. |
2310 | friend class Instruction; |
2311 | friend class CatchPadInst; |
2312 | friend class CleanupPadInst; |
2313 | |
2314 | FuncletPadInst *cloneImpl() const; |
2315 | |
2316 | public: |
2317 | /// Provide fast operand accessors |
2318 | 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; |
2319 | |
2320 | /// getNumArgOperands - Return the number of funcletpad arguments. |
2321 | /// |
2322 | unsigned getNumArgOperands() const { return getNumOperands() - 1; } |
2323 | |
2324 | /// Convenience accessors |
2325 | |
2326 | /// Return the outer EH-pad this funclet is nested within. |
2327 | /// |
2328 | /// Note: This returns the associated CatchSwitchInst if this FuncletPadInst |
2329 | /// is a CatchPadInst. |
2330 | Value *getParentPad() const { return Op<-1>(); } |
2331 | void setParentPad(Value *ParentPad) { |
2332 | assert(ParentPad)((ParentPad) ? static_cast<void> (0) : __assert_fail ("ParentPad" , "/build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 2332, __PRETTY_FUNCTION__)); |
2333 | Op<-1>() = ParentPad; |
2334 | } |
2335 | |
2336 | /// getArgOperand/setArgOperand - Return/set the i-th funcletpad argument. |
2337 | /// |
2338 | Value *getArgOperand(unsigned i) const { return getOperand(i); } |
2339 | void setArgOperand(unsigned i, Value *v) { setOperand(i, v); } |
2340 | |
2341 | /// arg_operands - iteration adapter for range-for loops. |
2342 | op_range arg_operands() { return op_range(op_begin(), op_end() - 1); } |
2343 | |
2344 | /// arg_operands - iteration adapter for range-for loops. |
2345 | const_op_range arg_operands() const { |
2346 | return const_op_range(op_begin(), op_end() - 1); |
2347 | } |
2348 | |
2349 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
2350 | static bool classof(const Instruction *I) { return I->isFuncletPad(); } |
2351 | static bool classof(const Value *V) { |
2352 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
2353 | } |
2354 | }; |
2355 | |
2356 | template <> |
2357 | struct OperandTraits<FuncletPadInst> |
2358 | : public VariadicOperandTraits<FuncletPadInst, /*MINARITY=*/1> {}; |
2359 | |
2360 | 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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 2360, __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-13~++20210405022414+5f57793c4fe4/llvm/include/llvm/IR/InstrTypes.h" , 2360, __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); } |
2361 | |
2362 | } // end namespace llvm |
2363 | |
2364 | #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-13~++20210405022414+5f57793c4fe4/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-13~++20210405022414+5f57793c4fe4/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 |