From patchwork Wed May 25 07:05:53 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 97303 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 CFB20B6F83 for ; Wed, 25 May 2011 17:06:18 +1000 (EST) Received: (qmail 22232 invoked by alias); 25 May 2011 07:06:13 -0000 Received: (qmail 22209 invoked by uid 22791); 25 May 2011 07:06:11 -0000 X-SWARE-Spam-Status: No, hits=-6.5 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; Wed, 25 May 2011 07:05:55 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p4P75tfW017784 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 25 May 2011 03:05:55 -0400 Received: from tyan-ft48-01.lab.bos.redhat.com (tyan-ft48-01.lab.bos.redhat.com [10.16.42.4]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p4P75sP3005188 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 25 May 2011 03:05:55 -0400 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 p4P75rVc015028 for ; Wed, 25 May 2011 09:05:54 +0200 Received: (from jakub@localhost) by tyan-ft48-01.lab.bos.redhat.com (8.14.4/8.14.4/Submit) id p4P75r6d015026 for gcc-patches@gcc.gnu.org; Wed, 25 May 2011 09:05:53 +0200 Date: Wed, 25 May 2011 09:05:53 +0200 From: Jakub Jelinek To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix cxx_eval_bit_field_ref (PR c++/49136) Message-ID: <20110525070553.GJ17079@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! optimize_bit_field_compare during folding can create BIT_FIELD_REFs that reference more than a single bitfield, then mask the right bits from it. The following patch changes cxx_eval_bit_field_ref to be able to read the multiple fields from the constructor. Bootstrapped/regtested on x86_64-linux and i686-linux, acked by Jason in bugzilla, committed to trunk/4.6. 2011-05-25 Jakub Jelinek PR c++/49136 * semantics.c (cxx_eval_bit_field_ref): Handle the case when BIT_FIELD_REF doesn't cover only a single field. * g++.dg/cpp0x/constexpr-bitfield2.C: New test. * g++.dg/cpp0x/constexpr-bitfield3.C: New test. Jakub --- gcc/cp/semantics.c.jj 2011-05-20 08:14:06.000000000 +0200 +++ gcc/cp/semantics.c 2011-05-24 18:57:00.000000000 +0200 @@ -6442,6 +6442,9 @@ cxx_eval_bit_field_ref (const constexpr_ bool *non_constant_p) { tree orig_whole = TREE_OPERAND (t, 0); + tree retval, fldval, utype, mask; + bool fld_seen = false; + HOST_WIDE_INT istart, isize; tree whole = cxx_eval_constant_expression (call, orig_whole, allow_non_constant, addr, non_constant_p); @@ -6462,12 +6465,47 @@ cxx_eval_bit_field_ref (const constexpr_ return t; start = TREE_OPERAND (t, 2); + istart = tree_low_cst (start, 0); + isize = tree_low_cst (TREE_OPERAND (t, 1), 0); + utype = TREE_TYPE (t); + if (!TYPE_UNSIGNED (utype)) + utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1); + retval = build_int_cst (utype, 0); FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) { - if (bit_position (field) == start) + tree bitpos = bit_position (field); + if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1)) return value; + if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE + && TREE_CODE (value) == INTEGER_CST + && host_integerp (bitpos, 0) + && host_integerp (DECL_SIZE (field), 0)) + { + HOST_WIDE_INT bit = tree_low_cst (bitpos, 0); + HOST_WIDE_INT sz = tree_low_cst (DECL_SIZE (field), 0); + HOST_WIDE_INT shift; + if (bit >= istart && bit + sz <= istart + isize) + { + fldval = fold_convert (utype, value); + mask = build_int_cst_type (utype, -1); + mask = fold_build2 (LSHIFT_EXPR, utype, mask, + size_int (TYPE_PRECISION (utype) - sz)); + mask = fold_build2 (RSHIFT_EXPR, utype, mask, + size_int (TYPE_PRECISION (utype) - sz)); + fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask); + shift = bit - istart; + if (BYTES_BIG_ENDIAN) + shift = TYPE_PRECISION (utype) - shift - sz; + fldval = fold_build2 (LSHIFT_EXPR, utype, fldval, + size_int (shift)); + retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval); + fld_seen = true; + } + } } - gcc_unreachable(); + if (fld_seen) + return fold_convert (TREE_TYPE (t), retval); + gcc_unreachable (); return error_mark_node; } --- gcc/testsuite/g++.dg/cpp0x/constexpr-bitfield2.C.jj 2011-05-24 14:37:39.000000000 +0200 +++ gcc/testsuite/g++.dg/cpp0x/constexpr-bitfield2.C 2011-05-24 14:36:43.000000000 +0200 @@ -0,0 +1,19 @@ +// PR c++/49136 +// { dg-do compile } +// { dg-options "-std=c++0x" } + +struct day +{ + unsigned d : 5; + unsigned n : 3; + constexpr explicit day (int dd) : d(dd), n(7) {} +}; + +struct date { + int d; + constexpr date (day dd) : d(dd.n != 7 ? 7 : dd.d) {} +}; + +constexpr day d(0); +constexpr date dt(d); +static_assert (dt.d == 0, "Error"); --- gcc/testsuite/g++.dg/cpp0x/constexpr-bitfield3.C.jj 2011-05-24 14:37:43.000000000 +0200 +++ gcc/testsuite/g++.dg/cpp0x/constexpr-bitfield3.C 2011-05-24 14:43:40.000000000 +0200 @@ -0,0 +1,33 @@ +// PR c++/49136 +// { dg-do compile } +// { dg-options "-std=c++0x" } + +struct S +{ + unsigned : 1; unsigned s : 27; unsigned : 4; + constexpr S (unsigned int x) : s(x) {} +}; + +template +struct T +{ + unsigned int t; + constexpr T (S s) : t(s.s != 7 ? 0 : s.s) {} + constexpr T (S s, S s2) : t(s.s != s2.s ? 0 : s.s) {} +}; + +constexpr S s (7), s2 (7); +constexpr T t (s), t2 (s, s2); +static_assert (t.t == 7, "Error"); +static_assert (t2.t == 7, "Error"); + +struct U +{ + int a : 1; int s : 1; + constexpr U (int x, int y) : a (x), s (y) {} +}; + +constexpr U u (0, -1), u2 (-1, -1); +constexpr T t3 (u), t4 (u, u2); +static_assert (t3.t == 0, "Error"); +static_assert (t4.t == -1, "Error");