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"
22 class DSNode; // Each node in the graph
23 class DSGraph; // A graph for a function
24 class DSNodeIterator; // Data structure graph traversal iterator
26 //===----------------------------------------------------------------------===//
27 /// DSGraph - The graph that represents a function.
31 std::vector<DSNode*> Nodes;
32 DSNodeHandle RetNode; // Node that gets returned...
33 std::map<Value*, DSNodeHandle> ValueMap;
36 // GlobalsGraph -- Reference to the common graph of globally visible objects.
37 // This includes GlobalValues, New nodes, Cast nodes, and Calls.
39 GlobalDSGraph* GlobalsGraph;
42 // FunctionCalls - This vector maintains a single entry for each call
43 // instruction in the current graph. Each call entry contains DSNodeHandles
44 // that refer to the arguments that are passed into the function call. The
45 // first entry in the vector is the scalar that holds the return value for the
46 // call, the second is the function scalar being invoked, and the rest are
47 // pointer arguments to the function.
49 std::vector<DSCallSite> FunctionCalls;
51 void operator=(const DSGraph &); // DO NOT IMPLEMENT
53 DSGraph() : Func(0) {} // Create a new, empty, DSGraph.
54 DSGraph(Function &F); // Compute the local DSGraph
56 // Copy ctor - If you want to capture the node mapping between the source and
57 // destination graph, you may optionally do this by specifying a map to record
59 DSGraph(const DSGraph &DSG);
60 DSGraph(const DSGraph &DSG, std::map<const DSNode*, DSNode*> &BUNodeMap);
63 bool hasFunction() const { return Func != 0; }
64 Function &getFunction() const { return *Func; }
66 /// getNodes - Get a vector of all the nodes in the graph
68 const std::vector<DSNode*> &getNodes() const { return Nodes; }
69 std::vector<DSNode*> &getNodes() { return Nodes; }
71 /// addNode - Add a new node to the graph.
73 void addNode(DSNode *N) { Nodes.push_back(N); }
75 /// getValueMap - Get a map that describes what the nodes the scalars in this
76 /// function point to...
78 std::map<Value*, DSNodeHandle> &getValueMap() { return ValueMap; }
79 const std::map<Value*, DSNodeHandle> &getValueMap() const { return ValueMap;}
81 std::vector<DSCallSite> &getFunctionCalls() {
84 const std::vector<DSCallSite> &getFunctionCalls() const {
88 /// getNodeForValue - Given a value that is used or defined in the body of the
89 /// current function, return the DSNode that it points to.
91 DSNodeHandle &getNodeForValue(Value *V) { return ValueMap[V]; }
93 const DSNodeHandle &getRetNode() const { return RetNode; }
94 DSNodeHandle &getRetNode() { return RetNode; }
96 unsigned getGraphSize() const {
100 void print(std::ostream &O) const;
102 void writeGraphToFile(std::ostream &O, const std::string &GraphName) const;
104 // maskNodeTypes - Apply a mask to all of the node types in the graph. This
105 // is useful for clearing out markers like Scalar or Incomplete.
107 void maskNodeTypes(unsigned char Mask);
108 void maskIncompleteMarkers() { maskNodeTypes(~DSNode::Incomplete); }
110 // markIncompleteNodes - Traverse the graph, identifying nodes that may be
111 // modified by other functions that have not been resolved yet. This marks
112 // nodes that are reachable through three sources of "unknownness":
113 // Global Variables, Function Calls, and Incoming Arguments
115 // For any node that may have unknown components (because something outside
116 // the scope of current analysis may have modified it), the 'Incomplete' flag
117 // is added to the NodeType.
119 void markIncompleteNodes(bool markFormalArgs = true);
121 // removeTriviallyDeadNodes - After the graph has been constructed, this
122 // method removes all unreachable nodes that are created because they got
123 // merged with other nodes in the graph.
125 void removeTriviallyDeadNodes(bool KeepAllGlobals = false);
127 // removeDeadNodes - Use a more powerful reachability analysis to eliminate
128 // subgraphs that are unreachable. This often occurs because the data
129 // structure doesn't "escape" into it's caller, and thus should be eliminated
130 // from the caller's graph entirely. This is only appropriate to use when
133 void removeDeadNodes(bool KeepAllGlobals = false, bool KeepCalls = true);
135 // cloneInto - Clone the specified DSGraph into the current graph, returning
136 // the Return node of the graph. The translated ValueMap for the old function
137 // is filled into the OldValMap member.
138 // If StripScalars (StripAllocas) is set to true, Scalar (Alloca) markers
139 // are removed from the graph as the graph is being cloned.
141 DSNodeHandle cloneInto(const DSGraph &G,
142 std::map<Value*, DSNodeHandle> &OldValMap,
143 std::map<const DSNode*, DSNode*> &OldNodeMap,
144 bool StripScalars = false, bool StripAllocas = false);
147 // cloneGlobalInto - Clone the given global node (or the node for the given
148 // GlobalValue) from the GlobalsGraph and all its target links (recursively).
150 DSNode* cloneGlobalInto(const DSNode* GNode);
151 DSNode* cloneGlobalInto(GlobalValue* GV) {
152 assert(!GV || (((DSGraph*) GlobalsGraph)->ValueMap[GV] != 0));
153 return GV? cloneGlobalInto(((DSGraph*) GlobalsGraph)->ValueMap[GV]) : 0;
158 bool isNodeDead(DSNode *N);