1c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===- CallGraphSCCPass.h - Pass that operates BU on call graph -*- C++ -*-===//
2c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
3c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//                     The LLVM Compiler Infrastructure
4c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
5c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// This file is distributed under the University of Illinois Open Source
6c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// License. See LICENSE.TXT for details.
7c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
8c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===----------------------------------------------------------------------===//
9c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
10c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// This file defines the CallGraphSCCPass class, which is used for passes which
11c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// are implemented as bottom-up traversals on the call graph.  Because there may
12c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// be cycles in the call graph, passes of this type operate on the call-graph in
13c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// SCC order: that is, they process function bottom-up, except for recursive
14c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// functions, which they process all at once.
15c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
16c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// These passes are inherently interprocedural, and are required to keep the
17c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// call graph up-to-date if they do anything which could modify it.
18c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
19c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===----------------------------------------------------------------------===//
20c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
21c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#ifndef LLVM_ANALYSIS_CALLGRAPHSCCPASS_H
22c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#define LLVM_ANALYSIS_CALLGRAPHSCCPASS_H
23c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
24c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/ADT/ArrayRef.h"
25c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/Pass.h"
26c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <vector>
27c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
28c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotnamespace llvm {
29c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
30c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass CallGraph;
31c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass CallGraphNode;
32c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass CallGraphSCC;
33c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass PMStack;
34c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
35c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass CallGraphSCCPass : public Pass {
36c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotpublic:
37c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  explicit CallGraphSCCPass(char &pid) : Pass(PT_CallGraphSCC, pid) {}
38c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
39c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// createPrinterPass - Get a pass that prints the Module
40c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// corresponding to a CallGraph.
41c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  Pass *createPrinterPass(raw_ostream &OS,
42c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                          const std::string &Banner) const override;
43c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
44c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using llvm::Pass::doInitialization;
45c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using llvm::Pass::doFinalization;
46c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
47c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// doInitialization - This method is called before the SCC's of the program
48c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// has been processed, allowing the pass to do initialization as necessary.
49c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  virtual bool doInitialization(CallGraph &CG) {
50c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return false;
51c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
52c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
53c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// runOnSCC - This method should be implemented by the subclass to perform
54c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// whatever action is necessary for the specified SCC.  Note that
55c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// non-recursive (or only self-recursive) functions will have an SCC size of
56c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// 1, where recursive portions of the call graph will have SCC size > 1.
57c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  ///
58c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// SCC passes that add or delete functions to the SCC are required to update
59c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// the SCC list, otherwise stale pointers may be dereferenced.
60c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  virtual bool runOnSCC(CallGraphSCC &SCC) = 0;
61c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
62c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// doFinalization - This method is called after the SCC's of the program has
63c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// been processed, allowing the pass to do final cleanup as necessary.
64c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  virtual bool doFinalization(CallGraph &CG) {
65c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return false;
66c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
67c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
68c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// Assign pass manager to manager this pass
69c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void assignPassManager(PMStack &PMS, PassManagerType PMT) override;
70c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
71c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  ///  Return what kind of Pass Manager can manage this pass.
72c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  PassManagerType getPotentialPassManagerType() const override {
73c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return PMT_CallGraphPassManager;
74c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
75c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
76c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// getAnalysisUsage - For this class, we declare that we require and preserve
77c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// the call graph.  If the derived class implements this method, it should
78c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// always explicitly call the implementation here.
79c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void getAnalysisUsage(AnalysisUsage &Info) const override;
80c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
81c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotprotected:
82c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// Optional passes call this function to check whether the pass should be
83c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// skipped. This is the case when optimization bisect is over the limit.
84c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  bool skipSCC(CallGraphSCC &SCC) const;
85c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot};
86c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
87c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// CallGraphSCC - This is a single SCC that a CallGraphSCCPass is run on.
88c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass CallGraphSCC {
89c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  const CallGraph &CG; // The call graph for this SCC.
90c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void *Context; // The CGPassManager object that is vending this.
91c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  std::vector<CallGraphNode *> Nodes;
92c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
93c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotpublic:
94c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  CallGraphSCC(CallGraph &cg, void *context) : CG(cg), Context(context) {}
95c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
96c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void initialize(ArrayRef<CallGraphNode *> NewNodes) {
97c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    Nodes.assign(NewNodes.begin(), NewNodes.end());
98c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
99c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
100c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  bool isSingular() const { return Nodes.size() == 1; }
101c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  unsigned size() const { return Nodes.size(); }
102c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
103c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// ReplaceNode - This informs the SCC and the pass manager that the specified
104c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// Old node has been deleted, and New is to be used in its place.
105c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void ReplaceNode(CallGraphNode *Old, CallGraphNode *New);
106c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
107c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using iterator = std::vector<CallGraphNode *>::const_iterator;
108c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
109c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  iterator begin() const { return Nodes.begin(); }
110c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  iterator end() const { return Nodes.end(); }
111c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
112c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  const CallGraph &getCallGraph() { return CG; }
113c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot};
114c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
115c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotvoid initializeDummyCGSCCPassPass(PassRegistry &);
116c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
117c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// This pass is required by interprocedural register allocation. It forces
118c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// codegen to follow bottom up order on call graph.
119c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass DummyCGSCCPass : public CallGraphSCCPass {
120c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotpublic:
121c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  static char ID;
122c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
123c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  DummyCGSCCPass() : CallGraphSCCPass(ID) {
124c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    PassRegistry &Registry = *PassRegistry::getPassRegistry();
125c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    initializeDummyCGSCCPassPass(Registry);
126c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
127c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
128c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  bool runOnSCC(CallGraphSCC &SCC) override { return false; }
129c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
130c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void getAnalysisUsage(AnalysisUsage &AU) const override {
131c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    AU.setPreservesAll();
132c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
133c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot};
134c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
135c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot} // end namespace llvm
136c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
137c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#endif // LLVM_ANALYSIS_CALLGRAPHSCCPASS_H
138