https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129268
Bug ID: 129268
Summary: Wrong code at -O1 and above: reg-stack compensate_edge
emits post-asm fxch BEFORE asm goto jump_insn
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: jiangchangwu at smail dot nju.edu.cn
Target Milestone: ---
Compiler Explorer: https://godbolt.org/z/vxTzG4shG
*******************************************************************************
Program:
$ cat case.c
double f_b, main___trans_tmp_1, main_a = 3.0L;
int main() {
{
asm goto("fsub %%st(1), %%st\n\tjmp %l[L]"
: "+t"(main_a), "+u"(f_b)
:
:
: L);
L:
main___trans_tmp_1 = main_a + f_b;
}
return main___trans_tmp_1 ? 0 : 1;
}
This is reduced by Cvise.
*******************************************************************************
Command:
$ gcc -O0 case.c -o case && ./case; echo $?
> 0
$ gcc -O1 case.c -o case && ./case; echo $?
> 1
*******************************************************************************
Generated assembly (gcc -O1):
main:
movsd main_a(%rip), %xmm0
movsd f_b(%rip), %xmm1
movsd %xmm0, -8(%rsp)
fldl -8(%rsp)
movsd %xmm1, -8(%rsp)
fldl -8(%rsp)
fxch %st(1) # input fix for +t/+u: st(0)=3.0, st(1)=0.0
fxch %st(1) # BUG: compensate_edge EMIT_BEFORE cancels input fxch
fsub %st(1), %st # computes 0.0 - 3.0 = -3.0 instead of 3.0 - 0.0
jmp .L2
.L2:
fstpl -8(%rsp)
movsd -8(%rsp), %xmm1
fstpl -8(%rsp)
movsd -8(%rsp), %xmm0
movsd %xmm0, main_a(%rip)
movsd %xmm1, f_b(%rip)
addsd %xmm1, %xmm0
...