On Wed, Mar 17, 2021 at 12:40 AM Uros Bizjak <ubiz...@gmail.com> wrote:
>
> On Tue, Mar 16, 2021 at 4:59 PM H.J. Lu <hjl.to...@gmail.com> wrote:
> >
> > On Sun, Mar 14, 2021 at 1:31 PM H.J. Lu <hjl.to...@gmail.com> wrote:
> > >
> > > On Sun, Mar 14, 2021 at 12:43 PM Uros Bizjak <ubiz...@gmail.com> wrote:
> > > >
> > > > On Sun, Mar 14, 2021 at 8:14 PM H.J. Lu <hjl.to...@gmail.com> wrote:
> > > > > > > Done.  Here is the updated patch.  Tested on Linux/x86-64.  OK 
> > > > > > > for master?
> > > > > >
> > > > > > I don't understand the purpose of the current_output_insn check and 
> > > > > > I
> > > > > > don't know if the usage of current_output_insn is correct. The
> > > > > > comments are not helpful either, and no other target uses this
> > > > > > variable in the way you propose. Can you please elaborate the reason
> > > > > > and the purpose of the check a bit more?
> > > > > >
> > > > > > Uros.
> > > > >
> > > > > Originally, ix86_force_load_from_GOT_p is only for non-PIC.   My 
> > > > > patch extended
> > > > > it to inline assembly statements where current_output_insn == NULL 
> > > > > and PIC is
> > > > > allowed in 64-bit.
> > > >
> > > > I can see this from the patch, but this explanation didn't answer my 
> > > > question.
> > > >
> > >
> > > The purpose of current_output_insn == NULL is to allow PIC for inline
> > > asm statements in 64-bit mode.  Is there a better way to check if
> > > ix86_print_operand () is called on inline asm statements?
> > >
> >
> > Here is the v4 patch to check this_is_asm_operands for inline
> >  asm statements.   OK for master?
>
> -ENOPATCH.
>
> Uros.

Here is the patch.

-- 
H.J.
From f3702d56973b241f50777f4749d043df7b905f0f Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.to...@gmail.com>
Date: Thu, 11 Mar 2021 06:48:24 -0800
Subject: [PATCH v4] x86: Update 'P' operand modifier for -fno-plt

Update 'P' operand modifier for -fno-plt to support inline assembly
statements.  In 64-bit, we can always load function address with
@GOTPCREL.  In 32-bit, we load function address with @GOT only for
non-PIC since PIC register may not be available at call site.

gcc/

	PR target/99504
	* config/i386/i386.c (ix86_force_load_from_GOT_p): Support
	inline assembly statements.
	(ix86_print_operand): Update 'P' handling for -fno-plt.

gcc/testsuite/

	PR target/99504
	* gcc.target/i386/pr99530-1.c: New test.
	* gcc.target/i386/pr99530-2.c: Likewise.
	* gcc.target/i386/pr99530-3.c: Likewise.
	* gcc.target/i386/pr99530-4.c: Likewise.
	* gcc.target/i386/pr99530-5.c: Likewise.
	* gcc.target/i386/pr99530-6.c: Likewise.
---
 gcc/config/i386/i386.c                    | 32 +++++++++++++++++++----
 gcc/testsuite/gcc.target/i386/pr99530-1.c | 11 ++++++++
 gcc/testsuite/gcc.target/i386/pr99530-2.c | 11 ++++++++
 gcc/testsuite/gcc.target/i386/pr99530-3.c | 11 ++++++++
 gcc/testsuite/gcc.target/i386/pr99530-4.c | 11 ++++++++
 gcc/testsuite/gcc.target/i386/pr99530-5.c | 11 ++++++++
 gcc/testsuite/gcc.target/i386/pr99530-6.c | 11 ++++++++
 7 files changed, 93 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/i386/pr99530-1.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr99530-2.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr99530-3.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr99530-4.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr99530-5.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr99530-6.c

diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
index 2603333f87b..701b4adcf11 100644
--- a/gcc/config/i386/i386.c
+++ b/gcc/config/i386/i386.c
@@ -10265,15 +10265,19 @@ darwin_local_data_pic (rtx disp)
 	  && XINT (disp, 1) == UNSPEC_MACHOPIC_OFFSET);
 }
 
-/* True if operand X should be loaded from GOT.  */
+/* True if the function symbol operand X should be loaded from GOT.
+   NB: For inline assembly statement, allow PIC/non-PIC in 64-bit mode
+   and in 32-bit mode, only non-PIC is allowed since we don't have
+   PIC register at call site.  Otherwise, only PIC is allowed.  */
 
 bool
 ix86_force_load_from_GOT_p (rtx x)
 {
-  return ((TARGET_64BIT || HAVE_AS_IX86_GOT32X)
+  return ((TARGET_64BIT || (!flag_pic && HAVE_AS_IX86_GOT32X))
 	  && !TARGET_PECOFF && !TARGET_MACHO
-	  && !flag_pic
+	  && (!flag_pic || this_is_asm_operands)
 	  && ix86_cmodel != CM_LARGE
+	  && ix86_cmodel != CM_LARGE_PIC
 	  && GET_CODE (x) == SYMBOL_REF
 	  && SYMBOL_REF_FUNCTION_P (x)
 	  && (!flag_plt
@@ -12701,7 +12705,8 @@ print_reg (rtx x, int code, FILE *file)
    y -- print "st(0)" instead of "st" as a register.
    d -- print duplicated register operand for AVX instruction.
    D -- print condition for SSE cmp instruction.
-   P -- if PIC, print an @PLT suffix.
+   P -- if PIC, print an @PLT suffix.  For -fno-plt, load function
+	address from GOT.
    p -- print raw symbol name.
    X -- don't print any sort of PIC '@' suffix for a symbol.
    & -- print some in-use local-dynamic symbol name.
@@ -13445,7 +13450,24 @@ ix86_print_operand (FILE *file, rtx x, int code)
 	  x = const0_rtx;
 	}
 
-      if (code != 'P' && code != 'p')
+      if (code == 'P')
+	{
+	  if (ix86_force_load_from_GOT_p (x))
+	    {
+	      /* For inline assembly statement, load function address
+		 from GOT with 'P' operand modifier to avoid PLT.
+		 NB: This works only with call or jmp.  */
+	      x = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
+				  (TARGET_64BIT
+				   ? UNSPEC_GOTPCREL
+				   : UNSPEC_GOT));
+	      x = gen_rtx_CONST (Pmode, x);
+	      x = gen_const_mem (Pmode, x);
+	      ix86_print_operand (file, x, 'A');
+	      return;
+	    }
+	}
+      else if (code != 'p')
 	{
 	  if (CONST_INT_P (x))
 	    {
diff --git a/gcc/testsuite/gcc.target/i386/pr99530-1.c b/gcc/testsuite/gcc.target/i386/pr99530-1.c
new file mode 100644
index 00000000000..080d7cc9399
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr99530-1.c
@@ -0,0 +1,11 @@
+/* { dg-do compile { target { i?86-*-linux* x86_64-*-linux* } } } */
+/* { dg-require-effective-target lp64 } */
+/* { dg-options "-O2 -fpic -mcmodel=large -fno-plt" } */
+/* { dg-final { scan-assembler-not "foo@GOTPCREL" } } */
+
+extern void foo (void); 
+void
+bar (void)
+{
+  asm ("call %P0" : : "X" (foo));
+} 
diff --git a/gcc/testsuite/gcc.target/i386/pr99530-2.c b/gcc/testsuite/gcc.target/i386/pr99530-2.c
new file mode 100644
index 00000000000..9808957d624
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr99530-2.c
@@ -0,0 +1,11 @@
+/* { dg-do compile { target { i?86-*-linux* x86_64-*-linux* } } } */
+/* { dg-require-effective-target lp64 } */
+/* { dg-options "-O2 -fno-pic -mcmodel=large -fno-plt" } */
+/* { dg-final { scan-assembler-not "foo@GOTPCREL" } } */
+
+extern void foo (void); 
+void
+bar (void)
+{
+  asm ("call %P0" : : "X" (foo));
+} 
diff --git a/gcc/testsuite/gcc.target/i386/pr99530-3.c b/gcc/testsuite/gcc.target/i386/pr99530-3.c
new file mode 100644
index 00000000000..22fe81b25f2
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr99530-3.c
@@ -0,0 +1,11 @@
+/* { dg-do compile { target { i?86-*-linux* x86_64-*-linux* } } } */
+/* { dg-options "-O2 -fno-pic -fno-plt" } */
+/* { dg-final { scan-assembler "call\[ \t\]+\[*\]foo@GOTPCREL" { target { ! ia32 } } } } */
+/* { dg-final { scan-assembler "call\[ \t\]+\[*\]foo@GOT" { target { ia32 && got32x_reloc } } } } */
+
+extern void foo (void); 
+void
+bar (void)
+{
+  asm ("call %P0" : : "X" (foo));
+} 
diff --git a/gcc/testsuite/gcc.target/i386/pr99530-4.c b/gcc/testsuite/gcc.target/i386/pr99530-4.c
new file mode 100644
index 00000000000..aba0b5d7892
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr99530-4.c
@@ -0,0 +1,11 @@
+/* { dg-do compile { target { i?86-*-linux* x86_64-*-linux* } } } */
+/* { dg-options "-O2 -fpic -fno-plt" } */
+/* { dg-final { scan-assembler "call\[ \t\]+\[*\]foo@GOTPCREL" { target { ! ia32 } } } } */
+/* { dg-final { scan-assembler-not "foo@GOT" { target { ia32 } } } } */
+
+extern void foo (void);
+void
+bar (void)
+{
+  asm ("call %P0" : : "X" (foo));
+} 
diff --git a/gcc/testsuite/gcc.target/i386/pr99530-5.c b/gcc/testsuite/gcc.target/i386/pr99530-5.c
new file mode 100644
index 00000000000..02c5799d273
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr99530-5.c
@@ -0,0 +1,11 @@
+/* { dg-do compile { target { i?86-*-linux* x86_64-*-linux* } } } */
+/* { dg-options "-O2 -fno-pic" } */
+/* { dg-final { scan-assembler "call\[ \t\]+\[*\]foo@GOTPCREL" { target { ! ia32 } } } } */
+/* { dg-final { scan-assembler "call\[ \t\]+\[*\]foo@GOT" { target { ia32 && got32x_reloc } } } } */
+
+extern void foo (void) __attribute__ ((noplt)); 
+void
+bar (void)
+{
+  asm ("call %P0" : : "X" (foo));
+} 
diff --git a/gcc/testsuite/gcc.target/i386/pr99530-6.c b/gcc/testsuite/gcc.target/i386/pr99530-6.c
new file mode 100644
index 00000000000..0dfdd3752f7
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr99530-6.c
@@ -0,0 +1,11 @@
+/* { dg-do compile { target { i?86-*-linux* x86_64-*-linux* } } } */
+/* { dg-options "-O2 -fpic" } */
+/* { dg-final { scan-assembler "call\[ \t\]+\[*\]foo@GOTPCREL" { target { ! ia32 } } } } */
+/* { dg-final { scan-assembler-not "foo@GOT" { target { ia32 } } } } */
+
+extern void foo (void) __attribute__ ((noplt)); 
+void
+bar (void)
+{
+  asm ("call %P0" : : "X" (foo));
+} 
-- 
2.30.2

Reply via email to