Skip to content

Commit 03b7eae

Browse files
committed
[SILOptimizer] NFC: Adopt reference storage type meta-programming macros
1 parent 54e85ba commit 03b7eae

17 files changed

+223
-194
lines changed

lib/SILOptimizer/Analysis/ARCAnalysis.cpp

+20-4
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,30 @@ using BasicBlockRetainValue = std::pair<SILBasicBlock *, SILValue>;
3636
//===----------------------------------------------------------------------===//
3737

3838
bool swift::isRetainInstruction(SILInstruction *I) {
39-
return isa<StrongRetainInst>(I) || isa<RetainValueInst>(I) ||
40-
isa<UnownedRetainInst>(I);
39+
switch (I->getKind()) {
40+
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
41+
case SILInstructionKind::Name##RetainInst:
42+
#include "swift/AST/ReferenceStorage.def"
43+
case SILInstructionKind::StrongRetainInst:
44+
case SILInstructionKind::RetainValueInst:
45+
return true;
46+
default:
47+
return false;
48+
}
4149
}
4250

4351

4452
bool swift::isReleaseInstruction(SILInstruction *I) {
45-
return isa<StrongReleaseInst>(I) || isa<ReleaseValueInst>(I) ||
46-
isa<UnownedReleaseInst>(I);
53+
switch (I->getKind()) {
54+
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
55+
case SILInstructionKind::Name##ReleaseInst:
56+
#include "swift/AST/ReferenceStorage.def"
57+
case SILInstructionKind::StrongReleaseInst:
58+
case SILInstructionKind::ReleaseValueInst:
59+
return true;
60+
default:
61+
return false;
62+
}
4763
}
4864

4965
//===----------------------------------------------------------------------===//

lib/SILOptimizer/Analysis/EscapeAnalysis.cpp

