1 //===- DSSupport.h - Support for datastructure graphs -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Support for graph nodes, call sites, and types.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_DSSUPPORT_H
15 #define LLVM_ANALYSIS_DSSUPPORT_H
18 #include "Support/hash_set"
19 #include "llvm/Support/CallSite.h"
29 class DSNode; // Each node in the graph
30 class DSGraph; // A graph for a function
32 namespace DS { // FIXME: After the paper, this should get cleaned up
33 enum { PointerShift = 2, // 64bit ptrs = 3, 32 bit ptrs = 2
34 PointerSize = 1 << PointerShift
37 // isPointerType - Return true if this first class type is big enough to hold
40 bool isPointerType(const Type *Ty);
43 //===----------------------------------------------------------------------===//
44 /// DSNodeHandle - Implement a "handle" to a data structure node that takes care
45 /// of all of the add/un'refing of the node to prevent the backpointers in the
46 /// graph from getting out of date. This class represents a "pointer" in the
47 /// graph, whose destination is an indexed offset into a node.
49 /// Note: some functions that are marked as inline in DSNodeHandle are actually
50 /// defined in DSNode.h because they need knowledge of DSNode operation. Putting
51 /// them in a CPP file wouldn't help making them inlined and keeping DSNode and
52 /// DSNodeHandle (and friends) in one file complicates things.
56 mutable unsigned Offset;
57 void operator==(const DSNode *N); // DISALLOW, use to promote N to nodehandle
59 // Allow construction, destruction, and assignment...
60 DSNodeHandle(DSNode *n = 0, unsigned offs = 0) : N(0), Offset(offs) {
63 DSNodeHandle(const DSNodeHandle &H) : N(0), Offset(0) {
65 Offset = H.Offset; // Must read offset AFTER the getNode()
67 ~DSNodeHandle() { setNode((DSNode*)0); }
68 DSNodeHandle &operator=(const DSNodeHandle &H) {
69 if (&H == this) return *this; // Don't set offset to 0 if self assigning.
70 Offset = 0; setNode(H.getNode()); Offset = H.Offset;
74 bool operator<(const DSNodeHandle &H) const { // Allow sorting
75 return getNode() < H.getNode() || (N == H.N && Offset < H.Offset);
77 bool operator>(const DSNodeHandle &H) const { return H < *this; }
78 bool operator==(const DSNodeHandle &H) const { // Allow comparison
79 return getNode() == H.getNode() && Offset == H.Offset;
81 bool operator!=(const DSNodeHandle &H) const { return !operator==(H); }
83 inline void swap(DSNodeHandle &NH) {
84 std::swap(Offset, NH.Offset);
88 /// isNull - Check to see if getNode() == 0, without going through the trouble
89 /// of checking to see if we are forwarding...
90 bool isNull() const { return N == 0; }
92 // Allow explicit conversion to DSNode...
93 inline DSNode *getNode() const; // Defined inline in DSNode.h
94 unsigned getOffset() const { return Offset; }
96 inline void setNode(DSNode *N); // Defined inline in DSNode.h
97 void setOffset(unsigned O) {
98 //assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
99 // !N->ForwardNH.isNull()) && "Node handle offset out of range!");
100 //assert((!N || O < N->Size || (N->Size == 0 && O == 0) ||
101 // !N->ForwardNH.isNull()) && "Node handle offset out of range!");
105 void addEdgeTo(unsigned LinkNo, const DSNodeHandle &N);
106 void addEdgeTo(const DSNodeHandle &N) { addEdgeTo(0, N); }
108 /// mergeWith - Merge the logical node pointed to by 'this' with the node
109 /// pointed to by 'N'.
111 void mergeWith(const DSNodeHandle &N);
113 // hasLink - Return true if there is a link at the specified offset...
114 inline bool hasLink(unsigned Num) const;
116 /// getLink - Treat this current node pointer as a pointer to a structure of
117 /// some sort. This method will return the pointer a mem[this+Num]
119 inline const DSNodeHandle &getLink(unsigned Num) const;
120 inline DSNodeHandle &getLink(unsigned Num);
122 inline void setLink(unsigned Num, const DSNodeHandle &NH);
124 DSNode *HandleForwarding() const;
127 } // End llvm namespace
131 inline void swap<llvm::DSNodeHandle>(llvm::DSNodeHandle &NH1, llvm::DSNodeHandle &NH2) { NH1.swap(NH2); }
136 //===----------------------------------------------------------------------===//
137 /// DSCallSite - Representation of a call site via its call instruction,
138 /// the DSNode handle for the callee function (or function pointer), and
139 /// the DSNode handles for the function arguments.
142 CallSite Site; // Actual call site
143 Function *CalleeF; // The function called (direct call)
144 DSNodeHandle CalleeN; // The function node called (indirect call)
145 DSNodeHandle RetVal; // Returned value
146 std::vector<DSNodeHandle> CallArgs;// The pointer arguments
148 static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
149 const hash_map<const DSNode*, DSNode*> &NodeMap) {
150 if (DSNode *N = Src.getNode()) {
151 hash_map<const DSNode*, DSNode*>::const_iterator I = NodeMap.find(N);
152 assert(I != NodeMap.end() && "Not not in mapping!");
154 NH.setOffset(Src.getOffset());
155 NH.setNode(I->second);
159 static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
160 const hash_map<const DSNode*, DSNodeHandle> &NodeMap) {
161 if (DSNode *N = Src.getNode()) {
162 hash_map<const DSNode*, DSNodeHandle>::const_iterator I = NodeMap.find(N);
163 assert(I != NodeMap.end() && "Not not in mapping!");
165 NH.setOffset(Src.getOffset()+I->second.getOffset());
166 NH.setNode(I->second.getNode());
170 DSCallSite(); // DO NOT IMPLEMENT
172 /// Constructor. Note - This ctor destroys the argument vector passed in. On
173 /// exit, the argument vector is empty.
175 DSCallSite(CallSite CS, const DSNodeHandle &rv, DSNode *Callee,
176 std::vector<DSNodeHandle> &Args)
177 : Site(CS), CalleeF(0), CalleeN(Callee), RetVal(rv) {
178 assert(Callee && "Null callee node specified for call site!");
181 DSCallSite(CallSite CS, const DSNodeHandle &rv, Function *Callee,
182 std::vector<DSNodeHandle> &Args)
183 : Site(CS), CalleeF(Callee), RetVal(rv) {
184 assert(Callee && "Null callee function specified for call site!");
188 DSCallSite(const DSCallSite &DSCS) // Simple copy ctor
189 : Site(DSCS.Site), CalleeF(DSCS.CalleeF), CalleeN(DSCS.CalleeN),
190 RetVal(DSCS.RetVal), CallArgs(DSCS.CallArgs) {}
192 /// Mapping copy constructor - This constructor takes a preexisting call site
193 /// to copy plus a map that specifies how the links should be transformed.
194 /// This is useful when moving a call site from one graph to another.
196 template<typename MapTy>
197 DSCallSite(const DSCallSite &FromCall, const MapTy &NodeMap) {
198 Site = FromCall.Site;
199 InitNH(RetVal, FromCall.RetVal, NodeMap);
200 InitNH(CalleeN, FromCall.CalleeN, NodeMap);
201 CalleeF = FromCall.CalleeF;
203 CallArgs.resize(FromCall.CallArgs.size());
204 for (unsigned i = 0, e = FromCall.CallArgs.size(); i != e; ++i)
205 InitNH(CallArgs[i], FromCall.CallArgs[i], NodeMap);
208 const DSCallSite &operator=(const DSCallSite &RHS) {
210 CalleeF = RHS.CalleeF;
211 CalleeN = RHS.CalleeN;
213 CallArgs = RHS.CallArgs;
217 /// isDirectCall - Return true if this call site is a direct call of the
218 /// function specified by getCalleeFunc. If not, it is an indirect call to
219 /// the node specified by getCalleeNode.
221 bool isDirectCall() const { return CalleeF != 0; }
222 bool isIndirectCall() const { return !isDirectCall(); }
225 // Accessor functions...
226 Function &getCaller() const;
227 CallSite getCallSite() const { return Site; }
228 DSNodeHandle &getRetVal() { return RetVal; }
229 const DSNodeHandle &getRetVal() const { return RetVal; }
231 DSNode *getCalleeNode() const {
232 assert(!CalleeF && CalleeN.getNode()); return CalleeN.getNode();
234 Function *getCalleeFunc() const {
235 assert(!CalleeN.getNode() && CalleeF); return CalleeF;
238 unsigned getNumPtrArgs() const { return CallArgs.size(); }
240 DSNodeHandle &getPtrArg(unsigned i) {
241 assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
244 const DSNodeHandle &getPtrArg(unsigned i) const {
245 assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
249 void swap(DSCallSite &CS) {
251 std::swap(Site, CS.Site);
252 std::swap(RetVal, CS.RetVal);
253 std::swap(CalleeN, CS.CalleeN);
254 std::swap(CalleeF, CS.CalleeF);
255 std::swap(CallArgs, CS.CallArgs);
259 // MergeWith - Merge the return value and parameters of the these two call
261 void mergeWith(DSCallSite &CS) {
262 getRetVal().mergeWith(CS.getRetVal());
263 unsigned MinArgs = getNumPtrArgs();
264 if (CS.getNumPtrArgs() < MinArgs) MinArgs = CS.getNumPtrArgs();
266 for (unsigned a = 0; a != MinArgs; ++a)
267 getPtrArg(a).mergeWith(CS.getPtrArg(a));
270 /// markReachableNodes - This method recursively traverses the specified
271 /// DSNodes, marking any nodes which are reachable. All reachable nodes it
272 /// adds to the set, which allows it to only traverse visited nodes once.
274 void markReachableNodes(hash_set<DSNode*> &Nodes);
276 bool operator<(const DSCallSite &CS) const {
277 if (isDirectCall()) { // This must sort by callee first!
278 if (CS.isIndirectCall()) return true;
279 if (CalleeF < CS.CalleeF) return true;
280 if (CalleeF > CS.CalleeF) return false;
282 if (CS.isDirectCall()) return false;
283 if (CalleeN < CS.CalleeN) return true;
284 if (CalleeN > CS.CalleeN) return false;
286 if (RetVal < CS.RetVal) return true;
287 if (RetVal > CS.RetVal) return false;
288 return CallArgs < CS.CallArgs;
291 bool operator==(const DSCallSite &CS) const {
292 return RetVal == CS.RetVal && CalleeN == CS.CalleeN &&
293 CalleeF == CS.CalleeF && CallArgs == CS.CallArgs;
297 } // End llvm namespace
301 inline void swap<llvm::DSCallSite>(llvm::DSCallSite &CS1,
302 llvm::DSCallSite &CS2) { CS1.swap(CS2); }