public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-8581] middle-end/106027 - fix types in needle folding
@ 2022-07-19 11:38 Richard Biener
  0 siblings, 0 replies; only message in thread
From: Richard Biener @ 2022-07-19 11:38 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:71c6baa9abc8c378a1aa913398a8f1a2277946e0

commit r12-8581-g71c6baa9abc8c378a1aa913398a8f1a2277946e0
Author: Richard Biener <rguenther@suse.de>
Date:   Mon Jun 20 13:40:50 2022 +0200

    middle-end/106027 - fix types in needle folding
    
    The fold_to_nonsharp_ineq_using_bound folding ends up creating invalid
    typed IL which confuses later foldings.  The following fixes that.
    
    2022-06-20  Richard Biener  <rguenther@suse.de>
    
            PR middle-end/106027
            * fold-const.cc (fold_to_nonsharp_ineq_using_bound): Use the
            type of the prevailing comparison for the new comparison type.
            (fold_binary_loc): Use proper types for the A < X && A + 1 > Y
            to A < X && A >= Y folding.
    
            * gcc.dg/pr106027.c: New testcase.
    
    (cherry picked from commit 713f2fd923442b1be620a44240ddf786ae0ab476)

Diff:
---
 gcc/fold-const.cc               | 10 +++++++---
 gcc/testsuite/gcc.dg/pr106027.c |  8 ++++++++
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index 8c423c83fa5..948e13a22d8 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -7529,7 +7529,7 @@ tree_swap_operands_p (const_tree arg0, const_tree arg1)
 static tree
 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
 {
-  tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
+  tree a, typea, type = TREE_TYPE (bound), a1, diff, y;
 
   if (TREE_CODE (bound) == LT_EXPR)
     a = TREE_OPERAND (bound, 0);
@@ -12036,11 +12036,15 @@ fold_binary_loc (location_t loc, enum tree_code code, tree type,
 	{
 	  tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
 	  if (tem && !operand_equal_p (tem, arg0, 0))
-	    return fold_build2_loc (loc, code, type, tem, arg1);
+	    return fold_convert (type,
+				 fold_build2_loc (loc, code, TREE_TYPE (arg1),
+						  tem, arg1));
 
 	  tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
 	  if (tem && !operand_equal_p (tem, arg1, 0))
-	    return fold_build2_loc (loc, code, type, arg0, tem);
+	    return fold_convert (type,
+				 fold_build2_loc (loc, code, TREE_TYPE (arg0),
+						  arg0, tem));
 	}
 
       if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
diff --git a/gcc/testsuite/gcc.dg/pr106027.c b/gcc/testsuite/gcc.dg/pr106027.c
new file mode 100644
index 00000000000..735205fb252
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr106027.c
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-O" } */
+
+int
+foo (unsigned int x, int y)
+{
+  return x <= (((y != y) < 0) ? y < 1 : 0);
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-07-19 11:38 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-19 11:38 [gcc r12-8581] middle-end/106027 - fix types in needle folding Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).