From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 2B8A63858C20; Thu, 8 Jun 2023 11:31:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2B8A63858C20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1686223866; bh=ApNWdNNqqlMyYD/aN6aKPHxvHwsuAY86ZolHS1/yeZ0=; h=From:To:Subject:Date:From; b=wCk0Ln/GY54LdtU+uZ1MK8mC4WHc+RcE06wBwUcu9aeFtx0OY3LSdYOqaFZcISQ9E iycZWXdI3/TwT62yWBBvqk2xwo0m5z/Hk5WoVy+S9jmfPTNaOXhQvfxfjob8bg7l6Z e4LONLYihhnUZveGdvaggOL7N1W2V0IcAEd2NQ/Q= From: "antoshkka at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/110170] New: Sub-optimal conditional jumps in conditional-swap with floating point Date: Thu, 08 Jun 2023 11:31:05 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: antoshkka at gmail dot com 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 keywords 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=3D110170 Bug ID: 110170 Summary: Sub-optimal conditional jumps in conditional-swap with floating point Product: gcc Version: 14.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: antoshkka at gmail dot com Target Milestone: --- Some of the C++ algorithms are written in attempt to avoid conditional jump= s in tight loops. For example, code close the following could be seen in libc++: void __cond_swap(double* __x, double* __y) { bool __r =3D (*__x < *__y); auto __tmp =3D __r ? *__x : *__y; *__y =3D __r ? *__y : *__x; *__x =3D __tmp; } GCC-14 with -O2 and -march=3Dx86-64 options generates the following code: __cond_swap(double*, double*): movsd xmm1, QWORD PTR [rdi] movsd xmm0, QWORD PTR [rsi] comisd xmm0, xmm1 jbe .L2 movq rax, xmm1 movapd xmm1, xmm0 movq xmm0, rax .L2: movsd QWORD PTR [rsi], xmm1 movsd QWORD PTR [rdi], xmm0 ret A conditional jump could be probably avoided in the following way: __cond_swap(double*, double*): movsd xmm0, qword ptr [rdi] movsd xmm1, qword ptr [rsi] movapd xmm2, xmm0 minsd xmm2, xmm1 maxsd xmm1, xmm0 movsd qword ptr [rsi], xmm1 movsd qword ptr [rdi], xmm2 ret Playground: https://godbolt.org/z/v3jW67x91=