xref: /PHP-7.1/ext/opcache/Optimizer/zend_dfg.c (revision ccd4716e)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine, DFG - Data Flow Graph                                   |
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: Dmitry Stogov <dmitry@zend.com>                             |
16    +----------------------------------------------------------------------+
17 */
18 
19 #include "php.h"
20 #include "zend_compile.h"
21 #include "zend_dfg.h"
22 
zend_build_dfg(const zend_op_array * op_array,const zend_cfg * cfg,zend_dfg * dfg,uint32_t build_flags)23 int zend_build_dfg(const zend_op_array *op_array, const zend_cfg *cfg, zend_dfg *dfg, uint32_t build_flags) /* {{{ */
24 {
25 	int set_size;
26 	zend_basic_block *blocks = cfg->blocks;
27 	int blocks_count = cfg->blocks_count;
28 	zend_bitset tmp, def, use, in, out;
29 	int k;
30 	uint32_t var_num;
31 	int j;
32 
33 	set_size = dfg->size;
34 	tmp = dfg->tmp;
35 	def = dfg->def;
36 	use = dfg->use;
37 	in  = dfg->in;
38 	out = dfg->out;
39 
40 	/* Collect "def" and "use" sets */
41 	for (j = 0; j < blocks_count; j++) {
42 		zend_op *opline, *end;
43 		if ((blocks[j].flags & ZEND_BB_REACHABLE) == 0) {
44 			continue;
45 		}
46 
47 		opline = op_array->opcodes + blocks[j].start;
48 		end = opline + blocks[j].len;
49 		for (; opline < end; opline++) {
50 			if (opline->opcode != ZEND_OP_DATA) {
51 				zend_op *next = opline + 1;
52 				if (next < end && next->opcode == ZEND_OP_DATA) {
53 					if (next->op1_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
54 						var_num = EX_VAR_TO_NUM(next->op1.var);
55 						if (!DFG_ISSET(def, set_size, j, var_num)) {
56 							DFG_SET(use, set_size, j, var_num);
57 						}
58 					}
59 					if (next->op2_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
60 						var_num = EX_VAR_TO_NUM(next->op2.var);
61 						if (!DFG_ISSET(def, set_size, j, var_num)) {
62 							DFG_SET(use, set_size, j, var_num);
63 						}
64 					}
65 				}
66 				if (opline->op1_type == IS_CV) {
67 					var_num = EX_VAR_TO_NUM(opline->op1.var);
68 					switch (opline->opcode) {
69 					case ZEND_ADD_ARRAY_ELEMENT:
70 					case ZEND_INIT_ARRAY:
71 						if ((build_flags & ZEND_SSA_RC_INFERENCE)
72 								|| (opline->extended_value & ZEND_ARRAY_ELEMENT_REF)) {
73 							goto op1_def;
74 						}
75 						goto op1_use;
76 					case ZEND_FE_RESET_R:
77 					case ZEND_SEND_VAR:
78 					case ZEND_CAST:
79 					case ZEND_QM_ASSIGN:
80 					case ZEND_JMP_SET:
81 					case ZEND_COALESCE:
82 						if (build_flags & ZEND_SSA_RC_INFERENCE) {
83 							goto op1_def;
84 						}
85 						goto op1_use;
86 					case ZEND_YIELD:
87 						if ((build_flags & ZEND_SSA_RC_INFERENCE)
88 								|| (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
89 							goto op1_def;
90 						}
91 						goto op1_use;
92 					case ZEND_UNSET_VAR:
93 						ZEND_ASSERT(opline->extended_value & ZEND_QUICK_SET);
94 						/* break missing intentionally */
95 					case ZEND_ASSIGN:
96 					case ZEND_ASSIGN_REF:
97 					case ZEND_BIND_GLOBAL:
98 					case ZEND_BIND_STATIC:
99 					case ZEND_SEND_VAR_EX:
100 					case ZEND_SEND_REF:
101 					case ZEND_SEND_VAR_NO_REF:
102 					case ZEND_SEND_VAR_NO_REF_EX:
103 					case ZEND_FE_RESET_RW:
104 					case ZEND_ASSIGN_ADD:
105 					case ZEND_ASSIGN_SUB:
106 					case ZEND_ASSIGN_MUL:
107 					case ZEND_ASSIGN_DIV:
108 					case ZEND_ASSIGN_MOD:
109 					case ZEND_ASSIGN_SL:
110 					case ZEND_ASSIGN_SR:
111 					case ZEND_ASSIGN_CONCAT:
112 					case ZEND_ASSIGN_BW_OR:
113 					case ZEND_ASSIGN_BW_AND:
114 					case ZEND_ASSIGN_BW_XOR:
115 					case ZEND_ASSIGN_POW:
116 					case ZEND_PRE_INC:
117 					case ZEND_PRE_DEC:
118 					case ZEND_POST_INC:
119 					case ZEND_POST_DEC:
120 					case ZEND_ASSIGN_DIM:
121 					case ZEND_ASSIGN_OBJ:
122 					case ZEND_UNSET_DIM:
123 					case ZEND_UNSET_OBJ:
124 					case ZEND_FETCH_DIM_W:
125 					case ZEND_FETCH_DIM_RW:
126 					case ZEND_FETCH_DIM_FUNC_ARG:
127 					case ZEND_FETCH_DIM_UNSET:
128 					case ZEND_FETCH_OBJ_W:
129 					case ZEND_FETCH_OBJ_RW:
130 					case ZEND_FETCH_OBJ_FUNC_ARG:
131 					case ZEND_FETCH_OBJ_UNSET:
132 					case ZEND_VERIFY_RETURN_TYPE:
133 op1_def:
134 						/* `def` always come along with dtor or separation,
135 						 * thus the origin var info might be also `use`d in the feature(CG) */
136 						DFG_SET(use, set_size, j, var_num);
137 						DFG_SET(def, set_size, j, var_num);
138 						break;
139 					default:
140 op1_use:
141 						if (!DFG_ISSET(def, set_size, j, var_num)) {
142 							DFG_SET(use, set_size, j, var_num);
143 						}
144 					}
145 				} else if (opline->op1_type & (IS_VAR|IS_TMP_VAR)) {
146 					var_num = EX_VAR_TO_NUM(opline->op1.var);
147 					if (!DFG_ISSET(def, set_size, j, var_num)) {
148 						DFG_SET(use, set_size, j, var_num);
149 					}
150 					if (opline->opcode == ZEND_VERIFY_RETURN_TYPE) {
151 						DFG_SET(def, set_size, j, var_num);
152 					}
153 				}
154 				if (opline->op2_type == IS_CV) {
155 					var_num = EX_VAR_TO_NUM(opline->op2.var);
156 					switch (opline->opcode) {
157 						case ZEND_ASSIGN:
158 							if (build_flags & ZEND_SSA_RC_INFERENCE) {
159 								goto op2_def;
160 							}
161 							goto op2_use;
162 						case ZEND_BIND_LEXICAL:
163 							if ((build_flags & ZEND_SSA_RC_INFERENCE) || opline->extended_value) {
164 								goto op2_def;
165 							}
166 							goto op2_use;
167 						case ZEND_ASSIGN_REF:
168 						case ZEND_FE_FETCH_R:
169 						case ZEND_FE_FETCH_RW:
170 op2_def:
171 							// FIXME: include into "use" too ...?
172 							DFG_SET(use, set_size, j, var_num);
173 							DFG_SET(def, set_size, j, var_num);
174 							break;
175 						default:
176 op2_use:
177 							if (!DFG_ISSET(def, set_size, j, var_num)) {
178 								DFG_SET(use, set_size, j, var_num);
179 							}
180 							break;
181 					}
182 				} else if (opline->op2_type & (IS_VAR|IS_TMP_VAR)) {
183 					var_num = EX_VAR_TO_NUM(opline->op2.var);
184 					if (opline->opcode == ZEND_FE_FETCH_R || opline->opcode == ZEND_FE_FETCH_RW) {
185 						DFG_SET(def, set_size, j, var_num);
186 					} else {
187 						if (!DFG_ISSET(def, set_size, j, var_num)) {
188 							DFG_SET(use, set_size, j, var_num);
189 						}
190 					}
191 				}
192 				if (opline->result_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
193 					var_num = EX_VAR_TO_NUM(opline->result.var);
194 					DFG_SET(def, set_size, j, var_num);
195 				}
196 			}
197 		}
198 	}
199 
200 	/* Calculate "in" and "out" sets */
201 	{
202 		uint32_t worklist_len = zend_bitset_len(blocks_count);
203 		zend_bitset worklist;
204 		ALLOCA_FLAG(use_heap);
205 		worklist = ZEND_BITSET_ALLOCA(worklist_len, use_heap);
206 		memset(worklist, 0, worklist_len * ZEND_BITSET_ELM_SIZE);
207 		for (j = 0; j < blocks_count; j++) {
208 			zend_bitset_incl(worklist, j);
209 		}
210 		while (!zend_bitset_empty(worklist, worklist_len)) {
211 			/* We use the last block on the worklist, because predecessors tend to be located
212 			 * before the succeeding block, so this converges faster. */
213 			j = zend_bitset_last(worklist, worklist_len);
214 			zend_bitset_excl(worklist, j);
215 
216 			if ((blocks[j].flags & ZEND_BB_REACHABLE) == 0) {
217 				continue;
218 			}
219 			if (blocks[j].successors[0] >= 0) {
220 				zend_bitset_copy(DFG_BITSET(out, set_size, j), DFG_BITSET(in, set_size, blocks[j].successors[0]), set_size);
221 				if (blocks[j].successors[1] >= 0) {
222 					zend_bitset_union(DFG_BITSET(out, set_size, j), DFG_BITSET(in, set_size, blocks[j].successors[1]), set_size);
223 				}
224 			} else {
225 				zend_bitset_clear(DFG_BITSET(out, set_size, j), set_size);
226 			}
227 			zend_bitset_union_with_difference(tmp, DFG_BITSET(use, set_size, j), DFG_BITSET(out, set_size, j), DFG_BITSET(def, set_size, j), set_size);
228 			if (!zend_bitset_equal(DFG_BITSET(in, set_size, j), tmp, set_size)) {
229 				zend_bitset_copy(DFG_BITSET(in, set_size, j), tmp, set_size);
230 
231 				/* Add predecessors of changed block to worklist */
232 				{
233 					int *predecessors = &cfg->predecessors[blocks[j].predecessor_offset];
234 					for (k = 0; k < blocks[j].predecessors_count; k++) {
235 						zend_bitset_incl(worklist, predecessors[k]);
236 					}
237 				}
238 			}
239 		}
240 
241 		free_alloca(worklist, use_heap);
242 	}
243 
244 	return SUCCESS;
245 }
246 /* }}} */
247 
248 /*
249  * Local variables:
250  * tab-width: 4
251  * c-basic-offset: 4
252  * indent-tabs-mode: t
253  * End:
254  */
255