Move obvious optimization patterns from pass1 directly to compiler

This commit is contained in:
Dmitry Stogov 2019-10-03 14:57:20 +03:00
parent a5465728c2
commit de3c1eb85e
3 changed files with 27 additions and 39 deletions

View File

@ -671,8 +671,21 @@ void zend_do_free(znode *op1) /* {{{ */
}
if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) {
if (opline->opcode == ZEND_BOOL || opline->opcode == ZEND_BOOL_NOT) {
return;
switch (opline->opcode) {
case ZEND_BOOL:
case ZEND_BOOL_NOT:
/* boolean resuls don't have to be freed */
return;
case ZEND_POST_INC_STATIC_PROP:
case ZEND_POST_DEC_STATIC_PROP:
case ZEND_POST_INC_OBJ:
case ZEND_POST_DEC_OBJ:
case ZEND_POST_INC:
case ZEND_POST_DEC:
/* convert $i++ to ++$i */
opline->opcode -= 2;
opline->result_type = IS_UNUSED;
return;
}
}
@ -3268,8 +3281,12 @@ int zend_compile_func_cast(znode *result, zend_ast_list *args, uint32_t type) /*
}
zend_compile_expr(&arg_node, args->child[0]);
opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL);
opline->extended_value = type;
if (type == _IS_BOOL) {
opline = zend_emit_op_tmp(result, ZEND_BOOL, &arg_node, NULL);
} else {
opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL);
opline->extended_value = type;
}
return SUCCESS;
}
/* }}} */
@ -7295,11 +7312,13 @@ void zend_compile_cast(znode *result, zend_ast *ast) /* {{{ */
zend_compile_expr(&expr_node, expr_ast);
opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL);
opline->extended_value = ast->attr;
if (ast->attr == IS_NULL) {
if (ast->attr == _IS_BOOL) {
opline = zend_emit_op_tmp(result, ZEND_BOOL, &expr_node, NULL);
} else if (ast->attr == IS_NULL) {
zend_error(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
} else {
opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL);
opline->extended_value = ast->attr;
}
}
/* }}} */

View File

@ -24,10 +24,8 @@
* - constant casting (ADD expects numbers, CONCAT strings, etc)
* - constant expression evaluation
* - optimize constant conditional JMPs
* - convert CAST(IS_BOOL,x) into BOOL(x)
* - pre-evaluate constant function calls
* - eliminate FETCH $GLOBALS followed by FETCH_DIM/UNSET_DIM/ISSET_ISEMPTY_DIM
* - change $i++ to ++$i where possible
*/
#include "php.h"
@ -249,12 +247,6 @@ constant_binary_op:
break;
}
}
if (opline->extended_value == _IS_BOOL) {
/* T = CAST(X, IS_BOOL) => T = BOOL(X) */
opline->opcode = ZEND_BOOL;
opline->extended_value = 0;
}
break;
case ZEND_BW_NOT:
@ -705,27 +697,6 @@ constant_binary_op:
}
break;
case ZEND_POST_INC_STATIC_PROP:
case ZEND_POST_DEC_STATIC_PROP:
case ZEND_POST_INC_OBJ:
case ZEND_POST_DEC_OBJ:
case ZEND_POST_INC:
case ZEND_POST_DEC: {
/* POST_INC, FREE => PRE_INC */
zend_op *next_op = opline + 1;
if (next_op >= end) {
break;
}
if (next_op->opcode == ZEND_FREE &&
next_op->op1.var == opline->result.var) {
MAKE_NOP(next_op);
opline->opcode -= 2;
opline->result_type = IS_UNUSED;
}
}
break;
case ZEND_JMPZ_EX:
case ZEND_JMPNZ_EX:
/* convert Ti = JMPZ_EX(C, L) => Ti = QM_ASSIGN(C)

View File

@ -939,10 +939,8 @@ static void zend_optimize(zend_op_array *op_array,
* - constant casting (ADD expects numbers, CONCAT strings, etc)
* - constant expression evaluation
* - optimize constant conditional JMPs
* - convert CAST(IS_BOOL,x) into BOOL(x)
* - pre-evaluate constant function calls
* - eliminate FETCH $GLOBALS followed by FETCH_DIM/UNSET_DIM/ISSET_ISEMPTY_DIM
* - change $i++ to ++$i where possible
*/
if (ZEND_OPTIMIZER_PASS_1 & ctx->optimization_level) {
zend_optimizer_pass1(op_array, ctx);