From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id CB9B93851C08; Sat, 13 Jun 2020 10:52:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CB9B93851C08 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1592045578; bh=PwkhZVqb2VWl4/9IJv29Xb28NuFtslb6BYMNehLTJkA=; h=From:To:Subject:Date:From; b=Uc++mN/crHu7avE7ipy5h0YXFd3P9fmbkN2y1ntk+P6kmO9ZdHd7Z5VNvCycduAB9 3bSsu+MIvZInjGvUBhGztnfqRVHvtolQ4MTtwQ8e31m0fTbz7OCh86sOraY+Cg0VPI TIJXI/m2pkiDbCPsapcZ3T9eAZ0QNvgSJnZTRCDk= From: "jzwinck at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/95663] New: static_cast checks for null even when the pointer is dereferenced Date: Sat, 13 Jun 2020 10:52:58 +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: jzwinck 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 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: Sat, 13 Jun 2020 10:52:58 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D95663 Bug ID: 95663 Summary: static_cast checks for null even when the pointer is dereferenced Product: gcc Version: 10.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: jzwinck at gmail dot com Target Milestone: --- Consider this C++ code: struct Base1 { int x; }; struct Base2 { int y; }; struct Derived : Base1, Base2 { int get_y() const { return y; } }; int field(Base2* base) { return static_cast(base)->y; } int getter(Base2* base) { return static_cast(base)->get_y(); } Both field() and getter() produce this with -O2 or -O3: test rdi, rdi je .L2 mov eax, DWORD PTR [rdi] ret .L2: mov eax, DWORD PTR ds:4 ud2 That's fair, it traps if we dereference a null pointer. But I need the best performance and don't want the null check, so I add -fno-isolate-erroneous-paths-dereference and see: lea rax, [rdi-4] test rdi, rdi cmovne rdi, rax mov eax, DWORD PTR [rdi+4] ret If I read that correctly, it checks if the pointer is null so it can dereference 0x4 instead of 0x0. That's hardly an improvement over the naive and optimal code: mov eax, DWORD PTR [rdi] ret Which is what Clang generates for field() in all versions through 10, and f= or getter() up to 3.6 (3.7 through 10 generate a cmovne like GCC with no-isola= te). I tried adding __attribute__((nonnull)) to the function declarations, but it didn't help. Live demo: https://godbolt.org/z/XnhZoz=