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