[cond-optab, trunk] fold &f != 0 better

Richard Guenther richard.guenther@gmail.com
Thu Apr 9 11:27:00 GMT 2009


On Thu, Apr 9, 2009 at 10:56 AM, Paolo Bonzini <bonzini@gnu.org> wrote:
> I noticed that a common failure on cond-optab (e500, m32r, mcore) was on
> pr27150-1.c.  This is a failure to optimize
>
> int g(int f)
> {
>  return (&f)!=0;
> }
>
> The reason is that there is more straight-line coding on cond-optab and
> this confuses combine, which is no longer able to optimize it.  But I
> decided to treat it as a missed tree optimization, fixing it with the
> following patch.
>
> I tested that the modified testcase fails on cond-optab without the
> patch, and passes with it.
>
> Bootstrap/regtest in progress, ok for trunk when it finishes?

Comments below

> Paolo
>
> 2009-04-09  Paolo Bonzini  <bonzini@gnu.org>
>
>        * fold-const.c (tree_single_nonzero_warnv_p): Always treat decls
>        for things others than variables or functions as nonzero.
>
> 2009-04-09  Paolo Bonzini  <bonzini@gnu.org>
>
>        * gcc.dg/pr27150-1.c: Change to a link test.
>
> Index: gcc/fold-const.c
> ===================================================================
> --- gcc/fold-const.c    (branch cond-optab)
> +++ gcc/fold-const.c    (working copy)
> @@ -15011,8 +15011,8 @@ tree_single_nonzero_warnv_p (tree t, boo
>          return false;
>
>        /* Weak declarations may link to NULL.  */
> -       if (VAR_OR_FUNCTION_DECL_P (base))
> -         return !DECL_WEAK (base);
> +       if (DECL_P (base))
> +         return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);

Hmm, I think this misses && flag_delete_null_pointer_checks, otherwise
a VAR_DECL may be at address zero.  I'm not sure if that would apply
for PARM_DECLs, LABEL_DECLs, etc.

Thus, the patch is ok if you add && flag_delete_null_pointer_checks to
the VAR_OR_FUNCTION_DECL_P case.

Thanks,
Richard.

>        /* Constants are never weak.  */
>        if (CONSTANT_CLASS_P (base))
> Index: gcc/testsuite/gcc.dg/pr27150-1.c
> ===================================================================
> --- gcc/testsuite/gcc.dg/pr27150-1.c    (branch cond-optab)
> +++ gcc/testsuite/gcc.dg/pr27150-1.c    (working copy)
> @@ -1,7 +1,9 @@
>  /* { dg-do compile } */
>  /* { dg-options "-O2" } */
> +extern int link_error ();
>  int g(int f)
>  {
> -  return (&f)!=0;
> +  int a = ((&f)!=0);
> +  if (a) link_error ();
> +  return a;
>  }
> -
>



More information about the Gcc-patches mailing list