blob: ee111e9d8fe887c225339bfb7ab2400f385335b0 [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;
160 delete NG;
161
162 Stack.pop_back();
163 IsMultiNodeSCC = true;
164 }
165
166 // Clean up the graph before we start inlining a bunch again...
167 if (IsMultiNodeSCC)
168 FG.removeTriviallyDeadNodes();
169
170 Stack.pop_back();
171 processGraph(FG);
172 ValMap[&FG] = ~0U;
173 return MyID;
174}
175
176
177/// processGraph - Process the BU graphs for the program in bottom-up order on
178/// the SCC of the __ACTUAL__ call graph. This builds "complete" BU graphs.
179void CompleteBUDataStructures::processGraph(DSGraph &G) {
Chris Lattnerda5c5a52004-03-04 19:16:35 +0000180 hash_set<Instruction*> calls;
Chris Lattnerd10b5fd2004-02-20 23:52:15 +0000181
Chris Lattner79390d42003-11-13 05:05:41 +0000182 // The edges out of the current node are the call site targets...
183 for (unsigned i = 0, e = G.getFunctionCalls().size(); i != e; ++i) {
184 const DSCallSite &CS = G.getFunctionCalls()[i];
185 Instruction *TheCall = CS.getCallSite().getInstruction();
186
Chris Lattnerda5c5a52004-03-04 19:16:35 +0000187 assert(calls.insert(TheCall).second &&
188 "Call instruction occurs multiple times in graph??");
189
190
Vikram S. Adve052682f2004-05-23 08:00:34 +0000191 // Loop over all of the potentially called functions...
192 // Inline direct calls as well as indirect calls because the direct
193 // callee may have indirect callees and so may have changed.
194 //
195 ActualCalleesTy::iterator I, E;
196 tie(I, E) = ActualCallees.equal_range(TheCall);
197 unsigned TNum = 0, Num = std::distance(I, E);
198 for (; I != E; ++I, ++TNum) {
199 Function *CalleeFunc = I->second;
200 if (!CalleeFunc->isExternal()) {
201 // Merge the callee's graph into this graph. This works for normal
202 // calls or for self recursion within an SCC.
203 DSGraph &GI = getOrCreateGraph(*CalleeFunc);
204 ++NumCBUInlines;
205 G.mergeInGraph(CS, *CalleeFunc, GI, DSGraph::KeepModRefBits |
206 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes |
207 DSGraph::DontCloneAuxCallNodes);
208 DEBUG(std::cerr << " Inlining graph [" << i << "/" << e-1
209 << ":" << TNum << "/" << Num-1 << "] for "
210 << CalleeFunc->getName() << "["
211 << GI.getGraphSize() << "+" << GI.getAuxFunctionCalls().size()
212 << "] into '" /*<< G.getFunctionNames()*/ << "' ["
213 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
214 << "]\n");
Chris Lattner79390d42003-11-13 05:05:41 +0000215 }
216 }
217 }
218
Chris Lattner79390d42003-11-13 05:05:41 +0000219 // Recompute the Incomplete markers
Chris Lattnerd10b5fd2004-02-20 23:52:15 +0000220 assert(G.getInlinedGlobals().empty());
Chris Lattner79390d42003-11-13 05:05:41 +0000221 G.maskIncompleteMarkers();
222 G.markIncompleteNodes(DSGraph::MarkFormalArgs);
223
224 // Delete dead nodes. Treat globals that are unreachable but that can
225 // reach live nodes as live.
Chris Lattner4ab483c2004-03-04 21:36:57 +0000226 G.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner79390d42003-11-13 05:05:41 +0000227}