1 //===- DSSupport.h - Support for datastructure graphs -----------*- C++ -*-===//
3 // Support for graph nodes, call sites, and types.
5 //===----------------------------------------------------------------------===//
7 #ifndef LLVM_ANALYSIS_DSSUPPORT_H
8 #define LLVM_ANALYSIS_DSSUPPORT_H
21 class DSNode; // Each node in the graph
22 class DSGraph; // A graph for a function
23 class DSNodeIterator; // Data structure graph traversal iterator
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
30 // isPointerType - Return true if this first class type is big enough to hold
33 bool isPointerType(const Type *Ty);
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.
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.
51 // Allow construction, destruction, and assignment...
52 DSNodeHandle(DSNode *n = 0, unsigned offs = 0) : N(0), Offset(offs) {
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;
62 bool operator<(const DSNodeHandle &H) const { // Allow sorting
63 return N < H.N || (N == H.N && Offset < H.Offset);
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;
69 bool operator!=(const DSNodeHandle &H) const { return !operator==(H); }
71 inline void swap(DSNodeHandle &H);
73 // Allow explicit conversion to DSNode...
74 DSNode *getNode() const { return N; }
75 unsigned getOffset() const { return Offset; }
77 inline void setNode(DSNode *N); // Defined inline later...
78 void setOffset(unsigned O) { Offset = O; }
80 void addEdgeTo(unsigned LinkNo, const DSNodeHandle &N);
81 void addEdgeTo(const DSNodeHandle &N) { addEdgeTo(0, N); }
83 /// mergeWith - Merge the logical node pointed to by 'this' with the node
84 /// pointed to by 'N'.
86 void mergeWith(const DSNodeHandle &N);
88 // hasLink - Return true if there is a link at the specified offset...
89 inline bool hasLink(unsigned Num) const;
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]
94 inline const DSNodeHandle &getLink(unsigned Num) const;
95 inline DSNodeHandle &getLink(unsigned Num);
97 inline void setLink(unsigned Num, const DSNodeHandle &NH);
100 inline void swap(DSNodeHandle &NH1, DSNodeHandle &NH2) { NH1.swap(NH2); }
102 //===----------------------------------------------------------------------===//
103 /// DSTypeRec - This structure is used to represent a single type that is held
107 const Type *Ty; // The type itself...
108 bool isArray; // Have we accessed an array of elements?
110 DSTypeRec(const Type *T = 0, bool A = false)
111 : Ty(T), isArray(A) {}
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.
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.
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
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!");
140 NH.setOffset(Src.getOffset());
141 NH.setNode(I->second);
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!");
151 NH.setOffset(Src.getOffset()+I->second.getOffset());
152 NH.setNode(I->second.getNode());
156 DSCallSite(); // DO NOT IMPLEMENT
158 /// Constructor. Note - This ctor destroys the argument vector passed in. On
159 /// exit, the argument vector is empty.
161 DSCallSite(CallInst &inst, const DSNodeHandle &rv, const DSNodeHandle &callee,
162 std::vector<DSNodeHandle> &Args)
163 : Inst(&inst), RetVal(rv), Callee(callee), ResolvingCaller(0) {
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) {}
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.
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);
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;
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; }
197 unsigned getNumPtrArgs() const { return CallArgs.size(); }
199 Function *getResolvingCaller() const { return ResolvingCaller; }
200 void setResolvingCaller(Function *F) { ResolvingCaller = F; }
202 DSNodeHandle &getPtrArg(unsigned i) {
203 assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
206 const DSNodeHandle &getPtrArg(unsigned i) const {
207 assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
211 void swap(DSCallSite &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);
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;
229 bool operator==(const DSCallSite &CS) const {
230 return RetVal == CS.RetVal && Callee == CS.Callee &&
231 CallArgs == CS.CallArgs;
235 inline void swap(DSCallSite &CS1, DSCallSite &CS2) { CS1.swap(CS2); }