1 //===- DSGraph.h - Represent a collection of data structures ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header defines the data structure graph.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_DSGRAPH_H
15 #define LLVM_ANALYSIS_DSGRAPH_H
17 #include "llvm/Analysis/DSNode.h"
20 //===----------------------------------------------------------------------===//
21 /// DSGraph - The graph that represents a function.
24 // Public data-type declarations...
25 typedef hash_map<Value*, DSNodeHandle> ScalarMapTy;
26 typedef hash_map<Function*, DSNodeHandle> ReturnNodesTy;
27 typedef hash_set<const GlobalValue*> GlobalSetTy;
29 /// NodeMapTy - This data type is used when cloning one graph into another to
30 /// keep track of the correspondence between the nodes in the old and new
32 typedef hash_map<const DSNode*, DSNodeHandle> NodeMapTy;
34 DSGraph *GlobalsGraph; // Pointer to the common graph of global objects
35 bool PrintAuxCalls; // Should this graph print the Aux calls vector?
37 std::vector<DSNode*> Nodes;
38 ScalarMapTy ScalarMap;
40 // ReturnNodes - A return value for every function merged into this graph.
41 // Each DSGraph may have multiple functions merged into it at any time, which
42 // is used for representing SCCs.
44 ReturnNodesTy ReturnNodes;
46 // FunctionCalls - This vector maintains a single entry for each call
47 // instruction in the current graph. The first entry in the vector is the
48 // scalar that holds the return value for the call, the second is the function
49 // scalar being invoked, and the rest are pointer arguments to the function.
50 // This vector is built by the Local graph and is never modified after that.
52 std::vector<DSCallSite> FunctionCalls;
54 // AuxFunctionCalls - This vector contains call sites that have been processed
55 // by some mechanism. In pratice, the BU Analysis uses this vector to hold
56 // the _unresolved_ call sites, because it cannot modify FunctionCalls.
58 std::vector<DSCallSite> AuxFunctionCalls;
60 // InlinedGlobals - This set records which globals have been inlined from
61 // other graphs (callers or callees, depending on the pass) into this one.
63 GlobalSetTy InlinedGlobals;
65 /// TD - This is the target data object for the machine this graph is
69 void operator=(const DSGraph &); // DO NOT IMPLEMENT
72 // Create a new, empty, DSGraph.
73 DSGraph(const TargetData &td)
74 : GlobalsGraph(0), PrintAuxCalls(false), TD(td) {}
76 // Compute the local DSGraph
77 DSGraph(const TargetData &td, Function &F, DSGraph *GlobalsGraph);
79 // Copy ctor - If you want to capture the node mapping between the source and
80 // destination graph, you may optionally do this by specifying a map to record
83 // Note that a copied graph does not retain the GlobalsGraph pointer of the
84 // source. You need to set a new GlobalsGraph with the setGlobalsGraph
87 DSGraph(const DSGraph &DSG);
88 DSGraph(const DSGraph &DSG, NodeMapTy &NodeMap);
91 DSGraph *getGlobalsGraph() const { return GlobalsGraph; }
92 void setGlobalsGraph(DSGraph *G) { GlobalsGraph = G; }
94 /// getTargetData - Return the TargetData object for the current target.
96 const TargetData &getTargetData() const { return TD; }
98 /// setPrintAuxCalls - If you call this method, the auxillary call vector will
99 /// be printed instead of the standard call vector to the dot file.
101 void setPrintAuxCalls() { PrintAuxCalls = true; }
102 bool shouldPrintAuxCalls() const { return PrintAuxCalls; }
104 /// getNodes - Get a vector of all the nodes in the graph
106 const std::vector<DSNode*> &getNodes() const { return Nodes; }
107 std::vector<DSNode*> &getNodes() { return Nodes; }
109 /// getFunctionNames - Return a space separated list of the name of the
110 /// functions in this graph (if any)
111 std::string getFunctionNames() const;
113 /// addNode - Add a new node to the graph.
115 void addNode(DSNode *N) { Nodes.push_back(N); }
117 /// getScalarMap - Get a map that describes what the nodes the scalars in this
118 /// function point to...
120 ScalarMapTy &getScalarMap() { return ScalarMap; }
121 const ScalarMapTy &getScalarMap() const { return ScalarMap; }
123 /// getFunctionCalls - Return the list of call sites in the original local
126 const std::vector<DSCallSite> &getFunctionCalls() const {
127 return FunctionCalls;
130 /// getAuxFunctionCalls - Get the call sites as modified by whatever passes
133 std::vector<DSCallSite> &getAuxFunctionCalls() {
134 return AuxFunctionCalls;
136 const std::vector<DSCallSite> &getAuxFunctionCalls() const {
137 return AuxFunctionCalls;
140 /// getInlinedGlobals - Get the set of globals that are have been inlined
141 /// (from callees in BU or from callers in TD) into the current graph.
143 GlobalSetTy& getInlinedGlobals() {
144 return InlinedGlobals;
147 /// getNodeForValue - Given a value that is used or defined in the body of the
148 /// current function, return the DSNode that it points to.
150 DSNodeHandle &getNodeForValue(Value *V) { return ScalarMap[V]; }
152 const DSNodeHandle &getNodeForValue(Value *V) const {
153 ScalarMapTy::const_iterator I = ScalarMap.find(V);
154 assert(I != ScalarMap.end() &&
155 "Use non-const lookup function if node may not be in the map");
159 /// getReturnNodes - Return the mapping of functions to their return nodes for
161 const ReturnNodesTy &getReturnNodes() const { return ReturnNodes; }
162 ReturnNodesTy &getReturnNodes() { return ReturnNodes; }
164 /// getReturnNodeFor - Return the return node for the specified function.
166 DSNodeHandle &getReturnNodeFor(Function &F) {
167 ReturnNodesTy::iterator I = ReturnNodes.find(&F);
168 assert(I != ReturnNodes.end() && "F not in this DSGraph!");
172 const DSNodeHandle &getReturnNodeFor(Function &F) const {
173 ReturnNodesTy::const_iterator I = ReturnNodes.find(&F);
174 assert(I != ReturnNodes.end() && "F not in this DSGraph!");
178 /// getGraphSize - Return the number of nodes in this graph.
180 unsigned getGraphSize() const {
184 /// print - Print a dot graph to the specified ostream...
186 void print(std::ostream &O) const;
188 /// dump - call print(std::cerr), for use from the debugger...
192 /// viewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
193 /// then cleanup. For use from the debugger.
194 void viewGraph() const;
196 void writeGraphToFile(std::ostream &O, const std::string &GraphName) const;
198 /// maskNodeTypes - Apply a mask to all of the node types in the graph. This
199 /// is useful for clearing out markers like Incomplete.
201 void maskNodeTypes(unsigned Mask) {
202 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
203 Nodes[i]->maskNodeTypes(Mask);
205 void maskIncompleteMarkers() { maskNodeTypes(~DSNode::Incomplete); }
207 // markIncompleteNodes - Traverse the graph, identifying nodes that may be
208 // modified by other functions that have not been resolved yet. This marks
209 // nodes that are reachable through three sources of "unknownness":
210 // Global Variables, Function Calls, and Incoming Arguments
212 // For any node that may have unknown components (because something outside
213 // the scope of current analysis may have modified it), the 'Incomplete' flag
214 // is added to the NodeType.
216 enum MarkIncompleteFlags {
217 MarkFormalArgs = 1, IgnoreFormalArgs = 0,
218 IgnoreGlobals = 2, MarkGlobalsIncomplete = 0,
220 void markIncompleteNodes(unsigned Flags);
222 // removeDeadNodes - Use a reachability analysis to eliminate subgraphs that
223 // are unreachable. This often occurs because the data structure doesn't
224 // "escape" into it's caller, and thus should be eliminated from the caller's
225 // graph entirely. This is only appropriate to use when inlining graphs.
227 enum RemoveDeadNodesFlags {
228 RemoveUnreachableGlobals = 1, KeepUnreachableGlobals = 0,
230 void removeDeadNodes(unsigned Flags);
232 /// CloneFlags enum - Bits that may be passed into the cloneInto method to
233 /// specify how to clone the function graph.
235 StripAllocaBit = 1 << 0, KeepAllocaBit = 0,
236 DontCloneCallNodes = 1 << 1, CloneCallNodes = 0,
237 DontCloneAuxCallNodes = 1 << 2, CloneAuxCallNodes = 0,
238 StripModRefBits = 1 << 3, KeepModRefBits = 0,
239 StripIncompleteBit = 1 << 4, KeepIncompleteBit = 0,
243 void cloneReachableNodes(const DSNode* Node,
244 unsigned BitsToClear,
245 NodeMapTy& OldNodeMap,
246 NodeMapTy& CompletedNodeMap);
249 void updateFromGlobalGraph();
251 void cloneReachableSubgraph(const DSGraph& G,
252 const hash_set<const DSNode*>& RootNodes,
253 NodeMapTy& OldNodeMap,
254 NodeMapTy& CompletedNodeMap,
255 unsigned CloneFlags = 0);
258 /// computeNodeMapping - Given roots in two different DSGraphs, traverse the
259 /// nodes reachable from the two graphs, computing the mapping of nodes from
260 /// the first to the second graph.
262 static void computeNodeMapping(const DSNodeHandle &NH1,
263 const DSNodeHandle &NH2, NodeMapTy &NodeMap);
266 /// cloneInto - Clone the specified DSGraph into the current graph. The
267 /// translated ScalarMap for the old function is filled into the OldValMap
268 /// member, and the translated ReturnNodes map is returned into ReturnNodes.
270 /// The CloneFlags member controls various aspects of the cloning process.
272 void cloneInto(const DSGraph &G, ScalarMapTy &OldValMap,
273 ReturnNodesTy &OldReturnNodes, NodeMapTy &OldNodeMap,
274 unsigned CloneFlags = 0);
276 /// mergeInGraph - The method is used for merging graphs together. If the
277 /// argument graph is not *this, it makes a clone of the specified graph, then
278 /// merges the nodes specified in the call site with the formal arguments in
279 /// the graph. If the StripAlloca's argument is 'StripAllocaBit' then Alloca
280 /// markers are removed from nodes.
282 void mergeInGraph(const DSCallSite &CS, Function &F, const DSGraph &Graph,
283 unsigned CloneFlags);
286 /// getCallSiteForArguments - Get the arguments and return value bindings for
287 /// the specified function in the current graph.
289 DSCallSite getCallSiteForArguments(Function &F) const;
291 // Methods for checking to make sure graphs are well formed...
292 void AssertNodeInGraph(const DSNode *N) const {
293 assert((!N || find(Nodes.begin(), Nodes.end(), N) != Nodes.end()) &&
294 "AssertNodeInGraph: Node is not in graph!");
296 void AssertNodeContainsGlobal(const DSNode *N, GlobalValue *GV) const {
297 assert(std::find(N->getGlobals().begin(), N->getGlobals().end(), GV) !=
298 N->getGlobals().end() && "Global value not in node!");
301 void AssertCallSiteInGraph(const DSCallSite &CS) const {
302 if (CS.isIndirectCall())
303 AssertNodeInGraph(CS.getCalleeNode());
304 AssertNodeInGraph(CS.getRetVal().getNode());
305 for (unsigned j = 0, e = CS.getNumPtrArgs(); j != e; ++j)
306 AssertNodeInGraph(CS.getPtrArg(j).getNode());
309 void AssertCallNodesInGraph() const {
310 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i)
311 AssertCallSiteInGraph(FunctionCalls[i]);
313 void AssertAuxCallNodesInGraph() const {
314 for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i)
315 AssertCallSiteInGraph(AuxFunctionCalls[i]);
318 void AssertGraphOK() const;
320 /// mergeInGlobalsGraph - This method is useful for clients to incorporate the
321 /// globals graph into the DS, BU or TD graph for a function. This code
322 /// retains all globals, i.e., does not delete unreachable globals after they
325 void mergeInGlobalsGraph();
327 /// removeTriviallyDeadNodes - After the graph has been constructed, this
328 /// method removes all unreachable nodes that are created because they got
329 /// merged with other nodes in the graph. This is used as the first step of
332 void removeTriviallyDeadNodes();