[pypy-svn] r74160 - in pypy/branch/blackhole-improvement/pypy/jit/codewriter: . test
arigo at codespeak.net
arigo at codespeak.net
Wed Apr 28 13:37:47 CEST 2010
Author: arigo
Date: Wed Apr 28 13:37:45 2010
New Revision: 74160
Modified:
pypy/branch/blackhole-improvement/pypy/jit/codewriter/jitter.py
pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_jitter.py
Log:
ptr_eq, ptr_ne, ptr_iszero, ptr_nonzero,
and the same on exitswitches.
Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/jitter.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/jitter.py (original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/jitter.py Wed Apr 28 13:37:45 2010
@@ -99,7 +99,9 @@
return False # variable is also used in cur block
if v is op.result:
if op.opname not in ('int_lt', 'int_le', 'int_eq', 'int_ne',
- 'int_gt', 'int_ge', 'int_is_true'):
+ 'int_gt', 'int_ge', 'int_is_true',
+ 'ptr_eq', 'ptr_ne',
+ 'ptr_iszero', 'ptr_nonzero'):
return False # not a supported operation
# ok! optimize this case
block.operations.remove(op)
@@ -117,6 +119,13 @@
link.llexitcase = link.exitcase = not link.exitcase
opname = 'int_is_true'
args = [args[0]]
+ elif op.opname in ('ptr_eq', 'ptr_ne'):
+ if isinstance(args[0], Constant):
+ args = args[::-1]
+ if isinstance(args[1], Constant) and not args[1].value:
+ opname = {'ptr_eq': 'ptr_iszero',
+ 'ptr_ne': 'ptr_nonzero'}[op.opname]
+ args = [args[0]]
block.exitswitch = (opname,) + tuple(args)
return True
return False
@@ -352,6 +361,21 @@
sizedescr = self.cpu.sizeof(STRUCT)
return SpaceOperation('new', [sizedescr], op.result)
+ def _rewrite_op_ptr_eq(self, op, opname):
+ arg0, arg1 = op.args
+ if isinstance(arg0, Constant) and not arg0.value:
+ return SpaceOperation(opname, [arg1], op.result)
+ elif isinstance(arg1, Constant) and not arg1.value:
+ return SpaceOperation(opname, [arg0], op.result)
+ else:
+ return op
+
+ def rewrite_op_ptr_eq(self, op):
+ return self._rewrite_op_ptr_eq(op, 'ptr_iszero')
+
+ def rewrite_op_ptr_ne(self, op):
+ return self._rewrite_op_ptr_eq(op, 'ptr_nonzero')
+
# ____________________________________________________________
def _with_prefix(prefix):
Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_jitter.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_jitter.py (original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_jitter.py Wed Apr 28 13:37:45 2010
@@ -95,6 +95,54 @@
assert exits[0].exitcase == exits[0].llexitcase == linkcase2
assert exits[1].exitcase == exits[1].llexitcase == linkcase1
+def test_optimize_goto_if_not__ptr_eq():
+ for opname in ['ptr_eq', 'ptr_ne']:
+ v1 = Variable()
+ v2 = Variable()
+ v3 = Variable(); v3.concretetype = lltype.Bool
+ block = Block([v1, v2])
+ block.operations = [SpaceOperation(opname, [v1, v2], v3)]
+ block.exitswitch = v3
+ block.exits = exits = [FakeLink(False), FakeLink(True)]
+ res = Transformer().optimize_goto_if_not(block)
+ assert res == True
+ assert block.operations == []
+ assert block.exitswitch == (opname, v1, v2)
+ assert block.exits == exits
+
+def test_optimize_goto_if_not__ptr_iszero():
+ for opname in ['ptr_iszero', 'ptr_nonzero']:
+ v1 = Variable()
+ v3 = Variable(); v3.concretetype = lltype.Bool
+ block = Block([v1])
+ block.operations = [SpaceOperation(opname, [v1], v3)]
+ block.exitswitch = v3
+ block.exits = exits = [FakeLink(False), FakeLink(True)]
+ res = Transformer().optimize_goto_if_not(block)
+ assert res == True
+ assert block.operations == []
+ assert block.exitswitch == (opname, v1)
+ assert block.exits == exits
+
+def test_optimize_goto_if_not__ptr_eq_reduced():
+ c0 = Constant(lltype.nullptr(rclass.OBJECT), rclass.OBJECTPTR)
+ for opname, reducedname in [('ptr_eq', 'ptr_iszero'),
+ ('ptr_ne', 'ptr_nonzero')]:
+ for nullindex in [0, 1]:
+ v1 = Variable()
+ v3 = Variable(); v3.concretetype = lltype.Bool
+ block = Block([v1])
+ args = [v1, v1]
+ args[nullindex] = c0
+ block.operations = [SpaceOperation(opname, args, v3)]
+ block.exitswitch = v3
+ block.exits = exits = [FakeLink(False), FakeLink(True)]
+ res = Transformer().optimize_goto_if_not(block)
+ assert res == True
+ assert block.operations == []
+ assert block.exitswitch == (reducedname, v1)
+ assert block.exits == exits
+
def test_residual_call():
for RESTYPE in [lltype.Signed, rclass.OBJECTPTR,
lltype.Float, lltype.Void]:
@@ -258,3 +306,26 @@
assert block.operations == []
assert block.exits[0].target is block2
assert block.exits[0].args == [v1]
+
+def test_ptr_eq():
+ v1 = varoftype(rclass.OBJECTPTR)
+ v2 = varoftype(rclass.OBJECTPTR)
+ v3 = varoftype(lltype.Bool)
+ c0 = Constant(lltype.nullptr(rclass.OBJECT), rclass.OBJECTPTR)
+ #
+ for opname, reducedname in [('ptr_eq', 'ptr_iszero'),
+ ('ptr_ne', 'ptr_nonzero')]:
+ op = SpaceOperation(opname, [v1, v2], v3)
+ op1 = Transformer().rewrite_operation(op)
+ assert op1.opname == opname
+ assert op1.args == [v1, v2]
+ #
+ op = SpaceOperation(opname, [v1, c0], v3)
+ op1 = Transformer().rewrite_operation(op)
+ assert op1.opname == reducedname
+ assert op1.args == [v1]
+ #
+ op = SpaceOperation(opname, [c0, v2], v3)
+ op1 = Transformer().rewrite_operation(op)
+ assert op1.opname == reducedname
+ assert op1.args == [v2]
More information about the Pypy-commit
mailing list