<html>
<head>
<base href="http://llvm.org/bugs/" />
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW --- - Integrate multiple switch statements into one"
href="http://llvm.org/bugs/show_bug.cgi?id=17344">17344</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Integrate multiple switch statements into one
</td>
</tr>
<tr>
<th>Product</th>
<td>libraries
</td>
</tr>
<tr>
<th>Version</th>
<td>3.2
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>Scalar Optimizations
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>fuzxxl@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvmbugs@cs.uiuc.edu
</td>
</tr>
<tr>
<th>Classification</th>
<td>Unclassified
</td>
</tr></table>
<p>
<div>
<pre>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,@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",@progbits
.align 8
.LJTI0_0:
.quad .LBB0_12
.quad .LBB0_5
.quad .LBB0_6
.quad .LBB0_7
.quad .LBB0_8
.section ".note.GNU-stack","",@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.</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>