https://gcc.gnu.org/g:14cb0610559fa33f211e1546260458496fdc5e71

commit r15-6891-g14cb0610559fa33f211e1546260458496fdc5e71
Author: Robin Dapp <rd...@ventanamicro.com>
Date:   Fri Dec 27 17:29:25 2024 +0100

    match: Keep conditional in simplification to constant [PR118140].
    
    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.
    
            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/autovec/pr118140.c: New test.

Diff:
---
 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(-)

diff --git a/gcc/gimple-match-exports.cc b/gcc/gimple-match-exports.cc
index e06a8aaa1712..ccba046a1d4f 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 000000000000..2dab98bfc913
--- /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 000000000000..31134de7b3a3
--- /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 ();
+}

Reply via email to