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"
27 class DSNode; // Each node in the graph
28 class DSGraph; // A graph for a function
30 namespace DS { // FIXME: After the paper, this should get cleaned up
31 enum { PointerShift = 3, // 64bit ptrs = 3, 32 bit ptrs = 2
32 PointerSize = 1 << PointerShift
35 // isPointerType - Return true if this first class type is big enough to hold
38 bool isPointerType(const Type *Ty);
41 //===----------------------------------------------------------------------===//
42 /// DSNodeHandle - Implement a "handle" to a data structure node that takes care
43 /// of all of the add/un'refing of the node to prevent the backpointers in the
44 /// graph from getting out of date. This class represents a "pointer" in the
45 /// graph, whose destination is an indexed offset into a node.
47 /// Note: some functions that are marked as inline in DSNodeHandle are actually
48 /// defined in DSNode.h because they need knowledge of DSNode operation. Putting
49 /// them in a CPP file wouldn't help making them inlined and keeping DSNode and
50 /// DSNodeHandle (and friends) in one file complicates things.
54 mutable unsigned Offset;
55 void operator==(const DSNode *N); // DISALLOW, use to promote N to nodehandle
57 // Allow construction, destruction, and assignment...
58 DSNodeHandle(DSNode *n = 0, unsigned offs = 0) : N(0), Offset(offs) {
61 DSNodeHandle(const DSNodeHandle &H) : N(0), Offset(0) {
63 Offset = H.Offset; // Must read offset AFTER the getNode()
65 ~DSNodeHandle() { setNode((DSNode*)0); }
66 DSNodeHandle &operator=(const DSNodeHandle &H) {
67 if (&H == this) return *this; // Don't set offset to 0 if self assigning.
68 Offset = 0; setNode(H.getNode()); Offset = H.Offset;
72 bool operator<(const DSNodeHandle &H) const { // Allow sorting
73 return getNode() < H.getNode() || (N == H.N && Offset < H.Offset);
75 bool operator>(const DSNodeHandle &H) const { return H < *this; }
76 bool operator==(const DSNodeHandle &H) const { // Allow comparison
77 return getNode() == H.getNode() && Offset == H.Offset;
79 bool operator!=(const DSNodeHandle &H) const { return !operator==(H); }
81 inline void swap(DSNodeHandle &NH) {
82 std::swap(Offset, NH.Offset);
86 /// isNull - Check to see if getNode() == 0, without going through the trouble
87 /// of checking to see if we are forwarding...
88 bool isNull() const { return N == 0; }
90 // Allow explicit conversion to DSNode...
91 inline DSNode *getNode() const; // Defined inline in DSNode.h
92 unsigned getOffset() const { return Offset; }
94 inline void setNode(DSNode *N); // Defined inline in DSNode.h
95 void setOffset(unsigned O) {
96 //assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
97 // !N->ForwardNH.isNull()) && "Node handle offset out of range!");
98 //assert((!N || O < N->Size || (N->Size == 0 && O == 0) ||
99 // !N->ForwardNH.isNull()) && "Node handle offset out of range!");
103 void addEdgeTo(unsigned LinkNo, const DSNodeHandle &N);
104 void addEdgeTo(const DSNodeHandle &N) { addEdgeTo(0, N); }
106 /// mergeWith - Merge the logical node pointed to by 'this' with the node
107 /// pointed to by 'N'.
109 void mergeWith(const DSNodeHandle &N);
111 // hasLink - Return true if there is a link at the specified offset...
112 inline bool hasLink(unsigned Num) const;
114 /// getLink - Treat this current node pointer as a pointer to a structure of
115 /// some sort. This method will return the pointer a mem[this+Num]
117 inline const DSNodeHandle &getLink(unsigned Num) const;
118 inline DSNodeHandle &getLink(unsigned Num);
120 inline void setLink(unsigned Num, const DSNodeHandle &NH);
122 DSNode *HandleForwarding() const;
126 inline void swap(DSNodeHandle &NH1, DSNodeHandle &NH2) { NH1.swap(NH2); }
129 //===----------------------------------------------------------------------===//
130 /// DSCallSite - Representation of a call site via its call instruction,
131 /// the DSNode handle for the callee function (or function pointer), and
132 /// the DSNode handles for the function arguments.
135 CallSite Site; // Actual call site
136 Function *CalleeF; // The function called (direct call)
137 DSNodeHandle CalleeN; // The function node called (indirect call)
138 DSNodeHandle RetVal; // Returned value
139 std::vector<DSNodeHandle> CallArgs;// The pointer arguments
141 static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
142 const hash_map<const DSNode*, DSNode*> &NodeMap) {
143 if (DSNode *N = Src.getNode()) {
144 hash_map<const DSNode*, DSNode*>::const_iterator I = NodeMap.find(N);
145 assert(I != NodeMap.end() && "Not not in mapping!");
147 NH.setOffset(Src.getOffset());
148 NH.setNode(I->second);
152 static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
153 const hash_map<const DSNode*, DSNodeHandle> &NodeMap) {
154 if (DSNode *N = Src.getNode()) {
155 hash_map<const DSNode*, DSNodeHandle>::const_iterator I = NodeMap.find(N);
156 assert(I != NodeMap.end() && "Not not in mapping!");
158 NH.setOffset(Src.getOffset()+I->second.getOffset());
159 NH.setNode(I->second.getNode());
163 DSCallSite(); // DO NOT IMPLEMENT
165 /// Constructor. Note - This ctor destroys the argument vector passed in. On
166 /// exit, the argument vector is empty.
168 DSCallSite(CallSite CS, const DSNodeHandle &rv, DSNode *Callee,
169 std::vector<DSNodeHandle> &Args)
170 : Site(CS), CalleeF(0), CalleeN(Callee), RetVal(rv) {
171 assert(Callee && "Null callee node specified for call site!");
174 DSCallSite(CallSite CS, const DSNodeHandle &rv, Function *Callee,
175 std::vector<DSNodeHandle> &Args)
176 : Site(CS), CalleeF(Callee), RetVal(rv) {
177 assert(Callee && "Null callee function specified for call site!");
181 DSCallSite(const DSCallSite &DSCS) // Simple copy ctor
182 : Site(DSCS.Site), CalleeF(DSCS.CalleeF), CalleeN(DSCS.CalleeN),
183 RetVal(DSCS.RetVal), CallArgs(DSCS.CallArgs) {}
185 /// Mapping copy constructor - This constructor takes a preexisting call site
186 /// to copy plus a map that specifies how the links should be transformed.
187 /// This is useful when moving a call site from one graph to another.
189 template<typename MapTy>
190 DSCallSite(const DSCallSite &FromCall, const MapTy &NodeMap) {
191 Site = FromCall.Site;
192 InitNH(RetVal, FromCall.RetVal, NodeMap);
193 InitNH(CalleeN, FromCall.CalleeN, NodeMap);
194 CalleeF = FromCall.CalleeF;
196 CallArgs.resize(FromCall.CallArgs.size());
197 for (unsigned i = 0, e = FromCall.CallArgs.size(); i != e; ++i)
198 InitNH(CallArgs[i], FromCall.CallArgs[i], NodeMap);
201 const DSCallSite &operator=(const DSCallSite &RHS) {
203 CalleeF = RHS.CalleeF;
204 CalleeN = RHS.CalleeN;
206 CallArgs = RHS.CallArgs;
210 /// isDirectCall - Return true if this call site is a direct call of the
211 /// function specified by getCalleeFunc. If not, it is an indirect call to
212 /// the node specified by getCalleeNode.
214 bool isDirectCall() const { return CalleeF != 0; }
215 bool isIndirectCall() const { return !isDirectCall(); }
218 // Accessor functions...
219 Function &getCaller() const;
220 CallSite getCallSite() const { return Site; }
221 DSNodeHandle &getRetVal() { return RetVal; }
222 const DSNodeHandle &getRetVal() const { return RetVal; }
224 DSNode *getCalleeNode() const {
225 assert(!CalleeF && CalleeN.getNode()); return CalleeN.getNode();
227 Function *getCalleeFunc() const {
228 assert(!CalleeN.getNode() && CalleeF); return CalleeF;
231 unsigned getNumPtrArgs() const { return CallArgs.size(); }
233 DSNodeHandle &getPtrArg(unsigned i) {
234 assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
237 const DSNodeHandle &getPtrArg(unsigned i) const {
238 assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
242 void swap(DSCallSite &CS) {
244 std::swap(Site, CS.Site);
245 std::swap(RetVal, CS.RetVal);
246 std::swap(CalleeN, CS.CalleeN);
247 std::swap(CalleeF, CS.CalleeF);
248 std::swap(CallArgs, CS.CallArgs);
252 // MergeWith - Merge the return value and parameters of the these two call
254 void mergeWith(DSCallSite &CS) {
255 getRetVal().mergeWith(CS.getRetVal());
256 unsigned MinArgs = getNumPtrArgs();
257 if (CS.getNumPtrArgs() < MinArgs) MinArgs = CS.getNumPtrArgs();
259 for (unsigned a = 0; a != MinArgs; ++a)
260 getPtrArg(a).mergeWith(CS.getPtrArg(a));
263 /// markReachableNodes - This method recursively traverses the specified
264 /// DSNodes, marking any nodes which are reachable. All reachable nodes it
265 /// adds to the set, which allows it to only traverse visited nodes once.
267 void markReachableNodes(hash_set<DSNode*> &Nodes);
269 bool operator<(const DSCallSite &CS) const {
270 if (isDirectCall()) { // This must sort by callee first!
271 if (CS.isIndirectCall()) return true;
272 if (CalleeF < CS.CalleeF) return true;
273 if (CalleeF > CS.CalleeF) return false;
275 if (CS.isDirectCall()) return false;
276 if (CalleeN < CS.CalleeN) return true;
277 if (CalleeN > CS.CalleeN) return false;
279 if (RetVal < CS.RetVal) return true;
280 if (RetVal > CS.RetVal) return false;
281 return CallArgs < CS.CallArgs;
284 bool operator==(const DSCallSite &CS) const {
285 return RetVal == CS.RetVal && CalleeN == CS.CalleeN &&
286 CalleeF == CS.CalleeF && CallArgs == CS.CallArgs;
291 inline void swap(DSCallSite &CS1, DSCallSite &CS2) { CS1.swap(CS2); }