[gcc r15-11415] tree-optimization: Fold constant conditional selects [PR124663]

Torbjorn Svensson via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:920c3de1f84a6d7ac6ea689e742da84446c0ac50

commit r15-11415-g920c3de1f84a6d7ac6ea689e742da84446c0ac50
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]>
    (cherry picked from commit 3e2b0fcab75527bae01a0e22fe993addcf8475d9)

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 57fd90fd95ea..1af481ecfdab 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -6689,7 +6689,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" } }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.