On 01/02/2018 04:12 PM, Jakub Jelinek wrote:
Hi!
This patch improves code generated for:
struct A { int a; };
struct B { int b; };
struct C : A, B { int c; };
C *bar (B *b) { return &static_cast<C &>(*b); }
Unlike return static_cast<C *>(b); where b can be validly NULL, the
reference shouldn't bind to NULL, but we still emit
b ? b - 4 : 0. The following patch omits the non-NULL check except when
-fsanitize=null (or undefined) and when sanitizing makes sure such bugs are
diagnosed.
It's sad the optimizers don't know REFERENCE_TYPE (x) means x != NULL.
(or perhaps that's just a C++ semantic of REFERENCE_TYPE?). Do we
manage to elide the check if we eventually dereference the pointer?
(Not that that'd be an easy fix, but maybe worth a (new?) bug report.)
Your patch is fine, but could you add a test case to make sure the null
check is not there in the output assembly -- it'd be $cpu-of-choice
specific, of course.
nathan
--
Nathan Sidwell