https://gcc.gnu.org/g:b46390257fecccc5cbc6708fa524b525fefd26f7

commit r17-1939-gb46390257fecccc5cbc6708fa524b525fefd26f7
Author: Andrew Pinski <[email protected]>
Date:   Mon May 11 17:23:10 2026 -0700

    match: Canonicalize `(float)(a CMP b)` into `(a CMP b) ? 1.0 : 0.0` 
[PR115571]
    
    Canonicalize `(type) (a CMP b)` to `(a CMP b) ? (type) true : (type) false` 
for
    floating point types. fold-const already does this so it makes sense to do 
it on
    the gimple level too. This also improves vectorization because a conversion
    from a comparison to a float does not need to implemented and also if it is
    implemented then selection of 1.0/0.0 is easier than the conversion would 
be.
    
    Bootstrapped and tested on x86_64-linux-gnu with no regressions.
    
            PR tree-optimization/115571
    
    gcc/ChangeLog:
    
            * match.pd (`(float)(a CMP b)`): New pattern.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/float-conv-1.c: New test.
    
    Signed-off-by: Andrew Pinski <[email protected]>

Diff:
---
 gcc/match.pd                                 |  6 ++++++
 gcc/testsuite/gcc.dg/tree-ssa/float-conv-1.c | 16 ++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index c8d274388b38..89d348050a1f 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2776,6 +2776,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  )
 )
 
+/* Canonicalize `(type) (a CMP b)` to `(a CMP b) ? (type) true : (type) false`
+   for floating point types.   */
+(simplify
+ (float (convert:s (tcc_comparison@0 @1 @2)))
+ (cond @0 { build_one_cst (type); } { build_zero_cst (type); } ))
+
 /* For integral types with undefined overflow and C != 0 fold
    x * C EQ/NE y * C into x EQ/NE y.  */
 (for cmp (eq ne)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/float-conv-1.c 
b/gcc/testsuite/gcc.dg/tree-ssa/float-conv-1.c
new file mode 100644
index 000000000000..ffb07c4b94da
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/float-conv-1.c
@@ -0,0 +1,16 @@
+/* PR tree-optimization/115571 */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized-raw" } */
+
+float f(float a)
+{
+  int t = a < 0.5;
+  return t;
+}
+
+float f0(float a)
+{
+  return (short)(a < 0.5);
+}
+
+/* { dg-final { scan-tree-dump-times "gimple_cond |gimple_assign <cond_expr" 2 
"optimized" } } */

Reply via email to