https://gcc.gnu.org/g:e12208722dabdad25cc13bb580991b5bf511a104

commit r16-3345-ge12208722dabdad25cc13bb580991b5bf511a104
Author: H.J. Lu <hjl.to...@gmail.com>
Date:   Fri Aug 22 05:56:42 2025 -0700

    Emit the TLS call after NOTE_INSN_FUNCTION_BEG
    
    For the beginning basic block:
    
    (note 4 0 2 2 [bb 2] NOTE_INSN_BASIC_BLOCK)
    (note 2 4 26 2 NOTE_INSN_FUNCTION_BEG)
    
    emit the TLS call after NOTE_INSN_FUNCTION_BEG.
    
    gcc/
    
            PR target/121635
            * config/i386/i386-features.cc (ix86_emit_tls_call): Emit the
            TLS call after NOTE_INSN_FUNCTION_BEG.
    
    gcc/testsuite/
    
            PR target/121635
            * gcc.target/i386/pr121635-1a.c: New test.
            * gcc.target/i386/pr121635-1b.c: Likewise.
    
    Signed-off-by: H.J. Lu <hjl.to...@gmail.com>

Diff:
---
 gcc/config/i386/i386-features.cc            | 20 ++++++++++++++++----
 gcc/testsuite/gcc.target/i386/pr121635-1a.c | 21 +++++++++++++++++++++
 gcc/testsuite/gcc.target/i386/pr121635-1b.c |  7 +++++++
 3 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/gcc/config/i386/i386-features.cc b/gcc/config/i386/i386-features.cc
index 514d2a5d3784..cdb2a0b34b27 100644
--- a/gcc/config/i386/i386-features.cc
+++ b/gcc/config/i386/i386-features.cc
@@ -3796,14 +3796,20 @@ ix86_emit_tls_call (rtx tls_set, x86_cse_kind kind, 
basic_block bb,
        {
          if (insn == BB_END (bb))
            {
-             /* This must be a basic block with only a label:
+             /* This must be the beginning basic block:
+
+                (note 4 0 2 2 [bb 2] NOTE_INSN_BASIC_BLOCK)
+                (note 2 4 26 2 NOTE_INSN_FUNCTION_BEG)
+
+                or a basic block with only a label:
 
                 (code_label 78 11 77 3 14 (nil) [1 uses])
                 (note 77 78 54 3 [bb 3] NOTE_INSN_BASIC_BLOCK)
 
               */
              gcc_assert (NOTE_P (insn)
-                         && NOTE_KIND (insn) == NOTE_INSN_BASIC_BLOCK);
+                         && (NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG
+                             || NOTE_KIND (insn) == NOTE_INSN_BASIC_BLOCK));
              insn = NULL;
              break;
            }
@@ -3842,8 +3848,14 @@ ix86_emit_tls_call (rtx tls_set, x86_cse_kind kind, 
basic_block bb,
            }
          else
            {
-             /* Emit the TLS call after NOTE_INSN_BASIC_BLOCK in a
-                basic block with only a label:
+             /* Emit the TLS call after NOTE_INSN_FUNCTION_BEG in the
+                beginning basic block:
+
+                (note 4 0 2 2 [bb 2] NOTE_INSN_BASIC_BLOCK)
+                (note 2 4 26 2 NOTE_INSN_FUNCTION_BEG)
+
+                or after NOTE_INSN_BASIC_BLOCK a basic block with only
+                a label:
 
                 (code_label 78 11 77 3 14 (nil) [1 uses])
                 (note 77 78 54 3 [bb 3] NOTE_INSN_BASIC_BLOCK)
diff --git a/gcc/testsuite/gcc.target/i386/pr121635-1a.c 
b/gcc/testsuite/gcc.target/i386/pr121635-1a.c
new file mode 100644
index 000000000000..4db7deff69a1
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr121635-1a.c
@@ -0,0 +1,21 @@
+/* { dg-do compile { target *-*-linux* } } */
+/* { dg-options "-O2 -fpic -fplt -mtls-dialect=gnu" } */
+
+extern int get_cordz_mean_interval ();
+extern thread_local long cordz_next_sample, kIntervalIfDisabled;
+extern bool cordz_should_profile_slow (void);
+inline bool
+cordz_should_profile (void)
+{
+  return cordz_should_profile_slow ();
+}
+bool
+cordz_should_profile_slow (void)
+{
+  int mean_interval = get_cordz_mean_interval ();
+  if (mean_interval)
+    cordz_next_sample = kIntervalIfDisabled;
+  return cordz_next_sample || cordz_should_profile ();
+}
+
+/* { dg-final { scan-assembler-times "call\[ \t\]__tls_get_addr@PLT" 2 { 
target { ! ia32 } } } } */
diff --git a/gcc/testsuite/gcc.target/i386/pr121635-1b.c 
b/gcc/testsuite/gcc.target/i386/pr121635-1b.c
new file mode 100644
index 000000000000..4095fb556aeb
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr121635-1b.c
@@ -0,0 +1,7 @@
+/* { dg-do compile { target *-*-linux* } } */
+/* { dg-options "-O2 -fpic -fplt -mtls-dialect=gnu2" } */
+
+#include "pr121635-1a.c"
+
+/* { dg-final { scan-assembler-times "call\[ 
\t\]\\*cordz_next_sample@TLSCALL\\(%(?:r|e)ax\\)" 1 { target { ! ia32 } } } } */
+/* { dg-final { scan-assembler-times "call\[ 
\t\]\\*kIntervalIfDisabled@TLSCALL\\(%(?:r|e)ax\\)" 1 { target { ! ia32 } } } } 
*/

Reply via email to