http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59521
Bug ID: 59521
Summary: __builtin_expect not effective in switch
Product: gcc
Version: 4.9.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: drepper.fsp at gmail dot com
When used in switch, __builtin_expect should reorder the comparisons
appropriately. Take this code:
#include <stdio.h>
void
f(int ch) {
switch (__builtin_expect(ch, 333)) {
case 3: puts("a"); break;
case 42: puts("e"); break;
case 333: puts("i"); break;
}
}
Current mainline (and also prior versions, I tested 4.8.2) produce with -O3
code like this:
0000000000000000 <f>:
0: 83 ff 2a cmp $0x2a,%edi
3: 74 33 je 38 <f+0x38>
5: 81 ff 4d 01 00 00 cmp $0x14d,%edi
b: 74 1b je 28 <f+0x28>
d: 83 ff 03 cmp $0x3,%edi
10: 74 06 je 18 <f+0x18>
12: c3 retq
13: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)
18: bf 00 00 00 00 mov $0x0,%edi
1d: e9 00 00 00 00 jmpq 22 <f+0x22>
22: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1)
28: bf 00 00 00 00 mov $0x0,%edi
2d: e9 00 00 00 00 jmpq 32 <f+0x32>
32: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1)
38: bf 00 00 00 00 mov $0x0,%edi
3d: e9 00 00 00 00 jmpq 42 <f+0x42>
Instead the test for 333/$0x14d should have been moved to the front.