summaryrefslogtreecommitdiff
path: root/ext/opcache/Optimizer/nop_removal.c
blob: 80da02ce32b8d218a77de9d42d24b8842aca0200 (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
/*
   +----------------------------------------------------------------------+
   | Zend OPcache                                                         |
   +----------------------------------------------------------------------+
   | Copyright (c) 1998-2014 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@zend.com>                                |
   |          Zeev Suraski <zeev@zend.com>                                |
   |          Stanislav Malyshev <stas@zend.com>                          |
   |          Dmitry Stogov <dmitry@zend.com>                             |
   +----------------------------------------------------------------------+
*/

/* pass 10:
 * - remove NOPs
 */

static void nop_removal(zend_op_array *op_array)
{
	zend_op *end, *opline;
	zend_uint new_count, i, shift;
	int j;
	zend_uint *shiftlist;
	ALLOCA_FLAG(use_heap);

	shiftlist = (zend_uint *)DO_ALLOCA(sizeof(zend_uint) * op_array->last);
	i = new_count = shift = 0;
	end = op_array->opcodes + op_array->last;
	for (opline = op_array->opcodes; opline < end; opline++) {

		/* GOTO target is unresolved yet. We can't optimize. */
		if (opline->opcode == ZEND_GOTO &&
			Z_TYPE(ZEND_OP2_LITERAL(opline)) != IS_LONG) {
			/* TODO: in general we can avoid this restriction */
			FREE_ALLOCA(shiftlist);
			return;
		}

		/* Kill JMP-over-NOP-s */
		if (opline->opcode == ZEND_JMP && ZEND_OP1(opline).opline_num > i) {
			/* check if there are only NOPs under the branch */
			zend_op *target = op_array->opcodes + ZEND_OP1(opline).opline_num - 1;

			while (target->opcode == ZEND_NOP) {
				target--;
			}
			if (target == opline) {
				/* only NOPs */
				opline->opcode = ZEND_NOP;
			}
		}

		shiftlist[i++] = shift;
		if (opline->opcode == ZEND_NOP) {
			shift++;
		} else {
			if (shift) {
				op_array->opcodes[new_count] = *opline;
			}
			new_count++;
		}
	}

	if (shift) {
		op_array->last = new_count;
		end = op_array->opcodes + op_array->last;

		/* update JMPs */
		for (opline = op_array->opcodes; opline<end; opline++) {
			switch (opline->opcode) {
				case ZEND_JMP:
				case ZEND_GOTO:
				case ZEND_FAST_CALL:
					ZEND_OP1(opline).opline_num -= shiftlist[ZEND_OP1(opline).opline_num];
					break;
				case ZEND_JMPZ:
				case ZEND_JMPNZ:
				case ZEND_JMPZ_EX:
				case ZEND_JMPNZ_EX:
				case ZEND_FE_FETCH:
				case ZEND_FE_RESET:
				case ZEND_NEW:
				case ZEND_JMP_SET:
				case ZEND_JMP_SET_VAR:
					ZEND_OP2(opline).opline_num -= shiftlist[ZEND_OP2(opline).opline_num];
					break;
				case ZEND_JMPZNZ:
					ZEND_OP2(opline).opline_num -= shiftlist[ZEND_OP2(opline).opline_num];
					opline->extended_value -= shiftlist[opline->extended_value];
					break;
				case ZEND_CATCH:
					opline->extended_value -= shiftlist[opline->extended_value];
					break;
			}
		}

		/* update brk/cont array */
		for (j = 0; j < op_array->last_brk_cont; j++) {
			op_array->brk_cont_array[j].brk -= shiftlist[op_array->brk_cont_array[j].brk];
			op_array->brk_cont_array[j].cont -= shiftlist[op_array->brk_cont_array[j].cont];
			op_array->brk_cont_array[j].start -= shiftlist[op_array->brk_cont_array[j].start];
		}

		/* update try/catch array */
		for (j = 0; j < op_array->last_try_catch; j++) {
			op_array->try_catch_array[j].try_op -= shiftlist[op_array->try_catch_array[j].try_op];
			op_array->try_catch_array[j].catch_op -= shiftlist[op_array->try_catch_array[j].catch_op];
			if (op_array->try_catch_array[j].finally_op) {
				op_array->try_catch_array[j].finally_op -= shiftlist[op_array->try_catch_array[j].finally_op];
				op_array->try_catch_array[j].finally_end -= shiftlist[op_array->try_catch_array[j].finally_end];
			}
		}

		/* update early binding list */
		if (op_array->early_binding != (zend_uint)-1) {
			zend_uint *opline_num = &op_array->early_binding;

			do {
				*opline_num -= shiftlist[*opline_num];
				opline_num = &ZEND_RESULT(&op_array->opcodes[*opline_num]).opline_num;
			} while (*opline_num != (zend_uint)-1);
		}
	}
	FREE_ALLOCA(shiftlist);
}