Skip to content

Commit c2cbbbb

Browse files
committed
[sil] Rather than maintaining manually the textual opcode for SILInstructions, just put the name in SILNodes.def and use metaprogramming.
This is a cleanup for SILParsing/Printing. I verified that everything was spelled correctly by taking the current parsing switch moving that into a file, regenerating it using the .def file and then diffed them. The diff was the same. rdar://28685236
1 parent 6a61476 commit c2cbbbb

File tree

8 files changed

+367
-551
lines changed

8 files changed

+367
-551
lines changed

include/swift/SIL/SILCloner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class SILCloner : protected SILVisitor<ImplClass> {
7878
void visit##CLASS(CLASS *I) { \
7979
llvm_unreachable("SILCloner visiting non-instruction?"); \
8080
}
81-
#define INST(CLASS, PARENT, MEMBEHAVIOR, RELEASINGBEHAVIOR) \
81+
#define INST(CLASS, PARENT, TEXTUALNAME, MEMBEHAVIOR, RELEASINGBEHAVIOR) \
8282
void visit##CLASS(CLASS *I);
8383
#include "swift/SIL/SILNodes.def"
8484

include/swift/SIL/SILInstruction.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ class FunctionRefInst;
5555

5656
template <typename ImplClass> class SILClonerWithScopes;
5757

