From patchwork Tue Mar 13 15:54:08 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 146436 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 5C554B6EEA for ; Wed, 14 Mar 2012 02:54:32 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1332258872; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Date: From:To:Subject:Message-ID:User-Agent:MIME-Version:Content-Type: Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:Sender:Delivered-To; bh=UCNS8Nri7h5vlEYKWm6e +nvqHmg=; b=m6hOTSphqWxPOpZ/SVjFBwIh4tpNom8oR2lElmReUHDeJkAtAPPY Vee8nskBJqe/5vVtMIAuzy4PJ0smbMwYfosPHdMxCDaWtghXVMFFv22oDD+a6ZEn V0ctu4IUxYcji1tJdkOQp4nhXiafRlmG1esKg/X9hiyKPU9ZRqBNwvw= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Date:From:To:Subject:Message-ID:User-Agent:MIME-Version:Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=g1EueXs9uS9ezts6R59M46muTuDKQCdGqiV9/efUlyURL+m6LcGsRTwhihCsRN h7wfGxo/S1HreyYZh9pthijzfrn6aB0rDyGwAPxcU7mMoNRPsgXbi/1YEZu7PlPm LNxG2VzCH1nLqXom47qP7ixMxH7KxLosQIqWY+I76wjoc=; Received: (qmail 14784 invoked by alias); 13 Mar 2012 15:54:25 -0000 Received: (qmail 14775 invoked by uid 22791); 13 Mar 2012 15:54:23 -0000 X-SWARE-Spam-Status: No, hits=-5.7 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, TW_TM, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from cantor2.suse.de (HELO mx2.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 13 Mar 2012 15:54:09 +0000 Received: from relay1.suse.de (unknown [195.135.220.254]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx2.suse.de (Postfix) with ESMTP id 628CEA3D51 for ; Tue, 13 Mar 2012 16:54:08 +0100 (CET) Date: Tue, 13 Mar 2012 16:54:08 +0100 (CET) From: Richard Guenther To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix PR52578 Message-ID: User-Agent: Alpine 2.00 (LNX 1167 2008-08-23) MIME-Version: 1.0 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 This makes sure we fold conversion chains where the outermost conversion is a sign-change only and that we associate expressions in more cases when two variables appear but they have equal value. Bootstrapped on x86_64-unknown-linux-gnu, testing in progress. Richard. 2012-03-13 Richard Guenther PR middle-end/52578 * fold-const.c (fold_unary_loc): Fold (T1)(T2)x to (T1)x if the outermost conversion is a sign-change only. (fold_binary_loc): Disregard widening and sign-changing conversions when we determine if two variables are equal for reassociation. * tree-ssa-forwprop.c (combine_conversions): Fold (T1)(T2)x to (T1)x if the outermost conversion is a sign-change only. * gcc.dg/pr52578.c: New testcase. Index: gcc/fold-const.c =================================================================== --- gcc/fold-const.c (revision 185322) +++ gcc/fold-const.c (working copy) @@ -7843,10 +7843,13 @@ fold_unary_loc (location_t loc, enum tre return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0)); /* If we have a sign-extension of a zero-extended value, we can - replace that by a single zero-extension. */ + replace that by a single zero-extension. Likewise if the + final conversion does not change precision we can drop the + intermediate conversion. */ if (inside_int && inter_int && final_int - && inside_prec < inter_prec && inter_prec < final_prec - && inside_unsignedp && !inter_unsignedp) + && ((inside_prec < inter_prec && inter_prec < final_prec + && inside_unsignedp && !inter_unsignedp) + || final_prec == inter_prec)) return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0)); /* Two conversions in a row are not needed unless: @@ -10335,10 +10341,21 @@ fold_binary_loc (location_t loc, if (TREE_CODE (tmp0) == NEGATE_EXPR) tmp0 = TREE_OPERAND (tmp0, 0); + if (CONVERT_EXPR_P (tmp0) + && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0))) + && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0))) + <= TYPE_PRECISION (type))) + tmp0 = TREE_OPERAND (tmp0, 0); if (TREE_CODE (tmp1) == NEGATE_EXPR) tmp1 = TREE_OPERAND (tmp1, 0); + if (CONVERT_EXPR_P (tmp1) + && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0))) + && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0))) + <= TYPE_PRECISION (type))) + tmp1 = TREE_OPERAND (tmp1, 0); /* The only case we can still associate with two variables - is if they are the same, modulo negation. */ + is if they are the same, modulo negation and bit-pattern + preserving conversions. */ if (!operand_equal_p (tmp0, tmp1, 0)) ok = false; } Index: gcc/tree-ssa-forwprop.c =================================================================== --- gcc/tree-ssa-forwprop.c (revision 185322) +++ gcc/tree-ssa-forwprop.c (working copy) @@ -2285,10 +2285,13 @@ combine_conversions (gimple_stmt_iterato } /* If we have a sign-extension of a zero-extended value, we can - replace that by a single zero-extension. */ + replace that by a single zero-extension. Likewise if the + final conversion does not change precision we can drop the + intermediate conversion. */ if (inside_int && inter_int && final_int - && inside_prec < inter_prec && inter_prec < final_prec - && inside_unsignedp && !inter_unsignedp) + && ((inside_prec < inter_prec && inter_prec < final_prec + && inside_unsignedp && !inter_unsignedp) + || final_prec == inter_prec)) { gimple_assign_set_rhs1 (stmt, defop0); update_stmt (stmt); Index: gcc/testsuite/gcc.dg/pr52578.c =================================================================== --- gcc/testsuite/gcc.dg/pr52578.c (revision 0) +++ gcc/testsuite/gcc.dg/pr52578.c (revision 0) @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-fdump-tree-original" } */ + +long bar (long i) +{ + return (long)((unsigned long)i + 2) - (long)i; +} +long foo (int i) +{ + return (long)((unsigned long)i + 2) - (long)i; +} + +/* { dg-final { scan-tree-dump-times "return 2;" 2 "original" } } */ +/* { dg-final { cleanup-tree-dump "original" } } */