blob: ade6ca68a5c1be1a42f4c7d3e1bb0a79708b8843 [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
Chris Lattneraa0b4682002-11-09 21:12:07 +000021// run - Calculate the bottom up data structure graphs for each function in the
22// program.
23//
24bool BUDataStructures::run(Module &M) {
25 GlobalsGraph = new DSGraph();
26
27 // Simply calculate the graphs for each function...
28 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
29 if (!I->isExternal())
30 calculateGraph(*I);
31 return false;
32}
Chris Lattner55c10582002-10-03 20:38:41 +000033
Chris Lattner0d9bab82002-07-18 00:12:30 +000034// releaseMemory - If the pass pipeline is done with this pass, we can release
35// our memory... here...
36//
37void BUDataStructures::releaseMemory() {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000038 for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
Chris Lattner0d9bab82002-07-18 00:12:30 +000039 E = DSInfo.end(); I != E; ++I)
40 delete I->second;
41
42 // Empty map so next time memory is released, data structures are not
43 // re-deleted.
44 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +000045 delete GlobalsGraph;
46 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +000047}
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));
Chris Lattneraa0b4682002-11-09 21:12:07 +000058 Graph->setGlobalsGraph(GlobalsGraph);
Chris Lattner0d9bab82002-07-18 00:12:30 +000059
Chris Lattner55c10582002-10-03 20:38:41 +000060#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000061 // Populate the GlobalsGraph with globals from this one.
62 Graph->GlobalsGraph->cloneGlobals(*Graph, /*cloneCalls*/ false);
Chris Lattner55c10582002-10-03 20:38:41 +000063#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000064
Chris Lattner0d9bab82002-07-18 00:12:30 +000065 // Start resolving calls...
Chris Lattner1a948a82002-11-08 21:25:24 +000066 std::vector<DSCallSite> &FCs = Graph->getAuxFunctionCalls();
67
68 // Start with a copy of the original call sites...
69 FCs = Graph->getFunctionCalls();
Chris Lattner0d9bab82002-07-18 00:12:30 +000070
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000071 DEBUG(std::cerr << " [BU] Inlining: " << F.getName() << "\n");
72
Chris Lattner0d9bab82002-07-18 00:12:30 +000073 bool Inlined;
74 do {
75 Inlined = false;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000076
Chris Lattner0d9bab82002-07-18 00:12:30 +000077 for (unsigned i = 0; i != FCs.size(); ++i) {
78 // Copy the call, because inlining graphs may invalidate the FCs vector.
Vikram S. Adve42fd1692002-10-20 18:07:37 +000079 DSCallSite Call = FCs[i];
Chris Lattner0d9bab82002-07-18 00:12:30 +000080
Chris Lattner55c10582002-10-03 20:38:41 +000081 // If the function list is complete...
Chris Lattner0969c502002-10-21 02:08:03 +000082 if ((Call.getCallee().getNode()->NodeType & DSNode::Incomplete)==0) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000083 // Start inlining all of the functions we can... some may not be
84 // inlinable if they are external...
85 //
Chris Lattner7836d602002-10-20 22:11:17 +000086 std::vector<GlobalValue*> Callees =
Chris Lattner0969c502002-10-21 02:08:03 +000087 Call.getCallee().getNode()->getGlobals();
Chris Lattner0d9bab82002-07-18 00:12:30 +000088
89 // Loop over the functions, inlining whatever we can...
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000090 for (unsigned c = 0; c != Callees.size(); ++c) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000091 // Must be a function type, so this cast MUST succeed.
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000092 Function &FI = cast<Function>(*Callees[c]);
Chris Lattner613692c2002-10-17 04:24:08 +000093
Chris Lattner0d9bab82002-07-18 00:12:30 +000094 if (&FI == &F) {
95 // Self recursion... simply link up the formal arguments with the
96 // actual arguments...
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000097 DEBUG(std::cerr << "\t[BU] Self Inlining: " << F.getName() << "\n");
Chris Lattner0d9bab82002-07-18 00:12:30 +000098
Chris Lattner076c1f92002-11-07 06:31:54 +000099 // Handle self recursion by resolving the arguments and return value
Chris Lattner460ea292002-11-07 07:06:20 +0000100 Graph->mergeInGraph(Call, *Graph, DSGraph::StripAllocaBit);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000101
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000102 // Erase the entry in the callees vector
103 Callees.erase(Callees.begin()+c--);
Chris Lattner613692c2002-10-17 04:24:08 +0000104
Chris Lattner0d9bab82002-07-18 00:12:30 +0000105 } else if (!FI.isExternal()) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000106 DEBUG(std::cerr << "\t[BU] In " << F.getName() << " inlining: "
Chris Lattner0d9bab82002-07-18 00:12:30 +0000107 << FI.getName() << "\n");
Vikram S. Advec44e9bf2002-07-18 16:13:52 +0000108
Chris Lattner0d9bab82002-07-18 00:12:30 +0000109 // Get the data structure graph for the called function, closing it
110 // if possible (which is only impossible in the case of mutual
111 // recursion...
112 //
113 DSGraph &GI = calculateGraph(FI); // Graph to inline
114
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000115 DEBUG(std::cerr << "\t\t[BU] Got graph for " << FI.getName()
116 << " in: " << F.getName() << "\n");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000117
Chris Lattner076c1f92002-11-07 06:31:54 +0000118 // Handle self recursion by resolving the arguments and return value
Chris Lattner70925b02002-11-08 22:27:25 +0000119 Graph->mergeInGraph(Call, GI, DSGraph::StripAllocaBit |
120 DSGraph::DontCloneCallNodes);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000121
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000122 // Erase the entry in the Callees vector
123 Callees.erase(Callees.begin()+c--);
124
Chris Lattner9eee58d2002-07-19 18:11:43 +0000125 } else if (FI.getName() == "printf" || FI.getName() == "sscanf" ||
126 FI.getName() == "fprintf" || FI.getName() == "open" ||
127 FI.getName() == "sprintf") {
Chris Lattner92673292002-11-02 00:13:20 +0000128 // FIXME: These special cases (eg printf) should go away when we can
129 // define functions that take a variable number of arguments.
Chris Lattner9eee58d2002-07-19 18:11:43 +0000130
Chris Lattner92673292002-11-02 00:13:20 +0000131 // FIXME: at the very least, this should update mod/ref info
Chris Lattner9eee58d2002-07-19 18:11:43 +0000132 // Erase the entry in the globals vector
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000133 Callees.erase(Callees.begin()+c--);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000134 }
135 }
136
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000137 if (Callees.empty()) { // Inlined all of the function calls?
Chris Lattner0d9bab82002-07-18 00:12:30 +0000138 // Erase the call if it is resolvable...
139 FCs.erase(FCs.begin()+i--); // Don't skip a the next call...
140 Inlined = true;
Chris Lattner0969c502002-10-21 02:08:03 +0000141 } else if (Callees.size() !=
142 Call.getCallee().getNode()->getGlobals().size()) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000143 // Was able to inline SOME, but not all of the functions. Construct a
144 // new global node here.
145 //
146 assert(0 && "Unimpl!");
147 Inlined = true;
148 }
149 }
150 }
151
152 // Recompute the Incomplete markers. If there are any function calls left
153 // now that are complete, we must loop!
154 if (Inlined) {
155 Graph->maskIncompleteMarkers();
156 Graph->markIncompleteNodes();
Chris Lattner65f28972002-11-09 21:00:49 +0000157 Graph->removeDeadNodes(/*KeepAllGlobals*/ true);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000158 }
159 } while (Inlined && !FCs.empty());
160
Chris Lattner221c9792002-08-07 21:41:11 +0000161 DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
162 << Graph->getGraphSize() << "+" << Graph->getFunctionCalls().size()
163 << "]\n");
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000164
Chris Lattner0d9bab82002-07-18 00:12:30 +0000165 return *Graph;
166}