From patchwork Wed Aug 17 17:59:57 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 110342 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 BF59EB6F99 for ; Thu, 18 Aug 2011 04:00:25 +1000 (EST) Received: (qmail 14719 invoked by alias); 17 Aug 2011 18:00:23 -0000 Received: (qmail 14706 invoked by uid 22791); 17 Aug 2011 18:00:21 -0000 X-SWARE-Spam-Status: No, hits=-2.6 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from rcsinet15.oracle.com (HELO rcsinet15.oracle.com) (148.87.113.117) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 17 Aug 2011 18:00:05 +0000 Received: from rtcsinet21.oracle.com (rtcsinet21.oracle.com [66.248.204.29]) by rcsinet15.oracle.com (Switch-3.4.4/Switch-3.4.4) with ESMTP id p7HI02ev005452 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 17 Aug 2011 18:00:04 GMT Received: from acsmt358.oracle.com (acsmt358.oracle.com [141.146.40.158]) by rtcsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id p7HI00BD017472 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 17 Aug 2011 18:00:01 GMT Received: from abhmt108.oracle.com (abhmt108.oracle.com [141.146.116.60]) by acsmt358.oracle.com (8.12.11.20060308/8.12.11) with ESMTP id p7HHxtDF005763; Wed, 17 Aug 2011 12:59:55 -0500 Received: from [192.168.1.4] (/79.33.222.167) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 17 Aug 2011 10:59:54 -0700 Message-ID: <4E4C019D.1040201@oracle.com> Date: Wed, 17 Aug 2011 19:59:57 +0200 From: Paolo Carlini User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20110624 Thunderbird/5.0 MIME-Version: 1.0 To: "gcc-patches@gcc.gnu.org" CC: Richard Guenther Subject: [Patch] Fix tree-optimization/49963 X-IsSubscribed: yes 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, I prepared the below basing on an hint provided by Joseph on the audit trail: essentially, I'm replacing all (but two) uses of abs_hwi outside hwint.c by absu_hwi, a variant which returns an unsigned HOST_WIDE_INT. All the replacements make sense to me: either we are comparing two abs, or we are passing the abs to a function actually expecting an unsigned HOST_WIDE_INT, or we are comparing to another unsigned HOST_WIDE_INT, or we are just comparing to a constant (I don't feel strongly in this case but seems safe to use absu_hwi here too). I'm *not* replacing 2 occurrences in gimple_expand_builtin_pow, because those are safe anyway in terms of range of the argument (it's an HOST_WIDE_INT / 2 or 3) and the result is passed to a function expecting a plain HOST_WIDE_INT (ie, gimple_expand_builtin_powi). I sanity checked the patch on x86_64-linux and OP reported that on AVR the patch fixes the regression. Is it ok? Thanks, Paolo. PS: compared to the draft version, the attached uses cast to unsigned in both arms of the ? : operator, I think Joseph preferred that stylistically in a snippet of him posted in an unrelated recent audit trail. /////////////////////////// 2011-08-17 Paolo Carlini Joseph Myers PR tree-optimization/49963 * hwint.c (absu_hwi): Define. * hwint.h (absu_hwi): Declare. * fold-const.c (fold_plusminus_mult_expr): Use absu_hwi instead of abs_hwi. * tree-ssa-math-opts.c (gimple_expand_builtin_pow): Likewise. * tree-ssa-loop-prefetch.c (prune_ref_by_group_reuse): Likewise. Index: fold-const.c =================================================================== --- fold-const.c (revision 177834) +++ fold-const.c (working copy) @@ -7036,7 +7036,7 @@ fold_plusminus_mult_expr (location_t loc, enum tre int11 = TREE_INT_CST_LOW (arg11); /* Move min of absolute values to int11. */ - if (abs_hwi (int01) < abs_hwi (int11)) + if (absu_hwi (int01) < absu_hwi (int11)) { tmp = int01, int01 = int11, int11 = tmp; alt0 = arg00, arg00 = arg10, arg10 = alt0; @@ -7046,7 +7046,7 @@ fold_plusminus_mult_expr (location_t loc, enum tre else maybe_same = arg11; - if (exact_log2 (abs_hwi (int11)) > 0 && int01 % int11 == 0 + if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0 /* The remainder should not be a constant, otherwise we end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has increased the number of multiplications necessary. */ Index: tree-ssa-math-opts.c =================================================================== --- tree-ssa-math-opts.c (revision 177834) +++ tree-ssa-math-opts.c (working copy) @@ -1231,7 +1231,7 @@ gimple_expand_builtin_pow (gimple_stmt_iterator *g /* Attempt to fold powi(arg0, abs(n/2)) into multiplies. If not possible or profitable, give up. Skip the degenerate case when n is 1 or -1, where the result is always 1. */ - if (abs_hwi (n) != 1) + if (absu_hwi (n) != 1) { powi_x_ndiv2 = gimple_expand_builtin_powi (gsi, loc, arg0, abs_hwi (n / 2)); @@ -1243,7 +1243,7 @@ gimple_expand_builtin_pow (gimple_stmt_iterator *g result of the optimal multiply sequence just calculated. */ sqrt_arg0 = build_and_insert_call (gsi, loc, &target, sqrtfn, arg0); - if (abs_hwi (n) == 1) + if (absu_hwi (n) == 1) result = sqrt_arg0; else result = build_and_insert_binop (gsi, loc, target, MULT_EXPR, @@ -1285,7 +1285,7 @@ gimple_expand_builtin_pow (gimple_stmt_iterator *g /* Attempt to fold powi(arg0, abs(n/3)) into multiplies. If not possible or profitable, give up. Skip the degenerate case when abs(n) < 3, where the result is always 1. */ - if (abs_hwi (n) >= 3) + if (absu_hwi (n) >= 3) { powi_x_ndiv3 = gimple_expand_builtin_powi (gsi, loc, arg0, abs_hwi (n / 3)); @@ -1298,14 +1298,14 @@ gimple_expand_builtin_pow (gimple_stmt_iterator *g either cbrt(x) or cbrt(x) * cbrt(x). */ cbrt_x = build_and_insert_call (gsi, loc, &target, cbrtfn, arg0); - if (abs_hwi (n) % 3 == 1) + if (absu_hwi (n) % 3 == 1) powi_cbrt_x = cbrt_x; else powi_cbrt_x = build_and_insert_binop (gsi, loc, target, MULT_EXPR, cbrt_x, cbrt_x); /* Multiply the two subexpressions, unless powi(x,abs(n)/3) = 1. */ - if (abs_hwi (n) < 3) + if (absu_hwi (n) < 3) result = powi_cbrt_x; else result = build_and_insert_binop (gsi, loc, target, MULT_EXPR, Index: tree-ssa-loop-prefetch.c =================================================================== --- tree-ssa-loop-prefetch.c (revision 177834) +++ tree-ssa-loop-prefetch.c (working copy) @@ -795,7 +795,7 @@ prune_ref_by_group_reuse (struct mem_ref *ref, str prefetch_before = (hit_from - delta_r + step - 1) / step; /* Do not reduce prefetch_before if we meet beyond cache size. */ - if (prefetch_before > (unsigned) abs_hwi (L2_CACHE_SIZE_BYTES / step)) + if (prefetch_before > absu_hwi (L2_CACHE_SIZE_BYTES / step)) prefetch_before = PREFETCH_ALL; if (prefetch_before < ref->prefetch_before) ref->prefetch_before = prefetch_before; Index: hwint.c =================================================================== --- hwint.c (revision 177834) +++ hwint.c (working copy) @@ -109,6 +109,14 @@ abs_hwi (HOST_WIDE_INT x) return x >= 0 ? x : -x; } +/* Compute the absolute value of X as an unsigned type. */ + +unsigned HOST_WIDE_INT +absu_hwi (HOST_WIDE_INT x) +{ + return x >= 0 ? (unsigned HOST_WIDE_INT)x : -(unsigned HOST_WIDE_INT)x; +} + /* Compute the greatest common divisor of two numbers A and B using Euclid's algorithm. */ Index: hwint.h =================================================================== --- hwint.h (revision 177834) +++ hwint.h (working copy) @@ -233,6 +233,7 @@ exact_log2 (unsigned HOST_WIDE_INT x) #define HOST_WIDE_INT_MAX (~(HOST_WIDE_INT_MIN)) extern HOST_WIDE_INT abs_hwi (HOST_WIDE_INT); +extern unsigned HOST_WIDE_INT absu_hwi (HOST_WIDE_INT); extern HOST_WIDE_INT gcd (HOST_WIDE_INT, HOST_WIDE_INT); extern HOST_WIDE_INT pos_mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT); extern HOST_WIDE_INT mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);