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