From patchwork Fri Feb 16 11:51:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 85876 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0CB1538582B9 for ; Fri, 16 Feb 2024 11:52:04 +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 925AB3858292 for ; Fri, 16 Feb 2024 11:51:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 925AB3858292 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 925AB3858292 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=1708084296; cv=none; b=dDxAz5qWdpeuAFXAcYeWYcbz7G7oplhrdCF+QZPjh3vwZQZMsBd7s1soP39vrtM4hRhBMmQrMSw8ZRzjKjoeFHy/KxIvku0bJKgLZNGzfmGWw0fhQ4MAGzkG+KRoKHaAGiUG4zVeBq9ZFiGUutS1ykm0/sWL2AkvLiwer40XjOc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708084296; c=relaxed/simple; bh=cgdkTk9uv8hAbGNkt5ZrOA97y3bSyX8HbzAPgt8zdP8=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version:Message-Id; b=Wi9F7s0Q7ZRhtIr5ZigzJjaTQQUrRtAFyVxB3kxhgZioQH1g5cXrSCgt0GaCqZ+iMNTNJt3zk+ywYkFU89we9DhyWPp1HK0umYhwQxlma+b5KoQAHm+dWNW3cK97yQjcHqAAYoyEc7b8/cQmo35uhhkyRZy0MXJ48Q1i9hdIjfo= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap2.dmz-prg2.suse.org (imap2.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:98]) (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 5E62C1FB65 for ; Fri, 16 Feb 2024 11:51:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1708084291; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=Y7sTuMeQ8c+73JXg6dUpPsN2Ix74+usKYMdmxyx7Lz0=; b=ZCRC8W/+ZBSYJfC3sqB3eBvyMJeyEXCN6T6mCarABjtCPz8lDZdvdj40VgQK50uQiL1O2M cE/aZPme177mRxb5YCvBYYjbp0Zx/41rX+kjqqPpp1eVaWRpM9LZXyJWXDO702hNiUXQD8 4Y3vEgHWhU391XXUK+1TUtoLj42LinE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1708084291; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=Y7sTuMeQ8c+73JXg6dUpPsN2Ix74+usKYMdmxyx7Lz0=; b=jYSF5+E71d90l/lDSPd34ELLmFTXNHsBgdUpH0JtkiMTXOvXV0F7xEhd1bBjA9NEbPhceo 1wxLA34MLs91iGAw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1708084291; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=Y7sTuMeQ8c+73JXg6dUpPsN2Ix74+usKYMdmxyx7Lz0=; b=ZCRC8W/+ZBSYJfC3sqB3eBvyMJeyEXCN6T6mCarABjtCPz8lDZdvdj40VgQK50uQiL1O2M cE/aZPme177mRxb5YCvBYYjbp0Zx/41rX+kjqqPpp1eVaWRpM9LZXyJWXDO702hNiUXQD8 4Y3vEgHWhU391XXUK+1TUtoLj42LinE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1708084291; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=Y7sTuMeQ8c+73JXg6dUpPsN2Ix74+usKYMdmxyx7Lz0=; b=jYSF5+E71d90l/lDSPd34ELLmFTXNHsBgdUpH0JtkiMTXOvXV0F7xEhd1bBjA9NEbPhceo 1wxLA34MLs91iGAw== Received: from imap2.dmz-prg2.suse.org (localhost [127.0.0.1]) (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 imap2.dmz-prg2.suse.org (Postfix) with ESMTPS id 4263B13421 for ; Fri, 16 Feb 2024 11:51:31 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap2.dmz-prg2.suse.org with ESMTPSA id DzJaDkNMz2UaGQAAn2gu4w (envelope-from ) for ; Fri, 16 Feb 2024 11:51:31 +0000 Date: Fri, 16 Feb 2024 12:51:30 +0100 (CET) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/113895 - consistency check fails in copy_reference_ops_from_ref MIME-Version: 1.0 Message-Id: <20240216115131.4263B13421@imap2.dmz-prg2.suse.org> X-Spam-Level: Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b="ZCRC8W/+"; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=jYSF5+E7 X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Spamd-Result: default: False [-4.51 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:98:from]; FROM_HAS_DN(0.00)[]; DWL_DNSWL_BLOCKED(0.00)[suse.de:dkim]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-1.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]; RCVD_COUNT_THREE(0.00)[3]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; DKIM_TRACE(0.00)[suse.de:+]; MX_GOOD(-0.01)[]; NEURAL_HAM_SHORT(-0.20)[-0.988]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Score: -4.51 X-Rspamd-Queue-Id: 5E62C1FB65 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, 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+patchwork=sourceware.org@gcc.gnu.org The following addresses consistency check fails in copy_reference_ops_from_ref when we are handling out-of-bound array accesses (it's almost impossible to identically mimic the get_ref_base_and_extent behavior). It also addresses the case where an out-of-bound constant offset computes to a -1 off which is the special value for "unknown". This patch basically turns off verification in those cases. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/113895 * tree-ssa-sccvn.cc (copy_reference_ops_from_ref): Disable consistency checking when there are out-of-bound array accesses. Allow -1 off when from an array reference with constant index. * gcc.dg/torture/pr113895-2.c: New testcase. * gcc.dg/torture/pr113895-3.c: Likewise. * gcc.dg/torture/pr113895-4.c: Likewise. --- gcc/testsuite/gcc.dg/torture/pr113895-2.c | 13 ++++++++++ gcc/testsuite/gcc.dg/torture/pr113895-3.c | 10 ++++++++ gcc/testsuite/gcc.dg/torture/pr113895-4.c | 14 ++++++++++ gcc/tree-ssa-sccvn.cc | 31 +++++++++++++++++++++-- 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr113895-2.c create mode 100644 gcc/testsuite/gcc.dg/torture/pr113895-3.c create mode 100644 gcc/testsuite/gcc.dg/torture/pr113895-4.c diff --git a/gcc/testsuite/gcc.dg/torture/pr113895-2.c b/gcc/testsuite/gcc.dg/torture/pr113895-2.c new file mode 100644 index 00000000000..a1c20250c99 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr113895-2.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ + +extern void d(int); +int a[2][4], b; +int main() { + while (b) { + int c; + d(a[b][c]); + for (c = 0; c < 7; c++) + ; + } + return 0; +} diff --git a/gcc/testsuite/gcc.dg/torture/pr113895-3.c b/gcc/testsuite/gcc.dg/torture/pr113895-3.c new file mode 100644 index 00000000000..255975f3353 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr113895-3.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ + +extern void f(); +char a[1][1], b; +int main() { + int c = -1U; + if (b) + f(a[c][b]); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/torture/pr113895-4.c b/gcc/testsuite/gcc.dg/torture/pr113895-4.c new file mode 100644 index 00000000000..75f71dcb451 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr113895-4.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ + +long a, b, c; +int d; +long e[2][1]; +int f() { + if (c == a) + c = b; +} +void g() { + int h, i = 0; + for (; f() + d + i; i++) + e[h][i] = 4; +} diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc index 707a62e94a3..e737c8c6a69 100644 --- a/gcc/tree-ssa-sccvn.cc +++ b/gcc/tree-ssa-sccvn.cc @@ -1107,9 +1107,29 @@ copy_reference_ops_from_ref (tree ref, vec *result) the vn_reference ops differ by adjusting those indexes to appropriate constants. */ poly_int64 off = 0; + bool oob_index = false; for (unsigned i = result->length (); i > start; --i) { auto &op = (*result)[i-1]; + if (flag_checking + && op.opcode == ARRAY_REF + && TREE_CODE (op.op0) == INTEGER_CST) + { + /* The verifier below chokes on inconsistencies of handling + out-of-bound accesses so disable it in that case. */ + tree atype = (*result)[i].type; + if (TREE_CODE (atype) == ARRAY_TYPE) + if (tree dom = TYPE_DOMAIN (atype)) + if ((TYPE_MIN_VALUE (dom) + && TREE_CODE (TYPE_MIN_VALUE (dom)) == INTEGER_CST + && (wi::to_widest (op.op0) + < wi::to_widest (TYPE_MIN_VALUE (dom)))) + || (TYPE_MAX_VALUE (dom) + && TREE_CODE (TYPE_MAX_VALUE (dom)) == INTEGER_CST + && (wi::to_widest (op.op0) + > wi::to_widest (TYPE_MAX_VALUE (dom))))) + oob_index = true; + } if ((op.opcode == ARRAY_REF || op.opcode == ARRAY_RANGE_REF) && TREE_CODE (op.op0) == SSA_NAME) @@ -1162,12 +1182,19 @@ copy_reference_ops_from_ref (tree ref, vec *result) } else { - gcc_assert (known_ne (op.off, -1)); + gcc_assert (known_ne (op.off, -1) + /* Out-of-bound indices can compute to + a known -1 offset. */ + || ((op.opcode == ARRAY_REF + || op.opcode == ARRAY_RANGE_REF) + && poly_int_tree_p (op.op0) + && poly_int_tree_p (op.op1) + && TREE_CODE (op.op2) == INTEGER_CST)); off += op.off * BITS_PER_UNIT; } } } - if (flag_checking) + if (flag_checking && !oob_index) { ao_ref r; if (start != 0)