From patchwork Mon Jan 17 08:56:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 50096 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D08CB3858415 for ; Mon, 17 Jan 2022 08:57:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D08CB3858415 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1642409851; bh=Jp6f2dCVh4VH490LGXVy30mzsSRvYqbk44xOaW6WADw=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=CiM9bHDuHqf06nUOmrxKbg2zGdAzb59WQNDIoPjAjo4IjsvIjHgnBKmS7MEF6hHE8 XuSkhCEUph5JbqTLtBj2gtAOXWEFj97jnv+Sx+dEW8GZcRYgu6HEcVU99w24LcwzpI p0brtoTClKZ6fzOImMpMApeh+iFyehuuoBRLGS8k= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 7D5B03858402 for ; Mon, 17 Jan 2022 08:57:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7D5B03858402 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-207-S2pGzii4PFuah_6iJ68bhQ-1; Mon, 17 Jan 2022 03:57:00 -0500 X-MC-Unique: S2pGzii4PFuah_6iJ68bhQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BF3E510151E0 for ; Mon, 17 Jan 2022 08:56:59 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.40.192.74]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 55D3D6F94C; Mon, 17 Jan 2022 08:56:53 +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 20H8uprL1496731 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 17 Jan 2022 09:56:51 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 20H8upkj1496730; Mon, 17 Jan 2022 09:56:51 +0100 Date: Mon, 17 Jan 2022 09:56:51 +0100 To: Jason Merrill Subject: [PATCH] c++: Fix cp_genericize_target_expr for TARGET_EXPRs created for global initialization [PR104031] Message-ID: <20220117085651.GF2646553@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-5.5 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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hi! The following patch is miscompiled, cp_genericize_target_expr expects that for the constant part split_nonconstant_init will emit an INIT_EXPR that will initialize it, but that doesn't happen and instead we get DECL_INITIAL on the TARGET_EXPR_SLOT that isn't initialized anywhere in the IL. The problem is that the TARGET_EXPR has been created while current_function_decl was NULL, it is inside a global var initializer. That means the build_local_temp created VAR_DECL has NULL DECL_CONTEXT. Later on when genericizing the ssdf (current_function_decl is already non-NULL), the new cp_genericize_target_expr is called and during split_nonconstant_init it checks is_local_temp, but that due to the NULL DECL_CONTEXT returns false. DECL_CONTEXT is set only later on during gimplification. The following patch fixes it by setting DECL_CONTEXT also inside of cp_genericize_target_expr, which fixes the testcase. But if there are better spots to do that, please let me know... Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2022-01-17 Jakub Jelinek PR c++/104031 * cp-gimplify.c (cp_genericize_target_expr): Set DECL_CONTEXT of TARGET_EXPR_SLOT to current_function_decl if it was NULL. * g++.dg/cpp1y/pr104031.C: New test. Jakub --- gcc/cp/cp-gimplify.c.jj 2022-01-14 12:09:13.003250834 +0100 +++ gcc/cp/cp-gimplify.c 2022-01-16 21:34:52.300937996 +0100 @@ -930,6 +930,11 @@ cp_genericize_init_expr (tree *stmt_p) static void cp_genericize_target_expr (tree *stmt_p) { + /* If TARGET_EXPR is created for some global var initializer, the slot + will have NULL and so is_local_temp will return false for it. If + this is a ssdf, set DECL_CONTEXT now. */ + if (DECL_CONTEXT (TARGET_EXPR_SLOT (*stmt_p)) == NULL_TREE) + DECL_CONTEXT (TARGET_EXPR_SLOT (*stmt_p)) = current_function_decl; cp_genericize_init (&TARGET_EXPR_INITIAL (*stmt_p), TARGET_EXPR_INITIAL (*stmt_p), TARGET_EXPR_SLOT (*stmt_p)); --- gcc/testsuite/g++.dg/cpp1y/pr104031.C.jj 2022-01-16 21:30:14.977838079 +0100 +++ gcc/testsuite/g++.dg/cpp1y/pr104031.C 2022-01-16 21:30:46.028401294 +0100 @@ -0,0 +1,23 @@ +// PR c++/104031 +// { dg-do run { target c++14 } } +// { dg-options "-O2" } + +struct A { + A () {} + ~A () {} +}; +struct B { + A a; + int b = 0; +}; +struct C +{ + [[gnu::noipa]] + C (B x) { if (x.b != 42) __builtin_abort (); } +}; +static C c ({ .a = A{}, .b = 42 }); + +int +main () +{ +}