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