//===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===//
-//
+//
// 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 file implements the BUDataStructures class, which represents the
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/DataStructure/DataStructure.h"
+#include "llvm/Analysis/DataStructure/DSGraph.h"
#include "llvm/Module.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/Debug.h"
-#include "DSCallSiteIterator.h"
+#include "llvm/Support/Timer.h"
using namespace llvm;
namespace {
Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph");
Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined");
Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges");
-
+
RegisterAnalysis<BUDataStructures>
X("budatastructure", "Bottom-up Data Structure Analysis");
}
-using namespace DS;
+/// 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]);
+
+ // Next, get the leader element.
+ assert(First == GlobalECs.getLeaderValue(First) &&
+ "First did not end up being the leader?");
+
+ // 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]));
+ }
+
+ // Finally, change the global node to only contain the leader.
+ I->clearGlobals();
+ I->addGlobal(First);
+ }
+
+ 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());
+}
// run - Calculate the bottom up data structure graphs for each function in the
// program.
//
bool BUDataStructures::runOnModule(Module &M) {
LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
- GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph());
+ GlobalECs = LocalDSA.getGlobalECs();
+
+ GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph(), GlobalECs);
GlobalsGraph->setPrintAuxCalls();
IndCallGraphMap = new std::map<std::vector<Function*>,
// 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.
- //
+ //
GlobalsGraph->removeTriviallyDeadNodes();
GlobalsGraph->maskIncompleteMarkers();
+ // Mark external globals incomplete.
+ GlobalsGraph->markIncompleteNodes(DSGraph::IgnoreGlobals);
+
+ // 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()) {
+ NamedRegionTimer X("Bottom-UP EC Cleanup");
+ std::cerr << "Eliminating " << ECGlobals.size() << " EC Globals!\n";
+ for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
+ E = DSInfo.end(); I != E; ++I)
+ EliminateUsesOfECGlobals(*I->second, ECGlobals);
+ }
+
// 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.
if (MainFunc && !MainFunc->isExternal()) {
DSGraph &MainGraph = getOrCreateGraph(MainFunc);
const DSGraph &GG = *MainGraph.getGlobalsGraph();
- ReachabilityCloner RC(MainGraph, GG,
+ ReachabilityCloner RC(MainGraph, GG,
DSGraph::DontCloneCallNodes |
DSGraph::DontCloneAuxCallNodes);
RC.getClonedNH(GG.getNodeForValue(*I));
MainGraph.maskIncompleteMarkers();
- MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
+ MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
DSGraph::IgnoreGlobals);
}
DSGraph *&Graph = DSInfo[F];
if (Graph) return *Graph;
- // Copy the local version into DSInfo...
- Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
+ DSGraph &LocGraph = getAnalysis<LocalDataStructures>().getDSGraph(*F);
+
+ // Steal the local graph.
+ Graph = new DSGraph(GlobalECs, LocGraph.getTargetData());
+ Graph->spliceFrom(LocGraph);
Graph->setGlobalsGraph(GlobalsGraph);
Graph->setPrintAuxCalls();
return *Graph;
}
+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" ||
+ F->getName() == "fscanf" || F->getName() == "malloc" ||
+ F->getName() == "free";
+}
+
+static bool isResolvableFunc(const Function* callee) {
+ return !callee->isExternal() || isVAHackFn(callee);
+}
+
+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;
+ }
+ }
+}
+
+
+/// 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);
+}
+
unsigned BUDataStructures::calculateGraphs(Function *F,
std::vector<Function*> &Stack,
- unsigned &NextID,
+ unsigned &NextID,
hash_map<Function*, unsigned> &ValMap) {
assert(!ValMap.count(F) && "Shouldn't revisit functions!");
unsigned Min = NextID++, MyID = Min;
DSGraph &Graph = getOrCreateGraph(F);
+ // Find all callee functions.
+ std::vector<Function*> CalleeFunctions;
+ GetAllAuxCallees(Graph, CalleeFunctions);
+
// The edges out of the current node are the call site targets...
- for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
- E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
- Function *Callee = *I;
+ for (unsigned i = 0, e = CalleeFunctions.size(); i != e; ++i) {
+ Function *Callee = CalleeFunctions[i];
unsigned M;
// Have we visited the destination function yet?
hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
if (MaxSCC < 1) MaxSCC = 1;
- // Should we revisit the graph?
- if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
+ // Should we revisit the graph? Only do it if there are now new resolvable
+ // callees.
+ GetAllAuxCallees(Graph, CalleeFunctions);
+ if (!CalleeFunctions.empty()) {
ValMap.erase(F);
return calculateGraphs(F, Stack, NextID, ValMap);
} else {
} else {
// SCCFunctions - Keep track of the functions in the current SCC
//
- hash_set<DSGraph*> SCCGraphs;
+ std::vector<DSGraph*> SCCGraphs;
+
+ unsigned SCCSize = 1;
+ Function *NF = Stack.back();
+ ValMap[NF] = ~0U;
+ DSGraph &SCCGraph = getDSGraph(*NF);
- Function *NF;
- std::vector<Function*>::iterator FirstInSCC = Stack.end();
- DSGraph *SCCGraph = 0;
- do {
- NF = *--FirstInSCC;
+ // 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();
ValMap[NF] = ~0U;
- // Figure out which graph is the largest one, in order to speed things up
- // a bit in situations where functions in the SCC have widely different
- // graph sizes.
- DSGraph &NFGraph = getDSGraph(*NF);
- SCCGraphs.insert(&NFGraph);
- // FIXME: If we used a better way of cloning graphs (ie, just splice all
- // of the nodes into the new graph), this would be completely unneeded!
- if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
- SCCGraph = &NFGraph;
- } while (NF != F);
+ DSGraph &NFG = getDSGraph(*NF);
- std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
- << SCCGraphs.size() << "\n";
+ // Update the Function -> DSG map.
+ for (DSGraph::retnodes_iterator I = NFG.retnodes_begin(),
+ E = NFG.retnodes_end(); I != E; ++I)
+ DSInfo[I->first] = &SCCGraph;
- // Compute the Max SCC Size...
- if (MaxSCC < SCCGraphs.size())
- MaxSCC = SCCGraphs.size();
+ SCCGraph.spliceFrom(NFG);
+ delete &NFG;
- // First thing first, collapse all of the DSGraphs into a single graph for
- // the entire SCC. We computed the largest graph, so clone all of the other
- // (smaller) graphs into it. Discard all of the old graphs.
- //
- for (hash_set<DSGraph*>::iterator I = SCCGraphs.begin(),
- E = SCCGraphs.end(); I != E; ++I) {
- DSGraph &G = **I;
- if (&G != SCCGraph) {
- {
- DSGraph::NodeMapTy NodeMap;
- SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
- SCCGraph->getReturnNodes(), NodeMap);
- }
- // Update the DSInfo map and delete the old graph...
- for (DSGraph::retnodes_iterator I = G.retnodes_begin(),
- E = G.retnodes_end(); I != E; ++I)
- DSInfo[I->first] = SCCGraph;
- delete &G;
- }
+ ++SCCSize;
}
+ Stack.pop_back();
+
+ std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
+ << SCCSize << "\n";
+
+ // Compute the Max SCC Size.
+ if (MaxSCC < SCCSize)
+ MaxSCC = SCCSize;
// Clean up the graph before we start inlining a bunch again...
- SCCGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
+ SCCGraph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
// Now that we have one big happy family, resolve all of the call sites in
// the graph...
- calculateGraph(*SCCGraph);
- DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
- << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
+ calculateGraph(SCCGraph);
+ DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph.getGraphSize()
+ << "+" << SCCGraph.getAuxFunctionCalls().size() << "]\n");
std::cerr << "DONE with SCC #: " << MyID << "\n";
// We never have to revisit "SCC" processed functions...
-
- // Drop the stuff we don't need from the end of the stack
- Stack.erase(FirstInSCC, Stack.end());
return MyID;
}
// releaseMemory - If the pass pipeline is done with this pass, we can release
// our memory... here...
//
-void BUDataStructures::releaseMemory() {
+void BUDataStructures::releaseMyMemory() {
for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
E = DSInfo.end(); I != E; ++I) {
I->second->getReturnNodes().erase(I->first);
GlobalsGraph = 0;
}
-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" ||
- F->getName() == "fscanf";
-}
+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();
-// isUnresolvableFunction - Return true if this is an unresolvable
-// external function. A direct or indirect call to this cannot be resolved.
-//
-static bool isResolvableFunc(const Function* callee) {
- return !callee->isExternal() || isVAHackFn(callee);
+ // Add function to the graph.
+ DSG->getReturnNodes().insert(std::make_pair(F, DSNodeHandle()));
+
+ if (F->getName() == "free") { // Taking the address of free.
+
+ // 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;
}
+
void BUDataStructures::calculateGraph(DSGraph &Graph) {
// 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!
CalledFuncs.clear();
- if (CS.isDirectCall()) {
- Function *F = CS.getCalleeFunc();
- if (isResolvableFunc(F))
- if (F->isExternal()) { // Call to fprintf, etc.
- TempFCs.erase(TempFCs.begin());
- continue;
- } else {
- CalledFuncs.push_back(F);
- }
- } else {
- DSNode *Node = CS.getCalleeNode();
-
- if (!Node->isIncomplete())
- for (unsigned i = 0, e = Node->getGlobals().size(); i != e; ++i)
- if (Function *CF = dyn_cast<Function>(Node->getGlobals()[i]))
- if (isResolvableFunc(CF) && !CF->isExternal())
- CalledFuncs.push_back(CF);
+ // Fast path for noop calls. Note that we don't care about merging globals
+ // in the callee with nodes in the caller here.
+ if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0) {
+ 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];
- ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
- Callee));
+ ActualCallees.insert(std::make_pair(TheCall, Callee));
// Get the data structure graph for the called function.
GI = &getDSGraph(*Callee); // Graph to inline
<< Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
<< Graph.getAuxFunctionCalls().size() << "]\n");
Graph.mergeInGraph(CS, *Callee, *GI,
- DSGraph::KeepModRefBits |
DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes);
++NumBUInlines;
} else {
if (!Printed)
std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n";
std::cerr << " calls " << CalledFuncs.size()
- << " fns from site: " << CS.getCallSite().getInstruction()
+ << " fns from site: " << CS.getCallSite().getInstruction()
<< " " << *CS.getCallSite().getInstruction();
- unsigned NumToPrint = CalledFuncs.size();
- if (NumToPrint > 8) NumToPrint = 8;
std::cerr << " Fns =";
+ unsigned NumPrinted = 0;
+
for (std::vector<Function*>::iterator I = CalledFuncs.begin(),
- E = CalledFuncs.end(); I != E && NumToPrint; ++I, --NumToPrint)
- std::cerr << " " << (*I)->getName();
+ E = CalledFuncs.end(); I != E; ++I) {
+ if (NumPrinted++ < 8) std::cerr << " " << (*I)->getName();
+
+ // Add the call edges to the call graph.
+ ActualCallees.insert(std::make_pair(TheCall, *I));
+ }
std::cerr << "\n";
// See if we already computed a graph for this set of callees.
if (IndCallGraph.first == 0) {
std::vector<Function*>::iterator I = CalledFuncs.begin(),
E = CalledFuncs.end();
-
+
// Start with a copy of the first graph.
- GI = IndCallGraph.first = new DSGraph(getDSGraph(**I));
+ GI = IndCallGraph.first = new DSGraph(getDSGraph(**I), GlobalECs);
GI->setGlobalsGraph(Graph.getGlobalsGraph());
std::vector<DSNodeHandle> &Args = IndCallGraph.second;
// If the graph already contains the nodes for the function, don't
// bother merging it in again.
if (!GI->containsFunction(*I)) {
- DSGraph::NodeMapTy NodeMap;
- GI->cloneInto(getDSGraph(**I), GI->getScalarMap(),
- GI->getReturnNodes(), NodeMap);
+ GI->cloneInto(getDSGraph(**I));
++NumBUInlines;
}
for (e = NextArgs.size(); i != e; ++i)
Args.push_back(NextArgs[i]);
}
-
+
// Clean up the final graph!
GI->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
} else {
<< Graph.getAuxFunctionCalls().size() << "]\n");
Graph.mergeInGraph(CS, IndCallGraph.second, *GI,
- DSGraph::KeepModRefBits |
DSGraph::StripAllocaBit |
DSGraph::DontCloneCallNodes);
++NumBUInlines;
}
// Recompute the Incomplete markers
- assert(Graph.getInlinedGlobals().empty());
Graph.maskIncompleteMarkers();
Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
// Clone everything reachable from globals in the function graph into the
// globals graph.
for (DSScalarMap::global_iterator I = MainSM.global_begin(),
- E = MainSM.global_end(); I != E; ++I)
+ E = MainSM.global_end(); I != E; ++I)
RC.getClonedNH(MainSM[*I]);
//Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
if (Function *FromF = dyn_cast<Function>(From)) {
Function *ToF = cast<Function>(To);
assert(!DSInfo.count(ToF) && "New Function already exists!");
- DSGraph *NG = new DSGraph(getDSGraph(*FromF));
+ DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs);
DSInfo[ToF] = NG;
assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
return;
}
- assert(!isa<GlobalVariable>(From) && "Do not know how to copy GV's yet!");
+ 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();
}