diff --git a/Zend/Optimizer/block_pass.c b/Zend/Optimizer/block_pass.c index 0601994479e6..4a1387a2c8bf 100644 --- a/Zend/Optimizer/block_pass.c +++ b/Zend/Optimizer/block_pass.c @@ -199,7 +199,14 @@ static void zend_optimize_block(zend_basic_block *block, zend_op_array *op_array zval c; ZVAL_COPY(&c, &ZEND_OP1_LITERAL(src)); - if (zend_optimizer_update_op2_const(op_array, opline, &c)) { + if (opline->opcode == ZEND_TYPE_ASSERT) { + /* Does not consume its operand, which is used again later. */ + if (zend_optimizer_update_op2_const(op_array, opline, &c)) { + ++(*opt_count); + } else { + zval_ptr_dtor_nogc(&c); + } + } else if (zend_optimizer_update_op2_const(op_array, opline, &c)) { VAR_SOURCE(op2) = NULL; if (!zend_bitset_in(used_ext, VAR_NUM(src->result.var))) { literal_dtor(&ZEND_OP1_LITERAL(src)); diff --git a/Zend/Optimizer/zend_optimizer.c b/Zend/Optimizer/zend_optimizer.c index 93706a8a0d4c..17e4acf77401 100644 --- a/Zend/Optimizer/zend_optimizer.c +++ b/Zend/Optimizer/zend_optimizer.c @@ -679,6 +679,16 @@ bool zend_optimizer_replace_by_const(zend_op_array *op_array, if (opline->op2_type == type && opline->op2.var == var) { + if (opline->opcode == ZEND_TYPE_ASSERT) { + /* Does not consume its operand, which is used again later. */ + Z_TRY_ADDREF_P(val); + if (!zend_optimizer_update_op2_const(op_array, opline, val)) { + zval_ptr_dtor(val); + return false; + } + opline++; + continue; + } return zend_optimizer_update_op2_const(op_array, opline, val); } opline++; diff --git a/ext/opcache/tests/opt/type_assert_const_operand.phpt b/ext/opcache/tests/opt/type_assert_const_operand.phpt new file mode 100644 index 000000000000..e3c2f77ffb4e --- /dev/null +++ b/ext/opcache/tests/opt/type_assert_const_operand.phpt @@ -0,0 +1,35 @@ +--TEST-- +TYPE_ASSERT does not consume its operand, which may be replaced by a constant +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +opcache.optimization_level=-1 +opcache.file_update_protection=0 +--EXTENSIONS-- +opcache +--FILE-- +getMessage(), "\n"; +} +?> +--EXPECT-- +array(2) { + [0]=> + int(1) + [1]=> + int(2) +} +array_map(): Argument #2 ($array) must be of type array, int given