/src/php-src/Zend/Optimizer/dce.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Zend Engine, DCE - Dead Code Elimination | |
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 | | | https://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: Nikita Popov <nikic@php.net> | |
16 | | | Dmitry Stogov <dmitry@php.net> | |
17 | | +----------------------------------------------------------------------+ |
18 | | */ |
19 | | |
20 | | #include "Optimizer/zend_optimizer_internal.h" |
21 | | #include "Optimizer/zend_inference.h" |
22 | | #include "Optimizer/zend_ssa.h" |
23 | | #include "Optimizer/zend_func_info.h" |
24 | | #include "Optimizer/zend_call_graph.h" |
25 | | #include "zend_bitset.h" |
26 | | |
27 | | /* This pass implements a form of dead code elimination (DCE). The algorithm optimistically assumes |
28 | | * that all instructions and phis are dead. Instructions with immediate side-effects are then marked |
29 | | * as live. We then recursively (using a worklist) propagate liveness to the instructions that def |
30 | | * the used operands. |
31 | | * |
32 | | * Notes: |
33 | | * * This pass does not perform unreachable code elimination. This happens as part of the SCCP |
34 | | * pass. |
35 | | * * The DCE is performed without taking control-dependence into account, i.e. all conditional |
36 | | * branches are assumed to be live. It's possible to take control-dependence into account using |
37 | | * the DCE algorithm described by Cytron et al., however it requires the construction of a |
38 | | * postdominator tree and of postdominance frontiers, which does not seem worthwhile at this |
39 | | * point. |
40 | | * * We separate intrinsic side-effects from potential side-effects in the form of notices thrown |
41 | | * by the instruction (in case we want to make this configurable). See may_have_side_effects() and |
42 | | * zend_may_throw(). |
43 | | * * We often cannot DCE assignments and unsets while guaranteeing that dtors run in the same |
44 | | * order. There is an optimization option to allow reordering of dtor effects. |
45 | | * * The algorithm is able to eliminate dead modifications of non-escaping arrays |
46 | | * and objects as well as dead arrays and objects allocations. |
47 | | */ |
48 | | |
49 | | typedef struct { |
50 | | zend_ssa *ssa; |
51 | | zend_op_array *op_array; |
52 | | zend_bitset instr_dead; |
53 | | zend_bitset phi_dead; |
54 | | zend_bitset instr_worklist; |
55 | | zend_bitset phi_worklist; |
56 | | zend_bitset phi_worklist_no_val; |
57 | | uint32_t instr_worklist_len; |
58 | | uint32_t phi_worklist_len; |
59 | | unsigned reorder_dtor_effects : 1; |
60 | | } context; |
61 | | |
62 | 33.5k | static inline bool is_bad_mod(const zend_ssa *ssa, int use, int def) { |
63 | 33.5k | if (def < 0) { |
64 | | /* This modification is not tracked by SSA, assume the worst */ |
65 | 6.07k | return 1; |
66 | 6.07k | } |
67 | 27.5k | if (ssa->var_info[use].type & MAY_BE_REF) { |
68 | | /* Modification of reference may have side-effect */ |
69 | 12.2k | return 1; |
70 | 12.2k | } |
71 | 15.2k | return 0; |
72 | 27.5k | } |
73 | | |
74 | | static inline bool may_have_side_effects( |
75 | | zend_op_array *op_array, zend_ssa *ssa, |
76 | | const zend_op *opline, const zend_ssa_op *ssa_op, |
77 | 763k | bool reorder_dtor_effects) { |
78 | 763k | switch (opline->opcode) { |
79 | 3.57k | case ZEND_NOP: |
80 | 3.75k | case ZEND_IS_IDENTICAL: |
81 | 3.78k | case ZEND_IS_NOT_IDENTICAL: |
82 | 7.00k | case ZEND_QM_ASSIGN: |
83 | 10.4k | case ZEND_FE_FREE: |
84 | 10.5k | case ZEND_TYPE_CHECK: |
85 | 10.5k | case ZEND_DEFINED: |
86 | 11.9k | case ZEND_ADD: |
87 | 12.4k | case ZEND_SUB: |
88 | 13.2k | case ZEND_MUL: |
89 | 13.2k | case ZEND_POW: |
90 | 13.3k | case ZEND_BW_OR: |
91 | 13.9k | case ZEND_BW_AND: |
92 | 14.2k | case ZEND_BW_XOR: |
93 | 16.8k | case ZEND_CONCAT: |
94 | 17.7k | case ZEND_FAST_CONCAT: |
95 | 17.8k | case ZEND_DIV: |
96 | 18.1k | case ZEND_MOD: |
97 | 18.6k | case ZEND_BOOL_XOR: |
98 | 19.5k | case ZEND_BOOL: |
99 | 19.7k | case ZEND_BOOL_NOT: |
100 | 20.9k | case ZEND_BW_NOT: |
101 | 21.4k | case ZEND_SL: |
102 | 21.7k | case ZEND_SR: |
103 | 22.1k | case ZEND_IS_EQUAL: |
104 | 22.6k | case ZEND_IS_NOT_EQUAL: |
105 | 26.2k | case ZEND_IS_SMALLER: |
106 | 26.3k | case ZEND_IS_SMALLER_OR_EQUAL: |
107 | 26.3k | case ZEND_CASE: |
108 | 26.3k | case ZEND_CASE_STRICT: |
109 | 26.4k | case ZEND_CAST: |
110 | 26.4k | case ZEND_ROPE_INIT: |
111 | 26.4k | case ZEND_ROPE_ADD: |
112 | 26.6k | case ZEND_INIT_ARRAY: |
113 | 26.6k | case ZEND_SPACESHIP: |
114 | 26.6k | case ZEND_STRLEN: |
115 | 26.7k | case ZEND_COUNT: |
116 | 26.7k | case ZEND_GET_TYPE: |
117 | 26.7k | case ZEND_ISSET_ISEMPTY_THIS: |
118 | 26.8k | case ZEND_ISSET_ISEMPTY_DIM_OBJ: |
119 | 27.0k | case ZEND_FETCH_DIM_IS: |
120 | 27.0k | case ZEND_ISSET_ISEMPTY_CV: |
121 | 27.0k | case ZEND_ISSET_ISEMPTY_VAR: |
122 | 27.0k | case ZEND_FETCH_IS: |
123 | 27.0k | case ZEND_IN_ARRAY: |
124 | 27.0k | case ZEND_FUNC_NUM_ARGS: |
125 | 27.0k | case ZEND_FUNC_GET_ARGS: |
126 | 27.0k | case ZEND_ARRAY_KEY_EXISTS: |
127 | | /* No side effects */ |
128 | 27.0k | return 0; |
129 | 30.6k | case ZEND_FREE: |
130 | 30.6k | return opline->extended_value == ZEND_FREE_VOID_CAST; |
131 | 138 | case ZEND_ADD_ARRAY_ELEMENT: |
132 | | /* TODO: We can't free two vars. Keep instruction alive. <?php [0, "$a" => "$b"]; */ |
133 | 138 | if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && (opline->op2_type & (IS_VAR|IS_TMP_VAR))) { |
134 | 8 | return 1; |
135 | 8 | } |
136 | 130 | return 0; |
137 | 325 | case ZEND_ROPE_END: |
138 | | /* TODO: Rope dce optimization, see #76446 */ |
139 | 325 | return 1; |
140 | 13.2k | case ZEND_JMP: |
141 | 19.8k | case ZEND_JMPZ: |
142 | 27.8k | case ZEND_JMPNZ: |
143 | 28.6k | case ZEND_JMPZ_EX: |
144 | 29.3k | case ZEND_JMPNZ_EX: |
145 | 29.9k | case ZEND_JMP_SET: |
146 | 30.2k | case ZEND_COALESCE: |
147 | 31.1k | case ZEND_ASSERT_CHECK: |
148 | 32.8k | case ZEND_JMP_NULL: |
149 | 33.0k | case ZEND_BIND_INIT_STATIC_OR_JMP: |
150 | 33.0k | case ZEND_JMP_FRAMELESS: |
151 | | /* For our purposes a jumps and branches are side effects. */ |
152 | 33.0k | return 1; |
153 | 0 | case ZEND_BEGIN_SILENCE: |
154 | 122k | case ZEND_END_SILENCE: |
155 | 161k | case ZEND_ECHO: |
156 | 161k | case ZEND_INCLUDE_OR_EVAL: |
157 | 162k | case ZEND_THROW: |
158 | 163k | case ZEND_MATCH_ERROR: |
159 | 163k | case ZEND_EXT_STMT: |
160 | 163k | case ZEND_EXT_FCALL_BEGIN: |
161 | 163k | case ZEND_EXT_FCALL_END: |
162 | 163k | case ZEND_TICKS: |
163 | 164k | case ZEND_YIELD: |
164 | 164k | case ZEND_YIELD_FROM: |
165 | 164k | case ZEND_VERIFY_NEVER_TYPE: |
166 | | /* Intrinsic side effects */ |
167 | 164k | return 1; |
168 | 96.3k | case ZEND_DO_FCALL: |
169 | 96.4k | case ZEND_DO_FCALL_BY_NAME: |
170 | 96.4k | case ZEND_DO_ICALL: |
171 | 102k | case ZEND_DO_UCALL: |
172 | 102k | case ZEND_FRAMELESS_ICALL_0: |
173 | 102k | case ZEND_FRAMELESS_ICALL_1: |
174 | 102k | case ZEND_FRAMELESS_ICALL_2: |
175 | 102k | case ZEND_FRAMELESS_ICALL_3: |
176 | | /* For now assume all calls have side effects */ |
177 | 102k | return 1; |
178 | 8.01k | case ZEND_RECV: |
179 | 9.70k | case ZEND_RECV_INIT: |
180 | | /* Even though RECV_INIT can be side-effect free, these cannot be simply dropped |
181 | | * due to the prologue skipping code. */ |
182 | 9.70k | return 1; |
183 | 672 | case ZEND_ASSIGN_REF: |
184 | 672 | return 1; |
185 | 20.2k | case ZEND_ASSIGN: |
186 | 20.2k | { |
187 | 20.2k | if (is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def)) { |
188 | 9.61k | return 1; |
189 | 9.61k | } |
190 | 10.6k | if (!reorder_dtor_effects) { |
191 | 10.6k | if (opline->op2_type != IS_CONST |
192 | 10.6k | && (OP2_INFO() & MAY_HAVE_DTOR) |
193 | 10.6k | && ssa->vars[ssa_op->op2_use].escape_state != ESCAPE_STATE_NO_ESCAPE) { |
194 | | /* DCE might shorten lifetime */ |
195 | 820 | return 1; |
196 | 820 | } |
197 | 10.6k | } |
198 | 9.85k | return 0; |
199 | 10.6k | } |
200 | 90 | case ZEND_UNSET_VAR: |
201 | 90 | return 1; |
202 | 1.31k | case ZEND_UNSET_CV: |
203 | 1.31k | { |
204 | 1.31k | uint32_t t1 = OP1_INFO(); |
205 | 1.31k | if (t1 & MAY_BE_REF) { |
206 | | /* We don't consider uses as the LHS of an assignment as real uses during DCE, so |
207 | | * an unset may be considered dead even if there is a later assignment to the |
208 | | * variable. Removing the unset in this case would not be correct if the variable |
209 | | * is a reference, because unset breaks references. */ |
210 | 926 | return 1; |
211 | 926 | } |
212 | 390 | return 0; |
213 | 1.31k | } |
214 | 3.44k | case ZEND_PRE_INC: |
215 | 3.74k | case ZEND_POST_INC: |
216 | 3.85k | case ZEND_PRE_DEC: |
217 | 3.96k | case ZEND_POST_DEC: |
218 | 3.96k | return is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def); |
219 | 1.05k | case ZEND_ASSIGN_OP: |
220 | 1.05k | return is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def) |
221 | 1.05k | || ssa->vars[ssa_op->op1_def].escape_state != ESCAPE_STATE_NO_ESCAPE; |
222 | 3.14k | case ZEND_ASSIGN_DIM: |
223 | 8.07k | case ZEND_ASSIGN_OBJ: |
224 | 8.07k | if (is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def) |
225 | 8.07k | || ssa->vars[ssa_op->op1_def].escape_state != ESCAPE_STATE_NO_ESCAPE) { |
226 | 7.59k | return 1; |
227 | 7.59k | } |
228 | 479 | if (!reorder_dtor_effects) { |
229 | 479 | opline++; |
230 | 479 | ssa_op++; |
231 | 479 | if (opline->op1_type != IS_CONST |
232 | 479 | && (OP1_INFO() & MAY_HAVE_DTOR)) { |
233 | | /* DCE might shorten lifetime */ |
234 | 114 | return 1; |
235 | 114 | } |
236 | 479 | } |
237 | 365 | return 0; |
238 | 165 | case ZEND_PRE_INC_OBJ: |
239 | 211 | case ZEND_PRE_DEC_OBJ: |
240 | 215 | case ZEND_POST_INC_OBJ: |
241 | 215 | case ZEND_POST_DEC_OBJ: |
242 | 215 | if (is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def) |
243 | 215 | || ssa->vars[ssa_op->op1_def].escape_state != ESCAPE_STATE_NO_ESCAPE) { |
244 | 207 | return 1; |
245 | 207 | } |
246 | 8 | return 0; |
247 | 442 | case ZEND_BIND_STATIC: |
248 | 442 | if (op_array->static_variables) { |
249 | | /* Implicit and Explicit bind static is effectively prologue of closure so |
250 | | report it has side effects like RECV, RECV_INIT; This allows us to |
251 | | reflect on the closure and discover used variable at runtime */ |
252 | 442 | if ((opline->extended_value & (ZEND_BIND_IMPLICIT|ZEND_BIND_EXPLICIT))) { |
253 | 198 | return 1; |
254 | 198 | } |
255 | | /* Modifies static variables which are observable through reflection */ |
256 | 244 | if ((opline->extended_value & ZEND_BIND_REF) && opline->op2_type != IS_UNUSED) { |
257 | 186 | return 1; |
258 | 186 | } |
259 | 244 | } |
260 | 58 | return 0; |
261 | 66 | case ZEND_CHECK_VAR: |
262 | 66 | return (OP1_INFO() & MAY_BE_UNDEF) != 0; |
263 | 2 | case ZEND_FE_RESET_R: |
264 | 2 | case ZEND_FE_RESET_RW: |
265 | | /* Model as not having side-effects -- let the side-effect be introduced by |
266 | | * FE_FETCH if the array is not known to be non-empty. */ |
267 | 2 | return (OP1_INFO() & MAY_BE_ANY) != MAY_BE_ARRAY; |
268 | 359k | default: |
269 | | /* For everything we didn't handle, assume a side-effect */ |
270 | 359k | return 1; |
271 | 763k | } |
272 | 763k | } |
273 | | |
274 | 1.60M | static zend_always_inline void add_to_worklists(context *ctx, int var_num, int check) { |
275 | 1.60M | zend_ssa_var *var = &ctx->ssa->vars[var_num]; |
276 | 1.60M | if (var->definition >= 0) { |
277 | 1.16M | if (!check || zend_bitset_in(ctx->instr_dead, var->definition)) { |
278 | 1.05M | zend_bitset_incl(ctx->instr_worklist, var->definition); |
279 | 1.05M | } |
280 | 1.16M | } else if (var->definition_phi) { |
281 | 332k | if (!check || zend_bitset_in(ctx->phi_dead, var_num)) { |
282 | 207k | zend_bitset_incl(ctx->phi_worklist, var_num); |
283 | 207k | } |
284 | 332k | } |
285 | 1.60M | } |
286 | | |
287 | 44.2k | static inline void add_to_phi_worklist_no_val(context *ctx, int var_num) { |
288 | 44.2k | zend_ssa_var *var = &ctx->ssa->vars[var_num]; |
289 | 44.2k | if (var->definition_phi && zend_bitset_in(ctx->phi_dead, var_num)) { |
290 | 5.69k | zend_bitset_incl(ctx->phi_worklist_no_val, var_num); |
291 | 5.69k | } |
292 | 44.2k | } |
293 | | |
294 | 1.71M | static zend_always_inline void add_operands_to_worklists(context *ctx, zend_op *opline, zend_ssa_op *ssa_op, zend_ssa *ssa, int check) { |
295 | 1.71M | if (ssa_op->result_use >= 0) { |
296 | 19.2k | add_to_worklists(ctx, ssa_op->result_use, check); |
297 | 19.2k | } |
298 | 1.71M | if (ssa_op->op1_use >= 0) { |
299 | 978k | if (!zend_ssa_is_no_val_use(opline, ssa_op, ssa_op->op1_use) |
300 | 978k | || (opline->opcode == ZEND_ASSIGN |
301 | 944k | && (ssa->var_info[ssa_op->op1_use].type & MAY_BE_REF) != 0)) { |
302 | 944k | add_to_worklists(ctx, ssa_op->op1_use, check); |
303 | 944k | } else { |
304 | 33.1k | add_to_phi_worklist_no_val(ctx, ssa_op->op1_use); |
305 | 33.1k | } |
306 | 978k | } |
307 | 1.71M | if (ssa_op->op2_use >= 0) { |
308 | 299k | if (!zend_ssa_is_no_val_use(opline, ssa_op, ssa_op->op2_use) |
309 | 299k | || (opline->opcode == ZEND_FE_FETCH_R |
310 | 297k | && (ssa->var_info[ssa_op->op2_use].type & MAY_BE_REF) != 0)) { |
311 | 297k | add_to_worklists(ctx, ssa_op->op2_use, check); |
312 | 297k | } else { |
313 | 1.52k | add_to_phi_worklist_no_val(ctx, ssa_op->op2_use); |
314 | 1.52k | } |
315 | 299k | } |
316 | 1.71M | } |
317 | | |
318 | 191k | static zend_always_inline void add_phi_sources_to_worklists(context *ctx, zend_ssa_phi *phi, int check) { |
319 | 191k | zend_ssa *ssa = ctx->ssa; |
320 | 191k | int source; |
321 | 870k | FOREACH_PHI_SOURCE(phi, source) { |
322 | 870k | add_to_worklists(ctx, source, check); |
323 | 870k | } FOREACH_PHI_SOURCE_END(); |
324 | 191k | } |
325 | | |
326 | 18.5k | static inline bool is_var_dead(context *ctx, int var_num) { |
327 | 18.5k | zend_ssa_var *var = &ctx->ssa->vars[var_num]; |
328 | 18.5k | if (var->definition_phi) { |
329 | 1.11k | return zend_bitset_in(ctx->phi_dead, var_num); |
330 | 17.4k | } else if (var->definition >= 0) { |
331 | 11.4k | return zend_bitset_in(ctx->instr_dead, var->definition); |
332 | 11.4k | } else { |
333 | | /* Variable has no definition, so either the definition has already been removed (var is |
334 | | * dead) or this is one of the implicit variables at the start of the function (for our |
335 | | * purposes live) */ |
336 | 5.96k | return var_num >= ctx->op_array->last_var; |
337 | 5.96k | } |
338 | 18.5k | } |
339 | | |
340 | | // Sometimes we can mark the var as EXT_UNUSED |
341 | 8.74k | static bool try_remove_var_def(context *ctx, int free_var, int use_chain, zend_op *opline) { |
342 | 8.74k | if (use_chain >= 0) { |
343 | 0 | return 0; |
344 | 0 | } |
345 | 8.74k | zend_ssa_var *var = &ctx->ssa->vars[free_var]; |
346 | 8.74k | int def = var->definition; |
347 | | |
348 | 8.74k | if (def >= 0) { |
349 | 8.61k | zend_ssa_op *def_op = &ctx->ssa->ops[def]; |
350 | | |
351 | 8.61k | if (def_op->result_def == free_var |
352 | 8.61k | && var->phi_use_chain == NULL |
353 | 8.61k | && var->use_chain == (opline - ctx->op_array->opcodes)) { |
354 | 8.50k | zend_op *def_opline = &ctx->op_array->opcodes[def]; |
355 | | |
356 | 8.50k | switch (def_opline->opcode) { |
357 | 183 | case ZEND_ASSIGN: |
358 | 183 | case ZEND_ASSIGN_REF: |
359 | 204 | case ZEND_ASSIGN_DIM: |
360 | 208 | case ZEND_ASSIGN_OBJ: |
361 | 208 | case ZEND_ASSIGN_OBJ_REF: |
362 | 208 | case ZEND_ASSIGN_STATIC_PROP: |
363 | 208 | case ZEND_ASSIGN_STATIC_PROP_REF: |
364 | 363 | case ZEND_ASSIGN_OP: |
365 | 373 | case ZEND_ASSIGN_DIM_OP: |
366 | 373 | case ZEND_ASSIGN_OBJ_OP: |
367 | 373 | case ZEND_ASSIGN_STATIC_PROP_OP: |
368 | 373 | case ZEND_PRE_INC: |
369 | 373 | case ZEND_PRE_DEC: |
370 | 373 | case ZEND_PRE_INC_OBJ: |
371 | 373 | case ZEND_PRE_DEC_OBJ: |
372 | 373 | case ZEND_DO_ICALL: |
373 | 375 | case ZEND_DO_UCALL: |
374 | 375 | case ZEND_DO_FCALL_BY_NAME: |
375 | 738 | case ZEND_DO_FCALL: |
376 | 738 | case ZEND_INCLUDE_OR_EVAL: |
377 | 738 | case ZEND_YIELD: |
378 | 738 | case ZEND_YIELD_FROM: |
379 | 738 | case ZEND_ASSERT_CHECK: |
380 | 738 | def_opline->result_type = IS_UNUSED; |
381 | 738 | def_opline->result.var = 0; |
382 | 738 | def_op->result_def = -1; |
383 | 738 | var->definition = -1; |
384 | 738 | return 1; |
385 | 7.76k | default: |
386 | 7.76k | break; |
387 | 8.50k | } |
388 | 8.50k | } |
389 | 8.61k | } |
390 | 8.00k | return 0; |
391 | 8.74k | } |
392 | | |
393 | 63.8k | static zend_always_inline bool may_be_refcounted(uint32_t type) { |
394 | 63.8k | return (type & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) != 0; |
395 | 63.8k | } |
396 | | |
397 | 22.0k | static inline bool is_free_of_live_var(context *ctx, zend_op *opline, zend_ssa_op *ssa_op) { |
398 | 22.0k | switch (opline->opcode) { |
399 | 10.0k | case ZEND_FREE: |
400 | | /* It is always safe to remove FREEs of non-refcounted values, even if they are live. */ |
401 | 10.0k | if ((ctx->ssa->var_info[ssa_op->op1_use].type & (MAY_BE_REF|MAY_BE_ANY|MAY_BE_UNDEF)) != 0 |
402 | 10.0k | && !may_be_refcounted(ctx->ssa->var_info[ssa_op->op1_use].type)) { |
403 | 7.00k | return 0; |
404 | 7.00k | } |
405 | 3.01k | ZEND_FALLTHROUGH; |
406 | 3.72k | case ZEND_FE_FREE: |
407 | 3.72k | return !is_var_dead(ctx, ssa_op->op1_use); |
408 | 11.3k | default: |
409 | 11.3k | return 0; |
410 | 22.0k | } |
411 | 22.0k | } |
412 | | |
413 | | /* Returns whether the instruction has been DCEd */ |
414 | 25.6k | static bool dce_instr(context *ctx, zend_op *opline, zend_ssa_op *ssa_op) { |
415 | 25.6k | zend_ssa *ssa = ctx->ssa; |
416 | 25.6k | int free_var = -1; |
417 | 25.6k | uint8_t free_var_type; |
418 | | |
419 | 25.6k | if (opline->opcode == ZEND_NOP) { |
420 | 3.57k | return 0; |
421 | 3.57k | } |
422 | | |
423 | | /* We mark FREEs as dead, but they're only really dead if the destroyed var is dead */ |
424 | 22.0k | if (is_free_of_live_var(ctx, opline, ssa_op)) { |
425 | 3.03k | return 0; |
426 | 3.03k | } |
427 | | |
428 | 19.0k | if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))&& !is_var_dead(ctx, ssa_op->op1_use)) { |
429 | 6.98k | if (!try_remove_var_def(ctx, ssa_op->op1_use, ssa_op->op1_use_chain, opline)) { |
430 | 6.77k | if (may_be_refcounted(ssa->var_info[ssa_op->op1_use].type) |
431 | 6.77k | && opline->opcode != ZEND_CASE && opline->opcode != ZEND_CASE_STRICT) { |
432 | 515 | free_var = ssa_op->op1_use; |
433 | 515 | free_var_type = opline->op1_type; |
434 | 515 | } |
435 | 6.77k | } |
436 | 6.98k | } |
437 | 19.0k | if ((opline->op2_type & (IS_VAR|IS_TMP_VAR)) && !is_var_dead(ctx, ssa_op->op2_use)) { |
438 | 1.75k | if (!try_remove_var_def(ctx, ssa_op->op2_use, ssa_op->op2_use_chain, opline)) { |
439 | 1.23k | if (may_be_refcounted(ssa->var_info[ssa_op->op2_use].type)) { |
440 | 509 | if (free_var >= 0) { |
441 | | // TODO: We can't free two vars. Keep instruction alive. |
442 | 284 | zend_bitset_excl(ctx->instr_dead, opline - ctx->op_array->opcodes); |
443 | 284 | return 0; |
444 | 284 | } |
445 | 225 | free_var = ssa_op->op2_use; |
446 | 225 | free_var_type = opline->op2_type; |
447 | 225 | } |
448 | 1.23k | } |
449 | 1.75k | } |
450 | | |
451 | 18.7k | zend_ssa_rename_defs_of_instr(ctx->ssa, ssa_op); |
452 | 18.7k | zend_ssa_remove_instr(ctx->ssa, opline, ssa_op); |
453 | | |
454 | 18.7k | if (free_var >= 0) { |
455 | 456 | opline->opcode = ZEND_FREE; |
456 | 456 | opline->op1.var = EX_NUM_TO_VAR(ssa->vars[free_var].var); |
457 | 456 | opline->op1_type = free_var_type; |
458 | | |
459 | 456 | ssa_op->op1_use = free_var; |
460 | 456 | ssa_op->op1_use_chain = ssa->vars[free_var].use_chain; |
461 | 456 | ssa->vars[free_var].use_chain = ssa_op - ssa->ops; |
462 | 456 | return 0; |
463 | 456 | } |
464 | 18.2k | return 1; |
465 | 18.7k | } |
466 | | |
467 | 108k | static inline int get_common_phi_source(zend_ssa *ssa, zend_ssa_phi *phi) { |
468 | 108k | int common_source = -1; |
469 | 108k | int source; |
470 | 543k | FOREACH_PHI_SOURCE(phi, source) { |
471 | 543k | if (source == phi->ssa_var) { |
472 | 1.33k | continue; |
473 | 1.33k | } |
474 | 216k | if (common_source == -1) { |
475 | 108k | common_source = source; |
476 | 108k | } else if (common_source != source) { |
477 | 107k | return -1; |
478 | 107k | } |
479 | 216k | } FOREACH_PHI_SOURCE_END(); |
480 | | |
481 | | /* If all sources are phi->ssa_var this phi must be in an unreachable cycle. |
482 | | * We can't easily drop the phi in that case, as we don't have something to replace it with. |
483 | | * Ideally SCCP would eliminate the whole cycle. */ |
484 | 542 | return common_source; |
485 | 108k | } |
486 | | |
487 | 156k | static void try_remove_trivial_phi(context *ctx, zend_ssa_phi *phi) { |
488 | 156k | zend_ssa *ssa = ctx->ssa; |
489 | 156k | if (phi->pi < 0) { |
490 | | /* Phi assignment with identical source operands */ |
491 | 108k | int common_source = get_common_phi_source(ssa, phi); |
492 | 108k | if (common_source >= 0) { |
493 | 542 | zend_ssa_rename_var_uses(ssa, phi->ssa_var, common_source, 1); |
494 | 542 | zend_ssa_remove_phi(ssa, phi); |
495 | 542 | } |
496 | 108k | } else { |
497 | | /* Pi assignment that is only used in Phi/Pi assignments */ |
498 | | // TODO What if we want to rerun type inference after DCE? Maybe separate this? |
499 | | /*ZEND_ASSERT(phi->sources[0] != -1); |
500 | | if (ssa->vars[phi->ssa_var].use_chain < 0) { |
501 | | zend_ssa_rename_var_uses_keep_types(ssa, phi->ssa_var, phi->sources[0], 1); |
502 | | zend_ssa_remove_phi(ssa, phi); |
503 | | }*/ |
504 | 48.0k | } |
505 | 156k | } |
506 | | |
507 | 4 | static inline bool may_break_varargs(const zend_op_array *op_array, const zend_ssa *ssa, const zend_ssa_op *ssa_op) { |
508 | 4 | if (ssa_op->op1_def >= 0 |
509 | 4 | && ssa->vars[ssa_op->op1_def].var < op_array->num_args) { |
510 | 0 | return 1; |
511 | 0 | } |
512 | 4 | if (ssa_op->op2_def >= 0 |
513 | 4 | && ssa->vars[ssa_op->op2_def].var < op_array->num_args) { |
514 | 0 | return 1; |
515 | 0 | } |
516 | 4 | if (ssa_op->result_def >= 0 |
517 | 4 | && ssa->vars[ssa_op->result_def].var < op_array->num_args) { |
518 | 0 | return 1; |
519 | 0 | } |
520 | 4 | return 0; |
521 | 4 | } |
522 | | |
523 | 37.1k | static inline bool may_throw_dce_exception(const zend_op *opline) { |
524 | 37.1k | return opline->opcode == ZEND_ADD_ARRAY_ELEMENT && opline->op2_type == IS_UNUSED; |
525 | 37.1k | } |
526 | | |
527 | 78.5k | int dce_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *optimizer_ctx, zend_ssa *ssa, bool reorder_dtor_effects) { |
528 | 78.5k | int i; |
529 | 78.5k | zend_ssa_phi *phi; |
530 | 78.5k | int removed_ops = 0; |
531 | | |
532 | | /* DCE of CV operations that changes arguments may affect vararg functions. */ |
533 | 78.5k | bool has_varargs = (ssa->cfg.flags & ZEND_FUNC_VARARG) != 0; |
534 | | |
535 | 78.5k | context ctx; |
536 | 78.5k | ctx.ssa = ssa; |
537 | 78.5k | ctx.op_array = op_array; |
538 | 78.5k | ctx.reorder_dtor_effects = reorder_dtor_effects; |
539 | | |
540 | 78.5k | void *checkpoint = zend_arena_checkpoint(optimizer_ctx->arena); |
541 | | /* We have no dedicated phi vector, so we use the whole ssa var vector instead */ |
542 | 78.5k | ctx.instr_worklist_len = zend_bitset_len(op_array->last); |
543 | 78.5k | ctx.instr_worklist = zend_arena_calloc(&optimizer_ctx->arena, ctx.instr_worklist_len, sizeof(zend_ulong)); |
544 | 78.5k | ctx.phi_worklist_len = zend_bitset_len(ssa->vars_count); |
545 | 78.5k | ctx.phi_worklist = zend_arena_calloc(&optimizer_ctx->arena, ctx.phi_worklist_len, sizeof(zend_ulong)); |
546 | 78.5k | ctx.phi_worklist_no_val = zend_arena_calloc(&optimizer_ctx->arena, ctx.phi_worklist_len, sizeof(zend_ulong)); |
547 | | |
548 | | /* Optimistically assume all instructions and phis to be dead */ |
549 | 78.5k | ctx.instr_dead = zend_arena_calloc(&optimizer_ctx->arena, ctx.instr_worklist_len, sizeof(zend_ulong)); |
550 | 78.5k | ctx.phi_dead = zend_arena_alloc(&optimizer_ctx->arena, ctx.phi_worklist_len * sizeof(zend_ulong)); |
551 | 78.5k | memset(ctx.phi_dead, 0xff, sizeof(zend_ulong) * ctx.phi_worklist_len); |
552 | | |
553 | | /* Mark non-CV phis as live. Even if the result is unused, we generally cannot remove one |
554 | | * of the producing instructions, as it combines producing the result with control flow. |
555 | | * This can be made more precise if there are any cases where this is not the case. */ |
556 | 463k | FOREACH_PHI(phi) { |
557 | 463k | if (phi->var >= op_array->last_var |
558 | 161k | && may_be_refcounted(ssa->var_info[phi->ssa_var].type)) { |
559 | 40.7k | zend_bitset_excl(ctx.phi_dead, phi->ssa_var); |
560 | 40.7k | add_phi_sources_to_worklists(&ctx, phi, 0); |
561 | 40.7k | } |
562 | 463k | } FOREACH_PHI_END(); |
563 | | |
564 | | /* Mark reachable instruction without side effects as dead */ |
565 | 78.5k | int b = ssa->cfg.blocks_count; |
566 | 302k | while (b > 0) { |
567 | 223k | int op_data = -1; |
568 | | |
569 | 223k | b--; |
570 | 223k | zend_basic_block *block = &ssa->cfg.blocks[b]; |
571 | 223k | if (!(block->flags & ZEND_BB_REACHABLE)) { |
572 | 7.63k | continue; |
573 | 7.63k | } |
574 | 216k | i = block->start + block->len; |
575 | 1.95M | while (i > block->start) { |
576 | 1.74M | i--; |
577 | | |
578 | 1.74M | if (op_array->opcodes[i].opcode == ZEND_OP_DATA) { |
579 | 18.4k | op_data = i; |
580 | 18.4k | continue; |
581 | 18.4k | } |
582 | | |
583 | 1.72M | if (zend_bitset_in(ctx.instr_worklist, i)) { |
584 | 959k | zend_bitset_excl(ctx.instr_worklist, i); |
585 | 959k | add_operands_to_worklists(&ctx, &op_array->opcodes[i], &ssa->ops[i], ssa, 0); |
586 | 959k | if (op_data >= 0) { |
587 | 8.32k | add_operands_to_worklists(&ctx, &op_array->opcodes[op_data], &ssa->ops[op_data], ssa, 0); |
588 | 8.32k | } |
589 | 959k | } else if (may_have_side_effects(op_array, ssa, &op_array->opcodes[i], &ssa->ops[i], ctx.reorder_dtor_effects) |
590 | 763k | || (zend_may_throw(&op_array->opcodes[i], &ssa->ops[i], op_array, ssa) |
591 | 70.8k | && !may_throw_dce_exception(&op_array->opcodes[i])) |
592 | 763k | || (has_varargs && may_break_varargs(op_array, ssa, &ssa->ops[i]))) { |
593 | 729k | if (op_array->opcodes[i].opcode == ZEND_NEW |
594 | 729k | && op_array->opcodes[i+1].opcode == ZEND_DO_FCALL |
595 | 729k | && ssa->ops[i].result_def >= 0 |
596 | 729k | && ssa->vars[ssa->ops[i].result_def].escape_state == ESCAPE_STATE_NO_ESCAPE) { |
597 | 94 | zend_bitset_incl(ctx.instr_dead, i); |
598 | 94 | zend_bitset_incl(ctx.instr_dead, i+1); |
599 | 729k | } else { |
600 | 729k | add_operands_to_worklists(&ctx, &op_array->opcodes[i], &ssa->ops[i], ssa, 0); |
601 | 729k | if (op_data >= 0) { |
602 | 10.0k | add_operands_to_worklists(&ctx, &op_array->opcodes[op_data], &ssa->ops[op_data], ssa, 0); |
603 | 10.0k | } |
604 | 729k | } |
605 | 729k | } else { |
606 | 33.7k | zend_bitset_incl(ctx.instr_dead, i); |
607 | 33.7k | if (op_data >= 0) { |
608 | 93 | zend_bitset_incl(ctx.instr_dead, op_data); |
609 | 93 | } |
610 | 33.7k | } |
611 | 1.72M | op_data = -1; |
612 | 1.72M | } |
613 | 216k | } |
614 | | |
615 | | /* Propagate liveness backwards to all definitions of used vars */ |
616 | 90.9k | while (!zend_bitset_empty(ctx.instr_worklist, ctx.instr_worklist_len) |
617 | 90.9k | || !zend_bitset_empty(ctx.phi_worklist, ctx.phi_worklist_len)) { |
618 | 20.8k | while ((i = zend_bitset_pop_first(ctx.instr_worklist, ctx.instr_worklist_len)) >= 0) { |
619 | 8.47k | zend_bitset_excl(ctx.instr_dead, i); |
620 | 8.47k | add_operands_to_worklists(&ctx, &op_array->opcodes[i], &ssa->ops[i], ssa, 1); |
621 | 8.47k | if (i < op_array->last |
622 | 8.47k | && (op_array->opcodes[i+1].opcode == ZEND_OP_DATA |
623 | 8.47k | || (op_array->opcodes[i].opcode == ZEND_NEW |
624 | 8.40k | && op_array->opcodes[i+1].opcode == ZEND_DO_FCALL))) { |
625 | 76 | zend_bitset_excl(ctx.instr_dead, i+1); |
626 | 76 | add_operands_to_worklists(&ctx, &op_array->opcodes[i+1], &ssa->ops[i+1], ssa, 1); |
627 | 76 | } |
628 | 8.47k | } |
629 | 163k | while ((i = zend_bitset_pop_first(ctx.phi_worklist, ctx.phi_worklist_len)) >= 0) { |
630 | 151k | zend_bitset_excl(ctx.phi_dead, i); |
631 | 151k | zend_bitset_excl(ctx.phi_worklist_no_val, i); |
632 | 151k | add_phi_sources_to_worklists(&ctx, ssa->vars[i].definition_phi, 1); |
633 | 151k | } |
634 | 12.4k | } |
635 | | |
636 | | /* Eliminate dead instructions */ |
637 | 353k | ZEND_BITSET_FOREACH(ctx.instr_dead, ctx.instr_worklist_len, i) { |
638 | 25.6k | removed_ops += dce_instr(&ctx, &op_array->opcodes[i], &ssa->ops[i]); |
639 | 25.6k | } ZEND_BITSET_FOREACH_END(); |
640 | | |
641 | | /* Improper uses don't count as "uses" for the purpose of instruction elimination, |
642 | | * but we have to retain phis defining them. |
643 | | * Propagate this information backwards, marking any phi with an improperly used |
644 | | * target as non-dead. */ |
645 | 83.4k | while ((i = zend_bitset_pop_first(ctx.phi_worklist_no_val, ctx.phi_worklist_len)) >= 0) { |
646 | 4.87k | zend_ssa_phi *phi = ssa->vars[i].definition_phi; |
647 | 4.87k | int source; |
648 | 4.87k | zend_bitset_excl(ctx.phi_dead, i); |
649 | 24.1k | FOREACH_PHI_SOURCE(phi, source) { |
650 | 24.1k | add_to_phi_worklist_no_val(&ctx, source); |
651 | 24.1k | } FOREACH_PHI_SOURCE_END(); |
652 | 4.87k | } |
653 | | |
654 | | /* Now collect the actually dead phis */ |
655 | 463k | FOREACH_PHI(phi) { |
656 | 463k | if (zend_bitset_in(ctx.phi_dead, phi->ssa_var)) { |
657 | 4.74k | zend_ssa_remove_uses_of_var(ssa, phi->ssa_var); |
658 | 4.74k | zend_ssa_remove_phi(ssa, phi); |
659 | 156k | } else { |
660 | | /* Remove trivial phis (phis with identical source operands) */ |
661 | 156k | try_remove_trivial_phi(&ctx, phi); |
662 | 156k | } |
663 | 463k | } FOREACH_PHI_END(); |
664 | | |
665 | 78.5k | zend_arena_release(&optimizer_ctx->arena, checkpoint); |
666 | | |
667 | 78.5k | return removed_ops; |
668 | 78.5k | } |