Coverage Report

Created: 2026-09-14 06:25

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
51.5k
static inline bool is_bad_mod(const zend_ssa *ssa, int use, int def) {
61
51.5k
  if (def < 0) {
62
    /* This modification is not tracked by SSA, assume the worst */
63
9.25k
    return true;
64
9.25k
  }
65
42.2k
  if (ssa->var_info[use].type & MAY_BE_REF) {
66
    /* Modification of reference may have side-effect */
67
15.3k
    return true;
68
15.3k
  }
69
26.8k
  return false;
70
42.2k
}
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
849k
    bool reorder_dtor_effects) {
76
849k
  switch (opline->opcode) {
77
4.72k
    case ZEND_NOP:
78
4.92k
    case ZEND_IS_IDENTICAL:
79
4.97k
    case ZEND_IS_NOT_IDENTICAL:
80
11.4k
    case ZEND_QM_ASSIGN:
81
16.3k
    case ZEND_FE_FREE:
82
16.7k
    case ZEND_TYPE_CHECK:
83
16.8k
    case ZEND_DEFINED:
84
18.7k
    case ZEND_ADD:
85
19.5k
    case ZEND_SUB:
86
20.5k
    case ZEND_MUL:
87
20.6k
    case ZEND_POW:
88
20.8k
    case ZEND_BW_OR:
89
20.9k
    case ZEND_BW_AND:
90
21.5k
    case ZEND_BW_XOR:
91
23.1k
    case ZEND_CONCAT:
92
24.1k
    case ZEND_FAST_CONCAT:
93
24.3k
    case ZEND_DIV:
94
24.5k
    case ZEND_MOD:
95
24.8k
    case ZEND_BOOL_XOR:
96
25.6k
    case ZEND_BOOL:
97
26.4k
    case ZEND_BOOL_NOT:
98
27.7k
    case ZEND_BW_NOT:
99
27.9k
    case ZEND_SL:
100
28.2k
    case ZEND_SR:
101
29.1k
    case ZEND_IS_EQUAL:
102
30.4k
    case ZEND_IS_NOT_EQUAL:
103
33.6k
    case ZEND_IS_SMALLER:
104
33.7k
    case ZEND_IS_SMALLER_OR_EQUAL:
105
33.8k
    case ZEND_CASE:
106
33.8k
    case ZEND_CASE_STRICT:
107
33.9k
    case ZEND_CAST:
108
33.9k
    case ZEND_ROPE_INIT:
109
33.9k
    case ZEND_ROPE_ADD:
110
34.2k
    case ZEND_INIT_ARRAY:
111
34.2k
    case ZEND_SPACESHIP:
112
35.5k
    case ZEND_STRLEN:
113
35.7k
    case ZEND_COUNT:
114
35.8k
    case ZEND_GET_TYPE:
115
35.8k
    case ZEND_ISSET_ISEMPTY_THIS:
116
36.0k
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
117
36.2k
    case ZEND_FETCH_DIM_IS:
118
36.2k
    case ZEND_ISSET_ISEMPTY_CV:
119
36.2k
    case ZEND_ISSET_ISEMPTY_VAR:
120
36.2k
    case ZEND_FETCH_IS:
121
36.2k
    case ZEND_IN_ARRAY:
122
36.2k
    case ZEND_FUNC_NUM_ARGS:
123
36.2k
    case ZEND_FUNC_GET_ARGS:
124
36.2k
    case ZEND_ARRAY_KEY_EXISTS:
125
37.5k
    case ZEND_COPY_TMP:
126
      /* No side effects */
127
37.5k
      return false;
128
28.6k
    case ZEND_FREE:
129
28.6k
      return opline->extended_value == ZEND_FREE_VOID_CAST;
130
133
    case ZEND_ADD_ARRAY_ELEMENT:
131
      /* TODO: We can't free two vars. Keep instruction alive. <?php [0, "$a" => "$b"]; */
132
133
      if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && (opline->op2_type & (IS_VAR|IS_TMP_VAR))) {
133
5
        return true;
134
5
      }
135
128
      return false;
136
425
    case ZEND_ROPE_END:
137
      /* TODO: Rope dce optimization, see #76446 */
138
425
      return true;
139
20.5k
    case ZEND_JMP:
140
31.9k
    case ZEND_JMPZ:
141
44.0k
    case ZEND_JMPNZ:
142
44.6k
    case ZEND_JMPZ_EX:
143
45.4k
    case ZEND_JMPNZ_EX:
144
47.2k
    case ZEND_JMP_SET:
145
47.5k
    case ZEND_COALESCE:
146
48.6k
    case ZEND_ASSERT_CHECK:
147
50.0k
    case ZEND_JMP_NULL:
148
50.1k
    case ZEND_BIND_INIT_STATIC_OR_JMP:
149
50.1k
    case ZEND_JMP_FRAMELESS:
150
      /* For our purposes a jumps and branches are side effects. */
151
50.1k
      return true;
152
0
    case ZEND_BEGIN_SILENCE:
153
67.7k
    case ZEND_END_SILENCE:
154
114k
    case ZEND_ECHO:
155
114k
    case ZEND_INCLUDE_OR_EVAL:
156
116k
    case ZEND_THROW:
157
116k
    case ZEND_MATCH_ERROR:
158
116k
    case ZEND_EXT_STMT:
159
116k
    case ZEND_EXT_FCALL_BEGIN:
160
116k
    case ZEND_EXT_FCALL_END:
161
117k
    case ZEND_TICKS:
162
118k
    case ZEND_YIELD:
163
118k
    case ZEND_VERIFY_NEVER_TYPE:
164
      /* Intrinsic side effects */
165
118k
      return true;
166
606
    case ZEND_YIELD_FROM: {
167
606
      uint32_t t1 = OP1_INFO();
168
606
      if ((t1 & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY && MAY_BE_EMPTY_ONLY(t1)) {
169
20
        return false;
170
20
      }
171
586
      return true;
172
606
    }
173
110k
    case ZEND_DO_FCALL:
174
110k
    case ZEND_DO_FCALL_BY_NAME:
175
110k
    case ZEND_DO_ICALL:
176
117k
    case ZEND_DO_UCALL:
177
117k
    case ZEND_FRAMELESS_ICALL_0:
178
117k
    case ZEND_FRAMELESS_ICALL_1:
179
117k
    case ZEND_FRAMELESS_ICALL_2:
180
117k
    case ZEND_FRAMELESS_ICALL_3:
181
      /* For now assume all calls have side effects */
182
117k
      return true;
183
10.4k
    case ZEND_RECV:
184
12.8k
    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
12.8k
      return true;
188
886
    case ZEND_ASSIGN_REF:
189
886
      return true;
190
29.6k
    case ZEND_ASSIGN:
191
29.6k
    {
192
29.6k
      if (is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def)) {
193
10.7k
        return true;
194
10.7k
      }
195
18.8k
      if (!reorder_dtor_effects) {
196
18.8k
        if (opline->op2_type != IS_CONST
197
7.05k
          && (OP2_INFO() & MAY_HAVE_DTOR)
198
2.20k
          && ssa->vars[ssa_op->op2_use].escape_state != ESCAPE_STATE_NO_ESCAPE) {
199
          /* DCE might shorten lifetime */
200
1.94k
          return true;
201
1.94k
        }
202
18.8k
      }
203
16.9k
      return false;
204
18.8k
    }
205
122
    case ZEND_UNSET_VAR:
206
122
      return true;
207
2.03k
    case ZEND_UNSET_CV:
208
2.03k
    {
209
2.03k
      uint32_t t1 = OP1_INFO();
210
2.03k
      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
1.43k
        return true;
216
1.43k
      }
217
601
      return false;
218
2.03k
    }