+27-11
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,11 @@ void EscapeAnalysis::ConnectionGraph::computeUsePoints() {
404404

405405
for (auto &I : BB) {
406406
switch (I.getKind()) {
407+
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
408+
case SILInstructionKind::Name##ReleaseInst:
409+
#include "swift/AST/ReferenceStorage.def"
407410
case SILInstructionKind::StrongReleaseInst:
408411
case SILInstructionKind::ReleaseValueInst:
409-
case SILInstructionKind::UnownedReleaseInst:
410412
case SILInstructionKind::ApplyInst:
411413
case SILInstructionKind::TryApplyInst: {
412414
/// Actually we only add instructions which may release a reference.
@@ -1369,12 +1371,14 @@ void EscapeAnalysis::analyzeInstruction(SILInstruction *I,
13691371
ConGraph->getNode(cast<SingleValueInstruction>(I), this);
13701372
return;
13711373

1374+
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
1375+
case SILInstructionKind::Name##RetainInst: \
1376+
case SILInstructionKind::StrongRetain##Name##Inst: \
1377+
case SILInstructionKind::Copy##Name##ValueInst:
1378+
#include "swift/AST/ReferenceStorage.def"
13721379
case SILInstructionKind::DeallocStackInst:
13731380
case SILInstructionKind::StrongRetainInst:
1374-
case SILInstructionKind::StrongRetainUnownedInst:
1375-
case SILInstructionKind::CopyUnownedValueInst:
13761381
case SILInstructionKind::RetainValueInst:
1377-
case SILInstructionKind::UnownedRetainInst:
13781382
case SILInstructionKind::BranchInst:
13791383
case SILInstructionKind::CondBranchInst:
13801384
case SILInstructionKind::SwitchEnumInst:
@@ -1391,10 +1395,13 @@ void EscapeAnalysis::analyzeInstruction(SILInstruction *I,
13911395
case SILInstructionKind::ValueToBridgeObjectInst:
13921396
// These instructions don't have any effect on escaping.
13931397
return;
1398+
1399+
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
1400+
case SILInstructionKind::Name##ReleaseInst:
1401+
#include "swift/AST/ReferenceStorage.def"
13941402
case SILInstructionKind::StrongReleaseInst:
13951403
case SILInstructionKind::ReleaseValueInst:
1396-
case SILInstructionKind::StrongUnpinInst:
1397-
case SILInstructionKind::UnownedReleaseInst: {
1404+
case SILInstructionKind::StrongUnpinInst: {
13981405
SILValue OpV = I->getOperand(0);
13991406
if (CGNode *AddrNode = ConGraph->getNode(OpV, this)) {
14001407
// A release instruction may deallocate the pointer operand. This may
@@ -1410,8 +1417,11 @@ void EscapeAnalysis::analyzeInstruction(SILInstruction *I,
14101417
}
14111418
return;
14121419
}
1420+
1421+
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
1422+
case SILInstructionKind::Load##Name##Inst:
1423+
#include "swift/AST/ReferenceStorage.def"
14131424
case SILInstructionKind::LoadInst:
1414-
case SILInstructionKind::LoadWeakInst:
14151425
// We treat ref_element_addr like a load (see NodeType::Content).
14161426
case SILInstructionKind::RefElementAddrInst:
14171427
case SILInstructionKind::RefTailAddrInst:
@@ -1462,9 +1472,11 @@ void EscapeAnalysis::analyzeInstruction(SILInstruction *I,
14621472
}
14631473
return;
14641474
}
1465-
break;
1475+
1476+
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
1477+
case SILInstructionKind::Store##Name##Inst:
1478+
#include "swift/AST/ReferenceStorage.def"
14661479
case SILInstructionKind::StoreInst:
1467-
case SILInstructionKind::StoreWeakInst:
14681480
if (CGNode *ValueNode = ConGraph->getNode(I->getOperand(StoreInst::Src),
14691481
this)) {
14701482
CGNode *AddrNode = ConGraph->getNode(I->getOperand(StoreInst::Dest),
@@ -1541,15 +1553,19 @@ void EscapeAnalysis::analyzeInstruction(SILInstruction *I,
15411553
case SILInstructionKind::UpcastInst:
15421554
case SILInstructionKind::InitExistentialRefInst:
15431555
case SILInstructionKind::OpenExistentialRefInst:
1544-
case SILInstructionKind::UnownedToRefInst:
1545-
case SILInstructionKind::RefToUnownedInst:
15461556
case SILInstructionKind::RawPointerToRefInst:
15471557
case SILInstructionKind::RefToRawPointerInst:
15481558
case SILInstructionKind::RefToBridgeObjectInst:
15491559
case SILInstructionKind::BridgeObjectToRefInst:
15501560
case SILInstructionKind::UncheckedAddrCastInst:
15511561
case SILInstructionKind::UnconditionalCheckedCastInst:
15521562
case SILInstructionKind::StrongPinInst:
1563+
// DO NOT use LOADABLE_REF_STORAGE because unchecked references don't have
1564+
// retain/release instructions that trigger the 'default' case.
1565+
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
1566+
case SILInstructionKind::RefTo##Name##Inst: \
1567+
case SILInstructionKind::Name##ToRefInst:
1568+
#include "swift/AST/ReferenceStorage.def"
15531569
// A cast is almost like a projection.
15541570
if (CGNode *OpNode = ConGraph->getNode(I->getOperand(0), this)) {
15551571
ConGraph->setNode(cast<SingleValueInstruction>(I), OpNode);

lib/SILOptimizer/Analysis/MemoryBehavior.cpp

+15-9
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ class MemoryBehaviorVisitor
9999
MemBehavior visitTryApplyInst(TryApplyInst *AI);
100100
MemBehavior visitBuiltinInst(BuiltinInst *BI);
101101
MemBehavior visitStrongReleaseInst(StrongReleaseInst *BI);
102-
MemBehavior visitUnownedReleaseInst(UnownedReleaseInst *BI);
103102
MemBehavior visitReleaseValueInst(ReleaseValueInst *BI);
104103
MemBehavior visitSetDeallocatingInst(SetDeallocatingInst *BI);
104+
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
105+
MemBehavior visit##Name##ReleaseInst(Name##ReleaseInst *BI);
106+
#include "swift/AST/ReferenceStorage.def"
105107

106108
// Instructions which are none if our SILValue does not alias one of its
107109
// arguments. If we cannot prove such a thing, return the relevant memory
@@ -151,10 +153,12 @@ class MemoryBehaviorVisitor
151153
return I->getMemoryBehavior(); \
152154
}
153155
REFCOUNTINC_MEMBEHAVIOR_INST(StrongRetainInst)
154-
REFCOUNTINC_MEMBEHAVIOR_INST(StrongRetainUnownedInst)
155-
REFCOUNTINC_MEMBEHAVIOR_INST(CopyUnownedValueInst)
156-
REFCOUNTINC_MEMBEHAVIOR_INST(UnownedRetainInst)
157156
REFCOUNTINC_MEMBEHAVIOR_INST(RetainValueInst)
157+
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
158+
REFCOUNTINC_MEMBEHAVIOR_INST(Name##RetainInst) \
159+
REFCOUNTINC_MEMBEHAVIOR_INST(StrongRetain##Name##Inst) \
160+
REFCOUNTINC_MEMBEHAVIOR_INST(Copy##Name##ValueInst)
161+
#include "swift/AST/ReferenceStorage.def"
158162
#undef REFCOUNTINC_MEMBEHAVIOR_INST
159163
};
160164

@@ -305,12 +309,14 @@ MemoryBehaviorVisitor::visitStrongReleaseInst(StrongReleaseInst *SI) {
305309
return MemBehavior::MayHaveSideEffects;
306310
}
307311

308-
MemBehavior
309-
MemoryBehaviorVisitor::visitUnownedReleaseInst(UnownedReleaseInst *SI) {
310-
if (!EA->canEscapeTo(V, SI))
311-
return MemBehavior::None;
312-
return MemBehavior::MayHaveSideEffects;
312+
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
313+
MemBehavior \
314+
MemoryBehaviorVisitor::visit##Name##ReleaseInst(Name##ReleaseInst *SI) { \
315+
if (!EA->canEscapeTo(V, SI)) \
316+
return MemBehavior::None; \
317+
return MemBehavior::MayHaveSideEffects; \
313318
}
319+
#include "swift/AST/ReferenceStorage.def"
314320

315321
MemBehavior MemoryBehaviorVisitor::visitReleaseValueInst(ReleaseValueInst *SI) {
316322
if (!EA->canEscapeTo(V, SI))

lib/SILOptimizer/Analysis/SideEffectAnalysis.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -481,16 +481,20 @@ void FunctionSideEffects::analyzeInstruction(SILInstruction *I) {
481481
case SILInstructionKind::AllocStackInst:
482482
case SILInstructionKind::DeallocStackInst:
483483
return;
484+
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
485+
case SILInstructionKind::Name##RetainInst: \
486+
case SILInstructionKind::StrongRetain##Name##Inst: \
487+
case SILInstructionKind::Copy##Name##ValueInst:
488+
#include "swift/AST/ReferenceStorage.def"
484489
case SILInstructionKind::StrongRetainInst:
485-
case SILInstructionKind::StrongRetainUnownedInst:
486-
case SILInstructionKind::CopyUnownedValueInst:
487490
case SILInstructionKind::RetainValueInst:
488-
case SILInstructionKind::UnownedRetainInst:
489491
getEffectsOn(I->getOperand(0))->Retains = true;
490492
return;
493+
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
494+
case SILInstructionKind::Name##ReleaseInst:
495+
#include "swift/AST/ReferenceStorage.def"
491496
case SILInstructionKind::StrongReleaseInst:
492497
case SILInstructionKind::ReleaseValueInst:
493-
case SILInstructionKind::UnownedReleaseInst:
494498
getEffectsOn(I->getOperand(0))->Releases = true;
495499
return;
496500
case SILInstructionKind::UnconditionalCheckedCastInst:

lib/SILOptimizer/Analysis/SimplifyInstruction.cpp

+19-38
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ namespace {
4646
SILValue visitTupleInst(TupleInst *SI);
4747
SILValue visitBuiltinInst(BuiltinInst *AI);
4848
SILValue visitUpcastInst(UpcastInst *UI);
49-
SILValue visitRefToUnownedInst(RefToUnownedInst *RUI);
50-
SILValue visitUnownedToRefInst(UnownedToRefInst *URI);
51-
SILValue visitRefToUnmanagedInst(RefToUnmanagedInst *RUI);
52-
SILValue visitUnmanagedToRefInst(UnmanagedToRefInst *URI);
49+
#define LOADABLE_REF_STORAGE(Name, ...) \
50+
SILValue visitRefTo##Name##Inst(RefTo##Name##Inst *I); \
51+
SILValue visit##Name##ToRefInst(Name##ToRefInst *I);
52+
#include "swift/AST/ReferenceStorage.def"
5353
SILValue visitUncheckedBitwiseCastInst(UncheckedBitwiseCastInst *UBCI);
5454
SILValue
5555
visitUncheckedTrivialBitCastInst(UncheckedTrivialBitCastInst *UTBCI);
@@ -368,41 +368,22 @@ SILValue InstSimplifier::visitUpcastInst(UpcastInst *UI) {
368368
return SILValue();
369369
}
370370

371-
SILValue
372-
InstSimplifier::visitRefToUnownedInst(RefToUnownedInst *RUI) {
373-
if (auto *URI = dyn_cast<UnownedToRefInst>(RUI->getOperand()))
374-
if (URI->getOperand()->getType() == RUI->getType())
375-
return URI->getOperand();
376-
377-
return SILValue();
378-
}
379-
380-
SILValue
381-
InstSimplifier::visitUnownedToRefInst(UnownedToRefInst *URI) {
382-
if (auto *RUI = dyn_cast<RefToUnownedInst>(URI->getOperand()))
383-
if (RUI->getOperand()->getType() == URI->getType())
384-
return RUI->getOperand();
385-
386-
return SILValue();
387-
}
388-
389-
SILValue
390-
InstSimplifier::visitRefToUnmanagedInst(RefToUnmanagedInst *RUI) {
391-
if (auto *URI = dyn_cast<UnmanagedToRefInst>(RUI->getOperand()))
392-
if (URI->getOperand()->getType() == RUI->getType())
393-
return URI->getOperand();
394-
395-
return SILValue();
396-
}
397-
398-
SILValue
399-
InstSimplifier::visitUnmanagedToRefInst(UnmanagedToRefInst *URI) {
400-
if (auto *RUI = dyn_cast<RefToUnmanagedInst>(URI->getOperand()))
401-
if (RUI->getOperand()->getType() == URI->getType())
402-
return RUI->getOperand();
403-
404-
return SILValue();
371+
#define LOADABLE_REF_STORAGE(Name, ...) \
372+
SILValue \
373+
InstSimplifier::visitRefTo##Name##Inst(RefTo##Name##Inst *RUI) { \
374+
if (auto *URI = dyn_cast<Name##ToRefInst>(RUI->getOperand())) \
375+
if (URI->getOperand()->getType() == RUI->getType()) \
376+
return URI->getOperand(); \
377+
return SILValue(); \
378+
} \
379+
SILValue \
380+
InstSimplifier::visit##Name##ToRefInst(Name##ToRefInst *URI) { \
381+
if (auto *RUI = dyn_cast<RefTo##Name##Inst>(URI->getOperand())) \
382+
if (RUI->getOperand()->getType() == URI->getType()) \
383+
return RUI->getOperand(); \
384+
return SILValue(); \
405385
}
386+
#include "swift/AST/ReferenceStorage.def"
406387

407388
SILValue
408389
InstSimplifier::

lib/SILOptimizer/Mandatory/DIMemoryUseCollectorOwnership.cpp

+20-31
Original file line numberDiff line numberDiff line change
@@ -736,10 +736,12 @@ void ElementUseCollector::collectUses(SILValue Pointer, unsigned BaseEltNo) {
736736
continue;
737737
}
738738

739-
if (isa<LoadWeakInst>(User)) {
740-
trackUse(DIMemoryUse(User, DIUseKind::Load, BaseEltNo, 1));
741-
continue;
739+
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
740+
if (isa<Load##Name##Inst>(User)) { \
741+
trackUse(DIMemoryUse(User, DIUseKind::Load, BaseEltNo, 1)); \
742+
continue; \
742743
}
744+
#include "swift/AST/ReferenceStorage.def"
743745

744746
// Stores *to* the allocation are writes.
745747
if ((isa<StoreInst>(User) || isa<AssignInst>(User)) &&
@@ -765,35 +767,22 @@ void ElementUseCollector::collectUses(SILValue Pointer, unsigned BaseEltNo) {
765767
continue;
766768
}
767769

768-
if (auto SWI = dyn_cast<StoreWeakInst>(User))
769-
if (Op->getOperandNumber() == 1) {
770-
DIUseKind Kind;
771-
if (InStructSubElement)
772-
Kind = DIUseKind::PartialStore;
773-
else if (SWI->isInitializationOfDest())
774-
Kind = DIUseKind::Initialization;
775-
else if (isDefiniteInitFinished)
776-
Kind = DIUseKind::Assign;
777-
else
778-
Kind = DIUseKind::InitOrAssign;
779-
trackUse(DIMemoryUse(User, Kind, BaseEltNo, 1));
780-
continue;
781-
}
782-
783-
if (auto SUI = dyn_cast<StoreUnownedInst>(User))
784-
if (Op->getOperandNumber() == 1) {
785-
DIUseKind Kind;
786-
if (InStructSubElement)
787-
Kind = DIUseKind::PartialStore;
788-
else if (SUI->isInitializationOfDest())
789-
Kind = DIUseKind::Initialization;
790-
else if (isDefiniteInitFinished)
791-
Kind = DIUseKind::Assign;
792-
else
793-
Kind = DIUseKind::InitOrAssign;
794-
trackUse(DIMemoryUse(User, Kind, BaseEltNo, 1));
795-
continue;
770+
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
771+
if (auto SWI = dyn_cast<Store##Name##Inst>(User)) \
772+
if (Op->getOperandNumber() == 1) { \
773+
DIUseKind Kind; \
774+
if (InStructSubElement) \
775+
Kind = DIUseKind::PartialStore; \
776+
else if (SWI->isInitializationOfDest()) \
777+
Kind = DIUseKind::Initialization; \
778+
else if (isDefiniteInitFinished) \
779+
Kind = DIUseKind::Assign; \
780+
else \
781+
Kind = DIUseKind::InitOrAssign; \
782+
trackUse(DIMemoryUse(User, Kind, BaseEltNo, 1)); \
783+
continue; \
796784
}
785+
#include "swift/AST/ReferenceStorage.def"
797786

798787
if (auto *CAI = dyn_cast<CopyAddrInst>(User)) {
799788
// If this is a copy of a tuple, we should scalarize it so that we don't

lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp

+9-15
Original file line numberDiff line numberDiff line change
@@ -1796,21 +1796,15 @@ void LifetimeChecker::updateInstructionForInitState(DIMemoryUse &Use) {
17961796
return;
17971797
}
17981798

1799-
if (auto *SW = dyn_cast<StoreWeakInst>(Inst)) {
1800-
assert(!SW->isInitializationOfDest() &&
1801-
"should not modify store_weak that already knows it is initialized");
1802-
1803-
SW->setIsInitializationOfDest(InitKind);
1804-
return;
1805-
}
1806-
1807-
if (auto *SU = dyn_cast<StoreUnownedInst>(Inst)) {
1808-
assert(!SU->isInitializationOfDest() &&
1809-
"should not modify store_unowned that already knows it is an init");
1810-
1811-
SU->setIsInitializationOfDest(InitKind);
1812-
return;
1813-
}
1799+
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, name, ...) \
1800+
if (auto *SW = dyn_cast<Store##Name##Inst>(Inst)) { \
1801+
if (SW->isInitializationOfDest()) \
1802+
llvm_unreachable("should not modify store_" #name \
1803+
" that already knows it is initialized"); \
1804+
SW->setIsInitializationOfDest(InitKind); \
1805+
return; \
1806+
}
1807+
#include "swift/AST/ReferenceStorage.def"
18141808

18151809
// If this is an assign, rewrite it based on whether it is an initialization
18161810
// or not.

0 commit comments

Comments
 (0)