From patchwork Thu Apr 4 07:36:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1919637 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=LVf44n0h; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V9D2s0Hxhz1yZH for ; Thu, 4 Apr 2024 18:36:53 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3BB033858C50 for ; Thu, 4 Apr 2024 07:36:51 +0000 (GMT) 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 ESMTPS id 19B683858D34 for ; Thu, 4 Apr 2024 07:36:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 19B683858D34 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 19B683858D34 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712216194; cv=none; b=GVTjePbBWqQsR0bOPt3TN3upmtNDOUPwFI0aOaNoJ+Ny4dL4yqCJG2OjYaNwNtR1MsWTkH8JiIbmgOKM+284vmgQJPAxz7rDHaPCpNgw9UqrdHG0RIZXDh4QH3aWmAv+BaCBx+ngdu6vzDl4mi6QrohgucXbvhRBjcH02udsF7g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712216194; c=relaxed/simple; bh=2Nop1T9wTBOCOI7QfMiHpCu1RNLfF8vYL9kx8akuRZ0=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=Cl1L2cUo7LzWhG2Hcl+yxXDyK2rWvTIV6Y92+ychQO1wi59OA4nj6zw3prYELAR2rERCCH3f8tQmn1aYICrOVEkg0roD4tO0maryO8vH0K3rTTyTkNZZFirrMEVxOeiufRGtFH7Qx2Cr0r50qJyUZI+6cdcaFiZWwEETDiRA/Yk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712216191; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=F82YcBjkbytBBAZhnnv/pJK95oAaNPO2pVxBMaYxWvw=; b=LVf44n0hE6R0kLCSJ9GlBCRKlwqcg7BLAxCwjw8v4U8Xpu5XIV6WOjrKzmPJMK5B1gFtJT a8REudPAM3Hg8rZ/bAQC7hK1isHtKPLrFbphvo+Etu1VnkSOMN8FIKnoldrRVod46dd79K GKOerb6JCesbK3uerMBjqhlCwFopA1Q= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-624-Kl1BCwNVOLKGfjZrMoh40w-1; Thu, 04 Apr 2024 03:36:30 -0400 X-MC-Unique: Kl1BCwNVOLKGfjZrMoh40w-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E6A3F8007A3; Thu, 4 Apr 2024 07:36:29 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.14]) by smtp.corp.redhat.com (Postfix) with ESMTPS id ACCB3492BC6; Thu, 4 Apr 2024 07:36:29 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 4347aNWh781011 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 4 Apr 2024 09:36:24 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 4347aN73781010; Thu, 4 Apr 2024 09:36:23 +0200 Date: Thu, 4 Apr 2024 09:36:23 +0200 From: Jakub Jelinek To: Richard Biener , Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] fold-const: Handle NON_LVALUE_EXPR in native_encode_initializer [PR114537] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! The following testcase is incorrectly rejected. The problem is that for bit-fields native_encode_initializer expects the corresponding CONSTRUCTOR elt value must be INTEGER_CST, but that isn't the case here, it is wrapped into NON_LVALUE_EXPR by maybe_wrap_with_location. We could STRIP_ANY_LOCATION_WRAPPER as well, but as all we are looking for is INTEGER_CST inside, just looking through NON_LVALUE_EXPR seems easier. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-04-04 Jakub Jelinek PR c++/114537 * fold-const.cc (native_encode_initializer): Look through NON_LVALUE_EXPR if val is INTEGER_CST. * g++.dg/cpp2a/bit-cast16.C: New test. Jakub --- gcc/fold-const.cc.jj 2024-03-26 11:21:31.996860739 +0100 +++ gcc/fold-const.cc 2024-04-03 16:59:05.747297410 +0200 @@ -8601,6 +8601,8 @@ native_encode_initializer (tree init, un if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) return 0; + if (TREE_CODE (val) == NON_LVALUE_EXPR) + val = TREE_OPERAND (val, 0); if (TREE_CODE (val) != INTEGER_CST) return 0; --- gcc/testsuite/g++.dg/cpp2a/bit-cast16.C.jj 2024-04-03 17:06:46.720974426 +0200 +++ gcc/testsuite/g++.dg/cpp2a/bit-cast16.C 2024-04-03 17:06:40.233063410 +0200 @@ -0,0 +1,16 @@ +// PR c++/114537 +// { dg-do compile { target c++20 } } + +namespace std { +template +constexpr T +bit_cast (const F& f) noexcept +{ + return __builtin_bit_cast (T, f); +} +} + +struct A { signed char b : 1 = 0; signed char c : 7 = 0; }; +struct D { unsigned char e; }; +constexpr unsigned char f = std::bit_cast (A{}).e; +static_assert (f == 0);