blob: 3cd2bb0eef5f7b46e5c8510da0c074664b5aeadc [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;
52 const std::vector<DSCallSite> &CSs = TD.getDSGraph(*MI).getFunctionCalls();
53
54 for (unsigned CSi = 0, e = CSs.size(); CSi != e; ++CSi) {
Vikram S. Adve052682f2004-05-23 08:00:34 +000055 Instruction *TheCall = CSs[CSi].getCallSite().getInstruction();
Chris Lattner95724a42003-11-13 01:43:00 +000056
Vikram S. Adve052682f2004-05-23 08:00:34 +000057 if (CSs[CSi].isIndirectCall()) { // indirect call: insert all callees
Chris Lattner95724a42003-11-13 01:43:00 +000058 const std::vector<GlobalValue*> &Callees =
59 CSs[CSi].getCalleeNode()->getGlobals();
60 for (unsigned i = 0, e = Callees.size(); i != e; ++i)
61 if (Function *F = dyn_cast<Function>(Callees[i]))
62 ActualCallees.insert(std::make_pair(TheCall, F));
Vikram S. Adve052682f2004-05-23 08:00:34 +000063 } else { // direct call: insert the single callee directly
64 ActualCallees.insert(std::make_pair(TheCall,
65 CSs[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 Lattner79390d42003-11-13 05:05:41 +000078 if (Function *Main = M.getMainFunction()) {
Chris Lattnerdeb87122004-03-05 22:04:07 +000079 if (!Main->isExternal())
80 calculateSCCGraphs(getOrCreateGraph(*Main), Stack, NextID, ValMap);
Chris Lattner79390d42003-11-13 05:05:41 +000081 } else {
82 std::cerr << "CBU-DSA: No 'main' function found!\n";
83 }
84
85 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
86 if (!I->isExternal() && !DSInfo.count(I))
87 calculateSCCGraphs(getOrCreateGraph(*I), Stack, NextID, ValMap);
Chris Lattner95724a42003-11-13 01:43:00 +000088
Chris Lattner2dea8d62004-02-08 01:53:10 +000089 GlobalsGraph->removeTriviallyDeadNodes();
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...
99 Graph = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F));
100 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!
105 for (DSGraph::ReturnNodesTy::iterator I = Graph->getReturnNodes().begin();
106 I != Graph->getReturnNodes().end(); ++I)
107 DSInfo[I->first] = Graph;
108
109 return *Graph;
110}
111
112
113
114unsigned CompleteBUDataStructures::calculateSCCGraphs(DSGraph &FG,
115 std::vector<DSGraph*> &Stack,
116 unsigned &NextID,
117 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...
124 for (unsigned i = 0, e = FG.getFunctionCalls().size(); i != e; ++i) {
125 Instruction *Call = FG.getFunctionCalls()[i].getCallSite().getInstruction();
126
127 // Loop over all of the actually called functions...
128 ActualCalleesTy::iterator I, E;
Chris Lattnera366c982003-11-13 18:48:11 +0000129 for (tie(I, E) = ActualCallees.equal_range(Call); I != E; ++I)
130 if (!I->second->isExternal()) {
131 DSGraph &Callee = getOrCreateGraph(*I->second);
132 unsigned M;
133 // Have we visited the destination function yet?
134 hash_map<DSGraph*, unsigned>::iterator It = ValMap.find(&Callee);
135 if (It == ValMap.end()) // No, visit it now.
136 M = calculateSCCGraphs(Callee, Stack, NextID, ValMap);
137 else // Yes, get it's number.
138 M = It->second;
139 if (M < Min) Min = M;
140 }
Chris Lattner79390d42003-11-13 05:05:41 +0000141 }
142
143 assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!");
144 if (Min != MyID)
145 return Min; // This is part of a larger SCC!
146
147 // If this is a new SCC, process it now.
148 bool IsMultiNodeSCC = false;
149 while (Stack.back() != &FG) {
150 DSGraph *NG = Stack.back();
151 ValMap[NG] = ~0U;
152
153 DSGraph::NodeMapTy NodeMap;
Chris Lattner825a02a2004-01-27 21:50:41 +0000154 FG.cloneInto(*NG, FG.getScalarMap(), FG.getReturnNodes(), NodeMap);
Chris Lattner79390d42003-11-13 05:05:41 +0000155
156 // Update the DSInfo map and delete the old graph...
157 for (DSGraph::ReturnNodesTy::iterator I = NG->getReturnNodes().begin();
158 I != NG->getReturnNodes().end(); ++I)
159 DSInfo[I->first] = &FG;
Chris Lattnera1c972d2004-10-07 20:01:31 +0000160
161 // Remove NG from the ValMap since the pointer may get recycled.
162 ValMap.erase(NG);
Chris Lattner79390d42003-11-13 05:05:41 +0000163 delete NG;
164
165 Stack.pop_back();
166 IsMultiNodeSCC = true;
167 }
168
169 // Clean up the graph before we start inlining a bunch again...
170 if (IsMultiNodeSCC)
171 FG.removeTriviallyDeadNodes();
172
173 Stack.pop_back();
174 processGraph(FG);
175 ValMap[&FG] = ~0U;
176 return MyID;
177}
178
179
180/// processGraph - Process the BU graphs for the program in bottom-up order on
181/// the SCC of the __ACTUAL__ call graph. This builds "complete" BU graphs.
182void CompleteBUDataStructures::processGraph(DSGraph &G) {
Chris Lattnerda5c5a52004-03-04 19:16:35 +0000183 hash_set<Instruction*> calls;
Chris Lattnerd10b5fd2004-02-20 23:52:15 +0000184
Chris Lattner79390d42003-11-13 05:05:41 +0000185 // The edges out of the current node are the call site targets...
186 for (unsigned i = 0, e = G.getFunctionCalls().size(); i != e; ++i) {
187 const DSCallSite &CS = G.getFunctionCalls()[i];
188 Instruction *TheCall = CS.getCallSite().getInstruction();
189
Chris Lattnerda5c5a52004-03-04 19:16:35 +0000190 assert(calls.insert(TheCall).second &&
191 "Call instruction occurs multiple times in graph??");
192
193
Vikram S. Adve052682f2004-05-23 08:00:34 +0000194 // Loop over all of the potentially called functions...
195 // Inline direct calls as well as indirect calls because the direct
196 // callee may have indirect callees and so may have changed.
197 //
198 ActualCalleesTy::iterator I, E;
199 tie(I, E) = ActualCallees.equal_range(TheCall);
200 unsigned TNum = 0, Num = std::distance(I, E);
201 for (; I != E; ++I, ++TNum) {
202 Function *CalleeFunc = I->second;
203 if (!CalleeFunc->isExternal()) {
204 // Merge the callee's graph into this graph. This works for normal
205 // calls or for self recursion within an SCC.
206 DSGraph &GI = getOrCreateGraph(*CalleeFunc);
207 ++NumCBUInlines;
208 G.mergeInGraph(CS, *CalleeFunc, GI, DSGraph::KeepModRefBits |
209 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes |
210 DSGraph::DontCloneAuxCallNodes);
211 DEBUG(std::cerr << " Inlining graph [" << i << "/" << e-1
212 << ":" << TNum << "/" << Num-1 << "] for "
213 << CalleeFunc->getName() << "["
214 << GI.getGraphSize() << "+" << GI.getAuxFunctionCalls().size()
215 << "] into '" /*<< G.getFunctionNames()*/ << "' ["
216 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
217 << "]\n");
Chris Lattner79390d42003-11-13 05:05:41 +0000218 }
219 }
220 }
221
Chris Lattner79390d42003-11-13 05:05:41 +0000222 // Recompute the Incomplete markers
Chris Lattnerd10b5fd2004-02-20 23:52:15 +0000223 assert(G.getInlinedGlobals().empty());
Chris Lattner79390d42003-11-13 05:05:41 +0000224 G.maskIncompleteMarkers();
225 G.markIncompleteNodes(DSGraph::MarkFormalArgs);
226
227 // Delete dead nodes. Treat globals that are unreachable but that can
228 // reach live nodes as live.
Chris Lattner4ab483c2004-03-04 21:36:57 +0000229 G.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner79390d42003-11-13 05:05:41 +0000230}