From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1873) id 524F43858D35; Sat, 1 Jul 2023 23:20:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 524F43858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688253628; bh=K6AVmI5+gyOr0GRzosVFZnKJgJVCyAEnCPhvTM+sWvo=; h=From:To:Subject:Date:From; b=fo8lwJxIgE53W8H2iDqeUtiF3aTit1QMgtBnf59caFs+G9DwUBPKkBGans/lnPOci z+NbeS9ZmOjsF8viKp1FoCfQlBV904cXYXl4nTcxR8NM7o7HMxvc02LBM8LPVW3IPA s8V2IEzLnPI6h/PEO1CwhpN9bfh0nUAA4AJiFAEg= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Iain Buclaw To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-9747] d: Fix accesses of immutable arrays using constant index still bounds checked X-Act-Checkin: gcc X-Git-Author: Iain Buclaw X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 3fd9e4742a943d358d326afbba90a807ef5f374f X-Git-Newrev: 750526ee180ae2af66ee07db95c9787e8d86ed4b Message-Id: <20230701232028.524F43858D35@sourceware.org> Date: Sat, 1 Jul 2023 23:20:28 +0000 (GMT) List-Id: https://gcc.gnu.org/g:750526ee180ae2af66ee07db95c9787e8d86ed4b commit r12-9747-g750526ee180ae2af66ee07db95c9787e8d86ed4b Author: Iain Buclaw Date: Sat Jul 1 23:32:53 2023 +0200 d: Fix accesses of immutable arrays using constant index still bounds checked Starts setting TREE_READONLY against specific kinds of VAR_DECLs, so that the middle-end/optimization passes can more aggressively constant fold D code that makes use of `immutable' or `const'. 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. (cherry picked from commit 61b1c562f8c703bff045e91257120e42b7fae523) Diff: --- 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(+) diff --git a/gcc/d/decl.cc b/gcc/d/decl.cc index 1cdfc24666e..de1dfd14189 100644 --- a/gcc/d/decl.cc +++ b/gcc/d/decl.cc @@ -1264,6 +1264,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 6654244292e..46d94f81fa2 100644 --- a/gcc/d/expr.cc +++ b/gcc/d/expr.cc @@ -2708,6 +2708,10 @@ public: if (tb->ty == TY::Tarray) ctor = d_array_value (type, size_int (e->elements->length), ctor); + /* Immutable literals 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" } }