eebefa5f5dd6dd451d21e16c20b2d6d1283e1fe9
[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   bool PrintAuxCalls;      // Should this graph print the Aux calls vector?
19
20   DSNodeHandle RetNode;    // The node that gets returned...
21   std::vector<DSNode*> Nodes;
22   hash_map<Value*, DSNodeHandle> ScalarMap;
23
24   // FunctionCalls - This vector maintains a single entry for each call
25   // instruction in the current graph.  The first entry in the vector is the
26   // scalar that holds the return value for the call, the second is the function
27   // scalar being invoked, and the rest are pointer arguments to the function.
28   // This vector is built by the Local graph and is never modified after that.
29   //
30   std::vector<DSCallSite> FunctionCalls;
31
32   // AuxFunctionCalls - This vector contains call sites that have been processed
33   // by some mechanism.  In pratice, the BU Analysis uses this vector to hold
34   // the _unresolved_ call sites, because it cannot modify FunctionCalls.
35   //
36   std::vector<DSCallSite> AuxFunctionCalls;
37
38   void operator=(const DSGraph &); // DO NOT IMPLEMENT
39 public:
40   // Create a new, empty, DSGraph.
41   DSGraph() : Func(0), GlobalsGraph(0), PrintAuxCalls(false) {}
42   DSGraph(Function &F, DSGraph *GlobalsGraph); // Compute the local DSGraph
43
44   // Copy ctor - If you want to capture the node mapping between the source and
45   // destination graph, you may optionally do this by specifying a map to record
46   // this into.
47   //
48   // Note that a copied graph does not retain the GlobalsGraph pointer of the
49   // source.  You need to set a new GlobalsGraph with the setGlobalsGraph
50   // method.
51   //
52   DSGraph(const DSGraph &DSG);
53   DSGraph(const DSGraph &DSG, hash_map<const DSNode*, DSNodeHandle> &NodeMap);
54   ~DSGraph();
55
56   bool hasFunction() const { return Func != 0; }
57   Function &getFunction() const {
58     assert(hasFunction() && "Cannot call getFunction on graph without a fn!");
59     return *Func;
60   }
61
62   DSGraph *getGlobalsGraph() const { return GlobalsGraph; }
63   void setGlobalsGraph(DSGraph *G) { GlobalsGraph = G; }
64
65   // setPrintAuxCalls - If you call this method, the auxillary call vector will
66   // be printed instead of the standard call vector to the dot file.
67   //
68   void setPrintAuxCalls() { PrintAuxCalls = true; }
69   bool shouldPrintAuxCalls() const { return PrintAuxCalls; }
70
71   /// getNodes - Get a vector of all the nodes in the graph
72   /// 
73   const std::vector<DSNode*> &getNodes() const { return Nodes; }
74         std::vector<DSNode*> &getNodes()       { return Nodes; }
75
76   /// addNode - Add a new node to the graph.
77   ///
78   void addNode(DSNode *N) { Nodes.push_back(N); }
79
80   /// getScalarMap - Get a map that describes what the nodes the scalars in this
81   /// function point to...
82   ///
83   hash_map<Value*, DSNodeHandle> &getScalarMap() { return ScalarMap; }
84   const hash_map<Value*, DSNodeHandle> &getScalarMap() const {return ScalarMap;}
85
86   /// getFunctionCalls - Return the list of call sites in the original local
87   /// graph...
88   ///
89   const std::vector<DSCallSite> &getFunctionCalls() const {
90     return FunctionCalls;
91   }
92
93   /// getAuxFunctionCalls - Get the call sites as modified by whatever passes
94   /// have been run.
95   ///
96   std::vector<DSCallSite> &getAuxFunctionCalls() {
97     return AuxFunctionCalls;
98   }
99   const std::vector<DSCallSite> &getAuxFunctionCalls() const {
100     return AuxFunctionCalls;
101   }
102
103   /// getNodeForValue - Given a value that is used or defined in the body of the
104   /// current function, return the DSNode that it points to.
105   ///
106   DSNodeHandle &getNodeForValue(Value *V) { return ScalarMap[V]; }
107
108   const DSNodeHandle &getNodeForValue(Value *V) const {
109     hash_map<Value*, DSNodeHandle>::const_iterator I = ScalarMap.find(V);
110     assert(I != ScalarMap.end() &&
111            "Use non-const lookup function if node may not be in the map");
112     return I->second;
113   }
114
115   const DSNodeHandle &getRetNode() const { return RetNode; }
116         DSNodeHandle &getRetNode()       { return RetNode; }
117
118   unsigned getGraphSize() const {
119     return Nodes.size();
120   }
121
122   /// print - Print a dot graph to the specified ostream...
123   void print(std::ostream &O) const;
124
125   /// dump - call print(std::cerr), for use from the debugger...
126   ///
127   void dump() const;
128
129   /// viewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
130   /// then cleanup.  For use from the debugger.
131   void viewGraph() const;
132
133   void writeGraphToFile(std::ostream &O, const std::string &GraphName) const;
134
135   /// maskNodeTypes - Apply a mask to all of the node types in the graph.  This
136   /// is useful for clearing out markers like Incomplete.
137   ///
138   void maskNodeTypes(unsigned char Mask) {
139     for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
140       Nodes[i]->NodeType &= Mask;
141   }
142   void maskIncompleteMarkers() { maskNodeTypes(~DSNode::Incomplete); }
143
144   // markIncompleteNodes - Traverse the graph, identifying nodes that may be
145   // modified by other functions that have not been resolved yet.  This marks
146   // nodes that are reachable through three sources of "unknownness":
147   //   Global Variables, Function Calls, and Incoming Arguments
148   //
149   // For any node that may have unknown components (because something outside
150   // the scope of current analysis may have modified it), the 'Incomplete' flag
151   // is added to the NodeType.
152   //
153   enum MarkIncompleteFlags {
154     MarkFormalArgs = 1, IgnoreFormalArgs = 0,
155     IgnoreGlobals = 2, MarkGlobalsIncomplete = 0,
156   };
157   void markIncompleteNodes(unsigned Flags);
158
159   // removeDeadNodes - Use a reachability analysis to eliminate subgraphs that
160   // are unreachable.  This often occurs because the data structure doesn't
161   // "escape" into it's caller, and thus should be eliminated from the caller's
162   // graph entirely.  This is only appropriate to use when inlining graphs.
163   //
164   enum RemoveDeadNodesFlags {
165     RemoveUnreachableGlobals = 1, KeepUnreachableGlobals = 0,
166   };
167   void removeDeadNodes(unsigned Flags);
168
169   // CloneFlags enum - Bits that may be passed into the cloneInto method to
170   // specify how to clone the function graph.
171   enum CloneFlags {
172     StripAllocaBit        = 1 << 0, KeepAllocaBit     = 0 << 0,
173     DontCloneCallNodes    = 1 << 1, CloneCallNodes    = 0 << 0,
174     DontCloneAuxCallNodes = 1 << 2, CloneAuxCallNodes = 0 << 0,
175     StripModRefBits       = 1 << 3, KeepModRefBits    = 0 << 0,
176   };
177
178   // cloneInto - Clone the specified DSGraph into the current graph, returning
179   // the Return node of the graph.  The translated ScalarMap for the old
180   // function is filled into the OldValMap member.  If StripAllocas is set to
181   // 'StripAllocaBit', Alloca markers are removed from the graph as the graph is
182   // being cloned.
183   //
184   DSNodeHandle cloneInto(const DSGraph &G,
185                          hash_map<Value*, DSNodeHandle> &OldValMap,
186                          hash_map<const DSNode*, DSNodeHandle> &OldNodeMap,
187                          unsigned CloneFlags = 0);
188
189   /// mergeInGraph - The method is used for merging graphs together.  If the
190   /// argument graph is not *this, it makes a clone of the specified graph, then
191   /// merges the nodes specified in the call site with the formal arguments in
192   /// the graph.  If the StripAlloca's argument is 'StripAllocaBit' then Alloca
193   /// markers are removed from nodes.
194   ///
195   void mergeInGraph(DSCallSite &CS, const DSGraph &Graph, unsigned CloneFlags);
196
197   // Methods for checking to make sure graphs are well formed...
198   void AssertNodeInGraph(const DSNode *N) const {
199     assert((!N || find(Nodes.begin(), Nodes.end(), N) != Nodes.end()) &&
200            "AssertNodeInGraph: Node is not in graph!");
201   }
202   void AssertNodeContainsGlobal(const DSNode *N, GlobalValue *GV) const {
203     assert(std::find(N->getGlobals().begin(), N->getGlobals().end(), GV) !=
204            N->getGlobals().end() && "Global value not in node!");
205   }
206
207   void AssertCallSiteInGraph(const DSCallSite &CS) const {
208     if (CS.isIndirectCall())
209       AssertNodeInGraph(CS.getCalleeNode());
210     AssertNodeInGraph(CS.getRetVal().getNode());
211     for (unsigned j = 0, e = CS.getNumPtrArgs(); j != e; ++j)
212       AssertNodeInGraph(CS.getPtrArg(j).getNode());
213   }
214
215   void AssertCallNodesInGraph() const {
216     for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i)
217       AssertCallSiteInGraph(FunctionCalls[i]);
218   }
219   void AssertAuxCallNodesInGraph() const {
220     for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i)
221       AssertCallSiteInGraph(AuxFunctionCalls[i]);
222   }
223
224   void AssertGraphOK() const;
225
226 public:
227   // removeTriviallyDeadNodes - After the graph has been constructed, this
228   // method removes all unreachable nodes that are created because they got
229   // merged with other nodes in the graph.  This is used as the first step of
230   // removeDeadNodes.
231   //
232   void removeTriviallyDeadNodes();
233 };
234
235 #endif