From patchwork Wed Apr 3 07:40:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 87955 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 A3AF1384772F for ; Wed, 3 Apr 2024 07:41:37 +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 373AC3847700 for ; Wed, 3 Apr 2024 07:41:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 373AC3847700 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 373AC3847700 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=1712130068; cv=none; b=Ht+/vtMq1D6Gclz/yHcOr0c8pZjCXR5m59nWutvjDkcx66/O7ogp00DMxFWz5awpslBtM2qbOw4BMyCFfC6Ii84ohRxECqrXsavHMD5NrlPe7b8k6V2di2XdAon8+tHzTTlAtoh+EfG9Z8edHY5HFOgr1/XLBHs7O+0IETEEJuk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712130068; c=relaxed/simple; bh=nT8ODxkZTwAfxrP4mGL0OSC5yre6gq07VydSwc/0//k=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=j39hEGddci4TSrpEOb6rNfWBrSU4MVLhBKp4NMjq833ZH3yPKL1oQCO8aPorZpfLEXSKnyIiTJkMvyQMdf8kpA5BvGC/gTSk51nXfL5xoXHMsVF424JEJw1bbsKKw40XYEemC129YGxHEO2f1qGJUoUFkaEE1hd6csCb8/kmmlw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712130064; 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=Z6eQoo/hKzo0CwSb7kDoHx+v+DJKYRZBn6XrymYgqSI=; b=iw0qxBNlj8ad6QNiLsdNwmpuRG79bLnIa5FAxbrXoBzBconUbLfSAdgX9xjFZiu8X2+uiH rOcNvW8vbonwBfFrfnetb4KeEKNgX0OV6D/lwkWOqeRTuwtxisCfMEAH7fCS4rkVzSEylj 6lAru1EqUut+FohMuoZuC/vfbvfB1Pw= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-458-KKae9_YdNkCXES9yH2ac6A-1; Wed, 03 Apr 2024 03:41:03 -0400 X-MC-Unique: KKae9_YdNkCXES9yH2ac6A-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 A6A131C54466; Wed, 3 Apr 2024 07:41:02 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.14]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5DD25C15773; Wed, 3 Apr 2024 07:41:02 +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 4337euHh1844720 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 3 Apr 2024 09:40:56 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 4337et8J1844719; Wed, 3 Apr 2024 09:40:55 +0200 Date: Wed, 3 Apr 2024 09:40:55 +0200 From: Jakub Jelinek To: Richard Biener , Jeff Law , Eric Botcazou Cc: gcc-patches@gcc.gnu.org, Roger Sayle Subject: [PATCH] expr: Fix up emit_push_insn [PR114552] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=no 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+patchwork=sourceware.org@gcc.gnu.org Hi! r13-990 added optimizations in multiple spots to optimize during expansion storing of constant initializers into targets. In the load_register_parameters and expand_expr_real_1 cases, it checks it has a tree as the source and so knows we are reading that whole decl's value, so the code is fine as is, but in the emit_push_insn case it checks for a MEM from which something is pushed and checks for SYMBOL_REF as the MEM's address, but still assumes the whole object is copied, which as the following testcase shows might not always be the case. In the testcase, k is 6 bytes, then 2 bytes of padding, then another 4 bytes, while the emit_push_insn wants to store just the 6 bytes. The following patch simply verifies it is the whole initializer that is being stored, I think that is best thing to do so late in GCC 14 cycle as well for backporting. For GCC 15, perhaps the code could stop requiring it must be at offset zero, nor that the size is a subset, but could use get_symbol_constant_value/fold_ctor_reference gimple-fold APIs to actually extract just part of the initializer if we e.g. push just some subset (of course, still verify that it is a subset). For sizes which are power of two bytes and we have some integer modes, we could use as type for fold_ctor_reference corresponding integral types, otherwise dunno, punt or use some structure (e.g. try to find one in the initializer?), whatever. But even in the other spots it could perhaps handle loading of COMPONENT_REFs or MEM_REFs from the .rodata vars. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-04-02 Jakub Jelinek PR middle-end/114552 * expr.cc (emit_push_insn): Only use store_constructor for immediate_const_ctor_p if int_expr_size matches size. * gcc.c-torture/execute/pr114552.c: New test. Jakub --- gcc/expr.cc.jj 2024-03-15 10:10:51.209237835 +0100 +++ gcc/expr.cc 2024-04-02 16:01:39.566744302 +0200 @@ -5466,6 +5466,7 @@ emit_push_insn (rtx x, machine_mode mode /* If source is a constant VAR_DECL with a simple constructor, store the constructor to the stack instead of moving it. */ const_tree decl; + HOST_WIDE_INT sz; if (partial == 0 && MEM_P (xinner) && SYMBOL_REF_P (XEXP (xinner, 0)) @@ -5473,9 +5474,11 @@ emit_push_insn (rtx x, machine_mode mode && VAR_P (decl) && TREE_READONLY (decl) && !TREE_SIDE_EFFECTS (decl) - && immediate_const_ctor_p (DECL_INITIAL (decl), 2)) - store_constructor (DECL_INITIAL (decl), target, 0, - int_expr_size (DECL_INITIAL (decl)), false); + && immediate_const_ctor_p (DECL_INITIAL (decl), 2) + && (sz = int_expr_size (DECL_INITIAL (decl))) > 0 + && CONST_INT_P (size) + && INTVAL (size) == sz) + store_constructor (DECL_INITIAL (decl), target, 0, sz, false); else emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM); } --- gcc/testsuite/gcc.c-torture/execute/pr114552.c.jj 2024-04-02 16:08:12.959366793 +0200 +++ gcc/testsuite/gcc.c-torture/execute/pr114552.c 2024-04-02 16:03:49.829963659 +0200 @@ -0,0 +1,24 @@ +/* PR middle-end/114552 */ + +struct __attribute__((packed)) S { short b; int c; }; +struct T { struct S b; int e; }; +static const struct T k = { { 1, 0 }, 0 }; + +__attribute__((noinline)) void +foo (void) +{ + asm volatile ("" : : : "memory"); +} + +__attribute__((noinline)) void +bar (struct S n) +{ + foo (); +} + +int +main () +{ + bar (k.b); + return 0; +}