Coverage Report

Created: 2025-07-23 06:33

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