Coverage Report

Created: 2026-01-18 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/Optimizer/zend_dfg.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine, DFG - Data Flow Graph                                   |
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: Dmitry Stogov <dmitry@php.net>                              |
16
   +----------------------------------------------------------------------+
17
*/
18
19
#include "zend_compile.h"
20
#include "zend_dfg.h"
21
22
static zend_always_inline void zend_dfg_add_use_def_op_impl(const zend_op_array *op_array, const zend_op *opline, uint32_t build_flags, zend_bitset use, zend_bitset def) /* {{{ */
23
20.9k
{
24
20.9k
  uint32_t var_num;
25
20.9k
  const zend_op *next;
26
27
20.9k
  if (opline->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
28
16.1k
    var_num = EX_VAR_TO_NUM(opline->op1.var);
29
16.1k
    if (!zend_bitset_in(def, var_num)) {
30
5.72k
      zend_bitset_incl(use, var_num);
31
5.72k
    }
32
16.1k
  }
33
20.9k
  if (((opline->op2_type & (IS_VAR|IS_TMP_VAR)) != 0
34
2.33k
    && opline->opcode != ZEND_FE_FETCH_R
35
2.33k
    && opline->opcode != ZEND_FE_FETCH_RW)
36
18.6k
   || (opline->op2_type == IS_CV)) {
37
4.90k
    var_num = EX_VAR_TO_NUM(opline->op2.var);
38
4.90k
    if (!zend_bitset_in(def, var_num)) {
39
3.81k
      zend_bitset_incl(use, var_num);
40
3.81k
    }
41
4.90k
  }
42
20.9k
  if ((build_flags & ZEND_SSA_USE_CV_RESULTS)
43
0
   && opline->result_type == IS_CV
44
0
   && opline->opcode != ZEND_RECV) {
45
0
    var_num = EX_VAR_TO_NUM(opline->result.var);
46
0
    if (!zend_bitset_in(def, var_num)) {
47
0
      zend_bitset_incl(use, var_num);
48
0
    }
49
0
  }
50
51
20.9k
  switch (opline->opcode) {
52
629
    case ZEND_ASSIGN:
53
629
      if ((build_flags & ZEND_SSA_RC_INFERENCE) && opline->op2_type == IS_CV) {
54
0
        zend_bitset_incl(def, EX_VAR_TO_NUM(opline->op2.var));
55
0
      }
56
629
      if (opline->op1_type == IS_CV) {
57
1.03k
add_op1_def:
58
1.03k
        zend_bitset_incl(def, EX_VAR_TO_NUM(opline->op1.var));
59
1.03k
      }
60
1.03k
      break;
61
1.03k
    case ZEND_ASSIGN_REF:
62
0
      if (opline->op2_type == IS_CV) {
63
0
        zend_bitset_incl(def, EX_VAR_TO_NUM(opline->op2.var));
64
0
      }
65
0
      if (opline->op1_type == IS_CV) {
66
0
        goto add_op1_def;
67
0
      }
68
0
      break;
69
42
    case ZEND_ASSIGN_DIM:
70
76
    case ZEND_ASSIGN_OBJ:
71
76
      next = opline + 1;
72
76
      if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
73
52
        var_num = EX_VAR_TO_NUM(next->op1.var);
74
52
        if (!zend_bitset_in(def, var_num)) {
75
4
          zend_bitset_incl(use, var_num);
76
4
        }
77
52
        if (build_flags & ZEND_SSA_RC_INFERENCE && next->op1_type == IS_CV) {
78
0
          zend_bitset_incl(def, var_num);
79
0
        }
80
52
      }
81
76
      if (opline->op1_type == IS_CV) {
82
50
        goto add_op1_def;
83
50
      }
84
26
      break;
85
26
    case ZEND_ASSIGN_OBJ_REF:
86
0
      next = opline + 1;
87
0
      if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
88
0
        var_num = EX_VAR_TO_NUM(next->op1.var);
89
0
        if (!zend_bitset_in(def, var_num)) {
90
0
          zend_bitset_incl(use, var_num);
91
0
        }
92
0
        if (next->op1_type == IS_CV) {
93
0
          zend_bitset_incl(def, var_num);
94
0
        }
95
0
      }
