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: 1731972 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gotplt.org header.i=@gotplt.org header.a=rsa-sha256 header.s=dreamhost header.b=XmTHtb2I; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4P2R9G0Y0Qz23h0 for ; Thu, 26 Jan 2023 14:32:32 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0670C3858281 for ; Thu, 26 Jan 2023 03:32:29 +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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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;