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 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?
20 DSNodeHandle RetNode; // The node that gets returned...
21 std::vector<DSNode*> Nodes;
22 hash_map<Value*, DSNodeHandle> ScalarMap;
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.
30 std::vector<DSCallSite> FunctionCalls;
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.
36 std::vector<DSCallSite> AuxFunctionCalls;
38 void operator=(const DSGraph &); // DO NOT IMPLEMENT
40 // Create a new, empty, DSGraph.
41 DSGraph() : Func(0), GlobalsGraph(0), PrintAuxCalls(false) {}
42 DSGraph(Function &F, DSGraph *GlobalsGraph); // Compute the local DSGraph
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
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
52 DSGraph(const DSGraph &DSG);
53 DSGraph(const DSGraph &DSG, hash_map<const DSNode*, DSNodeHandle> &NodeMap);
56 bool hasFunction() const { return Func != 0; }
57 Function &getFunction() const {
58 assert(hasFunction() && "Cannot call getFunction on graph without a fn!");
62 DSGraph *getGlobalsGraph() const { return GlobalsGraph; }
63 void setGlobalsGraph(DSGraph *G) { GlobalsGraph = G; }
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.
68 void setPrintAuxCalls() { PrintAuxCalls = true; }
69 bool shouldPrintAuxCalls() const { return PrintAuxCalls; }
71 /// getNodes - Get a vector of all the nodes in the graph
73 const std::vector<DSNode*> &getNodes() const { return Nodes; }
74 std::vector<DSNode*> &getNodes() { return Nodes; }
76 /// addNode - Add a new node to the graph.
78 void addNode(DSNode *N) { Nodes.push_back(N); }
80 /// getScalarMap - Get a map that describes what the nodes the scalars in this
81 /// function point to...
83 hash_map<Value*, DSNodeHandle> &getScalarMap() { return ScalarMap; }
84 const hash_map<Value*, DSNodeHandle> &getScalarMap() const {return ScalarMap;}
86 /// getFunctionCalls - Return the list of call sites in the original local
89 const std::vector<DSCallSite> &getFunctionCalls() const {
93 /// getAuxFunctionCalls - Get the call sites as modified by whatever passes
96 std::vector<DSCallSite> &getAuxFunctionCalls() {
97 return AuxFunctionCalls;
99 const std::vector<DSCallSite> &getAuxFunctionCalls() const {
100 return AuxFunctionCalls;
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.
106 DSNodeHandle &getNodeForValue(Value *V) { return ScalarMap[V]; }
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");
115 const DSNodeHandle &getRetNode() const { return RetNode; }
116 DSNodeHandle &getRetNode() { return RetNode; }
118 unsigned getGraphSize() const {
122 /// print - Print a dot graph to the specified ostream...
123 void print(std::ostream &O) const;
125 /// dump - call print(std::cerr), for use from the debugger...
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;
133 void writeGraphToFile(std::ostream &O, const std::string &GraphName) const;
135 /// maskNodeTypes - Apply a mask to all of the node types in the graph. This
136 /// is useful for clearing out markers like Incomplete.
138 void maskNodeTypes(unsigned char Mask) {
139 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
140 Nodes[i]->NodeType &= Mask;
142 void maskIncompleteMarkers() { maskNodeTypes(~DSNode::Incomplete); }
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
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.
153 enum MarkIncompleteFlags {
154 MarkFormalArgs = 1, IgnoreFormalArgs = 0,
155 IgnoreGlobals = 2, MarkGlobalsIncomplete = 0,
157 void markIncompleteNodes(unsigned Flags);
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.
164 enum RemoveDeadNodesFlags {
165 RemoveUnreachableGlobals = 1, KeepUnreachableGlobals = 0,
167 void removeDeadNodes(unsigned Flags);
169 // CloneFlags enum - Bits that may be passed into the cloneInto method to
170 // specify how to clone the function graph.
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,
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
184 DSNodeHandle cloneInto(const DSGraph &G,
185 hash_map<Value*, DSNodeHandle> &OldValMap,
186 hash_map<const DSNode*, DSNodeHandle> &OldNodeMap,
187 unsigned CloneFlags = 0);
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.
195 void mergeInGraph(DSCallSite &CS, const DSGraph &Graph, unsigned CloneFlags);
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!");
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!");
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());
215 void AssertCallNodesInGraph() const {
216 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i)
217 AssertCallSiteInGraph(FunctionCalls[i]);
219 void AssertAuxCallNodesInGraph() const {
220 for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i)
221 AssertCallSiteInGraph(AuxFunctionCalls[i]);
224 void AssertGraphOK() const;
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
232 void removeTriviallyDeadNodes();