public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Lulu Cheng <chenglulu@loongson.cn>
To: gcc-patches@gcc.gnu.org
Cc: xry111@xry111.site, i@xen0n.name, xuchenghua@loongson.cn,
	chenglulu@loongson.cn
Subject: [PATCH v2 2/2] LoongArch: When the code model is extreme, the symbol address is obtained through macro instructions regardless of the value of -mexplicit-relocs.
Date: Fri,  5 Jan 2024 11:40:21 +0800	[thread overview]
Message-ID: <20240105034021.30177-3-chenglulu@loongson.cn> (raw)
In-Reply-To: <20240105034021.30177-1-chenglulu@loongson.cn>

Instructions pcalau12i, addi.d, lu32i.d and lu52i.d must be adjancent so that the
linker can infer the PC of pcalau12i to apply relocations to lu32i.d and lu52i.d.
Otherwise, the results would be incorrect if these four instructions are not in
the same 4KiB page.

See the link for details:
https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc#extreme-code-model.

gcc/ChangeLog:

	* config/loongarch/loongarch.cc (loongarch_symbol_extreme_p): Add
	function declaration.
	(loongarch_explicit_relocs_p): Use the macro instruction to get
	the symbol address when loongarch_symbol_extreme_p returns true.

gcc/testsuite/ChangeLog:

	* gcc.target/loongarch/attr-model-1.c: Modify the content of the search
	string in the test case.
	* gcc.target/loongarch/attr-model-2.c: Likewise.
	* gcc.target/loongarch/attr-model-3.c: Likewise.
	* gcc.target/loongarch/attr-model-4.c: Likewise.
	* gcc.target/loongarch/func-call-extreme-1.c: Likewise.
	* gcc.target/loongarch/func-call-extreme-2.c: Likewise.
	* gcc.target/loongarch/func-call-extreme-3.c: Likewise.
	* gcc.target/loongarch/func-call-extreme-4.c: Likewise.
---
 gcc/config/loongarch/loongarch.cc                     | 11 +++++++++++
 gcc/testsuite/gcc.target/loongarch/attr-model-1.c     |  2 +-
 gcc/testsuite/gcc.target/loongarch/attr-model-2.c     |  2 +-
 gcc/testsuite/gcc.target/loongarch/attr-model-3.c     |  2 +-
 gcc/testsuite/gcc.target/loongarch/attr-model-4.c     |  2 +-
 .../gcc.target/loongarch/func-call-extreme-1.c        |  6 +++---
 .../gcc.target/loongarch/func-call-extreme-2.c        |  6 +++---
 .../gcc.target/loongarch/func-call-extreme-3.c        |  6 +++---
 .../gcc.target/loongarch/func-call-extreme-4.c        |  6 +++---
 9 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc
index 6a3321327ea..3b4b28f3bcc 100644
--- a/gcc/config/loongarch/loongarch.cc
+++ b/gcc/config/loongarch/loongarch.cc
@@ -264,6 +264,9 @@ const char *const
 loongarch_fp_conditions[16]= {LARCH_FP_CONDITIONS (STRINGIFY)};
 #undef STRINGIFY
 
+static bool
+loongarch_symbol_extreme_p (enum loongarch_symbol_type type);
+
 /* Size of guard page.  */
 #define STACK_CLASH_PROTECTION_GUARD_SIZE \
   (1 << param_stack_clash_protection_guard_size)
