match: Keep conditional in simplification to constant [PR118140].

Message ID D6Y9F1NFOFQQ.3J33ESMTN7C2V@gmail.com
State New
Headers
Series match: Keep conditional in simplification to constant [PR118140]. |

Checks

Context Check Description
rivoscibot/toolchain-ci-rivos-apply-patch success Patch applied
rivoscibot/toolchain-ci-rivos-lint success Lint passed
rivoscibot/toolchain-ci-rivos-build--newlib-rv64gcv-lp64d-multilib success Build passed
rivoscibot/toolchain-ci-rivos-build--linux-rv64gcv-lp64d-multilib success Build passed
rivoscibot/toolchain-ci-rivos-build--linux-rv64gc_zba_zbb_zbc_zbs-lp64d-multilib success Build passed
rivoscibot/toolchain-ci-rivos-test success Testing passed

Commit Message

Robin Dapp Jan. 10, 2025, 8:45 a.m. UTC
  Hi,

in PR118140 we simplify

  _ifc__33 = .COND_IOR (_41, d_lsm.7_11, _46, d_lsm.7_11);

to "1":

  Match-and-simplified .COND_IOR (_41, d_lsm.7_11, _46, d_lsm.7_11) to 1

when _46 == 1.  This happens by removing the conditional and applying
a | 1 = 1.  Normally we re-introduce the conditional and its else value
if needed but that does not happen here as we're not dealing with a
vector type.  For correctness's sake, we must not remove the conditional
even for non-vector types.

This patch re-introduces a COND_EXPR in such cases.  For PR118140 this
result in a non-vectorized loop.

Bootstrapped and regtested on x86 and aarch64.  Regtested on rv64gcv_zvl512b.

Regards
 Robin

	PR middle-end/118140

gcc/ChangeLog:

	* gimple-match-exports.cc (maybe_resimplify_conditional_op): Add
	COND_EXPR when we simplified to a scalar gimple value but still
	have an else value.

gcc/testsuite/ChangeLog:

	* gcc.dg/vect/pr118140.c: New test.
	* gcc.target/riscv/rvv/pr118140.c: New test.
---
 gcc/gimple-match-exports.cc                   | 26 ++++++++++-------
 gcc/testsuite/gcc.dg/vect/pr118140.c          | 27 +++++++++++++++++
 .../gcc.target/riscv/rvv/autovec/pr118140.c   | 29 +++++++++++++++++++
 3 files changed, 72 insertions(+), 10 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/vect/pr118140.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/autovec/pr118140.c
  

Comments

Richard Sandiford Jan. 10, 2025, 9:42 a.m. UTC | #1
"Robin Dapp" <rdapp.gcc@gmail.com> writes:
> Hi,
>
> in PR118140 we simplify
>
>   _ifc__33 = .COND_IOR (_41, d_lsm.7_11, _46, d_lsm.7_11);
>
> to "1":
>
>   Match-and-simplified .COND_IOR (_41, d_lsm.7_11, _46, d_lsm.7_11) to 1
>
> when _46 == 1.  This happens by removing the conditional and applying
> a | 1 = 1.  Normally we re-introduce the conditional and its else value
> if needed but that does not happen here as we're not dealing with a
> vector type.  For correctness's sake, we must not remove the conditional
> even for non-vector types.
>
> This patch re-introduces a COND_EXPR in such cases.  For PR118140 this
> result in a non-vectorized loop.
>
> Bootstrapped and regtested on x86 and aarch64.  Regtested on rv64gcv_zvl512b.
>
> Regards
>  Robin
>
> 	PR middle-end/118140
>
> gcc/ChangeLog:
>
> 	* gimple-match-exports.cc (maybe_resimplify_conditional_op): Add
> 	COND_EXPR when we simplified to a scalar gimple value but still
> 	have an else value.
>
> gcc/testsuite/ChangeLog:
>
> 	* gcc.dg/vect/pr118140.c: New test.
> 	* gcc.target/riscv/rvv/pr118140.c: New test.

OK, thanks.

Richard

