blob: c573a52c9e0bd00b9ee2ee904f7c092988d92152 [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 Lattner198be222002-10-21 19:47:18 +000023 BUMaps.clear();
Chris Lattner13ec72a2002-10-21 13:31:48 +000024 for (std::map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
Vikram S. Adveaaeee752002-07-30 22:06:40 +000025 E = DSInfo.end(); I != E; ++I)
26 delete I->second;
27
28 // Empty map so next time memory is released, data structures are not
29 // re-deleted.
30 DSInfo.clear();
31}
32
33// run - Calculate the top down data structure graphs for each function in the
34// program.
35//
36bool TDDataStructures::run(Module &M) {
37 // Simply calculate the graphs for each function...
38 for (Module::reverse_iterator I = M.rbegin(), E = M.rend(); I != E; ++I)
39 if (!I->isExternal())
40 calculateGraph(*I);
41 return false;
42}
43
Chris Lattner0e744122002-10-17 04:26:54 +000044/// ResolveCallSite - This method is used to link the actual arguments together
45/// with the formal arguments for a function call in the top-down closure. This
46/// method assumes that the call site arguments have been mapped into nodes
47/// local to the specified graph.
48///
49void TDDataStructures::ResolveCallSite(DSGraph &Graph,
Vikram S. Adve42fd1692002-10-20 18:07:37 +000050 const DSCallSite &CallSite) {
Chris Lattner0e744122002-10-17 04:26:54 +000051 // Resolve all of the function formal arguments...
52 Function &F = Graph.getFunction();
53 Function::aiterator AI = F.abegin();
Vikram S. Adveaaeee752002-07-30 22:06:40 +000054
Vikram S. Adve26b98262002-10-20 21:41:02 +000055 for (unsigned i = 0, e = CallSite.getNumPtrArgs(); i != e; ++i, ++AI) {
Chris Lattner0e744122002-10-17 04:26:54 +000056 // Advance the argument iterator to the first pointer argument...
57 while (!DataStructureAnalysis::isPointerType(AI->getType())) ++AI;
Vikram S. Adveaaeee752002-07-30 22:06:40 +000058
Chris Lattner0e744122002-10-17 04:26:54 +000059 // TD ...Merge the formal arg scalar with the actual arg node
60 DSNodeHandle &NodeForFormal = Graph.getNodeForValue(AI);
Chris Lattner99a22842002-10-21 15:04:18 +000061 assert(NodeForFormal.getNode() && "Pointer argument has no dest node!");
62 NodeForFormal.mergeWith(CallSite.getPtrArg(i));
Chris Lattner0e744122002-10-17 04:26:54 +000063 }
64
65 // Merge returned node in the caller with the "return" node in callee
Chris Lattner0969c502002-10-21 02:08:03 +000066 if (CallSite.getRetVal().getNode() && Graph.getRetNode().getNode())
67 Graph.getRetNode().mergeWith(CallSite.getRetVal());
Vikram S. Adveaaeee752002-07-30 22:06:40 +000068}
69
Vikram S. Adve26b98262002-10-20 21:41:02 +000070
Vikram S. Adveaaeee752002-07-30 22:06:40 +000071DSGraph &TDDataStructures::calculateGraph(Function &F) {
72 // Make sure this graph has not already been calculated, or that we don't get
73 // into an infinite loop with mutually recursive functions.
74 //
75 DSGraph *&Graph = DSInfo[&F];
76 if (Graph) return *Graph;
77
Chris Lattner0e744122002-10-17 04:26:54 +000078 BUDataStructures &BU = getAnalysis<BUDataStructures>();
79 DSGraph &BUGraph = BU.getDSGraph(F);
Chris Lattner198be222002-10-21 19:47:18 +000080
81 // Copy the BU graph, keeping a mapping from the BUGraph to the current Graph
82 std::map<const DSNode*, DSNode*> BUNodeMap;
83 Graph = new DSGraph(BUGraph, BUNodeMap);
84
85 // Convert the mapping from a node-to-node map into a node-to-nodehandle map
86 BUMaps[&F].insert(BUNodeMap.begin(), BUNodeMap.end());
87 BUNodeMap.clear(); // We are done with the temporary map.
Vikram S. Adveaaeee752002-07-30 22:06:40 +000088
Chris Lattner13ec72a2002-10-21 13:31:48 +000089 const std::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");
Chris Lattner13ec72a2002-10-21 13:31:48 +000099 const std::vector<DSCallSite> &CallSites = *CallSitesP;
Chris Lattner0e744122002-10-17 04:26:54 +0000100 for (unsigned c = 0, ce = CallSites.size(); c != ce; ++c) {
Chris Lattner198be222002-10-21 19:47:18 +0000101 const DSCallSite &CallSite = CallSites[c];
102 Function &Caller = *CallSite.getResolvingCaller();
103 assert(&Caller && !Caller.isExternal() &&
104 "Externals function cannot 'call'!");
Chris Lattner0e744122002-10-17 04:26:54 +0000105
106 DEBUG(std::cerr << "\t [TD] Inlining caller #" << c << " '"
107 << Caller.getName() << "' into callee: " << F.getName() << "\n");
108
Chris Lattner198be222002-10-21 19:47:18 +0000109 if (&Caller == &F) {
110 // Self-recursive call: this can happen after a cycle of calls is inlined.
111 ResolveCallSite(*Graph, CallSite);
112 } else {
113
Chris Lattner99a22842002-10-21 15:04:18 +0000114 // Recursively compute the graph for the Caller. It should be fully
115 // resolved except if there is mutual recursion...
Chris Lattner0e744122002-10-17 04:26:54 +0000116 //
117 DSGraph &CG = calculateGraph(Caller); // Graph to inline
118
119 DEBUG(std::cerr << "\t\t[TD] Got graph for " << Caller.getName()
120 << " in: " << F.getName() << "\n");
121
122 // These two maps keep track of where scalars in the old graph _used_
123 // to point to, and of new nodes matching nodes of the old graph.
124 std::map<Value*, DSNodeHandle> OldValMap;
125 std::map<const DSNode*, DSNode*> OldNodeMap;
126
Chris Lattner198be222002-10-21 19:47:18 +0000127 // Translate call site from having links into the BU graph
128 DSCallSite CallSiteInCG(CallSite, BUMaps[&Caller]);
129
Chris Lattner0e744122002-10-17 04:26:54 +0000130 // Clone the Caller's graph into the current graph, keeping
131 // track of where scalars in the old graph _used_ to point...
132 // Do this here because it only needs to happens once for each Caller!
133 // Strip scalars but not allocas since they are alive in callee.
134 //
135 DSNodeHandle RetVal = Graph->cloneInto(CG, OldValMap, OldNodeMap,
136 /*StripScalars*/ true,
137 /*StripAllocas*/ false,
138 /*CopyCallers*/ true,
139 /*CopyOrigCalls*/false);
Chris Lattner198be222002-10-21 19:47:18 +0000140 ResolveCallSite(*Graph, DSCallSite(CallSiteInCG, OldNodeMap));
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.
Chris Lattner198be222002-10-21 19:47:18 +0000145#if 0
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000146 Graph->maskIncompleteMarkers();
Chris Lattner19db0492002-10-17 04:57:28 +0000147 Graph->markIncompleteNodes(/*markFormals*/ !F.hasInternalLinkage()
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000148 /*&& FIXME: NEED TO CHECK IF ALL CALLERS FOUND!*/);
149 Graph->removeDeadNodes(/*KeepAllGlobals*/ false, /*KeepCalls*/ false);
Chris Lattner198be222002-10-21 19:47:18 +0000150#endif
Chris Lattner221c9792002-08-07 21:41:11 +0000151 DEBUG(std::cerr << " [TD] Done inlining callers for: " << F.getName() << " ["
152 << Graph->getGraphSize() << "+" << Graph->getFunctionCalls().size()
153 << "]\n");
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000154
155 return *Graph;
156}