public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH][committed] arm: Extend -mtp= arguments
@ 2023-06-13  9:15 Kyrylo Tkachov
  0 siblings, 0 replies; only message in thread
From: Kyrylo Tkachov @ 2023-06-13  9:15 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 1625 bytes --]

Hi all,

After discussing the -mtp= option with Arm's LLVM developers we'd like to extend
the functionality of the option somewhat.
There are actually 3 system registers that can be accessed for the thread pointer
in aarch32: tpidrurw, tpidruro, tpidrprw.  They are all read through the CP15 co-processor
mechanism. The current -mtp=cp15 option reads the tpidruro register.
This patch extends -mtp to allow for the above three explicit tpidr names and
keeps -mtp=cp15 as an alias of -mtp=tpidruro for backwards compatibility.

There is more relevant discussion of the options at https://reviews.llvm.org/D152433 if you're interested.

Bootstrapped and tested on arm-none-linux-gnueabihf.
Pushing to trunk.
Thanks,
Kyrill

gcc/ChangeLog:

	* config/arm/arm-opts.h (enum arm_tp_type): Remove TP_CP15.
	Add TP_TPIDRURW, TP_TPIDRURO, TP_TPIDRPRW values.
	* config/arm/arm-protos.h (arm_output_load_tpidr): Declare prototype.
	* config/arm/arm.cc (arm_option_reconfigure_globals): Replace TP_CP15
	with TP_TPIDRURO.
	(arm_output_load_tpidr): Define.
	* config/arm/arm.h (TARGET_HARD_TP): Define in terms of TARGET_SOFT_TP.
	* config/arm/arm.md (load_tp_hard): Call arm_output_load_tpidr to output
	assembly.
	(reload_tp_hard): Likewise.
	* config/arm/arm.opt (tpidrurw, tpidruro, tpidrprw): New values for
	arm_tp_type.
	* doc/invoke.texi (Arm Options, mtp): Document new values.

gcc/testsuite/ChangeLog:

	* gcc.target/arm/mtp.c: New test.
	* gcc.target/arm/mtp_1.c: New test.
	* gcc.target/arm/mtp_2.c: New test.
	* gcc.target/arm/mtp_3.c: New test.
	* gcc.target/arm/mtp_4.c: New test.

[-- Attachment #2: mtp-arm.patch --]
[-- Type: application/octet-stream, Size: 8224 bytes --]

diff --git a/gcc/config/arm/arm-opts.h b/gcc/config/arm/arm-opts.h
index 9964fd2dbb58360b86fed8d02089fcb34a0b51e6..174dbc505e05f6db9a4c46f28cefccec38b70174 100644
--- a/gcc/config/arm/arm-opts.h
+++ b/gcc/config/arm/arm-opts.h
@@ -61,7 +61,9 @@ enum float_abi_type
 enum arm_tp_type {
   TP_AUTO,
   TP_SOFT,
-  TP_CP15
+  TP_TPIDRURW,
+  TP_TPIDRURO,
+  TP_TPIDRPRW
 };
 
 /* Which TLS scheme to use.  */
diff --git a/gcc/config/arm/arm-protos.h b/gcc/config/arm/arm-protos.h
index 61fcd67143750ed729a253877e121c533eee59ba..7d73c66a15d03a86f8838df4b67c1ec96d20b237 100644
--- a/gcc/config/arm/arm-protos.h
+++ b/gcc/config/arm/arm-protos.h
@@ -182,6 +182,7 @@ extern bool arm_is_long_call_p (tree);
 extern int    arm_emit_vector_const (FILE *, rtx);
 extern void arm_emit_fp16_const (rtx c);
 extern const char * arm_output_load_gr (rtx *);
+extern const char * arm_output_load_tpidr (rtx, bool);
 extern const char *vfp_output_vstmd (rtx *);
 extern void arm_output_multireg_pop (rtx *, bool, rtx, bool, bool);
 extern void arm_set_return_address (rtx, rtx);
diff --git a/gcc/config/arm/arm.h b/gcc/config/arm/arm.h
index 7d40b8b7e00bc3b4dcff7ec685ba864ca3885052..4f54530adcb616413bf210dff8c43f0f2046fd3d 100644
--- a/gcc/config/arm/arm.h
+++ b/gcc/config/arm/arm.h
@@ -152,8 +152,8 @@ emission of floating point pcs attributes.  */
 #define TARGET_AAPCS_BASED \
     (arm_abi != ARM_ABI_APCS && arm_abi != ARM_ABI_ATPCS)
 
-#define TARGET_HARD_TP			(target_thread_pointer == TP_CP15)
 #define TARGET_SOFT_TP			(target_thread_pointer == TP_SOFT)
+#define TARGET_HARD_TP			!TARGET_SOFT_TP
 #define TARGET_GNU2_TLS			(target_tls_dialect == TLS_GNU2)
 
 /* Only 16-bit thumb code.  */
diff --git a/gcc/config/arm/arm.cc b/gcc/config/arm/arm.cc
index c3e731b89823d2c907fc866c83287092251f5bde..38f0839de1c75547c259ac3d655fcfc14e7208a2 100644
--- a/gcc/config/arm/arm.cc
+++ b/gcc/config/arm/arm.cc
@@ -3927,7 +3927,7 @@ arm_option_reconfigure_globals (void)
   if (target_thread_pointer == TP_AUTO)
     {
       if (arm_arch6k && !TARGET_THUMB1)
-	target_thread_pointer = TP_CP15;
+	target_thread_pointer = TP_TPIDRURO;
       else
 	target_thread_pointer = TP_SOFT;
     }
@@ -34648,4 +34648,34 @@ arm_get_mask_mode (machine_mode mode)
   return default_get_mask_mode (mode);
 }
 
