Re: [PATCH] i386: Update -mfunction-return= for return with pop

2018-02-26 Thread H.J. Lu
On Mon, Feb 26, 2018 at 7:44 AM, Jan Hubicka <hubi...@ucw.cz> wrote:
>> On Mon, Feb 26, 2018 at 7:06 AM, Jan Hubicka <hubi...@ucw.cz> wrote:
>> >> On Mon, Feb 26, 2018 at 2:58 AM, Jan Hubicka <hubi...@ucw.cz> wrote:
>> >> >> On Mon, Feb 26, 2018 at 2:05 AM, Martin Liška <mli...@suse.cz> wrote:
>> >> >> > On 02/25/2018 02:37 AM, H.J. Lu wrote:
>> >> >> >>   PR target/r84530
>> >> >> >
>> >> >> > Just a nit I've noticed:
>> >> >> >
>> >> >> > s/r84530/84530
>> >> >>
>> >> >> Fixed.
>> >> >>
>> >> >> --
>> >> >> H.J.
>> >> >
>> >> >> From f08b8721ed038cefcee5a0bb1329b90a2d322269 Mon Sep 17 00:00:00 2001
>> >> >> From: "H.J. Lu" <hjl.to...@gmail.com>
>> >> >> Date: Sat, 24 Feb 2018 05:05:49 -0800
>> >> >> Subject: [PATCH] i386: Update -mfunction-return= for return with pop
>> >> >>
>> >> >> When -mfunction-return= is used, simple_return_pop_internal should pop
>> >> >> return address into ECX register, adjust stack by bytes to pop from 
>> >> >> stack
>> >> >> and jump to the return thunk via ECX register.
>> >> >>
>> >> >> Tested on i686 and x86-64.
>> >> >>
>> >> >>   PR target/84530
>> >> >>   * config/i386/i386-protos.h (ix86_output_indirect_jmp): Remove
>> >> >>   the bool argument.
>> >> >>   (ix86_output_indirect_function_return): New prototype.
>> >> >>   (ix86_split_simple_return_pop_internal): Likewise.
>> >> >>   * config/i386/i386.c (indirect_return_via_cx): New.
>> >> >>   (indirect_return_via_cx_bnd): Likewise.
>> >> >>   (indirect_thunk_name): Handle return va CX_REG.
>> >> >>   (output_indirect_thunk_function): Create alias for
>> >> >>   __x86_return_thunk_[re]cx and __x86_return_thunk_[re]cx_bnd.
>> >> >>   (ix86_output_indirect_jmp): Remove the bool argument.
>> >> >>   (ix86_output_indirect_function_return): New function.
>> >> >>   (ix86_split_simple_return_pop_internal): Likewise.
>> >> >>   * config/i386/i386.md (*indirect_jump): Don't pass false
>> >> >>   to ix86_output_indirect_jmp.
>> >> >>   (*tablejump_1): Likewise.
>> >> >>   (simple_return_pop_internal): Change it to define_insn_and_split.
>> >> >>   Call ix86_split_simple_return_pop_internal to split it for
>> >> >>   -mfunction-return=.
>> >> >>   (simple_return_indirect_internal): Call
>> >> >>   ix86_output_indirect_function_return instead of
>> >> >>   ix86_output_indirect_jmp.
>> >> >
>> >> > It seems to make sense. Since LLVM has picked up our thunk names now,
>> >> > I wonder if they have same bug or someohow already assigned a name
>> >> > to this thunk. In the second case we probably ought to match it. Do
>> >> > you know what LLVM uses?
>> >> >
>> >>
>> >> I don't think LLVM has implemented -mfunction-return=.   I disclosed
>> >> my -mindirect-branch= approach to LLVM last year.  At the time, they
>> >> wanted freedom in their implementation and didn't want to be compatible
>> >> with mine.   Later they added GCC compatibility so that LLVM may be
>> >> used to compile Linux kernel.  I believe it will be the same case if this
>> >> option is used to compile Linux kernel.
>> >
>> > Patch is OK then, thanks!
>> >
>>
>> Checked into trunk.  OK for backport to GCC 7 branch after a few days?
> OK,