219
5.51k
    case ZEND_PRE_INC:
220
6.03k
    case ZEND_POST_INC:
221
6.35k
    case ZEND_PRE_DEC:
222
6.63k
    case ZEND_POST_DEC:
223
6.63k
      return is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def);
224
2.07k
    case ZEND_ASSIGN_OP:
225
2.07k
      return is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def)
226
1.51k
        || ssa->vars[ssa_op->op1_def].escape_state != ESCAPE_STATE_NO_ESCAPE;
227
5.67k
    case ZEND_ASSIGN_DIM:
228
12.9k
    case ZEND_ASSIGN_OBJ:
229
12.9k
      if (is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def)
230
12.1k
        || ssa->vars[ssa_op->op1_def].escape_state != ESCAPE_STATE_NO_ESCAPE) {
231
12.1k
        return true;
232
12.1k
      }
233
806
      if (!reorder_dtor_effects) {
234
806
        opline++;
235
806
        ssa_op++;
236
806
        if (opline->op1_type != IS_CONST
237
550
          && (OP1_INFO() & MAY_HAVE_DTOR)) {
238
          /* DCE might shorten lifetime */
239
208
          return true;
240
208
        }
241
806
      }
242
598
      return false;
243
230
    case ZEND_PRE_INC_OBJ:
