From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 62794 invoked by alias); 11 Mar 2018 19:56:01 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 62765 invoked by uid 89); 11 Mar 2018 19:56:00 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.9 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_LOW,SPF_PASS,T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=UD:tr, corner, sk:transfo, 57059 X-Spam-User: qpsmtpd, 2 recipients X-HELO: cc-smtpout3.netcologne.de Received: from cc-smtpout3.netcologne.de (HELO cc-smtpout3.netcologne.de) (89.1.8.213) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 11 Mar 2018 19:55:58 +0000 Received: from cc-smtpin1.netcologne.de (cc-smtpin1.netcologne.de [89.1.8.201]) by cc-smtpout3.netcologne.de (Postfix) with ESMTP id 59B8E12801; Sun, 11 Mar 2018 20:55:55 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by cc-smtpin1.netcologne.de (Postfix) with ESMTP id 4DEE511D87; Sun, 11 Mar 2018 20:55:55 +0100 (CET) Received: from [78.35.154.218] (helo=cc-smtpin1.netcologne.de) by localhost with ESMTP (eXpurgate 4.1.9) (envelope-from ) id 5aa589cb-029d-7f0000012729-7f000001b28f-1 for ; Sun, 11 Mar 2018 20:55:55 +0100 Received: from [192.168.178.68] (xdsl-78-35-154-218.netcologne.de [78.35.154.218]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by cc-smtpin1.netcologne.de (Postfix) with ESMTPSA; Sun, 11 Mar 2018 20:55:53 +0100 (CET) To: "fortran@gcc.gnu.org" , gcc-patches From: Thomas Koenig Subject: [patch, fortran] Some more corrections to zero-size simplification Message-ID: <5dcd57ba-c720-c519-221e-44686d98e791@netcologne.de> Date: Sun, 11 Mar 2018 19:56:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------6F63D65C8684BEAFD18D308D" X-SW-Source: 2018-03/txt/msg00501.txt.bz2 This is a multi-part message in MIME format. --------------6F63D65C8684BEAFD18D308D Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-length: 1024 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 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 PR fortran/66128 * gfortran.dg/zero_sized_9.f90: New test. --------------6F63D65C8684BEAFD18D308D Content-Type: text/x-fortran; name="zero_sized_9.f90" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="zero_sized_9.f90" Content-length: 1244 ! { 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 --------------6F63D65C8684BEAFD18D308D Content-Type: text/x-patch; name="p5.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="p5.diff" Content-length: 8416 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); } --------------6F63D65C8684BEAFD18D308D--