Implement support for swapping. Callsites now sort by callee
[oota-llvm.git] / include / llvm / Analysis / DataStructure / 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 <map>
12 #include <functional>
13 #include <string>
14
15 class Function;
16 class CallInst;
17 class Value;
18 class GlobalValue;
19 class Type;
20
21 class DSNode;                  // Each node in the graph
22 class DSGraph;                 // A graph for a function
23 class DSNodeIterator;          // Data structure graph traversal iterator
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   DSNode *N;
49   unsigned Offset;
50 public:
51   // Allow construction, destruction, and assignment...
52   DSNodeHandle(DSNode *n = 0, unsigned offs = 0) : N(0), Offset(offs) {
53     setNode(n);
54   }
55   DSNodeHandle(const DSNodeHandle &H) : N(0), Offset(H.Offset) { setNode(H.N); }
56   ~DSNodeHandle() { setNode((DSNode*)0); }
57   DSNodeHandle &operator=(const DSNodeHandle &H) {
58     setNode(H.N); Offset = H.Offset;
59     return *this;
60   }
61
62   bool operator<(const DSNodeHandle &H) const {  // Allow sorting
63     return N < H.N || (N == H.N && Offset < H.Offset);
64   }
65   bool operator>(const DSNodeHandle &H) const { return H < *this; }
66   bool operator==(const DSNodeHandle &H) const { // Allow comparison
67     return N == H.N && Offset == H.Offset;
68   }
69   bool operator!=(const DSNodeHandle &H) const { return !operator==(H); }
70
71   inline void swap(DSNodeHandle &H);
72
73   // Allow explicit conversion to DSNode...
74   DSNode *getNode() const { return N; }
75   unsigned getOffset() const { return Offset; }
76
77   inline void setNode(DSNode *N);  // Defined inline later...
78   void setOffset(unsigned O) { Offset = O; }
79
80   void addEdgeTo(unsigned LinkNo, const DSNodeHandle &N);
81   void addEdgeTo(const DSNodeHandle &N) { addEdgeTo(0, N); }
82
83   /// mergeWith - Merge the logical node pointed to by 'this' with the node
84   /// pointed to by 'N'.
85   ///
86   void mergeWith(const DSNodeHandle &N);
87
88   // hasLink - Return true if there is a link at the specified offset...
89   inline bool hasLink(unsigned Num) const;
90
91   /// getLink - Treat this current node pointer as a pointer to a structure of
92   /// some sort.  This method will return the pointer a mem[this+Num]
93   ///
94   inline const DSNodeHandle &getLink(unsigned Num) const;
95   inline DSNodeHandle &getLink(unsigned Num);
96
97   inline void setLink(unsigned Num, const DSNodeHandle &NH);
98 };
99
100 inline void swap(DSNodeHandle &NH1, DSNodeHandle &NH2) { NH1.swap(NH2); }
101
102 //===----------------------------------------------------------------------===//
103 /// DSTypeRec - This structure is used to represent a single type that is held
104 /// in a DSNode.
105 ///
106 struct DSTypeRec {
107   const Type *Ty;                 // The type itself...
108   bool isArray;                   // Have we accessed an array of elements?
109   
110   DSTypeRec(const Type *T = 0, bool A = false)
111     : Ty(T), isArray(A) {}
112 };
113
114
115
116 //===----------------------------------------------------------------------===//
117 /// DSCallSite - Representation of a call site via its call instruction,
118 /// the DSNode handle for the callee function (or function pointer), and
119 /// the DSNode handles for the function arguments.
120 ///
121 /// One unusual aspect of this callsite record is the ResolvingCaller member.
122 /// If this is non-null, then it indicates the function that allowed a call-site
123 /// to finally be resolved.  Because of indirect calls, this function may not
124 /// actually be the function that contains the Call instruction itself.  This is
125 /// used by the BU and TD passes to communicate.
126 /// 
127 class DSCallSite {
128   CallInst    *Inst;                    // Actual call site
129   DSNodeHandle RetVal;                  // Returned value
130   DSNodeHandle Callee;                  // The function node called
131   std::vector<DSNodeHandle> CallArgs;   // The pointer arguments
132   Function    *ResolvingCaller;         // See comments above
133
134   static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
135                      const std::map<const DSNode*, DSNode*> &NodeMap) {
136     if (DSNode *N = Src.getNode()) {
137       std::map<const DSNode*, DSNode*>::const_iterator I = NodeMap.find(N);
138       assert(I != NodeMap.end() && "Not not in mapping!");
139
140       NH.setOffset(Src.getOffset());
141       NH.setNode(I->second);
142     }
143   }
144
145   static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
146                      const std::map<const DSNode*, DSNodeHandle> &NodeMap) {
147     if (DSNode *N = Src.getNode()) {
148       std::map<const DSNode*, DSNodeHandle>::const_iterator I = NodeMap.find(N);
149       assert(I != NodeMap.end() && "Not not in mapping!");
150
151       NH.setOffset(Src.getOffset()+I->second.getOffset());
152       NH.setNode(I->second.getNode());
153     }
154   }
155
156   DSCallSite();                         // DO NOT IMPLEMENT
157 public:
158   /// Constructor.  Note - This ctor destroys the argument vector passed in.  On
159   /// exit, the argument vector is empty.
160   ///
161   DSCallSite(CallInst &inst, const DSNodeHandle &rv, const DSNodeHandle &callee,
162              std::vector<DSNodeHandle> &Args)
163     : Inst(&inst), RetVal(rv), Callee(callee), ResolvingCaller(0) {
164     Args.swap(CallArgs);
165   }
166
167   DSCallSite(const DSCallSite &DSCS)   // Simple copy ctor
168     : Inst(DSCS.Inst), RetVal(DSCS.RetVal),
169       Callee(DSCS.Callee), CallArgs(DSCS.CallArgs),
170       ResolvingCaller(DSCS.ResolvingCaller) {}
171
172   /// Mapping copy constructor - This constructor takes a preexisting call site
173   /// to copy plus a map that specifies how the links should be transformed.
174   /// This is useful when moving a call site from one graph to another.
175   ///
176   template<typename MapTy>
177   DSCallSite(const DSCallSite &FromCall, const MapTy &NodeMap) {
178     Inst = FromCall.Inst;
179     InitNH(RetVal, FromCall.RetVal, NodeMap);
180     InitNH(Callee, FromCall.Callee, NodeMap);
181
182     CallArgs.resize(FromCall.CallArgs.size());
183     for (unsigned i = 0, e = FromCall.CallArgs.size(); i != e; ++i)
184       InitNH(CallArgs[i], FromCall.CallArgs[i], NodeMap);
185     ResolvingCaller = FromCall.ResolvingCaller;
186   }
187
188   // Accessor functions...
189   Function           &getCaller()     const;
190   CallInst           &getCallInst()   const { return *Inst; }
191         DSNodeHandle &getRetVal()           { return RetVal; }
192         DSNodeHandle &getCallee()           { return Callee; }
193   const DSNodeHandle &getRetVal()     const { return RetVal; }
194   const DSNodeHandle &getCallee()     const { return Callee; }
195   void setCallee(const DSNodeHandle &H) { Callee = H; }
196
197   unsigned            getNumPtrArgs() const { return CallArgs.size(); }
198
199   Function           *getResolvingCaller() const { return ResolvingCaller; }
200   void setResolvingCaller(Function *F) { ResolvingCaller = F; }
201
202   DSNodeHandle &getPtrArg(unsigned i) {
203     assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
204     return CallArgs[i];
205   }
206   const DSNodeHandle &getPtrArg(unsigned i) const {
207     assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
208     return CallArgs[i];
209   }
210
211   void swap(DSCallSite &CS) {
212     if (this != &CS) {
213       std::swap(Inst, CS.Inst);
214       std::swap(RetVal, CS.RetVal);
215       std::swap(Callee, CS.Callee);
216       std::swap(CallArgs, CS.CallArgs);
217       std::swap(ResolvingCaller, CS.ResolvingCaller);
218     }
219   }
220
221   bool operator<(const DSCallSite &CS) const {
222     if (Callee < CS.Callee) return true;   // This must sort by callee first!
223     if (Callee > CS.Callee) return false;
224     if (RetVal < CS.RetVal) return true;
225     if (RetVal > CS.RetVal) return false;
226     return CallArgs < CS.CallArgs;
227   }
228
229   bool operator==(const DSCallSite &CS) const {
230     return RetVal == CS.RetVal && Callee == CS.Callee &&
231            CallArgs == CS.CallArgs;
232   }
233 };
234
235 inline void swap(DSCallSite &CS1, DSCallSite &CS2) { CS1.swap(CS2); }
236
237 #endif