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 // Allow explicit conversion to DSNode...
72 DSNode *getNode() const { return N; }
73 unsigned getOffset() const { return Offset; }
75 inline void setNode(DSNode *N); // Defined inline later...
76 void setOffset(unsigned O) { Offset = O; }
78 void addEdgeTo(unsigned LinkNo, const DSNodeHandle &N);
79 void addEdgeTo(const DSNodeHandle &N) { addEdgeTo(0, N); }
81 /// mergeWith - Merge the logical node pointed to by 'this' with the node
82 /// pointed to by 'N'.
84 void mergeWith(const DSNodeHandle &N);
86 // hasLink - Return true if there is a link at the specified offset...
87 inline bool hasLink(unsigned Num) const;
89 /// getLink - Treat this current node pointer as a pointer to a structure of
90 /// some sort. This method will return the pointer a mem[this+Num]
92 inline const DSNodeHandle &getLink(unsigned Num) const;
93 inline DSNodeHandle &getLink(unsigned Num);
95 inline void setLink(unsigned Num, const DSNodeHandle &NH);
99 //===----------------------------------------------------------------------===//
100 /// DSTypeRec - This structure is used to represent a single type that is held
104 const Type *Ty; // The type itself...
105 bool isArray; // Have we accessed an array of elements?
107 DSTypeRec(const Type *T = 0, bool A = false)
108 : Ty(T), isArray(A) {}
113 //===----------------------------------------------------------------------===//
114 /// DSCallSite - Representation of a call site via its call instruction,
115 /// the DSNode handle for the callee function (or function pointer), and
116 /// the DSNode handles for the function arguments.
118 /// One unusual aspect of this callsite record is the ResolvingCaller member.
119 /// If this is non-null, then it indicates the function that allowed a call-site
120 /// to finally be resolved. Because of indirect calls, this function may not
121 /// actually be the function that contains the Call instruction itself. This is
122 /// used by the BU and TD passes to communicate.
125 CallInst *Inst; // Actual call site
126 DSNodeHandle RetVal; // Returned value
127 DSNodeHandle Callee; // The function node called
128 std::vector<DSNodeHandle> CallArgs; // The pointer arguments
129 Function *ResolvingCaller; // See comments above
131 static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
132 const std::map<const DSNode*, DSNode*> &NodeMap) {
133 if (DSNode *N = Src.getNode()) {
134 std::map<const DSNode*, DSNode*>::const_iterator I = NodeMap.find(N);
135 assert(I != NodeMap.end() && "Not not in mapping!");
137 NH.setOffset(Src.getOffset());
138 NH.setNode(I->second);
142 static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
143 const std::map<const DSNode*, DSNodeHandle> &NodeMap) {
144 if (DSNode *N = Src.getNode()) {
145 std::map<const DSNode*, DSNodeHandle>::const_iterator I = NodeMap.find(N);
146 assert(I != NodeMap.end() && "Not not in mapping!");
148 NH.setOffset(Src.getOffset()+I->second.getOffset());
149 NH.setNode(I->second.getNode());
153 DSCallSite(); // DO NOT IMPLEMENT
155 /// Constructor. Note - This ctor destroys the argument vector passed in. On
156 /// exit, the argument vector is empty.
158 DSCallSite(CallInst &inst, const DSNodeHandle &rv, const DSNodeHandle &callee,
159 std::vector<DSNodeHandle> &Args)
160 : Inst(&inst), RetVal(rv), Callee(callee), ResolvingCaller(0) {
164 DSCallSite(const DSCallSite &DSCS) // Simple copy ctor
165 : Inst(DSCS.Inst), RetVal(DSCS.RetVal),
166 Callee(DSCS.Callee), CallArgs(DSCS.CallArgs),
167 ResolvingCaller(DSCS.ResolvingCaller) {}
169 /// Mapping copy constructor - This constructor takes a preexisting call site
170 /// to copy plus a map that specifies how the links should be transformed.
171 /// This is useful when moving a call site from one graph to another.
173 template<typename MapTy>
174 DSCallSite(const DSCallSite &FromCall, const MapTy &NodeMap) {
175 Inst = FromCall.Inst;
176 InitNH(RetVal, FromCall.RetVal, NodeMap);
177 InitNH(Callee, FromCall.Callee, NodeMap);
179 CallArgs.resize(FromCall.CallArgs.size());
180 for (unsigned i = 0, e = FromCall.CallArgs.size(); i != e; ++i)
181 InitNH(CallArgs[i], FromCall.CallArgs[i], NodeMap);
182 ResolvingCaller = FromCall.ResolvingCaller;
185 // Accessor functions...
186 Function &getCaller() const;
187 CallInst &getCallInst() const { return *Inst; }
188 DSNodeHandle &getRetVal() { return RetVal; }
189 DSNodeHandle &getCallee() { return Callee; }
190 const DSNodeHandle &getRetVal() const { return RetVal; }
191 const DSNodeHandle &getCallee() const { return Callee; }
192 void setCallee(const DSNodeHandle &H) { Callee = H; }
194 unsigned getNumPtrArgs() const { return CallArgs.size(); }
196 Function *getResolvingCaller() const { return ResolvingCaller; }
197 void setResolvingCaller(Function *F) { ResolvingCaller = F; }
199 DSNodeHandle &getPtrArg(unsigned i) {
200 assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
203 const DSNodeHandle &getPtrArg(unsigned i) const {
204 assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
208 bool operator<(const DSCallSite &CS) const {
209 if (RetVal < CS.RetVal) return true;
210 if (RetVal > CS.RetVal) return false;
211 if (Callee < CS.Callee) return true;
212 if (Callee > CS.Callee) return false;
213 return CallArgs < CS.CallArgs;
216 bool operator==(const DSCallSite &CS) const {
217 return RetVal == CS.RetVal && Callee == CS.Callee &&
218 CallArgs == CS.CallArgs;