xref: /PHP-8.3/Zend/Optimizer/zend_inference.h (revision 4ee72a8d)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine, e-SSA based Type & Range Inference                      |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | https://www.php.net/license/3_01.txt                                 |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Dmitry Stogov <dmitry@php.net>                              |
16    +----------------------------------------------------------------------+
17 */
18 
19 #ifndef ZEND_INFERENCE_H
20 #define ZEND_INFERENCE_H
21 
22 #include "zend_optimizer.h"
23 #include "zend_ssa.h"
24 #include "zend_bitset.h"
25 
26 /* Bitmask for type inference (zend_ssa_var_info.type) */
27 #include "zend_type_info.h"
28 
29 #include <stdint.h>
30 
31 #define MAY_BE_PACKED_GUARD         (1<<27) /* needs packed array guard */
32 #define MAY_BE_CLASS_GUARD          (1<<27) /* needs class guard */
33 #define MAY_BE_GUARD                (1<<28) /* needs type guard */
34 
35 #define MAY_HAVE_DTOR \
36 	(MAY_BE_OBJECT|MAY_BE_RESOURCE \
37 	|MAY_BE_ARRAY_OF_ARRAY|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE)
38 
39 #define DEFINE_SSA_OP_HAS_RANGE(opN) \
40 	static zend_always_inline bool _ssa_##opN##_has_range(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, const zend_ssa_op *ssa_op) \
41 	{ \
42 		if (opline->opN##_type == IS_CONST) { \
43 			zval *zv = CRT_CONSTANT(opline->opN); \
44 			return (Z_TYPE_P(zv) == IS_LONG); \
45 		} else { \
46 			return (opline->opN##_type != IS_UNUSED && \
47 		        ssa->var_info && \
48 		        ssa_op->opN##_use >= 0 && \
49 			    ssa->var_info[ssa_op->opN##_use].has_range); \
50 		} \
51 		return 0; \
52 	} \
53 
54 #define DEFINE_SSA_OP_MIN_RANGE(opN) \
55 	static zend_always_inline zend_long _ssa_##opN##_min_range(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, const zend_ssa_op *ssa_op) \
56 	{ \
57 		if (opline->opN##_type == IS_CONST) { \
58 			zval *zv = CRT_CONSTANT(opline->opN); \
59 			if (Z_TYPE_P(zv) == IS_LONG) { \
60 				return Z_LVAL_P(zv); \
61 			} \
62 		} else if (opline->opN##_type != IS_UNUSED && \
63 		    ssa->var_info && \
64 		    ssa_op->opN##_use >= 0 && \
65 		    ssa->var_info[ssa_op->opN##_use].has_range) { \
66 			return ssa->var_info[ssa_op->opN##_use].range.min; \
67 		} \
68 		return ZEND_LONG_MIN; \
69 	} \
70 
71 #define DEFINE_SSA_OP_MAX_RANGE(opN) \
72 	static zend_always_inline zend_long _ssa_##opN##_max_range(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, const zend_ssa_op *ssa_op) \
73 	{ \
74 		if (opline->opN##_type == IS_CONST) { \
75 			zval *zv = CRT_CONSTANT(opline->opN); \
76 			if (Z_TYPE_P(zv) == IS_LONG) { \
77 				return Z_LVAL_P(zv); \
78 			} \
79 		} else if (opline->opN##_type != IS_UNUSED && \
80 		    ssa->var_info && \
81 		    ssa_op->opN##_use >= 0 && \
82 		    ssa->var_info[ssa_op->opN##_use].has_range) { \
83 			return ssa->var_info[ssa_op->opN##_use].range.max; \
84 		} \
85 		return ZEND_LONG_MAX; \
86 	} \
87 
88 #define DEFINE_SSA_OP_RANGE_UNDERFLOW(opN) \
89 	static zend_always_inline char _ssa_##opN##_range_underflow(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, const zend_ssa_op *ssa_op) \
90 	{ \
91 		if (opline->opN##_type == IS_CONST) { \
92 			zval *zv = CRT_CONSTANT(opline->opN); \
93 			if (Z_TYPE_P(zv) == IS_LONG) { \
94 				return 0; \
95 			} \
96 		} else if (opline->opN##_type != IS_UNUSED && \
97 		    ssa->var_info && \
98 		    ssa_op->opN##_use >= 0 && \
99 		    ssa->var_info[ssa_op->opN##_use].has_range) { \
100 			return ssa->var_info[ssa_op->opN##_use].range.underflow; \
101 		} \
102 		return 1; \
103 	} \
104 
105 #define DEFINE_SSA_OP_RANGE_OVERFLOW(opN) \
106 	static zend_always_inline char _ssa_##opN##_range_overflow(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, const zend_ssa_op *ssa_op) \
107 	{ \
108 		if (opline->opN##_type == IS_CONST) { \
109 			zval *zv = CRT_CONSTANT(opline->opN); \
110 			if (Z_TYPE_P(zv) == IS_LONG) { \
111 				return 0; \
112 			} \
113 		} else if (opline->opN##_type != IS_UNUSED && \
114 		    ssa->var_info && \
115 		    ssa_op->opN##_use >= 0 && \
116 		    ssa->var_info[ssa_op->opN##_use].has_range) { \
117 			return ssa->var_info[ssa_op->opN##_use].range.overflow; \
118 		} \
119 		return 1; \
120 	} \
121 
122 DEFINE_SSA_OP_HAS_RANGE(op1)
123 DEFINE_SSA_OP_MIN_RANGE(op1)
124 DEFINE_SSA_OP_MAX_RANGE(op1)
125 DEFINE_SSA_OP_RANGE_UNDERFLOW(op1)
126 DEFINE_SSA_OP_RANGE_OVERFLOW(op1)
127 DEFINE_SSA_OP_HAS_RANGE(op2)
128 DEFINE_SSA_OP_MIN_RANGE(op2)
129 DEFINE_SSA_OP_MAX_RANGE(op2)
130 DEFINE_SSA_OP_RANGE_UNDERFLOW(op2)
131 DEFINE_SSA_OP_RANGE_OVERFLOW(op2)
132 
133 #define OP1_HAS_RANGE()       (_ssa_op1_has_range (op_array, ssa, opline, ssa_op))
134 #define OP1_MIN_RANGE()       (_ssa_op1_min_range (op_array, ssa, opline, ssa_op))
135 #define OP1_MAX_RANGE()       (_ssa_op1_max_range (op_array, ssa, opline, ssa_op))
136 #define OP1_RANGE_UNDERFLOW() (_ssa_op1_range_underflow (op_array, ssa, opline, ssa_op))
137 #define OP1_RANGE_OVERFLOW()  (_ssa_op1_range_overflow (op_array, ssa, opline, ssa_op))
138 #define OP2_HAS_RANGE()       (_ssa_op2_has_range (op_array, ssa, opline, ssa_op))
139 #define OP2_MIN_RANGE()       (_ssa_op2_min_range (op_array, ssa, opline, ssa_op))
140 #define OP2_MAX_RANGE()       (_ssa_op2_max_range (op_array, ssa, opline, ssa_op))
141 #define OP2_RANGE_UNDERFLOW() (_ssa_op2_range_underflow (op_array, ssa, opline, ssa_op))
142 #define OP2_RANGE_OVERFLOW()  (_ssa_op2_range_overflow (op_array, ssa, opline, ssa_op))
143 
144 BEGIN_EXTERN_C()
145 ZEND_API uint32_t ZEND_FASTCALL zend_array_type_info(const zval *zv);
END_EXTERN_C()146 END_EXTERN_C()
147 
148 static zend_always_inline uint32_t _const_op_type(const zval *zv) {
149 	if (Z_TYPE_P(zv) == IS_CONSTANT_AST) {
150 		return MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY;
151 	} else if (Z_TYPE_P(zv) == IS_ARRAY) {
152 		return zend_array_type_info(zv);
153 	} else {
154 		uint32_t tmp = (1 << Z_TYPE_P(zv));
155 
156 		if (Z_REFCOUNTED_P(zv)) {
157 			tmp |= MAY_BE_RC1 | MAY_BE_RCN;
158 		} else if (Z_TYPE_P(zv) == IS_STRING) {
159 			tmp |= MAY_BE_RCN;
160 		}
161 		return tmp;
162 	}
163 }
164 
get_ssa_var_info(const zend_ssa * ssa,int ssa_var_num)165 static zend_always_inline uint32_t get_ssa_var_info(const zend_ssa *ssa, int ssa_var_num)
166 {
167 	if (ssa->var_info && ssa_var_num >= 0) {
168 		return ssa->var_info[ssa_var_num].type;
169 	} else {
170 		return MAY_BE_UNDEF | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | MAY_BE_INDIRECT | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
171 	}
172 }
173 
174 #define DEFINE_SSA_OP_INFO(opN) \
175 	static zend_always_inline uint32_t _ssa_##opN##_info(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, const zend_ssa_op *ssa_op) \
176 	{																		\
177 		if (opline->opN##_type == IS_CONST) {							\
178 			return _const_op_type(CRT_CONSTANT(opline->opN)); \
179 		} else { \
180 			return get_ssa_var_info(ssa, ssa->var_info ? ssa_op->opN##_use : -1); \
181 		} \
182 	} \
183 
184 #define DEFINE_SSA_OP_DEF_INFO(opN) \
185 	static zend_always_inline uint32_t _ssa_##opN##_def_info(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, const zend_ssa_op *ssa_op) \
186 	{ \
187 		return get_ssa_var_info(ssa, ssa->var_info ? ssa_op->opN##_def : -1); \
188 	} \
189 
190 
191 DEFINE_SSA_OP_INFO(op1)
DEFINE_SSA_OP_INFO(op2)192 DEFINE_SSA_OP_INFO(op2)
193 DEFINE_SSA_OP_INFO(result)
194 DEFINE_SSA_OP_DEF_INFO(op1)
195 DEFINE_SSA_OP_DEF_INFO(op2)
196 DEFINE_SSA_OP_DEF_INFO(result)
197 
198 #define OP1_INFO()           (_ssa_op1_info(op_array, ssa, opline, ssa_op))
199 #define OP2_INFO()           (_ssa_op2_info(op_array, ssa, opline, ssa_op))
200 #define OP1_DATA_INFO()      (_ssa_op1_info(op_array, ssa, (opline+1), (ssa_op+1)))
201 #define OP2_DATA_INFO()      (_ssa_op2_info(op_array, ssa, (opline+1), (ssa_op+1)))
202 #define RES_USE_INFO()       (_ssa_result_info(op_array, ssa, opline, ssa_op))
203 #define OP1_DEF_INFO()       (_ssa_op1_def_info(op_array, ssa, opline, ssa_op))
204 #define OP2_DEF_INFO()       (_ssa_op2_def_info(op_array, ssa, opline, ssa_op))
205 #define OP1_DATA_DEF_INFO()  (_ssa_op1_def_info(op_array, ssa, (opline+1), (ssa_op+1)))
206 #define OP2_DATA_DEF_INFO()  (_ssa_op2_def_info(op_array, ssa, (opline+1), (ssa_op+1)))
207 #define RES_INFO()           (_ssa_result_def_info(op_array, ssa, opline, ssa_op))
208 
209 static zend_always_inline bool zend_add_will_overflow(zend_long a, zend_long b) {
210 	return (b > 0 && a > ZEND_LONG_MAX - b)
211 		|| (b < 0 && a < ZEND_LONG_MIN - b);
212 }
zend_sub_will_overflow(zend_long a,zend_long b)213 static zend_always_inline bool zend_sub_will_overflow(zend_long a, zend_long b) {
214 	return (b > 0 && a < ZEND_LONG_MIN + b)
215 		|| (b < 0 && a > ZEND_LONG_MAX + b);
216 }
217 
218 BEGIN_EXTERN_C()
219 
220 ZEND_API void zend_ssa_find_false_dependencies(const zend_op_array *op_array, zend_ssa *ssa);
221 ZEND_API void zend_ssa_find_sccs(const zend_op_array *op_array, zend_ssa *ssa);
222 ZEND_API zend_result zend_ssa_inference(zend_arena **raena, const zend_op_array *op_array, const zend_script *script, zend_ssa *ssa, zend_long optimization_level);
223 
224 ZEND_API uint32_t zend_array_element_type(uint32_t t1, uint8_t op_type, int write, int insert);
225 
226 ZEND_API bool zend_inference_propagate_range(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, const zend_ssa_op* ssa_op, int var, zend_ssa_range *tmp);
227 
228 ZEND_API uint32_t zend_fetch_arg_info_type(
229 	const zend_script *script, const zend_arg_info *arg_info, zend_class_entry **pce);
230 ZEND_API void zend_init_func_return_info(
231 	const zend_op_array *op_array, const zend_script *script, zend_ssa_var_info *ret);
232 uint32_t zend_get_return_info_from_signature_only(
233 		const zend_function *func, const zend_script *script,
234 		zend_class_entry **ce, bool *ce_is_instanceof, bool use_tentative_return_info);
235 
236 ZEND_API bool zend_may_throw_ex(const zend_op *opline, const zend_ssa_op *ssa_op, const zend_op_array *op_array, const zend_ssa *ssa, uint32_t t1, uint32_t t2);
237 ZEND_API bool zend_may_throw(const zend_op *opline, const zend_ssa_op *ssa_op, const zend_op_array *op_array, const zend_ssa *ssa);
238 
239 ZEND_API zend_result zend_update_type_info(
240 	const zend_op_array *op_array, zend_ssa *ssa, const zend_script *script,
241 	zend_op *opline, zend_ssa_op *ssa_op, const zend_op **ssa_opcodes,
242 	zend_long optimization_level);
243 
244 END_EXTERN_C()
245 
246 #endif /* ZEND_INFERENCE_H */
247