From patchwork Tue Apr 18 10:33:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 84742 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2739847vqo; Tue, 18 Apr 2023 03:34:20 -0700 (PDT) X-Google-Smtp-Source: AKy350YhFsJC0Zg/UUKdqSSkoE0bGXaCIrJbtmXCGzxpwgrpCes7QiM6+yogpx0TBd1Hc7NxVgce X-Received: by 2002:a17:906:7f9a:b0:94a:76f6:8e52 with SMTP id f26-20020a1709067f9a00b0094a76f68e52mr13147780ejr.35.1681814060718; Tue, 18 Apr 2023 03:34:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681814060; cv=none; d=google.com; s=arc-20160816; b=NS9HpTFgjYyUIgXsB5Liu5ZzQpa5gujnZqiJAetckHxKDbdd4deHeGUTop4CZOlRAJ /62bbLJcisN8NvBb6UUGIeolKTaIBVMK/xir1uB/8+N2NHX5187DgKXq46t6o4FDdojN U5qjoC681ceNU7TDdPTWcm9uHDn2HA3EW7V0m7FPbr32Fda/oyLe6ujMfJBXLxm/I7Zw enggm4R3jwhoN1Xwa6vsF1bunFwoIuP6UTfxgqsBdisPqHu9096jc68IJiglyRC3mz4O 6kyiZ6HvufY95qY7c2/UieAoV56Vxq2oD1tlZxbLAH13oogCq14auIr88LCGlZr2/iSE eduQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:message-id :mime-version:subject:cc:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=1A/h4lufhE8ObIdv/LEhaX9o07eV8yaHP4vPfrIkJX0=; b=n6kXbFNqKOrJEc2JXpXDctvoojjIYBnoVFW0nSpE4Ifabm2R+KcqZUq4M0RtodMVEF teNIiH8j1vg2/6NzeXri3IlqQMiG0Lp6ChKbVbotzSypG1RLrBJid9jlZUagZk+Oa6cg QrsonHySsfZf+T+nTOUM0HS1zajnoCMnfuadnZPBgmGkGpjTw5QPj2sBWvZzI9GpYkVx lPHpMT68QtwJOB/ciDWt/XUMd3kPF7+HgwIcus55Fc2rJSsMeGz6VRQy7gPMYa07GfVe J+pYwQx4+iTb8YZpTwtMcNH279wsyqH8Da5CRfopeHhz/FuGculICNtHdyXdhlqRnL6q YFmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=lDGLFCOM; 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=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id j2-20020a1709062a0200b0094e1026c195si12141377eje.812.2023.04.18.03.34.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Apr 2023 03:34:20 -0700 (PDT) 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=@gcc.gnu.org header.s=default header.b=lDGLFCOM; 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=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5EE943858C53 for ; Tue, 18 Apr 2023 10:34:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5EE943858C53 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1681814059; bh=1A/h4lufhE8ObIdv/LEhaX9o07eV8yaHP4vPfrIkJX0=; h=Date:To:cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=lDGLFCOM9FhYxQpUhScgjKbSVCH7IjZzaCixFl9J7TibUzdtutOdaDC24EW12SrlU 5UBLLXF/x1qOSLoc7FmyvBgw9KK1HFPXwh0hOPYasgiQ76oYwPdScxktomGLGECVoG XzetHL2ak6jGroH0awULCH1E1+BxrqlEoGxFnH0I= 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 417A53858D1E for ; Tue, 18 Apr 2023 10:33:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 417A53858D1E 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-out2.suse.de (Postfix) with ESMTPS id 6B67F1FE05; Tue, 18 Apr 2023 10:33:26 +0000 (UTC) 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 54716139CC; Tue, 18 Apr 2023 10:33:26 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id ifB0E/ZxPmSTTgAAMHmgww (envelope-from ); Tue, 18 Apr 2023 10:33:26 +0000 Date: Tue, 18 Apr 2023 12:33:25 +0200 (CEST) To: gcc-patches@gcc.gnu.org cc: Jakub Jelinek Subject: [PATCH] tree-optimization/109539 - restrict PHI handling in access diagnostics MIME-Version: 1.0 Message-Id: <20230418103326.54716139CC@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.7 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.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" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763509860639360106?= X-GMAIL-MSGID: =?utf-8?q?1763509860639360106?= Access diagnostics visits the SSA def-use chains to diagnose things like dangling pointer uses. When that runs into PHIs it tries to prove all incoming pointers of which one is the currently visited use are related to decide whether to keep looking for the PHI def uses. That turns out to be overly optimistic and thus costly. The following scraps the existing handling for simply requiring that we eventually visit all incoming pointers of the PHI during the def-use chain analysis and only then process uses of the PHI def. Note this handles backedges of natural loops optimistically, diagnosing the first iteration. There's gcc.dg/Wuse-after-free-2.c containing a testcase requiring this. I've bootstrapped and tested a version without the backedge handling (regressing that single test), currently bootstrapping and testing the version below. OK for trunk and branch if that succeeds? Thanks, Richard. PR tree-optimization/109539 * gimple-ssa-warn-access.cc (pass_waccess::check_pointer_uses): Re-implement pointer relatedness for PHIs. --- gcc/gimple-ssa-warn-access.cc | 47 ++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc index d0d2148c872..1034161d478 100644 --- a/gcc/gimple-ssa-warn-access.cc +++ b/gcc/gimple-ssa-warn-access.cc @@ -4175,6 +4175,7 @@ pass_waccess::check_pointer_uses (gimple *stmt, tree ptr, auto_vec pointers; pointers.safe_push (ptr); + hash_map phi_map; /* Starting with PTR, iterate over POINTERS added by the loop, and either warn for their uses in basic blocks dominated by the STMT @@ -4241,19 +4242,47 @@ pass_waccess::check_pointer_uses (gimple *stmt, tree ptr, tree_code code = gimple_cond_code (cond); equality = code == EQ_EXPR || code == NE_EXPR; } - else if (gimple_code (use_stmt) == GIMPLE_PHI) + else if (gphi *phi = dyn_cast (use_stmt)) { /* Only add a PHI result to POINTERS if all its - operands are related to PTR, otherwise continue. */ - tree lhs = gimple_phi_result (use_stmt); - if (!pointers_related_p (stmt, lhs, ptr, m_ptr_qry)) - continue; - - if (TREE_CODE (lhs) == SSA_NAME) + operands are related to PTR, otherwise continue. The + PHI result is related once we've reached all arguments + through this iteration. That also means any invariant + argument will make the PHI not related. For arguments + flowing over natural loop backedges we are optimistic + (and diagnose the first iteration). */ + tree lhs = gimple_phi_result (phi); + bool existed_p; + int &related = phi_map.get_or_insert (lhs, &existed_p); + if (!existed_p) { - pointers.safe_push (lhs); - continue; + related = gimple_phi_num_args (phi) - 1; + for (unsigned j = 0; j < gimple_phi_num_args (phi); ++j) + { + if ((unsigned) phi_arg_index_from_use (use_p) == j) + continue; + tree arg = gimple_phi_arg_def (phi, j); + edge e = gimple_phi_arg_edge (phi, j); + basic_block arg_bb; + if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest) + /* Make sure we are not forward visiting a + backedge argument. */ + && (TREE_CODE (arg) != SSA_NAME + || (!SSA_NAME_IS_DEFAULT_DEF (arg) + && ((arg_bb + = gimple_bb (SSA_NAME_DEF_STMT (arg))) + != e->dest) + && !dominated_by_p (CDI_DOMINATORS, + e->dest, arg_bb)))) + related--; + } } + else + related--; + + if (related == 0) + pointers.safe_push (lhs); + continue; } /* Warn if USE_STMT is dominated by the deallocation STMT.