Coverage Report

Created: 2025-09-27 06:26

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