From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 281263857C4F; Tue, 11 Aug 2020 17:45:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 281263857C4F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1597167910; bh=zzlYpgQC/knc0W9j2dH2uskPLtS/yXbHuZGk3UVaODo=; h=From:To:Subject:Date:From; b=Pl51tYnUpST67FHaiknF0cU1ADpIQnFmrQs38Wm1FLO+w5lcof7irrPEuy3K+OxGi ZF1Wrb7GxTSOMAZzvm/mt1MGPCMXwhXwKo/wC3NRxDFod35W37z9nM3Qfy9uzwMhpX vlPwT2VQJn6HlpPmFqzvgYRwT3J+r/D6e1SdqiIc= From: "remi.andruccioli at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/96573] New: [Regression] Regression in optimization on x86-64 with -O3 from GCC 9 to 10 Date: Tue, 11 Aug 2020 17:45:09 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 10.2.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: remi.andruccioli 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 attachments.created 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 11 Aug 2020 17:45:10 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D96573 Bug ID: 96573 Summary: [Regression] Regression in optimization on x86-64 with -O3 from GCC 9 to 10 Product: gcc Version: 10.2.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: remi.andruccioli at gmail dot com Target Milestone: --- Created attachment 49046 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D49046&action=3Dedit This file contains the source code of the function described in the report. Hello, I'd like to describe here what seems to be a regression/missed optimization since GCC 10. The host and target architecture is x86-64. I'm using Fedora 32 with Linux 5.7, but I could reproduce it on many other Linux platforms. I'm using GCC 10.2, but I could reproduce it with any GCC 10 minor version. Here is a function, written in pure ANSI C99: #include #include void * ReverseBytesOfPointer(void * const pointer) { const size_t maxIndex =3D sizeof(pointer) - 1; const uint8_t * const oldPointerPointer =3D (uint8_t*)&pointer; void *newPointer; uint8_t * const newPointerPointer =3D (uint8_t *)&newPointer; uint8_t i; for (i =3D 0; i <=3D maxIndex; ++i) { newPointerPointer[maxIndex - i] =3D oldPointerPointer[i]; } return newPointer; } What this function does is simply to reverse all the bytes of a pointer. It= is written in pure C99 and is extremely portable, as it works from 16-bit to 64-bit machines. (I wrote it and use it for embedded development and I'm ha= ppy with it). What makes it magical is that when compiled with -O3 (and -std=3Dc99 -Wall -Werror -Wextra), GCC 9 (yes, 9) is clever enough to deduce the intent of t= his function and compiles it all as: ReverseBytesOfPointer: mov rax, rdi bswap rax ret However, since GCC 10, the magic seems to have disappeared. This is the ASM code that is generated now, with the exact same command line invocation: ReverseBytesOfPointer: movq %rdi, %rax movb %dil, -1(%rsp) movzbl %ah, %edx shrq $56, %rax movb %dl, -2(%rsp) movq %rdi, %rdx shrq $16, %rdx movb %al, -8(%rsp) movb %dl, -3(%rsp) movq %rdi, %rdx shrq $24, %rdx movb %dl, -4(%rsp) movq %rdi, %rdx shrq $32, %rdx movb %dl, -5(%rsp) movq %rdi, %rdx shrq $40, %rdx movb %dl, -6(%rsp) movq %rdi, %rdx shrq $48, %rdx movb %dl, -7(%rsp) movq -8(%rsp), %rax ret For your convenience, I include here a link to a snippet on compiler-explor= er to show the comparison between versions 9.3 and 10.2. This snippet also includes a few unit tests: https://godbolt.org/z/YG1KPf Regards, Remi Andruccioli=