1
- // ===--- DeadFunctionElimination.cpp - Eliminate dead functions ---------- -===//
1
+ // ===- DeadFunctionElimination.cpp - Eliminate dead functions and globals -===//
2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
@@ -31,12 +31,7 @@ STATISTIC(NumDeadGlobals, "Number of dead global variables eliminated");
31
31
32
32
namespace {
33
33
34
- // / This is a base class for passes that are based on function liveness
35
- // / computations like e.g. dead function elimination.
36
- // / It provides a common logic for computing live (i.e. reachable) functions.
37
- class FunctionLivenessComputation {
38
- protected:
39
-
34
+ class DeadFunctionAndGlobalElimination {
40
35
// / Represents a function which is implementing a vtable or witness table
41
36
// / method.
42
37
struct FuncImpl {
@@ -402,9 +397,6 @@ class FunctionLivenessComputation {
402
397
return false ;
403
398
}
404
399
405
- // / Find anchors in vtables and witness tables, if required.
406
- virtual void findAnchorsInTables () = 0;
407
-
408
400
// / Find all functions which are alive from the beginning.
409
401
// / For example, functions which may be referenced externally.
410
402
void findAnchors () {
@@ -435,12 +427,6 @@ class FunctionLivenessComputation {
435
427
}
436
428
}
437
429
438
- public:
439
- FunctionLivenessComputation (SILModule *module ,
440
- bool keepExternalWitnessTablesAlive) :
441
- Module (module ),
442
- keepExternalWitnessTablesAlive (keepExternalWitnessTablesAlive) {}
443
-
444
430
// / The main entry point of the optimization.
445
431
bool findAliveFunctions () {
446
432
@@ -461,19 +447,6 @@ class FunctionLivenessComputation {
461
447
return false ;
462
448
}
463
449
464
- virtual ~FunctionLivenessComputation () {}
465
- };
466
-
467
- } // end anonymous namespace
468
-
469
- // ===----------------------------------------------------------------------===//
470
- // DeadFunctionElimination
471
- // ===----------------------------------------------------------------------===//
472
-
473
- namespace {
474
-
475
- class DeadFunctionElimination : FunctionLivenessComputation {
476
-
477
450
void collectMethodImplementations () {
478
451
// Collect vtable method implementations.
479
452
for (auto &vTable : Module->getVTables ()) {
@@ -528,10 +501,9 @@ class DeadFunctionElimination : FunctionLivenessComputation {
528
501
}
529
502
}
530
503
531
- // / DeadFunctionElimination pass takes functions
532
- // / reachable via vtables and witness_tables into account
504
+ // / Take functions reachable via vtables and witness_tables into account
533
505
// / when computing a function liveness information.
534
- void findAnchorsInTables () override {
506
+ void findAnchorsInTables () {
535
507
536
508
collectMethodImplementations ();
537
509
@@ -688,11 +660,13 @@ class DeadFunctionElimination : FunctionLivenessComputation {
688
660
}
689
661
690
662
public:
691
- DeadFunctionElimination (SILModule *module , bool keepExternalWitnessTablesAlive)
692
- : FunctionLivenessComputation(module , keepExternalWitnessTablesAlive) {}
663
+ DeadFunctionAndGlobalElimination (SILModule *module ,
664
+ bool keepExternalWitnessTablesAlive) :
665
+ Module (module ),
666
+ keepExternalWitnessTablesAlive (keepExternalWitnessTablesAlive) {}
693
667
694
668
// / The main entry point of the optimization.
695
- void eliminateFunctions (SILModuleTransform *DFEPass) {
669
+ void eliminateFunctionsAndGlobals (SILModuleTransform *DFEPass) {
696
670
697
671
LLVM_DEBUG (llvm::dbgs () << " running dead function elimination\n " );
698
672
findAliveFunctions ();
@@ -730,15 +704,12 @@ class DeadFunctionElimination : FunctionLivenessComputation {
730
704
}
731
705
732
706
// Last step: delete all dead functions.
733
- while (!DeadFunctions.empty ()) {
734
- SILFunction *F = DeadFunctions.back ();
735
- DeadFunctions.pop_back ();
736
-
737
- LLVM_DEBUG (llvm::dbgs () << " erase dead function " << F->getName ()
707
+ for (SILFunction *deadFunc : DeadFunctions) {
708
+ LLVM_DEBUG (llvm::dbgs () << " erase dead function " << deadFunc->getName ()
738
709
<< " \n " );
739
710
++NumDeadFunc;
740
- DFEPass->notifyWillDeleteFunction (F );
741
- Module->eraseFunction (F );
711
+ DFEPass->notifyWillDeleteFunction (deadFunc );
712
+ Module->eraseFunction (deadFunc );
742
713
}
743
714
for (SILGlobalVariable *deadGlobal : DeadGlobals) {
744
715
++NumDeadGlobals;
@@ -758,13 +729,13 @@ class DeadFunctionElimination : FunctionLivenessComputation {
758
729
759
730
namespace {
760
731
761
- class SILDeadFuncElimination : public SILModuleTransform {
732
+ class DeadFunctionAndGlobalEliminationPass : public SILModuleTransform {
762
733
763
734
private:
764
735
bool isLateDFE;
765
736
766
737
public:
767
- SILDeadFuncElimination (bool isLateDFE) : isLateDFE(isLateDFE) { }
738
+ DeadFunctionAndGlobalEliminationPass (bool isLateDFE) : isLateDFE(isLateDFE) {}
768
739
769
740
void run () override {
770
741
LLVM_DEBUG (llvm::dbgs () << " Running DeadFuncElimination\n " );
@@ -776,24 +747,25 @@ class SILDeadFuncElimination : public SILModuleTransform {
776
747
// can eliminate such functions.
777
748
getModule ()->invalidateSILLoaderCaches ();
778
749
779
- DeadFunctionElimination deadFunctionElimination (getModule (),
750
+ DeadFunctionAndGlobalElimination deadFunctionElimination (getModule (),
780
751
/* keepExternalWitnessTablesAlive*/ !isLateDFE);
781
- deadFunctionElimination.eliminateFunctions (this );
752
+ deadFunctionElimination.eliminateFunctionsAndGlobals (this );
782
753
}
783
754
};
784
755
785
756
} // end anonymous namespace
786
757
787
- SILTransform *swift::createDeadFunctionElimination () {
788
- return new SILDeadFuncElimination (/* isLateDFE*/ false );
758
+ SILTransform *swift::createDeadFunctionAndGlobalElimination () {
759
+ return new DeadFunctionAndGlobalEliminationPass (/* isLateDFE*/ false );
789
760
}
790
761
791
- SILTransform *swift::createLateDeadFunctionElimination () {
792
- return new SILDeadFuncElimination (/* isLateDFE*/ true );
762
+ SILTransform *swift::createLateDeadFunctionAndGlobalElimination () {
763
+ return new DeadFunctionAndGlobalEliminationPass (/* isLateDFE*/ true );
793
764
}
794
765
795
766
void swift::performSILDeadFunctionElimination (SILModule *M) {
796
- llvm::SmallVector<PassKind, 1 > Pass = {PassKind::DeadFunctionElimination};
767
+ llvm::SmallVector<PassKind, 1 > Pass =
768
+ {PassKind::DeadFunctionAndGlobalElimination};
797
769
auto &opts = M->getOptions ();
798
770
auto plan = SILPassPipelinePlan::getPassPipelineForKinds (opts, Pass);
799
771
executePassPipelinePlan (M, plan);
0 commit comments