blob: 1c9e3299d02945205e8a09d200ed38e557658893 [file] [log] [blame]
Chris Lattner95724a42003-11-13 01:43:00 +00001//===- CompleteBottomUp.cpp - Complete Bottom-Up Data Structure Graphs ----===//
2//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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 Lattner4dabb2c2004-07-07 06:32:21 +000016#include "llvm/Analysis/DataStructure/DataStructure.h"
Chris Lattner95724a42003-11-13 01:43:00 +000017#include "llvm/Module.h"
Chris Lattner4dabb2c2004-07-07 06:32:21 +000018#include "llvm/Analysis/DataStructure/DSGraph.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/Support/Debug.h"
20#include "llvm/ADT/SCCIterator.h"
21#include "llvm/ADT/Statistic.h"
22#include "llvm/ADT/STLExtras.h"
Chris Lattner95724a42003-11-13 01:43:00 +000023using namespace llvm;
24
25namespace {
26 RegisterAnalysis<CompleteBUDataStructures>
27 X("cbudatastructure", "'Complete' Bottom-up Data Structure Analysis");
Chris Lattnerda5c5a52004-03-04 19:16:35 +000028 Statistic<> NumCBUInlines("cbudatastructures", "Number of graphs inlined");
Chris Lattner95724a42003-11-13 01:43:00 +000029}
30
Chris Lattner95724a42003-11-13 01:43:00 +000031
32// run - Calculate the bottom up data structure graphs for each function in the
33// program.
34//
Chris Lattnerb12914b2004-09-20 04:48:05 +000035bool CompleteBUDataStructures::runOnModule(Module &M) {
Chris Lattner95724a42003-11-13 01:43:00 +000036 BUDataStructures &BU = getAnalysis<BUDataStructures>();
37 GlobalsGraph = new DSGraph(BU.getGlobalsGraph());
38 GlobalsGraph->setPrintAuxCalls();
39
Chris Lattner95724a42003-11-13 01:43:00 +000040#if 1 // REMOVE ME EVENTUALLY
41 // FIXME: TEMPORARY (remove once finalization of indirect call sites in the
42 // globals graph has been implemented in the BU pass)
43 TDDataStructures &TD = getAnalysis<TDDataStructures>();
44
Vikram S. Adve052682f2004-05-23 08:00:34 +000045 ActualCallees.clear();
46
Chris Lattner95724a42003-11-13 01:43:00 +000047 // The call graph extractable from the TD pass is _much more complete_ and
48 // trustable than that generated by the BU pass so far. Until this is fixed,
49 // we hack it like this:
50 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) {
51 if (MI->isExternal()) continue;
Chris Lattnera9548d92005-01-30 23:51:02 +000052 const std::list<DSCallSite> &CSs = TD.getDSGraph(*MI).getFunctionCalls();
Chris Lattner95724a42003-11-13 01:43:00 +000053
Chris Lattnera9548d92005-01-30 23:51:02 +000054 for (std::list<DSCallSite>::const_iterator CSI = CSs.begin(), E = CSs.end();
55 CSI != E; ++CSI) {
56 Instruction *TheCall = CSI->getCallSite().getInstruction();
Chris Lattner95724a42003-11-13 01:43:00 +000057
Chris Lattnera9548d92005-01-30 23:51:02 +000058 if (CSI->isIndirectCall()) { // indirect call: insert all callees
Chris Lattner95724a42003-11-13 01:43:00 +000059 const std::vector<GlobalValue*> &Callees =
Chris Lattnera9548d92005-01-30 23:51:02 +000060 CSI->getCalleeNode()->getGlobals();
Chris Lattner95724a42003-11-13 01:43:00 +000061 for (unsigned i = 0, e = Callees.size(); i != e; ++i)
62 if (Function *F = dyn_cast<Function>(Callees[i]))
63 ActualCallees.insert(std::make_pair(TheCall, F));
Vikram S. Adve052682f2004-05-23 08:00:34 +000064 } else { // direct call: insert the single callee directly
65 ActualCallees.insert(std::make_pair(TheCall,
Chris Lattnera9548d92005-01-30 23:51:02 +000066 CSI->getCalleeFunc()));
Chris Lattner95724a42003-11-13 01:43:00 +000067 }
68 }
69 }
Vikram S. Adve052682f2004-05-23 08:00:34 +000070#else
71 // Our call graph is the same as the BU data structures call graph
72 ActualCallees = BU.getActualCallees();
Chris Lattner95724a42003-11-13 01:43:00 +000073#endif
74
Chris Lattner79390d42003-11-13 05:05:41 +000075 std::vector<DSGraph*> Stack;
76 hash_map<DSGraph*, unsigned> ValMap;
77 unsigned NextID = 1;
Chris Lattner95724a42003-11-13 01:43:00 +000078
Chris Lattner79390d42003-11-13 05:05:41 +000079 if (Function *Main = M.getMainFunction()) {
Chris Lattnerdeb87122004-03-05 22:04:07 +000080 if (!Main->isExternal())
81 calculateSCCGraphs(getOrCreateGraph(*Main), Stack, NextID, ValMap);
Chris Lattner79390d42003-11-13 05:05:41 +000082 } else {
83 std::cerr << "CBU-DSA: No 'main' function found!\n";
84 }
85
86 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
87 if (!I->isExternal() && !DSInfo.count(I))
88 calculateSCCGraphs(getOrCreateGraph(*I), Stack, NextID, ValMap);
Chris Lattner95724a42003-11-13 01:43:00 +000089
Chris Lattner2dea8d62004-02-08 01:53:10 +000090 GlobalsGraph->removeTriviallyDeadNodes();
Chris Lattner95724a42003-11-13 01:43:00 +000091 return false;
92}
Chris Lattner79390d42003-11-13 05:05:41 +000093
94DSGraph &CompleteBUDataStructures::getOrCreateGraph(Function &F) {
95 // Has the graph already been created?
96 DSGraph *&Graph = DSInfo[&F];
97 if (Graph) return *Graph;
98
99 // Copy the BU graph...
100 Graph = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F));
101 Graph->setGlobalsGraph(GlobalsGraph);
102 Graph->setPrintAuxCalls();
103
104 // Make sure to update the DSInfo map for all of the functions currently in
105 // this graph!
106 for (DSGraph::ReturnNodesTy::iterator I = Graph->getReturnNodes().begin();
107 I != Graph->getReturnNodes().end(); ++I)
108 DSInfo[I->first] = Graph;
109
110 return *Graph;
111}
112
113
114
115unsigned CompleteBUDataStructures::calculateSCCGraphs(DSGraph &FG,
116 std::vector<DSGraph*> &Stack,
117 unsigned &NextID,
118 hash_map<DSGraph*, unsigned> &ValMap) {
119 assert(!ValMap.count(&FG) && "Shouldn't revisit functions!");
120 unsigned Min = NextID++, MyID = Min;
121 ValMap[&FG] = Min;
122 Stack.push_back(&FG);
123
124 // The edges out of the current node are the call site targets...
Chris Lattnerf9aace22005-01-31 00:10:58 +0000125 for (DSGraph::fc_iterator CI = FG.fc_begin(), CE = FG.fc_end();
126 CI != CE; ++CI) {
Chris Lattnera9548d92005-01-30 23:51:02 +0000127 Instruction *Call = CI->getCallSite().getInstruction();
Chris Lattner79390d42003-11-13 05:05:41 +0000128
129 // Loop over all of the actually called functions...
130 ActualCalleesTy::iterator I, E;
Chris Lattnera366c982003-11-13 18:48:11 +0000131 for (tie(I, E) = ActualCallees.equal_range(Call); I != E; ++I)
132 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 Lattner79390d42003-11-13 05:05:41 +0000143 }
144
145 assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!");
146 if (Min != MyID)
147 return Min; // This is part of a larger SCC!
148
149 // If this is a new SCC, process it now.
150 bool IsMultiNodeSCC = false;
151 while (Stack.back() != &FG) {
152 DSGraph *NG = Stack.back();
153 ValMap[NG] = ~0U;
154
155 DSGraph::NodeMapTy NodeMap;
Chris Lattner825a02a2004-01-27 21:50:41 +0000156 FG.cloneInto(*NG, FG.getScalarMap(), FG.getReturnNodes(), NodeMap);
Chris Lattner79390d42003-11-13 05:05:41 +0000157
158 // Update the DSInfo map and delete the old graph...
159 for (DSGraph::ReturnNodesTy::iterator I = NG->getReturnNodes().begin();
160 I != NG->getReturnNodes().end(); ++I)
161 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;
166
167 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();
174
175 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??");
196
197
Vikram S. Adve052682f2004-05-23 08:00:34 +0000198 // Loop over all of the potentially called functions...
199 // Inline direct calls as well as indirect calls because the direct
200 // callee may have indirect callees and so may have changed.
201 //
202 ActualCalleesTy::iterator I, E;
203 tie(I, E) = ActualCallees.equal_range(TheCall);
204 unsigned TNum = 0, Num = std::distance(I, E);
205 for (; I != E; ++I, ++TNum) {
206 Function *CalleeFunc = I->second;
207 if (!CalleeFunc->isExternal()) {
208 // Merge the callee's graph into this graph. This works for normal
209 // calls or for self recursion within an SCC.
210 DSGraph &GI = getOrCreateGraph(*CalleeFunc);
211 ++NumCBUInlines;
212 G.mergeInGraph(CS, *CalleeFunc, GI, DSGraph::KeepModRefBits |
213 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes |
214 DSGraph::DontCloneAuxCallNodes);
Chris Lattnera9548d92005-01-30 23:51:02 +0000215 DEBUG(std::cerr << " Inlining graph [" << i << "/"
216 << G.getFunctionCalls().size()-1
Vikram S. Adve052682f2004-05-23 08:00:34 +0000217 << ":" << TNum << "/" << Num-1 << "] for "
218 << CalleeFunc->getName() << "["
219 << GI.getGraphSize() << "+" << GI.getAuxFunctionCalls().size()
220 << "] into '" /*<< G.getFunctionNames()*/ << "' ["
221 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
222 << "]\n");
Chris Lattner79390d42003-11-13 05:05:41 +0000223 }
224 }
225 }
226
Chris Lattner79390d42003-11-13 05:05:41 +0000227 // Recompute the Incomplete markers
Chris Lattnerd10b5fd2004-02-20 23:52:15 +0000228 assert(G.getInlinedGlobals().empty());
Chris Lattner79390d42003-11-13 05:05:41 +0000229 G.maskIncompleteMarkers();
230 G.markIncompleteNodes(DSGraph::MarkFormalArgs);
231
232 // Delete dead nodes. Treat globals that are unreachable but that can
233 // reach live nodes as live.
Chris Lattner4ab483c2004-03-04 21:36:57 +0000234 G.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner79390d42003-11-13 05:05:41 +0000235}