Skip to content

Commit acf72bd

Browse files
authored
[SILOptimizer] Simplify 'calleesAreStaticallyKnowable' to handle both abstract functions and enum element decls (swiftlang#32968)
1 parent ec1c155 commit acf72bd

File tree

2 files changed

+11
-53
lines changed

2 files changed

+11
-53
lines changed

include/swift/SILOptimizer/Utils/InstOptUtils.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,7 @@ bool calleesAreStaticallyKnowable(SILModule &module, SILDeclRef decl);
497497

498498
/// Do we have enough information to determine all callees that could
499499
/// be reached by calling the function represented by Decl?
500-
bool calleesAreStaticallyKnowable(SILModule &module, AbstractFunctionDecl *afd);
501-
502-
/// Do we have enough information to determine all callees that could
503-
/// be reached by calling the function represented by Decl?
504-
bool calleesAreStaticallyKnowable(SILModule &module, EnumElementDecl *eed);
500+
bool calleesAreStaticallyKnowable(SILModule &module, ValueDecl *vd);
505501

506502
// Attempt to get the instance for , whose static type is the same as
507503
// its exact dynamic type, returning a null SILValue() if we cannot find it.

lib/SILOptimizer/Utils/InstOptUtils.cpp

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,42 +1801,35 @@ void swift::replaceLoadSequence(SILInstruction *inst, SILValue value) {
18011801
bool swift::calleesAreStaticallyKnowable(SILModule &module, SILDeclRef decl) {
18021802
if (decl.isForeign)
18031803
return false;
1804-
1805-
if (decl.isEnumElement()) {
1806-
return calleesAreStaticallyKnowable(module,
1807-
cast<EnumElementDecl>(decl.getDecl()));
1808-
}
1809-
1810-
auto *afd = decl.getAbstractFunctionDecl();
1811-
assert(afd && "Expected abstract function decl!");
1812-
return calleesAreStaticallyKnowable(module, afd);
1804+
return calleesAreStaticallyKnowable(module, decl.getDecl());
18131805
}
18141806

18151807
/// Are the callees that could be called through Decl statically
18161808
/// knowable based on the Decl and the compilation mode?
1817-
bool swift::calleesAreStaticallyKnowable(SILModule &module,
1818-
AbstractFunctionDecl *afd) {
1809+
bool swift::calleesAreStaticallyKnowable(SILModule &module, ValueDecl *vd) {
1810+
assert(isa<AbstractFunctionDecl>(vd) || isa<EnumElementDecl>(vd));
1811+
18191812
// Only handle members defined within the SILModule's associated context.
1820-
if (!afd->isChildContextOf(module.getAssociatedContext()))
1813+
if (!cast<DeclContext>(vd)->isChildContextOf(module.getAssociatedContext()))
18211814
return false;
18221815

1823-
if (afd->isDynamic()) {
1816+
if (vd->isDynamic()) {
18241817
return false;
18251818
}
18261819

1827-
if (!afd->hasAccess())
1820+
if (!vd->hasAccess())
18281821
return false;
18291822

18301823
// Only consider 'private' members, unless we are in whole-module compilation.
1831-
switch (afd->getEffectiveAccess()) {
1824+
switch (vd->getEffectiveAccess()) {
18321825
case AccessLevel::Open:
18331826
return false;
18341827
case AccessLevel::Public:
1835-
if (isa<ConstructorDecl>(afd)) {
1828+
if (isa<ConstructorDecl>(vd)) {
18361829
// Constructors are special: a derived class in another module can
18371830
// "override" a constructor if its class is "open", although the
18381831
// constructor itself is not open.
1839-
auto *nd = afd->getDeclContext()->getSelfNominalTypeDecl();
1832+
auto *nd = vd->getDeclContext()->getSelfNominalTypeDecl();
18401833
if (nd->getEffectiveAccess() == AccessLevel::Open)
18411834
return false;
18421835
}
@@ -1851,37 +1844,6 @@ bool swift::calleesAreStaticallyKnowable(SILModule &module,
18511844
llvm_unreachable("Unhandled access level in switch.");
18521845
}
18531846

1854-
/// Are the callees that could be called through Decl statically
1855-
/// knowable based on the Decl and the compilation mode?
1856-
// FIXME: Merge this with calleesAreStaticallyKnowable above
1857-
bool swift::calleesAreStaticallyKnowable(SILModule &module,
1858-
EnumElementDecl *eed) {
1859-
// Only handle members defined within the SILModule's associated context.
1860-
if (!eed->isChildContextOf(module.getAssociatedContext()))
1861-
return false;
1862-
1863-
if (eed->isDynamic()) {
1864-
return false;
1865-
}
1866-
1867-
if (!eed->hasAccess())
1868-
return false;
1869-
1870-
// Only consider 'private' members, unless we are in whole-module compilation.
1871-
switch (eed->getEffectiveAccess()) {
1872-
case AccessLevel::Open:
1873-
return false;
1874-
case AccessLevel::Public:
1875-
case AccessLevel::Internal:
1876-
return module.isWholeModule();
1877-
case AccessLevel::FilePrivate:
1878-
case AccessLevel::Private:
1879-
return true;
1880-
}
1881-
1882-
llvm_unreachable("Unhandled access level in switch.");
1883-
}
1884-
18851847
Optional<FindLocalApplySitesResult>
18861848
swift::findLocalApplySites(FunctionRefBaseInst *fri) {
18871849
SmallVector<Operand *, 32> worklist(fri->use_begin(), fri->use_end());

0 commit comments

Comments
 (0)