From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1666) id 6CF103851146; Tue, 11 Oct 2022 13:04:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6CF103851146 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665493458; bh=ogvJPmXyELHb0Y0eyBg5xz8qpJygAzcM/ppuxPzybSc=; h=From:To:Subject:Date:From; b=hfBbfg2xJvX12EJor4aHObhxdRcXiN7HnwGid6eVRg+bCZTb1678PhnfXm/970zzI IAgc5vp9lyV5T4dK22zfqRg/n5OJV0p1WgQ4hdfjhMXSzccc0EnaePxHMk92Esn6x8 Z7sx0pLveWiUAqV9gxdxj6d/d6Sk+Tq8qXd1eBsc= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Richard Biener To: gcc-cvs@gcc.gnu.org Subject: [gcc r11-10302] middle-end/106027 - fix types in needle folding X-Act-Checkin: gcc X-Git-Author: Richard Biener X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 20b9e2480c0a21e239a6cfb5a6cf5fc69df8a1d7 X-Git-Newrev: a27e5fc76a49b759d271a828f225899434b537e9 Message-Id: <20221011130418.6CF103851146@sourceware.org> Date: Tue, 11 Oct 2022 13:04:18 +0000 (GMT) List-Id: https://gcc.gnu.org/g:a27e5fc76a49b759d271a828f225899434b537e9 commit r11-10302-ga27e5fc76a49b759d271a828f225899434b537e9 Author: Richard Biener 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 PR middle-end/106027 * fold-const.c (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.c | 10 +++++++--- gcc/testsuite/gcc.dg/pr106027.c | 8 ++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 8ce775753f3..901255a18d6 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -7508,7 +7508,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); @@ -11997,11 +11997,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); +}