From patchwork Fri Sep 9 12:31:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1117 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp691778wrt; Fri, 9 Sep 2022 05:32:13 -0700 (PDT) X-Google-Smtp-Source: AA6agR7wMbGKyCCQJy2aaHYSlTEmFHIOoV0+klwXhjs0j5OqqD5baPS7fb/LOX3X0MBqCeRsU6zF X-Received: by 2002:a50:fa8c:0:b0:44e:8557:477a with SMTP id w12-20020a50fa8c000000b0044e8557477amr11442057edr.26.1662726733744; Fri, 09 Sep 2022 05:32:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662726733; cv=none; d=google.com; s=arc-20160816; b=tg3P9UMMwE2CN8ShAiCesG3scj3aATA9K8y632C9xuplGS/XHhus36gp5iOHSpt5pq tR6B4oVBzbsOUTk4ALnS+mK9DbcjaTM3j2PwN/QUolIzLgmU17ShVAKK3LdAQN+kl0wB i80z8zICSQSwXQPR+icZCFG6UeuOuvTgpgSCtoHVkzVEi3UHmLW3z0YiwqhJR9vbjhs5 Sd+O+2pqrVuKXZV3r21GJqLoCiZAENgaJRh8L4ZMkkGrvMfkM8DW3ru2HWIJddE5UAM1 KXr9bMdJToLO6WByiFOcPvfunqq9XDDKxnpvLHbHo7ImTw7IzRiI7UV4X9tCnxUlaDM1 UPlg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:sender:errors-to:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :mime-version:user-agent:subject:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=U+6qrLuHKoFkyQlKyI6kXJXzCDdsEEPsVggblGXWlnA=; b=L+kk68xZgBbn82/s72qlPzfq8+QePPJLjoTFW5tdlVXwg4ILbrq92h2M31SB67toAG SUNYFoWEeTlmOKWRlipOpuzYRE1BvZ3RO7+PcFtbddZRpxOR6FoDfzqOanY+x6Ey7OcY fKFU223Nra3ZBP9szS1RHkV6bMC6cZN77gVGV6VqAMw4umeNKFIb5h4HvgjW98fYWED2 64XEAiftbZt7NSxvrBr2nzL74iW5K+1Lvf43mcrMnhy8QAoWDZK4BtVi2C7URQcEg4Ja 7RdHTVusyJ+Keja6D0PORR6dO1PUI6gR4repHmJQoK8B369MbeyfzNCGzmVpZy3pDvZo wv2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=AhocQ1VF; 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=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id b23-20020aa7cd17000000b004405af7da47si290929edw.564.2022.09.09.05.32.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Sep 2022 05:32:13 -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=@gcc.gnu.org header.s=default header.b=AhocQ1VF; 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=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B707B385741A for ; Fri, 9 Sep 2022 12:32:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B707B385741A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662726732; bh=U+6qrLuHKoFkyQlKyI6kXJXzCDdsEEPsVggblGXWlnA=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=AhocQ1VFnY5GXsLO6X45RQz7wBqHt/nBgDBRz6v1R9V6M+F8nzWOEWtcZldOqFMEZ 5jc5+CJkVnjUNEDqVkUuAy2E1ILAjJwYWh5ueoIqf9nn++0t1GNBuU7pYylIKuODA2 +ECMSYM2Wii/VxOfiK71LBdLz//R0ZO40EDwfEBM= 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 [IPv6:2001:67c:2178:6::1c]) by sourceware.org (Postfix) with ESMTPS id C856938582BF for ; Fri, 9 Sep 2022 12:31:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C856938582BF Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id A18DB22DB5 for ; Fri, 9 Sep 2022 12:31:26 +0000 (UTC) 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 998222C141 for ; Fri, 9 Sep 2022 12:31:26 +0000 (UTC) Date: Fri, 9 Sep 2022 12:31:26 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/106722 - avoid invalid pointer association in predcom User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" Message-Id: <20220909123212.B707B385741A@sourceware.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1743495347239365792?= X-GMAIL-MSGID: =?utf-8?q?1743495347239365792?= When predictive commoning builds a reference for iteration N it prematurely associates a constant offset into the MEM_REF offset operand which can be invalid if the base pointer then points outside of an object which alias-analysis does not consider valid. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed to trunk sofar. PR tree-optimization/106722 * tree-predcom.cc (ref_at_iteration): Do not associate the constant part of the offset into the MEM_REF offset operand, across a non-zero offset. * gcc.dg/torture/pr106892.c: New testcase. --- gcc/testsuite/gcc.dg/torture/pr106892.c | 30 +++++++++++++++++++++++++ gcc/tree-predcom.cc | 18 +++++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr106892.c diff --git a/gcc/testsuite/gcc.dg/torture/pr106892.c b/gcc/testsuite/gcc.dg/torture/pr106892.c new file mode 100644 index 00000000000..73a66a037b7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr106892.c @@ -0,0 +1,30 @@ +/* { dg-do run } */ + +int a, b, c, d, e; +int f[8]; +static int g() { + while (a) + a >>= 4; + return 0; +} +static int h(int i) { + if (i >= '0') + return i - '0'; + //__builtin_unreachable (); +} +void __attribute__((noipa)) j(int i) { + for (b = 2; g() <= 7; b++) + if (i) { + for (; e <= 7; e++) + for (c = 1; c <= 7; c++) { + d = h(b + '0'); + f[-d + 4] ^= 3; + } + return; + } +} +int main() { + j(1); + if (f[2] != 0) + __builtin_abort (); +} diff --git a/gcc/tree-predcom.cc b/gcc/tree-predcom.cc index 5d923fba170..a6e45e36ffd 100644 --- a/gcc/tree-predcom.cc +++ b/gcc/tree-predcom.cc @@ -1771,10 +1771,24 @@ ref_at_iteration (data_reference_p dr, int iter, ref = TREE_OPERAND (ref, 0); } } - tree addr = fold_build_pointer_plus (DR_BASE_ADDRESS (dr), off); + /* We may not associate the constant offset across the pointer plus + expression because that might form a pointer to before the object + then. But for some cases we can retain that to allow tree_could_trap_p + to return false - see gcc.dg/tree-ssa/predcom-1.c */ + tree addr, alias_ptr; + if (integer_zerop (off)) + { + alias_ptr = fold_convert (reference_alias_ptr_type (ref), coff); + addr = DR_BASE_ADDRESS (dr); + } + else + { + alias_ptr = build_zero_cst (reference_alias_ptr_type (ref)); + off = size_binop (PLUS_EXPR, off, coff); + addr = fold_build_pointer_plus (DR_BASE_ADDRESS (dr), off); + } addr = force_gimple_operand_1 (unshare_expr (addr), stmts, is_gimple_mem_ref_addr, NULL_TREE); - tree alias_ptr = fold_convert (reference_alias_ptr_type (ref), coff); tree type = build_aligned_type (TREE_TYPE (ref), get_object_alignment (ref)); ref = build2 (MEM_REF, type, addr, alias_ptr);