244
290
    case ZEND_PRE_DEC_OBJ:
245
290
    case ZEND_POST_INC_OBJ:
246
290
    case ZEND_POST_DEC_OBJ:
247
290
      if (is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def)
248
274
        || ssa->vars[ssa_op->op1_def].escape_state != ESCAPE_STATE_NO_ESCAPE) {
249
274
        return true;
250
274
      }
251
16
      return false;
252
643
    case ZEND_BIND_STATIC:
253
643
      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
643
        if ((opline->extended_value & (ZEND_BIND_IMPLICIT|ZEND_BIND_EXPLICIT))) {
258
285
          return true;
259
285
        }
260
        /* Modifies static variables which are observable through reflection */
261
358
        if ((opline->extended_value & ZEND_BIND_REF) && opline->op2_type != IS_UNUSED) {
262
198
          return true;
263
198
        }
264
358
      }
265
160
      return false;
266
364
    case ZEND_CHECK_VAR:
267
364
      return (OP1_INFO() & MAY_BE_UNDEF) != 0;
268
10
    case ZEND_FE_RESET_R:
269
10
    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
10
      return (OP1_INFO() & MAY_BE_ANY) != MAY_BE_ARRAY;
273
426k
    default:
274
      /* For everything we didn't handle, assume a side-effect */
275
426k
      return true;
276
849k
  }
277
849k
}
278
279
1.56M
static zend_always_inline void add_to_worklists(const context *ctx, int var_num, int check) {
280
1.56M
  const zend_ssa_var *var = &ctx->ssa->vars[var_num];
281
1.56M
  if (var->definition >= 0) {
282
1.11M
    if (!check || zend_bitset_in(ctx->instr_dead, var->definition)) {
283
1.01M
      zend_bitset_incl(ctx->instr_worklist, var->definition);
284
1.01M
    }
285
1.11M
  } else if (var->definition_phi) {
286
295k
    if (!check || zend_bitset_in(ctx->phi_dead, var_num)) {
287
194k
      zend_bitset_incl(ctx->phi_worklist, var_num);
288
194k
    }
289
295k
  }
290
1.56M
}
291
292
56.1k
static inline void add_to_phi_worklist_no_val(const context *ctx, int var_num) {
293
56.1k
  const zend_ssa_var *var = &ctx->ssa->vars[var_num];
294
56.1k
  if (var->definition_phi && zend_bitset_in(ctx->phi_dead, var_num)) {
295
9.41k
    zend_bitset_incl(ctx->phi_worklist_no_val, var_num);
296
9.41k
  }
297
56.1k
}
298
299
1.73M
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
1.73M
  if (ssa_op->result_use >= 0) {
301
21.9k
    add_to_worklists(ctx, ssa_op->result_use, check);
302
21.9k
  }
