diff mbox series

fold: Remove (rrotate (rrotate A CST) CST) folding [PR117492]

Message ID 20241109035948.2707245-1-quic_apinski@quicinc.com
State New
Headers show
Series fold: Remove (rrotate (rrotate A CST) CST) folding [PR117492] | expand

Commit Message

Andrew Pinski Nov. 9, 2024, 3:59 a.m. UTC
This removes an (broken) simplification from fold which is already handled in match.
The reason why it was broken is because of the use of wi::to_wide on the RHS of the
rotate which could be 2 different types even though the LHS was the same type.
Since it is already handled in match (by the patterns for
`Turn (a OP c1) OP c2 into a OP (c1+c2).`). It can be removed without losing any optimizations.

Bootstrapped and tested on x86_64-linux-gnu.

	PR middle-end/117492

gcc/ChangeLog:

	* fold-const.cc (fold_binary_loc): Remove `Two consecutive rotates adding up
	to the some integer` simplifcation.

gcc/testsuite/ChangeLog:

	* gcc.dg/torture/pr117492-1.c: New test.

Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
---
 gcc/fold-const.cc                         | 10 ----------
 gcc/testsuite/gcc.dg/torture/pr117492-1.c | 16 ++++++++++++++++
 2 files changed, 16 insertions(+), 10 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr117492-1.c

Comments

Richard Biener Nov. 9, 2024, 7:47 a.m. UTC | #1
> Am 09.11.2024 um 05:00 schrieb Andrew Pinski <quic_apinski@quicinc.com>:
> 
> This removes an (broken) simplification from fold which is already handled in match.
> The reason why it was broken is because of the use of wi::to_wide on the RHS of the
> rotate which could be 2 different types even though the LHS was the same type.
> Since it is already handled in match (by the patterns for
> `Turn (a OP c1) OP c2 into a OP (c1+c2).`). It can be removed without losing any optimizations.
> 
> Bootstrapped and tested on x86_64-linux-gnu.

Ok

Richard 

>    PR middle-end/117492
> 
> gcc/ChangeLog:
> 
>    * fold-const.cc (fold_binary_loc): Remove `Two consecutive rotates adding up
>    to the some integer` simplifcation.
> 
> gcc/testsuite/ChangeLog:
> 
>    * gcc.dg/torture/pr117492-1.c: New test.
> 
> Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
> ---
> gcc/fold-const.cc                         | 10 ----------
> gcc/testsuite/gcc.dg/torture/pr117492-1.c | 16 ++++++++++++++++
> 2 files changed, 16 insertions(+), 10 deletions(-)
> create mode 100644 gcc/testsuite/gcc.dg/torture/pr117492-1.c
> 
> diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
> index 0e374294c01..1e8ae1ab493 100644
> --- a/gcc/fold-const.cc
> +++ b/gcc/fold-const.cc
> @@ -12530,16 +12530,6 @@ fold_binary_loc (location_t loc, enum tree_code code, tree type,
>                           arg01, arg1));
>    }
> 
> -      /* Two consecutive rotates adding up to the some integer
> -     multiple of the precision of the type can be ignored.  */
> -      if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
> -      && TREE_CODE (arg0) == RROTATE_EXPR
> -      && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
> -      && wi::umod_trunc (wi::to_wide (arg1)
> -                 + wi::to_wide (TREE_OPERAND (arg0, 1)),
> -                 prec) == 0)
> -    return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
> -
>       return NULL_TREE;
> 
>     case MIN_EXPR:
> diff --git a/gcc/testsuite/gcc.dg/torture/pr117492-1.c b/gcc/testsuite/gcc.dg/torture/pr117492-1.c
> new file mode 100644
> index 00000000000..543d8b7e709
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/torture/pr117492-1.c
> @@ -0,0 +1,16 @@
> +/* { dg-do compile } */
> +/* PR middle-end/117492 */
> +
> +/* This code would ICE in fold due to code which was using wi::to_wide with different types
> +   and adding them. */
> +
> +typedef unsigned u;
> +
> +u
> +foo(u x)
> +{
> +  return
> +    __builtin_stdc_rotate_left((unsigned)
> +      __builtin_stdc_rotate_right(x, 0x100000001ull),
> +                                  1);
> +}
> --
> 2.43.0
>
diff mbox series

Patch

diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index 0e374294c01..1e8ae1ab493 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -12530,16 +12530,6 @@  fold_binary_loc (location_t loc, enum tree_code code, tree type,
 						   arg01, arg1));
 	}
 
-      /* Two consecutive rotates adding up to the some integer
-	 multiple of the precision of the type can be ignored.  */
-      if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
-	  && TREE_CODE (arg0) == RROTATE_EXPR
-	  && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
-	  && wi::umod_trunc (wi::to_wide (arg1)
-			     + wi::to_wide (TREE_OPERAND (arg0, 1)),
-			     prec) == 0)
-	return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
-
       return NULL_TREE;
 
     case MIN_EXPR:
diff --git a/gcc/testsuite/gcc.dg/torture/pr117492-1.c b/gcc/testsuite/gcc.dg/torture/pr117492-1.c
new file mode 100644
index 00000000000..543d8b7e709
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr117492-1.c
@@ -0,0 +1,16 @@ 
+/* { dg-do compile } */
+/* PR middle-end/117492 */
+
+/* This code would ICE in fold due to code which was using wi::to_wide with different types
+   and adding them. */
+
+typedef unsigned u;
+
+u
+foo(u x)
+{
+  return
+    __builtin_stdc_rotate_left((unsigned)
+      __builtin_stdc_rotate_right(x, 0x100000001ull),
+                                  1);
+}