From patchwork Wed Jun 16 07:45:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1492789 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=fodVtZic; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4G4cgL6BFdz9sX2 for ; Wed, 16 Jun 2021 17:45:49 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0AB1B398797A for ; Wed, 16 Jun 2021 07:45:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0AB1B398797A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1623829546; bh=TR29Ut34BORmaPeP6Vvce+UMCAjEN260y5C9htHQbjM=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=fodVtZic8wLQ0S57xj/Eipm11sZv0AZsddKWX5wkWAxQrYlsgm/UPzzHnCjin816o EMBVyrPVGLqsLjOqshwddRd+u3V6Lmhtjm1KF9B7LDoci8BXWHQV2ygLxmyzm/7xFe 4JThxTfA9Esio0WfxRexgA6hSfO3fOsd4XAEFZ9A= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 7DF643894411 for ; Wed, 16 Jun 2021 07:45:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7DF643894411 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-176-gLc6BRZQN4-Bpmze5oNW_g-1; Wed, 16 Jun 2021 03:45:23 -0400 X-MC-Unique: gLc6BRZQN4-Bpmze5oNW_g-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 278AB19057AC; Wed, 16 Jun 2021 07:45:22 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-112-7.ams2.redhat.com [10.36.112.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B4F0960622; Wed, 16 Jun 2021 07:45:21 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 15G7jJ3a303283 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 16 Jun 2021 09:45:19 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 15G7jH4W303282; Wed, 16 Jun 2021 09:45:17 +0200 Date: Wed, 16 Jun 2021 09:45:17 +0200 To: Richard Biener Subject: [PATCH] stor-layout: Create DECL_BIT_FIELD_REPRESENTATIVE even for bitfields in unions [PR101062] Message-ID: <20210616074517.GW7746@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-5.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Hi! The following testcase is miscompiled on x86_64-linux, the bitfield store is implemented as a RMW 64-bit operation at d+24 when the d variable has size of only 28 bytes and scheduling moves in between the R and W part a store to a different variable that happens to be right after the d variable. The reason for this is that we weren't creating DECL_BIT_FIELD_REPRESENTATIVEs for bitfields in unions. The following patch does create them, but treats all such bitfields as if they were in a structure where the particular bitfield is the only field. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2021-06-16 Jakub Jelinek PR middle-end/101062 * stor-layout.c (finish_bitfield_representative): For fields in unions assume nextf is always NULL. (finish_bitfield_layout): Compute bit field representatives also in unions, but handle it as if each bitfield was the only field in the aggregate. * gcc.dg/pr101062.c: New test. Jakub --- gcc/stor-layout.c.jj 2021-03-30 18:11:52.537092233 +0200 +++ gcc/stor-layout.c 2021-06-15 10:58:59.244353965 +0200 @@ -2072,9 +2072,14 @@ finish_bitfield_representative (tree rep bitsize = (bitsize + BITS_PER_UNIT - 1) & ~(BITS_PER_UNIT - 1); /* Now nothing tells us how to pad out bitsize ... */ - nextf = DECL_CHAIN (field); - while (nextf && TREE_CODE (nextf) != FIELD_DECL) - nextf = DECL_CHAIN (nextf); + if (TREE_CODE (DECL_CONTEXT (field)) == RECORD_TYPE) + { + nextf = DECL_CHAIN (field); + while (nextf && TREE_CODE (nextf) != FIELD_DECL) + nextf = DECL_CHAIN (nextf); + } + else + nextf = NULL_TREE; if (nextf) { tree maxsize; @@ -2167,13 +2172,6 @@ finish_bitfield_layout (tree t) tree field, prev; tree repr = NULL_TREE; - /* Unions would be special, for the ease of type-punning optimizations - we could use the underlying type as hint for the representative - if the bitfield would fit and the representative would not exceed - the union in size. */ - if (TREE_CODE (t) != RECORD_TYPE) - return; - for (prev = NULL_TREE, field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) { @@ -2233,7 +2231,13 @@ finish_bitfield_layout (tree t) if (repr) DECL_BIT_FIELD_REPRESENTATIVE (field) = repr; - prev = field; + if (TREE_CODE (t) == RECORD_TYPE) + prev = field; + else if (repr) + { + finish_bitfield_representative (repr, field); + repr = NULL_TREE; + } } if (repr) --- gcc/testsuite/gcc.dg/pr101062.c.jj 2021-06-15 10:42:58.642919880 +0200 +++ gcc/testsuite/gcc.dg/pr101062.c 2021-06-15 10:42:40.897171191 +0200 @@ -0,0 +1,29 @@ +/* PR middle-end/101062 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fno-toplevel-reorder -frename-registers" } */ + +union U { signed b : 5; }; +int c; +volatile union U d[7] = { { 8 } }; +short e = 1; + +__attribute__((noipa)) void +foo () +{ + d[6].b = 0; + d[6].b = 0; + d[6].b = 0; + d[6].b = 0; + d[6].b = 0; + e = 0; + c = 0; +} + +int +main () +{ + foo (); + if (e != 0) + __builtin_abort (); + return 0; +}