From patchwork Fri Oct 20 13:18:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 156103 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2010:b0:403:3b70:6f57 with SMTP id fe16csp1050715vqb; Fri, 20 Oct 2023 06:18:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE9Bfye2w4EncWB7ivJp102OPfmHOhvGNzDw+GGUUJODBBU44aGedikDGRvColSEJ7DK+ED X-Received: by 2002:a25:aa2c:0:b0:d9b:f27:c76f with SMTP id s41-20020a25aa2c000000b00d9b0f27c76fmr1654109ybi.20.1697807924654; Fri, 20 Oct 2023 06:18:44 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697807924; cv=pass; d=google.com; s=arc-20160816; b=JzSct+gdekiX1pj4Q0an+BKpAWsj/nue1JzWmuBXqAZRydW7/iJarn49Z9LAV/s6gE 1IVWJMrnvgA2P2ORaahuCtw7wUA2YFH8t6tmjZRbU1Igdeu/1C6Nr6MXdNozjO+uffRw ZtaPxa6mrye2gCd5Dy+VzhwsozD/d7JmVH0X227JzesanamwnCS8wNQfrqfg2g4NR1p/ kpdyDkzUYQ8+WLGmPkX24atPQRutpAJx3hxohGIXXOfQp/dFG2rw9HW9YUJDH9V+zs1o 1NUiMGOVzqI2+EqeaqN3Ty2qQQwoW2Hg/EiNby5CqvWRYJlBFUGg6uzU/PPcN/JnRtnX btmA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:message-id:mime-version:subject :to:from:date:dkim-signature:dkim-signature:arc-filter:dmarc-filter :delivered-to; bh=UNG73APBi1q7Zu/GIDlor3mD/AqBn++yexIs0KhmN2A=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=jVjgNapKQ449vN4GK7WlQ8Uo6aZSU6948g7iP7BPt+Xiz5ub0plrh/gXECcT2+QjXy K7HWkhE2d02VQ9EjQ653iCSFaSEIlyagxiRQWruqfKEPIJfMnxEvqBz1W0QbKvgqZsPc OKEh5aQF4julOD/uM/AtbiwjjDL1lGULHDzrz9jSjAkvp/tpu6LT1uWYXif+QA8KZje2 wFMHE24Iy2j9sH3oNSMrk/F9Nquupu5Z2TrwelfJ52vlcvDk602SzOV4vehsdjeUvYF2 12HNCtni670OzLkgbkFsBwM3q2vw4mSnmRAhFHFx+CjAFhsd2hZCBwor3tOtu3Ns014r wATg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=0NuJnNKr; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id pi17-20020a0562144a9100b0066d0656ef0esi1427423qvb.615.2023.10.20.06.18.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 06:18:44 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=0NuJnNKr; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 6A9523858413 for ; Fri, 20 Oct 2023 13:18:44 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 27DD13858D35 for ; Fri, 20 Oct 2023 13:18:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 27DD13858D35 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 27DD13858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.220.28 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697807901; cv=none; b=FPTyJyrEF2GjHj5xv5J63lEltjISoZsZ37IECs7zHbqT9YTcuQdRsSCdIiN+ZnSLEE4NTWDRaSdOuRvoWMes+J0lxJD3Pm5bXoUOYhag0fqAY/rnpWo8aeI0LoJFZYTLtWMA0jLrQdeaGKXaFGH+PJBQAQmGWiEiUwzbhWu5bWY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697807901; c=relaxed/simple; bh=ppAMK1S13sKV6AKq1YK8siRaqEjUDFrIPOyOKaqjn4c=; h=DKIM-Signature:DKIM-Signature:Date:From:To:Subject:MIME-Version: Message-Id; b=wnYOwFKzKCCwEjayUk2WnnTSD/6eYm9zR/AFFOLIYi9xzizM94jB+gtGEyaIyTiGnHiTE9MR75KIjpudXZLKWjibdXrgs7cetmyN15R12w6NVT7EbKRvnPB4tfS4Ped9bYYRKBdnBaHPQtabeAyDBW6u0K+TfCRZzQnEov/gxVY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 1E3F521A2E for ; Fri, 20 Oct 2023 13:18:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1697807890; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=UNG73APBi1q7Zu/GIDlor3mD/AqBn++yexIs0KhmN2A=; b=0NuJnNKrVLZIlHjWt+MHJCE3XHQkpcY1KB7r1YNKxIL5g+oXlR1Wyh8V2E4Tn/1bi8YJaj MaWP8aR4cyUH7HCi/Q/Sc4MOLI4Ey4KgHi1d6rwSbW4w5ZjncST76h+LUOAQG8TgDMKzUH iupsPOXIv0MDABix22bXY+hHaW5D0bk= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1697807890; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=UNG73APBi1q7Zu/GIDlor3mD/AqBn++yexIs0KhmN2A=; b=Yjy/SASqbwRSaKuA4JhxWLY07JAjbzwRfxAQnM/iGMKmPsMRmEeT4Z6InipZZ/K7MDDexF qLP0NW5/sc+hkUBw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 0AC64138E2 for ; Fri, 20 Oct 2023 13:18:10 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id iY52ARJ+MmXoHwAAMHmgww (envelope-from ) for ; Fri, 20 Oct 2023 13:18:10 +0000 Date: Fri, 20 Oct 2023 15:18:09 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/111445 - simple_iv simplification fault MIME-Version: 1.0 Message-Id: <20231020131810.0AC64138E2@imap2.suse-dmz.suse.de> Authentication-Results: smtp-out1.suse.de; none X-Spam-Level: X-Spam-Score: -7.10 X-Spamd-Result: default: False [-7.10 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-3.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[gcc-patches@gcc.gnu.org]; TO_DN_NONE(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; MID_RHS_MATCH_FROMTLD(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-1.00)[-1.000]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780280642194238345 X-GMAIL-MSGID: 1780280642194238345 The following fixes a missed check in the simple_iv attempt to simplify (signed T)((unsigned T) base + step) where it allows a truncating inner conversion leading to wrong code. Bootstrap and regtest running on x86_64-unknown-linux-gnu. PR tree-optimization/111445 * tree-scalar-evolution.cc (simple_iv_with_niters): Add missing check for a sign-conversion. * gcc.dg/torture/pr111445.c: New testcase. --- gcc/testsuite/gcc.dg/torture/pr111445.c | 29 +++++++++++++++++++++++++ gcc/tree-scalar-evolution.cc | 3 ++- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr111445.c diff --git a/gcc/testsuite/gcc.dg/torture/pr111445.c b/gcc/testsuite/gcc.dg/torture/pr111445.c new file mode 100644 index 00000000000..320e0b90675 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr111445.c @@ -0,0 +1,29 @@ +/* { dg-do run } */ + +extern void abort (void); +short a, b; +unsigned char c = 255; +unsigned cnt; +void __attribute__((noipa)) +check (int x) +{ + if (x != 0) + abort (); + cnt++; +} +int main() +{ + int d; + unsigned char e; + d = 0; + for (; a >= 0; a--) { + int *f = &d; + *f = c; + } + e = 0; + for (; (unsigned char)(d - 255) + e <= 1; e++) + check (b); + if (cnt != 2) + abort (); + return 0; +} diff --git a/gcc/tree-scalar-evolution.cc b/gcc/tree-scalar-evolution.cc index 7cafe5ce576..95a15fe0988 100644 --- a/gcc/tree-scalar-evolution.cc +++ b/gcc/tree-scalar-evolution.cc @@ -3286,7 +3286,8 @@ simple_iv_with_niters (class loop *wrto_loop, class loop *use_loop, type = TREE_TYPE (iv->base); e = TREE_OPERAND (iv->base, 0); - if (TREE_CODE (e) != PLUS_EXPR + if (!tree_nop_conversion_p (type, TREE_TYPE (e)) + || TREE_CODE (e) != PLUS_EXPR || TREE_CODE (TREE_OPERAND (e, 1)) != INTEGER_CST || !tree_int_cst_equal (iv->step, fold_convert (type, TREE_OPERAND (e, 1))))