From patchwork Thu May 4 13:59:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 90090 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp337592vqo; Thu, 4 May 2023 07:00:17 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4PnCsGcSjp+E0eh2FXNlQp0DMMOxwKarQk3pUZD6W5iFDraP016oBtpl/dz09BR65RHJRf X-Received: by 2002:a17:906:da82:b0:94e:eb42:2a7c with SMTP id xh2-20020a170906da8200b0094eeb422a7cmr6512813ejb.25.1683208817439; Thu, 04 May 2023 07:00:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683208817; cv=none; d=google.com; s=arc-20160816; b=GnjXc+knMHlkGwDY3tnzRm0IKWNoVFTSnmeXm46sWtkPhTeWgJO7ckzJpgPnBq/WBc 2EkSe3a7KTuKhdhGYl/i3EQSBrNsDAyDQuj09PuOWQynKKuCOaP5MDb8n/0VL1HuIE01 jRe5cRs2o2xK+2fzTBxhIntoXOsOWMmwjDBcvkHceDCbudGW6ScWeVbX2Kfq8oA65MXd Eav3DRqFpagpt4lzxFU0ArDFb/KNgF3eaaCwqMgvELoU4qkK8mcqU6qIpDmC9uitFXnV XZ3Lb0s8uswVhP/lhhARcIN0WEi5N8gkkNMNcaOhENUjsXNPc9HLDfgNFkbi8fNgNPgA iwjg== 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=G51N5W/ZcWSpBUUQIhoa6rD1gGID/Mh7dFzgWiihGJg=; b=aIsDXQS7rOhnFhHg7GhfEfTgvrHRc6ZyuyIg91dt5jWcQyG7miWws6c806ccNKfOGY X6Is2dzr4GueRGYLVxjm7gqmK8DXHfB/eB0fH7KSVRwH0fIf2j6MM6foVrdWlMNPD5gw jtwb89IKgYgGES0QmQqWyihfouf7cAZFza1QWlrsbi+WRAx/W9RgUm20JTiaFj3l75Pv a+lyEpEOighd6oTNkkqyZUynTk436AtX6vHgqgkt2mDFp+xTmhFywxjOwBVlrF/kc6gS qLa/rRZnb+6ENtAobA5uTANE7JUXieDBIHEa+PDDIFF9GpqX37abfQldU/vsfQMP0TwC Od3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=d3Av4a3I; 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 m5-20020a17090607c500b0094f2e8d96a5si24722171ejc.985.2023.05.04.07.00.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 May 2023 07:00:17 -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=d3Av4a3I; 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 176553858CDB for ; Thu, 4 May 2023 14:00:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 176553858CDB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683208816; bh=G51N5W/ZcWSpBUUQIhoa6rD1gGID/Mh7dFzgWiihGJg=; h=Date:To:cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=d3Av4a3IUNzqjIxVT08EHg3aTGvGqahI2U6dTbRxZu2XFGuutzY8APmsdBORsmBcM EruL/7BMtpEvbX91Gx0v6b8Ru9jvPUWgeixe21KU7Gt/hGn1FfFKN7WUVruEUeko9y ee4zo/MB/TLVkDubGgKgKPltWGIF5I/vr522vYpo= 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 [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 0882D3858004 for ; Thu, 4 May 2023 13:59:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0882D3858004 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 2FAF11FD79; Thu, 4 May 2023 13:59:32 +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 1618B13444; Thu, 4 May 2023 13:59:32 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id YdlhBES6U2T1cAAAMHmgww (envelope-from ); Thu, 04 May 2023 13:59:32 +0000 Date: Thu, 4 May 2023 15:59:31 +0200 (CEST) To: gcc-patches@gcc.gnu.org cc: jason@redhat.com, Jakub Jelinek Subject: [PATCH][RFC] tree-optimization/104475 - bogus -Wstringop-overflow MIME-Version: 1.0 Message-Id: <20230504135932.1618B13444@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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?1764972369153794013?= X-GMAIL-MSGID: =?utf-8?q?1764972369153794013?= I've previously sent https://gcc.gnu.org/pipermail/gcc-patches/2022-December/608077.html adding ADDR_EXPR_NONZERO and there were comments from Jason where I just realized I ignored ARRAY_REF for the following. Anyway, here's a more aggressive variant not going for an extra flag set by the frontend but instead have the middle-end treat all &*.component as non-NULL (all handled_component_p). This passes bootstrap for all languages, testing there isn't complete but it already shows for example gcc.c-torture/execute/pr44555.c explicitely testing that we keep &p->z NULL when p is NULL and z is at offset zero. There's also execute FAILs for gfortran.dg/class_optional_2.f90 and some optimization dump scan fails I did not yet investigate. Nevertheless I'd like to hear opinions on whether a middle-end implementation without frontend help is the way to go and what the reasonable restrictions should be there? Is gcc.c-torture/execute/pr44555.c sanctioned by the C standard? If so I think we have a lost cause without some help from the frontend? Thanks, Richard. --- The following avoids a bogus -Wstringop-overflow diagnostic by properly recognizing that &d->m_mutex cannot be nullptr even if m_mutex is at offset zero. The C++ frontend already diagnoses a &d->m_mutex != nullptr comparison and the following transfers this knowledge to the middle-end in the most general way. To avoid the bogus diagnostic this avoids separating the nullptr path via jump-threading by eliminating the nullptr check. PR tree-optimization/104475 * fold-const.cc (tree_single_nonzero_warnv_p): An ADDR_EXPR of a component reference can never be null. * g++.dg/opt/pr104475.C: New testcase. --- gcc/fold-const.cc | 11 ++++++++++- gcc/testsuite/g++.dg/opt/pr104475.C | 12 ++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/opt/pr104475.C diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index db54bfc5662..c5c923e059d 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -15368,7 +15368,16 @@ tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p) tree base = TREE_OPERAND (t, 0); if (!DECL_P (base)) - base = get_base_address (base); + { + gcc_checking_assert (TREE_CODE (base) != WITH_SIZE_EXPR); + /* Any component reference, even if at offset zero, requires + a non-null base. */ + if (handled_component_p (base) + && !targetm.addr_space.zero_address_valid + (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (t))))) + return true; + base = get_base_address (base); + } if (base && TREE_CODE (base) == TARGET_EXPR) base = TARGET_EXPR_SLOT (base); diff --git a/gcc/testsuite/g++.dg/opt/pr104475.C b/gcc/testsuite/g++.dg/opt/pr104475.C new file mode 100644 index 00000000000..013c70302c6 --- /dev/null +++ b/gcc/testsuite/g++.dg/opt/pr104475.C @@ -0,0 +1,12 @@ +// { dg-do compile } +// { dg-require-effective-target c++11 } +// { dg-options "-O -Waddress -fdump-tree-original" } + +struct X { int i; }; + +bool foo (struct X *p) +{ + return &p->i != nullptr; /* { dg-warning "never be NULL" } */ +} + +/* { dg-final { scan-tree-dump "return = 1;" "original" } } */