File: | build/llvm-toolchain-snapshot-15~++20220407100720+1c9415806ba6/llvm/lib/Transforms/Scalar/LICM.cpp |
Warning: | line 1251, 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/PriorityWorklist.h" | ||||||||||
41 | #include "llvm/ADT/SetOperations.h" | ||||||||||
42 | #include "llvm/ADT/Statistic.h" | ||||||||||
43 | #include "llvm/Analysis/AliasAnalysis.h" | ||||||||||
44 | #include "llvm/Analysis/AliasSetTracker.h" | ||||||||||
45 | #include "llvm/Analysis/CaptureTracking.h" | ||||||||||
46 | #include "llvm/Analysis/ConstantFolding.h" | ||||||||||
47 | #include "llvm/Analysis/GuardUtils.h" | ||||||||||
48 | #include "llvm/Analysis/LazyBlockFrequencyInfo.h" | ||||||||||
49 | #include "llvm/Analysis/Loads.h" | ||||||||||
50 | #include "llvm/Analysis/LoopInfo.h" | ||||||||||
51 | #include "llvm/Analysis/LoopIterator.h" | ||||||||||
52 | #include "llvm/Analysis/LoopNestAnalysis.h" | ||||||||||
53 | #include "llvm/Analysis/LoopPass.h" | ||||||||||
54 | #include "llvm/Analysis/MemorySSA.h" | ||||||||||
55 | #include "llvm/Analysis/MemorySSAUpdater.h" | ||||||||||
56 | #include "llvm/Analysis/MustExecute.h" | ||||||||||
57 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" | ||||||||||
58 | #include "llvm/Analysis/ScalarEvolution.h" | ||||||||||
59 | #include "llvm/Analysis/TargetLibraryInfo.h" | ||||||||||
60 | #include "llvm/Analysis/TargetTransformInfo.h" | ||||||||||
61 | #include "llvm/Analysis/ValueTracking.h" | ||||||||||
62 | #include "llvm/IR/CFG.h" | ||||||||||
63 | #include "llvm/IR/Constants.h" | ||||||||||
64 | #include "llvm/IR/DataLayout.h" | ||||||||||
65 | #include "llvm/IR/DebugInfoMetadata.h" | ||||||||||
66 | #include "llvm/IR/DerivedTypes.h" | ||||||||||
67 | #include "llvm/IR/Dominators.h" | ||||||||||
68 | #include "llvm/IR/Instructions.h" | ||||||||||
69 | #include "llvm/IR/IntrinsicInst.h" | ||||||||||
70 | #include "llvm/IR/LLVMContext.h" | ||||||||||
71 | #include "llvm/IR/Metadata.h" | ||||||||||
72 | #include "llvm/IR/PatternMatch.h" | ||||||||||
73 | #include "llvm/IR/PredIteratorCache.h" | ||||||||||
74 | #include "llvm/InitializePasses.h" | ||||||||||
75 | #include "llvm/Support/CommandLine.h" | ||||||||||
76 | #include "llvm/Support/Debug.h" | ||||||||||
77 | #include "llvm/Support/raw_ostream.h" | ||||||||||
78 | #include "llvm/Transforms/Scalar.h" | ||||||||||
79 | #include "llvm/Transforms/Utils/AssumeBundleBuilder.h" | ||||||||||
80 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" | ||||||||||
81 | #include "llvm/Transforms/Utils/Local.h" | ||||||||||
82 | #include "llvm/Transforms/Utils/LoopUtils.h" | ||||||||||
83 | #include "llvm/Transforms/Utils/SSAUpdater.h" | ||||||||||
84 | #include <algorithm> | ||||||||||
85 | #include <utility> | ||||||||||
86 | using namespace llvm; | ||||||||||
87 | |||||||||||
88 | namespace llvm { | ||||||||||
89 | class BlockFrequencyInfo; | ||||||||||
90 | class LPMUpdater; | ||||||||||
91 | } // namespace llvm | ||||||||||
92 | |||||||||||
93 | #define DEBUG_TYPE"licm" "licm" | ||||||||||
94 | |||||||||||
95 | STATISTIC(NumCreatedBlocks, "Number of blocks created")static llvm::Statistic NumCreatedBlocks = {"licm", "NumCreatedBlocks" , "Number of blocks created"}; | ||||||||||
96 | STATISTIC(NumClonedBranches, "Number of branches cloned")static llvm::Statistic NumClonedBranches = {"licm", "NumClonedBranches" , "Number of branches cloned"}; | ||||||||||
97 | STATISTIC(NumSunk, "Number of instructions sunk out of loop")static llvm::Statistic NumSunk = {"licm", "NumSunk", "Number of instructions sunk out of loop" }; | ||||||||||
98 | STATISTIC(NumHoisted, "Number of instructions hoisted out of loop")static llvm::Statistic NumHoisted = {"licm", "NumHoisted", "Number of instructions hoisted out of loop" }; | ||||||||||
99 | STATISTIC(NumMovedLoads, "Number of load insts hoisted or sunk")static llvm::Statistic NumMovedLoads = {"licm", "NumMovedLoads" , "Number of load insts hoisted or sunk"}; | ||||||||||
100 | STATISTIC(NumMovedCalls, "Number of call insts hoisted or sunk")static llvm::Statistic NumMovedCalls = {"licm", "NumMovedCalls" , "Number of call insts hoisted or sunk"}; | ||||||||||
101 | STATISTIC(NumPromoted, "Number of memory locations promoted to registers")static llvm::Statistic NumPromoted = {"licm", "NumPromoted", "Number of memory locations promoted to registers" }; | ||||||||||
102 | |||||||||||
103 | /// Memory promotion is enabled by default. | ||||||||||
104 | static cl::opt<bool> | ||||||||||
105 | DisablePromotion("disable-licm-promotion", cl::Hidden, cl::init(false), | ||||||||||
106 | cl::desc("Disable memory promotion in LICM pass")); | ||||||||||
107 | |||||||||||
108 | static cl::opt<bool> ControlFlowHoisting( | ||||||||||
109 | "licm-control-flow-hoisting", cl::Hidden, cl::init(false), | ||||||||||
110 | cl::desc("Enable control flow (and PHI) hoisting in LICM")); | ||||||||||
111 | |||||||||||
112 | static cl::opt<uint32_t> MaxNumUsesTraversed( | ||||||||||
113 | "licm-max-num-uses-traversed", cl::Hidden, cl::init(8), | ||||||||||
114 | cl::desc("Max num uses visited for identifying load " | ||||||||||
115 | "invariance in loop using invariant start (default = 8)")); | ||||||||||
116 | |||||||||||
117 | // Experimental option to allow imprecision in LICM in pathological cases, in | ||||||||||
118 | // exchange for faster compile. This is to be removed if MemorySSA starts to | ||||||||||
119 | // address the same issue. This flag applies only when LICM uses MemorySSA | ||||||||||
120 | // instead on AliasSetTracker. LICM calls MemorySSAWalker's | ||||||||||
121 | // getClobberingMemoryAccess, up to the value of the Cap, getting perfect | ||||||||||
122 | // accuracy. Afterwards, LICM will call into MemorySSA's getDefiningAccess, | ||||||||||
123 | // which may not be precise, since optimizeUses is capped. The result is | ||||||||||
124 | // correct, but we may not get as "far up" as possible to get which access is | ||||||||||
125 | // clobbering the one queried. | ||||||||||
126 | cl::opt<unsigned> llvm::SetLicmMssaOptCap( | ||||||||||
127 | "licm-mssa-optimization-cap", cl::init(100), cl::Hidden, | ||||||||||
128 | cl::desc("Enable imprecision in LICM in pathological cases, in exchange " | ||||||||||
129 | "for faster compile. Caps the MemorySSA clobbering calls.")); | ||||||||||
130 | |||||||||||
131 | // Experimentally, memory promotion carries less importance than sinking and | ||||||||||
132 | // hoisting. Limit when we do promotion when using MemorySSA, in order to save | ||||||||||
133 | // compile time. | ||||||||||
134 | cl::opt<unsigned> llvm::SetLicmMssaNoAccForPromotionCap( | ||||||||||
135 | "licm-mssa-max-acc-promotion", cl::init(250), cl::Hidden, | ||||||||||
136 | cl::desc("[LICM & MemorySSA] When MSSA in LICM is disabled, this has no " | ||||||||||
137 | "effect. When MSSA in LICM is enabled, then this is the maximum " | ||||||||||
138 | "number of accesses allowed to be present in a loop in order to " | ||||||||||
139 | "enable memory promotion.")); | ||||||||||
140 | |||||||||||
141 | static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI); | ||||||||||
142 | static bool isNotUsedOrFreeInLoop(const Instruction &I, const Loop *CurLoop, | ||||||||||
143 | const LoopSafetyInfo *SafetyInfo, | ||||||||||
144 | TargetTransformInfo *TTI, bool &FreeInLoop, | ||||||||||
145 | bool LoopNestMode); | ||||||||||
146 | static void hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop, | ||||||||||
147 | BasicBlock *Dest, ICFLoopSafetyInfo *SafetyInfo, | ||||||||||
148 | MemorySSAUpdater *MSSAU, ScalarEvolution *SE, | ||||||||||
149 | OptimizationRemarkEmitter *ORE); | ||||||||||
150 | static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT, | ||||||||||
151 | BlockFrequencyInfo *BFI, const Loop *CurLoop, | ||||||||||
152 | ICFLoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU, | ||||||||||
153 | OptimizationRemarkEmitter *ORE); | ||||||||||
154 | static bool isSafeToExecuteUnconditionally( | ||||||||||
155 | Instruction &Inst, const DominatorTree *DT, const TargetLibraryInfo *TLI, | ||||||||||
156 | const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo, | ||||||||||
157 | OptimizationRemarkEmitter *ORE, const Instruction *CtxI, | ||||||||||
158 | bool AllowSpeculation); | ||||||||||
159 | static bool pointerInvalidatedByLoop(MemoryLocation MemLoc, | ||||||||||
160 | AliasSetTracker *CurAST, Loop *CurLoop, | ||||||||||
161 | AAResults *AA); | ||||||||||
162 | static bool pointerInvalidatedByLoopWithMSSA(MemorySSA *MSSA, MemoryUse *MU, | ||||||||||
163 | Loop *CurLoop, Instruction &I, | ||||||||||
164 | SinkAndHoistLICMFlags &Flags); | ||||||||||
165 | static bool pointerInvalidatedByBlockWithMSSA(BasicBlock &BB, MemorySSA &MSSA, | ||||||||||
166 | MemoryUse &MU); | ||||||||||
167 | static Instruction *cloneInstructionInExitBlock( | ||||||||||
168 | Instruction &I, BasicBlock &ExitBlock, PHINode &PN, const LoopInfo *LI, | ||||||||||
169 | const LoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU); | ||||||||||
170 | |||||||||||
171 | static void eraseInstruction(Instruction &I, ICFLoopSafetyInfo &SafetyInfo, | ||||||||||
172 | MemorySSAUpdater *MSSAU); | ||||||||||
173 | |||||||||||
174 | static void moveInstructionBefore(Instruction &I, Instruction &Dest, | ||||||||||
175 | ICFLoopSafetyInfo &SafetyInfo, | ||||||||||
176 | MemorySSAUpdater *MSSAU, ScalarEvolution *SE); | ||||||||||
177 | |||||||||||
178 | static void foreachMemoryAccess(MemorySSA *MSSA, Loop *L, | ||||||||||
179 | function_ref<void(Instruction *)> Fn); | ||||||||||
180 | static SmallVector<SmallSetVector<Value *, 8>, 0> | ||||||||||
181 | collectPromotionCandidates(MemorySSA *MSSA, AliasAnalysis *AA, Loop *L); | ||||||||||
182 | |||||||||||
183 | namespace { | ||||||||||
184 | struct LoopInvariantCodeMotion { | ||||||||||
185 | bool runOnLoop(Loop *L, AAResults *AA, LoopInfo *LI, DominatorTree *DT, | ||||||||||
186 | BlockFrequencyInfo *BFI, TargetLibraryInfo *TLI, | ||||||||||
187 | TargetTransformInfo *TTI, ScalarEvolution *SE, MemorySSA *MSSA, | ||||||||||
188 | OptimizationRemarkEmitter *ORE, bool LoopNestMode = false); | ||||||||||
189 | |||||||||||
190 | LoopInvariantCodeMotion(unsigned LicmMssaOptCap, | ||||||||||
191 | unsigned LicmMssaNoAccForPromotionCap, | ||||||||||
192 | bool LicmAllowSpeculation) | ||||||||||
193 | : LicmMssaOptCap(LicmMssaOptCap), | ||||||||||
194 | LicmMssaNoAccForPromotionCap(LicmMssaNoAccForPromotionCap), | ||||||||||
195 | LicmAllowSpeculation(LicmAllowSpeculation) {} | ||||||||||
196 | |||||||||||
197 | private: | ||||||||||
198 | unsigned LicmMssaOptCap; | ||||||||||
199 | unsigned LicmMssaNoAccForPromotionCap; | ||||||||||
200 | bool LicmAllowSpeculation; | ||||||||||
201 | }; | ||||||||||
202 | |||||||||||
203 | struct LegacyLICMPass : public LoopPass { | ||||||||||
204 | static char ID; // Pass identification, replacement for typeid | ||||||||||
205 | LegacyLICMPass( | ||||||||||
206 | unsigned LicmMssaOptCap = SetLicmMssaOptCap, | ||||||||||
207 | unsigned LicmMssaNoAccForPromotionCap = SetLicmMssaNoAccForPromotionCap, | ||||||||||
208 | bool LicmAllowSpeculation = true) | ||||||||||
209 | : LoopPass(ID), LICM(LicmMssaOptCap, LicmMssaNoAccForPromotionCap, | ||||||||||
210 | LicmAllowSpeculation) { | ||||||||||
211 | initializeLegacyLICMPassPass(*PassRegistry::getPassRegistry()); | ||||||||||
212 | } | ||||||||||
213 | |||||||||||
214 | bool runOnLoop(Loop *L, LPPassManager &LPM) override { | ||||||||||
215 | if (skipLoop(L)) | ||||||||||
216 | return false; | ||||||||||
217 | |||||||||||
218 | 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) | ||||||||||
219 | << 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); | ||||||||||
220 | |||||||||||
221 | auto *SE = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>(); | ||||||||||
222 | MemorySSA *MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA(); | ||||||||||
223 | bool hasProfileData = L->getHeader()->getParent()->hasProfileData(); | ||||||||||
224 | BlockFrequencyInfo *BFI = | ||||||||||
225 | hasProfileData ? &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI() | ||||||||||
226 | : nullptr; | ||||||||||
227 | // For the old PM, we can't use OptimizationRemarkEmitter as an analysis | ||||||||||
228 | // pass. Function analyses need to be preserved across loop transformations | ||||||||||
229 | // but ORE cannot be preserved (see comment before the pass definition). | ||||||||||
230 | OptimizationRemarkEmitter ORE(L->getHeader()->getParent()); | ||||||||||
231 | return LICM.runOnLoop( | ||||||||||
232 | L, &getAnalysis<AAResultsWrapperPass>().getAAResults(), | ||||||||||
233 | &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(), | ||||||||||
234 | &getAnalysis<DominatorTreeWrapperPass>().getDomTree(), BFI, | ||||||||||
235 | &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI( | ||||||||||
236 | *L->getHeader()->getParent()), | ||||||||||
237 | &getAnalysis<TargetTransformInfoWrapperPass>().getTTI( | ||||||||||
238 | *L->getHeader()->getParent()), | ||||||||||
239 | SE ? &SE->getSE() : nullptr, MSSA, &ORE); | ||||||||||
240 | } | ||||||||||
241 | |||||||||||
242 | /// This transformation requires natural loop information & requires that | ||||||||||
243 | /// loop preheaders be inserted into the CFG... | ||||||||||
244 | /// | ||||||||||
245 | void getAnalysisUsage(AnalysisUsage &AU) const override { | ||||||||||
246 | AU.addPreserved<DominatorTreeWrapperPass>(); | ||||||||||
247 | AU.addPreserved<LoopInfoWrapperPass>(); | ||||||||||
248 | AU.addRequired<TargetLibraryInfoWrapperPass>(); | ||||||||||
249 | AU.addRequired<MemorySSAWrapperPass>(); | ||||||||||
250 | AU.addPreserved<MemorySSAWrapperPass>(); | ||||||||||
251 | AU.addRequired<TargetTransformInfoWrapperPass>(); | ||||||||||
252 | getLoopAnalysisUsage(AU); | ||||||||||
253 | LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU); | ||||||||||
254 | AU.addPreserved<LazyBlockFrequencyInfoPass>(); | ||||||||||
255 | AU.addPreserved<LazyBranchProbabilityInfoPass>(); | ||||||||||
256 | } | ||||||||||
257 | |||||||||||
258 | private: | ||||||||||
259 | LoopInvariantCodeMotion LICM; | ||||||||||
260 | }; | ||||||||||
261 | } // namespace | ||||||||||
262 | |||||||||||
263 | PreservedAnalyses LICMPass::run(Loop &L, LoopAnalysisManager &AM, | ||||||||||
264 | LoopStandardAnalysisResults &AR, LPMUpdater &) { | ||||||||||
265 | if (!AR.MSSA) | ||||||||||
266 | report_fatal_error("LICM requires MemorySSA (loop-mssa)"); | ||||||||||
267 | |||||||||||
268 | // For the new PM, we also can't use OptimizationRemarkEmitter as an analysis | ||||||||||
269 | // pass. Function analyses need to be preserved across loop transformations | ||||||||||
270 | // but ORE cannot be preserved (see comment before the pass definition). | ||||||||||
271 | OptimizationRemarkEmitter ORE(L.getHeader()->getParent()); | ||||||||||
272 | |||||||||||
273 | LoopInvariantCodeMotion LICM(Opts.MssaOptCap, Opts.MssaNoAccForPromotionCap, | ||||||||||
274 | Opts.AllowSpeculation); | ||||||||||
275 | if (!LICM.runOnLoop(&L, &AR.AA, &AR.LI, &AR.DT, AR.BFI, &AR.TLI, &AR.TTI, | ||||||||||
276 | &AR.SE, AR.MSSA, &ORE)) | ||||||||||
277 | return PreservedAnalyses::all(); | ||||||||||
278 | |||||||||||
279 | auto PA = getLoopPassPreservedAnalyses(); | ||||||||||
280 | |||||||||||
281 | PA.preserve<DominatorTreeAnalysis>(); | ||||||||||
282 | PA.preserve<LoopAnalysis>(); | ||||||||||
283 | PA.preserve<MemorySSAAnalysis>(); | ||||||||||
284 | |||||||||||
285 | return PA; | ||||||||||
286 | } | ||||||||||
287 | |||||||||||
288 | void LICMPass::printPipeline( | ||||||||||
289 | raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) { | ||||||||||
290 | static_cast<PassInfoMixin<LICMPass> *>(this)->printPipeline( | ||||||||||
291 | OS, MapClassName2PassName); | ||||||||||
292 | |||||||||||
293 | OS << "<"; | ||||||||||
294 | OS << (Opts.AllowSpeculation ? "" : "no-") << "allowspeculation"; | ||||||||||
295 | OS << ">"; | ||||||||||
296 | } | ||||||||||
297 | |||||||||||
298 | PreservedAnalyses LNICMPass::run(LoopNest &LN, LoopAnalysisManager &AM, | ||||||||||
299 | LoopStandardAnalysisResults &AR, | ||||||||||
300 | LPMUpdater &) { | ||||||||||
301 | if (!AR.MSSA) | ||||||||||
302 | report_fatal_error("LNICM requires MemorySSA (loop-mssa)"); | ||||||||||
303 | |||||||||||
304 | // For the new PM, we also can't use OptimizationRemarkEmitter as an analysis | ||||||||||
305 | // pass. Function analyses need to be preserved across loop transformations | ||||||||||
306 | // but ORE cannot be preserved (see comment before the pass definition). | ||||||||||
307 | OptimizationRemarkEmitter ORE(LN.getParent()); | ||||||||||
308 | |||||||||||
309 | LoopInvariantCodeMotion LICM(Opts.MssaOptCap, Opts.MssaNoAccForPromotionCap, | ||||||||||
310 | Opts.AllowSpeculation); | ||||||||||
311 | |||||||||||
312 | Loop &OutermostLoop = LN.getOutermostLoop(); | ||||||||||
313 | bool Changed = LICM.runOnLoop(&OutermostLoop, &AR.AA, &AR.LI, &AR.DT, AR.BFI, | ||||||||||
314 | &AR.TLI, &AR.TTI, &AR.SE, AR.MSSA, &ORE, true); | ||||||||||
315 | |||||||||||
316 | if (!Changed) | ||||||||||
317 | return PreservedAnalyses::all(); | ||||||||||
318 | |||||||||||
319 | auto PA = getLoopPassPreservedAnalyses(); | ||||||||||
320 | |||||||||||
321 | PA.preserve<DominatorTreeAnalysis>(); | ||||||||||
322 | PA.preserve<LoopAnalysis>(); | ||||||||||
323 | PA.preserve<MemorySSAAnalysis>(); | ||||||||||
324 | |||||||||||
325 | return PA; | ||||||||||
326 | } | ||||||||||
327 | |||||||||||
328 | void LNICMPass::printPipeline( | ||||||||||
329 | raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) { | ||||||||||
330 | static_cast<PassInfoMixin<LNICMPass> *>(this)->printPipeline( | ||||||||||
331 | OS, MapClassName2PassName); | ||||||||||
332 | |||||||||||
333 | OS << "<"; | ||||||||||
334 | OS << (Opts.AllowSpeculation ? "" : "no-") << "allowspeculation"; | ||||||||||
335 | OS << ">"; | ||||||||||
336 | } | ||||||||||
337 | |||||||||||
338 | char LegacyLICMPass::ID = 0; | ||||||||||
339 | INITIALIZE_PASS_BEGIN(LegacyLICMPass, "licm", "Loop Invariant Code Motion",static void *initializeLegacyLICMPassPassOnce(PassRegistry & Registry) { | ||||||||||
340 | false, false)static void *initializeLegacyLICMPassPassOnce(PassRegistry & Registry) { | ||||||||||
341 | INITIALIZE_PASS_DEPENDENCY(LoopPass)initializeLoopPassPass(Registry); | ||||||||||
342 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)initializeTargetLibraryInfoWrapperPassPass(Registry); | ||||||||||
343 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)initializeTargetTransformInfoWrapperPassPass(Registry); | ||||||||||
344 | INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)initializeMemorySSAWrapperPassPass(Registry); | ||||||||||
345 | INITIALIZE_PASS_DEPENDENCY(LazyBFIPass)initializeLazyBFIPassPass(Registry); | ||||||||||
346 | 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)); } | ||||||||||
347 | 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)); } | ||||||||||
348 | |||||||||||
349 | Pass *llvm::createLICMPass() { return new LegacyLICMPass(); } | ||||||||||
350 | Pass *llvm::createLICMPass(unsigned LicmMssaOptCap, | ||||||||||
351 | unsigned LicmMssaNoAccForPromotionCap, | ||||||||||
352 | bool LicmAllowSpeculation) { | ||||||||||
353 | return new LegacyLICMPass(LicmMssaOptCap, LicmMssaNoAccForPromotionCap, | ||||||||||
354 | LicmAllowSpeculation); | ||||||||||
355 | } | ||||||||||
356 | |||||||||||
357 | llvm::SinkAndHoistLICMFlags::SinkAndHoistLICMFlags(bool IsSink, Loop *L, | ||||||||||
358 | MemorySSA *MSSA) | ||||||||||
359 | : SinkAndHoistLICMFlags(SetLicmMssaOptCap, SetLicmMssaNoAccForPromotionCap, | ||||||||||
360 | IsSink, L, MSSA) {} | ||||||||||
361 | |||||||||||
362 | llvm::SinkAndHoistLICMFlags::SinkAndHoistLICMFlags( | ||||||||||
363 | unsigned LicmMssaOptCap, unsigned LicmMssaNoAccForPromotionCap, bool IsSink, | ||||||||||
364 | Loop *L, MemorySSA *MSSA) | ||||||||||
365 | : LicmMssaOptCap(LicmMssaOptCap), | ||||||||||
366 | LicmMssaNoAccForPromotionCap(LicmMssaNoAccForPromotionCap), | ||||||||||
367 | IsSink(IsSink) { | ||||||||||
368 | assert(((L != nullptr) == (MSSA != nullptr)) &&(static_cast <bool> (((L != nullptr) == (MSSA != nullptr )) && "Unexpected values for SinkAndHoistLICMFlags") ? void (0) : __assert_fail ("((L != nullptr) == (MSSA != nullptr)) && \"Unexpected values for SinkAndHoistLICMFlags\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 369, __extension__ __PRETTY_FUNCTION__ )) | ||||||||||
369 | "Unexpected values for SinkAndHoistLICMFlags")(static_cast <bool> (((L != nullptr) == (MSSA != nullptr )) && "Unexpected values for SinkAndHoistLICMFlags") ? void (0) : __assert_fail ("((L != nullptr) == (MSSA != nullptr)) && \"Unexpected values for SinkAndHoistLICMFlags\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 369, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
370 | if (!MSSA) | ||||||||||
371 | return; | ||||||||||
372 | |||||||||||
373 | unsigned AccessCapCount = 0; | ||||||||||
374 | for (auto *BB : L->getBlocks()) | ||||||||||
375 | if (const auto *Accesses = MSSA->getBlockAccesses(BB)) | ||||||||||
376 | for (const auto &MA : *Accesses) { | ||||||||||
377 | (void)MA; | ||||||||||
378 | ++AccessCapCount; | ||||||||||
379 | if (AccessCapCount > LicmMssaNoAccForPromotionCap) { | ||||||||||
380 | NoOfMemAccTooLarge = true; | ||||||||||
381 | return; | ||||||||||
382 | } | ||||||||||
383 | } | ||||||||||
384 | } | ||||||||||
385 | |||||||||||
386 | /// Hoist expressions out of the specified loop. Note, alias info for inner | ||||||||||
387 | /// loop is not preserved so it is not a good idea to run LICM multiple | ||||||||||
388 | /// times on one loop. | ||||||||||
389 | bool LoopInvariantCodeMotion::runOnLoop( | ||||||||||
390 | Loop *L, AAResults *AA, LoopInfo *LI, DominatorTree *DT, | ||||||||||
391 | BlockFrequencyInfo *BFI, TargetLibraryInfo *TLI, TargetTransformInfo *TTI, | ||||||||||
392 | ScalarEvolution *SE, MemorySSA *MSSA, OptimizationRemarkEmitter *ORE, | ||||||||||
393 | bool LoopNestMode) { | ||||||||||
394 | bool Changed = false; | ||||||||||
395 | |||||||||||
396 | assert(L->isLCSSAForm(*DT) && "Loop is not in LCSSA form.")(static_cast <bool> (L->isLCSSAForm(*DT) && "Loop is not in LCSSA form." ) ? void (0) : __assert_fail ("L->isLCSSAForm(*DT) && \"Loop is not in LCSSA form.\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 396, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
397 | MSSA->ensureOptimizedUses(); | ||||||||||
398 | |||||||||||
399 | // If this loop has metadata indicating that LICM is not to be performed then | ||||||||||
400 | // just exit. | ||||||||||
401 | if (hasDisableLICMTransformsHint(L)) { | ||||||||||
402 | return false; | ||||||||||
403 | } | ||||||||||
404 | |||||||||||
405 | // Don't sink stores from loops with coroutine suspend instructions. | ||||||||||
406 | // LICM would sink instructions into the default destination of | ||||||||||
407 | // the coroutine switch. The default destination of the switch is to | ||||||||||
408 | // handle the case where the coroutine is suspended, by which point the | ||||||||||
409 | // coroutine frame may have been destroyed. No instruction can be sunk there. | ||||||||||
410 | // FIXME: This would unfortunately hurt the performance of coroutines, however | ||||||||||
411 | // there is currently no general solution for this. Similar issues could also | ||||||||||
412 | // potentially happen in other passes where instructions are being moved | ||||||||||
413 | // across that edge. | ||||||||||
414 | bool HasCoroSuspendInst = llvm::any_of(L->getBlocks(), [](BasicBlock *BB) { | ||||||||||
415 | return llvm::any_of(*BB, [](Instruction &I) { | ||||||||||
416 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I); | ||||||||||
417 | return II && II->getIntrinsicID() == Intrinsic::coro_suspend; | ||||||||||
418 | }); | ||||||||||
419 | }); | ||||||||||
420 | |||||||||||
421 | MemorySSAUpdater MSSAU(MSSA); | ||||||||||
422 | SinkAndHoistLICMFlags Flags(LicmMssaOptCap, LicmMssaNoAccForPromotionCap, | ||||||||||
423 | /*IsSink=*/true, L, MSSA); | ||||||||||
424 | |||||||||||
425 | // Get the preheader block to move instructions into... | ||||||||||
426 | BasicBlock *Preheader = L->getLoopPreheader(); | ||||||||||
427 | |||||||||||
428 | // Compute loop safety information. | ||||||||||
429 | ICFLoopSafetyInfo SafetyInfo; | ||||||||||
430 | SafetyInfo.computeLoopSafetyInfo(L); | ||||||||||
431 | |||||||||||
432 | // We want to visit all of the instructions in this loop... that are not parts | ||||||||||
433 | // of our subloops (they have already had their invariants hoisted out of | ||||||||||
434 | // their loop, into this loop, so there is no need to process the BODIES of | ||||||||||
435 | // the subloops). | ||||||||||
436 | // | ||||||||||
437 | // Traverse the body of the loop in depth first order on the dominator tree so | ||||||||||
438 | // that we are guaranteed to see definitions before we see uses. This allows | ||||||||||
439 | // us to sink instructions in one pass, without iteration. After sinking | ||||||||||
440 | // instructions, we perform another pass to hoist them out of the loop. | ||||||||||
441 | if (L->hasDedicatedExits()) | ||||||||||
442 | Changed |= LoopNestMode | ||||||||||
443 | ? sinkRegionForLoopNest(DT->getNode(L->getHeader()), AA, LI, | ||||||||||
444 | DT, BFI, TLI, TTI, L, &MSSAU, | ||||||||||
445 | &SafetyInfo, Flags, ORE) | ||||||||||
446 | : sinkRegion(DT->getNode(L->getHeader()), AA, LI, DT, BFI, | ||||||||||
447 | TLI, TTI, L, &MSSAU, &SafetyInfo, Flags, ORE); | ||||||||||
448 | Flags.setIsSink(false); | ||||||||||
449 | if (Preheader) | ||||||||||
450 | Changed |= hoistRegion(DT->getNode(L->getHeader()), AA, LI, DT, BFI, TLI, L, | ||||||||||
451 | &MSSAU, SE, &SafetyInfo, Flags, ORE, LoopNestMode, | ||||||||||
452 | LicmAllowSpeculation); | ||||||||||
453 | |||||||||||
454 | // Now that all loop invariants have been removed from the loop, promote any | ||||||||||
455 | // memory references to scalars that we can. | ||||||||||
456 | // Don't sink stores from loops without dedicated block exits. Exits | ||||||||||
457 | // containing indirect branches are not transformed by loop simplify, | ||||||||||
458 | // make sure we catch that. An additional load may be generated in the | ||||||||||
459 | // preheader for SSA updater, so also avoid sinking when no preheader | ||||||||||
460 | // is available. | ||||||||||
461 | if (!DisablePromotion && Preheader && L->hasDedicatedExits() && | ||||||||||
462 | !Flags.tooManyMemoryAccesses() && !HasCoroSuspendInst) { | ||||||||||
463 | // Figure out the loop exits and their insertion points | ||||||||||
464 | SmallVector<BasicBlock *, 8> ExitBlocks; | ||||||||||
465 | L->getUniqueExitBlocks(ExitBlocks); | ||||||||||
466 | |||||||||||
467 | // We can't insert into a catchswitch. | ||||||||||
468 | bool HasCatchSwitch = llvm::any_of(ExitBlocks, [](BasicBlock *Exit) { | ||||||||||
469 | return isa<CatchSwitchInst>(Exit->getTerminator()); | ||||||||||
470 | }); | ||||||||||
471 | |||||||||||
472 | if (!HasCatchSwitch) { | ||||||||||
473 | SmallVector<Instruction *, 8> InsertPts; | ||||||||||
474 | SmallVector<MemoryAccess *, 8> MSSAInsertPts; | ||||||||||
475 | InsertPts.reserve(ExitBlocks.size()); | ||||||||||
476 | MSSAInsertPts.reserve(ExitBlocks.size()); | ||||||||||
477 | for (BasicBlock *ExitBlock : ExitBlocks) { | ||||||||||
478 | InsertPts.push_back(&*ExitBlock->getFirstInsertionPt()); | ||||||||||
479 | MSSAInsertPts.push_back(nullptr); | ||||||||||
480 | } | ||||||||||
481 | |||||||||||
482 | PredIteratorCache PIC; | ||||||||||
483 | |||||||||||
484 | // Promoting one set of accesses may make the pointers for another set | ||||||||||
485 | // loop invariant, so run this in a loop. | ||||||||||
486 | bool Promoted = false; | ||||||||||
487 | bool LocalPromoted; | ||||||||||
488 | do { | ||||||||||
489 | LocalPromoted = false; | ||||||||||
490 | for (const SmallSetVector<Value *, 8> &PointerMustAliases : | ||||||||||
491 | collectPromotionCandidates(MSSA, AA, L)) { | ||||||||||
492 | LocalPromoted |= promoteLoopAccessesToScalars( | ||||||||||
493 | PointerMustAliases, ExitBlocks, InsertPts, MSSAInsertPts, PIC, LI, | ||||||||||
494 | DT, TLI, L, &MSSAU, &SafetyInfo, ORE, LicmAllowSpeculation); | ||||||||||
495 | } | ||||||||||
496 | Promoted |= LocalPromoted; | ||||||||||
497 | } while (LocalPromoted); | ||||||||||
498 | |||||||||||
499 | // Once we have promoted values across the loop body we have to | ||||||||||
500 | // recursively reform LCSSA as any nested loop may now have values defined | ||||||||||
501 | // within the loop used in the outer loop. | ||||||||||
502 | // FIXME: This is really heavy handed. It would be a bit better to use an | ||||||||||
503 | // SSAUpdater strategy during promotion that was LCSSA aware and reformed | ||||||||||
504 | // it as it went. | ||||||||||
505 | if (Promoted) | ||||||||||
506 | formLCSSARecursively(*L, *DT, LI, SE); | ||||||||||
507 | |||||||||||
508 | Changed |= Promoted; | ||||||||||
509 | } | ||||||||||
510 | } | ||||||||||
511 | |||||||||||
512 | // Check that neither this loop nor its parent have had LCSSA broken. LICM is | ||||||||||
513 | // specifically moving instructions across the loop boundary and so it is | ||||||||||
514 | // especially in need of basic functional correctness checking here. | ||||||||||
515 | assert(L->isLCSSAForm(*DT) && "Loop not left in LCSSA form after LICM!")(static_cast <bool> (L->isLCSSAForm(*DT) && "Loop not left in LCSSA form after LICM!" ) ? void (0) : __assert_fail ("L->isLCSSAForm(*DT) && \"Loop not left in LCSSA form after LICM!\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 515, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
516 | assert((L->isOutermost() || L->getParentLoop()->isLCSSAForm(*DT)) &&(static_cast <bool> ((L->isOutermost() || L->getParentLoop ()->isLCSSAForm(*DT)) && "Parent loop not left in LCSSA form after LICM!" ) ? void (0) : __assert_fail ("(L->isOutermost() || L->getParentLoop()->isLCSSAForm(*DT)) && \"Parent loop not left in LCSSA form after LICM!\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 517, __extension__ __PRETTY_FUNCTION__ )) | ||||||||||
517 | "Parent loop not left in LCSSA form after LICM!")(static_cast <bool> ((L->isOutermost() || L->getParentLoop ()->isLCSSAForm(*DT)) && "Parent loop not left in LCSSA form after LICM!" ) ? void (0) : __assert_fail ("(L->isOutermost() || L->getParentLoop()->isLCSSAForm(*DT)) && \"Parent loop not left in LCSSA form after LICM!\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 517, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
518 | |||||||||||
519 | if (VerifyMemorySSA) | ||||||||||
520 | MSSA->verifyMemorySSA(); | ||||||||||
521 | |||||||||||
522 | if (Changed && SE) | ||||||||||
523 | SE->forgetLoopDispositions(L); | ||||||||||
524 | return Changed; | ||||||||||
525 | } | ||||||||||
526 | |||||||||||
527 | /// Walk the specified region of the CFG (defined by all blocks dominated by | ||||||||||
528 | /// the specified block, and that are in the current loop) in reverse depth | ||||||||||
529 | /// first order w.r.t the DominatorTree. This allows us to visit uses before | ||||||||||
530 | /// definitions, allowing us to sink a loop body in one pass without iteration. | ||||||||||
531 | /// | ||||||||||
532 | bool llvm::sinkRegion(DomTreeNode *N, AAResults *AA, LoopInfo *LI, | ||||||||||
533 | DominatorTree *DT, BlockFrequencyInfo *BFI, | ||||||||||
534 | TargetLibraryInfo *TLI, TargetTransformInfo *TTI, | ||||||||||
535 | Loop *CurLoop, MemorySSAUpdater *MSSAU, | ||||||||||
536 | ICFLoopSafetyInfo *SafetyInfo, | ||||||||||
537 | SinkAndHoistLICMFlags &Flags, | ||||||||||
538 | OptimizationRemarkEmitter *ORE, Loop *OutermostLoop) { | ||||||||||
539 | |||||||||||
540 | // Verify inputs. | ||||||||||
541 | assert(N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr &&(static_cast <bool> (N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && MSSAU != nullptr && SafetyInfo != nullptr && "Unexpected input to sinkRegion.") ? void (0) : __assert_fail ("N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && MSSAU != nullptr && SafetyInfo != nullptr && \"Unexpected input to sinkRegion.\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 543, __extension__ __PRETTY_FUNCTION__ )) | ||||||||||
542 | CurLoop != nullptr && MSSAU != nullptr && SafetyInfo != nullptr &&(static_cast <bool> (N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && MSSAU != nullptr && SafetyInfo != nullptr && "Unexpected input to sinkRegion.") ? void (0) : __assert_fail ("N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && MSSAU != nullptr && SafetyInfo != nullptr && \"Unexpected input to sinkRegion.\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 543, __extension__ __PRETTY_FUNCTION__ )) | ||||||||||
543 | "Unexpected input to sinkRegion.")(static_cast <bool> (N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && MSSAU != nullptr && SafetyInfo != nullptr && "Unexpected input to sinkRegion.") ? void (0) : __assert_fail ("N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && MSSAU != nullptr && SafetyInfo != nullptr && \"Unexpected input to sinkRegion.\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 543, __extension__ __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 | // The instruction is not used in the loop if it is dead. In this case, | ||||||||||
562 | // we just delete it instead of sinking 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, 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 | bool LoopNestMode = OutermostLoop != nullptr; | ||||||||||
580 | if (!I.mayHaveSideEffects() && | ||||||||||
581 | isNotUsedOrFreeInLoop(I, LoopNestMode ? OutermostLoop : CurLoop, | ||||||||||
582 | SafetyInfo, TTI, FreeInLoop, LoopNestMode) && | ||||||||||
583 | canSinkOrHoistInst(I, AA, DT, CurLoop, /*CurAST*/nullptr, MSSAU, true, | ||||||||||
584 | &Flags, ORE)) { | ||||||||||
585 | if (sink(I, LI, DT, BFI, CurLoop, SafetyInfo, MSSAU, ORE)) { | ||||||||||
586 | if (!FreeInLoop) { | ||||||||||
587 | ++II; | ||||||||||
588 | salvageDebugInfo(I); | ||||||||||
589 | eraseInstruction(I, *SafetyInfo, MSSAU); | ||||||||||
590 | } | ||||||||||
591 | Changed = true; | ||||||||||
592 | } | ||||||||||
593 | } | ||||||||||
594 | } | ||||||||||
595 | } | ||||||||||
596 | if (VerifyMemorySSA) | ||||||||||
597 | MSSAU->getMemorySSA()->verifyMemorySSA(); | ||||||||||
598 | return Changed; | ||||||||||
599 | } | ||||||||||
600 | |||||||||||
601 | bool llvm::sinkRegionForLoopNest( | ||||||||||
602 | DomTreeNode *N, AAResults *AA, LoopInfo *LI, DominatorTree *DT, | ||||||||||
603 | BlockFrequencyInfo *BFI, TargetLibraryInfo *TLI, TargetTransformInfo *TTI, | ||||||||||
604 | Loop *CurLoop, MemorySSAUpdater *MSSAU, ICFLoopSafetyInfo *SafetyInfo, | ||||||||||
605 | SinkAndHoistLICMFlags &Flags, OptimizationRemarkEmitter *ORE) { | ||||||||||
606 | |||||||||||
607 | bool Changed = false; | ||||||||||
608 | SmallPriorityWorklist<Loop *, 4> Worklist; | ||||||||||
609 | Worklist.insert(CurLoop); | ||||||||||
610 | appendLoopsToWorklist(*CurLoop, Worklist); | ||||||||||
611 | while (!Worklist.empty()) { | ||||||||||
612 | Loop *L = Worklist.pop_back_val(); | ||||||||||
613 | Changed |= sinkRegion(DT->getNode(L->getHeader()), AA, LI, DT, BFI, TLI, | ||||||||||
614 | TTI, L, MSSAU, SafetyInfo, Flags, ORE, CurLoop); | ||||||||||
615 | } | ||||||||||
616 | return Changed; | ||||||||||
617 | } | ||||||||||
618 | |||||||||||
619 | namespace { | ||||||||||
620 | // This is a helper class for hoistRegion to make it able to hoist control flow | ||||||||||
621 | // in order to be able to hoist phis. The way this works is that we initially | ||||||||||
622 | // start hoisting to the loop preheader, and when we see a loop invariant branch | ||||||||||
623 | // we make note of this. When we then come to hoist an instruction that's | ||||||||||
624 | // conditional on such a branch we duplicate the branch and the relevant control | ||||||||||
625 | // flow, then hoist the instruction into the block corresponding to its original | ||||||||||
626 | // block in the duplicated control flow. | ||||||||||
627 | class ControlFlowHoister { | ||||||||||
628 | private: | ||||||||||
629 | // Information about the loop we are hoisting from | ||||||||||
630 | LoopInfo *LI; | ||||||||||
631 | DominatorTree *DT; | ||||||||||
632 | Loop *CurLoop; | ||||||||||
633 | MemorySSAUpdater *MSSAU; | ||||||||||
634 | |||||||||||
635 | // A map of blocks in the loop to the block their instructions will be hoisted | ||||||||||
636 | // to. | ||||||||||
637 | DenseMap<BasicBlock *, BasicBlock *> HoistDestinationMap; | ||||||||||
638 | |||||||||||
639 | // The branches that we can hoist, mapped to the block that marks a | ||||||||||
640 | // convergence point of their control flow. | ||||||||||
641 | DenseMap<BranchInst *, BasicBlock *> HoistableBranches; | ||||||||||
642 | |||||||||||
643 | public: | ||||||||||
644 | ControlFlowHoister(LoopInfo *LI, DominatorTree *DT, Loop *CurLoop, | ||||||||||
645 | MemorySSAUpdater *MSSAU) | ||||||||||
646 | : LI(LI), DT(DT), CurLoop(CurLoop), MSSAU(MSSAU) {} | ||||||||||
647 | |||||||||||
648 | void registerPossiblyHoistableBranch(BranchInst *BI) { | ||||||||||
649 | // We can only hoist conditional branches with loop invariant operands. | ||||||||||
650 | if (!ControlFlowHoisting || !BI->isConditional() || | ||||||||||
651 | !CurLoop->hasLoopInvariantOperands(BI)) | ||||||||||
652 | return; | ||||||||||
653 | |||||||||||
654 | // The branch destinations need to be in the loop, and we don't gain | ||||||||||
655 | // anything by duplicating conditional branches with duplicate successors, | ||||||||||
656 | // as it's essentially the same as an unconditional branch. | ||||||||||
657 | BasicBlock *TrueDest = BI->getSuccessor(0); | ||||||||||
658 | BasicBlock *FalseDest = BI->getSuccessor(1); | ||||||||||
659 | if (!CurLoop->contains(TrueDest) || !CurLoop->contains(FalseDest) || | ||||||||||
660 | TrueDest == FalseDest) | ||||||||||
661 | return; | ||||||||||
662 | |||||||||||
663 | // We can hoist BI if one branch destination is the successor of the other, | ||||||||||
664 | // or both have common successor which we check by seeing if the | ||||||||||
665 | // intersection of their successors is non-empty. | ||||||||||
666 | // TODO: This could be expanded to allowing branches where both ends | ||||||||||
667 | // eventually converge to a single block. | ||||||||||
668 | SmallPtrSet<BasicBlock *, 4> TrueDestSucc, FalseDestSucc; | ||||||||||
669 | TrueDestSucc.insert(succ_begin(TrueDest), succ_end(TrueDest)); | ||||||||||
670 | FalseDestSucc.insert(succ_begin(FalseDest), succ_end(FalseDest)); | ||||||||||
671 | BasicBlock *CommonSucc = nullptr; | ||||||||||
672 | if (TrueDestSucc.count(FalseDest)) { | ||||||||||
673 | CommonSucc = FalseDest; | ||||||||||
674 | } else if (FalseDestSucc.count(TrueDest)) { | ||||||||||
675 | CommonSucc = TrueDest; | ||||||||||
676 | } else { | ||||||||||
677 | set_intersect(TrueDestSucc, FalseDestSucc); | ||||||||||
678 | // If there's one common successor use that. | ||||||||||
679 | if (TrueDestSucc.size() == 1) | ||||||||||
680 | CommonSucc = *TrueDestSucc.begin(); | ||||||||||
681 | // If there's more than one pick whichever appears first in the block list | ||||||||||
682 | // (we can't use the value returned by TrueDestSucc.begin() as it's | ||||||||||
683 | // unpredicatable which element gets returned). | ||||||||||
684 | else if (!TrueDestSucc.empty()) { | ||||||||||
685 | Function *F = TrueDest->getParent(); | ||||||||||
686 | auto IsSucc = [&](BasicBlock &BB) { return TrueDestSucc.count(&BB); }; | ||||||||||
687 | auto It = llvm::find_if(*F, IsSucc); | ||||||||||
688 | assert(It != F->end() && "Could not find successor in function")(static_cast <bool> (It != F->end() && "Could not find successor in function" ) ? void (0) : __assert_fail ("It != F->end() && \"Could not find successor in function\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 688, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
689 | CommonSucc = &*It; | ||||||||||
690 | } | ||||||||||
691 | } | ||||||||||
692 | // The common successor has to be dominated by the branch, as otherwise | ||||||||||
693 | // there will be some other path to the successor that will not be | ||||||||||
694 | // controlled by this branch so any phi we hoist would be controlled by the | ||||||||||
695 | // wrong condition. This also takes care of avoiding hoisting of loop back | ||||||||||
696 | // edges. | ||||||||||
697 | // TODO: In some cases this could be relaxed if the successor is dominated | ||||||||||
698 | // by another block that's been hoisted and we can guarantee that the | ||||||||||
699 | // control flow has been replicated exactly. | ||||||||||
700 | if (CommonSucc && DT->dominates(BI, CommonSucc)) | ||||||||||
701 | HoistableBranches[BI] = CommonSucc; | ||||||||||
702 | } | ||||||||||
703 | |||||||||||
704 | bool canHoistPHI(PHINode *PN) { | ||||||||||
705 | // The phi must have loop invariant operands. | ||||||||||
706 | if (!ControlFlowHoisting || !CurLoop->hasLoopInvariantOperands(PN)) | ||||||||||
707 | return false; | ||||||||||
708 | // We can hoist phis if the block they are in is the target of hoistable | ||||||||||
709 | // branches which cover all of the predecessors of the block. | ||||||||||
710 | SmallPtrSet<BasicBlock *, 8> PredecessorBlocks; | ||||||||||
711 | BasicBlock *BB = PN->getParent(); | ||||||||||
712 | for (BasicBlock *PredBB : predecessors(BB)) | ||||||||||
713 | PredecessorBlocks.insert(PredBB); | ||||||||||
714 | // If we have less predecessor blocks than predecessors then the phi will | ||||||||||
715 | // have more than one incoming value for the same block which we can't | ||||||||||
716 | // handle. | ||||||||||
717 | // TODO: This could be handled be erasing some of the duplicate incoming | ||||||||||
718 | // values. | ||||||||||
719 | if (PredecessorBlocks.size() != pred_size(BB)) | ||||||||||
720 | return false; | ||||||||||
721 | for (auto &Pair : HoistableBranches) { | ||||||||||
722 | if (Pair.second == BB) { | ||||||||||
723 | // Which blocks are predecessors via this branch depends on if the | ||||||||||
724 | // branch is triangle-like or diamond-like. | ||||||||||
725 | if (Pair.first->getSuccessor(0) == BB) { | ||||||||||
726 | PredecessorBlocks.erase(Pair.first->getParent()); | ||||||||||
727 | PredecessorBlocks.erase(Pair.first->getSuccessor(1)); | ||||||||||
728 | } else if (Pair.first->getSuccessor(1) == BB) { | ||||||||||
729 | PredecessorBlocks.erase(Pair.first->getParent()); | ||||||||||
730 | PredecessorBlocks.erase(Pair.first->getSuccessor(0)); | ||||||||||
731 | } else { | ||||||||||
732 | PredecessorBlocks.erase(Pair.first->getSuccessor(0)); | ||||||||||
733 | PredecessorBlocks.erase(Pair.first->getSuccessor(1)); | ||||||||||
734 | } | ||||||||||
735 | } | ||||||||||
736 | } | ||||||||||
737 | // PredecessorBlocks will now be empty if for every predecessor of BB we | ||||||||||
738 | // found a hoistable branch source. | ||||||||||
739 | return PredecessorBlocks.empty(); | ||||||||||
740 | } | ||||||||||
741 | |||||||||||
742 | BasicBlock *getOrCreateHoistedBlock(BasicBlock *BB) { | ||||||||||
743 | if (!ControlFlowHoisting) | ||||||||||
744 | return CurLoop->getLoopPreheader(); | ||||||||||
745 | // If BB has already been hoisted, return that | ||||||||||
746 | if (HoistDestinationMap.count(BB)) | ||||||||||
747 | return HoistDestinationMap[BB]; | ||||||||||
748 | |||||||||||
749 | // Check if this block is conditional based on a pending branch | ||||||||||
750 | auto HasBBAsSuccessor = | ||||||||||
751 | [&](DenseMap<BranchInst *, BasicBlock *>::value_type &Pair) { | ||||||||||
752 | return BB != Pair.second && (Pair.first->getSuccessor(0) == BB || | ||||||||||
753 | Pair.first->getSuccessor(1) == BB); | ||||||||||
754 | }; | ||||||||||
755 | auto It = llvm::find_if(HoistableBranches, HasBBAsSuccessor); | ||||||||||
756 | |||||||||||
757 | // If not involved in a pending branch, hoist to preheader | ||||||||||
758 | BasicBlock *InitialPreheader = CurLoop->getLoopPreheader(); | ||||||||||
759 | if (It == HoistableBranches.end()) { | ||||||||||
760 | 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) | ||||||||||
761 | << InitialPreheader->getNameOrAsOperand()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM using " << InitialPreheader ->getNameOrAsOperand() << " as hoist destination for " << BB->getNameOrAsOperand() << "\n"; } } while (false) | ||||||||||
762 | << " 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) | ||||||||||
763 | << BB->getNameOrAsOperand() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM using " << InitialPreheader ->getNameOrAsOperand() << " as hoist destination for " << BB->getNameOrAsOperand() << "\n"; } } while (false); | ||||||||||
764 | HoistDestinationMap[BB] = InitialPreheader; | ||||||||||
765 | return InitialPreheader; | ||||||||||
766 | } | ||||||||||
767 | BranchInst *BI = It->first; | ||||||||||
768 | assert(std::find_if(++It, HoistableBranches.end(), HasBBAsSuccessor) ==(static_cast <bool> (std::find_if(++It, HoistableBranches .end(), HasBBAsSuccessor) == HoistableBranches.end() && "BB is expected to be the target of at most one branch") ? 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\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 770, __extension__ __PRETTY_FUNCTION__ )) | ||||||||||
769 | HoistableBranches.end() &&(static_cast <bool> (std::find_if(++It, HoistableBranches .end(), HasBBAsSuccessor) == HoistableBranches.end() && "BB is expected to be the target of at most one branch") ? 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\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 770, __extension__ __PRETTY_FUNCTION__ )) | ||||||||||
770 | "BB is expected to be the target of at most one branch")(static_cast <bool> (std::find_if(++It, HoistableBranches .end(), HasBBAsSuccessor) == HoistableBranches.end() && "BB is expected to be the target of at most one branch") ? 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\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 770, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
771 | |||||||||||
772 | LLVMContext &C = BB->getContext(); | ||||||||||
773 | BasicBlock *TrueDest = BI->getSuccessor(0); | ||||||||||
774 | BasicBlock *FalseDest = BI->getSuccessor(1); | ||||||||||
775 | BasicBlock *CommonSucc = HoistableBranches[BI]; | ||||||||||
776 | BasicBlock *HoistTarget = getOrCreateHoistedBlock(BI->getParent()); | ||||||||||
777 | |||||||||||
778 | // Create hoisted versions of blocks that currently don't have them | ||||||||||
779 | auto CreateHoistedBlock = [&](BasicBlock *Orig) { | ||||||||||
780 | if (HoistDestinationMap.count(Orig)) | ||||||||||
781 | return HoistDestinationMap[Orig]; | ||||||||||
782 | BasicBlock *New = | ||||||||||
783 | BasicBlock::Create(C, Orig->getName() + ".licm", Orig->getParent()); | ||||||||||
784 | HoistDestinationMap[Orig] = New; | ||||||||||
785 | DT->addNewBlock(New, HoistTarget); | ||||||||||
786 | if (CurLoop->getParentLoop()) | ||||||||||
787 | CurLoop->getParentLoop()->addBasicBlockToLoop(New, *LI); | ||||||||||
788 | ++NumCreatedBlocks; | ||||||||||
789 | 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) | ||||||||||
790 | << " 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) | ||||||||||
791 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM created " << New-> getName() << " as hoist destination for " << Orig ->getName() << "\n"; } } while (false); | ||||||||||
792 | return New; | ||||||||||
793 | }; | ||||||||||
794 | BasicBlock *HoistTrueDest = CreateHoistedBlock(TrueDest); | ||||||||||
795 | BasicBlock *HoistFalseDest = CreateHoistedBlock(FalseDest); | ||||||||||
796 | BasicBlock *HoistCommonSucc = CreateHoistedBlock(CommonSucc); | ||||||||||
797 | |||||||||||
798 | // Link up these blocks with branches. | ||||||||||
799 | if (!HoistCommonSucc->getTerminator()) { | ||||||||||
800 | // The new common successor we've generated will branch to whatever that | ||||||||||
801 | // hoist target branched to. | ||||||||||
802 | BasicBlock *TargetSucc = HoistTarget->getSingleSuccessor(); | ||||||||||
803 | assert(TargetSucc && "Expected hoist target to have a single successor")(static_cast <bool> (TargetSucc && "Expected hoist target to have a single successor" ) ? void (0) : __assert_fail ("TargetSucc && \"Expected hoist target to have a single successor\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 803, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
804 | HoistCommonSucc->moveBefore(TargetSucc); | ||||||||||
805 | BranchInst::Create(TargetSucc, HoistCommonSucc); | ||||||||||
806 | } | ||||||||||
807 | if (!HoistTrueDest->getTerminator()) { | ||||||||||
808 | HoistTrueDest->moveBefore(HoistCommonSucc); | ||||||||||
809 | BranchInst::Create(HoistCommonSucc, HoistTrueDest); | ||||||||||
810 | } | ||||||||||
811 | if (!HoistFalseDest->getTerminator()) { | ||||||||||
812 | HoistFalseDest->moveBefore(HoistCommonSucc); | ||||||||||
813 | BranchInst::Create(HoistCommonSucc, HoistFalseDest); | ||||||||||
814 | } | ||||||||||
815 | |||||||||||
816 | // If BI is being cloned to what was originally the preheader then | ||||||||||
817 | // HoistCommonSucc will now be the new preheader. | ||||||||||
818 | if (HoistTarget == InitialPreheader) { | ||||||||||
819 | // Phis in the loop header now need to use the new preheader. | ||||||||||
820 | InitialPreheader->replaceSuccessorsPhiUsesWith(HoistCommonSucc); | ||||||||||
821 | MSSAU->wireOldPredecessorsToNewImmediatePredecessor( | ||||||||||
822 | HoistTarget->getSingleSuccessor(), HoistCommonSucc, {HoistTarget}); | ||||||||||
823 | // The new preheader dominates the loop header. | ||||||||||
824 | DomTreeNode *PreheaderNode = DT->getNode(HoistCommonSucc); | ||||||||||
825 | DomTreeNode *HeaderNode = DT->getNode(CurLoop->getHeader()); | ||||||||||
826 | DT->changeImmediateDominator(HeaderNode, PreheaderNode); | ||||||||||
827 | // The preheader hoist destination is now the new preheader, with the | ||||||||||
828 | // exception of the hoist destination of this branch. | ||||||||||
829 | for (auto &Pair : HoistDestinationMap) | ||||||||||
830 | if (Pair.second == InitialPreheader && Pair.first != BI->getParent()) | ||||||||||
831 | Pair.second = HoistCommonSucc; | ||||||||||
832 | } | ||||||||||
833 | |||||||||||
834 | // Now finally clone BI. | ||||||||||
835 | ReplaceInstWithInst( | ||||||||||
836 | HoistTarget->getTerminator(), | ||||||||||
837 | BranchInst::Create(HoistTrueDest, HoistFalseDest, BI->getCondition())); | ||||||||||
838 | ++NumClonedBranches; | ||||||||||
839 | |||||||||||
840 | assert(CurLoop->getLoopPreheader() &&(static_cast <bool> (CurLoop->getLoopPreheader() && "Hoisting blocks should not have destroyed preheader") ? void (0) : __assert_fail ("CurLoop->getLoopPreheader() && \"Hoisting blocks should not have destroyed preheader\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 841, __extension__ __PRETTY_FUNCTION__ )) | ||||||||||
841 | "Hoisting blocks should not have destroyed preheader")(static_cast <bool> (CurLoop->getLoopPreheader() && "Hoisting blocks should not have destroyed preheader") ? void (0) : __assert_fail ("CurLoop->getLoopPreheader() && \"Hoisting blocks should not have destroyed preheader\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 841, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
842 | return HoistDestinationMap[BB]; | ||||||||||
843 | } | ||||||||||
844 | }; | ||||||||||
845 | } // namespace | ||||||||||
846 | |||||||||||
847 | /// Walk the specified region of the CFG (defined by all blocks dominated by | ||||||||||
848 | /// the specified block, and that are in the current loop) in depth first | ||||||||||
849 | /// order w.r.t the DominatorTree. This allows us to visit definitions before | ||||||||||
850 | /// uses, allowing us to hoist a loop body in one pass without iteration. | ||||||||||
851 | /// | ||||||||||
852 | bool llvm::hoistRegion(DomTreeNode *N, AAResults *AA, LoopInfo *LI, | ||||||||||
853 | DominatorTree *DT, BlockFrequencyInfo *BFI, | ||||||||||
854 | TargetLibraryInfo *TLI, Loop *CurLoop, | ||||||||||
855 | MemorySSAUpdater *MSSAU, ScalarEvolution *SE, | ||||||||||
856 | ICFLoopSafetyInfo *SafetyInfo, | ||||||||||
857 | SinkAndHoistLICMFlags &Flags, | ||||||||||
858 | OptimizationRemarkEmitter *ORE, bool LoopNestMode, | ||||||||||
859 | bool AllowSpeculation) { | ||||||||||
860 | // Verify inputs. | ||||||||||
861 | assert(N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr &&(static_cast <bool> (N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && MSSAU != nullptr && SafetyInfo != nullptr && "Unexpected input to hoistRegion.") ? void (0) : __assert_fail ("N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && MSSAU != nullptr && SafetyInfo != nullptr && \"Unexpected input to hoistRegion.\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 863, __extension__ __PRETTY_FUNCTION__ )) | ||||||||||
862 | CurLoop != nullptr && MSSAU != nullptr && SafetyInfo != nullptr &&(static_cast <bool> (N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && MSSAU != nullptr && SafetyInfo != nullptr && "Unexpected input to hoistRegion.") ? void (0) : __assert_fail ("N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && MSSAU != nullptr && SafetyInfo != nullptr && \"Unexpected input to hoistRegion.\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 863, __extension__ __PRETTY_FUNCTION__ )) | ||||||||||
863 | "Unexpected input to hoistRegion.")(static_cast <bool> (N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && MSSAU != nullptr && SafetyInfo != nullptr && "Unexpected input to hoistRegion.") ? void (0) : __assert_fail ("N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && MSSAU != nullptr && SafetyInfo != nullptr && \"Unexpected input to hoistRegion.\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 863, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
864 | |||||||||||
865 | ControlFlowHoister CFH(LI, DT, CurLoop, MSSAU); | ||||||||||
866 | |||||||||||
867 | // Keep track of instructions that have been hoisted, as they may need to be | ||||||||||
868 | // re-hoisted if they end up not dominating all of their uses. | ||||||||||
869 | SmallVector<Instruction *, 16> HoistedInstructions; | ||||||||||
870 | |||||||||||
871 | // For PHI hoisting to work we need to hoist blocks before their successors. | ||||||||||
872 | // We can do this by iterating through the blocks in the loop in reverse | ||||||||||
873 | // post-order. | ||||||||||
874 | LoopBlocksRPO Worklist(CurLoop); | ||||||||||
875 | Worklist.perform(LI); | ||||||||||
876 | bool Changed = false; | ||||||||||
877 | for (BasicBlock *BB : Worklist) { | ||||||||||
878 | // Only need to process the contents of this block if it is not part of a | ||||||||||
879 | // subloop (which would already have been processed). | ||||||||||
880 | if (!LoopNestMode && inSubLoop(BB, CurLoop, LI)) | ||||||||||
881 | continue; | ||||||||||
882 | |||||||||||
883 | for (Instruction &I : llvm::make_early_inc_range(*BB)) { | ||||||||||
884 | // Try constant folding this instruction. If all the operands are | ||||||||||
885 | // constants, it is technically hoistable, but it would be better to | ||||||||||
886 | // just fold it. | ||||||||||
887 | if (Constant *C = ConstantFoldInstruction( | ||||||||||
888 | &I, I.getModule()->getDataLayout(), TLI)) { | ||||||||||
889 | LLVM_DEBUG(dbgs() << "LICM folding inst: " << I << " --> " << *Cdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM folding inst: " << I << " --> " << *C << '\n'; } } while (false) | ||||||||||
890 | << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM folding inst: " << I << " --> " << *C << '\n'; } } while (false); | ||||||||||
891 | // FIXME MSSA: Such replacements may make accesses unoptimized (D51960). | ||||||||||
892 | I.replaceAllUsesWith(C); | ||||||||||
893 | if (isInstructionTriviallyDead(&I, TLI)) | ||||||||||
894 | eraseInstruction(I, *SafetyInfo, MSSAU); | ||||||||||
895 | Changed = true; | ||||||||||
896 | continue; | ||||||||||
897 | } | ||||||||||
898 | |||||||||||
899 | // Try hoisting the instruction out to the preheader. We can only do | ||||||||||
900 | // this if all of the operands of the instruction are loop invariant and | ||||||||||
901 | // if it is safe to hoist the instruction. We also check block frequency | ||||||||||
902 | // to make sure instruction only gets hoisted into colder blocks. | ||||||||||
903 | // TODO: It may be safe to hoist if we are hoisting to a conditional block | ||||||||||
904 | // and we have accurately duplicated the control flow from the loop header | ||||||||||
905 | // to that block. | ||||||||||
906 | if (CurLoop->hasLoopInvariantOperands(&I) && | ||||||||||
907 | canSinkOrHoistInst(I, AA, DT, CurLoop, /*CurAST*/ nullptr, MSSAU, | ||||||||||
908 | true, &Flags, ORE) && | ||||||||||
909 | isSafeToExecuteUnconditionally( | ||||||||||
910 | I, DT, TLI, CurLoop, SafetyInfo, ORE, | ||||||||||
911 | CurLoop->getLoopPreheader()->getTerminator(), AllowSpeculation)) { | ||||||||||
912 | hoist(I, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo, | ||||||||||
913 | MSSAU, SE, ORE); | ||||||||||
914 | HoistedInstructions.push_back(&I); | ||||||||||
915 | Changed = true; | ||||||||||
916 | continue; | ||||||||||
917 | } | ||||||||||
918 | |||||||||||
919 | // Attempt to remove floating point division out of the loop by | ||||||||||
920 | // converting it to a reciprocal multiplication. | ||||||||||
921 | if (I.getOpcode() == Instruction::FDiv && I.hasAllowReciprocal() && | ||||||||||
922 | CurLoop->isLoopInvariant(I.getOperand(1))) { | ||||||||||
923 | auto Divisor = I.getOperand(1); | ||||||||||
924 | auto One = llvm::ConstantFP::get(Divisor->getType(), 1.0); | ||||||||||
925 | auto ReciprocalDivisor = BinaryOperator::CreateFDiv(One, Divisor); | ||||||||||
926 | ReciprocalDivisor->setFastMathFlags(I.getFastMathFlags()); | ||||||||||
927 | SafetyInfo->insertInstructionTo(ReciprocalDivisor, I.getParent()); | ||||||||||
928 | ReciprocalDivisor->insertBefore(&I); | ||||||||||
929 | |||||||||||
930 | auto Product = | ||||||||||
931 | BinaryOperator::CreateFMul(I.getOperand(0), ReciprocalDivisor); | ||||||||||
932 | Product->setFastMathFlags(I.getFastMathFlags()); | ||||||||||
933 | SafetyInfo->insertInstructionTo(Product, I.getParent()); | ||||||||||
934 | Product->insertAfter(&I); | ||||||||||
935 | I.replaceAllUsesWith(Product); | ||||||||||
936 | eraseInstruction(I, *SafetyInfo, MSSAU); | ||||||||||
937 | |||||||||||
938 | hoist(*ReciprocalDivisor, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), | ||||||||||
939 | SafetyInfo, MSSAU, SE, ORE); | ||||||||||
940 | HoistedInstructions.push_back(ReciprocalDivisor); | ||||||||||
941 | Changed = true; | ||||||||||
942 | continue; | ||||||||||
943 | } | ||||||||||
944 | |||||||||||
945 | auto IsInvariantStart = [&](Instruction &I) { | ||||||||||
946 | using namespace PatternMatch; | ||||||||||
947 | return I.use_empty() && | ||||||||||
948 | match(&I, m_Intrinsic<Intrinsic::invariant_start>()); | ||||||||||
949 | }; | ||||||||||
950 | auto MustExecuteWithoutWritesBefore = [&](Instruction &I) { | ||||||||||
951 | return SafetyInfo->isGuaranteedToExecute(I, DT, CurLoop) && | ||||||||||
952 | SafetyInfo->doesNotWriteMemoryBefore(I, CurLoop); | ||||||||||
953 | }; | ||||||||||
954 | if ((IsInvariantStart(I) || isGuard(&I)) && | ||||||||||
955 | CurLoop->hasLoopInvariantOperands(&I) && | ||||||||||
956 | MustExecuteWithoutWritesBefore(I)) { | ||||||||||
957 | hoist(I, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo, | ||||||||||
958 | MSSAU, SE, ORE); | ||||||||||
959 | HoistedInstructions.push_back(&I); | ||||||||||
960 | Changed = true; | ||||||||||
961 | continue; | ||||||||||
962 | } | ||||||||||
963 | |||||||||||
964 | if (PHINode *PN = dyn_cast<PHINode>(&I)) { | ||||||||||
965 | if (CFH.canHoistPHI(PN)) { | ||||||||||
966 | // Redirect incoming blocks first to ensure that we create hoisted | ||||||||||
967 | // versions of those blocks before we hoist the phi. | ||||||||||
968 | for (unsigned int i = 0; i < PN->getNumIncomingValues(); ++i) | ||||||||||
969 | PN->setIncomingBlock( | ||||||||||
970 | i, CFH.getOrCreateHoistedBlock(PN->getIncomingBlock(i))); | ||||||||||
971 | hoist(*PN, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo, | ||||||||||
972 | MSSAU, SE, ORE); | ||||||||||
973 | assert(DT->dominates(PN, BB) && "Conditional PHIs not expected")(static_cast <bool> (DT->dominates(PN, BB) && "Conditional PHIs not expected") ? void (0) : __assert_fail ( "DT->dominates(PN, BB) && \"Conditional PHIs not expected\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 973, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
974 | Changed = true; | ||||||||||
975 | continue; | ||||||||||
976 | } | ||||||||||
977 | } | ||||||||||
978 | |||||||||||
979 | // Remember possibly hoistable branches so we can actually hoist them | ||||||||||
980 | // later if needed. | ||||||||||
981 | if (BranchInst *BI = dyn_cast<BranchInst>(&I)) | ||||||||||
982 | CFH.registerPossiblyHoistableBranch(BI); | ||||||||||
983 | } | ||||||||||
984 | } | ||||||||||
985 | |||||||||||
986 | // If we hoisted instructions to a conditional block they may not dominate | ||||||||||
987 | // their uses that weren't hoisted (such as phis where some operands are not | ||||||||||
988 | // loop invariant). If so make them unconditional by moving them to their | ||||||||||
989 | // immediate dominator. We iterate through the instructions in reverse order | ||||||||||
990 | // which ensures that when we rehoist an instruction we rehoist its operands, | ||||||||||
991 | // and also keep track of where in the block we are rehoisting to to make sure | ||||||||||
992 | // that we rehoist instructions before the instructions that use them. | ||||||||||
993 | Instruction *HoistPoint = nullptr; | ||||||||||
994 | if (ControlFlowHoisting) { | ||||||||||
995 | for (Instruction *I : reverse(HoistedInstructions)) { | ||||||||||
996 | if (!llvm::all_of(I->uses(), | ||||||||||
997 | [&](Use &U) { return DT->dominates(I, U); })) { | ||||||||||
998 | BasicBlock *Dominator = | ||||||||||
999 | DT->getNode(I->getParent())->getIDom()->getBlock(); | ||||||||||
1000 | if (!HoistPoint || !DT->dominates(HoistPoint->getParent(), Dominator)) { | ||||||||||
1001 | if (HoistPoint) | ||||||||||
1002 | assert(DT->dominates(Dominator, HoistPoint->getParent()) &&(static_cast <bool> (DT->dominates(Dominator, HoistPoint ->getParent()) && "New hoist point expected to dominate old hoist point" ) ? void (0) : __assert_fail ("DT->dominates(Dominator, HoistPoint->getParent()) && \"New hoist point expected to dominate old hoist point\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1003, __extension__ __PRETTY_FUNCTION__ )) | ||||||||||
1003 | "New hoist point expected to dominate old hoist point")(static_cast <bool> (DT->dominates(Dominator, HoistPoint ->getParent()) && "New hoist point expected to dominate old hoist point" ) ? void (0) : __assert_fail ("DT->dominates(Dominator, HoistPoint->getParent()) && \"New hoist point expected to dominate old hoist point\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1003, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
1004 | HoistPoint = Dominator->getTerminator(); | ||||||||||
1005 | } | ||||||||||
1006 | LLVM_DEBUG(dbgs() << "LICM rehoisting to "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM rehoisting to " << HoistPoint ->getParent()->getNameOrAsOperand() << ": " << *I << "\n"; } } while (false) | ||||||||||
1007 | << HoistPoint->getParent()->getNameOrAsOperand()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM rehoisting to " << HoistPoint ->getParent()->getNameOrAsOperand() << ": " << *I << "\n"; } } while (false) | ||||||||||
1008 | << ": " << *I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM rehoisting to " << HoistPoint ->getParent()->getNameOrAsOperand() << ": " << *I << "\n"; } } while (false); | ||||||||||
1009 | moveInstructionBefore(*I, *HoistPoint, *SafetyInfo, MSSAU, SE); | ||||||||||
1010 | HoistPoint = I; | ||||||||||
1011 | Changed = true; | ||||||||||
1012 | } | ||||||||||
1013 | } | ||||||||||
1014 | } | ||||||||||
1015 | if (VerifyMemorySSA) | ||||||||||
1016 | MSSAU->getMemorySSA()->verifyMemorySSA(); | ||||||||||
1017 | |||||||||||
1018 | // Now that we've finished hoisting make sure that LI and DT are still | ||||||||||
1019 | // valid. | ||||||||||
1020 | #ifdef EXPENSIVE_CHECKS | ||||||||||
1021 | if (Changed) { | ||||||||||
1022 | assert(DT->verify(DominatorTree::VerificationLevel::Fast) &&(static_cast <bool> (DT->verify(DominatorTree::VerificationLevel ::Fast) && "Dominator tree verification failed") ? void (0) : __assert_fail ("DT->verify(DominatorTree::VerificationLevel::Fast) && \"Dominator tree verification failed\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1023, __extension__ __PRETTY_FUNCTION__ )) | ||||||||||
1023 | "Dominator tree verification failed")(static_cast <bool> (DT->verify(DominatorTree::VerificationLevel ::Fast) && "Dominator tree verification failed") ? void (0) : __assert_fail ("DT->verify(DominatorTree::VerificationLevel::Fast) && \"Dominator tree verification failed\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1023, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
1024 | LI->verify(*DT); | ||||||||||
1025 | } | ||||||||||
1026 | #endif | ||||||||||
1027 | |||||||||||
1028 | return Changed; | ||||||||||
1029 | } | ||||||||||
1030 | |||||||||||
1031 | // Return true if LI is invariant within scope of the loop. LI is invariant if | ||||||||||
1032 | // CurLoop is dominated by an invariant.start representing the same memory | ||||||||||
1033 | // location and size as the memory location LI loads from, and also the | ||||||||||
1034 | // invariant.start has no uses. | ||||||||||
1035 | static bool isLoadInvariantInLoop(LoadInst *LI, DominatorTree *DT, | ||||||||||
1036 | Loop *CurLoop) { | ||||||||||
1037 | Value *Addr = LI->getOperand(0); | ||||||||||
1038 | const DataLayout &DL = LI->getModule()->getDataLayout(); | ||||||||||
1039 | const TypeSize LocSizeInBits = DL.getTypeSizeInBits(LI->getType()); | ||||||||||
1040 | |||||||||||
1041 | // It is not currently possible for clang to generate an invariant.start | ||||||||||
1042 | // intrinsic with scalable vector types because we don't support thread local | ||||||||||
1043 | // sizeless types and we don't permit sizeless types in structs or classes. | ||||||||||
1044 | // Furthermore, even if support is added for this in future the intrinsic | ||||||||||
1045 | // itself is defined to have a size of -1 for variable sized objects. This | ||||||||||
1046 | // makes it impossible to verify if the intrinsic envelops our region of | ||||||||||
1047 | // interest. For example, both <vscale x 32 x i8> and <vscale x 16 x i8> | ||||||||||
1048 | // types would have a -1 parameter, but the former is clearly double the size | ||||||||||
1049 | // of the latter. | ||||||||||
1050 | if (LocSizeInBits.isScalable()) | ||||||||||
1051 | return false; | ||||||||||
1052 | |||||||||||
1053 | // if the type is i8 addrspace(x)*, we know this is the type of | ||||||||||
1054 | // llvm.invariant.start operand | ||||||||||
1055 | auto *PtrInt8Ty = PointerType::get(Type::getInt8Ty(LI->getContext()), | ||||||||||
1056 | LI->getPointerAddressSpace()); | ||||||||||
1057 | unsigned BitcastsVisited = 0; | ||||||||||
1058 | // Look through bitcasts until we reach the i8* type (this is invariant.start | ||||||||||
1059 | // operand type). | ||||||||||
1060 | while (Addr->getType() != PtrInt8Ty) { | ||||||||||
1061 | auto *BC = dyn_cast<BitCastInst>(Addr); | ||||||||||
1062 | // Avoid traversing high number of bitcast uses. | ||||||||||
1063 | if (++BitcastsVisited > MaxNumUsesTraversed || !BC) | ||||||||||
1064 | return false; | ||||||||||
1065 | Addr = BC->getOperand(0); | ||||||||||
1066 | } | ||||||||||
1067 | // If we've ended up at a global/constant, bail. We shouldn't be looking at | ||||||||||
1068 | // uselists for non-local Values in a loop pass. | ||||||||||
1069 | if (isa<Constant>(Addr)) | ||||||||||
1070 | return false; | ||||||||||
1071 | |||||||||||
1072 | unsigned UsesVisited = 0; | ||||||||||
1073 | // Traverse all uses of the load operand value, to see if invariant.start is | ||||||||||
1074 | // one of the uses, and whether it dominates the load instruction. | ||||||||||
1075 | for (auto *U : Addr->users()) { | ||||||||||
1076 | // Avoid traversing for Load operand with high number of users. | ||||||||||
1077 | if (++UsesVisited > MaxNumUsesTraversed) | ||||||||||
1078 | return false; | ||||||||||
1079 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(U); | ||||||||||
1080 | // If there are escaping uses of invariant.start instruction, the load maybe | ||||||||||
1081 | // non-invariant. | ||||||||||
1082 | if (!II || II->getIntrinsicID() != Intrinsic::invariant_start || | ||||||||||
1083 | !II->use_empty()) | ||||||||||
1084 | continue; | ||||||||||
1085 | ConstantInt *InvariantSize = cast<ConstantInt>(II->getArgOperand(0)); | ||||||||||
1086 | // The intrinsic supports having a -1 argument for variable sized objects | ||||||||||
1087 | // so we should check for that here. | ||||||||||
1088 | if (InvariantSize->isNegative()) | ||||||||||
1089 | continue; | ||||||||||
1090 | uint64_t InvariantSizeInBits = InvariantSize->getSExtValue() * 8; | ||||||||||
1091 | // Confirm the invariant.start location size contains the load operand size | ||||||||||
1092 | // in bits. Also, the invariant.start should dominate the load, and we | ||||||||||
1093 | // should not hoist the load out of a loop that contains this dominating | ||||||||||
1094 | // invariant.start. | ||||||||||
1095 | if (LocSizeInBits.getFixedSize() <= InvariantSizeInBits && | ||||||||||
1096 | DT->properlyDominates(II->getParent(), CurLoop->getHeader())) | ||||||||||
1097 | return true; | ||||||||||
1098 | } | ||||||||||
1099 | |||||||||||
1100 | return false; | ||||||||||
1101 | } | ||||||||||
1102 | |||||||||||
1103 | namespace { | ||||||||||
1104 | /// Return true if-and-only-if we know how to (mechanically) both hoist and | ||||||||||
1105 | /// sink a given instruction out of a loop. Does not address legality | ||||||||||
1106 | /// concerns such as aliasing or speculation safety. | ||||||||||
1107 | bool isHoistableAndSinkableInst(Instruction &I) { | ||||||||||
1108 | // Only these instructions are hoistable/sinkable. | ||||||||||
1109 | return (isa<LoadInst>(I) || isa<StoreInst>(I) || isa<CallInst>(I) || | ||||||||||
1110 | isa<FenceInst>(I) || isa<CastInst>(I) || isa<UnaryOperator>(I) || | ||||||||||
1111 | isa<BinaryOperator>(I) || isa<SelectInst>(I) || | ||||||||||
1112 | isa<GetElementPtrInst>(I) || isa<CmpInst>(I) || | ||||||||||
1113 | isa<InsertElementInst>(I) || isa<ExtractElementInst>(I) || | ||||||||||
1114 | isa<ShuffleVectorInst>(I) || isa<ExtractValueInst>(I) || | ||||||||||
1115 | isa<InsertValueInst>(I) || isa<FreezeInst>(I)); | ||||||||||
1116 | } | ||||||||||
1117 | /// Return true if all of the alias sets within this AST are known not to | ||||||||||
1118 | /// contain a Mod, or if MSSA knows there are no MemoryDefs in the loop. | ||||||||||
1119 | bool isReadOnly(AliasSetTracker *CurAST, const MemorySSAUpdater *MSSAU, | ||||||||||
1120 | const Loop *L) { | ||||||||||
1121 | if (CurAST) { | ||||||||||
1122 | for (AliasSet &AS : *CurAST) { | ||||||||||
1123 | if (!AS.isForwardingAliasSet() && AS.isMod()) { | ||||||||||
1124 | return false; | ||||||||||
1125 | } | ||||||||||
1126 | } | ||||||||||
1127 | return true; | ||||||||||
1128 | } else { /*MSSAU*/ | ||||||||||
1129 | for (auto *BB : L->getBlocks()) | ||||||||||
1130 | if (MSSAU->getMemorySSA()->getBlockDefs(BB)) | ||||||||||
1131 | return false; | ||||||||||
1132 | return true; | ||||||||||
1133 | } | ||||||||||
1134 | } | ||||||||||
1135 | |||||||||||
1136 | /// Return true if I is the only Instruction with a MemoryAccess in L. | ||||||||||
1137 | bool isOnlyMemoryAccess(const Instruction *I, const Loop *L, | ||||||||||
1138 | const MemorySSAUpdater *MSSAU) { | ||||||||||
1139 | for (auto *BB : L->getBlocks()) | ||||||||||
1140 | if (auto *Accs = MSSAU->getMemorySSA()->getBlockAccesses(BB)) { | ||||||||||
1141 | int NotAPhi = 0; | ||||||||||
1142 | for (const auto &Acc : *Accs) { | ||||||||||
1143 | if (isa<MemoryPhi>(&Acc)) | ||||||||||
1144 | continue; | ||||||||||
1145 | const auto *MUD = cast<MemoryUseOrDef>(&Acc); | ||||||||||
1146 | if (MUD->getMemoryInst() != I || NotAPhi++ == 1) | ||||||||||
1147 | return false; | ||||||||||
1148 | } | ||||||||||
1149 | } | ||||||||||
1150 | return true; | ||||||||||
1151 | } | ||||||||||
1152 | } | ||||||||||
1153 | |||||||||||
1154 | bool llvm::canSinkOrHoistInst(Instruction &I, AAResults *AA, DominatorTree *DT, | ||||||||||
1155 | Loop *CurLoop, AliasSetTracker *CurAST, | ||||||||||
1156 | MemorySSAUpdater *MSSAU, | ||||||||||
1157 | bool TargetExecutesOncePerLoop, | ||||||||||
1158 | SinkAndHoistLICMFlags *Flags, | ||||||||||
1159 | OptimizationRemarkEmitter *ORE) { | ||||||||||
1160 | assert(((CurAST != nullptr) ^ (MSSAU != nullptr)) &&(static_cast <bool> (((CurAST != nullptr) ^ (MSSAU != nullptr )) && "Either AliasSetTracker or MemorySSA should be initialized." ) ? void (0) : __assert_fail ("((CurAST != nullptr) ^ (MSSAU != nullptr)) && \"Either AliasSetTracker or MemorySSA should be initialized.\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1161, __extension__ __PRETTY_FUNCTION__ )) | ||||||||||
| |||||||||||
1161 | "Either AliasSetTracker or MemorySSA should be initialized.")(static_cast <bool> (((CurAST != nullptr) ^ (MSSAU != nullptr )) && "Either AliasSetTracker or MemorySSA should be initialized." ) ? void (0) : __assert_fail ("((CurAST != nullptr) ^ (MSSAU != nullptr)) && \"Either AliasSetTracker or MemorySSA should be initialized.\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1161, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
1162 | |||||||||||
1163 | // If we don't understand the instruction, bail early. | ||||||||||
1164 | if (!isHoistableAndSinkableInst(I)) | ||||||||||
1165 | return false; | ||||||||||
1166 | |||||||||||
1167 | MemorySSA *MSSA = MSSAU
| ||||||||||
1168 | if (MSSA) | ||||||||||
1169 | assert(Flags != nullptr && "Flags cannot be null.")(static_cast <bool> (Flags != nullptr && "Flags cannot be null." ) ? void (0) : __assert_fail ("Flags != nullptr && \"Flags cannot be null.\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1169, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
1170 | |||||||||||
1171 | // Loads have extra constraints we have to verify before we can hoist them. | ||||||||||
1172 | if (LoadInst *LI
| ||||||||||
1173 | if (!LI->isUnordered()) | ||||||||||
1174 | return false; // Don't sink/hoist volatile or ordered atomic loads! | ||||||||||
1175 | |||||||||||
1176 | // Loads from constant memory are always safe to move, even if they end up | ||||||||||
1177 | // in the same alias set as something that ends up being modified. | ||||||||||
1178 | if (AA->pointsToConstantMemory(LI->getOperand(0))) | ||||||||||
1179 | return true; | ||||||||||
1180 | if (LI->hasMetadata(LLVMContext::MD_invariant_load)) | ||||||||||
1181 | return true; | ||||||||||
1182 | |||||||||||
1183 | if (LI->isAtomic() && !TargetExecutesOncePerLoop) | ||||||||||
1184 | return false; // Don't risk duplicating unordered loads | ||||||||||
1185 | |||||||||||
1186 | // This checks for an invariant.start dominating the load. | ||||||||||
1187 | if (isLoadInvariantInLoop(LI, DT, CurLoop)) | ||||||||||
1188 | return true; | ||||||||||
1189 | |||||||||||
1190 | bool Invalidated; | ||||||||||
1191 | if (CurAST) | ||||||||||
1192 | Invalidated = pointerInvalidatedByLoop(MemoryLocation::get(LI), CurAST, | ||||||||||
1193 | CurLoop, AA); | ||||||||||
1194 | else | ||||||||||
1195 | Invalidated = pointerInvalidatedByLoopWithMSSA( | ||||||||||
1196 | MSSA, cast<MemoryUse>(MSSA->getMemoryAccess(LI)), CurLoop, I, *Flags); | ||||||||||
1197 | // Check loop-invariant address because this may also be a sinkable load | ||||||||||
1198 | // whose address is not necessarily loop-invariant. | ||||||||||
1199 | if (ORE && Invalidated && CurLoop->isLoopInvariant(LI->getPointerOperand())) | ||||||||||
1200 | ORE->emit([&]() { | ||||||||||
1201 | return OptimizationRemarkMissed( | ||||||||||
1202 | DEBUG_TYPE"licm", "LoadWithLoopInvariantAddressInvalidated", LI) | ||||||||||
1203 | << "failed to move load with loop-invariant address " | ||||||||||
1204 | "because the loop may invalidate its value"; | ||||||||||
1205 | }); | ||||||||||
1206 | |||||||||||
1207 | return !Invalidated; | ||||||||||
1208 | } else if (CallInst *CI
| ||||||||||
1209 | // Don't sink or hoist dbg info; it's legal, but not useful. | ||||||||||
1210 | if (isa<DbgInfoIntrinsic>(I)) | ||||||||||
1211 | return false; | ||||||||||
1212 | |||||||||||
1213 | // Don't sink calls which can throw. | ||||||||||
1214 | if (CI->mayThrow()) | ||||||||||
1215 | return false; | ||||||||||
1216 | |||||||||||
1217 | // Convergent attribute has been used on operations that involve | ||||||||||
1218 | // inter-thread communication which results are implicitly affected by the | ||||||||||
1219 | // enclosing control flows. It is not safe to hoist or sink such operations | ||||||||||
1220 | // across control flow. | ||||||||||
1221 | if (CI->isConvergent()) | ||||||||||
1222 | return false; | ||||||||||
1223 | |||||||||||
1224 | using namespace PatternMatch; | ||||||||||
1225 | if (match(CI, m_Intrinsic<Intrinsic::assume>())) | ||||||||||
1226 | // Assumes don't actually alias anything or throw | ||||||||||
1227 | return true; | ||||||||||
1228 | |||||||||||
1229 | if (match(CI, m_Intrinsic<Intrinsic::experimental_widenable_condition>())) | ||||||||||
1230 | // Widenable conditions don't actually alias anything or throw | ||||||||||
1231 | return true; | ||||||||||
1232 | |||||||||||
1233 | // Handle simple cases by querying alias analysis. | ||||||||||
1234 | FunctionModRefBehavior Behavior = AA->getModRefBehavior(CI); | ||||||||||
1235 | if (Behavior == FMRB_DoesNotAccessMemory) | ||||||||||
1236 | return true; | ||||||||||
1237 | if (AAResults::onlyReadsMemory(Behavior)) { | ||||||||||
1238 | // A readonly argmemonly function only reads from memory pointed to by | ||||||||||
1239 | // it's arguments with arbitrary offsets. If we can prove there are no | ||||||||||
1240 | // writes to this memory in the loop, we can hoist or sink. | ||||||||||
1241 | if (AAResults::onlyAccessesArgPointees(Behavior)) { | ||||||||||
1242 | // TODO: expand to writeable arguments | ||||||||||
1243 | for (Value *Op : CI->args()) | ||||||||||
1244 | if (Op->getType()->isPointerTy()) { | ||||||||||
1245 | bool Invalidated; | ||||||||||
1246 | if (CurAST
| ||||||||||
1247 | Invalidated = pointerInvalidatedByLoop( | ||||||||||
1248 | MemoryLocation::getBeforeOrAfter(Op), CurAST, CurLoop, AA); | ||||||||||
1249 | else | ||||||||||
1250 | Invalidated = pointerInvalidatedByLoopWithMSSA( | ||||||||||
1251 | MSSA, cast<MemoryUse>(MSSA->getMemoryAccess(CI)), CurLoop, I, | ||||||||||
| |||||||||||
1252 | *Flags); | ||||||||||
1253 | if (Invalidated) | ||||||||||
1254 | return false; | ||||||||||
1255 | } | ||||||||||
1256 | return true; | ||||||||||
1257 | } | ||||||||||
1258 | |||||||||||
1259 | // If this call only reads from memory and there are no writes to memory | ||||||||||
1260 | // in the loop, we can hoist or sink the call as appropriate. | ||||||||||
1261 | if (isReadOnly(CurAST, MSSAU, CurLoop)) | ||||||||||
1262 | return true; | ||||||||||
1263 | } | ||||||||||
1264 | |||||||||||
1265 | // FIXME: This should use mod/ref information to see if we can hoist or | ||||||||||
1266 | // sink the call. | ||||||||||
1267 | |||||||||||
1268 | return false; | ||||||||||
1269 | } else if (auto *FI = dyn_cast<FenceInst>(&I)) { | ||||||||||
1270 | // Fences alias (most) everything to provide ordering. For the moment, | ||||||||||
1271 | // just give up if there are any other memory operations in the loop. | ||||||||||
1272 | if (CurAST) { | ||||||||||
1273 | auto Begin = CurAST->begin(); | ||||||||||
1274 | assert(Begin != CurAST->end() && "must contain FI")(static_cast <bool> (Begin != CurAST->end() && "must contain FI") ? void (0) : __assert_fail ("Begin != CurAST->end() && \"must contain FI\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1274, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
1275 | if (std::next(Begin) != CurAST->end()) | ||||||||||
1276 | // constant memory for instance, TODO: handle better | ||||||||||
1277 | return false; | ||||||||||
1278 | auto *UniqueI = Begin->getUniqueInstruction(); | ||||||||||
1279 | if (!UniqueI) | ||||||||||
1280 | // other memory op, give up | ||||||||||
1281 | return false; | ||||||||||
1282 | (void)FI; // suppress unused variable warning | ||||||||||
1283 | assert(UniqueI == FI && "AS must contain FI")(static_cast <bool> (UniqueI == FI && "AS must contain FI" ) ? void (0) : __assert_fail ("UniqueI == FI && \"AS must contain FI\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1283, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
1284 | return true; | ||||||||||
1285 | } else // MSSAU | ||||||||||
1286 | return isOnlyMemoryAccess(FI, CurLoop, MSSAU); | ||||||||||
1287 | } else if (auto *SI = dyn_cast<StoreInst>(&I)) { | ||||||||||
1288 | if (!SI->isUnordered()) | ||||||||||
1289 | return false; // Don't sink/hoist volatile or ordered atomic store! | ||||||||||
1290 | |||||||||||
1291 | // We can only hoist a store that we can prove writes a value which is not | ||||||||||
1292 | // read or overwritten within the loop. For those cases, we fallback to | ||||||||||
1293 | // load store promotion instead. TODO: We can extend this to cases where | ||||||||||
1294 | // there is exactly one write to the location and that write dominates an | ||||||||||
1295 | // arbitrary number of reads in the loop. | ||||||||||
1296 | if (CurAST) { | ||||||||||
1297 | auto &AS = CurAST->getAliasSetFor(MemoryLocation::get(SI)); | ||||||||||
1298 | |||||||||||
1299 | if (AS.isRef() || !AS.isMustAlias()) | ||||||||||
1300 | // Quick exit test, handled by the full path below as well. | ||||||||||
1301 | return false; | ||||||||||
1302 | auto *UniqueI = AS.getUniqueInstruction(); | ||||||||||
1303 | if (!UniqueI) | ||||||||||
1304 | // other memory op, give up | ||||||||||
1305 | return false; | ||||||||||
1306 | assert(UniqueI == SI && "AS must contain SI")(static_cast <bool> (UniqueI == SI && "AS must contain SI" ) ? void (0) : __assert_fail ("UniqueI == SI && \"AS must contain SI\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1306, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
1307 | return true; | ||||||||||
1308 | } else { // MSSAU | ||||||||||
1309 | if (isOnlyMemoryAccess(SI, CurLoop, MSSAU)) | ||||||||||
1310 | return true; | ||||||||||
1311 | // If there are more accesses than the Promotion cap or no "quota" to | ||||||||||
1312 | // check clobber, then give up as we're not walking a list that long. | ||||||||||
1313 | if (Flags->tooManyMemoryAccesses() || Flags->tooManyClobberingCalls()) | ||||||||||
1314 | return false; | ||||||||||
1315 | // If there are interfering Uses (i.e. their defining access is in the | ||||||||||
1316 | // loop), or ordered loads (stored as Defs!), don't move this store. | ||||||||||
1317 | // Could do better here, but this is conservatively correct. | ||||||||||
1318 | // TODO: Cache set of Uses on the first walk in runOnLoop, update when | ||||||||||
1319 | // moving accesses. Can also extend to dominating uses. | ||||||||||
1320 | auto *SIMD = MSSA->getMemoryAccess(SI); | ||||||||||
1321 | for (auto *BB : CurLoop->getBlocks()) | ||||||||||
1322 | if (auto *Accesses = MSSA->getBlockAccesses(BB)) { | ||||||||||
1323 | for (const auto &MA : *Accesses) | ||||||||||
1324 | if (const auto *MU = dyn_cast<MemoryUse>(&MA)) { | ||||||||||
1325 | auto *MD = MU->getDefiningAccess(); | ||||||||||
1326 | if (!MSSA->isLiveOnEntryDef(MD) && | ||||||||||
1327 | CurLoop->contains(MD->getBlock())) | ||||||||||
1328 | return false; | ||||||||||
1329 | // Disable hoisting past potentially interfering loads. Optimized | ||||||||||
1330 | // Uses may point to an access outside the loop, as getClobbering | ||||||||||
1331 | // checks the previous iteration when walking the backedge. | ||||||||||
1332 | // FIXME: More precise: no Uses that alias SI. | ||||||||||
1333 | if (!Flags->getIsSink() && !MSSA->dominates(SIMD, MU)) | ||||||||||
1334 | return false; | ||||||||||
1335 | } else if (const auto *MD = dyn_cast<MemoryDef>(&MA)) { | ||||||||||
1336 | if (auto *LI = dyn_cast<LoadInst>(MD->getMemoryInst())) { | ||||||||||
1337 | (void)LI; // Silence warning. | ||||||||||
1338 | assert(!LI->isUnordered() && "Expected unordered load")(static_cast <bool> (!LI->isUnordered() && "Expected unordered load" ) ? void (0) : __assert_fail ("!LI->isUnordered() && \"Expected unordered load\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1338, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
1339 | return false; | ||||||||||
1340 | } | ||||||||||
1341 | // Any call, while it may not be clobbering SI, it may be a use. | ||||||||||
1342 | if (auto *CI = dyn_cast<CallInst>(MD->getMemoryInst())) { | ||||||||||
1343 | // Check if the call may read from the memory location written | ||||||||||
1344 | // to by SI. Check CI's attributes and arguments; the number of | ||||||||||
1345 | // such checks performed is limited above by NoOfMemAccTooLarge. | ||||||||||
1346 | ModRefInfo MRI = AA->getModRefInfo(CI, MemoryLocation::get(SI)); | ||||||||||
1347 | if (isModOrRefSet(MRI)) | ||||||||||
1348 | return false; | ||||||||||
1349 | } | ||||||||||
1350 | } | ||||||||||
1351 | } | ||||||||||
1352 | auto *Source = MSSA->getSkipSelfWalker()->getClobberingMemoryAccess(SI); | ||||||||||
1353 | Flags->incrementClobberingCalls(); | ||||||||||
1354 | // If there are no clobbering Defs in the loop, store is safe to hoist. | ||||||||||
1355 | return MSSA->isLiveOnEntryDef(Source) || | ||||||||||
1356 | !CurLoop->contains(Source->getBlock()); | ||||||||||
1357 | } | ||||||||||
1358 | } | ||||||||||
1359 | |||||||||||
1360 | assert(!I.mayReadOrWriteMemory() && "unhandled aliasing")(static_cast <bool> (!I.mayReadOrWriteMemory() && "unhandled aliasing") ? void (0) : __assert_fail ("!I.mayReadOrWriteMemory() && \"unhandled aliasing\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1360, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
1361 | |||||||||||
1362 | // We've established mechanical ability and aliasing, it's up to the caller | ||||||||||
1363 | // to check fault safety | ||||||||||
1364 | return true; | ||||||||||
1365 | } | ||||||||||
1366 | |||||||||||
1367 | /// Returns true if a PHINode is a trivially replaceable with an | ||||||||||
1368 | /// Instruction. | ||||||||||
1369 | /// This is true when all incoming values are that instruction. | ||||||||||
1370 | /// This pattern occurs most often with LCSSA PHI nodes. | ||||||||||
1371 | /// | ||||||||||
1372 | static bool isTriviallyReplaceablePHI(const PHINode &PN, const Instruction &I) { | ||||||||||
1373 | for (const Value *IncValue : PN.incoming_values()) | ||||||||||
1374 | if (IncValue != &I) | ||||||||||
1375 | return false; | ||||||||||
1376 | |||||||||||
1377 | return true; | ||||||||||
1378 | } | ||||||||||
1379 | |||||||||||
1380 | /// Return true if the instruction is free in the loop. | ||||||||||
1381 | static bool isFreeInLoop(const Instruction &I, const Loop *CurLoop, | ||||||||||
1382 | const TargetTransformInfo *TTI) { | ||||||||||
1383 | |||||||||||
1384 | if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) { | ||||||||||
1385 | if (TTI->getUserCost(GEP, TargetTransformInfo::TCK_SizeAndLatency) != | ||||||||||
1386 | TargetTransformInfo::TCC_Free) | ||||||||||
1387 | return false; | ||||||||||
1388 | // For a GEP, we cannot simply use getUserCost because currently it | ||||||||||
1389 | // optimistically assumes that a GEP will fold into addressing mode | ||||||||||
1390 | // regardless of its users. | ||||||||||
1391 | const BasicBlock *BB = GEP->getParent(); | ||||||||||
1392 | for (const User *U : GEP->users()) { | ||||||||||
1393 | const Instruction *UI = cast<Instruction>(U); | ||||||||||
1394 | if (CurLoop->contains(UI) && | ||||||||||
1395 | (BB != UI->getParent() || | ||||||||||
1396 | (!isa<StoreInst>(UI) && !isa<LoadInst>(UI)))) | ||||||||||
1397 | return false; | ||||||||||
1398 | } | ||||||||||
1399 | return true; | ||||||||||
1400 | } else | ||||||||||
1401 | return TTI->getUserCost(&I, TargetTransformInfo::TCK_SizeAndLatency) == | ||||||||||
1402 | TargetTransformInfo::TCC_Free; | ||||||||||
1403 | } | ||||||||||
1404 | |||||||||||
1405 | /// Return true if the only users of this instruction are outside of | ||||||||||
1406 | /// the loop. If this is true, we can sink the instruction to the exit | ||||||||||
1407 | /// blocks of the loop. | ||||||||||
1408 | /// | ||||||||||
1409 | /// We also return true if the instruction could be folded away in lowering. | ||||||||||
1410 | /// (e.g., a GEP can be folded into a load as an addressing mode in the loop). | ||||||||||
1411 | static bool isNotUsedOrFreeInLoop(const Instruction &I, const Loop *CurLoop, | ||||||||||
1412 | const LoopSafetyInfo *SafetyInfo, | ||||||||||
1413 | TargetTransformInfo *TTI, bool &FreeInLoop, | ||||||||||
1414 | bool LoopNestMode) { | ||||||||||
1415 | const auto &BlockColors = SafetyInfo->getBlockColors(); | ||||||||||
1416 | bool IsFree = isFreeInLoop(I, CurLoop, TTI); | ||||||||||
1417 | for (const User *U : I.users()) { | ||||||||||
1418 | const Instruction *UI = cast<Instruction>(U); | ||||||||||
1419 | if (const PHINode *PN = dyn_cast<PHINode>(UI)) { | ||||||||||
1420 | const BasicBlock *BB = PN->getParent(); | ||||||||||
1421 | // We cannot sink uses in catchswitches. | ||||||||||
1422 | if (isa<CatchSwitchInst>(BB->getTerminator())) | ||||||||||
1423 | return false; | ||||||||||
1424 | |||||||||||
1425 | // We need to sink a callsite to a unique funclet. Avoid sinking if the | ||||||||||
1426 | // phi use is too muddled. | ||||||||||
1427 | if (isa<CallInst>(I)) | ||||||||||
1428 | if (!BlockColors.empty() && | ||||||||||
1429 | BlockColors.find(const_cast<BasicBlock *>(BB))->second.size() != 1) | ||||||||||
1430 | return false; | ||||||||||
1431 | |||||||||||
1432 | if (LoopNestMode) { | ||||||||||
1433 | while (isa<PHINode>(UI) && UI->hasOneUser() && | ||||||||||
1434 | UI->getNumOperands() == 1) { | ||||||||||
1435 | if (!CurLoop->contains(UI)) | ||||||||||
1436 | break; | ||||||||||
1437 | UI = cast<Instruction>(UI->user_back()); | ||||||||||
1438 | } | ||||||||||
1439 | } | ||||||||||
1440 | } | ||||||||||
1441 | |||||||||||
1442 | if (CurLoop->contains(UI)) { | ||||||||||
1443 | if (IsFree) { | ||||||||||
1444 | FreeInLoop = true; | ||||||||||
1445 | continue; | ||||||||||
1446 | } | ||||||||||
1447 | return false; | ||||||||||
1448 | } | ||||||||||
1449 | } | ||||||||||
1450 | return true; | ||||||||||
1451 | } | ||||||||||
1452 | |||||||||||
1453 | static Instruction *cloneInstructionInExitBlock( | ||||||||||
1454 | Instruction &I, BasicBlock &ExitBlock, PHINode &PN, const LoopInfo *LI, | ||||||||||
1455 | const LoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU) { | ||||||||||
1456 | Instruction *New; | ||||||||||
1457 | if (auto *CI = dyn_cast<CallInst>(&I)) { | ||||||||||
1458 | const auto &BlockColors = SafetyInfo->getBlockColors(); | ||||||||||
1459 | |||||||||||
1460 | // Sinking call-sites need to be handled differently from other | ||||||||||
1461 | // instructions. The cloned call-site needs a funclet bundle operand | ||||||||||
1462 | // appropriate for its location in the CFG. | ||||||||||
1463 | SmallVector<OperandBundleDef, 1> OpBundles; | ||||||||||
1464 | for (unsigned BundleIdx = 0, BundleEnd = CI->getNumOperandBundles(); | ||||||||||
1465 | BundleIdx != BundleEnd; ++BundleIdx) { | ||||||||||
1466 | OperandBundleUse Bundle = CI->getOperandBundleAt(BundleIdx); | ||||||||||
1467 | if (Bundle.getTagID() == LLVMContext::OB_funclet) | ||||||||||
1468 | continue; | ||||||||||
1469 | |||||||||||
1470 | OpBundles.emplace_back(Bundle); | ||||||||||
1471 | } | ||||||||||
1472 | |||||||||||
1473 | if (!BlockColors.empty()) { | ||||||||||
1474 | const ColorVector &CV = BlockColors.find(&ExitBlock)->second; | ||||||||||
1475 | assert(CV.size() == 1 && "non-unique color for exit block!")(static_cast <bool> (CV.size() == 1 && "non-unique color for exit block!" ) ? void (0) : __assert_fail ("CV.size() == 1 && \"non-unique color for exit block!\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1475, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
1476 | BasicBlock *BBColor = CV.front(); | ||||||||||
1477 | Instruction *EHPad = BBColor->getFirstNonPHI(); | ||||||||||
1478 | if (EHPad->isEHPad()) | ||||||||||
1479 | OpBundles.emplace_back("funclet", EHPad); | ||||||||||
1480 | } | ||||||||||
1481 | |||||||||||
1482 | New = CallInst::Create(CI, OpBundles); | ||||||||||
1483 | } else { | ||||||||||
1484 | New = I.clone(); | ||||||||||
1485 | } | ||||||||||
1486 | |||||||||||
1487 | ExitBlock.getInstList().insert(ExitBlock.getFirstInsertionPt(), New); | ||||||||||
1488 | if (!I.getName().empty()) | ||||||||||
1489 | New->setName(I.getName() + ".le"); | ||||||||||
1490 | |||||||||||
1491 | if (MSSAU && MSSAU->getMemorySSA()->getMemoryAccess(&I)) { | ||||||||||
1492 | // Create a new MemoryAccess and let MemorySSA set its defining access. | ||||||||||
1493 | MemoryAccess *NewMemAcc = MSSAU->createMemoryAccessInBB( | ||||||||||
1494 | New, nullptr, New->getParent(), MemorySSA::Beginning); | ||||||||||
1495 | if (NewMemAcc) { | ||||||||||
1496 | if (auto *MemDef = dyn_cast<MemoryDef>(NewMemAcc)) | ||||||||||
1497 | MSSAU->insertDef(MemDef, /*RenameUses=*/true); | ||||||||||
1498 | else { | ||||||||||
1499 | auto *MemUse = cast<MemoryUse>(NewMemAcc); | ||||||||||
1500 | MSSAU->insertUse(MemUse, /*RenameUses=*/true); | ||||||||||
1501 | } | ||||||||||
1502 | } | ||||||||||
1503 | } | ||||||||||
1504 | |||||||||||
1505 | // Build LCSSA PHI nodes for any in-loop operands (if legal). Note that | ||||||||||
1506 | // this is particularly cheap because we can rip off the PHI node that we're | ||||||||||
1507 | // replacing for the number and blocks of the predecessors. | ||||||||||
1508 | // OPT: If this shows up in a profile, we can instead finish sinking all | ||||||||||
1509 | // invariant instructions, and then walk their operands to re-establish | ||||||||||
1510 | // LCSSA. That will eliminate creating PHI nodes just to nuke them when | ||||||||||
1511 | // sinking bottom-up. | ||||||||||
1512 | for (Use &Op : New->operands()) | ||||||||||
1513 | if (LI->wouldBeOutOfLoopUseRequiringLCSSA(Op.get(), PN.getParent())) { | ||||||||||
1514 | auto *OInst = cast<Instruction>(Op.get()); | ||||||||||
1515 | PHINode *OpPN = | ||||||||||
1516 | PHINode::Create(OInst->getType(), PN.getNumIncomingValues(), | ||||||||||
1517 | OInst->getName() + ".lcssa", &ExitBlock.front()); | ||||||||||
1518 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) | ||||||||||
1519 | OpPN->addIncoming(OInst, PN.getIncomingBlock(i)); | ||||||||||
1520 | Op = OpPN; | ||||||||||
1521 | } | ||||||||||
1522 | return New; | ||||||||||
1523 | } | ||||||||||
1524 | |||||||||||
1525 | static void eraseInstruction(Instruction &I, ICFLoopSafetyInfo &SafetyInfo, | ||||||||||
1526 | MemorySSAUpdater *MSSAU) { | ||||||||||
1527 | if (MSSAU) | ||||||||||
1528 | MSSAU->removeMemoryAccess(&I); | ||||||||||
1529 | SafetyInfo.removeInstruction(&I); | ||||||||||
1530 | I.eraseFromParent(); | ||||||||||
1531 | } | ||||||||||
1532 | |||||||||||
1533 | static void moveInstructionBefore(Instruction &I, Instruction &Dest, | ||||||||||
1534 | ICFLoopSafetyInfo &SafetyInfo, | ||||||||||
1535 | MemorySSAUpdater *MSSAU, | ||||||||||
1536 | ScalarEvolution *SE) { | ||||||||||
1537 | SafetyInfo.removeInstruction(&I); | ||||||||||
1538 | SafetyInfo.insertInstructionTo(&I, Dest.getParent()); | ||||||||||
1539 | I.moveBefore(&Dest); | ||||||||||
1540 | if (MSSAU) | ||||||||||
1541 | if (MemoryUseOrDef *OldMemAcc = cast_or_null<MemoryUseOrDef>( | ||||||||||
1542 | MSSAU->getMemorySSA()->getMemoryAccess(&I))) | ||||||||||
1543 | MSSAU->moveToPlace(OldMemAcc, Dest.getParent(), | ||||||||||
1544 | MemorySSA::BeforeTerminator); | ||||||||||
1545 | if (SE) | ||||||||||
1546 | SE->forgetValue(&I); | ||||||||||
1547 | } | ||||||||||
1548 | |||||||||||
1549 | static Instruction *sinkThroughTriviallyReplaceablePHI( | ||||||||||
1550 | PHINode *TPN, Instruction *I, LoopInfo *LI, | ||||||||||
1551 | SmallDenseMap<BasicBlock *, Instruction *, 32> &SunkCopies, | ||||||||||
1552 | const LoopSafetyInfo *SafetyInfo, const Loop *CurLoop, | ||||||||||
1553 | MemorySSAUpdater *MSSAU) { | ||||||||||
1554 | assert(isTriviallyReplaceablePHI(*TPN, *I) &&(static_cast <bool> (isTriviallyReplaceablePHI(*TPN, *I ) && "Expect only trivially replaceable PHI") ? void ( 0) : __assert_fail ("isTriviallyReplaceablePHI(*TPN, *I) && \"Expect only trivially replaceable PHI\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1555, __extension__ __PRETTY_FUNCTION__ )) | ||||||||||
1555 | "Expect only trivially replaceable PHI")(static_cast <bool> (isTriviallyReplaceablePHI(*TPN, *I ) && "Expect only trivially replaceable PHI") ? void ( 0) : __assert_fail ("isTriviallyReplaceablePHI(*TPN, *I) && \"Expect only trivially replaceable PHI\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1555, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
1556 | BasicBlock *ExitBlock = TPN->getParent(); | ||||||||||
1557 | Instruction *New; | ||||||||||
1558 | auto It = SunkCopies.find(ExitBlock); | ||||||||||
1559 | if (It != SunkCopies.end()) | ||||||||||
1560 | New = It->second; | ||||||||||
1561 | else | ||||||||||
1562 | New = SunkCopies[ExitBlock] = cloneInstructionInExitBlock( | ||||||||||
1563 | *I, *ExitBlock, *TPN, LI, SafetyInfo, MSSAU); | ||||||||||
1564 | return New; | ||||||||||
1565 | } | ||||||||||
1566 | |||||||||||
1567 | static bool canSplitPredecessors(PHINode *PN, LoopSafetyInfo *SafetyInfo) { | ||||||||||
1568 | BasicBlock *BB = PN->getParent(); | ||||||||||
1569 | if (!BB->canSplitPredecessors()) | ||||||||||
1570 | return false; | ||||||||||
1571 | // It's not impossible to split EHPad blocks, but if BlockColors already exist | ||||||||||
1572 | // it require updating BlockColors for all offspring blocks accordingly. By | ||||||||||
1573 | // skipping such corner case, we can make updating BlockColors after splitting | ||||||||||
1574 | // predecessor fairly simple. | ||||||||||
1575 | if (!SafetyInfo->getBlockColors().empty() && BB->getFirstNonPHI()->isEHPad()) | ||||||||||
1576 | return false; | ||||||||||
1577 | for (BasicBlock *BBPred : predecessors(BB)) { | ||||||||||
1578 | if (isa<IndirectBrInst>(BBPred->getTerminator()) || | ||||||||||
1579 | isa<CallBrInst>(BBPred->getTerminator())) | ||||||||||
1580 | return false; | ||||||||||
1581 | } | ||||||||||
1582 | return true; | ||||||||||
1583 | } | ||||||||||
1584 | |||||||||||
1585 | static void splitPredecessorsOfLoopExit(PHINode *PN, DominatorTree *DT, | ||||||||||
1586 | LoopInfo *LI, const Loop *CurLoop, | ||||||||||
1587 | LoopSafetyInfo *SafetyInfo, | ||||||||||
1588 | MemorySSAUpdater *MSSAU) { | ||||||||||
1589 | #ifndef NDEBUG | ||||||||||
1590 | SmallVector<BasicBlock *, 32> ExitBlocks; | ||||||||||
1591 | CurLoop->getUniqueExitBlocks(ExitBlocks); | ||||||||||
1592 | SmallPtrSet<BasicBlock *, 32> ExitBlockSet(ExitBlocks.begin(), | ||||||||||
1593 | ExitBlocks.end()); | ||||||||||
1594 | #endif | ||||||||||
1595 | BasicBlock *ExitBB = PN->getParent(); | ||||||||||
1596 | assert(ExitBlockSet.count(ExitBB) && "Expect the PHI is in an exit block.")(static_cast <bool> (ExitBlockSet.count(ExitBB) && "Expect the PHI is in an exit block.") ? void (0) : __assert_fail ("ExitBlockSet.count(ExitBB) && \"Expect the PHI is in an exit block.\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1596, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
1597 | |||||||||||
1598 | // Split predecessors of the loop exit to make instructions in the loop are | ||||||||||
1599 | // exposed to exit blocks through trivially replaceable PHIs while keeping the | ||||||||||
1600 | // loop in the canonical form where each predecessor of each exit block should | ||||||||||
1601 | // be contained within the loop. For example, this will convert the loop below | ||||||||||
1602 | // from | ||||||||||
1603 | // | ||||||||||
1604 | // LB1: | ||||||||||
1605 | // %v1 = | ||||||||||
1606 | // br %LE, %LB2 | ||||||||||
1607 | // LB2: | ||||||||||
1608 | // %v2 = | ||||||||||
1609 | // br %LE, %LB1 | ||||||||||
1610 | // LE: | ||||||||||
1611 | // %p = phi [%v1, %LB1], [%v2, %LB2] <-- non-trivially replaceable | ||||||||||
1612 | // | ||||||||||
1613 | // to | ||||||||||
1614 | // | ||||||||||
1615 | // LB1: | ||||||||||
1616 | // %v1 = | ||||||||||
1617 | // br %LE.split, %LB2 | ||||||||||
1618 | // LB2: | ||||||||||
1619 | // %v2 = | ||||||||||
1620 | // br %LE.split2, %LB1 | ||||||||||
1621 | // LE.split: | ||||||||||
1622 | // %p1 = phi [%v1, %LB1] <-- trivially replaceable | ||||||||||
1623 | // br %LE | ||||||||||
1624 | // LE.split2: | ||||||||||
1625 | // %p2 = phi [%v2, %LB2] <-- trivially replaceable | ||||||||||
1626 | // br %LE | ||||||||||
1627 | // LE: | ||||||||||
1628 | // %p = phi [%p1, %LE.split], [%p2, %LE.split2] | ||||||||||
1629 | // | ||||||||||
1630 | const auto &BlockColors = SafetyInfo->getBlockColors(); | ||||||||||
1631 | SmallSetVector<BasicBlock *, 8> PredBBs(pred_begin(ExitBB), pred_end(ExitBB)); | ||||||||||
1632 | while (!PredBBs.empty()) { | ||||||||||
1633 | BasicBlock *PredBB = *PredBBs.begin(); | ||||||||||
1634 | assert(CurLoop->contains(PredBB) &&(static_cast <bool> (CurLoop->contains(PredBB) && "Expect all predecessors are in the loop") ? void (0) : __assert_fail ("CurLoop->contains(PredBB) && \"Expect all predecessors are in the loop\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1635, __extension__ __PRETTY_FUNCTION__ )) | ||||||||||
1635 | "Expect all predecessors are in the loop")(static_cast <bool> (CurLoop->contains(PredBB) && "Expect all predecessors are in the loop") ? void (0) : __assert_fail ("CurLoop->contains(PredBB) && \"Expect all predecessors are in the loop\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1635, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
1636 | if (PN->getBasicBlockIndex(PredBB) >= 0) { | ||||||||||
1637 | BasicBlock *NewPred = SplitBlockPredecessors( | ||||||||||
1638 | ExitBB, PredBB, ".split.loop.exit", DT, LI, MSSAU, true); | ||||||||||
1639 | // Since we do not allow splitting EH-block with BlockColors in | ||||||||||
1640 | // canSplitPredecessors(), we can simply assign predecessor's color to | ||||||||||
1641 | // the new block. | ||||||||||
1642 | if (!BlockColors.empty()) | ||||||||||
1643 | // Grab a reference to the ColorVector to be inserted before getting the | ||||||||||
1644 | // reference to the vector we are copying because inserting the new | ||||||||||
1645 | // element in BlockColors might cause the map to be reallocated. | ||||||||||
1646 | SafetyInfo->copyColors(NewPred, PredBB); | ||||||||||
1647 | } | ||||||||||
1648 | PredBBs.remove(PredBB); | ||||||||||
1649 | } | ||||||||||
1650 | } | ||||||||||
1651 | |||||||||||
1652 | /// When an instruction is found to only be used outside of the loop, this | ||||||||||
1653 | /// function moves it to the exit blocks and patches up SSA form as needed. | ||||||||||
1654 | /// This method is guaranteed to remove the original instruction from its | ||||||||||
1655 | /// position, and may either delete it or move it to outside of the loop. | ||||||||||
1656 | /// | ||||||||||
1657 | static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT, | ||||||||||
1658 | BlockFrequencyInfo *BFI, const Loop *CurLoop, | ||||||||||
1659 | ICFLoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU, | ||||||||||
1660 | OptimizationRemarkEmitter *ORE) { | ||||||||||
1661 | bool Changed = false; | ||||||||||
1662 | LLVM_DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM sinking instruction: " << I << "\n"; } } while (false); | ||||||||||
1663 | |||||||||||
1664 | // Iterate over users to be ready for actual sinking. Replace users via | ||||||||||
1665 | // unreachable blocks with undef and make all user PHIs trivially replaceable. | ||||||||||
1666 | SmallPtrSet<Instruction *, 8> VisitedUsers; | ||||||||||
1667 | for (Value::user_iterator UI = I.user_begin(), UE = I.user_end(); UI != UE;) { | ||||||||||
1668 | auto *User = cast<Instruction>(*UI); | ||||||||||
1669 | Use &U = UI.getUse(); | ||||||||||
1670 | ++UI; | ||||||||||
1671 | |||||||||||
1672 | if (VisitedUsers.count(User) || CurLoop->contains(User)) | ||||||||||
1673 | continue; | ||||||||||
1674 | |||||||||||
1675 | if (!DT->isReachableFromEntry(User->getParent())) { | ||||||||||
1676 | U = UndefValue::get(I.getType()); | ||||||||||
1677 | Changed = true; | ||||||||||
1678 | continue; | ||||||||||
1679 | } | ||||||||||
1680 | |||||||||||
1681 | // The user must be a PHI node. | ||||||||||
1682 | PHINode *PN = cast<PHINode>(User); | ||||||||||
1683 | |||||||||||
1684 | // Surprisingly, instructions can be used outside of loops without any | ||||||||||
1685 | // exits. This can only happen in PHI nodes if the incoming block is | ||||||||||
1686 | // unreachable. | ||||||||||
1687 | BasicBlock *BB = PN->getIncomingBlock(U); | ||||||||||
1688 | if (!DT->isReachableFromEntry(BB)) { | ||||||||||
1689 | U = UndefValue::get(I.getType()); | ||||||||||
1690 | Changed = true; | ||||||||||
1691 | continue; | ||||||||||
1692 | } | ||||||||||
1693 | |||||||||||
1694 | VisitedUsers.insert(PN); | ||||||||||
1695 | if (isTriviallyReplaceablePHI(*PN, I)) | ||||||||||
1696 | continue; | ||||||||||
1697 | |||||||||||
1698 | if (!canSplitPredecessors(PN, SafetyInfo)) | ||||||||||
1699 | return Changed; | ||||||||||
1700 | |||||||||||
1701 | // Split predecessors of the PHI so that we can make users trivially | ||||||||||
1702 | // replaceable. | ||||||||||
1703 | splitPredecessorsOfLoopExit(PN, DT, LI, CurLoop, SafetyInfo, MSSAU); | ||||||||||
1704 | |||||||||||
1705 | // Should rebuild the iterators, as they may be invalidated by | ||||||||||
1706 | // splitPredecessorsOfLoopExit(). | ||||||||||
1707 | UI = I.user_begin(); | ||||||||||
1708 | UE = I.user_end(); | ||||||||||
1709 | } | ||||||||||
1710 | |||||||||||
1711 | if (VisitedUsers.empty()) | ||||||||||
1712 | return Changed; | ||||||||||
1713 | |||||||||||
1714 | ORE->emit([&]() { | ||||||||||
1715 | return OptimizationRemark(DEBUG_TYPE"licm", "InstSunk", &I) | ||||||||||
1716 | << "sinking " << ore::NV("Inst", &I); | ||||||||||
1717 | }); | ||||||||||
1718 | if (isa<LoadInst>(I)) | ||||||||||
1719 | ++NumMovedLoads; | ||||||||||
1720 | else if (isa<CallInst>(I)) | ||||||||||
1721 | ++NumMovedCalls; | ||||||||||
1722 | ++NumSunk; | ||||||||||
1723 | |||||||||||
1724 | #ifndef NDEBUG | ||||||||||
1725 | SmallVector<BasicBlock *, 32> ExitBlocks; | ||||||||||
1726 | CurLoop->getUniqueExitBlocks(ExitBlocks); | ||||||||||
1727 | SmallPtrSet<BasicBlock *, 32> ExitBlockSet(ExitBlocks.begin(), | ||||||||||
1728 | ExitBlocks.end()); | ||||||||||
1729 | #endif | ||||||||||
1730 | |||||||||||
1731 | // Clones of this instruction. Don't create more than one per exit block! | ||||||||||
1732 | SmallDenseMap<BasicBlock *, Instruction *, 32> SunkCopies; | ||||||||||
1733 | |||||||||||
1734 | // If this instruction is only used outside of the loop, then all users are | ||||||||||
1735 | // PHI nodes in exit blocks due to LCSSA form. Just RAUW them with clones of | ||||||||||
1736 | // the instruction. | ||||||||||
1737 | // First check if I is worth sinking for all uses. Sink only when it is worth | ||||||||||
1738 | // across all uses. | ||||||||||
1739 | SmallSetVector<User*, 8> Users(I.user_begin(), I.user_end()); | ||||||||||
1740 | for (auto *UI : Users) { | ||||||||||
1741 | auto *User = cast<Instruction>(UI); | ||||||||||
1742 | |||||||||||
1743 | if (CurLoop->contains(User)) | ||||||||||
1744 | continue; | ||||||||||
1745 | |||||||||||
1746 | PHINode *PN = cast<PHINode>(User); | ||||||||||
1747 | assert(ExitBlockSet.count(PN->getParent()) &&(static_cast <bool> (ExitBlockSet.count(PN->getParent ()) && "The LCSSA PHI is not in an exit block!") ? void (0) : __assert_fail ("ExitBlockSet.count(PN->getParent()) && \"The LCSSA PHI is not in an exit block!\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1748, __extension__ __PRETTY_FUNCTION__ )) | ||||||||||
1748 | "The LCSSA PHI is not in an exit block!")(static_cast <bool> (ExitBlockSet.count(PN->getParent ()) && "The LCSSA PHI is not in an exit block!") ? void (0) : __assert_fail ("ExitBlockSet.count(PN->getParent()) && \"The LCSSA PHI is not in an exit block!\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1748, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
1749 | |||||||||||
1750 | // The PHI must be trivially replaceable. | ||||||||||
1751 | Instruction *New = sinkThroughTriviallyReplaceablePHI( | ||||||||||
1752 | PN, &I, LI, SunkCopies, SafetyInfo, CurLoop, MSSAU); | ||||||||||
1753 | PN->replaceAllUsesWith(New); | ||||||||||
1754 | eraseInstruction(*PN, *SafetyInfo, nullptr); | ||||||||||
1755 | Changed = true; | ||||||||||
1756 | } | ||||||||||
1757 | return Changed; | ||||||||||
1758 | } | ||||||||||
1759 | |||||||||||
1760 | /// When an instruction is found to only use loop invariant operands that | ||||||||||
1761 | /// is safe to hoist, this instruction is called to do the dirty work. | ||||||||||
1762 | /// | ||||||||||
1763 | static void hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop, | ||||||||||
1764 | BasicBlock *Dest, ICFLoopSafetyInfo *SafetyInfo, | ||||||||||
1765 | MemorySSAUpdater *MSSAU, ScalarEvolution *SE, | ||||||||||
1766 | OptimizationRemarkEmitter *ORE) { | ||||||||||
1767 | 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) | ||||||||||
1768 | << I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM hoisting to " << Dest ->getNameOrAsOperand() << ": " << I << "\n" ; } } while (false); | ||||||||||
1769 | ORE->emit([&]() { | ||||||||||
1770 | return OptimizationRemark(DEBUG_TYPE"licm", "Hoisted", &I) << "hoisting " | ||||||||||
1771 | << ore::NV("Inst", &I); | ||||||||||
1772 | }); | ||||||||||
1773 | |||||||||||
1774 | // Metadata can be dependent on conditions we are hoisting above. | ||||||||||
1775 | // Conservatively strip all metadata on the instruction unless we were | ||||||||||
1776 | // guaranteed to execute I if we entered the loop, in which case the metadata | ||||||||||
1777 | // is valid in the loop preheader. | ||||||||||
1778 | // Similarly, If I is a call and it is not guaranteed to execute in the loop, | ||||||||||
1779 | // then moving to the preheader means we should strip attributes on the call | ||||||||||
1780 | // that can cause UB since we may be hoisting above conditions that allowed | ||||||||||
1781 | // inferring those attributes. They may not be valid at the preheader. | ||||||||||
1782 | if ((I.hasMetadataOtherThanDebugLoc() || isa<CallInst>(I)) && | ||||||||||
1783 | // The check on hasMetadataOtherThanDebugLoc is to prevent us from burning | ||||||||||
1784 | // time in isGuaranteedToExecute if we don't actually have anything to | ||||||||||
1785 | // drop. It is a compile time optimization, not required for correctness. | ||||||||||
1786 | !SafetyInfo->isGuaranteedToExecute(I, DT, CurLoop)) | ||||||||||
1787 | I.dropUndefImplyingAttrsAndUnknownMetadata(); | ||||||||||
1788 | |||||||||||
1789 | if (isa<PHINode>(I)) | ||||||||||
1790 | // Move the new node to the end of the phi list in the destination block. | ||||||||||
1791 | moveInstructionBefore(I, *Dest->getFirstNonPHI(), *SafetyInfo, MSSAU, SE); | ||||||||||
1792 | else | ||||||||||
1793 | // Move the new node to the destination block, before its terminator. | ||||||||||
1794 | moveInstructionBefore(I, *Dest->getTerminator(), *SafetyInfo, MSSAU, SE); | ||||||||||
1795 | |||||||||||
1796 | I.updateLocationAfterHoist(); | ||||||||||
1797 | |||||||||||
1798 | if (isa<LoadInst>(I)) | ||||||||||
1799 | ++NumMovedLoads; | ||||||||||
1800 | else if (isa<CallInst>(I)) | ||||||||||
1801 | ++NumMovedCalls; | ||||||||||
1802 | ++NumHoisted; | ||||||||||
1803 | } | ||||||||||
1804 | |||||||||||
1805 | /// Only sink or hoist an instruction if it is not a trapping instruction, | ||||||||||
1806 | /// or if the instruction is known not to trap when moved to the preheader. | ||||||||||
1807 | /// or if it is a trapping instruction and is guaranteed to execute. | ||||||||||
1808 | static bool isSafeToExecuteUnconditionally( | ||||||||||
1809 | Instruction &Inst, const DominatorTree *DT, const TargetLibraryInfo *TLI, | ||||||||||
1810 | const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo, | ||||||||||
1811 | OptimizationRemarkEmitter *ORE, const Instruction *CtxI, | ||||||||||
1812 | bool AllowSpeculation) { | ||||||||||
1813 | if (AllowSpeculation && isSafeToSpeculativelyExecute(&Inst, CtxI, DT, TLI)) | ||||||||||
1814 | return true; | ||||||||||
1815 | |||||||||||
1816 | bool GuaranteedToExecute = | ||||||||||
1817 | SafetyInfo->isGuaranteedToExecute(Inst, DT, CurLoop); | ||||||||||
1818 | |||||||||||
1819 | if (!GuaranteedToExecute) { | ||||||||||
1820 | auto *LI = dyn_cast<LoadInst>(&Inst); | ||||||||||
1821 | if (LI && CurLoop->isLoopInvariant(LI->getPointerOperand())) | ||||||||||
1822 | ORE->emit([&]() { | ||||||||||
1823 | return OptimizationRemarkMissed( | ||||||||||
1824 | DEBUG_TYPE"licm", "LoadWithLoopInvariantAddressCondExecuted", LI) | ||||||||||
1825 | << "failed to hoist load with loop-invariant address " | ||||||||||
1826 | "because load is conditionally executed"; | ||||||||||
1827 | }); | ||||||||||
1828 | } | ||||||||||
1829 | |||||||||||
1830 | return GuaranteedToExecute; | ||||||||||
1831 | } | ||||||||||
1832 | |||||||||||
1833 | namespace { | ||||||||||
1834 | class LoopPromoter : public LoadAndStorePromoter { | ||||||||||
1835 | Value *SomePtr; // Designated pointer to store to. | ||||||||||
1836 | const SmallSetVector<Value *, 8> &PointerMustAliases; | ||||||||||
1837 | SmallVectorImpl<BasicBlock *> &LoopExitBlocks; | ||||||||||
1838 | SmallVectorImpl<Instruction *> &LoopInsertPts; | ||||||||||
1839 | SmallVectorImpl<MemoryAccess *> &MSSAInsertPts; | ||||||||||
1840 | PredIteratorCache &PredCache; | ||||||||||
1841 | MemorySSAUpdater *MSSAU; | ||||||||||
1842 | LoopInfo &LI; | ||||||||||
1843 | DebugLoc DL; | ||||||||||
1844 | Align Alignment; | ||||||||||
1845 | bool UnorderedAtomic; | ||||||||||
1846 | AAMDNodes AATags; | ||||||||||
1847 | ICFLoopSafetyInfo &SafetyInfo; | ||||||||||
1848 | bool CanInsertStoresInExitBlocks; | ||||||||||
1849 | |||||||||||
1850 | // We're about to add a use of V in a loop exit block. Insert an LCSSA phi | ||||||||||
1851 | // (if legal) if doing so would add an out-of-loop use to an instruction | ||||||||||
1852 | // defined in-loop. | ||||||||||
1853 | Value *maybeInsertLCSSAPHI(Value *V, BasicBlock *BB) const { | ||||||||||
1854 | if (!LI.wouldBeOutOfLoopUseRequiringLCSSA(V, BB)) | ||||||||||
1855 | return V; | ||||||||||
1856 | |||||||||||
1857 | Instruction *I = cast<Instruction>(V); | ||||||||||
1858 | // We need to create an LCSSA PHI node for the incoming value and | ||||||||||
1859 | // store that. | ||||||||||
1860 | PHINode *PN = PHINode::Create(I->getType(), PredCache.size(BB), | ||||||||||
1861 | I->getName() + ".lcssa", &BB->front()); | ||||||||||
1862 | for (BasicBlock *Pred : PredCache.get(BB)) | ||||||||||
1863 | PN->addIncoming(I, Pred); | ||||||||||
1864 | return PN; | ||||||||||
1865 | } | ||||||||||
1866 | |||||||||||
1867 | public: | ||||||||||
1868 | LoopPromoter(Value *SP, ArrayRef<const Instruction *> Insts, SSAUpdater &S, | ||||||||||
1869 | const SmallSetVector<Value *, 8> &PMA, | ||||||||||
1870 | SmallVectorImpl<BasicBlock *> &LEB, | ||||||||||
1871 | SmallVectorImpl<Instruction *> &LIP, | ||||||||||
1872 | SmallVectorImpl<MemoryAccess *> &MSSAIP, PredIteratorCache &PIC, | ||||||||||
1873 | MemorySSAUpdater *MSSAU, LoopInfo &li, DebugLoc dl, | ||||||||||
1874 | Align Alignment, bool UnorderedAtomic, const AAMDNodes &AATags, | ||||||||||
1875 | ICFLoopSafetyInfo &SafetyInfo, bool CanInsertStoresInExitBlocks) | ||||||||||
1876 | : LoadAndStorePromoter(Insts, S), SomePtr(SP), PointerMustAliases(PMA), | ||||||||||
1877 | LoopExitBlocks(LEB), LoopInsertPts(LIP), MSSAInsertPts(MSSAIP), | ||||||||||
1878 | PredCache(PIC), MSSAU(MSSAU), LI(li), DL(std::move(dl)), | ||||||||||
1879 | Alignment(Alignment), UnorderedAtomic(UnorderedAtomic), AATags(AATags), | ||||||||||
1880 | SafetyInfo(SafetyInfo), | ||||||||||
1881 | CanInsertStoresInExitBlocks(CanInsertStoresInExitBlocks) {} | ||||||||||
1882 | |||||||||||
1883 | bool isInstInList(Instruction *I, | ||||||||||
1884 | const SmallVectorImpl<Instruction *> &) const override { | ||||||||||
1885 | Value *Ptr; | ||||||||||
1886 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) | ||||||||||
1887 | Ptr = LI->getOperand(0); | ||||||||||
1888 | else | ||||||||||
1889 | Ptr = cast<StoreInst>(I)->getPointerOperand(); | ||||||||||
1890 | return PointerMustAliases.count(Ptr); | ||||||||||
1891 | } | ||||||||||
1892 | |||||||||||
1893 | void insertStoresInLoopExitBlocks() { | ||||||||||
1894 | // Insert stores after in the loop exit blocks. Each exit block gets a | ||||||||||
1895 | // store of the live-out values that feed them. Since we've already told | ||||||||||
1896 | // the SSA updater about the defs in the loop and the preheader | ||||||||||
1897 | // definition, it is all set and we can start using it. | ||||||||||
1898 | for (unsigned i = 0, e = LoopExitBlocks.size(); i != e; ++i) { | ||||||||||
1899 | BasicBlock *ExitBlock = LoopExitBlocks[i]; | ||||||||||
1900 | Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock); | ||||||||||
1901 | LiveInValue = maybeInsertLCSSAPHI(LiveInValue, ExitBlock); | ||||||||||
1902 | Value *Ptr = maybeInsertLCSSAPHI(SomePtr, ExitBlock); | ||||||||||
1903 | Instruction *InsertPos = LoopInsertPts[i]; | ||||||||||
1904 | StoreInst *NewSI = new StoreInst(LiveInValue, Ptr, InsertPos); | ||||||||||
1905 | if (UnorderedAtomic) | ||||||||||
1906 | NewSI->setOrdering(AtomicOrdering::Unordered); | ||||||||||
1907 | NewSI->setAlignment(Alignment); | ||||||||||
1908 | NewSI->setDebugLoc(DL); | ||||||||||
1909 | if (AATags) | ||||||||||
1910 | NewSI->setAAMetadata(AATags); | ||||||||||
1911 | |||||||||||
1912 | MemoryAccess *MSSAInsertPoint = MSSAInsertPts[i]; | ||||||||||
1913 | MemoryAccess *NewMemAcc; | ||||||||||
1914 | if (!MSSAInsertPoint) { | ||||||||||
1915 | NewMemAcc = MSSAU->createMemoryAccessInBB( | ||||||||||
1916 | NewSI, nullptr, NewSI->getParent(), MemorySSA::Beginning); | ||||||||||
1917 | } else { | ||||||||||
1918 | NewMemAcc = | ||||||||||
1919 | MSSAU->createMemoryAccessAfter(NewSI, nullptr, MSSAInsertPoint); | ||||||||||
1920 | } | ||||||||||
1921 | MSSAInsertPts[i] = NewMemAcc; | ||||||||||
1922 | MSSAU->insertDef(cast<MemoryDef>(NewMemAcc), true); | ||||||||||
1923 | // FIXME: true for safety, false may still be correct. | ||||||||||
1924 | } | ||||||||||
1925 | } | ||||||||||
1926 | |||||||||||
1927 | void doExtraRewritesBeforeFinalDeletion() override { | ||||||||||
1928 | if (CanInsertStoresInExitBlocks) | ||||||||||
1929 | insertStoresInLoopExitBlocks(); | ||||||||||
1930 | } | ||||||||||
1931 | |||||||||||
1932 | void instructionDeleted(Instruction *I) const override { | ||||||||||
1933 | SafetyInfo.removeInstruction(I); | ||||||||||
1934 | MSSAU->removeMemoryAccess(I); | ||||||||||
1935 | } | ||||||||||
1936 | |||||||||||
1937 | bool shouldDelete(Instruction *I) const override { | ||||||||||
1938 | if (isa<StoreInst>(I)) | ||||||||||
1939 | return CanInsertStoresInExitBlocks; | ||||||||||
1940 | return true; | ||||||||||
1941 | } | ||||||||||
1942 | }; | ||||||||||
1943 | |||||||||||
1944 | bool isNotCapturedBeforeOrInLoop(const Value *V, const Loop *L, | ||||||||||
1945 | DominatorTree *DT) { | ||||||||||
1946 | // We can perform the captured-before check against any instruction in the | ||||||||||
1947 | // loop header, as the loop header is reachable from any instruction inside | ||||||||||
1948 | // the loop. | ||||||||||
1949 | // TODO: ReturnCaptures=true shouldn't be necessary here. | ||||||||||
1950 | return !PointerMayBeCapturedBefore(V, /* ReturnCaptures */ true, | ||||||||||
1951 | /* StoreCaptures */ true, | ||||||||||
1952 | L->getHeader()->getTerminator(), DT); | ||||||||||
1953 | } | ||||||||||
1954 | |||||||||||
1955 | /// Return true if we can prove that a caller cannot inspect the object if an | ||||||||||
1956 | /// unwind occurs inside the loop. | ||||||||||
1957 | bool isNotVisibleOnUnwindInLoop(const Value *Object, const Loop *L, | ||||||||||
1958 | DominatorTree *DT) { | ||||||||||
1959 | bool RequiresNoCaptureBeforeUnwind; | ||||||||||
1960 | if (!isNotVisibleOnUnwind(Object, RequiresNoCaptureBeforeUnwind)) | ||||||||||
1961 | return false; | ||||||||||
1962 | |||||||||||
1963 | return !RequiresNoCaptureBeforeUnwind || | ||||||||||
1964 | isNotCapturedBeforeOrInLoop(Object, L, DT); | ||||||||||
1965 | } | ||||||||||
1966 | |||||||||||
1967 | } // namespace | ||||||||||
1968 | |||||||||||
1969 | /// Try to promote memory values to scalars by sinking stores out of the | ||||||||||
1970 | /// loop and moving loads to before the loop. We do this by looping over | ||||||||||
1971 | /// the stores in the loop, looking for stores to Must pointers which are | ||||||||||
1972 | /// loop invariant. | ||||||||||
1973 | /// | ||||||||||
1974 | bool llvm::promoteLoopAccessesToScalars( | ||||||||||
1975 | const SmallSetVector<Value *, 8> &PointerMustAliases, | ||||||||||
1976 | SmallVectorImpl<BasicBlock *> &ExitBlocks, | ||||||||||
1977 | SmallVectorImpl<Instruction *> &InsertPts, | ||||||||||
1978 | SmallVectorImpl<MemoryAccess *> &MSSAInsertPts, PredIteratorCache &PIC, | ||||||||||
1979 | LoopInfo *LI, DominatorTree *DT, const TargetLibraryInfo *TLI, | ||||||||||
1980 | Loop *CurLoop, MemorySSAUpdater *MSSAU, ICFLoopSafetyInfo *SafetyInfo, | ||||||||||
1981 | OptimizationRemarkEmitter *ORE, bool AllowSpeculation) { | ||||||||||
1982 | // Verify inputs. | ||||||||||
1983 | assert(LI != nullptr && DT != nullptr && CurLoop != nullptr &&(static_cast <bool> (LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && "Unexpected Input to promoteLoopAccessesToScalars" ) ? void (0) : __assert_fail ("LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected Input to promoteLoopAccessesToScalars\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1985, __extension__ __PRETTY_FUNCTION__ )) | ||||||||||
1984 | SafetyInfo != nullptr &&(static_cast <bool> (LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && "Unexpected Input to promoteLoopAccessesToScalars" ) ? void (0) : __assert_fail ("LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected Input to promoteLoopAccessesToScalars\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1985, __extension__ __PRETTY_FUNCTION__ )) | ||||||||||
1985 | "Unexpected Input to promoteLoopAccessesToScalars")(static_cast <bool> (LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && "Unexpected Input to promoteLoopAccessesToScalars" ) ? void (0) : __assert_fail ("LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected Input to promoteLoopAccessesToScalars\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 1985, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
1986 | |||||||||||
1987 | Value *SomePtr = *PointerMustAliases.begin(); | ||||||||||
1988 | BasicBlock *Preheader = CurLoop->getLoopPreheader(); | ||||||||||
1989 | |||||||||||
1990 | // It is not safe to promote a load/store from the loop if the load/store is | ||||||||||
1991 | // conditional. For example, turning: | ||||||||||
1992 | // | ||||||||||
1993 | // for () { if (c) *P += 1; } | ||||||||||
1994 | // | ||||||||||
1995 | // into: | ||||||||||
1996 | // | ||||||||||
1997 | // tmp = *P; for () { if (c) tmp +=1; } *P = tmp; | ||||||||||
1998 | // | ||||||||||
1999 | // is not safe, because *P may only be valid to access if 'c' is true. | ||||||||||
2000 | // | ||||||||||
2001 | // The safety property divides into two parts: | ||||||||||
2002 | // p1) The memory may not be dereferenceable on entry to the loop. In this | ||||||||||
2003 | // case, we can't insert the required load in the preheader. | ||||||||||
2004 | // p2) The memory model does not allow us to insert a store along any dynamic | ||||||||||
2005 | // path which did not originally have one. | ||||||||||
2006 | // | ||||||||||
2007 | // If at least one store is guaranteed to execute, both properties are | ||||||||||
2008 | // satisfied, and promotion is legal. | ||||||||||
2009 | // | ||||||||||
2010 | // This, however, is not a necessary condition. Even if no store/load is | ||||||||||
2011 | // guaranteed to execute, we can still establish these properties. | ||||||||||
2012 | // We can establish (p1) by proving that hoisting the load into the preheader | ||||||||||
2013 | // is safe (i.e. proving dereferenceability on all paths through the loop). We | ||||||||||
2014 | // can use any access within the alias set to prove dereferenceability, | ||||||||||
2015 | // since they're all must alias. | ||||||||||
2016 | // | ||||||||||
2017 | // There are two ways establish (p2): | ||||||||||
2018 | // a) Prove the location is thread-local. In this case the memory model | ||||||||||
2019 | // requirement does not apply, and stores are safe to insert. | ||||||||||
2020 | // b) Prove a store dominates every exit block. In this case, if an exit | ||||||||||
2021 | // blocks is reached, the original dynamic path would have taken us through | ||||||||||
2022 | // the store, so inserting a store into the exit block is safe. Note that this | ||||||||||
2023 | // is different from the store being guaranteed to execute. For instance, | ||||||||||
2024 | // if an exception is thrown on the first iteration of the loop, the original | ||||||||||
2025 | // store is never executed, but the exit blocks are not executed either. | ||||||||||
2026 | |||||||||||
2027 | bool DereferenceableInPH = false; | ||||||||||
2028 | bool SafeToInsertStore = false; | ||||||||||
2029 | bool FoundLoadToPromote = false; | ||||||||||
2030 | |||||||||||
2031 | SmallVector<Instruction *, 64> LoopUses; | ||||||||||
2032 | |||||||||||
2033 | // We start with an alignment of one and try to find instructions that allow | ||||||||||
2034 | // us to prove better alignment. | ||||||||||
2035 | Align Alignment; | ||||||||||
2036 | // Keep track of which types of access we see | ||||||||||
2037 | bool SawUnorderedAtomic = false; | ||||||||||
2038 | bool SawNotAtomic = false; | ||||||||||
2039 | AAMDNodes AATags; | ||||||||||
2040 | |||||||||||
2041 | const DataLayout &MDL = Preheader->getModule()->getDataLayout(); | ||||||||||
2042 | |||||||||||
2043 | bool IsKnownThreadLocalObject = false; | ||||||||||
2044 | if (SafetyInfo->anyBlockMayThrow()) { | ||||||||||
2045 | // If a loop can throw, we have to insert a store along each unwind edge. | ||||||||||
2046 | // That said, we can't actually make the unwind edge explicit. Therefore, | ||||||||||
2047 | // we have to prove that the store is dead along the unwind edge. We do | ||||||||||
2048 | // this by proving that the caller can't have a reference to the object | ||||||||||
2049 | // after return and thus can't possibly load from the object. | ||||||||||
2050 | Value *Object = getUnderlyingObject(SomePtr); | ||||||||||
2051 | if (!isNotVisibleOnUnwindInLoop(Object, CurLoop, DT)) | ||||||||||
2052 | return false; | ||||||||||
2053 | // Subtlety: Alloca's aren't visible to callers, but *are* potentially | ||||||||||
2054 | // visible to other threads if captured and used during their lifetimes. | ||||||||||
2055 | IsKnownThreadLocalObject = !isa<AllocaInst>(Object); | ||||||||||
2056 | } | ||||||||||
2057 | |||||||||||
2058 | // Check that all accesses to pointers in the aliass set use the same type. | ||||||||||
2059 | // We cannot (yet) promote a memory location that is loaded and stored in | ||||||||||
2060 | // different sizes. While we are at it, collect alignment and AA info. | ||||||||||
2061 | Type *AccessTy = nullptr; | ||||||||||
2062 | for (Value *ASIV : PointerMustAliases) { | ||||||||||
2063 | for (Use &U : ASIV->uses()) { | ||||||||||
2064 | // Ignore instructions that are outside the loop. | ||||||||||
2065 | Instruction *UI = dyn_cast<Instruction>(U.getUser()); | ||||||||||
2066 | if (!UI || !CurLoop->contains(UI)) | ||||||||||
2067 | continue; | ||||||||||
2068 | |||||||||||
2069 | // If there is an non-load/store instruction in the loop, we can't promote | ||||||||||
2070 | // it. | ||||||||||
2071 | if (LoadInst *Load = dyn_cast<LoadInst>(UI)) { | ||||||||||
2072 | if (!Load->isUnordered()) | ||||||||||
2073 | return false; | ||||||||||
2074 | |||||||||||
2075 | SawUnorderedAtomic |= Load->isAtomic(); | ||||||||||
2076 | SawNotAtomic |= !Load->isAtomic(); | ||||||||||
2077 | FoundLoadToPromote = true; | ||||||||||
2078 | |||||||||||
2079 | Align InstAlignment = Load->getAlign(); | ||||||||||
2080 | |||||||||||
2081 | // Note that proving a load safe to speculate requires proving | ||||||||||
2082 | // sufficient alignment at the target location. Proving it guaranteed | ||||||||||
2083 | // to execute does as well. Thus we can increase our guaranteed | ||||||||||
2084 | // alignment as well. | ||||||||||
2085 | if (!DereferenceableInPH || (InstAlignment > Alignment)) | ||||||||||
2086 | if (isSafeToExecuteUnconditionally( | ||||||||||
2087 | *Load, DT, TLI, CurLoop, SafetyInfo, ORE, | ||||||||||
2088 | Preheader->getTerminator(), AllowSpeculation)) { | ||||||||||
2089 | DereferenceableInPH = true; | ||||||||||
2090 | Alignment = std::max(Alignment, InstAlignment); | ||||||||||
2091 | } | ||||||||||
2092 | } else if (const StoreInst *Store = dyn_cast<StoreInst>(UI)) { | ||||||||||
2093 | // Stores *of* the pointer are not interesting, only stores *to* the | ||||||||||
2094 | // pointer. | ||||||||||
2095 | if (U.getOperandNo() != StoreInst::getPointerOperandIndex()) | ||||||||||
2096 | continue; | ||||||||||
2097 | if (!Store->isUnordered()) | ||||||||||
2098 | return false; | ||||||||||
2099 | |||||||||||
2100 | SawUnorderedAtomic |= Store->isAtomic(); | ||||||||||
2101 | SawNotAtomic |= !Store->isAtomic(); | ||||||||||
2102 | |||||||||||
2103 | // If the store is guaranteed to execute, both properties are satisfied. | ||||||||||
2104 | // We may want to check if a store is guaranteed to execute even if we | ||||||||||
2105 | // already know that promotion is safe, since it may have higher | ||||||||||
2106 | // alignment than any other guaranteed stores, in which case we can | ||||||||||
2107 | // raise the alignment on the promoted store. | ||||||||||
2108 | Align InstAlignment = Store->getAlign(); | ||||||||||
2109 | |||||||||||
2110 | if (!DereferenceableInPH || !SafeToInsertStore || | ||||||||||
2111 | (InstAlignment > Alignment)) { | ||||||||||
2112 | if (SafetyInfo->isGuaranteedToExecute(*UI, DT, CurLoop)) { | ||||||||||
2113 | DereferenceableInPH = true; | ||||||||||
2114 | SafeToInsertStore = true; | ||||||||||
2115 | Alignment = std::max(Alignment, InstAlignment); | ||||||||||
2116 | } | ||||||||||
2117 | } | ||||||||||
2118 | |||||||||||
2119 | // If a store dominates all exit blocks, it is safe to sink. | ||||||||||
2120 | // As explained above, if an exit block was executed, a dominating | ||||||||||
2121 | // store must have been executed at least once, so we are not | ||||||||||
2122 | // introducing stores on paths that did not have them. | ||||||||||
2123 | // Note that this only looks at explicit exit blocks. If we ever | ||||||||||
2124 | // start sinking stores into unwind edges (see above), this will break. | ||||||||||
2125 | if (!SafeToInsertStore) | ||||||||||
2126 | SafeToInsertStore = llvm::all_of(ExitBlocks, [&](BasicBlock *Exit) { | ||||||||||
2127 | return DT->dominates(Store->getParent(), Exit); | ||||||||||
2128 | }); | ||||||||||
2129 | |||||||||||
2130 | // If the store is not guaranteed to execute, we may still get | ||||||||||
2131 | // deref info through it. | ||||||||||
2132 | if (!DereferenceableInPH) { | ||||||||||
2133 | DereferenceableInPH = isDereferenceableAndAlignedPointer( | ||||||||||
2134 | Store->getPointerOperand(), Store->getValueOperand()->getType(), | ||||||||||
2135 | Store->getAlign(), MDL, Preheader->getTerminator(), DT, TLI); | ||||||||||
2136 | } | ||||||||||
2137 | } else | ||||||||||
2138 | return false; // Not a load or store. | ||||||||||
2139 | |||||||||||
2140 | if (!AccessTy) | ||||||||||
2141 | AccessTy = getLoadStoreType(UI); | ||||||||||
2142 | else if (AccessTy != getLoadStoreType(UI)) | ||||||||||
2143 | return false; | ||||||||||
2144 | |||||||||||
2145 | // Merge the AA tags. | ||||||||||
2146 | if (LoopUses.empty()) { | ||||||||||
2147 | // On the first load/store, just take its AA tags. | ||||||||||
2148 | AATags = UI->getAAMetadata(); | ||||||||||
2149 | } else if (AATags) { | ||||||||||
2150 | AATags = AATags.merge(UI->getAAMetadata()); | ||||||||||
2151 | } | ||||||||||
2152 | |||||||||||
2153 | LoopUses.push_back(UI); | ||||||||||
2154 | } | ||||||||||
2155 | } | ||||||||||
2156 | |||||||||||
2157 | // If we found both an unordered atomic instruction and a non-atomic memory | ||||||||||
2158 | // access, bail. We can't blindly promote non-atomic to atomic since we | ||||||||||
2159 | // might not be able to lower the result. We can't downgrade since that | ||||||||||
2160 | // would violate memory model. Also, align 0 is an error for atomics. | ||||||||||
2161 | if (SawUnorderedAtomic && SawNotAtomic) | ||||||||||
2162 | return false; | ||||||||||
2163 | |||||||||||
2164 | // If we're inserting an atomic load in the preheader, we must be able to | ||||||||||
2165 | // lower it. We're only guaranteed to be able to lower naturally aligned | ||||||||||
2166 | // atomics. | ||||||||||
2167 | if (SawUnorderedAtomic && Alignment < MDL.getTypeStoreSize(AccessTy)) | ||||||||||
2168 | return false; | ||||||||||
2169 | |||||||||||
2170 | // If we couldn't prove we can hoist the load, bail. | ||||||||||
2171 | if (!DereferenceableInPH) | ||||||||||
2172 | return false; | ||||||||||
2173 | |||||||||||
2174 | // We know we can hoist the load, but don't have a guaranteed store. | ||||||||||
2175 | // Check whether the location is thread-local. If it is, then we can insert | ||||||||||
2176 | // stores along paths which originally didn't have them without violating the | ||||||||||
2177 | // memory model. | ||||||||||
2178 | if (!SafeToInsertStore) { | ||||||||||
2179 | if (IsKnownThreadLocalObject) | ||||||||||
2180 | SafeToInsertStore = true; | ||||||||||
2181 | else { | ||||||||||
2182 | Value *Object = getUnderlyingObject(SomePtr); | ||||||||||
2183 | SafeToInsertStore = | ||||||||||
2184 | (isNoAliasCall(Object) || isa<AllocaInst>(Object)) && | ||||||||||
2185 | isNotCapturedBeforeOrInLoop(Object, CurLoop, DT); | ||||||||||
2186 | } | ||||||||||
2187 | } | ||||||||||
2188 | |||||||||||
2189 | // If we've still failed to prove we can sink the store, hoist the load | ||||||||||
2190 | // only, if possible. | ||||||||||
2191 | if (!SafeToInsertStore && !FoundLoadToPromote) | ||||||||||
2192 | // If we cannot hoist the load either, give up. | ||||||||||
2193 | return false; | ||||||||||
2194 | |||||||||||
2195 | // Lets do the promotion! | ||||||||||
2196 | if (SafeToInsertStore) | ||||||||||
2197 | LLVM_DEBUG(dbgs() << "LICM: Promoting load/store of the value: " << *SomePtrdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM: Promoting load/store of the value: " << *SomePtr << '\n'; } } while (false) | ||||||||||
2198 | << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM: Promoting load/store of the value: " << *SomePtr << '\n'; } } while (false); | ||||||||||
2199 | else | ||||||||||
2200 | LLVM_DEBUG(dbgs() << "LICM: Promoting load of the value: " << *SomePtrdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM: Promoting load of the value: " << *SomePtr << '\n'; } } while (false) | ||||||||||
2201 | << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("licm")) { dbgs() << "LICM: Promoting load of the value: " << *SomePtr << '\n'; } } while (false); | ||||||||||
2202 | |||||||||||
2203 | ORE->emit([&]() { | ||||||||||
2204 | return OptimizationRemark(DEBUG_TYPE"licm", "PromoteLoopAccessesToScalar", | ||||||||||
2205 | LoopUses[0]) | ||||||||||
2206 | << "Moving accesses to memory location out of the loop"; | ||||||||||
2207 | }); | ||||||||||
2208 | ++NumPromoted; | ||||||||||
2209 | |||||||||||
2210 | // Look at all the loop uses, and try to merge their locations. | ||||||||||
2211 | std::vector<const DILocation *> LoopUsesLocs; | ||||||||||
2212 | for (auto U : LoopUses) | ||||||||||
2213 | LoopUsesLocs.push_back(U->getDebugLoc().get()); | ||||||||||
2214 | auto DL = DebugLoc(DILocation::getMergedLocations(LoopUsesLocs)); | ||||||||||
2215 | |||||||||||
2216 | // We use the SSAUpdater interface to insert phi nodes as required. | ||||||||||
2217 | SmallVector<PHINode *, 16> NewPHIs; | ||||||||||
2218 | SSAUpdater SSA(&NewPHIs); | ||||||||||
2219 | LoopPromoter Promoter(SomePtr, LoopUses, SSA, PointerMustAliases, ExitBlocks, | ||||||||||
2220 | InsertPts, MSSAInsertPts, PIC, MSSAU, *LI, DL, | ||||||||||
2221 | Alignment, SawUnorderedAtomic, AATags, *SafetyInfo, | ||||||||||
2222 | SafeToInsertStore); | ||||||||||
2223 | |||||||||||
2224 | // Set up the preheader to have a definition of the value. It is the live-out | ||||||||||
2225 | // value from the preheader that uses in the loop will use. | ||||||||||
2226 | LoadInst *PreheaderLoad = new LoadInst( | ||||||||||
2227 | AccessTy, SomePtr, SomePtr->getName() + ".promoted", | ||||||||||
2228 | Preheader->getTerminator()); | ||||||||||
2229 | if (SawUnorderedAtomic) | ||||||||||
2230 | PreheaderLoad->setOrdering(AtomicOrdering::Unordered); | ||||||||||
2231 | PreheaderLoad->setAlignment(Alignment); | ||||||||||
2232 | PreheaderLoad->setDebugLoc(DebugLoc()); | ||||||||||
2233 | if (AATags) | ||||||||||
2234 | PreheaderLoad->setAAMetadata(AATags); | ||||||||||
2235 | SSA.AddAvailableValue(Preheader, PreheaderLoad); | ||||||||||
2236 | |||||||||||
2237 | MemoryAccess *PreheaderLoadMemoryAccess = MSSAU->createMemoryAccessInBB( | ||||||||||
2238 | PreheaderLoad, nullptr, PreheaderLoad->getParent(), MemorySSA::End); | ||||||||||
2239 | MemoryUse *NewMemUse = cast<MemoryUse>(PreheaderLoadMemoryAccess); | ||||||||||
2240 | MSSAU->insertUse(NewMemUse, /*RenameUses=*/true); | ||||||||||
2241 | |||||||||||
2242 | if (VerifyMemorySSA) | ||||||||||
2243 | MSSAU->getMemorySSA()->verifyMemorySSA(); | ||||||||||
2244 | // Rewrite all the loads in the loop and remember all the definitions from | ||||||||||
2245 | // stores in the loop. | ||||||||||
2246 | Promoter.run(LoopUses); | ||||||||||
2247 | |||||||||||
2248 | if (VerifyMemorySSA) | ||||||||||
2249 | MSSAU->getMemorySSA()->verifyMemorySSA(); | ||||||||||
2250 | // If the SSAUpdater didn't use the load in the preheader, just zap it now. | ||||||||||
2251 | if (PreheaderLoad->use_empty()) | ||||||||||
2252 | eraseInstruction(*PreheaderLoad, *SafetyInfo, MSSAU); | ||||||||||
2253 | |||||||||||
2254 | return true; | ||||||||||
2255 | } | ||||||||||
2256 | |||||||||||
2257 | static void foreachMemoryAccess(MemorySSA *MSSA, Loop *L, | ||||||||||
2258 | function_ref<void(Instruction *)> Fn) { | ||||||||||
2259 | for (const BasicBlock *BB : L->blocks()) | ||||||||||
2260 | if (const auto *Accesses = MSSA->getBlockAccesses(BB)) | ||||||||||
2261 | for (const auto &Access : *Accesses) | ||||||||||
2262 | if (const auto *MUD = dyn_cast<MemoryUseOrDef>(&Access)) | ||||||||||
2263 | Fn(MUD->getMemoryInst()); | ||||||||||
2264 | } | ||||||||||
2265 | |||||||||||
2266 | static SmallVector<SmallSetVector<Value *, 8>, 0> | ||||||||||
2267 | collectPromotionCandidates(MemorySSA *MSSA, AliasAnalysis *AA, Loop *L) { | ||||||||||
2268 | AliasSetTracker AST(*AA); | ||||||||||
2269 | |||||||||||
2270 | auto IsPotentiallyPromotable = [L](const Instruction *I) { | ||||||||||
2271 | if (const auto *SI = dyn_cast<StoreInst>(I)) | ||||||||||
2272 | return L->isLoopInvariant(SI->getPointerOperand()); | ||||||||||
2273 | if (const auto *LI = dyn_cast<LoadInst>(I)) | ||||||||||
2274 | return L->isLoopInvariant(LI->getPointerOperand()); | ||||||||||
2275 | return false; | ||||||||||
2276 | }; | ||||||||||
2277 | |||||||||||
2278 | // Populate AST with potentially promotable accesses. | ||||||||||
2279 | SmallPtrSet<Value *, 16> AttemptingPromotion; | ||||||||||
2280 | foreachMemoryAccess(MSSA, L, [&](Instruction *I) { | ||||||||||
2281 | if (IsPotentiallyPromotable(I)) { | ||||||||||
2282 | AttemptingPromotion.insert(I); | ||||||||||
2283 | AST.add(I); | ||||||||||
2284 | } | ||||||||||
2285 | }); | ||||||||||
2286 | |||||||||||
2287 | // We're only interested in must-alias sets that contain a mod. | ||||||||||
2288 | SmallVector<const AliasSet *, 8> Sets; | ||||||||||
2289 | for (AliasSet &AS : AST) | ||||||||||
2290 | if (!AS.isForwardingAliasSet() && AS.isMod() && AS.isMustAlias()) | ||||||||||
2291 | Sets.push_back(&AS); | ||||||||||
2292 | |||||||||||
2293 | if (Sets.empty()) | ||||||||||
2294 | return {}; // Nothing to promote... | ||||||||||
2295 | |||||||||||
2296 | // Discard any sets for which there is an aliasing non-promotable access. | ||||||||||
2297 | foreachMemoryAccess(MSSA, L, [&](Instruction *I) { | ||||||||||
2298 | if (AttemptingPromotion.contains(I)) | ||||||||||
2299 | return; | ||||||||||
2300 | |||||||||||
2301 | llvm::erase_if(Sets, [&](const AliasSet *AS) { | ||||||||||
2302 | return AS->aliasesUnknownInst(I, *AA); | ||||||||||
2303 | }); | ||||||||||
2304 | }); | ||||||||||
2305 | |||||||||||
2306 | SmallVector<SmallSetVector<Value *, 8>, 0> Result; | ||||||||||
2307 | for (const AliasSet *Set : Sets) { | ||||||||||
2308 | SmallSetVector<Value *, 8> PointerMustAliases; | ||||||||||
2309 | for (const auto &ASI : *Set) | ||||||||||
2310 | PointerMustAliases.insert(ASI.getValue()); | ||||||||||
2311 | Result.push_back(std::move(PointerMustAliases)); | ||||||||||
2312 | } | ||||||||||
2313 | |||||||||||
2314 | return Result; | ||||||||||
2315 | } | ||||||||||
2316 | |||||||||||
2317 | static bool pointerInvalidatedByLoop(MemoryLocation MemLoc, | ||||||||||
2318 | AliasSetTracker *CurAST, Loop *CurLoop, | ||||||||||
2319 | AAResults *AA) { | ||||||||||
2320 | return CurAST->getAliasSetFor(MemLoc).isMod(); | ||||||||||
2321 | } | ||||||||||
2322 | |||||||||||
2323 | bool pointerInvalidatedByLoopWithMSSA(MemorySSA *MSSA, MemoryUse *MU, | ||||||||||
2324 | Loop *CurLoop, Instruction &I, | ||||||||||
2325 | SinkAndHoistLICMFlags &Flags) { | ||||||||||
2326 | // For hoisting, use the walker to determine safety | ||||||||||
2327 | if (!Flags.getIsSink()) { | ||||||||||
2328 | MemoryAccess *Source; | ||||||||||
2329 | // See declaration of SetLicmMssaOptCap for usage details. | ||||||||||
2330 | if (Flags.tooManyClobberingCalls()) | ||||||||||
2331 | Source = MU->getDefiningAccess(); | ||||||||||
2332 | else { | ||||||||||
2333 | Source = MSSA->getSkipSelfWalker()->getClobberingMemoryAccess(MU); | ||||||||||
2334 | Flags.incrementClobberingCalls(); | ||||||||||
2335 | } | ||||||||||
2336 | return !MSSA->isLiveOnEntryDef(Source) && | ||||||||||
2337 | CurLoop->contains(Source->getBlock()); | ||||||||||
2338 | } | ||||||||||
2339 | |||||||||||
2340 | // For sinking, we'd need to check all Defs below this use. The getClobbering | ||||||||||
2341 | // call will look on the backedge of the loop, but will check aliasing with | ||||||||||
2342 | // the instructions on the previous iteration. | ||||||||||
2343 | // For example: | ||||||||||
2344 | // for (i ... ) | ||||||||||
2345 | // load a[i] ( Use (LoE) | ||||||||||
2346 | // store a[i] ( 1 = Def (2), with 2 = Phi for the loop. | ||||||||||
2347 | // i++; | ||||||||||
2348 | // The load sees no clobbering inside the loop, as the backedge alias check | ||||||||||
2349 | // does phi translation, and will check aliasing against store a[i-1]. | ||||||||||
2350 | // However sinking the load outside the loop, below the store is incorrect. | ||||||||||
2351 | |||||||||||
2352 | // For now, only sink if there are no Defs in the loop, and the existing ones | ||||||||||
2353 | // precede the use and are in the same block. | ||||||||||
2354 | // FIXME: Increase precision: Safe to sink if Use post dominates the Def; | ||||||||||
2355 | // needs PostDominatorTreeAnalysis. | ||||||||||
2356 | // FIXME: More precise: no Defs that alias this Use. | ||||||||||
2357 | if (Flags.tooManyMemoryAccesses()) | ||||||||||
2358 | return true; | ||||||||||
2359 | for (auto *BB : CurLoop->getBlocks()) | ||||||||||
2360 | if (pointerInvalidatedByBlockWithMSSA(*BB, *MSSA, *MU)) | ||||||||||
2361 | return true; | ||||||||||
2362 | // When sinking, the source block may not be part of the loop so check it. | ||||||||||
2363 | if (!CurLoop->contains(&I)) | ||||||||||
2364 | return pointerInvalidatedByBlockWithMSSA(*I.getParent(), *MSSA, *MU); | ||||||||||
2365 | |||||||||||
2366 | return false; | ||||||||||
2367 | } | ||||||||||
2368 | |||||||||||
2369 | bool pointerInvalidatedByBlockWithMSSA(BasicBlock &BB, MemorySSA &MSSA, | ||||||||||
2370 | MemoryUse &MU) { | ||||||||||
2371 | if (const auto *Accesses = MSSA.getBlockDefs(&BB)) | ||||||||||
2372 | for (const auto &MA : *Accesses) | ||||||||||
2373 | if (const auto *MD = dyn_cast<MemoryDef>(&MA)) | ||||||||||
2374 | if (MU.getBlock() != MD->getBlock() || !MSSA.locallyDominates(MD, &MU)) | ||||||||||
2375 | return true; | ||||||||||
2376 | return false; | ||||||||||
2377 | } | ||||||||||
2378 | |||||||||||
2379 | /// Little predicate that returns true if the specified basic block is in | ||||||||||
2380 | /// a subloop of the current one, not the current one itself. | ||||||||||
2381 | /// | ||||||||||
2382 | static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI) { | ||||||||||
2383 | assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop")(static_cast <bool> (CurLoop->contains(BB) && "Only valid if BB is IN the loop") ? void (0) : __assert_fail ("CurLoop->contains(BB) && \"Only valid if BB is IN the loop\"" , "llvm/lib/Transforms/Scalar/LICM.cpp", 2383, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||
2384 | return LI->getLoopFor(BB) != CurLoop; | ||||||||||
2385 | } |
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/Sequence.h" |
23 | #include "llvm/ADT/StringMap.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/DerivedTypes.h" |
29 | #include "llvm/IR/Function.h" |
30 | #include "llvm/IR/Instruction.h" |
31 | #include "llvm/IR/LLVMContext.h" |
32 | #include "llvm/IR/OperandTraits.h" |
33 | #include "llvm/IR/User.h" |
34 | #include <algorithm> |
35 | #include <cassert> |
36 | #include <cstddef> |
37 | #include <cstdint> |
38 | #include <iterator> |
39 | #include <string> |
40 | #include <vector> |
41 | |
42 | namespace llvm { |
43 | |
44 | class StringRef; |
45 | class Type; |
46 | class Value; |
47 | |
48 | namespace Intrinsic { |
49 | typedef unsigned ID; |
50 | } |
51 | |
52 | //===----------------------------------------------------------------------===// |
53 | // UnaryInstruction Class |
54 | //===----------------------------------------------------------------------===// |
55 | |
56 | class UnaryInstruction : public Instruction { |
57 | protected: |
58 | UnaryInstruction(Type *Ty, unsigned iType, Value *V, |
59 | Instruction *IB = nullptr) |
60 | : Instruction(Ty, iType, &Op<0>(), 1, IB) { |
61 | Op<0>() = V; |
62 | } |
63 | UnaryInstruction(Type *Ty, unsigned iType, Value *V, BasicBlock *IAE) |
64 | : Instruction(Ty, iType, &Op<0>(), 1, IAE) { |
65 | Op<0>() = V; |
66 | } |
67 | |
68 | public: |
69 | // allocate space for exactly one operand |
70 | void *operator new(size_t S) { return User::operator new(S, 1); } |
71 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
72 | |
73 | /// Transparently provide more efficient getOperand methods. |
74 | 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; |
75 | |
76 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
77 | static bool classof(const Instruction *I) { |
78 | return I->isUnaryOp() || |
79 | I->getOpcode() == Instruction::Alloca || |
80 | I->getOpcode() == Instruction::Load || |
81 | I->getOpcode() == Instruction::VAArg || |
82 | I->getOpcode() == Instruction::ExtractValue || |
83 | (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd); |
84 | } |
85 | static bool classof(const Value *V) { |
86 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
87 | } |
88 | }; |
89 | |
90 | template <> |
91 | struct OperandTraits<UnaryInstruction> : |
92 | public FixedNumOperandTraits<UnaryInstruction, 1> { |
93 | }; |
94 | |
95 | 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 { (static_cast <bool> (i_nocapture < OperandTraits<UnaryInstruction >::operands(this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<UnaryInstruction>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/InstrTypes.h", 95, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<UnaryInstruction>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<UnaryInstruction>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/InstrTypes.h", 95, __extension__ __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 ); } |
96 | |
97 | //===----------------------------------------------------------------------===// |
98 | // UnaryOperator Class |
99 | //===----------------------------------------------------------------------===// |
100 | |
101 | class UnaryOperator : public UnaryInstruction { |
102 | void AssertOK(); |
103 | |
104 | protected: |
105 | UnaryOperator(UnaryOps iType, Value *S, Type *Ty, |
106 | const Twine &Name, Instruction *InsertBefore); |
107 | UnaryOperator(UnaryOps iType, Value *S, Type *Ty, |
108 | const Twine &Name, BasicBlock *InsertAtEnd); |
109 | |
110 | // Note: Instruction needs to be a friend here to call cloneImpl. |
111 | friend class Instruction; |
112 | |
113 | UnaryOperator *cloneImpl() const; |
114 | |
115 | public: |
116 | |
117 | /// Construct a unary instruction, given the opcode and an operand. |
118 | /// Optionally (if InstBefore is specified) insert the instruction |
119 | /// into a BasicBlock right before the specified instruction. The specified |
120 | /// Instruction is allowed to be a dereferenced end iterator. |
121 | /// |
122 | static UnaryOperator *Create(UnaryOps Op, Value *S, |
123 | const Twine &Name = Twine(), |
124 | Instruction *InsertBefore = nullptr); |
125 | |
126 | /// Construct a unary instruction, given the opcode and an operand. |
127 | /// Also automatically insert this instruction to the end of the |
128 | /// BasicBlock specified. |
129 | /// |
130 | static UnaryOperator *Create(UnaryOps Op, Value *S, |
131 | const Twine &Name, |
132 | BasicBlock *InsertAtEnd); |
133 | |
134 | /// These methods just forward to Create, and are useful when you |
135 | /// statically know what type of instruction you're going to create. These |
136 | /// helpers just save some typing. |
137 | #define HANDLE_UNARY_INST(N, OPC, CLASS) \ |
138 | static UnaryOperator *Create##OPC(Value *V, const Twine &Name = "") {\ |
139 | return Create(Instruction::OPC, V, Name);\ |
140 | } |
141 | #include "llvm/IR/Instruction.def" |
142 | #define HANDLE_UNARY_INST(N, OPC, CLASS) \ |
143 | static UnaryOperator *Create##OPC(Value *V, const Twine &Name, \ |
144 | BasicBlock *BB) {\ |
145 | return Create(Instruction::OPC, V, Name, BB);\ |
146 | } |
147 | #include "llvm/IR/Instruction.def" |
148 | #define HANDLE_UNARY_INST(N, OPC, CLASS) \ |
149 | static UnaryOperator *Create##OPC(Value *V, const Twine &Name, \ |
150 | Instruction *I) {\ |
151 | return Create(Instruction::OPC, V, Name, I);\ |
152 | } |
153 | #include "llvm/IR/Instruction.def" |
154 | |
155 | static UnaryOperator * |
156 | CreateWithCopiedFlags(UnaryOps Opc, Value *V, Instruction *CopyO, |
157 | const Twine &Name = "", |
158 | Instruction *InsertBefore = nullptr) { |
159 | UnaryOperator *UO = Create(Opc, V, Name, InsertBefore); |
160 | UO->copyIRFlags(CopyO); |
161 | return UO; |
162 | } |
163 | |
164 | static UnaryOperator *CreateFNegFMF(Value *Op, Instruction *FMFSource, |
165 | const Twine &Name = "", |
166 | Instruction *InsertBefore = nullptr) { |
167 | return CreateWithCopiedFlags(Instruction::FNeg, Op, FMFSource, Name, |
168 | InsertBefore); |
169 | } |
170 | |
171 | UnaryOps getOpcode() const { |
172 | return static_cast<UnaryOps>(Instruction::getOpcode()); |
173 | } |
174 | |
175 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
176 | static bool classof(const Instruction *I) { |
177 | return I->isUnaryOp(); |
178 | } |
179 | static bool classof(const Value *V) { |
180 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
181 | } |
182 | }; |
183 | |
184 | //===----------------------------------------------------------------------===// |
185 | // BinaryOperator Class |
186 | //===----------------------------------------------------------------------===// |
187 | |
188 | class BinaryOperator : public Instruction { |
189 | void AssertOK(); |
190 | |
191 | protected: |
192 | BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty, |
193 | const Twine &Name, Instruction *InsertBefore); |
194 | BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty, |
195 | const Twine &Name, BasicBlock *InsertAtEnd); |
196 | |
197 | // Note: Instruction needs to be a friend here to call cloneImpl. |
198 | friend class Instruction; |
199 | |
200 | BinaryOperator *cloneImpl() const; |
201 | |
202 | public: |
203 | // allocate space for exactly two operands |
204 | void *operator new(size_t S) { return User::operator new(S, 2); } |
205 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
206 | |
207 | /// Transparently provide more efficient getOperand methods. |
208 | 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; |
209 | |
210 | /// Construct a binary instruction, given the opcode and the two |
211 | /// operands. Optionally (if InstBefore is specified) insert the instruction |
212 | /// into a BasicBlock right before the specified instruction. The specified |
213 | /// Instruction is allowed to be a dereferenced end iterator. |
214 | /// |
215 | static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2, |
216 | const Twine &Name = Twine(), |
217 | Instruction *InsertBefore = nullptr); |
218 | |
219 | /// Construct a binary instruction, given the opcode and the two |
220 | /// operands. Also automatically insert this instruction to the end of the |
221 | /// BasicBlock specified. |
222 | /// |
223 | static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2, |
224 | const Twine &Name, BasicBlock *InsertAtEnd); |
225 | |
226 | /// These methods just forward to Create, and are useful when you |
227 | /// statically know what type of instruction you're going to create. These |
228 | /// helpers just save some typing. |
229 | #define HANDLE_BINARY_INST(N, OPC, CLASS) \ |
230 | static BinaryOperator *Create##OPC(Value *V1, Value *V2, \ |
231 | const Twine &Name = "") {\ |
232 | return Create(Instruction::OPC, V1, V2, Name);\ |
233 | } |
234 | #include "llvm/IR/Instruction.def" |
235 | #define HANDLE_BINARY_INST(N, OPC, CLASS) \ |
236 | static BinaryOperator *Create##OPC(Value *V1, Value *V2, \ |
237 | const Twine &Name, BasicBlock *BB) {\ |
238 | return Create(Instruction::OPC, V1, V2, Name, BB);\ |
239 | } |
240 | #include "llvm/IR/Instruction.def" |
241 | #define HANDLE_BINARY_INST(N, OPC, CLASS) \ |
242 | static BinaryOperator *Create##OPC(Value *V1, Value *V2, \ |
243 | const Twine &Name, Instruction *I) {\ |
244 | return Create(Instruction::OPC, V1, V2, Name, I);\ |
245 | } |
246 | #include "llvm/IR/Instruction.def" |
247 | |
248 | static BinaryOperator * |
249 | CreateWithCopiedFlags(BinaryOps Opc, Value *V1, Value *V2, Instruction *CopyO, |
250 | const Twine &Name = "", |
251 | Instruction *InsertBefore = nullptr) { |
252 | BinaryOperator *BO = Create(Opc, V1, V2, Name, InsertBefore); |
253 | BO->copyIRFlags(CopyO); |
254 | return BO; |
255 | } |
256 | |
257 | static BinaryOperator *CreateFAddFMF(Value *V1, Value *V2, |
258 | Instruction *FMFSource, |
259 | const Twine &Name = "") { |
260 | return CreateWithCopiedFlags(Instruction::FAdd, V1, V2, FMFSource, Name); |
261 | } |
262 | static BinaryOperator *CreateFSubFMF(Value *V1, Value *V2, |
263 | Instruction *FMFSource, |
264 | const Twine &Name = "") { |
265 | return CreateWithCopiedFlags(Instruction::FSub, V1, V2, FMFSource, Name); |
266 | } |
267 | static BinaryOperator *CreateFMulFMF(Value *V1, Value *V2, |
268 | Instruction *FMFSource, |
269 | const Twine &Name = "") { |
270 | return CreateWithCopiedFlags(Instruction::FMul, V1, V2, FMFSource, Name); |
271 | } |
272 | static BinaryOperator *CreateFDivFMF(Value *V1, Value *V2, |
273 | Instruction *FMFSource, |
274 | const Twine &Name = "") { |
275 | return CreateWithCopiedFlags(Instruction::FDiv, V1, V2, FMFSource, Name); |
276 | } |
277 | static BinaryOperator *CreateFRemFMF(Value *V1, Value *V2, |
278 | Instruction *FMFSource, |
279 | const Twine &Name = "") { |
280 | return CreateWithCopiedFlags(Instruction::FRem, V1, V2, FMFSource, Name); |
281 | } |
282 | |
283 | static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2, |
284 | const Twine &Name = "") { |
285 | BinaryOperator *BO = Create(Opc, V1, V2, Name); |
286 | BO->setHasNoSignedWrap(true); |
287 | return BO; |
288 | } |
289 | static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2, |
290 | const Twine &Name, BasicBlock *BB) { |
291 | BinaryOperator *BO = Create(Opc, V1, V2, Name, BB); |
292 | BO->setHasNoSignedWrap(true); |
293 | return BO; |
294 | } |
295 | static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2, |
296 | const Twine &Name, Instruction *I) { |
297 | BinaryOperator *BO = Create(Opc, V1, V2, Name, I); |
298 | BO->setHasNoSignedWrap(true); |
299 | return BO; |
300 | } |
301 | |
302 | static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2, |
303 | const Twine &Name = "") { |
304 | BinaryOperator *BO = Create(Opc, V1, V2, Name); |
305 | BO->setHasNoUnsignedWrap(true); |
306 | return BO; |
307 | } |
308 | static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2, |
309 | const Twine &Name, BasicBlock *BB) { |
310 | BinaryOperator *BO = Create(Opc, V1, V2, Name, BB); |
311 | BO->setHasNoUnsignedWrap(true); |
312 | return BO; |
313 | } |
314 | static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2, |
315 | const Twine &Name, Instruction *I) { |
316 | BinaryOperator *BO = Create(Opc, V1, V2, Name, I); |
317 | BO->setHasNoUnsignedWrap(true); |
318 | return BO; |
319 | } |
320 | |
321 | static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2, |
322 | const Twine &Name = "") { |
323 | BinaryOperator *BO = Create(Opc, V1, V2, Name); |
324 | BO->setIsExact(true); |
325 | return BO; |
326 | } |
327 | static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2, |
328 | const Twine &Name, BasicBlock *BB) { |
329 | BinaryOperator *BO = Create(Opc, V1, V2, Name, BB); |
330 | BO->setIsExact(true); |
331 | return BO; |
332 | } |
333 | static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2, |
334 | const Twine &Name, Instruction *I) { |
335 | BinaryOperator *BO = Create(Opc, V1, V2, Name, I); |
336 | BO->setIsExact(true); |
337 | return BO; |
338 | } |
339 | |
340 | #define DEFINE_HELPERS(OPC, NUWNSWEXACT) \ |
341 | static BinaryOperator *Create##NUWNSWEXACT##OPC(Value *V1, Value *V2, \ |
342 | const Twine &Name = "") { \ |
343 | return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name); \ |
344 | } \ |
345 | static BinaryOperator *Create##NUWNSWEXACT##OPC( \ |
346 | Value *V1, Value *V2, const Twine &Name, BasicBlock *BB) { \ |
347 | return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name, BB); \ |
348 | } \ |
349 | static BinaryOperator *Create##NUWNSWEXACT##OPC( \ |
350 | Value *V1, Value *V2, const Twine &Name, Instruction *I) { \ |
351 | return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name, I); \ |
352 | } |
353 | |
354 | DEFINE_HELPERS(Add, NSW) // CreateNSWAdd |
355 | DEFINE_HELPERS(Add, NUW) // CreateNUWAdd |
356 | DEFINE_HELPERS(Sub, NSW) // CreateNSWSub |
357 | DEFINE_HELPERS(Sub, NUW) // CreateNUWSub |
358 | DEFINE_HELPERS(Mul, NSW) // CreateNSWMul |
359 | DEFINE_HELPERS(Mul, NUW) // CreateNUWMul |
360 | DEFINE_HELPERS(Shl, NSW) // CreateNSWShl |
361 | DEFINE_HELPERS(Shl, NUW) // CreateNUWShl |
362 | |
363 | DEFINE_HELPERS(SDiv, Exact) // CreateExactSDiv |
364 | DEFINE_HELPERS(UDiv, Exact) // CreateExactUDiv |
365 | DEFINE_HELPERS(AShr, Exact) // CreateExactAShr |
366 | DEFINE_HELPERS(LShr, Exact) // CreateExactLShr |
367 | |
368 | #undef DEFINE_HELPERS |
369 | |
370 | /// Helper functions to construct and inspect unary operations (NEG and NOT) |
371 | /// via binary operators SUB and XOR: |
372 | /// |
373 | /// Create the NEG and NOT instructions out of SUB and XOR instructions. |
374 | /// |
375 | static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "", |
376 | Instruction *InsertBefore = nullptr); |
377 | static BinaryOperator *CreateNeg(Value *Op, const Twine &Name, |
378 | BasicBlock *InsertAtEnd); |
379 | static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "", |
380 | Instruction *InsertBefore = nullptr); |
381 | static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name, |
382 | BasicBlock *InsertAtEnd); |
383 | static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "", |
384 | Instruction *InsertBefore = nullptr); |
385 | static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name, |
386 | BasicBlock *InsertAtEnd); |
387 | static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "", |
388 | Instruction *InsertBefore = nullptr); |
389 | static BinaryOperator *CreateNot(Value *Op, const Twine &Name, |
390 | BasicBlock *InsertAtEnd); |
391 | |
392 | BinaryOps getOpcode() const { |
393 | return static_cast<BinaryOps>(Instruction::getOpcode()); |
394 | } |
395 | |
396 | /// Exchange the two operands to this instruction. |
397 | /// This instruction is safe to use on any binary instruction and |
398 | /// does not modify the semantics of the instruction. If the instruction |
399 | /// cannot be reversed (ie, it's a Div), then return true. |
400 | /// |
401 | bool swapOperands(); |
402 | |
403 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
404 | static bool classof(const Instruction *I) { |
405 | return I->isBinaryOp(); |
406 | } |
407 | static bool classof(const Value *V) { |
408 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
409 | } |
410 | }; |
411 | |
412 | template <> |
413 | struct OperandTraits<BinaryOperator> : |
414 | public FixedNumOperandTraits<BinaryOperator, 2> { |
415 | }; |
416 | |
417 | 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 { (static_cast <bool > (i_nocapture < OperandTraits<BinaryOperator>::operands (this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<BinaryOperator>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/InstrTypes.h", 417, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<BinaryOperator>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<BinaryOperator>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/InstrTypes.h", 417, __extension__ __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 ); } |
418 | |
419 | //===----------------------------------------------------------------------===// |
420 | // CastInst Class |
421 | //===----------------------------------------------------------------------===// |
422 | |
423 | /// This is the base class for all instructions that perform data |
424 | /// casts. It is simply provided so that instruction category testing |
425 | /// can be performed with code like: |
426 | /// |
427 | /// if (isa<CastInst>(Instr)) { ... } |
428 | /// Base class of casting instructions. |
429 | class CastInst : public UnaryInstruction { |
430 | protected: |
431 | /// Constructor with insert-before-instruction semantics for subclasses |
432 | CastInst(Type *Ty, unsigned iType, Value *S, |
433 | const Twine &NameStr = "", Instruction *InsertBefore = nullptr) |
434 | : UnaryInstruction(Ty, iType, S, InsertBefore) { |
435 | setName(NameStr); |
436 | } |
437 | /// Constructor with insert-at-end-of-block semantics for subclasses |
438 | CastInst(Type *Ty, unsigned iType, Value *S, |
439 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
440 | : UnaryInstruction(Ty, iType, S, InsertAtEnd) { |
441 | setName(NameStr); |
442 | } |
443 | |
444 | public: |
445 | /// Provides a way to construct any of the CastInst subclasses using an |
446 | /// opcode instead of the subclass's constructor. The opcode must be in the |
447 | /// CastOps category (Instruction::isCast(opcode) returns true). This |
448 | /// constructor has insert-before-instruction semantics to automatically |
449 | /// insert the new CastInst before InsertBefore (if it is non-null). |
450 | /// Construct any of the CastInst subclasses |
451 | static CastInst *Create( |
452 | Instruction::CastOps, ///< The opcode of the cast instruction |
453 | Value *S, ///< The value to be casted (operand 0) |
454 | Type *Ty, ///< The type to which cast should be made |
455 | const Twine &Name = "", ///< Name for the instruction |
456 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
457 | ); |
458 | /// Provides a way to construct any of the CastInst subclasses using an |
459 | /// opcode instead of the subclass's constructor. The opcode must be in the |
460 | /// CastOps category. This constructor has insert-at-end-of-block semantics |
461 | /// to automatically insert the new CastInst at the end of InsertAtEnd (if |
462 | /// its non-null). |
463 | /// Construct any of the CastInst subclasses |
464 | static CastInst *Create( |
465 | Instruction::CastOps, ///< The opcode for the cast instruction |
466 | Value *S, ///< The value to be casted (operand 0) |
467 | Type *Ty, ///< The type to which operand is casted |
468 | const Twine &Name, ///< The name for the instruction |
469 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
470 | ); |
471 | |
472 | /// Create a ZExt or BitCast cast instruction |
473 | static CastInst *CreateZExtOrBitCast( |
474 | Value *S, ///< The value to be casted (operand 0) |
475 | Type *Ty, ///< The type to which cast should be made |
476 | const Twine &Name = "", ///< Name for the instruction |
477 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
478 | ); |
479 | |
480 | /// Create a ZExt or BitCast cast instruction |
481 | static CastInst *CreateZExtOrBitCast( |
482 | Value *S, ///< The value to be casted (operand 0) |
483 | Type *Ty, ///< The type to which operand is casted |
484 | const Twine &Name, ///< The name for the instruction |
485 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
486 | ); |
487 | |
488 | /// Create a SExt or BitCast cast instruction |
489 | static CastInst *CreateSExtOrBitCast( |
490 | Value *S, ///< The value to be casted (operand 0) |
491 | Type *Ty, ///< The type to which cast should be made |
492 | const Twine &Name = "", ///< Name for the instruction |
493 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
494 | ); |
495 | |
496 | /// Create a SExt or BitCast cast instruction |
497 | static CastInst *CreateSExtOrBitCast( |
498 | Value *S, ///< The value to be casted (operand 0) |
499 | Type *Ty, ///< The type to which operand is casted |
500 | const Twine &Name, ///< The name for the instruction |
501 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
502 | ); |
503 | |
504 | /// Create a BitCast AddrSpaceCast, or a PtrToInt cast instruction. |
505 | static CastInst *CreatePointerCast( |
506 | Value *S, ///< The pointer value to be casted (operand 0) |
507 | Type *Ty, ///< The type to which operand is casted |
508 | const Twine &Name, ///< The name for the instruction |
509 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
510 | ); |
511 | |
512 | /// Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction. |
513 | static CastInst *CreatePointerCast( |
514 | Value *S, ///< The pointer value to be casted (operand 0) |
515 | Type *Ty, ///< The type to which cast should be made |
516 | const Twine &Name = "", ///< Name for the instruction |
517 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
518 | ); |
519 | |
520 | /// Create a BitCast or an AddrSpaceCast cast instruction. |
521 | static CastInst *CreatePointerBitCastOrAddrSpaceCast( |
522 | Value *S, ///< The pointer value to be casted (operand 0) |
523 | Type *Ty, ///< The type to which operand is casted |
524 | const Twine &Name, ///< The name for the instruction |
525 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
526 | ); |
527 | |
528 | /// Create a BitCast or an AddrSpaceCast cast instruction. |
529 | static CastInst *CreatePointerBitCastOrAddrSpaceCast( |
530 | Value *S, ///< The pointer value to be casted (operand 0) |
531 | Type *Ty, ///< The type to which cast should be made |
532 | const Twine &Name = "", ///< Name for the instruction |
533 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
534 | ); |
535 | |
536 | /// Create a BitCast, a PtrToInt, or an IntToPTr cast instruction. |
537 | /// |
538 | /// If the value is a pointer type and the destination an integer type, |
539 | /// creates a PtrToInt cast. If the value is an integer type and the |
540 | /// destination a pointer type, creates an IntToPtr cast. Otherwise, creates |
541 | /// a bitcast. |
542 | static CastInst *CreateBitOrPointerCast( |
543 | Value *S, ///< The pointer value to be casted (operand 0) |
544 | Type *Ty, ///< The type to which cast should be made |
545 | const Twine &Name = "", ///< Name for the instruction |
546 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
547 | ); |
548 | |
549 | /// Create a ZExt, BitCast, or Trunc for int -> int casts. |
550 | static CastInst *CreateIntegerCast( |
551 | Value *S, ///< The pointer value to be casted (operand 0) |
552 | Type *Ty, ///< The type to which cast should be made |
553 | bool isSigned, ///< Whether to regard S as signed or not |
554 | const Twine &Name = "", ///< Name for the instruction |
555 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
556 | ); |
557 | |
558 | /// Create a ZExt, BitCast, or Trunc for int -> int casts. |
559 | static CastInst *CreateIntegerCast( |
560 | Value *S, ///< The integer value to be casted (operand 0) |
561 | Type *Ty, ///< The integer type to which operand is casted |
562 | bool isSigned, ///< Whether to regard S as signed or not |
563 | const Twine &Name, ///< The name for the instruction |
564 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
565 | ); |
566 | |
567 | /// Create an FPExt, BitCast, or FPTrunc for fp -> fp casts |
568 | static CastInst *CreateFPCast( |
569 | Value *S, ///< The floating point value to be casted |
570 | Type *Ty, ///< The floating point type to cast to |
571 | const Twine &Name = "", ///< Name for the instruction |
572 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
573 | ); |
574 | |
575 | /// Create an FPExt, BitCast, or FPTrunc for fp -> fp casts |
576 | static CastInst *CreateFPCast( |
577 | Value *S, ///< The floating point value to be casted |
578 | Type *Ty, ///< The floating point type to cast to |
579 | const Twine &Name, ///< The name for the instruction |
580 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
581 | ); |
582 | |
583 | /// Create a Trunc or BitCast cast instruction |
584 | static CastInst *CreateTruncOrBitCast( |
585 | Value *S, ///< The value to be casted (operand 0) |
586 | Type *Ty, ///< The type to which cast should be made |
587 | const Twine &Name = "", ///< Name for the instruction |
588 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
589 | ); |
590 | |
591 | /// Create a Trunc or BitCast cast instruction |
592 | static CastInst *CreateTruncOrBitCast( |
593 | Value *S, ///< The value to be casted (operand 0) |
594 | Type *Ty, ///< The type to which operand is casted |
595 | const Twine &Name, ///< The name for the instruction |
596 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
597 | ); |
598 | |
599 | /// Check whether a bitcast between these types is valid |
600 | static bool isBitCastable( |
601 | Type *SrcTy, ///< The Type from which the value should be cast. |
602 | Type *DestTy ///< The Type to which the value should be cast. |
603 | ); |
604 | |
605 | /// Check whether a bitcast, inttoptr, or ptrtoint cast between these |
606 | /// types is valid and a no-op. |
607 | /// |
608 | /// This ensures that any pointer<->integer cast has enough bits in the |
609 | /// integer and any other cast is a bitcast. |
610 | static bool isBitOrNoopPointerCastable( |
611 | Type *SrcTy, ///< The Type from which the value should be cast. |
612 | Type *DestTy, ///< The Type to which the value should be cast. |
613 | const DataLayout &DL); |
614 | |
615 | /// Returns the opcode necessary to cast Val into Ty using usual casting |
616 | /// rules. |
617 | /// Infer the opcode for cast operand and type |
618 | static Instruction::CastOps getCastOpcode( |
619 | const Value *Val, ///< The value to cast |
620 | bool SrcIsSigned, ///< Whether to treat the source as signed |
621 | Type *Ty, ///< The Type to which the value should be casted |
622 | bool DstIsSigned ///< Whether to treate the dest. as signed |
623 | ); |
624 | |
625 | /// There are several places where we need to know if a cast instruction |
626 | /// only deals with integer source and destination types. To simplify that |
627 | /// logic, this method is provided. |
628 | /// @returns true iff the cast has only integral typed operand and dest type. |
629 | /// Determine if this is an integer-only cast. |
630 | bool isIntegerCast() const; |
631 | |
632 | /// A lossless cast is one that does not alter the basic value. It implies |
633 | /// a no-op cast but is more stringent, preventing things like int->float, |
634 | /// long->double, or int->ptr. |
635 | /// @returns true iff the cast is lossless. |
636 | /// Determine if this is a lossless cast. |
637 | bool isLosslessCast() const; |
638 | |
639 | /// A no-op cast is one that can be effected without changing any bits. |
640 | /// It implies that the source and destination types are the same size. The |
641 | /// DataLayout argument is to determine the pointer size when examining casts |
642 | /// involving Integer and Pointer types. They are no-op casts if the integer |
643 | /// is the same size as the pointer. However, pointer size varies with |
644 | /// platform. Note that a precondition of this method is that the cast is |
645 | /// legal - i.e. the instruction formed with these operands would verify. |
646 | static bool isNoopCast( |
647 | Instruction::CastOps Opcode, ///< Opcode of cast |
648 | Type *SrcTy, ///< SrcTy of cast |
649 | Type *DstTy, ///< DstTy of cast |
650 | const DataLayout &DL ///< DataLayout to get the Int Ptr type from. |
651 | ); |
652 | |
653 | /// Determine if this cast is a no-op cast. |
654 | /// |
655 | /// \param DL is the DataLayout to determine pointer size. |
656 | bool isNoopCast(const DataLayout &DL) const; |
657 | |
658 | /// Determine how a pair of casts can be eliminated, if they can be at all. |
659 | /// This is a helper function for both CastInst and ConstantExpr. |
660 | /// @returns 0 if the CastInst pair can't be eliminated, otherwise |
661 | /// returns Instruction::CastOps value for a cast that can replace |
662 | /// the pair, casting SrcTy to DstTy. |
663 | /// Determine if a cast pair is eliminable |
664 | static unsigned isEliminableCastPair( |
665 | Instruction::CastOps firstOpcode, ///< Opcode of first cast |
666 | Instruction::CastOps secondOpcode, ///< Opcode of second cast |
667 | Type *SrcTy, ///< SrcTy of 1st cast |
668 | Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast |
669 | Type *DstTy, ///< DstTy of 2nd cast |
670 | Type *SrcIntPtrTy, ///< Integer type corresponding to Ptr SrcTy, or null |
671 | Type *MidIntPtrTy, ///< Integer type corresponding to Ptr MidTy, or null |
672 | Type *DstIntPtrTy ///< Integer type corresponding to Ptr DstTy, or null |
673 | ); |
674 | |
675 | /// Return the opcode of this CastInst |
676 | Instruction::CastOps getOpcode() const { |
677 | return Instruction::CastOps(Instruction::getOpcode()); |
678 | } |
679 | |
680 | /// Return the source type, as a convenience |
681 | Type* getSrcTy() const { return getOperand(0)->getType(); } |
682 | /// Return the destination type, as a convenience |
683 | Type* getDestTy() const { return getType(); } |
684 | |
685 | /// This method can be used to determine if a cast from SrcTy to DstTy using |
686 | /// Opcode op is valid or not. |
687 | /// @returns true iff the proposed cast is valid. |
688 | /// Determine if a cast is valid without creating one. |
689 | static bool castIsValid(Instruction::CastOps op, Type *SrcTy, Type *DstTy); |
690 | static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) { |
691 | return castIsValid(op, S->getType(), DstTy); |
692 | } |
693 | |
694 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
695 | static bool classof(const Instruction *I) { |
696 | return I->isCast(); |
697 | } |
698 | static bool classof(const Value *V) { |
699 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
700 | } |
701 | }; |
702 | |
703 | //===----------------------------------------------------------------------===// |
704 | // CmpInst Class |
705 | //===----------------------------------------------------------------------===// |
706 | |
707 | /// This class is the base class for the comparison instructions. |
708 | /// Abstract base class of comparison instructions. |
709 | class CmpInst : public Instruction { |
710 | public: |
711 | /// This enumeration lists the possible predicates for CmpInst subclasses. |
712 | /// Values in the range 0-31 are reserved for FCmpInst, while values in the |
713 | /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the |
714 | /// predicate values are not overlapping between the classes. |
715 | /// |
716 | /// Some passes (e.g. InstCombine) depend on the bit-wise characteristics of |
717 | /// FCMP_* values. Changing the bit patterns requires a potential change to |
718 | /// those passes. |
719 | enum Predicate : unsigned { |
720 | // Opcode U L G E Intuitive operation |
721 | FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded) |
722 | FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal |
723 | FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than |
724 | FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal |
725 | FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than |
726 | FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal |
727 | FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal |
728 | FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans) |
729 | FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y) |
730 | FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal |
731 | FCMP_UGT = 10, ///< 1 0 1 0 True if unordered or greater than |
732 | FCMP_UGE = 11, ///< 1 0 1 1 True if unordered, greater than, or equal |
733 | FCMP_ULT = 12, ///< 1 1 0 0 True if unordered or less than |
734 | FCMP_ULE = 13, ///< 1 1 0 1 True if unordered, less than, or equal |
735 | FCMP_UNE = 14, ///< 1 1 1 0 True if unordered or not equal |
736 | FCMP_TRUE = 15, ///< 1 1 1 1 Always true (always folded) |
737 | FIRST_FCMP_PREDICATE = FCMP_FALSE, |
738 | LAST_FCMP_PREDICATE = FCMP_TRUE, |
739 | BAD_FCMP_PREDICATE = FCMP_TRUE + 1, |
740 | ICMP_EQ = 32, ///< equal |
741 | ICMP_NE = 33, ///< not equal |
742 | ICMP_UGT = 34, ///< unsigned greater than |
743 | ICMP_UGE = 35, ///< unsigned greater or equal |
744 | ICMP_ULT = 36, ///< unsigned less than |
745 | ICMP_ULE = 37, ///< unsigned less or equal |
746 | ICMP_SGT = 38, ///< signed greater than |
747 | ICMP_SGE = 39, ///< signed greater or equal |
748 | ICMP_SLT = 40, ///< signed less than |
749 | ICMP_SLE = 41, ///< signed less or equal |
750 | FIRST_ICMP_PREDICATE = ICMP_EQ, |
751 | LAST_ICMP_PREDICATE = ICMP_SLE, |
752 | BAD_ICMP_PREDICATE = ICMP_SLE + 1 |
753 | }; |
754 | using PredicateField = |
755 | Bitfield::Element<Predicate, 0, 6, LAST_ICMP_PREDICATE>; |
756 | |
757 | /// Returns the sequence of all FCmp predicates. |
758 | static auto FCmpPredicates() { |
759 | return enum_seq_inclusive(Predicate::FIRST_FCMP_PREDICATE, |
760 | Predicate::LAST_FCMP_PREDICATE, |
761 | force_iteration_on_noniterable_enum); |
762 | } |
763 | |
764 | /// Returns the sequence of all ICmp predicates. |
765 | static auto ICmpPredicates() { |
766 | return enum_seq_inclusive(Predicate::FIRST_ICMP_PREDICATE, |
767 | Predicate::LAST_ICMP_PREDICATE, |
768 | force_iteration_on_noniterable_enum); |
769 | } |
770 | |
771 | protected: |
772 | CmpInst(Type *ty, Instruction::OtherOps op, Predicate pred, |
773 | Value *LHS, Value *RHS, const Twine &Name = "", |
774 | Instruction *InsertBefore = nullptr, |
775 | Instruction *FlagsSource = nullptr); |
776 | |
777 | CmpInst(Type *ty, Instruction::OtherOps op, Predicate pred, |
778 | Value *LHS, Value *RHS, const Twine &Name, |
779 | BasicBlock *InsertAtEnd); |
780 | |
781 | public: |
782 | // allocate space for exactly two operands |
783 | void *operator new(size_t S) { return User::operator new(S, 2); } |
784 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
785 | |
786 | /// Construct a compare instruction, given the opcode, the predicate and |
787 | /// the two operands. Optionally (if InstBefore is specified) insert the |
788 | /// instruction into a BasicBlock right before the specified instruction. |
789 | /// The specified Instruction is allowed to be a dereferenced end iterator. |
790 | /// Create a CmpInst |
791 | static CmpInst *Create(OtherOps Op, |
792 | Predicate predicate, Value *S1, |
793 | Value *S2, const Twine &Name = "", |
794 | Instruction *InsertBefore = nullptr); |
795 | |
796 | /// Construct a compare instruction, given the opcode, the predicate and the |
797 | /// two operands. Also automatically insert this instruction to the end of |
798 | /// the BasicBlock specified. |
799 | /// Create a CmpInst |
800 | static CmpInst *Create(OtherOps Op, Predicate predicate, Value *S1, |
801 | Value *S2, const Twine &Name, BasicBlock *InsertAtEnd); |
802 | |
803 | /// Get the opcode casted to the right type |
804 | OtherOps getOpcode() const { |
805 | return static_cast<OtherOps>(Instruction::getOpcode()); |
806 | } |
807 | |
808 | /// Return the predicate for this instruction. |
809 | Predicate getPredicate() const { return getSubclassData<PredicateField>(); } |
810 | |
811 | /// Set the predicate for this instruction to the specified value. |
812 | void setPredicate(Predicate P) { setSubclassData<PredicateField>(P); } |
813 | |
814 | static bool isFPPredicate(Predicate P) { |
815 | static_assert(FIRST_FCMP_PREDICATE == 0, |
816 | "FIRST_FCMP_PREDICATE is required to be 0"); |
817 | return P <= LAST_FCMP_PREDICATE; |
818 | } |
819 | |
820 | static bool isIntPredicate(Predicate P) { |
821 | return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE; |
822 | } |
823 | |
824 | static StringRef getPredicateName(Predicate P); |
825 | |
826 | bool isFPPredicate() const { return isFPPredicate(getPredicate()); } |
827 | bool isIntPredicate() const { return isIntPredicate(getPredicate()); } |
828 | |
829 | /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, |
830 | /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc. |
831 | /// @returns the inverse predicate for the instruction's current predicate. |
832 | /// Return the inverse of the instruction's predicate. |
833 | Predicate getInversePredicate() const { |
834 | return getInversePredicate(getPredicate()); |
835 | } |
836 | |
837 | /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, |
838 | /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc. |
839 | /// @returns the inverse predicate for predicate provided in \p pred. |
840 | /// Return the inverse of a given predicate |
841 | static Predicate getInversePredicate(Predicate pred); |
842 | |
843 | /// For example, EQ->EQ, SLE->SGE, ULT->UGT, |
844 | /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc. |
845 | /// @returns the predicate that would be the result of exchanging the two |
846 | /// operands of the CmpInst instruction without changing the result |
847 | /// produced. |
848 | /// Return the predicate as if the operands were swapped |
849 | Predicate getSwappedPredicate() const { |
850 | return getSwappedPredicate(getPredicate()); |
851 | } |
852 | |
853 | /// This is a static version that you can use without an instruction |
854 | /// available. |
855 | /// Return the predicate as if the operands were swapped. |
856 | static Predicate getSwappedPredicate(Predicate pred); |
857 | |
858 | /// This is a static version that you can use without an instruction |
859 | /// available. |
860 | /// @returns true if the comparison predicate is strict, false otherwise. |
861 | static bool isStrictPredicate(Predicate predicate); |
862 | |
863 | /// @returns true if the comparison predicate is strict, false otherwise. |
864 | /// Determine if this instruction is using an strict comparison predicate. |
865 | bool isStrictPredicate() const { return isStrictPredicate(getPredicate()); } |
866 | |
867 | /// This is a static version that you can use without an instruction |
868 | /// available. |
869 | /// @returns true if the comparison predicate is non-strict, false otherwise. |
870 | static bool isNonStrictPredicate(Predicate predicate); |
871 | |
872 | /// @returns true if the comparison predicate is non-strict, false otherwise. |
873 | /// Determine if this instruction is using an non-strict comparison predicate. |
874 | bool isNonStrictPredicate() const { |
875 | return isNonStrictPredicate(getPredicate()); |
876 | } |
877 | |
878 | /// For example, SGE -> SGT, SLE -> SLT, ULE -> ULT, UGE -> UGT. |
879 | /// Returns the strict version of non-strict comparisons. |
880 | Predicate getStrictPredicate() const { |
881 | return getStrictPredicate(getPredicate()); |
882 | } |
883 | |
884 | /// This is a static version that you can use without an instruction |
885 | /// available. |
886 | /// @returns the strict version of comparison provided in \p pred. |
887 | /// If \p pred is not a strict comparison predicate, returns \p pred. |
888 | /// Returns the strict version of non-strict comparisons. |
889 | static Predicate getStrictPredicate(Predicate pred); |
890 | |
891 | /// For example, SGT -> SGE, SLT -> SLE, ULT -> ULE, UGT -> UGE. |
892 | /// Returns the non-strict version of strict comparisons. |
893 | Predicate getNonStrictPredicate() const { |
894 | return getNonStrictPredicate(getPredicate()); |
895 | } |
896 | |
897 | /// This is a static version that you can use without an instruction |
898 | /// available. |
899 | /// @returns the non-strict version of comparison provided in \p pred. |
900 | /// If \p pred is not a strict comparison predicate, returns \p pred. |
901 | /// Returns the non-strict version of strict comparisons. |
902 | static Predicate getNonStrictPredicate(Predicate pred); |
903 | |
904 | /// This is a static version that you can use without an instruction |
905 | /// available. |
906 | /// Return the flipped strictness of predicate |
907 | static Predicate getFlippedStrictnessPredicate(Predicate pred); |
908 | |
909 | /// For predicate of kind "is X or equal to 0" returns the predicate "is X". |
910 | /// For predicate of kind "is X" returns the predicate "is X or equal to 0". |
911 | /// does not support other kind of predicates. |
912 | /// @returns the predicate that does not contains is equal to zero if |
913 | /// it had and vice versa. |
914 | /// Return the flipped strictness of predicate |
915 | Predicate getFlippedStrictnessPredicate() const { |
916 | return getFlippedStrictnessPredicate(getPredicate()); |
917 | } |
918 | |
919 | /// Provide more efficient getOperand methods. |
920 | 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; |
921 | |
922 | /// This is just a convenience that dispatches to the subclasses. |
923 | /// Swap the operands and adjust predicate accordingly to retain |
924 | /// the same comparison. |
925 | void swapOperands(); |
926 | |
927 | /// This is just a convenience that dispatches to the subclasses. |
928 | /// Determine if this CmpInst is commutative. |
929 | bool isCommutative() const; |
930 | |
931 | /// Determine if this is an equals/not equals predicate. |
932 | /// This is a static version that you can use without an instruction |
933 | /// available. |
934 | static bool isEquality(Predicate pred); |
935 | |
936 | /// Determine if this is an equals/not equals predicate. |
937 | bool isEquality() const { return isEquality(getPredicate()); } |
938 | |
939 | /// Return true if the predicate is relational (not EQ or NE). |
940 | static bool isRelational(Predicate P) { return !isEquality(P); } |
941 | |
942 | /// Return true if the predicate is relational (not EQ or NE). |
943 | bool isRelational() const { return !isEquality(); } |
944 | |
945 | /// @returns true if the comparison is signed, false otherwise. |
946 | /// Determine if this instruction is using a signed comparison. |
947 | bool isSigned() const { |
948 | return isSigned(getPredicate()); |
949 | } |
950 | |
951 | /// @returns true if the comparison is unsigned, false otherwise. |
952 | /// Determine if this instruction is using an unsigned comparison. |
953 | bool isUnsigned() const { |
954 | return isUnsigned(getPredicate()); |
955 | } |
956 | |
957 | /// For example, ULT->SLT, ULE->SLE, UGT->SGT, UGE->SGE, SLT->Failed assert |
958 | /// @returns the signed version of the unsigned predicate pred. |
959 | /// return the signed version of a predicate |
960 | static Predicate getSignedPredicate(Predicate pred); |
961 | |
962 | /// For example, ULT->SLT, ULE->SLE, UGT->SGT, UGE->SGE, SLT->Failed assert |
963 | /// @returns the signed version of the predicate for this instruction (which |
964 | /// has to be an unsigned predicate). |
965 | /// return the signed version of a predicate |
966 | Predicate getSignedPredicate() { |
967 | return getSignedPredicate(getPredicate()); |
968 | } |
969 | |
970 | /// For example, SLT->ULT, SLE->ULE, SGT->UGT, SGE->UGE, ULT->Failed assert |
971 | /// @returns the unsigned version of the signed predicate pred. |
972 | static Predicate getUnsignedPredicate(Predicate pred); |
973 | |
974 | /// For example, SLT->ULT, SLE->ULE, SGT->UGT, SGE->UGE, ULT->Failed assert |
975 | /// @returns the unsigned version of the predicate for this instruction (which |
976 | /// has to be an signed predicate). |
977 | /// return the unsigned version of a predicate |
978 | Predicate getUnsignedPredicate() { |
979 | return getUnsignedPredicate(getPredicate()); |
980 | } |
981 | |
982 | /// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert |
983 | /// @returns the unsigned version of the signed predicate pred or |
984 | /// the signed version of the signed predicate pred. |
985 | static Predicate getFlippedSignednessPredicate(Predicate pred); |
986 | |
987 | /// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert |
988 | /// @returns the unsigned version of the signed predicate pred or |
989 | /// the signed version of the signed predicate pred. |
990 | Predicate getFlippedSignednessPredicate() { |
991 | return getFlippedSignednessPredicate(getPredicate()); |
992 | } |
993 | |
994 | /// This is just a convenience. |
995 | /// Determine if this is true when both operands are the same. |
996 | bool isTrueWhenEqual() const { |
997 | return isTrueWhenEqual(getPredicate()); |
998 | } |
999 | |
1000 | /// This is just a convenience. |
1001 | /// Determine if this is false when both operands are the same. |
1002 | bool isFalseWhenEqual() const { |
1003 | return isFalseWhenEqual(getPredicate()); |
1004 | } |
1005 | |
1006 | /// @returns true if the predicate is unsigned, false otherwise. |
1007 | /// Determine if the predicate is an unsigned operation. |
1008 | static bool isUnsigned(Predicate predicate); |
1009 | |
1010 | /// @returns true if the predicate is signed, false otherwise. |
1011 | /// Determine if the predicate is an signed operation. |
1012 | static bool isSigned(Predicate predicate); |
1013 | |
1014 | /// Determine if the predicate is an ordered operation. |
1015 | static bool isOrdered(Predicate predicate); |
1016 | |
1017 | /// Determine if the predicate is an unordered operation. |
1018 | static bool isUnordered(Predicate predicate); |
1019 | |
1020 | /// Determine if the predicate is true when comparing a value with itself. |
1021 | static bool isTrueWhenEqual(Predicate predicate); |
1022 | |
1023 | /// Determine if the predicate is false when comparing a value with itself. |
1024 | static bool isFalseWhenEqual(Predicate predicate); |
1025 | |
1026 | /// Determine if Pred1 implies Pred2 is true when two compares have matching |
1027 | /// operands. |
1028 | static bool isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2); |
1029 | |
1030 | /// Determine if Pred1 implies Pred2 is false when two compares have matching |
1031 | /// operands. |
1032 | static bool isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2); |
1033 | |
1034 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
1035 | static bool classof(const Instruction *I) { |
1036 | return I->getOpcode() == Instruction::ICmp || |
1037 | I->getOpcode() == Instruction::FCmp; |
1038 | } |
1039 | static bool classof(const Value *V) { |
1040 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1041 | } |
1042 | |
1043 | /// Create a result type for fcmp/icmp |
1044 | static Type* makeCmpResultType(Type* opnd_type) { |
1045 | if (VectorType* vt = dyn_cast<VectorType>(opnd_type)) { |
1046 | return VectorType::get(Type::getInt1Ty(opnd_type->getContext()), |
1047 | vt->getElementCount()); |
1048 | } |
1049 | return Type::getInt1Ty(opnd_type->getContext()); |
1050 | } |
1051 | |
1052 | private: |
1053 | // Shadow Value::setValueSubclassData with a private forwarding method so that |
1054 | // subclasses cannot accidentally use it. |
1055 | void setValueSubclassData(unsigned short D) { |
1056 | Value::setValueSubclassData(D); |
1057 | } |
1058 | }; |
1059 | |
1060 | // FIXME: these are redundant if CmpInst < BinaryOperator |
1061 | template <> |
1062 | struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> { |
1063 | }; |
1064 | |
1065 | 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 { (static_cast <bool> (i_nocapture < OperandTraits <CmpInst>::operands(this) && "getOperand() out of range!" ) ? void (0) : __assert_fail ("i_nocapture < OperandTraits<CmpInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1065, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<CmpInst>::operands(this) && "setOperand() out of range!" ) ? void (0) : __assert_fail ("i_nocapture < OperandTraits<CmpInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1065, __extension__ __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); } |
1066 | |
1067 | /// A lightweight accessor for an operand bundle meant to be passed |
1068 | /// around by value. |
1069 | struct OperandBundleUse { |
1070 | ArrayRef<Use> Inputs; |
1071 | |
1072 | OperandBundleUse() = default; |
1073 | explicit OperandBundleUse(StringMapEntry<uint32_t> *Tag, ArrayRef<Use> Inputs) |
1074 | : Inputs(Inputs), Tag(Tag) {} |
1075 | |
1076 | /// Return true if the operand at index \p Idx in this operand bundle |
1077 | /// has the attribute A. |
1078 | bool operandHasAttr(unsigned Idx, Attribute::AttrKind A) const { |
1079 | if (isDeoptOperandBundle()) |
1080 | if (A == Attribute::ReadOnly || A == Attribute::NoCapture) |
1081 | return Inputs[Idx]->getType()->isPointerTy(); |
1082 | |
1083 | // Conservative answer: no operands have any attributes. |
1084 | return false; |
1085 | } |
1086 | |
1087 | /// Return the tag of this operand bundle as a string. |
1088 | StringRef getTagName() const { |
1089 | return Tag->getKey(); |
1090 | } |
1091 | |
1092 | /// Return the tag of this operand bundle as an integer. |
1093 | /// |
1094 | /// Operand bundle tags are interned by LLVMContextImpl::getOrInsertBundleTag, |
1095 | /// and this function returns the unique integer getOrInsertBundleTag |
1096 | /// associated the tag of this operand bundle to. |
1097 | uint32_t getTagID() const { |
1098 | return Tag->getValue(); |
1099 | } |
1100 | |
1101 | /// Return true if this is a "deopt" operand bundle. |
1102 | bool isDeoptOperandBundle() const { |
1103 | return getTagID() == LLVMContext::OB_deopt; |
1104 | } |
1105 | |
1106 | /// Return true if this is a "funclet" operand bundle. |
1107 | bool isFuncletOperandBundle() const { |
1108 | return getTagID() == LLVMContext::OB_funclet; |
1109 | } |
1110 | |
1111 | /// Return true if this is a "cfguardtarget" operand bundle. |
1112 | bool isCFGuardTargetOperandBundle() const { |
1113 | return getTagID() == LLVMContext::OB_cfguardtarget; |
1114 | } |
1115 | |
1116 | private: |
1117 | /// Pointer to an entry in LLVMContextImpl::getOrInsertBundleTag. |
1118 | StringMapEntry<uint32_t> *Tag; |
1119 | }; |
1120 | |
1121 | /// A container for an operand bundle being viewed as a set of values |
1122 | /// rather than a set of uses. |
1123 | /// |
1124 | /// Unlike OperandBundleUse, OperandBundleDefT owns the memory it carries, and |
1125 | /// so it is possible to create and pass around "self-contained" instances of |
1126 | /// OperandBundleDef and ConstOperandBundleDef. |
1127 | template <typename InputTy> class OperandBundleDefT { |
1128 | std::string Tag; |
1129 | std::vector<InputTy> Inputs; |
1130 | |
1131 | public: |
1132 | explicit OperandBundleDefT(std::string Tag, std::vector<InputTy> Inputs) |
1133 | : Tag(std::move(Tag)), Inputs(std::move(Inputs)) {} |
1134 | explicit OperandBundleDefT(std::string Tag, ArrayRef<InputTy> Inputs) |
1135 | : Tag(std::move(Tag)), Inputs(Inputs) {} |
1136 | |
1137 | explicit OperandBundleDefT(const OperandBundleUse &OBU) { |
1138 | Tag = std::string(OBU.getTagName()); |
1139 | llvm::append_range(Inputs, OBU.Inputs); |
1140 | } |
1141 | |
1142 | ArrayRef<InputTy> inputs() const { return Inputs; } |
1143 | |
1144 | using input_iterator = typename std::vector<InputTy>::const_iterator; |
1145 | |
1146 | size_t input_size() const { return Inputs.size(); } |
1147 | input_iterator input_begin() const { return Inputs.begin(); } |
1148 | input_iterator input_end() const { return Inputs.end(); } |
1149 | |
1150 | StringRef getTag() const { return Tag; } |
1151 | }; |
1152 | |
1153 | using OperandBundleDef = OperandBundleDefT<Value *>; |
1154 | using ConstOperandBundleDef = OperandBundleDefT<const Value *>; |
1155 | |
1156 | //===----------------------------------------------------------------------===// |
1157 | // CallBase Class |
1158 | //===----------------------------------------------------------------------===// |
1159 | |
1160 | /// Base class for all callable instructions (InvokeInst and CallInst) |
1161 | /// Holds everything related to calling a function. |
1162 | /// |
1163 | /// All call-like instructions are required to use a common operand layout: |
1164 | /// - Zero or more arguments to the call, |
1165 | /// - Zero or more operand bundles with zero or more operand inputs each |
1166 | /// bundle, |
1167 | /// - Zero or more subclass controlled operands |
1168 | /// - The called function. |
1169 | /// |
1170 | /// This allows this base class to easily access the called function and the |
1171 | /// start of the arguments without knowing how many other operands a particular |
1172 | /// subclass requires. Note that accessing the end of the argument list isn't |
1173 | /// as cheap as most other operations on the base class. |
1174 | class CallBase : public Instruction { |
1175 | protected: |
1176 | // The first two bits are reserved by CallInst for fast retrieval, |
1177 | using CallInstReservedField = Bitfield::Element<unsigned, 0, 2>; |
1178 | using CallingConvField = |
1179 | Bitfield::Element<CallingConv::ID, CallInstReservedField::NextBit, 10, |
1180 | CallingConv::MaxID>; |
1181 | static_assert( |
1182 | Bitfield::areContiguous<CallInstReservedField, CallingConvField>(), |
1183 | "Bitfields must be contiguous"); |
1184 | |
1185 | /// The last operand is the called operand. |
1186 | static constexpr int CalledOperandOpEndIdx = -1; |
1187 | |
1188 | AttributeList Attrs; ///< parameter attributes for callable |
1189 | FunctionType *FTy; |
1190 | |
1191 | template <class... ArgsTy> |
1192 | CallBase(AttributeList const &A, FunctionType *FT, ArgsTy &&... Args) |
1193 | : Instruction(std::forward<ArgsTy>(Args)...), Attrs(A), FTy(FT) {} |
1194 | |
1195 | using Instruction::Instruction; |
1196 | |
1197 | bool hasDescriptor() const { return Value::HasDescriptor; } |
1198 | |
1199 | unsigned getNumSubclassExtraOperands() const { |
1200 | switch (getOpcode()) { |
1201 | case Instruction::Call: |
1202 | return 0; |
1203 | case Instruction::Invoke: |
1204 | return 2; |
1205 | case Instruction::CallBr: |
1206 | return getNumSubclassExtraOperandsDynamic(); |
1207 | } |
1208 | llvm_unreachable("Invalid opcode!")::llvm::llvm_unreachable_internal("Invalid opcode!", "llvm/include/llvm/IR/InstrTypes.h" , 1208); |
1209 | } |
1210 | |
1211 | /// Get the number of extra operands for instructions that don't have a fixed |
1212 | /// number of extra operands. |
1213 | unsigned getNumSubclassExtraOperandsDynamic() const; |
1214 | |
1215 | public: |
1216 | using Instruction::getContext; |
1217 | |
1218 | /// Create a clone of \p CB with a different set of operand bundles and |
1219 | /// insert it before \p InsertPt. |
1220 | /// |
1221 | /// The returned call instruction is identical \p CB in every way except that |
1222 | /// the operand bundles for the new instruction are set to the operand bundles |
1223 | /// in \p Bundles. |
1224 | static CallBase *Create(CallBase *CB, ArrayRef<OperandBundleDef> Bundles, |
1225 | Instruction *InsertPt = nullptr); |
1226 | |
1227 | /// Create a clone of \p CB with the operand bundle with the tag matching |
1228 | /// \p Bundle's tag replaced with Bundle, and insert it before \p InsertPt. |
1229 | /// |
1230 | /// The returned call instruction is identical \p CI in every way except that |
1231 | /// the specified operand bundle has been replaced. |
1232 | static CallBase *Create(CallBase *CB, |
1233 | OperandBundleDef Bundle, |
1234 | Instruction *InsertPt = nullptr); |
1235 | |
1236 | /// Create a clone of \p CB with operand bundle \p OB added. |
1237 | static CallBase *addOperandBundle(CallBase *CB, uint32_t ID, |
1238 | OperandBundleDef OB, |
1239 | Instruction *InsertPt = nullptr); |
1240 | |
1241 | /// Create a clone of \p CB with operand bundle \p ID removed. |
1242 | static CallBase *removeOperandBundle(CallBase *CB, uint32_t ID, |
1243 | Instruction *InsertPt = nullptr); |
1244 | |
1245 | static bool classof(const Instruction *I) { |
1246 | return I->getOpcode() == Instruction::Call || |
1247 | I->getOpcode() == Instruction::Invoke || |
1248 | I->getOpcode() == Instruction::CallBr; |
1249 | } |
1250 | static bool classof(const Value *V) { |
1251 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1252 | } |
1253 | |
1254 | FunctionType *getFunctionType() const { return FTy; } |
1255 | |
1256 | void mutateFunctionType(FunctionType *FTy) { |
1257 | Value::mutateType(FTy->getReturnType()); |
1258 | this->FTy = FTy; |
1259 | } |
1260 | |
1261 | 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; |
1262 | |
1263 | /// data_operands_begin/data_operands_end - Return iterators iterating over |
1264 | /// the call / invoke argument list and bundle operands. For invokes, this is |
1265 | /// the set of instruction operands except the invoke target and the two |
1266 | /// successor blocks; and for calls this is the set of instruction operands |
1267 | /// except the call target. |
1268 | User::op_iterator data_operands_begin() { return op_begin(); } |
1269 | User::const_op_iterator data_operands_begin() const { |
1270 | return const_cast<CallBase *>(this)->data_operands_begin(); |
1271 | } |
1272 | User::op_iterator data_operands_end() { |
1273 | // Walk from the end of the operands over the called operand and any |
1274 | // subclass operands. |
1275 | return op_end() - getNumSubclassExtraOperands() - 1; |
1276 | } |
1277 | User::const_op_iterator data_operands_end() const { |
1278 | return const_cast<CallBase *>(this)->data_operands_end(); |
1279 | } |
1280 | iterator_range<User::op_iterator> data_ops() { |
1281 | return make_range(data_operands_begin(), data_operands_end()); |
1282 | } |
1283 | iterator_range<User::const_op_iterator> data_ops() const { |
1284 | return make_range(data_operands_begin(), data_operands_end()); |
1285 | } |
1286 | bool data_operands_empty() const { |
1287 | return data_operands_end() == data_operands_begin(); |
1288 | } |
1289 | unsigned data_operands_size() const { |
1290 | return std::distance(data_operands_begin(), data_operands_end()); |
1291 | } |
1292 | |
1293 | bool isDataOperand(const Use *U) const { |
1294 | assert(this == U->getUser() &&(static_cast <bool> (this == U->getUser() && "Only valid to query with a use of this instruction!") ? void (0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1295, __extension__ __PRETTY_FUNCTION__ )) |
1295 | "Only valid to query with a use of this instruction!")(static_cast <bool> (this == U->getUser() && "Only valid to query with a use of this instruction!") ? void (0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1295, __extension__ __PRETTY_FUNCTION__ )); |
1296 | return data_operands_begin() <= U && U < data_operands_end(); |
1297 | } |
1298 | bool isDataOperand(Value::const_user_iterator UI) const { |
1299 | return isDataOperand(&UI.getUse()); |
1300 | } |
1301 | |
1302 | /// Given a value use iterator, return the data operand corresponding to it. |
1303 | /// Iterator must actually correspond to a data operand. |
1304 | unsigned getDataOperandNo(Value::const_user_iterator UI) const { |
1305 | return getDataOperandNo(&UI.getUse()); |
1306 | } |
1307 | |
1308 | /// Given a use for a data operand, get the data operand number that |
1309 | /// corresponds to it. |
1310 | unsigned getDataOperandNo(const Use *U) const { |
1311 | assert(isDataOperand(U) && "Data operand # out of range!")(static_cast <bool> (isDataOperand(U) && "Data operand # out of range!" ) ? void (0) : __assert_fail ("isDataOperand(U) && \"Data operand # out of range!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1311, __extension__ __PRETTY_FUNCTION__ )); |
1312 | return U - data_operands_begin(); |
1313 | } |
1314 | |
1315 | /// Return the iterator pointing to the beginning of the argument list. |
1316 | User::op_iterator arg_begin() { return op_begin(); } |
1317 | User::const_op_iterator arg_begin() const { |
1318 | return const_cast<CallBase *>(this)->arg_begin(); |
1319 | } |
1320 | |
1321 | /// Return the iterator pointing to the end of the argument list. |
1322 | User::op_iterator arg_end() { |
1323 | // From the end of the data operands, walk backwards past the bundle |
1324 | // operands. |
1325 | return data_operands_end() - getNumTotalBundleOperands(); |
1326 | } |
1327 | User::const_op_iterator arg_end() const { |
1328 | return const_cast<CallBase *>(this)->arg_end(); |
1329 | } |
1330 | |
1331 | /// Iteration adapter for range-for loops. |
1332 | iterator_range<User::op_iterator> args() { |
1333 | return make_range(arg_begin(), arg_end()); |
1334 | } |
1335 | iterator_range<User::const_op_iterator> args() const { |
1336 | return make_range(arg_begin(), arg_end()); |
1337 | } |
1338 | bool arg_empty() const { return arg_end() == arg_begin(); } |
1339 | unsigned arg_size() const { return arg_end() - arg_begin(); } |
1340 | |
1341 | Value *getArgOperand(unsigned i) const { |
1342 | assert(i < arg_size() && "Out of bounds!")(static_cast <bool> (i < arg_size() && "Out of bounds!" ) ? void (0) : __assert_fail ("i < arg_size() && \"Out of bounds!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1342, __extension__ __PRETTY_FUNCTION__ )); |
1343 | return getOperand(i); |
1344 | } |
1345 | |
1346 | void setArgOperand(unsigned i, Value *v) { |
1347 | assert(i < arg_size() && "Out of bounds!")(static_cast <bool> (i < arg_size() && "Out of bounds!" ) ? void (0) : __assert_fail ("i < arg_size() && \"Out of bounds!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1347, __extension__ __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 < arg_size() && "Out of bounds!")(static_cast <bool> (i < arg_size() && "Out of bounds!" ) ? void (0) : __assert_fail ("i < arg_size() && \"Out of bounds!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1353, __extension__ __PRETTY_FUNCTION__ )); |
1354 | return User::getOperandUse(i); |
1355 | } |
1356 | Use &getArgOperandUse(unsigned i) { |
1357 | assert(i < arg_size() && "Out of bounds!")(static_cast <bool> (i < arg_size() && "Out of bounds!" ) ? void (0) : __assert_fail ("i < arg_size() && \"Out of bounds!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1357, __extension__ __PRETTY_FUNCTION__ )); |
1358 | return User::getOperandUse(i); |
1359 | } |
1360 | |
1361 | bool isArgOperand(const Use *U) const { |
1362 | assert(this == U->getUser() &&(static_cast <bool> (this == U->getUser() && "Only valid to query with a use of this instruction!") ? void (0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1363, __extension__ __PRETTY_FUNCTION__ )) |
1363 | "Only valid to query with a use of this instruction!")(static_cast <bool> (this == U->getUser() && "Only valid to query with a use of this instruction!") ? void (0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1363, __extension__ __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!")(static_cast <bool> (isArgOperand(U) && "Arg operand # out of range!" ) ? void (0) : __assert_fail ("isArgOperand(U) && \"Arg operand # out of range!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1373, __extension__ __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 indirect function |
1395 | /// invocation or the function signature does not match the call signature. |
1396 | Function *getCalledFunction() const { |
1397 | if (auto *F = dyn_cast_or_null<Function>(getCalledOperand())) |
1398 | if (F->getValueType() == getFunctionType()) |
1399 | return F; |
1400 | return nullptr; |
1401 | } |
1402 | |
1403 | /// Return true if the callsite is an indirect call. |
1404 | bool isIndirectCall() const; |
1405 | |
1406 | /// Determine whether the passed iterator points to the callee operand's Use. |
1407 | bool isCallee(Value::const_user_iterator UI) const { |
1408 | return isCallee(&UI.getUse()); |
1409 | } |
1410 | |
1411 | /// Determine whether this Use is the callee operand's Use. |
1412 | bool isCallee(const Use *U) const { return &getCalledOperandUse() == U; } |
1413 | |
1414 | /// Helper to get the caller (the parent function). |
1415 | Function *getCaller(); |
1416 | const Function *getCaller() const { |
1417 | return const_cast<CallBase *>(this)->getCaller(); |
1418 | } |
1419 | |
1420 | /// Tests if this call site must be tail call optimized. Only a CallInst can |
1421 | /// be tail call optimized. |
1422 | bool isMustTailCall() const; |
1423 | |
1424 | /// Tests if this call site is marked as a tail call. |
1425 | bool isTailCall() const; |
1426 | |
1427 | /// Returns the intrinsic ID of the intrinsic called or |
1428 | /// Intrinsic::not_intrinsic if the called function is not an intrinsic, or if |
1429 | /// this is an indirect call. |
1430 | Intrinsic::ID getIntrinsicID() const; |
1431 | |
1432 | void setCalledOperand(Value *V) { Op<CalledOperandOpEndIdx>() = V; } |
1433 | |
1434 | /// Sets the function called, including updating the function type. |
1435 | void setCalledFunction(Function *Fn) { |
1436 | setCalledFunction(Fn->getFunctionType(), Fn); |
1437 | } |
1438 | |
1439 | /// Sets the function called, including updating the function type. |
1440 | void setCalledFunction(FunctionCallee Fn) { |
1441 | setCalledFunction(Fn.getFunctionType(), Fn.getCallee()); |
1442 | } |
1443 | |
1444 | /// Sets the function called, including updating to the specified function |
1445 | /// type. |
1446 | void setCalledFunction(FunctionType *FTy, Value *Fn) { |
1447 | this->FTy = FTy; |
1448 | assert(cast<PointerType>(Fn->getType())->isOpaqueOrPointeeTypeMatches(FTy))(static_cast <bool> (cast<PointerType>(Fn->getType ())->isOpaqueOrPointeeTypeMatches(FTy)) ? void (0) : __assert_fail ("cast<PointerType>(Fn->getType())->isOpaqueOrPointeeTypeMatches(FTy)" , "llvm/include/llvm/IR/InstrTypes.h", 1448, __extension__ __PRETTY_FUNCTION__ )); |
1449 | // This function doesn't mutate the return type, only the function |
1450 | // type. Seems broken, but I'm just gonna stick an assert in for now. |
1451 | assert(getType() == FTy->getReturnType())(static_cast <bool> (getType() == FTy->getReturnType ()) ? void (0) : __assert_fail ("getType() == FTy->getReturnType()" , "llvm/include/llvm/IR/InstrTypes.h", 1451, __extension__ __PRETTY_FUNCTION__ )); |
1452 | setCalledOperand(Fn); |
1453 | } |
1454 | |
1455 | CallingConv::ID getCallingConv() const { |
1456 | return getSubclassData<CallingConvField>(); |
1457 | } |
1458 | |
1459 | void setCallingConv(CallingConv::ID CC) { |
1460 | setSubclassData<CallingConvField>(CC); |
1461 | } |
1462 | |
1463 | /// Check if this call is an inline asm statement. |
1464 | bool isInlineAsm() const { return isa<InlineAsm>(getCalledOperand()); } |
1465 | |
1466 | /// \name Attribute API |
1467 | /// |
1468 | /// These methods access and modify attributes on this call (including |
1469 | /// looking through to the attributes on the called function when necessary). |
1470 | ///@{ |
1471 | |
1472 | /// Return the parameter attributes for this call. |
1473 | /// |
1474 | AttributeList getAttributes() const { return Attrs; } |
1475 | |
1476 | /// Set the parameter attributes for this call. |
1477 | /// |
1478 | void setAttributes(AttributeList A) { Attrs = A; } |
1479 | |
1480 | /// Determine whether this call has the given attribute. If it does not |
1481 | /// then determine if the called function has the attribute, but only if |
1482 | /// the attribute is allowed for the call. |
1483 | bool hasFnAttr(Attribute::AttrKind Kind) const { |
1484 | assert(Kind != Attribute::NoBuiltin &&(static_cast <bool> (Kind != Attribute::NoBuiltin && "Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin" ) ? void (0) : __assert_fail ("Kind != Attribute::NoBuiltin && \"Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin\"" , "llvm/include/llvm/IR/InstrTypes.h", 1485, __extension__ __PRETTY_FUNCTION__ )) |
1485 | "Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin")(static_cast <bool> (Kind != Attribute::NoBuiltin && "Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin" ) ? void (0) : __assert_fail ("Kind != Attribute::NoBuiltin && \"Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin\"" , "llvm/include/llvm/IR/InstrTypes.h", 1485, __extension__ __PRETTY_FUNCTION__ )); |
1486 | return hasFnAttrImpl(Kind); |
1487 | } |
1488 | |
1489 | /// Determine whether this call has the given attribute. If it does not |
1490 | /// then determine if the called function has the attribute, but only if |
1491 | /// the attribute is allowed for the call. |
1492 | bool hasFnAttr(StringRef Kind) const { return hasFnAttrImpl(Kind); } |
1493 | |
1494 | // TODO: remove non-AtIndex versions of these methods. |
1495 | /// adds the attribute to the list of attributes. |
1496 | void addAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) { |
1497 | Attrs = Attrs.addAttributeAtIndex(getContext(), i, Kind); |
1498 | } |
1499 | |
1500 | /// adds the attribute to the list of attributes. |
1501 | void addAttributeAtIndex(unsigned i, Attribute Attr) { |
1502 | Attrs = Attrs.addAttributeAtIndex(getContext(), i, Attr); |
1503 | } |
1504 | |
1505 | /// Adds the attribute to the function. |
1506 | void addFnAttr(Attribute::AttrKind Kind) { |
1507 | Attrs = Attrs.addFnAttribute(getContext(), Kind); |
1508 | } |
1509 | |
1510 | /// Adds the attribute to the function. |
1511 | void addFnAttr(Attribute Attr) { |
1512 | Attrs = Attrs.addFnAttribute(getContext(), Attr); |
1513 | } |
1514 | |
1515 | /// Adds the attribute to the return value. |
1516 | void addRetAttr(Attribute::AttrKind Kind) { |
1517 | Attrs = Attrs.addRetAttribute(getContext(), Kind); |
1518 | } |
1519 | |
1520 | /// Adds the attribute to the return value. |
1521 | void addRetAttr(Attribute Attr) { |
1522 | Attrs = Attrs.addRetAttribute(getContext(), Attr); |
1523 | } |
1524 | |
1525 | /// Adds the attribute to the indicated argument |
1526 | void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { |
1527 | assert(ArgNo < arg_size() && "Out of bounds")(static_cast <bool> (ArgNo < arg_size() && "Out of bounds" ) ? void (0) : __assert_fail ("ArgNo < arg_size() && \"Out of bounds\"" , "llvm/include/llvm/IR/InstrTypes.h", 1527, __extension__ __PRETTY_FUNCTION__ )); |
1528 | Attrs = Attrs.addParamAttribute(getContext(), ArgNo, Kind); |
1529 | } |
1530 | |
1531 | /// Adds the attribute to the indicated argument |
1532 | void addParamAttr(unsigned ArgNo, Attribute Attr) { |
1533 | assert(ArgNo < arg_size() && "Out of bounds")(static_cast <bool> (ArgNo < arg_size() && "Out of bounds" ) ? void (0) : __assert_fail ("ArgNo < arg_size() && \"Out of bounds\"" , "llvm/include/llvm/IR/InstrTypes.h", 1533, __extension__ __PRETTY_FUNCTION__ )); |
1534 | Attrs = Attrs.addParamAttribute(getContext(), ArgNo, Attr); |
1535 | } |
1536 | |
1537 | /// removes the attribute from the list of attributes. |
1538 | void removeAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) { |
1539 | Attrs = Attrs.removeAttributeAtIndex(getContext(), i, Kind); |
1540 | } |
1541 | |
1542 | /// removes the attribute from the list of attributes. |
1543 | void removeAttributeAtIndex(unsigned i, StringRef Kind) { |
1544 | Attrs = Attrs.removeAttributeAtIndex(getContext(), i, Kind); |
1545 | } |
1546 | |
1547 | /// Removes the attributes from the function |
1548 | void removeFnAttrs(const AttributeMask &AttrsToRemove) { |
1549 | Attrs = Attrs.removeFnAttributes(getContext(), AttrsToRemove); |
1550 | } |
1551 | |
1552 | /// Removes the attribute from the function |
1553 | void removeFnAttr(Attribute::AttrKind Kind) { |
1554 | Attrs = Attrs.removeFnAttribute(getContext(), Kind); |
1555 | } |
1556 | |
1557 | /// Removes the attribute from the return value |
1558 | void removeRetAttr(Attribute::AttrKind Kind) { |
1559 | Attrs = Attrs.removeRetAttribute(getContext(), Kind); |
1560 | } |
1561 | |
1562 | /// Removes the attributes from the return value |
1563 | void removeRetAttrs(const AttributeMask &AttrsToRemove) { |
1564 | Attrs = Attrs.removeRetAttributes(getContext(), AttrsToRemove); |
1565 | } |
1566 | |
1567 | /// Removes the attribute from the given argument |
1568 | void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { |
1569 | assert(ArgNo < arg_size() && "Out of bounds")(static_cast <bool> (ArgNo < arg_size() && "Out of bounds" ) ? void (0) : __assert_fail ("ArgNo < arg_size() && \"Out of bounds\"" , "llvm/include/llvm/IR/InstrTypes.h", 1569, __extension__ __PRETTY_FUNCTION__ )); |
1570 | Attrs = Attrs.removeParamAttribute(getContext(), ArgNo, Kind); |
1571 | } |
1572 | |
1573 | /// Removes the attribute from the given argument |
1574 | void removeParamAttr(unsigned ArgNo, StringRef Kind) { |
1575 | assert(ArgNo < arg_size() && "Out of bounds")(static_cast <bool> (ArgNo < arg_size() && "Out of bounds" ) ? void (0) : __assert_fail ("ArgNo < arg_size() && \"Out of bounds\"" , "llvm/include/llvm/IR/InstrTypes.h", 1575, __extension__ __PRETTY_FUNCTION__ )); |
1576 | Attrs = Attrs.removeParamAttribute(getContext(), ArgNo, Kind); |
1577 | } |
1578 | |
1579 | /// Removes the attributes from the given argument |
1580 | void removeParamAttrs(unsigned ArgNo, const AttributeMask &AttrsToRemove) { |
1581 | Attrs = Attrs.removeParamAttributes(getContext(), ArgNo, AttrsToRemove); |
1582 | } |
1583 | |
1584 | /// adds the dereferenceable attribute to the list of attributes. |
1585 | void addDereferenceableParamAttr(unsigned i, uint64_t Bytes) { |
1586 | Attrs = Attrs.addDereferenceableParamAttr(getContext(), i, Bytes); |
1587 | } |
1588 | |
1589 | /// adds the dereferenceable attribute to the list of attributes. |
1590 | void addDereferenceableRetAttr(uint64_t Bytes) { |
1591 | Attrs = Attrs.addDereferenceableRetAttr(getContext(), Bytes); |
1592 | } |
1593 | |
1594 | /// Determine whether the return value has the given attribute. |
1595 | bool hasRetAttr(Attribute::AttrKind Kind) const { |
1596 | return hasRetAttrImpl(Kind); |
1597 | } |
1598 | /// Determine whether the return value has the given attribute. |
1599 | bool hasRetAttr(StringRef Kind) const { return hasRetAttrImpl(Kind); } |
1600 | |
1601 | /// Determine whether the argument or parameter has the given attribute. |
1602 | bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const; |
1603 | |
1604 | /// Get the attribute of a given kind at a position. |
1605 | Attribute getAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) const { |
1606 | return getAttributes().getAttributeAtIndex(i, Kind); |
1607 | } |
1608 | |
1609 | /// Get the attribute of a given kind at a position. |
1610 | Attribute getAttributeAtIndex(unsigned i, StringRef Kind) const { |
1611 | return getAttributes().getAttributeAtIndex(i, Kind); |
1612 | } |
1613 | |
1614 | /// Get the attribute of a given kind for the function. |
1615 | Attribute getFnAttr(StringRef Kind) const { |
1616 | Attribute Attr = getAttributes().getFnAttr(Kind); |
1617 | if (Attr.isValid()) |
1618 | return Attr; |
1619 | return getFnAttrOnCalledFunction(Kind); |
1620 | } |
1621 | |
1622 | /// Get the attribute of a given kind for the function. |
1623 | Attribute getFnAttr(Attribute::AttrKind Kind) const { |
1624 | Attribute A = getAttributes().getFnAttr(Kind); |
1625 | if (A.isValid()) |
1626 | return A; |
1627 | return getFnAttrOnCalledFunction(Kind); |
1628 | } |
1629 | |
1630 | /// Get the attribute of a given kind from a given arg |
1631 | Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const { |
1632 | assert(ArgNo < arg_size() && "Out of bounds")(static_cast <bool> (ArgNo < arg_size() && "Out of bounds" ) ? void (0) : __assert_fail ("ArgNo < arg_size() && \"Out of bounds\"" , "llvm/include/llvm/IR/InstrTypes.h", 1632, __extension__ __PRETTY_FUNCTION__ )); |
1633 | return getAttributes().getParamAttr(ArgNo, Kind); |
1634 | } |
1635 | |
1636 | /// Get the attribute of a given kind from a given arg |
1637 | Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const { |
1638 | assert(ArgNo < arg_size() && "Out of bounds")(static_cast <bool> (ArgNo < arg_size() && "Out of bounds" ) ? void (0) : __assert_fail ("ArgNo < arg_size() && \"Out of bounds\"" , "llvm/include/llvm/IR/InstrTypes.h", 1638, __extension__ __PRETTY_FUNCTION__ )); |
1639 | return getAttributes().getParamAttr(ArgNo, Kind); |
1640 | } |
1641 | |
1642 | /// Return true if the data operand at index \p i has the attribute \p |
1643 | /// A. |
1644 | /// |
1645 | /// Data operands include call arguments and values used in operand bundles, |
1646 | /// but does not include the callee operand. |
1647 | /// |
1648 | /// The index \p i is interpreted as |
1649 | /// |
1650 | /// \p i in [0, arg_size) -> argument number (\p i) |
1651 | /// \p i in [arg_size, data_operand_size) -> bundle operand at index |
1652 | /// (\p i) in the operand list. |
1653 | bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const { |
1654 | // Note that we have to add one because `i` isn't zero-indexed. |
1655 | assert(i < arg_size() + getNumTotalBundleOperands() &&(static_cast <bool> (i < arg_size() + getNumTotalBundleOperands () && "Data operand index out of bounds!") ? void (0) : __assert_fail ("i < arg_size() + getNumTotalBundleOperands() && \"Data operand index out of bounds!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1656, __extension__ __PRETTY_FUNCTION__ )) |
1656 | "Data operand index out of bounds!")(static_cast <bool> (i < arg_size() + getNumTotalBundleOperands () && "Data operand index out of bounds!") ? void (0) : __assert_fail ("i < arg_size() + getNumTotalBundleOperands() && \"Data operand index out of bounds!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1656, __extension__ __PRETTY_FUNCTION__ )); |
1657 | |
1658 | // The attribute A can either be directly specified, if the operand in |
1659 | // question is a call argument; or be indirectly implied by the kind of its |
1660 | // containing operand bundle, if the operand is a bundle operand. |
1661 | |
1662 | if (i < arg_size()) |
1663 | return paramHasAttr(i, Kind); |
1664 | |
1665 | assert(hasOperandBundles() && i >= getBundleOperandsStartIndex() &&(static_cast <bool> (hasOperandBundles() && i >= getBundleOperandsStartIndex() && "Must be either a call argument or an operand bundle!" ) ? void (0) : __assert_fail ("hasOperandBundles() && i >= getBundleOperandsStartIndex() && \"Must be either a call argument or an operand bundle!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1666, __extension__ __PRETTY_FUNCTION__ )) |
1666 | "Must be either a call argument or an operand bundle!")(static_cast <bool> (hasOperandBundles() && i >= getBundleOperandsStartIndex() && "Must be either a call argument or an operand bundle!" ) ? void (0) : __assert_fail ("hasOperandBundles() && i >= getBundleOperandsStartIndex() && \"Must be either a call argument or an operand bundle!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1666, __extension__ __PRETTY_FUNCTION__ )); |
1667 | return bundleOperandHasAttr(i, Kind); |
1668 | } |
1669 | |
1670 | /// Determine whether this data operand is not captured. |
1671 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1672 | // better indicate that this may return a conservative answer. |
1673 | bool doesNotCapture(unsigned OpNo) const { |
1674 | return dataOperandHasImpliedAttr(OpNo, Attribute::NoCapture); |
1675 | } |
1676 | |
1677 | /// Determine whether this argument is passed by value. |
1678 | bool isByValArgument(unsigned ArgNo) const { |
1679 | return paramHasAttr(ArgNo, Attribute::ByVal); |
1680 | } |
1681 | |
1682 | /// Determine whether this argument is passed in an alloca. |
1683 | bool isInAllocaArgument(unsigned ArgNo) const { |
1684 | return paramHasAttr(ArgNo, Attribute::InAlloca); |
1685 | } |
1686 | |
1687 | /// Determine whether this argument is passed by value, in an alloca, or is |
1688 | /// preallocated. |
1689 | bool isPassPointeeByValueArgument(unsigned ArgNo) const { |
1690 | return paramHasAttr(ArgNo, Attribute::ByVal) || |
1691 | paramHasAttr(ArgNo, Attribute::InAlloca) || |
1692 | paramHasAttr(ArgNo, Attribute::Preallocated); |
1693 | } |
1694 | |
1695 | /// Determine whether passing undef to this argument is undefined behavior. |
1696 | /// If passing undef to this argument is UB, passing poison is UB as well |
1697 | /// because poison is more undefined than undef. |
1698 | bool isPassingUndefUB(unsigned ArgNo) const { |
1699 | return paramHasAttr(ArgNo, Attribute::NoUndef) || |
1700 | // dereferenceable implies noundef. |
1701 | paramHasAttr(ArgNo, Attribute::Dereferenceable) || |
1702 | // dereferenceable implies noundef, and null is a well-defined value. |
1703 | paramHasAttr(ArgNo, Attribute::DereferenceableOrNull); |
1704 | } |
1705 | |
1706 | /// Determine if there are is an inalloca argument. Only the last argument can |
1707 | /// have the inalloca attribute. |
1708 | bool hasInAllocaArgument() const { |
1709 | return !arg_empty() && paramHasAttr(arg_size() - 1, Attribute::InAlloca); |
1710 | } |
1711 | |
1712 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1713 | // better indicate that this may return a conservative answer. |
1714 | bool doesNotAccessMemory(unsigned OpNo) const { |
1715 | return dataOperandHasImpliedAttr(OpNo, Attribute::ReadNone); |
1716 | } |
1717 | |
1718 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1719 | // better indicate that this may return a conservative answer. |
1720 | bool onlyReadsMemory(unsigned OpNo) const { |
1721 | return dataOperandHasImpliedAttr(OpNo, Attribute::ReadOnly) || |
1722 | dataOperandHasImpliedAttr(OpNo, Attribute::ReadNone); |
1723 | } |
1724 | |
1725 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1726 | // better indicate that this may return a conservative answer. |
1727 | bool onlyWritesMemory(unsigned OpNo) const { |
1728 | return dataOperandHasImpliedAttr(OpNo, Attribute::WriteOnly) || |
1729 | dataOperandHasImpliedAttr(OpNo, Attribute::ReadNone); |
1730 | } |
1731 | |
1732 | /// Extract the alignment of the return value. |
1733 | MaybeAlign getRetAlign() const { |
1734 | if (auto Align = Attrs.getRetAlignment()) |
1735 | return Align; |
1736 | if (const Function *F = getCalledFunction()) |
1737 | return F->getAttributes().getRetAlignment(); |
1738 | return None; |
1739 | } |
1740 | |
1741 | /// Extract the alignment for a call or parameter (0=unknown). |
1742 | MaybeAlign getParamAlign(unsigned ArgNo) const { |
1743 | return Attrs.getParamAlignment(ArgNo); |
1744 | } |
1745 | |
1746 | MaybeAlign getParamStackAlign(unsigned ArgNo) const { |
1747 | return Attrs.getParamStackAlignment(ArgNo); |
1748 | } |
1749 | |
1750 | /// Extract the byval type for a call or parameter. |
1751 | Type *getParamByValType(unsigned ArgNo) const { |
1752 | if (auto *Ty = Attrs.getParamByValType(ArgNo)) |
1753 | return Ty; |
1754 | if (const Function *F = getCalledFunction()) |
1755 | return F->getAttributes().getParamByValType(ArgNo); |
1756 | return nullptr; |
1757 | } |
1758 | |
1759 | /// Extract the preallocated type for a call or parameter. |
1760 | Type *getParamPreallocatedType(unsigned ArgNo) const { |
1761 | if (auto *Ty = Attrs.getParamPreallocatedType(ArgNo)) |
1762 | return Ty; |
1763 | if (const Function *F = getCalledFunction()) |
1764 | return F->getAttributes().getParamPreallocatedType(ArgNo); |
1765 | return nullptr; |
1766 | } |
1767 | |
1768 | /// Extract the inalloca type for a call or parameter. |
1769 | Type *getParamInAllocaType(unsigned ArgNo) const { |
1770 | if (auto *Ty = Attrs.getParamInAllocaType(ArgNo)) |
1771 | return Ty; |
1772 | if (const Function *F = getCalledFunction()) |
1773 | return F->getAttributes().getParamInAllocaType(ArgNo); |
1774 | return nullptr; |
1775 | } |
1776 | |
1777 | /// Extract the sret type for a call or parameter. |
1778 | Type *getParamStructRetType(unsigned ArgNo) const { |
1779 | if (auto *Ty = Attrs.getParamStructRetType(ArgNo)) |
1780 | return Ty; |
1781 | if (const Function *F = getCalledFunction()) |
1782 | return F->getAttributes().getParamStructRetType(ArgNo); |
1783 | return nullptr; |
1784 | } |
1785 | |
1786 | /// Extract the elementtype type for a parameter. |
1787 | /// Note that elementtype() can only be applied to call arguments, not |
1788 | /// function declaration parameters. |
1789 | Type *getParamElementType(unsigned ArgNo) const { |
1790 | return Attrs.getParamElementType(ArgNo); |
1791 | } |
1792 | |
1793 | /// Extract the number of dereferenceable bytes for a call or |
1794 | /// parameter (0=unknown). |
1795 | uint64_t getRetDereferenceableBytes() const { |
1796 | return Attrs.getRetDereferenceableBytes(); |
1797 | } |
1798 | |
1799 | /// Extract the number of dereferenceable bytes for a call or |
1800 | /// parameter (0=unknown). |
1801 | uint64_t getParamDereferenceableBytes(unsigned i) const { |
1802 | return Attrs.getParamDereferenceableBytes(i); |
1803 | } |
1804 | |
1805 | /// Extract the number of dereferenceable_or_null bytes for a call |
1806 | /// (0=unknown). |
1807 | uint64_t getRetDereferenceableOrNullBytes() const { |
1808 | return Attrs.getRetDereferenceableOrNullBytes(); |
1809 | } |
1810 | |
1811 | /// Extract the number of dereferenceable_or_null bytes for a |
1812 | /// parameter (0=unknown). |
1813 | uint64_t getParamDereferenceableOrNullBytes(unsigned i) const { |
1814 | return Attrs.getParamDereferenceableOrNullBytes(i); |
1815 | } |
1816 | |
1817 | /// Return true if the return value is known to be not null. |
1818 | /// This may be because it has the nonnull attribute, or because at least |
1819 | /// one byte is dereferenceable and the pointer is in addrspace(0). |
1820 | bool isReturnNonNull() const; |
1821 | |
1822 | /// Determine if the return value is marked with NoAlias attribute. |
1823 | bool returnDoesNotAlias() const { |
1824 | return Attrs.hasRetAttr(Attribute::NoAlias); |
1825 | } |
1826 | |
1827 | /// If one of the arguments has the 'returned' attribute, returns its |
1828 | /// operand value. Otherwise, return nullptr. |
1829 | Value *getReturnedArgOperand() const { |
1830 | return getArgOperandWithAttribute(Attribute::Returned); |
1831 | } |
1832 | |
1833 | /// If one of the arguments has the specified attribute, returns its |
1834 | /// operand value. Otherwise, return nullptr. |
1835 | Value *getArgOperandWithAttribute(Attribute::AttrKind Kind) const; |
1836 | |
1837 | /// Return true if the call should not be treated as a call to a |
1838 | /// builtin. |
1839 | bool isNoBuiltin() const { |
1840 | return hasFnAttrImpl(Attribute::NoBuiltin) && |
1841 | !hasFnAttrImpl(Attribute::Builtin); |
1842 | } |
1843 | |
1844 | /// Determine if the call requires strict floating point semantics. |
1845 | bool isStrictFP() const { return hasFnAttr(Attribute::StrictFP); } |
1846 | |
1847 | /// Return true if the call should not be inlined. |
1848 | bool isNoInline() const { return hasFnAttr(Attribute::NoInline); } |
1849 | void setIsNoInline() { addFnAttr(Attribute::NoInline); } |
1850 | /// Determine if the call does not access memory. |
1851 | bool doesNotAccessMemory() const { return hasFnAttr(Attribute::ReadNone); } |
1852 | void setDoesNotAccessMemory() { addFnAttr(Attribute::ReadNone); } |
1853 | |
1854 | /// Determine if the call does not access or only reads memory. |
1855 | bool onlyReadsMemory() const { |
1856 | return hasImpliedFnAttr(Attribute::ReadOnly); |
1857 | } |
1858 | |
1859 | void setOnlyReadsMemory() { addFnAttr(Attribute::ReadOnly); } |
1860 | |
1861 | /// Determine if the call does not access or only writes memory. |
1862 | bool onlyWritesMemory() const { |
1863 | return hasImpliedFnAttr(Attribute::WriteOnly); |
1864 | } |
1865 | void setOnlyWritesMemory() { addFnAttr(Attribute::WriteOnly); } |
1866 | |
1867 | /// Determine if the call can access memmory only using pointers based |
1868 | /// on its arguments. |
1869 | bool onlyAccessesArgMemory() const { |
1870 | return hasFnAttr(Attribute::ArgMemOnly); |
1871 | } |
1872 | void setOnlyAccessesArgMemory() { addFnAttr(Attribute::ArgMemOnly); } |
1873 | |
1874 | /// Determine if the function may only access memory that is |
1875 | /// inaccessible from the IR. |
1876 | bool onlyAccessesInaccessibleMemory() const { |
1877 | return hasFnAttr(Attribute::InaccessibleMemOnly); |
1878 | } |
1879 | void setOnlyAccessesInaccessibleMemory() { |
1880 | addFnAttr(Attribute::InaccessibleMemOnly); |
1881 | } |
1882 | |
1883 | /// Determine if the function may only access memory that is |
1884 | /// either inaccessible from the IR or pointed to by its arguments. |
1885 | bool onlyAccessesInaccessibleMemOrArgMem() const { |
1886 | return hasFnAttr(Attribute::InaccessibleMemOrArgMemOnly); |
1887 | } |
1888 | void setOnlyAccessesInaccessibleMemOrArgMem() { |
1889 | addFnAttr(Attribute::InaccessibleMemOrArgMemOnly); |
1890 | } |
1891 | /// Determine if the call cannot return. |
1892 | bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); } |
1893 | void setDoesNotReturn() { addFnAttr(Attribute::NoReturn); } |
1894 | |
1895 | /// Determine if the call should not perform indirect branch tracking. |
1896 | bool doesNoCfCheck() const { return hasFnAttr(Attribute::NoCfCheck); } |
1897 | |
1898 | /// Determine if the call cannot unwind. |
1899 | bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); } |
1900 | void setDoesNotThrow() { addFnAttr(Attribute::NoUnwind); } |
1901 | |
1902 | /// Determine if the invoke cannot be duplicated. |
1903 | bool cannotDuplicate() const { return hasFnAttr(Attribute::NoDuplicate); } |
1904 | void setCannotDuplicate() { addFnAttr(Attribute::NoDuplicate); } |
1905 | |
1906 | /// Determine if the call cannot be tail merged. |
1907 | bool cannotMerge() const { return hasFnAttr(Attribute::NoMerge); } |
1908 | void setCannotMerge() { addFnAttr(Attribute::NoMerge); } |
1909 | |
1910 | /// Determine if the invoke is convergent |
1911 | bool isConvergent() const { return hasFnAttr(Attribute::Convergent); } |
1912 | void setConvergent() { addFnAttr(Attribute::Convergent); } |
1913 | void setNotConvergent() { removeFnAttr(Attribute::Convergent); } |
1914 | |
1915 | /// Determine if the call returns a structure through first |
1916 | /// pointer argument. |
1917 | bool hasStructRetAttr() const { |
1918 | if (arg_empty()) |
1919 | return false; |
1920 | |
1921 | // Be friendly and also check the callee. |
1922 | return paramHasAttr(0, Attribute::StructRet); |
1923 | } |
1924 | |
1925 | /// Determine if any call argument is an aggregate passed by value. |
1926 | bool hasByValArgument() const { |
1927 | return Attrs.hasAttrSomewhere(Attribute::ByVal); |
1928 | } |
1929 | |
1930 | ///@{ |
1931 | // End of attribute API. |
1932 | |
1933 | /// \name Operand Bundle API |
1934 | /// |
1935 | /// This group of methods provides the API to access and manipulate operand |
1936 | /// bundles on this call. |
1937 | /// @{ |
1938 | |
1939 | /// Return the number of operand bundles associated with this User. |
1940 | unsigned getNumOperandBundles() const { |
1941 | return std::distance(bundle_op_info_begin(), bundle_op_info_end()); |
1942 | } |
1943 | |
1944 | /// Return true if this User has any operand bundles. |
1945 | bool hasOperandBundles() const { return getNumOperandBundles() != 0; } |
1946 | |
1947 | /// Return the index of the first bundle operand in the Use array. |
1948 | unsigned getBundleOperandsStartIndex() const { |
1949 | assert(hasOperandBundles() && "Don't call otherwise!")(static_cast <bool> (hasOperandBundles() && "Don't call otherwise!" ) ? void (0) : __assert_fail ("hasOperandBundles() && \"Don't call otherwise!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1949, __extension__ __PRETTY_FUNCTION__ )); |
1950 | return bundle_op_info_begin()->Begin; |
1951 | } |
1952 | |
1953 | /// Return the index of the last bundle operand in the Use array. |
1954 | unsigned getBundleOperandsEndIndex() const { |
1955 | assert(hasOperandBundles() && "Don't call otherwise!")(static_cast <bool> (hasOperandBundles() && "Don't call otherwise!" ) ? void (0) : __assert_fail ("hasOperandBundles() && \"Don't call otherwise!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1955, __extension__ __PRETTY_FUNCTION__ )); |
1956 | return bundle_op_info_end()[-1].End; |
1957 | } |
1958 | |
1959 | /// Return true if the operand at index \p Idx is a bundle operand. |
1960 | bool isBundleOperand(unsigned Idx) const { |
1961 | return hasOperandBundles() && Idx >= getBundleOperandsStartIndex() && |
1962 | Idx < getBundleOperandsEndIndex(); |
1963 | } |
1964 | |
1965 | /// Return true if the operand at index \p Idx is a bundle operand that has |
1966 | /// tag ID \p ID. |
1967 | bool isOperandBundleOfType(uint32_t ID, unsigned Idx) const { |
1968 | return isBundleOperand(Idx) && |
1969 | getOperandBundleForOperand(Idx).getTagID() == ID; |
1970 | } |
1971 | |
1972 | /// Returns true if the use is a bundle operand. |
1973 | bool isBundleOperand(const Use *U) const { |
1974 | assert(this == U->getUser() &&(static_cast <bool> (this == U->getUser() && "Only valid to query with a use of this instruction!") ? void (0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1975, __extension__ __PRETTY_FUNCTION__ )) |
1975 | "Only valid to query with a use of this instruction!")(static_cast <bool> (this == U->getUser() && "Only valid to query with a use of this instruction!") ? void (0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1975, __extension__ __PRETTY_FUNCTION__ )); |
1976 | return hasOperandBundles() && isBundleOperand(U - op_begin()); |
1977 | } |
1978 | bool isBundleOperand(Value::const_user_iterator UI) const { |
1979 | return isBundleOperand(&UI.getUse()); |
1980 | } |
1981 | |
1982 | /// Return the total number operands (not operand bundles) used by |
1983 | /// every operand bundle in this OperandBundleUser. |
1984 | unsigned getNumTotalBundleOperands() const { |
1985 | if (!hasOperandBundles()) |
1986 | return 0; |
1987 | |
1988 | unsigned Begin = getBundleOperandsStartIndex(); |
1989 | unsigned End = getBundleOperandsEndIndex(); |
1990 | |
1991 | assert(Begin <= End && "Should be!")(static_cast <bool> (Begin <= End && "Should be!" ) ? void (0) : __assert_fail ("Begin <= End && \"Should be!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1991, __extension__ __PRETTY_FUNCTION__ )); |
1992 | return End - Begin; |
1993 | } |
1994 | |
1995 | /// Return the operand bundle at a specific index. |
1996 | OperandBundleUse getOperandBundleAt(unsigned Index) const { |
1997 | assert(Index < getNumOperandBundles() && "Index out of bounds!")(static_cast <bool> (Index < getNumOperandBundles() && "Index out of bounds!") ? void (0) : __assert_fail ("Index < getNumOperandBundles() && \"Index out of bounds!\"" , "llvm/include/llvm/IR/InstrTypes.h", 1997, __extension__ __PRETTY_FUNCTION__ )); |
1998 | return operandBundleFromBundleOpInfo(*(bundle_op_info_begin() + Index)); |
1999 | } |
2000 | |
2001 | /// Return the number of operand bundles with the tag Name attached to |
2002 | /// this instruction. |
2003 | unsigned countOperandBundlesOfType(StringRef Name) const { |
2004 | unsigned Count = 0; |
2005 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) |
2006 | if (getOperandBundleAt(i).getTagName() == Name) |
2007 | Count++; |
2008 | |
2009 | return Count; |
2010 | } |
2011 | |
2012 | /// Return the number of operand bundles with the tag ID attached to |
2013 | /// this instruction. |
2014 | unsigned countOperandBundlesOfType(uint32_t ID) const { |
2015 | unsigned Count = 0; |
2016 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) |
2017 | if (getOperandBundleAt(i).getTagID() == ID) |
2018 | Count++; |
2019 | |
2020 | return Count; |
2021 | } |
2022 | |
2023 | /// Return an operand bundle by name, if present. |
2024 | /// |
2025 | /// It is an error to call this for operand bundle types that may have |
2026 | /// multiple instances of them on the same instruction. |
2027 | Optional<OperandBundleUse> getOperandBundle(StringRef Name) const { |
2028 | assert(countOperandBundlesOfType(Name) < 2 && "Precondition violated!")(static_cast <bool> (countOperandBundlesOfType(Name) < 2 && "Precondition violated!") ? void (0) : __assert_fail ("countOperandBundlesOfType(Name) < 2 && \"Precondition violated!\"" , "llvm/include/llvm/IR/InstrTypes.h", 2028, __extension__ __PRETTY_FUNCTION__ )); |
2029 | |
2030 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) { |
2031 | OperandBundleUse U = getOperandBundleAt(i); |
2032 | if (U.getTagName() == Name) |
2033 | return U; |
2034 | } |
2035 | |
2036 | return None; |
2037 | } |
2038 | |
2039 | /// Return an operand bundle by tag ID, if present. |
2040 | /// |
2041 | /// It is an error to call this for operand bundle types that may have |
2042 | /// multiple instances of them on the same instruction. |
2043 | Optional<OperandBundleUse> getOperandBundle(uint32_t ID) const { |
2044 | assert(countOperandBundlesOfType(ID) < 2 && "Precondition violated!")(static_cast <bool> (countOperandBundlesOfType(ID) < 2 && "Precondition violated!") ? void (0) : __assert_fail ("countOperandBundlesOfType(ID) < 2 && \"Precondition violated!\"" , "llvm/include/llvm/IR/InstrTypes.h", 2044, __extension__ __PRETTY_FUNCTION__ )); |
2045 | |
2046 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) { |
2047 | OperandBundleUse U = getOperandBundleAt(i); |
2048 | if (U.getTagID() == ID) |
2049 | return U; |
2050 | } |
2051 | |
2052 | return None; |
2053 | } |
2054 | |
2055 | /// Return the list of operand bundles attached to this instruction as |
2056 | /// a vector of OperandBundleDefs. |
2057 | /// |
2058 | /// This function copies the OperandBundeUse instances associated with this |
2059 | /// OperandBundleUser to a vector of OperandBundleDefs. Note: |
2060 | /// OperandBundeUses and OperandBundleDefs are non-trivially *different* |
2061 | /// representations of operand bundles (see documentation above). |
2062 | void getOperandBundlesAsDefs(SmallVectorImpl<OperandBundleDef> &Defs) const; |
2063 | |
2064 | /// Return the operand bundle for the operand at index OpIdx. |
2065 | /// |
2066 | /// It is an error to call this with an OpIdx that does not correspond to an |
2067 | /// bundle operand. |
2068 | OperandBundleUse getOperandBundleForOperand(unsigned OpIdx) const { |
2069 | return operandBundleFromBundleOpInfo(getBundleOpInfoForOperand(OpIdx)); |
2070 | } |
2071 | |
2072 | /// Return true if this operand bundle user has operand bundles that |
2073 | /// may read from the heap. |
2074 | bool hasReadingOperandBundles() const; |
2075 | |
2076 | /// Return true if this operand bundle user has operand bundles that |
2077 | /// may write to the heap. |
2078 | bool hasClobberingOperandBundles() const { |
2079 | for (auto &BOI : bundle_op_infos()) { |
2080 | if (BOI.Tag->second == LLVMContext::OB_deopt || |
2081 | BOI.Tag->second == LLVMContext::OB_funclet || |
2082 | BOI.Tag->second == LLVMContext::OB_ptrauth) |
2083 | continue; |
2084 | |
2085 | // This instruction has an operand bundle that is not known to us. |
2086 | // Assume the worst. |
2087 | return true; |
2088 | } |
2089 | |
2090 | return false; |
2091 | } |
2092 | |
2093 | /// Return true if the bundle operand at index \p OpIdx has the |
2094 | /// attribute \p A. |
2095 | bool bundleOperandHasAttr(unsigned OpIdx, Attribute::AttrKind A) const { |
2096 | auto &BOI = getBundleOpInfoForOperand(OpIdx); |
2097 | auto OBU = operandBundleFromBundleOpInfo(BOI); |
2098 | return OBU.operandHasAttr(OpIdx - BOI.Begin, A); |
2099 | } |
2100 | |
2101 | /// Return true if \p Other has the same sequence of operand bundle |
2102 | /// tags with the same number of operands on each one of them as this |
2103 | /// OperandBundleUser. |
2104 | bool hasIdenticalOperandBundleSchema(const CallBase &Other) const { |
2105 | if (getNumOperandBundles() != Other.getNumOperandBundles()) |
2106 | return false; |
2107 | |
2108 | return std::equal(bundle_op_info_begin(), bundle_op_info_end(), |
2109 | Other.bundle_op_info_begin()); |
2110 | } |
2111 | |
2112 | /// Return true if this operand bundle user contains operand bundles |
2113 | /// with tags other than those specified in \p IDs. |
2114 | bool hasOperandBundlesOtherThan(ArrayRef<uint32_t> IDs) const { |
2115 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) { |
2116 | uint32_t ID = getOperandBundleAt(i).getTagID(); |
2117 | if (!is_contained(IDs, ID)) |
2118 | return true; |
2119 | } |
2120 | return false; |
2121 | } |
2122 | |
2123 | /// Is the function attribute S disallowed by some operand bundle on |
2124 | /// this operand bundle user? |
2125 | bool isFnAttrDisallowedByOpBundle(StringRef S) const { |
2126 | // Operand bundles only possibly disallow memory access attributes. All |
2127 | // String attributes are fine. |
2128 | return false; |
2129 | } |
2130 | |
2131 | /// Is the function attribute A disallowed by some operand bundle on |
2132 | /// this operand bundle user? |
2133 | bool isFnAttrDisallowedByOpBundle(Attribute::AttrKind A) const { |
2134 | switch (A) { |
2135 | default: |
2136 | return false; |
2137 | |
2138 | case Attribute::InaccessibleMemOrArgMemOnly: |
2139 | return hasReadingOperandBundles(); |
2140 | |
2141 | case Attribute::InaccessibleMemOnly: |
2142 | return hasReadingOperandBundles(); |
2143 | |
2144 | case Attribute::ArgMemOnly: |
2145 | return hasReadingOperandBundles(); |
2146 | |
2147 | case Attribute::ReadNone: |
2148 | return hasReadingOperandBundles(); |
2149 | |
2150 | case Attribute::ReadOnly: |
2151 | return hasClobberingOperandBundles(); |
2152 | |
2153 | case Attribute::WriteOnly: |
2154 | return hasReadingOperandBundles(); |
2155 | } |
2156 | |
2157 | llvm_unreachable("switch has a default case!")::llvm::llvm_unreachable_internal("switch has a default case!" , "llvm/include/llvm/IR/InstrTypes.h", 2157); |
2158 | } |
2159 | |
2160 | /// Used to keep track of an operand bundle. See the main comment on |
2161 | /// OperandBundleUser above. |
2162 | struct BundleOpInfo { |
2163 | /// The operand bundle tag, interned by |
2164 | /// LLVMContextImpl::getOrInsertBundleTag. |
2165 | StringMapEntry<uint32_t> *Tag; |
2166 | |
2167 | /// The index in the Use& vector where operands for this operand |
2168 | /// bundle starts. |
2169 | uint32_t Begin; |
2170 | |
2171 | /// The index in the Use& vector where operands for this operand |
2172 | /// bundle ends. |
2173 | uint32_t End; |
2174 | |
2175 | bool operator==(const BundleOpInfo &Other) const { |
2176 | return Tag == Other.Tag && Begin == Other.Begin && End == Other.End; |
2177 | } |
2178 | }; |
2179 | |
2180 | /// Simple helper function to map a BundleOpInfo to an |
2181 | /// OperandBundleUse. |
2182 | OperandBundleUse |
2183 | operandBundleFromBundleOpInfo(const BundleOpInfo &BOI) const { |
2184 | auto begin = op_begin(); |
2185 | ArrayRef<Use> Inputs(begin + BOI.Begin, begin + BOI.End); |
2186 | return OperandBundleUse(BOI.Tag, Inputs); |
2187 | } |
2188 | |
2189 | using bundle_op_iterator = BundleOpInfo *; |
2190 | using const_bundle_op_iterator = const BundleOpInfo *; |
2191 | |
2192 | /// Return the start of the list of BundleOpInfo instances associated |
2193 | /// with this OperandBundleUser. |
2194 | /// |
2195 | /// OperandBundleUser uses the descriptor area co-allocated with the host User |
2196 | /// to store some meta information about which operands are "normal" operands, |
2197 | /// and which ones belong to some operand bundle. |
2198 | /// |
2199 | /// The layout of an operand bundle user is |
2200 | /// |
2201 | /// +-----------uint32_t End-------------------------------------+ |
2202 | /// | | |
2203 | /// | +--------uint32_t Begin--------------------+ | |
2204 | /// | | | | |
2205 | /// ^ ^ v v |
2206 | /// |------|------|----|----|----|----|----|---------|----|---------|----|----- |
2207 | /// | BOI0 | BOI1 | .. | DU | U0 | U1 | .. | BOI0_U0 | .. | BOI1_U0 | .. | Un |
2208 | /// |------|------|----|----|----|----|----|---------|----|---------|----|----- |
2209 | /// v v ^ ^ |
2210 | /// | | | | |
2211 | /// | +--------uint32_t Begin------------+ | |
2212 | /// | | |
2213 | /// +-----------uint32_t End-----------------------------+ |
2214 | /// |
2215 | /// |
2216 | /// BOI0, BOI1 ... are descriptions of operand bundles in this User's use |
2217 | /// list. These descriptions are installed and managed by this class, and |
2218 | /// they're all instances of OperandBundleUser<T>::BundleOpInfo. |
2219 | /// |
2220 | /// DU is an additional descriptor installed by User's 'operator new' to keep |
2221 | /// track of the 'BOI0 ... BOIN' co-allocation. OperandBundleUser does not |
2222 | /// access or modify DU in any way, it's an implementation detail private to |
2223 | /// User. |
2224 | /// |
2225 | /// The regular Use& vector for the User starts at U0. The operand bundle |
2226 | /// uses are part of the Use& vector, just like normal uses. In the diagram |
2227 | /// above, the operand bundle uses start at BOI0_U0. Each instance of |
2228 | /// BundleOpInfo has information about a contiguous set of uses constituting |
2229 | /// an operand bundle, and the total set of operand bundle uses themselves |
2230 | /// form a contiguous set of uses (i.e. there are no gaps between uses |
2231 | /// corresponding to individual operand bundles). |
2232 | /// |
2233 | /// This class does not know the location of the set of operand bundle uses |
2234 | /// within the use list -- that is decided by the User using this class via |
2235 | /// the BeginIdx argument in populateBundleOperandInfos. |
2236 | /// |
2237 | /// Currently operand bundle users with hung-off operands are not supported. |
2238 | bundle_op_iterator bundle_op_info_begin() { |
2239 | if (!hasDescriptor()) |
2240 | return nullptr; |
2241 | |
2242 | uint8_t *BytesBegin = getDescriptor().begin(); |
2243 | return reinterpret_cast<bundle_op_iterator>(BytesBegin); |
2244 | } |
2245 | |
2246 | /// Return the start of the list of BundleOpInfo instances associated |
2247 | /// with this OperandBundleUser. |
2248 | const_bundle_op_iterator bundle_op_info_begin() const { |
2249 | auto *NonConstThis = const_cast<CallBase *>(this); |
2250 | return NonConstThis->bundle_op_info_begin(); |
2251 | } |
2252 | |
2253 | /// Return the end of the list of BundleOpInfo instances associated |
2254 | /// with this OperandBundleUser. |
2255 | bundle_op_iterator bundle_op_info_end() { |
2256 | if (!hasDescriptor()) |
2257 | return nullptr; |
2258 | |
2259 | uint8_t *BytesEnd = getDescriptor().end(); |
2260 | return reinterpret_cast<bundle_op_iterator>(BytesEnd); |
2261 | } |
2262 | |
2263 | /// Return the end of the list of BundleOpInfo instances associated |
2264 | /// with this OperandBundleUser. |
2265 | const_bundle_op_iterator bundle_op_info_end() const { |
2266 | auto *NonConstThis = const_cast<CallBase *>(this); |
2267 | return NonConstThis->bundle_op_info_end(); |
2268 | } |
2269 | |
2270 | /// Return the range [\p bundle_op_info_begin, \p bundle_op_info_end). |
2271 | iterator_range<bundle_op_iterator> bundle_op_infos() { |
2272 | return make_range(bundle_op_info_begin(), bundle_op_info_end()); |
2273 | } |
2274 | |
2275 | /// Return the range [\p bundle_op_info_begin, \p bundle_op_info_end). |
2276 | iterator_range<const_bundle_op_iterator> bundle_op_infos() const { |
2277 | return make_range(bundle_op_info_begin(), bundle_op_info_end()); |
2278 | } |
2279 | |
2280 | /// Populate the BundleOpInfo instances and the Use& vector from \p |
2281 | /// Bundles. Return the op_iterator pointing to the Use& one past the last |
2282 | /// last bundle operand use. |
2283 | /// |
2284 | /// Each \p OperandBundleDef instance is tracked by a OperandBundleInfo |
2285 | /// instance allocated in this User's descriptor. |
2286 | op_iterator populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles, |
2287 | const unsigned BeginIndex); |
2288 | |
2289 | public: |
2290 | /// Return the BundleOpInfo for the operand at index OpIdx. |
2291 | /// |
2292 | /// It is an error to call this with an OpIdx that does not correspond to an |
2293 | /// bundle operand. |
2294 | BundleOpInfo &getBundleOpInfoForOperand(unsigned OpIdx); |
2295 | const BundleOpInfo &getBundleOpInfoForOperand(unsigned OpIdx) const { |
2296 | return const_cast<CallBase *>(this)->getBundleOpInfoForOperand(OpIdx); |
2297 | } |
2298 | |
2299 | protected: |
2300 | /// Return the total number of values used in \p Bundles. |
2301 | static unsigned CountBundleInputs(ArrayRef<OperandBundleDef> Bundles) { |
2302 | unsigned Total = 0; |
2303 | for (auto &B : Bundles) |
2304 | Total += B.input_size(); |
2305 | return Total; |
2306 | } |
2307 | |
2308 | /// @} |
2309 | // End of operand bundle API. |
2310 | |
2311 | private: |
2312 | bool hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const; |
2313 | bool hasFnAttrOnCalledFunction(StringRef Kind) const; |
2314 | |
2315 | template <typename AttrKind> bool hasFnAttrImpl(AttrKind Kind) const { |
2316 | if (Attrs.hasFnAttr(Kind)) |
2317 | return true; |
2318 | |
2319 | // Operand bundles override attributes on the called function, but don't |
2320 | // override attributes directly present on the call instruction. |
2321 | if (isFnAttrDisallowedByOpBundle(Kind)) |
2322 | return false; |
2323 | |
2324 | return hasFnAttrOnCalledFunction(Kind); |
2325 | } |
2326 | template <typename AK> Attribute getFnAttrOnCalledFunction(AK Kind) const; |
2327 | |
2328 | /// A specialized version of hasFnAttrImpl for when the caller wants to |
2329 | /// know if an attribute's semantics are implied, not whether the attribute |
2330 | /// is actually present. This distinction only exists when checking whether |
2331 | /// something is readonly or writeonly since readnone implies both. The case |
2332 | /// which motivates the specialized code is a callee with readnone, and an |
2333 | /// operand bundle on the call which disallows readnone but not either |
2334 | /// readonly or writeonly. |
2335 | bool hasImpliedFnAttr(Attribute::AttrKind Kind) const { |
2336 | assert((Kind == Attribute::ReadOnly || Kind == Attribute::WriteOnly) &&(static_cast <bool> ((Kind == Attribute::ReadOnly || Kind == Attribute::WriteOnly) && "use hasFnAttrImpl instead" ) ? void (0) : __assert_fail ("(Kind == Attribute::ReadOnly || Kind == Attribute::WriteOnly) && \"use hasFnAttrImpl instead\"" , "llvm/include/llvm/IR/InstrTypes.h", 2337, __extension__ __PRETTY_FUNCTION__ )) |
2337 | "use hasFnAttrImpl instead")(static_cast <bool> ((Kind == Attribute::ReadOnly || Kind == Attribute::WriteOnly) && "use hasFnAttrImpl instead" ) ? void (0) : __assert_fail ("(Kind == Attribute::ReadOnly || Kind == Attribute::WriteOnly) && \"use hasFnAttrImpl instead\"" , "llvm/include/llvm/IR/InstrTypes.h", 2337, __extension__ __PRETTY_FUNCTION__ )); |
2338 | if (Attrs.hasFnAttr(Kind) || Attrs.hasFnAttr(Attribute::ReadNone)) |
2339 | return true; |
2340 | |
2341 | if (isFnAttrDisallowedByOpBundle(Kind)) |
2342 | return false; |
2343 | |
2344 | return hasFnAttrOnCalledFunction(Kind) || |
2345 | hasFnAttrOnCalledFunction(Attribute::ReadNone); |
2346 | } |
2347 | |
2348 | /// Determine whether the return value has the given attribute. Supports |
2349 | /// Attribute::AttrKind and StringRef as \p AttrKind types. |
2350 | template <typename AttrKind> bool hasRetAttrImpl(AttrKind Kind) const { |
2351 | if (Attrs.hasRetAttr(Kind)) |
2352 | return true; |
2353 | |
2354 | // Look at the callee, if available. |
2355 | if (const Function *F = getCalledFunction()) |
2356 | return F->getAttributes().hasRetAttr(Kind); |
2357 | return false; |
2358 | } |
2359 | }; |
2360 | |
2361 | template <> |
2362 | struct OperandTraits<CallBase> : public VariadicOperandTraits<CallBase, 1> {}; |
2363 | |
2364 | 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 { (static_cast <bool> (i_nocapture < OperandTraits<CallBase>::operands(this) && "getOperand() out of range!" ) ? void (0) : __assert_fail ("i_nocapture < OperandTraits<CallBase>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/InstrTypes.h", 2364, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<CallBase>::operands(this) && "setOperand() out of range!" ) ? void (0) : __assert_fail ("i_nocapture < OperandTraits<CallBase>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/InstrTypes.h", 2364, __extension__ __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); } |
2365 | |
2366 | //===----------------------------------------------------------------------===// |
2367 | // FuncletPadInst Class |
2368 | //===----------------------------------------------------------------------===// |
2369 | class FuncletPadInst : public Instruction { |
2370 | private: |
2371 | FuncletPadInst(const FuncletPadInst &CPI); |
2372 | |
2373 | explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
2374 | ArrayRef<Value *> Args, unsigned Values, |
2375 | const Twine &NameStr, Instruction *InsertBefore); |
2376 | explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
2377 | ArrayRef<Value *> Args, unsigned Values, |
2378 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
2379 | |
2380 | void init(Value *ParentPad, ArrayRef<Value *> Args, const Twine &NameStr); |
2381 | |
2382 | protected: |
2383 | // Note: Instruction needs to be a friend here to call cloneImpl. |
2384 | friend class Instruction; |
2385 | friend class CatchPadInst; |
2386 | friend class CleanupPadInst; |
2387 | |
2388 | FuncletPadInst *cloneImpl() const; |
2389 | |
2390 | public: |
2391 | /// Provide fast operand accessors |
2392 | 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; |
2393 | |
2394 | /// getNumArgOperands - Return the number of funcletpad arguments. |
2395 | /// |
2396 | unsigned getNumArgOperands() const { return getNumOperands() - 1; } |
2397 | |
2398 | /// Convenience accessors |
2399 | |
2400 | /// Return the outer EH-pad this funclet is nested within. |
2401 | /// |
2402 | /// Note: This returns the associated CatchSwitchInst if this FuncletPadInst |
2403 | /// is a CatchPadInst. |
2404 | Value *getParentPad() const { return Op<-1>(); } |
2405 | void setParentPad(Value *ParentPad) { |
2406 | assert(ParentPad)(static_cast <bool> (ParentPad) ? void (0) : __assert_fail ("ParentPad", "llvm/include/llvm/IR/InstrTypes.h", 2406, __extension__ __PRETTY_FUNCTION__)); |
2407 | Op<-1>() = ParentPad; |
2408 | } |
2409 | |
2410 | /// getArgOperand/setArgOperand - Return/set the i-th funcletpad argument. |
2411 | /// |
2412 | Value *getArgOperand(unsigned i) const { return getOperand(i); } |
2413 | void setArgOperand(unsigned i, Value *v) { setOperand(i, v); } |
2414 | |
2415 | /// arg_operands - iteration adapter for range-for loops. |
2416 | op_range arg_operands() { return op_range(op_begin(), op_end() - 1); } |
2417 | |
2418 | /// arg_operands - iteration adapter for range-for loops. |
2419 | const_op_range arg_operands() const { |
2420 | return const_op_range(op_begin(), op_end() - 1); |
2421 | } |
2422 | |
2423 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
2424 | static bool classof(const Instruction *I) { return I->isFuncletPad(); } |
2425 | static bool classof(const Value *V) { |
2426 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
2427 | } |
2428 | }; |
2429 | |
2430 | template <> |
2431 | struct OperandTraits<FuncletPadInst> |
2432 | : public VariadicOperandTraits<FuncletPadInst, /*MINARITY=*/1> {}; |
2433 | |
2434 | 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 { (static_cast <bool > (i_nocapture < OperandTraits<FuncletPadInst>::operands (this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<FuncletPadInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/InstrTypes.h", 2434, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<FuncletPadInst>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<FuncletPadInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/InstrTypes.h", 2434, __extension__ __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 ); } |
2435 | |
2436 | } // end namespace llvm |
2437 | |
2438 | #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 | struct undef_match { | ||||||||||||||||||||
92 | static bool check(const Value *V) { | ||||||||||||||||||||
93 | if (isa<UndefValue>(V)) | ||||||||||||||||||||
94 | return true; | ||||||||||||||||||||
95 | |||||||||||||||||||||
96 | const auto *CA = dyn_cast<ConstantAggregate>(V); | ||||||||||||||||||||
97 | if (!CA) | ||||||||||||||||||||
98 | return false; | ||||||||||||||||||||
99 | |||||||||||||||||||||
100 | SmallPtrSet<const ConstantAggregate *, 8> Seen; | ||||||||||||||||||||
101 | SmallVector<const ConstantAggregate *, 8> Worklist; | ||||||||||||||||||||
102 | |||||||||||||||||||||
103 | // Either UndefValue, PoisonValue, or an aggregate that only contains | ||||||||||||||||||||
104 | // these is accepted by matcher. | ||||||||||||||||||||
105 | // CheckValue returns false if CA cannot satisfy this constraint. | ||||||||||||||||||||
106 | auto CheckValue = [&](const ConstantAggregate *CA) { | ||||||||||||||||||||
107 | for (const Value *Op : CA->operand_values()) { | ||||||||||||||||||||
108 | if (isa<UndefValue>(Op)) | ||||||||||||||||||||
109 | continue; | ||||||||||||||||||||
110 | |||||||||||||||||||||
111 | const auto *CA = dyn_cast<ConstantAggregate>(Op); | ||||||||||||||||||||
112 | if (!CA) | ||||||||||||||||||||
113 | return false; | ||||||||||||||||||||
114 | if (Seen.insert(CA).second) | ||||||||||||||||||||
115 | Worklist.emplace_back(CA); | ||||||||||||||||||||
116 | } | ||||||||||||||||||||
117 | |||||||||||||||||||||
118 | return true; | ||||||||||||||||||||
119 | }; | ||||||||||||||||||||
120 | |||||||||||||||||||||
121 | if (!CheckValue(CA)) | ||||||||||||||||||||
122 | return false; | ||||||||||||||||||||
123 | |||||||||||||||||||||
124 | while (!Worklist.empty()) { | ||||||||||||||||||||
125 | if (!CheckValue(Worklist.pop_back_val())) | ||||||||||||||||||||
126 | return false; | ||||||||||||||||||||
127 | } | ||||||||||||||||||||
128 | return true; | ||||||||||||||||||||
129 | } | ||||||||||||||||||||
130 | template <typename ITy> bool match(ITy *V) { return check(V); } | ||||||||||||||||||||
131 | }; | ||||||||||||||||||||
132 | |||||||||||||||||||||
133 | /// Match an arbitrary undef constant. This matches poison as well. | ||||||||||||||||||||
134 | /// If this is an aggregate and contains a non-aggregate element that is | ||||||||||||||||||||
135 | /// neither undef nor poison, the aggregate is not matched. | ||||||||||||||||||||
136 | inline auto m_Undef() { return undef_match(); } | ||||||||||||||||||||
137 | |||||||||||||||||||||
138 | /// Match an arbitrary poison constant. | ||||||||||||||||||||
139 | inline class_match<PoisonValue> m_Poison() { return class_match<PoisonValue>(); } | ||||||||||||||||||||
140 | |||||||||||||||||||||
141 | /// Match an arbitrary Constant and ignore it. | ||||||||||||||||||||
142 | inline class_match<Constant> m_Constant() { return class_match<Constant>(); } | ||||||||||||||||||||
143 | |||||||||||||||||||||
144 | /// Match an arbitrary ConstantInt and ignore it. | ||||||||||||||||||||
145 | inline class_match<ConstantInt> m_ConstantInt() { | ||||||||||||||||||||
146 | return class_match<ConstantInt>(); | ||||||||||||||||||||
147 | } | ||||||||||||||||||||
148 | |||||||||||||||||||||
149 | /// Match an arbitrary ConstantFP and ignore it. | ||||||||||||||||||||
150 | inline class_match<ConstantFP> m_ConstantFP() { | ||||||||||||||||||||
151 | return class_match<ConstantFP>(); | ||||||||||||||||||||
152 | } | ||||||||||||||||||||
153 | |||||||||||||||||||||
154 | /// Match an arbitrary ConstantExpr and ignore it. | ||||||||||||||||||||
155 | inline class_match<ConstantExpr> m_ConstantExpr() { | ||||||||||||||||||||
156 | return class_match<ConstantExpr>(); | ||||||||||||||||||||
157 | } | ||||||||||||||||||||
158 | |||||||||||||||||||||
159 | /// Match an arbitrary basic block value and ignore it. | ||||||||||||||||||||
160 | inline class_match<BasicBlock> m_BasicBlock() { | ||||||||||||||||||||
161 | return class_match<BasicBlock>(); | ||||||||||||||||||||
162 | } | ||||||||||||||||||||
163 | |||||||||||||||||||||
164 | /// Inverting matcher | ||||||||||||||||||||
165 | template <typename Ty> struct match_unless { | ||||||||||||||||||||
166 | Ty M; | ||||||||||||||||||||
167 | |||||||||||||||||||||
168 | match_unless(const Ty &Matcher) : M(Matcher) {} | ||||||||||||||||||||
169 | |||||||||||||||||||||
170 | template <typename ITy> bool match(ITy *V) { return !M.match(V); } | ||||||||||||||||||||
171 | }; | ||||||||||||||||||||
172 | |||||||||||||||||||||
173 | /// Match if the inner matcher does *NOT* match. | ||||||||||||||||||||
174 | template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) { | ||||||||||||||||||||
175 | return match_unless<Ty>(M); | ||||||||||||||||||||
176 | } | ||||||||||||||||||||
177 | |||||||||||||||||||||
178 | /// Matching combinators | ||||||||||||||||||||
179 | template <typename LTy, typename RTy> struct match_combine_or { | ||||||||||||||||||||
180 | LTy L; | ||||||||||||||||||||
181 | RTy R; | ||||||||||||||||||||
182 | |||||||||||||||||||||
183 | match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {} | ||||||||||||||||||||
184 | |||||||||||||||||||||
185 | template <typename ITy> bool match(ITy *V) { | ||||||||||||||||||||
186 | if (L.match(V)) | ||||||||||||||||||||
187 | return true; | ||||||||||||||||||||
188 | if (R.match(V)) | ||||||||||||||||||||
189 | return true; | ||||||||||||||||||||
190 | return false; | ||||||||||||||||||||
191 | } | ||||||||||||||||||||
192 | }; | ||||||||||||||||||||
193 | |||||||||||||||||||||
194 | template <typename LTy, typename RTy> struct match_combine_and { | ||||||||||||||||||||
195 | LTy L; | ||||||||||||||||||||
196 | RTy R; | ||||||||||||||||||||
197 | |||||||||||||||||||||
198 | match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {} | ||||||||||||||||||||
199 | |||||||||||||||||||||
200 | template <typename ITy> bool match(ITy *V) { | ||||||||||||||||||||
201 | if (L.match(V)) | ||||||||||||||||||||
202 | if (R.match(V)) | ||||||||||||||||||||
203 | return true; | ||||||||||||||||||||
204 | return false; | ||||||||||||||||||||
205 | } | ||||||||||||||||||||
206 | }; | ||||||||||||||||||||
207 | |||||||||||||||||||||
208 | /// Combine two pattern matchers matching L || R | ||||||||||||||||||||
209 | template <typename LTy, typename RTy> | ||||||||||||||||||||
210 | inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) { | ||||||||||||||||||||
211 | return match_combine_or<LTy, RTy>(L, R); | ||||||||||||||||||||
212 | } | ||||||||||||||||||||
213 | |||||||||||||||||||||
214 | /// Combine two pattern matchers matching L && R | ||||||||||||||||||||
215 | template <typename LTy, typename RTy> | ||||||||||||||||||||
216 | inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) { | ||||||||||||||||||||
217 | return match_combine_and<LTy, RTy>(L, R); | ||||||||||||||||||||
218 | } | ||||||||||||||||||||
219 | |||||||||||||||||||||
220 | struct apint_match { | ||||||||||||||||||||
221 | const APInt *&Res; | ||||||||||||||||||||
222 | bool AllowUndef; | ||||||||||||||||||||
223 | |||||||||||||||||||||
224 | apint_match(const APInt *&Res, bool AllowUndef) | ||||||||||||||||||||
225 | : Res(Res), AllowUndef(AllowUndef) {} | ||||||||||||||||||||
226 | |||||||||||||||||||||
227 | template <typename ITy> bool match(ITy *V) { | ||||||||||||||||||||
228 | if (auto *CI = dyn_cast<ConstantInt>(V)) { | ||||||||||||||||||||
229 | Res = &CI->getValue(); | ||||||||||||||||||||
230 | return true; | ||||||||||||||||||||
231 | } | ||||||||||||||||||||
232 | if (V->getType()->isVectorTy()) | ||||||||||||||||||||
233 | if (const auto *C = dyn_cast<Constant>(V)) | ||||||||||||||||||||
234 | if (auto *CI = dyn_cast_or_null<ConstantInt>( | ||||||||||||||||||||
235 | C->getSplatValue(AllowUndef))) { | ||||||||||||||||||||
236 | Res = &CI->getValue(); | ||||||||||||||||||||
237 | return true; | ||||||||||||||||||||
238 | } | ||||||||||||||||||||
239 | return false; | ||||||||||||||||||||
240 | } | ||||||||||||||||||||
241 | }; | ||||||||||||||||||||
242 | // Either constexpr if or renaming ConstantFP::getValueAPF to | ||||||||||||||||||||
243 | // ConstantFP::getValue is needed to do it via single template | ||||||||||||||||||||
244 | // function for both apint/apfloat. | ||||||||||||||||||||
245 | struct apfloat_match { | ||||||||||||||||||||
246 | const APFloat *&Res; | ||||||||||||||||||||
247 | bool AllowUndef; | ||||||||||||||||||||
248 | |||||||||||||||||||||
249 | apfloat_match(const APFloat *&Res, bool AllowUndef) | ||||||||||||||||||||
250 | : Res(Res), AllowUndef(AllowUndef) {} | ||||||||||||||||||||
251 | |||||||||||||||||||||
252 | template <typename ITy> bool match(ITy *V) { | ||||||||||||||||||||
253 | if (auto *CI = dyn_cast<ConstantFP>(V)) { | ||||||||||||||||||||
254 | Res = &CI->getValueAPF(); | ||||||||||||||||||||
255 | return true; | ||||||||||||||||||||
256 | } | ||||||||||||||||||||
257 | if (V->getType()->isVectorTy()) | ||||||||||||||||||||
258 | if (const auto *C = dyn_cast<Constant>(V)) | ||||||||||||||||||||
259 | if (auto *CI = dyn_cast_or_null<ConstantFP>( | ||||||||||||||||||||
260 | C->getSplatValue(AllowUndef))) { | ||||||||||||||||||||
261 | Res = &CI->getValueAPF(); | ||||||||||||||||||||
262 | return true; | ||||||||||||||||||||
263 | } | ||||||||||||||||||||
264 | return false; | ||||||||||||||||||||
265 | } | ||||||||||||||||||||
266 | }; | ||||||||||||||||||||
267 | |||||||||||||||||||||
268 | /// Match a ConstantInt or splatted ConstantVector, binding the | ||||||||||||||||||||
269 | /// specified pointer to the contained APInt. | ||||||||||||||||||||
270 | inline apint_match m_APInt(const APInt *&Res) { | ||||||||||||||||||||
271 | // Forbid undefs by default to maintain previous behavior. | ||||||||||||||||||||
272 | return apint_match(Res, /* AllowUndef */ false); | ||||||||||||||||||||
273 | } | ||||||||||||||||||||
274 | |||||||||||||||||||||
275 | /// Match APInt while allowing undefs in splat vector constants. | ||||||||||||||||||||
276 | inline apint_match m_APIntAllowUndef(const APInt *&Res) { | ||||||||||||||||||||
277 | return apint_match(Res, /* AllowUndef */ true); | ||||||||||||||||||||
278 | } | ||||||||||||||||||||
279 | |||||||||||||||||||||
280 | /// Match APInt while forbidding undefs in splat vector constants. | ||||||||||||||||||||
281 | inline apint_match m_APIntForbidUndef(const APInt *&Res) { | ||||||||||||||||||||
282 | return apint_match(Res, /* AllowUndef */ false); | ||||||||||||||||||||
283 | } | ||||||||||||||||||||
284 | |||||||||||||||||||||
285 | /// Match a ConstantFP or splatted ConstantVector, binding the | ||||||||||||||||||||
286 | /// specified pointer to the contained APFloat. | ||||||||||||||||||||
287 | inline apfloat_match m_APFloat(const APFloat *&Res) { | ||||||||||||||||||||
288 | // Forbid undefs by default to maintain previous behavior. | ||||||||||||||||||||
289 | return apfloat_match(Res, /* AllowUndef */ false); | ||||||||||||||||||||
290 | } | ||||||||||||||||||||
291 | |||||||||||||||||||||
292 | /// Match APFloat while allowing undefs in splat vector constants. | ||||||||||||||||||||
293 | inline apfloat_match m_APFloatAllowUndef(const APFloat *&Res) { | ||||||||||||||||||||
294 | return apfloat_match(Res, /* AllowUndef */ true); | ||||||||||||||||||||
295 | } | ||||||||||||||||||||
296 | |||||||||||||||||||||
297 | /// Match APFloat while forbidding undefs in splat vector constants. | ||||||||||||||||||||
298 | inline apfloat_match m_APFloatForbidUndef(const APFloat *&Res) { | ||||||||||||||||||||
299 | return apfloat_match(Res, /* AllowUndef */ false); | ||||||||||||||||||||
300 | } | ||||||||||||||||||||
301 | |||||||||||||||||||||
302 | template <int64_t Val> struct constantint_match { | ||||||||||||||||||||
303 | template <typename ITy> bool match(ITy *V) { | ||||||||||||||||||||
304 | if (const auto *CI = dyn_cast<ConstantInt>(V)) { | ||||||||||||||||||||
305 | const APInt &CIV = CI->getValue(); | ||||||||||||||||||||
306 | if (Val >= 0) | ||||||||||||||||||||
307 | return CIV == static_cast<uint64_t>(Val); | ||||||||||||||||||||
308 | // If Val is negative, and CI is shorter than it, truncate to the right | ||||||||||||||||||||
309 | // number of bits. If it is larger, then we have to sign extend. Just | ||||||||||||||||||||
310 | // compare their negated values. | ||||||||||||||||||||
311 | return -CIV == -Val; | ||||||||||||||||||||
312 | } | ||||||||||||||||||||
313 | return false; | ||||||||||||||||||||
314 | } | ||||||||||||||||||||
315 | }; | ||||||||||||||||||||
316 | |||||||||||||||||||||
317 | /// Match a ConstantInt with a specific value. | ||||||||||||||||||||
318 | template <int64_t Val> inline constantint_match<Val> m_ConstantInt() { | ||||||||||||||||||||
319 | return constantint_match<Val>(); | ||||||||||||||||||||
320 | } | ||||||||||||||||||||
321 | |||||||||||||||||||||
322 | /// This helper class is used to match constant scalars, vector splats, | ||||||||||||||||||||
323 | /// and fixed width vectors that satisfy a specified predicate. | ||||||||||||||||||||
324 | /// For fixed width vector constants, undefined elements are ignored. | ||||||||||||||||||||
325 | template <typename Predicate, typename ConstantVal> | ||||||||||||||||||||
326 | struct cstval_pred_ty : public Predicate { | ||||||||||||||||||||
327 | template <typename ITy> bool match(ITy *V) { | ||||||||||||||||||||
328 | if (const auto *CV = dyn_cast<ConstantVal>(V)) | ||||||||||||||||||||
329 | return this->isValue(CV->getValue()); | ||||||||||||||||||||
330 | if (const auto *VTy = dyn_cast<VectorType>(V->getType())) { | ||||||||||||||||||||
331 | if (const auto *C = dyn_cast<Constant>(V)) { | ||||||||||||||||||||
332 | if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue())) | ||||||||||||||||||||
333 | return this->isValue(CV->getValue()); | ||||||||||||||||||||
334 | |||||||||||||||||||||
335 | // Number of elements of a scalable vector unknown at compile time | ||||||||||||||||||||
336 | auto *FVTy = dyn_cast<FixedVectorType>(VTy); | ||||||||||||||||||||
337 | if (!FVTy) | ||||||||||||||||||||
338 | return false; | ||||||||||||||||||||
339 | |||||||||||||||||||||
340 | // Non-splat vector constant: check each element for a match. | ||||||||||||||||||||
341 | unsigned NumElts = FVTy->getNumElements(); | ||||||||||||||||||||
342 | assert(NumElts != 0 && "Constant vector with no elements?")(static_cast <bool> (NumElts != 0 && "Constant vector with no elements?" ) ? void (0) : __assert_fail ("NumElts != 0 && \"Constant vector with no elements?\"" , "llvm/include/llvm/IR/PatternMatch.h", 342, __extension__ __PRETTY_FUNCTION__ )); | ||||||||||||||||||||
343 | bool HasNonUndefElements = false; | ||||||||||||||||||||
344 | for (unsigned i = 0; i != NumElts; ++i) { | ||||||||||||||||||||
345 | Constant *Elt = C->getAggregateElement(i); | ||||||||||||||||||||
346 | if (!Elt) | ||||||||||||||||||||
347 | return false; | ||||||||||||||||||||
348 | if (isa<UndefValue>(Elt)) | ||||||||||||||||||||
349 | continue; | ||||||||||||||||||||
350 | auto *CV = dyn_cast<ConstantVal>(Elt); | ||||||||||||||||||||
351 | if (!CV || !this->isValue(CV->getValue())) | ||||||||||||||||||||
352 | return false; | ||||||||||||||||||||
353 | HasNonUndefElements = true; | ||||||||||||||||||||
354 | } | ||||||||||||||||||||
355 | return HasNonUndefElements; | ||||||||||||||||||||
356 | } | ||||||||||||||||||||
357 | } | ||||||||||||||||||||
358 | return false; | ||||||||||||||||||||
359 | } | ||||||||||||||||||||
360 | }; | ||||||||||||||||||||
361 | |||||||||||||||||||||
362 | /// specialization of cstval_pred_ty for ConstantInt | ||||||||||||||||||||
363 | template <typename Predicate> | ||||||||||||||||||||
364 | using cst_pred_ty = cstval_pred_ty<Predicate, ConstantInt>; | ||||||||||||||||||||
365 | |||||||||||||||||||||
366 | /// specialization of cstval_pred_ty for ConstantFP | ||||||||||||||||||||
367 | template <typename Predicate> | ||||||||||||||||||||
368 | using cstfp_pred_ty = cstval_pred_ty<Predicate, ConstantFP>; | ||||||||||||||||||||
369 | |||||||||||||||||||||
370 | /// This helper class is used to match scalar and vector constants that | ||||||||||||||||||||
371 | /// satisfy a specified predicate, and bind them to an APInt. | ||||||||||||||||||||
372 | template <typename Predicate> struct api_pred_ty : public Predicate { | ||||||||||||||||||||
373 | const APInt *&Res; | ||||||||||||||||||||
374 | |||||||||||||||||||||
375 | api_pred_ty(const APInt *&R) : Res(R) {} | ||||||||||||||||||||
376 | |||||||||||||||||||||
377 | template <typename ITy> bool match(ITy *V) { | ||||||||||||||||||||
378 | if (const auto *CI = dyn_cast<ConstantInt>(V)) | ||||||||||||||||||||
379 | if (this->isValue(CI->getValue())) { | ||||||||||||||||||||
380 | Res = &CI->getValue(); | ||||||||||||||||||||
381 | return true; | ||||||||||||||||||||
382 | } | ||||||||||||||||||||
383 | if (V->getType()->isVectorTy()) | ||||||||||||||||||||
384 | if (const auto *C = dyn_cast<Constant>(V)) | ||||||||||||||||||||
385 | if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) | ||||||||||||||||||||
386 | if (this->isValue(CI->getValue())) { | ||||||||||||||||||||
387 | Res = &CI->getValue(); | ||||||||||||||||||||
388 | return true; | ||||||||||||||||||||
389 | } | ||||||||||||||||||||
390 | |||||||||||||||||||||
391 | return false; | ||||||||||||||||||||
392 | } | ||||||||||||||||||||
393 | }; | ||||||||||||||||||||
394 | |||||||||||||||||||||
395 | /// This helper class is used to match scalar and vector constants that | ||||||||||||||||||||
396 | /// satisfy a specified predicate, and bind them to an APFloat. | ||||||||||||||||||||
397 | /// Undefs are allowed in splat vector constants. | ||||||||||||||||||||
398 | template <typename Predicate> struct apf_pred_ty : public Predicate { | ||||||||||||||||||||
399 | const APFloat *&Res; | ||||||||||||||||||||
400 | |||||||||||||||||||||
401 | apf_pred_ty(const APFloat *&R) : Res(R) {} | ||||||||||||||||||||
402 | |||||||||||||||||||||
403 | template <typename ITy> bool match(ITy *V) { | ||||||||||||||||||||
404 | if (const auto *CI = dyn_cast<ConstantFP>(V)) | ||||||||||||||||||||
405 | if (this->isValue(CI->getValue())) { | ||||||||||||||||||||
406 | Res = &CI->getValue(); | ||||||||||||||||||||
407 | return true; | ||||||||||||||||||||
408 | } | ||||||||||||||||||||
409 | if (V->getType()->isVectorTy()) | ||||||||||||||||||||
410 | if (const auto *C = dyn_cast<Constant>(V)) | ||||||||||||||||||||
411 | if (auto *CI = dyn_cast_or_null<ConstantFP>( | ||||||||||||||||||||
412 | C->getSplatValue(/* AllowUndef */ true))) | ||||||||||||||||||||
413 | if (this->isValue(CI->getValue())) { | ||||||||||||||||||||
414 | Res = &CI->getValue(); | ||||||||||||||||||||
415 | return true; | ||||||||||||||||||||
416 | } | ||||||||||||||||||||
417 | |||||||||||||||||||||
418 | return false; | ||||||||||||||||||||
419 | } | ||||||||||||||||||||
420 | }; | ||||||||||||||||||||
421 | |||||||||||||||||||||
422 | /////////////////////////////////////////////////////////////////////////////// | ||||||||||||||||||||
423 | // | ||||||||||||||||||||
424 | // Encapsulate constant value queries for use in templated predicate matchers. | ||||||||||||||||||||
425 | // This allows checking if constants match using compound predicates and works | ||||||||||||||||||||
426 | // with vector constants, possibly with relaxed constraints. For example, ignore | ||||||||||||||||||||
427 | // undef values. | ||||||||||||||||||||
428 | // | ||||||||||||||||||||
429 | /////////////////////////////////////////////////////////////////////////////// | ||||||||||||||||||||
430 | |||||||||||||||||||||
431 | struct is_any_apint { | ||||||||||||||||||||
432 | bool isValue(const APInt &C) { return true; } | ||||||||||||||||||||
433 | }; | ||||||||||||||||||||
434 | /// Match an integer or vector with any integral constant. | ||||||||||||||||||||
435 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
436 | inline cst_pred_ty<is_any_apint> m_AnyIntegralConstant() { | ||||||||||||||||||||
437 | return cst_pred_ty<is_any_apint>(); | ||||||||||||||||||||
438 | } | ||||||||||||||||||||
439 | |||||||||||||||||||||
440 | struct is_all_ones { | ||||||||||||||||||||
441 | bool isValue(const APInt &C) { return C.isAllOnes(); } | ||||||||||||||||||||
442 | }; | ||||||||||||||||||||
443 | /// Match an integer or vector with all bits set. | ||||||||||||||||||||
444 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
445 | inline cst_pred_ty<is_all_ones> m_AllOnes() { | ||||||||||||||||||||
446 | return cst_pred_ty<is_all_ones>(); | ||||||||||||||||||||
447 | } | ||||||||||||||||||||
448 | |||||||||||||||||||||
449 | struct is_maxsignedvalue { | ||||||||||||||||||||
450 | bool isValue(const APInt &C) { return C.isMaxSignedValue(); } | ||||||||||||||||||||
451 | }; | ||||||||||||||||||||
452 | /// Match an integer or vector with values having all bits except for the high | ||||||||||||||||||||
453 | /// bit set (0x7f...). | ||||||||||||||||||||
454 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
455 | inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() { | ||||||||||||||||||||
456 | return cst_pred_ty<is_maxsignedvalue>(); | ||||||||||||||||||||
457 | } | ||||||||||||||||||||
458 | inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) { | ||||||||||||||||||||
459 | return V; | ||||||||||||||||||||
460 | } | ||||||||||||||||||||
461 | |||||||||||||||||||||
462 | struct is_negative { | ||||||||||||||||||||
463 | bool isValue(const APInt &C) { return C.isNegative(); } | ||||||||||||||||||||
464 | }; | ||||||||||||||||||||
465 | /// Match an integer or vector of negative values. | ||||||||||||||||||||
466 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
467 | inline cst_pred_ty<is_negative> m_Negative() { | ||||||||||||||||||||
468 | return cst_pred_ty<is_negative>(); | ||||||||||||||||||||
469 | } | ||||||||||||||||||||
470 | inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { | ||||||||||||||||||||
471 | return V; | ||||||||||||||||||||
472 | } | ||||||||||||||||||||
473 | |||||||||||||||||||||
474 | struct is_nonnegative { | ||||||||||||||||||||
475 | bool isValue(const APInt &C) { return C.isNonNegative(); } | ||||||||||||||||||||
476 | }; | ||||||||||||||||||||
477 | /// Match an integer or vector of non-negative values. | ||||||||||||||||||||
478 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
479 | inline cst_pred_ty<is_nonnegative> m_NonNegative() { | ||||||||||||||||||||
480 | return cst_pred_ty<is_nonnegative>(); | ||||||||||||||||||||
481 | } | ||||||||||||||||||||
482 | inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { | ||||||||||||||||||||
483 | return V; | ||||||||||||||||||||
484 | } | ||||||||||||||||||||
485 | |||||||||||||||||||||
486 | struct is_strictlypositive { | ||||||||||||||||||||
487 | bool isValue(const APInt &C) { return C.isStrictlyPositive(); } | ||||||||||||||||||||
488 | }; | ||||||||||||||||||||
489 | /// Match an integer or vector of strictly positive values. | ||||||||||||||||||||
490 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
491 | inline cst_pred_ty<is_strictlypositive> m_StrictlyPositive() { | ||||||||||||||||||||
492 | return cst_pred_ty<is_strictlypositive>(); | ||||||||||||||||||||
493 | } | ||||||||||||||||||||
494 | inline api_pred_ty<is_strictlypositive> m_StrictlyPositive(const APInt *&V) { | ||||||||||||||||||||
495 | return V; | ||||||||||||||||||||
496 | } | ||||||||||||||||||||
497 | |||||||||||||||||||||
498 | struct is_nonpositive { | ||||||||||||||||||||
499 | bool isValue(const APInt &C) { return C.isNonPositive(); } | ||||||||||||||||||||
500 | }; | ||||||||||||||||||||
501 | /// Match an integer or vector of non-positive values. | ||||||||||||||||||||
502 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
503 | inline cst_pred_ty<is_nonpositive> m_NonPositive() { | ||||||||||||||||||||
504 | return cst_pred_ty<is_nonpositive>(); | ||||||||||||||||||||
505 | } | ||||||||||||||||||||
506 | inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; } | ||||||||||||||||||||
507 | |||||||||||||||||||||
508 | struct is_one { | ||||||||||||||||||||
509 | bool isValue(const APInt &C) { return C.isOne(); } | ||||||||||||||||||||
510 | }; | ||||||||||||||||||||
511 | /// Match an integer 1 or a vector with all elements equal to 1. | ||||||||||||||||||||
512 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
513 | inline cst_pred_ty<is_one> m_One() { | ||||||||||||||||||||
514 | return cst_pred_ty<is_one>(); | ||||||||||||||||||||
515 | } | ||||||||||||||||||||
516 | |||||||||||||||||||||
517 | struct is_zero_int { | ||||||||||||||||||||
518 | bool isValue(const APInt &C) { return C.isZero(); } | ||||||||||||||||||||
519 | }; | ||||||||||||||||||||
520 | /// Match an integer 0 or a vector with all elements equal to 0. | ||||||||||||||||||||
521 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
522 | inline cst_pred_ty<is_zero_int> m_ZeroInt() { | ||||||||||||||||||||
523 | return cst_pred_ty<is_zero_int>(); | ||||||||||||||||||||
524 | } | ||||||||||||||||||||
525 | |||||||||||||||||||||
526 | struct is_zero { | ||||||||||||||||||||
527 | template <typename ITy> bool match(ITy *V) { | ||||||||||||||||||||
528 | auto *C = dyn_cast<Constant>(V); | ||||||||||||||||||||
529 | // FIXME: this should be able to do something for scalable vectors | ||||||||||||||||||||
530 | return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C)); | ||||||||||||||||||||
531 | } | ||||||||||||||||||||
532 | }; | ||||||||||||||||||||
533 | /// Match any null constant or a vector with all elements equal to 0. | ||||||||||||||||||||
534 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
535 | inline is_zero m_Zero() { | ||||||||||||||||||||
536 | return is_zero(); | ||||||||||||||||||||
537 | } | ||||||||||||||||||||
538 | |||||||||||||||||||||
539 | struct is_power2 { | ||||||||||||||||||||
540 | bool isValue(const APInt &C) { return C.isPowerOf2(); } | ||||||||||||||||||||
541 | }; | ||||||||||||||||||||
542 | /// Match an integer or vector power-of-2. | ||||||||||||||||||||
543 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
544 | inline cst_pred_ty<is_power2> m_Power2() { | ||||||||||||||||||||
545 | return cst_pred_ty<is_power2>(); | ||||||||||||||||||||
546 | } | ||||||||||||||||||||
547 | inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { | ||||||||||||||||||||
548 | return V; | ||||||||||||||||||||
549 | } | ||||||||||||||||||||
550 | |||||||||||||||||||||
551 | struct is_negated_power2 { | ||||||||||||||||||||
552 | bool isValue(const APInt &C) { return C.isNegatedPowerOf2(); } | ||||||||||||||||||||
553 | }; | ||||||||||||||||||||
554 | /// Match a integer or vector negated power-of-2. | ||||||||||||||||||||
555 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
556 | inline cst_pred_ty<is_negated_power2> m_NegatedPower2() { | ||||||||||||||||||||
557 | return cst_pred_ty<is_negated_power2>(); | ||||||||||||||||||||
558 | } | ||||||||||||||||||||
559 | inline api_pred_ty<is_negated_power2> m_NegatedPower2(const APInt *&V) { | ||||||||||||||||||||
560 | return V; | ||||||||||||||||||||
561 | } | ||||||||||||||||||||
562 | |||||||||||||||||||||
563 | struct is_power2_or_zero { | ||||||||||||||||||||
564 | bool isValue(const APInt &C) { return !C || C.isPowerOf2(); } | ||||||||||||||||||||
565 | }; | ||||||||||||||||||||
566 | /// Match an integer or vector of 0 or power-of-2 values. | ||||||||||||||||||||
567 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
568 | inline cst_pred_ty<is_power2_or_zero> m_Power2OrZero() { | ||||||||||||||||||||
569 | return cst_pred_ty<is_power2_or_zero>(); | ||||||||||||||||||||
570 | } | ||||||||||||||||||||
571 | inline api_pred_ty<is_power2_or_zero> m_Power2OrZero(const APInt *&V) { | ||||||||||||||||||||
572 | return V; | ||||||||||||||||||||
573 | } | ||||||||||||||||||||
574 | |||||||||||||||||||||
575 | struct is_sign_mask { | ||||||||||||||||||||
576 | bool isValue(const APInt &C) { return C.isSignMask(); } | ||||||||||||||||||||
577 | }; | ||||||||||||||||||||
578 | /// Match an integer or vector with only the sign bit(s) set. | ||||||||||||||||||||
579 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
580 | inline cst_pred_ty<is_sign_mask> m_SignMask() { | ||||||||||||||||||||
581 | return cst_pred_ty<is_sign_mask>(); | ||||||||||||||||||||
582 | } | ||||||||||||||||||||
583 | |||||||||||||||||||||
584 | struct is_lowbit_mask { | ||||||||||||||||||||
585 | bool isValue(const APInt &C) { return C.isMask(); } | ||||||||||||||||||||
586 | }; | ||||||||||||||||||||
587 | /// Match an integer or vector with only the low bit(s) set. | ||||||||||||||||||||
588 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
589 | inline cst_pred_ty<is_lowbit_mask> m_LowBitMask() { | ||||||||||||||||||||
590 | return cst_pred_ty<is_lowbit_mask>(); | ||||||||||||||||||||
591 | } | ||||||||||||||||||||
592 | inline api_pred_ty<is_lowbit_mask> m_LowBitMask(const APInt *&V) { | ||||||||||||||||||||
593 | return V; | ||||||||||||||||||||
594 | } | ||||||||||||||||||||
595 | |||||||||||||||||||||
596 | struct icmp_pred_with_threshold { | ||||||||||||||||||||
597 | ICmpInst::Predicate Pred; | ||||||||||||||||||||
598 | const APInt *Thr; | ||||||||||||||||||||
599 | bool isValue(const APInt &C) { return ICmpInst::compare(C, *Thr, Pred); } | ||||||||||||||||||||
600 | }; | ||||||||||||||||||||
601 | /// Match an integer or vector with every element comparing 'pred' (eg/ne/...) | ||||||||||||||||||||
602 | /// to Threshold. For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
603 | inline cst_pred_ty<icmp_pred_with_threshold> | ||||||||||||||||||||
604 | m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold) { | ||||||||||||||||||||
605 | cst_pred_ty<icmp_pred_with_threshold> P; | ||||||||||||||||||||
606 | P.Pred = Predicate; | ||||||||||||||||||||
607 | P.Thr = &Threshold; | ||||||||||||||||||||
608 | return P; | ||||||||||||||||||||
609 | } | ||||||||||||||||||||
610 | |||||||||||||||||||||
611 | struct is_nan { | ||||||||||||||||||||
612 | bool isValue(const APFloat &C) { return C.isNaN(); } | ||||||||||||||||||||
613 | }; | ||||||||||||||||||||
614 | /// Match an arbitrary NaN constant. This includes quiet and signalling nans. | ||||||||||||||||||||
615 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
616 | inline cstfp_pred_ty<is_nan> m_NaN() { | ||||||||||||||||||||
617 | return cstfp_pred_ty<is_nan>(); | ||||||||||||||||||||
618 | } | ||||||||||||||||||||
619 | |||||||||||||||||||||
620 | struct is_nonnan { | ||||||||||||||||||||
621 | bool isValue(const APFloat &C) { return !C.isNaN(); } | ||||||||||||||||||||
622 | }; | ||||||||||||||||||||
623 | /// Match a non-NaN FP constant. | ||||||||||||||||||||
624 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
625 | inline cstfp_pred_ty<is_nonnan> m_NonNaN() { | ||||||||||||||||||||
626 | return cstfp_pred_ty<is_nonnan>(); | ||||||||||||||||||||
627 | } | ||||||||||||||||||||
628 | |||||||||||||||||||||
629 | struct is_inf { | ||||||||||||||||||||
630 | bool isValue(const APFloat &C) { return C.isInfinity(); } | ||||||||||||||||||||
631 | }; | ||||||||||||||||||||
632 | /// Match a positive or negative infinity FP constant. | ||||||||||||||||||||
633 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
634 | inline cstfp_pred_ty<is_inf> m_Inf() { | ||||||||||||||||||||
635 | return cstfp_pred_ty<is_inf>(); | ||||||||||||||||||||
636 | } | ||||||||||||||||||||
637 | |||||||||||||||||||||
638 | struct is_noninf { | ||||||||||||||||||||
639 | bool isValue(const APFloat &C) { return !C.isInfinity(); } | ||||||||||||||||||||
640 | }; | ||||||||||||||||||||
641 | /// Match a non-infinity FP constant, i.e. finite or NaN. | ||||||||||||||||||||
642 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
643 | inline cstfp_pred_ty<is_noninf> m_NonInf() { | ||||||||||||||||||||
644 | return cstfp_pred_ty<is_noninf>(); | ||||||||||||||||||||
645 | } | ||||||||||||||||||||
646 | |||||||||||||||||||||
647 | struct is_finite { | ||||||||||||||||||||
648 | bool isValue(const APFloat &C) { return C.isFinite(); } | ||||||||||||||||||||
649 | }; | ||||||||||||||||||||
650 | /// Match a finite FP constant, i.e. not infinity or NaN. | ||||||||||||||||||||
651 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
652 | inline cstfp_pred_ty<is_finite> m_Finite() { | ||||||||||||||||||||
653 | return cstfp_pred_ty<is_finite>(); | ||||||||||||||||||||
654 | } | ||||||||||||||||||||
655 | inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; } | ||||||||||||||||||||
656 | |||||||||||||||||||||
657 | struct is_finitenonzero { | ||||||||||||||||||||
658 | bool isValue(const APFloat &C) { return C.isFiniteNonZero(); } | ||||||||||||||||||||
659 | }; | ||||||||||||||||||||
660 | /// Match a finite non-zero FP constant. | ||||||||||||||||||||
661 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
662 | inline cstfp_pred_ty<is_finitenonzero> m_FiniteNonZero() { | ||||||||||||||||||||
663 | return cstfp_pred_ty<is_finitenonzero>(); | ||||||||||||||||||||
664 | } | ||||||||||||||||||||
665 | inline apf_pred_ty<is_finitenonzero> m_FiniteNonZero(const APFloat *&V) { | ||||||||||||||||||||
666 | return V; | ||||||||||||||||||||
667 | } | ||||||||||||||||||||
668 | |||||||||||||||||||||
669 | struct is_any_zero_fp { | ||||||||||||||||||||
670 | bool isValue(const APFloat &C) { return C.isZero(); } | ||||||||||||||||||||
671 | }; | ||||||||||||||||||||
672 | /// Match a floating-point negative zero or positive zero. | ||||||||||||||||||||
673 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
674 | inline cstfp_pred_ty<is_any_zero_fp> m_AnyZeroFP() { | ||||||||||||||||||||
675 | return cstfp_pred_ty<is_any_zero_fp>(); | ||||||||||||||||||||
676 | } | ||||||||||||||||||||
677 | |||||||||||||||||||||
678 | struct is_pos_zero_fp { | ||||||||||||||||||||
679 | bool isValue(const APFloat &C) { return C.isPosZero(); } | ||||||||||||||||||||
680 | }; | ||||||||||||||||||||
681 | /// Match a floating-point positive zero. | ||||||||||||||||||||
682 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
683 | inline cstfp_pred_ty<is_pos_zero_fp> m_PosZeroFP() { | ||||||||||||||||||||
684 | return cstfp_pred_ty<is_pos_zero_fp>(); | ||||||||||||||||||||
685 | } | ||||||||||||||||||||
686 | |||||||||||||||||||||
687 | struct is_neg_zero_fp { | ||||||||||||||||||||
688 | bool isValue(const APFloat &C) { return C.isNegZero(); } | ||||||||||||||||||||
689 | }; | ||||||||||||||||||||
690 | /// Match a floating-point negative zero. | ||||||||||||||||||||
691 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
692 | inline cstfp_pred_ty<is_neg_zero_fp> m_NegZeroFP() { | ||||||||||||||||||||
693 | return cstfp_pred_ty<is_neg_zero_fp>(); | ||||||||||||||||||||
694 | } | ||||||||||||||||||||
695 | |||||||||||||||||||||
696 | struct is_non_zero_fp { | ||||||||||||||||||||
697 | bool isValue(const APFloat &C) { return C.isNonZero(); } | ||||||||||||||||||||
698 | }; | ||||||||||||||||||||
699 | /// Match a floating-point non-zero. | ||||||||||||||||||||
700 | /// For vectors, this includes constants with undefined elements. | ||||||||||||||||||||
701 | inline cstfp_pred_ty<is_non_zero_fp> m_NonZeroFP() { | ||||||||||||||||||||
702 | return cstfp_pred_ty<is_non_zero_fp>(); | ||||||||||||||||||||
703 | } | ||||||||||||||||||||
704 | |||||||||||||||||||||
705 | /////////////////////////////////////////////////////////////////////////////// | ||||||||||||||||||||
706 | |||||||||||||||||||||
707 | template <typename Class> struct bind_ty { | ||||||||||||||||||||
708 | Class *&VR; | ||||||||||||||||||||
709 | |||||||||||||||||||||
710 | bind_ty(Class *&V) : VR(V) {} | ||||||||||||||||||||
711 | |||||||||||||||||||||
712 | template <typename ITy> bool match(ITy *V) { | ||||||||||||||||||||
713 | if (auto *CV = dyn_cast<Class>(V)) { | ||||||||||||||||||||
714 | VR = CV; | ||||||||||||||||||||
715 | return true; | ||||||||||||||||||||
716 | } | ||||||||||||||||||||
717 | return false; | ||||||||||||||||||||
718 | } | ||||||||||||||||||||
719 | }; | ||||||||||||||||||||
720 | |||||||||||||||||||||
721 | /// Match a value, capturing it if we match. | ||||||||||||||||||||
722 | inline bind_ty<Value> m_Value(Value *&V) { return V; } | ||||||||||||||||||||
723 | inline bind_ty<const Value> m_Value(const Value *&V) { return V; } | ||||||||||||||||||||
724 | |||||||||||||||||||||
725 | /// Match an instruction, capturing it if we match. | ||||||||||||||||||||
726 | inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; } | ||||||||||||||||||||
727 | /// Match a unary operator, capturing it if we match. | ||||||||||||||||||||
728 | inline bind_ty<UnaryOperator> m_UnOp(UnaryOperator *&I) { return I; } | ||||||||||||||||||||
729 | /// Match a binary operator, capturing it if we match. | ||||||||||||||||||||
730 | inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; } | ||||||||||||||||||||
731 | /// Match a with overflow intrinsic, capturing it if we match. | ||||||||||||||||||||
732 | inline bind_ty<WithOverflowInst> m_WithOverflowInst(WithOverflowInst *&I) { return I; } | ||||||||||||||||||||
733 | inline bind_ty<const WithOverflowInst> | ||||||||||||||||||||
734 | m_WithOverflowInst(const WithOverflowInst *&I) { | ||||||||||||||||||||
735 | return I; | ||||||||||||||||||||
736 | } | ||||||||||||||||||||
737 | |||||||||||||||||||||
738 | /// Match a Constant, capturing the value if we match. | ||||||||||||||||||||
739 | inline bind_ty<Constant> m_Constant(Constant *&C) { return C; } | ||||||||||||||||||||
740 | |||||||||||||||||||||
741 | /// Match a ConstantInt, capturing the value if we match. | ||||||||||||||||||||
742 | inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; } | ||||||||||||||||||||
743 | |||||||||||||||||||||
744 | /// Match a ConstantFP, capturing the value if we match. | ||||||||||||||||||||
745 | inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; } | ||||||||||||||||||||
746 | |||||||||||||||||||||
747 | /// Match a ConstantExpr, capturing the value if we match. | ||||||||||||||||||||
748 | inline bind_ty<ConstantExpr> m_ConstantExpr(ConstantExpr *&C) { return C; } | ||||||||||||||||||||
749 | |||||||||||||||||||||
750 | /// Match a basic block value, capturing it if we match. | ||||||||||||||||||||
751 | inline bind_ty<BasicBlock> m_BasicBlock(BasicBlock *&V) { return V; } | ||||||||||||||||||||
752 | inline bind_ty<const BasicBlock> m_BasicBlock(const BasicBlock *&V) { | ||||||||||||||||||||
753 | return V; | ||||||||||||||||||||
754 | } | ||||||||||||||||||||
755 | |||||||||||||||||||||
756 | /// Match an arbitrary immediate Constant and ignore it. | ||||||||||||||||||||
757 | inline match_combine_and<class_match<Constant>, | ||||||||||||||||||||
758 | match_unless<class_match<ConstantExpr>>> | ||||||||||||||||||||
759 | m_ImmConstant() { | ||||||||||||||||||||
760 | return m_CombineAnd(m_Constant(), m_Unless(m_ConstantExpr())); | ||||||||||||||||||||
761 | } | ||||||||||||||||||||
762 | |||||||||||||||||||||
763 | /// Match an immediate Constant, capturing the value if we match. | ||||||||||||||||||||
764 | inline match_combine_and<bind_ty<Constant>, | ||||||||||||||||||||
765 | match_unless<class_match<ConstantExpr>>> | ||||||||||||||||||||
766 | m_ImmConstant(Constant *&C) { | ||||||||||||||||||||
767 | return m_CombineAnd(m_Constant(C), m_Unless(m_ConstantExpr())); | ||||||||||||||||||||
768 | } | ||||||||||||||||||||
769 | |||||||||||||||||||||
770 | /// Match a specified Value*. | ||||||||||||||||||||
771 | struct specificval_ty { | ||||||||||||||||||||
772 | const Value *Val; | ||||||||||||||||||||
773 | |||||||||||||||||||||
774 | specificval_ty(const Value *V) : Val(V) {} | ||||||||||||||||||||
775 | |||||||||||||||||||||
776 | template <typename ITy> bool match(ITy *V) { return V == Val; } | ||||||||||||||||||||
777 | }; | ||||||||||||||||||||
778 | |||||||||||||||||||||
779 | /// Match if we have a specific specified value. | ||||||||||||||||||||
780 | inline specificval_ty m_Specific(const Value *V) { return V; } | ||||||||||||||||||||
781 | |||||||||||||||||||||
782 | /// Stores a reference to the Value *, not the Value * itself, | ||||||||||||||||||||
783 | /// thus can be used in commutative matchers. | ||||||||||||||||||||
784 | template <typename Class> struct deferredval_ty { | ||||||||||||||||||||
785 | Class *const &Val; | ||||||||||||||||||||
786 | |||||||||||||||||||||
787 | deferredval_ty(Class *const &V) : Val(V) {} | ||||||||||||||||||||
788 | |||||||||||||||||||||
789 | template <typename ITy> bool match(ITy *const V) { return V == Val; } | ||||||||||||||||||||
790 | }; | ||||||||||||||||||||
791 | |||||||||||||||||||||
792 | /// Like m_Specific(), but works if the specific value to match is determined | ||||||||||||||||||||
793 | /// as part of the same match() expression. For example: | ||||||||||||||||||||
794 | /// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will | ||||||||||||||||||||
795 | /// bind X before the pattern match starts. | ||||||||||||||||||||
796 | /// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against | ||||||||||||||||||||
797 | /// whichever value m_Value(X) populated. | ||||||||||||||||||||
798 | inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; } | ||||||||||||||||||||
799 | inline deferredval_ty<const Value> m_Deferred(const Value *const &V) { | ||||||||||||||||||||
800 | return V; | ||||||||||||||||||||
801 | } | ||||||||||||||||||||
802 | |||||||||||||||||||||
803 | /// Match a specified floating point value or vector of all elements of | ||||||||||||||||||||
804 | /// that value. | ||||||||||||||||||||
805 | struct specific_fpval { | ||||||||||||||||||||
806 | double Val; | ||||||||||||||||||||
807 | |||||||||||||||||||||
808 | specific_fpval(double V) : Val(V) {} | ||||||||||||||||||||
809 | |||||||||||||||||||||
810 | template <typename ITy> bool match(ITy *V) { | ||||||||||||||||||||
811 | if (const auto *CFP = dyn_cast<ConstantFP>(V)) | ||||||||||||||||||||
812 | return CFP->isExactlyValue(Val); | ||||||||||||||||||||
813 | if (V->getType()->isVectorTy()) | ||||||||||||||||||||
814 | if (const auto *C = dyn_cast<Constant>(V)) | ||||||||||||||||||||
815 | if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue())) | ||||||||||||||||||||
816 | return CFP->isExactlyValue(Val); | ||||||||||||||||||||
817 | return false; | ||||||||||||||||||||
818 | } | ||||||||||||||||||||
819 | }; | ||||||||||||||||||||
820 | |||||||||||||||||||||
821 | /// Match a specific floating point value or vector with all elements | ||||||||||||||||||||
822 | /// equal to the value. | ||||||||||||||||||||
823 | inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); } | ||||||||||||||||||||
824 | |||||||||||||||||||||
825 | /// Match a float 1.0 or vector with all elements equal to 1.0. | ||||||||||||||||||||
826 | inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); } | ||||||||||||||||||||
827 | |||||||||||||||||||||
828 | struct bind_const_intval_ty { | ||||||||||||||||||||
829 | uint64_t &VR; | ||||||||||||||||||||
830 | |||||||||||||||||||||
831 | bind_const_intval_ty(uint64_t &V) : VR(V) {} | ||||||||||||||||||||
832 | |||||||||||||||||||||
833 | template <typename ITy> bool match(ITy *V) { | ||||||||||||||||||||
834 | if (const auto *CV = dyn_cast<ConstantInt>(V)) | ||||||||||||||||||||
835 | if (CV->getValue().ule(UINT64_MAX(18446744073709551615UL))) { | ||||||||||||||||||||
836 | VR = CV->getZExtValue(); | ||||||||||||||||||||
837 | return true; | ||||||||||||||||||||
838 | } | ||||||||||||||||||||
839 | return false; | ||||||||||||||||||||
840 | } | ||||||||||||||||||||
841 | }; | ||||||||||||||||||||
842 | |||||||||||||||||||||
843 | /// Match a specified integer value or vector of all elements of that | ||||||||||||||||||||
844 | /// value. | ||||||||||||||||||||
845 | template <bool AllowUndefs> | ||||||||||||||||||||
846 | struct specific_intval { | ||||||||||||||||||||
847 | APInt Val; | ||||||||||||||||||||
848 | |||||||||||||||||||||
849 | specific_intval(APInt V) : Val(std::move(V)) {} | ||||||||||||||||||||
850 | |||||||||||||||||||||
851 | template <typename ITy> bool match(ITy *V) { | ||||||||||||||||||||
852 | const auto *CI = dyn_cast<ConstantInt>(V); | ||||||||||||||||||||
853 | if (!CI && V->getType()->isVectorTy()) | ||||||||||||||||||||
854 | if (const auto *C = dyn_cast<Constant>(V)) | ||||||||||||||||||||
855 | CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowUndefs)); | ||||||||||||||||||||
856 | |||||||||||||||||||||
857 | return CI && APInt::isSameValue(CI->getValue(), Val); | ||||||||||||||||||||
858 | } | ||||||||||||||||||||
859 | }; | ||||||||||||||||||||
860 | |||||||||||||||||||||
861 | /// Match a specific integer value or vector with all elements equal to | ||||||||||||||||||||
862 | /// the value. | ||||||||||||||||||||
863 | inline specific_intval<false> m_SpecificInt(APInt V) { | ||||||||||||||||||||
864 | return specific_intval<false>(std::move(V)); | ||||||||||||||||||||
865 | } | ||||||||||||||||||||
866 | |||||||||||||||||||||
867 | inline specific_intval<false> m_SpecificInt(uint64_t V) { | ||||||||||||||||||||
868 | return m_SpecificInt(APInt(64, V)); | ||||||||||||||||||||
869 | } | ||||||||||||||||||||
870 | |||||||||||||||||||||
871 | inline specific_intval<true> m_SpecificIntAllowUndef(APInt V) { | ||||||||||||||||||||
872 | return specific_intval<true>(std::move(V)); | ||||||||||||||||||||
873 | } | ||||||||||||||||||||
874 | |||||||||||||||||||||
875 | inline specific_intval<true> m_SpecificIntAllowUndef(uint64_t V) { | ||||||||||||||||||||
876 | return m_SpecificIntAllowUndef(APInt(64, V)); | ||||||||||||||||||||
877 | } | ||||||||||||||||||||
878 | |||||||||||||||||||||
879 | /// Match a ConstantInt and bind to its value. This does not match | ||||||||||||||||||||
880 | /// ConstantInts wider than 64-bits. | ||||||||||||||||||||
881 | inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; } | ||||||||||||||||||||
882 | |||||||||||||||||||||
883 | /// Match a specified basic block value. | ||||||||||||||||||||
884 | struct specific_bbval { | ||||||||||||||||||||
885 | BasicBlock *Val; | ||||||||||||||||||||
886 | |||||||||||||||||||||
887 | specific_bbval(BasicBlock *Val) : Val(Val) {} | ||||||||||||||||||||
888 | |||||||||||||||||||||
889 | template <typename ITy> bool match(ITy *V) { | ||||||||||||||||||||
890 | const auto *BB = dyn_cast<BasicBlock>(V); | ||||||||||||||||||||
891 | return BB && BB == Val; | ||||||||||||||||||||
892 | } | ||||||||||||||||||||
893 | }; | ||||||||||||||||||||
894 | |||||||||||||||||||||
895 | /// Match a specific basic block value. | ||||||||||||||||||||
896 | inline specific_bbval m_SpecificBB(BasicBlock *BB) { | ||||||||||||||||||||
897 | return specific_bbval(BB); | ||||||||||||||||||||
898 | } | ||||||||||||||||||||
899 | |||||||||||||||||||||
900 | /// A commutative-friendly version of m_Specific(). | ||||||||||||||||||||
901 | inline deferredval_ty<BasicBlock> m_Deferred(BasicBlock *const &BB) { | ||||||||||||||||||||
902 | return BB; | ||||||||||||||||||||
903 | } | ||||||||||||||||||||
904 | inline deferredval_ty<const BasicBlock> | ||||||||||||||||||||
905 | m_Deferred(const BasicBlock *const &BB) { | ||||||||||||||||||||
906 | return BB; | ||||||||||||||||||||
907 | } | ||||||||||||||||||||
908 | |||||||||||||||||||||
909 | //===----------------------------------------------------------------------===// | ||||||||||||||||||||
910 | // Matcher for any binary operator. | ||||||||||||||||||||
911 | // | ||||||||||||||||||||
912 | template <typename LHS_t, typename RHS_t, bool Commutable = false> | ||||||||||||||||||||
913 | struct AnyBinaryOp_match { | ||||||||||||||||||||
914 | LHS_t L; | ||||||||||||||||||||
915 | RHS_t R; | ||||||||||||||||||||
916 | |||||||||||||||||||||
917 | // The evaluation order is always stable, regardless of Commutability. | ||||||||||||||||||||
918 | // The LHS is always matched first. | ||||||||||||||||||||
919 | AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} | ||||||||||||||||||||
920 | |||||||||||||||||||||
921 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
922 | if (auto *I = dyn_cast<BinaryOperator>(V)) | ||||||||||||||||||||
923 | return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) || | ||||||||||||||||||||
924 | (Commutable && L.match(I->getOperand(1)) && | ||||||||||||||||||||
925 | R.match(I->getOperand(0))); | ||||||||||||||||||||
926 | return false; | ||||||||||||||||||||
927 | } | ||||||||||||||||||||
928 | }; | ||||||||||||||||||||
929 | |||||||||||||||||||||
930 | template <typename LHS, typename RHS> | ||||||||||||||||||||
931 | inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) { | ||||||||||||||||||||
932 | return AnyBinaryOp_match<LHS, RHS>(L, R); | ||||||||||||||||||||
933 | } | ||||||||||||||||||||
934 | |||||||||||||||||||||
935 | //===----------------------------------------------------------------------===// | ||||||||||||||||||||
936 | // Matcher for any unary operator. | ||||||||||||||||||||
937 | // TODO fuse unary, binary matcher into n-ary matcher | ||||||||||||||||||||
938 | // | ||||||||||||||||||||
939 | template <typename OP_t> struct AnyUnaryOp_match { | ||||||||||||||||||||
940 | OP_t X; | ||||||||||||||||||||
941 | |||||||||||||||||||||
942 | AnyUnaryOp_match(const OP_t &X) : X(X) {} | ||||||||||||||||||||
943 | |||||||||||||||||||||
944 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
945 | if (auto *I = dyn_cast<UnaryOperator>(V)) | ||||||||||||||||||||
946 | return X.match(I->getOperand(0)); | ||||||||||||||||||||
947 | return false; | ||||||||||||||||||||
948 | } | ||||||||||||||||||||
949 | }; | ||||||||||||||||||||
950 | |||||||||||||||||||||
951 | template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) { | ||||||||||||||||||||
952 | return AnyUnaryOp_match<OP_t>(X); | ||||||||||||||||||||
953 | } | ||||||||||||||||||||
954 | |||||||||||||||||||||
955 | //===----------------------------------------------------------------------===// | ||||||||||||||||||||
956 | // Matchers for specific binary operators. | ||||||||||||||||||||
957 | // | ||||||||||||||||||||
958 | |||||||||||||||||||||
959 | template <typename LHS_t, typename RHS_t, unsigned Opcode, | ||||||||||||||||||||
960 | bool Commutable = false> | ||||||||||||||||||||
961 | struct BinaryOp_match { | ||||||||||||||||||||
962 | LHS_t L; | ||||||||||||||||||||
963 | RHS_t R; | ||||||||||||||||||||
964 | |||||||||||||||||||||
965 | // The evaluation order is always stable, regardless of Commutability. | ||||||||||||||||||||
966 | // The LHS is always matched first. | ||||||||||||||||||||
967 | BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} | ||||||||||||||||||||
968 | |||||||||||||||||||||
969 | template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) { | ||||||||||||||||||||
970 | if (V->getValueID() == Value::InstructionVal + Opc) { | ||||||||||||||||||||
971 | auto *I = cast<BinaryOperator>(V); | ||||||||||||||||||||
972 | return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) || | ||||||||||||||||||||
973 | (Commutable && L.match(I->getOperand(1)) && | ||||||||||||||||||||
974 | R.match(I->getOperand(0))); | ||||||||||||||||||||
975 | } | ||||||||||||||||||||
976 | if (auto *CE = dyn_cast<ConstantExpr>(V)) | ||||||||||||||||||||
977 | return CE->getOpcode() == Opc && | ||||||||||||||||||||
978 | ((L.match(CE->getOperand(0)) && R.match(CE->getOperand(1))) || | ||||||||||||||||||||
979 | (Commutable && L.match(CE->getOperand(1)) && | ||||||||||||||||||||
980 | R.match(CE->getOperand(0)))); | ||||||||||||||||||||
981 | return false; | ||||||||||||||||||||
982 | } | ||||||||||||||||||||
983 | |||||||||||||||||||||
984 | template <typename OpTy> bool match(OpTy *V) { return match(Opcode, V); } | ||||||||||||||||||||
985 | }; | ||||||||||||||||||||
986 | |||||||||||||||||||||
987 | template <typename LHS, typename RHS> | ||||||||||||||||||||
988 | inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L, | ||||||||||||||||||||
989 | const RHS &R) { | ||||||||||||||||||||
990 | return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R); | ||||||||||||||||||||
991 | } | ||||||||||||||||||||
992 | |||||||||||||||||||||
993 | template <typename LHS, typename RHS> | ||||||||||||||||||||
994 | inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L, | ||||||||||||||||||||
995 | const RHS &R) { | ||||||||||||||||||||
996 | return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R); | ||||||||||||||||||||
997 | } | ||||||||||||||||||||
998 | |||||||||||||||||||||
999 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1000 | inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L, | ||||||||||||||||||||
1001 | const RHS &R) { | ||||||||||||||||||||
1002 | return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R); | ||||||||||||||||||||
1003 | } | ||||||||||||||||||||
1004 | |||||||||||||||||||||
1005 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1006 | inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L, | ||||||||||||||||||||
1007 | const RHS &R) { | ||||||||||||||||||||
1008 | return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R); | ||||||||||||||||||||
1009 | } | ||||||||||||||||||||
1010 | |||||||||||||||||||||
1011 | template <typename Op_t> struct FNeg_match { | ||||||||||||||||||||
1012 | Op_t X; | ||||||||||||||||||||
1013 | |||||||||||||||||||||
1014 | FNeg_match(const Op_t &Op) : X(Op) {} | ||||||||||||||||||||
1015 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
1016 | auto *FPMO = dyn_cast<FPMathOperator>(V); | ||||||||||||||||||||
1017 | if (!FPMO) return false; | ||||||||||||||||||||
1018 | |||||||||||||||||||||
1019 | if (FPMO->getOpcode() == Instruction::FNeg) | ||||||||||||||||||||
1020 | return X.match(FPMO->getOperand(0)); | ||||||||||||||||||||
1021 | |||||||||||||||||||||
1022 | if (FPMO->getOpcode() == Instruction::FSub) { | ||||||||||||||||||||
1023 | if (FPMO->hasNoSignedZeros()) { | ||||||||||||||||||||
1024 | // With 'nsz', any zero goes. | ||||||||||||||||||||
1025 | if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0))) | ||||||||||||||||||||
1026 | return false; | ||||||||||||||||||||
1027 | } else { | ||||||||||||||||||||
1028 | // Without 'nsz', we need fsub -0.0, X exactly. | ||||||||||||||||||||
1029 | if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0))) | ||||||||||||||||||||
1030 | return false; | ||||||||||||||||||||
1031 | } | ||||||||||||||||||||
1032 | |||||||||||||||||||||
1033 | return X.match(FPMO->getOperand(1)); | ||||||||||||||||||||
1034 | } | ||||||||||||||||||||
1035 | |||||||||||||||||||||
1036 | return false; | ||||||||||||||||||||
1037 | } | ||||||||||||||||||||
1038 | }; | ||||||||||||||||||||
1039 | |||||||||||||||||||||
1040 | /// Match 'fneg X' as 'fsub -0.0, X'. | ||||||||||||||||||||
1041 | template <typename OpTy> | ||||||||||||||||||||
1042 | inline FNeg_match<OpTy> | ||||||||||||||||||||
1043 | m_FNeg(const OpTy &X) { | ||||||||||||||||||||
1044 | return FNeg_match<OpTy>(X); | ||||||||||||||||||||
1045 | } | ||||||||||||||||||||
1046 | |||||||||||||||||||||
1047 | /// Match 'fneg X' as 'fsub +-0.0, X'. | ||||||||||||||||||||
1048 | template <typename RHS> | ||||||||||||||||||||
1049 | inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub> | ||||||||||||||||||||
1050 | m_FNegNSZ(const RHS &X) { | ||||||||||||||||||||
1051 | return m_FSub(m_AnyZeroFP(), X); | ||||||||||||||||||||
1052 | } | ||||||||||||||||||||
1053 | |||||||||||||||||||||
1054 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1055 | inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L, | ||||||||||||||||||||
1056 | const RHS &R) { | ||||||||||||||||||||
1057 | return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R); | ||||||||||||||||||||
1058 | } | ||||||||||||||||||||
1059 | |||||||||||||||||||||
1060 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1061 | inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L, | ||||||||||||||||||||
1062 | const RHS &R) { | ||||||||||||||||||||
1063 | return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R); | ||||||||||||||||||||
1064 | } | ||||||||||||||||||||
1065 | |||||||||||||||||||||
1066 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1067 | inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L, | ||||||||||||||||||||
1068 | const RHS &R) { | ||||||||||||||||||||
1069 | return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R); | ||||||||||||||||||||
1070 | } | ||||||||||||||||||||
1071 | |||||||||||||||||||||
1072 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1073 | inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L, | ||||||||||||||||||||
1074 | const RHS &R) { | ||||||||||||||||||||
1075 | return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R); | ||||||||||||||||||||
1076 | } | ||||||||||||||||||||
1077 | |||||||||||||||||||||
1078 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1079 | inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L, | ||||||||||||||||||||
1080 | const RHS &R) { | ||||||||||||||||||||
1081 | return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R); | ||||||||||||||||||||
1082 | } | ||||||||||||||||||||
1083 | |||||||||||||||||||||
1084 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1085 | inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L, | ||||||||||||||||||||
1086 | const RHS &R) { | ||||||||||||||||||||
1087 | return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R); | ||||||||||||||||||||
1088 | } | ||||||||||||||||||||
1089 | |||||||||||||||||||||
1090 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1091 | inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L, | ||||||||||||||||||||
1092 | const RHS &R) { | ||||||||||||||||||||
1093 | return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R); | ||||||||||||||||||||
1094 | } | ||||||||||||||||||||
1095 | |||||||||||||||||||||
1096 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1097 | inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L, | ||||||||||||||||||||
1098 | const RHS &R) { | ||||||||||||||||||||
1099 | return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R); | ||||||||||||||||||||
1100 | } | ||||||||||||||||||||
1101 | |||||||||||||||||||||
1102 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1103 | inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L, | ||||||||||||||||||||
1104 | const RHS &R) { | ||||||||||||||||||||
1105 | return BinaryOp_match<LHS, RHS, Instruction::And>(L, R); | ||||||||||||||||||||
1106 | } | ||||||||||||||||||||
1107 | |||||||||||||||||||||
1108 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1109 | inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L, | ||||||||||||||||||||
1110 | const RHS &R) { | ||||||||||||||||||||
1111 | return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R); | ||||||||||||||||||||
1112 | } | ||||||||||||||||||||
1113 | |||||||||||||||||||||
1114 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1115 | inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L, | ||||||||||||||||||||
1116 | const RHS &R) { | ||||||||||||||||||||
1117 | return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R); | ||||||||||||||||||||
1118 | } | ||||||||||||||||||||
1119 | |||||||||||||||||||||
1120 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1121 | inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L, | ||||||||||||||||||||
1122 | const RHS &R) { | ||||||||||||||||||||
1123 | return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R); | ||||||||||||||||||||
1124 | } | ||||||||||||||||||||
1125 | |||||||||||||||||||||
1126 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1127 | inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L, | ||||||||||||||||||||
1128 | const RHS &R) { | ||||||||||||||||||||
1129 | return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R); | ||||||||||||||||||||
1130 | } | ||||||||||||||||||||
1131 | |||||||||||||||||||||
1132 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1133 | inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L, | ||||||||||||||||||||
1134 | const RHS &R) { | ||||||||||||||||||||
1135 | return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R); | ||||||||||||||||||||
1136 | } | ||||||||||||||||||||
1137 | |||||||||||||||||||||
1138 | template <typename LHS_t, typename RHS_t, unsigned Opcode, | ||||||||||||||||||||
1139 | unsigned WrapFlags = 0> | ||||||||||||||||||||
1140 | struct OverflowingBinaryOp_match { | ||||||||||||||||||||
1141 | LHS_t L; | ||||||||||||||||||||
1142 | RHS_t R; | ||||||||||||||||||||
1143 | |||||||||||||||||||||
1144 | OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) | ||||||||||||||||||||
1145 | : L(LHS), R(RHS) {} | ||||||||||||||||||||
1146 | |||||||||||||||||||||
1147 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
1148 | if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) { | ||||||||||||||||||||
1149 | if (Op->getOpcode() != Opcode) | ||||||||||||||||||||
1150 | return false; | ||||||||||||||||||||
1151 | if ((WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap) && | ||||||||||||||||||||
1152 | !Op->hasNoUnsignedWrap()) | ||||||||||||||||||||
1153 | return false; | ||||||||||||||||||||
1154 | if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) && | ||||||||||||||||||||
1155 | !Op->hasNoSignedWrap()) | ||||||||||||||||||||
1156 | return false; | ||||||||||||||||||||
1157 | return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1)); | ||||||||||||||||||||
1158 | } | ||||||||||||||||||||
1159 | return false; | ||||||||||||||||||||
1160 | } | ||||||||||||||||||||
1161 | }; | ||||||||||||||||||||
1162 | |||||||||||||||||||||
1163 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1164 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, | ||||||||||||||||||||
1165 | OverflowingBinaryOperator::NoSignedWrap> | ||||||||||||||||||||
1166 | m_NSWAdd(const LHS &L, const RHS &R) { | ||||||||||||||||||||
1167 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, | ||||||||||||||||||||
1168 | OverflowingBinaryOperator::NoSignedWrap>( | ||||||||||||||||||||
1169 | L, R); | ||||||||||||||||||||
1170 | } | ||||||||||||||||||||
1171 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1172 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, | ||||||||||||||||||||
1173 | OverflowingBinaryOperator::NoSignedWrap> | ||||||||||||||||||||
1174 | m_NSWSub(const LHS &L, const RHS &R) { | ||||||||||||||||||||
1175 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, | ||||||||||||||||||||
1176 | OverflowingBinaryOperator::NoSignedWrap>( | ||||||||||||||||||||
1177 | L, R); | ||||||||||||||||||||
1178 | } | ||||||||||||||||||||
1179 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1180 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, | ||||||||||||||||||||
1181 | OverflowingBinaryOperator::NoSignedWrap> | ||||||||||||||||||||
1182 | m_NSWMul(const LHS &L, const RHS &R) { | ||||||||||||||||||||
1183 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, | ||||||||||||||||||||
1184 | OverflowingBinaryOperator::NoSignedWrap>( | ||||||||||||||||||||
1185 | L, R); | ||||||||||||||||||||
1186 | } | ||||||||||||||||||||
1187 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1188 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, | ||||||||||||||||||||
1189 | OverflowingBinaryOperator::NoSignedWrap> | ||||||||||||||||||||
1190 | m_NSWShl(const LHS &L, const RHS &R) { | ||||||||||||||||||||
1191 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, | ||||||||||||||||||||
1192 | OverflowingBinaryOperator::NoSignedWrap>( | ||||||||||||||||||||
1193 | L, R); | ||||||||||||||||||||
1194 | } | ||||||||||||||||||||
1195 | |||||||||||||||||||||
1196 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1197 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, | ||||||||||||||||||||
1198 | OverflowingBinaryOperator::NoUnsignedWrap> | ||||||||||||||||||||
1199 | m_NUWAdd(const LHS &L, const RHS &R) { | ||||||||||||||||||||
1200 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, | ||||||||||||||||||||
1201 | OverflowingBinaryOperator::NoUnsignedWrap>( | ||||||||||||||||||||
1202 | L, R); | ||||||||||||||||||||
1203 | } | ||||||||||||||||||||
1204 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1205 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, | ||||||||||||||||||||
1206 | OverflowingBinaryOperator::NoUnsignedWrap> | ||||||||||||||||||||
1207 | m_NUWSub(const LHS &L, const RHS &R) { | ||||||||||||||||||||
1208 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, | ||||||||||||||||||||
1209 | OverflowingBinaryOperator::NoUnsignedWrap>( | ||||||||||||||||||||
1210 | L, R); | ||||||||||||||||||||
1211 | } | ||||||||||||||||||||
1212 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1213 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, | ||||||||||||||||||||
1214 | OverflowingBinaryOperator::NoUnsignedWrap> | ||||||||||||||||||||
1215 | m_NUWMul(const LHS &L, const RHS &R) { | ||||||||||||||||||||
1216 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, | ||||||||||||||||||||
1217 | OverflowingBinaryOperator::NoUnsignedWrap>( | ||||||||||||||||||||
1218 | L, R); | ||||||||||||||||||||
1219 | } | ||||||||||||||||||||
1220 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1221 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, | ||||||||||||||||||||
1222 | OverflowingBinaryOperator::NoUnsignedWrap> | ||||||||||||||||||||
1223 | m_NUWShl(const LHS &L, const RHS &R) { | ||||||||||||||||||||
1224 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, | ||||||||||||||||||||
1225 | OverflowingBinaryOperator::NoUnsignedWrap>( | ||||||||||||||||||||
1226 | L, R); | ||||||||||||||||||||
1227 | } | ||||||||||||||||||||
1228 | |||||||||||||||||||||
1229 | template <typename LHS_t, typename RHS_t, bool Commutable = false> | ||||||||||||||||||||
1230 | struct SpecificBinaryOp_match | ||||||||||||||||||||
1231 | : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> { | ||||||||||||||||||||
1232 | unsigned Opcode; | ||||||||||||||||||||
1233 | |||||||||||||||||||||
1234 | SpecificBinaryOp_match(unsigned Opcode, const LHS_t &LHS, const RHS_t &RHS) | ||||||||||||||||||||
1235 | : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {} | ||||||||||||||||||||
1236 | |||||||||||||||||||||
1237 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
1238 | return BinaryOp_match<LHS_t, RHS_t, 0, Commutable>::match(Opcode, V); | ||||||||||||||||||||
1239 | } | ||||||||||||||||||||
1240 | }; | ||||||||||||||||||||
1241 | |||||||||||||||||||||
1242 | /// Matches a specific opcode. | ||||||||||||||||||||
1243 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1244 | inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L, | ||||||||||||||||||||
1245 | const RHS &R) { | ||||||||||||||||||||
1246 | return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R); | ||||||||||||||||||||
1247 | } | ||||||||||||||||||||
1248 | |||||||||||||||||||||
1249 | //===----------------------------------------------------------------------===// | ||||||||||||||||||||
1250 | // Class that matches a group of binary opcodes. | ||||||||||||||||||||
1251 | // | ||||||||||||||||||||
1252 | template <typename LHS_t, typename RHS_t, typename Predicate> | ||||||||||||||||||||
1253 | struct BinOpPred_match : Predicate { | ||||||||||||||||||||
1254 | LHS_t L; | ||||||||||||||||||||
1255 | RHS_t R; | ||||||||||||||||||||
1256 | |||||||||||||||||||||
1257 | BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} | ||||||||||||||||||||
1258 | |||||||||||||||||||||
1259 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
1260 | if (auto *I = dyn_cast<Instruction>(V)) | ||||||||||||||||||||
1261 | return this->isOpType(I->getOpcode()) && L.match(I->getOperand(0)) && | ||||||||||||||||||||
1262 | R.match(I->getOperand(1)); | ||||||||||||||||||||
1263 | if (auto *CE = dyn_cast<ConstantExpr>(V)) | ||||||||||||||||||||
1264 | return this->isOpType(CE->getOpcode()) && L.match(CE->getOperand(0)) && | ||||||||||||||||||||
1265 | R.match(CE->getOperand(1)); | ||||||||||||||||||||
1266 | return false; | ||||||||||||||||||||
1267 | } | ||||||||||||||||||||
1268 | }; | ||||||||||||||||||||
1269 | |||||||||||||||||||||
1270 | struct is_shift_op { | ||||||||||||||||||||
1271 | bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); } | ||||||||||||||||||||
1272 | }; | ||||||||||||||||||||
1273 | |||||||||||||||||||||
1274 | struct is_right_shift_op { | ||||||||||||||||||||
1275 | bool isOpType(unsigned Opcode) { | ||||||||||||||||||||
1276 | return Opcode == Instruction::LShr || Opcode == Instruction::AShr; | ||||||||||||||||||||
1277 | } | ||||||||||||||||||||
1278 | }; | ||||||||||||||||||||
1279 | |||||||||||||||||||||
1280 | struct is_logical_shift_op { | ||||||||||||||||||||
1281 | bool isOpType(unsigned Opcode) { | ||||||||||||||||||||
1282 | return Opcode == Instruction::LShr || Opcode == Instruction::Shl; | ||||||||||||||||||||
1283 | } | ||||||||||||||||||||
1284 | }; | ||||||||||||||||||||
1285 | |||||||||||||||||||||
1286 | struct is_bitwiselogic_op { | ||||||||||||||||||||
1287 | bool isOpType(unsigned Opcode) { | ||||||||||||||||||||
1288 | return Instruction::isBitwiseLogicOp(Opcode); | ||||||||||||||||||||
1289 | } | ||||||||||||||||||||
1290 | }; | ||||||||||||||||||||
1291 | |||||||||||||||||||||
1292 | struct is_idiv_op { | ||||||||||||||||||||
1293 | bool isOpType(unsigned Opcode) { | ||||||||||||||||||||
1294 | return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv; | ||||||||||||||||||||
1295 | } | ||||||||||||||||||||
1296 | }; | ||||||||||||||||||||
1297 | |||||||||||||||||||||
1298 | struct is_irem_op { | ||||||||||||||||||||
1299 | bool isOpType(unsigned Opcode) { | ||||||||||||||||||||
1300 | return Opcode == Instruction::SRem || Opcode == Instruction::URem; | ||||||||||||||||||||
1301 | } | ||||||||||||||||||||
1302 | }; | ||||||||||||||||||||
1303 | |||||||||||||||||||||
1304 | /// Matches shift operations. | ||||||||||||||||||||
1305 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1306 | inline BinOpPred_match<LHS, RHS, is_shift_op> m_Shift(const LHS &L, | ||||||||||||||||||||
1307 | const RHS &R) { | ||||||||||||||||||||
1308 | return BinOpPred_match<LHS, RHS, is_shift_op>(L, R); | ||||||||||||||||||||
1309 | } | ||||||||||||||||||||
1310 | |||||||||||||||||||||
1311 | /// Matches logical shift operations. | ||||||||||||||||||||
1312 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1313 | inline BinOpPred_match<LHS, RHS, is_right_shift_op> m_Shr(const LHS &L, | ||||||||||||||||||||
1314 | const RHS &R) { | ||||||||||||||||||||
1315 | return BinOpPred_match<LHS, RHS, is_right_shift_op>(L, R); | ||||||||||||||||||||
1316 | } | ||||||||||||||||||||
1317 | |||||||||||||||||||||
1318 | /// Matches logical shift operations. | ||||||||||||||||||||
1319 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1320 | inline BinOpPred_match<LHS, RHS, is_logical_shift_op> | ||||||||||||||||||||
1321 | m_LogicalShift(const LHS &L, const RHS &R) { | ||||||||||||||||||||
1322 | return BinOpPred_match<LHS, RHS, is_logical_shift_op>(L, R); | ||||||||||||||||||||
1323 | } | ||||||||||||||||||||
1324 | |||||||||||||||||||||
1325 | /// Matches bitwise logic operations. | ||||||||||||||||||||
1326 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1327 | inline BinOpPred_match<LHS, RHS, is_bitwiselogic_op> | ||||||||||||||||||||
1328 | m_BitwiseLogic(const LHS &L, const RHS &R) { | ||||||||||||||||||||
1329 | return BinOpPred_match<LHS, RHS, is_bitwiselogic_op>(L, R); | ||||||||||||||||||||
1330 | } | ||||||||||||||||||||
1331 | |||||||||||||||||||||
1332 | /// Matches integer division operations. | ||||||||||||||||||||
1333 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1334 | inline BinOpPred_match<LHS, RHS, is_idiv_op> m_IDiv(const LHS &L, | ||||||||||||||||||||
1335 | const RHS &R) { | ||||||||||||||||||||
1336 | return BinOpPred_match<LHS, RHS, is_idiv_op>(L, R); | ||||||||||||||||||||
1337 | } | ||||||||||||||||||||
1338 | |||||||||||||||||||||
1339 | /// Matches integer remainder operations. | ||||||||||||||||||||
1340 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1341 | inline BinOpPred_match<LHS, RHS, is_irem_op> m_IRem(const LHS &L, | ||||||||||||||||||||
1342 | const RHS &R) { | ||||||||||||||||||||
1343 | return BinOpPred_match<LHS, RHS, is_irem_op>(L, R); | ||||||||||||||||||||
1344 | } | ||||||||||||||||||||
1345 | |||||||||||||||||||||
1346 | //===----------------------------------------------------------------------===// | ||||||||||||||||||||
1347 | // Class that matches exact binary ops. | ||||||||||||||||||||
1348 | // | ||||||||||||||||||||
1349 | template <typename SubPattern_t> struct Exact_match { | ||||||||||||||||||||
1350 | SubPattern_t SubPattern; | ||||||||||||||||||||
1351 | |||||||||||||||||||||
1352 | Exact_match(const SubPattern_t &SP) : SubPattern(SP) {} | ||||||||||||||||||||
1353 | |||||||||||||||||||||
1354 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
1355 | if (auto *PEO = dyn_cast<PossiblyExactOperator>(V)) | ||||||||||||||||||||
1356 | return PEO->isExact() && SubPattern.match(V); | ||||||||||||||||||||
1357 | return false; | ||||||||||||||||||||
1358 | } | ||||||||||||||||||||
1359 | }; | ||||||||||||||||||||
1360 | |||||||||||||||||||||
1361 | template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) { | ||||||||||||||||||||
1362 | return SubPattern; | ||||||||||||||||||||
1363 | } | ||||||||||||||||||||
1364 | |||||||||||||||||||||
1365 | //===----------------------------------------------------------------------===// | ||||||||||||||||||||
1366 | // Matchers for CmpInst classes | ||||||||||||||||||||
1367 | // | ||||||||||||||||||||
1368 | |||||||||||||||||||||
1369 | template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy, | ||||||||||||||||||||
1370 | bool Commutable = false> | ||||||||||||||||||||
1371 | struct CmpClass_match { | ||||||||||||||||||||
1372 | PredicateTy &Predicate; | ||||||||||||||||||||
1373 | LHS_t L; | ||||||||||||||||||||
1374 | RHS_t R; | ||||||||||||||||||||
1375 | |||||||||||||||||||||
1376 | // The evaluation order is always stable, regardless of Commutability. | ||||||||||||||||||||
1377 | // The LHS is always matched first. | ||||||||||||||||||||
1378 | CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS) | ||||||||||||||||||||
1379 | : Predicate(Pred), L(LHS), R(RHS) {} | ||||||||||||||||||||
1380 | |||||||||||||||||||||
1381 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
1382 | if (auto *I = dyn_cast<Class>(V)) { | ||||||||||||||||||||
1383 | if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) { | ||||||||||||||||||||
1384 | Predicate = I->getPredicate(); | ||||||||||||||||||||
1385 | return true; | ||||||||||||||||||||
1386 | } else if (Commutable && L.match(I->getOperand(1)) && | ||||||||||||||||||||
1387 | R.match(I->getOperand(0))) { | ||||||||||||||||||||
1388 | Predicate = I->getSwappedPredicate(); | ||||||||||||||||||||
1389 | return true; | ||||||||||||||||||||
1390 | } | ||||||||||||||||||||
1391 | } | ||||||||||||||||||||
1392 | return false; | ||||||||||||||||||||
1393 | } | ||||||||||||||||||||
1394 | }; | ||||||||||||||||||||
1395 | |||||||||||||||||||||
1396 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1397 | inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate> | ||||||||||||||||||||
1398 | m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) { | ||||||||||||||||||||
1399 | return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R); | ||||||||||||||||||||
1400 | } | ||||||||||||||||||||
1401 | |||||||||||||||||||||
1402 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1403 | inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate> | ||||||||||||||||||||
1404 | m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) { | ||||||||||||||||||||
1405 | return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R); | ||||||||||||||||||||
1406 | } | ||||||||||||||||||||
1407 | |||||||||||||||||||||
1408 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1409 | inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate> | ||||||||||||||||||||
1410 | m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) { | ||||||||||||||||||||
1411 | return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R); | ||||||||||||||||||||
1412 | } | ||||||||||||||||||||
1413 | |||||||||||||||||||||
1414 | //===----------------------------------------------------------------------===// | ||||||||||||||||||||
1415 | // Matchers for instructions with a given opcode and number of operands. | ||||||||||||||||||||
1416 | // | ||||||||||||||||||||
1417 | |||||||||||||||||||||
1418 | /// Matches instructions with Opcode and three operands. | ||||||||||||||||||||
1419 | template <typename T0, unsigned Opcode> struct OneOps_match { | ||||||||||||||||||||
1420 | T0 Op1; | ||||||||||||||||||||
1421 | |||||||||||||||||||||
1422 | OneOps_match(const T0 &Op1) : Op1(Op1) {} | ||||||||||||||||||||
1423 | |||||||||||||||||||||
1424 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
1425 | if (V->getValueID() == Value::InstructionVal + Opcode) { | ||||||||||||||||||||
1426 | auto *I = cast<Instruction>(V); | ||||||||||||||||||||
1427 | return Op1.match(I->getOperand(0)); | ||||||||||||||||||||
1428 | } | ||||||||||||||||||||
1429 | return false; | ||||||||||||||||||||
1430 | } | ||||||||||||||||||||
1431 | }; | ||||||||||||||||||||
1432 | |||||||||||||||||||||
1433 | /// Matches instructions with Opcode and three operands. | ||||||||||||||||||||
1434 | template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match { | ||||||||||||||||||||
1435 | T0 Op1; | ||||||||||||||||||||
1436 | T1 Op2; | ||||||||||||||||||||
1437 | |||||||||||||||||||||
1438 | TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {} | ||||||||||||||||||||
1439 | |||||||||||||||||||||
1440 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
1441 | if (V->getValueID() == Value::InstructionVal + Opcode) { | ||||||||||||||||||||
1442 | auto *I = cast<Instruction>(V); | ||||||||||||||||||||
1443 | return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)); | ||||||||||||||||||||
1444 | } | ||||||||||||||||||||
1445 | return false; | ||||||||||||||||||||
1446 | } | ||||||||||||||||||||
1447 | }; | ||||||||||||||||||||
1448 | |||||||||||||||||||||
1449 | /// Matches instructions with Opcode and three operands. | ||||||||||||||||||||
1450 | template <typename T0, typename T1, typename T2, unsigned Opcode> | ||||||||||||||||||||
1451 | struct ThreeOps_match { | ||||||||||||||||||||
1452 | T0 Op1; | ||||||||||||||||||||
1453 | T1 Op2; | ||||||||||||||||||||
1454 | T2 Op3; | ||||||||||||||||||||
1455 | |||||||||||||||||||||
1456 | ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3) | ||||||||||||||||||||
1457 | : Op1(Op1), Op2(Op2), Op3(Op3) {} | ||||||||||||||||||||
1458 | |||||||||||||||||||||
1459 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
1460 | if (V->getValueID() == Value::InstructionVal + Opcode) { | ||||||||||||||||||||
1461 | auto *I = cast<Instruction>(V); | ||||||||||||||||||||
1462 | return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) && | ||||||||||||||||||||
1463 | Op3.match(I->getOperand(2)); | ||||||||||||||||||||
1464 | } | ||||||||||||||||||||
1465 | return false; | ||||||||||||||||||||
1466 | } | ||||||||||||||||||||
1467 | }; | ||||||||||||||||||||
1468 | |||||||||||||||||||||
1469 | /// Matches SelectInst. | ||||||||||||||||||||
1470 | template <typename Cond, typename LHS, typename RHS> | ||||||||||||||||||||
1471 | inline ThreeOps_match<Cond, LHS, RHS, Instruction::Select> | ||||||||||||||||||||
1472 | m_Select(const Cond &C, const LHS &L, const RHS &R) { | ||||||||||||||||||||
1473 | return ThreeOps_match<Cond, LHS, RHS, Instruction::Select>(C, L, R); | ||||||||||||||||||||
1474 | } | ||||||||||||||||||||
1475 | |||||||||||||||||||||
1476 | /// This matches a select of two constants, e.g.: | ||||||||||||||||||||
1477 | /// m_SelectCst<-1, 0>(m_Value(V)) | ||||||||||||||||||||
1478 | template <int64_t L, int64_t R, typename Cond> | ||||||||||||||||||||
1479 | inline ThreeOps_match<Cond, constantint_match<L>, constantint_match<R>, | ||||||||||||||||||||
1480 | Instruction::Select> | ||||||||||||||||||||
1481 | m_SelectCst(const Cond &C) { | ||||||||||||||||||||
1482 | return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>()); | ||||||||||||||||||||
1483 | } | ||||||||||||||||||||
1484 | |||||||||||||||||||||
1485 | /// Matches FreezeInst. | ||||||||||||||||||||
1486 | template <typename OpTy> | ||||||||||||||||||||
1487 | inline OneOps_match<OpTy, Instruction::Freeze> m_Freeze(const OpTy &Op) { | ||||||||||||||||||||
1488 | return OneOps_match<OpTy, Instruction::Freeze>(Op); | ||||||||||||||||||||
1489 | } | ||||||||||||||||||||
1490 | |||||||||||||||||||||
1491 | /// Matches InsertElementInst. | ||||||||||||||||||||
1492 | template <typename Val_t, typename Elt_t, typename Idx_t> | ||||||||||||||||||||
1493 | inline ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement> | ||||||||||||||||||||
1494 | m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) { | ||||||||||||||||||||
1495 | return ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>( | ||||||||||||||||||||
1496 | Val, Elt, Idx); | ||||||||||||||||||||
1497 | } | ||||||||||||||||||||
1498 | |||||||||||||||||||||
1499 | /// Matches ExtractElementInst. | ||||||||||||||||||||
1500 | template <typename Val_t, typename Idx_t> | ||||||||||||||||||||
1501 | inline TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement> | ||||||||||||||||||||
1502 | m_ExtractElt(const Val_t &Val, const Idx_t &Idx) { | ||||||||||||||||||||
1503 | return TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>(Val, Idx); | ||||||||||||||||||||
1504 | } | ||||||||||||||||||||
1505 | |||||||||||||||||||||
1506 | /// Matches shuffle. | ||||||||||||||||||||
1507 | template <typename T0, typename T1, typename T2> struct Shuffle_match { | ||||||||||||||||||||
1508 | T0 Op1; | ||||||||||||||||||||
1509 | T1 Op2; | ||||||||||||||||||||
1510 | T2 Mask; | ||||||||||||||||||||
1511 | |||||||||||||||||||||
1512 | Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask) | ||||||||||||||||||||
1513 | : Op1(Op1), Op2(Op2), Mask(Mask) {} | ||||||||||||||||||||
1514 | |||||||||||||||||||||
1515 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
1516 | if (auto *I = dyn_cast<ShuffleVectorInst>(V)) { | ||||||||||||||||||||
1517 | return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) && | ||||||||||||||||||||
1518 | Mask.match(I->getShuffleMask()); | ||||||||||||||||||||
1519 | } | ||||||||||||||||||||
1520 | return false; | ||||||||||||||||||||
1521 | } | ||||||||||||||||||||
1522 | }; | ||||||||||||||||||||
1523 | |||||||||||||||||||||
1524 | struct m_Mask { | ||||||||||||||||||||
1525 | ArrayRef<int> &MaskRef; | ||||||||||||||||||||
1526 | m_Mask(ArrayRef<int> &MaskRef) : MaskRef(MaskRef) {} | ||||||||||||||||||||
1527 | bool match(ArrayRef<int> Mask) { | ||||||||||||||||||||
1528 | MaskRef = Mask; | ||||||||||||||||||||
1529 | return true; | ||||||||||||||||||||
1530 | } | ||||||||||||||||||||
1531 | }; | ||||||||||||||||||||
1532 | |||||||||||||||||||||
1533 | struct m_ZeroMask { | ||||||||||||||||||||
1534 | bool match(ArrayRef<int> Mask) { | ||||||||||||||||||||
1535 | return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; }); | ||||||||||||||||||||
1536 | } | ||||||||||||||||||||
1537 | }; | ||||||||||||||||||||
1538 | |||||||||||||||||||||
1539 | struct m_SpecificMask { | ||||||||||||||||||||
1540 | ArrayRef<int> &MaskRef; | ||||||||||||||||||||
1541 | m_SpecificMask(ArrayRef<int> &MaskRef) : MaskRef(MaskRef) {} | ||||||||||||||||||||
1542 | bool match(ArrayRef<int> Mask) { return MaskRef == Mask; } | ||||||||||||||||||||
1543 | }; | ||||||||||||||||||||
1544 | |||||||||||||||||||||
1545 | struct m_SplatOrUndefMask { | ||||||||||||||||||||
1546 | int &SplatIndex; | ||||||||||||||||||||
1547 | m_SplatOrUndefMask(int &SplatIndex) : SplatIndex(SplatIndex) {} | ||||||||||||||||||||
1548 | bool match(ArrayRef<int> Mask) { | ||||||||||||||||||||
1549 | auto First = find_if(Mask, [](int Elem) { return Elem != -1; }); | ||||||||||||||||||||
1550 | if (First == Mask.end()) | ||||||||||||||||||||
1551 | return false; | ||||||||||||||||||||
1552 | SplatIndex = *First; | ||||||||||||||||||||
1553 | return all_of(Mask, | ||||||||||||||||||||
1554 | [First](int Elem) { return Elem == *First || Elem == -1; }); | ||||||||||||||||||||
1555 | } | ||||||||||||||||||||
1556 | }; | ||||||||||||||||||||
1557 | |||||||||||||||||||||
1558 | /// Matches ShuffleVectorInst independently of mask value. | ||||||||||||||||||||
1559 | template <typename V1_t, typename V2_t> | ||||||||||||||||||||
1560 | inline TwoOps_match<V1_t, V2_t, Instruction::ShuffleVector> | ||||||||||||||||||||
1561 | m_Shuffle(const V1_t &v1, const V2_t &v2) { | ||||||||||||||||||||
1562 | return TwoOps_match<V1_t, V2_t, Instruction::ShuffleVector>(v1, v2); | ||||||||||||||||||||
1563 | } | ||||||||||||||||||||
1564 | |||||||||||||||||||||
1565 | template <typename V1_t, typename V2_t, typename Mask_t> | ||||||||||||||||||||
1566 | inline Shuffle_match<V1_t, V2_t, Mask_t> | ||||||||||||||||||||
1567 | m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) { | ||||||||||||||||||||
1568 | return Shuffle_match<V1_t, V2_t, Mask_t>(v1, v2, mask); | ||||||||||||||||||||
1569 | } | ||||||||||||||||||||
1570 | |||||||||||||||||||||
1571 | /// Matches LoadInst. | ||||||||||||||||||||
1572 | template <typename OpTy> | ||||||||||||||||||||
1573 | inline OneOps_match<OpTy, Instruction::Load> m_Load(const OpTy &Op) { | ||||||||||||||||||||
1574 | return OneOps_match<OpTy, Instruction::Load>(Op); | ||||||||||||||||||||
1575 | } | ||||||||||||||||||||
1576 | |||||||||||||||||||||
1577 | /// Matches StoreInst. | ||||||||||||||||||||
1578 | template <typename ValueOpTy, typename PointerOpTy> | ||||||||||||||||||||
1579 | inline TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store> | ||||||||||||||||||||
1580 | m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) { | ||||||||||||||||||||
1581 | return TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>(ValueOp, | ||||||||||||||||||||
1582 | PointerOp); | ||||||||||||||||||||
1583 | } | ||||||||||||||||||||
1584 | |||||||||||||||||||||
1585 | //===----------------------------------------------------------------------===// | ||||||||||||||||||||
1586 | // Matchers for CastInst classes | ||||||||||||||||||||
1587 | // | ||||||||||||||||||||
1588 | |||||||||||||||||||||
1589 | template <typename Op_t, unsigned Opcode> struct CastClass_match { | ||||||||||||||||||||
1590 | Op_t Op; | ||||||||||||||||||||
1591 | |||||||||||||||||||||
1592 | CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {} | ||||||||||||||||||||
1593 | |||||||||||||||||||||
1594 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
1595 | if (auto *O = dyn_cast<Operator>(V)) | ||||||||||||||||||||
1596 | return O->getOpcode() == Opcode && Op.match(O->getOperand(0)); | ||||||||||||||||||||
1597 | return false; | ||||||||||||||||||||
1598 | } | ||||||||||||||||||||
1599 | }; | ||||||||||||||||||||
1600 | |||||||||||||||||||||
1601 | /// Matches BitCast. | ||||||||||||||||||||
1602 | template <typename OpTy> | ||||||||||||||||||||
1603 | inline CastClass_match<OpTy, Instruction::BitCast> m_BitCast(const OpTy &Op) { | ||||||||||||||||||||
1604 | return CastClass_match<OpTy, Instruction::BitCast>(Op); | ||||||||||||||||||||
1605 | } | ||||||||||||||||||||
1606 | |||||||||||||||||||||
1607 | /// Matches PtrToInt. | ||||||||||||||||||||
1608 | template <typename OpTy> | ||||||||||||||||||||
1609 | inline CastClass_match<OpTy, Instruction::PtrToInt> m_PtrToInt(const OpTy &Op) { | ||||||||||||||||||||
1610 | return CastClass_match<OpTy, Instruction::PtrToInt>(Op); | ||||||||||||||||||||
1611 | } | ||||||||||||||||||||
1612 | |||||||||||||||||||||
1613 | /// Matches IntToPtr. | ||||||||||||||||||||
1614 | template <typename OpTy> | ||||||||||||||||||||
1615 | inline CastClass_match<OpTy, Instruction::IntToPtr> m_IntToPtr(const OpTy &Op) { | ||||||||||||||||||||
1616 | return CastClass_match<OpTy, Instruction::IntToPtr>(Op); | ||||||||||||||||||||
1617 | } | ||||||||||||||||||||
1618 | |||||||||||||||||||||
1619 | /// Matches Trunc. | ||||||||||||||||||||
1620 | template <typename OpTy> | ||||||||||||||||||||
1621 | inline CastClass_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) { | ||||||||||||||||||||
1622 | return CastClass_match<OpTy, Instruction::Trunc>(Op); | ||||||||||||||||||||
1623 | } | ||||||||||||||||||||
1624 | |||||||||||||||||||||
1625 | template <typename OpTy> | ||||||||||||||||||||
1626 | inline match_combine_or<CastClass_match<OpTy, Instruction::Trunc>, OpTy> | ||||||||||||||||||||
1627 | m_TruncOrSelf(const OpTy &Op) { | ||||||||||||||||||||
1628 | return m_CombineOr(m_Trunc(Op), Op); | ||||||||||||||||||||
1629 | } | ||||||||||||||||||||
1630 | |||||||||||||||||||||
1631 | /// Matches SExt. | ||||||||||||||||||||
1632 | template <typename OpTy> | ||||||||||||||||||||
1633 | inline CastClass_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) { | ||||||||||||||||||||
1634 | return CastClass_match<OpTy, Instruction::SExt>(Op); | ||||||||||||||||||||
1635 | } | ||||||||||||||||||||
1636 | |||||||||||||||||||||
1637 | /// Matches ZExt. | ||||||||||||||||||||
1638 | template <typename OpTy> | ||||||||||||||||||||
1639 | inline CastClass_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) { | ||||||||||||||||||||
1640 | return CastClass_match<OpTy, Instruction::ZExt>(Op); | ||||||||||||||||||||
1641 | } | ||||||||||||||||||||
1642 | |||||||||||||||||||||
1643 | template <typename OpTy> | ||||||||||||||||||||
1644 | inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>, OpTy> | ||||||||||||||||||||
1645 | m_ZExtOrSelf(const OpTy &Op) { | ||||||||||||||||||||
1646 | return m_CombineOr(m_ZExt(Op), Op); | ||||||||||||||||||||
1647 | } | ||||||||||||||||||||
1648 | |||||||||||||||||||||
1649 | template <typename OpTy> | ||||||||||||||||||||
1650 | inline match_combine_or<CastClass_match<OpTy, Instruction::SExt>, OpTy> | ||||||||||||||||||||
1651 | m_SExtOrSelf(const OpTy &Op) { | ||||||||||||||||||||
1652 | return m_CombineOr(m_SExt(Op), Op); | ||||||||||||||||||||
1653 | } | ||||||||||||||||||||
1654 | |||||||||||||||||||||
1655 | template <typename OpTy> | ||||||||||||||||||||
1656 | inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>, | ||||||||||||||||||||
1657 | CastClass_match<OpTy, Instruction::SExt>> | ||||||||||||||||||||
1658 | m_ZExtOrSExt(const OpTy &Op) { | ||||||||||||||||||||
1659 | return m_CombineOr(m_ZExt(Op), m_SExt(Op)); | ||||||||||||||||||||
1660 | } | ||||||||||||||||||||
1661 | |||||||||||||||||||||
1662 | template <typename OpTy> | ||||||||||||||||||||
1663 | inline match_combine_or< | ||||||||||||||||||||
1664 | match_combine_or<CastClass_match<OpTy, Instruction::ZExt>, | ||||||||||||||||||||
1665 | CastClass_match<OpTy, Instruction::SExt>>, | ||||||||||||||||||||
1666 | OpTy> | ||||||||||||||||||||
1667 | m_ZExtOrSExtOrSelf(const OpTy &Op) { | ||||||||||||||||||||
1668 | return m_CombineOr(m_ZExtOrSExt(Op), Op); | ||||||||||||||||||||
1669 | } | ||||||||||||||||||||
1670 | |||||||||||||||||||||
1671 | template <typename OpTy> | ||||||||||||||||||||
1672 | inline CastClass_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) { | ||||||||||||||||||||
1673 | return CastClass_match<OpTy, Instruction::UIToFP>(Op); | ||||||||||||||||||||
1674 | } | ||||||||||||||||||||
1675 | |||||||||||||||||||||
1676 | template <typename OpTy> | ||||||||||||||||||||
1677 | inline CastClass_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) { | ||||||||||||||||||||
1678 | return CastClass_match<OpTy, Instruction::SIToFP>(Op); | ||||||||||||||||||||
1679 | } | ||||||||||||||||||||
1680 | |||||||||||||||||||||
1681 | template <typename OpTy> | ||||||||||||||||||||
1682 | inline CastClass_match<OpTy, Instruction::FPToUI> m_FPToUI(const OpTy &Op) { | ||||||||||||||||||||
1683 | return CastClass_match<OpTy, Instruction::FPToUI>(Op); | ||||||||||||||||||||
1684 | } | ||||||||||||||||||||
1685 | |||||||||||||||||||||
1686 | template <typename OpTy> | ||||||||||||||||||||
1687 | inline CastClass_match<OpTy, Instruction::FPToSI> m_FPToSI(const OpTy &Op) { | ||||||||||||||||||||
1688 | return CastClass_match<OpTy, Instruction::FPToSI>(Op); | ||||||||||||||||||||
1689 | } | ||||||||||||||||||||
1690 | |||||||||||||||||||||
1691 | template <typename OpTy> | ||||||||||||||||||||
1692 | inline CastClass_match<OpTy, Instruction::FPTrunc> m_FPTrunc(const OpTy &Op) { | ||||||||||||||||||||
1693 | return CastClass_match<OpTy, Instruction::FPTrunc>(Op); | ||||||||||||||||||||
1694 | } | ||||||||||||||||||||
1695 | |||||||||||||||||||||
1696 | template <typename OpTy> | ||||||||||||||||||||
1697 | inline CastClass_match<OpTy, Instruction::FPExt> m_FPExt(const OpTy &Op) { | ||||||||||||||||||||
1698 | return CastClass_match<OpTy, Instruction::FPExt>(Op); | ||||||||||||||||||||
1699 | } | ||||||||||||||||||||
1700 | |||||||||||||||||||||
1701 | //===----------------------------------------------------------------------===// | ||||||||||||||||||||
1702 | // Matchers for control flow. | ||||||||||||||||||||
1703 | // | ||||||||||||||||||||
1704 | |||||||||||||||||||||
1705 | struct br_match { | ||||||||||||||||||||
1706 | BasicBlock *&Succ; | ||||||||||||||||||||
1707 | |||||||||||||||||||||
1708 | br_match(BasicBlock *&Succ) : Succ(Succ) {} | ||||||||||||||||||||
1709 | |||||||||||||||||||||
1710 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
1711 | if (auto *BI = dyn_cast<BranchInst>(V)) | ||||||||||||||||||||
1712 | if (BI->isUnconditional()) { | ||||||||||||||||||||
1713 | Succ = BI->getSuccessor(0); | ||||||||||||||||||||
1714 | return true; | ||||||||||||||||||||
1715 | } | ||||||||||||||||||||
1716 | return false; | ||||||||||||||||||||
1717 | } | ||||||||||||||||||||
1718 | }; | ||||||||||||||||||||
1719 | |||||||||||||||||||||
1720 | inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); } | ||||||||||||||||||||
1721 | |||||||||||||||||||||
1722 | template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t> | ||||||||||||||||||||
1723 | struct brc_match { | ||||||||||||||||||||
1724 | Cond_t Cond; | ||||||||||||||||||||
1725 | TrueBlock_t T; | ||||||||||||||||||||
1726 | FalseBlock_t F; | ||||||||||||||||||||
1727 | |||||||||||||||||||||
1728 | brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f) | ||||||||||||||||||||
1729 | : Cond(C), T(t), F(f) {} | ||||||||||||||||||||
1730 | |||||||||||||||||||||
1731 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
1732 | if (auto *BI = dyn_cast<BranchInst>(V)) | ||||||||||||||||||||
1733 | if (BI->isConditional() && Cond.match(BI->getCondition())) | ||||||||||||||||||||
1734 | return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1)); | ||||||||||||||||||||
1735 | return false; | ||||||||||||||||||||
1736 | } | ||||||||||||||||||||
1737 | }; | ||||||||||||||||||||
1738 | |||||||||||||||||||||
1739 | template <typename Cond_t> | ||||||||||||||||||||
1740 | inline brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>> | ||||||||||||||||||||
1741 | m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) { | ||||||||||||||||||||
1742 | return brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>>( | ||||||||||||||||||||
1743 | C, m_BasicBlock(T), m_BasicBlock(F)); | ||||||||||||||||||||
1744 | } | ||||||||||||||||||||
1745 | |||||||||||||||||||||
1746 | template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t> | ||||||||||||||||||||
1747 | inline brc_match<Cond_t, TrueBlock_t, FalseBlock_t> | ||||||||||||||||||||
1748 | m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) { | ||||||||||||||||||||
1749 | return brc_match<Cond_t, TrueBlock_t, FalseBlock_t>(C, T, F); | ||||||||||||||||||||
1750 | } | ||||||||||||||||||||
1751 | |||||||||||||||||||||
1752 | //===----------------------------------------------------------------------===// | ||||||||||||||||||||
1753 | // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y). | ||||||||||||||||||||
1754 | // | ||||||||||||||||||||
1755 | |||||||||||||||||||||
1756 | template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t, | ||||||||||||||||||||
1757 | bool Commutable = false> | ||||||||||||||||||||
1758 | struct MaxMin_match { | ||||||||||||||||||||
1759 | using PredType = Pred_t; | ||||||||||||||||||||
1760 | LHS_t L; | ||||||||||||||||||||
1761 | RHS_t R; | ||||||||||||||||||||
1762 | |||||||||||||||||||||
1763 | // The evaluation order is always stable, regardless of Commutability. | ||||||||||||||||||||
1764 | // The LHS is always matched first. | ||||||||||||||||||||
1765 | MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} | ||||||||||||||||||||
1766 | |||||||||||||||||||||
1767 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
1768 | if (auto *II = dyn_cast<IntrinsicInst>(V)) { | ||||||||||||||||||||
1769 | Intrinsic::ID IID = II->getIntrinsicID(); | ||||||||||||||||||||
1770 | if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) || | ||||||||||||||||||||
1771 | (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) || | ||||||||||||||||||||
1772 | (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) || | ||||||||||||||||||||
1773 | (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) { | ||||||||||||||||||||
1774 | Value *LHS = II->getOperand(0), *RHS = II->getOperand(1); | ||||||||||||||||||||
1775 | return (L.match(LHS) && R.match(RHS)) || | ||||||||||||||||||||
1776 | (Commutable && L.match(RHS) && R.match(LHS)); | ||||||||||||||||||||
1777 | } | ||||||||||||||||||||
1778 | } | ||||||||||||||||||||
1779 | // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x". | ||||||||||||||||||||
1780 | auto *SI = dyn_cast<SelectInst>(V); | ||||||||||||||||||||
1781 | if (!SI) | ||||||||||||||||||||
1782 | return false; | ||||||||||||||||||||
1783 | auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition()); | ||||||||||||||||||||
1784 | if (!Cmp) | ||||||||||||||||||||
1785 | return false; | ||||||||||||||||||||
1786 | // At this point we have a select conditioned on a comparison. Check that | ||||||||||||||||||||
1787 | // it is the values returned by the select that are being compared. | ||||||||||||||||||||
1788 | auto *TrueVal = SI->getTrueValue(); | ||||||||||||||||||||
1789 | auto *FalseVal = SI->getFalseValue(); | ||||||||||||||||||||
1790 | auto *LHS = Cmp->getOperand(0); | ||||||||||||||||||||
1791 | auto *RHS = Cmp->getOperand(1); | ||||||||||||||||||||
1792 | if ((TrueVal != LHS || FalseVal != RHS) && | ||||||||||||||||||||
1793 | (TrueVal != RHS || FalseVal != LHS)) | ||||||||||||||||||||
1794 | return false; | ||||||||||||||||||||
1795 | typename CmpInst_t::Predicate Pred = | ||||||||||||||||||||
1796 | LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate(); | ||||||||||||||||||||
1797 | // Does "(x pred y) ? x : y" represent the desired max/min operation? | ||||||||||||||||||||
1798 | if (!Pred_t::match(Pred)) | ||||||||||||||||||||
1799 | return false; | ||||||||||||||||||||
1800 | // It does! Bind the operands. | ||||||||||||||||||||
1801 | return (L.match(LHS) && R.match(RHS)) || | ||||||||||||||||||||
1802 | (Commutable && L.match(RHS) && R.match(LHS)); | ||||||||||||||||||||
1803 | } | ||||||||||||||||||||
1804 | }; | ||||||||||||||||||||
1805 | |||||||||||||||||||||
1806 | /// Helper class for identifying signed max predicates. | ||||||||||||||||||||
1807 | struct smax_pred_ty { | ||||||||||||||||||||
1808 | static bool match(ICmpInst::Predicate Pred) { | ||||||||||||||||||||
1809 | return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE; | ||||||||||||||||||||
1810 | } | ||||||||||||||||||||
1811 | }; | ||||||||||||||||||||
1812 | |||||||||||||||||||||
1813 | /// Helper class for identifying signed min predicates. | ||||||||||||||||||||
1814 | struct smin_pred_ty { | ||||||||||||||||||||
1815 | static bool match(ICmpInst::Predicate Pred) { | ||||||||||||||||||||
1816 | return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE; | ||||||||||||||||||||
1817 | } | ||||||||||||||||||||
1818 | }; | ||||||||||||||||||||
1819 | |||||||||||||||||||||
1820 | /// Helper class for identifying unsigned max predicates. | ||||||||||||||||||||
1821 | struct umax_pred_ty { | ||||||||||||||||||||
1822 | static bool match(ICmpInst::Predicate Pred) { | ||||||||||||||||||||
1823 | return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE; | ||||||||||||||||||||
1824 | } | ||||||||||||||||||||
1825 | }; | ||||||||||||||||||||
1826 | |||||||||||||||||||||
1827 | /// Helper class for identifying unsigned min predicates. | ||||||||||||||||||||
1828 | struct umin_pred_ty { | ||||||||||||||||||||
1829 | static bool match(ICmpInst::Predicate Pred) { | ||||||||||||||||||||
1830 | return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE; | ||||||||||||||||||||
1831 | } | ||||||||||||||||||||
1832 | }; | ||||||||||||||||||||
1833 | |||||||||||||||||||||
1834 | /// Helper class for identifying ordered max predicates. | ||||||||||||||||||||
1835 | struct ofmax_pred_ty { | ||||||||||||||||||||
1836 | static bool match(FCmpInst::Predicate Pred) { | ||||||||||||||||||||
1837 | return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE; | ||||||||||||||||||||
1838 | } | ||||||||||||||||||||
1839 | }; | ||||||||||||||||||||
1840 | |||||||||||||||||||||
1841 | /// Helper class for identifying ordered min predicates. | ||||||||||||||||||||
1842 | struct ofmin_pred_ty { | ||||||||||||||||||||
1843 | static bool match(FCmpInst::Predicate Pred) { | ||||||||||||||||||||
1844 | return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE; | ||||||||||||||||||||
1845 | } | ||||||||||||||||||||
1846 | }; | ||||||||||||||||||||
1847 | |||||||||||||||||||||
1848 | /// Helper class for identifying unordered max predicates. | ||||||||||||||||||||
1849 | struct ufmax_pred_ty { | ||||||||||||||||||||
1850 | static bool match(FCmpInst::Predicate Pred) { | ||||||||||||||||||||
1851 | return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE; | ||||||||||||||||||||
1852 | } | ||||||||||||||||||||
1853 | }; | ||||||||||||||||||||
1854 | |||||||||||||||||||||
1855 | /// Helper class for identifying unordered min predicates. | ||||||||||||||||||||
1856 | struct ufmin_pred_ty { | ||||||||||||||||||||
1857 | static bool match(FCmpInst::Predicate Pred) { | ||||||||||||||||||||
1858 | return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE; | ||||||||||||||||||||
1859 | } | ||||||||||||||||||||
1860 | }; | ||||||||||||||||||||
1861 | |||||||||||||||||||||
1862 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1863 | inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L, | ||||||||||||||||||||
1864 | const RHS &R) { | ||||||||||||||||||||
1865 | return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R); | ||||||||||||||||||||
1866 | } | ||||||||||||||||||||
1867 | |||||||||||||||||||||
1868 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1869 | inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L, | ||||||||||||||||||||
1870 | const RHS &R) { | ||||||||||||||||||||
1871 | return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R); | ||||||||||||||||||||
1872 | } | ||||||||||||||||||||
1873 | |||||||||||||||||||||
1874 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1875 | inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L, | ||||||||||||||||||||
1876 | const RHS &R) { | ||||||||||||||||||||
1877 | return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R); | ||||||||||||||||||||
1878 | } | ||||||||||||||||||||
1879 | |||||||||||||||||||||
1880 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1881 | inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L, | ||||||||||||||||||||
1882 | const RHS &R) { | ||||||||||||||||||||
1883 | return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R); | ||||||||||||||||||||
1884 | } | ||||||||||||||||||||
1885 | |||||||||||||||||||||
1886 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1887 | inline match_combine_or< | ||||||||||||||||||||
1888 | match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>, | ||||||||||||||||||||
1889 | MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>>, | ||||||||||||||||||||
1890 | match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>, | ||||||||||||||||||||
1891 | MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>>> | ||||||||||||||||||||
1892 | m_MaxOrMin(const LHS &L, const RHS &R) { | ||||||||||||||||||||
1893 | return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)), | ||||||||||||||||||||
1894 | m_CombineOr(m_UMax(L, R), m_UMin(L, R))); | ||||||||||||||||||||
1895 | } | ||||||||||||||||||||
1896 | |||||||||||||||||||||
1897 | /// Match an 'ordered' floating point maximum function. | ||||||||||||||||||||
1898 | /// Floating point has one special value 'NaN'. Therefore, there is no total | ||||||||||||||||||||
1899 | /// order. However, if we can ignore the 'NaN' value (for example, because of a | ||||||||||||||||||||
1900 | /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum' | ||||||||||||||||||||
1901 | /// semantics. In the presence of 'NaN' we have to preserve the original | ||||||||||||||||||||
1902 | /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate. | ||||||||||||||||||||
1903 | /// | ||||||||||||||||||||
1904 | /// max(L, R) iff L and R are not NaN | ||||||||||||||||||||
1905 | /// m_OrdFMax(L, R) = R iff L or R are NaN | ||||||||||||||||||||
1906 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1907 | inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L, | ||||||||||||||||||||
1908 | const RHS &R) { | ||||||||||||||||||||
1909 | return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R); | ||||||||||||||||||||
1910 | } | ||||||||||||||||||||
1911 | |||||||||||||||||||||
1912 | /// Match an 'ordered' floating point minimum function. | ||||||||||||||||||||
1913 | /// Floating point has one special value 'NaN'. Therefore, there is no total | ||||||||||||||||||||
1914 | /// order. However, if we can ignore the 'NaN' value (for example, because of a | ||||||||||||||||||||
1915 | /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum' | ||||||||||||||||||||
1916 | /// semantics. In the presence of 'NaN' we have to preserve the original | ||||||||||||||||||||
1917 | /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate. | ||||||||||||||||||||
1918 | /// | ||||||||||||||||||||
1919 | /// min(L, R) iff L and R are not NaN | ||||||||||||||||||||
1920 | /// m_OrdFMin(L, R) = R iff L or R are NaN | ||||||||||||||||||||
1921 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1922 | inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L, | ||||||||||||||||||||
1923 | const RHS &R) { | ||||||||||||||||||||
1924 | return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R); | ||||||||||||||||||||
1925 | } | ||||||||||||||||||||
1926 | |||||||||||||||||||||
1927 | /// Match an 'unordered' floating point maximum function. | ||||||||||||||||||||
1928 | /// Floating point has one special value 'NaN'. Therefore, there is no total | ||||||||||||||||||||
1929 | /// order. However, if we can ignore the 'NaN' value (for example, because of a | ||||||||||||||||||||
1930 | /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum' | ||||||||||||||||||||
1931 | /// semantics. In the presence of 'NaN' we have to preserve the original | ||||||||||||||||||||
1932 | /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate. | ||||||||||||||||||||
1933 | /// | ||||||||||||||||||||
1934 | /// max(L, R) iff L and R are not NaN | ||||||||||||||||||||
1935 | /// m_UnordFMax(L, R) = L iff L or R are NaN | ||||||||||||||||||||
1936 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1937 | inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty> | ||||||||||||||||||||
1938 | m_UnordFMax(const LHS &L, const RHS &R) { | ||||||||||||||||||||
1939 | return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R); | ||||||||||||||||||||
1940 | } | ||||||||||||||||||||
1941 | |||||||||||||||||||||
1942 | /// Match an 'unordered' floating point minimum function. | ||||||||||||||||||||
1943 | /// Floating point has one special value 'NaN'. Therefore, there is no total | ||||||||||||||||||||
1944 | /// order. However, if we can ignore the 'NaN' value (for example, because of a | ||||||||||||||||||||
1945 | /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum' | ||||||||||||||||||||
1946 | /// semantics. In the presence of 'NaN' we have to preserve the original | ||||||||||||||||||||
1947 | /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate. | ||||||||||||||||||||
1948 | /// | ||||||||||||||||||||
1949 | /// min(L, R) iff L and R are not NaN | ||||||||||||||||||||
1950 | /// m_UnordFMin(L, R) = L iff L or R are NaN | ||||||||||||||||||||
1951 | template <typename LHS, typename RHS> | ||||||||||||||||||||
1952 | inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty> | ||||||||||||||||||||
1953 | m_UnordFMin(const LHS &L, const RHS &R) { | ||||||||||||||||||||
1954 | return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R); | ||||||||||||||||||||
1955 | } | ||||||||||||||||||||
1956 | |||||||||||||||||||||
1957 | //===----------------------------------------------------------------------===// | ||||||||||||||||||||
1958 | // Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b | ||||||||||||||||||||
1959 | // Note that S might be matched to other instructions than AddInst. | ||||||||||||||||||||
1960 | // | ||||||||||||||||||||
1961 | |||||||||||||||||||||
1962 | template <typename LHS_t, typename RHS_t, typename Sum_t> | ||||||||||||||||||||
1963 | struct UAddWithOverflow_match { | ||||||||||||||||||||
1964 | LHS_t L; | ||||||||||||||||||||
1965 | RHS_t R; | ||||||||||||||||||||
1966 | Sum_t S; | ||||||||||||||||||||
1967 | |||||||||||||||||||||
1968 | UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S) | ||||||||||||||||||||
1969 | : L(L), R(R), S(S) {} | ||||||||||||||||||||
1970 | |||||||||||||||||||||
1971 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
1972 | Value *ICmpLHS, *ICmpRHS; | ||||||||||||||||||||
1973 | ICmpInst::Predicate Pred; | ||||||||||||||||||||
1974 | if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V)) | ||||||||||||||||||||
1975 | return false; | ||||||||||||||||||||
1976 | |||||||||||||||||||||
1977 | Value *AddLHS, *AddRHS; | ||||||||||||||||||||
1978 | auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS)); | ||||||||||||||||||||
1979 | |||||||||||||||||||||
1980 | // (a + b) u< a, (a + b) u< b | ||||||||||||||||||||
1981 | if (Pred == ICmpInst::ICMP_ULT) | ||||||||||||||||||||
1982 | if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS)) | ||||||||||||||||||||
1983 | return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS); | ||||||||||||||||||||
1984 | |||||||||||||||||||||
1985 | // a >u (a + b), b >u (a + b) | ||||||||||||||||||||
1986 | if (Pred == ICmpInst::ICMP_UGT) | ||||||||||||||||||||
1987 | if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS)) | ||||||||||||||||||||
1988 | return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS); | ||||||||||||||||||||
1989 | |||||||||||||||||||||
1990 | Value *Op1; | ||||||||||||||||||||
1991 | auto XorExpr = m_OneUse(m_Xor(m_Value(Op1), m_AllOnes())); | ||||||||||||||||||||
1992 | // (a ^ -1) <u b | ||||||||||||||||||||
1993 | if (Pred == ICmpInst::ICMP_ULT) { | ||||||||||||||||||||
1994 | if (XorExpr.match(ICmpLHS)) | ||||||||||||||||||||
1995 | return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS); | ||||||||||||||||||||
1996 | } | ||||||||||||||||||||
1997 | // b > u (a ^ -1) | ||||||||||||||||||||
1998 | if (Pred == ICmpInst::ICMP_UGT) { | ||||||||||||||||||||
1999 | if (XorExpr.match(ICmpRHS)) | ||||||||||||||||||||
2000 | return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS); | ||||||||||||||||||||
2001 | } | ||||||||||||||||||||
2002 | |||||||||||||||||||||
2003 | // Match special-case for increment-by-1. | ||||||||||||||||||||
2004 | if (Pred == ICmpInst::ICMP_EQ) { | ||||||||||||||||||||
2005 | // (a + 1) == 0 | ||||||||||||||||||||
2006 | // (1 + a) == 0 | ||||||||||||||||||||
2007 | if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) && | ||||||||||||||||||||
2008 | (m_One().match(AddLHS) || m_One().match(AddRHS))) | ||||||||||||||||||||
2009 | return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS); | ||||||||||||||||||||
2010 | // 0 == (a + 1) | ||||||||||||||||||||
2011 | // 0 == (1 + a) | ||||||||||||||||||||
2012 | if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) && | ||||||||||||||||||||
2013 | (m_One().match(AddLHS) || m_One().match(AddRHS))) | ||||||||||||||||||||
2014 | return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS); | ||||||||||||||||||||
2015 | } | ||||||||||||||||||||
2016 | |||||||||||||||||||||
2017 | return false; | ||||||||||||||||||||
2018 | } | ||||||||||||||||||||
2019 | }; | ||||||||||||||||||||
2020 | |||||||||||||||||||||
2021 | /// Match an icmp instruction checking for unsigned overflow on addition. | ||||||||||||||||||||
2022 | /// | ||||||||||||||||||||
2023 | /// S is matched to the addition whose result is being checked for overflow, and | ||||||||||||||||||||
2024 | /// L and R are matched to the LHS and RHS of S. | ||||||||||||||||||||
2025 | template <typename LHS_t, typename RHS_t, typename Sum_t> | ||||||||||||||||||||
2026 | UAddWithOverflow_match<LHS_t, RHS_t, Sum_t> | ||||||||||||||||||||
2027 | m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) { | ||||||||||||||||||||
2028 | return UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>(L, R, S); | ||||||||||||||||||||
2029 | } | ||||||||||||||||||||
2030 | |||||||||||||||||||||
2031 | template <typename Opnd_t> struct Argument_match { | ||||||||||||||||||||
2032 | unsigned OpI; | ||||||||||||||||||||
2033 | Opnd_t Val; | ||||||||||||||||||||
2034 | |||||||||||||||||||||
2035 | Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {} | ||||||||||||||||||||
2036 | |||||||||||||||||||||
2037 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
2038 | // FIXME: Should likely be switched to use `CallBase`. | ||||||||||||||||||||
2039 | if (const auto *CI = dyn_cast<CallInst>(V)) | ||||||||||||||||||||
2040 | return Val.match(CI->getArgOperand(OpI)); | ||||||||||||||||||||
2041 | return false; | ||||||||||||||||||||
2042 | } | ||||||||||||||||||||
2043 | }; | ||||||||||||||||||||
2044 | |||||||||||||||||||||
2045 | /// Match an argument. | ||||||||||||||||||||
2046 | template <unsigned OpI, typename Opnd_t> | ||||||||||||||||||||
2047 | inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) { | ||||||||||||||||||||
2048 | return Argument_match<Opnd_t>(OpI, Op); | ||||||||||||||||||||
2049 | } | ||||||||||||||||||||
2050 | |||||||||||||||||||||
2051 | /// Intrinsic matchers. | ||||||||||||||||||||
2052 | struct IntrinsicID_match { | ||||||||||||||||||||
2053 | unsigned ID; | ||||||||||||||||||||
2054 | |||||||||||||||||||||
2055 | IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {} | ||||||||||||||||||||
2056 | |||||||||||||||||||||
2057 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
2058 | if (const auto *CI
| ||||||||||||||||||||
2059 | if (const auto *F = CI->getCalledFunction()) | ||||||||||||||||||||
2060 | return F->getIntrinsicID() == ID; | ||||||||||||||||||||
2061 | return false; | ||||||||||||||||||||
2062 | } | ||||||||||||||||||||
2063 | }; | ||||||||||||||||||||
2064 | |||||||||||||||||||||
2065 | /// Intrinsic matches are combinations of ID matchers, and argument | ||||||||||||||||||||
2066 | /// matchers. Higher arity matcher are defined recursively in terms of and-ing | ||||||||||||||||||||
2067 | /// them with lower arity matchers. Here's some convenient typedefs for up to | ||||||||||||||||||||
2068 | /// several arguments, and more can be added as needed | ||||||||||||||||||||
2069 | template <typename T0 = void, typename T1 = void, typename T2 = void, | ||||||||||||||||||||
2070 | typename T3 = void, typename T4 = void, typename T5 = void, | ||||||||||||||||||||
2071 | typename T6 = void, typename T7 = void, typename T8 = void, | ||||||||||||||||||||
2072 | typename T9 = void, typename T10 = void> | ||||||||||||||||||||
2073 | struct m_Intrinsic_Ty; | ||||||||||||||||||||
2074 | template <typename T0> struct m_Intrinsic_Ty<T0> { | ||||||||||||||||||||
2075 | using Ty = match_combine_and<IntrinsicID_match, Argument_match<T0>>; | ||||||||||||||||||||
2076 | }; | ||||||||||||||||||||
2077 | template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> { | ||||||||||||||||||||
2078 | using Ty = | ||||||||||||||||||||
2079 | match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>; | ||||||||||||||||||||
2080 | }; | ||||||||||||||||||||
2081 | template <typename T0, typename T1, typename T2> | ||||||||||||||||||||
2082 | struct m_Intrinsic_Ty<T0, T1, T2> { | ||||||||||||||||||||
2083 | using Ty = | ||||||||||||||||||||
2084 | match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty, | ||||||||||||||||||||
2085 | Argument_match<T2>>; | ||||||||||||||||||||
2086 | }; | ||||||||||||||||||||
2087 | template <typename T0, typename T1, typename T2, typename T3> | ||||||||||||||||||||
2088 | struct m_Intrinsic_Ty<T0, T1, T2, T3> { | ||||||||||||||||||||
2089 | using Ty = | ||||||||||||||||||||
2090 | match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty, | ||||||||||||||||||||
2091 | Argument_match<T3>>; | ||||||||||||||||||||
2092 | }; | ||||||||||||||||||||
2093 | |||||||||||||||||||||
2094 | template <typename T0, typename T1, typename T2, typename T3, typename T4> | ||||||||||||||||||||
2095 | struct m_Intrinsic_Ty<T0, T1, T2, T3, T4> { | ||||||||||||||||||||
2096 | using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty, | ||||||||||||||||||||
2097 | Argument_match<T4>>; | ||||||||||||||||||||
2098 | }; | ||||||||||||||||||||
2099 | |||||||||||||||||||||
2100 | template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5> | ||||||||||||||||||||
2101 | struct m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5> { | ||||||||||||||||||||
2102 | using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty, | ||||||||||||||||||||
2103 | Argument_match<T5>>; | ||||||||||||||||||||
2104 | }; | ||||||||||||||||||||
2105 | |||||||||||||||||||||
2106 | /// Match intrinsic calls like this: | ||||||||||||||||||||
2107 | /// m_Intrinsic<Intrinsic::fabs>(m_Value(X)) | ||||||||||||||||||||
2108 | template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() { | ||||||||||||||||||||
2109 | return IntrinsicID_match(IntrID); | ||||||||||||||||||||
2110 | } | ||||||||||||||||||||
2111 | |||||||||||||||||||||
2112 | /// Matches MaskedLoad Intrinsic. | ||||||||||||||||||||
2113 | template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3> | ||||||||||||||||||||
2114 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2, Opnd3>::Ty | ||||||||||||||||||||
2115 | m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2, | ||||||||||||||||||||
2116 | const Opnd3 &Op3) { | ||||||||||||||||||||
2117 | return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2, Op3); | ||||||||||||||||||||
2118 | } | ||||||||||||||||||||
2119 | |||||||||||||||||||||
2120 | /// Matches MaskedGather Intrinsic. | ||||||||||||||||||||
2121 | template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3> | ||||||||||||||||||||
2122 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2, Opnd3>::Ty | ||||||||||||||||||||
2123 | m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2, | ||||||||||||||||||||
2124 | const Opnd3 &Op3) { | ||||||||||||||||||||
2125 | return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2, Op3); | ||||||||||||||||||||
2126 | } | ||||||||||||||||||||
2127 | |||||||||||||||||||||
2128 | template <Intrinsic::ID IntrID, typename T0> | ||||||||||||||||||||
2129 | inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) { | ||||||||||||||||||||
2130 | return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0)); | ||||||||||||||||||||
2131 | } | ||||||||||||||||||||
2132 | |||||||||||||||||||||
2133 | template <Intrinsic::ID IntrID, typename T0, typename T1> | ||||||||||||||||||||
2134 | inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0, | ||||||||||||||||||||
2135 | const T1 &Op1) { | ||||||||||||||||||||
2136 | return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1)); | ||||||||||||||||||||
2137 | } | ||||||||||||||||||||
2138 | |||||||||||||||||||||
2139 | template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2> | ||||||||||||||||||||
2140 | inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty | ||||||||||||||||||||
2141 | m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) { | ||||||||||||||||||||
2142 | return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2)); | ||||||||||||||||||||
2143 | } | ||||||||||||||||||||
2144 | |||||||||||||||||||||
2145 | template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2, | ||||||||||||||||||||
2146 | typename T3> | ||||||||||||||||||||
2147 | inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty | ||||||||||||||||||||
2148 | m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) { | ||||||||||||||||||||
2149 | return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3)); | ||||||||||||||||||||
2150 | } | ||||||||||||||||||||
2151 | |||||||||||||||||||||
2152 | template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2, | ||||||||||||||||||||
2153 | typename T3, typename T4> | ||||||||||||||||||||
2154 | inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty | ||||||||||||||||||||
2155 | m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3, | ||||||||||||||||||||
2156 | const T4 &Op4) { | ||||||||||||||||||||
2157 | return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3), | ||||||||||||||||||||
2158 | m_Argument<4>(Op4)); | ||||||||||||||||||||
2159 | } | ||||||||||||||||||||
2160 | |||||||||||||||||||||
2161 | template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2, | ||||||||||||||||||||
2162 | typename T3, typename T4, typename T5> | ||||||||||||||||||||
2163 | inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5>::Ty | ||||||||||||||||||||
2164 | m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3, | ||||||||||||||||||||
2165 | const T4 &Op4, const T5 &Op5) { | ||||||||||||||||||||
2166 | return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4), | ||||||||||||||||||||
2167 | m_Argument<5>(Op5)); | ||||||||||||||||||||
2168 | } | ||||||||||||||||||||
2169 | |||||||||||||||||||||
2170 | // Helper intrinsic matching specializations. | ||||||||||||||||||||
2171 | template <typename Opnd0> | ||||||||||||||||||||
2172 | inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) { | ||||||||||||||||||||
2173 | return m_Intrinsic<Intrinsic::bitreverse>(Op0); | ||||||||||||||||||||
2174 | } | ||||||||||||||||||||
2175 | |||||||||||||||||||||
2176 | template <typename Opnd0> | ||||||||||||||||||||
2177 | inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) { | ||||||||||||||||||||
2178 | return m_Intrinsic<Intrinsic::bswap>(Op0); | ||||||||||||||||||||
2179 | } | ||||||||||||||||||||
2180 | |||||||||||||||||||||
2181 | template <typename Opnd0> | ||||||||||||||||||||
2182 | inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) { | ||||||||||||||||||||
2183 | return m_Intrinsic<Intrinsic::fabs>(Op0); | ||||||||||||||||||||
2184 | } | ||||||||||||||||||||
2185 | |||||||||||||||||||||
2186 | template <typename Opnd0> | ||||||||||||||||||||
2187 | inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) { | ||||||||||||||||||||
2188 | return m_Intrinsic<Intrinsic::canonicalize>(Op0); | ||||||||||||||||||||
2189 | } | ||||||||||||||||||||
2190 | |||||||||||||||||||||
2191 | template <typename Opnd0, typename Opnd1> | ||||||||||||||||||||
2192 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0, | ||||||||||||||||||||
2193 | const Opnd1 &Op1) { | ||||||||||||||||||||
2194 | return m_Intrinsic<Intrinsic::minnum>(Op0, Op1); | ||||||||||||||||||||
2195 | } | ||||||||||||||||||||
2196 | |||||||||||||||||||||
2197 | template <typename Opnd0, typename Opnd1> | ||||||||||||||||||||
2198 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0, | ||||||||||||||||||||
2199 | const Opnd1 &Op1) { | ||||||||||||||||||||
2200 | return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1); | ||||||||||||||||||||
2201 | } | ||||||||||||||||||||
2202 | |||||||||||||||||||||
2203 | template <typename Opnd0, typename Opnd1, typename Opnd2> | ||||||||||||||||||||
2204 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2>::Ty | ||||||||||||||||||||
2205 | m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) { | ||||||||||||||||||||
2206 | return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2); | ||||||||||||||||||||
2207 | } | ||||||||||||||||||||
2208 | |||||||||||||||||||||
2209 | template <typename Opnd0, typename Opnd1, typename Opnd2> | ||||||||||||||||||||
2210 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2>::Ty | ||||||||||||||||||||
2211 | m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) { | ||||||||||||||||||||
2212 | return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2); | ||||||||||||||||||||
2213 | } | ||||||||||||||||||||
2214 | |||||||||||||||||||||
2215 | //===----------------------------------------------------------------------===// | ||||||||||||||||||||
2216 | // Matchers for two-operands operators with the operators in either order | ||||||||||||||||||||
2217 | // | ||||||||||||||||||||
2218 | |||||||||||||||||||||
2219 | /// Matches a BinaryOperator with LHS and RHS in either order. | ||||||||||||||||||||
2220 | template <typename LHS, typename RHS> | ||||||||||||||||||||
2221 | inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) { | ||||||||||||||||||||
2222 | return AnyBinaryOp_match<LHS, RHS, true>(L, R); | ||||||||||||||||||||
2223 | } | ||||||||||||||||||||
2224 | |||||||||||||||||||||
2225 | /// Matches an ICmp with a predicate over LHS and RHS in either order. | ||||||||||||||||||||
2226 | /// Swaps the predicate if operands are commuted. | ||||||||||||||||||||
2227 | template <typename LHS, typename RHS> | ||||||||||||||||||||
2228 | inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true> | ||||||||||||||||||||
2229 | m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) { | ||||||||||||||||||||
2230 | return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>(Pred, L, | ||||||||||||||||||||
2231 | R); | ||||||||||||||||||||
2232 | } | ||||||||||||||||||||
2233 | |||||||||||||||||||||
2234 | /// Matches a specific opcode with LHS and RHS in either order. | ||||||||||||||||||||
2235 | template <typename LHS, typename RHS> | ||||||||||||||||||||
2236 | inline SpecificBinaryOp_match<LHS, RHS, true> | ||||||||||||||||||||
2237 | m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) { | ||||||||||||||||||||
2238 | return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R); | ||||||||||||||||||||
2239 | } | ||||||||||||||||||||
2240 | |||||||||||||||||||||
2241 | /// Matches a Add with LHS and RHS in either order. | ||||||||||||||||||||
2242 | template <typename LHS, typename RHS> | ||||||||||||||||||||
2243 | inline BinaryOp_match<LHS, RHS, Instruction::Add, true> m_c_Add(const LHS &L, | ||||||||||||||||||||
2244 | const RHS &R) { | ||||||||||||||||||||
2245 | return BinaryOp_match<LHS, RHS, Instruction::Add, true>(L, R); | ||||||||||||||||||||
2246 | } | ||||||||||||||||||||
2247 | |||||||||||||||||||||
2248 | /// Matches a Mul with LHS and RHS in either order. | ||||||||||||||||||||
2249 | template <typename LHS, typename RHS> | ||||||||||||||||||||
2250 | inline BinaryOp_match<LHS, RHS, Instruction::Mul, true> m_c_Mul(const LHS &L, | ||||||||||||||||||||
2251 | const RHS &R) { | ||||||||||||||||||||
2252 | return BinaryOp_match<LHS, RHS, Instruction::Mul, true>(L, R); | ||||||||||||||||||||
2253 | } | ||||||||||||||||||||
2254 | |||||||||||||||||||||
2255 | /// Matches an And with LHS and RHS in either order. | ||||||||||||||||||||
2256 | template <typename LHS, typename RHS> | ||||||||||||||||||||
2257 | inline BinaryOp_match<LHS, RHS, Instruction::And, true> m_c_And(const LHS &L, | ||||||||||||||||||||
2258 | const RHS &R) { | ||||||||||||||||||||
2259 | return BinaryOp_match<LHS, RHS, Instruction::And, true>(L, R); | ||||||||||||||||||||
2260 | } | ||||||||||||||||||||
2261 | |||||||||||||||||||||
2262 | /// Matches an Or with LHS and RHS in either order. | ||||||||||||||||||||
2263 | template <typename LHS, typename RHS> | ||||||||||||||||||||
2264 | inline BinaryOp_match<LHS, RHS, Instruction::Or, true> m_c_Or(const LHS &L, | ||||||||||||||||||||
2265 | const RHS &R) { | ||||||||||||||||||||
2266 | return BinaryOp_match<LHS, RHS, Instruction::Or, true>(L, R); | ||||||||||||||||||||
2267 | } | ||||||||||||||||||||
2268 | |||||||||||||||||||||
2269 | /// Matches an Xor with LHS and RHS in either order. | ||||||||||||||||||||
2270 | template <typename LHS, typename RHS> | ||||||||||||||||||||
2271 | inline BinaryOp_match<LHS, RHS, Instruction::Xor, true> m_c_Xor(const LHS &L, | ||||||||||||||||||||
2272 | const RHS &R) { | ||||||||||||||||||||
2273 | return BinaryOp_match<LHS, RHS, Instruction::Xor, true>(L, R); | ||||||||||||||||||||
2274 | } | ||||||||||||||||||||
2275 | |||||||||||||||||||||
2276 | /// Matches a 'Neg' as 'sub 0, V'. | ||||||||||||||||||||
2277 | template <typename ValTy> | ||||||||||||||||||||
2278 | inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub> | ||||||||||||||||||||
2279 | m_Neg(const ValTy &V) { | ||||||||||||||||||||
2280 | return m_Sub(m_ZeroInt(), V); | ||||||||||||||||||||
2281 | } | ||||||||||||||||||||
2282 | |||||||||||||||||||||
2283 | /// Matches a 'Neg' as 'sub nsw 0, V'. | ||||||||||||||||||||
2284 | template <typename ValTy> | ||||||||||||||||||||
2285 | inline OverflowingBinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, | ||||||||||||||||||||
2286 | Instruction::Sub, | ||||||||||||||||||||
2287 | OverflowingBinaryOperator::NoSignedWrap> | ||||||||||||||||||||
2288 | m_NSWNeg(const ValTy &V) { | ||||||||||||||||||||
2289 | return m_NSWSub(m_ZeroInt(), V); | ||||||||||||||||||||
2290 | } | ||||||||||||||||||||
2291 | |||||||||||||||||||||
2292 | /// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'. | ||||||||||||||||||||
2293 | template <typename ValTy> | ||||||||||||||||||||
2294 | inline BinaryOp_match<ValTy, cst_pred_ty<is_all_ones>, Instruction::Xor, true> | ||||||||||||||||||||
2295 | m_Not(const ValTy &V) { | ||||||||||||||||||||
2296 | return m_c_Xor(V, m_AllOnes()); | ||||||||||||||||||||
2297 | } | ||||||||||||||||||||
2298 | |||||||||||||||||||||
2299 | template <typename ValTy> struct NotForbidUndef_match { | ||||||||||||||||||||
2300 | ValTy Val; | ||||||||||||||||||||
2301 | NotForbidUndef_match(const ValTy &V) : Val(V) {} | ||||||||||||||||||||
2302 | |||||||||||||||||||||
2303 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
2304 | // We do not use m_c_Xor because that could match an arbitrary APInt that is | ||||||||||||||||||||
2305 | // not -1 as C and then fail to match the other operand if it is -1. | ||||||||||||||||||||
2306 | // This code should still work even when both operands are constants. | ||||||||||||||||||||
2307 | Value *X; | ||||||||||||||||||||
2308 | const APInt *C; | ||||||||||||||||||||
2309 | if (m_Xor(m_Value(X), m_APIntForbidUndef(C)).match(V) && C->isAllOnes()) | ||||||||||||||||||||
2310 | return Val.match(X); | ||||||||||||||||||||
2311 | if (m_Xor(m_APIntForbidUndef(C), m_Value(X)).match(V) && C->isAllOnes()) | ||||||||||||||||||||
2312 | return Val.match(X); | ||||||||||||||||||||
2313 | return false; | ||||||||||||||||||||
2314 | } | ||||||||||||||||||||
2315 | }; | ||||||||||||||||||||
2316 | |||||||||||||||||||||
2317 | /// Matches a bitwise 'not' as 'xor V, -1' or 'xor -1, V'. For vectors, the | ||||||||||||||||||||
2318 | /// constant value must be composed of only -1 scalar elements. | ||||||||||||||||||||
2319 | template <typename ValTy> | ||||||||||||||||||||
2320 | inline NotForbidUndef_match<ValTy> m_NotForbidUndef(const ValTy &V) { | ||||||||||||||||||||
2321 | return NotForbidUndef_match<ValTy>(V); | ||||||||||||||||||||
2322 | } | ||||||||||||||||||||
2323 | |||||||||||||||||||||
2324 | /// Matches an SMin with LHS and RHS in either order. | ||||||||||||||||||||
2325 | template <typename LHS, typename RHS> | ||||||||||||||||||||
2326 | inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true> | ||||||||||||||||||||
2327 | m_c_SMin(const LHS &L, const RHS &R) { | ||||||||||||||||||||
2328 | return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>(L, R); | ||||||||||||||||||||
2329 | } | ||||||||||||||||||||
2330 | /// Matches an SMax with LHS and RHS in either order. | ||||||||||||||||||||
2331 | template <typename LHS, typename RHS> | ||||||||||||||||||||
2332 | inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true> | ||||||||||||||||||||
2333 | m_c_SMax(const LHS &L, const RHS &R) { | ||||||||||||||||||||
2334 | return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>(L, R); | ||||||||||||||||||||
2335 | } | ||||||||||||||||||||
2336 | /// Matches a UMin with LHS and RHS in either order. | ||||||||||||||||||||
2337 | template <typename LHS, typename RHS> | ||||||||||||||||||||
2338 | inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true> | ||||||||||||||||||||
2339 | m_c_UMin(const LHS &L, const RHS &R) { | ||||||||||||||||||||
2340 | return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>(L, R); | ||||||||||||||||||||
2341 | } | ||||||||||||||||||||
2342 | /// Matches a UMax with LHS and RHS in either order. | ||||||||||||||||||||
2343 | template <typename LHS, typename RHS> | ||||||||||||||||||||
2344 | inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true> | ||||||||||||||||||||
2345 | m_c_UMax(const LHS &L, const RHS &R) { | ||||||||||||||||||||
2346 | return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>(L, R); | ||||||||||||||||||||
2347 | } | ||||||||||||||||||||
2348 | |||||||||||||||||||||
2349 | template <typename LHS, typename RHS> | ||||||||||||||||||||
2350 | inline match_combine_or< | ||||||||||||||||||||
2351 | match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>, | ||||||||||||||||||||
2352 | MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>>, | ||||||||||||||||||||
2353 | match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>, | ||||||||||||||||||||
2354 | MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>>> | ||||||||||||||||||||
2355 | m_c_MaxOrMin(const LHS &L, const RHS &R) { | ||||||||||||||||||||
2356 | return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)), | ||||||||||||||||||||
2357 | m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R))); | ||||||||||||||||||||
2358 | } | ||||||||||||||||||||
2359 | |||||||||||||||||||||
2360 | /// Matches FAdd with LHS and RHS in either order. | ||||||||||||||||||||
2361 | template <typename LHS, typename RHS> | ||||||||||||||||||||
2362 | inline BinaryOp_match<LHS, RHS, Instruction::FAdd, true> | ||||||||||||||||||||
2363 | m_c_FAdd(const LHS &L, const RHS &R) { | ||||||||||||||||||||
2364 | return BinaryOp_match<LHS, RHS, Instruction::FAdd, true>(L, R); | ||||||||||||||||||||
2365 | } | ||||||||||||||||||||
2366 | |||||||||||||||||||||
2367 | /// Matches FMul with LHS and RHS in either order. | ||||||||||||||||||||
2368 | template <typename LHS, typename RHS> | ||||||||||||||||||||
2369 | inline BinaryOp_match<LHS, RHS, Instruction::FMul, true> | ||||||||||||||||||||
2370 | m_c_FMul(const LHS &L, const RHS &R) { | ||||||||||||||||||||
2371 | return BinaryOp_match<LHS, RHS, Instruction::FMul, true>(L, R); | ||||||||||||||||||||
2372 | } | ||||||||||||||||||||
2373 | |||||||||||||||||||||
2374 | template <typename Opnd_t> struct Signum_match { | ||||||||||||||||||||
2375 | Opnd_t Val; | ||||||||||||||||||||
2376 | Signum_match(const Opnd_t &V) : Val(V) {} | ||||||||||||||||||||
2377 | |||||||||||||||||||||
2378 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
2379 | unsigned TypeSize = V->getType()->getScalarSizeInBits(); | ||||||||||||||||||||
2380 | if (TypeSize == 0) | ||||||||||||||||||||
2381 | return false; | ||||||||||||||||||||
2382 | |||||||||||||||||||||
2383 | unsigned ShiftWidth = TypeSize - 1; | ||||||||||||||||||||
2384 | Value *OpL = nullptr, *OpR = nullptr; | ||||||||||||||||||||
2385 | |||||||||||||||||||||
2386 | // This is the representation of signum we match: | ||||||||||||||||||||
2387 | // | ||||||||||||||||||||
2388 | // signum(x) == (x >> 63) | (-x >>u 63) | ||||||||||||||||||||
2389 | // | ||||||||||||||||||||
2390 | // An i1 value is its own signum, so it's correct to match | ||||||||||||||||||||
2391 | // | ||||||||||||||||||||
2392 | // signum(x) == (x >> 0) | (-x >>u 0) | ||||||||||||||||||||
2393 | // | ||||||||||||||||||||
2394 | // for i1 values. | ||||||||||||||||||||
2395 | |||||||||||||||||||||
2396 | auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth)); | ||||||||||||||||||||
2397 | auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth)); | ||||||||||||||||||||
2398 | auto Signum = m_Or(LHS, RHS); | ||||||||||||||||||||
2399 | |||||||||||||||||||||
2400 | return Signum.match(V) && OpL == OpR && Val.match(OpL); | ||||||||||||||||||||
2401 | } | ||||||||||||||||||||
2402 | }; | ||||||||||||||||||||
2403 | |||||||||||||||||||||
2404 | /// Matches a signum pattern. | ||||||||||||||||||||
2405 | /// | ||||||||||||||||||||
2406 | /// signum(x) = | ||||||||||||||||||||
2407 | /// x > 0 -> 1 | ||||||||||||||||||||
2408 | /// x == 0 -> 0 | ||||||||||||||||||||
2409 | /// x < 0 -> -1 | ||||||||||||||||||||
2410 | template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) { | ||||||||||||||||||||
2411 | return Signum_match<Val_t>(V); | ||||||||||||||||||||
2412 | } | ||||||||||||||||||||
2413 | |||||||||||||||||||||
2414 | template <int Ind, typename Opnd_t> struct ExtractValue_match { | ||||||||||||||||||||
2415 | Opnd_t Val; | ||||||||||||||||||||
2416 | ExtractValue_match(const Opnd_t &V) : Val(V) {} | ||||||||||||||||||||
2417 | |||||||||||||||||||||
2418 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
2419 | if (auto *I = dyn_cast<ExtractValueInst>(V)) { | ||||||||||||||||||||
2420 | // If Ind is -1, don't inspect indices | ||||||||||||||||||||
2421 | if (Ind != -1 && | ||||||||||||||||||||
2422 | !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind)) | ||||||||||||||||||||
2423 | return false; | ||||||||||||||||||||
2424 | return Val.match(I->getAggregateOperand()); | ||||||||||||||||||||
2425 | } | ||||||||||||||||||||
2426 | return false; | ||||||||||||||||||||
2427 | } | ||||||||||||||||||||
2428 | }; | ||||||||||||||||||||
2429 | |||||||||||||||||||||
2430 | /// Match a single index ExtractValue instruction. | ||||||||||||||||||||
2431 | /// For example m_ExtractValue<1>(...) | ||||||||||||||||||||
2432 | template <int Ind, typename Val_t> | ||||||||||||||||||||
2433 | inline ExtractValue_match<Ind, Val_t> m_ExtractValue(const Val_t &V) { | ||||||||||||||||||||
2434 | return ExtractValue_match<Ind, Val_t>(V); | ||||||||||||||||||||
2435 | } | ||||||||||||||||||||
2436 | |||||||||||||||||||||
2437 | /// Match an ExtractValue instruction with any index. | ||||||||||||||||||||
2438 | /// For example m_ExtractValue(...) | ||||||||||||||||||||
2439 | template <typename Val_t> | ||||||||||||||||||||
2440 | inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) { | ||||||||||||||||||||
2441 | return ExtractValue_match<-1, Val_t>(V); | ||||||||||||||||||||
2442 | } | ||||||||||||||||||||
2443 | |||||||||||||||||||||
2444 | /// Matcher for a single index InsertValue instruction. | ||||||||||||||||||||
2445 | template <int Ind, typename T0, typename T1> struct InsertValue_match { | ||||||||||||||||||||
2446 | T0 Op0; | ||||||||||||||||||||
2447 | T1 Op1; | ||||||||||||||||||||
2448 | |||||||||||||||||||||
2449 | InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {} | ||||||||||||||||||||
2450 | |||||||||||||||||||||
2451 | template <typename OpTy> bool match(OpTy *V) { | ||||||||||||||||||||
2452 | if (auto *I = dyn_cast<InsertValueInst>(V)) { | ||||||||||||||||||||
2453 | return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) && | ||||||||||||||||||||
2454 | I->getNumIndices() == 1 && Ind == I->getIndices()[0]; | ||||||||||||||||||||
2455 | } | ||||||||||||||||||||
2456 | return false; | ||||||||||||||||||||
2457 | } | ||||||||||||||||||||
2458 | }; | ||||||||||||||||||||
2459 | |||||||||||||||||||||
2460 | /// Matches a single index InsertValue instruction. | ||||||||||||||||||||
2461 | template <int Ind, typename Val_t, typename Elt_t> | ||||||||||||||||||||
2462 | inline InsertValue_match<Ind, Val_t, Elt_t> m_InsertValue(const Val_t &Val, | ||||||||||||||||||||
2463 | const Elt_t &Elt) { | ||||||||||||||||||||
2464 | return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt); | ||||||||||||||||||||
2465 | } | ||||||||||||||||||||
2466 | |||||||||||||||||||||
2467 | /// Matches patterns for `vscale`. This can either be a call to `llvm.vscale` or | ||||||||||||||||||||
2468 | /// the constant expression | ||||||||||||||||||||
2469 | /// `ptrtoint(gep <vscale x 1 x i8>, <vscale x 1 x i8>* null, i32 1>` | ||||||||||||||||||||
2470 | /// under the right conditions determined by DataLayout. | ||||||||||||||||||||
2471 | struct VScaleVal_match { | ||||||||||||||||||||
2472 | const DataLayout &DL; | ||||||||||||||||||||
2473 | VScaleVal_match(const DataLayout &DL) : DL(DL) {} | ||||||||||||||||||||
2474 | |||||||||||||||||||||
2475 | template <typename ITy> bool match(ITy *V) { | ||||||||||||||||||||
2476 | if (m_Intrinsic<Intrinsic::vscale>().match(V)) | ||||||||||||||||||||
2477 | return true; | ||||||||||||||||||||
2478 | |||||||||||||||||||||
2479 | Value *Ptr; | ||||||||||||||||||||
2480 | if (m_PtrToInt(m_Value(Ptr)).match(V)) { | ||||||||||||||||||||
2481 | if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) { | ||||||||||||||||||||
2482 | auto *DerefTy = GEP->getSourceElementType(); | ||||||||||||||||||||
2483 | if (GEP->getNumIndices() == 1 && isa<ScalableVectorType>(DerefTy) && | ||||||||||||||||||||
2484 | m_Zero().match(GEP->getPointerOperand()) && | ||||||||||||||||||||
2485 | m_SpecificInt(1).match(GEP->idx_begin()->get()) && | ||||||||||||||||||||
2486 | DL.getTypeAllocSizeInBits(DerefTy).getKnownMinSize() == 8) | ||||||||||||||||||||
2487 | return true; | ||||||||||||||||||||
2488 | } | ||||||||||||||||||||
2489 | } | ||||||||||||||||||||
2490 | |||||||||||||||||||||
2491 | return false; | ||||||||||||||||||||
2492 | } | ||||||||||||||||||||
2493 | }; | ||||||||||||||||||||
2494 | |||||||||||||||||||||
2495 | inline VScaleVal_match m_VScale(const DataLayout &DL) { | ||||||||||||||||||||
2496 | return VScaleVal_match(DL); | ||||||||||||||||||||
2497 | } | ||||||||||||||||||||
2498 | |||||||||||||||||||||
2499 | template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false> | ||||||||||||||||||||
2500 | struct LogicalOp_match { | ||||||||||||||||||||
2501 | LHS L; | ||||||||||||||||||||
2502 | RHS R; | ||||||||||||||||||||
2503 | |||||||||||||||||||||
2504 | LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {} | ||||||||||||||||||||
2505 | |||||||||||||||||||||
2506 | template <typename T> bool match(T *V) { | ||||||||||||||||||||
2507 | auto *I = dyn_cast<Instruction>(V); | ||||||||||||||||||||
2508 | if (!I || !I->getType()->isIntOrIntVectorTy(1)) | ||||||||||||||||||||
2509 | return false; | ||||||||||||||||||||
2510 | |||||||||||||||||||||
2511 | if (I->getOpcode() == Opcode) { | ||||||||||||||||||||
2512 | auto *Op0 = I->getOperand(0); | ||||||||||||||||||||
2513 | auto *Op1 = I->getOperand(1); | ||||||||||||||||||||
2514 | return (L.match(Op0) && R.match(Op1)) || | ||||||||||||||||||||
2515 | (Commutable && L.match(Op1) && R.match(Op0)); | ||||||||||||||||||||
2516 | } | ||||||||||||||||||||
2517 | |||||||||||||||||||||
2518 | if (auto *Select = dyn_cast<SelectInst>(I)) { | ||||||||||||||||||||
2519 | auto *Cond = Select->getCondition(); | ||||||||||||||||||||
2520 | auto *TVal = Select->getTrueValue(); | ||||||||||||||||||||
2521 | auto *FVal = Select->getFalseValue(); | ||||||||||||||||||||
2522 | if (Opcode == Instruction::And) { | ||||||||||||||||||||
2523 | auto *C = dyn_cast<Constant>(FVal); | ||||||||||||||||||||
2524 | if (C && C->isNullValue()) | ||||||||||||||||||||
2525 | return (L.match(Cond) && R.match(TVal)) || | ||||||||||||||||||||
2526 | (Commutable && L.match(TVal) && R.match(Cond)); | ||||||||||||||||||||
2527 | } else { | ||||||||||||||||||||
2528 | assert(Opcode == Instruction::Or)(static_cast <bool> (Opcode == Instruction::Or) ? void ( 0) : __assert_fail ("Opcode == Instruction::Or", "llvm/include/llvm/IR/PatternMatch.h" , 2528, __extension__ __PRETTY_FUNCTION__)); | ||||||||||||||||||||
2529 | auto *C = dyn_cast<Constant>(TVal); | ||||||||||||||||||||
2530 | if (C && C->isOneValue()) | ||||||||||||||||||||
2531 | return (L.match(Cond) && R.match(FVal)) || | ||||||||||||||||||||
2532 | (Commutable && L.match(FVal) && R.match(Cond)); | ||||||||||||||||||||
2533 | } | ||||||||||||||||||||
2534 | } | ||||||||||||||||||||
2535 | |||||||||||||||||||||
2536 | return false; | ||||||||||||||||||||
2537 | } | ||||||||||||||||||||
2538 | }; | ||||||||||||||||||||
2539 | |||||||||||||||||||||
2540 | /// Matches L && R either in the form of L & R or L ? R : false. | ||||||||||||||||||||
2541 | /// Note that the latter form is poison-blocking. | ||||||||||||||||||||
2542 | template <typename LHS, typename RHS> | ||||||||||||||||||||
2543 | inline LogicalOp_match<LHS, RHS, Instruction::And> | ||||||||||||||||||||
2544 | m_LogicalAnd(const LHS &L, const RHS &R) { | ||||||||||||||||||||
2545 | return LogicalOp_match<LHS, RHS, Instruction::And>(L, R); | ||||||||||||||||||||
2546 | } | ||||||||||||||||||||
2547 | |||||||||||||||||||||
2548 | /// Matches L && R where L and R are arbitrary values. | ||||||||||||||||||||
2549 | inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); } | ||||||||||||||||||||
2550 | |||||||||||||||||||||
2551 | /// Matches L && R with LHS and RHS in either order. | ||||||||||||||||||||
2552 | template <typename LHS, typename RHS> | ||||||||||||||||||||
2553 | inline LogicalOp_match<LHS, RHS, Instruction::And, true> | ||||||||||||||||||||
2554 | m_c_LogicalAnd(const LHS &L, const RHS &R) { | ||||||||||||||||||||
2555 | return LogicalOp_match<LHS, RHS, Instruction::And, true>(L, R); | ||||||||||||||||||||
2556 | } | ||||||||||||||||||||
2557 | |||||||||||||||||||||
2558 | /// Matches L || R either in the form of L | R or L ? true : R. | ||||||||||||||||||||
2559 | /// Note that the latter form is poison-blocking. | ||||||||||||||||||||
2560 | template <typename LHS, typename RHS> | ||||||||||||||||||||
2561 | inline LogicalOp_match<LHS, RHS, Instruction::Or> | ||||||||||||||||||||
2562 | m_LogicalOr(const LHS &L, const RHS &R) { | ||||||||||||||||||||
2563 | return LogicalOp_match<LHS, RHS, Instruction::Or>(L, R); | ||||||||||||||||||||
2564 | } | ||||||||||||||||||||
2565 | |||||||||||||||||||||
2566 | /// Matches L || R where L and R are arbitrary values. | ||||||||||||||||||||
2567 | inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); } | ||||||||||||||||||||
2568 | |||||||||||||||||||||
2569 | /// Matches L || R with LHS and RHS in either order. | ||||||||||||||||||||
2570 | template <typename LHS, typename RHS> | ||||||||||||||||||||
2571 | inline LogicalOp_match<LHS, RHS, Instruction::Or, true> | ||||||||||||||||||||
2572 | m_c_LogicalOr(const LHS &L, const RHS &R) { | ||||||||||||||||||||
2573 | return LogicalOp_match<LHS, RHS, Instruction::Or, true>(L, R); | ||||||||||||||||||||
2574 | } | ||||||||||||||||||||
2575 | |||||||||||||||||||||
2576 | } // end namespace PatternMatch | ||||||||||||||||||||
2577 | } // end namespace llvm | ||||||||||||||||||||
2578 | |||||||||||||||||||||
2579 | #endif // LLVM_IR_PATTERNMATCH_H |
1 | //===- llvm/Analysis/AliasAnalysis.h - Alias Analysis Interface -*- 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 the generic AliasAnalysis interface, which is used as the |
10 | // common interface used by all clients of alias analysis information, and |
11 | // implemented by all alias analysis implementations. Mod/Ref information is |
12 | // also captured by this interface. |
13 | // |
14 | // Implementations of this interface must implement the various virtual methods, |
15 | // which automatically provides functionality for the entire suite of client |
16 | // APIs. |
17 | // |
18 | // This API identifies memory regions with the MemoryLocation class. The pointer |
19 | // component specifies the base memory address of the region. The Size specifies |
20 | // the maximum size (in address units) of the memory region, or |
21 | // MemoryLocation::UnknownSize if the size is not known. The TBAA tag |
22 | // identifies the "type" of the memory reference; see the |
23 | // TypeBasedAliasAnalysis class for details. |
24 | // |
25 | // Some non-obvious details include: |
26 | // - Pointers that point to two completely different objects in memory never |
27 | // alias, regardless of the value of the Size component. |
28 | // - NoAlias doesn't imply inequal pointers. The most obvious example of this |
29 | // is two pointers to constant memory. Even if they are equal, constant |
30 | // memory is never stored to, so there will never be any dependencies. |
31 | // In this and other situations, the pointers may be both NoAlias and |
32 | // MustAlias at the same time. The current API can only return one result, |
33 | // though this is rarely a problem in practice. |
34 | // |
35 | //===----------------------------------------------------------------------===// |
36 | |
37 | #ifndef LLVM_ANALYSIS_ALIASANALYSIS_H |
38 | #define LLVM_ANALYSIS_ALIASANALYSIS_H |
39 | |
40 | #include "llvm/ADT/DenseMap.h" |
41 | #include "llvm/ADT/Optional.h" |
42 | #include "llvm/ADT/SmallVector.h" |
43 | #include "llvm/Analysis/MemoryLocation.h" |
44 | #include "llvm/IR/PassManager.h" |
45 | #include "llvm/Pass.h" |
46 | #include <cstdint> |
47 | #include <functional> |
48 | #include <memory> |
49 | #include <vector> |
50 | |
51 | namespace llvm { |
52 | |
53 | class AnalysisUsage; |
54 | class AtomicCmpXchgInst; |
55 | class BasicAAResult; |
56 | class BasicBlock; |
57 | class CatchPadInst; |
58 | class CatchReturnInst; |
59 | class DominatorTree; |
60 | class FenceInst; |
61 | class Function; |
62 | class LoopInfo; |
63 | class PreservedAnalyses; |
64 | class TargetLibraryInfo; |
65 | class Value; |
66 | |
67 | /// The possible results of an alias query. |
68 | /// |
69 | /// These results are always computed between two MemoryLocation objects as |
70 | /// a query to some alias analysis. |
71 | /// |
72 | /// Note that these are unscoped enumerations because we would like to support |
73 | /// implicitly testing a result for the existence of any possible aliasing with |
74 | /// a conversion to bool, but an "enum class" doesn't support this. The |
75 | /// canonical names from the literature are suffixed and unique anyways, and so |
76 | /// they serve as global constants in LLVM for these results. |
77 | /// |
78 | /// See docs/AliasAnalysis.html for more information on the specific meanings |
79 | /// of these values. |
80 | class AliasResult { |
81 | private: |
82 | static const int OffsetBits = 23; |
83 | static const int AliasBits = 8; |
84 | static_assert(AliasBits + 1 + OffsetBits <= 32, |
85 | "AliasResult size is intended to be 4 bytes!"); |
86 | |
87 | unsigned int Alias : AliasBits; |
88 | unsigned int HasOffset : 1; |
89 | signed int Offset : OffsetBits; |
90 | |
91 | public: |
92 | enum Kind : uint8_t { |
93 | /// The two locations do not alias at all. |
94 | /// |
95 | /// This value is arranged to convert to false, while all other values |
96 | /// convert to true. This allows a boolean context to convert the result to |
97 | /// a binary flag indicating whether there is the possibility of aliasing. |
98 | NoAlias = 0, |
99 | /// The two locations may or may not alias. This is the least precise |
100 | /// result. |
101 | MayAlias, |
102 | /// The two locations alias, but only due to a partial overlap. |
103 | PartialAlias, |
104 | /// The two locations precisely alias each other. |
105 | MustAlias, |
106 | }; |
107 | static_assert(MustAlias < (1 << AliasBits), |
108 | "Not enough bit field size for the enum!"); |
109 | |
110 | explicit AliasResult() = delete; |
111 | constexpr AliasResult(const Kind &Alias) |
112 | : Alias(Alias), HasOffset(false), Offset(0) {} |
113 | |
114 | operator Kind() const { return static_cast<Kind>(Alias); } |
115 | |
116 | constexpr bool hasOffset() const { return HasOffset; } |
117 | constexpr int32_t getOffset() const { |
118 | assert(HasOffset && "No offset!")(static_cast <bool> (HasOffset && "No offset!") ? void (0) : __assert_fail ("HasOffset && \"No offset!\"" , "llvm/include/llvm/Analysis/AliasAnalysis.h", 118, __extension__ __PRETTY_FUNCTION__)); |
119 | return Offset; |
120 | } |
121 | void setOffset(int32_t NewOffset) { |
122 | if (isInt<OffsetBits>(NewOffset)) { |
123 | HasOffset = true; |
124 | Offset = NewOffset; |
125 | } |
126 | } |
127 | |
128 | /// Helper for processing AliasResult for swapped memory location pairs. |
129 | void swap(bool DoSwap = true) { |
130 | if (DoSwap && hasOffset()) |
131 | setOffset(-getOffset()); |
132 | } |
133 | }; |
134 | |
135 | static_assert(sizeof(AliasResult) == 4, |
136 | "AliasResult size is intended to be 4 bytes!"); |
137 | |
138 | /// << operator for AliasResult. |
139 | raw_ostream &operator<<(raw_ostream &OS, AliasResult AR); |
140 | |
141 | /// Flags indicating whether a memory access modifies or references memory. |
142 | /// |
143 | /// This is no access at all, a modification, a reference, or both |
144 | /// a modification and a reference. These are specifically structured such that |
145 | /// they form a three bit matrix and bit-tests for 'mod' or 'ref' or 'must' |
146 | /// work with any of the possible values. |
147 | enum class ModRefInfo : uint8_t { |
148 | /// Must is provided for completeness, but no routines will return only |
149 | /// Must today. See definition of Must below. |
150 | Must = 0, |
151 | /// The access may reference the value stored in memory, |
152 | /// a mustAlias relation was found, and no mayAlias or partialAlias found. |
153 | MustRef = 1, |
154 | /// The access may modify the value stored in memory, |
155 | /// a mustAlias relation was found, and no mayAlias or partialAlias found. |
156 | MustMod = 2, |
157 | /// The access may reference, modify or both the value stored in memory, |
158 | /// a mustAlias relation was found, and no mayAlias or partialAlias found. |
159 | MustModRef = MustRef | MustMod, |
160 | /// The access neither references nor modifies the value stored in memory. |
161 | NoModRef = 4, |
162 | /// The access may reference the value stored in memory. |
163 | Ref = NoModRef | MustRef, |
164 | /// The access may modify the value stored in memory. |
165 | Mod = NoModRef | MustMod, |
166 | /// The access may reference and may modify the value stored in memory. |
167 | ModRef = Ref | Mod, |
168 | |
169 | /// About Must: |
170 | /// Must is set in a best effort manner. |
171 | /// We usually do not try our best to infer Must, instead it is merely |
172 | /// another piece of "free" information that is presented when available. |
173 | /// Must set means there was certainly a MustAlias found. For calls, |
174 | /// where multiple arguments are checked (argmemonly), this translates to |
175 | /// only MustAlias or NoAlias was found. |
176 | /// Must is not set for RAR accesses, even if the two locations must |
177 | /// alias. The reason is that two read accesses translate to an early return |
178 | /// of NoModRef. An additional alias check to set Must may be |
179 | /// expensive. Other cases may also not set Must(e.g. callCapturesBefore). |
180 | /// We refer to Must being *set* when the most significant bit is *cleared*. |
181 | /// Conversely we *clear* Must information by *setting* the Must bit to 1. |
182 | }; |
183 | |
184 | LLVM_NODISCARD[[clang::warn_unused_result]] inline bool isNoModRef(const ModRefInfo MRI) { |
185 | return (static_cast<int>(MRI) & static_cast<int>(ModRefInfo::MustModRef)) == |
186 | static_cast<int>(ModRefInfo::Must); |
187 | } |
188 | LLVM_NODISCARD[[clang::warn_unused_result]] inline bool isModOrRefSet(const ModRefInfo MRI) { |
189 | return static_cast<int>(MRI) & static_cast<int>(ModRefInfo::MustModRef); |
190 | } |
191 | LLVM_NODISCARD[[clang::warn_unused_result]] inline bool isModAndRefSet(const ModRefInfo MRI) { |
192 | return (static_cast<int>(MRI) & static_cast<int>(ModRefInfo::MustModRef)) == |
193 | static_cast<int>(ModRefInfo::MustModRef); |
194 | } |
195 | LLVM_NODISCARD[[clang::warn_unused_result]] inline bool isModSet(const ModRefInfo MRI) { |
196 | return static_cast<int>(MRI) & static_cast<int>(ModRefInfo::MustMod); |
197 | } |
198 | LLVM_NODISCARD[[clang::warn_unused_result]] inline bool isRefSet(const ModRefInfo MRI) { |
199 | return static_cast<int>(MRI) & static_cast<int>(ModRefInfo::MustRef); |
200 | } |
201 | LLVM_NODISCARD[[clang::warn_unused_result]] inline bool isMustSet(const ModRefInfo MRI) { |
202 | return !(static_cast<int>(MRI) & static_cast<int>(ModRefInfo::NoModRef)); |
203 | } |
204 | |
205 | LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo setMod(const ModRefInfo MRI) { |
206 | return ModRefInfo(static_cast<int>(MRI) | |
207 | static_cast<int>(ModRefInfo::MustMod)); |
208 | } |
209 | LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo setRef(const ModRefInfo MRI) { |
210 | return ModRefInfo(static_cast<int>(MRI) | |
211 | static_cast<int>(ModRefInfo::MustRef)); |
212 | } |
213 | LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo setMust(const ModRefInfo MRI) { |
214 | return ModRefInfo(static_cast<int>(MRI) & |
215 | static_cast<int>(ModRefInfo::MustModRef)); |
216 | } |
217 | LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo setModAndRef(const ModRefInfo MRI) { |
218 | return ModRefInfo(static_cast<int>(MRI) | |
219 | static_cast<int>(ModRefInfo::MustModRef)); |
220 | } |
221 | LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo clearMod(const ModRefInfo MRI) { |
222 | return ModRefInfo(static_cast<int>(MRI) & static_cast<int>(ModRefInfo::Ref)); |
223 | } |
224 | LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo clearRef(const ModRefInfo MRI) { |
225 | return ModRefInfo(static_cast<int>(MRI) & static_cast<int>(ModRefInfo::Mod)); |
226 | } |
227 | LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo clearMust(const ModRefInfo MRI) { |
228 | return ModRefInfo(static_cast<int>(MRI) | |
229 | static_cast<int>(ModRefInfo::NoModRef)); |
230 | } |
231 | LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo unionModRef(const ModRefInfo MRI1, |
232 | const ModRefInfo MRI2) { |
233 | return ModRefInfo(static_cast<int>(MRI1) | static_cast<int>(MRI2)); |
234 | } |
235 | LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo intersectModRef(const ModRefInfo MRI1, |
236 | const ModRefInfo MRI2) { |
237 | return ModRefInfo(static_cast<int>(MRI1) & static_cast<int>(MRI2)); |
238 | } |
239 | |
240 | /// The locations at which a function might access memory. |
241 | /// |
242 | /// These are primarily used in conjunction with the \c AccessKind bits to |
243 | /// describe both the nature of access and the locations of access for a |
244 | /// function call. |
245 | enum FunctionModRefLocation { |
246 | /// Base case is no access to memory. |
247 | FMRL_Nowhere = 0, |
248 | /// Access to memory via argument pointers. |
249 | FMRL_ArgumentPointees = 8, |
250 | /// Memory that is inaccessible via LLVM IR. |
251 | FMRL_InaccessibleMem = 16, |
252 | /// Access to any memory. |
253 | FMRL_Anywhere = 32 | FMRL_InaccessibleMem | FMRL_ArgumentPointees |
254 | }; |
255 | |
256 | /// Summary of how a function affects memory in the program. |
257 | /// |
258 | /// Loads from constant globals are not considered memory accesses for this |
259 | /// interface. Also, functions may freely modify stack space local to their |
260 | /// invocation without having to report it through these interfaces. |
261 | enum FunctionModRefBehavior { |
262 | /// This function does not perform any non-local loads or stores to memory. |
263 | /// |
264 | /// This property corresponds to the GCC 'const' attribute. |
265 | /// This property corresponds to the LLVM IR 'readnone' attribute. |
266 | /// This property corresponds to the IntrNoMem LLVM intrinsic flag. |
267 | FMRB_DoesNotAccessMemory = |
268 | FMRL_Nowhere | static_cast<int>(ModRefInfo::NoModRef), |
269 | |
270 | /// The only memory references in this function (if it has any) are |
271 | /// non-volatile loads from objects pointed to by its pointer-typed |
272 | /// arguments, with arbitrary offsets. |
273 | /// |
274 | /// This property corresponds to the combination of the IntrReadMem |
275 | /// and IntrArgMemOnly LLVM intrinsic flags. |
276 | FMRB_OnlyReadsArgumentPointees = |
277 | FMRL_ArgumentPointees | static_cast<int>(ModRefInfo::Ref), |
278 | |
279 | /// The only memory references in this function (if it has any) are |
280 | /// non-volatile stores from objects pointed to by its pointer-typed |
281 | /// arguments, with arbitrary offsets. |
282 | /// |
283 | /// This property corresponds to the combination of the IntrWriteMem |
284 | /// and IntrArgMemOnly LLVM intrinsic flags. |
285 | FMRB_OnlyWritesArgumentPointees = |
286 | FMRL_ArgumentPointees | static_cast<int>(ModRefInfo::Mod), |
287 | |
288 | /// The only memory references in this function (if it has any) are |
289 | /// non-volatile loads and stores from objects pointed to by its |
290 | /// pointer-typed arguments, with arbitrary offsets. |
291 | /// |
292 | /// This property corresponds to the IntrArgMemOnly LLVM intrinsic flag. |
293 | FMRB_OnlyAccessesArgumentPointees = |
294 | FMRL_ArgumentPointees | static_cast<int>(ModRefInfo::ModRef), |
295 | |
296 | /// The only memory references in this function (if it has any) are |
297 | /// reads of memory that is otherwise inaccessible via LLVM IR. |
298 | /// |
299 | /// This property corresponds to the LLVM IR inaccessiblememonly attribute. |
300 | FMRB_OnlyReadsInaccessibleMem = |
301 | FMRL_InaccessibleMem | static_cast<int>(ModRefInfo::Ref), |
302 | |
303 | /// The only memory references in this function (if it has any) are |
304 | /// writes to memory that is otherwise inaccessible via LLVM IR. |
305 | /// |
306 | /// This property corresponds to the LLVM IR inaccessiblememonly attribute. |
307 | FMRB_OnlyWritesInaccessibleMem = |
308 | FMRL_InaccessibleMem | static_cast<int>(ModRefInfo::Mod), |
309 | |
310 | /// The only memory references in this function (if it has any) are |
311 | /// references of memory that is otherwise inaccessible via LLVM IR. |
312 | /// |
313 | /// This property corresponds to the LLVM IR inaccessiblememonly attribute. |
314 | FMRB_OnlyAccessesInaccessibleMem = |
315 | FMRL_InaccessibleMem | static_cast<int>(ModRefInfo::ModRef), |
316 | |
317 | /// The function may perform non-volatile loads from objects pointed |
318 | /// to by its pointer-typed arguments, with arbitrary offsets, and |
319 | /// it may also perform loads of memory that is otherwise |
320 | /// inaccessible via LLVM IR. |
321 | /// |
322 | /// This property corresponds to the LLVM IR |
323 | /// inaccessiblemem_or_argmemonly attribute. |
324 | FMRB_OnlyReadsInaccessibleOrArgMem = FMRL_InaccessibleMem | |
325 | FMRL_ArgumentPointees | |
326 | static_cast<int>(ModRefInfo::Ref), |
327 | |
328 | /// The function may perform non-volatile stores to objects pointed |
329 | /// to by its pointer-typed arguments, with arbitrary offsets, and |
330 | /// it may also perform stores of memory that is otherwise |
331 | /// inaccessible via LLVM IR. |
332 | /// |
333 | /// This property corresponds to the LLVM IR |
334 | /// inaccessiblemem_or_argmemonly attribute. |
335 | FMRB_OnlyWritesInaccessibleOrArgMem = FMRL_InaccessibleMem | |
336 | FMRL_ArgumentPointees | |
337 | static_cast<int>(ModRefInfo::Mod), |
338 | |
339 | /// The function may perform non-volatile loads and stores of objects |
340 | /// pointed to by its pointer-typed arguments, with arbitrary offsets, and |
341 | /// it may also perform loads and stores of memory that is otherwise |
342 | /// inaccessible via LLVM IR. |
343 | /// |
344 | /// This property corresponds to the LLVM IR |
345 | /// inaccessiblemem_or_argmemonly attribute. |
346 | FMRB_OnlyAccessesInaccessibleOrArgMem = FMRL_InaccessibleMem | |
347 | FMRL_ArgumentPointees | |
348 | static_cast<int>(ModRefInfo::ModRef), |
349 | |
350 | /// This function does not perform any non-local stores or volatile loads, |
351 | /// but may read from any memory location. |
352 | /// |
353 | /// This property corresponds to the GCC 'pure' attribute. |
354 | /// This property corresponds to the LLVM IR 'readonly' attribute. |
355 | /// This property corresponds to the IntrReadMem LLVM intrinsic flag. |
356 | FMRB_OnlyReadsMemory = FMRL_Anywhere | static_cast<int>(ModRefInfo::Ref), |
357 | |
358 | // This function does not read from memory anywhere, but may write to any |
359 | // memory location. |
360 | // |
361 | // This property corresponds to the LLVM IR 'writeonly' attribute. |
362 | // This property corresponds to the IntrWriteMem LLVM intrinsic flag. |
363 | FMRB_OnlyWritesMemory = FMRL_Anywhere | static_cast<int>(ModRefInfo::Mod), |
364 | |
365 | /// This indicates that the function could not be classified into one of the |
366 | /// behaviors above. |
367 | FMRB_UnknownModRefBehavior = |
368 | FMRL_Anywhere | static_cast<int>(ModRefInfo::ModRef) |
369 | }; |
370 | |
371 | // Wrapper method strips bits significant only in FunctionModRefBehavior, |
372 | // to obtain a valid ModRefInfo. The benefit of using the wrapper is that if |
373 | // ModRefInfo enum changes, the wrapper can be updated to & with the new enum |
374 | // entry with all bits set to 1. |
375 | LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo |
376 | createModRefInfo(const FunctionModRefBehavior FMRB) { |
377 | return ModRefInfo(FMRB & static_cast<int>(ModRefInfo::ModRef)); |
378 | } |
379 | |
380 | /// Virtual base class for providers of capture information. |
381 | struct CaptureInfo { |
382 | virtual ~CaptureInfo() = 0; |
383 | virtual bool isNotCapturedBeforeOrAt(const Value *Object, |
384 | const Instruction *I) = 0; |
385 | }; |
386 | |
387 | /// Context-free CaptureInfo provider, which computes and caches whether an |
388 | /// object is captured in the function at all, but does not distinguish whether |
389 | /// it was captured before or after the context instruction. |
390 | class SimpleCaptureInfo final : public CaptureInfo { |
391 | SmallDenseMap<const Value *, bool, 8> IsCapturedCache; |
392 | |
393 | public: |
394 | bool isNotCapturedBeforeOrAt(const Value *Object, |
395 | const Instruction *I) override; |
396 | }; |
397 | |
398 | /// Context-sensitive CaptureInfo provider, which computes and caches the |
399 | /// earliest common dominator closure of all captures. It provides a good |
400 | /// approximation to a precise "captures before" analysis. |
401 | class EarliestEscapeInfo final : public CaptureInfo { |
402 | DominatorTree &DT; |
403 | const LoopInfo &LI; |
404 | |
405 | /// Map from identified local object to an instruction before which it does |
406 | /// not escape, or nullptr if it never escapes. The "earliest" instruction |
407 | /// may be a conservative approximation, e.g. the first instruction in the |
408 | /// function is always a legal choice. |
409 | DenseMap<const Value *, Instruction *> EarliestEscapes; |
410 | |
411 | /// Reverse map from instruction to the objects it is the earliest escape for. |
412 | /// This is used for cache invalidation purposes. |
413 | DenseMap<Instruction *, TinyPtrVector<const Value *>> Inst2Obj; |
414 | |
415 | public: |
416 | EarliestEscapeInfo(DominatorTree &DT, const LoopInfo &LI) : DT(DT), LI(LI) {} |
417 | |
418 | bool isNotCapturedBeforeOrAt(const Value *Object, |
419 | const Instruction *I) override; |
420 | |
421 | void removeInstruction(Instruction *I); |
422 | }; |
423 | |
424 | /// Reduced version of MemoryLocation that only stores a pointer and size. |
425 | /// Used for caching AATags independent BasicAA results. |
426 | struct AACacheLoc { |
427 | const Value *Ptr; |
428 | LocationSize Size; |
429 | }; |
430 | |
431 | template <> struct DenseMapInfo<AACacheLoc> { |
432 | static inline AACacheLoc getEmptyKey() { |
433 | return {DenseMapInfo<const Value *>::getEmptyKey(), |
434 | DenseMapInfo<LocationSize>::getEmptyKey()}; |
435 | } |
436 | static inline AACacheLoc getTombstoneKey() { |
437 | return {DenseMapInfo<const Value *>::getTombstoneKey(), |
438 | DenseMapInfo<LocationSize>::getTombstoneKey()}; |
439 | } |
440 | static unsigned getHashValue(const AACacheLoc &Val) { |
441 | return DenseMapInfo<const Value *>::getHashValue(Val.Ptr) ^ |
442 | DenseMapInfo<LocationSize>::getHashValue(Val.Size); |
443 | } |
444 | static bool isEqual(const AACacheLoc &LHS, const AACacheLoc &RHS) { |
445 | return LHS.Ptr == RHS.Ptr && LHS.Size == RHS.Size; |
446 | } |
447 | }; |
448 | |
449 | /// This class stores info we want to provide to or retain within an alias |
450 | /// query. By default, the root query is stateless and starts with a freshly |
451 | /// constructed info object. Specific alias analyses can use this query info to |
452 | /// store per-query state that is important for recursive or nested queries to |
453 | /// avoid recomputing. To enable preserving this state across multiple queries |
454 | /// where safe (due to the IR not changing), use a `BatchAAResults` wrapper. |
455 | /// The information stored in an `AAQueryInfo` is currently limitted to the |
456 | /// caches used by BasicAA, but can further be extended to fit other AA needs. |
457 | class AAQueryInfo { |
458 | public: |
459 | using LocPair = std::pair<AACacheLoc, AACacheLoc>; |
460 | struct CacheEntry { |
461 | AliasResult Result; |
462 | /// Number of times a NoAlias assumption has been used. |
463 | /// 0 for assumptions that have not been used, -1 for definitive results. |
464 | int NumAssumptionUses; |
465 | /// Whether this is a definitive (non-assumption) result. |
466 | bool isDefinitive() const { return NumAssumptionUses < 0; } |
467 | }; |
468 | using AliasCacheT = SmallDenseMap<LocPair, CacheEntry, 8>; |
469 | AliasCacheT AliasCache; |
470 | |
471 | CaptureInfo *CI; |
472 | |
473 | /// Query depth used to distinguish recursive queries. |
474 | unsigned Depth = 0; |
475 | |
476 | /// How many active NoAlias assumption uses there are. |
477 | int NumAssumptionUses = 0; |
478 | |
479 | /// Location pairs for which an assumption based result is currently stored. |
480 | /// Used to remove all potentially incorrect results from the cache if an |
481 | /// assumption is disproven. |
482 | SmallVector<AAQueryInfo::LocPair, 4> AssumptionBasedResults; |
483 | |
484 | AAQueryInfo(CaptureInfo *CI) : CI(CI) {} |
485 | |
486 | /// Create a new AAQueryInfo based on this one, but with the cache cleared. |
487 | /// This is used for recursive queries across phis, where cache results may |
488 | /// not be valid. |
489 | AAQueryInfo withEmptyCache() { |
490 | AAQueryInfo NewAAQI(CI); |
491 | NewAAQI.Depth = Depth; |
492 | return NewAAQI; |
493 | } |
494 | }; |
495 | |
496 | /// AAQueryInfo that uses SimpleCaptureInfo. |
497 | class SimpleAAQueryInfo : public AAQueryInfo { |
498 | SimpleCaptureInfo CI; |
499 | |
500 | public: |
501 | SimpleAAQueryInfo() : AAQueryInfo(&CI) {} |
502 | }; |
503 | |
504 | class BatchAAResults; |
505 | |
506 | class AAResults { |
507 | public: |
508 | // Make these results default constructable and movable. We have to spell |
509 | // these out because MSVC won't synthesize them. |
510 | AAResults(const TargetLibraryInfo &TLI) : TLI(TLI) {} |
511 | AAResults(AAResults &&Arg); |
512 | ~AAResults(); |
513 | |
514 | /// Register a specific AA result. |
515 | template <typename AAResultT> void addAAResult(AAResultT &AAResult) { |
516 | // FIXME: We should use a much lighter weight system than the usual |
517 | // polymorphic pattern because we don't own AAResult. It should |
518 | // ideally involve two pointers and no separate allocation. |
519 | AAs.emplace_back(new Model<AAResultT>(AAResult, *this)); |
520 | } |
521 | |
522 | /// Register a function analysis ID that the results aggregation depends on. |
523 | /// |
524 | /// This is used in the new pass manager to implement the invalidation logic |
525 | /// where we must invalidate the results aggregation if any of our component |
526 | /// analyses become invalid. |
527 | void addAADependencyID(AnalysisKey *ID) { AADeps.push_back(ID); } |
528 | |
529 | /// Handle invalidation events in the new pass manager. |
530 | /// |
531 | /// The aggregation is invalidated if any of the underlying analyses is |
532 | /// invalidated. |
533 | bool invalidate(Function &F, const PreservedAnalyses &PA, |
534 | FunctionAnalysisManager::Invalidator &Inv); |
535 | |
536 | //===--------------------------------------------------------------------===// |
537 | /// \name Alias Queries |
538 | /// @{ |
539 | |
540 | /// The main low level interface to the alias analysis implementation. |
541 | /// Returns an AliasResult indicating whether the two pointers are aliased to |
542 | /// each other. This is the interface that must be implemented by specific |
543 | /// alias analysis implementations. |
544 | AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB); |
545 | |
546 | /// A convenience wrapper around the primary \c alias interface. |
547 | AliasResult alias(const Value *V1, LocationSize V1Size, const Value *V2, |
548 | LocationSize V2Size) { |
549 | return alias(MemoryLocation(V1, V1Size), MemoryLocation(V2, V2Size)); |
550 | } |
551 | |
552 | /// A convenience wrapper around the primary \c alias interface. |
553 | AliasResult alias(const Value *V1, const Value *V2) { |
554 | return alias(MemoryLocation::getBeforeOrAfter(V1), |
555 | MemoryLocation::getBeforeOrAfter(V2)); |
556 | } |
557 | |
558 | /// A trivial helper function to check to see if the specified pointers are |
559 | /// no-alias. |
560 | bool isNoAlias(const MemoryLocation &LocA, const MemoryLocation &LocB) { |
561 | return alias(LocA, LocB) == AliasResult::NoAlias; |
562 | } |
563 | |
564 | /// A convenience wrapper around the \c isNoAlias helper interface. |
565 | bool isNoAlias(const Value *V1, LocationSize V1Size, const Value *V2, |
566 | LocationSize V2Size) { |
567 | return isNoAlias(MemoryLocation(V1, V1Size), MemoryLocation(V2, V2Size)); |
568 | } |
569 | |
570 | /// A convenience wrapper around the \c isNoAlias helper interface. |
571 | bool isNoAlias(const Value *V1, const Value *V2) { |
572 | return isNoAlias(MemoryLocation::getBeforeOrAfter(V1), |
573 | MemoryLocation::getBeforeOrAfter(V2)); |
574 | } |
575 | |
576 | /// A trivial helper function to check to see if the specified pointers are |
577 | /// must-alias. |
578 | bool isMustAlias(const MemoryLocation &LocA, const MemoryLocation &LocB) { |
579 | return alias(LocA, LocB) == AliasResult::MustAlias; |
580 | } |
581 | |
582 | /// A convenience wrapper around the \c isMustAlias helper interface. |
583 | bool isMustAlias(const Value *V1, const Value *V2) { |
584 | return alias(V1, LocationSize::precise(1), V2, LocationSize::precise(1)) == |
585 | AliasResult::MustAlias; |
586 | } |
587 | |
588 | /// Checks whether the given location points to constant memory, or if |
589 | /// \p OrLocal is true whether it points to a local alloca. |
590 | bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal = false); |
591 | |
592 | /// A convenience wrapper around the primary \c pointsToConstantMemory |
593 | /// interface. |
594 | bool pointsToConstantMemory(const Value *P, bool OrLocal = false) { |
595 | return pointsToConstantMemory(MemoryLocation::getBeforeOrAfter(P), OrLocal); |
596 | } |
597 | |
598 | /// @} |
599 | //===--------------------------------------------------------------------===// |
600 | /// \name Simple mod/ref information |
601 | /// @{ |
602 | |
603 | /// Get the ModRef info associated with a pointer argument of a call. The |
604 | /// result's bits are set to indicate the allowed aliasing ModRef kinds. Note |
605 | /// that these bits do not necessarily account for the overall behavior of |
606 | /// the function, but rather only provide additional per-argument |
607 | /// information. This never sets ModRefInfo::Must. |
608 | ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx); |
609 | |
610 | /// Return the behavior of the given call site. |
611 | FunctionModRefBehavior getModRefBehavior(const CallBase *Call); |
612 | |
613 | /// Return the behavior when calling the given function. |
614 | FunctionModRefBehavior getModRefBehavior(const Function *F); |
615 | |
616 | /// Checks if the specified call is known to never read or write memory. |
617 | /// |
618 | /// Note that if the call only reads from known-constant memory, it is also |
619 | /// legal to return true. Also, calls that unwind the stack are legal for |
620 | /// this predicate. |
621 | /// |
622 | /// Many optimizations (such as CSE and LICM) can be performed on such calls |
623 | /// without worrying about aliasing properties, and many calls have this |
624 | /// property (e.g. calls to 'sin' and 'cos'). |
625 | /// |
626 | /// This property corresponds to the GCC 'const' attribute. |
627 | bool doesNotAccessMemory(const CallBase *Call) { |
628 | return getModRefBehavior(Call) == FMRB_DoesNotAccessMemory; |
629 | } |
630 | |
631 | /// Checks if the specified function is known to never read or write memory. |
632 | /// |
633 | /// Note that if the function only reads from known-constant memory, it is |
634 | /// also legal to return true. Also, function that unwind the stack are legal |
635 | /// for this predicate. |
636 | /// |
637 | /// Many optimizations (such as CSE and LICM) can be performed on such calls |
638 | /// to such functions without worrying about aliasing properties, and many |
639 | /// functions have this property (e.g. 'sin' and 'cos'). |
640 | /// |
641 | /// This property corresponds to the GCC 'const' attribute. |
642 | bool doesNotAccessMemory(const Function *F) { |
643 | return getModRefBehavior(F) == FMRB_DoesNotAccessMemory; |
644 | } |
645 | |
646 | /// Checks if the specified call is known to only read from non-volatile |
647 | /// memory (or not access memory at all). |
648 | /// |
649 | /// Calls that unwind the stack are legal for this predicate. |
650 | /// |
651 | /// This property allows many common optimizations to be performed in the |
652 | /// absence of interfering store instructions, such as CSE of strlen calls. |
653 | /// |
654 | /// This property corresponds to the GCC 'pure' attribute. |
655 | bool onlyReadsMemory(const CallBase *Call) { |
656 | return onlyReadsMemory(getModRefBehavior(Call)); |
657 | } |
658 | |
659 | /// Checks if the specified function is known to only read from non-volatile |
660 | /// memory (or not access memory at all). |
661 | /// |
662 | /// Functions that unwind the stack are legal for this predicate. |
663 | /// |
664 | /// This property allows many common optimizations to be performed in the |
665 | /// absence of interfering store instructions, such as CSE of strlen calls. |
666 | /// |
667 | /// This property corresponds to the GCC 'pure' attribute. |
668 | bool onlyReadsMemory(const Function *F) { |
669 | return onlyReadsMemory(getModRefBehavior(F)); |
670 | } |
671 | |
672 | /// Checks if functions with the specified behavior are known to only read |
673 | /// from non-volatile memory (or not access memory at all). |
674 | static bool onlyReadsMemory(FunctionModRefBehavior MRB) { |
675 | return !isModSet(createModRefInfo(MRB)); |
676 | } |
677 | |
678 | /// Checks if functions with the specified behavior are known to only write |
679 | /// memory (or not access memory at all). |
680 | static bool onlyWritesMemory(FunctionModRefBehavior MRB) { |
681 | return !isRefSet(createModRefInfo(MRB)); |
682 | } |
683 | |
684 | /// Checks if functions with the specified behavior are known to read and |
685 | /// write at most from objects pointed to by their pointer-typed arguments |
686 | /// (with arbitrary offsets). |
687 | static bool onlyAccessesArgPointees(FunctionModRefBehavior MRB) { |
688 | return !((unsigned)MRB & FMRL_Anywhere & ~FMRL_ArgumentPointees); |
689 | } |
690 | |
691 | /// Checks if functions with the specified behavior are known to potentially |
692 | /// read or write from objects pointed to be their pointer-typed arguments |
693 | /// (with arbitrary offsets). |
694 | static bool doesAccessArgPointees(FunctionModRefBehavior MRB) { |
695 | return isModOrRefSet(createModRefInfo(MRB)) && |
696 | ((unsigned)MRB & FMRL_ArgumentPointees); |
697 | } |
698 | |
699 | /// Checks if functions with the specified behavior are known to read and |
700 | /// write at most from memory that is inaccessible from LLVM IR. |
701 | static bool onlyAccessesInaccessibleMem(FunctionModRefBehavior MRB) { |
702 | return !((unsigned)MRB & FMRL_Anywhere & ~FMRL_InaccessibleMem); |
703 | } |
704 | |
705 | /// Checks if functions with the specified behavior are known to potentially |
706 | /// read or write from memory that is inaccessible from LLVM IR. |
707 | static bool doesAccessInaccessibleMem(FunctionModRefBehavior MRB) { |
708 | return isModOrRefSet(createModRefInfo(MRB)) && |
709 | ((unsigned)MRB & FMRL_InaccessibleMem); |
710 | } |
711 | |
712 | /// Checks if functions with the specified behavior are known to read and |
713 | /// write at most from memory that is inaccessible from LLVM IR or objects |
714 | /// pointed to by their pointer-typed arguments (with arbitrary offsets). |
715 | static bool onlyAccessesInaccessibleOrArgMem(FunctionModRefBehavior MRB) { |
716 | return !((unsigned)MRB & FMRL_Anywhere & |
717 | ~(FMRL_InaccessibleMem | FMRL_ArgumentPointees)); |
718 | } |
719 | |
720 | /// getModRefInfo (for call sites) - Return information about whether |
721 | /// a particular call site modifies or reads the specified memory location. |
722 | ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc); |
723 | |
724 | /// getModRefInfo (for call sites) - A convenience wrapper. |
725 | ModRefInfo getModRefInfo(const CallBase *Call, const Value *P, |
726 | LocationSize Size) { |
727 | return getModRefInfo(Call, MemoryLocation(P, Size)); |
728 | } |
729 | |
730 | /// getModRefInfo (for loads) - Return information about whether |
731 | /// a particular load modifies or reads the specified memory location. |
732 | ModRefInfo getModRefInfo(const LoadInst *L, const MemoryLocation &Loc); |
733 | |
734 | /// getModRefInfo (for loads) - A convenience wrapper. |
735 | ModRefInfo getModRefInfo(const LoadInst *L, const Value *P, |
736 | LocationSize Size) { |
737 | return getModRefInfo(L, MemoryLocation(P, Size)); |
738 | } |
739 | |
740 | /// getModRefInfo (for stores) - Return information about whether |
741 | /// a particular store modifies or reads the specified memory location. |
742 | ModRefInfo getModRefInfo(const StoreInst *S, const MemoryLocation &Loc); |
743 | |
744 | /// getModRefInfo (for stores) - A convenience wrapper. |
745 | ModRefInfo getModRefInfo(const StoreInst *S, const Value *P, |
746 | LocationSize Size) { |
747 | return getModRefInfo(S, MemoryLocation(P, Size)); |
748 | } |
749 | |
750 | /// getModRefInfo (for fences) - Return information about whether |
751 | /// a particular store modifies or reads the specified memory location. |
752 | ModRefInfo getModRefInfo(const FenceInst *S, const MemoryLocation &Loc); |
753 | |
754 | /// getModRefInfo (for fences) - A convenience wrapper. |
755 | ModRefInfo getModRefInfo(const FenceInst *S, const Value *P, |
756 | LocationSize Size) { |
757 | return getModRefInfo(S, MemoryLocation(P, Size)); |
758 | } |
759 | |
760 | /// getModRefInfo (for cmpxchges) - Return information about whether |
761 | /// a particular cmpxchg modifies or reads the specified memory location. |
762 | ModRefInfo getModRefInfo(const AtomicCmpXchgInst *CX, |
763 | const MemoryLocation &Loc); |
764 | |
765 | /// getModRefInfo (for cmpxchges) - A convenience wrapper. |
766 | ModRefInfo getModRefInfo(const AtomicCmpXchgInst *CX, const Value *P, |
767 | LocationSize Size) { |
768 | return getModRefInfo(CX, MemoryLocation(P, Size)); |
769 | } |
770 | |
771 | /// getModRefInfo (for atomicrmws) - Return information about whether |
772 | /// a particular atomicrmw modifies or reads the specified memory location. |
773 | ModRefInfo getModRefInfo(const AtomicRMWInst *RMW, const MemoryLocation &Loc); |
774 | |
775 | /// getModRefInfo (for atomicrmws) - A convenience wrapper. |
776 | ModRefInfo getModRefInfo(const AtomicRMWInst *RMW, const Value *P, |
777 | LocationSize Size) { |
778 | return getModRefInfo(RMW, MemoryLocation(P, Size)); |
779 | } |
780 | |
781 | /// getModRefInfo (for va_args) - Return information about whether |
782 | /// a particular va_arg modifies or reads the specified memory location. |
783 | ModRefInfo getModRefInfo(const VAArgInst *I, const MemoryLocation &Loc); |
784 | |
785 | /// getModRefInfo (for va_args) - A convenience wrapper. |
786 | ModRefInfo getModRefInfo(const VAArgInst *I, const Value *P, |
787 | LocationSize Size) { |
788 | return getModRefInfo(I, MemoryLocation(P, Size)); |
789 | } |
790 | |
791 | /// getModRefInfo (for catchpads) - Return information about whether |
792 | /// a particular catchpad modifies or reads the specified memory location. |
793 | ModRefInfo getModRefInfo(const CatchPadInst *I, const MemoryLocation &Loc); |
794 | |
795 | /// getModRefInfo (for catchpads) - A convenience wrapper. |
796 | ModRefInfo getModRefInfo(const CatchPadInst *I, const Value *P, |
797 | LocationSize Size) { |
798 | return getModRefInfo(I, MemoryLocation(P, Size)); |
799 | } |
800 | |
801 | /// getModRefInfo (for catchrets) - Return information about whether |
802 | /// a particular catchret modifies or reads the specified memory location. |
803 | ModRefInfo getModRefInfo(const CatchReturnInst *I, const MemoryLocation &Loc); |
804 | |
805 | /// getModRefInfo (for catchrets) - A convenience wrapper. |
806 | ModRefInfo getModRefInfo(const CatchReturnInst *I, const Value *P, |
807 | LocationSize Size) { |
808 | return getModRefInfo(I, MemoryLocation(P, Size)); |
809 | } |
810 | |
811 | /// Check whether or not an instruction may read or write the optionally |
812 | /// specified memory location. |
813 | /// |
814 | /// |
815 | /// An instruction that doesn't read or write memory may be trivially LICM'd |
816 | /// for example. |
817 | /// |
818 | /// For function calls, this delegates to the alias-analysis specific |
819 | /// call-site mod-ref behavior queries. Otherwise it delegates to the specific |
820 | /// helpers above. |
821 | ModRefInfo getModRefInfo(const Instruction *I, |
822 | const Optional<MemoryLocation> &OptLoc) { |
823 | SimpleAAQueryInfo AAQIP; |
824 | return getModRefInfo(I, OptLoc, AAQIP); |
825 | } |
826 | |
827 | /// A convenience wrapper for constructing the memory location. |
828 | ModRefInfo getModRefInfo(const Instruction *I, const Value *P, |
829 | LocationSize Size) { |
830 | return getModRefInfo(I, MemoryLocation(P, Size)); |
831 | } |
832 | |
833 | /// Return information about whether a call and an instruction may refer to |
834 | /// the same memory locations. |
835 | ModRefInfo getModRefInfo(Instruction *I, const CallBase *Call); |
836 | |
837 | /// Return information about whether two call sites may refer to the same set |
838 | /// of memory locations. See the AA documentation for details: |
839 | /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo |
840 | ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2); |
841 | |
842 | /// Return information about whether a particular call site modifies |
843 | /// or reads the specified memory location \p MemLoc before instruction \p I |
844 | /// in a BasicBlock. |
845 | /// Early exits in callCapturesBefore may lead to ModRefInfo::Must not being |
846 | /// set. |
847 | ModRefInfo callCapturesBefore(const Instruction *I, |
848 | const MemoryLocation &MemLoc, |
849 | DominatorTree *DT) { |
850 | SimpleAAQueryInfo AAQIP; |
851 | return callCapturesBefore(I, MemLoc, DT, AAQIP); |
852 | } |
853 | |
854 | /// A convenience wrapper to synthesize a memory location. |
855 | ModRefInfo callCapturesBefore(const Instruction *I, const Value *P, |
856 | LocationSize Size, DominatorTree *DT) { |
857 | return callCapturesBefore(I, MemoryLocation(P, Size), DT); |
858 | } |
859 | |
860 | /// @} |
861 | //===--------------------------------------------------------------------===// |
862 | /// \name Higher level methods for querying mod/ref information. |
863 | /// @{ |
864 | |
865 | /// Check if it is possible for execution of the specified basic block to |
866 | /// modify the location Loc. |
867 | bool canBasicBlockModify(const BasicBlock &BB, const MemoryLocation &Loc); |
868 | |
869 | /// A convenience wrapper synthesizing a memory location. |
870 | bool canBasicBlockModify(const BasicBlock &BB, const Value *P, |
871 | LocationSize Size) { |
872 | return canBasicBlockModify(BB, MemoryLocation(P, Size)); |
873 | } |
874 | |
875 | /// Check if it is possible for the execution of the specified instructions |
876 | /// to mod\ref (according to the mode) the location Loc. |
877 | /// |
878 | /// The instructions to consider are all of the instructions in the range of |
879 | /// [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block. |
880 | bool canInstructionRangeModRef(const Instruction &I1, const Instruction &I2, |
881 | const MemoryLocation &Loc, |
882 | const ModRefInfo Mode); |
883 | |
884 | /// A convenience wrapper synthesizing a memory location. |
885 | bool canInstructionRangeModRef(const Instruction &I1, const Instruction &I2, |
886 | const Value *Ptr, LocationSize Size, |
887 | const ModRefInfo Mode) { |
888 | return canInstructionRangeModRef(I1, I2, MemoryLocation(Ptr, Size), Mode); |
889 | } |
890 | |
891 | private: |
892 | AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, |
893 | AAQueryInfo &AAQI); |
894 | bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI, |
895 | bool OrLocal = false); |
896 | ModRefInfo getModRefInfo(Instruction *I, const CallBase *Call2, |
897 | AAQueryInfo &AAQIP); |
898 | ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, |
899 | AAQueryInfo &AAQI); |
900 | ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2, |
901 | AAQueryInfo &AAQI); |
902 | ModRefInfo getModRefInfo(const VAArgInst *V, const MemoryLocation &Loc, |
903 | AAQueryInfo &AAQI); |
904 | ModRefInfo getModRefInfo(const LoadInst *L, const MemoryLocation &Loc, |
905 | AAQueryInfo &AAQI); |
906 | ModRefInfo getModRefInfo(const StoreInst *S, const MemoryLocation &Loc, |
907 | AAQueryInfo &AAQI); |
908 | ModRefInfo getModRefInfo(const FenceInst *S, const MemoryLocation &Loc, |
909 | AAQueryInfo &AAQI); |
910 | ModRefInfo getModRefInfo(const AtomicCmpXchgInst *CX, |
911 | const MemoryLocation &Loc, AAQueryInfo &AAQI); |
912 | ModRefInfo getModRefInfo(const AtomicRMWInst *RMW, const MemoryLocation &Loc, |
913 | AAQueryInfo &AAQI); |
914 | ModRefInfo getModRefInfo(const CatchPadInst *I, const MemoryLocation &Loc, |
915 | AAQueryInfo &AAQI); |
916 | ModRefInfo getModRefInfo(const CatchReturnInst *I, const MemoryLocation &Loc, |
917 | AAQueryInfo &AAQI); |
918 | ModRefInfo getModRefInfo(const Instruction *I, |
919 | const Optional<MemoryLocation> &OptLoc, |
920 | AAQueryInfo &AAQIP); |
921 | ModRefInfo callCapturesBefore(const Instruction *I, |
922 | const MemoryLocation &MemLoc, DominatorTree *DT, |
923 | AAQueryInfo &AAQIP); |
924 | |
925 | class Concept; |
926 | |
927 | template <typename T> class Model; |
928 | |
929 | template <typename T> friend class AAResultBase; |
930 | |
931 | const TargetLibraryInfo &TLI; |
932 | |
933 | std::vector<std::unique_ptr<Concept>> AAs; |
934 | |
935 | std::vector<AnalysisKey *> AADeps; |
936 | |
937 | friend class BatchAAResults; |
938 | }; |
939 | |
940 | /// This class is a wrapper over an AAResults, and it is intended to be used |
941 | /// only when there are no IR changes inbetween queries. BatchAAResults is |
942 | /// reusing the same `AAQueryInfo` to preserve the state across queries, |
943 | /// esentially making AA work in "batch mode". The internal state cannot be |
944 | /// cleared, so to go "out-of-batch-mode", the user must either use AAResults, |
945 | /// or create a new BatchAAResults. |
946 | class BatchAAResults { |
947 | AAResults &AA; |
948 | AAQueryInfo AAQI; |
949 | SimpleCaptureInfo SimpleCI; |
950 | |
951 | public: |
952 | BatchAAResults(AAResults &AAR) : AA(AAR), AAQI(&SimpleCI) {} |
953 | BatchAAResults(AAResults &AAR, CaptureInfo *CI) : AA(AAR), AAQI(CI) {} |
954 | |
955 | AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB) { |
956 | return AA.alias(LocA, LocB, AAQI); |
957 | } |
958 | bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal = false) { |
959 | return AA.pointsToConstantMemory(Loc, AAQI, OrLocal); |
960 | } |
961 | ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc) { |
962 | return AA.getModRefInfo(Call, Loc, AAQI); |
963 | } |
964 | ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2) { |
965 | return AA.getModRefInfo(Call1, Call2, AAQI); |
966 | } |
967 | ModRefInfo getModRefInfo(const Instruction *I, |
968 | const Optional<MemoryLocation> &OptLoc) { |
969 | return AA.getModRefInfo(I, OptLoc, AAQI); |
970 | } |
971 | ModRefInfo getModRefInfo(Instruction *I, const CallBase *Call2) { |
972 | return AA.getModRefInfo(I, Call2, AAQI); |
973 | } |
974 | ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) { |
975 | return AA.getArgModRefInfo(Call, ArgIdx); |
976 | } |
977 | FunctionModRefBehavior getModRefBehavior(const CallBase *Call) { |
978 | return AA.getModRefBehavior(Call); |
979 | } |
980 | bool isMustAlias(const MemoryLocation &LocA, const MemoryLocation &LocB) { |
981 | return alias(LocA, LocB) == AliasResult::MustAlias; |
982 | } |
983 | bool isMustAlias(const Value *V1, const Value *V2) { |
984 | return alias(MemoryLocation(V1, LocationSize::precise(1)), |
985 | MemoryLocation(V2, LocationSize::precise(1))) == |
986 | AliasResult::MustAlias; |
987 | } |
988 | ModRefInfo callCapturesBefore(const Instruction *I, |
989 | const MemoryLocation &MemLoc, |
990 | DominatorTree *DT) { |
991 | return AA.callCapturesBefore(I, MemLoc, DT, AAQI); |
992 | } |
993 | }; |
994 | |
995 | /// Temporary typedef for legacy code that uses a generic \c AliasAnalysis |
996 | /// pointer or reference. |
997 | using AliasAnalysis = AAResults; |
998 | |
999 | /// A private abstract base class describing the concept of an individual alias |
1000 | /// analysis implementation. |
1001 | /// |
1002 | /// This interface is implemented by any \c Model instantiation. It is also the |
1003 | /// interface which a type used to instantiate the model must provide. |
1004 | /// |
1005 | /// All of these methods model methods by the same name in the \c |
1006 | /// AAResults class. Only differences and specifics to how the |
1007 | /// implementations are called are documented here. |
1008 | class AAResults::Concept { |
1009 | public: |
1010 | virtual ~Concept() = 0; |
1011 | |
1012 | /// An update API used internally by the AAResults to provide |
1013 | /// a handle back to the top level aggregation. |
1014 | virtual void setAAResults(AAResults *NewAAR) = 0; |
1015 | |
1016 | //===--------------------------------------------------------------------===// |
1017 | /// \name Alias Queries |
1018 | /// @{ |
1019 | |
1020 | /// The main low level interface to the alias analysis implementation. |
1021 | /// Returns an AliasResult indicating whether the two pointers are aliased to |
1022 | /// each other. This is the interface that must be implemented by specific |
1023 | /// alias analysis implementations. |
1024 | virtual AliasResult alias(const MemoryLocation &LocA, |
1025 | const MemoryLocation &LocB, AAQueryInfo &AAQI) = 0; |
1026 | |
1027 | /// Checks whether the given location points to constant memory, or if |
1028 | /// \p OrLocal is true whether it points to a local alloca. |
1029 | virtual bool pointsToConstantMemory(const MemoryLocation &Loc, |
1030 | AAQueryInfo &AAQI, bool OrLocal) = 0; |
1031 | |
1032 | /// @} |
1033 | //===--------------------------------------------------------------------===// |
1034 | /// \name Simple mod/ref information |
1035 | /// @{ |
1036 | |
1037 | /// Get the ModRef info associated with a pointer argument of a callsite. The |
1038 | /// result's bits are set to indicate the allowed aliasing ModRef kinds. Note |
1039 | /// that these bits do not necessarily account for the overall behavior of |
1040 | /// the function, but rather only provide additional per-argument |
1041 | /// information. |
1042 | virtual ModRefInfo getArgModRefInfo(const CallBase *Call, |
1043 | unsigned ArgIdx) = 0; |
1044 | |
1045 | /// Return the behavior of the given call site. |
1046 | virtual FunctionModRefBehavior getModRefBehavior(const CallBase *Call) = 0; |
1047 | |
1048 | /// Return the behavior when calling the given function. |
1049 | virtual FunctionModRefBehavior getModRefBehavior(const Function *F) = 0; |
1050 | |
1051 | /// getModRefInfo (for call sites) - Return information about whether |
1052 | /// a particular call site modifies or reads the specified memory location. |
1053 | virtual ModRefInfo getModRefInfo(const CallBase *Call, |
1054 | const MemoryLocation &Loc, |
1055 | AAQueryInfo &AAQI) = 0; |
1056 | |
1057 | /// Return information about whether two call sites may refer to the same set |
1058 | /// of memory locations. See the AA documentation for details: |
1059 | /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo |
1060 | virtual ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2, |
1061 | AAQueryInfo &AAQI) = 0; |
1062 | |
1063 | /// @} |
1064 | }; |
1065 | |
1066 | /// A private class template which derives from \c Concept and wraps some other |
1067 | /// type. |
1068 | /// |
1069 | /// This models the concept by directly forwarding each interface point to the |
1070 | /// wrapped type which must implement a compatible interface. This provides |
1071 | /// a type erased binding. |
1072 | template <typename AAResultT> class AAResults::Model final : public Concept { |
1073 | AAResultT &Result; |
1074 | |
1075 | public: |
1076 | explicit Model(AAResultT &Result, AAResults &AAR) : Result(Result) { |
1077 | Result.setAAResults(&AAR); |
1078 | } |
1079 | ~Model() override = default; |
1080 | |
1081 | void setAAResults(AAResults *NewAAR) override { Result.setAAResults(NewAAR); } |
1082 | |
1083 | AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, |
1084 | AAQueryInfo &AAQI) override { |
1085 | return Result.alias(LocA, LocB, AAQI); |
1086 | } |
1087 | |
1088 | bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI, |
1089 | bool OrLocal) override { |
1090 | return Result.pointsToConstantMemory(Loc, AAQI, OrLocal); |
1091 | } |
1092 | |
1093 | ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) override { |
1094 | return Result.getArgModRefInfo(Call, ArgIdx); |
1095 | } |
1096 | |
1097 | FunctionModRefBehavior getModRefBehavior(const CallBase *Call) override { |
1098 | return Result.getModRefBehavior(Call); |
1099 | } |
1100 | |
1101 | FunctionModRefBehavior getModRefBehavior(const Function *F) override { |
1102 | return Result.getModRefBehavior(F); |
1103 | } |
1104 | |
1105 | ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, |
1106 | AAQueryInfo &AAQI) override { |
1107 | return Result.getModRefInfo(Call, Loc, AAQI); |
1108 | } |
1109 | |
1110 | ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2, |
1111 | AAQueryInfo &AAQI) override { |
1112 | return Result.getModRefInfo(Call1, Call2, AAQI); |
1113 | } |
1114 | }; |
1115 | |
1116 | /// A CRTP-driven "mixin" base class to help implement the function alias |
1117 | /// analysis results concept. |
1118 | /// |
1119 | /// Because of the nature of many alias analysis implementations, they often |
1120 | /// only implement a subset of the interface. This base class will attempt to |
1121 | /// implement the remaining portions of the interface in terms of simpler forms |
1122 | /// of the interface where possible, and otherwise provide conservatively |
1123 | /// correct fallback implementations. |
1124 | /// |
1125 | /// Implementors of an alias analysis should derive from this CRTP, and then |
1126 | /// override specific methods that they wish to customize. There is no need to |
1127 | /// use virtual anywhere, the CRTP base class does static dispatch to the |
1128 | /// derived type passed into it. |
1129 | template <typename DerivedT> class AAResultBase { |
1130 | // Expose some parts of the interface only to the AAResults::Model |
1131 | // for wrapping. Specifically, this allows the model to call our |
1132 | // setAAResults method without exposing it as a fully public API. |
1133 | friend class AAResults::Model<DerivedT>; |
1134 | |
1135 | /// A pointer to the AAResults object that this AAResult is |
1136 | /// aggregated within. May be null if not aggregated. |
1137 | AAResults *AAR = nullptr; |
1138 | |
1139 | /// Helper to dispatch calls back through the derived type. |
1140 | DerivedT &derived() { return static_cast<DerivedT &>(*this); } |
1141 | |
1142 | /// A setter for the AAResults pointer, which is used to satisfy the |
1143 | /// AAResults::Model contract. |
1144 | void setAAResults(AAResults *NewAAR) { AAR = NewAAR; } |
1145 | |
1146 | protected: |
1147 | /// This proxy class models a common pattern where we delegate to either the |
1148 | /// top-level \c AAResults aggregation if one is registered, or to the |
1149 | /// current result if none are registered. |
1150 | class AAResultsProxy { |
1151 | AAResults *AAR; |
1152 | DerivedT &CurrentResult; |
1153 | |
1154 | public: |
1155 | AAResultsProxy(AAResults *AAR, DerivedT &CurrentResult) |
1156 | : AAR(AAR), CurrentResult(CurrentResult) {} |
1157 | |
1158 | AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, |
1159 | AAQueryInfo &AAQI) { |
1160 | return AAR ? AAR->alias(LocA, LocB, AAQI) |
1161 | : CurrentResult.alias(LocA, LocB, AAQI); |
1162 | } |
1163 | |
1164 | bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI, |
1165 | bool OrLocal) { |
1166 | return AAR ? AAR->pointsToConstantMemory(Loc, AAQI, OrLocal) |
1167 | : CurrentResult.pointsToConstantMemory(Loc, AAQI, OrLocal); |
1168 | } |
1169 | |
1170 | ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) { |
1171 | return AAR ? AAR->getArgModRefInfo(Call, ArgIdx) |
1172 | : CurrentResult.getArgModRefInfo(Call, ArgIdx); |
1173 | } |
1174 | |
1175 | FunctionModRefBehavior getModRefBehavior(const CallBase *Call) { |
1176 | return AAR ? AAR->getModRefBehavior(Call) |
1177 | : CurrentResult.getModRefBehavior(Call); |
1178 | } |
1179 | |
1180 | FunctionModRefBehavior getModRefBehavior(const Function *F) { |
1181 | return AAR ? AAR->getModRefBehavior(F) : CurrentResult.getModRefBehavior(F); |
1182 | } |
1183 | |
1184 | ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, |
1185 | AAQueryInfo &AAQI) { |
1186 | return AAR ? AAR->getModRefInfo(Call, Loc, AAQI) |
1187 | : CurrentResult.getModRefInfo(Call, Loc, AAQI); |
1188 | } |
1189 | |
1190 | ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2, |
1191 | AAQueryInfo &AAQI) { |
1192 | return AAR ? AAR->getModRefInfo(Call1, Call2, AAQI) |
1193 | : CurrentResult.getModRefInfo(Call1, Call2, AAQI); |
1194 | } |
1195 | }; |
1196 | |
1197 | explicit AAResultBase() = default; |
1198 | |
1199 | // Provide all the copy and move constructors so that derived types aren't |
1200 | // constrained. |
1201 | AAResultBase(const AAResultBase &Arg) {} |
1202 | AAResultBase(AAResultBase &&Arg) {} |
1203 | |
1204 | /// Get a proxy for the best AA result set to query at this time. |
1205 | /// |
1206 | /// When this result is part of a larger aggregation, this will proxy to that |
1207 | /// aggregation. When this result is used in isolation, it will just delegate |
1208 | /// back to the derived class's implementation. |
1209 | /// |
1210 | /// Note that callers of this need to take considerable care to not cause |
1211 | /// performance problems when they use this routine, in the case of a large |
1212 | /// number of alias analyses being aggregated, it can be expensive to walk |
1213 | /// back across the chain. |
1214 | AAResultsProxy getBestAAResults() { return AAResultsProxy(AAR, derived()); } |
1215 | |
1216 | public: |
1217 | AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, |
1218 | AAQueryInfo &AAQI) { |
1219 | return AliasResult::MayAlias; |
1220 | } |
1221 | |
1222 | bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI, |
1223 | bool OrLocal) { |
1224 | return false; |
1225 | } |
1226 | |
1227 | ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) { |
1228 | return ModRefInfo::ModRef; |
1229 | } |
1230 | |
1231 | FunctionModRefBehavior getModRefBehavior(const CallBase *Call) { |
1232 | return FMRB_UnknownModRefBehavior; |
1233 | } |
1234 | |
1235 | FunctionModRefBehavior getModRefBehavior(const Function *F) { |
1236 | return FMRB_UnknownModRefBehavior; |
1237 | } |
1238 | |
1239 | ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, |
1240 | AAQueryInfo &AAQI) { |
1241 | return ModRefInfo::ModRef; |
1242 | } |
1243 | |
1244 | ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2, |
1245 | AAQueryInfo &AAQI) { |
1246 | return ModRefInfo::ModRef; |
1247 | } |
1248 | }; |
1249 | |
1250 | /// Return true if this pointer is returned by a noalias function. |
1251 | bool isNoAliasCall(const Value *V); |
1252 | |
1253 | /// Return true if this pointer refers to a distinct and identifiable object. |
1254 | /// This returns true for: |
1255 | /// Global Variables and Functions (but not Global Aliases) |
1256 | /// Allocas |
1257 | /// ByVal and NoAlias Arguments |
1258 | /// NoAlias returns (e.g. calls to malloc) |
1259 | /// |
1260 | bool isIdentifiedObject(const Value *V); |
1261 | |
1262 | /// Return true if V is umabigously identified at the function-level. |
1263 | /// Different IdentifiedFunctionLocals can't alias. |
1264 | /// Further, an IdentifiedFunctionLocal can not alias with any function |
1265 | /// arguments other than itself, which is not necessarily true for |
1266 | /// IdentifiedObjects. |
1267 | bool isIdentifiedFunctionLocal(const Value *V); |
1268 | |
1269 | /// Return true if Object memory is not visible after an unwind, in the sense |
1270 | /// that program semantics cannot depend on Object containing any particular |
1271 | /// value on unwind. If the RequiresNoCaptureBeforeUnwind out parameter is set |
1272 | /// to true, then the memory is only not visible if the object has not been |
1273 | /// captured prior to the unwind. Otherwise it is not visible even if captured. |
1274 | bool isNotVisibleOnUnwind(const Value *Object, |
1275 | bool &RequiresNoCaptureBeforeUnwind); |
1276 | |
1277 | /// A manager for alias analyses. |
1278 | /// |
1279 | /// This class can have analyses registered with it and when run, it will run |
1280 | /// all of them and aggregate their results into single AA results interface |
1281 | /// that dispatches across all of the alias analysis results available. |
1282 | /// |
1283 | /// Note that the order in which analyses are registered is very significant. |
1284 | /// That is the order in which the results will be aggregated and queried. |
1285 | /// |
1286 | /// This manager effectively wraps the AnalysisManager for registering alias |
1287 | /// analyses. When you register your alias analysis with this manager, it will |
1288 | /// ensure the analysis itself is registered with its AnalysisManager. |
1289 | /// |
1290 | /// The result of this analysis is only invalidated if one of the particular |
1291 | /// aggregated AA results end up being invalidated. This removes the need to |
1292 | /// explicitly preserve the results of `AAManager`. Note that analyses should no |
1293 | /// longer be registered once the `AAManager` is run. |
1294 | class AAManager : public AnalysisInfoMixin<AAManager> { |
1295 | public: |
1296 | using Result = AAResults; |
1297 | |
1298 | /// Register a specific AA result. |
1299 | template <typename AnalysisT> void registerFunctionAnalysis() { |
1300 | ResultGetters.push_back(&getFunctionAAResultImpl<AnalysisT>); |
1301 | } |
1302 | |
1303 | /// Register a specific AA result. |
1304 | template <typename AnalysisT> void registerModuleAnalysis() { |
1305 | ResultGetters.push_back(&getModuleAAResultImpl<AnalysisT>); |
1306 | } |
1307 | |
1308 | Result run(Function &F, FunctionAnalysisManager &AM); |
1309 | |
1310 | private: |
1311 | friend AnalysisInfoMixin<AAManager>; |
1312 | |
1313 | static AnalysisKey Key; |
1314 | |
1315 | SmallVector<void (*)(Function &F, FunctionAnalysisManager &AM, |
1316 | AAResults &AAResults), |
1317 | 4> ResultGetters; |
1318 | |
1319 | template <typename AnalysisT> |
1320 | static void getFunctionAAResultImpl(Function &F, |
1321 | FunctionAnalysisManager &AM, |
1322 | AAResults &AAResults) { |
1323 | AAResults.addAAResult(AM.template getResult<AnalysisT>(F)); |
1324 | AAResults.addAADependencyID(AnalysisT::ID()); |
1325 | } |
1326 | |
1327 | template <typename AnalysisT> |
1328 | static void getModuleAAResultImpl(Function &F, FunctionAnalysisManager &AM, |
1329 | AAResults &AAResults) { |
1330 | auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F); |
1331 | if (auto *R = |
1332 | MAMProxy.template getCachedResult<AnalysisT>(*F.getParent())) { |
1333 | AAResults.addAAResult(*R); |
1334 | MAMProxy |
1335 | .template registerOuterAnalysisInvalidation<AnalysisT, AAManager>(); |
1336 | } |
1337 | } |
1338 | }; |
1339 | |
1340 | /// A wrapper pass to provide the legacy pass manager access to a suitably |
1341 | /// prepared AAResults object. |
1342 | class AAResultsWrapperPass : public FunctionPass { |
1343 | std::unique_ptr<AAResults> AAR; |
1344 | |
1345 | public: |
1346 | static char ID; |
1347 | |
1348 | AAResultsWrapperPass(); |
1349 | |
1350 | AAResults &getAAResults() { return *AAR; } |
1351 | const AAResults &getAAResults() const { return *AAR; } |
1352 | |
1353 | bool runOnFunction(Function &F) override; |
1354 | |
1355 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
1356 | }; |
1357 | |
1358 | /// A wrapper pass for external alias analyses. This just squirrels away the |
1359 | /// callback used to run any analyses and register their results. |
1360 | struct ExternalAAWrapperPass : ImmutablePass { |
1361 | using CallbackT = std::function<void(Pass &, Function &, AAResults &)>; |
1362 | |
1363 | CallbackT CB; |
1364 | |
1365 | static char ID; |
1366 | |
1367 | ExternalAAWrapperPass(); |
1368 | |
1369 | explicit ExternalAAWrapperPass(CallbackT CB); |
1370 | |
1371 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
1372 | AU.setPreservesAll(); |
1373 | } |
1374 | }; |
1375 | |
1376 | FunctionPass *createAAResultsWrapperPass(); |
1377 | |
1378 | /// A wrapper pass around a callback which can be used to populate the |
1379 | /// AAResults in the AAResultsWrapperPass from an external AA. |
1380 | /// |
1381 | /// The callback provided here will be used each time we prepare an AAResults |
1382 | /// object, and will receive a reference to the function wrapper pass, the |
1383 | /// function, and the AAResults object to populate. This should be used when |
1384 | /// setting up a custom pass pipeline to inject a hook into the AA results. |
1385 | ImmutablePass *createExternalAAWrapperPass( |
1386 | std::function<void(Pass &, Function &, AAResults &)> Callback); |
1387 | |
1388 | /// A helper for the legacy pass manager to create a \c AAResults |
1389 | /// object populated to the best of our ability for a particular function when |
1390 | /// inside of a \c ModulePass or a \c CallGraphSCCPass. |
1391 | /// |
1392 | /// If a \c ModulePass or a \c CallGraphSCCPass calls \p |
1393 | /// createLegacyPMAAResults, it also needs to call \p addUsedAAAnalyses in \p |
1394 | /// getAnalysisUsage. |
1395 | AAResults createLegacyPMAAResults(Pass &P, Function &F, BasicAAResult &BAR); |
1396 | |
1397 | /// A helper for the legacy pass manager to populate \p AU to add uses to make |
1398 | /// sure the analyses required by \p createLegacyPMAAResults are available. |
1399 | void getAAResultsAnalysisUsage(AnalysisUsage &AU); |
1400 | |
1401 | } // end namespace llvm |
1402 | |
1403 | #endif // LLVM_ANALYSIS_ALIASANALYSIS_H |
1 | //===- llvm/Type.h - Classes for handling data types ------------*- 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 contains the declaration of the Type class. For more "Type" |
10 | // stuff, look in DerivedTypes.h. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #ifndef LLVM_IR_TYPE_H |
15 | #define LLVM_IR_TYPE_H |
16 | |
17 | #include "llvm/ADT/ArrayRef.h" |
18 | #include "llvm/Support/CBindingWrapping.h" |
19 | #include "llvm/Support/Casting.h" |
20 | #include "llvm/Support/Compiler.h" |
21 | #include "llvm/Support/ErrorHandling.h" |
22 | #include "llvm/Support/TypeSize.h" |
23 | #include <cassert> |
24 | #include <cstdint> |
25 | #include <iterator> |
26 | |
27 | namespace llvm { |
28 | |
29 | class IntegerType; |
30 | struct fltSemantics; |
31 | class LLVMContext; |
32 | class PointerType; |
33 | class raw_ostream; |
34 | class StringRef; |
35 | template <typename PtrType> class SmallPtrSetImpl; |
36 | |
37 | /// The instances of the Type class are immutable: once they are created, |
38 | /// they are never changed. Also note that only one instance of a particular |
39 | /// type is ever created. Thus seeing if two types are equal is a matter of |
40 | /// doing a trivial pointer comparison. To enforce that no two equal instances |
41 | /// are created, Type instances can only be created via static factory methods |
42 | /// in class Type and in derived classes. Once allocated, Types are never |
43 | /// free'd. |
44 | /// |
45 | class Type { |
46 | public: |
47 | //===--------------------------------------------------------------------===// |
48 | /// Definitions of all of the base types for the Type system. Based on this |
49 | /// value, you can cast to a class defined in DerivedTypes.h. |
50 | /// Note: If you add an element to this, you need to add an element to the |
51 | /// Type::getPrimitiveType function, or else things will break! |
52 | /// Also update LLVMTypeKind and LLVMGetTypeKind () in the C binding. |
53 | /// |
54 | enum TypeID { |
55 | // PrimitiveTypes |
56 | HalfTyID = 0, ///< 16-bit floating point type |
57 | BFloatTyID, ///< 16-bit floating point type (7-bit significand) |
58 | FloatTyID, ///< 32-bit floating point type |
59 | DoubleTyID, ///< 64-bit floating point type |
60 | X86_FP80TyID, ///< 80-bit floating point type (X87) |
61 | FP128TyID, ///< 128-bit floating point type (112-bit significand) |
62 | PPC_FP128TyID, ///< 128-bit floating point type (two 64-bits, PowerPC) |
63 | VoidTyID, ///< type with no size |
64 | LabelTyID, ///< Labels |
65 | MetadataTyID, ///< Metadata |
66 | X86_MMXTyID, ///< MMX vectors (64 bits, X86 specific) |
67 | X86_AMXTyID, ///< AMX vectors (8192 bits, X86 specific) |
68 | TokenTyID, ///< Tokens |
69 | |
70 | // Derived types... see DerivedTypes.h file. |
71 | IntegerTyID, ///< Arbitrary bit width integers |
72 | FunctionTyID, ///< Functions |
73 | PointerTyID, ///< Pointers |
74 | StructTyID, ///< Structures |
75 | ArrayTyID, ///< Arrays |
76 | FixedVectorTyID, ///< Fixed width SIMD vector type |
77 | ScalableVectorTyID ///< Scalable SIMD vector type |
78 | }; |
79 | |
80 | private: |
81 | /// This refers to the LLVMContext in which this type was uniqued. |
82 | LLVMContext &Context; |
83 | |
84 | TypeID ID : 8; // The current base type of this type. |
85 | unsigned SubclassData : 24; // Space for subclasses to store data. |
86 | // Note that this should be synchronized with |
87 | // MAX_INT_BITS value in IntegerType class. |
88 | |
89 | protected: |
90 | friend class LLVMContextImpl; |
91 | |
92 | explicit Type(LLVMContext &C, TypeID tid) |
93 | : Context(C), ID(tid), SubclassData(0) {} |
94 | ~Type() = default; |
95 | |
96 | unsigned getSubclassData() const { return SubclassData; } |
97 | |
98 | void setSubclassData(unsigned val) { |
99 | SubclassData = val; |
100 | // Ensure we don't have any accidental truncation. |
101 | assert(getSubclassData() == val && "Subclass data too large for field")(static_cast <bool> (getSubclassData() == val && "Subclass data too large for field") ? void (0) : __assert_fail ("getSubclassData() == val && \"Subclass data too large for field\"" , "llvm/include/llvm/IR/Type.h", 101, __extension__ __PRETTY_FUNCTION__ )); |
102 | } |
103 | |
104 | /// Keeps track of how many Type*'s there are in the ContainedTys list. |
105 | unsigned NumContainedTys = 0; |
106 | |
107 | /// A pointer to the array of Types contained by this Type. For example, this |
108 | /// includes the arguments of a function type, the elements of a structure, |
109 | /// the pointee of a pointer, the element type of an array, etc. This pointer |
110 | /// may be 0 for types that don't contain other types (Integer, Double, |
111 | /// Float). |
112 | Type * const *ContainedTys = nullptr; |
113 | |
114 | public: |
115 | /// Print the current type. |
116 | /// Omit the type details if \p NoDetails == true. |
117 | /// E.g., let %st = type { i32, i16 } |
118 | /// When \p NoDetails is true, we only print %st. |
119 | /// Put differently, \p NoDetails prints the type as if |
120 | /// inlined with the operands when printing an instruction. |
121 | void print(raw_ostream &O, bool IsForDebug = false, |
122 | bool NoDetails = false) const; |
123 | |
124 | void dump() const; |
125 | |
126 | /// Return the LLVMContext in which this type was uniqued. |
127 | LLVMContext &getContext() const { return Context; } |
128 | |
129 | //===--------------------------------------------------------------------===// |
130 | // Accessors for working with types. |
131 | // |
132 | |
133 | /// Return the type id for the type. This will return one of the TypeID enum |
134 | /// elements defined above. |
135 | TypeID getTypeID() const { return ID; } |
136 | |
137 | /// Return true if this is 'void'. |
138 | bool isVoidTy() const { return getTypeID() == VoidTyID; } |
139 | |
140 | /// Return true if this is 'half', a 16-bit IEEE fp type. |
141 | bool isHalfTy() const { return getTypeID() == HalfTyID; } |
142 | |
143 | /// Return true if this is 'bfloat', a 16-bit bfloat type. |
144 | bool isBFloatTy() const { return getTypeID() == BFloatTyID; } |
145 | |
146 | /// Return true if this is 'float', a 32-bit IEEE fp type. |
147 | bool isFloatTy() const { return getTypeID() == FloatTyID; } |
148 | |
149 | /// Return true if this is 'double', a 64-bit IEEE fp type. |
150 | bool isDoubleTy() const { return getTypeID() == DoubleTyID; } |
151 | |
152 | /// Return true if this is x86 long double. |
153 | bool isX86_FP80Ty() const { return getTypeID() == X86_FP80TyID; } |
154 | |
155 | /// Return true if this is 'fp128'. |
156 | bool isFP128Ty() const { return getTypeID() == FP128TyID; } |
157 | |
158 | /// Return true if this is powerpc long double. |
159 | bool isPPC_FP128Ty() const { return getTypeID() == PPC_FP128TyID; } |
160 | |
161 | /// Return true if this is one of the six floating-point types |
162 | bool isFloatingPointTy() const { |
163 | return getTypeID() == HalfTyID || getTypeID() == BFloatTyID || |
164 | getTypeID() == FloatTyID || getTypeID() == DoubleTyID || |
165 | getTypeID() == X86_FP80TyID || getTypeID() == FP128TyID || |
166 | getTypeID() == PPC_FP128TyID; |
167 | } |
168 | |
169 | const fltSemantics &getFltSemantics() const; |
170 | |
171 | /// Return true if this is X86 MMX. |
172 | bool isX86_MMXTy() const { return getTypeID() == X86_MMXTyID; } |
173 | |
174 | /// Return true if this is X86 AMX. |
175 | bool isX86_AMXTy() const { return getTypeID() == X86_AMXTyID; } |
176 | |
177 | /// Return true if this is a FP type or a vector of FP. |
178 | bool isFPOrFPVectorTy() const { return getScalarType()->isFloatingPointTy(); } |
179 | |
180 | /// Return true if this is 'label'. |
181 | bool isLabelTy() const { return getTypeID() == LabelTyID; } |
182 | |
183 | /// Return true if this is 'metadata'. |
184 | bool isMetadataTy() const { return getTypeID() == MetadataTyID; } |
185 | |
186 | /// Return true if this is 'token'. |
187 | bool isTokenTy() const { return getTypeID() == TokenTyID; } |
188 | |
189 | /// True if this is an instance of IntegerType. |
190 | bool isIntegerTy() const { return getTypeID() == IntegerTyID; } |
191 | |
192 | /// Return true if this is an IntegerType of the given width. |
193 | bool isIntegerTy(unsigned Bitwidth) const; |
194 | |
195 | /// Return true if this is an integer type or a vector of integer types. |
196 | bool isIntOrIntVectorTy() const { return getScalarType()->isIntegerTy(); } |
197 | |
198 | /// Return true if this is an integer type or a vector of integer types of |
199 | /// the given width. |
200 | bool isIntOrIntVectorTy(unsigned BitWidth) const { |
201 | return getScalarType()->isIntegerTy(BitWidth); |
202 | } |
203 | |
204 | /// Return true if this is an integer type or a pointer type. |
205 | bool isIntOrPtrTy() const { return isIntegerTy() || isPointerTy(); } |
206 | |
207 | /// True if this is an instance of FunctionType. |
208 | bool isFunctionTy() const { return getTypeID() == FunctionTyID; } |
209 | |
210 | /// True if this is an instance of StructType. |
211 | bool isStructTy() const { return getTypeID() == StructTyID; } |
212 | |
213 | /// True if this is an instance of ArrayType. |
214 | bool isArrayTy() const { return getTypeID() == ArrayTyID; } |
215 | |
216 | /// True if this is an instance of PointerType. |
217 | bool isPointerTy() const { return getTypeID() == PointerTyID; } |
218 | |
219 | /// True if this is an instance of an opaque PointerType. |
220 | bool isOpaquePointerTy() const; |
221 | |
222 | /// Return true if this is a pointer type or a vector of pointer types. |
223 | bool isPtrOrPtrVectorTy() const { return getScalarType()->isPointerTy(); } |
224 | |
225 | /// True if this is an instance of VectorType. |
226 | inline bool isVectorTy() const { |
227 | return getTypeID() == ScalableVectorTyID || getTypeID() == FixedVectorTyID; |
228 | } |
229 | |
230 | /// Return true if this type could be converted with a lossless BitCast to |
231 | /// type 'Ty'. For example, i8* to i32*. BitCasts are valid for types of the |
232 | /// same size only where no re-interpretation of the bits is done. |
233 | /// Determine if this type could be losslessly bitcast to Ty |
234 | bool canLosslesslyBitCastTo(Type *Ty) const; |
235 | |
236 | /// Return true if this type is empty, that is, it has no elements or all of |
237 | /// its elements are empty. |
238 | bool isEmptyTy() const; |
239 | |
240 | /// Return true if the type is "first class", meaning it is a valid type for a |
241 | /// Value. |
242 | bool isFirstClassType() const { |
243 | return getTypeID() != FunctionTyID && getTypeID() != VoidTyID; |
244 | } |
245 | |
246 | /// Return true if the type is a valid type for a register in codegen. This |
247 | /// includes all first-class types except struct and array types. |
248 | bool isSingleValueType() const { |
249 | return isFloatingPointTy() || isX86_MMXTy() || isIntegerTy() || |
250 | isPointerTy() || isVectorTy() || isX86_AMXTy(); |
251 | } |
252 | |
253 | /// Return true if the type is an aggregate type. This means it is valid as |
254 | /// the first operand of an insertvalue or extractvalue instruction. This |
255 | /// includes struct and array types, but does not include vector types. |
256 | bool isAggregateType() const { |
257 | return getTypeID() == StructTyID || getTypeID() == ArrayTyID; |
258 | } |
259 | |
260 | /// Return true if it makes sense to take the size of this type. To get the |
261 | /// actual size for a particular target, it is reasonable to use the |
262 | /// DataLayout subsystem to do this. |
263 | bool isSized(SmallPtrSetImpl<Type*> *Visited = nullptr) const { |
264 | // If it's a primitive, it is always sized. |
265 | if (getTypeID() == IntegerTyID || isFloatingPointTy() || |
266 | getTypeID() == PointerTyID || getTypeID() == X86_MMXTyID || |
267 | getTypeID() == X86_AMXTyID) |
268 | return true; |
269 | // If it is not something that can have a size (e.g. a function or label), |
270 | // it doesn't have a size. |
271 | if (getTypeID() != StructTyID && getTypeID() != ArrayTyID && !isVectorTy()) |
272 | return false; |
273 | // Otherwise we have to try harder to decide. |
274 | return isSizedDerivedType(Visited); |
275 | } |
276 | |
277 | /// Return the basic size of this type if it is a primitive type. These are |
278 | /// fixed by LLVM and are not target-dependent. |
279 | /// This will return zero if the type does not have a size or is not a |
280 | /// primitive type. |
281 | /// |
282 | /// If this is a scalable vector type, the scalable property will be set and |
283 | /// the runtime size will be a positive integer multiple of the base size. |
284 | /// |
285 | /// Note that this may not reflect the size of memory allocated for an |
286 | /// instance of the type or the number of bytes that are written when an |
287 | /// instance of the type is stored to memory. The DataLayout class provides |
288 | /// additional query functions to provide this information. |
289 | /// |
290 | TypeSize getPrimitiveSizeInBits() const LLVM_READONLY__attribute__((__pure__)); |
291 | |
292 | /// If this is a vector type, return the getPrimitiveSizeInBits value for the |
293 | /// element type. Otherwise return the getPrimitiveSizeInBits value for this |
294 | /// type. |
295 | unsigned getScalarSizeInBits() const LLVM_READONLY__attribute__((__pure__)); |
296 | |
297 | /// Return the width of the mantissa of this type. This is only valid on |
298 | /// floating-point types. If the FP type does not have a stable mantissa (e.g. |
299 | /// ppc long double), this method returns -1. |
300 | int getFPMantissaWidth() const; |
301 | |
302 | /// Return whether the type is IEEE compatible, as defined by the eponymous |
303 | /// method in APFloat. |
304 | bool isIEEE() const; |
305 | |
306 | /// If this is a vector type, return the element type, otherwise return |
307 | /// 'this'. |
308 | inline Type *getScalarType() const { |
309 | if (isVectorTy()) |
310 | return getContainedType(0); |
311 | return const_cast<Type *>(this); |
312 | } |
313 | |
314 | //===--------------------------------------------------------------------===// |
315 | // Type Iteration support. |
316 | // |
317 | using subtype_iterator = Type * const *; |
318 | |
319 | subtype_iterator subtype_begin() const { return ContainedTys; } |
320 | subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];} |
321 | ArrayRef<Type*> subtypes() const { |
322 | return makeArrayRef(subtype_begin(), subtype_end()); |
323 | } |
324 | |
325 | using subtype_reverse_iterator = std::reverse_iterator<subtype_iterator>; |
326 | |
327 | subtype_reverse_iterator subtype_rbegin() const { |
328 | return subtype_reverse_iterator(subtype_end()); |
329 | } |
330 | subtype_reverse_iterator subtype_rend() const { |
331 | return subtype_reverse_iterator(subtype_begin()); |
332 | } |
333 | |
334 | /// This method is used to implement the type iterator (defined at the end of |
335 | /// the file). For derived types, this returns the types 'contained' in the |
336 | /// derived type. |
337 | Type *getContainedType(unsigned i) const { |
338 | assert(i < NumContainedTys && "Index out of range!")(static_cast <bool> (i < NumContainedTys && "Index out of range!" ) ? void (0) : __assert_fail ("i < NumContainedTys && \"Index out of range!\"" , "llvm/include/llvm/IR/Type.h", 338, __extension__ __PRETTY_FUNCTION__ )); |
339 | return ContainedTys[i]; |
340 | } |
341 | |
342 | /// Return the number of types in the derived type. |
343 | unsigned getNumContainedTypes() const { return NumContainedTys; } |
344 | |
345 | //===--------------------------------------------------------------------===// |
346 | // Helper methods corresponding to subclass methods. This forces a cast to |
347 | // the specified subclass and calls its accessor. "getArrayNumElements" (for |
348 | // example) is shorthand for cast<ArrayType>(Ty)->getNumElements(). This is |
349 | // only intended to cover the core methods that are frequently used, helper |
350 | // methods should not be added here. |
351 | |
352 | inline unsigned getIntegerBitWidth() const; |
353 | |
354 | inline Type *getFunctionParamType(unsigned i) const; |
355 | inline unsigned getFunctionNumParams() const; |
356 | inline bool isFunctionVarArg() const; |
357 | |
358 | inline StringRef getStructName() const; |
359 | inline unsigned getStructNumElements() const; |
360 | inline Type *getStructElementType(unsigned N) const; |
361 | |
362 | inline uint64_t getArrayNumElements() const; |
363 | |
364 | Type *getArrayElementType() const { |
365 | assert(getTypeID() == ArrayTyID)(static_cast <bool> (getTypeID() == ArrayTyID) ? void ( 0) : __assert_fail ("getTypeID() == ArrayTyID", "llvm/include/llvm/IR/Type.h" , 365, __extension__ __PRETTY_FUNCTION__)); |
366 | return ContainedTys[0]; |
367 | } |
368 | |
369 | /// This method is deprecated without replacement. Pointer element types are |
370 | /// not available with opaque pointers. |
371 | Type *getPointerElementType() const { |
372 | return getNonOpaquePointerElementType(); |
373 | } |
374 | |
375 | /// Only use this method in code that is not reachable with opaque pointers, |
376 | /// or part of deprecated methods that will be removed as part of the opaque |
377 | /// pointers transition. |
378 | Type *getNonOpaquePointerElementType() const { |
379 | assert(getTypeID() == PointerTyID)(static_cast <bool> (getTypeID() == PointerTyID) ? void (0) : __assert_fail ("getTypeID() == PointerTyID", "llvm/include/llvm/IR/Type.h" , 379, __extension__ __PRETTY_FUNCTION__)); |
380 | assert(NumContainedTys &&(static_cast <bool> (NumContainedTys && "Attempting to get element type of opaque pointer" ) ? void (0) : __assert_fail ("NumContainedTys && \"Attempting to get element type of opaque pointer\"" , "llvm/include/llvm/IR/Type.h", 381, __extension__ __PRETTY_FUNCTION__ )) |
381 | "Attempting to get element type of opaque pointer")(static_cast <bool> (NumContainedTys && "Attempting to get element type of opaque pointer" ) ? void (0) : __assert_fail ("NumContainedTys && \"Attempting to get element type of opaque pointer\"" , "llvm/include/llvm/IR/Type.h", 381, __extension__ __PRETTY_FUNCTION__ )); |
382 | return ContainedTys[0]; |
383 | } |
384 | |
385 | /// Given vector type, change the element type, |
386 | /// whilst keeping the old number of elements. |
387 | /// For non-vectors simply returns \p EltTy. |
388 | inline Type *getWithNewType(Type *EltTy) const; |
389 | |
390 | /// Given an integer or vector type, change the lane bitwidth to NewBitwidth, |
391 | /// whilst keeping the old number of lanes. |
392 | inline Type *getWithNewBitWidth(unsigned NewBitWidth) const; |
393 | |
394 | /// Given scalar/vector integer type, returns a type with elements twice as |
395 | /// wide as in the original type. For vectors, preserves element count. |
396 | inline Type *getExtendedType() const; |
397 | |
398 | /// Get the address space of this pointer or pointer vector type. |
399 | inline unsigned getPointerAddressSpace() const; |
400 | |
401 | //===--------------------------------------------------------------------===// |
402 | // Static members exported by the Type class itself. Useful for getting |
403 | // instances of Type. |
404 | // |
405 | |
406 | /// Return a type based on an identifier. |
407 | static Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber); |
408 | |
409 | //===--------------------------------------------------------------------===// |
410 | // These are the builtin types that are always available. |
411 | // |
412 | static Type *getVoidTy(LLVMContext &C); |
413 | static Type *getLabelTy(LLVMContext &C); |
414 | static Type *getHalfTy(LLVMContext &C); |
415 | static Type *getBFloatTy(LLVMContext &C); |
416 | static Type *getFloatTy(LLVMContext &C); |
417 | static Type *getDoubleTy(LLVMContext &C); |
418 | static Type *getMetadataTy(LLVMContext &C); |
419 | static Type *getX86_FP80Ty(LLVMContext &C); |
420 | static Type *getFP128Ty(LLVMContext &C); |
421 | static Type *getPPC_FP128Ty(LLVMContext &C); |
422 | static Type *getX86_MMXTy(LLVMContext &C); |
423 | static Type *getX86_AMXTy(LLVMContext &C); |
424 | static Type *getTokenTy(LLVMContext &C); |
425 | static IntegerType *getIntNTy(LLVMContext &C, unsigned N); |
426 | static IntegerType *getInt1Ty(LLVMContext &C); |
427 | static IntegerType *getInt8Ty(LLVMContext &C); |
428 | static IntegerType *getInt16Ty(LLVMContext &C); |
429 | static IntegerType *getInt32Ty(LLVMContext &C); |
430 | static IntegerType *getInt64Ty(LLVMContext &C); |
431 | static IntegerType *getInt128Ty(LLVMContext &C); |
432 | template <typename ScalarTy> static Type *getScalarTy(LLVMContext &C) { |
433 | int noOfBits = sizeof(ScalarTy) * CHAR_BIT8; |
434 | if (std::is_integral<ScalarTy>::value) { |
435 | return (Type*) Type::getIntNTy(C, noOfBits); |
436 | } else if (std::is_floating_point<ScalarTy>::value) { |
437 | switch (noOfBits) { |
438 | case 32: |
439 | return Type::getFloatTy(C); |
440 | case 64: |
441 | return Type::getDoubleTy(C); |
442 | } |
443 | } |
444 | llvm_unreachable("Unsupported type in Type::getScalarTy")::llvm::llvm_unreachable_internal("Unsupported type in Type::getScalarTy" , "llvm/include/llvm/IR/Type.h", 444); |
445 | } |
446 | static Type *getFloatingPointTy(LLVMContext &C, const fltSemantics &S); |
447 | |
448 | //===--------------------------------------------------------------------===// |
449 | // Convenience methods for getting pointer types with one of the above builtin |
450 | // types as pointee. |
451 | // |
452 | static PointerType *getHalfPtrTy(LLVMContext &C, unsigned AS = 0); |
453 | static PointerType *getBFloatPtrTy(LLVMContext &C, unsigned AS = 0); |
454 | static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0); |
455 | static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0); |
456 | static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0); |
457 | static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0); |
458 | static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0); |
459 | static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0); |
460 | static PointerType *getX86_AMXPtrTy(LLVMContext &C, unsigned AS = 0); |
461 | static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0); |
462 | static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0); |
463 | static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0); |
464 | static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0); |
465 | static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0); |
466 | static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0); |
467 | |
468 | /// Return a pointer to the current type. This is equivalent to |
469 | /// PointerType::get(Foo, AddrSpace). |
470 | /// TODO: Remove this after opaque pointer transition is complete. |
471 | PointerType *getPointerTo(unsigned AddrSpace = 0) const; |
472 | |
473 | private: |
474 | /// Derived types like structures and arrays are sized iff all of the members |
475 | /// of the type are sized as well. Since asking for their size is relatively |
476 | /// uncommon, move this operation out-of-line. |
477 | bool isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited = nullptr) const; |
478 | }; |
479 | |
480 | // Printing of types. |
481 | inline raw_ostream &operator<<(raw_ostream &OS, const Type &T) { |
482 | T.print(OS); |
483 | return OS; |
484 | } |
485 | |
486 | // allow isa<PointerType>(x) to work without DerivedTypes.h included. |
487 | template <> struct isa_impl<PointerType, Type> { |
488 | static inline bool doit(const Type &Ty) { |
489 | return Ty.getTypeID() == Type::PointerTyID; |
490 | } |
491 | }; |
492 | |
493 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
494 | DEFINE_ISA_CONVERSION_FUNCTIONS(Type, LLVMTypeRef)inline Type *unwrap(LLVMTypeRef P) { return reinterpret_cast< Type*>(P); } inline LLVMTypeRef wrap(const Type *P) { return reinterpret_cast<LLVMTypeRef>(const_cast<Type*>( P)); } template<typename T> inline T *unwrap(LLVMTypeRef P) { return cast<T>(unwrap(P)); } |
495 | |
496 | /* Specialized opaque type conversions. |
497 | */ |
498 | inline Type **unwrap(LLVMTypeRef* Tys) { |
499 | return reinterpret_cast<Type**>(Tys); |
500 | } |
501 | |
502 | inline LLVMTypeRef *wrap(Type **Tys) { |
503 | return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys)); |
504 | } |
505 | |
506 | } // end namespace llvm |
507 | |
508 | #endif // LLVM_IR_TYPE_H |