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 #ifndef ZEND_OPTIMIZER_INTERNAL_H
23 #define ZEND_OPTIMIZER_INTERNAL_H
24
25 #include "zend_ssa.h"
26 #include "zend_func_info.h"
27
28 #define ZEND_OP1_LITERAL(opline) (op_array)->literals[(opline)->op1.constant]
29 #define ZEND_OP1_JMP_ADDR(opline) OP_JMP_ADDR(opline, (opline)->op1)
30 #define ZEND_OP2_LITERAL(opline) (op_array)->literals[(opline)->op2.constant]
31 #define ZEND_OP2_JMP_ADDR(opline) OP_JMP_ADDR(opline, (opline)->op2)
32
33 #define VAR_NUM(v) EX_VAR_TO_NUM(v)
34 #define NUM_VAR(v) EX_NUM_TO_VAR(v)
35
36 #define INV_COND(op) ((op) == ZEND_JMPZ ? ZEND_JMPNZ : ZEND_JMPZ)
37 #define INV_EX_COND(op) ((op) == ZEND_JMPZ_EX ? ZEND_JMPNZ : ZEND_JMPZ)
38 #define INV_COND_EX(op) ((op) == ZEND_JMPZ ? ZEND_JMPNZ_EX : ZEND_JMPZ_EX)
39 #define INV_EX_COND_EX(op) ((op) == ZEND_JMPZ_EX ? ZEND_JMPNZ_EX : ZEND_JMPZ_EX)
40
41 #define RESULT_UNUSED(op) (op->result_type == IS_UNUSED)
42 #define SAME_VAR(op1, op2) (op1 ## _type == op2 ## _type && op1.var == op2.var)
43
44 typedef struct _zend_optimizer_ctx {
45 zend_arena *arena;
46 zend_script *script;
47 HashTable *constants;
48 zend_long optimization_level;
49 zend_long debug_level;
50 } zend_optimizer_ctx;
51
52 #define LITERAL_LONG(op, val) do { \
53 zval _c; \
54 ZVAL_LONG(&_c, val); \
55 op.constant = zend_optimizer_add_literal(op_array, &_c); \
56 } while (0)
57
58 #define LITERAL_BOOL(op, val) do { \
59 zval _c; \
60 ZVAL_BOOL(&_c, val); \
61 op.constant = zend_optimizer_add_literal(op_array, &_c); \
62 } while (0)
63
64 #define literal_dtor(zv) do { \
65 zval_ptr_dtor_nogc(zv); \
66 ZVAL_NULL(zv); \
67 } while (0)
68
69 #define COPY_NODE(target, src) do { \
70 target ## _type = src ## _type; \
71 target = src; \
72 } while (0)
73
zend_optimizer_is_loop_var_free(const zend_op * opline)74 static inline bool zend_optimizer_is_loop_var_free(const zend_op *opline) {
75 return (opline->opcode == ZEND_FE_FREE && opline->extended_value != ZEND_FREE_ON_RETURN)
76 || (opline->opcode == ZEND_FREE && opline->extended_value == ZEND_FREE_SWITCH);
77 }
78
79 void zend_optimizer_convert_to_free_op1(zend_op_array *op_array, zend_op *opline);
80 int zend_optimizer_add_literal(zend_op_array *op_array, zval *zv);
81 bool zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int copy);
82 void zend_optimizer_collect_constant(zend_optimizer_ctx *ctx, zval *name, zval* value);
83 bool zend_optimizer_get_collected_constant(HashTable *constants, zval *name, zval* value);
84 zend_result zend_optimizer_eval_binary_op(zval *result, zend_uchar opcode, zval *op1, zval *op2);
85 zend_result zend_optimizer_eval_unary_op(zval *result, zend_uchar opcode, zval *op1);
86 zend_result zend_optimizer_eval_cast(zval *result, uint32_t type, zval *op1);
87 zend_result zend_optimizer_eval_strlen(zval *result, zval *op1);
88 zend_result zend_optimizer_eval_special_func_call(
89 zval *result, zend_string *name, zend_string *arg);
90 bool zend_optimizer_update_op1_const(zend_op_array *op_array,
91 zend_op *opline,
92 zval *val);
93 bool zend_optimizer_update_op2_const(zend_op_array *op_array,
94 zend_op *opline,
95 zval *val);
96 bool zend_optimizer_replace_by_const(zend_op_array *op_array,
97 zend_op *opline,
98 zend_uchar type,
99 uint32_t var,
100 zval *val);
101 zend_op *zend_optimizer_get_loop_var_def(const zend_op_array *op_array, zend_op *free_opline);
102 zend_class_entry *zend_optimizer_get_class_entry(
103 const zend_script *script, const zend_op_array *op_array, zend_string *lcname);
104 zend_class_entry *zend_optimizer_get_class_entry_from_op1(
105 const zend_script *script, const zend_op_array *op_array, const zend_op *opline);
106
107 void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx);
108 void zend_optimizer_pass3(zend_op_array *op_array, zend_optimizer_ctx *ctx);
109 void zend_optimize_func_calls(zend_op_array *op_array, zend_optimizer_ctx *ctx);
110 void zend_optimize_cfg(zend_op_array *op_array, zend_optimizer_ctx *ctx);
111 void zend_optimize_dfa(zend_op_array *op_array, zend_optimizer_ctx *ctx);
112 zend_result zend_dfa_analyze_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx, zend_ssa *ssa);
113 void zend_dfa_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx, zend_ssa *ssa, zend_call_info **call_map);
114 void zend_optimize_temporary_variables(zend_op_array *op_array, zend_optimizer_ctx *ctx);
115 void zend_optimizer_nop_removal(zend_op_array *op_array, zend_optimizer_ctx *ctx);
116 void zend_optimizer_compact_literals(zend_op_array *op_array, zend_optimizer_ctx *ctx);
117 void zend_optimizer_compact_vars(zend_op_array *op_array);
118 zend_function *zend_optimizer_get_called_func(
119 zend_script *script, zend_op_array *op_array, zend_op *opline, bool *is_prototype);
120 uint32_t zend_optimizer_classify_function(zend_string *name, uint32_t num_args);
121 void zend_optimizer_migrate_jump(zend_op_array *op_array, zend_op *new_opline, zend_op *opline);
122 void zend_optimizer_shift_jump(zend_op_array *op_array, zend_op *opline, uint32_t *shiftlist);
123 int sccp_optimize_op_array(zend_optimizer_ctx *ctx, zend_op_array *op_array, zend_ssa *ssa, zend_call_info **call_map);
124 int dce_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *optimizer_ctx, zend_ssa *ssa, bool reorder_dtor_effects);
125 zend_result zend_ssa_escape_analysis(const zend_script *script, zend_op_array *op_array, zend_ssa *ssa);
126
127 typedef void (*zend_op_array_func_t)(zend_op_array *, void *context);
128 void zend_foreach_op_array(zend_script *script, zend_op_array_func_t func, void *context);
129
130 #endif
131