96
0
      if (opline->op1_type == IS_CV) {
97
0
        goto add_op1_def;
98
0
      }
99
0
      break;
100
0
    case ZEND_ASSIGN_STATIC_PROP:
101
0
      next = opline + 1;
102
0
      if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
103
0
        var_num = EX_VAR_TO_NUM(next->op1.var);
104
0
        if (!zend_bitset_in(def, var_num)) {
105
0
          zend_bitset_incl(use, var_num);
106
0
        }
107
0
        if ((build_flags & ZEND_SSA_RC_INFERENCE) && next->op1_type == IS_CV) {
108
0
          zend_bitset_incl(def, var_num);
109
0
        }
110
0
      }
111
0
      break;
112
0
    case ZEND_ASSIGN_STATIC_PROP_REF:
113
0
      next = opline + 1;
114
0
      if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
115
0
        var_num = EX_VAR_TO_NUM(next->op1.var);
116
0
        if (!zend_bitset_in(def, var_num)) {
117
0
          zend_bitset_incl(use, var_num);
118
0
        }
119
0
        if (next->op1_type == IS_CV) {
120
0
          zend_bitset_incl(def, var_num);
121
0
        }
122
0
      }
123
0
      break;
124
0
    case ZEND_ASSIGN_STATIC_PROP_OP:
125
0
    case ZEND_FRAMELESS_ICALL_3:
126
0
      next = opline + 1;
127
0
      if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
128
0
        var_num = EX_VAR_TO_NUM(next->op1.var);
129
0
        if (!zend_bitset_in(def, var_num)) {
130
0
          zend_bitset_incl(use, var_num);
131
0
        }
132
0
      }
133
0
      break;
134
0
    case ZEND_ASSIGN_DIM_OP:
135
4
    case ZEND_ASSIGN_OBJ_OP:
136
4
      next = opline + 1;
137
4
      if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
138
4
        var_num = EX_VAR_TO_NUM(next->op1.var);
139
4
        if (!zend_bitset_in(def, var_num)) {
140
4
          zend_bitset_incl(use, var_num);
141
4
        }
142
4
      }
143
4
      if (opline->op1_type == IS_CV) {
144
4
        goto add_op1_def;
145
4
      }
146
0
      break;
147
134
    case ZEND_ASSIGN_OP:
148
167
    case ZEND_PRE_INC:
149
167
    case ZEND_PRE_DEC:
150
238
    case ZEND_POST_INC:
151
270
    case ZEND_POST_DEC:
152
270
    case ZEND_BIND_GLOBAL:
153
270
    case ZEND_BIND_STATIC:
154
270
    case ZEND_BIND_INIT_STATIC_OR_JMP:
155
270
    case ZEND_SEND_VAR_NO_REF:
156
270
    case ZEND_SEND_VAR_NO_REF_EX:
157
294
    case ZEND_SEND_VAR_EX:
158
298
    case ZEND_SEND_FUNC_ARG:
159
300
    case ZEND_SEND_REF:
160
300
    case ZEND_SEND_UNPACK:
161
300
    case ZEND_FE_RESET_RW:
162
300
    case ZEND_MAKE_REF:
163
300
    case ZEND_PRE_INC_OBJ:
164
300
    case ZEND_PRE_DEC_OBJ:
165
300
    case ZEND_POST_INC_OBJ:
166
300
    case ZEND_POST_DEC_OBJ:
167
324
    case ZEND_UNSET_DIM:
168
324
    case ZEND_UNSET_OBJ:
169
350
    case ZEND_FETCH_DIM_W:
170
350
    case ZEND_FETCH_DIM_RW:
171
354
    case ZEND_FETCH_DIM_FUNC_ARG:
172
354
    case ZEND_FETCH_DIM_UNSET:
173
354
    case ZEND_FETCH_LIST_W:
