From patchwork Thu May 25 16:14:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1786216 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 (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=wRZ1M/iC; 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 4QRtSw27zkz20Q1 for ; Fri, 26 May 2023 02:15:44 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A756D3856964 for ; Thu, 25 May 2023 16:15:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A756D3856964 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685031340; bh=Lp87e/zggjOskq+IfbmAyPFFSj8n5qTs/FH+rr5l3FY=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=wRZ1M/iCNgPCykuNRM7e3hxD491PTBODjn92lNWfKdI6WR664CzxFUgbA3tGoAzrd /SQbXmTO8a/WmHvyE9AUjUz3wAJQdoClrNAssusZiKkLcNZ5zUIk/os1J2tTF7S19V 6stKykIhv4D0XlgeFtxwg7r0ANd4WEEEJueL8eRs= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id D81803857727 for ; Thu, 25 May 2023 16:15:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D81803857727 Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 34PGEmiV014582; Thu, 25 May 2023 16:15:14 GMT Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3qtb0yr02j-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 May 2023 16:15:13 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 34PFPSQw016200; Thu, 25 May 2023 16:15:13 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2174.outbound.protection.outlook.com [104.47.56.174]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3qqk6nccsk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 May 2023 16:15:13 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=foRRM3uUw5YOBFMbvgIjfQzeMklC9PVLGaFLgcvGxstcN1gH0cyT0hm6tcPnw4Ip4fnBoI4goybiOrCEkwnmjRyJ14n5rRZI34mAPskmH+RBtn3BAphHc9fBxVwjaPYBnsO4H2Ku1t/9O6mjTSkZw700INAc0NEsY+pFg/VD1dMo0a5jg9f0AteLubGaSx78X1rox2TcZAfPoXBjqUvDG5t0jjsgkY4woJmwIR/x/FjtX3OxC2NQetgE0OgceOTmAPbNCl1xYSpWjmSfOPKp3yR5YRwXZYtZv+emXbMBTIBesQ9bBXsL7A+7/E+p1EYeVRvbbP50HHY5wj+LmiL9yQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Lp87e/zggjOskq+IfbmAyPFFSj8n5qTs/FH+rr5l3FY=; b=WoskqxXxa/CMstNBjdDmqJV8kGRPvcYJFkuSEP2WoX50gwgkZ1HSjTJzuDOx5pRKhWDGt2iv/bHaLc4xn+3iSVZ6e1mLmlsqememVWpsPM6mgx0Dkm/9K1MAe4utAqP63e3QjtZ8KnoZj6FvC/+aebTgg439+SP+zi5fZ+FKoC6sQfqe3k9MAB+RSmfFXl4y+aYLD0paX3gIrsYMN2tTn3i84FoJCR6W4KlqjNEswxoNJBlvCiAubBRLGlhN+8GhFxnYhdpXNJFRTLZEE+APMqZnF/Um2ZmylZOMTNEVaFlOSXB1BEbEW3WnzxO4Ly4qgq79/MlatctVzjYJ75noVA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none Received: from CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) by BLAPR10MB5041.namprd10.prod.outlook.com (2603:10b6:208:30e::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6433.15; Thu, 25 May 2023 16:15:09 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7aa1:df38:105a:21fa]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7aa1:df38:105a:21fa%7]) with mapi id 15.20.6411.028; Thu, 25 May 2023 16:15:08 +0000 To: joseph@codesourcery.com, richard.guenther@gmail.com, jakub@redhat.com, gcc-patches@gcc.gnu.org Cc: keescook@chromium.org, siddhesh@gotplt.org, uecker@tugraz.at, isanbard@gmail.com, Qing Zhao Subject: [V1][PATCH 2/3] Use the element_count atribute info in builtin object size [PR108896]. Date: Thu, 25 May 2023 16:14:49 +0000 Message-Id: <20230525161450.3704901-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230525161450.3704901-1-qing.zhao@oracle.com> References: <20230525161450.3704901-1-qing.zhao@oracle.com> X-ClientProxiedBy: SA0PR11CA0088.namprd11.prod.outlook.com (2603:10b6:806:d2::33) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|BLAPR10MB5041:EE_ X-MS-Office365-Filtering-Correlation-Id: 4b82474e-02d1-42aa-7084-08db5d3b355b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: E64P51ZdmiQrIMf0Mk29XprFtoV4ECHid8CX+TmetoKX20czOG9paL+5JMzLOgsZp2xTwgSiyqSIzhJyPPbSVthhSHUpk9TCv6FGdKLmpmgz+LCHGylATm8lluXwi2H2/Jax7LySVJKScQk1o+KaMLG7560kt5D4rnO8NIn5YQBI722DLI7slxaYv3WoYC7Pecot0zve73nDITIDtf4DMVNt3n5g9h69c0oLUOch4zXEVmcPVb9cMzitg+ECXHiIndl8DGwAuX2RJSSJ3IvXoi4NwVmvoTsxq2j1JUDxAj3Iggx4IwiqkyHmnuh7h4PuOmuBm9edf1yIMb6vipFY5SXGxeRA9/wBm/snzxEboqlHjO6MFkPE8tpjYN5u/Lu1q78B+xjSHf/C0GvDb8NPEerlX5EZcst50/zZUxiPSq87DGkCZHaeVDPUERvONqlxnncm3LqXV7fE5VCb+B3OOJT2Bx+E1dijW0MP6pGg5CH6pFL2qTB2e+uFvfDdilIR2BgXSmj/6ZOm6Ntr1Rpx6J1Wrz/1tfocUYkyICelrMQ= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH2PR10MB4344.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(346002)(39860400002)(366004)(396003)(376002)(136003)(451199021)(2906002)(86362001)(83380400001)(186003)(2616005)(38100700002)(8936002)(36756003)(5660300002)(8676002)(316002)(44832011)(41300700001)(6506007)(107886003)(26005)(6512007)(6666004)(1076003)(4326008)(66476007)(66556008)(66946007)(478600001)(6486002)(84970400001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: UvQX0c60Q9CcTlmIyd0qaf7iEc9XnGU5NXwSPREsvFPzBPkJG/BEqzCnJK4s3/TP8Te4XmQfavQJUh2Ar+ieVMT8egZeDahlIZZsdH0h9HgzRDCfphg/Q2SUCAMQaMk8xVR4slSO/3B3HIhcAGRBLPgxgf4EPYbPs9t51VCf4VrRiBgZ+m47h10dgiTzCWHoi/2rf2A2OLF4pbLGABso5O5FJZWeGDsZBe4q9yYpXj39P0798d58Ho9jYOwmgcbhSy+NF2l5qS2Z/bFuAC1BiwuGdQHK3P3N3I8QHs/TRh0RCkGmx1IdrTRQ77O1kRKX+OXWs249cyr7qxsB7yccnO3RUynuLzr/5MkIozz1V1HQLtDv0ofqsriRLEU0+F/2t56R6Xx5FvB8Y5JtVlMyo/45PlGOpgG1BgDpAxtMde/CZkVunyM+IuQYqH/KU9Vww+H6NFEnukkwCwTQtyQJc7plZMXQCc7LN5Tuvtob159LfKd0K58cPe+EAnoLZY5QGsLJaSuvJGRy3X2LL8+N8KnFiQZz/RSxEEvACt+LAfIb/WlABa2IDn7B4+ooOmKBgTiaxWnsp3Vp/9eojrV+uyJ7hCAswx7Ed8O/OEy7J7EwAcGt3lPYYMQQi+Mg6z3aEN+HkAuAILSDkWUgWoUCHGdPEsCwncYZpaa95NMMhj/QfIj7TPeKQ6mg+gRwHch1neMgAHIMaTwPLS+JmfVEnBrM9p6iTjelcsFra86suk6aZNsU6lFR4hsmZX282ovbb+kJudcjCOZ1Ccy/euhFditrGV8vUEkWdW15n9Fmjty5r9iFVvNjO0RpSCjtuTYey/SN0ZWbpK5qgbJ90V0kUmylH20qnii42tARfbvHq8UJYtev+5zy6/kEDLqEuSTuKhGG9OnPO4YNhbirJc1jRayqXDfuDUKv35DSyTP5J+TatgczVU4UQuZh6Zd7PM8jf0AmOpCf6g+TfMejMyeP3K/V0o2ZtcvHXvi459StKget9s0H7SWqXzNk/j4RDoH/rE7Wk7v9C9QT5flpVmPW4V/zeCFmMrL+bNllCUb+kde2LFfdslYn0QSSr2HFJivrtupAK7jefwNukaaHSK2ojKvbGlZq8ifOUyEH/PAf6N8s0KXmJFXM52Z+UB9cHVBhz1socj+AQO2yV0xZg0cILLFD6YffB3+HB7J9zPCFlYKyTrYV/8kJHgKyP5Z3jFTcbaKmS6av4h57KX6RVVWyeE9kJ9YAKdlsjnVuGlaQ2LsHpWU4rSZwHJGKx6bi3N/XdL9Vonu6HCFPxMqE6leG98r+w4con9HcgTcg9DzPkqzwnr42EdjJLf982wOW/Mm2/2I1vg8A0iozmLs7nm9sVrQz0gMW0fULd6aVLqkLwdQlSKtOhJcTeLGX9fJv4iqztP+9ZI1/2UC+Dtvf7rNntvt0Rozl2A7xlyY/5UdqV9Df1v6lmRi1AFTLMd0WiWXTDiaW9AdBoznwyH8C1HIRabh3hW4XAWL0VP8ilXOtqCIqjwCwNCRIgDyMlKt6AUYrY0TNsyqC4zV6MjoMQeD+rw4w9pM9LBqWC3kj5HUHuk3xLaO2+mz/3cVJS2/APZ7o X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: e4PxH68RFkXuOu8VTePJnZPMoSpyGnZPyREv2urwBRJ+mBREQZU3iER2hEHEbAkwkfyqfM3U/x0MLrvARtKjHMctV1wwpPHZimjEFyYEarSKYiSiRF9GcKjHbG/o4OieCcvXG33ewEhj/CKv77BAZsEHvSSuOiF4KMs2zbhqShAVF50QyVWtf1l7Nnw5OgRclIGyPAFQnaZNFel2cHHCXcW0H8mAruwRS4j6xEIAWVMq91NBf3KoD6h74cHzdk/Bwct81rk8j7fS0kOsaA8OzX+9YL3Ss8HLvGZmcSlAvWVBsSLnE1RiFOGLPzQODq25EYbF0XHnB8YvuT8Hm9QzP4amX3rz7ODnrVkm2hSBLsisrQCXU6RBRSRN51Ck7wDb64qKn3zpFmnrRZqRB0fIv8+Qx8N1vOLIsiN+wy4r+Yq1zPG9STYHr/itg4wq1/axu/do/0evEB8nFxftRRjkwqJy4a5QUJ/CiHA5gvrHQF29a1Bfe5a9AKycfaT+ojd2pGrbl0BzKwzOgEwOYbHJQTrxsVTUvlFiAEb2K+4ZPLNdNxl04sqpCbB7jDTtWId30p6dj+dz+5utoEY9jTLWPeb6C4xtgRfxDGiu1YySS5KSa50URM/361+abrz8iis5zSh98wUBuQ9sfghxTCj3Hm3jgCyNWsvyD2ZfAlSi6HXw7MYqw6htMPDn04o0QBJBfhSFuuUYOu5YWZv3YuUJh2vc4aLXeUGvj9BrQGnUDLas3bO3SjybgjOQAn7r2Y23Xl36ELOPuIjJp7fgGnF+UDA3tNix102o1NqM3UDGzzPIOoeg8X6ZCtBGsYKbwAo/hhLAQqT26D0PBZY9YEanmAVYLyVawwgvEwAN3kukLoreCESELsQRwJOVEnayZSCsCWTpEWR9EaS4MBwtlSIbceDpcxQKKfXPTbbLnJodmgSQ/eyVeYSS1KGkss5M5LTV X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4b82474e-02d1-42aa-7084-08db5d3b355b X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 May 2023 16:15:08.8891 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 2ZV5iSLXwpWriSrxhcJ+5Zu/I7hbXVu2x2Zn+uMXaPGNbsBKyPT7Vt3dnPQinRy21yeIbYaebYOfqTHc7ZlEvg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLAPR10MB5041 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.573,FMLib:17.11.176.26 definitions=2023-05-25_09,2023-05-25_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 malwarescore=0 bulkscore=0 mlxlogscore=999 mlxscore=0 adultscore=0 spamscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2304280000 definitions=main-2305250134 X-Proofpoint-ORIG-GUID: 2KTPWCr-TWSoyxCbXxkP8c7ShKtx3l03 X-Proofpoint-GUID: 2KTPWCr-TWSoyxCbXxkP8c7ShKtx3l03 X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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: Qing Zhao via Gcc-patches From: Qing Zhao Reply-To: Qing Zhao Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 2023-05-17 Qing Zhao gcc/ChangeLog: PR C/108896 * tree-object-size.cc (addr_object_size): Use the element_count attribute info. * tree.cc (component_ref_has_element_count_p): New function. (component_ref_get_element_count): New function. * tree.h (component_ref_has_element_count_p): New prototype. (component_ref_get_element_count): New prototype. gcc/testsuite/ChangeLog: PR C/108896 * gcc.dg/flex-array-element-count-2.c: New test. --- .../gcc.dg/flex-array-element-count-2.c | 56 +++++++++++ gcc/tree-object-size.cc | 37 ++++++-- gcc/tree.cc | 93 +++++++++++++++++++ gcc/tree.h | 10 ++ 4 files changed, 189 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/flex-array-element-count-2.c diff --git a/gcc/testsuite/gcc.dg/flex-array-element-count-2.c b/gcc/testsuite/gcc.dg/flex-array-element-count-2.c new file mode 100644 index 00000000000..5a280e8c731 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-element-count-2.c @@ -0,0 +1,56 @@ +/* test the attribute element_count and its usage in + * __builtin_dynamic_object_size. */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +#include "builtin-object-size-common.h" + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + __builtin_printf ("ok: %s == %zd\n", #p, p); \ + else \ + { \ + __builtin_printf ("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + FAIL (); \ + } \ +} while (0); + +struct flex { + int b; + int c[]; +} *array_flex; + +struct annotated { + int b; + int c[] __attribute__ ((element_count ("b"))); +} *array_annotated; + +void __attribute__((__noinline__)) setup (int normal_count, int attr_count) +{ + array_flex + = (struct flex *)malloc (sizeof (struct flex) + + normal_count * sizeof (int)); + array_flex->b = normal_count; + + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated) + + attr_count * sizeof (int)); + array_annotated->b = attr_count; + + return; +} + +void __attribute__((__noinline__)) test () +{ + expect(__builtin_dynamic_object_size(array_flex->c, 1), -1); + expect(__builtin_dynamic_object_size(array_annotated->c, 1), + array_annotated->b * sizeof (int)); +} + +int main(int argc, char *argv[]) +{ + setup (10,10); + test (); + DONE (); +} diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 9a936a91983..f9aadd59054 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -585,6 +585,7 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, if (pt_var != TREE_OPERAND (ptr, 0)) { tree var; + tree element_count_ref = NULL_TREE; if (object_size_type & OST_SUBOBJECT) { @@ -600,11 +601,12 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, var = TREE_OPERAND (var, 0); if (var != pt_var && TREE_CODE (var) == ARRAY_REF) var = TREE_OPERAND (var, 0); - if (! TYPE_SIZE_UNIT (TREE_TYPE (var)) + if (! component_ref_has_element_count_p (var) + && ((! TYPE_SIZE_UNIT (TREE_TYPE (var)) || ! tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (var))) || (pt_var_size && TREE_CODE (pt_var_size) == INTEGER_CST && tree_int_cst_lt (pt_var_size, - TYPE_SIZE_UNIT (TREE_TYPE (var))))) + TYPE_SIZE_UNIT (TREE_TYPE (var))))))) var = pt_var; else if (var != pt_var && TREE_CODE (pt_var) == MEM_REF) { @@ -612,6 +614,7 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, /* For &X->fld, compute object size if fld isn't a flexible array member. */ bool is_flexible_array_mem_ref = false; + while (v && v != pt_var) switch (TREE_CODE (v)) { @@ -639,6 +642,8 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, break; } is_flexible_array_mem_ref = array_ref_flexible_size_p (v); + element_count_ref = component_ref_get_element_count (v); + while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) != UNION_TYPE @@ -652,8 +657,11 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, == RECORD_TYPE) { /* compute object size only if v is not a - flexible array member. */ - if (!is_flexible_array_mem_ref) + flexible array member or the flexible array member + has a known element count indicated by the user + through attribute element_count. */ + if (!is_flexible_array_mem_ref + || element_count_ref) { v = NULL_TREE; break; @@ -686,9 +694,24 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, if (var != pt_var) { - var_size = TYPE_SIZE_UNIT (TREE_TYPE (var)); - if (!TREE_CONSTANT (var_size)) - var_size = get_or_create_ssa_default_def (cfun, var_size); + if (!element_count_ref) + { + var_size = TYPE_SIZE_UNIT (TREE_TYPE (var)); + if (!TREE_CONSTANT (var_size)) + var_size = get_or_create_ssa_default_def (cfun, var_size); + } + else + { + gcc_assert (TREE_CODE (var) == COMPONENT_REF + && TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE); + tree element_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (var))); + var_size + = size_binop (MULT_EXPR, + fold_convert (sizetype, element_count_ref), + fold_convert (sizetype, element_size)); + if (!todo) + todo = TODO_update_ssa_only_virtuals; + } if (!var_size) return false; } diff --git a/gcc/tree.cc b/gcc/tree.cc index 7e6de288886..48753b94f2c 100644 --- a/gcc/tree.cc +++ b/gcc/tree.cc @@ -12745,6 +12745,99 @@ array_ref_element_size (tree exp) return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp); } +/* For a component_ref that has an array type ARRAY_REF, return TRUE when + an element_count attribute attached to the corresponding FIELD_DECL. + return FALSE otherwise. */ +bool +component_ref_has_element_count_p (tree array_ref) +{ + if (TREE_CODE (array_ref) != COMPONENT_REF) + return false; + + if (TREE_CODE (TREE_TYPE (array_ref)) != ARRAY_TYPE) + return false; + + tree struct_object = TREE_OPERAND (array_ref, 0); + tree struct_type = TREE_TYPE (struct_object); + + if (!RECORD_OR_UNION_TYPE_P (struct_type)) + return false; + tree field_decl = TREE_OPERAND (array_ref, 1); + tree attr_element_count = lookup_attribute ("element_count", + DECL_ATTRIBUTES (field_decl)); + + if (!attr_element_count) + return false; + return true; +} + + +/* For a component_ref that has an array type ARRAY_REF, get the object that + represents its element_count per the attribute element_count attached to + the corresponding FIELD_DECL. return NULL_TREE when cannot find such + object. + For example, if: + + struct P { + int k; + int x[] __attribute__ ((element_count ("k"))); + } *p; + + for the following reference: + + p->x[b] + + the object that represents its element count will be: + + p->k + + So, when component_ref_get_element_count (p->x[b]) is called, p->k should be + returned. +*/ + +tree +component_ref_get_element_count (tree array_ref) +{ + if (! component_ref_has_element_count_p (array_ref)) + return NULL_TREE; + + tree struct_object = TREE_OPERAND (array_ref, 0); + tree struct_type = TREE_TYPE (struct_object); + tree field_decl = TREE_OPERAND (array_ref, 1); + tree attr_element_count = lookup_attribute ("element_count", + DECL_ATTRIBUTES (field_decl)); + gcc_assert (attr_element_count); + + /* If there is an element_count attribute attached to the field, + get the field that maps to the element_count. */ + + const char *fieldname + = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr_element_count))); + + tree element_count_field = NULL_TREE; + for (tree field = TYPE_FIELDS (struct_type); field; + field = DECL_CHAIN (field)) + if (TREE_CODE (field) == FIELD_DECL + && DECL_NAME (field) != NULL + && strcmp (IDENTIFIER_POINTER (DECL_NAME (field)), fieldname) == 0) + { + element_count_field = field; + break; + } + + gcc_assert (element_count_field); + + /* generate the tree node that represent the element_count of this array + ref. This is a COMPONENT_REF to the element_count_field of the + containing structure. */ + + tree element_count_ref = build3 (COMPONENT_REF, + TREE_TYPE (element_count_field), + struct_object, element_count_field, + NULL_TREE); + return element_count_ref; +} + /* Return a tree representing the lower bound of the array mentioned in EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */ diff --git a/gcc/tree.h b/gcc/tree.h index 0b72663e6a1..8f36370a02d 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -5582,6 +5582,16 @@ extern tree get_base_address (tree t); of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */ extern tree array_ref_element_size (tree); +/* Give a component_ref that has an array type, return true when an + attribute element_count attached to the corresponding FIELD_DECL. */ +extern bool component_ref_has_element_count_p (tree); + +/* Give a component_ref that has an array type, return the object that + represents its element_count per the attribute element_count attached to + the corresponding FIELD_DECL. return NULL_TREE when cannot find such + object. */ +extern tree component_ref_get_element_count (tree); + /* Return a typenode for the "standard" C type with a given name. */ extern tree get_typenode_from_name (const char *);