xref: /PHP-8.4/Zend/Optimizer/pass1.c (revision c5745f40)
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 /* pass 1 (Simple local optimizations)
23  * - persistent constant substitution (true, false, null, etc)
24  * - constant casting (ADD expects numbers, CONCAT strings, etc)
25  * - constant expression evaluation
26  * - optimize constant conditional JMPs
27  * - pre-evaluate constant function calls
28  */
29 
30 #include "Optimizer/zend_optimizer.h"
31 #include "Optimizer/zend_optimizer_internal.h"
32 #include "zend_API.h"
33 #include "zend_constants.h"
34 #include "zend_execute.h"
35 #include "zend_vm.h"
36 
37 #define TO_STRING_NOWARN(val) do { \
38 	if (Z_TYPE_P(val) < IS_ARRAY) { \
39 		convert_to_string(val); \
40 	} \
41 } while (0)
42 
replace_by_const_or_qm_assign(zend_op_array * op_array,zend_op * opline,zval * result)43 static void replace_by_const_or_qm_assign(zend_op_array *op_array, zend_op *opline, zval *result) {
44 	if (opline->op1_type == IS_CONST) {
45 		literal_dtor(&ZEND_OP1_LITERAL(opline));
46 	}
47 	if (opline->op2_type == IS_CONST) {
48 		literal_dtor(&ZEND_OP2_LITERAL(opline));
49 	}
50 	if (zend_optimizer_replace_by_const(op_array, opline + 1, opline->result_type, opline->result.var, result)) {
51 		MAKE_NOP(opline);
52 	} else {
53 		opline->opcode = ZEND_QM_ASSIGN;
54 		opline->extended_value = 0;
55 		SET_UNUSED(opline->op2);
56 		zend_optimizer_update_op1_const(op_array, opline, result);
57 	}
58 }
59 
zend_optimizer_pass1(zend_op_array * op_array,zend_optimizer_ctx * ctx)60 void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx)
61 {
62 	zend_op *opline = op_array->opcodes;
63 	zend_op *end = opline + op_array->last;
64 	bool collect_constants = (ZEND_OPTIMIZER_PASS_15 & ctx->optimization_level)?
65 		(op_array == &ctx->script->main_op_array) : 0;
66 	zval result;
67 
68 	while (opline < end) {
69 		switch (opline->opcode) {
70 		case ZEND_CONCAT:
71 		case ZEND_FAST_CONCAT:
72 			if (opline->op1_type == IS_CONST && Z_TYPE(ZEND_OP1_LITERAL(opline)) != IS_STRING) {
73 				TO_STRING_NOWARN(&ZEND_OP1_LITERAL(opline));
74 			}
75 			if (opline->op2_type == IS_CONST && Z_TYPE(ZEND_OP2_LITERAL(opline)) != IS_STRING) {
76 				TO_STRING_NOWARN(&ZEND_OP2_LITERAL(opline));
77 			}
78 			ZEND_FALLTHROUGH;
79 		case ZEND_ADD:
80 		case ZEND_SUB:
81 		case ZEND_MUL:
82 		case ZEND_DIV:
83 		case ZEND_POW:
84 		case ZEND_MOD:
85 		case ZEND_SL:
86 		case ZEND_SR:
87 		case ZEND_BW_OR:
88 		case ZEND_BW_AND:
89 		case ZEND_BW_XOR:
90 		case ZEND_IS_EQUAL:
91 		case ZEND_IS_NOT_EQUAL:
92 		case ZEND_IS_SMALLER:
93 		case ZEND_IS_SMALLER_OR_EQUAL:
94 		case ZEND_IS_IDENTICAL:
95 		case ZEND_IS_NOT_IDENTICAL:
96 		case ZEND_BOOL_XOR:
97 		case ZEND_SPACESHIP:
98 		case ZEND_CASE:
99 		case ZEND_CASE_STRICT:
100 			if (opline->op1_type == IS_CONST && opline->op2_type == IS_CONST &&
101 					zend_optimizer_eval_binary_op(&result, opline->opcode, &ZEND_OP1_LITERAL(opline), &ZEND_OP2_LITERAL(opline)) == SUCCESS) {
102 				replace_by_const_or_qm_assign(op_array, opline, &result);
103 			}
104 			break;
105 
106 		case ZEND_ASSIGN_OP:
107 			if (opline->extended_value == ZEND_CONCAT && opline->op2_type == IS_CONST
108 					&& Z_TYPE(ZEND_OP2_LITERAL(opline)) != IS_STRING) {
109 				TO_STRING_NOWARN(&ZEND_OP2_LITERAL(opline));
110 			}
111 			break;
112 
113 		case ZEND_CAST:
114 			if (opline->op1_type == IS_CONST &&
115 					zend_optimizer_eval_cast(&result, opline->extended_value, &ZEND_OP1_LITERAL(opline)) == SUCCESS) {
116 				replace_by_const_or_qm_assign(op_array, opline, &result);
117 			}
118 			break;
119 
120 		case ZEND_BW_NOT:
121 		case ZEND_BOOL_NOT:
122 			if (opline->op1_type == IS_CONST &&
123 					zend_optimizer_eval_unary_op(&result, opline->opcode, &ZEND_OP1_LITERAL(opline)) == SUCCESS) {
124 				replace_by_const_or_qm_assign(op_array, opline, &result);
125 			}
126 			break;
127 
128 		case ZEND_FETCH_CONSTANT:
129 			if (opline->op2_type == IS_CONST &&
130 				Z_TYPE(ZEND_OP2_LITERAL(opline)) == IS_STRING &&
131 				zend_string_equals_literal(Z_STR(ZEND_OP2_LITERAL(opline)), "__COMPILER_HALT_OFFSET__")) {
132 				/* substitute __COMPILER_HALT_OFFSET__ constant */
133 				zend_execute_data *orig_execute_data = EG(current_execute_data);
134 				zend_execute_data fake_execute_data;
135 				zval *offset;
136 
137 				memset(&fake_execute_data, 0, sizeof(zend_execute_data));
138 				fake_execute_data.func = (zend_function*)op_array;
139 				EG(current_execute_data) = &fake_execute_data;
140 				if ((offset = zend_get_constant_str("__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__") - 1)) != NULL) {
141 
142 					literal_dtor(&ZEND_OP2_LITERAL(opline));
143 					replace_by_const_or_qm_assign(op_array, opline, offset);
144 				}
145 				EG(current_execute_data) = orig_execute_data;
146 				break;
147 			}
148 
149 			if (opline->op2_type == IS_CONST &&
150 				Z_TYPE(ZEND_OP2_LITERAL(opline)) == IS_STRING) {
151 				/* substitute persistent constants */
152 				if (!zend_optimizer_get_persistent_constant(Z_STR(ZEND_OP2_LITERAL(opline)), &result, 1)) {
153 					if (!ctx->constants || !zend_optimizer_get_collected_constant(ctx->constants, &ZEND_OP2_LITERAL(opline), &result)) {
154 						break;
155 					}
156 				}
157 				if (Z_TYPE(result) == IS_CONSTANT_AST) {
158 					break;
159 				}
160 				replace_by_const_or_qm_assign(op_array, opline, &result);
161 			}
162 			break;
163 
164 		case ZEND_FETCH_CLASS_CONSTANT: {
165 			bool is_prototype;
166 			const zend_class_constant *cc = zend_fetch_class_const_info(ctx->script, op_array, opline, &is_prototype);
167 			if (!cc || is_prototype) {
168 				break;
169 			}
170 			const zval *c = &cc->value;
171 			if (Z_TYPE_P(c) == IS_CONSTANT_AST) {
172 				zend_ast *ast = Z_ASTVAL_P(c);
173 				if (ast->kind != ZEND_AST_CONSTANT
174 				 || !zend_optimizer_get_persistent_constant(zend_ast_get_constant_name(ast), &result, 1)
175 				 || Z_TYPE(result) == IS_CONSTANT_AST) {
176 					break;
177 				}
178 			} else {
179 				ZVAL_COPY_OR_DUP(&result, c);
180 			}
181 			replace_by_const_or_qm_assign(op_array, opline, &result);
182 			break;
183 		}
184 
185 		case ZEND_DO_ICALL: {
186 			zend_op *send1_opline = opline - 1;
187 			zend_op *send2_opline = NULL;
188 			zend_op *init_opline = NULL;
189 
190 			while (send1_opline->opcode == ZEND_NOP) {
191 				send1_opline--;
192 			}
193 			if (send1_opline->opcode != ZEND_SEND_VAL ||
194 			    send1_opline->op1_type != IS_CONST) {
195 				/* don't collect constants after unknown function call */
196 				collect_constants = 0;
197 				break;
198 			}
199 			if (send1_opline->op2.num == 2) {
200 				send2_opline = send1_opline;
201 				send1_opline--;
202 				while (send1_opline->opcode == ZEND_NOP) {
203 					send1_opline--;
204 				}
205 				if (send1_opline->opcode != ZEND_SEND_VAL ||
206 				    send1_opline->op1_type != IS_CONST) {
207 					/* don't collect constants after unknown function call */
208 					collect_constants = 0;
209 					break;
210 				}
211 			}
212 			init_opline = send1_opline - 1;
213 			while (init_opline->opcode == ZEND_NOP) {
214 				init_opline--;
215 			}
216 			if (init_opline->opcode != ZEND_INIT_FCALL ||
217 			    init_opline->op2_type != IS_CONST ||
218 			    Z_TYPE(ZEND_OP2_LITERAL(init_opline)) != IS_STRING) {
219 				/* don't collect constants after unknown function call */
220 				collect_constants = 0;
221 				break;
222 			}
223 
224 			/* define("name", scalar); */
225 			if (zend_string_equals_literal_ci(Z_STR(ZEND_OP2_LITERAL(init_opline)), "define")) {
226 
227 				if (Z_TYPE(ZEND_OP1_LITERAL(send1_opline)) == IS_STRING && send2_opline) {
228 
229 					if (collect_constants) {
230 						zend_optimizer_collect_constant(ctx, &ZEND_OP1_LITERAL(send1_opline), &ZEND_OP1_LITERAL(send2_opline));
231 					}
232 
233 					if (RESULT_UNUSED(opline) &&
234 					    !zend_memnstr(Z_STRVAL(ZEND_OP1_LITERAL(send1_opline)), "::", sizeof("::") - 1, Z_STRVAL(ZEND_OP1_LITERAL(send1_opline)) + Z_STRLEN(ZEND_OP1_LITERAL(send1_opline)))) {
235 
236 						opline->opcode = ZEND_DECLARE_CONST;
237 						opline->op1_type = IS_CONST;
238 						opline->op2_type = IS_CONST;
239 						opline->result_type = IS_UNUSED;
240 						opline->op1.constant = send1_opline->op1.constant;
241 						opline->op2.constant = send2_opline->op1.constant;
242 						opline->result.num = 0;
243 
244 						literal_dtor(&ZEND_OP2_LITERAL(init_opline));
245 						MAKE_NOP(init_opline);
246 						MAKE_NOP(send1_opline);
247 						MAKE_NOP(send2_opline);
248 					}
249 					break;
250 				}
251 			}
252 
253 			if (!send2_opline && Z_TYPE(ZEND_OP1_LITERAL(send1_opline)) == IS_STRING &&
254 					zend_optimizer_eval_special_func_call(&result, Z_STR(ZEND_OP2_LITERAL(init_opline)), Z_STR(ZEND_OP1_LITERAL(send1_opline))) == SUCCESS) {
255 				literal_dtor(&ZEND_OP2_LITERAL(init_opline));
256 				MAKE_NOP(init_opline);
257 				literal_dtor(&ZEND_OP1_LITERAL(send1_opline));
258 				MAKE_NOP(send1_opline);
259 				replace_by_const_or_qm_assign(op_array, opline, &result);
260 				break;
261 			}
262 
263 			/* don't collect constants after any other function call */
264 			collect_constants = 0;
265 			break;
266 		}
267 		case ZEND_STRLEN:
268 			if (opline->op1_type == IS_CONST &&
269 					zend_optimizer_eval_strlen(&result, &ZEND_OP1_LITERAL(opline)) == SUCCESS) {
270 				replace_by_const_or_qm_assign(op_array, opline, &result);
271 			}
272 			break;
273 		case ZEND_DEFINED:
274 			if (!zend_optimizer_get_persistent_constant(Z_STR(ZEND_OP1_LITERAL(opline)), &result, 0)) {
275 				break;
276 			}
277 			ZVAL_TRUE(&result);
278 			literal_dtor(&ZEND_OP1_LITERAL(opline));
279 			replace_by_const_or_qm_assign(op_array, opline, &result);
280 			break;
281 		case ZEND_DECLARE_CONST:
282 			if (collect_constants &&
283 			    Z_TYPE(ZEND_OP1_LITERAL(opline)) == IS_STRING &&
284 			    Z_TYPE(ZEND_OP2_LITERAL(opline)) != IS_CONSTANT_AST) {
285 				zend_optimizer_collect_constant(ctx, &ZEND_OP1_LITERAL(opline), &ZEND_OP2_LITERAL(opline));
286 			}
287 			break;
288 
289 		case ZEND_JMPZ_EX:
290 		case ZEND_JMPNZ_EX:
291 			/* convert Ti = JMPZ_EX(C, L) => Ti = QM_ASSIGN(C)
292 			   in case we know it wouldn't jump */
293 			if (opline->op1_type == IS_CONST) {
294 				if (zend_is_true(&ZEND_OP1_LITERAL(opline))) {
295 					if (opline->opcode == ZEND_JMPZ_EX) {
296 						opline->opcode = ZEND_QM_ASSIGN;
297 						zval_ptr_dtor_nogc(&ZEND_OP1_LITERAL(opline));
298 						ZVAL_TRUE(&ZEND_OP1_LITERAL(opline));
299 						opline->op2.num = 0;
300 						break;
301 					}
302 				} else {
303 					if (opline->opcode == ZEND_JMPNZ_EX) {
304 						opline->opcode = ZEND_QM_ASSIGN;
305 						zval_ptr_dtor_nogc(&ZEND_OP1_LITERAL(opline));
306 						ZVAL_FALSE(&ZEND_OP1_LITERAL(opline));
307 						opline->op2.num = 0;
308 						break;
309 					}
310 				}
311 			}
312 			collect_constants = 0;
313 			break;
314 
315 		case ZEND_JMPZ:
316 		case ZEND_JMPNZ:
317 			if (opline->op1_type == IS_CONST) {
318 				bool should_jmp = zend_is_true(&ZEND_OP1_LITERAL(opline));
319 
320 				if (opline->opcode == ZEND_JMPZ) {
321 					should_jmp = !should_jmp;
322 				}
323 				literal_dtor(&ZEND_OP1_LITERAL(opline));
324 				opline->op1_type = IS_UNUSED;
325 				if (should_jmp) {
326 					opline->opcode = ZEND_JMP;
327 					COPY_NODE(opline->op1, opline->op2);
328 					opline->op2.num = 0;
329 				} else {
330 					MAKE_NOP(opline);
331 					break;
332 				}
333 			}
334 			collect_constants = 0;
335 			break;
336 
337 		case ZEND_RETURN:
338 		case ZEND_RETURN_BY_REF:
339 		case ZEND_GENERATOR_RETURN:
340 		case ZEND_THROW:
341 		case ZEND_MATCH_ERROR:
342 		case ZEND_CATCH:
343 		case ZEND_FAST_CALL:
344 		case ZEND_FAST_RET:
345 		case ZEND_JMP:
346 		case ZEND_FE_RESET_R:
347 		case ZEND_FE_RESET_RW:
348 		case ZEND_FE_FETCH_R:
349 		case ZEND_FE_FETCH_RW:
350 		case ZEND_JMP_SET:
351 		case ZEND_COALESCE:
352 		case ZEND_ASSERT_CHECK:
353 		case ZEND_JMP_NULL:
354 		case ZEND_VERIFY_NEVER_TYPE:
355 		case ZEND_BIND_INIT_STATIC_OR_JMP:
356 		case ZEND_JMP_FRAMELESS:
357 			collect_constants = 0;
358 			break;
359 		}
360 		opline++;
361 	}
362 }
363