Coverage Report

Created: 2026-06-02 06:36

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