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_ssa.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine, SSA - Static Single Assignment Form                     |
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
   |          Nikita Popov <nikic@php.net>                                |
17
   +----------------------------------------------------------------------+
18
*/
19
20
#include "zend_compile.h"
21
#include "zend_dfg.h"
22
#include "zend_ssa.h"
23
#include "zend_dump.h"
24
#include "zend_inference.h"
25
#include "zend_worklist.h"
26
#include "Optimizer/zend_optimizer_internal.h"
27
28
0
static bool dominates(const zend_basic_block *blocks, int a, int b) {
29
0
  while (blocks[b].level > blocks[a].level) {
30
0
    b = blocks[b].idom;
31
0
  }
32
0
  return a == b;
33
0
}
34
35
static bool will_rejoin(
36
    const zend_cfg *cfg, const zend_dfg *dfg, const zend_basic_block *block,
37
9
    int other_successor, int exclude, int var) {
38
9
  int i;
39
27
  for (i = 0; i < block->predecessors_count; i++) {
40
18
    int predecessor = cfg->predecessors[block->predecessor_offset + i];
41
18
    if (predecessor == exclude) {
42
9
      continue;
43
9
    }
44
45
    /* The variable is changed in this predecessor,
46
     * so we will not rejoin with the original value. */
47
    // TODO: This should not be limited to the direct predecessor block.
48
9
    if (DFG_ISSET(dfg->def, dfg->size, predecessor, var)) {
49
9
      continue;
50
9
    }
51
52
    /* The other successor dominates this predecessor,
53
     * so we will get the original value from it. */
54
0
    if (dominates(cfg->blocks, other_successor, predecessor)) {
55
0
      return true;
56
0
    }
57
0
  }
58
9
  return false;
59
9
}
60
61
static bool needs_pi(const zend_op_array *op_array, const zend_dfg *dfg, const zend_ssa *ssa, int from, int to, int var) /* {{{ */
62
312
{
63
312
  const zend_basic_block *from_block, *to_block;
64
312
  int other_successor;
65
66
312
  if (!DFG_ISSET(dfg->in, dfg->size, to, var)) {
67
    /* Variable is not live, certainly won't benefit from pi */
68
0
    return false;
69
0
  }
70
71
  /* Make sure that both successors of the from block aren't the same. Pi nodes are associated
72
   * with predecessor blocks, so we can't distinguish which edge the pi belongs to. */
73
312
  from_block = &ssa->cfg.blocks[from];
74
312
  ZEND_ASSERT(from_block->successors_count == 2);
75
312
  if (from_block->successors[0] == from_block->successors[1]) {
76
0
    return false;
77
0
  }
78
79
312
  to_block = &ssa->cfg.blocks[to];
80
312
  if (to_block->predecessors_count == 1) {
81
    /* Always place pi if one predecessor (an if branch) */
82
303
    return true;
83
303
  }
84
85
  /* Check whether we will rejoin with the original value coming from the other successor,
86
   * in which case the pi node will not have an effect. */
87
9
  other_successor = from_block->successors[0] == to
88
9
    ? from_block->successors[1] : from_block->successors[0];
89
9
  return !will_rejoin(&ssa->cfg, dfg, to_block, other_successor, from, var);
90
312
}
91
/* }}} */
92
93
static zend_ssa_phi *add_pi(
94
    zend_arena **arena, const zend_op_array *op_array, const zend_dfg *dfg, const zend_ssa *ssa,
95
    int from, int to, int var) /* {{{ */
96
312
{
97
312
  zend_ssa_phi *phi;
98
312
  if (!needs_pi(op_array, dfg, ssa, from, to, var)) {
99
0
    return NULL;
100
0
  }
101
102
312
  phi = zend_arena_calloc(arena, 1,
103
312
    ZEND_MM_ALIGNED_SIZE(sizeof(zend_ssa_phi)) +
104
312
    ZEND_MM_ALIGNED_SIZE(sizeof(int) * ssa->cfg.blocks[to].predecessors_count) +
105
312
    sizeof(void*) * ssa->cfg.blocks[to].predecessors_count);
106
312
  phi->sources = (int*)(((char*)phi) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_ssa_phi)));
107
312
  memset(phi->sources, 0xff, sizeof(int) * ssa->cfg.blocks[to].predecessors_count);
108
312
  phi->use_chains = (zend_ssa_phi**)(((char*)phi->sources) + ZEND_MM_ALIGNED_SIZE(sizeof(int) * ssa->cfg.blocks[to].predecessors_count));
109
110
312
  phi->pi = from;
111
312
  phi->var = var;
112
312
  phi->ssa_var = -1;
113
312
  phi->next = ssa->blocks[to].phis;
114
312
  ssa->blocks[to].phis = phi;
115
116
  /* Block "to" now defines "var" via the pi statement, so add it to the "def" set. Note that
117
   * this is not entirely accurate, because the pi is actually placed along the edge from->to.
118
   * If there is a back-edge to "to" this may result in non-minimal SSA form. */
119
312
  DFG_SET(dfg->def, dfg->size, to, var);
120
121
  /* If there are multiple predecessors in the target block, we need to place a phi there.
122
   * However this can (generally) not be expressed in terms of dominance frontiers, so place it
123
   * explicitly. dfg->use here really is dfg->phi, we're reusing the set. */
124
312
  if (ssa->cfg.blocks[to].predecessors_count > 1) {
125
9
    DFG_SET(dfg->use, dfg->size, to, var);
126
9
  }
127
128
312
  return phi;
129
312
}
130
/* }}} */
131
132
static void pi_range(
133
    zend_ssa_phi *phi, int min_var, int max_var, zend_long min, zend_long max,
134
    bool underflow, bool overflow, bool negative) /* {{{ */
135
80
{
136
80
  zend_ssa_range_constraint *constraint = &phi->constraint.range;
137
80
  constraint->min_var = min_var;
138
80
  constraint->max_var = max_var;
139
80
  constraint->min_ssa_var = -1;
140
80
  constraint->max_ssa_var = -1;
141
80
  constraint->range.min = min;
142
80
  constraint->range.max = max;
143
80
  constraint->range.underflow = underflow;
144
80
  constraint->range.overflow = overflow;
145
80
  constraint->negative = negative ? NEG_INIT : NEG_NONE;
146
80
  phi->has_range_constraint = true;
147
80
}
148
/* }}} */
149
150
0
static inline void pi_range_equals(zend_ssa_phi *phi, int var, zend_long val) {
151
0
  pi_range(phi, var, var, val, val, false, false, false);
152
0
}
153
0
static inline void pi_range_not_equals(zend_ssa_phi *phi, int var, zend_long val) {
154
0
  pi_range(phi, var, var, val, val, false, false, true);
155
0
}
156
40
static inline void pi_range_min(zend_ssa_phi *phi, int var, zend_long val) {
157
40
  pi_range(phi, var, -1, val, ZEND_LONG_MAX, false, true, false);
158
40
}
159
40
static inline void pi_range_max(zend_ssa_phi *phi, int var, zend_long val) {
160
40
  pi_range(phi, -1, var, ZEND_LONG_MIN, val, true, false, false);
161
40
}
162
163
232
static void pi_type_mask(zend_ssa_phi *phi, uint32_t type_mask) {
164
232
  phi->has_range_constraint = false;
165
232
  phi->constraint.type.ce = NULL;
166
232
  phi->constraint.type.type_mask = MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN;
167
232
  phi->constraint.type.type_mask |= type_mask;
168
232
  if (type_mask & MAY_BE_NULL) {
169
0
    phi->constraint.type.type_mask |= MAY_BE_UNDEF;
170
0
  }
171
232
}
172
232
static inline void pi_not_type_mask(zend_ssa_phi *phi, uint32_t type_mask) {
173
232
  uint32_t relevant = MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
174
232
  pi_type_mask(phi, ~type_mask & relevant);
175
232
}
176
0
static inline uint32_t mask_for_type_check(uint32_t type) {
177
0
  if (type & MAY_BE_ARRAY) {
178
0
    return type | (MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF);
179
0
  } else {
180
0
    return type;
181
0
  }
182
0
}
183
184
/* We can interpret $a + 5 == 0 as $a = 0 - 5, i.e. shift the adjustment to the other operand.
185
 * This negated adjustment is what is written into the "adjustment" parameter. */
186
static int find_adjusted_tmp_var(const zend_op_array *op_array, uint32_t build_flags, zend_op *opline, uint32_t var_num, zend_long *adjustment) /* {{{ */
187
0
{
188
0
  zend_op *op = opline;
189
0
  zval *zv;
190
191
0
  while (op != op_array->opcodes) {
192
0
    op--;
193
0
    if (op->result_type != IS_TMP_VAR || op->result.var != var_num) {
194
0
      continue;
195
0
    }
196
197
0
    if (op->opcode == ZEND_POST_DEC) {
198
0
      if (op->op1_type == IS_CV) {
199
0
        *adjustment = -1;
200
0
        return EX_VAR_TO_NUM(op->op1.var);
201
0
      }
202
0
    } else if (op->opcode == ZEND_POST_INC) {
203
0
      if (op->op1_type == IS_CV) {
204
0
        *adjustment = 1;
205
0
        return EX_VAR_TO_NUM(op->op1.var);
206
0
      }
207
0
    } else if (op->opcode == ZEND_ADD) {
208
0
      if (op->op1_type == IS_CV && op->op2_type == IS_CONST) {
209
0
        zv = CRT_CONSTANT_EX(op_array, op, op->op2);
210
0
        if (Z_TYPE_P(zv) == IS_LONG
211
0
         && Z_LVAL_P(zv) != ZEND_LONG_MIN) {
212
0
          *adjustment = -Z_LVAL_P(zv);
213
0
          return EX_VAR_TO_NUM(op->op1.var);
214
0
        }
215
0
      } else if (op->op2_type == IS_CV && op->op1_type == IS_CONST) {
216
0
        zv = CRT_CONSTANT_EX(op_array, op, op->op1);
217
0
        if (Z_TYPE_P(zv) == IS_LONG
218
0
         && Z_LVAL_P(zv) != ZEND_LONG_MIN) {
219
0
          *adjustment = -Z_LVAL_P(zv);
220
0
          return EX_VAR_TO_NUM(op->op2.var);
221
0
        }
222
0
      }
223
0
    } else if (op->opcode == ZEND_SUB) {
224
0
      if (op->op1_type == IS_CV && op->op2_type == IS_CONST) {
225
0
        zv = CRT_CONSTANT_EX(op_array, op, op->op2);
226
0
        if (Z_TYPE_P(zv) == IS_LONG) {
227
0
          *adjustment = Z_LVAL_P(zv);
228
0
          return EX_VAR_TO_NUM(op->op1.var);
229
0
        }
230
0
      }
231
0
    }
232
0
    break;
233
0
  }
234
0
  return -1;
235
0
}
236
/* }}} */
237
238
/* e-SSA construction: Pi placement (Pi is actually a Phi with single
239
 * source and constraint).
240
 * Order of Phis is important, Pis must be placed before Phis
241
 */
