Skip to content

Commit 0826a54

Browse files
committed
Fix DCE of FREE of COALESCE
When encountering the following SSA graph: BB1: #2.T1 [string] = COALESCE #1.CV0($str) [null, string] BB2 BB2: #5.T1 [string] = QM_ASSIGN string("") BB3: #7.X1 [string] = Phi(#2.X1 [string], #5.X1 [string]) FREE #7.T1 [string] We would currently determine that #7, #5 are dead, and eliminate the FREE and QM_ASSIGN. However, we cannot eliminate #2, as COALESCE is also responsible for control flow. Fix this my marking all non-CV phis as live to start with. This can be relaxed to check the kind of the source instruction, but I couldn't immediately come up with a case where it would be useful.
1 parent 25f5a1b commit 0826a54

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

ext/opcache/Optimizer/dce.c

+10
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,16 @@ int dce_optimize_op_array(zend_op_array *op_array, zend_ssa *ssa, zend_bool reor
503503
ctx.phi_dead = alloca(sizeof(zend_ulong) * ctx.phi_worklist_len);
504504
memset(ctx.phi_dead, 0xff, sizeof(zend_ulong) * ctx.phi_worklist_len);
505505

506+
/* Mark non-CV phis as live. Even if the result is unused, we generally cannot remove one
507+
* of the producing instructions, as it combines producing the result with control flow.
508+
* This can be made more precise if there are any cases where this is not the case. */
509+
FOREACH_PHI(phi) {
510+
if (phi->var >= op_array->last_var) {
511+
zend_bitset_excl(ctx.phi_dead, phi->ssa_var);
512+
add_phi_sources_to_worklists(&ctx, phi, 0);
513+
}
514+
} FOREACH_PHI_END();
515+
506516
/* Mark reacable instruction without side effects as dead */
507517
int b = ssa->cfg.blocks_count;
508518
while (b > 0) {

ext/opcache/tests/opt/dce_009.phpt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Incorrect DCE of FREE of COALESCE
3+
--FILE--
4+
<?php
5+
6+
function test(?string $str) {
7+
$str ?? $str = '';
8+
return strlen($str);
9+
}
10+
11+
$foo = 'foo';
12+
$foo .= 'bar';
13+
var_dump(test($foo));
14+
15+
?>
16+
--EXPECT--
17+
int(6)

0 commit comments

Comments
 (0)