I checked in this patch to update comments.  I will backport it together to
GCC 7 branch.

Index: config/i386/i386.c
===
--- config/i386/i386.c (revision 258004)
+++ config/i386/i386.c (working copy)
@@ -28999,8 +28999,7 @@ ix86_output_indirect_branch (rtx call_op
 ix86_output_indirect_branch_via_push (call_op, xasm, sibcall_p);
 }

-/* Output indirect jump.  CALL_OP is the jump target.  Jump is a
-   function return if RET_P is true.  */
+/* Output indirect jump.  CALL_OP is the jump target.  */

 const char *
 ix86_output_indirect_jmp (rtx call_op)


-- 
H.J.


Re: [PATCH] i386: Update -mfunction-return= for return with pop

2018-02-26 Thread Jan Hubicka
> On Mon, Feb 26, 2018 at 7:06 AM, Jan Hubicka <hubi...@ucw.cz> wrote:
> >> On Mon, Feb 26, 2018 at 2:58 AM, Jan Hubicka <hubi...@ucw.cz> wrote:
> >> >> On Mon, Feb 26, 2018 at 2:05 AM, Martin Liška <mli...@suse.cz> wrote:
> >> >> > On 02/25/2018 02:37 AM, H.J. Lu wrote:
> >> >> >>   PR target/r84530
> >> >> >
> >> >> > Just a nit I've noticed:
> >> >> >
> >> >> > s/r84530/84530
> >> >>
> >> >> Fixed.
> >> >>
> >> >> --
> >> >> H.J.
> >> >
> >> >> From f08b8721ed038cefcee5a0bb1329b90a2d322269 Mon Sep 17 00:00:00 2001
> >> >> From: "H.J. Lu" <hjl.to...@gmail.com>
> >> >> Date: Sat, 24 Feb 2018 05:05:49 -0800
> >> >> Subject: [PATCH] i386: Update -mfunction-return= for return with pop
> >> >>
> >> >> When -mfunction-return= is used, simple_return_pop_internal should pop
> >> >> return address into ECX register, adjust stack by bytes to pop from 
> >> >> stack
> >> >> and jump to the return thunk via ECX register.
> >> >>
> >> >> Tested on i686 and x86-64.
> >> >>
> >> >>   PR target/84530
> >> >>   * config/i386/i386-protos.h (ix86_output_indirect_jmp): Remove
> >> >>   the bool argument.
> >> >>   (ix86_output_indirect_function_return): New prototype.
> >> >>   (ix86_split_simple_return_pop_internal): Likewise.
> >> >>   * config/i386/i386.c (indirect_return_via_cx): New.
> >> >>   (indirect_return_via_cx_bnd): Likewise.
> >> >>   (indirect_thunk_name): Handle return va CX_REG.
> >> >>   (output_indirect_thunk_function): Create alias for
> >> >>   __x86_return_thunk_[re]cx and __x86_return_thunk_[re]cx_bnd.
> >> >>   (ix86_output_indirect_jmp): Remove the bool argument.
> >> >>   (ix86_output_indirect_function_return): New function.
> >> >>   (ix86_split_simple_return_pop_internal): Likewise.
> >> >>   * config/i386/i386.md (*indirect_jump): Don't pass false
> >> >>   to ix86_output_indirect_jmp.
> >> >>   (*tablejump_1): Likewise.
> >> >>   (simple_return_pop_internal): Change it to define_insn_and_split.
> >> >>   Call ix86_split_simple_return_pop_internal to split it for
> >> >>   -mfunction-return=.
> >> >>   (simple_return_indirect_internal): Call
> >> >>   ix86_output_indirect_function_return instead of
> >> >>   ix86_output_indirect_jmp.
> >> >
> >> > It seems to make sense. Since LLVM has picked up our thunk names now,
> >> > I wonder if they have same bug or someohow already assigned a name
> >> > to this thunk. In the second case we probably ought to match it. Do
> >> > you know what LLVM uses?
> >> >
> >>
> >> I don't think LLVM has implemented -mfunction-return=.   I disclosed
> >> my -mindirect-branch= approach to LLVM last year.  At the time, they
> >> wanted freedom in their implementation and didn't want to be compatible
> >> with mine.   Later they added GCC compatibility so that LLVM may be
> >> used to compile Linux kernel.  I believe it will be the same case if this
> >> option is used to compile Linux kernel.
> >
> > Patch is OK then, thanks!
> >
> 
> Checked into trunk.  OK for backport to GCC 7 branch after a few days?
OK,
thanks!
Honza
> 
> 
> -- 
> H.J.