242
static void place_essa_pis(
243
    zend_arena **arena, const zend_script *script, const zend_op_array *op_array,
244
32
    uint32_t build_flags, const zend_ssa *ssa, const zend_dfg *dfg) /* {{{ */ {
245
32
  const zend_basic_block *blocks = ssa->cfg.blocks;
246
32
  int j, blocks_count = ssa->cfg.blocks_count;
247
823
  for (j = 0; j < blocks_count; j++) {
248
791
    zend_ssa_phi *pi;
249
791
    zend_op *opline = op_array->opcodes + blocks[j].start + blocks[j].len - 1;
250
791
    int bt; /* successor block number if a condition is true */
251
791
    int bf; /* successor block number if a condition is false */
252
253
791
    if ((blocks[j].flags & ZEND_BB_REACHABLE) == 0 || blocks[j].len == 0) {
254
0
      continue;
255
0
    }
256
    /* the last instruction of basic block is conditional branch,
257
     * based on comparison of CV(s)
258
     */
259
791
    switch (opline->opcode) {
260
0
      case ZEND_JMPZ:
261
0
        bf = blocks[j].successors[0];
262
0
        bt = blocks[j].successors[1];
263
0
        break;
264
61
      case ZEND_JMPNZ:
265
61
        bt = blocks[j].successors[0];
266
61
        bf = blocks[j].successors[1];
267
61
        break;
268
32
      case ZEND_COALESCE:
269
32
        if (opline->op1_type == IS_CV) {
270
0
          int var = EX_VAR_TO_NUM(opline->op1.var);
271
0
          if ((pi = add_pi(arena, op_array, dfg, ssa, j, blocks[j].successors[0], var))) {
272
0
            pi_not_type_mask(pi, MAY_BE_NULL);
273
0
          }
274
0
        }
275
32
        continue;
276
232
      case ZEND_JMP_NULL:
277
232
        if (opline->op1_type == IS_CV) {
278
232
          int var = EX_VAR_TO_NUM(opline->op1.var);
279
232
          if ((pi = add_pi(arena, op_array, dfg, ssa, j, blocks[j].successors[1], var))) {
280
232
            pi_not_type_mask(pi, MAY_BE_NULL);
281
232
          }
282
232
        }
283
232
        continue;
284
466
      default:
285
466
        continue;
286
791
    }
287
288
    /* The following patterns all inspect the opline directly before the JMPZ opcode.
289
     * Make sure that it is part of the same block, otherwise it might not be a dominating
290
     * assignment. */
291
61
    if (blocks[j].len == 1) {
292
0
      continue;
293
0
    }
294
295
61
    if (opline->op1_type == IS_TMP_VAR &&
296
61
        ((opline-1)->opcode == ZEND_IS_EQUAL ||
297
61
         (opline-1)->opcode == ZEND_IS_NOT_EQUAL ||
298
61
         (opline-1)->opcode == ZEND_IS_SMALLER ||
299
61
         (opline-1)->opcode == ZEND_IS_SMALLER_OR_EQUAL) &&
300
61
        opline->op1.var == (opline-1)->result.var) {
301
61
      int  var1 = -1;
302
61
      int  var2 = -1;
303
61
      zend_long val1 = 0;
304
61
      zend_long val2 = 0;
305
//      long val = 0;
306
307
61
      if ((opline-1)->op1_type == IS_CV) {
308
61
        var1 = EX_VAR_TO_NUM((opline-1)->op1.var);
309
61
      } else if ((opline-1)->op1_type == IS_TMP_VAR) {
310
0
        var1 = find_adjusted_tmp_var(
311
0
          op_array, build_flags, opline, (opline-1)->op1.var, &val2);
312
0
      }
313
314
61
      if ((opline-1)->op2_type == IS_CV) {
315
0
        var2 = EX_VAR_TO_NUM((opline-1)->op2.var);
316
61
      } else if ((opline-1)->op2_type == IS_TMP_VAR) {
317
0
        var2 = find_adjusted_tmp_var(
318
0
          op_array, build_flags, opline, (opline-1)->op2.var, &val1);
319
0
      }
320
321
61
      if (var1 >= 0 && var2 >= 0) {
322
0
        if (!zend_sub_will_overflow(val1, val2) && !zend_sub_will_overflow(val2, val1)) {
323
0
          zend_long tmp = val1;
324
0
          val1 -= val2;
325
0
          val2 -= tmp;
326
0
        } else {
327
0
          var1 = -1;
328
0
          var2 = -1;
329
0
        }
330
61
      } else if (var1 >= 0 && var2 < 0) {
331
61
        zend_long add_val2 = 0;
332
61
        if ((opline-1)->op2_type == IS_CONST) {
333
61
          zval *zv = CRT_CONSTANT_EX(op_array, (opline-1), (opline-1)->op2);
334
335
61
          if (Z_TYPE_P(zv) == IS_LONG) {
336
40
            add_val2 = Z_LVAL_P(zv);
337
40
          } else {
338
21
            var1 = -1;
339
21
          }
340
61
        } else {
341
0
          var1 = -1;
342
0
        }
343
61
        if (!zend_add_will_overflow(val2, add_val2)) {
344
61
          val2 += add_val2;
345
61
        } else {
346
0
          var1 = -1;
347
0
        }
348
61
      } else if (var1 < 0 && var2 >= 0) {
349
0
        zend_long add_val1 = 0;
350
0
        if ((opline-1)->op1_type == IS_CONST) {
351
0
          zval *zv = CRT_CONSTANT_EX(op_array, (opline-1), (opline-1)->op1);
352
0
          if (Z_TYPE_P(zv) == IS_LONG) {
353
0
            add_val1 = Z_LVAL_P(CRT_CONSTANT_EX(op_array, (opline-1), (opline-1)->op1));
354
0
          } else {
355
0
            var2 = -1;
356
0
          }
357
0
        } else {
358
0
          var2 = -1;
359
0
        }
360
0
        if (!zend_add_will_overflow(val1, add_val1)) {
361
0
          val1 += add_val1;
362
0
        } else {
363
0
          var2 = -1;
364
0
        }
365
0
      }
366
367
61
      if (var1 >= 0) {
368
40
        if ((opline-1)->opcode == ZEND_IS_EQUAL) {
369
0
          if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var1))) {
370
0
            pi_range_equals(pi, var2, val2);
371
0
          }
372
0
          if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var1))) {
373
0
            pi_range_not_equals(pi, var2, val2);
374
0
          }
375
40
        } else if ((opline-1)->opcode == ZEND_IS_NOT_EQUAL) {
376
0
          if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var1))) {
377
0
            pi_range_equals(pi, var2, val2);
378
0
          }
379
0
          if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var1))) {
380
0
            pi_range_not_equals(pi, var2, val2);
381
0
          }
382
40
        } else if ((opline-1)->opcode == ZEND_IS_SMALLER) {
383
0
          if (val2 > ZEND_LONG_MIN) {
384
0
            if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var1))) {
385
0
              pi_range_max(pi, var2, val2-1);
386
0
            }
387
0
          }
388
0
          if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var1))) {
389
0
            pi_range_min(pi, var2, val2);
390
0
          }
391
40
        } else if ((opline-1)->opcode == ZEND_IS_SMALLER_OR_EQUAL) {
392
40
          if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var1))) {
393
40
            pi_range_max(pi, var2, val2);
394
40
          }
395
40
          if (val2 < ZEND_LONG_MAX) {
396
40
            if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var1))) {
397
40
              pi_range_min(pi, var2, val2+1);
398
40
            }
399
40
          }
400
40
        }
401
40
      }
402
61
      if (var2 >= 0) {
403
0
        if((opline-1)->opcode == ZEND_IS_EQUAL) {
404
0
          if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var2))) {
405
0
            pi_range_equals(pi, var1, val1);
406
0
          }
407
0
          if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var2))) {
408
0
            pi_range_not_equals(pi, var1, val1);
409
0
          }
410
0
        } else if ((opline-1)->opcode == ZEND_IS_NOT_EQUAL) {
411
0
          if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var2))) {
412
0
            pi_range_equals(pi, var1, val1);
413
0
          }
414
0
          if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var2))) {
415
0
            pi_range_not_equals(pi, var1, val1);
416
0
          }
417
0
        } else if ((opline-1)->opcode == ZEND_IS_SMALLER) {
418
0
          if (val1 < ZEND_LONG_MAX) {
419
0
            if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var2))) {
420
0
              pi_range_min(pi, var1, val1+1);
421
0
            }
422
0
          }
423
0
          if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var2))) {
424
0
            pi_range_max(pi, var1, val1);
425
0
          }
426
0
        } else if ((opline-1)->opcode == ZEND_IS_SMALLER_OR_EQUAL) {
427
0
          if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var2))) {
428
0
            pi_range_min(pi, var1, val1);
429
0
          }
430
0
          if (val1 > ZEND_LONG_MIN) {
431
0
            if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var2))) {
432
0
              pi_range_max(pi, var1, val1-1);
433
0
            }
434
0
          }
435
0
        }
436
0
      }
437
61
    } else if (opline->op1_type == IS_TMP_VAR &&
438
0
               ((opline-1)->opcode == ZEND_POST_INC ||
439
0
                (opline-1)->opcode == ZEND_POST_DEC) &&
440
0
               opline->op1.var == (opline-1)->result.var &&
441
0
               (opline-1)->op1_type == IS_CV) {
442
0
      int var = EX_VAR_TO_NUM((opline-1)->op1.var);
443
444
0
      if ((opline-1)->opcode == ZEND_POST_DEC) {
445
0
        if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var))) {
446
0
          pi_range_equals(pi, -1, -1);
447
0
        }
448
0
        if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var))) {
449
0
          pi_range_not_equals(pi, -1, -1);
450
0
        }
451
0
      } else if ((opline-1)->opcode == ZEND_POST_INC) {
452
0
        if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var))) {
453
0
          pi_range_equals(pi, -1, 1);
454
0
        }
455
0
        if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var))) {
456
0
          pi_range_not_equals(pi, -1, 1);
457
0
        }
458
0
      }
459
0
    } else if (opline->op1_type == IS_TMP_VAR &&
460
0
               ((opline-1)->opcode == ZEND_PRE_INC ||
461
0
                (opline-1)->opcode == ZEND_PRE_DEC) &&
462
0
               opline->op1.var == (opline-1)->result.var &&
463
0
               (opline-1)->op1_type == IS_CV) {
464
0
      int var = EX_VAR_TO_NUM((opline-1)->op1.var);
465
466
0
      if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var))) {
467
0
        pi_range_equals(pi, -1, 0);
468
0
      }
469
      /* speculative */
470
0
      if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var))) {
471
0
        pi_range_not_equals(pi, -1, 0);
472
0
      }
473
0
    } else if (opline->op1_type == IS_TMP_VAR && (opline-1)->opcode == ZEND_TYPE_CHECK &&
474
0
           opline->op1.var == (opline-1)->result.var && (opline-1)->op1_type == IS_CV) {
475
0
      int var = EX_VAR_TO_NUM((opline-1)->op1.var);
476
0
      uint32_t type = (opline-1)->extended_value;
477
0
      if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var))) {
478
0
        pi_type_mask(pi, mask_for_type_check(type));
479
0
      }
