1 //===- LoopPass.h - LoopPass class ----------------------------------------===//
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 // This file defines LoopPass class. All loop optimization
10 // and transformation passes are derived from LoopPass.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_LOOPPASS_H
15 #define LLVM_ANALYSIS_LOOPPASS_H
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/IR/LegacyPassManagers.h"
19 #include "llvm/Pass.h"
28 class LoopPass
: public Pass
{
30 explicit LoopPass(char &pid
) : Pass(PT_Loop
, pid
) {}
32 /// getPrinterPass - Get a pass to print the function corresponding
34 Pass
*createPrinterPass(raw_ostream
&O
,
35 const std::string
&Banner
) const override
;
37 // runOnLoop - This method should be implemented by the subclass to perform
38 // whatever action is necessary for the specified Loop.
39 virtual bool runOnLoop(Loop
*L
, LPPassManager
&LPM
) = 0;
41 using llvm::Pass::doInitialization
;
42 using llvm::Pass::doFinalization
;
44 // Initialization and finalization hooks.
45 virtual bool doInitialization(Loop
*L
, LPPassManager
&LPM
) {
49 // Finalization hook does not supply Loop because at this time
50 // loop nest is completely different.
51 virtual bool doFinalization() { return false; }
53 // Check if this pass is suitable for the current LPPassManager, if
54 // available. This pass P is not suitable for a LPPassManager if P
55 // is not preserving higher level analysis info used by other
56 // LPPassManager passes. In such case, pop LPPassManager from the
57 // stack. This will force assignPassManager() to create new
58 // LPPassManger as expected.
59 void preparePassManager(PMStack
&PMS
) override
;
61 /// Assign pass manager to manage this pass
62 void assignPassManager(PMStack
&PMS
, PassManagerType PMT
) override
;
64 /// Return what kind of Pass Manager can manage this pass.
65 PassManagerType
getPotentialPassManagerType() const override
{
66 return PMT_LoopPassManager
;
69 //===--------------------------------------------------------------------===//
70 /// SimpleAnalysis - Provides simple interface to update analysis info
71 /// maintained by various passes. Note, if required this interface can
72 /// be extracted into a separate abstract class but it would require
73 /// additional use of multiple inheritance in Pass class hierarchy, something
74 /// we are trying to avoid.
76 /// Each loop pass can override these simple analysis hooks to update
77 /// desired analysis information.
78 /// cloneBasicBlockAnalysis - Clone analysis info associated with basic block.
79 virtual void cloneBasicBlockAnalysis(BasicBlock
*F
, BasicBlock
*T
, Loop
*L
) {}
81 /// deleteAnalysisValue - Delete analysis info associated with value V.
82 virtual void deleteAnalysisValue(Value
*V
, Loop
*L
) {}
84 /// Delete analysis info associated with Loop L.
85 /// Called to notify a Pass that a loop has been deleted and any
86 /// associated analysis values can be deleted.
87 virtual void deleteAnalysisLoop(Loop
*L
) {}
90 /// Optional passes call this function to check whether the pass should be
91 /// skipped. This is the case when Attribute::OptimizeNone is set or when
92 /// optimization bisect is over the limit.
93 bool skipLoop(const Loop
*L
) const;
96 class LPPassManager
: public FunctionPass
, public PMDataManager
{
99 explicit LPPassManager();
101 /// run - Execute all of the passes scheduled for execution. Keep track of
102 /// whether any of the passes modifies the module, and if so, return true.
103 bool runOnFunction(Function
&F
) override
;
105 /// Pass Manager itself does not invalidate any analysis info.
106 // LPPassManager needs LoopInfo.
107 void getAnalysisUsage(AnalysisUsage
&Info
) const override
;
109 StringRef
getPassName() const override
{ return "Loop Pass Manager"; }
111 PMDataManager
*getAsPMDataManager() override
{ return this; }
112 Pass
*getAsPass() override
{ return this; }
114 /// Print passes managed by this manager
115 void dumpPassStructure(unsigned Offset
) override
;
117 LoopPass
*getContainedPass(unsigned N
) {
118 assert(N
< PassVector
.size() && "Pass number out of range!");
119 LoopPass
*LP
= static_cast<LoopPass
*>(PassVector
[N
]);
123 PassManagerType
getPassManagerType() const override
{
124 return PMT_LoopPassManager
;
128 // Add a new loop into the loop queue.
129 void addLoop(Loop
&L
);
131 // Mark \p L as deleted.
132 void markLoopAsDeleted(Loop
&L
);
134 //===--------------------------------------------------------------------===//
135 /// SimpleAnalysis - Provides simple interface to update analysis info
136 /// maintained by various passes. Note, if required this interface can
137 /// be extracted into a separate abstract class but it would require
138 /// additional use of multiple inheritance in Pass class hierarchy, something
139 /// we are trying to avoid.
141 /// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for
142 /// all passes that implement simple analysis interface.
143 void cloneBasicBlockSimpleAnalysis(BasicBlock
*From
, BasicBlock
*To
, Loop
*L
);
145 /// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes
146 /// that implement simple analysis interface.
147 void deleteSimpleAnalysisValue(Value
*V
, Loop
*L
);
149 /// Invoke deleteAnalysisLoop hook for all passes that implement simple
150 /// analysis interface.
151 void deleteSimpleAnalysisLoop(Loop
*L
);
154 std::deque
<Loop
*> LQ
;
157 bool CurrentLoopDeleted
;
160 // This pass is required by the LCSSA transformation. It is used inside
161 // LPPassManager to check if current pass preserves LCSSA form, and if it does
162 // pass manager calls lcssa verification for the current loop.
163 struct LCSSAVerificationPass
: public FunctionPass
{
165 LCSSAVerificationPass() : FunctionPass(ID
) {
166 initializeLCSSAVerificationPassPass(*PassRegistry::getPassRegistry());
169 bool runOnFunction(Function
&F
) override
{ return false; }
171 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
172 AU
.setPreservesAll();
176 } // End llvm namespace