From patchwork Mon Feb 12 12:28:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 199718 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp2393584dyd; Mon, 12 Feb 2024 04:29:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IEPPLBARn1J8hT3Cw/u4o+AF0hNPpug6VelsauzBLD3DwwvxC+fJIyUF6owsjhUTU9O8Qwm X-Received: by 2002:a0c:aa5a:0:b0:68c:aac2:3020 with SMTP id e26-20020a0caa5a000000b0068caac23020mr8023860qvb.1.1707740942617; Mon, 12 Feb 2024 04:29:02 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707740942; cv=pass; d=google.com; s=arc-20160816; b=0TMJEO36nEjdtmpL828eAZSLDywLn8aySkMSjQ4gKCLpEj1VDcOC93AOrrrHYBdOo/ IR3Y+qo3Mf3w0n58zJaBKiW5rUcFbzi7RsSDFEW2vxqNzXMYICgF+28r98KqmqZu8wRB aHPNr0Q3a/Uk2dPpLnODxUAZd9Dil04VQuuZvJLkQvc9Sp/R9JFbJuFG7PYd6ZP5p5RU LHGL3YJ6BJIntfqezTjnaT166cK1IQMF8jonk0i9lzUV56S79AEQPS6rVHyle34nHkPQ dJ2SBa/ZB+Xka2TVlnN17gKv4GGkI1wrXVxx4zRzgJy4p8By5FfLwFDAUC4dqopaEoYu ZcXQ== 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 :subject:to:from:date:dkim-signature:dkim-signature:dkim-signature :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=kxXla5RZ9DOkY+KdjnVl3bbRPSJaMMavPjLFl1izhoI=; fh=HEVstH+qpnGVTPcM3h1YOY9Kge5QSBDqyA5sqg5VNGA=; b=OZZO10h20ED1+YcQ4wwXwagxuOg2pEXeVPNFy1m0ajdSb9Jv+It9KgwyU0c/NDxakU cSU6kj/4GtRCXcgUap7DocqLnx8R7Lfa0xW7uLdqTDWu35rL4eTkr2GdJKp0cOdx86Hv bJtLbEjuy0rpXoVBj3R8oFsr8H4Hk2tj/mFhJOkcXSVQpVwNa+sUFvm6hzAmXKzkVvY8 u7A1Xe/zlmsddrsCLbOxrKwc6BYAR5/O++1kIoJo1gMztLO9hCReffXDN7eGuIqhzHHJ W/XOXjhJT/LnmAfUZ4gTx3t/dJ+e5XVLEfG/741w/I1vDPzx37iu/K2AbuBubOyqWy+M XQ4g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b="JW4Y/k25"; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=rhq0xRAL; 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 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 X-Forwarded-Encrypted: i=2; AJvYcCVJtHvkW4uUUlLXZl07LaUrPKj3oleymG1yGVhEjBPHZfzddv4tKyt+wlEitoKoNRfty/sOv63v2/Z8rQR8LSTcTcJSMw== Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id t13-20020ad45bcd000000b0068c964590e3si251435qvt.210.2024.02.12.04.29.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 04:29:02 -0800 (PST) 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="JW4Y/k25"; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=rhq0xRAL; 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 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 4DF883858C98 for ; Mon, 12 Feb 2024 12:29:02 +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.223.130]) by sourceware.org (Postfix) with ESMTPS id 02B133858CDB for ; Mon, 12 Feb 2024 12:28:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 02B133858CDB 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 02B133858CDB Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707740901; cv=none; b=a7EM1wfoW9udbhZGFJrU79Ug2D5PlTVWCw+yo2v9na3WnCw00Moq4o6WB5Gomlpdih4RluCInQTfOl9dWyCDZ0kgtAUQsOva1qAoTlZUKqxSxasAl0AufgwCdVntI0v6k4SShR0PdAGUyQ8upOCno7NWlXutPHF1WAI1cuX5pvQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707740901; c=relaxed/simple; bh=lRhTcHz9pDObBPmveo0fbQAKZIdhWSgZ/iHP87Ujnok=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version; b=wUbIW2Hfz9eTLWKr3mHs5GvS46syPR6A55Mg1GPtQ5OW7Lrgb2WgH6Bmw/oAlazSP5oX3eLi/NqWNYqi3qDlE1uNxMc6FjgrAK0kIcbxv0PWoa/+9hE3ikIXwRr5Mj56J4tlMXWDJO8beQzalNKK/U0beJKdyBjiTazRsUqU9Zk= 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-out1.suse.de (Postfix) with ESMTPS id B935521E23 for ; Mon, 12 Feb 2024 12:28:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707740897; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=kxXla5RZ9DOkY+KdjnVl3bbRPSJaMMavPjLFl1izhoI=; b=JW4Y/k25Z7LGCAZMEvfl0ha2Adm2lmxSbWPQgrZJV0AaiSAZ8miBSDz1QRflKMO/4tumaZ CIyb7kSJpv2DRxeCrfMfdAVrAYqiMp0azM6XKO6jaSbCwT+A7GlPs0K6cZnyN4BunbFQC4 ox6dMwn1EjMrF3FYokqQPMcLa+t1t1I= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707740897; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=kxXla5RZ9DOkY+KdjnVl3bbRPSJaMMavPjLFl1izhoI=; b=bI1qH+hCNhlLaVaoUwihsNQhkDX90bB2dK50JVw7DzDU8plrpasCyOSzGEqlL4LwOzU8rR YH3iC1ZfrSsf2LAg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707740895; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=kxXla5RZ9DOkY+KdjnVl3bbRPSJaMMavPjLFl1izhoI=; b=rhq0xRALhM5Ts+bCv/I0FEWH+o9DcGXoEbUaem4+i9CuFc97ai1hYjBAOnZg8pcm/gteIn AkNgSHzauBXf8NM9iKAYFmXmUTySeXTzVKUOlz5HN8+3uIL8zUqTLEVYvzb25uWERZwkQm Pa0rHlqBqroHLza+FBTtA0/nZOEDd5k= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707740895; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=kxXla5RZ9DOkY+KdjnVl3bbRPSJaMMavPjLFl1izhoI=; b=/s8BwuNNRvnv5bIStWkEqWeNSurczVs3LLQXfe4ix6xoA7u+Kdfdob+rqyDnmqcyyW+oO1 v5MakTpXOFaGNfDw== Date: Mon, 12 Feb 2024 13:28:15 +0100 (CET) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/113831 - wrong VN with structurally identical ref MIME-Version: 1.0 Authentication-Results: smtp-out1.suse.de; none X-Spam-Score: 2.64 X-Spamd-Result: default: False [2.64 / 50.00]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; 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.15)[-0.728]; NEURAL_SPAM_LONG(3.38)[0.966]; 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+ouuuleilei=gmail.com@gcc.gnu.org Message-Id: <20240212122902.4DF883858C98@sourceware.org> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790696166517378680 X-GMAIL-MSGID: 1790696166517378680 When we use get_ref_base_and_extent during VN and that ends up using global ranges to restrict the range of a ref we have to take care of not using the same expression in the hashtable as for a ref that could not use that global range. The following attempts to ensure this by applying similar logic as get_ref_base_and_extent to copy_reference_ops_from_ref so they behave consistent. Bootstrapped and tested on x86_64-unknown-linux-gnu for all languages, pushed. Richard. PR tree-optimization/113831 PR tree-optimization/108355 * tree-ssa-sccvn.cc (copy_reference_ops_from_ref): When we see variable array indices and get_ref_base_and_extent can resolve those to constants fix up the ops to constants as well. (ao_ref_init_from_vn_reference): Use 'off' member for ARRAY_REF and ARRAY_RANGE_REF instead of recomputing it. (valueize_refs_1): Also fixup 'off' of ARRAY_RANGE_REF. * gcc.dg/torture/pr113831.c: New testcase. * gcc.dg/tree-ssa/ssa-fre-104.c: Likewise. --- gcc/testsuite/gcc.dg/torture/pr113831.c | 26 +++++ gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-104.c | 24 +++++ gcc/tree-ssa-sccvn.cc | 103 +++++++++++++++++--- 3 files changed, 139 insertions(+), 14 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr113831.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-104.c diff --git a/gcc/testsuite/gcc.dg/torture/pr113831.c b/gcc/testsuite/gcc.dg/torture/pr113831.c new file mode 100644 index 00000000000..12aea7ce0bc --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr113831.c @@ -0,0 +1,26 @@ +/* { dg-do run } */ + +int a[3]; +int __attribute__((noipa)) +foo(int i, int x) +{ + int tem = 0; + a[2] = x; + if (i < 1) + ++i; + else + { + ++i; + tem = a[i]; + } + tem += a[i]; + return tem; +} + +int +main() +{ + if (foo (0, 7) != 0) + __builtin_abort(); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-104.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-104.c new file mode 100644 index 00000000000..f0f12ef82b7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-104.c @@ -0,0 +1,24 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-fre1" } */ + +int a; +int *b = &a; +int **c = &b; +int d; +void bar25_(void); +void foo(void); +int main() { + int e[][1] = {0, 0, 0, 0, 0, 1}; + for (;;) { + bar25_(); + /* We should optimistically treat a == 0 because of the bounds of + the object. */ + if (e[5][a]) + break; + e[a][0] = 0; + foo(); + } + *c = &d; +} + +/* { dg-final { scan-tree-dump-not "foo" "fre1" } } */ diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc index 8792cd07901..2823573b656 100644 --- a/gcc/tree-ssa-sccvn.cc +++ b/gcc/tree-ssa-sccvn.cc @@ -898,6 +898,8 @@ copy_reference_ops_from_ref (tree ref, vec *result) { /* For non-calls, store the information that makes up the address. */ tree orig = ref; + unsigned start = result->length (); + bool seen_variable_array_ref = false; while (ref) { vn_reference_op_s temp; @@ -984,6 +986,12 @@ copy_reference_ops_from_ref (tree ref, vec *result) tree eltype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref, 0))); /* Record index as operand. */ temp.op0 = TREE_OPERAND (ref, 1); + /* When the index is not constant we have to apply the same + logic as get_ref_base_and_extent which eventually uses + global ranges to refine the overall ref extent. Record + we've seen such a case, fixup below. */ + if (TREE_CODE (temp.op0) == SSA_NAME) + seen_variable_array_ref = true; /* Always record lower bounds and element size. */ temp.op1 = array_ref_low_bound (ref); /* But record element size in units of the type alignment. */ @@ -1076,6 +1084,82 @@ copy_reference_ops_from_ref (tree ref, vec *result) else ref = NULL_TREE; } + poly_int64 offset, size, max_size; + tree base; + bool rev; + if (seen_variable_array_ref + && handled_component_p (orig) + && (base = get_ref_base_and_extent (orig, + &offset, &size, &max_size, &rev)) + && known_size_p (max_size) + && known_eq (size, max_size)) + { + poly_int64 orig_offset = offset; + poly_int64 tem; + if (TREE_CODE (base) == MEM_REF + && mem_ref_offset (base).to_shwi (&tem)) + offset += tem * BITS_PER_UNIT; + HOST_WIDE_INT coffset = offset.to_constant (); + /* When get_ref_base_and_extent computes an offset constrained to + a constant position we have to fixup variable array indexes in + the ref to avoid the situation where based on context we'd have + to value-number the same vn_reference ops differently. Make + the vn_reference ops differ by adjusting those indexes to + appropriate constants. */ + poly_int64 off = 0; + for (unsigned i = result->length (); i > start; --i) + { + auto &op = (*result)[i-1]; + if ((op.opcode == ARRAY_REF + || op.opcode == ARRAY_RANGE_REF) + && TREE_CODE (op.op0) == SSA_NAME) + { + /* There's a single constant index that get's 'off' closer + to 'offset'. */ + unsigned HOST_WIDE_INT elsz + = tree_to_uhwi (op.op2) * vn_ref_op_align_unit (&op); + unsigned HOST_WIDE_INT idx + = (coffset / BITS_PER_UNIT - off.to_constant ()) / elsz; + if (idx == 0) + op.op0 = op.op1; + else + op.op0 = wide_int_to_tree (TREE_TYPE (op.op0), + wi::to_poly_wide (op.op1) + idx); + op.off = idx * elsz; + } + else + { + if (op.opcode == ERROR_MARK) + /* two-ops codes have the offset in the first op. */ + ; + else if (op.opcode == ADDR_EXPR + || op.opcode == SSA_NAME + || op.opcode == CONSTRUCTOR + || TREE_CODE_CLASS (op.opcode) == tcc_declaration + || TREE_CODE_CLASS (op.opcode) == tcc_constant) + /* end-of ref. */ + gcc_assert (i == result->length ()); + else + { + gcc_assert (known_ne (op.off, -1)); + off += op.off; + } + } + } + if (flag_checking) + { + ao_ref r; + if (start != 0) + ; + else if (ao_ref_init_from_vn_reference (&r, 0, 0, TREE_TYPE (orig), + *result)) + gcc_assert (known_eq (r.offset, orig_offset) + && known_eq (r.size, size) + && known_eq (r.max_size, max_size)); + else + gcc_unreachable (); + } + } } /* Build a alias-oracle reference abstraction in *REF from the vn_reference @@ -1203,21 +1287,11 @@ ao_ref_init_from_vn_reference (ao_ref *ref, case ARRAY_RANGE_REF: case ARRAY_REF: - /* We recorded the lower bound and the element size. */ - if (!poly_int_tree_p (op->op0) - || !poly_int_tree_p (op->op1) - || TREE_CODE (op->op2) != INTEGER_CST) + /* Use the recored constant offset. */ + if (maybe_eq (op->off, -1)) max_size = -1; else - { - poly_offset_int woffset - = wi::sext (wi::to_poly_offset (op->op0) - - wi::to_poly_offset (op->op1), - TYPE_PRECISION (sizetype)); - woffset *= wi::to_offset (op->op2) * vn_ref_op_align_unit (op); - woffset <<= LOG2_BITS_PER_UNIT; - offset += woffset; - } + offset += op->off << LOG2_BITS_PER_UNIT; break; case REALPART_EXPR: @@ -1724,7 +1798,8 @@ re_valueize: } /* If it transforms a non-constant ARRAY_REF into a constant one, adjust the constant offset. */ - else if (vro->opcode == ARRAY_REF + else if ((vro->opcode == ARRAY_REF + || vro->opcode == ARRAY_RANGE_REF) && known_eq (vro->off, -1) && poly_int_tree_p (vro->op0) && poly_int_tree_p (vro->op1)