xref: /PHP-8.4/Zend/Optimizer/pass3.c (revision a79c70f5)
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 3: (Jump optimization)
23  * - optimize series of JMPs
24  */
25 
26 #include "Optimizer/zend_optimizer.h"
27 #include "Optimizer/zend_optimizer_internal.h"
28 #include "zend_API.h"
29 #include "zend_constants.h"
30 #include "zend_execute.h"
31 #include "zend_vm.h"
32 
33 /* we use "jmp_hitlist" to avoid infinity loops during jmp optimization */
in_hitlist(zend_op * target,zend_op ** jmp_hitlist,int jmp_hitlist_count)34 static zend_always_inline bool in_hitlist(zend_op *target, zend_op **jmp_hitlist, int jmp_hitlist_count)
35 {
36 	int i;
37 
38 	for (i = 0; i < jmp_hitlist_count; i++) {
39 		if (jmp_hitlist[i] == target) {
40 			return 1;
41 		}
42 	}
43 	return 0;
44 }
45 
46 #define CHECK_LOOP(target) \
47 	if (EXPECTED(!in_hitlist(target, jmp_hitlist, jmp_hitlist_count))) { \
48 		jmp_hitlist[jmp_hitlist_count++] = target;	\
49 	} else { \
50 		break; \
51 	}
52 
zend_optimizer_pass3(zend_op_array * op_array,zend_optimizer_ctx * ctx)53 void zend_optimizer_pass3(zend_op_array *op_array, zend_optimizer_ctx *ctx)
54 {
55 	zend_op *opline;
56 	zend_op *end;
57 	zend_op *target;
58 	zend_op **jmp_hitlist;
59 	int jmp_hitlist_count;
60 	ALLOCA_FLAG(use_heap);
61 
62 	jmp_hitlist = (zend_op**)do_alloca(sizeof(zend_op*)*op_array->last, use_heap);
63 	opline = op_array->opcodes;
64 	end =  opline + op_array->last;
65 
66 	while (opline < end) {
67 
68 		switch (opline->opcode) {
69 			case ZEND_JMP:
70 				jmp_hitlist_count = 0;
71 
72 				target = ZEND_OP1_JMP_ADDR(opline);
73 				while (1) {
74 					if (target->opcode == ZEND_JMP) {
75 						/* convert JMP L1 ... L1: JMP L2 to JMP L2 .. L1: JMP L2 */
76 						target = ZEND_OP1_JMP_ADDR(target);
77 						CHECK_LOOP(target);
78 					} else if (target->opcode == ZEND_NOP) {
79 						target = target + 1;
80 					} else {
81 						break;
82 					}
83 					ZEND_SET_OP_JMP_ADDR(opline, opline->op1, target);
84 				}
85 
86 				if (target == opline + 1) {
87 					/* convert L: JMP L+1 to NOP */
88 					MAKE_NOP(opline);
89 				} else if ((target->opcode == ZEND_RETURN ||
90 				            target->opcode == ZEND_RETURN_BY_REF ||
91 				            target->opcode == ZEND_GENERATOR_RETURN) &&
92 				           !(op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
93 					/* JMP L, L: RETURN to immediate RETURN */
94 					*opline = *target;
95 					if (opline->op1_type == IS_CONST) {
96 						zval zv;
97 						ZVAL_COPY(&zv, &ZEND_OP1_LITERAL(opline));
98 						opline->op1.constant = zend_optimizer_add_literal(op_array, &zv);
99 					}
100 				} else if (opline > op_array->opcodes &&
101 				           ((opline-1)->opcode == ZEND_JMPZ ||
102 				            (opline-1)->opcode == ZEND_JMPNZ)) {
103 				    if (ZEND_OP2_JMP_ADDR(opline-1) == target) {
104 						/* JMPZ(X,L1), JMP(L1) -> NOP, JMP(L1) */
105 						zend_optimizer_convert_to_free_op1(op_array, opline - 1);
106 				    }
107 				}
108 				break;
109 
110 			case ZEND_JMP_SET:
111 			case ZEND_COALESCE:
112 				jmp_hitlist_count = 0;
113 
114 				target = ZEND_OP2_JMP_ADDR(opline);
115 				while (1) {
116 					if (target->opcode == ZEND_JMP) {
117 						target = ZEND_OP1_JMP_ADDR(target);
118 						CHECK_LOOP(target);
119 					} else if (target->opcode == ZEND_NOP) {
120 						target = target + 1;
121 					} else {
122 						break;
123 					}
124 					ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
125 				}
126 				break;
127 
128 			case ZEND_JMPZ:
129 			case ZEND_JMPNZ:
130 				jmp_hitlist_count = 0;
131 
132 				target = ZEND_OP2_JMP_ADDR(opline);
133 				while (1) {
134 					if (target->opcode == ZEND_JMP) {
135 						/* plain JMP */
136 						/* JMPZ(X,L1), L1: JMP(L2) => JMPZ(X,L2), L1: JMP(L2) */
137 						target = ZEND_OP1_JMP_ADDR(target);
138 						CHECK_LOOP(target);
139 					} else if (target->opcode == opline->opcode &&
140 					           SAME_VAR(opline->op1, target->op1)) {
141 						/* same opcode and same var as this opcode */
142 						/* JMPZ(X,L1), L1: JMPZ(X,L2) => JMPZ(X,L2), L1: JMPZ(X,L2) */
143 						target = ZEND_OP2_JMP_ADDR(target);
144 						CHECK_LOOP(target);
145 					} else if (target->opcode == INV_COND(opline->opcode) &&
146 					           SAME_VAR(opline->op1, target->op1)) {
147 						/* convert JMPZ(X,L1), L1: JMPNZ(X,L2) to
148 						   JMPZ(X,L1+1) */
149 						target = target + 1;
150 					} else if (target->opcode == ZEND_NOP) {
151 						target = target + 1;
152 					} else {
153 						break;
154 					}
155 					ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
156 				}
157 
158 				/* convert L: JMPZ L+1 to NOP */
159 				if (target == opline + 1) {
160 					zend_optimizer_convert_to_free_op1(op_array, opline);
161 				}
162 				break;
163 
164 			case ZEND_JMPZ_EX:
165 			case ZEND_JMPNZ_EX:
166 				jmp_hitlist_count = 0;
167 
168 				target = ZEND_OP2_JMP_ADDR(opline);
169 				while (1) {
170 					if (target->opcode == ZEND_JMP) {
171 						/* plain JMP */
172 						/* JMPZ_EX(X,L1), L1: JMP(L2) => JMPZ_EX(X,L2), L1: JMP(L2) */
173 						target = ZEND_OP1_JMP_ADDR(target);
174 						CHECK_LOOP(target);
175 					} else if (target->opcode == opline->opcode-3 &&
176 					           (SAME_VAR(target->op1, opline->result) ||
177 					            SAME_VAR(target->op1, opline->op1))) {
178 						/* convert T=JMPZ_EX(X,L1), L1: JMPZ(T,L2) to
179 						   JMPZ_EX(X,L2) */
180 						target = ZEND_OP2_JMP_ADDR(target);
181 						CHECK_LOOP(target);
182 					} else if (target->opcode == opline->opcode &&
183 					           target->result.var == opline->result.var &&
184 					           (SAME_VAR(target->op1, opline->result) ||
185 					            SAME_VAR(target->op1, opline->op1))) {
186 						/* convert T=JMPZ_EX(X,L1), L1: T=JMPZ_EX(T,L2) to
187 						   JMPZ_EX(X,L2) */
188 						target = ZEND_OP2_JMP_ADDR(target);
189 						CHECK_LOOP(target);
190 					} else if (target->opcode == INV_EX_COND(opline->opcode) &&
191 					           (SAME_VAR(target->op1, opline->result) ||
192 					            SAME_VAR(target->op1, opline->op1))) {
193 					   /* convert T=JMPZ_EX(X,L1), L1: JMPNZ(T,L2) to
194 						  JMPZ_EX(X,L1+1) */
195 						target = target + 1;
196 					} else if (target->opcode == INV_EX_COND_EX(opline->opcode) &&
197 					           target->result.var == opline->result.var &&
198 					           (SAME_VAR(target->op1, opline->result) ||
199 					            SAME_VAR(target->op1, opline->op1))) {
200 					   /* convert T=JMPZ_EX(X,L1), L1: T=JMPNZ_EX(T,L2) to
201 						  JMPZ_EX(X,L1+1) */
202 						target = target + 1;
203 					} else if (target->opcode == ZEND_BOOL &&
204 					           (SAME_VAR(target->op1, opline->result) ||
205 					            SAME_VAR(target->op1, opline->op1))) {
206 						/* convert Y = JMPZ_EX(X,L1), L1: Z = BOOL(Y) to
207 						   Z = JMPZ_EX(X,L1+1) */
208 
209 						/* NOTE: This optimization pattern is not safe, but works, */
210 						/*       because result of JMPZ_EX instruction             */
211 						/*       is not used on the following path and             */
212 						/*       should be used once on the branch path.           */
213 						/*                                                         */
214 						/*       The pattern works well only if jumps processed in */
215 						/*       direct order, otherwise it breaks JMPZ_EX         */
216 						/*       sequences too early.                              */
217 						opline->result.var = target->result.var;
218 						target = target + 1;
219 						CHECK_LOOP(target);
220 					} else if (target->opcode == ZEND_NOP) {
221 						target = target + 1;
222 					} else {
223 						break;
224 					}
225 					ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
226 				}
227 
228 				/* convert L: T = JMPZ_EX X,L+1 to T = BOOL(X) */
229 				if (target == opline + 1) {
230 					opline->opcode = ZEND_BOOL;
231 					opline->op2.num = 0;
232 				}
233 				break;
234 		}
235 		opline++;
236 	}
237 	free_alloca(jmp_hitlist, use_heap);
238 }
239