480
0
      if (type != MAY_BE_RESOURCE) {
481
        /* is_resource() may return false for closed resources */
482
0
        if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var))) {
483
0
          pi_not_type_mask(pi, mask_for_type_check(type));
484
0
        }
485
0
      }
486
0
    } else if (opline->op1_type == IS_TMP_VAR &&
487
0
           ((opline-1)->opcode == ZEND_IS_IDENTICAL
488
0
          || (opline-1)->opcode == ZEND_IS_NOT_IDENTICAL) &&
489
0
           opline->op1.var == (opline-1)->result.var) {
490
0
      int var;
491
0
      zval *val;
492
0
      uint32_t type_mask;
493
0
      if ((opline-1)->op1_type == IS_CV && (opline-1)->op2_type == IS_CONST) {
494
0
        var = EX_VAR_TO_NUM((opline-1)->op1.var);
495
0
        val = CRT_CONSTANT_EX(op_array, (opline-1), (opline-1)->op2);
496
0
      } else if ((opline-1)->op1_type == IS_CONST && (opline-1)->op2_type == IS_CV) {
497
0
        var = EX_VAR_TO_NUM((opline-1)->op2.var);
498
0
        val = CRT_CONSTANT_EX(op_array, (opline-1), (opline-1)->op1);
499
0
      } else {
500
0
        continue;
501
0
      }
502
503
      /* We're interested in === null/true/false comparisons here, because they eliminate
504
       * a type in the false-branch. Other === VAL comparisons are unlikely to be useful. */
505
0
      if (Z_TYPE_P(val) != IS_NULL && Z_TYPE_P(val) != IS_TRUE && Z_TYPE_P(val) != IS_FALSE) {
506
0
        continue;
507
0
      }
508
509
0
      type_mask = _const_op_type(val);
510
0
      if ((opline-1)->opcode == ZEND_IS_IDENTICAL) {
511
0
        if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var))) {
512
0
          pi_type_mask(pi, type_mask);
513
0
        }
514
0
        if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var))) {
515
0
          pi_not_type_mask(pi, type_mask);
516
0
        }
517
0
      } else {
518
0
        if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var))) {
519
0
          pi_type_mask(pi, type_mask);
520
0
        }
521
0
        if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var))) {
522
0
          pi_not_type_mask(pi, type_mask);
523
0
        }
524
0
      }
525
0
    } else if (opline->op1_type == IS_TMP_VAR && (opline-1)->opcode == ZEND_INSTANCEOF &&
526
0
           opline->op1.var == (opline-1)->result.var && (opline-1)->op1_type == IS_CV &&
527
0
           (opline-1)->op2_type == IS_CONST) {
528
0
      int var = EX_VAR_TO_NUM((opline-1)->op1.var);
529
0
      zend_string *lcname = Z_STR_P(CRT_CONSTANT_EX(op_array, (opline-1), (opline-1)->op2) + 1);
530
0
      zend_class_entry *ce = zend_optimizer_get_class_entry(script, op_array, lcname);
531
0
      if (!ce) {
532
0
        continue;
533
0
      }
534
535
0
      if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var))) {
536
0
        pi_type_mask(pi, MAY_BE_OBJECT);
537
0
        pi->constraint.type.ce = ce;
538
0
      }
539
0
    }
540
61
  }
541
32
}
542
/* }}} */
543
544
static zend_always_inline int _zend_ssa_rename_op(const zend_op_array *op_array, const zend_op *opline, uint32_t k, uint32_t build_flags, int ssa_vars_count, zend_ssa_op *ssa_ops, int *var) /* {{{ */
545
3.96k
{
546
3.96k
  const zend_op *next;
547
548
3.96k
  if (opline->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
549
3.53k
    ssa_ops[k].op1_use = var[EX_VAR_TO_NUM(opline->op1.var)];
550
    //USE_SSA_VAR(op_array->last_var + opline->op1.var)
551
3.53k
  }
552
3.96k
  if (opline->op2_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
553
1.28k
    ssa_ops[k].op2_use = var[EX_VAR_TO_NUM(opline->op2.var)];
554
    //USE_SSA_VAR(op_array->last_var + opline->op2.var)
555
1.28k
  }
556
3.96k
  if ((build_flags & ZEND_SSA_USE_CV_RESULTS)
557
0
   && opline->result_type == IS_CV
558
0
   && opline->opcode != ZEND_RECV) {
559
0
    ssa_ops[k].result_use = var[EX_VAR_TO_NUM(opline->result.var)];
560
    //USE_SSA_VAR(op_array->last_var + opline->result.var)
561
0
  }
562
563
3.96k
  switch (opline->opcode) {
564
104
    case ZEND_ASSIGN:
565
104
      if ((build_flags & ZEND_SSA_RC_INFERENCE) && opline->op2_type == IS_CV) {
566
0
        ssa_ops[k].op2_def = ssa_vars_count;
567
0
        var[EX_VAR_TO_NUM(opline->op2.var)] = ssa_vars_count;
568
0
        ssa_vars_count++;
569
        //NEW_SSA_VAR(opline->op2.var)
570
0
      }
571
104
      if (opline->op1_type == IS_CV) {
572
211
add_op1_def:
573
211
        ssa_ops[k].op1_def = ssa_vars_count;
574
211
        var[EX_VAR_TO_NUM(opline->op1.var)] = ssa_vars_count;
575
211
        ssa_vars_count++;
576
        //NEW_SSA_VAR(opline->op1.var)
577
211
      }
578
211
      break;
579
211
    case ZEND_ASSIGN_REF:
580
3
      if (opline->op2_type == IS_CV) {
581
3
        ssa_ops[k].op2_def = ssa_vars_count;
582
3
        var[EX_VAR_TO_NUM(opline->op2.var)] = ssa_vars_count;
583
3
        ssa_vars_count++;
584
        //NEW_SSA_VAR(opline->op2.var)
585
3
      }
586
3
      if (opline->op1_type == IS_CV) {
587
3
        goto add_op1_def;
588
3
      }
589
0
      break;
590
3
    case ZEND_ASSIGN_DIM:
591
3
    case ZEND_ASSIGN_OBJ:
592
3
      next = opline + 1;
593
3
      if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
594
0
        ssa_ops[k + 1].op1_use = var[EX_VAR_TO_NUM(next->op1.var)];
595
        //USE_SSA_VAR(op_array->last_var + next->op1.var);
596
0
        if (build_flags & ZEND_SSA_RC_INFERENCE && next->op1_type == IS_CV) {
597
0
          ssa_ops[k + 1].op1_def = ssa_vars_count;
598
0
          var[EX_VAR_TO_NUM(next->op1.var)] = ssa_vars_count;
599
0
          ssa_vars_count++;
600
          //NEW_SSA_VAR(next->op1.var)
601
0
        }
602
0
      }
603
3
      if (opline->op1_type == IS_CV) {
604
0
        ssa_ops[k].op1_def = ssa_vars_count;
605
0
        var[EX_VAR_TO_NUM(opline->op1.var)] = ssa_vars_count;
606
0
        ssa_vars_count++;
607
        //NEW_SSA_VAR(opline->op1.var)
608
0
      }
609
3
      break;
610
0
    case ZEND_ASSIGN_OBJ_REF:
611
0
      if ((build_flags & ZEND_SSA_RC_INFERENCE) && opline->op1_type == IS_CV) {
612
0
        ssa_ops[k].op1_def = ssa_vars_count;
613
0
        var[EX_VAR_TO_NUM(opline->op1.var)] = ssa_vars_count;
614
0
        ssa_vars_count++;
615
        //NEW_SSA_VAR(opline->op1.var)
616
0
      }
617
0
      next = opline + 1;
618
0
      if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
619
0
        ssa_ops[k + 1].op1_use = var[EX_VAR_TO_NUM(next->op1.var)];
620
        //USE_SSA_VAR(op_array->last_var + next->op1.var);
621
0
        if (next->op1_type == IS_CV) {
622
0
          ssa_ops[k + 1].op1_def = ssa_vars_count;
623
0
          var[EX_VAR_TO_NUM(next->op1.var)] = ssa_vars_count;
624
0
          ssa_vars_count++;
625
          //NEW_SSA_VAR(next->op1.var)
626
0
        }
627
0
      }
628
0
      break;
629
0
    case ZEND_ASSIGN_STATIC_PROP:
630
0
      next = opline + 1;
631
0
      if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
632
0
        ssa_ops[k + 1].op1_use = var[EX_VAR_TO_NUM(next->op1.var)];
633
        //USE_SSA_VAR(op_array->last_var + next->op1.var);
634
0
        if ((build_flags & ZEND_SSA_RC_INFERENCE) && next->op1_type == IS_CV) {
635
0
          ssa_ops[k + 1].op1_def = ssa_vars_count;
636
0
          var[EX_VAR_TO_NUM(next->op1.var)] = ssa_vars_count;
637
0
          ssa_vars_count++;
638
          //NEW_SSA_VAR(next->op1.var)
639
0
        }
640
0
      }
641
0
      break;
642
0
    case ZEND_ASSIGN_STATIC_PROP_REF:
643
0
      next = opline + 1;
644
0
      if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
645
0
        ssa_ops[k + 1].op1_use = var[EX_VAR_TO_NUM(next->op1.var)];
646
        //USE_SSA_VAR(op_array->last_var + next->op1.var);
647
0
        if (next->op1_type == IS_CV) {
648
0
          ssa_ops[k + 1].op1_def = ssa_vars_count;
649
0
          var[EX_VAR_TO_NUM(next->op1.var)] = ssa_vars_count;
650
0
          ssa_vars_count++;
651
          //NEW_SSA_VAR(next->op1.var)
652
0
        }
653
0
      }
654
0
      break;
655
0
    case ZEND_ASSIGN_STATIC_PROP_OP:
656
0
      next = opline + 1;
657
0
      if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
658
0
        ssa_ops[k + 1].op1_use = var[EX_VAR_TO_NUM(next->op1.var)];
659
        //USE_SSA_VAR(op_array->last_var + next->op1.var);
660
0
      }
661
0
      break;
662
0
    case ZEND_ASSIGN_DIM_OP:
663
0
    case ZEND_ASSIGN_OBJ_OP:
664
0
      if (opline->op1_type == IS_CV) {
665
0
        ssa_ops[k].op1_def = ssa_vars_count;
666
0
        var[EX_VAR_TO_NUM(opline->op1.var)] = ssa_vars_count;
667
0
        ssa_vars_count++;
668
        //NEW_SSA_VAR(opline->op1.var)
669
0
      }
670
0
      next = opline + 1;
671
0
      if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
672
0
        ssa_ops[k + 1].op1_use = var[EX_VAR_TO_NUM(next->op1.var)];
673
        //USE_SSA_VAR(op_array->last_var + next->op1.var);
674
0
      }
675
0
      break;
676
25
    case ZEND_ASSIGN_OP:
677
56
    case ZEND_PRE_INC:
678
56
    case ZEND_PRE_DEC:
679
67
    case ZEND_POST_INC:
680
67
    case ZEND_POST_DEC:
681
67
    case ZEND_BIND_GLOBAL:
682
104
    case ZEND_BIND_STATIC:
683
104
    case ZEND_BIND_INIT_STATIC_OR_JMP:
