Skip to content

Commit 3502062

Browse files
committedFeb 7, 2025
tcg/optimize: optimize TSTNE using smask and zmask
Generalize the existing optimization of "TSTNE x,sign" and "TSTNE x,-1". This can be useful for example in the i386 frontend, which will generate tests of zero-extended registers against 0xffffffff. Ironically, on x86 hosts this is a very slight pessimization in the very case it's meant to optimize because brcond_i64 cc_dst,$0xffffffff,tsteq,$L1 (test %ebx, %ebx) is 1 byte smaller than brcond_i64 cc_dst,$0x0,eq,$L1 (test %rbx, %rbx). However, in general it is an improvement, especially if it avoids placing a large immediate in the constant pool. Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 059fad2 commit 3502062

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed
 

‎tcg/optimize.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,7 @@ static int do_constant_folding_cond1(OptContext *ctx, TCGOp *op, TCGArg dest,
766766
TCGArg *p1, TCGArg *p2, TCGArg *pcond)
767767
{
768768
TCGCond cond;
769+
TempOptInfo *i1;
769770
bool swap;
770771
int r;
771772

@@ -783,19 +784,21 @@ static int do_constant_folding_cond1(OptContext *ctx, TCGOp *op, TCGArg dest,
783784
return -1;
784785
}
785786

787+
i1 = arg_info(*p1);
788+
786789
/*
787790
* TSTNE x,x -> NE x,0
788-
* TSTNE x,-1 -> NE x,0
791+
* TSTNE x,i -> NE x,0 if i includes all nonzero bits of x
789792
*/
790-
if (args_are_copies(*p1, *p2) || arg_is_const_val(*p2, -1)) {
793+
if (args_are_copies(*p1, *p2) ||
794+
(arg_is_const(*p2) && (i1->z_mask & ~arg_info(*p2)->val) == 0)) {
791795
*p2 = arg_new_constant(ctx, 0);
792796
*pcond = tcg_tst_eqne_cond(cond);
793797
return -1;
794798
}
795799

796-
/* TSTNE x,sign -> LT x,0 */
797-
if (arg_is_const_val(*p2, (ctx->type == TCG_TYPE_I32
798-
? INT32_MIN : INT64_MIN))) {
800+
/* TSTNE x,i -> LT x,0 if i only includes sign bit copies */
801+
if (arg_is_const(*p2) && (arg_info(*p2)->val & ~i1->s_mask) == 0) {
799802
*p2 = arg_new_constant(ctx, 0);
800803
*pcond = tcg_tst_ltge_cond(cond);
801804
return -1;

0 commit comments

Comments
 (0)
Please sign in to comment.