tcg/optimize: Fix s_mask computation for shifts
Skip s_mask computation for logical right shift. Cc: qemu-stable@nongnu.org Fixes: 93a967fbb57 ("tcg/optimize: Propagate sign info for shifting") Reported-by: Jacob Young <jacobly@ziglang.org> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Richard Henderson committed
Aug 11, 2026 at 14:15 UTC
5fe51606146cbf04dac07bf27256febbe56881e7
2 files changed
+31
-1
tcg/optimize.c
+10
-1
@@ -2656,8 +2656,17 @@ static bool fold_shift(OptContext *ctx, TCGOp *op)
2656
2657
z_mask = do_constant_folding(op->opc, ctx->type, z_mask, sh);
2658
o_mask = do_constant_folding(op->opc, ctx->type, o_mask, sh);
2659
- s_mask = do_constant_folding(op->opc, ctx->type, s_mask, sh);
2659
2660
+ if (op->opc == INDEX_op_shr) {
2661
+ /*
2662
+ * Logical right shift will force the sign bit zero.
2663
+ * Don't bother computing s_mask and let fold_masks
2664
+ * recompute from z_mask.
2665
+ */
2666
+ return fold_masks_zo(ctx, op, z_mask, o_mask);
2667
+ }
2668
+
2669
+ s_mask = do_constant_folding(op->opc, ctx->type, s_mask, sh);
2670
return fold_masks_zos(ctx, op, z_mask, o_mask, s_mask);
2671
}
2672
tests/tcg/i386/test-i386-opt-shr.c
new
+21
@@ -0,0 +1,21 @@
1
+/* SPDX-License-Identifier: GPL-2.0-or-later */
2
+/* Regression test for tcg optimize vs sign bit repetition counting. */
3
+
4
+#include <assert.h>
5
+
6
+int main()
7
+{
8
+#ifndef __x86_64__
9
+ char test;
10
+
11
+ asm("movw $0x4000, %%ax\n\t"
12
+ "addw %%ax, %%ax\n\t"
13
+ "cwtl\n\t"
14
+ "shrl %%eax\n\t"
15
+ "cmpw $-0x3fff, %%ax\n\t"
16
+ "setnl %%al"
17
+ : "=a"(test));
18
+ assert(!test);
19
+#endif
20
+ return 0;
21
+}