From patchwork Mon Apr 27 18:45:08 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Koenig X-Patchwork-Id: 465178 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 E48F01400B7 for ; Tue, 28 Apr 2015 04:45:34 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass reason="1024-bit key; unprotected key" header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=hsLvhn2k; dkim-adsp=none (unprotected policy); 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 :message-id:date:from:mime-version:to:subject:references :in-reply-to:content-type; q=dns; s=default; b=epdi2B6bsaJiFzx8S oc8/1JtdyViWh2SULgwbLygCa/tvdN7uHSh0EdCslvSuxMaLB27U1kLIkNGxmb9U P9E4D9NEzSUs7cBkUnc+JVvo1Pcydp+hv7oxmQNWZr/HBnFKdwTjHxBgBNWlomAM Y4tEc/v/JVhDZqTQP6DNgOxlN4= 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 :message-id:date:from:mime-version:to:subject:references :in-reply-to:content-type; s=default; bh=H0Y/zxu+HwYWingFvBAL4lB UxE4=; b=hsLvhn2ko/vP5yI3HgwU9dp1jkn9ddKltEFf9RyeTWJ/bTZ3NB9GCES MSO5yeRRYKPhzwWpel9w9MXKkpIahBS849JJ2Tw1UCSyAVWMkp5D3rXu44CCS4D5 JNJpUbOtIH+7IWm/VgtSbifBAhoL5oWlIB63Eo0cuuKR6+4VI8Jw= Received: (qmail 10394 invoked by alias); 27 Apr 2015 18:45:18 -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 10375 invoked by uid 89); 27 Apr 2015 18:45:17 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.2 required=5.0 tests=AWL, BAYES_50, KAM_ASCII_DIVIDERS, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_LOW, T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: cc-smtpout2.netcologne.de Received: from cc-smtpout2.netcologne.de (HELO cc-smtpout2.netcologne.de) (89.1.8.212) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Mon, 27 Apr 2015 18:45:15 +0000 Received: from cc-smtpin3.netcologne.de (cc-smtpin3.netcologne.de [89.1.8.203]) by cc-smtpout2.netcologne.de (Postfix) with ESMTP id 01D5D1201B; Mon, 27 Apr 2015 20:45:12 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by cc-smtpin3.netcologne.de (Postfix) with ESMTP id E4AE611DFC; Mon, 27 Apr 2015 20:45:11 +0200 (CEST) Received: from [78.35.156.120] (helo=cc-smtpin3.netcologne.de) by localhost with ESMTP (eXpurgate 4.0.6) (envelope-from ) id 553e83b7-0b4b-7f0000012729-7f000001bf9d-1 for ; Mon, 27 Apr 2015 20:45:11 +0200 Received: from [192.168.178.20] (xdsl-78-35-156-120.netcologne.de [78.35.156.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by cc-smtpin3.netcologne.de (Postfix) with ESMTPSA; Mon, 27 Apr 2015 20:45:09 +0200 (CEST) Message-ID: <553E83B4.8020005@netcologne.de> Date: Mon, 27 Apr 2015 20:45:08 +0200 From: Thomas Koenig User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-Version: 1.0 To: Mikael Morin , "fortran@gcc.gnu.org" , gcc-patches Subject: Re: [Patch, Fortran] Simplify lbound References: <553B7BA4.6040103@netcologne.de> <553BD925.3050401@sfr.fr> In-Reply-To: <553BD925.3050401@sfr.fr> Am 25.04.2015 um 20:12 schrieb Mikael Morin: > I've double-checked in the standard, and it seems it is not possible to > simplify after all: > > If ARRAY is a whole array and either ARRAY is an assumed-size > array of rank DIM or dimension DIM of ARRAY has nonzero extent, > LBOUND (ARRAY, DIM) has a value equal to the lower bound for > subscript DIM of ARRAY. Otherwise the result value is 1. > > We can't tell whether the array is zero-sized, so we can't tell the > lbound value. So it is only possible to simplify LBOUND if the lower bound is equal to one, both for assumed-shape and explicit-shape arrays... OK. The attached patch does that, including a test case which catches that particular case. > As you may want to simplify in the limited scope of the matmul inlining, > I'm giving comments about the patch (otherwise you can ignore them): > - No need to check for allocatable or pointer, it should be excluded by > as->type == AS_ASSUMED_SHAPE (but does no harm either). Actually, no. You can have assumed-shape allocatable or pointer dummy arguments which keep their original lbound; see the subroutine 'bar' in the test case. > - Please modify the early return condition: > if (as && (as->type == AS_DEFERRED || as->type == AS_ASSUMED_SHAPE > || as->type == AS_ASSUMED_RANK)) > return NULL; > and let the existing code do the simplification work. That is not part of my patch. > Or drop the lbound simplification idea, and fetch the lbound "by hand" > at matmul inline time. I will probably do so as a future optimization, but I think that most people will see no reason for using different lower bounds, so it is OK for the time being to (slightly) pessimize this case. So... here is the new patch. OK for trunk? Thomas 2015-04-25 Thomas Koenig PR fortran/37131 * simplify.c (simplify_bound): Get constant lower bounds of one from array spec for assumed and explicit shape shape arrays if the lower bounds are indeed one. 2015-04-25 Thomas Koenig PR fortran/37131 * gfortran.dg/coarray_lib_this_image_2.f90: Adjust scan pattern. * gfortran.dg/bound_9.f90: New test case. P.S: In an earlier version, I also added but that caused the condition to always return true. I haven't figured out why, but either I am misunderstanding something here, or gfc_likely is buggy, or both. Index: simplify.c =================================================================== --- simplify.c (Revision 222431) +++ simplify.c (Arbeitskopie) @@ -3445,6 +3445,39 @@ simplify_bound (gfc_expr *array, gfc_expr *dim, gf done: + /* If the array shape is assumed shape or explicit, we can simplify lbound + to 1 if the given lower bound is one because this matches what lbound + should return for an empty array. */ + + if (!upper && as && dim && dim->expr_type == EXPR_CONSTANT + && (as->type == AS_ASSUMED_SHAPE || as->type == AS_EXPLICIT) + && ref->u.ar.type != AR_SECTION) + { + /* Watch out for allocatable or pointer dummy arrays, they can have + lower bounds that are not equal to one. */ + if (!(array->symtree && array->symtree->n.sym + && (array->symtree->n.sym->attr.allocatable + || array->symtree->n.sym->attr.pointer))) + { + unsigned long int ndim; + gfc_expr *lower, *res; + + ndim = mpz_get_si (dim->value.integer) - 1; + lower = as->lower[ndim]; + if (lower->expr_type == EXPR_CONSTANT + && mpz_cmp_si (lower->value.integer, 1) == 0) + { + res = gfc_copy_expr (lower); + if (kind) + { + int nkind = mpz_get_si (kind->value.integer); + res->ts.kind = nkind; + } + return res; + } + } + } + if (as && (as->type == AS_DEFERRED || as->type == AS_ASSUMED_SHAPE || as->type == AS_ASSUMED_RANK)) return NULL;