代码:
#define OPPOSITE(c) (*((typeof(x) *)&(x)))
int foo(volatile int x)
{
OPPOSITE(x) = OPPOSITE(x) + OPPOSITE(x);
return x;
}
int bar(volatile int x)
{
OPPOSITE(x) = OPPOSITE(x) + OPPOSITE(x);
return x;
}
结果(-Os):
foo:
mov DWORD PTR [rsp-4], edi
mov eax, DWORD PTR [rsp-4]
mov edx, DWORD PTR [rsp-4]
add eax, edx
mov DWORD PTR [rsp-4], eax
mov eax, DWORD PTR [rsp-4]
ret
bar:
mov DWORD PTR [rsp-4], edi
mov eax, DWORD PTR [rsp-4]
add eax, eax
ret
或ARM gcc.(-O3
)
foo:
sub sp, sp, #8
str r0, [sp, #4]
ldr r3, [sp, #4]
ldr r2, [sp, #4]
add r3, r3, r2
str r3, [sp, #4]
ldr r0, [sp, #4]
add sp, sp, #8
bx lr
bar:
sub sp, sp, #8
str r0, [sp, #4]
ldr r0, [sp, #4]
lsl r0, r0, #1
add sp, sp, #8
bx lr
1条答案
按热度按时间1dkrff031#
您可以将代码替换为
并且有相同的效果,除了GCC,没有其他的C编译器会产生这种效果,所以我认为可以合理地说这是某种编译器bug。