From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id A0CBC3858D37; Wed, 19 Apr 2023 12:39:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A0CBC3858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1681907961; bh=Ly0rLnQR8vCnGPdRklJYJq1KtryT7FBlclX6hEG5DLw=; h=From:To:Subject:Date:From; b=JUfK4VB5mgRjYJjWmjgP85LFWAbXSHVflnshNgN81HjGUPMrWs5s1hc0Pn6N+nxFZ ugaj6URda4s2tohayiQ/8hcy45mbZnJxRnboN3fIvJPxVy2syMK1swB87bnX5uoQkC Zj+Uoenmz5MsXmHgukazKcxAohg2eo7MxwOQzzjI= From: "i.nixman at autistici dot org" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/109555] New: suboptimal code for comparing strings with string literals Date: Wed, 19 Apr 2023 12:39:21 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: i.nixman at autistici dot org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D109555 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 int main(int argc, char **argv) { return std::memcmp(argv[0], "MCRYF", 5) =3D=3D 0; } this code compiled with GCC (-std=3Dc++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 imp= rove this... thanks!=