blob: 6b70490feb731dbfebdc86698f534131930553bf [file] [log] [blame]
Chris Lattner55c10582002-10-03 20:38:41 +00001//===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===//
Chris Lattner0d9bab82002-07-18 00:12:30 +00002//
3// This file implements the BUDataStructures class, which represents the
4// Bottom-Up Interprocedural closure of the data structure graph over the
5// program. This is useful for applications like pool allocation, but **not**
Chris Lattner55c10582002-10-03 20:38:41 +00006// applications like alias analysis.
Chris Lattner0d9bab82002-07-18 00:12:30 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Analysis/DataStructure.h"
Chris Lattner55c10582002-10-03 20:38:41 +000011#include "llvm/Analysis/DSGraph.h"
Chris Lattner0d9bab82002-07-18 00:12:30 +000012#include "llvm/Module.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000013#include "Support/Statistic.h"
Chris Lattner0d9bab82002-07-18 00:12:30 +000014using std::map;
15
Chris Lattner1e435162002-07-26 21:12:44 +000016static RegisterAnalysis<BUDataStructures>
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000017X("budatastructure", "Bottom-up Data Structure Analysis Closure");
Chris Lattner0d9bab82002-07-18 00:12:30 +000018
Chris Lattner92673292002-11-02 00:13:20 +000019namespace DataStructureAnalysis { // TODO: FIXME: Eliminate
Chris Lattner55c10582002-10-03 20:38:41 +000020 // isPointerType - Return true if this first class type is big enough to hold
21 // a pointer.
22 //
23 bool isPointerType(const Type *Ty);
24}
25using namespace DataStructureAnalysis;
26
27
Chris Lattner0d9bab82002-07-18 00:12:30 +000028// releaseMemory - If the pass pipeline is done with this pass, we can release
29// our memory... here...
30//
31void BUDataStructures::releaseMemory() {
Chris Lattner613692c2002-10-17 04:24:08 +000032 // Delete all call site information
33 CallSites.clear();
34
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000035 for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
Chris Lattner0d9bab82002-07-18 00:12:30 +000036 E = DSInfo.end(); I != E; ++I)
37 delete I->second;
38
39 // Empty map so next time memory is released, data structures are not
40 // re-deleted.
41 DSInfo.clear();
42}
43
44// run - Calculate the bottom up data structure graphs for each function in the
45// program.
46//
47bool BUDataStructures::run(Module &M) {
48 // Simply calculate the graphs for each function...
49 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
50 if (!I->isExternal())
51 calculateGraph(*I);
52 return false;
53}
54
Chris Lattner0d9bab82002-07-18 00:12:30 +000055// ResolveArguments - Resolve the formal and actual arguments for a function
56// call.
57//
Vikram S. Adve42fd1692002-10-20 18:07:37 +000058static void ResolveArguments(DSCallSite &Call, Function &F,
Chris Lattnerc875f022002-11-03 21:27:48 +000059 map<Value*, DSNodeHandle> &ScalarMap) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000060 // Resolve all of the function arguments...
61 Function::aiterator AI = F.abegin();
Chris Lattner92673292002-11-02 00:13:20 +000062 for (unsigned i = 0, e = Call.getNumPtrArgs(); i != e; ++i, ++AI) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000063 // Advance the argument iterator to the first pointer argument...
Chris Lattner048912b2002-11-04 02:29:15 +000064 while (!isPointerType(AI->getType())) {
65 ++AI;
66#ifndef NDEBUG
67 if (AI == F.aend())
68 std::cerr << "Bad call to Function: " << F.getName() << "\n";
69#endif
70 assert(AI != F.aend() && "# Args provided is not # Args required!");
71 }
Chris Lattner0d9bab82002-07-18 00:12:30 +000072
73 // Add the link from the argument scalar to the provided value
Chris Lattnerc875f022002-11-03 21:27:48 +000074 ScalarMap[AI].mergeWith(Call.getPtrArg(i));
Chris Lattner0d9bab82002-07-18 00:12:30 +000075 }
76}
77
Chris Lattner0d9bab82002-07-18 00:12:30 +000078DSGraph &BUDataStructures::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
85 // Copy the local version into DSInfo...
86 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(F));
87
Chris Lattner55c10582002-10-03 20:38:41 +000088#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000089 // Populate the GlobalsGraph with globals from this one.
90 Graph->GlobalsGraph->cloneGlobals(*Graph, /*cloneCalls*/ false);
Chris Lattner55c10582002-10-03 20:38:41 +000091#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000092
Chris Lattner0d9bab82002-07-18 00:12:30 +000093 // Start resolving calls...
Vikram S. Adve42fd1692002-10-20 18:07:37 +000094 std::vector<DSCallSite> &FCs = Graph->getFunctionCalls();
Chris Lattner0d9bab82002-07-18 00:12:30 +000095
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000096 DEBUG(std::cerr << " [BU] Inlining: " << F.getName() << "\n");
97
Chris Lattner0d9bab82002-07-18 00:12:30 +000098 bool Inlined;
99 do {
100 Inlined = false;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000101
Chris Lattner0d9bab82002-07-18 00:12:30 +0000102 for (unsigned i = 0; i != FCs.size(); ++i) {
103 // Copy the call, because inlining graphs may invalidate the FCs vector.
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000104 DSCallSite Call = FCs[i];
Chris Lattner0d9bab82002-07-18 00:12:30 +0000105
Chris Lattner55c10582002-10-03 20:38:41 +0000106 // If the function list is complete...
Chris Lattner0969c502002-10-21 02:08:03 +0000107 if ((Call.getCallee().getNode()->NodeType & DSNode::Incomplete)==0) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000108 // Start inlining all of the functions we can... some may not be
109 // inlinable if they are external...
110 //
Chris Lattner7836d602002-10-20 22:11:17 +0000111 std::vector<GlobalValue*> Callees =
Chris Lattner0969c502002-10-21 02:08:03 +0000112 Call.getCallee().getNode()->getGlobals();
Chris Lattner0d9bab82002-07-18 00:12:30 +0000113
114 // Loop over the functions, inlining whatever we can...
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000115 for (unsigned c = 0; c != Callees.size(); ++c) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000116 // Must be a function type, so this cast MUST succeed.
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000117 Function &FI = cast<Function>(*Callees[c]);
Chris Lattner613692c2002-10-17 04:24:08 +0000118
Chris Lattner0d9bab82002-07-18 00:12:30 +0000119 if (&FI == &F) {
120 // Self recursion... simply link up the formal arguments with the
121 // actual arguments...
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000122 DEBUG(std::cerr << "\t[BU] Self Inlining: " << F.getName() << "\n");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000123
Chris Lattner7836d602002-10-20 22:11:17 +0000124 // Handle the return value if present...
Chris Lattner92673292002-11-02 00:13:20 +0000125 Graph->getRetNode().mergeWith(Call.getRetVal());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000126
127 // Resolve the arguments in the call to the actual values...
Chris Lattnerc875f022002-11-03 21:27:48 +0000128 ResolveArguments(Call, F, Graph->getScalarMap());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000129
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000130 // Erase the entry in the callees vector
131 Callees.erase(Callees.begin()+c--);
Chris Lattner613692c2002-10-17 04:24:08 +0000132
Chris Lattner0d9bab82002-07-18 00:12:30 +0000133 } else if (!FI.isExternal()) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000134 DEBUG(std::cerr << "\t[BU] In " << F.getName() << " inlining: "
Chris Lattner0d9bab82002-07-18 00:12:30 +0000135 << FI.getName() << "\n");
Vikram S. Advec44e9bf2002-07-18 16:13:52 +0000136
Chris Lattner0d9bab82002-07-18 00:12:30 +0000137 // Get the data structure graph for the called function, closing it
138 // if possible (which is only impossible in the case of mutual
139 // recursion...
140 //
141 DSGraph &GI = calculateGraph(FI); // Graph to inline
142
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000143 DEBUG(std::cerr << "\t\t[BU] Got graph for " << FI.getName()
144 << " in: " << F.getName() << "\n");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000145
Vikram S. Adve26b98262002-10-20 21:41:02 +0000146 // Record that the original DSCallSite was a call site of FI.
147 // This may or may not have been known when the DSCallSite was
148 // originally created.
Chris Lattner198be222002-10-21 19:47:18 +0000149 std::vector<DSCallSite> &CallSitesForFunc = CallSites[&FI];
150 CallSitesForFunc.push_back(Call);
151 CallSitesForFunc.back().setResolvingCaller(&F);
Chris Lattner9faf18d2002-10-22 15:58:46 +0000152 CallSitesForFunc.back().setCallee(0);
Chris Lattner7a0b5bb2002-11-02 00:26:32 +0000153
Vikram S. Advec44e9bf2002-07-18 16:13:52 +0000154 // Clone the callee's graph into the current graph, keeping
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000155 // track of where scalars in the old graph _used_ to point,
156 // and of the new nodes matching nodes of the old graph.
Chris Lattner55c10582002-10-03 20:38:41 +0000157 map<Value*, DSNodeHandle> OldValMap;
158 map<const DSNode*, DSNode*> OldNodeMap;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000159
160 // The clone call may invalidate any of the vectors in the data
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000161 // structure graph. Strip locals and don't copy the list of callers
Chris Lattner55c10582002-10-03 20:38:41 +0000162 DSNodeHandle RetVal = Graph->cloneInto(GI, OldValMap, OldNodeMap,
163 /*StripScalars*/ true,
Chris Lattnere4ae3042002-10-21 19:50:29 +0000164 /*StripAllocas*/ true);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000165
Chris Lattner55c10582002-10-03 20:38:41 +0000166 // Resolve the arguments in the call to the actual values...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000167 ResolveArguments(Call, FI, OldValMap);
168
Chris Lattner92673292002-11-02 00:13:20 +0000169 // Handle the return value if present...
170 RetVal.mergeWith(Call.getRetVal());
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000171
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000172 // Erase the entry in the Callees vector
173 Callees.erase(Callees.begin()+c--);
174
Chris Lattner9eee58d2002-07-19 18:11:43 +0000175 } else if (FI.getName() == "printf" || FI.getName() == "sscanf" ||
176 FI.getName() == "fprintf" || FI.getName() == "open" ||
177 FI.getName() == "sprintf") {
Chris Lattner92673292002-11-02 00:13:20 +0000178 // FIXME: These special cases (eg printf) should go away when we can
179 // define functions that take a variable number of arguments.
Chris Lattner9eee58d2002-07-19 18:11:43 +0000180
Chris Lattner92673292002-11-02 00:13:20 +0000181 // FIXME: at the very least, this should update mod/ref info
Chris Lattner9eee58d2002-07-19 18:11:43 +0000182 // Erase the entry in the globals vector
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000183 Callees.erase(Callees.begin()+c--);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000184 }
185 }
186
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000187 if (Callees.empty()) { // Inlined all of the function calls?
Chris Lattner0d9bab82002-07-18 00:12:30 +0000188 // Erase the call if it is resolvable...
189 FCs.erase(FCs.begin()+i--); // Don't skip a the next call...
190 Inlined = true;
Chris Lattner0969c502002-10-21 02:08:03 +0000191 } else if (Callees.size() !=
192 Call.getCallee().getNode()->getGlobals().size()) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000193 // Was able to inline SOME, but not all of the functions. Construct a
194 // new global node here.
195 //
196 assert(0 && "Unimpl!");
197 Inlined = true;
198 }
199 }
200 }
201
202 // Recompute the Incomplete markers. If there are any function calls left
203 // now that are complete, we must loop!
204 if (Inlined) {
205 Graph->maskIncompleteMarkers();
206 Graph->markIncompleteNodes();
Chris Lattner55c10582002-10-03 20:38:41 +0000207 Graph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000208 }
209 } while (Inlined && !FCs.empty());
210
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000211 Graph->maskIncompleteMarkers();
212 Graph->markIncompleteNodes();
Chris Lattnera00397e2002-10-03 21:55:28 +0000213 Graph->removeTriviallyDeadNodes(false);
Chris Lattner55c10582002-10-03 20:38:41 +0000214 Graph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000215
Chris Lattner221c9792002-08-07 21:41:11 +0000216 DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
217 << Graph->getGraphSize() << "+" << Graph->getFunctionCalls().size()
218 << "]\n");
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000219
Chris Lattner0d9bab82002-07-18 00:12:30 +0000220 return *Graph;
221}