[C++] Omit overflow check for new char[n]

Jakub Jelinek jakub@redhat.com
Wed Oct 31 09:12:00 GMT 2012


On Mon, Oct 08, 2012 at 05:50:34PM +0200, Florian Weimer wrote:
> gcc/:
> 
> 2012-10-08  Florian Weimer  <fweimer@redhat.com>
> 
> 	* init.c (build_new_1): Do not check for arithmetic overflow if
> 	inner array size is 1.
> 
> gcc/testsuite/:
> 
> 2012-10-08  Florian Weimer  <fweimer@redhat.com>
> 
> 	* g++.dg/init/new40.C: New.

> @@ -2450,7 +2450,13 @@
>  	  if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
>  	    size = size_binop (PLUS_EXPR, size, cookie_size);
>  	  else
> -	    cookie_size = NULL_TREE;
> +	    {
> +	      cookie_size = NULL_TREE;
> +	      /* No size arithmetic necessary, so the size check is
> +		 not needed. */
> +	      if (outer_nelts_check != NULL && inner_size == double_int_one)
> +		outer_nelts_check = NULL_TREE;
> +	    }

I don't see any == double_int_one (or zero) comparisons in grep,
so I'd say inner_size.is_one () should be used instead (which is used
pretty frequently).  Ditto in the second spot.
Otherwise the patch looks good to me, but I'd like Jason to chime in too.

>  	  /* Perform the overflow check.  */
>  	  if (outer_nelts_check != NULL_TREE)
>              size = fold_build3 (COND_EXPR, sizetype, outer_nelts_check,
> @@ -2486,7 +2492,13 @@
>  	  /* Use a global operator new.  */
>  	  /* See if a cookie might be required.  */
>  	  if (!(array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type)))
> -	    cookie_size = NULL_TREE;
> +	    {
> +	      cookie_size = NULL_TREE;
> +	      /* No size arithmetic necessary, so the size check is
> +		 not needed. */
> +	      if (outer_nelts_check != NULL && inner_size == double_int_one)
> +		outer_nelts_check = NULL_TREE;
> +	    }
>  
>  	  alloc_call = build_operator_new_call (fnname, placement,
>  						&size, &cookie_size,

	Jakub



More information about the Gcc-patches mailing list