Add a comment to the method decl
[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 class GlobalValue;
12
13 //===----------------------------------------------------------------------===//
14 /// DSGraph - The graph that represents a function.
15 ///
16 struct DSGraph {
17   // Public data-type declarations...
18   typedef hash_map<Value*, DSNodeHandle> ScalarMapTy;
19   typedef hash_map<Function*, DSNodeHandle> ReturnNodesTy;
20   typedef hash_set<const GlobalValue*> GlobalSetTy;
21
22   /// NodeMapTy - This data type is used when cloning one graph into another to
23   /// keep track of the correspondence between the nodes in the old and new
24   /// graphs.
25   typedef hash_map<const DSNode*, DSNodeHandle> NodeMapTy;
26 private:
27   DSGraph *GlobalsGraph;   // Pointer to the common graph of global objects
28   bool PrintAuxCalls;      // Should this graph print the Aux calls vector?
29
30   std::vector<DSNode*> Nodes;
31   ScalarMapTy ScalarMap;
32
33   // ReturnNodes - A return value for every function merged into this graph.
34   // Each DSGraph may have multiple functions merged into it at any time, which
35   // is used for representing SCCs.
36   //
37   ReturnNodesTy ReturnNodes;
38
39   // FunctionCalls - This vector maintains a single entry for each call
40   // instruction in the current graph.  The first entry in the vector is the
41   // scalar that holds the return value for the call, the second is the function
42   // scalar being invoked, and the rest are pointer arguments to the function.
43   // This vector is built by the Local graph and is never modified after that.
44   //
45   std::vector<DSCallSite> FunctionCalls;
46
47   // AuxFunctionCalls - This vector contains call sites that have been processed
48   // by some mechanism.  In pratice, the BU Analysis uses this vector to hold
49   // the _unresolved_ call sites, because it cannot modify FunctionCalls.
50   //
51   std::vector<DSCallSite> AuxFunctionCalls;
52
53   // InlinedGlobals - This set records which globals have been inlined from
54   // other graphs (callers or callees, depending on the pass) into this one.
55   // 
56   GlobalSetTy InlinedGlobals;
57
58   void operator=(const DSGraph &); // DO NOT IMPLEMENT
59
60 public:
61   // Create a new, empty, DSGraph.
62   DSGraph() : GlobalsGraph(0), PrintAuxCalls(false) {}
63   DSGraph(Function &F, DSGraph *GlobalsGraph); // Compute the local DSGraph
64
65   // Copy ctor - If you want to capture the node mapping between the source and
66   // destination graph, you may optionally do this by specifying a map to record
67   // this into.
68   //
69   // Note that a copied graph does not retain the GlobalsGraph pointer of the
70   // source.  You need to set a new GlobalsGraph with the setGlobalsGraph
71   // method.
72   //
73   DSGraph(const DSGraph &DSG);
74   DSGraph(const DSGraph &DSG, NodeMapTy &NodeMap);
75   ~DSGraph();
76
77   DSGraph *getGlobalsGraph() const { return GlobalsGraph; }
78   void setGlobalsGraph(DSGraph *G) { GlobalsGraph = G; }
79
80   // setPrintAuxCalls - If you call this method, the auxillary call vector will
81   // be printed instead of the standard call vector to the dot file.
82   //
83   void setPrintAuxCalls() { PrintAuxCalls = true; }
84   bool shouldPrintAuxCalls() const { return PrintAuxCalls; }
85
86   /// getNodes - Get a vector of all the nodes in the graph
87   /// 
88   const std::vector<DSNode*> &getNodes() const { return Nodes; }
89         std::vector<DSNode*> &getNodes()       { return Nodes; }
90
91   /// getFunctionNames - Return a space separated list of the name of the
92   /// functions in this graph (if any)
93   std::string getFunctionNames() const;
94
95   /// addNode - Add a new node to the graph.
96   ///
97   void addNode(DSNode *N) { Nodes.push_back(N); }
98
99   /// getScalarMap - Get a map that describes what the nodes the scalars in this
100   /// function point to...
101   ///
102   ScalarMapTy &getScalarMap() { return ScalarMap; }
103   const ScalarMapTy &getScalarMap() const {return ScalarMap;}
104
105   /// getFunctionCalls - Return the list of call sites in the original local
106   /// graph...
107   ///
108   const std::vector<DSCallSite> &getFunctionCalls() const {
109     return FunctionCalls;
110   }
111
112   /// getAuxFunctionCalls - Get the call sites as modified by whatever passes
113   /// have been run.
114   ///
115   std::vector<DSCallSite> &getAuxFunctionCalls() {
116     return AuxFunctionCalls;
117   }
118   const std::vector<DSCallSite> &getAuxFunctionCalls() const {
119     return AuxFunctionCalls;
120   }
121
122   /// getInlinedGlobals - Get the set of globals that are have been inlined
123   /// (from callees in BU or from callers in TD) into the current graph.
124   ///
125   GlobalSetTy& getInlinedGlobals() {
126     return InlinedGlobals;
127   }
128
129   /// getNodeForValue - Given a value that is used or defined in the body of the
130   /// current function, return the DSNode that it points to.
131   ///
132   DSNodeHandle &getNodeForValue(Value *V) { return ScalarMap[V]; }
133
134   const DSNodeHandle &getNodeForValue(Value *V) const {
135     ScalarMapTy::const_iterator I = ScalarMap.find(V);
136     assert(I != ScalarMap.end() &&
137            "Use non-const lookup function if node may not be in the map");
138     return I->second;
139   }
140
141   /// getReturnNodes - Return the mapping of functions to their return nodes for
142   /// this graph.
143   const ReturnNodesTy &getReturnNodes() const { return ReturnNodes; }
144         ReturnNodesTy &getReturnNodes()       { return ReturnNodes; }
145
146   /// getReturnNodeFor - Return the return node for the specified function.
147   ///
148   DSNodeHandle &getReturnNodeFor(Function &F) {
149     ReturnNodesTy::iterator I = ReturnNodes.find(&F);
150     assert(I != ReturnNodes.end() && "F not in this DSGraph!");
151     return I->second;
152   }
153
154   const DSNodeHandle &getReturnNodeFor(Function &F) const {
155     ReturnNodesTy::const_iterator I = ReturnNodes.find(&F);
156     assert(I != ReturnNodes.end() && "F not in this DSGraph!");
157     return I->second;
158   }
159
160   /// getGraphSize - Return the number of nodes in this graph.
161   ///
162   unsigned getGraphSize() const {
163     return Nodes.size();
164   }
165
166   /// print - Print a dot graph to the specified ostream...
167   ///
168   void print(std::ostream &O) const;
169
170   /// dump - call print(std::cerr), for use from the debugger...
171   ///
172   void dump() const;
173
174   /// viewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
175   /// then cleanup.  For use from the debugger.
176   void viewGraph() const;
177
178   void writeGraphToFile(std::ostream &O, const std::string &GraphName) const;
179
180   /// maskNodeTypes - Apply a mask to all of the node types in the graph.  This
181   /// is useful for clearing out markers like Incomplete.
182   ///
183   void maskNodeTypes(unsigned Mask) {
184     for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
185       Nodes[i]->maskNodeTypes(Mask);
186   }
187   void maskIncompleteMarkers() { maskNodeTypes(~DSNode::Incomplete); }
188
189   // markIncompleteNodes - Traverse the graph, identifying nodes that may be
190   // modified by other functions that have not been resolved yet.  This marks
191   // nodes that are reachable through three sources of "unknownness":
192   //   Global Variables, Function Calls, and Incoming Arguments
193   //
194   // For any node that may have unknown components (because something outside
195   // the scope of current analysis may have modified it), the 'Incomplete' flag
196   // is added to the NodeType.
197   //
198   enum MarkIncompleteFlags {
199     MarkFormalArgs = 1, IgnoreFormalArgs = 0,
200     IgnoreGlobals = 2, MarkGlobalsIncomplete = 0,
201   };
202   void markIncompleteNodes(unsigned Flags);
203
204   // removeDeadNodes - Use a reachability analysis to eliminate subgraphs that
205   // are unreachable.  This often occurs because the data structure doesn't
206   // "escape" into it's caller, and thus should be eliminated from the caller's
207   // graph entirely.  This is only appropriate to use when inlining graphs.
208   //
209   enum RemoveDeadNodesFlags {
210     RemoveUnreachableGlobals = 1, KeepUnreachableGlobals = 0,
211   };
212   void removeDeadNodes(unsigned Flags);
213
214   /// CloneFlags enum - Bits that may be passed into the cloneInto method to
215   /// specify how to clone the function graph.
216   enum CloneFlags {
217     StripAllocaBit        = 1 << 0, KeepAllocaBit     = 0,
218     DontCloneCallNodes    = 1 << 1, CloneCallNodes    = 0,
219     DontCloneAuxCallNodes = 1 << 2, CloneAuxCallNodes = 0,
220     StripModRefBits       = 1 << 3, KeepModRefBits    = 0,
221     StripIncompleteBit    = 1 << 4, KeepIncompleteBit = 0,
222   };
223
224 private:
225   void cloneReachableNodes(const DSNode*  Node,
226                            unsigned BitsToClear,
227                            NodeMapTy& OldNodeMap,
228                            NodeMapTy& CompletedNodeMap);
229
230 public:
231   void updateFromGlobalGraph();
232
233   void cloneReachableSubgraph(const DSGraph& G,
234                               const hash_set<const DSNode*>& RootNodes,
235                               NodeMapTy& OldNodeMap,
236                               NodeMapTy& CompletedNodeMap,
237                               unsigned CloneFlags = 0);
238
239   /// cloneInto - Clone the specified DSGraph into the current graph.  The
240   /// translated ScalarMap for the old function is filled into the OldValMap
241   /// member, and the translated ReturnNodes map is returned into ReturnNodes.
242   ///
243   /// The CloneFlags member controls various aspects of the cloning process.
244   ///
245   void cloneInto(const DSGraph &G, ScalarMapTy &OldValMap,
246                  ReturnNodesTy &OldReturnNodes, NodeMapTy &OldNodeMap,
247                  unsigned CloneFlags = 0);
248
249   /// mergeInGraph - The method is used for merging graphs together.  If the
250   /// argument graph is not *this, it makes a clone of the specified graph, then
251   /// merges the nodes specified in the call site with the formal arguments in
252   /// the graph.  If the StripAlloca's argument is 'StripAllocaBit' then Alloca
253   /// markers are removed from nodes.
254   ///
255   void mergeInGraph(const DSCallSite &CS, Function &F, const DSGraph &Graph,
256                     unsigned CloneFlags);
257
258
259   /// getCallSiteForArguments - Get the arguments and return value bindings for
260   /// the specified function in the current graph.
261   ///
262   DSCallSite getCallSiteForArguments(Function &F) const;
263
264   // Methods for checking to make sure graphs are well formed...
265   void AssertNodeInGraph(const DSNode *N) const {
266     assert((!N || find(Nodes.begin(), Nodes.end(), N) != Nodes.end()) &&
267            "AssertNodeInGraph: Node is not in graph!");
268   }
269   void AssertNodeContainsGlobal(const DSNode *N, GlobalValue *GV) const {
270     assert(std::find(N->getGlobals().begin(), N->getGlobals().end(), GV) !=
271            N->getGlobals().end() && "Global value not in node!");
272   }
273
274   void AssertCallSiteInGraph(const DSCallSite &CS) const {
275     if (CS.isIndirectCall())
276       AssertNodeInGraph(CS.getCalleeNode());
277     AssertNodeInGraph(CS.getRetVal().getNode());
278     for (unsigned j = 0, e = CS.getNumPtrArgs(); j != e; ++j)
279       AssertNodeInGraph(CS.getPtrArg(j).getNode());
280   }
281
282   void AssertCallNodesInGraph() const {
283     for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i)
284       AssertCallSiteInGraph(FunctionCalls[i]);
285   }
286   void AssertAuxCallNodesInGraph() const {
287     for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i)
288       AssertCallSiteInGraph(AuxFunctionCalls[i]);
289   }
290
291   void AssertGraphOK() const;
292
293   /// mergeInGlobalsGraph - This method is useful for clients to incorporate the
294   /// globals graph into the DS, BU or TD graph for a function.  This code
295   /// retains all globals, i.e., does not delete unreachable globals after they
296   /// are inlined.
297   ///
298   void mergeInGlobalsGraph();
299
300   /// removeTriviallyDeadNodes - After the graph has been constructed, this
301   /// method removes all unreachable nodes that are created because they got
302   /// merged with other nodes in the graph.  This is used as the first step of
303   /// removeDeadNodes.
304   ///
305   void removeTriviallyDeadNodes();
306 };
307
308 #endif