> ---
>  gcc/gimple-match-exports.cc                   | 26 ++++++++++-------
>  gcc/testsuite/gcc.dg/vect/pr118140.c          | 27 +++++++++++++++++
>  .../gcc.target/riscv/rvv/autovec/pr118140.c   | 29 +++++++++++++++++++
>  3 files changed, 72 insertions(+), 10 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/vect/pr118140.c
>  create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/autovec/pr118140.c
>
> diff --git a/gcc/gimple-match-exports.cc b/gcc/gimple-match-exports.cc
> index e06a8aaa171..ccba046a1d4 100644
> --- a/gcc/gimple-match-exports.cc
> +++ b/gcc/gimple-match-exports.cc
> @@ -337,23 +337,29 @@ maybe_resimplify_conditional_op (gimple_seq *seq, gimple_match_op *res_op,
>      }
>  
>    /* If the "then" value is a gimple value and the "else" value matters,
> -     create a VEC_COND_EXPR between them, then see if it can be further
> +     create a (VEC_)COND_EXPR between them, then see if it can be further
>       simplified.  */
>    gimple_match_op new_op;
>    if (res_op->cond.else_value
> -      && VECTOR_TYPE_P (res_op->type)
>        && gimple_simplified_result_is_gimple_val (res_op))
>      {
> -      tree len = res_op->cond.len;
> -      if (!len)
> -	new_op.set_op (VEC_COND_EXPR, res_op->type,
> -		       res_op->cond.cond, res_op->ops[0],
> -		       res_op->cond.else_value);
> +      if (VECTOR_TYPE_P (res_op->type))
> +	{
> +	  tree len = res_op->cond.len;
> +	  if (!len)
> +	    new_op.set_op (VEC_COND_EXPR, res_op->type,
> +			   res_op->cond.cond, res_op->ops[0],
> +			   res_op->cond.else_value);
> +	  else
> +	    new_op.set_op (IFN_VCOND_MASK_LEN, res_op->type,
> +			   res_op->cond.cond, res_op->ops[0],
> +			   res_op->cond.else_value,
> +			   res_op->cond.len, res_op->cond.bias);
> +	}
>        else
> -	new_op.set_op (IFN_VCOND_MASK_LEN, res_op->type,
> +	new_op.set_op (COND_EXPR, res_op->type,
>  		       res_op->cond.cond, res_op->ops[0],
> -		       res_op->cond.else_value,
> -		       res_op->cond.len, res_op->cond.bias);
> +		       res_op->cond.else_value);
>        *res_op = new_op;
>        return gimple_resimplify3 (seq, res_op, valueize);
>      }
> diff --git a/gcc/testsuite/gcc.dg/vect/pr118140.c b/gcc/testsuite/gcc.dg/vect/pr118140.c
> new file mode 100644
> index 00000000000..2dab98bfc91
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/vect/pr118140.c
> @@ -0,0 +1,27 @@
> +/* { dg-do run { target { aarch64*-*-* || riscv*-*-* } } } */
> +/* { dg-additional-options "-std=gnu99" } */
> +
> +long long a;
> +_Bool d;
> +char e;
> +_Bool f[17];
> +_Bool f_3;
> +
> +int main() {
> +  for (char g = 3; g < 16; g++) {
> +      d |= ({
> +        int h = f[g - 1] ? 2 : 0;
> +        _Bool t;
> +        if (f[g - 1])
> +          t = f_3;
> +        else
> +          t = 0;
> +        int i = t;
> +        h > i;
> +      });
> +    e += f[g + 1];
> +  }
> +
> +  if (d != 0)
> +    __builtin_abort ();
> +}
> diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr118140.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr118140.c
> new file mode 100644
> index 00000000000..31134de7b3a
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr118140.c
> @@ -0,0 +1,29 @@
> +/* { dg-do run } */
> +/* { dg-require-effective-target riscv_v_ok } */
> +/* { dg-add-options riscv_v } */
> +/* { dg-additional-options "-std=gnu99 -Wno-pedantic" } */
> +
> +long long a;
> +_Bool d;
> +char e;
> +_Bool f[17];
> +_Bool f_3;
> +
> +int main() {
> +  for (char g = 3; g < 16; g++) {
> +      d |= ({
> +        int h = f[g - 1] ? 2 : 0;
> +        _Bool t;
> +        if (f[g - 1])
> +          t = f_3;
> +        else
> +          t = 0;
> +        int i = t;
> +        h > i;
> +      });
> +    e += f[g + 1];
> +  }
> +
> +  if (d != 0)
> +    __builtin_abort ();
> +}
  
Robin Dapp Jan. 14, 2025, 2:05 p.m. UTC | #2
> OK, thanks.
>
> Richard

