public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] builtins: Fix fold_builtin_query clzg/ctzg side-effects handling [PR112639]
@ 2023-11-21  8:15 Jakub Jelinek
  2023-11-21  8:26 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Jakub Jelinek @ 2023-11-21  8:15 UTC (permalink / raw)
  To: Richard Biener; +Cc: gcc-patches

Hi!

As the testcase shows, I've missed one spot where initially the code thinks
it could use 2 argument IFN_CLZ/IFN_CTZ form, but then verifies it can't
because it doesn't have the right target value and turns it into the
arg0 ? arg1 : .C[LT]Z (arg0)
form.  That form evaluates the argument twice though and so needs save_expr,
which I've missed to call in that case.  In other cases where it is known
from the beginning that it will be needed (e.g. the __builtin_clzg case
on types smaller than unsigned int where we'll need to add an addend
to the clz value) or the unsigned __int128 expansion called save_expr
before.

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

2023-11-21  Jakub Jelinek  <jakub@redhat.com>

	PR middle-end/112639
	* builtins.cc (fold_builtin_bit_query): If arg0 has side-effects, arg1
	is specified but cleared, call save_expr on arg0.

	* gcc.dg/torture/pr112639.c: New test.

--- gcc/builtins.cc.jj	2023-11-20 10:36:18.642625716 +0100
+++ gcc/builtins.cc	2023-11-20 15:25:59.665718971 +0100
@@ -9819,6 +9819,8 @@ fold_builtin_bit_query (location_t loc,
 	  if (!direct_internal_fn_supported_p (ifn, arg0_type,
 					       OPTIMIZE_FOR_BOTH))
 	    arg2 = NULL_TREE;
+	  if (arg2 == NULL_TREE)
+	    arg0 = save_expr (arg0);
 	}
       if (fcodei == END_BUILTINS || arg2)
 	call = build_call_expr_internal_loc (loc, ifn, integer_type_node,
--- gcc/testsuite/gcc.dg/torture/pr112639.c.jj	2023-11-20 15:32:24.994391656 +0100
+++ gcc/testsuite/gcc.dg/torture/pr112639.c	2023-11-20 15:34:14.395882993 +0100
@@ -0,0 +1,34 @@
+/* PR middle-end/112639 */
+/* { dg-do run } */
+
+unsigned long long b = 0;
+
+int
+foo (void)
+{
+  return __builtin_clzg (b++, __SIZEOF_LONG_LONG__ * __CHAR_BIT__);
+}
+
+int
+bar (void)
+{
+  return __builtin_ctzg (b++, __SIZEOF_LONG_LONG__ * __CHAR_BIT__);
+}
+
+int
+main ()
+{
+  if (foo () != __SIZEOF_LONG_LONG__ * __CHAR_BIT__ || b != 1)
+    __builtin_abort ();
+  if (foo () != __SIZEOF_LONG_LONG__ * __CHAR_BIT__ - 1 || b != 2)
+    __builtin_abort ();
+  if (foo () != __SIZEOF_LONG_LONG__ * __CHAR_BIT__ - 2 || b != 3)
+    __builtin_abort ();
+  b = 0;
+  if (bar () != __SIZEOF_LONG_LONG__ * __CHAR_BIT__ || b != 1)
+    __builtin_abort ();
+  if (bar () != 0 || b != 2)
+    __builtin_abort ();
+  if (bar () != 1 || b != 3)
+    __builtin_abort ();
+}

	Jakub


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

* Re: [PATCH] builtins: Fix fold_builtin_query clzg/ctzg side-effects handling [PR112639]
  2023-11-21  8:15 [PATCH] builtins: Fix fold_builtin_query clzg/ctzg side-effects handling [PR112639] Jakub Jelinek
@ 2023-11-21  8:26 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2023-11-21  8:26 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches

On Tue, 21 Nov 2023, Jakub Jelinek wrote:

> Hi!
> 
> As the testcase shows, I've missed one spot where initially the code thinks
> it could use 2 argument IFN_CLZ/IFN_CTZ form, but then verifies it can't
> because it doesn't have the right target value and turns it into the
> arg0 ? arg1 : .C[LT]Z (arg0)
> form.  That form evaluates the argument twice though and so needs save_expr,
> which I've missed to call in that case.  In other cases where it is known
> from the beginning that it will be needed (e.g. the __builtin_clzg case
> on types smaller than unsigned int where we'll need to add an addend
> to the clz value) or the unsigned __int128 expansion called save_expr
> before.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK.

> 2023-11-21  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR middle-end/112639
> 	* builtins.cc (fold_builtin_bit_query): If arg0 has side-effects, arg1
> 	is specified but cleared, call save_expr on arg0.
> 
> 	* gcc.dg/torture/pr112639.c: New test.
> 
> --- gcc/builtins.cc.jj	2023-11-20 10:36:18.642625716 +0100
> +++ gcc/builtins.cc	2023-11-20 15:25:59.665718971 +0100
> @@ -9819,6 +9819,8 @@ fold_builtin_bit_query (location_t loc,
>  	  if (!direct_internal_fn_supported_p (ifn, arg0_type,
>  					       OPTIMIZE_FOR_BOTH))
>  	    arg2 = NULL_TREE;
> +	  if (arg2 == NULL_TREE)
> +	    arg0 = save_expr (arg0);
>  	}
>        if (fcodei == END_BUILTINS || arg2)
>  	call = build_call_expr_internal_loc (loc, ifn, integer_type_node,
> --- gcc/testsuite/gcc.dg/torture/pr112639.c.jj	2023-11-20 15:32:24.994391656 +0100
> +++ gcc/testsuite/gcc.dg/torture/pr112639.c	2023-11-20 15:34:14.395882993 +0100
> @@ -0,0 +1,34 @@
> +/* PR middle-end/112639 */
> +/* { dg-do run } */
> +
> +unsigned long long b = 0;
> +
> +int
> +foo (void)
> +{
> +  return __builtin_clzg (b++, __SIZEOF_LONG_LONG__ * __CHAR_BIT__);
> +}
> +
> +int
> +bar (void)
> +{
> +  return __builtin_ctzg (b++, __SIZEOF_LONG_LONG__ * __CHAR_BIT__);
> +}
> +
> +int
> +main ()
> +{
> +  if (foo () != __SIZEOF_LONG_LONG__ * __CHAR_BIT__ || b != 1)
> +    __builtin_abort ();
> +  if (foo () != __SIZEOF_LONG_LONG__ * __CHAR_BIT__ - 1 || b != 2)
> +    __builtin_abort ();
> +  if (foo () != __SIZEOF_LONG_LONG__ * __CHAR_BIT__ - 2 || b != 3)
> +    __builtin_abort ();
> +  b = 0;
> +  if (bar () != __SIZEOF_LONG_LONG__ * __CHAR_BIT__ || b != 1)
> +    __builtin_abort ();
> +  if (bar () != 0 || b != 2)
> +    __builtin_abort ();
> +  if (bar () != 1 || b != 3)
> +    __builtin_abort ();
> +}
> 
> 	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-11-21  8:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-21  8:15 [PATCH] builtins: Fix fold_builtin_query clzg/ctzg side-effects handling [PR112639] Jakub Jelinek
2023-11-21  8:26 ` 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).