From af9345d7df615812a078bb942cadd4c317c3e3de Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Tue, 22 Dec 2015 15:38:25 +0000 Subject: [PATCH] [PATCH] Use call-clobbered register for sibcall via GOT From: H.J. Lu Since sibcall never returns, we can only use call-clobbered register as GOT base. Otherwise, callee-saved register used as GOT base won't be properly restored. sibcall_memory_operand is changed to allow 32-bit GOT slot only with pseudo register as GOT base for RTL expansion. 2 new patterns, *sibcall_GOT_32 and *sibcall_value_GOT_32, are added to expose GOT base register to register allocator so that call-clobbered register will be used for GOT base. gcc/ PR target/68937 * config/i386/i386.c (ix86_function_ok_for_sibcall): Count call to global function via GOT slot as indirect call. * config/i386/i386.md (*sibcall_GOT_32): New pattern. (*sibcall_value_GOT_32): Likewise. * config/i386/predicates.md (sibcall_memory_operand): Rewrite. Allow 32-bit GOT slot only with pseudo register as GOT base. (GOT32_symbol_operand): New predicate. gcc/testsuite/ PR target/68937 * gcc.target/i386/pr68937-1.c: New test. * gcc.target/i386/pr68937-2.c: Likewise. * gcc.target/i386/pr68937-3.c: Likewise. * gcc.target/i386/pr68937-4.c: Likewise. * gcc.target/i386/pr68937-5.c: Likewise. * gcc.target/i386/pr68937-6.c: Likewise. From-SVN: r231903 --- gcc/ChangeLog | 11 ++++++++ gcc/config/i386/i386.c | 7 +++-- gcc/config/i386/i386.md | 33 ++++++++++++++++++++++ gcc/config/i386/predicates.md | 34 ++++++++++++++++++----- gcc/testsuite/ChangeLog | 10 +++++++ gcc/testsuite/gcc.target/i386/pr68937-1.c | 13 +++++++++ gcc/testsuite/gcc.target/i386/pr68937-2.c | 13 +++++++++ gcc/testsuite/gcc.target/i386/pr68937-3.c | 13 +++++++++ gcc/testsuite/gcc.target/i386/pr68937-4.c | 13 +++++++++ gcc/testsuite/gcc.target/i386/pr68937-5.c | 9 ++++++ gcc/testsuite/gcc.target/i386/pr68937-6.c | 16 +++++++++++ 11 files changed, 163 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/gcc.target/i386/pr68937-1.c create mode 100644 gcc/testsuite/gcc.target/i386/pr68937-2.c create mode 100644 gcc/testsuite/gcc.target/i386/pr68937-3.c create mode 100644 gcc/testsuite/gcc.target/i386/pr68937-4.c create mode 100644 gcc/testsuite/gcc.target/i386/pr68937-5.c create mode 100644 gcc/testsuite/gcc.target/i386/pr68937-6.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 134e4fb584d..0cf2dc48823 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,14 @@ +2015-12-22 H.J. Lu + + PR target/68937 + * config/i386/i386.c (ix86_function_ok_for_sibcall): Count + call to global function via GOT slot as indirect call. + * config/i386/i386.md (*sibcall_GOT_32): New pattern. + (*sibcall_value_GOT_32): Likewise. + * config/i386/predicates.md (sibcall_memory_operand): Rewrite. + Allow 32-bit GOT slot only with pseudo register as GOT base. + (GOT32_symbol_operand): New predicate. + 2015-12-22 Sujoy Saraswati * fold-const.c (const_binop): Convert sNaN to qNaN when diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c index cecea2496a9..f5d23d9b947 100644 --- a/gcc/config/i386/i386.c +++ b/gcc/config/i386/i386.c @@ -6657,6 +6657,7 @@ ix86_function_ok_for_sibcall (tree decl, tree exp) { tree type, decl_or_type; rtx a, b; + bool bind_global = decl && !targetm.binds_local_p (decl); /* If we are generating position-independent code, we cannot sibcall optimize direct calls to global functions, as the PLT requires @@ -6665,7 +6666,7 @@ ix86_function_ok_for_sibcall (tree decl, tree exp) && !TARGET_64BIT && flag_pic && flag_plt - && decl && !targetm.binds_local_p (decl)) + && bind_global) return false; /* If we need to align the outgoing stack, then sibcalling would @@ -6723,8 +6724,10 @@ ix86_function_ok_for_sibcall (tree decl, tree exp) /* If this call is indirect, we'll need to be able to use a call-clobbered register for the address of the target function. Make sure that all such registers are not used for passing - parameters. Note that DLLIMPORT functions are indirect. */ + parameters. Note that DLLIMPORT functions and call to global + function via GOT slot are indirect. */ if (!decl + || (bind_global && flag_pic && !flag_plt) || (TARGET_DLLIMPORT_DECL_ATTRIBUTES && DECL_DLLIMPORT_P (decl))) { /* Check if regparm >= 3 since arg_reg_available is set to diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md index e8178f3f9b6..5e5c97bf515 100644 --- a/gcc/config/i386/i386.md +++ b/gcc/config/i386/i386.md @@ -11865,6 +11865,22 @@ "* return ix86_output_call_insn (insn, operands[0]);" [(set_attr "type" "call")]) +;; Since sibcall never returns, we can only use call-clobbered register +;; as GOT base. +(define_insn "*sibcall_GOT_32" + [(call (mem:QI + (mem:SI (plus:SI + (match_operand:SI 0 "register_no_elim_operand" "U") + (match_operand:SI 1 "GOT32_symbol_operand")))) + (match_operand 2))] + "!TARGET_MACHO && !TARGET_64BIT && SIBLING_CALL_P (insn)" +{ + rtx fnaddr = gen_rtx_PLUS (Pmode, operands[0], operands[1]); + fnaddr = gen_const_mem (Pmode, fnaddr); + return ix86_output_call_insn (insn, fnaddr); +} + [(set_attr "type" "call")]) + (define_insn "*sibcall" [(call (mem:QI (match_operand:W 0 "sibcall_insn_operand" "UBsBz")) (match_operand 1))] @@ -12042,6 +12058,23 @@ "* return ix86_output_call_insn (insn, operands[1]);" [(set_attr "type" "callv")]) +;; Since sibcall never returns, we can only use call-clobbered register +;; as GOT base. +(define_insn "*sibcall_value_GOT_32" + [(set (match_operand 0) + (call (mem:QI + (mem:SI (plus:SI + (match_operand:SI 1 "register_no_elim_operand" "U") + (match_operand:SI 2 "GOT32_symbol_operand")))) + (match_operand 3)))] + "!TARGET_MACHO && !TARGET_64BIT && SIBLING_CALL_P (insn)" +{ + rtx fnaddr = gen_rtx_PLUS (Pmode, operands[1], operands[2]); + fnaddr = gen_const_mem (Pmode, fnaddr); + return ix86_output_call_insn (insn, fnaddr); +} + [(set_attr "type" "callv")]) + (define_insn "*sibcall_value" [(set (match_operand 0) (call (mem:QI (match_operand:W 1 "sibcall_insn_operand" "UBsBz")) diff --git a/gcc/config/i386/predicates.md b/gcc/config/i386/predicates.md index 8bdd5d88efb..96d946c63d3 100644 --- a/gcc/config/i386/predicates.md +++ b/gcc/config/i386/predicates.md @@ -597,14 +597,28 @@ (match_operand 0 "memory_operand")))) ;; Return true if OP is a memory operands that can be used in sibcalls. +;; Since sibcall never returns, we can only use call-clobbered register +;; as GOT base. Allow GOT slot here only with pseudo register as GOT +;; base. Properly handle sibcall over GOT slot with *sibcall_GOT_32 +;; and *sibcall_value_GOT_32 patterns. (define_predicate "sibcall_memory_operand" - (and (match_operand 0 "memory_operand") - (match_test "CONSTANT_P (XEXP (op, 0)) - || (GET_CODE (XEXP (op, 0)) == PLUS - && REG_P (XEXP (XEXP (op, 0), 0)) - && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST - && GET_CODE (XEXP (XEXP (XEXP (op, 0), 1), 0)) == UNSPEC - && XINT (XEXP (XEXP (XEXP (op, 0), 1), 0), 1) == UNSPEC_GOT)"))) + (match_operand 0 "memory_operand") +{ + op = XEXP (op, 0); + if (CONSTANT_P (op)) + return true; + if (GET_CODE (op) == PLUS && REG_P (XEXP (op, 0))) + { + int regno = REGNO (XEXP (op, 0)); + if (!HARD_REGISTER_NUM_P (regno) || call_used_regs[regno]) + { + op = XEXP (op, 1); + if (GOT32_symbol_operand (op, VOIDmode)) + return true; + } + } + return false; +}) ;; Test for a valid operand for a call instruction. ;; Allow constant call address operands in Pmode only. @@ -633,6 +647,12 @@ && XINT (XEXP (op, 0), 1) == UNSPEC_GOTPCREL); }) +;; Return true if OP is a 32-bit GOT symbol operand. +(define_predicate "GOT32_symbol_operand" + (match_test "GET_CODE (op) == CONST + && GET_CODE (XEXP (op, 0)) == UNSPEC + && XINT (XEXP (op, 0), 1) == UNSPEC_GOT")) + ;; Match exactly zero. (define_predicate "const0_operand" (match_code "const_int,const_wide_int,const_double,const_vector") diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 50733e6b46c..b43cfee63e3 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,13 @@ +2015-12-22 H.J. Lu + + PR target/68937 + * gcc.target/i386/pr68937-1.c: New test. + * gcc.target/i386/pr68937-2.c: Likewise. + * gcc.target/i386/pr68937-3.c: Likewise. + * gcc.target/i386/pr68937-4.c: Likewise. + * gcc.target/i386/pr68937-5.c: Likewise. + * gcc.target/i386/pr68937-6.c: Likewise. + 2015-12-22 Marek Polacek PR c/69002 diff --git a/gcc/testsuite/gcc.target/i386/pr68937-1.c b/gcc/testsuite/gcc.target/i386/pr68937-1.c new file mode 100644 index 00000000000..897856bc590 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr68937-1.c @@ -0,0 +1,13 @@ +/* { dg-do compile { target { *-*-linux* && ia32 } } } */ +/* { dg-options "-O2 -fpic -fno-plt -mregparm=3" } */ + +extern void bar (int); + +void +foo (int b) +{ + bar (b); + bar (b); +} + +/* { dg-final { scan-assembler "jmp\[ \t\]*.bar@GOT\\(%e(a|c|d)x\\)" } } */ diff --git a/gcc/testsuite/gcc.target/i386/pr68937-2.c b/gcc/testsuite/gcc.target/i386/pr68937-2.c new file mode 100644 index 00000000000..257f4e267f4 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr68937-2.c @@ -0,0 +1,13 @@ +/* { dg-do compile { target { *-*-linux* && ia32 } } } */ +/* { dg-options "-O2 -fpic -fno-plt -mregparm=3" } */ + +extern void bar (int, int); + +void +foo (int a, int b) +{ + bar (a, b); + bar (a, b); +} + +/* { dg-final { scan-assembler "jmp\[ \t\]*.bar@GOT\\(%e(a|c|d)x\\)" } } */ diff --git a/gcc/testsuite/gcc.target/i386/pr68937-3.c b/gcc/testsuite/gcc.target/i386/pr68937-3.c new file mode 100644 index 00000000000..6d8e40ff769 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr68937-3.c @@ -0,0 +1,13 @@ +/* { dg-do compile { target { *-*-linux* && ia32 } } } */ +/* { dg-options "-O2 -fpic -fno-plt -mregparm=3" } */ + +extern void bar (int, int, int); + +void +foo (int a, int b, int c) +{ + bar (a, b, c); + bar (a, b, c); +} + +/* { dg-final { scan-assembler-not "jmp\[ \t\]*.bar@GOT" } } */ diff --git a/gcc/testsuite/gcc.target/i386/pr68937-4.c b/gcc/testsuite/gcc.target/i386/pr68937-4.c new file mode 100644 index 00000000000..9c199568d66 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr68937-4.c @@ -0,0 +1,13 @@ +/* { dg-do compile { target { *-*-linux* && ia32 } } } */ +/* { dg-options "-O2 -fpic -fno-plt -mregparm=3" } */ + +extern int bar (int, int); + +int +foo (int a, int b) +{ + (void) bar (a, b); + return bar (a, b); +} + +/* { dg-final { scan-assembler "jmp\[ \t\]*.bar@GOT\\(%e(a|c|d)x\\)" } } */ diff --git a/gcc/testsuite/gcc.target/i386/pr68937-5.c b/gcc/testsuite/gcc.target/i386/pr68937-5.c new file mode 100644 index 00000000000..f7e3ec5ef1e --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr68937-5.c @@ -0,0 +1,9 @@ +/* { dg-do compile { target { *-*-linux* } } } */ +/* { dg-options "-O2 -fpic -fno-plt -funroll-loops" } */ + +extern void *f(); +void dmi_scan_machine(void) { + char *p = f(), *q; + for (q = p; q < p + 10; q++) + ; +} diff --git a/gcc/testsuite/gcc.target/i386/pr68937-6.c b/gcc/testsuite/gcc.target/i386/pr68937-6.c new file mode 100644 index 00000000000..406ce282d94 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr68937-6.c @@ -0,0 +1,16 @@ +/* { dg-do compile { target { *-*-linux* && ia32 } } } */ +/* { dg-options "-O2 -fpic -fno-plt -mregparm=3" } */ + +extern void foo (int, int, int); +extern void bar (int, int, int) __attribute__((visibility("hidden"))); + +void +foo (int a, int b, int c) +{ + foo (a, b, c); + bar (a, b, c); + foo (a, b, c); + bar (a, b, c); +} + +/* { dg-final { scan-assembler "jmp\[ \t\]bar" } } */ -- 2.30.2