Re: com php-src: Expand optimizations regarding short-circuting a bit: Zend/tests/assert/expect_015.phpt Zend/zend_compile.c
| From: | Dmitry Stogov | Date: | Mon, 15 Jun 2015 09:39:04 +0000 |
| Subject: | Re: com php-src: Expand optimizations regarding short-circuting a bit: Zend/tests/assert/expect_015.phpt Zend/zend_compile.c | ||
| References: | 1 | Groups: | php.cvs |
| Request: | Send a blank email to php-cvs+get-87270@lists.php.net to get a copy of this message | ||
Hi Bob,
Note, that this kind of optimization actually turns "if(0 || A) into
something similar to #ifdef.
With your patch "A" is not compiled at all and even is not checked for
possible compilation errors.
I'm not sure about all possible consequences. Actually, this is not a
self-containing change...
I don't say it's bad. May be with some future development, this would allow
some new use cases...
if (PHP_VERSION[0] == "7") {
// PHP 7 code
} else {
// something else
}
But, please, discuss your ideas before committing them (especially when
PHP-7 is in the feature-freeze state).
For real-life this optimization makes nothing, because opcache removes
unreachable code anyway.
Thanks. Dmitry.
On Sun, Jun 14, 2015 at 7:01 AM, Bob Weinand <bwoebi@php.net> wrote:
> Commit: 3cfa58367b1b85d346d9be6cf9ae116c63571247
> Author: Bob Weinand <bobwei9@hotmail.com> Sun, 14 Jun 2015
> 06:01:01 +0200
> Parents: b97944c6d1469ba9979843e27050068aa914eb39
> Branches: master
>
> Link:
>
> http://git.php.net/?p=php-src.git;a=commitdiff;h=3cfa58367b1b85d346d9be6cf9ae116c63571247
>
> Log:
> Expand optimizations regarding short-circuting a bit
>
> Changed paths:
> M Zend/tests/assert/expect_015.phpt
> M Zend/zend_compile.c
>
>
> Diff:
> diff --git a/Zend/tests/assert/expect_015.phpt
> b/Zend/tests/assert/expect_015.phpt
> index 80f1bd8..030913f 100644
> --- a/Zend/tests/assert/expect_015.phpt
> +++ b/Zend/tests/assert/expect_015.phpt
> @@ -144,12 +144,6 @@ assert(0 && ($a = function () {
>
> ?>
> --EXPECTF--
> -Warning: Unsupported declare 'A' in %sexpect_015.php on line %d
> -
> -Warning: Unsupported declare 'B' in %sexpect_015.php on line %d
> -
> -Warning: Unsupported declare 'C' in %sexpect_015.php on line %d
> -
> Warning: assert(): assert(0 && ($a = function () {
> global $a;
> global $$b;
> diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
> index f753af4..61271f6 100644
> --- a/Zend/zend_compile.c
> +++ b/Zend/zend_compile.c
> @@ -5840,31 +5840,65 @@ void zend_compile_short_circuiting(znode *result,
> zend_ast *ast) /* {{{ */
>
> zend_compile_expr(&left_node, left_ast);
>
> + if (left_node.op_type == IS_CONST) {
> + if (
> + (ast->kind == ZEND_AST_AND &&
> !zend_is_true(&left_node.u.constant))
> + || (ast->kind == ZEND_AST_OR &&
> zend_is_true(&left_node.u.constant))
> + ) {
> + result->op_type = IS_CONST;
> + ZVAL_BOOL(&result->u.constant,
> zend_is_true(&left_node.u.constant));
> + } else {
> + zend_compile_expr(&right_node, right_ast);
> +
> + if (right_node.op_type == IS_CONST) {
> + result->op_type = IS_CONST;
> + ZVAL_BOOL(&result->u.constant,
> zend_is_true(&right_node.u.constant));
> + } else {
> + zend_emit_op(result, ZEND_BOOL,
> &right_node, NULL);
> + }
> +
> + zval_ptr_dtor(&right_node.u.constant);
> + }
> +
> + zval_ptr_dtor(&left_node.u.constant);
> + return;
> + }
> +
> opnum_jmpz = get_next_op_number(CG(active_op_array));
> opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ?
> ZEND_JMPZ_EX : ZEND_JMPNZ_EX,
> &left_node, NULL);
>
> if (left_node.op_type == IS_TMP_VAR) {
> SET_NODE(opline_jmpz->result, &left_node);
> - } else {
> - opline_jmpz->result.var =
> get_temporary_variable(CG(active_op_array));
> - opline_jmpz->result_type = IS_TMP_VAR;
> }
> - GET_NODE(result, opline_jmpz->result);
>
> zend_compile_expr(&right_node, right_ast);
>
> - if (right_node.op_type == IS_CONST && (
> - (ast->kind == ZEND_AST_AND &&
> !zend_is_true(&right_node.u.constant))
> - || (ast->kind == ZEND_AST_OR &&
> zend_is_true(&right_node.u.constant))
> - )) {
> - result->op_type = IS_CONST;
> - ZVAL_BOOL(&result->u.constant,
> zend_is_true(&right_node.u.constant));
> + if (right_node.op_type == IS_CONST && opnum_jmpz ==
> CG(active_op_array)->last) {
> + if (
> + (ast->kind == ZEND_AST_AND &&
> !zend_is_true(&right_node.u.constant))
> + || (ast->kind == ZEND_AST_OR &&
> zend_is_true(&right_node.u.constant))
> + ) {
> + CG(active_op_array)->last--;
> + result->op_type = IS_CONST;
> + ZVAL_BOOL(&result->u.constant,
> zend_is_true(&right_node.u.constant));
> + } else {
> + opline_jmpz->opcode = ZEND_BOOL;
> + zend_make_var_result(result, opline_jmpz);
> + }
> +
> zval_ptr_dtor(&right_node.u.constant);
> - } else {
> - opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node,
> NULL);
> - SET_NODE(opline_bool->result, result);
> + return;
> + }
> +
> + if (left_node.op_type != IS_TMP_VAR) {
> + opline_jmpz->result.var =
> get_temporary_variable(CG(active_op_array));
> + opline_jmpz->result_type = IS_TMP_VAR;
> }
> + GET_NODE(result, opline_jmpz->result);
> +
> + opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL);
> + SET_NODE(opline_bool->result, result);
>
> zend_update_jump_target_to_next(opnum_jmpz);
> }
>
>
> --
> PHP CVS Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>