blob: ed9336d2b73a76ebd3a3b7b37a09bb622df0357a [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 Lattnerb1060432002-11-07 05:20:53 +000019using namespace DS;
Chris Lattner55c10582002-10-03 20:38:41 +000020
21
Chris Lattner0d9bab82002-07-18 00:12:30 +000022// releaseMemory - If the pass pipeline is done with this pass, we can release
23// our memory... here...
24//
25void BUDataStructures::releaseMemory() {
Chris Lattner613692c2002-10-17 04:24:08 +000026 // Delete all call site information
27 CallSites.clear();
28
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000029 for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
Chris Lattner0d9bab82002-07-18 00:12:30 +000030 E = DSInfo.end(); I != E; ++I)
31 delete I->second;
32
33 // Empty map so next time memory is released, data structures are not
34 // re-deleted.
35 DSInfo.clear();
36}
37
38// run - Calculate the bottom up data structure graphs for each function in the
39// program.
40//
41bool BUDataStructures::run(Module &M) {
42 // Simply calculate the graphs for each function...
43 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
44 if (!I->isExternal())
45 calculateGraph(*I);
46 return false;
47}
48
Chris Lattner0d9bab82002-07-18 00:12:30 +000049DSGraph &BUDataStructures::calculateGraph(Function &F) {
50 // Make sure this graph has not already been calculated, or that we don't get
51 // into an infinite loop with mutually recursive functions.
52 //
53 DSGraph *&Graph = DSInfo[&F];
54 if (Graph) return *Graph;
55
56 // Copy the local version into DSInfo...
57 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(F));
58
Chris Lattner55c10582002-10-03 20:38:41 +000059#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000060 // Populate the GlobalsGraph with globals from this one.
61 Graph->GlobalsGraph->cloneGlobals(*Graph, /*cloneCalls*/ false);
Chris Lattner55c10582002-10-03 20:38:41 +000062#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000063
Chris Lattner0d9bab82002-07-18 00:12:30 +000064 // Start resolving calls...
Vikram S. Adve42fd1692002-10-20 18:07:37 +000065 std::vector<DSCallSite> &FCs = Graph->getFunctionCalls();
Chris Lattner0d9bab82002-07-18 00:12:30 +000066
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000067 DEBUG(std::cerr << " [BU] Inlining: " << F.getName() << "\n");
68
Chris Lattner0d9bab82002-07-18 00:12:30 +000069 bool Inlined;
70 do {
71 Inlined = false;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000072
Chris Lattner0d9bab82002-07-18 00:12:30 +000073 for (unsigned i = 0; i != FCs.size(); ++i) {
74 // Copy the call, because inlining graphs may invalidate the FCs vector.
Vikram S. Adve42fd1692002-10-20 18:07:37 +000075 DSCallSite Call = FCs[i];
Chris Lattner0d9bab82002-07-18 00:12:30 +000076
Chris Lattner55c10582002-10-03 20:38:41 +000077 // If the function list is complete...
Chris Lattner0969c502002-10-21 02:08:03 +000078 if ((Call.getCallee().getNode()->NodeType & DSNode::Incomplete)==0) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000079 // Start inlining all of the functions we can... some may not be
80 // inlinable if they are external...
81 //
Chris Lattner7836d602002-10-20 22:11:17 +000082 std::vector<GlobalValue*> Callees =
Chris Lattner0969c502002-10-21 02:08:03 +000083 Call.getCallee().getNode()->getGlobals();
Chris Lattner0d9bab82002-07-18 00:12:30 +000084
85 // Loop over the functions, inlining whatever we can...
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000086 for (unsigned c = 0; c != Callees.size(); ++c) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000087 // Must be a function type, so this cast MUST succeed.
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000088 Function &FI = cast<Function>(*Callees[c]);
Chris Lattner613692c2002-10-17 04:24:08 +000089
Chris Lattner0d9bab82002-07-18 00:12:30 +000090 if (&FI == &F) {
91 // Self recursion... simply link up the formal arguments with the
92 // actual arguments...
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000093 DEBUG(std::cerr << "\t[BU] Self Inlining: " << F.getName() << "\n");
Chris Lattner0d9bab82002-07-18 00:12:30 +000094
Chris Lattner076c1f92002-11-07 06:31:54 +000095 // Handle self recursion by resolving the arguments and return value
Chris Lattner460ea292002-11-07 07:06:20 +000096 Graph->mergeInGraph(Call, *Graph, DSGraph::StripAllocaBit);
Chris Lattner0d9bab82002-07-18 00:12:30 +000097
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000098 // Erase the entry in the callees vector
99 Callees.erase(Callees.begin()+c--);
Chris Lattner613692c2002-10-17 04:24:08 +0000100
Chris Lattner0d9bab82002-07-18 00:12:30 +0000101 } else if (!FI.isExternal()) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000102 DEBUG(std::cerr << "\t[BU] In " << F.getName() << " inlining: "
Chris Lattner0d9bab82002-07-18 00:12:30 +0000103 << FI.getName() << "\n");
Vikram S. Advec44e9bf2002-07-18 16:13:52 +0000104
Chris Lattner0d9bab82002-07-18 00:12:30 +0000105 // Get the data structure graph for the called function, closing it
106 // if possible (which is only impossible in the case of mutual
107 // recursion...
108 //
109 DSGraph &GI = calculateGraph(FI); // Graph to inline
110
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000111 DEBUG(std::cerr << "\t\t[BU] Got graph for " << FI.getName()
112 << " in: " << F.getName() << "\n");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000113
Vikram S. Adve26b98262002-10-20 21:41:02 +0000114 // Record that the original DSCallSite was a call site of FI.
115 // This may or may not have been known when the DSCallSite was
116 // originally created.
Chris Lattner198be222002-10-21 19:47:18 +0000117 std::vector<DSCallSite> &CallSitesForFunc = CallSites[&FI];
118 CallSitesForFunc.push_back(Call);
119 CallSitesForFunc.back().setResolvingCaller(&F);
Chris Lattner9faf18d2002-10-22 15:58:46 +0000120 CallSitesForFunc.back().setCallee(0);
Chris Lattner7a0b5bb2002-11-02 00:26:32 +0000121
Chris Lattner076c1f92002-11-07 06:31:54 +0000122 // Handle self recursion by resolving the arguments and return value
Chris Lattner460ea292002-11-07 07:06:20 +0000123 Graph->mergeInGraph(Call, GI, DSGraph::StripAllocaBit);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000124
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000125 // Erase the entry in the Callees vector
126 Callees.erase(Callees.begin()+c--);
127
Chris Lattner9eee58d2002-07-19 18:11:43 +0000128 } else if (FI.getName() == "printf" || FI.getName() == "sscanf" ||
129 FI.getName() == "fprintf" || FI.getName() == "open" ||
130 FI.getName() == "sprintf") {
Chris Lattner92673292002-11-02 00:13:20 +0000131 // FIXME: These special cases (eg printf) should go away when we can
132 // define functions that take a variable number of arguments.
Chris Lattner9eee58d2002-07-19 18:11:43 +0000133
Chris Lattner92673292002-11-02 00:13:20 +0000134 // FIXME: at the very least, this should update mod/ref info
Chris Lattner9eee58d2002-07-19 18:11:43 +0000135 // Erase the entry in the globals vector
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000136 Callees.erase(Callees.begin()+c--);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000137 }
138 }
139
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000140 if (Callees.empty()) { // Inlined all of the function calls?
Chris Lattner0d9bab82002-07-18 00:12:30 +0000141 // Erase the call if it is resolvable...
142 FCs.erase(FCs.begin()+i--); // Don't skip a the next call...
143 Inlined = true;
Chris Lattner0969c502002-10-21 02:08:03 +0000144 } else if (Callees.size() !=
145 Call.getCallee().getNode()->getGlobals().size()) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000146 // Was able to inline SOME, but not all of the functions. Construct a
147 // new global node here.
148 //
149 assert(0 && "Unimpl!");
150 Inlined = true;
151 }
152 }
153 }
154
155 // Recompute the Incomplete markers. If there are any function calls left
156 // now that are complete, we must loop!
157 if (Inlined) {
158 Graph->maskIncompleteMarkers();
159 Graph->markIncompleteNodes();
Chris Lattner55c10582002-10-03 20:38:41 +0000160 Graph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000161 }
162 } while (Inlined && !FCs.empty());
163
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000164 Graph->maskIncompleteMarkers();
165 Graph->markIncompleteNodes();
Chris Lattnera00397e2002-10-03 21:55:28 +0000166 Graph->removeTriviallyDeadNodes(false);
Chris Lattner55c10582002-10-03 20:38:41 +0000167 Graph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000168
Chris Lattner221c9792002-08-07 21:41:11 +0000169 DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
170 << Graph->getGraphSize() << "+" << Graph->getFunctionCalls().size()
171 << "]\n");
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000172
Chris Lattner0d9bab82002-07-18 00:12:30 +0000173 return *Graph;
174}