The issue is also present on GCC 14 as well and the patch applies cleanly.
Regtested on rv64gcv_zvl512b.  To make it explicit: OK to backport to 14?
  
Richard Sandiford Jan. 14, 2025, 2:14 p.m. UTC | #3
"Robin Dapp" <rdapp.gcc@gmail.com> writes:
>> OK, thanks.
>>
>> Richard
>
> The issue is also present on GCC 14 as well and the patch applies cleanly.
> Regtested on rv64gcv_zvl512b.  To make it explicit: OK to backport to 14?

Yes, thanks.

Richard
  

Patch

diff --git a/gcc/gimple-match-exports.cc b/gcc/gimple-match-exports.cc
index e06a8aaa171..ccba046a1d4 100644
--- a/gcc/gimple-match-exports.cc
+++ b/gcc/gimple-match-exports.cc
@@ -337,23 +337,29 @@  maybe_resimplify_conditional_op (gimple_seq *seq, gimple_match_op *res_op,
     }
 
   /* If the "then" value is a gimple value and the "else" value matters,
-     create a VEC_COND_EXPR between them, then see if it can be further
+     create a (VEC_)COND_EXPR between them, then see if it can be further
      simplified.  */
   gimple_match_op new_op;
   if (res_op->cond.else_value
-      && VECTOR_TYPE_P (res_op->type)
       && gimple_simplified_result_is_gimple_val (res_op))
     {
-      tree len = res_op->cond.len;
-      if (!len)
-	new_op.set_op (VEC_COND_EXPR, res_op->type,
-		       res_op->cond.cond, res_op->ops[0],
-		       res_op->cond.else_value);
+      if (VECTOR_TYPE_P (res_op->type))
+	{
+	  tree len = res_op->cond.len;
+	  if (!len)
+	    new_op.set_op (VEC_COND_EXPR, res_op->type,
+			   res_op->cond.cond, res_op->ops[0],
+			   res_op->cond.else_value);
+	  else
+	    new_op.set_op (IFN_VCOND_MASK_LEN, res_op->type,
+			   res_op->cond.cond, res_op->ops[0],
+			   res_op->cond.else_value,
+			   res_op->cond.len, res_op->cond.bias);
+	}
       else
-	new_op.set_op (IFN_VCOND_MASK_LEN, res_op->type,
+	new_op.set_op (COND_EXPR, res_op->type,
 		       res_op->cond.cond, res_op->ops[0],
-		       res_op->cond.else_value,
-		       res_op->cond.len, res_op->cond.bias);
+		       res_op->cond.else_value);
       *res_op = new_op;
       return gimple_resimplify3 (seq, res_op, valueize);
     }
diff --git a/gcc/testsuite/gcc.dg/vect/pr118140.c b/gcc/testsuite/gcc.dg/vect/pr118140.c
new file mode 100644
index 00000000000..2dab98bfc91
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/pr118140.c
@@ -0,0 +1,27 @@ 
+/* { dg-do run { target { aarch64*-*-* || riscv*-*-* } } } */
+/* { dg-additional-options "-std=gnu99" } */
+
+long long a;
+_Bool d;
+char e;
+_Bool f[17];
+_Bool f_3;
+
+int main() {
+  for (char g = 3; g < 16; g++) {
+      d |= ({
+        int h = f[g - 1] ? 2 : 0;
+        _Bool t;
+        if (f[g - 1])
+          t = f_3;
+        else
+          t = 0;
+        int i = t;
+        h > i;
+      });
+    e += f[g + 1];
+  }
+
+  if (d != 0)
+    __builtin_abort ();
+}
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr118140.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr118140.c
new file mode 100644
index 00000000000..31134de7b3a
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr118140.c
@@ -0,0 +1,29 @@ 
+/* { dg-do run } */
+/* { dg-require-effective-target riscv_v_ok } */
+/* { dg-add-options riscv_v } */
+/* { dg-additional-options "-std=gnu99 -Wno-pedantic" } */
+
+long long a;
+_Bool d;
+char e;
+_Bool f[17];
+_Bool f_3;
+
+int main() {
+  for (char g = 3; g < 16; g++) {
+      d |= ({
+        int h = f[g - 1] ? 2 : 0;
+        _Bool t;
+        if (f[g - 1])
+          t = f_3;
+        else
+          t = 0;
+        int i = t;
+        h > i;
+      });
+    e += f[g + 1];
+  }
+
+  if (d != 0)
+    __builtin_abort ();
+}