blob: e65a06c6e6dc15e6f2c4a8443b2731d85519c047 [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 Lattner55c10582002-10-03 20:38:41 +000019// TODO: FIXME
20namespace DataStructureAnalysis {
21 // isPointerType - Return true if this first class type is big enough to hold
22 // a pointer.
23 //
24 bool isPointerType(const Type *Ty);
25}
26using namespace DataStructureAnalysis;
27
28
Chris Lattner0d9bab82002-07-18 00:12:30 +000029// releaseMemory - If the pass pipeline is done with this pass, we can release
30// our memory... here...
31//
32void BUDataStructures::releaseMemory() {
Chris Lattner613692c2002-10-17 04:24:08 +000033 // Delete all call site information
34 CallSites.clear();
35
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000036 for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
Chris Lattner0d9bab82002-07-18 00:12:30 +000037 E = DSInfo.end(); I != E; ++I)
38 delete I->second;
39
40 // Empty map so next time memory is released, data structures are not
41 // re-deleted.
42 DSInfo.clear();
43}
44
45// run - Calculate the bottom up data structure graphs for each function in the
46// program.
47//
48bool BUDataStructures::run(Module &M) {
49 // Simply calculate the graphs for each function...
50 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
51 if (!I->isExternal())
52 calculateGraph(*I);
53 return false;
54}
55
Chris Lattner0d9bab82002-07-18 00:12:30 +000056// ResolveArguments - Resolve the formal and actual arguments for a function
57// call.
58//
Vikram S. Adve42fd1692002-10-20 18:07:37 +000059static void ResolveArguments(DSCallSite &Call, Function &F,
Chris Lattner0d9bab82002-07-18 00:12:30 +000060 map<Value*, DSNodeHandle> &ValueMap) {
61 // Resolve all of the function arguments...
62 Function::aiterator AI = F.abegin();
63 for (unsigned i = 2, e = Call.size(); i != e; ++i) {
64 // Advance the argument iterator to the first pointer argument...
Chris Lattner55c10582002-10-03 20:38:41 +000065 while (!isPointerType(AI->getType())) ++AI;
Chris Lattner0d9bab82002-07-18 00:12:30 +000066
67 // Add the link from the argument scalar to the provided value
Chris Lattner55c10582002-10-03 20:38:41 +000068 DSNodeHandle &NN = ValueMap[AI];
69 NN.addEdgeTo(Call[i]);
Chris Lattner0d9bab82002-07-18 00:12:30 +000070 ++AI;
71 }
72}
73
Chris Lattner0d9bab82002-07-18 00:12:30 +000074DSGraph &BUDataStructures::calculateGraph(Function &F) {
75 // Make sure this graph has not already been calculated, or that we don't get
76 // into an infinite loop with mutually recursive functions.
77 //
78 DSGraph *&Graph = DSInfo[&F];
79 if (Graph) return *Graph;
80
81 // Copy the local version into DSInfo...
82 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(F));
83
Chris Lattner55c10582002-10-03 20:38:41 +000084#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000085 // Populate the GlobalsGraph with globals from this one.
86 Graph->GlobalsGraph->cloneGlobals(*Graph, /*cloneCalls*/ false);
Chris Lattner55c10582002-10-03 20:38:41 +000087#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000088
Chris Lattner0d9bab82002-07-18 00:12:30 +000089 // Start resolving calls...
Vikram S. Adve42fd1692002-10-20 18:07:37 +000090 std::vector<DSCallSite> &FCs = Graph->getFunctionCalls();
Chris Lattner0d9bab82002-07-18 00:12:30 +000091
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000092 DEBUG(std::cerr << " [BU] Inlining: " << F.getName() << "\n");
93
Chris Lattner0d9bab82002-07-18 00:12:30 +000094 bool Inlined;
95 do {
96 Inlined = false;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000097
Chris Lattner0d9bab82002-07-18 00:12:30 +000098 for (unsigned i = 0; i != FCs.size(); ++i) {
99 // Copy the call, because inlining graphs may invalidate the FCs vector.
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000100 DSCallSite Call = FCs[i];
Chris Lattner0d9bab82002-07-18 00:12:30 +0000101
Chris Lattner55c10582002-10-03 20:38:41 +0000102 // If the function list is complete...
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000103 if ((Call.getCalleeNode().getNode()->NodeType & DSNode::Incomplete)==0) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000104 // Start inlining all of the functions we can... some may not be
105 // inlinable if they are external...
106 //
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000107 std::vector<GlobalValue*> Callees(Call.getCalleeNode().getNode()->getGlobals());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000108
109 // Loop over the functions, inlining whatever we can...
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000110 for (unsigned c = 0; c != Callees.size(); ++c) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000111 // Must be a function type, so this cast MUST succeed.
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000112 Function &FI = cast<Function>(*Callees[c]);
Chris Lattner613692c2002-10-17 04:24:08 +0000113
114 // Record that this is a call site of FI.
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000115 assert(&Call.getCaller() == &F && "Invalid caller in DSCallSite?");
116 CallSites[&FI].push_back(DSCallSite(Call));
Chris Lattner613692c2002-10-17 04:24:08 +0000117
Chris Lattner0d9bab82002-07-18 00:12:30 +0000118 if (&FI == &F) {
119 // Self recursion... simply link up the formal arguments with the
120 // actual arguments...
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000121
122 DEBUG(std::cerr << "\t[BU] Self Inlining: " << F.getName() << "\n");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000123
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000124 if (Call.getReturnValueNode().getNode()) // Handle the return value if present...
125 Graph->getRetNode().mergeWith(Call.getReturnValueNode());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000126
127 // Resolve the arguments in the call to the actual values...
128 ResolveArguments(Call, F, Graph->getValueMap());
129
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. Advec44e9bf2002-07-18 16:13:52 +0000146 // Clone the callee's graph into the current graph, keeping
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000147 // track of where scalars in the old graph _used_ to point,
148 // and of the new nodes matching nodes of the old graph.
Chris Lattner55c10582002-10-03 20:38:41 +0000149 map<Value*, DSNodeHandle> OldValMap;
150 map<const DSNode*, DSNode*> OldNodeMap;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000151
152 // The clone call may invalidate any of the vectors in the data
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000153 // structure graph. Strip locals and don't copy the list of callers
Chris Lattner55c10582002-10-03 20:38:41 +0000154 DSNodeHandle RetVal = Graph->cloneInto(GI, OldValMap, OldNodeMap,
155 /*StripScalars*/ true,
156 /*StripAllocas*/ true,
157 /*CopyCallers*/ false,
158 /*CopyOrigCalls*/ false);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000159
Chris Lattner55c10582002-10-03 20:38:41 +0000160 // Resolve the arguments in the call to the actual values...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000161 ResolveArguments(Call, FI, OldValMap);
162
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000163 if (Call.getReturnValueNode().getNode()) // Handle the return value if present
164 RetVal.mergeWith(Call.getReturnValueNode());
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000165
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000166 // Erase the entry in the Callees vector
167 Callees.erase(Callees.begin()+c--);
168
Chris Lattner9eee58d2002-07-19 18:11:43 +0000169 } else if (FI.getName() == "printf" || FI.getName() == "sscanf" ||
170 FI.getName() == "fprintf" || FI.getName() == "open" ||
171 FI.getName() == "sprintf") {
172
173 // Erase the entry in the globals vector
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000174 Callees.erase(Callees.begin()+c--);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000175 }
176 }
177
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000178 if (Callees.empty()) { // Inlined all of the function calls?
Chris Lattner0d9bab82002-07-18 00:12:30 +0000179 // Erase the call if it is resolvable...
180 FCs.erase(FCs.begin()+i--); // Don't skip a the next call...
181 Inlined = true;
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000182 } else if (Callees.size() != Call.getCalleeNode().getNode()->getGlobals().size()) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000183 // Was able to inline SOME, but not all of the functions. Construct a
184 // new global node here.
185 //
186 assert(0 && "Unimpl!");
187 Inlined = true;
188 }
189 }
190 }
191
192 // Recompute the Incomplete markers. If there are any function calls left
193 // now that are complete, we must loop!
194 if (Inlined) {
195 Graph->maskIncompleteMarkers();
196 Graph->markIncompleteNodes();
Chris Lattner55c10582002-10-03 20:38:41 +0000197 Graph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000198 }
199 } while (Inlined && !FCs.empty());
200
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000201 Graph->maskIncompleteMarkers();
202 Graph->markIncompleteNodes();
Chris Lattnera00397e2002-10-03 21:55:28 +0000203 Graph->removeTriviallyDeadNodes(false);
Chris Lattner55c10582002-10-03 20:38:41 +0000204 Graph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000205
Chris Lattner221c9792002-08-07 21:41:11 +0000206 DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
207 << Graph->getGraphSize() << "+" << Graph->getFunctionCalls().size()
208 << "]\n");
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000209
Chris Lattner0d9bab82002-07-18 00:12:30 +0000210 return *Graph;
211}