From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 506B2386F406; Thu, 7 Jan 2021 13:14:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 506B2386F406 From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/95663] static_cast checks for null even when the pointer is dereferenced Date: Thu, 07 Jan 2021 13:14:26 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 10.1.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: NEW 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: everconfirmed cf_reconfirmed_on bug_status 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, 07 Jan 2021 13:14:27 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D95663 Jonathan Wakely changed: What |Removed |Added ---------------------------------------------------------------------------- Ever confirmed|0 |1 Last reconfirmed| |2021-01-07 Status|UNCONFIRMED |NEW --- Comment #17 from Jonathan Wakely --- Copied from the PR 98501 dup: Consider this code: struct base1 { int a; }; struct base2 { int b; }; struct derived : base1, base2 {}; derived& to_derived_bad(base2* b) { return *static_cast(b); } derived& to_derived_good(base2* b) { return static_cast(*b); } I believe both of these functions are functionally equivalent and should generate the same code. Both functions cast pointer from base to derived if= it is not nullptr and both cause undefined behavior if it is nullptr. GCC optimizes to_derived_good() to a single subtraction, but it inserts nullptr-check into to_derived_bad(): to_derived_good(base2*): lea rax, [rdi-4] ret to_derived_bad(base2*): lea rax, [rdi-4] test rdi, rdi mov edx, 0 cmove rax, rdx ret Could GCC omit the nullptr-check in to_derived_bad?=