From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id C31253851C26; Fri, 29 May 2020 08:38:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C31253851C26 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1590741535; bh=kH2HeXKNV2npTMo0w+rNxHLr2wJcmnh1ZI0vF7WxbUQ=; h=From:To:Subject:Date:From; b=mBeCWJXPwwWptq7VKFcovmPiM8Md3IBPqNaUN+4dUiafnhzhCX/aGHQkDV0NgNm1Z F+6CXsEkFbAcfcTWGjrO2vj20bcxhhbP3Ms1II0UzOXBo/KmlVBfR3R+A8Lf7n1Y4k +8JdqQ9ipZ2dN8MROZWa1cnIDW2rKO/C1ujiCBcY= From: "steffen.hirschmann at ipvs dot uni-stuttgart.de" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/95405] New: Unnecessary stores with std::optional Date: Fri, 29 May 2020 08:38:55 +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.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: steffen.hirschmann at ipvs dot uni-stuttgart.de 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 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: Fri, 29 May 2020 08:38:55 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D95405 Bug ID: 95405 Summary: Unnecessary stores with std::optional Product: gcc Version: 10.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: steffen.hirschmann at ipvs dot uni-stuttgart.de Target Milestone: --- I posted this to the gcc-help mailing list a few days ago (https://gcc.gnu.org/pipermail/gcc-help/2020-May/138978.html). GCC produces stores that don't seem to be required for std::optional. Code: -------- #include std::optional foo(); long bar() { auto r =3D foo(); if (r) return *r; else return 0L; } -------- What gcc 10.1 with -std=3Dc++17 -O3 produces is: bar(): sub rsp, 24 call foo() mov QWORD PTR [rsp+8], rdx cmp BYTE PTR [rsp+8], 0 mov QWORD PTR [rsp], rax mov rax, QWORD PTR [rsp] jne .L1 xor eax, eax .L1: add rsp, 24 ret (see: https://godbolt.org/z/uHE6QB) I don't understand the stores (and loads) after the call to foo. They don't seem necessary to me. Marc Glisse pointed out (https://gcc.gnu.org/pipermail/gcc-help/2020-May/138982.html) that the first pair of store/load seems to be a tuning choice and can be removed with the correct tuning flags. What I expected is: mov QWORD PTR [rsp+8], rdx cmp BYTE PTR [rsp+8], 0 should be a compare/test directly of dl. And: mov QWORD PTR [rsp], rax mov rax, QWORD PTR [rsp] is not present at all. Can someone explain this behavior? Shouldn't the optimizer produce what I pointed out?=