303
1.73M
  if (ssa_op->op1_use >= 0) {
304
969k
    if (!zend_ssa_is_no_val_use(opline, ssa_op, ssa_op->op1_use)
305
87.4k
     || (opline->opcode == ZEND_ASSIGN
306
931k
      && (ssa->var_info[ssa_op->op1_use].type & MAY_BE_REF) != 0)) {
307
931k
      add_to_worklists(ctx, ssa_op->op1_use, check);
308
931k
    } else {
309
38.0k
      add_to_phi_worklist_no_val(ctx, ssa_op->op1_use);
310
38.0k
    }
311
969k
  }
312
1.73M
  if (ssa_op->op2_use >= 0) {
313
296k
    if (!zend_ssa_is_no_val_use(opline, ssa_op, ssa_op->op2_use)
314
4.39k
     || (opline->opcode == ZEND_FE_FETCH_R
315
294k
      && (ssa->var_info[ssa_op->op2_use].type & MAY_BE_REF) != 0)) {
316
294k
      add_to_worklists(ctx, ssa_op->op2_use, check);
317
294k
    } else {
318
2.11k
      add_to_phi_worklist_no_val(ctx, ssa_op->op2_use);
319
2.11k
    }
320
296k
  }
321
1.73M
}
322
323
174k
static zend_always_inline void add_phi_sources_to_worklists(const context *ctx, zend_ssa_phi *phi, int check) {
324
174k
  const zend_ssa *ssa = ctx->ssa;
325
174k
  int source;
326
799k
  FOREACH_PHI_SOURCE(phi, source) {
327
799k
    add_to_worklists(ctx, source, check);
328
799k
  } FOREACH_PHI_SOURCE_END();
329
174k
}
330
331
26.4k
static inline bool is_var_dead(const context *ctx, int var_num) {
332
26.4k
  const zend_ssa_var *var = &ctx->ssa->vars[var_num];
333
26.4k
  if (var->definition_phi) {
334
2.24k
    return zend_bitset_in(ctx->phi_dead, var_num);
335
24.1k
  } else if (var->definition >= 0) {
336
14.7k
    return zend_bitset_in(ctx->instr_dead, var->definition);
337
14.7k
  } 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
9.40k
    return var_num >= ctx->op_array->last_var;
342
9.40k
  }
