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