Skip s_mask computation for logical right shift.
Cc: [email protected]
Fixes: 93a967fbb57 ("tcg/optimize: Propagate sign info for shifting")
Reported-by: Jacob Young <[email protected]>
Signed-off-by: Richard Henderson <[email protected]>
---
tcg/optimize.c | 11 ++++++++++-
tests/tcg/i386/test-i386-opt-shr.c | 21 +++++++++++++++++++++
2 files changed, 31 insertions(+), 1 deletion(-)
create mode 100644 tests/tcg/i386/test-i386-opt-shr.c
diff --git a/tcg/optimize.c b/tcg/optimize.c
index ed2ff32ed2..d12babad88 100644
--- a/tcg/optimize.c
+++ b/tcg/optimize.c
@@ -2656,8 +2656,17 @@ static bool fold_shift(OptContext *ctx, TCGOp *op)
z_mask = do_constant_folding(op->opc, ctx->type, z_mask, sh);
o_mask = do_constant_folding(op->opc, ctx->type, o_mask, sh);
- s_mask = do_constant_folding(op->opc, ctx->type, s_mask, sh);
+ if (op->opc == INDEX_op_shr) {
+ /*
+ * Logical right shift will force the sign bit zero.
+ * Don't bother computing s_mask and let fold_masks
+ * recompute from z_mask.
+ */
+ return fold_masks_zo(ctx, op, z_mask, o_mask);
+ }
+
+ s_mask = do_constant_folding(op->opc, ctx->type, s_mask, sh);
return fold_masks_zos(ctx, op, z_mask, o_mask, s_mask);
}
diff --git a/tests/tcg/i386/test-i386-opt-shr.c
b/tests/tcg/i386/test-i386-opt-shr.c
new file mode 100644
index 0000000000..9fb8d42022
--- /dev/null
+++ b/tests/tcg/i386/test-i386-opt-shr.c
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/* Regression test for tcg optimize vs sign bit repetition counting. */
+
+#include <assert.h>
+
+int main()
+{
+#ifndef __x86_64__
+ char test;
+
+ asm("movw $0x4000, %%ax\n\t"
+ "addw %%ax, %%ax\n\t"
+ "cwtl\n\t"
+ "shrl %%eax\n\t"
+ "cmpw $-0x3fff, %%ax\n\t"
+ "setnl %%al"
+ : "=a"(test));
+ assert(!test);
+#endif
+ return 0;
+}
--
2.43.0