174
354
      if (opline->op1_type == IS_CV) {
175
350
        goto add_op1_def;
176
350
      }
177
4
      break;
178
100
    case ZEND_SEND_VAR:
179
124
    case ZEND_CAST:
180
496
    case ZEND_QM_ASSIGN:
181
632
    case ZEND_JMP_SET:
182
686
    case ZEND_COALESCE:
183
699
    case ZEND_FE_RESET_R:
184
699
      if ((build_flags & ZEND_SSA_RC_INFERENCE) && opline->op1_type == IS_CV) {
185
0
        goto add_op1_def;
186
0
      }
187
699
      break;
188
699
    case ZEND_ADD_ARRAY_UNPACK:
189
0
      var_num = EX_VAR_TO_NUM(opline->result.var);
190
0
      if (!zend_bitset_in(def, var_num)) {
191
0
        zend_bitset_incl(use, var_num);
192
0
      }
193
0
      break;
194
189
    case ZEND_ADD_ARRAY_ELEMENT:
195
189
      var_num = EX_VAR_TO_NUM(opline->result.var);
196
189
      if (!zend_bitset_in(def, var_num)) {
197
14
        zend_bitset_incl(use, var_num);
198
14
      }
199
189
      ZEND_FALLTHROUGH;
200
204
    case ZEND_INIT_ARRAY:
201
204
      if (((build_flags & ZEND_SSA_RC_INFERENCE)
202
204
            || (opline->extended_value & ZEND_ARRAY_ELEMENT_REF))
203
0
          && opline->op1_type == IS_CV) {
204
0
        goto add_op1_def;
205
0
      }
206
204
      break;
207
204
    case ZEND_YIELD:
208
0
      if (opline->op1_type == IS_CV
209
0
          && ((op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)
210
0
            || (build_flags & ZEND_SSA_RC_INFERENCE))) {
211
0
        goto add_op1_def;
212
0
      }
213
0
      break;
214
0
    case ZEND_UNSET_CV:
215
0
      goto add_op1_def;
216
0
    case ZEND_VERIFY_RETURN_TYPE:
217
0
      if (opline->op1_type & (IS_TMP_VAR|IS_VAR|IS_CV)) {
218
0
        goto add_op1_def;
219
0
      }
220
0
      break;
221
13
    case ZEND_FE_FETCH_R:
222
13
    case ZEND_FE_FETCH_RW:
223
#if 0
224
      /* This special case was handled above the switch */
225
      if (opline->op2_type != IS_CV) {
226
        op2_use = -1; /* not used */
227
      }
228
#endif
229
13
      zend_bitset_incl(def, EX_VAR_TO_NUM(opline->op2.var));
230
13
      break;
231
0
    case ZEND_BIND_LEXICAL:
232
0
      if ((opline->extended_value & ZEND_BIND_REF) || (build_flags & ZEND_SSA_RC_INFERENCE)) {
233
0
        zend_bitset_incl(def, EX_VAR_TO_NUM(opline->op2.var));
234
0
      }
235
0
      break;
236
18.9k
    default:
237
18.9k
      break;
238
20.9k
  }
239
240
20.9k
  if (opline->result_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
241
15.7k
    zend_bitset_incl(def, EX_VAR_TO_NUM(opline->result.var));
242
15.7k
  }
