From patchwork Tue Mar 12 14:09:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1911097 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=JYcuzut2; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=YmVRqrth; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=JYcuzut2; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=YmVRqrth; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TvFrT2KGKz1yWt for ; Wed, 13 Mar 2024 01:09:29 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 549333858030 for ; Tue, 12 Mar 2024 14:09:27 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2a07:de40:b251:101:10:150:64:2]) by sourceware.org (Postfix) with ESMTPS id 1DCC4385829A for ; Tue, 12 Mar 2024 14:09:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1DCC4385829A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1DCC4385829A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:2 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710252549; cv=none; b=ds8y4/cUHaU/Wu0lG1Infn84u04me/CrvTnJ1mo4ZC8ETjoFljOECynhUWIrZue5xAhun1GI0t42BCoYgWf9W+KvAzRCA++L2H+cz5KHedldxXIC7HI0EtsB9b1SX03yMveUtoR/lWDEFLGPEUXngLwentEH7Ni3KVog3baLb10= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710252549; c=relaxed/simple; bh=VxKxRFfXo0wIKk+4PWFjNcXogIENlSER04IGCS3iajQ=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version; b=FMxL9c1jJdHPog8loK4+Ope3qqqSHBmcuCWUxwqHkyj4f0d+lABE4uDN68ZT97EKdfUzNW8/oWN9VVD8DP4zLo2benq8j3ZKZJ4KBSwXHABScwu6H7y4IA+WS0NbPTdjuSuBc4L3trG2dHOcJapepb64cYdFM+z9MwIx2YKlTfg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from [10.168.4.150] (unknown [10.168.4.150]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 012885D5BE for ; Tue, 12 Mar 2024 14:09:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1710252544; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=pRc/FC+zuHwouYp6IzCPwSp8QhwAztXCqVRG7i2V1+0=; b=JYcuzut2XLJajAqv6o10/Rdfcg9+1pFOLOCbyozxxr6QofBX4xT+kJkFMa+K4yoLVe4IvL 1vlCEwRrqUel/aKGfb/r5wH3BTSXugNk6z+WqRv9LlZNOA12i6A0DUTJGoVZqefduQhPqb 0buzC8qUV9Qf4C/H/94tQpnoc47z/+g= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1710252544; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=pRc/FC+zuHwouYp6IzCPwSp8QhwAztXCqVRG7i2V1+0=; b=YmVRqrthJqkfytpK+azZck955D75SpJBEJ6DBnhJ6HsRF0kbldzEkcTQP7FS8F/WFrekTo 4qzAtjYAhuIqKUAw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1710252544; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=pRc/FC+zuHwouYp6IzCPwSp8QhwAztXCqVRG7i2V1+0=; b=JYcuzut2XLJajAqv6o10/Rdfcg9+1pFOLOCbyozxxr6QofBX4xT+kJkFMa+K4yoLVe4IvL 1vlCEwRrqUel/aKGfb/r5wH3BTSXugNk6z+WqRv9LlZNOA12i6A0DUTJGoVZqefduQhPqb 0buzC8qUV9Qf4C/H/94tQpnoc47z/+g= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1710252544; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=pRc/FC+zuHwouYp6IzCPwSp8QhwAztXCqVRG7i2V1+0=; b=YmVRqrthJqkfytpK+azZck955D75SpJBEJ6DBnhJ6HsRF0kbldzEkcTQP7FS8F/WFrekTo 4qzAtjYAhuIqKUAw== Date: Tue, 12 Mar 2024 15:09:03 +0100 (CET) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/114121 - chrec_fold_{plus,multiply} and recursion MIME-Version: 1.0 Authentication-Results: smtp-out2.suse.de; none X-Spam-Level: X-Spam-Score: -1.66 X-Spamd-Result: default: False [-1.66 / 50.00]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; MISSING_MID(2.50)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.06)[-0.305]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_COUNT_ZERO(0.00)[0]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; BAYES_HAM(-3.00)[100.00%] X-Spam-Status: No, score=-10.6 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Message-Id: <20240312140927.549333858030@sourceware.org> The following addresses endless recursion in the chrec_fold_{plus,multiply} functions when handling sign-conversions. We only need to apply tricks when we'd fail (there's a chrec in the converted operand) and we need to make sure to not turn the other operand into something worse (for the chrec-vs-chrec case). Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/114121 * tree-chrec.cc (chrec_fold_plus_1): Guard recursion with converted operand properly. (chrec_fold_multiply): Likewise. Handle missed recursion. * gcc.dg/torture/pr114312.c: New testcase. --- gcc/testsuite/gcc.dg/torture/pr114312.c | 15 ++ gcc/tree-chrec.cc | 176 +++++++++++++----------- 2 files changed, 107 insertions(+), 84 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr114312.c diff --git a/gcc/testsuite/gcc.dg/torture/pr114312.c b/gcc/testsuite/gcc.dg/torture/pr114312.c new file mode 100644 index 00000000000..c508c64ed19 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr114312.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-require-effective-target bitint } */ + +#if __BITINT_MAXWIDTH__ >= 129 +typedef _BitInt(129) B; +B b; + +B +foo(void) +{ + _BitInt(64) a = 1; + a &= b * b; + return b << a; +} +#endif diff --git a/gcc/tree-chrec.cc b/gcc/tree-chrec.cc index 7cd0ebc1010..1b2ed753551 100644 --- a/gcc/tree-chrec.cc +++ b/gcc/tree-chrec.cc @@ -251,23 +251,27 @@ chrec_fold_plus_1 (enum tree_code code, tree type, return chrec_fold_plus_poly_poly (code, type, op0, op1); CASE_CONVERT: - { - /* We can strip sign-conversions to signed by performing the - operation in unsigned. */ - tree optype = TREE_TYPE (TREE_OPERAND (op1, 0)); - if (INTEGRAL_TYPE_P (type) - && INTEGRAL_TYPE_P (optype) - && tree_nop_conversion_p (type, optype) - && TYPE_UNSIGNED (optype)) - return chrec_convert (type, - chrec_fold_plus_1 (code, optype, - chrec_convert (optype, - op0, NULL), - TREE_OPERAND (op1, 0)), - NULL); - if (tree_contains_chrecs (op1, NULL)) + if (tree_contains_chrecs (op1, NULL)) + { + /* We can strip sign-conversions to signed by performing the + operation in unsigned. */ + tree optype = TREE_TYPE (TREE_OPERAND (op1, 0)); + if (INTEGRAL_TYPE_P (type) + && INTEGRAL_TYPE_P (optype) + && tree_nop_conversion_p (type, optype) + && TYPE_UNSIGNED (optype)) + { + tree tem = chrec_convert (optype, op0, NULL); + if (TREE_CODE (tem) == POLYNOMIAL_CHREC) + return chrec_convert (type, + chrec_fold_plus_1 (code, optype, + tem, + TREE_OPERAND + (op1, 0)), + NULL); + } return chrec_dont_know; - } + } /* FALLTHRU */ default: @@ -284,26 +288,27 @@ chrec_fold_plus_1 (enum tree_code code, tree type, } CASE_CONVERT: - { - /* We can strip sign-conversions to signed by performing the - operation in unsigned. */ - tree optype = TREE_TYPE (TREE_OPERAND (op0, 0)); - if (INTEGRAL_TYPE_P (type) - && INTEGRAL_TYPE_P (optype) - && tree_nop_conversion_p (type, optype) - && TYPE_UNSIGNED (optype)) - return chrec_convert (type, - chrec_fold_plus_1 (code, optype, - TREE_OPERAND (op0, 0), - chrec_convert (optype, - op1, NULL)), - NULL); - if (tree_contains_chrecs (op0, NULL)) + if (tree_contains_chrecs (op0, NULL)) + { + /* We can strip sign-conversions to signed by performing the + operation in unsigned. */ + tree optype = TREE_TYPE (TREE_OPERAND (op0, 0)); + if (INTEGRAL_TYPE_P (type) + && INTEGRAL_TYPE_P (optype) + && tree_nop_conversion_p (type, optype) + && TYPE_UNSIGNED (optype)) + return chrec_convert (type, + chrec_fold_plus_1 (code, optype, + TREE_OPERAND (op0, 0), + chrec_convert (optype, + op1, NULL)), + NULL); return chrec_dont_know; - } + } /* FALLTHRU */ default: + gcc_checking_assert (!tree_contains_chrecs (op0, NULL)); switch (TREE_CODE (op1)) { case POLYNOMIAL_CHREC: @@ -325,24 +330,24 @@ chrec_fold_plus_1 (enum tree_code code, tree type, : build_int_cst_type (type, -1))); CASE_CONVERT: - { - /* We can strip sign-conversions to signed by performing the - operation in unsigned. */ - tree optype = TREE_TYPE (TREE_OPERAND (op1, 0)); - if (INTEGRAL_TYPE_P (type) - && INTEGRAL_TYPE_P (optype) - && tree_nop_conversion_p (type, optype) - && TYPE_UNSIGNED (optype)) - return chrec_convert (type, - chrec_fold_plus_1 (code, optype, - chrec_convert (optype, - op0, NULL), - TREE_OPERAND (op1, 0)), - NULL); - } - if (tree_contains_chrecs (op1, NULL)) - return chrec_dont_know; + { + /* We can strip sign-conversions to signed by performing the + operation in unsigned. */ + tree optype = TREE_TYPE (TREE_OPERAND (op1, 0)); + if (INTEGRAL_TYPE_P (type) + && INTEGRAL_TYPE_P (optype) + && tree_nop_conversion_p (type, optype) + && TYPE_UNSIGNED (optype)) + return chrec_convert (type, + chrec_fold_plus_1 (code, optype, + chrec_convert (optype, + op0, + NULL), + TREE_OPERAND (op1, 0)), + NULL); + return chrec_dont_know; + } /* FALLTHRU */ default: @@ -440,24 +445,26 @@ chrec_fold_multiply (tree type, return chrec_fold_multiply_poly_poly (type, op0, op1); CASE_CONVERT: - { - /* We can strip sign-conversions to signed by performing the - operation in unsigned. */ - tree optype = TREE_TYPE (TREE_OPERAND (op1, 0)); - if (INTEGRAL_TYPE_P (type) - && INTEGRAL_TYPE_P (optype) - && tree_nop_conversion_p (type, optype) - && TYPE_UNSIGNED (optype)) - return chrec_convert (type, - chrec_fold_multiply (optype, - chrec_convert (optype, - op0, NULL), - TREE_OPERAND (op1, 0)), - NULL); - } - if (tree_contains_chrecs (op1, NULL)) - return chrec_dont_know; + { + /* We can strip sign-conversions to signed by performing the + operation in unsigned. */ + tree optype = TREE_TYPE (TREE_OPERAND (op1, 0)); + if (INTEGRAL_TYPE_P (type) + && INTEGRAL_TYPE_P (optype) + && tree_nop_conversion_p (type, optype) + && TYPE_UNSIGNED (optype)) + { + tree tem = chrec_convert (optype, op0, NULL); + if (TREE_CODE (tem) == POLYNOMIAL_CHREC) + return chrec_convert (type, + chrec_fold_multiply (optype, tem, + TREE_OPERAND + (op1, 0)), + NULL); + } + return chrec_dont_know; + } /* FALLTHRU */ default: @@ -506,27 +513,28 @@ chrec_fold_multiply (tree type, } CASE_CONVERT: - { - /* We can strip sign-conversions to signed by performing the - operation in unsigned. */ - tree optype = TREE_TYPE (TREE_OPERAND (op0, 0)); - if (INTEGRAL_TYPE_P (type) - && INTEGRAL_TYPE_P (optype) - && tree_nop_conversion_p (type, optype) - && TYPE_UNSIGNED (optype)) - return chrec_convert (type, - chrec_fold_multiply (optype, - TREE_OPERAND (op0, 0), - chrec_convert (optype, - op1, NULL)), - NULL); - } - if (tree_contains_chrecs (op0, NULL)) - return chrec_dont_know; + { + /* We can strip sign-conversions to signed by performing the + operation in unsigned. */ + tree optype = TREE_TYPE (TREE_OPERAND (op0, 0)); + if (INTEGRAL_TYPE_P (type) + && INTEGRAL_TYPE_P (optype) + && tree_nop_conversion_p (type, optype) + && TYPE_UNSIGNED (optype)) + return chrec_convert (type, + chrec_fold_multiply (optype, + TREE_OPERAND (op0, 0), + chrec_convert (optype, + op1, + NULL)), + NULL); + return chrec_dont_know; + } /* FALLTHRU */ default: + gcc_checking_assert (!tree_contains_chrecs (op0, NULL)); if (integer_onep (op0)) return op1; @@ -540,7 +548,7 @@ chrec_fold_multiply (tree type, CASE_CONVERT: if (tree_contains_chrecs (op1, NULL)) - return chrec_dont_know; + return chrec_fold_multiply (type, op1, op0); /* FALLTHRU */ default: