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]>
---
 gcc/match.pd                                 |  6 ++++++
 gcc/testsuite/gcc.dg/tree-ssa/float-conv-1.c | 16 ++++++++++++++++
 2 files changed, 22 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/float-conv-1.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 8dcf8c318cf..6f1c53e5d7b 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2733,6 +2733,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 00000000000..ffb07c4b94d
--- /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" } } */
-- 
2.43.0

Reply via email to