xref: /PHP-8.1/Zend/Optimizer/zend_ssa.c (revision cfcee97a)
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,zend_dfg * dfg,zend_ssa * ssa,int from,int to,int var)60 static bool needs_pi(const zend_op_array *op_array, zend_dfg *dfg, zend_ssa *ssa, int from, int to, int var) /* {{{ */
61 {
62 	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 = 1;
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 = 0;
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 			case ZEND_JMPZNZ:
261 				bf = blocks[j].successors[0];
262 				bt = blocks[j].successors[1];
263 				break;
264 			case ZEND_JMPNZ:
265 				bt = blocks[j].successors[0];
266 				bf = blocks[j].successors[1];
267 				break;
268 			case ZEND_COALESCE:
269 				if (opline->op1_type == IS_CV) {
270 					int var = EX_VAR_TO_NUM(opline->op1.var);
271 					if ((pi = add_pi(arena, op_array, dfg, ssa, j, blocks[j].successors[0], var))) {
272 						pi_not_type_mask(pi, MAY_BE_NULL);
273 					}
274 				}
275 				continue;
276 			case ZEND_JMP_NULL:
277 				if (opline->op1_type == IS_CV) {
278 					int var = EX_VAR_TO_NUM(opline->op1.var);
279 					if ((pi = add_pi(arena, op_array, dfg, ssa, j, blocks[j].successors[1], var))) {
280 						pi_not_type_mask(pi, MAY_BE_NULL);
281 					}
282 				}
283 				continue;
284 			default:
285 				continue;
286 		}
287 
288 		/* The following patterns all inspect the opline directly before the JMPZ opcode.
289 		 * Make sure that it is part of the same block, otherwise it might not be a dominating
290 		 * assignment. */
291 		if (blocks[j].len == 1) {
292 			continue;
293 		}
294 
295 		if (opline->op1_type == IS_TMP_VAR &&
296 		    ((opline-1)->opcode == ZEND_IS_EQUAL ||
297 		     (opline-1)->opcode == ZEND_IS_NOT_EQUAL ||
298 		     (opline-1)->opcode == ZEND_IS_SMALLER ||
299 		     (opline-1)->opcode == ZEND_IS_SMALLER_OR_EQUAL) &&
300 		    opline->op1.var == (opline-1)->result.var) {
301 			int  var1 = -1;
302 			int  var2 = -1;
303 			zend_long val1 = 0;
304 			zend_long val2 = 0;
305 //			long val = 0;
306 
307 			if ((opline-1)->op1_type == IS_CV) {
308 				var1 = EX_VAR_TO_NUM((opline-1)->op1.var);
309 			} else if ((opline-1)->op1_type == IS_TMP_VAR) {
310 				var1 = find_adjusted_tmp_var(
311 					op_array, build_flags, opline, (opline-1)->op1.var, &val2);
312 			}
313 
314 			if ((opline-1)->op2_type == IS_CV) {
315 				var2 = EX_VAR_TO_NUM((opline-1)->op2.var);
316 			} else if ((opline-1)->op2_type == IS_TMP_VAR) {
317 				var2 = find_adjusted_tmp_var(
318 					op_array, build_flags, opline, (opline-1)->op2.var, &val1);
319 			}
320 
321 			if (var1 >= 0 && var2 >= 0) {
322 				if (!zend_sub_will_overflow(val1, val2) && !zend_sub_will_overflow(val2, val1)) {
323 					zend_long tmp = val1;
324 					val1 -= val2;
325 					val2 -= tmp;
326 				} else {
327 					var1 = -1;
328 					var2 = -1;
329 				}
330 			} else if (var1 >= 0 && var2 < 0) {
331 				zend_long add_val2 = 0;
332 				if ((opline-1)->op2_type == IS_CONST) {
333 					zval *zv = CRT_CONSTANT_EX(op_array, (opline-1), (opline-1)->op2);
334 
335 					if (Z_TYPE_P(zv) == IS_LONG) {
336 						add_val2 = Z_LVAL_P(zv);
337 					} else {
338 						var1 = -1;
339 					}
340 				} else {
341 					var1 = -1;
342 				}
343 				if (!zend_add_will_overflow(val2, add_val2)) {
344 					val2 += add_val2;
345 				} else {
346 					var1 = -1;
347 				}
348 			} else if (var1 < 0 && var2 >= 0) {
349 				zend_long add_val1 = 0;
350 				if ((opline-1)->op1_type == IS_CONST) {
351 					zval *zv = CRT_CONSTANT_EX(op_array, (opline-1), (opline-1)->op1);
352 					if (Z_TYPE_P(zv) == IS_LONG) {
353 						add_val1 = Z_LVAL_P(CRT_CONSTANT_EX(op_array, (opline-1), (opline-1)->op1));
354 					} else {
355 						var2 = -1;
356 					}
357 				} else {
358 					var2 = -1;
359 				}
360 				if (!zend_add_will_overflow(val1, add_val1)) {
361 					val1 += add_val1;
362 				} else {
363 					var2 = -1;
364 				}
365 			}
366 
367 			if (var1 >= 0) {
368 				if ((opline-1)->opcode == ZEND_IS_EQUAL) {
369 					if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var1))) {
370 						pi_range_equals(pi, var2, val2);
371 					}
372 					if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var1))) {
373 						pi_range_not_equals(pi, var2, val2);
374 					}
375 				} else if ((opline-1)->opcode == ZEND_IS_NOT_EQUAL) {
376 					if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var1))) {
377 						pi_range_equals(pi, var2, val2);
378 					}
379 					if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var1))) {
380 						pi_range_not_equals(pi, var2, val2);
381 					}
382 				} else if ((opline-1)->opcode == ZEND_IS_SMALLER) {
383 					if (val2 > ZEND_LONG_MIN) {
384 						if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var1))) {
385 							pi_range_max(pi, var2, val2-1);
386 						}
387 					}
388 					if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var1))) {
389 						pi_range_min(pi, var2, val2);
390 					}
391 				} else if ((opline-1)->opcode == ZEND_IS_SMALLER_OR_EQUAL) {
392 					if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var1))) {
393 						pi_range_max(pi, var2, val2);
394 					}
395 					if (val2 < ZEND_LONG_MAX) {
396 						if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var1))) {
397 							pi_range_min(pi, var2, val2+1);
398 						}
399 					}
400 				}
401 			}
402 			if (var2 >= 0) {
403 				if((opline-1)->opcode == ZEND_IS_EQUAL) {
404 					if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var2))) {
405 						pi_range_equals(pi, var1, val1);
406 					}
407 					if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var2))) {
408 						pi_range_not_equals(pi, var1, val1);
409 					}
410 				} else if ((opline-1)->opcode == ZEND_IS_NOT_EQUAL) {
411 					if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var2))) {
412 						pi_range_equals(pi, var1, val1);
413 					}
414 					if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var2))) {
415 						pi_range_not_equals(pi, var1, val1);
416 					}
417 				} else if ((opline-1)->opcode == ZEND_IS_SMALLER) {
418 					if (val1 < ZEND_LONG_MAX) {
419 						if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var2))) {
420 							pi_range_min(pi, var1, val1+1);
421 						}
422 					}
423 					if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var2))) {
424 						pi_range_max(pi, var1, val1);
425 					}
426 				} else if ((opline-1)->opcode == ZEND_IS_SMALLER_OR_EQUAL) {
427 					if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var2))) {
428 						pi_range_min(pi, var1, val1);
429 					}
430 					if (val1 > ZEND_LONG_MIN) {
431 						if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var2))) {
432 							pi_range_max(pi, var1, val1-1);
433 						}
434 					}
435 				}
436 			}
437 		} else if (opline->op1_type == IS_TMP_VAR &&
438 		           ((opline-1)->opcode == ZEND_POST_INC ||
439 		            (opline-1)->opcode == ZEND_POST_DEC) &&
440 		           opline->op1.var == (opline-1)->result.var &&
441 		           (opline-1)->op1_type == IS_CV) {
442 			int var = EX_VAR_TO_NUM((opline-1)->op1.var);
443 
444 			if ((opline-1)->opcode == ZEND_POST_DEC) {
445 				if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var))) {
446 					pi_range_equals(pi, -1, -1);
447 				}
448 				if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var))) {
449 					pi_range_not_equals(pi, -1, -1);
450 				}
451 			} else if ((opline-1)->opcode == ZEND_POST_INC) {
452 				if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var))) {
453 					pi_range_equals(pi, -1, 1);
454 				}
455 				if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var))) {
456 					pi_range_not_equals(pi, -1, 1);
457 				}
458 			}
459 		} else if (opline->op1_type == IS_TMP_VAR &&
460 		           ((opline-1)->opcode == ZEND_PRE_INC ||
461 		            (opline-1)->opcode == ZEND_PRE_DEC) &&
462 		           opline->op1.var == (opline-1)->result.var &&
463 		           (opline-1)->op1_type == IS_CV) {
464 			int var = EX_VAR_TO_NUM((opline-1)->op1.var);
465 
466 			if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var))) {
467 				pi_range_equals(pi, -1, 0);
468 			}
469 			/* speculative */
470 			if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var))) {
471 				pi_range_not_equals(pi, -1, 0);
472 			}
473 		} else if (opline->op1_type == IS_TMP_VAR && (opline-1)->opcode == ZEND_TYPE_CHECK &&
474 				   opline->op1.var == (opline-1)->result.var && (opline-1)->op1_type == IS_CV) {
475 			int var = EX_VAR_TO_NUM((opline-1)->op1.var);
476 			uint32_t type = (opline-1)->extended_value;
477 			if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var))) {
478 				pi_type_mask(pi, mask_for_type_check(type));
479 			}
480 			if (type != MAY_BE_RESOURCE) {
481 				/* is_resource() may return false for closed resources */
482 				if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var))) {
483 					pi_not_type_mask(pi, mask_for_type_check(type));
484 				}
485 			}
486 		} else if (opline->op1_type == IS_TMP_VAR &&
487 				   ((opline-1)->opcode == ZEND_IS_IDENTICAL
488 					|| (opline-1)->opcode == ZEND_IS_NOT_IDENTICAL) &&
489 				   opline->op1.var == (opline-1)->result.var) {
490 			int var;
491 			zval *val;
492 			uint32_t type_mask;
493 			if ((opline-1)->op1_type == IS_CV && (opline-1)->op2_type == IS_CONST) {
494 				var = EX_VAR_TO_NUM((opline-1)->op1.var);
495 				val = CRT_CONSTANT_EX(op_array, (opline-1), (opline-1)->op2);
496 			} else if ((opline-1)->op1_type == IS_CONST && (opline-1)->op2_type == IS_CV) {
497 				var = EX_VAR_TO_NUM((opline-1)->op2.var);
498 				val = CRT_CONSTANT_EX(op_array, (opline-1), (opline-1)->op1);
499 			} else {
500 				continue;
501 			}
502 
503 			/* We're interested in === null/true/false comparisons here, because they eliminate
504 			 * a type in the false-branch. Other === VAL comparisons are unlikely to be useful. */
505 			if (Z_TYPE_P(val) != IS_NULL && Z_TYPE_P(val) != IS_TRUE && Z_TYPE_P(val) != IS_FALSE) {
506 				continue;
507 			}
508 
509 			type_mask = _const_op_type(val);
510 			if ((opline-1)->opcode == ZEND_IS_IDENTICAL) {
511 				if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var))) {
512 					pi_type_mask(pi, type_mask);
513 				}
514 				if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var))) {
515 					pi_not_type_mask(pi, type_mask);
516 				}
517 			} else {
518 				if ((pi = add_pi(arena, op_array, dfg, ssa, j, bf, var))) {
519 					pi_type_mask(pi, type_mask);
520 				}
521 				if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var))) {
522 					pi_not_type_mask(pi, type_mask);
523 				}
524 			}
525 		} else if (opline->op1_type == IS_TMP_VAR && (opline-1)->opcode == ZEND_INSTANCEOF &&
526 				   opline->op1.var == (opline-1)->result.var && (opline-1)->op1_type == IS_CV &&
527 				   (opline-1)->op2_type == IS_CONST) {
528 			int var = EX_VAR_TO_NUM((opline-1)->op1.var);
529 			zend_string *lcname = Z_STR_P(CRT_CONSTANT_EX(op_array, (opline-1), (opline-1)->op2) + 1);
530 			zend_class_entry *ce = zend_optimizer_get_class_entry(script, lcname);
531 			if (!ce) {
532 				continue;
533 			}
534 
535 			if ((pi = add_pi(arena, op_array, dfg, ssa, j, bt, var))) {
536 				pi_type_mask(pi, MAY_BE_OBJECT);
537 				pi->constraint.type.ce = ce;
538 			}
539 		}
540 	}
541 }
542 /* }}} */
543 
_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 static zend_always_inline int _zend_ssa_rename_op(const zend_op_array *op_array, const zend_op *opline, uint32_t k, uint32_t build_flags, int ssa_vars_count, zend_ssa_op *ssa_ops, int *var) /* {{{ */
545 {
546 	const zend_op *next;
547 
548 	if (opline->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
549 		ssa_ops[k].op1_use = var[EX_VAR_TO_NUM(opline->op1.var)];
550 		//USE_SSA_VAR(op_array->last_var + opline->op1.var)
551 	}
552 	if (opline->op2_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
553 		ssa_ops[k].op2_use = var[EX_VAR_TO_NUM(opline->op2.var)];
554 		//USE_SSA_VAR(op_array->last_var + opline->op2.var)
555 	}
556 	if ((build_flags & ZEND_SSA_USE_CV_RESULTS)
557 	 && opline->result_type == IS_CV
558 	 && opline->opcode != ZEND_RECV) {
559 		ssa_ops[k].result_use = var[EX_VAR_TO_NUM(opline->result.var)];
560 		//USE_SSA_VAR(op_array->last_var + opline->result.var)
561 	}
562 
563 	switch (opline->opcode) {
564 		case ZEND_ASSIGN:
565 			if ((build_flags & ZEND_SSA_RC_INFERENCE) && opline->op2_type == IS_CV) {
566 				ssa_ops[k].op2_def = ssa_vars_count;
567 				var[EX_VAR_TO_NUM(opline->op2.var)] = ssa_vars_count;
568 				ssa_vars_count++;
569 				//NEW_SSA_VAR(opline->op2.var)
570 			}
571 			if (opline->op1_type == IS_CV) {
572 add_op1_def:
573 				ssa_ops[k].op1_def = ssa_vars_count;
574 				var[EX_VAR_TO_NUM(opline->op1.var)] = ssa_vars_count;
575 				ssa_vars_count++;
576 				//NEW_SSA_VAR(opline->op1.var)
577 			}
578 			break;
579 		case ZEND_ASSIGN_REF:
580 			if (opline->op2_type == IS_CV) {
581 				ssa_ops[k].op2_def = ssa_vars_count;
582 				var[EX_VAR_TO_NUM(opline->op2.var)] = ssa_vars_count;
583 				ssa_vars_count++;
584 				//NEW_SSA_VAR(opline->op2.var)
585 			}
586 			if (opline->op1_type == IS_CV) {
587 				goto add_op1_def;
588 			}
589 			break;
590 		case ZEND_ASSIGN_DIM:
591 		case ZEND_ASSIGN_OBJ:
592 			if (opline->op1_type == IS_CV) {
593 				ssa_ops[k].op1_def = ssa_vars_count;
594 				var[EX_VAR_TO_NUM(opline->op1.var)] = ssa_vars_count;
595 				ssa_vars_count++;
596 				//NEW_SSA_VAR(opline->op1.var)
597 			}
598 			next = opline + 1;
599 			if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
600 				ssa_ops[k + 1].op1_use = var[EX_VAR_TO_NUM(next->op1.var)];
601 				//USE_SSA_VAR(op_array->last_var + next->op1.var);
602 				if (build_flags & ZEND_SSA_RC_INFERENCE && next->op1_type == IS_CV) {
603 					ssa_ops[k + 1].op1_def = ssa_vars_count;
604 					var[EX_VAR_TO_NUM(next->op1.var)] = ssa_vars_count;
605 					ssa_vars_count++;
606 					//NEW_SSA_VAR(next->op1.var)
607 				}
608 			}
609 			break;
610 		case ZEND_ASSIGN_OBJ_REF:
611 			if (opline->op1_type == IS_CV) {
612 				ssa_ops[k].op1_def = ssa_vars_count;
613 				var[EX_VAR_TO_NUM(opline->op1.var)] = ssa_vars_count;
614 				ssa_vars_count++;
615 				//NEW_SSA_VAR(opline->op1.var)
616 			}
617 			next = opline + 1;
618 			if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
619 				ssa_ops[k + 1].op1_use = var[EX_VAR_TO_NUM(next->op1.var)];
620 				//USE_SSA_VAR(op_array->last_var + next->op1.var);
621 				if (next->op1_type == IS_CV) {
622 					ssa_ops[k + 1].op1_def = ssa_vars_count;
623 					var[EX_VAR_TO_NUM(next->op1.var)] = ssa_vars_count;
624 					ssa_vars_count++;
625 					//NEW_SSA_VAR(next->op1.var)
626 				}
627 			}
628 			break;
629 		case ZEND_ASSIGN_STATIC_PROP:
630 			next = opline + 1;
631 			if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
632 				ssa_ops[k + 1].op1_use = var[EX_VAR_TO_NUM(next->op1.var)];
633 				//USE_SSA_VAR(op_array->last_var + next->op1.var);
634 				if ((build_flags & ZEND_SSA_RC_INFERENCE) && next->op1_type == IS_CV) {
635 					ssa_ops[k + 1].op1_def = ssa_vars_count;
636 					var[EX_VAR_TO_NUM(next->op1.var)] = ssa_vars_count;
637 					ssa_vars_count++;
638 					//NEW_SSA_VAR(next->op1.var)
639 				}
640 			}
641 			break;
642 		case ZEND_ASSIGN_STATIC_PROP_REF:
643 			next = opline + 1;
644 			if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
645 				ssa_ops[k + 1].op1_use = var[EX_VAR_TO_NUM(next->op1.var)];
646 				//USE_SSA_VAR(op_array->last_var + next->op1.var);
647 				if (next->op1_type == IS_CV) {
648 					ssa_ops[k + 1].op1_def = ssa_vars_count;
649 					var[EX_VAR_TO_NUM(next->op1.var)] = ssa_vars_count;
650 					ssa_vars_count++;
651 					//NEW_SSA_VAR(next->op1.var)
652 				}
653 			}
654 			break;
655 		case ZEND_ASSIGN_STATIC_PROP_OP:
656 			next = opline + 1;
657 			if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
658 				ssa_ops[k + 1].op1_use = var[EX_VAR_TO_NUM(next->op1.var)];
659 				//USE_SSA_VAR(op_array->last_var + next->op1.var);
660 			}
661 			break;
662 		case ZEND_ASSIGN_DIM_OP:
663 		case ZEND_ASSIGN_OBJ_OP:
664 			if (opline->op1_type == IS_CV) {
665 				ssa_ops[k].op1_def = ssa_vars_count;
666 				var[EX_VAR_TO_NUM(opline->op1.var)] = ssa_vars_count;
667 				ssa_vars_count++;
668 				//NEW_SSA_VAR(opline->op1.var)
669 			}
670 			next = opline + 1;
671 			if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
672 				ssa_ops[k + 1].op1_use = var[EX_VAR_TO_NUM(next->op1.var)];
673 				//USE_SSA_VAR(op_array->last_var + next->op1.var);
674 			}
675 			break;
676 		case ZEND_ASSIGN_OP:
677 		case ZEND_PRE_INC:
678 		case ZEND_PRE_DEC:
679 		case ZEND_POST_INC:
680 		case ZEND_POST_DEC:
681 		case ZEND_BIND_GLOBAL:
682 		case ZEND_BIND_STATIC:
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 int 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) != SUCCESS)
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 int 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 	if (zend_build_dfg(op_array, &ssa->cfg, &dfg, build_flags) != SUCCESS) {
939 		free_alloca(dfg.tmp, dfg_use_heap);
940 		return FAILURE;
941 	}
942 
943 	if (build_flags & ZEND_SSA_DEBUG_LIVENESS) {
944 		zend_dump_dfg(op_array, &ssa->cfg, &dfg);
945 	}
946 
947 	def = dfg.def;
948 	in  = dfg.in;
949 
950 	/* Reuse the "use" set, as we no longer need it */
951 	phi = dfg.use;
952 	zend_bitset_clear(phi, set_size * blocks_count);
953 
954 	/* Place e-SSA pis. This will add additional "def" points, so it must
955 	 * happen before def propagation. */
956 	place_essa_pis(arena, script, op_array, build_flags, ssa, &dfg);
957 
958 	/* SSA construction, Step 1: Propagate "def" sets in merge points */
959 	do {
960 		changed = 0;
961 		for (j = 0; j < blocks_count; j++) {
962 			zend_bitset def_j = def + j * set_size, phi_j = phi + j * set_size;
963 			if ((blocks[j].flags & ZEND_BB_REACHABLE) == 0) {
964 				continue;
965 			}
966 			if (blocks[j].predecessors_count > 1) {
967 				if (blocks[j].flags & ZEND_BB_IRREDUCIBLE_LOOP) {
968 					/* Prevent any values from flowing into irreducible loops by
969 					   replacing all incoming values with explicit phis.  The
970 					   register allocator depends on this property.  */
971 					zend_bitset_union(phi_j, in + (j * set_size), set_size);
972 				} else {
973 					for (k = 0; k < blocks[j].predecessors_count; k++) {
974 						i = ssa->cfg.predecessors[blocks[j].predecessor_offset + k];
975 						while (i != -1 && i != blocks[j].idom) {
976 							zend_bitset_union_with_intersection(
977 								phi_j, phi_j, def + (i * set_size), in + (j * set_size), set_size);
978 							i = blocks[i].idom;
979 						}
980 					}
981 				}
982 				if (!zend_bitset_subset(phi_j, def_j, set_size)) {
983 					zend_bitset_union(def_j, phi_j, set_size);
984 					changed = 1;
985 				}
986 			}
987 		}
988 	} while (changed);
989 
990 	/* SSA construction, Step 2: Phi placement based on Dominance Frontiers */
991 	var = do_alloca(sizeof(int) * (op_array->last_var + op_array->T), var_use_heap);
992 	if (!var) {
993 		free_alloca(dfg.tmp, dfg_use_heap);
994 		return FAILURE;
995 	}
996 
997 	for (j = 0; j < blocks_count; j++) {
998 		if ((blocks[j].flags & ZEND_BB_REACHABLE) == 0) {
999 			continue;
1000 		}
1001 		if (!zend_bitset_empty(phi + j * set_size, set_size)) {
1002 			ZEND_BITSET_REVERSE_FOREACH(phi + j * set_size, set_size, i) {
1003 				zend_ssa_phi *phi = zend_arena_calloc(arena, 1,
1004 					ZEND_MM_ALIGNED_SIZE(sizeof(zend_ssa_phi)) +
1005 					ZEND_MM_ALIGNED_SIZE(sizeof(int) * blocks[j].predecessors_count) +
1006 					sizeof(void*) * blocks[j].predecessors_count);
1007 
1008 				phi->sources = (int*)(((char*)phi) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_ssa_phi)));
1009 				memset(phi->sources, 0xff, sizeof(int) * blocks[j].predecessors_count);
1010 				phi->use_chains = (zend_ssa_phi**)(((char*)phi->sources) + ZEND_MM_ALIGNED_SIZE(sizeof(int) * ssa->cfg.blocks[j].predecessors_count));
1011 
1012 				phi->pi = -1;
1013 				phi->var = i;
1014 				phi->ssa_var = -1;
1015 
1016 				/* Place phis after pis */
1017 				{
1018 					zend_ssa_phi **pp = &ssa_blocks[j].phis;
1019 					while (*pp) {
1020 						if ((*pp)->pi < 0) {
1021 							break;
1022 						}
1023 						pp = &(*pp)->next;
1024 					}
1025 					phi->next = *pp;
1026 					*pp = phi;
1027 				}
1028 			} ZEND_BITSET_FOREACH_END();
1029 		}
1030 	}
1031 
1032 	if (build_flags & ZEND_SSA_DEBUG_PHI_PLACEMENT) {
1033 		zend_dump_phi_placement(op_array, ssa);
1034 	}
1035 
1036 	/* SSA construction, Step 3: Renaming */
1037 	ssa->ops = zend_arena_calloc(arena, op_array->last, sizeof(zend_ssa_op));
1038 	memset(ssa->ops, 0xff, op_array->last * sizeof(zend_ssa_op));
1039 	memset(var + op_array->last_var, 0xff, op_array->T * sizeof(int));
1040 	/* Create uninitialized SSA variables for each CV */
1041 	for (j = 0; j < op_array->last_var; j++) {
1042 		var[j] = j;
1043 	}
1044 	ssa->vars_count = op_array->last_var;
1045 	if (zend_ssa_rename(op_array, build_flags, ssa, var, 0) != SUCCESS) {
1046 		free_alloca(var, var_use_heap);
1047 		free_alloca(dfg.tmp, dfg_use_heap);
1048 		return FAILURE;
1049 	}
1050 
1051 	free_alloca(var, var_use_heap);
1052 	free_alloca(dfg.tmp, dfg_use_heap);
1053 
1054 	return SUCCESS;
1055 }
1056 /* }}} */
1057 
zend_ssa_compute_use_def_chains(zend_arena ** arena,const zend_op_array * op_array,zend_ssa * ssa)1058 ZEND_API int zend_ssa_compute_use_def_chains(zend_arena **arena, const zend_op_array *op_array, zend_ssa *ssa) /* {{{ */
1059 {
1060 	zend_ssa_var *ssa_vars;
1061 	int i;
1062 
1063 	if (!ssa->vars) {
1064 		ssa->vars = zend_arena_calloc(arena, ssa->vars_count, sizeof(zend_ssa_var));
1065 	}
1066 	ssa_vars = ssa->vars;
1067 
1068 	for (i = 0; i < op_array->last_var; i++) {
1069 		ssa_vars[i].var = i;
1070 		ssa_vars[i].scc = -1;
1071 		ssa_vars[i].definition = -1;
1072 		ssa_vars[i].use_chain = -1;
1073 	}
1074 	for (i = op_array->last_var; i < ssa->vars_count; i++) {
1075 		ssa_vars[i].var = -1;
1076 		ssa_vars[i].scc = -1;
1077 		ssa_vars[i].definition = -1;
1078 		ssa_vars[i].use_chain = -1;
1079 	}
1080 
1081 	for (i = op_array->last - 1; i >= 0; i--) {
1082 		zend_ssa_op *op = ssa->ops + i;
1083 
1084 		if (op->op1_use >= 0) {
1085 			op->op1_use_chain = ssa_vars[op->op1_use].use_chain;
1086 			ssa_vars[op->op1_use].use_chain = i;
1087 		}
1088 		if (op->op2_use >= 0 && op->op2_use != op->op1_use) {
1089 			op->op2_use_chain = ssa_vars[op->op2_use].use_chain;
1090 			ssa_vars[op->op2_use].use_chain = i;
1091 		}
1092 		if (op->result_use >= 0 && op->result_use != op->op1_use && op->result_use != op->op2_use) {
1093 			op->res_use_chain = ssa_vars[op->result_use].use_chain;
1094 			ssa_vars[op->result_use].use_chain = i;
1095 		}
1096 		if (op->op1_def >= 0) {
1097 			ssa_vars[op->op1_def].var = EX_VAR_TO_NUM(op_array->opcodes[i].op1.var);
1098 			ssa_vars[op->op1_def].definition = i;
1099 		}
1100 		if (op->op2_def >= 0) {
1101 			ssa_vars[op->op2_def].var = EX_VAR_TO_NUM(op_array->opcodes[i].op2.var);
1102 			ssa_vars[op->op2_def].definition = i;
1103 		}
1104 		if (op->result_def >= 0) {
1105 			ssa_vars[op->result_def].var = EX_VAR_TO_NUM(op_array->opcodes[i].result.var);
1106 			ssa_vars[op->result_def].definition = i;
1107 		}
1108 	}
1109 
1110 	for (i = 0; i < ssa->cfg.blocks_count; i++) {
1111 		zend_ssa_phi *phi = ssa->blocks[i].phis;
1112 		while (phi) {
1113 			phi->block = i;
1114 			ssa_vars[phi->ssa_var].var = phi->var;
1115 			ssa_vars[phi->ssa_var].definition_phi = phi;
1116 			if (phi->pi >= 0) {
1117 				zend_ssa_phi *p;
1118 
1119 				ZEND_ASSERT(phi->sources[0] >= 0);
1120 				p = ssa_vars[phi->sources[0]].phi_use_chain;
1121 				while (p && p != phi) {
1122 					p = zend_ssa_next_use_phi(ssa, phi->sources[0], p);
1123 				}
1124 				if (!p) {
1125 					phi->use_chains[0] = ssa_vars[phi->sources[0]].phi_use_chain;
1126 					ssa_vars[phi->sources[0]].phi_use_chain = phi;
1127 				}
1128 				if (phi->has_range_constraint) {
1129 					/* min and max variables can't be used together */
1130 					zend_ssa_range_constraint *constraint = &phi->constraint.range;
1131 					if (constraint->min_ssa_var >= 0) {
1132 						phi->sym_use_chain = ssa_vars[constraint->min_ssa_var].sym_use_chain;
1133 						ssa_vars[constraint->min_ssa_var].sym_use_chain = phi;
1134 					} else if (constraint->max_ssa_var >= 0) {
1135 						phi->sym_use_chain = ssa_vars[constraint->max_ssa_var].sym_use_chain;
1136 						ssa_vars[constraint->max_ssa_var].sym_use_chain = phi;
1137 					}
1138 				}
1139 			} else {
1140 				int j;
1141 
1142 				for (j = 0; j < ssa->cfg.blocks[i].predecessors_count; j++) {
1143 					zend_ssa_phi *p;
1144 
1145 					ZEND_ASSERT(phi->sources[j] >= 0);
1146 					p = ssa_vars[phi->sources[j]].phi_use_chain;
1147 					while (p && p != phi) {
1148 						p = zend_ssa_next_use_phi(ssa, phi->sources[j], p);
1149 					}
1150 					if (!p) {
1151 						phi->use_chains[j] = ssa_vars[phi->sources[j]].phi_use_chain;
1152 						ssa_vars[phi->sources[j]].phi_use_chain = phi;
1153 					}
1154 				}
1155 			}
1156 			phi = phi->next;
1157 		}
1158 	}
1159 
1160 	/* Mark indirectly accessed variables */
1161 	for (i = 0; i < op_array->last_var; i++) {
1162 		if ((ssa->cfg.flags & ZEND_FUNC_INDIRECT_VAR_ACCESS)) {
1163 			ssa_vars[i].alias = SYMTABLE_ALIAS;
1164 		} else if (zend_string_equals_literal(op_array->vars[i], "http_response_header")) {
1165 			ssa_vars[i].alias = HTTP_RESPONSE_HEADER_ALIAS;
1166 		}
1167 	}
1168 	for (i = op_array->last_var; i < ssa->vars_count; i++) {
1169 		if (ssa_vars[i].var < op_array->last_var) {
1170 			ssa_vars[i].alias = ssa_vars[ssa_vars[i].var].alias;
1171 		}
1172 	}
1173 
1174 	return SUCCESS;
1175 }
1176 /* }}} */
1177 
zend_ssa_unlink_use_chain(zend_ssa * ssa,int op,int var)1178 int zend_ssa_unlink_use_chain(zend_ssa *ssa, int op, int var) /* {{{ */
1179 {
1180 	if (ssa->vars[var].use_chain == op) {
1181 		ssa->vars[var].use_chain = zend_ssa_next_use(ssa->ops, var, op);
1182 		return 1;
1183 	} else {
1184 		int use = ssa->vars[var].use_chain;
1185 
1186 		while (use >= 0) {
1187 			if (ssa->ops[use].result_use == var) {
1188 				if (ssa->ops[use].res_use_chain == op) {
1189 					ssa->ops[use].res_use_chain = zend_ssa_next_use(ssa->ops, var, op);
1190 					return 1;
1191 				} else {
1192 					use = ssa->ops[use].res_use_chain;
1193 				}
1194 			} else if (ssa->ops[use].op1_use == var) {
1195 				if (ssa->ops[use].op1_use_chain == op) {
1196 					ssa->ops[use].op1_use_chain = zend_ssa_next_use(ssa->ops, var, op);
1197 					return 1;
1198 				} else {
1199 					use = ssa->ops[use].op1_use_chain;
1200 				}
1201 			} else if (ssa->ops[use].op2_use == var) {
1202 				if (ssa->ops[use].op2_use_chain == op) {
1203 					ssa->ops[use].op2_use_chain = zend_ssa_next_use(ssa->ops, var, op);
1204 					return 1;
1205 				} else {
1206 					use = ssa->ops[use].op2_use_chain;
1207 				}
1208 			} else {
1209 				break;
1210 			}
1211 		}
1212 		/* something wrong */
1213 		ZEND_UNREACHABLE();
1214 		return 0;
1215 	}
1216 }
1217 /* }}} */
1218 
zend_ssa_remove_instr(zend_ssa * ssa,zend_op * opline,zend_ssa_op * ssa_op)1219 void zend_ssa_remove_instr(zend_ssa *ssa, zend_op *opline, zend_ssa_op *ssa_op) /* {{{ */
1220 {
1221 	if (ssa_op->result_use >= 0) {
1222 		zend_ssa_unlink_use_chain(ssa, ssa_op - ssa->ops, ssa_op->result_use);
1223 		ssa_op->result_use = -1;
1224 		ssa_op->res_use_chain = -1;
1225 	}
1226 	if (ssa_op->op1_use >= 0) {
1227 		if (ssa_op->op1_use != ssa_op->op2_use) {
1228 			zend_ssa_unlink_use_chain(ssa, ssa_op - ssa->ops, ssa_op->op1_use);
1229 		} else {
1230 			ssa_op->op2_use_chain = ssa_op->op1_use_chain;
1231 		}
1232 		ssa_op->op1_use = -1;
1233 		ssa_op->op1_use_chain = -1;
1234 	}
1235 	if (ssa_op->op2_use >= 0) {
1236 		zend_ssa_unlink_use_chain(ssa, ssa_op - ssa->ops, ssa_op->op2_use);
1237 		ssa_op->op2_use = -1;
1238 		ssa_op->op2_use_chain = -1;
1239 	}
1240 
1241 	/* We let the caller make sure that all defs are gone */
1242 	ZEND_ASSERT(ssa_op->result_def == -1);
1243 	ZEND_ASSERT(ssa_op->op1_def == -1);
1244 	ZEND_ASSERT(ssa_op->op2_def == -1);
1245 
1246 	MAKE_NOP(opline);
1247 }
1248 /* }}} */
1249 
zend_ssa_next_use_phi_ptr(zend_ssa * ssa,int var,zend_ssa_phi * p)1250 static inline zend_ssa_phi **zend_ssa_next_use_phi_ptr(zend_ssa *ssa, int var, zend_ssa_phi *p) /* {{{ */
1251 {
1252 	if (p->pi >= 0) {
1253 		return &p->use_chains[0];
1254 	} else {
1255 		int j;
1256 		for (j = 0; j < ssa->cfg.blocks[p->block].predecessors_count; j++) {
1257 			if (p->sources[j] == var) {
1258 				return &p->use_chains[j];
1259 			}
1260 		}
1261 	}
1262 	ZEND_UNREACHABLE();
1263 	return NULL;
1264 }
1265 /* }}} */
1266 
1267 /* May be called even if source is not used in the phi (useful when removing uses in a phi
1268  * 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)1269 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) /* {{{ */
1270 {
1271 	zend_ssa_phi **cur = &ssa->vars[source].phi_use_chain;
1272 	while (*cur && *cur != phi) {
1273 		cur = zend_ssa_next_use_phi_ptr(ssa, source, *cur);
1274 	}
1275 	if (*cur) {
1276 		*cur = next_use_phi;
1277 	}
1278 }
1279 /* }}} */
1280 
zend_ssa_remove_uses_of_phi_sources(zend_ssa * ssa,zend_ssa_phi * phi)1281 static void zend_ssa_remove_uses_of_phi_sources(zend_ssa *ssa, zend_ssa_phi *phi) /* {{{ */
1282 {
1283 	int source;
1284 	FOREACH_PHI_SOURCE(phi, source) {
1285 		zend_ssa_remove_use_of_phi_source(ssa, phi, source, zend_ssa_next_use_phi(ssa, source, phi));
1286 	} FOREACH_PHI_SOURCE_END();
1287 }
1288 /* }}} */
1289 
zend_ssa_remove_phi_from_block(zend_ssa * ssa,zend_ssa_phi * phi)1290 static void zend_ssa_remove_phi_from_block(zend_ssa *ssa, zend_ssa_phi *phi) /* {{{ */
1291 {
1292 	zend_ssa_block *block = &ssa->blocks[phi->block];
1293 	zend_ssa_phi **cur = &block->phis;
1294 	while (*cur != phi) {
1295 		ZEND_ASSERT(*cur != NULL);
1296 		cur = &(*cur)->next;
1297 	}
1298 	*cur = (*cur)->next;
1299 }
1300 /* }}} */
1301 
zend_ssa_remove_defs_of_instr(zend_ssa * ssa,zend_ssa_op * ssa_op)1302 void zend_ssa_remove_defs_of_instr(zend_ssa *ssa, zend_ssa_op *ssa_op) /* {{{ */
1303 {
1304 	if (ssa_op->op1_def >= 0) {
1305 		zend_ssa_remove_uses_of_var(ssa, ssa_op->op1_def);
1306 		zend_ssa_remove_op1_def(ssa, ssa_op);
1307 	}
1308 	if (ssa_op->op2_def >= 0) {
1309 		zend_ssa_remove_uses_of_var(ssa, ssa_op->op2_def);
1310 		zend_ssa_remove_op2_def(ssa, ssa_op);
1311 	}
1312 	if (ssa_op->result_def >= 0) {
1313 		zend_ssa_remove_uses_of_var(ssa, ssa_op->result_def);
1314 		zend_ssa_remove_result_def(ssa, ssa_op);
1315 	}
1316 }
1317 /* }}} */
1318 
zend_ssa_remove_phi_source(zend_ssa * ssa,zend_ssa_phi * phi,int pred_offset,int predecessors_count)1319 static inline void zend_ssa_remove_phi_source(zend_ssa *ssa, zend_ssa_phi *phi, int pred_offset, int predecessors_count) /* {{{ */
1320 {
1321 	int j, var_num = phi->sources[pred_offset];
1322 	zend_ssa_phi *next_phi = phi->use_chains[pred_offset];
1323 
1324 	predecessors_count--;
1325 	if (pred_offset < predecessors_count) {
1326 		memmove(phi->sources + pred_offset, phi->sources + pred_offset + 1, (predecessors_count - pred_offset) * sizeof(uint32_t));
1327 		memmove(phi->use_chains + pred_offset, phi->use_chains + pred_offset + 1, (predecessors_count - pred_offset) * sizeof(zend_ssa_phi*));
1328 	}
1329 
1330 	/* Check if they same var is used in a different phi operand as well, in this case we don't
1331 	 * need to adjust the use chain (but may have to move the next pointer). */
1332 	for (j = 0; j < predecessors_count; j++) {
1333 		if (phi->sources[j] == var_num) {
1334 			if (j < pred_offset) {
1335 				ZEND_ASSERT(next_phi == NULL);
1336 			} else if (j >= pred_offset) {
1337 				phi->use_chains[j] = next_phi;
1338 			}
1339 			return;
1340 		}
1341 	}
1342 
1343 	/* Variable only used in one operand, remove the phi from the use chain. */
1344 	zend_ssa_remove_use_of_phi_source(ssa, phi, var_num, next_phi);
1345 }
1346 /* }}} */
1347 
zend_ssa_remove_phi(zend_ssa * ssa,zend_ssa_phi * phi)1348 void zend_ssa_remove_phi(zend_ssa *ssa, zend_ssa_phi *phi) /* {{{ */
1349 {
1350 	ZEND_ASSERT(phi->ssa_var >= 0);
1351 	ZEND_ASSERT(ssa->vars[phi->ssa_var].use_chain < 0
1352 		&& ssa->vars[phi->ssa_var].phi_use_chain == NULL);
1353 	zend_ssa_remove_uses_of_phi_sources(ssa, phi);
1354 	zend_ssa_remove_phi_from_block(ssa, phi);
1355 	ssa->vars[phi->ssa_var].definition_phi = NULL;
1356 	phi->ssa_var = -1;
1357 }
1358 /* }}} */
1359 
zend_ssa_remove_uses_of_var(zend_ssa * ssa,int var_num)1360 void zend_ssa_remove_uses_of_var(zend_ssa *ssa, int var_num) /* {{{ */
1361 {
1362 	zend_ssa_var *var = &ssa->vars[var_num];
1363 	zend_ssa_phi *phi;
1364 	int use;
1365 	FOREACH_PHI_USE(var, phi) {
1366 		int i, end = NUM_PHI_SOURCES(phi);
1367 		for (i = 0; i < end; i++) {
1368 			if (phi->sources[i] == var_num) {
1369 				phi->use_chains[i] = NULL;
1370 			}
1371 		}
1372 	} FOREACH_PHI_USE_END();
1373 	var->phi_use_chain = NULL;
1374 	FOREACH_USE(var, use) {
1375 		zend_ssa_op *ssa_op = &ssa->ops[use];
1376 		if (ssa_op->op1_use == var_num) {
1377 			ssa_op->op1_use = -1;
1378 			ssa_op->op1_use_chain = -1;
1379 		}
1380 		if (ssa_op->op2_use == var_num) {
1381 			ssa_op->op2_use = -1;
1382 			ssa_op->op2_use_chain = -1;
1383 		}
1384 		if (ssa_op->result_use == var_num) {
1385 			ssa_op->result_use = -1;
1386 			ssa_op->res_use_chain = -1;
1387 		}
1388 	} FOREACH_USE_END();
1389 	var->use_chain = -1;
1390 }
1391 /* }}} */
1392 
zend_ssa_remove_predecessor(zend_ssa * ssa,int from,int to)1393 void zend_ssa_remove_predecessor(zend_ssa *ssa, int from, int to) /* {{{ */
1394 {
1395 	zend_basic_block *next_block = &ssa->cfg.blocks[to];
1396 	zend_ssa_block *next_ssa_block = &ssa->blocks[to];
1397 	zend_ssa_phi *phi;
1398 	int j;
1399 
1400 	/* Find at which predecessor offset this block is referenced */
1401 	int pred_offset = -1;
1402 	int *predecessors = &ssa->cfg.predecessors[next_block->predecessor_offset];
1403 
1404 	for (j = 0; j < next_block->predecessors_count; j++) {
1405 		if (predecessors[j] == from) {
1406 			pred_offset = j;
1407 			break;
1408 		}
1409 	}
1410 
1411 	/* If there are duplicate successors, the predecessors may have been removed in
1412 	 * a previous iteration already. */
1413 	if (pred_offset == -1) {
1414 		return;
1415 	}
1416 
1417 	/* For phis in successor blocks, remove the operands associated with this block */
1418 	for (phi = next_ssa_block->phis; phi; phi = phi->next) {
1419 		if (phi->pi >= 0) {
1420 			if (phi->pi == from) {
1421 				zend_ssa_rename_var_uses(ssa, phi->ssa_var, phi->sources[0], /* update_types */ 0);
1422 				zend_ssa_remove_phi(ssa, phi);
1423 			}
1424 		} else {
1425 			ZEND_ASSERT(phi->sources[pred_offset] >= 0);
1426 			zend_ssa_remove_phi_source(ssa, phi, pred_offset, next_block->predecessors_count);
1427 		}
1428 	}
1429 
1430 	/* Remove this predecessor */
1431 	next_block->predecessors_count--;
1432 	if (pred_offset < next_block->predecessors_count) {
1433 		predecessors = &ssa->cfg.predecessors[next_block->predecessor_offset + pred_offset];
1434 		memmove(predecessors, predecessors + 1, (next_block->predecessors_count - pred_offset) * sizeof(uint32_t));
1435 	}
1436 }
1437 /* }}} */
1438 
zend_ssa_remove_block(zend_op_array * op_array,zend_ssa * ssa,int i)1439 void zend_ssa_remove_block(zend_op_array *op_array, zend_ssa *ssa, int i) /* {{{ */
1440 {
1441 	zend_basic_block *block = &ssa->cfg.blocks[i];
1442 	zend_ssa_block *ssa_block = &ssa->blocks[i];
1443 	zend_ssa_phi *phi;
1444 	int j;
1445 
1446 	block->flags &= ~ZEND_BB_REACHABLE;
1447 
1448 	/* Removes phis in this block */
1449 	for (phi = ssa_block->phis; phi; phi = phi->next) {
1450 		zend_ssa_remove_uses_of_var(ssa, phi->ssa_var);
1451 		zend_ssa_remove_phi(ssa, phi);
1452 	}
1453 
1454 	/* Remove instructions in this block */
1455 	for (j = block->start; j < block->start + block->len; j++) {
1456 		if (op_array->opcodes[j].opcode == ZEND_NOP) {
1457 			continue;
1458 		}
1459 
1460 		zend_ssa_remove_defs_of_instr(ssa, &ssa->ops[j]);
1461 		zend_ssa_remove_instr(ssa, &op_array->opcodes[j], &ssa->ops[j]);
1462 	}
1463 
1464 	zend_ssa_remove_block_from_cfg(ssa, i);
1465 }
1466 /* }}} */
1467 
zend_ssa_remove_block_from_cfg(zend_ssa * ssa,int i)1468 void zend_ssa_remove_block_from_cfg(zend_ssa *ssa, int i) /* {{{ */
1469 {
1470 	zend_basic_block *block = &ssa->cfg.blocks[i];
1471 	int *predecessors;
1472 	int j, s;
1473 
1474 	for (s = 0; s < block->successors_count; s++) {
1475 		zend_ssa_remove_predecessor(ssa, i, block->successors[s]);
1476 	}
1477 
1478 	/* Remove successors of predecessors */
1479 	predecessors = &ssa->cfg.predecessors[block->predecessor_offset];
1480 	for (j = 0; j < block->predecessors_count; j++) {
1481 		if (predecessors[j] >= 0) {
1482 			zend_basic_block *prev_block = &ssa->cfg.blocks[predecessors[j]];
1483 
1484 			for (s = 0; s < prev_block->successors_count; s++) {
1485 				if (prev_block->successors[s] == i) {
1486 					memmove(prev_block->successors + s,
1487 							prev_block->successors + s + 1,
1488 							sizeof(int) * (prev_block->successors_count - s - 1));
1489 					prev_block->successors_count--;
1490 					s--;
1491 				}
1492 			}
1493 		}
1494 	}
1495 
1496 	block->successors_count = 0;
1497 	block->predecessors_count = 0;
1498 
1499 	/* Remove from dominators tree */
1500 	if (block->idom >= 0) {
1501 		j = ssa->cfg.blocks[block->idom].children;
1502 		if (j == i) {
1503 			ssa->cfg.blocks[block->idom].children = block->next_child;
1504 		} else if (j >= 0) {
1505 			while (ssa->cfg.blocks[j].next_child >= 0) {
1506 				if (ssa->cfg.blocks[j].next_child == i) {
1507 					ssa->cfg.blocks[j].next_child = block->next_child;
1508 					break;
1509 				}
1510 				j = ssa->cfg.blocks[j].next_child;
1511 			}
1512 		}
1513 	}
1514 	block->idom = -1;
1515 	block->level = -1;
1516 	block->children = -1;
1517 	block->next_child = -1;
1518 }
1519 /* }}} */
1520 
propagate_phi_type_widening(zend_ssa * ssa,int var)1521 static void propagate_phi_type_widening(zend_ssa *ssa, int var) /* {{{ */
1522 {
1523 	zend_ssa_phi *phi;
1524 	FOREACH_PHI_USE(&ssa->vars[var], phi) {
1525 		if (ssa->var_info[var].type & ~ssa->var_info[phi->ssa_var].type) {
1526 			ssa->var_info[phi->ssa_var].type |= ssa->var_info[var].type;
1527 			propagate_phi_type_widening(ssa, phi->ssa_var);
1528 		}
1529 	} FOREACH_PHI_USE_END();
1530 }
1531 /* }}} */
1532 
zend_ssa_rename_var_uses(zend_ssa * ssa,int old,int new,bool update_types)1533 void zend_ssa_rename_var_uses(zend_ssa *ssa, int old, int new, bool update_types) /* {{{ */
1534 {
1535 	zend_ssa_var *old_var = &ssa->vars[old];
1536 	zend_ssa_var *new_var = &ssa->vars[new];
1537 	int use;
1538 	zend_ssa_phi *phi;
1539 
1540 	ZEND_ASSERT(old >= 0 && new >= 0);
1541 	ZEND_ASSERT(old != new);
1542 
1543 	/* Only a no_val is both variables are */
1544 	new_var->no_val &= old_var->no_val;
1545 
1546 	/* Update ssa_op use chains */
1547 	FOREACH_USE(old_var, use) {
1548 		zend_ssa_op *ssa_op = &ssa->ops[use];
1549 
1550 		/* If the op already uses the new var, don't add the op to the use
1551 		 * list again. Instead move the use_chain to the correct operand. */
1552 		bool add_to_use_chain = 1;
1553 		if (ssa_op->result_use == new) {
1554 			add_to_use_chain = 0;
1555 		} else if (ssa_op->op1_use == new) {
1556 			if (ssa_op->result_use == old) {
1557 				ssa_op->res_use_chain = ssa_op->op1_use_chain;
1558 				ssa_op->op1_use_chain = -1;
1559 			}
1560 			add_to_use_chain = 0;
1561 		} else if (ssa_op->op2_use == new) {
1562 			if (ssa_op->result_use == old) {
1563 				ssa_op->res_use_chain = ssa_op->op2_use_chain;
1564 				ssa_op->op2_use_chain = -1;
1565 			} else if (ssa_op->op1_use == old) {
1566 				ssa_op->op1_use_chain = ssa_op->op2_use_chain;
1567 				ssa_op->op2_use_chain = -1;
1568 			}
1569 			add_to_use_chain = 0;
1570 		}
1571 
1572 		/* Perform the actual renaming */
1573 		if (ssa_op->result_use == old) {
1574 			ssa_op->result_use = new;
1575 		}
1576 		if (ssa_op->op1_use == old) {
1577 			ssa_op->op1_use = new;
1578 		}
1579 		if (ssa_op->op2_use == old) {
1580 			ssa_op->op2_use = new;
1581 		}
1582 
1583 		/* Add op to use chain of new var (if it isn't already). We use the
1584 		 * first use chain of (result, op1, op2) that has the new variable. */
1585 		if (add_to_use_chain) {
1586 			if (ssa_op->result_use == new) {
1587 				ssa_op->res_use_chain = new_var->use_chain;
1588 				new_var->use_chain = use;
1589 			} else if (ssa_op->op1_use == new) {
1590 				ssa_op->op1_use_chain = new_var->use_chain;
1591 				new_var->use_chain = use;
1592 			} else {
1593 				ZEND_ASSERT(ssa_op->op2_use == new);
1594 				ssa_op->op2_use_chain = new_var->use_chain;
1595 				new_var->use_chain = use;
1596 			}
1597 		}
1598 	} FOREACH_USE_END();
1599 	old_var->use_chain = -1;
1600 
1601 	/* Update phi use chains */
1602 	FOREACH_PHI_USE(old_var, phi) {
1603 		int j;
1604 		bool after_first_new_source = 0;
1605 
1606 		/* If the phi already uses the new var, find its use chain, as we may
1607 		 * need to move it to a different source operand. */
1608 		zend_ssa_phi **existing_use_chain_ptr = NULL;
1609 		for (j = 0; j < ssa->cfg.blocks[phi->block].predecessors_count; j++) {
1610 			if (phi->sources[j] == new) {
1611 				existing_use_chain_ptr = &phi->use_chains[j];
1612 				break;
1613 			}
1614 		}
1615 
1616 		for (j = 0; j < ssa->cfg.blocks[phi->block].predecessors_count; j++) {
1617 			if (phi->sources[j] == new) {
1618 				after_first_new_source = 1;
1619 			} else if (phi->sources[j] == old) {
1620 				phi->sources[j] = new;
1621 
1622 				/* Either move existing use chain to this source, or add the phi
1623 				 * to the phi use chain of the new variables. Do this only once. */
1624 				if (!after_first_new_source) {
1625 					if (existing_use_chain_ptr) {
1626 						phi->use_chains[j] = *existing_use_chain_ptr;
1627 						*existing_use_chain_ptr = NULL;
1628 					} else {
1629 						phi->use_chains[j] = new_var->phi_use_chain;
1630 						new_var->phi_use_chain = phi;
1631 					}
1632 					after_first_new_source = 1;
1633 				} else {
1634 					phi->use_chains[j] = NULL;
1635 				}
1636 			}
1637 		}
1638 
1639 		/* Make sure phi result types are not incorrectly narrow after renaming.
1640 		 * This should not normally happen, but can occur if we DCE an assignment
1641 		 * or unset and there is an improper phi-indirected use lateron. */
1642 		// TODO Alternatively we could rerun type-inference after DCE
1643 		if (update_types && (ssa->var_info[new].type & ~ssa->var_info[phi->ssa_var].type)) {
1644 			ssa->var_info[phi->ssa_var].type |= ssa->var_info[new].type;
1645 			propagate_phi_type_widening(ssa, phi->ssa_var);
1646 		}
1647 	} FOREACH_PHI_USE_END();
1648 	old_var->phi_use_chain = NULL;
1649 }
1650 /* }}} */
1651