Add another option to cloneGraph
[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
12 //===----------------------------------------------------------------------===//
13 /// DSGraph - The graph that represents a function.
14 ///
15 class DSGraph {
16   Function *Func;
17   std::vector<DSNode*> Nodes;
18   DSNodeHandle RetNode;                          // Node that gets returned...
19   std::map<Value*, DSNodeHandle> ScalarMap;
20
21 #if 0
22   // GlobalsGraph -- Reference to the common graph of globally visible objects.
23   // This includes GlobalValues, New nodes, Cast nodes, and Calls.
24   // 
25   GlobalDSGraph* GlobalsGraph;
26 #endif
27
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.
33   //
34   std::vector<DSCallSite> FunctionCalls;
35
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.
39   //
40   std::vector<DSCallSite> AuxFunctionCalls;
41
42   void operator=(const DSGraph &); // DO NOT IMPLEMENT
43 public:
44   DSGraph() : Func(0) {}           // Create a new, empty, DSGraph.
45   DSGraph(Function &F);            // Compute the local DSGraph
46
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
49   // this into.
50   DSGraph(const DSGraph &DSG);
51   DSGraph(const DSGraph &DSG, std::map<const DSNode*, DSNodeHandle> &NodeMap);
52   ~DSGraph();
53
54   bool hasFunction() const { return Func != 0; }
55   Function &getFunction() const { return *Func; }
56
57   /// getNodes - Get a vector of all the nodes in the graph
58   /// 
59   const std::vector<DSNode*> &getNodes() const { return Nodes; }
60         std::vector<DSNode*> &getNodes()       { return Nodes; }
61
62   /// addNode - Add a new node to the graph.
63   ///
64   void addNode(DSNode *N) { Nodes.push_back(N); }
65
66   /// getScalarMap - Get a map that describes what the nodes the scalars in this
67   /// function point to...
68   ///
69   std::map<Value*, DSNodeHandle> &getScalarMap() { return ScalarMap; }
70   const std::map<Value*, DSNodeHandle> &getScalarMap() const {return ScalarMap;}
71
72   /// getFunctionCalls - Return the list of call sites in the original local
73   /// graph...
74   ///
75   const std::vector<DSCallSite> &getFunctionCalls() const {
76     return FunctionCalls;
77   }
78
79   /// getAuxFunctionCalls - Get the call sites as modified by whatever passes
80   /// have been run.
81   ///
82   std::vector<DSCallSite> &getAuxFunctionCalls() {
83     return AuxFunctionCalls;
84   }
85
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.
88   ///
89   DSNodeHandle &getNodeForValue(Value *V) { return ScalarMap[V]; }
90
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");
95     return I->second;
96   }
97
98   const DSNodeHandle &getRetNode() const { return RetNode; }
99         DSNodeHandle &getRetNode()       { return RetNode; }
100
101   unsigned getGraphSize() const {
102     return Nodes.size();
103   }
104
105   void print(std::ostream &O) const;
106   void dump() const;
107   void writeGraphToFile(std::ostream &O, const std::string &GraphName) const;
108
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.
111   //
112   void maskNodeTypes(unsigned char Mask);
113   void maskIncompleteMarkers() { maskNodeTypes(~DSNode::Incomplete); }
114
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
119   //
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.
123   //
124   void markIncompleteNodes(bool markFormalArgs = true);
125
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.
129   //
130   void removeTriviallyDeadNodes(bool KeepAllGlobals = false);
131
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
136   // inlining graphs.
137   //
138   void removeDeadNodes(bool KeepAllGlobals = false, bool KeepCalls = true);
139
140   // CloneFlags enum - Bits that may be passed into the cloneInto method to
141   // specify how to clone the function graph.
142   enum CloneFlags {
143     StripAllocaBit        = 1 << 0, KeepAllocaBit     = 0 << 0,
144     DontCloneCallNodes    = 1 << 1, CloneCallNodes    = 0 << 0,
145     DontCloneAuxCallNodes = 1 << 2, CloneAuxCallNodes = 0 << 0,
146   };
147
148   // cloneInto - Clone the specified DSGraph into the current graph, returning
149   // the Return node of the graph.  The translated ScalarMap for the old
150   // function is filled into the OldValMap member.  If StripAllocas is set to
151   // 'StripAllocaBit', Alloca markers are removed from the graph as the graph is
152   // being cloned.
153   //
154   DSNodeHandle cloneInto(const DSGraph &G,
155                          std::map<Value*, DSNodeHandle> &OldValMap,
156                          std::map<const DSNode*, DSNodeHandle> &OldNodeMap,
157                          unsigned CloneFlags = 0);
158
159   /// mergeInGraph - The method is used for merging graphs together.  If the
160   /// argument graph is not *this, it makes a clone of the specified graph, then
161   /// merges the nodes specified in the call site with the formal arguments in
162   /// the graph.  If the StripAlloca's argument is 'StripAllocaBit' then Alloca
163   /// markers are removed from nodes.
164   ///
165   void mergeInGraph(DSCallSite &CS, const DSGraph &Graph, unsigned CloneFlags);
166
167 #if 0
168   // cloneGlobalInto - Clone the given global node (or the node for the given
169   // GlobalValue) from the GlobalsGraph and all its target links (recursively).
170   // 
171   DSNode* cloneGlobalInto(const DSNode* GNode);
172   DSNode* cloneGlobalInto(GlobalValue* GV) {
173     assert(!GV || (((DSGraph*) GlobalsGraph)->ScalarMap[GV] != 0));
174     return GV? cloneGlobalInto(((DSGraph*) GlobalsGraph)->ScalarMap[GV]) : 0;
175   }
176 #endif
177
178 private:
179   bool isNodeDead(DSNode *N);
180 };
181
182 #endif