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