blob: 90c14b408ff59f29dcd020d66f60f4ec53e5ee60 [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 Lattner72382102006-01-22 23:19:18 +000024#include <iostream>
Chris Lattner95724a42003-11-13 01:43:00 +000025using namespace llvm;
26
27namespace {
Chris Lattner5d8925c2006-08-27 22:30:17 +000028 RegisterPass<CompleteBUDataStructures>
Chris Lattner95724a42003-11-13 01:43:00 +000029 X("cbudatastructure", "'Complete' Bottom-up Data Structure Analysis");
Chris Lattnerda5c5a52004-03-04 19:16:35 +000030 Statistic<> NumCBUInlines("cbudatastructures", "Number of graphs inlined");
Chris Lattner95724a42003-11-13 01:43:00 +000031}
32
Chris Lattner95724a42003-11-13 01:43:00 +000033
34// run - Calculate the bottom up data structure graphs for each function in the
35// program.
36//
Chris Lattnerb12914b2004-09-20 04:48:05 +000037bool CompleteBUDataStructures::runOnModule(Module &M) {
Chris Lattner95724a42003-11-13 01:43:00 +000038 BUDataStructures &BU = getAnalysis<BUDataStructures>();
Chris Lattnercc1f2452005-04-23 21:11:05 +000039 GlobalECs = BU.getGlobalECs();
Chris Lattnerf4f62272005-03-19 22:23:45 +000040 GlobalsGraph = new DSGraph(BU.getGlobalsGraph(), GlobalECs);
Chris Lattner95724a42003-11-13 01:43:00 +000041 GlobalsGraph->setPrintAuxCalls();
42
Vikram S. Adve052682f2004-05-23 08:00:34 +000043 // Our call graph is the same as the BU data structures call graph
44 ActualCallees = BU.getActualCallees();
Chris Lattner95724a42003-11-13 01:43:00 +000045
Chris Lattner79390d42003-11-13 05:05:41 +000046 std::vector<DSGraph*> Stack;
47 hash_map<DSGraph*, unsigned> ValMap;
48 unsigned NextID = 1;
Chris Lattner95724a42003-11-13 01:43:00 +000049
Chris Lattnera66e3532005-03-13 20:15:06 +000050 Function *MainFunc = M.getMainFunction();
51 if (MainFunc) {
52 if (!MainFunc->isExternal())
53 calculateSCCGraphs(getOrCreateGraph(*MainFunc), Stack, NextID, ValMap);
Chris Lattner79390d42003-11-13 05:05:41 +000054 } else {
Andrew Lenharth7445ea62006-10-13 17:38:22 +000055 DEBUG(std::cerr << "CBU-DSA: No 'main' function found!\n");
Chris Lattner79390d42003-11-13 05:05:41 +000056 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000057
Chris Lattner79390d42003-11-13 05:05:41 +000058 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Andrew Lenharthc269c522006-06-16 14:43:36 +000059 if (!I->isExternal() && !DSInfo.count(I)) {
Andrew Lenharth7445ea62006-10-13 17:38:22 +000060 if (MainFunc) {
61 DEBUG(std::cerr << "*** CBU: Function unreachable from main: "
62 << I->getName() << "\n");
63 }
Chris Lattner79390d42003-11-13 05:05:41 +000064 calculateSCCGraphs(getOrCreateGraph(*I), Stack, NextID, ValMap);
Andrew Lenharthc269c522006-06-16 14:43:36 +000065 }
Chris Lattner95724a42003-11-13 01:43:00 +000066
Chris Lattner2dea8d62004-02-08 01:53:10 +000067 GlobalsGraph->removeTriviallyDeadNodes();
Chris Lattnera66e3532005-03-13 20:15:06 +000068
69
70 // Merge the globals variables (not the calls) from the globals graph back
71 // into the main function's graph so that the main function contains all of
72 // the information about global pools and GV usage in the program.
Chris Lattner49e88e82005-03-15 22:10:04 +000073 if (MainFunc && !MainFunc->isExternal()) {
Chris Lattnera66e3532005-03-13 20:15:06 +000074 DSGraph &MainGraph = getOrCreateGraph(*MainFunc);
75 const DSGraph &GG = *MainGraph.getGlobalsGraph();
Misha Brukman2b37d7c2005-04-21 21:13:18 +000076 ReachabilityCloner RC(MainGraph, GG,
Chris Lattnera66e3532005-03-13 20:15:06 +000077 DSGraph::DontCloneCallNodes |
78 DSGraph::DontCloneAuxCallNodes);
79
80 // Clone the global nodes into this graph.
81 for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(),
82 E = GG.getScalarMap().global_end(); I != E; ++I)
83 if (isa<GlobalVariable>(*I))
84 RC.getClonedNH(GG.getNodeForValue(*I));
85
Chris Lattner270cf502005-03-13 20:32:26 +000086 MainGraph.maskIncompleteMarkers();
Misha Brukman2b37d7c2005-04-21 21:13:18 +000087 MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
Chris Lattnera66e3532005-03-13 20:15:06 +000088 DSGraph::IgnoreGlobals);
89 }
90
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...
Chris Lattnerf4f62272005-03-19 22:23:45 +0000100 Graph = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F), GlobalECs);
Chris Lattner79390d42003-11-13 05:05:41 +0000101 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!
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000106 for (DSGraph::retnodes_iterator I = Graph->retnodes_begin();
107 I != Graph->retnodes_end(); ++I)
Chris Lattner79390d42003-11-13 05:05:41 +0000108 DSInfo[I->first] = Graph;
109
110 return *Graph;
111}
112
113
114
115unsigned CompleteBUDataStructures::calculateSCCGraphs(DSGraph &FG,
116 std::vector<DSGraph*> &Stack,
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000117 unsigned &NextID,
Chris Lattner79390d42003-11-13 05:05:41 +0000118 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...
Chris Lattner2ccc5f12005-04-02 20:02:41 +0000130 callee_iterator I = callee_begin(Call), E = callee_end(Call);
Chris Lattner021decc2005-04-02 19:17:18 +0000131 for (; I != E && I->first == Call; ++I) {
132 assert(I->first == Call && "Bad callee construction!");
Chris Lattnera366c982003-11-13 18:48:11 +0000133 if (!I->second->isExternal()) {
134 DSGraph &Callee = getOrCreateGraph(*I->second);
135 unsigned M;
136 // Have we visited the destination function yet?
137 hash_map<DSGraph*, unsigned>::iterator It = ValMap.find(&Callee);
138 if (It == ValMap.end()) // No, visit it now.
139 M = calculateSCCGraphs(Callee, Stack, NextID, ValMap);
140 else // Yes, get it's number.
141 M = It->second;
142 if (M < Min) Min = M;
143 }
Chris Lattner021decc2005-04-02 19:17:18 +0000144 }
Chris Lattner79390d42003-11-13 05:05:41 +0000145 }
146
147 assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!");
148 if (Min != MyID)
149 return Min; // This is part of a larger SCC!
150
151 // If this is a new SCC, process it now.
152 bool IsMultiNodeSCC = false;
153 while (Stack.back() != &FG) {
154 DSGraph *NG = Stack.back();
155 ValMap[NG] = ~0U;
156
Chris Lattnera2197132005-03-22 00:36:51 +0000157 FG.cloneInto(*NG);
Chris Lattner79390d42003-11-13 05:05:41 +0000158
159 // Update the DSInfo map and delete the old graph...
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000160 for (DSGraph::retnodes_iterator I = NG->retnodes_begin();
161 I != NG->retnodes_end(); ++I)
Chris Lattner79390d42003-11-13 05:05:41 +0000162 DSInfo[I->first] = &FG;
Chris Lattnera1c972d2004-10-07 20:01:31 +0000163
164 // Remove NG from the ValMap since the pointer may get recycled.
165 ValMap.erase(NG);
Chris Lattner79390d42003-11-13 05:05:41 +0000166 delete NG;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000167
Chris Lattner79390d42003-11-13 05:05:41 +0000168 Stack.pop_back();
169 IsMultiNodeSCC = true;
170 }
171
172 // Clean up the graph before we start inlining a bunch again...
173 if (IsMultiNodeSCC)
174 FG.removeTriviallyDeadNodes();
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000175
Chris Lattner79390d42003-11-13 05:05:41 +0000176 Stack.pop_back();
177 processGraph(FG);
178 ValMap[&FG] = ~0U;
179 return MyID;
180}
181
182
183/// processGraph - Process the BU graphs for the program in bottom-up order on
184/// the SCC of the __ACTUAL__ call graph. This builds "complete" BU graphs.
185void CompleteBUDataStructures::processGraph(DSGraph &G) {
Chris Lattnerda5c5a52004-03-04 19:16:35 +0000186 hash_set<Instruction*> calls;
Chris Lattnerd10b5fd2004-02-20 23:52:15 +0000187
Chris Lattner79390d42003-11-13 05:05:41 +0000188 // The edges out of the current node are the call site targets...
Chris Lattnera9548d92005-01-30 23:51:02 +0000189 unsigned i = 0;
Chris Lattnerf9aace22005-01-31 00:10:58 +0000190 for (DSGraph::fc_iterator CI = G.fc_begin(), CE = G.fc_end(); CI != CE;
Chris Lattnera9548d92005-01-30 23:51:02 +0000191 ++CI, ++i) {
192 const DSCallSite &CS = *CI;
Chris Lattner79390d42003-11-13 05:05:41 +0000193 Instruction *TheCall = CS.getCallSite().getInstruction();
194
Chris Lattnerda5c5a52004-03-04 19:16:35 +0000195 assert(calls.insert(TheCall).second &&
196 "Call instruction occurs multiple times in graph??");
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000197
Chris Lattner5021b8c2005-03-18 23:19:47 +0000198 // Fast path for noop calls. Note that we don't care about merging globals
199 // in the callee with nodes in the caller here.
200 if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0)
201 continue;
Chris Lattnerda5c5a52004-03-04 19:16:35 +0000202
Vikram S. Adve052682f2004-05-23 08:00:34 +0000203 // Loop over all of the potentially called functions...
204 // Inline direct calls as well as indirect calls because the direct
205 // callee may have indirect callees and so may have changed.
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000206 //
Chris Lattner2ccc5f12005-04-02 20:02:41 +0000207 callee_iterator I = callee_begin(TheCall),E = callee_end(TheCall);
Chris Lattner021decc2005-04-02 19:17:18 +0000208 unsigned TNum = 0, Num = 0;
209 DEBUG(Num = std::distance(I, E));
Vikram S. Adve052682f2004-05-23 08:00:34 +0000210 for (; I != E; ++I, ++TNum) {
Chris Lattner021decc2005-04-02 19:17:18 +0000211 assert(I->first == TheCall && "Bad callee construction!");
Vikram S. Adve052682f2004-05-23 08:00:34 +0000212 Function *CalleeFunc = I->second;
213 if (!CalleeFunc->isExternal()) {
214 // Merge the callee's graph into this graph. This works for normal
215 // calls or for self recursion within an SCC.
216 DSGraph &GI = getOrCreateGraph(*CalleeFunc);
217 ++NumCBUInlines;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000218 G.mergeInGraph(CS, *CalleeFunc, GI,
Vikram S. Adve052682f2004-05-23 08:00:34 +0000219 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes |
220 DSGraph::DontCloneAuxCallNodes);
Chris Lattnera9548d92005-01-30 23:51:02 +0000221 DEBUG(std::cerr << " Inlining graph [" << i << "/"
222 << G.getFunctionCalls().size()-1
Vikram S. Adve052682f2004-05-23 08:00:34 +0000223 << ":" << TNum << "/" << Num-1 << "] for "
224 << CalleeFunc->getName() << "["
225 << GI.getGraphSize() << "+" << GI.getAuxFunctionCalls().size()
226 << "] into '" /*<< G.getFunctionNames()*/ << "' ["
227 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
228 << "]\n");
Chris Lattner79390d42003-11-13 05:05:41 +0000229 }
230 }
231 }
232
Chris Lattner79390d42003-11-13 05:05:41 +0000233 // Recompute the Incomplete markers
234 G.maskIncompleteMarkers();
235 G.markIncompleteNodes(DSGraph::MarkFormalArgs);
236
237 // Delete dead nodes. Treat globals that are unreachable but that can
238 // reach live nodes as live.
Chris Lattner4ab483c2004-03-04 21:36:57 +0000239 G.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner79390d42003-11-13 05:05:41 +0000240}