blob: e8a720eb663ca4d1dce1ebea0908c77554206f05 [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 +000015
16static RegisterAnalysis<TDDataStructures>
17Y("tddatastructure", "Top-down Data Structure Analysis Closure");
Vikram S. Adveaaeee752002-07-30 22:06:40 +000018
19// releaseMemory - If the pass pipeline is done with this pass, we can release
20// our memory... here...
21//
22void TDDataStructures::releaseMemory() {
Chris Lattner13ec72a2002-10-21 13:31:48 +000023 for (std::map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
Vikram S. Adveaaeee752002-07-30 22:06:40 +000024 E = DSInfo.end(); I != E; ++I)
25 delete I->second;
26
27 // Empty map so next time memory is released, data structures are not
28 // re-deleted.
29 DSInfo.clear();
30}
31
32// run - Calculate the top down data structure graphs for each function in the
33// program.
34//
35bool TDDataStructures::run(Module &M) {
36 // Simply calculate the graphs for each function...
37 for (Module::reverse_iterator I = M.rbegin(), E = M.rend(); I != E; ++I)
38 if (!I->isExternal())
39 calculateGraph(*I);
40 return false;
41}
42
Chris Lattner0e744122002-10-17 04:26:54 +000043/// ResolveCallSite - This method is used to link the actual arguments together
44/// with the formal arguments for a function call in the top-down closure. This
45/// method assumes that the call site arguments have been mapped into nodes
46/// local to the specified graph.
47///
48void TDDataStructures::ResolveCallSite(DSGraph &Graph,
Vikram S. Adve42fd1692002-10-20 18:07:37 +000049 const DSCallSite &CallSite) {
Chris Lattner0e744122002-10-17 04:26:54 +000050 // Resolve all of the function formal arguments...
51 Function &F = Graph.getFunction();
52 Function::aiterator AI = F.abegin();
Vikram S. Adveaaeee752002-07-30 22:06:40 +000053
Vikram S. Adve26b98262002-10-20 21:41:02 +000054 for (unsigned i = 0, e = CallSite.getNumPtrArgs(); i != e; ++i, ++AI) {
Chris Lattner0e744122002-10-17 04:26:54 +000055 // Advance the argument iterator to the first pointer argument...
56 while (!DataStructureAnalysis::isPointerType(AI->getType())) ++AI;
Vikram S. Adveaaeee752002-07-30 22:06:40 +000057
Chris Lattner0e744122002-10-17 04:26:54 +000058 // TD ...Merge the formal arg scalar with the actual arg node
59 DSNodeHandle &NodeForFormal = Graph.getNodeForValue(AI);
60 if (NodeForFormal.getNode())
Chris Lattner0969c502002-10-21 02:08:03 +000061 NodeForFormal.mergeWith(CallSite.getPtrArg(i));
Chris Lattner0e744122002-10-17 04:26:54 +000062 }
63
64 // Merge returned node in the caller with the "return" node in callee
Chris Lattner0969c502002-10-21 02:08:03 +000065 if (CallSite.getRetVal().getNode() && Graph.getRetNode().getNode())
66 Graph.getRetNode().mergeWith(CallSite.getRetVal());
Vikram S. Adveaaeee752002-07-30 22:06:40 +000067}
68
Vikram S. Adve26b98262002-10-20 21:41:02 +000069
70static DSNodeHandle copyHelper(const DSNodeHandle* fromNode,
71 std::map<const DSNode*, DSNode*> *NodeMap) {
72 return DSNodeHandle((*NodeMap)[fromNode->getNode()], fromNode->getOffset());
73}
74
75
Vikram S. Adveaaeee752002-07-30 22:06:40 +000076DSGraph &TDDataStructures::calculateGraph(Function &F) {
77 // Make sure this graph has not already been calculated, or that we don't get
78 // into an infinite loop with mutually recursive functions.
79 //
80 DSGraph *&Graph = DSInfo[&F];
81 if (Graph) return *Graph;
82
Chris Lattner0e744122002-10-17 04:26:54 +000083 BUDataStructures &BU = getAnalysis<BUDataStructures>();
84 DSGraph &BUGraph = BU.getDSGraph(F);
Vikram S. Adveaaeee752002-07-30 22:06:40 +000085 Graph = new DSGraph(BUGraph);
86
Chris Lattner13ec72a2002-10-21 13:31:48 +000087 const std::vector<DSCallSite> *CallSitesP = BU.getCallSites(F);
Chris Lattner0e744122002-10-17 04:26:54 +000088 if (CallSitesP == 0) {
89 DEBUG(std::cerr << " [TD] No callers for: " << F.getName() << "\n");
90 return *Graph; // If no call sites, the graph is the same as the BU graph!
Vikram S. Adveaaeee752002-07-30 22:06:40 +000091 }
92
Chris Lattner0e744122002-10-17 04:26:54 +000093 // Loop over all call sites of this function, merging each one into this
94 // graph.
95 //
96 DEBUG(std::cerr << " [TD] Inlining callers for: " << F.getName() << "\n");
Chris Lattner13ec72a2002-10-21 13:31:48 +000097 const std::vector<DSCallSite> &CallSites = *CallSitesP;
Chris Lattner0e744122002-10-17 04:26:54 +000098 for (unsigned c = 0, ce = CallSites.size(); c != ce; ++c) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +000099 const DSCallSite &CallSite = CallSites[c]; // Copy
100 Function &Caller = CallSite.getCaller();
Chris Lattner0e744122002-10-17 04:26:54 +0000101 assert(!Caller.isExternal() && "Externals function cannot 'call'!");
102
103 DEBUG(std::cerr << "\t [TD] Inlining caller #" << c << " '"
104 << Caller.getName() << "' into callee: " << F.getName() << "\n");
105
106 if (&Caller == &F) {
107 // Self-recursive call: this can happen after a cycle of calls is inlined.
108 ResolveCallSite(*Graph, CallSite);
109 } else {
110 // Recursively compute the graph for the Caller. That should
111 // be fully resolved except if there is mutual recursion...
112 //
113 DSGraph &CG = calculateGraph(Caller); // Graph to inline
114
115 DEBUG(std::cerr << "\t\t[TD] Got graph for " << Caller.getName()
116 << " in: " << F.getName() << "\n");
117
118 // These two maps keep track of where scalars in the old graph _used_
119 // to point to, and of new nodes matching nodes of the old graph.
120 std::map<Value*, DSNodeHandle> OldValMap;
121 std::map<const DSNode*, DSNode*> OldNodeMap;
122
123 // Clone the Caller's graph into the current graph, keeping
124 // track of where scalars in the old graph _used_ to point...
125 // Do this here because it only needs to happens once for each Caller!
126 // Strip scalars but not allocas since they are alive in callee.
127 //
128 DSNodeHandle RetVal = Graph->cloneInto(CG, OldValMap, OldNodeMap,
129 /*StripScalars*/ true,
130 /*StripAllocas*/ false,
131 /*CopyCallers*/ true,
132 /*CopyOrigCalls*/false);
133
134 // Make a temporary copy of the call site, and transform the argument node
135 // pointers.
Vikram S. Adve26b98262002-10-20 21:41:02 +0000136 DSCallSite TmpCallSite(CallSite, std::bind2nd(std::ptr_fun(&copyHelper),
137 &OldNodeMap));
Chris Lattner0e744122002-10-17 04:26:54 +0000138 ResolveCallSite(*Graph, CallSite);
Chris Lattner0e744122002-10-17 04:26:54 +0000139 }
140 }
Chris Lattner0e744122002-10-17 04:26:54 +0000141
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000142 // Recompute the Incomplete markers and eliminate unreachable nodes.
143 Graph->maskIncompleteMarkers();
Chris Lattner19db0492002-10-17 04:57:28 +0000144 Graph->markIncompleteNodes(/*markFormals*/ !F.hasInternalLinkage()
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000145 /*&& FIXME: NEED TO CHECK IF ALL CALLERS FOUND!*/);
146 Graph->removeDeadNodes(/*KeepAllGlobals*/ false, /*KeepCalls*/ false);
147
Chris Lattner221c9792002-08-07 21:41:11 +0000148 DEBUG(std::cerr << " [TD] Done inlining callers for: " << F.getName() << " ["
149 << Graph->getGraphSize() << "+" << Graph->getFunctionCalls().size()
150 << "]\n");
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000151
152 return *Graph;
153}