From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id DDDC63840C10; Sat, 27 Jun 2020 15:57:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DDDC63840C10 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1593273444; bh=iTlz07goXVzgsu/73wvqcqog+bNyfkEa5mV+ShewHlk=; h=From:To:Subject:Date:In-Reply-To:References:From; b=hD0l8rvQ8K3qyuB/VvbYwlM2vVPMh0IH0XKQTBLyPENpNY/8lNFo1idZOk30PxwLL 2ToodQHwxLADFLtxHMlhGj0WSlYCxYb0H6ra8NpEoEpzB4fgQrYtlJvkaK/dHgQ5tI pApB+xNY4/EOaufojCDOWqrLs15wk4oeSt1gOV04= From: "jakub 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: Sat, 27 Jun 2020 15:57:24 +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: jakub 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: 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: Sat, 27 Jun 2020 15:57:25 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D95663 --- Comment #15 from Jakub Jelinek --- In the particular case we are talking about, security/non-security, it does= n't make sense to do anything but optimize it into straight line code, any __builtin_trap or similar will just hurt. If you feel e.g. by default addi= ng __builtin_unreachable is too dangerous in some cases, it can just handle similar cases manually and optimize away the conditional if there are no side-effect statements in between. We are talking about: [local count: 1073741824]: if (base_2(D) !=3D 0B) goto ; [70.00%] else goto ; [30.00%] [local count: 751619281]: iftmp.1_3 =3D base_2(D) + (sizetype)-4; [local count: 1073741824]: # iftmp.1_1 =3D PHI _5 =3D MEM[(const struct Derived *)iftmp.1_1].D.2340.y; where without -fno-delete-null-pointer-checks and without -fwrapv-pointer, = we can assume: 1) pointers in valid programs don't wrap 2) the first page is n= ot mapped As offsetof (Derived, D.2340.y) is >=3D 4 and < 4096 here, we don't need to= even care about pointer wrapping, just rely on accesses to 0 .. 4095 offsets to trap. If the offsetof would be 0, it would be about pointer wrapping, whether we = are ok if instead of dereferencing *(int *)0 we dereference *(int *)-4 instead.=