http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56767
Bug #: 56767
Summary: gcc does not generate correct code with -O2
Classification: Unclassified
Product: gcc
Version: 4.7.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: [email protected]
ReportedBy: [email protected]
== How to repeat (on a 32-bit Linux machine) ==
With following code:
// begin of code
#include <stdio.h>
int round2(int n) {
int ret;
if (n == 0) { return 0; }
__asm__ __volatile__ ("bsr %0, %1\n\t"
:"=r"(ret): "r"(n));
ret = 1 << ret;
}
int main() {
int a;
scanf("%d", &a);
printf("%d\n", round2(a));
return 0;
}
// end of code
With -O0, we get correct result, but with -O2, the generated assembly is:
main:
.LFB12:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
andl $-16, %esp
subl $32, %esp
leal 28(%esp), %eax
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call __isoc99_scanf
movl 28(%esp), %eax
testl %eax, %eax
je .L6
#APP
# 6 "bit.c" 1
bsr %eax, %eax
# 0 "" 2
#NO_APP
.L6:
movl $0, 4(%esp) // ALWAYS output 0
movl $.LC1, (%esp)
call printf
xorl %eax, %eax
leave