1 /*
2 +----------------------------------------------------------------------+
3 | Zend OPcache |
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: Andi Gutmans <andi@php.net> |
16 | Zeev Suraski <zeev@php.net> |
17 | Stanislav Malyshev <stas@zend.com> |
18 | Dmitry Stogov <dmitry@php.net> |
19 +----------------------------------------------------------------------+
20 */
21
22 #include "Optimizer/zend_optimizer.h"
23 #include "Optimizer/zend_optimizer_internal.h"
24 #include "zend_API.h"
25 #include "zend_constants.h"
26 #include "zend_execute.h"
27 #include "zend_vm.h"
28 #include "zend_cfg.h"
29 #include "zend_func_info.h"
30 #include "zend_call_graph.h"
31 #include "zend_inference.h"
32 #include "zend_dump.h"
33 #include "php.h"
34
35 #ifndef ZEND_OPTIMIZER_MAX_REGISTERED_PASSES
36 # define ZEND_OPTIMIZER_MAX_REGISTERED_PASSES 32
37 #endif
38
39 struct {
40 zend_optimizer_pass_t pass[ZEND_OPTIMIZER_MAX_REGISTERED_PASSES];
41 int last;
42 } zend_optimizer_registered_passes = {{NULL}, 0};
43
zend_optimizer_collect_constant(zend_optimizer_ctx * ctx,zval * name,zval * value)44 void zend_optimizer_collect_constant(zend_optimizer_ctx *ctx, zval *name, zval* value)
45 {
46 if (!ctx->constants) {
47 ctx->constants = zend_arena_alloc(&ctx->arena, sizeof(HashTable));
48 zend_hash_init(ctx->constants, 16, NULL, zval_ptr_dtor_nogc, 0);
49 }
50
51 if (zend_hash_add(ctx->constants, Z_STR_P(name), value)) {
52 Z_TRY_ADDREF_P(value);
53 }
54 }
55
zend_optimizer_eval_binary_op(zval * result,zend_uchar opcode,zval * op1,zval * op2)56 zend_result zend_optimizer_eval_binary_op(zval *result, zend_uchar opcode, zval *op1, zval *op2) /* {{{ */
57 {
58 if (zend_binary_op_produces_error(opcode, op1, op2)) {
59 return FAILURE;
60 }
61
62 binary_op_type binary_op = get_binary_op(opcode);
63 return binary_op(result, op1, op2);
64 }
65 /* }}} */
66
zend_optimizer_eval_unary_op(zval * result,zend_uchar opcode,zval * op1)67 zend_result zend_optimizer_eval_unary_op(zval *result, zend_uchar opcode, zval *op1) /* {{{ */
68 {
69 unary_op_type unary_op = get_unary_op(opcode);
70
71 if (unary_op) {
72 if (zend_unary_op_produces_error(opcode, op1)) {
73 return FAILURE;
74 }
75 return unary_op(result, op1);
76 } else { /* ZEND_BOOL */
77 ZVAL_BOOL(result, zend_is_true(op1));
78 return SUCCESS;
79 }
80 }
81 /* }}} */
82
zend_optimizer_eval_cast(zval * result,uint32_t type,zval * op1)83 zend_result zend_optimizer_eval_cast(zval *result, uint32_t type, zval *op1) /* {{{ */
84 {
85 switch (type) {
86 case IS_NULL:
87 ZVAL_NULL(result);
88 return SUCCESS;
89 case _IS_BOOL:
90 ZVAL_BOOL(result, zval_is_true(op1));
91 return SUCCESS;
92 case IS_LONG:
93 ZVAL_LONG(result, zval_get_long(op1));
94 return SUCCESS;
95 case IS_DOUBLE:
96 ZVAL_DOUBLE(result, zval_get_double(op1));
97 return SUCCESS;
98 case IS_STRING:
99 /* Conversion from double to string takes into account run-time
100 'precision' setting and cannot be evaluated at compile-time */
101 if (Z_TYPE_P(op1) != IS_ARRAY && Z_TYPE_P(op1) != IS_DOUBLE) {
102 ZVAL_STR(result, zval_get_string(op1));
103 return SUCCESS;
104 }
105 break;
106 case IS_ARRAY:
107 ZVAL_COPY(result, op1);
108 convert_to_array(result);
109 return SUCCESS;
110 }
111 return FAILURE;
112 }
113 /* }}} */
114
zend_optimizer_eval_strlen(zval * result,zval * op1)115 zend_result zend_optimizer_eval_strlen(zval *result, zval *op1) /* {{{ */
116 {
117 if (Z_TYPE_P(op1) != IS_STRING) {
118 return FAILURE;
119 }
120 ZVAL_LONG(result, Z_STRLEN_P(op1));
121 return SUCCESS;
122 }
123 /* }}} */
124
zend_optimizer_eval_special_func_call(zval * result,zend_string * name,zend_string * arg)125 zend_result zend_optimizer_eval_special_func_call(
126 zval *result, zend_string *name, zend_string *arg) {
127 if (zend_string_equals_literal(name, "function_exists") ||
128 zend_string_equals_literal(name, "is_callable")) {
129 zend_string *lc_name = zend_string_tolower(arg);
130 zend_internal_function *func = zend_hash_find_ptr(EG(function_table), lc_name);
131 zend_string_release_ex(lc_name, 0);
132
133 if (func && func->type == ZEND_INTERNAL_FUNCTION
134 && func->module->type == MODULE_PERSISTENT
135 #ifdef ZEND_WIN32
136 && func->module->handle == NULL
137 #endif
138 ) {
139 ZVAL_TRUE(result);
140 return SUCCESS;
141 }
142 return FAILURE;
143 }
144 if (zend_string_equals_literal(name, "extension_loaded")) {
145 zend_string *lc_name = zend_string_tolower(arg);
146 zend_module_entry *m = zend_hash_find_ptr(&module_registry, lc_name);
147 zend_string_release_ex(lc_name, 0);
148
149 if (!m) {
150 if (PG(enable_dl)) {
151 return FAILURE;
152 }
153 ZVAL_FALSE(result);
154 return SUCCESS;
155 }
156
157 if (m->type == MODULE_PERSISTENT
158 #ifdef ZEND_WIN32
159 && m->handle == NULL
160 #endif
161 ) {
162 ZVAL_TRUE(result);
163 return SUCCESS;
164 }
165 return FAILURE;
166 }
167 if (zend_string_equals_literal(name, "constant")) {
168 return zend_optimizer_get_persistent_constant(arg, result, 1) ? SUCCESS : FAILURE;
169 }
170 if (zend_string_equals_literal(name, "dirname")) {
171 if (!IS_ABSOLUTE_PATH(ZSTR_VAL(arg), ZSTR_LEN(arg))) {
172 return FAILURE;
173 }
174
175 zend_string *dirname = zend_string_init(ZSTR_VAL(arg), ZSTR_LEN(arg), 0);
176 ZSTR_LEN(dirname) = zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
177 if (IS_ABSOLUTE_PATH(ZSTR_VAL(dirname), ZSTR_LEN(dirname))) {
178 ZVAL_STR(result, dirname);
179 return SUCCESS;
180 }
181 zend_string_release_ex(dirname, 0);
182 return FAILURE;
183 }
184 if (zend_string_equals_literal(name, "ini_get")) {
185 zend_ini_entry *ini_entry = zend_hash_find_ptr(EG(ini_directives), arg);
186 if (!ini_entry) {
187 if (PG(enable_dl)) {
188 return FAILURE;
189 }
190 ZVAL_FALSE(result);
191 } else if (ini_entry->modifiable != ZEND_INI_SYSTEM) {
192 return FAILURE;
193 } else if (ini_entry->value) {
194 ZVAL_STR_COPY(result, ini_entry->value);
195 } else {
196 ZVAL_EMPTY_STRING(result);
197 }
198 return SUCCESS;
199 }
200 return FAILURE;
201 }
202
zend_optimizer_get_collected_constant(HashTable * constants,zval * name,zval * value)203 bool zend_optimizer_get_collected_constant(HashTable *constants, zval *name, zval* value)
204 {
205 zval *val;
206
207 if ((val = zend_hash_find(constants, Z_STR_P(name))) != NULL) {
208 ZVAL_COPY(value, val);
209 return 1;
210 }
211 return 0;
212 }
213
zend_optimizer_convert_to_free_op1(zend_op_array * op_array,zend_op * opline)214 void zend_optimizer_convert_to_free_op1(zend_op_array *op_array, zend_op *opline)
215 {
216 if (opline->op1_type == IS_CV) {
217 opline->opcode = ZEND_CHECK_VAR;
218 SET_UNUSED(opline->op2);
219 SET_UNUSED(opline->result);
220 opline->extended_value = 0;
221 } else if (opline->op1_type & (IS_TMP_VAR|IS_VAR)) {
222 opline->opcode = ZEND_FREE;
223 SET_UNUSED(opline->op2);
224 SET_UNUSED(opline->result);
225 opline->extended_value = 0;
226 } else {
227 ZEND_ASSERT(opline->op1_type == IS_CONST);
228 literal_dtor(&ZEND_OP1_LITERAL(opline));
229 MAKE_NOP(opline);
230 }
231 }
232
zend_optimizer_add_literal(zend_op_array * op_array,zval * zv)233 int zend_optimizer_add_literal(zend_op_array *op_array, zval *zv)
234 {
235 int i = op_array->last_literal;
236 op_array->last_literal++;
237 op_array->literals = (zval*)erealloc(op_array->literals, op_array->last_literal * sizeof(zval));
238 ZVAL_COPY_VALUE(&op_array->literals[i], zv);
239 Z_EXTRA(op_array->literals[i]) = 0;
240 return i;
241 }
242
zend_optimizer_add_literal_string(zend_op_array * op_array,zend_string * str)243 static inline int zend_optimizer_add_literal_string(zend_op_array *op_array, zend_string *str) {
244 zval zv;
245 ZVAL_STR(&zv, str);
246 zend_string_hash_val(str);
247 return zend_optimizer_add_literal(op_array, &zv);
248 }
249
drop_leading_backslash(zval * val)250 static inline void drop_leading_backslash(zval *val) {
251 if (Z_STRVAL_P(val)[0] == '\\') {
252 zend_string *str = zend_string_init(Z_STRVAL_P(val) + 1, Z_STRLEN_P(val) - 1, 0);
253 zval_ptr_dtor_nogc(val);
254 ZVAL_STR(val, str);
255 }
256 }
257
alloc_cache_slots(zend_op_array * op_array,uint32_t num)258 static inline uint32_t alloc_cache_slots(zend_op_array *op_array, uint32_t num) {
259 uint32_t ret = op_array->cache_size;
260 op_array->cache_size += num * sizeof(void *);
261 return ret;
262 }
263
264 #define REQUIRES_STRING(val) do { \
265 if (Z_TYPE_P(val) != IS_STRING) { \
266 return 0; \
267 } \
268 } while (0)
269
270 #define TO_STRING_NOWARN(val) do { \
271 if (Z_TYPE_P(val) >= IS_ARRAY) { \
272 return 0; \
273 } \
274 convert_to_string(val); \
275 } while (0)
276
zend_optimizer_update_op1_const(zend_op_array * op_array,zend_op * opline,zval * val)277 bool zend_optimizer_update_op1_const(zend_op_array *op_array,
278 zend_op *opline,
279 zval *val)
280 {
281 switch (opline->opcode) {
282 case ZEND_OP_DATA:
283 switch ((opline-1)->opcode) {
284 case ZEND_ASSIGN_OBJ_REF:
285 case ZEND_ASSIGN_STATIC_PROP_REF:
286 return 0;
287 }
288 opline->op1.constant = zend_optimizer_add_literal(op_array, val);
289 break;
290 case ZEND_FREE:
291 case ZEND_CHECK_VAR:
292 MAKE_NOP(opline);
293 zval_ptr_dtor_nogc(val);
294 return 1;
295 case ZEND_SEND_VAR_EX:
296 case ZEND_SEND_FUNC_ARG:
297 case ZEND_FETCH_DIM_W:
298 case ZEND_FETCH_DIM_RW:
299 case ZEND_FETCH_DIM_FUNC_ARG:
300 case ZEND_FETCH_DIM_UNSET:
301 case ZEND_FETCH_LIST_W:
302 case ZEND_ASSIGN_DIM:
303 case ZEND_RETURN_BY_REF:
304 case ZEND_INSTANCEOF:
305 case ZEND_MAKE_REF:
306 case ZEND_SEPARATE:
307 case ZEND_SEND_VAR_NO_REF:
308 case ZEND_SEND_VAR_NO_REF_EX:
309 return 0;
310 case ZEND_CATCH:
311 REQUIRES_STRING(val);
312 drop_leading_backslash(val);
313 opline->op1.constant = zend_optimizer_add_literal(op_array, val);
314 opline->extended_value = alloc_cache_slots(op_array, 1) | (opline->extended_value & ZEND_LAST_CATCH);
315 zend_optimizer_add_literal_string(op_array, zend_string_tolower(Z_STR_P(val)));
316 break;
317 case ZEND_DEFINED:
318 REQUIRES_STRING(val);
319 drop_leading_backslash(val);
320 opline->op1.constant = zend_optimizer_add_literal(op_array, val);
321 opline->extended_value = alloc_cache_slots(op_array, 1);
322 zend_optimizer_add_literal_string(op_array, zend_string_tolower(Z_STR_P(val)));
323 break;
324 case ZEND_NEW:
325 REQUIRES_STRING(val);
326 drop_leading_backslash(val);
327 opline->op1.constant = zend_optimizer_add_literal(op_array, val);
328 opline->op2.num = alloc_cache_slots(op_array, 1);
329 zend_optimizer_add_literal_string(op_array, zend_string_tolower(Z_STR_P(val)));
330 break;
331 case ZEND_INIT_STATIC_METHOD_CALL:
332 REQUIRES_STRING(val);
333 drop_leading_backslash(val);
334 opline->op1.constant = zend_optimizer_add_literal(op_array, val);
335 if (opline->op2_type != IS_CONST) {
336 opline->result.num = alloc_cache_slots(op_array, 1);
337 }
338 zend_optimizer_add_literal_string(op_array, zend_string_tolower(Z_STR_P(val)));
339 break;
340 case ZEND_FETCH_CLASS_CONSTANT:
341 REQUIRES_STRING(val);
342 drop_leading_backslash(val);
343 opline->op1.constant = zend_optimizer_add_literal(op_array, val);
344 if (opline->op2_type != IS_CONST) {
345 opline->extended_value = alloc_cache_slots(op_array, 1);
346 }
347 zend_optimizer_add_literal_string(op_array, zend_string_tolower(Z_STR_P(val)));
348 break;
349 case ZEND_ASSIGN_OP:
350 case ZEND_ASSIGN_DIM_OP:
351 case ZEND_ASSIGN_OBJ_OP:
352 break;
353 case ZEND_ASSIGN_STATIC_PROP_OP:
354 case ZEND_ASSIGN_STATIC_PROP:
355 case ZEND_ASSIGN_STATIC_PROP_REF:
356 case ZEND_FETCH_STATIC_PROP_R:
357 case ZEND_FETCH_STATIC_PROP_W:
358 case ZEND_FETCH_STATIC_PROP_RW:
359 case ZEND_FETCH_STATIC_PROP_IS:
360 case ZEND_FETCH_STATIC_PROP_UNSET:
361 case ZEND_FETCH_STATIC_PROP_FUNC_ARG:
362 case ZEND_UNSET_STATIC_PROP:
363 case ZEND_ISSET_ISEMPTY_STATIC_PROP:
364 case ZEND_PRE_INC_STATIC_PROP:
365 case ZEND_PRE_DEC_STATIC_PROP:
366 case ZEND_POST_INC_STATIC_PROP:
367 case ZEND_POST_DEC_STATIC_PROP:
368 TO_STRING_NOWARN(val);
369 opline->op1.constant = zend_optimizer_add_literal(op_array, val);
370 if (opline->op2_type == IS_CONST && (opline->extended_value & ~ZEND_FETCH_OBJ_FLAGS) + sizeof(void*) == op_array->cache_size) {
371 op_array->cache_size += sizeof(void *);
372 } else {
373 opline->extended_value = alloc_cache_slots(op_array, 3) | (opline->extended_value & ZEND_FETCH_OBJ_FLAGS);
374 }
375 break;
376 case ZEND_SEND_VAR:
377 opline->opcode = ZEND_SEND_VAL;
378 opline->op1.constant = zend_optimizer_add_literal(op_array, val);
379 break;
380 case ZEND_CASE:
381 opline->opcode = ZEND_IS_EQUAL;
382 opline->op1.constant = zend_optimizer_add_literal(op_array, val);
383 break;
384 case ZEND_CASE_STRICT:
385 opline->opcode = ZEND_IS_IDENTICAL;
386 opline->op1.constant = zend_optimizer_add_literal(op_array, val);
387 break;
388 case ZEND_VERIFY_RETURN_TYPE:
389 /* This would require a non-local change.
390 * zend_optimizer_replace_by_const() supports this. */
391 return 0;
392 case ZEND_COPY_TMP:
393 case ZEND_FETCH_CLASS_NAME:
394 return 0;
395 case ZEND_ECHO:
396 {
397 zval zv;
398 if (Z_TYPE_P(val) != IS_STRING && zend_optimizer_eval_cast(&zv, IS_STRING, val) == SUCCESS) {
399 zval_ptr_dtor_nogc(val);
400 val = &zv;
401 }
402 opline->op1.constant = zend_optimizer_add_literal(op_array, val);
403 if (Z_TYPE_P(val) == IS_STRING && Z_STRLEN_P(val) == 0) {
404 MAKE_NOP(opline);
405 return 1;
406 }
407 /* TODO: In a subsequent pass, *after* this step and compacting nops, combine consecutive ZEND_ECHOs using the block information from ssa->cfg */
408 /* (e.g. for ext/opcache/tests/opt/sccp_010.phpt) */
409 break;
410 }
411 case ZEND_CONCAT:
412 case ZEND_FAST_CONCAT:
413 case ZEND_FETCH_R:
414 case ZEND_FETCH_W:
415 case ZEND_FETCH_RW:
416 case ZEND_FETCH_IS:
417 case ZEND_FETCH_UNSET:
418 case ZEND_FETCH_FUNC_ARG:
419 case ZEND_ISSET_ISEMPTY_VAR:
420 case ZEND_UNSET_VAR:
421 TO_STRING_NOWARN(val);
422 if (opline->opcode == ZEND_CONCAT && opline->op2_type == IS_CONST) {
423 opline->opcode = ZEND_FAST_CONCAT;
424 }
425 ZEND_FALLTHROUGH;
426 default:
427 opline->op1.constant = zend_optimizer_add_literal(op_array, val);
428 break;
429 }
430
431 opline->op1_type = IS_CONST;
432 if (Z_TYPE(ZEND_OP1_LITERAL(opline)) == IS_STRING) {
433 zend_string_hash_val(Z_STR(ZEND_OP1_LITERAL(opline)));
434 }
435 return 1;
436 }
437
zend_optimizer_update_op2_const(zend_op_array * op_array,zend_op * opline,zval * val)438 bool zend_optimizer_update_op2_const(zend_op_array *op_array,
439 zend_op *opline,
440 zval *val)
441 {
442 zval tmp;
443
444 switch (opline->opcode) {
445 case ZEND_ASSIGN_REF:
446 case ZEND_FAST_CALL:
447 return 0;
448 case ZEND_FETCH_CLASS:
449 case ZEND_INSTANCEOF:
450 REQUIRES_STRING(val);
451 drop_leading_backslash(val);
452 opline->op2.constant = zend_optimizer_add_literal(op_array, val);
453 zend_optimizer_add_literal_string(op_array, zend_string_tolower(Z_STR_P(val)));
454 opline->extended_value = alloc_cache_slots(op_array, 1);
455 break;
456 case ZEND_INIT_FCALL_BY_NAME:
457 REQUIRES_STRING(val);
458 drop_leading_backslash(val);
459 opline->op2.constant = zend_optimizer_add_literal(op_array, val);
460 zend_optimizer_add_literal_string(op_array, zend_string_tolower(Z_STR_P(val)));
461 opline->result.num = alloc_cache_slots(op_array, 1);
462 break;
463 case ZEND_ASSIGN_STATIC_PROP:
464 case ZEND_ASSIGN_STATIC_PROP_REF:
465 case ZEND_FETCH_STATIC_PROP_R:
466 case ZEND_FETCH_STATIC_PROP_W:
467 case ZEND_FETCH_STATIC_PROP_RW:
468 case ZEND_FETCH_STATIC_PROP_IS:
469 case ZEND_FETCH_STATIC_PROP_UNSET:
470 case ZEND_FETCH_STATIC_PROP_FUNC_ARG:
471 case ZEND_UNSET_STATIC_PROP:
472 case ZEND_ISSET_ISEMPTY_STATIC_PROP:
473 case ZEND_PRE_INC_STATIC_PROP:
474 case ZEND_PRE_DEC_STATIC_PROP:
475 case ZEND_POST_INC_STATIC_PROP:
476 case ZEND_POST_DEC_STATIC_PROP:
477 case ZEND_ASSIGN_STATIC_PROP_OP:
478 REQUIRES_STRING(val);
479 drop_leading_backslash(val);
480 opline->op2.constant = zend_optimizer_add_literal(op_array, val);
481 zend_optimizer_add_literal_string(op_array, zend_string_tolower(Z_STR_P(val)));
482 if (opline->op1_type != IS_CONST) {
483 opline->extended_value = alloc_cache_slots(op_array, 1) | (opline->extended_value & (ZEND_RETURNS_FUNCTION|ZEND_ISEMPTY|ZEND_FETCH_OBJ_FLAGS));
484 }
485 break;
486 case ZEND_INIT_FCALL:
487 REQUIRES_STRING(val);
488 if (Z_REFCOUNT_P(val) == 1) {
489 zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val));
490 } else {
491 ZVAL_STR(&tmp, zend_string_tolower(Z_STR_P(val)));
492 zval_ptr_dtor_nogc(val);
493 val = &tmp;
494 }
495 opline->op2.constant = zend_optimizer_add_literal(op_array, val);
496 opline->result.num = alloc_cache_slots(op_array, 1);
497 break;
498 case ZEND_INIT_DYNAMIC_CALL:
499 if (Z_TYPE_P(val) == IS_STRING) {
500 if (zend_memrchr(Z_STRVAL_P(val), ':', Z_STRLEN_P(val))) {
501 return 0;
502 }
503
504 if (zend_optimizer_classify_function(Z_STR_P(val), opline->extended_value)) {
505 /* Dynamic call to various special functions must stay dynamic,
506 * otherwise would drop a warning */
507 return 0;
508 }
509
510 opline->opcode = ZEND_INIT_FCALL_BY_NAME;
511 drop_leading_backslash(val);
512 opline->op2.constant = zend_optimizer_add_literal(op_array, val);
513 zend_optimizer_add_literal_string(op_array, zend_string_tolower(Z_STR_P(val)));
514 opline->result.num = alloc_cache_slots(op_array, 1);
515 } else {
516 opline->op2.constant = zend_optimizer_add_literal(op_array, val);
517 }
518 break;
519 case ZEND_INIT_METHOD_CALL:
520 REQUIRES_STRING(val);
521 opline->op2.constant = zend_optimizer_add_literal(op_array, val);
522 zend_optimizer_add_literal_string(op_array, zend_string_tolower(Z_STR_P(val)));
523 opline->result.num = alloc_cache_slots(op_array, 2);
524 break;
525 case ZEND_INIT_STATIC_METHOD_CALL:
526 REQUIRES_STRING(val);
527 opline->op2.constant = zend_optimizer_add_literal(op_array, val);
528 zend_optimizer_add_literal_string(op_array, zend_string_tolower(Z_STR_P(val)));
529 if (opline->op1_type != IS_CONST) {
530 opline->result.num = alloc_cache_slots(op_array, 2);
531 }
532 break;
533 case ZEND_ASSIGN_OBJ:
534 case ZEND_ASSIGN_OBJ_REF:
535 case ZEND_FETCH_OBJ_R:
536 case ZEND_FETCH_OBJ_W:
537 case ZEND_FETCH_OBJ_RW:
538 case ZEND_FETCH_OBJ_IS:
539 case ZEND_FETCH_OBJ_UNSET:
540 case ZEND_FETCH_OBJ_FUNC_ARG:
541 case ZEND_UNSET_OBJ:
542 case ZEND_PRE_INC_OBJ:
543 case ZEND_PRE_DEC_OBJ:
544 case ZEND_POST_INC_OBJ:
545 case ZEND_POST_DEC_OBJ:
546 TO_STRING_NOWARN(val);
547 opline->op2.constant = zend_optimizer_add_literal(op_array, val);
548 opline->extended_value = alloc_cache_slots(op_array, 3);
549 break;
550 case ZEND_ASSIGN_OBJ_OP:
551 TO_STRING_NOWARN(val);
552 opline->op2.constant = zend_optimizer_add_literal(op_array, val);
553 ZEND_ASSERT((opline + 1)->opcode == ZEND_OP_DATA);
554 (opline + 1)->extended_value = alloc_cache_slots(op_array, 3);
555 break;
556 case ZEND_ISSET_ISEMPTY_PROP_OBJ:
557 TO_STRING_NOWARN(val);
558 opline->op2.constant = zend_optimizer_add_literal(op_array, val);
559 opline->extended_value = alloc_cache_slots(op_array, 3) | (opline->extended_value & ZEND_ISEMPTY);
560 break;
561 case ZEND_ASSIGN_DIM_OP:
562 case ZEND_ISSET_ISEMPTY_DIM_OBJ:
563 case ZEND_ASSIGN_DIM:
564 case ZEND_UNSET_DIM:
565 case ZEND_FETCH_DIM_R:
566 case ZEND_FETCH_DIM_W:
567 case ZEND_FETCH_DIM_RW:
568 case ZEND_FETCH_DIM_IS:
569 case ZEND_FETCH_DIM_FUNC_ARG:
570 case ZEND_FETCH_DIM_UNSET:
571 case ZEND_FETCH_LIST_R:
572 case ZEND_FETCH_LIST_W:
573 if (Z_TYPE_P(val) == IS_STRING) {
574 zend_ulong index;
575
576 if (ZEND_HANDLE_NUMERIC(Z_STR_P(val), index)) {
577 ZVAL_LONG(&tmp, index);
578 opline->op2.constant = zend_optimizer_add_literal(op_array, &tmp);
579 zend_string_hash_val(Z_STR_P(val));
580 zend_optimizer_add_literal(op_array, val);
581 Z_EXTRA(op_array->literals[opline->op2.constant]) = ZEND_EXTRA_VALUE;
582 break;
583 }
584 }
585 opline->op2.constant = zend_optimizer_add_literal(op_array, val);
586 break;
587 case ZEND_ADD_ARRAY_ELEMENT:
588 case ZEND_INIT_ARRAY:
589 if (Z_TYPE_P(val) == IS_STRING) {
590 zend_ulong index;
591 if (ZEND_HANDLE_NUMERIC(Z_STR_P(val), index)) {
592 zval_ptr_dtor_nogc(val);
593 ZVAL_LONG(val, index);
594 }
595 }
596 opline->op2.constant = zend_optimizer_add_literal(op_array, val);
597 break;
598 case ZEND_ROPE_INIT:
599 case ZEND_ROPE_ADD:
600 case ZEND_ROPE_END:
601 case ZEND_CONCAT:
602 case ZEND_FAST_CONCAT:
603 TO_STRING_NOWARN(val);
604 if (opline->opcode == ZEND_CONCAT && opline->op1_type == IS_CONST) {
605 opline->opcode = ZEND_FAST_CONCAT;
606 }
607 ZEND_FALLTHROUGH;
608 default:
609 opline->op2.constant = zend_optimizer_add_literal(op_array, val);
610 break;
611 }
612
613 opline->op2_type = IS_CONST;
614 if (Z_TYPE(ZEND_OP2_LITERAL(opline)) == IS_STRING) {
615 zend_string_hash_val(Z_STR(ZEND_OP2_LITERAL(opline)));
616 }
617 return 1;
618 }
619
zend_optimizer_replace_by_const(zend_op_array * op_array,zend_op * opline,zend_uchar type,uint32_t var,zval * val)620 bool zend_optimizer_replace_by_const(zend_op_array *op_array,
621 zend_op *opline,
622 zend_uchar type,
623 uint32_t var,
624 zval *val)
625 {
626 zend_op *end = op_array->opcodes + op_array->last;
627
628 while (opline < end) {
629 if (opline->op1_type == type &&
630 opline->op1.var == var) {
631 switch (opline->opcode) {
632 /* In most cases IS_TMP_VAR operand may be used only once.
633 * The operands are usually destroyed by the opcode handler.
634 * However, there are some exception which keep the operand alive. In that case
635 * we want to try to replace all uses of the temporary.
636 */
637 case ZEND_FETCH_LIST_R:
638 case ZEND_CASE:
639 case ZEND_CASE_STRICT:
640 case ZEND_SWITCH_LONG:
641 case ZEND_SWITCH_STRING:
642 case ZEND_MATCH:
643 case ZEND_JMP_NULL: {
644 zend_op *end = op_array->opcodes + op_array->last;
645 while (opline < end) {
646 if (opline->op1_type == type && opline->op1.var == var) {
647 /* If this opcode doesn't keep the operand alive, we're done. Check
648 * this early, because op replacement may modify the opline. */
649 bool is_last = opline->opcode != ZEND_FETCH_LIST_R
650 && opline->opcode != ZEND_CASE
651 && opline->opcode != ZEND_CASE_STRICT
652 && opline->opcode != ZEND_SWITCH_LONG
653 && opline->opcode != ZEND_SWITCH_STRING
654 && opline->opcode != ZEND_MATCH
655 && opline->opcode != ZEND_JMP_NULL
656 && (opline->opcode != ZEND_FREE
657 || opline->extended_value != ZEND_FREE_ON_RETURN);
658
659 Z_TRY_ADDREF_P(val);
660 if (!zend_optimizer_update_op1_const(op_array, opline, val)) {
661 zval_ptr_dtor(val);
662 return 0;
663 }
664 if (is_last) {
665 break;
666 }
667 }
668 opline++;
669 }
670 zval_ptr_dtor_nogc(val);
671 return 1;
672 }
673 case ZEND_VERIFY_RETURN_TYPE: {
674 zend_arg_info *ret_info = op_array->arg_info - 1;
675 if (!ZEND_TYPE_CONTAINS_CODE(ret_info->type, Z_TYPE_P(val))
676 || (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
677 return 0;
678 }
679 MAKE_NOP(opline);
680
681 /* zend_handle_loops_and_finally may inserts other oplines */
682 do {
683 ++opline;
684 } while (opline->opcode != ZEND_RETURN && opline->opcode != ZEND_RETURN_BY_REF);
685 ZEND_ASSERT(opline->op1.var == var);
686
687 break;
688 }
689 default:
690 break;
691 }
692 return zend_optimizer_update_op1_const(op_array, opline, val);
693 }
694
695 if (opline->op2_type == type &&
696 opline->op2.var == var) {
697 return zend_optimizer_update_op2_const(op_array, opline, val);
698 }
699 opline++;
700 }
701
702 return 1;
703 }
704
705 /* Update jump offsets after a jump was migrated to another opline */
zend_optimizer_migrate_jump(zend_op_array * op_array,zend_op * new_opline,zend_op * opline)706 void zend_optimizer_migrate_jump(zend_op_array *op_array, zend_op *new_opline, zend_op *opline) {
707 switch (new_opline->opcode) {
708 case ZEND_JMP:
709 case ZEND_FAST_CALL:
710 ZEND_SET_OP_JMP_ADDR(new_opline, new_opline->op1, ZEND_OP1_JMP_ADDR(opline));
711 break;
712 case ZEND_JMPZ:
713 case ZEND_JMPNZ:
714 case ZEND_JMPZ_EX:
715 case ZEND_JMPNZ_EX:
716 case ZEND_FE_RESET_R:
717 case ZEND_FE_RESET_RW:
718 case ZEND_JMP_SET:
719 case ZEND_COALESCE:
720 case ZEND_ASSERT_CHECK:
721 case ZEND_JMP_NULL:
722 ZEND_SET_OP_JMP_ADDR(new_opline, new_opline->op2, ZEND_OP2_JMP_ADDR(opline));
723 break;
724 case ZEND_FE_FETCH_R:
725 case ZEND_FE_FETCH_RW:
726 new_opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, new_opline, ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value));
727 break;
728 case ZEND_CATCH:
729 if (!(opline->extended_value & ZEND_LAST_CATCH)) {
730 ZEND_SET_OP_JMP_ADDR(new_opline, new_opline->op2, ZEND_OP2_JMP_ADDR(opline));
731 }
732 break;
733 case ZEND_SWITCH_LONG:
734 case ZEND_SWITCH_STRING:
735 case ZEND_MATCH:
736 {
737 HashTable *jumptable = Z_ARRVAL(ZEND_OP2_LITERAL(opline));
738 zval *zv;
739 ZEND_HASH_FOREACH_VAL(jumptable, zv) {
740 Z_LVAL_P(zv) = ZEND_OPLINE_NUM_TO_OFFSET(op_array, new_opline, ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, Z_LVAL_P(zv)));
741 } ZEND_HASH_FOREACH_END();
742 new_opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, new_opline, ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value));
743 break;
744 }
745 }
746 }
747
748 /* Shift jump offsets based on shiftlist */
zend_optimizer_shift_jump(zend_op_array * op_array,zend_op * opline,uint32_t * shiftlist)749 void zend_optimizer_shift_jump(zend_op_array *op_array, zend_op *opline, uint32_t *shiftlist) {
750 switch (opline->opcode) {
751 case ZEND_JMP:
752 case ZEND_FAST_CALL:
753 ZEND_SET_OP_JMP_ADDR(opline, opline->op1, ZEND_OP1_JMP_ADDR(opline) - shiftlist[ZEND_OP1_JMP_ADDR(opline) - op_array->opcodes]);
754 break;
755 case ZEND_JMPZ:
756 case ZEND_JMPNZ:
757 case ZEND_JMPZ_EX:
758 case ZEND_JMPNZ_EX:
759 case ZEND_FE_RESET_R:
760 case ZEND_FE_RESET_RW:
761 case ZEND_JMP_SET:
762 case ZEND_COALESCE:
763 case ZEND_ASSERT_CHECK:
764 case ZEND_JMP_NULL:
765 ZEND_SET_OP_JMP_ADDR(opline, opline->op2, ZEND_OP2_JMP_ADDR(opline) - shiftlist[ZEND_OP2_JMP_ADDR(opline) - op_array->opcodes]);
766 break;
767 case ZEND_CATCH:
768 if (!(opline->extended_value & ZEND_LAST_CATCH)) {
769 ZEND_SET_OP_JMP_ADDR(opline, opline->op2, ZEND_OP2_JMP_ADDR(opline) - shiftlist[ZEND_OP2_JMP_ADDR(opline) - op_array->opcodes]);
770 }
771 break;
772 case ZEND_FE_FETCH_R:
773 case ZEND_FE_FETCH_RW:
774 opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value) - shiftlist[ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value)]);
775 break;
776 case ZEND_SWITCH_LONG:
777 case ZEND_SWITCH_STRING:
778 case ZEND_MATCH:
779 {
780 HashTable *jumptable = Z_ARRVAL(ZEND_OP2_LITERAL(opline));
781 zval *zv;
782 ZEND_HASH_FOREACH_VAL(jumptable, zv) {
783 Z_LVAL_P(zv) = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, Z_LVAL_P(zv)) - shiftlist[ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, Z_LVAL_P(zv))]);
784 } ZEND_HASH_FOREACH_END();
785 opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value) - shiftlist[ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value)]);
786 break;
787 }
788 }
789 }
790
zend_optimizer_get_class_entry(const zend_script * script,const zend_op_array * op_array,zend_string * lcname)791 zend_class_entry *zend_optimizer_get_class_entry(
792 const zend_script *script, const zend_op_array *op_array, zend_string *lcname) {
793 zend_class_entry *ce = script ? zend_hash_find_ptr(&script->class_table, lcname) : NULL;
794 if (ce) {
795 return ce;
796 }
797
798 ce = zend_hash_find_ptr(CG(class_table), lcname);
799 if (ce
800 && (ce->type == ZEND_INTERNAL_CLASS
801 || (op_array && ce->info.user.filename == op_array->filename))) {
802 return ce;
803 }
804
805 if (op_array && op_array->scope && zend_string_equals_ci(op_array->scope->name, lcname)) {
806 return op_array->scope;
807 }
808
809 return NULL;
810 }
811
zend_optimizer_get_class_entry_from_op1(const zend_script * script,const zend_op_array * op_array,const zend_op * opline)812 zend_class_entry *zend_optimizer_get_class_entry_from_op1(
813 const zend_script *script, const zend_op_array *op_array, const zend_op *opline) {
814 if (opline->op1_type == IS_CONST) {
815 zval *op1 = CRT_CONSTANT(opline->op1);
816 if (Z_TYPE_P(op1) == IS_STRING) {
817 return zend_optimizer_get_class_entry(script, op_array, Z_STR_P(op1 + 1));
818 }
819 } else if (opline->op1_type == IS_UNUSED && op_array->scope
820 && !(op_array->scope->ce_flags & ZEND_ACC_TRAIT)
821 && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF) {
822 return op_array->scope;
823 }
824 return NULL;
825 }
826
zend_optimizer_get_called_func(zend_script * script,zend_op_array * op_array,zend_op * opline,bool * is_prototype)827 zend_function *zend_optimizer_get_called_func(
828 zend_script *script, zend_op_array *op_array, zend_op *opline, bool *is_prototype)
829 {
830 *is_prototype = 0;
831 switch (opline->opcode) {
832 case ZEND_INIT_FCALL:
833 {
834 zend_string *function_name = Z_STR_P(CRT_CONSTANT(opline->op2));
835 zend_function *func;
836 if (script && (func = zend_hash_find_ptr(&script->function_table, function_name)) != NULL) {
837 return func;
838 } else if ((func = zend_hash_find_ptr(EG(function_table), function_name)) != NULL) {
839 if (func->type == ZEND_INTERNAL_FUNCTION) {
840 return func;
841 } else if (func->type == ZEND_USER_FUNCTION &&
842 func->op_array.filename &&
843 func->op_array.filename == op_array->filename) {
844 return func;
845 }
846 }
847 break;
848 }
849 case ZEND_INIT_FCALL_BY_NAME:
850 case ZEND_INIT_NS_FCALL_BY_NAME:
851 if (opline->op2_type == IS_CONST && Z_TYPE_P(CRT_CONSTANT(opline->op2)) == IS_STRING) {
852 zval *function_name = CRT_CONSTANT(opline->op2) + 1;
853 zend_function *func;
854 if (script && (func = zend_hash_find_ptr(&script->function_table, Z_STR_P(function_name)))) {
855 return func;
856 } else if ((func = zend_hash_find_ptr(EG(function_table), Z_STR_P(function_name))) != NULL) {
857 if (func->type == ZEND_INTERNAL_FUNCTION) {
858 return func;
859 } else if (func->type == ZEND_USER_FUNCTION &&
860 func->op_array.filename &&
861 func->op_array.filename == op_array->filename) {
862 return func;
863 }
864 }
865 }
866 break;
867 case ZEND_INIT_STATIC_METHOD_CALL:
868 if (opline->op2_type == IS_CONST && Z_TYPE_P(CRT_CONSTANT(opline->op2)) == IS_STRING) {
869 zend_class_entry *ce = zend_optimizer_get_class_entry_from_op1(
870 script, op_array, opline);
871 if (ce) {
872 zend_string *func_name = Z_STR_P(CRT_CONSTANT(opline->op2) + 1);
873 zend_function *fbc = zend_hash_find_ptr(&ce->function_table, func_name);
874 if (fbc) {
875 bool is_public = (fbc->common.fn_flags & ZEND_ACC_PUBLIC) != 0;
876 bool same_scope = fbc->common.scope == op_array->scope;
877 if (is_public || same_scope) {
878 return fbc;
879 }
880 }
881 }
882 }
883 break;
884 case ZEND_INIT_METHOD_CALL:
885 if (opline->op1_type == IS_UNUSED
886 && opline->op2_type == IS_CONST && Z_TYPE_P(CRT_CONSTANT(opline->op2)) == IS_STRING
887 && op_array->scope
888 && !(op_array->fn_flags & ZEND_ACC_TRAIT_CLONE)
889 && !(op_array->scope->ce_flags & ZEND_ACC_TRAIT)) {
890 zend_string *method_name = Z_STR_P(CRT_CONSTANT(opline->op2) + 1);
891 zend_function *fbc = zend_hash_find_ptr(
892 &op_array->scope->function_table, method_name);
893 if (fbc) {
894 bool is_private = (fbc->common.fn_flags & ZEND_ACC_PRIVATE) != 0;
895 bool is_final = (fbc->common.fn_flags & ZEND_ACC_FINAL) != 0;
896 bool same_scope = fbc->common.scope == op_array->scope;
897 if (is_private) {
898 /* Only use private method if in the same scope. We can't even use it
899 * as a prototype, as it may be overridden with changed signature. */
900 return same_scope ? fbc : NULL;
901 }
902 /* If the method is non-final, it may be overridden,
903 * but only with a compatible method signature. */
904 *is_prototype = !is_final;
905 return fbc;
906 }
907 }
908 break;
909 case ZEND_NEW:
910 {
911 zend_class_entry *ce = zend_optimizer_get_class_entry_from_op1(
912 script, op_array, opline);
913 if (ce && ce->type == ZEND_USER_CLASS) {
914 return ce->constructor;
915 }
916 break;
917 }
918 }
919 return NULL;
920 }
921
zend_optimizer_classify_function(zend_string * name,uint32_t num_args)922 uint32_t zend_optimizer_classify_function(zend_string *name, uint32_t num_args) {
923 if (zend_string_equals_literal(name, "extract")) {
924 return ZEND_FUNC_INDIRECT_VAR_ACCESS;
925 } else if (zend_string_equals_literal(name, "compact")) {
926 return ZEND_FUNC_INDIRECT_VAR_ACCESS;
927 } else if (zend_string_equals_literal(name, "get_defined_vars")) {
928 return ZEND_FUNC_INDIRECT_VAR_ACCESS;
929 } else if (zend_string_equals_literal(name, "db2_execute")) {
930 return ZEND_FUNC_INDIRECT_VAR_ACCESS;
931 } else if (zend_string_equals_literal(name, "func_num_args")) {
932 return ZEND_FUNC_VARARG;
933 } else if (zend_string_equals_literal(name, "func_get_arg")) {
934 return ZEND_FUNC_VARARG;
935 } else if (zend_string_equals_literal(name, "func_get_args")) {
936 return ZEND_FUNC_VARARG;
937 } else {
938 return 0;
939 }
940 }
941
zend_optimizer_get_loop_var_def(const zend_op_array * op_array,zend_op * free_opline)942 zend_op *zend_optimizer_get_loop_var_def(const zend_op_array *op_array, zend_op *free_opline) {
943 uint32_t var = free_opline->op1.var;
944 ZEND_ASSERT(zend_optimizer_is_loop_var_free(free_opline));
945
946 while (--free_opline >= op_array->opcodes) {
947 if ((free_opline->result_type & (IS_TMP_VAR|IS_VAR)) && free_opline->result.var == var) {
948 return free_opline;
949 }
950 }
951 return NULL;
952 }
953
zend_optimize(zend_op_array * op_array,zend_optimizer_ctx * ctx)954 static void zend_optimize(zend_op_array *op_array,
955 zend_optimizer_ctx *ctx)
956 {
957 if (op_array->type == ZEND_EVAL_CODE) {
958 return;
959 }
960
961 if (ctx->debug_level & ZEND_DUMP_BEFORE_OPTIMIZER) {
962 zend_dump_op_array(op_array, ZEND_DUMP_LIVE_RANGES, "before optimizer", NULL);
963 }
964
965 /* pass 1 (Simple local optimizations)
966 * - persistent constant substitution (true, false, null, etc)
967 * - constant casting (ADD expects numbers, CONCAT strings, etc)
968 * - constant expression evaluation
969 * - optimize constant conditional JMPs
970 * - pre-evaluate constant function calls
971 * - eliminate FETCH $GLOBALS followed by FETCH_DIM/UNSET_DIM/ISSET_ISEMPTY_DIM
972 */
973 if (ZEND_OPTIMIZER_PASS_1 & ctx->optimization_level) {
974 zend_optimizer_pass1(op_array, ctx);
975 if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_1) {
976 zend_dump_op_array(op_array, 0, "after pass 1", NULL);
977 }
978 }
979
980 /* pass 3: (Jump optimization)
981 * - optimize series of JMPs
982 */
983 if (ZEND_OPTIMIZER_PASS_3 & ctx->optimization_level) {
984 zend_optimizer_pass3(op_array, ctx);
985 if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_3) {
986 zend_dump_op_array(op_array, 0, "after pass 3", NULL);
987 }
988 }
989
990 /* pass 4:
991 * - INIT_FCALL_BY_NAME -> DO_FCALL
992 */
993 if (ZEND_OPTIMIZER_PASS_4 & ctx->optimization_level) {
994 zend_optimize_func_calls(op_array, ctx);
995 if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_4) {
996 zend_dump_op_array(op_array, 0, "after pass 4", NULL);
997 }
998 }
999
1000 /* pass 5:
1001 * - CFG optimization
1002 */
1003 if (ZEND_OPTIMIZER_PASS_5 & ctx->optimization_level) {
1004 zend_optimize_cfg(op_array, ctx);
1005 if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_5) {
1006 zend_dump_op_array(op_array, 0, "after pass 5", NULL);
1007 }
1008 }
1009
1010 /* pass 6:
1011 * - DFA optimization
1012 */
1013 if ((ZEND_OPTIMIZER_PASS_6 & ctx->optimization_level) &&
1014 !(ZEND_OPTIMIZER_PASS_7 & ctx->optimization_level)) {
1015 zend_optimize_dfa(op_array, ctx);
1016 if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_6) {
1017 zend_dump_op_array(op_array, 0, "after pass 6", NULL);
1018 }
1019 }
1020
1021 /* pass 9:
1022 * - Optimize temp variables usage
1023 */
1024 if ((ZEND_OPTIMIZER_PASS_9 & ctx->optimization_level) &&
1025 !(ZEND_OPTIMIZER_PASS_7 & ctx->optimization_level)) {
1026 zend_optimize_temporary_variables(op_array, ctx);
1027 if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_9) {
1028 zend_dump_op_array(op_array, 0, "after pass 9", NULL);
1029 }
1030 }
1031
1032 /* pass 10:
1033 * - remove NOPs
1034 */
1035 if (((ZEND_OPTIMIZER_PASS_10|ZEND_OPTIMIZER_PASS_5) & ctx->optimization_level) == ZEND_OPTIMIZER_PASS_10) {
1036 zend_optimizer_nop_removal(op_array, ctx);
1037 if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_10) {
1038 zend_dump_op_array(op_array, 0, "after pass 10", NULL);
1039 }
1040 }
1041
1042 /* pass 11:
1043 * - Compact literals table
1044 */
1045 if ((ZEND_OPTIMIZER_PASS_11 & ctx->optimization_level) &&
1046 (!(ZEND_OPTIMIZER_PASS_6 & ctx->optimization_level) ||
1047 !(ZEND_OPTIMIZER_PASS_7 & ctx->optimization_level))) {
1048 zend_optimizer_compact_literals(op_array, ctx);
1049 if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_11) {
1050 zend_dump_op_array(op_array, 0, "after pass 11", NULL);
1051 }
1052 }
1053
1054 if ((ZEND_OPTIMIZER_PASS_13 & ctx->optimization_level) &&
1055 (!(ZEND_OPTIMIZER_PASS_6 & ctx->optimization_level) ||
1056 !(ZEND_OPTIMIZER_PASS_7 & ctx->optimization_level))) {
1057 zend_optimizer_compact_vars(op_array);
1058 if (ctx->debug_level & ZEND_DUMP_AFTER_PASS_13) {
1059 zend_dump_op_array(op_array, 0, "after pass 13", NULL);
1060 }
1061 }
1062
1063 if (ZEND_OPTIMIZER_PASS_7 & ctx->optimization_level) {
1064 return;
1065 }
1066
1067 if (ctx->debug_level & ZEND_DUMP_AFTER_OPTIMIZER) {
1068 zend_dump_op_array(op_array, 0, "after optimizer", NULL);
1069 }
1070 }
1071
zend_revert_pass_two(zend_op_array * op_array)1072 static void zend_revert_pass_two(zend_op_array *op_array)
1073 {
1074 zend_op *opline, *end;
1075
1076 ZEND_ASSERT((op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) != 0);
1077
1078 opline = op_array->opcodes;
1079 end = opline + op_array->last;
1080 while (opline < end) {
1081 if (opline->op1_type == IS_CONST) {
1082 ZEND_PASS_TWO_UNDO_CONSTANT(op_array, opline, opline->op1);
1083 }
1084 if (opline->op2_type == IS_CONST) {
1085 ZEND_PASS_TWO_UNDO_CONSTANT(op_array, opline, opline->op2);
1086 }
1087 /* reset smart branch flags IS_SMART_BRANCH_JMP[N]Z */
1088 opline->result_type &= (IS_TMP_VAR|IS_VAR|IS_CV|IS_CONST);
1089 opline++;
1090 }
1091 #if !ZEND_USE_ABS_CONST_ADDR
1092 if (op_array->literals) {
1093 zval *literals = emalloc(sizeof(zval) * op_array->last_literal);
1094 memcpy(literals, op_array->literals, sizeof(zval) * op_array->last_literal);
1095 op_array->literals = literals;
1096 }
1097 #endif
1098
1099 op_array->fn_flags &= ~ZEND_ACC_DONE_PASS_TWO;
1100 }
1101
zend_redo_pass_two(zend_op_array * op_array)1102 static void zend_redo_pass_two(zend_op_array *op_array)
1103 {
1104 zend_op *opline, *end;
1105 #if ZEND_USE_ABS_JMP_ADDR && !ZEND_USE_ABS_CONST_ADDR
1106 zend_op *old_opcodes = op_array->opcodes;
1107 #endif
1108
1109 ZEND_ASSERT((op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) == 0);
1110
1111 #if !ZEND_USE_ABS_CONST_ADDR
1112 if (op_array->last_literal) {
1113 op_array->opcodes = (zend_op *) erealloc(op_array->opcodes,
1114 ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16) +
1115 sizeof(zval) * op_array->last_literal);
1116 memcpy(((char*)op_array->opcodes) + ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16),
1117 op_array->literals, sizeof(zval) * op_array->last_literal);
1118 efree(op_array->literals);
1119 op_array->literals = (zval*)(((char*)op_array->opcodes) + ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16));
1120 } else {
1121 if (op_array->literals) {
1122 efree(op_array->literals);
1123 }
1124 op_array->literals = NULL;
1125 }
1126 #endif
1127
1128 opline = op_array->opcodes;
1129 end = opline + op_array->last;
1130 while (opline < end) {
1131 if (opline->op1_type == IS_CONST) {
1132 ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, opline->op1);
1133 }
1134 if (opline->op2_type == IS_CONST) {
1135 ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, opline->op2);
1136 }
1137 /* fix jumps to point to new array */
1138 switch (opline->opcode) {
1139 #if ZEND_USE_ABS_JMP_ADDR && !ZEND_USE_ABS_CONST_ADDR
1140 case ZEND_JMP:
1141 case ZEND_FAST_CALL:
1142 opline->op1.jmp_addr = &op_array->opcodes[opline->op1.jmp_addr - old_opcodes];
1143 break;
1144 case ZEND_JMPZ:
1145 case ZEND_JMPNZ:
1146 case ZEND_JMPZ_EX:
1147 case ZEND_JMPNZ_EX:
1148 case ZEND_JMP_SET:
1149 case ZEND_COALESCE:
1150 case ZEND_FE_RESET_R:
1151 case ZEND_FE_RESET_RW:
1152 case ZEND_ASSERT_CHECK:
1153 case ZEND_JMP_NULL:
1154 opline->op2.jmp_addr = &op_array->opcodes[opline->op2.jmp_addr - old_opcodes];
1155 break;
1156 case ZEND_CATCH:
1157 if (!(opline->extended_value & ZEND_LAST_CATCH)) {
1158 opline->op2.jmp_addr = &op_array->opcodes[opline->op2.jmp_addr - old_opcodes];
1159 }
1160 break;
1161 case ZEND_FE_FETCH_R:
1162 case ZEND_FE_FETCH_RW:
1163 case ZEND_SWITCH_LONG:
1164 case ZEND_SWITCH_STRING:
1165 case ZEND_MATCH:
1166 /* relative extended_value don't have to be changed */
1167 break;
1168 #endif
1169 case ZEND_IS_IDENTICAL:
1170 case ZEND_IS_NOT_IDENTICAL:
1171 case ZEND_IS_EQUAL:
1172 case ZEND_IS_NOT_EQUAL:
1173 case ZEND_IS_SMALLER:
1174 case ZEND_IS_SMALLER_OR_EQUAL:
1175 case ZEND_CASE:
1176 case ZEND_CASE_STRICT:
1177 case ZEND_ISSET_ISEMPTY_CV:
1178 case ZEND_ISSET_ISEMPTY_VAR:
1179 case ZEND_ISSET_ISEMPTY_DIM_OBJ:
1180 case ZEND_ISSET_ISEMPTY_PROP_OBJ:
1181 case ZEND_ISSET_ISEMPTY_STATIC_PROP:
1182 case ZEND_INSTANCEOF:
1183 case ZEND_TYPE_CHECK:
1184 case ZEND_DEFINED:
1185 case ZEND_IN_ARRAY:
1186 case ZEND_ARRAY_KEY_EXISTS:
1187 if (opline->result_type & IS_TMP_VAR) {
1188 /* reinitialize result_type of smart branch instructions */
1189 if (opline + 1 < end) {
1190 if ((opline+1)->opcode == ZEND_JMPZ
1191 && (opline+1)->op1_type == IS_TMP_VAR
1192 && (opline+1)->op1.var == opline->result.var) {
1193 opline->result_type = IS_SMART_BRANCH_JMPZ | IS_TMP_VAR;
1194 } else if ((opline+1)->opcode == ZEND_JMPNZ
1195 && (opline+1)->op1_type == IS_TMP_VAR
1196 && (opline+1)->op1.var == opline->result.var) {
1197 opline->result_type = IS_SMART_BRANCH_JMPNZ | IS_TMP_VAR;
1198 }
1199 }
1200 }
1201 break;
1202 }
1203 ZEND_VM_SET_OPCODE_HANDLER(opline);
1204 opline++;
1205 }
1206
1207 op_array->fn_flags |= ZEND_ACC_DONE_PASS_TWO;
1208 }
1209
zend_redo_pass_two_ex(zend_op_array * op_array,zend_ssa * ssa)1210 static void zend_redo_pass_two_ex(zend_op_array *op_array, zend_ssa *ssa)
1211 {
1212 zend_op *opline, *end;
1213 #if ZEND_USE_ABS_JMP_ADDR && !ZEND_USE_ABS_CONST_ADDR
1214 zend_op *old_opcodes = op_array->opcodes;
1215 #endif
1216
1217 ZEND_ASSERT((op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) == 0);
1218
1219 #if !ZEND_USE_ABS_CONST_ADDR
1220 if (op_array->last_literal) {
1221 op_array->opcodes = (zend_op *) erealloc(op_array->opcodes,
1222 ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16) +
1223 sizeof(zval) * op_array->last_literal);
1224 memcpy(((char*)op_array->opcodes) + ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16),
1225 op_array->literals, sizeof(zval) * op_array->last_literal);
1226 efree(op_array->literals);
1227 op_array->literals = (zval*)(((char*)op_array->opcodes) + ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16));
1228 } else {
1229 if (op_array->literals) {
1230 efree(op_array->literals);
1231 }
1232 op_array->literals = NULL;
1233 }
1234 #endif
1235
1236 opline = op_array->opcodes;
1237 end = opline + op_array->last;
1238 while (opline < end) {
1239 zend_ssa_op *ssa_op = &ssa->ops[opline - op_array->opcodes];
1240 uint32_t op1_info = opline->op1_type == IS_UNUSED ? 0 : (OP1_INFO() & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_KEY_ANY));
1241 uint32_t op2_info = opline->op1_type == IS_UNUSED ? 0 : (OP2_INFO() & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_KEY_ANY));
1242 uint32_t res_info =
1243 (opline->opcode == ZEND_PRE_INC ||
1244 opline->opcode == ZEND_PRE_DEC ||
1245 opline->opcode == ZEND_POST_INC ||
1246 opline->opcode == ZEND_POST_DEC) ?
1247 ((ssa->ops[opline - op_array->opcodes].op1_def >= 0) ? (OP1_DEF_INFO() & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_KEY_ANY)) : MAY_BE_ANY) :
1248 (opline->result_type == IS_UNUSED ? 0 : (RES_INFO() & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_KEY_ANY)));
1249
1250 if (opline->op1_type == IS_CONST) {
1251 ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, opline->op1);
1252 }
1253 if (opline->op2_type == IS_CONST) {
1254 ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, opline->op2);
1255 }
1256
1257 /* fix jumps to point to new array */
1258 switch (opline->opcode) {
1259 #if ZEND_USE_ABS_JMP_ADDR && !ZEND_USE_ABS_CONST_ADDR
1260 case ZEND_JMP:
1261 case ZEND_FAST_CALL:
1262 opline->op1.jmp_addr = &op_array->opcodes[opline->op1.jmp_addr - old_opcodes];
1263 break;
1264 case ZEND_JMPZ:
1265 case ZEND_JMPNZ:
1266 case ZEND_JMPZ_EX:
1267 case ZEND_JMPNZ_EX:
1268 case ZEND_JMP_SET:
1269 case ZEND_COALESCE:
1270 case ZEND_FE_RESET_R:
1271 case ZEND_FE_RESET_RW:
1272 case ZEND_ASSERT_CHECK:
1273 case ZEND_JMP_NULL:
1274 opline->op2.jmp_addr = &op_array->opcodes[opline->op2.jmp_addr - old_opcodes];
1275 break;
1276 case ZEND_CATCH:
1277 if (!(opline->extended_value & ZEND_LAST_CATCH)) {
1278 opline->op2.jmp_addr = &op_array->opcodes[opline->op2.jmp_addr - old_opcodes];
1279 }
1280 break;
1281 case ZEND_FE_FETCH_R:
1282 case ZEND_FE_FETCH_RW:
1283 case ZEND_SWITCH_LONG:
1284 case ZEND_SWITCH_STRING:
1285 case ZEND_MATCH:
1286 /* relative extended_value don't have to be changed */
1287 break;
1288 #endif
1289 case ZEND_IS_IDENTICAL:
1290 case ZEND_IS_NOT_IDENTICAL:
1291 case ZEND_IS_EQUAL:
1292 case ZEND_IS_NOT_EQUAL:
1293 case ZEND_IS_SMALLER:
1294 case ZEND_IS_SMALLER_OR_EQUAL:
1295 case ZEND_CASE:
1296 case ZEND_CASE_STRICT:
1297 case ZEND_ISSET_ISEMPTY_CV:
1298 case ZEND_ISSET_ISEMPTY_VAR:
1299 case ZEND_ISSET_ISEMPTY_DIM_OBJ:
1300 case ZEND_ISSET_ISEMPTY_PROP_OBJ:
1301 case ZEND_ISSET_ISEMPTY_STATIC_PROP:
1302 case ZEND_INSTANCEOF:
1303 case ZEND_TYPE_CHECK:
1304 case ZEND_DEFINED:
1305 case ZEND_IN_ARRAY:
1306 case ZEND_ARRAY_KEY_EXISTS:
1307 if (opline->result_type & IS_TMP_VAR) {
1308 /* reinitialize result_type of smart branch instructions */
1309 if (opline + 1 < end) {
1310 if ((opline+1)->opcode == ZEND_JMPZ
1311 && (opline+1)->op1_type == IS_TMP_VAR
1312 && (opline+1)->op1.var == opline->result.var) {
1313 opline->result_type = IS_SMART_BRANCH_JMPZ | IS_TMP_VAR;
1314 } else if ((opline+1)->opcode == ZEND_JMPNZ
1315 && (opline+1)->op1_type == IS_TMP_VAR
1316 && (opline+1)->op1.var == opline->result.var) {
1317 opline->result_type = IS_SMART_BRANCH_JMPNZ | IS_TMP_VAR;
1318 }
1319 }
1320 }
1321 break;
1322 }
1323 zend_vm_set_opcode_handler_ex(opline, op1_info, op2_info, res_info);
1324 opline++;
1325 }
1326
1327 op_array->fn_flags |= ZEND_ACC_DONE_PASS_TWO;
1328 }
1329
zend_optimize_op_array(zend_op_array * op_array,zend_optimizer_ctx * ctx)1330 static void zend_optimize_op_array(zend_op_array *op_array,
1331 zend_optimizer_ctx *ctx)
1332 {
1333 /* Revert pass_two() */
1334 zend_revert_pass_two(op_array);
1335
1336 /* Do actual optimizations */
1337 zend_optimize(op_array, ctx);
1338
1339 /* Redo pass_two() */
1340 zend_redo_pass_two(op_array);
1341
1342 if (op_array->live_range) {
1343 zend_recalc_live_ranges(op_array, NULL);
1344 }
1345 }
1346
zend_adjust_fcall_stack_size(zend_op_array * op_array,zend_optimizer_ctx * ctx)1347 static void zend_adjust_fcall_stack_size(zend_op_array *op_array, zend_optimizer_ctx *ctx)
1348 {
1349 zend_function *func;
1350 zend_op *opline, *end;
1351
1352 opline = op_array->opcodes;
1353 end = opline + op_array->last;
1354 while (opline < end) {
1355 if (opline->opcode == ZEND_INIT_FCALL) {
1356 func = zend_hash_find_ptr(
1357 &ctx->script->function_table,
1358 Z_STR_P(RT_CONSTANT(opline, opline->op2)));
1359 if (func) {
1360 opline->op1.num = zend_vm_calc_used_stack(opline->extended_value, func);
1361 }
1362 }
1363 opline++;
1364 }
1365 }
1366
zend_adjust_fcall_stack_size_graph(zend_op_array * op_array)1367 static void zend_adjust_fcall_stack_size_graph(zend_op_array *op_array)
1368 {
1369 zend_func_info *func_info = ZEND_FUNC_INFO(op_array);
1370
1371 if (func_info) {
1372 zend_call_info *call_info =func_info->callee_info;
1373
1374 while (call_info) {
1375 zend_op *opline = call_info->caller_init_opline;
1376
1377 if (opline && call_info->callee_func && opline->opcode == ZEND_INIT_FCALL) {
1378 ZEND_ASSERT(!call_info->is_prototype);
1379 opline->op1.num = zend_vm_calc_used_stack(opline->extended_value, call_info->callee_func);
1380 }
1381 call_info = call_info->next_callee;
1382 }
1383 }
1384 }
1385
needs_live_range(zend_op_array * op_array,zend_op * def_opline)1386 static bool needs_live_range(zend_op_array *op_array, zend_op *def_opline) {
1387 zend_func_info *func_info = ZEND_FUNC_INFO(op_array);
1388 zend_ssa_op *ssa_op = &func_info->ssa.ops[def_opline - op_array->opcodes];
1389 int ssa_var = ssa_op->result_def;
1390 if (ssa_var < 0) {
1391 /* Be conservative. */
1392 return 1;
1393 }
1394
1395 /* If the variable is used by a PHI, this may be the assignment of the final branch of a
1396 * ternary/etc structure. While this is where the live range starts, the value from the other
1397 * branch may also be used. As such, use the type of the PHI node for the following check. */
1398 if (func_info->ssa.vars[ssa_var].phi_use_chain) {
1399 ssa_var = func_info->ssa.vars[ssa_var].phi_use_chain->ssa_var;
1400 }
1401
1402 uint32_t type = func_info->ssa.var_info[ssa_var].type;
1403 return (type & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) != 0;
1404 }
1405
zend_foreach_op_array_helper(zend_op_array * op_array,zend_op_array_func_t func,void * context)1406 static void zend_foreach_op_array_helper(
1407 zend_op_array *op_array, zend_op_array_func_t func, void *context) {
1408 func(op_array, context);
1409 for (uint32_t i = 0; i < op_array->num_dynamic_func_defs; i++) {
1410 zend_foreach_op_array_helper(op_array->dynamic_func_defs[i], func, context);
1411 }
1412 }
1413
zend_foreach_op_array(zend_script * script,zend_op_array_func_t func,void * context)1414 void zend_foreach_op_array(zend_script *script, zend_op_array_func_t func, void *context)
1415 {
1416 zend_class_entry *ce;
1417 zend_string *key;
1418 zend_op_array *op_array;
1419
1420 zend_foreach_op_array_helper(&script->main_op_array, func, context);
1421
1422 ZEND_HASH_MAP_FOREACH_PTR(&script->function_table, op_array) {
1423 zend_foreach_op_array_helper(op_array, func, context);
1424 } ZEND_HASH_FOREACH_END();
1425
1426 ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&script->class_table, key, ce) {
1427 if (ce->refcount > 1 && !zend_string_equals_ci(key, ce->name)) {
1428 continue;
1429 }
1430 ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, op_array) {
1431 if (op_array->scope == ce
1432 && op_array->type == ZEND_USER_FUNCTION
1433 && !(op_array->fn_flags & ZEND_ACC_ABSTRACT)
1434 && !(op_array->fn_flags & ZEND_ACC_TRAIT_CLONE)) {
1435 zend_foreach_op_array_helper(op_array, func, context);
1436 }
1437 } ZEND_HASH_FOREACH_END();
1438 } ZEND_HASH_FOREACH_END();
1439 }
1440
step_optimize_op_array(zend_op_array * op_array,void * context)1441 static void step_optimize_op_array(zend_op_array *op_array, void *context) {
1442 zend_optimize_op_array(op_array, (zend_optimizer_ctx *) context);
1443 }
1444
step_adjust_fcall_stack_size(zend_op_array * op_array,void * context)1445 static void step_adjust_fcall_stack_size(zend_op_array *op_array, void *context) {
1446 zend_adjust_fcall_stack_size(op_array, (zend_optimizer_ctx *) context);
1447 }
1448
step_dump_after_optimizer(zend_op_array * op_array,void * context)1449 static void step_dump_after_optimizer(zend_op_array *op_array, void *context) {
1450 zend_dump_op_array(op_array, ZEND_DUMP_LIVE_RANGES, "after optimizer", NULL);
1451 }
1452
zend_optimizer_call_registered_passes(zend_script * script,void * ctx)1453 static void zend_optimizer_call_registered_passes(zend_script *script, void *ctx) {
1454 for (int i = 0; i < zend_optimizer_registered_passes.last; i++) {
1455 if (!zend_optimizer_registered_passes.pass[i]) {
1456 continue;
1457 }
1458
1459 zend_optimizer_registered_passes.pass[i](script, ctx);
1460 }
1461 }
1462
zend_optimize_script(zend_script * script,zend_long optimization_level,zend_long debug_level)1463 ZEND_API void zend_optimize_script(zend_script *script, zend_long optimization_level, zend_long debug_level)
1464 {
1465 zend_class_entry *ce;
1466 zend_string *key;
1467 zend_op_array *op_array;
1468 zend_string *name;
1469 zend_optimizer_ctx ctx;
1470
1471 ctx.arena = zend_arena_create(64 * 1024);
1472 ctx.script = script;
1473 ctx.constants = NULL;
1474 ctx.optimization_level = optimization_level;
1475 ctx.debug_level = debug_level;
1476
1477 if ((ZEND_OPTIMIZER_PASS_6 & optimization_level) &&
1478 (ZEND_OPTIMIZER_PASS_7 & optimization_level)) {
1479 /* Optimize using call-graph */
1480 zend_call_graph call_graph;
1481 zend_build_call_graph(&ctx.arena, script, &call_graph);
1482
1483 int i;
1484 zend_func_info *func_info;
1485
1486 for (i = 0; i < call_graph.op_arrays_count; i++) {
1487 zend_revert_pass_two(call_graph.op_arrays[i]);
1488 zend_optimize(call_graph.op_arrays[i], &ctx);
1489 }
1490
1491 zend_analyze_call_graph(&ctx.arena, script, &call_graph);
1492
1493 for (i = 0; i < call_graph.op_arrays_count; i++) {
1494 func_info = ZEND_FUNC_INFO(call_graph.op_arrays[i]);
1495 if (func_info) {
1496 func_info->call_map = zend_build_call_map(&ctx.arena, func_info, call_graph.op_arrays[i]);
1497 if (call_graph.op_arrays[i]->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
1498 zend_init_func_return_info(call_graph.op_arrays[i], script, &func_info->return_info);
1499 }
1500 }
1501 }
1502
1503 for (i = 0; i < call_graph.op_arrays_count; i++) {
1504 func_info = ZEND_FUNC_INFO(call_graph.op_arrays[i]);
1505 if (func_info) {
1506 if (zend_dfa_analyze_op_array(call_graph.op_arrays[i], &ctx, &func_info->ssa) == SUCCESS) {
1507 func_info->flags = func_info->ssa.cfg.flags;
1508 } else {
1509 ZEND_SET_FUNC_INFO(call_graph.op_arrays[i], NULL);
1510 }
1511 }
1512 }
1513
1514 //TODO: perform inner-script inference???
1515 for (i = 0; i < call_graph.op_arrays_count; i++) {
1516 func_info = ZEND_FUNC_INFO(call_graph.op_arrays[i]);
1517 if (func_info) {
1518 zend_dfa_optimize_op_array(call_graph.op_arrays[i], &ctx, &func_info->ssa, func_info->call_map);
1519 }
1520 }
1521
1522 if (debug_level & ZEND_DUMP_AFTER_PASS_7) {
1523 for (i = 0; i < call_graph.op_arrays_count; i++) {
1524 zend_dump_op_array(call_graph.op_arrays[i], 0, "after pass 7", NULL);
1525 }
1526 }
1527
1528 if (ZEND_OPTIMIZER_PASS_9 & optimization_level) {
1529 for (i = 0; i < call_graph.op_arrays_count; i++) {
1530 zend_optimize_temporary_variables(call_graph.op_arrays[i], &ctx);
1531 if (debug_level & ZEND_DUMP_AFTER_PASS_9) {
1532 zend_dump_op_array(call_graph.op_arrays[i], 0, "after pass 9", NULL);
1533 }
1534 }
1535 }
1536
1537 if (ZEND_OPTIMIZER_PASS_11 & optimization_level) {
1538 for (i = 0; i < call_graph.op_arrays_count; i++) {
1539 zend_optimizer_compact_literals(call_graph.op_arrays[i], &ctx);
1540 if (debug_level & ZEND_DUMP_AFTER_PASS_11) {
1541 zend_dump_op_array(call_graph.op_arrays[i], 0, "after pass 11", NULL);
1542 }
1543 }
1544 }
1545
1546 if (ZEND_OPTIMIZER_PASS_13 & optimization_level) {
1547 for (i = 0; i < call_graph.op_arrays_count; i++) {
1548 zend_optimizer_compact_vars(call_graph.op_arrays[i]);
1549 if (debug_level & ZEND_DUMP_AFTER_PASS_13) {
1550 zend_dump_op_array(call_graph.op_arrays[i], 0, "after pass 13", NULL);
1551 }
1552 }
1553 }
1554
1555 if (ZEND_OPTIMIZER_PASS_12 & optimization_level) {
1556 for (i = 0; i < call_graph.op_arrays_count; i++) {
1557 zend_adjust_fcall_stack_size_graph(call_graph.op_arrays[i]);
1558 }
1559 }
1560
1561 for (i = 0; i < call_graph.op_arrays_count; i++) {
1562 op_array = call_graph.op_arrays[i];
1563 func_info = ZEND_FUNC_INFO(op_array);
1564 if (func_info && func_info->ssa.var_info) {
1565 zend_redo_pass_two_ex(op_array, &func_info->ssa);
1566 if (op_array->live_range) {
1567 zend_recalc_live_ranges(op_array, needs_live_range);
1568 }
1569 } else {
1570 zend_redo_pass_two(op_array);
1571 if (op_array->live_range) {
1572 zend_recalc_live_ranges(op_array, NULL);
1573 }
1574 }
1575 }
1576
1577 for (i = 0; i < call_graph.op_arrays_count; i++) {
1578 ZEND_SET_FUNC_INFO(call_graph.op_arrays[i], NULL);
1579 }
1580 } else {
1581 zend_foreach_op_array(script, step_optimize_op_array, &ctx);
1582
1583 if (ZEND_OPTIMIZER_PASS_12 & optimization_level) {
1584 zend_foreach_op_array(script, step_adjust_fcall_stack_size, &ctx);
1585 }
1586 }
1587
1588 ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&script->class_table, key, ce) {
1589 if (ce->refcount > 1 && !zend_string_equals_ci(key, ce->name)) {
1590 continue;
1591 }
1592 ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&ce->function_table, name, op_array) {
1593 if (op_array->scope != ce && op_array->type == ZEND_USER_FUNCTION) {
1594 zend_op_array *orig_op_array =
1595 zend_hash_find_ptr(&op_array->scope->function_table, name);
1596
1597 ZEND_ASSERT(orig_op_array != NULL);
1598 if (orig_op_array != op_array) {
1599 uint32_t fn_flags = op_array->fn_flags;
1600 zend_function *prototype = op_array->prototype;
1601 HashTable *ht = op_array->static_variables;
1602
1603 *op_array = *orig_op_array;
1604 op_array->fn_flags = fn_flags;
1605 op_array->prototype = prototype;
1606 op_array->static_variables = ht;
1607 }
1608 }
1609 } ZEND_HASH_FOREACH_END();
1610 } ZEND_HASH_FOREACH_END();
1611
1612 zend_optimizer_call_registered_passes(script, &ctx);
1613
1614 if ((debug_level & ZEND_DUMP_AFTER_OPTIMIZER) &&
1615 (ZEND_OPTIMIZER_PASS_7 & optimization_level)) {
1616 zend_foreach_op_array(script, step_dump_after_optimizer, NULL);
1617 }
1618
1619 if (ctx.constants) {
1620 zend_hash_destroy(ctx.constants);
1621 }
1622 zend_arena_destroy(ctx.arena);
1623 }
1624
zend_optimizer_register_pass(zend_optimizer_pass_t pass)1625 ZEND_API int zend_optimizer_register_pass(zend_optimizer_pass_t pass)
1626 {
1627 if (!pass) {
1628 return -1;
1629 }
1630
1631 if (zend_optimizer_registered_passes.last == ZEND_OPTIMIZER_MAX_REGISTERED_PASSES) {
1632 return -1;
1633 }
1634
1635 zend_optimizer_registered_passes.pass[
1636 zend_optimizer_registered_passes.last++] = pass;
1637
1638 return zend_optimizer_registered_passes.last;
1639 }
1640
zend_optimizer_unregister_pass(int idx)1641 ZEND_API void zend_optimizer_unregister_pass(int idx)
1642 {
1643 zend_optimizer_registered_passes.pass[idx-1] = NULL;
1644 }
1645
zend_optimizer_startup(void)1646 zend_result zend_optimizer_startup(void)
1647 {
1648 return zend_func_info_startup();
1649 }
1650
zend_optimizer_shutdown(void)1651 zend_result zend_optimizer_shutdown(void)
1652 {
1653 return zend_func_info_shutdown();
1654 }
1655