Revert r110396 to fix buildbots.
[oota-llvm.git] / include / llvm / CallGraphSCCPass.h
1 //===- CallGraphSCCPass.h - Pass that operates BU on call graph -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the CallGraphSCCPass class, which is used for passes which
11 // are implemented as bottom-up traversals on the call graph.  Because there may
12 // be cycles in the call graph, passes of this type operate on the call-graph in
13 // SCC order: that is, they process function bottom-up, except for recursive
14 // functions, which they process all at once.
15 //
16 // These passes are inherently interprocedural, and are required to keep the
17 // call graph up-to-date if they do anything which could modify it.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_CALL_GRAPH_SCC_PASS_H
22 #define LLVM_CALL_GRAPH_SCC_PASS_H
23
24 #include "llvm/Pass.h"
25 #include "llvm/Analysis/CallGraph.h"
26
27 namespace llvm {
28
29 class CallGraphNode;
30 class CallGraph;
31 class PMStack;
32 class CallGraphSCC;
33   
34 class CallGraphSCCPass : public Pass {
35 public:
36   explicit CallGraphSCCPass(intptr_t pid) : Pass(PT_CallGraphSCC, pid) {}
37   explicit CallGraphSCCPass(void *pid) : Pass(PT_CallGraphSCC, pid) {}
38
39   /// createPrinterPass - Get a pass that prints the Module
40   /// corresponding to a CallGraph.
41   Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
42
43   /// doInitialization - This method is called before the SCC's of the program
44   /// has been processed, allowing the pass to do initialization as necessary.
45   virtual bool doInitialization(CallGraph &CG) {
46     return false;
47   }
48
49   /// runOnSCC - This method should be implemented by the subclass to perform
50   /// whatever action is necessary for the specified SCC.  Note that
51   /// non-recursive (or only self-recursive) functions will have an SCC size of
52   /// 1, where recursive portions of the call graph will have SCC size > 1.
53   ///
54   /// SCC passes that add or delete functions to the SCC are required to update
55   /// the SCC list, otherwise stale pointers may be dereferenced.
56   ///
57   virtual bool runOnSCC(CallGraphSCC &SCC) = 0;
58
59   /// doFinalization - This method is called after the SCC's of the program has
60   /// been processed, allowing the pass to do final cleanup as necessary.
61   virtual bool doFinalization(CallGraph &CG) {
62     return false;
63   }
64
65   /// Assign pass manager to manager this pass
66   virtual void assignPassManager(PMStack &PMS,
67                                  PassManagerType PMT =PMT_CallGraphPassManager);
68
69   ///  Return what kind of Pass Manager can manage this pass.
70   virtual PassManagerType getPotentialPassManagerType() const {
71     return PMT_CallGraphPassManager;
72   }
73
74   /// getAnalysisUsage - For this class, we declare that we require and preserve
75   /// the call graph.  If the derived class implements this method, it should
76   /// always explicitly call the implementation here.
77   virtual void getAnalysisUsage(AnalysisUsage &Info) const;
78 };
79
80 /// CallGraphSCC - This is a single SCC that a CallGraphSCCPass is run on. 
81 class CallGraphSCC {
82   void *Context; // The CGPassManager object that is vending this.
83   std::vector<CallGraphNode*> Nodes;
84 public:
85   CallGraphSCC(void *context) : Context(context) {}
86   
87   void initialize(CallGraphNode*const*I, CallGraphNode*const*E) {
88     Nodes.assign(I, E);
89   }
90   
91   bool isSingular() const { return Nodes.size() == 1; }
92   unsigned size() const { return Nodes.size(); }
93   
94   /// ReplaceNode - This informs the SCC and the pass manager that the specified
95   /// Old node has been deleted, and New is to be used in its place.
96   void ReplaceNode(CallGraphNode *Old, CallGraphNode *New);
97   
98   typedef std::vector<CallGraphNode*>::const_iterator iterator;
99   iterator begin() const { return Nodes.begin(); }
100   iterator end() const { return Nodes.end(); }
101 };
102
103 } // End llvm namespace
104
105 #endif