[LLVMbugs] [Bug 17344] New: Integrate multiple switch statements into one

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Tue Sep 24 08:00:08 PDT 2013


http://llvm.org/bugs/show_bug.cgi?id=17344

            Bug ID: 17344
           Summary: Integrate multiple switch statements into one
           Product: libraries
           Version: 3.2
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: fuzxxl at gmail.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

I sometimes have code like this:

static int foo(int a, char x) {
    switch (x) {
    case '0': return a + 0;
    case '1': return a + 1;
    case '2': return a + 4;
    case '3': return a + 9;
    case '4': return a +16;
    default: return -1;
    }
}

int bar(int a, char x) {
    switch(x) {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5': return foo(a,x);
    case '.': return a - 2;
    case '/': return a - 3;
    default: return -1;
    }
}

clang 3.2 compiles this code into

    .file    "x.c"
    .text
    .globl    bar
    .align    16, 0x90
    .type    bar, at function
bar:                                    # @bar
    .cfi_startproc
# BB#0:
                                        # kill: ESI<def> ESI<kill> RSI<def>
    cmpl    $47, %esi
    je    .LBB0_10
# BB#1:
    cmpl    $46, %esi
    je    .LBB0_9
# BB#2:
    movl    $-1, %eax
    addl    $-48, %esi
    cmpl    $5, %esi
    ja    .LBB0_12
# BB#3:
    cmpl    $4, %esi
    ja    .LBB0_12
# BB#4:
    movl    %edi, %eax
    jmpq    *.LJTI0_0(,%rsi,8)
.LBB0_5:
    incl    %edi
    jmp    .LBB0_11
.LBB0_10:
    addl    $-3, %edi
    jmp    .LBB0_11
.LBB0_9:
    addl    $-2, %edi
    jmp    .LBB0_11
.LBB0_6:
    addl    $4, %edi
    jmp    .LBB0_11
.LBB0_7:
    addl    $9, %edi
    jmp    .LBB0_11
.LBB0_8:
    addl    $16, %edi
.LBB0_11:
    movl    %edi, %eax
.LBB0_12:                               # %foo.exit
    ret
.Ltmp0:
    .size    bar, .Ltmp0-bar
    .cfi_endproc
    .section    .rodata,"a", at progbits
    .align    8
.LJTI0_0:
    .quad    .LBB0_12
    .quad    .LBB0_5
    .quad    .LBB0_6
    .quad    .LBB0_7
    .quad    .LBB0_8


    .section    ".note.GNU-stack","", at progbits

It would be nice if clang was capable of noticing that both switch statements
switch over the same variable, optimizing the code above something like this:


int bar(int a, char x) {
    switch(x) {
    case '0': return a + 0;
    case '1': return a + 1;
    case '2': return a + 4;
    case '3': return a + 9;
    case '4': return a +16;
    case '.': return a - 2;
    case '/': return a - 3;
    default: return -1;
    }
}

As far as I know, gcc is not yet capable of this optimization.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20130924/6ddb5a77/attachment.html>


More information about the llvm-bugs mailing list