summaryrefslogtreecommitdiff
path: root/ext/opcache/Optimizer/pass3.c
blob: f98c41848c243551e93c2e3d53fd2d78b8965155 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
   +----------------------------------------------------------------------+
   | Zend OPcache                                                         |
   +----------------------------------------------------------------------+
   | Copyright (c) The PHP Group                                          |
   +----------------------------------------------------------------------+
   | This source file is subject to version 3.01 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
   | http://www.php.net/license/3_01.txt                                  |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors: Andi Gutmans <andi@php.net>                                 |
   |          Zeev Suraski <zeev@php.net>                                 |
   |          Stanislav Malyshev <stas@zend.com>                          |
   |          Dmitry Stogov <dmitry@php.net>                              |
   +----------------------------------------------------------------------+
*/

/* pass 3: (Jump optimization)
 * - optimize series of JMPs
 */

#include "php.h"
#include "Optimizer/zend_optimizer.h"
#include "Optimizer/zend_optimizer_internal.h"
#include "zend_API.h"
#include "zend_constants.h"
#include "zend_execute.h"
#include "zend_vm.h"

/* we use "jmp_hitlist" to avoid infinity loops during jmp optimization */
static zend_always_inline int in_hitlist(zend_op *target, zend_op **jmp_hitlist, int jmp_hitlist_count)
{
	int i;

	for (i = 0; i < jmp_hitlist_count; i++) {
		if (jmp_hitlist[i] == target) {
			return 1;
		}
	}
	return 0;
}

#define CHECK_LOOP(target) \
	if (EXPECTED(!in_hitlist(target, jmp_hitlist, jmp_hitlist_count))) { \
		jmp_hitlist[jmp_hitlist_count++] = target;	\
	} else { \
		break; \
	}