Re: [PATCH] i386: Update -mfunction-return= for return with pop

2018-02-26 Thread H.J. Lu
On Mon, Feb 26, 2018 at 7:06 AM, Jan Hubicka <hubi...@ucw.cz> wrote:
>> On Mon, Feb 26, 2018 at 2:58 AM, Jan Hubicka <hubi...@ucw.cz> wrote:
>> >> On Mon, Feb 26, 2018 at 2:05 AM, Martin Liška <mli...@suse.cz> wrote:
>> >> > On 02/25/2018 02:37 AM, H.J. Lu wrote:
>> >> >>   PR target/r84530
>> >> >
>> >> > Just a nit I've noticed:
>> >> >
>> >> > s/r84530/84530
>> >>
>> >> Fixed.
>> >>
>> >> --
>> >> H.J.
>> >
>> >> From f08b8721ed038cefcee5a0bb1329b90a2d322269 Mon Sep 17 00:00:00 2001
>> >> From: "H.J. Lu" <hjl.to...@gmail.com>
>> >> Date: Sat, 24 Feb 2018 05:05:49 -0800
>> >> Subject: [PATCH] i386: Update -mfunction-return= for return with pop
>> >>
>> >> When -mfunction-return= is used, simple_return_pop_internal should pop
>> >> return address into ECX register, adjust stack by bytes to pop from stack
>> >> and jump to the return thunk via ECX register.
>> >>
>> >> Tested on i686 and x86-64.
>> >>
>> >>   PR target/84530
>> >>   * config/i386/i386-protos.h (ix86_output_indirect_jmp): Remove
>> >>   the bool argument.
>> >>   (ix86_output_indirect_function_return): New prototype.
>> >>   (ix86_split_simple_return_pop_internal): Likewise.
>> >>   * config/i386/i386.c (indirect_return_via_cx): New.
>> >>   (indirect_return_via_cx_bnd): Likewise.
>> >>   (indirect_thunk_name): Handle return va CX_REG.
>> >>   (output_indirect_thunk_function): Create alias for
>> >>   __x86_return_thunk_[re]cx and __x86_return_thunk_[re]cx_bnd.
>> >>   (ix86_output_indirect_jmp): Remove the bool argument.
>> >>   (ix86_output_indirect_function_return): New function.
>> >>   (ix86_split_simple_return_pop_internal): Likewise.
>> >>   * config/i386/i386.md (*indirect_jump): Don't pass false
>> >>   to ix86_output_indirect_jmp.
>> >>   (*tablejump_1): Likewise.
>> >>   (simple_return_pop_internal): Change it to define_insn_and_split.
>> >>   Call ix86_split_simple_return_pop_internal to split it for
>> >>   -mfunction-return=.
>> >>   (simple_return_indirect_internal): Call
>> >>   ix86_output_indirect_function_return instead of
>> >>   ix86_output_indirect_jmp.
>> >
>> > It seems to make sense. Since LLVM has picked up our thunk names now,
>> > I wonder if they have same bug or someohow already assigned a name
>> > to this thunk. In the second case we probably ought to match it. Do
>> > you know what LLVM uses?
>> >
>>
>> I don't think LLVM has implemented -mfunction-return=.   I disclosed
>> my -mindirect-branch= approach to LLVM last year.  At the time, they
>> wanted freedom in their implementation and didn't want to be compatible
>> with mine.   Later they added GCC compatibility so that LLVM may be
>> used to compile Linux kernel.  I believe it will be the same case if this
>> option is used to compile Linux kernel.
>
> Patch is OK then, thanks!
>

Checked into trunk.  OK for backport to GCC 7 branch after a few days?


-- 
H.J.


Re: [PATCH] i386: Update -mfunction-return= for return with pop

