Home | History | Annotate | Download | only in llvm
      1 //===- llvm/PassInfo.h - Pass Info class ------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file defines and implements the PassInfo class.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 #ifndef LLVM_PASSINFO_H
     14 #define LLVM_PASSINFO_H
     15 
     16 #include <cassert>
     17 #include <vector>
     18 
     19 namespace llvm {
     20 
     21 class Pass;
     22 class TargetMachine;
     23 
     24 //===---------------------------------------------------------------------------
     25 /// PassInfo class - An instance of this class exists for every pass known by
     26 /// the system, and can be obtained from a live Pass by calling its
     27 /// getPassInfo() method.  These objects are set up by the RegisterPass<>
     28 /// template.
     29 ///
     30 class PassInfo {
     31 public:
     32   typedef Pass* (*NormalCtor_t)();
     33   typedef Pass *(*TargetMachineCtor_t)(TargetMachine *);
     34 
     35 private:
     36   const char      *const PassName;     // Nice name for Pass
     37   const char      *const PassArgument; // Command Line argument to run this pass
     38   const void *PassID;
     39   const bool IsCFGOnlyPass;            // Pass only looks at the CFG.
     40   const bool IsAnalysis;               // True if an analysis pass.
     41   const bool IsAnalysisGroup;          // True if an analysis group.
     42   std::vector<const PassInfo*> ItfImpl;// Interfaces implemented by this pass
     43 
     44   NormalCtor_t NormalCtor;
     45   TargetMachineCtor_t TargetMachineCtor;
     46 
     47 public:
     48   /// PassInfo ctor - Do not call this directly, this should only be invoked
     49   /// through RegisterPass.
     50   PassInfo(const char *name, const char *arg, const void *pi,
     51            NormalCtor_t normal, bool isCFGOnly, bool is_analysis,
     52            TargetMachineCtor_t machine = nullptr)
     53     : PassName(name), PassArgument(arg), PassID(pi),
     54       IsCFGOnlyPass(isCFGOnly),
     55       IsAnalysis(is_analysis), IsAnalysisGroup(false), NormalCtor(normal),
     56       TargetMachineCtor(machine) {}
     57   /// PassInfo ctor - Do not call this directly, this should only be invoked
     58   /// through RegisterPass. This version is for use by analysis groups; it
     59   /// does not auto-register the pass.
     60   PassInfo(const char *name, const void *pi)
     61     : PassName(name), PassArgument(""), PassID(pi),
     62       IsCFGOnlyPass(false),
     63       IsAnalysis(false), IsAnalysisGroup(true), NormalCtor(nullptr),
     64       TargetMachineCtor(nullptr) {}
     65 
     66   /// getPassName - Return the friendly name for the pass, never returns null
     67   ///
     68   const char *getPassName() const { return PassName; }
     69 
     70   /// getPassArgument - Return the command line option that may be passed to
     71   /// 'opt' that will cause this pass to be run.  This will return null if there
     72   /// is no argument.
     73   ///
     74   const char *getPassArgument() const { return PassArgument; }
     75 
     76   /// getTypeInfo - Return the id object for the pass...
     77   /// TODO : Rename
     78   const void *getTypeInfo() const { return PassID; }
     79 
     80   /// Return true if this PassID implements the specified ID pointer.
     81   bool isPassID(const void *IDPtr) const {
     82     return PassID == IDPtr;
     83   }
     84 
     85   /// isAnalysisGroup - Return true if this is an analysis group, not a normal
     86   /// pass.
     87   ///
     88   bool isAnalysisGroup() const { return IsAnalysisGroup; }
     89   bool isAnalysis() const { return IsAnalysis; }
     90 
     91   /// isCFGOnlyPass - return true if this pass only looks at the CFG for the
     92   /// function.
     93   bool isCFGOnlyPass() const { return IsCFGOnlyPass; }
     94 
     95   /// getNormalCtor - Return a pointer to a function, that when called, creates
     96   /// an instance of the pass and returns it.  This pointer may be null if there
     97   /// is no default constructor for the pass.
     98   ///
     99   NormalCtor_t getNormalCtor() const {
    100     return NormalCtor;
    101   }
    102   void setNormalCtor(NormalCtor_t Ctor) {
    103     NormalCtor = Ctor;
    104   }
    105 
    106   /// getTargetMachineCtor - Return a pointer to a function, that when called
    107   /// with a TargetMachine, creates an instance of the pass and returns it.
    108   /// This pointer may be null if there is no constructor with a TargetMachine
    109   /// for the pass.
    110   ///
    111   TargetMachineCtor_t getTargetMachineCtor() const { return TargetMachineCtor; }
    112   void setTargetMachineCtor(TargetMachineCtor_t Ctor) {
    113     TargetMachineCtor = Ctor;
    114   }
    115 
    116   /// createPass() - Use this method to create an instance of this pass.
    117   Pass *createPass() const {
    118     assert((!isAnalysisGroup() || NormalCtor) &&
    119            "No default implementation found for analysis group!");
    120     assert(NormalCtor &&
    121            "Cannot call createPass on PassInfo without default ctor!");
    122     return NormalCtor();
    123   }
    124 
    125   /// addInterfaceImplemented - This method is called when this pass is
    126   /// registered as a member of an analysis group with the RegisterAnalysisGroup
    127   /// template.
    128   ///
    129   void addInterfaceImplemented(const PassInfo *ItfPI) {
    130     ItfImpl.push_back(ItfPI);
    131   }
    132 
    133   /// getInterfacesImplemented - Return a list of all of the analysis group
    134   /// interfaces implemented by this pass.
    135   ///
    136   const std::vector<const PassInfo*> &getInterfacesImplemented() const {
    137     return ItfImpl;
    138   }
    139 
    140 private:
    141   void operator=(const PassInfo &) = delete;
    142   PassInfo(const PassInfo &) = delete;
    143 };
    144 
    145 }
    146 
    147 #endif
    148