]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
i386: Don't emit pushf;pop for __builtin_ia32_readeflags_u* with unused lhs [PR104971]
authorJakub Jelinek <jakub@redhat.com>
Sat, 19 Mar 2022 12:53:12 +0000 (13:53 +0100)
committerJakub Jelinek <jakub@redhat.com>
Tue, 10 May 2022 08:14:35 +0000 (10:14 +0200)
__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.

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

PR middle-end/104971
* config/i386/i386-expand.c
(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.

(cherry picked from commit b60bc913cca7439d29a7ec9e9a7f448d8841b43c)

gcc/config/i386/i386-expand.c
gcc/testsuite/gcc.target/i386/pr104971.c [new file with mode: 0644]

index ea9bdbce9b424de00af457b63ab17507a7afdf93..9e47ac9ba7a190976585a019a22409ff50110c52 100644 (file)
@@ -11932,6 +11932,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
diff --git a/gcc/testsuite/gcc.target/i386/pr104971.c b/gcc/testsuite/gcc.target/i386/pr104971.c
new file mode 100644 (file)
index 0000000..80ac6b6
--- /dev/null
@@ -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;
+}