Implement __builtin_thread_pointer for x86 TLS.
authorliuhongt <hongtao.liu@intel.com>
Tue, 8 Sep 2020 07:44:58 +0000 (15:44 +0800)
committerliuhongt <hongtao.liu@intel.com>
Wed, 9 Sep 2020 08:17:42 +0000 (16:17 +0800)
gcc/ChangeLog:
PR target/96955
* config/i386/i386.md (get_thread_pointer<mode>): New
expander.

gcc/testsuite/ChangeLog:

* gcc.target/i386/builtin_thread_pointer.c: New test.

gcc/config/i386/i386.md
gcc/testsuite/gcc.target/i386/builtin_thread_pointer.c [new file with mode: 0644]

index 446793b78db1e19f5f0b0ff2ffa44227431c9811..93aae8123fd5962a71a5b71fe6e0d0c947524c8a 100644 (file)
       (clobber (reg:CC FLAGS_REG))])])
 
 ;; Load and add the thread base pointer from %<tp_seg>:0.
+(define_expand "get_thread_pointer<mode>"
+  [(set (match_operand:PTR 0 "register_operand")
+       (unspec:PTR [(const_int 0)] UNSPEC_TP))]
+  ""
+{
+  /* targetm is not visible in the scope of the condition.  */
+  if (!targetm.have_tls)
+    error ("%<__builtin_thread_pointer%> is not supported on this target");
+})
+
 (define_insn_and_split "*load_tp_<mode>"
   [(set (match_operand:PTR 0 "register_operand" "=r")
        (unspec:PTR [(const_int 0)] UNSPEC_TP))]
diff --git a/gcc/testsuite/gcc.target/i386/builtin_thread_pointer.c b/gcc/testsuite/gcc.target/i386/builtin_thread_pointer.c
new file mode 100644 (file)
index 0000000..dce3148
--- /dev/null
@@ -0,0 +1,28 @@
+/* { dg-do compile } */
+/* { dg-options "-mtls-direct-seg-refs -O2 -masm=att" } */
+
+int*
+foo1 ()
+{
+  return (int*) __builtin_thread_pointer ();
+}
+
+/* { dg-final { scan-assembler "mov\[lq\]\[ \t\]*%\[fg\]s:0, %\[re\]ax" } }  */
+
+int
+foo2 ()
+{
+  int* p =  (int*) __builtin_thread_pointer ();
+  return p[4];
+}
+
+/* { dg-final { scan-assembler "movl\[ \t\]*%\[fg\]s:16, %eax" } }  */
+
+int
+foo3 (int i)
+{
+  int* p = (int*) __builtin_thread_pointer ();
+  return p[i];
+}
+
+/* { dg-final { scan-assembler "movl\[ \t\]*%\[fg\]s:0\\(,%\[a-z0-9\]*,4\\), %eax" } }  */