public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] lower-bitint: Fix lowering of middle sized _BitInt operations which can throw [PR112770]
@ 2023-12-01  8:10 Jakub Jelinek
  2023-12-01  8:34 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Jakub Jelinek @ 2023-12-01  8:10 UTC (permalink / raw)
  To: Richard Biener; +Cc: gcc-patches

Hi!

The middle kind _BitInt lowering is mostly done by casting the BITINT_TYPE
operands (if any) to a signed/unsigned integer type which has larger/equal
precision, using such integer type also for the lhs (if BITINT_TYPE) and
and adding a cast after the statement from that new lhs to the old
(BITINT_TYPE) lhs.  Note, for middle kind this isn't done for GIMPLE_CALLs.
Most of the time that works nicely, the exception as the following testcase
shows is -fnon-call-exceptions and some operations which can trap.  Because
inserting the cast to a new lhs after the statement results in a trapping
statement in the middle of a basic block.
The following patch fixes that by emitting the cast on the fallthru edge
instead.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2023-12-01  Jakub Jelinek  <jakub@redhat.com>

	PR middle-end/112770
	* gimple-lower-bitint.cc (gimple_lower_bitint): When adjusting
	lhs of middle _BitInt setter which ends bb, insert cast on
	the fallthru edge rather than after stmt.

	* gcc.dg/bitint-45.c: New test.

--- gcc/gimple-lower-bitint.cc.jj	2023-11-30 17:42:07.172487347 +0100
+++ gcc/gimple-lower-bitint.cc	2023-11-30 19:04:08.004372919 +0100
@@ -6336,7 +6336,13 @@ gimple_lower_bitint (void)
 		    type = build_nonstandard_integer_type (prec, uns);
 		    tree lhs2 = make_ssa_name (type);
 		    gimple *g = gimple_build_assign (lhs, NOP_EXPR, lhs2);
-		    gsi_insert_after (&gsi, g, GSI_SAME_STMT);
+		    if (stmt_ends_bb_p (stmt))
+		      {
+			edge e = find_fallthru_edge (gsi_bb (gsi)->succs);
+			gsi_insert_on_edge_immediate (e, g);
+		      }
+		    else
+		      gsi_insert_after (&gsi, g, GSI_SAME_STMT);
 		    gimple_set_lhs (stmt, lhs2);
 		  }
 	      unsigned int nops = gimple_num_ops (stmt);
--- gcc/testsuite/gcc.dg/bitint-45.c.jj	2023-11-30 19:17:43.923888015 +0100
+++ gcc/testsuite/gcc.dg/bitint-45.c	2023-11-30 19:17:35.502006495 +0100
@@ -0,0 +1,11 @@
+/* PR middle-end/112770 */
+/* { dg-do compile { target bitint128 } } */
+/* { dg-options "-std=c23 -fnon-call-exceptions" } */
+
+void
+foo (void)
+{
+  _BitInt(128) a = 0;
+  a /= 0;		/* { dg-warning "division by zero" } */
+  &a;
+}

	Jakub


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] lower-bitint: Fix lowering of middle sized _BitInt operations which can throw [PR112770]
  2023-12-01  8:10 [PATCH] lower-bitint: Fix lowering of middle sized _BitInt operations which can throw [PR112770] Jakub Jelinek
@ 2023-12-01  8:34 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2023-12-01  8:34 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches

On Fri, 1 Dec 2023, Jakub Jelinek wrote:

> Hi!
> 
> The middle kind _BitInt lowering is mostly done by casting the BITINT_TYPE
> operands (if any) to a signed/unsigned integer type which has larger/equal
> precision, using such integer type also for the lhs (if BITINT_TYPE) and
> and adding a cast after the statement from that new lhs to the old
> (BITINT_TYPE) lhs.  Note, for middle kind this isn't done for GIMPLE_CALLs.
> Most of the time that works nicely, the exception as the following testcase
> shows is -fnon-call-exceptions and some operations which can trap.  Because
> inserting the cast to a new lhs after the statement results in a trapping
> statement in the middle of a basic block.
> The following patch fixes that by emitting the cast on the fallthru edge
> instead.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK.

> 2023-12-01  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR middle-end/112770
> 	* gimple-lower-bitint.cc (gimple_lower_bitint): When adjusting
> 	lhs of middle _BitInt setter which ends bb, insert cast on
> 	the fallthru edge rather than after stmt.
> 
> 	* gcc.dg/bitint-45.c: New test.
> 
> --- gcc/gimple-lower-bitint.cc.jj	2023-11-30 17:42:07.172487347 +0100
> +++ gcc/gimple-lower-bitint.cc	2023-11-30 19:04:08.004372919 +0100
> @@ -6336,7 +6336,13 @@ gimple_lower_bitint (void)
>  		    type = build_nonstandard_integer_type (prec, uns);
>  		    tree lhs2 = make_ssa_name (type);
>  		    gimple *g = gimple_build_assign (lhs, NOP_EXPR, lhs2);
> -		    gsi_insert_after (&gsi, g, GSI_SAME_STMT);
> +		    if (stmt_ends_bb_p (stmt))
> +		      {
> +			edge e = find_fallthru_edge (gsi_bb (gsi)->succs);
> +			gsi_insert_on_edge_immediate (e, g);
> +		      }
> +		    else
> +		      gsi_insert_after (&gsi, g, GSI_SAME_STMT);
>  		    gimple_set_lhs (stmt, lhs2);
>  		  }
>  	      unsigned int nops = gimple_num_ops (stmt);
> --- gcc/testsuite/gcc.dg/bitint-45.c.jj	2023-11-30 19:17:43.923888015 +0100
> +++ gcc/testsuite/gcc.dg/bitint-45.c	2023-11-30 19:17:35.502006495 +0100
> @@ -0,0 +1,11 @@
> +/* PR middle-end/112770 */
> +/* { dg-do compile { target bitint128 } } */
> +/* { dg-options "-std=c23 -fnon-call-exceptions" } */
> +
> +void
> +foo (void)
> +{
> +  _BitInt(128) a = 0;
> +  a /= 0;		/* { dg-warning "division by zero" } */
> +  &a;
> +}
> 
> 	Jakub
> 
> 

-- 
Richard Biener <rguenther@suse.de>
SUSE Software Solutions Germany GmbH,
Frankenstrasse 146, 90461 Nuernberg, Germany;
GF: Ivo Totev, Andrew McDonald, Werner Knoblich; (HRB 36809, AG Nuernberg)

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-12-01  8:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-01  8:10 [PATCH] lower-bitint: Fix lowering of middle sized _BitInt operations which can throw [PR112770] Jakub Jelinek
2023-12-01  8:34 ` Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).