Skip to content

Commit 6491577

Browse files
committed
JIT: Fixed incorrect MOD into BW_AND optimization
1 parent 3081423 commit 6491577

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

ext/opcache/jit/zend_jit_x86.dasc

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4966,7 +4966,6 @@ static int zend_jit_long_math_helper(dasm_State **Dst,
49664966
{
49674967
zend_bool same_ops = zend_jit_same_addr(op1_addr, op2_addr);
49684968
zend_reg result_reg;
4969-
zval tmp;
49704969

49714970
if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) {
49724971
| IF_NOT_ZVAL_TYPE op1_addr, IS_LONG, >6
@@ -4975,19 +4974,6 @@ static int zend_jit_long_math_helper(dasm_State **Dst,
49754974
| IF_NOT_ZVAL_TYPE op2_addr, IS_LONG, >6
49764975
}
49774976

4978-
if (opcode == ZEND_MOD && Z_MODE(op2_addr) == IS_CONST_ZVAL &&
4979-
op1_range &&
4980-
op1_range->min >= 0) {
4981-
zend_long l = Z_LVAL_P(Z_ZV(op2_addr));
4982-
4983-
if (zend_long_is_power_of_two(l)) {
4984-
/* Optimisation for mod of power of 2 */
4985-
opcode = ZEND_BW_AND;
4986-
ZVAL_LONG(&tmp, l - 1);
4987-
op2_addr = ZEND_ADDR_CONST_ZVAL(&tmp);
4988-
}
4989-
}
4990-
49914977
if (opcode == ZEND_MOD) {
49924978
result_reg = ZREG_RAX;
49934979
if (Z_MODE(res_addr) == IS_MEM_ZVAL && Z_REG(res_addr) == ZREG_RAX) {
@@ -5097,6 +5083,15 @@ static int zend_jit_long_math_helper(dasm_State **Dst,
50975083
if (op2_lval == 0) {
50985084
| SET_EX_OPLINE opline, r0
50995085
| jmp ->mod_by_zero
5086+
} else if (zend_long_is_power_of_two(op2_lval) && op1_range && op1_range->min >= 0) {
5087+
zval tmp;
5088+
zend_jit_addr tmp_addr;
5089+
5090+
/* Optimisation for mod of power of 2 */
5091+
ZVAL_LONG(&tmp, op2_lval - 1);
5092+
tmp_addr = ZEND_ADDR_CONST_ZVAL(&tmp);
5093+
| GET_ZVAL_LVAL result_reg, op1_addr
5094+
| LONG_MATH ZEND_BW_AND, result_reg, tmp_addr
51005095
} else {
51015096
result_reg = ZREG_RDX;
51025097
if (op2_lval == -1) {

ext/opcache/tests/jit/mul_007.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
JIT MUL: 007 incorrect optimization
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.file_update_protection=0
7+
opcache.jit_buffer_size=1M
8+
opcache.protect_memory=1
9+
--FILE--
10+
<?php
11+
function test() {
12+
1.5%2%2%2/2%2;
13+
}
14+
test();
15+
?>
16+
DONE
17+
--EXPECT--
18+
DONE

0 commit comments

Comments
 (0)