The define_split that converts "and $-65536, reg" to a strict_low_part
HImode store of zero can fire before reload. When operands[0] is a
(subreg:SI (reg:SF N) 0), gen_lowpart (HImode, operands[0]) cannot be
folded into a valid subreg of the SFmode pseudo, so it silently copies
the value to a fresh SImode pseudo and returns the lowpart of that.
The strict_low_part store then updates a pseudo nobody reads and the
original register keeps its old value, which is a miscompile.
Fix this by requiring reload_completed, where operands[0] is a hard
register and gen_lowpart yields a plain REG that aliases the original.
PR target/128836
gcc/ChangeLog:
* config/i386/i386.md
(split and with -65536 to strict_low_part movhi):
Require reload_completed.
gcc/testsuite/ChangeLog:
* gcc.target/i386/pr128836.c: New test.
Bootstrapped and regression tested on x86_64-pc-linux-gnu {,-m32}.
Uros.
diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md
index 23a9bc7fa59..461974f7151 100644
--- a/gcc/config/i386/i386.md
+++ b/gcc/config/i386/i386.md
@@ -13896,8 +13896,9 @@ (define_split
(and:SWI48 (match_dup 0)
(const_int -65536)))
(clobber (reg:CC FLAGS_REG))]
- "(TARGET_FAST_PREFIX && !TARGET_PARTIAL_REG_STALL)
- || optimize_function_for_size_p (cfun)"
+ "((TARGET_FAST_PREFIX && !TARGET_PARTIAL_REG_STALL)
+ || optimize_function_for_size_p (cfun))
+ && reload_completed"
[(set (strict_low_part (match_dup 1)) (const_int 0))]
"operands[1] = gen_lowpart (HImode, operands[0]);")
diff --git a/gcc/testsuite/gcc.target/i386/pr128836.c
b/gcc/testsuite/gcc.target/i386/pr128836.c
new file mode 100644
index 00000000000..7c5993b7c3e
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr128836.c
@@ -0,0 +1,28 @@
+/* PR target/128836 */
+/* { dg-do run } */
+/* { dg-options "-Os" } */
+
+__attribute__((noipa)) float
+f(int p, float x)
+{
+ if (p == 1)
+ {
+ unsigned int u;
+ __builtin_memcpy (&u, &x, sizeof (u));
+ u &= 0xFFFF0000u;
+ __builtin_memcpy (&x, &u, sizeof (u));
+ return x;
+ }
+ return p == 2 ? -x : x;
+}
+
+int
+main ()
+{
+ volatile int p = 1;
+ volatile float v = -87.3f;
+ float r = f (p, v);
+ if (r != -87.0f)
+ __builtin_abort ();
+ return 0;
+}