This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[patch, fortran] Some more corrections to zero-size simplification
- From: Thomas Koenig <tkoenig at netcologne dot de>
- To: "fortran at gcc dot gnu dot org" <fortran at gcc dot gnu dot org>, gcc-patches <gcc-patches at gcc dot gnu dot org>
- Date: Sun, 11 Mar 2018 20:55:53 +0100
- Subject: [patch, fortran] Some more corrections to zero-size simplification
- Authentication-results: sourceware.org; auth=none
Hello world,
the attached patch fixes a few corner cases of a corner case in
simplification, i.e. empty arrays where array intrinsics can actually
have a non-empty array result.
Regression-tested. OK for trunk?
Regards
Thomas
2017-06-11 Thomas Koenig <tkoenig@gcc.gnu.org>
PR fortran/66128
* simplify.c (simplify_transformation): Return default result for
empty array argument.
(gfc_simplify_all): Remove special-case handling for zerosize.
(gfc_simplify_any): Likewise.
(gfc_simplify_count): Likewise.
(gfc_simplify_iall): Likewise.
(gfc_simplify_iany): Likewise.
(gfc_simplify_iparity): Likewise.
(gfc_simplify_minval): Likewise.
(gfc_simplify_maxval): Likewise.
(gfc_simplify_norm2): Likewise.
(gfc_simplify_product): Likewise.
(gfc_simplify_sum): Likewise.
2017-06-11 Thomas Koenig <tkoenig@gcc.gnu.org>
PR fortran/66128
* gfortran.dg/zero_sized_9.f90: New test.
! { dg-do run }
program main
implicit none
integer, parameter :: a(0,3) = 0
integer, parameter :: b(3,0) = -42
integer, parameter, dimension(3) :: a1 = minval(a,dim=1)
integer, parameter, dimension(0) :: a2 = minval(a,dim=2)
integer, parameter, dimension(0) :: b1 = minval(b,dim=1)
integer, parameter, dimension(3) :: b2 = minval(b,dim=2)
logical, parameter :: c(0,3) = .false.
logical, parameter :: d(3,0) = .false.
logical, parameter, dimension(3) :: tr = all(c,dim=1)
logical, parameter, dimension(3) :: fa = any(c,dim=1)
integer, parameter, dimension(3) :: ze = count(d,dim=2)
integer, parameter, dimension(3) :: ze2 = iany(b,dim=2)
integer, parameter, dimension(3) :: ze3 = iparity(a,dim=1)
real, parameter, dimension(0,3) :: r = 1.0
real, parameter, dimension(3) :: n2 = norm2(r,dim=1)
integer, parameter, dimension(3) :: one = product(b,dim=2)
integer, parameter, dimension(3) :: ze4 = sum(a,dim=1)
if (any(a1 /= huge(0))) stop 1
if (any(b2 /= huge(b2))) stop 2
if (any(.not.tr)) stop 3
if (any(fa)) stop 3
if (any(ze /= 0)) stop 4
if (any(ze2 /= 0)) stop 5
if (any(ze3 /= 0)) stop 6
if (any(n2 /= 0.0)) stop 7
if (any(one /= 1)) stop 8
if (any(ze4 /= 0)) stop 9
end program main
Index: simplify.c
===================================================================
--- simplify.c (Revision 258433)
+++ simplify.c (Arbeitskopie)
@@ -689,8 +689,11 @@ simplify_transformation (gfc_expr *array, gfc_expr
int init_val, transformational_op op)
{
gfc_expr *result;
+ bool size_zero;
- if (!is_constant_array_expr (array)
+ size_zero = gfc_is_size_zero_array (array);
+
+ if (!(is_constant_array_expr (array) || size_zero)
|| !gfc_is_constant_expr (dim))
return NULL;
@@ -703,6 +706,9 @@ simplify_transformation (gfc_expr *array, gfc_expr
array->ts.kind, &array->where);
init_result_expr (result, init_val, array);
+ if (size_zero)
+ return result;
+
return !dim || array->rank == 1 ?
simplify_transformation_to_scalar (result, array, mask, op) :
simplify_transformation_to_array (result, array, dim, mask, op, NULL);
@@ -976,9 +982,6 @@ gfc_simplify_aint (gfc_expr *e, gfc_expr *k)
gfc_expr *
gfc_simplify_all (gfc_expr *mask, gfc_expr *dim)
{
- if (gfc_is_size_zero_array (mask))
- return gfc_get_logical_expr (mask->ts.kind, &mask->where, true);
-
return simplify_transformation (mask, dim, NULL, true, gfc_and);
}
@@ -1068,9 +1071,6 @@ gfc_simplify_and (gfc_expr *x, gfc_expr *y)
gfc_expr *
gfc_simplify_any (gfc_expr *mask, gfc_expr *dim)
{
- if (gfc_is_size_zero_array (mask))
- return gfc_get_logical_expr (mask->ts.kind, &mask->where, false);
-
return simplify_transformation (mask, dim, NULL, false, gfc_or);
}
@@ -1966,15 +1966,11 @@ gfc_expr *
gfc_simplify_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
{
gfc_expr *result;
+ bool size_zero;
- if (gfc_is_size_zero_array (mask))
- {
- int k;
- k = kind ? mpz_get_si (kind->value.integer) : gfc_default_integer_kind;
- return gfc_get_int_expr (k, NULL, 0);
- }
+ size_zero = gfc_is_size_zero_array (mask);
- if (!is_constant_array_expr (mask)
+ if (!(is_constant_array_expr (mask) || size_zero)
|| !gfc_is_constant_expr (dim)
|| !gfc_is_constant_expr (kind))
return NULL;
@@ -1987,6 +1983,9 @@ gfc_simplify_count (gfc_expr *mask, gfc_expr *dim,
init_result_expr (result, 0, NULL);
+ if (size_zero)
+ return result;
+
/* Passing MASK twice, once as data array, once as mask.
Whenever gfc_count is called, '1' is added to the result. */
return !dim || mask->rank == 1 ?
@@ -3265,9 +3264,6 @@ do_bit_and (gfc_expr *result, gfc_expr *e)
gfc_expr *
gfc_simplify_iall (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
{
- if (gfc_is_size_zero_array (array))
- return gfc_get_int_expr (array->ts.kind, NULL, -1);
-
return simplify_transformation (array, dim, mask, -1, do_bit_and);
}
@@ -3287,9 +3283,6 @@ do_bit_ior (gfc_expr *result, gfc_expr *e)
gfc_expr *
gfc_simplify_iany (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
{
- if (gfc_is_size_zero_array (array))
- return gfc_get_int_expr (array->ts.kind, NULL, 0);
-
return simplify_transformation (array, dim, mask, 0, do_bit_ior);
}
@@ -3730,9 +3723,6 @@ do_bit_xor (gfc_expr *result, gfc_expr *e)
gfc_expr *
gfc_simplify_iparity (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
{
- if (gfc_is_size_zero_array (array))
- return gfc_get_int_expr (array->ts.kind, NULL, 0);
-
return simplify_transformation (array, dim, mask, 0, do_bit_xor);
}
@@ -5040,43 +5030,6 @@ gfc_min (gfc_expr *op1, gfc_expr *op2)
gfc_expr *
gfc_simplify_minval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask)
{
- if (gfc_is_size_zero_array (array))
- {
- gfc_expr *result;
- int i;
-
- i = gfc_validate_kind (array->ts.type, array->ts.kind, false);
- result = gfc_get_constant_expr (array->ts.type, array->ts.kind,
- &array->where);
- switch (array->ts.type)
- {
- case BT_INTEGER:
- mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
- break;
-
- case BT_REAL:
- mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
- break;
-
- case BT_CHARACTER:
- /* If ARRAY has size zero and type character, the result has the
- value of a string of characters of length LEN (ARRAY), with
- each character equal to CHAR(n - 1, KIND (ARRAY)), where n is
- the number of characters in the collating sequence for
- characters with the kind type parameter of ARRAY. */
- gfc_error ("MINVAL(string) at %L is not implemented, yet!",
- &array->where);
- gfc_free_expr (result);
- return &gfc_bad_expr;
- break;
-
- default:
- gcc_unreachable ();
- }
-
- return result;
- }
-
return simplify_transformation (array, dim, mask, INT_MAX, gfc_min);
}
@@ -5096,42 +5049,6 @@ gfc_max (gfc_expr *op1, gfc_expr *op2)
gfc_expr *
gfc_simplify_maxval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask)
{
- if (gfc_is_size_zero_array (array))
- {
- gfc_expr *result;
- int i;
-
- i = gfc_validate_kind (array->ts.type, array->ts.kind, false);
- result = gfc_get_constant_expr (array->ts.type, array->ts.kind,
- &array->where);
- switch (array->ts.type)
- {
- case BT_INTEGER:
- mpz_set (result->value.integer, gfc_integer_kinds[i].min_int);
- break;
-
- case BT_REAL:
- mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
- mpfr_neg (result->value.real, result->value.real, GFC_RND_MODE);
- break;
-
- case BT_CHARACTER:
- /* If ARRAY has size zero and type character, the result has the
- value of a string of characters of length LEN (ARRAY), with
- each character equal to CHAR (0, KIND (ARRAY)). */
- gfc_error ("MAXVAL(string) at %L is not implemented, yet!",
- &array->where);
- gfc_free_expr (result);
- return &gfc_bad_expr;
- break;
-
- default:
- gcc_unreachable ();
- }
-
- return result;
- }
-
return simplify_transformation (array, dim, mask, INT_MIN, gfc_max);
}
@@ -5777,16 +5694,11 @@ gfc_expr *
gfc_simplify_norm2 (gfc_expr *e, gfc_expr *dim)
{
gfc_expr *result;
+ bool size_zero;
- if (gfc_is_size_zero_array (e))
- {
- gfc_expr *result;
- result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
- mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
- return result;
- }
+ size_zero = gfc_is_size_zero_array (e);
- if (!is_constant_array_expr (e)
+ if (!(is_constant_array_expr (e) || size_zero)
|| (dim != NULL && !gfc_is_constant_expr (dim)))
return NULL;
@@ -5793,6 +5705,9 @@ gfc_simplify_norm2 (gfc_expr *e, gfc_expr *dim)
result = transformational_result (e, dim, e->ts.type, e->ts.kind, &e->where);
init_result_expr (result, 0, NULL);
+ if (size_zero)
+ return result;
+
if (!dim || e->rank == 1)
{
result = simplify_transformation_to_scalar (result, e, NULL,
@@ -6042,33 +5957,6 @@ gfc_simplify_precision (gfc_expr *e)
gfc_expr *
gfc_simplify_product (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
{
- if (gfc_is_size_zero_array (array))
- {
- gfc_expr *result;
-
- result = gfc_get_constant_expr (array->ts.type, array->ts.kind,
- &array->where);
- switch (array->ts.type)
- {
- case BT_INTEGER:
- mpz_set_ui (result->value.integer, 1);
- break;
-
- case BT_REAL:
- mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
- break;
-
- case BT_COMPLEX:
- mpc_set_ui (result->value.complex, 1, GFC_MPC_RND_MODE);
- break;
-
- default:
- gcc_unreachable ();
- }
-
- return result;
- }
-
return simplify_transformation (array, dim, mask, 1, gfc_multiply);
}
@@ -7386,33 +7274,6 @@ gfc_simplify_sqrt (gfc_expr *e)
gfc_expr *
gfc_simplify_sum (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
{
- if (gfc_is_size_zero_array (array))
- {
- gfc_expr *result;
-
- result = gfc_get_constant_expr (array->ts.type, array->ts.kind,
- &array->where);
- switch (array->ts.type)
- {
- case BT_INTEGER:
- mpz_set_ui (result->value.integer, 0);
- break;
-
- case BT_REAL:
- mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
- break;
-
- case BT_COMPLEX:
- mpc_set_ui (result->value.complex, 0, GFC_MPC_RND_MODE);
- break;
-
- default:
- gcc_unreachable ();
- }
-
- return result;
- }
-
return simplify_transformation (array, dim, mask, 0, gfc_add);
}