From patchwork Fri Feb 2 15:42:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1894685 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=ehcMZXGM; 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 4TRKls1Lrlz23g7 for ; Sat, 3 Feb 2024 02:42:33 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 387213857C5E for ; Fri, 2 Feb 2024 15:42:31 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by sourceware.org (Postfix) with ESMTPS id 35B80385802F for ; Fri, 2 Feb 2024 15:42:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 35B80385802F 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 35B80385802F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706888526; cv=none; b=wQHNNgK0l6sVB7qbKW7ulyO//tLrR8G8kvsEB6Q2aPAkopWUw2yrUi+JTRvtnFryXe1xEFfcAMkG6Uvpkd7dVnMK2+1pZLICYI9eAVcIserlI7YSbaDd8oan+1SC9V7fUDXP5aLEMoIlnKDaTH6I7yWG9lCh7lpsLPjAjl8to+Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706888526; c=relaxed/simple; bh=7BbuyW0oE6eZ9TwlJx8Q5q4lZyEjDBPq/JQDgXiIU+g=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=uGrKFHQzwqSzqwp9mgc4+TITUSUlG2r82sQ3bluP2tCt58f5NB7tlRbHPVLBbjg0agLw1U43f+IOYiO9ptljoe13P8fAaWm6qxjsmfvJ7dwAi+FfyeWOtHfRrz9hA5I00wTUXdAU5KStRErOzVakcNh+mOwZURmpKA9q6lmoPGQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1d7431e702dso19447825ad.1 for ; Fri, 02 Feb 2024 07:42:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706888522; x=1707493322; 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; bh=kLO7OKqQDRjxR7kZKSByeV7c/vuJjcpOLVH6anL8AXg=; b=ehcMZXGMeWNvxLbGvJiCTn0LaQ931NmoUDZoa34zjZKYdnlbslk7N/RnE/VuaNGwjx 8VxRqaA3sqq68yJkOUqN9Jyy5lrvCXLZKwXHLQPmcalA0XvsPCjQfMLYAuaayPceqims nuBP/DFEGVPZy4qK0nKAzYJGUBjVB0ENiiF6AyLgqM8kt0A2R8p6zcZASn/ofkfg+1p/ CAeZZ9JB2Mgyg1kbAzdW2u0wYNXVrrE+HgBNgq2Ig1Ay5qHZHGmf6eTPMK1xgvyyZKuX siU72BTU5m6U07iF4MiY22hUDCjn9UdyzyAJnR8qFhAkNIiMnKCyFRHcdZuWgPNamMoS QYEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706888522; x=1707493322; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=kLO7OKqQDRjxR7kZKSByeV7c/vuJjcpOLVH6anL8AXg=; b=bw1SSUqVC4dTYTsMaGmk4qfGv5SJKv1kh1NhZrmq0tn//cj8x+bjLdjsShFcN9yAvJ ptxXUnednBG2hFbv8c9ewbfOeYEaL3zUEGR5GslpYDHRNDkekVUUvDTPGPMAL1hy2jkr 55/Mi+/grwZwYiYCaKelQgBtbYNQQSBSOpCDbtRIJcRQ/hGD9Fxif4YmHRuznO7NQWbg Z+6nMY7m/bkwu9QZqtTHMKH2fVhqCl43IkN3xf3OB3KXQTKHzpwFU5mM5G3jRiQsi0Ly EwUpdhzBRoGhhdCos18u6VIHZD+wvLioXTWQLPj2ksSO196X/CnE5btw7HVrl2fNfVrX hvJQ== X-Gm-Message-State: AOJu0Yx0+51ZMrfYC8462Hsd6y0t3F1EX1441WuWuWcM3nOx2UZl3ojj 8cKg20SJdTqdtoL0Qg5xLCo2f7BUdO3dcdfYJ2zxNmJFuO0f0Ex5Yju/BI+A X-Google-Smtp-Source: AGHT+IHg5GMCmWO+pejnxVx3kbRRqQKRXFZfIKOUtnKgtjHge1eQNCBjn7jSzzG6tEy+efYYWd2wZQ== X-Received: by 2002:a17:903:11d0:b0:1d6:f1cb:7a95 with SMTP id q16-20020a17090311d000b001d6f1cb7a95mr9177652plh.57.1706888521688; Fri, 02 Feb 2024 07:42:01 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCX2rP9F9eT3Hz+F7C5wkR4NbHzd4UNxjWfF7rafIpzeJhRwg9mQMCYbfnkFF+wUCXZ2gfjt4kj+EwKfhKXgKaR7 Received: from gnu-cfl-3.localdomain ([172.56.169.51]) by smtp.gmail.com with ESMTPSA id i2-20020a17090332c200b001d8f4b57c01sm1724212plr.297.2024.02.02.07.42.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 07:42:01 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 31E5C740172; Fri, 2 Feb 2024 07:42:00 -0800 (PST) From: "H.J. Lu" To: gcc-patches@gcc.gnu.org Cc: rep.dot.nop@gmail.com, jakub@redhat.com Subject: [PATCH v4] x86-64: Find a scratch register for large model profiling Date: Fri, 2 Feb 2024 07:42:00 -0800 Message-ID: <20240202154200.306107-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-3023.8 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, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Changes in v4: 1. Remove pr113689-2.c. 2. Use df_get_live_out. Changes in v3: 1. Remove r10_ok. Changes in v2: 1. Add int_parameter_registers to machine_function to track integer registers used for parameter passing. 2. Update x86_64_select_profile_regnum to try %r10 first and use an caller-saved register, which isn't used for parameter passing. --- 2 scratch registers, %r10 and %r11, are available at function entry for large model profiling. But %r10 may be used by stack realignment and we can't use %r10 in this case. Add x86_64_select_profile_regnum to find an caller-saved register, which isn't live, at entry for large model profiling and sorry if we can't find one. gcc/ PR target/113689 * config/i386/i386.cc (x86_64_select_profile_regnum): New. (x86_function_profiler): Call x86_64_select_profile_regnum to get a scratch register for large model profiling. * config/i386/i386.h (machine_function): Add int_parameter_registers. gcc/testsuite/ PR target/113689 * gcc.target/i386/pr113689-1.c: New file. * gcc.target/i386/pr113689-2.c: Likewise. --- gcc/config/i386/i386.cc | 81 ++++++++++++++++++---- gcc/testsuite/gcc.target/i386/pr113689-1.c | 49 +++++++++++++ gcc/testsuite/gcc.target/i386/pr113689-2.c | 41 +++++++++++ 3 files changed, 156 insertions(+), 15 deletions(-) create mode 100644 gcc/testsuite/gcc.target/i386/pr113689-1.c create mode 100644 gcc/testsuite/gcc.target/i386/pr113689-2.c diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc index b3e7c74846e..b27445b8371 100644 --- a/gcc/config/i386/i386.cc +++ b/gcc/config/i386/i386.cc @@ -22749,6 +22749,39 @@ current_fentry_section (const char **name) return true; } +/* Return an caller-saved register, which isn't live, at entry for + profile. */ + +static int +x86_64_select_profile_regnum (bool r11_ok ATTRIBUTE_UNUSED) +{ + /* Use %r10 if it isn't used by DRAP. */ + if (!crtl->drap_reg || REGNO (crtl->drap_reg) != R10_REG) + return R10_REG; + + bitmap reg_live = df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)); + + int i; + for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) + if (GENERAL_REGNO_P (i) + && i != R10_REG +#ifdef NO_PROFILE_COUNTERS + && (r11_ok || i != R11_REG) +#else + && i != R11_REG +#endif + && (!REX2_INT_REGNO_P (i) || TARGET_APX_EGPR) + && !fixed_regs[i] + && call_used_regs[i] + && !REGNO_REG_SET_P (reg_live, i)) + return i; + + sorry ("no register available for profiling %<-mcmodel=large%s%>", + ix86_cmodel == CM_LARGE_PIC ? " -fPIC" : ""); + + return INVALID_REGNUM; +} + /* Output assembler code to FILE to increment profiler label # LABELNO for profiling a function entry. */ void @@ -22783,42 +22816,60 @@ x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED) fprintf (file, "\tleaq\t%sP%d(%%rip), %%r11\n", LPREFIX, labelno); #endif + int scratch; + const char *reg_prefix; + const char *reg; + if (!TARGET_PECOFF) { switch (ix86_cmodel) { case CM_LARGE: - /* NB: R10 is caller-saved. Although it can be used as a - static chain register, it is preserved when calling - mcount for nested functions. */ + scratch = x86_64_select_profile_regnum (true); + reg = hi_reg_name[scratch]; + reg_prefix = LEGACY_INT_REGNO_P (scratch) ? "r" : ""; if (ASSEMBLER_DIALECT == ASM_INTEL) - fprintf (file, "1:\tmovabs\tr10, OFFSET FLAT:%s\n" - "\tcall\tr10\n", mcount_name); + fprintf (file, + "1:\tmovabs\t%s%s, OFFSET FLAT:%s\n" + "\tcall\t%s%s\n", + reg_prefix, reg, mcount_name, reg_prefix, reg); else - fprintf (file, "1:\tmovabsq\t$%s, %%r10\n\tcall\t*%%r10\n", - mcount_name); + fprintf (file, + "1:\tmovabsq\t$%s, %%%s%s\n\tcall\t*%%%s%s\n", + mcount_name, reg_prefix, reg, reg_prefix, reg); break; case CM_LARGE_PIC: #ifdef NO_PROFILE_COUNTERS + scratch = x86_64_select_profile_regnum (false); + reg = hi_reg_name[scratch]; + reg_prefix = LEGACY_INT_REGNO_P (scratch) ? "r" : ""; if (ASSEMBLER_DIALECT == ASM_INTEL) { fprintf (file, "1:movabs\tr11, " "OFFSET FLAT:_GLOBAL_OFFSET_TABLE_-1b\n"); - fprintf (file, "\tlea\tr10, 1b[rip]\n"); - fprintf (file, "\tadd\tr10, r11\n"); + fprintf (file, "\tlea\t%s%s, 1b[rip]\n", + reg_prefix, reg); + fprintf (file, "\tadd\t%s%s, r11\n", + reg_prefix, reg); fprintf (file, "\tmovabs\tr11, OFFSET FLAT:%s@PLTOFF\n", mcount_name); - fprintf (file, "\tadd\tr10, r11\n"); - fprintf (file, "\tcall\tr10\n"); + fprintf (file, "\tadd\t%s%s, r11\n", + reg_prefix, reg); + fprintf (file, "\tcall\t%s%s\n", + reg_prefix, reg); break; } fprintf (file, "1:\tmovabsq\t$_GLOBAL_OFFSET_TABLE_-1b, %%r11\n"); - fprintf (file, "\tleaq\t1b(%%rip), %%r10\n"); - fprintf (file, "\taddq\t%%r11, %%r10\n"); + fprintf (file, "\tleaq\t1b(%%rip), %%%s%s\n", + reg_prefix, reg); + fprintf (file, "\taddq\t%%r11, %%%s%s\n", + reg_prefix, reg); fprintf (file, "\tmovabsq\t$%s@PLTOFF, %%r11\n", mcount_name); - fprintf (file, "\taddq\t%%r11, %%r10\n"); - fprintf (file, "\tcall\t*%%r10\n"); + fprintf (file, "\taddq\t%%r11, %%%s%s\n", + reg_prefix, reg); + fprintf (file, "\tcall\t*%%%s%s\n", + reg_prefix, reg); #else sorry ("profiling %<-mcmodel=large%> with PIC is not supported"); #endif diff --git a/gcc/testsuite/gcc.target/i386/pr113689-1.c b/gcc/testsuite/gcc.target/i386/pr113689-1.c new file mode 100644 index 00000000000..c32445e0fc4 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr113689-1.c @@ -0,0 +1,49 @@ +/* { dg-do run { target { lp64 && fpic } } } */ +/* { dg-options "-O2 -fno-pic -fprofile -mcmodel=large" } */ + +#include + +__attribute__((noipa,noclone,noinline)) +void +bar (int a1, int a2, int a3, int a4, int a5, int a6, + char *x, char *y, int *z) +{ + if (a1 != 1) + __builtin_abort (); + if (a2 != 2) + __builtin_abort (); + if (a3 != 3) + __builtin_abort (); + if (a4 != 4) + __builtin_abort (); + if (a5 != 5) + __builtin_abort (); + if (a6 != 6) + __builtin_abort (); + x[0] = 42; + y[0] = 42; + if (z[0] != 16) + __builtin_abort (); +} + +__attribute__((noipa,noclone,noinline)) +void +foo (int c, int d, int e, int f, int g, int h, int z, ...) +{ + typedef char B[32]; + B b __attribute__((aligned (32))); + va_list ap; + va_start (ap, z); + int x = va_arg (ap, int); + if (x != 38) + __builtin_abort (); + bar (c, d, e, f, g, h, &b[0], __builtin_alloca (z), &z); + va_end (ap); +} + +int +main () +{ + foo (1, 2, 3, 4, 5, 6, 16, 38); + return 0; +} diff --git a/gcc/testsuite/gcc.target/i386/pr113689-2.c b/gcc/testsuite/gcc.target/i386/pr113689-2.c new file mode 100644 index 00000000000..fec5b171a39 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr113689-2.c @@ -0,0 +1,41 @@ +/* { dg-do run { target { lp64 && fpic } } } */ +/* { dg-options "-O2 -fpic -fprofile -mcmodel=large" } */ + +__attribute__((noipa,noclone,noinline)) +void +bar (int a1, int a2, int a3, int a4, int a5, int a6, + char *x, char *y, int *z) +{ + if (a1 != 1) + __builtin_abort (); + if (a2 != 2) + __builtin_abort (); + if (a3 != 3) + __builtin_abort (); + if (a4 != 4) + __builtin_abort (); + if (a5 != 5) + __builtin_abort (); + if (a6 != 6) + __builtin_abort (); + x[0] = 42; + y[0] = 42; + if (z[0] != 16) + __builtin_abort (); +} + +__attribute__((noipa,noclone,noinline)) +void +foo (int c, int d, int e, int f, int g, int h, int z) +{ + typedef char B[32]; + B b __attribute__((aligned (32))); + bar (c, d, e, f, g, h, &b[0], __builtin_alloca (z), &z); +} + +int +main () +{ + foo (1, 2, 3, 4, 5, 6, 16); + return 0; +}