https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79265

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic, wrong-code
            Summary|[7 regression]              |-fsanitize=undefined
                   |-fsanitize=undefined        |inserts unnecessary null
                   |inserts unnecessary null    |pointer tests
                   |pointer tests               |

--- Comment #7 from Richard Biener <rguenth at gcc dot gnu.org> ---
The bogus optimization is done via

 (simplify
  (cmp (convert? addr@0) integer_zerop)
  (if (tree_single_nonzero_warnv_p (@0, NULL))
   { constant_boolean_node (cmp == NE_EXPR, type); })))

and

bool
tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
{
...
    case ADDR_EXPR:
      {
        tree base = TREE_OPERAND (t, 0);

        if (!DECL_P (base))
          base = get_base_address (base);

        if (base && TREE_CODE (base) == TARGET_EXPR)
          base = TARGET_EXPR_SLOT (base);

        if (!base)
          return false;

        /* For objects in symbol table check if we know they are non-zero.
           Don't do anything for variables and functions before symtab is
built;
           it is quite possible that they will be declared weak later.  */
        int nonzero_addr = maybe_nonzero_address (base);
        if (nonzero_addr >= 0)
          return nonzero_addr;

        /* Constants are never weak.  */
        if (CONSTANT_CLASS_P (base))
          return true;

        return false;

plus

static int
maybe_nonzero_address (tree decl)
{
  if (DECL_P (decl) && decl_in_symtab_p (decl))
    if (struct symtab_node *symbol = symtab_node::get_create (decl))
      return symbol->nonzero_address ();

  /* Function local objects are never NULL.  */
  if (DECL_P (decl)
      && (DECL_CONTEXT (decl)
      && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
      && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
    return 1;

so if we think that we have to handle the possibility of automatic vars
being at address zero with -fno-delete-null-pointer-checks we have to
guard that test.

More fine-grained control would be nice as well.

Reply via email to