Skip to content

Commit 0b6e3bf

Browse files
committed
DeadFunctionElimination: factor out function/method visiting of keypath components into a general utility.
This is a NFC
1 parent 402e228 commit 0b6e3bf

File tree

3 files changed

+61
-34
lines changed

3 files changed

+61
-34
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2892,6 +2892,10 @@ class KeyPathPatternComponent {
28922892
return KeyPathPatternComponent(tupleIndex, ty);
28932893
}
28942894

2895+
void visitReferencedFunctionsAndMethods(
2896+
std::function<void (SILFunction *)> functionCallBack,
2897+
std::function<void (SILDeclRef)> methodCallBack) const;
2898+
28952899
void incrementRefCounts() const;
28962900
void decrementRefCounts() const;
28972901

@@ -2948,6 +2952,15 @@ class KeyPathPattern final
29482952

29492953
ArrayRef<KeyPathPatternComponent> getComponents() const;
29502954

2955+
void visitReferencedFunctionsAndMethods(
2956+
std::function<void (SILFunction *)> functionCallBack,
2957+
std::function<void (SILDeclRef)> methodCallBack) {
2958+
for (auto &component : getComponents()) {
2959+
component.visitReferencedFunctionsAndMethods(functionCallBack,
2960+
methodCallBack);
2961+
}
2962+
}
2963+
29512964
static KeyPathPattern *get(SILModule &M,
29522965
CanGenericSignature signature,
29532966
CanType rootType,

lib/SIL/SILInstructions.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2419,6 +2419,46 @@ void KeyPathInst::dropReferencedPattern() {
24192419
Pattern = nullptr;
24202420
}
24212421

2422+
void KeyPathPatternComponent::
2423+
visitReferencedFunctionsAndMethods(
2424+
std::function<void (SILFunction *)> functionCallBack,
2425+
std::function<void (SILDeclRef)> methodCallBack) const {
2426+
switch (getKind()) {
2427+
case KeyPathPatternComponent::Kind::SettableProperty:
2428+
functionCallBack(getComputedPropertySetter());
2429+
LLVM_FALLTHROUGH;
2430+
case KeyPathPatternComponent::Kind::GettableProperty: {
2431+
functionCallBack(getComputedPropertyGetter());
2432+
auto id = getComputedPropertyId();
2433+
switch (id.getKind()) {
2434+
case KeyPathPatternComponent::ComputedPropertyId::DeclRef: {
2435+
methodCallBack(id.getDeclRef());
2436+
break;
2437+
}
2438+
case KeyPathPatternComponent::ComputedPropertyId::Function:
2439+
functionCallBack(id.getFunction());
2440+
break;
2441+
case KeyPathPatternComponent::ComputedPropertyId::Property:
2442+
break;
2443+
}
2444+
2445+
if (auto equals = getSubscriptIndexEquals())
2446+
functionCallBack(equals);
2447+
if (auto hash = getSubscriptIndexHash())
2448+
functionCallBack(hash);
2449+
2450+
break;
2451+
}
2452+
case KeyPathPatternComponent::Kind::StoredProperty:
2453+
case KeyPathPatternComponent::Kind::OptionalChain:
2454+
case KeyPathPatternComponent::Kind::OptionalForce:
2455+
case KeyPathPatternComponent::Kind::OptionalWrap:
2456+
case KeyPathPatternComponent::Kind::TupleElement:
2457+
break;
2458+
}
2459+
}
2460+
2461+
24222462
GenericSpecializationInformation::GenericSpecializationInformation(
24232463
SILFunction *Caller, SILFunction *Parent, SubstitutionMap Subs)
24242464
: Caller(Caller), Parent(Parent), Subs(Subs) {}

lib/SILOptimizer/IPO/DeadFunctionElimination.cpp

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -196,22 +196,17 @@ class FunctionLivenessComputation {
196196
/// aren't yet.
197197
void
198198
ensureKeyPathComponentIsAlive(const KeyPathPatternComponent &component) {
199-
switch (component.getKind()) {
200-
case KeyPathPatternComponent::Kind::SettableProperty:
201-
ensureAlive(component.getComputedPropertySetter());
202-
LLVM_FALLTHROUGH;
203-
case KeyPathPatternComponent::Kind::GettableProperty: {
204-
ensureAlive(component.getComputedPropertyGetter());
205-
auto id = component.getComputedPropertyId();
206-
switch (id.getKind()) {
207-
case KeyPathPatternComponent::ComputedPropertyId::DeclRef: {
208-
auto declRef = id.getDeclRef();
209-
if (declRef.isForeign) {
199+
component.visitReferencedFunctionsAndMethods(
200+
[this](SILFunction *F) {
201+
ensureAlive(F);
202+
},
203+
[this](SILDeclRef method) {
204+
if (method.isForeign) {
210205
// Nothing to do here: foreign functions aren't ours to be deleting.
211206
// (And even if they were, they're ObjC-dispatched and thus anchored
212207
// already: see isAnchorFunction)
213208
} else {
214-
auto decl = cast<AbstractFunctionDecl>(declRef.getDecl());
209+
auto decl = cast<AbstractFunctionDecl>(method.getDecl());
215210
if (auto clas = dyn_cast<ClassDecl>(decl->getDeclContext())) {
216211
ensureAliveClassMethod(getMethodInfo(decl, /*witness*/ false),
217212
dyn_cast<FuncDecl>(decl),
@@ -222,29 +217,8 @@ class FunctionLivenessComputation {
222217
llvm_unreachable("key path keyed by a non-class, non-protocol method");
223218
}
224219
}
225-
break;
226220
}
227-
case KeyPathPatternComponent::ComputedPropertyId::Function:
228-
ensureAlive(id.getFunction());
229-
break;
230-
case KeyPathPatternComponent::ComputedPropertyId::Property:
231-
break;
232-
}
233-
234-
if (auto equals = component.getSubscriptIndexEquals())
235-
ensureAlive(equals);
236-
if (auto hash = component.getSubscriptIndexHash())
237-
ensureAlive(hash);
238-
239-
break;
240-
}
241-
case KeyPathPatternComponent::Kind::StoredProperty:
242-
case KeyPathPatternComponent::Kind::OptionalChain:
243-
case KeyPathPatternComponent::Kind::OptionalForce:
244-
case KeyPathPatternComponent::Kind::OptionalWrap:
245-
case KeyPathPatternComponent::Kind::TupleElement:
246-
break;
247-
}
221+
);
248222
}
249223

250224
/// Marks a function as alive if it is not alive yet.

0 commit comments

Comments
 (0)