58+
// An enum class for SILInstructions that enables exhaustive switches over
59+
// instructions.
60+
enum class SILInstructionKind : std::underlying_type<ValueKind>::type {
61+
#define INST(Id, Parent, TextualName, MemoryBehavior, ReleasingBehavior) \
62+
Id = static_cast<std::underlying_type<ValueKind>::type>(ValueKind::Id),
63+
#include "SILNodes.def"
64+
};
65+
5866
/// This is the root class for all instructions that can be used as the contents
5967
/// of a Swift SILBasicBlock.
6068
class SILInstruction : public ValueBase,public llvm::ilist_node<SILInstruction>{
@@ -4444,7 +4452,7 @@ class IndexRawPointerInst : public IndexingInst {
44444452
//===----------------------------------------------------------------------===//
44454453

44464454
enum class TermKind {
4447-
#define TERMINATOR(Id, Parent, MemBehavior, MayRelease) Id,
4455+
#define TERMINATOR(Id, Parent, TextualName, MemBehavior, MayRelease) Id,
44484456
#include "SILNodes.def"
44494457
};
44504458

@@ -4453,10 +4461,10 @@ struct ValueKindAsTermKind {
44534461

44544462
ValueKindAsTermKind(ValueKind V) {
44554463
switch (V) {
4456-
#define TERMINATOR(Id, Parent, MemBehavior, MayRelease) \
4457-
case ValueKind::Id: \
4458-
K = TermKind::Id; \
4459-
break;
4464+
#define TERMINATOR(Id, Parent, TextualName, MemBehavior, MayRelease) \
4465+
case ValueKind::Id: \
4466+
K = TermKind::Id; \
4467+
break;
44604468
#include "SILNodes.def"
44614469
default:
44624470
llvm_unreachable("Not a terminator kind?!");

include/swift/SIL/SILNodes.def

Lines changed: 150 additions & 147 deletions
Large diffs are not rendered by default.

lib/Parse/ParseSIL.cpp

Lines changed: 4 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,6 @@ bool SILParser::parseTypedValueRef(SILValue &Result, SourceLoc &Loc,
11411141
return false;
11421142
}
11431143

1144-
11451144
/// getInstructionKind - This method maps the string form of a SIL instruction
11461145
/// opcode to an enum.
11471146
bool SILParser::parseSILOpcode(ValueKind &Opcode, SourceLoc &OpcodeLoc,
@@ -1151,144 +1150,10 @@ bool SILParser::parseSILOpcode(ValueKind &Opcode, SourceLoc &OpcodeLoc,
11511150
// Parse this textually to avoid Swift keywords (like 'return') from
11521151
// interfering with opcode recognition.
11531152
Opcode = llvm::StringSwitch<ValueKind>(OpcodeName)
1154-
.Case("alloc_box", ValueKind::AllocBoxInst)
1155-
.Case("alloc_existential_box", ValueKind::AllocExistentialBoxInst)
1156-
.Case("address_to_pointer", ValueKind::AddressToPointerInst)
1157-
.Case("alloc_stack", ValueKind::AllocStackInst)
1158-
.Case("alloc_ref", ValueKind::AllocRefInst)
1159-
.Case("alloc_ref_dynamic", ValueKind::AllocRefDynamicInst)
1160-
.Case("alloc_value_buffer", ValueKind::AllocValueBufferInst)
1161-
.Case("bind_memory", ValueKind::BindMemoryInst)
1162-
.Case("value_metatype", ValueKind::ValueMetatypeInst)
1163-
.Case("witness_method", ValueKind::WitnessMethodInst)
1164-
.Case("apply", ValueKind::ApplyInst)
1165-
.Case("assign", ValueKind::AssignInst)
1166-
.Case("autorelease_value", ValueKind::AutoreleaseValueInst)
1167-
.Case("br", ValueKind::BranchInst)
1168-
.Case("builtin", ValueKind::BuiltinInst)
1169-
.Case("bridge_object_to_ref", ValueKind::BridgeObjectToRefInst)
1170-
.Case("bridge_object_to_word", ValueKind::BridgeObjectToWordInst)
1171-
.Case("checked_cast_br", ValueKind::CheckedCastBranchInst)
1172-
.Case("checked_cast_addr_br", ValueKind::CheckedCastAddrBranchInst)
1173-
.Case("class_method", ValueKind::ClassMethodInst)
1174-
.Case("cond_br", ValueKind::CondBranchInst)
1175-
.Case("cond_fail", ValueKind::CondFailInst)
1176-
.Case("convert_function", ValueKind::ConvertFunctionInst)
1177-
.Case("copy_addr", ValueKind::CopyAddrInst)
1178-
.Case("copy_block", ValueKind::CopyBlockInst)
1179-
.Case("dealloc_box", ValueKind::DeallocBoxInst)
1180-
.Case("dealloc_existential_box", ValueKind::DeallocExistentialBoxInst)
1181-
.Case("dealloc_ref", ValueKind::DeallocRefInst)
1182-
.Case("dealloc_partial_ref", ValueKind::DeallocPartialRefInst)
1183-
.Case("dealloc_stack", ValueKind::DeallocStackInst)
1184-
.Case("dealloc_value_buffer", ValueKind::DeallocValueBufferInst)
1185-
.Case("debug_value", ValueKind::DebugValueInst)
1186-
.Case("debug_value_addr", ValueKind::DebugValueAddrInst)
1187-
.Case("deinit_existential_addr", ValueKind::DeinitExistentialAddrInst)
1188-
.Case("destroy_addr", ValueKind::DestroyAddrInst)
1189-
.Case("release_value", ValueKind::ReleaseValueInst)
1190-
.Case("dynamic_method", ValueKind::DynamicMethodInst)
1191-
.Case("dynamic_method_br", ValueKind::DynamicMethodBranchInst)
1192-
.Case("enum", ValueKind::EnumInst)
1193-
.Case("fix_lifetime", ValueKind::FixLifetimeInst)
1194-
.Case("float_literal", ValueKind::FloatLiteralInst)
1195-
.Case("index_addr", ValueKind::IndexAddrInst)
1196-
.Case("index_raw_pointer", ValueKind::IndexRawPointerInst)
1197-
.Case("init_block_storage_header", ValueKind::InitBlockStorageHeaderInst)
1198-
.Case("init_enum_data_addr", ValueKind::InitEnumDataAddrInst)
1199-
.Case("init_existential_addr", ValueKind::InitExistentialAddrInst)
1200-
.Case("init_existential_metatype", ValueKind::InitExistentialMetatypeInst)
1201-
.Case("init_existential_ref", ValueKind::InitExistentialRefInst)
1202-
.Case("inject_enum_addr", ValueKind::InjectEnumAddrInst)
1203-
.Case("integer_literal", ValueKind::IntegerLiteralInst)
1204-
.Case("is_nonnull", ValueKind::IsNonnullInst)
1205-
.Case("is_unique", ValueKind::IsUniqueInst)
1206-
.Case("is_unique_or_pinned", ValueKind::IsUniqueOrPinnedInst)
1207-
.Case("function_ref", ValueKind::FunctionRefInst)
1208-
.Case("load", ValueKind::LoadInst)
1209-
.Case("load_unowned", ValueKind::LoadUnownedInst)
1210-
.Case("load_weak", ValueKind::LoadWeakInst)
1211-
.Case("mark_dependence", ValueKind::MarkDependenceInst)
1212-
.Case("mark_uninitialized", ValueKind::MarkUninitializedInst)
1213-
.Case("mark_uninitialized_behavior",
1214-
ValueKind::MarkUninitializedBehaviorInst)
1215-
.Case("mark_function_escape", ValueKind::MarkFunctionEscapeInst)
1216-
.Case("metatype", ValueKind::MetatypeInst)
1217-
.Case("objc_existential_metatype_to_object",
1218-
ValueKind::ObjCExistentialMetatypeToObjectInst)
1219-
.Case("objc_metatype_to_object", ValueKind::ObjCMetatypeToObjectInst)
1220-
.Case("objc_protocol", ValueKind::ObjCProtocolInst)
1221-
.Case("objc_to_thick_metatype", ValueKind::ObjCToThickMetatypeInst)
1222-
.Case("open_existential_addr", ValueKind::OpenExistentialAddrInst)
1223-
.Case("open_existential_box", ValueKind::OpenExistentialBoxInst)
1224-
.Case("open_existential_metatype", ValueKind::OpenExistentialMetatypeInst)
1225-
.Case("open_existential_ref", ValueKind::OpenExistentialRefInst)
1226-
.Case("partial_apply", ValueKind::PartialApplyInst)
1227-
.Case("pointer_to_address", ValueKind::PointerToAddressInst)
1228-
.Case("pointer_to_thin_function", ValueKind::PointerToThinFunctionInst)
1229-
.Case("project_block_storage", ValueKind::ProjectBlockStorageInst)
1230-
.Case("project_box", ValueKind::ProjectBoxInst)
1231-
.Case("project_existential_box", ValueKind::ProjectExistentialBoxInst)
1232-
.Case("project_value_buffer", ValueKind::ProjectValueBufferInst)
1233-
.Case("existential_metatype", ValueKind::ExistentialMetatypeInst)
1234-
.Case("raw_pointer_to_ref", ValueKind::RawPointerToRefInst)
1235-
.Case("ref_element_addr", ValueKind::RefElementAddrInst)
1236-
.Case("ref_tail_addr", ValueKind::RefTailAddrInst)
1237-
.Case("ref_to_bridge_object", ValueKind::RefToBridgeObjectInst)
1238-
.Case("ref_to_raw_pointer", ValueKind::RefToRawPointerInst)
1239-
.Case("ref_to_unmanaged", ValueKind::RefToUnmanagedInst)
1240-
.Case("ref_to_unowned", ValueKind::RefToUnownedInst)
1241-
.Case("retain_value", ValueKind::RetainValueInst)
1242-
.Case("alloc_global", ValueKind::AllocGlobalInst)
1243-
.Case("global_addr", ValueKind::GlobalAddrInst)
1244-
.Case("strong_pin", ValueKind::StrongPinInst)
1245-
.Case("strong_release", ValueKind::StrongReleaseInst)
1246-
.Case("strong_retain", ValueKind::StrongRetainInst)
1247-
.Case("strong_retain_unowned", ValueKind::StrongRetainUnownedInst)
1248-
.Case("strong_unpin", ValueKind::StrongUnpinInst)
1249-
.Case("return", ValueKind::ReturnInst)
1250-
.Case("select_enum", ValueKind::SelectEnumInst)
1251-
.Case("select_enum_addr", ValueKind::SelectEnumAddrInst)
1252-
.Case("select_value", ValueKind::SelectValueInst)
1253-
.Case("set_deallocating", ValueKind::SetDeallocatingInst)
1254-
.Case("store", ValueKind::StoreInst)
1255-
.Case("store_unowned", ValueKind::StoreUnownedInst)
1256-
.Case("store_weak", ValueKind::StoreWeakInst)
1257-
.Case("string_literal", ValueKind::StringLiteralInst)
1258-
.Case("struct", ValueKind::StructInst)
1259-
.Case("struct_element_addr", ValueKind::StructElementAddrInst)
1260-
.Case("struct_extract", ValueKind::StructExtractInst)
1261-
.Case("super_method", ValueKind::SuperMethodInst)
1262-
.Case("switch_enum", ValueKind::SwitchEnumInst)
1263-
.Case("switch_enum_addr",
1264-
ValueKind::SwitchEnumAddrInst)
1265-
.Case("switch_value", ValueKind::SwitchValueInst)
1266-
.Case("tail_addr", ValueKind::TailAddrInst)
1267-
.Case("try_apply", ValueKind::TryApplyInst)
1268-
.Case("unchecked_enum_data", ValueKind::UncheckedEnumDataInst)
1269-
.Case("unchecked_addr_cast", ValueKind::UncheckedAddrCastInst)
1270-
.Case("unchecked_trivial_bit_cast", ValueKind::UncheckedTrivialBitCastInst)
1271-
.Case("unchecked_bitwise_cast", ValueKind::UncheckedBitwiseCastInst)
1272-
.Case("unchecked_ref_cast", ValueKind::UncheckedRefCastInst)
1273-
.Case("unchecked_ref_cast_addr", ValueKind::UncheckedRefCastAddrInst)
1274-
.Case("unchecked_take_enum_data_addr", ValueKind::UncheckedTakeEnumDataAddrInst)
1275-
.Case("thick_to_objc_metatype", ValueKind::ThickToObjCMetatypeInst)
1276-
.Case("thin_function_to_pointer", ValueKind::ThinFunctionToPointerInst)
1277-
.Case("thin_to_thick_function", ValueKind::ThinToThickFunctionInst)
1278-
.Case("throw", ValueKind::ThrowInst)
1279-
.Case("tuple", ValueKind::TupleInst)
1280-
.Case("tuple_element_addr", ValueKind::TupleElementAddrInst)
1281-
.Case("tuple_extract", ValueKind::TupleExtractInst)
1282-
.Case("unconditional_checked_cast", ValueKind::UnconditionalCheckedCastInst)
1283-
.Case("unconditional_checked_cast_addr",
1284-
ValueKind::UnconditionalCheckedCastAddrInst)
1285-
.Case("unmanaged_to_ref", ValueKind::UnmanagedToRefInst)
1286-
.Case("unowned_retain", ValueKind::UnownedRetainInst)
1287-
.Case("unowned_release", ValueKind::UnownedReleaseInst)
1288-
.Case("unowned_to_ref", ValueKind::UnownedToRefInst)
1289-
.Case("unreachable", ValueKind::UnreachableInst)
1290-
.Case("upcast", ValueKind::UpcastInst)
1291-
.Default(ValueKind::SILArgument);
1153+
#define INST(Id, Parent, TextualName, MemBehavior, MayRelease) \
1154+
.Case(#TextualName, ValueKind::Id)
1155+
#include "swift/SIL/SILNodes.def"
1156+
.Default(ValueKind::SILArgument);
12921157

12931158
if (Opcode != ValueKind::SILArgument) {
12941159
P.consumeToken();

lib/SIL/SILInstruction.cpp

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -673,12 +673,12 @@ namespace {
673673
ArrayRef<Operand> visit##CLASS(const CLASS *I) { \
674674
llvm_unreachable("accessing non-instruction " #CLASS); \
675675
}
676-
#define INST(CLASS, PARENT, MEMBEHAVIOR, RELEASINGBEHAVIOR) \
677-
ArrayRef<Operand> visit##CLASS(const CLASS *I) { \
678-
ASSERT_IMPLEMENTS(CLASS, SILInstruction, getAllOperands, \
679-
ArrayRef<Operand>() const); \
680-
return I->getAllOperands(); \
681-
}
676+
#define INST(CLASS, PARENT, TEXTUALNAME, MEMBEHAVIOR, RELEASINGBEHAVIOR) \
677+
ArrayRef<Operand> visit##CLASS(const CLASS *I) { \
678+
ASSERT_IMPLEMENTS(CLASS, SILInstruction, getAllOperands, \
679+
ArrayRef<Operand>() const); \
680+
return I->getAllOperands(); \
681+
}
682682
#include "swift/SIL/SILNodes.def"
683683
};
684684

@@ -690,12 +690,12 @@ namespace {
690690
MutableArrayRef<Operand> visit##CLASS(const CLASS *I) { \
691691
llvm_unreachable("accessing non-instruction " #CLASS); \
692692
}
693-
#define INST(CLASS, PARENT, MEMBEHAVIOR, RELEASINGBEHAVIOR) \
694-
MutableArrayRef<Operand> visit##CLASS(CLASS *I) { \
695-
ASSERT_IMPLEMENTS(CLASS, SILInstruction, getAllOperands, \
696-
MutableArrayRef<Operand>()); \
697-
return I->getAllOperands(); \
698-
}
693+
#define INST(CLASS, PARENT, TEXTUALNAME, MEMBEHAVIOR, RELEASINGBEHAVIOR) \
694+
MutableArrayRef<Operand> visit##CLASS(CLASS *I) { \
695+
ASSERT_IMPLEMENTS(CLASS, SILInstruction, getAllOperands, \
696+
MutableArrayRef<Operand>()); \
697+
return I->getAllOperands(); \
698+
}
699699
#include "swift/SIL/SILNodes.def"
700700
};
701701

@@ -711,13 +711,13 @@ namespace {
711711
ArrayRef<Operand> visit##CLASS(const CLASS *I) { \
712712
llvm_unreachable("accessing non-instruction " #CLASS); \
713713
}
714-
#define INST(CLASS, PARENT, MEMBEHAVIOR, RELEASINGBEHAVIOR) \
715-
ArrayRef<Operand> visit##CLASS(const CLASS *I) { \
716-
if (!IMPLEMENTS_METHOD(CLASS, SILInstruction, getTypeDependentOperands, \
717-
ArrayRef<Operand>() const)) \
718-
return {}; \
719-
return I->getTypeDependentOperands(); \
720-
}
714+
#define INST(CLASS, PARENT, TEXTUALNAME, MEMBEHAVIOR, RELEASINGBEHAVIOR) \
715+
ArrayRef<Operand> visit##CLASS(const CLASS *I) { \
716+
if (!IMPLEMENTS_METHOD(CLASS, SILInstruction, getTypeDependentOperands, \
717+
ArrayRef<Operand>() const)) \
718+
return {}; \
719+
return I->getTypeDependentOperands(); \
720+
}
721721
#include "swift/SIL/SILNodes.def"
722722
};
723723

@@ -729,13 +729,13 @@ namespace {
729729
MutableArrayRef<Operand> visit##CLASS(const CLASS *I) { \
730730
llvm_unreachable("accessing non-instruction " #CLASS); \
731731
}
732-
#define INST(CLASS, PARENT, MEMBEHAVIOR, RELEASINGBEHAVIOR) \
733-
MutableArrayRef<Operand> visit##CLASS(CLASS *I) { \
734-
if (!IMPLEMENTS_METHOD(CLASS, SILInstruction, getTypeDependentOperands, \
735-
MutableArrayRef<Operand>())) \
736-
return {}; \
737-
return I->getTypeDependentOperands(); \
738-
}
732+
#define INST(CLASS, PARENT, TEXTUALNAME, MEMBEHAVIOR, RELEASINGBEHAVIOR) \
733+
MutableArrayRef<Operand> visit##CLASS(CLASS *I) { \
734+
if (!IMPLEMENTS_METHOD(CLASS, SILInstruction, getTypeDependentOperands, \
735+
MutableArrayRef<Operand>())) \
736+
return {}; \
737+
return I->getTypeDependentOperands(); \
738+
}
739739
#include "swift/SIL/SILNodes.def"
740740
};
741741
} // end anonymous namespace
@@ -799,8 +799,9 @@ SILInstruction::MemoryBehavior SILInstruction::getMemoryBehavior() const {
799799
}
800800

