https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109555
Bug ID: 109555
Summary: suboptimal code for comparing strings with string
literals
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: i.nixman at autistici dot org
Target Milestone: ---
hello!
the example:
#include <cstring>
int main(int argc, char **argv) {
return std::memcmp(argv[0], "MCRYF", 5) == 0;
}
this code compiled with GCC (-std=c++11 -O2) will be translated as the
following assembler code (https://godbolt.org/z/zzjaGdWdT):
main:
mov rax, QWORD PTR [rsi]
cmp DWORD PTR [rax], 1498563405
je .L5
.L2:
mov eax, 1
.L3:
xor eax, 1
ret
.L5:
cmp BYTE PTR [rax+4], 70
jne .L2
xor eax, eax
jmp .L3
but if the code is compiled using CLang, we get a more optimal branch-less
assembler code (https://godbolt.org/z/E9o1oMzra):
main: # @main
mov rax, qword ptr [rsi]
mov ecx, 1498563405
xor ecx, dword ptr [rax]
movzx edx, byte ptr [rax + 4]
xor edx, 70
xor eax, eax
or edx, ecx
sete al
ret
maybe it's not a difficult fix, and maybe someone will see the point to improve
this...
thanks!