1 //===- CompleteBottomUp.cpp - Complete Bottom-Up Data Structure Graphs ----===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "cbudatastructure"
17 #include "llvm/Analysis/DataStructure/DataStructure.h"
18 #include "llvm/Module.h"
19 #include "llvm/Analysis/DataStructure/DSGraph.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/ADT/SCCIterator.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/ADT/STLExtras.h"
28 RegisterPass<CompleteBUDataStructures>
29 X("cbudatastructure", "'Complete' Bottom-up Data Structure Analysis");
30 Statistic<> NumCBUInlines("cbudatastructures", "Number of graphs inlined");
34 // run - Calculate the bottom up data structure graphs for each function in the
37 bool CompleteBUDataStructures::runOnModule(Module &M) {
38 BUDataStructures &BU = getAnalysis<BUDataStructures>();
39 GlobalECs = BU.getGlobalECs();
40 GlobalsGraph = new DSGraph(BU.getGlobalsGraph(), GlobalECs);
41 GlobalsGraph->setPrintAuxCalls();
43 // Our call graph is the same as the BU data structures call graph
44 ActualCallees = BU.getActualCallees();
46 std::vector<DSGraph*> Stack;
47 hash_map<DSGraph*, unsigned> ValMap;
50 Function *MainFunc = M.getMainFunction();
52 if (!MainFunc->isExternal())
53 calculateSCCGraphs(getOrCreateGraph(*MainFunc), Stack, NextID, ValMap);
55 DEBUG(std::cerr << "CBU-DSA: No 'main' function found!\n");
58 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
59 if (!I->isExternal() && !DSInfo.count(I)) {
61 DEBUG(std::cerr << "*** CBU: Function unreachable from main: "
62 << I->getName() << "\n");
64 calculateSCCGraphs(getOrCreateGraph(*I), Stack, NextID, ValMap);
67 GlobalsGraph->removeTriviallyDeadNodes();
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.
73 if (MainFunc && !MainFunc->isExternal()) {
74 DSGraph &MainGraph = getOrCreateGraph(*MainFunc);
75 const DSGraph &GG = *MainGraph.getGlobalsGraph();
76 ReachabilityCloner RC(MainGraph, GG,
77 DSGraph::DontCloneCallNodes |
78 DSGraph::DontCloneAuxCallNodes);
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));
86 MainGraph.maskIncompleteMarkers();
87 MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
88 DSGraph::IgnoreGlobals);
94 DSGraph &CompleteBUDataStructures::getOrCreateGraph(Function &F) {
95 // Has the graph already been created?
96 DSGraph *&Graph = DSInfo[&F];
97 if (Graph) return *Graph;
99 // Copy the BU graph...
100 Graph = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F), GlobalECs);
101 Graph->setGlobalsGraph(GlobalsGraph);
102 Graph->setPrintAuxCalls();
104 // Make sure to update the DSInfo map for all of the functions currently in
106 for (DSGraph::retnodes_iterator I = Graph->retnodes_begin();
107 I != Graph->retnodes_end(); ++I)
108 DSInfo[I->first] = Graph;
115 unsigned CompleteBUDataStructures::calculateSCCGraphs(DSGraph &FG,
116 std::vector<DSGraph*> &Stack,
118 hash_map<DSGraph*, unsigned> &ValMap) {
119 assert(!ValMap.count(&FG) && "Shouldn't revisit functions!");
120 unsigned Min = NextID++, MyID = Min;
122 Stack.push_back(&FG);
124 // The edges out of the current node are the call site targets...
125 for (DSGraph::fc_iterator CI = FG.fc_begin(), CE = FG.fc_end();
127 Instruction *Call = CI->getCallSite().getInstruction();
129 // Loop over all of the actually called functions...
130 callee_iterator I = callee_begin(Call), E = callee_end(Call);
131 for (; I != E && I->first == Call; ++I) {
132 assert(I->first == Call && "Bad callee construction!");
133 if (!I->second->isExternal()) {
134 DSGraph &Callee = getOrCreateGraph(*I->second);
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.
142 if (M < Min) Min = M;
147 assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!");
149 return Min; // This is part of a larger SCC!
151 // If this is a new SCC, process it now.
152 bool IsMultiNodeSCC = false;
153 while (Stack.back() != &FG) {
154 DSGraph *NG = Stack.back();
159 // Update the DSInfo map and delete the old graph...
160 for (DSGraph::retnodes_iterator I = NG->retnodes_begin();
161 I != NG->retnodes_end(); ++I)
162 DSInfo[I->first] = &FG;
164 // Remove NG from the ValMap since the pointer may get recycled.
169 IsMultiNodeSCC = true;
172 // Clean up the graph before we start inlining a bunch again...
174 FG.removeTriviallyDeadNodes();
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.
185 void CompleteBUDataStructures::processGraph(DSGraph &G) {
186 hash_set<Instruction*> calls;
188 // The edges out of the current node are the call site targets...
190 for (DSGraph::fc_iterator CI = G.fc_begin(), CE = G.fc_end(); CI != CE;
192 const DSCallSite &CS = *CI;
193 Instruction *TheCall = CS.getCallSite().getInstruction();
195 assert(calls.insert(TheCall).second &&
196 "Call instruction occurs multiple times in graph??");
198 // 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)
203 // 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.
207 callee_iterator I = callee_begin(TheCall),E = callee_end(TheCall);
208 unsigned TNum = 0, Num = 0;
209 DEBUG(Num = std::distance(I, E));
210 for (; I != E; ++I, ++TNum) {
211 assert(I->first == TheCall && "Bad callee construction!");
212 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);
218 G.mergeInGraph(CS, *CalleeFunc, GI,
219 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes |
220 DSGraph::DontCloneAuxCallNodes);
221 DEBUG(std::cerr << " Inlining graph [" << i << "/"
222 << G.getFunctionCalls().size()-1
223 << ":" << TNum << "/" << Num-1 << "] for "
224 << CalleeFunc->getName() << "["
225 << GI.getGraphSize() << "+" << GI.getAuxFunctionCalls().size()
226 << "] into '" /*<< G.getFunctionNames()*/ << "' ["
227 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
233 // Recompute the Incomplete markers
234 G.maskIncompleteMarkers();
235 G.markIncompleteNodes(DSGraph::MarkFormalArgs);
237 // Delete dead nodes. Treat globals that are unreachable but that can
238 // reach live nodes as live.
239 G.removeDeadNodes(DSGraph::KeepUnreachableGlobals);