Bug creation and email sending has been disabled, file new bugs at gcc.gnu.org/bugzilla
Bug 197 - Optimize final switch
Summary: Optimize final switch
Status: NEW
Alias: None
Product: GDC
Classification: Unclassified
Component: gdc (show other bugs)
Version: development
Hardware: All All
: --- enhancement
Assignee: Iain Buclaw
URL:
Depends on:
Blocks:
 
Reported: 2015-08-20 10:43 CEST by Johannes Pfau
Modified: 2015-08-20 10:43 CEST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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