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