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
24 namespace DS { // FIXME: After the paper, this should get cleaned up
25 enum { PointerShift = 3, // 64bit ptrs = 3, 32 bit ptrs = 2
26 PointerSize = 1 << PointerShift
29 // isPointerType - Return true if this first class type is big enough to hold
32 bool isPointerType(const Type *Ty);
35 //===----------------------------------------------------------------------===//
36 /// DSNodeHandle - Implement a "handle" to a data structure node that takes care
37 /// of all of the add/un'refing of the node to prevent the backpointers in the
38 /// graph from getting out of date. This class represents a "pointer" in the
39 /// graph, whose destination is an indexed offset into a node.
41 /// Note: some functions that are marked as inline in DSNodeHandle are actually
42 /// defined in DSNode.h because they need knowledge of DSNode operation. Putting
43 /// them in a CPP file wouldn't help making them inlined and keeping DSNode and
44 /// DSNodeHandle (and friends) in one file complicates things.
50 // Allow construction, destruction, and assignment...
51 DSNodeHandle(DSNode *n = 0, unsigned offs = 0) : N(0), Offset(offs) {
54 DSNodeHandle(const DSNodeHandle &H) : N(0), Offset(H.Offset) { setNode(H.N); }
55 ~DSNodeHandle() { setNode((DSNode*)0); }
56 DSNodeHandle &operator=(const DSNodeHandle &H) {
57 setNode(H.N); Offset = H.Offset;
61 bool operator<(const DSNodeHandle &H) const { // Allow sorting
62 return N < H.N || (N == H.N && Offset < H.Offset);
64 bool operator>(const DSNodeHandle &H) const { return H < *this; }
65 bool operator==(const DSNodeHandle &H) const { // Allow comparison
66 return N == H.N && Offset == H.Offset;
68 bool operator!=(const DSNodeHandle &H) const { return !operator==(H); }
70 inline void swap(DSNodeHandle &H);
72 // Allow explicit conversion to DSNode...
73 DSNode *getNode() const { return N; }
74 unsigned getOffset() const { return Offset; }
76 inline void setNode(DSNode *N); // Defined inline later...
77 void setOffset(unsigned O) { Offset = O; }
79 void addEdgeTo(unsigned LinkNo, const DSNodeHandle &N);
80 void addEdgeTo(const DSNodeHandle &N) { addEdgeTo(0, N); }
82 /// mergeWith - Merge the logical node pointed to by 'this' with the node
83 /// pointed to by 'N'.
85 void mergeWith(const DSNodeHandle &N);
87 // hasLink - Return true if there is a link at the specified offset...
88 inline bool hasLink(unsigned Num) const;
90 /// getLink - Treat this current node pointer as a pointer to a structure of
91 /// some sort. This method will return the pointer a mem[this+Num]
93 inline const DSNodeHandle &getLink(unsigned Num) const;
94 inline DSNodeHandle &getLink(unsigned Num);
96 inline void setLink(unsigned Num, const DSNodeHandle &NH);
99 inline void swap(DSNodeHandle &NH1, DSNodeHandle &NH2) { NH1.swap(NH2); }
101 //===----------------------------------------------------------------------===//
102 /// DSCallSite - Representation of a call site via its call instruction,
103 /// the DSNode handle for the callee function (or function pointer), and
104 /// the DSNode handles for the function arguments.
106 /// One unusual aspect of this callsite record is the ResolvingCaller member.
107 /// If this is non-null, then it indicates the function that allowed a call-site
108 /// to finally be resolved. Because of indirect calls, this function may not
109 /// actually be the function that contains the Call instruction itself. This is
110 /// used by the BU and TD passes to communicate.
113 CallInst *Inst; // Actual call site
114 DSNodeHandle RetVal; // Returned value
115 DSNodeHandle Callee; // The function node called
116 std::vector<DSNodeHandle> CallArgs; // The pointer arguments
117 Function *ResolvingCaller; // See comments above
119 static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
120 const std::map<const DSNode*, DSNode*> &NodeMap) {
121 if (DSNode *N = Src.getNode()) {
122 std::map<const DSNode*, DSNode*>::const_iterator I = NodeMap.find(N);
123 assert(I != NodeMap.end() && "Not not in mapping!");
125 NH.setOffset(Src.getOffset());
126 NH.setNode(I->second);
130 static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
131 const std::map<const DSNode*, DSNodeHandle> &NodeMap) {
132 if (DSNode *N = Src.getNode()) {
133 std::map<const DSNode*, DSNodeHandle>::const_iterator I = NodeMap.find(N);
134 assert(I != NodeMap.end() && "Not not in mapping!");
136 NH.setOffset(Src.getOffset()+I->second.getOffset());
137 NH.setNode(I->second.getNode());
141 DSCallSite(); // DO NOT IMPLEMENT
143 /// Constructor. Note - This ctor destroys the argument vector passed in. On
144 /// exit, the argument vector is empty.
146 DSCallSite(CallInst &inst, const DSNodeHandle &rv, const DSNodeHandle &callee,
147 std::vector<DSNodeHandle> &Args)
148 : Inst(&inst), RetVal(rv), Callee(callee), ResolvingCaller(0) {
152 DSCallSite(const DSCallSite &DSCS) // Simple copy ctor
153 : Inst(DSCS.Inst), RetVal(DSCS.RetVal),
154 Callee(DSCS.Callee), CallArgs(DSCS.CallArgs),
155 ResolvingCaller(DSCS.ResolvingCaller) {}
157 /// Mapping copy constructor - This constructor takes a preexisting call site
158 /// to copy plus a map that specifies how the links should be transformed.
159 /// This is useful when moving a call site from one graph to another.
161 template<typename MapTy>
162 DSCallSite(const DSCallSite &FromCall, const MapTy &NodeMap) {
163 Inst = FromCall.Inst;
164 InitNH(RetVal, FromCall.RetVal, NodeMap);
165 InitNH(Callee, FromCall.Callee, NodeMap);
167 CallArgs.resize(FromCall.CallArgs.size());
168 for (unsigned i = 0, e = FromCall.CallArgs.size(); i != e; ++i)
169 InitNH(CallArgs[i], FromCall.CallArgs[i], NodeMap);
170 ResolvingCaller = FromCall.ResolvingCaller;
173 // Accessor functions...
174 Function &getCaller() const;
175 CallInst &getCallInst() const { return *Inst; }
176 DSNodeHandle &getRetVal() { return RetVal; }
177 DSNodeHandle &getCallee() { return Callee; }
178 const DSNodeHandle &getRetVal() const { return RetVal; }
179 const DSNodeHandle &getCallee() const { return Callee; }
180 void setCallee(const DSNodeHandle &H) { Callee = H; }
182 unsigned getNumPtrArgs() const { return CallArgs.size(); }
184 Function *getResolvingCaller() const { return ResolvingCaller; }
185 void setResolvingCaller(Function *F) { ResolvingCaller = F; }
187 DSNodeHandle &getPtrArg(unsigned i) {
188 assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
191 const DSNodeHandle &getPtrArg(unsigned i) const {
192 assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
196 void swap(DSCallSite &CS) {
198 std::swap(Inst, CS.Inst);
199 std::swap(RetVal, CS.RetVal);
200 std::swap(Callee, CS.Callee);
201 std::swap(CallArgs, CS.CallArgs);
202 std::swap(ResolvingCaller, CS.ResolvingCaller);
206 // MergeWith - Merge the return value and parameters of the these two call
208 void mergeWith(DSCallSite &CS) {
209 getRetVal().mergeWith(CS.getRetVal());
210 unsigned MinArgs = getNumPtrArgs();
211 if (CS.getNumPtrArgs() < MinArgs) MinArgs = CS.getNumPtrArgs();
213 for (unsigned a = 0; a != MinArgs; ++a)
214 getPtrArg(a).mergeWith(CS.getPtrArg(a));
217 bool operator<(const DSCallSite &CS) const {
218 if (Callee < CS.Callee) return true; // This must sort by callee first!
219 if (Callee > CS.Callee) return false;
220 if (RetVal < CS.RetVal) return true;
221 if (RetVal > CS.RetVal) return false;
222 return CallArgs < CS.CallArgs;
225 bool operator==(const DSCallSite &CS) const {
226 return RetVal == CS.RetVal && Callee == CS.Callee &&
227 CallArgs == CS.CallArgs;
231 inline void swap(DSCallSite &CS1, DSCallSite &CS2) { CS1.swap(CS2); }