2002-10-03 22:38:41 +02:00
|
|
|
//===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===//
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02: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 23:13:18 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-07-18 02:12:30 +02:00
|
|
|
//
|
|
|
|
// This file implements the BUDataStructures class, which represents the
|
|
|
|
// Bottom-Up Interprocedural closure of the data structure graph over the
|
|
|
|
// program. This is useful for applications like pool allocation, but **not**
|
2002-10-03 22:38:41 +02:00
|
|
|
// applications like alias analysis.
|
2002-07-18 02:12:30 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2006-06-19 20:23:36 +02:00
|
|
|
#define DEBUG_TYPE "bu_dsa"
|
2004-07-07 08:35:22 +02:00
|
|
|
#include "llvm/Analysis/DataStructure/DataStructure.h"
|
2005-03-20 03:42:07 +01:00
|
|
|
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
2002-07-18 02:12:30 +02:00
|
|
|
#include "llvm/Module.h"
|
2006-06-19 20:23:36 +02:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2006-06-19 20:23:36 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/Debug.h"
|
2005-04-21 18:09:43 +02:00
|
|
|
#include "llvm/Support/Timer.h"
|
2006-01-23 00:19:18 +01:00
|
|
|
#include <iostream>
|
2003-11-13 00:11:14 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2002-11-17 23:16:28 +01:00
|
|
|
namespace {
|
|
|
|
Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph");
|
2003-07-02 22:24:42 +02:00
|
|
|
Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined");
|
2003-07-03 01:42:48 +02:00
|
|
|
Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges");
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2006-06-19 20:23:36 +02:00
|
|
|
cl::opt<bool>
|
2006-10-23 21:55:24 +02:00
|
|
|
AddGlobals("budatastructures-annotate-calls", cl::Hidden,
|
2006-06-19 20:23:36 +02:00
|
|
|
cl::desc("Annotate call sites with functions as they are resolved"));
|
|
|
|
cl::opt<bool>
|
2006-10-23 21:55:24 +02:00
|
|
|
UpdateGlobals("budatastructures-update-from-globals", cl::Hidden,
|
2006-06-19 20:23:36 +02:00
|
|
|
cl::desc("Update local graph from global graph when processing function"));
|
|
|
|
|
2006-08-28 00:30:17 +02:00
|
|
|
RegisterPass<BUDataStructures>
|
2003-06-29 00:14:55 +02:00
|
|
|
X("budatastructure", "Bottom-up Data Structure Analysis");
|
2002-11-17 23:16:28 +01:00
|
|
|
}
|
2002-07-18 02:12:30 +02:00
|
|
|
|
2006-10-23 21:53:37 +02:00
|
|
|
static bool GetAllCalleesN(const DSCallSite &CS,
|
2006-06-19 20:23:36 +02:00
|
|
|
std::vector<Function*> &Callees);
|
|
|
|
|
2005-03-25 17:45:43 +01:00
|
|
|
/// BuildGlobalECs - Look at all of the nodes in the globals graph. If any node
|
|
|
|
/// contains multiple globals, DSA will never, ever, be able to tell the globals
|
|
|
|
/// apart. Instead of maintaining this information in all of the graphs
|
|
|
|
/// throughout the entire program, store only a single global (the "leader") in
|
|
|
|
/// the graphs, and build equivalence classes for the rest of the globals.
|
|
|
|
static void BuildGlobalECs(DSGraph &GG, std::set<GlobalValue*> &ECGlobals) {
|
|
|
|
DSScalarMap &SM = GG.getScalarMap();
|
|
|
|
EquivalenceClasses<GlobalValue*> &GlobalECs = SM.getGlobalECs();
|
|
|
|
for (DSGraph::node_iterator I = GG.node_begin(), E = GG.node_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (I->getGlobalsList().size() <= 1) continue;
|
|
|
|
|
|
|
|
// First, build up the equivalence set for this block of globals.
|
|
|
|
const std::vector<GlobalValue*> &GVs = I->getGlobalsList();
|
|
|
|
GlobalValue *First = GVs[0];
|
|
|
|
for (unsigned i = 1, e = GVs.size(); i != e; ++i)
|
|
|
|
GlobalECs.unionSets(First, GVs[i]);
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2005-03-25 17:45:43 +01:00
|
|
|
// Next, get the leader element.
|
|
|
|
assert(First == GlobalECs.getLeaderValue(First) &&
|
|
|
|
"First did not end up being the leader?");
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2005-03-25 17:45:43 +01:00
|
|
|
// Next, remove all globals from the scalar map that are not the leader.
|
|
|
|
assert(GVs[0] == First && "First had to be at the front!");
|
|
|
|
for (unsigned i = 1, e = GVs.size(); i != e; ++i) {
|
|
|
|
ECGlobals.insert(GVs[i]);
|
|
|
|
SM.erase(SM.find(GVs[i]));
|
|
|
|
}
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2005-03-25 17:45:43 +01:00
|
|
|
// Finally, change the global node to only contain the leader.
|
|
|
|
I->clearGlobals();
|
|
|
|
I->addGlobal(First);
|
|
|
|
}
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2005-03-25 17:45:43 +01:00
|
|
|
DEBUG(GG.AssertGraphOK());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EliminateUsesOfECGlobals - Once we have determined that some globals are in
|
|
|
|
/// really just equivalent to some other globals, remove the globals from the
|
|
|
|
/// specified DSGraph (if present), and merge any nodes with their leader nodes.
|
|
|
|
static void EliminateUsesOfECGlobals(DSGraph &G,
|
|
|
|
const std::set<GlobalValue*> &ECGlobals) {
|
|
|
|
DSScalarMap &SM = G.getScalarMap();
|
|
|
|
EquivalenceClasses<GlobalValue*> &GlobalECs = SM.getGlobalECs();
|
|
|
|
|
|
|
|
bool MadeChange = false;
|
|
|
|
for (DSScalarMap::global_iterator GI = SM.global_begin(), E = SM.global_end();
|
|
|
|
GI != E; ) {
|
|
|
|
GlobalValue *GV = *GI++;
|
|
|
|
if (!ECGlobals.count(GV)) continue;
|
|
|
|
|
|
|
|
const DSNodeHandle &GVNH = SM[GV];
|
|
|
|
assert(!GVNH.isNull() && "Global has null NH!?");
|
|
|
|
|
|
|
|
// Okay, this global is in some equivalence class. Start by finding the
|
|
|
|
// leader of the class.
|
|
|
|
GlobalValue *Leader = GlobalECs.getLeaderValue(GV);
|
|
|
|
|
|
|
|
// If the leader isn't already in the graph, insert it into the node
|
|
|
|
// corresponding to GV.
|
|
|
|
if (!SM.global_count(Leader)) {
|
|
|
|
GVNH.getNode()->addGlobal(Leader);
|
|
|
|
SM[Leader] = GVNH;
|
|
|
|
} else {
|
|
|
|
// Otherwise, the leader is in the graph, make sure the nodes are the
|
|
|
|
// merged in the specified graph.
|
|
|
|
const DSNodeHandle &LNH = SM[Leader];
|
|
|
|
if (LNH.getNode() != GVNH.getNode())
|
|
|
|
LNH.mergeWith(GVNH);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next step, remove the global from the DSNode.
|
|
|
|
GVNH.getNode()->removeGlobal(GV);
|
|
|
|
|
|
|
|
// Finally, remove the global from the ScalarMap.
|
|
|
|
SM.erase(GV);
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(if(MadeChange) G.AssertGraphOK());
|
|
|
|
}
|
|
|
|
|
2006-06-19 20:23:36 +02:00
|
|
|
static void AddGlobalToNode(BUDataStructures* B, DSCallSite D, Function* F) {
|
|
|
|
if(!AddGlobals)
|
|
|
|
return;
|
|
|
|
if(D.isIndirectCall()) {
|
|
|
|
DSGraph* GI = &B->getDSGraph(D.getCaller());
|
|
|
|
DSNodeHandle& NHF = GI->getNodeForValue(F);
|
|
|
|
DSCallSite DL = GI->getDSCallSiteForCallSite(D.getCallSite());
|
|
|
|
if (DL.getCalleeNode() != NHF.getNode() || NHF.isNull()) {
|
|
|
|
if (NHF.isNull()) {
|
|
|
|
DSNode *N = new DSNode(F->getType()->getElementType(), GI); // Create the node
|
|
|
|
N->addGlobal(F);
|
|
|
|
NHF.setTo(N,0);
|
|
|
|
DEBUG(std::cerr << "Adding " << F->getName() << " to a call node in "
|
|
|
|
<< D.getCaller().getName() << "\n");
|
|
|
|
}
|
|
|
|
DL.getCalleeNode()->mergeWith(NHF, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-09 22:12:07 +01:00
|
|
|
// run - Calculate the bottom up data structure graphs for each function in the
|
|
|
|
// program.
|
|
|
|
//
|
2004-09-20 06:48:05 +02:00
|
|
|
bool BUDataStructures::runOnModule(Module &M) {
|
2003-06-29 00:14:55 +02:00
|
|
|
LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
|
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.
llvm-svn: 20696
2005-03-19 23:23:45 +01:00
|
|
|
GlobalECs = LocalDSA.getGlobalECs();
|
|
|
|
|
|
|
|
GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph(), GlobalECs);
|
2003-02-03 20:11:38 +01:00
|
|
|
GlobalsGraph->setPrintAuxCalls();
|
2002-11-09 22:12:07 +01:00
|
|
|
|
2005-02-07 17:09:15 +01:00
|
|
|
IndCallGraphMap = new std::map<std::vector<Function*>,
|
|
|
|
std::pair<DSGraph*, std::vector<DSNodeHandle> > >();
|
|
|
|
|
2005-02-01 18:35:52 +01:00
|
|
|
std::vector<Function*> Stack;
|
|
|
|
hash_map<Function*, unsigned> ValMap;
|
|
|
|
unsigned NextID = 1;
|
|
|
|
|
2002-11-11 22:35:13 +01:00
|
|
|
Function *MainFunc = M.getMainFunction();
|
|
|
|
if (MainFunc)
|
2005-02-01 18:35:52 +01:00
|
|
|
calculateGraphs(MainFunc, Stack, NextID, ValMap);
|
2002-11-11 22:35:13 +01:00
|
|
|
|
|
|
|
// Calculate the graphs for any functions that are unreachable from main...
|
2002-11-09 22:12:07 +01:00
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
2003-07-01 18:04:18 +02:00
|
|
|
if (!I->isExternal() && !DSInfo.count(I)) {
|
2002-11-11 22:35:13 +01:00
|
|
|
if (MainFunc)
|
2006-10-13 19:38:22 +02:00
|
|
|
DEBUG(std::cerr << "*** BU: Function unreachable from main: "
|
|
|
|
<< I->getName() << "\n");
|
2005-02-01 18:35:52 +01:00
|
|
|
calculateGraphs(I, Stack, NextID, ValMap); // Calculate all graphs.
|
2002-11-11 22:35:13 +01:00
|
|
|
}
|
2003-07-03 01:42:48 +02:00
|
|
|
|
2005-02-04 20:59:49 +01:00
|
|
|
// If we computed any temporary indcallgraphs, free them now.
|
|
|
|
for (std::map<std::vector<Function*>,
|
|
|
|
std::pair<DSGraph*, std::vector<DSNodeHandle> > >::iterator I =
|
2005-02-07 17:09:15 +01:00
|
|
|
IndCallGraphMap->begin(), E = IndCallGraphMap->end(); I != E; ++I) {
|
2005-02-04 20:59:49 +01:00
|
|
|
I->second.second.clear(); // Drop arg refs into the graph.
|
|
|
|
delete I->second.first;
|
|
|
|
}
|
2005-02-07 17:09:15 +01:00
|
|
|
delete IndCallGraphMap;
|
2005-02-04 20:59:49 +01:00
|
|
|
|
2003-09-21 01:27:05 +02:00
|
|
|
// At the end of the bottom-up pass, the globals graph becomes complete.
|
|
|
|
// FIXME: This is not the right way to do this, but it is sorta better than
|
2003-09-21 01:58:33 +02:00
|
|
|
// nothing! In particular, externally visible globals and unresolvable call
|
|
|
|
// nodes at the end of the BU phase should make things that they point to
|
|
|
|
// incomplete in the globals graph.
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2004-02-08 02:51:48 +01:00
|
|
|
GlobalsGraph->removeTriviallyDeadNodes();
|
2003-09-21 01:27:05 +02:00
|
|
|
GlobalsGraph->maskIncompleteMarkers();
|
2005-03-13 21:15:06 +01:00
|
|
|
|
2005-03-22 23:10:22 +01:00
|
|
|
// Mark external globals incomplete.
|
|
|
|
GlobalsGraph->markIncompleteNodes(DSGraph::IgnoreGlobals);
|
|
|
|
|
2005-03-25 17:45:43 +01:00
|
|
|
// Grow the equivalence classes for the globals to include anything that we
|
|
|
|
// now know to be aliased.
|
|
|
|
std::set<GlobalValue*> ECGlobals;
|
|
|
|
BuildGlobalECs(*GlobalsGraph, ECGlobals);
|
|
|
|
if (!ECGlobals.empty()) {
|
2005-04-21 18:09:43 +02:00
|
|
|
NamedRegionTimer X("Bottom-UP EC Cleanup");
|
2006-10-13 19:38:22 +02:00
|
|
|
DEBUG(std::cerr << "Eliminating " << ECGlobals.size() << " EC Globals!\n");
|
2005-03-25 17:45:43 +01:00
|
|
|
for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
|
|
|
|
E = DSInfo.end(); I != E; ++I)
|
|
|
|
EliminateUsesOfECGlobals(*I->second, ECGlobals);
|
|
|
|
}
|
|
|
|
|
2005-03-13 21:15:06 +01: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 23:10:04 +01:00
|
|
|
if (MainFunc && !MainFunc->isExternal()) {
|
2005-03-13 21:15:06 +01:00
|
|
|
DSGraph &MainGraph = getOrCreateGraph(MainFunc);
|
|
|
|
const DSGraph &GG = *MainGraph.getGlobalsGraph();
|
2006-10-23 21:53:37 +02:00
|
|
|
ReachabilityCloner RC(MainGraph, GG,
|
|
|
|
DSGraph::DontCloneCallNodes |
|
|
|
|
DSGraph::DontCloneAuxCallNodes);
|
2005-03-13 21:15:06 +01:00
|
|
|
|
|
|
|
// 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 21:32:26 +01:00
|
|
|
MainGraph.maskIncompleteMarkers();
|
2005-04-21 23:13:18 +02:00
|
|
|
MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
|
2005-03-13 21:15:06 +01:00
|
|
|
DSGraph::IgnoreGlobals);
|
2006-06-19 20:23:36 +02:00
|
|
|
|
|
|
|
//Debug messages if along the way we didn't resolve a call site
|
|
|
|
//also update the call graph and callsites we did find.
|
|
|
|
for(DSGraph::afc_iterator ii = MainGraph.afc_begin(),
|
2006-10-23 21:53:37 +02:00
|
|
|
ee = MainGraph.afc_end(); ii != ee; ++ii) {
|
2006-06-19 20:23:36 +02:00
|
|
|
std::vector<Function*> Funcs;
|
2006-10-23 21:53:37 +02:00
|
|
|
GetAllCalleesN(*ii, Funcs);
|
2006-10-13 19:38:22 +02:00
|
|
|
DEBUG(std::cerr << "Lost site\n");
|
|
|
|
DEBUG(ii->getCallSite().getInstruction()->dump());
|
2006-06-19 20:23:36 +02:00
|
|
|
for (std::vector<Function*>::iterator iif = Funcs.begin(), eef = Funcs.end();
|
2006-10-23 21:53:37 +02:00
|
|
|
iif != eef; ++iif) {
|
|
|
|
AddGlobalToNode(this, *ii, *iif);
|
|
|
|
DEBUG(std::cerr << "Adding\n");
|
|
|
|
ActualCallees.insert(std::make_pair(ii->getCallSite().getInstruction(), *iif));
|
2006-06-19 20:23:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-13 21:15:06 +01:00
|
|
|
}
|
|
|
|
|
2006-06-19 20:23:36 +02:00
|
|
|
NumCallEdges += ActualCallees.size();
|
|
|
|
|
2002-11-09 22:12:07 +01:00
|
|
|
return false;
|
|
|
|
}
|
2002-10-03 22:38:41 +02:00
|
|
|
|
2002-11-11 22:35:13 +01:00
|
|
|
DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
|
|
|
|
// Has the graph already been created?
|
|
|
|
DSGraph *&Graph = DSInfo[F];
|
|
|
|
if (Graph) return *Graph;
|
|
|
|
|
2005-03-25 01:05:04 +01:00
|
|
|
DSGraph &LocGraph = getAnalysis<LocalDataStructures>().getDSGraph(*F);
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2005-03-25 01:05:04 +01:00
|
|
|
// Steal the local graph.
|
|
|
|
Graph = new DSGraph(GlobalECs, LocGraph.getTargetData());
|
|
|
|
Graph->spliceFrom(LocGraph);
|
2002-11-11 22:35:13 +01:00
|
|
|
|
|
|
|
Graph->setGlobalsGraph(GlobalsGraph);
|
|
|
|
Graph->setPrintAuxCalls();
|
|
|
|
|
|
|
|
// Start with a copy of the original call sites...
|
|
|
|
Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
|
|
|
|
return *Graph;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:42:07 +01:00
|
|
|
static bool isVAHackFn(const Function *F) {
|
|
|
|
return F->getName() == "printf" || F->getName() == "sscanf" ||
|
|
|
|
F->getName() == "fprintf" || F->getName() == "open" ||
|
|
|
|
F->getName() == "sprintf" || F->getName() == "fputs" ||
|
2005-04-21 18:09:43 +02:00
|
|
|
F->getName() == "fscanf" || F->getName() == "malloc" ||
|
|
|
|
F->getName() == "free";
|
2005-03-20 03:42:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isResolvableFunc(const Function* callee) {
|
|
|
|
return !callee->isExternal() || isVAHackFn(callee);
|
|
|
|
}
|
|
|
|
|
2006-10-23 21:53:37 +02:00
|
|
|
static void GetAllCallees(const DSCallSite &CS,
|
|
|
|
std::vector<Function*> &Callees) {
|
|
|
|
if (CS.isDirectCall()) {
|
|
|
|
if (isResolvableFunc(CS.getCalleeFunc()))
|
|
|
|
Callees.push_back(CS.getCalleeFunc());
|
|
|
|
} else if (!CS.getCalleeNode()->isIncomplete()) {
|
|
|
|
// Get all callees.
|
|
|
|
unsigned OldSize = Callees.size();
|
|
|
|
CS.getCalleeNode()->addFullFunctionList(Callees);
|
|
|
|
|
|
|
|
// If any of the callees are unresolvable, remove the whole batch!
|
|
|
|
for (unsigned i = OldSize, e = Callees.size(); i != e; ++i)
|
|
|
|
if (!isResolvableFunc(Callees[i])) {
|
|
|
|
Callees.erase(Callees.begin()+OldSize, Callees.end());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-19 20:23:36 +02:00
|
|
|
//returns true if all callees were resolved
|
2006-10-23 21:53:37 +02:00
|
|
|
static bool GetAllCalleesN(const DSCallSite &CS,
|
2005-03-20 03:42:07 +01:00
|
|
|
std::vector<Function*> &Callees) {
|
|
|
|
if (CS.isDirectCall()) {
|
2006-06-19 20:23:36 +02:00
|
|
|
if (isResolvableFunc(CS.getCalleeFunc())) {
|
2005-03-20 03:42:07 +01:00
|
|
|
Callees.push_back(CS.getCalleeFunc());
|
2006-06-19 20:23:36 +02:00
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
} else {
|
2005-03-20 03:42:07 +01:00
|
|
|
// Get all callees.
|
2006-06-19 20:23:36 +02:00
|
|
|
bool retval = CS.getCalleeNode()->isComplete();
|
2005-03-20 03:42:07 +01:00
|
|
|
unsigned OldSize = Callees.size();
|
|
|
|
CS.getCalleeNode()->addFullFunctionList(Callees);
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2006-06-19 20:23:36 +02:00
|
|
|
// If any of the callees are unresolvable, remove that one
|
|
|
|
for (unsigned i = OldSize; i != Callees.size(); ++i)
|
2005-03-20 03:42:07 +01:00
|
|
|
if (!isResolvableFunc(Callees[i])) {
|
2006-06-19 20:23:36 +02:00
|
|
|
Callees.erase(Callees.begin()+i);
|
|
|
|
--i;
|
|
|
|
retval = false;
|
2005-03-20 03:42:07 +01:00
|
|
|
}
|
2006-06-19 20:23:36 +02:00
|
|
|
return retval;
|
|
|
|
//return false;
|
2005-03-20 03:42:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// GetAllAuxCallees - Return a list containing all of the resolvable callees in
|
|
|
|
/// the aux list for the specified graph in the Callees vector.
|
|
|
|
static void GetAllAuxCallees(DSGraph &G, std::vector<Function*> &Callees) {
|
|
|
|
Callees.clear();
|
|
|
|
for (DSGraph::afc_iterator I = G.afc_begin(), E = G.afc_end(); I != E; ++I)
|
|
|
|
GetAllCallees(*I, Callees);
|
|
|
|
}
|
|
|
|
|
2002-11-11 22:35:13 +01:00
|
|
|
unsigned BUDataStructures::calculateGraphs(Function *F,
|
|
|
|
std::vector<Function*> &Stack,
|
2005-04-21 23:13:18 +02:00
|
|
|
unsigned &NextID,
|
2006-10-23 21:53:37 +02:00
|
|
|
hash_map<Function*, unsigned> &ValMap) {
|
2003-11-13 06:04:19 +01:00
|
|
|
assert(!ValMap.count(F) && "Shouldn't revisit functions!");
|
2002-11-11 22:35:13 +01:00
|
|
|
unsigned Min = NextID++, MyID = Min;
|
|
|
|
ValMap[F] = Min;
|
|
|
|
Stack.push_back(F);
|
|
|
|
|
2004-03-04 18:05:28 +01:00
|
|
|
// FIXME! This test should be generalized to be any function that we have
|
|
|
|
// already processed, in the case when there isn't a main or there are
|
|
|
|
// unreachable functions!
|
2002-11-11 22:35:13 +01:00
|
|
|
if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
|
|
|
|
// No callees!
|
|
|
|
Stack.pop_back();
|
|
|
|
ValMap[F] = ~0;
|
|
|
|
return Min;
|
|
|
|
}
|
|
|
|
|
|
|
|
DSGraph &Graph = getOrCreateGraph(F);
|
2006-06-19 20:23:36 +02:00
|
|
|
if (UpdateGlobals)
|
|
|
|
Graph.updateFromGlobalGraph();
|
2002-11-11 22:35:13 +01:00
|
|
|
|
2005-03-20 03:42:07 +01:00
|
|
|
// Find all callee functions.
|
|
|
|
std::vector<Function*> CalleeFunctions;
|
|
|
|
GetAllAuxCallees(Graph, CalleeFunctions);
|
|
|
|
|
2002-11-11 22:35:13 +01:00
|
|
|
// The edges out of the current node are the call site targets...
|
2005-03-20 03:42:07 +01:00
|
|
|
for (unsigned i = 0, e = CalleeFunctions.size(); i != e; ++i) {
|
|
|
|
Function *Callee = CalleeFunctions[i];
|
2002-11-11 22:35:13 +01:00
|
|
|
unsigned M;
|
|
|
|
// Have we visited the destination function yet?
|
2003-02-01 05:52:08 +01:00
|
|
|
hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
|
2002-11-11 22:35:13 +01:00
|
|
|
if (It == ValMap.end()) // No, visit it now.
|
|
|
|
M = calculateGraphs(Callee, Stack, NextID, ValMap);
|
|
|
|
else // Yes, get it's number.
|
|
|
|
M = It->second;
|
|
|
|
if (M < Min) Min = M;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
|
|
|
|
if (Min != MyID)
|
|
|
|
return Min; // This is part of a larger SCC!
|
|
|
|
|
|
|
|
// If this is a new SCC, process it now.
|
|
|
|
if (Stack.back() == F) { // Special case the single "SCC" case here.
|
|
|
|
DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
|
|
|
|
<< F->getName() << "\n");
|
|
|
|
Stack.pop_back();
|
2003-06-30 07:09:58 +02:00
|
|
|
DSGraph &G = getDSGraph(*F);
|
|
|
|
DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
|
2006-10-23 21:53:37 +02:00
|
|
|
calculateGraph(G);
|
2003-06-30 07:09:58 +02:00
|
|
|
DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
|
|
|
|
<< G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
|
|
|
|
<< "]\n");
|
2002-11-11 22:35:13 +01:00
|
|
|
|
2002-11-17 23:16:28 +01:00
|
|
|
if (MaxSCC < 1) MaxSCC = 1;
|
|
|
|
|
2005-03-20 03:42:07 +01:00
|
|
|
// Should we revisit the graph? Only do it if there are now new resolvable
|
|
|
|
// callees.
|
2006-10-23 21:53:37 +02:00
|
|
|
GetAllAuxCallees(Graph, CalleeFunctions);
|
|
|
|
if (!CalleeFunctions.empty()) {
|
2006-06-19 20:23:36 +02:00
|
|
|
DEBUG(std::cerr << "Recalculating " << F->getName() << " due to new knowledge\n");
|
2002-11-11 22:35:13 +01:00
|
|
|
ValMap.erase(F);
|
|
|
|
return calculateGraphs(F, Stack, NextID, ValMap);
|
|
|
|
} else {
|
|
|
|
ValMap[F] = ~0U;
|
|
|
|
}
|
|
|
|
return MyID;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// SCCFunctions - Keep track of the functions in the current SCC
|
|
|
|
//
|
2005-03-25 01:05:04 +01:00
|
|
|
std::vector<DSGraph*> SCCGraphs;
|
|
|
|
|
|
|
|
unsigned SCCSize = 1;
|
|
|
|
Function *NF = Stack.back();
|
|
|
|
ValMap[NF] = ~0U;
|
|
|
|
DSGraph &SCCGraph = getDSGraph(*NF);
|
2002-11-11 22:35:13 +01:00
|
|
|
|
2005-03-25 01:05:04 +01:00
|
|
|
// First thing first, collapse all of the DSGraphs into a single graph for
|
|
|
|
// the entire SCC. Splice all of the graphs into one and discard all of the
|
|
|
|
// old graphs.
|
|
|
|
//
|
|
|
|
while (NF != F) {
|
|
|
|
Stack.pop_back();
|
|
|
|
NF = Stack.back();
|
2002-11-11 22:35:13 +01:00
|
|
|
ValMap[NF] = ~0U;
|
2003-06-30 07:09:58 +02:00
|
|
|
|
2005-03-25 01:05:04 +01:00
|
|
|
DSGraph &NFG = getDSGraph(*NF);
|
2002-11-11 22:35:13 +01:00
|
|
|
|
2005-03-25 01:05:04 +01:00
|
|
|
// Update the Function -> DSG map.
|
|
|
|
for (DSGraph::retnodes_iterator I = NFG.retnodes_begin(),
|
|
|
|
E = NFG.retnodes_end(); I != E; ++I)
|
|
|
|
DSInfo[I->first] = &SCCGraph;
|
2002-11-11 22:35:13 +01:00
|
|
|
|
2005-03-25 01:05:04 +01:00
|
|
|
SCCGraph.spliceFrom(NFG);
|
|
|
|
delete &NFG;
|
2002-11-12 16:58:08 +01:00
|
|
|
|
2005-03-25 01:05:04 +01:00
|
|
|
++SCCSize;
|
|
|
|
}
|
|
|
|
Stack.pop_back();
|
2005-03-25 01:06:09 +01:00
|
|
|
|
2006-10-13 19:38:22 +02:00
|
|
|
DEBUG(std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
|
2006-10-23 21:53:37 +02:00
|
|
|
<< SCCSize << "\n");
|
2005-03-25 01:05:04 +01:00
|
|
|
|
|
|
|
// Compute the Max SCC Size.
|
|
|
|
if (MaxSCC < SCCSize)
|
|
|
|
MaxSCC = SCCSize;
|
2002-11-11 22:35:13 +01:00
|
|
|
|
2003-07-02 06:37:48 +02:00
|
|
|
// Clean up the graph before we start inlining a bunch again...
|
2005-03-25 01:05:04 +01:00
|
|
|
SCCGraph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
|
2003-07-02 06:37:48 +02:00
|
|
|
|
2003-06-30 07:09:58 +02:00
|
|
|
// Now that we have one big happy family, resolve all of the call sites in
|
|
|
|
// the graph...
|
2006-10-23 21:53:37 +02:00
|
|
|
calculateGraph(SCCGraph);
|
2005-03-25 01:05:04 +01:00
|
|
|
DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph.getGraphSize()
|
|
|
|
<< "+" << SCCGraph.getAuxFunctionCalls().size() << "]\n");
|
2002-11-11 22:35:13 +01:00
|
|
|
|
2006-10-13 19:38:22 +02:00
|
|
|
DEBUG(std::cerr << "DONE with SCC #: " << MyID << "\n");
|
2002-11-11 22:35:13 +01:00
|
|
|
|
|
|
|
// We never have to revisit "SCC" processed functions...
|
|
|
|
return MyID;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MyID; // == Min
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-18 02:12:30 +02:00
|
|
|
// releaseMemory - If the pass pipeline is done with this pass, we can release
|
|
|
|
// our memory... here...
|
|
|
|
//
|
2005-03-23 22:59:34 +01:00
|
|
|
void BUDataStructures::releaseMyMemory() {
|
2003-06-30 07:09:58 +02:00
|
|
|
for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
|
|
|
|
E = DSInfo.end(); I != E; ++I) {
|
|
|
|
I->second->getReturnNodes().erase(I->first);
|
|
|
|
if (I->second->getReturnNodes().empty())
|
|
|
|
delete I->second;
|
|
|
|
}
|
2002-07-18 02:12:30 +02:00
|
|
|
|
|
|
|
// Empty map so next time memory is released, data structures are not
|
|
|
|
// re-deleted.
|
|
|
|
DSInfo.clear();
|
2002-11-09 22:12:07 +01:00
|
|
|
delete GlobalsGraph;
|
|
|
|
GlobalsGraph = 0;
|
2002-07-18 02:12:30 +02:00
|
|
|
}
|
|
|
|
|
2005-04-21 18:09:43 +02:00
|
|
|
DSGraph &BUDataStructures::CreateGraphForExternalFunction(const Function &Fn) {
|
|
|
|
Function *F = const_cast<Function*>(&Fn);
|
|
|
|
DSGraph *DSG = new DSGraph(GlobalECs, GlobalsGraph->getTargetData());
|
|
|
|
DSInfo[F] = DSG;
|
|
|
|
DSG->setGlobalsGraph(GlobalsGraph);
|
|
|
|
DSG->setPrintAuxCalls();
|
|
|
|
|
|
|
|
// Add function to the graph.
|
|
|
|
DSG->getReturnNodes().insert(std::make_pair(F, DSNodeHandle()));
|
|
|
|
|
|
|
|
if (F->getName() == "free") { // Taking the address of free.
|
2005-07-27 08:12:32 +02:00
|
|
|
|
2005-04-21 18:09:43 +02:00
|
|
|
// Free should take a single pointer argument, mark it as heap memory.
|
|
|
|
DSNode *N = new DSNode(0, DSG);
|
|
|
|
N->setHeapNodeMarker();
|
|
|
|
DSG->getNodeForValue(F->arg_begin()).mergeWith(N);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
std::cerr << "Unrecognized external function: " << F->getName() << "\n";
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
return *DSG;
|
|
|
|
}
|
|
|
|
|
2006-10-23 21:53:37 +02:00
|
|
|
void BUDataStructures::calculateGraph(DSGraph &Graph) {
|
2005-04-25 21:16:31 +02:00
|
|
|
// If this graph contains the main function, clone the globals graph into this
|
|
|
|
// graph before we inline callees and other fun stuff.
|
|
|
|
bool ContainsMain = false;
|
|
|
|
DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
|
|
|
|
|
|
|
|
for (DSGraph::ReturnNodesTy::iterator I = ReturnNodes.begin(),
|
|
|
|
E = ReturnNodes.end(); I != E; ++I)
|
|
|
|
if (I->first->hasExternalLinkage() && I->first->getName() == "main") {
|
|
|
|
ContainsMain = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this graph contains main, copy the contents of the globals graph over.
|
|
|
|
// Note that this is *required* for correctness. If a callee contains a use
|
|
|
|
// of a global, we have to make sure to link up nodes due to global-argument
|
|
|
|
// bindings.
|
|
|
|
if (ContainsMain) {
|
|
|
|
const DSGraph &GG = *Graph.getGlobalsGraph();
|
|
|
|
ReachabilityCloner RC(Graph, GG,
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-11 22:35:13 +01:00
|
|
|
// Move our call site list into TempFCs so that inline call sites go into the
|
|
|
|
// new call site list and doesn't invalidate our iterators!
|
2005-01-31 00:51:02 +01:00
|
|
|
std::list<DSCallSite> TempFCs;
|
|
|
|
std::list<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
|
2002-11-11 22:35:13 +01:00
|
|
|
TempFCs.swap(AuxCallsList);
|
|
|
|
|
2005-02-01 18:35:52 +01:00
|
|
|
bool Printed = false;
|
2006-10-23 21:53:37 +02:00
|
|
|
std::vector<Function*> CalledFuncs;
|
2005-02-01 22:37:27 +01:00
|
|
|
while (!TempFCs.empty()) {
|
|
|
|
DSCallSite &CS = *TempFCs.begin();
|
2006-10-23 21:53:37 +02:00
|
|
|
|
|
|
|
CalledFuncs.clear();
|
2002-11-11 01:01:34 +01:00
|
|
|
|
2005-03-19 00:19:47 +01:00
|
|
|
// Fast path for noop calls. Note that we don't care about merging globals
|
|
|
|
// in the callee with nodes in the caller here.
|
2006-10-23 21:53:37 +02:00
|
|
|
if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0) {
|
|
|
|
TempFCs.erase(TempFCs.begin());
|
|
|
|
continue;
|
|
|
|
} else if (CS.isDirectCall() && isVAHackFn(CS.getCalleeFunc())) {
|
|
|
|
TempFCs.erase(TempFCs.begin());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetAllCallees(CS, CalledFuncs);
|
|
|
|
|
|
|
|
if (CalledFuncs.empty()) {
|
|
|
|
// Remember that we could not resolve this yet!
|
|
|
|
AuxCallsList.splice(AuxCallsList.end(), TempFCs, TempFCs.begin());
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
DSGraph *GI;
|
|
|
|
Instruction *TheCall = CS.getCallSite().getInstruction();
|
|
|
|
|
|
|
|
if (CalledFuncs.size() == 1) {
|
|
|
|
Function *Callee = CalledFuncs[0];
|
2005-03-21 21:20:49 +01:00
|
|
|
ActualCallees.insert(std::make_pair(TheCall, Callee));
|
2006-10-23 21:53:37 +02:00
|
|
|
|
|
|
|
// Get the data structure graph for the called function.
|
2005-02-04 20:59:49 +01:00
|
|
|
GI = &getDSGraph(*Callee); // Graph to inline
|
|
|
|
DEBUG(std::cerr << " Inlining graph for " << Callee->getName());
|
2006-10-23 21:53:37 +02:00
|
|
|
|
2005-02-04 20:59:49 +01:00
|
|
|
DEBUG(std::cerr << "[" << GI->getGraphSize() << "+"
|
|
|
|
<< GI->getAuxFunctionCalls().size() << "] into '"
|
2005-02-01 22:49:43 +01:00
|
|
|
<< Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
|
|
|
|
<< Graph.getAuxFunctionCalls().size() << "]\n");
|
2005-02-04 20:59:49 +01:00
|
|
|
Graph.mergeInGraph(CS, *Callee, *GI,
|
2005-02-01 22:49:43 +01:00
|
|
|
DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes);
|
|
|
|
++NumBUInlines;
|
2005-02-04 20:59:49 +01:00
|
|
|
} else {
|
|
|
|
if (!Printed)
|
|
|
|
std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n";
|
|
|
|
std::cerr << " calls " << CalledFuncs.size()
|
2005-04-21 23:13:18 +02:00
|
|
|
<< " fns from site: " << CS.getCallSite().getInstruction()
|
2005-02-04 20:59:49 +01:00
|
|
|
<< " " << *CS.getCallSite().getInstruction();
|
|
|
|
std::cerr << " Fns =";
|
2005-03-21 21:20:49 +01:00
|
|
|
unsigned NumPrinted = 0;
|
|
|
|
|
2005-02-04 20:59:49 +01:00
|
|
|
for (std::vector<Function*>::iterator I = CalledFuncs.begin(),
|
2005-03-21 21:20:49 +01:00
|
|
|
E = CalledFuncs.end(); I != E; ++I) {
|
2006-10-23 21:53:37 +02:00
|
|
|
if (NumPrinted++ < 8) std::cerr << " " << (*I)->getName();
|
2005-03-21 21:20:49 +01:00
|
|
|
|
|
|
|
// Add the call edges to the call graph.
|
|
|
|
ActualCallees.insert(std::make_pair(TheCall, *I));
|
|
|
|
}
|
2006-10-23 21:53:37 +02:00
|
|
|
std::cerr << "\n";
|
2005-02-04 20:59:49 +01:00
|
|
|
|
|
|
|
// See if we already computed a graph for this set of callees.
|
|
|
|
std::sort(CalledFuncs.begin(), CalledFuncs.end());
|
|
|
|
std::pair<DSGraph*, std::vector<DSNodeHandle> > &IndCallGraph =
|
2005-02-07 17:09:15 +01:00
|
|
|
(*IndCallGraphMap)[CalledFuncs];
|
2005-02-04 20:59:49 +01:00
|
|
|
|
|
|
|
if (IndCallGraph.first == 0) {
|
|
|
|
std::vector<Function*>::iterator I = CalledFuncs.begin(),
|
|
|
|
E = CalledFuncs.end();
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2005-02-04 20:59:49 +01:00
|
|
|
// Start with a copy of the first 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.
llvm-svn: 20696
2005-03-19 23:23:45 +01:00
|
|
|
GI = IndCallGraph.first = new DSGraph(getDSGraph(**I), GlobalECs);
|
2005-02-04 20:59:49 +01:00
|
|
|
GI->setGlobalsGraph(Graph.getGlobalsGraph());
|
|
|
|
std::vector<DSNodeHandle> &Args = IndCallGraph.second;
|
|
|
|
|
|
|
|
// Get the argument nodes for the first callee. The return value is
|
|
|
|
// the 0th index in the vector.
|
|
|
|
GI->getFunctionArgumentsForCall(*I, Args);
|
|
|
|
|
|
|
|
// Merge all of the other callees into this graph.
|
2006-10-23 21:53:37 +02:00
|
|
|
for (++I; I != E; ++I) {
|
2005-02-04 20:59:49 +01:00
|
|
|
// If the graph already contains the nodes for the function, don't
|
|
|
|
// bother merging it in again.
|
|
|
|
if (!GI->containsFunction(*I)) {
|
2005-03-22 01:36:51 +01:00
|
|
|
GI->cloneInto(getDSGraph(**I));
|
2005-02-04 20:59:49 +01:00
|
|
|
++NumBUInlines;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<DSNodeHandle> NextArgs;
|
|
|
|
GI->getFunctionArgumentsForCall(*I, NextArgs);
|
|
|
|
unsigned i = 0, e = Args.size();
|
|
|
|
for (; i != e; ++i) {
|
|
|
|
if (i == NextArgs.size()) break;
|
|
|
|
Args[i].mergeWith(NextArgs[i]);
|
|
|
|
}
|
|
|
|
for (e = NextArgs.size(); i != e; ++i)
|
|
|
|
Args.push_back(NextArgs[i]);
|
|
|
|
}
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2005-02-04 20:59:49 +01:00
|
|
|
// Clean up the final graph!
|
|
|
|
GI->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
|
|
|
|
} else {
|
2006-10-23 21:53:37 +02:00
|
|
|
std::cerr << "***\n*** RECYCLED GRAPH ***\n***\n";
|
2005-02-04 20:59:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GI = IndCallGraph.first;
|
|
|
|
|
2006-10-23 21:53:37 +02:00
|
|
|
// Merge the unified graph into this graph now.
|
|
|
|
DEBUG(std::cerr << " Inlining multi callee graph "
|
|
|
|
<< "[" << GI->getGraphSize() << "+"
|
|
|
|
<< GI->getAuxFunctionCalls().size() << "] into '"
|
|
|
|
<< Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
|
|
|
|
<< Graph.getAuxFunctionCalls().size() << "]\n");
|
2006-06-19 20:23:36 +02:00
|
|
|
|
2006-10-23 21:53:37 +02:00
|
|
|
Graph.mergeInGraph(CS, IndCallGraph.second, *GI,
|
|
|
|
DSGraph::StripAllocaBit |
|
|
|
|
DSGraph::DontCloneCallNodes);
|
|
|
|
++NumBUInlines;
|
2005-02-01 22:37:27 +01:00
|
|
|
}
|
2005-01-31 00:51:02 +01:00
|
|
|
}
|
2006-10-23 21:53:37 +02:00
|
|
|
TempFCs.erase(TempFCs.begin());
|
2005-01-31 00:51:02 +01:00
|
|
|
}
|
2002-11-11 01:01:34 +01:00
|
|
|
|
2003-07-16 23:42:03 +02:00
|
|
|
// Recompute the Incomplete markers
|
2002-11-11 22:35:13 +01:00
|
|
|
Graph.maskIncompleteMarkers();
|
2003-01-23 23:05:33 +01:00
|
|
|
Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
|
2003-07-16 23:42:03 +02:00
|
|
|
|
|
|
|
// Delete dead nodes. Treat globals that are unreachable but that can
|
|
|
|
// reach live nodes as live.
|
2003-01-23 23:05:33 +01:00
|
|
|
Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
|
2002-11-11 01:01:34 +01:00
|
|
|
|
2004-02-21 01:30:28 +01: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.
|
|
|
|
DSScalarMap &MainSM = Graph.getScalarMap();
|
|
|
|
ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit);
|
|
|
|
|
2004-10-31 22:54:51 +01:00
|
|
|
// Clone everything reachable from globals in the function graph into the
|
2004-02-21 01:30:28 +01:00
|
|
|
// globals graph.
|
|
|
|
for (DSScalarMap::global_iterator I = MainSM.global_begin(),
|
2005-04-21 23:13:18 +02:00
|
|
|
E = MainSM.global_end(); I != E; ++I)
|
2004-02-21 01:30:28 +01:00
|
|
|
RC.getClonedNH(MainSM[*I]);
|
|
|
|
|
2002-11-11 22:35:13 +01:00
|
|
|
//Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
|
2002-11-12 16:58:08 +01:00
|
|
|
}
|
2005-01-24 21:00:14 +01:00
|
|
|
|
|
|
|
static const Function *getFnForValue(const Value *V) {
|
|
|
|
if (const Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
return I->getParent()->getParent();
|
|
|
|
else if (const Argument *A = dyn_cast<Argument>(V))
|
|
|
|
return A->getParent();
|
|
|
|
else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
|
|
|
|
return BB->getParent();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// deleteValue/copyValue - Interfaces to update the DSGraphs in the program.
|
|
|
|
/// These correspond to the interfaces defined in the AliasAnalysis class.
|
|
|
|
void BUDataStructures::deleteValue(Value *V) {
|
|
|
|
if (const Function *F = getFnForValue(V)) { // Function local value?
|
|
|
|
// If this is a function local value, just delete it from the scalar map!
|
|
|
|
getDSGraph(*F).getScalarMap().eraseIfExists(V);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-01-31 01:10:45 +01:00
|
|
|
if (Function *F = dyn_cast<Function>(V)) {
|
2005-01-24 21:00:14 +01:00
|
|
|
assert(getDSGraph(*F).getReturnNodes().size() == 1 &&
|
|
|
|
"cannot handle scc's");
|
|
|
|
delete DSInfo[F];
|
|
|
|
DSInfo.erase(F);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void BUDataStructures::copyValue(Value *From, Value *To) {
|
|
|
|
if (From == To) return;
|
|
|
|
if (const Function *F = getFnForValue(From)) { // Function local value?
|
|
|
|
// If this is a function local value, just delete it from the scalar map!
|
|
|
|
getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Function *FromF = dyn_cast<Function>(From)) {
|
|
|
|
Function *ToF = cast<Function>(To);
|
|
|
|
assert(!DSInfo.count(ToF) && "New Function already exists!");
|
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.
llvm-svn: 20696
2005-03-19 23:23:45 +01:00
|
|
|
DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs);
|
2005-01-24 21:00:14 +01:00
|
|
|
DSInfo[ToF] = NG;
|
|
|
|
assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
|
|
|
|
|
|
|
|
// Change the Function* is the returnnodes map to the ToF.
|
2005-03-15 17:55:04 +01:00
|
|
|
DSNodeHandle Ret = NG->retnodes_begin()->second;
|
2005-01-24 21:00:14 +01:00
|
|
|
NG->getReturnNodes().clear();
|
|
|
|
NG->getReturnNodes()[ToF] = Ret;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-03-24 05:22:04 +01:00
|
|
|
if (const Function *F = getFnForValue(To)) {
|
|
|
|
DSGraph &G = getDSGraph(*F);
|
|
|
|
G.getScalarMap().copyScalarIfExists(From, To);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << *From;
|
|
|
|
std::cerr << *To;
|
|
|
|
assert(0 && "Do not know how to copy this yet!");
|
|
|
|
abort();
|
2005-01-24 21:00:14 +01:00
|
|
|
}
|