blob: aea113d97dde6f97430992fd69468c96146f1388 [file] [log] [blame]
Chris Lattner95724a42003-11-13 01:43:00 +00001//===- CompleteBottomUp.cpp - Complete Bottom-Up Data Structure Graphs ----===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
Chris Lattner95724a42003-11-13 01:43:00 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
Chris Lattner95724a42003-11-13 01:43:00 +00008//===----------------------------------------------------------------------===//
9//
10// This is the exact same as the bottom-up graphs, but we use take a completed
11// call graph and inline all indirect callees into their callers graphs, making
12// the result more useful for things like pool allocation.
13//
14//===----------------------------------------------------------------------===//
15
Chris Lattner021decc2005-04-02 19:17:18 +000016#define DEBUG_TYPE "cbudatastructure"
Chris Lattner4dabb2c2004-07-07 06:32:21 +000017#include "llvm/Analysis/DataStructure/DataStructure.h"
Chris Lattner95724a42003-11-13 01:43:00 +000018#include "llvm/Module.h"
Chris Lattner4dabb2c2004-07-07 06:32:21 +000019#include "llvm/Analysis/DataStructure/DSGraph.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/Support/Debug.h"
21#include "llvm/ADT/SCCIterator.h"
22#include "llvm/ADT/Statistic.h"
23#include "llvm/ADT/STLExtras.h"
Chris Lattner95724a42003-11-13 01:43:00 +000024using namespace llvm;
25
26namespace {
Chris Lattner5d8925c2006-08-27 22:30:17 +000027 RegisterPass<CompleteBUDataStructures>
Chris Lattner95724a42003-11-13 01:43:00 +000028 X("cbudatastructure", "'Complete' Bottom-up Data Structure Analysis");
Chris Lattnerda5c5a52004-03-04 19:16:35 +000029 Statistic<> NumCBUInlines("cbudatastructures", "Number of graphs inlined");
Chris Lattner95724a42003-11-13 01:43:00 +000030}
31
Chris Lattner95724a42003-11-13 01:43:00 +000032
33// run - Calculate the bottom up data structure graphs for each function in the
34// program.
35//
Chris Lattnerb12914b2004-09-20 04:48:05 +000036bool CompleteBUDataStructures::runOnModule(Module &M) {
Chris Lattner95724a42003-11-13 01:43:00 +000037 BUDataStructures &BU = getAnalysis<BUDataStructures>();
Chris Lattnercc1f2452005-04-23 21:11:05 +000038 GlobalECs = BU.getGlobalECs();
Chris Lattnerf4f62272005-03-19 22:23:45 +000039 GlobalsGraph = new DSGraph(BU.getGlobalsGraph(), GlobalECs);
Chris Lattner95724a42003-11-13 01:43:00 +000040 GlobalsGraph->setPrintAuxCalls();
41
Vikram S. Adve052682f2004-05-23 08:00:34 +000042 // Our call graph is the same as the BU data structures call graph
43 ActualCallees = BU.getActualCallees();
Chris Lattner95724a42003-11-13 01:43:00 +000044
Chris Lattner79390d42003-11-13 05:05:41 +000045 std::vector<DSGraph*> Stack;
46 hash_map<DSGraph*, unsigned> ValMap;
47 unsigned NextID = 1;
Chris Lattner95724a42003-11-13 01:43:00 +000048
Chris Lattnera66e3532005-03-13 20:15:06 +000049 Function *MainFunc = M.getMainFunction();
50 if (MainFunc) {
51 if (!MainFunc->isExternal())
52 calculateSCCGraphs(getOrCreateGraph(*MainFunc), Stack, NextID, ValMap);
Chris Lattner79390d42003-11-13 05:05:41 +000053 } else {
Bill Wendling5294fb02006-11-17 07:33:59 +000054 DOUT << "CBU-DSA: No 'main' function found!\n";
Chris Lattner79390d42003-11-13 05:05:41 +000055 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000056
Chris Lattner79390d42003-11-13 05:05:41 +000057 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Andrew Lenharthc269c522006-06-16 14:43:36 +000058 if (!I->isExternal() && !DSInfo.count(I)) {
Andrew Lenharth7445ea62006-10-13 17:38:22 +000059 if (MainFunc) {
Bill Wendling5294fb02006-11-17 07:33:59 +000060 DOUT << "*** CBU: Function unreachable from main: "
61 << I->getName() << "\n";
Andrew Lenharth7445ea62006-10-13 17:38:22 +000062 }
Chris Lattner79390d42003-11-13 05:05:41 +000063 calculateSCCGraphs(getOrCreateGraph(*I), Stack, NextID, ValMap);
Andrew Lenharthc269c522006-06-16 14:43:36 +000064 }
Chris Lattner95724a42003-11-13 01:43:00 +000065
Chris Lattner2dea8d62004-02-08 01:53:10 +000066 GlobalsGraph->removeTriviallyDeadNodes();
Chris Lattnera66e3532005-03-13 20:15:06 +000067
68
69 // Merge the globals variables (not the calls) from the globals graph back
70 // into the main function's graph so that the main function contains all of
71 // the information about global pools and GV usage in the program.
Chris Lattner49e88e82005-03-15 22:10:04 +000072 if (MainFunc && !MainFunc->isExternal()) {
Chris Lattnera66e3532005-03-13 20:15:06 +000073 DSGraph &MainGraph = getOrCreateGraph(*MainFunc);
74 const DSGraph &GG = *MainGraph.getGlobalsGraph();
Misha Brukman2b37d7c2005-04-21 21:13:18 +000075 ReachabilityCloner RC(MainGraph, GG,
Chris Lattnera66e3532005-03-13 20:15:06 +000076 DSGraph::DontCloneCallNodes |
77 DSGraph::DontCloneAuxCallNodes);
78
79 // Clone the global nodes into this graph.
80 for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(),
81 E = GG.getScalarMap().global_end(); I != E; ++I)
82 if (isa<GlobalVariable>(*I))
83 RC.getClonedNH(GG.getNodeForValue(*I));
84
Chris Lattner270cf502005-03-13 20:32:26 +000085 MainGraph.maskIncompleteMarkers();
Misha Brukman2b37d7c2005-04-21 21:13:18 +000086 MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
Chris Lattnera66e3532005-03-13 20:15:06 +000087 DSGraph::IgnoreGlobals);
88 }
89
Chris Lattner95724a42003-11-13 01:43:00 +000090 return false;
91}
Chris Lattner79390d42003-11-13 05:05:41 +000092
93DSGraph &CompleteBUDataStructures::getOrCreateGraph(Function &F) {
94 // Has the graph already been created?
95 DSGraph *&Graph = DSInfo[&F];
96 if (Graph) return *Graph;
97
98 // Copy the BU graph...
Chris Lattnerf4f62272005-03-19 22:23:45 +000099 Graph = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F), GlobalECs);
Chris Lattner79390d42003-11-13 05:05:41 +0000100 Graph->setGlobalsGraph(GlobalsGraph);
101 Graph->setPrintAuxCalls();
102
103 // Make sure to update the DSInfo map for all of the functions currently in
104 // this graph!
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000105 for (DSGraph::retnodes_iterator I = Graph->retnodes_begin();
106 I != Graph->retnodes_end(); ++I)
Chris Lattner79390d42003-11-13 05:05:41 +0000107 DSInfo[I->first] = Graph;
108
109 return *Graph;
110}
111
112
113
114unsigned CompleteBUDataStructures::calculateSCCGraphs(DSGraph &FG,
115 std::vector<DSGraph*> &Stack,
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000116 unsigned &NextID,
Chris Lattner79390d42003-11-13 05:05:41 +0000117 hash_map<DSGraph*, unsigned> &ValMap) {
118 assert(!ValMap.count(&FG) && "Shouldn't revisit functions!");
119 unsigned Min = NextID++, MyID = Min;
120 ValMap[&FG] = Min;
121 Stack.push_back(&FG);
122
123 // The edges out of the current node are the call site targets...
Chris Lattnerf9aace22005-01-31 00:10:58 +0000124 for (DSGraph::fc_iterator CI = FG.fc_begin(), CE = FG.fc_end();
125 CI != CE; ++CI) {
Chris Lattnera9548d92005-01-30 23:51:02 +0000126 Instruction *Call = CI->getCallSite().getInstruction();
Chris Lattner79390d42003-11-13 05:05:41 +0000127
128 // Loop over all of the actually called functions...
Chris Lattner2ccc5f12005-04-02 20:02:41 +0000129 callee_iterator I = callee_begin(Call), E = callee_end(Call);
Chris Lattner021decc2005-04-02 19:17:18 +0000130 for (; I != E && I->first == Call; ++I) {
131 assert(I->first == Call && "Bad callee construction!");
Chris Lattnera366c982003-11-13 18:48:11 +0000132 if (!I->second->isExternal()) {
133 DSGraph &Callee = getOrCreateGraph(*I->second);
134 unsigned M;
135 // Have we visited the destination function yet?
136 hash_map<DSGraph*, unsigned>::iterator It = ValMap.find(&Callee);
137 if (It == ValMap.end()) // No, visit it now.
138 M = calculateSCCGraphs(Callee, Stack, NextID, ValMap);
139 else // Yes, get it's number.
140 M = It->second;
141 if (M < Min) Min = M;
142 }
Chris Lattner021decc2005-04-02 19:17:18 +0000143 }
Chris Lattner79390d42003-11-13 05:05:41 +0000144 }
145
146 assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!");
147 if (Min != MyID)
148 return Min; // This is part of a larger SCC!
149
150 // If this is a new SCC, process it now.
151 bool IsMultiNodeSCC = false;
152 while (Stack.back() != &FG) {
153 DSGraph *NG = Stack.back();
154 ValMap[NG] = ~0U;
155
Chris Lattnera2197132005-03-22 00:36:51 +0000156 FG.cloneInto(*NG);
Chris Lattner79390d42003-11-13 05:05:41 +0000157
158 // Update the DSInfo map and delete the old graph...
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000159 for (DSGraph::retnodes_iterator I = NG->retnodes_begin();
160 I != NG->retnodes_end(); ++I)
Chris Lattner79390d42003-11-13 05:05:41 +0000161 DSInfo[I->first] = &FG;
Chris Lattnera1c972d2004-10-07 20:01:31 +0000162
163 // Remove NG from the ValMap since the pointer may get recycled.
164 ValMap.erase(NG);
Chris Lattner79390d42003-11-13 05:05:41 +0000165 delete NG;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000166
Chris Lattner79390d42003-11-13 05:05:41 +0000167 Stack.pop_back();
168 IsMultiNodeSCC = true;
169 }
170
171 // Clean up the graph before we start inlining a bunch again...
172 if (IsMultiNodeSCC)
173 FG.removeTriviallyDeadNodes();
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000174
Chris Lattner79390d42003-11-13 05:05:41 +0000175 Stack.pop_back();
176 processGraph(FG);
177 ValMap[&FG] = ~0U;
178 return MyID;
179}
180
181
182/// processGraph - Process the BU graphs for the program in bottom-up order on
183/// the SCC of the __ACTUAL__ call graph. This builds "complete" BU graphs.
184void CompleteBUDataStructures::processGraph(DSGraph &G) {
Chris Lattnerda5c5a52004-03-04 19:16:35 +0000185 hash_set<Instruction*> calls;
Chris Lattnerd10b5fd2004-02-20 23:52:15 +0000186
Chris Lattner79390d42003-11-13 05:05:41 +0000187 // The edges out of the current node are the call site targets...
Chris Lattnera9548d92005-01-30 23:51:02 +0000188 unsigned i = 0;
Chris Lattnerf9aace22005-01-31 00:10:58 +0000189 for (DSGraph::fc_iterator CI = G.fc_begin(), CE = G.fc_end(); CI != CE;
Chris Lattnera9548d92005-01-30 23:51:02 +0000190 ++CI, ++i) {
191 const DSCallSite &CS = *CI;
Chris Lattner79390d42003-11-13 05:05:41 +0000192 Instruction *TheCall = CS.getCallSite().getInstruction();
193
Chris Lattnerda5c5a52004-03-04 19:16:35 +0000194 assert(calls.insert(TheCall).second &&
195 "Call instruction occurs multiple times in graph??");
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000196
Chris Lattner5021b8c2005-03-18 23:19:47 +0000197 // Fast path for noop calls. Note that we don't care about merging globals
198 // in the callee with nodes in the caller here.
199 if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0)
200 continue;
Chris Lattnerda5c5a52004-03-04 19:16:35 +0000201
Vikram S. Adve052682f2004-05-23 08:00:34 +0000202 // Loop over all of the potentially called functions...
203 // Inline direct calls as well as indirect calls because the direct
204 // callee may have indirect callees and so may have changed.
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000205 //
Chris Lattner2ccc5f12005-04-02 20:02:41 +0000206 callee_iterator I = callee_begin(TheCall),E = callee_end(TheCall);
Chris Lattner021decc2005-04-02 19:17:18 +0000207 unsigned TNum = 0, Num = 0;
208 DEBUG(Num = std::distance(I, E));
Vikram S. Adve052682f2004-05-23 08:00:34 +0000209 for (; I != E; ++I, ++TNum) {
Chris Lattner021decc2005-04-02 19:17:18 +0000210 assert(I->first == TheCall && "Bad callee construction!");
Vikram S. Adve052682f2004-05-23 08:00:34 +0000211 Function *CalleeFunc = I->second;
212 if (!CalleeFunc->isExternal()) {
213 // Merge the callee's graph into this graph. This works for normal
214 // calls or for self recursion within an SCC.
215 DSGraph &GI = getOrCreateGraph(*CalleeFunc);
216 ++NumCBUInlines;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000217 G.mergeInGraph(CS, *CalleeFunc, GI,
Vikram S. Adve052682f2004-05-23 08:00:34 +0000218 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes |
219 DSGraph::DontCloneAuxCallNodes);
Bill Wendling5294fb02006-11-17 07:33:59 +0000220 DOUT << " Inlining graph [" << i << "/"
221 << G.getFunctionCalls().size()-1
222 << ":" << TNum << "/" << Num-1 << "] for "
223 << CalleeFunc->getName() << "["
224 << GI.getGraphSize() << "+" << GI.getAuxFunctionCalls().size()
225 << "] into '" /*<< G.getFunctionNames()*/ << "' ["
226 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
227 << "]\n";
Chris Lattner79390d42003-11-13 05:05:41 +0000228 }
229 }
230 }
231
Chris Lattner79390d42003-11-13 05:05:41 +0000232 // Recompute the Incomplete markers
233 G.maskIncompleteMarkers();
234 G.markIncompleteNodes(DSGraph::MarkFormalArgs);
235
236 // Delete dead nodes. Treat globals that are unreachable but that can
237 // reach live nodes as live.
Chris Lattner4ab483c2004-03-04 21:36:57 +0000238 G.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner79390d42003-11-13 05:05:41 +0000239}