diff mbox series

i386: Don't emit pushf;pop for __builtin_ia32_readeflags_u* with unused lhs [PR104971]

Message ID YjWInQFmlqKLsl6W@tucnak
State New
Headers show
Series i386: Don't emit pushf;pop for __builtin_ia32_readeflags_u* with unused lhs [PR104971] | expand

Commit Message

Jakub Jelinek March 19, 2022, 7:39 a.m. UTC
Hi!

__builtin_ia32_readeflags_u* aren't marked const or pure I think
intentionally, so that they aren't CSEd from different regions of a function
etc. because we don't and can't easily track all dependencies between
it and surrounding code (if somebody looks at the condition flags, it is
dependent on the vast majority of instructions).
But the builtin itself doesn't have any side-effects, so if we ignore the
result of the builtin, there is no point to emit anything.

There is a LRA bug that miscompiles the testcase which this patch makes
latent, which is certainly worth fixing too, but IMHO this change
(and maybe ix86_gimple_fold_builtin too which would fold it even earlier
when it looses lhs) is worth it as well.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2022-03-19  Jakub Jelinek  <jakub@redhat.com>

	PR middle-end/104971
	* config/i386/i386-expand.cc
	(ix86_expand_builtin) <case IX86_BUILTIN_READ_FLAGS>: If ignore,
	don't push/pop anything and just return const0_rtx.

	* gcc.target/i386/pr104971.c: New test.


	Jakub

Comments

Uros Bizjak March 19, 2022, 12:40 p.m. UTC | #1
On Sat, Mar 19, 2022 at 8:39 AM Jakub Jelinek <jakub@redhat.com> wrote:
>
> Hi!
>
> __builtin_ia32_readeflags_u* aren't marked const or pure I think
> intentionally, so that they aren't CSEd from different regions of a function
> etc. because we don't and can't easily track all dependencies between
> it and surrounding code (if somebody looks at the condition flags, it is
> dependent on the vast majority of instructions).
> But the builtin itself doesn't have any side-effects, so if we ignore the
> result of the builtin, there is no point to emit anything.
>
> There is a LRA bug that miscompiles the testcase which this patch makes
> latent, which is certainly worth fixing too, but IMHO this change
> (and maybe ix86_gimple_fold_builtin too which would fold it even earlier
> when it looses lhs) is worth it as well.
>
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
>
> 2022-03-19  Jakub Jelinek  <jakub@redhat.com>
>
>         PR middle-end/104971
>         * config/i386/i386-expand.cc
>         (ix86_expand_builtin) <case IX86_BUILTIN_READ_FLAGS>: If ignore,
>         don't push/pop anything and just return const0_rtx.
>
>         * gcc.target/i386/pr104971.c: New test.

OK.

Thanks,
Uros.

>
> --- gcc/config/i386/i386-expand.cc.jj   2022-03-14 10:34:34.149924022 +0100
> +++ gcc/config/i386/i386-expand.cc      2022-03-18 18:28:25.029500676 +0100
> @@ -13556,6 +13556,9 @@ rdseed_step:
>        return target;
>
>      case IX86_BUILTIN_READ_FLAGS:
> +      if (ignore)
> +       return const0_rtx;
> +
>        emit_insn (gen_push (gen_rtx_REG (word_mode, FLAGS_REG)));
>
>        if (optimize
> --- gcc/testsuite/gcc.target/i386/pr104971.c.jj 2022-03-18 18:21:38.879034278 +0100
> +++ gcc/testsuite/gcc.target/i386/pr104971.c    2022-03-18 18:21:24.300232909 +0100
> @@ -0,0 +1,18 @@
> +/* PR middle-end/104971 */
> +/* { dg-do run } */
> +/* { dg-options "-O2" } */
> +
> +#include <x86intrin.h>
> +
> +__attribute__((noipa)) void
> +foo (void)
> +{
> +  __readeflags ();
> +}
> +
> +int
> +main ()
> +{
> +  foo ();
> +  return 0;
> +}
>
>         Jakub
>
diff mbox series

Patch

--- gcc/config/i386/i386-expand.cc.jj	2022-03-14 10:34:34.149924022 +0100
+++ gcc/config/i386/i386-expand.cc	2022-03-18 18:28:25.029500676 +0100
@@ -13556,6 +13556,9 @@  rdseed_step:
       return target;
 
     case IX86_BUILTIN_READ_FLAGS:
+      if (ignore)
+	return const0_rtx;
+
       emit_insn (gen_push (gen_rtx_REG (word_mode, FLAGS_REG)));
 
       if (optimize
--- gcc/testsuite/gcc.target/i386/pr104971.c.jj	2022-03-18 18:21:38.879034278 +0100
+++ gcc/testsuite/gcc.target/i386/pr104971.c	2022-03-18 18:21:24.300232909 +0100
@@ -0,0 +1,18 @@ 
+/* PR middle-end/104971 */
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+#include <x86intrin.h>
+
+__attribute__((noipa)) void
+foo (void)
+{
+  __readeflags ();
+}
+
+int
+main ()
+{
+  foo ();
+  return 0;
+}