From patchwork Sat Jul 1 23:21:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1802276 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=2620:52:3:1:0:246e:9693:128c; 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=UvHKxGkM; dkim-atps=neutral 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 ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QtpB06Qzcz20ZC for ; Sun, 2 Jul 2023 09:22:15 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3E8C73858413 for ; Sat, 1 Jul 2023 23:22:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3E8C73858413 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688253733; bh=jVDYEGnuoVbSJVMydpLhsfuly6tIs0ukReNvOP/7yFE=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=UvHKxGkMfnr6AJMNHUX0nnJUVA6tkq/MK+5BisX5IgEQYt/4J+bDAWt8WbA4qDoPo V3VGA8SToFuqFzKIR8A44n8ViEUq1Q/gWCwDqdlLIQ9KyQdvto4ODFnNp33QQbNan5 EeaC5IpYIZsVncLC5maTPrquiJ29KRs2iLxQTBQk= 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 D06E93858D39 for ; Sat, 1 Jul 2023 23:21:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D06E93858D39 Received: from smtp202.mailbox.org (smtp202.mailbox.org [10.196.197.202]) (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 4Qtp9V0Dxgz9sQ8; Sun, 2 Jul 2023 01:21:50 +0200 (CEST) To: gcc-patches@gcc.gnu.org Cc: Iain Buclaw Subject: [committed] d: Fix accesses of immutable arrays using constant index still bounds checked Date: Sun, 2 Jul 2023 01:21:47 +0200 Message-Id: <20230701232147.3265358-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.2 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 sets TREE_READONLY on all non-static const and immutable variables in D, as well as all static immutable variables that aren't initialized by a module constructor. This allows more aggressive constant folding of D code which makes use of `immutable' or `const'. Bootstrapped and regression tested on x86_64-linux-gnu, committed to mainline, and backported to releases/gcc-13 and releases/gcc-12. Regards, Iain. --- PR d/110514 gcc/d/ChangeLog: * decl.cc (get_symbol_decl): Set TREE_READONLY on certain kinds of const and immutable variables. * expr.cc (ExprVisitor::visit (ArrayLiteralExp *)): Set TREE_READONLY on immutable dynamic array literals. gcc/testsuite/ChangeLog: * gdc.dg/pr110514a.d: New test. * gdc.dg/pr110514b.d: New test. * gdc.dg/pr110514c.d: New test. * gdc.dg/pr110514d.d: New test. --- gcc/d/decl.cc | 14 ++++++++++++++ gcc/d/expr.cc | 4 ++++ gcc/testsuite/gdc.dg/pr110514a.d | 9 +++++++++ gcc/testsuite/gdc.dg/pr110514b.d | 8 ++++++++ gcc/testsuite/gdc.dg/pr110514c.d | 8 ++++++++ gcc/testsuite/gdc.dg/pr110514d.d | 8 ++++++++ 6 files changed, 51 insertions(+) create mode 100644 gcc/testsuite/gdc.dg/pr110514a.d create mode 100644 gcc/testsuite/gdc.dg/pr110514b.d create mode 100644 gcc/testsuite/gdc.dg/pr110514c.d create mode 100644 gcc/testsuite/gdc.dg/pr110514d.d diff --git a/gcc/d/decl.cc b/gcc/d/decl.cc index 78c4ab554dc..3f980851259 100644 --- a/gcc/d/decl.cc +++ b/gcc/d/decl.cc @@ -1277,6 +1277,20 @@ get_symbol_decl (Declaration *decl) DECL_INITIAL (decl->csym) = build_expr (ie, true); } } + + /* [type-qualifiers/const-and-immutable] + + `immutable` applies to data that cannot change. Immutable data values, + once constructed, remain the same for the duration of the program's + execution. */ + if (vd->isImmutable () && !vd->setInCtorOnly ()) + TREE_READONLY (decl->csym) = 1; + + /* `const` applies to data that cannot be changed by the const reference + to that data. It may, however, be changed by another reference to that + same data. */ + if (vd->isConst () && !vd->isDataseg ()) + TREE_READONLY (decl->csym) = 1; } /* Set the declaration mangled identifier if static. */ diff --git a/gcc/d/expr.cc b/gcc/d/expr.cc index c6245ff5fc1..b7cec1327fd 100644 --- a/gcc/d/expr.cc +++ b/gcc/d/expr.cc @@ -2701,6 +2701,10 @@ public: if (tb->ty == TY::Tarray) ctor = d_array_value (type, size_int (e->elements->length), ctor); + /* Immutable data can be placed in rodata. */ + if (tb->isImmutable ()) + TREE_READONLY (decl) = 1; + d_pushdecl (decl); rest_of_decl_compilation (decl, 1, 0); } diff --git a/gcc/testsuite/gdc.dg/pr110514a.d b/gcc/testsuite/gdc.dg/pr110514a.d new file mode 100644 index 00000000000..46e370527d3 --- /dev/null +++ b/gcc/testsuite/gdc.dg/pr110514a.d @@ -0,0 +1,9 @@ +// { dg-do "compile" } +// { dg-options "-O -fdump-tree-optimized" } +immutable uint[] imm_arr = [1,2,3]; +int test_imm(immutable uint[] ptr) +{ + return imm_arr[2] == 3 ? 123 : 456; +} +// { dg-final { scan-assembler-not "_d_arraybounds_indexp" } } +// { dg-final { scan-tree-dump "return 123;" optimized } } diff --git a/gcc/testsuite/gdc.dg/pr110514b.d b/gcc/testsuite/gdc.dg/pr110514b.d new file mode 100644 index 00000000000..86aeb485c34 --- /dev/null +++ b/gcc/testsuite/gdc.dg/pr110514b.d @@ -0,0 +1,8 @@ +// { dg-do "compile" } +// { dg-options "-O" } +immutable uint[] imm_ctor_arr; +int test_imm_ctor(immutable uint[] ptr) +{ + return imm_ctor_arr[2] == 3; +} +// { dg-final { scan-assembler "_d_arraybounds_indexp" } } diff --git a/gcc/testsuite/gdc.dg/pr110514c.d b/gcc/testsuite/gdc.dg/pr110514c.d new file mode 100644 index 00000000000..94779e123a4 --- /dev/null +++ b/gcc/testsuite/gdc.dg/pr110514c.d @@ -0,0 +1,8 @@ +// { dg-do "compile" } +// { dg-options "-O" } +const uint[] cst_arr = [1,2,3]; +int test_cst(const uint[] ptr) +{ + return cst_arr[2] == 3; +} +// { dg-final { scan-assembler "_d_arraybounds_indexp" } } diff --git a/gcc/testsuite/gdc.dg/pr110514d.d b/gcc/testsuite/gdc.dg/pr110514d.d new file mode 100644 index 00000000000..56e9a3139ea --- /dev/null +++ b/gcc/testsuite/gdc.dg/pr110514d.d @@ -0,0 +1,8 @@ +// { dg-do "compile" } +// { dg-options "-O" } +const uint[] cst_ctor_arr; +int test_cst_ctor(const uint[] ptr) +{ + return cst_ctor_arr[2] == 3; +} +// { dg-final { scan-assembler "_d_arraybounds_indexp" } }