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 (DSGraph) and the
11 // ReachabilityCloner class.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_DSGRAPH_H
16 #define LLVM_ANALYSIS_DSGRAPH_H
18 #include "llvm/Analysis/DSNode.h"
24 //===----------------------------------------------------------------------===//
25 /// DSGraph - The graph that represents a function.
28 // Public data-type declarations...
29 typedef hash_map<Value*, DSNodeHandle> ScalarMapTy;
30 typedef hash_map<Function*, DSNodeHandle> ReturnNodesTy;
31 typedef hash_set<GlobalValue*> GlobalSetTy;
33 /// NodeMapTy - This data type is used when cloning one graph into another to
34 /// keep track of the correspondence between the nodes in the old and new
36 typedef hash_map<const DSNode*, DSNodeHandle> NodeMapTy;
38 DSGraph *GlobalsGraph; // Pointer to the common graph of global objects
39 bool PrintAuxCalls; // Should this graph print the Aux calls vector?
41 std::vector<DSNode*> Nodes;
42 ScalarMapTy ScalarMap;
44 // ReturnNodes - A return value for every function merged into this graph.
45 // Each DSGraph may have multiple functions merged into it at any time, which
46 // is used for representing SCCs.
48 ReturnNodesTy ReturnNodes;
50 // FunctionCalls - This vector maintains a single entry for each call
51 // instruction in the current graph. The first entry in the vector is the
52 // scalar that holds the return value for the call, the second is the function
53 // scalar being invoked, and the rest are pointer arguments to the function.
54 // This vector is built by the Local graph and is never modified after that.
56 std::vector<DSCallSite> FunctionCalls;
58 // AuxFunctionCalls - This vector contains call sites that have been processed
59 // by some mechanism. In pratice, the BU Analysis uses this vector to hold
60 // the _unresolved_ call sites, because it cannot modify FunctionCalls.
62 std::vector<DSCallSite> AuxFunctionCalls;
64 // InlinedGlobals - This set records which globals have been inlined from
65 // other graphs (callers or callees, depending on the pass) into this one.
67 GlobalSetTy InlinedGlobals;
69 /// TD - This is the target data object for the machine this graph is
73 void operator=(const DSGraph &); // DO NOT IMPLEMENT
76 // Create a new, empty, DSGraph.
77 DSGraph(const TargetData &td)
78 : GlobalsGraph(0), PrintAuxCalls(false), TD(td) {}
80 // Compute the local DSGraph
81 DSGraph(const TargetData &td, Function &F, DSGraph *GlobalsGraph);
83 // Copy ctor - If you want to capture the node mapping between the source and
84 // destination graph, you may optionally do this by specifying a map to record
87 // Note that a copied graph does not retain the GlobalsGraph pointer of the
88 // source. You need to set a new GlobalsGraph with the setGlobalsGraph
91 DSGraph(const DSGraph &DSG);
92 DSGraph(const DSGraph &DSG, NodeMapTy &NodeMap);
95 DSGraph *getGlobalsGraph() const { return GlobalsGraph; }
96 void setGlobalsGraph(DSGraph *G) { GlobalsGraph = G; }
98 /// getTargetData - Return the TargetData object for the current target.
100 const TargetData &getTargetData() const { return TD; }
102 /// setPrintAuxCalls - If you call this method, the auxillary call vector will
103 /// be printed instead of the standard call vector to the dot file.
105 void setPrintAuxCalls() { PrintAuxCalls = true; }
106 bool shouldPrintAuxCalls() const { return PrintAuxCalls; }
108 /// getNodes - Get a vector of all the nodes in the graph
110 const std::vector<DSNode*> &getNodes() const { return Nodes; }
111 std::vector<DSNode*> &getNodes() { return Nodes; }
113 /// getFunctionNames - Return a space separated list of the name of the
114 /// functions in this graph (if any)
115 std::string getFunctionNames() const;
117 /// addNode - Add a new node to the graph.
119 void addNode(DSNode *N) { Nodes.push_back(N); }
121 /// getScalarMap - Get a map that describes what the nodes the scalars in this
122 /// function point to...
124 ScalarMapTy &getScalarMap() { return ScalarMap; }
125 const ScalarMapTy &getScalarMap() const { return ScalarMap; }
127 /// getFunctionCalls - Return the list of call sites in the original local
130 const std::vector<DSCallSite> &getFunctionCalls() const {
131 return FunctionCalls;
134 /// getAuxFunctionCalls - Get the call sites as modified by whatever passes
137 std::vector<DSCallSite> &getAuxFunctionCalls() {
138 return AuxFunctionCalls;
140 const std::vector<DSCallSite> &getAuxFunctionCalls() const {
141 return AuxFunctionCalls;
144 /// getInlinedGlobals - Get the set of globals that are have been inlined
145 /// (from callees in BU or from callers in TD) into the current graph.
147 GlobalSetTy& getInlinedGlobals() {
148 return InlinedGlobals;
151 /// getNodeForValue - Given a value that is used or defined in the body of the
152 /// current function, return the DSNode that it points to.
154 DSNodeHandle &getNodeForValue(Value *V) { return ScalarMap[V]; }
156 const DSNodeHandle &getNodeForValue(Value *V) const {
157 ScalarMapTy::const_iterator I = ScalarMap.find(V);
158 assert(I != ScalarMap.end() &&
159 "Use non-const lookup function if node may not be in the map");
163 /// getReturnNodes - Return the mapping of functions to their return nodes for
165 const ReturnNodesTy &getReturnNodes() const { return ReturnNodes; }
166 ReturnNodesTy &getReturnNodes() { return ReturnNodes; }
168 /// getReturnNodeFor - Return the return node for the specified function.
170 DSNodeHandle &getReturnNodeFor(Function &F) {
171 ReturnNodesTy::iterator I = ReturnNodes.find(&F);
172 assert(I != ReturnNodes.end() && "F not in this DSGraph!");
176 const DSNodeHandle &getReturnNodeFor(Function &F) const {
177 ReturnNodesTy::const_iterator I = ReturnNodes.find(&F);
178 assert(I != ReturnNodes.end() && "F not in this DSGraph!");
182 /// getGraphSize - Return the number of nodes in this graph.
184 unsigned getGraphSize() const {
188 /// print - Print a dot graph to the specified ostream...
190 void print(std::ostream &O) const;
192 /// dump - call print(std::cerr), for use from the debugger...
196 /// viewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
197 /// then cleanup. For use from the debugger.
198 void viewGraph() const;
200 void writeGraphToFile(std::ostream &O, const std::string &GraphName) const;
202 /// maskNodeTypes - Apply a mask to all of the node types in the graph. This
203 /// is useful for clearing out markers like Incomplete.
205 void maskNodeTypes(unsigned Mask) {
206 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
207 Nodes[i]->maskNodeTypes(Mask);
209 void maskIncompleteMarkers() { maskNodeTypes(~DSNode::Incomplete); }
211 // markIncompleteNodes - Traverse the graph, identifying nodes that may be
212 // modified by other functions that have not been resolved yet. This marks
213 // nodes that are reachable through three sources of "unknownness":
214 // Global Variables, Function Calls, and Incoming Arguments
216 // For any node that may have unknown components (because something outside
217 // the scope of current analysis may have modified it), the 'Incomplete' flag
218 // is added to the NodeType.
220 enum MarkIncompleteFlags {
221 MarkFormalArgs = 1, IgnoreFormalArgs = 0,
222 IgnoreGlobals = 2, MarkGlobalsIncomplete = 0,
224 void markIncompleteNodes(unsigned Flags);
226 // removeDeadNodes - Use a reachability analysis to eliminate subgraphs that
227 // are unreachable. This often occurs because the data structure doesn't
228 // "escape" into it's caller, and thus should be eliminated from the caller's
229 // graph entirely. This is only appropriate to use when inlining graphs.
231 enum RemoveDeadNodesFlags {
232 RemoveUnreachableGlobals = 1, KeepUnreachableGlobals = 0,
234 void removeDeadNodes(unsigned Flags);
236 /// CloneFlags enum - Bits that may be passed into the cloneInto method to
237 /// specify how to clone the function graph.
239 StripAllocaBit = 1 << 0, KeepAllocaBit = 0,
240 DontCloneCallNodes = 1 << 1, CloneCallNodes = 0,
241 DontCloneAuxCallNodes = 1 << 2, CloneAuxCallNodes = 0,
242 StripModRefBits = 1 << 3, KeepModRefBits = 0,
243 StripIncompleteBit = 1 << 4, KeepIncompleteBit = 0,
244 UpdateInlinedGlobals = 1 << 5, DontUpdateInlinedGlobals = 0,
247 void updateFromGlobalGraph();
249 /// computeNodeMapping - Given roots in two different DSGraphs, traverse the
250 /// nodes reachable from the two graphs, computing the mapping of nodes from
251 /// the first to the second graph.
253 static void computeNodeMapping(const DSNodeHandle &NH1,
254 const DSNodeHandle &NH2, NodeMapTy &NodeMap,
255 bool StrictChecking = true);
258 /// cloneInto - Clone the specified DSGraph into the current graph. The
259 /// translated ScalarMap for the old function is filled into the OldValMap
260 /// member, and the translated ReturnNodes map is returned into ReturnNodes.
261 /// OldNodeMap contains a mapping from the original nodes to the newly cloned
264 /// The CloneFlags member controls various aspects of the cloning process.
266 void cloneInto(const DSGraph &G, ScalarMapTy &OldValMap,
267 ReturnNodesTy &OldReturnNodes, NodeMapTy &OldNodeMap,
268 unsigned CloneFlags = 0);
270 /// mergeInGraph - The method is used for merging graphs together. If the
271 /// argument graph is not *this, it makes a clone of the specified graph, then
272 /// merges the nodes specified in the call site with the formal arguments in
273 /// the graph. If the StripAlloca's argument is 'StripAllocaBit' then Alloca
274 /// markers are removed from nodes.
276 void mergeInGraph(const DSCallSite &CS, Function &F, const DSGraph &Graph,
277 unsigned CloneFlags);
280 /// getCallSiteForArguments - Get the arguments and return value bindings for
281 /// the specified function in the current graph.
283 DSCallSite getCallSiteForArguments(Function &F) const;
285 // Methods for checking to make sure graphs are well formed...
286 void AssertNodeInGraph(const DSNode *N) const {
287 assert((!N || N->getParentGraph() == this) &&
288 "AssertNodeInGraph: Node is not in graph!");
290 void AssertNodeContainsGlobal(const DSNode *N, GlobalValue *GV) const {
291 assert(std::find(N->getGlobals().begin(), N->getGlobals().end(), GV) !=
292 N->getGlobals().end() && "Global value not in node!");
295 void AssertCallSiteInGraph(const DSCallSite &CS) const {
296 if (CS.isIndirectCall())
297 AssertNodeInGraph(CS.getCalleeNode());
298 AssertNodeInGraph(CS.getRetVal().getNode());
299 for (unsigned j = 0, e = CS.getNumPtrArgs(); j != e; ++j)
300 AssertNodeInGraph(CS.getPtrArg(j).getNode());
303 void AssertCallNodesInGraph() const {
304 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i)
305 AssertCallSiteInGraph(FunctionCalls[i]);
307 void AssertAuxCallNodesInGraph() const {
308 for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i)
309 AssertCallSiteInGraph(AuxFunctionCalls[i]);
312 void AssertGraphOK() const;
314 /// removeTriviallyDeadNodes - After the graph has been constructed, this
315 /// method removes all unreachable nodes that are created because they got
316 /// merged with other nodes in the graph. This is used as the first step of
319 void removeTriviallyDeadNodes();
323 /// ReachabilityCloner - This class is used to incrementally clone and merge
324 /// nodes from a non-changing source graph into a potentially mutating
325 /// destination graph. Nodes are only cloned over on demand, either in
326 /// responds to a merge() or getClonedNH() call. When a node is cloned over,
327 /// all of the nodes reachable from it are automatically brought over as well.
328 class ReachabilityCloner {
332 /// BitsToKeep - These bits are retained from the source node when the
333 /// source nodes are merged into the destination graph.
337 // NodeMap - A mapping from nodes in the source graph to the nodes that
338 // represent them in the destination graph.
339 DSGraph::NodeMapTy NodeMap;
341 ReachabilityCloner(DSGraph &dest, const DSGraph &src, unsigned cloneFlags)
342 : Dest(dest), Src(src), CloneFlags(cloneFlags) {
343 assert(&Dest != &Src && "Cannot clone from graph to same graph!");
344 BitsToKeep = ~DSNode::DEAD;
345 if (CloneFlags & DSGraph::StripAllocaBit)
346 BitsToKeep &= ~DSNode::AllocaNode;
347 if (CloneFlags & DSGraph::StripModRefBits)
348 BitsToKeep &= ~(DSNode::Modified | DSNode::Read);
349 if (CloneFlags & DSGraph::StripIncompleteBit)
350 BitsToKeep &= ~DSNode::Incomplete;
353 DSNodeHandle getClonedNH(const DSNodeHandle &SrcNH);
355 void merge(const DSNodeHandle &NH, const DSNodeHandle &SrcNH);
357 /// mergeCallSite - Merge the nodes reachable from the specified src call
358 /// site into the nodes reachable from DestCS.
359 void mergeCallSite(const DSCallSite &DestCS, const DSCallSite &SrcCS);
361 bool clonedNode() const { return !NodeMap.empty(); }
363 void destroy() { NodeMap.clear(); }
365 } // End llvm namespace