/src/php-src/Zend/Optimizer/dfa_pass.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Zend OPcache | |
4 | | +----------------------------------------------------------------------+ |
5 | | | Copyright © The PHP Group and Contributors. | |
6 | | +----------------------------------------------------------------------+ |
7 | | | This source file is subject to the Modified BSD License that is | |
8 | | | bundled with this package in the file LICENSE, and is available | |
9 | | | through the World Wide Web at <https://www.php.net/license/>. | |
10 | | | | |
11 | | | SPDX-License-Identifier: BSD-3-Clause | |
12 | | +----------------------------------------------------------------------+ |
13 | | | Authors: Dmitry Stogov <dmitry@php.net> | |
14 | | +----------------------------------------------------------------------+ |
15 | | */ |
16 | | |
17 | | #include "Optimizer/zend_optimizer.h" |
18 | | #include "Optimizer/zend_optimizer_internal.h" |
19 | | #include "zend_API.h" |
20 | | #include "zend_constants.h" |
21 | | #include "zend_execute.h" |
22 | | #include "zend_vm.h" |
23 | | #include "zend_bitset.h" |
24 | | #include "zend_cfg.h" |
25 | | #include "zend_ssa.h" |
26 | | #include "zend_func_info.h" |
27 | | #include "zend_call_graph.h" |
28 | | #include "zend_inference.h" |
29 | | #include "zend_dump.h" |
30 | | |
31 | | #ifndef ZEND_DEBUG_DFA |
32 | | # define ZEND_DEBUG_DFA ZEND_DEBUG |
33 | | #endif |
34 | | |
35 | | #if ZEND_DEBUG_DFA |
36 | | # include "ssa_integrity.c" |
37 | | #endif |
38 | | |
39 | | zend_result zend_dfa_analyze_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx, zend_ssa *ssa) |
40 | 47.7k | { |
41 | 47.7k | uint32_t build_flags; |
42 | | |
43 | 47.7k | if (op_array->last_try_catch) { |
44 | | /* TODO: we can't analyze functions with try/catch/finally ??? */ |
45 | 9.06k | return FAILURE; |
46 | 9.06k | } |
47 | | |
48 | | /* Build SSA */ |
49 | 38.6k | memset(ssa, 0, sizeof(zend_ssa)); |
50 | | |
51 | 38.6k | zend_build_cfg(&ctx->arena, op_array, ZEND_CFG_NO_ENTRY_PREDECESSORS, &ssa->cfg); |
52 | | |
53 | 38.6k | if ((ssa->cfg.flags & ZEND_FUNC_INDIRECT_VAR_ACCESS)) { |
54 | | /* TODO: we can't analyze functions with indirect variable access ??? */ |
55 | 1.52k | return FAILURE; |
56 | 1.52k | } |
57 | | |
58 | 37.1k | zend_cfg_build_predecessors(&ctx->arena, &ssa->cfg); |
59 | | |
60 | 37.1k | if (ctx->debug_level & ZEND_DUMP_DFA_CFG) { |
61 | 0 | zend_dump_op_array(op_array, ZEND_DUMP_CFG, "dfa cfg", &ssa->cfg); |
62 | 0 | } |
63 | | |
64 | | /* Compute Dominators Tree */ |
65 | 37.1k | zend_cfg_compute_dominators_tree(op_array, &ssa->cfg); |
66 | | |
67 | | /* Identify reducible and irreducible loops */ |
68 | 37.1k | zend_cfg_identify_loops(op_array, &ssa->cfg); |
69 | | |
70 | 37.1k | if (ctx->debug_level & ZEND_DUMP_DFA_DOMINATORS) { |
71 | 0 | zend_dump_dominators(op_array, &ssa->cfg); |
72 | 0 | } |
73 | | |
74 | 37.1k | build_flags = 0; |
75 | 37.1k | if (ctx->debug_level & ZEND_DUMP_DFA_LIVENESS) { |
76 | 0 | build_flags |= ZEND_SSA_DEBUG_LIVENESS; |
77 | 0 | } |
78 | 37.1k | if (ctx->debug_level & ZEND_DUMP_DFA_PHI) { |
79 | 0 | build_flags |= ZEND_SSA_DEBUG_PHI_PLACEMENT; |
80 | 0 | } |
81 | 37.1k | if (zend_build_ssa(&ctx->arena, ctx->script, op_array, build_flags, ssa) == FAILURE) { |
82 | 0 | return FAILURE; |
83 | 0 | } |
84 | | |
85 | 37.1k | if (ctx->debug_level & ZEND_DUMP_DFA_SSA) { |
86 | 0 | zend_dump_op_array(op_array, ZEND_DUMP_SSA, "dfa ssa", ssa); |
87 | 0 | } |
88 | | |
89 | | |
90 | 37.1k | zend_ssa_compute_use_def_chains(&ctx->arena, op_array, ssa); |
91 | | |
92 | 37.1k | zend_ssa_find_false_dependencies(op_array, ssa); |
93 | | |
94 | 37.1k | zend_ssa_find_sccs(op_array, ssa); |
95 | | |
96 | 37.1k | if (zend_ssa_inference(&ctx->arena, op_array, ctx->script, ssa, ctx->optimization_level) == FAILURE) { |
97 | 0 | return FAILURE; |
98 | 0 | } |
99 | | |
100 | 37.1k | if (zend_ssa_escape_analysis(ctx->script, op_array, ssa) == FAILURE) { |
101 | 0 | return FAILURE; |
102 | 0 | } |
103 | | |
104 | 37.1k | if (ctx->debug_level & ZEND_DUMP_DFA_SSA_VARS) { |
105 | 0 | zend_dump_ssa_variables(op_array, ssa, 0); |
106 | 0 | } |
107 | | |
108 | 37.1k | return SUCCESS; |
109 | 37.1k | } |
110 | | |
111 | | static void zend_ssa_remove_nops(zend_op_array *op_array, zend_ssa *ssa, zend_optimizer_ctx *ctx) |
112 | 3.85k | { |
113 | 3.85k | zend_basic_block *blocks = ssa->cfg.blocks; |
114 | 3.85k | zend_basic_block *blocks_end = blocks + ssa->cfg.blocks_count; |
115 | 3.85k | zend_basic_block *b; |
116 | 3.85k | zend_func_info *func_info; |
117 | 3.85k | int j; |
118 | 3.85k | uint32_t i = 0; |
119 | 3.85k | uint32_t target = 0; |
120 | 3.85k | uint32_t *shiftlist; |
121 | 3.85k | ALLOCA_FLAG(use_heap); |
122 | | |
123 | 3.85k | shiftlist = (uint32_t *)do_alloca(sizeof(uint32_t) * op_array->last, use_heap); |
124 | 3.85k | memset(shiftlist, 0, sizeof(uint32_t) * op_array->last); |
125 | | /* remove empty callee_info */ |
126 | 3.85k | func_info = ZEND_FUNC_INFO(op_array); |
127 | 3.85k | if (func_info) { |
128 | 3.85k | zend_call_info **call_info = &func_info->callee_info; |
129 | 7.78k | while ((*call_info)) { |
130 | 3.93k | if ((*call_info)->caller_init_opline->opcode == ZEND_NOP) { |
131 | 42 | *call_info = (*call_info)->next_callee; |
132 | 3.88k | } else { |
133 | 3.88k | call_info = &(*call_info)->next_callee; |
134 | 3.88k | } |
135 | 3.93k | } |
136 | 3.85k | } |
137 | | |
138 | 35.5k | for (b = blocks; b < blocks_end; b++) { |
139 | 31.6k | if (b->flags & (ZEND_BB_REACHABLE|ZEND_BB_UNREACHABLE_FREE)) { |
140 | 29.0k | if (b->len) { |
141 | 28.7k | uint32_t new_start, old_end; |
142 | 34.8k | while (i < b->start) { |
143 | 6.04k | shiftlist[i] = i - target; |
144 | 6.04k | i++; |
145 | 6.04k | } |
146 | | |
147 | 28.7k | if (b->flags & ZEND_BB_UNREACHABLE_FREE) { |
148 | | /* Only keep the FREE for the loop var */ |
149 | 6 | ZEND_ASSERT(op_array->opcodes[b->start].opcode == ZEND_FREE |
150 | 6 | || op_array->opcodes[b->start].opcode == ZEND_FE_FREE); |
151 | 6 | b->len = 1; |
152 | 6 | } |
153 | | |
154 | 28.7k | new_start = target; |
155 | 28.7k | old_end = b->start + b->len; |
156 | 237k | while (i < old_end) { |
157 | 208k | shiftlist[i] = i - target; |
158 | 208k | if (EXPECTED(op_array->opcodes[i].opcode != ZEND_NOP)) { |
159 | 195k | if (i != target) { |
160 | 100k | op_array->opcodes[target] = op_array->opcodes[i]; |
161 | 100k | ssa->ops[target] = ssa->ops[i]; |
162 | 100k | ssa->cfg.map[target] = b - blocks; |
163 | 100k | } |
164 | 195k | target++; |
165 | 195k | } |
166 | 208k | i++; |
167 | 208k | } |
168 | 28.7k | b->start = new_start; |
169 | 28.7k | if (target != old_end) { |
170 | 16.3k | zend_op *opline; |
171 | 16.3k | zend_op *new_opline; |
172 | | |
173 | 16.3k | b->len = target - b->start; |
174 | 16.3k | opline = op_array->opcodes + old_end - 1; |
175 | 16.3k | if (opline->opcode == ZEND_NOP) { |
176 | 83 | continue; |
177 | 83 | } |
178 | | |
179 | 16.2k | new_opline = op_array->opcodes + target - 1; |
180 | 16.2k | zend_optimizer_migrate_jump(op_array, new_opline, opline); |
181 | 16.2k | } |
182 | 28.7k | } else { |
183 | 272 | b->start = target; |
184 | 272 | } |
185 | 29.0k | } else { |
186 | 2.62k | b->start = target; |
187 | 2.62k | b->len = 0; |
188 | 2.62k | } |
189 | 31.6k | } |
190 | | |
191 | 3.85k | if (target != op_array->last) { |
192 | | /* reset rest opcodes */ |
193 | 23.2k | for (i = target; i < op_array->last; i++) { |
194 | 19.3k | MAKE_NOP(op_array->opcodes + i); |
195 | 19.3k | } |
196 | | |
197 | | /* update SSA variables */ |
198 | 205k | for (j = 0; j < ssa->vars_count; j++) { |
199 | 201k | if (ssa->vars[j].definition >= 0) { |
200 | 137k | ssa->vars[j].definition -= shiftlist[ssa->vars[j].definition]; |
201 | 137k | } |
202 | 201k | if (ssa->vars[j].use_chain >= 0) { |
203 | 142k | ssa->vars[j].use_chain -= shiftlist[ssa->vars[j].use_chain]; |
204 | 142k | } |
205 | 201k | } |
206 | 218k | for (i = 0; i < op_array->last; i++) { |
207 | 214k | if (ssa->ops[i].op1_use_chain >= 0) { |
208 | 10.0k | ssa->ops[i].op1_use_chain -= shiftlist[ssa->ops[i].op1_use_chain]; |
209 | 10.0k | } |
210 | 214k | if (ssa->ops[i].op2_use_chain >= 0) { |
211 | 13.1k | ssa->ops[i].op2_use_chain -= shiftlist[ssa->ops[i].op2_use_chain]; |
212 | 13.1k | } |
213 | 214k | if (ssa->ops[i].res_use_chain >= 0) { |
214 | 0 | ssa->ops[i].res_use_chain -= shiftlist[ssa->ops[i].res_use_chain]; |
215 | 0 | } |
216 | 214k | } |
217 | | |
218 | | /* update branch targets */ |
219 | 35.5k | for (b = blocks; b < blocks_end; b++) { |
220 | 31.6k | if ((b->flags & ZEND_BB_REACHABLE) && b->len != 0) { |
221 | 28.7k | zend_op *opline = op_array->opcodes + b->start + b->len - 1; |
222 | 28.7k | zend_optimizer_shift_jump(op_array, opline, shiftlist); |
223 | 28.7k | } |
224 | 31.6k | } |
225 | | |
226 | | /* update try/catch array */ |
227 | 3.85k | for (uint32_t j = 0; j < op_array->last_try_catch; j++) { |
228 | 0 | op_array->try_catch_array[j].try_op -= shiftlist[op_array->try_catch_array[j].try_op]; |
229 | 0 | op_array->try_catch_array[j].catch_op -= shiftlist[op_array->try_catch_array[j].catch_op]; |
230 | 0 | if (op_array->try_catch_array[j].finally_op) { |
231 | 0 | op_array->try_catch_array[j].finally_op -= shiftlist[op_array->try_catch_array[j].finally_op]; |
232 | 0 | op_array->try_catch_array[j].finally_end -= shiftlist[op_array->try_catch_array[j].finally_end]; |
233 | 0 | } |
234 | 0 | } |
235 | | |
236 | | /* update call graph */ |
237 | 3.85k | if (func_info) { |
238 | 3.85k | zend_call_info *call_info = func_info->callee_info; |
239 | 7.74k | while (call_info) { |
240 | 3.88k | call_info->caller_init_opline -= |
241 | 3.88k | shiftlist[call_info->caller_init_opline - op_array->opcodes]; |
242 | 3.88k | if (call_info->caller_call_opline) { |
243 | 3.88k | call_info->caller_call_opline -= |
244 | 3.88k | shiftlist[call_info->caller_call_opline - op_array->opcodes]; |
245 | 3.88k | } |
246 | 3.88k | call_info = call_info->next_callee; |
247 | 3.88k | } |
248 | 3.85k | } |
249 | | |
250 | 3.85k | op_array->last = target; |
251 | 3.85k | } |
252 | 3.85k | free_alloca(shiftlist, use_heap); |
253 | 3.85k | } |
254 | | |
255 | 143 | static bool safe_instanceof(const zend_class_entry *ce1, const zend_class_entry *ce2) { |
256 | 143 | if (ce1 == ce2) { |
257 | 91 | return true; |
258 | 91 | } |
259 | 52 | if (!(ce1->ce_flags & ZEND_ACC_LINKED)) { |
260 | | /* This case could be generalized, similarly to unlinked_instanceof */ |
261 | 8 | return false; |
262 | 8 | } |
263 | 44 | return instanceof_function(ce1, ce2); |
264 | 52 | } |
265 | | |
266 | | static inline bool can_elide_list_type( |
267 | | const zend_script *script, const zend_op_array *op_array, |
268 | | const zend_ssa_var_info *use_info, const zend_type type) |
269 | 179 | { |
270 | 179 | const zend_type *single_type; |
271 | | /* For intersection: result==false is failure, default is success. |
272 | | * For union: result==true is success, default is failure. */ |
273 | 179 | bool is_intersection = ZEND_TYPE_IS_INTERSECTION(type); |
274 | 358 | ZEND_TYPE_FOREACH(type, single_type) { |
275 | 358 | if (ZEND_TYPE_HAS_LIST(*single_type)) { |
276 | 16 | ZEND_ASSERT(!is_intersection); |
277 | 16 | return can_elide_list_type(script, op_array, use_info, *single_type); |
278 | 16 | } |
279 | 163 | if (ZEND_TYPE_HAS_NAME(*single_type)) { |
280 | 163 | zend_string *lcname = zend_string_tolower(ZEND_TYPE_NAME(*single_type)); |
281 | 163 | const zend_class_entry *ce = zend_optimizer_get_class_entry(script, op_array, lcname); |
282 | 163 | zend_string_release(lcname); |
283 | 163 | bool result = ce && safe_instanceof(use_info->ce, ce); |
284 | 163 | if (result == !is_intersection) { |
285 | 123 | return result; |
286 | 123 | } |
287 | 163 | } |
288 | 163 | } ZEND_TYPE_FOREACH_END(); |
289 | 40 | return is_intersection; |
290 | 179 | } |
291 | | |
292 | | static inline bool can_elide_return_type_check( |
293 | 1.31k | const zend_script *script, zend_op_array *op_array, zend_ssa *ssa, zend_ssa_op *ssa_op) { |
294 | 1.31k | zend_arg_info *arg_info = &op_array->arg_info[-1]; |
295 | 1.31k | zend_ssa_var_info *use_info = &ssa->var_info[ssa_op->op1_use]; |
296 | 1.31k | uint32_t use_type = use_info->type & (MAY_BE_ANY|MAY_BE_UNDEF); |
297 | 1.31k | if (use_type & MAY_BE_REF) { |
298 | 0 | return false; |
299 | 0 | } |
300 | | |
301 | 1.31k | if (use_type & MAY_BE_UNDEF) { |
302 | 32 | use_type &= ~MAY_BE_UNDEF; |
303 | 32 | use_type |= MAY_BE_NULL; |
304 | 32 | } |
305 | | |
306 | 1.31k | uint32_t disallowed_types = use_type & ~ZEND_TYPE_PURE_MASK(arg_info->type); |
307 | 1.31k | if (!disallowed_types) { |
308 | | /* Only contains allowed types. */ |
309 | 333 | return true; |
310 | 333 | } |
311 | | |
312 | 985 | if (disallowed_types == MAY_BE_OBJECT && use_info->ce && ZEND_TYPE_IS_COMPLEX(arg_info->type)) { |
313 | 163 | return can_elide_list_type(script, op_array, use_info, arg_info->type); |
314 | 163 | } |
315 | | |
316 | 822 | return false; |
317 | 985 | } |
318 | | |
319 | | static bool opline_supports_assign_contraction( |
320 | 2.92k | zend_op_array *op_array, zend_ssa *ssa, zend_op *opline, int src_var, uint32_t cv_var) { |
321 | 2.92k | if (opline->opcode == ZEND_NEW) { |
322 | | /* see Zend/tests/generators/aborted_yield_during_new.phpt */ |
323 | 583 | return false; |
324 | 583 | } |
325 | | |
326 | | /* Frameless calls override the return value, but the return value may overlap with the arguments. */ |
327 | 2.34k | switch (opline->opcode) { |
328 | 0 | case ZEND_FRAMELESS_ICALL_3: |
329 | 0 | if ((opline + 1)->op1_type == IS_CV && (opline + 1)->op1.var == cv_var) return false; |
330 | 0 | ZEND_FALLTHROUGH; |
331 | 0 | case ZEND_FRAMELESS_ICALL_2: |
332 | 0 | if (opline->op2_type == IS_CV && opline->op2.var == cv_var) return false; |
333 | 0 | ZEND_FALLTHROUGH; |
334 | 0 | case ZEND_FRAMELESS_ICALL_1: |
335 | 0 | if (opline->op1_type == IS_CV && opline->op1.var == cv_var) return false; |
336 | 0 | return true; |
337 | 2.34k | } |
338 | | |
339 | 2.34k | if (opline->opcode == ZEND_DO_ICALL || opline->opcode == ZEND_DO_UCALL |
340 | 2.33k | || opline->opcode == ZEND_DO_FCALL || opline->opcode == ZEND_DO_FCALL_BY_NAME) { |
341 | | /* Function calls may dtor the return value after it has already been written -- allow |
342 | | * direct assignment only for types where a double-dtor does not matter. */ |
343 | 295 | uint32_t type = ssa->var_info[src_var].type; |
344 | 295 | uint32_t simple = MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE; |
345 | 295 | return !((type & MAY_BE_ANY) & ~simple); |
346 | 295 | } |
347 | | |
348 | 2.04k | if (opline->opcode == ZEND_POST_INC || opline->opcode == ZEND_POST_DEC) { |
349 | | /* POST_INC/DEC write the result variable before performing the inc/dec. For $i = $i++ |
350 | | * eliding the temporary variable would thus yield an incorrect result. */ |
351 | 19 | return opline->op1_type != IS_CV || opline->op1.var != cv_var; |
352 | 19 | } |
353 | | |
354 | 2.02k | if (opline->opcode == ZEND_INIT_ARRAY) { |
355 | | /* INIT_ARRAY initializes the result array before reading key/value. */ |
356 | 22 | return (opline->op1_type != IS_CV || opline->op1.var != cv_var) |
357 | 18 | && (opline->op2_type != IS_CV || opline->op2.var != cv_var); |
358 | 22 | } |
359 | | |
360 | 2.00k | if (opline->opcode == ZEND_CAST |
361 | 62 | && (opline->extended_value == IS_ARRAY || opline->extended_value == IS_OBJECT)) { |
362 | | /* CAST to array/object may initialize the result to an empty array/object before |
363 | | * reading the expression. */ |
364 | 12 | return opline->op1_type != IS_CV || opline->op1.var != cv_var; |
365 | 12 | } |
366 | | |
367 | 1.99k | if ((opline->opcode == ZEND_ASSIGN_OP |
368 | 1.98k | || opline->opcode == ZEND_ASSIGN_OBJ |
369 | 1.98k | || opline->opcode == ZEND_ASSIGN_DIM |
370 | 1.97k | || opline->opcode == ZEND_ASSIGN_OBJ_OP |
371 | 1.97k | || opline->opcode == ZEND_ASSIGN_DIM_OP) |
372 | 18 | && opline->op1_type == IS_CV |
373 | 18 | && opline->op1.var == cv_var |
374 | 10 | && zend_may_throw(opline, &ssa->ops[ssa->vars[src_var].definition], op_array, ssa)) { |
375 | 6 | return false; |
376 | 6 | } |
377 | | |
378 | 1.98k | return true; |
379 | 1.99k | } |
380 | | |
381 | | static bool variable_defined_or_used_in_range(zend_ssa *ssa, int var, int start, int end) |
382 | 2.02k | { |
383 | 2.05k | while (start < end) { |
384 | 28 | const zend_ssa_op *ssa_op = &ssa->ops[start]; |
385 | 28 | if ((ssa_op->op1_def >= 0 && ssa->vars[ssa_op->op1_def].var == var) || |
386 | 28 | (ssa_op->op2_def >= 0 && ssa->vars[ssa_op->op2_def].var == var) || |
387 | 28 | (ssa_op->result_def >= 0 && ssa->vars[ssa_op->result_def].var == var) || |
388 | 28 | (ssa_op->op1_use >= 0 && ssa->vars[ssa_op->op1_use].var == var) || |
389 | 28 | (ssa_op->op2_use >= 0 && ssa->vars[ssa_op->op2_use].var == var) || |
390 | 28 | (ssa_op->result_use >= 0 && ssa->vars[ssa_op->result_use].var == var) |
391 | 28 | ) { |
392 | 0 | return true; |
393 | 0 | } |
394 | 28 | start++; |
395 | 28 | } |
396 | 2.02k | return false; |
397 | 2.02k | } |
398 | | |
399 | | static uint32_t zend_dfa_optimize_calls(zend_op_array *op_array, zend_ssa *ssa) |
400 | 37.1k | { |
401 | 37.1k | const zend_func_info *func_info = ZEND_FUNC_INFO(op_array); |
402 | 37.1k | uint32_t removed_ops = 0; |
403 | | |
404 | 37.1k | if (func_info->callee_info) { |
405 | 16.8k | const zend_call_info *call_info = func_info->callee_info; |
406 | | |
407 | 39.4k | do { |
408 | 39.4k | zend_op *op = call_info->caller_init_opline; |
409 | | |
410 | 39.4k | if ((op->opcode == ZEND_FRAMELESS_ICALL_2 |
411 | 39.4k | || (op->opcode == ZEND_FRAMELESS_ICALL_3 && (op + 1)->op1_type == IS_CONST)) |
412 | 0 | && call_info->callee_func |
413 | 0 | && zend_string_equals_literal_ci(call_info->callee_func->common.function_name, "in_array")) { |
414 | 0 | bool strict = false; |
415 | 0 | bool has_opdata = op->opcode == ZEND_FRAMELESS_ICALL_3; |
416 | 0 | ZEND_ASSERT(!call_info->is_prototype); |
417 | |
|
418 | 0 | if (has_opdata) { |
419 | 0 | if (zend_is_true(CT_CONSTANT_EX(op_array, (op + 1)->op1.constant))) { |
420 | 0 | strict = true; |
421 | 0 | } |
422 | 0 | } |
423 | |
|
424 | 0 | if (op->op2_type == IS_CONST |
425 | 0 | && Z_TYPE_P(CT_CONSTANT_EX(op_array, op->op2.constant)) == IS_ARRAY) { |
426 | 0 | bool ok = true; |
427 | |
|
428 | 0 | const HashTable *src = Z_ARRVAL_P(CT_CONSTANT_EX(op_array, op->op2.constant)); |
429 | 0 | HashTable *dst; |
430 | 0 | zval *val, tmp; |
431 | 0 | zend_ulong idx; |
432 | |
|
433 | 0 | ZVAL_TRUE(&tmp); |
434 | 0 | dst = zend_new_array(zend_hash_num_elements(src)); |
435 | 0 | if (strict) { |
436 | 0 | ZEND_HASH_FOREACH_VAL(src, val) { |
437 | 0 | if (Z_TYPE_P(val) == IS_STRING) { |
438 | 0 | zend_hash_add(dst, Z_STR_P(val), &tmp); |
439 | 0 | } else if (Z_TYPE_P(val) == IS_LONG) { |
440 | 0 | zend_hash_index_add(dst, Z_LVAL_P(val), &tmp); |
441 | 0 | } else { |
442 | 0 | zend_array_destroy(dst); |
443 | 0 | ok = false; |
444 | 0 | break; |
445 | 0 | } |
446 | 0 | } ZEND_HASH_FOREACH_END(); |
447 | 0 | } else { |
448 | 0 | ZEND_HASH_FOREACH_VAL(src, val) { |
449 | 0 | if (Z_TYPE_P(val) != IS_STRING || ZEND_HANDLE_NUMERIC(Z_STR_P(val), idx)) { |
450 | 0 | zend_array_destroy(dst); |
451 | 0 | ok = false; |
452 | 0 | break; |
453 | 0 | } |
454 | 0 | zend_hash_add(dst, Z_STR_P(val), &tmp); |
455 | 0 | } ZEND_HASH_FOREACH_END(); |
456 | 0 | } |
457 | |
|
458 | 0 | if (ok) { |
459 | 0 | ZVAL_ARR(&tmp, dst); |
460 | | |
461 | | /* Update opcode */ |
462 | 0 | op->opcode = ZEND_IN_ARRAY; |
463 | 0 | op->extended_value = strict; |
464 | 0 | op->op2.constant = zend_optimizer_add_literal(op_array, &tmp); |
465 | 0 | if (has_opdata) { |
466 | 0 | MAKE_NOP(op + 1); |
467 | 0 | removed_ops++; |
468 | 0 | } |
469 | 0 | } |
470 | 0 | } |
471 | 0 | } |
472 | 39.4k | call_info = call_info->next_callee; |
473 | 39.4k | } while (call_info); |
474 | 16.8k | } |
475 | | |
476 | 37.1k | return removed_ops; |
477 | 37.1k | } |
478 | | |
479 | | static zend_always_inline void take_successor_0(zend_ssa *ssa, uint32_t block_num, zend_basic_block *block) |
480 | 909 | { |
481 | 909 | if (block->successors_count == 2) { |
482 | 154 | if (block->successors[1] != block->successors[0]) { |
483 | 42 | zend_ssa_remove_predecessor(ssa, block_num, block->successors[1]); |
484 | 42 | } |
485 | 154 | block->successors_count = 1; |
486 | 154 | } |
487 | 909 | } |
488 | | |
489 | | static zend_always_inline void take_successor_1(zend_ssa *ssa, uint32_t block_num, zend_basic_block *block) |
490 | 391 | { |
491 | 391 | if (block->successors_count == 2) { |
492 | 250 | if (block->successors[1] != block->successors[0]) { |
493 | 232 | zend_ssa_remove_predecessor(ssa, block_num, block->successors[0]); |
494 | 232 | block->successors[0] = block->successors[1]; |
495 | 232 | } |
496 | 250 | block->successors_count = 1; |
497 | 250 | } |
498 | 391 | } |
499 | | |
500 | | static zend_always_inline void take_successor_ex(zend_ssa *ssa, uint32_t block_num, zend_basic_block *block, int target_block) |
501 | 14 | { |
502 | 38 | for (uint32_t i = 0; i < block->successors_count; i++) { |
503 | 24 | if (block->successors[i] != target_block) { |
504 | 10 | zend_ssa_remove_predecessor(ssa, block_num, block->successors[i]); |
505 | 10 | } |
506 | 24 | } |
507 | 14 | block->successors[0] = target_block; |
508 | 14 | block->successors_count = 1; |
509 | 14 | } |
510 | | |
511 | | static void compress_block(zend_op_array *op_array, zend_basic_block *block) |
512 | 135k | { |
513 | 139k | while (block->len > 0) { |
514 | 137k | zend_op *opline = &op_array->opcodes[block->start + block->len - 1]; |
515 | | |
516 | 137k | if (opline->opcode == ZEND_NOP) { |
517 | 3.11k | block->len--; |
518 | 134k | } else { |
519 | 134k | break; |
520 | 134k | } |
521 | 137k | } |
522 | 135k | } |
523 | | |
524 | 876 | static void replace_predecessor(zend_ssa *ssa, int block_id, int old_pred, int new_pred) { |
525 | 876 | zend_basic_block *block = &ssa->cfg.blocks[block_id]; |
526 | 876 | int *predecessors = &ssa->cfg.predecessors[block->predecessor_offset]; |
527 | 876 | zend_ssa_phi *phi; |
528 | | |
529 | 876 | int old_pred_idx = -1; |
530 | 876 | int new_pred_idx = -1; |
531 | 2.05k | for (uint32_t i = 0; i < block->predecessors_count; i++) { |
532 | 1.18k | if (predecessors[i] == old_pred) { |
533 | 876 | old_pred_idx = i; |
534 | 876 | } |
535 | 1.18k | if (predecessors[i] == new_pred) { |
536 | 153 | new_pred_idx = i; |
537 | 153 | } |
538 | 1.18k | } |
539 | | |
540 | 876 | ZEND_ASSERT(old_pred_idx != -1); |
541 | 876 | if (new_pred_idx == -1) { |
542 | | /* If the new predecessor doesn't exist yet, simply rewire the old one */ |
543 | 723 | predecessors[old_pred_idx] = new_pred; |
544 | 723 | } else { |
545 | | /* Otherwise, rewiring the old predecessor would make the new predecessor appear |
546 | | * twice, which violates our CFG invariants. Remove the old predecessor instead. */ |
547 | 153 | memmove( |
548 | 153 | predecessors + old_pred_idx, |
549 | 153 | predecessors + old_pred_idx + 1, |
550 | 153 | sizeof(int) * (block->predecessors_count - old_pred_idx - 1) |
551 | 153 | ); |
552 | | |
553 | | /* Also remove the corresponding phi node entries */ |
554 | 170 | for (phi = ssa->blocks[block_id].phis; phi; phi = phi->next) { |
555 | 17 | if (phi->pi >= 0) { |
556 | 4 | if (phi->pi == old_pred || phi->pi == new_pred) { |
557 | 4 | zend_ssa_rename_var_uses( |
558 | 4 | ssa, phi->ssa_var, phi->sources[0], /* update_types */ 0); |
559 | 4 | zend_ssa_remove_phi(ssa, phi); |
560 | 4 | } |
561 | 13 | } else { |
562 | 13 | memmove( |
563 | 13 | phi->sources + old_pred_idx, |
564 | 13 | phi->sources + old_pred_idx + 1, |
565 | 13 | sizeof(int) * (block->predecessors_count - old_pred_idx - 1) |
566 | 13 | ); |
567 | 13 | } |
568 | 17 | } |
569 | | |
570 | 153 | block->predecessors_count--; |
571 | 153 | } |
572 | 876 | } |
573 | | |
574 | | static void zend_ssa_replace_control_link(zend_op_array *op_array, zend_ssa *ssa, int from, int to, int new_to) |
575 | 876 | { |
576 | 876 | zend_basic_block *src = &ssa->cfg.blocks[from]; |
577 | 876 | zend_basic_block *old = &ssa->cfg.blocks[to]; |
578 | 876 | zend_basic_block *dst = &ssa->cfg.blocks[new_to]; |
579 | 876 | zend_op *opline; |
580 | | |
581 | 2.34k | for (uint32_t i = 0; i < src->successors_count; i++) { |
582 | 1.47k | if (src->successors[i] == to) { |
583 | 894 | src->successors[i] = new_to; |
584 | 894 | } |
585 | 1.47k | } |
586 | | |
587 | 876 | if (src->len > 0) { |
588 | 772 | opline = op_array->opcodes + src->start + src->len - 1; |
589 | 772 | switch (opline->opcode) { |
590 | 2 | case ZEND_JMP: |
591 | 2 | case ZEND_FAST_CALL: |
592 | 2 | ZEND_ASSERT(ZEND_OP1_JMP_ADDR(opline) == op_array->opcodes + old->start); |
593 | 2 | ZEND_SET_OP_JMP_ADDR(opline, opline->op1, op_array->opcodes + dst->start); |
594 | 2 | break; |
595 | 285 | case ZEND_JMPZ: |
596 | 391 | case ZEND_JMPNZ: |
597 | 487 | case ZEND_JMPZ_EX: |
598 | 499 | case ZEND_JMPNZ_EX: |
599 | 499 | case ZEND_FE_RESET_R: |
600 | 499 | case ZEND_FE_RESET_RW: |
601 | 541 | case ZEND_JMP_SET: |
602 | 619 | case ZEND_COALESCE: |
603 | 619 | case ZEND_ASSERT_CHECK: |
604 | 619 | case ZEND_JMP_NULL: |
605 | 619 | case ZEND_BIND_INIT_STATIC_OR_JMP: |
606 | 619 | case ZEND_JMP_FRAMELESS: |
607 | 619 | if (ZEND_OP2_JMP_ADDR(opline) == op_array->opcodes + old->start) { |
608 | 135 | ZEND_SET_OP_JMP_ADDR(opline, opline->op2, op_array->opcodes + dst->start); |
609 | 135 | } |
610 | 619 | break; |
611 | 0 | case ZEND_CATCH: |
612 | 0 | if (!(opline->extended_value & ZEND_LAST_CATCH)) { |
613 | 0 | if (ZEND_OP2_JMP_ADDR(opline) == op_array->opcodes + old->start) { |
614 | 0 | ZEND_SET_OP_JMP_ADDR(opline, opline->op2, op_array->opcodes + dst->start); |
615 | 0 | } |
616 | 0 | } |
617 | 0 | break; |
618 | 7 | case ZEND_FE_FETCH_R: |
619 | 7 | case ZEND_FE_FETCH_RW: |
620 | 7 | if (ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value) == old->start) { |
621 | 0 | opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, dst->start); |
622 | 0 | } |
623 | 7 | break; |
624 | 0 | case ZEND_SWITCH_LONG: |
625 | 0 | case ZEND_SWITCH_STRING: |
626 | 4 | case ZEND_MATCH: |
627 | 4 | { |
628 | 4 | HashTable *jumptable = Z_ARRVAL(ZEND_OP2_LITERAL(opline)); |
629 | 4 | zval *zv; |
630 | 20 | ZEND_HASH_FOREACH_VAL(jumptable, zv) { |
631 | 20 | if (ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, Z_LVAL_P(zv)) == old->start) { |
632 | 0 | Z_LVAL_P(zv) = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, dst->start); |
633 | 0 | } |
634 | 20 | } ZEND_HASH_FOREACH_END(); |
635 | 4 | if (ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value) == old->start) { |
636 | 4 | opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, dst->start); |
637 | 4 | } |
638 | 4 | break; |
639 | 0 | } |
640 | 772 | } |
641 | 772 | } |
642 | | |
643 | 876 | replace_predecessor(ssa, new_to, to, from); |
644 | 876 | } |
645 | | |
646 | | static void zend_ssa_unlink_block(zend_op_array *op_array, zend_ssa *ssa, zend_basic_block *block, uint32_t block_num) |
647 | 1.21k | { |
648 | 1.21k | if (block->predecessors_count == 1 && ssa->blocks[block_num].phis == NULL) { |
649 | 876 | int *predecessors; |
650 | 876 | zend_basic_block *fe_fetch_block = NULL; |
651 | | |
652 | 876 | ZEND_ASSERT(block->successors_count == 1); |
653 | 876 | predecessors = &ssa->cfg.predecessors[block->predecessor_offset]; |
654 | 876 | if (block->predecessors_count == 1 && (block->flags & ZEND_BB_FOLLOW)) { |
655 | 793 | zend_basic_block *pred_block = &ssa->cfg.blocks[predecessors[0]]; |
656 | | |
657 | 793 | if (pred_block->len > 0 && (pred_block->flags & ZEND_BB_REACHABLE)) { |
658 | 691 | if ((op_array->opcodes[pred_block->start + pred_block->len - 1].opcode == ZEND_FE_FETCH_R |
659 | 684 | || op_array->opcodes[pred_block->start + pred_block->len - 1].opcode == ZEND_FE_FETCH_RW) |
660 | 7 | && op_array->opcodes[pred_block->start + pred_block->len - 1].op2_type == IS_CV) { |
661 | 7 | fe_fetch_block = pred_block; |
662 | 7 | } |
663 | 691 | } |
664 | 793 | } |
665 | 1.75k | for (uint32_t i = 0; i < block->predecessors_count; i++) { |
666 | 876 | zend_ssa_replace_control_link(op_array, ssa, predecessors[i], block_num, block->successors[0]); |
667 | 876 | } |
668 | 876 | zend_ssa_remove_block(op_array, ssa, block_num); |
669 | 876 | if (fe_fetch_block && fe_fetch_block->successors[0] == fe_fetch_block->successors[1]) { |
670 | | /* The body of "foreach" loop was removed */ |
671 | 5 | int ssa_var = ssa->ops[fe_fetch_block->start + fe_fetch_block->len - 1].op2_def; |
672 | 5 | if (ssa_var >= 0) { |
673 | 5 | zend_ssa_remove_uses_of_var(ssa, ssa_var); |
674 | 5 | } |
675 | 5 | } |
676 | 876 | } |
677 | 1.21k | } |
678 | | |
679 | | static int zend_dfa_optimize_jmps(zend_op_array *op_array, zend_ssa *ssa) |
680 | 74.3k | { |
681 | 74.3k | int removed_ops = 0; |
682 | 74.3k | uint32_t block_num = 0; |
683 | | |
684 | 213k | for (block_num = 1; block_num < ssa->cfg.blocks_count; block_num++) { |
685 | 138k | zend_basic_block *block = &ssa->cfg.blocks[block_num]; |
686 | | |
687 | 138k | if (!(block->flags & ZEND_BB_REACHABLE)) { |
688 | 4.11k | continue; |
689 | 4.11k | } |
690 | 134k | compress_block(op_array, block); |
691 | 134k | if (block->len == 0) { |
692 | 527 | zend_ssa_unlink_block(op_array, ssa, block, block_num); |
693 | 527 | } |
694 | 134k | } |
695 | | |
696 | 74.3k | block_num = 0; |
697 | 74.3k | while (block_num < ssa->cfg.blocks_count |
698 | 74.3k | && !(ssa->cfg.blocks[block_num].flags & ZEND_BB_REACHABLE)) { |
699 | 0 | block_num++; |
700 | 0 | } |
701 | 283k | while (block_num < ssa->cfg.blocks_count) { |
702 | 209k | uint32_t next_block_num = block_num + 1; |
703 | 209k | zend_basic_block *block = &ssa->cfg.blocks[block_num]; |
704 | 209k | uint32_t op_num; |
705 | 209k | zend_op *opline; |
706 | 209k | zend_ssa_op *ssa_op; |
707 | 209k | bool can_follow = true; |
708 | | |
709 | 218k | while (next_block_num < ssa->cfg.blocks_count |
710 | 144k | && !(ssa->cfg.blocks[next_block_num].flags & ZEND_BB_REACHABLE)) { |
711 | 9.42k | if (ssa->cfg.blocks[next_block_num].flags & ZEND_BB_UNREACHABLE_FREE) { |
712 | 28 | can_follow = false; |
713 | 28 | } |
714 | 9.42k | next_block_num++; |
715 | 9.42k | } |
716 | | |
717 | 209k | if (block->len) { |
718 | 208k | op_num = block->start + block->len - 1; |
719 | 208k | opline = op_array->opcodes + op_num; |
720 | 208k | ssa_op = ssa->ops + op_num; |
721 | | |
722 | 208k | switch (opline->opcode) { |
723 | 13.7k | case ZEND_JMP: |
724 | 14.5k | optimize_jmp: |
725 | 14.5k | if (block->successors[0] == next_block_num && can_follow) { |
726 | 830 | MAKE_NOP(opline); |
727 | 830 | removed_ops++; |
728 | 830 | goto optimize_nop; |
729 | 830 | } |
730 | 13.7k | break; |
731 | 13.7k | case ZEND_JMPZ: |
732 | 7.95k | optimize_jmpz: |
733 | 7.95k | if (opline->op1_type == IS_CONST) { |
734 | 303 | if (zend_is_true(CT_CONSTANT_EX(op_array, opline->op1.constant))) { |
735 | 137 | MAKE_NOP(opline); |
736 | 137 | removed_ops++; |
737 | 137 | take_successor_1(ssa, block_num, block); |
738 | 137 | goto optimize_nop; |
739 | 166 | } else { |
740 | 166 | opline->opcode = ZEND_JMP; |
741 | 166 | COPY_NODE(opline->op1, opline->op2); |
742 | 166 | take_successor_0(ssa, block_num, block); |
743 | 166 | goto optimize_jmp; |
744 | 166 | } |
745 | 7.65k | } else { |
746 | 7.65k | if (block->successors[0] == next_block_num && can_follow) { |
747 | 68 | take_successor_0(ssa, block_num, block); |
748 | 68 | if (opline->op1_type == IS_CV && (OP1_INFO() & MAY_BE_UNDEF)) { |
749 | 7 | opline->opcode = ZEND_CHECK_VAR; |
750 | 7 | opline->op2.num = 0; |
751 | 61 | } else if (opline->op1_type == IS_CV || !(OP1_INFO() & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) { |
752 | 39 | zend_ssa_remove_instr(ssa, opline, ssa_op); |
753 | 39 | removed_ops++; |
754 | 39 | goto optimize_nop; |
755 | 39 | } else { |
756 | 22 | opline->opcode = ZEND_FREE; |
757 | 22 | opline->op2.num = 0; |
758 | 22 | } |
759 | 68 | } |
760 | 7.65k | } |
761 | 7.61k | break; |
762 | 8.04k | case ZEND_JMPNZ: |
763 | 8.76k | optimize_jmpnz: |
764 | 8.76k | if (opline->op1_type == IS_CONST) { |
765 | 335 | if (zend_is_true(CT_CONSTANT_EX(op_array, opline->op1.constant))) { |
766 | 215 | opline->opcode = ZEND_JMP; |
767 | 215 | COPY_NODE(opline->op1, opline->op2); |
768 | 215 | take_successor_0(ssa, block_num, block); |
769 | 215 | goto optimize_jmp; |
770 | 215 | } else { |
771 | 120 | MAKE_NOP(opline); |
772 | 120 | removed_ops++; |
773 | 120 | take_successor_1(ssa, block_num, block); |
774 | 120 | goto optimize_nop; |
775 | 120 | } |
776 | 8.43k | } else if (block->successors_count == 2) { |
777 | 8.43k | if (block->successors[0] == next_block_num && can_follow) { |
778 | 44 | take_successor_0(ssa, block_num, block); |
779 | 44 | if (opline->op1_type == IS_CV && (OP1_INFO() & MAY_BE_UNDEF)) { |
780 | 0 | opline->opcode = ZEND_CHECK_VAR; |
781 | 0 | opline->op2.num = 0; |
782 | 44 | } else if (opline->op1_type == IS_CV || !(OP1_INFO() & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) { |
783 | 40 | zend_ssa_remove_instr(ssa, opline, ssa_op); |
784 | 40 | removed_ops++; |
785 | 40 | goto optimize_nop; |
786 | 40 | } else { |
787 | 4 | opline->opcode = ZEND_FREE; |
788 | 4 | opline->op2.num = 0; |
789 | 4 | } |
790 | 44 | } |
791 | 8.43k | } |
792 | 8.39k | break; |
793 | 8.39k | case ZEND_JMPZ_EX: |
794 | 704 | if (ssa->vars[ssa_op->result_def].use_chain < 0 |
795 | 702 | && ssa->vars[ssa_op->result_def].phi_use_chain == NULL) { |
796 | 292 | opline->opcode = ZEND_JMPZ; |
797 | 292 | opline->result_type = IS_UNUSED; |
798 | 292 | zend_ssa_remove_result_def(ssa, ssa_op); |
799 | 292 | goto optimize_jmpz; |
800 | 412 | } else if (opline->op1_type == IS_CONST) { |
801 | 62 | if (zend_is_true(CT_CONSTANT_EX(op_array, opline->op1.constant))) { |
802 | 54 | opline->opcode = ZEND_BOOL; |
803 | 54 | take_successor_1(ssa, block_num, block); |
804 | 54 | } |
805 | 62 | } |
806 | 412 | break; |
807 | 516 | case ZEND_JMPNZ_EX: |
808 | 516 | if (ssa->vars[ssa_op->result_def].use_chain < 0 |
809 | 514 | && ssa->vars[ssa_op->result_def].phi_use_chain == NULL) { |
810 | 72 | opline->opcode = ZEND_JMPNZ; |
811 | 72 | opline->result_type = IS_UNUSED; |
812 | 72 | zend_ssa_remove_result_def(ssa, ssa_op); |
813 | 72 | goto optimize_jmpnz; |
814 | 444 | } else if (opline->op1_type == IS_CONST) { |
815 | 62 | if (!zend_is_true(CT_CONSTANT_EX(op_array, opline->op1.constant))) { |
816 | 0 | opline->opcode = ZEND_BOOL; |
817 | 0 | take_successor_1(ssa, block_num, block); |
818 | 0 | } |
819 | 62 | } |
820 | 444 | break; |
821 | 1.43k | case ZEND_JMP_SET: |
822 | 1.43k | if (ssa->vars[ssa_op->result_def].use_chain < 0 |
823 | 1.43k | && ssa->vars[ssa_op->result_def].phi_use_chain == NULL) { |
824 | 645 | opline->opcode = ZEND_JMPNZ; |
825 | 645 | opline->result_type = IS_UNUSED; |
826 | 645 | zend_ssa_remove_result_def(ssa, ssa_op); |
827 | 645 | goto optimize_jmpnz; |
828 | 793 | } else if (opline->op1_type == IS_CONST) { |
829 | 0 | if (!zend_is_true(CT_CONSTANT_EX(op_array, opline->op1.constant))) { |
830 | 0 | MAKE_NOP(opline); |
831 | 0 | removed_ops++; |
832 | 0 | take_successor_1(ssa, block_num, block); |
833 | 0 | zend_ssa_remove_result_def(ssa, ssa_op); |
834 | 0 | goto optimize_nop; |
835 | 0 | } |
836 | 0 | } |
837 | 793 | break; |
838 | 2.29k | case ZEND_COALESCE: |
839 | 2.29k | { |
840 | 2.29k | zend_ssa_var *var = &ssa->vars[ssa_op->result_def]; |
841 | 2.29k | if (opline->op1_type == IS_CONST |
842 | 466 | && var->use_chain < 0 && var->phi_use_chain == NULL) { |
843 | 452 | if (Z_TYPE_P(CT_CONSTANT_EX(op_array, opline->op1.constant)) == IS_NULL) { |
844 | 48 | zend_ssa_remove_result_def(ssa, ssa_op); |
845 | 48 | MAKE_NOP(opline); |
846 | 48 | removed_ops++; |
847 | 48 | take_successor_1(ssa, block_num, block); |
848 | 48 | goto optimize_nop; |
849 | 404 | } else { |
850 | 404 | opline->opcode = ZEND_JMP; |
851 | 404 | opline->result_type = IS_UNUSED; |
852 | 404 | zend_ssa_remove_result_def(ssa, ssa_op); |
853 | 404 | COPY_NODE(opline->op1, opline->op2); |
854 | 404 | take_successor_0(ssa, block_num, block); |
855 | 404 | goto optimize_jmp; |
856 | 404 | } |
857 | 452 | } |
858 | 1.84k | break; |
859 | 2.29k | } |
860 | 37.0k | case ZEND_JMP_NULL: |
861 | 37.0k | { |
862 | 37.0k | zend_ssa_var *var = &ssa->vars[ssa_op->result_def]; |
863 | 37.0k | if (opline->op1_type == IS_CONST |
864 | 76 | && var->use_chain < 0 && var->phi_use_chain == NULL) { |
865 | 44 | if (Z_TYPE_P(CT_CONSTANT_EX(op_array, opline->op1.constant)) == IS_NULL) { |
866 | 12 | opline->opcode = ZEND_JMP; |
867 | 12 | opline->result_type = IS_UNUSED; |
868 | 12 | zend_ssa_remove_result_def(ssa, ssa_op); |
869 | 12 | COPY_NODE(opline->op1, opline->op2); |
870 | 12 | take_successor_0(ssa, block_num, block); |
871 | 12 | goto optimize_jmp; |
872 | 32 | } else { |
873 | 32 | zend_ssa_remove_result_def(ssa, ssa_op); |
874 | 32 | MAKE_NOP(opline); |
875 | 32 | removed_ops++; |
876 | 32 | take_successor_1(ssa, block_num, block); |
877 | 32 | goto optimize_nop; |
878 | 32 | } |
879 | 44 | } |
880 | 37.0k | break; |
881 | 37.0k | } |
882 | 37.0k | case ZEND_SWITCH_LONG: |
883 | 60 | case ZEND_SWITCH_STRING: |
884 | 234 | case ZEND_MATCH: |
885 | 234 | if (opline->op1_type == IS_CONST) { |
886 | 14 | zval *zv = CT_CONSTANT_EX(op_array, opline->op1.constant); |
887 | 14 | uint8_t type = Z_TYPE_P(zv); |
888 | 14 | bool correct_type = |
889 | 14 | (opline->opcode == ZEND_SWITCH_LONG && type == IS_LONG) |
890 | 14 | || (opline->opcode == ZEND_SWITCH_STRING && type == IS_STRING) |
891 | 14 | || (opline->opcode == ZEND_MATCH && (type == IS_LONG || type == IS_STRING)); |
892 | | |
893 | | /* Switch statements have a fallback chain for loose comparison. In those |
894 | | * cases the SWITCH_* instruction is a NOP. Match does strict comparison and |
895 | | * thus jumps to the default branch on mismatched types, so we need to |
896 | | * convert MATCH to a jmp. */ |
897 | 14 | if (!correct_type && opline->opcode != ZEND_MATCH) { |
898 | 4 | removed_ops++; |
899 | 4 | MAKE_NOP(opline); |
900 | 4 | opline->extended_value = 0; |
901 | 4 | take_successor_ex(ssa, block_num, block, block->successors[block->successors_count - 1]); |
902 | 4 | goto optimize_nop; |
903 | 4 | } |
904 | | |
905 | 10 | uint32_t target; |
906 | 10 | if (correct_type) { |
907 | 6 | HashTable *jmptable = Z_ARRVAL_P(CT_CONSTANT_EX(op_array, opline->op2.constant)); |
908 | 6 | zval *jmp_zv = type == IS_LONG |
909 | 6 | ? zend_hash_index_find(jmptable, Z_LVAL_P(zv)) |
910 | 6 | : zend_hash_find(jmptable, Z_STR_P(zv)); |
911 | | |
912 | 6 | if (jmp_zv) { |
913 | 2 | target = ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, Z_LVAL_P(jmp_zv)); |
914 | 4 | } else { |
915 | 4 | target = ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value); |
916 | 4 | } |
917 | 6 | } else { |
918 | 4 | ZEND_ASSERT(opline->opcode == ZEND_MATCH); |
919 | 4 | target = ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value); |
920 | 4 | } |
921 | 10 | opline->opcode = ZEND_JMP; |
922 | 10 | opline->extended_value = 0; |
923 | 10 | SET_UNUSED(opline->op1); |
924 | 10 | ZEND_SET_OP_JMP_ADDR(opline, opline->op1, op_array->opcodes + target); |
925 | 10 | SET_UNUSED(opline->op2); |
926 | 10 | take_successor_ex(ssa, block_num, block, ssa->cfg.map[target]); |
927 | 10 | goto optimize_jmp; |
928 | 10 | } |
929 | 220 | break; |
930 | 220 | case ZEND_NOP: |
931 | 1.30k | optimize_nop: |
932 | 1.30k | compress_block(op_array, block); |
933 | 1.30k | if (block->len == 0) { |
934 | 739 | if (block_num > 0) { |
935 | 689 | zend_ssa_unlink_block(op_array, ssa, block, block_num); |
936 | | /* backtrack to previous basic block */ |
937 | 689 | int backtracking_block_num = block_num; |
938 | 3.84k | do { |
939 | 3.84k | backtracking_block_num--; |
940 | 3.84k | } while (backtracking_block_num >= 0 |
941 | 3.84k | && !(ssa->cfg.blocks[backtracking_block_num].flags & ZEND_BB_REACHABLE)); |
942 | 689 | if (backtracking_block_num >= 0) { |
943 | 689 | block_num = backtracking_block_num; |
944 | 689 | continue; |
945 | 689 | } |
946 | 689 | } |
947 | 739 | } |
948 | 615 | break; |
949 | 137k | default: |
950 | 137k | break; |
951 | 208k | } |
952 | 208k | } |
953 | | |
954 | 208k | block_num = next_block_num; |
955 | 208k | } |
956 | | |
957 | 74.3k | return removed_ops; |
958 | 74.3k | } |
959 | | |
960 | | static bool zend_dfa_try_to_replace_result(zend_op_array *op_array, zend_ssa *ssa, int def, int cv_var) |
961 | 6.36k | { |
962 | 6.36k | int result_var = ssa->ops[def].result_def; |
963 | 6.36k | uint32_t cv = EX_NUM_TO_VAR(ssa->vars[cv_var].var); |
964 | | |
965 | 6.36k | if (result_var >= 0 |
966 | 6.36k | && !(ssa->var_info[cv_var].type & MAY_BE_REF) |
967 | 4.09k | && ssa->vars[cv_var].alias == NO_ALIAS |
968 | 4.09k | && ssa->vars[result_var].phi_use_chain == NULL |
969 | 4.09k | && ssa->vars[result_var].sym_use_chain == NULL) { |
970 | 4.09k | int use = ssa->vars[result_var].use_chain; |
971 | | |
972 | 4.09k | if (use >= 0 |
973 | 3.79k | && zend_ssa_next_use(ssa->ops, result_var, use) < 0 |
974 | 3.79k | && op_array->opcodes[use].opcode != ZEND_FREE |
975 | 3.79k | && op_array->opcodes[use].opcode != ZEND_SEND_VAL |
976 | 3.78k | && op_array->opcodes[use].opcode != ZEND_SEND_VAL_EX |
977 | 3.77k | && op_array->opcodes[use].opcode != ZEND_VERIFY_RETURN_TYPE |
978 | 3.77k | && op_array->opcodes[use].opcode != ZEND_YIELD) { |
979 | 3.76k | if (use > def) { |
980 | 3.76k | int i = use; |
981 | 3.76k | const zend_op *opline = &op_array->opcodes[use]; |
982 | | |
983 | 7.39k | while (i > def) { |
984 | 4.54k | if ((opline->op1_type == IS_CV && opline->op1.var == cv) |
985 | 3.70k | || (opline->op2_type == IS_CV && opline->op2.var == cv) |
986 | 3.63k | || (opline->result_type == IS_CV && opline->result.var == cv)) { |
987 | 918 | return false; |
988 | 918 | } |
989 | 3.63k | opline--; |
990 | 3.63k | i--; |
991 | 3.63k | } |
992 | | |
993 | | /* Update opcodes and reconstruct SSA */ |
994 | 2.84k | ssa->vars[result_var].definition = -1; |
995 | 2.84k | ssa->vars[result_var].use_chain = -1; |
996 | 2.84k | ssa->ops[def].result_def = -1; |
997 | | |
998 | 2.84k | op_array->opcodes[def].result_type = IS_UNUSED; |
999 | 2.84k | op_array->opcodes[def].result.var = 0; |
1000 | | |
1001 | 2.84k | if (ssa->ops[use].op1_use == result_var) { |
1002 | 911 | ssa->ops[use].op1_use = cv_var; |
1003 | 911 | ssa->ops[use].op1_use_chain = ssa->vars[cv_var].use_chain; |
1004 | 911 | ssa->vars[cv_var].use_chain = use; |
1005 | | |
1006 | 911 | op_array->opcodes[use].op1_type = IS_CV; |
1007 | 911 | op_array->opcodes[use].op1.var = cv; |
1008 | 1.93k | } else if (ssa->ops[use].op2_use == result_var) { |
1009 | 1.93k | ssa->ops[use].op2_use = cv_var; |
1010 | 1.93k | ssa->ops[use].op2_use_chain = ssa->vars[cv_var].use_chain; |
1011 | 1.93k | ssa->vars[cv_var].use_chain = use; |
1012 | | |
1013 | 1.93k | op_array->opcodes[use].op2_type = IS_CV; |
1014 | 1.93k | op_array->opcodes[use].op2.var = cv; |
1015 | 1.93k | } else if (ssa->ops[use].result_use == result_var) { |
1016 | 0 | ssa->ops[use].result_use = cv_var; |
1017 | 0 | ssa->ops[use].res_use_chain = ssa->vars[cv_var].use_chain; |
1018 | 0 | ssa->vars[cv_var].use_chain = use; |
1019 | |
|
1020 | 0 | op_array->opcodes[use].result_type = IS_CV; |
1021 | 0 | op_array->opcodes[use].result.var = cv; |
1022 | 0 | } |
1023 | | |
1024 | 2.84k | return true; |
1025 | 3.76k | } |
1026 | 3.76k | } |
1027 | 4.09k | } |
1028 | | |
1029 | 2.60k | return false; |
1030 | 6.36k | } |
1031 | | |
1032 | | void zend_dfa_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx, zend_ssa *ssa, zend_call_info **call_map) |
1033 | 37.1k | { |
1034 | 37.1k | if (ctx->debug_level & ZEND_DUMP_BEFORE_DFA_PASS) { |
1035 | 0 | zend_dump_op_array(op_array, ZEND_DUMP_SSA, "before dfa pass", ssa); |
1036 | 0 | } |
1037 | | |
1038 | 37.1k | if (ssa->var_info) { |
1039 | 37.1k | int op_1; |
1040 | 37.1k | int v; |
1041 | 37.1k | int remove_nops = 0; |
1042 | 37.1k | zend_op *opline; |
1043 | 37.1k | zend_ssa_op *ssa_op; |
1044 | 37.1k | zval tmp; |
1045 | | |
1046 | 37.1k | #if ZEND_DEBUG_DFA |
1047 | 37.1k | ssa_verify_integrity(op_array, ssa, "before dfa"); |
1048 | 37.1k | #endif |
1049 | | |
1050 | 37.1k | if (ZEND_OPTIMIZER_PASS_8 & ctx->optimization_level) { |
1051 | 37.1k | if (sccp_optimize_op_array(ctx, op_array, ssa, call_map)) { |
1052 | 824 | remove_nops = 1; |
1053 | 824 | } |
1054 | | |
1055 | 37.1k | if (zend_dfa_optimize_jmps(op_array, ssa)) { |
1056 | 306 | remove_nops = 1; |
1057 | 306 | } |
1058 | | |
1059 | 37.1k | #if ZEND_DEBUG_DFA |
1060 | 37.1k | ssa_verify_integrity(op_array, ssa, "after sccp"); |
1061 | 37.1k | #endif |
1062 | 37.1k | if (ZEND_FUNC_INFO(op_array)) { |
1063 | 37.1k | if (zend_dfa_optimize_calls(op_array, ssa)) { |
1064 | 0 | remove_nops = 1; |
1065 | 0 | } |
1066 | 37.1k | } |
1067 | 37.1k | if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_8) { |
1068 | 0 | zend_dump_op_array(op_array, ZEND_DUMP_SSA, "after sccp pass", ssa); |
1069 | 0 | } |
1070 | 37.1k | #if ZEND_DEBUG_DFA |
1071 | 37.1k | ssa_verify_integrity(op_array, ssa, "after calls"); |
1072 | 37.1k | #endif |
1073 | 37.1k | } |
1074 | | |
1075 | 37.1k | if (ZEND_OPTIMIZER_PASS_14 & ctx->optimization_level) { |
1076 | 37.1k | if (dce_optimize_op_array(op_array, ctx, ssa, 0)) { |
1077 | 2.76k | remove_nops = 1; |
1078 | 2.76k | } |
1079 | 37.1k | if (zend_dfa_optimize_jmps(op_array, ssa)) { |
1080 | 119 | remove_nops = 1; |
1081 | 119 | } |
1082 | 37.1k | if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_14) { |
1083 | 0 | zend_dump_op_array(op_array, ZEND_DUMP_SSA, "after dce pass", ssa); |
1084 | 0 | } |
1085 | 37.1k | #if ZEND_DEBUG_DFA |
1086 | 37.1k | ssa_verify_integrity(op_array, ssa, "after dce"); |
1087 | 37.1k | #endif |
1088 | 37.1k | } |
1089 | | |
1090 | 556k | for (v = op_array->last_var; v < ssa->vars_count; v++) { |
1091 | | |
1092 | 519k | op_1 = ssa->vars[v].definition; |
1093 | | |
1094 | 519k | if (op_1 < 0) { |
1095 | 96.4k | continue; |
1096 | 96.4k | } |
1097 | | |
1098 | 422k | opline = op_array->opcodes + op_1; |
1099 | 422k | ssa_op = &ssa->ops[op_1]; |
1100 | | |
1101 | | /* Convert LONG constants to DOUBLE */ |
1102 | 422k | if (ssa->var_info[v].use_as_double) { |
1103 | 0 | if (opline->opcode == ZEND_ASSIGN |
1104 | 0 | && opline->op2_type == IS_CONST |
1105 | 0 | && ssa->ops[op_1].op1_def == v |
1106 | 0 | && !RETURN_VALUE_USED(opline) |
1107 | 0 | ) { |
1108 | | |
1109 | | // op_1: ASSIGN ? -> #v [use_as_double], long(?) => ASSIGN ? -> #v, double(?) |
1110 | |
|
1111 | 0 | zval *zv = CT_CONSTANT_EX(op_array, opline->op2.constant); |
1112 | 0 | ZEND_ASSERT(Z_TYPE_INFO_P(zv) == IS_LONG); |
1113 | 0 | ZVAL_DOUBLE(&tmp, zval_get_double(zv)); |
1114 | 0 | opline->op2.constant = zend_optimizer_add_literal(op_array, &tmp); |
1115 | |
|
1116 | 0 | } else if (opline->opcode == ZEND_QM_ASSIGN |
1117 | 0 | && opline->op1_type == IS_CONST |
1118 | 0 | ) { |
1119 | | |
1120 | | // op_1: QM_ASSIGN #v [use_as_double], long(?) => QM_ASSIGN #v, double(?) |
1121 | |
|
1122 | 0 | zval *zv = CT_CONSTANT_EX(op_array, opline->op1.constant); |
1123 | 0 | ZEND_ASSERT(Z_TYPE_INFO_P(zv) == IS_LONG); |
1124 | 0 | ZVAL_DOUBLE(&tmp, zval_get_double(zv)); |
1125 | 0 | opline->op1.constant = zend_optimizer_add_literal(op_array, &tmp); |
1126 | 0 | } |
1127 | |
|
1128 | 422k | } else { |
1129 | 422k | if (opline->opcode == ZEND_ADD |
1130 | 418k | || opline->opcode == ZEND_SUB |
1131 | 415k | || opline->opcode == ZEND_MUL |
1132 | 412k | || opline->opcode == ZEND_IS_EQUAL |
1133 | 411k | || opline->opcode == ZEND_IS_NOT_EQUAL |
1134 | 409k | || opline->opcode == ZEND_IS_SMALLER |
1135 | 406k | || opline->opcode == ZEND_IS_SMALLER_OR_EQUAL |
1136 | 422k | ) { |
1137 | | |
1138 | 17.7k | if (opline->op1_type == IS_CONST && opline->op2_type != IS_CONST) { |
1139 | 2.28k | zval *zv = CT_CONSTANT_EX(op_array, opline->op1.constant); |
1140 | | |
1141 | 2.28k | if ((OP2_INFO() & MAY_BE_ANY) == MAY_BE_DOUBLE |
1142 | 6 | && Z_TYPE_INFO_P(zv) == IS_LONG) { |
1143 | | |
1144 | | // op_1: #v.? = ADD long(?), #?.? [double] => #v.? = ADD double(?), #?.? [double] |
1145 | | |
1146 | 2 | ZVAL_DOUBLE(&tmp, zval_get_double(zv)); |
1147 | 2 | opline->op1.constant = zend_optimizer_add_literal(op_array, &tmp); |
1148 | 2 | zv = CT_CONSTANT_EX(op_array, opline->op1.constant); |
1149 | 2 | } |
1150 | 2.28k | if (opline->opcode == ZEND_ADD) { |
1151 | 561 | zv = CT_CONSTANT_EX(op_array, opline->op1.constant); |
1152 | | |
1153 | 561 | if (((OP2_INFO() & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG |
1154 | 34 | && Z_TYPE_INFO_P(zv) == IS_LONG |
1155 | 18 | && Z_LVAL_P(zv) == 0) |
1156 | 561 | || ((OP2_INFO() & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_DOUBLE |
1157 | 4 | && Z_TYPE_INFO_P(zv) == IS_DOUBLE |
1158 | 0 | && Z_DVAL_P(zv) == 0.0)) { |
1159 | | |
1160 | | // op_1: #v.? = ADD 0, #?.? [double,long] => #v.? = QM_ASSIGN #?.? |
1161 | |
|
1162 | 0 | opline->opcode = ZEND_QM_ASSIGN; |
1163 | 0 | opline->op1_type = opline->op2_type; |
1164 | 0 | opline->op1.var = opline->op2.var; |
1165 | 0 | opline->op2_type = IS_UNUSED; |
1166 | 0 | opline->op2.num = 0; |
1167 | 0 | ssa->ops[op_1].op1_use = ssa->ops[op_1].op2_use; |
1168 | 0 | ssa->ops[op_1].op1_use_chain = ssa->ops[op_1].op2_use_chain; |
1169 | 0 | ssa->ops[op_1].op2_use = -1; |
1170 | 0 | ssa->ops[op_1].op2_use_chain = -1; |
1171 | 0 | } |
1172 | 1.72k | } else if (opline->opcode == ZEND_MUL |
1173 | 16 | && (OP2_INFO() & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) == 0) { |
1174 | 0 | zv = CT_CONSTANT_EX(op_array, opline->op1.constant); |
1175 | |
|
1176 | 0 | if ((Z_TYPE_INFO_P(zv) == IS_LONG |
1177 | 0 | && Z_LVAL_P(zv) == 2) |
1178 | 0 | || (Z_TYPE_INFO_P(zv) == IS_DOUBLE |
1179 | 0 | && Z_DVAL_P(zv) == 2.0 |
1180 | 0 | && !(OP2_INFO() & MAY_BE_LONG))) { |
1181 | | |
1182 | | // op_1: #v.? = MUL 2, #x.? [double,long] => #v.? = ADD #x.?, #x.? |
1183 | |
|
1184 | 0 | opline->opcode = ZEND_ADD; |
1185 | 0 | opline->op1_type = opline->op2_type; |
1186 | 0 | opline->op1.var = opline->op2.var; |
1187 | 0 | ssa->ops[op_1].op1_use = ssa->ops[op_1].op2_use; |
1188 | 0 | ssa->ops[op_1].op1_use_chain = ssa->ops[op_1].op2_use_chain; |
1189 | 0 | } |
1190 | 0 | } |
1191 | 15.4k | } else if (opline->op1_type != IS_CONST && opline->op2_type == IS_CONST) { |
1192 | 6.46k | zval *zv = CT_CONSTANT_EX(op_array, opline->op2.constant); |
1193 | | |
1194 | 6.46k | if ((OP1_INFO() & MAY_BE_ANY) == MAY_BE_DOUBLE |
1195 | 111 | && Z_TYPE_INFO_P(CT_CONSTANT_EX(op_array, opline->op2.constant)) == IS_LONG) { |
1196 | | |
1197 | | // op_1: #v.? = ADD #?.? [double], long(?) => #v.? = ADD #?.? [double], double(?) |
1198 | | |
1199 | 59 | ZVAL_DOUBLE(&tmp, zval_get_double(zv)); |
1200 | 59 | opline->op2.constant = zend_optimizer_add_literal(op_array, &tmp); |
1201 | 59 | zv = CT_CONSTANT_EX(op_array, opline->op2.constant); |
1202 | 59 | } |
1203 | 6.46k | if (opline->opcode == ZEND_ADD || opline->opcode == ZEND_SUB) { |
1204 | 1.50k | if (((OP1_INFO() & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG |
1205 | 378 | && Z_TYPE_INFO_P(zv) == IS_LONG |
1206 | 359 | && Z_LVAL_P(zv) == 0) |
1207 | 1.49k | || ((OP1_INFO() & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_DOUBLE |
1208 | 27 | && Z_TYPE_INFO_P(zv) == IS_DOUBLE |
1209 | 19 | && Z_DVAL_P(zv) == 0.0)) { |
1210 | | |
1211 | | // op_1: #v.? = ADD #?.? [double,long], 0 => #v.? = QM_ASSIGN #?.? |
1212 | | |
1213 | 12 | opline->opcode = ZEND_QM_ASSIGN; |
1214 | 12 | opline->op2_type = IS_UNUSED; |
1215 | 12 | opline->op2.num = 0; |
1216 | 12 | } |
1217 | 4.95k | } else if (opline->opcode == ZEND_MUL |
1218 | 1.36k | && (OP1_INFO() & ((MAY_BE_ANY|MAY_BE_UNDEF)-(MAY_BE_LONG|MAY_BE_DOUBLE))) == 0) { |
1219 | 332 | zv = CT_CONSTANT_EX(op_array, opline->op2.constant); |
1220 | | |
1221 | 332 | if ((Z_TYPE_INFO_P(zv) == IS_LONG |
1222 | 200 | && Z_LVAL_P(zv) == 2) |
1223 | 312 | || (Z_TYPE_INFO_P(zv) == IS_DOUBLE |
1224 | 36 | && Z_DVAL_P(zv) == 2.0 |
1225 | 22 | && !(OP1_INFO() & MAY_BE_LONG))) { |
1226 | | |
1227 | | // op_1: #v.? = MUL #x.? [double,long], 2 => #v.? = ADD #x.?, #x.? |
1228 | | |
1229 | 22 | opline->opcode = ZEND_ADD; |
1230 | 22 | opline->op2_type = opline->op1_type; |
1231 | 22 | opline->op2.var = opline->op1.var; |
1232 | 22 | ssa->ops[op_1].op2_use = ssa->ops[op_1].op1_use; |
1233 | 22 | ssa->ops[op_1].op2_use_chain = ssa->ops[op_1].op1_use_chain; |
1234 | 22 | } |
1235 | 332 | } |
1236 | 6.46k | } |
1237 | 405k | } else if (opline->opcode == ZEND_CONCAT) { |
1238 | 4.00k | if (!(OP1_INFO() & MAY_BE_OBJECT) |
1239 | 2.63k | && !(OP2_INFO() & MAY_BE_OBJECT)) { |
1240 | 1.84k | opline->opcode = ZEND_FAST_CONCAT; |
1241 | 1.84k | } |
1242 | 401k | } else if (opline->opcode == ZEND_VERIFY_RETURN_TYPE |
1243 | 946 | && opline->op1_type != IS_CONST |
1244 | 880 | && ssa->ops[op_1].op1_def == v |
1245 | 880 | && ssa->ops[op_1].op1_use >= 0) { |
1246 | 880 | int orig_var = ssa->ops[op_1].op1_use; |
1247 | 880 | int ret = ssa->vars[v].use_chain; |
1248 | | |
1249 | 880 | if (ssa->ops[op_1].op1_use_chain == -1 |
1250 | 866 | && can_elide_return_type_check(ctx->script, op_array, ssa, &ssa->ops[op_1])) { |
1251 | | |
1252 | | // op_1: VERIFY_RETURN_TYPE #orig_var.? [T] -> #v.? [T] => NOP |
1253 | | |
1254 | 428 | zend_ssa_unlink_use_chain(ssa, op_1, orig_var); |
1255 | | |
1256 | 428 | if (ret >= 0) { |
1257 | 428 | ssa->ops[ret].op1_use = orig_var; |
1258 | 428 | ssa->ops[ret].op1_use_chain = ssa->vars[orig_var].use_chain; |
1259 | 428 | ssa->vars[orig_var].use_chain = ret; |
1260 | 428 | } |
1261 | | |
1262 | 428 | ssa->vars[v].definition = -1; |
1263 | 428 | ssa->vars[v].use_chain = -1; |
1264 | | |
1265 | 428 | ssa->ops[op_1].op1_def = -1; |
1266 | 428 | ssa->ops[op_1].op1_use = -1; |
1267 | | |
1268 | 428 | MAKE_NOP(opline); |
1269 | 428 | remove_nops = 1; |
1270 | 452 | } else if (ret >= 0 |
1271 | 452 | && ssa->ops[ret].op1_use == v |
1272 | 452 | && ssa->ops[ret].op1_use_chain == -1 |
1273 | 452 | && can_elide_return_type_check(ctx->script, op_array, ssa, &ssa->ops[op_1])) { |
1274 | | |
1275 | | // op_1: VERIFY_RETURN_TYPE #orig_var.? [T] -> #v.? [T] => NOP |
1276 | | |
1277 | 12 | zend_ssa_replace_use_chain(ssa, op_1, ret, orig_var); |
1278 | | |
1279 | 12 | ssa->ops[ret].op1_use = orig_var; |
1280 | 12 | ssa->ops[ret].op1_use_chain = ssa->ops[op_1].op1_use_chain; |
1281 | | |
1282 | 12 | ssa->vars[v].definition = -1; |
1283 | 12 | ssa->vars[v].use_chain = -1; |
1284 | | |
1285 | 12 | ssa->ops[op_1].op1_def = -1; |
1286 | 12 | ssa->ops[op_1].op1_use = -1; |
1287 | | |
1288 | 12 | MAKE_NOP(opline); |
1289 | 12 | remove_nops = 1; |
1290 | 12 | } |
1291 | 880 | } |
1292 | 422k | } |
1293 | | |
1294 | 422k | if (opline->opcode == ZEND_QM_ASSIGN |
1295 | 3.97k | && ssa->ops[op_1].result_def == v |
1296 | 3.97k | && opline->op1_type & (IS_TMP_VAR|IS_VAR) |
1297 | 399 | && !(ssa->var_info[v].type & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) |
1298 | 422k | ) { |
1299 | | |
1300 | 54 | int src_var = ssa->ops[op_1].op1_use; |
1301 | | |
1302 | 54 | if (src_var >= 0 |
1303 | 54 | && !(ssa->var_info[src_var].type & MAY_BE_REF) |
1304 | 54 | && (ssa->var_info[src_var].type & (MAY_BE_UNDEF|MAY_BE_ANY)) |
1305 | 54 | && ssa->vars[src_var].definition >= 0 |
1306 | 12 | && ssa->ops[ssa->vars[src_var].definition].result_def == src_var |
1307 | 12 | && ssa->ops[ssa->vars[src_var].definition].result_use < 0 |
1308 | 12 | && ssa->vars[src_var].use_chain == op_1 |
1309 | 12 | && ssa->ops[op_1].op1_use_chain < 0 |
1310 | 12 | && !ssa->vars[src_var].phi_use_chain |
1311 | 12 | && !ssa->vars[src_var].sym_use_chain |
1312 | 12 | && opline_supports_assign_contraction( |
1313 | 12 | op_array, ssa, &op_array->opcodes[ssa->vars[src_var].definition], |
1314 | 12 | src_var, opline->result.var) |
1315 | 12 | && !variable_defined_or_used_in_range(ssa, EX_VAR_TO_NUM(opline->result.var), |
1316 | 12 | ssa->vars[src_var].definition+1, op_1) |
1317 | 54 | ) { |
1318 | | |
1319 | 12 | int orig_var = ssa->ops[op_1].result_use; |
1320 | 12 | int op_2 = ssa->vars[src_var].definition; |
1321 | | |
1322 | | // op_2: #src_var.T = OP ... => #v.CV = OP ... |
1323 | | // op_1: QM_ASSIGN #src_var.T #orig_var.CV [undef,scalar] -> #v.CV, NOP |
1324 | | |
1325 | 12 | if (orig_var >= 0) { |
1326 | 0 | zend_ssa_unlink_use_chain(ssa, op_1, orig_var); |
1327 | 0 | } |
1328 | | |
1329 | | /* Reconstruct SSA */ |
1330 | 12 | ssa->vars[v].definition = op_2; |
1331 | 12 | ssa->ops[op_2].result_def = v; |
1332 | | |
1333 | 12 | ssa->vars[src_var].definition = -1; |
1334 | 12 | ssa->vars[src_var].use_chain = -1; |
1335 | | |
1336 | 12 | ssa->ops[op_1].op1_use = -1; |
1337 | 12 | ssa->ops[op_1].op1_def = -1; |
1338 | 12 | ssa->ops[op_1].op1_use_chain = -1; |
1339 | 12 | ssa->ops[op_1].result_use = -1; |
1340 | 12 | ssa->ops[op_1].result_def = -1; |
1341 | 12 | ssa->ops[op_1].res_use_chain = -1; |
1342 | | |
1343 | | /* Update opcodes */ |
1344 | 12 | op_array->opcodes[op_2].result_type = opline->result_type; |
1345 | 12 | op_array->opcodes[op_2].result.var = opline->result.var; |
1346 | | |
1347 | 12 | MAKE_NOP(opline); |
1348 | 12 | remove_nops = 1; |
1349 | | |
1350 | 12 | if (op_array->opcodes[op_2].opcode == ZEND_SUB |
1351 | 2 | && op_array->opcodes[op_2].op1_type == op_array->opcodes[op_2].result_type |
1352 | 2 | && op_array->opcodes[op_2].op1.var == op_array->opcodes[op_2].result.var |
1353 | 0 | && op_array->opcodes[op_2].op2_type == IS_CONST |
1354 | 0 | && Z_TYPE_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op2.constant)) == IS_LONG |
1355 | 0 | && Z_LVAL_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op2.constant)) == 1 |
1356 | 0 | && ssa->ops[op_2].op1_use >= 0 |
1357 | 0 | && !(ssa->var_info[ssa->ops[op_2].op1_use].type & (MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) { |
1358 | |
|
1359 | 0 | op_array->opcodes[op_2].opcode = ZEND_PRE_DEC; |
1360 | 0 | SET_UNUSED(op_array->opcodes[op_2].op2); |
1361 | 0 | SET_UNUSED(op_array->opcodes[op_2].result); |
1362 | |
|
1363 | 0 | ssa->ops[op_2].result_def = -1; |
1364 | 0 | ssa->ops[op_2].op1_def = v; |
1365 | |
|
1366 | 12 | } else if (op_array->opcodes[op_2].opcode == ZEND_ADD |
1367 | 0 | && op_array->opcodes[op_2].op1_type == op_array->opcodes[op_2].result_type |
1368 | 0 | && op_array->opcodes[op_2].op1.var == op_array->opcodes[op_2].result.var |
1369 | 0 | && op_array->opcodes[op_2].op2_type == IS_CONST |
1370 | 0 | && Z_TYPE_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op2.constant)) == IS_LONG |
1371 | 0 | && Z_LVAL_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op2.constant)) == 1 |
1372 | 0 | && ssa->ops[op_2].op1_use >= 0 |
1373 | 0 | && !(ssa->var_info[ssa->ops[op_2].op1_use].type & (MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) { |
1374 | |
|
1375 | 0 | op_array->opcodes[op_2].opcode = ZEND_PRE_INC; |
1376 | 0 | SET_UNUSED(op_array->opcodes[op_2].op2); |
1377 | 0 | SET_UNUSED(op_array->opcodes[op_2].result); |
1378 | |
|
1379 | 0 | ssa->ops[op_2].result_def = -1; |
1380 | 0 | ssa->ops[op_2].op1_def = v; |
1381 | |
|
1382 | 12 | } else if (op_array->opcodes[op_2].opcode == ZEND_ADD |
1383 | 0 | && op_array->opcodes[op_2].op2_type == op_array->opcodes[op_2].result_type |
1384 | 0 | && op_array->opcodes[op_2].op2.var == op_array->opcodes[op_2].result.var |
1385 | 0 | && op_array->opcodes[op_2].op1_type == IS_CONST |
1386 | 0 | && Z_TYPE_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op1.constant)) == IS_LONG |
1387 | 0 | && Z_LVAL_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op1.constant)) == 1 |
1388 | 0 | && ssa->ops[op_2].op2_use >= 0 |
1389 | 0 | && !(ssa->var_info[ssa->ops[op_2].op2_use].type & (MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) { |
1390 | |
|
1391 | 0 | op_array->opcodes[op_2].opcode = ZEND_PRE_INC; |
1392 | 0 | op_array->opcodes[op_2].op1_type = op_array->opcodes[op_2].op2_type; |
1393 | 0 | op_array->opcodes[op_2].op1.var = op_array->opcodes[op_2].op2.var; |
1394 | 0 | SET_UNUSED(op_array->opcodes[op_2].op2); |
1395 | 0 | SET_UNUSED(op_array->opcodes[op_2].result); |
1396 | |
|
1397 | 0 | ssa->ops[op_2].result_def = -1; |
1398 | 0 | ssa->ops[op_2].op1_def = v; |
1399 | 0 | ssa->ops[op_2].op1_use = ssa->ops[op_2].op2_use; |
1400 | 0 | ssa->ops[op_2].op1_use_chain = ssa->ops[op_2].op2_use_chain; |
1401 | 0 | ssa->ops[op_2].op2_use = -1; |
1402 | 0 | ssa->ops[op_2].op2_use_chain = -1; |
1403 | 0 | } |
1404 | 12 | } |
1405 | 54 | } |
1406 | | |
1407 | 422k | if (ssa->vars[v].var >= op_array->last_var) { |
1408 | | /* skip TMP and VAR */ |
1409 | 355k | continue; |
1410 | 355k | } |
1411 | | |
1412 | 67.7k | if (ssa->ops[op_1].op1_def == v |
1413 | 52.7k | && RETURN_VALUE_USED(opline)) { |
1414 | 11.4k | if (opline->opcode == ZEND_ASSIGN |
1415 | 6.64k | || opline->opcode == ZEND_ASSIGN_OP |
1416 | 5.53k | || opline->opcode == ZEND_PRE_INC |
1417 | 6.34k | || opline->opcode == ZEND_PRE_DEC) { |
1418 | 6.34k | zend_dfa_try_to_replace_result(op_array, ssa, op_1, v); |
1419 | 6.34k | } else if (opline->opcode == ZEND_POST_INC) { |
1420 | 462 | int result_var = ssa->ops[op_1].result_def; |
1421 | | |
1422 | 462 | if (result_var >= 0 |
1423 | 462 | && (ssa->var_info[result_var].type & ((MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_DOUBLE))) == 0) { |
1424 | 256 | int use = ssa->vars[result_var].use_chain; |
1425 | | |
1426 | 256 | if (use >= 0 && op_array->opcodes[use].opcode == ZEND_IS_SMALLER |
1427 | 20 | && ssa->ops[use].op1_use == result_var |
1428 | 20 | && zend_dfa_try_to_replace_result(op_array, ssa, op_1, v)) { |
1429 | 20 | opline->opcode = ZEND_PRE_INC; |
1430 | 20 | op_array->opcodes[use].opcode = ZEND_IS_SMALLER_OR_EQUAL; |
1431 | 20 | } |
1432 | 256 | } |
1433 | 4.60k | } else if (opline->opcode == ZEND_POST_DEC) { |
1434 | 370 | int result_var = ssa->ops[op_1].result_def; |
1435 | | |
1436 | 370 | if (result_var >= 0 |
1437 | 370 | && (ssa->var_info[result_var].type & ((MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_DOUBLE))) == 0) { |
1438 | 130 | int use = ssa->vars[result_var].use_chain; |
1439 | | |
1440 | 130 | if (use >= 0 && op_array->opcodes[use].opcode == ZEND_IS_SMALLER |
1441 | 2 | && ssa->ops[use].op2_use == result_var |
1442 | 2 | && zend_dfa_try_to_replace_result(op_array, ssa, op_1, v)) { |
1443 | 2 | opline->opcode = ZEND_PRE_DEC; |
1444 | 2 | op_array->opcodes[use].opcode = ZEND_IS_SMALLER_OR_EQUAL; |
1445 | 2 | } |
1446 | 130 | } |
1447 | 370 | } |
1448 | 11.4k | } |
1449 | | |
1450 | 67.7k | if (opline->opcode == ZEND_ASSIGN |
1451 | 27.7k | && ssa->ops[op_1].op1_def == v |
1452 | 27.7k | && !RETURN_VALUE_USED(opline) |
1453 | 67.7k | ) { |
1454 | 25.0k | int orig_var = ssa->ops[op_1].op1_use; |
1455 | | |
1456 | 25.0k | if (orig_var >= 0 |
1457 | 24.9k | && !(ssa->var_info[orig_var].type & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) |
1458 | 25.0k | ) { |
1459 | 5.93k | int src_var = ssa->ops[op_1].op2_use; |
1460 | | |
1461 | 5.93k | if ((opline->op2_type & (IS_TMP_VAR|IS_VAR)) |
1462 | 3.22k | && src_var >= 0 |
1463 | 3.22k | && !(ssa->var_info[src_var].type & MAY_BE_REF) |
1464 | 3.22k | && (ssa->var_info[src_var].type & (MAY_BE_UNDEF|MAY_BE_ANY)) |
1465 | 3.17k | && ssa->vars[src_var].definition >= 0 |
1466 | 2.98k | && ssa->ops[ssa->vars[src_var].definition].result_def == src_var |
1467 | 2.98k | && ssa->ops[ssa->vars[src_var].definition].result_use < 0 |
1468 | 2.94k | && ssa->vars[src_var].use_chain == op_1 |
1469 | 2.91k | && ssa->ops[op_1].op2_use_chain < 0 |
1470 | 2.91k | && !ssa->vars[src_var].phi_use_chain |
1471 | 2.91k | && !ssa->vars[src_var].sym_use_chain |
1472 | 2.91k | && opline_supports_assign_contraction( |
1473 | 2.91k | op_array, ssa, &op_array->opcodes[ssa->vars[src_var].definition], |
1474 | 2.91k | src_var, opline->op1.var) |
1475 | 2.01k | && !variable_defined_or_used_in_range(ssa, EX_VAR_TO_NUM(opline->op1.var), |
1476 | 2.01k | ssa->vars[src_var].definition+1, op_1) |
1477 | 5.93k | ) { |
1478 | | |
1479 | 2.01k | int op_2 = ssa->vars[src_var].definition; |
1480 | | |
1481 | | // op_2: #src_var.T = OP ... => #v.CV = OP ... |
1482 | | // op_1: ASSIGN #orig_var.CV [undef,scalar] -> #v.CV, #src_var.T NOP |
1483 | | |
1484 | 2.01k | zend_ssa_unlink_use_chain(ssa, op_1, orig_var); |
1485 | | /* Reconstruct SSA */ |
1486 | 2.01k | ssa->vars[v].definition = op_2; |
1487 | 2.01k | ssa->ops[op_2].result_def = v; |
1488 | | |
1489 | 2.01k | ssa->vars[src_var].definition = -1; |
1490 | 2.01k | ssa->vars[src_var].use_chain = -1; |
1491 | | |
1492 | 2.01k | ssa->ops[op_1].op1_use = -1; |
1493 | 2.01k | ssa->ops[op_1].op2_use = -1; |
1494 | 2.01k | ssa->ops[op_1].op1_def = -1; |
1495 | 2.01k | ssa->ops[op_1].op1_use_chain = -1; |
1496 | | |
1497 | | /* Update opcodes */ |
1498 | 2.01k | op_array->opcodes[op_2].result_type = opline->op1_type; |
1499 | 2.01k | op_array->opcodes[op_2].result.var = opline->op1.var; |
1500 | | |
1501 | 2.01k | MAKE_NOP(opline); |
1502 | 2.01k | remove_nops = 1; |
1503 | | |
1504 | 2.01k | if (op_array->opcodes[op_2].opcode == ZEND_SUB |
1505 | 155 | && op_array->opcodes[op_2].op1_type == op_array->opcodes[op_2].result_type |
1506 | 98 | && op_array->opcodes[op_2].op1.var == op_array->opcodes[op_2].result.var |
1507 | 6 | && op_array->opcodes[op_2].op2_type == IS_CONST |
1508 | 0 | && Z_TYPE_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op2.constant)) == IS_LONG |
1509 | 0 | && Z_LVAL_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op2.constant)) == 1 |
1510 | 0 | && ssa->ops[op_2].op1_use >= 0 |
1511 | 0 | && !(ssa->var_info[ssa->ops[op_2].op1_use].type & (MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) { |
1512 | |
|
1513 | 0 | op_array->opcodes[op_2].opcode = ZEND_PRE_DEC; |
1514 | 0 | SET_UNUSED(op_array->opcodes[op_2].op2); |
1515 | 0 | SET_UNUSED(op_array->opcodes[op_2].result); |
1516 | |
|
1517 | 0 | ssa->ops[op_2].result_def = -1; |
1518 | 0 | ssa->ops[op_2].op1_def = v; |
1519 | |
|
1520 | 2.01k | } else if (op_array->opcodes[op_2].opcode == ZEND_ADD |
1521 | 950 | && op_array->opcodes[op_2].op1_type == op_array->opcodes[op_2].result_type |
1522 | 555 | && op_array->opcodes[op_2].op1.var == op_array->opcodes[op_2].result.var |
1523 | 330 | && op_array->opcodes[op_2].op2_type == IS_CONST |
1524 | 20 | && Z_TYPE_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op2.constant)) == IS_LONG |
1525 | 0 | && Z_LVAL_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op2.constant)) == 1 |
1526 | 0 | && ssa->ops[op_2].op1_use >= 0 |
1527 | 0 | && !(ssa->var_info[ssa->ops[op_2].op1_use].type & (MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) { |
1528 | |
|
1529 | 0 | op_array->opcodes[op_2].opcode = ZEND_PRE_INC; |
1530 | 0 | SET_UNUSED(op_array->opcodes[op_2].op2); |
1531 | 0 | SET_UNUSED(op_array->opcodes[op_2].result); |
1532 | |
|
1533 | 0 | ssa->ops[op_2].result_def = -1; |
1534 | 0 | ssa->ops[op_2].op1_def = v; |
1535 | |
|
1536 | 2.01k | } else if (op_array->opcodes[op_2].opcode == ZEND_ADD |
1537 | 950 | && op_array->opcodes[op_2].op2_type == op_array->opcodes[op_2].result_type |
1538 | 527 | && op_array->opcodes[op_2].op2.var == op_array->opcodes[op_2].result.var |
1539 | 114 | && op_array->opcodes[op_2].op1_type == IS_CONST |
1540 | 4 | && Z_TYPE_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op1.constant)) == IS_LONG |
1541 | 0 | && Z_LVAL_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op1.constant)) == 1 |
1542 | 0 | && ssa->ops[op_2].op2_use >= 0 |
1543 | 0 | && !(ssa->var_info[ssa->ops[op_2].op2_use].type & (MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) { |
1544 | |
|
1545 | 0 | op_array->opcodes[op_2].opcode = ZEND_PRE_INC; |
1546 | 0 | op_array->opcodes[op_2].op1_type = op_array->opcodes[op_2].op2_type; |
1547 | 0 | op_array->opcodes[op_2].op1.var = op_array->opcodes[op_2].op2.var; |
1548 | 0 | SET_UNUSED(op_array->opcodes[op_2].op2); |
1549 | 0 | SET_UNUSED(op_array->opcodes[op_2].result); |
1550 | |
|
1551 | 0 | ssa->ops[op_2].result_def = -1; |
1552 | 0 | ssa->ops[op_2].op1_def = v; |
1553 | 0 | ssa->ops[op_2].op1_use = ssa->ops[op_2].op2_use; |
1554 | 0 | ssa->ops[op_2].op1_use_chain = ssa->ops[op_2].op2_use_chain; |
1555 | 0 | ssa->ops[op_2].op2_use = -1; |
1556 | 0 | ssa->ops[op_2].op2_use_chain = -1; |
1557 | 0 | } |
1558 | 3.92k | } else if (opline->op2_type == IS_CONST |
1559 | 2.08k | || ((opline->op2_type & (IS_TMP_VAR|IS_VAR|IS_CV)) |
1560 | 2.08k | && ssa->ops[op_1].op2_use >= 0 |
1561 | 2.08k | && ssa->ops[op_1].op2_def < 0) |
1562 | 3.92k | ) { |
1563 | | |
1564 | | // op_1: ASSIGN #orig_var.CV [undef,scalar] -> #v.CV, CONST|TMPVAR => QM_ASSIGN v.CV, CONST|TMPVAR |
1565 | | |
1566 | 3.92k | if (ssa->ops[op_1].op1_use != ssa->ops[op_1].op2_use) { |
1567 | 3.72k | zend_ssa_unlink_use_chain(ssa, op_1, orig_var); |
1568 | 3.72k | } else { |
1569 | 196 | ssa->ops[op_1].op2_use_chain = ssa->ops[op_1].op1_use_chain; |
1570 | 196 | } |
1571 | | |
1572 | | /* Reconstruct SSA */ |
1573 | 3.92k | ssa->ops[op_1].result_def = v; |
1574 | 3.92k | ssa->ops[op_1].op1_def = -1; |
1575 | 3.92k | ssa->ops[op_1].op1_use = ssa->ops[op_1].op2_use; |
1576 | 3.92k | ssa->ops[op_1].op1_use_chain = ssa->ops[op_1].op2_use_chain; |
1577 | 3.92k | ssa->ops[op_1].op2_use = -1; |
1578 | 3.92k | ssa->ops[op_1].op2_use_chain = -1; |
1579 | | |
1580 | | /* Update opcode */ |
1581 | 3.92k | opline->result_type = opline->op1_type; |
1582 | 3.92k | opline->result.var = opline->op1.var; |
1583 | 3.92k | opline->op1_type = opline->op2_type; |
1584 | 3.92k | opline->op1.var = opline->op2.var; |
1585 | 3.92k | opline->op2_type = IS_UNUSED; |
1586 | 3.92k | opline->op2.var = 0; |
1587 | 3.92k | opline->opcode = ZEND_QM_ASSIGN; |
1588 | 3.92k | } |
1589 | 5.93k | } |
1590 | | |
1591 | 42.6k | } else if (opline->opcode == ZEND_ASSIGN_OP |
1592 | 2.87k | && opline->extended_value == ZEND_ADD |
1593 | 933 | && ssa->ops[op_1].op1_def == v |
1594 | 933 | && opline->op2_type == IS_CONST |
1595 | 116 | && Z_TYPE_P(CT_CONSTANT_EX(op_array, opline->op2.constant)) == IS_LONG |
1596 | 91 | && Z_LVAL_P(CT_CONSTANT_EX(op_array, opline->op2.constant)) == 1 |
1597 | 23 | && ssa->ops[op_1].op1_use >= 0 |
1598 | 23 | && !(ssa->var_info[ssa->ops[op_1].op1_use].type & (MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) { |
1599 | | |
1600 | | // op_1: ASSIGN_ADD #?.CV [undef,null,int,foat] ->#v.CV, int(1) => PRE_INC #?.CV ->#v.CV |
1601 | | |
1602 | 5 | opline->opcode = ZEND_PRE_INC; |
1603 | 5 | opline->extended_value = 0; |
1604 | 5 | SET_UNUSED(opline->op2); |
1605 | | |
1606 | 42.6k | } else if (opline->opcode == ZEND_ASSIGN_OP |
1607 | 2.87k | && opline->extended_value == ZEND_SUB |
1608 | 173 | && ssa->ops[op_1].op1_def == v |
1609 | 173 | && opline->op2_type == IS_CONST |
1610 | 69 | && Z_TYPE_P(CT_CONSTANT_EX(op_array, opline->op2.constant)) == IS_LONG |
1611 | 63 | && Z_LVAL_P(CT_CONSTANT_EX(op_array, opline->op2.constant)) == 1 |
1612 | 4 | && ssa->ops[op_1].op1_use >= 0 |
1613 | 4 | && !(ssa->var_info[ssa->ops[op_1].op1_use].type & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) { |
1614 | | |
1615 | | // op_1: ASSIGN_SUB #?.CV [undef,null,int,foat] -> #v.CV, int(1) => PRE_DEC #?.CV ->#v.CV |
1616 | |
|
1617 | 0 | opline->opcode = ZEND_PRE_DEC; |
1618 | 0 | opline->extended_value = 0; |
1619 | 0 | SET_UNUSED(opline->op2); |
1620 | |
|
1621 | 42.6k | } else if (ssa->ops[op_1].op1_def == v |
1622 | 27.6k | && !RETURN_VALUE_USED(opline) |
1623 | 19.1k | && ssa->ops[op_1].op1_use >= 0 |
1624 | 19.1k | && !(ssa->var_info[ssa->ops[op_1].op1_use].type & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) |
1625 | 5.92k | && opline->opcode == ZEND_ASSIGN_OP |
1626 | 1.15k | && opline->extended_value != ZEND_CONCAT) { |
1627 | | |
1628 | | // op_1: ASSIGN_OP #orig_var.CV [undef,null,bool,int,double] -> #v.CV, ? => #v.CV = ADD #orig_var.CV, ? |
1629 | | |
1630 | | /* Reconstruct SSA */ |
1631 | 991 | ssa->ops[op_1].result_def = ssa->ops[op_1].op1_def; |
1632 | 991 | ssa->ops[op_1].op1_def = -1; |
1633 | | |
1634 | | /* Update opcode */ |
1635 | 991 | opline->opcode = opline->extended_value; |
1636 | 991 | opline->extended_value = 0; |
1637 | 991 | opline->result_type = opline->op1_type; |
1638 | 991 | opline->result.var = opline->op1.var; |
1639 | | |
1640 | 991 | } |
1641 | 67.7k | } |
1642 | | |
1643 | 37.1k | #if ZEND_DEBUG_DFA |
1644 | 37.1k | ssa_verify_integrity(op_array, ssa, "after dfa"); |
1645 | 37.1k | #endif |
1646 | | |
1647 | 37.1k | if (remove_nops) { |
1648 | 3.85k | zend_ssa_remove_nops(op_array, ssa, ctx); |
1649 | 3.85k | #if ZEND_DEBUG_DFA |
1650 | 3.85k | ssa_verify_integrity(op_array, ssa, "after nop"); |
1651 | 3.85k | #endif |
1652 | 3.85k | } |
1653 | 37.1k | } |
1654 | | |
1655 | 37.1k | if (ctx->debug_level & ZEND_DUMP_AFTER_DFA_PASS) { |
1656 | 0 | zend_dump_op_array(op_array, ZEND_DUMP_SSA, "after dfa pass", ssa); |
1657 | 0 | } |
1658 | 37.1k | } |
1659 | | |
1660 | | void zend_optimize_dfa(zend_op_array *op_array, zend_optimizer_ctx *ctx) |
1661 | 0 | { |
1662 | 0 | void *checkpoint = zend_arena_checkpoint(ctx->arena); |
1663 | 0 | zend_ssa ssa; |
1664 | |
|
1665 | 0 | if (zend_dfa_analyze_op_array(op_array, ctx, &ssa) == FAILURE) { |
1666 | 0 | zend_arena_release(&ctx->arena, checkpoint); |
1667 | 0 | return; |
1668 | 0 | } |
1669 | | |
1670 | 0 | zend_dfa_optimize_op_array(op_array, ctx, &ssa, NULL); |
1671 | | |
1672 | | /* Destroy SSA */ |
1673 | 0 | zend_arena_release(&ctx->arena, checkpoint); |
1674 | 0 | } |