From patchwork Tue Apr 25 13:08:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 87390 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp3392022vqo; Tue, 25 Apr 2023 06:09:36 -0700 (PDT) X-Google-Smtp-Source: AKy350bRVwGPcv8qenzykxuHd8FZwl3XIlCBMfl3j1db2O/6/FLvPd2eT72YZJ+1MPtQl3LUO4ij X-Received: by 2002:a17:906:3144:b0:953:463d:80f4 with SMTP id e4-20020a170906314400b00953463d80f4mr11208293eje.3.1682428176444; Tue, 25 Apr 2023 06:09:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682428176; cv=none; d=google.com; s=arc-20160816; b=TzneuK7B3GLjaGI3lLJ9mYUYU5athgaUndyfZy8Emu0n7vpITmWmTijnxAIX97dsrg EVsy5UA6UqBTkXFEihrjnX8/8ioW7taezby4LUgYMzlbcu1K04ju6bg3Xt3Q8qOjUQI3 FqVrbSvYrRAFb5xNGKgr2/109vsZ+DwgbiNe9KTSczmptHTDDmbGD7RolANwT3TZ5iRF Y/bRV+yvjfoFu2zk+zJRZ8bsbqkGkpzU8MvXR4H0lVphS/Tb22+8t6rRibbPxcssTYkN d/fuYJv/hiQU2GiEWwxtswsiWLfwTBvCfmVcBRPTVXbixMctaXoBZaE24503xhY/Xlvn 9udg== 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=P2+JEk3DfbVtRutnOkSzUKLGfLZPgasQaJjaAvnXgAI=; b=Q/XX/xI+h8PXY5BodDrHfm3yPaCeJJGXAaroeAiqPl6YRZ9jXWybb2re1JX0Op0LHx CFuO6OctupJGNq/7iG2Ost8aVpnnoH37HqmBANu6D8ToIqZgd2ctS0lBFbuZuERvOCKA lEO8zS/+6eoIZBhxTeTf6jAIsN3Kpb3ZKDTVWwyRKDnnUokXh6xR6QMiJdmfPdurIjv0 RSrPi/hUBW+OWKMKCkXK1F0yw5gzZp9EulSDrRwt1RBjHan8MZ7bwBAdwWLWlXUJFrik Yu8HxdqT/hnQq9OhiPlFeTt+poqvFFS9dkBiOTnWvK2hqQGK2feFII4ntBxJKyEzSGnD F6vg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=YsTzWsrW; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id ia17-20020a170907a07100b009538667be5esi9321348ejc.424.2023.04.25.06.09.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Apr 2023 06:09:36 -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=YsTzWsrW; 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 03D0B3858C5E for ; Tue, 25 Apr 2023 13:09:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 03D0B3858C5E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682428165; bh=P2+JEk3DfbVtRutnOkSzUKLGfLZPgasQaJjaAvnXgAI=; h=Date:To:cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=YsTzWsrWo9QNK/Aghm2G0FO4mjz2Sq4F5hzhgGVUYLKRaiE/VqlZVXTIrEa73IbeV KML5XugeF9OKRAMagQOqyaH9XQcM8l//77tgLyOJMLPE+tmECTKcAeyphFdQnqQV81 JoTy+poqHq/0qbwmgNKINBZfXz48x+Ni5W8xBvEI= 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.220.28]) by sourceware.org (Postfix) with ESMTPS id 54ED03858C2D for ; Tue, 25 Apr 2023 13:08:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 54ED03858C2D 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-out1.suse.de (Postfix) with ESMTPS id 7EA5B2193C; Tue, 25 Apr 2023 13:08:36 +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 5E2B513466; Tue, 25 Apr 2023 13:08:36 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id cIQ9FdTQR2RWCwAAMHmgww (envelope-from ); Tue, 25 Apr 2023 13:08:36 +0000 Date: Tue, 25 Apr 2023 15:08:35 +0200 (CEST) To: gcc-patches@gcc.gnu.org cc: Jan Hubicka Subject: [PATCH] tree-optimization/109609 - correctly interpret arg size in fnspec MIME-Version: 1.0 Message-Id: <20230425130836.5E2B513466@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, 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?1764153807688939713?= X-GMAIL-MSGID: =?utf-8?q?1764153807688939713?= By majority vote and a hint from the API name which is arg_max_access_size_given_by_arg_p this interprets a memory access size specified as given as other argument such as for strncpy in the testcase which has "1cO313" as specifying the _maximum_ size read/written rather than the exact size. There are two uses interpreting it that way already and one differing. The following adjusts the differing and clarifies the documentation. Bootstrap & regtest running on x86_64-unknown-linux-gnu. It looks like arg_access_size_given_by_type_p is interpreted in a similar way but the API doesn't reflect this (no _max). On IRC we discussed it would be nice to have both, exact and non-exact size (for example use 'a'..'j' for the alternate variant). I still wonder if my interpretation is correct in that '1'..'9' specify a _bound_ for the size. Honza, do you remember why you wrote the IPA modref users this way? Was that intentional? Thus is my reverse engineering of the desired semantics correct? Thanks, Richard. PR tree-optimization/109609 * attr-fnspec.h (arg_max_access_size_given_by_arg_p): Clarify semantics. * tree-ssa-alias.cc (check_fnspec): Correctly interpret the size given by arg_max_access_size_given_by_arg_p as maximum, not exact, size. * gcc.dg/torture/pr109609.c: New testcase. --- gcc/attr-fnspec.h | 4 ++-- gcc/testsuite/gcc.dg/torture/pr109609.c | 26 +++++++++++++++++++++++++ gcc/tree-ssa-alias.cc | 18 ++++++++++++++--- 3 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr109609.c diff --git a/gcc/attr-fnspec.h b/gcc/attr-fnspec.h index acf1c5f6be7..99d5f896e8b 100644 --- a/gcc/attr-fnspec.h +++ b/gcc/attr-fnspec.h @@ -54,7 +54,7 @@ ' ' nothing is known 't' the size of value written/read corresponds to the size of of the pointed-to type of the argument type - '1'...'9' specifies the size of value written/read is given by the + '1'...'9' specifies the size of value written/read is bound by the specified argument */ @@ -169,7 +169,7 @@ public: && str[idx] != 'x' && str[idx] != 'X'; } - /* Return true if load of memory pointed to by argument I is specified + /* Return true if load of memory pointed to by argument I is bound by another argument. In this case set ARG. */ bool arg_max_access_size_given_by_arg_p (unsigned int i, unsigned int *arg) diff --git a/gcc/testsuite/gcc.dg/torture/pr109609.c b/gcc/testsuite/gcc.dg/torture/pr109609.c new file mode 100644 index 00000000000..0e191cd1ee8 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr109609.c @@ -0,0 +1,26 @@ +/* { dg-do run } */ + +#define N 23 +#define MAX_LEN 13 +char dst[N + 1]; + +void __attribute__((noipa)) +invert(const char *id) +{ + char buf[MAX_LEN]; + char *ptr = buf + sizeof(buf); // start from the end of buf + *(--ptr) = '\0'; // terminate string + while (*id && ptr > buf) { + *(--ptr) = *(id++); // copy id backwards + } + __builtin_strncpy(dst, ptr, N); // copy ptr/buf to dst +} + + +int main() +{ + invert("abcde"); + if (__builtin_strcmp(dst, "edcba")) + __builtin_abort(); + return 0; +} diff --git a/gcc/tree-ssa-alias.cc b/gcc/tree-ssa-alias.cc index 8a1ec9091fa..e0693e146bf 100644 --- a/gcc/tree-ssa-alias.cc +++ b/gcc/tree-ssa-alias.cc @@ -2726,9 +2726,21 @@ check_fnspec (gcall *call, ao_ref *ref, bool clobber) t = TREE_CHAIN (t); size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_VALUE (t))); } - ao_ref_init_from_ptr_and_size (&dref, - gimple_call_arg (call, i), - size); + poly_int64 size_hwi; + if (size + && poly_int_tree_p (size, &size_hwi) + && coeffs_in_range_p (size_hwi, 0, + HOST_WIDE_INT_MAX / BITS_PER_UNIT)) + { + size_hwi = size_hwi * BITS_PER_UNIT; + ao_ref_init_from_ptr_and_range (&dref, + gimple_call_arg (call, i), + true, 0, -1, size_hwi); + } + else + ao_ref_init_from_ptr_and_range (&dref, + gimple_call_arg (call, i), + false, 0, -1, -1); if (refs_may_alias_p_1 (&dref, ref, false)) return 1; }