1 //===- DSGraph.h - Represent a collection of data structures ----*- C++ -*-===//
3 // This header defines the data structure graph.
5 //===----------------------------------------------------------------------===//
7 #ifndef LLVM_ANALYSIS_DSGRAPH_H
8 #define LLVM_ANALYSIS_DSGRAPH_H
10 #include "llvm/Analysis/DSNode.h"
12 //===----------------------------------------------------------------------===//
13 /// DSGraph - The graph that represents a function.
16 // Public data-type declarations...
17 typedef hash_map<Value*, DSNodeHandle> ScalarMapTy;
18 typedef hash_map<Function*, DSNodeHandle> ReturnNodesTy;
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
23 typedef hash_map<const DSNode*, DSNodeHandle> NodeMapTy;
25 DSGraph *GlobalsGraph; // Pointer to the common graph of global objects
26 bool PrintAuxCalls; // Should this graph print the Aux calls vector?
28 std::vector<DSNode*> Nodes;
29 ScalarMapTy ScalarMap;
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.
35 ReturnNodesTy ReturnNodes;
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.
43 std::vector<DSCallSite> FunctionCalls;
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.
49 std::vector<DSCallSite> AuxFunctionCalls;
51 void operator=(const DSGraph &); // DO NOT IMPLEMENT
53 // Create a new, empty, DSGraph.
54 DSGraph() : GlobalsGraph(0), PrintAuxCalls(false) {}
55 DSGraph(Function &F, DSGraph *GlobalsGraph); // Compute the local DSGraph
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
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
65 DSGraph(const DSGraph &DSG);
66 DSGraph(const DSGraph &DSG, NodeMapTy &NodeMap);
69 DSGraph *getGlobalsGraph() const { return GlobalsGraph; }
70 void setGlobalsGraph(DSGraph *G) { GlobalsGraph = G; }
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.
75 void setPrintAuxCalls() { PrintAuxCalls = true; }
76 bool shouldPrintAuxCalls() const { return PrintAuxCalls; }
78 /// getNodes - Get a vector of all the nodes in the graph
80 const std::vector<DSNode*> &getNodes() const { return Nodes; }
81 std::vector<DSNode*> &getNodes() { return Nodes; }
83 /// addNode - Add a new node to the graph.
85 void addNode(DSNode *N) { Nodes.push_back(N); }
87 /// getScalarMap - Get a map that describes what the nodes the scalars in this
88 /// function point to...
90 ScalarMapTy &getScalarMap() { return ScalarMap; }
91 const ScalarMapTy &getScalarMap() const {return ScalarMap;}
93 /// getFunctionCalls - Return the list of call sites in the original local
96 const std::vector<DSCallSite> &getFunctionCalls() const {
100 /// getAuxFunctionCalls - Get the call sites as modified by whatever passes
103 std::vector<DSCallSite> &getAuxFunctionCalls() {
104 return AuxFunctionCalls;
106 const std::vector<DSCallSite> &getAuxFunctionCalls() const {
107 return AuxFunctionCalls;
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.
113 DSNodeHandle &getNodeForValue(Value *V) { return ScalarMap[V]; }
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");
122 /// getReturnNodes - Return the mapping of functions to their return nodes for
124 const ReturnNodesTy &getReturnNodes() const { return ReturnNodes; }
125 ReturnNodesTy &getReturnNodes() { return ReturnNodes; }
127 /// getReturnNodeFor - Return the return node for the specified function.
129 DSNodeHandle &getReturnNodeFor(Function &F) {
130 ReturnNodesTy::iterator I = ReturnNodes.find(&F);
131 assert(I != ReturnNodes.end() && "F not in this DSGraph!");
135 /// getGraphSize - Return the number of nodes in this graph.
137 unsigned getGraphSize() const {
141 /// print - Print a dot graph to the specified ostream...
143 void print(std::ostream &O) const;
145 /// dump - call print(std::cerr), for use from the debugger...
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;
153 void writeGraphToFile(std::ostream &O, const std::string &GraphName) const;
155 /// maskNodeTypes - Apply a mask to all of the node types in the graph. This
156 /// is useful for clearing out markers like Incomplete.
158 void maskNodeTypes(unsigned Mask) {
159 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
160 Nodes[i]->maskNodeTypes(Mask);
162 void maskIncompleteMarkers() { maskNodeTypes(~DSNode::Incomplete); }
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
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.
173 enum MarkIncompleteFlags {
174 MarkFormalArgs = 1, IgnoreFormalArgs = 0,
175 IgnoreGlobals = 2, MarkGlobalsIncomplete = 0,
177 void markIncompleteNodes(unsigned Flags);
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.
184 enum RemoveDeadNodesFlags {
185 RemoveUnreachableGlobals = 1, KeepUnreachableGlobals = 0,
187 void removeDeadNodes(unsigned Flags);
189 /// CloneFlags enum - Bits that may be passed into the cloneInto method to
190 /// specify how to clone the function graph.
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,
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.
202 /// The CloneFlags member controls various aspects of the cloning process.
204 void cloneInto(const DSGraph &G, ScalarMapTy &OldValMap,
205 ReturnNodesTy &OldReturnNodes, NodeMapTy &OldNodeMap,
206 unsigned CloneFlags = 0);
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.
214 void mergeInGraph(DSCallSite &CS, Function &F, const DSGraph &Graph,
215 unsigned CloneFlags);
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!");
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!");
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());
235 void AssertCallNodesInGraph() const {
236 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i)
237 AssertCallSiteInGraph(FunctionCalls[i]);
239 void AssertAuxCallNodesInGraph() const {
240 for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i)
241 AssertCallSiteInGraph(AuxFunctionCalls[i]);
244 void AssertGraphOK() const;
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
252 void removeTriviallyDeadNodes();