http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46076
Summary: [4.6 regression] constant propogation and compile-time math no longer happening versus 4.4 and 4.5 Product: gcc Version: 4.6.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end AssignedTo: unassig...@gcc.gnu.org ReportedBy: m...@use.net For the following code: #include <stdio.h> typedef unsigned char(*Calculable)(void); static unsigned char one() { return 1; } static unsigned char two() { return 2; } static void print(Calculable calculate) { printf("%d\n", calculate()); printf("+1: %d\n", calculate() + 1); } int main() { print(one); print(two); return 0; } GCC 4.4 (Ubuntu/Linaro 4.4.4-14ubuntu5) and 4.5(Ubuntu/Linaro 4.5.1-7ubuntu2), when using -O3 on amd64 using Ubuntu 10.10's packages, produce the following code: 00000000004005a0 <main>: 4005a0: 48 83 ec 08 sub rsp,0x8 4005a4: ba 01 00 00 00 mov edx,0x1 4005a9: be 00 07 40 00 mov esi,0x400700 4005ae: bf 01 00 00 00 mov edi,0x1 4005b3: 31 c0 xor eax,eax 4005b5: e8 ae fe ff ff call 400468 <__printf_...@plt> 4005ba: ba 02 00 00 00 mov edx,0x2 4005bf: be fc 06 40 00 mov esi,0x4006fc 4005c4: bf 01 00 00 00 mov edi,0x1 4005c9: 31 c0 xor eax,eax 4005cb: e8 98 fe ff ff call 400468 <__printf_...@plt> 4005d0: ba 02 00 00 00 mov edx,0x2 4005d5: be 00 07 40 00 mov esi,0x400700 4005da: bf 01 00 00 00 mov edi,0x1 4005df: 31 c0 xor eax,eax 4005e1: e8 82 fe ff ff call 400468 <__printf_...@plt> 4005e6: ba 03 00 00 00 mov edx,0x3 4005eb: be fc 06 40 00 mov esi,0x4006fc 4005f0: bf 01 00 00 00 mov edi,0x1 4005f5: 31 c0 xor eax,eax 4005f7: e8 6c fe ff ff call 400468 <__printf_...@plt> 4005fc: 31 c0 xor eax,eax 4005fe: 48 83 c4 08 add rsp,0x8 400602: c3 ret where the constants are correctly propogated and the compile-time math occurs. With Ubuntu 10.10's gcc-snapshot of 4.6 (Ubuntu 20101004-0ubuntu1) or an alternate 4.6 version (Sourcery G++ 2010.09-31), neither of these things happens, even with -fwhole-program: 0000000000400510 <_ZL3onev>: 400510: b8 01 00 00 00 mov eax,0x1 400515: c3 ret 400516: eb 08 jmp 400520 <_ZL3twov> 400518: 90 nop 400519: 90 nop 40051a: 90 nop 40051b: 90 nop 40051c: 90 nop 40051d: 90 nop 40051e: 90 nop 40051f: 90 nop 0000000000400520 <_ZL3twov>: 400520: b8 02 00 00 00 mov eax,0x2 400525: c3 ret 400526: eb 08 jmp 400530 <_ZL5printPFhvE> 400528: 90 nop 400529: 90 nop 40052a: 90 nop 40052b: 90 nop 40052c: 90 nop 40052d: 90 nop 40052e: 90 nop 40052f: 90 nop 0000000000400530 <_ZL5printPFhvE>: 400530: 53 push rbx 400531: 48 89 fb mov rbx,rdi 400534: ff d7 call rdi 400536: bf 74 06 40 00 mov edi,0x400674 40053b: 0f b6 f0 movzx esi,al 40053e: 31 c0 xor eax,eax 400540: e8 b3 fe ff ff call 4003f8 <pri...@plt> 400545: ff d3 call rbx 400547: 5b pop rbx 400548: 0f b6 f0 movzx esi,al 40054b: bf 70 06 40 00 mov edi,0x400670 400550: 31 c0 xor eax,eax 400552: 83 c6 01 add esi,0x1 400555: e9 9e fe ff ff jmp 4003f8 <pri...@plt> 40055a: eb 04 jmp 400560 <main> 40055c: 90 nop 40055d: 90 nop 40055e: 90 nop 40055f: 90 nop 0000000000400560 <main>: 400560: 48 83 ec 08 sub rsp,0x8 400564: bf 10 05 40 00 mov edi,0x400510 400569: e8 c2 ff ff ff call 400530 <_ZL5printPFhvE> 40056e: bf 20 05 40 00 mov edi,0x400520 400573: e8 b8 ff ff ff call 400530 <_ZL5printPFhvE> 400578: 31 c0 xor eax,eax 40057a: 48 83 c4 08 add rsp,0x8 40057e: c3 ret