1 //===- llvm/PassInfo.h - Pass Info class ------------------------*- C++ -*-===//
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 and implements the PassInfo class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_PASSINFO_H
14 #define LLVM_PASSINFO_H
15 
16 #include "llvm/ADT/StringRef.h"
17 #include <cassert>
18 #include <vector>
19 
20 namespace llvm {
21 
22 class Pass;
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   using NormalCtor_t = Pass* (*)();
33 
34 private:
35   StringRef PassName;     // Nice name for Pass
36   StringRef PassArgument; // Command Line argument to run this pass
37   const void *PassID;
38   const bool IsCFGOnlyPass = false;      // Pass only looks at the CFG.
39   const bool IsAnalysis;                 // True if an analysis pass.
40   const bool IsAnalysisGroup;            // True if an analysis group.
41   std::vector<const PassInfo *> ItfImpl; // Interfaces implemented by this pass
42   NormalCtor_t NormalCtor = nullptr;
43 
44 public:
45   /// PassInfo ctor - Do not call this directly, this should only be invoked
46   /// through RegisterPass.
PassInfo(StringRef name,StringRef arg,const void * pi,NormalCtor_t normal,bool isCFGOnly,bool is_analysis)47   PassInfo(StringRef name, StringRef arg, const void *pi, NormalCtor_t normal,
48            bool isCFGOnly, bool is_analysis)
49       : PassName(name), PassArgument(arg), PassID(pi), IsCFGOnlyPass(isCFGOnly),
50         IsAnalysis(is_analysis), IsAnalysisGroup(false), NormalCtor(normal) {}
51 
52   /// PassInfo ctor - Do not call this directly, this should only be invoked
53   /// through RegisterPass. This version is for use by analysis groups; it
54   /// does not auto-register the pass.
PassInfo(StringRef name,const void * pi)55   PassInfo(StringRef name, const void *pi)
56       : PassName(name), PassID(pi), IsAnalysis(false), IsAnalysisGroup(true) {}
57 
58   PassInfo(const PassInfo &) = delete;
59   PassInfo &operator=(const PassInfo &) = delete;
60 
61   /// getPassName - Return the friendly name for the pass, never returns null
getPassName()62   StringRef getPassName() const { return PassName; }
63 
64   /// getPassArgument - Return the command line option that may be passed to
65   /// 'opt' that will cause this pass to be run.  This will return null if there
66   /// is no argument.
getPassArgument()67   StringRef getPassArgument() const { return PassArgument; }
68 
69   /// getTypeInfo - Return the id object for the pass...
70   /// TODO : Rename
getTypeInfo()71   const void *getTypeInfo() const { return PassID; }
72 
73   /// Return true if this PassID implements the specified ID pointer.
isPassID(const void * IDPtr)74   bool isPassID(const void *IDPtr) const { return PassID == IDPtr; }
75 
76   /// isAnalysisGroup - Return true if this is an analysis group, not a normal
77   /// pass.
isAnalysisGroup()78   bool isAnalysisGroup() const { return IsAnalysisGroup; }
isAnalysis()79   bool isAnalysis() const { return IsAnalysis; }
80 
81   /// isCFGOnlyPass - return true if this pass only looks at the CFG for the
82   /// function.
isCFGOnlyPass()83   bool isCFGOnlyPass() const { return IsCFGOnlyPass; }
84 
85   /// getNormalCtor - Return a pointer to a function, that when called, creates
86   /// an instance of the pass and returns it.  This pointer may be null if there
87   /// is no default constructor for the pass.
getNormalCtor()88   NormalCtor_t getNormalCtor() const {
89     return NormalCtor;
90   }
setNormalCtor(NormalCtor_t Ctor)91   void setNormalCtor(NormalCtor_t Ctor) {
92     NormalCtor = Ctor;
93   }
94 
95   /// createPass() - Use this method to create an instance of this pass.
createPass()96   Pass *createPass() const {
97     assert((!isAnalysisGroup() || NormalCtor) &&
98            "No default implementation found for analysis group!");
99     assert(NormalCtor &&
100            "Cannot call createPass on PassInfo without default ctor!");
101     return NormalCtor();
102   }
103 
104   /// addInterfaceImplemented - This method is called when this pass is
105   /// registered as a member of an analysis group with the RegisterAnalysisGroup
106   /// template.
addInterfaceImplemented(const PassInfo * ItfPI)107   void addInterfaceImplemented(const PassInfo *ItfPI) {
108     ItfImpl.push_back(ItfPI);
109   }
110 
111   /// getInterfacesImplemented - Return a list of all of the analysis group
112   /// interfaces implemented by this pass.
getInterfacesImplemented()113   const std::vector<const PassInfo*> &getInterfacesImplemented() const {
114     return ItfImpl;
115   }
116 };
117 
118 } // end namespace llvm
119 
120 #endif // LLVM_PASSINFO_H
121