From: Richard Henderson <[email protected]>

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]>
(cherry picked from commit 5fe51606146cbf04dac07bf27256febbe56881e7)
(Mjt: back-port to 10.0 across v10.0.0-1791-g03329e3ce460
 "tcg/optimize: Build and use o_bits in fold_shift"
 and v10.0.0-425-g74dbd36f1f87 "tcg: Merge INDEX_op_shr_{i32,i64}")
Signed-off-by: Michael Tokarev <[email protected]>

diff --git a/tcg/optimize.c b/tcg/optimize.c
index 1b9553737e2..c7c16518f08 100644
--- a/tcg/optimize.c
+++ b/tcg/optimize.c
@@ -2602,8 +2602,17 @@ static bool fold_shift(OptContext *ctx, TCGOp *op)
         int sh = ti_const_val(t2);
 
         z_mask = do_constant_folding(op->opc, ctx->type, z_mask, sh);
-        s_mask = do_constant_folding(op->opc, ctx->type, s_mask, sh);
 
+        if (op->opc == INDEX_op_shr_i32 || op->opc == INDEX_op_shr_i64) {
+            /*
+             * 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_z(ctx, op, z_mask);
+        }
+
+        s_mask = do_constant_folding(op->opc, ctx->type, s_mask, sh);
         return fold_masks_zs(ctx, op, z_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 00000000000..9fb8d420223
--- /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.47.3


Reply via email to