1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===- CallGraphSCCPass.h - Pass that operates BU on call graph -*- C++ -*-===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file defines the CallGraphSCCPass class, which is used for passes which
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// are implemented as bottom-up traversals on the call graph.  Because there may
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// be cycles in the call graph, passes of this type operate on the call-graph in
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// SCC order: that is, they process function bottom-up, except for recursive
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// functions, which they process all at once.
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// These passes are inherently interprocedural, and are required to keep the
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// call graph up-to-date if they do anything which could modify it.
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef LLVM_CALL_GRAPH_SCC_PASS_H
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define LLVM_CALL_GRAPH_SCC_PASS_H
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Pass.h"
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Analysis/CallGraph.h"
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace llvm {
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass CallGraphNode;
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass CallGraph;
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass PMStack;
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass CallGraphSCC;
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass CallGraphSCCPass : public Pass {
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  explicit CallGraphSCCPass(char &pid) : Pass(PT_CallGraphSCC, pid) {}
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// createPrinterPass - Get a pass that prints the Module
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// corresponding to a CallGraph.
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// doInitialization - This method is called before the SCC's of the program
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// has been processed, allowing the pass to do initialization as necessary.
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual bool doInitialization(CallGraph &CG) {
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// runOnSCC - This method should be implemented by the subclass to perform
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// whatever action is necessary for the specified SCC.  Note that
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// non-recursive (or only self-recursive) functions will have an SCC size of
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// 1, where recursive portions of the call graph will have SCC size > 1.
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// SCC passes that add or delete functions to the SCC are required to update
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// the SCC list, otherwise stale pointers may be dereferenced.
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual bool runOnSCC(CallGraphSCC &SCC) = 0;
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// doFinalization - This method is called after the SCC's of the program has
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// been processed, allowing the pass to do final cleanup as necessary.
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual bool doFinalization(CallGraph &CG) {
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Assign pass manager to manager this pass
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void assignPassManager(PMStack &PMS,
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 PassManagerType PMT);
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///  Return what kind of Pass Manager can manage this pass.
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual PassManagerType getPotentialPassManagerType() const {
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return PMT_CallGraphPassManager;
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getAnalysisUsage - For this class, we declare that we require and preserve
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// the call graph.  If the derived class implements this method, it should
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// always explicitly call the implementation here.
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void getAnalysisUsage(AnalysisUsage &Info) const;
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CallGraphSCC - This is a single SCC that a CallGraphSCCPass is run on.
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass CallGraphSCC {
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void *Context; // The CGPassManager object that is vending this.
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<CallGraphNode*> Nodes;
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CallGraphSCC(void *context) : Context(context) {}
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void initialize(CallGraphNode*const*I, CallGraphNode*const*E) {
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Nodes.assign(I, E);
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isSingular() const { return Nodes.size() == 1; }
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned size() const { return Nodes.size(); }
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// ReplaceNode - This informs the SCC and the pass manager that the specified
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Old node has been deleted, and New is to be used in its place.
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void ReplaceNode(CallGraphNode *Old, CallGraphNode *New);
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef std::vector<CallGraphNode*>::const_iterator iterator;
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  iterator begin() const { return Nodes.begin(); }
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  iterator end() const { return Nodes.end(); }
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} // End llvm namespace
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
105