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"
13 //===----------------------------------------------------------------------===//
14 /// DSGraph - The graph that represents a function.
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;
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
25 typedef hash_map<const DSNode*, DSNodeHandle> NodeMapTy;
27 DSGraph *GlobalsGraph; // Pointer to the common graph of global objects
28 bool PrintAuxCalls; // Should this graph print the Aux calls vector?
30 std::vector<DSNode*> Nodes;
31 ScalarMapTy ScalarMap;
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.
37 ReturnNodesTy ReturnNodes;
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.
45 std::vector<DSCallSite> FunctionCalls;
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.
51 std::vector<DSCallSite> AuxFunctionCalls;
53 // InlinedGlobals - This set records which globals have been inlined from
54 // other graphs (callers or callees, depending on the pass) into this one.
56 GlobalSetTy InlinedGlobals;
58 void operator=(const DSGraph &); // DO NOT IMPLEMENT
61 // Create a new, empty, DSGraph.
62 DSGraph() : GlobalsGraph(0), PrintAuxCalls(false) {}
63 DSGraph(Function &F, DSGraph *GlobalsGraph); // Compute the local DSGraph
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
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
73 DSGraph(const DSGraph &DSG);
74 DSGraph(const DSGraph &DSG, NodeMapTy &NodeMap);
77 DSGraph *getGlobalsGraph() const { return GlobalsGraph; }
78 void setGlobalsGraph(DSGraph *G) { GlobalsGraph = G; }
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.
83 void setPrintAuxCalls() { PrintAuxCalls = true; }
84 bool shouldPrintAuxCalls() const { return PrintAuxCalls; }
86 /// getNodes - Get a vector of all the nodes in the graph
88 const std::vector<DSNode*> &getNodes() const { return Nodes; }
89 std::vector<DSNode*> &getNodes() { return Nodes; }
91 /// getFunctionNames - Return a space separated list of the name of the
92 /// functions in this graph (if any)
93 std::string getFunctionNames() const;
95 /// addNode - Add a new node to the graph.
97 void addNode(DSNode *N) { Nodes.push_back(N); }
99 /// getScalarMap - Get a map that describes what the nodes the scalars in this
100 /// function point to...
102 ScalarMapTy &getScalarMap() { return ScalarMap; }
103 const ScalarMapTy &getScalarMap() const { return ScalarMap; }
105 /// getFunctionCalls - Return the list of call sites in the original local
108 const std::vector<DSCallSite> &getFunctionCalls() const {
109 return FunctionCalls;
112 /// getAuxFunctionCalls - Get the call sites as modified by whatever passes
115 std::vector<DSCallSite> &getAuxFunctionCalls() {
116 return AuxFunctionCalls;
118 const std::vector<DSCallSite> &getAuxFunctionCalls() const {
119 return AuxFunctionCalls;
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.
125 GlobalSetTy& getInlinedGlobals() {
126 return InlinedGlobals;
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.
132 DSNodeHandle &getNodeForValue(Value *V) { return ScalarMap[V]; }
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");
141 /// getReturnNodes - Return the mapping of functions to their return nodes for
143 const ReturnNodesTy &getReturnNodes() const { return ReturnNodes; }
144 ReturnNodesTy &getReturnNodes() { return ReturnNodes; }
146 /// getReturnNodeFor - Return the return node for the specified function.
148 DSNodeHandle &getReturnNodeFor(Function &F) {
149 ReturnNodesTy::iterator I = ReturnNodes.find(&F);
150 assert(I != ReturnNodes.end() && "F not in this DSGraph!");
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!");
160 /// getGraphSize - Return the number of nodes in this graph.
162 unsigned getGraphSize() const {
166 /// print - Print a dot graph to the specified ostream...
168 void print(std::ostream &O) const;
170 /// dump - call print(std::cerr), for use from the debugger...
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;
178 void writeGraphToFile(std::ostream &O, const std::string &GraphName) const;
180 /// maskNodeTypes - Apply a mask to all of the node types in the graph. This
181 /// is useful for clearing out markers like Incomplete.
183 void maskNodeTypes(unsigned Mask) {
184 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
185 Nodes[i]->maskNodeTypes(Mask);
187 void maskIncompleteMarkers() { maskNodeTypes(~DSNode::Incomplete); }
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
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.
198 enum MarkIncompleteFlags {
199 MarkFormalArgs = 1, IgnoreFormalArgs = 0,
200 IgnoreGlobals = 2, MarkGlobalsIncomplete = 0,
202 void markIncompleteNodes(unsigned Flags);
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.
209 enum RemoveDeadNodesFlags {
210 RemoveUnreachableGlobals = 1, KeepUnreachableGlobals = 0,
212 void removeDeadNodes(unsigned Flags);
214 /// CloneFlags enum - Bits that may be passed into the cloneInto method to
215 /// specify how to clone the function graph.
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,
225 void cloneReachableNodes(const DSNode* Node,
226 unsigned BitsToClear,
227 NodeMapTy& OldNodeMap,
228 NodeMapTy& CompletedNodeMap);
231 void updateFromGlobalGraph();
233 void cloneReachableSubgraph(const DSGraph& G,
234 const hash_set<const DSNode*>& RootNodes,
235 NodeMapTy& OldNodeMap,
236 NodeMapTy& CompletedNodeMap,
237 unsigned CloneFlags = 0);
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.
243 /// The CloneFlags member controls various aspects of the cloning process.
245 void cloneInto(const DSGraph &G, ScalarMapTy &OldValMap,
246 ReturnNodesTy &OldReturnNodes, NodeMapTy &OldNodeMap,
247 unsigned CloneFlags = 0);
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.
255 void mergeInGraph(const DSCallSite &CS, Function &F, const DSGraph &Graph,
256 unsigned CloneFlags);
259 /// getCallSiteForArguments - Get the arguments and return value bindings for
260 /// the specified function in the current graph.
262 DSCallSite getCallSiteForArguments(Function &F) const;
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!");
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!");
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());
282 void AssertCallNodesInGraph() const {
283 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i)
284 AssertCallSiteInGraph(FunctionCalls[i]);
286 void AssertAuxCallNodesInGraph() const {
287 for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i)
288 AssertCallSiteInGraph(AuxFunctionCalls[i]);
291 void AssertGraphOK() const;
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
298 void mergeInGlobalsGraph();
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
305 void removeTriviallyDeadNodes();