1 /*
2 +----------------------------------------------------------------------+
3 | Zend OPcache |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1998-2018 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 | http://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:
23 * - optimize $i = $i+expr to $i+=expr
24 * - optimize series of JMPs
25 * - change $i++ to ++$i where possible
26 */
27
28 #include "php.h"
29 #include "Optimizer/zend_optimizer.h"
30 #include "Optimizer/zend_optimizer_internal.h"
31 #include "zend_API.h"
32 #include "zend_constants.h"
33 #include "zend_execute.h"
34 #include "zend_vm.h"
35
36 /* we use "jmp_hitlist" to avoid infinity loops during jmp optimization */
37 #define CHECK_JMP(target, label) \
38 for (i=0; i<jmp_hitlist_count; i++) { \
39 if (jmp_hitlist[i] == ZEND_OP1_JMP_ADDR(target)) { \
40 goto label; \
41 } \
42 } \
43 jmp_hitlist[jmp_hitlist_count++] = ZEND_OP1_JMP_ADDR(target);
44
45 #define CHECK_JMP2(target, label) \
46 for (i=0; i<jmp_hitlist_count; i++) { \
47 if (jmp_hitlist[i] == ZEND_OP2_JMP_ADDR(target)) { \
48 goto label; \
49 } \
50 } \
51 jmp_hitlist[jmp_hitlist_count++] = ZEND_OP2_JMP_ADDR(target);
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 = op_array->opcodes + op_array->last;
57 zend_op **jmp_hitlist;
58 int jmp_hitlist_count;
59 int i;
60 uint32_t opline_num = 0;
61 ALLOCA_FLAG(use_heap);
62
63 jmp_hitlist = (zend_op**)do_alloca(sizeof(zend_op*)*op_array->last, use_heap);
64 opline = op_array->opcodes;
65
66 while (opline < end) {
67 jmp_hitlist_count = 0;
68
69 switch (opline->opcode) {
70 case ZEND_ADD:
71 case ZEND_SUB:
72 case ZEND_MUL:
73 case ZEND_DIV:
74 case ZEND_MOD:
75 case ZEND_POW:
76 case ZEND_CONCAT:
77 case ZEND_SL:
78 case ZEND_SR:
79 case ZEND_BW_OR:
80 case ZEND_BW_AND:
81 case ZEND_BW_XOR:
82 {
83 zend_op *next_opline = opline + 1;
84
85 while (next_opline < end && next_opline->opcode == ZEND_NOP) {
86 ++next_opline;
87 }
88
89 if (next_opline >= end || next_opline->opcode != ZEND_ASSIGN) {
90 break;
91 }
92
93 /* change $i=expr+$i to $i=$i+expr so that the following optimization
94 * works on it. Only do this if we are ignoring operator overloading,
95 * as operand order might be significant otherwise. */
96 if ((ctx->optimization_level & ZEND_OPTIMIZER_IGNORE_OVERLOADING)
97 && (opline->op2_type & (IS_VAR | IS_CV))
98 && opline->op2.var == next_opline->op1.var &&
99 (opline->opcode == ZEND_ADD ||
100 opline->opcode == ZEND_MUL ||
101 opline->opcode == ZEND_BW_OR ||
102 opline->opcode == ZEND_BW_AND ||
103 opline->opcode == ZEND_BW_XOR)) {
104 zend_uchar tmp_type = opline->op1_type;
105 znode_op tmp = opline->op1;
106
107 if (opline->opcode != ZEND_ADD
108 || (opline->op1_type == IS_CONST
109 && Z_TYPE(ZEND_OP1_LITERAL(opline)) != IS_ARRAY)) {
110 /* protection from array add: $a = array + $a is not commutative! */
111 COPY_NODE(opline->op1, opline->op2);
112 COPY_NODE(opline->op2, tmp);
113 }
114 }
115
116 if ((opline->op1_type & (IS_VAR | IS_CV))
117 && opline->op1.var == next_opline->op1.var
118 && opline->op1_type == next_opline->op1_type) {
119 switch (opline->opcode) {
120 case ZEND_ADD:
121 opline->opcode = ZEND_ASSIGN_ADD;
122 break;
123 case ZEND_SUB:
124 opline->opcode = ZEND_ASSIGN_SUB;
125 break;
126 case ZEND_MUL:
127 opline->opcode = ZEND_ASSIGN_MUL;
128 break;
129 case ZEND_DIV:
130 opline->opcode = ZEND_ASSIGN_DIV;
131 break;
132 case ZEND_MOD:
133 opline->opcode = ZEND_ASSIGN_MOD;
134 break;
135 case ZEND_POW:
136 opline->opcode = ZEND_ASSIGN_POW;
137 break;
138 case ZEND_CONCAT:
139 opline->opcode = ZEND_ASSIGN_CONCAT;
140 break;
141 case ZEND_SL:
142 opline->opcode = ZEND_ASSIGN_SL;
143 break;
144 case ZEND_SR:
145 opline->opcode = ZEND_ASSIGN_SR;
146 break;
147 case ZEND_BW_OR:
148 opline->opcode = ZEND_ASSIGN_BW_OR;
149 break;
150 case ZEND_BW_AND:
151 opline->opcode = ZEND_ASSIGN_BW_AND;
152 break;
153 case ZEND_BW_XOR:
154 opline->opcode = ZEND_ASSIGN_BW_XOR;
155 break;
156 }
157 COPY_NODE(opline->result, next_opline->result);
158 MAKE_NOP(next_opline);
159 opline++;
160 opline_num++;
161 }
162 }
163 break;
164
165 case ZEND_JMP:
166 if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
167 break;
168 }
169
170 /* convert L: JMP L+1 to NOP */
171 if (ZEND_OP1_JMP_ADDR(opline) == opline + 1) {
172 MAKE_NOP(opline);
173 goto done_jmp_optimization;
174 }
175
176 /* convert JMP L1 ... L1: JMP L2 to JMP L2 .. L1: JMP L2 */
177 while (ZEND_OP1_JMP_ADDR(opline) < end
178 && ZEND_OP1_JMP_ADDR(opline)->opcode == ZEND_JMP) {
179 zend_op *target = ZEND_OP1_JMP_ADDR(opline);
180 CHECK_JMP(target, done_jmp_optimization);
181 ZEND_SET_OP_JMP_ADDR(opline, opline->op1, ZEND_OP1_JMP_ADDR(target));
182 }
183 break;
184
185 case ZEND_JMP_SET:
186 case ZEND_COALESCE:
187 if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
188 break;
189 }
190
191 while (ZEND_OP2_JMP_ADDR(opline) < end) {
192 zend_op *target = ZEND_OP2_JMP_ADDR(opline);
193 if (target->opcode == ZEND_JMP) {
194 ZEND_SET_OP_JMP_ADDR(opline, opline->op2, ZEND_OP1_JMP_ADDR(target));
195 } else {
196 break;
197 }
198 }
199 break;
200 case ZEND_JMPZ:
201 case ZEND_JMPNZ:
202 if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
203 break;
204 }
205
206 while (ZEND_OP2_JMP_ADDR(opline) < end) {
207 zend_op *target = ZEND_OP2_JMP_ADDR(opline);
208
209 if (target->opcode == ZEND_JMP) {
210 /* plain JMP */
211 /* JMPZ(X,L1), L1: JMP(L2) => JMPZ(X,L2), L1: JMP(L2) */
212 CHECK_JMP(target, done_jmp_optimization);
213 ZEND_SET_OP_JMP_ADDR(opline, opline->op2, ZEND_OP1_JMP_ADDR(target));
214 } else if (target->opcode == opline->opcode &&
215 SAME_VAR(opline->op1, target->op1)) {
216 /* same opcode and same var as this opcode */
217 /* JMPZ(X,L1), L1: JMPZ(X,L2) => JMPZ(X,L2), L1: JMPZ(X,L2) */
218 CHECK_JMP2(target, done_jmp_optimization);
219 ZEND_SET_OP_JMP_ADDR(opline, opline->op2, ZEND_OP2_JMP_ADDR(target));
220 } else if (target->opcode == opline->opcode + 3 &&
221 SAME_VAR(opline->op1, target->op1)) {
222 /* convert JMPZ(X,L1), L1: T JMPZ_EX(X,L2) to
223 T = JMPZ_EX(X, L2) */
224 ZEND_SET_OP_JMP_ADDR(opline, opline->op2, ZEND_OP2_JMP_ADDR(target));
225 opline->opcode += 3;
226 COPY_NODE(opline->result, target->result);
227 break;
228 } else if (target->opcode == INV_COND(opline->opcode) &&
229 SAME_VAR(opline->op1, target->op1)) {
230 /* convert JMPZ(X,L1), L1: JMPNZ(X,L2) to
231 JMPZ(X,L1+1) */
232 ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target + 1);
233 break;
234 } else if (target->opcode == INV_COND_EX(opline->opcode) &&
235 SAME_VAR(opline->op1, target->op1)) {
236 /* convert JMPZ(X,L1), L1: T = JMPNZ_EX(X,L2) to
237 T = JMPZ_EX(X,L1+1) */
238 ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target + 1);
239 opline->opcode += 3;
240 COPY_NODE(opline->result, target->result);
241 break;
242 } else {
243 break;
244 }
245 }
246 break;
247
248 case ZEND_JMPZ_EX:
249 case ZEND_JMPNZ_EX: {
250 zend_uchar T_type = opline->result_type;
251 znode_op T = opline->result;
252
253 if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
254 break;
255 }
256
257 /* convert L: T = JMPZ_EX X,L+1 to T = BOOL(X) */
258 /* convert L: T = JMPZ_EX T,L+1 to NOP */
259 if (ZEND_OP2_JMP_ADDR(opline) == opline + 1) {
260 if (opline->op1.var == opline->result.var) {
261 MAKE_NOP(opline);
262 } else {
263 opline->opcode = ZEND_BOOL;
264 SET_UNUSED(opline->op2);
265 }
266 goto done_jmp_optimization;
267 }
268
269 while (ZEND_OP2_JMP_ADDR(opline) < end) {
270 zend_op *target = ZEND_OP2_JMP_ADDR(opline);
271
272 if (target->opcode == opline->opcode-3 &&
273 SAME_VAR(target->op1, T)) {
274 /* convert T=JMPZ_EX(X,L1), L1: JMPZ(T,L2) to
275 JMPZ_EX(X,L2) */
276 CHECK_JMP2(target, continue_jmp_ex_optimization);
277 ZEND_SET_OP_JMP_ADDR(opline, opline->op2, ZEND_OP2_JMP_ADDR(target));
278 } else if (target->opcode == opline->opcode &&
279 SAME_VAR(target->op1, T) &&
280 SAME_VAR(target->result, T)) {
281 /* convert T=JMPZ_EX(X,L1), L1: T=JMPZ_EX(T,L2) to
282 JMPZ_EX(X,L2) */
283 CHECK_JMP2(target, continue_jmp_ex_optimization);
284 ZEND_SET_OP_JMP_ADDR(opline, opline->op2, ZEND_OP2_JMP_ADDR(target));
285 } else if (target->opcode == ZEND_JMPZNZ &&
286 SAME_VAR(target->op1, T)) {
287 /* Check for JMPZNZ with same cond variable */
288 zend_op *new_target;
289
290 CHECK_JMP2(target, continue_jmp_ex_optimization);
291 if (opline->opcode == ZEND_JMPZ_EX) {
292 new_target = ZEND_OP2_JMP_ADDR(target);
293 } else {
294 /* JMPNZ_EX */
295 new_target = ZEND_OFFSET_TO_OPLINE(target, target->extended_value);
296 }
297 ZEND_SET_OP_JMP_ADDR(opline, opline->op2, new_target);
298 } else if ((target->opcode == INV_EX_COND_EX(opline->opcode) ||
299 target->opcode == INV_EX_COND(opline->opcode)) &&
300 SAME_VAR(opline->op1, target->op1)) {
301 /* convert JMPZ_EX(X,L1), L1: JMPNZ_EX(X,L2) to
302 JMPZ_EX(X,L1+1) */
303 ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target + 1);
304 break;
305 } else if (target->opcode == INV_EX_COND(opline->opcode) &&
306 SAME_VAR(target->op1, T)) {
307 /* convert T=JMPZ_EX(X,L1), L1: JMPNZ(T,L2) to
308 JMPZ_EX(X,L1+1) */
309 ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target + 1);
310 break;
311 } else if (target->opcode == INV_EX_COND_EX(opline->opcode) &&
312 SAME_VAR(target->op1, T) &&
313 SAME_VAR(target->result, T)) {
314 /* convert T=JMPZ_EX(X,L1), L1: T=JMPNZ_EX(T,L2) to
315 JMPZ_EX(X,L1+1) */
316 ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target + 1);
317 break;
318 } else if (target->opcode == ZEND_BOOL &&
319 SAME_VAR(opline->result, target->op1)) {
320 /* convert Y = JMPZ_EX(X,L1), L1: Z = BOOL(Y) to
321 Z = JMPZ_EX(X,L1+1) */
322 opline->result.var = target->result.var;
323 ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target + 1);
324 break;
325 } else {
326 break;
327 }
328 } /* while */
329 continue_jmp_ex_optimization:
330 break;
331 #if 0
332 /* If Ti = JMPZ_EX(X, L) and Ti is not used, convert to JMPZ(X, L) */
333 {
334 zend_op *op;
335 for(op = opline+1; op<end; op++) {
336 if(op->result_type == IS_TMP_VAR &&
337 op->result.var == opline->result.var) {
338 break; /* can pass to part 2 */
339 }
340
341 if(op->opcode == ZEND_JMP ||
342 op->opcode == ZEND_JMPZ ||
343 op->opcode == ZEND_JMPZ_EX ||
344 op->opcode == ZEND_JMPNZ ||
345 op->opcode == ZEND_JMPNZ_EX ||
346 op->opcode == ZEND_JMPZNZ ||
347 op->opcode == ZEND_CASE ||
348 op->opcode == ZEND_RETURN ||
349 op->opcode == ZEND_RETURN_BY_REF ||
350 op->opcode == ZEND_FAST_RET ||
351 op->opcode == ZEND_FE_FETCH_R ||
352 op->opcode == ZEND_FE_FETCH_RW ||
353 op->opcode == ZEND_EXIT) {
354 break;
355 }
356
357 if(op->op1_type == IS_TMP_VAR &&
358 op->op1.var == opline->result.var) {
359 goto done_jmp_optimization;
360 }
361
362 if(op->op2_type == IS_TMP_VAR &&
363 op->op2.var == opline->result.var) {
364 goto done_jmp_optimization;
365 }
366 } /* for */
367
368 for(op = &op_array->opcodes[opline->op2.opline_num]; op<end; op++) {
369
370 if(op->result_type == IS_TMP_VAR &&
371 op->result.var == opline->result.var) {
372 break; /* can pass to optimization */
373 }
374
375 if(op->opcode == ZEND_JMP ||
376 op->opcode == ZEND_JMPZ ||
377 op->opcode == ZEND_JMPZ_EX ||
378 op->opcode == ZEND_JMPNZ ||
379 op->opcode == ZEND_JMPNZ_EX ||
380 op->opcode == ZEND_JMPZNZ ||
381 op->opcode == ZEND_CASE ||
382 op->opcode == ZEND_RETURN ||
383 op->opcode == ZEND_RETURN_BY_REF ||
384 op->opcode == ZEND_FAST_RET ||
385 op->opcode == ZEND_FE_FETCH_R ||
386 op->opcode == ZEND_FE_FETCH_RW ||
387 op->opcode == ZEND_EXIT) {
388 break;
389 }
390
391 if(op->op1_type == IS_TMP_VAR &&
392 op->op1.var == opline->result.var) {
393 goto done_jmp_optimization;
394 }
395
396 if(op->op2_type == IS_TMP_VAR &&
397 op->op2.var == opline->result.var) {
398 goto done_jmp_optimization;
399 }
400 }
401
402 opline->opcode = opline->opcode-3; /* JMP_EX -> JMP */
403 SET_UNUSED(opline->result);
404 break;
405 }
406 #endif
407 }
408 break;
409
410 case ZEND_JMPZNZ:
411 if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
412 break;
413 }
414
415 /* JMPZNZ(X,L1,L2), L1: JMP(L3) => JMPZNZ(X,L3,L2), L1: JMP(L3) */
416 while (ZEND_OP2_JMP_ADDR(opline) < end
417 && ZEND_OP2_JMP_ADDR(opline)->opcode == ZEND_JMP) {
418 zend_op *target = ZEND_OP2_JMP_ADDR(opline);
419 CHECK_JMP(target, continue_jmpznz_optimization);
420 ZEND_SET_OP_JMP_ADDR(opline, opline->op2, ZEND_OP1_JMP_ADDR(target));
421 }
422 continue_jmpznz_optimization:
423 /* JMPZNZ(X,L1,L2), L2: JMP(L3) => JMPZNZ(X,L1,L3), L2: JMP(L3) */
424 while (ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value) < end
425 && ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value)->opcode == ZEND_JMP) {
426 zend_op *target = ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value);
427 CHECK_JMP(target, done_jmp_optimization);
428 opline->extended_value = ZEND_OPLINE_TO_OFFSET(opline, ZEND_OP1_JMP_ADDR(target));
429 }
430 break;
431
432 case ZEND_POST_INC_OBJ:
433 case ZEND_POST_DEC_OBJ:
434 case ZEND_POST_INC:
435 case ZEND_POST_DEC: {
436 /* POST_INC, FREE => PRE_INC */
437 zend_op *next_op = opline + 1;
438
439 if (next_op >= end) {
440 break;
441 }
442 if (next_op->opcode == ZEND_FREE &&
443 next_op->op1.var == opline->result.var) {
444 MAKE_NOP(next_op);
445 opline->opcode -= 2;
446 opline->result_type = IS_UNUSED;
447 }
448 }
449 break;
450 }
451 done_jmp_optimization:
452 opline++;
453 opline_num++;
454 }
455 free_alloca(jmp_hitlist, use_heap);
456 }
457