1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine, DCE - Dead Code Elimination |
4 +----------------------------------------------------------------------+
5 | Copyright (c) The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Nikita Popov <nikic@php.net> |
16 | Dmitry Stogov <dmitry@php.net> |
17 +----------------------------------------------------------------------+
18 */
19
20 #include "ZendAccelerator.h"
21 #include "Optimizer/zend_optimizer_internal.h"
22 #include "Optimizer/zend_inference.h"
23 #include "Optimizer/zend_ssa.h"
24 #include "Optimizer/zend_func_info.h"
25 #include "Optimizer/zend_call_graph.h"
26 #include "zend_bitset.h"
27
28 /* This pass implements a form of dead code elimination (DCE). The algorithm optimistically assumes
29 * that all instructions and phis are dead. Instructions with immediate side-effects are then marked
30 * as live. We then recursively (using a worklist) propagate liveness to the instructions that def
31 * the used operands.
32 *
33 * Notes:
34 * * This pass does not perform unreachable code elimination. This happens as part of the SCCP
35 * pass.
36 * * The DCE is performed without taking control-dependence into account, i.e. all conditional
37 * branches are assumed to be live. It's possible to take control-dependence into account using
38 * the DCE algorithm described by Cytron et al., however it requires the construction of a
39 * postdominator tree and of postdominance frontiers, which does not seem worthwhile at this
40 * point.
41 * * We separate intrinsic side-effects from potential side-effects in the form of notices thrown
42 * by the instruction (in case we want to make this configurable). See may_have_side_effects() and
43 * zend_may_throw().
44 * * We often cannot DCE assignments and unsets while guaranteeing that dtors run in the same
45 * order. There is an optimization option to allow reordering of dtor effects.
46 * * The algorithm is able to eliminate dead modifications of non-escaping arrays
47 * and objects as well as dead arrays and objects allocations.
48 */
49
50 typedef struct {
51 zend_ssa *ssa;
52 zend_op_array *op_array;
53 zend_bitset instr_dead;
54 zend_bitset phi_dead;
55 zend_bitset instr_worklist;
56 zend_bitset phi_worklist;
57 zend_bitset phi_worklist_no_val;
58 uint32_t instr_worklist_len;
59 uint32_t phi_worklist_len;
60 unsigned reorder_dtor_effects : 1;
61 } context;
62
is_bad_mod(const zend_ssa * ssa,int use,int def)63 static inline zend_bool is_bad_mod(const zend_ssa *ssa, int use, int def) {
64 if (def < 0) {
65 /* This modification is not tracked by SSA, assume the worst */
66 return 1;
67 }
68 if (ssa->var_info[use].type & MAY_BE_REF) {
69 /* Modification of reference may have side-effect */
70 return 1;
71 }
72 return 0;
73 }
74
may_have_side_effects(zend_op_array * op_array,zend_ssa * ssa,const zend_op * opline,const zend_ssa_op * ssa_op,zend_bool reorder_dtor_effects)75 static inline zend_bool may_have_side_effects(
76 zend_op_array *op_array, zend_ssa *ssa,
77 const zend_op *opline, const zend_ssa_op *ssa_op,
78 zend_bool reorder_dtor_effects) {
79 switch (opline->opcode) {
80 case ZEND_NOP:
81 case ZEND_IS_IDENTICAL:
82 case ZEND_IS_NOT_IDENTICAL:
83 case ZEND_QM_ASSIGN:
84 case ZEND_FREE:
85 case ZEND_TYPE_CHECK:
86 case ZEND_DEFINED:
87 case ZEND_ADD:
88 case ZEND_SUB:
89 case ZEND_MUL:
90 case ZEND_POW:
91 case ZEND_BW_OR:
92 case ZEND_BW_AND:
93 case ZEND_BW_XOR:
94 case ZEND_CONCAT:
95 case ZEND_FAST_CONCAT:
96 case ZEND_DIV:
97 case ZEND_MOD:
98 case ZEND_BOOL_XOR:
99 case ZEND_BOOL:
100 case ZEND_BOOL_NOT:
101 case ZEND_BW_NOT:
102 case ZEND_SL:
103 case ZEND_SR:
104 case ZEND_IS_EQUAL:
105 case ZEND_IS_NOT_EQUAL:
106 case ZEND_IS_SMALLER:
107 case ZEND_IS_SMALLER_OR_EQUAL:
108 case ZEND_CASE:
109 case ZEND_CASE_STRICT:
110 case ZEND_CAST:
111 case ZEND_ROPE_INIT:
112 case ZEND_ROPE_ADD:
113 case ZEND_INIT_ARRAY:
114 case ZEND_SPACESHIP:
115 case ZEND_STRLEN:
116 case ZEND_COUNT:
117 case ZEND_GET_TYPE:
118 case ZEND_ISSET_ISEMPTY_THIS:
119 case ZEND_ISSET_ISEMPTY_DIM_OBJ:
120 case ZEND_FETCH_DIM_IS:
121 case ZEND_ISSET_ISEMPTY_CV:
122 case ZEND_ISSET_ISEMPTY_VAR:
123 case ZEND_FETCH_IS:
124 case ZEND_IN_ARRAY:
125 case ZEND_FUNC_NUM_ARGS:
126 case ZEND_FUNC_GET_ARGS:
127 case ZEND_ARRAY_KEY_EXISTS:
128 /* No side effects */
129 return 0;
130 case ZEND_ADD_ARRAY_ELEMENT:
131 /* TODO: We can't free two vars. Keep instruction alive. <?php [0, "$a" => "$b"]; */
132 if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && (opline->op2_type & (IS_VAR|IS_TMP_VAR))) {
133 return 1;
134 }
135 return 0;
136 case ZEND_ROPE_END:
137 /* TODO: Rope dce optimization, see #76446 */
138 return 1;
139 case ZEND_JMP:
140 case ZEND_JMPZ:
141 case ZEND_JMPNZ:
142 case ZEND_JMPZNZ:
143 case ZEND_JMPZ_EX:
144 case ZEND_JMPNZ_EX:
145 case ZEND_JMP_SET:
146 case ZEND_COALESCE:
147 case ZEND_ASSERT_CHECK:
148 case ZEND_JMP_NULL:
149 /* For our purposes a jumps and branches are side effects. */
150 return 1;
151 case ZEND_BEGIN_SILENCE:
152 case ZEND_END_SILENCE:
153 case ZEND_ECHO:
154 case ZEND_INCLUDE_OR_EVAL:
155 case ZEND_THROW:
156 case ZEND_MATCH_ERROR:
157 case ZEND_EXT_STMT:
158 case ZEND_EXT_FCALL_BEGIN:
159 case ZEND_EXT_FCALL_END:
160 case ZEND_TICKS:
161 case ZEND_YIELD:
162 case ZEND_YIELD_FROM:
163 /* Intrinsic side effects */
164 return 1;
165 case ZEND_DO_FCALL:
166 case ZEND_DO_FCALL_BY_NAME:
167 case ZEND_DO_ICALL:
168 case ZEND_DO_UCALL:
169 /* For now assume all calls have side effects */
170 return 1;
171 case ZEND_RECV:
172 case ZEND_RECV_INIT:
173 /* Even though RECV_INIT can be side-effect free, these cannot be simply dropped
174 * due to the prologue skipping code. */
175 return 1;
176 case ZEND_ASSIGN_REF:
177 return 1;
178 case ZEND_ASSIGN:
179 {
180 if (is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def)) {
181 return 1;
182 }
183 if (!reorder_dtor_effects) {
184 if (opline->op2_type != IS_CONST
185 && (OP2_INFO() & MAY_HAVE_DTOR)
186 && ssa->vars[ssa_op->op2_use].escape_state != ESCAPE_STATE_NO_ESCAPE) {
187 /* DCE might shorten lifetime */
188 return 1;
189 }
190 }
191 return 0;
192 }
193 case ZEND_UNSET_VAR:
194 return 1;
195 case ZEND_UNSET_CV:
196 {
197 uint32_t t1 = OP1_INFO();
198 if (t1 & MAY_BE_REF) {
199 /* We don't consider uses as the LHS of an assignment as real uses during DCE, so
200 * an unset may be considered dead even if there is a later assignment to the
201 * variable. Removing the unset in this case would not be correct if the variable
202 * is a reference, because unset breaks references. */
203 return 1;
204 }
205 return 0;
206 }
207 case ZEND_PRE_INC:
208 case ZEND_POST_INC:
209 case ZEND_PRE_DEC:
210 case ZEND_POST_DEC:
211 return is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def);
212 case ZEND_ASSIGN_OP:
213 return is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def)
214 || ssa->vars[ssa_op->op1_def].escape_state != ESCAPE_STATE_NO_ESCAPE;
215 case ZEND_ASSIGN_DIM:
216 case ZEND_ASSIGN_OBJ:
217 if (is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def)
218 || ssa->vars[ssa_op->op1_def].escape_state != ESCAPE_STATE_NO_ESCAPE) {
219 return 1;
220 }
221 if (!reorder_dtor_effects) {
222 opline++;
223 ssa_op++;
224 if (opline->op1_type != IS_CONST
225 && (OP1_INFO() & MAY_HAVE_DTOR)) {
226 /* DCE might shorten lifetime */
227 return 1;
228 }
229 }
230 return 0;
231 case ZEND_PRE_INC_OBJ:
232 case ZEND_PRE_DEC_OBJ:
233 case ZEND_POST_INC_OBJ:
234 case ZEND_POST_DEC_OBJ:
235 if (is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def)
236 || ssa->vars[ssa_op->op1_def].escape_state != ESCAPE_STATE_NO_ESCAPE) {
237 return 1;
238 }
239 return 0;
240 case ZEND_BIND_STATIC:
241 if (op_array->static_variables
242 && (opline->extended_value & ZEND_BIND_REF) != 0) {
243 zval *value =
244 (zval*)((char*)op_array->static_variables->arData +
245 (opline->extended_value & ~ZEND_BIND_REF));
246 if (Z_TYPE_P(value) == IS_CONSTANT_AST) {
247 /* AST may contain undefined constants */
248 return 1;
249 }
250 }
251 return 0;
252 case ZEND_CHECK_VAR:
253 return (OP1_INFO() & MAY_BE_UNDEF) != 0;
254 default:
255 /* For everything we didn't handle, assume a side-effect */
256 return 1;
257 }
258 }
259
add_to_worklists(context * ctx,int var_num,int check)260 static zend_always_inline void add_to_worklists(context *ctx, int var_num, int check) {
261 zend_ssa_var *var = &ctx->ssa->vars[var_num];
262 if (var->definition >= 0) {
263 if (!check || zend_bitset_in(ctx->instr_dead, var->definition)) {
264 zend_bitset_incl(ctx->instr_worklist, var->definition);
265 }
266 } else if (var->definition_phi) {
267 if (!check || zend_bitset_in(ctx->phi_dead, var_num)) {
268 zend_bitset_incl(ctx->phi_worklist, var_num);
269 }
270 }
271 }
272
add_to_phi_worklist_no_val(context * ctx,int var_num)273 static inline void add_to_phi_worklist_no_val(context *ctx, int var_num) {
274 zend_ssa_var *var = &ctx->ssa->vars[var_num];
275 if (var->definition_phi && zend_bitset_in(ctx->phi_dead, var_num)) {
276 zend_bitset_incl(ctx->phi_worklist_no_val, var_num);
277 }
278 }
279
add_operands_to_worklists(context * ctx,zend_op * opline,zend_ssa_op * ssa_op,zend_ssa * ssa,int check)280 static zend_always_inline void add_operands_to_worklists(context *ctx, zend_op *opline, zend_ssa_op *ssa_op, zend_ssa *ssa, int check) {
281 if (ssa_op->result_use >= 0) {
282 add_to_worklists(ctx, ssa_op->result_use, check);
283 }
284 if (ssa_op->op1_use >= 0) {
285 if (!zend_ssa_is_no_val_use(opline, ssa_op, ssa_op->op1_use)
286 || (opline->opcode == ZEND_ASSIGN
287 && (ssa->var_info[ssa_op->op1_use].type & MAY_BE_REF) != 0)) {
288 add_to_worklists(ctx, ssa_op->op1_use, check);
289 } else {
290 add_to_phi_worklist_no_val(ctx, ssa_op->op1_use);
291 }
292 }
293 if (ssa_op->op2_use >= 0) {
294 if (!zend_ssa_is_no_val_use(opline, ssa_op, ssa_op->op2_use)
295 || (opline->opcode == ZEND_FE_FETCH_R
296 && (ssa->var_info[ssa_op->op2_use].type & MAY_BE_REF) != 0)) {
297 add_to_worklists(ctx, ssa_op->op2_use, check);
298 } else {
299 add_to_phi_worklist_no_val(ctx, ssa_op->op2_use);
300 }
301 }
302 }
303
add_phi_sources_to_worklists(context * ctx,zend_ssa_phi * phi,int check)304 static zend_always_inline void add_phi_sources_to_worklists(context *ctx, zend_ssa_phi *phi, int check) {
305 zend_ssa *ssa = ctx->ssa;
306 int source;
307 FOREACH_PHI_SOURCE(phi, source) {
308 add_to_worklists(ctx, source, check);
309 } FOREACH_PHI_SOURCE_END();
310 }
311
is_var_dead(context * ctx,int var_num)312 static inline zend_bool is_var_dead(context *ctx, int var_num) {
313 zend_ssa_var *var = &ctx->ssa->vars[var_num];
314 if (var->definition_phi) {
315 return zend_bitset_in(ctx->phi_dead, var_num);
316 } else if (var->definition >= 0) {
317 return zend_bitset_in(ctx->instr_dead, var->definition);
318 } else {
319 /* Variable has no definition, so either the definition has already been removed (var is
320 * dead) or this is one of the implicit variables at the start of the function (for our
321 * purposes live) */
322 return var_num >= ctx->op_array->last_var;
323 }
324 }
325
326 // Sometimes we can mark the var as EXT_UNUSED
try_remove_var_def(context * ctx,int free_var,int use_chain,zend_op * opline)327 static zend_bool try_remove_var_def(context *ctx, int free_var, int use_chain, zend_op *opline) {
328 if (use_chain >= 0) {
329 return 0;
330 }
331 zend_ssa_var *var = &ctx->ssa->vars[free_var];
332 int def = var->definition;
333
334 if (def >= 0) {
335 zend_ssa_op *def_op = &ctx->ssa->ops[def];
336
337 if (def_op->result_def == free_var
338 && var->phi_use_chain == NULL
339 && var->use_chain == (opline - ctx->op_array->opcodes)) {
340 zend_op *def_opline = &ctx->op_array->opcodes[def];
341
342 switch (def_opline->opcode) {
343 case ZEND_ASSIGN:
344 case ZEND_ASSIGN_REF:
345 case ZEND_ASSIGN_DIM:
346 case ZEND_ASSIGN_OBJ:
347 case ZEND_ASSIGN_OBJ_REF:
348 case ZEND_ASSIGN_STATIC_PROP:
349 case ZEND_ASSIGN_STATIC_PROP_REF:
350 case ZEND_ASSIGN_OP:
351 case ZEND_ASSIGN_DIM_OP:
352 case ZEND_ASSIGN_OBJ_OP:
353 case ZEND_ASSIGN_STATIC_PROP_OP:
354 case ZEND_PRE_INC:
355 case ZEND_PRE_DEC:
356 case ZEND_PRE_INC_OBJ:
357 case ZEND_PRE_DEC_OBJ:
358 case ZEND_DO_ICALL:
359 case ZEND_DO_UCALL:
360 case ZEND_DO_FCALL_BY_NAME:
361 case ZEND_DO_FCALL:
362 case ZEND_INCLUDE_OR_EVAL:
363 case ZEND_YIELD:
364 case ZEND_YIELD_FROM:
365 case ZEND_ASSERT_CHECK:
366 def_opline->result_type = IS_UNUSED;
367 def_opline->result.var = 0;
368 def_op->result_def = -1;
369 var->definition = -1;
370 return 1;
371 default:
372 break;
373 }
374 }
375 }
376 return 0;
377 }
378
may_be_refcounted(uint32_t type)379 static zend_always_inline zend_bool may_be_refcounted(uint32_t type) {
380 return (type & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) != 0;
381 }
382
383 /* Returns whether the instruction has been DCEd */
dce_instr(context * ctx,zend_op * opline,zend_ssa_op * ssa_op)384 static zend_bool dce_instr(context *ctx, zend_op *opline, zend_ssa_op *ssa_op) {
385 zend_ssa *ssa = ctx->ssa;
386 int free_var = -1;
387 zend_uchar free_var_type;
388
389 if (opline->opcode == ZEND_NOP) {
390 return 0;
391 }
392
393 /* We mark FREEs as dead, but they're only really dead if the destroyed var is dead */
394 if (opline->opcode == ZEND_FREE
395 && ((ssa->var_info[ssa_op->op1_use].type & (MAY_BE_REF|MAY_BE_ANY|MAY_BE_UNDEF)) == 0
396 || may_be_refcounted(ssa->var_info[ssa_op->op1_use].type))
397 && !is_var_dead(ctx, ssa_op->op1_use)) {
398 return 0;
399 }
400
401 if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))&& !is_var_dead(ctx, ssa_op->op1_use)) {
402 if (!try_remove_var_def(ctx, ssa_op->op1_use, ssa_op->op1_use_chain, opline)) {
403 if (may_be_refcounted(ssa->var_info[ssa_op->op1_use].type)
404 && opline->opcode != ZEND_CASE && opline->opcode != ZEND_CASE_STRICT) {
405 free_var = ssa_op->op1_use;
406 free_var_type = opline->op1_type;
407 }
408 }
409 }
410 if ((opline->op2_type & (IS_VAR|IS_TMP_VAR)) && !is_var_dead(ctx, ssa_op->op2_use)) {
411 if (!try_remove_var_def(ctx, ssa_op->op2_use, ssa_op->op2_use_chain, opline)) {
412 if (may_be_refcounted(ssa->var_info[ssa_op->op2_use].type)) {
413 if (free_var >= 0) {
414 // TODO: We can't free two vars. Keep instruction alive.
415 zend_bitset_excl(ctx->instr_dead, opline - ctx->op_array->opcodes);
416 return 0;
417 }
418 free_var = ssa_op->op2_use;
419 free_var_type = opline->op2_type;
420 }
421 }
422 }
423
424 zend_ssa_rename_defs_of_instr(ctx->ssa, ssa_op);
425 zend_ssa_remove_instr(ctx->ssa, opline, ssa_op);
426
427 if (free_var >= 0) {
428 opline->opcode = ZEND_FREE;
429 opline->op1.var = EX_NUM_TO_VAR(ssa->vars[free_var].var);
430 opline->op1_type = free_var_type;
431
432 ssa_op->op1_use = free_var;
433 ssa_op->op1_use_chain = ssa->vars[free_var].use_chain;
434 ssa->vars[free_var].use_chain = ssa_op - ssa->ops;
435 return 0;
436 }
437 return 1;
438 }
439
get_common_phi_source(zend_ssa * ssa,zend_ssa_phi * phi)440 static inline int get_common_phi_source(zend_ssa *ssa, zend_ssa_phi *phi) {
441 int common_source = -1;
442 int source;
443 FOREACH_PHI_SOURCE(phi, source) {
444 if (source == phi->ssa_var) {
445 continue;
446 }
447 if (common_source == -1) {
448 common_source = source;
449 } else if (common_source != source) {
450 return -1;
451 }
452 } FOREACH_PHI_SOURCE_END();
453
454 /* If all sources are phi->ssa_var this phi must be in an unreachable cycle.
455 * We can't easily drop the phi in that case, as we don't have something to replace it with.
456 * Ideally SCCP would eliminate the whole cycle. */
457 return common_source;
458 }
459
try_remove_trivial_phi(context * ctx,zend_ssa_phi * phi)460 static void try_remove_trivial_phi(context *ctx, zend_ssa_phi *phi) {
461 zend_ssa *ssa = ctx->ssa;
462 if (phi->pi < 0) {
463 /* Phi assignment with identical source operands */
464 int common_source = get_common_phi_source(ssa, phi);
465 if (common_source >= 0) {
466 zend_ssa_rename_var_uses(ssa, phi->ssa_var, common_source, 1);
467 zend_ssa_remove_phi(ssa, phi);
468 }
469 } else {
470 /* Pi assignment that is only used in Phi/Pi assignments */
471 // TODO What if we want to rerun type inference after DCE? Maybe separate this?
472 /*ZEND_ASSERT(phi->sources[0] != -1);
473 if (ssa->vars[phi->ssa_var].use_chain < 0) {
474 zend_ssa_rename_var_uses_keep_types(ssa, phi->ssa_var, phi->sources[0], 1);
475 zend_ssa_remove_phi(ssa, phi);
476 }*/
477 }
478 }
479
may_break_varargs(const zend_op_array * op_array,const zend_ssa * ssa,const zend_ssa_op * ssa_op)480 static inline zend_bool may_break_varargs(const zend_op_array *op_array, const zend_ssa *ssa, const zend_ssa_op *ssa_op) {
481 if (ssa_op->op1_def >= 0
482 && ssa->vars[ssa_op->op1_def].var < op_array->num_args) {
483 return 1;
484 }
485 if (ssa_op->op2_def >= 0
486 && ssa->vars[ssa_op->op2_def].var < op_array->num_args) {
487 return 1;
488 }
489 if (ssa_op->result_def >= 0
490 && ssa->vars[ssa_op->result_def].var < op_array->num_args) {
491 return 1;
492 }
493 return 0;
494 }
495
may_throw_dce_exception(const zend_op * opline)496 static inline zend_bool may_throw_dce_exception(const zend_op *opline) {
497 return opline->opcode == ZEND_ADD_ARRAY_ELEMENT && opline->op2_type == IS_UNUSED;
498 }
499
dce_optimize_op_array(zend_op_array * op_array,zend_ssa * ssa,zend_bool reorder_dtor_effects)500 int dce_optimize_op_array(zend_op_array *op_array, zend_ssa *ssa, zend_bool reorder_dtor_effects) {
501 int i;
502 zend_ssa_phi *phi;
503 int removed_ops = 0;
504
505 /* DCE of CV operations that changes arguments may affect vararg functions. */
506 zend_bool has_varargs = (ssa->cfg.flags & ZEND_FUNC_VARARG) != 0;
507
508 context ctx;
509 ctx.ssa = ssa;
510 ctx.op_array = op_array;
511 ctx.reorder_dtor_effects = reorder_dtor_effects;
512
513 /* We have no dedicated phi vector, so we use the whole ssa var vector instead */
514 ctx.instr_worklist_len = zend_bitset_len(op_array->last);
515 ctx.instr_worklist = alloca(sizeof(zend_ulong) * ctx.instr_worklist_len);
516 memset(ctx.instr_worklist, 0, sizeof(zend_ulong) * ctx.instr_worklist_len);
517 ctx.phi_worklist_len = zend_bitset_len(ssa->vars_count);
518 ctx.phi_worklist = alloca(sizeof(zend_ulong) * ctx.phi_worklist_len);
519 memset(ctx.phi_worklist, 0, sizeof(zend_ulong) * ctx.phi_worklist_len);
520 ctx.phi_worklist_no_val = alloca(sizeof(zend_ulong) * ctx.phi_worklist_len);
521 memset(ctx.phi_worklist_no_val, 0, sizeof(zend_ulong) * ctx.phi_worklist_len);
522
523 /* Optimistically assume all instructions and phis to be dead */
524 ctx.instr_dead = alloca(sizeof(zend_ulong) * ctx.instr_worklist_len);
525 memset(ctx.instr_dead, 0, sizeof(zend_ulong) * ctx.instr_worklist_len);
526 ctx.phi_dead = alloca(sizeof(zend_ulong) * ctx.phi_worklist_len);
527 memset(ctx.phi_dead, 0xff, sizeof(zend_ulong) * ctx.phi_worklist_len);
528
529 /* Mark non-CV phis as live. Even if the result is unused, we generally cannot remove one
530 * of the producing instructions, as it combines producing the result with control flow.
531 * This can be made more precise if there are any cases where this is not the case. */
532 FOREACH_PHI(phi) {
533 if (phi->var >= op_array->last_var
534 && may_be_refcounted(ssa->var_info[phi->ssa_var].type)) {
535 zend_bitset_excl(ctx.phi_dead, phi->ssa_var);
536 add_phi_sources_to_worklists(&ctx, phi, 0);
537 }
538 } FOREACH_PHI_END();
539
540 /* Mark reacable instruction without side effects as dead */
541 int b = ssa->cfg.blocks_count;
542 while (b > 0) {
543 int op_data = -1;
544
545 b--;
546 zend_basic_block *block = &ssa->cfg.blocks[b];
547 if (!(block->flags & ZEND_BB_REACHABLE)) {
548 continue;
549 }
550 i = block->start + block->len;
551 while (i > block->start) {
552 i--;
553
554 if (op_array->opcodes[i].opcode == ZEND_OP_DATA) {
555 op_data = i;
556 continue;
557 }
558
559 if (zend_bitset_in(ctx.instr_worklist, i)) {
560 zend_bitset_excl(ctx.instr_worklist, i);
561 add_operands_to_worklists(&ctx, &op_array->opcodes[i], &ssa->ops[i], ssa, 0);
562 if (op_data >= 0) {
563 add_operands_to_worklists(&ctx, &op_array->opcodes[op_data], &ssa->ops[op_data], ssa, 0);
564 }
565 } else if (may_have_side_effects(op_array, ssa, &op_array->opcodes[i], &ssa->ops[i], ctx.reorder_dtor_effects)
566 || (zend_may_throw(&op_array->opcodes[i], &ssa->ops[i], op_array, ssa)
567 && !may_throw_dce_exception(&op_array->opcodes[i]))
568 || (has_varargs && may_break_varargs(op_array, ssa, &ssa->ops[i]))) {
569 if (op_array->opcodes[i].opcode == ZEND_NEW
570 && op_array->opcodes[i+1].opcode == ZEND_DO_FCALL
571 && ssa->ops[i].result_def >= 0
572 && ssa->vars[ssa->ops[i].result_def].escape_state == ESCAPE_STATE_NO_ESCAPE) {
573 zend_bitset_incl(ctx.instr_dead, i);
574 zend_bitset_incl(ctx.instr_dead, i+1);
575 } else {
576 add_operands_to_worklists(&ctx, &op_array->opcodes[i], &ssa->ops[i], ssa, 0);
577 if (op_data >= 0) {
578 add_operands_to_worklists(&ctx, &op_array->opcodes[op_data], &ssa->ops[op_data], ssa, 0);
579 }
580 }
581 } else {
582 zend_bitset_incl(ctx.instr_dead, i);
583 if (op_data >= 0) {
584 zend_bitset_incl(ctx.instr_dead, op_data);
585 }
586 }
587 op_data = -1;
588 }
589 }
590
591 /* Propagate liveness backwards to all definitions of used vars */
592 while (!zend_bitset_empty(ctx.instr_worklist, ctx.instr_worklist_len)
593 || !zend_bitset_empty(ctx.phi_worklist, ctx.phi_worklist_len)) {
594 while ((i = zend_bitset_pop_first(ctx.instr_worklist, ctx.instr_worklist_len)) >= 0) {
595 zend_bitset_excl(ctx.instr_dead, i);
596 add_operands_to_worklists(&ctx, &op_array->opcodes[i], &ssa->ops[i], ssa, 1);
597 if (i < op_array->last
598 && (op_array->opcodes[i+1].opcode == ZEND_OP_DATA
599 || (op_array->opcodes[i].opcode == ZEND_NEW
600 && op_array->opcodes[i+1].opcode == ZEND_DO_FCALL))) {
601 zend_bitset_excl(ctx.instr_dead, i+1);
602 add_operands_to_worklists(&ctx, &op_array->opcodes[i+1], &ssa->ops[i+1], ssa, 1);
603 }
604 }
605 while ((i = zend_bitset_pop_first(ctx.phi_worklist, ctx.phi_worklist_len)) >= 0) {
606 zend_bitset_excl(ctx.phi_dead, i);
607 zend_bitset_excl(ctx.phi_worklist_no_val, i);
608 add_phi_sources_to_worklists(&ctx, ssa->vars[i].definition_phi, 1);
609 }
610 }
611
612 /* Eliminate dead instructions */
613 ZEND_BITSET_FOREACH(ctx.instr_dead, ctx.instr_worklist_len, i) {
614 removed_ops += dce_instr(&ctx, &op_array->opcodes[i], &ssa->ops[i]);
615 } ZEND_BITSET_FOREACH_END();
616
617 /* Improper uses don't count as "uses" for the purpose of instruction elimination,
618 * but we have to retain phis defining them.
619 * Propagate this information backwards, marking any phi with an improperly used
620 * target as non-dead. */
621 while ((i = zend_bitset_pop_first(ctx.phi_worklist_no_val, ctx.phi_worklist_len)) >= 0) {
622 zend_ssa_phi *phi = ssa->vars[i].definition_phi;
623 int source;
624 zend_bitset_excl(ctx.phi_dead, i);
625 FOREACH_PHI_SOURCE(phi, source) {
626 add_to_phi_worklist_no_val(&ctx, source);
627 } FOREACH_PHI_SOURCE_END();
628 }
629
630 /* Now collect the actually dead phis */
631 FOREACH_PHI(phi) {
632 if (zend_bitset_in(ctx.phi_dead, phi->ssa_var)) {
633 zend_ssa_remove_uses_of_var(ssa, phi->ssa_var);
634 zend_ssa_remove_phi(ssa, phi);
635 } else {
636 /* Remove trivial phis (phis with identical source operands) */
637 try_remove_trivial_phi(&ctx, phi);
638 }
639 } FOREACH_PHI_END();
640
641 return removed_ops;
642 }
643