From patchwork Fri Mar 1 08:34:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1906585 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=QUqNfaD0; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4TmLxs3ZjHz23fC for ; Fri, 1 Mar 2024 19:35:13 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 35902385840F for ; Fri, 1 Mar 2024 08:35:11 +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.129.124]) by sourceware.org (Postfix) with ESMTPS id D3F493858D39 for ; Fri, 1 Mar 2024 08:34:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D3F493858D39 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 D3F493858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709282076; cv=none; b=M4R2vuPGg6ZysGboHuBWzO44g47o60JXSacSLt0zdObKqyY2wOz2tXA/JCCPySzaXpS3bser9ykTC86hmYfQrXY8RKSj9Zb5H+z/KFmFEjcsx7NZHqUvZyTo9HsUBnTJhtLJlqMEggTuw/x6KhSY0vRabZ9joUV+y4WIhOrx8QI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709282076; c=relaxed/simple; bh=QNQj2fjCsBSJGG5WJ5tcgpG5ThF7EA/FRgFiX2RynFw=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=oFwq3j6ZcaOwrXXJdGbWTPqFxFsmZEpQ3wlQgiFSuhENQaIeJPrXMjD0fCqa/2qo52PuHX1nkn+gPB5F40UEM8IJuc9yWI2uT0BX0b1CtPcOxq4z9GhO4Rbg9cCqKCj36WQEfTT2kb9YiCSNPfd6dCx3fwrMv3oxYXdXJMox5n0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709282073; 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=yVYiEFo4VBL+CGFKcGsPvsWynJKtydJvqxD5wZLoyoM=; b=QUqNfaD0m+OcZlm2zF/YRq/Yea9Sl6IKGHzaokxFCQlXR4l1AvskvbmUdeam5B8r0sanqr klzCkAnGHtAN+/lKAdM7cDdaSTBZq75wWx75ukeR2v+jwDOBBAWVcjuujz5nwk0+akV1ej 0zncSjsL3db5EcQ7xblUdlSIPmqDmI4= 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-435-eKJdCkkWMFauWf6M_GBU7A-1; Fri, 01 Mar 2024 03:34:30 -0500 X-MC-Unique: eKJdCkkWMFauWf6M_GBU7A-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (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 91F8686EB28; Fri, 1 Mar 2024 08:34:30 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.226.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 52D8A4D90F; Fri, 1 Mar 2024 08:34:30 +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 4218YSfF506304 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 1 Mar 2024 09:34:28 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 4218YSW1506303; Fri, 1 Mar 2024 09:34:28 +0100 Date: Fri, 1 Mar 2024 09:34:28 +0100 From: Jakub Jelinek To: Richard Biener , "Joseph S. Myers" , Jeff Law Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] function: Fix another TYPE_NO_NAMED_ARGS_STDARG_P spot Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.0 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_H2, SCC_5_SHORT_WORD_LINES, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! When looking at PR114175 (although that bug seems to be now a riscv backend bug), I've noticed that for the TYPE_NO_NAMED_ARGS_STDARG_P functions which return value through hidden reference, like #include struct S { char a[64]; }; int n; struct S foo (...) { struct S s = {}; va_list ap; va_start (ap); for (int i = 0; i < n; ++i) if ((i & 1)) s.a[0] += va_arg (ap, double); else s.a[0] += va_arg (ap, int); va_end (ap); return s; } we were incorrectly calling assign_parms_setup_varargs twice, once at the start of the function and once in if (cfun->stdarg && !DECL_CHAIN (parm)) assign_parms_setup_varargs (&all, &data, false); where parm is the last and only "named" parameter. The first call, guarded with TYPE_NO_NAMED_ARGS_STDARG_P, was added in r13-3549 and is needed for int bar (...) etc. functions using va_start/va_arg/va_end, otherwise the FOR_EACH_VEC_ELT (fnargs, i, parm) in which the other call is will not iterate at all. But we shouldn't be doing that if we have the hidden return pointer. With the following patch on the above testcase with -O0 -std=c23 the assembly difference is: pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 pushq %rbx subq $192, %rsp .cfi_offset 3, -24 - movq %rdi, -192(%rbp) - movq %rsi, -184(%rbp) - movq %rdx, -176(%rbp) - movq %rcx, -168(%rbp) - movq %r8, -160(%rbp) - movq %r9, -152(%rbp) - testb %al, %al - je .L2 - movaps %xmm0, -144(%rbp) - movaps %xmm1, -128(%rbp) - movaps %xmm2, -112(%rbp) - movaps %xmm3, -96(%rbp) - movaps %xmm4, -80(%rbp) - movaps %xmm5, -64(%rbp) - movaps %xmm6, -48(%rbp) - movaps %xmm7, -32(%rbp) -.L2: movq %rdi, -312(%rbp) movq %rdi, -192(%rbp) movq %rsi, -184(%rbp) movq %rdx, -176(%rbp) movq %rcx, -168(%rbp) movq %r8, -160(%rbp) movq %r9, -152(%rbp) testb %al, %al - je .L13 + je .L12 movaps %xmm0, -144(%rbp) movaps %xmm1, -128(%rbp) movaps %xmm2, -112(%rbp) movaps %xmm3, -96(%rbp) movaps %xmm4, -80(%rbp) movaps %xmm5, -64(%rbp) movaps %xmm6, -48(%rbp) movaps %xmm7, -32(%rbp) -.L13: +.L12: plus some renumbering of labels later on which clearly shows that because of this bug, we were saving all the registers twice rather then once. With -O2 -std=c23 some of it is DCEd, but we still get subq $160, %rsp .cfi_def_cfa_offset 168 - testb %al, %al - je .L2 - movaps %xmm0, 24(%rsp) - movaps %xmm1, 40(%rsp) - movaps %xmm2, 56(%rsp) - movaps %xmm3, 72(%rsp) - movaps %xmm4, 88(%rsp) - movaps %xmm5, 104(%rsp) - movaps %xmm6, 120(%rsp) - movaps %xmm7, 136(%rsp) -.L2: movq %rdi, -24(%rsp) movq %rsi, -16(%rsp) movq %rdx, -8(%rsp) movq %rcx, (%rsp) movq %r8, 8(%rsp) movq %r9, 16(%rsp) testb %al, %al - je .L13 + je .L12 movaps %xmm0, 24(%rsp) movaps %xmm1, 40(%rsp) movaps %xmm2, 56(%rsp) movaps %xmm3, 72(%rsp) movaps %xmm4, 88(%rsp) movaps %xmm5, 104(%rsp) movaps %xmm6, 120(%rsp) movaps %xmm7, 136(%rsp) -.L13: +.L12: difference, i.e. this time not all, but the floating point args were conditionally all saved twice. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-03-01 Jakub Jelinek * function.cc (assign_parms): Only call assign_parms_setup_varargs early for TYPE_NO_NAMED_ARGS_STDARG_P functions if fnargs is empty. Jakub --- gcc/function.cc.jj 2024-01-12 13:47:20.834428745 +0100 +++ gcc/function.cc 2024-02-29 21:14:35.275889093 +0100 @@ -3650,7 +3650,8 @@ assign_parms (tree fndecl) assign_parms_initialize_all (&all); fnargs = assign_parms_augmented_arg_list (&all); - if (TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (fndecl))) + if (TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (fndecl)) + && fnargs.is_empty ()) { struct assign_parm_data_one data = {}; assign_parms_setup_varargs (&all, &data, false);