This patch also uses memset for zeroing contiguous array slices. A
regression led to the discovery that, for
real, dimension (1,2) :: b
b(1,:) = 0.
end
gfc_is_simply_contiguous returned false, whereas gfc_full_array_ref_p
returned true. This is also fixed.
Regression-tested. OK for trunk?
Best regards
Thomas
gcc/fortran/ChangeLog:
PR fortran/40598
PR fortran/128776
* expr.cc (gfc_is_simply_contiguous): An extent of one can
also be contiguous.
* trans-expr.cc (gfc_trans_zero_assign): Modify to also deal with
array slices.
(copyable_slice_p): Reallocatable expressions can be fine; check
is in gfc_trans_assignment. Also return false for expressions
which have a corank.
(gfc_trans_assignment): Use copyable_slice_p instead of
copyable_array_p for gfc_trans_zero_assign.
gcc/testsuite/ChangeLog:
PR fortran/40598
PR fortran/128776
* gfortran.dg/vect/fast-math-rnflow-trs2a2.f90: Adjust vect count;
zeroing is now done by memset.
* gfortran.dg/array_memset_4.f90: New test.
diff --git a/gcc/fortran/expr.cc b/gcc/fortran/expr.cc
index a88cd9a8c1f..1b896b40de8 100644
--- a/gcc/fortran/expr.cc
+++ b/gcc/fortran/expr.cc
@@ -6559,6 +6559,13 @@ gfc_is_simply_contiguous (gfc_expr *expr, bool strict, bool permit_element)
if (ar->dimen_type[i] == DIMEN_ELEMENT)
{
+ /* An extent of one does not hurt as long as we aren't strict. */
+ if (ar->as && ar->as->lower[i] && ar->as->upper[i]
+ && ar->start[i]
+ && gfc_dep_compare_expr (ar->as->lower[i], ar->as->upper[i]) == 0
+ && gfc_dep_compare_expr (ar->as->lower[i], ar->start[i]) == 0)
+ continue;
+
colon = false;
continue;
}
diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index 9a5e0b19f5b..f53c81ed11a 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -13024,50 +13024,51 @@ gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
return gfc_finish_block (&se.pre);
}
-
-/* Try to efficiently translate array(:) = 0. Return NULL if this
- can't be done. */
+/* Try to efficiently translate array(:,1) or similar to zero. */
static tree
gfc_trans_zero_assign (gfc_expr * expr)
{
- tree dest, len, type;
+ tree dest_decl, len, type, dest;
tree tmp;
gfc_symbol *sym;
+ stmtblock_t block;
+ gfc_se dest_se;
sym = expr->symtree->n.sym;
- dest = gfc_get_symbol_decl (sym);
+ dest_decl = gfc_get_symbol_decl (sym);
+
+ type = TREE_TYPE (dest_decl);
+ if (POINTER_TYPE_P (dest_decl))
+ type = TREE_TYPE (dest_decl);
- type = TREE_TYPE (dest);
- if (POINTER_TYPE_P (type))
- type = TREE_TYPE (type);
- if (GFC_ARRAY_TYPE_P (type))
+ /* If we are zeroing a full local array avoid taking its address by emitting
+ a = {} instead. */
+
+ if (gfc_full_array_ref_p (expr->ref, NULL) && GFC_ARRAY_TYPE_P (type))
{
- /* Determine the length of the array. */
len = GFC_TYPE_ARRAY_SIZE (type);
- if (!len || TREE_CODE (len) != INTEGER_CST)
- return NULL_TREE;
+ if (len != NULL_TREE && TREE_CODE (len) == INTEGER_CST
+ && !POINTER_TYPE_P (TREE_TYPE (dest_decl)))
+ return build2_loc (input_location, MODIFY_EXPR, void_type_node,
+ dest_decl, build_constructor (TREE_TYPE (dest_decl),
+ NULL));
}
- else if (GFC_DESCRIPTOR_TYPE_P (type)
- && gfc_is_simply_contiguous (expr, false, false))
- {
- if (POINTER_TYPE_P (TREE_TYPE (dest)))
- dest = build_fold_indirect_ref_loc (input_location, dest);
- len = gfc_conv_descriptor_size (dest, GFC_TYPE_ARRAY_RANK (type));
- dest = gfc_conv_descriptor_data_get (dest);
- }
- else
- return NULL_TREE;
- /* If we are zeroing a local array avoid taking its address by emitting
- a = {} instead. */
- if (!POINTER_TYPE_P (TREE_TYPE (dest)))
- return build2_loc (input_location, MODIFY_EXPR, void_type_node,
- dest, build_constructor (TREE_TYPE (dest),
- NULL));
+ gfc_init_block (&block);
+ gfc_init_se (&dest_se, NULL);
+ gfc_conv_expr_descriptor (&dest_se, expr);
+ gfc_add_block_to_block (&block, &dest_se.pre);
+
+ len = gfc_conv_descriptor_size (dest_se.expr, expr->rank);
+
+ if (gfc_full_array_ref_p (expr->ref, NULL) && GFC_ARRAY_TYPE_P (type))
+ dest = dest_decl;
+ else
+ dest = gfc_conv_descriptor_data_get (dest_se.expr);
/* Multiply len by element size. */
- tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
+ tmp = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (dest_se.expr)));
len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
len, fold_convert (gfc_array_index_type, tmp));
@@ -13076,10 +13077,16 @@ gfc_trans_zero_assign (gfc_expr * expr)
len = fold_convert (size_type_node, len);
/* Construct call to __builtin_memset. */
+
tmp = build_call_expr_loc (input_location,
builtin_decl_explicit (BUILT_IN_MEMSET),
3, dest, integer_zero_node, len);
- return fold_convert (void_type_node, tmp);
+ tmp = fold_convert (void_type_node, tmp);
+
+ gfc_add_expr_to_block (&block, tmp);
+ gfc_add_block_to_block (&block, &dest_se.post);
+
+ return gfc_finish_block (&block);
}
@@ -14456,15 +14463,13 @@ copyable_slice_p (gfc_expr * expr)
return false;
/* Don't touch anything with coarrays. */
- if (gfc_is_coindexed (expr) || expr->symtree->n.sym->attr.codimension)
+ if (gfc_is_coindexed (expr) || expr->symtree->n.sym->attr.codimension
+ || expr->corank > 0)
return false;
if (!gfc_is_simply_contiguous (expr, false, false))
return false;
- if (gfc_is_reallocatable_lhs (expr))
- return false;
-
if (gfc_may_be_finalized (expr->ts))
return false;
@@ -14507,9 +14512,8 @@ gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
return tmp;
}
- /* Special case assigning an array to zero. */
- if (copyable_array_p (expr1)
- && is_zero_initializer_p (expr2))
+ /* Special case assigning an array or array slice to zero. */
+ if (copyable_slice_p (expr1) && is_zero_initializer_p (expr2))
{
tmp = gfc_trans_zero_assign (expr1);
if (tmp)
diff --git a/gcc/testsuite/gfortran.dg/array_memset_4.f90 b/gcc/testsuite/gfortran.dg/array_memset_4.f90
new file mode 100644
index 00000000000..472c5477c35
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/array_memset_4.f90
@@ -0,0 +1,27 @@
+! { dg-do run }
+! { dg-additional-options "-fdump-tree-original" }
+! PR 40598 - optimizing zero assignments via memset for
+! array slices.
+
+program memain
+ integer, dimension(:,:), allocatable :: a
+ integer, dimension(3,3) :: b
+ integer, dimension(4,3) :: c
+ allocate (a(3,3))
+ a = 1
+ a(:,2) = 0
+ if (any (a /= reshape([1,1,1,0,0,0,1,1,1], shape(a)))) stop 1
+ b = 2
+ b(:2,3) = 0
+ if (any (b /= reshape([2,2,2,2,2,2,0,0,2], shape(b)))) stop 2
+ c = 3
+ call foo(c,4,3,2)
+ if (any(c /= reshape([3,3,3,3,3,0,0,0,3,3,3,3], shape(c)))) stop 3
+contains
+ subroutine foo(x,n,m,k)
+ integer, intent(in) :: n, m, k
+ integer, dimension(n,m), intent(inout) :: x
+ x(k:,k) = 0
+ end subroutine foo
+end program memain
+! { dg-final { scan-tree-dump-times "__builtin_memset" 3 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/vect/fast-math-rnflow-trs2a2.f90 b/gcc/testsuite/gfortran.dg/vect/fast-math-rnflow-trs2a2.f90
index 02d3e545673..635435a11e1 100644
--- a/gcc/testsuite/gfortran.dg/vect/fast-math-rnflow-trs2a2.f90
+++ b/gcc/testsuite/gfortran.dg/vect/fast-math-rnflow-trs2a2.f90
@@ -30,4 +30,4 @@
return
end function trs2a2
-! { dg-final { scan-tree-dump-times "vectorized 2 loops" 1 "vect" } }
+! { dg-final { scan-tree-dump-times "vectorized 1 loops" 1 "vect" } }