php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
pass1.c
Go to the documentation of this file.
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
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
43static void replace_by_const_or_qm_assign(zend_op_array *op_array, zend_op *opline, zval *result) {
44 if (opline->op1_type == IS_CONST) {
46 }
47 if (opline->op2_type == IS_CONST) {
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
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;
67
68 while (opline < end) {
69 switch (opline->opcode) {
70 case ZEND_CONCAT:
72 if (opline->op1_type == IS_CONST && Z_TYPE(ZEND_OP1_LITERAL(opline)) != IS_STRING) {
74 }
75 if (opline->op2_type == IS_CONST && Z_TYPE(ZEND_OP2_LITERAL(opline)) != IS_STRING) {
77 }
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:
92 case ZEND_IS_SMALLER:
96 case ZEND_BOOL_XOR:
97 case ZEND_SPACESHIP:
98 case ZEND_CASE:
100 if (opline->op1_type == IS_CONST && opline->op2_type == IS_CONST &&
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) {
110 }
111 break;
112
113 case ZEND_CAST:
114 if (opline->op1_type == IS_CONST &&
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 &&
124 replace_by_const_or_qm_assign(op_array, opline, &result);
125 }
126 break;
127
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
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 */
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
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)
176 break;
177 }
178 } else {
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 &&
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 &&
270 replace_by_const_or_qm_assign(op_array, opline, &result);
271 }
272 break;
273 case ZEND_DEFINED:
275 break;
276 }
279 replace_by_const_or_qm_assign(op_array, opline, &result);
280 break;
282 if (collect_constants &&
283 Z_TYPE(ZEND_OP1_LITERAL(opline)) == IS_STRING &&
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));
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 }
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:
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:
353 case ZEND_JMP_NULL:
357 collect_constants = 0;
358 break;
359 }
360 opline++;
361 }
362}
bool zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int copy)
Definition block_pass.c:33
memset(ptr, 0, type->size)
zend_long offset
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
#define TO_STRING_NOWARN(val)
Definition pass1.c:37
void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx)
Definition pass1.c:60
unsigned const char * end
Definition php_ffi.h:51
zend_ast_kind kind
Definition zend_ast.h:187
zend_function * func
zend_op * opcodes
znode_op op1
uint8_t result_type
znode_op op2
znode_op result
uint8_t opcode
uint8_t op1_type
uint32_t extended_value
uint8_t op2_type
zend_op_array main_op_array
uint32_t var
uint32_t constant
uint32_t num
@ ZEND_AST_CONSTANT
Definition zend_ast.h:37
struct _zval_struct zval
struct _zend_op zend_op
#define IS_UNUSED
#define IS_CONST
struct _zend_op_array zend_op_array
struct _zend_class_constant zend_class_constant
#define MAKE_NOP(opline)
#define SET_UNUSED(op)
ZEND_API zval * zend_get_constant_str(const char *name, size_t name_len)
union _zend_function zend_function
#define EG(v)
ZEND_API bool ZEND_FASTCALL zend_is_true(const zval *op)
const zend_class_constant * zend_fetch_class_const_info(const zend_script *script, const zend_op_array *op_array, const zend_op *opline, bool *is_prototype)
bool zend_optimizer_get_collected_constant(HashTable *constants, zval *name, zval *value)
void zend_optimizer_collect_constant(zend_optimizer_ctx *ctx, zval *name, zval *value)
bool zend_optimizer_update_op1_const(zend_op_array *op_array, zend_op *opline, zval *val)
zend_result zend_optimizer_eval_strlen(zval *result, const zval *op1)
zend_result zend_optimizer_eval_binary_op(zval *result, uint8_t opcode, zval *op1, zval *op2)
zend_result zend_optimizer_eval_cast(zval *result, uint32_t type, zval *op1)
zend_result zend_optimizer_eval_special_func_call(zval *result, zend_string *name, zend_string *arg)
zend_result zend_optimizer_eval_unary_op(zval *result, uint8_t opcode, zval *op1)
bool zend_optimizer_replace_by_const(zend_op_array *op_array, zend_op *opline, uint8_t type, uint32_t var, zval *val)
#define ZEND_OPTIMIZER_PASS_15
#define literal_dtor(zv)
#define ZEND_OP1_LITERAL(opline)
#define RESULT_UNUSED(op)
#define ZEND_OP2_LITERAL(opline)
struct _zend_optimizer_ctx zend_optimizer_ctx
#define COPY_NODE(target, src)
#define ZEND_FALLTHROUGH
#define zend_string_equals_literal(str, literal)
#define zend_string_equals_literal_ci(str, c)
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define ZVAL_FALSE(z)
#define ZVAL_TRUE(z)
#define IS_STRING
Definition zend_types.h:606
#define ZVAL_COPY_OR_DUP(z, v)
#define Z_STR(zval)
Definition zend_types.h:971
#define Z_STRVAL(zval)
Definition zend_types.h:974
#define Z_STRLEN(zval)
Definition zend_types.h:977
#define IS_CONSTANT_AST
Definition zend_types.h:611
#define Z_TYPE(zval)
Definition zend_types.h:659
struct _zend_ast zend_ast
Definition zend_types.h:102
struct _zend_execute_data zend_execute_data
Definition zend_types.h:91
#define Z_ASTVAL_P(zval_p)
bool result
#define ZEND_SL
#define ZEND_IS_IDENTICAL
#define ZEND_FETCH_CONSTANT
#define ZEND_BOOL_NOT
#define ZEND_FE_FETCH_RW
#define ZEND_RETURN
#define ZEND_FE_RESET_RW
#define ZEND_THROW
#define ZEND_FE_FETCH_R
#define ZEND_INIT_FCALL
#define ZEND_ASSERT_CHECK
#define ZEND_SEND_VAL
#define ZEND_RETURN_BY_REF
#define ZEND_BOOL_XOR
#define ZEND_DECLARE_CONST
#define ZEND_NOP
#define ZEND_JMPZ
#define ZEND_JMP_SET
#define ZEND_POW
#define ZEND_SUB
#define ZEND_JMPZ_EX
#define ZEND_IS_SMALLER
#define ZEND_DEFINED
#define ZEND_BW_XOR
#define ZEND_IS_NOT_EQUAL
#define ZEND_IS_NOT_IDENTICAL
#define ZEND_DIV
#define ZEND_CONCAT
#define ZEND_FAST_CONCAT
#define ZEND_BW_OR
#define ZEND_JMPNZ_EX
#define ZEND_IS_EQUAL
#define ZEND_IS_SMALLER_OR_EQUAL
#define ZEND_STRLEN
#define ZEND_MUL
#define ZEND_CASE_STRICT
#define ZEND_FAST_CALL
#define ZEND_DO_ICALL
#define ZEND_JMP_NULL
#define ZEND_BW_NOT
#define ZEND_BIND_INIT_STATIC_OR_JMP
#define ZEND_BW_AND
#define ZEND_GENERATOR_RETURN
#define ZEND_JMP
#define ZEND_FE_RESET_R
#define ZEND_JMP_FRAMELESS
#define ZEND_SPACESHIP
#define ZEND_SR
#define ZEND_FETCH_CLASS_CONSTANT
#define ZEND_VERIFY_NEVER_TYPE
#define ZEND_JMPNZ
#define ZEND_CAST
#define ZEND_QM_ASSIGN
#define ZEND_MOD
#define ZEND_CATCH
#define ZEND_ADD
#define ZEND_COALESCE
#define ZEND_ASSIGN_OP
#define ZEND_MATCH_ERROR
#define ZEND_FAST_RET
#define ZEND_CASE