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
23 class DSNodeIterator; // Data structure graph traversal iterator
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
31 //===----------------------------------------------------------------------===//
32 /// DSNodeHandle - Implement a "handle" to a data structure node that takes care
33 /// of all of the add/un'refing of the node to prevent the backpointers in the
34 /// graph from getting out of date. This class represents a "pointer" in the
35 /// graph, whose destination is an indexed offset into a node.
37 /// Note: some functions that are marked as inline in DSNodeHandle are actually
38 /// defined in DSNode.h because they need knowledge of DSNode operation. Putting
39 /// them in a CPP file wouldn't help making them inlined and keeping DSNode and
40 /// DSNodeHandle (and friends) in one file complicates things.
46 // Allow construction, destruction, and assignment...
47 DSNodeHandle(DSNode *n = 0, unsigned offs = 0) : N(0), Offset(offs) {
50 DSNodeHandle(const DSNodeHandle &H) : N(0), Offset(H.Offset) { setNode(H.N); }
51 ~DSNodeHandle() { setNode((DSNode*)0); }
52 DSNodeHandle &operator=(const DSNodeHandle &H) {
53 setNode(H.N); Offset = H.Offset;
57 bool operator<(const DSNodeHandle &H) const { // Allow sorting
58 return N < H.N || (N == H.N && Offset < H.Offset);
60 bool operator>(const DSNodeHandle &H) const { return H < *this; }
61 bool operator==(const DSNodeHandle &H) const { // Allow comparison
62 return N == H.N && Offset == H.Offset;
64 bool operator!=(const DSNodeHandle &H) const { return !operator==(H); }
66 // Allow explicit conversion to DSNode...
67 DSNode *getNode() const { return N; }
68 unsigned getOffset() const { return Offset; }
70 inline void setNode(DSNode *N); // Defined inline later...
71 void setOffset(unsigned O) { Offset = O; }
73 void addEdgeTo(unsigned LinkNo, const DSNodeHandle &N);
74 void addEdgeTo(const DSNodeHandle &N) { addEdgeTo(0, N); }
76 /// mergeWith - Merge the logical node pointed to by 'this' with the node
77 /// pointed to by 'N'.
79 void mergeWith(const DSNodeHandle &N);
81 // hasLink - Return true if there is a link at the specified offset...
82 inline bool hasLink(unsigned Num) const;
84 /// getLink - Treat this current node pointer as a pointer to a structure of
85 /// some sort. This method will return the pointer a mem[this+Num]
87 inline const DSNodeHandle &getLink(unsigned Num) const;
88 inline DSNodeHandle &getLink(unsigned Num);
90 inline void setLink(unsigned Num, const DSNodeHandle &NH);
94 //===----------------------------------------------------------------------===//
95 /// DSTypeRec - This structure is used to represent a single type that is held
99 const Type *Ty; // The type itself...
100 bool isArray; // Have we accessed an array of elements?
102 DSTypeRec(const Type *T = 0, bool A = false)
103 : Ty(T), isArray(A) {}
108 //===----------------------------------------------------------------------===//
109 /// DSCallSite - Representation of a call site via its call instruction,
110 /// the DSNode handle for the callee function (or function pointer), and
111 /// the DSNode handles for the function arguments.
113 /// One unusual aspect of this callsite record is the ResolvingCaller member.
114 /// If this is non-null, then it indicates the function that allowed a call-site
115 /// to finally be resolved. Because of indirect calls, this function may not
116 /// actually be the function that contains the Call instruction itself. This is
117 /// used by the BU and TD passes to communicate.
120 CallInst *Inst; // Actual call site
121 DSNodeHandle RetVal; // Returned value
122 DSNodeHandle Callee; // The function node called
123 std::vector<DSNodeHandle> CallArgs; // The pointer arguments
124 Function *ResolvingCaller; // See comments above
126 static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
127 const std::map<const DSNode*, DSNode*> &NodeMap) {
128 if (DSNode *N = Src.getNode()) {
129 std::map<const DSNode*, DSNode*>::const_iterator I = NodeMap.find(N);
130 assert(I != NodeMap.end() && "Not not in mapping!");
132 NH.setOffset(Src.getOffset());
133 NH.setNode(I->second);
137 static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
138 const std::map<const DSNode*, DSNodeHandle> &NodeMap) {
139 if (DSNode *N = Src.getNode()) {
140 std::map<const DSNode*, DSNodeHandle>::const_iterator I = NodeMap.find(N);
141 assert(I != NodeMap.end() && "Not not in mapping!");
143 NH.setOffset(Src.getOffset()+I->second.getOffset());
144 NH.setNode(I->second.getNode());
148 DSCallSite(); // DO NOT IMPLEMENT
150 /// Constructor. Note - This ctor destroys the argument vector passed in. On
151 /// exit, the argument vector is empty.
153 DSCallSite(CallInst &inst, const DSNodeHandle &rv, const DSNodeHandle &callee,
154 std::vector<DSNodeHandle> &Args)
155 : Inst(&inst), RetVal(rv), Callee(callee), ResolvingCaller(0) {
159 DSCallSite(const DSCallSite &DSCS) // Simple copy ctor
160 : Inst(DSCS.Inst), RetVal(DSCS.RetVal),
161 Callee(DSCS.Callee), CallArgs(DSCS.CallArgs),
162 ResolvingCaller(DSCS.ResolvingCaller) {}
164 /// Mapping copy constructor - This constructor takes a preexisting call site
165 /// to copy plus a map that specifies how the links should be transformed.
166 /// This is useful when moving a call site from one graph to another.
168 template<typename MapTy>
169 DSCallSite(const DSCallSite &FromCall, const MapTy &NodeMap) {
170 Inst = FromCall.Inst;
171 InitNH(RetVal, FromCall.RetVal, NodeMap);
172 InitNH(Callee, FromCall.Callee, NodeMap);
174 CallArgs.resize(FromCall.CallArgs.size());
175 for (unsigned i = 0, e = FromCall.CallArgs.size(); i != e; ++i)
176 InitNH(CallArgs[i], FromCall.CallArgs[i], NodeMap);
177 ResolvingCaller = FromCall.ResolvingCaller;
180 // Accessor functions...
181 Function &getCaller() const;
182 CallInst &getCallInst() const { return *Inst; }
183 DSNodeHandle &getRetVal() { return RetVal; }
184 DSNodeHandle &getCallee() { return Callee; }
185 const DSNodeHandle &getRetVal() const { return RetVal; }
186 const DSNodeHandle &getCallee() const { return Callee; }
187 void setCallee(const DSNodeHandle &H) { Callee = H; }
189 unsigned getNumPtrArgs() const { return CallArgs.size(); }
191 Function *getResolvingCaller() const { return ResolvingCaller; }
192 void setResolvingCaller(Function *F) { ResolvingCaller = F; }
194 DSNodeHandle &getPtrArg(unsigned i) {
195 assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
198 const DSNodeHandle &getPtrArg(unsigned i) const {
199 assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
203 bool operator<(const DSCallSite &CS) const {
204 if (RetVal < CS.RetVal) return true;
205 if (RetVal > CS.RetVal) return false;
206 if (Callee < CS.Callee) return true;
207 if (Callee > CS.Callee) return false;
208 return CallArgs < CS.CallArgs;
211 bool operator==(const DSCallSite &CS) const {
212 return RetVal == CS.RetVal && Callee == CS.Callee &&
213 CallArgs == CS.CallArgs;