aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorIain Buclaw <ibuclaw@gdcproject.org>2022-06-24 19:48:55 +0200
committerIain Buclaw <ibuclaw@gdcproject.org>2022-06-24 20:49:58 +0200
commit8288cd635fa0bd75a8c5f25c7a90d4a7a4acec81 (patch)
treedf4f53f4d7171d7691f26ecd7c49b3fe4c3bb9a1 /gcc
parentd97f3bca6eec50ac4ec007d731d345db3e560c52 (diff)
downloadgcc-8288cd635fa0bd75a8c5f25c7a90d4a7a4acec81.zip
gcc-8288cd635fa0bd75a8c5f25c7a90d4a7a4acec81.tar.gz
gcc-8288cd635fa0bd75a8c5f25c7a90d4a7a4acec81.tar.bz2
d: Construct indexes of ARRAY_TYPE using ARRAY_REF.
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. 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.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/d/d-codegen.cc2
-rw-r--r--gcc/d/d-tree.h2
-rw-r--r--gcc/d/expr.cc38
-rw-r--r--gcc/d/intrinsics.cc4
4 files changed, 35 insertions, 11 deletions
diff --git a/gcc/d/d-codegen.cc b/gcc/d/d-codegen.cc
index 0e14654..3a20114 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 7517463..a6c3811 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 fba397b..bf75092 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 4222b8a..0f96284 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); */