]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
match: Keep conditional in simplification to constant [PR118140].
authorRobin Dapp <rdapp@ventanamicro.com>
Fri, 27 Dec 2024 16:29:25 +0000 (17:29 +0100)
committerRobin Dapp <rdapp@ventanamicro.com>
Tue, 14 Jan 2025 16:29:34 +0000 (17:29 +0100)
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.

(cherry picked from commit 14cb0610559fa33f211e1546260458496fdc5e71)

gcc/gimple-match-exports.cc
gcc/testsuite/gcc.dg/vect/pr118140.c [new file with mode: 0644]
gcc/testsuite/gcc.target/riscv/rvv/autovec/pr118140.c [new file with mode: 0644]

index aacf3ff04145662dc3a1352eceb0c8dc87fdab06..1fe6c0e38833abf171bb46626291285004c697c0 100644 (file)
@@ -323,23 +323,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 (file)
index 0000000..2dab98b
--- /dev/null
@@ -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 (file)
index 0000000..31134de
--- /dev/null
@@ -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 ();
+}