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.
17 std::vector<DSNode*> Nodes;
18 DSNodeHandle RetNode; // Node that gets returned...
19 std::map<Value*, DSNodeHandle> ScalarMap;
22 // GlobalsGraph -- Reference to the common graph of globally visible objects.
23 // This includes GlobalValues, New nodes, Cast nodes, and Calls.
25 GlobalDSGraph* GlobalsGraph;
28 // FunctionCalls - This vector maintains a single entry for each call
29 // instruction in the current graph. The first entry in the vector is the
30 // scalar that holds the return value for the call, the second is the function
31 // scalar being invoked, and the rest are pointer arguments to the function.
32 // This vector is built by the Local graph and is never modified after that.
34 std::vector<DSCallSite> FunctionCalls;
36 // AuxFunctionCalls - This vector contains call sites that have been processed
37 // by some mechanism. In pratice, the BU Analysis uses this vector to hold
38 // the _unresolved_ call sites, because it cannot modify FunctionCalls.
40 std::vector<DSCallSite> AuxFunctionCalls;
42 void operator=(const DSGraph &); // DO NOT IMPLEMENT
44 DSGraph() : Func(0) {} // Create a new, empty, DSGraph.
45 DSGraph(Function &F); // Compute the local DSGraph
47 // Copy ctor - If you want to capture the node mapping between the source and
48 // destination graph, you may optionally do this by specifying a map to record
50 DSGraph(const DSGraph &DSG);
51 DSGraph(const DSGraph &DSG, std::map<const DSNode*, DSNodeHandle> &NodeMap);
54 bool hasFunction() const { return Func != 0; }
55 Function &getFunction() const { return *Func; }
57 /// getNodes - Get a vector of all the nodes in the graph
59 const std::vector<DSNode*> &getNodes() const { return Nodes; }
60 std::vector<DSNode*> &getNodes() { return Nodes; }
62 /// addNode - Add a new node to the graph.
64 void addNode(DSNode *N) { Nodes.push_back(N); }
66 /// getScalarMap - Get a map that describes what the nodes the scalars in this
67 /// function point to...
69 std::map<Value*, DSNodeHandle> &getScalarMap() { return ScalarMap; }
70 const std::map<Value*, DSNodeHandle> &getScalarMap() const {return ScalarMap;}
72 /// getFunctionCalls - Return the list of call sites in the original local
75 const std::vector<DSCallSite> &getFunctionCalls() const {
79 /// getAuxFunctionCalls - Get the call sites as modified by whatever passes
82 std::vector<DSCallSite> &getAuxFunctionCalls() {
83 return AuxFunctionCalls;
86 /// getNodeForValue - Given a value that is used or defined in the body of the
87 /// current function, return the DSNode that it points to.
89 DSNodeHandle &getNodeForValue(Value *V) { return ScalarMap[V]; }
91 const DSNodeHandle &getNodeForValue(Value *V) const {
92 std::map<Value*, DSNodeHandle>::const_iterator I = ScalarMap.find(V);
93 assert(I != ScalarMap.end() &&
94 "Use non-const lookup function if node may not be in the map");
98 const DSNodeHandle &getRetNode() const { return RetNode; }
99 DSNodeHandle &getRetNode() { return RetNode; }
101 unsigned getGraphSize() const {
105 void print(std::ostream &O) const;
107 void writeGraphToFile(std::ostream &O, const std::string &GraphName) const;
109 // maskNodeTypes - Apply a mask to all of the node types in the graph. This
110 // is useful for clearing out markers like Scalar or Incomplete.
112 void maskNodeTypes(unsigned char Mask);
113 void maskIncompleteMarkers() { maskNodeTypes(~DSNode::Incomplete); }
115 // markIncompleteNodes - Traverse the graph, identifying nodes that may be
116 // modified by other functions that have not been resolved yet. This marks
117 // nodes that are reachable through three sources of "unknownness":
118 // Global Variables, Function Calls, and Incoming Arguments
120 // For any node that may have unknown components (because something outside
121 // the scope of current analysis may have modified it), the 'Incomplete' flag
122 // is added to the NodeType.
124 void markIncompleteNodes(bool markFormalArgs = true);
126 // removeTriviallyDeadNodes - After the graph has been constructed, this
127 // method removes all unreachable nodes that are created because they got
128 // merged with other nodes in the graph.
130 void removeTriviallyDeadNodes(bool KeepAllGlobals = false);
132 // removeDeadNodes - Use a more powerful reachability analysis to eliminate
133 // subgraphs that are unreachable. This often occurs because the data
134 // structure doesn't "escape" into it's caller, and thus should be eliminated
135 // from the caller's graph entirely. This is only appropriate to use when
138 void removeDeadNodes(bool KeepAllGlobals = false, bool KeepCalls = true);
140 // CloneFlags enum - Bits that may be passed into the cloneInto method to
141 // specify how to clone the function graph.
143 StripAllocaBit = 1 << 0, KeepAllocaBit = 0 << 0,
144 DontCloneCallNodes = 2 << 0, CloneCallNodes = 0 << 0,
147 // cloneInto - Clone the specified DSGraph into the current graph, returning
148 // the Return node of the graph. The translated ScalarMap for the old
149 // function is filled into the OldValMap member. If StripAllocas is set to
150 // 'StripAllocaBit', Alloca markers are removed from the graph as the graph is
153 DSNodeHandle cloneInto(const DSGraph &G,
154 std::map<Value*, DSNodeHandle> &OldValMap,
155 std::map<const DSNode*, DSNodeHandle> &OldNodeMap,
156 unsigned CloneFlags = 0);
158 /// mergeInGraph - The method is used for merging graphs together. If the
159 /// argument graph is not *this, it makes a clone of the specified graph, then
160 /// merges the nodes specified in the call site with the formal arguments in
161 /// the graph. If the StripAlloca's argument is 'StripAllocaBit' then Alloca
162 /// markers are removed from nodes.
164 void mergeInGraph(DSCallSite &CS, const DSGraph &Graph, unsigned CloneFlags);
167 // cloneGlobalInto - Clone the given global node (or the node for the given
168 // GlobalValue) from the GlobalsGraph and all its target links (recursively).
170 DSNode* cloneGlobalInto(const DSNode* GNode);
171 DSNode* cloneGlobalInto(GlobalValue* GV) {
172 assert(!GV || (((DSGraph*) GlobalsGraph)->ScalarMap[GV] != 0));
173 return GV? cloneGlobalInto(((DSGraph*) GlobalsGraph)->ScalarMap[GV]) : 0;
178 bool isNodeDead(DSNode *N);