Make removeTriviallyDeadNodes a private interface
[oota-llvm.git] / include / llvm / Analysis / DSGraph.h
1 //===- DSGraph.h - Represent a collection of data structures ----*- C++ -*-===//
2 //
3 // This header defines the data structure graph.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #ifndef LLVM_ANALYSIS_DSGRAPH_H
8 #define LLVM_ANALYSIS_DSGRAPH_H
9
10 #include "llvm/Analysis/DSNode.h"
11
12 //===----------------------------------------------------------------------===//
13 /// DSGraph - The graph that represents a function.
14 ///
15 class DSGraph {
16   Function *Func;          // Func - The LLVM function this graph corresponds to
17   DSGraph *GlobalsGraph;   // Pointer to the common graph of global objects
18
19   DSNodeHandle RetNode;    // The node that gets returned...
20   std::vector<DSNode*> Nodes;
21   std::map<Value*, DSNodeHandle> ScalarMap;
22
23   // FunctionCalls - This vector maintains a single entry for each call
24   // instruction in the current graph.  The first entry in the vector is the
25   // scalar that holds the return value for the call, the second is the function
26   // scalar being invoked, and the rest are pointer arguments to the function.
27   // This vector is built by the Local graph and is never modified after that.
28   //
29   std::vector<DSCallSite> FunctionCalls;
30
31   // AuxFunctionCalls - This vector contains call sites that have been processed
32   // by some mechanism.  In pratice, the BU Analysis uses this vector to hold
33   // the _unresolved_ call sites, because it cannot modify FunctionCalls.
34   //
35   std::vector<DSCallSite> AuxFunctionCalls;
36
37   void operator=(const DSGraph &); // DO NOT IMPLEMENT
38 public:
39   DSGraph() : Func(0), GlobalsGraph(0) {}      // Create a new, empty, DSGraph.
40   DSGraph(Function &F, DSGraph *GlobalsGraph); // Compute the local DSGraph
41
42   // Copy ctor - If you want to capture the node mapping between the source and
43   // destination graph, you may optionally do this by specifying a map to record
44   // this into.
45   //
46   // Note that a copied graph does not retain the GlobalsGraph pointer of the
47   // source.  You need to set a new GlobalsGraph with the setGlobalsGraph
48   // method.
49   //
50   DSGraph(const DSGraph &DSG);
51   DSGraph(const DSGraph &DSG, std::map<const DSNode*, DSNodeHandle> &NodeMap);
52   ~DSGraph();
53
54   bool hasFunction() const { return Func != 0; }
55   Function &getFunction() const { return *Func; }
56
57   DSGraph *getGlobalsGraph() const { return GlobalsGraph; }
58   void setGlobalsGraph(DSGraph *G) { GlobalsGraph = G; }
59
60   /// getNodes - Get a vector of all the nodes in the graph
61   /// 
62   const std::vector<DSNode*> &getNodes() const { return Nodes; }
63         std::vector<DSNode*> &getNodes()       { return Nodes; }
64
65   /// addNode - Add a new node to the graph.
66   ///
67   void addNode(DSNode *N) { Nodes.push_back(N); }
68
69   /// getScalarMap - Get a map that describes what the nodes the scalars in this
70   /// function point to...
71   ///
72   std::map<Value*, DSNodeHandle> &getScalarMap() { return ScalarMap; }
73   const std::map<Value*, DSNodeHandle> &getScalarMap() const {return ScalarMap;}
74
75   /// getFunctionCalls - Return the list of call sites in the original local
76   /// graph...
77   ///
78   const std::vector<DSCallSite> &getFunctionCalls() const {
79     return FunctionCalls;
80   }
81
82   /// getAuxFunctionCalls - Get the call sites as modified by whatever passes
83   /// have been run.
84   ///
85   std::vector<DSCallSite> &getAuxFunctionCalls() {
86     return AuxFunctionCalls;
87   }
88
89   /// getNodeForValue - Given a value that is used or defined in the body of the
90   /// current function, return the DSNode that it points to.
91   ///
92   DSNodeHandle &getNodeForValue(Value *V) { return ScalarMap[V]; }
93
94   const DSNodeHandle &getNodeForValue(Value *V) const {
95     std::map<Value*, DSNodeHandle>::const_iterator I = ScalarMap.find(V);
96     assert(I != ScalarMap.end() &&
97            "Use non-const lookup function if node may not be in the map");
98     return I->second;
99   }
100
101   const DSNodeHandle &getRetNode() const { return RetNode; }
102         DSNodeHandle &getRetNode()       { return RetNode; }
103
104   unsigned getGraphSize() const {
105     return Nodes.size();
106   }
107
108   void print(std::ostream &O) const;
109   void dump() const;
110   void writeGraphToFile(std::ostream &O, const std::string &GraphName) const;
111
112   // maskNodeTypes - Apply a mask to all of the node types in the graph.  This
113   // is useful for clearing out markers like Scalar or Incomplete.
114   //
115   void maskNodeTypes(unsigned char Mask);
116   void maskIncompleteMarkers() { maskNodeTypes(~DSNode::Incomplete); }
117
118   // markIncompleteNodes - Traverse the graph, identifying nodes that may be
119   // modified by other functions that have not been resolved yet.  This marks
120   // nodes that are reachable through three sources of "unknownness":
121   //   Global Variables, Function Calls, and Incoming Arguments
122   //
123   // For any node that may have unknown components (because something outside
124   // the scope of current analysis may have modified it), the 'Incomplete' flag
125   // is added to the NodeType.
126   //
127   void markIncompleteNodes(bool markFormalArgs = true);
128
129   // removeDeadNodes - Use a more powerful reachability analysis to eliminate
130   // subgraphs that are unreachable.  This often occurs because the data
131   // structure doesn't "escape" into it's caller, and thus should be eliminated
132   // from the caller's graph entirely.  This is only appropriate to use when
133   // inlining graphs.
134   //
135   void removeDeadNodes(bool KeepAllGlobals, bool KeepCalls);
136
137   // CloneFlags enum - Bits that may be passed into the cloneInto method to
138   // specify how to clone the function graph.
139   enum CloneFlags {
140     StripAllocaBit        = 1 << 0, KeepAllocaBit     = 0 << 0,
141     DontCloneCallNodes    = 1 << 1, CloneCallNodes    = 0 << 0,
142     DontCloneAuxCallNodes = 1 << 2, CloneAuxCallNodes = 0 << 0,
143   };
144
145   // cloneInto - Clone the specified DSGraph into the current graph, returning
146   // the Return node of the graph.  The translated ScalarMap for the old
147   // function is filled into the OldValMap member.  If StripAllocas is set to
148   // 'StripAllocaBit', Alloca markers are removed from the graph as the graph is
149   // being cloned.
150   //
151   DSNodeHandle cloneInto(const DSGraph &G,
152                          std::map<Value*, DSNodeHandle> &OldValMap,
153                          std::map<const DSNode*, DSNodeHandle> &OldNodeMap,
154                          unsigned CloneFlags = 0);
155
156   /// mergeInGraph - The method is used for merging graphs together.  If the
157   /// argument graph is not *this, it makes a clone of the specified graph, then
158   /// merges the nodes specified in the call site with the formal arguments in
159   /// the graph.  If the StripAlloca's argument is 'StripAllocaBit' then Alloca
160   /// markers are removed from nodes.
161   ///
162   void mergeInGraph(DSCallSite &CS, const DSGraph &Graph, unsigned CloneFlags);
163
164 private:
165   bool isNodeDead(DSNode *N);
166
167   // removeTriviallyDeadNodes - After the graph has been constructed, this
168   // method removes all unreachable nodes that are created because they got
169   // merged with other nodes in the graph.  This is used as the first step of
170   // removeDeadNodes.
171   //
172   void removeTriviallyDeadNodes(bool KeepAllGlobals = false);
173 };
174
175 #endif