From patchwork Fri Jun 24 18:57:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1648139 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=E4e/w1pk; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LV5wX3XtHz9sGJ for ; Sat, 25 Jun 2022 04:57:48 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6A121383E6A6 for ; Fri, 24 Jun 2022 18:57:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6A121383E6A6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1656097066; bh=pKgnkhjBd4Crk1TiKJDg9rWoVIXVl/m4h6SV+tYCmSg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=E4e/w1pkLw7JOtV4bwGhCffE3B1HiQEFAVet76lK9vW9u1+W+87dvoj30LKmqW4Ov DeZHztv+o5glAvoKvZY2fkkS3vnOyLmA6vtXVZXAYUrA0HN8h+Nyo/LKyXAXKXTb5B AYEK7e9KOrqBVC3tw6UoS6yE4tKJaOSUpxEiQZVI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [80.241.56.171]) by sourceware.org (Postfix) with ESMTPS id 960D5383DBBF for ; Fri, 24 Jun 2022 18:57:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 960D5383DBBF Received: from smtp2.mailbox.org (smtp2.mailbox.org [10.196.197.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4LV5w30L8wz9sRV; Fri, 24 Jun 2022 20:57:23 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [committed] d: Construct indexes of ARRAY_TYPE using ARRAY_REF. Date: Fri, 24 Jun 2022 20:57:21 +0200 Message-Id: <20220624185721.3906435-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, 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: Iain Buclaw via Gcc-patches From: Iain Buclaw Reply-To: Iain Buclaw Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Hi, This patch changes the D front-end code generation of index expressions to use an ARRAY_REF when the array expression is a ARRAY_TYPE. This is a small simplification over `((T *)&array)[index]', which also allows eliding an unneccesary marking of TREE_ADDRESSABLE when the array expression is a parameter or variable declaration. Bootstrapped and regression tested on x86_64-linux-gnu/-m32/-mx32, and committed to mainline. Regards, Iain. --- gcc/d/ChangeLog: * d-codegen.cc (build_array_index): Rename to... (build_pointer_index): ...this. * d-tree.h (build_array_index): Rename declaration to... (build_pointer_index): ...this. * expr.cc (ExprVisitor::visit (IndexExp *)): Construct indexes of ARRAY_TYPE using ARRAY_REF. (ExprVisitor::visit (SliceExp *)): Update. * intrinsics.cc (expand_intrinsic_bt): Update. --- gcc/d/d-codegen.cc | 2 +- gcc/d/d-tree.h | 2 +- gcc/d/expr.cc | 38 +++++++++++++++++++++++++++++++------- gcc/d/intrinsics.cc | 4 ++-- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/gcc/d/d-codegen.cc b/gcc/d/d-codegen.cc index 0e14654e56b..3a201149d74 100644 --- a/gcc/d/d-codegen.cc +++ b/gcc/d/d-codegen.cc @@ -1626,7 +1626,7 @@ build_deref (tree exp) /* Builds pointer offset expression PTR[INDEX]. */ tree -build_array_index (tree ptr, tree index) +build_pointer_index (tree ptr, tree index) { if (error_operand_p (ptr) || error_operand_p (index)) return error_mark_node; diff --git a/gcc/d/d-tree.h b/gcc/d/d-tree.h index 751746395e6..a6c38119458 100644 --- a/gcc/d/d-tree.h +++ b/gcc/d/d-tree.h @@ -578,7 +578,7 @@ extern tree imaginary_part (tree); extern tree complex_expr (tree, tree, tree); extern tree indirect_ref (tree, tree); extern tree build_deref (tree); -extern tree build_array_index (tree, tree); +extern tree build_pointer_index (tree, tree); extern tree build_offset_op (tree_code, tree, tree); extern tree build_offset (tree, tree); extern tree build_memref (tree, tree, tree); diff --git a/gcc/d/expr.cc b/gcc/d/expr.cc index fba397bed35..bf750924594 100644 --- a/gcc/d/expr.cc +++ b/gcc/d/expr.cc @@ -1222,9 +1222,8 @@ public: } else { - /* Get the data pointer and length for static and dynamic arrays. */ + /* Get the array and length for static and dynamic arrays. */ tree array = d_save_expr (build_expr (e->e1)); - tree ptr = convert_expr (array, tb1, tb1->nextOf ()->pointerTo ()); tree length = NULL_TREE; if (tb1->ty != TY::Tpointer) @@ -1245,10 +1244,35 @@ public: if (tb1->ty != TY::Tpointer) index = build_bounds_index_condition (e, index, length); - /* Index the .ptr. */ - ptr = void_okay_p (ptr); - this->result_ = indirect_ref (TREE_TYPE (TREE_TYPE (ptr)), - build_array_index (ptr, index)); + /* Convert vectors to their underlying array type. */ + if (VECTOR_TYPE_P (TREE_TYPE (array))) + { + tree array_type = + build_array_type_nelts (TREE_TYPE (TREE_TYPE (array)), + TYPE_VECTOR_SUBPARTS (TREE_TYPE (array))); + d_mark_addressable (array); + array = build1 (VIEW_CONVERT_EXPR, array_type, array); + } + + if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) + { + /* Generate `array[index]'. When the index is non-constant, we must + mark the array as addressable because we'll need to do pointer + arithmetic on its address. */ + if (TREE_CODE (index) != INTEGER_CST) + d_mark_addressable (array); + + this->result_ = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (array)), + array, index, NULL_TREE, NULL_TREE); + } + else + { + /* Generate `array.ptr[index]'. */ + tree ptr = convert_expr (array, tb1, tb1->nextOf ()->pointerTo ()); + ptr = void_okay_p (ptr); + this->result_ = indirect_ref (TREE_TYPE (TREE_TYPE (ptr)), + build_pointer_index (ptr, index)); + } } } @@ -1347,7 +1371,7 @@ public: if (!integer_zerop (lwr_tree)) { tree ptrtype = TREE_TYPE (ptr); - ptr = build_array_index (void_okay_p (ptr), lwr_tree); + ptr = build_pointer_index (void_okay_p (ptr), lwr_tree); ptr = build_nop (ptrtype, ptr); } diff --git a/gcc/d/intrinsics.cc b/gcc/d/intrinsics.cc index 4222b8a0290..0f96284473f 100644 --- a/gcc/d/intrinsics.cc +++ b/gcc/d/intrinsics.cc @@ -306,8 +306,8 @@ expand_intrinsic_bt (intrinsic_code intrinsic, tree callexp) tree bitsize = fold_convert (type, TYPE_SIZE (TREE_TYPE (ptr))); /* ptr[bitnum / bitsize] */ - ptr = build_array_index (ptr, fold_build2 (TRUNC_DIV_EXPR, type, - bitnum, bitsize)); + ptr = build_pointer_index (ptr, fold_build2 (TRUNC_DIV_EXPR, type, + bitnum, bitsize)); ptr = indirect_ref (type, ptr); /* mask = 1 << (bitnum % bitsize); */