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 std::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 DSGraph() : Func(0), GlobalsGraph(0) {} // Create a new, empty, DSGraph.
41 DSGraph(Function &F, DSGraph *GlobalsGraph); // Compute the local DSGraph
43 // Copy ctor - If you want to capture the node mapping between the source and
44 // destination graph, you may optionally do this by specifying a map to record
47 // Note that a copied graph does not retain the GlobalsGraph pointer of the
48 // source. You need to set a new GlobalsGraph with the setGlobalsGraph
51 DSGraph(const DSGraph &DSG);
52 DSGraph(const DSGraph &DSG, std::map<const DSNode*, DSNodeHandle> &NodeMap);
55 bool hasFunction() const { return Func != 0; }
56 Function &getFunction() const { return *Func; }
58 DSGraph *getGlobalsGraph() const { return GlobalsGraph; }
59 void setGlobalsGraph(DSGraph *G) { GlobalsGraph = G; }
61 // setPrintAuxCalls - If you call this method, the auxillary call vector will
62 // be printed instead of the standard call vector to the dot file.
64 void setPrintAuxCalls() { PrintAuxCalls = true; }
65 bool shouldPrintAuxCalls() const { return PrintAuxCalls; }
67 /// getNodes - Get a vector of all the nodes in the graph
69 const std::vector<DSNode*> &getNodes() const { return Nodes; }
70 std::vector<DSNode*> &getNodes() { return Nodes; }
72 /// addNode - Add a new node to the graph.
74 void addNode(DSNode *N) { Nodes.push_back(N); }
76 /// getScalarMap - Get a map that describes what the nodes the scalars in this
77 /// function point to...
79 std::map<Value*, DSNodeHandle> &getScalarMap() { return ScalarMap; }
80 const std::map<Value*, DSNodeHandle> &getScalarMap() const {return ScalarMap;}
82 /// getFunctionCalls - Return the list of call sites in the original local
85 const std::vector<DSCallSite> &getFunctionCalls() const {
89 /// getAuxFunctionCalls - Get the call sites as modified by whatever passes
92 std::vector<DSCallSite> &getAuxFunctionCalls() {
93 return AuxFunctionCalls;
95 const std::vector<DSCallSite> &getAuxFunctionCalls() const {
96 return AuxFunctionCalls;
99 /// getNodeForValue - Given a value that is used or defined in the body of the
100 /// current function, return the DSNode that it points to.
102 DSNodeHandle &getNodeForValue(Value *V) { return ScalarMap[V]; }
104 const DSNodeHandle &getNodeForValue(Value *V) const {
105 std::map<Value*, DSNodeHandle>::const_iterator I = ScalarMap.find(V);
106 assert(I != ScalarMap.end() &&
107 "Use non-const lookup function if node may not be in the map");
111 const DSNodeHandle &getRetNode() const { return RetNode; }
112 DSNodeHandle &getRetNode() { return RetNode; }
114 unsigned getGraphSize() const {
118 void print(std::ostream &O) const;
120 void writeGraphToFile(std::ostream &O, const std::string &GraphName) const;
122 /// maskNodeTypes - Apply a mask to all of the node types in the graph. This
123 /// is useful for clearing out markers like Incomplete.
125 void maskNodeTypes(unsigned char Mask) {
126 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
127 Nodes[i]->NodeType &= Mask;
129 void maskIncompleteMarkers() { maskNodeTypes(~DSNode::Incomplete); }
131 // markIncompleteNodes - Traverse the graph, identifying nodes that may be
132 // modified by other functions that have not been resolved yet. This marks
133 // nodes that are reachable through three sources of "unknownness":
134 // Global Variables, Function Calls, and Incoming Arguments
136 // For any node that may have unknown components (because something outside
137 // the scope of current analysis may have modified it), the 'Incomplete' flag
138 // is added to the NodeType.
140 void markIncompleteNodes(bool markFormalArgs = true);
142 // removeDeadNodes - Use a more powerful reachability analysis to eliminate
143 // subgraphs that are unreachable. This often occurs because the data
144 // structure doesn't "escape" into it's caller, and thus should be eliminated
145 // from the caller's graph entirely. This is only appropriate to use when
148 void removeDeadNodes();
150 // CloneFlags enum - Bits that may be passed into the cloneInto method to
151 // specify how to clone the function graph.
153 StripAllocaBit = 1 << 0, KeepAllocaBit = 0 << 0,
154 DontCloneCallNodes = 1 << 1, CloneCallNodes = 0 << 0,
155 DontCloneAuxCallNodes = 1 << 2, CloneAuxCallNodes = 0 << 0,
158 // cloneInto - Clone the specified DSGraph into the current graph, returning
159 // the Return node of the graph. The translated ScalarMap for the old
160 // function is filled into the OldValMap member. If StripAllocas is set to
161 // 'StripAllocaBit', Alloca markers are removed from the graph as the graph is
164 DSNodeHandle cloneInto(const DSGraph &G,
165 std::map<Value*, DSNodeHandle> &OldValMap,
166 std::map<const DSNode*, DSNodeHandle> &OldNodeMap,
167 unsigned CloneFlags = 0);
169 /// mergeInGraph - The method is used for merging graphs together. If the
170 /// argument graph is not *this, it makes a clone of the specified graph, then
171 /// merges the nodes specified in the call site with the formal arguments in
172 /// the graph. If the StripAlloca's argument is 'StripAllocaBit' then Alloca
173 /// markers are removed from nodes.
175 void mergeInGraph(DSCallSite &CS, const DSGraph &Graph, unsigned CloneFlags);
178 bool isNodeDead(DSNode *N);
181 // removeTriviallyDeadNodes - After the graph has been constructed, this
182 // method removes all unreachable nodes that are created because they got
183 // merged with other nodes in the graph. This is used as the first step of
186 void removeTriviallyDeadNodes();