+/* Output assembly to read the thread pointer from the appropriate TPIDR
+   register into DEST.  If PRED_P also emit the %? that can be used to
+   output the predication code.  */
+
+const char *
+arm_output_load_tpidr (rtx dst, bool pred_p)
+{
+  char buf[64];
+  int tpidr_coproc_num = -1;
+  switch (target_thread_pointer)
+    {
+    case TP_TPIDRURW:
+      tpidr_coproc_num = 2;
+      break;
+    case TP_TPIDRURO:
+      tpidr_coproc_num = 3;
+      break;
+    case TP_TPIDRPRW:
+      tpidr_coproc_num = 4;
+      break;
+    default:
+      gcc_unreachable ();
+    }
+  snprintf (buf, sizeof (buf),
+	    "mrc%s\tp15, 0, %%0, c13, c0, %d\t@ load_tp_hard",
+	    pred_p ? "%?" : "", tpidr_coproc_num);
+  output_asm_insn (buf, &dst);
+  return "";
+}
+
 #include "gt-arm.h"
diff --git a/gcc/config/arm/arm.md b/gcc/config/arm/arm.md
index 2c7249f01937eafcab175e73149881b06a929872..2ac97232ffd2c030cc3f231cb21e4fcc42f5d5b8 100644
--- a/gcc/config/arm/arm.md
+++ b/gcc/config/arm/arm.md
@@ -12275,7 +12275,7 @@ (define_insn "load_tp_hard"
   [(set (match_operand:SI 0 "register_operand" "=r")
 	(unspec:SI [(const_int 0)] UNSPEC_TLS))]
   "TARGET_HARD_TP"
-  "mrc%?\\tp15, 0, %0, c13, c0, 3\\t@ load_tp_hard"
+  "* return arm_output_load_tpidr (operands[0], true);"
   [(set_attr "predicable" "yes")
    (set_attr "type" "mrs")]
 )
@@ -12285,7 +12285,7 @@ (define_insn "reload_tp_hard"
   [(set (match_operand:SI 0 "register_operand" "=r")
 	(unspec_volatile:SI [(const_int 0)] VUNSPEC_MRC))]
   "TARGET_HARD_TP"
-  "mrc\\tp15, 0, %0, c13, c0, 3\\t@ reload_tp_hard"
+  "* return arm_output_load_tpidr (operands[0], false);"
   [(set_attr "type" "mrs")]
 )
 
diff --git a/gcc/config/arm/arm.opt b/gcc/config/arm/arm.opt
index 3a49b51ece0e3711d49ee5fb94e2bfdac3a05e38..88299dabc3aefa356501e0d720a104a571e11dfb 100644
--- a/gcc/config/arm/arm.opt
+++ b/gcc/config/arm/arm.opt
@@ -230,7 +230,16 @@ EnumValue
 Enum(arm_tp_type) String(auto) Value(TP_AUTO)
 
 EnumValue
