On Fri, 27 Nov 2020, Jakub Jelinek wrote:

> Hi!
> 
> My recent match.pd change required quite a lot of code due to the separate
> need to handle INTEGER_CSTs and SSA_NAMEs, and after all, I didn't even
> handle one case there, when in x * y / y the x is INTEGER_CST and y is
> SSA_NAME.
> The following patch allows to simplify it, by allowing non-SSA_NAME argument
> to get_range_info, for INTEGER_CSTs it will return VR_RANGE with *min == *max
> equal to the constnat, and for non-INTEGER_CST/SSA_NAMEs it will just return
> VR_VARYING.
> 
> This allows not to simplify just the match.pd, but some other spots too.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK.

Thanks,
Richard.

> 2020-11-27  Jakub Jelinek  <ja...@redhat.com>
> 
>       * tree-ssanames.c (get_range_info): Handle INTEGER_CST by returning
>       VR_RANGE with both *min and *max set to the wide_int value of the
>       INTEGER_CST.  Return VR_VARYING for non-SSA_NAMEs.
>       * match.pd ((t * 2) / 2) -> t): Handle also @0 being INTEGER_CST.
>       Simplify by calling get_range_info on everything.
>       * tree-ssa-strlen.c (maybe_diag_stxncpy_trunc): Simplify by calling
>       get_range_info on everything.
>       * tree-scalar-evolution.c (iv_can_overflow_p): Likewise.
> 
> --- gcc/tree-ssanames.c.jj    2020-11-23 17:01:52.002013364 +0100
> +++ gcc/tree-ssanames.c       2020-11-26 16:28:10.092549635 +0100
> @@ -420,21 +420,30 @@ set_range_info (tree name, const value_r
>     is used to determine if MIN and MAX are valid values.  */
>  
>  enum value_range_kind
> -get_range_info (const_tree name, wide_int *min, wide_int *max)
> +get_range_info (const_tree expr, wide_int *min, wide_int *max)
>  {
> -  gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
> +  gcc_assert (!POINTER_TYPE_P (TREE_TYPE (expr)));
>    gcc_assert (min && max);
> -  range_info_def *ri = SSA_NAME_RANGE_INFO (name);
> +  if (TREE_CODE (expr) == INTEGER_CST)
> +    {
> +      *min = wi::to_wide (expr);
> +      *max = *min;
> +      return VR_RANGE;
> +    }
> +  if (TREE_CODE (expr) != SSA_NAME)
> +    return VR_VARYING;
> +
> +  range_info_def *ri = SSA_NAME_RANGE_INFO (expr);
>  
>    /* Return VR_VARYING for SSA_NAMEs with NULL RANGE_INFO or SSA_NAMEs
>       with integral types width > 2 * HOST_BITS_PER_WIDE_INT precision.  */
> -  if (!ri || (GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (TREE_TYPE (name)))
> +  if (!ri || (GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (TREE_TYPE (expr)))
>             > 2 * HOST_BITS_PER_WIDE_INT))
>      return VR_VARYING;
>  
>    *min = ri->get_min ();
>    *max = ri->get_max ();
> -  return SSA_NAME_RANGE_TYPE (name);
> +  return SSA_NAME_RANGE_TYPE (expr);
>  }
>  
>  /* Gets range information corresponding to ssa_name NAME and stores it
> --- gcc/match.pd.jj   2020-11-26 16:23:55.037394625 +0100
> +++ gcc/match.pd      2020-11-26 16:42:11.527163758 +0100
> @@ -654,42 +654,30 @@ (define_operator_list COND_TERNARY
>     (if (TYPE_OVERFLOW_UNDEFINED (type))
>      @0
>  #if GIMPLE
> -    (if (TREE_CODE (@0) == SSA_NAME
> -      && (TREE_CODE (@1) == SSA_NAME || TREE_CODE (@1) == INTEGER_CST))
> -     (with
> -      {
> -     bool overflowed = true;
> -     wide_int wmin0, wmax0;
> -     if (get_range_info (@0, &wmin0, &wmax0) == VR_RANGE)
> -       {
> -         /* If the multiplication can't overflow/wrap around, then
> -            it can be optimized too.  */
> -         wide_int wmin1, wmax1;
> -         wi::overflow_type min_ovf, max_ovf;
> -         if (TREE_CODE (@1) == INTEGER_CST)
> -           {
> -             wmin1 = wi::to_wide (@1);
> -             wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
> -             wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
> -             if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
> -               overflowed = false;
> -           }
> -         else if (get_range_info (@1, &wmin1, &wmax1) == VR_RANGE)
> -           {
> -             wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
> -             wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
> -             if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
> -               {
> -                 wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
> -                 wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
> -                 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
> -                   overflowed = false;
> -               }
> -           }
> -       }
> -      }
> -     (if (!overflowed)
> -      @0)))
> +    (with
> +     {
> +       bool overflowed = true;
> +       wide_int wmin0, wmax0, wmin1, wmax1;
> +       if (INTEGRAL_TYPE_P (type)
> +        && get_range_info (@0, &wmin0, &wmax0) == VR_RANGE
> +        && get_range_info (@1, &wmin1, &wmax1) == VR_RANGE)
> +      {
> +        /* If the multiplication can't overflow/wrap around, then
> +           it can be optimized too.  */
> +        wi::overflow_type min_ovf, max_ovf;
> +        wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
> +        wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
> +        if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
> +          {
> +            wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
> +            wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
> +            if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
> +              overflowed = false;
> +          }
> +      }
> +     }
> +    (if (!overflowed)
> +     @0))
>  #endif
>     ))))
>  
> --- gcc/tree-ssa-strlen.c.jj  2020-11-18 09:40:09.782661182 +0100
> +++ gcc/tree-ssa-strlen.c     2020-11-26 16:38:02.633940185 +0100
> @@ -3038,31 +3038,24 @@ maybe_diag_stxncpy_trunc (gimple_stmt_it
>  
>    wide_int cntrange[2];
>  
> -  if (TREE_CODE (cnt) == INTEGER_CST)
> -    cntrange[0] = cntrange[1] = wi::to_wide (cnt);
> -  else if (TREE_CODE (cnt) == SSA_NAME)
> +  // FIXME: Use range_query instead of global ranges.
> +  enum value_range_kind rng = get_range_info (cnt, cntrange, cntrange + 1);
> +  if (rng == VR_RANGE)
> +    ;
> +  else if (rng == VR_ANTI_RANGE)
>      {
> -      // FIXME: Use range_query instead of global ranges.
> -      enum value_range_kind rng = get_range_info (cnt, cntrange, cntrange + 
> 1);
> -      if (rng == VR_RANGE)
> -     ;
> -      else if (rng == VR_ANTI_RANGE)
> -     {
> -       wide_int maxobjsize = wi::to_wide (TYPE_MAX_VALUE 
> (ptrdiff_type_node));
> +      wide_int maxobjsize = wi::to_wide (TYPE_MAX_VALUE (ptrdiff_type_node));
>  
> -       if (wi::ltu_p (cntrange[1], maxobjsize))
> -         {
> -           cntrange[0] = cntrange[1] + 1;
> -           cntrange[1] = maxobjsize;
> -         }
> -       else
> -         {
> -           cntrange[1] = cntrange[0] - 1;
> -           cntrange[0] = wi::zero (TYPE_PRECISION (TREE_TYPE (cnt)));
> -         }
> +      if (wi::ltu_p (cntrange[1], maxobjsize))
> +     {
> +       cntrange[0] = cntrange[1] + 1;
> +       cntrange[1] = maxobjsize;
>       }
>        else
> -     return false;
> +     {
> +       cntrange[1] = cntrange[0] - 1;
> +       cntrange[0] = wi::zero (TYPE_PRECISION (TREE_TYPE (cnt)));
> +     }
>      }
>    else
>      return false;
> --- gcc/tree-scalar-evolution.c.jj    2020-01-12 11:54:38.500381922 +0100
> +++ gcc/tree-scalar-evolution.c       2020-11-26 16:35:24.873699912 +0100
> @@ -3043,22 +3043,12 @@ iv_can_overflow_p (class loop *loop, tre
>    if (integer_zerop (step))
>      return false;
>  
> -  if (TREE_CODE (base) == INTEGER_CST)
> -    base_min = base_max = wi::to_wide (base);
> -  else if (TREE_CODE (base) == SSA_NAME
> -        && INTEGRAL_TYPE_P (TREE_TYPE (base))
> -        && get_range_info (base, &base_min, &base_max) == VR_RANGE)
> -    ;
> -  else
> +  if (!INTEGRAL_TYPE_P (TREE_TYPE (base))
> +      || get_range_info (base, &base_min, &base_max) != VR_RANGE)
>      return true;
>  
> -  if (TREE_CODE (step) == INTEGER_CST)
> -    step_min = step_max = wi::to_wide (step);
> -  else if (TREE_CODE (step) == SSA_NAME
> -        && INTEGRAL_TYPE_P (TREE_TYPE (step))
> -        && get_range_info (step, &step_min, &step_max) == VR_RANGE)
> -    ;
> -  else
> +  if (!INTEGRAL_TYPE_P (TREE_TYPE (step))
> +      || get_range_info (step, &step_min, &step_max) != VR_RANGE)
>      return true;
>  
>    if (!get_max_loop_iterations (loop, &nit))
> 
>       Jakub
> 
> 

-- 
Richard Biener <rguent...@suse.de>
SUSE Software Solutions Germany GmbH, Maxfeldstrasse 5, 90409 Nuernberg,
Germany; GF: Felix Imend

Reply via email to