Skip to content

Commit 4cb03c2

Browse files
committed
[diagnose-unreachable] Constant fold simple switch_enum_addr to eliminate more unreachable code.
This patch comes out of my reading some generic code using .none in transparent functions to conditionally compile out code at -Onone. Sadly, before this the dead code in question wouldn't be compiled out unless the protocol was constrained to be a class protocol. I added a test that validates that this conditional compilation property can be relied on in -Onone code in both cases.
1 parent 31cb832 commit 4cb03c2

File tree

4 files changed

+679
-136
lines changed

4 files changed

+679
-136
lines changed

include/swift/SIL/Projection.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -377,15 +377,15 @@ class Projection {
377377

378378
/// Returns true if this instruction projects from an address type to an
379379
/// address subtype.
380-
static SingleValueInstruction *isAddressProjection(SILValue V) {
381-
switch (V->getKind()) {
380+
static SingleValueInstruction *isAddressProjection(SILValue v) {
381+
switch (v->getKind()) {
382382
default:
383383
return nullptr;
384384
case ValueKind::IndexAddrInst: {
385-
auto I = cast<IndexAddrInst>(V);
386-
unsigned Scalar;
387-
if (getIntegerIndex(I->getIndex(), Scalar))
388-
return I;
385+
auto *i = cast<IndexAddrInst>(v);
386+
unsigned scalar;
387+
if (getIntegerIndex(i->getIndex(), scalar))
388+
return i;
389389
return nullptr;
390390
}
391391
case ValueKind::StructElementAddrInst:
@@ -394,10 +394,17 @@ class Projection {
394394
case ValueKind::ProjectBoxInst:
395395
case ValueKind::TupleElementAddrInst:
396396
case ValueKind::UncheckedTakeEnumDataAddrInst:
397-
return cast<SingleValueInstruction>(V);
397+
return cast<SingleValueInstruction>(v);
398398
}
399399
}
400400

401+
static SingleValueInstruction *isAddressProjection(SILInstruction *i) {
402+
auto *svi = dyn_cast<SingleValueInstruction>(i);
403+
if (!svi)
404+
return nullptr;
405+
return isAddressProjection(SILValue(svi));
406+
}
407+
401408
/// Returns true if this instruction projects from an object type to an object
402409
/// subtype.
403410
static SingleValueInstruction *isObjectProjection(SILValue V) {

0 commit comments

Comments
 (0)