243
20.9k
}
244
/* }}} */
245
246
ZEND_API void zend_dfg_add_use_def_op(const zend_op_array *op_array, const zend_op *opline, uint32_t build_flags, zend_bitset use, zend_bitset def) /* {{{ */
247
0
{
248
0
  zend_dfg_add_use_def_op_impl(op_array, opline, build_flags, use, def);
249
0
}
250
/* }}} */
251
252
void zend_build_dfg(const zend_op_array *op_array, const zend_cfg *cfg, const zend_dfg *dfg, uint32_t build_flags) /* {{{ */
253
159
{
254
159
  uint32_t set_size = dfg->size;
255
159
  zend_basic_block *blocks = cfg->blocks;
256
159
  int blocks_count = cfg->blocks_count;
257
159
  zend_bitset tmp, def, use, in, out;
258
159
  int k;
259
159
  int j;
260
261
159
  tmp = dfg->tmp;
262
159
  def = dfg->def;
263
159
  use = dfg->use;
264
159
  in  = dfg->in;
265
159
  out = dfg->out;
266
267
  /* Collect "def" and "use" sets */
268
4.06k
  for (j = 0; j < blocks_count; j++) {
269
3.90k
    const zend_op *opline, *end;
270
3.90k
    zend_bitset b_use, b_def;
271
272
3.90k
    if ((blocks[j].flags & ZEND_BB_REACHABLE) == 0) {
273
0
      continue;
274
0
    }
275
276
3.90k
    opline = op_array->opcodes + blocks[j].start;
277
3.90k
    end = opline + blocks[j].len;
278
3.90k
    b_use = DFG_BITSET(use, set_size, j);
279
3.90k
    b_def = DFG_BITSET(def, set_size, j);
280
24.9k
    for (; opline < end; opline++) {
281
21.0k
      if (opline->opcode != ZEND_OP_DATA) {
282
20.9k
        zend_dfg_add_use_def_op_impl(op_array, opline, build_flags, b_use, b_def);
283
20.9k
      }
284
21.0k
    }
285
3.90k
  }
286
287
  /* Calculate "in" and "out" sets */
288
159
  {
289
159
    uint32_t worklist_len = zend_bitset_len(blocks_count);
290
159
    zend_bitset worklist;
291
159
    ALLOCA_FLAG(use_heap);
292
159
    worklist = ZEND_BITSET_ALLOCA(worklist_len, use_heap);
293
159
    memset(worklist, 0, worklist_len * ZEND_BITSET_ELM_SIZE);
294
4.06k
    for (j = 0; j < blocks_count; j++) {
295
3.90k
      zend_bitset_incl(worklist, j);
296
3.90k
    }
297
5.02k
    while (!zend_bitset_empty(worklist, worklist_len)) {
298
      /* We use the last block on the worklist, because predecessors tend to be located
299
       * before the succeeding block, so this converges faster. */
300
4.87k
      j = zend_bitset_last(worklist, worklist_len);
301
4.87k
      zend_bitset_excl(worklist, j);
302
303
4.87k
      if ((blocks[j].flags & ZEND_BB_REACHABLE) == 0) {
304
0
        continue;
305
0
      }
306
4.87k
      if (blocks[j].successors_count != 0) {
307
4.71k
        zend_bitset_copy(DFG_BITSET(out, set_size, j), DFG_BITSET(in, set_size, blocks[j].successors[0]), set_size);
308
7.00k
        for (k = 1; k < blocks[j].successors_count; k++) {
309
2.29k
          zend_bitset_union(DFG_BITSET(out, set_size, j), DFG_BITSET(in, set_size, blocks[j].successors[k]), set_size);
310
2.29k
        }
311
4.71k
      } else {
312
160
        zend_bitset_clear(DFG_BITSET(out, set_size, j), set_size);
313
160
      }
314
4.87k
      zend_bitset_union_with_difference(tmp, DFG_BITSET(use, set_size, j), DFG_BITSET(out, set_size, j), DFG_BITSET(def, set_size, j), set_size);
315
4.87k
      if (!zend_bitset_equal(DFG_BITSET(in, set_size, j), tmp, set_size)) {
316
4.46k
        zend_bitset_copy(DFG_BITSET(in, set_size, j), tmp, set_size);
317
318
        /* Add predecessors of changed block to worklist */
319
4.46k
        {
320
4.46k
          const int *predecessors = &cfg->predecessors[blocks[j].predecessor_offset];
321
10.9k
          for (k = 0; k < blocks[j].predecessors_count; k++) {
322
6.52k
            zend_bitset_incl(worklist, predecessors[k]);
323
6.52k
          }
324
4.46k
        }
325
4.46k
      }
326
4.87k
    }
327
328
    free_alloca(worklist, use_heap);
329
159
  }
330
159
}
331
/* }}} */