xref: /PHP-7.2/ext/opcache/Optimizer/zend_dfg.c (revision 7a7ec01a)
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_CV:
93 					case ZEND_ASSIGN:
94 					case ZEND_ASSIGN_REF:
95 					case ZEND_BIND_GLOBAL:
96 					case ZEND_BIND_STATIC:
97 					case ZEND_SEND_VAR_EX:
98 					case ZEND_SEND_REF:
99 					case ZEND_SEND_VAR_NO_REF:
100 					case ZEND_SEND_VAR_NO_REF_EX:
101 					case ZEND_FE_RESET_RW:
102 					case ZEND_ASSIGN_ADD:
103 					case ZEND_ASSIGN_SUB:
104 					case ZEND_ASSIGN_MUL:
105 					case ZEND_ASSIGN_DIV:
106 					case ZEND_ASSIGN_MOD:
107 					case ZEND_ASSIGN_SL:
108 					case ZEND_ASSIGN_SR:
109 					case ZEND_ASSIGN_CONCAT:
110 					case ZEND_ASSIGN_BW_OR:
111 					case ZEND_ASSIGN_BW_AND:
112 					case ZEND_ASSIGN_BW_XOR:
113 					case ZEND_ASSIGN_POW:
114 					case ZEND_PRE_INC:
115 					case ZEND_PRE_DEC:
116 					case ZEND_POST_INC:
117 					case ZEND_POST_DEC:
118 					case ZEND_ASSIGN_DIM:
119 					case ZEND_ASSIGN_OBJ:
120 					case ZEND_UNSET_DIM:
121 					case ZEND_UNSET_OBJ:
122 					case ZEND_FETCH_DIM_W:
123 					case ZEND_FETCH_DIM_RW:
124 					case ZEND_FETCH_DIM_FUNC_ARG:
125 					case ZEND_FETCH_DIM_UNSET:
126 					case ZEND_FETCH_OBJ_W:
127 					case ZEND_FETCH_OBJ_RW:
128 					case ZEND_FETCH_OBJ_FUNC_ARG:
129 					case ZEND_FETCH_OBJ_UNSET:
130 					case ZEND_VERIFY_RETURN_TYPE:
131 op1_def:
132 						/* `def` always come along with dtor or separation,
133 						 * thus the origin var info might be also `use`d in the feature(CG) */
134 						DFG_SET(use, set_size, j, var_num);
135 						DFG_SET(def, set_size, j, var_num);
136 						break;
137 					default:
138 op1_use:
139 						if (!DFG_ISSET(def, set_size, j, var_num)) {
140 							DFG_SET(use, set_size, j, var_num);
141 						}
142 					}
143 				} else if (opline->op1_type & (IS_VAR|IS_TMP_VAR)) {
144 					var_num = EX_VAR_TO_NUM(opline->op1.var);
145 					if (!DFG_ISSET(def, set_size, j, var_num)) {
146 						DFG_SET(use, set_size, j, var_num);
147 					}
148 					if (opline->opcode == ZEND_VERIFY_RETURN_TYPE) {
149 						DFG_SET(def, set_size, j, var_num);
150 					}
151 				}
152 				if (opline->op2_type == IS_CV) {
153 					var_num = EX_VAR_TO_NUM(opline->op2.var);
154 					switch (opline->opcode) {
155 						case ZEND_ASSIGN:
156 							if (build_flags & ZEND_SSA_RC_INFERENCE) {
157 								goto op2_def;
158 							}
159 							goto op2_use;
160 						case ZEND_BIND_LEXICAL:
161 							if ((build_flags & ZEND_SSA_RC_INFERENCE) || opline->extended_value) {
162 								goto op2_def;
163 							}
164 							goto op2_use;
165 						case ZEND_ASSIGN_REF:
166 						case ZEND_FE_FETCH_R:
167 						case ZEND_FE_FETCH_RW:
168 op2_def:
169 							// FIXME: include into "use" too ...?
170 							DFG_SET(use, set_size, j, var_num);
171 							DFG_SET(def, set_size, j, var_num);
172 							break;
173 						default:
174 op2_use:
175 							if (!DFG_ISSET(def, set_size, j, var_num)) {
176 								DFG_SET(use, set_size, j, var_num);
177 							}
178 							break;
179 					}
180 				} else if (opline->op2_type & (IS_VAR|IS_TMP_VAR)) {
181 					var_num = EX_VAR_TO_NUM(opline->op2.var);
182 					if (opline->opcode == ZEND_FE_FETCH_R || opline->opcode == ZEND_FE_FETCH_RW) {
183 						DFG_SET(def, set_size, j, var_num);
184 					} else {
185 						if (!DFG_ISSET(def, set_size, j, var_num)) {
186 							DFG_SET(use, set_size, j, var_num);
187 						}
188 					}
189 				}
190 				if (opline->result_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
191 					var_num = EX_VAR_TO_NUM(opline->result.var);
192 					if ((build_flags & ZEND_SSA_USE_CV_RESULTS)
193 					 && opline->result_type == IS_CV) {
194 						DFG_SET(use, set_size, j, var_num);
195 					}
196 					DFG_SET(def, set_size, j, var_num);
197 				}
198 			}
199 		}
200 	}
201 
202 	/* Calculate "in" and "out" sets */
203 	{
204 		uint32_t worklist_len = zend_bitset_len(blocks_count);
205 		zend_bitset worklist;
206 		ALLOCA_FLAG(use_heap);
207 		worklist = ZEND_BITSET_ALLOCA(worklist_len, use_heap);
208 		memset(worklist, 0, worklist_len * ZEND_BITSET_ELM_SIZE);
209 		for (j = 0; j < blocks_count; j++) {
210 			zend_bitset_incl(worklist, j);
211 		}
212 		while (!zend_bitset_empty(worklist, worklist_len)) {
213 			/* We use the last block on the worklist, because predecessors tend to be located
214 			 * before the succeeding block, so this converges faster. */
215 			j = zend_bitset_last(worklist, worklist_len);
216 			zend_bitset_excl(worklist, j);
217 
218 			if ((blocks[j].flags & ZEND_BB_REACHABLE) == 0) {
219 				continue;
220 			}
221 			if (blocks[j].successors_count != 0) {
222 				zend_bitset_copy(DFG_BITSET(out, set_size, j), DFG_BITSET(in, set_size, blocks[j].successors[0]), set_size);
223 				for (k = 1; k < blocks[j].successors_count; k++) {
224 					zend_bitset_union(DFG_BITSET(out, set_size, j), DFG_BITSET(in, set_size, blocks[j].successors[k]), set_size);
225 				}
226 			} else {
227 				zend_bitset_clear(DFG_BITSET(out, set_size, j), set_size);
228 			}
229 			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);
230 			if (!zend_bitset_equal(DFG_BITSET(in, set_size, j), tmp, set_size)) {
231 				zend_bitset_copy(DFG_BITSET(in, set_size, j), tmp, set_size);
232 
233 				/* Add predecessors of changed block to worklist */
234 				{
235 					int *predecessors = &cfg->predecessors[blocks[j].predecessor_offset];
236 					for (k = 0; k < blocks[j].predecessors_count; k++) {
237 						zend_bitset_incl(worklist, predecessors[k]);
238 					}
239 				}
240 			}
241 		}
242 
243 		free_alloca(worklist, use_heap);
244 	}
245 
246 	return SUCCESS;
247 }
248 /* }}} */
249 
250 /*
251  * Local variables:
252  * tab-width: 4
253  * c-basic-offset: 4
254  * indent-tabs-mode: t
255  * End:
256  */
257