https://gcc.gnu.org/g:3e2b0fcab75527bae01a0e22fe993addcf8475d9

commit r17-2604-g3e2b0fcab75527bae01a0e22fe993addcf8475d9
Author: Torbjörn SVENSSON <[email protected]>
Date:   Sat Jul 18 11:00:32 2026 +0200

    tree-optimization: Fold constant conditional selects [PR124663]
    
    Fold `(x == CST) ? x : CST` and `(x != CST) ? CST : x` to `CST` for
    integral scalar and vector types. This catches cases where earlier
    folding has converted bitwise mask expressions into conditional selects,
    including ARM MVE predicate-to-vector mask forms.
    
            PR tree-optimization/124663
    
    gcc/ChangeLog:
    
            * match.pd: Fold constant conditional selects.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/tree-ssa/pr124663.C: New test.
    
    Signed-off-by: Torbjörn SVENSSON <[email protected]>

Diff:
---
 gcc/match.pd                             | 16 +++++++++++++++-
 gcc/testsuite/g++.dg/tree-ssa/pr124663.C | 13 +++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 017b1362b7e4..723780b8c381 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -7112,7 +7112,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   /* For CONDs, don't handle signed values here. */
   (if (cnd == VEC_COND_EXPR
        || TYPE_UNSIGNED (TREE_TYPE (@0)))
-   (cnd @0 @2 @1))))
+   (cnd @0 @2 @1)))
+
+ /* A == CST ? A : CST -> CST.  */
+ (simplify
+  (cnd (eq @0 CONSTANT_CLASS_P@1) (nop_convert? @0) CONSTANT_CLASS_P@2)
+  (if (ANY_INTEGRAL_TYPE_P (type)
+       && operand_equal_p (@1, @2))
+   (convert @1)))
+
+ /* A != CST ? CST : A -> CST.  */
+ (simplify
+  (cnd (ne @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2 (nop_convert? @0))
+  (if (ANY_INTEGRAL_TYPE_P (type)
+       && operand_equal_p (@1, @2))
+   (convert @1))))
 
 /* abs/negative simplifications moved from fold_cond_expr_with_comparison.
 
diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr124663.C 
b/gcc/testsuite/g++.dg/tree-ssa/pr124663.C
new file mode 100644
index 000000000000..dfd12debff95
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tree-ssa/pr124663.C
@@ -0,0 +1,13 @@
+// PR c++/124663
+// { dg-do compile }
+// { dg-options "-O2 -fdump-tree-forwprop3-raw" }
+
+#define vector __attribute__((vector_size(4*sizeof(int))))
+void f(vector int *a)
+{
+  vector int cst = {1,2,3,4};
+  vector int t = (*a == cst);
+  *a = (t ? *a : cst);
+}
+
+// { dg-final { scan-tree-dump-not "_expr" "forwprop3" } }

Reply via email to