From patchwork Thu Jan 6 00:11:34 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 77648 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 423B9B7043 for ; Thu, 6 Jan 2011 11:12:01 +1100 (EST) Received: (qmail 6513 invoked by alias); 6 Jan 2011 00:11:59 -0000 Received: (qmail 6495 invoked by uid 22791); 6 Jan 2011 00:11:54 -0000 X-SWARE-Spam-Status: No, hits=-6.3 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 06 Jan 2011 00:11:49 +0000 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id p060BdVt015509 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 5 Jan 2011 19:11:39 -0500 Received: from tyan-ft48-01.lab.bos.redhat.com (tyan-ft48-01.lab.bos.redhat.com [10.16.42.4]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p060Bcsc027172 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 5 Jan 2011 19:11:39 -0500 Received: from tyan-ft48-01.lab.bos.redhat.com (localhost.localdomain [127.0.0.1]) by tyan-ft48-01.lab.bos.redhat.com (8.14.4/8.14.4) with ESMTP id p060Bchw020793; Thu, 6 Jan 2011 01:11:38 +0100 Received: (from jakub@localhost) by tyan-ft48-01.lab.bos.redhat.com (8.14.4/8.14.4/Submit) id p060BYDE020792; Thu, 6 Jan 2011 01:11:34 +0100 Date: Thu, 6 Jan 2011 01:11:34 +0100 From: Jakub Jelinek To: "Joseph S. Myers" Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix complex -> complex C conversions (PR c/47150) Message-ID: <20110106001133.GO16156@tyan-ft48-01.lab.bos.redhat.com> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) 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! As discussed in the PR, convert for COMPLEX_TYPE calls convert_to_complex (a generic FE function), which calls save_expr. This is sometimes undesirable for the C (and ObjC) FEs, where c_save_expr needs to be called instead to fully fold the argument. Given that we are in stage4, this patch just duplicates part of the convert_to_complex in convert so that it can call c_save_expr. c_save_expr can't be called always, as then a couple of tests in the testsuite crash (when the convert is called from convert_to_assignment, i.e. after it is already fully folded, save_expr needs to be called instead). Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 4.5 after a while? 2011-01-05 Jakub Jelinek PR c/47150 * c-convert.c (convert): When converting a complex expression other than COMPLEX_EXPR to a different complex type, ensure c_save_expr is called instead of save_expr, unless in_late_binary_op. * c-typeck.c (convert_for_assignment): Set in_late_binary_op also when converting COMPLEX_TYPE. * gcc.c-torture/compile/pr47150.c: New test. Jakub --- gcc/c-convert.c.jj 2010-12-02 11:51:32.000000000 +0100 +++ gcc/c-convert.c 2011-01-05 14:09:02.000000000 +0100 @@ -130,6 +130,32 @@ convert (tree type, tree expr) goto maybe_fold; case COMPLEX_TYPE: + /* If converting from COMPLEX_TYPE to a different COMPLEX_TYPE + and e is not COMPLEX_EXPR, convert_to_complex uses save_expr, + but for the C FE c_save_expr needs to be called instead. */ + if (TREE_CODE (TREE_TYPE (e)) == COMPLEX_TYPE) + { + tree subtype = TREE_TYPE (type); + tree elt_type = TREE_TYPE (TREE_TYPE (e)); + + if (TYPE_MAIN_VARIANT (elt_type) != TYPE_MAIN_VARIANT (subtype) + && TREE_CODE (e) != COMPLEX_EXPR) + { + if (in_late_binary_op) + e = save_expr (e); + else + e = c_save_expr (e); + ret + = fold_build2 (COMPLEX_EXPR, type, + convert (subtype, + fold_build1 (REALPART_EXPR, + elt_type, e)), + convert (subtype, + fold_build1 (IMAGPART_EXPR, + elt_type, e))); + goto maybe_fold; + } + } ret = convert_to_complex (type, e); goto maybe_fold; --- gcc/c-typeck.c.jj 2010-12-14 08:11:39.000000000 +0100 +++ gcc/c-typeck.c 2011-01-05 13:51:54.000000000 +0100 @@ -5272,10 +5272,10 @@ convert_for_assignment (location_t locat { tree ret; bool save = in_late_binary_op; - if (codel == BOOLEAN_TYPE) + if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE) in_late_binary_op = true; ret = convert_and_check (type, orig_rhs); - if (codel == BOOLEAN_TYPE) + if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE) in_late_binary_op = save; return ret; } --- gcc/testsuite/gcc.c-torture/compile/pr47150.c.jj 2011-01-04 19:41:17.000000000 +0100 +++ gcc/testsuite/gcc.c-torture/compile/pr47150.c 2011-01-04 19:40:59.000000000 +0100 @@ -0,0 +1,11 @@ +/* PR c/47150 */ + +float _Complex foo (float, float); + +void +bar () +{ + float w = 2; + float _Complex b; + b = 0.5 * (foo (0, w) + foo (1, w) / w); +}