blob: e3deaff609576c990368a7e0098ef445fc6a3b0b [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 {
27 RegisterAnalysis<CompleteBUDataStructures>
28 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 {
54 std::cerr << "CBU-DSA: No 'main' function found!\n";
55 }
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)
58 if (!I->isExternal() && !DSInfo.count(I))
59 calculateSCCGraphs(getOrCreateGraph(*I), Stack, NextID, ValMap);
Chris Lattner95724a42003-11-13 01:43:00 +000060
Chris Lattner2dea8d62004-02-08 01:53:10 +000061 GlobalsGraph->removeTriviallyDeadNodes();
Chris Lattnera66e3532005-03-13 20:15:06 +000062
63
64 // Merge the globals variables (not the calls) from the globals graph back
65 // into the main function's graph so that the main function contains all of
66 // the information about global pools and GV usage in the program.
Chris Lattner49e88e82005-03-15 22:10:04 +000067 if (MainFunc && !MainFunc->isExternal()) {
Chris Lattnera66e3532005-03-13 20:15:06 +000068 DSGraph &MainGraph = getOrCreateGraph(*MainFunc);
69 const DSGraph &GG = *MainGraph.getGlobalsGraph();
Misha Brukman2b37d7c2005-04-21 21:13:18 +000070 ReachabilityCloner RC(MainGraph, GG,
Chris Lattnera66e3532005-03-13 20:15:06 +000071 DSGraph::DontCloneCallNodes |
72 DSGraph::DontCloneAuxCallNodes);
73
74 // Clone the global nodes into this graph.
75 for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(),
76 E = GG.getScalarMap().global_end(); I != E; ++I)
77 if (isa<GlobalVariable>(*I))
78 RC.getClonedNH(GG.getNodeForValue(*I));
79
Chris Lattner270cf502005-03-13 20:32:26 +000080 MainGraph.maskIncompleteMarkers();
Misha Brukman2b37d7c2005-04-21 21:13:18 +000081 MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
Chris Lattnera66e3532005-03-13 20:15:06 +000082 DSGraph::IgnoreGlobals);
83 }
84
Chris Lattner95724a42003-11-13 01:43:00 +000085 return false;
86}
Chris Lattner79390d42003-11-13 05:05:41 +000087
88DSGraph &CompleteBUDataStructures::getOrCreateGraph(Function &F) {
89 // Has the graph already been created?
90 DSGraph *&Graph = DSInfo[&F];
91 if (Graph) return *Graph;
92
93 // Copy the BU graph...
Chris Lattnerf4f62272005-03-19 22:23:45 +000094 Graph = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F), GlobalECs);
Chris Lattner79390d42003-11-13 05:05:41 +000095 Graph->setGlobalsGraph(GlobalsGraph);
96 Graph->setPrintAuxCalls();
97
98 // Make sure to update the DSInfo map for all of the functions currently in
99 // this graph!
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000100 for (DSGraph::retnodes_iterator I = Graph->retnodes_begin();
101 I != Graph->retnodes_end(); ++I)
Chris Lattner79390d42003-11-13 05:05:41 +0000102 DSInfo[I->first] = Graph;
103
104 return *Graph;
105}
106
107
108
109unsigned CompleteBUDataStructures::calculateSCCGraphs(DSGraph &FG,
110 std::vector<DSGraph*> &Stack,
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000111 unsigned &NextID,
Chris Lattner79390d42003-11-13 05:05:41 +0000112 hash_map<DSGraph*, unsigned> &ValMap) {
113 assert(!ValMap.count(&FG) && "Shouldn't revisit functions!");
114 unsigned Min = NextID++, MyID = Min;
115 ValMap[&FG] = Min;
116 Stack.push_back(&FG);
117
118 // The edges out of the current node are the call site targets...
Chris Lattnerf9aace22005-01-31 00:10:58 +0000119 for (DSGraph::fc_iterator CI = FG.fc_begin(), CE = FG.fc_end();
120 CI != CE; ++CI) {
Chris Lattnera9548d92005-01-30 23:51:02 +0000121 Instruction *Call = CI->getCallSite().getInstruction();
Chris Lattner79390d42003-11-13 05:05:41 +0000122
123 // Loop over all of the actually called functions...
Chris Lattner2ccc5f12005-04-02 20:02:41 +0000124 callee_iterator I = callee_begin(Call), E = callee_end(Call);
Chris Lattner021decc2005-04-02 19:17:18 +0000125 for (; I != E && I->first == Call; ++I) {
126 assert(I->first == Call && "Bad callee construction!");
Chris Lattnera366c982003-11-13 18:48:11 +0000127 if (!I->second->isExternal()) {
128 DSGraph &Callee = getOrCreateGraph(*I->second);
129 unsigned M;
130 // Have we visited the destination function yet?
131 hash_map<DSGraph*, unsigned>::iterator It = ValMap.find(&Callee);
132 if (It == ValMap.end()) // No, visit it now.
133 M = calculateSCCGraphs(Callee, Stack, NextID, ValMap);
134 else // Yes, get it's number.
135 M = It->second;
136 if (M < Min) Min = M;
137 }
Chris Lattner021decc2005-04-02 19:17:18 +0000138 }
Chris Lattner79390d42003-11-13 05:05:41 +0000139 }
140
141 assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!");
142 if (Min != MyID)
143 return Min; // This is part of a larger SCC!
144
145 // If this is a new SCC, process it now.
146 bool IsMultiNodeSCC = false;
147 while (Stack.back() != &FG) {
148 DSGraph *NG = Stack.back();
149 ValMap[NG] = ~0U;
150
Chris Lattnera2197132005-03-22 00:36:51 +0000151 FG.cloneInto(*NG);
Chris Lattner79390d42003-11-13 05:05:41 +0000152
153 // Update the DSInfo map and delete the old graph...
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000154 for (DSGraph::retnodes_iterator I = NG->retnodes_begin();
155 I != NG->retnodes_end(); ++I)
Chris Lattner79390d42003-11-13 05:05:41 +0000156 DSInfo[I->first] = &FG;
Chris Lattnera1c972d2004-10-07 20:01:31 +0000157
158 // Remove NG from the ValMap since the pointer may get recycled.
159 ValMap.erase(NG);
Chris Lattner79390d42003-11-13 05:05:41 +0000160 delete NG;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000161
Chris Lattner79390d42003-11-13 05:05:41 +0000162 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();
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000169
Chris Lattner79390d42003-11-13 05:05:41 +0000170 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...
Chris Lattnera9548d92005-01-30 23:51:02 +0000183 unsigned i = 0;
Chris Lattnerf9aace22005-01-31 00:10:58 +0000184 for (DSGraph::fc_iterator CI = G.fc_begin(), CE = G.fc_end(); CI != CE;
Chris Lattnera9548d92005-01-30 23:51:02 +0000185 ++CI, ++i) {
186 const DSCallSite &CS = *CI;
Chris Lattner79390d42003-11-13 05:05:41 +0000187 Instruction *TheCall = CS.getCallSite().getInstruction();
188
Chris Lattnerda5c5a52004-03-04 19:16:35 +0000189 assert(calls.insert(TheCall).second &&
190 "Call instruction occurs multiple times in graph??");
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000191
Chris Lattner5021b8c2005-03-18 23:19:47 +0000192 // Fast path for noop calls. Note that we don't care about merging globals
193 // in the callee with nodes in the caller here.
194 if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0)
195 continue;
Chris Lattnerda5c5a52004-03-04 19:16:35 +0000196
Vikram S. Adve052682f2004-05-23 08:00:34 +0000197 // Loop over all of the potentially called functions...
198 // Inline direct calls as well as indirect calls because the direct
199 // callee may have indirect callees and so may have changed.
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000200 //
Chris Lattner2ccc5f12005-04-02 20:02:41 +0000201 callee_iterator I = callee_begin(TheCall),E = callee_end(TheCall);
Chris Lattner021decc2005-04-02 19:17:18 +0000202 unsigned TNum = 0, Num = 0;
203 DEBUG(Num = std::distance(I, E));
Vikram S. Adve052682f2004-05-23 08:00:34 +0000204 for (; I != E; ++I, ++TNum) {
Chris Lattner021decc2005-04-02 19:17:18 +0000205 assert(I->first == TheCall && "Bad callee construction!");
Vikram S. Adve052682f2004-05-23 08:00:34 +0000206 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;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000212 G.mergeInGraph(CS, *CalleeFunc, GI,
Vikram S. Adve052682f2004-05-23 08:00:34 +0000213 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
228 G.maskIncompleteMarkers();
229 G.markIncompleteNodes(DSGraph::MarkFormalArgs);
230
231 // Delete dead nodes. Treat globals that are unreachable but that can
232 // reach live nodes as live.
Chris Lattner4ab483c2004-03-04 21:36:57 +0000233 G.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner79390d42003-11-13 05:05:41 +0000234}