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
22 class DSNode; // Each node in the graph
23 class DSGraph; // A graph for a function
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 /// DSCallSite - Representation of a call site via its call instruction,
104 /// the DSNode handle for the callee function (or function pointer), and
105 /// the DSNode handles for the function arguments.
107 /// One unusual aspect of this callsite record is the ResolvingCaller member.
108 /// If this is non-null, then it indicates the function that allowed a call-site
109 /// to finally be resolved. Because of indirect calls, this function may not
110 /// actually be the function that contains the Call instruction itself. This is
111 /// used by the BU and TD passes to communicate.
114 CallInst *Inst; // Actual call site
115 DSNodeHandle RetVal; // Returned value
116 DSNodeHandle Callee; // The function node called
117 std::vector<DSNodeHandle> CallArgs; // The pointer arguments
118 Function *ResolvingCaller; // See comments above
120 static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
121 const std::map<const DSNode*, DSNode*> &NodeMap) {
122 if (DSNode *N = Src.getNode()) {
123 std::map<const DSNode*, DSNode*>::const_iterator I = NodeMap.find(N);
124 assert(I != NodeMap.end() && "Not not in mapping!");
126 NH.setOffset(Src.getOffset());
127 NH.setNode(I->second);
131 static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
132 const std::map<const DSNode*, DSNodeHandle> &NodeMap) {
133 if (DSNode *N = Src.getNode()) {
134 std::map<const DSNode*, DSNodeHandle>::const_iterator I = NodeMap.find(N);
135 assert(I != NodeMap.end() && "Not not in mapping!");
137 NH.setOffset(Src.getOffset()+I->second.getOffset());
138 NH.setNode(I->second.getNode());
142 DSCallSite(); // DO NOT IMPLEMENT
144 /// Constructor. Note - This ctor destroys the argument vector passed in. On
145 /// exit, the argument vector is empty.
147 DSCallSite(CallInst &inst, const DSNodeHandle &rv, const DSNodeHandle &callee,
148 std::vector<DSNodeHandle> &Args)
149 : Inst(&inst), RetVal(rv), Callee(callee), ResolvingCaller(0) {
153 DSCallSite(const DSCallSite &DSCS) // Simple copy ctor
154 : Inst(DSCS.Inst), RetVal(DSCS.RetVal),
155 Callee(DSCS.Callee), CallArgs(DSCS.CallArgs),
156 ResolvingCaller(DSCS.ResolvingCaller) {}
158 /// Mapping copy constructor - This constructor takes a preexisting call site
159 /// to copy plus a map that specifies how the links should be transformed.
160 /// This is useful when moving a call site from one graph to another.
162 template<typename MapTy>
163 DSCallSite(const DSCallSite &FromCall, const MapTy &NodeMap) {
164 Inst = FromCall.Inst;
165 InitNH(RetVal, FromCall.RetVal, NodeMap);
166 InitNH(Callee, FromCall.Callee, NodeMap);
168 CallArgs.resize(FromCall.CallArgs.size());
169 for (unsigned i = 0, e = FromCall.CallArgs.size(); i != e; ++i)
170 InitNH(CallArgs[i], FromCall.CallArgs[i], NodeMap);
171 ResolvingCaller = FromCall.ResolvingCaller;
174 // Accessor functions...
175 Function &getCaller() const;
176 CallInst &getCallInst() const { return *Inst; }
177 DSNodeHandle &getRetVal() { return RetVal; }
178 DSNodeHandle &getCallee() { return Callee; }
179 const DSNodeHandle &getRetVal() const { return RetVal; }
180 const DSNodeHandle &getCallee() const { return Callee; }
181 void setCallee(const DSNodeHandle &H) { Callee = H; }
183 unsigned getNumPtrArgs() const { return CallArgs.size(); }
185 Function *getResolvingCaller() const { return ResolvingCaller; }
186 void setResolvingCaller(Function *F) { ResolvingCaller = F; }
188 DSNodeHandle &getPtrArg(unsigned i) {
189 assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
192 const DSNodeHandle &getPtrArg(unsigned i) const {
193 assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
197 void swap(DSCallSite &CS) {
199 std::swap(Inst, CS.Inst);
200 std::swap(RetVal, CS.RetVal);
201 std::swap(Callee, CS.Callee);
202 std::swap(CallArgs, CS.CallArgs);
203 std::swap(ResolvingCaller, CS.ResolvingCaller);
207 // MergeWith - Merge the return value and parameters of the these two call
209 void mergeWith(DSCallSite &CS) {
210 getRetVal().mergeWith(CS.getRetVal());
211 unsigned MinArgs = getNumPtrArgs();
212 if (CS.getNumPtrArgs() < MinArgs) MinArgs = CS.getNumPtrArgs();
214 for (unsigned a = 0; a != MinArgs; ++a)
215 getPtrArg(a).mergeWith(CS.getPtrArg(a));
218 /// markReachableNodes - This method recursively traverses the specified
219 /// DSNodes, marking any nodes which are reachable. All reachable nodes it
220 /// adds to the set, which allows it to only traverse visited nodes once.
222 void markReachableNodes(std::set<DSNode*> &Nodes);
224 bool operator<(const DSCallSite &CS) const {
225 if (Callee < CS.Callee) return true; // This must sort by callee first!
226 if (Callee > CS.Callee) return false;
227 if (RetVal < CS.RetVal) return true;
228 if (RetVal > CS.RetVal) return false;
229 return CallArgs < CS.CallArgs;
232 bool operator==(const DSCallSite &CS) const {
233 return RetVal == CS.RetVal && Callee == CS.Callee &&
234 CallArgs == CS.CallArgs;
238 inline void swap(DSCallSite &CS1, DSCallSite &CS2) { CS1.swap(CS2); }