04f80e162432515739c41f80e9980a3144d191ad
[oota-llvm.git] / include / llvm / Analysis / DataStructure / DSGraph.h
1 //===- DSGraph.h - Represent a collection of data structures ----*- C++ -*-===//
2 //
3 // This header defines the data structure graph.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #ifndef LLVM_ANALYSIS_DSGRAPH_H
8 #define LLVM_ANALYSIS_DSGRAPH_H
9
10 #include "llvm/Analysis/DSNode.h"
11 #include <vector>
12 #include <map>
13 #include <functional>
14 #include <string>
15
16 class Function;
17 class CallInst;
18 class Value;
19 class GlobalValue;
20 class Type;
21
22 class DSNode;                  // Each node in the graph
23 class DSGraph;                 // A graph for a function
24 class DSNodeIterator;          // Data structure graph traversal iterator
25
26 //===----------------------------------------------------------------------===//
27 /// DSGraph - The graph that represents a function.
28 ///
29 class DSGraph {
30   Function *Func;
31   std::vector<DSNode*> Nodes;
32   DSNodeHandle RetNode;                          // Node that gets returned...
33   std::map<Value*, DSNodeHandle> ValueMap;
34
35 #if 0
36   // GlobalsGraph -- Reference to the common graph of globally visible objects.
37   // This includes GlobalValues, New nodes, Cast nodes, and Calls.
38   // 
39   GlobalDSGraph* GlobalsGraph;
40 #endif
41
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.
48   //
49   std::vector<DSCallSite> FunctionCalls;
50
51   void operator=(const DSGraph &); // DO NOT IMPLEMENT
52 public:
53   DSGraph() : Func(0) {}           // Create a new, empty, DSGraph.
54   DSGraph(Function &F);            // Compute the local DSGraph
55
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
58   // this into.
59   DSGraph(const DSGraph &DSG);
60   DSGraph(const DSGraph &DSG, std::map<const DSNode*, DSNode*> &BUNodeMap);
61   ~DSGraph();
62
63   bool hasFunction() const { return Func != 0; }
64   Function &getFunction() const { return *Func; }
65
66   /// getNodes - Get a vector of all the nodes in the graph
67   /// 
68   const std::vector<DSNode*> &getNodes() const { return Nodes; }
69         std::vector<DSNode*> &getNodes()       { return Nodes; }
70
71   /// addNode - Add a new node to the graph.
72   ///
73   void addNode(DSNode *N) { Nodes.push_back(N); }
74
75   /// getValueMap - Get a map that describes what the nodes the scalars in this
76   /// function point to...
77   ///
78   std::map<Value*, DSNodeHandle> &getValueMap() { return ValueMap; }
79   const std::map<Value*, DSNodeHandle> &getValueMap() const { return ValueMap;}
80
81   std::vector<DSCallSite> &getFunctionCalls() {
82     return FunctionCalls;
83   }
84   const std::vector<DSCallSite> &getFunctionCalls() const {
85     return FunctionCalls;
86   }
87
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.
90   ///
91   DSNodeHandle &getNodeForValue(Value *V) { return ValueMap[V]; }
92
93   const DSNodeHandle &getRetNode() const { return RetNode; }
94         DSNodeHandle &getRetNode()       { return RetNode; }
95
96   unsigned getGraphSize() const {
97     return Nodes.size();
98   }
99
100   void print(std::ostream &O) const;
101   void dump() const;
102   void writeGraphToFile(std::ostream &O, const std::string &GraphName) const;
103
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.
106   //
107   void maskNodeTypes(unsigned char Mask);
108   void maskIncompleteMarkers() { maskNodeTypes(~DSNode::Incomplete); }
109
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
114   //
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.
118   //
119   void markIncompleteNodes(bool markFormalArgs = true);
120
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.
124   //
125   void removeTriviallyDeadNodes(bool KeepAllGlobals = false);
126
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
131   // inlining graphs.
132   //
133   void removeDeadNodes(bool KeepAllGlobals = false, bool KeepCalls = true);
134
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.
140   //
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);
145
146 #if 0
147   // cloneGlobalInto - Clone the given global node (or the node for the given
148   // GlobalValue) from the GlobalsGraph and all its target links (recursively).
149   // 
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;
154   }
155 #endif
156
157 private:
158   bool isNodeDead(DSNode *N);
159 };
160
161 #endif