2018-02-26 Thread Jan Hubicka
> On Mon, Feb 26, 2018 at 2:58 AM, Jan Hubicka <hubi...@ucw.cz> wrote:
> >> On Mon, Feb 26, 2018 at 2:05 AM, Martin Liška <mli...@suse.cz> wrote:
> >> > On 02/25/2018 02:37 AM, H.J. Lu wrote:
> >> >>   PR target/r84530
> >> >
> >> > Just a nit I've noticed:
> >> >
> >> > s/r84530/84530
> >>
> >> Fixed.
> >>
> >> --
> >> H.J.
> >
> >> From f08b8721ed038cefcee5a0bb1329b90a2d322269 Mon Sep 17 00:00:00 2001
> >> From: "H.J. Lu" <hjl.to...@gmail.com>
> >> Date: Sat, 24 Feb 2018 05:05:49 -0800
> >> Subject: [PATCH] i386: Update -mfunction-return= for return with pop
> >>
> >> When -mfunction-return= is used, simple_return_pop_internal should pop
> >> return address into ECX register, adjust stack by bytes to pop from stack
> >> and jump to the return thunk via ECX register.
> >>
> >> Tested on i686 and x86-64.
> >>
> >>   PR target/84530
> >>   * config/i386/i386-protos.h (ix86_output_indirect_jmp): Remove
> >>   the bool argument.
> >>   (ix86_output_indirect_function_return): New prototype.
> >>   (ix86_split_simple_return_pop_internal): Likewise.
> >>   * config/i386/i386.c (indirect_return_via_cx): New.
> >>   (indirect_return_via_cx_bnd): Likewise.
> >>   (indirect_thunk_name): Handle return va CX_REG.
> >>   (output_indirect_thunk_function): Create alias for
> >>   __x86_return_thunk_[re]cx and __x86_return_thunk_[re]cx_bnd.
> >>   (ix86_output_indirect_jmp): Remove the bool argument.
> >>   (ix86_output_indirect_function_return): New function.
> >>   (ix86_split_simple_return_pop_internal): Likewise.
> >>   * config/i386/i386.md (*indirect_jump): Don't pass false
> >>   to ix86_output_indirect_jmp.
> >>   (*tablejump_1): Likewise.
> >>   (simple_return_pop_internal): Change it to define_insn_and_split.
> >>   Call ix86_split_simple_return_pop_internal to split it for
> >>   -mfunction-return=.
> >>   (simple_return_indirect_internal): Call
> >>   ix86_output_indirect_function_return instead of
> >>   ix86_output_indirect_jmp.
> >
> > It seems to make sense. Since LLVM has picked up our thunk names now,
> > I wonder if they have same bug or someohow already assigned a name
> > to this thunk. In the second case we probably ought to match it. Do
> > you know what LLVM uses?
> >
> 
> I don't think LLVM has implemented -mfunction-return=.   I disclosed
> my -mindirect-branch= approach to LLVM last year.  At the time, they
> wanted freedom in their implementation and didn't want to be compatible
> with mine.   Later they added GCC compatibility so that LLVM may be
> used to compile Linux kernel.  I believe it will be the same case if this
> option is used to compile Linux kernel.

Patch is OK then, thanks!

Honza
> 
> BTW, is my patch:
> 
> https://gcc.gnu.org/ml/gcc-patches/2018-01/msg02233.html
> 
> OK for trunk?  The review died at
> 
> https://gcc.gnu.org/ml/gcc-patches/2018-02/msg01355.html
> 
> Thanks.
> 
> -- 
> H.J.


Re: [PATCH] i386: Update -mfunction-return= for return with pop

