Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:46

0001 //===- llvm/Pass.h - Base class for Passes ----------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines a base class that indicates that a specified class is a
0010 // transformation pass implementation.
0011 //
0012 // Passes are designed this way so that it is possible to run passes in a cache
0013 // and organizationally optimal order without having to specify it at the front
0014 // end.  This allows arbitrary passes to be strung together and have them
0015 // executed as efficiently as possible.
0016 //
0017 // Passes should extend one of the classes below, depending on the guarantees
0018 // that it can make about what will be modified as it is run.  For example, most
0019 // global optimizations should derive from FunctionPass, because they do not add
0020 // or delete functions, they operate on the internals of the function.
0021 //
0022 // Note that this file #includes PassSupport.h and PassAnalysisSupport.h (at the
0023 // bottom), so the APIs exposed by these files are also automatically available
0024 // to all users of this file.
0025 //
0026 //===----------------------------------------------------------------------===//
0027 
0028 #ifndef LLVM_PASS_H
0029 #define LLVM_PASS_H
0030 
0031 #ifdef EXPENSIVE_CHECKS
0032 #include <cstdint>
0033 #endif
0034 #include <string>
0035 
0036 namespace llvm {
0037 
0038 class AnalysisResolver;
0039 class AnalysisUsage;
0040 class Function;
0041 class ImmutablePass;
0042 class Module;
0043 class PassInfo;
0044 class PMDataManager;
0045 class PMStack;
0046 class raw_ostream;
0047 class StringRef;
0048 
0049 // AnalysisID - Use the PassInfo to identify a pass...
0050 using AnalysisID = const void *;
0051 
0052 /// Different types of internal pass managers. External pass managers
0053 /// (PassManager and FunctionPassManager) are not represented here.
0054 /// Ordering of pass manager types is important here.
0055 enum PassManagerType {
0056   PMT_Unknown = 0,
0057   PMT_ModulePassManager = 1, ///< MPPassManager
0058   PMT_CallGraphPassManager,  ///< CGPassManager
0059   PMT_FunctionPassManager,   ///< FPPassManager
0060   PMT_LoopPassManager,       ///< LPPassManager
0061   PMT_RegionPassManager,     ///< RGPassManager
0062   PMT_Last
0063 };
0064 
0065 // Different types of passes.
0066 enum PassKind {
0067   PT_Region,
0068   PT_Loop,
0069   PT_Function,
0070   PT_CallGraphSCC,
0071   PT_Module,
0072   PT_PassManager
0073 };
0074 
0075 /// This enumerates the LLVM full LTO or ThinLTO optimization phases.
0076 enum class ThinOrFullLTOPhase {
0077   /// No LTO/ThinLTO behavior needed.
0078   None,
0079   /// ThinLTO prelink (summary) phase.
0080   ThinLTOPreLink,
0081   /// ThinLTO postlink (backend compile) phase.
0082   ThinLTOPostLink,
0083   /// Full LTO prelink phase.
0084   FullLTOPreLink,
0085   /// Full LTO postlink (backend compile) phase.
0086   FullLTOPostLink
0087 };
0088 
0089 //===----------------------------------------------------------------------===//
0090 /// Pass interface - Implemented by all 'passes'.  Subclass this if you are an
0091 /// interprocedural optimization or you do not fit into any of the more
0092 /// constrained passes described below.
0093 ///
0094 class Pass {
0095   AnalysisResolver *Resolver = nullptr;  // Used to resolve analysis
0096   const void *PassID;
0097   PassKind Kind;
0098 
0099 public:
0100   explicit Pass(PassKind K, char &pid) : PassID(&pid), Kind(K) {}
0101   Pass(const Pass &) = delete;
0102   Pass &operator=(const Pass &) = delete;
0103   virtual ~Pass();
0104 
0105   PassKind getPassKind() const { return Kind; }
0106 
0107   /// getPassName - Return a nice clean name for a pass.  This usually
0108   /// implemented in terms of the name that is registered by one of the
0109   /// Registration templates, but can be overloaded directly.
0110   virtual StringRef getPassName() const;
0111 
0112   /// getPassID - Return the PassID number that corresponds to this pass.
0113   AnalysisID getPassID() const {
0114     return PassID;
0115   }
0116 
0117   /// doInitialization - Virtual method overridden by subclasses to do
0118   /// any necessary initialization before any pass is run.
0119   virtual bool doInitialization(Module &)  { return false; }
0120 
0121   /// doFinalization - Virtual method overriden by subclasses to do any
0122   /// necessary clean up after all passes have run.
0123   virtual bool doFinalization(Module &) { return false; }
0124 
0125   /// print - Print out the internal state of the pass.  This is called by
0126   /// Analyze to print out the contents of an analysis.  Otherwise it is not
0127   /// necessary to implement this method.  Beware that the module pointer MAY be
0128   /// null.  This automatically forwards to a virtual function that does not
0129   /// provide the Module* in case the analysis doesn't need it it can just be
0130   /// ignored.
0131   virtual void print(raw_ostream &OS, const Module *M) const;
0132 
0133   void dump() const; // dump - Print to stderr.
0134 
0135   /// createPrinterPass - Get a Pass appropriate to print the IR this
0136   /// pass operates on (Module, Function or MachineFunction).
0137   virtual Pass *createPrinterPass(raw_ostream &OS,
0138                                   const std::string &Banner) const = 0;
0139 
0140   /// Each pass is responsible for assigning a pass manager to itself.
0141   /// PMS is the stack of available pass manager.
0142   virtual void assignPassManager(PMStack &,
0143                                  PassManagerType) {}
0144 
0145   /// Check if available pass managers are suitable for this pass or not.
0146   virtual void preparePassManager(PMStack &);
0147 
0148   ///  Return what kind of Pass Manager can manage this pass.
0149   virtual PassManagerType getPotentialPassManagerType() const;
0150 
0151   // Access AnalysisResolver
0152   void setResolver(AnalysisResolver *AR);
0153   AnalysisResolver *getResolver() const { return Resolver; }
0154 
0155   /// getAnalysisUsage - This function should be overriden by passes that need
0156   /// analysis information to do their job.  If a pass specifies that it uses a
0157   /// particular analysis result to this function, it can then use the
0158   /// getAnalysis<AnalysisType>() function, below.
0159   virtual void getAnalysisUsage(AnalysisUsage &) const;
0160 
0161   /// releaseMemory() - This member can be implemented by a pass if it wants to
0162   /// be able to release its memory when it is no longer needed.  The default
0163   /// behavior of passes is to hold onto memory for the entire duration of their
0164   /// lifetime (which is the entire compile time).  For pipelined passes, this
0165   /// is not a big deal because that memory gets recycled every time the pass is
0166   /// invoked on another program unit.  For IP passes, it is more important to
0167   /// free memory when it is unused.
0168   ///
0169   /// Optionally implement this function to release pass memory when it is no
0170   /// longer used.
0171   virtual void releaseMemory();
0172 
0173   /// getAdjustedAnalysisPointer - This method is used when a pass implements
0174   /// an analysis interface through multiple inheritance.  If needed, it should
0175   /// override this to adjust the this pointer as needed for the specified pass
0176   /// info.
0177   virtual void *getAdjustedAnalysisPointer(AnalysisID ID);
0178   virtual ImmutablePass *getAsImmutablePass();
0179   virtual PMDataManager *getAsPMDataManager();
0180 
0181   /// verifyAnalysis() - This member can be implemented by a analysis pass to
0182   /// check state of analysis information.
0183   virtual void verifyAnalysis() const;
0184 
0185   // dumpPassStructure - Implement the -debug-passes=PassStructure option
0186   virtual void dumpPassStructure(unsigned Offset = 0);
0187 
0188   // lookupPassInfo - Return the pass info object for the specified pass class,
0189   // or null if it is not known.
0190   static const PassInfo *lookupPassInfo(const void *TI);
0191 
0192   // lookupPassInfo - Return the pass info object for the pass with the given
0193   // argument string, or null if it is not known.
0194   static const PassInfo *lookupPassInfo(StringRef Arg);
0195 
0196   // createPass - Create a object for the specified pass class,
0197   // or null if it is not known.
0198   static Pass *createPass(AnalysisID ID);
0199 
0200   /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
0201   /// get analysis information that might be around, for example to update it.
0202   /// This is different than getAnalysis in that it can fail (if the analysis
0203   /// results haven't been computed), so should only be used if you can handle
0204   /// the case when the analysis is not available.  This method is often used by
0205   /// transformation APIs to update analysis results for a pass automatically as
0206   /// the transform is performed.
0207   template<typename AnalysisType> AnalysisType *
0208     getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h
0209 
0210   /// mustPreserveAnalysisID - This method serves the same function as
0211   /// getAnalysisIfAvailable, but works if you just have an AnalysisID.  This
0212   /// obviously cannot give you a properly typed instance of the class if you
0213   /// don't have the class name available (use getAnalysisIfAvailable if you
0214   /// do), but it can tell you if you need to preserve the pass at least.
0215   bool mustPreserveAnalysisID(char &AID) const;
0216 
0217   /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
0218   /// to the analysis information that they claim to use by overriding the
0219   /// getAnalysisUsage function.
0220   template<typename AnalysisType>
0221   AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h
0222 
0223   template <typename AnalysisType>
0224   AnalysisType &
0225   getAnalysis(Function &F,
0226               bool *Changed = nullptr); // Defined in PassAnalysisSupport.h
0227 
0228   template<typename AnalysisType>
0229   AnalysisType &getAnalysisID(AnalysisID PI) const;
0230 
0231   template <typename AnalysisType>
0232   AnalysisType &getAnalysisID(AnalysisID PI, Function &F,
0233                               bool *Changed = nullptr);
0234 
0235 #ifdef EXPENSIVE_CHECKS
0236   /// Hash a module in order to detect when a module (or more specific) pass has
0237   /// modified it.
0238   uint64_t structuralHash(Module &M) const;
0239 
0240   /// Hash a function in order to detect when a function (or more specific) pass
0241   /// has modified it.
0242   virtual uint64_t structuralHash(Function &F) const;
0243 #endif
0244 };
0245 
0246 //===----------------------------------------------------------------------===//
0247 /// ModulePass class - This class is used to implement unstructured
0248 /// interprocedural optimizations and analyses.  ModulePasses may do anything
0249 /// they want to the program.
0250 ///
0251 class ModulePass : public Pass {
0252 public:
0253   explicit ModulePass(char &pid) : Pass(PT_Module, pid) {}
0254 
0255   // Force out-of-line virtual method.
0256   ~ModulePass() override;
0257 
0258   /// createPrinterPass - Get a module printer pass.
0259   Pass *createPrinterPass(raw_ostream &OS,
0260                           const std::string &Banner) const override;
0261 
0262   /// runOnModule - Virtual method overriden by subclasses to process the module
0263   /// being operated on.
0264   virtual bool runOnModule(Module &M) = 0;
0265 
0266   void assignPassManager(PMStack &PMS, PassManagerType T) override;
0267 
0268   ///  Return what kind of Pass Manager can manage this pass.
0269   PassManagerType getPotentialPassManagerType() const override;
0270 
0271 protected:
0272   /// Optional passes call this function to check whether the pass should be
0273   /// skipped. This is the case when optimization bisect is over the limit.
0274   bool skipModule(Module &M) const;
0275 };
0276 
0277 //===----------------------------------------------------------------------===//
0278 /// ImmutablePass class - This class is used to provide information that does
0279 /// not need to be run.  This is useful for things like target information.
0280 ///
0281 class ImmutablePass : public ModulePass {
0282 public:
0283   explicit ImmutablePass(char &pid) : ModulePass(pid) {}
0284 
0285   // Force out-of-line virtual method.
0286   ~ImmutablePass() override;
0287 
0288   /// initializePass - This method may be overriden by immutable passes to allow
0289   /// them to perform various initialization actions they require.  This is
0290   /// primarily because an ImmutablePass can "require" another ImmutablePass,
0291   /// and if it does, the overloaded version of initializePass may get access to
0292   /// these passes with getAnalysis<>.
0293   virtual void initializePass();
0294 
0295   ImmutablePass *getAsImmutablePass() override { return this; }
0296 
0297   /// ImmutablePasses are never run.
0298   bool runOnModule(Module &) override { return false; }
0299 };
0300 
0301 //===----------------------------------------------------------------------===//
0302 /// FunctionPass class - This class is used to implement most global
0303 /// optimizations.  Optimizations should subclass this class if they meet the
0304 /// following constraints:
0305 ///
0306 ///  1. Optimizations are organized globally, i.e., a function at a time
0307 ///  2. Optimizing a function does not cause the addition or removal of any
0308 ///     functions in the module
0309 ///
0310 class FunctionPass : public Pass {
0311 public:
0312   explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {}
0313 
0314   /// createPrinterPass - Get a function printer pass.
0315   Pass *createPrinterPass(raw_ostream &OS,
0316                           const std::string &Banner) const override;
0317 
0318   /// runOnFunction - Virtual method overriden by subclasses to do the
0319   /// per-function processing of the pass.
0320   virtual bool runOnFunction(Function &F) = 0;
0321 
0322   void assignPassManager(PMStack &PMS, PassManagerType T) override;
0323 
0324   ///  Return what kind of Pass Manager can manage this pass.
0325   PassManagerType getPotentialPassManagerType() const override;
0326 
0327 protected:
0328   /// Optional passes call this function to check whether the pass should be
0329   /// skipped. This is the case when Attribute::OptimizeNone is set or when
0330   /// optimization bisect is over the limit.
0331   bool skipFunction(const Function &F) const;
0332 };
0333 
0334 /// If the user specifies the -time-passes argument on an LLVM tool command line
0335 /// then the value of this boolean will be true, otherwise false.
0336 /// This is the storage for the -time-passes option.
0337 extern bool TimePassesIsEnabled;
0338 /// If TimePassesPerRun is true, there would be one line of report for
0339 /// each pass invocation.
0340 /// If TimePassesPerRun is false, there would be only one line of
0341 /// report for each pass (even there are more than one pass objects).
0342 /// (For new pass manager only)
0343 extern bool TimePassesPerRun;
0344 
0345 } // end namespace llvm
0346 
0347 // Include support files that contain important APIs commonly used by Passes,
0348 // but that we want to separate out to make it easier to read the header files.
0349 #include "llvm/PassAnalysisSupport.h"
0350 #include "llvm/PassSupport.h"
0351 
0352 #endif // LLVM_PASS_H