2004-05-23 07:54:02 +00:00
|
|
|
//===- EquivClassGraphs.cpp - Merge equiv-class graphs & inline bottom-up -===//
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
#include "EquivClassGraphs.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
2004-07-07 06:22:54 +00:00
|
|
|
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
|
|
|
#include "llvm/Analysis/DataStructure/DataStructure.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 {
|
2004-10-12 16:52:09 +00:00
|
|
|
RegisterAnalysis<PA::EquivClassGraphs> X("equivdatastructure",
|
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);
|
|
|
|
|
|
|
|
DSGraph::NodeMapTy GlobalsGraphNodeMapping;
|
|
|
|
for (DSScalarMap::global_iterator I = G.getScalarMap().global_begin(),
|
|
|
|
E = G.getScalarMap().global_end(); I != E; ++I)
|
|
|
|
DSGraph::computeNodeMapping(G.getNodeForValue(*I),
|
|
|
|
GG.getNodeForValue(*I),
|
|
|
|
GlobalsGraphNodeMapping);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-11-01 21:07:05 +00:00
|
|
|
// getSomeCalleeForCallSite - Return any one callee function at a call site.
|
2004-05-23 07:54:02 +00:00
|
|
|
//
|
2004-10-12 16:52:09 +00:00
|
|
|
Function *PA::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
|
|
|
//
|
2004-10-31 21:56:11 +00:00
|
|
|
bool PA::EquivClassGraphs::runOnModule(Module &M) {
|
2004-05-23 07:54:02 +00:00
|
|
|
CBU = &getAnalysis<CompleteBUDataStructures>();
|
2004-11-11 22:11:17 +00:00
|
|
|
DEBUG(CheckAllGraphs(&M, *CBU));
|
2004-05-23 07:54:02 +00:00
|
|
|
|
2004-10-31 21:56:11 +00:00
|
|
|
GlobalsGraph = new DSGraph(CBU->getGlobalsGraph());
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (Function *Main = M.getMainFunction()) {
|
|
|
|
if (!Main->isExternal())
|
2004-11-02 17:51:11 +00:00
|
|
|
processSCC(getOrCreateGraph(*Main), Stack, NextID, ValMap);
|
2004-05-23 07:54:02 +00:00
|
|
|
} else {
|
|
|
|
std::cerr << "Fold Graphs: No 'main' function found!\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
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.
|
|
|
|
//
|
2004-10-12 16:52:09 +00:00
|
|
|
void PA::EquivClassGraphs::buildIndirectFunctionSets(Module &M) {
|
2004-05-23 07:54:02 +00:00
|
|
|
const ActualCalleesTy& AC = CBU->getActualCallees();
|
2004-11-01 20:37:00 +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:
|
|
|
|
FuncECs.addElement(I->second); // -- Make sure function has equiv class
|
|
|
|
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;
|
|
|
|
// 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);
|
|
|
|
DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
|
2004-05-23 07:54:02 +00:00
|
|
|
OneCalledFunction[calleeNode] = FirstFunc;
|
|
|
|
FuncECs.addElement(I->second);
|
|
|
|
} else {
|
|
|
|
// This is not the first possible callee from a particular call site.
|
|
|
|
// Union the callee in with the other functions.
|
|
|
|
FuncECs.unionSetsWith(FirstFunc, I->second);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
Function *thisFunc = LastInst->getParent()->getParent();
|
2004-10-12 16:52:09 +00:00
|
|
|
DSGraph &TFG = CBU->getDSGraph(*thisFunc);
|
|
|
|
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.
|
|
|
|
//
|
|
|
|
DSGraph& funcDSGraph = CBU->getDSGraph(*I->second);
|
|
|
|
const DSGraph::ReturnNodesTy &RetNodes = funcDSGraph.getReturnNodes();
|
|
|
|
for (DSGraph::ReturnNodesTy::const_iterator RI=RetNodes.begin(),
|
|
|
|
RE=RetNodes.end(); RI != RE; ++RI)
|
|
|
|
FuncECs.unionSetsWith(FirstFunc, RI->first);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that all of the equivalences have been built, merge the graphs for
|
|
|
|
// each equivalence class.
|
|
|
|
//
|
|
|
|
std::set<Function*> &leaderSet = FuncECs.getLeaderSet();
|
|
|
|
DEBUG(std::cerr << "\nIndirect Function Equivalence Sets:\n");
|
|
|
|
for (std::set<Function*>::iterator LI = leaderSet.begin(),
|
|
|
|
LE = leaderSet.end(); LI != LE; ++LI) {
|
|
|
|
|
|
|
|
Function* LF = *LI;
|
|
|
|
const std::set<Function*>& EqClass = FuncECs.getEqClass(LF);
|
|
|
|
|
|
|
|
if (EqClass.size() > 1) {
|
2004-11-02 06:38:58 +00:00
|
|
|
#ifndef NDEBUG
|
2004-05-23 07:54:02 +00:00
|
|
|
DEBUG(std::cerr <<" Equivalence set for leader " <<LF->getName()<<" = ");
|
|
|
|
for (std::set<Function*>::const_iterator EqI = EqClass.begin(),
|
|
|
|
EqEnd = EqClass.end(); EqI != EqEnd; ++EqI)
|
|
|
|
DEBUG(std::cerr << " " << (*EqI)->getName() << ",");
|
|
|
|
DEBUG(std::cerr << "\n");
|
|
|
|
#endif
|
|
|
|
|
2004-10-31 21:56:11 +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.
|
2004-11-02 06:38:58 +00:00
|
|
|
DSGraph &MergedG = getOrCreateGraph(*LF);
|
2004-05-23 07:54:02 +00:00
|
|
|
|
2004-11-01 20:37:00 +00:00
|
|
|
// Record the argument nodes for use in merging later below.
|
|
|
|
std::vector<DSNodeHandle> ArgNodes;
|
|
|
|
|
2004-05-23 07:54:02 +00:00
|
|
|
for (Function::aiterator AI1 = LF->abegin(); AI1 != LF->aend(); ++AI1)
|
2004-10-31 18:13:19 +00:00
|
|
|
if (DS::isPointerType(AI1->getType()))
|
2004-11-02 06:38:58 +00:00
|
|
|
ArgNodes.push_back(MergedG.getNodeForValue(AI1));
|
2004-05-23 07:54:02 +00:00
|
|
|
|
2004-10-31 21:56:11 +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
|
2004-11-02 06:38:58 +00:00
|
|
|
// to be merged in once.
|
2004-05-23 07:54:02 +00:00
|
|
|
std::set<DSGraph*> GraphsMerged;
|
2004-11-02 06:38:58 +00:00
|
|
|
GraphsMerged.insert(&CBU->getDSGraph(*LF));
|
|
|
|
|
2004-05-23 07:54:02 +00:00
|
|
|
for (std::set<Function*>::const_iterator EqI = EqClass.begin(),
|
2004-11-02 06:38:58 +00:00
|
|
|
E = EqClass.end(); EqI != E; ++EqI) {
|
|
|
|
Function *F = *EqI;
|
|
|
|
DSGraph *&FG = DSInfo[F];
|
2004-10-31 17:47:48 +00:00
|
|
|
|
2004-11-02 06:38:58 +00:00
|
|
|
DSGraph &CBUGraph = CBU->getDSGraph(*F);
|
|
|
|
if (!GraphsMerged.insert(&CBUGraph).second)
|
2004-05-23 07:54:02 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Record the "folded" graph for the function.
|
2004-11-02 20:31:06 +00:00
|
|
|
for (DSGraph::ReturnNodesTy::iterator
|
|
|
|
I = CBUGraph.getReturnNodes().begin(),
|
|
|
|
E = CBUGraph.getReturnNodes().end();
|
|
|
|
I != E; ++I) {
|
|
|
|
assert(DSInfo[I->first] == 0 && "Graph already exists for Fn!");
|
|
|
|
DSInfo[I->first] = &MergedG;
|
|
|
|
}
|
2004-05-23 07:54:02 +00:00
|
|
|
|
2004-11-02 06:38:58 +00:00
|
|
|
// Clone this member of the equivalence class into MergedG.
|
2004-05-23 07:54:02 +00:00
|
|
|
DSGraph::NodeMapTy NodeMap;
|
2004-10-31 17:47:48 +00:00
|
|
|
|
2004-11-02 06:38:58 +00:00
|
|
|
MergedG.cloneInto(CBUGraph, MergedG.getScalarMap(),
|
2004-11-02 20:31:06 +00:00
|
|
|
MergedG.getReturnNodes(), NodeMap, 0);
|
2004-05-23 07:54:02 +00:00
|
|
|
|
|
|
|
// Merge the return nodes of all functions together.
|
2004-11-02 06:38:58 +00:00
|
|
|
MergedG.getReturnNodes()[LF].mergeWith(MergedG.getReturnNodes()[F]);
|
2004-05-23 07:54:02 +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::aiterator AI2 = F->abegin(), AI2end = F->aend();
|
2004-11-01 20:37:00 +00:00
|
|
|
for (unsigned arg=0, numArgs = ArgNodes.size();
|
2004-10-31 17:47:48 +00:00
|
|
|
arg != numArgs && AI2 != AI2end; ++AI2, ++arg)
|
|
|
|
if (DS::isPointerType(AI2->getType()))
|
2004-11-02 06:38:58 +00:00
|
|
|
ArgNodes[arg].mergeWith(MergedG.getNodeForValue(AI2));
|
2004-10-31 17:47:48 +00:00
|
|
|
|
2004-05-23 07:54:02 +00:00
|
|
|
for ( ; AI2 != AI2end; ++AI2)
|
2004-10-31 17:47:48 +00:00
|
|
|
if (DS::isPointerType(AI2->getType()))
|
2004-11-02 06:38:58 +00:00
|
|
|
ArgNodes.push_back(MergedG.getNodeForValue(AI2));
|
|
|
|
DEBUG(MergedG.AssertGraphOK());
|
2004-05-23 07:54:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DEBUG(std::cerr << "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-12 16:52:09 +00:00
|
|
|
DSGraph &PA::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...
|
|
|
|
Graph = new DSGraph(CBUGraph); // updates the map via reference
|
|
|
|
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!
|
2004-05-23 07:54:02 +00:00
|
|
|
for (DSGraph::ReturnNodesTy::iterator I = Graph->getReturnNodes().begin();
|
|
|
|
I != Graph->getReturnNodes().end(); ++I)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-02 17:51:11 +00:00
|
|
|
unsigned PA::EquivClassGraphs::
|
|
|
|
processSCC(DSGraph &FG, std::vector<DSGraph*> &Stack, unsigned &NextID,
|
|
|
|
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...
|
|
|
|
for (unsigned i = 0, e = FG.getFunctionCalls().size(); i != e; ++i) {
|
|
|
|
Instruction *Call = FG.getFunctionCalls()[i].getCallSite().getInstruction();
|
|
|
|
|
|
|
|
// Loop over all of the actually called functions...
|
|
|
|
ActualCalleesTy::const_iterator I, E;
|
|
|
|
for (tie(I, E) = getActualCallees().equal_range(Call); I != E; ++I)
|
|
|
|
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.
|
|
|
|
DSGraph::NodeMapTy NodeMap;
|
|
|
|
FG.cloneInto(*NG, FG.getScalarMap(), FG.getReturnNodes(), NodeMap);
|
|
|
|
|
|
|
|
// Update the DSInfo map and delete the old graph...
|
|
|
|
for (DSGraph::ReturnNodesTy::iterator I = NG->getReturnNodes().begin();
|
|
|
|
I != NG->getReturnNodes().end(); ++I)
|
|
|
|
DSInfo[I->first] = &FG;
|
2004-05-23 07:54:02 +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.
|
2004-11-02 17:51:11 +00:00
|
|
|
void PA::EquivClassGraphs::processGraph(DSGraph &G) {
|
|
|
|
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...
|
2004-11-02 17:51:11 +00:00
|
|
|
for (unsigned i=0, e = G.getFunctionCalls().size(); i != e; ++i) {
|
|
|
|
const DSCallSite &CS = G.getFunctionCalls()[i];
|
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??");
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
DSGraph* CalleeGraph = NULL;
|
|
|
|
ActualCalleesTy::const_iterator I, E;
|
|
|
|
tie(I, E) = getActualCallees().equal_range(TheCall);
|
|
|
|
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;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
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,
|
|
|
|
DSGraph::KeepModRefBits | DSGraph::StripAllocaBit |
|
|
|
|
DSGraph::DontCloneCallNodes |
|
|
|
|
DSGraph::DontCloneAuxCallNodes);
|
2004-05-23 07:54:02 +00:00
|
|
|
DEBUG(std::cerr << " Inlining graph [" << i << "/" << e-1
|
|
|
|
<< ":" << 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
|
|
|
assert(G.getInlinedGlobals().empty());
|
|
|
|
G.maskIncompleteMarkers();
|
|
|
|
G.markIncompleteNodes(DSGraph::MarkFormalArgs);
|
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(),
|
|
|
|
E = MainSM.global_end(); I != E; ++I)
|
|
|
|
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
|
|
|
}
|