2018-02-26 Thread H.J. Lu
On Mon, Feb 26, 2018 at 2:58 AM, Jan Hubicka <hubi...@ucw.cz> wrote:
>> On Mon, Feb 26, 2018 at 2:05 AM, Martin Liška <mli...@suse.cz> wrote:
>> > On 02/25/2018 02:37 AM, H.J. Lu wrote:
>> >>   PR target/r84530
>> >
>> > Just a nit I've noticed:
>> >
>> > s/r84530/84530
>>
>> Fixed.
>>
>> --
>> H.J.
>
>> From f08b8721ed038cefcee5a0bb1329b90a2d322269 Mon Sep 17 00:00:00 2001
>> From: "H.J. Lu" <hjl.to...@gmail.com>
>> Date: Sat, 24 Feb 2018 05:05:49 -0800
>> Subject: [PATCH] i386: Update -mfunction-return= for return with pop
>>
>> When -mfunction-return= is used, simple_return_pop_internal should pop
>> return address into ECX register, adjust stack by bytes to pop from stack
>> and jump to the return thunk via ECX register.
>>
>> Tested on i686 and x86-64.
>>
>>   PR target/84530
>>   * config/i386/i386-protos.h (ix86_output_indirect_jmp): Remove
>>   the bool argument.
>>   (ix86_output_indirect_function_return): New prototype.
>>   (ix86_split_simple_return_pop_internal): Likewise.
>>   * config/i386/i386.c (indirect_return_via_cx): New.
>>   (indirect_return_via_cx_bnd): Likewise.
>>   (indirect_thunk_name): Handle return va CX_REG.
>>   (output_indirect_thunk_function): Create alias for
>>   __x86_return_thunk_[re]cx and __x86_return_thunk_[re]cx_bnd.
>>   (ix86_output_indirect_jmp): Remove the bool argument.
>>   (ix86_output_indirect_function_return): New function.
>>   (ix86_split_simple_return_pop_internal): Likewise.
>>   * config/i386/i386.md (*indirect_jump): Don't pass false
>>   to ix86_output_indirect_jmp.
>>   (*tablejump_1): Likewise.
>>   (simple_return_pop_internal): Change it to define_insn_and_split.
>>   Call ix86_split_simple_return_pop_internal to split it for
>>   -mfunction-return=.
>>   (simple_return_indirect_internal): Call
>>   ix86_output_indirect_function_return instead of
>>   ix86_output_indirect_jmp.
>
> It seems to make sense. Since LLVM has picked up our thunk names now,
> I wonder if they have same bug or someohow already assigned a name
> to this thunk. In the second case we probably ought to match it. Do
> you know what LLVM uses?
>

I don't think LLVM has implemented -mfunction-return=.   I disclosed
my -mindirect-branch= approach to LLVM last year.  At the time, they
wanted freedom in their implementation and didn't want to be compatible
with mine.   Later they added GCC compatibility so that LLVM may be
used to compile Linux kernel.  I believe it will be the same case if this
option is used to compile Linux kernel.

BTW, is my patch:

https://gcc.gnu.org/ml/gcc-patches/2018-01/msg02233.html

OK for trunk?  The review died at

https://gcc.gnu.org/ml/gcc-patches/2018-02/msg01355.html

Thanks.

-- 
H.J.


Re: [PATCH] i386: Update -mfunction-return= for return with pop

2018-02-26 Thread Jan Hubicka
> On Mon, Feb 26, 2018 at 2:05 AM, Martin Liška <mli...@suse.cz> wrote:
> > On 02/25/2018 02:37 AM, H.J. Lu wrote:
> >>   PR target/r84530
> >
> > Just a nit I've noticed:
> >
> > s/r84530/84530
> 
> Fixed.
> 
> -- 
> H.J.

> From f08b8721ed038cefcee5a0bb1329b90a2d322269 Mon Sep 17 00:00:00 2001
> From: "H.J. Lu" <hjl.to...@gmail.com>
> Date: Sat, 24 Feb 2018 05:05:49 -0800
> Subject: [PATCH] i386: Update -mfunction-return= for return with pop
> 
> When -mfunction-return= is used, simple_return_pop_internal should pop
> return address into ECX register, adjust stack by bytes to pop from stack
> and jump to the return thunk via ECX register.
> 
> Tested on i686 and x86-64.
> 
>   PR target/84530
>   * config/i386/i386-protos.h (ix86_output_indirect_jmp): Remove
>   the bool argument.
>   (ix86_output_indirect_function_return): New prototype.
>   (ix86_split_simple_return_pop_internal): Likewise.
>   * config/i386/i386.c (indirect_return_via_cx): New.
>   (indirect_return_via_cx_bnd): Likewise.
>   (indirect_thunk_name): Handle return va CX_REG.
>   (output_indirect_thunk_function): Create alias for
>   __x86_return_thunk_[re]cx and __x86_return_thunk_[re]cx_bnd.
>   (ix86_output_indirect_jmp): Remove the bool argument.
>   (ix86_output_indirect_function_return): New function.
>   (ix86_split_simple_return_pop_internal): Likewise.
>   * config/i386/i386.md (*indirect_jump): Don't pass false
>   to ix86_output_indirect_jmp.
>   (*tablejump_1): Likewise.
>   (simple_return_pop_internal): Change it to define_insn_and_split.
>   Call ix86_split_simple_return_pop_internal to split it for
>   -mfunction-return=.
>   (simple_return_indirect_internal): Call
>   ix86_output_indirect_function_return instead of
>   ix86_output_indirect_jmp.

