mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-08-09 11:25:55 +00:00
Remove DSA.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@32550 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -1,753 +0,0 @@
|
|||||||
//===- 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
|
|
||||||
// Bottom-Up Interprocedural closure of the data structure graph over the
|
|
||||||
// program. This is useful for applications like pool allocation, but **not**
|
|
||||||
// applications like alias analysis.
|
|
||||||
//
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
#define DEBUG_TYPE "bu_dsa"
|
|
||||||
#include "llvm/Analysis/DataStructure/DataStructure.h"
|
|
||||||
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
|
||||||
#include "llvm/Module.h"
|
|
||||||
#include "llvm/DerivedTypes.h"
|
|
||||||
#include "llvm/ADT/Statistic.h"
|
|
||||||
#include "llvm/Support/CommandLine.h"
|
|
||||||
#include "llvm/Support/Debug.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");
|
|
||||||
|
|
||||||
cl::opt<bool>
|
|
||||||
AddGlobals("budatastructures-annotate-calls", cl::Hidden,
|
|
||||||
cl::desc("Annotate call sites with functions as they are resolved"));
|
|
||||||
cl::opt<bool>
|
|
||||||
UpdateGlobals("budatastructures-update-from-globals", cl::Hidden,
|
|
||||||
cl::desc("Update local graph from global graph when processing function"));
|
|
||||||
|
|
||||||
RegisterPass<BUDataStructures>
|
|
||||||
X("budatastructure", "Bottom-up Data Structure Analysis");
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool GetAllCalleesN(const DSCallSite &CS,
|
|
||||||
std::vector<Function*> &Callees);
|
|
||||||
|
|
||||||
/// 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());
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
DOUT << "Adding " << F->getName() << " to a call node in "
|
|
||||||
<< D.getCaller().getName() << "\n";
|
|
||||||
}
|
|
||||||
DL.getCalleeNode()->mergeWith(NHF, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// run - Calculate the bottom up data structure graphs for each function in the
|
|
||||||
// program.
|
|
||||||
//
|
|
||||||
bool BUDataStructures::runOnModule(Module &M) {
|
|
||||||
LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
|
|
||||||
GlobalECs = LocalDSA.getGlobalECs();
|
|
||||||
|
|
||||||
GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph(), GlobalECs);
|
|
||||||
GlobalsGraph->setPrintAuxCalls();
|
|
||||||
|
|
||||||
IndCallGraphMap = new std::map<std::vector<Function*>,
|
|
||||||
std::pair<DSGraph*, std::vector<DSNodeHandle> > >();
|
|
||||||
|
|
||||||
std::vector<Function*> Stack;
|
|
||||||
hash_map<Function*, unsigned> ValMap;
|
|
||||||
unsigned NextID = 1;
|
|
||||||
|
|
||||||
Function *MainFunc = M.getMainFunction();
|
|
||||||
if (MainFunc)
|
|
||||||
calculateGraphs(MainFunc, Stack, NextID, ValMap);
|
|
||||||
|
|
||||||
// Calculate the graphs for any functions that are unreachable from main...
|
|
||||||
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
|
||||||
if (!I->isExternal() && !DSInfo.count(I)) {
|
|
||||||
if (MainFunc)
|
|
||||||
DOUT << "*** BU: Function unreachable from main: "
|
|
||||||
<< I->getName() << "\n";
|
|
||||||
calculateGraphs(I, Stack, NextID, ValMap); // Calculate all graphs.
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we computed any temporary indcallgraphs, free them now.
|
|
||||||
for (std::map<std::vector<Function*>,
|
|
||||||
std::pair<DSGraph*, std::vector<DSNodeHandle> > >::iterator I =
|
|
||||||
IndCallGraphMap->begin(), E = IndCallGraphMap->end(); I != E; ++I) {
|
|
||||||
I->second.second.clear(); // Drop arg refs into the graph.
|
|
||||||
delete I->second.first;
|
|
||||||
}
|
|
||||||
delete IndCallGraphMap;
|
|
||||||
|
|
||||||
// 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
|
|
||||||
// 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");
|
|
||||||
DOUT << "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,
|
|
||||||
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));
|
|
||||||
|
|
||||||
MainGraph.maskIncompleteMarkers();
|
|
||||||
MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
|
|
||||||
DSGraph::IgnoreGlobals);
|
|
||||||
|
|
||||||
//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(),
|
|
||||||
ee = MainGraph.afc_end(); ii != ee; ++ii) {
|
|
||||||
std::vector<Function*> Funcs;
|
|
||||||
GetAllCalleesN(*ii, Funcs);
|
|
||||||
DOUT << "Lost site\n";
|
|
||||||
DEBUG(ii->getCallSite().getInstruction()->dump());
|
|
||||||
for (std::vector<Function*>::iterator iif = Funcs.begin(), eef = Funcs.end();
|
|
||||||
iif != eef; ++iif) {
|
|
||||||
AddGlobalToNode(this, *ii, *iif);
|
|
||||||
DOUT << "Adding\n";
|
|
||||||
ActualCallees.insert(std::make_pair(ii->getCallSite().getInstruction(), *iif));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
NumCallEdges += ActualCallees.size();
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
|
|
||||||
// Has the graph already been created?
|
|
||||||
DSGraph *&Graph = DSInfo[F];
|
|
||||||
if (Graph) return *Graph;
|
|
||||||
|
|
||||||
DSGraph &LocGraph = getAnalysis<LocalDataStructures>().getDSGraph(*F);
|
|
||||||
|
|
||||||
// Steal the local graph.
|
|
||||||
Graph = new DSGraph(GlobalECs, LocGraph.getTargetData());
|
|
||||||
Graph->spliceFrom(LocGraph);
|
|
||||||
|
|
||||||
Graph->setGlobalsGraph(GlobalsGraph);
|
|
||||||
Graph->setPrintAuxCalls();
|
|
||||||
|
|
||||||
// Start with a copy of the original call sites...
|
|
||||||
Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//returns true if all callees were resolved
|
|
||||||
static bool GetAllCalleesN(const DSCallSite &CS,
|
|
||||||
std::vector<Function*> &Callees) {
|
|
||||||
if (CS.isDirectCall()) {
|
|
||||||
if (isResolvableFunc(CS.getCalleeFunc())) {
|
|
||||||
Callees.push_back(CS.getCalleeFunc());
|
|
||||||
return true;
|
|
||||||
} else
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
// Get all callees.
|
|
||||||
bool retval = CS.getCalleeNode()->isComplete();
|
|
||||||
unsigned OldSize = Callees.size();
|
|
||||||
CS.getCalleeNode()->addFullFunctionList(Callees);
|
|
||||||
|
|
||||||
// If any of the callees are unresolvable, remove that one
|
|
||||||
for (unsigned i = OldSize; i != Callees.size(); ++i)
|
|
||||||
if (!isResolvableFunc(Callees[i])) {
|
|
||||||
Callees.erase(Callees.begin()+i);
|
|
||||||
--i;
|
|
||||||
retval = false;
|
|
||||||
}
|
|
||||||
return retval;
|
|
||||||
//return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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,
|
|
||||||
hash_map<Function*, unsigned> &ValMap) {
|
|
||||||
assert(!ValMap.count(F) && "Shouldn't revisit functions!");
|
|
||||||
unsigned Min = NextID++, MyID = Min;
|
|
||||||
ValMap[F] = Min;
|
|
||||||
Stack.push_back(F);
|
|
||||||
|
|
||||||
// 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!
|
|
||||||
if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
|
|
||||||
// No callees!
|
|
||||||
Stack.pop_back();
|
|
||||||
ValMap[F] = ~0;
|
|
||||||
return Min;
|
|
||||||
}
|
|
||||||
|
|
||||||
DSGraph &Graph = getOrCreateGraph(F);
|
|
||||||
if (UpdateGlobals)
|
|
||||||
Graph.updateFromGlobalGraph();
|
|
||||||
|
|
||||||
// Find all callee functions.
|
|
||||||
std::vector<Function*> CalleeFunctions;
|
|
||||||
GetAllAuxCallees(Graph, CalleeFunctions);
|
|
||||||
|
|
||||||
// The edges out of the current node are the call site targets...
|
|
||||||
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 (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.
|
|
||||||
DOUT << "Visiting single node SCC #: " << MyID << " fn: "
|
|
||||||
<< F->getName() << "\n";
|
|
||||||
Stack.pop_back();
|
|
||||||
DSGraph &G = getDSGraph(*F);
|
|
||||||
DOUT << " [BU] Calculating graph for: " << F->getName()<< "\n";
|
|
||||||
calculateGraph(G);
|
|
||||||
DOUT << " [BU] Done inlining: " << F->getName() << " ["
|
|
||||||
<< G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
|
|
||||||
<< "]\n";
|
|
||||||
|
|
||||||
if (MaxSCC < 1) MaxSCC = 1;
|
|
||||||
|
|
||||||
// Should we revisit the graph? Only do it if there are now new resolvable
|
|
||||||
// callees.
|
|
||||||
GetAllAuxCallees(Graph, CalleeFunctions);
|
|
||||||
if (!CalleeFunctions.empty()) {
|
|
||||||
DOUT << "Recalculating " << F->getName() << " due to new knowledge\n";
|
|
||||||
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
|
|
||||||
//
|
|
||||||
std::vector<DSGraph*> SCCGraphs;
|
|
||||||
|
|
||||||
unsigned SCCSize = 1;
|
|
||||||
Function *NF = Stack.back();
|
|
||||||
ValMap[NF] = ~0U;
|
|
||||||
DSGraph &SCCGraph = getDSGraph(*NF);
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
|
|
||||||
DSGraph &NFG = getDSGraph(*NF);
|
|
||||||
|
|
||||||
// Update the Function -> DSG map.
|
|
||||||
for (DSGraph::retnodes_iterator I = NFG.retnodes_begin(),
|
|
||||||
E = NFG.retnodes_end(); I != E; ++I)
|
|
||||||
DSInfo[I->first] = &SCCGraph;
|
|
||||||
|
|
||||||
SCCGraph.spliceFrom(NFG);
|
|
||||||
delete &NFG;
|
|
||||||
|
|
||||||
++SCCSize;
|
|
||||||
}
|
|
||||||
Stack.pop_back();
|
|
||||||
|
|
||||||
DOUT << "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);
|
|
||||||
|
|
||||||
// Now that we have one big happy family, resolve all of the call sites in
|
|
||||||
// the graph...
|
|
||||||
calculateGraph(SCCGraph);
|
|
||||||
DOUT << " [BU] Done inlining SCC [" << SCCGraph.getGraphSize()
|
|
||||||
<< "+" << SCCGraph.getAuxFunctionCalls().size() << "]\n"
|
|
||||||
<< "DONE with SCC #: " << MyID << "\n";
|
|
||||||
|
|
||||||
// We never have to revisit "SCC" processed functions...
|
|
||||||
return MyID;
|
|
||||||
}
|
|
||||||
|
|
||||||
return MyID; // == Min
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// releaseMemory - If the pass pipeline is done with this pass, we can release
|
|
||||||
// our memory... here...
|
|
||||||
//
|
|
||||||
void BUDataStructures::releaseMyMemory() {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Empty map so next time memory is released, data structures are not
|
|
||||||
// re-deleted.
|
|
||||||
DSInfo.clear();
|
|
||||||
delete GlobalsGraph;
|
|
||||||
GlobalsGraph = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
// 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 {
|
|
||||||
cerr << "Unrecognized external function: " << F->getName() << "\n";
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
return *DSG;
|
|
||||||
}
|
|
||||||
|
|
||||||
void BUDataStructures::calculateGraph(DSGraph &Graph) {
|
|
||||||
// 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));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// 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!
|
|
||||||
std::list<DSCallSite> TempFCs;
|
|
||||||
std::list<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
|
|
||||||
TempFCs.swap(AuxCallsList);
|
|
||||||
|
|
||||||
bool Printed = false;
|
|
||||||
std::vector<Function*> CalledFuncs;
|
|
||||||
while (!TempFCs.empty()) {
|
|
||||||
DSCallSite &CS = *TempFCs.begin();
|
|
||||||
|
|
||||||
CalledFuncs.clear();
|
|
||||||
|
|
||||||
// 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(TheCall, Callee));
|
|
||||||
|
|
||||||
// Get the data structure graph for the called function.
|
|
||||||
GI = &getDSGraph(*Callee); // Graph to inline
|
|
||||||
DOUT << " Inlining graph for " << Callee->getName()
|
|
||||||
<< "[" << GI->getGraphSize() << "+"
|
|
||||||
<< GI->getAuxFunctionCalls().size() << "] into '"
|
|
||||||
<< Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
|
|
||||||
<< Graph.getAuxFunctionCalls().size() << "]\n";
|
|
||||||
Graph.mergeInGraph(CS, *Callee, *GI,
|
|
||||||
DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes);
|
|
||||||
++NumBUInlines;
|
|
||||||
} else {
|
|
||||||
if (!Printed)
|
|
||||||
cerr << "In Fns: " << Graph.getFunctionNames() << "\n";
|
|
||||||
cerr << " calls " << CalledFuncs.size()
|
|
||||||
<< " fns from site: " << CS.getCallSite().getInstruction()
|
|
||||||
<< " " << *CS.getCallSite().getInstruction();
|
|
||||||
cerr << " Fns =";
|
|
||||||
unsigned NumPrinted = 0;
|
|
||||||
|
|
||||||
for (std::vector<Function*>::iterator I = CalledFuncs.begin(),
|
|
||||||
E = CalledFuncs.end(); I != E; ++I) {
|
|
||||||
if (NumPrinted++ < 8) cerr << " " << (*I)->getName();
|
|
||||||
|
|
||||||
// Add the call edges to the call graph.
|
|
||||||
ActualCallees.insert(std::make_pair(TheCall, *I));
|
|
||||||
}
|
|
||||||
cerr << "\n";
|
|
||||||
|
|
||||||
// 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 =
|
|
||||||
(*IndCallGraphMap)[CalledFuncs];
|
|
||||||
|
|
||||||
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), GlobalECs);
|
|
||||||
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.
|
|
||||||
for (++I; I != E; ++I) {
|
|
||||||
// If the graph already contains the nodes for the function, don't
|
|
||||||
// bother merging it in again.
|
|
||||||
if (!GI->containsFunction(*I)) {
|
|
||||||
GI->cloneInto(getDSGraph(**I));
|
|
||||||
++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]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clean up the final graph!
|
|
||||||
GI->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
|
|
||||||
} else {
|
|
||||||
cerr << "***\n*** RECYCLED GRAPH ***\n***\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
GI = IndCallGraph.first;
|
|
||||||
|
|
||||||
// Merge the unified graph into this graph now.
|
|
||||||
DOUT << " Inlining multi callee graph "
|
|
||||||
<< "[" << GI->getGraphSize() << "+"
|
|
||||||
<< GI->getAuxFunctionCalls().size() << "] into '"
|
|
||||||
<< Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
|
|
||||||
<< Graph.getAuxFunctionCalls().size() << "]\n";
|
|
||||||
|
|
||||||
Graph.mergeInGraph(CS, IndCallGraph.second, *GI,
|
|
||||||
DSGraph::StripAllocaBit |
|
|
||||||
DSGraph::DontCloneCallNodes);
|
|
||||||
++NumBUInlines;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
TempFCs.erase(TempFCs.begin());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Recompute the Incomplete markers
|
|
||||||
Graph.maskIncompleteMarkers();
|
|
||||||
Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
|
|
||||||
|
|
||||||
// Delete dead nodes. Treat globals that are unreachable but that can
|
|
||||||
// reach live nodes as live.
|
|
||||||
Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
RC.getClonedNH(MainSM[*I]);
|
|
||||||
|
|
||||||
//Graph.writeGraphToFile(cerr, "bu_" + F.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Function *F = dyn_cast<Function>(V)) {
|
|
||||||
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!");
|
|
||||||
DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs);
|
|
||||||
DSInfo[ToF] = NG;
|
|
||||||
assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
|
|
||||||
|
|
||||||
// Change the Function* is the returnnodes map to the ToF.
|
|
||||||
DSNodeHandle Ret = NG->retnodes_begin()->second;
|
|
||||||
NG->getReturnNodes().clear();
|
|
||||||
NG->getReturnNodes()[ToF] = Ret;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (const Function *F = getFnForValue(To)) {
|
|
||||||
DSGraph &G = getDSGraph(*F);
|
|
||||||
G.getScalarMap().copyScalarIfExists(From, To);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
cerr << *From;
|
|
||||||
cerr << *To;
|
|
||||||
assert(0 && "Do not know how to copy this yet!");
|
|
||||||
abort();
|
|
||||||
}
|
|
@@ -1,128 +0,0 @@
|
|||||||
//=- lib/Analysis/IPA/CallTargets.cpp - Resolve Call Targets --*- C++ -*-=====//
|
|
||||||
//
|
|
||||||
// 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 uses DSA to map targets of all calls, and reports on if it
|
|
||||||
// thinks it knows all targets of a given call.
|
|
||||||
//
|
|
||||||
// Loop over all callsites, and lookup the DSNode for that site. Pull the
|
|
||||||
// Functions from the node as callees.
|
|
||||||
// This is essentially a utility pass to simplify later passes that only depend
|
|
||||||
// on call sites and callees to operate (such as a devirtualizer).
|
|
||||||
//
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
#include "llvm/Analysis/DataStructure/CallTargets.h"
|
|
||||||
#include "llvm/Module.h"
|
|
||||||
#include "llvm/Instructions.h"
|
|
||||||
#include "llvm/Analysis/DataStructure/DataStructure.h"
|
|
||||||
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
|
||||||
#include "llvm/ADT/Statistic.h"
|
|
||||||
#include "llvm/Support/Streams.h"
|
|
||||||
#include "llvm/Constants.h"
|
|
||||||
#include <ostream>
|
|
||||||
using namespace llvm;
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
Statistic DirCall("calltarget", "Number of direct calls");
|
|
||||||
Statistic IndCall("calltarget", "Number of indirect calls");
|
|
||||||
Statistic CompleteInd("calltarget", "Number of complete indirect calls");
|
|
||||||
Statistic CompleteEmpty("calltarget", "Number of complete empty calls");
|
|
||||||
|
|
||||||
RegisterPass<CallTargetFinder> X("calltarget","Find Call Targets (uses DSA)");
|
|
||||||
}
|
|
||||||
|
|
||||||
void CallTargetFinder::findIndTargets(Module &M)
|
|
||||||
{
|
|
||||||
TDDataStructures* T = &getAnalysis<TDDataStructures>();
|
|
||||||
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
|
||||||
if (!I->isExternal())
|
|
||||||
for (Function::iterator F = I->begin(), FE = I->end(); F != FE; ++F)
|
|
||||||
for (BasicBlock::iterator B = F->begin(), BE = F->end(); B != BE; ++B)
|
|
||||||
if (isa<CallInst>(B) || isa<InvokeInst>(B)) {
|
|
||||||
CallSite cs = CallSite::get(B);
|
|
||||||
AllSites.push_back(cs);
|
|
||||||
if (!cs.getCalledFunction()) {
|
|
||||||
IndCall++;
|
|
||||||
DSNode* N = T->getDSGraph(*cs.getCaller())
|
|
||||||
.getNodeForValue(cs.getCalledValue()).getNode();
|
|
||||||
N->addFullFunctionList(IndMap[cs]);
|
|
||||||
if (N->isComplete() && IndMap[cs].size()) {
|
|
||||||
CompleteSites.insert(cs);
|
|
||||||
++CompleteInd;
|
|
||||||
}
|
|
||||||
if (N->isComplete() && !IndMap[cs].size()) {
|
|
||||||
++CompleteEmpty;
|
|
||||||
cerr << "Call site empty: '"
|
|
||||||
<< cs.getInstruction()->getName()
|
|
||||||
<< "' In '"
|
|
||||||
<< cs.getInstruction()->getParent()->getParent()->getName()
|
|
||||||
<< "'\n";
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
++DirCall;
|
|
||||||
IndMap[cs].push_back(cs.getCalledFunction());
|
|
||||||
CompleteSites.insert(cs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void CallTargetFinder::print(std::ostream &O, const Module *M) const
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
O << "[* = incomplete] CS: func list\n";
|
|
||||||
for (std::map<CallSite, std::vector<Function*> >::const_iterator ii =
|
|
||||||
IndMap.begin(),
|
|
||||||
ee = IndMap.end(); ii != ee; ++ii) {
|
|
||||||
if (!ii->first.getCalledFunction()) { //only print indirect
|
|
||||||
if (!isComplete(ii->first)) {
|
|
||||||
O << "* ";
|
|
||||||
CallSite cs = ii->first;
|
|
||||||
cs.getInstruction()->dump();
|
|
||||||
O << cs.getInstruction()->getParent()->getParent()->getName() << " "
|
|
||||||
<< cs.getInstruction()->getName() << " ";
|
|
||||||
}
|
|
||||||
O << ii->first.getInstruction() << ":";
|
|
||||||
for (std::vector<Function*>::const_iterator i = ii->second.begin(),
|
|
||||||
e = ii->second.end(); i != e; ++i) {
|
|
||||||
O << " " << (*i)->getName();
|
|
||||||
}
|
|
||||||
O << "\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CallTargetFinder::runOnModule(Module &M) {
|
|
||||||
findIndTargets(M);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CallTargetFinder::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
||||||
AU.setPreservesAll();
|
|
||||||
AU.addRequired<TDDataStructures>();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<Function*>::iterator CallTargetFinder::begin(CallSite cs) {
|
|
||||||
return IndMap[cs].begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<Function*>::iterator CallTargetFinder::end(CallSite cs) {
|
|
||||||
return IndMap[cs].end();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CallTargetFinder::isComplete(CallSite cs) const {
|
|
||||||
return CompleteSites.find(cs) != CompleteSites.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::list<CallSite>::iterator CallTargetFinder::cs_begin() {
|
|
||||||
return AllSites.begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::list<CallSite>::iterator CallTargetFinder::cs_end() {
|
|
||||||
return AllSites.end();
|
|
||||||
}
|
|
@@ -1,239 +0,0 @@
|
|||||||
//===- CompleteBottomUp.cpp - Complete Bottom-Up Data Structure Graphs ----===//
|
|
||||||
//
|
|
||||||
// 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 is the exact same as the bottom-up graphs, but we use take a completed
|
|
||||||
// call graph and inline all indirect callees into their callers graphs, making
|
|
||||||
// the result more useful for things like pool allocation.
|
|
||||||
//
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
#define DEBUG_TYPE "cbudatastructure"
|
|
||||||
#include "llvm/Analysis/DataStructure/DataStructure.h"
|
|
||||||
#include "llvm/Module.h"
|
|
||||||
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
|
||||||
#include "llvm/Support/Debug.h"
|
|
||||||
#include "llvm/ADT/SCCIterator.h"
|
|
||||||
#include "llvm/ADT/Statistic.h"
|
|
||||||
#include "llvm/ADT/STLExtras.h"
|
|
||||||
using namespace llvm;
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
RegisterPass<CompleteBUDataStructures>
|
|
||||||
X("cbudatastructure", "'Complete' Bottom-up Data Structure Analysis");
|
|
||||||
Statistic NumCBUInlines("cbudatastructures", "Number of graphs inlined");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// run - Calculate the bottom up data structure graphs for each function in the
|
|
||||||
// program.
|
|
||||||
//
|
|
||||||
bool CompleteBUDataStructures::runOnModule(Module &M) {
|
|
||||||
BUDataStructures &BU = getAnalysis<BUDataStructures>();
|
|
||||||
GlobalECs = BU.getGlobalECs();
|
|
||||||
GlobalsGraph = new DSGraph(BU.getGlobalsGraph(), GlobalECs);
|
|
||||||
GlobalsGraph->setPrintAuxCalls();
|
|
||||||
|
|
||||||
// Our call graph is the same as the BU data structures call graph
|
|
||||||
ActualCallees = BU.getActualCallees();
|
|
||||||
|
|
||||||
std::vector<DSGraph*> Stack;
|
|
||||||
hash_map<DSGraph*, unsigned> ValMap;
|
|
||||||
unsigned NextID = 1;
|
|
||||||
|
|
||||||
Function *MainFunc = M.getMainFunction();
|
|
||||||
if (MainFunc) {
|
|
||||||
if (!MainFunc->isExternal())
|
|
||||||
calculateSCCGraphs(getOrCreateGraph(*MainFunc), Stack, NextID, ValMap);
|
|
||||||
} else {
|
|
||||||
DOUT << "CBU-DSA: No 'main' function found!\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
|
||||||
if (!I->isExternal() && !DSInfo.count(I)) {
|
|
||||||
if (MainFunc) {
|
|
||||||
DOUT << "*** CBU: Function unreachable from main: "
|
|
||||||
<< I->getName() << "\n";
|
|
||||||
}
|
|
||||||
calculateSCCGraphs(getOrCreateGraph(*I), Stack, NextID, ValMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
GlobalsGraph->removeTriviallyDeadNodes();
|
|
||||||
|
|
||||||
|
|
||||||
// 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,
|
|
||||||
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));
|
|
||||||
|
|
||||||
MainGraph.maskIncompleteMarkers();
|
|
||||||
MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
|
|
||||||
DSGraph::IgnoreGlobals);
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
DSGraph &CompleteBUDataStructures::getOrCreateGraph(Function &F) {
|
|
||||||
// Has the graph already been created?
|
|
||||||
DSGraph *&Graph = DSInfo[&F];
|
|
||||||
if (Graph) return *Graph;
|
|
||||||
|
|
||||||
// Copy the BU graph...
|
|
||||||
Graph = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F), GlobalECs);
|
|
||||||
Graph->setGlobalsGraph(GlobalsGraph);
|
|
||||||
Graph->setPrintAuxCalls();
|
|
||||||
|
|
||||||
// Make sure to update the DSInfo map for all of the functions currently in
|
|
||||||
// this graph!
|
|
||||||
for (DSGraph::retnodes_iterator I = Graph->retnodes_begin();
|
|
||||||
I != Graph->retnodes_end(); ++I)
|
|
||||||
DSInfo[I->first] = Graph;
|
|
||||||
|
|
||||||
return *Graph;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
unsigned CompleteBUDataStructures::calculateSCCGraphs(DSGraph &FG,
|
|
||||||
std::vector<DSGraph*> &Stack,
|
|
||||||
unsigned &NextID,
|
|
||||||
hash_map<DSGraph*, unsigned> &ValMap) {
|
|
||||||
assert(!ValMap.count(&FG) && "Shouldn't revisit functions!");
|
|
||||||
unsigned Min = NextID++, MyID = Min;
|
|
||||||
ValMap[&FG] = Min;
|
|
||||||
Stack.push_back(&FG);
|
|
||||||
|
|
||||||
// The edges out of the current node are the call site targets...
|
|
||||||
for (DSGraph::fc_iterator CI = FG.fc_begin(), CE = FG.fc_end();
|
|
||||||
CI != CE; ++CI) {
|
|
||||||
Instruction *Call = CI->getCallSite().getInstruction();
|
|
||||||
|
|
||||||
// Loop over all of the actually called functions...
|
|
||||||
callee_iterator I = callee_begin(Call), E = callee_end(Call);
|
|
||||||
for (; I != E && I->first == Call; ++I) {
|
|
||||||
assert(I->first == Call && "Bad callee construction!");
|
|
||||||
if (!I->second->isExternal()) {
|
|
||||||
DSGraph &Callee = getOrCreateGraph(*I->second);
|
|
||||||
unsigned M;
|
|
||||||
// Have we visited the destination function yet?
|
|
||||||
hash_map<DSGraph*, unsigned>::iterator It = ValMap.find(&Callee);
|
|
||||||
if (It == ValMap.end()) // No, visit it now.
|
|
||||||
M = calculateSCCGraphs(Callee, Stack, NextID, ValMap);
|
|
||||||
else // Yes, get it's number.
|
|
||||||
M = It->second;
|
|
||||||
if (M < Min) Min = M;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(ValMap[&FG] == 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.
|
|
||||||
bool IsMultiNodeSCC = false;
|
|
||||||
while (Stack.back() != &FG) {
|
|
||||||
DSGraph *NG = Stack.back();
|
|
||||||
ValMap[NG] = ~0U;
|
|
||||||
|
|
||||||
FG.cloneInto(*NG);
|
|
||||||
|
|
||||||
// Update the DSInfo map and delete the old graph...
|
|
||||||
for (DSGraph::retnodes_iterator I = NG->retnodes_begin();
|
|
||||||
I != NG->retnodes_end(); ++I)
|
|
||||||
DSInfo[I->first] = &FG;
|
|
||||||
|
|
||||||
// Remove NG from the ValMap since the pointer may get recycled.
|
|
||||||
ValMap.erase(NG);
|
|
||||||
delete NG;
|
|
||||||
|
|
||||||
Stack.pop_back();
|
|
||||||
IsMultiNodeSCC = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clean up the graph before we start inlining a bunch again...
|
|
||||||
if (IsMultiNodeSCC)
|
|
||||||
FG.removeTriviallyDeadNodes();
|
|
||||||
|
|
||||||
Stack.pop_back();
|
|
||||||
processGraph(FG);
|
|
||||||
ValMap[&FG] = ~0U;
|
|
||||||
return MyID;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// processGraph - Process the BU graphs for the program in bottom-up order on
|
|
||||||
/// the SCC of the __ACTUAL__ call graph. This builds "complete" BU graphs.
|
|
||||||
void CompleteBUDataStructures::processGraph(DSGraph &G) {
|
|
||||||
hash_set<Instruction*> calls;
|
|
||||||
|
|
||||||
// The edges out of the current node are the call site targets...
|
|
||||||
unsigned i = 0;
|
|
||||||
for (DSGraph::fc_iterator CI = G.fc_begin(), CE = G.fc_end(); CI != CE;
|
|
||||||
++CI, ++i) {
|
|
||||||
const DSCallSite &CS = *CI;
|
|
||||||
Instruction *TheCall = CS.getCallSite().getInstruction();
|
|
||||||
|
|
||||||
assert(calls.insert(TheCall).second &&
|
|
||||||
"Call instruction occurs multiple times in graph??");
|
|
||||||
|
|
||||||
// Fast path for noop calls. Note that we don't care about merging globals
|
|
||||||
// in the callee with nodes in the caller here.
|
|
||||||
if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// Loop over all of the potentially called functions...
|
|
||||||
// Inline direct calls as well as indirect calls because the direct
|
|
||||||
// callee may have indirect callees and so may have changed.
|
|
||||||
//
|
|
||||||
callee_iterator I = callee_begin(TheCall),E = callee_end(TheCall);
|
|
||||||
unsigned TNum = 0, Num = 0;
|
|
||||||
DEBUG(Num = std::distance(I, E));
|
|
||||||
for (; I != E; ++I, ++TNum) {
|
|
||||||
assert(I->first == TheCall && "Bad callee construction!");
|
|
||||||
Function *CalleeFunc = I->second;
|
|
||||||
if (!CalleeFunc->isExternal()) {
|
|
||||||
// Merge the callee's graph into this graph. This works for normal
|
|
||||||
// calls or for self recursion within an SCC.
|
|
||||||
DSGraph &GI = getOrCreateGraph(*CalleeFunc);
|
|
||||||
++NumCBUInlines;
|
|
||||||
G.mergeInGraph(CS, *CalleeFunc, GI,
|
|
||||||
DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes |
|
|
||||||
DSGraph::DontCloneAuxCallNodes);
|
|
||||||
DOUT << " Inlining graph [" << i << "/"
|
|
||||||
<< G.getFunctionCalls().size()-1
|
|
||||||
<< ":" << TNum << "/" << Num-1 << "] for "
|
|
||||||
<< CalleeFunc->getName() << "["
|
|
||||||
<< GI.getGraphSize() << "+" << GI.getAuxFunctionCalls().size()
|
|
||||||
<< "] into '" /*<< G.getFunctionNames()*/ << "' ["
|
|
||||||
<< G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
|
|
||||||
<< "]\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Recompute the Incomplete markers
|
|
||||||
G.maskIncompleteMarkers();
|
|
||||||
G.markIncompleteNodes(DSGraph::MarkFormalArgs);
|
|
||||||
|
|
||||||
// Delete dead nodes. Treat globals that are unreachable but that can
|
|
||||||
// reach live nodes as live.
|
|
||||||
G.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load Diff
@@ -1,300 +0,0 @@
|
|||||||
//===- DataStructureAA.cpp - Data Structure Based Alias Analysis ----------===//
|
|
||||||
//
|
|
||||||
// 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 uses the top-down data structure graphs to implement a simple
|
|
||||||
// context sensitive alias analysis.
|
|
||||||
//
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
#include "llvm/Constants.h"
|
|
||||||
#include "llvm/DerivedTypes.h"
|
|
||||||
#include "llvm/Module.h"
|
|
||||||
#include "llvm/Analysis/AliasAnalysis.h"
|
|
||||||
#include "llvm/Analysis/Passes.h"
|
|
||||||
#include "llvm/Analysis/DataStructure/DataStructure.h"
|
|
||||||
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
|
||||||
using namespace llvm;
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
class DSAA : public ModulePass, public AliasAnalysis {
|
|
||||||
TDDataStructures *TD;
|
|
||||||
BUDataStructures *BU;
|
|
||||||
|
|
||||||
// These members are used to cache mod/ref information to make us return
|
|
||||||
// results faster, particularly for aa-eval. On the first request of
|
|
||||||
// mod/ref information for a particular call site, we compute and store the
|
|
||||||
// calculated nodemap for the call site. Any time DSA info is updated we
|
|
||||||
// free this information, and when we move onto a new call site, this
|
|
||||||
// information is also freed.
|
|
||||||
CallSite MapCS;
|
|
||||||
std::multimap<DSNode*, const DSNode*> CallerCalleeMap;
|
|
||||||
public:
|
|
||||||
DSAA() : TD(0) {}
|
|
||||||
~DSAA() {
|
|
||||||
InvalidateCache();
|
|
||||||
}
|
|
||||||
|
|
||||||
void InvalidateCache() {
|
|
||||||
MapCS = CallSite();
|
|
||||||
CallerCalleeMap.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
//------------------------------------------------
|
|
||||||
// Implement the Pass API
|
|
||||||
//
|
|
||||||
|
|
||||||
// run - Build up the result graph, representing the pointer graph for the
|
|
||||||
// program.
|
|
||||||
//
|
|
||||||
bool runOnModule(Module &M) {
|
|
||||||
InitializeAliasAnalysis(this);
|
|
||||||
TD = &getAnalysis<TDDataStructures>();
|
|
||||||
BU = &getAnalysis<BUDataStructures>();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
||||||
AliasAnalysis::getAnalysisUsage(AU);
|
|
||||||
AU.setPreservesAll(); // Does not transform code
|
|
||||||
AU.addRequiredTransitive<TDDataStructures>(); // Uses TD Datastructures
|
|
||||||
AU.addRequiredTransitive<BUDataStructures>(); // Uses BU Datastructures
|
|
||||||
}
|
|
||||||
|
|
||||||
//------------------------------------------------
|
|
||||||
// Implement the AliasAnalysis API
|
|
||||||
//
|
|
||||||
|
|
||||||
AliasResult alias(const Value *V1, unsigned V1Size,
|
|
||||||
const Value *V2, unsigned V2Size);
|
|
||||||
|
|
||||||
ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
|
|
||||||
ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
|
|
||||||
return AliasAnalysis::getModRefInfo(CS1,CS2);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void deleteValue(Value *V) {
|
|
||||||
InvalidateCache();
|
|
||||||
BU->deleteValue(V);
|
|
||||||
TD->deleteValue(V);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void copyValue(Value *From, Value *To) {
|
|
||||||
if (From == To) return;
|
|
||||||
InvalidateCache();
|
|
||||||
BU->copyValue(From, To);
|
|
||||||
TD->copyValue(From, To);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
DSGraph *getGraphForValue(const Value *V);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Register the pass...
|
|
||||||
RegisterPass<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis");
|
|
||||||
|
|
||||||
// Register as an implementation of AliasAnalysis
|
|
||||||
RegisterAnalysisGroup<AliasAnalysis> Y(X);
|
|
||||||
}
|
|
||||||
|
|
||||||
ModulePass *llvm::createDSAAPass() { return new DSAA(); }
|
|
||||||
|
|
||||||
// getGraphForValue - Return the DSGraph to use for queries about the specified
|
|
||||||
// value...
|
|
||||||
//
|
|
||||||
DSGraph *DSAA::getGraphForValue(const Value *V) {
|
|
||||||
if (const Instruction *I = dyn_cast<Instruction>(V))
|
|
||||||
return &TD->getDSGraph(*I->getParent()->getParent());
|
|
||||||
else if (const Argument *A = dyn_cast<Argument>(V))
|
|
||||||
return &TD->getDSGraph(*A->getParent());
|
|
||||||
else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
|
|
||||||
return &TD->getDSGraph(*BB->getParent());
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size,
|
|
||||||
const Value *V2, unsigned V2Size) {
|
|
||||||
if (V1 == V2) return MustAlias;
|
|
||||||
|
|
||||||
DSGraph *G1 = getGraphForValue(V1);
|
|
||||||
DSGraph *G2 = getGraphForValue(V2);
|
|
||||||
assert((!G1 || !G2 || G1 == G2) && "Alias query for 2 different functions?");
|
|
||||||
|
|
||||||
// Get the graph to use...
|
|
||||||
DSGraph &G = *(G1 ? G1 : (G2 ? G2 : &TD->getGlobalsGraph()));
|
|
||||||
|
|
||||||
const DSGraph::ScalarMapTy &GSM = G.getScalarMap();
|
|
||||||
DSGraph::ScalarMapTy::const_iterator I = GSM.find((Value*)V1);
|
|
||||||
if (I == GSM.end()) return NoAlias;
|
|
||||||
|
|
||||||
DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2);
|
|
||||||
if (J == GSM.end()) return NoAlias;
|
|
||||||
|
|
||||||
DSNode *N1 = I->second.getNode(), *N2 = J->second.getNode();
|
|
||||||
unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
|
|
||||||
if (N1 == 0 || N2 == 0)
|
|
||||||
// Can't tell whether anything aliases null.
|
|
||||||
return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
|
|
||||||
|
|
||||||
// We can only make a judgment if one of the nodes is complete.
|
|
||||||
if (N1->isComplete() || N2->isComplete()) {
|
|
||||||
if (N1 != N2)
|
|
||||||
return NoAlias; // Completely different nodes.
|
|
||||||
|
|
||||||
// See if they point to different offsets... if so, we may be able to
|
|
||||||
// determine that they do not alias...
|
|
||||||
if (O1 != O2) {
|
|
||||||
if (O2 < O1) { // Ensure that O1 <= O2
|
|
||||||
std::swap(V1, V2);
|
|
||||||
std::swap(O1, O2);
|
|
||||||
std::swap(V1Size, V2Size);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (O1+V1Size <= O2)
|
|
||||||
return NoAlias;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME: we could improve on this by checking the globals graph for aliased
|
|
||||||
// global queries...
|
|
||||||
return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// getModRefInfo - does a callsite modify or reference a value?
|
|
||||||
///
|
|
||||||
AliasAnalysis::ModRefResult
|
|
||||||
DSAA::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
|
|
||||||
DSNode *N = 0;
|
|
||||||
// First step, check our cache.
|
|
||||||
if (CS.getInstruction() == MapCS.getInstruction()) {
|
|
||||||
{
|
|
||||||
const Function *Caller = CS.getInstruction()->getParent()->getParent();
|
|
||||||
DSGraph &CallerTDGraph = TD->getDSGraph(*Caller);
|
|
||||||
|
|
||||||
// Figure out which node in the TD graph this pointer corresponds to.
|
|
||||||
DSScalarMap &CallerSM = CallerTDGraph.getScalarMap();
|
|
||||||
DSScalarMap::iterator NI = CallerSM.find(P);
|
|
||||||
if (NI == CallerSM.end()) {
|
|
||||||
InvalidateCache();
|
|
||||||
return DSAA::getModRefInfo(CS, P, Size);
|
|
||||||
}
|
|
||||||
N = NI->second.getNode();
|
|
||||||
}
|
|
||||||
|
|
||||||
HaveMappingInfo:
|
|
||||||
assert(N && "Null pointer in scalar map??");
|
|
||||||
|
|
||||||
typedef std::multimap<DSNode*, const DSNode*>::iterator NodeMapIt;
|
|
||||||
std::pair<NodeMapIt, NodeMapIt> Range = CallerCalleeMap.equal_range(N);
|
|
||||||
|
|
||||||
// Loop over all of the nodes in the callee that correspond to "N", keeping
|
|
||||||
// track of aggregate mod/ref info.
|
|
||||||
bool NeverReads = true, NeverWrites = true;
|
|
||||||
for (; Range.first != Range.second; ++Range.first) {
|
|
||||||
if (Range.first->second->isModified())
|
|
||||||
NeverWrites = false;
|
|
||||||
if (Range.first->second->isRead())
|
|
||||||
NeverReads = false;
|
|
||||||
if (NeverReads == false && NeverWrites == false)
|
|
||||||
return AliasAnalysis::getModRefInfo(CS, P, Size);
|
|
||||||
}
|
|
||||||
|
|
||||||
ModRefResult Result = ModRef;
|
|
||||||
if (NeverWrites) // We proved it was not modified.
|
|
||||||
Result = ModRefResult(Result & ~Mod);
|
|
||||||
if (NeverReads) // We proved it was not read.
|
|
||||||
Result = ModRefResult(Result & ~Ref);
|
|
||||||
|
|
||||||
return ModRefResult(Result & AliasAnalysis::getModRefInfo(CS, P, Size));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Any cached info we have is for the wrong function.
|
|
||||||
InvalidateCache();
|
|
||||||
|
|
||||||
Function *F = CS.getCalledFunction();
|
|
||||||
|
|
||||||
if (!F) return AliasAnalysis::getModRefInfo(CS, P, Size);
|
|
||||||
|
|
||||||
if (F->isExternal()) {
|
|
||||||
// If we are calling an external function, and if this global doesn't escape
|
|
||||||
// the portion of the program we have analyzed, we can draw conclusions
|
|
||||||
// based on whether the global escapes the program.
|
|
||||||
Function *Caller = CS.getInstruction()->getParent()->getParent();
|
|
||||||
DSGraph *G = &TD->getDSGraph(*Caller);
|
|
||||||
DSScalarMap::iterator NI = G->getScalarMap().find(P);
|
|
||||||
if (NI == G->getScalarMap().end()) {
|
|
||||||
// If it wasn't in the local function graph, check the global graph. This
|
|
||||||
// can occur for globals who are locally reference but hoisted out to the
|
|
||||||
// globals graph despite that.
|
|
||||||
G = G->getGlobalsGraph();
|
|
||||||
NI = G->getScalarMap().find(P);
|
|
||||||
if (NI == G->getScalarMap().end())
|
|
||||||
return AliasAnalysis::getModRefInfo(CS, P, Size);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we found a node and it's complete, it cannot be passed out to the
|
|
||||||
// called function.
|
|
||||||
if (NI->second.getNode()->isComplete())
|
|
||||||
return NoModRef;
|
|
||||||
return AliasAnalysis::getModRefInfo(CS, P, Size);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the graphs for the callee and caller. Note that we want the BU graph
|
|
||||||
// for the callee because we don't want all caller's effects incorporated!
|
|
||||||
const Function *Caller = CS.getInstruction()->getParent()->getParent();
|
|
||||||
DSGraph &CallerTDGraph = TD->getDSGraph(*Caller);
|
|
||||||
DSGraph &CalleeBUGraph = BU->getDSGraph(*F);
|
|
||||||
|
|
||||||
// Figure out which node in the TD graph this pointer corresponds to.
|
|
||||||
DSScalarMap &CallerSM = CallerTDGraph.getScalarMap();
|
|
||||||
DSScalarMap::iterator NI = CallerSM.find(P);
|
|
||||||
if (NI == CallerSM.end()) {
|
|
||||||
ModRefResult Result = ModRef;
|
|
||||||
if (isa<ConstantPointerNull>(P) || isa<UndefValue>(P))
|
|
||||||
return NoModRef; // null is never modified :)
|
|
||||||
else {
|
|
||||||
assert(isa<GlobalVariable>(P) &&
|
|
||||||
cast<GlobalVariable>(P)->getType()->getElementType()->isFirstClassType() &&
|
|
||||||
"This isn't a global that DSA inconsiderately dropped "
|
|
||||||
"from the graph?");
|
|
||||||
|
|
||||||
DSGraph &GG = *CallerTDGraph.getGlobalsGraph();
|
|
||||||
DSScalarMap::iterator NI = GG.getScalarMap().find(P);
|
|
||||||
if (NI != GG.getScalarMap().end() && !NI->second.isNull()) {
|
|
||||||
// Otherwise, if the node is only M or R, return this. This can be
|
|
||||||
// useful for globals that should be marked const but are not.
|
|
||||||
DSNode *N = NI->second.getNode();
|
|
||||||
if (!N->isModified())
|
|
||||||
Result = (ModRefResult)(Result & ~Mod);
|
|
||||||
if (!N->isRead())
|
|
||||||
Result = (ModRefResult)(Result & ~Ref);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Result == NoModRef) return Result;
|
|
||||||
return ModRefResult(Result & AliasAnalysis::getModRefInfo(CS, P, Size));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compute the mapping from nodes in the callee graph to the nodes in the
|
|
||||||
// caller graph for this call site.
|
|
||||||
DSGraph::NodeMapTy CalleeCallerMap;
|
|
||||||
DSCallSite DSCS = CallerTDGraph.getDSCallSiteForCallSite(CS);
|
|
||||||
CallerTDGraph.computeCalleeCallerMapping(DSCS, *F, CalleeBUGraph,
|
|
||||||
CalleeCallerMap);
|
|
||||||
|
|
||||||
// Remember the mapping and the call site for future queries.
|
|
||||||
MapCS = CS;
|
|
||||||
|
|
||||||
// Invert the mapping into CalleeCallerInvMap.
|
|
||||||
for (DSGraph::NodeMapTy::iterator I = CalleeCallerMap.begin(),
|
|
||||||
E = CalleeCallerMap.end(); I != E; ++I)
|
|
||||||
CallerCalleeMap.insert(std::make_pair(I->second.getNode(), I->first));
|
|
||||||
|
|
||||||
N = NI->second.getNode();
|
|
||||||
goto HaveMappingInfo;
|
|
||||||
}
|
|
@@ -1,102 +0,0 @@
|
|||||||
//===- DataStructureOpt.cpp - Data Structure Analysis Based Optimizations -===//
|
|
||||||
//
|
|
||||||
// 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 uses DSA to a series of simple optimizations, like marking
|
|
||||||
// unwritten global variables 'constant'.
|
|
||||||
//
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
#include "llvm/Analysis/DataStructure/DataStructure.h"
|
|
||||||
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
|
||||||
#include "llvm/Analysis/Passes.h"
|
|
||||||
#include "llvm/Module.h"
|
|
||||||
#include "llvm/Constant.h"
|
|
||||||
#include "llvm/Type.h"
|
|
||||||
#include "llvm/ADT/Statistic.h"
|
|
||||||
using namespace llvm;
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
Statistic
|
|
||||||
NumGlobalsConstanted("ds-opt", "Number of globals marked constant");
|
|
||||||
Statistic
|
|
||||||
NumGlobalsIsolated("ds-opt", "Number of globals with references dropped");
|
|
||||||
|
|
||||||
class DSOpt : public ModulePass {
|
|
||||||
TDDataStructures *TD;
|
|
||||||
public:
|
|
||||||
bool runOnModule(Module &M) {
|
|
||||||
TD = &getAnalysis<TDDataStructures>();
|
|
||||||
bool Changed = OptimizeGlobals(M);
|
|
||||||
return Changed;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
||||||
AU.addRequired<TDDataStructures>(); // Uses TD Datastructures
|
|
||||||
AU.addPreserved<LocalDataStructures>(); // Preserves local...
|
|
||||||
AU.addPreserved<TDDataStructures>(); // Preserves bu...
|
|
||||||
AU.addPreserved<BUDataStructures>(); // Preserves td...
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
bool OptimizeGlobals(Module &M);
|
|
||||||
};
|
|
||||||
|
|
||||||
RegisterPass<DSOpt> X("ds-opt", "DSA-based simple optimizations");
|
|
||||||
}
|
|
||||||
|
|
||||||
ModulePass *llvm::createDSOptPass() { return new DSOpt(); }
|
|
||||||
|
|
||||||
/// OptimizeGlobals - This method uses information taken from DSA to optimize
|
|
||||||
/// global variables.
|
|
||||||
///
|
|
||||||
bool DSOpt::OptimizeGlobals(Module &M) {
|
|
||||||
DSGraph &GG = TD->getGlobalsGraph();
|
|
||||||
const DSGraph::ScalarMapTy &SM = GG.getScalarMap();
|
|
||||||
bool Changed = false;
|
|
||||||
|
|
||||||
for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
|
|
||||||
if (!I->isExternal()) { // Loop over all of the non-external globals...
|
|
||||||
// Look up the node corresponding to this global, if it exists.
|
|
||||||
DSNode *GNode = 0;
|
|
||||||
DSGraph::ScalarMapTy::const_iterator SMI = SM.find(I);
|
|
||||||
if (SMI != SM.end()) GNode = SMI->second.getNode();
|
|
||||||
|
|
||||||
if (GNode == 0 && I->hasInternalLinkage()) {
|
|
||||||
// If there is no entry in the scalar map for this global, it was never
|
|
||||||
// referenced in the program. If it has internal linkage, that means we
|
|
||||||
// can delete it. We don't ACTUALLY want to delete the global, just
|
|
||||||
// remove anything that references the global: later passes will take
|
|
||||||
// care of nuking it.
|
|
||||||
if (!I->use_empty()) {
|
|
||||||
I->replaceAllUsesWith(Constant::getNullValue((Type*)I->getType()));
|
|
||||||
++NumGlobalsIsolated;
|
|
||||||
}
|
|
||||||
} else if (GNode && GNode->isComplete()) {
|
|
||||||
|
|
||||||
// If the node has not been read or written, and it is not externally
|
|
||||||
// visible, kill any references to it so it can be DCE'd.
|
|
||||||
if (!GNode->isModified() && !GNode->isRead() &&I->hasInternalLinkage()){
|
|
||||||
if (!I->use_empty()) {
|
|
||||||
I->replaceAllUsesWith(Constant::getNullValue((Type*)I->getType()));
|
|
||||||
++NumGlobalsIsolated;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// We expect that there will almost always be a node for this global.
|
|
||||||
// If there is, and the node doesn't have the M bit set, we can set the
|
|
||||||
// 'constant' bit on the global.
|
|
||||||
if (!GNode->isModified() && !I->isConstant()) {
|
|
||||||
I->setConstant(true);
|
|
||||||
++NumGlobalsConstanted;
|
|
||||||
Changed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Changed;
|
|
||||||
}
|
|
@@ -1,150 +0,0 @@
|
|||||||
//===- DataStructureStats.cpp - Various statistics for DS Graphs ----------===//
|
|
||||||
//
|
|
||||||
// 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 defines a little pass that prints out statistics for DS Graphs.
|
|
||||||
//
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
#include "llvm/Analysis/DataStructure/DataStructure.h"
|
|
||||||
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
|
||||||
#include "llvm/Function.h"
|
|
||||||
#include "llvm/Instructions.h"
|
|
||||||
#include "llvm/Pass.h"
|
|
||||||
#include "llvm/Support/InstVisitor.h"
|
|
||||||
#include "llvm/Support/Streams.h"
|
|
||||||
#include "llvm/ADT/Statistic.h"
|
|
||||||
#include <ostream>
|
|
||||||
using namespace llvm;
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
Statistic TotalNumCallees("totalcallees",
|
|
||||||
"Total number of callee functions at all indirect call sites");
|
|
||||||
Statistic NumIndirectCalls("numindirect",
|
|
||||||
"Total number of indirect call sites in the program");
|
|
||||||
Statistic NumPoolNodes("numpools",
|
|
||||||
"Number of allocation nodes that could be pool allocated");
|
|
||||||
|
|
||||||
// Typed/Untyped memory accesses: If DSA can infer that the types the loads
|
|
||||||
// and stores are accessing are correct (ie, the node has not been collapsed),
|
|
||||||
// increment the appropriate counter.
|
|
||||||
Statistic NumTypedMemAccesses("numtypedmemaccesses",
|
|
||||||
"Number of loads/stores which are fully typed");
|
|
||||||
Statistic NumUntypedMemAccesses("numuntypedmemaccesses",
|
|
||||||
"Number of loads/stores which are untyped");
|
|
||||||
|
|
||||||
class DSGraphStats : public FunctionPass, public InstVisitor<DSGraphStats> {
|
|
||||||
void countCallees(const Function &F);
|
|
||||||
const DSGraph *TDGraph;
|
|
||||||
|
|
||||||
DSNode *getNodeForValue(Value *V);
|
|
||||||
bool isNodeForValueCollapsed(Value *V);
|
|
||||||
public:
|
|
||||||
/// Driver functions to compute the Load/Store Dep. Graph per function.
|
|
||||||
bool runOnFunction(Function& F);
|
|
||||||
|
|
||||||
/// getAnalysisUsage - This modify nothing, and uses the Top-Down Graph.
|
|
||||||
void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
||||||
AU.setPreservesAll();
|
|
||||||
AU.addRequired<TDDataStructures>();
|
|
||||||
}
|
|
||||||
|
|
||||||
void visitLoad(LoadInst &LI);
|
|
||||||
void visitStore(StoreInst &SI);
|
|
||||||
|
|
||||||
/// Debugging support methods
|
|
||||||
void print(std::ostream &O, const Module* = 0) const { }
|
|
||||||
};
|
|
||||||
|
|
||||||
static RegisterPass<DSGraphStats> Z("dsstats", "DS Graph Statistics");
|
|
||||||
}
|
|
||||||
|
|
||||||
FunctionPass *llvm::createDataStructureStatsPass() {
|
|
||||||
return new DSGraphStats();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static bool isIndirectCallee(Value *V) {
|
|
||||||
if (isa<Function>(V)) return false;
|
|
||||||
|
|
||||||
if (CastInst *CI = dyn_cast<CastInst>(V))
|
|
||||||
return isIndirectCallee(CI->getOperand(0));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void DSGraphStats::countCallees(const Function& F) {
|
|
||||||
unsigned numIndirectCalls = 0, totalNumCallees = 0;
|
|
||||||
|
|
||||||
for (DSGraph::fc_iterator I = TDGraph->fc_begin(), E = TDGraph->fc_end();
|
|
||||||
I != E; ++I)
|
|
||||||
if (isIndirectCallee(I->getCallSite().getCalledValue())) {
|
|
||||||
// This is an indirect function call
|
|
||||||
std::vector<Function*> Callees;
|
|
||||||
I->getCalleeNode()->addFullFunctionList(Callees);
|
|
||||||
|
|
||||||
if (Callees.size() > 0) {
|
|
||||||
totalNumCallees += Callees.size();
|
|
||||||
++numIndirectCalls;
|
|
||||||
} else
|
|
||||||
cerr << "WARNING: No callee in Function '" << F.getName()
|
|
||||||
<< "' at call: \n"
|
|
||||||
<< *I->getCallSite().getInstruction();
|
|
||||||
}
|
|
||||||
|
|
||||||
TotalNumCallees += totalNumCallees;
|
|
||||||
NumIndirectCalls += numIndirectCalls;
|
|
||||||
|
|
||||||
if (numIndirectCalls)
|
|
||||||
cout << " In function " << F.getName() << ": "
|
|
||||||
<< (totalNumCallees / (double) numIndirectCalls)
|
|
||||||
<< " average callees per indirect call\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
DSNode *DSGraphStats::getNodeForValue(Value *V) {
|
|
||||||
const DSGraph *G = TDGraph;
|
|
||||||
if (isa<Constant>(V))
|
|
||||||
G = TDGraph->getGlobalsGraph();
|
|
||||||
|
|
||||||
const DSGraph::ScalarMapTy &ScalarMap = G->getScalarMap();
|
|
||||||
DSGraph::ScalarMapTy::const_iterator I = ScalarMap.find(V);
|
|
||||||
if (I != ScalarMap.end())
|
|
||||||
return I->second.getNode();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool DSGraphStats::isNodeForValueCollapsed(Value *V) {
|
|
||||||
if (DSNode *N = getNodeForValue(V))
|
|
||||||
return N->isNodeCompletelyFolded() || N->isIncomplete();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void DSGraphStats::visitLoad(LoadInst &LI) {
|
|
||||||
if (isNodeForValueCollapsed(LI.getOperand(0))) {
|
|
||||||
NumUntypedMemAccesses++;
|
|
||||||
} else {
|
|
||||||
NumTypedMemAccesses++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void DSGraphStats::visitStore(StoreInst &SI) {
|
|
||||||
if (isNodeForValueCollapsed(SI.getOperand(1))) {
|
|
||||||
NumUntypedMemAccesses++;
|
|
||||||
} else {
|
|
||||||
NumTypedMemAccesses++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool DSGraphStats::runOnFunction(Function& F) {
|
|
||||||
TDGraph = &getAnalysis<TDDataStructures>().getDSGraph(F);
|
|
||||||
countCallees(F);
|
|
||||||
visit(F);
|
|
||||||
return true;
|
|
||||||
}
|
|
@@ -1,477 +0,0 @@
|
|||||||
//===- 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 "llvm/Analysis/DataStructure/DataStructure.h"
|
|
||||||
#include "llvm/DerivedTypes.h"
|
|
||||||
#include "llvm/Module.h"
|
|
||||||
#include "llvm/Pass.h"
|
|
||||||
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
|
||||||
#include "llvm/Support/CallSite.h"
|
|
||||||
#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"
|
|
||||||
using namespace llvm;
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
RegisterPass<EquivClassGraphs> X("eqdatastructure",
|
|
||||||
"Equivalence-class Bottom-up Data Structure Analysis");
|
|
||||||
Statistic NumEquivBUInlines("equivdatastructures",
|
|
||||||
"Number of graphs inlined");
|
|
||||||
Statistic NumFoldGraphInlines("Inline equiv-class graphs bottom up",
|
|
||||||
"Number of graphs inlined");
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifndef NDEBUG
|
|
||||||
template<typename GT>
|
|
||||||
static void CheckAllGraphs(Module *M, GT &ECGraphs) {
|
|
||||||
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
||||||
if (!I->isExternal()) {
|
|
||||||
DSGraph &G = ECGraphs.getDSGraph(*I);
|
|
||||||
if (G.retnodes_begin()->first != I)
|
|
||||||
continue; // Only check a graph once.
|
|
||||||
|
|
||||||
DSGraph::NodeMapTy GlobalsGraphNodeMapping;
|
|
||||||
G.computeGToGGMapping(GlobalsGraphNodeMapping);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// getSomeCalleeForCallSite - Return any one callee function at a call site.
|
|
||||||
//
|
|
||||||
Function *EquivClassGraphs::getSomeCalleeForCallSite(const CallSite &CS) const{
|
|
||||||
Function *thisFunc = CS.getCaller();
|
|
||||||
assert(thisFunc && "getSomeCalleeForCallSite(): Not a valid call site?");
|
|
||||||
DSGraph &DSG = getDSGraph(*thisFunc);
|
|
||||||
DSNode *calleeNode = DSG.getNodeForValue(CS.getCalledValue()).getNode();
|
|
||||||
std::map<DSNode*, Function *>::const_iterator I =
|
|
||||||
OneCalledFunction.find(calleeNode);
|
|
||||||
return (I == OneCalledFunction.end())? NULL : I->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
// runOnModule - Calculate the bottom up data structure graphs for each function
|
|
||||||
// in the program.
|
|
||||||
//
|
|
||||||
bool EquivClassGraphs::runOnModule(Module &M) {
|
|
||||||
CBU = &getAnalysis<CompleteBUDataStructures>();
|
|
||||||
GlobalECs = CBU->getGlobalECs();
|
|
||||||
DEBUG(CheckAllGraphs(&M, *CBU));
|
|
||||||
|
|
||||||
GlobalsGraph = new DSGraph(CBU->getGlobalsGraph(), GlobalECs);
|
|
||||||
GlobalsGraph->setPrintAuxCalls();
|
|
||||||
|
|
||||||
ActualCallees = CBU->getActualCallees();
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
std::vector<DSGraph*> Stack;
|
|
||||||
std::map<DSGraph*, unsigned> ValMap;
|
|
||||||
unsigned NextID = 1;
|
|
||||||
|
|
||||||
Function *MainFunc = M.getMainFunction();
|
|
||||||
if (MainFunc && !MainFunc->isExternal()) {
|
|
||||||
processSCC(getOrCreateGraph(*MainFunc), Stack, NextID, ValMap);
|
|
||||||
} else {
|
|
||||||
cerr << "Fold Graphs: No 'main' function found!\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
|
||||||
if (!I->isExternal())
|
|
||||||
processSCC(getOrCreateGraph(*I), Stack, NextID, ValMap);
|
|
||||||
|
|
||||||
DEBUG(CheckAllGraphs(&M, *this));
|
|
||||||
|
|
||||||
getGlobalsGraph().removeTriviallyDeadNodes();
|
|
||||||
getGlobalsGraph().markIncompleteNodes(DSGraph::IgnoreGlobals);
|
|
||||||
|
|
||||||
// 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,
|
|
||||||
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));
|
|
||||||
|
|
||||||
MainGraph.maskIncompleteMarkers();
|
|
||||||
MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
|
|
||||||
DSGraph::IgnoreGlobals);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
//
|
|
||||||
void EquivClassGraphs::buildIndirectFunctionSets(Module &M) {
|
|
||||||
const ActualCalleesTy& AC = CBU->getActualCallees();
|
|
||||||
|
|
||||||
// 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.insert(I->second); // -- Make sure function has equiv class
|
|
||||||
FirstFunc = I->second; // -- First callee at this site
|
|
||||||
} else { // Else indirect call
|
|
||||||
// DOUT << "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();
|
|
||||||
DSGraph &TFG = CBU->getDSGraph(*thisFunc);
|
|
||||||
DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
|
|
||||||
OneCalledFunction[calleeNode] = FirstFunc;
|
|
||||||
FuncECs.insert(I->second);
|
|
||||||
} else {
|
|
||||||
// This is not the first possible callee from a particular call site.
|
|
||||||
// Union the callee in with the other functions.
|
|
||||||
FuncECs.unionSets(FirstFunc, I->second);
|
|
||||||
#ifndef NDEBUG
|
|
||||||
Function *thisFunc = LastInst->getParent()->getParent();
|
|
||||||
DSGraph &TFG = CBU->getDSGraph(*thisFunc);
|
|
||||||
DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
|
|
||||||
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);
|
|
||||||
for (DSGraph::retnodes_iterator RI = funcDSGraph.retnodes_begin(),
|
|
||||||
RE = funcDSGraph.retnodes_end(); RI != RE; ++RI)
|
|
||||||
FuncECs.unionSets(FirstFunc, RI->first);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now that all of the equivalences have been built, merge the graphs for
|
|
||||||
// each equivalence class.
|
|
||||||
//
|
|
||||||
DOUT << "\nIndirect Function Equivalence Sets:\n";
|
|
||||||
for (EquivalenceClasses<Function*>::iterator EQSI = FuncECs.begin(), E =
|
|
||||||
FuncECs.end(); EQSI != E; ++EQSI) {
|
|
||||||
if (!EQSI->isLeader()) continue;
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
#ifndef NDEBUG
|
|
||||||
DOUT <<" Equivalence set for leader " << LF->getName() <<" = ";
|
|
||||||
for (SN = SI; SN != FuncECs.member_end(); ++SN)
|
|
||||||
DOUT << " " << (*SN)->getName() << "," ;
|
|
||||||
DOUT << "\n";
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
// Record the argument nodes for use in merging later below.
|
|
||||||
std::vector<DSNodeHandle> ArgNodes;
|
|
||||||
|
|
||||||
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));
|
|
||||||
|
|
||||||
// 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));
|
|
||||||
|
|
||||||
for (++SI; SI != FuncECs.member_end(); ++SI) {
|
|
||||||
Function *F = *SI;
|
|
||||||
DSGraph &CBUGraph = CBU->getDSGraph(*F);
|
|
||||||
if (GraphsMerged.insert(&CBUGraph).second) {
|
|
||||||
// Record the "folded" graph for the function.
|
|
||||||
for (DSGraph::retnodes_iterator I = CBUGraph.retnodes_begin(),
|
|
||||||
E = CBUGraph.retnodes_end(); I != E; ++I) {
|
|
||||||
assert(DSInfo[I->first] == 0 && "Graph already exists for Fn!");
|
|
||||||
DSInfo[I->first] = &MergedG;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clone this member of the equivalence class into MergedG.
|
|
||||||
MergedG.cloneInto(CBUGraph);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Merge the return nodes of all functions together.
|
|
||||||
MergedG.getReturnNodes()[LF].mergeWith(MergedG.getReturnNodes()[F]);
|
|
||||||
|
|
||||||
// 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));
|
|
||||||
|
|
||||||
for ( ; AI2 != AI2end; ++AI2)
|
|
||||||
if (DS::isPointerType(AI2->getType()))
|
|
||||||
ArgNodes.push_back(MergedG.getNodeForValue(AI2));
|
|
||||||
DEBUG(MergedG.AssertGraphOK());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
DOUT << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
DSGraph &EquivClassGraphs::getOrCreateGraph(Function &F) {
|
|
||||||
// Has the graph already been created?
|
|
||||||
DSGraph *&Graph = DSInfo[&F];
|
|
||||||
if (Graph) return *Graph;
|
|
||||||
|
|
||||||
DSGraph &CBUGraph = CBU->getDSGraph(F);
|
|
||||||
|
|
||||||
// Copy the CBU graph...
|
|
||||||
Graph = new DSGraph(CBUGraph, GlobalECs); // updates the map via reference
|
|
||||||
Graph->setGlobalsGraph(&getGlobalsGraph());
|
|
||||||
Graph->setPrintAuxCalls();
|
|
||||||
|
|
||||||
// Make sure to update the DSInfo map for all functions in the graph!
|
|
||||||
for (DSGraph::retnodes_iterator I = Graph->retnodes_begin();
|
|
||||||
I != Graph->retnodes_end(); ++I)
|
|
||||||
if (I->first != &F) {
|
|
||||||
DSGraph *&FG = DSInfo[I->first];
|
|
||||||
assert(FG == 0 && "Merging function in SCC twice?");
|
|
||||||
FG = Graph;
|
|
||||||
}
|
|
||||||
|
|
||||||
return *Graph;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
unsigned 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)
|
|
||||||
return It->second;
|
|
||||||
|
|
||||||
DOUT << " ProcessSCC for function " << FG.getFunctionNames() << "\n";
|
|
||||||
|
|
||||||
unsigned Min = NextID++, MyID = Min;
|
|
||||||
ValMap[&FG] = Min;
|
|
||||||
Stack.push_back(&FG);
|
|
||||||
|
|
||||||
// The edges out of the current node are the call site targets...
|
|
||||||
for (DSGraph::fc_iterator CI = FG.fc_begin(), CE = FG.fc_end();
|
|
||||||
CI != CE; ++CI) {
|
|
||||||
Instruction *Call = CI->getCallSite().getInstruction();
|
|
||||||
|
|
||||||
// Loop over all of the actually called functions...
|
|
||||||
for (callee_iterator I = callee_begin(Call), E = callee_end(Call);
|
|
||||||
I != E; ++I)
|
|
||||||
if (!I->second->isExternal()) {
|
|
||||||
// Process the callee as necessary.
|
|
||||||
unsigned M = processSCC(getOrCreateGraph(*I->second),
|
|
||||||
Stack, NextID, ValMap);
|
|
||||||
if (M < Min) Min = M;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(ValMap[&FG] == 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.
|
|
||||||
bool MergedGraphs = false;
|
|
||||||
while (Stack.back() != &FG) {
|
|
||||||
DSGraph *NG = Stack.back();
|
|
||||||
ValMap[NG] = ~0U;
|
|
||||||
|
|
||||||
// If the SCC found is not the same as those found in CBU, make sure to
|
|
||||||
// merge the graphs as appropriate.
|
|
||||||
FG.cloneInto(*NG);
|
|
||||||
|
|
||||||
// Update the DSInfo map and delete the old graph...
|
|
||||||
for (DSGraph::retnodes_iterator I = NG->retnodes_begin();
|
|
||||||
I != NG->retnodes_end(); ++I)
|
|
||||||
DSInfo[I->first] = &FG;
|
|
||||||
|
|
||||||
// Remove NG from the ValMap since the pointer may get recycled.
|
|
||||||
ValMap.erase(NG);
|
|
||||||
delete NG;
|
|
||||||
MergedGraphs = true;
|
|
||||||
Stack.pop_back();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clean up the graph before we start inlining a bunch again.
|
|
||||||
if (MergedGraphs)
|
|
||||||
FG.removeTriviallyDeadNodes();
|
|
||||||
|
|
||||||
Stack.pop_back();
|
|
||||||
|
|
||||||
processGraph(FG);
|
|
||||||
ValMap[&FG] = ~0U;
|
|
||||||
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.
|
|
||||||
void EquivClassGraphs::processGraph(DSGraph &G) {
|
|
||||||
DOUT << " ProcessGraph for function " << G.getFunctionNames() << "\n";
|
|
||||||
|
|
||||||
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...
|
|
||||||
unsigned i = 0;
|
|
||||||
for (DSGraph::fc_iterator CI = G.fc_begin(), CE = G.fc_end(); CI != CE;
|
|
||||||
++CI, ++i) {
|
|
||||||
const DSCallSite &CS = *CI;
|
|
||||||
Instruction *TheCall = CS.getCallSite().getInstruction();
|
|
||||||
|
|
||||||
assert(calls.insert(TheCall).second &&
|
|
||||||
"Call instruction occurs multiple times in graph??");
|
|
||||||
|
|
||||||
if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
callee_iterator I = callee_begin(TheCall), E = callee_end(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.
|
|
||||||
if (I != E) {
|
|
||||||
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);
|
|
||||||
if (CalleeGraph != &G) {
|
|
||||||
++NumFoldGraphInlines;
|
|
||||||
G.mergeInGraph(CS, *CalleeFunc, *CalleeGraph,
|
|
||||||
DSGraph::StripAllocaBit |
|
|
||||||
DSGraph::DontCloneCallNodes |
|
|
||||||
DSGraph::DontCloneAuxCallNodes);
|
|
||||||
DOUT << " Inlining graph [" << i << "/"
|
|
||||||
<< G.getFunctionCalls().size()-1
|
|
||||||
<< ":" << TNum << "/" << Num-1 << "] for "
|
|
||||||
<< CalleeFunc->getName() << "["
|
|
||||||
<< CalleeGraph->getGraphSize() << "+"
|
|
||||||
<< CalleeGraph->getAuxFunctionCalls().size()
|
|
||||||
<< "] into '" /*<< G.getFunctionNames()*/ << "' ["
|
|
||||||
<< G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
|
|
||||||
<< "]\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
|
|
||||||
}
|
|
||||||
|
|
||||||
// Recompute the Incomplete markers.
|
|
||||||
G.maskIncompleteMarkers();
|
|
||||||
G.markIncompleteNodes(DSGraph::MarkFormalArgs);
|
|
||||||
|
|
||||||
// Delete dead nodes. Treat globals that are unreachable but that can
|
|
||||||
// reach live nodes as live.
|
|
||||||
G.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
|
|
||||||
|
|
||||||
// When this graph is finalized, clone the globals in the graph into the
|
|
||||||
// globals graph to make sure it has everything, from all graphs.
|
|
||||||
ReachabilityCloner RC(*G.getGlobalsGraph(), G, DSGraph::StripAllocaBit);
|
|
||||||
|
|
||||||
// Clone everything reachable from globals in the function graph into the
|
|
||||||
// globals graph.
|
|
||||||
DSScalarMap &MainSM = G.getScalarMap();
|
|
||||||
for (DSScalarMap::global_iterator I = MainSM.global_begin(),
|
|
||||||
E = MainSM.global_end(); I != E; ++I)
|
|
||||||
RC.getClonedNH(MainSM[*I]);
|
|
||||||
|
|
||||||
DOUT << " -- DONE ProcessGraph for function " << G.getFunctionNames() <<"\n";
|
|
||||||
}
|
|
@@ -1,204 +0,0 @@
|
|||||||
//===- GraphChecker.cpp - Assert that various graph properties hold -------===//
|
|
||||||
//
|
|
||||||
// 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 used to test DSA with regression tests. It can be used to check
|
|
||||||
// that certain graph properties hold, such as two nodes being disjoint, whether
|
|
||||||
// or not a node is collapsed, etc. These are the command line arguments that
|
|
||||||
// it supports:
|
|
||||||
//
|
|
||||||
// --dsgc-dspass={local,bu,td} - Specify what flavor of graph to check
|
|
||||||
// --dsgc-abort-if-any-collapsed - Abort if any collapsed nodes are found
|
|
||||||
// --dsgc-abort-if-collapsed=<list> - Abort if a node pointed to by an SSA
|
|
||||||
// value with name in <list> is collapsed
|
|
||||||
// --dsgc-check-flags=<list> - Abort if the specified nodes have flags
|
|
||||||
// that are not specified.
|
|
||||||
// --dsgc-abort-if-merged=<list> - Abort if any of the named SSA values
|
|
||||||
// point to the same node.
|
|
||||||
//
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
#include "llvm/Analysis/DataStructure/DataStructure.h"
|
|
||||||
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
|
||||||
#include "llvm/Support/CommandLine.h"
|
|
||||||
#include "llvm/Support/Streams.h"
|
|
||||||
#include "llvm/Value.h"
|
|
||||||
#include <set>
|
|
||||||
using namespace llvm;
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
enum DSPass { local, bu, td };
|
|
||||||
cl::opt<DSPass>
|
|
||||||
DSPass("dsgc-dspass", cl::Hidden,
|
|
||||||
cl::desc("Specify which DSA pass the -datastructure-gc pass should use"),
|
|
||||||
cl::values(clEnumVal(local, "Local pass"),
|
|
||||||
clEnumVal(bu, "Bottom-up pass"),
|
|
||||||
clEnumVal(td, "Top-down pass"),
|
|
||||||
clEnumValEnd), cl::init(local));
|
|
||||||
|
|
||||||
cl::opt<bool>
|
|
||||||
AbortIfAnyCollapsed("dsgc-abort-if-any-collapsed", cl::Hidden,
|
|
||||||
cl::desc("Abort if any collapsed nodes are found"));
|
|
||||||
cl::list<std::string>
|
|
||||||
AbortIfCollapsed("dsgc-abort-if-collapsed", cl::Hidden, cl::CommaSeparated,
|
|
||||||
cl::desc("Abort if any of the named symbols is collapsed"));
|
|
||||||
cl::list<std::string>
|
|
||||||
CheckFlags("dsgc-check-flags", cl::Hidden, cl::CommaSeparated,
|
|
||||||
cl::desc("Check that flags are specified for nodes"));
|
|
||||||
cl::list<std::string>
|
|
||||||
AbortIfMerged("dsgc-abort-if-merged", cl::Hidden, cl::CommaSeparated,
|
|
||||||
cl::desc("Abort if any of the named symbols are merged together"));
|
|
||||||
|
|
||||||
struct DSGC : public FunctionPass {
|
|
||||||
DSGC();
|
|
||||||
bool doFinalization(Module &M);
|
|
||||||
bool runOnFunction(Function &F);
|
|
||||||
|
|
||||||
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
||||||
switch (DSPass) {
|
|
||||||
case local: AU.addRequired<LocalDataStructures>(); break;
|
|
||||||
case bu: AU.addRequired<BUDataStructures>(); break;
|
|
||||||
case td: AU.addRequired<TDDataStructures>(); break;
|
|
||||||
}
|
|
||||||
AU.setPreservesAll();
|
|
||||||
}
|
|
||||||
void print(std::ostream &O, const Module *M) const {}
|
|
||||||
|
|
||||||
private:
|
|
||||||
void verify(const DSGraph &G);
|
|
||||||
};
|
|
||||||
|
|
||||||
RegisterPass<DSGC> X("datastructure-gc", "DSA Graph Checking Pass");
|
|
||||||
}
|
|
||||||
|
|
||||||
FunctionPass *llvm::createDataStructureGraphCheckerPass() {
|
|
||||||
return new DSGC();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
DSGC::DSGC() {
|
|
||||||
if (!AbortIfAnyCollapsed && AbortIfCollapsed.empty() &&
|
|
||||||
CheckFlags.empty() && AbortIfMerged.empty()) {
|
|
||||||
cerr << "The -datastructure-gc is useless if you don't specify any"
|
|
||||||
<< " -dsgc-* options. See the -help-hidden output for a list.\n";
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// doFinalization - Verify that the globals graph is in good shape...
|
|
||||||
///
|
|
||||||
bool DSGC::doFinalization(Module &M) {
|
|
||||||
switch (DSPass) {
|
|
||||||
case local:verify(getAnalysis<LocalDataStructures>().getGlobalsGraph());break;
|
|
||||||
case bu: verify(getAnalysis<BUDataStructures>().getGlobalsGraph()); break;
|
|
||||||
case td: verify(getAnalysis<TDDataStructures>().getGlobalsGraph()); break;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// runOnFunction - Get the DSGraph for this function and verify that it is ok.
|
|
||||||
///
|
|
||||||
bool DSGC::runOnFunction(Function &F) {
|
|
||||||
switch (DSPass) {
|
|
||||||
case local: verify(getAnalysis<LocalDataStructures>().getDSGraph(F)); break;
|
|
||||||
case bu: verify(getAnalysis<BUDataStructures>().getDSGraph(F)); break;
|
|
||||||
case td: verify(getAnalysis<TDDataStructures>().getDSGraph(F)); break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// verify - This is the function which checks to make sure that all of the
|
|
||||||
/// invariants established on the command line are true.
|
|
||||||
///
|
|
||||||
void DSGC::verify(const DSGraph &G) {
|
|
||||||
// Loop over all of the nodes, checking to see if any are collapsed...
|
|
||||||
if (AbortIfAnyCollapsed) {
|
|
||||||
for (DSGraph::node_const_iterator I = G.node_begin(), E = G.node_end();
|
|
||||||
I != E; ++I)
|
|
||||||
if (I->isNodeCompletelyFolded()) {
|
|
||||||
cerr << "Node is collapsed: ";
|
|
||||||
I->print(cerr, &G);
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!AbortIfCollapsed.empty() || !CheckFlags.empty() ||
|
|
||||||
!AbortIfMerged.empty()) {
|
|
||||||
// Convert from a list to a set, because we don't have cl::set's yet. FIXME
|
|
||||||
std::set<std::string> AbortIfCollapsedS(AbortIfCollapsed.begin(),
|
|
||||||
AbortIfCollapsed.end());
|
|
||||||
std::set<std::string> AbortIfMergedS(AbortIfMerged.begin(),
|
|
||||||
AbortIfMerged.end());
|
|
||||||
std::map<std::string, unsigned> CheckFlagsM;
|
|
||||||
|
|
||||||
for (cl::list<std::string>::iterator I = CheckFlags.begin(),
|
|
||||||
E = CheckFlags.end(); I != E; ++I) {
|
|
||||||
std::string::size_type ColonPos = I->rfind(':');
|
|
||||||
if (ColonPos == std::string::npos) {
|
|
||||||
cerr << "Error: '" << *I
|
|
||||||
<< "' is an invalid value for the --dsgc-check-flags option!\n";
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned Flags = 0;
|
|
||||||
for (unsigned C = ColonPos+1; C != I->size(); ++C)
|
|
||||||
switch ((*I)[C]) {
|
|
||||||
case 'S': Flags |= DSNode::AllocaNode; break;
|
|
||||||
case 'H': Flags |= DSNode::HeapNode; break;
|
|
||||||
case 'G': Flags |= DSNode::GlobalNode; break;
|
|
||||||
case 'U': Flags |= DSNode::UnknownNode; break;
|
|
||||||
case 'I': Flags |= DSNode::Incomplete; break;
|
|
||||||
case 'M': Flags |= DSNode::Modified; break;
|
|
||||||
case 'R': Flags |= DSNode::Read; break;
|
|
||||||
case 'A': Flags |= DSNode::Array; break;
|
|
||||||
default: cerr << "Invalid DSNode flag!\n"; abort();
|
|
||||||
}
|
|
||||||
CheckFlagsM[std::string(I->begin(), I->begin()+ColonPos)] = Flags;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now we loop over all of the scalars, checking to see if any are collapsed
|
|
||||||
// that are not supposed to be, or if any are merged together.
|
|
||||||
const DSGraph::ScalarMapTy &SM = G.getScalarMap();
|
|
||||||
std::map<DSNode*, std::string> AbortIfMergedNodes;
|
|
||||||
|
|
||||||
for (DSGraph::ScalarMapTy::const_iterator I = SM.begin(), E = SM.end();
|
|
||||||
I != E; ++I)
|
|
||||||
if (I->first->hasName() && I->second.getNode()) {
|
|
||||||
const std::string &Name = I->first->getName();
|
|
||||||
DSNode *N = I->second.getNode();
|
|
||||||
|
|
||||||
// Verify it is not collapsed if it is not supposed to be...
|
|
||||||
if (N->isNodeCompletelyFolded() && AbortIfCollapsedS.count(Name)) {
|
|
||||||
cerr << "Node for value '%" << Name << "' is collapsed: ";
|
|
||||||
N->print(cerr, &G);
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (CheckFlagsM.count(Name) && CheckFlagsM[Name] != N->getNodeFlags()) {
|
|
||||||
cerr << "Node flags are not as expected for node: " << Name
|
|
||||||
<< " (" << CheckFlagsM[Name] << ":" <<N->getNodeFlags()
|
|
||||||
<< ")\n";
|
|
||||||
N->print(cerr, &G);
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify that it is not merged if it is not supposed to be...
|
|
||||||
if (AbortIfMergedS.count(Name)) {
|
|
||||||
if (AbortIfMergedNodes.count(N)) {
|
|
||||||
cerr << "Nodes for values '%" << Name << "' and '%"
|
|
||||||
<< AbortIfMergedNodes[N] << "' is merged: ";
|
|
||||||
N->print(cerr, &G);
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
AbortIfMergedNodes[N] = Name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load Diff
@@ -1,14 +0,0 @@
|
|||||||
##===- lib/Analysis/DataStructure/Makefile -----------------*- Makefile -*-===##
|
|
||||||
#
|
|
||||||
# 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.
|
|
||||||
#
|
|
||||||
##===----------------------------------------------------------------------===##
|
|
||||||
|
|
||||||
LEVEL = ../../..
|
|
||||||
LIBRARYNAME = LLVMDataStructure
|
|
||||||
|
|
||||||
include $(LEVEL)/Makefile.common
|
|
||||||
|
|
@@ -1,356 +0,0 @@
|
|||||||
//===- Printer.cpp - Code for printing data structure graphs nicely -------===//
|
|
||||||
//
|
|
||||||
// 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 'dot' graph printer.
|
|
||||||
//
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
#include "llvm/Analysis/DataStructure/DataStructure.h"
|
|
||||||
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
|
||||||
#include "llvm/Analysis/DataStructure/DSGraphTraits.h"
|
|
||||||
#include "llvm/Module.h"
|
|
||||||
#include "llvm/Constants.h"
|
|
||||||
#include "llvm/Assembly/Writer.h"
|
|
||||||
#include "llvm/Support/CommandLine.h"
|
|
||||||
#include "llvm/Support/GraphWriter.h"
|
|
||||||
#include "llvm/Support/Streams.h"
|
|
||||||
#include "llvm/ADT/Statistic.h"
|
|
||||||
#include "llvm/Config/config.h"
|
|
||||||
#include <ostream>
|
|
||||||
#include <fstream>
|
|
||||||
#include <sstream>
|
|
||||||
using namespace llvm;
|
|
||||||
|
|
||||||
// OnlyPrintMain - The DataStructure printer exposes this option to allow
|
|
||||||
// printing of only the graph for "main".
|
|
||||||
//
|
|
||||||
namespace {
|
|
||||||
cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
|
|
||||||
cl::opt<bool> DontPrintAnything("dont-print-ds", cl::ReallyHidden);
|
|
||||||
Statistic MaxGraphSize ("dsa", "Maximum graph size");
|
|
||||||
Statistic NumFoldedNodes ("dsa", "Number of folded nodes (in final graph)");
|
|
||||||
}
|
|
||||||
|
|
||||||
void DSNode::dump() const { print(cerr, 0); }
|
|
||||||
|
|
||||||
static std::string getCaption(const DSNode *N, const DSGraph *G) {
|
|
||||||
std::stringstream OS;
|
|
||||||
Module *M = 0;
|
|
||||||
|
|
||||||
if (!G) G = N->getParentGraph();
|
|
||||||
|
|
||||||
// Get the module from ONE of the functions in the graph it is available.
|
|
||||||
if (G && G->retnodes_begin() != G->retnodes_end())
|
|
||||||
M = G->retnodes_begin()->first->getParent();
|
|
||||||
if (M == 0 && G) {
|
|
||||||
// If there is a global in the graph, we can use it to find the module.
|
|
||||||
const DSScalarMap &SM = G->getScalarMap();
|
|
||||||
if (SM.global_begin() != SM.global_end())
|
|
||||||
M = (*SM.global_begin())->getParent();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (N->isNodeCompletelyFolded())
|
|
||||||
OS << "COLLAPSED";
|
|
||||||
else {
|
|
||||||
WriteTypeSymbolic(OS, N->getType(), M);
|
|
||||||
if (N->isArray())
|
|
||||||
OS << " array";
|
|
||||||
}
|
|
||||||
if (unsigned NodeType = N->getNodeFlags()) {
|
|
||||||
OS << ": ";
|
|
||||||
if (NodeType & DSNode::AllocaNode ) OS << "S";
|
|
||||||
if (NodeType & DSNode::HeapNode ) OS << "H";
|
|
||||||
if (NodeType & DSNode::GlobalNode ) OS << "G";
|
|
||||||
if (NodeType & DSNode::UnknownNode) OS << "U";
|
|
||||||
if (NodeType & DSNode::Incomplete ) OS << "I";
|
|
||||||
if (NodeType & DSNode::Modified ) OS << "M";
|
|
||||||
if (NodeType & DSNode::Read ) OS << "R";
|
|
||||||
#ifndef NDEBUG
|
|
||||||
if (NodeType & DSNode::DEAD ) OS << "<dead>";
|
|
||||||
#endif
|
|
||||||
OS << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
EquivalenceClasses<GlobalValue*> *GlobalECs = 0;
|
|
||||||
if (G) GlobalECs = &G->getGlobalECs();
|
|
||||||
|
|
||||||
for (unsigned i = 0, e = N->getGlobalsList().size(); i != e; ++i) {
|
|
||||||
WriteAsOperand(OS, N->getGlobalsList()[i], false, M);
|
|
||||||
|
|
||||||
// Figure out how many globals are equivalent to this one.
|
|
||||||
if (GlobalECs) {
|
|
||||||
EquivalenceClasses<GlobalValue*>::iterator I =
|
|
||||||
GlobalECs->findValue(N->getGlobalsList()[i]);
|
|
||||||
if (I != GlobalECs->end()) {
|
|
||||||
unsigned NumMembers =
|
|
||||||
std::distance(GlobalECs->member_begin(I), GlobalECs->member_end());
|
|
||||||
if (NumMembers != 1) OS << " + " << (NumMembers-1) << " EC";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
OS << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
return OS.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace llvm {
|
|
||||||
template<>
|
|
||||||
struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
|
|
||||||
static std::string getGraphName(const DSGraph *G) {
|
|
||||||
switch (G->getReturnNodes().size()) {
|
|
||||||
case 0: return G->getFunctionNames();
|
|
||||||
case 1: return "Function " + G->getFunctionNames();
|
|
||||||
default: return "Functions: " + G->getFunctionNames();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
|
|
||||||
return getCaption(Node, Graph);
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string getNodeAttributes(const DSNode *N, const DSGraph *Graph) {
|
|
||||||
return "shape=Mrecord";
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool edgeTargetsEdgeSource(const void *Node,
|
|
||||||
DSNode::const_iterator I) {
|
|
||||||
unsigned O = I.getNode()->getLink(I.getOffset()).getOffset();
|
|
||||||
return (O >> DS::PointerShift) != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static DSNode::const_iterator getEdgeTarget(const DSNode *Node,
|
|
||||||
DSNode::const_iterator I) {
|
|
||||||
unsigned O = I.getNode()->getLink(I.getOffset()).getOffset();
|
|
||||||
unsigned LinkNo = O >> DS::PointerShift;
|
|
||||||
const DSNode *N = *I;
|
|
||||||
DSNode::const_iterator R = N->begin();
|
|
||||||
for (; LinkNo; --LinkNo)
|
|
||||||
++R;
|
|
||||||
return R;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
|
|
||||||
/// and the return node.
|
|
||||||
///
|
|
||||||
static void addCustomGraphFeatures(const DSGraph *G,
|
|
||||||
GraphWriter<const DSGraph*> &GW) {
|
|
||||||
Module *CurMod = 0;
|
|
||||||
if (G->retnodes_begin() != G->retnodes_end())
|
|
||||||
CurMod = G->retnodes_begin()->first->getParent();
|
|
||||||
else {
|
|
||||||
// If there is a global in the graph, we can use it to find the module.
|
|
||||||
const DSScalarMap &SM = G->getScalarMap();
|
|
||||||
if (SM.global_begin() != SM.global_end())
|
|
||||||
CurMod = (*SM.global_begin())->getParent();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Add scalar nodes to the graph...
|
|
||||||
const DSGraph::ScalarMapTy &VM = G->getScalarMap();
|
|
||||||
for (DSGraph::ScalarMapTy::const_iterator I = VM.begin(); I != VM.end();++I)
|
|
||||||
if (!isa<GlobalValue>(I->first)) {
|
|
||||||
std::stringstream OS;
|
|
||||||
WriteAsOperand(OS, I->first, false, CurMod);
|
|
||||||
GW.emitSimpleNode(I->first, "", OS.str());
|
|
||||||
|
|
||||||
// Add edge from return node to real destination
|
|
||||||
DSNode *DestNode = I->second.getNode();
|
|
||||||
int EdgeDest = I->second.getOffset() >> DS::PointerShift;
|
|
||||||
if (EdgeDest == 0) EdgeDest = -1;
|
|
||||||
GW.emitEdge(I->first, -1, DestNode,
|
|
||||||
EdgeDest, "arrowtail=tee,color=gray63");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Output the returned value pointer...
|
|
||||||
for (DSGraph::retnodes_iterator I = G->retnodes_begin(),
|
|
||||||
E = G->retnodes_end(); I != E; ++I)
|
|
||||||
if (I->second.getNode()) {
|
|
||||||
std::string Label;
|
|
||||||
if (G->getReturnNodes().size() == 1)
|
|
||||||
Label = "returning";
|
|
||||||
else
|
|
||||||
Label = I->first->getName() + " ret node";
|
|
||||||
// Output the return node...
|
|
||||||
GW.emitSimpleNode((void*)I->first, "plaintext=circle", Label);
|
|
||||||
|
|
||||||
// Add edge from return node to real destination
|
|
||||||
DSNode *RetNode = I->second.getNode();
|
|
||||||
int RetEdgeDest = I->second.getOffset() >> DS::PointerShift;;
|
|
||||||
if (RetEdgeDest == 0) RetEdgeDest = -1;
|
|
||||||
GW.emitEdge((void*)I->first, -1, RetNode,
|
|
||||||
RetEdgeDest, "arrowtail=tee,color=gray63");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Output all of the call nodes...
|
|
||||||
const std::list<DSCallSite> &FCs =
|
|
||||||
G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
|
|
||||||
: G->getFunctionCalls();
|
|
||||||
for (std::list<DSCallSite>::const_iterator I = FCs.begin(), E = FCs.end();
|
|
||||||
I != E; ++I) {
|
|
||||||
const DSCallSite &Call = *I;
|
|
||||||
std::vector<std::string> EdgeSourceCaptions(Call.getNumPtrArgs()+2);
|
|
||||||
EdgeSourceCaptions[0] = "r";
|
|
||||||
if (Call.isDirectCall())
|
|
||||||
EdgeSourceCaptions[1] = Call.getCalleeFunc()->getName();
|
|
||||||
else
|
|
||||||
EdgeSourceCaptions[1] = "f";
|
|
||||||
|
|
||||||
GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2,
|
|
||||||
&EdgeSourceCaptions);
|
|
||||||
|
|
||||||
if (DSNode *N = Call.getRetVal().getNode()) {
|
|
||||||
int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
|
|
||||||
if (EdgeDest == 0) EdgeDest = -1;
|
|
||||||
GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63,tailclip=false");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print out the callee...
|
|
||||||
if (Call.isIndirectCall()) {
|
|
||||||
DSNode *N = Call.getCalleeNode();
|
|
||||||
assert(N && "Null call site callee node!");
|
|
||||||
GW.emitEdge(&Call, 1, N, -1, "color=gray63,tailclip=false");
|
|
||||||
}
|
|
||||||
|
|
||||||
for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
|
|
||||||
if (DSNode *N = Call.getPtrArg(j).getNode()) {
|
|
||||||
int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
|
|
||||||
if (EdgeDest == 0) EdgeDest = -1;
|
|
||||||
GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63,tailclip=false");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} // end namespace llvm
|
|
||||||
|
|
||||||
void DSNode::print(std::ostream &O, const DSGraph *G) const {
|
|
||||||
GraphWriter<const DSGraph *> W(O, G);
|
|
||||||
W.writeNode(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
void DSGraph::print(std::ostream &O) const {
|
|
||||||
WriteGraph(O, this, "DataStructures");
|
|
||||||
}
|
|
||||||
|
|
||||||
void DSGraph::writeGraphToFile(std::ostream &O,
|
|
||||||
const std::string &GraphName) const {
|
|
||||||
std::string Filename = GraphName + ".dot";
|
|
||||||
O << "Writing '" << Filename << "'...";
|
|
||||||
std::ofstream F(Filename.c_str());
|
|
||||||
|
|
||||||
if (F.good()) {
|
|
||||||
print(F);
|
|
||||||
unsigned NumCalls = shouldPrintAuxCalls() ?
|
|
||||||
getAuxFunctionCalls().size() : getFunctionCalls().size();
|
|
||||||
O << " [" << getGraphSize() << "+" << NumCalls << "]\n";
|
|
||||||
} else {
|
|
||||||
O << " error opening file for writing!\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// viewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
|
|
||||||
/// then cleanup. For use from the debugger.
|
|
||||||
///
|
|
||||||
void DSGraph::viewGraph() const {
|
|
||||||
ViewGraph(this, "ds.tempgraph", "DataStructures");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template <typename Collection>
|
|
||||||
static void printCollection(const Collection &C, std::ostream &O,
|
|
||||||
const Module *M, const std::string &Prefix) {
|
|
||||||
if (M == 0) {
|
|
||||||
O << "Null Module pointer, cannot continue!\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned TotalNumNodes = 0, TotalCallNodes = 0;
|
|
||||||
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
||||||
if (C.hasGraph(*I)) {
|
|
||||||
DSGraph &Gr = C.getDSGraph((Function&)*I);
|
|
||||||
unsigned NumCalls = Gr.shouldPrintAuxCalls() ?
|
|
||||||
Gr.getAuxFunctionCalls().size() : Gr.getFunctionCalls().size();
|
|
||||||
bool IsDuplicateGraph = false;
|
|
||||||
|
|
||||||
if (I->getName() == "main" || !OnlyPrintMain) {
|
|
||||||
Function *SCCFn = Gr.retnodes_begin()->first;
|
|
||||||
if (&*I == SCCFn) {
|
|
||||||
Gr.writeGraphToFile(O, Prefix+I->getName());
|
|
||||||
} else {
|
|
||||||
IsDuplicateGraph = true; // Don't double count node/call nodes.
|
|
||||||
O << "Didn't write '" << Prefix+I->getName()
|
|
||||||
<< ".dot' - Graph already emitted to '" << Prefix+SCCFn->getName()
|
|
||||||
<< "\n";
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Function *SCCFn = Gr.retnodes_begin()->first;
|
|
||||||
if (&*I == SCCFn) {
|
|
||||||
O << "Skipped Writing '" << Prefix+I->getName() << ".dot'... ["
|
|
||||||
<< Gr.getGraphSize() << "+" << NumCalls << "]\n";
|
|
||||||
} else {
|
|
||||||
IsDuplicateGraph = true; // Don't double count node/call nodes.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!IsDuplicateGraph) {
|
|
||||||
unsigned GraphSize = Gr.getGraphSize();
|
|
||||||
if (MaxGraphSize < GraphSize) MaxGraphSize = GraphSize;
|
|
||||||
|
|
||||||
TotalNumNodes += Gr.getGraphSize();
|
|
||||||
TotalCallNodes += NumCalls;
|
|
||||||
for (DSGraph::node_iterator NI = Gr.node_begin(), E = Gr.node_end();
|
|
||||||
NI != E; ++NI)
|
|
||||||
if (NI->isNodeCompletelyFolded())
|
|
||||||
++NumFoldedNodes;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
DSGraph &GG = C.getGlobalsGraph();
|
|
||||||
TotalNumNodes += GG.getGraphSize();
|
|
||||||
TotalCallNodes += GG.getFunctionCalls().size();
|
|
||||||
if (!OnlyPrintMain) {
|
|
||||||
GG.writeGraphToFile(O, Prefix+"GlobalsGraph");
|
|
||||||
} else {
|
|
||||||
O << "Skipped Writing '" << Prefix << "GlobalsGraph.dot'... ["
|
|
||||||
<< GG.getGraphSize() << "+" << GG.getFunctionCalls().size() << "]\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
O << "\nGraphs contain [" << TotalNumNodes << "+" << TotalCallNodes
|
|
||||||
<< "] nodes total" << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// print - Print out the analysis results...
|
|
||||||
void LocalDataStructures::print(std::ostream &O, const Module *M) const {
|
|
||||||
if (DontPrintAnything) return;
|
|
||||||
printCollection(*this, O, M, "ds.");
|
|
||||||
}
|
|
||||||
|
|
||||||
void BUDataStructures::print(std::ostream &O, const Module *M) const {
|
|
||||||
if (DontPrintAnything) return;
|
|
||||||
printCollection(*this, O, M, "bu.");
|
|
||||||
}
|
|
||||||
|
|
||||||
void TDDataStructures::print(std::ostream &O, const Module *M) const {
|
|
||||||
if (DontPrintAnything) return;
|
|
||||||
printCollection(*this, O, M, "td.");
|
|
||||||
}
|
|
||||||
|
|
||||||
void CompleteBUDataStructures::print(std::ostream &O, const Module *M) const {
|
|
||||||
if (DontPrintAnything) return;
|
|
||||||
printCollection(*this, O, M, "cbu.");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void EquivClassGraphs::print(std::ostream &O, const Module *M) const {
|
|
||||||
if (DontPrintAnything) return;
|
|
||||||
printCollection(*this, O, M, "eq.");
|
|
||||||
}
|
|
||||||
|
|
@@ -1,278 +0,0 @@
|
|||||||
//===- Steensgaard.cpp - Context Insensitive Alias Analysis ---------------===//
|
|
||||||
//
|
|
||||||
// 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 uses the data structure graphs to implement a simple context
|
|
||||||
// insensitive alias analysis. It does this by computing the local analysis
|
|
||||||
// graphs for all of the functions, then merging them together into a single big
|
|
||||||
// graph without cloning.
|
|
||||||
//
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
#include "llvm/Analysis/DataStructure/DataStructure.h"
|
|
||||||
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
|
||||||
#include "llvm/Analysis/AliasAnalysis.h"
|
|
||||||
#include "llvm/Analysis/Passes.h"
|
|
||||||
#include "llvm/Module.h"
|
|
||||||
#include "llvm/Support/Debug.h"
|
|
||||||
#include <ostream>
|
|
||||||
using namespace llvm;
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
class Steens : public ModulePass, public AliasAnalysis {
|
|
||||||
DSGraph *ResultGraph;
|
|
||||||
|
|
||||||
EquivalenceClasses<GlobalValue*> GlobalECs; // Always empty
|
|
||||||
public:
|
|
||||||
Steens() : ResultGraph(0) {}
|
|
||||||
~Steens() {
|
|
||||||
releaseMyMemory();
|
|
||||||
assert(ResultGraph == 0 && "releaseMemory not called?");
|
|
||||||
}
|
|
||||||
|
|
||||||
//------------------------------------------------
|
|
||||||
// Implement the Pass API
|
|
||||||
//
|
|
||||||
|
|
||||||
// run - Build up the result graph, representing the pointer graph for the
|
|
||||||
// program.
|
|
||||||
//
|
|
||||||
bool runOnModule(Module &M);
|
|
||||||
|
|
||||||
virtual void releaseMyMemory() { delete ResultGraph; ResultGraph = 0; }
|
|
||||||
|
|
||||||
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
||||||
AliasAnalysis::getAnalysisUsage(AU);
|
|
||||||
AU.setPreservesAll(); // Does not transform code...
|
|
||||||
AU.addRequired<LocalDataStructures>(); // Uses local dsgraph
|
|
||||||
}
|
|
||||||
|
|
||||||
// print - Implement the Pass::print method...
|
|
||||||
void print(OStream O, const Module *M) const {
|
|
||||||
if (O.stream()) print(*O.stream(), M);
|
|
||||||
}
|
|
||||||
void print(std::ostream &O, const Module *M) const {
|
|
||||||
assert(ResultGraph && "Result graph has not yet been computed!");
|
|
||||||
ResultGraph->writeGraphToFile(O, "steensgaards");
|
|
||||||
}
|
|
||||||
|
|
||||||
//------------------------------------------------
|
|
||||||
// Implement the AliasAnalysis API
|
|
||||||
//
|
|
||||||
|
|
||||||
AliasResult alias(const Value *V1, unsigned V1Size,
|
|
||||||
const Value *V2, unsigned V2Size);
|
|
||||||
|
|
||||||
virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
|
|
||||||
virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
|
|
||||||
|
|
||||||
private:
|
|
||||||
void ResolveFunctionCall(Function *F, const DSCallSite &Call,
|
|
||||||
DSNodeHandle &RetVal);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Register the pass...
|
|
||||||
RegisterPass<Steens> X("steens-aa",
|
|
||||||
"Steensgaard's alias analysis (DSGraph based)");
|
|
||||||
|
|
||||||
// Register as an implementation of AliasAnalysis
|
|
||||||
RegisterAnalysisGroup<AliasAnalysis> Y(X);
|
|
||||||
}
|
|
||||||
|
|
||||||
ModulePass *llvm::createSteensgaardPass() { return new Steens(); }
|
|
||||||
|
|
||||||
/// ResolveFunctionCall - Resolve the actual arguments of a call to function F
|
|
||||||
/// with the specified call site descriptor. This function links the arguments
|
|
||||||
/// and the return value for the call site context-insensitively.
|
|
||||||
///
|
|
||||||
void Steens::ResolveFunctionCall(Function *F, const DSCallSite &Call,
|
|
||||||
DSNodeHandle &RetVal) {
|
|
||||||
assert(ResultGraph != 0 && "Result graph not allocated!");
|
|
||||||
DSGraph::ScalarMapTy &ValMap = ResultGraph->getScalarMap();
|
|
||||||
|
|
||||||
// Handle the return value of the function...
|
|
||||||
if (Call.getRetVal().getNode() && RetVal.getNode())
|
|
||||||
RetVal.mergeWith(Call.getRetVal());
|
|
||||||
|
|
||||||
// Loop over all pointer arguments, resolving them to their provided pointers
|
|
||||||
unsigned PtrArgIdx = 0;
|
|
||||||
for (Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
|
|
||||||
AI != AE && PtrArgIdx < Call.getNumPtrArgs(); ++AI) {
|
|
||||||
DSGraph::ScalarMapTy::iterator I = ValMap.find(AI);
|
|
||||||
if (I != ValMap.end()) // If its a pointer argument...
|
|
||||||
I->second.mergeWith(Call.getPtrArg(PtrArgIdx++));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// run - Build up the result graph, representing the pointer graph for the
|
|
||||||
/// program.
|
|
||||||
///
|
|
||||||
bool Steens::runOnModule(Module &M) {
|
|
||||||
InitializeAliasAnalysis(this);
|
|
||||||
assert(ResultGraph == 0 && "Result graph already allocated!");
|
|
||||||
LocalDataStructures &LDS = getAnalysis<LocalDataStructures>();
|
|
||||||
|
|
||||||
// Create a new, empty, graph...
|
|
||||||
ResultGraph = new DSGraph(GlobalECs, getTargetData());
|
|
||||||
ResultGraph->spliceFrom(LDS.getGlobalsGraph());
|
|
||||||
|
|
||||||
// Loop over the rest of the module, merging graphs for non-external functions
|
|
||||||
// into this graph.
|
|
||||||
//
|
|
||||||
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
|
||||||
if (!I->isExternal())
|
|
||||||
ResultGraph->spliceFrom(LDS.getDSGraph(*I));
|
|
||||||
|
|
||||||
ResultGraph->removeTriviallyDeadNodes();
|
|
||||||
|
|
||||||
// FIXME: Must recalculate and use the Incomplete markers!!
|
|
||||||
|
|
||||||
// Now that we have all of the graphs inlined, we can go about eliminating
|
|
||||||
// call nodes...
|
|
||||||
//
|
|
||||||
std::list<DSCallSite> &Calls = ResultGraph->getAuxFunctionCalls();
|
|
||||||
assert(Calls.empty() && "Aux call list is already in use??");
|
|
||||||
|
|
||||||
// Start with a copy of the original call sites.
|
|
||||||
Calls = ResultGraph->getFunctionCalls();
|
|
||||||
|
|
||||||
for (std::list<DSCallSite>::iterator CI = Calls.begin(), E = Calls.end();
|
|
||||||
CI != E;) {
|
|
||||||
DSCallSite &CurCall = *CI++;
|
|
||||||
|
|
||||||
// Loop over the called functions, eliminating as many as possible...
|
|
||||||
std::vector<Function*> CallTargets;
|
|
||||||
if (CurCall.isDirectCall())
|
|
||||||
CallTargets.push_back(CurCall.getCalleeFunc());
|
|
||||||
else
|
|
||||||
CurCall.getCalleeNode()->addFullFunctionList(CallTargets);
|
|
||||||
|
|
||||||
for (unsigned c = 0; c != CallTargets.size(); ) {
|
|
||||||
// If we can eliminate this function call, do so!
|
|
||||||
Function *F = CallTargets[c];
|
|
||||||
if (!F->isExternal()) {
|
|
||||||
ResolveFunctionCall(F, CurCall, ResultGraph->getReturnNodes()[F]);
|
|
||||||
CallTargets[c] = CallTargets.back();
|
|
||||||
CallTargets.pop_back();
|
|
||||||
} else
|
|
||||||
++c; // Cannot eliminate this call, skip over it...
|
|
||||||
}
|
|
||||||
|
|
||||||
if (CallTargets.empty()) { // Eliminated all calls?
|
|
||||||
std::list<DSCallSite>::iterator I = CI;
|
|
||||||
Calls.erase(--I); // Remove entry
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove our knowledge of what the return values of the functions are, except
|
|
||||||
// for functions that are externally visible from this module (e.g. main). We
|
|
||||||
// keep these functions so that their arguments are marked incomplete.
|
|
||||||
for (DSGraph::ReturnNodesTy::iterator I =
|
|
||||||
ResultGraph->getReturnNodes().begin(),
|
|
||||||
E = ResultGraph->getReturnNodes().end(); I != E; )
|
|
||||||
if (I->first->hasInternalLinkage())
|
|
||||||
ResultGraph->getReturnNodes().erase(I++);
|
|
||||||
else
|
|
||||||
++I;
|
|
||||||
|
|
||||||
// Update the "incomplete" markers on the nodes, ignoring unknownness due to
|
|
||||||
// incoming arguments...
|
|
||||||
ResultGraph->maskIncompleteMarkers();
|
|
||||||
ResultGraph->markIncompleteNodes(DSGraph::IgnoreGlobals |
|
|
||||||
DSGraph::MarkFormalArgs);
|
|
||||||
|
|
||||||
// Remove any nodes that are dead after all of the merging we have done...
|
|
||||||
// FIXME: We should be able to disable the globals graph for steens!
|
|
||||||
//ResultGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
|
|
||||||
|
|
||||||
print(DOUT, &M);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
AliasAnalysis::AliasResult Steens::alias(const Value *V1, unsigned V1Size,
|
|
||||||
const Value *V2, unsigned V2Size) {
|
|
||||||
assert(ResultGraph && "Result graph has not been computed yet!");
|
|
||||||
|
|
||||||
DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap();
|
|
||||||
|
|
||||||
DSGraph::ScalarMapTy::iterator I = GSM.find(const_cast<Value*>(V1));
|
|
||||||
DSGraph::ScalarMapTy::iterator J = GSM.find(const_cast<Value*>(V2));
|
|
||||||
if (I != GSM.end() && !I->second.isNull() &&
|
|
||||||
J != GSM.end() && !J->second.isNull()) {
|
|
||||||
DSNodeHandle &V1H = I->second;
|
|
||||||
DSNodeHandle &V2H = J->second;
|
|
||||||
|
|
||||||
// If at least one of the nodes is complete, we can say something about
|
|
||||||
// this. If one is complete and the other isn't, then they are obviously
|
|
||||||
// different nodes. If they are both complete, we can't say anything
|
|
||||||
// useful.
|
|
||||||
if (I->second.getNode()->isComplete() ||
|
|
||||||
J->second.getNode()->isComplete()) {
|
|
||||||
// If the two pointers point to different data structure graph nodes, they
|
|
||||||
// cannot alias!
|
|
||||||
if (V1H.getNode() != V2H.getNode())
|
|
||||||
return NoAlias;
|
|
||||||
|
|
||||||
// See if they point to different offsets... if so, we may be able to
|
|
||||||
// determine that they do not alias...
|
|
||||||
unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
|
|
||||||
if (O1 != O2) {
|
|
||||||
if (O2 < O1) { // Ensure that O1 <= O2
|
|
||||||
std::swap(V1, V2);
|
|
||||||
std::swap(O1, O2);
|
|
||||||
std::swap(V1Size, V2Size);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (O1+V1Size <= O2)
|
|
||||||
return NoAlias;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we cannot determine alias properties based on our graph, fall back on
|
|
||||||
// some other AA implementation.
|
|
||||||
//
|
|
||||||
return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
|
|
||||||
}
|
|
||||||
|
|
||||||
AliasAnalysis::ModRefResult
|
|
||||||
Steens::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
|
|
||||||
AliasAnalysis::ModRefResult Result = ModRef;
|
|
||||||
|
|
||||||
// Find the node in question.
|
|
||||||
DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap();
|
|
||||||
DSGraph::ScalarMapTy::iterator I = GSM.find(P);
|
|
||||||
|
|
||||||
if (I != GSM.end() && !I->second.isNull()) {
|
|
||||||
DSNode *N = I->second.getNode();
|
|
||||||
if (N->isComplete()) {
|
|
||||||
// If this is a direct call to an external function, and if the pointer
|
|
||||||
// points to a complete node, the external function cannot modify or read
|
|
||||||
// the value (we know it's not passed out of the program!).
|
|
||||||
if (Function *F = CS.getCalledFunction())
|
|
||||||
if (F->isExternal())
|
|
||||||
return NoModRef;
|
|
||||||
|
|
||||||
// Otherwise, if the node is complete, but it is only M or R, return this.
|
|
||||||
// This can be useful for globals that should be marked const but are not.
|
|
||||||
if (!N->isModified())
|
|
||||||
Result = (ModRefResult)(Result & ~Mod);
|
|
||||||
if (!N->isRead())
|
|
||||||
Result = (ModRefResult)(Result & ~Ref);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return (ModRefResult)(Result & AliasAnalysis::getModRefInfo(CS, P, Size));
|
|
||||||
}
|
|
||||||
|
|
||||||
AliasAnalysis::ModRefResult
|
|
||||||
Steens::getModRefInfo(CallSite CS1, CallSite CS2)
|
|
||||||
{
|
|
||||||
return AliasAnalysis::getModRefInfo(CS1,CS2);
|
|
||||||
}
|
|
@@ -1,466 +0,0 @@
|
|||||||
//===- TopDownClosure.cpp - Compute the top-down interprocedure 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 TDDataStructures class, which represents the
|
|
||||||
// Top-down Interprocedural closure of the data structure graph over the
|
|
||||||
// program. This is useful (but not strictly necessary?) for applications
|
|
||||||
// like pointer analysis.
|
|
||||||
//
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
#define DEBUG_TYPE "td_dsa"
|
|
||||||
#include "llvm/Analysis/DataStructure/DataStructure.h"
|
|
||||||
#include "llvm/Module.h"
|
|
||||||
#include "llvm/DerivedTypes.h"
|
|
||||||
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
|
||||||
#include "llvm/Support/Debug.h"
|
|
||||||
#include "llvm/Support/Timer.h"
|
|
||||||
#include "llvm/ADT/Statistic.h"
|
|
||||||
using namespace llvm;
|
|
||||||
|
|
||||||
#if 0
|
|
||||||
#define TIME_REGION(VARNAME, DESC) \
|
|
||||||
NamedRegionTimer VARNAME(DESC)
|
|
||||||
#else
|
|
||||||
#define TIME_REGION(VARNAME, DESC)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
RegisterPass<TDDataStructures> // Register the pass
|
|
||||||
Y("tddatastructure", "Top-down Data Structure Analysis");
|
|
||||||
|
|
||||||
Statistic NumTDInlines("tddatastructures", "Number of graphs inlined");
|
|
||||||
}
|
|
||||||
|
|
||||||
void TDDataStructures::markReachableFunctionsExternallyAccessible(DSNode *N,
|
|
||||||
hash_set<DSNode*> &Visited) {
|
|
||||||
if (!N || Visited.count(N)) return;
|
|
||||||
Visited.insert(N);
|
|
||||||
|
|
||||||
for (unsigned i = 0, e = N->getNumLinks(); i != e; ++i) {
|
|
||||||
DSNodeHandle &NH = N->getLink(i*N->getPointerSize());
|
|
||||||
if (DSNode *NN = NH.getNode()) {
|
|
||||||
std::vector<Function*> Functions;
|
|
||||||
NN->addFullFunctionList(Functions);
|
|
||||||
ArgsRemainIncomplete.insert(Functions.begin(), Functions.end());
|
|
||||||
markReachableFunctionsExternallyAccessible(NN, Visited);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// run - Calculate the top down data structure graphs for each function in the
|
|
||||||
// program.
|
|
||||||
//
|
|
||||||
bool TDDataStructures::runOnModule(Module &M) {
|
|
||||||
BUInfo = &getAnalysis<BUDataStructures>();
|
|
||||||
GlobalECs = BUInfo->getGlobalECs();
|
|
||||||
GlobalsGraph = new DSGraph(BUInfo->getGlobalsGraph(), GlobalECs);
|
|
||||||
GlobalsGraph->setPrintAuxCalls();
|
|
||||||
|
|
||||||
// Figure out which functions must not mark their arguments complete because
|
|
||||||
// they are accessible outside this compilation unit. Currently, these
|
|
||||||
// arguments are functions which are reachable by global variables in the
|
|
||||||
// globals graph.
|
|
||||||
const DSScalarMap &GGSM = GlobalsGraph->getScalarMap();
|
|
||||||
hash_set<DSNode*> Visited;
|
|
||||||
for (DSScalarMap::global_iterator I=GGSM.global_begin(), E=GGSM.global_end();
|
|
||||||
I != E; ++I) {
|
|
||||||
DSNode *N = GGSM.find(*I)->second.getNode();
|
|
||||||
if (N->isIncomplete())
|
|
||||||
markReachableFunctionsExternallyAccessible(N, Visited);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Loop over unresolved call nodes. Any functions passed into (but not
|
|
||||||
// returned!) from unresolvable call nodes may be invoked outside of the
|
|
||||||
// current module.
|
|
||||||
for (DSGraph::afc_iterator I = GlobalsGraph->afc_begin(),
|
|
||||||
E = GlobalsGraph->afc_end(); I != E; ++I)
|
|
||||||
for (unsigned arg = 0, e = I->getNumPtrArgs(); arg != e; ++arg)
|
|
||||||
markReachableFunctionsExternallyAccessible(I->getPtrArg(arg).getNode(),
|
|
||||||
Visited);
|
|
||||||
Visited.clear();
|
|
||||||
|
|
||||||
// Functions without internal linkage also have unknown incoming arguments!
|
|
||||||
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
|
||||||
if (!I->isExternal() && !I->hasInternalLinkage())
|
|
||||||
ArgsRemainIncomplete.insert(I);
|
|
||||||
|
|
||||||
// We want to traverse the call graph in reverse post-order. To do this, we
|
|
||||||
// calculate a post-order traversal, then reverse it.
|
|
||||||
hash_set<DSGraph*> VisitedGraph;
|
|
||||||
std::vector<DSGraph*> PostOrder;
|
|
||||||
|
|
||||||
#if 0
|
|
||||||
{TIME_REGION(XXX, "td:Copy graphs");
|
|
||||||
|
|
||||||
// Visit each of the graphs in reverse post-order now!
|
|
||||||
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
|
||||||
if (!I->isExternal())
|
|
||||||
getOrCreateDSGraph(*I);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
{TIME_REGION(XXX, "td:Compute postorder");
|
|
||||||
|
|
||||||
// Calculate top-down from main...
|
|
||||||
if (Function *F = M.getMainFunction())
|
|
||||||
ComputePostOrder(*F, VisitedGraph, PostOrder);
|
|
||||||
|
|
||||||
// Next calculate the graphs for each unreachable function...
|
|
||||||
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
|
||||||
ComputePostOrder(*I, VisitedGraph, PostOrder);
|
|
||||||
|
|
||||||
VisitedGraph.clear(); // Release memory!
|
|
||||||
}
|
|
||||||
|
|
||||||
{TIME_REGION(XXX, "td:Inline stuff");
|
|
||||||
|
|
||||||
// Visit each of the graphs in reverse post-order now!
|
|
||||||
while (!PostOrder.empty()) {
|
|
||||||
InlineCallersIntoGraph(*PostOrder.back());
|
|
||||||
PostOrder.pop_back();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Free the IndCallMap.
|
|
||||||
while (!IndCallMap.empty()) {
|
|
||||||
delete IndCallMap.begin()->second;
|
|
||||||
IndCallMap.erase(IndCallMap.begin());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ArgsRemainIncomplete.clear();
|
|
||||||
GlobalsGraph->removeTriviallyDeadNodes();
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
DSGraph &TDDataStructures::getOrCreateDSGraph(Function &F) {
|
|
||||||
DSGraph *&G = DSInfo[&F];
|
|
||||||
if (G == 0) { // Not created yet? Clone BU graph...
|
|
||||||
G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F), GlobalECs,
|
|
||||||
DSGraph::DontCloneAuxCallNodes);
|
|
||||||
assert(G->getAuxFunctionCalls().empty() && "Cloned aux calls?");
|
|
||||||
G->setPrintAuxCalls();
|
|
||||||
G->setGlobalsGraph(GlobalsGraph);
|
|
||||||
|
|
||||||
// Note that this graph is the graph for ALL of the function in the SCC, not
|
|
||||||
// just F.
|
|
||||||
for (DSGraph::retnodes_iterator RI = G->retnodes_begin(),
|
|
||||||
E = G->retnodes_end(); RI != E; ++RI)
|
|
||||||
if (RI->first != &F)
|
|
||||||
DSInfo[RI->first] = G;
|
|
||||||
}
|
|
||||||
return *G;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void TDDataStructures::ComputePostOrder(Function &F,hash_set<DSGraph*> &Visited,
|
|
||||||
std::vector<DSGraph*> &PostOrder) {
|
|
||||||
if (F.isExternal()) return;
|
|
||||||
DSGraph &G = getOrCreateDSGraph(F);
|
|
||||||
if (Visited.count(&G)) return;
|
|
||||||
Visited.insert(&G);
|
|
||||||
|
|
||||||
// Recursively traverse all of the callee graphs.
|
|
||||||
for (DSGraph::fc_iterator CI = G.fc_begin(), CE = G.fc_end(); CI != CE; ++CI){
|
|
||||||
Instruction *CallI = CI->getCallSite().getInstruction();
|
|
||||||
for (BUDataStructures::callee_iterator I = BUInfo->callee_begin(CallI),
|
|
||||||
E = BUInfo->callee_end(CallI); I != E; ++I)
|
|
||||||
ComputePostOrder(*I->second, Visited, PostOrder);
|
|
||||||
}
|
|
||||||
|
|
||||||
PostOrder.push_back(&G);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// releaseMemory - If the pass pipeline is done with this pass, we can release
|
|
||||||
// our memory... here...
|
|
||||||
//
|
|
||||||
// FIXME: This should be releaseMemory and will work fine, except that LoadVN
|
|
||||||
// has no way to extend the lifetime of the pass, which screws up ds-aa.
|
|
||||||
//
|
|
||||||
void TDDataStructures::releaseMyMemory() {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Empty map so next time memory is released, data structures are not
|
|
||||||
// re-deleted.
|
|
||||||
DSInfo.clear();
|
|
||||||
delete GlobalsGraph;
|
|
||||||
GlobalsGraph = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// InlineCallersIntoGraph - Inline all of the callers of the specified DS graph
|
|
||||||
/// into it, then recompute completeness of nodes in the resultant graph.
|
|
||||||
void TDDataStructures::InlineCallersIntoGraph(DSGraph &DSG) {
|
|
||||||
// Inline caller graphs into this graph. First step, get the list of call
|
|
||||||
// sites that call into this graph.
|
|
||||||
std::vector<CallerCallEdge> EdgesFromCaller;
|
|
||||||
std::map<DSGraph*, std::vector<CallerCallEdge> >::iterator
|
|
||||||
CEI = CallerEdges.find(&DSG);
|
|
||||||
if (CEI != CallerEdges.end()) {
|
|
||||||
std::swap(CEI->second, EdgesFromCaller);
|
|
||||||
CallerEdges.erase(CEI);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sort the caller sites to provide a by-caller-graph ordering.
|
|
||||||
std::sort(EdgesFromCaller.begin(), EdgesFromCaller.end());
|
|
||||||
|
|
||||||
|
|
||||||
// Merge information from the globals graph into this graph. FIXME: This is
|
|
||||||
// stupid. Instead of us cloning information from the GG into this graph,
|
|
||||||
// then having RemoveDeadNodes clone it back, we should do all of this as a
|
|
||||||
// post-pass over all of the graphs. We need to take cloning out of
|
|
||||||
// removeDeadNodes and gut removeDeadNodes at the same time first though. :(
|
|
||||||
{
|
|
||||||
DSGraph &GG = *DSG.getGlobalsGraph();
|
|
||||||
ReachabilityCloner RC(DSG, GG,
|
|
||||||
DSGraph::DontCloneCallNodes |
|
|
||||||
DSGraph::DontCloneAuxCallNodes);
|
|
||||||
for (DSScalarMap::global_iterator
|
|
||||||
GI = DSG.getScalarMap().global_begin(),
|
|
||||||
E = DSG.getScalarMap().global_end(); GI != E; ++GI)
|
|
||||||
RC.getClonedNH(GG.getNodeForValue(*GI));
|
|
||||||
}
|
|
||||||
|
|
||||||
DOUT << "[TD] Inlining callers into '" << DSG.getFunctionNames() << "'\n";
|
|
||||||
|
|
||||||
// Iteratively inline caller graphs into this graph.
|
|
||||||
while (!EdgesFromCaller.empty()) {
|
|
||||||
DSGraph &CallerGraph = *EdgesFromCaller.back().CallerGraph;
|
|
||||||
|
|
||||||
// Iterate through all of the call sites of this graph, cloning and merging
|
|
||||||
// any nodes required by the call.
|
|
||||||
ReachabilityCloner RC(DSG, CallerGraph,
|
|
||||||
DSGraph::DontCloneCallNodes |
|
|
||||||
DSGraph::DontCloneAuxCallNodes);
|
|
||||||
|
|
||||||
// Inline all call sites from this caller graph.
|
|
||||||
do {
|
|
||||||
const DSCallSite &CS = *EdgesFromCaller.back().CS;
|
|
||||||
Function &CF = *EdgesFromCaller.back().CalledFunction;
|
|
||||||
DOUT << " [TD] Inlining graph into Fn '" << CF.getName() << "' from ";
|
|
||||||
if (CallerGraph.getReturnNodes().empty())
|
|
||||||
DOUT << "SYNTHESIZED INDIRECT GRAPH";
|
|
||||||
else
|
|
||||||
DOUT << "Fn '" << CS.getCallSite().getInstruction()->
|
|
||||||
getParent()->getParent()->getName() << "'";
|
|
||||||
DOUT << ": " << CF.getFunctionType()->getNumParams() << " args\n";
|
|
||||||
|
|
||||||
// Get the formal argument and return nodes for the called function and
|
|
||||||
// merge them with the cloned subgraph.
|
|
||||||
DSCallSite T1 = DSG.getCallSiteForArguments(CF);
|
|
||||||
RC.mergeCallSite(T1, CS);
|
|
||||||
++NumTDInlines;
|
|
||||||
|
|
||||||
EdgesFromCaller.pop_back();
|
|
||||||
} while (!EdgesFromCaller.empty() &&
|
|
||||||
EdgesFromCaller.back().CallerGraph == &CallerGraph);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Next, now that this graph is finalized, we need to recompute the
|
|
||||||
// incompleteness markers for this graph and remove unreachable nodes.
|
|
||||||
DSG.maskIncompleteMarkers();
|
|
||||||
|
|
||||||
// If any of the functions has incomplete incoming arguments, don't mark any
|
|
||||||
// of them as complete.
|
|
||||||
bool HasIncompleteArgs = false;
|
|
||||||
for (DSGraph::retnodes_iterator I = DSG.retnodes_begin(),
|
|
||||||
E = DSG.retnodes_end(); I != E; ++I)
|
|
||||||
if (ArgsRemainIncomplete.count(I->first)) {
|
|
||||||
HasIncompleteArgs = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Recompute the Incomplete markers. Depends on whether args are complete
|
|
||||||
unsigned Flags
|
|
||||||
= HasIncompleteArgs ? DSGraph::MarkFormalArgs : DSGraph::IgnoreFormalArgs;
|
|
||||||
DSG.markIncompleteNodes(Flags | DSGraph::IgnoreGlobals);
|
|
||||||
|
|
||||||
// Delete dead nodes. Treat globals that are unreachable as dead also.
|
|
||||||
DSG.removeDeadNodes(DSGraph::RemoveUnreachableGlobals);
|
|
||||||
|
|
||||||
// We are done with computing the current TD Graph! Finally, before we can
|
|
||||||
// finish processing this function, we figure out which functions it calls and
|
|
||||||
// records these call graph edges, so that we have them when we process the
|
|
||||||
// callee graphs.
|
|
||||||
if (DSG.fc_begin() == DSG.fc_end()) return;
|
|
||||||
|
|
||||||
// Loop over all the call sites and all the callees at each call site, and add
|
|
||||||
// edges to the CallerEdges structure for each callee.
|
|
||||||
for (DSGraph::fc_iterator CI = DSG.fc_begin(), E = DSG.fc_end();
|
|
||||||
CI != E; ++CI) {
|
|
||||||
|
|
||||||
// Handle direct calls efficiently.
|
|
||||||
if (CI->isDirectCall()) {
|
|
||||||
if (!CI->getCalleeFunc()->isExternal() &&
|
|
||||||
!DSG.getReturnNodes().count(CI->getCalleeFunc()))
|
|
||||||
CallerEdges[&getDSGraph(*CI->getCalleeFunc())]
|
|
||||||
.push_back(CallerCallEdge(&DSG, &*CI, CI->getCalleeFunc()));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Instruction *CallI = CI->getCallSite().getInstruction();
|
|
||||||
// For each function in the invoked function list at this call site...
|
|
||||||
BUDataStructures::callee_iterator IPI =
|
|
||||||
BUInfo->callee_begin(CallI), IPE = BUInfo->callee_end(CallI);
|
|
||||||
|
|
||||||
// Skip over all calls to this graph (SCC calls).
|
|
||||||
while (IPI != IPE && &getDSGraph(*IPI->second) == &DSG)
|
|
||||||
++IPI;
|
|
||||||
|
|
||||||
// All SCC calls?
|
|
||||||
if (IPI == IPE) continue;
|
|
||||||
|
|
||||||
Function *FirstCallee = IPI->second;
|
|
||||||
++IPI;
|
|
||||||
|
|
||||||
// Skip over more SCC calls.
|
|
||||||
while (IPI != IPE && &getDSGraph(*IPI->second) == &DSG)
|
|
||||||
++IPI;
|
|
||||||
|
|
||||||
// If there is exactly one callee from this call site, remember the edge in
|
|
||||||
// CallerEdges.
|
|
||||||
if (IPI == IPE) {
|
|
||||||
if (!FirstCallee->isExternal())
|
|
||||||
CallerEdges[&getDSGraph(*FirstCallee)]
|
|
||||||
.push_back(CallerCallEdge(&DSG, &*CI, FirstCallee));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, there are multiple callees from this call site, so it must be
|
|
||||||
// an indirect call. Chances are that there will be other call sites with
|
|
||||||
// this set of targets. If so, we don't want to do M*N inlining operations,
|
|
||||||
// so we build up a new, private, graph that represents the calls of all
|
|
||||||
// calls to this set of functions.
|
|
||||||
std::vector<Function*> Callees;
|
|
||||||
for (BUDataStructures::ActualCalleesTy::const_iterator I =
|
|
||||||
BUInfo->callee_begin(CallI), E = BUInfo->callee_end(CallI);
|
|
||||||
I != E; ++I)
|
|
||||||
if (!I->second->isExternal())
|
|
||||||
Callees.push_back(I->second);
|
|
||||||
std::sort(Callees.begin(), Callees.end());
|
|
||||||
|
|
||||||
std::map<std::vector<Function*>, DSGraph*>::iterator IndCallRecI =
|
|
||||||
IndCallMap.lower_bound(Callees);
|
|
||||||
|
|
||||||
DSGraph *IndCallGraph;
|
|
||||||
|
|
||||||
// If we already have this graph, recycle it.
|
|
||||||
if (IndCallRecI != IndCallMap.end() && IndCallRecI->first == Callees) {
|
|
||||||
DOUT << " [TD] *** Reuse of indcall graph for " << Callees.size()
|
|
||||||
<< " callees!\n";
|
|
||||||
IndCallGraph = IndCallRecI->second;
|
|
||||||
} else {
|
|
||||||
// Otherwise, create a new DSGraph to represent this.
|
|
||||||
IndCallGraph = new DSGraph(DSG.getGlobalECs(), DSG.getTargetData());
|
|
||||||
|
|
||||||
// Make a nullary dummy call site, which will eventually get some content
|
|
||||||
// merged into it. The actual callee function doesn't matter here, so we
|
|
||||||
// just pass it something to keep the ctor happy.
|
|
||||||
std::vector<DSNodeHandle> ArgDummyVec;
|
|
||||||
DSCallSite DummyCS(CI->getCallSite(), DSNodeHandle(), Callees[0]/*dummy*/,
|
|
||||||
ArgDummyVec);
|
|
||||||
IndCallGraph->getFunctionCalls().push_back(DummyCS);
|
|
||||||
|
|
||||||
IndCallRecI = IndCallMap.insert(IndCallRecI,
|
|
||||||
std::make_pair(Callees, IndCallGraph));
|
|
||||||
|
|
||||||
// Additionally, make sure that each of the callees inlines this graph
|
|
||||||
// exactly once.
|
|
||||||
DSCallSite *NCS = &IndCallGraph->getFunctionCalls().front();
|
|
||||||
for (unsigned i = 0, e = Callees.size(); i != e; ++i) {
|
|
||||||
DSGraph& CalleeGraph = getDSGraph(*Callees[i]);
|
|
||||||
if (&CalleeGraph != &DSG)
|
|
||||||
CallerEdges[&CalleeGraph].push_back(CallerCallEdge(IndCallGraph, NCS,
|
|
||||||
Callees[i]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now that we know which graph to use for this, merge the caller
|
|
||||||
// information into the graph, based on information from the call site.
|
|
||||||
ReachabilityCloner RC(*IndCallGraph, DSG, 0);
|
|
||||||
RC.mergeCallSite(IndCallGraph->getFunctionCalls().front(), *CI);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TDDataStructures::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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Function *F = dyn_cast<Function>(V)) {
|
|
||||||
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 TDDataStructures::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!");
|
|
||||||
DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs);
|
|
||||||
DSInfo[ToF] = NG;
|
|
||||||
assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
|
|
||||||
|
|
||||||
// Change the Function* is the returnnodes map to the ToF.
|
|
||||||
DSNodeHandle Ret = NG->retnodes_begin()->second;
|
|
||||||
NG->getReturnNodes().clear();
|
|
||||||
NG->getReturnNodes()[ToF] = Ret;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (const Function *F = getFnForValue(To)) {
|
|
||||||
DSGraph &G = getDSGraph(*F);
|
|
||||||
G.getScalarMap().copyScalarIfExists(From, To);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
DOUT << *From;
|
|
||||||
DOUT << *To;
|
|
||||||
assert(0 && "Do not know how to copy this yet!");
|
|
||||||
abort();
|
|
||||||
}
|
|
Reference in New Issue
Block a user