Skip to content

Commit cb97922

Browse files
committed
Convert ZEND_ECHO operand to string after sccp
And filter out echoes of the empty string (e.g. false/null) Split out of php#5097
1 parent d6a6a60 commit cb97922

12 files changed

+67
-15
lines changed

ext/opcache/Optimizer/zend_optimizer.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,22 @@ int zend_optimizer_update_op1_const(zend_op_array *op_array,
340340
case ZEND_FETCH_LIST_R:
341341
case ZEND_COPY_TMP:
342342
return 0;
343+
case ZEND_ECHO:
344+
{
345+
zval zv;
346+
if (Z_TYPE_P(val) != IS_STRING && zend_optimizer_eval_cast(&zv, IS_STRING, val) == SUCCESS) {
347+
zval_ptr_dtor_nogc(val);
348+
val = &zv;
349+
}
350+
opline->op1.constant = zend_optimizer_add_literal(op_array, val);
351+
if (Z_TYPE_P(val) == IS_STRING && Z_STRLEN_P(val) == 0) {
352+
MAKE_NOP(opline);
353+
}
354+
/* TODO: In a subsequent pass, *after* this step and compacting nops, combine consecutive ZEND_ECHOs using the block information from ssa->cfg */
355+
/* (e.g. for ext/opcache/tests/opt/sccp_010.phpt) */
356+
/* https://github.com/php/php-src/pull/5097#issuecomment-577306560 */
357+
break;
358+
}
343359
case ZEND_CONCAT:
344360
case ZEND_FAST_CONCAT:
345361
case ZEND_FETCH_R:

ext/opcache/tests/opt/sccp_002.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ foo: ; (lines=4, args=1, vars=1, tmps=0)
3232
; (after optimizer)
3333
; %ssccp_002.php:2-12
3434
L0 (2): CV0($x) = RECV 1
35-
L1 (9): ECHO int(1)
36-
L2 (11): ECHO int(1)
35+
L1 (9): ECHO string("1")
36+
L2 (11): ECHO string("1")
3737
L3 (12): RETURN null

ext/opcache/tests/opt/sccp_003.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ L0 (14): RETURN int(1)
3131
foo: ; (lines=3, args=0, vars=0, tmps=0)
3232
; (after optimizer)
3333
; %ssccp_003.php:2-12
34-
L0 (9): ECHO int(1)
35-
L1 (11): ECHO int(1)
34+
L0 (9): ECHO string("1")
35+
L1 (11): ECHO string("1")
3636
L2 (12): RETURN null

ext/opcache/tests/opt/sccp_004.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ foo: ; (lines=4, args=1, vars=1, tmps=0)
3535
; (after optimizer)
3636
; %ssccp_004.php:2-15
3737
L0 (2): CV0($x) = RECV 1
38-
L1 (11): ECHO bool(true)
39-
L2 (14): ECHO int(1)
38+
L1 (11): ECHO string("1")
39+
L2 (14): ECHO string("1")
4040
L3 (15): RETURN null

ext/opcache/tests/opt/sccp_005.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ foo: ; (lines=3, args=1, vars=1, tmps=0)
2525
; (after optimizer)
2626
; %ssccp_005.php:2-5
2727
L0 (2): CV0($x) = RECV 1
28-
L1 (4): ECHO int(2)
28+
L1 (4): ECHO string("2")
2929
L2 (5): RETURN null

ext/opcache/tests/opt/sccp_007.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ foo: ; (lines=3, args=1, vars=1, tmps=0)
2929
; (after optimizer)
3030
; %ssccp_007.php:2-9
3131
L0 (2): CV0($x) = RECV 1
32-
L1 (8): ECHO int(0)
32+
L1 (8): ECHO string("0")
3333
L2 (9): RETURN null

ext/opcache/tests/opt/sccp_009.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ foo: ; (lines=3, args=1, vars=1, tmps=0)
2626
; (after optimizer)
2727
; %ssccp_009.php:2-6
2828
L0 (2): CV0($x) = RECV 1
29-
L1 (5): ECHO int(2)
29+
L1 (5): ECHO string("2")
3030
L2 (6): RETURN null

ext/opcache/tests/opt/sccp_010.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ L0 (15): RETURN int(1)
3232
foo: ; (lines=3, args=0, vars=0, tmps=0)
3333
; (after optimizer)
3434
; %ssccp_010.php:2-13
35-
L0 (10): ECHO int(1)
36-
L1 (12): ECHO int(1)
35+
L0 (10): ECHO string("1")
36+
L1 (12): ECHO string("1")
3737
L2 (13): RETURN null

ext/opcache/tests/opt/sccp_011.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ foo: ; (lines=3, args=1, vars=1, tmps=0)
3232
; (after optimizer)
3333
; %ssccp_011.php:2-12
3434
L0 (2): CV0($x) = RECV 1
35-
L1 (11): ECHO int(0)
35+
L1 (11): ECHO string("0")
3636
L2 (12): RETURN null

ext/opcache/tests/opt/sccp_012.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ L0 (17): RETURN int(1)
3434
foo: ; (lines=3, args=0, vars=0, tmps=0)
3535
; (after optimizer)
3636
; %ssccp_012.php:2-15
37-
L0 (10): ECHO int(1)
38-
L1 (14): ECHO int(4)
37+
L0 (10): ECHO string("1")
38+
L1 (14): ECHO string("4")
3939
L2 (15): RETURN null

ext/opcache/tests/opt/sccp_022.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ L1 (3): ASSIGN_DIM CV1($a) int(0)
3232
L2 (3): OP_DATA CV0($x)
3333
L3 (4): ASSIGN_DIM CV1($a) int(1)
3434
L4 (4): OP_DATA int(5)
35-
L5 (5): ECHO int(5)
35+
L5 (5): ECHO string("5")
3636
L6 (6): ASSIGN_OBJ CV1($a) string("foo")
3737
L7 (6): OP_DATA int(5)
3838
L8 (7): T2 = FETCH_DIM_R CV1($a) int(1)

ext/opcache/tests/opt/sccp_031.phpt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
SCCP 031: Echo optimizations
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.optimization_level=-1
7+
opcache.opt_debug_level=0x20000
8+
opcache.preload=
9+
--SKIPIF--
10+
<?php require_once('skipif.inc'); ?>
11+
--FILE--
12+
<?php
13+
function foo() {
14+
$k = 0;
15+
$a = [null];
16+
echo isset($a[$k]);
17+
echo "b";
18+
echo isset($a[$k+1]);
19+
echo "c";
20+
echo $a[$k];
21+
echo $a; // Should not be optimized
22+
}
23+
?>
24+
--EXPECTF--
25+
$_main: ; (lines=1, args=0, vars=0, tmps=0)
26+
; (after optimizer)
27+
; %ssccp_031.php:1-13
28+
L0 (13): RETURN int(1)
29+
30+
foo: ; (lines=4, args=0, vars=0, tmps=0)
31+
; (after optimizer)
32+
; %s_031.php:2-11
33+
L0 (6): ECHO string("b")
34+
L1 (8): ECHO string("c")
35+
L2 (10): ECHO array(...)
36+
L3 (11): RETURN null

0 commit comments

Comments
 (0)