Fix PR 106900: array-bounds warning inside simplify_builtin_call

Message ID 20230517005256.1718424-1-apinski@marvell.com
State Accepted
Headers
Series Fix PR 106900: array-bounds warning inside simplify_builtin_call |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Andrew Pinski May 17, 2023, 12:52 a.m. UTC
  The problem here is that VRP cannot figure out isize could not be 0
due to using integer_zerop. This patch removes the use of integer_zerop
and instead checks for 0 directly after converting the tree to
an unsigned HOST_WIDE_INT. This allows VRP to figure out isize is not 0
and `isize - 1` will always be >= 0.

This patch is just to avoid the warning that GCC could produce sometimes
and does not change any code generation or even VRP.

OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

gcc/ChangeLog:

	* tree-ssa-forwprop.cc (simplify_builtin_call): Check
	against 0 instead of calling integer_zerop.
---
 gcc/tree-ssa-forwprop.cc | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Richard Biener May 17, 2023, 6:29 a.m. UTC | #1
On Wed, May 17, 2023 at 2:54 AM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> The problem here is that VRP cannot figure out isize could not be 0
> due to using integer_zerop. This patch removes the use of integer_zerop
> and instead checks for 0 directly after converting the tree to
> an unsigned HOST_WIDE_INT. This allows VRP to figure out isize is not 0
> and `isize - 1` will always be >= 0.
>
> This patch is just to avoid the warning that GCC could produce sometimes
> and does not change any code generation or even VRP.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

OK.

Richard.

> gcc/ChangeLog:
>
>         * tree-ssa-forwprop.cc (simplify_builtin_call): Check
>         against 0 instead of calling integer_zerop.
> ---
>  gcc/tree-ssa-forwprop.cc | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/gcc/tree-ssa-forwprop.cc b/gcc/tree-ssa-forwprop.cc
> index 06f19868ade..0326e6733e8 100644
> --- a/gcc/tree-ssa-forwprop.cc
> +++ b/gcc/tree-ssa-forwprop.cc
> @@ -1231,14 +1231,14 @@ simplify_builtin_call (gimple_stmt_iterator *gsi_p, tree callee2)
>           tree size = gimple_call_arg (stmt2, 2);
>           /* Size must be a constant which is <= UNITS_PER_WORD and
>              <= the string length.  */
> -         if (TREE_CODE (size) != INTEGER_CST || integer_zerop (size))
> +         if (TREE_CODE (size) != INTEGER_CST)
>             break;
>
>           if (!tree_fits_uhwi_p (size))
>             break;
>
>           unsigned HOST_WIDE_INT sz = tree_to_uhwi (size);
> -         if (sz > UNITS_PER_WORD || sz >= slen)
> +         if (sz == 0 || sz > UNITS_PER_WORD || sz >= slen)
>             break;
>
>           tree ch = gimple_call_arg (stmt2, 1);
> --
> 2.31.1
>
  

Patch

diff --git a/gcc/tree-ssa-forwprop.cc b/gcc/tree-ssa-forwprop.cc
index 06f19868ade..0326e6733e8 100644
--- a/gcc/tree-ssa-forwprop.cc
+++ b/gcc/tree-ssa-forwprop.cc
@@ -1231,14 +1231,14 @@  simplify_builtin_call (gimple_stmt_iterator *gsi_p, tree callee2)
 	  tree size = gimple_call_arg (stmt2, 2);
 	  /* Size must be a constant which is <= UNITS_PER_WORD and
 	     <= the string length.  */
-	  if (TREE_CODE (size) != INTEGER_CST || integer_zerop (size))
+	  if (TREE_CODE (size) != INTEGER_CST)
 	    break;
 
 	  if (!tree_fits_uhwi_p (size))
 	    break;
 
 	  unsigned HOST_WIDE_INT sz = tree_to_uhwi (size);
-	  if (sz > UNITS_PER_WORD || sz >= slen)
+	  if (sz == 0 || sz > UNITS_PER_WORD || sz >= slen)
 	    break;
 
 	  tree ch = gimple_call_arg (stmt2, 1);