aarch64: Fix eh_return for -mtrack-speculation [PR112987]

Message ID 20240117143703.2998316-1-szabolcs.nagy@arm.com
State Unresolved
Headers
Series aarch64: Fix eh_return for -mtrack-speculation [PR112987] |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Szabolcs Nagy Jan. 17, 2024, 2:37 p.m. UTC
  Recent commit introduced a conditional branch in eh_return epilogues
that is not compatible with speculation tracking:

  commit 426fddcbdad6746fe70e031f707fb07f55dfb405
  Author:     Szabolcs Nagy <szabolcs.nagy@arm.com>
  CommitDate: 2023-11-27 15:52:48 +0000

  aarch64: Use br instead of ret for eh_return

gcc/ChangeLog:

	PR target/112987
	* config/aarch64/aarch64.cc (aarch64_expand_epilogue): Use
	explicit compare and separate jump with speculation tracking.
---
 gcc/config/aarch64/aarch64.cc | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
  

Comments

Richard Sandiford Jan. 24, 2024, 5:41 p.m. UTC | #1
Szabolcs Nagy <szabolcs.nagy@arm.com> writes:
> Recent commit introduced a conditional branch in eh_return epilogues
> that is not compatible with speculation tracking:
>
>   commit 426fddcbdad6746fe70e031f707fb07f55dfb405
>   Author:     Szabolcs Nagy <szabolcs.nagy@arm.com>
>   CommitDate: 2023-11-27 15:52:48 +0000
>
>   aarch64: Use br instead of ret for eh_return
>
> gcc/ChangeLog:
>
> 	PR target/112987
> 	* config/aarch64/aarch64.cc (aarch64_expand_epilogue): Use
> 	explicit compare and separate jump with speculation tracking.
> ---
>  gcc/config/aarch64/aarch64.cc | 12 +++++++++++-
>  1 file changed, 11 insertions(+), 1 deletion(-)
>
> diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
> index e6bd3fd0bb4..e6de62dc02a 100644
> --- a/gcc/config/aarch64/aarch64.cc
> +++ b/gcc/config/aarch64/aarch64.cc
> @@ -9879,7 +9879,17 @@ aarch64_expand_epilogue (rtx_call_insn *sibcall)
>  	 is just as correct as retaining the CFA from the body
>  	 of the function.  Therefore, do nothing special.  */
>        rtx label = gen_label_rtx ();
> -      rtx x = gen_rtx_EQ (VOIDmode, EH_RETURN_TAKEN_RTX, const0_rtx);
> +      rtx x;
> +      if (aarch64_track_speculation)
> +	{
> +	  /* Emit an explicit compare, so cc can be tracked.  */
> +	  rtx cc_reg = aarch64_gen_compare_reg (EQ,
> +						EH_RETURN_TAKEN_RTX,
> +						const0_rtx);
> +	  x = gen_rtx_EQ (GET_MODE (cc_reg), cc_reg, const0_rtx);
> +	}
> +      else
> +	x = gen_rtx_EQ (VOIDmode, EH_RETURN_TAKEN_RTX, const0_rtx);

It looks from a quick scan like we already have 3 instances of
this kind of construct.  Would you mind factoring them out into
a helper?

E.g. (strawman):

static rtx
aarch64_gen_compare_zero_and_branch (rtx_code code, rtx x, rtx_label *label)
{
}

that returns the SET pattern.  The caller can then emit the pattern
using whichever interface is appropriate.

Thanks,
Richard

>        x = gen_rtx_IF_THEN_ELSE (VOIDmode, x,
>  				gen_rtx_LABEL_REF (Pmode, label), pc_rtx);
>        rtx jump = emit_jump_insn (gen_rtx_SET (pc_rtx, x));
  

Patch

diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
index e6bd3fd0bb4..e6de62dc02a 100644
--- a/gcc/config/aarch64/aarch64.cc
+++ b/gcc/config/aarch64/aarch64.cc
@@ -9879,7 +9879,17 @@  aarch64_expand_epilogue (rtx_call_insn *sibcall)
 	 is just as correct as retaining the CFA from the body
 	 of the function.  Therefore, do nothing special.  */
       rtx label = gen_label_rtx ();
-      rtx x = gen_rtx_EQ (VOIDmode, EH_RETURN_TAKEN_RTX, const0_rtx);
+      rtx x;
+      if (aarch64_track_speculation)
+	{
+	  /* Emit an explicit compare, so cc can be tracked.  */
+	  rtx cc_reg = aarch64_gen_compare_reg (EQ,
+						EH_RETURN_TAKEN_RTX,
+						const0_rtx);
+	  x = gen_rtx_EQ (GET_MODE (cc_reg), cc_reg, const0_rtx);
+	}
+      else
+	x = gen_rtx_EQ (VOIDmode, EH_RETURN_TAKEN_RTX, const0_rtx);
       x = gen_rtx_IF_THEN_ELSE (VOIDmode, x,
 				gen_rtx_LABEL_REF (Pmode, label), pc_rtx);
       rtx jump = emit_jump_insn (gen_rtx_SET (pc_rtx, x));