[php-src] jit-arm64: Optimizing LONG MUL to SHIFT: refine the trigger condition and add overflow detection

From: Date: Mon, 26 Apr 2021 05:57:03 +0000
Subject: [php-src] jit-arm64: Optimizing LONG MUL to SHIFT: refine the trigger condition and add overflow detection
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-115800@lists.php.net to get a copy of this message
Author: Hao Sun (shqking) Date: 2021-04-26T05:56:33Z Commit: https://github.com/php/php-src/commit/036978dcea34354d2e7d8c6cbf40703cc5426c72 Raw diff: https://github.com/php/php-src/commit/036978dcea34354d2e7d8c6cbf40703cc5426c72.diff Optimizing LONG MUL to SHIFT: refine the trigger condition and add overflow detection LONG MUL can be optimzied into left shift if either operand is a power of two. Conditions "IS_SIGNED_32BIT()" and "is_power_of_two()" are used to filter out invalid candidates. However, there exists one exception, i.e. -2147483648(that is 0xffff,ffff,8000,0000). See the stand-alone case[1]. Assume "a = 3; b = -2147483648;". The expected result of "a * b" is one negative value. However, it would be optimized to "a << 31", which is positive. This trigger condition is refined. 1) For x86 implementation, another check for positive numbers is added. Note that LONG type, i.e. zend_long, is defined as int32_t for x86 arch and int64_t for x64 arch. This optimization only accepts values which can be represented by int32_t type as default. See IS_SIGNED_32BIlT(), 2) For AArch64, we employ helper function zend_long_is_power_of_two() since values of int64_t type are used. Overflow detection for left shifting is added in this patch as well. Note 1: bit helper functions are arch-independent and we move them into zend_jit_internals.h. Note 2: two test cases are added. Test case mul_003.phpt is used to check the trigger condition and mul_004.phpt is designed to check overflow detection. Note 3: overflow detection for x86 is not implemented yet as I think anotehr temporay register besides R0 is needed. Hence mul_004.phpt would fail on x86 machine. If we can use R1 as tmp_reg, the code can be updated as below. ``` | GET_ZVAL_LVAL result_reg, op1_addr if (may_overflow) { use_ovf_flag = 0; /* Compare 'op' and '((op << n) >> n)' for overflow. * Flag: jne -> overflow. je -> no overflow. */ tmp_reg = ZREG_R1 | mov Ra(tmp_reg), Ra(result_reg) | shl Ra(tmp_reg), floor_log2(Z_LVAL_P(Z_ZV(op2_addr))) | sar Ra(tmp_reg), floor_log2(Z_LVAL_P(Z_ZV(op2_addr))) | cmp Ra(tmp_reg), Ra(result_reg) } | shl Ra(result_reg), floor_log2(Z_LVAL_P(Z_ZV(op2_addr))) ``` [1]. https://godbolt.org/z/1vKbfv8oG Change-Id: Ie90e1d4e7c8b94a0c8f61386dfe650fa2c6879a1 Changed paths: A ext/opcache/tests/jit/arm64/mul_003.phpt A ext/opcache/tests/jit/arm64/mul_004.phpt M ext/opcache/jit/zend_jit.c M ext/opcache/jit/zend_jit_arm64.dasc M ext/opcache/jit/zend_jit_internal.h M ext/opcache/jit/zend_jit_x86.dasc Diff: diff --git a/ext/opcache/jit/zend_jit.c b/ext/opcache/jit/zend_jit.c index 52d9c686bd62..028e279a145f 100644 --- a/ext/opcache/jit/zend_jit.c +++ b/ext/opcache/jit/zend_jit.c @@ -195,11 +195,6 @@ static bool zend_is_commutative(zend_uchar opcode) opcode == ZEND_BW_XOR; } -static bool zend_long_is_power_of_two(zend_long x) -{ - return (x > 0) && !(x & (x - 1)); -} - #define OP_RANGE(ssa_op, opN) \ (((opline->opN##_type & (IS_TMP_VAR|IS_VAR|IS_CV)) && \ ssa->var_info && \ diff --git a/ext/opcache/jit/zend_jit_arm64.dasc b/ext/opcache/jit/zend_jit_arm64.dasc index 36be7fc86790..90d8c6ab06b4 100644 --- a/ext/opcache/jit/zend_jit_arm64.dasc +++ b/ext/opcache/jit/zend_jit_arm64.dasc @@ -1534,50 +1534,6 @@ static void zend_jit_stop_reuse_ip(void) reuse_ip = 0; } -/* bit helpers */ - -/* from http://aggregate.org/MAGIC/ */ -static uint32_t ones32(uint32_t x) -{ - x -= ((x >> 1) & 0x55555555); - x = (((x >> 2) & 0x33333333) + (x & 0x33333333)); - x = (((x >> 4) + x) & 0x0f0f0f0f); - x += (x >> 8); - x += (x >> 16); - return x & 0x0000003f; -} - -static uint32_t floor_log2(uint32_t x) -{ - ZEND_ASSERT(x != 0); - x |= (x >> 1); - x |= (x >> 2); - x |= (x >> 4); - x |= (x >> 8); - x |= (x >> 16); - return ones32(x) - 1; -} - -static bool is_power_of_two(uint32_t x) -{ - return !(x & (x - 1)) && x != 0; -} - -static bool has_concrete_type(uint32_t value_type) -{ - return is_power_of_two (value_type & (MAY_BE_ANY|MAY_BE_UNDEF)); -} - -static uint32_t concrete_type(uint32_t value_type) -{ - return floor_log2(value_type & (MAY_BE_ANY|MAY_BE_UNDEF)); -} - -static inline bool is_signed(double d) -{ - return (((unsigned char*)&d)[sizeof(double)-1] & 0x80) != 0; -} - static int zend_jit_interrupt_handler_stub(dasm_State **Dst) { |->interrupt_handler: @@ -3341,7 +3297,7 @@ static int zend_jit_math_long_long(dasm_State **Dst, bool same_ops = zend_jit_same_addr(op1_addr, op2_addr); zend_reg result_reg; zend_reg tmp_reg = ZREG_REG0; - bool use_mul = 0; + bool use_ovf_flag = 1; if (Z_MODE(res_addr) == IS_REG && (res_info & MAY_BE_LONG)) { if (may_overflow && (res_info & MAY_BE_GUARD) @@ -3363,8 +3319,7 @@ static int zend_jit_math_long_long(dasm_State **Dst, if (opcode == ZEND_MUL && Z_MODE(op2_addr) == IS_CONST_ZVAL && - IS_SIGNED_32BIT(Z_LVAL_P(Z_ZV(op2_addr))) && - is_power_of_two(Z_LVAL_P(Z_ZV(op2_addr)))) { + zend_long_is_power_of_two(Z_LVAL_P(Z_ZV(op2_addr)))) { if (Z_LVAL_P(Z_ZV(op2_addr)) == 2) { if (Z_MODE(op1_addr) == IS_REG) { @@ -3374,15 +3329,21 @@ static int zend_jit_math_long_long(dasm_State **Dst, | adds Rx(result_reg), Rx(result_reg), Rx(result_reg) } } else { - | GET_ZVAL_LVAL result_reg, op1_addr, TMP1 - | mov TMP1, #floor_log2(Z_LVAL_P(Z_ZV(op2_addr))) - | lsl Rx(result_reg), Rx(result_reg), TMP1 - | // TODO: overflow may be missed + | GET_ZVAL_LVAL ZREG_TMP1, op1_addr, TMP2 + | mov TMP2, #zend_long_floor_log2(Z_LVAL_P(Z_ZV(op2_addr))) + | lsl Rx(result_reg), TMP1, TMP2 + if (may_overflow) { + /* Compare 'op' and '((op << n) >> n)' for overflow. + * Flag: bne -> overflow. beq -> no overflow. + */ + use_ovf_flag = 0; + | asr TMP3, Rx(result_reg), TMP2 + | cmp TMP1, TMP3 + } } } else if (opcode == ZEND_MUL && Z_MODE(op1_addr) == IS_CONST_ZVAL && - IS_SIGNED_32BIT(Z_LVAL_P(Z_ZV(op1_addr))) && - is_power_of_two(Z_LVAL_P(Z_ZV(op1_addr)))) { + zend_long_is_power_of_two(Z_LVAL_P(Z_ZV(op1_addr)))) { if (Z_LVAL_P(Z_ZV(op1_addr)) == 2) { if (Z_MODE(op2_addr) == IS_REG) { @@ -3392,10 +3353,17 @@ static int zend_jit_math_long_long(dasm_State **Dst, | adds Rx(result_reg), Rx(result_reg), Rx(result_reg) } } else { - | GET_ZVAL_LVAL result_reg, op2_addr, TMP1 - | mov TMP1, #floor_log2(Z_LVAL_P(Z_ZV(op1_addr))) - | lsl Rx(result_reg), Rx(result_reg), TMP1 - | // TODO: overflow may be missed + | GET_ZVAL_LVAL ZREG_TMP1, op2_addr, TMP2 + | mov TMP2, #zend_long_floor_log2(Z_LVAL_P(Z_ZV(op1_addr))) + | lsl Rx(result_reg), TMP1, TMP2 + if (may_overflow) { + /* Compare 'op' and '((op << n) >> n)' for overflow. + * Flag: bne -> overflow. beq -> no overflow. + */ + use_ovf_flag = 0; + | asr TMP3, Rx(result_reg), TMP2 + | cmp TMP1, TMP3 + } } } else if (opcode == ZEND_DIV && (Z_MODE(op2_addr) == IS_CONST_ZVAL && @@ -3421,7 +3389,6 @@ static int zend_jit_math_long_long(dasm_State **Dst, | NIY // TODO: test #endif } else if (opcode == ZEND_MUL) { - use_mul = 1; | GET_ZVAL_LVAL ZREG_TMP2, op1_addr, TMP1 | GET_ZVAL_LVAL ZREG_TMP3, op2_addr, TMP1 | mul Rx(result_reg), TMP2, TMP3 @@ -3433,6 +3400,7 @@ static int zend_jit_math_long_long(dasm_State **Dst, * currently, and we put 'asr' and 'cmp' separately. * Flag: bne -> overflow. beq -> no overflow. */ + use_ovf_flag = 0; | smulh TMP1, TMP2, TMP3 | asr TMP2, Rx(result_reg), #63 | cmp TMP1, TMP2 @@ -3455,10 +3423,10 @@ static int zend_jit_math_long_long(dasm_State **Dst, int32_t exit_point = zend_jit_trace_get_exit_point(opline, 0); const void *exit_addr = zend_jit_trace_get_exit_addr(exit_point); if ((res_info & MAY_BE_ANY) == MAY_BE_LONG) { - if (use_mul) { - | bne >3 - } else { + if (use_ovf_flag) { | bvs >3 + } else { + | bne >3 } |.cold_code |3: @@ -3468,10 +3436,10 @@ static int zend_jit_math_long_long(dasm_State **Dst, | mov Rx(Z_REG(res_addr)), Rx(result_reg) } } else if ((res_info & MAY_BE_ANY) == MAY_BE_DOUBLE) { - if (use_mul) { - | beq >3 - } else { + if (use_ovf_flag) { | bvc >3 + } else { + | beq >3 } |.cold_code |3: @@ -3482,16 +3450,16 @@ static int zend_jit_math_long_long(dasm_State **Dst, } } else { if (res_info & MAY_BE_LONG) { - if (use_mul) { - | bne >1 - } else { + if (use_ovf_flag) { | bvs >1 + } else { + | bne >1 } } else { - if (use_mul) { - | beq >1 - } else { + if (use_ovf_flag) { | bvc >1 + } else { + | beq >1 } } } diff --git a/ext/opcache/jit/zend_jit_internal.h b/ext/opcache/jit/zend_jit_internal.h index 203a32c383a6..29eab0491949 100644 --- a/ext/opcache/jit/zend_jit_internal.h +++ b/ext/opcache/jit/zend_jit_internal.h @@ -738,4 +738,59 @@ static zend_always_inline bool zend_jit_may_be_polymorphic_call(const zend_op *o # endif #endif /* !JIT_CACHE_FLUSH */ +/* bit helpers */ + +static bool zend_long_is_power_of_two(zend_long x) +{ + return (x > 0) && !(x & (x - 1)); +} + +static uint32_t zend_long_floor_log2(uint64_t x) +{ + ZEND_ASSERT(zend_long_is_power_of_two(x)); + return __builtin_ctzll(x); +} + +/* from http://aggregate.org/MAGIC/ */ +static uint32_t ones32(uint32_t x) +{ + x -= ((x >> 1) & 0x55555555); + x = (((x >> 2) & 0x33333333) + (x & 0x33333333)); + x = (((x >> 4) + x) & 0x0f0f0f0f); + x += (x >> 8); + x += (x >> 16); + return x & 0x0000003f; +} + +static uint32_t floor_log2(uint32_t x) +{ + ZEND_ASSERT(x != 0); + x |= (x >> 1); + x |= (x >> 2); + x |= (x >> 4); + x |= (x >> 8); + x |= (x >> 16); + return ones32(x) - 1; +} + +static bool is_power_of_two(uint32_t x) +{ + return !(x & (x - 1)) && x != 0; +} + +static bool has_concrete_type(uint32_t value_type) +{ + return is_power_of_two (value_type & (MAY_BE_ANY|MAY_BE_UNDEF)); +} + +static uint32_t concrete_type(uint32_t value_type) +{ + return floor_log2(value_type & (MAY_BE_ANY|MAY_BE_UNDEF)); +} + +static inline bool is_signed(double d) +{ + return (((unsigned char*)&d)[sizeof(double)-1] & 0x80) != 0; +} + #endif /* ZEND_JIT_INTERNAL_H */ diff --git a/ext/opcache/jit/zend_jit_x86.dasc b/ext/opcache/jit/zend_jit_x86.dasc index c027fb63773b..8dbf12b94b5e 100644 --- a/ext/opcache/jit/zend_jit_x86.dasc +++ b/ext/opcache/jit/zend_jit_x86.dasc @@ -1703,50 +1703,6 @@ static void zend_jit_stop_reuse_ip(void) reuse_ip = 0; } -/* bit helpers */ - -/* from http://aggregate.org/MAGIC/ */ -static uint32_t ones32(uint32_t x) -{ - x -= ((x >> 1) & 0x55555555); - x = (((x >> 2) & 0x33333333) + (x & 0x33333333)); - x = (((x >> 4) + x) & 0x0f0f0f0f); - x += (x >> 8); - x += (x >> 16); - return x & 0x0000003f; -} - -static uint32_t floor_log2(uint32_t x) -{ - ZEND_ASSERT(x != 0); - x |= (x >> 1); - x |= (x >> 2); - x |= (x >> 4); - x |= (x >> 8); - x |= (x >> 16); - return ones32(x) - 1; -} - -static bool is_power_of_two(uint32_t x) -{ - return !(x & (x - 1)) && x != 0; -} - -static bool has_concrete_type(uint32_t value_type) -{ - return is_power_of_two (value_type & (MAY_BE_ANY|MAY_BE_UNDEF)); -} - -static uint32_t concrete_type(uint32_t value_type) -{ - return floor_log2(value_type & (MAY_BE_ANY|MAY_BE_UNDEF)); -} - -static inline bool is_signed(double d) -{ - return (((unsigned char*)&d)[sizeof(double)-1] & 0x80) != 0; -} - static int zend_jit_interrupt_handler_stub(dasm_State **Dst) { |->interrupt_handler: @@ -4245,6 +4201,7 @@ static int zend_jit_math_long_long(dasm_State **Dst, if (opcode == ZEND_MUL && Z_MODE(op2_addr) == IS_CONST_ZVAL && + Z_LVAL_P(Z_ZV(op2_addr)) > 0 && IS_SIGNED_32BIT(Z_LVAL_P(Z_ZV(op2_addr))) && is_power_of_two(Z_LVAL_P(Z_ZV(op2_addr)))) { @@ -4253,9 +4210,13 @@ static int zend_jit_math_long_long(dasm_State **Dst, } else { | GET_ZVAL_LVAL result_reg, op1_addr | shl Ra(result_reg), floor_log2(Z_LVAL_P(Z_ZV(op2_addr))) + if (may_overflow) { + // TODO: check overflow. + } } } else if (opcode == ZEND_MUL && Z_MODE(op1_addr) == IS_CONST_ZVAL && + Z_LVAL_P(Z_ZV(op1_addr)) > 0 && IS_SIGNED_32BIT(Z_LVAL_P(Z_ZV(op1_addr))) && is_power_of_two(Z_LVAL_P(Z_ZV(op1_addr)))) { @@ -4264,6 +4225,9 @@ static int zend_jit_math_long_long(dasm_State **Dst, } else { | GET_ZVAL_LVAL result_reg, op2_addr | shl Ra(result_reg), floor_log2(Z_LVAL_P(Z_ZV(op1_addr))) + if (may_overflow) { + // TODO: check overflow. + } } } else if (opcode == ZEND_DIV && (Z_MODE(op2_addr) == IS_CONST_ZVAL && diff --git a/ext/opcache/tests/jit/arm64/mul_003.phpt b/ext/opcache/tests/jit/arm64/mul_003.phpt new file mode 100644 index 000000000000..7404e8692678 --- /dev/null +++ b/ext/opcache/tests/jit/arm64/mul_003.phpt @@ -0,0 +1,29 @@ +--TEST-- +JIT MUL: 003 boundary value for optmizing MUL to SHIFT +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +opcache.file_update_protection=0 +opcache.jit_buffer_size=32M +;opcache.jit_debug=257 +--EXTENSIONS-- +opcache +--FILE-- +<?php + +function mul2_bound(int $a) { + $res = $a * -2147483648; + var_dump($res); +} + +function mul1_bound(int $a) { + $res = -2147483648 * $a; + var_dump($res); +} + +mul2_bound(3); +mul1_bound(3); +?> +--EXPECT-- +int(-6442450944) +int(-6442450944) \ No newline at end of file diff --git a/ext/opcache/tests/jit/arm64/mul_004.phpt b/ext/opcache/tests/jit/arm64/mul_004.phpt new file mode 100644 index 000000000000..438e4325245a --- /dev/null +++ b/ext/opcache/tests/jit/arm64/mul_004.phpt @@ -0,0 +1,59 @@ +--TEST-- +JIT MUL: 004 Overflow check for optmizing MUL to SHIFT +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +opcache.file_update_protection=0 +opcache.jit_buffer_size=32M +;opcache.jit_debug=257 +--EXTENSIONS-- +opcache +--FILE-- +<?php + +function mul2_8(int $a) { + $res = $a * 8; // shift cnt: 3 + var_dump($res); +} + +function mul1_16(int $a) { + $res = 16 * $a; // shift cnt: 4 + var_dump($res); +} + +function mul2_big_int32(int $a) { + $res = $a * 0x10000000; // shift cnt: 29 + var_dump($res); +} + +function mul2_big_int64(int $a) { + $res = $a * 0x100000000; // shift cnt: 32 + var_dump($res); +} + +mul2_8(3); +mul2_8(-11); +mul2_8(0x7fffffffffffffff); +mul1_16(3); +mul1_16(-13); +mul1_16(0x7fffffffffffffff); +mul2_big_int32(3); +mul2_big_int32(-3); +mul2_big_int32(0x10000000000); +mul2_big_int64(3); +mul2_big_int64(-3); +mul2_big_int64(0x100000000); +?> +--EXPECT-- +int(24) +int(-88) +float(7.378697629483821E+19) +int(48) +int(-208) +float(1.4757395258967641E+20) +int(805306368) +int(-805306368) +float(2.9514790517935283E+20) +int(12884901888) +int(-12884901888) +float(1.8446744073709552E+19) \ No newline at end of file

« previous php.cvs (#115800) next »