Skip to content

Commit 1073b50

Browse files
committed
RISC-V: Implement __builtin_thread_pointer
RISC-V has a dedicate register for thread pointer which is specified in psABI doc, so we could support __builtin_thread_pointer in straightforward way. Note: clang/llvm was supported __builtin_thread_pointer for RISC-V port recently. - https://reviews.llvm.org/rGaabc24acf0d5f8677bd22fe9c108581e07c3e180 gcc/ChangeLog: * config/riscv/riscv.md (get_thread_pointer<mode>): New. (TP_REGNUM): Ditto. * doc/extend.texi (Target Builtins): Add RISC-V built-in section. Document __builtin_thread_pointer. gcc/testsuite/ChangeLog: * gcc.target/riscv/read-thread-pointer.c: New.
1 parent 4c0d132 commit 1073b50

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

gcc/config/riscv/riscv.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
(define_constants
7171
[(RETURN_ADDR_REGNUM 1)
7272
(GP_REGNUM 3)
73+
(TP_REGNUM 4)
7374
(T0_REGNUM 5)
7475
(T1_REGNUM 6)
7576
(S0_REGNUM 8)
@@ -2515,6 +2516,13 @@
25152516
DONE;
25162517
})
25172518

2519+
;; Named pattern for expanding thread pointer reference.
2520+
(define_expand "get_thread_pointer<mode>"
2521+
[(set (match_operand:P 0 "register_operand" "=r")
2522+
(reg:P TP_REGNUM))]
2523+
""
2524+
{})
2525+
25182526
(include "sync.md")
25192527
(include "peephole.md")
25202528
(include "pic.md")

gcc/doc/extend.texi

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13859,6 +13859,7 @@ instructions, but allow the compiler to schedule those calls.
1385913859
* PowerPC Hardware Transactional Memory Built-in Functions::
1386013860
* PowerPC Atomic Memory Operation Functions::
1386113861
* PowerPC Matrix-Multiply Assist Built-in Functions::
13862+
* RISC-V Built-in Functions::
1386213863
* RX Built-in Functions::
1386313864
* S/390 System z Built-in Functions::
1386413865
* SH Built-in Functions::
@@ -21471,6 +21472,16 @@ vec_t __builtin_vsx_xvcvspbf16 (vec_t);
2147121472
vec_t __builtin_vsx_xvcvbf16sp (vec_t);
2147221473
@end smallexample
2147321474

21475+
@node RISC-V Built-in Functions
21476+
@subsection RISC-V Built-in Functions
21477+
21478+
These built-in functions are available for the RISC-V family of
21479+
processors.
21480+
21481+
@deftypefn {Built-in Function} {void *} __builtin_thread_pointer (void)
21482+
Returns the value that is currently set in the @samp{tp} register.
21483+
@end deftypefn
21484+
2147421485
@node RX Built-in Functions
2147521486
@subsection RX Built-in Functions
2147621487
GCC supports some of the RX instructions which cannot be expressed in
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* { dg-do compile } */
2+
3+
void *get_tp()
4+
{
5+
return __builtin_thread_pointer ();
6+
}
7+
/* { dg-final { scan-assembler "mv\[ \t\]*[at][0-9]+,tp" } } */

0 commit comments

Comments
 (0)