343
26.4k
}
344
345
// Sometimes we can mark the var as EXT_UNUSED
346
12.1k
static bool try_remove_var_def(const context *ctx, int free_var, int use_chain, const zend_op *opline) {
347
12.1k
  if (use_chain >= 0) {
348
1.45k
    return false;
349
1.45k
  }
350
10.7k
  zend_ssa_var *var = &ctx->ssa->vars[free_var];
351
10.7k
  int def = var->definition;
352
353
10.7k
  if (def >= 0) {
354
10.6k
    zend_ssa_op *def_op = &ctx->ssa->ops[def];
355
356
10.6k
    if (def_op->result_def == free_var
357
10.6k
        && var->phi_use_chain == NULL
358
10.6k
        && var->use_chain == (opline - ctx->op_array->opcodes)) {
359
10.3k
      zend_op *def_opline = &ctx->op_array->opcodes[def];
360
361
10.3k
      switch (def_opline->opcode) {
362
409
        case ZEND_ASSIGN:
363
409
        case ZEND_ASSIGN_REF:
364
445
        case ZEND_ASSIGN_DIM:
365
453
        case ZEND_ASSIGN_OBJ:
366
453
        case ZEND_ASSIGN_OBJ_REF:
367
453
        case ZEND_ASSIGN_STATIC_PROP:
368
453
        case ZEND_ASSIGN_STATIC_PROP_REF:
369
778
        case ZEND_ASSIGN_OP:
370
786
        case ZEND_ASSIGN_DIM_OP:
371
790
        case ZEND_ASSIGN_OBJ_OP:
372
790
        case ZEND_ASSIGN_STATIC_PROP_OP:
373
798
        case ZEND_PRE_INC:
374
806
        case ZEND_PRE_DEC:
375
806
        case ZEND_PRE_INC_OBJ:
376
806
        case ZEND_PRE_DEC_OBJ:
377
806
        case ZEND_DO_ICALL:
378
808
        case ZEND_DO_UCALL:
379
808
        case ZEND_DO_FCALL_BY_NAME:
380
1.08k
        case ZEND_DO_FCALL:
381
1.08k
        case ZEND_INCLUDE_OR_EVAL:
382
1.08k
        case ZEND_YIELD:
383
1.08k
        case ZEND_YIELD_FROM:
384
1.08k
        case ZEND_ASSERT_CHECK:
385
1.08k
          def_opline->result_type = IS_UNUSED;
386
1.08k
          def_opline->result.var = 0;
387
1.08k
          def_op->result_def = -1;
388
1.08k
          var->definition = -1;
389
1.08k
          return true;
390
9.24k
        default:
391
9.24k
          break;
392
10.3k
      }
393
10.3k
    }
394
10.6k
  }
395
9.63k
  return false;
396
10.7k
}
397
398
64.8k
static zend_always_inline bool may_be_refcounted(uint32_t type) {
399
64.8k
  return (type & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) != 0;
400
64.8k
}
401
402
34.1k
static inline bool is_free_of_live_var(const context *ctx, const zend_op *opline, const zend_ssa_op *ssa_op) {
403
34.1k
  switch (opline->opcode) {
404
14.0k
    case ZEND_FREE:
405
      /* It is always safe to remove FREEs of non-refcounted values, even if they are live. */
406
14.0k
      if ((ctx->ssa->var_info[ssa_op->op1_use].type & (MAY_BE_REF|MAY_BE_ANY|MAY_BE_UNDEF)) != 0
407
14.0k
       && !may_be_refcounted(ctx->ssa->var_info[ssa_op->op1_use].type)) {
408
11.4k
        return false;
409
11.4k
      }
410
2.55k
      ZEND_FALLTHROUGH;
411
3.55k
    case ZEND_FE_FREE:
412
3.55k
      return !is_var_dead(ctx, ssa_op->op1_use);
413
19.0k
    default:
414
19.0k
      return false;
415
34.1k
  }
416
34.1k
}
417
418
/* Returns whether the instruction has been DCEd */
419
38.8k
static bool dce_instr(const context *ctx, zend_op *opline, zend_ssa_op *ssa_op) {
420
38.8k
  const zend_ssa *ssa = ctx->ssa;
421
38.8k
  int free_var = -1;
422
38.8k
  uint8_t free_var_type;
423
424
38.8k
  if (opline->opcode == ZEND_NOP) {
425
4.72k
    return false;
426
4.72k
  }
427
428
  /* We mark FREEs as dead, but they're only really dead if the destroyed var is dead */
429
34.1k
  if (is_free_of_live_var(ctx, opline, ssa_op)) {
430
2.82k
    return false;
431
2.82k
  }
432
433
31.2k
  if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && !is_var_dead(ctx, ssa_op->op1_use)) {
434
10.0k
    if (!try_remove_var_def(ctx, ssa_op->op1_use, ssa_op->op1_use_chain, opline)) {
435
9.67k
      if (may_be_refcounted(ssa->var_info[ssa_op->op1_use].type)
436
1.79k
          && opline->opcode != ZEND_CASE
437
1.79k
          && opline->opcode != ZEND_CASE_STRICT
438
1.79k
          && opline->opcode != ZEND_COPY_TMP) {
439
463
        free_var = ssa_op->op1_use;
440
463
        free_var_type = opline->op1_type;
441
463
      }
442
9.67k
    }
443
10.0k
  }
