1 //===- DSGraph.h - Represent a collection of data structures ----*- C++ -*-===//
3 // This header defines the primative classes that make up a data structure
6 //===----------------------------------------------------------------------===//
8 #ifndef LLVM_ANALYSIS_DSGRAPH_H
9 #define LLVM_ANALYSIS_DSGRAPH_H
22 class DSNode; // Each node in the graph
23 class DSGraph; // A graph for a function
24 class DSNodeIterator; // Data structure graph traversal iterator
27 //===----------------------------------------------------------------------===//
28 /// DSNodeHandle - Implement a "handle" to a data structure node that takes care
29 /// of all of the add/un'refing of the node to prevent the backpointers in the
30 /// graph from getting out of date. This class represents a "pointer" in the
31 /// graph, whose destination is an indexed offset into a node.
37 // Allow construction, destruction, and assignment...
38 DSNodeHandle(DSNode *n = 0, unsigned offs = 0) : N(0), Offset(offs) {
41 DSNodeHandle(const DSNodeHandle &H) : N(0), Offset(H.Offset) { setNode(H.N); }
42 ~DSNodeHandle() { setNode((DSNode*)0); }
43 DSNodeHandle &operator=(const DSNodeHandle &H) {
44 setNode(H.N); Offset = H.Offset;
48 bool operator<(const DSNodeHandle &H) const { // Allow sorting
49 return N < H.N || (N == H.N && Offset < H.Offset);
51 bool operator>(const DSNodeHandle &H) const { return H < *this; }
52 bool operator==(const DSNodeHandle &H) const { // Allow comparison
53 return N == H.N && Offset == H.Offset;
55 bool operator!=(const DSNodeHandle &H) const { return !operator==(H); }
57 // Allow explicit conversion to DSNode...
58 DSNode *getNode() const { return N; }
59 unsigned getOffset() const { return Offset; }
61 inline void setNode(DSNode *N); // Defined inline later...
62 void setOffset(unsigned O) { Offset = O; }
64 void addEdgeTo(unsigned LinkNo, const DSNodeHandle &N);
65 void addEdgeTo(const DSNodeHandle &N) { addEdgeTo(0, N); }
67 /// mergeWith - Merge the logical node pointed to by 'this' with the node
68 /// pointed to by 'N'.
70 void mergeWith(const DSNodeHandle &N);
72 // hasLink - Return true if there is a link at the specified offset...
73 inline bool hasLink(unsigned Num) const;
75 /// getLink - Treat this current node pointer as a pointer to a structure of
76 /// some sort. This method will return the pointer a mem[this+Num]
78 inline const DSNodeHandle *getLink(unsigned Num) const;
79 inline DSNodeHandle *getLink(unsigned Num);
81 inline void setLink(unsigned Num, const DSNodeHandle &NH);
85 //===----------------------------------------------------------------------===//
86 /// DSTypeRec - This structure is used to represent a single type that is held
90 const Type *Ty; // The type itself...
91 unsigned Offset; // The offset in the node
92 bool isArray; // Have we accessed an array of elements?
94 DSTypeRec() : Ty(0), Offset(0), isArray(false) {}
95 DSTypeRec(const Type *T, unsigned O) : Ty(T), Offset(O), isArray(false) {}
97 bool operator<(const DSTypeRec &TR) const {
98 // Sort first by offset!
99 return Offset < TR.Offset || (Offset == TR.Offset && Ty < TR.Ty);
101 bool operator==(const DSTypeRec &TR) const {
102 return Ty == TR.Ty && Offset == TR.Offset;
104 bool operator!=(const DSTypeRec &TR) const { return !operator==(TR); }
109 //===----------------------------------------------------------------------===//
110 /// DSNode - Data structure node class
112 /// This class represents an untyped memory object of Size bytes. It keeps
113 /// track of any pointers that have been stored into the object as well as the
114 /// different types represented in this object.
117 /// Links - Contains one entry for every _distinct_ pointer field in the
118 /// memory block. These are demand allocated and indexed by the MergeMap
121 std::vector<DSNodeHandle> Links;
123 /// MergeMap - Maps from every byte in the object to a signed byte number.
124 /// This map is neccesary due to the merging that is possible as part of the
125 /// unification algorithm. To merge two distinct bytes of the object together
126 /// into a single logical byte, the indexes for the two bytes are set to the
127 /// same value. This fully general merging is capable of representing all
128 /// manners of array merging if neccesary.
130 /// This map is also used to map outgoing pointers to various byte offsets in
131 /// this data structure node. If this value is >= 0, then it indicates that
132 /// the numbered entry in the Links vector contains the outgoing edge for this
133 /// byte offset. In this way, the Links vector can be demand allocated and
134 /// byte elements of the node may be merged without needing a Link allocated
137 /// Initially, each each element of the MergeMap is assigned a unique negative
138 /// number, which are then merged as the unification occurs.
140 std::vector<signed char> MergeMap;
142 /// Referrers - Keep track of all of the node handles that point to this
143 /// DSNode. These pointers may need to be updated to point to a different
144 /// node if this node gets merged with it.
146 std::vector<DSNodeHandle*> Referrers;
148 /// TypeEntries - As part of the merging process of this algorithm, nodes of
149 /// different types can be represented by this single DSNode. This vector is
152 std::vector<DSTypeRec> TypeEntries;
154 /// Globals - The list of global values that are merged into this node.
156 std::vector<GlobalValue*> Globals;
158 void operator=(const DSNode &); // DO NOT IMPLEMENT
161 ShadowNode = 0, // Nothing is known about this node...
162 ScalarNode = 1 << 0, // Scalar of the current function contains this value
163 AllocaNode = 1 << 1, // This node was allocated with alloca
164 NewNode = 1 << 2, // This node was allocated with malloc
165 GlobalNode = 1 << 3, // This node was allocated by a global var decl
166 Incomplete = 1 << 4, // This node may not be complete
167 Modified = 1 << 5, // This node is modified in this context
168 Read = 1 << 6, // This node is read in this context
171 /// NodeType - A union of the above bits. "Shadow" nodes do not add any flags
172 /// to the nodes in the data structure graph, so it is possible to have nodes
173 /// with a value of 0 for their NodeType. Scalar and Alloca markers go away
174 /// when function graphs are inlined.
176 unsigned char NodeType;
178 DSNode(enum NodeTy NT, const Type *T);
179 DSNode(const DSNode &);
183 dropAllReferences(); // Only needed to satisfy assertion checks...
184 assert(Referrers.empty() && "Referrers to dead node exist!");
188 // Iterator for graph interface...
189 typedef DSNodeIterator iterator;
190 typedef DSNodeIterator const_iterator;
191 inline iterator begin() const; // Defined in DSGraphTraits.h
192 inline iterator end() const;
194 //===--------------------------------------------------
197 /// getSize - Return the maximum number of bytes occupied by this object...
199 unsigned getSize() const { return MergeMap.size(); }
201 // getTypeEntries - Return the possible types and their offsets in this object
202 const std::vector<DSTypeRec> &getTypeEntries() const { return TypeEntries; }
204 /// getReferrers - Return a list of the pointers to this node...
206 const std::vector<DSNodeHandle*> &getReferrers() const { return Referrers; }
208 /// isModified - Return true if this node may be modified in this context
210 bool isModified() const { return (NodeType & Modified) != 0; }
212 /// isRead - Return true if this node may be read in this context
214 bool isRead() const { return (NodeType & Read) != 0; }
217 /// hasLink - Return true if this memory object has a link at the specified
220 bool hasLink(unsigned i) const {
221 assert(i < getSize() && "Field Link index is out of range!");
222 return MergeMap[i] >= 0;
225 DSNodeHandle *getLink(unsigned i) {
227 return &Links[MergeMap[i]];
230 const DSNodeHandle *getLink(unsigned i) const {
232 return &Links[MergeMap[i]];
236 /// getMergeMapLabel - Return the merge map entry specified, to allow printing
237 /// out of DSNodes nicely for DOT graphs.
239 int getMergeMapLabel(unsigned i) const {
240 assert(i < MergeMap.size() && "MergeMap index out of range!");
244 /// getTypeRec - This method returns the specified type record if it exists.
245 /// If it does not yet exist, the method checks to see whether or not the
246 /// request would result in an untrackable state. If adding it would cause
247 /// untrackable state, we foldNodeCompletely the node and return the void
248 /// record, otherwise we add an new TypeEntry and return it.
250 DSTypeRec &getTypeRec(const Type *Ty, unsigned Offset);
252 /// foldNodeCompletely - If we determine that this node has some funny
253 /// behavior happening to it that we cannot represent, we fold it down to a
254 /// single, completely pessimistic, node. This node is represented as a
255 /// single byte with a single TypeEntry of "void".
257 void foldNodeCompletely();
259 /// isNodeCompletelyFolded - Return true if this node has been completely
260 /// folded down to something that can never be expanded, effectively losing
261 /// all of the field sensitivity that may be present in the node.
263 bool isNodeCompletelyFolded() const;
265 /// setLink - Set the link at the specified offset to the specified
266 /// NodeHandle, replacing what was there. It is uncommon to use this method,
267 /// instead one of the higher level methods should be used, below.
269 void setLink(unsigned i, const DSNodeHandle &NH);
271 /// addEdgeTo - Add an edge from the current node to the specified node. This
272 /// can cause merging of nodes in the graph.
274 void addEdgeTo(unsigned Offset, const DSNodeHandle &NH);
276 /// mergeWith - Merge this node and the specified node, moving all links to
277 /// and from the argument node into the current node, deleting the node
278 /// argument. Offset indicates what offset the specified node is to be merged
279 /// into the current node.
281 /// The specified node may be a null pointer (in which case, nothing happens).
283 void mergeWith(const DSNodeHandle &NH, unsigned Offset);
285 /// mergeIndexes - If we discover that two indexes are equivalent and must be
286 /// merged, this function is used to do the dirty work.
288 void mergeIndexes(unsigned idx1, unsigned idx2) {
289 assert(idx1 < getSize() && idx2 < getSize() && "Indexes out of range!");
290 signed char MV1 = MergeMap[idx1];
291 signed char MV2 = MergeMap[idx2];
293 mergeMappedValues(MV1, MV2);
297 /// addGlobal - Add an entry for a global value to the Globals list. This
298 /// also marks the node with the 'G' flag if it does not already have it.
300 void addGlobal(GlobalValue *GV);
301 const std::vector<GlobalValue*> &getGlobals() const { return Globals; }
302 std::vector<GlobalValue*> &getGlobals() { return Globals; }
304 void print(std::ostream &O, const DSGraph *G) const;
307 void dropAllReferences() {
311 /// remapLinks - Change all of the Links in the current node according to the
312 /// specified mapping.
313 void remapLinks(std::map<const DSNode*, DSNode*> &OldNodeMap);
316 friend class DSNodeHandle;
317 // addReferrer - Keep the referrer set up to date...
318 void addReferrer(DSNodeHandle *H) { Referrers.push_back(H); }
319 void removeReferrer(DSNodeHandle *H);
321 /// rewriteMergeMap - Loop over the mergemap, replacing any references to the
322 /// index From to be references to the index To.
324 void rewriteMergeMap(signed char From, signed char To) {
325 assert(From != To && "Cannot change something into itself!");
326 for (unsigned i = 0, e = MergeMap.size(); i != e; ++i)
327 if (MergeMap[i] == From)
331 /// mergeMappedValues - This is the higher level form of rewriteMergeMap. It
332 /// is fully capable of merging links together if neccesary as well as simply
333 /// rewriting the map entries.
335 void mergeMappedValues(signed char V1, signed char V2);
337 /// growNode - Attempt to grow the node to the specified size. This may do
338 /// one of three things:
339 /// 1. Grow the node, return false
340 /// 2. Refuse to grow the node, but maintain a trackable situation, return
342 /// 3. Be unable to track if node was that size, so collapse the node and
345 bool growNode(unsigned RequestedSize);
349 //===----------------------------------------------------------------------===//
350 // Define inline DSNodeHandle functions that depend on the definition of DSNode
353 inline void DSNodeHandle::setNode(DSNode *n) {
354 if (N) N->removeReferrer(this);
356 if (N) N->addReferrer(this);
359 inline bool DSNodeHandle::hasLink(unsigned Num) const {
360 assert(N && "DSNodeHandle does not point to a node yet!");
361 return N->hasLink(Num+Offset);
365 /// getLink - Treat this current node pointer as a pointer to a structure of
366 /// some sort. This method will return the pointer a mem[this+Num]
368 inline const DSNodeHandle *DSNodeHandle::getLink(unsigned Num) const {
369 assert(N && "DSNodeHandle does not point to a node yet!");
370 return N->getLink(Num+Offset);
372 inline DSNodeHandle *DSNodeHandle::getLink(unsigned Num) {
373 assert(N && "DSNodeHandle does not point to a node yet!");
374 return N->getLink(Num+Offset);
377 inline void DSNodeHandle::setLink(unsigned Num, const DSNodeHandle &NH) {
378 assert(N && "DSNodeHandle does not point to a node yet!");
379 N->setLink(Num+Offset, NH);
382 /// addEdgeTo - Add an edge from the current node to the specified node. This
383 /// can cause merging of nodes in the graph.
385 inline void DSNodeHandle::addEdgeTo(unsigned LinkNo, const DSNodeHandle &Node) {
386 assert(N && "DSNodeHandle does not point to a node yet!");
387 N->addEdgeTo(LinkNo+Offset, Node);
390 /// mergeWith - Merge the logical node pointed to by 'this' with the node
391 /// pointed to by 'N'.
393 inline void DSNodeHandle::mergeWith(const DSNodeHandle &Node) {
394 assert(N && "DSNodeHandle does not point to a node yet!");
395 N->mergeWith(Node, Offset);
399 //===----------------------------------------------------------------------===//
400 /// DSCallSite - Representation of a call site via its call instruction,
401 /// the DSNode handle for the callee function (or function pointer), and
402 /// the DSNode handles for the function arguments.
404 /// One unusual aspect of this callsite record is the ResolvingCaller member.
405 /// If this is non-null, then it indicates the function that allowed a call-site
406 /// to finally be resolved. Because of indirect calls, this function may not
407 /// actually be the function that contains the Call instruction itself. This is
408 /// used by the BU and TD passes to communicate.
411 CallInst *Inst; // Actual call site
412 DSNodeHandle RetVal; // Returned value
413 DSNodeHandle Callee; // The function node called
414 std::vector<DSNodeHandle> CallArgs; // The pointer arguments
415 Function *ResolvingCaller; // See comments above
417 static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
418 const std::map<const DSNode*, DSNode*> &NodeMap) {
419 if (DSNode *N = Src.getNode()) {
420 std::map<const DSNode*, DSNode*>::const_iterator I = NodeMap.find(N);
421 assert(I != NodeMap.end() && "Not not in mapping!");
423 NH.setOffset(Src.getOffset());
424 NH.setNode(I->second);
428 static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
429 const std::map<const DSNode*, DSNodeHandle> &NodeMap) {
430 if (DSNode *N = Src.getNode()) {
431 std::map<const DSNode*, DSNodeHandle>::const_iterator I = NodeMap.find(N);
432 assert(I != NodeMap.end() && "Not not in mapping!");
434 NH.setOffset(Src.getOffset()+I->second.getOffset());
435 NH.setNode(I->second.getNode());
439 DSCallSite(); // DO NOT IMPLEMENT
441 /// Constructor. Note - This ctor destroys the argument vector passed in. On
442 /// exit, the argument vector is empty.
444 DSCallSite(CallInst &inst, const DSNodeHandle &rv, const DSNodeHandle &callee,
445 std::vector<DSNodeHandle> &Args)
446 : Inst(&inst), RetVal(rv), Callee(callee), ResolvingCaller(0) {
450 DSCallSite(const DSCallSite &DSCS) // Simple copy ctor
451 : Inst(DSCS.Inst), RetVal(DSCS.RetVal),
452 Callee(DSCS.Callee), CallArgs(DSCS.CallArgs),
453 ResolvingCaller(DSCS.ResolvingCaller) {}
455 /// Mapping copy constructor - This constructor takes a preexisting call site
456 /// to copy plus a map that specifies how the links should be transformed.
457 /// This is useful when moving a call site from one graph to another.
459 template<typename MapTy>
460 DSCallSite(const DSCallSite &FromCall, const MapTy &NodeMap) {
461 Inst = FromCall.Inst;
462 InitNH(RetVal, FromCall.RetVal, NodeMap);
463 InitNH(Callee, FromCall.Callee, NodeMap);
465 CallArgs.resize(FromCall.CallArgs.size());
466 for (unsigned i = 0, e = FromCall.CallArgs.size(); i != e; ++i)
467 InitNH(CallArgs[i], FromCall.CallArgs[i], NodeMap);
468 ResolvingCaller = FromCall.ResolvingCaller;
471 // Accessor functions...
472 Function &getCaller() const;
473 CallInst &getCallInst() const { return *Inst; }
474 DSNodeHandle &getRetVal() { return RetVal; }
475 DSNodeHandle &getCallee() { return Callee; }
476 const DSNodeHandle &getRetVal() const { return RetVal; }
477 const DSNodeHandle &getCallee() const { return Callee; }
478 void setCallee(const DSNodeHandle &H) { Callee = H; }
480 unsigned getNumPtrArgs() const { return CallArgs.size(); }
482 Function *getResolvingCaller() const { return ResolvingCaller; }
483 void setResolvingCaller(Function *F) { ResolvingCaller = F; }
485 DSNodeHandle &getPtrArg(unsigned i) {
486 assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
489 const DSNodeHandle &getPtrArg(unsigned i) const {
490 assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
494 bool operator<(const DSCallSite &CS) const {
495 if (RetVal < CS.RetVal) return true;
496 if (RetVal > CS.RetVal) return false;
497 if (Callee < CS.Callee) return true;
498 if (Callee > CS.Callee) return false;
499 return CallArgs < CS.CallArgs;
502 bool operator==(const DSCallSite &CS) const {
503 return RetVal == CS.RetVal && Callee == CS.Callee &&
504 CallArgs == CS.CallArgs;
509 //===----------------------------------------------------------------------===//
510 /// DSGraph - The graph that represents a function.
514 std::vector<DSNode*> Nodes;
515 DSNodeHandle RetNode; // Node that gets returned...
516 std::map<Value*, DSNodeHandle> ValueMap;
519 // GlobalsGraph -- Reference to the common graph of globally visible objects.
520 // This includes GlobalValues, New nodes, Cast nodes, and Calls.
522 GlobalDSGraph* GlobalsGraph;
525 // FunctionCalls - This vector maintains a single entry for each call
526 // instruction in the current graph. Each call entry contains DSNodeHandles
527 // that refer to the arguments that are passed into the function call. The
528 // first entry in the vector is the scalar that holds the return value for the
529 // call, the second is the function scalar being invoked, and the rest are
530 // pointer arguments to the function.
532 std::vector<DSCallSite> FunctionCalls;
534 void operator=(const DSGraph &); // DO NOT IMPLEMENT
536 DSGraph() : Func(0) {} // Create a new, empty, DSGraph.
537 DSGraph(Function &F); // Compute the local DSGraph
539 // Copy ctor - If you want to capture the node mapping between the source and
540 // destination graph, you may optionally do this by specifying a map to record
542 DSGraph(const DSGraph &DSG);
543 DSGraph(const DSGraph &DSG, std::map<const DSNode*, DSNode*> &BUNodeMap);
546 bool hasFunction() const { return Func != 0; }
547 Function &getFunction() const { return *Func; }
549 /// getNodes - Get a vector of all the nodes in the graph
551 const std::vector<DSNode*> &getNodes() const { return Nodes; }
552 std::vector<DSNode*> &getNodes() { return Nodes; }
554 /// addNode - Add a new node to the graph.
556 void addNode(DSNode *N) { Nodes.push_back(N); }
558 /// getValueMap - Get a map that describes what the nodes the scalars in this
559 /// function point to...
561 std::map<Value*, DSNodeHandle> &getValueMap() { return ValueMap; }
562 const std::map<Value*, DSNodeHandle> &getValueMap() const { return ValueMap;}
564 std::vector<DSCallSite> &getFunctionCalls() {
565 return FunctionCalls;
567 const std::vector<DSCallSite> &getFunctionCalls() const {
568 return FunctionCalls;
571 /// getNodeForValue - Given a value that is used or defined in the body of the
572 /// current function, return the DSNode that it points to.
574 DSNodeHandle &getNodeForValue(Value *V) { return ValueMap[V]; }
576 const DSNodeHandle &getRetNode() const { return RetNode; }
577 DSNodeHandle &getRetNode() { return RetNode; }
579 unsigned getGraphSize() const {
583 void print(std::ostream &O) const;
585 void writeGraphToFile(std::ostream &O, const std::string &GraphName) const;
587 // maskNodeTypes - Apply a mask to all of the node types in the graph. This
588 // is useful for clearing out markers like Scalar or Incomplete.
590 void maskNodeTypes(unsigned char Mask);
591 void maskIncompleteMarkers() { maskNodeTypes(~DSNode::Incomplete); }
593 // markIncompleteNodes - Traverse the graph, identifying nodes that may be
594 // modified by other functions that have not been resolved yet. This marks
595 // nodes that are reachable through three sources of "unknownness":
596 // Global Variables, Function Calls, and Incoming Arguments
598 // For any node that may have unknown components (because something outside
599 // the scope of current analysis may have modified it), the 'Incomplete' flag
600 // is added to the NodeType.
602 void markIncompleteNodes(bool markFormalArgs = true);
604 // removeTriviallyDeadNodes - After the graph has been constructed, this
605 // method removes all unreachable nodes that are created because they got
606 // merged with other nodes in the graph.
608 void removeTriviallyDeadNodes(bool KeepAllGlobals = false);
610 // removeDeadNodes - Use a more powerful reachability analysis to eliminate
611 // subgraphs that are unreachable. This often occurs because the data
612 // structure doesn't "escape" into it's caller, and thus should be eliminated
613 // from the caller's graph entirely. This is only appropriate to use when
616 void removeDeadNodes(bool KeepAllGlobals = false, bool KeepCalls = true);
618 // cloneInto - Clone the specified DSGraph into the current graph, returning
619 // the Return node of the graph. The translated ValueMap for the old function
620 // is filled into the OldValMap member.
621 // If StripScalars (StripAllocas) is set to true, Scalar (Alloca) markers
622 // are removed from the graph as the graph is being cloned.
624 DSNodeHandle cloneInto(const DSGraph &G,
625 std::map<Value*, DSNodeHandle> &OldValMap,
626 std::map<const DSNode*, DSNode*> &OldNodeMap,
627 bool StripScalars = false, bool StripAllocas = false);
630 // cloneGlobalInto - Clone the given global node (or the node for the given
631 // GlobalValue) from the GlobalsGraph and all its target links (recursively).
633 DSNode* cloneGlobalInto(const DSNode* GNode);
634 DSNode* cloneGlobalInto(GlobalValue* GV) {
635 assert(!GV || (((DSGraph*) GlobalsGraph)->ValueMap[GV] != 0));
636 return GV? cloneGlobalInto(((DSGraph*) GlobalsGraph)->ValueMap[GV]) : 0;
641 bool isNodeDead(DSNode *N);