From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3625 invoked by alias); 11 Aug 2014 11:25:28 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 3557 invoked by uid 89); 11 Aug 2014 11:25:27 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-lb0-f173.google.com Received: from mail-lb0-f173.google.com (HELO mail-lb0-f173.google.com) (209.85.217.173) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Mon, 11 Aug 2014 11:25:25 +0000 Received: by mail-lb0-f173.google.com with SMTP id u10so3835441lbd.18 for ; Mon, 11 Aug 2014 04:25:22 -0700 (PDT) X-Received: by 10.112.52.225 with SMTP id w1mr36364570lbo.44.1407756322513; Mon, 11 Aug 2014 04:25:22 -0700 (PDT) Received: from cascade (h109n3-u-a31.ias.bredband.telia.com. [213.65.120.109]) by mx.google.com with ESMTPSA id al8sm12186109lbc.11.2014.08.11.04.25.20 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 11 Aug 2014 04:25:21 -0700 (PDT) From: Mikael Pettersson X-Google-Original-From: "Mikael Pettersson" Received: by cascade (sSMTP sendmail emulation); Mon, 11 Aug 2014 13:25:19 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <21480.43039.502462.244848@gargle.gargle.HOWL> Date: Mon, 11 Aug 2014 11:25:00 -0000 To: "Thomas Preud'homme" Cc: Subject: Re: [PATCH] Fix incorrect folding of bitfield in a union on big endian target In-Reply-To: <000301cfb536$ea32c150$be9843f0$@arm.com> References: <000301cfb536$ea32c150$be9843f0$@arm.com> X-SW-Source: 2014-08/txt/msg01007.txt.bz2 Thomas Preud'homme writes: > In the code dealing with folding of structure and union initialization, there is a > check that the size of the constructor is the same as the field being read. > However, in the case of bitfield this test can be wrong because it relies on > TYPE_SIZE to get the size of the field being read but TYPE_SIZE returns the > size of the enclosing integer in that case. This patch also check the size > parameter which contains the actual size of the field being read. > > The patch was tested by running the testsuite with three different builds of GCC: > 1) bootstrap of GCC on x86_64-linux-gnu > 2) arm-none-eabi cross compiler (defaulting to little endian) with testsuite > run under qemu emulqting a Cortex M4 > 3) arm-none-eabi cross compiler (defaulting to big endian, thanks to patch at [1]) > with testsuite run under qemu emulating a Cortex M3. > > [1] https://sourceware.org/ml/binutils/2014-08/msg00014.html > > No regression were observed on any of the tests. The ChangeLog is as follows: > > > 2014-08-11 Thomas Preud'homme > > * gimple-fold.c (fold_ctor_reference): Don't fold in presence of > bitfields, that is when size doesn't match the size of type or the > size of the constructor. > > > diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c > index 3dcb576..6270c34 100644 > --- a/gcc/gimple-fold.c > +++ b/gcc/gimple-fold.c > @@ -3099,7 +3099,9 @@ fold_ctor_reference (tree type, tree ctor, unsigned HOST_WIDE_INT offset, > if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset > /* VIEW_CONVERT_EXPR is defined only for matching sizes. */ > && operand_equal_p (TYPE_SIZE (type), > - TYPE_SIZE (TREE_TYPE (ctor)), 0)) > + TYPE_SIZE (TREE_TYPE (ctor)), 0) > + && !compare_tree_int (TYPE_SIZE (type), size) > + && !compare_tree_int (TYPE_SIZE (TREE_TYPE (ctor)), size)) > { > ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl); > ret = fold_unary (VIEW_CONVERT_EXPR, type, ret); > diff --git a/gcc/testsuite/gcc.c-torture/execute/bitfld-6.c b/gcc/testsuite/gcc.c-torture/execute/bitfld-6.c > new file mode 100644 > index 0000000..50927dc > --- /dev/null > +++ b/gcc/testsuite/gcc.c-torture/execute/bitfld-6.c > @@ -0,0 +1,23 @@ > +union U > +{ > + const int a; > + unsigned b : 20; > +}; > + > +static union U u = { 0x12345678 }; > + > +/* Constant folding used to fail to account for endianness when folding a > + union. */ > + > +int > +main (void) > +{ > +#ifdef __BYTE_ORDER__ > +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ > + return u.b - 0x45678; > +#else > + return u.b - 0x12345; > +#endif > +#endif > + return 0; > +} > > Is it ok for trunk? > > Best regards, > > Thomas > This test case also fails on powerpc64 (BE not LE), sparc64, and m68k, with all current gcc branches (4.10, 4.9, 4.8). On my powerpc64 box the failure started with gcc-4.6; gcc-4.5 back to gcc-3.4.6 seem Ok. Perhaps give this a proper PR and consider backporting? /Mikael