801801
switch (getKind()) {
802-
#define INST(CLASS, PARENT, MEMBEHAVIOR, RELEASINGBEHAVIOR) \
803-
case ValueKind::CLASS: return MemoryBehavior::MEMBEHAVIOR;
802+
#define INST(CLASS, PARENT, TEXTUALNAME, MEMBEHAVIOR, RELEASINGBEHAVIOR) \
803+
case ValueKind::CLASS: \
804+
return MemoryBehavior::MEMBEHAVIOR;
804805
#include "swift/SIL/SILNodes.def"
805806
case ValueKind::SILArgument:
806807
case ValueKind::SILUndef:
@@ -811,8 +812,9 @@ SILInstruction::MemoryBehavior SILInstruction::getMemoryBehavior() const {
811812

812813
SILInstruction::ReleasingBehavior SILInstruction::getReleasingBehavior() const {
813814
switch (getKind()) {
814-
#define INST(CLASS, PARENT, MEMBEHAVIOR, RELEASINGBEHAVIOR) \
815-
case ValueKind::CLASS: return ReleasingBehavior::RELEASINGBEHAVIOR;
815+
#define INST(CLASS, PARENT, TEXTUALNAME, MEMBEHAVIOR, RELEASINGBEHAVIOR) \
816+
case ValueKind::CLASS: \
817+
return ReleasingBehavior::RELEASINGBEHAVIOR;
816818
#include "swift/SIL/SILNodes.def"
817819
case ValueKind::SILArgument:
818820
case ValueKind::SILUndef:

lib/SIL/SILInstructions.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -938,10 +938,10 @@ bool StructExtractInst::isFieldOnlyNonTrivialField() const {
938938

939939

940940
TermInst::SuccessorListTy TermInst::getSuccessors() {
941-
#define TERMINATOR(TYPE, PARENT, EFFECT, RELEASING) \
942-
if (auto I = dyn_cast<TYPE>(this)) \
943-
return I->getSuccessors();
944-
#include "swift/SIL/SILNodes.def"
941+
#define TERMINATOR(TYPE, PARENT, TEXTUALNAME, EFFECT, RELEASING) \
942+
if (auto I = dyn_cast<TYPE>(this)) \
943+
return I->getSuccessors();
944+
#include "swift/SIL/SILNodes.def"
945945

946946
llvm_unreachable("not a terminator?!");
947947
}

0 commit comments

Comments
 (0)