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