Coverage Report

Created: 2026-06-02 06:39

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