It seems to make sense. Since LLVM has picked up our thunk names now,
I wonder if they have same bug or someohow already assigned a name
to this thunk. In the second case we probably ought to match it. Do
you know what LLVM uses?

Honza


Re: [PATCH] i386: Update -mfunction-return= for return with pop

2018-02-26 Thread H.J. Lu
On Mon, Feb 26, 2018 at 2:05 AM, Martin Liška <mli...@suse.cz> wrote:
> On 02/25/2018 02:37 AM, H.J. Lu wrote:
>>   PR target/r84530
>
> Just a nit I've noticed:
>
> s/r84530/84530

Fixed.

-- 
H.J.
From f08b8721ed038cefcee5a0bb1329b90a2d322269 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.to...@gmail.com>
Date: Sat, 24 Feb 2018 05:05:49 -0800
Subject: [PATCH] i386: Update -mfunction-return= for return with pop

When -mfunction-return= is used, simple_return_pop_internal should pop
return address into ECX register, adjust stack by bytes to pop from stack
and jump to the return thunk via ECX register.

Tested on i686 and x86-64.

	PR target/84530
	* config/i386/i386-protos.h (ix86_output_indirect_jmp): Remove
	the bool argument.
	(ix86_output_indirect_function_return): New prototype.
	(ix86_split_simple_return_pop_internal): Likewise.
	* config/i386/i386.c (indirect_return_via_cx): New.
	(indirect_return_via_cx_bnd): Likewise.
	(indirect_thunk_name): Handle return va CX_REG.
	(output_indirect_thunk_function): Create alias for
	__x86_return_thunk_[re]cx and __x86_return_thunk_[re]cx_bnd.
	(ix86_output_indirect_jmp): Remove the bool argument.
	(ix86_output_indirect_function_return): New function.
	(ix86_split_simple_return_pop_internal): Likewise.
	* config/i386/i386.md (*indirect_jump): Don't pass false
	to ix86_output_indirect_jmp.
	(*tablejump_1): Likewise.
	(simple_return_pop_internal): Change it to define_insn_and_split.
	Call ix86_split_simple_return_pop_internal to split it for
	-mfunction-return=.
	(simple_return_indirect_internal): Call
	ix86_output_indirect_function_return instead of
	ix86_output_indirect_jmp.

gcc/testsuite/

	PR target/84530
	* gcc.target/i386/ret-thunk-22.c: New test.
	* gcc.target/i386/ret-thunk-23.c: Likewise.
	* gcc.target/i386/ret-thunk-24.c: Likewise.
	* gcc.target/i386/ret-thunk-25.c: Likewise.
	* gcc.target/i386/ret-thunk-26.c: Likewise.
---
 gcc/config/i386/i386-protos.h|   4 +-
 gcc/config/i386/i386.c   | 126 ---
 gcc/config/i386/i386.md  |  11 ++-
 gcc/testsuite/gcc.target/i386/ret-thunk-22.c |  15 
 gcc/testsuite/gcc.target/i386/ret-thunk-23.c |  15 
 gcc/testsuite/gcc.target/i386/ret-thunk-24.c |  15 
 gcc/testsuite/gcc.target/i386/ret-thunk-25.c |  15 
 gcc/testsuite/gcc.target/i386/ret-thunk-26.c |  40 +
 8 files changed, 223 insertions(+), 18 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-22.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-23.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-24.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-25.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-26.c