@@ -1963,6 +1966,14 @@ loongarch_symbolic_constant_p (rtx x, enum loongarch_symbol_type *symbol_type)
 bool
 loongarch_explicit_relocs_p (enum loongarch_symbol_type type)
 {
+  /* Instructions pcalau12i, addi.d, lu32i.d and lu52i.d must be adjancent
+     so that the linker can infer the PC of pcalau12i to apply relocations
+     to lu32i.d and lu52i.d.  Otherwise, the results would be incorrect if
+     these four instructions are not in the same 4KiB page.
+     Therefore, macro instructions are used when cmodel=extreme.  */
+  if (loongarch_symbol_extreme_p (type))
+    return false;
+
   if (la_opt_explicit_relocs != EXPLICIT_RELOCS_AUTO)
     return la_opt_explicit_relocs == EXPLICIT_RELOCS_ALWAYS;
 
diff --git a/gcc/testsuite/gcc.target/loongarch/attr-model-1.c b/gcc/testsuite/gcc.target/loongarch/attr-model-1.c
index 916d715b98b..65acb29162c 100644
--- a/gcc/testsuite/gcc.target/loongarch/attr-model-1.c
+++ b/gcc/testsuite/gcc.target/loongarch/attr-model-1.c
@@ -1,6 +1,6 @@
 /* { dg-do compile } */
 /* { dg-options "-mexplicit-relocs -mcmodel=normal -O2" } */
-/* { dg-final { scan-assembler-times "%pc64_hi12" 2 } } */
+/* { dg-final { scan-assembler-times "la\.local\t\\\$r\[0-9\]+,\\\$r15," 2 } } */
 
 #define ATTR_MODEL_TEST
 #include "attr-model-test.c"
diff --git a/gcc/testsuite/gcc.target/loongarch/attr-model-2.c b/gcc/testsuite/gcc.target/loongarch/attr-model-2.c
index a74c795ac3e..cf0f079e39a 100644
--- a/gcc/testsuite/gcc.target/loongarch/attr-model-2.c
+++ b/gcc/testsuite/gcc.target/loongarch/attr-model-2.c
@@ -1,6 +1,6 @@
 /* { dg-do compile } */
 /* { dg-options "-mexplicit-relocs -mcmodel=extreme -O2" } */
-/* { dg-final { scan-assembler-times "%pc64_hi12" 3 } } */
+/* { dg-final { scan-assembler-times "la\.local\t\\\$r\[0-9\]+,\\\$r15," 3 } } */
 
 #define ATTR_MODEL_TEST
 #include "attr-model-test.c"
diff --git a/gcc/testsuite/gcc.target/loongarch/attr-model-3.c b/gcc/testsuite/gcc.target/loongarch/attr-model-3.c
index 5622d508678..7c270d462f7 100644
--- a/gcc/testsuite/gcc.target/loongarch/attr-model-3.c
+++ b/gcc/testsuite/gcc.target/loongarch/attr-model-3.c
@@ -1,6 +1,6 @@
 /* { dg-do compile } */
 /* { dg-options "-mexplicit-relocs=auto -mcmodel=normal -O2" } */
-/* { dg-final { scan-assembler-times "%pc64_hi12" 2 } } */
+/* { dg-final { scan-assembler-times "la\.local\t\\\$r\[0-9\]+,\\\$r15," 2 } } */
 
 #define ATTR_MODEL_TEST
 #include "attr-model-test.c"
diff --git a/gcc/testsuite/gcc.target/loongarch/attr-model-4.c b/gcc/testsuite/gcc.target/loongarch/attr-model-4.c
index 482724bb974..627d630c36d 100644
--- a/gcc/testsuite/gcc.target/loongarch/attr-model-4.c
+++ b/gcc/testsuite/gcc.target/loongarch/attr-model-4.c
@@ -1,6 +1,6 @@
 /* { dg-do compile } */
 /* { dg-options "-mexplicit-relocs=auto -mcmodel=extreme -O2" } */
-/* { dg-final { scan-assembler-times "%pc64_hi12" 3 } } */
+/* { dg-final { scan-assembler-times "la\.local\t\\\$r\[0-9\]+,\\\$r15," 3 } } */
 
 #define ATTR_MODEL_TEST
 #include "attr-model-test.c"
diff --git a/gcc/testsuite/gcc.target/loongarch/func-call-extreme-1.c b/gcc/testsuite/gcc.target/loongarch/func-call-extreme-1.c
index db1e0f85396..46318f3d23f 100644
--- a/gcc/testsuite/gcc.target/loongarch/func-call-extreme-1.c
+++ b/gcc/testsuite/gcc.target/loongarch/func-call-extreme-1.c
@@ -1,8 +1,8 @@
 /* { dg-do compile } */
 /* { dg-options "-mabi=lp64d -O0 -fno-pic -fno-plt -mexplicit-relocs -mcmodel=extreme" } */
-/* { dg-final { scan-assembler "test:.*pcalau12i.*%got_pc_hi20.*\n\taddi\.d.*%got_pc_lo12.*\n\tlu32i\.d.*%got64_pc_lo20.*\n\tlu52i\.d.*%got64_pc_hi12.*\n\tldx\.d" } } */
-/* { dg-final { scan-assembler "test1:.*pcalau12i.*%pc_hi20.*\n\taddi\.d.*%pc_lo12.*\n\tlu32i\.d.*%pc64_lo20.*\n\tlu52i\.d.*pc64_hi12.*\n\tadd\.d" } } */
-/* { dg-final { scan-assembler "test2:.*pcalau12i.*%pc_hi20.*\n\taddi\.d.*%pc_lo12.*\n\tlu32i\.d.*%pc64_lo20.*\n\tlu52i\.d.*pc64_hi12.*\n\tadd\.d" } } */
+/* { dg-final { scan-assembler "test:.*la\.global.*,\\\$r15," } } */
+/* { dg-final { scan-assembler "test1:.*la\.local.*,\\\$r15," } } */
+/* { dg-final { scan-assembler "test2:.*la\.local.*,\\\$r15," } } */
 
 extern void g (void);
 void
diff --git a/gcc/testsuite/gcc.target/loongarch/func-call-extreme-2.c b/gcc/testsuite/gcc.target/loongarch/func-call-extreme-2.c
index 21bf81ae837..14b6e658ca1 100644
--- a/gcc/testsuite/gcc.target/loongarch/func-call-extreme-2.c
+++ b/gcc/testsuite/gcc.target/loongarch/func-call-extreme-2.c
@@ -1,8 +1,8 @@
 /* { dg-do compile } */
 /* { dg-options "-mabi=lp64d -O0 -fpic -fno-plt -mexplicit-relocs -mcmodel=extreme" } */
-/* { dg-final { scan-assembler "test:.*pcalau12i.*%got_pc_hi20.*\n\taddi\.d.*%got_pc_lo12.*\n\tlu32i\.d.*%got64_pc_lo20.*\n\tlu52i\.d.*%got64_pc_hi12.*\n\tldx\.d" } } */
-/* { dg-final { scan-assembler "test1:.*pcalau12i.*%got_pc_hi20.*\n\taddi\.d.*%got_pc_lo12.*\n\tlu32i\.d.*%got64_pc_lo20.*\n\tlu52i\.d.*%got64_pc_hi12.*\n\tldx\.d" } } */
-/* { dg-final { scan-assembler "test2:.*pcalau12i.*%pc_hi20.*\n\taddi\.d.*%pc_lo12.*\n\tlu32i\.d.*%pc64_lo20.*\n\tlu52i\.d.*pc64_hi12.*\n\tadd\.d" } } */
+/* { dg-final { scan-assembler "test:.*la\.global.*,\\\$r15," } } */
+/* { dg-final { scan-assembler "test1:.*la\.global.*,\\\$r15," } } */
+/* { dg-final { scan-assembler "test2:.*la\.local.*,\\\$r15," } } */
 
 extern void g (void);
 void
diff --git a/gcc/testsuite/gcc.target/loongarch/func-call-extreme-3.c b/gcc/testsuite/gcc.target/loongarch/func-call-extreme-3.c
index a4da44b4a3d..2ccbd2deb7c 100644
--- a/gcc/testsuite/gcc.target/loongarch/func-call-extreme-3.c
+++ b/gcc/testsuite/gcc.target/loongarch/func-call-extreme-3.c
@@ -1,7 +1,7 @@
 /* { dg-do compile } */
 /* { dg-options "-mabi=lp64d -O0 -fno-pic -fno-plt -mexplicit-relocs=auto -mcmodel=extreme" } */
-/* { dg-final { scan-assembler "test:.*pcalau12i.*%got_pc_hi20.*\n\taddi\.d.*%got_pc_lo12.*\n\tlu32i\.d.*%got64_pc_lo20.*\n\tlu52i\.d.*%got64_pc_hi12.*\n\tldx\.d" } } */
-/* { dg-final { scan-assembler "test1:.*pcalau12i.*%pc_hi20.*\n\taddi\.d.*%pc_lo12.*\n\tlu32i\.d.*%pc64_lo20.*\n\tlu52i\.d.*pc64_hi12.*\n\tadd\.d" } } */
-/* { dg-final { scan-assembler "test2:.*pcalau12i.*%pc_hi20.*\n\taddi\.d.*%pc_lo12.*\n\tlu32i\.d.*%pc64_lo20.*\n\tlu52i\.d.*pc64_hi12.*\n\tadd\.d" } } */
+/* { dg-final { scan-assembler "test:.*la\.global.*,\\\$r15," } } */
+/* { dg-final { scan-assembler "test1:.*la\.local.*,\\\$r15," } } */
+/* { dg-final { scan-assembler "test2:.*la\.local.*,\\\$r15," } } */
 
 #include "func-call-extreme-1.c"
diff --git a/gcc/testsuite/gcc.target/loongarch/func-call-extreme-4.c b/gcc/testsuite/gcc.target/loongarch/func-call-extreme-4.c
index 16b00f4c5f2..0067024ef7d 100644
--- a/gcc/testsuite/gcc.target/loongarch/func-call-extreme-4.c
+++ b/gcc/testsuite/gcc.target/loongarch/func-call-extreme-4.c
@@ -1,7 +1,7 @@
 /* { dg-do compile } */
 /* { dg-options "-mabi=lp64d -O0 -fpic -fno-plt -mexplicit-relocs=auto -mcmodel=extreme" } */
-/* { dg-final { scan-assembler "test:.*pcalau12i.*%got_pc_hi20.*\n\taddi\.d.*%got_pc_lo12.*\n\tlu32i\.d.*%got64_pc_lo20.*\n\tlu52i\.d.*%got64_pc_hi12.*\n\tldx\.d" } } */
-/* { dg-final { scan-assembler "test1:.*pcalau12i.*%got_pc_hi20.*\n\taddi\.d.*%got_pc_lo12.*\n\tlu32i\.d.*%got64_pc_lo20.*\n\tlu52i\.d.*%got64_pc_hi12.*\n\tldx\.d" } } */
-/* { dg-final { scan-assembler "test2:.*pcalau12i.*%pc_hi20.*\n\taddi\.d.*%pc_lo12.*\n\tlu32i\.d.*%pc64_lo20.*\n\tlu52i\.d.*pc64_hi12.*\n\tadd\.d" } } */
+/* { dg-final { scan-assembler "test:.*la\.global.*,\\\$r15," } } */
+/* { dg-final { scan-assembler "test1:.*la\.global.*,\\\$r15," } } */
+/* { dg-final { scan-assembler "test2:.*la\.local.*,\\\$r15," } } */
 
 #include "func-call-extreme-1.c"
-- 
2.39.3


  parent reply	other threads:[~2024-01-05  3:40 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-05  3:40 [PATCH v2 0/2] When cmodel=extreme, add macro support and only support macros Lulu Cheng
2024-01-05  3:40 ` [PATCH v2 1/2] LoongArch: Add the macro implementation of mcmodel=extreme Lulu Cheng
2024-01-05  3:40 ` Lulu Cheng [this message]
2024-01-05  8:37   ` [PATCH v2 2/2] LoongArch: When the code model is extreme, the symbol address is obtained through macro instructions regardless of the value of -mexplicit-relocs Xi Ruoyao
2024-01-05  8:51     ` chenglulu
2024-01-05  9:57     ` chenglulu
2024-01-05 10:25       ` Xi Ruoyao
2024-01-05 11:55         ` Xi Ruoyao
2024-01-05 12:45           ` chenglulu
2024-01-05 14:16             ` Xi Ruoyao
2024-01-12  1:46               ` chenglulu
2024-01-12 11:42                 ` Xi Ruoyao
2024-01-13  7:01                   ` chenglulu
2024-01-13 13:05                     ` Xi Ruoyao
2024-01-13 14:05                       ` chenglulu
2024-01-17  9:38                       ` chenglulu
2024-01-17  9:50                         ` Xi Ruoyao
2024-01-17  9:57                           ` chenglulu
2024-01-19  5:46                             ` Xi Ruoyao
2024-01-19  8:51                               ` chenglulu
2024-01-22  7:27                                 ` chenglulu
2024-01-23 19:36                                   ` Xi Ruoyao
2024-01-25  0:48                                     ` chenglulu
2024-01-25  7:59                                       ` Xi Ruoyao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240105034021.30177-3-chenglulu@loongson.cn \
    --to=chenglulu@loongson.cn \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=i@xen0n.name \
    --cc=xry111@xry111.site \
    --cc=xuchenghua@loongson.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).