From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id CEDCB385E02E for ; Mon, 20 Jun 2022 13:01:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CEDCB385E02E Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 1414021B6B for ; Mon, 20 Jun 2022 13:01:43 +0000 (UTC) Received: from [10.168.4.8] (unknown [10.168.4.8]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 0D3CA2C143 for ; Mon, 20 Jun 2022 13:01:43 +0000 (UTC) Date: Mon, 20 Jun 2022 15:01:42 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] middle-end/106027 - fix types in needle folding MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, MISSING_MID, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 20 Jun 2022 13:01:48 -0000 Message-ID: <20220620130142.j8P0a7WAfKy2E4D296UdOUXh9wPxILtOfWhpPJAuhHI@z> The fold_to_nonsharp_ineq_using_bound folding ends up creating invalid typed IL which confuses later foldings. The following fixes that. Bootstrapped and tested on x86_64-unknwon-linux-gnu, pushed. 2022-06-20 Richard Biener 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. --- gcc/fold-const.cc | 10 +++++++--- gcc/testsuite/gcc.dg/pr106027.c | 8 ++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr106027.c diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index fbdf3c824af..99021a82df4 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -7530,7 +7530,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); @@ -12037,11 +12037,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); +} -- 2.35.3