2004-05-23 07:54:02 +00:00
|
|
|
//===- EquivClassGraphs.cpp - Merge equiv-class graphs & inline bottom-up -===//
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2004-05-23 07:54:02 +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
|
|
|
//
|
2004-05-23 07:54:02 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass is the same as the complete bottom-up graphs, but
|
|
|
|
// with functions partitioned into equivalence classes and a single merged
|
|
|
|
// DS graph for all functions in an equivalence class. After this merging,
|
|
|
|
// graphs are inlined bottom-up on the SCCs of the final (CBU) call graph.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "ECGraphs"
|
2005-04-02 20:08:17 +00:00
|
|
|
#include "llvm/Analysis/DataStructure/DataStructure.h"
|
2005-03-15 17:14:09 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2004-05-23 07:54:02 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
2004-07-07 06:22:54 +00:00
|
|
|
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
2004-05-23 07:54:02 +00:00
|
|
|
#include "llvm/Support/CallSite.h"
|
2004-10-11 20:53:28 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/ADT/SCCIterator.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/EquivalenceClasses.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2004-05-23 07:54:02 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2005-03-13 19:51:24 +00:00
|
|
|
RegisterAnalysis<EquivClassGraphs> X("eqdatastructure",
|
2004-05-23 07:54:02 +00:00
|
|
|
"Equivalence-class Bottom-up Data Structure Analysis");
|
2004-10-31 21:56:11 +00:00
|
|
|
Statistic<> NumEquivBUInlines("equivdatastructures",
|
|
|
|
"Number of graphs inlined");
|
2004-10-12 16:52:09 +00:00
|
|
|
Statistic<> NumFoldGraphInlines("Inline equiv-class graphs bottom up",
|
|
|
|
"Number of graphs inlined");
|
2004-05-23 07:54:02 +00:00
|
|
|
}
|
|
|
|
|
2004-10-31 23:41:26 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
template<typename GT>
|
|
|
|
static void CheckAllGraphs(Module *M, GT &ECGraphs) {
|
|
|
|
DSGraph &GG = ECGraphs.getGlobalsGraph();
|
|
|
|
|
|
|
|
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
if (!I->isExternal()) {
|
|
|
|
DSGraph &G = ECGraphs.getDSGraph(*I);
|
2005-03-15 16:55:04 +00:00
|
|
|
if (G.retnodes_begin()->first != I)
|
2005-03-14 19:22:47 +00:00
|
|
|
continue; // Only check a graph once.
|
2004-10-31 23:41:26 +00:00
|
|
|
|
|
|
|
DSGraph::NodeMapTy GlobalsGraphNodeMapping;
|
2005-03-15 00:58:16 +00:00
|
|
|
G.computeGToGGMapping(GlobalsGraphNodeMapping);
|
2005-04-21 21:13:18 +00:00
|
|
|
}
|
2004-10-31 23:41:26 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-11-01 21:07:05 +00:00
|
|
|
// getSomeCalleeForCallSite - Return any one callee function at a call site.
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2005-03-13 19:51:24 +00:00
|
|
|
Function *EquivClassGraphs::getSomeCalleeForCallSite(const CallSite &CS) const{
|
2004-05-23 07:54:02 +00:00
|
|
|
Function *thisFunc = CS.getCaller();
|
2004-11-01 21:07:05 +00:00
|
|
|
assert(thisFunc && "getSomeCalleeForCallSite(): Not a valid call site?");
|
2004-10-31 21:56:11 +00:00
|
|
|
DSGraph &DSG = getDSGraph(*thisFunc);
|
|
|
|
DSNode *calleeNode = DSG.getNodeForValue(CS.getCalledValue()).getNode();
|
2004-05-23 07:54:02 +00:00
|
|
|
std::map<DSNode*, Function *>::const_iterator I =
|
|
|
|
OneCalledFunction.find(calleeNode);
|
|
|
|
return (I == OneCalledFunction.end())? NULL : I->second;
|
|
|
|
}
|
|
|
|
|
2004-10-31 21:56:11 +00:00
|
|
|
// runOnModule - Calculate the bottom up data structure graphs for each function
|
|
|
|
// in the program.
|
2004-05-23 07:54:02 +00:00
|
|
|
//
|
2005-03-12 12:08:52 +00:00
|
|
|
bool EquivClassGraphs::runOnModule(Module &M) {
|
2004-05-23 07:54:02 +00:00
|
|
|
CBU = &getAnalysis<CompleteBUDataStructures>();
|
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
|
|
|
GlobalECs = CBU->getGlobalECs();
|
2004-11-11 22:11:17 +00:00
|
|
|
DEBUG(CheckAllGraphs(&M, *CBU));
|
2004-05-23 07:54:02 +00:00
|
|
|
|
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(CBU->getGlobalsGraph(), GlobalECs);
|
2004-10-31 21:56:11 +00:00
|
|
|
GlobalsGraph->setPrintAuxCalls();
|
|
|
|
|
2004-10-31 23:41:26 +00:00
|
|
|
ActualCallees = CBU->getActualCallees();
|
|
|
|
|
2004-05-23 07:54:02 +00:00
|
|
|
// Find equivalence classes of functions called from common call sites.
|
|
|
|
// Fold the CBU graphs for all functions in an equivalence class.
|
|
|
|
buildIndirectFunctionSets(M);
|
|
|
|
|
|
|
|
// Stack of functions used for Tarjan's SCC-finding algorithm.
|
2004-11-02 17:51:11 +00:00
|
|
|
std::vector<DSGraph*> Stack;
|
|
|
|
std::map<DSGraph*, unsigned> ValMap;
|
2004-05-23 07:54:02 +00:00
|
|
|
unsigned NextID = 1;
|
|
|
|
|
2005-03-13 20:15:06 +00:00
|
|
|
Function *MainFunc = M.getMainFunction();
|
|
|
|
if (MainFunc && !MainFunc->isExternal()) {
|
|
|
|
processSCC(getOrCreateGraph(*MainFunc), Stack, NextID, ValMap);
|
2004-05-23 07:54:02 +00:00
|
|
|
} else {
|
|
|
|
std::cerr << "Fold Graphs: No 'main' function found!\n";
|
|
|
|
}
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2004-05-23 07:54:02 +00:00
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
2004-11-02 17:51:11 +00:00
|
|
|
if (!I->isExternal())
|
|
|
|
processSCC(getOrCreateGraph(*I), Stack, NextID, ValMap);
|
2004-05-23 07:54:02 +00:00
|
|
|
|
2004-10-31 23:41:26 +00:00
|
|
|
DEBUG(CheckAllGraphs(&M, *this));
|
|
|
|
|
2004-05-23 07:54:02 +00:00
|
|
|
getGlobalsGraph().removeTriviallyDeadNodes();
|
2005-03-15 22:47:18 +00:00
|
|
|
getGlobalsGraph().markIncompleteNodes(DSGraph::IgnoreGlobals);
|
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);
|
|
|
|
}
|
|
|
|
|
2005-03-15 17:14:09 +00:00
|
|
|
// Final processing. Note that dead node elimination may actually remove
|
|
|
|
// globals from a function graph that are immediately used. If there are no
|
|
|
|
// scalars pointing to the node (e.g. because the only use is a direct store
|
|
|
|
// to a scalar global) we have to make sure to rematerialize the globals back
|
|
|
|
// into the graphs here, or clients will break!
|
|
|
|
for (Module::global_iterator GI = M.global_begin(), E = M.global_end();
|
|
|
|
GI != E; ++GI)
|
|
|
|
// This only happens to first class typed globals.
|
|
|
|
if (GI->getType()->getElementType()->isFirstClassType())
|
|
|
|
for (Value::use_iterator UI = GI->use_begin(), E = GI->use_end();
|
|
|
|
UI != E; ++UI)
|
|
|
|
// This only happens to direct uses by instructions.
|
|
|
|
if (Instruction *User = dyn_cast<Instruction>(*UI)) {
|
|
|
|
DSGraph &DSG = getOrCreateGraph(*User->getParent()->getParent());
|
|
|
|
if (!DSG.getScalarMap().count(GI)) {
|
|
|
|
// If this global does not exist in the graph, but it is immediately
|
|
|
|
// used by an instruction in the graph, clone it over from the
|
|
|
|
// globals graph.
|
|
|
|
ReachabilityCloner RC(DSG, *GlobalsGraph, 0);
|
|
|
|
RC.getClonedNH(GlobalsGraph->getNodeForValue(GI));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-31 21:56:11 +00:00
|
|
|
return false;
|
2004-05-23 07:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// buildIndirectFunctionSets - Iterate over the module looking for indirect
|
|
|
|
// calls to functions. If a call site can invoke any functions [F1, F2... FN],
|
|
|
|
// unify the N functions together in the FuncECs set.
|
|
|
|
//
|
2005-03-12 12:08:52 +00:00
|
|
|
void EquivClassGraphs::buildIndirectFunctionSets(Module &M) {
|
2004-05-23 07:54:02 +00:00
|
|
|
const ActualCalleesTy& AC = CBU->getActualCallees();
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2004-05-23 07:54:02 +00:00
|
|
|
// Loop over all of the indirect calls in the program. If a call site can
|
|
|
|
// call multiple different functions, we need to unify all of the callees into
|
|
|
|
// the same equivalence class.
|
|
|
|
Instruction *LastInst = 0;
|
|
|
|
Function *FirstFunc = 0;
|
|
|
|
for (ActualCalleesTy::const_iterator I=AC.begin(), E=AC.end(); I != E; ++I) {
|
|
|
|
if (I->second->isExternal())
|
|
|
|
continue; // Ignore functions we cannot modify
|
|
|
|
|
|
|
|
CallSite CS = CallSite::get(I->first);
|
|
|
|
|
|
|
|
if (CS.getCalledFunction()) { // Direct call:
|
2005-03-19 05:15:27 +00:00
|
|
|
FuncECs.insert(I->second); // -- Make sure function has equiv class
|
2004-05-23 07:54:02 +00:00
|
|
|
FirstFunc = I->second; // -- First callee at this site
|
|
|
|
} else { // Else indirect call
|
|
|
|
// DEBUG(std::cerr << "CALLEE: " << I->second->getName()
|
|
|
|
// << " from : " << I->first);
|
|
|
|
if (I->first != LastInst) {
|
|
|
|
// This is the first callee from this call site.
|
|
|
|
LastInst = I->first;
|
|
|
|
FirstFunc = I->second;
|
2005-04-22 04:01:18 +00:00
|
|
|
// Instead of storing the lastInst For Indirection call Sites we store
|
|
|
|
// the DSNode for the function ptr arguemnt
|
|
|
|
Function *thisFunc = LastInst->getParent()->getParent();
|
2004-10-12 16:52:09 +00:00
|
|
|
DSGraph &TFG = CBU->getDSGraph(*thisFunc);
|
2005-04-22 04:01:18 +00:00
|
|
|
DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
|
2004-05-23 07:54:02 +00:00
|
|
|
OneCalledFunction[calleeNode] = FirstFunc;
|
2005-03-19 05:15:27 +00:00
|
|
|
FuncECs.insert(I->second);
|
2004-05-23 07:54:02 +00:00
|
|
|
} else {
|
|
|
|
// This is not the first possible callee from a particular call site.
|
|
|
|
// Union the callee in with the other functions.
|
2005-03-19 05:15:27 +00:00
|
|
|
FuncECs.unionSets(FirstFunc, I->second);
|
2004-05-23 07:54:02 +00:00
|
|
|
#ifndef NDEBUG
|
2005-04-22 04:01:18 +00:00
|
|
|
Function *thisFunc = LastInst->getParent()->getParent();
|
2004-10-12 16:52:09 +00:00
|
|
|
DSGraph &TFG = CBU->getDSGraph(*thisFunc);
|
2005-04-22 04:01:18 +00:00
|
|
|
DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
|
2004-05-23 07:54:02 +00:00
|
|
|
assert(OneCalledFunction.count(calleeNode) > 0 && "Missed a call?");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now include all functions that share a graph with any function in the
|
|
|
|
// equivalence class. More precisely, if F is in the class, and G(F) is
|
|
|
|
// its graph, then we include all other functions that are also in G(F).
|
|
|
|
// Currently, that is just the functions in the same call-graph-SCC as F.
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2004-05-23 07:54:02 +00:00
|
|
|
DSGraph& funcDSGraph = CBU->getDSGraph(*I->second);
|
2005-03-15 16:55:04 +00:00
|
|
|
for (DSGraph::retnodes_iterator RI = funcDSGraph.retnodes_begin(),
|
|
|
|
RE = funcDSGraph.retnodes_end(); RI != RE; ++RI)
|
2005-03-19 05:15:27 +00:00
|
|
|
FuncECs.unionSets(FirstFunc, RI->first);
|
2004-05-23 07:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now that all of the equivalences have been built, merge the graphs for
|
|
|
|
// each equivalence class.
|
|
|
|
//
|
|
|
|
DEBUG(std::cerr << "\nIndirect Function Equivalence Sets:\n");
|
2005-03-19 05:15:27 +00:00
|
|
|
for (EquivalenceClasses<Function*>::iterator EQSI = FuncECs.begin(), E =
|
|
|
|
FuncECs.end(); EQSI != E; ++EQSI) {
|
|
|
|
if (!EQSI->isLeader()) continue;
|
2004-05-23 07:54:02 +00:00
|
|
|
|
2005-03-19 05:15:27 +00:00
|
|
|
EquivalenceClasses<Function*>::member_iterator SI =
|
|
|
|
FuncECs.member_begin(EQSI);
|
|
|
|
assert(SI != FuncECs.member_end() && "Empty equiv set??");
|
|
|
|
EquivalenceClasses<Function*>::member_iterator SN = SI;
|
|
|
|
++SN;
|
|
|
|
if (SN == FuncECs.member_end())
|
|
|
|
continue; // Single function equivalence set, no merging to do.
|
|
|
|
|
|
|
|
Function* LF = *SI;
|
2004-05-23 07:54:02 +00:00
|
|
|
|
2004-11-02 06:38:58 +00:00
|
|
|
#ifndef NDEBUG
|
2005-03-19 05:15:27 +00:00
|
|
|
DEBUG(std::cerr <<" Equivalence set for leader " << LF->getName() <<" = ");
|
|
|
|
for (SN = SI; SN != FuncECs.member_end(); ++SN)
|
|
|
|
DEBUG(std::cerr << " " << (*SN)->getName() << "," );
|
|
|
|
DEBUG(std::cerr << "\n");
|
2004-05-23 07:54:02 +00:00
|
|
|
#endif
|
|
|
|
|
2005-03-19 05:15:27 +00:00
|
|
|
// This equiv class has multiple functions: merge their graphs. First,
|
|
|
|
// clone the CBU graph for the leader and make it the common graph for the
|
|
|
|
// equivalence graph.
|
|
|
|
DSGraph &MergedG = getOrCreateGraph(*LF);
|
2004-05-23 07:54:02 +00:00
|
|
|
|
2005-03-19 05:15:27 +00:00
|
|
|
// Record the argument nodes for use in merging later below.
|
2005-04-21 21:13:18 +00:00
|
|
|
std::vector<DSNodeHandle> ArgNodes;
|
2004-11-01 20:37:00 +00:00
|
|
|
|
2005-03-19 05:15:27 +00:00
|
|
|
for (Function::arg_iterator AI = LF->arg_begin(), E = LF->arg_end();
|
|
|
|
AI != E; ++AI)
|
|
|
|
if (DS::isPointerType(AI->getType()))
|
|
|
|
ArgNodes.push_back(MergedG.getNodeForValue(AI));
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2005-03-19 05:15:27 +00:00
|
|
|
// Merge in the graphs of all other functions in this equiv. class. Note
|
|
|
|
// that two or more functions may have the same graph, and it only needs
|
|
|
|
// to be merged in once.
|
|
|
|
std::set<DSGraph*> GraphsMerged;
|
|
|
|
GraphsMerged.insert(&CBU->getDSGraph(*LF));
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2005-03-19 05:15:27 +00:00
|
|
|
for (++SI; SI != FuncECs.member_end(); ++SI) {
|
|
|
|
Function *F = *SI;
|
|
|
|
DSGraph *&FG = DSInfo[F];
|
2005-04-21 21:13:18 +00:00
|
|
|
|
|
|
|
DSGraph &CBUGraph = CBU->getDSGraph(*F);
|
2005-03-19 05:15:27 +00:00
|
|
|
if (GraphsMerged.insert(&CBUGraph).second) {
|
2004-05-23 07:54:02 +00:00
|
|
|
// Record the "folded" graph for the function.
|
2005-03-15 16:55:04 +00:00
|
|
|
for (DSGraph::retnodes_iterator I = CBUGraph.retnodes_begin(),
|
|
|
|
E = CBUGraph.retnodes_end(); I != E; ++I) {
|
2004-11-02 20:31:06 +00:00
|
|
|
assert(DSInfo[I->first] == 0 && "Graph already exists for Fn!");
|
|
|
|
DSInfo[I->first] = &MergedG;
|
|
|
|
}
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2004-11-02 06:38:58 +00:00
|
|
|
// Clone this member of the equivalence class into MergedG.
|
2005-03-22 00:36:51 +00:00
|
|
|
MergedG.cloneInto(CBUGraph);
|
2004-05-23 07:54:02 +00:00
|
|
|
}
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2005-03-19 05:15:27 +00:00
|
|
|
// Merge the return nodes of all functions together.
|
|
|
|
MergedG.getReturnNodes()[LF].mergeWith(MergedG.getReturnNodes()[F]);
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2005-03-19 05:15:27 +00:00
|
|
|
// Merge the function arguments with all argument nodes found so far.
|
|
|
|
// If there are extra function args, add them to the vector of argNodes
|
|
|
|
Function::arg_iterator AI2 = F->arg_begin(), AI2end = F->arg_end();
|
|
|
|
for (unsigned arg = 0, numArgs = ArgNodes.size();
|
|
|
|
arg != numArgs && AI2 != AI2end; ++AI2, ++arg)
|
|
|
|
if (DS::isPointerType(AI2->getType()))
|
|
|
|
ArgNodes[arg].mergeWith(MergedG.getNodeForValue(AI2));
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2005-03-19 05:15:27 +00:00
|
|
|
for ( ; AI2 != AI2end; ++AI2)
|
|
|
|
if (DS::isPointerType(AI2->getType()))
|
|
|
|
ArgNodes.push_back(MergedG.getNodeForValue(AI2));
|
|
|
|
DEBUG(MergedG.AssertGraphOK());
|
2004-05-23 07:54:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
DEBUG(std::cerr << "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-12 12:08:52 +00:00
|
|
|
DSGraph &EquivClassGraphs::getOrCreateGraph(Function &F) {
|
2004-05-23 07:54:02 +00:00
|
|
|
// Has the graph already been created?
|
2004-11-01 21:02:23 +00:00
|
|
|
DSGraph *&Graph = DSInfo[&F];
|
2004-05-23 07:54:02 +00:00
|
|
|
if (Graph) return *Graph;
|
|
|
|
|
|
|
|
DSGraph &CBUGraph = CBU->getDSGraph(F);
|
|
|
|
|
|
|
|
// Copy the CBU 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(CBUGraph, GlobalECs); // updates the map via reference
|
2004-05-23 07:54:02 +00:00
|
|
|
Graph->setGlobalsGraph(&getGlobalsGraph());
|
|
|
|
Graph->setPrintAuxCalls();
|
|
|
|
|
2004-11-01 21:02:23 +00:00
|
|
|
// Make sure to update the DSInfo map for all functions in the graph!
|
2005-03-15 16:55:04 +00:00
|
|
|
for (DSGraph::retnodes_iterator I = Graph->retnodes_begin();
|
|
|
|
I != Graph->retnodes_end(); ++I)
|
2004-05-23 07:54:02 +00:00
|
|
|
if (I->first != &F) {
|
2004-11-01 21:02:23 +00:00
|
|
|
DSGraph *&FG = DSInfo[I->first];
|
2004-11-02 06:38:58 +00:00
|
|
|
assert(FG == 0 && "Merging function in SCC twice?");
|
2004-05-23 07:54:02 +00:00
|
|
|
FG = Graph;
|
|
|
|
}
|
|
|
|
|
2004-11-01 19:54:06 +00:00
|
|
|
return *Graph;
|
2004-05-23 07:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-12 12:08:52 +00:00
|
|
|
unsigned EquivClassGraphs::
|
2005-04-21 21:13:18 +00:00
|
|
|
processSCC(DSGraph &FG, std::vector<DSGraph*> &Stack, unsigned &NextID,
|
2004-11-02 17:51:11 +00:00
|
|
|
std::map<DSGraph*, unsigned> &ValMap) {
|
|
|
|
std::map<DSGraph*, unsigned>::iterator It = ValMap.lower_bound(&FG);
|
|
|
|
if (It != ValMap.end() && It->first == &FG)
|
2004-10-31 23:01:34 +00:00
|
|
|
return It->second;
|
|
|
|
|
2004-11-02 17:51:11 +00:00
|
|
|
DEBUG(std::cerr << " ProcessSCC for function " << FG.getFunctionNames()
|
|
|
|
<< "\n");
|
|
|
|
|
2004-05-23 07:54:02 +00:00
|
|
|
unsigned Min = NextID++, MyID = Min;
|
2004-11-02 17:51:11 +00:00
|
|
|
ValMap[&FG] = Min;
|
|
|
|
Stack.push_back(&FG);
|
2004-05-23 07:54:02 +00:00
|
|
|
|
|
|
|
// The edges out of the current node are the call site targets...
|
2005-03-15 06:29:12 +00:00
|
|
|
for (DSGraph::fc_iterator CI = FG.fc_begin(), CE = FG.fc_end();
|
|
|
|
CI != CE; ++CI) {
|
2005-01-30 23:51:25 +00:00
|
|
|
Instruction *Call = CI->getCallSite().getInstruction();
|
2004-05-23 07:54:02 +00:00
|
|
|
|
|
|
|
// Loop over all of the actually called functions...
|
2005-04-02 20:02:41 +00:00
|
|
|
for (callee_iterator I = callee_begin(Call), E = callee_end(Call);
|
|
|
|
I != E; ++I)
|
2004-05-23 07:54:02 +00:00
|
|
|
if (!I->second->isExternal()) {
|
2004-10-31 23:41:26 +00:00
|
|
|
// Process the callee as necessary.
|
2004-11-02 17:51:11 +00:00
|
|
|
unsigned M = processSCC(getOrCreateGraph(*I->second),
|
2004-10-31 23:41:26 +00:00
|
|
|
Stack, NextID, ValMap);
|
2004-05-23 07:54:02 +00:00
|
|
|
if (M < Min) Min = M;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-02 17:51:11 +00:00
|
|
|
assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!");
|
2004-05-23 07:54:02 +00:00
|
|
|
if (Min != MyID)
|
|
|
|
return Min; // This is part of a larger SCC!
|
|
|
|
|
|
|
|
// If this is a new SCC, process it now.
|
2004-11-02 19:29:59 +00:00
|
|
|
bool MergedGraphs = false;
|
2004-11-02 17:51:11 +00:00
|
|
|
while (Stack.back() != &FG) {
|
|
|
|
DSGraph *NG = Stack.back();
|
|
|
|
ValMap[NG] = ~0U;
|
2004-05-23 07:54:02 +00:00
|
|
|
|
2004-11-02 19:29:59 +00:00
|
|
|
// If the SCC found is not the same as those found in CBU, make sure to
|
|
|
|
// merge the graphs as appropriate.
|
2005-03-22 00:36:51 +00:00
|
|
|
FG.cloneInto(*NG);
|
2004-11-02 19:29:59 +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)
|
2004-11-02 19:29:59 +00:00
|
|
|
DSInfo[I->first] = &FG;
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2004-11-02 19:29:59 +00:00
|
|
|
// Remove NG from the ValMap since the pointer may get recycled.
|
|
|
|
ValMap.erase(NG);
|
|
|
|
delete NG;
|
|
|
|
MergedGraphs = true;
|
2004-05-23 07:54:02 +00:00
|
|
|
Stack.pop_back();
|
|
|
|
}
|
|
|
|
|
2004-11-02 19:29:59 +00:00
|
|
|
// Clean up the graph before we start inlining a bunch again.
|
|
|
|
if (MergedGraphs)
|
|
|
|
FG.removeTriviallyDeadNodes();
|
|
|
|
|
2004-05-23 07:54:02 +00:00
|
|
|
Stack.pop_back();
|
2004-10-31 17:47:48 +00:00
|
|
|
|
2004-11-02 17:51:11 +00:00
|
|
|
processGraph(FG);
|
|
|
|
ValMap[&FG] = ~0U;
|
2004-05-23 07:54:02 +00:00
|
|
|
return MyID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// processGraph - Process the CBU graphs for the program in bottom-up order on
|
|
|
|
/// the SCC of the __ACTUAL__ call graph. This builds final folded CBU graphs.
|
2005-03-12 12:08:52 +00:00
|
|
|
void EquivClassGraphs::processGraph(DSGraph &G) {
|
2004-11-02 17:51:11 +00:00
|
|
|
DEBUG(std::cerr << " ProcessGraph for function "
|
|
|
|
<< G.getFunctionNames() << "\n");
|
2004-05-23 07:54:02 +00:00
|
|
|
|
|
|
|
hash_set<Instruction*> calls;
|
|
|
|
|
|
|
|
// Else we need to inline some callee graph. Visit all call sites.
|
|
|
|
// The edges out of the current node are the call site targets...
|
2005-01-30 23:51:25 +00:00
|
|
|
unsigned i = 0;
|
2005-03-15 06:29:12 +00:00
|
|
|
for (DSGraph::fc_iterator CI = G.fc_begin(), CE = G.fc_end(); CI != CE;
|
2005-01-30 23:51:25 +00:00
|
|
|
++CI, ++i) {
|
|
|
|
const DSCallSite &CS = *CI;
|
2004-05-23 07:54:02 +00:00
|
|
|
Instruction *TheCall = CS.getCallSite().getInstruction();
|
|
|
|
|
|
|
|
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
|
|
|
if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0)
|
|
|
|
continue;
|
|
|
|
|
2004-05-23 07:54:02 +00:00
|
|
|
// Inline the common callee graph into the current graph, if the callee
|
|
|
|
// graph has not changed. Note that all callees should have the same
|
|
|
|
// graph so we only need to do this once.
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2004-05-23 07:54:02 +00:00
|
|
|
DSGraph* CalleeGraph = NULL;
|
2005-04-02 20:02:41 +00:00
|
|
|
callee_iterator I = callee_begin(TheCall), E = callee_end(TheCall);
|
2004-05-23 07:54:02 +00:00
|
|
|
unsigned TNum, Num;
|
|
|
|
|
|
|
|
// Loop over all potential callees to find the first non-external callee.
|
|
|
|
for (TNum = 0, Num = std::distance(I, E); I != E; ++I, ++TNum)
|
|
|
|
if (!I->second->isExternal())
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Now check if the graph has changed and if so, clone and inline it.
|
2004-10-31 21:56:11 +00:00
|
|
|
if (I != E) {
|
2004-05-23 07:54:02 +00:00
|
|
|
Function *CalleeFunc = I->second;
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2004-05-23 07:54:02 +00:00
|
|
|
// Merge the callee's graph into this graph, if not already the same.
|
|
|
|
// Callees in the same equivalence class (which subsumes those
|
|
|
|
// in the same SCCs) have the same graph. Note that all recursion
|
|
|
|
// including self-recursion have been folded in the equiv classes.
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2004-05-23 07:54:02 +00:00
|
|
|
CalleeGraph = &getOrCreateGraph(*CalleeFunc);
|
2004-11-02 17:51:11 +00:00
|
|
|
if (CalleeGraph != &G) {
|
2004-05-23 07:54:02 +00:00
|
|
|
++NumFoldGraphInlines;
|
2004-11-02 17:51:11 +00:00
|
|
|
G.mergeInGraph(CS, *CalleeFunc, *CalleeGraph,
|
2005-03-24 18:42:51 +00:00
|
|
|
DSGraph::StripAllocaBit |
|
2004-11-02 17:51:11 +00:00
|
|
|
DSGraph::DontCloneCallNodes |
|
|
|
|
DSGraph::DontCloneAuxCallNodes);
|
2005-01-30 23:51:25 +00:00
|
|
|
DEBUG(std::cerr << " Inlining graph [" << i << "/"
|
|
|
|
<< G.getFunctionCalls().size()-1
|
2004-05-23 07:54:02 +00:00
|
|
|
<< ":" << TNum << "/" << Num-1 << "] for "
|
|
|
|
<< CalleeFunc->getName() << "["
|
|
|
|
<< CalleeGraph->getGraphSize() << "+"
|
|
|
|
<< CalleeGraph->getAuxFunctionCalls().size()
|
2004-11-02 17:51:11 +00:00
|
|
|
<< "] into '" /*<< G.getFunctionNames()*/ << "' ["
|
|
|
|
<< G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
|
2004-05-23 07:54:02 +00:00
|
|
|
<< "]\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Now loop over the rest of the callees and make sure they have the
|
|
|
|
// same graph as the one inlined above.
|
|
|
|
if (CalleeGraph)
|
|
|
|
for (++I, ++TNum; I != E; ++I, ++TNum)
|
|
|
|
if (!I->second->isExternal())
|
|
|
|
assert(CalleeGraph == &getOrCreateGraph(*I->second) &&
|
|
|
|
"Callees at a call site have different graphs?");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-10-31 23:41:26 +00:00
|
|
|
// Recompute the Incomplete markers.
|
2004-11-02 17:51:11 +00:00
|
|
|
G.maskIncompleteMarkers();
|
|
|
|
G.markIncompleteNodes(DSGraph::MarkFormalArgs);
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2004-10-31 23:41:26 +00:00
|
|
|
// Delete dead nodes. Treat globals that are unreachable but that can
|
|
|
|
// reach live nodes as live.
|
2004-11-02 17:51:11 +00:00
|
|
|
G.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
|
2004-10-31 21:56:11 +00:00
|
|
|
|
|
|
|
// When this graph is finalized, clone the globals in the graph into the
|
|
|
|
// globals graph to make sure it has everything, from all graphs.
|
2004-11-02 17:51:11 +00:00
|
|
|
ReachabilityCloner RC(*G.getGlobalsGraph(), G, DSGraph::StripAllocaBit);
|
2004-10-31 21:56:11 +00:00
|
|
|
|
|
|
|
// Clone everything reachable from globals in the function graph into the
|
|
|
|
// globals graph.
|
2004-11-02 17:51:11 +00:00
|
|
|
DSScalarMap &MainSM = G.getScalarMap();
|
2004-10-31 21:56:11 +00:00
|
|
|
for (DSScalarMap::global_iterator I = MainSM.global_begin(),
|
2005-04-21 21:13:18 +00:00
|
|
|
E = MainSM.global_end(); I != E; ++I)
|
2004-10-31 21:56:11 +00:00
|
|
|
RC.getClonedNH(MainSM[*I]);
|
|
|
|
|
2004-10-31 23:41:26 +00:00
|
|
|
DEBUG(std::cerr << " -- DONE ProcessGraph for function "
|
2004-11-02 17:51:11 +00:00
|
|
|
<< G.getFunctionNames() << "\n");
|
2004-05-23 07:54:02 +00:00
|
|
|
}
|