2009-06-02 17:52:33 +00:00
|
|
|
//===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the CallGraphSCCPass class, which is used for passes
|
|
|
|
// which are implemented as bottom-up traversals on the call graph. Because
|
|
|
|
// there may be cycles in the call graph, passes of this type operate on the
|
|
|
|
// call-graph in SCC order: that is, they process function bottom-up, except for
|
|
|
|
// recursive functions, which they process all at once.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-04-08 18:41:23 +00:00
|
|
|
#include "llvm/Analysis/CallGraphSCCPass.h"
|
2009-06-02 17:52:33 +00:00
|
|
|
#include "llvm/ADT/SCCIterator.h"
|
2010-05-04 16:11:02 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2013-04-08 18:41:23 +00:00
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2014-11-24 09:08:18 +00:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2015-01-18 16:17:27 +00:00
|
|
|
#include "llvm/IR/LegacyPassManagers.h"
|
2016-07-23 20:41:05 +00:00
|
|
|
#include "llvm/IR/OptBisect.h"
|
2010-05-04 16:11:02 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2009-10-14 17:57:32 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2010-04-02 08:54:30 +00:00
|
|
|
#include "llvm/Support/Timer.h"
|
2009-10-14 17:57:32 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-06-02 17:52:33 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
#define DEBUG_TYPE "cgscc-passmgr"
|
|
|
|
|
2010-05-04 16:11:02 +00:00
|
|
|
static cl::opt<unsigned>
|
|
|
|
MaxIterations("max-cg-scc-iterations", cl::ReallyHidden, cl::init(4));
|
|
|
|
|
|
|
|
STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC");
|
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CGPassManager
|
|
|
|
//
|
2009-10-14 17:57:32 +00:00
|
|
|
/// CGPassManager manages FPPassManagers and CallGraphSCCPasses.
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CGPassManager : public ModulePass, public PMDataManager {
|
|
|
|
public:
|
|
|
|
static char ID;
|
2011-10-20 21:10:27 +00:00
|
|
|
explicit CGPassManager()
|
|
|
|
: ModulePass(ID), PMDataManager() { }
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
/// Execute all of the passes scheduled for execution. Keep track of
|
2009-06-02 17:52:33 +00:00
|
|
|
/// whether any of the passes modifies the module, and if so, return true.
|
2014-11-24 09:08:18 +00:00
|
|
|
bool runOnModule(Module &M) override;
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2013-04-08 18:41:23 +00:00
|
|
|
using ModulePass::doInitialization;
|
|
|
|
using ModulePass::doFinalization;
|
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
bool doInitialization(CallGraph &CG);
|
|
|
|
bool doFinalization(CallGraph &CG);
|
|
|
|
|
|
|
|
/// Pass Manager itself does not invalidate any analysis info.
|
2014-11-24 09:08:18 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &Info) const override {
|
2009-06-02 17:52:33 +00:00
|
|
|
// CGPassManager walks SCC and it needs CallGraph.
|
2014-11-24 09:08:18 +00:00
|
|
|
Info.addRequired<CallGraphWrapperPass>();
|
2009-06-02 17:52:33 +00:00
|
|
|
Info.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
StringRef getPassName() const override { return "CallGraph Pass Manager"; }
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
PMDataManager *getAsPMDataManager() override { return this; }
|
|
|
|
Pass *getAsPass() override { return this; }
|
2010-01-23 11:09:33 +00:00
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
// Print passes managed by this manager
|
2014-11-24 09:08:18 +00:00
|
|
|
void dumpPassStructure(unsigned Offset) override {
|
2009-10-14 17:57:32 +00:00
|
|
|
errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n";
|
2009-06-02 17:52:33 +00:00
|
|
|
for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
|
|
|
|
Pass *P = getContainedPass(Index);
|
|
|
|
P->dumpPassStructure(Offset + 1);
|
|
|
|
dumpLastUses(P, Offset+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Pass *getContainedPass(unsigned N) {
|
2009-10-14 17:57:32 +00:00
|
|
|
assert(N < PassVector.size() && "Pass number out of range!");
|
|
|
|
return static_cast<Pass *>(PassVector[N]);
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
PassManagerType getPassManagerType() const override {
|
2009-06-02 17:52:33 +00:00
|
|
|
return PMT_CallGraphPassManager;
|
|
|
|
}
|
2009-10-14 17:57:32 +00:00
|
|
|
|
|
|
|
private:
|
2010-05-04 16:11:02 +00:00
|
|
|
bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
|
|
|
|
bool &DevirtualizedCall);
|
|
|
|
|
|
|
|
bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
|
|
|
|
CallGraph &CG, bool &CallGraphUpToDate,
|
|
|
|
bool &DevirtualizedCall);
|
2017-01-02 19:17:04 +00:00
|
|
|
bool RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG,
|
2009-10-14 17:57:32 +00:00
|
|
|
bool IsCheckingMode);
|
2009-06-02 17:52:33 +00:00
|
|
|
};
|
|
|
|
|
2009-10-14 17:57:32 +00:00
|
|
|
} // end anonymous namespace.
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
char CGPassManager::ID = 0;
|
2009-10-14 17:57:32 +00:00
|
|
|
|
2010-04-03 07:51:10 +00:00
|
|
|
|
2010-05-04 16:11:02 +00:00
|
|
|
bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
|
|
|
|
CallGraph &CG, bool &CallGraphUpToDate,
|
|
|
|
bool &DevirtualizedCall) {
|
2009-10-14 17:57:32 +00:00
|
|
|
bool Changed = false;
|
2010-01-23 11:09:33 +00:00
|
|
|
PMDataManager *PM = P->getAsPMDataManager();
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
if (!PM) {
|
2010-01-23 11:09:33 +00:00
|
|
|
CallGraphSCCPass *CGSP = (CallGraphSCCPass*)P;
|
2009-10-14 17:57:32 +00:00
|
|
|
if (!CallGraphUpToDate) {
|
2010-05-04 16:11:02 +00:00
|
|
|
DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
|
2009-10-14 17:57:32 +00:00
|
|
|
CallGraphUpToDate = true;
|
|
|
|
}
|
|
|
|
|
2010-04-02 08:54:30 +00:00
|
|
|
{
|
|
|
|
TimeRegion PassTimer(getPassTimer(CGSP));
|
|
|
|
Changed = CGSP->runOnSCC(CurSCC);
|
|
|
|
}
|
2009-10-14 17:57:32 +00:00
|
|
|
|
|
|
|
// After the CGSCCPass is done, when assertions are enabled, use
|
|
|
|
// RefreshCallGraph to verify that the callgraph was correctly updated.
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (Changed)
|
|
|
|
RefreshCallGraph(CurSCC, CG, true);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2010-01-23 11:09:33 +00:00
|
|
|
|
|
|
|
assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
|
|
|
|
"Invalid CGPassManager member");
|
|
|
|
FPPassManager *FPP = (FPPassManager*)P;
|
2009-10-14 17:57:32 +00:00
|
|
|
|
|
|
|
// Run pass P on all functions in the current SCC.
|
2015-05-27 18:44:32 +00:00
|
|
|
for (CallGraphNode *CGN : CurSCC) {
|
|
|
|
if (Function *F = CGN->getFunction()) {
|
2009-10-14 17:57:32 +00:00
|
|
|
dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
|
2014-11-24 09:08:18 +00:00
|
|
|
{
|
|
|
|
TimeRegion PassTimer(getPassTimer(FPP));
|
|
|
|
Changed |= FPP->runOnFunction(*F);
|
|
|
|
}
|
|
|
|
F->getContext().yield();
|
2009-10-14 17:57:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The function pass(es) modified the IR, they may have clobbered the
|
|
|
|
// callgraph.
|
|
|
|
if (Changed && CallGraphUpToDate) {
|
2010-01-01 10:31:22 +00:00
|
|
|
DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: "
|
2009-10-14 17:57:32 +00:00
|
|
|
<< P->getPassName() << '\n');
|
|
|
|
CallGraphUpToDate = false;
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
/// Scan the functions in the specified CFG and resync the
|
2009-10-14 17:57:32 +00:00
|
|
|
/// callgraph with the call sites found in it. This is used after
|
|
|
|
/// FunctionPasses have potentially munged the callgraph, and can be used after
|
|
|
|
/// CallGraphSCC passes to verify that they correctly updated the callgraph.
|
|
|
|
///
|
2010-05-04 16:11:02 +00:00
|
|
|
/// This function returns true if it devirtualized an existing function call,
|
|
|
|
/// meaning it turned an indirect call into a direct call. This happens when
|
|
|
|
/// a function pass like GVN optimizes away stuff feeding the indirect call.
|
|
|
|
/// This never happens in checking mode.
|
|
|
|
///
|
2017-01-02 19:17:04 +00:00
|
|
|
bool CGPassManager::RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG,
|
|
|
|
bool CheckingMode) {
|
2009-10-14 17:57:32 +00:00
|
|
|
DenseMap<Value*, CallGraphNode*> CallSites;
|
|
|
|
|
2010-01-01 10:31:22 +00:00
|
|
|
DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size()
|
2009-10-14 17:57:32 +00:00
|
|
|
<< " nodes:\n";
|
2015-05-27 18:44:32 +00:00
|
|
|
for (CallGraphNode *CGN : CurSCC)
|
|
|
|
CGN->dump();
|
2009-10-14 17:57:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
bool MadeChange = false;
|
2010-05-04 16:11:02 +00:00
|
|
|
bool DevirtualizedCall = false;
|
2009-10-14 17:57:32 +00:00
|
|
|
|
|
|
|
// Scan all functions in the SCC.
|
2010-05-04 16:11:02 +00:00
|
|
|
unsigned FunctionNo = 0;
|
|
|
|
for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end();
|
|
|
|
SCCIdx != E; ++SCCIdx, ++FunctionNo) {
|
|
|
|
CallGraphNode *CGN = *SCCIdx;
|
2009-10-14 17:57:32 +00:00
|
|
|
Function *F = CGN->getFunction();
|
2014-11-24 09:08:18 +00:00
|
|
|
if (!F || F->isDeclaration()) continue;
|
2009-10-14 17:57:32 +00:00
|
|
|
|
|
|
|
// Walk the function body looking for call sites. Sync up the call sites in
|
|
|
|
// CGN with those actually in the function.
|
2010-05-04 16:11:02 +00:00
|
|
|
|
|
|
|
// Keep track of the number of direct and indirect calls that were
|
|
|
|
// invalidated and removed.
|
|
|
|
unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0;
|
2009-10-14 17:57:32 +00:00
|
|
|
|
|
|
|
// Get the set of call sites currently in the function.
|
|
|
|
for (CallGraphNode::iterator I = CGN->begin(), E = CGN->end(); I != E; ) {
|
|
|
|
// If this call site is null, then the function pass deleted the call
|
|
|
|
// entirely and the WeakVH nulled it out.
|
2014-11-24 09:08:18 +00:00
|
|
|
if (!I->first ||
|
2009-10-14 17:57:32 +00:00
|
|
|
// If we've already seen this call site, then the FunctionPass RAUW'd
|
|
|
|
// one call with another, which resulted in two "uses" in the edge
|
|
|
|
// list of the same call.
|
|
|
|
CallSites.count(I->first) ||
|
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
// If the call edge is not from a call or invoke, or it is a
|
|
|
|
// instrinsic call, then the function pass RAUW'd a call with
|
|
|
|
// another value. This can happen when constant folding happens
|
|
|
|
// of well known functions etc.
|
|
|
|
!CallSite(I->first) ||
|
2015-06-21 13:59:01 +00:00
|
|
|
(CallSite(I->first).getCalledFunction() &&
|
|
|
|
CallSite(I->first).getCalledFunction()->isIntrinsic() &&
|
|
|
|
Intrinsic::isLeaf(
|
|
|
|
CallSite(I->first).getCalledFunction()->getIntrinsicID()))) {
|
2009-10-14 17:57:32 +00:00
|
|
|
assert(!CheckingMode &&
|
|
|
|
"CallGraphSCCPass did not update the CallGraph correctly!");
|
|
|
|
|
2010-05-04 16:11:02 +00:00
|
|
|
// If this was an indirect call site, count it.
|
2014-11-24 09:08:18 +00:00
|
|
|
if (!I->second->getFunction())
|
2010-05-04 16:11:02 +00:00
|
|
|
++NumIndirectRemoved;
|
|
|
|
else
|
|
|
|
++NumDirectRemoved;
|
|
|
|
|
2009-10-14 17:57:32 +00:00
|
|
|
// Just remove the edge from the set of callees, keep track of whether
|
|
|
|
// I points to the last element of the vector.
|
|
|
|
bool WasLast = I + 1 == E;
|
|
|
|
CGN->removeCallEdge(I);
|
|
|
|
|
|
|
|
// If I pointed to the last element of the vector, we have to bail out:
|
|
|
|
// iterator checking rejects comparisons of the resultant pointer with
|
|
|
|
// end.
|
|
|
|
if (WasLast)
|
|
|
|
break;
|
|
|
|
E = CGN->end();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!CallSites.count(I->first) &&
|
|
|
|
"Call site occurs in node multiple times");
|
2015-01-18 16:17:27 +00:00
|
|
|
|
|
|
|
CallSite CS(I->first);
|
|
|
|
if (CS) {
|
|
|
|
Function *Callee = CS.getCalledFunction();
|
|
|
|
// Ignore intrinsics because they're not really function calls.
|
|
|
|
if (!Callee || !(Callee->isIntrinsic()))
|
|
|
|
CallSites.insert(std::make_pair(I->first, I->second));
|
|
|
|
}
|
2009-10-14 17:57:32 +00:00
|
|
|
++I;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop over all of the instructions in the function, getting the callsites.
|
2010-05-04 16:11:02 +00:00
|
|
|
// Keep track of the number of direct/indirect calls added.
|
|
|
|
unsigned NumDirectAdded = 0, NumIndirectAdded = 0;
|
2016-07-23 20:41:05 +00:00
|
|
|
|
|
|
|
for (BasicBlock &BB : *F)
|
|
|
|
for (Instruction &I : BB) {
|
|
|
|
CallSite CS(&I);
|
2012-08-15 19:34:23 +00:00
|
|
|
if (!CS) continue;
|
|
|
|
Function *Callee = CS.getCalledFunction();
|
|
|
|
if (Callee && Callee->isIntrinsic()) continue;
|
2009-10-14 17:57:32 +00:00
|
|
|
|
|
|
|
// If this call site already existed in the callgraph, just verify it
|
|
|
|
// matches up to expectations and remove it from CallSites.
|
|
|
|
DenseMap<Value*, CallGraphNode*>::iterator ExistingIt =
|
|
|
|
CallSites.find(CS.getInstruction());
|
|
|
|
if (ExistingIt != CallSites.end()) {
|
|
|
|
CallGraphNode *ExistingNode = ExistingIt->second;
|
|
|
|
|
|
|
|
// Remove from CallSites since we have now seen it.
|
|
|
|
CallSites.erase(ExistingIt);
|
|
|
|
|
|
|
|
// Verify that the callee is right.
|
|
|
|
if (ExistingNode->getFunction() == CS.getCalledFunction())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If we are in checking mode, we are not allowed to actually mutate
|
|
|
|
// the callgraph. If this is a case where we can infer that the
|
|
|
|
// callgraph is less precise than it could be (e.g. an indirect call
|
|
|
|
// site could be turned direct), don't reject it in checking mode, and
|
|
|
|
// don't tweak it to be more precise.
|
|
|
|
if (CheckingMode && CS.getCalledFunction() &&
|
2014-11-24 09:08:18 +00:00
|
|
|
ExistingNode->getFunction() == nullptr)
|
2009-10-14 17:57:32 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
assert(!CheckingMode &&
|
|
|
|
"CallGraphSCCPass did not update the CallGraph correctly!");
|
|
|
|
|
|
|
|
// If not, we either went from a direct call to indirect, indirect to
|
|
|
|
// direct, or direct to different direct.
|
|
|
|
CallGraphNode *CalleeNode;
|
2010-05-04 16:11:02 +00:00
|
|
|
if (Function *Callee = CS.getCalledFunction()) {
|
2009-10-14 17:57:32 +00:00
|
|
|
CalleeNode = CG.getOrInsertFunction(Callee);
|
2010-05-04 16:11:02 +00:00
|
|
|
// Keep track of whether we turned an indirect call into a direct
|
|
|
|
// one.
|
2014-11-24 09:08:18 +00:00
|
|
|
if (!ExistingNode->getFunction()) {
|
2010-05-04 16:11:02 +00:00
|
|
|
DevirtualizedCall = true;
|
|
|
|
DEBUG(dbgs() << " CGSCCPASSMGR: Devirtualized call to '"
|
|
|
|
<< Callee->getName() << "'\n");
|
|
|
|
}
|
|
|
|
} else {
|
2009-10-14 17:57:32 +00:00
|
|
|
CalleeNode = CG.getCallsExternalNode();
|
2010-05-04 16:11:02 +00:00
|
|
|
}
|
2009-10-14 17:57:32 +00:00
|
|
|
|
|
|
|
// Update the edge target in CGN.
|
2010-05-04 16:11:02 +00:00
|
|
|
CGN->replaceCallEdge(CS, CS, CalleeNode);
|
2009-10-14 17:57:32 +00:00
|
|
|
MadeChange = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!CheckingMode &&
|
|
|
|
"CallGraphSCCPass did not update the CallGraph correctly!");
|
|
|
|
|
2010-05-04 16:11:02 +00:00
|
|
|
// If the call site didn't exist in the CGN yet, add it.
|
2009-10-14 17:57:32 +00:00
|
|
|
CallGraphNode *CalleeNode;
|
2010-05-04 16:11:02 +00:00
|
|
|
if (Function *Callee = CS.getCalledFunction()) {
|
2009-10-14 17:57:32 +00:00
|
|
|
CalleeNode = CG.getOrInsertFunction(Callee);
|
2010-05-04 16:11:02 +00:00
|
|
|
++NumDirectAdded;
|
|
|
|
} else {
|
2009-10-14 17:57:32 +00:00
|
|
|
CalleeNode = CG.getCallsExternalNode();
|
2010-05-04 16:11:02 +00:00
|
|
|
++NumIndirectAdded;
|
|
|
|
}
|
2009-10-14 17:57:32 +00:00
|
|
|
|
|
|
|
CGN->addCalledFunction(CS, CalleeNode);
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
|
2010-05-04 16:11:02 +00:00
|
|
|
// We scanned the old callgraph node, removing invalidated call sites and
|
|
|
|
// then added back newly found call sites. One thing that can happen is
|
|
|
|
// that an old indirect call site was deleted and replaced with a new direct
|
|
|
|
// call. In this case, we have devirtualized a call, and CGSCCPM would like
|
|
|
|
// to iteratively optimize the new code. Unfortunately, we don't really
|
|
|
|
// have a great way to detect when this happens. As an approximation, we
|
|
|
|
// just look at whether the number of indirect calls is reduced and the
|
|
|
|
// number of direct calls is increased. There are tons of ways to fool this
|
|
|
|
// (e.g. DCE'ing an indirect call and duplicating an unrelated block with a
|
|
|
|
// direct call) but this is close enough.
|
|
|
|
if (NumIndirectRemoved > NumIndirectAdded &&
|
|
|
|
NumDirectRemoved < NumDirectAdded)
|
|
|
|
DevirtualizedCall = true;
|
|
|
|
|
2009-10-14 17:57:32 +00:00
|
|
|
// After scanning this function, if we still have entries in callsites, then
|
|
|
|
// they are dangling pointers. WeakVH should save us for this, so abort if
|
|
|
|
// this happens.
|
|
|
|
assert(CallSites.empty() && "Dangling pointers found in call sites map");
|
|
|
|
|
|
|
|
// Periodically do an explicit clear to remove tombstones when processing
|
|
|
|
// large scc's.
|
2010-05-04 16:11:02 +00:00
|
|
|
if ((FunctionNo & 15) == 15)
|
2009-10-14 17:57:32 +00:00
|
|
|
CallSites.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(if (MadeChange) {
|
2010-01-01 10:31:22 +00:00
|
|
|
dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n";
|
2015-05-27 18:44:32 +00:00
|
|
|
for (CallGraphNode *CGN : CurSCC)
|
|
|
|
CGN->dump();
|
2010-05-04 16:11:02 +00:00
|
|
|
if (DevirtualizedCall)
|
|
|
|
dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n";
|
|
|
|
|
2009-10-14 17:57:32 +00:00
|
|
|
} else {
|
2010-01-01 10:31:22 +00:00
|
|
|
dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n";
|
2009-10-14 17:57:32 +00:00
|
|
|
}
|
|
|
|
);
|
2011-10-20 21:10:27 +00:00
|
|
|
(void)MadeChange;
|
2010-05-04 16:11:02 +00:00
|
|
|
|
|
|
|
return DevirtualizedCall;
|
|
|
|
}
|
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
/// Execute the body of the entire pass manager on the specified SCC.
|
|
|
|
/// This keeps track of whether a function pass devirtualizes
|
2010-05-04 16:11:02 +00:00
|
|
|
/// any calls and returns it in DevirtualizedCall.
|
|
|
|
bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
|
|
|
|
bool &DevirtualizedCall) {
|
|
|
|
bool Changed = false;
|
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
// Keep track of whether the callgraph is known to be up-to-date or not.
|
|
|
|
// The CGSSC pass manager runs two types of passes:
|
2010-05-04 16:11:02 +00:00
|
|
|
// CallGraphSCC Passes and other random function passes. Because other
|
|
|
|
// random function passes are not CallGraph aware, they may clobber the
|
|
|
|
// call graph by introducing new calls or deleting other ones. This flag
|
|
|
|
// is set to false when we run a function pass so that we know to clean up
|
|
|
|
// the callgraph when we need to run a CGSCCPass again.
|
|
|
|
bool CallGraphUpToDate = true;
|
|
|
|
|
|
|
|
// Run all passes on current SCC.
|
|
|
|
for (unsigned PassNo = 0, e = getNumContainedPasses();
|
|
|
|
PassNo != e; ++PassNo) {
|
|
|
|
Pass *P = getContainedPass(PassNo);
|
|
|
|
|
|
|
|
// If we're in -debug-pass=Executions mode, construct the SCC node list,
|
|
|
|
// otherwise avoid constructing this string as it is expensive.
|
|
|
|
if (isPassDebuggingExecutionsOrMore()) {
|
|
|
|
std::string Functions;
|
|
|
|
#ifndef NDEBUG
|
|
|
|
raw_string_ostream OS(Functions);
|
|
|
|
for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (I != CurSCC.begin()) OS << ", ";
|
|
|
|
(*I)->print(OS);
|
|
|
|
}
|
|
|
|
OS.flush();
|
|
|
|
#endif
|
|
|
|
dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions);
|
|
|
|
}
|
|
|
|
dumpRequiredSet(P);
|
|
|
|
|
|
|
|
initializeAnalysisImpl(P);
|
|
|
|
|
|
|
|
// Actually run this pass on the current SCC.
|
|
|
|
Changed |= RunPassOnSCC(P, CurSCC, CG,
|
|
|
|
CallGraphUpToDate, DevirtualizedCall);
|
|
|
|
|
|
|
|
if (Changed)
|
|
|
|
dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
|
|
|
|
dumpPreservedSet(P);
|
|
|
|
|
|
|
|
verifyPreservedAnalysis(P);
|
|
|
|
removeNotPreservedAnalysis(P);
|
|
|
|
recordAvailableAnalysis(P);
|
|
|
|
removeDeadPasses(P, "", ON_CG_MSG);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the callgraph was left out of date (because the last pass run was a
|
|
|
|
// functionpass), refresh it before we move on to the next SCC.
|
|
|
|
if (!CallGraphUpToDate)
|
|
|
|
DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
|
|
|
|
return Changed;
|
2009-10-14 17:57:32 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
/// Execute all of the passes scheduled for execution. Keep track of
|
2009-06-02 17:52:33 +00:00
|
|
|
/// whether any of the passes modifies the module, and if so, return true.
|
|
|
|
bool CGPassManager::runOnModule(Module &M) {
|
2014-11-24 09:08:18 +00:00
|
|
|
CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
|
2009-06-02 17:52:33 +00:00
|
|
|
bool Changed = doInitialization(CG);
|
2009-10-14 17:57:32 +00:00
|
|
|
|
|
|
|
// Walk the callgraph in bottom-up SCC order.
|
2010-05-04 16:11:02 +00:00
|
|
|
scc_iterator<CallGraph*> CGI = scc_begin(&CG);
|
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
CallGraphSCC CurSCC(CG, &CGI);
|
2010-05-04 16:11:02 +00:00
|
|
|
while (!CGI.isAtEnd()) {
|
2009-10-14 17:57:32 +00:00
|
|
|
// Copy the current SCC and increment past it so that the pass can hack
|
|
|
|
// on the SCC if it wants to without invalidating our iterator.
|
2014-11-24 09:08:18 +00:00
|
|
|
const std::vector<CallGraphNode *> &NodeVec = *CGI;
|
2017-01-02 19:17:04 +00:00
|
|
|
CurSCC.initialize(NodeVec);
|
2009-10-14 17:57:32 +00:00
|
|
|
++CGI;
|
2015-07-05 14:21:36 +00:00
|
|
|
|
2010-05-04 16:11:02 +00:00
|
|
|
// At the top level, we run all the passes in this pass manager on the
|
|
|
|
// functions in this SCC. However, we support iterative compilation in the
|
|
|
|
// case where a function pass devirtualizes a call to a function. For
|
|
|
|
// example, it is very common for a function pass (often GVN or instcombine)
|
|
|
|
// to eliminate the addressing that feeds into a call. With that improved
|
|
|
|
// information, we would like the call to be an inline candidate, infer
|
|
|
|
// mod-ref information etc.
|
|
|
|
//
|
|
|
|
// Because of this, we allow iteration up to a specified iteration count.
|
|
|
|
// This only happens in the case of a devirtualized call, so we only burn
|
|
|
|
// compile time in the case that we're making progress. We also have a hard
|
|
|
|
// iteration count limit in case there is crazy code.
|
|
|
|
unsigned Iteration = 0;
|
|
|
|
bool DevirtualizedCall = false;
|
|
|
|
do {
|
|
|
|
DEBUG(if (Iteration)
|
|
|
|
dbgs() << " SCCPASSMGR: Re-visiting SCC, iteration #"
|
|
|
|
<< Iteration << '\n');
|
|
|
|
DevirtualizedCall = false;
|
|
|
|
Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall);
|
|
|
|
} while (Iteration++ < MaxIterations && DevirtualizedCall);
|
2009-10-14 17:57:32 +00:00
|
|
|
|
2010-05-04 16:11:02 +00:00
|
|
|
if (DevirtualizedCall)
|
|
|
|
DEBUG(dbgs() << " CGSCCPASSMGR: Stopped iteration after " << Iteration
|
|
|
|
<< " times, due to -max-cg-scc-iterations\n");
|
2009-10-14 17:57:32 +00:00
|
|
|
|
2010-05-04 16:11:02 +00:00
|
|
|
if (Iteration > MaxSCCIterations)
|
|
|
|
MaxSCCIterations = Iteration;
|
2009-10-14 17:57:32 +00:00
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
Changed |= doFinalization(CG);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2010-05-04 16:11:02 +00:00
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
/// Initialize CG
|
|
|
|
bool CGPassManager::doInitialization(CallGraph &CG) {
|
|
|
|
bool Changed = false;
|
2010-01-23 11:09:33 +00:00
|
|
|
for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
|
|
|
|
if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
|
|
|
|
assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
|
|
|
|
"Invalid CGPassManager member");
|
|
|
|
Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule());
|
2009-06-02 17:52:33 +00:00
|
|
|
} else {
|
2010-01-23 11:09:33 +00:00
|
|
|
Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG);
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Finalize CG
|
|
|
|
bool CGPassManager::doFinalization(CallGraph &CG) {
|
|
|
|
bool Changed = false;
|
2010-01-23 11:09:33 +00:00
|
|
|
for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
|
|
|
|
if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
|
|
|
|
assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
|
|
|
|
"Invalid CGPassManager member");
|
|
|
|
Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule());
|
2009-06-02 17:52:33 +00:00
|
|
|
} else {
|
2010-01-23 11:09:33 +00:00
|
|
|
Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG);
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2010-05-04 16:11:02 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CallGraphSCC Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
/// This informs the SCC and the pass manager that the specified
|
2010-05-04 16:11:02 +00:00
|
|
|
/// Old node has been deleted, and New is to be used in its place.
|
|
|
|
void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) {
|
|
|
|
assert(Old != New && "Should not replace node with self");
|
|
|
|
for (unsigned i = 0; ; ++i) {
|
|
|
|
assert(i != Nodes.size() && "Node not in SCC");
|
|
|
|
if (Nodes[i] != Old) continue;
|
|
|
|
Nodes[i] = New;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the active scc_iterator so that it doesn't contain dangling
|
|
|
|
// pointers to the old CallGraphNode.
|
|
|
|
scc_iterator<CallGraph*> *CGI = (scc_iterator<CallGraph*>*)Context;
|
|
|
|
CGI->ReplaceNode(Old, New);
|
2010-04-03 07:51:10 +00:00
|
|
|
}
|
|
|
|
|
2010-05-04 16:11:02 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CallGraphSCCPass Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
/// Assign pass manager to manage this pass.
|
|
|
|
void CallGraphSCCPass::assignPassManager(PMStack &PMS,
|
|
|
|
PassManagerType PreferredType) {
|
|
|
|
// Find CGPassManager
|
|
|
|
while (!PMS.empty() &&
|
|
|
|
PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
|
|
|
|
PMS.pop();
|
|
|
|
|
2010-01-23 11:09:33 +00:00
|
|
|
assert(!PMS.empty() && "Unable to handle Call Graph Pass");
|
|
|
|
CGPassManager *CGP;
|
|
|
|
|
|
|
|
if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager)
|
|
|
|
CGP = (CGPassManager*)PMS.top();
|
|
|
|
else {
|
|
|
|
// Create new Call Graph SCC Pass Manager if it does not exist.
|
|
|
|
assert(!PMS.empty() && "Unable to create Call Graph Pass Manager");
|
2009-06-02 17:52:33 +00:00
|
|
|
PMDataManager *PMD = PMS.top();
|
|
|
|
|
|
|
|
// [1] Create new Call Graph Pass Manager
|
2011-10-20 21:10:27 +00:00
|
|
|
CGP = new CGPassManager();
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
// [2] Set up new manager's top level manager
|
|
|
|
PMTopLevelManager *TPM = PMD->getTopLevelManager();
|
|
|
|
TPM->addIndirectPassManager(CGP);
|
|
|
|
|
|
|
|
// [3] Assign manager to manage this new manager. This may create
|
|
|
|
// and push new managers into PMS
|
2010-01-23 11:09:33 +00:00
|
|
|
Pass *P = CGP;
|
2009-06-02 17:52:33 +00:00
|
|
|
TPM->schedulePass(P);
|
|
|
|
|
|
|
|
// [4] Push new manager into PMS
|
|
|
|
PMS.push(CGP);
|
|
|
|
}
|
|
|
|
|
|
|
|
CGP->add(this);
|
|
|
|
}
|
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
/// For this class, we declare that we require and preserve the call graph.
|
|
|
|
/// If the derived class implements this method, it should
|
2009-06-02 17:52:33 +00:00
|
|
|
/// always explicitly call the implementation here.
|
|
|
|
void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
|
2014-11-24 09:08:18 +00:00
|
|
|
AU.addRequired<CallGraphWrapperPass>();
|
|
|
|
AU.addPreserved<CallGraphWrapperPass>();
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
2010-05-04 16:11:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PrintCallGraphPass Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// PrintCallGraphPass - Print a Module corresponding to a call graph.
|
|
|
|
///
|
|
|
|
class PrintCallGraphPass : public CallGraphSCCPass {
|
|
|
|
std::string Banner;
|
|
|
|
raw_ostream &Out; // raw_ostream to print on.
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
PrintCallGraphPass(const std::string &B, raw_ostream &o)
|
2010-09-17 15:48:55 +00:00
|
|
|
: CallGraphSCCPass(ID), Banner(B), Out(o) {}
|
2014-11-24 09:08:18 +00:00
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2010-05-04 16:11:02 +00:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2014-11-24 09:08:18 +00:00
|
|
|
|
|
|
|
bool runOnSCC(CallGraphSCC &SCC) override {
|
2010-05-04 16:11:02 +00:00
|
|
|
Out << Banner;
|
2015-05-27 18:44:32 +00:00
|
|
|
for (CallGraphNode *CGN : SCC) {
|
2016-01-13 19:58:01 +00:00
|
|
|
if (CGN->getFunction()) {
|
|
|
|
if (isFunctionInPrintList(CGN->getFunction()->getName()))
|
|
|
|
CGN->getFunction()->print(Out);
|
|
|
|
} else
|
2014-11-24 09:08:18 +00:00
|
|
|
Out << "\nPrinting <null> Function\n";
|
|
|
|
}
|
2010-05-04 16:11:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace.
|
|
|
|
|
|
|
|
char PrintCallGraphPass::ID = 0;
|
|
|
|
|
|
|
|
Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &O,
|
|
|
|
const std::string &Banner) const {
|
|
|
|
return new PrintCallGraphPass(Banner, O);
|
|
|
|
}
|
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
bool CallGraphSCCPass::skipSCC(CallGraphSCC &SCC) const {
|
|
|
|
return !SCC.getCallGraph().getModule()
|
|
|
|
.getContext()
|
|
|
|
.getOptBisect()
|
|
|
|
.shouldRunPass(this, SCC);
|
|
|
|
}
|
|
|
|
|
|
|
|
char DummyCGSCCPass::ID = 0;
|
|
|
|
INITIALIZE_PASS(DummyCGSCCPass, "DummyCGSCCPass", "DummyCGSCCPass", false,
|
|
|
|
false)
|