diff mbox series

tree-optimization/113126 - vector extension compare optimization

Message ID 20240111141313.2C30A385DC0C@sourceware.org
State New
Headers show
Series tree-optimization/113126 - vector extension compare optimization | expand

Commit Message

Richard Biener Jan. 11, 2024, 2:07 p.m. UTC
The following makes sure the resulting boolean type is the same
when eliding a float extension.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.

	PR tree-optimization/113126
	* match.pd ((double)float CMP (double)float -> float CMP float):
	Make sure the boolean type is the same.
	* fold-const.cc (fold_binary_loc): Likewise.

	* gcc.dg/torture/pr113126.c: New testcase.
---
 gcc/fold-const.cc                       |  3 ++-
 gcc/match.pd                            |  9 +++++----
 gcc/testsuite/gcc.dg/torture/pr113126.c | 15 +++++++++++++++
 3 files changed, 22 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr113126.c
diff mbox series

Patch

diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index 3a9d78b0c11..585c5099a37 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -12900,7 +12900,8 @@  fold_binary_loc (location_t loc, enum tree_code code, tree type,
 	if (element_precision (TREE_TYPE (targ1)) > element_precision (newtype))
 	  newtype = TREE_TYPE (targ1);
 
-	if (element_precision (newtype) < element_precision (TREE_TYPE (arg0)))
+	if (element_precision (newtype) < element_precision (TREE_TYPE (arg0))
+	    && is_truth_type_for (newtype, type))
 	  return fold_build2_loc (loc, code, type,
 			      fold_convert_loc (loc, newtype, targ0),
 			      fold_convert_loc (loc, newtype, targ1));
diff --git a/gcc/match.pd b/gcc/match.pd
index 876a9d1c06e..abbd03742f9 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -6792,11 +6792,12 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 	       && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
 	     type1 = double_type_node;
 	 }
-      tree newtype
-        = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
-	   ? TREE_TYPE (@00) : type1);
+       tree newtype
+	 = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
+	    ? TREE_TYPE (@00) : type1);
      }
-     (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
+     (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype)
+	  && is_truth_type_for (newtype, type))
       (cmp (convert:newtype @00) (convert:newtype @10))))))))
 
 
diff --git a/gcc/testsuite/gcc.dg/torture/pr113126.c b/gcc/testsuite/gcc.dg/torture/pr113126.c
new file mode 100644
index 00000000000..4aa38e0a255
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr113126.c
@@ -0,0 +1,15 @@ 
+/* { dg-do compile } */
+
+typedef float __attribute__((__vector_size__ (8))) F;
+typedef double __attribute__((__vector_size__ (16))) G;
+
+F f;
+G g;
+
+F
+foo (void)
+{
+  G h = __builtin_convertvector (f, G);
+  g = h <= h;
+  return f;
+}