From patchwork Mon Jul 18 12:04:24 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 105252 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (unknown [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 842A0B71BF for ; Mon, 18 Jul 2011 22:07:41 +1000 (EST) Received: (qmail 7871 invoked by alias); 18 Jul 2011 12:04:42 -0000 Received: (qmail 7854 invoked by uid 22791); 18 Jul 2011 12:04:41 -0000 X-SWARE-Spam-Status: No, hits=-3.9 required=5.0 tests=AWL, BAYES_00, 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; Mon, 18 Jul 2011 12:04:25 +0000 Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.221.2]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx2.suse.de (Postfix) with ESMTP id 39FA88980B for ; Mon, 18 Jul 2011 14:04:24 +0200 (CEST) Date: Mon, 18 Jul 2011 14:04:24 +0200 (CEST) From: Richard Guenther To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix BIT_NOT_EXPR expansion with bitfields 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 We currently fail to handle bitfield precision BIT_NOT_EXPRs properly and miscompile the testcase below. Fixed by properly truncating to bitfield precision if required. Do that by expanding the NOT to a proper XOR instead of doing the truncation via a AND though. Bootstrap and regtest pending on x86_64-unknown-linux-gnu. Richard. 2011-07-18 Richard Guenther * expr.c (expand_expr_real_2): Properly truncate the BIT_NOT_EXPR expansion result to bitfield precision if required. * gcc.dg/torture/20110718-1.c: New testcase. Index: gcc/expr.c =================================================================== --- gcc/expr.c (revision 176386) +++ gcc/expr.c (working copy) @@ -8037,7 +8037,15 @@ expand_expr_real_2 (sepops ops, rtx targ VOIDmode, EXPAND_NORMAL); if (modifier == EXPAND_STACK_PARM) target = 0; - temp = expand_unop (mode, one_cmpl_optab, op0, target, 1); + /* In case we have to reduce the result to bitfield precision + expand this as XOR with a proper constant instead. */ + if (reduce_bit_field) + temp = expand_binop (mode, xor_optab, op0, + immed_double_int_const + (double_int_mask (TYPE_PRECISION (type)), mode), + target, 1, OPTAB_LIB_WIDEN); + else + temp = expand_unop (mode, one_cmpl_optab, op0, target, 1); gcc_assert (temp); return temp; Index: gcc/testsuite/gcc.dg/torture/20110718-1.c =================================================================== --- gcc/testsuite/gcc.dg/torture/20110718-1.c (revision 0) +++ gcc/testsuite/gcc.dg/torture/20110718-1.c (revision 0) @@ -0,0 +1,17 @@ +/* { dg-do run } */ + +extern void abort (void); +struct X { unsigned long i : 33; }; +unsigned long __attribute__((noinline)) +foo (struct X *p) +{ + return ~p->i; +} +int main() +{ + struct X x; + x.i = -1; + if (foo (&x) != 0) + abort (); + return 0; +}