684
104
    case ZEND_SEND_VAR_NO_REF:
685
104
    case ZEND_SEND_VAR_NO_REF_EX:
686
104
    case ZEND_SEND_VAR_EX:
687
104
    case ZEND_SEND_FUNC_ARG:
688
104
    case ZEND_SEND_REF:
689
104
    case ZEND_SEND_UNPACK:
690
104
    case ZEND_FE_RESET_RW:
691
104
    case ZEND_MAKE_REF:
692
104
    case ZEND_PRE_INC_OBJ:
693
104
    case ZEND_PRE_DEC_OBJ:
694
104
    case ZEND_POST_INC_OBJ:
695
104
    case ZEND_POST_DEC_OBJ:
696
104
    case ZEND_UNSET_DIM:
697
104
    case ZEND_UNSET_OBJ:
698
104
    case ZEND_FETCH_DIM_W:
699
104
    case ZEND_FETCH_DIM_RW:
700
104
    case ZEND_FETCH_DIM_FUNC_ARG:
701
104
    case ZEND_FETCH_DIM_UNSET:
702
104
    case ZEND_FETCH_LIST_W:
703
104
      if (opline->op1_type == IS_CV) {
704
104
        goto add_op1_def;
705
104
      }
706
0
      break;
707
38
    case ZEND_SEND_VAR:
708
38
    case ZEND_CAST:
709
82
    case ZEND_QM_ASSIGN:
710
94
    case ZEND_JMP_SET:
711
126
    case ZEND_COALESCE:
712
134
    case ZEND_FE_RESET_R:
713
134
      if ((build_flags & ZEND_SSA_RC_INFERENCE) && opline->op1_type == IS_CV) {
714
0
        goto add_op1_def;
715
0
      }
716
134
      break;
717
134
    case ZEND_ADD_ARRAY_UNPACK:
718
0
      ssa_ops[k].result_use = var[EX_VAR_TO_NUM(opline->result.var)];
719
0
      break;
720
5
    case ZEND_ADD_ARRAY_ELEMENT:
721
5
      ssa_ops[k].result_use = var[EX_VAR_TO_NUM(opline->result.var)];
722
5
      ZEND_FALLTHROUGH;
723
6
    case ZEND_INIT_ARRAY:
724
6
      if (((build_flags & ZEND_SSA_RC_INFERENCE)
725
6
            || (opline->extended_value & ZEND_ARRAY_ELEMENT_REF))
726
0
          && opline->op1_type == IS_CV) {
727
0
        goto add_op1_def;
728
0
      }
729
6
      break;
730
6
    case ZEND_YIELD:
731
0
      if (opline->op1_type == IS_CV
732
0
          && ((op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)
733
0
            || (build_flags & ZEND_SSA_RC_INFERENCE))) {
734
0
        goto add_op1_def;
735
0
      }
736
0
      break;
737
0
    case ZEND_UNSET_CV:
738
0
      goto add_op1_def;
739
0
    case ZEND_VERIFY_RETURN_TYPE:
740
0
      if (opline->op1_type & (IS_TMP_VAR|IS_VAR|IS_CV)) {
741
0
        goto add_op1_def;
742
0
      }
743
0
      break;
744
8
    case ZEND_FE_FETCH_R:
745
8
    case ZEND_FE_FETCH_RW:
746
8
      if (opline->op2_type != IS_CV) {
747
0
        ssa_ops[k].op2_use = -1; /* not used */
748
0
      }
749
8
      ssa_ops[k].op2_def = ssa_vars_count;
750
8
      var[EX_VAR_TO_NUM(opline->op2.var)] = ssa_vars_count;
751
8
      ssa_vars_count++;
752
      //NEW_SSA_VAR(opline->op2.var)
753
8
      break;
754
37
    case ZEND_BIND_LEXICAL:
755
37
      if ((opline->extended_value & ZEND_BIND_REF) || (build_flags & ZEND_SSA_RC_INFERENCE)) {
756
0
        ssa_ops[k].op2_def = ssa_vars_count;
757
0
        var[EX_VAR_TO_NUM(opline->op2.var)] = ssa_vars_count;
758
0
        ssa_vars_count++;
759
        //NEW_SSA_VAR(opline->op2.var)
760
0
      }
761
37
      break;
762
0
    case ZEND_COPY_TMP:
763
0
      if (build_flags & ZEND_SSA_RC_INFERENCE) {
764
0
        ssa_ops[k].op1_def = ssa_vars_count;
765
0
        var[EX_VAR_TO_NUM(opline->op1.var)] = ssa_vars_count;
766
0
        ssa_vars_count++;
767
        //NEW_SSA_VAR(opline->op1.var)
768
0
      }
769
0
      break;
770
0
    case ZEND_FRAMELESS_ICALL_1:
771
0
    case ZEND_FRAMELESS_ICALL_2:
772
0
    case ZEND_FRAMELESS_ICALL_3: {
773
0
      if ((build_flags & ZEND_SSA_RC_INFERENCE) && opline->op1_type == IS_CV) {
774
0
        ssa_ops[k].op1_def = ssa_vars_count;
775
0
        var[EX_VAR_TO_NUM(opline->op1.var)] = ssa_vars_count;
776
0
        ssa_vars_count++;
777
        //NEW_SSA_VAR(opline->op1.var)
778
0
      }
779
0
      if ((build_flags & ZEND_SSA_RC_INFERENCE) && opline->op2_type == IS_CV) {
780
0
        ssa_ops[k].op2_def = ssa_vars_count;
781
0
        var[EX_VAR_TO_NUM(opline->op2.var)] = ssa_vars_count;
782
0
        ssa_vars_count++;
783
        //NEW_SSA_VAR(opline->op2.var)
784
0
      }
785
0
      if (opline->opcode == ZEND_FRAMELESS_ICALL_3) {
786
0
        next = opline + 1;
787
0
        if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
788
0
          ssa_ops[k + 1].op1_use = var[EX_VAR_TO_NUM(next->op1.var)];
789
          //USE_SSA_VAR(op_array->last_var + next->op1.var);
790
0
          if ((build_flags & ZEND_SSA_RC_INFERENCE) && next->op1_type == IS_CV) {
791
0
            ssa_ops[k + 1].op1_def = ssa_vars_count;
792
0
            var[EX_VAR_TO_NUM(next->op1.var)] = ssa_vars_count;
793
0
            ssa_vars_count++;
794
            //NEW_SSA_VAR(next->op1.var)
795
0
          }
796
0
        }
797
0
      }
798
0
    }
799
3.56k
    default:
800
3.56k
      break;
801
3.96k
  }
802
803
3.96k
  if (opline->result_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
804
3.32k
    ssa_ops[k].result_def = ssa_vars_count;
805
3.32k
    var[EX_VAR_TO_NUM(opline->result.var)] = ssa_vars_count;
806
3.32k
    ssa_vars_count++;
807
    //NEW_SSA_VAR(op_array->last_var + opline->result.var)
808
3.32k
  }
809
810
3.96k
  return ssa_vars_count;
