x86 branches vs conditional moves
Michael Clark
michaeljclark@mac.com
Fri Jul 7 23:30:00 GMT 2017
Hi,
Curious about this codegen:
- https://godbolt.org/g/5XxP5S
Why does gcc branch on _Bool, but emits a conditional move for an integer? can it emit cmovne instead of branching? also curious where one would change this to learn about GCC internals.
It’s not a bug, but it is a performance issue (*1).
I was just curious under which conditions the ternary operator is lowered to cmov on x86 and found this difference in lowering.
Michael
[1] https://github.com/xiadz/cmov
#include <stdbool.h>
extern int a;
extern int b;
extern int c;
extern _Bool C;
int select_int()
{
return c ? a : b;
}
_Bool select_bool()
{
return C ? a : b;
}
_Bool a_bool()
{
return 2;
}
select_int():
mov eax, DWORD PTR c[rip]
test eax, eax
mov eax, DWORD PTR a[rip]
cmove eax, DWORD PTR b[rip]
ret
select_bool():
cmp BYTE PTR C[rip], 0
jne .L8
mov eax, DWORD PTR b[rip]
test eax, eax
setne al
ret
.L8:
mov edx, DWORD PTR a[rip]
test edx, edx
setne al
ret
a_bool():
mov eax, 1
ret
More information about the Gcc
mailing list