Eliminated the CompletedNodes argument to the cloneReachable* methods. This
[oota-llvm.git] / include / llvm / Analysis / DSSupport.h
1 //===- DSSupport.h - Support for datastructure graphs -----------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // Support for graph nodes, call sites, and types.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ANALYSIS_DSSUPPORT_H
15 #define LLVM_ANALYSIS_DSSUPPORT_H
16
17 #include <functional>
18 #include "Support/hash_set"
19 #include "llvm/Support/CallSite.h"
20
21 namespace llvm {
22
23 class Function;
24 class CallInst;
25 class Value;
26 class GlobalValue;
27 class Type;
28
29 class DSNode;                  // Each node in the graph
30 class DSGraph;                 // A graph for a function
31
32 namespace DS { // FIXME: After the paper, this should get cleaned up
33   enum { PointerShift = 2,     // 64bit ptrs = 3, 32 bit ptrs = 2
34          PointerSize = 1 << PointerShift
35   };
36
37   // isPointerType - Return true if this first class type is big enough to hold
38   // a pointer.
39   //
40   bool isPointerType(const Type *Ty);
41 };
42
43 //===----------------------------------------------------------------------===//
44 /// DSNodeHandle - Implement a "handle" to a data structure node that takes care
45 /// of all of the add/un'refing of the node to prevent the backpointers in the
46 /// graph from getting out of date.  This class represents a "pointer" in the
47 /// graph, whose destination is an indexed offset into a node.
48 ///
49 /// Note: some functions that are marked as inline in DSNodeHandle are actually
50 /// defined in DSNode.h because they need knowledge of DSNode operation. Putting
51 /// them in a CPP file wouldn't help making them inlined and keeping DSNode and
52 /// DSNodeHandle (and friends) in one file complicates things.
53 ///
54 class DSNodeHandle {
55   mutable DSNode *N;
56   mutable unsigned Offset;
57   void operator==(const DSNode *N);  // DISALLOW, use to promote N to nodehandle
58 public:
59   // Allow construction, destruction, and assignment...
60   DSNodeHandle(DSNode *n = 0, unsigned offs = 0) : N(0), Offset(offs) {
61     setNode(n);
62   }
63   DSNodeHandle(const DSNodeHandle &H) : N(0), Offset(0) {
64     setNode(H.getNode());
65     Offset = H.Offset;      // Must read offset AFTER the getNode()
66   }
67   ~DSNodeHandle() { setNode((DSNode*)0); }
68   DSNodeHandle &operator=(const DSNodeHandle &H) {
69     if (&H == this) return *this;  // Don't set offset to 0 if self assigning.
70     Offset = 0; setNode(H.getNode()); Offset = H.Offset;
71     return *this;
72   }
73
74   bool operator<(const DSNodeHandle &H) const {  // Allow sorting
75     return getNode() < H.getNode() || (N == H.N && Offset < H.Offset);
76   }
77   bool operator>(const DSNodeHandle &H) const { return H < *this; }
78   bool operator==(const DSNodeHandle &H) const { // Allow comparison
79     return getNode() == H.getNode() && Offset == H.Offset;
80   }
81   bool operator!=(const DSNodeHandle &H) const { return !operator==(H); }
82
83   inline void swap(DSNodeHandle &NH) {
84     std::swap(Offset, NH.Offset);
85     std::swap(N, NH.N);
86   }
87
88   /// isNull - Check to see if getNode() == 0, without going through the trouble
89   /// of checking to see if we are forwarding...
90   bool isNull() const { return N == 0; }
91
92   // Allow explicit conversion to DSNode...
93   inline DSNode *getNode() const;  // Defined inline in DSNode.h
94   unsigned getOffset() const { return Offset; }
95
96   inline void setNode(DSNode *N) const;  // Defined inline in DSNode.h
97   void setOffset(unsigned O) {
98     //assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
99     //       !N->ForwardNH.isNull()) && "Node handle offset out of range!");
100     //assert((!N || O < N->Size || (N->Size == 0 && O == 0) ||
101     //       !N->ForwardNH.isNull()) && "Node handle offset out of range!");
102     Offset = O;
103   }
104
105   void addEdgeTo(unsigned LinkNo, const DSNodeHandle &N);
106   void addEdgeTo(const DSNodeHandle &N) { addEdgeTo(0, N); }
107
108   /// mergeWith - Merge the logical node pointed to by 'this' with the node
109   /// pointed to by 'N'.
110   ///
111   void mergeWith(const DSNodeHandle &N) const;
112
113   // hasLink - Return true if there is a link at the specified offset...
114   inline bool hasLink(unsigned Num) const;
115
116   /// getLink - Treat this current node pointer as a pointer to a structure of
117   /// some sort.  This method will return the pointer a mem[this+Num]
118   ///
119   inline const DSNodeHandle &getLink(unsigned Num) const;
120   inline DSNodeHandle &getLink(unsigned Num);
121
122   inline void setLink(unsigned Num, const DSNodeHandle &NH);
123 private:
124   DSNode *HandleForwarding() const;
125 };
126
127 } // End llvm namespace
128
129 namespace std {
130   template<>
131   inline void swap<llvm::DSNodeHandle>(llvm::DSNodeHandle &NH1, llvm::DSNodeHandle &NH2) { NH1.swap(NH2); }
132 }
133
134 namespace llvm {
135
136 //===----------------------------------------------------------------------===//
137 /// DSCallSite - Representation of a call site via its call instruction,
138 /// the DSNode handle for the callee function (or function pointer), and
139 /// the DSNode handles for the function arguments.
140 /// 
141 class DSCallSite {
142   CallSite     Site;                 // Actual call site
143   Function    *CalleeF;              // The function called (direct call)
144   DSNodeHandle CalleeN;              // The function node called (indirect call)
145   DSNodeHandle RetVal;               // Returned value
146   std::vector<DSNodeHandle> CallArgs;// The pointer arguments
147
148   static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
149                      const hash_map<const DSNode*, DSNode*> &NodeMap) {
150     if (DSNode *N = Src.getNode()) {
151       hash_map<const DSNode*, DSNode*>::const_iterator I = NodeMap.find(N);
152       assert(I != NodeMap.end() && "Not not in mapping!");
153
154       NH.setOffset(Src.getOffset());
155       NH.setNode(I->second);
156     }
157   }
158
159   static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
160                      const hash_map<const DSNode*, DSNodeHandle> &NodeMap) {
161     if (DSNode *N = Src.getNode()) {
162       hash_map<const DSNode*, DSNodeHandle>::const_iterator I = NodeMap.find(N);
163       assert(I != NodeMap.end() && "Not not in mapping!");
164
165       NH.setOffset(Src.getOffset()+I->second.getOffset());
166       NH.setNode(I->second.getNode());
167     }
168   }
169
170   DSCallSite();                         // DO NOT IMPLEMENT
171 public:
172   /// Constructor.  Note - This ctor destroys the argument vector passed in.  On
173   /// exit, the argument vector is empty.
174   ///
175   DSCallSite(CallSite CS, const DSNodeHandle &rv, DSNode *Callee,
176              std::vector<DSNodeHandle> &Args)
177     : Site(CS), CalleeF(0), CalleeN(Callee), RetVal(rv) {
178     assert(Callee && "Null callee node specified for call site!");
179     Args.swap(CallArgs);
180   }
181   DSCallSite(CallSite CS, const DSNodeHandle &rv, Function *Callee,
182              std::vector<DSNodeHandle> &Args)
183     : Site(CS), CalleeF(Callee), RetVal(rv) {
184     assert(Callee && "Null callee function specified for call site!");
185     Args.swap(CallArgs);
186   }
187
188   DSCallSite(const DSCallSite &DSCS)   // Simple copy ctor
189     : Site(DSCS.Site), CalleeF(DSCS.CalleeF), CalleeN(DSCS.CalleeN),
190       RetVal(DSCS.RetVal), CallArgs(DSCS.CallArgs) {}
191
192   /// Mapping copy constructor - This constructor takes a preexisting call site
193   /// to copy plus a map that specifies how the links should be transformed.
194   /// This is useful when moving a call site from one graph to another.
195   ///
196   template<typename MapTy>
197   DSCallSite(const DSCallSite &FromCall, const MapTy &NodeMap) {
198     Site = FromCall.Site;
199     InitNH(RetVal, FromCall.RetVal, NodeMap);
200     InitNH(CalleeN, FromCall.CalleeN, NodeMap);
201     CalleeF = FromCall.CalleeF;
202
203     CallArgs.resize(FromCall.CallArgs.size());
204     for (unsigned i = 0, e = FromCall.CallArgs.size(); i != e; ++i)
205       InitNH(CallArgs[i], FromCall.CallArgs[i], NodeMap);
206   }
207
208   const DSCallSite &operator=(const DSCallSite &RHS) {
209     Site     = RHS.Site;
210     CalleeF  = RHS.CalleeF;
211     CalleeN  = RHS.CalleeN;
212     RetVal   = RHS.RetVal;
213     CallArgs = RHS.CallArgs;
214     return *this;
215   }
216
217   /// isDirectCall - Return true if this call site is a direct call of the
218   /// function specified by getCalleeFunc.  If not, it is an indirect call to
219   /// the node specified by getCalleeNode.
220   ///
221   bool isDirectCall() const { return CalleeF != 0; }
222   bool isIndirectCall() const { return !isDirectCall(); }
223
224
225   // Accessor functions...
226   Function           &getCaller()     const;
227   CallSite            getCallSite()   const { return Site; }
228         DSNodeHandle &getRetVal()           { return RetVal; }
229   const DSNodeHandle &getRetVal()     const { return RetVal; }
230
231   DSNode *getCalleeNode() const {
232     assert(!CalleeF && CalleeN.getNode()); return CalleeN.getNode();
233   }
234   Function *getCalleeFunc() const {
235     assert(!CalleeN.getNode() && CalleeF); return CalleeF;
236   }
237
238   unsigned            getNumPtrArgs() const { return CallArgs.size(); }
239
240   DSNodeHandle &getPtrArg(unsigned i) {
241     assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
242     return CallArgs[i];
243   }
244   const DSNodeHandle &getPtrArg(unsigned i) const {
245     assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
246     return CallArgs[i];
247   }
248
249   void swap(DSCallSite &CS) {
250     if (this != &CS) {
251       std::swap(Site, CS.Site);
252       std::swap(RetVal, CS.RetVal);
253       std::swap(CalleeN, CS.CalleeN);
254       std::swap(CalleeF, CS.CalleeF);
255       std::swap(CallArgs, CS.CallArgs);
256     }
257   }
258
259   // MergeWith - Merge the return value and parameters of the these two call
260   // sites.
261   void mergeWith(DSCallSite &CS) {
262     getRetVal().mergeWith(CS.getRetVal());
263     unsigned MinArgs = getNumPtrArgs();
264     if (CS.getNumPtrArgs() < MinArgs) MinArgs = CS.getNumPtrArgs();
265
266     for (unsigned a = 0; a != MinArgs; ++a)
267       getPtrArg(a).mergeWith(CS.getPtrArg(a));
268   }
269
270   /// markReachableNodes - This method recursively traverses the specified
271   /// DSNodes, marking any nodes which are reachable.  All reachable nodes it
272   /// adds to the set, which allows it to only traverse visited nodes once.
273   ///
274   void markReachableNodes(hash_set<DSNode*> &Nodes);
275
276   bool operator<(const DSCallSite &CS) const {
277     if (isDirectCall()) {      // This must sort by callee first!
278       if (CS.isIndirectCall()) return true;
279       if (CalleeF < CS.CalleeF) return true;
280       if (CalleeF > CS.CalleeF) return false;
281     } else {
282       if (CS.isDirectCall()) return false;
283       if (CalleeN < CS.CalleeN) return true;
284       if (CalleeN > CS.CalleeN) return false;
285     }
286     if (RetVal < CS.RetVal) return true;
287     if (RetVal > CS.RetVal) return false;
288     return CallArgs < CS.CallArgs;
289   }
290
291   bool operator==(const DSCallSite &CS) const {
292     return RetVal == CS.RetVal && CalleeN == CS.CalleeN &&
293            CalleeF == CS.CalleeF && CallArgs == CS.CallArgs;
294   }
295 };
296
297 } // End llvm namespace
298
299 namespace std {
300   template<>
301   inline void swap<llvm::DSCallSite>(llvm::DSCallSite &CS1,
302                                      llvm::DSCallSite &CS2) { CS1.swap(CS2); }
303 }
304 #endif