From patchwork Mon Sep 7 13:15:45 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Lawrence X-Patchwork-Id: 515139 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 19E691401CD for ; Mon, 7 Sep 2015 23:16:46 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=jfCEFRco; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references :content-type:content-transfer-encoding; q=dns; s=default; b=DW2 McSv1sGV4az76ZmL3AqbD1TdVi8BBAvUqs643pRc0YPB9XtvvJHSm4ros5wRynWW i69UQssxhZTZxMdEoXlFTSqNGi/TRda3Fpm9VbfgKPggvhBYLMtl80/vjctY64Nc SopEKvuAp9z9nOaQSsE/1FpKwXURMc4KL4oH/8kY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references :content-type:content-transfer-encoding; s=default; bh=7YMLT1P2N IinmHt0ETgMdDeq8Bc=; b=jfCEFRcoekWD3Oa5iVSmrDnYcFey19q4/rjQ6dGeF 2tyYNRupL9qJPxc7yKOqpK3m3IdWOkIKBXAeQLLHevSW0Hgo361apujWGd7n0fyp LEXVO3v50IWCKgrf8t8fWQ0A5ROG1s40ip5iCzNq+JCcH3go6t6jHmPBTwkCXJWq 7g= Received: (qmail 78653 invoked by alias); 7 Sep 2015 13:16:38 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 78618 invoked by uid 89); 7 Sep 2015 13:16:34 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.7 required=5.0 tests=AWL, BAYES_00, SPF_PASS autolearn=ham version=3.3.2 X-HELO: eu-smtp-delivery-143.mimecast.com Received: from eu-smtp-delivery-143.mimecast.com (HELO eu-smtp-delivery-143.mimecast.com) (207.82.80.143) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 07 Sep 2015 13:16:32 +0000 Received: from cam-owa2.Emea.Arm.com (fw-tnat.cambridge.arm.com [217.140.96.140]) by eu-smtp-1.mimecast.com with ESMTP id uk-mta-16-qZywI0f7Q7KHIcLdG_jZnQ-1; Mon, 07 Sep 2015 14:16:28 +0100 Received: from arm.com ([10.1.2.79]) by cam-owa2.Emea.Arm.com with Microsoft SMTPSVC(6.0.3790.3959); Mon, 7 Sep 2015 14:16:27 +0100 From: Alan Lawrence To: gcc-patches@gcc.gnu.org Cc: rguenther@suse.de, christophe.lyon@linaro.org, martin.jambor@suse.cz, law@redhat.com Subject: Re: [PATCH 2/5] completely_scalarize arrays as well as records. Date: Mon, 7 Sep 2015 14:15:45 +0100 Message-Id: <1441631745-7615-1-git-send-email-alan.lawrence@arm.com> In-Reply-To: <55E0795E.8060009@arm.com> References: <55E0795E.8060009@arm.com> X-MC-Unique: qZywI0f7Q7KHIcLdG_jZnQ-1 X-IsSubscribed: yes In-Reply-To: <55E0697D.2010008@arm.com> On 28/08/15 16:08, Alan Lawrence wrote: > Alan Lawrence wrote: >> >> Right. I think VLA's are the problem with pr64312.C also. I'm testing a fix >> (that declares arrays with any of these properties as unscalarizable). > ... > In the meantime I've reverted the patch pending further testing on x86, aarch64 > and arm. I've now tested g++ and fortran (+ bootstrap + check-gcc) on x86, AArch64 and ARM, and Ada on x86 and ARM. So far the list of failures from the original patch seems to be: * g++.dg/torture/pr64312.C on ARM and m68k-linux * Building Ada on x86 * Ada ACATS c87b31a on ARM (where the Ada frontend builds fine) Here's a new version, that fixes all the above, by adding a dose of paranoia in scalarizable_type_p... (I wonder about adding a comment in completely_scalarize that such cases have already been ruled out?) OK to install? Cheers, Alan --- gcc/testsuite/gcc.dg/tree-ssa/sra-15.c | 37 ++++++++ gcc/tree-sra.c | 155 +++++++++++++++++++++++---------- 2 files changed, 144 insertions(+), 48 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/sra-15.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/sra-15.c b/gcc/testsuite/gcc.dg/tree-ssa/sra-15.c new file mode 100644 index 0000000..a22062e --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/sra-15.c @@ -0,0 +1,37 @@ +/* Verify that SRA total scalarization works on records containing arrays. */ +/* { dg-do run } */ +/* { dg-options "-O1 -fdump-tree-release_ssa --param sra-max-scalarization-size-Ospeed=32" } */ + +extern void abort (void); + +struct S +{ + char c; + unsigned short f[2][2]; + int i; + unsigned short f3, f4; +}; + + +int __attribute__ ((noinline)) +foo (struct S *p) +{ + struct S l; + + l = *p; + l.i++; + l.f[1][0] += 3; + *p = l; +} + +int +main (int argc, char **argv) +{ + struct S a = {0, { {5, 7}, {9, 11} }, 4, 0, 0}; + foo (&a); + if (a.i != 5 || a.f[1][0] != 12) + abort (); + return 0; +} + +/* { dg-final { scan-tree-dump-times "l;" 0 "release_ssa" } } */ diff --git a/gcc/tree-sra.c b/gcc/tree-sra.c index 8b3a0ad..d9fe058 100644 --- a/gcc/tree-sra.c +++ b/gcc/tree-sra.c @@ -915,73 +915,132 @@ create_access (tree expr, gimple stmt, bool write) } -/* Return true iff TYPE is a RECORD_TYPE with fields that are either of gimple - register types or (recursively) records with only these two kinds of fields. - It also returns false if any of these records contains a bit-field. */ +/* Return true iff TYPE is scalarizable - i.e. a RECORD_TYPE or fixed-length + ARRAY_TYPE with fields that are either of gimple register types (excluding + bit-fields) or (recursively) scalarizable types. */ static bool -type_consists_of_records_p (tree type) +scalarizable_type_p (tree type) { - tree fld; + gcc_assert (!is_gimple_reg_type (type)); - if (TREE_CODE (type) != RECORD_TYPE) - return false; + switch (TREE_CODE (type)) + { + case RECORD_TYPE: + for (tree fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld)) + if (TREE_CODE (fld) == FIELD_DECL) + { + tree ft = TREE_TYPE (fld); - for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld)) - if (TREE_CODE (fld) == FIELD_DECL) - { - tree ft = TREE_TYPE (fld); + if (DECL_BIT_FIELD (fld)) + return false; - if (DECL_BIT_FIELD (fld)) - return false; + if (!is_gimple_reg_type (ft) + && !scalarizable_type_p (ft)) + return false; + } - if (!is_gimple_reg_type (ft) - && !type_consists_of_records_p (ft)) - return false; - } + return true; - return true; + case ARRAY_TYPE: + { + if (TYPE_DOMAIN (type) == NULL_TREE + || !TREE_CONSTANT (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + || !TREE_CONSTANT (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + || !TREE_CONSTANT (TYPE_SIZE (type)) + || (tree_to_shwi (TYPE_SIZE (type)) <= 0)) + return false; + tree elem = TREE_TYPE (type); + if (DECL_P (elem) && DECL_BIT_FIELD (elem)) + return false; + if (!is_gimple_reg_type (elem) + && !scalarizable_type_p (elem)) + return false; + return true; + } + default: + return false; + } } -/* Create total_scalarization accesses for all scalar type fields in DECL that - must be of a RECORD_TYPE conforming to type_consists_of_records_p. BASE - must be the top-most VAR_DECL representing the variable, OFFSET must be the - offset of DECL within BASE. REF must be the memory reference expression for - the given decl. */ +static void scalarize_elem (tree, HOST_WIDE_INT, HOST_WIDE_INT, tree, tree); + +/* Create total_scalarization accesses for all scalar fields of a member + of type DECL_TYPE conforming to scalarizable_type_p. BASE + must be the top-most VAR_DECL representing the variable; within that, + OFFSET locates the member and REF must be the memory reference expression for + the member. */ static void -completely_scalarize_record (tree base, tree decl, HOST_WIDE_INT offset, - tree ref) +completely_scalarize (tree base, tree decl_type, HOST_WIDE_INT offset, tree ref) { - tree fld, decl_type = TREE_TYPE (decl); + switch (TREE_CODE (decl_type)) + { + case RECORD_TYPE: + for (tree fld = TYPE_FIELDS (decl_type); fld; fld = DECL_CHAIN (fld)) + if (TREE_CODE (fld) == FIELD_DECL) + { + HOST_WIDE_INT pos = offset + int_bit_position (fld); + tree ft = TREE_TYPE (fld); + tree nref = build3 (COMPONENT_REF, ft, ref, fld, NULL_TREE); - for (fld = TYPE_FIELDS (decl_type); fld; fld = DECL_CHAIN (fld)) - if (TREE_CODE (fld) == FIELD_DECL) + scalarize_elem (base, pos, tree_to_uhwi (DECL_SIZE (fld)), nref, + ft); + } + break; + case ARRAY_TYPE: { - HOST_WIDE_INT pos = offset + int_bit_position (fld); - tree ft = TREE_TYPE (fld); - tree nref = build3 (COMPONENT_REF, TREE_TYPE (fld), ref, fld, - NULL_TREE); - - if (is_gimple_reg_type (ft)) + tree elemtype = TREE_TYPE (decl_type); + tree elem_size = TYPE_SIZE (elemtype); + gcc_assert (elem_size && tree_fits_uhwi_p (elem_size)); + int el_size = tree_to_uhwi (elem_size); + gcc_assert (el_size); + + tree minidx = TYPE_MIN_VALUE (TYPE_DOMAIN (decl_type)); + tree maxidx = TYPE_MAX_VALUE (TYPE_DOMAIN (decl_type)); + gcc_assert (TREE_CODE (minidx) == INTEGER_CST + && TREE_CODE (maxidx) == INTEGER_CST); + unsigned HOST_WIDE_INT len = tree_to_uhwi (maxidx) + + 1 - tree_to_uhwi (minidx); + /* 4th operand to ARRAY_REF is size in units of the type alignment. */ + for (unsigned HOST_WIDE_INT idx = 0; idx < len; idx++) { - struct access *access; - HOST_WIDE_INT size; - - size = tree_to_uhwi (DECL_SIZE (fld)); - access = create_access_1 (base, pos, size); - access->expr = nref; - access->type = ft; - access->grp_total_scalarization = 1; - /* Accesses for intraprocedural SRA can have their stmt NULL. */ + tree t_idx = build_int_cst (TYPE_DOMAIN (decl_type), idx); + tree nref = build4 (ARRAY_REF, elemtype, ref, t_idx, NULL_TREE, + NULL_TREE); + int el_off = offset + idx * el_size; + scalarize_elem (base, el_off, el_size, nref, elemtype); } - else - completely_scalarize_record (base, fld, pos, nref); } + break; + default: + gcc_unreachable (); + } +} + +/* Create total_scalarization accesses for a member of type TYPE, which must + satisfy either is_gimple_reg_type or scalarizable_type_p. BASE must be the + top-most VAR_DECL representing the variable; within that, POS and SIZE locate + the member and REF must be the reference expression for it. */ + +static void +scalarize_elem (tree base, HOST_WIDE_INT pos, HOST_WIDE_INT size, + tree ref, tree type) +{ + if (is_gimple_reg_type (type)) + { + struct access *access = create_access_1 (base, pos, size); + access->expr = ref; + access->type = type; + access->grp_total_scalarization = 1; + /* Accesses for intraprocedural SRA can have their stmt NULL. */ + } + else + completely_scalarize (base, type, pos, ref); } /* Create a total_scalarization access for VAR as a whole. VAR must be of a - RECORD_TYPE conforming to type_consists_of_records_p. */ + RECORD_TYPE or ARRAY_TYPE conforming to scalarizable_type_p. */ static void create_total_scalarization_access (tree var) @@ -2521,13 +2580,13 @@ analyze_all_variable_accesses (void) tree var = candidate (i); if (TREE_CODE (var) == VAR_DECL - && type_consists_of_records_p (TREE_TYPE (var))) + && scalarizable_type_p (TREE_TYPE (var))) { if (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (var))) <= max_scalarization_size) { create_total_scalarization_access (var); - completely_scalarize_record (var, var, 0, var); + completely_scalarize (var, TREE_TYPE (var), 0, var); if (dump_file && (dump_flags & TDF_DETAILS)) { fprintf (dump_file, "Will attempt to totally scalarize ");