From patchwork Sun Dec 16 22:00:47 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Koenig X-Patchwork-Id: 1014211 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-492610-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=netcologne.de Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="TADNIKbn"; dkim=pass (2048-bit key; unprotected) header.d=netcologne.de header.i=@netcologne.de header.b="GKhLBdVh"; dkim-atps=neutral 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 43Hysf5khTz9s5c for ; Mon, 17 Dec 2018 09:01:09 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=xz+0BHBq+WSAqjueOpuU7VBqxdS36iAeEt6juyuxcKd01tdCLW 3Qu5sCuwosQT+x4qedPsEBt+rQhHiHMVfh6bTUQO7/iJjsnSF02wy06P9Y//rDoo VdJFk0mUrN3CGHse2Y52rEvk8CnLCj6U6uSALjOpmRuow6IueFWfJ52+0= 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:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=W4LoNNroDRuSl0xFoQtqkAnfEh0=; b=TADNIKbnt40D1SZ/bDwx CrguD+5gYFGuEB1Rnj2nq3Y1PeOHFpP9U9sP27DWnCeY8SxVOBBbDUcKk6l2uqX4 F+Pw1AOP2uo4hayjc2DfSVf6WfkuPVkh48M6CdA4YI3lZUu9YjGyVKha+HzvzD3g Il4FG6jxDthHXIBRQK93Y+U= Received: (qmail 95060 invoked by alias); 16 Dec 2018 22:00:57 -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 95027 invoked by uid 89); 16 Dec 2018 22:00:55 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-8.6 required=5.0 tests=BAYES_50, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, KAM_NUMSUBJECT, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=powers, strongly, sk:gfc_get, input_location 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, 16 Dec 2018 22:00:52 +0000 Received: from cc-smtpin1.netcologne.de (cc-smtpin1.netcologne.de [89.1.8.201]) by cc-smtpout3.netcologne.de (Postfix) with ESMTP id 6BF4B1262C; Sun, 16 Dec 2018 23:00:49 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=netcologne.de; s=nc1116a; t=1544997649; bh=2A2ZeotOX4MlDedlO2FjBEqKrtDmgxJE77AUksRIXp8=; h=To:From:Subject:Message-ID:Date:From; b=GKhLBdVhhSNwDpnuH7j2vozqtU6EKUXhmZ/GD3zSE6v6RBpW9sO0RGZrlTlHiI05L T8yWQSSKO3+Jc/Zo7EVq0GbR6ddTow+QaQtyOjFGfBUnq+nXRLe8SFGVIZJsKj6B5n Xp9XsqxOi21p8/0PNYGjj4i2HbzYzegZPtk/eJBA3JJr+EqCdSOfufw2pFDBQHZzn0 nBWPOOHMznDtE4rSGUh8c+t5kThdhFVpb+UIXFD8t6//MSt/li9s9ZzHm3dNbc2LdJ ESS/mhoG2joFP9CdipDCl51RoM5jMi2LM+Wexjt0DruC+d86sk/7vwdqv/nI0kKuFc rELgARsj/EZng== Received: from localhost (localhost [127.0.0.1]) by cc-smtpin1.netcologne.de (Postfix) with ESMTP id 5E65511EBF; Sun, 16 Dec 2018 23:00:49 +0100 (CET) Received: from [78.35.148.227] (helo=cc-smtpin1.netcologne.de) by localhost with ESMTP (eXpurgate 4.6.0) (envelope-from ) id 5c16cb11-5f8a-7f0000012729-7f000001d160-1 for ; Sun, 16 Dec 2018 23:00:49 +0100 Received: from [192.168.178.68] (xdsl-78-35-148-227.nc.de [78.35.148.227]) (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, 16 Dec 2018 23:00:48 +0100 (CET) To: "fortran@gcc.gnu.org" , gcc-patches From: Thomas Koenig Subject: [patch, fortran] Fix PR 85544 Message-ID: Date: Sun, 16 Dec 2018 23:00:47 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 MIME-Version: 1.0 Hello world, the PR pointed out an old regression because the front-end optimization pass was substituting 2**n with ishift(1,n), where n was an array. Simply removing the optimization for that case would have been easy, but also introduced a performance regression. So, for this, I moved the optimization to trans-*, where it makes more sense. Regression-tested. This turned up that one of our tests, mvbits_1.f90, depends on the behavior that 2**32 is zero. This is certainly not guaranteed by the standard, but I chose to keep the behavior as not to introduce any changes in behavior. This fixes a regression, so I would like to backport to all active branches if this if possible. Oh yes, if anybody feels strongly that we should also optimize 32**n and powers of other powers to two, now is the time to speak up :-) OK for affected branches? Regards Thomas 2018-12-16 Thomas Koenig PR fortran/85544 * frontend-passes.c (optimize_power): Remove. (optimize_op): Remove call to optimize_power. * trans-expr.c (gfc_conv_power_op): Handle cases of 1**integer, (2|4|8|16) ** integer and (-1) ** integer. 2018-12-16 Thomas Koenig PR fortran/85544 * gfortran.dg/power_7.f90: New test. Index: frontend-passes.c =================================================================== --- frontend-passes.c (Revision 267172) +++ frontend-passes.c (Arbeitskopie) @@ -1863,84 +1863,6 @@ combine_array_constructor (gfc_expr *e) return true; } -/* Change (-1)**k into 1-ishift(iand(k,1),1) and - 2**k into ishift(1,k) */ - -static bool -optimize_power (gfc_expr *e) -{ - gfc_expr *op1, *op2; - gfc_expr *iand, *ishft; - - if (e->ts.type != BT_INTEGER) - return false; - - op1 = e->value.op.op1; - - if (op1 == NULL || op1->expr_type != EXPR_CONSTANT) - return false; - - if (mpz_cmp_si (op1->value.integer, -1L) == 0) - { - gfc_free_expr (op1); - - op2 = e->value.op.op2; - - if (op2 == NULL) - return false; - - iand = gfc_build_intrinsic_call (current_ns, GFC_ISYM_IAND, - "_internal_iand", e->where, 2, op2, - gfc_get_int_expr (e->ts.kind, - &e->where, 1)); - - ishft = gfc_build_intrinsic_call (current_ns, GFC_ISYM_ISHFT, - "_internal_ishft", e->where, 2, iand, - gfc_get_int_expr (e->ts.kind, - &e->where, 1)); - - e->value.op.op = INTRINSIC_MINUS; - e->value.op.op1 = gfc_get_int_expr (e->ts.kind, &e->where, 1); - e->value.op.op2 = ishft; - return true; - } - else if (mpz_cmp_si (op1->value.integer, 2L) == 0) - { - gfc_free_expr (op1); - - op2 = e->value.op.op2; - if (op2 == NULL) - return false; - - ishft = gfc_build_intrinsic_call (current_ns, GFC_ISYM_ISHFT, - "_internal_ishft", e->where, 2, - gfc_get_int_expr (e->ts.kind, - &e->where, 1), - op2); - *e = *ishft; - return true; - } - - else if (mpz_cmp_si (op1->value.integer, 1L) == 0) - { - op2 = e->value.op.op2; - if (op2 == NULL) - return false; - - gfc_free_expr (op1); - gfc_free_expr (op2); - - e->expr_type = EXPR_CONSTANT; - e->value.op.op1 = NULL; - e->value.op.op2 = NULL; - mpz_init_set_si (e->value.integer, 1); - /* Typespec and location are still OK. */ - return true; - } - - return false; -} - /* Recursive optimization of operators. */ static bool @@ -2001,9 +1923,6 @@ optimize_op (gfc_expr *e) case INTRINSIC_DIVIDE: return combine_array_constructor (e) || changed; - case INTRINSIC_POWER: - return optimize_power (e); - default: break; } Index: trans-expr.c =================================================================== --- trans-expr.c (Revision 267187) +++ trans-expr.c (Arbeitskopie) @@ -3056,6 +3056,83 @@ gfc_conv_power_op (gfc_se * se, gfc_expr * expr) if (gfc_conv_cst_int_power (se, lse.expr, rse.expr)) return; + if (INTEGER_CST_P (lse.expr) + && TREE_CODE (TREE_TYPE (rse.expr)) == INTEGER_TYPE) + { + wi::tree_to_wide_ref wlhs = wi::to_wide (lse.expr); + HOST_WIDE_INT v; + v = wlhs.to_shwi (); + if (v == 1) + { + /* 1**something is always 1. */ + se->expr = build_int_cst (TREE_TYPE (lse.expr), 1); + return; + } + else if (v == 2 || v == 4 || v == 8 || v == 16) + { + /* 2**n = 1<expr = fold_build3_loc (input_location, COND_EXPR, type, cond2, + build_int_cst (type, 0), cond); + return; + } + else if (v == -1) + { + /* (-1)**n is 1 - ((n & 1) << 1) */ + tree type; + tree tmp; + + type = TREE_TYPE (lse.expr); + tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type, + rse.expr, build_int_cst (type, 1)); + tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type, + tmp, build_int_cst (type, 1)); + tmp = fold_build2_loc (input_location, MINUS_EXPR, type, + build_int_cst (type, 1), tmp); + se->expr = tmp; + return; + } + } + gfc_int4_type_node = gfc_get_int_type (4); /* In case of integer operands with kinds 1 or 2, we call the integer kind 4