444
31.2k
  if ((opline->op2_type & (IS_VAR|IS_TMP_VAR)) && !is_var_dead(ctx, ssa_op->op2_use)) {
445
2.11k
    if (!try_remove_var_def(ctx, ssa_op->op2_use, ssa_op->op2_use_chain, opline)) {
446
1.42k
      if (may_be_refcounted(ssa->var_info[ssa_op->op2_use].type)) {
447
409
        if (free_var >= 0) {
448
          // TODO: We can't free two vars. Keep instruction alive.
449
127
          zend_bitset_excl(ctx->instr_dead, opline - ctx->op_array->opcodes);
450
127
          return false;
451
127
        }
452
282
        free_var = ssa_op->op2_use;
453
282
        free_var_type = opline->op2_type;
454
282
      }
455
1.42k
    }
456
2.11k
  }
457
458
31.1k
  zend_ssa_rename_defs_of_instr(ctx->ssa, ssa_op);
459
31.1k
  zend_ssa_remove_instr(ctx->ssa, opline, ssa_op);
460
461
31.1k
  if (free_var >= 0) {
462
618
    opline->opcode = ZEND_FREE;
463
618
    opline->op1.var = EX_NUM_TO_VAR(ssa->vars[free_var].var);
464
618
    opline->op1_type = free_var_type;
465
466
618
    ssa_op->op1_use = free_var;
467
618
    ssa_op->op1_use_chain = ssa->vars[free_var].use_chain;
468
618
    ssa->vars[free_var].use_chain = ssa_op - ssa->ops;
469
618
    return false;
470
618
  }
471
30.5k
  return true;
472
31.1k
}
473
474
106k
static inline int get_common_phi_source(const zend_ssa *ssa, zend_ssa_phi *phi) {
475
106k
  int common_source = -1;
476
106k
  int source;
477
538k
  FOREACH_PHI_SOURCE(phi, source) {
478
538k
    if (source == phi->ssa_var) {
479
1.57k
      continue;
480
1.57k
    }
481
214k
    if (common_source == -1) {
482
106k
      common_source = source;
483
107k
    } else if (common_source != source) {
484
106k
      return -1;
485
106k
    }
486
214k
  } 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
774
  return common_source;
492
106k
}
493
494
150k
static void try_remove_trivial_phi(const context *ctx, zend_ssa_phi *phi) {
495
150k
  zend_ssa *ssa = ctx->ssa;
496
150k
  if (phi->pi < 0) {
497
    /* Phi assignment with identical source operands */
498
106k
    int common_source = get_common_phi_source(ssa, phi);
499
106k
    if (common_source >= 0) {
500
774
      zend_ssa_rename_var_uses(ssa, phi->ssa_var, common_source, 1);
501
774
      zend_ssa_remove_phi(ssa, phi);
502
774
    }
503
106k
  } 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
43.7k
  }
