middle-end: Move constant args folding of .UBSAN_CHECK_* and .*_OVERFLOW into fold-const-call.cc

Message ID ZInGN+99kNXeSQ+h@tucnak
State Unresolved
Headers
Series middle-end: Move constant args folding of .UBSAN_CHECK_* and .*_OVERFLOW into fold-const-call.cc |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Jakub Jelinek June 14, 2023, 1:52 p.m. UTC
  On Wed, Jun 14, 2023 at 12:25:46PM +0000, Richard Biener wrote:
> I think that's still very much desirable so this followup looks OK.
> Maybe you can re-base it as prerequesite though?

Rebased then (of course with the UADDC/USUBC handling removed from this
first patch, will be added in the second one).

Ok for trunk if it passes bootstrap/regtest?

2023-06-14  Jakub Jelinek  <jakub@redhat.com>

	* gimple-fold.cc (gimple_fold_call): Move handling of arg0
	as well as arg1 INTEGER_CSTs for .UBSAN_CHECK_{ADD,SUB,MUL}
	and .{ADD,SUB,MUL}_OVERFLOW calls from here...
	* fold-const-call.cc (fold_const_call): ... here.



	Jakub
  

Comments

Richard Biener June 14, 2023, 1:54 p.m. UTC | #1
On Wed, 14 Jun 2023, Jakub Jelinek wrote:

> On Wed, Jun 14, 2023 at 12:25:46PM +0000, Richard Biener wrote:
> > I think that's still very much desirable so this followup looks OK.
> > Maybe you can re-base it as prerequesite though?
> 
> Rebased then (of course with the UADDC/USUBC handling removed from this
> first patch, will be added in the second one).
> 
> Ok for trunk if it passes bootstrap/regtest?

OK.

Thanks,
Richard.

