xref: /PHP-7.4/ext/opcache/Optimizer/zend_dfg.c (revision 48ca5a1e)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine, DFG - Data Flow Graph                                   |
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: Dmitry Stogov <dmitry@php.net>                              |
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 (next->op1_type == IS_CV && (opline->opcode == ZEND_ASSIGN_OBJ_REF
56 								|| opline->opcode == ZEND_ASSIGN_STATIC_PROP_REF)) {
57 							DFG_SET(use, set_size, j, var_num);
58 							DFG_SET(def, set_size, j, var_num);
59 						} else {
60 							if (!DFG_ISSET(def, set_size, j, var_num)) {
61 								DFG_SET(use, set_size, j, var_num);
62 							}
63 						}
64 					}
65 					if (next->op2_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
66 						var_num = EX_VAR_TO_NUM(next->op2.var);
67 						if (!DFG_ISSET(def, set_size, j, var_num)) {
68 							DFG_SET(use, set_size, j, var_num);
69 						}
70 					}
71 				}
72 				if (opline->op1_type == IS_CV) {
73 					var_num = EX_VAR_TO_NUM(opline->op1.var);
74 					switch (opline->opcode) {
75 					case ZEND_ADD_ARRAY_ELEMENT:
76 					case ZEND_INIT_ARRAY:
77 						if ((build_flags & ZEND_SSA_RC_INFERENCE)
78 								|| (opline->extended_value & ZEND_ARRAY_ELEMENT_REF)) {
79 							goto op1_def;
80 						}
81 						goto op1_use;
82 					case ZEND_FE_RESET_R:
83 					case ZEND_SEND_VAR:
84 					case ZEND_CAST:
85 					case ZEND_QM_ASSIGN:
86 					case ZEND_JMP_SET:
87 					case ZEND_COALESCE:
88 						if (build_flags & ZEND_SSA_RC_INFERENCE) {
89 							goto op1_def;
90 						}
91 						goto op1_use;
92 					case ZEND_YIELD:
93 						if ((build_flags & ZEND_SSA_RC_INFERENCE)
94 								|| (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
95 							goto op1_def;
96 						}
97 						goto op1_use;
98 					case ZEND_UNSET_CV:
99 					case ZEND_ASSIGN:
100 					case ZEND_ASSIGN_REF:
101 					case ZEND_ASSIGN_OBJ_REF:
102 					case ZEND_BIND_GLOBAL:
103 					case ZEND_BIND_STATIC:
104 					case ZEND_SEND_VAR_EX:
105 					case ZEND_SEND_FUNC_ARG:
106 					case ZEND_SEND_REF:
107 					case ZEND_SEND_VAR_NO_REF:
108 					case ZEND_SEND_VAR_NO_REF_EX:
109 					case ZEND_FE_RESET_RW:
110 					case ZEND_ASSIGN_OP:
111 					case ZEND_ASSIGN_DIM_OP:
112 					case ZEND_ASSIGN_OBJ_OP:
113 					case ZEND_ASSIGN_STATIC_PROP_OP:
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_FETCH_LIST_W:
131 					case ZEND_VERIFY_RETURN_TYPE:
132 					case ZEND_PRE_INC_OBJ:
133 					case ZEND_PRE_DEC_OBJ:
134 					case ZEND_POST_INC_OBJ:
135 					case ZEND_POST_DEC_OBJ:
136 op1_def:
137 						/* `def` always come along with dtor or separation,
138 						 * thus the origin var info might be also `use`d in the feature(CG) */
139 						DFG_SET(use, set_size, j, var_num);
140 						DFG_SET(def, set_size, j, var_num);
141 						break;
142 					default:
143 op1_use:
144 						if (!DFG_ISSET(def, set_size, j, var_num)) {
145 							DFG_SET(use, set_size, j, var_num);
146 						}
147 					}
148 				} else if (opline->op1_type & (IS_VAR|IS_TMP_VAR)) {
149 					var_num = EX_VAR_TO_NUM(opline->op1.var);
150 					if (!DFG_ISSET(def, set_size, j, var_num)) {
151 						DFG_SET(use, set_size, j, var_num);
152 					}
153 					if (opline->opcode == ZEND_VERIFY_RETURN_TYPE) {
154 						DFG_SET(def, set_size, j, var_num);
155 					}
156 				}
157 				if (opline->op2_type == IS_CV) {
158 					var_num = EX_VAR_TO_NUM(opline->op2.var);
159 					switch (opline->opcode) {
160 						case ZEND_ASSIGN:
161 							if (build_flags & ZEND_SSA_RC_INFERENCE) {
162 								goto op2_def;
163 							}
164 							goto op2_use;
165 						case ZEND_BIND_LEXICAL:
166 							if ((build_flags & ZEND_SSA_RC_INFERENCE) || (opline->extended_value & ZEND_BIND_REF)) {
167 								goto op2_def;
168 							}
169 							goto op2_use;
170 						case ZEND_ASSIGN_REF:
171 						case ZEND_FE_FETCH_R:
172 						case ZEND_FE_FETCH_RW:
173 op2_def:
174 							// FIXME: include into "use" too ...?
175 							DFG_SET(use, set_size, j, var_num);
176 							DFG_SET(def, set_size, j, var_num);
177 							break;
178 						default:
179 op2_use:
180 							if (!DFG_ISSET(def, set_size, j, var_num)) {
181 								DFG_SET(use, set_size, j, var_num);
182 							}
183 							break;
184 					}
185 				} else if (opline->op2_type & (IS_VAR|IS_TMP_VAR)) {
186 					var_num = EX_VAR_TO_NUM(opline->op2.var);
187 					if (opline->opcode == ZEND_FE_FETCH_R || opline->opcode == ZEND_FE_FETCH_RW) {
188 						DFG_SET(def, set_size, j, var_num);
189 					} else {
190 						if (!DFG_ISSET(def, set_size, j, var_num)) {
191 							DFG_SET(use, set_size, j, var_num);
192 						}
193 					}
194 				}
195 				if (opline->result_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
196 					var_num = EX_VAR_TO_NUM(opline->result.var);
197 					if ((build_flags & ZEND_SSA_USE_CV_RESULTS)
198 					 && opline->result_type == IS_CV) {
199 						DFG_SET(use, set_size, j, var_num);
200 					}
201 					DFG_SET(def, set_size, j, var_num);
202 				}
203 			}
204 		}
205 	}
206 
207 	/* Calculate "in" and "out" sets */
208 	{
209 		uint32_t worklist_len = zend_bitset_len(blocks_count);
210 		zend_bitset worklist;
211 		ALLOCA_FLAG(use_heap);
212 		worklist = ZEND_BITSET_ALLOCA(worklist_len, use_heap);
213 		memset(worklist, 0, worklist_len * ZEND_BITSET_ELM_SIZE);
214 		for (j = 0; j < blocks_count; j++) {
215 			zend_bitset_incl(worklist, j);
216 		}
217 		while (!zend_bitset_empty(worklist, worklist_len)) {
218 			/* We use the last block on the worklist, because predecessors tend to be located
219 			 * before the succeeding block, so this converges faster. */
220 			j = zend_bitset_last(worklist, worklist_len);
221 			zend_bitset_excl(worklist, j);
222 
223 			if ((blocks[j].flags & ZEND_BB_REACHABLE) == 0) {
224 				continue;
225 			}
226 			if (blocks[j].successors_count != 0) {
227 				zend_bitset_copy(DFG_BITSET(out, set_size, j), DFG_BITSET(in, set_size, blocks[j].successors[0]), set_size);
228 				for (k = 1; k < blocks[j].successors_count; k++) {
229 					zend_bitset_union(DFG_BITSET(out, set_size, j), DFG_BITSET(in, set_size, blocks[j].successors[k]), set_size);
230 				}
231 			} else {
232 				zend_bitset_clear(DFG_BITSET(out, set_size, j), set_size);
233 			}
234 			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);
235 			if (!zend_bitset_equal(DFG_BITSET(in, set_size, j), tmp, set_size)) {
236 				zend_bitset_copy(DFG_BITSET(in, set_size, j), tmp, set_size);
237 
238 				/* Add predecessors of changed block to worklist */
239 				{
240 					int *predecessors = &cfg->predecessors[blocks[j].predecessor_offset];
241 					for (k = 0; k < blocks[j].predecessors_count; k++) {
242 						zend_bitset_incl(worklist, predecessors[k]);
243 					}
244 				}
245 			}
246 		}
247 
248 		free_alloca(worklist, use_heap);
249 	}
250 
251 	return SUCCESS;
252 }
253 /* }}} */
254