From patchwork Thu Jan 26 03:32:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 48480 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp74279wrn; Wed, 25 Jan 2023 19:32:46 -0800 (PST) X-Google-Smtp-Source: AMrXdXubwzQ4KVQT1CwKHKf39DFf0yrX2kt6BXoUVWt5XHr+Ng+YdQLhB8TgoYBPRnnVPSJua/pU X-Received: by 2002:a17:906:3e51:b0:7c1:932c:96d7 with SMTP id t17-20020a1709063e5100b007c1932c96d7mr32045159eji.58.1674703966729; Wed, 25 Jan 2023 19:32:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1674703966; cv=pass; d=google.com; s=arc-20160816; b=0nt1WN0ruVtJUNbv4ZxS0OgBoanDabzz82ppHl2ZFOIy0tyfE3+oN74CqoAmLfrC9W WvR5V1CpNDtTLvXs9iFNITj5k+iEzBi6qmWVZFNDXxmWcr54S0QtoRDain0pQlAXzU1J Hbzntc1yOXnS5VGTNWei7WZoRWRxGePoP+RBwKtFYM6nTGSPPnyf9seHvUecpf6K50L7 /P5PFmNgMn3otLYE+/sq+oW7QZ/ik3di3/r7IfzGhVG5xCxDjpmxnZy2XmJW53m9b3Gw 2HT6Le66+NzpjKxQoA5YNwdYkE7w47FidGDprWP1ioNRuWeGfDDBJ/qAbMlzQzgskrJ8 sUAw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dmarc-filter:delivered-to; bh=OGAujSJbrUnVf8zN3Tdft5Gc7uQ6jwivHSonBYclD8I=; b=qpkQuLK1jTXzVERkMCaCKcxuEQnPOSXqHs2SdcUC+wPQBflpjiWY0LScilI1oPysl+ yu3Ro4nIxuOJVIV3dPTjDAY0z7gUvSYZv6ePGAnS4h7UU9eh8KxSAhrfSj3J4zTv+Cyz XJf7xDgBzHzrT+q/OfgRVhiRdyJumgto/+tDrGWqd5MEVr6lGedGKC+Uhzbuq67+hSL7 FI3ljLXCo09YQ7sM/nMjDWeEYlEOlF7SI8FGR/+Z3NToerF3dGpe/10gfcpeXwmfmWKw GLchwTUNUoA/1jYF5dHzcY7DP60W8WkV0HALbv+2gtQnVJXsKC0JCYMp/5WZK9XpV9wD UA8A== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gotplt.org header.s=dreamhost header.b=XmTHtb2I; arc=pass (i=1); 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" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id mv10-20020a170907838a00b00872baebe5dbsi7506807ejc.968.2023.01.25.19.32.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Jan 2023 19:32:46 -0800 (PST) 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=@gotplt.org header.s=dreamhost header.b=XmTHtb2I; arc=pass (i=1); 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" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A368E3858004 for ; Thu, 26 Jan 2023 03:32:41 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from butterfly.birch.relay.mailchannels.net (butterfly.birch.relay.mailchannels.net [23.83.209.27]) by sourceware.org (Postfix) with ESMTPS id D39A63858D38 for ; Thu, 26 Jan 2023 03:32:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D39A63858D38 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gotplt.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 9D98B5C098A; Thu, 26 Jan 2023 03:32:15 +0000 (UTC) Received: from pdx1-sub0-mail-a307.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 260DF5C0506; Thu, 26 Jan 2023 03:32:15 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1674703935; a=rsa-sha256; cv=none; b=L7cvHHLTbYDrPQZ4sVXc9jZw+GOzymL1RWhI6SmkQFLreyz/y5oGmbrV6iVGA7bUxoaoQ0 4XbPpj1/4QILoo3Pp66KTWUkEqRAJ2lGkVgThMLi6NFnXuzyfWmHU7zFH02bVcikmOFw/0 gppQEnv2pIuMUeRVqyiasa6IuhYW9zK54E3jsPTswojVFvs7t27YS8x1TEYgXZ86JoNqaX SwhvHCgvM2jU24ywdTtB0hx72QS1Rrgo2ibMMr1ivh/scy2shewyH73usufinWN19SOvt5 E0s0x4LL/GuUz86SAKd7tBBn5FX2LsFrRFztp2jjLWCMQfjOpw35cJww5dEuKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1674703935; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:dkim-signature; bh=OGAujSJbrUnVf8zN3Tdft5Gc7uQ6jwivHSonBYclD8I=; b=tXzbTHgreOdAZb5HwigpZ7sn63aYZfHo+cFn+nKyvdS6gDrmRN681lJsiqocD0IIQHRnLl i1YYCwf8LpqHPMBcuNJpv2jlkp4naKA8OXdBZIojI3ZvhqgZPwx9BhcFbilAGvBREEWCa5 2lVU/PyZIUnKMA0Dc5W06OIhiE+12kMT6QVKEKuFzburOC2Dij7YxyUpO2tLKAAHEjAUWQ 6XCda8KtqIluzAtNb7D2tTrUOyhxmk51tReIL0XZJ5iLuaWFDd5UlrSXlNKSVPxquUvUdo EATgQgpSz9PJgaJ7x6PUSMkQx9EvheWpYCsPfb092EJ6r3FM71/pYgGNbw7ERw== ARC-Authentication-Results: i=1; rspamd-6989874cc5-sxnxh; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Bored-Interest: 64d395c114eb56ed_1674703935412_1065339919 X-MC-Loop-Signature: 1674703935412:3875535063 X-MC-Ingress-Time: 1674703935412 Received: from pdx1-sub0-mail-a307.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.123.200.113 (trex/6.7.1); Thu, 26 Jan 2023 03:32:15 +0000 Received: from fedora.redhat.com (bras-base-toroon4834w-grc-23-76-68-24-147.dsl.bell.ca [76.68.24.147]) (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) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a307.dreamhost.com (Postfix) with ESMTPSA id 4P2R8t1LKQz1H; Wed, 25 Jan 2023 19:32:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gotplt.org; s=dreamhost; t=1674703934; bh=OGAujSJbrUnVf8zN3Tdft5Gc7uQ6jwivHSonBYclD8I=; h=From:To:Cc:Subject:Date:Content-Transfer-Encoding; b=XmTHtb2IIp1PNgw5T71Bmr4AIUtkNAcQD9eMn9sCNG1yikWgDdPDSbpzIX2P8DV7r HyDIPBl09xT5g74FnopLjrw9RzWMgm83LRvmlo/wTf64T4dRFVC/4SCZvfUmAJhUFa Fv74aRyEW82cwzKNoKcsnX2XAkcjMolpWbeGxtrgkMTT8nB+F7UEQh7emItoZyxqZ7 YkRGdbYzAkfPlGP9hHI49YkoobUclTzIUSdKXYtvkzYGDkI5q5Rx4g9cegN4PpaabU /93y91RNIz0tTupwUfCyCtXDNUvjGYX2T2eUmS9eI4NtJgAfL/LLdMDJbWP8wt199l DJXzLNruRGc8A== From: Siddhesh Poyarekar To: gcc-patches@gcc.gnu.org Cc: jakub@redhat.com Subject: [PATCH] tree-optimization/108522 Use component_ref_field_offset Date: Wed, 25 Jan 2023 22:32:10 -0500 Message-Id: <20230126033210.1926726-1-siddhesh@gotplt.org> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3037.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP 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: , 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?1756054386434350747?= X-GMAIL-MSGID: =?utf-8?q?1756054386434350747?= Instead of using TREE_OPERAND (expr, 2) directly, use component_ref_field_offset instead, which does scaling for us. The function also substitutes PLACEHOLDER_EXPRs, which is probably what we want anyway but I'm not sure if it's relevant for tree-object-size. gcc/ChangeLog: PR tree-optimization/108522 * tree-object-size.cc (compute_object_offset): Make EXPR argument non-const. Call component_ref_field_offset. gcc/testsuite/ChangeLog: PR tree-optimization/108522 * gcc.dg/builtin-dynamic-object-size-0.c (DEFSTRUCT): New macro. (test_dynarray_struct_member_b, test_dynarray_struct_member_c, test_dynarray_struct_member_d, test_dynarray_struct_member_subobj_b, test_dynarray_struct_member_subobj_c, test_dynarray_struct_member_subobj_d): New tests. (main): Call them. Signed-off-by: Siddhesh Poyarekar --- Testing: - Tested i686 to confirm that there are no new regressions - Tested x86_64 bootstrap and confirmed that there are no new regressions - ubsan config bootstrap in progress .../gcc.dg/builtin-dynamic-object-size-0.c | 81 +++++++++++++++++-- gcc/tree-object-size.cc | 7 +- 2 files changed, 77 insertions(+), 11 deletions(-) diff --git a/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c index 569c0a87722..76079d8702e 100644 --- a/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c +++ b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c @@ -315,19 +315,70 @@ test_dynarray_struct_subobj2 (size_t sz, size_t off, size_t *objsz) } /* See pr #108522. */ + +#define DEFSTRUCT(_s, _n) \ + struct DS \ + { \ + char a[_n]; \ + unsigned long long b; \ + int c; \ + char d[2 * _n]; \ + } _s + size_t __attribute__ ((noinline)) -test_dynarray_struct_member (size_t sz) +test_dynarray_struct_member_b (size_t sz) { - struct - { - char a[sz]; - char b; - } s; + DEFSTRUCT (s, sz); return __builtin_dynamic_object_size (&s.b, 0); } +size_t +__attribute__ ((noinline)) +test_dynarray_struct_member_c (size_t sz) +{ + DEFSTRUCT (s, sz); + + return __builtin_dynamic_object_size (&s.c, 0); +} + +size_t +__attribute__ ((noinline)) +test_dynarray_struct_member_d (size_t sz, size_t offset) +{ + DEFSTRUCT (s, sz); + + return __builtin_dynamic_object_size (&s.d[offset], 0); +} + +size_t +__attribute__ ((noinline)) +test_dynarray_struct_member_subobj_b (size_t sz) +{ + DEFSTRUCT (s, sz); + + return __builtin_dynamic_object_size (&s.b, 1); +} + +size_t +__attribute__ ((noinline)) +test_dynarray_struct_member_subobj_c (size_t sz) +{ + DEFSTRUCT (s, sz); + + return __builtin_dynamic_object_size (&s.c, 1); +} + +size_t +__attribute__ ((noinline)) +test_dynarray_struct_member_subobj_d (size_t sz, size_t offset) +{ + DEFSTRUCT (s, sz); + + return __builtin_dynamic_object_size (&s.d[offset], 1); +} + size_t __attribute__ ((noinline)) test_substring (size_t sz, size_t off) @@ -633,7 +684,23 @@ main (int argc, char **argv) if (test_dynarray_struct_subobj2 (42, 4, &objsz) != objsz - 4 - sizeof (long) - sizeof (int)) FAIL (); - if (test_dynarray_struct_member (42) != sizeof (char)) + DEFSTRUCT(ds, 64); + const size_t n = sizeof (ds.a); + if (test_dynarray_struct_member_b (n) + != sizeof (ds) - __builtin_offsetof (struct DS, b)) + FAIL (); + if (test_dynarray_struct_member_c (n) + != sizeof (ds) - __builtin_offsetof (struct DS, c)) + FAIL (); + if (test_dynarray_struct_member_d (n, 0) + != sizeof (ds) - __builtin_offsetof (struct DS, d)) + FAIL (); + if (test_dynarray_struct_member_subobj_b (n) != sizeof (ds.b)) + FAIL (); + if (test_dynarray_struct_member_subobj_c (n) != sizeof (ds.c)) + FAIL (); + if (test_dynarray_struct_member_subobj_d (n, n - 2) + != sizeof (ds) - __builtin_offsetof (struct DS, d) - n + 2) FAIL (); if (test_substring_ptrplus (128, 4) != (128 - 4) * sizeof (int)) FAIL (); diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index de93ffad9c9..9a936a91983 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -56,7 +56,7 @@ struct GTY(()) object_size tree wholesize; }; -static tree compute_object_offset (const_tree, const_tree); +static tree compute_object_offset (tree, const_tree); static bool addr_object_size (struct object_size_info *, const_tree, int, tree *, tree *t = NULL); static tree alloc_object_size (const gcall *, int); @@ -396,7 +396,7 @@ size_for_offset (tree sz, tree offset, tree wholesize = NULL_TREE) if unknown. */ static tree -compute_object_offset (const_tree expr, const_tree var) +compute_object_offset (tree expr, const_tree var) { enum tree_code code = PLUS_EXPR; tree base, off, t; @@ -413,8 +413,7 @@ compute_object_offset (const_tree expr, const_tree var) t = TREE_OPERAND (expr, 1); off = size_binop (PLUS_EXPR, - (TREE_OPERAND (expr, 2) ? TREE_OPERAND (expr, 2) - : DECL_FIELD_OFFSET (t)), + component_ref_field_offset (expr), size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t)) / BITS_PER_UNIT)); break;