> 2023-06-14  Jakub Jelinek  <jakub@redhat.com>
> 
> 	* gimple-fold.cc (gimple_fold_call): Move handling of arg0
> 	as well as arg1 INTEGER_CSTs for .UBSAN_CHECK_{ADD,SUB,MUL}
> 	and .{ADD,SUB,MUL}_OVERFLOW calls from here...
> 	* fold-const-call.cc (fold_const_call): ... here.
> 
> --- gcc/gimple-fold.cc.jj	2023-06-13 18:23:37.199793275 +0200
> +++ gcc/gimple-fold.cc	2023-06-14 15:41:51.090987708 +0200
> @@ -5702,22 +5702,6 @@ gimple_fold_call (gimple_stmt_iterator *
>  	    result = arg0;
>  	  else if (subcode == MULT_EXPR && integer_onep (arg0))
>  	    result = arg1;
> -	  else if (TREE_CODE (arg0) == INTEGER_CST
> -		   && TREE_CODE (arg1) == INTEGER_CST)
> -	    {
> -	      if (cplx_result)
> -		result = int_const_binop (subcode, fold_convert (type, arg0),
> -					  fold_convert (type, arg1));
> -	      else
> -		result = int_const_binop (subcode, arg0, arg1);
> -	      if (result && arith_overflowed_p (subcode, type, arg0, arg1))
> -		{
> -		  if (cplx_result)
> -		    overflow = build_one_cst (type);
> -		  else
> -		    result = NULL_TREE;
> -		}
> -	    }
>  	  if (result)
>  	    {
>  	      if (result == integer_zero_node)
> --- gcc/fold-const-call.cc.jj	2023-06-02 10:36:43.096967505 +0200
> +++ gcc/fold-const-call.cc	2023-06-14 15:40:34.388064498 +0200
> @@ -1669,6 +1669,7 @@ fold_const_call (combined_fn fn, tree ty
>  {
>    const char *p0, *p1;
>    char c;
> +  tree_code subcode;
>    switch (fn)
>      {
>      case CFN_BUILT_IN_STRSPN:
> @@ -1738,6 +1739,46 @@ fold_const_call (combined_fn fn, tree ty
>      case CFN_FOLD_LEFT_PLUS:
>        return fold_const_fold_left (type, arg0, arg1, PLUS_EXPR);
>  
> +    case CFN_UBSAN_CHECK_ADD:
> +    case CFN_ADD_OVERFLOW:
> +      subcode = PLUS_EXPR;
> +      goto arith_overflow;
> +
> +    case CFN_UBSAN_CHECK_SUB:
> +    case CFN_SUB_OVERFLOW:
> +      subcode = MINUS_EXPR;
> +      goto arith_overflow;
> +
> +    case CFN_UBSAN_CHECK_MUL:
> +    case CFN_MUL_OVERFLOW:
> +      subcode = MULT_EXPR;
> +      goto arith_overflow;
> +
> +    arith_overflow:
> +      if (integer_cst_p (arg0) && integer_cst_p (arg1))
> +	{
> +	  tree itype
> +	    = TREE_CODE (type) == COMPLEX_TYPE ? TREE_TYPE (type) : type;
> +	  bool ovf = false;
> +	  tree r = int_const_binop (subcode, fold_convert (itype, arg0),
> +				    fold_convert (itype, arg1));
> +	  if (!r || TREE_CODE (r) != INTEGER_CST)
> +	    return NULL_TREE;
> +	  if (arith_overflowed_p (subcode, itype, arg0, arg1))
> +	    ovf = true;
> +	  if (TREE_OVERFLOW (r))
> +	    r = drop_tree_overflow (r);
> +	  if (itype == type)
> +	    {
> +	      if (ovf)
> +		return NULL_TREE;
> +	      return r;
> +	    }
> +	  else
> +	    return build_complex (type, r, build_int_cst (itype, ovf));
> +	}
> +      return NULL_TREE;
> +
>      default:
>        return fold_const_call_1 (fn, type, arg0, arg1);
>      }
> 
> 
> 	Jakub
> 
>
  

Patch

--- gcc/gimple-fold.cc.jj	2023-06-13 18:23:37.199793275 +0200
+++ gcc/gimple-fold.cc	2023-06-14 15:41:51.090987708 +0200
@@ -5702,22 +5702,6 @@  gimple_fold_call (gimple_stmt_iterator *
 	    result = arg0;
 	  else if (subcode == MULT_EXPR && integer_onep (arg0))
 	    result = arg1;
-	  else if (TREE_CODE (arg0) == INTEGER_CST
-		   && TREE_CODE (arg1) == INTEGER_CST)
-	    {
-	      if (cplx_result)
-		result = int_const_binop (subcode, fold_convert (type, arg0),
-					  fold_convert (type, arg1));
-	      else
-		result = int_const_binop (subcode, arg0, arg1);
-	      if (result && arith_overflowed_p (subcode, type, arg0, arg1))
-		{
-		  if (cplx_result)
-		    overflow = build_one_cst (type);
-		  else
-		    result = NULL_TREE;
-		}
-	    }
 	  if (result)
 	    {
 	      if (result == integer_zero_node)
--- gcc/fold-const-call.cc.jj	2023-06-02 10:36:43.096967505 +0200
+++ gcc/fold-const-call.cc	2023-06-14 15:40:34.388064498 +0200
@@ -1669,6 +1669,7 @@  fold_const_call (combined_fn fn, tree ty
 {
   const char *p0, *p1;
   char c;
+  tree_code subcode;
   switch (fn)
     {
     case CFN_BUILT_IN_STRSPN:
@@ -1738,6 +1739,46 @@  fold_const_call (combined_fn fn, tree ty
     case CFN_FOLD_LEFT_PLUS:
       return fold_const_fold_left (type, arg0, arg1, PLUS_EXPR);
 
+    case CFN_UBSAN_CHECK_ADD:
+    case CFN_ADD_OVERFLOW:
+      subcode = PLUS_EXPR;
+      goto arith_overflow;
+
+    case CFN_UBSAN_CHECK_SUB:
+    case CFN_SUB_OVERFLOW:
+      subcode = MINUS_EXPR;
+      goto arith_overflow;
+
+    case CFN_UBSAN_CHECK_MUL:
+    case CFN_MUL_OVERFLOW:
+      subcode = MULT_EXPR;
+      goto arith_overflow;
+
+    arith_overflow:
+      if (integer_cst_p (arg0) && integer_cst_p (arg1))
+	{
+	  tree itype
+	    = TREE_CODE (type) == COMPLEX_TYPE ? TREE_TYPE (type) : type;
+	  bool ovf = false;
+	  tree r = int_const_binop (subcode, fold_convert (itype, arg0),
+				    fold_convert (itype, arg1));
+	  if (!r || TREE_CODE (r) != INTEGER_CST)
+	    return NULL_TREE;
+	  if (arith_overflowed_p (subcode, itype, arg0, arg1))
+	    ovf = true;
+	  if (TREE_OVERFLOW (r))
+	    r = drop_tree_overflow (r);
+	  if (itype == type)
+	    {
+	      if (ovf)
+		return NULL_TREE;
+	      return r;
+	    }
+	  else
+	    return build_complex (type, r, build_int_cst (itype, ovf));
+	}
+      return NULL_TREE;
+
     default:
       return fold_const_call_1 (fn, type, arg0, arg1);
     }