Skip to content

Commit

Permalink
Remove call graph maintenance in dead function elimination.
Browse files Browse the repository at this point in the history
  • Loading branch information
rudkx committed Nov 24, 2015
1 parent 7b5e2fd commit 40a89cb
Showing 1 changed file with 12 additions and 25 deletions.
37 changes: 12 additions & 25 deletions lib/SILPasses/IPO/DeadFunctionElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
//===----------------------------------------------------------------------===//

#define DEBUG_TYPE "sil-dead-function-elimination"
#include "swift/SILAnalysis/CallGraphAnalysis.h"
#include "swift/SILPasses/Passes.h"
#include "swift/SILPasses/Transforms.h"
#include "swift/SIL/PatternMatch.h"
Expand Down Expand Up @@ -51,7 +50,6 @@ class FunctionLivenessComputation {
};

SILModule *Module;
CallGraphAnalysis *CGA;

llvm::DenseMap<AbstractFunctionDecl *, MethodInfo *> MethodInfos;
llvm::SpecificBumpPtrAllocator<MethodInfo> MethodInfoAllocator;
Expand Down Expand Up @@ -251,8 +249,8 @@ class FunctionLivenessComputation {
}

public:
FunctionLivenessComputation(SILModule *module, CallGraphAnalysis *CGA) :
Module(module), CGA(CGA) {}
FunctionLivenessComputation(SILModule *module) :
Module(module) {}

/// The main entry point of the optimization.
bool findAliveFunctions() {
Expand Down Expand Up @@ -373,8 +371,8 @@ class DeadFunctionElimination : FunctionLivenessComputation {
}

public:
DeadFunctionElimination(SILModule *module, CallGraphAnalysis *CGA)
: FunctionLivenessComputation(module, CGA) {}
DeadFunctionElimination(SILModule *module)
: FunctionLivenessComputation(module) {}

/// The main entry point of the optimization.
void eliminateFunctions(SILModuleTransform *DFEPass) {
Expand All @@ -384,13 +382,11 @@ class DeadFunctionElimination : FunctionLivenessComputation {

removeDeadEntriesFromTables();

CallGraph *CG = CGA->getCallGraphOrNull();

// First drop all references so that we don't get problems with non-zero
// reference counts of dead functions.
for (SILFunction &F : *Module)
if (!isAlive(&F))
CallGraphEditor(CG).dropAllReferences(&F);
F.dropAllReferences();

// Next step: delete all dead functions.
bool NeedUpdate = false;
Expand All @@ -400,11 +396,9 @@ class DeadFunctionElimination : FunctionLivenessComputation {
if (!isAlive(F)) {
DEBUG(llvm::dbgs() << " erase dead function " << F->getName() << "\n");
NumDeadFunc++;
CallGraphEditor(CG).eraseFunction(F);
Module->eraseFunction(F);
NeedUpdate = true;
CGA->lockInvalidation();
DFEPass->invalidateAnalysis(F, SILAnalysis::InvalidationKind::Everything);
CGA->unlockInvalidation();
}
}
}
Expand Down Expand Up @@ -475,22 +469,21 @@ class ExternalFunctionDefinitionsElimination : FunctionLivenessComputation {

DEBUG(llvm::dbgs() << " removed external function " << F->getName()
<< "\n");
CallGraph *CG = CGA->getCallGraphOrNull();
CallGraphEditor(CG).dropAllReferences(F);
F->dropAllReferences();
auto &Blocks = F->getBlocks();
Blocks.clear();
assert(F->isExternalDeclaration() &&
"Function should be an external declaration");
if (F->getRefCount() == 0)
CallGraphEditor(CG).eraseFunction(F);
F->getModule().eraseFunction(F);

NumEliminatedExternalDefs++;
return true;
}

public:
ExternalFunctionDefinitionsElimination(SILModule *module, CallGraphAnalysis *CGA)
: FunctionLivenessComputation(module, CGA) {}
ExternalFunctionDefinitionsElimination(SILModule *module)
: FunctionLivenessComputation(module) {}

/// Eliminate bodies of external functions which are not alive.
///
Expand All @@ -509,10 +502,8 @@ class ExternalFunctionDefinitionsElimination : FunctionLivenessComputation {
if (!isAlive(F)) {
if (tryToConvertExternalDefinitionIntoDeclaration(F)) {
NeedUpdate = true;
CGA->lockInvalidation();
DFEPass->invalidateAnalysis(F,
SILAnalysis::InvalidationKind::Everything);
CGA->unlockInvalidation();
}
}
}
Expand All @@ -529,8 +520,6 @@ namespace {

class SILDeadFuncElimination : public SILModuleTransform {
void run() override {
auto *CGA = getAnalysis<CallGraphAnalysis>();

DEBUG(llvm::dbgs() << "Running DeadFuncElimination\n");

// The deserializer caches functions that it deserializes so that if it is
Expand All @@ -540,7 +529,7 @@ class SILDeadFuncElimination : public SILModuleTransform {
// can eliminate such functions.
getModule()->invalidateSILLoaderCaches();

DeadFunctionElimination deadFunctionElimination(getModule(), CGA);
DeadFunctionElimination deadFunctionElimination(getModule());
deadFunctionElimination.eliminateFunctions(this);
}

Expand All @@ -549,8 +538,6 @@ class SILDeadFuncElimination : public SILModuleTransform {

class SILExternalFuncDefinitionsElimination : public SILModuleTransform {
void run() override {
auto *CGA = getAnalysis<CallGraphAnalysis>();

DEBUG(llvm::dbgs() << "Running ExternalFunctionDefinitionsElimination\n");

// The deserializer caches functions that it deserializes so that if it is
Expand All @@ -560,7 +547,7 @@ class SILExternalFuncDefinitionsElimination : public SILModuleTransform {
// can eliminate the definitions of such functions.
getModule()->invalidateSILLoaderCaches();

ExternalFunctionDefinitionsElimination EFDFE(getModule(), CGA);
ExternalFunctionDefinitionsElimination EFDFE(getModule());
EFDFE.eliminateFunctions(this);
}

Expand Down

0 comments on commit 40a89cb

Please sign in to comment.