/src/php-src/Zend/Optimizer/dce.c
Line | Count | Source |
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 | 6.27k | static inline bool is_bad_mod(const zend_ssa *ssa, int use, int def) { |
63 | 6.27k | if (def < 0) { |
64 | | /* This modification is not tracked by SSA, assume the worst */ |
65 | 855 | return true; |
66 | 855 | } |
67 | 5.42k | if (ssa->var_info[use].type & MAY_BE_REF) { |
68 | | /* Modification of reference may have side-effect */ |
69 | 997 | return true; |
70 | 997 | } |
71 | 4.42k | return false; |
72 | 5.42k | } |
73 | | |
74 | | static inline bool may_have_side_effects( |
75 | | const zend_op_array *op_array, const zend_ssa *ssa, |
76 | | const zend_op *opline, const zend_ssa_op *ssa_op, |
77 | 147k | bool reorder_dtor_effects) { |
78 | 147k | switch (opline->opcode) { |
79 | 657 | case ZEND_NOP: |
80 | 808 | case ZEND_IS_IDENTICAL: |
81 | 810 | case ZEND_IS_NOT_IDENTICAL: |
82 | 2.13k | case ZEND_QM_ASSIGN: |
83 | 2.53k | case ZEND_FE_FREE: |
84 | 2.58k | case ZEND_TYPE_CHECK: |
85 | 2.58k | case ZEND_DEFINED: |
86 | 3.22k | case ZEND_ADD: |
87 | 3.41k | case ZEND_SUB: |
88 | 3.71k | case ZEND_MUL: |
89 | 3.71k | case ZEND_POW: |
90 | 3.76k | case ZEND_BW_OR: |
91 | 3.79k | case ZEND_BW_AND: |
92 | 3.91k | case ZEND_BW_XOR: |
93 | 4.74k | case ZEND_CONCAT: |
94 | 5.72k | case ZEND_FAST_CONCAT: |
95 | 5.73k | case ZEND_DIV: |
96 | 5.79k | case ZEND_MOD: |
97 | 5.99k | case ZEND_BOOL_XOR: |
98 | 6.25k | case ZEND_BOOL: |
99 | 6.43k | case ZEND_BOOL_NOT: |
100 | 6.56k | case ZEND_BW_NOT: |
101 | 6.67k | case ZEND_SL: |
102 | 6.73k | case ZEND_SR: |
103 | 6.93k | case ZEND_IS_EQUAL: |
104 | 7.14k | case ZEND_IS_NOT_EQUAL: |
105 | 7.74k | case ZEND_IS_SMALLER: |
106 | 7.75k | case ZEND_IS_SMALLER_OR_EQUAL: |
107 | 7.75k | case ZEND_CASE: |
108 | 7.75k | case ZEND_CASE_STRICT: |
109 | 7.75k | case ZEND_CAST: |
110 | 7.75k | case ZEND_ROPE_INIT: |
111 | 7.75k | case ZEND_ROPE_ADD: |
112 | 7.81k | case ZEND_INIT_ARRAY: |
113 | 7.81k | case ZEND_SPACESHIP: |
114 | 8.10k | case ZEND_STRLEN: |
115 | 8.12k | case ZEND_COUNT: |
116 | 8.12k | case ZEND_GET_TYPE: |
117 | 8.12k | case ZEND_ISSET_ISEMPTY_THIS: |
118 | 8.18k | case ZEND_ISSET_ISEMPTY_DIM_OBJ: |
119 | 8.20k | case ZEND_FETCH_DIM_IS: |
120 | 8.20k | case ZEND_ISSET_ISEMPTY_CV: |
121 | 8.20k | case ZEND_ISSET_ISEMPTY_VAR: |
122 | 8.20k | case ZEND_FETCH_IS: |
123 | 8.20k | case ZEND_IN_ARRAY: |
124 | 8.20k | case ZEND_FUNC_NUM_ARGS: |
125 | 8.20k | case ZEND_FUNC_GET_ARGS: |
126 | 8.20k | case ZEND_ARRAY_KEY_EXISTS: |
127 | 8.49k | case ZEND_COPY_TMP: |
128 | | /* No side effects */ |
129 | 8.49k | return false; |
130 | 11.0k | case ZEND_FREE: |
131 | 11.0k | return opline->extended_value == ZEND_FREE_VOID_CAST; |
132 | 119 | case ZEND_ADD_ARRAY_ELEMENT: |
133 | | /* TODO: We can't free two vars. Keep instruction alive. <?php [0, "$a" => "$b"]; */ |
134 | 119 | if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && (opline->op2_type & (IS_VAR|IS_TMP_VAR))) { |
135 | 9 | return true; |
136 | 9 | } |
137 | 110 | return false; |
138 | 166 | case ZEND_ROPE_END: |
139 | | /* TODO: Rope dce optimization, see #76446 */ |
140 | 166 | return true; |
141 | 2.24k | case ZEND_JMP: |
142 | 4.20k | case ZEND_JMPZ: |
143 | 5.43k | case ZEND_JMPNZ: |
144 | 5.68k | case ZEND_JMPZ_EX: |
145 | 5.89k | case ZEND_JMPNZ_EX: |
146 | 6.26k | case ZEND_JMP_SET: |
147 | 6.36k | case ZEND_COALESCE: |
148 | 6.44k | case ZEND_ASSERT_CHECK: |
149 | 6.46k | case ZEND_JMP_NULL: |
150 | 6.46k | case ZEND_BIND_INIT_STATIC_OR_JMP: |
151 | 6.46k | case ZEND_JMP_FRAMELESS: |
152 | | /* For our purposes a jumps and branches are side effects. */ |
153 | 6.46k | return true; |
154 | 0 | case ZEND_BEGIN_SILENCE: |
155 | 46.8k | case ZEND_END_SILENCE: |
156 | 54.3k | case ZEND_ECHO: |
157 | 54.3k | case ZEND_INCLUDE_OR_EVAL: |
158 | 54.4k | case ZEND_THROW: |
159 | 54.4k | case ZEND_MATCH_ERROR: |
160 | 54.4k | case ZEND_EXT_STMT: |
161 | 54.4k | case ZEND_EXT_FCALL_BEGIN: |
162 | 54.4k | case ZEND_EXT_FCALL_END: |
163 | 54.5k | case ZEND_TICKS: |
164 | 54.5k | case ZEND_YIELD: |
165 | 54.5k | case ZEND_YIELD_FROM: |
166 | 54.5k | case ZEND_VERIFY_NEVER_TYPE: |
167 | | /* Intrinsic side effects */ |
168 | 54.5k | return true; |
169 | 9.69k | case ZEND_DO_FCALL: |
170 | 9.69k | case ZEND_DO_FCALL_BY_NAME: |
171 | 9.69k | case ZEND_DO_ICALL: |
172 | 10.5k | case ZEND_DO_UCALL: |
173 | 10.5k | case ZEND_FRAMELESS_ICALL_0: |
174 | 10.5k | case ZEND_FRAMELESS_ICALL_1: |
175 | 10.5k | case ZEND_FRAMELESS_ICALL_2: |
176 | 10.5k | case ZEND_FRAMELESS_ICALL_3: |
177 | | /* For now assume all calls have side effects */ |
178 | 10.5k | return true; |
179 | 918 | case ZEND_RECV: |
180 | 1.05k | case ZEND_RECV_INIT: |
181 | | /* Even though RECV_INIT can be side-effect free, these cannot be simply dropped |
182 | | * due to the prologue skipping code. */ |
183 | 1.05k | return true; |
184 | 50 | case ZEND_ASSIGN_REF: |
185 | 50 | return true; |
186 | 4.23k | case ZEND_ASSIGN: |
187 | 4.23k | { |
188 | 4.23k | if (is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def)) { |
189 | 919 | return true; |
190 | 919 | } |
191 | 3.31k | if (!reorder_dtor_effects) { |
192 | 3.31k | if (opline->op2_type != IS_CONST |
193 | 1.70k | && (OP2_INFO() & MAY_HAVE_DTOR) |
194 | 295 | && ssa->vars[ssa_op->op2_use].escape_state != ESCAPE_STATE_NO_ESCAPE) { |
195 | | /* DCE might shorten lifetime */ |
196 | 211 | return true; |
197 | 211 | } |
198 | 3.31k | } |
199 | 3.10k | return false; |
200 | 3.31k | } |
201 | 0 | case ZEND_UNSET_VAR: |
202 | 0 | return true; |
203 | 552 | case ZEND_UNSET_CV: |
204 | 552 | { |
205 | 552 | uint32_t t1 = OP1_INFO(); |
206 | 552 | if (t1 & MAY_BE_REF) { |
207 | | /* We don't consider uses as the LHS of an assignment as real uses during DCE, so |
208 | | * an unset may be considered dead even if there is a later assignment to the |
209 | | * variable. Removing the unset in this case would not be correct if the variable |
210 | | * is a reference, because unset breaks references. */ |
211 | 32 | return true; |
212 | 32 | } |
213 | 520 | return false; |
214 | 552 | } |
215 | 459 | case ZEND_PRE_INC: |
216 | 580 | case ZEND_POST_INC: |
217 | 582 | case ZEND_PRE_DEC: |
218 | 595 | case ZEND_POST_DEC: |
219 | 595 | return is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def); |
220 | 307 | case ZEND_ASSIGN_OP: |
221 | 307 | return is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def) |
222 | 293 | || ssa->vars[ssa_op->op1_def].escape_state != ESCAPE_STATE_NO_ESCAPE; |
223 | 532 | case ZEND_ASSIGN_DIM: |
224 | 1.11k | case ZEND_ASSIGN_OBJ: |
225 | 1.11k | if (is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def) |
226 | 991 | || ssa->vars[ssa_op->op1_def].escape_state != ESCAPE_STATE_NO_ESCAPE) { |
227 | 991 | return true; |
228 | 991 | } |
229 | 128 | if (!reorder_dtor_effects) { |
230 | 128 | opline++; |
231 | 128 | ssa_op++; |
232 | 128 | if (opline->op1_type != IS_CONST |
233 | 58 | && (OP1_INFO() & MAY_HAVE_DTOR)) { |
234 | | /* DCE might shorten lifetime */ |
235 | 8 | return true; |
236 | 8 | } |
237 | 128 | } |
238 | 120 | return false; |
239 | 18 | case ZEND_PRE_INC_OBJ: |
240 | 18 | case ZEND_PRE_DEC_OBJ: |
241 | 18 | case ZEND_POST_INC_OBJ: |
242 | 18 | case ZEND_POST_DEC_OBJ: |
243 | 18 | if (is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def) |
244 | 18 | || ssa->vars[ssa_op->op1_def].escape_state != ESCAPE_STATE_NO_ESCAPE) { |
245 | 18 | return true; |
246 | 18 | } |
247 | 0 | return false; |
248 | 20 | case ZEND_BIND_STATIC: |
249 | 20 | if (op_array->static_variables) { |
250 | | /* Implicit and Explicit bind static is effectively prologue of closure so |
251 | | report it has side effects like RECV, RECV_INIT; This allows us to |
252 | | reflect on the closure and discover used variable at runtime */ |
253 | 20 | if ((opline->extended_value & (ZEND_BIND_IMPLICIT|ZEND_BIND_EXPLICIT))) { |
254 | 4 | return true; |
255 | 4 | } |
256 | | /* Modifies static variables which are observable through reflection */ |
257 | 16 | if ((opline->extended_value & ZEND_BIND_REF) && opline->op2_type != IS_UNUSED) { |
258 | 0 | return true; |
259 | 0 | } |
260 | 16 | } |
261 | 16 | return false; |
262 | 0 | case ZEND_CHECK_VAR: |
263 | 0 | return (OP1_INFO() & MAY_BE_UNDEF) != 0; |
264 | 0 | case ZEND_FE_RESET_R: |
265 | 0 | case ZEND_FE_RESET_RW: |
266 | | /* Model as not having side-effects -- let the side-effect be introduced by |
267 | | * FE_FETCH if the array is not known to be non-empty. */ |
268 | 0 | return (OP1_INFO() & MAY_BE_ANY) != MAY_BE_ARRAY; |
269 | 48.3k | default: |
270 | | /* For everything we didn't handle, assume a side-effect */ |
271 | 48.3k | return true; |
272 | 147k | } |
273 | 147k | } |
274 | | |
275 | 737k | static zend_always_inline void add_to_worklists(const context *ctx, int var_num, int check) { |
276 | 737k | const zend_ssa_var *var = &ctx->ssa->vars[var_num]; |
277 | 737k | if (var->definition >= 0) { |
278 | 486k | if (!check || zend_bitset_in(ctx->instr_dead, var->definition)) { |
279 | 419k | zend_bitset_incl(ctx->instr_worklist, var->definition); |
280 | 419k | } |
281 | 486k | } else if (var->definition_phi) { |
282 | 222k | if (!check || zend_bitset_in(ctx->phi_dead, var_num)) { |
283 | 132k | zend_bitset_incl(ctx->phi_worklist, var_num); |
284 | 132k | } |
285 | 222k | } |
286 | 737k | } |
287 | | |
288 | 9.33k | static inline void add_to_phi_worklist_no_val(const context *ctx, int var_num) { |
289 | 9.33k | const zend_ssa_var *var = &ctx->ssa->vars[var_num]; |
290 | 9.33k | if (var->definition_phi && zend_bitset_in(ctx->phi_dead, var_num)) { |
291 | 1.49k | zend_bitset_incl(ctx->phi_worklist_no_val, var_num); |
292 | 1.49k | } |
293 | 9.33k | } |
294 | | |
295 | 543k | static zend_always_inline void add_operands_to_worklists(const context *ctx, const zend_op *opline, const zend_ssa_op *ssa_op, const zend_ssa *ssa, int check) { |
296 | 543k | if (ssa_op->result_use >= 0) { |
297 | 10.2k | add_to_worklists(ctx, ssa_op->result_use, check); |
298 | 10.2k | } |
299 | 543k | if (ssa_op->op1_use >= 0) { |
300 | 389k | if (!zend_ssa_is_no_val_use(opline, ssa_op, ssa_op->op1_use) |
301 | 10.7k | || (opline->opcode == ZEND_ASSIGN |
302 | 382k | && (ssa->var_info[ssa_op->op1_use].type & MAY_BE_REF) != 0)) { |
303 | 382k | add_to_worklists(ctx, ssa_op->op1_use, check); |
304 | 382k | } else { |
305 | 6.76k | add_to_phi_worklist_no_val(ctx, ssa_op->op1_use); |
306 | 6.76k | } |
307 | 389k | } |
308 | 543k | if (ssa_op->op2_use >= 0) { |
309 | 119k | if (!zend_ssa_is_no_val_use(opline, ssa_op, ssa_op->op2_use) |
310 | 370 | || (opline->opcode == ZEND_FE_FETCH_R |
311 | 119k | && (ssa->var_info[ssa_op->op2_use].type & MAY_BE_REF) != 0)) { |
312 | 119k | add_to_worklists(ctx, ssa_op->op2_use, check); |
313 | 119k | } else { |
314 | 188 | add_to_phi_worklist_no_val(ctx, ssa_op->op2_use); |
315 | 188 | } |
316 | 119k | } |
317 | 543k | } |
318 | | |
319 | 128k | static zend_always_inline void add_phi_sources_to_worklists(const context *ctx, zend_ssa_phi *phi, int check) { |
320 | 128k | const zend_ssa *ssa = ctx->ssa; |
321 | 128k | int source; |
322 | 578k | FOREACH_PHI_SOURCE(phi, source) { |
323 | 578k | add_to_worklists(ctx, source, check); |
324 | 578k | } FOREACH_PHI_SOURCE_END(); |
325 | 128k | } |
326 | | |
327 | 6.97k | static inline bool is_var_dead(const context *ctx, int var_num) { |
328 | 6.97k | const zend_ssa_var *var = &ctx->ssa->vars[var_num]; |
329 | 6.97k | if (var->definition_phi) { |
330 | 540 | return zend_bitset_in(ctx->phi_dead, var_num); |
331 | 6.43k | } else if (var->definition >= 0) { |
332 | 4.26k | return zend_bitset_in(ctx->instr_dead, var->definition); |
333 | 4.26k | } else { |
334 | | /* Variable has no definition, so either the definition has already been removed (var is |
335 | | * dead) or this is one of the implicit variables at the start of the function (for our |
336 | | * purposes live) */ |
337 | 2.17k | return var_num >= ctx->op_array->last_var; |
338 | 2.17k | } |
339 | 6.97k | } |
340 | | |
341 | | // Sometimes we can mark the var as EXT_UNUSED |
342 | 2.56k | static bool try_remove_var_def(const context *ctx, int free_var, int use_chain, const zend_op *opline) { |
343 | 2.56k | if (use_chain >= 0) { |
344 | 294 | return false; |
345 | 294 | } |
346 | 2.27k | zend_ssa_var *var = &ctx->ssa->vars[free_var]; |
347 | 2.27k | int def = var->definition; |
348 | | |
349 | 2.27k | if (def >= 0) { |
350 | 2.23k | zend_ssa_op *def_op = &ctx->ssa->ops[def]; |
351 | | |
352 | 2.23k | if (def_op->result_def == free_var |
353 | 2.23k | && var->phi_use_chain == NULL |
354 | 2.23k | && var->use_chain == (opline - ctx->op_array->opcodes)) { |
355 | 2.22k | zend_op *def_opline = &ctx->op_array->opcodes[def]; |
356 | | |
357 | 2.22k | switch (def_opline->opcode) { |
358 | 129 | case ZEND_ASSIGN: |
359 | 129 | case ZEND_ASSIGN_REF: |
360 | 135 | case ZEND_ASSIGN_DIM: |
361 | 135 | case ZEND_ASSIGN_OBJ: |
362 | 135 | case ZEND_ASSIGN_OBJ_REF: |
363 | 135 | case ZEND_ASSIGN_STATIC_PROP: |
364 | 135 | case ZEND_ASSIGN_STATIC_PROP_REF: |
365 | 203 | case ZEND_ASSIGN_OP: |
366 | 203 | case ZEND_ASSIGN_DIM_OP: |
367 | 203 | case ZEND_ASSIGN_OBJ_OP: |
368 | 203 | case ZEND_ASSIGN_STATIC_PROP_OP: |
369 | 203 | case ZEND_PRE_INC: |
370 | 203 | case ZEND_PRE_DEC: |
371 | 203 | case ZEND_PRE_INC_OBJ: |
372 | 203 | case ZEND_PRE_DEC_OBJ: |
373 | 203 | case ZEND_DO_ICALL: |
374 | 203 | case ZEND_DO_UCALL: |
375 | 203 | case ZEND_DO_FCALL_BY_NAME: |
376 | 203 | case ZEND_DO_FCALL: |
377 | 203 | case ZEND_INCLUDE_OR_EVAL: |
378 | 203 | case ZEND_YIELD: |
379 | 203 | case ZEND_YIELD_FROM: |
380 | 203 | case ZEND_ASSERT_CHECK: |
381 | 203 | def_opline->result_type = IS_UNUSED; |
382 | 203 | def_opline->result.var = 0; |
383 | 203 | def_op->result_def = -1; |
384 | 203 | var->definition = -1; |
385 | 203 | return true; |
386 | 2.02k | default: |
387 | 2.02k | break; |
388 | 2.22k | } |
389 | 2.22k | } |
390 | 2.23k | } |
391 | 2.06k | return false; |
392 | 2.27k | } |
393 | | |
394 | 38.7k | static zend_always_inline bool may_be_refcounted(uint32_t type) { |
395 | 38.7k | return (type & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) != 0; |
396 | 38.7k | } |
397 | | |
398 | 8.16k | static inline bool is_free_of_live_var(const context *ctx, const zend_op *opline, const zend_ssa_op *ssa_op) { |
399 | 8.16k | switch (opline->opcode) { |
400 | 4.14k | case ZEND_FREE: |
401 | | /* It is always safe to remove FREEs of non-refcounted values, even if they are live. */ |
402 | 4.14k | if ((ctx->ssa->var_info[ssa_op->op1_use].type & (MAY_BE_REF|MAY_BE_ANY|MAY_BE_UNDEF)) != 0 |
403 | 4.12k | && !may_be_refcounted(ctx->ssa->var_info[ssa_op->op1_use].type)) { |
404 | 2.36k | return false; |
405 | 2.36k | } |
406 | 1.78k | ZEND_FALLTHROUGH; |
407 | 1.89k | case ZEND_FE_FREE: |
408 | 1.89k | return !is_var_dead(ctx, ssa_op->op1_use); |
409 | 3.91k | default: |
410 | 3.91k | return false; |
411 | 8.16k | } |
412 | 8.16k | } |
413 | | |
414 | | /* Returns whether the instruction has been DCEd */ |
415 | 8.82k | static bool dce_instr(const context *ctx, zend_op *opline, zend_ssa_op *ssa_op) { |
416 | 8.82k | const zend_ssa *ssa = ctx->ssa; |
417 | 8.82k | int free_var = -1; |
418 | 8.82k | uint8_t free_var_type; |
419 | | |
420 | 8.82k | if (opline->opcode == ZEND_NOP) { |
421 | 657 | return false; |
422 | 657 | } |
423 | | |
424 | | /* We mark FREEs as dead, but they're only really dead if the destroyed var is dead */ |
425 | 8.16k | if (is_free_of_live_var(ctx, opline, ssa_op)) { |
426 | 1.78k | return false; |
427 | 1.78k | } |
428 | | |
429 | 6.37k | if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && !is_var_dead(ctx, ssa_op->op1_use)) { |
430 | 2.01k | if (!try_remove_var_def(ctx, ssa_op->op1_use, ssa_op->op1_use_chain, opline)) { |
431 | 1.84k | if (may_be_refcounted(ssa->var_info[ssa_op->op1_use].type) |
432 | 428 | && opline->opcode != ZEND_CASE |
433 | 428 | && opline->opcode != ZEND_CASE_STRICT |
434 | 428 | && opline->opcode != ZEND_COPY_TMP) { |
435 | 134 | free_var = ssa_op->op1_use; |
436 | 134 | free_var_type = opline->op1_type; |
437 | 134 | } |
438 | 1.84k | } |
439 | 2.01k | } |
440 | 6.37k | if ((opline->op2_type & (IS_VAR|IS_TMP_VAR)) && !is_var_dead(ctx, ssa_op->op2_use)) { |
441 | 552 | if (!try_remove_var_def(ctx, ssa_op->op2_use, ssa_op->op2_use_chain, opline)) { |
442 | 513 | if (may_be_refcounted(ssa->var_info[ssa_op->op2_use].type)) { |
443 | 152 | if (free_var >= 0) { |
444 | | // TODO: We can't free two vars. Keep instruction alive. |
445 | 97 | zend_bitset_excl(ctx->instr_dead, opline - ctx->op_array->opcodes); |
446 | 97 | return false; |
447 | 97 | } |
448 | 55 | free_var = ssa_op->op2_use; |
449 | 55 | free_var_type = opline->op2_type; |
450 | 55 | } |
451 | 513 | } |
452 | 552 | } |
453 | | |
454 | 6.27k | zend_ssa_rename_defs_of_instr(ctx->ssa, ssa_op); |
455 | 6.27k | zend_ssa_remove_instr(ctx->ssa, opline, ssa_op); |
456 | | |
457 | 6.27k | if (free_var >= 0) { |
458 | 92 | opline->opcode = ZEND_FREE; |
459 | 92 | opline->op1.var = EX_NUM_TO_VAR(ssa->vars[free_var].var); |
460 | 92 | opline->op1_type = free_var_type; |
461 | | |
462 | 92 | ssa_op->op1_use = free_var; |
463 | 92 | ssa_op->op1_use_chain = ssa->vars[free_var].use_chain; |
464 | 92 | ssa->vars[free_var].use_chain = ssa_op - ssa->ops; |
465 | 92 | return false; |
466 | 92 | } |
467 | 6.18k | return true; |
468 | 6.27k | } |
469 | | |
470 | 66.7k | static inline int get_common_phi_source(const zend_ssa *ssa, zend_ssa_phi *phi) { |
471 | 66.7k | int common_source = -1; |
472 | 66.7k | int source; |
473 | 334k | FOREACH_PHI_SOURCE(phi, source) { |
474 | 334k | if (source == phi->ssa_var) { |
475 | 302 | continue; |
476 | 302 | } |
477 | 133k | if (common_source == -1) { |
478 | 66.7k | common_source = source; |
479 | 66.7k | } else if (common_source != source) { |
480 | 66.6k | return -1; |
481 | 66.6k | } |
482 | 133k | } FOREACH_PHI_SOURCE_END(); |
483 | | |
484 | | /* If all sources are phi->ssa_var this phi must be in an unreachable cycle. |
485 | | * We can't easily drop the phi in that case, as we don't have something to replace it with. |
486 | | * Ideally SCCP would eliminate the whole cycle. */ |
487 | 103 | return common_source; |
488 | 66.7k | } |
489 | | |
490 | 98.5k | static void try_remove_trivial_phi(const context *ctx, zend_ssa_phi *phi) { |
491 | 98.5k | zend_ssa *ssa = ctx->ssa; |
492 | 98.5k | if (phi->pi < 0) { |
493 | | /* Phi assignment with identical source operands */ |
494 | 66.7k | int common_source = get_common_phi_source(ssa, phi); |
495 | 66.7k | if (common_source >= 0) { |
496 | 103 | zend_ssa_rename_var_uses(ssa, phi->ssa_var, common_source, 1); |
497 | 103 | zend_ssa_remove_phi(ssa, phi); |
498 | 103 | } |
499 | 66.7k | } else { |
500 | | /* Pi assignment that is only used in Phi/Pi assignments */ |
501 | | // TODO What if we want to rerun type inference after DCE? Maybe separate this? |
502 | | /*ZEND_ASSERT(phi->sources[0] != -1); |
503 | | if (ssa->vars[phi->ssa_var].use_chain < 0) { |
504 | | zend_ssa_rename_var_uses_keep_types(ssa, phi->ssa_var, phi->sources[0], 1); |
505 | | zend_ssa_remove_phi(ssa, phi); |
506 | | }*/ |
507 | 31.7k | } |
508 | 98.5k | } |
509 | | |
510 | 0 | static inline bool may_break_varargs(const zend_op_array *op_array, const zend_ssa *ssa, const zend_ssa_op *ssa_op) { |
511 | 0 | if (ssa_op->op1_def >= 0 |
512 | 0 | && ssa->vars[ssa_op->op1_def].var < op_array->num_args) { |
513 | 0 | return true; |
514 | 0 | } |
515 | 0 | if (ssa_op->op2_def >= 0 |
516 | 0 | && ssa->vars[ssa_op->op2_def].var < op_array->num_args) { |
517 | 0 | return true; |
518 | 0 | } |
519 | 0 | if (ssa_op->result_def >= 0 |
520 | 0 | && ssa->vars[ssa_op->result_def].var < op_array->num_args) { |
521 | 0 | return true; |
522 | 0 | } |
523 | 0 | return false; |
524 | 0 | } |
525 | | |
526 | 12.7k | static inline bool may_throw_dce_exception(const zend_op *opline) { |
527 | 12.7k | return opline->opcode == ZEND_ADD_ARRAY_ELEMENT && opline->op2_type == IS_UNUSED; |
528 | 12.7k | } |
529 | | |
530 | 6.78k | int dce_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *optimizer_ctx, zend_ssa *ssa, bool reorder_dtor_effects) { |
531 | 6.78k | int i; |
532 | 6.78k | zend_ssa_phi *phi; |
533 | 6.78k | int removed_ops = 0; |
534 | | |
535 | | /* DCE of CV operations that changes arguments may affect vararg functions. */ |
536 | 6.78k | bool has_varargs = (ssa->cfg.flags & ZEND_FUNC_VARARG) != 0; |
537 | | |
538 | 6.78k | context ctx; |
539 | 6.78k | ctx.ssa = ssa; |
540 | 6.78k | ctx.op_array = op_array; |
541 | 6.78k | ctx.reorder_dtor_effects = reorder_dtor_effects; |
542 | | |
543 | 6.78k | void *checkpoint = zend_arena_checkpoint(optimizer_ctx->arena); |
544 | | /* We have no dedicated phi vector, so we use the whole ssa var vector instead */ |
545 | 6.78k | ctx.instr_worklist_len = zend_bitset_len(op_array->last); |
546 | 6.78k | ctx.instr_worklist = zend_arena_calloc(&optimizer_ctx->arena, ctx.instr_worklist_len, sizeof(zend_ulong)); |
547 | 6.78k | ctx.phi_worklist_len = zend_bitset_len(ssa->vars_count); |
548 | 6.78k | ctx.phi_worklist = zend_arena_calloc(&optimizer_ctx->arena, ctx.phi_worklist_len, sizeof(zend_ulong)); |
549 | 6.78k | ctx.phi_worklist_no_val = zend_arena_calloc(&optimizer_ctx->arena, ctx.phi_worklist_len, sizeof(zend_ulong)); |
550 | | |
551 | | /* Optimistically assume all instructions and phis to be dead */ |
552 | 6.78k | ctx.instr_dead = zend_arena_calloc(&optimizer_ctx->arena, ctx.instr_worklist_len, sizeof(zend_ulong)); |
553 | 6.78k | ctx.phi_dead = zend_arena_alloc(&optimizer_ctx->arena, ctx.phi_worklist_len * sizeof(zend_ulong)); |
554 | 6.78k | memset(ctx.phi_dead, 0xff, sizeof(zend_ulong) * ctx.phi_worklist_len); |
555 | | |
556 | | /* Mark non-CV phis as live. Even if the result is unused, we generally cannot remove one |
557 | | * of the producing instructions, as it combines producing the result with control flow. |
558 | | * This can be made more precise if there are any cases where this is not the case. */ |
559 | 188k | FOREACH_PHI(phi) { |
560 | 188k | if (phi->var >= op_array->last_var |
561 | 32.2k | && may_be_refcounted(ssa->var_info[phi->ssa_var].type)) { |
562 | 30.9k | zend_bitset_excl(ctx.phi_dead, phi->ssa_var); |
563 | 30.9k | add_phi_sources_to_worklists(&ctx, phi, 0); |
564 | 30.9k | } |
565 | 188k | } FOREACH_PHI_END(); |
566 | | |
567 | | /* Mark reachable instruction without side effects as dead */ |
568 | 6.78k | int b = ssa->cfg.blocks_count; |
569 | 88.7k | while (b > 0) { |
570 | 81.9k | int op_data = -1; |
571 | | |
572 | 81.9k | b--; |
573 | 81.9k | const zend_basic_block *block = &ssa->cfg.blocks[b]; |
574 | 81.9k | if (!(block->flags & ZEND_BB_REACHABLE)) { |
575 | 3.39k | continue; |
576 | 3.39k | } |
577 | 78.5k | i = block->start + block->len; |
578 | 631k | while (i > block->start) { |
579 | 552k | i--; |
580 | | |
581 | 552k | if (op_array->opcodes[i].opcode == ZEND_OP_DATA) { |
582 | 3.27k | op_data = i; |
583 | 3.27k | continue; |
584 | 3.27k | } |
585 | | |
586 | 549k | if (zend_bitset_in(ctx.instr_worklist, i)) { |
587 | 401k | zend_bitset_excl(ctx.instr_worklist, i); |
588 | 401k | add_operands_to_worklists(&ctx, &op_array->opcodes[i], &ssa->ops[i], ssa, 0); |
589 | 401k | if (op_data >= 0) { |
590 | 1.91k | add_operands_to_worklists(&ctx, &op_array->opcodes[op_data], &ssa->ops[op_data], ssa, 0); |
591 | 1.91k | } |
592 | 401k | } else if (may_have_side_effects(op_array, ssa, &op_array->opcodes[i], &ssa->ops[i], ctx.reorder_dtor_effects) |
593 | 23.9k | || (zend_may_throw(&op_array->opcodes[i], &ssa->ops[i], op_array, ssa) |
594 | 12.7k | && !may_throw_dce_exception(&op_array->opcodes[i])) |
595 | 136k | || (has_varargs && may_break_varargs(op_array, ssa, &ssa->ops[i]))) { |
596 | 136k | if (op_array->opcodes[i].opcode == ZEND_NEW |
597 | 20 | && op_array->opcodes[i+1].opcode == ZEND_DO_FCALL |
598 | 20 | && ssa->ops[i].result_def >= 0 |
599 | 20 | && ssa->vars[ssa->ops[i].result_def].escape_state == ESCAPE_STATE_NO_ESCAPE) { |
600 | 20 | zend_bitset_incl(ctx.instr_dead, i); |
601 | 20 | zend_bitset_incl(ctx.instr_dead, i+1); |
602 | 136k | } else { |
603 | 136k | add_operands_to_worklists(&ctx, &op_array->opcodes[i], &ssa->ops[i], ssa, 0); |
604 | 136k | if (op_data >= 0) { |
605 | 1.32k | add_operands_to_worklists(&ctx, &op_array->opcodes[op_data], &ssa->ops[op_data], ssa, 0); |
606 | 1.32k | } |
607 | 136k | } |
608 | 136k | } else { |
609 | 11.2k | zend_bitset_incl(ctx.instr_dead, i); |
610 | 11.2k | if (op_data >= 0) { |
611 | 38 | zend_bitset_incl(ctx.instr_dead, op_data); |
612 | 38 | } |
613 | 11.2k | } |
614 | 549k | op_data = -1; |
615 | 549k | } |
616 | 78.5k | } |
617 | | |
618 | | /* Propagate liveness backwards to all definitions of used vars */ |
619 | 8.42k | while (!zend_bitset_empty(ctx.instr_worklist, ctx.instr_worklist_len) |
620 | 7.93k | || !zend_bitset_empty(ctx.phi_worklist, ctx.phi_worklist_len)) { |
621 | 4.11k | while ((i = zend_bitset_pop_first(ctx.instr_worklist, ctx.instr_worklist_len)) >= 0) { |
622 | 2.47k | zend_bitset_excl(ctx.instr_dead, i); |
623 | 2.47k | add_operands_to_worklists(&ctx, &op_array->opcodes[i], &ssa->ops[i], ssa, 1); |
624 | 2.47k | if (i < op_array->last |
625 | 2.47k | && (op_array->opcodes[i+1].opcode == ZEND_OP_DATA |
626 | 2.44k | || (op_array->opcodes[i].opcode == ZEND_NEW |
627 | 30 | && op_array->opcodes[i+1].opcode == ZEND_DO_FCALL))) { |
628 | 30 | zend_bitset_excl(ctx.instr_dead, i+1); |
629 | 30 | add_operands_to_worklists(&ctx, &op_array->opcodes[i+1], &ssa->ops[i+1], ssa, 1); |
630 | 30 | } |
631 | 2.47k | } |
632 | 98.8k | while ((i = zend_bitset_pop_first(ctx.phi_worklist, ctx.phi_worklist_len)) >= 0) { |
633 | 97.1k | zend_bitset_excl(ctx.phi_dead, i); |
634 | 97.1k | zend_bitset_excl(ctx.phi_worklist_no_val, i); |
635 | 97.1k | add_phi_sources_to_worklists(&ctx, ssa->vars[i].definition_phi, 1); |
636 | 97.1k | } |
637 | 1.64k | } |
638 | | |
639 | | /* Eliminate dead instructions */ |
640 | 89.3k | ZEND_BITSET_FOREACH(ctx.instr_dead, ctx.instr_worklist_len, i) { |
641 | 8.82k | removed_ops += dce_instr(&ctx, &op_array->opcodes[i], &ssa->ops[i]); |
642 | 8.82k | } ZEND_BITSET_FOREACH_END(); |
643 | | |
644 | | /* Improper uses don't count as "uses" for the purpose of instruction elimination, |
645 | | * but we have to retain phis defining them. |
646 | | * Propagate this information backwards, marking any phi with an improperly used |
647 | | * target as non-dead. */ |
648 | 8.01k | while ((i = zend_bitset_pop_first(ctx.phi_worklist_no_val, ctx.phi_worklist_len)) >= 0) { |
649 | 1.23k | zend_ssa_phi *phi = ssa->vars[i].definition_phi; |
650 | 1.23k | int source; |
651 | 1.23k | zend_bitset_excl(ctx.phi_dead, i); |
652 | 6.00k | FOREACH_PHI_SOURCE(phi, source) { |
653 | 6.00k | add_to_phi_worklist_no_val(&ctx, source); |
654 | 6.00k | } FOREACH_PHI_SOURCE_END(); |
655 | 1.23k | } |
656 | | |
657 | | /* Now collect the actually dead phis */ |
658 | 188k | FOREACH_PHI(phi) { |
659 | 188k | if (zend_bitset_in(ctx.phi_dead, phi->ssa_var)) { |
660 | 1.20k | zend_ssa_remove_uses_of_var(ssa, phi->ssa_var); |
661 | 1.20k | zend_ssa_remove_phi(ssa, phi); |
662 | 98.5k | } else { |
663 | | /* Remove trivial phis (phis with identical source operands) */ |
664 | 98.5k | try_remove_trivial_phi(&ctx, phi); |
665 | 98.5k | } |
666 | 188k | } FOREACH_PHI_END(); |
667 | | |
668 | 6.78k | zend_arena_release(&optimizer_ctx->arena, checkpoint); |
669 | | |
670 | 6.78k | return removed_ops; |
671 | 6.78k | } |