[Bug inline-asm/58818] New: parameters optimized out using -O2

2013-10-21 Thread gonwan at gmail dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58818

Bug ID: 58818
   Summary: parameters optimized out using -O2
   Product: gcc
   Version: 4.8.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: inline-asm
  Assignee: unassigned at gcc dot gnu.org
  Reporter: gonwan at gmail dot com

# cat testasm2.c
#include 

int add3(int a, int b)
{
int res;
__asm__ __volatile__ ("movl 12(%%ebp), %%eax\n\t"
  "addl 8(%%ebp), %%eax"
  : "=a" (res)
);
return res;
}

int main()
{
int ret;
ret = add3(5, 6);
printf("ret3=%d\n", ret);
return 0;
}

# gcc testasm2.c -o testasm2
# ./testasm2
ret3=11
# gcc -O2 testasm2.c -o testasm2
# ./testasm2
ret3=-1078224411

Output of gcc -S -O2:
main:
.LFB23:
.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$16, %esp# <-- no parameter is passed after sub %esp.
#APP
# 6 "testasm2.c" 1
movl 12(%ebp), %eax
addl 8(%ebp), %eax
# 0 "" 2
#NO_APP
movl%eax, 8(%esp)
movl$.LC0, 4(%esp)
movl$1, (%esp)
call__printf_chk
xorl%eax, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc

No parameter is passed after sub %esp. gcc 4.6/4.7/4.8 can reproduce this.


[Bug inline-asm/58819] New: return value ignored using -O2

2013-10-21 Thread gonwan at gmail dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58819

Bug ID: 58819
   Summary: return value ignored using -O2
   Product: gcc
   Version: 4.8.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: inline-asm
  Assignee: unassigned at gcc dot gnu.org
  Reporter: gonwan at gmail dot com

# cat testasm.c
#include 

int add1(int a, int b)
{
__asm__ __volatile__ ("movl 12(%ebp), %eax\n\t"
  "addl 8(%ebp), %eax"
);
}

int add2(int a, int b)
{
__asm__ __volatile__ ("movl 12(%%ebp), %%eax\n\t"
  "addl 8(%%ebp), %%eax"
  : : : "%eax"
);
}

int main()
{
int ret;
ret = add1(1, 2);
printf("ret1=%d\n", ret);
ret = add2(3, 4);
printf("ret2=%d\n", ret);
return 0;
}

# gcc testasm.c -o testasm
# ./testasm
ret1=3
ret2=7
# gcc -O2 testasm.c -o testasm
# ./testasm
ret1=0
ret2=0

Output of gcc -S -O2:
main:
.LFB24:
.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$16, %esp
#APP
# 5 "testasm.c" 1
movl 12(%ebp), %eax
addl 8(%ebp), %eax
# 0 "" 2
#NO_APP
movl$0, 8(%esp)# <-- 0 is passed to print
movl$.LC0, 4(%esp)
movl$1, (%esp)
call__printf_chk
#APP
# 12 "testasm.c" 1
movl 12(%ebp), %eax
addl 8(%ebp), %eax
# 0 "" 2
#NO_APP
movl$0, 8(%esp)# <-- 0 is passed to print
movl$.LC1, 4(%esp)
movl$1, (%esp)
call__printf_chk
xorl%eax, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc

0 is passed to print, even %eax is added to clobber list. gcc 4.6/4.7/4.8 can
reproduce this.