Commit e1c04233 authored by Armin Rigo's avatar Armin Rigo
Browse files

Generate more compact code for bit tests using the TEST instruction:

     i1 = int_and(i0, 7); i2 = int_is_zero(i1); guard_true(i2)

     =>

     i2 = int_test_is_zero(i0, 7); guard_true(i2)

The more compact version doesn't need to allocate a register, copy
a value there, AND a constant, and then CMP the result with zero.
Instead a single TEST instruction suffices.

--HG--
branch : int-test-is-zero
parent 41990e65c077
......@@ -1491,6 +1491,8 @@ class BaseBackendTest(Runner):
(rop.INT_NE, lambda x, y: x != y),
(rop.INT_GT, lambda x, y: x > y),
(rop.INT_GE, lambda x, y: x >= y),
(rop.INT_TEST_IS_ZERO, lambda x, y: (x & y) == 0),
(rop.INT_TEST_IS_TRUE, lambda x, y: (x & y) != 0),
]:
for opguard, guard_case in [
(rop.GUARD_FALSE, False),
......
......@@ -546,6 +546,8 @@ for _op in [rop.INT_LT,
rop.UINT_LE,
rop.UINT_GT,
rop.UINT_GE,
rop.INT_TEST_IS_ZERO,
rop.INT_TEST_IS_TRUE,
]:
OPERATIONS.append(BinaryOperation(_op, boolres=True))
......
......@@ -1281,6 +1281,17 @@ class Assembler386(BaseAssembler, VectorAssemblerMixin):
self.flush_cc(cond, result_loc)
return genop_cmp
def _testop(cond):
cond = rx86.Conditions[cond]
#
def genop_test(self, op, arglocs, result_loc):
if arglocs[1].is_stack() or isinstance(arglocs[0], ImmedLoc):
self.mc.TEST(arglocs[1], arglocs[0])
else:
self.mc.TEST(arglocs[0], arglocs[1])
self.flush_cc(cond, result_loc)
return genop_test
def _if_parity_clear_zero_and_carry(self):
jnp_location = self.mc.emit_forward_jump('NP')
# CMP EBP, 0: as EBP cannot be null here, that operation should
......@@ -1401,6 +1412,9 @@ class Assembler386(BaseAssembler, VectorAssemblerMixin):
genop_float_gt = _cmpop_float("A", "B")
genop_float_ge = _cmpop_float("AE","BE")
genop_int_test_is_zero = _testop("Z")
genop_int_test_is_true = _testop("NZ")
def genop_math_sqrt(self, op, arglocs, resloc):
self.mc.SQRTSD(arglocs[0], resloc)
......
......@@ -644,6 +644,8 @@ class RegAlloc(BaseRegalloc, VectorRegallocMixin):
consider_uint_ge = _consider_compop
consider_ptr_eq = consider_instance_ptr_eq = _consider_compop
consider_ptr_ne = consider_instance_ptr_ne = _consider_compop
consider_int_test_is_zero = _consider_compop
consider_int_test_is_true = _consider_compop
def _consider_float_op(self, op):
loc1 = self.xrm.loc(op.getarg(1))
......
......@@ -699,6 +699,9 @@ class AbstractX86CodeBuilder(object):
TEST_ai = insn(rex_w, '\xF7', orbyte(0<<3), mem_reg_plus_scaled_reg_plus_const(1), immediate(2))
TEST_mi = insn(rex_w, '\xF7', orbyte(0<<3), mem_reg_plus_const(1), immediate(2))
TEST_ji = insn(rex_w, '\xF7', orbyte(0<<3), abs_(1), immediate(2))
TEST_ri = insn(rex_w, '\xF7', orbyte(0<<3), register(1), '\xC0', immediate(2))
TEST_bi = insn(rex_w, '\xF7', orbyte(0<<3), stack_bp(1), immediate(2))
TEST_br = insn(rex_w, '\x85', register(2,8), stack_bp(1))
BTS_mr = insn(rex_w, '\x0F\xAB', register(2,8), mem_reg_plus_const(1))
BTS_jr = insn(rex_w, '\x0F\xAB', register(2,8), abs_(1))
......
......@@ -311,6 +311,8 @@ class TestRx86_32(object):
return [] # MOV AL, [immediate]: there is a special encoding
if methname == 'MOV8_jr' and args[1] == rx86.R.al:
return [] # MOV [immediate], AL: there is a special encoding
if methname == 'TEST_ri' and args[0] == rx86.R.eax:
return [] # TEST EAX, constant: there is a special encoding
return [args]
......
......@@ -542,6 +542,12 @@ class BlackholeInterpreter(object):
@arguments("i", "i", returns="i")
def bhimpl_int_signext(a, b):
return int_signext(a, b)
@arguments("i", "i", returns="i")
def bhimpl_int_test_is_zero(a, b):
return (a & b) == 0
@arguments("i", "i", returns="i")
def bhimpl_int_test_is_true(a, b):
return (a & b) != 0
@arguments("i", "i", returns="i")
def bhimpl_uint_lt(a, b):
......
......@@ -442,6 +442,8 @@ def _make_execute_list():
rop.GC_STORE_INDEXED,
rop.LOAD_FROM_GC_TABLE,
rop.LOAD_EFFECTIVE_ADDRESS,
rop.INT_TEST_IS_ZERO,
rop.INT_TEST_IS_TRUE,
): # list of opcodes never executed by pyjitpl
continue
if rop._VEC_PURE_FIRST <= value <= rop._VEC_PURE_LAST:
......
......@@ -646,6 +646,15 @@ class OptRewrite(Optimization):
elif info == INFO_NULL:
self.make_constant_int(op, not expect_nonnull)
else:
box = get_box_replacement(box)
box1 = self.optimizer.as_operation(box)
if box1 is not None and box1.getopnum() == rop.INT_AND:
if expect_nonnull:
opnum = rop.INT_TEST_IS_TRUE
else:
opnum = rop.INT_TEST_IS_ZERO
args = [box1.getarg(0), box1.getarg(1)]
op = self.replace_op_with(op, opnum, args=args)
return self.emit(op)
def optimize_INT_IS_TRUE(self, op):
......
......@@ -5790,3 +5790,33 @@ class TestOptimizeBasic(BaseTestBasic):
i57 = int_or(i51, i52)
"""
self.optimize_loop(ops, expected)
def test_int_test_is_zero(self):
ops = """
[i1, i2]
i51 = int_and(i1, i2)
i52 = int_is_zero(i51)
guard_true(i52) []
"""
expected = """
[i1, i2]
i51 = int_and(i1, i2) # likely dead instruction
i52 = int_test_is_zero(i1, i2)
guard_true(i52) []
"""
self.optimize_loop(ops, expected)
def test_int_test_is_true(self):
ops = """
[i1, i2]
i51 = int_and(i1, i2)
i52 = int_is_true(i51)
guard_true(i52) []
"""
expected = """
[i1, i2]
i51 = int_and(i1, i2) # likely dead instruction
i52 = int_test_is_true(i1, i2)
guard_true(i52) []
"""
self.optimize_loop(ops, expected)
......@@ -1036,6 +1036,8 @@ _oplist = [
'INT_NEG/1/i',
'INT_INVERT/1/i',
'INT_FORCE_GE_ZERO/1/i',
'INT_TEST_IS_ZERO/2b/i',
'INT_TEST_IS_TRUE/2b/i',
#
'SAME_AS/1/ifr', # gets a Const or a Box, turns it into another Box
'CAST_PTR_TO_INT/1/i',
......
......@@ -4832,3 +4832,11 @@ class TestLLtype(BaseLLtypeTests, LLJitMixin):
res2 = self.interp_operations(f, [6])
assert res1 == res2
self.check_operations_history(guard_class=1, record_exact_class=0)
def test_int_test_instructions(self):
def f(x, y):
if (x & 7) == 0 and (y & 7) != 0:
return 1
return 0
res = self.interp_operations(f, [24, 25])
assert res == 1
......@@ -187,6 +187,8 @@ def _int_comparison_operations():
(rop.UINT_LE, lambda x, y: r_uint(x) <= r_uint(y)),
(rop.UINT_GT, lambda x, y: r_uint(x) > r_uint(y)),
(rop.UINT_GE, lambda x, y: r_uint(x) >= r_uint(y)),
(rop.INT_TEST_IS_ZERO, lambda x, y: (x & y) == 0),
(rop.INT_TEST_IS_TRUE, lambda x, y: (x & y) != 0),
]:
for i in range(20):
x = pick()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment