LLVM  13.0.0git
CallGraphSCCPass.h
Go to the documentation of this file.
1 //===- CallGraphSCCPass.h - Pass that operates BU on call graph -*- 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 the CallGraphSCCPass class, which is used for passes which
10 // are implemented as bottom-up traversals on the call graph. Because there may
11 // be cycles in the call graph, passes of this type operate on the call-graph in
12 // SCC order: that is, they process function bottom-up, except for recursive
13 // functions, which they process all at once.
14 //
15 // These passes are inherently interprocedural, and are required to keep the
16 // call graph up-to-date if they do anything which could modify it.
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #ifndef LLVM_ANALYSIS_CALLGRAPHSCCPASS_H
21 #define LLVM_ANALYSIS_CALLGRAPHSCCPASS_H
22 
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/Pass.h"
25 #include <vector>
26 
27 namespace llvm {
28 
29 class CallGraph;
30 class CallGraphNode;
31 class CallGraphSCC;
32 class PMStack;
33 
34 class CallGraphSCCPass : public Pass {
35 public:
36  explicit CallGraphSCCPass(char &pid) : Pass(PT_CallGraphSCC, pid) {}
37 
38  /// createPrinterPass - Get a pass that prints the Module
39  /// corresponding to a CallGraph.
41  const std::string &Banner) const override;
42 
45 
46  /// doInitialization - This method is called before the SCC's of the program
47  /// has been processed, allowing the pass to do initialization as necessary.
48  virtual bool doInitialization(CallGraph &CG) {
49  return false;
50  }
51 
52  /// runOnSCC - This method should be implemented by the subclass to perform
53  /// whatever action is necessary for the specified SCC. Note that
54  /// non-recursive (or only self-recursive) functions will have an SCC size of
55  /// 1, where recursive portions of the call graph will have SCC size > 1.
56  ///
57  /// SCC passes that add or delete functions to the SCC are required to update
58  /// the SCC list, otherwise stale pointers may be dereferenced.
59  virtual bool runOnSCC(CallGraphSCC &SCC) = 0;
60 
61  /// doFinalization - This method is called after the SCC's of the program has
62  /// been processed, allowing the pass to do final cleanup as necessary.
63  virtual bool doFinalization(CallGraph &CG) {
64  return false;
65  }
66 
67  /// Assign pass manager to manager this pass
68  void assignPassManager(PMStack &PMS, PassManagerType PMT) override;
69 
70  /// Return what kind of Pass Manager can manage this pass.
73  }
74 
75  /// getAnalysisUsage - For this class, we declare that we require and preserve
76  /// the call graph. If the derived class implements this method, it should
77  /// always explicitly call the implementation here.
78  void getAnalysisUsage(AnalysisUsage &Info) const override;
79 
80 protected:
81  /// Optional passes call this function to check whether the pass should be
82  /// skipped. This is the case when optimization bisect is over the limit.
83  bool skipSCC(CallGraphSCC &SCC) const;
84 };
85 
86 /// CallGraphSCC - This is a single SCC that a CallGraphSCCPass is run on.
87 class CallGraphSCC {
88  const CallGraph &CG; // The call graph for this SCC.
89  void *Context; // The CGPassManager object that is vending this.
90  std::vector<CallGraphNode *> Nodes;
91 
92 public:
93  CallGraphSCC(CallGraph &cg, void *context) : CG(cg), Context(context) {}
94 
96  Nodes.assign(NewNodes.begin(), NewNodes.end());
97  }
98 
99  bool isSingular() const { return Nodes.size() == 1; }
100  unsigned size() const { return Nodes.size(); }
101 
102  /// ReplaceNode - This informs the SCC and the pass manager that the specified
103  /// Old node has been deleted, and New is to be used in its place.
104  void ReplaceNode(CallGraphNode *Old, CallGraphNode *New);
105 
106  /// DeleteNode - This informs the SCC and the pass manager that the specified
107  /// Old node has been deleted.
108  void DeleteNode(CallGraphNode *Old);
109 
110  using iterator = std::vector<CallGraphNode *>::const_iterator;
111 
112  iterator begin() const { return Nodes.begin(); }
113  iterator end() const { return Nodes.end(); }
114 
115  const CallGraph &getCallGraph() { return CG; }
116 };
117 
118 void initializeDummyCGSCCPassPass(PassRegistry &);
119 
120 /// This pass is required by interprocedural register allocation. It forces
121 /// codegen to follow bottom up order on call graph.
123 public:
124  static char ID;
125 
129  }
130 
131  bool runOnSCC(CallGraphSCC &SCC) override { return false; }
132 
133  void getAnalysisUsage(AnalysisUsage &AU) const override {
134  AU.setPreservesAll();
135  }
136 };
137 
138 } // end namespace llvm
139 
140 #endif // LLVM_ANALYSIS_CALLGRAPHSCCPASS_H
llvm
Definition: AllocatorList.h:23
llvm::PMT_CallGraphPassManager
@ PMT_CallGraphPassManager
CGPassManager.
Definition: Pass.h:55
Pass.h
llvm::CallGraph
The basic data container for the call graph of a Module of IR.
Definition: CallGraph.h:73
llvm::PassManagerType
PassManagerType
Different types of internal pass managers.
Definition: Pass.h:52
llvm::CallGraphSCC::CallGraphSCC
CallGraphSCC(CallGraph &cg, void *context)
Definition: CallGraphSCCPass.h:93
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::CallGraphSCCPass::doInitialization
virtual bool doInitialization(CallGraph &CG)
doInitialization - This method is called before the SCC's of the program has been processed,...
Definition: CallGraphSCCPass.h:48
llvm::CallGraphSCC
CallGraphSCC - This is a single SCC that a CallGraphSCCPass is run on.
Definition: CallGraphSCCPass.h:87
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::CallGraphSCCPass::getPotentialPassManagerType
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: CallGraphSCCPass.h:71
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::CallGraphSCC::isSingular
bool isSingular() const
Definition: CallGraphSCCPass.h:99
llvm::CallGraphSCC::size
unsigned size() const
Definition: CallGraphSCCPass.h:100
llvm::CallGraphSCC::ReplaceNode
void ReplaceNode(CallGraphNode *Old, CallGraphNode *New)
ReplaceNode - This informs the SCC and the pass manager that the specified Old node has been deleted,...
Definition: CallGraphSCCPass.cpp:593
llvm::CallGraphSCCPass::skipSCC
bool skipSCC(CallGraphSCC &SCC) const
Optional passes call this function to check whether the pass should be skipped.
Definition: CallGraphSCCPass.cpp:752
llvm::DummyCGSCCPass::ID
static char ID
Definition: CallGraphSCCPass.h:124
llvm::PassRegistry
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
llvm::CallGraphNode
A node in the call graph for a module.
Definition: CallGraph.h:167
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
llvm::DummyCGSCCPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - For this class, we declare that we require and preserve the call graph.
Definition: CallGraphSCCPass.h:133
llvm::CallGraphSCC::iterator
std::vector< CallGraphNode * >::const_iterator iterator
Definition: CallGraphSCCPass.h:110
llvm::CallGraphSCCPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &Info) const override
getAnalysisUsage - For this class, we declare that we require and preserve the call graph.
Definition: CallGraphSCCPass.cpp:659
llvm::PT_CallGraphSCC
@ PT_CallGraphSCC
Definition: Pass.h:67
llvm::CallGraphSCC::DeleteNode
void DeleteNode(CallGraphNode *Old)
DeleteNode - This informs the SCC and the pass manager that the specified Old node has been deleted.
Definition: CallGraphSCCPass.cpp:611
llvm::CallGraphSCC::begin
iterator begin() const
Definition: CallGraphSCCPass.h:112
llvm::CallGraphSCC::getCallGraph
const CallGraph & getCallGraph()
Definition: CallGraphSCCPass.h:115
llvm::CallGraphSCCPass::createPrinterPass
Pass * createPrinterPass(raw_ostream &OS, const std::string &Banner) const override
createPrinterPass - Get a pass that prints the Module corresponding to a CallGraph.
Definition: CallGraphSCCPass.cpp:732
ArrayRef.h
llvm::DummyCGSCCPass
This pass is required by interprocedural register allocation.
Definition: CallGraphSCCPass.h:122
llvm::CallGraphSCC::end
iterator end() const
Definition: CallGraphSCCPass.h:113
llvm::DummyCGSCCPass::runOnSCC
bool runOnSCC(CallGraphSCC &SCC) override
runOnSCC - This method should be implemented by the subclass to perform whatever action is necessary ...
Definition: CallGraphSCCPass.h:131
llvm::AMDGPU::CPol::SCC
@ SCC
Definition: SIDefines.h:285
llvm::Pass::doFinalization
virtual bool doFinalization(Module &)
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
Definition: Pass.h:120
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::Pass::doInitialization
virtual bool doInitialization(Module &)
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
Definition: Pass.h:116
llvm::PMStack
PMStack - This class implements a stack data structure of PMDataManager pointers.
Definition: LegacyPassManagers.h:137
llvm::CallGraphSCCPass::doFinalization
virtual bool doFinalization(CallGraph &CG)
doFinalization - This method is called after the SCC's of the program has been processed,...
Definition: CallGraphSCCPass.h:63
llvm::CallGraphSCC::initialize
void initialize(ArrayRef< CallGraphNode * > NewNodes)
Definition: CallGraphSCCPass.h:95
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
llvm::DummyCGSCCPass::DummyCGSCCPass
DummyCGSCCPass()
Definition: CallGraphSCCPass.h:126
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:153
llvm::CallGraphSCCPass
Definition: CallGraphSCCPass.h:34
llvm::CallGraphSCCPass::assignPassManager
void assignPassManager(PMStack &PMS, PassManagerType PMT) override
Assign pass manager to manager this pass.
Definition: CallGraphSCCPass.cpp:620
llvm::Pass
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
llvm::Registry
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:44
llvm::CallGraphSCCPass::CallGraphSCCPass
CallGraphSCCPass(char &pid)
Definition: CallGraphSCCPass.h:36
llvm::initializeDummyCGSCCPassPass
void initializeDummyCGSCCPassPass(PassRegistry &)
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:154
llvm::CallGraphSCCPass::runOnSCC
virtual bool runOnSCC(CallGraphSCC &SCC)=0
runOnSCC - This method should be implemented by the subclass to perform whatever action is necessary ...