diff --git a/gcc/config/i386/i386-protos.h b/gcc/config/i386/i386-protos.h
index fb86f00b3a6..79b96c0f80b 100644
--- a/gcc/config/i386/i386-protos.h
+++ b/gcc/config/i386/i386-protos.h
@@ -305,8 +305,10 @@ extern enum attr_cpu ix86_schedule;
 #endif
 
 extern const char * ix86_output_call_insn (rtx_insn *insn, rtx call_op);
-extern const char * ix86_output_indirect_jmp (rtx call_op, bool ret_p);
+extern const char * ix86_output_indirect_jmp (rtx call_op);
 extern const char * ix86_output_function_return (bool long_p);
+extern const char * ix86_output_indirect_function_return (rtx ret_op);
+extern void ix86_split_simple_return_pop_internal (rtx);
 extern bool ix86_operands_ok_for_move_multiple (rtx *operands, bool load,
 		machine_mode mode);
 extern int ix86_min_insn_size (rtx_insn *);
diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
index 640c5921863..98075ad42c7 100644
--- a/gcc/config/i386/i386.c
+++ b/gcc/config/i386/i386.c
@@ -10812,6 +10812,12 @@ static int indirect_thunks_used;
by call and return thunks functions with the BND prefix.  */
 static int indirect_thunks_bnd_used;
 
+/* True if return thunk function via CX is needed.  */
+static bool indirect_return_via_cx;
+/* True if return thunk function via CX with the BND prefix is
+   needed.  */
+static bool indirect_return_via_cx_bnd;
+
 #ifndef INDIRECT_LABEL
 # define INDIRECT_LABEL "LIND"
 #endif
