1 //===- LoopPass.h - LoopPass class ----------------------------------------===// 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 LoopPass class. All loop optimization 10 // and transformation passes are derived from LoopPass. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_ANALYSIS_LOOPPASS_H 15 #define LLVM_ANALYSIS_LOOPPASS_H 16 17 #include "llvm/IR/LegacyPassManagers.h" 18 #include "llvm/Pass.h" 19 #include "llvm/Support/Compiler.h" 20 #include <deque> 21 22 namespace llvm { 23 24 class Loop; 25 class LoopInfo; 26 class LPPassManager; 27 class Function; 28 29 class LLVM_ABI LoopPass : public Pass { 30 public: LoopPass(char & pid)31 explicit LoopPass(char &pid) : Pass(PT_Loop, pid) {} 32 33 /// getPrinterPass - Get a pass to print the function corresponding 34 /// to a Loop. 35 Pass *createPrinterPass(raw_ostream &O, 36 const std::string &Banner) const override; 37 38 // runOnLoop - This method should be implemented by the subclass to perform 39 // whatever action is necessary for the specified Loop. 40 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) = 0; 41 42 using llvm::Pass::doInitialization; 43 using llvm::Pass::doFinalization; 44 45 // Initialization and finalization hooks. doInitialization(Loop * L,LPPassManager & LPM)46 virtual bool doInitialization(Loop *L, LPPassManager &LPM) { 47 return false; 48 } 49 50 // Finalization hook does not supply Loop because at this time 51 // loop nest is completely different. doFinalization()52 virtual bool doFinalization() { return false; } 53 54 // Check if this pass is suitable for the current LPPassManager, if 55 // available. This pass P is not suitable for a LPPassManager if P 56 // is not preserving higher level analysis info used by other 57 // LPPassManager passes. In such case, pop LPPassManager from the 58 // stack. This will force assignPassManager() to create new 59 // LPPassManger as expected. 60 void preparePassManager(PMStack &PMS) override; 61 62 /// Assign pass manager to manage this pass 63 void assignPassManager(PMStack &PMS, PassManagerType PMT) override; 64 65 /// Return what kind of Pass Manager can manage this pass. getPotentialPassManagerType()66 PassManagerType getPotentialPassManagerType() const override { 67 return PMT_LoopPassManager; 68 } 69 70 protected: 71 /// Optional passes call this function to check whether the pass should be 72 /// skipped. This is the case when Attribute::OptimizeNone is set or when 73 /// optimization bisect is over the limit. 74 bool skipLoop(const Loop *L) const; 75 }; 76 77 class LLVM_ABI LPPassManager : public FunctionPass, public PMDataManager { 78 public: 79 static char ID; 80 explicit LPPassManager(); 81 82 /// run - Execute all of the passes scheduled for execution. Keep track of 83 /// whether any of the passes modifies the module, and if so, return true. 84 bool runOnFunction(Function &F) override; 85 86 /// Pass Manager itself does not invalidate any analysis info. 87 // LPPassManager needs LoopInfo. 88 void getAnalysisUsage(AnalysisUsage &Info) const override; 89 getPassName()90 StringRef getPassName() const override { return "Loop Pass Manager"; } 91 getAsPMDataManager()92 PMDataManager *getAsPMDataManager() override { return this; } getAsPass()93 Pass *getAsPass() override { return this; } 94 95 /// Print passes managed by this manager 96 void dumpPassStructure(unsigned Offset) override; 97 getContainedPass(unsigned N)98 LoopPass *getContainedPass(unsigned N) { 99 assert(N < PassVector.size() && "Pass number out of range!"); 100 LoopPass *LP = static_cast<LoopPass *>(PassVector[N]); 101 return LP; 102 } 103 getPassManagerType()104 PassManagerType getPassManagerType() const override { 105 return PMT_LoopPassManager; 106 } 107 108 public: 109 // Add a new loop into the loop queue. 110 void addLoop(Loop &L); 111 112 // Mark \p L as deleted. 113 void markLoopAsDeleted(Loop &L); 114 115 private: 116 std::deque<Loop *> LQ; 117 LoopInfo *LI; 118 Loop *CurrentLoop; 119 bool CurrentLoopDeleted; 120 }; 121 122 // This pass is required by the LCSSA transformation. It is used inside 123 // LPPassManager to check if current pass preserves LCSSA form, and if it does 124 // pass manager calls lcssa verification for the current loop. 125 struct LCSSAVerificationPass : public FunctionPass { 126 LLVM_ABI static char ID; 127 LLVM_ABI LCSSAVerificationPass(); 128 runOnFunctionLCSSAVerificationPass129 bool runOnFunction(Function &F) override { return false; } 130 getAnalysisUsageLCSSAVerificationPass131 void getAnalysisUsage(AnalysisUsage &AU) const override { 132 AU.setPreservesAll(); 133 } 134 }; 135 136 } // End llvm namespace 137 138 #endif 139