Bug creation and email sending has been disabled, file new bugs at gcc.gnu.org/bugzilla

Bug 197

Summary: Optimize final switch
Product: GDC Reporter: Johannes Pfau <johannespfau>
Component: gdcAssignee: Iain Buclaw <ibuclaw>
Status: NEW ---    
Severity: enhancement    
Priority: ---    
Version: development   
Hardware: All   
OS: All   

Description Johannes Pfau 2015-08-20 10:43:36 CEST
D example:
---------------------------------------------------------
enum TestEnum
{
    a,
    b,
    c,
    d,
    e
}

int foo(TestEnum e)
{
    final switch(e)
    {
        case TestEnum.a:
            return 10;
        case TestEnum.b:
            return 11;
        case TestEnum.c:
            return 12;
        case TestEnum.d:
            return 13;
        case TestEnum.e:
            return 14;
    }
}
---------------------------------------------------------

Generates:
---------------------------------------------------------
    cmpl    $4, -4(%rbp)
    ja  .L2
    ; [jump table]
.L2:
    popq    %rbp
---------------------------------------------------------

For a normal switch, the cmpl is used to check if the enum value is not handled in the switch case, so the jump table can't be used (no index in the table for that value). For a final switch (in release mode) this can't happen and we could remove the check.

However, the gcc backend doesn't support optimizing this case:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49054

GDC- implementation (useless without backend support): https://github.com/D-Programming-GDC/GDC/pull/135