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 | 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 #include "php.h"
23 #include "Optimizer/zend_optimizer.h"
24 #include "Optimizer/zend_optimizer_internal.h"
25 #include "zend_API.h"
26 #include "zend_constants.h"
27 #include "zend_execute.h"
28 #include "zend_vm.h"
29 #include "zend_bitset.h"
30
31 #define GET_AVAILABLE_T() \
32 for (i = 0; i < T; i++) { \
33 if (!zend_bitset_in(taken_T, i)) { \
34 break; \
35 } \
36 } \
37 zend_bitset_incl(taken_T, i); \
38 if (i > max) { \
39 max = i; \
40 }
41
zend_optimize_temporary_variables(zend_op_array * op_array,zend_optimizer_ctx * ctx)42 void zend_optimize_temporary_variables(zend_op_array *op_array, zend_optimizer_ctx *ctx)
43 {
44 int T = op_array->T;
45 int offset = op_array->last_var;
46 uint32_t bitset_len;
47 zend_bitset taken_T; /* T index in use */
48 zend_op **start_of_T; /* opline where T is first used */
49 zend_bitset valid_T; /* Is the map_T valid */
50 int *map_T; /* Map's the T to its new index */
51 zend_op *opline, *end;
52 int currT;
53 int i;
54 int max = -1;
55 void *checkpoint = zend_arena_checkpoint(ctx->arena);
56
57 bitset_len = zend_bitset_len(T);
58 taken_T = (zend_bitset) zend_arena_alloc(&ctx->arena, bitset_len * ZEND_BITSET_ELM_SIZE);
59 start_of_T = (zend_op **) zend_arena_alloc(&ctx->arena, T * sizeof(zend_op *));
60 valid_T = (zend_bitset) zend_arena_alloc(&ctx->arena, bitset_len * ZEND_BITSET_ELM_SIZE);
61 map_T = (int *) zend_arena_alloc(&ctx->arena, T * sizeof(int));
62
63 end = op_array->opcodes;
64 opline = &op_array->opcodes[op_array->last - 1];
65
66 /* Find T definition points */
67 while (opline >= end) {
68 if (opline->result_type & (IS_VAR | IS_TMP_VAR)) {
69 start_of_T[VAR_NUM(opline->result.var) - offset] = opline;
70 }
71 opline--;
72 }
73
74 zend_bitset_clear(valid_T, bitset_len);
75 zend_bitset_clear(taken_T, bitset_len);
76
77 end = op_array->opcodes;
78 opline = &op_array->opcodes[op_array->last - 1];
79
80 while (opline >= end) {
81 if ((opline->op1_type & (IS_VAR | IS_TMP_VAR))) {
82 currT = VAR_NUM(opline->op1.var) - offset;
83 if (opline->opcode == ZEND_ROPE_END) {
84 int num = (((opline->extended_value + 1) * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
85 int var;
86
87 var = max;
88 while (var >= 0 && !zend_bitset_in(taken_T, var)) {
89 var--;
90 }
91 max = MAX(max, var + num);
92 var = var + 1;
93 map_T[currT] = var;
94 zend_bitset_incl(valid_T, currT);
95 zend_bitset_incl(taken_T, var);
96 opline->op1.var = NUM_VAR(var + offset);
97 while (num > 1) {
98 num--;
99 zend_bitset_incl(taken_T, var + num);
100 }
101 } else {
102 if (!zend_bitset_in(valid_T, currT)) {
103 int use_new_var = 0;
104
105 /* Code in "finally" blocks may modify temporary variables.
106 * We allocate new temporaries for values that need to
107 * relive FAST_CALLs.
108 */
109 if ((op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) &&
110 (opline->opcode == ZEND_RETURN ||
111 opline->opcode == ZEND_GENERATOR_RETURN ||
112 opline->opcode == ZEND_RETURN_BY_REF ||
113 opline->opcode == ZEND_FREE ||
114 opline->opcode == ZEND_FE_FREE)) {
115 zend_op *curr = opline;
116
117 while (--curr >= end) {
118 if (curr->opcode == ZEND_FAST_CALL) {
119 use_new_var = 1;
120 break;
121 } else if (curr->opcode != ZEND_FREE &&
122 curr->opcode != ZEND_FE_FREE &&
123 curr->opcode != ZEND_VERIFY_RETURN_TYPE &&
124 curr->opcode != ZEND_DISCARD_EXCEPTION) {
125 break;
126 }
127 }
128 }
129 if (use_new_var) {
130 i = ++max;
131 zend_bitset_incl(taken_T, i);
132 } else {
133 GET_AVAILABLE_T();
134 }
135 map_T[currT] = i;
136 zend_bitset_incl(valid_T, currT);
137 }
138 opline->op1.var = NUM_VAR(map_T[currT] + offset);
139 }
140 }
141
142 if ((opline->op2_type & (IS_VAR | IS_TMP_VAR))) {
143 currT = VAR_NUM(opline->op2.var) - offset;
144 if (!zend_bitset_in(valid_T, currT)) {
145 GET_AVAILABLE_T();
146 map_T[currT] = i;
147 zend_bitset_incl(valid_T, currT);
148 }
149 opline->op2.var = NUM_VAR(map_T[currT] + offset);
150 }
151
152 if (opline->result_type & (IS_VAR | IS_TMP_VAR)) {
153 currT = VAR_NUM(opline->result.var) - offset;
154 if (zend_bitset_in(valid_T, currT)) {
155 if (start_of_T[currT] == opline) {
156 /* ZEND_FAST_CALL can not share temporary var with others
157 * since the fast_var could also be set by ZEND_HANDLE_EXCEPTION
158 * which could be ahead of it */
159 if (opline->opcode != ZEND_FAST_CALL) {
160 zend_bitset_excl(taken_T, map_T[currT]);
161 }
162 }
163 opline->result.var = NUM_VAR(map_T[currT] + offset);
164 if (opline->opcode == ZEND_ROPE_INIT) {
165 if (start_of_T[currT] == opline) {
166 uint32_t num = ((opline->extended_value * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
167 while (num > 1) {
168 num--;
169 zend_bitset_excl(taken_T, map_T[currT]+num);
170 }
171 }
172 }
173 } else {
174 /* Code which gets here is using a wrongly built opcode such as RECV() */
175 GET_AVAILABLE_T();
176 map_T[currT] = i;
177 zend_bitset_incl(valid_T, currT);
178 opline->result.var = NUM_VAR(i + offset);
179 }
180 }
181
182 opline--;
183 }
184
185 zend_arena_release(&ctx->arena, checkpoint);
186 op_array->T = max + 1;
187 }
188