512
150k
}
513
514
110
static inline bool may_break_varargs(const zend_op_array *op_array, const zend_ssa *ssa, const zend_ssa_op *ssa_op) {
515
110
  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
110
  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
110
  if (ssa_op->result_def >= 0
524
6
      && ssa->vars[ssa_op->result_def].var < op_array->num_args) {
525
0
    return true;
526
0
  }
527
110
  return false;
528
110
}
529
530
37.3k
static inline bool may_throw_dce_exception(const zend_op *opline) {
531
37.3k
  return opline->opcode == ZEND_ADD_ARRAY_ELEMENT && opline->op2_type == IS_UNUSED;
532
37.3k
}
533
534
90.1k
int dce_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *optimizer_ctx, zend_ssa *ssa, bool reorder_dtor_effects) {
535
90.1k
  int i;
536
90.1k
  zend_ssa_phi *phi;
537
90.1k
  int removed_ops = 0;
538
539
  /* DCE of CV operations that changes arguments may affect vararg functions. */
540
90.1k
  bool has_varargs = (ssa->cfg.flags & ZEND_FUNC_VARARG) != 0;
541
542
90.1k
  context ctx;
543
90.1k
  ctx.ssa = ssa;
544
90.1k
  ctx.op_array = op_array;
545
90.1k
  ctx.reorder_dtor_effects = reorder_dtor_effects;
546
547
90.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
90.1k
  ctx.instr_worklist_len = zend_bitset_len(op_array->last);
550
90.1k
  ctx.instr_worklist = zend_arena_calloc(&optimizer_ctx->arena, ctx.instr_worklist_len, sizeof(zend_ulong));
551
90.1k
  ctx.phi_worklist_len = zend_bitset_len(ssa->vars_count);
552
90.1k
  ctx.phi_worklist = zend_arena_calloc(&optimizer_ctx->arena, ctx.phi_worklist_len, sizeof(zend_ulong));
553
90.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
90.1k
  ctx.instr_dead = zend_arena_calloc(&optimizer_ctx->arena, ctx.instr_worklist_len, sizeof(zend_ulong));
557
90.1k
  ctx.phi_dead = zend_arena_alloc(&optimizer_ctx->arena, ctx.phi_worklist_len * sizeof(zend_ulong));
558
90.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
494k
  FOREACH_PHI(phi) {
564
494k
    if (phi->var >= op_array->last_var
565
39.7k
        && may_be_refcounted(ssa->var_info[phi->ssa_var].type)) {
566
32.4k
      zend_bitset_excl(ctx.phi_dead, phi->ssa_var);
567
32.4k
      add_phi_sources_to_worklists(&ctx, phi, 0);
568
32.4k
    }
569
494k
  } FOREACH_PHI_END();
570
571
  /* Mark reachable instruction without side effects as dead */
572
90.1k
  uint32_t b = ssa->cfg.blocks_count;
573
334k
  while (b > 0) {
574
244k
    int op_data = -1;
575
576
244k
    b--;
577
244k
    const zend_basic_block *block = &ssa->cfg.blocks[b];
578
244k
    if (!(block->flags & ZEND_BB_REACHABLE)) {
579
6.48k
      continue;
580
6.48k
    }
581
238k
    i = block->start + block->len;
582
2.01M
    while (i > block->start) {
583
1.77M
      i--;
584
585
1.77M
      if (op_array->opcodes[i].opcode == ZEND_OP_DATA) {
586
31.5k
        op_data = i;
587
31.5k
        continue;
588
31.5k
      }
589
590
1.74M
      if (zend_bitset_in(ctx.instr_worklist, i)) {
591
893k
        zend_bitset_excl(ctx.instr_worklist, i);
592
893k
        add_operands_to_worklists(&ctx, &op_array->opcodes[i], &ssa->ops[i], ssa, 0);
593
893k
        if (op_data >= 0) {
594
15.8k
          add_operands_to_worklists(&ctx, &op_array->opcodes[op_data], &ssa->ops[op_data], ssa, 0);
595
15.8k
        }
596
893k
      } else if (may_have_side_effects(op_array, ssa, &op_array->opcodes[i], &ssa->ops[i], ctx.reorder_dtor_effects)
597
89.0k
          || (zend_may_throw(&op_array->opcodes[i], &ssa->ops[i], op_array, ssa)
598
37.3k
            && !may_throw_dce_exception(&op_array->opcodes[i]))
599
797k
          || (has_varargs && may_break_varargs(op_array, ssa, &ssa->ops[i]))) {
600
797k
        if (op_array->opcodes[i].opcode == ZEND_NEW
601
124
            && op_array->opcodes[i+1].opcode == ZEND_DO_FCALL
602
122
            && ssa->ops[i].result_def >= 0
603
122
            && ssa->vars[ssa->ops[i].result_def].escape_state == ESCAPE_STATE_NO_ESCAPE) {
604
112
          zend_bitset_incl(ctx.instr_dead, i);
605
112
          zend_bitset_incl(ctx.instr_dead, i+1);
606
797k
        } else {
607
797k
          add_operands_to_worklists(&ctx, &op_array->opcodes[i], &ssa->ops[i], ssa, 0);
608
797k
          if (op_data >= 0) {
609
15.5k
            add_operands_to_worklists(&ctx, &op_array->opcodes[op_data], &ssa->ops[op_data], ssa, 0);
610
15.5k
          }
611
797k
        }
612
797k
      } else {
613
51.7k
        zend_bitset_incl(ctx.instr_dead, i);
614
51.7k
        if (op_data >= 0) {
615
136
          zend_bitset_incl(ctx.instr_dead, op_data);
616
136
        }
617
51.7k
      }
618
1.74M
      op_data = -1;
619
1.74M
    }
620
238k
  }
621
622
  /* Propagate liveness backwards to all definitions of used vars */
623
107k
  while (!zend_bitset_empty(ctx.instr_worklist, ctx.instr_worklist_len)
624
103k
      || !zend_bitset_empty(ctx.phi_worklist, ctx.phi_worklist_len)) {
625
30.9k
    while ((i = zend_bitset_pop_first(ctx.instr_worklist, ctx.instr_worklist_len)) >= 0) {
626
13.3k
      zend_bitset_excl(ctx.instr_dead, i);
627
13.3k
      add_operands_to_worklists(&ctx, &op_array->opcodes[i], &ssa->ops[i], ssa, 1);
628
13.3k
      if (i < op_array->last
629
13.3k
       && (op_array->opcodes[i+1].opcode == ZEND_OP_DATA
630
13.3k
        || (op_array->opcodes[i].opcode == ZEND_NEW
631
90
         && op_array->opcodes[i+1].opcode == ZEND_DO_FCALL))) {
632
90
        zend_bitset_excl(ctx.instr_dead, i+1);
633
90
        add_operands_to_worklists(&ctx, &op_array->opcodes[i+1], &ssa->ops[i+1], ssa, 1);
634
90
      }
635
13.3k
    }
636
160k
    while ((i = zend_bitset_pop_first(ctx.phi_worklist, ctx.phi_worklist_len)) >= 0) {
637
142k
      zend_bitset_excl(ctx.phi_dead, i);
638
142k
      zend_bitset_excl(ctx.phi_worklist_no_val, i);
639
142k
      add_phi_sources_to_worklists(&ctx, ssa->vars[i].definition_phi, 1);
640
142k
    }
641
17.6k
  }
642
643
  /* Eliminate dead instructions */
644
436k
  ZEND_BITSET_FOREACH(ctx.instr_dead, ctx.instr_worklist_len, i) {
645
38.8k
    removed_ops += dce_instr(&ctx, &op_array->opcodes[i], &ssa->ops[i]);
646
38.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
98.1k
  while ((i = zend_bitset_pop_first(ctx.phi_worklist_no_val, ctx.phi_worklist_len)) >= 0) {
653
8.00k
    zend_ssa_phi *phi = ssa->vars[i].definition_phi;
654
8.00k
    int source;
655
8.00k
    zend_bitset_excl(ctx.phi_dead, i);
656
39.9k
    FOREACH_PHI_SOURCE(phi, source) {
657
39.9k
      add_to_phi_worklist_no_val(&ctx, source);
658
39.9k
    } FOREACH_PHI_SOURCE_END();
659
8.00k
  }
660
661
  /* Now collect the actually dead phis */
662
494k
  FOREACH_PHI(phi) {
663
494k
    if (zend_bitset_in(ctx.phi_dead, phi->ssa_var)) {
664
8.70k
      zend_ssa_remove_uses_of_var(ssa, phi->ssa_var);
665
8.70k
      zend_ssa_remove_phi(ssa, phi);
666
150k
    } else {
667
      /* Remove trivial phis (phis with identical source operands) */
668
150k
      try_remove_trivial_phi(&ctx, phi);
669
150k
    }
670
494k
  } FOREACH_PHI_END();
671
672
90.1k
  zend_arena_release(&optimizer_ctx->arena, checkpoint);
673
674
90.1k
  return removed_ops;
675
90.1k
}