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 "llvm/Support/Compiler.h"
26#include <vector>
27
28namespace llvm {
29
30class CallGraph;
31class CallGraphNode;
32class CallGraphSCC;
33class PMStack;
34namespace cl {
35class LibraryOptionsContext;
36} // namespace cl
37
38LLVM_ABI unsigned getMaxDevirtIterations(const cl::LibraryOptionsContext &);
39
40class LLVM_ABI CallGraphSCCPass : public Pass {
41public:
42 explicit CallGraphSCCPass(char &pid) : Pass(PT_CallGraphSCC, pid) {}
43
44 /// createPrinterPass - Get a pass that prints the Module
45 /// corresponding to a CallGraph.
46 Pass *createPrinterPass(raw_ostream &OS,
47 const std::string &Banner) const override;
48
49 using llvm::Pass::doInitialization;
50 using llvm::Pass::doFinalization;
51
52 /// doInitialization - This method is called before the SCC's of the program
53 /// has been processed, allowing the pass to do initialization as necessary.
54 virtual bool doInitialization(CallGraph &CG) {
55 return false;
56 }
57
58 /// runOnSCC - This method should be implemented by the subclass to perform
59 /// whatever action is necessary for the specified SCC. Note that
60 /// non-recursive (or only self-recursive) functions will have an SCC size of
61 /// 1, where recursive portions of the call graph will have SCC size > 1.
62 ///
63 /// SCC passes that add or delete functions to the SCC are required to update
64 /// the SCC list, otherwise stale pointers may be dereferenced.
65 virtual bool runOnSCC(CallGraphSCC &SCC) = 0;
66
67 /// doFinalization - This method is called after the SCC's of the program has
68 /// been processed, allowing the pass to do final cleanup as necessary.
69 virtual bool doFinalization(CallGraph &CG) {
70 return false;
71 }
72
73 /// Assign pass manager to manager this pass
74 void assignPassManager(PMStack &PMS, PassManagerType PMT) override;
75
76 /// Return what kind of Pass Manager can manage this pass.
77 PassManagerType getPotentialPassManagerType() const override {
78 return PMT_CallGraphPassManager;
79 }
80
81 /// getAnalysisUsage - For this class, we declare that we require and preserve
82 /// the call graph. If the derived class implements this method, it should
83 /// always explicitly call the implementation here.
84 void getAnalysisUsage(AnalysisUsage &Info) const override;
85};
86
87/// CallGraphSCC - This is a single SCC that a CallGraphSCCPass is run on.
88class CallGraphSCC {
89 const CallGraph &CG; // The call graph for this SCC.
90 std::vector<CallGraphNode *> Nodes;
91
92public:
93 CallGraphSCC(CallGraph &cg) : CG(cg) {}
94
95 void initialize(ArrayRef<CallGraphNode *> NewNodes) {
96 Nodes.assign(first: NewNodes.begin(), last: NewNodes.end());
97 }
98
99 bool isSingular() const { return Nodes.size() == 1; }
100 unsigned size() const { return Nodes.size(); }
101
102 using iterator = std::vector<CallGraphNode *>::const_iterator;
103
104 iterator begin() const { return Nodes.begin(); }
105 iterator end() const { return Nodes.end(); }
106
107 const CallGraph &getCallGraph() { return CG; }
108};
109
110/// This pass is required by interprocedural register allocation. It forces
111/// codegen to follow bottom up order on call graph.
112class DummyCGSCCPass : public CallGraphSCCPass {
113public:
114 LLVM_ABI static char ID;
115
116 DummyCGSCCPass() : CallGraphSCCPass(ID) {}
117
118 bool runOnSCC(CallGraphSCC &SCC) override { return false; }
119
120 void getAnalysisUsage(AnalysisUsage &AU) const override {
121 AU.setPreservesAll();
122 }
123};
124
125} // end namespace llvm
126
127#endif // LLVM_ANALYSIS_CALLGRAPHSCCPASS_H
128