From patchwork Fri Dec 3 22:45:40 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Kraft X-Patchwork-Id: 74218 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]) by ozlabs.org (Postfix) with SMTP id 464B6B70A8 for ; Sat, 4 Dec 2010 09:38:56 +1100 (EST) Received: (qmail 30379 invoked by alias); 3 Dec 2010 22:38:53 -0000 Received: (qmail 30277 invoked by uid 22791); 3 Dec 2010 22:38:52 -0000 X-SWARE-Spam-Status: No, hits=-2.1 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_HELO_PASS, TW_FN X-Spam-Check-By: sourceware.org Received: from taro.utanet.at (HELO taro.utanet.at) (213.90.36.45) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 03 Dec 2010 22:38:45 +0000 Received: from pam.xoc.tele2net.at ([213.90.36.6]) by taro.utanet.at with esmtp (Exim 4.71) (envelope-from ) id 1POeH5-0001W0-Av; Fri, 03 Dec 2010 23:38:43 +0100 Received: from d83-187-160-20.cust.tele2.at ([83.187.160.20] helo=[10.0.0.18]) by pam.xoc.tele2net.at with esmtpa (Exim 4.71) (envelope-from ) id 1POeH5-0003aY-5W; Fri, 03 Dec 2010 23:38:43 +0100 Message-ID: <4CF97314.4000206@domob.eu> Date: Fri, 03 Dec 2010 23:45:40 +0100 From: Daniel Kraft User-Agent: Thunderbird 2.0.0.0 (X11/20070425) MIME-Version: 1.0 To: Fortran List , gcc-patches Subject: [Patch, Fortran] PR fortran/46794: Fix ICE with powers of integers 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 Hi all, I'm (a little) back to gfortran ;) The attached patch fixes the problem of PR 46794 -- namely that when we call _gfortran_pow_i4_i4 even for integer operands with kinds 1 / 2, the result was not converted back properly to the smaller kinds and this ICEd (in certain cases). The fix is simply to add an appropriate conversion. I do not entirely like the way this is done in the patch (with the two variables and "special casing"), but don't see a better implementation -- what do you think? Regression-tested on x86_64-unknown-linux-gnu without failures -- though the run somehow looked strange to me (on the compile-farm); I'll try again to be sure. Ok for trunk? Yours, Daniel Index: gcc/testsuite/gfortran.dg/power2.f90 =================================================================== --- gcc/testsuite/gfortran.dg/power2.f90 (revision 0) +++ gcc/testsuite/gfortran.dg/power2.f90 (revision 0) @@ -0,0 +1,22 @@ +! { dg-do compile } +! PR fortran/46794 + +! Check that results of powers of integers with kinds 1 and 2 are +! correctly converted back; this used to ICE because a conversion +! from kind 4 to the correct one was missing. + +! Contributed by Daniel Kraft, d@domob.eu. + +PROGRAM main + IMPLICIT NONE + + INTEGER(KIND=1) :: k1 + INTEGER(KIND=2) :: k2 + + k1 = 1_1 + 1_1**k1 + k2 = 1_2 + 1_2**k2 + + k2 = 1_1 + 1_1**k2 + k2 = 1_1 + 1_2**k1 + k2 = 1_1 + 1_2**k2 +END PROGRAM main Index: gcc/fortran/trans-expr.c =================================================================== --- gcc/fortran/trans-expr.c (revision 167440) +++ gcc/fortran/trans-expr.c (working copy) @@ -976,6 +976,7 @@ tree gfc_int4_type_node; int kind; int ikind; + int res_ikind_1, res_ikind_2; gfc_se lse; gfc_se rse; tree fndecl = NULL; @@ -996,6 +997,13 @@ 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 + library routine. But in the end, we have to convert the result back + if this case applies -- with res_ikind_K, we keep track whether operand K + falls into this case. */ + res_ikind_1 = -1; + res_ikind_2 = -1; + kind = expr->value.op.op1->ts.kind; switch (expr->value.op.op2->ts.type) { @@ -1006,6 +1014,7 @@ case 1: case 2: rse.expr = convert (gfc_int4_type_node, rse.expr); + res_ikind_2 = ikind; /* Fall through. */ case 4: @@ -1028,7 +1037,10 @@ case 1: case 2: if (expr->value.op.op1->ts.type == BT_INTEGER) - lse.expr = convert (gfc_int4_type_node, lse.expr); + { + lse.expr = convert (gfc_int4_type_node, lse.expr); + res_ikind_1 = kind; + } else gcc_unreachable (); /* Fall through. */ @@ -1121,6 +1133,15 @@ se->expr = build_call_expr_loc (input_location, fndecl, 2, lse.expr, rse.expr); + + /* Convert the result back if it is of wrong integer kind. */ + if (res_ikind_1 != -1 && res_ikind_2 != -1) + { + /* We want the maximum of both operand kinds as result. */ + if (res_ikind_1 < res_ikind_2) + res_ikind_1 = res_ikind_2; + se->expr = convert (gfc_get_int_type (res_ikind_1), se->expr); + } }