tcg/optimize: fix constant signedness

By convention, on a 64-bit host TCG internally stores 32-bit constants
as sign-extended. This is not the case in the optimizer when a 32-bit
constant is folded.

This doesn't seem to have more consequences than suboptimal code
generation. For instance the x86 backend assumes sign-extended constants,
and in some rare cases uses a 32-bit unsigned immediate 0xffffffff
instead of a 8-bit signed immediate 0xff for the constant -1. This is
with a ppc guest:

before
------

 ---- 0x9f29cc
 movi_i32 tmp1,$0xffffffff
 movi_i32 tmp2,$0x0
 add2_i32 tmp0,CA,CA,tmp2,r6,tmp2
 add2_i32 tmp0,CA,tmp0,CA,tmp1,tmp2
 mov_i32 r10,tmp0

0x7fd8c7dfe90c:  xor    %ebp,%ebp
0x7fd8c7dfe90e:  mov    %ebp,%r11d
0x7fd8c7dfe911:  mov    0x18(%r14),%r9d
0x7fd8c7dfe915:  add    %r9d,%r10d
0x7fd8c7dfe918:  adc    %ebp,%r11d
0x7fd8c7dfe91b:  add    $0xffffffff,%r10d
0x7fd8c7dfe922:  adc    %ebp,%r11d
0x7fd8c7dfe925:  mov    %r11d,0x134(%r14)
0x7fd8c7dfe92c:  mov    %r10d,0x28(%r14)

after
-----

 ---- 0x9f29cc
 movi_i32 tmp1,$0xffffffffffffffff
 movi_i32 tmp2,$0x0
 add2_i32 tmp0,CA,CA,tmp2,r6,tmp2
 add2_i32 tmp0,CA,tmp0,CA,tmp1,tmp2
 mov_i32 r10,tmp0

0x7f37010d490c:  xor    %ebp,%ebp
0x7f37010d490e:  mov    %ebp,%r11d
0x7f37010d4911:  mov    0x18(%r14),%r9d
0x7f37010d4915:  add    %r9d,%r10d
0x7f37010d4918:  adc    %ebp,%r11d
0x7f37010d491b:  add    $0xffffffffffffffff,%r10d
0x7f37010d491f:  adc    %ebp,%r11d
0x7f37010d4922:  mov    %r11d,0x134(%r14)
0x7f37010d4929:  mov    %r10d,0x28(%r14)

Backports commit 29f3ff8d6cbc28f79933aeaa25805408d0984a8f from qemu
This commit is contained in:
Aurelien Jarno 2018-02-10 21:39:52 -05:00 committed by Lioncash
parent e273acf87a
commit 5f67ab74e7
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7

View file

@ -390,7 +390,7 @@ static TCGArg do_constant_folding(TCGContext *s, TCGOpcode op, TCGArg x, TCGArg
{
TCGArg res = do_constant_folding_2(op, x, y);
if (op_bits(s, op) == 32) {
res &= 0xffffffff;
res = (int32_t)res;
}
return res;
}
@ -1132,8 +1132,8 @@ void tcg_optimize(TCGContext *s)
rl = args[0];
rh = args[1];
tcg_opt_gen_movi(s, op, args, rl, (uint32_t)a);
tcg_opt_gen_movi(s, op2, args2, rh, (uint32_t)(a >> 32));
tcg_opt_gen_movi(s, op, args, rl, (int32_t)a);
tcg_opt_gen_movi(s, op2, args2, rh, (int32_t)(a >> 32));
/* We've done all we need to do with the movi. Skip it. */
oi_next = op2->next;
@ -1153,8 +1153,8 @@ void tcg_optimize(TCGContext *s)
rl = args[0];
rh = args[1];
tcg_opt_gen_movi(s, op, args, rl, (uint32_t)r);
tcg_opt_gen_movi(s, op2, args2, rh, (uint32_t)(r >> 32));
tcg_opt_gen_movi(s, op, args, rl, (int32_t)r);
tcg_opt_gen_movi(s, op2, args2, rh, (int32_t)(r >> 32));
/* We've done all we need to do with the movi. Skip it. */
oi_next = op2->next;