blob: 7db811dbca9878004a51df9f2619a7d447ab9d16 [file] [log] [blame]
Vikram S. Adveaaeee752002-07-30 22:06:40 +00001//===- TopDownClosure.cpp - Compute the top-down interprocedure closure ---===//
2//
3// This file implements the TDDataStructures class, which represents the
4// Top-down Interprocedural closure of the data structure graph over the
5// program. This is useful (but not strictly necessary?) for applications
6// like pointer analysis.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Analysis/DataStructure.h"
Chris Lattner0e744122002-10-17 04:26:54 +000011#include "llvm/Analysis/DSGraph.h"
Vikram S. Adveaaeee752002-07-30 22:06:40 +000012#include "llvm/Module.h"
13#include "llvm/DerivedTypes.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000014#include "Support/Statistic.h"
Vikram S. Adveaaeee752002-07-30 22:06:40 +000015using std::map;
Chris Lattner0e744122002-10-17 04:26:54 +000016using std::vector;
Vikram S. Adveaaeee752002-07-30 22:06:40 +000017
18static RegisterAnalysis<TDDataStructures>
19Y("tddatastructure", "Top-down Data Structure Analysis Closure");
Vikram S. Adveaaeee752002-07-30 22:06:40 +000020
21// releaseMemory - If the pass pipeline is done with this pass, we can release
22// our memory... here...
23//
24void TDDataStructures::releaseMemory() {
25 for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
26 E = DSInfo.end(); I != E; ++I)
27 delete I->second;
28
29 // Empty map so next time memory is released, data structures are not
30 // re-deleted.
31 DSInfo.clear();
32}
33
34// run - Calculate the top down data structure graphs for each function in the
35// program.
36//
37bool TDDataStructures::run(Module &M) {
38 // Simply calculate the graphs for each function...
39 for (Module::reverse_iterator I = M.rbegin(), E = M.rend(); I != E; ++I)
40 if (!I->isExternal())
41 calculateGraph(*I);
42 return false;
43}
44
Chris Lattner0e744122002-10-17 04:26:54 +000045/// ResolveCallSite - This method is used to link the actual arguments together
46/// with the formal arguments for a function call in the top-down closure. This
47/// method assumes that the call site arguments have been mapped into nodes
48/// local to the specified graph.
49///
50void TDDataStructures::ResolveCallSite(DSGraph &Graph,
Vikram S. Adve42fd1692002-10-20 18:07:37 +000051 const DSCallSite &CallSite) {
Chris Lattner0e744122002-10-17 04:26:54 +000052 // Resolve all of the function formal arguments...
53 Function &F = Graph.getFunction();
54 Function::aiterator AI = F.abegin();
Vikram S. Adveaaeee752002-07-30 22:06:40 +000055
Vikram S. Adve26b98262002-10-20 21:41:02 +000056 for (unsigned i = 0, e = CallSite.getNumPtrArgs(); i != e; ++i, ++AI) {
Chris Lattner0e744122002-10-17 04:26:54 +000057 // Advance the argument iterator to the first pointer argument...
58 while (!DataStructureAnalysis::isPointerType(AI->getType())) ++AI;
Vikram S. Adveaaeee752002-07-30 22:06:40 +000059
Chris Lattner0e744122002-10-17 04:26:54 +000060 // TD ...Merge the formal arg scalar with the actual arg node
61 DSNodeHandle &NodeForFormal = Graph.getNodeForValue(AI);
62 if (NodeForFormal.getNode())
Chris Lattner0969c502002-10-21 02:08:03 +000063 NodeForFormal.mergeWith(CallSite.getPtrArg(i));
Chris Lattner0e744122002-10-17 04:26:54 +000064 }
65
66 // Merge returned node in the caller with the "return" node in callee
Chris Lattner0969c502002-10-21 02:08:03 +000067 if (CallSite.getRetVal().getNode() && Graph.getRetNode().getNode())
68 Graph.getRetNode().mergeWith(CallSite.getRetVal());
Vikram S. Adveaaeee752002-07-30 22:06:40 +000069}
70
Vikram S. Adve26b98262002-10-20 21:41:02 +000071
72static DSNodeHandle copyHelper(const DSNodeHandle* fromNode,
73 std::map<const DSNode*, DSNode*> *NodeMap) {
74 return DSNodeHandle((*NodeMap)[fromNode->getNode()], fromNode->getOffset());
75}
76
77
Vikram S. Adveaaeee752002-07-30 22:06:40 +000078DSGraph &TDDataStructures::calculateGraph(Function &F) {
79 // Make sure this graph has not already been calculated, or that we don't get
80 // into an infinite loop with mutually recursive functions.
81 //
82 DSGraph *&Graph = DSInfo[&F];
83 if (Graph) return *Graph;
84
Chris Lattner0e744122002-10-17 04:26:54 +000085 BUDataStructures &BU = getAnalysis<BUDataStructures>();
86 DSGraph &BUGraph = BU.getDSGraph(F);
Vikram S. Adveaaeee752002-07-30 22:06:40 +000087 Graph = new DSGraph(BUGraph);
88
Vikram S. Adve42fd1692002-10-20 18:07:37 +000089 const vector<DSCallSite> *CallSitesP = BU.getCallSites(F);
Chris Lattner0e744122002-10-17 04:26:54 +000090 if (CallSitesP == 0) {
91 DEBUG(std::cerr << " [TD] No callers for: " << F.getName() << "\n");
92 return *Graph; // If no call sites, the graph is the same as the BU graph!
Vikram S. Adveaaeee752002-07-30 22:06:40 +000093 }
94
Chris Lattner0e744122002-10-17 04:26:54 +000095 // Loop over all call sites of this function, merging each one into this
96 // graph.
97 //
98 DEBUG(std::cerr << " [TD] Inlining callers for: " << F.getName() << "\n");
Vikram S. Adve42fd1692002-10-20 18:07:37 +000099 const vector<DSCallSite> &CallSites = *CallSitesP;
Chris Lattner0e744122002-10-17 04:26:54 +0000100 for (unsigned c = 0, ce = CallSites.size(); c != ce; ++c) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000101 const DSCallSite &CallSite = CallSites[c]; // Copy
102 Function &Caller = CallSite.getCaller();
Chris Lattner0e744122002-10-17 04:26:54 +0000103 assert(!Caller.isExternal() && "Externals function cannot 'call'!");
104
105 DEBUG(std::cerr << "\t [TD] Inlining caller #" << c << " '"
106 << Caller.getName() << "' into callee: " << F.getName() << "\n");
107
108 if (&Caller == &F) {
109 // Self-recursive call: this can happen after a cycle of calls is inlined.
110 ResolveCallSite(*Graph, CallSite);
111 } else {
112 // Recursively compute the graph for the Caller. That should
113 // be fully resolved except if there is mutual recursion...
114 //
115 DSGraph &CG = calculateGraph(Caller); // Graph to inline
116
117 DEBUG(std::cerr << "\t\t[TD] Got graph for " << Caller.getName()
118 << " in: " << F.getName() << "\n");
119
120 // These two maps keep track of where scalars in the old graph _used_
121 // to point to, and of new nodes matching nodes of the old graph.
122 std::map<Value*, DSNodeHandle> OldValMap;
123 std::map<const DSNode*, DSNode*> OldNodeMap;
124
125 // Clone the Caller's graph into the current graph, keeping
126 // track of where scalars in the old graph _used_ to point...
127 // Do this here because it only needs to happens once for each Caller!
128 // Strip scalars but not allocas since they are alive in callee.
129 //
130 DSNodeHandle RetVal = Graph->cloneInto(CG, OldValMap, OldNodeMap,
131 /*StripScalars*/ true,
132 /*StripAllocas*/ false,
133 /*CopyCallers*/ true,
134 /*CopyOrigCalls*/false);
135
136 // Make a temporary copy of the call site, and transform the argument node
137 // pointers.
Vikram S. Adve26b98262002-10-20 21:41:02 +0000138 DSCallSite TmpCallSite(CallSite, std::bind2nd(std::ptr_fun(&copyHelper),
139 &OldNodeMap));
Chris Lattner0e744122002-10-17 04:26:54 +0000140 ResolveCallSite(*Graph, CallSite);
Chris Lattner0e744122002-10-17 04:26:54 +0000141 }
142 }
Chris Lattner0e744122002-10-17 04:26:54 +0000143
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000144 // Recompute the Incomplete markers and eliminate unreachable nodes.
145 Graph->maskIncompleteMarkers();
Chris Lattner19db0492002-10-17 04:57:28 +0000146 Graph->markIncompleteNodes(/*markFormals*/ !F.hasInternalLinkage()
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000147 /*&& FIXME: NEED TO CHECK IF ALL CALLERS FOUND!*/);
148 Graph->removeDeadNodes(/*KeepAllGlobals*/ false, /*KeepCalls*/ false);
149
Chris Lattner221c9792002-08-07 21:41:11 +0000150 DEBUG(std::cerr << " [TD] Done inlining callers for: " << F.getName() << " ["
151 << Graph->getGraphSize() << "+" << Graph->getFunctionCalls().size()
152 << "]\n");
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000153
154 return *Graph;
155}