-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathPruneVTables.cpp
119 lines (103 loc) · 4.1 KB
/
PruneVTables.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//===--- PruneVTables.cpp - Prune unnecessary vtable entries --------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Mark sil_vtable entries as [nonoverridden] when possible, so that we know
// at IRGen time they can be elided from runtime vtables.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "prune-vtables"
#include "swift/SIL/CalleeCache.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
STATISTIC(NumNonoverriddenVTableEntries,
"# of vtable entries marked non-overridden");
using namespace swift;
namespace {
class PruneVTables : public SILModuleTransform {
void runOnVTable(SILModule *M, SILVTable *vtable) {
LLVM_DEBUG(llvm::dbgs() << "PruneVTables inspecting table:\n";
vtable->print(llvm::dbgs()));
if (!M->isWholeModule() &&
vtable->getClass()->getEffectiveAccess() >= AccessLevel::FilePrivate) {
LLVM_DEBUG(llvm::dbgs() << "Ignoring visible table: ";
vtable->print(llvm::dbgs()));
return;
}
for (auto &entry : vtable->getMutableEntries()) {
// We don't need to worry about entries that are overridden,
// or have already been found to have no overrides.
if (entry.isNonOverridden()) {
LLVM_DEBUG(llvm::dbgs() << "-- entry for ";
entry.getMethod().print(llvm::dbgs());
llvm::dbgs() << " is already nonoverridden\n");
continue;
}
switch (entry.getKind()) {
case SILVTable::Entry::Normal:
case SILVTable::Entry::Inherited:
break;
case SILVTable::Entry::Override:
LLVM_DEBUG(llvm::dbgs() << "-- entry for ";
entry.getMethod().print(llvm::dbgs());
llvm::dbgs() << " is an override\n");
continue;
}
// The destructor entry must remain.
if (entry.getMethod().kind == SILDeclRef::Kind::Deallocator) {
LLVM_DEBUG(llvm::dbgs() << "-- entry for ";
entry.getMethod().print(llvm::dbgs());
llvm::dbgs() << " is a destructor\n");
continue;
}
auto methodDecl = entry.getMethod().getAbstractFunctionDecl();
if (!methodDecl) {
LLVM_DEBUG(llvm::dbgs() << "-- entry for ";
entry.getMethod().print(llvm::dbgs());
llvm::dbgs() << " is not a function decl\n");
continue;
}
// Is the method declared final?
if (!methodDecl->isFinal()) {
// Are callees of this entry statically knowable?
if (!calleesAreStaticallyKnowable(*M, entry.getMethod())) {
LLVM_DEBUG(llvm::dbgs() << "-- entry for ";
entry.getMethod().print(llvm::dbgs());
llvm::dbgs() << " does not have statically-knowable callees\n");
continue;
}
// Does the method have any overrides in this module?
if (methodDecl->isOverridden()) {
LLVM_DEBUG(llvm::dbgs() << "-- entry for ";
entry.getMethod().print(llvm::dbgs());
llvm::dbgs() << " has overrides\n");
continue;
}
}
LLVM_DEBUG(llvm::dbgs() << "++ entry for ";
entry.getMethod().print(llvm::dbgs());
llvm::dbgs() << " can be marked non-overridden!\n");
++NumNonoverriddenVTableEntries;
entry.setNonOverridden(true);
vtable->updateVTableCache(entry);
}
}
void run() override {
SILModule *M = getModule();
for (auto &vtable : M->getVTables()) {
runOnVTable(M, vtable);
}
}
};
}
SILTransform *swift::createPruneVTables() {
return new PruneVTables();
}