LLVM API Documentation

LoopPass.h
Go to the documentation of this file.
00001 //===- LoopPass.h - LoopPass class ----------------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines LoopPass class. All loop optimization
00011 // and transformation passes are derived from LoopPass.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_ANALYSIS_LOOPPASS_H
00016 #define LLVM_ANALYSIS_LOOPPASS_H
00017 
00018 #include "llvm/Analysis/LoopInfo.h"
00019 #include "llvm/IR/LegacyPassManagers.h"
00020 #include "llvm/Pass.h"
00021 #include <deque>
00022 
00023 namespace llvm {
00024 
00025 class LPPassManager;
00026 class Function;
00027 class PMStack;
00028 
00029 class LoopPass : public Pass {
00030 public:
00031   explicit LoopPass(char &pid) : Pass(PT_Loop, pid) {}
00032 
00033   /// getPrinterPass - Get a pass to print the function corresponding
00034   /// to a Loop.
00035   Pass *createPrinterPass(raw_ostream &O,
00036                           const std::string &Banner) const override;
00037 
00038   // runOnLoop - This method should be implemented by the subclass to perform
00039   // whatever action is necessary for the specified Loop.
00040   virtual bool runOnLoop(Loop *L, LPPassManager &LPM) = 0;
00041 
00042   using llvm::Pass::doInitialization;
00043   using llvm::Pass::doFinalization;
00044 
00045   // Initialization and finalization hooks.
00046   virtual bool doInitialization(Loop *L, LPPassManager &LPM) {
00047     return false;
00048   }
00049 
00050   // Finalization hook does not supply Loop because at this time
00051   // loop nest is completely different.
00052   virtual bool doFinalization() { return false; }
00053 
00054   // Check if this pass is suitable for the current LPPassManager, if
00055   // available. This pass P is not suitable for a LPPassManager if P
00056   // is not preserving higher level analysis info used by other
00057   // LPPassManager passes. In such case, pop LPPassManager from the
00058   // stack. This will force assignPassManager() to create new
00059   // LPPassManger as expected.
00060   void preparePassManager(PMStack &PMS) override;
00061 
00062   /// Assign pass manager to manage this pass
00063   void assignPassManager(PMStack &PMS, PassManagerType PMT) override;
00064 
00065   ///  Return what kind of Pass Manager can manage this pass.
00066   PassManagerType getPotentialPassManagerType() const override {
00067     return PMT_LoopPassManager;
00068   }
00069 
00070   //===--------------------------------------------------------------------===//
00071   /// SimpleAnalysis - Provides simple interface to update analysis info
00072   /// maintained by various passes. Note, if required this interface can
00073   /// be extracted into a separate abstract class but it would require
00074   /// additional use of multiple inheritance in Pass class hierarchy, something
00075   /// we are trying to avoid.
00076 
00077   /// Each loop pass can override these simple analysis hooks to update
00078   /// desired analysis information.
00079   /// cloneBasicBlockAnalysis - Clone analysis info associated with basic block.
00080   virtual void cloneBasicBlockAnalysis(BasicBlock *F, BasicBlock *T, Loop *L) {}
00081 
00082   /// deleteAnalysisValue - Delete analysis info associated with value V.
00083   virtual void deleteAnalysisValue(Value *V, Loop *L) {}
00084 
00085 protected:
00086   /// skipOptnoneFunction - Containing function has Attribute::OptimizeNone
00087   /// and most transformation passes should skip it.
00088   bool skipOptnoneFunction(const Loop *L) const;
00089 };
00090 
00091 class LPPassManager : public FunctionPass, public PMDataManager {
00092 public:
00093   static char ID;
00094   explicit LPPassManager();
00095 
00096   /// run - Execute all of the passes scheduled for execution.  Keep track of
00097   /// whether any of the passes modifies the module, and if so, return true.
00098   bool runOnFunction(Function &F) override;
00099 
00100   /// Pass Manager itself does not invalidate any analysis info.
00101   // LPPassManager needs LoopInfo.
00102   void getAnalysisUsage(AnalysisUsage &Info) const override;
00103 
00104   const char *getPassName() const override {
00105     return "Loop Pass Manager";
00106   }
00107 
00108   PMDataManager *getAsPMDataManager() override { return this; }
00109   Pass *getAsPass() override { return this; }
00110 
00111   /// Print passes managed by this manager
00112   void dumpPassStructure(unsigned Offset) override;
00113 
00114   LoopPass *getContainedPass(unsigned N) {
00115     assert(N < PassVector.size() && "Pass number out of range!");
00116     LoopPass *LP = static_cast<LoopPass *>(PassVector[N]);
00117     return LP;
00118   }
00119 
00120   PassManagerType getPassManagerType() const override {
00121     return PMT_LoopPassManager;
00122   }
00123 
00124 public:
00125   // Delete loop from the loop queue and loop nest (LoopInfo).
00126   void deleteLoopFromQueue(Loop *L);
00127 
00128   // Insert loop into the loop queue and add it as a child of the
00129   // given parent.
00130   void insertLoop(Loop *L, Loop *ParentLoop);
00131 
00132   // Insert a loop into the loop queue.
00133   void insertLoopIntoQueue(Loop *L);
00134 
00135   // Reoptimize this loop. LPPassManager will re-insert this loop into the
00136   // queue. This allows LoopPass to change loop nest for the loop. This
00137   // utility may send LPPassManager into infinite loops so use caution.
00138   void redoLoop(Loop *L);
00139 
00140   //===--------------------------------------------------------------------===//
00141   /// SimpleAnalysis - Provides simple interface to update analysis info
00142   /// maintained by various passes. Note, if required this interface can
00143   /// be extracted into a separate abstract class but it would require
00144   /// additional use of multiple inheritance in Pass class hierarchy, something
00145   /// we are trying to avoid.
00146 
00147   /// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for
00148   /// all passes that implement simple analysis interface.
00149   void cloneBasicBlockSimpleAnalysis(BasicBlock *From, BasicBlock *To, Loop *L);
00150 
00151   /// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes
00152   /// that implement simple analysis interface.
00153   void deleteSimpleAnalysisValue(Value *V, Loop *L);
00154 
00155 private:
00156   std::deque<Loop *> LQ;
00157   bool skipThisLoop;
00158   bool redoThisLoop;
00159   LoopInfo *LI;
00160   Loop *CurrentLoop;
00161 };
00162 
00163 } // End llvm namespace
00164 
00165 #endif