1 //===- LoopVersioningLICM.cpp - LICM Loop Versioning ----------------------===//
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
7 //===----------------------------------------------------------------------===//
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.
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.
25 // Following are the top level steps:
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
34 // It transforms loop as shown below:
40 // +----------+----------------+----------+
42 // +---------+----------+ +-----------+----------+
43 // |Orig Loop Preheader | |Cloned Loop Preheader |
44 // +--------------------+ +----------------------+
46 // +--------------------+ +----------------------+
47 // |Orig Loop Body | |Cloned Loop Body |
48 // +--------------------+ +----------------------+
50 // +--------------------+ +----------------------+
51 // |Orig Loop Exit Block| |Cloned Loop Exit Block|
52 // +--------------------+ +-----------+----------+
54 // +----------+--------------+-----------+
60 //===----------------------------------------------------------------------===//
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/Constants.h"
74 #include "llvm/IR/Dominators.h"
75 #include "llvm/IR/Instruction.h"
76 #include "llvm/IR/Instructions.h"
77 #include "llvm/IR/LLVMContext.h"
78 #include "llvm/IR/MDBuilder.h"
79 #include "llvm/IR/Metadata.h"
80 #include "llvm/IR/Type.h"
81 #include "llvm/IR/Value.h"
82 #include "llvm/InitializePasses.h"
83 #include "llvm/Pass.h"
84 #include "llvm/Support/Casting.h"
85 #include "llvm/Support/CommandLine.h"
86 #include "llvm/Support/Debug.h"
87 #include "llvm/Support/raw_ostream.h"
88 #include "llvm/Transforms/Scalar.h"
89 #include "llvm/Transforms/Utils.h"
90 #include "llvm/Transforms/Utils/LoopUtils.h"
91 #include "llvm/Transforms/Utils/LoopVersioning.h"
97 #define DEBUG_TYPE "loop-versioning-licm"
99 static const char *LICMVersioningMetaData
= "llvm.loop.licm_versioning.disable";
101 /// Threshold minimum allowed percentage for possible
102 /// invariant instructions in a loop.
103 static cl::opt
<float>
104 LVInvarThreshold("licm-versioning-invariant-threshold",
105 cl::desc("LoopVersioningLICM's minimum allowed percentage"
106 "of possible invariant instructions per loop"),
107 cl::init(25), cl::Hidden
);
109 /// Threshold for maximum allowed loop nest/depth
110 static cl::opt
<unsigned> LVLoopDepthThreshold(
111 "licm-versioning-max-depth-threshold",
113 "LoopVersioningLICM's threshold for maximum allowed loop nest/depth"),
114 cl::init(2), cl::Hidden
);
118 struct LoopVersioningLICMLegacyPass
: public LoopPass
{
121 LoopVersioningLICMLegacyPass() : LoopPass(ID
) {
122 initializeLoopVersioningLICMLegacyPassPass(
123 *PassRegistry::getPassRegistry());
126 bool runOnLoop(Loop
*L
, LPPassManager
&LPM
) override
;
128 StringRef
getPassName() const override
{ return "Loop Versioning for LICM"; }
130 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
131 AU
.setPreservesCFG();
132 AU
.addRequired
<AAResultsWrapperPass
>();
133 AU
.addRequired
<DominatorTreeWrapperPass
>();
134 AU
.addRequiredID(LCSSAID
);
135 AU
.addRequired
<LoopAccessLegacyAnalysis
>();
136 AU
.addRequired
<LoopInfoWrapperPass
>();
137 AU
.addRequiredID(LoopSimplifyID
);
138 AU
.addRequired
<ScalarEvolutionWrapperPass
>();
139 AU
.addPreserved
<AAResultsWrapperPass
>();
140 AU
.addPreserved
<GlobalsAAWrapperPass
>();
141 AU
.addRequired
<OptimizationRemarkEmitterWrapperPass
>();
145 struct LoopVersioningLICM
{
146 // We don't explicitly pass in LoopAccessInfo to the constructor since the
147 // loop versioning might return early due to instructions that are not safe
148 // for versioning. By passing the proxy instead the construction of
149 // LoopAccessInfo will take place only when it's necessary.
150 LoopVersioningLICM(AliasAnalysis
*AA
, ScalarEvolution
*SE
,
151 OptimizationRemarkEmitter
*ORE
,
152 function_ref
<const LoopAccessInfo
&(Loop
*)> GetLAI
)
153 : AA(AA
), SE(SE
), GetLAI(GetLAI
),
154 LoopDepthThreshold(LVLoopDepthThreshold
),
155 InvariantThreshold(LVInvarThreshold
), ORE(ORE
) {}
157 bool runOnLoop(Loop
*L
, LoopInfo
*LI
, DominatorTree
*DT
);
163 LoadAndStoreCounter
= 0;
164 InvariantCounter
= 0;
165 IsReadOnlyLoop
= true;
172 AutoResetter(LoopVersioningLICM
&LVLICM
) : LVLICM(LVLICM
) {}
173 ~AutoResetter() { LVLICM
.reset(); }
176 LoopVersioningLICM
&LVLICM
;
180 // Current AliasAnalysis information
181 AliasAnalysis
*AA
= nullptr;
183 // Current ScalarEvolution
184 ScalarEvolution
*SE
= nullptr;
186 // Current Loop's LoopAccessInfo
187 const LoopAccessInfo
*LAI
= nullptr;
189 // Proxy for retrieving LoopAccessInfo.
190 function_ref
<const LoopAccessInfo
&(Loop
*)> GetLAI
;
192 // The current loop we are working on.
193 Loop
*CurLoop
= nullptr;
195 // AliasSet information for the current loop.
196 std::unique_ptr
<AliasSetTracker
> CurAST
;
198 // Maximum loop nest threshold
199 unsigned LoopDepthThreshold
;
201 // Minimum invariant threshold
202 float InvariantThreshold
;
204 // Counter to track num of load & store
205 unsigned LoadAndStoreCounter
= 0;
207 // Counter to track num of invariant
208 unsigned InvariantCounter
= 0;
210 // Read only loop marker.
211 bool IsReadOnlyLoop
= true;
213 // OptimizationRemarkEmitter
214 OptimizationRemarkEmitter
*ORE
;
216 bool isLegalForVersioning();
217 bool legalLoopStructure();
218 bool legalLoopInstructions();
219 bool legalLoopMemoryAccesses();
220 bool isLoopAlreadyVisited();
221 void setNoAliasToLoop(Loop
*VerLoop
);
222 bool instructionSafeForVersioning(Instruction
*I
);
225 } // end anonymous namespace
227 /// Check loop structure and confirms it's good for LoopVersioningLICM.
228 bool LoopVersioningLICM::legalLoopStructure() {
229 // Loop must be in loop simplify form.
230 if (!CurLoop
->isLoopSimplifyForm()) {
231 LLVM_DEBUG(dbgs() << " loop is not in loop-simplify form.\n");
234 // Loop should be innermost loop, if not return false.
235 if (!CurLoop
->getSubLoops().empty()) {
236 LLVM_DEBUG(dbgs() << " loop is not innermost\n");
239 // Loop should have a single backedge, if not return false.
240 if (CurLoop
->getNumBackEdges() != 1) {
241 LLVM_DEBUG(dbgs() << " loop has multiple backedges\n");
244 // Loop must have a single exiting block, if not return false.
245 if (!CurLoop
->getExitingBlock()) {
246 LLVM_DEBUG(dbgs() << " loop has multiple exiting block\n");
249 // We only handle bottom-tested loop, i.e. loop in which the condition is
250 // checked at the end of each iteration. With that we can assume that all
251 // instructions in the loop are executed the same number of times.
252 if (CurLoop
->getExitingBlock() != CurLoop
->getLoopLatch()) {
253 LLVM_DEBUG(dbgs() << " loop is not bottom tested\n");
256 // Parallel loops must not have aliasing loop-invariant memory accesses.
257 // Hence we don't need to version anything in this case.
258 if (CurLoop
->isAnnotatedParallel()) {
259 LLVM_DEBUG(dbgs() << " Parallel loop is not worth versioning\n");
262 // Loop depth more then LoopDepthThreshold are not allowed
263 if (CurLoop
->getLoopDepth() > LoopDepthThreshold
) {
264 LLVM_DEBUG(dbgs() << " loop depth is more then threshold\n");
267 // We need to be able to compute the loop trip count in order
268 // to generate the bound checks.
269 const SCEV
*ExitCount
= SE
->getBackedgeTakenCount(CurLoop
);
270 if (isa
<SCEVCouldNotCompute
>(ExitCount
)) {
271 LLVM_DEBUG(dbgs() << " loop does not has trip count\n");
277 /// Check memory accesses in loop and confirms it's good for
278 /// LoopVersioningLICM.
279 bool LoopVersioningLICM::legalLoopMemoryAccesses() {
280 bool HasMayAlias
= false;
281 bool TypeSafety
= false;
284 // Transform phase will generate a versioned loop and also a runtime check to
285 // ensure the pointers are independent and they don’t alias.
286 // In version variant of loop, alias meta data asserts that all access are
287 // mutually independent.
289 // Pointers aliasing in alias domain are avoided because with multiple
290 // aliasing domains we may not be able to hoist potential loop invariant
291 // access out of the loop.
293 // Iterate over alias tracker sets, and confirm AliasSets doesn't have any
295 for (const auto &I
: *CurAST
) {
296 const AliasSet
&AS
= I
;
297 // Skip Forward Alias Sets, as this should be ignored as part of
298 // the AliasSetTracker object.
299 if (AS
.isForwardingAliasSet())
301 // With MustAlias its not worth adding runtime bound check.
302 if (AS
.isMustAlias())
304 Value
*SomePtr
= AS
.begin()->getValue();
305 bool TypeCheck
= true;
306 // Check for Mod & MayAlias
307 HasMayAlias
|= AS
.isMayAlias();
308 HasMod
|= AS
.isMod();
309 for (const auto &A
: AS
) {
310 Value
*Ptr
= A
.getValue();
311 // Alias tracker should have pointers of same data type.
312 TypeCheck
= (TypeCheck
&& (SomePtr
->getType() == Ptr
->getType()));
314 // At least one alias tracker should have pointers of same data type.
315 TypeSafety
|= TypeCheck
;
317 // Ensure types should be of same type.
319 LLVM_DEBUG(dbgs() << " Alias tracker type safety failed!\n");
322 // Ensure loop body shouldn't be read only.
324 LLVM_DEBUG(dbgs() << " No memory modified in loop body\n");
327 // Make sure alias set has may alias case.
328 // If there no alias memory ambiguity, return false.
330 LLVM_DEBUG(dbgs() << " No ambiguity in memory access.\n");
336 /// Check loop instructions safe for Loop versioning.
337 /// It returns true if it's safe else returns false.
338 /// Consider following:
339 /// 1) Check all load store in loop body are non atomic & non volatile.
340 /// 2) Check function call safety, by ensuring its not accessing memory.
341 /// 3) Loop body shouldn't have any may throw instruction.
342 /// 4) Loop body shouldn't have any convergent or noduplicate instructions.
343 bool LoopVersioningLICM::instructionSafeForVersioning(Instruction
*I
) {
344 assert(I
!= nullptr && "Null instruction found!");
345 // Check function call safety
346 if (auto *Call
= dyn_cast
<CallBase
>(I
)) {
347 if (Call
->isConvergent() || Call
->cannotDuplicate()) {
348 LLVM_DEBUG(dbgs() << " Convergent call site found.\n");
352 if (!AA
->doesNotAccessMemory(Call
)) {
353 LLVM_DEBUG(dbgs() << " Unsafe call site found.\n");
358 // Avoid loops with possiblity of throw
360 LLVM_DEBUG(dbgs() << " May throw instruction found in loop body\n");
363 // If current instruction is load instructions
364 // make sure it's a simple load (non atomic & non volatile)
365 if (I
->mayReadFromMemory()) {
366 LoadInst
*Ld
= dyn_cast
<LoadInst
>(I
);
367 if (!Ld
|| !Ld
->isSimple()) {
368 LLVM_DEBUG(dbgs() << " Found a non-simple load.\n");
371 LoadAndStoreCounter
++;
372 Value
*Ptr
= Ld
->getPointerOperand();
373 // Check loop invariant.
374 if (SE
->isLoopInvariant(SE
->getSCEV(Ptr
), CurLoop
))
377 // If current instruction is store instruction
378 // make sure it's a simple store (non atomic & non volatile)
379 else if (I
->mayWriteToMemory()) {
380 StoreInst
*St
= dyn_cast
<StoreInst
>(I
);
381 if (!St
|| !St
->isSimple()) {
382 LLVM_DEBUG(dbgs() << " Found a non-simple store.\n");
385 LoadAndStoreCounter
++;
386 Value
*Ptr
= St
->getPointerOperand();
387 // Check loop invariant.
388 if (SE
->isLoopInvariant(SE
->getSCEV(Ptr
), CurLoop
))
391 IsReadOnlyLoop
= false;
396 /// Check loop instructions and confirms it's good for
397 /// LoopVersioningLICM.
398 bool LoopVersioningLICM::legalLoopInstructions() {
399 // Resetting counters.
400 LoadAndStoreCounter
= 0;
401 InvariantCounter
= 0;
402 IsReadOnlyLoop
= true;
404 // Iterate over loop blocks and instructions of each block and check
405 // instruction safety.
406 for (auto *Block
: CurLoop
->getBlocks())
407 for (auto &Inst
: *Block
) {
408 // If instruction is unsafe just return false.
409 if (!instructionSafeForVersioning(&Inst
)) {
411 return OptimizationRemarkMissed(DEBUG_TYPE
, "IllegalLoopInst", &Inst
)
412 << " Unsafe Loop Instruction";
417 // Get LoopAccessInfo from current loop via the proxy.
418 LAI
= &GetLAI(CurLoop
);
419 // Check LoopAccessInfo for need of runtime check.
420 if (LAI
->getRuntimePointerChecking()->getChecks().empty()) {
421 LLVM_DEBUG(dbgs() << " LAA: Runtime check not found !!\n");
424 // Number of runtime-checks should be less then RuntimeMemoryCheckThreshold
425 if (LAI
->getNumRuntimePointerChecks() >
426 VectorizerParams::RuntimeMemoryCheckThreshold
) {
428 dbgs() << " LAA: Runtime checks are more than threshold !!\n");
430 return OptimizationRemarkMissed(DEBUG_TYPE
, "RuntimeCheck",
431 CurLoop
->getStartLoc(),
432 CurLoop
->getHeader())
433 << "Number of runtime checks "
434 << NV("RuntimeChecks", LAI
->getNumRuntimePointerChecks())
435 << " exceeds threshold "
436 << NV("Threshold", VectorizerParams::RuntimeMemoryCheckThreshold
);
440 // Loop should have at least one invariant load or store instruction.
441 if (!InvariantCounter
) {
442 LLVM_DEBUG(dbgs() << " Invariant not found !!\n");
445 // Read only loop not allowed.
446 if (IsReadOnlyLoop
) {
447 LLVM_DEBUG(dbgs() << " Found a read-only loop!\n");
450 // Profitablity check:
451 // Check invariant threshold, should be in limit.
452 if (InvariantCounter
* 100 < InvariantThreshold
* LoadAndStoreCounter
) {
455 << " Invariant load & store are less then defined threshold\n");
456 LLVM_DEBUG(dbgs() << " Invariant loads & stores: "
457 << ((InvariantCounter
* 100) / LoadAndStoreCounter
)
459 LLVM_DEBUG(dbgs() << " Invariant loads & store threshold: "
460 << InvariantThreshold
<< "%\n");
462 return OptimizationRemarkMissed(DEBUG_TYPE
, "InvariantThreshold",
463 CurLoop
->getStartLoc(),
464 CurLoop
->getHeader())
465 << "Invariant load & store "
466 << NV("LoadAndStoreCounter",
467 ((InvariantCounter
* 100) / LoadAndStoreCounter
))
468 << " are less then defined threshold "
469 << NV("Threshold", InvariantThreshold
);
476 /// It checks loop is already visited or not.
477 /// check loop meta data, if loop revisited return true
479 bool LoopVersioningLICM::isLoopAlreadyVisited() {
480 // Check LoopVersioningLICM metadata into loop
481 if (findStringMetadataForLoop(CurLoop
, LICMVersioningMetaData
)) {
487 /// Checks legality for LoopVersioningLICM by considering following:
488 /// a) loop structure legality b) loop instruction legality
489 /// c) loop memory access legality.
490 /// Return true if legal else returns false.
491 bool LoopVersioningLICM::isLegalForVersioning() {
493 LLVM_DEBUG(dbgs() << "Loop: " << *CurLoop
);
494 // Make sure not re-visiting same loop again.
495 if (isLoopAlreadyVisited()) {
497 dbgs() << " Revisiting loop in LoopVersioningLICM not allowed.\n\n");
500 // Check loop structure leagality.
501 if (!legalLoopStructure()) {
503 dbgs() << " Loop structure not suitable for LoopVersioningLICM\n\n");
505 return OptimizationRemarkMissed(DEBUG_TYPE
, "IllegalLoopStruct",
506 CurLoop
->getStartLoc(),
507 CurLoop
->getHeader())
508 << " Unsafe Loop structure";
512 // Check loop instruction leagality.
513 if (!legalLoopInstructions()) {
516 << " Loop instructions not suitable for LoopVersioningLICM\n\n");
519 // Check loop memory access leagality.
520 if (!legalLoopMemoryAccesses()) {
523 << " Loop memory access not suitable for LoopVersioningLICM\n\n");
525 return OptimizationRemarkMissed(DEBUG_TYPE
, "IllegalLoopMemoryAccess",
526 CurLoop
->getStartLoc(),
527 CurLoop
->getHeader())
528 << " Unsafe Loop memory access";
532 // Loop versioning is feasible, return true.
533 LLVM_DEBUG(dbgs() << " Loop Versioning found to be beneficial\n\n");
535 return OptimizationRemark(DEBUG_TYPE
, "IsLegalForVersioning",
536 CurLoop
->getStartLoc(), CurLoop
->getHeader())
537 << " Versioned loop for LICM."
538 << " Number of runtime checks we had to insert "
539 << NV("RuntimeChecks", LAI
->getNumRuntimePointerChecks());
544 /// Update loop with aggressive aliasing assumptions.
545 /// It marks no-alias to any pairs of memory operations by assuming
546 /// loop should not have any must-alias memory accesses pairs.
547 /// During LoopVersioningLICM legality we ignore loops having must
548 /// aliasing memory accesses.
549 void LoopVersioningLICM::setNoAliasToLoop(Loop
*VerLoop
) {
550 // Get latch terminator instruction.
551 Instruction
*I
= VerLoop
->getLoopLatch()->getTerminator();
552 // Create alias scope domain.
553 MDBuilder
MDB(I
->getContext());
554 MDNode
*NewDomain
= MDB
.createAnonymousAliasScopeDomain("LVDomain");
555 StringRef Name
= "LVAliasScope";
556 MDNode
*NewScope
= MDB
.createAnonymousAliasScope(NewDomain
, Name
);
557 SmallVector
<Metadata
*, 4> Scopes
{NewScope
}, NoAliases
{NewScope
};
558 // Iterate over each instruction of loop.
559 // set no-alias for all load & store instructions.
560 for (auto *Block
: CurLoop
->getBlocks()) {
561 for (auto &Inst
: *Block
) {
562 // Only interested in instruction that may modify or read memory.
563 if (!Inst
.mayReadFromMemory() && !Inst
.mayWriteToMemory())
565 // Set no-alias for current instruction.
567 LLVMContext::MD_noalias
,
568 MDNode::concatenate(Inst
.getMetadata(LLVMContext::MD_noalias
),
569 MDNode::get(Inst
.getContext(), NoAliases
)));
570 // set alias-scope for current instruction.
572 LLVMContext::MD_alias_scope
,
573 MDNode::concatenate(Inst
.getMetadata(LLVMContext::MD_alias_scope
),
574 MDNode::get(Inst
.getContext(), Scopes
)));
579 bool LoopVersioningLICMLegacyPass::runOnLoop(Loop
*L
, LPPassManager
&LPM
) {
583 AliasAnalysis
*AA
= &getAnalysis
<AAResultsWrapperPass
>().getAAResults();
584 ScalarEvolution
*SE
= &getAnalysis
<ScalarEvolutionWrapperPass
>().getSE();
585 OptimizationRemarkEmitter
*ORE
=
586 &getAnalysis
<OptimizationRemarkEmitterWrapperPass
>().getORE();
587 LoopInfo
*LI
= &getAnalysis
<LoopInfoWrapperPass
>().getLoopInfo();
588 DominatorTree
*DT
= &getAnalysis
<DominatorTreeWrapperPass
>().getDomTree();
590 auto GetLAI
= [&](Loop
*L
) -> const LoopAccessInfo
& {
591 return getAnalysis
<LoopAccessLegacyAnalysis
>().getInfo(L
);
594 return LoopVersioningLICM(AA
, SE
, ORE
, GetLAI
).runOnLoop(L
, LI
, DT
);
597 bool LoopVersioningLICM::runOnLoop(Loop
*L
, LoopInfo
*LI
, DominatorTree
*DT
) {
598 // This will automatically release all resources hold by the current
599 // LoopVersioningLICM object.
600 AutoResetter
Resetter(*this);
602 // Do not do the transformation if disabled by metadata.
603 if (hasLICMVersioningTransformation(L
) & TM_Disable
)
608 CurAST
.reset(new AliasSetTracker(*AA
));
610 // Loop over the body of this loop, construct AST.
611 for (auto *Block
: L
->getBlocks()) {
612 if (LI
->getLoopFor(Block
) == L
) // Ignore blocks in subloop.
613 CurAST
->add(*Block
); // Incorporate the specified basic block
616 bool Changed
= false;
618 // Check feasiblity of LoopVersioningLICM.
619 // If versioning found to be feasible and beneficial then proceed
620 // else simply return, by cleaning up memory.
621 if (isLegalForVersioning()) {
622 // Do loop versioning.
623 // Create memcheck for memory accessed inside loop.
624 // Clone original loop, and set blocks properly.
625 LoopVersioning
LVer(*LAI
, LAI
->getRuntimePointerChecking()->getChecks(),
626 CurLoop
, LI
, DT
, SE
);
628 // Set Loop Versioning metaData for original loop.
629 addStringMetadataToLoop(LVer
.getNonVersionedLoop(), LICMVersioningMetaData
);
630 // Set Loop Versioning metaData for version loop.
631 addStringMetadataToLoop(LVer
.getVersionedLoop(), LICMVersioningMetaData
);
632 // Set "llvm.mem.parallel_loop_access" metaData to versioned loop.
633 // FIXME: "llvm.mem.parallel_loop_access" annotates memory access
634 // instructions, not loops.
635 addStringMetadataToLoop(LVer
.getVersionedLoop(),
636 "llvm.mem.parallel_loop_access");
637 // Update version loop with aggressive aliasing assumption.
638 setNoAliasToLoop(LVer
.getVersionedLoop());
644 char LoopVersioningLICMLegacyPass::ID
= 0;
646 INITIALIZE_PASS_BEGIN(LoopVersioningLICMLegacyPass
, "loop-versioning-licm",
647 "Loop Versioning For LICM", false, false)
648 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass
)
649 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass
)
650 INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass
)
651 INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass
)
652 INITIALIZE_PASS_DEPENDENCY(LoopAccessLegacyAnalysis
)
653 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass
)
654 INITIALIZE_PASS_DEPENDENCY(LoopSimplify
)
655 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass
)
656 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass
)
657 INITIALIZE_PASS_END(LoopVersioningLICMLegacyPass
, "loop-versioning-licm",
658 "Loop Versioning For LICM", false, false)
660 Pass
*llvm::createLoopVersioningLICMPass() {
661 return new LoopVersioningLICMLegacyPass();
666 PreservedAnalyses
LoopVersioningLICMPass::run(Loop
&L
, LoopAnalysisManager
&AM
,
667 LoopStandardAnalysisResults
&LAR
,
669 AliasAnalysis
*AA
= &LAR
.AA
;
670 ScalarEvolution
*SE
= &LAR
.SE
;
671 DominatorTree
*DT
= &LAR
.DT
;
672 LoopInfo
*LI
= &LAR
.LI
;
673 const Function
*F
= L
.getHeader()->getParent();
674 OptimizationRemarkEmitter
ORE(F
);
676 auto GetLAI
= [&](Loop
*L
) -> const LoopAccessInfo
& {
677 return AM
.getResult
<LoopAccessAnalysis
>(*L
, LAR
);
680 if (!LoopVersioningLICM(AA
, SE
, &ORE
, GetLAI
).runOnLoop(&L
, LI
, DT
))
681 return PreservedAnalyses::all();
682 return getLoopPassPreservedAnalyses();