Skip to content

Optimize $a = !is_string($x) in opcache #4906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Zend/Optimizer/block_pass.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,19 @@ static void zend_optimize_block(zend_basic_block *block, zend_op_array *op_array
MAKE_NOP(opline);
++(*opt_count);
break;
case ZEND_TYPE_CHECK:
if (opline->opcode == ZEND_BOOL_NOT) {
if (src->extended_value == MAY_BE_RESOURCE || src->extended_value == (MAY_BE_ANY - MAY_BE_RESOURCE)) {
/* is_resource() is a special case - it returns false if the resource is closed. Don't convert to/from it. */
break;
}
src->extended_value = MAY_BE_ANY - src->extended_value;
}
COPY_NODE(src->result, opline->result);
SET_VAR_SOURCE(src);
MAKE_NOP(opline);
++(*opt_count);
break;
case ZEND_IS_SMALLER:
if (opline->opcode == ZEND_BOOL_NOT) {
zend_uchar tmp_type;
Expand Down Expand Up @@ -545,7 +558,6 @@ static void zend_optimize_block(zend_basic_block *block, zend_op_array *op_array
case ZEND_ISSET_ISEMPTY_PROP_OBJ:
case ZEND_ISSET_ISEMPTY_STATIC_PROP:
case ZEND_INSTANCEOF:
case ZEND_TYPE_CHECK:
case ZEND_DEFINED:
case ZEND_IN_ARRAY:
case ZEND_ARRAY_KEY_EXISTS:
Expand Down
2 changes: 1 addition & 1 deletion Zend/Optimizer/sccp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@ static void sccp_visit_instr(scdf_ctx *scdf, zend_op *opline, zend_ssa_op *ssa_o
SET_RESULT(result, &zv);
return;
} else if (!(type & ((MAY_BE_ANY|MAY_BE_UNDEF) - expected_type_mask))
&& !(expected_type_mask & MAY_BE_RESOURCE)) {
&& !(type & expected_type_mask & MAY_BE_RESOURCE)) {
Copy link
Member

@iluuu1994 iluuu1994 Dec 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think expected_type_mask could be removed, as type will only contain a subset of bits of expected_type_mask.

ZVAL_TRUE(&zv);
SET_RESULT(result, &zv);
return;
Expand Down
4 changes: 2 additions & 2 deletions ext/opcache/tests/opt/sccp_026.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ test:
; (after optimizer)
; %s:2-8
0000 CV0($var) = RECV 1
0001 T2 = TYPE_CHECK (string) CV0($var)
0002 JMPZ T2 0004
0001 T2 = TYPE_CHECK TYPE [null, bool, long, double, array, object, resource] CV0($var)
0002 JMPNZ T2 0004
0003 JMP 0005
0004 RETURN null
0005 INIT_FCALL 1 %d string("var_dump")
Expand Down