From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 67967385DC35; Mon, 15 Jun 2020 06:47:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 67967385DC35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1592203649; bh=mjNAdEyVhhPKNilnUoftyaJ81MpoRa5njnIfrOu4A6c=; h=From:To:Subject:Date:In-Reply-To:References:From; b=XniIaW9FcxLveYQkfxUGTOe/v6Mvd2zCy4yzWUS+NX0rv3NFCEGlp2e17J7d07TBl OTQhtmYjUSyFzTFxlBbV158fZrYNJuoirMGn4ZIzKjN1qVNSYb1cvXIz1KM7FnRPcg UQChILrax8l5J8IuX9FhmSOViMG8orhKCPdjXA/U= From: "rguenth 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: Mon, 15 Jun 2020 06:47:29 +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: rguenth at gcc dot gnu.org 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: cc component keywords cf_gcctarget 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: Mon, 15 Jun 2020 06:47:29 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D95663 Richard Biener changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |law at gcc dot gnu.org, | |rguenth at gcc dot gnu.org Component|c++ |tree-optimization Keywords| |missed-optimization Target| |x86_64-*-* i?86-*-* --- Comment #1 from Richard Biener --- I suppose the C++ standard says static_cast(nullptr) =3D=3D null= ptr and we literally follow that. Note it will make a difference for very large objects (and thus very large offsets added) which may end up acccessing actually mapped memory so IMHO what clang does by default is a security risk. Now, what we should eventually improve is the code generated in the isolated path. On GIMPLE we retain the load: [count: 0]: _7 =3D{v} MEM[(struct Derived *)0B].D.2340.y; __builtin_trap (); (because it can trap). The use of the cold section for the ud2 is probably also bad since it will cause a larger jump instruction where very likely testq %rdi, %rdi jne .L2 ud2 .L2: movl (%rdi), ... would be both faster and smaller. For reference the generated code: _Z5fieldP5Base2: .LFB1: .cfi_startproc testq %rdi, %rdi je .L2 movl (%rdi), %eax ret .cfi_endproc .section .text.unlikely .cfi_startproc .type _Z5fieldP5Base2.cold, @function _Z5fieldP5Base2.cold: .LFSB1: .L2: movl 4, %eax ud2 CCing Jeff for the RTL side representation - IIRC we have some special CFG magic for gcc_unreachable, not sure if what we end up with trap() matches that or if we should adjust this somehow. Currently DCE marks the load as always necessary because it seems isolate-paths makes the load volatile: /* We want the NULL pointer dereference to actually occur so that code that wishes to catch the signal can do so. ... fair enough - but as you see above we dereference not NULL but some derived constant which might not actually trap. I wonder if it is more useful/safe to replace the load with a plain *(char *)0? Note I don't think what clang does here is reasonable.=