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