void zend_optimizer_pass3(zend_op_array *op_array, zend_optimizer_ctx *ctx)
{
	zend_op *opline;
	zend_op *end;
	zend_op *target;
	zend_op **jmp_hitlist;
	int jmp_hitlist_count;
	ALLOCA_FLAG(use_heap);

	jmp_hitlist = (zend_op**)do_alloca(sizeof(zend_op*)*op_array->last, use_heap);
	opline = op_array->opcodes;
	end =  opline + op_array->last;

	while (opline < end) {

		switch (opline->opcode) {
			case ZEND_JMP:
				jmp_hitlist_count = 0;

				target = ZEND_OP1_JMP_ADDR(opline);
				while (1) {
					if (target->opcode == ZEND_JMP) {
						/* convert JMP L1 ... L1: JMP L2 to JMP L2 .. L1: JMP L2 */
						target = ZEND_OP1_JMP_ADDR(target);
						CHECK_LOOP(target);
					} else if (target->opcode == ZEND_NOP) {
						target = target + 1;
					} else {
						break;
					}
					ZEND_SET_OP_JMP_ADDR(opline, opline->op1, target);
				}

				if (target == opline + 1) {
					/* convert L: JMP L+1 to NOP */
					MAKE_NOP(opline);
				} else if (target->opcode == ZEND_JMPZNZ) {
					/* JMP L, L: JMPZNZ L1,L2 -> JMPZNZ L1,L2 */
					*opline = *target;
					if (opline->op1_type == IS_CONST) {
						zval zv;
						ZVAL_COPY(&zv, &ZEND_OP1_LITERAL(opline));
						opline->op1.constant = zend_optimizer_add_literal(op_array, &zv);
					}
					goto optimize_jmpznz;
				} else if ((target->opcode == ZEND_RETURN ||
				            target->opcode == ZEND_RETURN_BY_REF ||
				            target->opcode == ZEND_GENERATOR_RETURN ||
				            target->opcode == ZEND_EXIT) &&
				           !(op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
					/* JMP L, L: RETURN to immediate RETURN */
					*opline = *target;
					if (opline->op1_type == IS_CONST) {
						zval zv;
						ZVAL_COPY(&zv, &ZEND_OP1_LITERAL(opline));
						opline->op1.constant = zend_optimizer_add_literal(op_array, &zv);
					}
				} else if (opline > op_array->opcodes &&
				           ((opline-1)->opcode == ZEND_JMPZ ||
				            (opline-1)->opcode == ZEND_JMPNZ)) {
				    if (ZEND_OP2_JMP_ADDR(opline-1) == target) {
						/* JMPZ(X,L1), JMP(L1) -> NOP, JMP(L1) */
						if ((opline-1)->op1_type == IS_CV) {
							(opline-1)->opcode = ZEND_CHECK_VAR;
							(opline-1)->op2.num = 0;
						} else if ((opline-1)->op1_type & (IS_TMP_VAR|IS_VAR)) {
							(opline-1)->opcode = ZEND_FREE;
							(opline-1)->op2.num = 0;
						} else {
							MAKE_NOP(opline-1);
						}
				    } else {
						/* JMPZ(X,L1), JMP(L2) -> JMPZNZ(X,L1,L2) */
						if ((opline-1)->opcode == ZEND_JMPZ) {
							(opline-1)->extended_value = ZEND_OPLINE_TO_OFFSET((opline-1), target);
						} else {
							(opline-1)->extended_value = ZEND_OPLINE_TO_OFFSET((opline-1), ZEND_OP2_JMP_ADDR(opline-1));
							ZEND_SET_OP_JMP_ADDR((opline-1), (opline-1)->op2, target);
						}
						(opline-1)->opcode = ZEND_JMPZNZ;
				    }
				}
				break;

			case ZEND_JMP_SET:
			case ZEND_COALESCE:
				jmp_hitlist_count = 0;

				target = ZEND_OP2_JMP_ADDR(opline);
				while (1) {
					if (target->opcode == ZEND_JMP) {
						target = ZEND_OP1_JMP_ADDR(target);
						CHECK_LOOP(target);
					} else if (target->opcode == ZEND_NOP) {
						target = target + 1;
					} else {
						break;
					}
					ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
				}
				break;

			case ZEND_JMPZ:
			case ZEND_JMPNZ:
				jmp_hitlist_count = 0;

				target = ZEND_OP2_JMP_ADDR(opline);
				while (1) {
					if (target->opcode == ZEND_JMP) {
						/* plain JMP */
						/* JMPZ(X,L1), L1: JMP(L2) => JMPZ(X,L2), L1: JMP(L2) */
						target = ZEND_OP1_JMP_ADDR(target);
						CHECK_LOOP(target);
					} else if (target->opcode == opline->opcode &&
					           SAME_VAR(opline->op1, target->op1)) {
						/* same opcode and same var as this opcode */
						/* JMPZ(X,L1), L1: JMPZ(X,L2) => JMPZ(X,L2), L1: JMPZ(X,L2) */
						target = ZEND_OP2_JMP_ADDR(target);
						CHECK_LOOP(target);
					} else if (target->opcode == INV_COND(opline->opcode) &&
					           SAME_VAR(opline->op1, target->op1)) {
						/* convert JMPZ(X,L1), L1: JMPNZ(X,L2) to
						   JMPZ(X,L1+1) */
						target = target + 1;
					} else if (target->opcode == ZEND_JMPZNZ &&
					           SAME_VAR(opline->op1, target->op1)) {
						target = (opline->opcode == ZEND_JMPZ) ?
							ZEND_OP2_JMP_ADDR(target) :
							ZEND_OFFSET_TO_OPLINE(target, target->extended_value);
						CHECK_LOOP(target);
					} else if (target->opcode == ZEND_NOP) {
						target = target + 1;
					} else {
						break;
					}
					ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
				}

				/* convert L: JMPZ L+1 to NOP */
				if (target == opline + 1) {
					if (opline->op1_type == IS_CV) {
						opline->opcode = ZEND_CHECK_VAR;
						opline->op2.num = 0;
					} else if (opline->op1_type & (IS_TMP_VAR|IS_VAR)) {
						opline->opcode = ZEND_FREE;
						opline->op2.num = 0;
					} else {
						MAKE_NOP(opline);
					}
				}
				break;

			case ZEND_JMPZ_EX:
			case ZEND_JMPNZ_EX:
				jmp_hitlist_count = 0;

				target = ZEND_OP2_JMP_ADDR(opline);
				while (1) {
					if (target->opcode == ZEND_JMP) {
						/* plain JMP */
						/* JMPZ_EX(X,L1), L1: JMP(L2) => JMPZ_EX(X,L2), L1: JMP(L2) */
						target = ZEND_OP1_JMP_ADDR(target);
						CHECK_LOOP(target);
					} else if (target->opcode == opline->opcode-3 &&
					           (SAME_VAR(target->op1, opline->result) ||
					            SAME_VAR(target->op1, opline->op1))) {
						/* convert T=JMPZ_EX(X,L1), L1: JMPZ(T,L2) to
						   JMPZ_EX(X,L2) */
						target = ZEND_OP2_JMP_ADDR(target);
						CHECK_LOOP(target);
					} else if (target->opcode == opline->opcode &&
					           target->result.var == opline->result.var &&
					           (SAME_VAR(target->op1, opline->result) ||
					            SAME_VAR(target->op1, opline->op1))) {
						/* convert T=JMPZ_EX(X,L1), L1: T=JMPZ_EX(T,L2) to
						   JMPZ_EX(X,L2) */
						target = ZEND_OP2_JMP_ADDR(target);
						CHECK_LOOP(target);
					} else if (target->opcode == ZEND_JMPZNZ &&
					           (SAME_VAR(target->op1, opline->result) ||
					            SAME_VAR(target->op1, opline->op1))) {
						/* Check for JMPZNZ with same cond variable */
						target = (opline->opcode == ZEND_JMPZ_EX) ?
							ZEND_OP2_JMP_ADDR(target) :
							ZEND_OFFSET_TO_OPLINE(target, target->extended_value);
						CHECK_LOOP(target);
					} else if (target->opcode == INV_EX_COND(opline->opcode) &&
					           (SAME_VAR(target->op1, opline->result) ||
					            SAME_VAR(target->op1, opline->op1))) {
					   /* convert T=JMPZ_EX(X,L1), L1: JMPNZ(T,L2) to
						  JMPZ_EX(X,L1+1) */
						target = target + 1;
					} else if (target->opcode == INV_EX_COND_EX(opline->opcode) &&
					           target->result.var == opline->result.var &&
					           (SAME_VAR(target->op1, opline->result) ||
					            SAME_VAR(target->op1, opline->op1))) {
					   /* convert T=JMPZ_EX(X,L1), L1: T=JMPNZ_EX(T,L2) to
						  JMPZ_EX(X,L1+1) */
						target = target + 1;
					} else if (target->opcode == ZEND_BOOL &&
					           (SAME_VAR(target->op1, opline->result) ||
					            SAME_VAR(target->op1, opline->op1))) {
						/* convert Y = JMPZ_EX(X,L1), L1: Z = BOOL(Y) to
						   Z = JMPZ_EX(X,L1+1) */

						/* NOTE: This optimization pattern is not safe, but works, */
						/*       because result of JMPZ_EX instruction             */
						/*       is not used on the following path and             */
						/*       should be used once on the branch path.           */
						/*                                                         */
						/*       The pattern works well only if jums processed in  */
						/*       direct order, otherwise it breaks JMPZ_EX         */
						/*       sequences too early.                              */
						opline->result.var = target->result.var;
						target = target + 1;
						CHECK_LOOP(target);
					} else if (target->opcode == ZEND_NOP) {
						target = target + 1;
					} else {
						break;
					}
					ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
				}

				/* convert L: T = JMPZ_EX X,L+1 to T = BOOL(X) */
				if (target == opline + 1) {
					opline->opcode = ZEND_BOOL;
					opline->op2.num = 0;
				}
				break;

			case ZEND_JMPZNZ:
optimize_jmpznz:
				jmp_hitlist_count = 0;
				target = ZEND_OP2_JMP_ADDR(opline);
				while (1) {
					if (target->opcode == ZEND_JMP) {
						/* JMPZNZ(X,L1,L2), L1: JMP(L3) => JMPZNZ(X,L3,L2), L1: JMP(L3) */
						target = ZEND_OP1_JMP_ADDR(target);
						CHECK_LOOP(target);
					} else if ((target->opcode == ZEND_JMPZ || target->opcode == ZEND_JMPZNZ) &&
					           SAME_VAR(target->op1, opline->op1)) {
						/* JMPZNZ(X, L1, L2), L1: JMPZ(X, L3) -> JMPZNZ(X, L3, L2) */
						target = ZEND_OP2_JMP_ADDR(target);
						CHECK_LOOP(target);
					} else if (target->opcode == ZEND_JMPNZ &&
					           SAME_VAR(target->op1, opline->op1)) {
						/* JMPZNZ(X, L1, L2), L1: X = JMPNZ(X, L3) -> JMPZNZ(X, L1+1, L2) */
						target = target + 1;
					} else if (target->opcode == ZEND_NOP) {
						target = target + 1;
					} else {
						break;
					}
					ZEND_SET_OP_JMP_ADDR(opline, opline->op2, target);
				}

				jmp_hitlist_count = 0;
				target = ZEND_OFFSET_TO_OPLINE(opline, opline->extended_value);
				while (1) {
					if (target->opcode == ZEND_JMP) {
						/* JMPZNZ(X,L1,L2), L2: JMP(L3) => JMPZNZ(X,L1,L3), L2: JMP(L3) */
						target = ZEND_OP1_JMP_ADDR(target);
						CHECK_LOOP(target);
					} else if (target->opcode == ZEND_JMPNZ &&
					           SAME_VAR(target->op1, opline->op1)) {
						/* JMPZNZ(X, L1, L2), L1: X = JMPNZ(X, L3) -> JMPZNZ(X, L1+1, L2) */
						target = ZEND_OP2_JMP_ADDR(target);
						CHECK_LOOP(target);
					} else if (target->opcode == ZEND_JMPZ &&
					           SAME_VAR(target->op1, opline->op1)) {
						/* JMPZNZ(X, L1, L2), L1: JMPZ(X, L3) -> JMPZNZ(X, L3, L2) */
						target = target + 1;
					} else if (target->opcode == ZEND_JMPZNZ &&
					           SAME_VAR(target->op1, opline->op1)) {
						/* JMPZNZ(X, L1, L2), L1: JMPZ(X, L3) -> JMPZNZ(X, L3, L2) */
						target = ZEND_OFFSET_TO_OPLINE(target, target->extended_value);
						CHECK_LOOP(target);
					} else if (target->opcode == ZEND_NOP) {
						target = target + 1;
					} else {
						break;
					}
					opline->extended_value = ZEND_OPLINE_TO_OFFSET(opline, target);
				}

				if (ZEND_OP2_JMP_ADDR(opline) == target &&
				    !(opline->op1_type & (IS_VAR|IS_TMP_VAR))) {
					/* JMPZNZ(?,L,L) -> JMP(L) */
					opline->opcode = ZEND_JMP;
					ZEND_SET_OP_JMP_ADDR(opline, opline->op1, target);
					SET_UNUSED(opline->op1);
					SET_UNUSED(opline->op2);
					opline->extended_value = 0;
				}
				/* Don't convert JMPZNZ back to JMPZ/JMPNZ, because the
				   following JMP is not removed yet. */
				break;
		}
		opline++;
	}
	free_alloca(jmp_hitlist, use_heap);
}