811
3.96k
}
812
/* }}} */
813
814
ZEND_API int zend_ssa_rename_op(const zend_op_array *op_array, const zend_op *opline, uint32_t k, uint32_t build_flags, int ssa_vars_count, zend_ssa_op *ssa_ops, int *var) /* {{{ */
815
0
{
816
0
  return _zend_ssa_rename_op(op_array, opline, k, build_flags, ssa_vars_count, ssa_ops, var);
817
0
}
818
/* }}} */
819
820
static void zend_ssa_rename_in_block(const zend_op_array *op_array, uint32_t build_flags, zend_ssa *ssa, int *var, int n) /* {{{ */
821
791
{
822
791
  const zend_basic_block *blocks = ssa->cfg.blocks;
823
791
  const zend_ssa_block *ssa_blocks = ssa->blocks;
824
791
  zend_ssa_op *ssa_ops = ssa->ops;
825
791
  int ssa_vars_count = ssa->vars_count;
826
791
  int i, j;
827
791
  const zend_op *opline, *end;
828
829
791
  if (ssa_blocks[n].phis) {
830
657
    zend_ssa_phi *phi = ssa_blocks[n].phis;
831
912
    do {
832
912
      if (phi->ssa_var < 0) {
833
609
        phi->ssa_var = ssa_vars_count;
834
609
        var[phi->var] = ssa_vars_count;
835
609
        ssa_vars_count++;
836
609
      } else {
837
303
        var[phi->var] = phi->ssa_var;
838
303
      }
839
912
      phi = phi->next;
840
912
    } while (phi);
841
657
  }
842
843
791
  opline = op_array->opcodes + blocks[n].start;
844
791
  end = opline + blocks[n].len;
845
4.75k
  for (; opline < end; opline++) {
846
3.96k
    uint32_t k = opline - op_array->opcodes;
847
3.96k
    if (opline->opcode != ZEND_OP_DATA) {
848
3.96k
      ssa_vars_count = _zend_ssa_rename_op(op_array, opline, k, build_flags, ssa_vars_count, ssa_ops, var);
849
3.96k
    }
850
3.96k
  }
851
852
791
  const zend_ssa_op *fe_fetch_ssa_op = blocks[n].len != 0
853
791
      && ((end-1)->opcode == ZEND_FE_FETCH_R || (end-1)->opcode == ZEND_FE_FETCH_RW)
854
8
      && (end-1)->op2_type == IS_CV
855
791
    ? &ssa_ops[blocks[n].start + blocks[n].len - 1] : NULL;
856
1.90k
  for (i = 0; i < blocks[n].successors_count; i++) {
857
1.11k
    int succ = blocks[n].successors[i];
858
1.11k
    zend_ssa_phi *p;
859
2.63k
    for (p = ssa_blocks[succ].phis; p; p = p->next) {
860
1.52k
      if (p->pi == n) {
861
        /* e-SSA Pi */
862
312
        if (p->has_range_constraint) {
863
80
          if (p->constraint.range.min_var >= 0) {
864
0
            p->constraint.range.min_ssa_var = var[p->constraint.range.min_var];
865
0
          }
866
80
          if (p->constraint.range.max_var >= 0) {
867
0
            p->constraint.range.max_ssa_var = var[p->constraint.range.max_var];
868
0
          }
869
80
        }
870
633
        for (j = 0; j < blocks[succ].predecessors_count; j++) {
871
321
          p->sources[j] = var[p->var];
872
321
        }
873
312
        if (p->ssa_var < 0) {
874
303
          p->ssa_var = ssa_vars_count;
875
303
          ssa_vars_count++;
876
303
        }
877
1.20k
      } else if (p->pi < 0) {
878
        /* Normal Phi */
879
1.80k
        for (j = 0; j < blocks[succ].predecessors_count; j++)
880
1.80k
          if (ssa->cfg.predecessors[blocks[succ].predecessor_offset + j] == n) {
881
1.20k
            break;
882
1.20k
          }
883
1.20k
        ZEND_ASSERT(j < blocks[succ].predecessors_count);
884
1.20k
        p->sources[j] = var[p->var];
885
1.20k
        if (fe_fetch_ssa_op && i == 0 && p->sources[j] == fe_fetch_ssa_op->op2_def) {
886
          /* On the exit edge of an FE_FETCH, use the pre-modification value instead. */
887
4
          p->sources[j] = fe_fetch_ssa_op->op2_use;
888
4
        }
889
1.20k
      }
890
1.52k
    }
891
1.43k
    for (p = ssa_blocks[succ].phis; p && (p->pi >= 0); p = p->next) {
892
321
      if (p->pi == n) {
893
312
        zend_ssa_phi *q = p->next;
894
321
        while (q) {
895
9
          if (q->pi < 0 && q->var == p->var) {
896
18
            for (j = 0; j < blocks[succ].predecessors_count; j++) {
897
18
              if (ssa->cfg.predecessors[blocks[succ].predecessor_offset + j] == n) {
898
9
                break;
899
9
              }
900
18
            }
901
9
            ZEND_ASSERT(j < blocks[succ].predecessors_count);
902
9
            q->sources[j] = p->ssa_var;
903
9
          }
904
9
          q = q->next;
905
9
        }
906
312
      }
907
321
    }
908
1.11k
  }
909
910
791
  ssa->vars_count = ssa_vars_count;
911
791
}
912
/* }}} */
913
914
static zend_result zend_ssa_rename(const zend_op_array *op_array, uint32_t build_flags, zend_ssa *ssa, int *var, int n)
915
32
{
916
  /* The worklist contains block numbers, encoded as positive or negative value.
917
   * Positive values indicate that the variable rename still needs to happen for the block.
918
   * Negative values indicate the variable rename was done and all children were handled too.
919
   * In that case, we will clean up.
920
   * Because block 0 is valid, we bias the block numbers by adding 1 such that we can distinguish
921
   * positive and negative values in all cases. */
922
32
  zend_worklist_stack work;
923
32
  ALLOCA_FLAG(work_use_heap);
924
32
  ZEND_WORKLIST_STACK_ALLOCA(&work, ssa->cfg.blocks_count, work_use_heap);
925
32
  zend_worklist_stack_push(&work, n + 1);
926
927
  /* This is used to backtrack the right version of the renamed variables to use. */
928
32
  ALLOCA_FLAG(save_vars_use_heap);
929
32
  unsigned int save_vars_top = 0;
930
32
  int **save_vars = do_alloca(sizeof(int *) * (ssa->cfg.blocks_count + 1), save_vars_use_heap);
931
32
  save_vars[0] = var;
932
933
1.24k
  while (work.len) {
934
1.21k
    n = zend_worklist_stack_pop(&work);
935
936
    /* Enter state: perform SSA variable rename */
937
1.21k
    if (n > 0) {
938
791
      n--;
939
940
      // FIXME: Can we optimize this copying out in some cases?
941
791
      int *new_var;
942
791
      if (ssa->cfg.blocks[n].next_child >= 0) {
943
340
        new_var = emalloc(sizeof(int) * (op_array->last_var + op_array->T));
944
340
        memcpy(new_var, save_vars[save_vars_top], sizeof(int) * (op_array->last_var + op_array->T));
945
340
        save_vars[++save_vars_top] = new_var;
946
451
      } else {
947
451
        new_var = save_vars[save_vars_top];
948
451
      }
949
950
791
      zend_ssa_rename_in_block(op_array, build_flags, ssa, new_var, n);
951
952
791
      int j = ssa->cfg.blocks[n].children;
953
791
      if (j >= 0) {
954
        /* Push backtrack state */
955
419
        zend_worklist_stack_push(&work, -(n + 1));
956
957
        /* Push children in enter state */
958
419
        unsigned int child_count = 0;
959
419
        int len_prior = work.len;
960
759
        do {
961
759
          zend_worklist_stack_push(&work, j + 1);
962
759
          j = ssa->cfg.blocks[j].next_child;
963
759
          child_count++;
964
759
        } while (j >= 0);
965
966
        /* Reverse block order to maintain SSA variable number order given in previous PHP versions,
967
         * but the data structure doesn't allow reverse dominator tree traversal. */
968
759
        for (unsigned int i = 0; i < child_count / 2; i++) {
969
340
          int tmp = work.buf[len_prior + i];
970
340
          work.buf[len_prior + i] = work.buf[work.len - 1 - i];
971
340
          work.buf[work.len - 1 - i] = tmp;
972
340
        }
973
419
      } else {
974
        /* Leafs jump directly to backtracking */
975
372
        goto backtrack;
976
372
      }
977
791
    }
978
    /* Leave state: backtrack */
979
419
    else {
980
419
      n = -n;
981
419
      n--;
982
791
backtrack:;
983
791
      if (ssa->cfg.blocks[n].next_child >= 0) {
984
340
        efree(save_vars[save_vars_top]);
985
340
        save_vars_top--;
986
340
      }
987
791
    }
988
1.21k
  }
989
990
32
  free_alloca(save_vars, save_vars_use_heap);
991
32
  ZEND_WORKLIST_STACK_FREE_ALLOCA(&work, work_use_heap);
992
993
32
  return SUCCESS;
994
32
}
995
996
ZEND_API zend_result zend_build_ssa(zend_arena **arena, const zend_script *script, const zend_op_array *op_array, uint32_t build_flags, zend_ssa *ssa) /* {{{ */
997
32
{
998
32
  const zend_basic_block *blocks = ssa->cfg.blocks;
999
32
  zend_ssa_block *ssa_blocks;
1000
32
  int blocks_count = ssa->cfg.blocks_count;
1001
32
  uint32_t set_size;
1002
32
  zend_bitset def, in, phi;
1003
32
  int *var = NULL;
1004
32
  int i, j, k, changed;
1005
32
  zend_dfg dfg;
1006
32
  ALLOCA_FLAG(dfg_use_heap)
1007
32
  ALLOCA_FLAG(var_use_heap)
1008
1009
32
  if ((blocks_count * (op_array->last_var + op_array->T)) > 4 * 1024 * 1024) {
1010
      /* Don't build SSA for very big functions */
1011
0
    return FAILURE;
1012
0
  }
1013
1014
32
  ssa_blocks = zend_arena_calloc(arena, blocks_count, sizeof(zend_ssa_block));
1015
32
  ssa->blocks = ssa_blocks;
1016
1017
  /* Compute Variable Liveness */
1018
32
  dfg.vars = op_array->last_var + op_array->T;
1019
32
  dfg.size = set_size = zend_bitset_len(dfg.vars);
1020
32
  dfg.tmp = do_alloca((set_size * sizeof(zend_ulong)) * (blocks_count * 4 + 1), dfg_use_heap);
1021
32
  memset(dfg.tmp, 0, (set_size * sizeof(zend_ulong)) * (blocks_count * 4 + 1));
1022
32
  dfg.def = dfg.tmp + set_size;
1023
32
  dfg.use = dfg.def + set_size * blocks_count;
1024
32
  dfg.in  = dfg.use + set_size * blocks_count;
1025
32
  dfg.out = dfg.in  + set_size * blocks_count;
1026
1027
32
  zend_build_dfg(op_array, &ssa->cfg, &dfg, build_flags);
1028
1029
32
  if (build_flags & ZEND_SSA_DEBUG_LIVENESS) {
1030
0
    zend_dump_dfg(op_array, &ssa->cfg, &dfg);
1031
0
  }
1032
1033
32
  def = dfg.def;
1034
32
  in  = dfg.in;
1035
1036
  /* Reuse the "use" set, as we no longer need it */
1037
32
  phi = dfg.use;
1038
32
  zend_bitset_clear(phi, set_size * blocks_count);
1039
1040
  /* Place e-SSA pis. This will add additional "def" points, so it must
1041
   * happen before def propagation. */
1042
32
  place_essa_pis(arena, script, op_array, build_flags, ssa, &dfg);
1043
1044
  /* SSA construction, Step 1: Propagate "def" sets in merge points */
1045
41
  do {
1046
41
    changed = 0;
1047
1.59k
    for (j = 0; j < blocks_count; j++) {
1048
1.55k
      zend_bitset def_j = def + j * set_size, phi_j = phi + j * set_size;
1049
1.55k
      if ((blocks[j].flags & ZEND_BB_REACHABLE) == 0) {
1050
0
        continue;
1051
0
      }
1052
1.55k
      if (blocks[j].predecessors_count > 1) {
1053
715
        if (blocks[j].flags & ZEND_BB_IRREDUCIBLE_LOOP) {
1054
          /* Prevent any values from flowing into irreducible loops by
1055
             replacing all incoming values with explicit phis.  The
1056
             register allocator depends on this property.  */
1057
0
          zend_bitset_union(phi_j, in + (j * set_size), set_size);
1058
715
        } else {
1059
2.14k
          for (k = 0; k < blocks[j].predecessors_count; k++) {
1060
1.43k
            i = ssa->cfg.predecessors[blocks[j].predecessor_offset + k];
1061
2.47k
            while (i != -1 && i != blocks[j].idom) {
1062
1.04k
              zend_bitset_union_with_intersection(
1063
1.04k
                phi_j, phi_j, def + (i * set_size), in + (j * set_size), set_size);
1064
1.04k
              i = blocks[i].idom;
1065
1.04k
            }
1066
1.43k
          }
1067
715
        }
1068
715
        if (!zend_bitset_subset(phi_j, def_j, set_size)) {
1069
340
          zend_bitset_union(def_j, phi_j, set_size);
1070
340
          changed = 1;
1071
340
        }
1072
715
      }
1073
1.55k
    }
1074
41
  } while (changed);
1075
1076
  /* SSA construction, Step 2: Phi placement based on Dominance Frontiers */
1077
32
  var = do_alloca(sizeof(int) * (op_array->last_var + op_array->T), var_use_heap);
1078
32
  if (!var) {
1079
0
    free_alloca(dfg.tmp, dfg_use_heap);
1080
0
    return FAILURE;
1081
0
  }
1082
1083
823
  for (j = 0; j < blocks_count; j++) {
1084
791
    if ((blocks[j].flags & ZEND_BB_REACHABLE) == 0) {
1085
0
      continue;
1086
0
    }
1087
791
    if (!zend_bitset_empty(phi + j * set_size, set_size)) {
1088
31.2k
      ZEND_BITSET_REVERSE_FOREACH(phi + j * set_size, set_size, i) {
1089
600
        zend_ssa_phi *phi = zend_arena_calloc(arena, 1,
1090
600
          ZEND_MM_ALIGNED_SIZE(sizeof(zend_ssa_phi)) +
1091
600
          ZEND_MM_ALIGNED_SIZE(sizeof(int) * blocks[j].predecessors_count) +
1092
600
          sizeof(void*) * blocks[j].predecessors_count);
1093
1094
600
        phi->sources = (int*)(((char*)phi) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_ssa_phi)));
1095
600
        memset(phi->sources, 0xff, sizeof(int) * blocks[j].predecessors_count);
1096
600
        phi->use_chains = (zend_ssa_phi**)(((char*)phi->sources) + ZEND_MM_ALIGNED_SIZE(sizeof(int) * ssa->cfg.blocks[j].predecessors_count));
1097
1098
600
        phi->pi = -1;
1099
600
        phi->var = i;
1100
600
        phi->ssa_var = -1;
1101
1102
        /* Place phis after pis */
1103
600
        {
1104
600
          zend_ssa_phi **pp = &ssa_blocks[j].phis;
1105
609
          while (*pp) {
1106
255
            if ((*pp)->pi < 0) {
1107
246
              break;
1108
246
            }
1109
9
            pp = &(*pp)->next;
1110
9
          }
1111
600
          phi->next = *pp;
1112
600
          *pp = phi;
1113
600
        }
1114
600
      } ZEND_BITSET_FOREACH_END();
1115
354
    }
