com php-src: Avoid "$a = $a + ..." to " $a += ..." conversion (compound assignm ents work slower). Added DFA optimization patte rs (e.g. "$a = $a + 1"
to " ++$a"), that previously worked only for com pound assignments.: ext/opcache/Optimizer/dfa_pass. c ext/opcache/Optimizer/pass1.c

From: Date: Thu, 03 Oct 2019 13:33:17 +0000
Subject: com php-src: Avoid "$a = $a + ..." to " $a += ..." conversion (compound assignm ents work slower). Added DFA optimization patte rs (e.g. "$a = $a + 1"
to " ++$a"), that previously worked only for com pound assignments.: ext/opcache/Optimizer/dfa_pass. c ext/opcache/Optimizer/pass1.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-108703@lists.php.net to get a copy of this message
Commit: 39d1e50b2d5a513a8e5f36c08c4984680dbe0c65 Author: Dmitry Stogov <dmitry@zend.com> Thu, 3 Oct 2019 16:33:17 +0300 Parents: de3c1eb85e4585d4db8307d5c30991ed6973f4d3 Branches: master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=39d1e50b2d5a513a8e5f36c08c4984680dbe0c65 Log: Avoid "$a = $a + ..." to "$a += ..." conversion (compound assignments work slower). Added DFA optimization patters (e.g. "$a = $a + 1" to "++$a"), that previously worked only for compound assignments. Changed paths: M ext/opcache/Optimizer/dfa_pass.c M ext/opcache/Optimizer/pass1.c Diff: diff --git a/ext/opcache/Optimizer/dfa_pass.c b/ext/opcache/Optimizer/dfa_pass.c index 5ea77759217..f3156e1fa59 100644 --- a/ext/opcache/Optimizer/dfa_pass.c +++ b/ext/opcache/Optimizer/dfa_pass.c @@ -1168,8 +1168,64 @@ void zend_dfa_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx /* Update opcodes */ op_array->opcodes[op_2].result_type = opline->op1_type; op_array->opcodes[op_2].result.var = opline->op1.var; + MAKE_NOP(opline); remove_nops = 1; + + if (op_array->opcodes[op_2].opcode == ZEND_SUB + && op_array->opcodes[op_2].op1_type == op_array->opcodes[op_2].result_type + && op_array->opcodes[op_2].op1.var == op_array->opcodes[op_2].result.var + && op_array->opcodes[op_2].op2_type == IS_CONST + && Z_TYPE_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op2.constant)) == IS_LONG + && Z_LVAL_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op2.constant)) == 1 + && ssa->ops[op_2].op1_use >= 0 + && !(ssa->var_info[ssa->ops[op_2].op1_use].type & (MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) { + + op_array->opcodes[op_2].opcode = ZEND_PRE_DEC; + SET_UNUSED(op_array->opcodes[op_2].op2); + SET_UNUSED(op_array->opcodes[op_2].result); + + ssa->ops[op_2].result_def = -1; + ssa->ops[op_2].op1_def = v; + + } else if (op_array->opcodes[op_2].opcode == ZEND_ADD + && op_array->opcodes[op_2].op1_type == op_array->opcodes[op_2].result_type + && op_array->opcodes[op_2].op1.var == op_array->opcodes[op_2].result.var + && op_array->opcodes[op_2].op2_type == IS_CONST + && Z_TYPE_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op2.constant)) == IS_LONG + && Z_LVAL_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op2.constant)) == 1 + && ssa->ops[op_2].op1_use >= 0 + && !(ssa->var_info[ssa->ops[op_2].op1_use].type & (MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) { + + op_array->opcodes[op_2].opcode = ZEND_PRE_INC; + SET_UNUSED(op_array->opcodes[op_2].op2); + SET_UNUSED(op_array->opcodes[op_2].result); + + ssa->ops[op_2].result_def = -1; + ssa->ops[op_2].op1_def = v; + + } else if (op_array->opcodes[op_2].opcode == ZEND_ADD + && op_array->opcodes[op_2].op2_type == op_array->opcodes[op_2].result_type + && op_array->opcodes[op_2].op2.var == op_array->opcodes[op_2].result.var + && op_array->opcodes[op_2].op1_type == IS_CONST + && Z_TYPE_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op1.constant)) == IS_LONG + && Z_LVAL_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op1.constant)) == 1 + && ssa->ops[op_2].op2_use >= 0 + && !(ssa->var_info[ssa->ops[op_2].op2_use].type & (MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) { + + op_array->opcodes[op_2].opcode = ZEND_PRE_INC; + op_array->opcodes[op_2].op1_type = op_array->opcodes[op_2].op2_type; + op_array->opcodes[op_2].op1.var = op_array->opcodes[op_2].op2.var; + SET_UNUSED(op_array->opcodes[op_2].op2); + SET_UNUSED(op_array->opcodes[op_2].result); + + ssa->ops[op_2].result_def = -1; + ssa->ops[op_2].op1_def = v; + ssa->ops[op_2].op1_use = ssa->ops[op_2].op2_use; + ssa->ops[op_2].op1_use_chain = ssa->ops[op_2].op2_use_chain; + ssa->ops[op_2].op2_use = -1; + ssa->ops[op_2].op2_use_chain = -1; + } } } else if (opline->op2_type == IS_CONST || ((opline->op2_type & (IS_TMP_VAR|IS_VAR|IS_CV)) diff --git a/ext/opcache/Optimizer/pass1.c b/ext/opcache/Optimizer/pass1.c index c166fe95e2a..3a17cee011e 100644 --- a/ext/opcache/Optimizer/pass1.c +++ b/ext/opcache/Optimizer/pass1.c @@ -69,7 +69,7 @@ void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx) goto constant_binary_op; } } - goto try_compound_assignment; + break; case ZEND_MOD: case ZEND_SL: @@ -95,7 +95,7 @@ void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx) goto constant_binary_op; } } - goto try_compound_assignment; + break; case ZEND_CONCAT: case ZEND_FAST_CONCAT: @@ -112,64 +112,11 @@ void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx) goto constant_binary_op; } } - goto try_compound_assignment; + break; case ZEND_BW_OR: case ZEND_BW_AND: case ZEND_BW_XOR: - if (opline->op1_type == IS_CONST && - opline->op2_type == IS_CONST) { - - goto constant_binary_op; - } else { - zend_op *next_opline; - -try_compound_assignment: - next_opline = opline + 1;; - while (next_opline < end && next_opline->opcode == ZEND_NOP) { - ++next_opline; - } - - if (next_opline >= end || next_opline->opcode != ZEND_ASSIGN) { - break; - } - - /* change $i=expr+$i to $i=$i+expr so that the following optimization - * works on it. Only do this if we are ignoring operator overloading, - * as operand order might be significant otherwise. */ - if ((ctx->optimization_level & ZEND_OPTIMIZER_IGNORE_OVERLOADING) - && (opline->op2_type & (IS_VAR | IS_CV)) - && opline->op2.var == next_opline->op1.var && - (opline->opcode == ZEND_ADD || - opline->opcode == ZEND_MUL || - opline->opcode == ZEND_BW_OR || - opline->opcode == ZEND_BW_AND || - opline->opcode == ZEND_BW_XOR)) { - zend_uchar tmp_type = opline->op1_type; - znode_op tmp = opline->op1; - - if (opline->opcode != ZEND_ADD - || (opline->op1_type == IS_CONST - && Z_TYPE(ZEND_OP1_LITERAL(opline)) != IS_ARRAY)) { - /* protection from array add: $a = array + $a is not commutative! */ - COPY_NODE(opline->op1, opline->op2); - COPY_NODE(opline->op2, tmp); - } - } - - if (ZEND_IS_BINARY_ASSIGN_OP_OPCODE(opline->opcode) - && (opline->op1_type & (IS_VAR | IS_CV)) - && opline->op1.var == next_opline->op1.var - && opline->op1_type == next_opline->op1_type) { - opline->extended_value = opline->opcode; - opline->opcode = ZEND_ASSIGN_OP; - COPY_NODE(opline->result, next_opline->result); - MAKE_NOP(next_opline); - opline++; - } - } - break; - case ZEND_IS_EQUAL: case ZEND_IS_NOT_EQUAL: case ZEND_IS_SMALLER:

« previous php.cvs (#108703) next »