@@ -10852,7 +10858,7 @@ indirect_thunk_name (char name[32], unsigned int regno,
 		 enum indirect_thunk_prefix need_prefix,
 		 bool ret_p)
 {
-  if (regno != INVALID_REGNUM && ret_p)
+  if (regno != INVALID_REGNUM && regno != CX_REG && ret_p)
 gcc_unreachable ();
 
   if (USE_HIDDEN_LINKONCE)
@@ -10872,6 +10878,8 @@ indirect_thunk_name (char name[32], unsigned int regno,
   else
 	prefix = "";
 
+  const char *ret = ret_p ? "return" : "indirect";
+
   if (regno != INVALID_REGNUM)
 	{
 	  const char *reg_prefix;
@@ -10879,14 +10887,11 @@ indirect_thunk_name (char name[32], unsigned int regno,
 	reg_prefix = TARGET_64BIT ? "r" : "e";
 	  else
 	reg_prefix 

Re: [PATCH] i386: Update -mfunction-return= for return with pop

2018-02-26 Thread Martin Liška
On 02/25/2018 02:37 AM, H.J. Lu wrote:
>   PR target/r84530

Just a nit I've noticed:

s/r84530/84530

Martin


[PATCH] i386: Update -mfunction-return= for return with pop

2018-02-24 Thread H.J. Lu
When -mfunction-return= is used, simple_return_pop_internal should pop
return address into ECX register, adjust stack by bytes to pop from stack
and jump to the return thunk via ECX register.

Tested on i686 and x86-64.

PR target/r84530
* config/i386/i386-protos.h (ix86_output_indirect_jmp): Remove
the bool argument.
(ix86_output_indirect_function_return): New prototype.
(ix86_split_simple_return_pop_internal): Likewise.
* config/i386/i386.c (indirect_return_via_cx): New.
(indirect_return_via_cx_bnd): Likewise.
(indirect_thunk_name): Handle return va CX_REG.
(output_indirect_thunk_function): Create alias for
__x86_return_thunk_[re]cx and __x86_return_thunk_[re]cx_bnd.
(ix86_output_indirect_jmp): Remove the bool argument.
(ix86_output_indirect_function_return): New function.
(ix86_split_simple_return_pop_internal): Likewise.
* config/i386/i386.md (*indirect_jump): Don't pass false
to ix86_output_indirect_jmp.
(*tablejump_1): Likewise.
(simple_return_pop_internal): Change it to define_insn_and_split.
Call ix86_split_simple_return_pop_internal to split it for
-mfunction-return=.
(simple_return_indirect_internal): Call
ix86_output_indirect_function_return instead of
ix86_output_indirect_jmp.

gcc/testsuite/

PR target/r84530
* gcc.target/i386/ret-thunk-22.c: New test.
* gcc.target/i386/ret-thunk-23.c: Likewise.
* gcc.target/i386/ret-thunk-24.c: Likewise.
* gcc.target/i386/ret-thunk-25.c: Likewise.
* gcc.target/i386/ret-thunk-26.c: Likewise.
---
 gcc/config/i386/i386-protos.h|   4 +-
 gcc/config/i386/i386.c   | 126 ---
 gcc/config/i386/i386.md  |  11 ++-
 gcc/testsuite/gcc.target/i386/ret-thunk-22.c |  15 
 gcc/testsuite/gcc.target/i386/ret-thunk-23.c |  15 
 gcc/testsuite/gcc.target/i386/ret-thunk-24.c |  15 
 gcc/testsuite/gcc.target/i386/ret-thunk-25.c |  15 
 gcc/testsuite/gcc.target/i386/ret-thunk-26.c |  40 +
 8 files changed, 223 insertions(+), 18 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-22.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-23.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-24.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-25.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ret-thunk-26.c

diff --git a/gcc/config/i386/i386-protos.h b/gcc/config/i386/i386-protos.h
index fb86f00b3a6..79b96c0f80b 100644
--- a/gcc/config/i386/i386-protos.h
+++ b/gcc/config/i386/i386-protos.h
@@ -305,8 +305,10 @@ extern enum attr_cpu ix86_schedule;
 #endif
 
 extern const char * ix86_output_call_insn (rtx_insn *insn, rtx call_op);
-extern const char * ix86_output_indirect_jmp (rtx call_op, bool ret_p);
+extern const char * ix86_output_indirect_jmp (rtx call_op);
 extern const char * ix86_output_function_return (bool long_p);
+extern const char * ix86_output_indirect_function_return (rtx ret_op);
+extern void ix86_split_simple_return_pop_internal (rtx);
 extern bool ix86_operands_ok_for_move_multiple (rtx *operands, bool load,
machine_mode mode);
 extern int ix86_min_insn_size (rtx_insn *);
diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
index 640c5921863..98075ad42c7 100644
--- a/gcc/config/i386/i386.c
+++ b/gcc/config/i386/i386.c
@@ -10812,6 +10812,12 @@ static int indirect_thunks_used;
by call and return thunks functions with the BND prefix.  */
 static int indirect_thunks_bnd_used;
 
+/* True if return thunk function via CX is needed.  */
+static bool indirect_return_via_cx;
+/* True if return thunk function via CX with the BND prefix is
+   needed.  */
+static bool indirect_return_via_cx_bnd;
+
 #ifndef INDIRECT_LABEL
 # define INDIRECT_LABEL "LIND"
 #endif
@@ -10852,7 +10858,7 @@ indirect_thunk_name (char name[32], unsigned int regno,
 enum indirect_thunk_prefix need_prefix,
 bool ret_p)
 {
-  if (regno != INVALID_REGNUM && ret_p)
+  if (regno != INVALID_REGNUM && regno != CX_REG && ret_p)
 gcc_unreachable ();
 
   if (USE_HIDDEN_LINKONCE)
@@ -10872,6 +10878,8 @@ indirect_thunk_name (char name[32], unsigned int regno,
   else
prefix = "";
 
+  const char *ret = ret_p ? "return" : "indirect";
+
   if (regno != INVALID_REGNUM)
{
  const char *reg_prefix;
@@ -10879,14 +10887,11 @@ indirect_thunk_name (char name[32], unsigned int 
regno,
reg_prefix = TARGET_64BIT ? "r" : "e";
  else
reg_prefix = "";
- sprintf (name, "__x86_indirect_thunk%s_%s%s",
-  prefix, reg_prefix, reg_names[regno]);
+ sprintf (name, "__x86_%s_thunk%s_%s%s",
+  ret, prefix, reg_prefix, reg_names[regno]);
}