1116
791
  }
1117
1118
32
  if (build_flags & ZEND_SSA_DEBUG_PHI_PLACEMENT) {
1119
0
    zend_dump_phi_placement(op_array, ssa);
1120
0
  }
1121
1122
  /* SSA construction, Step 3: Renaming */
1123
32
  ssa->ops = zend_arena_calloc(arena, op_array->last, sizeof(zend_ssa_op));
1124
32
  memset(ssa->ops, 0xff, op_array->last * sizeof(zend_ssa_op));
1125
32
  memset(var + op_array->last_var, 0xff, op_array->T * sizeof(int));
1126
  /* Create uninitialized SSA variables for each CV */
1127
180
  for (j = 0; j < op_array->last_var; j++) {
1128
148
    var[j] = j;
1129
148
  }
1130
32
  ssa->vars_count = op_array->last_var;
1131
32
  if (zend_ssa_rename(op_array, build_flags, ssa, var, 0) == FAILURE) {
1132
0
    free_alloca(var, var_use_heap);
1133
0
    free_alloca(dfg.tmp, dfg_use_heap);
1134
0
    return FAILURE;
1135
0
  }
1136
1137
32
  free_alloca(var, var_use_heap);
1138
32
  free_alloca(dfg.tmp, dfg_use_heap);
1139
1140
32
  return SUCCESS;
1141
32
}
1142
/* }}} */
1143
1144
ZEND_API void zend_ssa_compute_use_def_chains(zend_arena **arena, const zend_op_array *op_array, zend_ssa *ssa) /* {{{ */
1145
32
{
1146
32
  zend_ssa_var *ssa_vars;
1147
32
  int i;
1148
1149
32
  if (!ssa->vars) {
1150
32
    ssa->vars = zend_arena_calloc(arena, ssa->vars_count, sizeof(zend_ssa_var));
1151
32
  }
1152
32
  ssa_vars = ssa->vars;
1153
1154
180
  for (i = 0; i < op_array->last_var; i++) {
1155
148
    ssa_vars[i].var = i;
1156
148
    ssa_vars[i].scc = -1;
1157
148
    ssa_vars[i].definition = -1;
1158
148
    ssa_vars[i].use_chain = -1;
1159
148
  }
1160
4.49k
  for (i = op_array->last_var; i < ssa->vars_count; i++) {
1161
4.46k
    ssa_vars[i].var = -1;
1162
4.46k
    ssa_vars[i].scc = -1;
1163
4.46k
    ssa_vars[i].definition = -1;
1164
4.46k
    ssa_vars[i].use_chain = -1;
1165
4.46k
  }
1166
1167
3.99k
  for (i = op_array->last - 1; i >= 0; i--) {
1168
3.96k
    zend_ssa_op *op = ssa->ops + i;
1169
1170
3.96k
    if (op->op1_use >= 0) {
1171
3.53k
      op->op1_use_chain = ssa_vars[op->op1_use].use_chain;
1172
3.53k
      ssa_vars[op->op1_use].use_chain = i;
1173
3.53k
    }
1174
3.96k
    if (op->op2_use >= 0 && op->op2_use != op->op1_use) {
1175
1.28k
      op->op2_use_chain = ssa_vars[op->op2_use].use_chain;
1176
1.28k
      ssa_vars[op->op2_use].use_chain = i;
1177
1.28k
    }
1178
3.96k
    if (op->result_use >= 0 && op->result_use != op->op1_use && op->result_use != op->op2_use) {
1179
5
      op->res_use_chain = ssa_vars[op->result_use].use_chain;
1180
5
      ssa_vars[op->result_use].use_chain = i;
1181
5
    }
1182
3.96k
    if (op->op1_def >= 0) {
1183
211
      ssa_vars[op->op1_def].var = EX_VAR_TO_NUM(op_array->opcodes[i].op1.var);
1184
211
      ssa_vars[op->op1_def].definition = i;
1185
211
    }
1186
3.96k
    if (op->op2_def >= 0) {
1187
11
      ssa_vars[op->op2_def].var = EX_VAR_TO_NUM(op_array->opcodes[i].op2.var);
1188
11
      ssa_vars[op->op2_def].definition = i;
1189
11
    }
1190
3.96k
    if (op->result_def >= 0) {
1191
3.32k
      ssa_vars[op->result_def].var = EX_VAR_TO_NUM(op_array->opcodes[i].result.var);
1192
3.32k
      ssa_vars[op->result_def].definition = i;
1193
3.32k
    }
1194
3.96k
  }
1195
1196
823
  for (i = 0; i < ssa->cfg.blocks_count; i++) {
1197
791
    zend_ssa_phi *phi = ssa->blocks[i].phis;
1198
1.70k
    while (phi) {
1199
912
      phi->block = i;
1200
912
      ssa_vars[phi->ssa_var].var = phi->var;
1201
912
      ssa_vars[phi->ssa_var].definition_phi = phi;
1202
912
      if (phi->pi >= 0) {
1203
312
        zend_ssa_phi *p;
1204
1205
312
        ZEND_ASSERT(phi->sources[0] >= 0);
1206
312
        p = ssa_vars[phi->sources[0]].phi_use_chain;
1207
352
        while (p && p != phi) {
1208
40
          p = zend_ssa_next_use_phi(ssa, phi->sources[0], p);
1209
40
        }
1210
312
        if (!p) {
1211
312
          phi->use_chains[0] = ssa_vars[phi->sources[0]].phi_use_chain;
1212
312
          ssa_vars[phi->sources[0]].phi_use_chain = phi;
1213
312
        }
1214
312
        if (phi->has_range_constraint) {
1215
          /* min and max variables can't be used together */
1216
80
          zend_ssa_range_constraint *constraint = &phi->constraint.range;
1217
80
          if (constraint->min_ssa_var >= 0) {
1218
0
            phi->sym_use_chain = ssa_vars[constraint->min_ssa_var].sym_use_chain;
1219
0
            ssa_vars[constraint->min_ssa_var].sym_use_chain = phi;
1220
80
          } else if (constraint->max_ssa_var >= 0) {
1221
0
            phi->sym_use_chain = ssa_vars[constraint->max_ssa_var].sym_use_chain;
1222
0
            ssa_vars[constraint->max_ssa_var].sym_use_chain = phi;
1223
0
          }
1224
80
        }
1225
600
      } else {
1226
600
        int j;
1227
1228
1.80k
        for (j = 0; j < ssa->cfg.blocks[i].predecessors_count; j++) {
1229
1.20k
          zend_ssa_phi *p;
1230
1231
1.20k
          ZEND_ASSERT(phi->sources[j] >= 0);
1232
1.20k
          p = ssa_vars[phi->sources[j]].phi_use_chain;
1233
1.43k
          while (p && p != phi) {
1234
237
            p = zend_ssa_next_use_phi(ssa, phi->sources[j], p);
1235
237
          }
1236
1.20k
          if (!p) {
1237
1.20k
            phi->use_chains[j] = ssa_vars[phi->sources[j]].phi_use_chain;
1238
1.20k
            ssa_vars[phi->sources[j]].phi_use_chain = phi;
1239
1.20k
          }
1240
1.20k
        }
1241
600
      }
1242
912
      phi = phi->next;
1243
912
    }
1244
791
  }
1245
1246
  /* Mark indirectly accessed variables */
1247
180
  for (i = 0; i < op_array->last_var; i++) {
1248
148
    if ((ssa->cfg.flags & ZEND_FUNC_INDIRECT_VAR_ACCESS)) {
1249
0
      ssa_vars[i].alias = SYMTABLE_ALIAS;
1250
148
    } else if (zend_string_equals_literal(op_array->vars[i], "http_response_header")) {
1251
0
      ssa_vars[i].alias = HTTP_RESPONSE_HEADER_ALIAS;
1252
0
    }
1253
148
  }
1254
4.49k
  for (i = op_array->last_var; i < ssa->vars_count; i++) {
1255
4.46k
    if (ssa_vars[i].var < op_array->last_var) {
1256
861
      ssa_vars[i].alias = ssa_vars[ssa_vars[i].var].alias;
1257
861
    }
1258
4.46k
  }
1259
32
}
1260
/* }}} */
1261
1262
void zend_ssa_unlink_use_chain(const zend_ssa *ssa, int op, int var) /* {{{ */
1263
108
{
1264
108
  if (ssa->vars[var].use_chain == op) {
1265
85
    ssa->vars[var].use_chain = zend_ssa_next_use(ssa->ops, var, op);
1266
85
    return;
1267
85
  }
1268
23
  int use = ssa->vars[var].use_chain;
1269
1270
71
  while (use >= 0) {
1271
71
    if (ssa->ops[use].result_use == var) {
1272
0
      if (ssa->ops[use].res_use_chain == op) {
1273
0
        ssa->ops[use].res_use_chain = zend_ssa_next_use(ssa->ops, var, op);
1274
0
        return;
1275
0
      } else {
1276
0
        use = ssa->ops[use].res_use_chain;
1277
0
      }
1278
71
    } else if (ssa->ops[use].op1_use == var) {
1279
37
      if (ssa->ops[use].op1_use_chain == op) {
1280
17
        ssa->ops[use].op1_use_chain = zend_ssa_next_use(ssa->ops, var, op);
1281
17
        return;
1282
20
      } else {
1283
20
        use = ssa->ops[use].op1_use_chain;
1284
20
      }
1285
37
    } else if (ssa->ops[use].op2_use == var) {
1286
34
      if (ssa->ops[use].op2_use_chain == op) {
1287
6
        ssa->ops[use].op2_use_chain = zend_ssa_next_use(ssa->ops, var, op);
1288
6
        return;
1289
28
      } else {
1290
28
        use = ssa->ops[use].op2_use_chain;
1291
28
      }
1292
34
    } else {
1293
0
      break;
1294
0
    }
1295
71
  }
1296
  /* something wrong */
1297
0
  ZEND_UNREACHABLE();
1298
0
}
1299
/* }}} */
1300
1301
void zend_ssa_replace_use_chain(const zend_ssa *ssa, int op, int new_op, int var) /* {{{ */
1302
0
{
1303
0
  if (ssa->vars[var].use_chain == op) {
1304
0
    ssa->vars[var].use_chain = new_op;
1305
0
    return;
1306
0
  } else {
1307
0
    int use = ssa->vars[var].use_chain;
1308
1309
0
    while (use >= 0) {
1310
0
      if (ssa->ops[use].result_use == var) {
1311
0
        if (ssa->ops[use].res_use_chain == op) {
1312
0
          ssa->ops[use].res_use_chain = new_op;
1313
0
          return;
1314
0
        } else {
1315
0
          use = ssa->ops[use].res_use_chain;
1316
0
        }
1317
0
      } else if (ssa->ops[use].op1_use == var) {
1318
0
        if (ssa->ops[use].op1_use_chain == op) {
1319
0
          ssa->ops[use].op1_use_chain = new_op;
1320
0
          return;
1321
0
        } else {
1322
0
          use = ssa->ops[use].op1_use_chain;
1323
0
        }
1324
0
      } else if (ssa->ops[use].op2_use == var) {
1325
0
        if (ssa->ops[use].op2_use_chain == op) {
1326
0
          ssa->ops[use].op2_use_chain = new_op;
1327
0
          return;
1328
0
        } else {
1329
0
          use = ssa->ops[use].op2_use_chain;
1330
0
        }
1331
0
      } else {
1332
0
        break;
1333
0
      }
1334
0
    }
1335
0
  }
1336
  /* something wrong */
1337
0
  ZEND_UNREACHABLE();
1338
0
}
1339
/* }}} */
1340
1341
void zend_ssa_remove_instr(const zend_ssa *ssa, zend_op *opline, zend_ssa_op *ssa_op) /* {{{ */
1342
175
{
1343
175
  if (ssa_op->result_use >= 0) {
1344
0
    zend_ssa_unlink_use_chain(ssa, ssa_op - ssa->ops, ssa_op->result_use);
1345
0
    ssa_op->result_use = -1;
1346
0
    ssa_op->res_use_chain = -1;
1347
0
  }
1348
175
  if (ssa_op->op1_use >= 0) {
1349
63
    if (ssa_op->op1_use != ssa_op->op2_use) {
1350
63
      zend_ssa_unlink_use_chain(ssa, ssa_op - ssa->ops, ssa_op->op1_use);
1351
63
    } else {
1352
0
      ssa_op->op2_use_chain = ssa_op->op1_use_chain;
1353
0
    }
1354
63
    ssa_op->op1_use = -1;
1355
63
    ssa_op->op1_use_chain = -1;
1356
63
  }
1357
175
  if (ssa_op->op2_use >= 0) {
1358
15
    zend_ssa_unlink_use_chain(ssa, ssa_op - ssa->ops, ssa_op->op2_use);
1359
15
    ssa_op->op2_use = -1;
1360
15
    ssa_op->op2_use_chain = -1;
1361
15
  }
1362
1363
  /* We let the caller make sure that all defs are gone */
1364
175
  ZEND_ASSERT(ssa_op->result_def == -1);
1365
175
  ZEND_ASSERT(ssa_op->op1_def == -1);
1366
175
  ZEND_ASSERT(ssa_op->op2_def == -1);
1367
1368
175
  MAKE_NOP(opline);
1369
175
}
1370
/* }}} */
1371
1372
static inline zend_ssa_phi **zend_ssa_next_use_phi_ptr(const zend_ssa *ssa, int var, zend_ssa_phi *p) /* {{{ */
1373
18
{
1374
18
  if (p->pi >= 0) {
1375
18
    return &p->use_chains[0];
1376
18
  } else {
1377
0
    int j;
1378
0
    for (j = 0; j < ssa->cfg.blocks[p->block].predecessors_count; j++) {
1379
0
      if (p->sources[j] == var) {
1380
0
        return &p->use_chains[j];
1381
0
      }
1382
0
    }
1383
0
  }
1384
0
  ZEND_UNREACHABLE();
1385
0
  return NULL;
1386
0
}
1387
/* }}} */
1388
1389
/* May be called even if source is not used in the phi (useful when removing uses in a phi
1390
 * with multiple identical operands) */
