From patchwork Wed Sep 30 22:45:24 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleg Tolmatcev X-Patchwork-Id: 145059 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BB38A4BB58D1 for ; Wed, 30 Sep 2026 22:46:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BB38A4BB58D1 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=qBpGvaJ7 X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr2-x11.google.com (mail-wr2-x11.google.com [IPv6:2a00:1450:4864:30::11]) by sourceware.org (Postfix) with ESMTPS id 796874BB3BD9 for ; Wed, 30 Sep 2026 22:46:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 796874BB3BD9 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 796874BB3BD9 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:30::11 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790808362; cv=none; b=CTpQPQ4rlYOF/WepbkzNBsJpW4+ZNs+DlejqrrXSfYtokYscj0VsRJgPJOzHXNUE+Sns+6CsNhhTEYRD1XDaa/cAdFQbkTSRmEgrETFgoXX94peHqWdXfEZcG22Zzu7i6sjzb190RAIZfCYnzrGTjxakAEA5b9XjFEODycQbHf0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790808362; c=relaxed/simple; bh=LG9X4EOnTZZa3AAuaxiAQeJ1DN2PyHr33Nl5qBrkX0Y=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=E0dyW9F5IaAKcke3HndnSq5Y6kHqxF7jy9GERDFeO0Vt53wUiIDner/NFab7eOlE4VqbRUJ12jT6GcFoymnvXf4OS/t7+OehToq1BCe90Q4h18ahyBnmTSaSn8x5OXrSt6P2MpkrvgiIVvXl4R9NQoigiy1Rh9PYNVQ6NL51PNk= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=qBpGvaJ7 DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 796874BB3BD9 Received: by mail-wr2-x11.google.com with SMTP id ffacd0b85a97d-4843c3ee4cfso3018921f8f.2 for ; Wed, 30 Sep 2026 15:46:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790808361; x=1791413161; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=XwYUSVfbhitu6Cvb5JrxPRWRXl35194BjCQaUkIk14g=; b=qBpGvaJ7jtrPlPZ8zNAsnAnxaDFuoqZnxAwn+26QvqRIzJejVUlMkVPn9HCa+IO58K d+LBQg92CPd0S8kEnfParULRQIqF59rG2tMb3mpOMVzDvQy1yLbQyZF435J3cS+vr7O1 eG6SzR/U8zVNqs7C3hPWcg06JwqZxtu3eGCGcZQyyB03aiidRwnBBiHE92yL3LmSnEgO E3Oxbb8gO60fT9fpW0kqsKnJYpyKetmExYDuHwW/0PwV8GWWSbOUsTbN+DN8jGpz4c17 8FOORJKhTTqGGOG/oEbfQGjCfepCWRfRiM7i36cul9RV2kWbBqhiSlZB8ZNT9b0XGyxr TjbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790808361; x=1791413161; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=XwYUSVfbhitu6Cvb5JrxPRWRXl35194BjCQaUkIk14g=; b=ar9mM96ztSOOBeV2U71+nGaTSXd35AIsQisHFtMSrbgp2HSeB0IjMmW/WQdXmybr/K 4ZBF8PtC0kUrEtRBoJevS0fVFwJl7m+spPVaAx9XkE/c0Qn4Uv/QCLjxb6Izg1s6Hck+ IWKrkPgnmcwBzT1HG/et2Of6a9QMZEUIXI6hnXM0KGGMMvtM+dX53baEwCRZp9b44dx4 h5EYw839Wo31fLjdNJySGmCysLekgzjK4ekLG0b0GoX0QqcH8z+vcRyOwKXxuoGDozc2 RcGSxrCVNSDonRz/ksRM5z+hidDj1ESH1Cza9J4RXKkw7Mt/IRLfkALqU9IzgSibDe/O Vjyg== X-Gm-Message-State: AFq9FYI3W4TEtZ8hgwnZCqaUUayyqjBvTZHgw6yhjKTqtMtwLHJGhYeQ OTnKjdIVCKZaOxn813iFWgodPxg8mqoNvq1TK1YkurTn6ePHiMqBZvD57chEOQ== X-Gm-Gg: AYBFou2UUfF0BHThL9re8dJHAytfH8+1af30r0hYgXjjMPQBW6pr9ghT+LgqxyL+bF/ 0MbJfl6FgJ5Pawrgb51AJXxOH06jM5FnZZRUIMo7ZqOOLZxjsbvUjJVWadOy1EPSti54/pX5OCF sAkooyZz+cUbg9+T3dx+oElWE4CJLSHS6Ot+HARFjPkgShcI6WMF+Lvf7FpUmPditlUz0JyxQhi Tj2JGaC9c+Gg7vf6O/D0VnqbRrOCvD8vRXsSmDH5NZ2/omt5AfQpYiogNeACGrEw9TAd2t0iQhA 02rHX3O0FszDRH++eD0FBC9heaExo5t3H/o3zWqkYEOZSce+MFG1co1jO+OMlK6Bu8kubnu2vWH BrpJAjzh7EkIbY51vAO9f+vwNFPscJje4eNkaclaZBECrOf/PY7DR9YkJve/C+Ryr5Qq7JtlRuC HUU/UMvXmzw6n2Z+TMEgRoBIVju/rD9bA0uSzrHyOlUAmmYmmEILRcuQFzs0ADt6ePRI37dUz8h 7IlU8yM+pkc9tkTMkev6Of5AM2gTIE7q4YUlu5CX8/AnNYQ15o7TDtWu4gwjjc1B7U= X-Received: by 2002:a05:6000:2484:b0:487:6f2:1a4c with SMTP id ffacd0b85a97d-48b025253damr5692096f8f.25.1790808361103; Wed, 30 Sep 2026 15:46:01 -0700 (PDT) Received: from Mein-PC (p200300ed8f0406002186408458360e23.dip0.t-ipconnect.de. [2003:ed:8f04:600:2186:4084:5836:e23]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-48b0692b5d7sm2300709f8f.28.2026.09.30.15.46.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Sep 2026 15:46:00 -0700 (PDT) From: Oleg Tolmatcev To: gcc-patches@gcc.gnu.org Cc: Oleg Tolmatcev Subject: [PATCH v2 1/2] i386: Take the va_arg by-reference decision from the va_list type Date: Thu, 1 Oct 2026 00:45:24 +0200 Message-ID: <20260930224523.495-2-oleg.tolmatcev@gmail.com> X-Mailer: git-send-email 2.56.0.windows.1 MIME-Version: 1.0 X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_PBL, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org For an MS ABI va_list, ix86_gimplify_va_arg calls std_gimplify_va_arg_expr, which asks the pass_by_reference hook whether the argument was passed by reference. Without cumulative args, the hook assumes the default ABI, SysV on Linux, so va_arg reads an argument passed by reference as if it were passed by value. Split the ABI-specific decision out of ix86_pass_by_reference. For an indirect MS ABI argument, follow ia64_gimplify_va_arg: use the standard va_arg helper to read its pointer and then dereference it. Use the explicit SysV ABI for a SysV va_list so the opposite target-default mismatch is handled as well. gcc/ChangeLog: * config/i386/i386.cc (ix86_pass_by_reference_abi): New function, split out of ... (ix86_pass_by_reference): ... this. (ix86_gimplify_va_arg): Derive argument indirection from the ABI of the va_list type. Read indirect MS ABI arguments as pointers. gcc/testsuite/ChangeLog: * gcc.target/i386/va-arg-abi-attribute.c: New test. Signed-off-by: Oleg Tolmatcev --- gcc/config/i386/i386.cc | 37 +++++++--- .../gcc.target/i386/va-arg-abi-attribute.c | 73 +++++++++++++++++++ 2 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/gcc.target/i386/va-arg-abi-attribute.c diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc index e9015d2427..3b1f680b22 100644 --- a/gcc/config/i386/i386.cc +++ b/gcc/config/i386/i386.cc @@ -3655,17 +3655,15 @@ ix86_function_arg (cumulative_args_t cum_v, const function_arg_info &arg) reference. If nonzero for an argument, a copy of that argument is made in memory and a pointer to the argument is passed instead of the argument itself. The pointer is passed in whatever way is - appropriate for passing a pointer to that type. */ + appropriate for passing a pointer to that type. CALL_ABI is the + calling convention the argument is passed under. */ static bool -ix86_pass_by_reference (cumulative_args_t cum_v, const function_arg_info &arg) +ix86_pass_by_reference_abi (enum calling_abi call_abi, + const function_arg_info &arg) { - CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v); - if (TARGET_64BIT) { - enum calling_abi call_abi = cum ? cum->call_abi : ix86_abi; - /* See Windows x64 Software Convention. */ if (call_abi == MS_ABI) { @@ -3695,6 +3693,16 @@ ix86_pass_by_reference (cumulative_args_t cum_v, const function_arg_info &arg) return false; } +/* Implement TARGET_PASS_BY_REFERENCE. */ + +static bool +ix86_pass_by_reference (cumulative_args_t cum_v, const function_arg_info &arg) +{ + CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v); + + return ix86_pass_by_reference_abi (cum ? cum->call_abi : ix86_abi, arg); +} + /* Return true when TYPE should be 128bit aligned for 32bit argument passing ABI. XXX: This function is obsolete and is only used for checking psABI compatibility with previous versions of GCC. */ @@ -4988,7 +4996,7 @@ ix86_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p, tree lab_false, lab_over = NULL_TREE; tree addr, t2; rtx container; - int indirect_p = 0; + bool indirect_p; tree ptrtype; machine_mode nat_mode; unsigned int arg_boundary; @@ -4996,7 +5004,17 @@ ix86_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p, /* Only 64bit target needs something special. */ if (is_va_list_char_pointer (TREE_TYPE (valist))) - return std_gimplify_va_arg_expr (valist, type, pre_p, post_p); + { + indirect_p + = ix86_pass_by_reference_abi (MS_ABI, function_arg_info (type, false)); + if (indirect_p) + { + type = build_pointer_type (type); + t = std_gimplify_va_arg_expr (valist, type, pre_p, post_p); + return build_va_arg_indirect_ref (t); + } + return std_gimplify_va_arg_expr (valist, type, pre_p, post_p); + } f_gpr = TYPE_FIELDS (TREE_TYPE (sysv_va_list_type_node)); f_fpr = DECL_CHAIN (f_gpr); @@ -5010,7 +5028,8 @@ ix86_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p, ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE); sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE); - indirect_p = pass_va_arg_by_reference (type); + indirect_p + = ix86_pass_by_reference_abi (SYSV_ABI, function_arg_info (type, false)); if (indirect_p) type = build_pointer_type (type); size = arg_int_size_in_bytes (type); diff --git a/gcc/testsuite/gcc.target/i386/va-arg-abi-attribute.c b/gcc/testsuite/gcc.target/i386/va-arg-abi-attribute.c new file mode 100644 index 0000000000..48aed0c914 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/va-arg-abi-attribute.c @@ -0,0 +1,73 @@ +/* ix86_gimplify_va_arg must decide whether an argument is passed by + reference from the va_list type, not from the ABI of the function + containing the va_arg or from the target default. */ +/* { dg-do run { target { lp64 || llp64 } } } */ +/* { dg-options "-std=gnu99" } */ + +typedef int v4si __attribute__ ((vector_size (16))); +struct s3 { char a, b, c; }; +struct s8 { int a, b; }; +struct s12 { int a, b, c; }; +struct s16 { long long a, b; }; + +#define READER(name, abi, list) \ +static __attribute__ ((abi, noipa)) void \ +name (list ap) \ +{ \ + struct s3 x3 = __builtin_va_arg (ap, struct s3); \ + struct s8 x8 = __builtin_va_arg (ap, struct s8); \ + struct s12 x12 = __builtin_va_arg (ap, struct s12); \ + struct s16 x16 = __builtin_va_arg (ap, struct s16); \ + v4si v = __builtin_va_arg (ap, v4si); \ + int i = __builtin_va_arg (ap, int); \ + if (x3.a != 1 || x3.b != 2 || x3.c != 3 \ + || x8.a != 4 || x8.b != 5 \ + || x12.a != 6 || x12.b != 7 || x12.c != 8 \ + || x16.a != 9 || x16.b != 10 \ + || v[0] != 11 || v[1] != 12 || v[2] != 13 || v[3] != 14 \ + || i != 15) \ + __builtin_abort (); \ +} + +READER (sysv_reads_sysv, sysv_abi, __builtin_sysv_va_list) +READER (ms_reads_sysv, ms_abi, __builtin_sysv_va_list) +READER (sysv_reads_ms, sysv_abi, __builtin_ms_va_list) +READER (ms_reads_ms, ms_abi, __builtin_ms_va_list) + +static __attribute__ ((sysv_abi, noipa)) void +sysv_varargs (int n, ...) +{ + __builtin_sysv_va_list ap; + __builtin_sysv_va_start (ap, n); + sysv_reads_sysv (ap); + __builtin_sysv_va_end (ap); + __builtin_sysv_va_start (ap, n); + ms_reads_sysv (ap); + __builtin_sysv_va_end (ap); +} + +static __attribute__ ((ms_abi, noipa)) void +ms_varargs (int n, ...) +{ + __builtin_ms_va_list ap; + __builtin_ms_va_start (ap, n); + sysv_reads_ms (ap); + __builtin_ms_va_end (ap); + __builtin_ms_va_start (ap, n); + ms_reads_ms (ap); + __builtin_ms_va_end (ap); +} + +int +main (void) +{ + struct s3 x3 = { 1, 2, 3 }; + struct s8 x8 = { 4, 5 }; + struct s12 x12 = { 6, 7, 8 }; + struct s16 x16 = { 9, 10 }; + v4si v = { 11, 12, 13, 14 }; + + sysv_varargs (0, x3, x8, x12, x16, v, 15); + ms_varargs (0, x3, x8, x12, x16, v, 15); + return 0; +}