2003-11-13 01:43:00 +00:00
|
|
|
//===- CompleteBottomUp.cpp - Complete Bottom-Up Data Structure Graphs ----===//
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2003-11-13 01:43:00 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2003-11-13 01:43:00 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is the exact same as the bottom-up graphs, but we use take a completed
|
|
|
|
// call graph and inline all indirect callees into their callers graphs, making
|
|
|
|
// the result more useful for things like pool allocation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-04-02 19:17:18 +00:00
|
|
|
#define DEBUG_TYPE "cbudatastructure"
|
2004-07-07 06:32:21 +00:00
|
|
|
#include "llvm/Analysis/DataStructure/DataStructure.h"
|
2003-11-13 01:43:00 +00:00
|
|
|
#include "llvm/Module.h"
|
2004-07-07 06:32:21 +00:00
|
|
|
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/ADT/SCCIterator.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2006-01-22 23:19:18 +00:00
|
|
|
#include <iostream>
|
2003-11-13 01:43:00 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2006-08-27 22:30:17 +00:00
|
|
|
RegisterPass<CompleteBUDataStructures>
|
2003-11-13 01:43:00 +00:00
|
|
|
X("cbudatastructure", "'Complete' Bottom-up Data Structure Analysis");
|
2004-03-04 19:16:35 +00:00
|
|
|
Statistic<> NumCBUInlines("cbudatastructures", "Number of graphs inlined");
|
2003-11-13 01:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// run - Calculate the bottom up data structure graphs for each function in the
|
|
|
|
// program.
|
|
|
|
//
|
2004-09-20 04:48:05 +00:00
|
|
|
bool CompleteBUDataStructures::runOnModule(Module &M) {
|
2003-11-13 01:43:00 +00:00
|
|
|
BUDataStructures &BU = getAnalysis<BUDataStructures>();
|
2005-04-23 21:11:05 +00:00
|
|
|
GlobalECs = BU.getGlobalECs();
|
Create an equivalence class of global variables that DSA will never be able
to tell apart anyway, and only track the leader for of these equivalence
classes in our graphs.
This dramatically reduces the number of GlobalValue*'s that appear in scalar
maps, which A) reduces memory usage, by eliminating many many scalarmap entries
and B) reduces time for operations that need to execute an operation for each
global in the scalar map.
As an example, this reduces the memory used to analyze 176.gcc from 1GB to
511MB, which (while it's still way too much) is better because it doesn't hit
swap anymore. On eon, this shrinks the local graphs from 14MB to 6.8MB,
shrinks the bu+td graphs of povray from 50M to 40M, shrinks the TD graphs of
130.li from 8.8M to 3.6M, etc.
This change also speeds up DSA on large programs where this makes a big
difference. For example, 130.li goes from 1.17s -> 0.56s, 134.perl goes
from 2.14 -> 0.93s, povray goes from 15.63s->7.99s (!!!).
This also apparently either fixes the problem that caused DSA to crash on
perlbmk and gcc, or it hides it, because DSA now works on these. These
both take entirely too much time in the TD pass (147s for perl, 538s for
gcc, vs 7.67/5.9s in the bu pass for either one), but this is a known
problem that I'll deal with later.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@20696 91177308-0d34-0410-b5e6-96231b3b80d8
2005-03-19 22:23:45 +00:00
|
|
|
GlobalsGraph = new DSGraph(BU.getGlobalsGraph(), GlobalECs);
|
2003-11-13 01:43:00 +00:00
|
|
|
GlobalsGraph->setPrintAuxCalls();
|
|
|
|
|
2004-05-23 08:00:34 +00:00
|
|
|
// Our call graph is the same as the BU data structures call graph
|
|
|
|
ActualCallees = BU.getActualCallees();
|
2003-11-13 01:43:00 +00:00
|
|
|
|
2003-11-13 05:05:41 +00:00
|
|
|
std::vector<DSGraph*> Stack;
|
|
|
|
hash_map<DSGraph*, unsigned> ValMap;
|
|
|
|
unsigned NextID = 1;
|
|
|
|
|
2005-03-13 20:15:06 +00:00
|
|
|
Function *MainFunc = M.getMainFunction();
|
|
|
|
if (MainFunc) {
|
|
|
|
if (!MainFunc->isExternal())
|
|
|
|
calculateSCCGraphs(getOrCreateGraph(*MainFunc), Stack, NextID, ValMap);
|
2003-11-13 05:05:41 +00:00
|
|
|
} else {
|
2006-10-13 17:38:22 +00:00
|
|
|
DEBUG(std::cerr << "CBU-DSA: No 'main' function found!\n");
|
2003-11-13 05:05:41 +00:00
|
|
|
}
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2003-11-13 01:43:00 +00:00
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
2006-06-16 14:43:36 +00:00
|
|
|
if (!I->isExternal() && !DSInfo.count(I)) {
|
2006-10-13 17:38:22 +00:00
|
|
|
if (MainFunc) {
|
|
|
|
DEBUG(std::cerr << "*** CBU: Function unreachable from main: "
|
|
|
|
<< I->getName() << "\n");
|
|
|
|
}
|
2003-11-13 05:05:41 +00:00
|
|
|
calculateSCCGraphs(getOrCreateGraph(*I), Stack, NextID, ValMap);
|
2006-06-16 14:43:36 +00:00
|
|
|
}
|
2003-11-13 05:05:41 +00:00
|
|
|
|
2004-02-08 01:53:10 +00:00
|
|
|
GlobalsGraph->removeTriviallyDeadNodes();
|
2005-03-13 20:15:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Merge the globals variables (not the calls) from the globals graph back
|
|
|
|
// into the main function's graph so that the main function contains all of
|
|
|
|
// the information about global pools and GV usage in the program.
|
2005-03-15 22:10:04 +00:00
|
|
|
if (MainFunc && !MainFunc->isExternal()) {
|
2005-03-13 20:15:06 +00:00
|
|
|
DSGraph &MainGraph = getOrCreateGraph(*MainFunc);
|
|
|
|
const DSGraph &GG = *MainGraph.getGlobalsGraph();
|
2005-04-21 21:13:18 +00:00
|
|
|
ReachabilityCloner RC(MainGraph, GG,
|
2005-03-13 20:15:06 +00:00
|
|
|
DSGraph::DontCloneCallNodes |
|
|
|
|
DSGraph::DontCloneAuxCallNodes);
|
|
|
|
|
|
|
|
// Clone the global nodes into this graph.
|
|
|
|
for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(),
|
|
|
|
E = GG.getScalarMap().global_end(); I != E; ++I)
|
|
|
|
if (isa<GlobalVariable>(*I))
|
|
|
|
RC.getClonedNH(GG.getNodeForValue(*I));
|
|
|
|
|
2005-03-13 20:32:26 +00:00
|
|
|
MainGraph.maskIncompleteMarkers();
|
2005-04-21 21:13:18 +00:00
|
|
|
MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
|
2005-03-13 20:15:06 +00:00
|
|
|
DSGraph::IgnoreGlobals);
|
|
|
|
}
|
|
|
|
|
2003-11-13 05:05:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DSGraph &CompleteBUDataStructures::getOrCreateGraph(Function &F) {
|
|
|
|
// Has the graph already been created?
|
|
|
|
DSGraph *&Graph = DSInfo[&F];
|
|
|
|
if (Graph) return *Graph;
|
|
|
|
|
|
|
|
// Copy the BU graph...
|
Create an equivalence class of global variables that DSA will never be able
to tell apart anyway, and only track the leader for of these equivalence
classes in our graphs.
This dramatically reduces the number of GlobalValue*'s that appear in scalar
maps, which A) reduces memory usage, by eliminating many many scalarmap entries
and B) reduces time for operations that need to execute an operation for each
global in the scalar map.
As an example, this reduces the memory used to analyze 176.gcc from 1GB to
511MB, which (while it's still way too much) is better because it doesn't hit
swap anymore. On eon, this shrinks the local graphs from 14MB to 6.8MB,
shrinks the bu+td graphs of povray from 50M to 40M, shrinks the TD graphs of
130.li from 8.8M to 3.6M, etc.
This change also speeds up DSA on large programs where this makes a big
difference. For example, 130.li goes from 1.17s -> 0.56s, 134.perl goes
from 2.14 -> 0.93s, povray goes from 15.63s->7.99s (!!!).
This also apparently either fixes the problem that caused DSA to crash on
perlbmk and gcc, or it hides it, because DSA now works on these. These
both take entirely too much time in the TD pass (147s for perl, 538s for
gcc, vs 7.67/5.9s in the bu pass for either one), but this is a known
problem that I'll deal with later.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@20696 91177308-0d34-0410-b5e6-96231b3b80d8
2005-03-19 22:23:45 +00:00
|
|
|
Graph = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F), GlobalECs);
|
2003-11-13 05:05:41 +00:00
|
|
|
Graph->setGlobalsGraph(GlobalsGraph);
|
|
|
|
Graph->setPrintAuxCalls();
|
|
|
|
|
|
|
|
// Make sure to update the DSInfo map for all of the functions currently in
|
|
|
|
// this graph!
|
2005-03-15 16:55:04 +00:00
|
|
|
for (DSGraph::retnodes_iterator I = Graph->retnodes_begin();
|
|
|
|
I != Graph->retnodes_end(); ++I)
|
2003-11-13 05:05:41 +00:00
|
|
|
DSInfo[I->first] = Graph;
|
|
|
|
|
|
|
|
return *Graph;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned CompleteBUDataStructures::calculateSCCGraphs(DSGraph &FG,
|
|
|
|
std::vector<DSGraph*> &Stack,
|
2005-04-21 21:13:18 +00:00
|
|
|
unsigned &NextID,
|
2003-11-13 05:05:41 +00:00
|
|
|
hash_map<DSGraph*, unsigned> &ValMap) {
|
|
|
|
assert(!ValMap.count(&FG) && "Shouldn't revisit functions!");
|
|
|
|
unsigned Min = NextID++, MyID = Min;
|
|
|
|
ValMap[&FG] = Min;
|
|
|
|
Stack.push_back(&FG);
|
|
|
|
|
|
|
|
// The edges out of the current node are the call site targets...
|
2005-01-31 00:10:58 +00:00
|
|
|
for (DSGraph::fc_iterator CI = FG.fc_begin(), CE = FG.fc_end();
|
|
|
|
CI != CE; ++CI) {
|
2005-01-30 23:51:02 +00:00
|
|
|
Instruction *Call = CI->getCallSite().getInstruction();
|
2003-11-13 05:05:41 +00:00
|
|
|
|
|
|
|
// Loop over all of the actually called functions...
|
2005-04-02 20:02:41 +00:00
|
|
|
callee_iterator I = callee_begin(Call), E = callee_end(Call);
|
2005-04-02 19:17:18 +00:00
|
|
|
for (; I != E && I->first == Call; ++I) {
|
|
|
|
assert(I->first == Call && "Bad callee construction!");
|
2003-11-13 18:48:11 +00:00
|
|
|
if (!I->second->isExternal()) {
|
|
|
|
DSGraph &Callee = getOrCreateGraph(*I->second);
|
|
|
|
unsigned M;
|
|
|
|
// Have we visited the destination function yet?
|
|
|
|
hash_map<DSGraph*, unsigned>::iterator It = ValMap.find(&Callee);
|
|
|
|
if (It == ValMap.end()) // No, visit it now.
|
|
|
|
M = calculateSCCGraphs(Callee, Stack, NextID, ValMap);
|
|
|
|
else // Yes, get it's number.
|
|
|
|
M = It->second;
|
|
|
|
if (M < Min) Min = M;
|
|
|
|
}
|
2005-04-02 19:17:18 +00:00
|
|
|
}
|
2003-11-13 05:05:41 +00:00
|
|
|
}
|
2003-11-13 01:43:00 +00:00
|
|
|
|
2003-11-13 05:05:41 +00:00
|
|
|
assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!");
|
|
|
|
if (Min != MyID)
|
|
|
|
return Min; // This is part of a larger SCC!
|
2003-11-13 01:43:00 +00:00
|
|
|
|
2003-11-13 05:05:41 +00:00
|
|
|
// If this is a new SCC, process it now.
|
|
|
|
bool IsMultiNodeSCC = false;
|
|
|
|
while (Stack.back() != &FG) {
|
|
|
|
DSGraph *NG = Stack.back();
|
|
|
|
ValMap[NG] = ~0U;
|
|
|
|
|
2005-03-22 00:36:51 +00:00
|
|
|
FG.cloneInto(*NG);
|
2003-11-13 05:05:41 +00:00
|
|
|
|
|
|
|
// Update the DSInfo map and delete the old graph...
|
2005-03-15 16:55:04 +00:00
|
|
|
for (DSGraph::retnodes_iterator I = NG->retnodes_begin();
|
|
|
|
I != NG->retnodes_end(); ++I)
|
2003-11-13 05:05:41 +00:00
|
|
|
DSInfo[I->first] = &FG;
|
2004-10-07 20:01:31 +00:00
|
|
|
|
|
|
|
// Remove NG from the ValMap since the pointer may get recycled.
|
|
|
|
ValMap.erase(NG);
|
2003-11-13 05:05:41 +00:00
|
|
|
delete NG;
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2003-11-13 05:05:41 +00:00
|
|
|
Stack.pop_back();
|
|
|
|
IsMultiNodeSCC = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up the graph before we start inlining a bunch again...
|
|
|
|
if (IsMultiNodeSCC)
|
|
|
|
FG.removeTriviallyDeadNodes();
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2003-11-13 05:05:41 +00:00
|
|
|
Stack.pop_back();
|
|
|
|
processGraph(FG);
|
|
|
|
ValMap[&FG] = ~0U;
|
|
|
|
return MyID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// processGraph - Process the BU graphs for the program in bottom-up order on
|
|
|
|
/// the SCC of the __ACTUAL__ call graph. This builds "complete" BU graphs.
|
|
|
|
void CompleteBUDataStructures::processGraph(DSGraph &G) {
|
2004-03-04 19:16:35 +00:00
|
|
|
hash_set<Instruction*> calls;
|
2004-02-20 23:52:15 +00:00
|
|
|
|
2003-11-13 05:05:41 +00:00
|
|
|
// The edges out of the current node are the call site targets...
|
2005-01-30 23:51:02 +00:00
|
|
|
unsigned i = 0;
|
2005-01-31 00:10:58 +00:00
|
|
|
for (DSGraph::fc_iterator CI = G.fc_begin(), CE = G.fc_end(); CI != CE;
|
2005-01-30 23:51:02 +00:00
|
|
|
++CI, ++i) {
|
|
|
|
const DSCallSite &CS = *CI;
|
2003-11-13 05:05:41 +00:00
|
|
|
Instruction *TheCall = CS.getCallSite().getInstruction();
|
|
|
|
|
2004-03-04 19:16:35 +00:00
|
|
|
assert(calls.insert(TheCall).second &&
|
|
|
|
"Call instruction occurs multiple times in graph??");
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2005-03-18 23:19:47 +00:00
|
|
|
// Fast path for noop calls. Note that we don't care about merging globals
|
|
|
|
// in the callee with nodes in the caller here.
|
|
|
|
if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0)
|
|
|
|
continue;
|
2004-03-04 19:16:35 +00:00
|
|
|
|
2004-05-23 08:00:34 +00:00
|
|
|
// Loop over all of the potentially called functions...
|
|
|
|
// Inline direct calls as well as indirect calls because the direct
|
|
|
|
// callee may have indirect callees and so may have changed.
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2005-04-02 20:02:41 +00:00
|
|
|
callee_iterator I = callee_begin(TheCall),E = callee_end(TheCall);
|
2005-04-02 19:17:18 +00:00
|
|
|
unsigned TNum = 0, Num = 0;
|
|
|
|
DEBUG(Num = std::distance(I, E));
|
2004-05-23 08:00:34 +00:00
|
|
|
for (; I != E; ++I, ++TNum) {
|
2005-04-02 19:17:18 +00:00
|
|
|
assert(I->first == TheCall && "Bad callee construction!");
|
2004-05-23 08:00:34 +00:00
|
|
|
Function *CalleeFunc = I->second;
|
|
|
|
if (!CalleeFunc->isExternal()) {
|
|
|
|
// Merge the callee's graph into this graph. This works for normal
|
|
|
|
// calls or for self recursion within an SCC.
|
|
|
|
DSGraph &GI = getOrCreateGraph(*CalleeFunc);
|
|
|
|
++NumCBUInlines;
|
2005-04-21 21:13:18 +00:00
|
|
|
G.mergeInGraph(CS, *CalleeFunc, GI,
|
2004-05-23 08:00:34 +00:00
|
|
|
DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes |
|
|
|
|
DSGraph::DontCloneAuxCallNodes);
|
2005-01-30 23:51:02 +00:00
|
|
|
DEBUG(std::cerr << " Inlining graph [" << i << "/"
|
|
|
|
<< G.getFunctionCalls().size()-1
|
2004-05-23 08:00:34 +00:00
|
|
|
<< ":" << TNum << "/" << Num-1 << "] for "
|
|
|
|
<< CalleeFunc->getName() << "["
|
|
|
|
<< GI.getGraphSize() << "+" << GI.getAuxFunctionCalls().size()
|
|
|
|
<< "] into '" /*<< G.getFunctionNames()*/ << "' ["
|
|
|
|
<< G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
|
|
|
|
<< "]\n");
|
2003-11-13 05:05:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recompute the Incomplete markers
|
|
|
|
G.maskIncompleteMarkers();
|
|
|
|
G.markIncompleteNodes(DSGraph::MarkFormalArgs);
|
|
|
|
|
|
|
|
// Delete dead nodes. Treat globals that are unreachable but that can
|
|
|
|
// reach live nodes as live.
|
2004-03-04 21:36:57 +00:00
|
|
|
G.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
|
2003-11-13 01:43:00 +00:00
|
|
|
}
|