]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-cfgcleanup.c
[PATCH 7/9] ENABLE_CHECKING refactoring: middle-end, LTO FE
[thirdparty/gcc.git] / gcc / tree-cfgcleanup.c
1 /* CFG cleanup for trees.
2 Copyright (C) 2001-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "cfghooks.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "rtl.h"
28 #include "ssa.h"
29 #include "alias.h"
30 #include "fold-const.h"
31 #include "tm_p.h"
32 #include "cfganal.h"
33 #include "cfgcleanup.h"
34 #include "diagnostic-core.h"
35 #include "flags.h"
36 #include "langhooks.h"
37 #include "internal-fn.h"
38 #include "tree-eh.h"
39 #include "gimplify.h"
40 #include "gimple-iterator.h"
41 #include "tree-cfg.h"
42 #include "tree-ssa-loop-manip.h"
43 #include "insn-config.h"
44 #include "expmed.h"
45 #include "dojump.h"
46 #include "explow.h"
47 #include "calls.h"
48 #include "emit-rtl.h"
49 #include "varasm.h"
50 #include "stmt.h"
51 #include "expr.h"
52 #include "tree-dfa.h"
53 #include "tree-ssa.h"
54 #include "tree-pass.h"
55 #include "except.h"
56 #include "cfgloop.h"
57 #include "tree-ssa-propagate.h"
58 #include "tree-scalar-evolution.h"
59 #include "gimple-match.h"
60 #include "gimple-fold.h"
61
62
63 /* The set of blocks in that at least one of the following changes happened:
64 -- the statement at the end of the block was changed
65 -- the block was newly created
66 -- the set of the predecessors of the block changed
67 -- the set of the successors of the block changed
68 ??? Maybe we could track these changes separately, since they determine
69 what cleanups it makes sense to try on the block. */
70 bitmap cfgcleanup_altered_bbs;
71
72 /* Remove any fallthru edge from EV. Return true if an edge was removed. */
73
74 static bool
75 remove_fallthru_edge (vec<edge, va_gc> *ev)
76 {
77 edge_iterator ei;
78 edge e;
79
80 FOR_EACH_EDGE (e, ei, ev)
81 if ((e->flags & EDGE_FALLTHRU) != 0)
82 {
83 if (e->flags & EDGE_COMPLEX)
84 e->flags &= ~EDGE_FALLTHRU;
85 else
86 remove_edge_and_dominated_blocks (e);
87 return true;
88 }
89 return false;
90 }
91
92
93 /* Disconnect an unreachable block in the control expression starting
94 at block BB. */
95
96 static bool
97 cleanup_control_expr_graph (basic_block bb, gimple_stmt_iterator gsi)
98 {
99 edge taken_edge;
100 bool retval = false;
101 gimple *stmt = gsi_stmt (gsi);
102
103 if (!single_succ_p (bb))
104 {
105 edge e;
106 edge_iterator ei;
107 bool warned;
108 tree val = NULL_TREE;
109
110 fold_defer_overflow_warnings ();
111 switch (gimple_code (stmt))
112 {
113 case GIMPLE_COND:
114 {
115 code_helper rcode;
116 tree ops[3] = {};
117 if (gimple_simplify (stmt, &rcode, ops, NULL, no_follow_ssa_edges,
118 no_follow_ssa_edges)
119 && rcode == INTEGER_CST)
120 val = ops[0];
121 break;
122 }
123
124 case GIMPLE_SWITCH:
125 val = gimple_switch_index (as_a <gswitch *> (stmt));
126 break;
127
128 default:
129 ;
130 }
131 taken_edge = find_taken_edge (bb, val);
132 if (!taken_edge)
133 {
134 fold_undefer_and_ignore_overflow_warnings ();
135 return false;
136 }
137
138 /* Remove all the edges except the one that is always executed. */
139 warned = false;
140 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
141 {
142 if (e != taken_edge)
143 {
144 if (!warned)
145 {
146 fold_undefer_overflow_warnings
147 (true, stmt, WARN_STRICT_OVERFLOW_CONDITIONAL);
148 warned = true;
149 }
150
151 taken_edge->probability += e->probability;
152 taken_edge->count += e->count;
153 remove_edge_and_dominated_blocks (e);
154 retval = true;
155 }
156 else
157 ei_next (&ei);
158 }
159 if (!warned)
160 fold_undefer_and_ignore_overflow_warnings ();
161 if (taken_edge->probability > REG_BR_PROB_BASE)
162 taken_edge->probability = REG_BR_PROB_BASE;
163 }
164 else
165 taken_edge = single_succ_edge (bb);
166
167 bitmap_set_bit (cfgcleanup_altered_bbs, bb->index);
168 gsi_remove (&gsi, true);
169 taken_edge->flags = EDGE_FALLTHRU;
170
171 return retval;
172 }
173
174 /* Cleanup the GF_CALL_CTRL_ALTERING flag according to
175 to updated gimple_call_flags. */
176
177 static void
178 cleanup_call_ctrl_altering_flag (gimple *bb_end)
179 {
180 if (!is_gimple_call (bb_end)
181 || !gimple_call_ctrl_altering_p (bb_end))
182 return;
183
184 int flags = gimple_call_flags (bb_end);
185 if (((flags & (ECF_CONST | ECF_PURE))
186 && !(flags & ECF_LOOPING_CONST_OR_PURE))
187 || (flags & ECF_LEAF))
188 gimple_call_set_ctrl_altering (bb_end, false);
189 }
190
191 /* Try to remove superfluous control structures in basic block BB. Returns
192 true if anything changes. */
193
194 static bool
195 cleanup_control_flow_bb (basic_block bb)
196 {
197 gimple_stmt_iterator gsi;
198 bool retval = false;
199 gimple *stmt;
200
201 /* If the last statement of the block could throw and now cannot,
202 we need to prune cfg. */
203 retval |= gimple_purge_dead_eh_edges (bb);
204
205 gsi = gsi_last_bb (bb);
206 if (gsi_end_p (gsi))
207 return retval;
208
209 stmt = gsi_stmt (gsi);
210
211 /* Try to cleanup ctrl altering flag for call which ends bb. */
212 cleanup_call_ctrl_altering_flag (stmt);
213
214 if (gimple_code (stmt) == GIMPLE_COND
215 || gimple_code (stmt) == GIMPLE_SWITCH)
216 retval |= cleanup_control_expr_graph (bb, gsi);
217 else if (gimple_code (stmt) == GIMPLE_GOTO
218 && TREE_CODE (gimple_goto_dest (stmt)) == ADDR_EXPR
219 && (TREE_CODE (TREE_OPERAND (gimple_goto_dest (stmt), 0))
220 == LABEL_DECL))
221 {
222 /* If we had a computed goto which has a compile-time determinable
223 destination, then we can eliminate the goto. */
224 edge e;
225 tree label;
226 edge_iterator ei;
227 basic_block target_block;
228
229 /* First look at all the outgoing edges. Delete any outgoing
230 edges which do not go to the right block. For the one
231 edge which goes to the right block, fix up its flags. */
232 label = TREE_OPERAND (gimple_goto_dest (stmt), 0);
233 target_block = label_to_block (label);
234 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
235 {
236 if (e->dest != target_block)
237 remove_edge_and_dominated_blocks (e);
238 else
239 {
240 /* Turn off the EDGE_ABNORMAL flag. */
241 e->flags &= ~EDGE_ABNORMAL;
242
243 /* And set EDGE_FALLTHRU. */
244 e->flags |= EDGE_FALLTHRU;
245 ei_next (&ei);
246 }
247 }
248
249 bitmap_set_bit (cfgcleanup_altered_bbs, bb->index);
250 bitmap_set_bit (cfgcleanup_altered_bbs, target_block->index);
251
252 /* Remove the GOTO_EXPR as it is not needed. The CFG has all the
253 relevant information we need. */
254 gsi_remove (&gsi, true);
255 retval = true;
256 }
257
258 /* Check for indirect calls that have been turned into
259 noreturn calls. */
260 else if (is_gimple_call (stmt)
261 && gimple_call_noreturn_p (stmt)
262 && remove_fallthru_edge (bb->succs))
263 retval = true;
264
265 return retval;
266 }
267
268 /* Return true if basic block BB does nothing except pass control
269 flow to another block and that we can safely insert a label at
270 the start of the successor block.
271
272 As a precondition, we require that BB be not equal to
273 the entry block. */
274
275 static bool
276 tree_forwarder_block_p (basic_block bb, bool phi_wanted)
277 {
278 gimple_stmt_iterator gsi;
279 location_t locus;
280
281 /* BB must have a single outgoing edge. */
282 if (single_succ_p (bb) != 1
283 /* If PHI_WANTED is false, BB must not have any PHI nodes.
284 Otherwise, BB must have PHI nodes. */
285 || gimple_seq_empty_p (phi_nodes (bb)) == phi_wanted
286 /* BB may not be a predecessor of the exit block. */
287 || single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun)
288 /* Nor should this be an infinite loop. */
289 || single_succ (bb) == bb
290 /* BB may not have an abnormal outgoing edge. */
291 || (single_succ_edge (bb)->flags & EDGE_ABNORMAL))
292 return false;
293
294 gcc_checking_assert (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun));
295
296 locus = single_succ_edge (bb)->goto_locus;
297
298 /* There should not be an edge coming from entry, or an EH edge. */
299 {
300 edge_iterator ei;
301 edge e;
302
303 FOR_EACH_EDGE (e, ei, bb->preds)
304 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun) || (e->flags & EDGE_EH))
305 return false;
306 /* If goto_locus of any of the edges differs, prevent removing
307 the forwarder block for -O0. */
308 else if (optimize == 0 && e->goto_locus != locus)
309 return false;
310 }
311
312 /* Now walk through the statements backward. We can ignore labels,
313 anything else means this is not a forwarder block. */
314 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
315 {
316 gimple *stmt = gsi_stmt (gsi);
317
318 switch (gimple_code (stmt))
319 {
320 case GIMPLE_LABEL:
321 if (DECL_NONLOCAL (gimple_label_label (as_a <glabel *> (stmt))))
322 return false;
323 if (optimize == 0 && gimple_location (stmt) != locus)
324 return false;
325 break;
326
327 /* ??? For now, hope there's a corresponding debug
328 assignment at the destination. */
329 case GIMPLE_DEBUG:
330 break;
331
332 default:
333 return false;
334 }
335 }
336
337 if (current_loops)
338 {
339 basic_block dest;
340 /* Protect loop headers. */
341 if (bb->loop_father->header == bb)
342 return false;
343
344 dest = EDGE_SUCC (bb, 0)->dest;
345 /* Protect loop preheaders and latches if requested. */
346 if (dest->loop_father->header == dest)
347 {
348 if (bb->loop_father == dest->loop_father)
349 {
350 if (loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES))
351 return false;
352 /* If bb doesn't have a single predecessor we'd make this
353 loop have multiple latches. Don't do that if that
354 would in turn require disambiguating them. */
355 return (single_pred_p (bb)
356 || loops_state_satisfies_p
357 (LOOPS_MAY_HAVE_MULTIPLE_LATCHES));
358 }
359 else if (bb->loop_father == loop_outer (dest->loop_father))
360 return !loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS);
361 /* Always preserve other edges into loop headers that are
362 not simple latches or preheaders. */
363 return false;
364 }
365 }
366
367 return true;
368 }
369
370 /* If all the PHI nodes in DEST have alternatives for E1 and E2 and
371 those alternatives are equal in each of the PHI nodes, then return
372 true, else return false. */
373
374 static bool
375 phi_alternatives_equal (basic_block dest, edge e1, edge e2)
376 {
377 int n1 = e1->dest_idx;
378 int n2 = e2->dest_idx;
379 gphi_iterator gsi;
380
381 for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi))
382 {
383 gphi *phi = gsi.phi ();
384 tree val1 = gimple_phi_arg_def (phi, n1);
385 tree val2 = gimple_phi_arg_def (phi, n2);
386
387 gcc_assert (val1 != NULL_TREE);
388 gcc_assert (val2 != NULL_TREE);
389
390 if (!operand_equal_for_phi_arg_p (val1, val2))
391 return false;
392 }
393
394 return true;
395 }
396
397 /* Removes forwarder block BB. Returns false if this failed. */
398
399 static bool
400 remove_forwarder_block (basic_block bb)
401 {
402 edge succ = single_succ_edge (bb), e, s;
403 basic_block dest = succ->dest;
404 gimple *label;
405 edge_iterator ei;
406 gimple_stmt_iterator gsi, gsi_to;
407 bool can_move_debug_stmts;
408
409 /* We check for infinite loops already in tree_forwarder_block_p.
410 However it may happen that the infinite loop is created
411 afterwards due to removal of forwarders. */
412 if (dest == bb)
413 return false;
414
415 /* If the destination block consists of a nonlocal label or is a
416 EH landing pad, do not merge it. */
417 label = first_stmt (dest);
418 if (label)
419 if (glabel *label_stmt = dyn_cast <glabel *> (label))
420 if (DECL_NONLOCAL (gimple_label_label (label_stmt))
421 || EH_LANDING_PAD_NR (gimple_label_label (label_stmt)) != 0)
422 return false;
423
424 /* If there is an abnormal edge to basic block BB, but not into
425 dest, problems might occur during removal of the phi node at out
426 of ssa due to overlapping live ranges of registers.
427
428 If there is an abnormal edge in DEST, the problems would occur
429 anyway since cleanup_dead_labels would then merge the labels for
430 two different eh regions, and rest of exception handling code
431 does not like it.
432
433 So if there is an abnormal edge to BB, proceed only if there is
434 no abnormal edge to DEST and there are no phi nodes in DEST. */
435 if (bb_has_abnormal_pred (bb)
436 && (bb_has_abnormal_pred (dest)
437 || !gimple_seq_empty_p (phi_nodes (dest))))
438 return false;
439
440 /* If there are phi nodes in DEST, and some of the blocks that are
441 predecessors of BB are also predecessors of DEST, check that the
442 phi node arguments match. */
443 if (!gimple_seq_empty_p (phi_nodes (dest)))
444 {
445 FOR_EACH_EDGE (e, ei, bb->preds)
446 {
447 s = find_edge (e->src, dest);
448 if (!s)
449 continue;
450
451 if (!phi_alternatives_equal (dest, succ, s))
452 return false;
453 }
454 }
455
456 can_move_debug_stmts = MAY_HAVE_DEBUG_STMTS && single_pred_p (dest);
457
458 basic_block pred = NULL;
459 if (single_pred_p (bb))
460 pred = single_pred (bb);
461
462 /* Redirect the edges. */
463 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
464 {
465 bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
466
467 if (e->flags & EDGE_ABNORMAL)
468 {
469 /* If there is an abnormal edge, redirect it anyway, and
470 move the labels to the new block to make it legal. */
471 s = redirect_edge_succ_nodup (e, dest);
472 }
473 else
474 s = redirect_edge_and_branch (e, dest);
475
476 if (s == e)
477 {
478 /* Create arguments for the phi nodes, since the edge was not
479 here before. */
480 for (gphi_iterator psi = gsi_start_phis (dest);
481 !gsi_end_p (psi);
482 gsi_next (&psi))
483 {
484 gphi *phi = psi.phi ();
485 source_location l = gimple_phi_arg_location_from_edge (phi, succ);
486 tree def = gimple_phi_arg_def (phi, succ->dest_idx);
487 add_phi_arg (phi, unshare_expr (def), s, l);
488 }
489 }
490 }
491
492 /* Move nonlocal labels and computed goto targets as well as user
493 defined labels and labels with an EH landing pad number to the
494 new block, so that the redirection of the abnormal edges works,
495 jump targets end up in a sane place and debug information for
496 labels is retained. */
497 gsi_to = gsi_start_bb (dest);
498 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
499 {
500 tree decl;
501 label = gsi_stmt (gsi);
502 if (is_gimple_debug (label))
503 break;
504 decl = gimple_label_label (as_a <glabel *> (label));
505 if (EH_LANDING_PAD_NR (decl) != 0
506 || DECL_NONLOCAL (decl)
507 || FORCED_LABEL (decl)
508 || !DECL_ARTIFICIAL (decl))
509 {
510 gsi_remove (&gsi, false);
511 gsi_insert_before (&gsi_to, label, GSI_SAME_STMT);
512 }
513 else
514 gsi_next (&gsi);
515 }
516
517 /* Move debug statements if the destination has a single predecessor. */
518 if (can_move_debug_stmts)
519 {
520 gsi_to = gsi_after_labels (dest);
521 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); )
522 {
523 gimple *debug = gsi_stmt (gsi);
524 if (!is_gimple_debug (debug))
525 break;
526 gsi_remove (&gsi, false);
527 gsi_insert_before (&gsi_to, debug, GSI_SAME_STMT);
528 }
529 }
530
531 bitmap_set_bit (cfgcleanup_altered_bbs, dest->index);
532
533 /* Update the dominators. */
534 if (dom_info_available_p (CDI_DOMINATORS))
535 {
536 basic_block dom, dombb, domdest;
537
538 dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
539 domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
540 if (domdest == bb)
541 {
542 /* Shortcut to avoid calling (relatively expensive)
543 nearest_common_dominator unless necessary. */
544 dom = dombb;
545 }
546 else
547 dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
548
549 set_immediate_dominator (CDI_DOMINATORS, dest, dom);
550 }
551
552 /* Adjust latch infomation of BB's parent loop as otherwise
553 the cfg hook has a hard time not to kill the loop. */
554 if (current_loops && bb->loop_father->latch == bb)
555 bb->loop_father->latch = pred;
556
557 /* And kill the forwarder block. */
558 delete_basic_block (bb);
559
560 return true;
561 }
562
563 /* STMT is a call that has been discovered noreturn. Split the
564 block to prepare fixing up the CFG and remove LHS.
565 Return true if cleanup-cfg needs to run. */
566
567 bool
568 fixup_noreturn_call (gimple *stmt)
569 {
570 basic_block bb = gimple_bb (stmt);
571 bool changed = false;
572
573 if (gimple_call_builtin_p (stmt, BUILT_IN_RETURN))
574 return false;
575
576 /* First split basic block if stmt is not last. */
577 if (stmt != gsi_stmt (gsi_last_bb (bb)))
578 {
579 if (stmt == gsi_stmt (gsi_last_nondebug_bb (bb)))
580 {
581 /* Don't split if there are only debug stmts
582 after stmt, that can result in -fcompare-debug
583 failures. Remove the debug stmts instead,
584 they should be all unreachable anyway. */
585 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
586 for (gsi_next (&gsi); !gsi_end_p (gsi); )
587 gsi_remove (&gsi, true);
588 }
589 else
590 {
591 split_block (bb, stmt);
592 changed = true;
593 }
594 }
595
596 /* If there is an LHS, remove it, but only if its type has fixed size.
597 The LHS will need to be recreated during RTL expansion and creating
598 temporaries of variable-sized types is not supported. */
599 tree lhs = gimple_call_lhs (stmt);
600 if (lhs && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
601 {
602 gimple_call_set_lhs (stmt, NULL_TREE);
603
604 /* We need to fix up the SSA name to avoid checking errors. */
605 if (TREE_CODE (lhs) == SSA_NAME)
606 {
607 tree new_var = create_tmp_reg (TREE_TYPE (lhs));
608 SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs, new_var);
609 SSA_NAME_DEF_STMT (lhs) = gimple_build_nop ();
610 set_ssa_default_def (cfun, new_var, lhs);
611 }
612
613 update_stmt (stmt);
614 }
615
616 /* Mark the call as altering control flow. */
617 if (!gimple_call_ctrl_altering_p (stmt))
618 {
619 gimple_call_set_ctrl_altering (stmt, true);
620 changed = true;
621 }
622
623 return changed;
624 }
625
626
627 /* Tries to cleanup cfg in basic block BB. Returns true if anything
628 changes. */
629
630 static bool
631 cleanup_tree_cfg_bb (basic_block bb)
632 {
633 bool retval = cleanup_control_flow_bb (bb);
634
635 if (tree_forwarder_block_p (bb, false)
636 && remove_forwarder_block (bb))
637 return true;
638
639 /* Merging the blocks may create new opportunities for folding
640 conditional branches (due to the elimination of single-valued PHI
641 nodes). */
642 if (single_succ_p (bb)
643 && can_merge_blocks_p (bb, single_succ (bb)))
644 {
645 /* If there is a merge opportunity with the predecessor
646 do nothing now but wait until we process the predecessor.
647 This happens when we visit BBs in a non-optimal order and
648 avoids quadratic behavior with adjusting stmts BB pointer. */
649 if (single_pred_p (bb)
650 && can_merge_blocks_p (single_pred (bb), bb))
651 ;
652 else
653 {
654 merge_blocks (bb, single_succ (bb));
655 return true;
656 }
657 }
658
659 return retval;
660 }
661
662 /* Iterate the cfg cleanups, while anything changes. */
663
664 static bool
665 cleanup_tree_cfg_1 (void)
666 {
667 bool retval = false;
668 basic_block bb;
669 unsigned i, n;
670
671 /* Prepare the worklists of altered blocks. */
672 cfgcleanup_altered_bbs = BITMAP_ALLOC (NULL);
673
674 /* During forwarder block cleanup, we may redirect edges out of
675 SWITCH_EXPRs, which can get expensive. So we want to enable
676 recording of edge to CASE_LABEL_EXPR. */
677 start_recording_case_labels ();
678
679 /* Start by iterating over all basic blocks. We cannot use FOR_EACH_BB_FN,
680 since the basic blocks may get removed. */
681 n = last_basic_block_for_fn (cfun);
682 for (i = NUM_FIXED_BLOCKS; i < n; i++)
683 {
684 bb = BASIC_BLOCK_FOR_FN (cfun, i);
685 if (bb)
686 retval |= cleanup_tree_cfg_bb (bb);
687 }
688
689 /* Now process the altered blocks, as long as any are available. */
690 while (!bitmap_empty_p (cfgcleanup_altered_bbs))
691 {
692 i = bitmap_first_set_bit (cfgcleanup_altered_bbs);
693 bitmap_clear_bit (cfgcleanup_altered_bbs, i);
694 if (i < NUM_FIXED_BLOCKS)
695 continue;
696
697 bb = BASIC_BLOCK_FOR_FN (cfun, i);
698 if (!bb)
699 continue;
700
701 retval |= cleanup_tree_cfg_bb (bb);
702 }
703
704 end_recording_case_labels ();
705 BITMAP_FREE (cfgcleanup_altered_bbs);
706 return retval;
707 }
708
709
710 /* Remove unreachable blocks and other miscellaneous clean up work.
711 Return true if the flowgraph was modified, false otherwise. */
712
713 static bool
714 cleanup_tree_cfg_noloop (void)
715 {
716 bool changed;
717
718 timevar_push (TV_TREE_CLEANUP_CFG);
719
720 /* Iterate until there are no more cleanups left to do. If any
721 iteration changed the flowgraph, set CHANGED to true.
722
723 If dominance information is available, there cannot be any unreachable
724 blocks. */
725 if (!dom_info_available_p (CDI_DOMINATORS))
726 {
727 changed = delete_unreachable_blocks ();
728 calculate_dominance_info (CDI_DOMINATORS);
729 }
730 else
731 {
732 checking_verify_dominators (CDI_DOMINATORS);
733 changed = false;
734 }
735
736 changed |= cleanup_tree_cfg_1 ();
737
738 gcc_assert (dom_info_available_p (CDI_DOMINATORS));
739 compact_blocks ();
740
741 checking_verify_flow_info ();
742
743 timevar_pop (TV_TREE_CLEANUP_CFG);
744
745 if (changed && current_loops)
746 loops_state_set (LOOPS_NEED_FIXUP);
747
748 return changed;
749 }
750
751 /* Repairs loop structures. */
752
753 static void
754 repair_loop_structures (void)
755 {
756 bitmap changed_bbs;
757 unsigned n_new_loops;
758
759 calculate_dominance_info (CDI_DOMINATORS);
760
761 timevar_push (TV_REPAIR_LOOPS);
762 changed_bbs = BITMAP_ALLOC (NULL);
763 n_new_loops = fix_loop_structure (changed_bbs);
764
765 /* This usually does nothing. But sometimes parts of cfg that originally
766 were inside a loop get out of it due to edge removal (since they
767 become unreachable by back edges from latch). Also a former
768 irreducible loop can become reducible - in this case force a full
769 rewrite into loop-closed SSA form. */
770 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
771 rewrite_into_loop_closed_ssa (n_new_loops ? NULL : changed_bbs,
772 TODO_update_ssa);
773
774 BITMAP_FREE (changed_bbs);
775
776 checking_verify_loop_structure ();
777 scev_reset ();
778
779 timevar_pop (TV_REPAIR_LOOPS);
780 }
781
782 /* Cleanup cfg and repair loop structures. */
783
784 bool
785 cleanup_tree_cfg (void)
786 {
787 bool changed = cleanup_tree_cfg_noloop ();
788
789 if (current_loops != NULL
790 && loops_state_satisfies_p (LOOPS_NEED_FIXUP))
791 repair_loop_structures ();
792
793 return changed;
794 }
795
796 /* Tries to merge the PHI nodes at BB into those at BB's sole successor.
797 Returns true if successful. */
798
799 static bool
800 remove_forwarder_block_with_phi (basic_block bb)
801 {
802 edge succ = single_succ_edge (bb);
803 basic_block dest = succ->dest;
804 gimple *label;
805 basic_block dombb, domdest, dom;
806
807 /* We check for infinite loops already in tree_forwarder_block_p.
808 However it may happen that the infinite loop is created
809 afterwards due to removal of forwarders. */
810 if (dest == bb)
811 return false;
812
813 /* If the destination block consists of a nonlocal label, do not
814 merge it. */
815 label = first_stmt (dest);
816 if (label)
817 if (glabel *label_stmt = dyn_cast <glabel *> (label))
818 if (DECL_NONLOCAL (gimple_label_label (label_stmt)))
819 return false;
820
821 /* Record BB's single pred in case we need to update the father
822 loop's latch information later. */
823 basic_block pred = NULL;
824 if (single_pred_p (bb))
825 pred = single_pred (bb);
826
827 /* Redirect each incoming edge to BB to DEST. */
828 while (EDGE_COUNT (bb->preds) > 0)
829 {
830 edge e = EDGE_PRED (bb, 0), s;
831 gphi_iterator gsi;
832
833 s = find_edge (e->src, dest);
834 if (s)
835 {
836 /* We already have an edge S from E->src to DEST. If S and
837 E->dest's sole successor edge have the same PHI arguments
838 at DEST, redirect S to DEST. */
839 if (phi_alternatives_equal (dest, s, succ))
840 {
841 e = redirect_edge_and_branch (e, dest);
842 redirect_edge_var_map_clear (e);
843 continue;
844 }
845
846 /* PHI arguments are different. Create a forwarder block by
847 splitting E so that we can merge PHI arguments on E to
848 DEST. */
849 e = single_succ_edge (split_edge (e));
850 }
851
852 s = redirect_edge_and_branch (e, dest);
853
854 /* redirect_edge_and_branch must not create a new edge. */
855 gcc_assert (s == e);
856
857 /* Add to the PHI nodes at DEST each PHI argument removed at the
858 destination of E. */
859 for (gsi = gsi_start_phis (dest);
860 !gsi_end_p (gsi);
861 gsi_next (&gsi))
862 {
863 gphi *phi = gsi.phi ();
864 tree def = gimple_phi_arg_def (phi, succ->dest_idx);
865 source_location locus = gimple_phi_arg_location_from_edge (phi, succ);
866
867 if (TREE_CODE (def) == SSA_NAME)
868 {
869 /* If DEF is one of the results of PHI nodes removed during
870 redirection, replace it with the PHI argument that used
871 to be on E. */
872 vec<edge_var_map> *head = redirect_edge_var_map_vector (e);
873 size_t length = head ? head->length () : 0;
874 for (size_t i = 0; i < length; i++)
875 {
876 edge_var_map *vm = &(*head)[i];
877 tree old_arg = redirect_edge_var_map_result (vm);
878 tree new_arg = redirect_edge_var_map_def (vm);
879
880 if (def == old_arg)
881 {
882 def = new_arg;
883 locus = redirect_edge_var_map_location (vm);
884 break;
885 }
886 }
887 }
888
889 add_phi_arg (phi, def, s, locus);
890 }
891
892 redirect_edge_var_map_clear (e);
893 }
894
895 /* Update the dominators. */
896 dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
897 domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
898 if (domdest == bb)
899 {
900 /* Shortcut to avoid calling (relatively expensive)
901 nearest_common_dominator unless necessary. */
902 dom = dombb;
903 }
904 else
905 dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
906
907 set_immediate_dominator (CDI_DOMINATORS, dest, dom);
908
909 /* Adjust latch infomation of BB's parent loop as otherwise
910 the cfg hook has a hard time not to kill the loop. */
911 if (current_loops && bb->loop_father->latch == bb)
912 bb->loop_father->latch = pred;
913
914 /* Remove BB since all of BB's incoming edges have been redirected
915 to DEST. */
916 delete_basic_block (bb);
917
918 return true;
919 }
920
921 /* This pass merges PHI nodes if one feeds into another. For example,
922 suppose we have the following:
923
924 goto <bb 9> (<L9>);
925
926 <L8>:;
927 tem_17 = foo ();
928
929 # tem_6 = PHI <tem_17(8), tem_23(7)>;
930 <L9>:;
931
932 # tem_3 = PHI <tem_6(9), tem_2(5)>;
933 <L10>:;
934
935 Then we merge the first PHI node into the second one like so:
936
937 goto <bb 9> (<L10>);
938
939 <L8>:;
940 tem_17 = foo ();
941
942 # tem_3 = PHI <tem_23(7), tem_2(5), tem_17(8)>;
943 <L10>:;
944 */
945
946 namespace {
947
948 const pass_data pass_data_merge_phi =
949 {
950 GIMPLE_PASS, /* type */
951 "mergephi", /* name */
952 OPTGROUP_NONE, /* optinfo_flags */
953 TV_TREE_MERGE_PHI, /* tv_id */
954 ( PROP_cfg | PROP_ssa ), /* properties_required */
955 0, /* properties_provided */
956 0, /* properties_destroyed */
957 0, /* todo_flags_start */
958 0, /* todo_flags_finish */
959 };
960
961 class pass_merge_phi : public gimple_opt_pass
962 {
963 public:
964 pass_merge_phi (gcc::context *ctxt)
965 : gimple_opt_pass (pass_data_merge_phi, ctxt)
966 {}
967
968 /* opt_pass methods: */
969 opt_pass * clone () { return new pass_merge_phi (m_ctxt); }
970 virtual unsigned int execute (function *);
971
972 }; // class pass_merge_phi
973
974 unsigned int
975 pass_merge_phi::execute (function *fun)
976 {
977 basic_block *worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (fun));
978 basic_block *current = worklist;
979 basic_block bb;
980
981 calculate_dominance_info (CDI_DOMINATORS);
982
983 /* Find all PHI nodes that we may be able to merge. */
984 FOR_EACH_BB_FN (bb, fun)
985 {
986 basic_block dest;
987
988 /* Look for a forwarder block with PHI nodes. */
989 if (!tree_forwarder_block_p (bb, true))
990 continue;
991
992 dest = single_succ (bb);
993
994 /* We have to feed into another basic block with PHI
995 nodes. */
996 if (gimple_seq_empty_p (phi_nodes (dest))
997 /* We don't want to deal with a basic block with
998 abnormal edges. */
999 || bb_has_abnormal_pred (bb))
1000 continue;
1001
1002 if (!dominated_by_p (CDI_DOMINATORS, dest, bb))
1003 {
1004 /* If BB does not dominate DEST, then the PHI nodes at
1005 DEST must be the only users of the results of the PHI
1006 nodes at BB. */
1007 *current++ = bb;
1008 }
1009 else
1010 {
1011 gphi_iterator gsi;
1012 unsigned int dest_idx = single_succ_edge (bb)->dest_idx;
1013
1014 /* BB dominates DEST. There may be many users of the PHI
1015 nodes in BB. However, there is still a trivial case we
1016 can handle. If the result of every PHI in BB is used
1017 only by a PHI in DEST, then we can trivially merge the
1018 PHI nodes from BB into DEST. */
1019 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1020 gsi_next (&gsi))
1021 {
1022 gphi *phi = gsi.phi ();
1023 tree result = gimple_phi_result (phi);
1024 use_operand_p imm_use;
1025 gimple *use_stmt;
1026
1027 /* If the PHI's result is never used, then we can just
1028 ignore it. */
1029 if (has_zero_uses (result))
1030 continue;
1031
1032 /* Get the single use of the result of this PHI node. */
1033 if (!single_imm_use (result, &imm_use, &use_stmt)
1034 || gimple_code (use_stmt) != GIMPLE_PHI
1035 || gimple_bb (use_stmt) != dest
1036 || gimple_phi_arg_def (use_stmt, dest_idx) != result)
1037 break;
1038 }
1039
1040 /* If the loop above iterated through all the PHI nodes
1041 in BB, then we can merge the PHIs from BB into DEST. */
1042 if (gsi_end_p (gsi))
1043 *current++ = bb;
1044 }
1045 }
1046
1047 /* Now let's drain WORKLIST. */
1048 bool changed = false;
1049 while (current != worklist)
1050 {
1051 bb = *--current;
1052 changed |= remove_forwarder_block_with_phi (bb);
1053 }
1054 free (worklist);
1055
1056 /* Removing forwarder blocks can cause formerly irreducible loops
1057 to become reducible if we merged two entry blocks. */
1058 if (changed
1059 && current_loops)
1060 loops_state_set (LOOPS_NEED_FIXUP);
1061
1062 return 0;
1063 }
1064
1065 } // anon namespace
1066
1067 gimple_opt_pass *
1068 make_pass_merge_phi (gcc::context *ctxt)
1069 {
1070 return new pass_merge_phi (ctxt);
1071 }
1072
1073 /* Pass: cleanup the CFG just before expanding trees to RTL.
1074 This is just a round of label cleanups and case node grouping
1075 because after the tree optimizers have run such cleanups may
1076 be necessary. */
1077
1078 static unsigned int
1079 execute_cleanup_cfg_post_optimizing (void)
1080 {
1081 unsigned int todo = execute_fixup_cfg ();
1082 if (cleanup_tree_cfg ())
1083 {
1084 todo &= ~TODO_cleanup_cfg;
1085 todo |= TODO_update_ssa;
1086 }
1087 maybe_remove_unreachable_handlers ();
1088 cleanup_dead_labels ();
1089 group_case_labels ();
1090 if ((flag_compare_debug_opt || flag_compare_debug)
1091 && flag_dump_final_insns)
1092 {
1093 FILE *final_output = fopen (flag_dump_final_insns, "a");
1094
1095 if (!final_output)
1096 {
1097 error ("could not open final insn dump file %qs: %m",
1098 flag_dump_final_insns);
1099 flag_dump_final_insns = NULL;
1100 }
1101 else
1102 {
1103 int save_unnumbered = flag_dump_unnumbered;
1104 int save_noaddr = flag_dump_noaddr;
1105
1106 flag_dump_noaddr = flag_dump_unnumbered = 1;
1107 fprintf (final_output, "\n");
1108 dump_enumerated_decls (final_output, dump_flags | TDF_NOUID);
1109 flag_dump_noaddr = save_noaddr;
1110 flag_dump_unnumbered = save_unnumbered;
1111 if (fclose (final_output))
1112 {
1113 error ("could not close final insn dump file %qs: %m",
1114 flag_dump_final_insns);
1115 flag_dump_final_insns = NULL;
1116 }
1117 }
1118 }
1119 return todo;
1120 }
1121
1122 namespace {
1123
1124 const pass_data pass_data_cleanup_cfg_post_optimizing =
1125 {
1126 GIMPLE_PASS, /* type */
1127 "optimized", /* name */
1128 OPTGROUP_NONE, /* optinfo_flags */
1129 TV_TREE_CLEANUP_CFG, /* tv_id */
1130 PROP_cfg, /* properties_required */
1131 0, /* properties_provided */
1132 0, /* properties_destroyed */
1133 0, /* todo_flags_start */
1134 TODO_remove_unused_locals, /* todo_flags_finish */
1135 };
1136
1137 class pass_cleanup_cfg_post_optimizing : public gimple_opt_pass
1138 {
1139 public:
1140 pass_cleanup_cfg_post_optimizing (gcc::context *ctxt)
1141 : gimple_opt_pass (pass_data_cleanup_cfg_post_optimizing, ctxt)
1142 {}
1143
1144 /* opt_pass methods: */
1145 virtual unsigned int execute (function *)
1146 {
1147 return execute_cleanup_cfg_post_optimizing ();
1148 }
1149
1150 }; // class pass_cleanup_cfg_post_optimizing
1151
1152 } // anon namespace
1153
1154 gimple_opt_pass *
1155 make_pass_cleanup_cfg_post_optimizing (gcc::context *ctxt)
1156 {
1157 return new pass_cleanup_cfg_post_optimizing (ctxt);
1158 }
1159
1160