Line data Source code
1 : /* Dead code elimination pass for the GNU compiler.
2 : Copyright (C) 2002-2026 Free Software Foundation, Inc.
3 : Contributed by Ben Elliston <bje@redhat.com>
4 : and Andrew MacLeod <amacleod@redhat.com>
5 : Adapted to use control dependence by Steven Bosscher, SUSE Labs.
6 :
7 : This file is part of GCC.
8 :
9 : GCC is free software; you can redistribute it and/or modify it
10 : under the terms of the GNU General Public License as published by the
11 : Free Software Foundation; either version 3, or (at your option) any
12 : later version.
13 :
14 : GCC is distributed in the hope that it will be useful, but WITHOUT
15 : ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 : for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with GCC; see the file COPYING3. If not see
21 : <http://www.gnu.org/licenses/>. */
22 :
23 : /* Dead code elimination.
24 :
25 : References:
26 :
27 : Building an Optimizing Compiler,
28 : Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
29 :
30 : Advanced Compiler Design and Implementation,
31 : Steven Muchnick, Morgan Kaufmann, 1997, Section 18.10.
32 :
33 : Dead-code elimination is the removal of statements which have no
34 : impact on the program's output. "Dead statements" have no impact
35 : on the program's output, while "necessary statements" may have
36 : impact on the output.
37 :
38 : The algorithm consists of three phases:
39 : 1. Marking as necessary all statements known to be necessary,
40 : e.g. most function calls, writing a value to memory, etc;
41 : 2. Propagating necessary statements, e.g., the statements
42 : giving values to operands in necessary statements; and
43 : 3. Removing dead statements. */
44 :
45 : #include "config.h"
46 : #include "system.h"
47 : #include "coretypes.h"
48 : #include "backend.h"
49 : #include "rtl.h"
50 : #include "tree.h"
51 : #include "gimple.h"
52 : #include "cfghooks.h"
53 : #include "tree-pass.h"
54 : #include "ssa.h"
55 : #include "gimple-pretty-print.h"
56 : #include "fold-const.h"
57 : #include "calls.h"
58 : #include "cfganal.h"
59 : #include "tree-eh.h"
60 : #include "gimplify.h"
61 : #include "gimple-iterator.h"
62 : #include "tree-cfg.h"
63 : #include "tree-ssa-loop-niter.h"
64 : #include "tree-into-ssa.h"
65 : #include "tree-dfa.h"
66 : #include "cfgloop.h"
67 : #include "tree-scalar-evolution.h"
68 : #include "tree-ssa-propagate.h"
69 : #include "gimple-fold.h"
70 : #include "tree-ssa.h"
71 : #include "ipa-modref-tree.h"
72 : #include "ipa-modref.h"
73 : #include "memmodel.h"
74 :
75 : static struct stmt_stats
76 : {
77 : int total;
78 : int total_phis;
79 : int removed;
80 : int removed_phis;
81 : } stats;
82 :
83 : #define STMT_NECESSARY GF_PLF_1
84 :
85 : static vec<gimple *> worklist;
86 :
87 : /* Vector indicating an SSA name has already been processed and marked
88 : as necessary. */
89 : static sbitmap processed;
90 :
91 : /* Vector indicating that the last statement of a basic block has already
92 : been marked as necessary. */
93 : static sbitmap last_stmt_necessary;
94 :
95 : /* Vector indicating that BB contains statements that are live. */
96 : static sbitmap bb_contains_live_stmts;
97 :
98 : /* Before we can determine whether a control branch is dead, we need to
99 : compute which blocks are control dependent on which edges.
100 :
101 : We expect each block to be control dependent on very few edges so we
102 : use a bitmap for each block recording its edges. An array holds the
103 : bitmap. The Ith bit in the bitmap is set if that block is dependent
104 : on the Ith edge. */
105 : static control_dependences *cd;
106 :
107 : /* Vector indicating that a basic block has already had all the edges
108 : processed that it is control dependent on. */
109 : static sbitmap visited_control_parents;
110 :
111 : /* TRUE if this pass alters the CFG (by removing control statements).
112 : FALSE otherwise.
113 :
114 : If this pass alters the CFG, then it will arrange for the dominators
115 : to be recomputed. */
116 : static bool cfg_altered;
117 :
118 : /* When non-NULL holds map from basic block index into the postorder. */
119 : static int *bb_postorder;
120 :
121 :
122 : /* True if we should treat any stmt with a vdef as necessary. */
123 :
124 : static inline bool
125 278539668 : keep_all_vdefs_p ()
126 : {
127 278539668 : return optimize_debug;
128 : }
129 :
130 : /* 1 if CALLEE is the function __cxa_atexit.
131 : 2 if CALLEE is the function __aeabi_atexit.
132 : 0 otherwise. */
133 :
134 : static inline int
135 88766123 : is_cxa_atexit (const_tree callee)
136 : {
137 88766123 : if (callee != NULL_TREE
138 88766123 : && strcmp (IDENTIFIER_POINTER (DECL_NAME (callee)), "__cxa_atexit") == 0)
139 : return 1;
140 88643292 : if (callee != NULL_TREE
141 88643292 : && strcmp (IDENTIFIER_POINTER (DECL_NAME (callee)), "__aeabi_atexit") == 0)
142 0 : return 2;
143 : return 0;
144 : }
145 :
146 : /* True if STMT is a call to __cxa_atexit (or __aeabi_atexit)
147 : and the function argument to that call is a const or pure
148 : non-looping function decl. */
149 :
150 : static inline bool
151 88766123 : is_removable_cxa_atexit_call (gimple *stmt)
152 : {
153 88766123 : tree callee = gimple_call_fndecl (stmt);
154 88766123 : int functype = is_cxa_atexit (callee);
155 88766123 : if (!functype)
156 : return false;
157 122831 : if (gimple_call_num_args (stmt) != 3)
158 : return false;
159 :
160 : /* The function argument is the 1st argument for __cxa_atexit
161 : or the 2nd argument for __eabi_atexit. */
162 245662 : tree arg = gimple_call_arg (stmt, functype == 2 ? 1 : 0);
163 122831 : if (TREE_CODE (arg) != ADDR_EXPR)
164 : return false;
165 122831 : arg = TREE_OPERAND (arg, 0);
166 122831 : if (TREE_CODE (arg) != FUNCTION_DECL)
167 : return false;
168 122831 : int flags = flags_from_decl_or_type (arg);
169 :
170 : /* If the function is noreturn then it cannot be removed. */
171 122831 : if (flags & ECF_NORETURN)
172 : return false;
173 :
174 : /* The function needs to be const or pure and non looping. */
175 122673 : return (flags & (ECF_CONST|ECF_PURE))
176 122673 : && !(flags & ECF_LOOPING_CONST_OR_PURE);
177 : }
178 :
179 : /* If STMT is not already marked necessary, mark it, and add it to the
180 : worklist if ADD_TO_WORKLIST is true. */
181 :
182 : static inline void
183 498368102 : mark_stmt_necessary (gimple *stmt, bool add_to_worklist)
184 : {
185 498368102 : gcc_assert (stmt);
186 :
187 498368102 : if (gimple_plf (stmt, STMT_NECESSARY))
188 : return;
189 :
190 498367901 : if (dump_file && (dump_flags & TDF_DETAILS))
191 : {
192 852 : fprintf (dump_file, "Marking useful stmt: ");
193 852 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
194 852 : fprintf (dump_file, "\n");
195 : }
196 :
197 498367901 : gimple_set_plf (stmt, STMT_NECESSARY, true);
198 498367901 : if (add_to_worklist)
199 110644562 : worklist.safe_push (stmt);
200 110644562 : if (add_to_worklist && bb_contains_live_stmts && !is_gimple_debug (stmt))
201 38823321 : bitmap_set_bit (bb_contains_live_stmts, gimple_bb (stmt)->index);
202 : }
203 :
204 :
205 : /* Mark the statement defining operand OP as necessary. */
206 :
207 : static inline void
208 348987075 : mark_operand_necessary (tree op)
209 : {
210 348987075 : gimple *stmt;
211 348987075 : int ver;
212 :
213 348987075 : gcc_assert (op);
214 :
215 348987075 : ver = SSA_NAME_VERSION (op);
216 348987075 : if (bitmap_bit_p (processed, ver))
217 : {
218 127702982 : stmt = SSA_NAME_DEF_STMT (op);
219 127702982 : gcc_assert (gimple_nop_p (stmt)
220 : || gimple_plf (stmt, STMT_NECESSARY));
221 190568327 : return;
222 : }
223 221284093 : bitmap_set_bit (processed, ver);
224 :
225 221284093 : stmt = SSA_NAME_DEF_STMT (op);
226 221284093 : gcc_assert (stmt);
227 :
228 221284093 : if (gimple_plf (stmt, STMT_NECESSARY) || gimple_nop_p (stmt))
229 : return;
230 :
231 158418748 : if (dump_file && (dump_flags & TDF_DETAILS))
232 : {
233 266 : fprintf (dump_file, "marking necessary through ");
234 266 : print_generic_expr (dump_file, op);
235 266 : fprintf (dump_file, " stmt ");
236 266 : print_gimple_stmt (dump_file, stmt, 0);
237 : }
238 :
239 158418748 : gimple_set_plf (stmt, STMT_NECESSARY, true);
240 158418748 : if (bb_contains_live_stmts)
241 54304222 : bitmap_set_bit (bb_contains_live_stmts, gimple_bb (stmt)->index);
242 158418748 : worklist.safe_push (stmt);
243 : }
244 :
245 : /* Return true if STMT is a call to allocation function that can be
246 : optimized out if the memory block is never used for anything else
247 : than NULL pointer check or free.
248 : If NON_NULL_CHECK is false, we can further assume that return value
249 : is never checked to be non-NULL.
250 : Don't return true if it is called with constant size (or sizes for calloc)
251 : and the size is excessively large (larger than PTRDIFF_MAX, for calloc
252 : either argument larger than PTRDIFF_MAX or both constant and their product
253 : larger than PTRDIFF_MAX). */
254 :
255 : static bool
256 37077473 : is_removable_allocation_p (gcall *stmt, bool non_null_check)
257 : {
258 37077473 : int arg = -1;
259 37077473 : tree callee = gimple_call_fndecl (stmt), a1, a2;
260 37077473 : if (callee != NULL_TREE
261 37077473 : && fndecl_built_in_p (callee, BUILT_IN_NORMAL))
262 9415540 : switch (DECL_FUNCTION_CODE (callee))
263 : {
264 639192 : case BUILT_IN_MALLOC:
265 639192 : arg = 1;
266 639192 : goto do_malloc;
267 150 : case BUILT_IN_ALIGNED_ALLOC:
268 150 : arg = 2;
269 150 : goto do_malloc;
270 6082 : case BUILT_IN_CALLOC:
271 6082 : arg = 3;
272 6082 : goto do_malloc;
273 122471 : CASE_BUILT_IN_ALLOCA:
274 122471 : arg = 1;
275 122471 : goto do_malloc;
276 : case BUILT_IN_STRDUP:
277 : case BUILT_IN_STRNDUP:
278 : arg = 0;
279 : /* FALLTHRU */
280 772128 : do_malloc:
281 772128 : if (non_null_check)
282 : {
283 131629 : if (flag_malloc_dce <= 1)
284 : return false;
285 : }
286 640499 : else if (!flag_malloc_dce)
287 : return false;
288 : break;
289 :
290 : case BUILT_IN_GOMP_ALLOC:
291 37077161 : arg = 2;
292 : break;
293 :
294 : default:;
295 : }
296 :
297 37077161 : if (arg == -1
298 37077161 : && callee != NULL_TREE
299 34082463 : && flag_allocation_dce
300 34079019 : && gimple_call_from_new_or_delete (stmt)
301 38390019 : && DECL_IS_REPLACEABLE_OPERATOR_NEW_P (callee))
302 : arg = 1;
303 :
304 36646215 : switch (arg)
305 : {
306 : case -1:
307 : return false;
308 : case 0:
309 : return true;
310 1198406 : case 1:
311 1198406 : case 2:
312 1198406 : if (gimple_call_num_args (stmt) < (unsigned) arg)
313 : return false;
314 1198400 : a1 = gimple_call_arg (stmt, arg - 1);
315 1198400 : if (tree_fits_uhwi_p (a1)
316 1198400 : && (tree_to_uhwi (a1)
317 620428 : > tree_to_uhwi (TYPE_MAX_VALUE (ptrdiff_type_node))))
318 : return false;
319 : return true;
320 6082 : case 3:
321 6082 : if (gimple_call_num_args (stmt) < 2)
322 : return false;
323 6082 : a1 = gimple_call_arg (stmt, 0);
324 6082 : a2 = gimple_call_arg (stmt, 1);
325 6082 : if (tree_fits_uhwi_p (a1)
326 6082 : && (tree_to_uhwi (a1)
327 5073 : > tree_to_uhwi (TYPE_MAX_VALUE (ptrdiff_type_node))))
328 : return false;
329 6060 : if (tree_fits_uhwi_p (a2)
330 6060 : && (tree_to_uhwi (a2)
331 5332 : > tree_to_uhwi (TYPE_MAX_VALUE (ptrdiff_type_node))))
332 : return false;
333 12076 : if (TREE_CODE (a1) == INTEGER_CST
334 5041 : && TREE_CODE (a2) == INTEGER_CST
335 10664 : && (wi::to_widest (a1) * wi::to_widest (a2)
336 10664 : > tree_to_uhwi (TYPE_MAX_VALUE (ptrdiff_type_node))))
337 : return false;
338 : return true;
339 : default:
340 : gcc_unreachable ();
341 : }
342 : }
343 :
344 : /* Return true if STMT is an assignment
345 : foo = (ptr != NULL)
346 : foo = (ptr == NULL)
347 : or a conditional
348 : if (ptr != NULL)
349 : if (ptr == NULL)
350 : where ptr was returned by a removable allocation function. */
351 :
352 : static bool
353 249388949 : checks_return_value_of_removable_allocation_p (gimple *stmt)
354 : {
355 249388949 : enum tree_code code;
356 249388949 : tree ptr, value;
357 :
358 249388949 : if (gassign *assign = dyn_cast <gassign *> (stmt))
359 : {
360 168403604 : code = gimple_assign_rhs_code (assign);
361 168403604 : ptr = gimple_assign_rhs1 (assign);
362 168403604 : value = gimple_assign_rhs2 (assign);
363 : }
364 281308292 : else if (gcond *cond = dyn_cast <gcond *> (stmt))
365 : {
366 32052279 : code = gimple_cond_code (cond);
367 32052279 : ptr = gimple_cond_lhs (cond);
368 32052279 : value = gimple_cond_rhs (cond);
369 : }
370 : else
371 : return false;
372 :
373 200455883 : if (code != EQ_EXPR && code != NE_EXPR)
374 : return false;
375 :
376 26203788 : if (!integer_zerop (value) || TREE_CODE (ptr) != SSA_NAME)
377 : return false;
378 :
379 14101849 : gcall *def_stmt = dyn_cast <gcall *> (SSA_NAME_DEF_STMT (ptr));
380 3520366 : return def_stmt && is_removable_allocation_p (def_stmt, true);
381 : }
382 :
383 :
384 : /* Mark STMT as necessary if it obviously is. Add it to the worklist if
385 : it can make other statements necessary.
386 :
387 : If AGGRESSIVE is false, control statements are conservatively marked as
388 : necessary. */
389 :
390 : static void
391 639913483 : mark_stmt_if_obviously_necessary (gimple *stmt, bool aggressive)
392 : {
393 : /* Statements that are implicitly live. Most function calls, asm
394 : and return statements are required. Labels and GIMPLE_BIND nodes
395 : are kept because they are control flow, and we have no way of
396 : knowing whether they can be removed. DCE can eliminate all the
397 : other statements in a block, and CFG can then remove the block
398 : and labels. */
399 639913483 : switch (gimple_code (stmt))
400 : {
401 7050009 : case GIMPLE_PREDICT:
402 7050009 : case GIMPLE_LABEL:
403 7050009 : mark_stmt_necessary (stmt, false);
404 7050009 : return;
405 :
406 10382731 : case GIMPLE_ASM:
407 10382731 : case GIMPLE_RESX:
408 10382731 : case GIMPLE_RETURN:
409 10382731 : mark_stmt_necessary (stmt, true);
410 10382731 : return;
411 :
412 38581629 : case GIMPLE_CALL:
413 38581629 : {
414 38581629 : gcall *call = as_a <gcall *> (stmt);
415 :
416 : /* Never elide a noreturn call we pruned control-flow for.
417 : Same for statements that can alter control flow in unpredictable
418 : ways. */
419 38581629 : if (gimple_call_ctrl_altering_p (call))
420 : {
421 5326302 : mark_stmt_necessary (call, true);
422 5326302 : return;
423 : }
424 :
425 33255327 : if (is_removable_allocation_p (call, false))
426 : return;
427 :
428 :
429 : /* For __cxa_atexit calls, don't mark as necessary right away. */
430 32380381 : if (is_removable_cxa_atexit_call (call))
431 : return;
432 :
433 : /* A relaxed atomic load with no LHS has no observable effect:
434 : the value is discarded and relaxed ordering provides no
435 : inter-thread synchronisation guarantee. Don't mark it
436 : necessary so DCE can remove it. */
437 32379892 : if (gimple_call_builtin_p (call, BUILT_IN_NORMAL))
438 6519422 : switch (DECL_FUNCTION_CODE (gimple_call_fndecl (call)))
439 : {
440 430835 : case BUILT_IN_ATOMIC_LOAD_1:
441 430835 : case BUILT_IN_ATOMIC_LOAD_2:
442 430835 : case BUILT_IN_ATOMIC_LOAD_4:
443 430835 : case BUILT_IN_ATOMIC_LOAD_8:
444 430835 : case BUILT_IN_ATOMIC_LOAD_16:
445 430835 : {
446 430835 : tree model_arg = gimple_call_arg (call, 1);
447 430835 : if (TREE_CODE (model_arg) == INTEGER_CST
448 430835 : && is_mm_relaxed (memmodel_from_int (tree_to_uhwi (model_arg))))
449 : return;
450 : break;
451 : }
452 : default:
453 : break;
454 : }
455 :
456 : /* IFN_GOACC_LOOP calls are necessary in that they are used to
457 : represent parameter (i.e. step, bound) of a lowered OpenACC
458 : partitioned loop. But this kind of partitioned loop might not
459 : survive from aggressive loop removal for it has loop exit and
460 : is assumed to be finite. Therefore, we need to explicitly mark
461 : these calls. (An example is libgomp.oacc-c-c++-common/pr84955.c) */
462 32283013 : if (gimple_call_internal_p (call, IFN_GOACC_LOOP))
463 : {
464 27733 : mark_stmt_necessary (call, true);
465 27733 : return;
466 : }
467 : break;
468 : }
469 :
470 381066417 : case GIMPLE_DEBUG:
471 : /* Debug temps without a value are not useful. ??? If we could
472 : easily locate the debug temp bind stmt for a use thereof,
473 : would could refrain from marking all debug temps here, and
474 : mark them only if they're used. */
475 381066417 : if (gimple_debug_nonbind_marker_p (stmt)
476 296958853 : || !gimple_debug_bind_p (stmt)
477 295907178 : || gimple_debug_bind_has_value_p (stmt)
478 514073614 : || TREE_CODE (gimple_debug_bind_get_var (stmt)) != DEBUG_EXPR_DECL)
479 380673330 : mark_stmt_necessary (stmt, false);
480 : return;
481 :
482 2104 : case GIMPLE_GOTO:
483 2104 : gcc_assert (!simple_goto_p (stmt));
484 2104 : mark_stmt_necessary (stmt, true);
485 2104 : return;
486 :
487 32088528 : case GIMPLE_COND:
488 32088528 : gcc_assert (EDGE_COUNT (gimple_bb (stmt)->succs) == 2);
489 : /* Fall through. */
490 :
491 32252576 : case GIMPLE_SWITCH:
492 32252576 : if (! aggressive)
493 21342558 : mark_stmt_necessary (stmt, true);
494 : break;
495 :
496 170537457 : case GIMPLE_ASSIGN:
497 : /* Mark indirect CLOBBERs to be lazily removed if their SSA operands
498 : do not prevail. That also makes control flow leading to them
499 : not necessary in aggressive mode. */
500 181538749 : if (gimple_clobber_p (stmt) && !zero_ssa_operands (stmt, SSA_OP_USE))
501 : return;
502 : break;
503 :
504 : default:
505 : break;
506 : }
507 :
508 : /* If the statement has volatile operands, it needs to be preserved.
509 : Same for statements that can alter control flow in unpredictable
510 : ways. */
511 234209155 : if (gimple_has_side_effects (stmt) || is_ctrl_altering_stmt (stmt))
512 : {
513 42679110 : mark_stmt_necessary (stmt, true);
514 42679110 : return;
515 : }
516 :
517 : /* If a statement could throw, it can be deemed necessary unless we
518 : are allowed to remove dead EH. Test this after checking for
519 : new/delete operators since we always elide their EH. */
520 191530045 : if (!cfun->can_delete_dead_exceptions
521 191530045 : && stmt_could_throw_p (cfun, stmt))
522 : {
523 5824050 : mark_stmt_necessary (stmt, true);
524 5824050 : return;
525 : }
526 :
527 231542786 : if ((gimple_vdef (stmt) && keep_all_vdefs_p ())
528 231524038 : || stmt_may_clobber_global_p (stmt, false))
529 : {
530 14189172 : mark_stmt_necessary (stmt, true);
531 14189172 : return;
532 : }
533 :
534 : return;
535 : }
536 :
537 :
538 : /* Mark the last statement of BB as necessary. */
539 :
540 : static bool
541 44013014 : mark_last_stmt_necessary (basic_block bb)
542 : {
543 44013014 : if (!bitmap_set_bit (last_stmt_necessary, bb->index))
544 : return true;
545 :
546 17375014 : bitmap_set_bit (bb_contains_live_stmts, bb->index);
547 :
548 : /* We actually mark the statement only if it is a control statement. */
549 17375014 : gimple *stmt = *gsi_last_bb (bb);
550 17375014 : if (stmt && is_ctrl_stmt (stmt))
551 : {
552 10871003 : mark_stmt_necessary (stmt, true);
553 10871003 : return true;
554 : }
555 : return false;
556 : }
557 :
558 :
559 : /* Mark control dependent edges of BB as necessary. We have to do this only
560 : once for each basic block so we set the appropriate bit after we're done.
561 :
562 : When IGNORE_SELF is true, ignore BB in the list of control dependences. */
563 :
564 : static void
565 32920876 : mark_control_dependent_edges_necessary (basic_block bb, bool ignore_self)
566 : {
567 32920876 : bitmap_iterator bi;
568 32920876 : unsigned edge_number;
569 32920876 : bool skipped = false;
570 :
571 32920876 : gcc_assert (bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
572 :
573 32920876 : if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
574 3636374 : return;
575 :
576 71916081 : EXECUTE_IF_SET_IN_BITMAP (cd->get_edges_dependent_on (bb->index),
577 : 0, edge_number, bi)
578 : {
579 42631579 : basic_block cd_bb = cd->get_edge_src (edge_number);
580 :
581 42631579 : if (ignore_self && cd_bb == bb)
582 : {
583 76230 : skipped = true;
584 76230 : continue;
585 : }
586 :
587 42555349 : if (!mark_last_stmt_necessary (cd_bb))
588 6501479 : mark_control_dependent_edges_necessary (cd_bb, false);
589 : }
590 :
591 29284502 : if (!skipped)
592 29208272 : bitmap_set_bit (visited_control_parents, bb->index);
593 : }
594 :
595 :
596 : /* Find obviously necessary statements. These are things like most function
597 : calls, and stores to file level variables.
598 :
599 : If EL is NULL, control statements are conservatively marked as
600 : necessary. Otherwise it contains the list of edges used by control
601 : dependence analysis. */
602 :
603 : static void
604 8337506 : find_obviously_necessary_stmts (bool aggressive)
605 : {
606 8337506 : basic_block bb;
607 8337506 : gimple_stmt_iterator gsi;
608 8337506 : edge e;
609 8337506 : gimple *phi, *stmt;
610 8337506 : int flags;
611 :
612 92856965 : FOR_EACH_BB_FN (bb, cfun)
613 : {
614 : /* PHI nodes are never inherently necessary. */
615 120330164 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
616 : {
617 35810705 : phi = gsi_stmt (gsi);
618 35810705 : gimple_set_plf (phi, STMT_NECESSARY, false);
619 : }
620 :
621 : /* Check all statements in the block. */
622 808952401 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
623 : {
624 639913483 : stmt = gsi_stmt (gsi);
625 639913483 : gimple_set_plf (stmt, STMT_NECESSARY, false);
626 639913483 : mark_stmt_if_obviously_necessary (stmt, aggressive);
627 : }
628 : }
629 :
630 : /* Pure and const functions are finite and thus have no infinite loops in
631 : them. */
632 8337506 : flags = flags_from_decl_or_type (current_function_decl);
633 8337506 : if ((flags & (ECF_CONST|ECF_PURE)) && !(flags & ECF_LOOPING_CONST_OR_PURE))
634 975142 : return;
635 :
636 : /* Prevent the empty possibly infinite loops from being removed. This is
637 : needed to make the logic in remove_dead_stmt work to identify the
638 : correct edge to keep when removing a controlling condition. */
639 7362364 : if (aggressive)
640 : {
641 3442894 : if (mark_irreducible_loops ())
642 334344 : FOR_EACH_BB_FN (bb, cfun)
643 : {
644 327856 : edge_iterator ei;
645 815029 : FOR_EACH_EDGE (e, ei, bb->succs)
646 487173 : if ((e->flags & EDGE_DFS_BACK)
647 36943 : && (e->flags & EDGE_IRREDUCIBLE_LOOP))
648 : {
649 12521 : if (dump_file)
650 0 : fprintf (dump_file, "Marking back edge of irreducible "
651 0 : "loop %i->%i\n", e->src->index, e->dest->index);
652 12521 : mark_control_dependent_edges_necessary (e->dest, false);
653 : }
654 : }
655 :
656 12187920 : for (auto loop : loops_list (cfun, 0))
657 : /* For loops without an exit do not mark any condition. */
658 1859238 : if (loop->exits->next->e && !finite_loop_p (loop))
659 : {
660 284274 : if (dump_file)
661 1 : fprintf (dump_file, "cannot prove finiteness of loop %i\n",
662 : loop->num);
663 284274 : mark_control_dependent_edges_necessary (loop->latch, false);
664 3442894 : }
665 : }
666 : }
667 :
668 :
669 : /* Return true if REF is based on an aliased base, otherwise false. */
670 :
671 : static bool
672 100789570 : ref_may_be_aliased (tree ref)
673 : {
674 100789570 : if (TREE_CODE (ref) == WITH_SIZE_EXPR)
675 0 : ref = TREE_OPERAND (ref, 0);
676 180999749 : while (handled_component_p (ref))
677 80210179 : ref = TREE_OPERAND (ref, 0);
678 100789570 : if ((TREE_CODE (ref) == MEM_REF || TREE_CODE (ref) == TARGET_MEM_REF)
679 100789570 : && TREE_CODE (TREE_OPERAND (ref, 0)) == ADDR_EXPR)
680 16095741 : ref = TREE_OPERAND (TREE_OPERAND (ref, 0), 0);
681 100789570 : return !(DECL_P (ref)
682 67245113 : && !may_be_aliased (ref));
683 : }
684 :
685 : static bitmap visited = NULL;
686 : static unsigned int longest_chain = 0;
687 : static unsigned int total_chain = 0;
688 : static unsigned int nr_walks = 0;
689 : static bool chain_ovfl = false;
690 :
691 : /* Worker for the walker that marks reaching definitions of REF,
692 : which is based on a non-aliased decl, necessary. It returns
693 : true whenever the defining statement of the current VDEF is
694 : a kill for REF, as no dominating may-defs are necessary for REF
695 : anymore. DATA points to the basic-block that contains the
696 : stmt that refers to REF. */
697 :
698 : static bool
699 40890947 : mark_aliased_reaching_defs_necessary_1 (ao_ref *ref, tree vdef, void *data)
700 : {
701 40890947 : gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
702 :
703 : /* All stmts we visit are necessary. */
704 40890947 : if (! gimple_clobber_p (def_stmt))
705 40680350 : mark_operand_necessary (vdef);
706 :
707 : /* If the stmt lhs kills ref, then we can stop walking. */
708 40890947 : if (gimple_has_lhs (def_stmt)
709 29600331 : && TREE_CODE (gimple_get_lhs (def_stmt)) != SSA_NAME
710 : /* The assignment is not necessarily carried out if it can throw
711 : and we can catch it in the current function where we could inspect
712 : the previous value.
713 : ??? We only need to care about the RHS throwing. For aggregate
714 : assignments or similar calls and non-call exceptions the LHS
715 : might throw as well. */
716 38728881 : && !stmt_can_throw_internal (cfun, def_stmt))
717 : {
718 25296034 : tree base, lhs = gimple_get_lhs (def_stmt);
719 25296034 : poly_int64 size, offset, max_size;
720 25296034 : bool reverse;
721 25296034 : ao_ref_base (ref);
722 25296034 : base
723 25296034 : = get_ref_base_and_extent (lhs, &offset, &size, &max_size, &reverse);
724 : /* We can get MEM[symbol: sZ, index: D.8862_1] here,
725 : so base == refd->base does not always hold. */
726 25296034 : if (base == ref->base)
727 : {
728 : /* For a must-alias check we need to be able to constrain
729 : the accesses properly. */
730 23232020 : if (known_eq (size, max_size)
731 23232020 : && known_subrange_p (ref->offset, ref->max_size, offset, size))
732 8775777 : return true;
733 : /* Or they need to be exactly the same. */
734 14456911 : else if (ref->ref
735 : /* Make sure there is no induction variable involved
736 : in the references (gcc.c-torture/execute/pr42142.c).
737 : The simplest way is to check if the kill dominates
738 : the use. */
739 : /* But when both are in the same block we cannot
740 : easily tell whether we came from a backedge
741 : unless we decide to compute stmt UIDs
742 : (see PR58246). */
743 14456911 : && (basic_block) data != gimple_bb (def_stmt)
744 6852056 : && dominated_by_p (CDI_DOMINATORS, (basic_block) data,
745 6852056 : gimple_bb (def_stmt))
746 18690128 : && operand_equal_p (ref->ref, lhs, 0))
747 : return true;
748 : }
749 : }
750 :
751 : /* Otherwise keep walking. */
752 : return false;
753 : }
754 :
755 : static void
756 13724187 : mark_aliased_reaching_defs_necessary (gimple *stmt, tree ref)
757 : {
758 : /* Should have been caught before calling this function. */
759 13724187 : gcc_checking_assert (!keep_all_vdefs_p ());
760 :
761 13724187 : unsigned int chain;
762 13724187 : ao_ref refd;
763 13724187 : gcc_assert (!chain_ovfl);
764 13724187 : ao_ref_init (&refd, ref);
765 27448374 : chain = walk_aliased_vdefs (&refd, gimple_vuse (stmt),
766 : mark_aliased_reaching_defs_necessary_1,
767 13724187 : gimple_bb (stmt), NULL);
768 13724187 : if (chain > longest_chain)
769 2294535 : longest_chain = chain;
770 13724187 : total_chain += chain;
771 13724187 : nr_walks++;
772 13724187 : }
773 :
774 : /* Worker for the walker that marks reaching definitions of REF, which
775 : is not based on a non-aliased decl. For simplicity we need to end
776 : up marking all may-defs necessary that are not based on a non-aliased
777 : decl. The only job of this walker is to skip may-defs based on
778 : a non-aliased decl. */
779 :
780 : static bool
781 78308456 : mark_all_reaching_defs_necessary_1 (ao_ref *ref ATTRIBUTE_UNUSED,
782 : tree vdef, void *data ATTRIBUTE_UNUSED)
783 : {
784 78308456 : gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
785 :
786 : /* We have to skip already visited (and thus necessary) statements
787 : to make the chaining work after we dropped back to simple mode. */
788 78308456 : if (chain_ovfl
789 78308456 : && bitmap_bit_p (processed, SSA_NAME_VERSION (vdef)))
790 : {
791 2534386 : gcc_assert (gimple_nop_p (def_stmt)
792 : || gimple_plf (def_stmt, STMT_NECESSARY));
793 : return false;
794 : }
795 :
796 : /* We want to skip stores to non-aliased variables. */
797 75774070 : if (!chain_ovfl
798 75774070 : && gimple_assign_single_p (def_stmt))
799 : {
800 50735115 : tree lhs = gimple_assign_lhs (def_stmt);
801 50735115 : if (!ref_may_be_aliased (lhs))
802 : return false;
803 : }
804 :
805 : /* We want to skip statements that do not constitute stores but have
806 : a virtual definition. */
807 63182263 : if (gcall *call = dyn_cast <gcall *> (def_stmt))
808 : {
809 24161751 : tree callee = gimple_call_fndecl (call);
810 24161751 : if (callee != NULL_TREE
811 24161751 : && fndecl_built_in_p (callee, BUILT_IN_NORMAL))
812 4267895 : switch (DECL_FUNCTION_CODE (callee))
813 : {
814 : case BUILT_IN_MALLOC:
815 : case BUILT_IN_ALIGNED_ALLOC:
816 : case BUILT_IN_CALLOC:
817 : CASE_BUILT_IN_ALLOCA:
818 : case BUILT_IN_STRDUP:
819 : case BUILT_IN_STRNDUP:
820 : case BUILT_IN_FREE:
821 : case BUILT_IN_GOMP_ALLOC:
822 : case BUILT_IN_GOMP_FREE:
823 : return false;
824 :
825 : default:;
826 : }
827 :
828 23425164 : if (callee != NULL_TREE
829 22112909 : && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (callee)
830 21760079 : || DECL_IS_OPERATOR_DELETE_P (callee))
831 24376225 : && gimple_call_from_new_or_delete (call))
832 : return false;
833 22481270 : if (is_removable_cxa_atexit_call (call))
834 : return false;
835 : }
836 :
837 61501524 : if (! gimple_clobber_p (def_stmt))
838 56225680 : mark_operand_necessary (vdef);
839 :
840 : return false;
841 : }
842 :
843 : static void
844 72260618 : mark_all_reaching_defs_necessary (gimple *stmt)
845 : {
846 : /* Should have been caught before calling this function. */
847 72260618 : gcc_checking_assert (!keep_all_vdefs_p ());
848 144521236 : walk_aliased_vdefs (NULL, gimple_vuse (stmt),
849 : mark_all_reaching_defs_necessary_1, NULL, &visited);
850 72260618 : }
851 :
852 : /* Return true for PHI nodes with one or identical arguments
853 : can be removed. */
854 : static bool
855 18950368 : degenerate_phi_p (gimple *phi)
856 : {
857 18950368 : unsigned int i;
858 18950368 : tree op = gimple_phi_arg_def (phi, 0);
859 19727388 : for (i = 1; i < gimple_phi_num_args (phi); i++)
860 17854280 : if (gimple_phi_arg_def (phi, i) != op)
861 : return false;
862 : return true;
863 : }
864 :
865 : /* Return that NEW_CALL and DELETE_CALL are a valid pair of new
866 : and delete operators. */
867 :
868 : static bool
869 56026 : valid_new_delete_pair_p (gimple *new_call, gimple *delete_call)
870 : {
871 56026 : tree new_asm = DECL_ASSEMBLER_NAME (gimple_call_fndecl (new_call));
872 56026 : tree delete_asm = DECL_ASSEMBLER_NAME (gimple_call_fndecl (delete_call));
873 56026 : return valid_new_delete_pair_p (new_asm, delete_asm);
874 : }
875 :
876 : /* Propagate necessity using the operands of necessary statements.
877 : Process the uses on each statement in the worklist, and add all
878 : feeding statements which contribute to the calculation of this
879 : value to the worklist.
880 :
881 : In conservative mode, EL is NULL. */
882 :
883 : static void
884 8337506 : propagate_necessity (bool aggressive)
885 : {
886 8337506 : gimple *stmt;
887 :
888 8337506 : if (dump_file && (dump_flags & TDF_DETAILS))
889 208 : fprintf (dump_file, "\nProcessing worklist:\n");
890 :
891 277400816 : while (worklist.length () > 0)
892 : {
893 : /* Take STMT from worklist. */
894 269063310 : stmt = worklist.pop ();
895 :
896 269063310 : if (dump_file && (dump_flags & TDF_DETAILS))
897 : {
898 1099 : fprintf (dump_file, "processing: ");
899 1099 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
900 1099 : fprintf (dump_file, "\n");
901 : }
902 :
903 269063310 : if (aggressive)
904 : {
905 : /* Mark the last statement of the basic blocks on which the block
906 : containing STMT is control dependent, but only if we haven't
907 : already done so. */
908 93127543 : basic_block bb = gimple_bb (stmt);
909 93127543 : if (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
910 93127543 : && !bitmap_bit_p (visited_control_parents, bb->index))
911 19538169 : mark_control_dependent_edges_necessary (bb, false);
912 : }
913 :
914 269063310 : if (gimple_code (stmt) == GIMPLE_PHI
915 : /* We do not process virtual PHI nodes nor do we track their
916 : necessity. */
917 308020062 : && !virtual_operand_p (gimple_phi_result (stmt)))
918 : {
919 : /* PHI nodes are somewhat special in that each PHI alternative has
920 : data and control dependencies. All the statements feeding the
921 : PHI node's arguments are always necessary. In aggressive mode,
922 : we also consider the control dependent edges leading to the
923 : predecessor block associated with each PHI alternative as
924 : necessary. */
925 19478376 : gphi *phi = as_a <gphi *> (stmt);
926 19478376 : size_t k;
927 :
928 84731395 : for (k = 0; k < gimple_phi_num_args (stmt); k++)
929 : {
930 45774643 : tree arg = PHI_ARG_DEF (stmt, k);
931 45774643 : if (TREE_CODE (arg) == SSA_NAME)
932 33742074 : mark_operand_necessary (arg);
933 : }
934 :
935 : /* For PHI operands it matters from where the control flow arrives
936 : to the BB. Consider the following example:
937 :
938 : a=exp1;
939 : b=exp2;
940 : if (test)
941 : ;
942 : else
943 : ;
944 : c=PHI(a,b)
945 :
946 : We need to mark control dependence of the empty basic blocks, since they
947 : contains computation of PHI operands.
948 :
949 : Doing so is too restrictive in the case the predecestor block is in
950 : the loop. Consider:
951 :
952 : if (b)
953 : {
954 : int i;
955 : for (i = 0; i<1000; ++i)
956 : ;
957 : j = 0;
958 : }
959 : return j;
960 :
961 : There is PHI for J in the BB containing return statement.
962 : In this case the control dependence of predecestor block (that is
963 : within the empty loop) also contains the block determining number
964 : of iterations of the block that would prevent removing of empty
965 : loop in this case.
966 :
967 : This scenario can be avoided by splitting critical edges.
968 : To save the critical edge splitting pass we identify how the control
969 : dependence would look like if the edge was split.
970 :
971 : Consider the modified CFG created from current CFG by splitting
972 : edge B->C. In the postdominance tree of modified CFG, C' is
973 : always child of C. There are two cases how children of C' can look
974 : like:
975 :
976 : 1) C' is leaf
977 :
978 : In this case the only basic block C' is control dependent on is B.
979 :
980 : 2) C' has single child that is B
981 :
982 : In this case control dependence of C' is same as control
983 : dependence of B in original CFG except for block B itself.
984 : (since C' postdominate B in modified CFG)
985 :
986 : Now how to decide what case happens? There are two basic options:
987 :
988 : a) C postdominate B. Then C immediately postdominate B and
989 : case 2 happens iff there is no other way from B to C except
990 : the edge B->C.
991 :
992 : There is other way from B to C iff there is successor of B that
993 : is not postdominated by B. Testing this condition is somewhat
994 : expensive, because we need to iterate all successors of B.
995 : We are safe to assume that this does not happen: we will mark B
996 : as needed when processing the other path from B to C that is
997 : control dependent on B and marking control dependencies of B
998 : itself is harmless because they will be processed anyway after
999 : processing control statement in B.
1000 :
1001 : b) C does not postdominate B. Always case 1 happens since there is
1002 : path from C to exit that does not go through B and thus also C'. */
1003 :
1004 32731409 : if (aggressive && !degenerate_phi_p (stmt))
1005 : {
1006 20773476 : for (k = 0; k < gimple_phi_num_args (stmt); k++)
1007 : {
1008 14548133 : basic_block arg_bb = gimple_phi_arg_edge (phi, k)->src;
1009 :
1010 14548133 : if (gimple_bb (stmt)
1011 14548133 : != get_immediate_dominator (CDI_POST_DOMINATORS, arg_bb))
1012 : {
1013 1457665 : if (!mark_last_stmt_necessary (arg_bb))
1014 2532 : mark_control_dependent_edges_necessary (arg_bb, false);
1015 : }
1016 13090468 : else if (arg_bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1017 13090468 : && !bitmap_bit_p (visited_control_parents,
1018 : arg_bb->index))
1019 6581901 : mark_control_dependent_edges_necessary (arg_bb, true);
1020 : }
1021 : }
1022 : }
1023 : else
1024 : {
1025 : /* Propagate through the operands. Examine all the USE, VUSE and
1026 : VDEF operands in this statement. Mark all the statements
1027 : which feed this statement's uses as necessary. */
1028 249584934 : ssa_op_iter iter;
1029 249584934 : tree use;
1030 :
1031 : /* If this is a call to free which is directly fed by an
1032 : allocation function do not mark that necessary through
1033 : processing the argument. */
1034 249584934 : bool is_delete_operator
1035 249584934 : = (is_gimple_call (stmt)
1036 38546636 : && gimple_call_from_new_or_delete (as_a <gcall *> (stmt))
1037 250839624 : && gimple_call_operator_delete_p (as_a <gcall *> (stmt)));
1038 248703438 : if (is_delete_operator
1039 248703438 : || gimple_call_builtin_p (stmt, BUILT_IN_FREE)
1040 248346904 : || gimple_call_builtin_p (stmt, BUILT_IN_GOMP_FREE))
1041 : {
1042 1238030 : tree ptr = gimple_call_arg (stmt, 0);
1043 1238030 : gcall *def_stmt;
1044 : /* If the pointer we free is defined by an allocation
1045 : function do not add the call to the worklist. */
1046 1238030 : if (TREE_CODE (ptr) == SSA_NAME
1047 1236669 : && (def_stmt = dyn_cast <gcall *> (SSA_NAME_DEF_STMT (ptr)))
1048 1461608 : && is_removable_allocation_p (def_stmt, false))
1049 : {
1050 200446 : if (is_delete_operator
1051 200446 : && !valid_new_delete_pair_p (def_stmt, stmt))
1052 143 : mark_operand_necessary (gimple_call_arg (stmt, 0));
1053 :
1054 : /* Delete operators can have alignment and (or) size
1055 : as next arguments. When being a SSA_NAME, they
1056 : must be marked as necessary. Similarly GOMP_free. */
1057 200446 : if (gimple_call_num_args (stmt) >= 2)
1058 101442 : for (unsigned i = 1; i < gimple_call_num_args (stmt);
1059 : i++)
1060 : {
1061 50772 : tree arg = gimple_call_arg (stmt, i);
1062 50772 : if (TREE_CODE (arg) == SSA_NAME)
1063 8658 : mark_operand_necessary (arg);
1064 : }
1065 :
1066 109202603 : continue;
1067 200446 : }
1068 : }
1069 :
1070 249384488 : if (checks_return_value_of_removable_allocation_p (stmt))
1071 128475 : continue;
1072 :
1073 467586183 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
1074 218330170 : mark_operand_necessary (use);
1075 :
1076 249256013 : use = gimple_vuse (stmt);
1077 215430862 : if (!use)
1078 102537941 : continue;
1079 :
1080 : /* No need to search for vdefs if we intrinsicly keep them all. */
1081 146718072 : if (keep_all_vdefs_p ())
1082 68207 : continue;
1083 :
1084 : /* If we dropped to simple mode make all immediately
1085 : reachable definitions necessary. */
1086 146649865 : if (chain_ovfl)
1087 : {
1088 3689660 : mark_all_reaching_defs_necessary (stmt);
1089 3689660 : continue;
1090 : }
1091 :
1092 : /* For statements that may load from memory (have a VUSE) we
1093 : have to mark all reaching (may-)definitions as necessary.
1094 : We partition this task into two cases:
1095 : 1) explicit loads based on decls that are not aliased
1096 : 2) implicit loads (like calls) and explicit loads not
1097 : based on decls that are not aliased (like indirect
1098 : references or loads from globals)
1099 : For 1) we mark all reaching may-defs as necessary, stopping
1100 : at dominating kills. For 2) we want to mark all dominating
1101 : references necessary, but non-aliased ones which we handle
1102 : in 1). By keeping a global visited bitmap for references
1103 : we walk for 2) we avoid quadratic behavior for those. */
1104 :
1105 142960205 : if (gcall *call = dyn_cast <gcall *> (stmt))
1106 : {
1107 35102126 : tree callee = gimple_call_fndecl (call);
1108 35102126 : unsigned i;
1109 :
1110 36299780 : if (callee != NULL_TREE
1111 33488818 : && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (callee)
1112 33111276 : || DECL_IS_OPERATOR_DELETE_P (callee))
1113 36312414 : && gimple_call_from_new_or_delete (call))
1114 1197654 : continue;
1115 :
1116 33904472 : if (is_removable_cxa_atexit_call (call))
1117 0 : continue;
1118 :
1119 : bool all_refs = false;
1120 : /* Calls implicitly load from memory, their arguments
1121 : in addition may explicitly perform memory loads. */
1122 103997435 : for (i = 0; i < gimple_call_num_args (call); ++i)
1123 : {
1124 70092963 : tree arg = gimple_call_arg (call, i);
1125 136007664 : if (TREE_CODE (arg) == SSA_NAME
1126 70092963 : || is_gimple_min_invariant (arg))
1127 65914701 : continue;
1128 4178262 : if (TREE_CODE (arg) == WITH_SIZE_EXPR)
1129 664 : arg = TREE_OPERAND (arg, 0);
1130 4178262 : if (!ref_may_be_aliased (arg))
1131 3771530 : mark_aliased_reaching_defs_necessary (call, arg);
1132 : else
1133 : all_refs = true;
1134 : }
1135 :
1136 33904472 : if (!all_refs && ipa_modref_callee_reads_no_memory_p (call))
1137 1380220 : continue;
1138 32524252 : mark_all_reaching_defs_necessary (call);
1139 : }
1140 107858079 : else if (gimple_assign_single_p (stmt))
1141 : {
1142 99531745 : tree rhs;
1143 : /* If this is a load mark things necessary. */
1144 99531745 : rhs = gimple_assign_rhs1 (stmt);
1145 99531745 : if (TREE_CODE (rhs) != SSA_NAME
1146 80822457 : && !is_gimple_min_invariant (rhs)
1147 155518066 : && TREE_CODE (rhs) != CONSTRUCTOR)
1148 : {
1149 45130419 : if (!ref_may_be_aliased (rhs))
1150 9277984 : mark_aliased_reaching_defs_necessary (stmt, rhs);
1151 : else
1152 35852435 : mark_all_reaching_defs_necessary (stmt);
1153 : }
1154 : }
1155 8326334 : else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
1156 : {
1157 8157522 : tree rhs = gimple_return_retval (return_stmt);
1158 : /* A return statement may perform a load. */
1159 8157522 : if (rhs
1160 4505419 : && TREE_CODE (rhs) != SSA_NAME
1161 1463301 : && !is_gimple_min_invariant (rhs)
1162 8851812 : && TREE_CODE (rhs) != CONSTRUCTOR)
1163 : {
1164 694290 : if (!ref_may_be_aliased (rhs))
1165 668831 : mark_aliased_reaching_defs_necessary (stmt, rhs);
1166 : else
1167 25459 : mark_all_reaching_defs_necessary (stmt);
1168 : }
1169 : }
1170 168812 : else if (gasm *asm_stmt = dyn_cast <gasm *> (stmt))
1171 : {
1172 167628 : unsigned i;
1173 167628 : mark_all_reaching_defs_necessary (stmt);
1174 : /* Inputs may perform loads. */
1175 479623 : for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
1176 : {
1177 144367 : tree op = TREE_VALUE (gimple_asm_input_op (asm_stmt, i));
1178 144367 : if (TREE_CODE (op) != SSA_NAME
1179 90437 : && !is_gimple_min_invariant (op)
1180 51484 : && TREE_CODE (op) != CONSTRUCTOR
1181 195851 : && !ref_may_be_aliased (op))
1182 5842 : mark_aliased_reaching_defs_necessary (stmt, op);
1183 : }
1184 : }
1185 1184 : else if (gimple_code (stmt) == GIMPLE_TRANSACTION)
1186 : {
1187 : /* The beginning of a transaction is a memory barrier. */
1188 : /* ??? If we were really cool, we'd only be a barrier
1189 : for the memories touched within the transaction. */
1190 1184 : mark_all_reaching_defs_necessary (stmt);
1191 : }
1192 : else
1193 0 : gcc_unreachable ();
1194 :
1195 : /* If we over-used our alias oracle budget drop to simple
1196 : mode. The cost metric allows quadratic behavior
1197 : (number of uses times number of may-defs queries) up to
1198 : a constant maximal number of queries and after that falls back to
1199 : super-linear complexity. */
1200 140382331 : if (/* Constant but quadratic for small functions. */
1201 140382331 : total_chain > 128 * 128
1202 : /* Linear in the number of may-defs. */
1203 1228149 : && total_chain > 32 * longest_chain
1204 : /* Linear in the number of uses. */
1205 4496 : && total_chain > nr_walks * 32)
1206 : {
1207 4387 : chain_ovfl = true;
1208 4387 : if (visited)
1209 4387 : bitmap_clear (visited);
1210 : }
1211 : }
1212 : }
1213 8337506 : }
1214 :
1215 : /* Remove dead PHI nodes from block BB. */
1216 :
1217 : static bool
1218 84519558 : remove_dead_phis (basic_block bb)
1219 : {
1220 84519558 : bool something_changed = false;
1221 84519558 : gphi *phi;
1222 84519558 : gphi_iterator gsi;
1223 :
1224 120330263 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);)
1225 : {
1226 35810705 : stats.total_phis++;
1227 35810705 : phi = gsi.phi ();
1228 :
1229 : /* We do not track necessity of virtual PHI nodes. Instead do
1230 : very simple dead PHI removal here. */
1231 71621410 : if (virtual_operand_p (gimple_phi_result (phi)))
1232 : {
1233 : /* Virtual PHI nodes with one or identical arguments
1234 : can be removed. */
1235 15986484 : if (!loops_state_satisfies_p (LOOP_CLOSED_SSA)
1236 15986484 : && degenerate_phi_p (phi))
1237 : {
1238 1348450 : tree vdef = gimple_phi_result (phi);
1239 1348450 : tree vuse = gimple_phi_arg_def (phi, 0);
1240 :
1241 1348450 : use_operand_p use_p;
1242 1348450 : imm_use_iterator iter;
1243 1348450 : gimple *use_stmt;
1244 3568592 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
1245 4504948 : FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1246 2252474 : SET_USE (use_p, vuse);
1247 1348450 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vdef)
1248 1348450 : && TREE_CODE (vuse) == SSA_NAME)
1249 470 : SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse) = 1;
1250 : }
1251 : else
1252 14638034 : gimple_set_plf (phi, STMT_NECESSARY, true);
1253 : }
1254 :
1255 35810705 : if (!gimple_plf (phi, STMT_NECESSARY))
1256 : {
1257 1694295 : something_changed = true;
1258 1694295 : if (dump_file && (dump_flags & TDF_DETAILS))
1259 : {
1260 9 : fprintf (dump_file, "Deleting : ");
1261 9 : print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
1262 9 : fprintf (dump_file, "\n");
1263 : }
1264 :
1265 1694295 : remove_phi_node (&gsi, true);
1266 1694295 : stats.removed_phis++;
1267 1694295 : continue;
1268 : }
1269 :
1270 34116410 : gsi_next (&gsi);
1271 : }
1272 84519558 : return something_changed;
1273 : }
1274 :
1275 :
1276 : /* Remove dead statement pointed to by iterator I. Receives the basic block BB
1277 : containing I so that we don't have to look it up. */
1278 :
1279 : static void
1280 9847764 : remove_dead_stmt (gimple_stmt_iterator *i, basic_block bb,
1281 : vec<edge> &to_remove_edges)
1282 : {
1283 9847764 : gimple *stmt = gsi_stmt (*i);
1284 :
1285 9847764 : if (dump_file && (dump_flags & TDF_DETAILS))
1286 : {
1287 125 : fprintf (dump_file, "Deleting : ");
1288 125 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1289 125 : fprintf (dump_file, "\n");
1290 : }
1291 :
1292 9847764 : stats.removed++;
1293 :
1294 : /* If we have determined that a conditional branch statement contributes
1295 : nothing to the program, then we not only remove it, but we need to update
1296 : the CFG. We can chose any of edges out of BB as long as we are sure to not
1297 : close infinite loops. This is done by always choosing the edge closer to
1298 : exit in inverted_rev_post_order_compute order. */
1299 9847764 : if (is_ctrl_stmt (stmt))
1300 : {
1301 39216 : edge_iterator ei;
1302 39216 : edge e = NULL, e2;
1303 :
1304 : /* See if there is only one non-abnormal edge. */
1305 39216 : if (single_succ_p (bb))
1306 0 : e = single_succ_edge (bb);
1307 : /* Otherwise chose one that is closer to bb with live statement in it.
1308 : To be able to chose one, we compute inverted post order starting from
1309 : all BBs with live statements. */
1310 0 : if (!e)
1311 : {
1312 39216 : if (!bb_postorder)
1313 : {
1314 20144 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
1315 20144 : int n = inverted_rev_post_order_compute (cfun, rpo,
1316 : &bb_contains_live_stmts);
1317 20144 : bb_postorder = XNEWVEC (int, last_basic_block_for_fn (cfun));
1318 808867 : for (int i = 0; i < n; ++i)
1319 788723 : bb_postorder[rpo[i]] = i;
1320 20144 : free (rpo);
1321 : }
1322 117669 : FOR_EACH_EDGE (e2, ei, bb->succs)
1323 78453 : if (!e || e2->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
1324 39237 : || bb_postorder [e->dest->index]
1325 39237 : >= bb_postorder [e2->dest->index])
1326 61151 : e = e2;
1327 : }
1328 39216 : gcc_assert (e);
1329 39216 : e->probability = profile_probability::always ();
1330 :
1331 : /* The edge is no longer associated with a conditional, so it does
1332 : not have TRUE/FALSE flags.
1333 : We are also safe to drop EH/ABNORMAL flags and turn them into
1334 : normal control flow, because we know that all the destinations (including
1335 : those odd edges) are equivalent for program execution. */
1336 39216 : e->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE | EDGE_EH | EDGE_ABNORMAL);
1337 :
1338 : /* The lone outgoing edge from BB will be a fallthru edge. */
1339 39216 : e->flags |= EDGE_FALLTHRU;
1340 :
1341 : /* Remove the remaining outgoing edges. */
1342 117669 : FOR_EACH_EDGE (e2, ei, bb->succs)
1343 78453 : if (e != e2)
1344 : {
1345 : /* If we made a BB unconditionally exit a loop or removed
1346 : an entry into an irreducible region, then this transform
1347 : alters the set of BBs in the loop. Schedule a fixup. */
1348 39237 : if (loop_exit_edge_p (bb->loop_father, e)
1349 39237 : || (e2->dest->flags & BB_IRREDUCIBLE_LOOP))
1350 22202 : loops_state_set (LOOPS_NEED_FIXUP);
1351 39237 : to_remove_edges.safe_push (e2);
1352 : }
1353 : }
1354 :
1355 : /* If this is a store into a variable that is being optimized away,
1356 : add a debug bind stmt if possible. */
1357 9847764 : if (MAY_HAVE_DEBUG_BIND_STMTS
1358 9156972 : && gimple_assign_single_p (stmt)
1359 10160449 : && is_gimple_val (gimple_assign_rhs1 (stmt)))
1360 : {
1361 172034 : tree lhs = gimple_assign_lhs (stmt);
1362 147064 : if ((VAR_P (lhs) || TREE_CODE (lhs) == PARM_DECL)
1363 25049 : && !DECL_IGNORED_P (lhs)
1364 5446 : && is_gimple_reg_type (TREE_TYPE (lhs))
1365 50 : && !is_global_var (lhs)
1366 172084 : && !DECL_HAS_VALUE_EXPR_P (lhs))
1367 : {
1368 50 : tree rhs = gimple_assign_rhs1 (stmt);
1369 50 : gdebug *note
1370 50 : = gimple_build_debug_bind (lhs, unshare_expr (rhs), stmt);
1371 50 : gsi_insert_after (i, note, GSI_SAME_STMT);
1372 : }
1373 : }
1374 :
1375 9847764 : unlink_stmt_vdef (stmt);
1376 9847764 : gsi_remove (i, true);
1377 9847764 : release_defs (stmt);
1378 9847764 : }
1379 :
1380 : /* Helper for maybe_optimize_arith_overflow. Find in *TP if there are any
1381 : uses of data (SSA_NAME) other than REALPART_EXPR referencing it. */
1382 :
1383 : static tree
1384 26 : find_non_realpart_uses (tree *tp, int *walk_subtrees, void *data)
1385 : {
1386 26 : if (TYPE_P (*tp) || TREE_CODE (*tp) == REALPART_EXPR)
1387 0 : *walk_subtrees = 0;
1388 26 : if (*tp == (tree) data)
1389 13 : return *tp;
1390 : return NULL_TREE;
1391 : }
1392 :
1393 : /* If the IMAGPART_EXPR of the {ADD,SUB,MUL}_OVERFLOW result is never used,
1394 : but REALPART_EXPR is, optimize the {ADD,SUB,MUL}_OVERFLOW internal calls
1395 : into plain unsigned {PLUS,MINUS,MULT}_EXPR, and if needed reset debug
1396 : uses. */
1397 :
1398 : static void
1399 287702 : maybe_optimize_arith_overflow (gimple_stmt_iterator *gsi,
1400 : enum tree_code subcode)
1401 : {
1402 287702 : gimple *stmt = gsi_stmt (*gsi);
1403 287702 : tree lhs = gimple_call_lhs (stmt);
1404 :
1405 287702 : if (lhs == NULL || TREE_CODE (lhs) != SSA_NAME)
1406 287521 : return;
1407 :
1408 287702 : imm_use_iterator imm_iter;
1409 287702 : use_operand_p use_p;
1410 287702 : bool has_debug_uses = false;
1411 287702 : bool has_realpart_uses = false;
1412 287702 : bool has_other_uses = false;
1413 296732 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
1414 : {
1415 296551 : gimple *use_stmt = USE_STMT (use_p);
1416 296551 : auto assign = dyn_cast<gassign *> (use_stmt);
1417 296551 : if (is_gimple_debug (use_stmt))
1418 : has_debug_uses = true;
1419 292726 : else if (assign
1420 291679 : && gimple_assign_rhs_code (assign) == REALPART_EXPR
1421 297931 : && TREE_OPERAND (gimple_assign_rhs1 (assign), 0) == lhs)
1422 : has_realpart_uses = true;
1423 : else
1424 : {
1425 : has_other_uses = true;
1426 : break;
1427 : }
1428 287702 : }
1429 :
1430 287702 : if (!has_realpart_uses || has_other_uses)
1431 : return;
1432 :
1433 181 : tree arg0 = gimple_call_arg (stmt, 0);
1434 181 : tree arg1 = gimple_call_arg (stmt, 1);
1435 181 : location_t loc = gimple_location (stmt);
1436 181 : tree type = TREE_TYPE (TREE_TYPE (lhs));
1437 181 : tree utype = unsigned_type_for (type);
1438 181 : tree result = fold_build2_loc (loc, subcode, utype,
1439 : fold_convert_loc (loc, utype, arg0),
1440 : fold_convert_loc (loc, utype, arg1));
1441 181 : result = fold_convert_loc (loc, type, result);
1442 :
1443 181 : if (has_debug_uses)
1444 : {
1445 13 : gimple *use_stmt;
1446 39 : FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, lhs)
1447 : {
1448 26 : if (!gimple_debug_bind_p (use_stmt))
1449 13 : continue;
1450 13 : tree v = gimple_debug_bind_get_value (use_stmt);
1451 13 : if (walk_tree (&v, find_non_realpart_uses, lhs, NULL))
1452 : {
1453 13 : gimple_debug_bind_reset_value (use_stmt);
1454 13 : update_stmt (use_stmt);
1455 : }
1456 13 : }
1457 : }
1458 :
1459 181 : if (TREE_CODE (result) == INTEGER_CST && TREE_OVERFLOW (result))
1460 0 : result = drop_tree_overflow (result);
1461 181 : tree overflow = build_zero_cst (type);
1462 181 : tree ctype = build_complex_type (type);
1463 181 : if (TREE_CODE (result) == INTEGER_CST)
1464 0 : result = build_complex (ctype, result, overflow);
1465 : else
1466 181 : result = build2_loc (gimple_location (stmt), COMPLEX_EXPR,
1467 : ctype, result, overflow);
1468 :
1469 181 : if (dump_file && (dump_flags & TDF_DETAILS))
1470 : {
1471 0 : fprintf (dump_file, "Transforming call: ");
1472 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1473 0 : fprintf (dump_file, "because the overflow result is never used into: ");
1474 0 : print_generic_stmt (dump_file, result, TDF_SLIM);
1475 0 : fprintf (dump_file, "\n");
1476 : }
1477 :
1478 181 : gimplify_and_update_call_from_tree (gsi, result);
1479 : }
1480 :
1481 : /* Returns whether the control parents of BB are preserved. */
1482 :
1483 : static bool
1484 368112 : control_parents_preserved_p (basic_block bb)
1485 : {
1486 : /* If we marked the control parents from BB they are preserved. */
1487 368112 : if (bitmap_bit_p (visited_control_parents, bb->index))
1488 : return true;
1489 :
1490 : /* But they can also end up being marked from elsewhere. */
1491 7103 : bitmap_iterator bi;
1492 7103 : unsigned edge_number;
1493 11211 : EXECUTE_IF_SET_IN_BITMAP (cd->get_edges_dependent_on (bb->index),
1494 : 0, edge_number, bi)
1495 : {
1496 7223 : basic_block cd_bb = cd->get_edge_src (edge_number);
1497 7223 : if (cd_bb != bb
1498 7223 : && !bitmap_bit_p (last_stmt_necessary, cd_bb->index))
1499 : return false;
1500 : }
1501 : /* And cache the result. */
1502 3988 : bitmap_set_bit (visited_control_parents, bb->index);
1503 3988 : return true;
1504 : }
1505 :
1506 : /* If basic block is empty, we can remove conditionals that controls
1507 : its execution. However in some cases the empty BB can stay live
1508 : (such as when it was a header of empty loop). In this case we
1509 : need to update its count. Since regions of dead BBs are acyclic
1510 : we simply propagate counts forward from live BBs to dead ones. */
1511 :
1512 : static void
1513 20177 : propagate_counts ()
1514 : {
1515 20177 : basic_block bb;
1516 20177 : auto_vec<basic_block, 16> queue;
1517 20177 : hash_map <basic_block, int> cnt;
1518 :
1519 724753 : FOR_EACH_BB_FN (bb, cfun)
1520 704576 : if (!bitmap_bit_p (bb_contains_live_stmts, bb->index))
1521 : {
1522 152549 : int n = 0;
1523 632606 : for (edge e : bb->preds)
1524 174959 : if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1525 174959 : && !bitmap_bit_p (bb_contains_live_stmts, e->src->index))
1526 31425 : n++;
1527 152549 : if (!n)
1528 124019 : queue.safe_push (bb);
1529 152549 : cnt.put (bb, n);
1530 : }
1531 190969 : while (!queue.is_empty ())
1532 : {
1533 150615 : basic_block bb = queue.pop ();
1534 150615 : profile_count sum = profile_count::zero ();
1535 :
1536 473367 : for (edge e : bb->preds)
1537 : {
1538 172137 : sum += e->count ();
1539 172137 : gcc_checking_assert (!cnt.get (e->src));
1540 : }
1541 : /* If we have partial profile and some counts of incoming edges are
1542 : unknown, it is probably better to keep the existing count.
1543 : We could also propagate bi-directionally. */
1544 150615 : if (sum.initialized_p () && !(sum == bb->count))
1545 : {
1546 18722 : if (dump_file && (dump_flags & TDF_DETAILS))
1547 : {
1548 3 : fprintf (dump_file, "Updating count of empty bb %i from ",
1549 : bb->index);
1550 3 : bb->count.dump (dump_file);
1551 3 : fprintf (dump_file, " to ");
1552 3 : sum.dump (dump_file);
1553 3 : fprintf (dump_file, "\n");
1554 : }
1555 18722 : bb->count = sum;
1556 : }
1557 150615 : cnt.remove (bb);
1558 602460 : for (edge e : bb->succs)
1559 150615 : if (int *n = cnt.get (e->dest))
1560 : {
1561 29491 : (*n)--;
1562 29491 : if (!*n)
1563 26596 : queue.safe_push (e->dest);
1564 : }
1565 : }
1566 : /* Do not check that all blocks has been processed, since for
1567 : empty infinite loops this is not the case. */
1568 20177 : }
1569 :
1570 : /* Eliminate unnecessary statements. Any instruction not marked as necessary
1571 : contributes nothing to the program, and can be deleted. */
1572 :
1573 : static bool
1574 8337506 : eliminate_unnecessary_stmts (bool aggressive)
1575 : {
1576 8337506 : bool something_changed = false;
1577 8337506 : basic_block bb;
1578 8337506 : gimple_stmt_iterator gsi, psi;
1579 8337506 : gimple *stmt;
1580 8337506 : auto_vec<edge> to_remove_edges;
1581 :
1582 8337506 : if (dump_file && (dump_flags & TDF_DETAILS))
1583 208 : fprintf (dump_file, "\nEliminating unnecessary statements:\n");
1584 :
1585 8337506 : bool had_setjmp = cfun->calls_setjmp;
1586 8337506 : clear_special_calls ();
1587 :
1588 : /* Walking basic blocks and statements in reverse order avoids
1589 : releasing SSA names before any other DEFs that refer to them are
1590 : released. This helps avoid loss of debug information, as we get
1591 : a chance to propagate all RHSs of removed SSAs into debug uses,
1592 : rather than only the latest ones. E.g., consider:
1593 :
1594 : x_3 = y_1 + z_2;
1595 : a_5 = x_3 - b_4;
1596 : # DEBUG a => a_5
1597 :
1598 : If we were to release x_3 before a_5, when we reached a_5 and
1599 : tried to substitute it into the debug stmt, we'd see x_3 there,
1600 : but x_3's DEF, type, etc would have already been disconnected.
1601 : By going backwards, the debug stmt first changes to:
1602 :
1603 : # DEBUG a => x_3 - b_4
1604 :
1605 : and then to:
1606 :
1607 : # DEBUG a => y_1 + z_2 - b_4
1608 :
1609 : as desired. */
1610 8337506 : gcc_assert (dom_info_available_p (CDI_DOMINATORS));
1611 8337506 : auto_vec<basic_block> h;
1612 8337506 : h = get_all_dominated_blocks (CDI_DOMINATORS,
1613 16675012 : single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1614 :
1615 92857064 : while (h.length ())
1616 : {
1617 84519558 : bb = h.pop ();
1618 :
1619 : /* Remove dead statements. */
1620 84519558 : auto_bitmap debug_seen;
1621 84519558 : hash_set<int_hash <location_t, 0>> locs_seen;
1622 808952599 : for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi = psi)
1623 : {
1624 639913483 : stmt = gsi_stmt (gsi);
1625 :
1626 639913483 : psi = gsi;
1627 639913483 : gsi_prev (&psi);
1628 :
1629 639913483 : stats.total++;
1630 :
1631 : /* We can mark a call to free as not necessary if the
1632 : defining statement of its argument is not necessary
1633 : (and thus is getting removed). */
1634 639913483 : if (gimple_plf (stmt, STMT_NECESSARY)
1635 639913483 : && (gimple_call_builtin_p (stmt, BUILT_IN_FREE)
1636 636951739 : || (is_gimple_call (stmt)
1637 38190102 : && gimple_call_from_new_or_delete (as_a <gcall *> (stmt))
1638 1254690 : && gimple_call_operator_delete_p (as_a <gcall *> (stmt)))))
1639 : {
1640 1238030 : tree ptr = gimple_call_arg (stmt, 0);
1641 1238030 : if (TREE_CODE (ptr) == SSA_NAME)
1642 : {
1643 1236669 : gimple *def_stmt = SSA_NAME_DEF_STMT (ptr);
1644 1236669 : if (!gimple_nop_p (def_stmt)
1645 1236669 : && !gimple_plf (def_stmt, STMT_NECESSARY))
1646 7481 : gimple_set_plf (stmt, STMT_NECESSARY, false);
1647 : }
1648 : }
1649 :
1650 : /* Comparisons of the return value of an allocation against NULL
1651 : can be turned to a constant if the allocation itself is
1652 : unnecessary. */
1653 639913483 : gassign *assign = dyn_cast<gassign *> (stmt);
1654 639913483 : if (gimple_plf (stmt, STMT_NECESSARY) && assign
1655 168402085 : && (gimple_assign_rhs_code (assign) == EQ_EXPR
1656 167569622 : || gimple_assign_rhs_code (assign) == NE_EXPR)
1657 641568882 : && TREE_CODE (gimple_assign_rhs1 (assign)) == SSA_NAME)
1658 : {
1659 1655130 : tree rhs1 = gimple_assign_rhs1 (assign);
1660 1655130 : gimple *def_stmt = SSA_NAME_DEF_STMT (rhs1);
1661 1655130 : if (!gimple_nop_p (def_stmt)
1662 1655130 : && !gimple_plf (def_stmt, STMT_NECESSARY))
1663 : {
1664 1519 : gcc_checking_assert
1665 : (checks_return_value_of_removable_allocation_p (assign));
1666 1519 : tree lhs = gimple_assign_lhs (assign);
1667 1519 : bool is_ne = gimple_assign_rhs_code (assign) == NE_EXPR;
1668 1519 : tree cst = constant_boolean_node (is_ne, TREE_TYPE (lhs));
1669 1519 : gimple_assign_set_rhs_with_ops (&gsi, TREE_CODE (cst), cst);
1670 1519 : update_stmt (gsi_stmt (gsi));
1671 : }
1672 : }
1673 :
1674 : /* Conditional checking that return value of allocation is non-NULL
1675 : can be turned to constant if the allocation itself
1676 : is unnecessary. */
1677 639913483 : if (gimple_plf (stmt, STMT_NECESSARY)
1678 637300792 : && gimple_code (stmt) == GIMPLE_COND
1679 671962820 : && TREE_CODE (gimple_cond_lhs (stmt)) == SSA_NAME)
1680 : {
1681 32044940 : gimple *def_stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
1682 32044940 : if (!gimple_nop_p (def_stmt)
1683 32044940 : && !gimple_plf (def_stmt, STMT_NECESSARY))
1684 : {
1685 2942 : gcc_checking_assert
1686 : (checks_return_value_of_removable_allocation_p (stmt));
1687 2942 : gimple_cond_set_lhs (as_a <gcond *>(stmt),
1688 : build_one_cst
1689 2942 : (TREE_TYPE (gimple_cond_rhs (stmt))));
1690 2942 : update_stmt (stmt);
1691 : }
1692 : }
1693 :
1694 : /* If GSI is not necessary then remove it. */
1695 639913483 : if (!gimple_plf (stmt, STMT_NECESSARY))
1696 : {
1697 : /* Keep clobbers that we can keep live live. */
1698 2612691 : if (gimple_clobber_p (stmt))
1699 : {
1700 876718 : ssa_op_iter iter;
1701 876718 : use_operand_p use_p;
1702 876718 : bool dead = false;
1703 1751102 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1704 : {
1705 876718 : tree name = USE_FROM_PTR (use_p);
1706 876718 : if (!SSA_NAME_IS_DEFAULT_DEF (name)
1707 876718 : && !bitmap_bit_p (processed, SSA_NAME_VERSION (name)))
1708 : {
1709 : dead = true;
1710 : break;
1711 : }
1712 : }
1713 1747987 : if (!dead
1714 : /* When doing CD-DCE we have to ensure all controls
1715 : of the stmt are still live. */
1716 876718 : && (!aggressive || control_parents_preserved_p (bb)))
1717 : {
1718 871269 : bitmap_clear (debug_seen);
1719 871269 : continue;
1720 : }
1721 : }
1722 1741422 : if (!is_gimple_debug (stmt))
1723 1348335 : something_changed = true;
1724 1741422 : remove_dead_stmt (&gsi, bb, to_remove_edges);
1725 1741422 : continue;
1726 1741422 : }
1727 637300792 : else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
1728 : {
1729 38539155 : tree name = gimple_call_lhs (call_stmt);
1730 :
1731 38539155 : notice_special_calls (call_stmt);
1732 :
1733 : /* When LHS of var = call (); is dead, simplify it into
1734 : call (); saving one operand. */
1735 38539155 : if (name
1736 14896194 : && TREE_CODE (name) == SSA_NAME
1737 12498979 : && !bitmap_bit_p (processed, SSA_NAME_VERSION (name))
1738 : /* Avoid doing so for allocation calls which we
1739 : did not mark as necessary, it will confuse the
1740 : special logic we apply to malloc/free pair removal. */
1741 38617357 : && !is_removable_allocation_p (call_stmt, false))
1742 : {
1743 78116 : something_changed = true;
1744 78116 : if (dump_file && (dump_flags & TDF_DETAILS))
1745 : {
1746 1 : fprintf (dump_file, "Deleting LHS of call: ");
1747 1 : print_gimple_stmt (dump_file, call_stmt, 0, TDF_SLIM);
1748 1 : fprintf (dump_file, "\n");
1749 : }
1750 :
1751 78116 : gimple_call_set_lhs (call_stmt, NULL_TREE);
1752 78116 : maybe_clean_or_replace_eh_stmt (call_stmt, call_stmt);
1753 78116 : update_stmt (call_stmt);
1754 78116 : release_ssa_name (name);
1755 :
1756 : /* __builtin_stack_save without lhs is not needed. */
1757 78116 : if (gimple_call_builtin_p (call_stmt, BUILT_IN_STACK_SAVE))
1758 37 : remove_dead_stmt (&gsi, bb, to_remove_edges);
1759 : /* GOMP_SIMD_LANE (unless three argument) or ASAN_POISON
1760 : without lhs is not needed. */
1761 78079 : else if (gimple_call_internal_p (call_stmt))
1762 7400 : switch (gimple_call_internal_fn (call_stmt))
1763 : {
1764 792 : case IFN_GOMP_SIMD_LANE:
1765 792 : if (gimple_call_num_args (call_stmt) >= 3
1766 825 : && !integer_nonzerop
1767 33 : (gimple_call_arg (call_stmt, 2)))
1768 : break;
1769 : /* FALLTHRU */
1770 882 : case IFN_ASAN_POISON:
1771 882 : remove_dead_stmt (&gsi, bb, to_remove_edges);
1772 882 : break;
1773 : default:
1774 : break;
1775 : }
1776 : }
1777 38461039 : else if (gimple_call_internal_p (call_stmt))
1778 1096005 : switch (gimple_call_internal_fn (call_stmt))
1779 : {
1780 88784 : case IFN_ADD_OVERFLOW:
1781 88784 : maybe_optimize_arith_overflow (&gsi, PLUS_EXPR);
1782 88784 : break;
1783 95670 : case IFN_SUB_OVERFLOW:
1784 95670 : maybe_optimize_arith_overflow (&gsi, MINUS_EXPR);
1785 95670 : break;
1786 98174 : case IFN_MUL_OVERFLOW:
1787 98174 : maybe_optimize_arith_overflow (&gsi, MULT_EXPR);
1788 98174 : break;
1789 32121 : case IFN_UADDC:
1790 32121 : if (integer_zerop (gimple_call_arg (call_stmt, 2)))
1791 2986 : maybe_optimize_arith_overflow (&gsi, PLUS_EXPR);
1792 : break;
1793 15170 : case IFN_USUBC:
1794 15170 : if (integer_zerop (gimple_call_arg (call_stmt, 2)))
1795 2088 : maybe_optimize_arith_overflow (&gsi, MINUS_EXPR);
1796 : break;
1797 : default:
1798 : break;
1799 : }
1800 : }
1801 598761637 : else if (gimple_debug_bind_p (stmt))
1802 : {
1803 : /* We are only keeping the last debug-bind of a
1804 : non-DEBUG_EXPR_DECL variable in a series of
1805 : debug-bind stmts. */
1806 295514091 : tree var = gimple_debug_bind_get_var (stmt);
1807 295514091 : if (TREE_CODE (var) != DEBUG_EXPR_DECL
1808 295514091 : && !bitmap_set_bit (debug_seen, DECL_UID (var)))
1809 8084687 : remove_dead_stmt (&gsi, bb, to_remove_edges);
1810 295514091 : continue;
1811 295514091 : }
1812 303247546 : else if (gimple_debug_begin_stmt_p (stmt))
1813 : {
1814 : /* We are only keeping the last debug-begin in a series of
1815 : debug-begin stmts. */
1816 28232370 : if (locs_seen.add (gimple_location (stmt)))
1817 20736 : remove_dead_stmt (&gsi, bb, to_remove_edges);
1818 28232370 : continue;
1819 : }
1820 313554331 : locs_seen.empty ();
1821 313554331 : bitmap_clear (debug_seen);
1822 : }
1823 :
1824 : /* Remove dead PHI nodes. */
1825 84519558 : something_changed |= remove_dead_phis (bb);
1826 84519558 : }
1827 :
1828 : /* First remove queued edges. */
1829 8337506 : if (!to_remove_edges.is_empty ())
1830 : {
1831 : /* Remove edges. We've delayed this to not get bogus debug stmts
1832 : during PHI node removal. */
1833 59381 : for (unsigned i = 0; i < to_remove_edges.length (); ++i)
1834 39237 : remove_edge (to_remove_edges[i]);
1835 20144 : cfg_altered = true;
1836 : }
1837 : /* When we cleared calls_setjmp we can purge all abnormal edges. Do so.
1838 : ??? We'd like to assert that setjmp calls do not pop out of nothing
1839 : but we currently lack a per-stmt way of noting whether a call was
1840 : recognized as returns-twice (or rather receives-control). */
1841 8337506 : if (!cfun->calls_setjmp && had_setjmp)
1842 : {
1843 : /* Make sure we only remove the edges, not dominated blocks. Using
1844 : gimple_purge_dead_abnormal_call_edges would do that and we
1845 : cannot free dominators yet. */
1846 3823 : FOR_EACH_BB_FN (bb, cfun)
1847 9815 : if (gcall *stmt = safe_dyn_cast <gcall *> (*gsi_last_bb (bb)))
1848 2094 : if (!stmt_can_make_abnormal_goto (stmt))
1849 : {
1850 750 : edge_iterator ei;
1851 750 : edge e;
1852 1022 : for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
1853 : {
1854 272 : if (e->flags & EDGE_ABNORMAL)
1855 : {
1856 112 : if (e->flags & EDGE_FALLTHRU)
1857 0 : e->flags &= ~EDGE_ABNORMAL;
1858 : else
1859 112 : remove_edge (e);
1860 112 : cfg_altered = true;
1861 : }
1862 : else
1863 160 : ei_next (&ei);
1864 : }
1865 : }
1866 : }
1867 :
1868 : /* Now remove the unreachable blocks. */
1869 8337506 : if (cfg_altered)
1870 : {
1871 20185 : basic_block prev_bb;
1872 :
1873 20185 : find_unreachable_blocks ();
1874 :
1875 : /* Delete all unreachable basic blocks in reverse dominator order. */
1876 20185 : for (bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
1877 767634 : bb != ENTRY_BLOCK_PTR_FOR_FN (cfun); bb = prev_bb)
1878 : {
1879 747449 : prev_bb = bb->prev_bb;
1880 :
1881 747449 : if ((bb_contains_live_stmts
1882 747402 : && !bitmap_bit_p (bb_contains_live_stmts, bb->index))
1883 1299515 : || !(bb->flags & BB_REACHABLE))
1884 : {
1885 : /* Since we don't track liveness of virtual PHI nodes, it is
1886 : possible that we rendered some PHI nodes unreachable while
1887 : they are still in use. Mark them for renaming. */
1888 210648 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1889 15265 : gsi_next (&gsi))
1890 45793 : if (virtual_operand_p (gimple_phi_result (gsi.phi ())))
1891 : {
1892 15263 : bool found = false;
1893 15263 : imm_use_iterator iter;
1894 :
1895 15411 : FOR_EACH_IMM_USE_STMT (stmt, iter,
1896 : gimple_phi_result (gsi.phi ()))
1897 : {
1898 14758 : if (!(gimple_bb (stmt)->flags & BB_REACHABLE))
1899 121 : continue;
1900 14637 : if (gimple_code (stmt) == GIMPLE_PHI
1901 14637 : || gimple_plf (stmt, STMT_NECESSARY))
1902 : {
1903 : found = true;
1904 : break;
1905 : }
1906 15263 : }
1907 15263 : if (found)
1908 14610 : mark_virtual_phi_result_for_renaming (gsi.phi ());
1909 : }
1910 :
1911 195383 : if (!(bb->flags & BB_REACHABLE))
1912 : {
1913 : /* Speed up the removal of blocks that don't
1914 : dominate others. Walking backwards, this should
1915 : be the common case. ??? Do we need to recompute
1916 : dominators because of cfg_altered? */
1917 42834 : if (!first_dom_son (CDI_DOMINATORS, bb))
1918 42379 : delete_basic_block (bb);
1919 : else
1920 : {
1921 455 : h = get_all_dominated_blocks (CDI_DOMINATORS, bb);
1922 :
1923 2102 : while (h.length ())
1924 : {
1925 1647 : bb = h.pop ();
1926 1647 : prev_bb = bb->prev_bb;
1927 : /* Rearrangements to the CFG may have failed
1928 : to update the dominators tree, so that
1929 : formerly-dominated blocks are now
1930 : otherwise reachable. */
1931 1647 : if (!!(bb->flags & BB_REACHABLE))
1932 0 : continue;
1933 1647 : delete_basic_block (bb);
1934 : }
1935 :
1936 455 : h.release ();
1937 : }
1938 : }
1939 : }
1940 : }
1941 20185 : if (bb_contains_live_stmts)
1942 20177 : propagate_counts ();
1943 : }
1944 :
1945 8337506 : if (bb_postorder)
1946 20144 : free (bb_postorder);
1947 8337506 : bb_postorder = NULL;
1948 :
1949 8337506 : return something_changed;
1950 8337506 : }
1951 :
1952 :
1953 : /* Print out removed statement statistics. */
1954 :
1955 : static void
1956 214 : print_stats (void)
1957 : {
1958 214 : float percg;
1959 :
1960 214 : percg = ((float) stats.removed / (float) stats.total) * 100;
1961 214 : fprintf (dump_file, "Removed %d of %d statements (%d%%)\n",
1962 : stats.removed, stats.total, (int) percg);
1963 :
1964 214 : if (stats.total_phis == 0)
1965 : percg = 0;
1966 : else
1967 38 : percg = ((float) stats.removed_phis / (float) stats.total_phis) * 100;
1968 :
1969 214 : fprintf (dump_file, "Removed %d of %d PHI nodes (%d%%)\n",
1970 : stats.removed_phis, stats.total_phis, (int) percg);
1971 214 : }
1972 :
1973 : /* Initialization for this pass. Set up the used data structures. */
1974 :
1975 : static void
1976 8337506 : tree_dce_init (bool aggressive)
1977 : {
1978 8337506 : memset ((void *) &stats, 0, sizeof (stats));
1979 :
1980 8337506 : if (aggressive)
1981 : {
1982 3637886 : last_stmt_necessary = sbitmap_alloc (last_basic_block_for_fn (cfun));
1983 3637886 : bitmap_clear (last_stmt_necessary);
1984 3637886 : bb_contains_live_stmts = sbitmap_alloc (last_basic_block_for_fn (cfun));
1985 3637886 : bitmap_clear (bb_contains_live_stmts);
1986 : }
1987 :
1988 16675012 : processed = sbitmap_alloc (num_ssa_names + 1);
1989 8337506 : bitmap_clear (processed);
1990 :
1991 8337506 : worklist.create (64);
1992 8337506 : cfg_altered = false;
1993 8337506 : }
1994 :
1995 : /* Cleanup after this pass. */
1996 :
1997 : static void
1998 8337506 : tree_dce_done (bool aggressive)
1999 : {
2000 8337506 : if (aggressive)
2001 : {
2002 3637886 : delete cd;
2003 3637886 : sbitmap_free (visited_control_parents);
2004 3637886 : sbitmap_free (last_stmt_necessary);
2005 3637886 : sbitmap_free (bb_contains_live_stmts);
2006 3637886 : bb_contains_live_stmts = NULL;
2007 : }
2008 :
2009 8337506 : sbitmap_free (processed);
2010 :
2011 8337506 : worklist.release ();
2012 8337506 : }
2013 :
2014 :
2015 : /* Main routine to eliminate dead code.
2016 :
2017 : AGGRESSIVE controls the aggressiveness of the algorithm.
2018 : In conservative mode, we ignore control dependence and simply declare
2019 : all but the most trivially dead branches necessary. This mode is fast.
2020 : In aggressive mode, control dependences are taken into account, which
2021 : results in more dead code elimination, but at the cost of some time. */
2022 :
2023 : static unsigned int
2024 8337506 : perform_tree_ssa_dce (bool aggressive)
2025 : {
2026 8337506 : bool something_changed = 0;
2027 8337506 : unsigned todo = 0;
2028 :
2029 : /* Preheaders are needed for SCEV to work.
2030 : Simple lateches and recorded exits improve chances that loop will
2031 : proved to be finite in testcases such as in loop-15.c and loop-24.c */
2032 8337506 : bool in_loop_pipeline = scev_initialized_p ();
2033 8337506 : if (aggressive && ! in_loop_pipeline)
2034 : {
2035 3403816 : loop_optimizer_init (LOOPS_NORMAL
2036 : | LOOPS_HAVE_RECORDED_EXITS);
2037 3403816 : scev_initialize ();
2038 : }
2039 :
2040 8337506 : if (aggressive && make_forwarders_with_degenerate_phis (cfun))
2041 : todo |= TODO_cleanup_cfg;
2042 :
2043 8337506 : calculate_dominance_info (CDI_DOMINATORS);
2044 :
2045 8337506 : tree_dce_init (aggressive);
2046 :
2047 8337506 : if (aggressive)
2048 : {
2049 : /* Compute control dependence. */
2050 3637886 : calculate_dominance_info (CDI_POST_DOMINATORS);
2051 3637886 : cd = new control_dependences ();
2052 :
2053 7275772 : visited_control_parents =
2054 3637886 : sbitmap_alloc (last_basic_block_for_fn (cfun));
2055 3637886 : bitmap_clear (visited_control_parents);
2056 :
2057 3637886 : mark_dfs_back_edges ();
2058 : }
2059 :
2060 8337506 : find_obviously_necessary_stmts (aggressive);
2061 :
2062 8337506 : if (aggressive && ! in_loop_pipeline)
2063 : {
2064 3403816 : scev_finalize ();
2065 3403816 : loop_optimizer_finalize ();
2066 : }
2067 :
2068 8337506 : longest_chain = 0;
2069 8337506 : total_chain = 0;
2070 8337506 : nr_walks = 0;
2071 8337506 : chain_ovfl = false;
2072 8337506 : visited = BITMAP_ALLOC (NULL);
2073 8337506 : propagate_necessity (aggressive);
2074 8337506 : BITMAP_FREE (visited);
2075 :
2076 8337506 : something_changed |= eliminate_unnecessary_stmts (aggressive);
2077 8337506 : something_changed |= cfg_altered;
2078 :
2079 : /* We do not update postdominators, so free them unconditionally. */
2080 8337506 : free_dominance_info (CDI_POST_DOMINATORS);
2081 :
2082 : /* If we removed paths in the CFG, then we need to update
2083 : dominators as well. I haven't investigated the possibility
2084 : of incrementally updating dominators. */
2085 8337506 : if (cfg_altered)
2086 20185 : free_dominance_info (CDI_DOMINATORS);
2087 :
2088 8337506 : statistics_counter_event (cfun, "Statements deleted", stats.removed);
2089 8337506 : statistics_counter_event (cfun, "PHI nodes deleted", stats.removed_phis);
2090 :
2091 : /* Debugging dumps. */
2092 8337506 : if (dump_file && (dump_flags & (TDF_STATS|TDF_DETAILS)))
2093 214 : print_stats ();
2094 :
2095 8337506 : tree_dce_done (aggressive);
2096 :
2097 8337506 : if (something_changed)
2098 : {
2099 635060 : free_numbers_of_iterations_estimates (cfun);
2100 635060 : if (in_loop_pipeline)
2101 68947 : scev_reset ();
2102 : todo |= TODO_update_ssa | TODO_cleanup_cfg;
2103 : }
2104 8337506 : return todo;
2105 : }
2106 :
2107 : namespace {
2108 :
2109 : const pass_data pass_data_dce =
2110 : {
2111 : GIMPLE_PASS, /* type */
2112 : "dce", /* name */
2113 : OPTGROUP_NONE, /* optinfo_flags */
2114 : TV_TREE_DCE, /* tv_id */
2115 : ( PROP_cfg | PROP_ssa ), /* properties_required */
2116 : 0, /* properties_provided */
2117 : 0, /* properties_destroyed */
2118 : 0, /* todo_flags_start */
2119 : 0, /* todo_flags_finish */
2120 : };
2121 :
2122 : class pass_dce_base : public gimple_opt_pass
2123 : {
2124 : public:
2125 : /* opt_pass methods: */
2126 8341377 : bool gate (function *) final override { return flag_tree_dce != 0; }
2127 3029850 : void set_pass_param (unsigned n, bool param) final override
2128 : {
2129 3029850 : gcc_assert (n == 0 || n == 1);
2130 3029850 : if (n == 0)
2131 1817910 : update_address_taken_p = param;
2132 1211940 : else if (n == 1)
2133 1211940 : remove_unused_locals_p = param;
2134 3029850 : }
2135 :
2136 : protected:
2137 3332835 : pass_dce_base (const pass_data &data, gcc::context *ctxt)
2138 6665670 : : gimple_opt_pass (data, ctxt)
2139 : {}
2140 8337506 : unsigned int execute_dce (function *, bool aggressive)
2141 : {
2142 8337506 : return (perform_tree_ssa_dce (aggressive)
2143 8337506 : | (remove_unused_locals_p ? TODO_remove_unused_locals : 0)
2144 8337506 : | (update_address_taken_p ? TODO_update_address_taken : 0));
2145 : }
2146 :
2147 : private:
2148 : bool update_address_taken_p = false;
2149 : bool remove_unused_locals_p = false;
2150 : }; // class pass_dce_base
2151 :
2152 :
2153 : class pass_dce : public pass_dce_base
2154 : {
2155 : public:
2156 2423880 : pass_dce (gcc::context *ctxt)
2157 4847760 : : pass_dce_base (pass_data_dce, ctxt)
2158 : {}
2159 :
2160 : /* opt_pass methods: */
2161 2120895 : opt_pass * clone () final override { return new pass_dce (m_ctxt); }
2162 4491856 : unsigned int execute (function *func) final override
2163 : {
2164 4491856 : return execute_dce (func, /*aggressive=*/false);
2165 : }
2166 :
2167 : }; // class pass_dce
2168 :
2169 : } // anon namespace
2170 :
2171 : gimple_opt_pass *
2172 302985 : make_pass_dce (gcc::context *ctxt)
2173 : {
2174 302985 : return new pass_dce (ctxt);
2175 : }
2176 :
2177 : namespace {
2178 :
2179 : const pass_data pass_data_cd_dce =
2180 : {
2181 : GIMPLE_PASS, /* type */
2182 : "cddce", /* name */
2183 : OPTGROUP_NONE, /* optinfo_flags */
2184 : TV_TREE_CD_DCE, /* tv_id */
2185 : ( PROP_cfg | PROP_ssa ), /* properties_required */
2186 : 0, /* properties_provided */
2187 : 0, /* properties_destroyed */
2188 : 0, /* todo_flags_start */
2189 : 0, /* todo_flags_finish */
2190 : };
2191 :
2192 : class pass_cd_dce : public pass_dce_base
2193 : {
2194 : public:
2195 908955 : pass_cd_dce (gcc::context *ctxt)
2196 1817910 : : pass_dce_base (pass_data_cd_dce, ctxt)
2197 : {}
2198 :
2199 : /* opt_pass methods: */
2200 605970 : opt_pass * clone () final override { return new pass_cd_dce (m_ctxt); }
2201 3845650 : unsigned int execute (function *func) final override
2202 : {
2203 3845650 : return execute_dce (func, /*aggressive=*/optimize >= 2);
2204 : }
2205 :
2206 : }; // class pass_cd_dce
2207 :
2208 : } // anon namespace
2209 :
2210 : gimple_opt_pass *
2211 302985 : make_pass_cd_dce (gcc::context *ctxt)
2212 : {
2213 302985 : return new pass_cd_dce (ctxt);
2214 : }
2215 :
2216 :
2217 : /* A cheap DCE interface. WORKLIST is a list of possibly dead stmts and
2218 : is consumed by this function. The function has linear complexity in
2219 : the number of dead stmts with a constant factor like the average SSA
2220 : use operands number.
2221 : If no_delete is true (defaults to false) then rather than deleting the
2222 : statement, it is replaced with an assignment to 0. This allows the
2223 : same elimination of statement dependencies, but delays the actual statement
2224 : removal from the IL until the next time DCE is run and they are detected
2225 : as dead statements with no uses. */
2226 :
2227 : void
2228 39142645 : simple_dce_from_worklist (bitmap worklist, bitmap need_eh_cleanup,
2229 : bool no_delete)
2230 : {
2231 39142645 : int phiremoved = 0;
2232 39142645 : int stmtremoved = 0;
2233 75215529 : while (! bitmap_empty_p (worklist))
2234 : {
2235 : /* Pop item. */
2236 36072884 : unsigned i = bitmap_clear_first_set_bit (worklist);
2237 :
2238 36072884 : tree def = ssa_name (i);
2239 : /* Removed by somebody else or still in use.
2240 : Note use in itself for a phi node is not counted as still in use. */
2241 36072884 : if (!def)
2242 14711339 : continue;
2243 35909227 : if (!has_zero_uses (def))
2244 : {
2245 14507951 : gimple *def_stmt = SSA_NAME_DEF_STMT (def);
2246 :
2247 14507951 : if (gimple_code (def_stmt) != GIMPLE_PHI)
2248 14503184 : continue;
2249 :
2250 2691587 : imm_use_iterator use_iter;
2251 2691587 : use_operand_p use_p;
2252 2691587 : bool canremove = true;
2253 :
2254 2777175 : FOR_EACH_IMM_USE_FAST (use_p, use_iter, def)
2255 : {
2256 2772408 : gimple *use_stmt = USE_STMT (use_p);
2257 : /* Ignore debug statements. */
2258 2772408 : if (is_gimple_debug (use_stmt))
2259 78100 : continue;
2260 2694308 : if (use_stmt != def_stmt)
2261 : {
2262 : canremove = false;
2263 : break;
2264 : }
2265 2691587 : }
2266 2691587 : if (!canremove)
2267 2686820 : continue;
2268 : }
2269 :
2270 21406043 : gimple *t = SSA_NAME_DEF_STMT (def);
2271 21406043 : if (gimple_has_side_effects (t))
2272 : {
2273 41983 : gcall *call = dyn_cast <gcall *> (t);
2274 : // Remove the lhs for a call if not no_delete.
2275 41983 : if (call && !no_delete)
2276 : {
2277 37127 : gimple_call_set_lhs (call, NULL_TREE);
2278 37127 : update_stmt (call);
2279 37127 : release_ssa_name (def);
2280 : }
2281 41983 : continue;
2282 41983 : }
2283 :
2284 : /* The defining statement needs to be defining only this name.
2285 : ASM is the only statement that can define more than one
2286 : name. */
2287 21364060 : if (is_a<gasm *>(t)
2288 21364060 : && !single_ssa_def_operand (t, SSA_OP_ALL_DEFS))
2289 15 : continue;
2290 :
2291 : /* Don't remove statements that are needed for non-call
2292 : eh to work. */
2293 21364045 : if (stmt_unremovable_because_of_non_call_eh_p (cfun, t))
2294 2500 : continue;
2295 :
2296 : /* Tell the caller that we removed a statement that might
2297 : throw so it could cleanup the cfg for that block. */
2298 21361545 : if (need_eh_cleanup && stmt_could_throw_p (cfun, t))
2299 134 : bitmap_set_bit (need_eh_cleanup, gimple_bb (t)->index);
2300 :
2301 : /* Add uses to the worklist. */
2302 21361545 : ssa_op_iter iter;
2303 21361545 : use_operand_p use_p;
2304 59840446 : FOR_EACH_PHI_OR_STMT_USE (use_p, t, iter, SSA_OP_USE)
2305 : {
2306 17117356 : tree use = USE_FROM_PTR (use_p);
2307 17117356 : if (TREE_CODE (use) == SSA_NAME
2308 17117356 : && ! SSA_NAME_IS_DEFAULT_DEF (use))
2309 15385727 : bitmap_set_bit (worklist, SSA_NAME_VERSION (use));
2310 : }
2311 :
2312 : /* Remove stmt. */
2313 21361545 : if (dump_file && (dump_flags & TDF_DETAILS))
2314 : {
2315 638 : fprintf (dump_file, "Removing dead stmt:");
2316 638 : print_gimple_stmt (dump_file, t, 0);
2317 : }
2318 21361545 : gimple_stmt_iterator gsi = gsi_for_stmt (t);
2319 21361545 : if (gimple_code (t) == GIMPLE_PHI)
2320 : {
2321 3201826 : if (no_delete)
2322 : {
2323 45 : gphi *phi = as_a<gphi *> (t);
2324 45 : tree zero = build_zero_cst (TREE_TYPE (def));
2325 45 : edge_iterator ei;
2326 45 : edge e;
2327 118 : FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
2328 73 : if (!(e->flags & EDGE_ABNORMAL))
2329 69 : SET_PHI_ARG_DEF (phi, e->dest_idx, zero);
2330 45 : reset_flow_sensitive_info (def);
2331 : }
2332 : else
2333 3201781 : remove_phi_node (&gsi, true);
2334 :
2335 3201826 : phiremoved++;
2336 : }
2337 : else
2338 : {
2339 18159719 : unlink_stmt_vdef (t);
2340 18159719 : if (no_delete)
2341 : {
2342 400 : tree zero = build_zero_cst (TREE_TYPE (def));
2343 400 : gassign *new_stmt = gimple_build_assign (def, zero);
2344 400 : gsi_replace (&gsi, new_stmt, true);
2345 400 : reset_flow_sensitive_info (def);
2346 : }
2347 : else
2348 : {
2349 18159319 : gsi_remove (&gsi, true);
2350 18159319 : release_defs (t);
2351 : }
2352 18159719 : stmtremoved++;
2353 : }
2354 : }
2355 78284765 : statistics_counter_event (cfun, no_delete ? "PHIs rewritten" : "PHIs removed",
2356 : phiremoved);
2357 78284765 : statistics_counter_event (cfun, no_delete ? "Statements rewritten"
2358 : : "Statements removed", stmtremoved);
2359 39142645 : }
|