1 //===- DataStructure.h - Build data structure graphs -------------*- C++ -*--=//
3 // Implement the LLVM data structure analysis library.
5 //===----------------------------------------------------------------------===//
7 #ifndef LLVM_ANALYSIS_DATA_STRUCTURE_H
8 #define LLVM_ANALYSIS_DATA_STRUCTURE_H
10 #include "llvm/Pass.h"
11 #include "llvm/GlobalValue.h"
12 #include "Support/HashExtras.h"
13 #include "Support/hash_set"
19 class DSNode; // Each node in the graph
20 class DSGraph; // A graph for a function
21 class GlobalDSGraph; // A common graph for globals in a program
22 class DSNodeIterator; // Data structure graph traversal iterator
23 class LocalDataStructures; // A collection of local graphs for a program
24 class BUDataStructures; // A collection of bu graphs for a program
25 class TDDataStructures; // A collection of td graphs for a program
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.
35 // Allow construction, destruction, and assignment...
36 DSNodeHandle(DSNode *n = 0) : N(0) { operator=(n); }
37 DSNodeHandle(const DSNodeHandle &H) : N(0) { operator=(H.N); }
38 ~DSNodeHandle() { operator=(0); }
39 DSNodeHandle &operator=(const DSNodeHandle &H) {operator=(H.N); return *this;}
41 // Assignment of DSNode*, implement all of the add/un'refing (defined later)
42 inline DSNodeHandle &operator=(DSNode *n);
44 // Allow automatic, implicit, conversion to DSNode*
45 operator DSNode*() { return N; }
46 operator const DSNode*() const { return N; }
47 operator const DSNode*() { return N; }
48 operator bool() const { return N != 0; }
49 operator bool() { return N != 0; }
51 bool operator<(const DSNodeHandle &H) const { // Allow sorting
54 bool operator==(const DSNodeHandle &H) const { return N == H.N; }
55 bool operator!=(const DSNodeHandle &H) const { return N != H.N; }
56 bool operator==(const DSNode *Node) const { return N == Node; }
57 bool operator!=(const DSNode *Node) const { return N != Node; }
58 bool operator==(DSNode *Node) const { return N == Node; }
59 bool operator!=(DSNode *Node) const { return N != Node; }
61 // Avoid having comparisons to null cause errors...
62 bool operator==(int X) const {
63 assert(X == 0 && "Bad comparison!");
64 return operator==((DSNode*)0);
66 bool operator!=(int X) const { return !operator==(X); }
68 // Allow explicit conversion to DSNode...
69 DSNode *get() { return N; }
70 const DSNode *get() const { return N; }
72 // Allow this to be treated like a pointer...
73 DSNode *operator->() { return N; }
74 const DSNode *operator->() const { return N; }
78 //===----------------------------------------------------------------------===//
79 // DSNode - Data structure node class
81 // This class keeps track of a node's type, and the fields in the data
87 std::vector<DSNodeHandle> Links;
88 std::vector<DSNodeHandle*> Referrers;
90 // Globals - The list of global values that are merged into this node.
91 std::vector<GlobalValue*> Globals;
93 void operator=(const DSNode &); // DO NOT IMPLEMENT
96 ShadowNode = 0, // Nothing is known about this node...
97 ScalarNode = 1 << 0, // Scalar of the current function contains this value
98 AllocaNode = 1 << 1, // This node was allocated with alloca
99 NewNode = 1 << 2, // This node was allocated with malloc
100 GlobalNode = 1 << 3, // This node was allocated by a global var decl
101 SubElement = 1 << 4, // This node is a part of some other node
102 CastNode = 1 << 5, // This node is accessed in unsafe ways
103 Incomplete = 1 << 6, // This node may not be complete
106 // NodeType - A union of the above bits. "Shadow" nodes do not add any flags
107 // to the nodes in the data structure graph, so it is possible to have nodes
108 // with a value of 0 for their NodeType. Scalar and Alloca markers go away
109 // when function graphs are inlined.
111 unsigned char NodeType;
113 DSNode(enum NodeTy NT, const Type *T);
114 DSNode(const DSNode &);
118 dropAllReferences(); // Only needed to satisfy assertion checks...
120 assert(Referrers.empty() && "Referrers to dead node exist!");
123 // Iterator for graph interface...
124 typedef DSNodeIterator iterator;
125 inline iterator begin(); // Defined in DataStructureGraph.h
126 inline iterator end();
129 const Type *getType() const { return Ty; }
131 unsigned getNumLinks() const { return Links.size(); }
132 DSNode *getLink(unsigned i) {
133 assert(i < getNumLinks() && "Field links access out of range...");
136 const DSNode *getLink(unsigned i) const {
137 assert(i < getNumLinks() && "Field links access out of range...");
141 void setLink(unsigned i, DSNode *N) {
142 assert(i < getNumLinks() && "Field links access out of range...");
146 // addGlobal - Add an entry for a global value to the Globals list. This also
147 // marks the node with the 'G' flag if it does not already have it.
149 void addGlobal(GlobalValue *GV);
150 const std::vector<GlobalValue*> &getGlobals() const { return Globals; }
151 std::vector<GlobalValue*> &getGlobals() { return Globals; }
153 // addEdgeTo - Add an edge from the current node to the specified node. This
154 // can cause merging of nodes in the graph.
156 void addEdgeTo(unsigned LinkNo, DSNode *N);
157 void addEdgeTo(DSNode *N) {
158 assert(getNumLinks() == 1 && "Must specify a field number to add edge if "
159 " more than one field exists!");
163 // mergeWith - Merge this node into the specified node, moving all links to
164 // and from the argument node into the current node. The specified node may
165 // be a null pointer (in which case, nothing happens).
167 void mergeWith(DSNode *N);
169 // addReferrer - Keep the referrer set up to date...
170 void addReferrer(DSNodeHandle *H) { Referrers.push_back(H); }
171 void removeReferrer(DSNodeHandle *H);
172 const std::vector<DSNodeHandle*> &getReferrers() const { return Referrers; }
174 void print(std::ostream &O, const DSGraph *G) const;
177 std::string getCaption(const DSGraph *G) const;
179 void dropAllReferences() {
185 inline DSNodeHandle &DSNodeHandle::operator=(DSNode *n) {
186 if (N) N->removeReferrer(this);
188 if (N) N->addReferrer(this);
193 // DSGraph - The graph that represents a function.
196 friend class GlobalDSGraph;
199 std::vector<DSNode*> Nodes;
200 DSNodeHandle RetNode; // Node that gets returned...
201 std::map<Value*, DSNodeHandle> ValueMap;
203 // GlobalsGraph -- Reference to the common graph of globally visible objects.
204 // This includes GlobalValues, New nodes, Cast nodes, and Calls.
206 GlobalDSGraph* GlobalsGraph;
208 // FunctionCalls - This vector maintains a single entry for each call
209 // instruction in the current graph. Each call entry contains DSNodeHandles
210 // that refer to the arguments that are passed into the function call. The
211 // first entry in the vector is the scalar that holds the return value for the
212 // call, the second is the function scalar being invoked, and the rest are
213 // pointer arguments to the function.
215 std::vector<std::vector<DSNodeHandle> > FunctionCalls;
217 // OrigFunctionCalls - This vector retains a copy of the original function
218 // calls of the current graph. This is needed to support top-down inlining
219 // after bottom-up inlining is complete, since the latter deletes call nodes.
221 std::vector<std::vector<DSNodeHandle> > OrigFunctionCalls;
223 // PendingCallers - This vector records all unresolved callers of the
224 // current function, i.e., ones whose graphs have not been inlined into
225 // the current graph. As long as there are unresolved callers, the nodes
226 // for formal arguments in the current graph cannot be eliminated, and
227 // nodes in the graph reachable from the formal argument nodes or
228 // global variable nodes must be considered incomplete.
229 std::set<Function*> PendingCallers;
232 // Define the interface only accessable to DataStructure
233 friend class LocalDataStructures;
234 friend class BUDataStructures;
235 friend class TDDataStructures;
236 DSGraph(Function &F, GlobalDSGraph* GlobalsG); // Compute the local DSGraph
237 DSGraph(const DSGraph &DSG); // Copy ctor
240 // clone all the call nodes and save the copies in OrigFunctionCalls
241 void saveOrigFunctionCalls() {
242 assert(OrigFunctionCalls.size() == 0 && "Do this only once!");
243 OrigFunctionCalls = FunctionCalls;
246 // get the saved copies of the original function call nodes
247 std::vector<std::vector<DSNodeHandle> > &getOrigFunctionCalls() {
248 return OrigFunctionCalls;
251 void operator=(const DSGraph &); // DO NOT IMPLEMENT
254 Function &getFunction() const { return Func; }
256 // getNodes - Get a vector of all the nodes in the graph
258 const std::vector<DSNode*>& getNodes() const { return Nodes; }
259 std::vector<DSNode*>& getNodes() { return Nodes; }
261 // getValueMap - Get a map that describes what the nodes the scalars in this
262 // function point to...
264 std::map<Value*, DSNodeHandle> &getValueMap() { return ValueMap; }
265 const std::map<Value*, DSNodeHandle> &getValueMap() const { return ValueMap;}
267 std::vector<std::vector<DSNodeHandle> > &getFunctionCalls() {
268 return FunctionCalls;
270 const std::vector<std::vector<DSNodeHandle> > &getFunctionCalls() const {
271 return FunctionCalls;
274 const DSNode *getRetNode() const { return RetNode; }
275 DSNode *getRetNode() { return RetNode; }
277 unsigned getGraphSize() const {
281 void print(std::ostream &O) const;
284 // maskNodeTypes - Apply a mask to all of the node types in the graph. This
285 // is useful for clearing out markers like Scalar or Incomplete.
287 void maskNodeTypes(unsigned char Mask);
288 void maskIncompleteMarkers() { maskNodeTypes(~DSNode::Incomplete); }
290 // markIncompleteNodes - Traverse the graph, identifying nodes that may be
291 // modified by other functions that have not been resolved yet. This marks
292 // nodes that are reachable through three sources of "unknownness":
293 // Global Variables, Function Calls, and Incoming Arguments
295 // For any node that may have unknown components (because something outside
296 // the scope of current analysis may have modified it), the 'Incomplete' flag
297 // is added to the NodeType.
299 void markIncompleteNodes(bool markFormalArgs = true);
301 // removeTriviallyDeadNodes - After the graph has been constructed, this
302 // method removes all unreachable nodes that are created because they got
303 // merged with other nodes in the graph.
305 void removeTriviallyDeadNodes(bool KeepAllGlobals = false);
307 // removeDeadNodes - Use a more powerful reachability analysis to eliminate
308 // subgraphs that are unreachable. This often occurs because the data
309 // structure doesn't "escape" into it's caller, and thus should be eliminated
310 // from the caller's graph entirely. This is only appropriate to use when
313 void removeDeadNodes(bool KeepAllGlobals = false, bool KeepCalls = true);
315 // AddCaller - add a known caller node into the graph and mark it pending.
316 // getCallers - get a vector of the functions that call this one
317 // getCallersPending - get a matching vector of bools indicating if each
318 // caller's DSGraph has been resolved into this one.
320 void addCaller(Function& caller) {
321 PendingCallers.insert(&caller);
323 std::set<Function*>& getPendingCallers() {
324 return PendingCallers;
327 // cloneInto - Clone the specified DSGraph into the current graph, returning
328 // the Return node of the graph. The translated ValueMap for the old function
329 // is filled into the OldValMap member.
330 // If StripScalars (StripAllocas) is set to true, Scalar (Alloca) markers
331 // are removed from the graph as the graph is being cloned.
332 // If CopyCallers is set to true, the PendingCallers list is copied.
333 // If CopyOrigCalls is set to true, the OrigFunctionCalls list is copied.
335 DSNode *cloneInto(const DSGraph &G, std::map<Value*, DSNodeHandle> &OldValMap,
336 std::map<const DSNode*, DSNode*>& OldNodeMap,
337 bool StripScalars = false, bool StripAllocas = false,
338 bool CopyCallers = true, bool CopyOrigCalls = true);
340 // cloneGlobalInto - Clone the given global node (or the node for the given
341 // GlobalValue) from the GlobalsGraph and all its target links (recursively).
343 DSNode* cloneGlobalInto(const DSNode* GNode);
344 DSNode* cloneGlobalInto(GlobalValue* GV) {
345 assert(!GV || (((DSGraph*) GlobalsGraph)->ValueMap[GV] != 0));
346 return GV? cloneGlobalInto(((DSGraph*) GlobalsGraph)->ValueMap[GV]) : 0;
350 bool isNodeDead(DSNode *N);
354 // GlobalDSGraph - A common graph for all the globals and their outgoing links
355 // to externally visible nodes. This includes GlobalValues, New nodes,
356 // Cast nodes, and Calls. This graph can only be used by one of the
357 // individual function graphs, and it goes away when they all go away.
359 class GlobalDSGraph : public DSGraph {
360 hash_set<const DSGraph*> Referrers;
361 void addReference(const DSGraph* referrer);
362 void removeReference(const DSGraph* referrer);
363 friend class DSGraph; // give access to Referrers
365 GlobalDSGraph(const GlobalDSGraph &GlobalDSG); // Do not implement
367 // Helper function for cloneGlobals and cloneCalls
368 DSNode* cloneNodeInto(DSNode *OldNode,
369 std::map<const DSNode*, DSNode*> &NodeCache,
370 bool GlobalsAreFinal = false);
373 GlobalDSGraph(); // Create an empty DSGraph
374 virtual ~GlobalDSGraph();
376 void cloneGlobals(DSGraph& Graph, bool CloneCalls = false);
377 void cloneCalls (DSGraph& Graph);
381 // LocalDataStructures - The analysis that computes the local data structure
382 // graphs for all of the functions in the program.
384 // FIXME: This should be a Function pass that can be USED by a Pass, and would
385 // be automatically preserved. Until we can do that, this is a Pass.
387 class LocalDataStructures : public Pass {
388 // DSInfo, one graph for each function
389 std::map<const Function*, DSGraph*> DSInfo;
391 ~LocalDataStructures() { releaseMemory(); }
393 virtual bool run(Module &M);
395 // getDSGraph - Return the data structure graph for the specified function.
396 DSGraph &getDSGraph(const Function &F) const {
397 std::map<const Function*, DSGraph*>::const_iterator I = DSInfo.find(&F);
398 assert(I != DSInfo.end() && "Function not in module!");
402 // print - Print out the analysis results...
403 void print(std::ostream &O, const Module *M) const;
405 // If the pass pipeline is done with this pass, we can release our memory...
406 virtual void releaseMemory();
408 // getAnalysisUsage - This obviously provides a data structure graph.
409 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
410 AU.setPreservesAll();
415 // BUDataStructures - The analysis that computes the interprocedurally closed
416 // data structure graphs for all of the functions in the program. This pass
417 // only performs a "Bottom Up" propogation (hence the name).
419 class BUDataStructures : public Pass {
420 // DSInfo, one graph for each function
421 std::map<const Function*, DSGraph*> DSInfo;
423 ~BUDataStructures() { releaseMemory(); }
425 virtual bool run(Module &M);
427 // getDSGraph - Return the data structure graph for the specified function.
428 DSGraph &getDSGraph(const Function &F) const {
429 std::map<const Function*, DSGraph*>::const_iterator I = DSInfo.find(&F);
430 assert(I != DSInfo.end() && "Function not in module!");
434 // print - Print out the analysis results...
435 void print(std::ostream &O, const Module *M) const;
437 // If the pass pipeline is done with this pass, we can release our memory...
438 virtual void releaseMemory();
440 // getAnalysisUsage - This obviously provides a data structure graph.
441 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
442 AU.setPreservesAll();
443 AU.addRequired<LocalDataStructures>();
446 DSGraph &calculateGraph(Function &F);
450 // TDDataStructures - Analysis that computes new data structure graphs
451 // for each function using the closed graphs for the callers computed
452 // by the bottom-up pass.
454 class TDDataStructures : public Pass {
455 // DSInfo, one graph for each function
456 std::map<const Function*, DSGraph*> DSInfo;
458 ~TDDataStructures() { releaseMemory(); }
460 virtual bool run(Module &M);
462 // getDSGraph - Return the data structure graph for the specified function.
463 DSGraph &getDSGraph(const Function &F) const {
464 std::map<const Function*, DSGraph*>::const_iterator I = DSInfo.find(&F);
465 assert(I != DSInfo.end() && "Function not in module!");
469 // print - Print out the analysis results...
470 void print(std::ostream &O, const Module *M) const;
472 // If the pass pipeline is done with this pass, we can release our memory...
473 virtual void releaseMemory();
475 // getAnalysisUsage - This obviously provides a data structure graph.
476 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
477 AU.setPreservesAll();
478 AU.addRequired<BUDataStructures>();
481 DSGraph &calculateGraph(Function &F);
482 void pushGraphIntoCallee(DSGraph &callerGraph, DSGraph &calleeGraph,
483 std::map<Value*, DSNodeHandle> &OldValMap,
484 std::map<const DSNode*, DSNode*> &OldNodeMap);