From patchwork Wed Nov 22 14:40:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 168403 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2b07:b0:403:3b70:6f57 with SMTP id io7csp1370493vqb; Wed, 22 Nov 2023 06:40:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IGNCjRBOKwghDcmit3V/vxhmDtDqK8sGCAfwSdWV8bDRbg6uYevmmtylegri8v4HmpdMKD5 X-Received: by 2002:a9d:7f04:0:b0:6d6:4be1:442d with SMTP id j4-20020a9d7f04000000b006d64be1442dmr2858066otq.6.1700664042032; Wed, 22 Nov 2023 06:40:42 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700664042; cv=pass; d=google.com; s=arc-20160816; b=LH7QB7cNk5eOdsBI9yDgW7MHKe7XgY35foZm+fXl21UsGdUJa7qJLmOEqk5rdewHS5 NC9cW+tngcYfhSETGsRgE4B8bkVAsNxwve4Ous3mOYUN3VPxVbrhBUq0/D9vOc0kz1uD H/25eyaIytKbS5wuligMIoGg7EsfLjbdZH8GO7GYsYoQWHYoXh9usU+HS4bcTeTlWtn4 rsGUHtE5kqa0QlZgECFEKugHEt/4YYYo4uZL3do8lqw3CgD7C1aTnysSGJFRBlNWadNs ov2b3zsnLmdoKLE7eSYYUedphwq1qHfoeuBpg4PO3zfq1oU3hi6XqN4OghtkiTt0Ixfd IIVA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:errors-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:mime-version :user-agent:subject:to:from:date:dkim-signature:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=P5kCKihStfrUX5uMmHbBOwm/37TWyeX33Inodm8BMrM=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=LmZRlwIXiPX+4aFOE2E94j8jFJIeGRjPFc/gonohQxS4v3KpCicNTvTeakMcvq8gLO /kqQ3pcj1D/4DtdaXhyWs7KntkcWOLtSuvcIjGPRtYmk4/WjS8dnC/OalxHQel+MWLWD 8U8LXGvTOXNVOpFy4MZtMaeU6nOZ3lYh+HH1fnNFytqtWKqXtz8D4PTGPckh/CncKH+u rKJ80+PgB4bUnvPWb3su0ZJ9xoDSTthspGFrQCtqZaVWy5piWx2LgaBSiQomFqqa6tWf 7B86CMxher+gyaxPTOmuN3OlvkHE5E/JMz10YDpPcPDk0LuDDUBr5FnayEB9g+2sMIFC 3ueg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=1yn2njvm; dkim=neutral (no key) header.i=@suse.de; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id eu19-20020ad44f53000000b0066d07d8ecc3si10805776qvb.610.2023.11.22.06.40.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 06:40:42 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=1yn2njvm; dkim=neutral (no key) header.i=@suse.de; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C53983858C33 for ; Wed, 22 Nov 2023 14:40:41 +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:2001:67c:2178:6::1d]) by sourceware.org (Postfix) with ESMTPS id 473753858D39 for ; Wed, 22 Nov 2023 14:40:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 473753858D39 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 473753858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:67c:2178:6::1d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700664019; cv=none; b=ezF8pata+mH0Y7uFSazViGeyCS3VSivWz5L6bwBZiJWSGoEcOZwrtDVE5zqkGs9RdPETi+VpgzUwguXoF+sm4ovHdO2B38qsmNtZGZBLhIM8fxuLcERoHs7PNGo1e8XeLvUAS1KkowvoOLjvfJzt1Mrw/vm4+g4Dm0OP+k5GuMQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700664019; c=relaxed/simple; bh=FHTifQktVWFPMRqMFXdCVNpigzk4vULlv3KqbgL+uJI=; h=DKIM-Signature:DKIM-Signature:Date:From:To:Subject:MIME-Version; b=S/5dIHe1/URhCDk72wGD9MLMdorp0r3+sVpuAOmYRVFZtCe6AnsLznosUGG9jHJ+J+q1bUaS42d62qq9XBWz5D7FlFMDC7W8FuKutYyhyMBsCXzY2HqEydSSf1wjWf1NiTNOuNVs+f+5K8wj2Nv/rNz7iAUTZVJ5S6RGfcNcroc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id EC0311F8D7 for ; Wed, 22 Nov 2023 14:40:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1700664017; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=P5kCKihStfrUX5uMmHbBOwm/37TWyeX33Inodm8BMrM=; b=1yn2njvm59TicHiX/iXTvAZ8k/VExP1soSHsukMJrKrjgv+Qh1tpeqGvdEOaqajmo0nExp t0JeNATo89O7dCNFxQoYTJeCS+dFZlGsxRr8vKkIvPFLWu+BFDMG+i1k5W0Zc4gB64lwgQ /E0L16eGhi2dGpS1smisetlMcgVDpHg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1700664017; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=P5kCKihStfrUX5uMmHbBOwm/37TWyeX33Inodm8BMrM=; b=mjaTXxmK7gNC4J03/KH/UoFFSoIa4twXcfcU9t1SoYA4LGvTtd+IICHxytXp042oVTabFW qGBlJWLLpg/zDfAQ== Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (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 E9F032C14F for ; Wed, 22 Nov 2023 14:40:16 +0000 (UTC) Date: Wed, 22 Nov 2023 14:40:16 +0000 (UTC) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/112344 - wrong final value replacement User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 X-Spamd-Bar: ++++++++++++++++ Authentication-Results: smtp-out2.suse.de; dkim=none; dmarc=none; spf=softfail (smtp-out2.suse.de: 149.44.160.134 is neither permitted nor denied by domain of rguenther@suse.de) smtp.mailfrom=rguenther@suse.de X-Rspamd-Server: rspamd2 X-Spamd-Result: default: False [16.49 / 50.00]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; RWL_MAILSPIKE_GOOD(-1.00)[149.44.160.134:from]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_SPAM_SHORT(3.00)[1.000]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[gcc-patches@gcc.gnu.org]; TO_DN_NONE(0.00)[]; R_SPF_SOFTFAIL(4.60)[~all:c]; RCPT_COUNT_ONE(0.00)[1]; MISSING_MID(2.50)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; MX_GOOD(-0.01)[]; NEURAL_SPAM_LONG(3.50)[1.000]; DMARC_NA(1.20)[suse.de]; VIOLATED_DIRECT_SPF(3.50)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_NO_TLS_LAST(0.10)[]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(2.20)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; BAYES_HAM(-3.00)[100.00%] X-Spam-Score: 16.49 X-Rspamd-Queue-Id: EC0311F8D7 X-Spam-Status: No, score=-10.7 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+ouuuleilei=gmail.com@gcc.gnu.org Message-Id: <20231122144041.C53983858C33@sourceware.org> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783275498488412675 X-GMAIL-MSGID: 1783275498488412675 When performing final value replacement chrec_apply that's used to compute the overall effect of niters to a CHREC doesn't consider that the overall increment of { -2147483648, +, 2 } doesn't fit in a signed integer when the loop iterates until the value of the IV of 20. The following fixes this mistake, carrying out the multiply and add in an unsigned type instead, avoiding undefined overflow and thus later miscompilation by path range analysis. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/112344 * tree-chrec.cc (chrec_apply): Perform the overall increment calculation and increment in an unsigned type. * gcc.dg/torture/pr112344.c: New testcase. --- gcc/testsuite/gcc.dg/torture/pr112344.c | 20 ++++++++++++++++ gcc/tree-chrec.cc | 32 ++++++++++++++++--------- 2 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr112344.c diff --git a/gcc/testsuite/gcc.dg/torture/pr112344.c b/gcc/testsuite/gcc.dg/torture/pr112344.c new file mode 100644 index 00000000000..c52d2c8304b --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr112344.c @@ -0,0 +1,20 @@ +/* { dg-do run } */ +/* { dg-require-effective-target int32plus } */ + +int +main () +{ + long long b = 2036854775807LL; + signed char c = 3; + short d = 0; + int e = -2147483647 - 1, f; + for (f = 0; f < 7; f++) + while (e < 20) + { + e += 2; + d = c -= b; + } + if (d != 13) + __builtin_abort (); + return 0; +} diff --git a/gcc/tree-chrec.cc b/gcc/tree-chrec.cc index 2f67581591a..f4ba130ba20 100644 --- a/gcc/tree-chrec.cc +++ b/gcc/tree-chrec.cc @@ -613,32 +613,42 @@ chrec_apply (unsigned var, if (evolution_function_is_affine_p (chrec)) { tree chrecr = CHREC_RIGHT (chrec); + tree chrecl = CHREC_LEFT (chrec); if (CHREC_VARIABLE (chrec) != var) - res = build_polynomial_chrec - (CHREC_VARIABLE (chrec), - chrec_apply (var, CHREC_LEFT (chrec), x), - chrec_apply (var, chrecr, x)); + res = build_polynomial_chrec (CHREC_VARIABLE (chrec), + chrec_apply (var, chrecl, x), + chrec_apply (var, chrecr, x)); - /* "{a, +, b} (x)" -> "a + b*x". */ - else if (operand_equal_p (CHREC_LEFT (chrec), chrecr) + /* "{a, +, a}" (x-1) -> "a*x". */ + else if (operand_equal_p (chrecl, chrecr) && TREE_CODE (x) == PLUS_EXPR && integer_all_onesp (TREE_OPERAND (x, 1)) && !POINTER_TYPE_P (type) && TYPE_PRECISION (TREE_TYPE (x)) >= TYPE_PRECISION (type)) { - /* We know the number of iterations can't be negative. - So {a, +, a} (x-1) -> "a*x". */ + /* We know the number of iterations can't be negative. */ res = build_int_cst (TREE_TYPE (x), 1); res = chrec_fold_plus (TREE_TYPE (x), x, res); res = chrec_convert_rhs (type, res, NULL); res = chrec_fold_multiply (type, chrecr, res); } + /* "{a, +, b} (x)" -> "a + b*x". */ else { - res = chrec_convert_rhs (TREE_TYPE (chrecr), x, NULL); - res = chrec_fold_multiply (TREE_TYPE (chrecr), chrecr, res); - res = chrec_fold_plus (type, CHREC_LEFT (chrec), res); + /* The overall increment might not fit in a signed type so + use an unsigned computation to get at the final value + and avoid undefined signed overflow. */ + tree utype = TREE_TYPE (chrecr); + if (INTEGRAL_TYPE_P (utype) && !TYPE_OVERFLOW_WRAPS (utype)) + utype = unsigned_type_for (TREE_TYPE (chrecr)); + res = chrec_convert_rhs (utype, x, NULL); + res = chrec_fold_multiply (utype, + chrec_convert (utype, chrecr, NULL), + res); + res = chrec_fold_plus (utype, + chrec_convert (utype, chrecl, NULL), res); + res = chrec_convert (type, res, NULL); } } else if (TREE_CODE (x) == INTEGER_CST