blob: 103c6bf5392263530af1ffc07991939949e4a213 [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>();
Chris Lattnerf4f62272005-03-19 22:23:45 +000037 GlobalsGraph = new DSGraph(BU.getGlobalsGraph(), GlobalECs);
Chris Lattner95724a42003-11-13 01:43:00 +000038 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 Lattner2496d692005-03-20 02:41:16 +000059 std::vector<Function*> Callees;
60 CSI->getCalleeNode()->addFullFunctionList(Callees);
Chris Lattner95724a42003-11-13 01:43:00 +000061 for (unsigned i = 0, e = Callees.size(); i != e; ++i)
Chris Lattner2496d692005-03-20 02:41:16 +000062 ActualCallees.insert(std::make_pair(TheCall, Callees[i]));
Vikram S. Adve052682f2004-05-23 08:00:34 +000063 } else { // direct call: insert the single callee directly
64 ActualCallees.insert(std::make_pair(TheCall,
Chris Lattnera9548d92005-01-30 23:51:02 +000065 CSI->getCalleeFunc()));
Chris Lattner95724a42003-11-13 01:43:00 +000066 }
67 }
68 }
Vikram S. Adve052682f2004-05-23 08:00:34 +000069#else
70 // Our call graph is the same as the BU data structures call graph
71 ActualCallees = BU.getActualCallees();
Chris Lattner95724a42003-11-13 01:43:00 +000072#endif
73
Chris Lattner79390d42003-11-13 05:05:41 +000074 std::vector<DSGraph*> Stack;
75 hash_map<DSGraph*, unsigned> ValMap;
76 unsigned NextID = 1;
Chris Lattner95724a42003-11-13 01:43:00 +000077
Chris Lattnera66e3532005-03-13 20:15:06 +000078 Function *MainFunc = M.getMainFunction();
79 if (MainFunc) {
80 if (!MainFunc->isExternal())
81 calculateSCCGraphs(getOrCreateGraph(*MainFunc), 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 Lattnera66e3532005-03-13 20:15:06 +000091
92
93 // Merge the globals variables (not the calls) from the globals graph back
94 // into the main function's graph so that the main function contains all of
95 // the information about global pools and GV usage in the program.
Chris Lattner49e88e82005-03-15 22:10:04 +000096 if (MainFunc && !MainFunc->isExternal()) {
Chris Lattnera66e3532005-03-13 20:15:06 +000097 DSGraph &MainGraph = getOrCreateGraph(*MainFunc);
98 const DSGraph &GG = *MainGraph.getGlobalsGraph();
99 ReachabilityCloner RC(MainGraph, GG,
100 DSGraph::DontCloneCallNodes |
101 DSGraph::DontCloneAuxCallNodes);
102
103 // Clone the global nodes into this graph.
104 for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(),
105 E = GG.getScalarMap().global_end(); I != E; ++I)
106 if (isa<GlobalVariable>(*I))
107 RC.getClonedNH(GG.getNodeForValue(*I));
108
Chris Lattner270cf502005-03-13 20:32:26 +0000109 MainGraph.maskIncompleteMarkers();
Chris Lattnera66e3532005-03-13 20:15:06 +0000110 MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
111 DSGraph::IgnoreGlobals);
112 }
113
Chris Lattner95724a42003-11-13 01:43:00 +0000114 return false;
115}
Chris Lattner79390d42003-11-13 05:05:41 +0000116
117DSGraph &CompleteBUDataStructures::getOrCreateGraph(Function &F) {
118 // Has the graph already been created?
119 DSGraph *&Graph = DSInfo[&F];
120 if (Graph) return *Graph;
121
122 // Copy the BU graph...
Chris Lattnerf4f62272005-03-19 22:23:45 +0000123 Graph = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F), GlobalECs);
Chris Lattner79390d42003-11-13 05:05:41 +0000124 Graph->setGlobalsGraph(GlobalsGraph);
125 Graph->setPrintAuxCalls();
126
127 // Make sure to update the DSInfo map for all of the functions currently in
128 // this graph!
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000129 for (DSGraph::retnodes_iterator I = Graph->retnodes_begin();
130 I != Graph->retnodes_end(); ++I)
Chris Lattner79390d42003-11-13 05:05:41 +0000131 DSInfo[I->first] = Graph;
132
133 return *Graph;
134}
135
136
137
138unsigned CompleteBUDataStructures::calculateSCCGraphs(DSGraph &FG,
139 std::vector<DSGraph*> &Stack,
140 unsigned &NextID,
141 hash_map<DSGraph*, unsigned> &ValMap) {
142 assert(!ValMap.count(&FG) && "Shouldn't revisit functions!");
143 unsigned Min = NextID++, MyID = Min;
144 ValMap[&FG] = Min;
145 Stack.push_back(&FG);
146
147 // The edges out of the current node are the call site targets...
Chris Lattnerf9aace22005-01-31 00:10:58 +0000148 for (DSGraph::fc_iterator CI = FG.fc_begin(), CE = FG.fc_end();
149 CI != CE; ++CI) {
Chris Lattnera9548d92005-01-30 23:51:02 +0000150 Instruction *Call = CI->getCallSite().getInstruction();
Chris Lattner79390d42003-11-13 05:05:41 +0000151
152 // Loop over all of the actually called functions...
153 ActualCalleesTy::iterator I, E;
Chris Lattnera366c982003-11-13 18:48:11 +0000154 for (tie(I, E) = ActualCallees.equal_range(Call); I != E; ++I)
155 if (!I->second->isExternal()) {
156 DSGraph &Callee = getOrCreateGraph(*I->second);
157 unsigned M;
158 // Have we visited the destination function yet?
159 hash_map<DSGraph*, unsigned>::iterator It = ValMap.find(&Callee);
160 if (It == ValMap.end()) // No, visit it now.
161 M = calculateSCCGraphs(Callee, Stack, NextID, ValMap);
162 else // Yes, get it's number.
163 M = It->second;
164 if (M < Min) Min = M;
165 }
Chris Lattner79390d42003-11-13 05:05:41 +0000166 }
167
168 assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!");
169 if (Min != MyID)
170 return Min; // This is part of a larger SCC!
171
172 // If this is a new SCC, process it now.
173 bool IsMultiNodeSCC = false;
174 while (Stack.back() != &FG) {
175 DSGraph *NG = Stack.back();
176 ValMap[NG] = ~0U;
177
Chris Lattnera2197132005-03-22 00:36:51 +0000178 FG.cloneInto(*NG);
Chris Lattner79390d42003-11-13 05:05:41 +0000179
180 // Update the DSInfo map and delete the old graph...
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000181 for (DSGraph::retnodes_iterator I = NG->retnodes_begin();
182 I != NG->retnodes_end(); ++I)
Chris Lattner79390d42003-11-13 05:05:41 +0000183 DSInfo[I->first] = &FG;
Chris Lattnera1c972d2004-10-07 20:01:31 +0000184
185 // Remove NG from the ValMap since the pointer may get recycled.
186 ValMap.erase(NG);
Chris Lattner79390d42003-11-13 05:05:41 +0000187 delete NG;
188
189 Stack.pop_back();
190 IsMultiNodeSCC = true;
191 }
192
193 // Clean up the graph before we start inlining a bunch again...
194 if (IsMultiNodeSCC)
195 FG.removeTriviallyDeadNodes();
196
197 Stack.pop_back();
198 processGraph(FG);
199 ValMap[&FG] = ~0U;
200 return MyID;
201}
202
203
204/// processGraph - Process the BU graphs for the program in bottom-up order on
205/// the SCC of the __ACTUAL__ call graph. This builds "complete" BU graphs.
206void CompleteBUDataStructures::processGraph(DSGraph &G) {
Chris Lattnerda5c5a52004-03-04 19:16:35 +0000207 hash_set<Instruction*> calls;
Chris Lattnerd10b5fd2004-02-20 23:52:15 +0000208
Chris Lattner79390d42003-11-13 05:05:41 +0000209 // The edges out of the current node are the call site targets...
Chris Lattnera9548d92005-01-30 23:51:02 +0000210 unsigned i = 0;
Chris Lattnerf9aace22005-01-31 00:10:58 +0000211 for (DSGraph::fc_iterator CI = G.fc_begin(), CE = G.fc_end(); CI != CE;
Chris Lattnera9548d92005-01-30 23:51:02 +0000212 ++CI, ++i) {
213 const DSCallSite &CS = *CI;
Chris Lattner79390d42003-11-13 05:05:41 +0000214 Instruction *TheCall = CS.getCallSite().getInstruction();
215
Chris Lattnerda5c5a52004-03-04 19:16:35 +0000216 assert(calls.insert(TheCall).second &&
217 "Call instruction occurs multiple times in graph??");
Chris Lattner5021b8c2005-03-18 23:19:47 +0000218
219 // Fast path for noop calls. Note that we don't care about merging globals
220 // in the callee with nodes in the caller here.
221 if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0)
222 continue;
Chris Lattnerda5c5a52004-03-04 19:16:35 +0000223
Vikram S. Adve052682f2004-05-23 08:00:34 +0000224 // Loop over all of the potentially called functions...
225 // Inline direct calls as well as indirect calls because the direct
226 // callee may have indirect callees and so may have changed.
227 //
228 ActualCalleesTy::iterator I, E;
229 tie(I, E) = ActualCallees.equal_range(TheCall);
230 unsigned TNum = 0, Num = std::distance(I, E);
231 for (; I != E; ++I, ++TNum) {
232 Function *CalleeFunc = I->second;
233 if (!CalleeFunc->isExternal()) {
234 // Merge the callee's graph into this graph. This works for normal
235 // calls or for self recursion within an SCC.
236 DSGraph &GI = getOrCreateGraph(*CalleeFunc);
237 ++NumCBUInlines;
238 G.mergeInGraph(CS, *CalleeFunc, GI, DSGraph::KeepModRefBits |
239 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes |
240 DSGraph::DontCloneAuxCallNodes);
Chris Lattnera9548d92005-01-30 23:51:02 +0000241 DEBUG(std::cerr << " Inlining graph [" << i << "/"
242 << G.getFunctionCalls().size()-1
Vikram S. Adve052682f2004-05-23 08:00:34 +0000243 << ":" << TNum << "/" << Num-1 << "] for "
244 << CalleeFunc->getName() << "["
245 << GI.getGraphSize() << "+" << GI.getAuxFunctionCalls().size()
246 << "] into '" /*<< G.getFunctionNames()*/ << "' ["
247 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
248 << "]\n");
Chris Lattner79390d42003-11-13 05:05:41 +0000249 }
250 }
251 }
252
Chris Lattner79390d42003-11-13 05:05:41 +0000253 // Recompute the Incomplete markers
254 G.maskIncompleteMarkers();
255 G.markIncompleteNodes(DSGraph::MarkFormalArgs);
256
257 // Delete dead nodes. Treat globals that are unreachable but that can
258 // reach live nodes as live.
Chris Lattner4ab483c2004-03-04 21:36:57 +0000259 G.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner79390d42003-11-13 05:05:41 +0000260}