1 //===- LoopVersioningLICM.cpp - LICM Loop Versioning ----------------------===//
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 // When alias analysis is uncertain about the aliasing between any two accesses,
10 // it will return MayAlias. This uncertainty from alias analysis restricts LICM
11 // from proceeding further. In cases where alias analysis is uncertain we might
12 // use loop versioning as an alternative.
13 //
14 // Loop Versioning will create a version of the loop with aggressive aliasing
15 // assumptions in addition to the original with conservative (default) aliasing
16 // assumptions. The version of the loop making aggressive aliasing assumptions
17 // will have all the memory accesses marked as no-alias. These two versions of
18 // loop will be preceded by a memory runtime check. This runtime check consists
19 // of bound checks for all unique memory accessed in loop, and it ensures the
20 // lack of memory aliasing. The result of the runtime check determines which of
21 // the loop versions is executed: If the runtime check detects any memory
22 // aliasing, then the original loop is executed. Otherwise, the version with
23 // aggressive aliasing assumptions is used.
24 //
25 // Following are the top level steps:
26 //
27 // a) Perform LoopVersioningLICM's feasibility check.
28 // b) If loop is a candidate for versioning then create a memory bound check,
29 // by considering all the memory accesses in loop body.
30 // c) Clone original loop and set all memory accesses as no-alias in new loop.
31 // d) Set original loop & versioned loop as a branch target of the runtime check
32 // result.
33 //
34 // It transforms loop as shown below:
35 //
36 // +----------------+
37 // |Runtime Memcheck|
38 // +----------------+
39 // |
40 // +----------+----------------+----------+
41 // | |
42 // +---------+----------+ +-----------+----------+
43 // |Orig Loop Preheader | |Cloned Loop Preheader |
44 // +--------------------+ +----------------------+
45 // | |
46 // +--------------------+ +----------------------+
47 // |Orig Loop Body | |Cloned Loop Body |
48 // +--------------------+ +----------------------+
49 // | |
50 // +--------------------+ +----------------------+
51 // |Orig Loop Exit Block| |Cloned Loop Exit Block|
52 // +--------------------+ +-----------+----------+
53 // | |
54 // +----------+--------------+-----------+
55 // |
56 // +-----+----+
57 // |Join Block|
58 // +----------+
59 //
60 //===----------------------------------------------------------------------===//
61
62 #include "llvm/Transforms/Scalar/LoopVersioningLICM.h"
63 #include "llvm/ADT/SmallVector.h"
64 #include "llvm/ADT/StringRef.h"
65 #include "llvm/Analysis/AliasAnalysis.h"
66 #include "llvm/Analysis/AliasSetTracker.h"
67 #include "llvm/Analysis/GlobalsModRef.h"
68 #include "llvm/Analysis/LoopAccessAnalysis.h"
69 #include "llvm/Analysis/LoopInfo.h"
70 #include "llvm/Analysis/LoopPass.h"
71 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
72 #include "llvm/Analysis/ScalarEvolution.h"
73 #include "llvm/IR/Dominators.h"
74 #include "llvm/IR/Instruction.h"
75 #include "llvm/IR/Instructions.h"
76 #include "llvm/IR/LLVMContext.h"
77 #include "llvm/IR/MDBuilder.h"
78 #include "llvm/IR/Metadata.h"
79 #include "llvm/IR/Value.h"
80 #include "llvm/InitializePasses.h"
81 #include "llvm/Pass.h"
82 #include "llvm/Support/Casting.h"
83 #include "llvm/Support/CommandLine.h"
84 #include "llvm/Support/Debug.h"
85 #include "llvm/Support/raw_ostream.h"
86 #include "llvm/Transforms/Scalar.h"
87 #include "llvm/Transforms/Utils.h"
88 #include "llvm/Transforms/Utils/LoopUtils.h"
89 #include "llvm/Transforms/Utils/LoopVersioning.h"
90 #include <cassert>
91 #include <memory>
92
93 using namespace llvm;
94
95 #define DEBUG_TYPE "loop-versioning-licm"
96
97 static const char *LICMVersioningMetaData = "llvm.loop.licm_versioning.disable";
98
99 /// Threshold minimum allowed percentage for possible
100 /// invariant instructions in a loop.
101 static cl::opt<float>
102 LVInvarThreshold("licm-versioning-invariant-threshold",
103 cl::desc("LoopVersioningLICM's minimum allowed percentage"
104 "of possible invariant instructions per loop"),
105 cl::init(25), cl::Hidden);
106
107 /// Threshold for maximum allowed loop nest/depth
108 static cl::opt<unsigned> LVLoopDepthThreshold(
109 "licm-versioning-max-depth-threshold",
110 cl::desc(
111 "LoopVersioningLICM's threshold for maximum allowed loop nest/depth"),
112 cl::init(2), cl::Hidden);
113
114 namespace {
115
116 struct LoopVersioningLICMLegacyPass : public LoopPass {
117 static char ID;
118
LoopVersioningLICMLegacyPass__anonbad469a30111::LoopVersioningLICMLegacyPass119 LoopVersioningLICMLegacyPass() : LoopPass(ID) {
120 initializeLoopVersioningLICMLegacyPassPass(
121 *PassRegistry::getPassRegistry());
122 }
123
124 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
125
getPassName__anonbad469a30111::LoopVersioningLICMLegacyPass126 StringRef getPassName() const override { return "Loop Versioning for LICM"; }
127
getAnalysisUsage__anonbad469a30111::LoopVersioningLICMLegacyPass128 void getAnalysisUsage(AnalysisUsage &AU) const override {
129 AU.setPreservesCFG();
130 AU.addRequired<AAResultsWrapperPass>();
131 AU.addRequired<DominatorTreeWrapperPass>();
132 AU.addRequiredID(LCSSAID);
133 AU.addRequired<LoopAccessLegacyAnalysis>();
134 AU.addRequired<LoopInfoWrapperPass>();
135 AU.addRequiredID(LoopSimplifyID);
136 AU.addRequired<ScalarEvolutionWrapperPass>();
137 AU.addPreserved<AAResultsWrapperPass>();
138 AU.addPreserved<GlobalsAAWrapperPass>();
139 AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
140 }
141 };
142
143 struct LoopVersioningLICM {
144 // We don't explicitly pass in LoopAccessInfo to the constructor since the
145 // loop versioning might return early due to instructions that are not safe
146 // for versioning. By passing the proxy instead the construction of
147 // LoopAccessInfo will take place only when it's necessary.
LoopVersioningLICM__anonbad469a30111::LoopVersioningLICM148 LoopVersioningLICM(AliasAnalysis *AA, ScalarEvolution *SE,
149 OptimizationRemarkEmitter *ORE,
150 LoopAccessInfoManager &LAIs, LoopInfo &LI,
151 Loop *CurLoop)
152 : AA(AA), SE(SE), LAIs(LAIs), LI(LI), CurLoop(CurLoop),
153 LoopDepthThreshold(LVLoopDepthThreshold),
154 InvariantThreshold(LVInvarThreshold), ORE(ORE) {}
155
156 bool run(DominatorTree *DT);
157
158 private:
159 // Current AliasAnalysis information
160 AliasAnalysis *AA;
161
162 // Current ScalarEvolution
163 ScalarEvolution *SE;
164
165 // Current Loop's LoopAccessInfo
166 const LoopAccessInfo *LAI = nullptr;
167
168 // Proxy for retrieving LoopAccessInfo.
169 LoopAccessInfoManager &LAIs;
170
171 LoopInfo &LI;
172
173 // The current loop we are working on.
174 Loop *CurLoop;
175
176 // Maximum loop nest threshold
177 unsigned LoopDepthThreshold;
178
179 // Minimum invariant threshold
180 float InvariantThreshold;
181
182 // Counter to track num of load & store
183 unsigned LoadAndStoreCounter = 0;
184
185 // Counter to track num of invariant
186 unsigned InvariantCounter = 0;
187
188 // Read only loop marker.
189 bool IsReadOnlyLoop = true;
190
191 // OptimizationRemarkEmitter
192 OptimizationRemarkEmitter *ORE;
193
194 bool isLegalForVersioning();
195 bool legalLoopStructure();
196 bool legalLoopInstructions();
197 bool legalLoopMemoryAccesses();
198 bool isLoopAlreadyVisited();
199 void setNoAliasToLoop(Loop *VerLoop);
200 bool instructionSafeForVersioning(Instruction *I);
201 };
202
203 } // end anonymous namespace
204
205 /// Check loop structure and confirms it's good for LoopVersioningLICM.
legalLoopStructure()206 bool LoopVersioningLICM::legalLoopStructure() {
207 // Loop must be in loop simplify form.
208 if (!CurLoop->isLoopSimplifyForm()) {
209 LLVM_DEBUG(dbgs() << " loop is not in loop-simplify form.\n");
210 return false;
211 }
212 // Loop should be innermost loop, if not return false.
213 if (!CurLoop->getSubLoops().empty()) {
214 LLVM_DEBUG(dbgs() << " loop is not innermost\n");
215 return false;
216 }
217 // Loop should have a single backedge, if not return false.
218 if (CurLoop->getNumBackEdges() != 1) {
219 LLVM_DEBUG(dbgs() << " loop has multiple backedges\n");
220 return false;
221 }
222 // Loop must have a single exiting block, if not return false.
223 if (!CurLoop->getExitingBlock()) {
224 LLVM_DEBUG(dbgs() << " loop has multiple exiting block\n");
225 return false;
226 }
227 // We only handle bottom-tested loop, i.e. loop in which the condition is
228 // checked at the end of each iteration. With that we can assume that all
229 // instructions in the loop are executed the same number of times.
230 if (CurLoop->getExitingBlock() != CurLoop->getLoopLatch()) {
231 LLVM_DEBUG(dbgs() << " loop is not bottom tested\n");
232 return false;
233 }
234 // Parallel loops must not have aliasing loop-invariant memory accesses.
235 // Hence we don't need to version anything in this case.
236 if (CurLoop->isAnnotatedParallel()) {
237 LLVM_DEBUG(dbgs() << " Parallel loop is not worth versioning\n");
238 return false;
239 }
240 // Loop depth more then LoopDepthThreshold are not allowed
241 if (CurLoop->getLoopDepth() > LoopDepthThreshold) {
242 LLVM_DEBUG(dbgs() << " loop depth is more then threshold\n");
243 return false;
244 }
245 // We need to be able to compute the loop trip count in order
246 // to generate the bound checks.
247 const SCEV *ExitCount = SE->getBackedgeTakenCount(CurLoop);
248 if (isa<SCEVCouldNotCompute>(ExitCount)) {
249 LLVM_DEBUG(dbgs() << " loop does not has trip count\n");
250 return false;
251 }
252 return true;
253 }
254
255 /// Check memory accesses in loop and confirms it's good for
256 /// LoopVersioningLICM.
legalLoopMemoryAccesses()257 bool LoopVersioningLICM::legalLoopMemoryAccesses() {
258 // Loop over the body of this loop, construct AST.
259 BatchAAResults BAA(*AA);
260 AliasSetTracker AST(BAA);
261 for (auto *Block : CurLoop->getBlocks()) {
262 // Ignore blocks in subloops.
263 if (LI.getLoopFor(Block) == CurLoop)
264 AST.add(*Block);
265 }
266
267 // Memory check:
268 // Transform phase will generate a versioned loop and also a runtime check to
269 // ensure the pointers are independent and they don’t alias.
270 // In version variant of loop, alias meta data asserts that all access are
271 // mutually independent.
272 //
273 // Pointers aliasing in alias domain are avoided because with multiple
274 // aliasing domains we may not be able to hoist potential loop invariant
275 // access out of the loop.
276 //
277 // Iterate over alias tracker sets, and confirm AliasSets doesn't have any
278 // must alias set.
279 bool HasMayAlias = false;
280 bool TypeSafety = false;
281 bool HasMod = false;
282 for (const auto &I : AST) {
283 const AliasSet &AS = I;
284 // Skip Forward Alias Sets, as this should be ignored as part of
285 // the AliasSetTracker object.
286 if (AS.isForwardingAliasSet())
287 continue;
288 // With MustAlias its not worth adding runtime bound check.
289 if (AS.isMustAlias())
290 return false;
291 Value *SomePtr = AS.begin()->getValue();
292 bool TypeCheck = true;
293 // Check for Mod & MayAlias
294 HasMayAlias |= AS.isMayAlias();
295 HasMod |= AS.isMod();
296 for (const auto &A : AS) {
297 Value *Ptr = A.getValue();
298 // Alias tracker should have pointers of same data type.
299 TypeCheck = (TypeCheck && (SomePtr->getType() == Ptr->getType()));
300 }
301 // At least one alias tracker should have pointers of same data type.
302 TypeSafety |= TypeCheck;
303 }
304 // Ensure types should be of same type.
305 if (!TypeSafety) {
306 LLVM_DEBUG(dbgs() << " Alias tracker type safety failed!\n");
307 return false;
308 }
309 // Ensure loop body shouldn't be read only.
310 if (!HasMod) {
311 LLVM_DEBUG(dbgs() << " No memory modified in loop body\n");
312 return false;
313 }
314 // Make sure alias set has may alias case.
315 // If there no alias memory ambiguity, return false.
316 if (!HasMayAlias) {
317 LLVM_DEBUG(dbgs() << " No ambiguity in memory access.\n");
318 return false;
319 }
320 return true;
321 }
322
323 /// Check loop instructions safe for Loop versioning.
324 /// It returns true if it's safe else returns false.
325 /// Consider following:
326 /// 1) Check all load store in loop body are non atomic & non volatile.
327 /// 2) Check function call safety, by ensuring its not accessing memory.
328 /// 3) Loop body shouldn't have any may throw instruction.
329 /// 4) Loop body shouldn't have any convergent or noduplicate instructions.
instructionSafeForVersioning(Instruction * I)330 bool LoopVersioningLICM::instructionSafeForVersioning(Instruction *I) {
331 assert(I != nullptr && "Null instruction found!");
332 // Check function call safety
333 if (auto *Call = dyn_cast<CallBase>(I)) {
334 if (Call->isConvergent() || Call->cannotDuplicate()) {
335 LLVM_DEBUG(dbgs() << " Convergent call site found.\n");
336 return false;
337 }
338
339 if (!AA->doesNotAccessMemory(Call)) {
340 LLVM_DEBUG(dbgs() << " Unsafe call site found.\n");
341 return false;
342 }
343 }
344
345 // Avoid loops with possiblity of throw
346 if (I->mayThrow()) {
347 LLVM_DEBUG(dbgs() << " May throw instruction found in loop body\n");
348 return false;
349 }
350 // If current instruction is load instructions
351 // make sure it's a simple load (non atomic & non volatile)
352 if (I->mayReadFromMemory()) {
353 LoadInst *Ld = dyn_cast<LoadInst>(I);
354 if (!Ld || !Ld->isSimple()) {
355 LLVM_DEBUG(dbgs() << " Found a non-simple load.\n");
356 return false;
357 }
358 LoadAndStoreCounter++;
359 Value *Ptr = Ld->getPointerOperand();
360 // Check loop invariant.
361 if (SE->isLoopInvariant(SE->getSCEV(Ptr), CurLoop))
362 InvariantCounter++;
363 }
364 // If current instruction is store instruction
365 // make sure it's a simple store (non atomic & non volatile)
366 else if (I->mayWriteToMemory()) {
367 StoreInst *St = dyn_cast<StoreInst>(I);
368 if (!St || !St->isSimple()) {
369 LLVM_DEBUG(dbgs() << " Found a non-simple store.\n");
370 return false;
371 }
372 LoadAndStoreCounter++;
373 Value *Ptr = St->getPointerOperand();
374 // Check loop invariant.
375 if (SE->isLoopInvariant(SE->getSCEV(Ptr), CurLoop))
376 InvariantCounter++;
377
378 IsReadOnlyLoop = false;
379 }
380 return true;
381 }
382
383 /// Check loop instructions and confirms it's good for
384 /// LoopVersioningLICM.
legalLoopInstructions()385 bool LoopVersioningLICM::legalLoopInstructions() {
386 // Resetting counters.
387 LoadAndStoreCounter = 0;
388 InvariantCounter = 0;
389 IsReadOnlyLoop = true;
390 using namespace ore;
391 // Iterate over loop blocks and instructions of each block and check
392 // instruction safety.
393 for (auto *Block : CurLoop->getBlocks())
394 for (auto &Inst : *Block) {
395 // If instruction is unsafe just return false.
396 if (!instructionSafeForVersioning(&Inst)) {
397 ORE->emit([&]() {
398 return OptimizationRemarkMissed(DEBUG_TYPE, "IllegalLoopInst", &Inst)
399 << " Unsafe Loop Instruction";
400 });
401 return false;
402 }
403 }
404 // Get LoopAccessInfo from current loop via the proxy.
405 LAI = &LAIs.getInfo(*CurLoop);
406 // Check LoopAccessInfo for need of runtime check.
407 if (LAI->getRuntimePointerChecking()->getChecks().empty()) {
408 LLVM_DEBUG(dbgs() << " LAA: Runtime check not found !!\n");
409 return false;
410 }
411 // Number of runtime-checks should be less then RuntimeMemoryCheckThreshold
412 if (LAI->getNumRuntimePointerChecks() >
413 VectorizerParams::RuntimeMemoryCheckThreshold) {
414 LLVM_DEBUG(
415 dbgs() << " LAA: Runtime checks are more than threshold !!\n");
416 ORE->emit([&]() {
417 return OptimizationRemarkMissed(DEBUG_TYPE, "RuntimeCheck",
418 CurLoop->getStartLoc(),
419 CurLoop->getHeader())
420 << "Number of runtime checks "
421 << NV("RuntimeChecks", LAI->getNumRuntimePointerChecks())
422 << " exceeds threshold "
423 << NV("Threshold", VectorizerParams::RuntimeMemoryCheckThreshold);
424 });
425 return false;
426 }
427 // Loop should have at least one invariant load or store instruction.
428 if (!InvariantCounter) {
429 LLVM_DEBUG(dbgs() << " Invariant not found !!\n");
430 return false;
431 }
432 // Read only loop not allowed.
433 if (IsReadOnlyLoop) {
434 LLVM_DEBUG(dbgs() << " Found a read-only loop!\n");
435 return false;
436 }
437 // Profitablity check:
438 // Check invariant threshold, should be in limit.
439 if (InvariantCounter * 100 < InvariantThreshold * LoadAndStoreCounter) {
440 LLVM_DEBUG(
441 dbgs()
442 << " Invariant load & store are less then defined threshold\n");
443 LLVM_DEBUG(dbgs() << " Invariant loads & stores: "
444 << ((InvariantCounter * 100) / LoadAndStoreCounter)
445 << "%\n");
446 LLVM_DEBUG(dbgs() << " Invariant loads & store threshold: "
447 << InvariantThreshold << "%\n");
448 ORE->emit([&]() {
449 return OptimizationRemarkMissed(DEBUG_TYPE, "InvariantThreshold",
450 CurLoop->getStartLoc(),
451 CurLoop->getHeader())
452 << "Invariant load & store "
453 << NV("LoadAndStoreCounter",
454 ((InvariantCounter * 100) / LoadAndStoreCounter))
455 << " are less then defined threshold "
456 << NV("Threshold", InvariantThreshold);
457 });
458 return false;
459 }
460 return true;
461 }
462
463 /// It checks loop is already visited or not.
464 /// check loop meta data, if loop revisited return true
465 /// else false.
isLoopAlreadyVisited()466 bool LoopVersioningLICM::isLoopAlreadyVisited() {
467 // Check LoopVersioningLICM metadata into loop
468 if (findStringMetadataForLoop(CurLoop, LICMVersioningMetaData)) {
469 return true;
470 }
471 return false;
472 }
473
474 /// Checks legality for LoopVersioningLICM by considering following:
475 /// a) loop structure legality b) loop instruction legality
476 /// c) loop memory access legality.
477 /// Return true if legal else returns false.
isLegalForVersioning()478 bool LoopVersioningLICM::isLegalForVersioning() {
479 using namespace ore;
480 LLVM_DEBUG(dbgs() << "Loop: " << *CurLoop);
481 // Make sure not re-visiting same loop again.
482 if (isLoopAlreadyVisited()) {
483 LLVM_DEBUG(
484 dbgs() << " Revisiting loop in LoopVersioningLICM not allowed.\n\n");
485 return false;
486 }
487 // Check loop structure leagality.
488 if (!legalLoopStructure()) {
489 LLVM_DEBUG(
490 dbgs() << " Loop structure not suitable for LoopVersioningLICM\n\n");
491 ORE->emit([&]() {
492 return OptimizationRemarkMissed(DEBUG_TYPE, "IllegalLoopStruct",
493 CurLoop->getStartLoc(),
494 CurLoop->getHeader())
495 << " Unsafe Loop structure";
496 });
497 return false;
498 }
499 // Check loop instruction leagality.
500 if (!legalLoopInstructions()) {
501 LLVM_DEBUG(
502 dbgs()
503 << " Loop instructions not suitable for LoopVersioningLICM\n\n");
504 return false;
505 }
506 // Check loop memory access leagality.
507 if (!legalLoopMemoryAccesses()) {
508 LLVM_DEBUG(
509 dbgs()
510 << " Loop memory access not suitable for LoopVersioningLICM\n\n");
511 ORE->emit([&]() {
512 return OptimizationRemarkMissed(DEBUG_TYPE, "IllegalLoopMemoryAccess",
513 CurLoop->getStartLoc(),
514 CurLoop->getHeader())
515 << " Unsafe Loop memory access";
516 });
517 return false;
518 }
519 // Loop versioning is feasible, return true.
520 LLVM_DEBUG(dbgs() << " Loop Versioning found to be beneficial\n\n");
521 ORE->emit([&]() {
522 return OptimizationRemark(DEBUG_TYPE, "IsLegalForVersioning",
523 CurLoop->getStartLoc(), CurLoop->getHeader())
524 << " Versioned loop for LICM."
525 << " Number of runtime checks we had to insert "
526 << NV("RuntimeChecks", LAI->getNumRuntimePointerChecks());
527 });
528 return true;
529 }
530
531 /// Update loop with aggressive aliasing assumptions.
532 /// It marks no-alias to any pairs of memory operations by assuming
533 /// loop should not have any must-alias memory accesses pairs.
534 /// During LoopVersioningLICM legality we ignore loops having must
535 /// aliasing memory accesses.
setNoAliasToLoop(Loop * VerLoop)536 void LoopVersioningLICM::setNoAliasToLoop(Loop *VerLoop) {
537 // Get latch terminator instruction.
538 Instruction *I = VerLoop->getLoopLatch()->getTerminator();
539 // Create alias scope domain.
540 MDBuilder MDB(I->getContext());
541 MDNode *NewDomain = MDB.createAnonymousAliasScopeDomain("LVDomain");
542 StringRef Name = "LVAliasScope";
543 MDNode *NewScope = MDB.createAnonymousAliasScope(NewDomain, Name);
544 SmallVector<Metadata *, 4> Scopes{NewScope}, NoAliases{NewScope};
545 // Iterate over each instruction of loop.
546 // set no-alias for all load & store instructions.
547 for (auto *Block : CurLoop->getBlocks()) {
548 for (auto &Inst : *Block) {
549 // Only interested in instruction that may modify or read memory.
550 if (!Inst.mayReadFromMemory() && !Inst.mayWriteToMemory())
551 continue;
552 // Set no-alias for current instruction.
553 Inst.setMetadata(
554 LLVMContext::MD_noalias,
555 MDNode::concatenate(Inst.getMetadata(LLVMContext::MD_noalias),
556 MDNode::get(Inst.getContext(), NoAliases)));
557 // set alias-scope for current instruction.
558 Inst.setMetadata(
559 LLVMContext::MD_alias_scope,
560 MDNode::concatenate(Inst.getMetadata(LLVMContext::MD_alias_scope),
561 MDNode::get(Inst.getContext(), Scopes)));
562 }
563 }
564 }
565
runOnLoop(Loop * L,LPPassManager & LPM)566 bool LoopVersioningLICMLegacyPass::runOnLoop(Loop *L, LPPassManager &LPM) {
567 if (skipLoop(L))
568 return false;
569
570 AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
571 ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
572 OptimizationRemarkEmitter *ORE =
573 &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
574 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
575 DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
576 auto &LAIs = getAnalysis<LoopAccessLegacyAnalysis>().getLAIs();
577
578 return LoopVersioningLICM(AA, SE, ORE, LAIs, LI, L).run(DT);
579 }
580
run(DominatorTree * DT)581 bool LoopVersioningLICM::run(DominatorTree *DT) {
582 // Do not do the transformation if disabled by metadata.
583 if (hasLICMVersioningTransformation(CurLoop) & TM_Disable)
584 return false;
585
586 bool Changed = false;
587
588 // Check feasiblity of LoopVersioningLICM.
589 // If versioning found to be feasible and beneficial then proceed
590 // else simply return, by cleaning up memory.
591 if (isLegalForVersioning()) {
592 // Do loop versioning.
593 // Create memcheck for memory accessed inside loop.
594 // Clone original loop, and set blocks properly.
595 LoopVersioning LVer(*LAI, LAI->getRuntimePointerChecking()->getChecks(),
596 CurLoop, &LI, DT, SE);
597 LVer.versionLoop();
598 // Set Loop Versioning metaData for original loop.
599 addStringMetadataToLoop(LVer.getNonVersionedLoop(), LICMVersioningMetaData);
600 // Set Loop Versioning metaData for version loop.
601 addStringMetadataToLoop(LVer.getVersionedLoop(), LICMVersioningMetaData);
602 // Set "llvm.mem.parallel_loop_access" metaData to versioned loop.
603 // FIXME: "llvm.mem.parallel_loop_access" annotates memory access
604 // instructions, not loops.
605 addStringMetadataToLoop(LVer.getVersionedLoop(),
606 "llvm.mem.parallel_loop_access");
607 // Update version loop with aggressive aliasing assumption.
608 setNoAliasToLoop(LVer.getVersionedLoop());
609 Changed = true;
610 }
611 return Changed;
612 }
613
614 char LoopVersioningLICMLegacyPass::ID = 0;
615
616 INITIALIZE_PASS_BEGIN(LoopVersioningLICMLegacyPass, "loop-versioning-licm",
617 "Loop Versioning For LICM", false, false)
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)618 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
619 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
620 INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
621 INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass)
622 INITIALIZE_PASS_DEPENDENCY(LoopAccessLegacyAnalysis)
623 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
624 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
625 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
626 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
627 INITIALIZE_PASS_END(LoopVersioningLICMLegacyPass, "loop-versioning-licm",
628 "Loop Versioning For LICM", false, false)
629
630 Pass *llvm::createLoopVersioningLICMPass() {
631 return new LoopVersioningLICMLegacyPass();
632 }
633
634 namespace llvm {
635
run(Loop & L,LoopAnalysisManager & AM,LoopStandardAnalysisResults & LAR,LPMUpdater & U)636 PreservedAnalyses LoopVersioningLICMPass::run(Loop &L, LoopAnalysisManager &AM,
637 LoopStandardAnalysisResults &LAR,
638 LPMUpdater &U) {
639 AliasAnalysis *AA = &LAR.AA;
640 ScalarEvolution *SE = &LAR.SE;
641 DominatorTree *DT = &LAR.DT;
642 const Function *F = L.getHeader()->getParent();
643 OptimizationRemarkEmitter ORE(F);
644
645 LoopAccessInfoManager LAIs(*SE, *AA, *DT, LAR.LI, nullptr);
646 if (!LoopVersioningLICM(AA, SE, &ORE, LAIs, LAR.LI, &L).run(DT))
647 return PreservedAnalyses::all();
648 return getLoopPassPreservedAnalyses();
649 }
650 } // namespace llvm
651