From 64915775a725c200f6f2f23b5c0e5c1d8ddeac89 Mon Sep 17 00:00:00 2001 From: Dmitry Stogov Date: Wed, 10 Nov 2021 21:14:41 +0300 Subject: [PATCH] JIT: Fixed incorrect MOD into BW_AND optimization --- ext/opcache/jit/zend_jit_x86.dasc | 23 +++++++++-------------- ext/opcache/tests/jit/mul_007.phpt | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 14 deletions(-) create mode 100644 ext/opcache/tests/jit/mul_007.phpt diff --git a/ext/opcache/jit/zend_jit_x86.dasc b/ext/opcache/jit/zend_jit_x86.dasc index 53fd144196c..d8f953fe003 100644 --- a/ext/opcache/jit/zend_jit_x86.dasc +++ b/ext/opcache/jit/zend_jit_x86.dasc @@ -4966,7 +4966,6 @@ static int zend_jit_long_math_helper(dasm_State **Dst, { zend_bool same_ops = zend_jit_same_addr(op1_addr, op2_addr); zend_reg result_reg; - zval tmp; if (op1_info & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG)) { | IF_NOT_ZVAL_TYPE op1_addr, IS_LONG, >6 @@ -4975,19 +4974,6 @@ static int zend_jit_long_math_helper(dasm_State **Dst, | IF_NOT_ZVAL_TYPE op2_addr, IS_LONG, >6 } - if (opcode == ZEND_MOD && Z_MODE(op2_addr) == IS_CONST_ZVAL && - op1_range && - op1_range->min >= 0) { - zend_long l = Z_LVAL_P(Z_ZV(op2_addr)); - - if (zend_long_is_power_of_two(l)) { - /* Optimisation for mod of power of 2 */ - opcode = ZEND_BW_AND; - ZVAL_LONG(&tmp, l - 1); - op2_addr = ZEND_ADDR_CONST_ZVAL(&tmp); - } - } - if (opcode == ZEND_MOD) { result_reg = ZREG_RAX; 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, if (op2_lval == 0) { | SET_EX_OPLINE opline, r0 | jmp ->mod_by_zero + } else if (zend_long_is_power_of_two(op2_lval) && op1_range && op1_range->min >= 0) { + zval tmp; + zend_jit_addr tmp_addr; + + /* Optimisation for mod of power of 2 */ + ZVAL_LONG(&tmp, op2_lval - 1); + tmp_addr = ZEND_ADDR_CONST_ZVAL(&tmp); + | GET_ZVAL_LVAL result_reg, op1_addr + | LONG_MATH ZEND_BW_AND, result_reg, tmp_addr } else { result_reg = ZREG_RDX; if (op2_lval == -1) { diff --git a/ext/opcache/tests/jit/mul_007.phpt b/ext/opcache/tests/jit/mul_007.phpt new file mode 100644 index 00000000000..4cf7902027d --- /dev/null +++ b/ext/opcache/tests/jit/mul_007.phpt @@ -0,0 +1,18 @@ +--TEST-- +JIT MUL: 007 incorrect optimization +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +opcache.file_update_protection=0 +opcache.jit_buffer_size=1M +opcache.protect_memory=1 +--FILE-- + +DONE +--EXPECT-- +DONE \ No newline at end of file