Make sure to print indirect node again
[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 <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
24 namespace DS { // FIXME: After the paper, this should get cleaned up
25   enum { PointerShift = 3,     // 64bit ptrs = 3, 32 bit ptrs = 2
26          PointerSize = 1 << PointerShift
27   };
28
29   // isPointerType - Return true if this first class type is big enough to hold
30   // a pointer.
31   //
32   bool isPointerType(const Type *Ty);
33 };
34
35 //===----------------------------------------------------------------------===//
36 /// DSNodeHandle - Implement a "handle" to a data structure node that takes care
37 /// of all of the add/un'refing of the node to prevent the backpointers in the
38 /// graph from getting out of date.  This class represents a "pointer" in the
39 /// graph, whose destination is an indexed offset into a node.
40 ///
41 /// Note: some functions that are marked as inline in DSNodeHandle are actually
42 /// defined in DSNode.h because they need knowledge of DSNode operation. Putting
43 /// them in a CPP file wouldn't help making them inlined and keeping DSNode and
44 /// DSNodeHandle (and friends) in one file complicates things.
45 ///
46 class DSNodeHandle {
47   DSNode *N;
48   unsigned Offset;
49 public:
50   // Allow construction, destruction, and assignment...
51   DSNodeHandle(DSNode *n = 0, unsigned offs = 0) : N(0), Offset(offs) {
52     setNode(n);
53   }
54   DSNodeHandle(const DSNodeHandle &H) : N(0), Offset(H.Offset) { setNode(H.N); }
55   ~DSNodeHandle() { setNode((DSNode*)0); }
56   DSNodeHandle &operator=(const DSNodeHandle &H) {
57     setNode(H.N); Offset = H.Offset;
58     return *this;
59   }
60
61   bool operator<(const DSNodeHandle &H) const {  // Allow sorting
62     return N < H.N || (N == H.N && Offset < H.Offset);
63   }
64   bool operator>(const DSNodeHandle &H) const { return H < *this; }
65   bool operator==(const DSNodeHandle &H) const { // Allow comparison
66     return N == H.N && Offset == H.Offset;
67   }
68   bool operator!=(const DSNodeHandle &H) const { return !operator==(H); }
69
70   inline void swap(DSNodeHandle &H);
71
72   // Allow explicit conversion to DSNode...
73   DSNode *getNode() const { return N; }
74   unsigned getOffset() const { return Offset; }
75
76   inline void setNode(DSNode *N);  // Defined inline later...
77   void setOffset(unsigned O) { Offset = O; }
78
79   void addEdgeTo(unsigned LinkNo, const DSNodeHandle &N);
80   void addEdgeTo(const DSNodeHandle &N) { addEdgeTo(0, N); }
81
82   /// mergeWith - Merge the logical node pointed to by 'this' with the node
83   /// pointed to by 'N'.
84   ///
85   void mergeWith(const DSNodeHandle &N);
86
87   // hasLink - Return true if there is a link at the specified offset...
88   inline bool hasLink(unsigned Num) const;
89
90   /// getLink - Treat this current node pointer as a pointer to a structure of
91   /// some sort.  This method will return the pointer a mem[this+Num]
92   ///
93   inline const DSNodeHandle &getLink(unsigned Num) const;
94   inline DSNodeHandle &getLink(unsigned Num);
95
96   inline void setLink(unsigned Num, const DSNodeHandle &NH);
97 };
98
99 inline void swap(DSNodeHandle &NH1, DSNodeHandle &NH2) { NH1.swap(NH2); }
100
101 //===----------------------------------------------------------------------===//
102 /// DSCallSite - Representation of a call site via its call instruction,
103 /// the DSNode handle for the callee function (or function pointer), and
104 /// the DSNode handles for the function arguments.
105 ///
106 /// One unusual aspect of this callsite record is the ResolvingCaller member.
107 /// If this is non-null, then it indicates the function that allowed a call-site
108 /// to finally be resolved.  Because of indirect calls, this function may not
109 /// actually be the function that contains the Call instruction itself.  This is
110 /// used by the BU and TD passes to communicate.
111 /// 
112 class DSCallSite {
113   CallInst    *Inst;                    // Actual call site
114   DSNodeHandle RetVal;                  // Returned value
115   DSNodeHandle Callee;                  // The function node called
116   std::vector<DSNodeHandle> CallArgs;   // The pointer arguments
117   Function    *ResolvingCaller;         // See comments above
118
119   static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
120                      const std::map<const DSNode*, DSNode*> &NodeMap) {
121     if (DSNode *N = Src.getNode()) {
122       std::map<const DSNode*, DSNode*>::const_iterator I = NodeMap.find(N);
123       assert(I != NodeMap.end() && "Not not in mapping!");
124
125       NH.setOffset(Src.getOffset());
126       NH.setNode(I->second);
127     }
128   }
129
130   static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
131                      const std::map<const DSNode*, DSNodeHandle> &NodeMap) {
132     if (DSNode *N = Src.getNode()) {
133       std::map<const DSNode*, DSNodeHandle>::const_iterator I = NodeMap.find(N);
134       assert(I != NodeMap.end() && "Not not in mapping!");
135
136       NH.setOffset(Src.getOffset()+I->second.getOffset());
137       NH.setNode(I->second.getNode());
138     }
139   }
140
141   DSCallSite();                         // DO NOT IMPLEMENT
142 public:
143   /// Constructor.  Note - This ctor destroys the argument vector passed in.  On
144   /// exit, the argument vector is empty.
145   ///
146   DSCallSite(CallInst &inst, const DSNodeHandle &rv, const DSNodeHandle &callee,
147              std::vector<DSNodeHandle> &Args)
148     : Inst(&inst), RetVal(rv), Callee(callee), ResolvingCaller(0) {
149     Args.swap(CallArgs);
150   }
151
152   DSCallSite(const DSCallSite &DSCS)   // Simple copy ctor
153     : Inst(DSCS.Inst), RetVal(DSCS.RetVal),
154       Callee(DSCS.Callee), CallArgs(DSCS.CallArgs),
155       ResolvingCaller(DSCS.ResolvingCaller) {}
156
157   /// Mapping copy constructor - This constructor takes a preexisting call site
158   /// to copy plus a map that specifies how the links should be transformed.
159   /// This is useful when moving a call site from one graph to another.
160   ///
161   template<typename MapTy>
162   DSCallSite(const DSCallSite &FromCall, const MapTy &NodeMap) {
163     Inst = FromCall.Inst;
164     InitNH(RetVal, FromCall.RetVal, NodeMap);
165     InitNH(Callee, FromCall.Callee, NodeMap);
166
167     CallArgs.resize(FromCall.CallArgs.size());
168     for (unsigned i = 0, e = FromCall.CallArgs.size(); i != e; ++i)
169       InitNH(CallArgs[i], FromCall.CallArgs[i], NodeMap);
170     ResolvingCaller = FromCall.ResolvingCaller;
171   }
172
173   // Accessor functions...
174   Function           &getCaller()     const;
175   CallInst           &getCallInst()   const { return *Inst; }
176         DSNodeHandle &getRetVal()           { return RetVal; }
177         DSNodeHandle &getCallee()           { return Callee; }
178   const DSNodeHandle &getRetVal()     const { return RetVal; }
179   const DSNodeHandle &getCallee()     const { return Callee; }
180   void setCallee(const DSNodeHandle &H) { Callee = H; }
181
182   unsigned            getNumPtrArgs() const { return CallArgs.size(); }
183
184   Function           *getResolvingCaller() const { return ResolvingCaller; }
185   void setResolvingCaller(Function *F) { ResolvingCaller = F; }
186
187   DSNodeHandle &getPtrArg(unsigned i) {
188     assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
189     return CallArgs[i];
190   }
191   const DSNodeHandle &getPtrArg(unsigned i) const {
192     assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
193     return CallArgs[i];
194   }
195
196   void swap(DSCallSite &CS) {
197     if (this != &CS) {
198       std::swap(Inst, CS.Inst);
199       std::swap(RetVal, CS.RetVal);
200       std::swap(Callee, CS.Callee);
201       std::swap(CallArgs, CS.CallArgs);
202       std::swap(ResolvingCaller, CS.ResolvingCaller);
203     }
204   }
205
206   // MergeWith - Merge the return value and parameters of the these two call
207   // sites.
208   void mergeWith(DSCallSite &CS) {
209     getRetVal().mergeWith(CS.getRetVal());
210     unsigned MinArgs = getNumPtrArgs();
211     if (CS.getNumPtrArgs() < MinArgs) MinArgs = CS.getNumPtrArgs();
212
213     for (unsigned a = 0; a != MinArgs; ++a)
214       getPtrArg(a).mergeWith(CS.getPtrArg(a));
215   }
216
217   bool operator<(const DSCallSite &CS) const {
218     if (Callee < CS.Callee) return true;   // This must sort by callee first!
219     if (Callee > CS.Callee) return false;
220     if (RetVal < CS.RetVal) return true;
221     if (RetVal > CS.RetVal) return false;
222     return CallArgs < CS.CallArgs;
223   }
224
225   bool operator==(const DSCallSite &CS) const {
226     return RetVal == CS.RetVal && Callee == CS.Callee &&
227            CallArgs == CS.CallArgs;
228   }
229 };
230
231 inline void swap(DSCallSite &CS1, DSCallSite &CS2) { CS1.swap(CS2); }
232
233 #endif