With most optimizations disabled (-O0) GCC compiles this function to the below assembly code:
int dummy_function(int x)
{
if (x == 1) {
return 1;
} else if (x == 2) {
return 2;
} else if (x == 3) {
return 3;
} else if (x == 4) {
return 4;
} else if (x == 5) {
return 5;
} else if (x == 6) {
return 6;
} else {
return 10;
}
}
0: 55 push rbp
1: 48 89 e5 mov rbp,rsp
4: 89 7d fc mov DWORD PTR [rbp-0x4],edi
7: 83 7d fc 01 cmp DWORD PTR [rbp-0x4],0x1
b: 75 07 jne 14 <test+0x14>
d: b8 01 00 00 00 mov eax,0x1
12: eb 46 jmp 5a <test+0x5a>
14: 83 7d fc 02 cmp DWORD PTR [rbp-0x4],0x2
18: 75 07 jne 21 <test+0x21>
1a: b8 02 00 00 00 mov eax,0x2
1f: eb 39 jmp 5a <test+0x5a>
21: 83 7d fc 03 cmp DWORD PTR [rbp-0x4],0x3
25: 75 07 jne 2e <test+0x2e>
27: b8 03 00 00 00 mov eax,0x3
2c: eb 2c jmp 5a <test+0x5a>
2e: 83 7d fc 04 cmp DWORD PTR [rbp-0x4],0x4
32: 75 07 jne 3b <test+0x3b>
34: b8 04 00 00 00 mov eax,0x4
39: eb 1f jmp 5a <test+0x5a>
3b: 83 7d fc 05 cmp DWORD PTR [rbp-0x4],0x5
3f: 75 07 jne 48 <test+0x48>
41: b8 05 00 00 00 mov eax,0x5
46: eb 12 jmp 5a <test+0x5a>
48: 83 7d fc 06 cmp DWORD PTR [rbp-0x4],0x6
4c: 75 07 jne 55 <test+0x55>
4e: b8 06 00 00 00 mov eax,0x6
53: eb 05 jmp 5a <test+0x5a>
55: b8 0a 00 00 00 mov eax,0xa
5a: 5d pop rbp
5b: c3 ret
Return instructions are replaced with a jmp to the last one even when non merge instructions option is used. How to prevent GCC from doing this optimization ?