-Enum(arm_tp_type) String(cp15) Value(TP_CP15)
+Enum(arm_tp_type) String(tpidrurw) Value(TP_TPIDRURW)
+
+EnumValue
+Enum(arm_tp_type) String(cp15) Value(TP_TPIDRURO)
+
+EnumValue
+Enum(arm_tp_type) String(tpidruro) Value(TP_TPIDRURO)
+
+EnumValue
+Enum(arm_tp_type) String(tpidrprw) Value(TP_TPIDRPRW)
 
 mtpcs-frame
 Target Mask(TPCS_FRAME)
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index c5891335d2e4a6a30eccb18afcad35aeeb598e9d..758c1e4c9fde284ff9b1a261e0043bb8fee467d9 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -22649,12 +22649,15 @@ by default.
 
 @opindex mtp
 @item -mtp=@var{name}
-Specify the access model for the thread local storage pointer.  The valid
-models are @samp{soft}, which generates calls to @code{__aeabi_read_tp},
-@samp{cp15}, which fetches the thread pointer from @code{cp15} directly
-(supported in the arm6k architecture), and @samp{auto}, which uses the
-best available method for the selected processor.  The default setting is
-@samp{auto}.
+Specify the access model for the thread local storage pointer.  The model
+@samp{soft} generates calls to @code{__aeabi_read_tp}.  Other accepted
+models are @samp{tpidrurw}, @samp{tpidruro} and @samp{tpidrprw} which fetch
+the thread pointer from the corresponding system register directly
+(supported from the arm6k architecture and later).  These system registers
+are accessed through the CP15 co-processor interface and the argument
+@samp{cp15} is also accepted as a convenience alias of @samp{tpidruro}.
+The argument @samp{auto} uses the best available method for the selected
+processor.  The default setting is @samp{auto}.
 
 @opindex mtls-dialect
 @item -mtls-dialect=@var{dialect}
diff --git a/gcc/testsuite/gcc.target/arm/mtp.c b/gcc/testsuite/gcc.target/arm/mtp.c
new file mode 100644
index 0000000000000000000000000000000000000000..d994c377fd36fe219558a0c8fe39315421a13188
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/mtp.c
@@ -0,0 +1,8 @@
+__thread int i;
+
+int
+foo (void)
+{
+  return i;
+}
+
diff --git a/gcc/testsuite/gcc.target/arm/mtp_1.c b/gcc/testsuite/gcc.target/arm/mtp_1.c
new file mode 100644
index 0000000000000000000000000000000000000000..678d27d9234405abddfef6d571328bb7c10fba3a
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/mtp_1.c
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target tls_native } */
+/* { dg-options "-O -mtp=cp15" } */
+
+#include "mtp.c"
+
+/* { dg-final { scan-assembler-times {mrc\tp15, 0, r3, c13, c0, 3} 1 } } */
diff --git a/gcc/testsuite/gcc.target/arm/mtp_2.c b/gcc/testsuite/gcc.target/arm/mtp_2.c
new file mode 100644
index 0000000000000000000000000000000000000000..bcb308f2637c6d6d6c6c7507ed16128581ba6179
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/mtp_2.c
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target tls_native } */
+/* { dg-options "-O -mtp=tpidrprw" } */
+
+#include "mtp.c"
+
+/* { dg-final { scan-assembler-times {mrc\tp15, 0, r3, c13, c0, 4} 1 } } */
diff --git a/gcc/testsuite/gcc.target/arm/mtp_3.c b/gcc/testsuite/gcc.target/arm/mtp_3.c
new file mode 100644
index 0000000000000000000000000000000000000000..7d5cea3cab61ddb9da60b418cfaf408e1060b58f
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/mtp_3.c
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target tls_native } */
+/* { dg-options "-O -mtp=tpidruro" } */
+
+#include "mtp.c"
+
+/* { dg-final { scan-assembler-times {mrc\tp15, 0, r3, c13, c0, 3} 1 } } */
diff --git a/gcc/testsuite/gcc.target/arm/mtp_4.c b/gcc/testsuite/gcc.target/arm/mtp_4.c
new file mode 100644
index 0000000000000000000000000000000000000000..068078df84ed6560072edacf4bf2064b115f6b49
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/mtp_4.c
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target tls_native } */
+/* { dg-options "-O -mtp=tpidrurw" } */
+
+#include "mtp.c"
+
+/* { dg-final { scan-assembler-times {mrc\tp15, 0, r3, c13, c0, 2} 1 } } */

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-06-13  9:15 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-13  9:15 [PATCH][committed] arm: Extend -mtp= arguments Kyrylo Tkachov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).