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
13 #include "Support/HashExtras.h"
14 #include "Support/hash_set"
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.
49 mutable unsigned Offset;
50 void operator==(const DSNode *N); // DISALLOW, use to promote N to nodehandle
52 // Allow construction, destruction, and assignment...
53 DSNodeHandle(DSNode *n = 0, unsigned offs = 0) : N(0), Offset(offs) {
56 DSNodeHandle(const DSNodeHandle &H) : N(0), Offset(0) {
58 Offset = H.Offset; // Must read offset AFTER the getNode()
60 ~DSNodeHandle() { setNode((DSNode*)0); }
61 DSNodeHandle &operator=(const DSNodeHandle &H) {
62 Offset = 0; setNode(H.getNode()); Offset = H.Offset;
66 bool operator<(const DSNodeHandle &H) const { // Allow sorting
67 return getNode() < H.getNode() || (N == H.N && Offset < H.Offset);
69 bool operator>(const DSNodeHandle &H) const { return H < *this; }
70 bool operator==(const DSNodeHandle &H) const { // Allow comparison
71 return getNode() == H.getNode() && Offset == H.Offset;
73 bool operator!=(const DSNodeHandle &H) const { return !operator==(H); }
75 inline void swap(DSNodeHandle &NH) {
76 std::swap(Offset, NH.Offset);
80 /// isNull - Check to see if getNode() == 0, without going through the trouble
81 /// of checking to see if we are forwarding...
82 bool isNull() const { return N == 0; }
84 // Allow explicit conversion to DSNode...
85 inline DSNode *getNode() const; // Defined inline in DSNode.h
86 unsigned getOffset() const { return Offset; }
88 inline void setNode(DSNode *N); // Defined inline in DSNode.h
89 void setOffset(unsigned O) {
90 //assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
91 // !N->ForwardNH.isNull()) && "Node handle offset out of range!");
92 //assert((!N || O < N->Size || (N->Size == 0 && O == 0) ||
93 // !N->ForwardNH.isNull()) && "Node handle offset out of range!");
97 void addEdgeTo(unsigned LinkNo, const DSNodeHandle &N);
98 void addEdgeTo(const DSNodeHandle &N) { addEdgeTo(0, N); }
100 /// mergeWith - Merge the logical node pointed to by 'this' with the node
101 /// pointed to by 'N'.
103 void mergeWith(const DSNodeHandle &N);
105 // hasLink - Return true if there is a link at the specified offset...
106 inline bool hasLink(unsigned Num) const;
108 /// getLink - Treat this current node pointer as a pointer to a structure of
109 /// some sort. This method will return the pointer a mem[this+Num]
111 inline const DSNodeHandle &getLink(unsigned Num) const;
112 inline DSNodeHandle &getLink(unsigned Num);
114 inline void setLink(unsigned Num, const DSNodeHandle &NH);
116 DSNode *HandleForwarding() const;
120 inline void swap(DSNodeHandle &NH1, DSNodeHandle &NH2) { NH1.swap(NH2); }
123 //===----------------------------------------------------------------------===//
124 /// DSCallSite - Representation of a call site via its call instruction,
125 /// the DSNode handle for the callee function (or function pointer), and
126 /// the DSNode handles for the function arguments.
129 CallInst *Inst; // Actual call site
130 Function *CalleeF; // The function called (direct call)
131 DSNodeHandle CalleeN; // The function node called (indirect call)
132 DSNodeHandle RetVal; // Returned value
133 std::vector<DSNodeHandle> CallArgs;// The pointer arguments
135 static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
136 const hash_map<const DSNode*, DSNode*> &NodeMap) {
137 if (DSNode *N = Src.getNode()) {
138 hash_map<const DSNode*, DSNode*>::const_iterator I = NodeMap.find(N);
139 assert(I != NodeMap.end() && "Not not in mapping!");
141 NH.setOffset(Src.getOffset());
142 NH.setNode(I->second);
146 static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
147 const hash_map<const DSNode*, DSNodeHandle> &NodeMap) {
148 if (DSNode *N = Src.getNode()) {
149 hash_map<const DSNode*, DSNodeHandle>::const_iterator I = NodeMap.find(N);
150 assert(I != NodeMap.end() && "Not not in mapping!");
152 NH.setOffset(Src.getOffset()+I->second.getOffset());
153 NH.setNode(I->second.getNode());
157 DSCallSite(); // DO NOT IMPLEMENT
159 /// Constructor. Note - This ctor destroys the argument vector passed in. On
160 /// exit, the argument vector is empty.
162 DSCallSite(CallInst &inst, const DSNodeHandle &rv, DSNode *Callee,
163 std::vector<DSNodeHandle> &Args)
164 : Inst(&inst), CalleeF(0), CalleeN(Callee), RetVal(rv) {
165 assert(Callee && "Null callee node specified for call site!");
168 DSCallSite(CallInst &inst, const DSNodeHandle &rv, Function *Callee,
169 std::vector<DSNodeHandle> &Args)
170 : Inst(&inst), CalleeF(Callee), RetVal(rv) {
171 assert(Callee && "Null callee function specified for call site!");
175 DSCallSite(const DSCallSite &DSCS) // Simple copy ctor
176 : Inst(DSCS.Inst), CalleeF(DSCS.CalleeF), CalleeN(DSCS.CalleeN),
177 RetVal(DSCS.RetVal), CallArgs(DSCS.CallArgs) {}
179 /// Mapping copy constructor - This constructor takes a preexisting call site
180 /// to copy plus a map that specifies how the links should be transformed.
181 /// This is useful when moving a call site from one graph to another.
183 template<typename MapTy>
184 DSCallSite(const DSCallSite &FromCall, const MapTy &NodeMap) {
185 Inst = FromCall.Inst;
186 InitNH(RetVal, FromCall.RetVal, NodeMap);
187 InitNH(CalleeN, FromCall.CalleeN, NodeMap);
188 CalleeF = FromCall.CalleeF;
190 CallArgs.resize(FromCall.CallArgs.size());
191 for (unsigned i = 0, e = FromCall.CallArgs.size(); i != e; ++i)
192 InitNH(CallArgs[i], FromCall.CallArgs[i], NodeMap);
195 /// isDirectCall - Return true if this call site is a direct call of the
196 /// function specified by getCalleeFunc. If not, it is an indirect call to
197 /// the node specified by getCalleeNode.
199 bool isDirectCall() const { return CalleeF != 0; }
200 bool isIndirectCall() const { return !isDirectCall(); }
203 // Accessor functions...
204 Function &getCaller() const;
205 CallInst &getCallInst() const { return *Inst; }
206 DSNodeHandle &getRetVal() { return RetVal; }
207 const DSNodeHandle &getRetVal() const { return RetVal; }
209 DSNode *getCalleeNode() const {
210 assert(!CalleeF && CalleeN.getNode()); return CalleeN.getNode();
212 Function *getCalleeFunc() const {
213 assert(!CalleeN.getNode() && CalleeF); return CalleeF;
216 unsigned getNumPtrArgs() const { return CallArgs.size(); }
218 DSNodeHandle &getPtrArg(unsigned i) {
219 assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
222 const DSNodeHandle &getPtrArg(unsigned i) const {
223 assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
227 void swap(DSCallSite &CS) {
229 std::swap(Inst, CS.Inst);
230 std::swap(RetVal, CS.RetVal);
231 std::swap(CalleeN, CS.CalleeN);
232 std::swap(CalleeF, CS.CalleeF);
233 std::swap(CallArgs, CS.CallArgs);
237 // MergeWith - Merge the return value and parameters of the these two call
239 void mergeWith(DSCallSite &CS) {
240 getRetVal().mergeWith(CS.getRetVal());
241 unsigned MinArgs = getNumPtrArgs();
242 if (CS.getNumPtrArgs() < MinArgs) MinArgs = CS.getNumPtrArgs();
244 for (unsigned a = 0; a != MinArgs; ++a)
245 getPtrArg(a).mergeWith(CS.getPtrArg(a));
248 /// markReachableNodes - This method recursively traverses the specified
249 /// DSNodes, marking any nodes which are reachable. All reachable nodes it
250 /// adds to the set, which allows it to only traverse visited nodes once.
252 void markReachableNodes(hash_set<DSNode*> &Nodes);
254 bool operator<(const DSCallSite &CS) const {
255 if (isDirectCall()) { // This must sort by callee first!
256 if (CS.isIndirectCall()) return true;
257 if (CalleeF < CS.CalleeF) return true;
258 if (CalleeF > CS.CalleeF) return false;
260 if (CS.isDirectCall()) return false;
261 if (CalleeN < CS.CalleeN) return true;
262 if (CalleeN > CS.CalleeN) return false;
264 if (RetVal < CS.RetVal) return true;
265 if (RetVal > CS.RetVal) return false;
266 return CallArgs < CS.CallArgs;
269 bool operator==(const DSCallSite &CS) const {
270 return RetVal == CS.RetVal && CalleeN == CS.CalleeN &&
271 CalleeF == CS.CalleeF && CallArgs == CS.CallArgs;
276 inline void swap(DSCallSite &CS1, DSCallSite &CS2) { CS1.swap(CS2); }