1391
static inline void zend_ssa_remove_use_of_phi_source(const zend_ssa *ssa, const zend_ssa_phi *phi, int source, zend_ssa_phi *next_use_phi) /* {{{ */
1392
106
{
1393
106
  zend_ssa_phi **cur = &ssa->vars[source].phi_use_chain;
1394
124
  while (*cur && *cur != phi) {
1395
18
    cur = zend_ssa_next_use_phi_ptr(ssa, source, *cur);
1396
18
  }
1397
106
  if (*cur) {
1398
50
    *cur = next_use_phi;
1399
50
  }
1400
106
}
1401
/* }}} */
1402
1403
static void zend_ssa_remove_uses_of_phi_sources(const zend_ssa *ssa, zend_ssa_phi *phi) /* {{{ */
1404
62
{
1405
62
  int source;
1406
174
  FOREACH_PHI_SOURCE(phi, source) {
1407
174
    zend_ssa_remove_use_of_phi_source(ssa, phi, source, zend_ssa_next_use_phi(ssa, source, phi));
1408
174
  } FOREACH_PHI_SOURCE_END();
1409
62
}
1410
/* }}} */
1411
1412
static void zend_ssa_remove_phi_from_block(const zend_ssa *ssa, const zend_ssa_phi *phi) /* {{{ */
1413
62
{
1414
62
  zend_ssa_block *block = &ssa->blocks[phi->block];
1415
62
  zend_ssa_phi **cur = &block->phis;
1416
62
  while (*cur != phi) {
1417
0
    ZEND_ASSERT(*cur != NULL);
1418
0
    cur = &(*cur)->next;
1419
0
  }
1420
62
  *cur = (*cur)->next;
1421
62
}
1422
/* }}} */
1423
1424
void zend_ssa_remove_defs_of_instr(zend_ssa *ssa, zend_ssa_op *ssa_op) /* {{{ */
1425
120
{
1426
120
  if (ssa_op->op1_def >= 0) {
1427
18
    zend_ssa_remove_uses_of_var(ssa, ssa_op->op1_def);
1428
18
    zend_ssa_remove_op1_def(ssa, ssa_op);
1429
18
  }
1430
120
  if (ssa_op->op2_def >= 0) {
1431
0
    zend_ssa_remove_uses_of_var(ssa, ssa_op->op2_def);
1432
0
    zend_ssa_remove_op2_def(ssa, ssa_op);
1433
0
  }
1434
120
  if (ssa_op->result_def >= 0) {
1435
84
    zend_ssa_remove_uses_of_var(ssa, ssa_op->result_def);
1436
84
    zend_ssa_remove_result_def(ssa, ssa_op);
1437
84
  }
1438
120
}
1439
/* }}} */
1440
1441
static inline void zend_ssa_remove_phi_source(const zend_ssa *ssa, const zend_ssa_phi *phi, int pred_offset, int predecessors_count) /* {{{ */
1442
50
{
1443
50
  int j, var_num = phi->sources[pred_offset];
1444
50
  zend_ssa_phi *next_phi = phi->use_chains[pred_offset];
1445
1446
50
  predecessors_count--;
1447
50
  if (pred_offset < predecessors_count) {
1448
20
    memmove(phi->sources + pred_offset, phi->sources + pred_offset + 1, (predecessors_count - pred_offset) * sizeof(uint32_t));
1449
20
    memmove(phi->use_chains + pred_offset, phi->use_chains + pred_offset + 1, (predecessors_count - pred_offset) * sizeof(zend_ssa_phi*));
1450
20
  }
1451
1452
  /* Check if they same var is used in a different phi operand as well, in this case we don't
1453
   * need to adjust the use chain (but may have to move the next pointer). */
1454
80
  for (j = 0; j < predecessors_count; j++) {
1455
30
    if (phi->sources[j] == var_num) {
1456
0
      if (j < pred_offset) {
1457
0
        ZEND_ASSERT(next_phi == NULL);
1458
0
      } else if (j >= pred_offset) {
1459
0
        phi->use_chains[j] = next_phi;
1460
0
      }
1461
0
      return;
1462
0
    }
1463
30
  }
1464
1465
  /* Variable only used in one operand, remove the phi from the use chain. */
1466
50
  zend_ssa_remove_use_of_phi_source(ssa, phi, var_num, next_phi);
1467
50
}
1468
/* }}} */
1469
1470
void zend_ssa_remove_phi(const zend_ssa *ssa, zend_ssa_phi *phi) /* {{{ */
1471
62
{
1472
62
  ZEND_ASSERT(phi->ssa_var >= 0);
1473
62
  ZEND_ASSERT(ssa->vars[phi->ssa_var].use_chain < 0
1474
62
    && ssa->vars[phi->ssa_var].phi_use_chain == NULL);
1475
62
  zend_ssa_remove_uses_of_phi_sources(ssa, phi);
1476
62
  zend_ssa_remove_phi_from_block(ssa, phi);
1477
62
  ssa->vars[phi->ssa_var].definition_phi = NULL;
1478
62
  phi->ssa_var = -1;
1479
62
}
1480
/* }}} */
1481
1482
void zend_ssa_remove_uses_of_var(const zend_ssa *ssa, int var_num) /* {{{ */
1483
148
{
1484
148
  zend_ssa_var *var = &ssa->vars[var_num];
1485
148
  zend_ssa_phi *phi;
1486
148
  int use;
1487
204
  FOREACH_PHI_USE(var, phi) {
1488
204
    int i, end = NUM_PHI_SOURCES(phi);
1489
204
    for (i = 0; i < end; i++) {
1490
86
      if (phi->sources[i] == var_num) {
1491
56
        phi->use_chains[i] = NULL;
1492
56
      }
1493
86
    }
1494
204
  } FOREACH_PHI_USE_END();
1495
148
  var->phi_use_chain = NULL;
1496
232
  FOREACH_USE(var, use) {
1497
232
    zend_ssa_op *ssa_op = &ssa->ops[use];
1498
232
    if (ssa_op->op1_use == var_num) {
1499
78
      ssa_op->op1_use = -1;
1500
78
      ssa_op->op1_use_chain = -1;
1501
78
    }
1502
232
    if (ssa_op->op2_use == var_num) {
1503
6
      ssa_op->op2_use = -1;
1504
6
      ssa_op->op2_use_chain = -1;
1505
6
    }
1506
232
    if (ssa_op->result_use == var_num) {
1507
0
      ssa_op->result_use = -1;
1508
0
      ssa_op->res_use_chain = -1;
1509
0
    }
1510
232
  } FOREACH_USE_END();
1511
148
  var->use_chain = -1;
1512
148
}
1513
/* }}} */
1514
1515
void zend_ssa_remove_predecessor(zend_ssa *ssa, int from, int to) /* {{{ */
1516
84
{
1517
84
  zend_basic_block *next_block = &ssa->cfg.blocks[to];
1518
84
  const zend_ssa_block *next_ssa_block = &ssa->blocks[to];
1519
84
  zend_ssa_phi *phi;
1520
84
  int j;
1521
1522
  /* Find at which predecessor offset this block is referenced */
1523
84
  int pred_offset = -1;
1524
84
  int *predecessors = &ssa->cfg.predecessors[next_block->predecessor_offset];
1525
1526
108
  for (j = 0; j < next_block->predecessors_count; j++) {
1527
94
    if (predecessors[j] == from) {
1528
70
      pred_offset = j;
1529
70
      break;
1530
70
    }
1531
94
  }
1532
1533
  /* If there are duplicate successors, the predecessors may have been removed in
1534
   * a previous iteration already. */
1535
84
  if (pred_offset == -1) {
1536
14
    return;
1537
14
  }
1538
1539
  /* For phis in successor blocks, remove the operands associated with this block */
1540
126
  for (phi = next_ssa_block->phis; phi; phi = phi->next) {
1541
56
    if (phi->pi >= 0) {
1542
6
      if (phi->pi == from) {
1543
6
        zend_ssa_rename_var_uses(ssa, phi->ssa_var, phi->sources[0], /* update_types */ false);
1544
6
        zend_ssa_remove_phi(ssa, phi);
1545
6
      }
1546
50
    } else {
1547
50
      ZEND_ASSERT(phi->sources[pred_offset] >= 0);
1548
50
      zend_ssa_remove_phi_source(ssa, phi, pred_offset, next_block->predecessors_count);
1549
50
    }
1550
56
  }
1551
1552
  /* Remove this predecessor */
1553
70
  next_block->predecessors_count--;
1554
70
  if (pred_offset < next_block->predecessors_count) {
1555
20
    predecessors = &ssa->cfg.predecessors[next_block->predecessor_offset + pred_offset];
1556
20
    memmove(predecessors, predecessors + 1, (next_block->predecessors_count - pred_offset) * sizeof(uint32_t));
1557
20
  }
1558
70
}
1559
/* }}} */
1560
1561
void zend_ssa_remove_block(const zend_op_array *op_array, zend_ssa *ssa, int i) /* {{{ */
1562
70
{
1563
70
  zend_basic_block *block = &ssa->cfg.blocks[i];
1564
70
  const zend_ssa_block *ssa_block = &ssa->blocks[i];
1565
70
  zend_ssa_phi *phi;
1566
70
  int j;
1567
1568
70
  block->flags &= ~ZEND_BB_REACHABLE;
1569
1570
  /* Removes phis in this block */
1571
102
  for (phi = ssa_block->phis; phi; phi = phi->next) {
1572
32
    zend_ssa_remove_uses_of_var(ssa, phi->ssa_var);
1573
32
    zend_ssa_remove_phi(ssa, phi);
1574
32
  }
1575
1576
  /* Remove instructions in this block */
1577
190
  for (j = block->start; j < block->start + block->len; j++) {
1578
120
    if (op_array->opcodes[j].opcode == ZEND_NOP) {
1579
0
      continue;
1580
0
    }
1581
1582
120
    zend_ssa_remove_defs_of_instr(ssa, &ssa->ops[j]);
1583
120
    zend_ssa_remove_instr(ssa, &op_array->opcodes[j], &ssa->ops[j]);
1584
120
  }
1585
1586
70
  zend_ssa_remove_block_from_cfg(ssa, i);
1587
70
}
1588
/* }}} */
1589
1590
void zend_ssa_remove_block_from_cfg(zend_ssa *ssa, int i) /* {{{ */
1591
70
{
1592
70
  zend_basic_block *block = &ssa->cfg.blocks[i];
1593
70
  int *predecessors;
1594
70
  int j, s;
1595
1596
154
  for (s = 0; s < block->successors_count; s++) {
1597
84
    zend_ssa_remove_predecessor(ssa, i, block->successors[s]);
1598
84
  }
1599
1600
  /* Remove successors of predecessors */
1601
70
  predecessors = &ssa->cfg.predecessors[block->predecessor_offset];
1602
100
  for (j = 0; j < block->predecessors_count; j++) {
1603
30
    if (predecessors[j] >= 0) {
1604
30
      zend_basic_block *prev_block = &ssa->cfg.blocks[predecessors[j]];
1605
1606
84
      for (s = 0; s < prev_block->successors_count; s++) {
1607
54
        if (prev_block->successors[s] == i) {
1608
16
          memmove(prev_block->successors + s,
1609
16
              prev_block->successors + s + 1,
1610
16
              sizeof(int) * (prev_block->successors_count - s - 1));
1611
16
          prev_block->successors_count--;
1612
16
          s--;
1613
16
        }
1614
54
      }
1615
30
    }
1616
30
  }
1617
1618
70
  block->successors_count = 0;
1619
70
  block->predecessors_count = 0;
1620
1621
  /* Remove from dominators tree */
1622
70
  if (block->idom >= 0) {
1623
70
    j = ssa->cfg.blocks[block->idom].children;
1624
70
    if (j == i) {
1625
28
      ssa->cfg.blocks[block->idom].children = block->next_child;
1626
42
    } else if (j >= 0) {
1627
0
      while (ssa->cfg.blocks[j].next_child >= 0) {
1628
0
        if (ssa->cfg.blocks[j].next_child == i) {
1629
0
          ssa->cfg.blocks[j].next_child = block->next_child;
1630
0
          break;
1631
0
        }
1632
0
        j = ssa->cfg.blocks[j].next_child;
1633
0
      }
1634
0
    }
1635
70
  }
1636
70
  block->idom = -1;
1637
70
  block->level = -1;
1638
70
  block->children = -1;
1639
70
  block->next_child = -1;
1640
70
}
1641
/* }}} */
1642
1643
static void propagate_phi_type_widening(zend_ssa *ssa, int var) /* {{{ */
1644
0
{
1645
0
  zend_ssa_phi *phi;
1646
0
  FOREACH_PHI_USE(&ssa->vars[var], phi) {
1647
0
    if (ssa->var_info[var].type & ~ssa->var_info[phi->ssa_var].type) {
1648
0
      ssa->var_info[phi->ssa_var].type |= ssa->var_info[var].type;
1649
0
      propagate_phi_type_widening(ssa, phi->ssa_var);
1650
0
    }
1651
0
  } FOREACH_PHI_USE_END();
1652
0
}
1653
/* }}} */
1654
1655
void zend_ssa_rename_var_uses(zend_ssa *ssa, int old, int new, bool update_types) /* {{{ */
1656
15
{
1657
15
  zend_ssa_var *old_var = &ssa->vars[old];
1658
15
  zend_ssa_var *new_var = &ssa->vars[new];
1659
15
  int use;
1660
15
  zend_ssa_phi *phi;
1661
1662
15
  ZEND_ASSERT(old >= 0 && new >= 0);
1663
15
  ZEND_ASSERT(old != new);
1664
1665
  /* Only a no_val is both variables are */
1666
15
  new_var->no_val &= old_var->no_val;
1667
1668
  /* Update ssa_op use chains */
1669
21
  FOREACH_USE(old_var, use) {
1670
21
    zend_ssa_op *ssa_op = &ssa->ops[use];
1671
1672
    /* If the op already uses the new var, don't add the op to the use
1673
     * list again. Instead move the use_chain to the correct operand. */
1674
21
    bool add_to_use_chain = true;
1675
21
    if (ssa_op->result_use == new) {
1676
0
      add_to_use_chain = false;
1677
6
    } else if (ssa_op->op1_use == new) {
1678
0
      if (ssa_op->result_use == old) {
1679
0
        ssa_op->res_use_chain = ssa_op->op1_use_chain;
1680
0
        ssa_op->op1_use_chain = -1;
1681
0
      }
1682
0
      add_to_use_chain = false;
1683
6
    } else if (ssa_op->op2_use == new) {
1684
0
      if (ssa_op->result_use == old) {
1685
0
        ssa_op->res_use_chain = ssa_op->op2_use_chain;
1686
0
        ssa_op->op2_use_chain = -1;
1687
0
      } else if (ssa_op->op1_use == old) {
1688
0
        ssa_op->op1_use_chain = ssa_op->op2_use_chain;
1689
0
        ssa_op->op2_use_chain = -1;
1690
0
      }
1691
0
      add_to_use_chain = false;
1692
0
    }
1693
1694
    /* Perform the actual renaming */
1695
21
    if (ssa_op->result_use == old) {
1696
0
      ssa_op->result_use = new;
1697
0
    }
1698
21
    if (ssa_op->op1_use == old) {
1699
6
      ssa_op->op1_use = new;
1700
6
    }
1701
21
    if (ssa_op->op2_use == old) {
1702
0
      ssa_op->op2_use = new;
1703
0
    }
1704
1705
    /* Add op to use chain of new var (if it isn't already). We use the
1706
     * first use chain of (result, op1, op2) that has the new variable. */
1707
21
    if (add_to_use_chain) {
1708
6
      if (ssa_op->result_use == new) {
1709
0
        ssa_op->res_use_chain = new_var->use_chain;
1710
0
        new_var->use_chain = use;
1711
6
      } else if (ssa_op->op1_use == new) {
1712
6
        ssa_op->op1_use_chain = new_var->use_chain;
1713
6
        new_var->use_chain = use;
1714
6
      } else {
1715
0
        ZEND_ASSERT(ssa_op->op2_use == new);
1716
0
        ssa_op->op2_use_chain = new_var->use_chain;
1717
0
        new_var->use_chain = use;
1718
0
      }
1719
6
    }
1720
21
  } FOREACH_USE_END();
1721
15
  old_var->use_chain = -1;
1722
1723
  /* Update phi use chains */
1724
21
  FOREACH_PHI_USE(old_var, phi) {
1725
21
    int j;
1726
21
    bool after_first_new_source = false;
1727
1728
    /* If the phi already uses the new var, find its use chain, as we may
1729
     * need to move it to a different source operand. */
1730
21
    zend_ssa_phi **existing_use_chain_ptr = NULL;
1731
21
    for (j = 0; j < ssa->cfg.blocks[phi->block].predecessors_count; j++) {
1732
6
      if (phi->sources[j] == new) {
1733
0
        existing_use_chain_ptr = &phi->use_chains[j];
1734
0
        break;
1735
0
      }
1736
6
    }
1737
1738
21
    for (j = 0; j < ssa->cfg.blocks[phi->block].predecessors_count; j++) {
1739
6
      if (phi->sources[j] == new) {
1740
0
        after_first_new_source = true;
1741
6
      } else if (phi->sources[j] == old) {
1742
6
        phi->sources[j] = new;
1743
1744
        /* Either move existing use chain to this source, or add the phi
1745
         * to the phi use chain of the new variables. Do this only once. */
1746
6
        if (!after_first_new_source) {
1747
6
          if (existing_use_chain_ptr) {
1748
0
            phi->use_chains[j] = *existing_use_chain_ptr;
1749
0
            *existing_use_chain_ptr = NULL;
1750
6
          } else {
1751
6
            phi->use_chains[j] = new_var->phi_use_chain;
1752
6
            new_var->phi_use_chain = phi;
1753
6
          }
1754
6
          after_first_new_source = true;
1755
6
        } else {
1756
0
          phi->use_chains[j] = NULL;
1757
0
        }
1758
6
      }
1759
6
    }
1760
1761
    /* Make sure phi result types are not incorrectly narrow after renaming.
1762
     * This should not normally happen, but can occur if we DCE an assignment
1763
     * or unset and there is an improper phi-indirected use lateron. */
1764
    // TODO Alternatively we could rerun type-inference after DCE
1765
21
    if (update_types && (ssa->var_info[new].type & ~ssa->var_info[phi->ssa_var].type)) {
1766
0
      ssa->var_info[phi->ssa_var].type |= ssa->var_info[new].type;
1767
0
      propagate_phi_type_widening(ssa, phi->ssa_var);
1768
0
    }
1769
21
  } FOREACH_PHI_USE_END();
1770
  old_var->phi_use_chain = NULL;
1771
15
}
1772
/* }}} */