From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 334493858413; Thu, 17 Mar 2022 20:49:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 334493858413 From: "andrew.cooper3 at citrix dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/104971] [9/10/11/12 Regression] Optimisation for __builtin_ia32_readeflags corrupts the stack Date: Thu, 17 Mar 2022 20:49:26 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: andrew.cooper3 at citrix dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: 9.5 X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: 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: Thu, 17 Mar 2022 20:49:26 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D104971 --- Comment #3 from Andrew Cooper --- So yes - my experimentation did start from investigating the memory ordering behaviour of these builtins, based on a thread on LKML. The pushf in readflags and popf in writeflags have wildly different ordering requirements, depending on which flags are wanted/modified. AC for example (and IF for kernels) need to not be reordered with respect to any memory access. As you observe, readflags in particular needs to not be reordered with any instruction that modifies the arithmetic flags (which is most of them). IMO, it would be safe to omit the pushf from readflags if the result is not= not used, because there are no unexpected side effects for pushf. The same is not true of popf in writeflags, which has side effects even when written twice with the same value.=